$sql

( documentation for Telosys generator version 4.0.0 )


Object for schema creation in SQL language (for a relational database)
with functions for :
- names conversion (table name, column name, pk/fk name)
- field type conversion (neutral type to SQL column type)
It is designed to facilitate DDL commands generation
( CREATE TABLE, ADD CONSTRAINT FOREIGN KEY, etc)

Each instance of this object is dedicated to a database type
To get a new instance use : $factory.newSql('DatabaseName')
or $factory.newSql('DatabaseName', specificConventionsFileName)


Example :
   ## Get predefined conventions for a standard database (eg 'PostgreSQL')
   ## Known databases names : 'SQL-ANSI', 'PostgreSQL', 'Oracle', 'SQLServer'
   ## Database name is not case sensitive
   #set( $sql = $factory.newSql('PostgreSQL') )
   
   ## Get specific conventions using a specific file located in the bundle of templates
   #set( $sql = $factory.newSql('PostgreSQL', $fn.fileFromBundle('postgresql.properties') ) )
   

Since : 3.4.0

Attributes and methods
.columnConstraints(AttributeInContext attribute) : String

Returns the column constraints for the given attribute
For example : NOT NULL DEFAULT 12


Parameters :
   attribute : attribute from which to get column constraints

Example :
   $sql.columnConstraints($attribute)

Since : 3.4.0

.columnName(AttributeInContext attribute) : String

Returns the database column name for the given attribute
For example 'city_code' for an attribute named 'cityCode'
The database name defined in the model is used in priority
if no database name is defined then the attribute name is converted to database name
by applying the target database conventions


Parameters :
   attribute : attribute from which to get column name

Example :
   $sql.columnName($attribute)

Since : 3.4.0

.columnType(AttributeInContext attribute) : String

Converts the attribute neutral type to the corresponding SQL type
For example converts 'string' to 'varchar(x)'
The database type defined in the model is used in priority
if no database type is defined then the attribute type is converted to database type
by applying the target database conventions


Parameters :
   attribute : attribute from which to get column type

Example :
   $sql.columnType($attribute)

Since : 3.4.0

.convertToColumnName(String originalName) : String

Converts the given string to column naming style
For example converts 'firstName' to 'first_name'


Parameters :
   originalName : name to be converted

Example :
   $sql.convertToColumnName($var)

Since : 3.4.0

.convertToColumnType(String neutralType, boolean autoInc, BigDecimal size) : String

Converts the given neutral type to column type
For example converts 'string' to 'varchar(20)'


Parameters :
   neutralType : neutral type to be converted
   autoInc : auto-incremented attribute (true/false)
   size : maximum size (for a variable length string, eg 45 or 8.2 )

Example :
   $sql.convertToColumnType('string', false, 20)

Since : 3.4.0

.convertToFkName(String originalName) : String

Converts the given string to foreign key naming style
For example converts 'PkFirstName' to 'pk_first_name'


Parameters :
   originalName : name to be converted

Example :
   $sql.convertToFkName($var)

Since : 3.4.0

.convertToPkName(String originalName) : String

Converts the given string to primary key naming style
For example converts 'PkFirstName' to 'pk_first_name'


Parameters :
   originalName : name to be converted

Example :
   $sql.convertToPkName($var)

Since : 3.4.0

.convertToTableName(String ?) : String

Converts the given string to table naming style
For example converts 'EmployeeJobs' to 'employee_jobs'


Parameters :
   ? :

Example :
   $sql.convertToTableName($var)

Since : 3.4.0

.databaseConfigFile : String

Returns the target database configuration file


Example :
   $sql.databaseConfigFile()

Since : 3.4.0

.databaseName : String

Returns the target database name


Example :
   $sql.databaseName()

Since : 3.4.0

.fkColumns(ForeignKeyInContext ?) : String

Returns a string containing the names of all the columns composing the foreign key
Each column name is converted according to the naming rules for the target database
Examples : 'group_id' or 'car_code, group_id'

Parameters :
   ? :

Example :
   $sql.fkColumns($fk)

Since : 3.4.0

.fkName(ForeignKeyInContext ?) : String

Returns the name of the given Foreign Key
(converts the name to expected naming style if necessary)
For example converts 'fkFooBar' or 'FK_FOO_BAR' to 'fk_foo_bar'


Parameters :
   ? :

Example :
   $sql.fkName($fk)

Since : 3.4.0

.fkOriginTable(ForeignKeyInContext ?) : String

Returns the name of the table for the given Foreign Key
(converts the name to table naming style if necessary)
For example converts 'SpecialCar' or 'SPECIAL_CAR' to 'special_car'


Parameters :
   ? :

Example :
   $sql.fkTable($fk)

Since : 3.4.0

.fkReferencedColumns(ForeignKeyInContext ?) : String

Returns a string containing the names of all the columns referenced by the foreign key
Each column name is converted according to the naming rules for the target database
For example 'id' or 'code, group'

Parameters :
   ? :

Example :
   $sql.fkReferencedColumns($fk)

Since : 3.4.0

.fkReferencedTable(ForeignKeyInContext ?) : String

Returns the name of the referenced table for the given Foreign Key
(converts the name to table naming style if necessary)
For example converts 'SpecialCar' or 'SPECIAL_CAR' to 'special_car'


Parameters :
   ? :

Example :
   $sql.fkReferencedTable($fk)

Since : 3.4.0

.pkColumns(EntityInContext ?) : String

Returns a string containing the names of all the columns composing the primary key
for the given entity
Each column name is converted according to the naming rules for the target database
For example 'id' or 'code, group'
Returns an empty string if the entity does not have a primary key

Parameters :
   ? :

Example :
   $sql.pkColumns($entity)

Since : 3.4.0

.tableName(EntityInContext ?) : String

Returns the database table name for the given entity
If the table name is defined in the model it is used in priority
if no table name is defined then the entity name is converted to table name
by applying the target database conventions
(for example 'student_projects' for an entity named 'StudentProjects')


Parameters :
   ? :

Example :
   $sql.tableName($entity)

Since : 3.4.0