3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 // This library includes all the required functions used to handle the DB
28 // structure (DDL) independently of the underlying RDBMS in use. All the functions
29 // rely on the XMLDBDriver classes to be able to generate the correct SQL
30 // syntax needed by each DB.
32 // To define any structure to be created we'll use the schema defined
33 // by the XMLDB classes, for tables, fields, indexes, keys and other
34 // statements instead of direct handling of SQL sentences.
36 // This library should be used, exclusively, by the installation and
37 // upgrade process of Moodle.
39 // For further documentation, visit http://docs.moodle.org/en/DDL_functions
41 /// Add required XMLDB constants
42 require_once($CFG->libdir
. '/xmldb/classes/XMLDBConstants.php');
44 /// Add main XMLDB Generator
45 require_once($CFG->libdir
. '/xmldb/classes/generators/XMLDBGenerator.class.php');
47 /// Add required XMLDB DB classes
48 require_once($CFG->libdir
. '/xmldb/classes/XMLDBObject.class.php');
49 require_once($CFG->libdir
. '/xmldb/classes/XMLDBFile.class.php');
50 require_once($CFG->libdir
. '/xmldb/classes/XMLDBStructure.class.php');
51 require_once($CFG->libdir
. '/xmldb/classes/XMLDBTable.class.php');
52 require_once($CFG->libdir
. '/xmldb/classes/XMLDBField.class.php');
53 require_once($CFG->libdir
. '/xmldb/classes/XMLDBKey.class.php');
54 require_once($CFG->libdir
. '/xmldb/classes/XMLDBIndex.class.php');
55 require_once($CFG->libdir
. '/xmldb/classes/XMLDBStatement.class.php');
57 /// Based on $CFG->dbtype, add the proper generator class
58 if (!file_exists($CFG->libdir
. '/xmldb/classes/generators/' . $CFG->dbtype
. '/' . $CFG->dbtype
. '.class.php')) {
59 error ('DB Type: ' . $CFG->dbtype
. ' not supported by XMLDDB');
61 require_once($CFG->libdir
. '/xmldb/classes/generators/' . $CFG->dbtype
. '/' . $CFG->dbtype
. '.class.php');
64 /// Add other libraries
65 require_once($CFG->libdir
. '/xmlize.php');
67 * Add a new field to a table, or modify an existing one (if oldfield is defined).
68 * Warning: Please be careful on primary keys, as this function will eat auto_increments
72 * @param string $table the name of the table to modify. (Without the prefix.)
73 * @param string $oldfield If changing an existing column, the name of that column.
74 * @param string $field The name of the column at the end of the operation.
75 * @param string $type The type of the column at the end of the operation. TEXT, VARCHAR, CHAR, INTEGER, REAL, or TINYINT
76 * @param string $size The size of that column type. As in VARCHAR($size), or INTEGER($size).
77 * @param string $signed For numeric column types, whether that column is 'signed' or 'unsigned'.
78 * @param string $default The new default value for the column.
79 * @param string $null 'not null', or '' to allow nulls.
80 * @param string $after Which column to insert this one after. Not supported on Postgres.
82 * @return boolean Wheter the operation succeeded.
84 function table_column($table, $oldfield, $field, $type='integer', $size='10',
85 $signed='unsigned', $default='0', $null='not null', $after='') {
86 global $CFG, $db, $empty_rs_cache;
88 if (!empty($empty_rs_cache[$table])) { // Clear the recordset cache because it's out of date
89 unset($empty_rs_cache[$table]);
92 switch (strtolower($CFG->dbtype
)) {
97 switch (strtolower($type)) {
103 $type = 'INTEGER('. $size .')';
106 $type = 'VARCHAR('. $size .')';
110 $type = 'CHAR('. $size .')';
115 if (!empty($oldfield)) {
116 $operation = 'CHANGE '. $oldfield .' '. $field;
118 $operation = 'ADD '. $field;
121 $default = 'DEFAULT \''. $default .'\'';
123 if (!empty($after)) {
124 $after = 'AFTER `'. $after .'`';
127 return execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' '. $operation .' '. $type .' '. $signed .' '. $default .' '. $null .' '. $after);
129 case 'postgres7': // From Petri Asikainen
131 $dbinfo = $db->ServerInfo();
132 $dbver = substr($dbinfo['version'],0,3);
134 //to prevent conflicts with reserved words
135 $realfield = '"'. $field .'"';
136 $field = '"'. $field .'_alter_column_tmp"';
137 $oldfield = '"'. $oldfield .'"';
139 switch (strtolower($type)) {
153 $type = 'VARCHAR('. $size .')';
156 $type = 'CHAR('. $size .')';
161 $default = '\''. $default .'\'';
163 //After is not implemented in postgesql
164 //if (!empty($after)) {
165 // $after = "AFTER '$after'";
169 execute_sql('BEGIN');
171 //Always use temporary column
172 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' ADD COLUMN '. $field .' '. $type);
174 execute_sql('UPDATE '. $CFG->prefix
. $table .' SET '. $field .'='. $default);
177 if ($dbver >= '7.3') {
178 // modifying 'not null' is posible before 7.3
179 //update default values to table
180 if (strtoupper($null) == 'NOT NULL') {
181 execute_sql('UPDATE '. $CFG->prefix
. $table .' SET '. $field .'='. $default .' WHERE '. $field .' IS NULL');
182 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' ALTER COLUMN '. $field .' SET '. $null);
184 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' ALTER COLUMN '. $field .' DROP NOT NULL');
188 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' ALTER COLUMN '. $field .' SET DEFAULT '. $default);
190 if ( $oldfield != '""' ) {
192 // We are changing the type of a column. This may require doing some casts...
194 $oldtype = column_type($table, $oldfield);
195 $newtype = column_type($table, $field);
197 // Do we need a cast?
198 if($newtype == 'N' && $oldtype == 'C') {
199 $casting = 'CAST(CAST('.$oldfield.' AS TEXT) AS REAL)';
201 else if($newtype == 'I' && $oldtype == 'C') {
202 $casting = 'CAST(CAST('.$oldfield.' AS TEXT) AS INTEGER)';
205 $casting = $oldfield;
208 // Run the update query, casting as necessary
209 execute_sql('UPDATE '. $CFG->prefix
. $table .' SET '. $field .' = '. $casting);
210 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' DROP COLUMN '. $oldfield);
213 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' RENAME COLUMN '. $field .' TO '. $realfield);
215 return execute_sql('COMMIT');
218 switch (strtolower($type)) {
227 $default = 'DEFAULT \''. $default .'\'';
229 if (!empty($after)) {
230 $after = 'AFTER '. $after;
233 if (!empty($oldfield)) {
234 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' RENAME COLUMN '. $oldfield .' '. $field);
236 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' ADD COLUMN '. $field .' '. $type);
239 execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' ALTER COLUMN '. $field .' SET '. $null);
240 return execute_sql('ALTER TABLE '. $CFG->prefix
. $table .' ALTER COLUMN '. $field .' SET '. $default);
245 * Given one XMLDBTable, check if it exists in DB (true/false)
247 * @param XMLDBTable table to be searched for
248 * @return boolean true/false
250 function table_exists($table) {
256 /// Do this function silenty (to avoid output in install/upgrade process)
257 $olddbdebug = $db->debug
;
260 /// Load the needed generator
261 $classname = 'XMLDB' . $CFG->dbtype
;
262 $generator = new $classname();
263 $generator->setPrefix($CFG->prefix
);
264 /// Calculate the name of the table
265 $tablename = $generator->getTableName($table, false);
267 /// Search such tablename in DB
268 $metatables = $db->MetaTables();
269 $metatables = array_flip($metatables);
270 $metatables = array_change_key_case($metatables, CASE_LOWER
);
271 if (!array_key_exists($tablename, $metatables)) {
275 /// Re-set original debug
276 $db->debug
= $olddbdebug;
282 * Given one XMLDBField, check if it exists in DB (true/false)
285 * @param XMLDBTable the table
286 * @param XMLDBField the field to be searched for
287 * @return boolean true/false
289 function field_exists($table, $field) {
295 /// Do this function silenty (to avoid output in install/upgrade process)
296 $olddbdebug = $db->debug
;
299 /// Check the table exists
300 if (!table_exists($table)) {
301 $db->debug
= $olddbdebug; //Re-set original $db->debug
305 /// Load the needed generator
306 $classname = 'XMLDB' . $CFG->dbtype
;
307 $generator = new $classname();
308 $generator->setPrefix($CFG->prefix
);
309 /// Calculate the name of the table
310 $tablename = $generator->getTableName($table, false);
312 /// Get list of fields in table
314 if ($fields = $db->MetaColumns($tablename)) {
315 $fields = array_change_key_case($fields, CASE_LOWER
);
318 if (!array_key_exists($field->getName(), $fields)) {
322 /// Re-set original debug
323 $db->debug
= $olddbdebug;
329 * Given one XMLDBIndex, check if it exists in DB (true/false)
332 * @param XMLDBTable the table
333 * @param XMLDBIndex the index to be searched for
334 * @return boolean true/false
336 function index_exists($table, $index) {
342 /// Do this function silenty (to avoid output in install/upgrade process)
343 $olddbdebug = $db->debug
;
346 /// Wrap over find_index_name to see if the index exists
347 if (!find_index_name($table, $index)) {
351 /// Re-set original debug
352 $db->debug
= $olddbdebug;
358 * This function IS NOT IMPLEMENTED. ONCE WE'LL BE USING RELATIONAL
359 * INTEGRITY IT WILL BECOME MORE USEFUL. FOR NOW, JUST CALCULATE "OFFICIAL"
360 * KEY NAMES WITHOUT ACCESSING TO DB AT ALL.
361 * Given one XMLDBKey, the function returns the name of the key in DB (if exists)
362 * of false if it doesn't exist
365 * @param XMLDBTable the table to be searched
366 * @param XMLDBKey the key to be searched
367 * @return string key name of false
369 function find_key_name($table, $xmldb_key) {
373 /// Extract key columns
374 $keycolumns = $xmldb_key->getFields();
376 /// Get list of keys in table
377 /// first primaries (we aren't going to use this now, because the MetaPrimaryKeys is awful)
378 ///TODO: To implement when we advance in relational integrity
379 /// then uniques (note that Moodle, for now, shouldn't have any UNIQUE KEY for now, but unique indexes)
380 ///TODO: To implement when we advance in relational integrity (note that AdoDB hasn't any MetaXXX for this.
381 /// then foreign (note that Moodle, for now, shouldn't have any FOREIGN KEY for now, but indexes)
382 ///TODO: To implement when we advance in relational integrity (note that AdoDB has one MetaForeignKeys()
383 ///but it's far from perfect.
384 /// TODO: To create the proper functions inside each generator to retrieve all the needed KEY info (name
385 /// columns, reftable and refcolumns
387 /// So all we do is to return the official name of the requested key without any confirmation!)
388 $classname = 'XMLDB' . $CFG->dbtype
;
389 $generator = new $classname();
390 $generator->setPrefix($CFG->prefix
);
391 /// One exception, harcoded primary constraint names
392 if ($generator->primary_key_name
&& $xmldb_key->getType() == XMLDB_KEY_PRIMARY
) {
393 return $generator->primary_key_name
;
395 /// Calculate the name suffix
396 switch ($xmldb_key->getType()) {
397 case XMLDB_KEY_PRIMARY
:
400 case XMLDB_KEY_UNIQUE
:
403 case XMLDB_KEY_FOREIGN_UNIQUE
:
404 case XMLDB_KEY_FOREIGN
:
408 /// And simply, return the oficial name
409 return $generator->getNameForObject($table->getName(), implode(', ', $xmldb_key->getFields()), $suffix);
414 * Given one XMLDBIndex, the function returns the name of the index in DB (if exists)
415 * of false if it doesn't exist
418 * @param XMLDBTable the table to be searched
419 * @param XMLDBIndex the index to be searched
420 * @return string index name of false
422 function find_index_name($table, $index) {
426 /// Do this function silenty (to avoid output in install/upgrade process)
427 $olddbdebug = $db->debug
;
430 /// Extract index columns
431 $indcolumns = $index->getFields();
433 /// Check the table exists
434 if (!table_exists($table)) {
435 $db->debug
= $olddbdebug; //Re-set original $db->debug
439 /// Load the needed generator
440 $classname = 'XMLDB' . $CFG->dbtype
;
441 $generator = new $classname();
442 $generator->setPrefix($CFG->prefix
);
443 /// Calculate the name of the table
444 $tablename = $generator->getTableName($table, false);
446 /// Get list of indexes in table
448 if ($indexes = $db->MetaIndexes($tablename)) {
449 $indexes = array_change_key_case($indexes, CASE_LOWER
);
452 /// Iterate over them looking for columns coincidence
454 foreach ($indexes as $indexname => $index) {
455 $columns = $index['columns'];
456 /// Lower case column names
457 $columns = array_flip($columns);
458 $columns = array_change_key_case($columns, CASE_LOWER
);
459 $columns = array_flip($columns);
460 /// Check if index matchs queried index
461 $diferences = array_merge(array_diff($columns, $indcolumns), array_diff($indcolumns, $columns));
462 /// If no diferences, we have find the index
463 if (empty($diferences)) {
464 $db->debug
= $olddbdebug; //Re-set original $db->debug
469 /// Arriving here, index not found
470 $db->debug
= $olddbdebug; //Re-set original $db->debug
475 * Given one XMLDBTable, the function returns the name of its sequence in DB (if exists)
476 * of false if it doesn't exist
478 * @param XMLDBTable the table to be searched
479 * @return string sequence name of false
481 function find_sequence_name($table) {
485 $sequencename = false;
487 /// Do this function silenty (to avoid output in install/upgrade process)
488 $olddbdebug = $db->debug
;
491 if (strtolower(get_class($table)) != 'xmldbtable') {
492 $db->debug
= $olddbdebug; //Re-set original $db->debug
496 /// Check table exists
497 if (!table_exists($table)) {
498 debugging('Table ' . $table->getName() . ' do not exist. Sequence not found', DEBUG_DEVELOPER
);
499 $db->debug
= $olddbdebug; //Re-set original $db->debug
500 return false; //Table doesn't exist, nothing to do
503 $sequencename = $table->getSequenceFromDB($CFG->dbtype
, $CFG->prefix
);
505 $db->debug
= $olddbdebug; //Re-set original $db->debug
506 return $sequencename;
510 * This function will load one entire XMLDB file, generating all the needed
511 * SQL statements, specific for each RDBMS ($CFG->dbtype) and, finally, it
512 * will execute all those statements against the DB.
515 * @param $file full path to the XML file to be used
516 * @return boolean (true on success, false on error)
518 function install_from_xmldb_file($file) {
525 $xmldb_file = new XMLDBFile($file);
527 if (!$xmldb_file->fileExists()) {
531 $loaded = $xmldb_file->loadXMLStructure();
532 if (!$loaded ||
!$xmldb_file->isLoaded()) {
533 /// Show info about the error if we can find it
534 if ($structure =& $xmldb_file->getStructure()) {
535 if ($errors = $structure->getAllErrors()) {
536 notify('Errors found in XMLDB file: '. implode (', ', $errors));
542 $structure = $xmldb_file->getStructure();
544 if (!$sqlarr = $structure->getCreateStructureSQL($CFG->dbtype
, $CFG->prefix
, false)) {
545 return true; //Empty array = nothing to do = no error
548 return execute_sql_arr($sqlarr);
552 * This function will create the table passed as argument with all its
553 * fields/keys/indexes/sequences, everything based in the XMLDB object
556 * @param XMLDBTable table object (full specs are required)
557 * @param boolean continue to specify if must continue on error (true) or stop (false)
558 * @param boolean feedback to specify to show status info (true) or not (false)
559 * @return boolean true on success, false on error
561 function create_table($table, $continue=true, $feedback=true) {
567 if (strtolower(get_class($table)) != 'xmldbtable') {
571 /// Check table doesn't exist
572 if (table_exists($table)) {
573 debugging('Table ' . $table->getName() . ' exists. Create skipped', DEBUG_DEVELOPER
);
574 return true; //Table exists, nothing to do
577 if(!$sqlarr = $table->getCreateTableSQL($CFG->dbtype
, $CFG->prefix
, false)) {
578 return true; //Empty array = nothing to do = no error
581 return execute_sql_arr($sqlarr, $continue, $feedback);
585 * This function will drop the table passed as argument
586 * and all the associated objects (keys, indexes, constaints, sequences, triggers)
587 * will be dropped too.
590 * @param XMLDBTable table object (just the name is mandatory)
591 * @param boolean continue to specify if must continue on error (true) or stop (false)
592 * @param boolean feedback to specify to show status info (true) or not (false)
593 * @return boolean true on success, false on error
595 function drop_table($table, $continue=true, $feedback=true) {
601 if (strtolower(get_class($table)) != 'xmldbtable') {
605 /// Check table exists
606 if (!table_exists($table)) {
607 debugging('Table ' . $table->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER
);
608 return true; //Table don't exist, nothing to do
611 if(!$sqlarr = $table->getDropTableSQL($CFG->dbtype
, $CFG->prefix
, false)) {
612 return true; //Empty array = nothing to do = no error
615 return execute_sql_arr($sqlarr, $continue, $feedback);
619 * This function will rename the table passed as argument
620 * Before renaming the index, the function will check it exists
623 * @param XMLDBTable table object (just the name is mandatory)
624 * @param string new name of the index
625 * @param boolean continue to specify if must continue on error (true) or stop (false)
626 * @param boolean feedback to specify to show status info (true) or not (false)
627 * @return boolean true on success, false on error
629 function rename_table($table, $newname, $continue=true, $feedback=true) {
635 if (strtolower(get_class($table)) != 'xmldbtable') {
639 /// Check table exists
640 if (!table_exists($table)) {
641 debugging('Table ' . $table->getName() . ' do not exist. Rename skipped', DEBUG_DEVELOPER
);
642 return true; //Table doesn't exist, nothing to do
645 /// Check new table doesn't exist
646 $check = new XMLDBTable($newname);
647 if (table_exists($check)) {
648 debugging('Table ' . $check->getName() . ' already exists. Rename skipped', DEBUG_DEVELOPER
);
649 return true; //Table exists, nothing to do
652 /// Check newname isn't empty
654 debugging('New name for table ' . $index->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER
);
655 return true; //Table doesn't exist, nothing to do
658 if(!$sqlarr = $table->getRenameTableSQL($CFG->dbtype
, $CFG->prefix
, $newname, false)) {
659 return true; //Empty array = nothing to do = no error
662 return execute_sql_arr($sqlarr, $continue, $feedback);
666 * This function will add the field to the table passed as arguments
669 * @param XMLDBTable table object (just the name is mandatory)
670 * @param XMLDBField field object (full specs are required)
671 * @param boolean continue to specify if must continue on error (true) or stop (false)
672 * @param boolean feedback to specify to show status info (true) or not (false)
673 * @return boolean true on success, false on error
675 function add_field($table, $field, $continue=true, $feedback=true) {
681 if (strtolower(get_class($table)) != 'xmldbtable') {
684 if (strtolower(get_class($field)) != 'xmldbfield') {
688 /// Check the field doesn't exist
689 if (field_exists($table, $field)) {
690 debugging('Field ' . $field->getName() . ' exists. Create skipped', DEBUG_DEVELOPER
);
694 /// If NOT NULL and no default given, check the table is empty
695 if ($field->getNotNull() && $field->getDefault() === NULL && count_records($table->getName())) {
696 debugging('Field ' . $field->getName() . ' cannot be added. Not null fields added to non empty tables require default value. Create skipped', DEBUG_DEVELOPER
);
700 if(!$sqlarr = $table->getAddFieldSQL($CFG->dbtype
, $CFG->prefix
, $field, false)) {
701 return true; //Empty array = nothing to do = no error
704 return execute_sql_arr($sqlarr, $continue, $feedback);
708 * This function will drop the field from the table passed as arguments
711 * @param XMLDBTable table object (just the name is mandatory)
712 * @param XMLDBField field object (just the name is mandatory)
713 * @param boolean continue to specify if must continue on error (true) or stop (false)
714 * @param boolean feedback to specify to show status info (true) or not (false)
715 * @return boolean true on success, false on error
717 function drop_field($table, $field, $continue=true, $feedback=true) {
723 if (strtolower(get_class($table)) != 'xmldbtable') {
726 if (strtolower(get_class($field)) != 'xmldbfield') {
730 /// Check the field exists
731 if (!field_exists($table, $field)) {
732 debugging('Field ' . $field->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER
);
736 if(!$sqlarr = $table->getDropFieldSQL($CFG->dbtype
, $CFG->prefix
, $field, false)) {
737 return true; //Empty array = nothing to do = no error
740 return execute_sql_arr($sqlarr, $continue, $feedback);
744 * This function will change the type of the field in the table passed as arguments
747 * @param XMLDBTable table object (just the name is mandatory)
748 * @param XMLDBField field object (full specs are required)
749 * @param boolean continue to specify if must continue on error (true) or stop (false)
750 * @param boolean feedback to specify to show status info (true) or not (false)
751 * @return boolean true on success, false on error
753 function change_field_type($table, $field, $continue=true, $feedback=true) {
759 if (strtolower(get_class($table)) != 'xmldbtable') {
762 if (strtolower(get_class($field)) != 'xmldbfield') {
766 if(!$sqlarr = $table->getAlterFieldSQL($CFG->dbtype
, $CFG->prefix
, $field, false)) {
767 return true; //Empty array = nothing to do = no error
770 return execute_sql_arr($sqlarr, $continue, $feedback);
774 * This function will change the precision of the field in the table passed as arguments
777 * @param XMLDBTable table object (just the name is mandatory)
778 * @param XMLDBField field object (full specs are required)
779 * @param boolean continue to specify if must continue on error (true) or stop (false)
780 * @param boolean feedback to specify to show status info (true) or not (false)
781 * @return boolean true on success, false on error
783 function change_field_precision($table, $field, $continue=true, $feedback=true) {
785 /// Just a wrapper over change_field_type. Does exactly the same processing
786 return change_field_type($table, $field, $continue, $feedback);
790 * This function will change the unsigned/signed of the field in the table passed as arguments
793 * @param XMLDBTable table object (just the name is mandatory)
794 * @param XMLDBField field object (full specs are required)
795 * @param boolean continue to specify if must continue on error (true) or stop (false)
796 * @param boolean feedback to specify to show status info (true) or not (false)
797 * @return boolean true on success, false on error
799 function change_field_unsigned($table, $field, $continue=true, $feedback=true) {
801 /// Just a wrapper over change_field_type. Does exactly the same processing
802 return change_field_type($table, $field, $continue, $feedback);
806 * This function will change the nullability of the field in the table passed as arguments
809 * @param XMLDBTable table object (just the name is mandatory)
810 * @param XMLDBField field object (full specs are required)
811 * @param boolean continue to specify if must continue on error (true) or stop (false)
812 * @param boolean feedback to specify to show status info (true) or not (false)
813 * @return boolean true on success, false on error
815 function change_field_notnull($table, $field, $continue=true, $feedback=true) {
817 /// Just a wrapper over change_field_type. Does exactly the same processing
818 return change_field_type($table, $field, $continue, $feedback);
822 * This function will change the enum status of the field in the table passed as arguments
825 * @param XMLDBTable table object (just the name is mandatory)
826 * @param XMLDBField field object (full specs are required)
827 * @param boolean continue to specify if must continue on error (true) or stop (false)
828 * @param boolean feedback to specify to show status info (true) or not (false)
829 * @return boolean true on success, false on error
831 function change_field_enum($table, $field, $continue=true, $feedback=true) {
837 if (strtolower(get_class($table)) != 'xmldbtable') {
840 if (strtolower(get_class($field)) != 'xmldbfield') {
844 if(!$sqlarr = $table->getModifyEnumSQL($CFG->dbtype
, $CFG->prefix
, $field, false)) {
845 return true; //Empty array = nothing to do = no error
848 return execute_sql_arr($sqlarr, $continue, $feedback);
851 * This function will change the default of the field in the table passed as arguments
852 * One null value in the default field means delete the default
855 * @param XMLDBTable table object (just the name is mandatory)
856 * @param XMLDBField field object (full specs are required)
857 * @param boolean continue to specify if must continue on error (true) or stop (false)
858 * @param boolean feedback to specify to show status info (true) or not (false)
859 * @return boolean true on success, false on error
861 function change_field_default($table, $field, $continue=true, $feedback=true) {
867 if (strtolower(get_class($table)) != 'xmldbtable') {
870 if (strtolower(get_class($field)) != 'xmldbfield') {
874 if(!$sqlarr = $table->getModifyDefaultSQL($CFG->dbtype
, $CFG->prefix
, $field, false)) {
875 return true; //Empty array = nothing to do = no error
878 return execute_sql_arr($sqlarr, $continue, $feedback);
882 * This function will rename the field in the table passed as arguments
883 * Before renaming the field, the function will check it exists
886 * @param XMLDBTable table object (just the name is mandatory)
887 * @param XMLDBField index object (full specs are required)
888 * @param string new name of the field
889 * @param boolean continue to specify if must continue on error (true) or stop (false)
890 * @param boolean feedback to specify to show status info (true) or not (false)
891 * @return boolean true on success, false on error
893 function rename_field($table, $field, $newname, $continue=true, $feedback=true) {
899 if (strtolower(get_class($table)) != 'xmldbtable') {
902 if (strtolower(get_class($field)) != 'xmldbfield') {
906 /// Check we have included full field specs
907 if (!$field->getType()) {
908 debugging('Field ' . $field->getName() . ' must contain full specs. Rename skipped', DEBUG_DEVELOPER
);
912 /// Check field isn't id. Renaming over that field is not allowed
913 if ($field->getName() == 'id') {
914 debugging('Field ' . $field->getName() . ' cannot be renamed. Rename skipped', DEBUG_DEVELOPER
);
915 return true; //Field is "id", nothing to do
918 /// Check field exists
919 if (!field_exists($table, $field)) {
920 debugging('Field ' . $field->getName() . ' do not exist. Rename skipped', DEBUG_DEVELOPER
);
921 return true; //Field doesn't exist, nothing to do
924 /// Check newname isn't empty
926 debugging('New name for field ' . $field->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER
);
927 return true; //Field doesn't exist, nothing to do
930 if(!$sqlarr = $table->getRenameFieldSQL($CFG->dbtype
, $CFG->prefix
, $field, $newname, false)) {
931 return true; //Empty array = nothing to do = no error
934 return execute_sql_arr($sqlarr, $continue, $feedback);
938 * This function will create the key in the table passed as arguments
941 * @param XMLDBTable table object (just the name is mandatory)
942 * @param XMLDBKey index object (full specs are required)
943 * @param boolean continue to specify if must continue on error (true) or stop (false)
944 * @param boolean feedback to specify to show status info (true) or not (false)
945 * @return boolean true on success, false on error
947 function add_key($table, $key, $continue=true, $feedback=true) {
953 if (strtolower(get_class($table)) != 'xmldbtable') {
956 if (strtolower(get_class($key)) != 'xmldbkey') {
959 if ($key->getType() == XMLDB_KEY_PRIMARY
) { // Prevent PRIMARY to be added (only in create table, being serious :-P)
960 debugging('Primary Keys can be added at table create time only', DEBUG_DEVELOPER
);
964 if(!$sqlarr = $table->getAddKeySQL($CFG->dbtype
, $CFG->prefix
, $key, false)) {
965 return true; //Empty array = nothing to do = no error
968 return execute_sql_arr($sqlarr, $continue, $feedback);
972 * This function will drop the key in the table passed as arguments
975 * @param XMLDBTable table object (just the name is mandatory)
976 * @param XMLDBKey key object (full specs are required)
977 * @param boolean continue to specify if must continue on error (true) or stop (false)
978 * @param boolean feedback to specify to show status info (true) or not (false)
979 * @return boolean true on success, false on error
981 function drop_key($table, $key, $continue=true, $feedback=true) {
987 if (strtolower(get_class($table)) != 'xmldbtable') {
990 if (strtolower(get_class($key)) != 'xmldbkey') {
993 if ($key->getType() == XMLDB_KEY_PRIMARY
) { // Prevent PRIMARY to be dropped (only in drop table, being serious :-P)
994 debugging('Primary Keys can be deleted at table drop time only', DEBUG_DEVELOPER
);
998 if(!$sqlarr = $table->getDropKeySQL($CFG->dbtype
, $CFG->prefix
, $key, false)) {
999 return true; //Empty array = nothing to do = no error
1002 return execute_sql_arr($sqlarr, $continue, $feedback);
1006 * This function will rename the key in the table passed as arguments
1007 * Experimental. Shouldn't be used at all in normal installation/upgrade!
1010 * @param XMLDBTable table object (just the name is mandatory)
1011 * @param XMLDBKey key object (full specs are required)
1012 * @param string new name of the key
1013 * @param boolean continue to specify if must continue on error (true) or stop (false)
1014 * @param boolean feedback to specify to show status info (true) or not (false)
1015 * @return boolean true on success, false on error
1017 function rename_key($table, $key, $newname, $continue=true, $feedback=true) {
1021 debugging('rename_key() is one experimental feature. You must not use it in production!', DEBUG_DEVELOPER
);
1025 if (strtolower(get_class($table)) != 'xmldbtable') {
1028 if (strtolower(get_class($key)) != 'xmldbkey') {
1032 /// Check newname isn't empty
1034 debugging('New name for key ' . $key->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER
);
1035 return true; //Key doesn't exist, nothing to do
1038 if(!$sqlarr = $table->getRenameKeySQL($CFG->dbtype
, $CFG->prefix
, $key, $newname, false)) {
1039 debugging('Some DBs do not support key renaming (MySQL, PostgreSQL, MsSQL). Rename skipped', DEBUG_DEVELOPER
);
1040 return true; //Empty array = nothing to do = no error
1043 return execute_sql_arr($sqlarr, $continue, $feedback);
1047 * This function will create the index in the table passed as arguments
1048 * Before creating the index, the function will check it doesn't exists
1051 * @param XMLDBTable table object (just the name is mandatory)
1052 * @param XMLDBIndex index object (full specs are required)
1053 * @param boolean continue to specify if must continue on error (true) or stop (false)
1054 * @param boolean feedback to specify to show status info (true) or not (false)
1055 * @return boolean true on success, false on error
1057 function add_index($table, $index, $continue=true, $feedback=true) {
1063 if (strtolower(get_class($table)) != 'xmldbtable') {
1066 if (strtolower(get_class($index)) != 'xmldbindex') {
1070 /// Check index doesn't exist
1071 if (index_exists($table, $index)) {
1072 debugging('Index ' . $index->getName() . ' exists. Create skipped', DEBUG_DEVELOPER
);
1073 return true; //Index exists, nothing to do
1076 if(!$sqlarr = $table->getAddIndexSQL($CFG->dbtype
, $CFG->prefix
, $index, false)) {
1077 return true; //Empty array = nothing to do = no error
1080 return execute_sql_arr($sqlarr, $continue, $feedback);
1084 * This function will drop the index in the table passed as arguments
1085 * Before dropping the index, the function will check it exists
1088 * @param XMLDBTable table object (just the name is mandatory)
1089 * @param XMLDBIndex index object (full specs are required)
1090 * @param boolean continue to specify if must continue on error (true) or stop (false)
1091 * @param boolean feedback to specify to show status info (true) or not (false)
1092 * @return boolean true on success, false on error
1094 function drop_index($table, $index, $continue=true, $feedback=true) {
1100 if (strtolower(get_class($table)) != 'xmldbtable') {
1103 if (strtolower(get_class($index)) != 'xmldbindex') {
1107 /// Check index exists
1108 if (!index_exists($table, $index)) {
1109 debugging('Index ' . $index->getName() . ' do not exist. Delete skipped', DEBUG_DEVELOPER
);
1110 return true; //Index doesn't exist, nothing to do
1113 if(!$sqlarr = $table->getDropIndexSQL($CFG->dbtype
, $CFG->prefix
, $index, false)) {
1114 return true; //Empty array = nothing to do = no error
1117 return execute_sql_arr($sqlarr, $continue, $feedback);
1121 * This function will rename the index in the table passed as arguments
1122 * Before renaming the index, the function will check it exists
1123 * Experimental. Shouldn't be used at all!
1126 * @param XMLDBTable table object (just the name is mandatory)
1127 * @param XMLDBIndex index object (full specs are required)
1128 * @param string new name of the index
1129 * @param boolean continue to specify if must continue on error (true) or stop (false)
1130 * @param boolean feedback to specify to show status info (true) or not (false)
1131 * @return boolean true on success, false on error
1133 function rename_index($table, $index, $newname, $continue=true, $feedback=true) {
1137 debugging('rename_index() is one experimental feature. You must not use it in production!', DEBUG_DEVELOPER
);
1141 if (strtolower(get_class($table)) != 'xmldbtable') {
1144 if (strtolower(get_class($index)) != 'xmldbindex') {
1148 /// Check index exists
1149 if (!index_exists($table, $index)) {
1150 debugging('Index ' . $index->getName() . ' do not exist. Rename skipped', DEBUG_DEVELOPER
);
1151 return true; //Index doesn't exist, nothing to do
1154 /// Check newname isn't empty
1156 debugging('New name for index ' . $index->getName() . ' is empty! Rename skipped', DEBUG_DEVELOPER
);
1157 return true; //Index doesn't exist, nothing to do
1160 if(!$sqlarr = $table->getRenameIndexSQL($CFG->dbtype
, $CFG->prefix
, $index, $newname, false)) {
1161 debugging('Some DBs do not support index renaming (MySQL). Rename skipped', DEBUG_DEVELOPER
);
1162 return true; //Empty array = nothing to do = no error
1165 return execute_sql_arr($sqlarr, $continue, $feedback);
1168 /* trys to change default db encoding to utf8, if empty db
1170 function change_db_encoding() {
1172 // try forcing utf8 collation, if mysql db and no tables present
1173 if (($CFG->dbfamily
=='mysql') && !$db->Metatables()) {
1174 $SQL = 'ALTER DATABASE '.$CFG->dbname
.' CHARACTER SET utf8';
1175 execute_sql($SQL, false); // silent, if it fails it fails
1176 if (setup_is_unicodedb()) {
1177 configure_dbconnection();