First, create a new project by clicking on ‘New Project’ on the home screen. This will set up a default project with a source, a package, and a template. Double click on ‘Source’ on the left project explorer pane to set up a database connection by configuring a connection string. Now double click on ‘Main’ just under the package node. This is used for when a project package is executed, MAIN is the entry point for code to run.
Variables are defined by the set keyword. Standard scoping rules apply. If a variable is declared in MAIN, any template within the main’s package can see that variable. If a variable is declared in a template, MAIN cannot see it. Additionally a variable declared within a block cannot be accessed outside of that scope.
Templates are run by using the call keyword. You may pass parameters into a template as you would a function.
Double click on ‘Template’ on the left explorer pane.
Code is defined within tags <> and any text outside of those tags is output directly. This works just like server side web scripting languages such as <% ASP %> or . Code completion will popup to help suggest keywords or variables.
Metadrone code generation will typically involve looping through tables in a database, and looping through columns within each table. In the default example, a table for loop is defined with a subsequent column for loop nested within it. The loop variables are used to output the table or column name or attribute within the text body.
Here is a sample of the result:
Address - AddressID : int
True
Address - AddressLine1 : nvarchar
False
Address - AddressLine2 : nvarchar
False
Address - City : nvarchar
False
Address - StateProvinceID : int
False
Address - PostalCode : nvarchar
False
Address - rowguid : uniqueidentifier
False
Address - ModifiedDate : datetime
False
AddressType - AddressTypeID : int
True
AddressType - Name : nvarchar
False
AddressType - rowguid : uniqueidentifier
False
AddressType - ModifiedDate : datetime
False
Explanation:
Source line | Explanation |
---|---|
<<!header; is Template() |
Begin header definition of the template. Template source is contained within tags <<! like this !>>. Any text outside of a tag is direct output. Statements are separated by semicolons; like; this; ‘is’ defines the template’s name, and parameters are defined between parenthesis. This example has an empty parameter list. |
// for table tbl in MainConn |
Comments follow double forward slashes //like this. These two commented out statements are for an example for loop return. The for statement declares a for table variable (this does not need a closing end statement). The return statement makes use of the for table variable for multiple outputs based on this single template. |
return BasePath + “destination.txt” |
‘return’ followed by an expression specifies the output file name. BasePath is a variable defined in the ‘Directives which is set to a blank string “”. The ‘+’ operator is a string concatenator. |
end!>> |
‘end’ ends a block; in this case the header block. |
<<!for table tbl in MainConn!>> |
Begin a for loop block. For each table in the connection as defined by the variable ‘MainConn’, repeat the contents within the block. You could also refer to the source directly. Eg: for table tbl in sources.Source |
<<!tbl!>> |
Output the current table name. |
<<!for column col in tbl!>> |
Begin a for loop block for each column for the current table. |
<<!col!>> : <<!col.datatype!>> |
Output the current column name followed by template text and then the column data type. |
<<!col.isidentity!>> |
Output the boolean value of whether the current column is an identity column or not. |
<<!end!>> |
End the column loop block. |
<<!end!>> |
End the table loop block. |
This example will output to a single file. To output to multiple files, replace the return statement within header for the two commented out statements above it. You will need to either rename the variable ‘tbl’ in the table for loop or remove the statement altogether.