views:

265

answers:

2

I want to rename columns in a PowerDesigner LDM/PDM according to the state of the Primary checkbox. To be more specific, I want the column renamed from "oldname" to "id_oldname" if Primary is checked.

I think it is possible with a custom check/autofix script like the one in "MSSQLSRV2008::Profile\Column\Custom Checks\Identity Seed and Increment validity" but I'm really not a VBScript expert :)

Is there an easy way to achieve that in PowerDesigner 15?

A: 

One can write the VBScript to trawl the object model, looking for columns that are members of primary keys and renaming them.

Or:

  • One can bring up a list of columns (Right click on a package or the model, List of -> Columns)

  • Press control-U (or click on the funnel-and-pencil icon) to bring up Customize Columns and Filter.

    • Find the Primary Key entry in the Column Heading column.
    • In the Filter Expression, set the expression to True.
    • Check the U column for the Primary Key entry, and make sure no other rows have that checked.
    • Press Apply.

You should now see all the columns that are part of keys. You can similarly filter with wildcards for non-compliant names. You can also select multiple rows and rename them at the same time.

Adam Musch
Exactly. The second one is not a solution, since the Primary Key column is already visible. I don't want to "see" them, I want them renamed. And this is where the firs option kicks in.I would have created a script if I was able but as I already mentioned, I am not a VBScript expert and it has not changed since then :) I need help with it.
At some point, you have to do the work to learn VBScript -- I'm not running someone else's VBScript on my datamodel, thankyouverymuch -- or do the work to rename them manually. At least the second solution showed you how to highlight them to get them in a single place.
Adam Musch
I don't think I should learn VBScript just for that single task. "Not running someone else's VBScript on my datamodel" is a very narrow-minded approach. It would be like not accepting the help I asked for, wouldn't it? I'm not going to reinvent the wheel, that is considered an inefficient waste of time :) But this conversation leads astray, we should stop flaming each other. I already got some piece of code (someone else's) though it does not work yet. I'll publish it later if I can fix it.
A: 

The solution is quite simple. Unfortunately not a flawless one, because PowerDesigner fails to execute the event handler in some cases (for some unknown reason). The small vbscript below should be able to handle it. An extended model definition file is to be created and attached to the project. The script is a Validate Event Handler of the Table metaclass (though it's rather an autofix than a validation).

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 
         if left(col.name,3) <> "id_" then 
            With col 
               .name = "id_" & .name 
               .SetNameAndCode .Name, "", True 
            End With 
         end if 
      else 
         if left(col.name,3) = "id_" then 
            with col 
               .name = right(.name, len(.name)-3) 
               .SetNameAndCode .Name, "", True 
            end with 
         end if 
      end if 
   next 
   %Validate% = True
End Function 

The credit goes to Richard Kier who supplied the original code. Thanks, Richard.

You could also provide the function as a custom command, so that it's easier to call it that launching a Check Model.
pascal