Search moodle.org's
Developer Documentation

See Release Notes

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