Book a Demo

Author Topic: understanding SQL access  (Read 8934 times)

ngong

  • EA User
  • **
  • Posts: 275
  • Karma: +2/-2
    • View Profile
understanding SQL access
« on: September 07, 2021, 06:17:10 pm »
I wonder why I am not able to access t_diagram by

Code: [Select]
!INC Local Scripts.EAConstants-JScript

 function getClassDiags() {
var ids = Repository.GetElementSet(
"select Diagram_ID from " +
"t_diagram"
,2)
return ids.Count;
 }
 
function main()
{
var diags = getClassDiags();
Session.Output(diags + " finished");
}

main();

Whereas
Code: [Select]
!INC Local Scripts.EAConstants-JScript

 function getClassDiags() {
var ids = Repository.GetElementSet(
"select Object_ID from " +
"t_object"
,2)
return ids.Count;
 }
 
function main()
{
var diags = getClassDiags();
Session.Output(diags + " diagrams");
}

main();

works fine.

I am looking at the create statements of t_object and t_diagram in EASchema_1558_MySQL, which I believe shall describe the JET4 database of the eapx file as well.
Rolf

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: understanding SQL access
« Reply #1 on: September 07, 2021, 06:25:35 pm »
GetElementSet only works to Get a Set of Elements. It doesn't work for diagrams, packages, attributes, connectors, etc...

For those types of objects you'll need to use Repository.SQLQuery, parse the resulting xml and get the id's, and then use the appropriate Repository.GetxxxByID() to get the objects.

For examples see https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs

To get attributes:

Code: [Select]
function getAttributesFromQuery(sqlQuery)
dim xmlResult
xmlResult = Repository.SQLQuery(sqlQuery)
dim attributeIDs
attributeIDs = convertQueryResultToArray(xmlResult)
dim attributes
set attributes = CreateObject("System.Collections.ArrayList")
dim attributeID
dim attribute as EA.Attribute
for each attributeID in attributeIDs
if attributeID > 0 then
set attribute = Repository.GetAttributeByID(attributeID)
if not attribute is nothing then
attributes.Add(attribute)
end if
end if
next
set getattributesFromQuery = attributes
end function

Geert

ngong

  • EA User
  • **
  • Posts: 275
  • Karma: +2/-2
    • View Profile
Re: understanding SQL access
« Reply #2 on: September 11, 2021, 02:26:34 pm »
Yes, thank you Geert. Some feedback:
I started with your suggestion - realized that I do not have ActiveX in JavaScript - but in JScript. I could not get ActiveX in JScript work for me. However, as the xmlResult is fairly regular, regex is doing well by switching back to JavaScript:

Code: [Select]
!INC Local Scripts.EAConstants-JScript

function getDiagramIDFromQuery(sqlQuery) {
r = [];
xmlResult = Repository.SQLQuery(sqlQuery);
m = xmlResult.split('<Diagram_ID>');
for (i=1; i<m.length; i++) {
id = m[i].match(/^(\d+).*/)[1];
r.push(id);
}
return r;
}

function main()
{
diagIds = getDiagramIDFromQuery(
"select Diagram_ID from " +
"t_diagram t " +
"where t.Diagram_Type = 'Logical' " +
"and t.Name like '*Overview*' "
);
for(id of diagIds) {
diag = Repository.GetDiagramByID(id);
Session.Output(diag.Name);
}
}

main();

This selects the diagram ids of Class diagrams exposing the word "Overview" in its name.
« Last Edit: September 11, 2021, 02:43:54 pm by ngong »
Rolf