| 1441 |
ariadna |
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 replica helper that exposes selected moodle_read_replica_trait mehtods.
|
|
|
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 |
use ReflectionProperty;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Read replica helper that exposes selected moodle_read_replica_trait methods.
|
|
|
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_replica_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 |
|
|
|
55 |
$rcp = new ReflectionProperty(parent::class, 'wantreadreplica');
|
|
|
56 |
$rcp->setValue($this, true);
|
|
|
57 |
|
|
|
58 |
$this->dbhwrite = $rw;
|
|
|
59 |
$this->dbhreadonly = $ro;
|
|
|
60 |
$this->set_db_handle($this->dbhwrite);
|
|
|
61 |
|
|
|
62 |
$this->temptables = new \moodle_temptables($this);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Check db handle
|
|
|
67 |
* @param string $id
|
|
|
68 |
* @return bool
|
|
|
69 |
*/
|
|
|
70 |
public function db_handle_is($id) {
|
|
|
71 |
$dbh = $this->get_db_handle();
|
|
|
72 |
rewind($dbh);
|
|
|
73 |
return stream_get_contents($dbh) == $id;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Check db handle is rw
|
|
|
78 |
* @return bool
|
|
|
79 |
*/
|
|
|
80 |
public function db_handle_is_rw() {
|
|
|
81 |
return $this->db_handle_is('rw');
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Check db handle is ro
|
|
|
86 |
* @return bool
|
|
|
87 |
*/
|
|
|
88 |
public function db_handle_is_ro() {
|
|
|
89 |
return $this->db_handle_is('ro');
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Upgrade to public
|
|
|
94 |
* @return resource
|
|
|
95 |
*/
|
|
|
96 |
public function get_db_handle() {
|
|
|
97 |
return parent::get_db_handle();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Upgrade to public
|
|
|
102 |
* @param string $sql
|
|
|
103 |
* @param array|null $params
|
|
|
104 |
* @param int $type
|
|
|
105 |
* @param array $extrainfo
|
|
|
106 |
*/
|
|
|
107 |
public function query_start($sql, ?array $params, $type, $extrainfo = null) {
|
|
|
108 |
return parent::query_start($sql, $params, $type);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Upgrade to public
|
|
|
113 |
* @param mixed $result
|
|
|
114 |
*/
|
|
|
115 |
public function query_end($result) {
|
|
|
116 |
parent::query_end($result);
|
|
|
117 |
$this->set_db_handle($this->dbhwrite);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Upgrade to public
|
|
|
122 |
*/
|
|
|
123 |
public function dispose() {
|
|
|
124 |
}
|
|
|
125 |
}
|