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