如何使用Enterprise Architect VBScript访问FK连接器的关联列?
在Enterprise Architect VBScript中访问PDM外键连接器的关联列
完全可行,通过EA的API结合属性标签值或直接查询底层数据库,就能获取外键连接器关联的子表外键列和父表主键列,以下是具体实现方案:
方法一:通过EA对象模型+标签值获取
步骤说明
- 定位目标外键连接器
- 关联子表(外键所在表)和父表(主键所在表)对象
- 遍历子表属性,筛选外键列并匹配关联的父表主键列
代码示例
' 获取当前选中的外键连接器 Dim selectedConnector As EA.Connector Set selectedConnector = Repository.GetContextObject() ' 校验是否为外键连接器 If selectedConnector.Type <> "Foreign Key" Then MsgBox "请选择一个外键连接器" Exit Sub End If ' 获取子表和父表对象 Dim childTable As EA.Element Dim parentTable As EA.Element Set childTable = Repository.GetElementByID(selectedConnector.ClientID) Set parentTable = Repository.GetElementByID(selectedConnector.SupplierID) ' 遍历子表属性,提取外键列及关联主键列 MsgBox "外键关联信息:" & vbCrLf & "子表:" & childTable.Name & " -> 父表:" & parentTable.Name Dim attr As EA.Attribute For Each attr In childTable.Attributes If attr.IsForeignKey Then ' 获取关联的父表主键列标识(标签值格式为"父表GUID::主键列GUID") Dim fkParentTag As EA.TaggedValue Set fkParentTag = attr.TaggedValues("PDM.ForeignKeyParent") If Not fkParentTag Is Nothing Then Dim tagParts() As String tagParts = Split(fkParentTag.Value, "::") If UBound(tagParts) = 1 Then Dim parentAttr As EA.Attribute Set parentAttr = Repository.GetAttributeByID(CInt(tagParts(1))) If Not parentAttr Is Nothing Then MsgBox vbCrLf & "- 外键列:" & attr.Name & " -> 父表主键列:" & parentAttr.Name End If End If End If End If Next
方法二:通过SQL查询直接获取(高效适合批量处理)
EA底层存储为SQL数据库,直接查询可快速获取关联关系,避免遍历对象的性能损耗:
代码示例
' 获取当前选中的外键连接器ID Dim selectedConnector As EA.Connector Set selectedConnector = Repository.GetContextObject() If selectedConnector.Type <> "Foreign Key" Then MsgBox "请选择一个外键连接器" Exit Sub End If ' 构造查询SQL Dim sql As String sql = "SELECT child_e.Name AS ChildTable, child_a.Name AS FKColumn, " & _ "parent_e.Name AS ParentTable, parent_a.Name AS PKColumn " & _ "FROM t_connector c " & _ "INNER JOIN t_object child_e ON c.ClientID = child_e.Object_ID " & _ "INNER JOIN t_attribute child_a ON child_e.Object_ID = child_a.Object_ID " & _ "INNER JOIN t_object parent_e ON c.SupplierID = parent_e.Object_ID " & _ "INNER JOIN t_attribute parent_a ON parent_e.Object_ID = parent_a.Object_ID " & _ "INNER JOIN t_attributetag tag ON child_a.AttributeID = tag.AttributeID " & _ "WHERE c.Connector_Type = 'Foreign Key' " & _ "AND tag.Property = 'PDM.ForeignKeyParent' " & _ "AND tag.Value LIKE '%::' + CAST(parent_a.AttributeID AS VARCHAR) + '%' " & _ "AND c.Connector_ID = " & selectedConnector.ConnectorID ' 执行查询并解析XML结果(EA SQLQuery返回XML格式) Dim xmlResult As String xmlResult = Repository.SQLQuery(sql) Dim xmlDoc As Object Set xmlDoc = CreateObject("MSXML2.DOMDocument") xmlDoc.LoadXML(xmlResult) Dim rowNodes As Object Set rowNodes = xmlDoc.SelectNodes("//Row") Dim rowNode As Object For Each rowNode In rowNodes MsgBox "子表:" & rowNode.SelectSingleNode("ChildTable").Text & vbCrLf & _ "外键列:" & rowNode.SelectSingleNode("FKColumn").Text & vbCrLf & _ "父表:" & rowNode.SelectSingleNode("ParentTable").Text & vbCrLf & _ "主键列:" & rowNode.SelectSingleNode("PKColumn").Text Next
注意事项
- 确保目标连接器的
Type属性为Foreign Key,避免处理其他类型的连接器 PDM.ForeignKeyParent标签值的格式可能因EA版本略有差异,测试时可先输出标签值确认格式- 多列复合外键的场景下,每个外键列都会对应一个关联父表主键列的标签值
内容的提问来源于stack exchange,提问作者PGB
相关产品推荐
相关产品推荐

