Change the SQL template

Change the code of the template GenerateSQL to:

[#package tutorial.uml2relational]

[#template public GenerateSQL(in model : relational)]
[#file]generated/generatedTables.sql[/#file]
[#foreach table : relational.Table in model.getInstances("Table")]
-- 
-- TABLE ${table.name}
-- 
DROP TABLE IF EXISTS `${table.name}`;
CREATE TABLE `${table.name}` (
    [#foreach column : relational.Column in table.columns]
    [#set sqlName = column.name.toUpperCase()
          sqlType = column.type.name ? "VARCHAR(255)"
          hasNext = column != table.columns.last()]
    `${sqlName}` ${sqlType} [#if hasNext],[/#if]
    [/#foreach]
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
[/#foreach]
[/#template]

Directive tags (e.g. [#foreach]) are similar to HTML tags, but they are instructions and will not be output. Dynamic text (e.g. ${table.name}) will be replaced with a calculated value in the output.

Here is the behavior of this template:

  1. Iterates on Tables.
  2. Outputs some text related to a Table.
  3. Iterates on each Column in the current Table.
    1. Computes the upper-case name of the Column (using the toUpperCase() method defined on String) and stores the value in the variable sqlName.
    2. Computes the type name of the Column and stores the value in the variable sqlType. If the column's type is not set, the type "VARCHAR(255)" is generated (using a default value expression).
    3. Outputs some text related to the Column. A comma is inserted only if the column is not the last column of the table.
  4. Write the generated text into the file generated/generatedTables.sql.

Related reference
file directive
Default value expression
if directive