Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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  /**
  18   * Abstract database driver test class providing some moodle database interface
  19   *
  20   * @package    core
  21   * @category   dml
  22   * @copyright  2018 Srdjan Janković, Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once (__DIR__.'/../../moodle_database.php');
  29  require_once (__DIR__.'/../../moodle_temptables.php');
  30  require_once (__DIR__.'/../../../ddl/database_manager.php');
  31  require_once (__DIR__.'/test_sql_generator.php');
  32  
  33  /**
  34   * Abstract database driver test class
  35   *
  36   * @package    core
  37   * @category   dml
  38   * @copyright  2018 Catalyst IT
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  abstract class test_moodle_database extends moodle_database {
  42  
  43      /** @var string */
  44      private $error;
  45  
  46      /** @var array */
  47      private $_tables = [];
  48  
  49      /**
  50       * Constructor - Instantiates the database
  51       * @param bool $external True means that an external database is used.
  52       */
  53      public function __construct($external = false) {
  54          parent::__construct($external);
  55  
  56          $this->temptables = new moodle_temptables($this);
  57      }
  58  
  59      /**
  60       * Default implementation
  61       * @return boolean true
  62       */
  63      public function driver_installed() {
  64          return true;
  65      }
  66  
  67      /**
  68       * Default implementation
  69       * @return string 'test'
  70       */
  71      public function get_dbfamily() {
  72          return 'test';
  73      }
  74  
  75      /**
  76       * Default implementation
  77       * @return string 'test'
  78       */
  79      protected function get_dbtype() {
  80          return 'test';
  81      }
  82  
  83      /**
  84       * Default implementation
  85       * @return string 'test'
  86       */
  87      protected function get_dblibrary() {
  88          return 'test';
  89      }
  90  
  91      /**
  92       * Default implementation
  93       * @return string 'test'
  94       */
  95      public function get_name() {
  96          return 'test';
  97      }
  98  
  99      /**
 100       * Default implementation
 101       * @return string
 102       */
 103      public function get_configuration_help() {
 104          return 'test database driver';
 105      }
 106  
 107      /**
 108       * Default implementation
 109       * @return array
 110       */
 111      public function get_server_info() {
 112          return ['description' => $this->name(), 'version' => '0'];
 113      }
 114  
 115      /**
 116       * Default implementation
 117       * @return int 0
 118       */
 119      protected function allowed_param_types() {
 120          return 0;
 121      }
 122  
 123      /**
 124       * Returns error property
 125       * @return string $error
 126       */
 127      public function get_last_error() {
 128          return $this->error;
 129      }
 130  
 131      /**
 132       * Sets tables property
 133       * @param array $tables
 134       * @return void
 135       */
 136      public function set_tables($tables) {
 137          $this->_tables = $tables;
 138      }
 139  
 140      /**
 141       * Returns keys of tables property
 142       * @param bool $usecache
 143       * @return array $tablenames
 144       */
 145      public function get_tables($usecache = true) {
 146          return array_keys($this->_tables);
 147      }
 148  
 149      /**
 150       * Return table indexes
 151       * @param string $table
 152       * @return array $indexes
 153       */
 154      public function get_indexes($table) {
 155          return isset($this->_tables[$table]['indexes']) ? $this->_tables[$table]['indexes'] : [];
 156      }
 157  
 158      /**
 159       * Return table columns
 160       * @param string $table
 161       * @return array database_column_info[] of database_column_info objects indexed with column names
 162       */
 163      public function fetch_columns($table) : array {
 164          return $this->_tables[$table]['columns'];
 165      }
 166  
 167      /**
 168       * Default implementation
 169       * @param StdClass $column metadata
 170       * @param mixed $value
 171       * @return mixed $value
 172       */
 173      protected function normalise_value($column, $value) {
 174          return $value;
 175      }
 176  
 177      /**
 178       * Default implementation
 179       * @param string|array $sql
 180       * @param array|null $tablenames
 181       * @return bool true
 182       */
 183      public function change_database_structure($sql, $tablenames = null) {
 184          return true;
 185      }
 186  
 187      /**
 188       * Default implementation, throws Exception
 189       * @param string $sql
 190       * @param array $params
 191       * @return bool true
 192       * @throws Exception
 193       */
 194      public function execute($sql, array $params = null) {
 195          throw new Exception("execute() not implemented");
 196      }
 197  
 198      /**
 199       * Default implementation, throws Exception
 200       * @param string $sql
 201       * @param array $params
 202       * @param int $limitfrom
 203       * @param int $limitnum
 204       * @return bool true
 205       * @throws Exception
 206       */
 207      public function get_recordset_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) {
 208          throw new Exception("get_recordset_sql() not implemented");
 209      }
 210  
 211      /**
 212       * Default implementation, throws Exception
 213       * @param string $sql
 214       * @param array $params
 215       * @param int $limitfrom
 216       * @param int $limitnum
 217       * @return bool true
 218       * @throws Exception
 219       */
 220      public function get_records_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) {
 221          throw new Exception("get_records_sql() not implemented");
 222      }
 223  
 224      /**
 225       * Default implementation, throws Exception
 226       * @param string $sql
 227       * @param array $params
 228       * @return bool true
 229       * @throws Exception
 230       */
 231      public function get_fieldset_sql($sql, array $params = null) {
 232          throw new Exception("get_fieldset_sql() not implemented");
 233      }
 234  
 235      /**
 236       * Default implementation, throws Exception
 237       * @param string $table
 238       * @param array $params
 239       * @param bool $returnid
 240       * @param bool $bulk
 241       * @param bool $customsequence
 242       * @return bool|int true or new id
 243       * @throws Exception
 244       */
 245      public function insert_record_raw($table, $params, $returnid = true, $bulk = false, $customsequence = false) {
 246          throw new Exception("insert_record_raw() not implemented");
 247      }
 248  
 249      /**
 250       * Default implementation, throws Exception
 251       * @param string $table
 252       * @param object|array $dataobject
 253       * @param bool $returnid
 254       * @param bool $bulk
 255       * @return bool|int true or new id
 256       * @throws Exception
 257       */
 258      public function insert_record($table, $dataobject, $returnid = true, $bulk = false) {
 259          return $this->insert_record_raw($table, (array)$dataobject, $returnid, $bulk);
 260      }
 261  
 262      /**
 263       * Default implementation, throws Exception
 264       * @param string $table
 265       * @param StdObject $dataobject
 266       * @return bool true
 267       * @throws Exception
 268       */
 269      public function import_record($table, $dataobject) {
 270          throw new Exception("import_record() not implemented");
 271      }
 272  
 273      /**
 274       * Default implementation, throws Exception
 275       * @param string $table
 276       * @param array $params
 277       * @param bool $bulk
 278       * @return bool true
 279       * @throws Exception
 280       */
 281      public function update_record_raw($table, $params, $bulk = false) {
 282          throw new Exception("update_record_raw() not implemented");
 283      }
 284  
 285      /**
 286       * Default implementation, throws Exception
 287       * @param string $table
 288       * @param StdObject $dataobject
 289       * @param bool $bulk
 290       * @return bool true
 291       * @throws Exception
 292       */
 293      public function update_record($table, $dataobject, $bulk = false) {
 294          throw new Exception("update_record() not implemented");
 295      }
 296  
 297      /**
 298       * Default implementation, throws Exception
 299       * @param string $table
 300       * @param string $newfield
 301       * @param string $newvalue
 302       * @param string $select
 303       * @param array $params
 304       * @return bool true
 305       * @throws Exception
 306       */
 307      public function set_field_select($table, $newfield, $newvalue, $select, array $params = null) {
 308          throw new Exception("set_field_select() not implemented");
 309      }
 310  
 311      /**
 312       * Default implementation, throws Exception
 313       * @param string $table
 314       * @param string $select
 315       * @param array $params
 316       * @return bool true
 317       * @throws Exception
 318       */
 319      public function delete_records_select($table, $select, array $params = null) {
 320          throw new Exception("delete_records_select() not implemented");
 321      }
 322  
 323      /**
 324       * Default implementation, throws Exception
 325       * @return string $sql
 326       * @throws Exception
 327       */
 328      public function sql_concat() {
 329          throw new Exception("sql_concat() not implemented");
 330      }
 331  
 332      /**
 333       * Default implementation, throws Exception
 334       * @param string $separator
 335       * @param array  $elements
 336       * @return string $sql
 337       * @throws Exception
 338       */
 339      public function sql_concat_join($separator = "' '", $elements = []) {
 340          throw new Exception("sql_concat_join() not implemented");
 341      }
 342  
 343      /**
 344       * Default implementation, throws Exception
 345       * @return void
 346       * @throws Exception
 347       */
 348      protected function begin_transaction() {
 349          throw new Exception("begin_transaction() not implemented");
 350      }
 351  
 352      /**
 353       * Default implementation, throws Exception
 354       * @return void
 355       * @throws Exception
 356       */
 357      protected function commit_transaction() {
 358          throw new Exception("commit_transaction() not implemented");
 359      }
 360  
 361      /**
 362       * Default implementation, throws Exception
 363       * @return void
 364       * @throws Exception
 365       */
 366      protected function rollback_transaction() {
 367          throw new Exception("rollback_transaction() not implemented");
 368      }
 369  
 370      /**
 371       * Returns the database manager used for db manipulation.
 372       * Used mostly in upgrade.php scripts.
 373       * @return database_manager The instance used to perform ddl operations.
 374       * @see lib/ddl/database_manager.php
 375       */
 376      public function get_manager() {
 377          if (!$this->database_manager) {
 378              $generator = new test_sql_generator($this, $this->temptables);
 379  
 380              $this->database_manager = new database_manager($this, $generator);
 381          }
 382          return $this->database_manager;
 383      }
 384  }