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   * Read slave helper that exposes selected moodle_read_slave_trait metods
  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  /**
  29   * Read slave helper that exposes selected moodle_read_slave_trait metods
  30   *
  31   * @package    core
  32   * @category   dml
  33   * @copyright  2018 Catalyst IT
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  trait test_moodle_read_slave_trait {
  37      /**
  38       * Constructs a mock db driver
  39       *
  40       * @param bool $external
  41       */
  42      public function __construct($external = false) {
  43          parent::__construct($external);
  44  
  45          $rw = fopen("php://memory", 'r+');
  46          fputs($rw, 'rw');
  47  
  48          $ro = fopen("php://memory", 'r+');
  49          fputs($ro, 'ro');
  50  
  51          $this->prefix = 'test_'; // Default, not to leave empty.
  52          $this->wantreadslave = true;
  53          $this->dbhwrite = $rw;
  54          $this->dbhreadonly = $ro;
  55          $this->set_db_handle($this->dbhwrite);
  56  
  57          $this->temptables = new \moodle_temptables($this);
  58      }
  59  
  60      /**
  61       * Check db handle
  62       * @param string $id
  63       * @return bool
  64       */
  65      public function db_handle_is($id) {
  66          $dbh = $this->get_db_handle();
  67          rewind($dbh);
  68          return stream_get_contents($dbh) == $id;
  69      }
  70  
  71      /**
  72       * Check db handle is rw
  73       * @return bool
  74       */
  75      public function db_handle_is_rw() {
  76          return $this->db_handle_is('rw');
  77      }
  78  
  79      /**
  80       * Check db handle is ro
  81       * @return bool
  82       */
  83      public function db_handle_is_ro() {
  84          return $this->db_handle_is('ro');
  85      }
  86  
  87      /**
  88       * Upgrade to public
  89       * @return resource
  90       */
  91      public function get_db_handle() {
  92          return parent::get_db_handle();
  93      }
  94  
  95      /**
  96       * Upgrade to public
  97       * @param string $sql
  98       * @param array|null $params
  99       * @param int $type
 100       * @param array $extrainfo
 101       */
 102      public function query_start($sql, ?array $params, $type, $extrainfo = null) {
 103          return parent::query_start($sql, $params, $type);
 104      }
 105  
 106      /**
 107       * Upgrade to public
 108       * @param mixed $result
 109       */
 110      public function query_end($result) {
 111          parent::query_end($result);
 112          $this->set_db_handle($this->dbhwrite);
 113      }
 114  
 115      /**
 116       * Upgrade to public
 117       */
 118      public function dispose() {
 119      }
 120  }