Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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