ca world 2007 - edm03lna - scripts and macros

Upload: alagar-pitchai

Post on 14-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    1/39

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

    Getting started withScripts and Macros

    Session EDM03LNA / EDM03LNBEnterprise Systems Management

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    2/39

    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 differencebetween model and table level scripts,attached and unattached scripts, when to usewhat type of script, and how you can make allyour scripts work together.

    Starting with an easy Hello World script toget the hang of it, we will switch to somesimple iteration through tables and columns,followed by more complex iterations usingrelations, subroutines and nesting.

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    3/39

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

    Biography

    Theo van WestrienenMartinair 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, carindustry, Dutch flower, fruit and vegetablesauctions

    Currently as Data Manager for airlinerMartinair

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    4/39Copyright 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 Navigation6. More Navigation

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

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

    Agenda - Exercises

    7. Iteration8. More Iteration

    9. Nested Iteration

    10.Selection11.Iteration + Selection

    12.Subroutines

    13.Filtering

    14.Finale A Complete Script

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    6/39Copyright 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 Ungeneratedtables/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 onlyproduces no

    output yet try again after next exercise!

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    7/39Copyright 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 andselect 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

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    8/39Copyright 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

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    9/39Copyright 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 Post-Creation

    FE using the 3 option sets, check thedifferences

    Select Pre-Creation and FE again, checkthe differences

    Deselect Generate and FE again, checkthe differences

    Note that with these settings you can control

    if and where the output is produced

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    10/39Copyright 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

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    11/39

    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 Pre-

    Creation

    Type Next DDL is for %TableName

    Create script Exercise5T Select Table-Level, Generate and Pre-

    Creation 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

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    12/39

    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 Post-

    Creation 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 aview is only a View

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    13/39

    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 Post-Creation

    Type %ForEachTable()

    Type {this is %TableName}

    Set previous created scripts toUngenerated

    Check Expanded for results

    Note that the view is not part of theoutput

    Note that %ForEachTable only effects Tables,not Views

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    14/39

    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 Tablesand Views

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    15/39

    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 between %TableNameand}

    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 expandexactly what you tell them between the {}

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    16/39

    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 FE and check output

    You can generate output forUngenerated objects

    Go to SA Change script Exercise7

    Change %ForEachEntity() to%ForEachEntity(Flight)

    Now FE again

    Note that you can use a %ForEachXxxxmacros for exact navigation

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    17/39

    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 Post-

    Creation and attach to all tables/views

    Type %ForEachAttribute()

    Type {Col: %ColName Att: %AttName Type }

    Now FE from SA Locations, and checkthe output for tables and views

    Note that %ForEachAttribute does not expandfor Views

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    18/39

    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 againand check the output

    for tables and views Note the output for %AttName for

    View columns

    Physical only columns

    Change the script to model level, andcheck the FE output

    Note that %ForEachColumn does expand forViews, and that %AttName always expands

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    19/39

    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 ofExercise8 into Exercise9 Make it Model-Level, Generated, Post-

    Creation

    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 andviews

    Note the difference in navigation betweenModel level and Table-level scripts

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    20/39

    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, Post-

    Creation

    Type %If(%==(1,1)) {True}

    Type %Else {Not True} Now check the expanded code

    Vary the Comparison Operators:==,>=,>,

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    21/39

    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 ofExercise9 intoExercise11

    Make it Model-Level, Generated, Post-Creation

    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

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    22/39

    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, NotGenerated, Pre or Post

    Type This is table %TableName

    Check Expanded for results

    Note that the subroutine knows the currenttable

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    23/39

    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, NotGenerated, Pre or Post

    Type Col: %ColName

    Now FE

    Note that the subroutine does NOT know thecurrent column / attribute anymore

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    24/39

    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 CurrentDiagram Selection

    Notice that the output is limited tothat selection

    plus the Ungenerated objects in thesame Subject Area!!

    Note that object selection for scripts followsthe standard FE method

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    25/39

    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 wecan delete rows from all tables,taking Referential Integrity intoaccount

    After this exercise you will navigatethrough the data model usingrelations, nesting, iteration etc.

    Exercise in 3 parts:

    Design

    Intro of specific macros needed

    Build

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    26/39

    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 nograndchildren has level 1

    a table having at least 1 grandchild butno greatgrandchildren has level 2 etc.

    Loop several times through yourmodel:

    First delete rows from tables with

    ChildLevel 0

    Then delete from ChildLevel 1, etc.

    Note that reversing the order offers you Insertlogic i.s.o. Delete logic

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    27/39

    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 byERwin, these are not very helpful here

    Using Model level script in general offers

    more control and flexibility

    Note that Model level scripts offer morecontrol and flexibility than table level scripts

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    28/39

    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 youneed that is the number of iterationsyou need

    How much for our sample model?

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    29/39

    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 whetheror not that action should be executed

    for the current table

    That logic depends on the last script -the script that calculates the so calledChildLevel.

    Script SubGetChildLevel

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    30/39

    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 thename,and yyy the optional initial value

    When a subroutine should return a

    value to a main routine, than declarethe variable in the main

    Parameters needed

    MaxChildLevel: holds the number of

    loops TblChildLevel: holds the ChildLevel for a

    specific table. Determined at the lowestlevel, used in the mid level

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    31/39

    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, weimprovise 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 untilthe max level you decided on is reached

    Within this loop: the table loop inwhich you call the subroutine

    Now you can build script Main

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    32/39

    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 fewlines

    First: call the subroutineSubGetChildLevel

    Then compare the returned TblChldLevelto MaxChildLevel; macros needed:

    %If() {}

    %==(,)

    When equal, execute a Delete

    statement; keep it simple for thisexercise

    Now you can build script SubTable

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    33/39

    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 containsthe most complex code

    Define a local variable (CurChildLevel)to keep track of the current level

    Check whether or not a child exists byusing %ForEachParentRel() {}

    This macro loops through all entitiesfor which the current entity / table isdefined as parent

    Meaning: code within {} is onlyexecuted when a relation actuallyexists

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    34/39

    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 toTblChildLevel

    Use macro %=(xxx,yyy) to set thevalue for a variable, xxx

    Now go to the next step: change focusto the child table, and start applying thesame logic again to determine whetheror not the child has children

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    35/39

    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, nowlimited to 1 table, the child table.

    Use a parameter for that:

    %ForEachEntity() {}

    Within a %ForEachParentRel() {}context you can use specific macroslike %Parent, %Child to navigate

    %ForEachEntity(%Child) {zzz} Replace zzz by the same logic

    Repeat this nesting as many timesas you need!

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    36/39

    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!!!!

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    37/39

    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 editorlike PSPad to keep track of {}() etc.

    An then: start generating! Result: 100% code generation, no

    manual coding of SQL statementsneeded

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    38/39

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

    Questions & Answers

  • 7/30/2019 CA World 2007 - EDM03LNA - Scripts and Macros

    39/39

    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 completingyour sessionevaluation form ...

    ... place it in thebasket at the backof the room.

    Please left justify thesession number