Search code examples
database-designrenamepowerdesigner

Auto-renaming columns in PowerDesigner LDM/PDM


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?


Solution

  • 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.