| 1 |
efrain |
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 |
* Abstract database driver test class providing some moodle database interface
|
|
|
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 Exception;
|
|
|
29 |
|
|
|
30 |
defined('MOODLE_INTERNAL') || die();
|
|
|
31 |
|
|
|
32 |
require_once(__DIR__.'/../../moodle_database.php');
|
|
|
33 |
require_once(__DIR__.'/../../moodle_temptables.php');
|
|
|
34 |
require_once(__DIR__.'/../../../ddl/database_manager.php');
|
|
|
35 |
require_once(__DIR__.'/test_sql_generator.php');
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Abstract database driver test class
|
|
|
39 |
*
|
|
|
40 |
* @package core
|
|
|
41 |
* @category dml
|
|
|
42 |
* @copyright 2018 Catalyst IT
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
abstract class test_moodle_database extends \moodle_database {
|
|
|
46 |
|
|
|
47 |
/** @var string */
|
|
|
48 |
private $error;
|
|
|
49 |
|
|
|
50 |
/** @var array */
|
|
|
51 |
private $_tables = [];
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Constructor - Instantiates the database
|
|
|
55 |
* @param bool $external True means that an external database is used.
|
|
|
56 |
*/
|
|
|
57 |
public function __construct($external = false) {
|
|
|
58 |
parent::__construct($external);
|
|
|
59 |
|
|
|
60 |
$this->temptables = new \moodle_temptables($this);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Default implementation
|
|
|
65 |
* @return boolean true
|
|
|
66 |
*/
|
|
|
67 |
public function driver_installed() {
|
|
|
68 |
return true;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Default implementation
|
|
|
73 |
* @return string 'test'
|
|
|
74 |
*/
|
|
|
75 |
public function get_dbfamily() {
|
|
|
76 |
return 'test';
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Default implementation
|
|
|
81 |
* @return string 'test'
|
|
|
82 |
*/
|
|
|
83 |
protected function get_dbtype() {
|
|
|
84 |
return 'test';
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Default implementation
|
|
|
89 |
* @return string 'test'
|
|
|
90 |
*/
|
|
|
91 |
protected function get_dblibrary() {
|
|
|
92 |
return 'test';
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Default implementation
|
|
|
97 |
* @return string 'test'
|
|
|
98 |
*/
|
|
|
99 |
public function get_name() {
|
|
|
100 |
return 'test';
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Default implementation
|
|
|
105 |
* @return string
|
|
|
106 |
*/
|
|
|
107 |
public function get_configuration_help() {
|
|
|
108 |
return 'test database driver';
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Default implementation
|
|
|
113 |
* @return array
|
|
|
114 |
*/
|
|
|
115 |
public function get_server_info() {
|
|
|
116 |
return ['description' => $this->name(), 'version' => '0'];
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Default implementation
|
|
|
121 |
* @return int 0
|
|
|
122 |
*/
|
|
|
123 |
protected function allowed_param_types() {
|
|
|
124 |
return 0;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Returns error property
|
|
|
129 |
* @return string $error
|
|
|
130 |
*/
|
|
|
131 |
public function get_last_error() {
|
|
|
132 |
return $this->error;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Sets tables property
|
|
|
137 |
* @param array $tables
|
|
|
138 |
* @return void
|
|
|
139 |
*/
|
|
|
140 |
public function set_tables($tables) {
|
|
|
141 |
$this->_tables = $tables;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Returns keys of tables property
|
|
|
146 |
* @param bool $usecache
|
|
|
147 |
* @return array $tablenames
|
|
|
148 |
*/
|
|
|
149 |
public function get_tables($usecache = true) {
|
|
|
150 |
return array_keys($this->_tables);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Return table indexes
|
|
|
155 |
* @param string $table
|
|
|
156 |
* @return array $indexes
|
|
|
157 |
*/
|
|
|
158 |
public function get_indexes($table) {
|
|
|
159 |
return isset($this->_tables[$table]['indexes']) ? $this->_tables[$table]['indexes'] : [];
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Return table columns
|
|
|
164 |
* @param string $table
|
|
|
165 |
* @return array database_column_info[] of database_column_info objects indexed with column names
|
|
|
166 |
*/
|
|
|
167 |
public function fetch_columns($table): array {
|
|
|
168 |
return $this->_tables[$table]['columns'];
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Default implementation
|
|
|
173 |
* @param \stdClass $column metadata
|
|
|
174 |
* @param mixed $value
|
|
|
175 |
* @return mixed $value
|
|
|
176 |
*/
|
|
|
177 |
protected function normalise_value($column, $value) {
|
|
|
178 |
return $value;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Default implementation
|
|
|
183 |
* @param string|array $sql
|
|
|
184 |
* @param array|null $tablenames
|
|
|
185 |
* @return bool true
|
|
|
186 |
*/
|
|
|
187 |
public function change_database_structure($sql, $tablenames = null) {
|
|
|
188 |
return true;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Default implementation, throws Exception
|
|
|
193 |
* @param string $sql
|
|
|
194 |
* @param array $params
|
|
|
195 |
* @return bool true
|
|
|
196 |
* @throws Exception
|
|
|
197 |
*/
|
| 1441 |
ariadna |
198 |
public function execute($sql, ?array $params = null) {
|
| 1 |
efrain |
199 |
throw new Exception("execute() not implemented");
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Default implementation, throws Exception
|
|
|
204 |
* @param string $sql
|
|
|
205 |
* @param array $params
|
|
|
206 |
* @param int $limitfrom
|
|
|
207 |
* @param int $limitnum
|
|
|
208 |
* @return bool true
|
|
|
209 |
* @throws Exception
|
|
|
210 |
*/
|
| 1441 |
ariadna |
211 |
public function get_recordset_sql($sql, ?array $params = null, $limitfrom = 0, $limitnum = 0) {
|
| 1 |
efrain |
212 |
throw new Exception("get_recordset_sql() not implemented");
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* Default implementation, throws Exception
|
|
|
217 |
* @param string $sql
|
|
|
218 |
* @param array $params
|
|
|
219 |
* @param int $limitfrom
|
|
|
220 |
* @param int $limitnum
|
|
|
221 |
* @return bool true
|
|
|
222 |
* @throws Exception
|
|
|
223 |
*/
|
| 1441 |
ariadna |
224 |
public function get_records_sql($sql, ?array $params = null, $limitfrom = 0, $limitnum = 0) {
|
| 1 |
efrain |
225 |
throw new Exception("get_records_sql() not implemented");
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* Default implementation, throws Exception
|
|
|
230 |
* @param string $sql
|
|
|
231 |
* @param array $params
|
|
|
232 |
* @return bool true
|
|
|
233 |
* @throws Exception
|
|
|
234 |
*/
|
| 1441 |
ariadna |
235 |
public function get_fieldset_sql($sql, ?array $params = null) {
|
| 1 |
efrain |
236 |
throw new Exception("get_fieldset_sql() not implemented");
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Default implementation, throws Exception
|
|
|
241 |
* @param string $table
|
|
|
242 |
* @param array $params
|
|
|
243 |
* @param bool $returnid
|
|
|
244 |
* @param bool $bulk
|
|
|
245 |
* @param bool $customsequence
|
|
|
246 |
* @return bool|int true or new id
|
|
|
247 |
* @throws Exception
|
|
|
248 |
*/
|
|
|
249 |
public function insert_record_raw($table, $params, $returnid = true, $bulk = false, $customsequence = false) {
|
|
|
250 |
throw new Exception("insert_record_raw() not implemented");
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* Default implementation, throws Exception
|
|
|
255 |
* @param string $table
|
|
|
256 |
* @param object|array $dataobject
|
|
|
257 |
* @param bool $returnid
|
|
|
258 |
* @param bool $bulk
|
|
|
259 |
* @return bool|int true or new id
|
|
|
260 |
* @throws Exception
|
|
|
261 |
*/
|
|
|
262 |
public function insert_record($table, $dataobject, $returnid = true, $bulk = false) {
|
|
|
263 |
return $this->insert_record_raw($table, (array)$dataobject, $returnid, $bulk);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Default implementation, throws Exception
|
|
|
268 |
* @param string $table
|
|
|
269 |
* @param StdObject $dataobject
|
|
|
270 |
* @return bool true
|
|
|
271 |
* @throws Exception
|
|
|
272 |
*/
|
|
|
273 |
public function import_record($table, $dataobject) {
|
|
|
274 |
throw new Exception("import_record() not implemented");
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Default implementation, throws Exception
|
|
|
279 |
* @param string $table
|
|
|
280 |
* @param array $params
|
|
|
281 |
* @param bool $bulk
|
|
|
282 |
* @return bool true
|
|
|
283 |
* @throws Exception
|
|
|
284 |
*/
|
|
|
285 |
public function update_record_raw($table, $params, $bulk = false) {
|
|
|
286 |
throw new Exception("update_record_raw() not implemented");
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* Default implementation, throws Exception
|
|
|
291 |
* @param string $table
|
|
|
292 |
* @param StdObject $dataobject
|
|
|
293 |
* @param bool $bulk
|
|
|
294 |
* @return bool true
|
|
|
295 |
* @throws Exception
|
|
|
296 |
*/
|
|
|
297 |
public function update_record($table, $dataobject, $bulk = false) {
|
|
|
298 |
throw new Exception("update_record() not implemented");
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* Default implementation, throws Exception
|
|
|
303 |
* @param string $table
|
|
|
304 |
* @param string $newfield
|
|
|
305 |
* @param string $newvalue
|
|
|
306 |
* @param string $select
|
|
|
307 |
* @param array $params
|
|
|
308 |
* @return bool true
|
|
|
309 |
* @throws Exception
|
|
|
310 |
*/
|
| 1441 |
ariadna |
311 |
public function set_field_select($table, $newfield, $newvalue, $select, ?array $params = null) {
|
| 1 |
efrain |
312 |
throw new Exception("set_field_select() not implemented");
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
/**
|
|
|
316 |
* Default implementation, throws Exception
|
|
|
317 |
* @param string $table
|
|
|
318 |
* @param string $select
|
|
|
319 |
* @param array $params
|
|
|
320 |
* @return bool true
|
|
|
321 |
* @throws Exception
|
|
|
322 |
*/
|
| 1441 |
ariadna |
323 |
public function delete_records_select($table, $select, ?array $params = null) {
|
| 1 |
efrain |
324 |
throw new Exception("delete_records_select() not implemented");
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
/**
|
|
|
328 |
* Default implementation, throws Exception
|
|
|
329 |
* @return string $arr,...
|
|
|
330 |
* @throws Exception
|
|
|
331 |
*/
|
|
|
332 |
public function sql_concat(...$arr) {
|
|
|
333 |
throw new Exception("sql_concat() not implemented");
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Default implementation, throws Exception
|
|
|
338 |
* @param string $separator
|
|
|
339 |
* @param array $elements
|
|
|
340 |
* @return string $sql
|
|
|
341 |
* @throws Exception
|
|
|
342 |
*/
|
|
|
343 |
public function sql_concat_join($separator = "' '", $elements = []) {
|
|
|
344 |
throw new Exception("sql_concat_join() not implemented");
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
/**
|
|
|
348 |
* Default implementation, throws Exception
|
|
|
349 |
*
|
|
|
350 |
* @param string $field
|
|
|
351 |
* @param string $separator
|
|
|
352 |
* @param string $sort
|
|
|
353 |
* @return string
|
|
|
354 |
* @throws Exception
|
|
|
355 |
*/
|
|
|
356 |
public function sql_group_concat(string $field, string $separator = ', ', string $sort = ''): string {
|
|
|
357 |
throw new Exception('sql_group_concat() not implemented');
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
/**
|
|
|
361 |
* Default implementation, throws Exception
|
|
|
362 |
* @return void
|
|
|
363 |
* @throws Exception
|
|
|
364 |
*/
|
|
|
365 |
protected function begin_transaction() {
|
|
|
366 |
throw new Exception("begin_transaction() not implemented");
|
|
|
367 |
}
|
|
|
368 |
|
|
|
369 |
/**
|
|
|
370 |
* Default implementation, throws Exception
|
|
|
371 |
* @return void
|
|
|
372 |
* @throws Exception
|
|
|
373 |
*/
|
|
|
374 |
protected function commit_transaction() {
|
|
|
375 |
throw new Exception("commit_transaction() not implemented");
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
/**
|
|
|
379 |
* Default implementation, throws Exception
|
|
|
380 |
* @return void
|
|
|
381 |
* @throws Exception
|
|
|
382 |
*/
|
|
|
383 |
protected function rollback_transaction() {
|
|
|
384 |
throw new Exception("rollback_transaction() not implemented");
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
/**
|
|
|
388 |
* Returns the database manager used for db manipulation.
|
|
|
389 |
* Used mostly in upgrade.php scripts.
|
|
|
390 |
* @return database_manager The instance used to perform ddl operations.
|
|
|
391 |
* @see lib/ddl/database_manager.php
|
|
|
392 |
*/
|
|
|
393 |
public function get_manager() {
|
|
|
394 |
if (!$this->database_manager) {
|
|
|
395 |
$generator = new test_sql_generator($this, $this->temptables);
|
|
|
396 |
|
|
|
397 |
$this->database_manager = new database_manager($this, $generator);
|
|
|
398 |
}
|
|
|
399 |
return $this->database_manager;
|
|
|
400 |
}
|
|
|
401 |
}
|