Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

Database manager instance is responsible for all database structure modifications.

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

Defines 1 class


Class: database_manager  - X-Ref

Database manager instance is responsible for all database structure modifications.

It is using db specific generators to find out the correct SQL syntax to do that.

__construct($mdb, $generator)   X-Ref
Creates a new database manager instance.

param: moodle_database $mdb A moodle_database driver specific instance.
param: sql_generator $generator A driver specific SQL generator instance.

dispose()   X-Ref
Releases all resources


execute_sql_arr(array $sqlarr, $tablenames = null)   X-Ref
This function will execute an array of SQL commands.

param: string[] $sqlarr Array of sql statements to execute.
param: array|null $tablenames an array of xmldb table names affected by this request.

execute_sql($sql)   X-Ref
Execute a given sql command string.

param: string $sql The sql string you wish to be executed.

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

param: string|xmldb_table $table The table to be searched (string name or xmldb_table instance).
return: bool True is a table exists, false otherwise.

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

param: string|xmldb_table $table Name of table.

field_exists($table, $field)   X-Ref
Given one xmldb_field, check if it exists in DB (true/false).

param: string|xmldb_table $table The table to be searched (string name or xmldb_table instance).
param: string|xmldb_field $field The field to be searched for (string name or xmldb_field instance).
return: boolean true is exists false otherwise.

find_index_name(xmldb_table $xmldb_table, xmldb_index $xmldb_index, $returnall = false)   X-Ref
Given one xmldb_index, the function returns the name of the index in DB
of false if it doesn't exist

param: xmldb_table $xmldb_table table to be searched
param: xmldb_index $xmldb_index the index to be searched
param: bool $returnall true means return array of all indexes, false means first index only as string
return: array|string|bool Index name, array of index names or false if no indexes are found.

index_exists(xmldb_table $xmldb_table, xmldb_index $xmldb_index)   X-Ref
Given one xmldb_index, check if it exists in DB (true/false).

param: xmldb_table $xmldb_table The table to be searched.
param: xmldb_index $xmldb_index The index to be searched for.
return: boolean true id index exists, false otherwise.

find_key_name(xmldb_table $xmldb_table, xmldb_key $xmldb_key)   X-Ref
This function IS NOT IMPLEMENTED. ONCE WE'LL BE USING RELATIONAL
INTEGRITY IT WILL BECOME MORE USEFUL. FOR NOW, JUST CALCULATE "OFFICIAL"
KEY NAMES WITHOUT ACCESSING TO DB AT ALL.
Given one xmldb_key, the function returns the name of the key in DB (if exists)
of false if it doesn't exist

param: xmldb_table $xmldb_table The table to be searched.
param: xmldb_key $xmldb_key The key to be searched.
return: string key name if found

delete_tables_from_xmldb_file($file)   X-Ref
This function will delete all tables found in XMLDB file from db

param: string $file Full path to the XML file to be used.
return: void

drop_table(xmldb_table $xmldb_table)   X-Ref
This function will drop the table passed as argument
and all the associated objects (keys, indexes, constraints, sequences, triggers)
will be dropped too.

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
return: void

load_xmldb_file($file)   X-Ref
Load an install.xml file, checking that it exists, and that the structure is OK.

param: string $file the full path to the XMLDB file.
return: xmldb_file the loaded file.

install_from_xmldb_file($file)   X-Ref
This function will load one entire XMLDB file and call install_from_xmldb_structure.

param: string $file full path to the XML file to be used
return: void

install_one_table_from_xmldb_file($file, $tablename, $cachestructures = false)   X-Ref
This function will load one entire XMLDB file and call install_from_xmldb_structure.

param: string $file full path to the XML file to be used
param: string $tablename the name of the table.
param: bool $cachestructures boolean to decide if loaded xmldb structures can be safely cached

install_from_xmldb_structure($xmldb_structure)   X-Ref
This function will generate all the needed SQL statements, specific for each
RDBMS type and, finally, it will execute all those statements against the DB.

param: stdClass $xmldb_structure xmldb_structure object.
return: void

create_table(xmldb_table $xmldb_table)   X-Ref
This function will create the table passed as argument with all its
fields/keys/indexes/sequences, everything based in the XMLDB object

param: xmldb_table $xmldb_table Table object (full specs are required).
return: void

create_temp_table(xmldb_table $xmldb_table)   X-Ref
This function will create the temporary table passed as argument with all its
fields/keys/indexes/sequences, everything based in the XMLDB object

If table already exists ddl_exception will be thrown, please make sure
the table name does not collide with existing normal table!

param: xmldb_table $xmldb_table Table object (full specs are required).
return: void

drop_temp_table(xmldb_table $xmldb_table)   X-Ref
This function will drop the temporary table passed as argument with all its
fields/keys/indexes/sequences, everything based in the XMLDB object

It is recommended to drop temp table when not used anymore.

param: xmldb_table $xmldb_table Table object.
return: void

rename_table(xmldb_table $xmldb_table, $newname)   X-Ref
This function will rename the table passed as argument
Before renaming the index, the function will check it exists

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: string $newname New name of the index.
return: void

add_field(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will add the field to the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
return: void

drop_field(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will drop the field from the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
return: void

change_field_type(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will change the type of the field in the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
return: void

change_field_precision(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will change the precision of the field in the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
return: void

change_field_unsigned(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will change the unsigned/signed of the field in the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Field object (full specs are required).
return: void

change_field_notnull(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will change the nullability of the field in the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
return: void

change_field_default(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will change the default of the field in the table passed as arguments
One null value in the default field means delete the default

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
return: void

rename_field(xmldb_table $xmldb_table, xmldb_field $xmldb_field, $newname)   X-Ref
This function will rename the field in the table passed as arguments
Before renaming the field, the function will check it exists

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
param: string $newname New name of the field.
return: void

check_field_dependencies(xmldb_table $xmldb_table, xmldb_field $xmldb_field)   X-Ref
This function will check, for the given table and field, if there there is any dependency
preventing the field to be modified. It's used by all the public methods that perform any
DDL change on fields, throwing one ddl_dependency_exception if dependencies are found.

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_field $xmldb_field Index object (full specs are required).
return: void

add_key(xmldb_table $xmldb_table, xmldb_key $xmldb_key)   X-Ref
This function will create the key in the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_key $xmldb_key Index object (full specs are required).
return: void

drop_key(xmldb_table $xmldb_table, xmldb_key $xmldb_key)   X-Ref
This function will drop the key in the table passed as arguments

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_key $xmldb_key Key object (full specs are required).
return: void

rename_key(xmldb_table $xmldb_table, xmldb_key $xmldb_key, $newname)   X-Ref
This function will rename the key in the table passed as arguments
Experimental. Shouldn't be used at all in normal installation/upgrade!

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_key $xmldb_key key object (full specs are required).
param: string $newname New name of the key.
return: void

add_index($xmldb_table, $xmldb_intex)   X-Ref
This function will create the index in the table passed as arguments
Before creating the index, the function will check it doesn't exists

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_index $xmldb_intex Index object (full specs are required).
return: void

drop_index($xmldb_table, $xmldb_intex)   X-Ref
This function will drop the index in the table passed as arguments
Before dropping the index, the function will check it exists

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_index $xmldb_intex Index object (full specs are required).
return: void

rename_index($xmldb_table, $xmldb_intex, $newname)   X-Ref
This function will rename the index in the table passed as arguments
Before renaming the index, the function will check it exists
Experimental. Shouldn't be used at all!

param: xmldb_table $xmldb_table Table object (just the name is mandatory).
param: xmldb_index $xmldb_intex Index object (full specs are required).
param: string $newname New name of the index.
return: void

get_install_xml_files()   X-Ref
Get the list of install.xml files.

return: array

get_install_xml_schema()   X-Ref
Reads the install.xml files for Moodle core and modules and returns an array of
xmldb_structure object with xmldb_table from these files.

return: xmldb_structure schema from install.xml files

check_database_schema(xmldb_structure $schema, array $options = null)   X-Ref
Checks the database schema against a schema specified by an xmldb_structure object

param: xmldb_structure $schema export schema describing all known tables
param: array $options
return: array keyed by table name with array of difference messages as values

get_missing_index_error(xmldb_table $table, xmldb_index $index, string $indexname)   X-Ref
Returns a string describing the missing index error.

param: xmldb_table $table
param: xmldb_index $index
param: string $indexname
return: string

remove_index_from_dbindex(array &$dbindexes, xmldb_index $index)   X-Ref
Removes an index from the array $dbindexes if it is found.

param: array $dbindexes
param: xmldb_index $index