So I want to have EA automatically add a check constraint, for any column in a modelled database table that uses a particular data type; in my case, the (added) datatype "dbo.adtYesNo".
I made myself a nice script. I then select all the database tables on the diagram, and run the script. The script works great for processing all the tables, checking all the columns, and it can alter/update the check constraints that it finds.
The problem is that the script cannot create a new check constraint, if there is currently none. And that is the biggest part of what I wanted to be automatically handled. The following is the related code fragment; the comments for "PROBLEM" note the various properties in the EA.Method object that EA does not allow me to set.
dim newMethod as EA.Method
'This line works to create a new EA.Method object, but that object is useless
'because:
' - FQStereotype is set to "", it is a read-only property, and that property must be set.
set newMethod = myElement.Methods.AddNew(requiredCheckConstraintName, "")
'This line works, but the resulting EA.Method object is useless because FQStereotype is "".
' ReturnType is also incorrectly set to "EAUML::check"; it needs to be "".
'set newMethod = myElement.Methods.AddNew(constraintName, "EAUML::check")
' For the new EA.Method object, set the various properties.
newMethod.Code = requiredCheckConstraintExpression
newMethod.Concurrency = "Sequential"
'newMethod.FQStereotype = "EAUML::check" - PROBLEM: this property must be set, but it is read-only
'newMethod.MethodGUID - need to set?
'newMethod.MethodID - need to set? - PROBLEM: this property likely needs be set, as the value is currently 0, but it is read-only
'newMethod.Name - this is already set; good
'newMethod.Notes - not needed
'newMethod.ObjectType = 24 'From EAConstants-VBScript: otMethod = 24 - PROBLEM: this property must be set, but it is read-only
'newMethod.Parameters - - PROBLEM: this collection must have an object added, to list the affected table column, but it is documented as read-only in
https://www.sparxsystems.com/enterprise_architect_user_guide/15.2/automation/method.html'newMethod.ParentID = myElement.ElementID ' - PROBLEM: need to set the table ElementID into newMethod.ParentID, but it is read-only
newMethod.Pos = myElement.Methods.Count ' Pos is 0-based. Use next value.
'newMethod.PostConditions - will be left empty
'newMethod.PreConditions - will be left empty
newMethod.Stereotype = "check"
newMethod.StereotypeEx = "check"
'newMethod.Style and StyleEx - will be left as empty string
'newMethod.TaggedValues - will be left empty
'newMethod.TypeInfoProperties - might not need to be set
newMethod.Visibility = "Public"
' Save the Method object
newMethod.Update()
myElement.Methods.Refresh()
dim retrievedMethod as EA.Method
retrievedMethod = myElement.Methods.GetByName(requiredCheckConstraintName)
dim newParameter as EA.Parameter
newParameter = retrievedMethod.Parameters.AddNew(attribute.Name, attribute.Type)
newParameter.ClassifierID = "0"
newParameter.Kind = "in"
'newParameter.Name - should already be set
'newParameter.Notes - not needed
newParameter.ObjectType = 25 ' From EAConstants-VBScript: otParameter = 25
newParameter.OperationID = retrievedMethod.MethodID
'newParameter.ParameterGUID - ?
newParameter.Position = 0 ' Pos is 0-based
'newParameter.Stereotype and Stereotype - ""
'newParameter.Style and StyleEx - ""
'newParameter.TaggedValues - will be left empty
newParameter.Type = attribute.Type
'newParameter.TypeInfoProperties - might not need to be set
newParameter.Update()
retrievedMethod.Parameters.Refresh()
Any ideas?
I had a look at Geert's framework for add-ons. Geert has code there for check constraints; I don't know if his framework is only capable of reading existing check constraints, of if it can successfully create new ones also.
Thanks.