Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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       * Query wrapper that calls query_start() and query_end()
  85       * @param string $sql
  86       * @param array|null $params
  87       * @param int $querytype
  88       * @param ?callable $callback
  89       * @return string $handle handle property
  90       */
  91      public function with_query_start_end($sql, ?array $params, $querytype, $callback = null) {
  92          $this->query_start($sql, $params, $querytype);
  93          $ret = $this->handle;
  94          if ($callback) {
  95              call_user_func($callback, $ret);
  96          }
  97          $this->query_end(null);
  98          return $ret;
  99      }
 100  
 101      /**
 102       * get_dbhwrite()
 103       * @return string $dbhwrite handle property
 104       */
 105      public function get_dbhwrite() {
 106          return $this->dbhwrite;
 107      }
 108  
 109      /**
 110       * Calls with_query_start_end()
 111       * @param string $sql
 112       * @param array $params
 113       * @return bool true
 114       * @throws \Exception
 115       */
 116      public function execute($sql, array $params = null) {
 117          list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
 118          return $this->with_query_start_end($sql, $params, SQL_QUERY_UPDATE);
 119      }
 120  
 121      /**
 122       * get_records_sql() override, calls with_query_start_end()
 123       * @param string $sql the SQL select query to execute.
 124       * @param array $params array of sql parameters
 125       * @param int $limitfrom return a subset of records, starting at this point (optional).
 126       * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
 127       * @return string $handle handle property
 128       */
 129      public function get_records_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) {
 130          list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
 131          return $this->with_query_start_end($sql, $params, SQL_QUERY_SELECT);
 132      }
 133  
 134      /**
 135       * Calls with_query_start_end()
 136       * @param string $sql
 137       * @param array $params
 138       * @param int $limitfrom
 139       * @param int $limitnum
 140       * @return bool true
 141       */
 142      public function get_recordset_sql($sql, array $params = null, $limitfrom = 0, $limitnum = 0) {
 143          list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
 144          return $this->with_query_start_end($sql, $params, SQL_QUERY_SELECT);
 145      }
 146  
 147      /**
 148       * Calls with_query_start_end()
 149       * @param string $table
 150       * @param array $params
 151       * @param bool $returnid
 152       * @param bool $bulk
 153       * @param bool $customsequence
 154       * @return string $handle handle property
 155       */
 156      public function insert_record_raw($table, $params, $returnid = true, $bulk = false, $customsequence = false) {
 157          $fields = implode(',', array_keys($params));
 158          $i = 1;
 159          foreach ($params as $value) {
 160              $values[] = "\$".$i++;
 161          }
 162          $values = implode(',', $values);
 163          $sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($values)";
 164          return $this->with_query_start_end($sql, $params, SQL_QUERY_INSERT);
 165      }
 166  
 167      /**
 168       * Calls with_query_start_end()
 169       * @param string $table
 170       * @param array $params
 171       * @param bool $bulk
 172       * @return string $handle handle property
 173       */
 174      public function update_record_raw($table, $params, $bulk = false) {
 175          $id = $params['id'];
 176          unset($params['id']);
 177          $i = 1;
 178          $sets = array();
 179          foreach ($params as $field => $value) {
 180              $sets[] = "$field = \$".$i++;
 181          }
 182          $params[] = $id;
 183          $sets = implode(',', $sets);
 184          $sql = "UPDATE {$this->prefix}$table SET $sets WHERE id=\$".$i;
 185          return $this->with_query_start_end($sql, $params, SQL_QUERY_UPDATE);
 186      }
 187  
 188      /**
 189       * Gets handle property
 190       * @return string $handle handle property
 191       */
 192      protected function get_db_handle() {
 193          return $this->handle;
 194      }
 195  
 196      /**
 197       * Sets handle property
 198       * @param string $dbh
 199       * @return void
 200       */
 201      protected function set_db_handle($dbh): void {
 202          $this->handle = $dbh;
 203      }
 204  
 205      /**
 206       * Add temptable
 207       * @param string $temptable
 208       * @return void
 209       */
 210      public function add_temptable($temptable) {
 211          $this->temptables->add_temptable($temptable);
 212      }
 213  
 214      /**
 215       * Remove temptable
 216       * @param string $temptable
 217       * @return void
 218       */
 219      public function delete_temptable($temptable) {
 220          $this->temptables->delete_temptable($temptable);
 221      }
 222  
 223      /**
 224       * Is session lock supported in this driver?
 225       * @return bool
 226       */
 227      public function session_lock_supported() {
 228          return true;
 229      }
 230  }