Extending Generation with Before and After Statements

You can extend script generation statements to complement generation using the extension statements. The extension mechanism allows you to generate statements immediately before or after Create, Drop, and Modify statements, and to retrieve these statements during reverse engineering.

Extension statements are written in GTL (see Customizing Generation with GTL). During generation, the statements and variables are evaluated and the result is added to the global script.

Note: We recommend that you avoid using GTL macros (other than .if) in generation scripts, as they may not be resolvable when reverse engineering by script. Generating and reverse engineering via a live database connection are not subject to this limitation.

Example - Adding an AfterCreate Statement

The extension statement AfterCreate is defined in the Table category to complement the table Create statement by adding partitions to the table if the value of the partition extended attribute requires it:
.if (%ExtTablePartition% > 1)
%CreatePartition%
go
.endif
The .if macro evaluates variable %ExtTablePartition%, which is an extended attribute that contains the number of table partitions. If the value is higher than 1, then %CreatePartition%, defined in the Table category, will be generated as follows:
alter table [%QUALIFIER%]%TABLE%
 partition %ExtTablePartition%

This item generates the statement for creating the number of table partitions specified in %ExtTablePartition%.

Example - Adding a BeforeCreate Statement

The extension statement BeforeCreate is defined in the User category to create the login of a user before the user Create statement is executed:

sp_addlogin %Name% %Password%
go

The automatically generated login will have the same name as the user, and its password. The BeforeCreate statement is displayed before the user creation statement in the Preview:



Example - Modify Statements

You can also add BeforeModify and AfterModify statements to standard Modify statements.

Modify statements are executed to synchronize the database with the schema created in the PDM. By default, the modify database feature does not take into account extended attributes when it compares changes performed in the model from the last generation. You can bypass this rule by adding extended attributes in the ModifiableAttributes list item. Extended attributes defined in this list will be taken into account in the merge dialog box during database synchronization.

To detect that an extended attribute value has been modified you can use the following variables:
  • %OLDOBJECT% - to access an old value of the object

  • %NEWOBJECT% - to access a new value of the object

For example, you can verify that the value of the extended attribute ExtTablePartition has been modified using the following GTL syntax:

.if (%OLDOBJECT.ExtTablePartition% != %NEWOBJECT.ExtTablePartition%)

If the extended attribute value was changed, an extended statement will be generated to update the database. In the Sybase ASE syntax, the ModifyPartition extended statement is the following because in case of partition change you need to delete the previous partition and then recreate it:

.if (%OLDOBJECT.ExtTablePartition% != %NEWOBJECT.ExtTablePartition%)
 .if (%NEWOBJECT.ExtTablePartition% > 1)
  .if (%OLDOBJECT.ExtTablePartition% > 1)
%DropPartition%
  .endif
%CreatePartition%
 .else
%DropPartition%
 .endif
.endif