天天看點

(原創)在ER/Studio中使用宏把Attribute name複制到Definition

最近在處理ER/Studio生成SQL腳本時發現,如果在Definition處沒有定義,那麼在生成SQL腳本後就沒有表和字段的注釋。

是以就寫了一個宏來實作這個功能,代碼如下:

把以下代碼儲存為XXX.bas檔案放到ER/Studio安裝目錄下的Macros的檔案夾下,ER/Studio就可以自動加載到Macros頁籤中,如:C:\Program Files\Embarcadero\ERStudio6.5\Macros

Dim EntCount As Integer

Dim ColCount As Integer

Dim MyDiagram As Diagram

Dim MyModel As Model

Dim MyEntity As Entity

Dim MyAttribute As AttributeObjDim TableArray() As String

Dim ColArray() As String

Function getColumns(TableName As String )

Dim Indx As Integer

Dim count As Integer

count = 1

Indx = 0

Set MyEntity = MyModel.Entities.Item(TableName)

ColCount = MyEntity.Attributes.Count

ReDim ColArray(0 To ColCount) As String

For count=1 To ColCount

  For Each MyAttribute In MyEntity.Attributes

    If MyAttribute.SequenceNumber = count Then

      If MyModel.Logical = True Then

        If MyAttribute.HasLogicalRoleName = True Then

          ColArray(Indx) = MyAttribute.LogicalRoleName

      Else

        ColArray(Indx) = MyAttribute.AttributeName

      End If

    Else

      If MyAttribute.HasRoleName = True Then

        ColArray(Indx) = MyAttribute.RoleName

        ColArray(Indx) = MyAttribute.ColumnName

    End If

    MyAttribute.Definition = ColArray(Indx)

    Indx= Indx +1

  End If

  Next MyAttribute

  Next count

End Function

Sub Main

Debug.Clear

Set MyDiagram = DiagramManager.ActiveDiagram

Set MyModel = MyDiagram.ActiveModel

EntCount = MyModel.Entities.Count - 1

ReDim TableArray(0 To EntCount) As String

For Each MyEntity In MyModel.Entities

  If MyModel.Logical = True Then

    TableArray(Indx) = MyEntity.EntityName

  Else

    TableArray(Indx) = MyEntity.TableName

  MyEntity.Definition = TableArray(Indx)

  getColumns(TableArray(Indx))

  Indx = Indx +1

Next MyEntity

End Sub