you're reading...
PowerDesigner, XEM

Validation Events

I answered a great question on the PowerDesigner usenet group today.  The question was how to automatically add an “id_” prefix to a column name and code when the Primary Key attribute was set.  In the past, I would’ve had to do this with a script, and probably a custom model check, but with PD15 we have events, which allow us to perform scripts automatically when certain actions are taken.

I thought this would be a good topic to elaborate on since a) I can include pictures and bit more formatting control than I can on the usenet group and b) I’m actually home, the wife is out, and the kids are asleep.  Anyway, here’s the solution.

1.  Open or add an extended model definition file.  For information on how to add an XEM file see the Sybase documentation.

2.  Add the table metaclass to the file.  Even though we actually want to validate a column property, the column validation event seems to only fire when a column is created, so we’ll put our validation event on the table and loop through the columns when the table is changed.

3.  Add a validation event handler to the table.

4.  Put this code into the handler script

Function %Validate%(obj, ByRef message)
 ' Implement your object validation rule on <parent> here
 ' and return True in case of success, False otherwise with a message

 dim col

 for each col in obj.columns

    if col.Primary = true then
       ' check to see if the id_ is already there
       if left(col.name,3) = "id_" then
          %Validate% = True
          ' if it is not there add it to the name and set the code using the current abbreviation file
          With col
             .name = "id_" & .name
             .SetNameAndCode .Name, "", True
          End With

          %Validate% = True
       end if
       ' if the primary attribute is false, see if we need to remove an id_ prefix
       if left(col.name,3) = "id_" then
          with col
             .name = right(.name, len(.name)-3)
             .SetNameAndCode .Name, "", True
          end with
          %Validate% = True
          %Validate% = True
       end if
    end if


 %Validate% = True

End Function

All done.  Note that this could be combined with some other techniques I’ve posted before.  For example, much of this code could be placed in a method on the column itself, then called in both a custom model check and a model level script using execute custom method.  However, the key advantage of validations is that, as long as people have the correct XEM attached (it can also be done in the XDB file), you can make sure that validations take place as an object is edited, which saves you the trouble of constantly reminding people to execute all the custom model checks BEFORE they generate DDL.

Best of Luck.



2 thoughts on “Validation Events

  1. Ho can I compare value of variable before validate and current ? eg: Something on column was changed and I want to know if user changed Name => this is forbiden, other changes are OK.

    Posted by Pavel | March 22, 2018, 8:33 am
  2. Interesting question, I’m not sure if there’s a ‘before’ state available outside of a transform. You could probably get the desired effect with extended attributes. I don’t think there’s a solution robust enough to stop it completely but you could make it harder to accidentally change it. When you think your model is stable, write the current name value to an extended attribute (I’d create a Lock method and menu to handle that). Then you have a before state to compare against. It could be changed in extended attributes, but it would stop accidental changes. You could make it more elaborate with custom forms, but I’m not sure there’s a way to completely lock it down.

    If I can think of anything more robust I’ll post it here.

    Posted by Rich Kier | March 23, 2018, 7:02 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: