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.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 and 403]

This class represent the base generator class where all the needed functions to generate proper SQL are defined. The rest of classes will inherit, by default, the same logic. Functions will be overridden as needed to generate correct SQL.

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

Defines 1 class


Class: sql_generator  - X-Ref

Abstract sql generator class, base for all db specific implementations.

__construct($mdb, $temptables = null)   X-Ref
Creates a new sql_generator.

param: moodle_database $mdb The moodle_database object instance.
param: moodle_temptables $temptables The optional moodle_temptables instance, null by default.

dispose()   X-Ref
Releases all resources.


getEndedStatements($input)   X-Ref
Given one string (or one array), ends it with $statement_end .

return: array|string
param: array|string $input SQL statement(s).

table_exists($table)   X-Ref
Given one xmldb_table, checks if it exists in DB (true/false).

return: boolean true/false
param: mixed $table The table to be searched (string name or xmldb_table instance).

getCreateStructureSQL($xmldb_structure)   X-Ref
This function will return the SQL code needed to create db tables and statements.

return: array
param: xmldb_structure $xmldb_structure An xmldb_structure instance.

getTableName(xmldb_table $xmldb_table, $quoted=true)   X-Ref
Given one xmldb_table, this returns it's correct name, depending of all the parameterization.
eg: This appends $prefix to the table name.

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

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

return: array An array of SQL statements, starting with the table creation SQL followed
param: xmldb_table $xmldb_table An xmldb_table instance.

getCreateIndexSQL($xmldb_table, $xmldb_index)   X-Ref
Given one correct xmldb_index, returns the SQL statements
needed to create it (in array).

return: array An array of SQL statements to create the index.
param: xmldb_table $xmldb_table The xmldb_table instance to create the index on.
param: xmldb_index $xmldb_index The xmldb_index to create.

getFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL, $specify_nulls_clause = NULL, $specify_field_name = true)   X-Ref
Given one correct xmldb_field, returns the complete SQL line to create it.

return: string The field generating 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.
param: string $specify_nulls_clause To force a specific null clause, NULL by default.
param: bool $specify_field_name Flag to specify fieldname in return.

getKeySQL($xmldb_table, $xmldb_key)   X-Ref
Given one correct xmldb_key, returns its specs.

return: string SQL statement about the xmldb_key.
param: xmldb_table $xmldb_table The table related to $xmldb_key.
param: xmldb_key $xmldb_key The xmldb_key's specifications requested.

getDefaultValue($xmldb_field)   X-Ref
Give one xmldb_field, returns the correct "default value" for the current configuration

return: The default value of the field.
param: xmldb_field $xmldb_field The field.

getDefaultClause($xmldb_field)   X-Ref
Given one xmldb_field, returns the correct "default clause" for the current configuration.

return: The SQL clause for generating the default value as in $xmldb_field.
param: xmldb_field $xmldb_field The xmldb_field.

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

return: array SQL statement(s) to rename the table.
param: xmldb_table $xmldb_table The table to rename.
param: string $newname The new name to rename the table to.

getDropTableSQL($xmldb_table)   X-Ref
Given one correct xmldb_table, returns the SQL statements
to drop it (inside one array). Works also for temporary tables.

return: array SQL statement(s) for dropping the specified table.
param: xmldb_table $xmldb_table The table to drop.

cleanup_after_drop(xmldb_table $table)   X-Ref
Performs any clean up that needs to be done after a table is dropped.

param: xmldb_table $table

getAddFieldSQL($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 add the field to the table.

return: array The SQL statement for adding a field to 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.
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.

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.

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.

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.

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).

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.

getAddKeySQL($xmldb_table, $xmldb_key)   X-Ref
Given one xmldb_table and one xmldb_key, return the SQL statements needed to add the key to the table
note that undelying indexes will be added as parametrised by $xxxx_keys and $xxxx_index parameters.

return: array SQL statement to add the xmldb_key.
param: xmldb_table $xmldb_table The table related to $xmldb_key.
param: xmldb_key $xmldb_key The xmldb_key to add.

getDropKeySQL($xmldb_table, $xmldb_key)   X-Ref
Given one xmldb_table and one xmldb_index, return the SQL statements needed to drop the index from the table.

return: array SQL statement to drop the xmldb_key.
param: xmldb_table $xmldb_table The table related to $xmldb_key.
param: xmldb_key $xmldb_key The xmldb_key to drop.

getRenameKeySQL($xmldb_table, $xmldb_key, $newname)   X-Ref
Given one xmldb_table and one xmldb_key, return the SQL statements needed to rename the key in the table
Experimental! Shouldn't be used at all!

return: array SQL statement to rename the xmldb_key.
param: xmldb_table $xmldb_table The table related to $xmldb_key.
param: xmldb_key $xmldb_key The xmldb_key to rename.
param: string $newname The xmldb_key's new name.

getAddIndexSQL($xmldb_table, $xmldb_index)   X-Ref
Given one xmldb_table and one xmldb_index, return the SQL statements needed to add the index to the table.

return: array An array of SQL statements to add the index.
param: xmldb_table $xmldb_table The xmldb_table instance to add the index on.
param: xmldb_index $xmldb_index The xmldb_index to add.

getDropIndexSQL($xmldb_table, $xmldb_index)   X-Ref
Given one xmldb_table and one xmldb_index, return the SQL statements needed to drop the index from the table.

return: array An array of SQL statements to drop the index.
param: xmldb_table $xmldb_table The xmldb_table instance to drop the index on.
param: xmldb_index $xmldb_index The xmldb_index to drop.

getRenameIndexSQL($xmldb_table, $xmldb_index, $newname)   X-Ref
Given one xmldb_table and one xmldb_index, return the SQL statements needed to rename the index in the table
Experimental! Shouldn't be used at all!

return: array An array of SQL statements to rename the index.
param: xmldb_table $xmldb_table The xmldb_table instance to rename the index on.
param: xmldb_index $xmldb_index The xmldb_index to rename.
param: string $newname The xmldb_index's new name.

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!!!

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.

getEncQuoted($input)   X-Ref
Given any string (or one array), enclose it by the proper quotes
if it's a reserved word

return: string|array Quoted string.
param: string|array $input String to quote.

getExecuteInsertSQL($statement)   X-Ref
Given one XMLDB Statement, build the needed SQL insert sentences to execute it.

return: array Array of sentences in the SQL statement.
param: string $statement SQL statement.

getConcatSQL($elements)   X-Ref
Given one array of elements, build the proper CONCAT expression, based
in the $concat_character setting. If such setting is empty, then
MySQL's CONCAT function will be used instead.

return: mixed Returns the result of moodle_database::sql_concat() or false.
param: array $elements An array of elements to concatenate.

getSequenceFromDB($xmldb_table)   X-Ref
Returns the name (string) of the sequence used in the table for the autonumeric pk
Only some DB have this implemented.

return: bool Returns the sequence from the DB or false.
param: xmldb_table $xmldb_table The xmldb_table instance.

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

getRenameFieldExtraSQL($xmldb_table, $xmldb_field)   X-Ref
Returns the code (array of statements) needed to execute extra statements on field rename.

return: array Array of extra SQL statements to run with a field being renamed.
param: xmldb_table $xmldb_table The xmldb_table object instance.
param: xmldb_field $xmldb_field The xmldb_field object instance.

getCreateSequenceSQL($xmldb_table, $xmldb_field)   X-Ref
Returns the code (array of statements) needed
to create one sequence for the xmldb_table and xmldb_field passed in.

return: array Array of SQL statements to create the sequence.
param: xmldb_table $xmldb_table The xmldb_table object instance.
param: xmldb_field $xmldb_field The xmldb_field object instance.

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.

getDropTableExtraSQL($xmldb_table)   X-Ref
Returns the code (array of statements) needed to execute extra statements on table drop

return: array Array of extra SQL statements to drop a table.
param: xmldb_table $xmldb_table The xmldb_table object instance.

getReservedWords()   X-Ref
Returns an array of reserved words (lowercase) for this DB
You MUST provide the real list for each DB inside every XMLDB class.

return: array An array of database specific reserved words.

getAllReservedWords()   X-Ref
Returns all reserved words in supported databases.
Reserved words should be lowercase.

return: array ('word'=>array(databases))

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

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

get_nullable_fields_in_index($xmldb_table, $xmldb_index)   X-Ref
Get the fields from an index definition that might be null.

return: array list of fields in the index definition that might be null.
param: xmldb_table $xmldb_table the table
param: xmldb_index $xmldb_index the index