Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: pianoman on November 27, 2019, 12:08:52 pm
-
Hi All,
I have a script running multiple functions to automate a lot of things among which creation of instances is one of them. I have a SQL Query that specifies the ClassifierIDs from which to create instances. This script works great and all, but I want to add one more level of complexity.
I want to make sure the script doesn't create instances if there is a case that there is an instance with a specified ClassifierID existing already. My function below has inputs of classifierID, stereotype, and name provided from a query in another function. I'm looking for help in figuring out the if statement to check for an existing instance with a classifierID that will be specified by my query.
function addInstance(classifierID, stereotype, name)
Dim package
Dim newinstance AS EA.Element
set package = Repository.GetTreeSelectedPackage()
if newinstance.ClassifierID exists for some instance in the model
do nothing
else if
do the below things
end if
' create the new instance element
set newinstance = package.Elements.AddNew("", stereotype)
' assign a classifier element to the new instance element
newinstance.ClassifierID = classifierID
' assign a name to the new instance element
newinstance.Name = name
' update and refresh for the push to happen to EA
newinstance.Update
package.Elements.Refresh
addInstance = newinstance.ElementID
End Function
Appreciate any help! Thanks!
-
Use Repository.SqlQuery with a query similar to this:
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.
Alternative is to use Repository.GetElementSet with the same query.
That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.
Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs) for lots of utility functions that use SQLQuery
Geert
-
Use Repository.SqlQuery with a query similar to this:
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.
Alternative is to use Repository.GetElementSet with the same query.
That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.
Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs) for lots of utility functions that use SQLQuery
Geert
If you just want to know if there are records, I think you can say something like SELECT Count(*) AS Num FROM ... etc and then see if Num is non-zero. Sorry I don't remember the exact syntax.
-
Use Repository.SqlQuery with a query similar to this:
select o.Object_ID from t_object o where o.Classifier = 123
This returns an xml string with the results of the query that you can parse then.
Alternative is to use Repository.GetElementSet with the same query.
That returns a collection of elements. This is a bit slower, especially if there are a lot of elements to be returned.
Check also https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library/blob/master/Framework/Utils/Util.vbs) for lots of utility functions that use SQLQuery
Geert
If you just want to know if there are records, I think you can say something like SELECT Count(*) AS Num FROM ... etc and then see if Num is non-zero. Sorry I don't remember the exact syntax.
Yes indeed. The syntax is just fine. Just don't forget the alias 'AS Num'.
If you don't you get a very cryptic xml error message because EA tries to create an xml node without a name.
Geert