Book a Demo

Author Topic: Batch adding check constraint for set of columns  (Read 4337 times)

peigee

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Batch adding check constraint for set of columns
« on: November 12, 2015, 03:14:40 pm »
Tasked to create a script (JSERV) to add check constraint for columns of all tables under a package/sub-package whose name ending '_IND' and of CHAR data type.

I know how to write the script to navigate a selected package and to create a check constraint for columns meeting the criteria as shown below:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.......
// Add check constraint for the attribute intended
                  if ( currentAttribute.Name.substring(currentAttribute.Name.length-4)== "_IND" && currentAttribute.Type == "CHAR" )
                  {
                        //checking if the check constriant already exists
                        var chk_exists = false;
                        for( var j = 0; j < methodsCollection.Count; j++ )
                        {
                              var method as EA.Method;
                              method = methodsCollection.GetAt( j );
                                    
                              if( method == null )
                                    continue;
                              //Session.Output("method: "+method.Name);
                              if ( method.Name == "CK_"+ currentAttribute.Name.substring(0,Math.min(currentAttribute.Name.length,27)) )
                              {
                                    chk_exists = true;
                                    break;
                              }
                        }
                        if ( chk_exists == false )
                        {
                              var Methods as EA.Collection;
                              Methods = theElement.Methods;
                              var newMethod as EA.Method;
                              newMethod = Methods.addNew( "CK_"+currentAttribute.Name.substring(0,Math.min(currentAttribute.Name.length,27)),"");
                              newMethod.Stereotype="check"
                              newMethod.Code=currentAttribute.Name+" in ('Y','N')"
                              newMethod.Update();
                              Methods.Refresh();
                              Session.Output( "  Added Check constraint: " + newMethod.Name +
                        " (Type=" + newMethod.Stereotype +
                              ", ID=" + newMethod.MethodID + ")" );
                        }
                        else
....
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

However, I don't know what object/API to use to specify the related column for the check constraint created, as one can do manually via table properties, in other words, how to add a record into the SPARX repository table t_operationparams (information only, it does not actually prevent me to generate correct check constraint under ORACLE target platform once the check constraint is created. Attempted the following however, it had not worked:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
......
var Methods as EA.Collection;
                              Methods = theElement.Methods;
                              var newMethod as EA.Method;
                              newMethod = Methods.addNew( "CK_"+currentAttribute.Name.substring(0,Math.min(currentAttribute.Name.length,27)),"");
                              newMethod.Stereotype="check"
                              newMethod.Code=currentAttribute.Name+" in ('Y','N')"
                              newMethod.Update();
                              Methods.Refresh();
                              [highlight]var MethodsPara as EA.Collection;
                              MethodsPara = newMethod.Parameters
                              var newMethodPara as EA.Parameter;
                              newMethodPara=MethodsPara.AddNew(currentAttribute.Name,"CHAR");[/highlight]
                            Session.Output( "  Added Check constraint: " + newMethod.Name +
                        " (Type=" + newMethod.Stereotype +
                              ", ID=" + newMethod.MethodID + ")" );
.....
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Could anyone shed some light on me? Many thanks in advance.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Batch adding check constraint for set of colum
« Reply #1 on: November 12, 2015, 04:04:55 pm »
It should be done using EA.Method.Parameters.AddNew.
You might need to play with the parameters a bit to get it right. Often you can only pass the name in the AddNew.
In that case you'll have to set the type of the parameter (CHAR) later on.

But I don't see a newMethodPara.Update() anywhere.
that might be the problem.
Also make sure the also call Method.Update(). Sometimes it seems you have to save the parent object in order to "really" save the owned object.

Geert

peigee

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Batch adding check constraint for set of colum
« Reply #2 on: November 12, 2015, 04:19:39 pm »
Thank you Geert for your promptly and helpful response.
My code is working now after adding the missing call newMethodPara.Update(); (how could I have missed - thanks for the third eye!).