PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ddllib.php

https://github.com/borrown/moodle
PHP | 149 lines | 51 code | 17 blank | 81 comment | 1 complexity | 90c9526057f16f6d49935ba0f9694dd2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This library includes all the required functions used to handle the DB
  18. * structure (DDL) independently of the underlying RDBMS in use.
  19. *
  20. * This library includes all the required functions used to handle the DB
  21. * structure (DDL) independently of the underlying RDBMS in use. All the functions
  22. * rely on the XMLDBDriver classes to be able to generate the correct SQL
  23. * syntax needed by each DB.
  24. *
  25. * To define any structure to be created we'll use the schema defined
  26. * by the XMLDB classes, for tables, fields, indexes, keys and other
  27. * statements instead of direct handling of SQL sentences.
  28. *
  29. * This library should be used, exclusively, by the installation and
  30. * upgrade process of Moodle.
  31. *
  32. * For further documentation, visit {@link http://docs.moodle.org/en/DDL_functions}
  33. *
  34. * @package core
  35. * @subpackage ddl
  36. * @copyright 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
  37. * 2008 Petr Skoda http://skodak.org
  38. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39. */
  40. defined('MOODLE_INTERNAL') || die();
  41. // Add required library
  42. require_once($CFG->libdir.'/xmlize.php');
  43. // Add required XMLDB constants
  44. require_once($CFG->libdir.'/xmldb/xmldb_constants.php');
  45. // Add required XMLDB DB classes
  46. require_once($CFG->libdir.'/xmldb/xmldb_object.php');
  47. // Add required XMLDB DB classes
  48. require_once($CFG->libdir.'/xmldb/xmldb_file.php');
  49. // Add required XMLDB DB classes
  50. require_once($CFG->libdir.'/xmldb/xmldb_structure.php');
  51. // Add required XMLDB DB classes
  52. require_once($CFG->libdir.'/xmldb/xmldb_table.php');
  53. // Add required XMLDB DB classes
  54. require_once($CFG->libdir.'/xmldb/xmldb_field.php');
  55. // Add required XMLDB DB classes
  56. require_once($CFG->libdir.'/xmldb/xmldb_key.php');
  57. // Add required XMLDB DB classes
  58. require_once($CFG->libdir.'/xmldb/xmldb_index.php');
  59. require_once($CFG->libdir.'/ddl/sql_generator.php');
  60. require_once($CFG->libdir.'/ddl/database_manager.php');
  61. /**
  62. * DDL exception class, use instead of print_error() and "return false;" in ddl code.
  63. */
  64. class ddl_exception extends moodle_exception {
  65. /**
  66. * @param string $errorcode
  67. * @param string $debuginfo
  68. */
  69. function __construct($errorcode, $a=NULL, $debuginfo=null) {
  70. parent::__construct($errorcode, '', '', $a, $debuginfo);
  71. }
  72. }
  73. /**
  74. * Table does not exist problem exception
  75. */
  76. class ddl_table_missing_exception extends ddl_exception {
  77. /**
  78. * @param string $tablename
  79. * @param string $debuginfo
  80. */
  81. function __construct($tablename, $debuginfo=null) {
  82. parent::__construct('ddltablenotexist', $tablename, $debuginfo);
  83. }
  84. }
  85. /**
  86. * Table does not exist problem exception
  87. */
  88. class ddl_field_missing_exception extends ddl_exception {
  89. /**
  90. * @param string $fieldname
  91. * @param string $tablename
  92. * @param string $debuginfo
  93. */
  94. function __construct($fieldname, $tablename, $debuginfo=null) {
  95. $a = new stdClass();
  96. $a->fieldname = $fieldname;
  97. $a->tablename = $tablename;
  98. parent::__construct('ddlfieldnotexist', $a, $debuginfo);
  99. }
  100. }
  101. /**
  102. * Error during changing db structure
  103. */
  104. class ddl_change_structure_exception extends ddl_exception {
  105. /** @var string */
  106. public $error;
  107. public $sql;
  108. /**
  109. * @param string $error
  110. * @param string $sql
  111. */
  112. function __construct($error, $sql=null) {
  113. $this->error = $error;
  114. $this->sql = $sql;
  115. $errorinfo = $error."\n".$sql;
  116. parent::__construct('ddlexecuteerror', NULL, $errorinfo);
  117. }
  118. }
  119. /**
  120. * Error changing db structure, caused by some dependency found
  121. * like trying to modify one field having related indexes.
  122. */
  123. class ddl_dependency_exception extends ddl_exception {
  124. function __construct($targettype, $targetname, $offendingtype, $offendingname, $debuginfo=null) {
  125. $a = new stdClass();
  126. $a->targettype = $targettype;
  127. $a->targetname = $targetname;
  128. $a->offendingtype = $offendingtype;
  129. $a->offendingname = $offendingname;
  130. parent::__construct('ddldependencyerror', $a, $debuginfo);
  131. }
  132. }