NOMDL Workshop: adding explicit type declaration for hidden form elements
[moodle.git] / lib / dml / mysqli_native_moodle_database.php
CommitLineData
49926145 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Native mysqli class representing moodle database interface.
21 *
66c0ee78 22 * @package core
f2ed3f05 23 * @subpackage dml
49926145 24 * @copyright 2008 Petr Skoda (http://skodak.org)
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
7466a42f 27
f2ed3f05
PS
28defined('MOODLE_INTERNAL') || die();
29
7466a42f 30require_once($CFG->libdir.'/dml/moodle_database.php');
31require_once($CFG->libdir.'/dml/mysqli_native_moodle_recordset.php');
3f33c9e2 32require_once($CFG->libdir.'/dml/mysqli_native_moodle_temptables.php');
7466a42f 33
34/**
35 * Native mysqli class representing moodle database interface.
7466a42f 36 */
37class mysqli_native_moodle_database extends moodle_database {
38
0487f9e2 39 protected $mysqli = null;
7466a42f 40
d5a8d9aa
PS
41 private $transactions_supported = null;
42
30d2832d 43 /**
44 * Attempt to create the database
45 * @param string $dbhost
46 * @param string $dbuser
47 * @param string $dbpass
48 * @param string $dbname
49 * @return bool success
50 * @throws dml_exception if error
51 */
3b093310 52 public function create_database($dbhost, $dbuser, $dbpass, $dbname, array $dboptions=null) {
797ffad0 53 $driverstatus = $this->driver_installed();
54
55 if ($driverstatus !== true) {
56 throw new dml_exception('dbdriverproblem', $driverstatus);
57 }
58
30d2832d 59 ob_start();
7f79aaea 60 $conn = new mysqli($dbhost, $dbuser, $dbpass); /// Connect without db
30d2832d 61 $dberr = ob_get_contents();
62 ob_end_clean();
63 $errorno = @$conn->connect_errno;
64
65 if ($errorno !== 0) {
66 throw new dml_connection_exception($dberr);
67 }
68
69 $result = $conn->query("CREATE DATABASE $dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");
70
71 $conn->close();
72
73 if (!$result) {
74 throw new dml_exception('cannotcreatedb');
75 }
76
77 return true;
78 }
79
7466a42f 80 /**
81 * Detects if all needed PHP stuff installed.
82 * Note: can be used before connect()
83 * @return mixed true if ok, string if something
84 */
85 public function driver_installed() {
86 if (!extension_loaded('mysqli')) {
87 return get_string('mysqliextensionisnotpresentinphp', 'install');
88 }
89 return true;
90 }
91
92 /**
93 * Returns database family type - describes SQL dialect
94 * Note: can be used before connect()
95 * @return string db family name (mysql, postgres, mssql, oracle, etc.)
96 */
97 public function get_dbfamily() {
98 return 'mysql';
99 }
100
101 /**
102 * Returns more specific database driver type
103 * Note: can be used before connect()
104 * @return string db type mysql, mysqli, postgres7
105 */
106 protected function get_dbtype() {
107 return 'mysqli';
108 }
109
110 /**
111 * Returns general database library name
112 * Note: can be used before connect()
eb123deb 113 * @return string db type pdo, native
7466a42f 114 */
115 protected function get_dblibrary() {
116 return 'native';
117 }
118
119 /**
120 * Returns localised database type name
121 * Note: can be used before connect()
122 * @return string
123 */
124 public function get_name() {
2aeb3bcb 125 return get_string('nativemysqli', 'install');
7466a42f 126 }
127
3b093310 128 /**
129 * Returns localised database configuration help.
130 * Note: can be used before connect()
131 * @return string
132 */
133 public function get_configuration_help() {
134 return get_string('nativemysqlihelp', 'install');
135 }
136
7466a42f 137 /**
138 * Returns localised database description
139 * Note: can be used before connect()
140 * @return string
141 */
142 public function get_configuration_hints() {
2aeb3bcb 143 return get_string('databasesettingssub_mysqli', 'install');
7466a42f 144 }
145
146 /**
147 * Connect to db
148 * Must be called before other methods.
149 * @param string $dbhost
150 * @param string $dbuser
151 * @param string $dbpass
152 * @param string $dbname
7466a42f 153 * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
154 * @param array $dboptions driver specific options
155 * @return bool success
156 */
beaa43db 157 public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
ce152606 158 $driverstatus = $this->driver_installed();
159
160 if ($driverstatus !== true) {
161 throw new dml_exception('dbdriverproblem', $driverstatus);
162 }
163
beaa43db 164 $this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
3b093310 165 unset($this->dboptions['dbsocket']);
7466a42f 166
ce152606 167 ob_start();
7466a42f 168 $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
ce152606 169 $dberr = ob_get_contents();
170 ob_end_clean();
171 $errorno = @$this->mysqli->connect_errno;
172
173 if ($errorno !== 0) {
174 throw new dml_connection_exception($dberr);
7466a42f 175 }
ce152606 176
a6283e91 177 $this->query_start("--set_charset()", null, SQL_QUERY_AUX);
7466a42f 178 $this->mysqli->set_charset('utf8');
a6283e91 179 $this->query_end(true);
180
77abbc47 181 // If available, enforce strict mode for the session. That guaranties
182 // standard behaviour under some situations, avoiding some MySQL nasty
183 // habits like truncating data or performing some transparent cast losses.
02df9cfd 184 // With strict mode enforced, Moodle DB layer will be consistently throwing
77abbc47 185 // the corresponding exceptions as expected.
186 $si = $this->get_server_info();
187 if (version_compare($si['version'], '5.0.2', '>=')) {
188 $sql = "SET SESSION sql_mode = 'STRICT_ALL_TABLES'";
189 $this->query_start($sql, null, SQL_QUERY_AUX);
190 $result = $this->mysqli->query($sql);
191 $this->query_end($result);
192 }
193
3f33c9e2 194 // Connection stabilished and configured, going to instantiate the temptables controller
195 $this->temptables = new mysqli_native_moodle_temptables($this);
196
7466a42f 197 return true;
198 }
199
200 /**
201 * Close database connection and release all resources
202 * and memory (especially circular memory references).
203 * Do NOT use connect() again, create a new instance if needed.
204 */
205 public function dispose() {
02df9cfd 206 parent::dispose(); // Call parent dispose to write/close session and other common stuff before closing connection
7466a42f 207 if ($this->mysqli) {
208 $this->mysqli->close();
209 $this->mysqli = null;
210 }
7466a42f 211 }
212
213 /**
214 * Returns database server info array
215 * @return array
216 */
217 public function get_server_info() {
218 return array('description'=>$this->mysqli->server_info, 'version'=>$this->mysqli->server_info);
219 }
220
221 /**
222 * Returns supported query parameter types
223 * @return bitmask
224 */
225 protected function allowed_param_types() {
226 return SQL_PARAMS_QM;
227 }
228
229 /**
230 * Returns last error reported by database engine.
231 */
232 public function get_last_error() {
233 return $this->mysqli->error;
234 }
235
236 /**
237 * Return tables in database WITHOUT current prefix
238 * @return array of table names in lowercase and without prefix
239 */
117679db 240 public function get_tables($usecache=true) {
241 if ($usecache and $this->tables !== null) {
242 return $this->tables;
243 }
244 $this->tables = array();
a6283e91 245 $sql = "SHOW TABLES";
246 $this->query_start($sql, null, SQL_QUERY_AUX);
247 $result = $this->mysqli->query($sql);
248 $this->query_end($result);
249 if ($result) {
7466a42f 250 while ($arr = $result->fetch_assoc()) {
251 $tablename = reset($arr);
7fdcb779 252 if ($this->prefix !== '') {
253 if (strpos($tablename, $this->prefix) !== 0) {
254 continue;
255 }
256 $tablename = substr($tablename, strlen($this->prefix));
7466a42f 257 }
117679db 258 $this->tables[$tablename] = $tablename;
7466a42f 259 }
260 $result->close();
261 }
3f33c9e2 262
263 // Add the currently available temptables
264 $this->tables = array_merge($this->tables, $this->temptables->get_temptables());
117679db 265 return $this->tables;
7466a42f 266 }
267
268 /**
269 * Return table indexes - everything lowercased
270 * @return array of arrays
271 */
272 public function get_indexes($table) {
7466a42f 273 $indexes = array();
a6283e91 274 $sql = "SHOW INDEXES FROM {$this->prefix}$table";
275 $this->query_start($sql, null, SQL_QUERY_AUX);
276 $result = $this->mysqli->query($sql);
277 $this->query_end($result);
278 if ($result) {
7466a42f 279 while ($res = $result->fetch_object()) {
280 if ($res->Key_name === 'PRIMARY') {
281 continue;
282 }
283 if (!isset($indexes[$res->Key_name])) {
284 $indexes[$res->Key_name] = array('unique'=>empty($res->Non_unique), 'columns'=>array());
285 }
286 $indexes[$res->Key_name]['columns'][$res->Seq_in_index-1] = $res->Column_name;
287 }
288 $result->close();
289 }
290 return $indexes;
291 }
292
293 /**
02df9cfd 294 * Returns detailed information about columns in table. This information is cached internally.
7466a42f 295 * @param string $table name
296 * @param bool $usecache
297 * @return array array of database_column_info objects indexed with column names
298 */
299 public function get_columns($table, $usecache=true) {
300 if ($usecache and isset($this->columns[$table])) {
301 return $this->columns[$table];
302 }
303
304 $this->columns[$table] = array();
305
a6283e91 306 $sql = "SHOW COLUMNS FROM {$this->prefix}$table";
307 $this->query_start($sql, null, SQL_QUERY_AUX);
308 $result = $this->mysqli->query($sql);
309 $this->query_end($result);
310
311 if ($result === false) {
7466a42f 312 return array();
313 }
314
a6283e91 315 while ($rawcolumn = $result->fetch_assoc()) {
316 $rawcolumn = (object)array_change_key_case($rawcolumn, CASE_LOWER);
317
7466a42f 318 $info = new object();
319 $info->name = $rawcolumn->field;
320 $matches = null;
321
322 if (preg_match('/varchar\((\d+)\)/i', $rawcolumn->type, $matches)) {
323 $info->type = 'varchar';
324 $info->meta_type = 'C';
325 $info->max_length = $matches[1];
326 $info->scale = null;
327 $info->not_null = ($rawcolumn->null === 'NO');
328 $info->default_value = $rawcolumn->default;
329 $info->has_default = is_null($info->default_value) ? false : true;
330 $info->primary_key = ($rawcolumn->key === 'PRI');
331 $info->binary = false;
332 $info->unsigned = null;
333 $info->auto_increment= false;
334 $info->unique = null;
335
336 } else if (preg_match('/([a-z]*int[a-z]*)\((\d+)\)/i', $rawcolumn->type, $matches)) {
337 $info->type = $matches[1];
338 $info->primary_key = ($rawcolumn->key === 'PRI');
339 if ($info->primary_key) {
340 $info->meta_type = 'R';
341 $info->max_length = $matches[2];
342 $info->scale = null;
343 $info->not_null = ($rawcolumn->null === 'NO');
344 $info->default_value = $rawcolumn->default;
345 $info->has_default = is_null($info->default_value) ? false : true;
346 $info->binary = false;
347 $info->unsigned = (stripos($rawcolumn->type, 'unsigned') !== false);
348 $info->auto_increment= true;
349 $info->unique = true;
350 } else {
351 $info->meta_type = 'I';
352 $info->max_length = $matches[2];
353 $info->scale = null;
354 $info->not_null = ($rawcolumn->null === 'NO');
355 $info->default_value = $rawcolumn->default;
356 $info->has_default = is_null($info->default_value) ? false : true;
357 $info->binary = false;
358 $info->unsigned = (stripos($rawcolumn->type, 'unsigned') !== false);
359 $info->auto_increment= false;
360 $info->unique = null;
361 }
362
c65845cc 363 } else if (preg_match('/(decimal|double|float)\((\d+),(\d+)\)/i', $rawcolumn->type, $matches)) {
364 $info->type = $matches[1];
7466a42f 365 $info->meta_type = 'N';
c65845cc 366 $info->max_length = $matches[2];
367 $info->scale = $matches[3];
7466a42f 368 $info->not_null = ($rawcolumn->null === 'NO');
369 $info->default_value = $rawcolumn->default;
370 $info->has_default = is_null($info->default_value) ? false : true;
371 $info->primary_key = ($rawcolumn->key === 'PRI');
372 $info->binary = false;
373 $info->unsigned = null;
374 $info->auto_increment= false;
375 $info->unique = null;
376
377 } else if (preg_match('/([a-z]*text)/i', $rawcolumn->type, $matches)) {
378 $info->type = $matches[1];
379 $info->meta_type = 'X';
380 $info->max_length = -1;
381 $info->scale = null;
382 $info->not_null = ($rawcolumn->null === 'NO');
383 $info->default_value = $rawcolumn->default;
384 $info->has_default = is_null($info->default_value) ? false : true;
385 $info->primary_key = ($rawcolumn->key === 'PRI');
386 $info->binary = false;
387 $info->unsigned = null;
388 $info->auto_increment= false;
389 $info->unique = null;
390
75dfa4a3 391 } else if (preg_match('/([a-z]*blob)/i', $rawcolumn->type, $matches)) {
392 $info->type = $matches[1];
393 $info->meta_type = 'B';
394 $info->max_length = -1;
395 $info->scale = null;
396 $info->not_null = ($rawcolumn->null === 'NO');
397 $info->default_value = $rawcolumn->default;
398 $info->has_default = is_null($info->default_value) ? false : true;
399 $info->primary_key = false;
400 $info->binary = true;
401 $info->unsigned = null;
402 $info->auto_increment= false;
403 $info->unique = null;
404
7466a42f 405 } else if (preg_match('/enum\((.*)\)/i', $rawcolumn->type, $matches)) {
406 $info->type = 'enum';
407 $info->meta_type = 'C';
408 $info->enums = array();
409 $info->max_length = 0;
410 $values = $matches[1];
411 $values = explode(',', $values);
412 $textlib = textlib_get_instance();
413 foreach ($values as $val) {
414 $val = trim($val, "'");
415 $length = $textlib->strlen($val);
416 $info->enums[] = $val;
417 $info->max_length = ($info->max_length < $length) ? $length : $info->max_length;
418 }
419 $info->scale = null;
420 $info->not_null = ($rawcolumn->null === 'NO');
421 $info->default_value = $rawcolumn->default;
422 $info->has_default = is_null($info->default_value) ? false : true;
423 $info->primary_key = ($rawcolumn->key === 'PRI');
424 $info->binary = false;
425 $info->unsigned = null;
426 $info->auto_increment= false;
427 $info->unique = null;
428 }
429
430 $this->columns[$table][$info->name] = new database_column_info($info);
431 }
432
a6283e91 433 $result->close();
434
7466a42f 435 return $this->columns[$table];
436 }
437
8fb501e9
EL
438 /**
439 * Normalise values based in RDBMS dependencies (booleans, LOBs...)
440 *
441 * @param database_column_info $column column metadata corresponding with the value we are going to normalise
442 * @param mixed $value value we are going to normalise
443 * @return mixed the normalised value
444 */
e3acc8af 445 protected function normalise_value($column, $value) {
8fb501e9
EL
446 if (is_bool($value)) { // Always, convert boolean to int
447 $value = (int)$value;
448
449 } else if ($value === '') {
450 if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
451 $value = 0; // prevent '' problems in numeric fields
452 }
453 }
454 // workaround for problem with wrong enums in mysql - TODO: Out in Moodle 2.1
455 if (!empty($column->enums)) {
456 if (is_null($value) and !$column->not_null) {
457 // ok - nulls allowed
458 } else {
459 if (!in_array((string)$value, $column->enums)) {
460 throw new dml_write_exception('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.');
461 }
462 }
463 }
464 return $value;
465 }
466
7466a42f 467 /**
468 * Is db in unicode mode?
469 * @return bool
470 */
471 public function setup_is_unicodedb() {
a6283e91 472 $sql = "SHOW LOCAL VARIABLES LIKE 'character_set_database'";
473 $this->query_start($sql, null, SQL_QUERY_AUX);
474 $result = $this->mysqli->query($sql);
475 $this->query_end($result);
476
477 if ($result) {
7466a42f 478 $result->close();
479 return true;
480 }
481 return false;
482 }
483
7466a42f 484 /**
485 * Do NOT use in code, to be used by database_manager only!
486 * @param string $sql query
22d77567 487 * @return bool true
488 * @throws dml_exception if error
7466a42f 489 */
490 public function change_database_structure($sql) {
117679db 491 $this->reset_caches();
a6283e91 492
493 $this->query_start($sql, null, SQL_QUERY_STRUCTURE);
494 $result = $this->mysqli->query($sql);
495 $this->query_end($result);
496
12e09c6b 497 return true;
7466a42f 498 }
499
500 /**
501 * Very ugly hack which emulates bound parameters in queries
502 * because prepared statements do not use query cache.
503 */
504 protected function emulate_bound_params($sql, array $params=null) {
505 if (empty($params)) {
506 return $sql;
507 }
508 /// ok, we have verified sql statement with ? and correct number of params
509 $return = strtok($sql, '?');
510 foreach ($params as $param) {
511 if (is_bool($param)) {
512 $return .= (int)$param;
513 } else if (is_null($param)) {
514 $return .= 'NULL';
c1a6529b 515 } else if (is_number($param)) { // we can not use is_numeric() because it eats leading zeros from strings like 0045646
516 $return .= $param;
517 } else if (is_float($param)) {
7466a42f 518 $return .= $param;
519 } else {
520 $param = $this->mysqli->real_escape_string($param);
521 $return .= "'$param'";
522 }
523 $return .= strtok('?');
524 }
525 return $return;
526 }
527
528 /**
529 * Execute general sql query. Should be used only when no other method suitable.
530 * Do NOT use this to make changes in db structure, use database_manager::execute_sql() instead!
531 * @param string $sql query
532 * @param array $params query parameters
22d77567 533 * @return bool true
534 * @throws dml_exception if error
7466a42f 535 */
536 public function execute($sql, array $params=null) {
537 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
538
539 if (strpos($sql, ';') !== false) {
22d77567 540 throw new coding_exception('moodle_database::execute() Multiple sql statements found or bound parameters not used properly in query!');
7466a42f 541 }
542
543 $rawsql = $this->emulate_bound_params($sql, $params);
544
a6283e91 545 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 546 $result = $this->mysqli->query($rawsql);
a6283e91 547 $this->query_end($result);
7466a42f 548
22d77567 549 if ($result === true) {
7466a42f 550 return true;
551
552 } else {
553 $result->close();
554 return true;
555 }
556 }
557
558 /**
559 * Get a number of records as a moodle_recordset using a SQL statement.
560 *
561 * Since this method is a little less readable, use of it should be restricted to
562 * code where it's possible there might be large datasets being returned. For known
563 * small datasets use get_records_sql - it leads to simpler code.
564 *
565 * The return type is as for @see function get_recordset.
566 *
567 * @param string $sql the SQL select query to execute.
568 * @param array $params array of sql parameters
569 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
570 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
22d77567 571 * @return mixed an moodle_recordset object
572 * @throws dml_exception if error
7466a42f 573 */
574 public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
088a4df8 575 $limitfrom = (int)$limitfrom;
576 $limitnum = (int)$limitnum;
3ff8bf26 577 $limitfrom = ($limitfrom < 0) ? 0 : $limitfrom;
578 $limitnum = ($limitnum < 0) ? 0 : $limitnum;
579
7466a42f 580 if ($limitfrom or $limitnum) {
7466a42f 581 if ($limitnum < 1) {
582 $limitnum = "18446744073709551615";
583 }
0487f9e2 584 $sql .= " LIMIT $limitfrom, $limitnum";
7466a42f 585 }
586
0487f9e2 587 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
588 $rawsql = $this->emulate_bound_params($sql, $params);
589
a6283e91 590 $this->query_start($sql, $params, SQL_QUERY_SELECT);
7466a42f 591 // no MYSQLI_USE_RESULT here, it would block write ops on affected tables
e0eda209 592 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
593 $this->query_end($result);
7466a42f 594
e0eda209 595 return $this->create_recordset($result);
7466a42f 596 }
597
598 protected function create_recordset($result) {
599 return new mysqli_native_moodle_recordset($result);
600 }
601
602 /**
603 * Get a number of records as an array of objects using a SQL statement.
604 *
605 * Return value as for @see function get_records.
606 *
607 * @param string $sql the SQL select query to execute. The first column of this SELECT statement
608 * must be a unique value (usually the 'id' field), as it will be used as the key of the
609 * returned array.
610 * @param array $params array of sql parameters
611 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
612 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
22d77567 613 * @return mixed an array of objects, or empty array if no records were found
614 * @throws dml_exception if error
7466a42f 615 */
616 public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
088a4df8 617 $limitfrom = (int)$limitfrom;
618 $limitnum = (int)$limitnum;
3ff8bf26 619 $limitfrom = ($limitfrom < 0) ? 0 : $limitfrom;
620 $limitnum = ($limitnum < 0) ? 0 : $limitnum;
621
7466a42f 622 if ($limitfrom or $limitnum) {
7466a42f 623 if ($limitnum < 1) {
624 $limitnum = "18446744073709551615";
625 }
0487f9e2 626 $sql .= " LIMIT $limitfrom, $limitnum";
7466a42f 627 }
628
0487f9e2 629 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
630 $rawsql = $this->emulate_bound_params($sql, $params);
631
a6283e91 632 $this->query_start($sql, $params, SQL_QUERY_SELECT);
7466a42f 633 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
a6283e91 634 $this->query_end($result);
7466a42f 635
7466a42f 636 $return = array();
a6283e91 637
7466a42f 638 while($row = $result->fetch_assoc()) {
639 $row = array_change_key_case($row, CASE_LOWER);
640 $id = reset($row);
758ba89a 641 if (isset($return[$id])) {
642 $colname = key($row);
643 debugging("Did you remember to make the first column something unique in your call to get_records? Duplicate value '$id' found in column '$colname'.", DEBUG_DEVELOPER);
644 }
7466a42f 645 $return[$id] = (object)$row;
646 }
647 $result->close();
a6283e91 648
7466a42f 649 return $return;
650 }
651
652 /**
653 * Selects records and return values (first field) as an array using a SQL statement.
654 *
655 * @param string $sql The SQL query
656 * @param array $params array of sql parameters
22d77567 657 * @return mixed array of values
658 * @throws dml_exception if error
7466a42f 659 */
660 public function get_fieldset_sql($sql, array $params=null) {
661 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
662 $rawsql = $this->emulate_bound_params($sql, $params);
663
a6283e91 664 $this->query_start($sql, $params, SQL_QUERY_SELECT);
7466a42f 665 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
a6283e91 666 $this->query_end($result);
7466a42f 667
7466a42f 668 $return = array();
a6283e91 669
7466a42f 670 while($row = $result->fetch_assoc()) {
671 $return[] = reset($row);
672 }
673 $result->close();
a6283e91 674
7466a42f 675 return $return;
676 }
677
678 /**
679 * Insert new record into database, as fast as possible, no safety checks, lobs not supported.
680 * @param string $table name
681 * @param mixed $params data record as object or array
682 * @param bool $returnit return it of inserted record
683 * @param bool $bulk true means repeated inserts expected
684 * @param bool $customsequence true if 'id' included in $params, disables $returnid
22d77567 685 * @return true or new id
686 * @throws dml_exception if error
7466a42f 687 */
688 public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) {
689 if (!is_array($params)) {
690 $params = (array)$params;
691 }
692
693 if ($customsequence) {
694 if (!isset($params['id'])) {
22d77567 695 throw new coding_exception('moodle_database::insert_record_raw() id field must be specified if custom sequences used.');
7466a42f 696 }
697 $returnid = false;
698 } else {
699 unset($params['id']);
700 }
701
702 if (empty($params)) {
22d77567 703 throw new coding_exception('moodle_database::insert_record_raw() no fields found.');
7466a42f 704 }
705
7466a42f 706 $fields = implode(',', array_keys($params));
707 $qms = array_fill(0, count($params), '?');
708 $qms = implode(',', $qms);
709
710 $sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($qms)";
76b6daf2 711
712 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
7466a42f 713 $rawsql = $this->emulate_bound_params($sql, $params);
714
a6283e91 715 $this->query_start($sql, $params, SQL_QUERY_INSERT);
7466a42f 716 $result = $this->mysqli->query($rawsql);
b4154c2d 717 $id = @$this->mysqli->insert_id; // must be called before query_end() which may insert log into db
a6283e91 718 $this->query_end($result);
7466a42f 719
b4154c2d 720 if (!$id) {
22d77567 721 throw new dml_write_exception('unknown error fetching inserted id');
7466a42f 722 }
723
724 if (!$returnid) {
725 return true;
726 } else {
727 return (int)$id;
728 }
729 }
730
731 /**
732 * Insert a record into a table and return the "id" field if required.
733 *
734 * Some conversions and safety checks are carried out. Lobs are supported.
735 * If the return ID isn't required, then this just reports success as true/false.
736 * $data is an object containing needed data
737 * @param string $table The database table to be inserted into
738 * @param object $data A data object with values for one or more fields in the record
739 * @param bool $returnid Should the id of the newly created record entry be returned? If this option is not requested then true/false is returned.
22d77567 740 * @return true or new id
741 * @throws dml_exception if error
7466a42f 742 */
743 public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {
d8fa8e40 744 $dataobject = (array)$dataobject;
7466a42f 745
746 $columns = $this->get_columns($table);
7466a42f 747 $cleaned = array();
748
749 foreach ($dataobject as $field=>$value) {
d8fa8e40
PS
750 if ($field === 'id') {
751 continue;
752 }
7466a42f 753 if (!isset($columns[$field])) {
754 continue;
755 }
756 $column = $columns[$field];
8fb501e9 757 $cleaned[$field] = $this->normalise_value($column, $value);
7466a42f 758 }
759
7466a42f 760 return $this->insert_record_raw($table, $cleaned, $returnid, $bulk);
761 }
762
763 /**
764 * Import a record into a table, id field is required.
765 * Safety checks are NOT carried out. Lobs are supported.
766 *
767 * @param string $table name of database table to be inserted into
768 * @param object $dataobject A data object with values for one or more fields in the record
22d77567 769 * @return bool true
770 * @throws dml_exception if error
7466a42f 771 */
772 public function import_record($table, $dataobject) {
d8fa8e40 773 $dataobject = (array)$dataobject;
7466a42f 774
7466a42f 775 $columns = $this->get_columns($table);
776 $cleaned = array();
777
778 foreach ($dataobject as $field=>$value) {
779 if (!isset($columns[$field])) {
780 continue;
781 }
782 $cleaned[$field] = $value;
783 }
784
785 return $this->insert_record_raw($table, $cleaned, false, true, true);
786 }
787
788 /**
789 * Update record in database, as fast as possible, no safety checks, lobs not supported.
790 * @param string $table name
791 * @param mixed $params data record as object or array
792 * @param bool true means repeated updates expected
22d77567 793 * @return bool true
794 * @throws dml_exception if error
7466a42f 795 */
796 public function update_record_raw($table, $params, $bulk=false) {
d8fa8e40
PS
797 $params = (array)$params;
798
7466a42f 799 if (!isset($params['id'])) {
22d77567 800 throw new coding_exception('moodle_database::update_record_raw() id field must be specified.');
7466a42f 801 }
802 $id = $params['id'];
803 unset($params['id']);
804
805 if (empty($params)) {
22d77567 806 throw new coding_exception('moodle_database::update_record_raw() no fields found.');
7466a42f 807 }
808
7466a42f 809 $sets = array();
810 foreach ($params as $field=>$value) {
811 $sets[] = "$field = ?";
812 }
813
814 $params[] = $id; // last ? in WHERE condition
815
816 $sets = implode(',', $sets);
817 $sql = "UPDATE {$this->prefix}$table SET $sets WHERE id=?";
76b6daf2 818
819 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
7466a42f 820 $rawsql = $this->emulate_bound_params($sql, $params);
821
a6283e91 822 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 823 $result = $this->mysqli->query($rawsql);
a6283e91 824 $this->query_end($result);
7466a42f 825
7466a42f 826 return true;
827 }
828
829 /**
830 * Update a record in a table
831 *
832 * $dataobject is an object containing needed data
833 * Relies on $dataobject having a variable "id" to
834 * specify the record to update
835 *
836 * @param string $table The database table to be checked against.
837 * @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
838 * @param bool true means repeated updates expected
22d77567 839 * @return bool true
840 * @throws dml_exception if error
7466a42f 841 */
842 public function update_record($table, $dataobject, $bulk=false) {
d8fa8e40 843 $dataobject = (array)$dataobject;
7466a42f 844
7466a42f 845 $columns = $this->get_columns($table);
846 $cleaned = array();
847
848 foreach ($dataobject as $field=>$value) {
849 if (!isset($columns[$field])) {
850 continue;
851 }
8fb501e9
EL
852 $column = $columns[$field];
853 $cleaned[$field] = $this->normalise_value($column, $value);
7466a42f 854 }
855
856 return $this->update_record_raw($table, $cleaned, $bulk);
857 }
858
859 /**
860 * Set a single field in every table record which match a particular WHERE clause.
861 *
862 * @param string $table The database table to be checked against.
863 * @param string $newfield the field to set.
864 * @param string $newvalue the value to set the field to.
865 * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
866 * @param array $params array of sql parameters
22d77567 867 * @return bool true
868 * @throws dml_exception if error
7466a42f 869 */
870 public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {
871 if ($select) {
872 $select = "WHERE $select";
873 }
874 if (is_null($params)) {
875 $params = array();
876 }
877 list($select, $params, $type) = $this->fix_sql_params($select, $params);
878
8fb501e9
EL
879 // Get column metadata
880 $columns = $this->get_columns($table);
881 $column = $columns[$newfield];
882
883 $normalised_value = $this->normalise_value($column, $newvalue);
884
885 if (is_null($normalised_value)) {
7466a42f 886 $newfield = "$newfield = NULL";
887 } else {
888 $newfield = "$newfield = ?";
8fb501e9 889 array_unshift($params, $normalised_value);
7466a42f 890 }
891 $sql = "UPDATE {$this->prefix}$table SET $newfield $select";
892 $rawsql = $this->emulate_bound_params($sql, $params);
893
a6283e91 894 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 895 $result = $this->mysqli->query($rawsql);
a6283e91 896 $this->query_end($result);
7466a42f 897
7466a42f 898 return true;
899 }
900
901 /**
902 * Delete one or more records from a table which match a particular WHERE clause.
903 *
904 * @param string $table The database table to be checked against.
905 * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria).
906 * @param array $params array of sql parameters
22d77567 907 * @return bool true
908 * @throws dml_exception if error
7466a42f 909 */
910 public function delete_records_select($table, $select, array $params=null) {
911 if ($select) {
912 $select = "WHERE $select";
913 }
914 $sql = "DELETE FROM {$this->prefix}$table $select";
915
916 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
917 $rawsql = $this->emulate_bound_params($sql, $params);
918
a6283e91 919 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 920 $result = $this->mysqli->query($rawsql);
a6283e91 921 $this->query_end($result);
7466a42f 922
7466a42f 923 return true;
924 }
925
926 public function sql_cast_char2int($fieldname, $text=false) {
927 return ' CAST(' . $fieldname . ' AS SIGNED) ';
928 }
929
930 public function sql_concat() {
931 $arr = func_get_args();
082ae821 932 $s = implode(', ', $arr);
7466a42f 933 if ($s === '') {
d5d0890c 934 return "''";
7466a42f 935 }
936 return "CONCAT($s)";
937 }
938
939 public function sql_concat_join($separator="' '", $elements=array()) {
082ae821 940 $s = implode(', ', $elements);
941
7466a42f 942 if ($s === '') {
d5d0890c 943 return "''";
7466a42f 944 }
082ae821 945 return "CONCAT_WS($separator, $s)";
7466a42f 946 }
947
7e0db2e2 948 /**
949 * Returns the SQL text to be used to calculate the length in characters of one expression.
950 * @param string fieldname or expression to calculate its length in characters.
951 * @return string the piece of SQL code to be used in the statement.
952 */
953 public function sql_length($fieldname) {
954 return ' CHAR_LENGTH(' . $fieldname . ')';
955 }
956
7466a42f 957 /**
02df9cfd 958 * Does this driver support regex syntax when searching
7466a42f 959 */
960 public function sql_regex_supported() {
961 return true;
962 }
963
964 /**
965 * Return regex positive or negative match sql
966 * @param bool $positivematch
967 * @return string or empty if not supported
968 */
969 public function sql_regex($positivematch=true) {
970 return $positivematch ? 'REGEXP' : 'NOT REGEXP';
971 }
fa76662b 972
adff97c5 973 public function sql_cast_2signed($fieldname) {
974 return ' CAST(' . $fieldname . ' AS SIGNED) ';
975 }
976
7f79aaea 977/// session locking
5e9dd017 978 public function session_lock_supported() {
979 return true;
980 }
981
3b1a9849 982 public function get_session_lock($rowid) {
5e9dd017 983 parent::get_session_lock($rowid);
3b1a9849 984 $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
985 $sql = "SELECT GET_LOCK('$fullname',120)";
986 $this->query_start($sql, null, SQL_QUERY_AUX);
987 $result = $this->mysqli->query($sql);
7f79aaea 988 $this->query_end($result);
989
990 if ($result) {
991 $arr = $result->fetch_assoc();
992 $result->close();
993
994 if (reset($arr) == 1) {
5e9dd017 995 return;
3b1a9849 996 } else {
997 // try again!
5e9dd017 998 $this->get_session_lock($rowid);
7f79aaea 999 }
1000 }
7f79aaea 1001 }
1002
3b1a9849 1003 public function release_session_lock($rowid) {
5e9dd017 1004 parent::release_session_lock($rowid);
3b1a9849 1005 $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
1006 $sql = "SELECT RELEASE_LOCK('$fullname')";
1007 $this->query_start($sql, null, SQL_QUERY_AUX);
1008 $result = $this->mysqli->query($sql);
7f79aaea 1009 $this->query_end($result);
1010
1011 if ($result) {
7f79aaea 1012 $result->close();
7f79aaea 1013 }
7f79aaea 1014 }
1015
fa76662b 1016/// transactions
1017 /**
d5a8d9aa
PS
1018 * Are transactions supported?
1019 * It is not responsible to run productions servers
1020 * on databases without transaction support ;-)
1021 *
1022 * MyISAM does not support support transactions.
fa76662b 1023 *
d5a8d9aa 1024 * @return bool
fa76662b 1025 */
d5a8d9aa
PS
1026 protected function transactions_supported() {
1027 if (!is_null($this->transactions_supported)) {
1028 return $this->transactions_supported;
1029 }
1030
dd67e10c 1031 // Only will accept transactions if using InnoDB storage engine (more engines can be added easily BDB, Falcon...)
d5a8d9aa
PS
1032 $this->transactions_supported = false;
1033
dd67e10c 1034 $sql = "SELECT @@storage_engine";
1035 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1036 $result = $this->mysqli->query($sql);
1037 $this->query_end($result);
d5a8d9aa 1038
dd67e10c 1039 if ($rec = $result->fetch_assoc()) {
d5a8d9aa
PS
1040 if (in_array($rec['@@storage_engine'], array('InnoDB'))) {
1041 $this->transactions_supported = true;
dd67e10c 1042 }
dd67e10c 1043 }
1044 $result->close();
1045
d5a8d9aa
PS
1046 return $this->transactions_supported;
1047 }
1048
1049 /**
1050 * Driver specific start of real database transaction,
1051 * this can not be used directly in code.
1052 * @return void
1053 */
1054 protected function begin_transaction() {
1055 if (!$this->transactions_supported()) {
1056 return;
a1dda107 1057 }
dd67e10c 1058
a6283e91 1059 $sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED";
1060 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1061 $result = $this->mysqli->query($sql);
1062 $this->query_end($result);
1063
dd67e10c 1064 $sql = "START TRANSACTION";
a6283e91 1065 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1066 $result = $this->mysqli->query($sql);
1067 $this->query_end($result);
fa76662b 1068 }
1069
1070 /**
d5a8d9aa
PS
1071 * Driver specific commit of real database transaction,
1072 * this can not be used directly in code.
1073 * @return void
fa76662b 1074 */
d5a8d9aa
PS
1075 protected function commit_transaction() {
1076 if (!$this->transactions_supported()) {
1077 return;
a1dda107 1078 }
d5a8d9aa 1079
a6283e91 1080 $sql = "COMMIT";
1081 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1082 $result = $this->mysqli->query($sql);
1083 $this->query_end($result);
fa76662b 1084 }
1085
1086 /**
d5a8d9aa
PS
1087 * Driver specific abort of real database transaction,
1088 * this can not be used directly in code.
1089 * @return void
fa76662b 1090 */
d5a8d9aa
PS
1091 protected function rollback_transaction() {
1092 if (!$this->transactions_supported()) {
1093 return;
a1dda107 1094 }
d5a8d9aa 1095
a6283e91 1096 $sql = "ROLLBACK";
1097 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1098 $result = $this->mysqli->query($sql);
1099 $this->query_end($result);
1100
fa76662b 1101 return true;
1102 }
7466a42f 1103}