Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/lib/ -> ddllib.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   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.
   9  //
  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.
  14  //
  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/>.
  17  
  18  
  19  /**
  20   * This library includes all the required functions used to handle the DB
  21   * structure (DDL) independently of the underlying RDBMS in use.
  22   *
  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.
  27   *
  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.
  31   *
  32   * This library should be used, exclusively, by the installation and
  33   * upgrade process of Moodle.
  34   *
  35   * For further documentation, visit {@link http://docs.moodle.org/en/DDL_functions}
  36   *
  37   * @package    core
  38   * @subpackage ddl
  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
  42   */
  43  
  44  defined('MOODLE_INTERNAL') || die();
  45  
  46  // Add required library
  47  require_once($CFG->libdir.'/xmlize.php');
  48  
  49  // Add required XMLDB constants
  50  require_once($CFG->libdir.'/xmldb/xmldb_constants.php');
  51  
  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');
  66  
  67  require_once($CFG->libdir.'/ddl/sql_generator.php');
  68  require_once($CFG->libdir.'/ddl/database_manager.php');
  69  
  70  
  71  
  72  /**
  73   * DDL exception class, use instead of throw new \moodle_exception() and "return false;" in ddl code.
  74   */
  75  class ddl_exception extends moodle_exception {
  76      /**
  77       * @param string $errorcode
  78       * @param string $debuginfo
  79       */
  80      function __construct($errorcode, $a=NULL, $debuginfo=null) {
  81          parent::__construct($errorcode, '', '', $a, $debuginfo);
  82      }
  83  }
  84  
  85  /**
  86   * Table does not exist problem exception
  87   */
  88  class ddl_table_missing_exception extends ddl_exception {
  89      /**
  90       * @param string $tablename
  91       * @param string $debuginfo
  92       */
  93      function __construct($tablename, $debuginfo=null) {
  94          parent::__construct('ddltablenotexist', $tablename, $debuginfo);
  95      }
  96  }
  97  
  98  /**
  99   * Table does not exist problem exception
 100   */
 101  class ddl_field_missing_exception extends ddl_exception {
 102      /**
 103       * @param string $fieldname
 104       * @param string $tablename
 105       * @param string $debuginfo
 106       */
 107      function __construct($fieldname, $tablename, $debuginfo=null) {
 108          $a = new stdClass();
 109          $a->fieldname = $fieldname;
 110          $a->tablename = $tablename;
 111          parent::__construct('ddlfieldnotexist', $a, $debuginfo);
 112      }
 113  }
 114  
 115  /**
 116   * Error during changing db structure
 117   */
 118  class ddl_change_structure_exception extends ddl_exception {
 119      /** @var string */
 120      public $error;
 121      public $sql;
 122      /**
 123       * @param string $error
 124       * @param string $sql
 125       */
 126      function __construct($error, $sql=null) {
 127          $this->error = $error;
 128          $this->sql   = $sql;
 129          $errorinfo   = $error."\n".$sql;
 130          parent::__construct('ddlexecuteerror', NULL, $errorinfo);
 131      }
 132  }
 133  
 134  /**
 135   * Error changing db structure, caused by some dependency found
 136   * like trying to modify one field having related indexes.
 137   */
 138  class ddl_dependency_exception extends ddl_exception {
 139  
 140      function __construct($targettype, $targetname, $offendingtype, $offendingname, $debuginfo=null) {
 141          $a = new stdClass();
 142          $a->targettype = $targettype;
 143          $a->targetname = $targetname;
 144          $a->offendingtype = $offendingtype;
 145          $a->offendingname = $offendingname;
 146  
 147          parent::__construct('ddldependencyerror', $a, $debuginfo);
 148      }
 149  }