3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * This library includes all the required functions used to handle the DB
21 * structure (DDL) independently of the underlying RDBMS in use.
23 * This library includes all the required functions used to handle the DB
24 * structure (DDL) independently of the underlying RDBMS in use. All the functions
25 * rely on the XMLDBDriver classes to be able to generate the correct SQL
26 * syntax needed by each DB.
28 * To define any structure to be created we'll use the schema defined
29 * by the XMLDB classes, for tables, fields, indexes, keys and other
30 * statements instead of direct handling of SQL sentences.
32 * This library should be used, exclusively, by the installation and
33 * upgrade process of Moodle.
35 * For further documentation, visit {@link http://docs.moodle.org/en/DDL_functions}
39 * @copyright 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
40 * 2008 Petr Skoda http://skodak.org
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 defined('MOODLE_INTERNAL') ||
die();
46 // Add required library
47 require_once($CFG->libdir
.'/xmlize.php');
49 // Add required XMLDB constants
50 require_once($CFG->libdir
.'/xmldb/xmldb_constants.php');
52 // Add required XMLDB DB classes
53 require_once($CFG->libdir
.'/xmldb/xmldb_object.php');
54 // Add required XMLDB DB classes
55 require_once($CFG->libdir
.'/xmldb/xmldb_file.php');
56 // Add required XMLDB DB classes
57 require_once($CFG->libdir
.'/xmldb/xmldb_structure.php');
58 // Add required XMLDB DB classes
59 require_once($CFG->libdir
.'/xmldb/xmldb_table.php');
60 // Add required XMLDB DB classes
61 require_once($CFG->libdir
.'/xmldb/xmldb_field.php');
62 // Add required XMLDB DB classes
63 require_once($CFG->libdir
.'/xmldb/xmldb_key.php');
64 // Add required XMLDB DB classes
65 require_once($CFG->libdir
.'/xmldb/xmldb_index.php');
67 require_once($CFG->libdir
.'/ddl/sql_generator.php');
68 require_once($CFG->libdir
.'/ddl/database_manager.php');
73 * DDL exception class, use instead of error() and "return false;" in ddl code.
75 class ddl_exception
extends moodle_exception
{
77 * @param string $errorcode
78 * @param string $debuginfo
80 function __construct($errorcode, $a=NULL, $debuginfo=null) {
81 parent
::__construct($errorcode, '', '', $a, $debuginfo);
86 * Table does not exist problem exception
88 class ddl_table_missing_exception
extends ddl_exception
{
90 * @param string $tablename
91 * @param string $debuginfo
93 function __construct($tablename, $debuginfo=null) {
94 parent
::__construct('ddltablenotexist', $tablename, $debuginfo);
99 * Table does not exist problem exception
101 class ddl_field_missing_exception
extends ddl_exception
{
103 * @param string $fieldname
104 * @param string $tablename
105 * @param string $debuginfo
107 function __construct($fieldname, $tablename, $debuginfo=null) {
109 $a->fieldname
= $fieldname;
110 $a->tablename
= $tablename;
111 parent
::__construct('ddlfieldnotexist', $a, $debuginfo);
116 * Error during changing db structure
118 class ddl_change_structure_exception
extends ddl_exception
{
123 * @param string $error
126 function __construct($error, $sql=null) {
127 $this->error
= $error;
129 $errorinfo = $error."\n".$sql;
130 parent
::__construct('ddlexecuteerror', NULL, $errorinfo);
135 * Error changing db structure, caused by some dependency found
136 * like trying to modify one field having related indexes.
138 class ddl_dependency_exception
extends ddl_exception
{
140 function __construct($targettype, $targetname, $offendingtype, $offendingname, $debuginfo=null) {
142 $a->targettype
= $targettype;
143 $a->targetname
= $targetname;
144 $a->offendingtype
= $offendingtype;
145 $a->offendingname
= $offendingname;
147 parent
::__construct('ddldependencyerror', $a, $debuginfo);