Getting started with Scripts and Macros

Session EDM03LNA / EDM03LNB Enterprise Systems Management

Copyright © 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Abstract
After this lab you will know the difference between model and table level scripts, attached and unattached scripts, when to use what type of script, and how you can make all your scripts work together. Starting with an easy “Hello World” script to get the hang of it, we will switch to some simple iteration through tables and columns, followed by more complex iterations using relations, subroutines and nesting.

Copyright © 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Biography
Theo van Westrienen Martinair Holland
 Working with ERwin since 1999  President and founding member of eBUG (ERwin BeNeLux User Group) since 2004  Working in IT since 1987, in various industries such as telecom, banking, car industry, Dutch flower, fruit and vegetables auctions  Currently as Data Manager for airliner Martinair

Copyright © 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

trade names. . Sample Model  Traditional Forward Engineering 2. service marks and logos referenced herein belong to their respective companies.Exercises 1. All rights reserved.Agenda . Simple Navigation 6. Hello World Script 3. More Navigation Copyright © 2007 CA. All trademarks. Apply FE    Pre and Post Scripts Table and Model Level Attached and Unattached 5. Using a Simple Macro 4.

Subroutines 13. More Iteration 9. Nested Iteration 10. All trademarks.Iteration + Selection 12.Selection 11.Agenda . All rights reserved.Exercises 7.Filtering 14. trade names.Finale – A Complete Script Copyright © 2007 CA. Iteration 8. . service marks and logos referenced herein belong to their respective companies.

trade names.er1  Contains simplified model with:  Generated and Ungenerated tables/views  Subject Areas (SA)  Forward Engineering (FE) Option Sets  Classic FE  Classic FE with Scripts  Scripts Only  … but without:  Scripts … yet Note that FE using Scripts only produces no output yet – try again after next exercise! Copyright © 2007 CA. All rights reserved. . service marks and logos referenced herein belong to their respective companies.Exercise 1: Sample Model MacroTestFlight. All trademarks.

service marks and logos referenced herein belong to their respective companies. trade names.Ex. . 2: Hello World Script My First Script  Open the Pre & Post Scripts editor  Always in Physical display  Via Model Explorer  right click Script Templates and select Properties  Select New and type a name: Exercise2  Type “Hello World” (under Code)  Click on Expanded for result Note that a script is just reproducing text that you type Copyright © 2007 CA. All rights reserved. All trademarks.

s. All trademarks. .Ex.o. using the macro toolbox for the same result Copyright © 2007 CA. service marks and logos referenced herein belong to their respective companies. All rights reserved. trade names. 3: Use a simple macro  Create a script Exercise3      Type “Current date and time is ” Click on Macro Toolbox Select Miscellaneous Macro Select %Datetime Click on Insert Macro and Close  Click on Expanded for result Note that you can just type the macro i.

trade names. check the differences Note that with these settings you can control if and where the output is produced Copyright © 2007 CA. All trademarks. 4: Apply FE  Use script Exercise3  Select SA Flight. service marks and logos referenced herein belong to their respective companies. Generate and PostCreation  FE using the 3 option sets. check the differences  Select Pre-Creation and FE again. All rights reserved. open the Script editor and select tab General  Select Model-Level.Ex. . check the differences  Deselect Generate and FE again.

4: Apply FE (cont.)  Still using script Exercise3  Select Table-Level. check differences Note that table level scripts need to be attached to generate output Copyright © 2007 CA. All rights reserved. service marks and logos referenced herein belong to their respective companies. trade names. and Generate and FE again.Ex. check the differences  Select Exercise3 and click on Browser …  Attach all tables and views  Check out this interface before closing:  Resize and move the 2 windows  Navigate through the tables  Note option “Attach To New Table”  After closing: FE again. . All trademarks.

All rights reserved. service marks and logos referenced herein belong to their respective companies. 5: Simple Navigation  Create script Exercise5M  Select Model-Level. trade names. Generate and PreCreation and attach to all tables/views  Type “Next DDL is for %TableName”  Check Expanded code  Now FE again. . check differences Note that ERwin needs to “know” for what object it should generate output Copyright © 2007 CA. All trademarks. Generate and PreCreation  Type “Next DDL is for %TableName”  Create script Exercise5T  Select Table-Level.Ex.

check differences for tables and views Note that Table can be Table or View. service marks and logos referenced herein belong to their respective companies. trade names. All rights reserved. All trademarks. . 6: More Navigation  Create script Exercise6  Select Table-Level. Generate and PostCreation and attach to all tables/views  Type “Name of Table: %TableName”  Type “Name of View: %ViewName”  Type “Name of Entity: %EntityName”  Switch to SA Locations  Now FE again.Ex. while a view is only a View Copyright © 2007 CA.

not Views Copyright © 2007 CA. service marks and logos referenced herein belong to their respective companies. All trademarks. 7: Iteration  Create script Exercise7  Select SA Locations  Select Model-Level. Generate and PostCreation  Type “%ForEachTable()”  Type “{this is %TableName}”  Set previous created scripts to Ungenerated  Check Expanded for results  Note that the view is not part of the output Note that %ForEachTable only effects Tables. .Ex. All rights reserved. trade names.

trade names.Ex.)  Change script Exercise7  Change “%ForEachTable()” into “%ForEachEntity()”  Now check Expanded  Try the same for %ForEachView  Check Expanded again  Change it back to %ForEachEntity Note that %ForEachEntity effects both Tables and Views Copyright © 2007 CA. All rights reserved. . service marks and logos referenced herein belong to their respective companies. 7: Iteration (cont. All trademarks.

Ex.)  Change script Exercise7  Change “{this is %TableName}” by adding <Enter> between %TableName and }  Now FE again  Note that the output is on a separated line for each table / view  Note that the output order is alphabetical Note that %ForEachXxxx macros expand exactly what you tell them between the {} Copyright © 2007 CA. trade names. All rights reserved. All trademarks. 7: Iteration (cont. service marks and logos referenced herein belong to their respective companies. .

All rights reserved. trade names. 7: Iteration (cont.Ex. service marks and logos referenced herein belong to their respective companies. All trademarks. .)  Go to SA <Ungenerated>  FE and check output  You can generate output for Ungenerated objects  Go to SA <Main Subject Area>  Change script Exercise7  Change “%ForEachEntity()” to “%ForEachEntity(Flight)”  Now FE again Note that you can use a %ForEachXxxx macros for exact navigation Copyright © 2007 CA.

All rights reserved. and check the output for tables and views Note that %ForEachAttribute does not expand for Views Copyright © 2007 CA. 8: More Iteration  Create script Exercise8  Select Table-Level. service marks and logos referenced herein belong to their respective companies. trade names. All trademarks. Generate and PostCreation and attach to all tables/views  Type “%ForEachAttribute()”  Type “{Col: %ColName Att: %AttName”  Type “}”  Now FE from SA Locations.Ex. .

All rights reserved. and that %AttName always expands Copyright © 2007 CA. 8: More Iteration (cont. trade names. .)  Change script Exercise8  Change “%ForEachAttribute()” into “%ForEachColumn()”  Now FE again and check the output for tables and views  Note the output for %AttName for  View columns  Physical only columns  Change the script to model level. and check the FE output Note that %ForEachColumn does expand for Views. service marks and logos referenced herein belong to their respective companies. All trademarks.Ex.

Ex. Generated. trade names. PostCreation  Change the script by adding a first line: “%ForEachEntity() {Entity %EntityName”  And adding a “}” on the last line  Now Check Expanded for results  Repeat this from within various SAs. . All trademarks. All rights reserved. 9: Nested Iteration  Copy code of Exercise8 into Exercise9  Make it Model-Level. service marks and logos referenced herein belong to their respective companies. and check the output for tables and views Note the difference in navigation between Model level and Table-level scripts Copyright © 2007 CA.

All rights reserved. .>.>=.<>  Vary the condition Note that what you type between { and } is expanded exactly – except leading spaces Copyright © 2007 CA. service marks and logos referenced herein belong to their respective companies.<=.1)) {True}”  Type “%Else {Not True}”  Now check the expanded code  Vary the Comparison Operators: ==.Ex.!=.<. All trademarks. PostCreation  Type “%If(%==(1. Generated. trade names. 10: Selection  Create script Exercise10  Make it Model-Level.

. All trademarks. All rights reserved. service marks and logos referenced herein belong to their respective companies. trade names. 11: Iteration + Selection  Copy code of Exercise9 into Exercise11  Make it Model-Level. Generated.Ex. PostCreation  Change the script by adding a selection: expand only PK attributes – use macro %AttIsPK  Check Expanded for results Note how the number of brackets }} grows – using the wrong amount leads to errors easily Copyright © 2007 CA.

Ex. Generated. service marks and logos referenced herein belong to their respective companies. 12: Subroutine  Create Exercise12Main  Model-Level. Pre or Post  Type “This is table %TableName”  Check Expanded for results Note that the subroutine “knows” the current table Copyright © 2007 CA. All trademarks. All rights reserved. Post  Type “%ForEachTable() {%Include(Exercise12Sub)}”  Create Exercise12Sub  Any Level. trade names. Not Generated. .

trade names. Pre or Post  Type “Col: %ColName”  Now FE Note that the subroutine does NOT “know” the current column / attribute anymore Copyright © 2007 CA. All rights reserved. Not Generated. service marks and logos referenced herein belong to their respective companies.Ex.)  Change Exercise12Sub     Add the following Type “%ForEachColumn()” Type “{%Include(Exercise12SubSub)” Type “}”  Create Exercise12SubSub  Any Level. . 12: Subroutine (cont. All trademarks.

13: Filtering  Use the Exercise12 scripts  Select one table. All trademarks.Ex. All rights reserved. trade names. service marks and logos referenced herein belong to their respective companies. and start FE  Click on filter and use Current Diagram Selection  Notice that the output is limited to that selection …  … plus the Ungenerated objects in the same Subject Area!! Note that object selection for scripts follows the standard FE method Copyright © 2007 CA. .

iteration etc. nesting. All rights reserved.  Exercise in 3 parts:  Design  Intro of specific macros needed  Build Copyright © 2007 CA. trade names.Ex. 14: Finale  Building a set of scripts by which we can delete rows from all tables. taking Referential Integrity into account  After this exercise you will navigate through the data model using relations. All trademarks. service marks and logos referenced herein belong to their respective companies. .

Delete logic Copyright © 2007 CA. . All rights reserved. All trademarks. service marks and logos referenced herein belong to their respective companies. Note that reversing the order offers you Insert logic i.s. trade names.Ex. 14: Finale – Design (1)  Determine the so called “ChildLevel”:  a table that has no children has level 0  a table that has only children but no grandchildren has level 1  a table having at least 1 grandchild but no greatgrandchildren has level 2 etc.  Loop several times through your model:  First delete rows from tables with ChildLevel 0  Then delete from ChildLevel 1. etc.o.

14: Finale – Design (2)  Type of script: Model level  Since table level scripts are executed per table. these are not very helpful here  Using Model level script in general offers more control and flexibility Note that Model level scripts offer more control and flexibility than table level scripts Copyright © 2007 CA. and in an order defined by ERwin. service marks and logos referenced herein belong to their respective companies.Ex. . All rights reserved. trade names. All trademarks.

service marks and logos referenced herein belong to their respective companies. All rights reserved. All trademarks. . or bottom up for deletes)  Call the delete subroutine from within the loop  Initialize variables / parameters etc. trade names. 14: Finale – Design (3)  Structure: nested subroutines  Script Main  Takes care of the main loop  Determine here the order (top down for inserts.Ex.  Define the maximum child level you need – that is the number of iterations you need  How much for our sample model? Copyright © 2007 CA.

for a specific table  Holds the logic that determines whether or not that action should be executed for the current table  That logic depends on the last script the script that calculates the so called ChildLevel. trade names. 14: Finale – Design (4)  Script SubTable  Holds the action . All trademarks. .in this situation delete rows . service marks and logos referenced herein belong to their respective companies.Ex.  Script SubGetChildLevel Copyright © 2007 CA. All rights reserved.

and yyy the optional initial value  When a subroutine should return a value to a main routine. trade names. . All trademarks. Determined at the lowest level. 14: Finale – Macros (1)  Variables and parameters  Syntax for declaring variables: %Decl(xxx.Ex. service marks and logos referenced herein belong to their respective companies. used in the mid level Copyright © 2007 CA.yyy) where xxx is the name. All rights reserved. than declare the variable in the main  Parameters needed  MaxChildLevel: holds the number of loops  TblChildLevel: holds the ChildLevel for a specific table.

Ex. and increase by 1 for each entity until the max level you decided on is reached  Within this loop: the table loop in which you call the subroutine  Now you can build script Main Copyright © 2007 CA. All rights reserved. service marks and logos referenced herein belong to their respective companies. 14: Finale – Macros (2)  Looping  Since there is no FOR … NEXT functionality defined with macros. we improvise that by using some or other %ForEachXxxxx routine. in this case %ForEachEntity  Before the loop MaxChildLevel is set at 0. . All trademarks. trade names.

Ex. service marks and logos referenced herein belong to their respective companies. trade names. keep it simple for this exercise  Now you can build script SubTable Copyright © 2007 CA. All trademarks. All rights reserved.)  When equal. execute a Delete statement. 14: Finale – Macros (3)  Script SubTable contains only a few lines  First: call the subroutine SubGetChildLevel  Then compare the returned TblChldLevel to MaxChildLevel. macros needed:  %If() {}  %==(. .

14: Finale – Macros (4)  Script SubGetChildLevel contains the most complex code  Define a local variable (CurChildLevel) to keep track of the current level  Check whether or not a child exists by using %ForEachParentRel() {}  This macro loops through all entities for which the current entity / table is defined as parent  Meaning: code within {} is only executed when a relation actually exists Copyright © 2007 CA. trade names. .Ex. All trademarks. All rights reserved. service marks and logos referenced herein belong to their respective companies.

trade names. All trademarks. 14: Finale – Macros (5)  Within the {}:  Determine whether or not variable TblChidLevel < CurChildLevel  If so. service marks and logos referenced herein belong to their respective companies.Ex. . All rights reserved. and start applying the same logic again to determine whether or not the child has children Copyright © 2007 CA. set the CurChildLevel equal to TblChildLevel  Use macro %=(xxx. xxx  Now go to the next step: change focus to the child table.yyy) to set the value for a variable.

trade names.  Use a parameter for that:  %ForEachEntity(<tablename>) {}  Within a %ForEachParentRel() {} context you can use specific macros like %Parent. now limited to 1 table. All trademarks. 14: Finale – Macros (6)  Still within the {}:  How to change focus?  Start a new %ForEachEntity loop. %Child to navigate  %ForEachEntity(%Child) {zzz}  Replace zzz by the same logic  Repeat this nesting as many times as you need! Copyright © 2007 CA. All rights reserved. . the child table.Ex. service marks and logos referenced herein belong to their respective companies.

14: Finale – Build  Start building!!!! Copyright © 2007 CA. service marks and logos referenced herein belong to their respective companies. . All trademarks. trade names. All rights reserved.Ex.

All rights reserved. Selection.Summary A few words to review  Sequention. .  An then: start generating!  Result: 100% code generation. All trademarks. Iteration  Build it step by step  Design the structure to keep it maintainable  Keep it simple  For Advanced Use: apply an editor like PSPad to keep track of {}() etc. trade names. no manual coding of SQL statements needed Copyright © 2007 CA. service marks and logos referenced herein belong to their respective companies.

Questions & Answers Copyright © 2007 CA. . All trademarks. service marks and logos referenced herein belong to their respective companies. trade names. All rights reserved.

 . All rights reserved. .. place it in the basket at the back of the room.  Please left justify the session number Copyright © 2007 CA.Session Evaluation Form  After completing your session evaluation form . service marks and logos referenced herein belong to their respective companies. All trademarks.. trade names...

Sign up to vote on this title
UsefulNot useful