vbscriptpowerdesigner

How to create a Trigger and assign a TriggerTemplate using PowerDesigner VBScript?


I'm using PowerDesigner v16 and I created an Extension; in this Extension a MetaClass of the Table type was added; in this MetaClass an Event Handler was created, using VBScript, to go through all the tables and create some fields / columns (attributes) default in all tables.

But now I want to take advantage of that I'm going through all the tables and also through VBScript to create a standard Trigger that is in TriggerTemplate, and I don´t know how to do this through VBScript.

My main question is: how to create a Trigger using VBScript and assign a TriggerTemplate?

Can you provide me with an example?

Possibly related question:

Note: This function is performed in a PowerDesigner Extencion using PDM modeling. And the path of this extension is as follows: DEFAULT COLUMNS::Profile\Table\Event Handlers\Initialize


'******************************************************************************
' Função para checar se a coluna já existe na tabela.
'******************************************************************************
function ColumnExists(tab, name)
   'output "ClassName: " + tab.ClassName
   
   'Checa se a o objeto passano no parâmetro "tab" é do tipo Table (tabela)
   if tab.ClassName = "Table" then
      dim col
      'Passa por todas as colunas da tabela
      for each col in tab.Columns
         'Checa se o nome da coluna atual já existe igual ao passado por parâmetro ("name")
         if col.Name = name then
            'output "... já existe " + col.Name
            ColumnExists = true
            exit function
         end if
      next 
   end if
   ColumnExists = false
end function

'******************************************************************************
' Função responsável por criar as colunas padrao de uma tabela.
'******************************************************************************
Sub DoCreateColumns(tab)
   
   ' Checa se o objeto passado no parâmetro ("tab") é do tipo "Table"
   if not tab.ClassName = "Table" then exit sub
   dim c
   dim myColumns, column

 
   ' Executa função "DefaultColumns()" serve para criar um array com todas as colunas padrão
   myColumns = DefaultColumns()
   
   'Passa por todas as colunas salvas'
   for each column in myColumns
       
       'Checa se esta coluna é um ID
       if column.Name = "ID_" then
            ' Adiciona o nome da tabela junto com a palavra ID
            column.Name = "ID_" + tab.Name
            column.Code = column.Name
       end if
       
       'Checa se a coluna ja existe
       if not ColumnExists(tab, column.Name) then
           set c = tab.Columns.CreateNewAt(column.Position) 
           c.Name = column.Name
           c.Code = column.Code
           c.domain = column.Domain
           c.Mandatory = column.Mandatory
           output "... adding column " + column.Name + " table " + tab.Name
       end if
   next
    
End Sub



Solution

  • I created a SAP SQL Anywhere 17 PDM with one table, saved it as a .pdm file; then added a trigger based on a template on this table, and saved the model as a new .pdm file. By comparing the files, I get some hints of the representation of triggers + templates.

    Especially that the trigger template is attached to the trigger. Through a shortcut, as the template is in the DBMS while the trigger is in the model.

    <o:Table Id="o9">
        <a:Name>Table_1</a:Name>
        <a:Code>TABLE_1</a:Code>
        <c:Triggers>
            <o:Trigger Id="o10">
                <a:Name>Trigger_1</a:Name>
                <a:Code>TRIGGER_1</a:Code>
                <c:BaseTrigger.TriggerItems>
                    <o:Shortcut Ref="o5"/>
                    <o:Shortcut Ref="o6"/>
                </c:BaseTrigger.TriggerItems>
                <c:TriggerTemplate>             <===
                    <o:Shortcut Ref="o4"/>      <===
                </c:TriggerTemplate>
            </o:Trigger>
        </c:Triggers>
    

    Looking at the help file SAP PowerDesigner 16 OLE Help, I see the TriggerTemplate as a property of the BaseTrigger class, from which Trigger is derived.

    Here is an example which uses that.

    option explicit
    ' create model
    dim mdl : set mdl = CreateModel(PDPdm.cls_PdmModel, "|DBMS=SAP SQL Anywhere 17")
    ' create table and trigger
    dim tbl : set tbl = mdl.CreateObject(PDPdm.cls_Table)
    dim trig : set trig = tbl.CreateObject(PDPdm.cls_Trigger)
    ' set trigger template
    SetTemplate trig, "BeforeUpdateTrigger"
    
    function SetTemplate(trg, typ)
       SetTemplate = false
       ' find template
       dim db : set db = trg.Model.DBMS
       ' in case of shared DBMS instead of embedded one
       if db.IsShortcut() then set db = db.TargetObject
       dim tm, found
       for each tm in db.TriggerTemplates
          if tm.name = typ then
             set found = tm
             exit for
          end if
       next
       if IsEmpty(found) then exit function
       ' create shortcut alongside the table
       dim fld : set fld = trg.Parent.Folder
       dim short : set short = found.CreateShortcut(fld)
       ' assign, and initialize
       set trg.TriggerTemplate = short
       trg.InitFromTemplate
       SetTemplate = true
    end function