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   * Database driver test class for testing moodle_read_slave_trait
  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__.'/test_moodle_database.php');
  31  require_once (__DIR__.'/../../moodle_read_slave_trait.php');
  32  
  33  /**
  34   * Database driver test class with moodle_read_slave_trait
  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  class read_slave_moodle_database extends test_moodle_database {
  42      use \moodle_read_slave_trait;
  43  
  44      /** @var string */
  45      protected $handle;
  46  
  47      /**
  48       * Does not connect to the database. Sets handle property to $dbhost
  49       * @param string $dbhost
  50       * @param string $dbuser
  51       * @param string $dbpass
  52       * @param string $dbname
  53       * @param mixed $prefix
  54       * @param array $dboptions
  55       * @return bool true
  56       */
  57      public function raw_connect(string $dbhost, string $dbuser, string $dbpass, string $dbname, $prefix, array $dboptions = null): bool {
  58          $dbport = isset($dboptions['dbport']) ? $dboptions['dbport'] : "";
  59          $this->handle = implode(':', [$dbhost, $dbport, $dbuser, $dbpass]);
  60          $this->prefix = $prefix;
  61  
  62          if ($dbhost == 'test_ro_fail') {
  63              throw new \dml_connection_exception($dbhost);
  64          }
  65  
  66          return true;
  67      }
  68  
  69      /**
  70       * Begin database transaction
  71       * @return void
  72       */
  73      protected function begin_transaction() {
  74      }
  75  
  76      /**
  77       * Commit database transaction
  78       * @return void
  79       */
  80      protected function commit_transaction() {
  81      }
  82  
  83      /**
  84       * Abort database transaction
  85       * @return void
  86       */
  87      protected function rollback_transaction() {
  88          $this->txnhandle = $this->handle;
  89      }
  90  
  91      /**
  92       * Query wrapper that calls query_start() and query_end()
  93       * @param string $sql
  94       * @param array|null $params
  95       * @param int $querytype
  96       * @param ?callable $callback
  97       * @return string $handle handle property
  98       */
  99      public function with_query_start_end($sql, ?array $params, $querytype, $callback = null) {
 100          $this->query_start($sql, $params, $querytype);
 101          $ret = $this->handle;
 102          if ($callback) {
 103              call_user_func($callback, $ret);
 104          }
 105          $this->query_end(null);
 106          return $ret;
 107      }
 108  
 109      /**
 110       * get_dbhwrite()
 111       * @return string $dbhwrite handle property
 112       */
 113      public function get_dbhwrite() {
 114          return $this->dbhwrite;
 115      }
 116  
 117      /**
 118       * Calls with_query_start_end()
 119       * @param string $sql
 120       * @param array $params
 121       * @return bool true
 122       * @throws Exception
 123       */
 124      public function execute($sql, array $params = null) {
 125          list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
 126          return $this->with_query_start_end($sql, $params, SQL_QUERY_UPDATE);
 127      }
 128  
 129      /**
 130       * get_records_sql() override, calls with_query_start_end()
 131       * @param string $sql the SQL select query to execute.
 132       * @param array $params array of sql parameters
 133       * @param int $limitfrom return a subset of records, starting at this point (optional).
 134       * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
 135       * @return string $handle handle property
 136       */
 137      public function get_records_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) {
 138          list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
 139          return $this->with_query_start_end($sql, $params, SQL_QUERY_SELECT);
 140      }
 141  
 142      /**
 143       * Calls with_query_start_end()
 144       * @param string $sql
 145       * @param array $params
 146       * @param int $limitfrom
 147       * @param int $limitnum
 148       * @return bool true
 149       */
 150      public function get_recordset_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) {
 151          list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
 152          return $this->with_query_start_end($sql, $params, SQL_QUERY_SELECT);
 153      }
 154  
 155      /**
 156       * Calls with_query_start_end()
 157       * @param string $table
 158       * @param array $params
 159       * @param bool $returnid
 160       * @param bool $bulk
 161       * @param bool $customsequence
 162       * @return string $handle handle property
 163       */
 164      public function insert_record_raw($table, $params, $returnid = true, $bulk = false, $customsequence = false) {
 165          $fields = implode(',', array_keys($params));
 166          $i = 1;
 167          foreach ($params as $value) {
 168              $values[] = "\$".$i++;
 169          }
 170          $values = implode(',', $values);
 171          $sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($values)";
 172          return $this->with_query_start_end($sql, $params, SQL_QUERY_INSERT);
 173      }
 174  
 175      /**
 176       * Calls with_query_start_end()
 177       * @param string $table
 178       * @param array $params
 179       * @param bool $bulk
 180       * @return string $handle handle property
 181       */
 182      public function update_record_raw($table, $params, $bulk = false) {
 183          $id = $params['id'];
 184          unset($params['id']);
 185          $i = 1;
 186          $sets = array();
 187          foreach ($params as $field => $value) {
 188              $sets[] = "$field = \$".$i++;
 189          }
 190          $params[] = $id;
 191          $sets = implode(',', $sets);
 192          $sql = "UPDATE {$this->prefix}$table SET $sets WHERE id=\$".$i;
 193          return $this->with_query_start_end($sql, $params, SQL_QUERY_UPDATE);
 194      }
 195  
 196      /**
 197       * Gets handle property
 198       * @return string $handle handle property
 199       */
 200      protected function get_db_handle() {
 201          return $this->handle;
 202      }
 203  
 204      /**
 205       * Sets handle property
 206       * @param string $dbh
 207       * @return void
 208       */
 209      protected function set_db_handle($dbh): void {
 210          $this->handle = $dbh;
 211      }
 212  
 213      /**
 214       * Add temptable
 215       * @param string $temptable
 216       * @return void
 217       */
 218      public function add_temptable($temptable) {
 219          $this->temptables->add_temptable($temptable);
 220      }
 221  
 222      /**
 223       * Remove temptable
 224       * @param string $temptable
 225       * @return void
 226       */
 227      public function delete_temptable($temptable) {
 228          $this->temptables->delete_temptable($temptable);
 229      }
 230  
 231      /**
 232       * Is session lock supported in this driver?
 233       * @return bool
 234       */
 235      public function session_lock_supported() {
 236          return true;
 237      }
 238  }