Sie sind auf Seite 1von 39

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.

Agenda - Exercises
1. Sample Model
Traditional Forward Engineering

2. Hello World Script 3. Using a Simple Macro 4. Apply FE


Pre and Post Scripts Table and Model Level Attached and Unattached

5. Simple Navigation 6. More Navigation

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

Agenda - Exercises
7. Iteration 8. More Iteration 9. Nested Iteration 10.Selection 11.Iteration + Selection 12.Subroutines 13.Filtering 14.Finale A Complete Script

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

Exercise 1: Sample Model


MacroTestFlight.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. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

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, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 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.s.o. using the macro toolbox for the same result
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 4: Apply FE
Use script Exercise3
Select SA Flight, open the Script editor and select tab General Select Model-Level, Generate and PostCreation FE using the 3 option sets, check the differences Select Pre-Creation and FE again, check the differences Deselect Generate and FE again, check the differences

Note that with these settings you can control if and where the output is produced
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 4: Apply FE (cont.)


Still using script Exercise3
Select Table-Level, and Generate and FE again, 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, check differences

Note that table level scripts need to be attached to generate output


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

Ex. 5: Simple Navigation


Create script Exercise5M
Select Model-Level, Generate and PreCreation Type Next DDL is for %TableName

Create script Exercise5T


Select Table-Level, 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 rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 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, check differences for tables and views

Note that Table can be Table or View, while a view is only a View
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 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, not Views


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

Ex. 7: Iteration (cont.)


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. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 7: Iteration (cont.)


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. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 7: Iteration (cont.)


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. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 8: More Iteration


Create script Exercise8
Select Table-Level, Generate and PostCreation and attach to all tables/views Type %ForEachAttribute() Type {Col: %ColName Att: %AttName Type }

Now FE from SA Locations, and check the output for tables and views

Note that %ForEachAttribute does not expand for Views


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

Ex. 8: More Iteration (cont.)


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, and that %AttName always expands
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 9: Nested Iteration


Copy code of Exercise8 into Exercise9
Make it Model-Level, Generated, 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, 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. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 10: Selection


Create script Exercise10
Make it Model-Level, Generated, PostCreation Type %If(%==(1,1)) {True} Type %Else {Not True}

Now check the expanded code Vary the Comparison Operators: ==,>=,>,<=,<,!=,<> Vary the condition

Note that what you type between { and } is expanded exactly except leading spaces
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 11: Iteration + Selection


Copy code of Exercise9 into Exercise11
Make it Model-Level, Generated, 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. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 12: Subroutine


Create Exercise12Main
Model-Level, Generated, Post Type %ForEachTable() {%Include(Exercise12Sub)}

Create Exercise12Sub
Any Level, Not Generated, Pre or Post Type This is table %TableName

Check Expanded for results

Note that the subroutine knows the current table


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

Ex. 12: Subroutine (cont.)


Change Exercise12Sub
Add the following Type %ForEachColumn() Type {%Include(Exercise12SubSub) Type }

Create Exercise12SubSub
Any Level, Not Generated, 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. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 13: Filtering


Use the Exercise12 scripts Select one table, 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. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

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, nesting, iteration etc. Exercise in 3 parts:
Design Intro of specific macros needed Build

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

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.

Note that reversing the order offers you Insert logic i.s.o. Delete logic
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 14: Finale Design (2)


Type of script: Model level
Since table level scripts are executed per table, and in an order defined by ERwin, 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. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 14: Finale Design (3)


Structure: nested subroutines Script Main
Takes care of the main loop Determine here the order (top down for inserts, or bottom up for deletes) Call the delete subroutine from within the loop Initialize variables / parameters etc. Define the maximum child level you need that is the number of iterations you need How much for our sample model?

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

Ex. 14: Finale Design (4)


Script SubTable
Holds the action - in this situation delete rows - 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.

Script SubGetChildLevel

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

Ex. 14: Finale Macros (1)


Variables and parameters
Syntax for declaring variables: %Decl(xxx,yyy) where xxx is the name,and yyy the optional initial value When a subroutine should return a value to a main routine, than declare the variable in the main

Parameters needed
MaxChildLevel: holds the number of loops TblChildLevel: holds the ChildLevel for a specific table. Determined at the lowest level, used in the mid level
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 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, 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. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 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() {} %==(,) When equal, execute a Delete statement; keep it simple for this exercise

Now you can build script SubTable

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

Ex. 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. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 14: Finale Macros (5)


Within the {}:
Determine whether or not variable TblChidLevel < CurChildLevel If so, set the CurChildLevel equal to TblChildLevel Use macro %=(xxx,yyy) to set the value for a variable, xxx Now go to the next step: change focus to the child table, and start applying the same logic again to determine whether or not the child has children

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

Ex. 14: Finale Macros (6)


Still within the {}: How to change focus?
Start a new %ForEachEntity loop, now limited to 1 table, the child table. Use a parameter for that: %ForEachEntity(<tablename>) {}

Within a %ForEachParentRel() {} context you can use specific macros like %Parent, %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. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Ex. 14: Finale Build


Start building!!!!

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

Summary
A few words to review Sequention, Selection, 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. An then: start generating! Result: 100% code generation, no manual coding of SQL statements needed
Copyright 2007 CA. All rights reserved. All trademarks, trade names, service marks and logos referenced herein belong to their respective companies.

Questions & Answers

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

Session Evaluation Form


After completing your session evaluation form ... ... place it in the basket at the back of the room. Please left justify the session number

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

Das könnte Ihnen auch gefallen