Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

MSSQL specific SQL code generator.

Copyright: 1999 onwards Martin Dougiamas http://dougiamas.com
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 700 lines (34 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: mssql_sql_generator  - X-Ref

This class generate SQL code to be used against MSSQL
It extends XMLDBgenerator so everything can be
overridden as needed to generate correct SQL.

getResetSequenceSQL($table)   X-Ref
Reset a sequence to the id field of a table.

return: array of sql statements
param: xmldb_table|string $table name of table or the table object.

getTableName(xmldb_table $xmldb_table, $quoted=true)   X-Ref
Given one xmldb_table, returns it's correct name, depending of all the parametrization
Overridden to allow change of names in temp tables

return: string the correct name of the table
param: xmldb_table table whose name we want
param: boolean to specify if the name must be quoted (if reserved word, only!)

getCreateIndexSQL($xmldb_table, $xmldb_index)   X-Ref
No description

getCreateTempTableSQL($xmldb_table)   X-Ref
Given one correct xmldb_table, returns the SQL statements
to create temporary table (inside one array).

return: array of sql statements
param: xmldb_table $xmldb_table The xmldb_table object instance.

getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null)   X-Ref
Given one XMLDB Type, length and decimals, returns the DB proper SQL type.

return: string The DB defined data type.
param: int $xmldb_type The xmldb_type defined constant. XMLDB_TYPE_INTEGER and other XMLDB_TYPE_* constants.
param: int $xmldb_length The length of that data type.
param: int $xmldb_decimals The decimal places of precision of the data type.

getDropFieldSQL($xmldb_table, $xmldb_field)   X-Ref
Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop the field from the table.
MSSQL overwrites the standard sentence because it needs to do some extra work dropping the default and
check constraints

return: array The SQL statement for dropping a field from the table.
param: xmldb_table $xmldb_table The table related to $xmldb_field.
param: xmldb_field $xmldb_field The instance of xmldb_field to create the SQL from.

getRenameFieldSQL($xmldb_table, $xmldb_field, $newname)   X-Ref
Given one correct xmldb_field and the new name, returns the SQL statements
to rename it (inside one array).

MSSQL is special, so we overload the function here. It needs to
drop the constraints BEFORE renaming the field

return: array The SQL statements for renaming the field.
param: xmldb_table $xmldb_table The table related to $xmldb_field.
param: xmldb_field $xmldb_field The instance of xmldb_field to get the renamed field from.
param: string $newname The new name to rename the field to.

getRenameTableExtraSQL($xmldb_table, $newname)   X-Ref
Returns the code (array of statements) needed to execute extra statements on table rename.

return: array Array of extra SQL statements to rename a table.
param: xmldb_table $xmldb_table The xmldb_table object instance.
param: string $newname The new name for the table.

getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL)   X-Ref
Given one xmldb_table and one xmldb_field, return the SQL statements needed to alter the field in the table.

return: string The field altering SQL statement.
param: xmldb_table $xmldb_table The table related to $xmldb_field.
param: xmldb_field $xmldb_field The instance of xmldb_field to create the SQL from.
param: string $skip_type_clause The type clause on alter columns, NULL by default.
param: string $skip_default_clause The default clause on alter columns, NULL by default.
param: string $skip_notnull_clause The null/notnull clause on alter columns, NULL by default.

getModifyDefaultSQL($xmldb_table, $xmldb_field)   X-Ref
Given one xmldb_table and one xmldb_field, return the SQL statements needed to modify the default of the field in the table.

return: array The SQL statement for modifying the default value.
param: xmldb_table $xmldb_table The table related to $xmldb_field.
param: xmldb_field $xmldb_field The instance of xmldb_field to get the modified default value from.

getCreateDefaultSQL($xmldb_table, $xmldb_field)   X-Ref
Given one xmldb_table and one xmldb_field, return the SQL statements needed to add its default
(usually invoked from getModifyDefaultSQL()

return: array Array of SQL statements to create a field's default.
param: xmldb_table $xmldb_table The xmldb_table object instance.
param: xmldb_field $xmldb_field The xmldb_field object instance.

getDropDefaultSQL($xmldb_table, $xmldb_field)   X-Ref
Given one xmldb_table and one xmldb_field, return the SQL statements needed to drop its default
(usually invoked from getModifyDefaultSQL()

Note that this method may be dropped in future.

return: array Array of SQL statements to create a field's default.
param: xmldb_table $xmldb_table The xmldb_table object instance.
param: xmldb_field $xmldb_field The xmldb_field object instance.

getDefaultConstraintName($xmldb_table, $xmldb_field)   X-Ref
Given one xmldb_table and one xmldb_field, returns the name of its default constraint in DB
or false if not found
This function should be considered internal and never used outside from generator

return: mixed
param: xmldb_table $xmldb_table The xmldb_table object instance.
param: xmldb_field $xmldb_field The xmldb_field object instance.

getNameForObject($tablename, $fields, $suffix='')   X-Ref
Given three strings (table name, list of fields (comma separated) and suffix),
create the proper object name quoting it if necessary.

IMPORTANT: This function must be used to CALCULATE NAMES of objects TO BE CREATED,
NEVER TO GUESS NAMES of EXISTING objects!!!

IMPORTANT: We are overriding this function for the MSSQL generator because objects
belonging to temporary tables aren't searchable in the catalog neither in information
schema tables. So, for temporary tables, we are going to add 4 randomly named "virtual"
fields, so the generated names won't cause concurrency problems. Really nasty hack,
but the alternative involves modifying all the creation table code to avoid naming
constraints for temp objects and that will dupe a lot of code.

return: string Object's name.
param: string $tablename The table name.
param: string $fields A list of comma separated fields.
param: string $suffix A suffix for the object name.

isNameInUse($object_name, $type, $table_name)   X-Ref
Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg).

(MySQL requires the whole xmldb_table object to be specified, so we add it always)

This is invoked from getNameForObject().
Only some DB have this implemented.

return: bool If such name is currently in use (true) or no (false)
param: string $object_name The object's name to check for.
param: string $type The object's type (pk, uk, fk, ck, ix, uix, seq, trg).
param: string $table_name The table's name to check in

getCommentSQL($xmldb_table)   X-Ref
Returns the code (array of statements) needed to add one comment to the table.

return: array Array of SQL statements to add one comment to the table.
param: xmldb_table $xmldb_table The xmldb_table object instance.

addslashes($s)   X-Ref
Adds slashes to string.

return: string The escaped string.
param: string $s

getReservedWords()   X-Ref
Returns an array of reserved words (lowercase) for this DB

return: array An array of database specific reserved words