如何以编程方式从 sql 查询中获取字段的值?

How to programatically get the value of a field from a sql query?

我正在编写一个脚本来查询 t_object 和 t_object 属性表并迭代结果元素。 这其实相当简单。我正在使用这个:

function find_model_doc() {
var query = "select o.Object_ID, o.Name, op.Value from t_objectproperties op inner join (select * from t_object where stereotype = 'model document') o on o.Object_ID = op.Object_ID where op.Property = 'RTFTemplate'"
var all_modeldocs = Repository.GetElementSet(query, 2);
for (var o = 0; o < all_modeldocs.Count; o++) {
    elem_name = all_modeldocs.GetAt(o).Name;
    elem_value = all_modeldocs.GetAt(o).Value;
    Session.Output("name|value: " + elem_name + " | " + elem_value)
}}

如果我在 'SQL scratch pad' 中使用这个查询,我得到:

但是,使用脚本我似乎无法从 'Value' 字段中获取字符串。 对我如何做到这一点有什么建议吗?

方法 EA.Repository.GetElementSet(query, 2) returns 一个 EA.Collection 类型为 EA.Element 的项目。它根据您的查询返回的 Object_ID 这样做。忽略所有其他返回值。

这些对象没有 Value 属性。

您似乎要查找的是元素上定义的名称为“RTFTemplate”的标记值的值。

您可以通过两种方式做到这一点

迭代 GetElementSet 返回的元素的标记值

像这样 (vbscript)

for each element in all_modeldocs
    elem_name = element.Name
    for each tag in element.TaggedValues
        if tag.Name = "RTFTemplate" then
            elem_value : tag.Value
        end if
    next
next

使用Repository.SQLQuery并解析生成的xml字符串

像这样 (vbscript)

function getArrayListFromQuery(sqlQuery)
    dim xmlResult
    xmlResult = Repository.SQLQuery(sqlQuery)
    set getArrayListFromQuery = convertQueryResultToArrayList(xmlResult)
end function

Function convertQueryResultToArrayList(xmlQueryResult)
    Dim result
    set result = CreateObject("System.Collections.ArrayList")
    Dim xDoc 
    Set xDoc = CreateObject( "MSXML2.DOMDocument" )
    'load the resultset in the xml document
    If xDoc.LoadXML(xmlQueryResult) Then        
        'select the rows
        Dim rowList
        Set rowList = xDoc.SelectNodes("//Row")
        Dim rowNode 
        Dim fieldNode
        'loop rows and find fields
        For Each rowNode In rowList
            dim rowArrayList
            set rowArrayList = CreateObject("System.Collections.ArrayList")
            'loop the field nodes
            For Each fieldNode In rowNode.ChildNodes
                'add the contents
                rowArrayList.Add fieldNode.Text
            Next
            'add the row the the general list
            result.Add rowArrayList
        Next
    end if
    set convertQueryResultToArrayList = result
end function