Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
database_manager Class Reference

Database manager instance is responsible for all database structure modifications. More...

Public Member Functions

 __construct ($mdb, $generator)
 Creates a new database manager instance. More...
 
 add_field (xmldb_table $xmldb_table, xmldb_field $xmldb_field)
 This function will add the field to the table passed as arguments. More...
 
 add_index ($xmldb_table, $xmldb_intex)
 This function will create the index in the table passed as arguments Before creating the index, the function will check it doesn't exists. More...
 
 add_key (xmldb_table $xmldb_table, xmldb_key $xmldb_key)
 This function will create the key in the table passed as arguments. More...
 
 change_field_default (xmldb_table $xmldb_table, xmldb_field $xmldb_field)
 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. More...
 
 change_field_notnull (xmldb_table $xmldb_table, xmldb_field $xmldb_field)
 This function will change the nullability of the field in the table passed as arguments. More...
 
 change_field_precision (xmldb_table $xmldb_table, xmldb_field $xmldb_field)
 This function will change the precision of the field in the table passed as arguments. More...
 
 change_field_type (xmldb_table $xmldb_table, xmldb_field $xmldb_field)
 This function will change the type of the field in the table passed as arguments. More...
 
 change_field_unsigned (xmldb_table $xmldb_table, xmldb_field $xmldb_field)
 This function will change the unsigned/signed of the field in the table passed as arguments. More...
 
 check_database_schema (xmldb_structure $schema, array $options=null)
 Checks the database schema against a schema specified by an xmldb_structure object. More...
 
 create_table (xmldb_table $xmldb_table)
 This function will create the table passed as argument with all its fields/keys/indexes/sequences, everything based in the XMLDB object. More...
 
 create_temp_table (xmldb_table $xmldb_table)
 This function will create the temporary table passed as argument with all its fields/keys/indexes/sequences, everything based in the XMLDB object. More...
 
 delete_tables_from_xmldb_file ($file)
 This function will delete all tables found in XMLDB file from db. More...
 
 dispose ()
 Releases all resources.
 
 drop_field (xmldb_table $xmldb_table, xmldb_field $xmldb_field)
 This function will drop the field from the table passed as arguments. More...
 
 drop_index ($xmldb_table, $xmldb_intex)
 This function will drop the index in the table passed as arguments Before dropping the index, the function will check it exists. More...
 
 drop_key (xmldb_table $xmldb_table, xmldb_key $xmldb_key)
 This function will drop the key in the table passed as arguments. More...
 
 drop_table (xmldb_table $xmldb_table)
 This function will drop the table passed as argument and all the associated objects (keys, indexes, constraints, sequences, triggers) will be dropped too. More...
 
 drop_temp_table (xmldb_table $xmldb_table)
 This function will drop the temporary table passed as argument with all its fields/keys/indexes/sequences, everything based in the XMLDB object. More...
 
 field_exists ($table, $field)
 Given one xmldb_field, check if it exists in DB (true/false). More...
 
 find_index_name (xmldb_table $xmldb_table, xmldb_index $xmldb_index, $returnall=false)
 Given one xmldb_index, the function returns the name of the index in DB of false if it doesn't exist. More...
 
 find_key_name (xmldb_table $xmldb_table, xmldb_key $xmldb_key)
 This function IS NOT IMPLEMENTED. More...
 
 get_install_xml_files ()
 Get the list of install.xml files. More...
 
 get_install_xml_schema ()
 Reads the install.xml files for Moodle core and modules and returns an array of xmldb_structure object with xmldb_table from these files. More...
 
 index_exists (xmldb_table $xmldb_table, xmldb_index $xmldb_index)
 Given one xmldb_index, check if it exists in DB (true/false). More...
 
 install_from_xmldb_file ($file)
 This function will load one entire XMLDB file and call install_from_xmldb_structure. More...
 
 install_from_xmldb_structure ($xmldb_structure)
 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. More...
 
 install_one_table_from_xmldb_file ($file, $tablename, $cachestructures=false)
 This function will load one entire XMLDB file and call install_from_xmldb_structure. More...
 
 rename_field (xmldb_table $xmldb_table, xmldb_field $xmldb_field, $newname)
 This function will rename the field in the table passed as arguments Before renaming the field, the function will check it exists. More...
 
 rename_index ($xmldb_table, $xmldb_intex, $newname)
 This function will rename the index in the table passed as arguments Before renaming the index, the function will check it exists Experimental. More...
 
 rename_key (xmldb_table $xmldb_table, xmldb_key $xmldb_key, $newname)
 This function will rename the key in the table passed as arguments Experimental. More...
 
 rename_table (xmldb_table $xmldb_table, $newname)
 This function will rename the table passed as argument Before renaming the index, the function will check it exists. More...
 
 reset_sequence ($table)
 Reset a sequence to the id field of a table. More...
 
 table_exists ($table)
 Given one xmldb_table, check if it exists in DB (true/false). More...
 

Public Attributes

sql_generator $generator
 A driver specific SQL generator instance. More...
 

Protected Member Functions

 execute_sql ($sql)
 Execute a given sql command string. More...
 
 execute_sql_arr (array $sqlarr, $tablenames=null)
 This function will execute an array of SQL commands. More...
 

Protected Attributes

moodle_database $mdb
 A moodle_database driver specific instance.
 

Detailed Description

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.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

database_manager::__construct (   $mdb,
  $generator 
)

Creates a new database manager instance.

Parameters
moodle_database$mdbA moodle_database driver specific instance.
sql_generator$generatorA driver specific SQL generator instance.

Member Function Documentation

◆ add_field()

database_manager::add_field ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field 
)

This function will add the field to the table passed as arguments.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldIndex object (full specs are required).
Return values
void

◆ add_index()

database_manager::add_index (   $xmldb_table,
  $xmldb_intex 
)

This function will create the index in the table passed as arguments Before creating the index, the function will check it doesn't exists.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_index$xmldb_intexIndex object (full specs are required).
Return values
void

◆ add_key()

database_manager::add_key ( xmldb_table  $xmldb_table,
xmldb_key  $xmldb_key 
)

This function will create the key in the table passed as arguments.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_key$xmldb_keyIndex object (full specs are required).
Return values
void

◆ change_field_default()

database_manager::change_field_default ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field 
)

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.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldIndex object (full specs are required).
Return values
void

◆ change_field_notnull()

database_manager::change_field_notnull ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field 
)

This function will change the nullability of the field in the table passed as arguments.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldIndex object (full specs are required).
Return values
void

◆ change_field_precision()

database_manager::change_field_precision ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field 
)

This function will change the precision of the field in the table passed as arguments.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldIndex object (full specs are required).
Return values
void

◆ change_field_type()

database_manager::change_field_type ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field 
)

This function will change the type of the field in the table passed as arguments.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldIndex object (full specs are required).
Return values
void

◆ change_field_unsigned()

database_manager::change_field_unsigned ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field 
)

This function will change the unsigned/signed of the field in the table passed as arguments.

Deprecated:
since 2.3, only singed numbers are allowed now, migration is automatic
Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldField object (full specs are required).
Return values
void

◆ check_database_schema()

database_manager::check_database_schema ( xmldb_structure  $schema,
array  $options = null 
)

Checks the database schema against a schema specified by an xmldb_structure object.

Parameters
xmldb_structure$schemaexport schema describing all known tables
array$options
Return values
arraykeyed by table name with array of difference messages as values

◆ create_table()

database_manager::create_table ( xmldb_table  $xmldb_table)

This function will create the table passed as argument with all its fields/keys/indexes/sequences, everything based in the XMLDB object.

Parameters
xmldb_table$xmldb_tableTable object (full specs are required).
Return values
void

◆ create_temp_table()

database_manager::create_temp_table ( xmldb_table  $xmldb_table)

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!

Parameters
xmldb_table$xmldb_tableTable object (full specs are required).
Return values
void

◆ delete_tables_from_xmldb_file()

database_manager::delete_tables_from_xmldb_file (   $file)

This function will delete all tables found in XMLDB file from db.

Parameters
string$fileFull path to the XML file to be used.
Return values
void

◆ drop_field()

database_manager::drop_field ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field 
)

This function will drop the field from the table passed as arguments.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldIndex object (full specs are required).
Return values
void

◆ drop_index()

database_manager::drop_index (   $xmldb_table,
  $xmldb_intex 
)

This function will drop the index in the table passed as arguments Before dropping the index, the function will check it exists.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_index$xmldb_intexIndex object (full specs are required).
Return values
void

◆ drop_key()

database_manager::drop_key ( xmldb_table  $xmldb_table,
xmldb_key  $xmldb_key 
)

This function will drop the key in the table passed as arguments.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_key$xmldb_keyKey object (full specs are required).
Return values
void

◆ drop_table()

database_manager::drop_table ( xmldb_table  $xmldb_table)

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

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
Return values
void

◆ drop_temp_table()

database_manager::drop_temp_table ( xmldb_table  $xmldb_table)

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.

Deprecated:
since 2.3, use drop_table() for all table types
Parameters
xmldb_table$xmldb_tableTable object.
Return values
void

◆ execute_sql()

database_manager::execute_sql (   $sql)
protected

Execute a given sql command string.

Parameters
string$sqlThe sql string you wish to be executed.
Exceptions
ddl_change_structure_exceptionThis exception is thrown if any error is found.

◆ execute_sql_arr()

database_manager::execute_sql_arr ( array  $sqlarr,
  $tablenames = null 
)
protected

This function will execute an array of SQL commands.

Parameters
string[]$sqlarrArray of sql statements to execute.
array | null$tablenamesan array of xmldb table names affected by this request.
Exceptions
ddl_change_structure_exceptionThis exception is thrown if any error is found.

◆ field_exists()

database_manager::field_exists (   $table,
  $field 
)

Given one xmldb_field, check if it exists in DB (true/false).

Parameters
string | xmldb_table$tableThe table to be searched (string name or xmldb_table instance).
string | xmldb_field$fieldThe field to be searched for (string name or xmldb_field instance).
Return values
booleantrue is exists false otherwise.
Exceptions
ddl_table_missing_exception

◆ find_index_name()

database_manager::find_index_name ( xmldb_table  $xmldb_table,
xmldb_index  $xmldb_index,
  $returnall = false 
)

Given one xmldb_index, the function returns the name of the index in DB of false if it doesn't exist.

Parameters
xmldb_table$xmldb_tabletable to be searched
xmldb_index$xmldb_indexthe index to be searched
bool$returnalltrue means return array of all indexes, false means first index only as string
Return values
array|string|boolIndex name, array of index names or false if no indexes are found.
Exceptions
ddl_table_missing_exceptionThrown when table is not found.

◆ find_key_name()

database_manager::find_key_name ( xmldb_table  $xmldb_table,
xmldb_key  $xmldb_key 
)

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

Parameters
xmldb_table$xmldb_tableThe table to be searched.
xmldb_key$xmldb_keyThe key to be searched.
Return values
stringkey name if found

◆ get_install_xml_files()

database_manager::get_install_xml_files ( )

Get the list of install.xml files.

Return values
array

◆ get_install_xml_schema()

database_manager::get_install_xml_schema ( )

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 values
xmldb_structureschema from install.xml files

◆ index_exists()

database_manager::index_exists ( xmldb_table  $xmldb_table,
xmldb_index  $xmldb_index 
)

Given one xmldb_index, check if it exists in DB (true/false).

Parameters
xmldb_table$xmldb_tableThe table to be searched.
xmldb_index$xmldb_indexThe index to be searched for.
Return values
booleantrue id index exists, false otherwise.

◆ install_from_xmldb_file()

database_manager::install_from_xmldb_file (   $file)

This function will load one entire XMLDB file and call install_from_xmldb_structure.

Parameters
string$filefull path to the XML file to be used
Return values
void

◆ install_from_xmldb_structure()

database_manager::install_from_xmldb_structure (   $xmldb_structure)

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.

Parameters
stdClass$xmldb_structurexmldb_structure object.
Return values
void

◆ install_one_table_from_xmldb_file()

database_manager::install_one_table_from_xmldb_file (   $file,
  $tablename,
  $cachestructures = false 
)

This function will load one entire XMLDB file and call install_from_xmldb_structure.

Parameters
string$filefull path to the XML file to be used
string$tablenamethe name of the table.
bool$cachestructuresboolean to decide if loaded xmldb structures can be safely cached useful for testunits loading the enormous main xml file hundred of times (100x)

◆ rename_field()

database_manager::rename_field ( xmldb_table  $xmldb_table,
xmldb_field  $xmldb_field,
  $newname 
)

This function will rename the field in the table passed as arguments Before renaming the field, the function will check it exists.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_field$xmldb_fieldIndex object (full specs are required).
string$newnameNew name of the field.
Return values
void

◆ rename_index()

database_manager::rename_index (   $xmldb_table,
  $xmldb_intex,
  $newname 
)

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!

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_index$xmldb_intexIndex object (full specs are required).
string$newnameNew name of the index.
Return values
void

◆ rename_key()

database_manager::rename_key ( xmldb_table  $xmldb_table,
xmldb_key  $xmldb_key,
  $newname 
)

This function will rename the key in the table passed as arguments Experimental.

Shouldn't be used at all in normal installation/upgrade!

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
xmldb_key$xmldb_keykey object (full specs are required).
string$newnameNew name of the key.
Return values
void

◆ rename_table()

database_manager::rename_table ( xmldb_table  $xmldb_table,
  $newname 
)

This function will rename the table passed as argument Before renaming the index, the function will check it exists.

Parameters
xmldb_table$xmldb_tableTable object (just the name is mandatory).
string$newnameNew name of the index.
Return values
void

◆ reset_sequence()

database_manager::reset_sequence (   $table)

Reset a sequence to the id field of a table.

Parameters
string | xmldb_table$tableName of table.
Exceptions
ddl_exceptionthrown upon reset errors.

◆ table_exists()

database_manager::table_exists (   $table)

Given one xmldb_table, check if it exists in DB (true/false).

Parameters
string | xmldb_table$tableThe table to be searched (string name or xmldb_table instance).
Return values
boolTrue is a table exists, false otherwise.

Member Data Documentation

◆ $generator

sql_generator database_manager::$generator

A driver specific SQL generator instance.

Public because XMLDB editor needs to access it.


The documentation for this class was generated from the following file: