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.

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