MDL-29295 do not use strtok in dml and ddl layers
[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
9e9543e5
PS
59 if (!empty($dboptions['dbsocket'])
60 and (strpos($dboptions['dbsocket'], '/') !== false or strpos($dboptions['dbsocket'], '\\') !== false)) {
61 $dbsocket = $dboptions['dbsocket'];
62 } else {
63 $dbsocket = ini_get('mysqli.default_socket');
64 }
65 if (empty($dboptions['dbport'])) {
77f9f23b 66 $dbport = (int)ini_get('mysqli.default_port');
bac14015 67 } else {
9e9543e5 68 $dbport = (int)$dboptions['dbport'];
bac14015 69 }
77f9f23b
PS
70 // verify ini.get does not return nonsense
71 if (empty($dbport)) {
72 $dbport = 3306;
73 }
30d2832d 74 ob_start();
9e9543e5 75 $conn = new mysqli($dbhost, $dbuser, $dbpass, '', $dbport, $dbsocket); /// Connect without db
30d2832d 76 $dberr = ob_get_contents();
77 ob_end_clean();
78 $errorno = @$conn->connect_errno;
79
80 if ($errorno !== 0) {
81 throw new dml_connection_exception($dberr);
82 }
83
84 $result = $conn->query("CREATE DATABASE $dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");
85
86 $conn->close();
87
88 if (!$result) {
89 throw new dml_exception('cannotcreatedb');
90 }
91
92 return true;
93 }
94
7466a42f 95 /**
96 * Detects if all needed PHP stuff installed.
97 * Note: can be used before connect()
98 * @return mixed true if ok, string if something
99 */
100 public function driver_installed() {
101 if (!extension_loaded('mysqli')) {
102 return get_string('mysqliextensionisnotpresentinphp', 'install');
103 }
104 return true;
105 }
106
107 /**
108 * Returns database family type - describes SQL dialect
109 * Note: can be used before connect()
110 * @return string db family name (mysql, postgres, mssql, oracle, etc.)
111 */
112 public function get_dbfamily() {
113 return 'mysql';
114 }
115
116 /**
117 * Returns more specific database driver type
118 * Note: can be used before connect()
4a6c2ab8 119 * @return string db type mysqli, pgsql, oci, mssql, sqlsrv
7466a42f 120 */
121 protected function get_dbtype() {
122 return 'mysqli';
123 }
124
125 /**
126 * Returns general database library name
127 * Note: can be used before connect()
eb123deb 128 * @return string db type pdo, native
7466a42f 129 */
130 protected function get_dblibrary() {
131 return 'native';
132 }
133
d35ece6c
PS
134 /**
135 * Returns the current MySQL db engine.
136 *
137 * This is an ugly workaround for MySQL default engine problems,
138 * Moodle is designed to work best on ACID compliant databases
139 * with full transaction support. Do not use MyISAM.
140 *
141 * @return string or null MySQL engine name
142 */
143 public function get_dbengine() {
144 if (isset($this->dboptions['dbengine'])) {
145 return $this->dboptions['dbengine'];
146 }
147
148 $engine = null;
149
150 if (!$this->external) {
151 // look for current engine of our config table (the first table that gets created),
152 // so that we create all tables with the same engine
153 $sql = "SELECT engine FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = DATABASE() AND table_name = '{$this->prefix}config'";
154 $this->query_start($sql, NULL, SQL_QUERY_AUX);
155 $result = $this->mysqli->query($sql);
156 $this->query_end($result);
157 if ($rec = $result->fetch_assoc()) {
158 $engine = $rec['engine'];
159 }
160 $result->close();
161 }
162
163 if ($engine) {
164 return $engine;
165 }
166
167 // get the default database engine
168 $sql = "SELECT @@storage_engine";
169 $this->query_start($sql, NULL, SQL_QUERY_AUX);
170 $result = $this->mysqli->query($sql);
171 $this->query_end($result);
172 if ($rec = $result->fetch_assoc()) {
173 $engine = $rec['@@storage_engine'];
174 }
175 $result->close();
176
177 if (!$this->external and $engine === 'MyISAM') {
178 // we really do not want MyISAM for Moodle, InnoDB or XtraDB is a reasonable defaults if supported
179 $sql = "SHOW STORAGE ENGINES";
180 $this->query_start($sql, NULL, SQL_QUERY_AUX);
181 $result = $this->mysqli->query($sql);
182 $this->query_end($result);
ed22700d 183 $engines = array();
d35ece6c 184 while ($res = $result->fetch_assoc()) {
ed22700d
PS
185 if ($res['Support'] === 'YES' or $res['Support'] === 'DEFAULT') {
186 $engines[$res['Engine']] = true;
d35ece6c
PS
187 }
188 }
189 $result->close();
ed22700d
PS
190 if (isset($engines['InnoDB'])) {
191 $engine = 'InnoDB';
192 }
193 if (isset($engines['XtraDB'])) {
194 $engine = 'XtraDB';
195 }
d35ece6c
PS
196 }
197
198 return $engine;
199 }
200
7466a42f 201 /**
202 * Returns localised database type name
203 * Note: can be used before connect()
204 * @return string
205 */
206 public function get_name() {
2aeb3bcb 207 return get_string('nativemysqli', 'install');
7466a42f 208 }
209
3b093310 210 /**
211 * Returns localised database configuration help.
212 * Note: can be used before connect()
213 * @return string
214 */
215 public function get_configuration_help() {
216 return get_string('nativemysqlihelp', 'install');
217 }
218
7466a42f 219 /**
220 * Returns localised database description
221 * Note: can be used before connect()
222 * @return string
223 */
224 public function get_configuration_hints() {
2aeb3bcb 225 return get_string('databasesettingssub_mysqli', 'install');
7466a42f 226 }
227
d35ece6c
PS
228 /**
229 * Diagnose database and tables, this function is used
230 * to verify database and driver settings, db engine types, etc.
231 *
232 * @return string null means everything ok, string means problem found.
233 */
234 public function diagnose() {
235 $sloppymyisamfound = false;
236 $prefix = str_replace('_', '\\_', $this->prefix);
237 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'";
238 $this->query_start($sql, null, SQL_QUERY_AUX);
239 $result = $this->mysqli->query($sql);
240 $this->query_end($result);
241 if ($result) {
242 while ($arr = $result->fetch_assoc()) {
243 if ($arr['Engine'] === 'MyISAM') {
244 $sloppymyisamfound = true;
245 break;
246 }
247 }
248 $result->close();
249 }
250
251 if ($sloppymyisamfound) {
252 return get_string('myisamproblem', 'error');
253 } else {
254 return null;
255 }
256 }
257
7466a42f 258 /**
259 * Connect to db
260 * Must be called before other methods.
261 * @param string $dbhost
262 * @param string $dbuser
263 * @param string $dbpass
264 * @param string $dbname
7466a42f 265 * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
266 * @param array $dboptions driver specific options
267 * @return bool success
268 */
beaa43db 269 public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
ce152606 270 $driverstatus = $this->driver_installed();
271
272 if ($driverstatus !== true) {
273 throw new dml_exception('dbdriverproblem', $driverstatus);
274 }
275
beaa43db 276 $this->store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions);
7466a42f 277
79ac5a8c
PS
278 // dbsocket is used ONLY if host is NULL or 'localhost',
279 // you can not disable it because it is always tried if dbhost is 'localhost'
8c4b142c
PS
280 if (!empty($this->dboptions['dbsocket'])
281 and (strpos($this->dboptions['dbsocket'], '/') !== false or strpos($this->dboptions['dbsocket'], '\\') !== false)) {
79ac5a8c
PS
282 $dbsocket = $this->dboptions['dbsocket'];
283 } else {
284 $dbsocket = ini_get('mysqli.default_socket');
285 }
feaddca9 286 if (empty($this->dboptions['dbport'])) {
77f9f23b 287 $dbport = (int)ini_get('mysqli.default_port');
feaddca9
AB
288 } else {
289 $dbport = (int)$this->dboptions['dbport'];
290 }
77f9f23b
PS
291 // verify ini.get does not return nonsense
292 if (empty($dbport)) {
293 $dbport = 3306;
294 }
ce152606 295 ob_start();
79ac5a8c 296 $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbsocket);
ce152606 297 $dberr = ob_get_contents();
298 ob_end_clean();
299 $errorno = @$this->mysqli->connect_errno;
300
301 if ($errorno !== 0) {
302 throw new dml_connection_exception($dberr);
7466a42f 303 }
ce152606 304
a6283e91 305 $this->query_start("--set_charset()", null, SQL_QUERY_AUX);
7466a42f 306 $this->mysqli->set_charset('utf8');
a6283e91 307 $this->query_end(true);
308
77abbc47 309 // If available, enforce strict mode for the session. That guaranties
310 // standard behaviour under some situations, avoiding some MySQL nasty
311 // habits like truncating data or performing some transparent cast losses.
02df9cfd 312 // With strict mode enforced, Moodle DB layer will be consistently throwing
77abbc47 313 // the corresponding exceptions as expected.
314 $si = $this->get_server_info();
315 if (version_compare($si['version'], '5.0.2', '>=')) {
316 $sql = "SET SESSION sql_mode = 'STRICT_ALL_TABLES'";
317 $this->query_start($sql, null, SQL_QUERY_AUX);
318 $result = $this->mysqli->query($sql);
319 $this->query_end($result);
320 }
321
3f33c9e2 322 // Connection stabilished and configured, going to instantiate the temptables controller
323 $this->temptables = new mysqli_native_moodle_temptables($this);
324
7466a42f 325 return true;
326 }
327
328 /**
329 * Close database connection and release all resources
330 * and memory (especially circular memory references).
331 * Do NOT use connect() again, create a new instance if needed.
332 */
333 public function dispose() {
02df9cfd 334 parent::dispose(); // Call parent dispose to write/close session and other common stuff before closing connection
7466a42f 335 if ($this->mysqli) {
336 $this->mysqli->close();
337 $this->mysqli = null;
338 }
7466a42f 339 }
340
341 /**
342 * Returns database server info array
343 * @return array
344 */
345 public function get_server_info() {
346 return array('description'=>$this->mysqli->server_info, 'version'=>$this->mysqli->server_info);
347 }
348
349 /**
350 * Returns supported query parameter types
9331d879 351 * @return int bitmask
7466a42f 352 */
353 protected function allowed_param_types() {
354 return SQL_PARAMS_QM;
355 }
356
357 /**
358 * Returns last error reported by database engine.
9331d879 359 * @return string error message
7466a42f 360 */
361 public function get_last_error() {
362 return $this->mysqli->error;
363 }
364
365 /**
366 * Return tables in database WITHOUT current prefix
367 * @return array of table names in lowercase and without prefix
368 */
117679db 369 public function get_tables($usecache=true) {
370 if ($usecache and $this->tables !== null) {
371 return $this->tables;
372 }
373 $this->tables = array();
a6283e91 374 $sql = "SHOW TABLES";
375 $this->query_start($sql, null, SQL_QUERY_AUX);
376 $result = $this->mysqli->query($sql);
377 $this->query_end($result);
378 if ($result) {
7466a42f 379 while ($arr = $result->fetch_assoc()) {
380 $tablename = reset($arr);
7fdcb779 381 if ($this->prefix !== '') {
382 if (strpos($tablename, $this->prefix) !== 0) {
383 continue;
384 }
385 $tablename = substr($tablename, strlen($this->prefix));
7466a42f 386 }
117679db 387 $this->tables[$tablename] = $tablename;
7466a42f 388 }
389 $result->close();
390 }
3f33c9e2 391
392 // Add the currently available temptables
393 $this->tables = array_merge($this->tables, $this->temptables->get_temptables());
117679db 394 return $this->tables;
7466a42f 395 }
396
397 /**
398 * Return table indexes - everything lowercased
399 * @return array of arrays
400 */
401 public function get_indexes($table) {
7466a42f 402 $indexes = array();
a6283e91 403 $sql = "SHOW INDEXES FROM {$this->prefix}$table";
404 $this->query_start($sql, null, SQL_QUERY_AUX);
405 $result = $this->mysqli->query($sql);
406 $this->query_end($result);
407 if ($result) {
7466a42f 408 while ($res = $result->fetch_object()) {
409 if ($res->Key_name === 'PRIMARY') {
410 continue;
411 }
412 if (!isset($indexes[$res->Key_name])) {
413 $indexes[$res->Key_name] = array('unique'=>empty($res->Non_unique), 'columns'=>array());
414 }
415 $indexes[$res->Key_name]['columns'][$res->Seq_in_index-1] = $res->Column_name;
416 }
417 $result->close();
418 }
419 return $indexes;
420 }
421
422 /**
02df9cfd 423 * Returns detailed information about columns in table. This information is cached internally.
7466a42f 424 * @param string $table name
425 * @param bool $usecache
426 * @return array array of database_column_info objects indexed with column names
427 */
428 public function get_columns($table, $usecache=true) {
429 if ($usecache and isset($this->columns[$table])) {
430 return $this->columns[$table];
431 }
432
433 $this->columns[$table] = array();
434
a6283e91 435 $sql = "SHOW COLUMNS FROM {$this->prefix}$table";
436 $this->query_start($sql, null, SQL_QUERY_AUX);
437 $result = $this->mysqli->query($sql);
438 $this->query_end($result);
439
440 if ($result === false) {
7466a42f 441 return array();
442 }
443
a6283e91 444 while ($rawcolumn = $result->fetch_assoc()) {
445 $rawcolumn = (object)array_change_key_case($rawcolumn, CASE_LOWER);
446
ac6f1a82 447 $info = new stdClass();
7466a42f 448 $info->name = $rawcolumn->field;
449 $matches = null;
450
451 if (preg_match('/varchar\((\d+)\)/i', $rawcolumn->type, $matches)) {
452 $info->type = 'varchar';
453 $info->meta_type = 'C';
454 $info->max_length = $matches[1];
455 $info->scale = null;
456 $info->not_null = ($rawcolumn->null === 'NO');
457 $info->default_value = $rawcolumn->default;
458 $info->has_default = is_null($info->default_value) ? false : true;
459 $info->primary_key = ($rawcolumn->key === 'PRI');
460 $info->binary = false;
461 $info->unsigned = null;
462 $info->auto_increment= false;
463 $info->unique = null;
464
465 } else if (preg_match('/([a-z]*int[a-z]*)\((\d+)\)/i', $rawcolumn->type, $matches)) {
466 $info->type = $matches[1];
467 $info->primary_key = ($rawcolumn->key === 'PRI');
468 if ($info->primary_key) {
469 $info->meta_type = 'R';
470 $info->max_length = $matches[2];
471 $info->scale = null;
472 $info->not_null = ($rawcolumn->null === 'NO');
473 $info->default_value = $rawcolumn->default;
474 $info->has_default = is_null($info->default_value) ? false : true;
475 $info->binary = false;
476 $info->unsigned = (stripos($rawcolumn->type, 'unsigned') !== false);
477 $info->auto_increment= true;
478 $info->unique = true;
479 } else {
480 $info->meta_type = 'I';
481 $info->max_length = $matches[2];
482 $info->scale = null;
483 $info->not_null = ($rawcolumn->null === 'NO');
484 $info->default_value = $rawcolumn->default;
485 $info->has_default = is_null($info->default_value) ? false : true;
486 $info->binary = false;
487 $info->unsigned = (stripos($rawcolumn->type, 'unsigned') !== false);
488 $info->auto_increment= false;
489 $info->unique = null;
490 }
491
c65845cc 492 } else if (preg_match('/(decimal|double|float)\((\d+),(\d+)\)/i', $rawcolumn->type, $matches)) {
493 $info->type = $matches[1];
7466a42f 494 $info->meta_type = 'N';
c65845cc 495 $info->max_length = $matches[2];
496 $info->scale = $matches[3];
7466a42f 497 $info->not_null = ($rawcolumn->null === 'NO');
498 $info->default_value = $rawcolumn->default;
499 $info->has_default = is_null($info->default_value) ? false : true;
500 $info->primary_key = ($rawcolumn->key === 'PRI');
501 $info->binary = false;
d466e23c 502 $info->unsigned = (stripos($rawcolumn->type, 'unsigned') !== false);
7466a42f 503 $info->auto_increment= false;
504 $info->unique = null;
505
506 } else if (preg_match('/([a-z]*text)/i', $rawcolumn->type, $matches)) {
507 $info->type = $matches[1];
508 $info->meta_type = 'X';
509 $info->max_length = -1;
510 $info->scale = null;
511 $info->not_null = ($rawcolumn->null === 'NO');
512 $info->default_value = $rawcolumn->default;
513 $info->has_default = is_null($info->default_value) ? false : true;
514 $info->primary_key = ($rawcolumn->key === 'PRI');
515 $info->binary = false;
516 $info->unsigned = null;
517 $info->auto_increment= false;
518 $info->unique = null;
519
75dfa4a3 520 } else if (preg_match('/([a-z]*blob)/i', $rawcolumn->type, $matches)) {
521 $info->type = $matches[1];
522 $info->meta_type = 'B';
523 $info->max_length = -1;
524 $info->scale = null;
525 $info->not_null = ($rawcolumn->null === 'NO');
526 $info->default_value = $rawcolumn->default;
527 $info->has_default = is_null($info->default_value) ? false : true;
528 $info->primary_key = false;
529 $info->binary = true;
530 $info->unsigned = null;
531 $info->auto_increment= false;
532 $info->unique = null;
533
7466a42f 534 } else if (preg_match('/enum\((.*)\)/i', $rawcolumn->type, $matches)) {
535 $info->type = 'enum';
536 $info->meta_type = 'C';
537 $info->enums = array();
538 $info->max_length = 0;
539 $values = $matches[1];
540 $values = explode(',', $values);
541 $textlib = textlib_get_instance();
542 foreach ($values as $val) {
543 $val = trim($val, "'");
544 $length = $textlib->strlen($val);
545 $info->enums[] = $val;
546 $info->max_length = ($info->max_length < $length) ? $length : $info->max_length;
547 }
548 $info->scale = null;
549 $info->not_null = ($rawcolumn->null === 'NO');
550 $info->default_value = $rawcolumn->default;
551 $info->has_default = is_null($info->default_value) ? false : true;
552 $info->primary_key = ($rawcolumn->key === 'PRI');
553 $info->binary = false;
554 $info->unsigned = null;
555 $info->auto_increment= false;
556 $info->unique = null;
557 }
558
559 $this->columns[$table][$info->name] = new database_column_info($info);
560 }
561
a6283e91 562 $result->close();
563
7466a42f 564 return $this->columns[$table];
565 }
566
8fb501e9
EL
567 /**
568 * Normalise values based in RDBMS dependencies (booleans, LOBs...)
569 *
570 * @param database_column_info $column column metadata corresponding with the value we are going to normalise
571 * @param mixed $value value we are going to normalise
572 * @return mixed the normalised value
573 */
e3acc8af 574 protected function normalise_value($column, $value) {
8fb501e9
EL
575 if (is_bool($value)) { // Always, convert boolean to int
576 $value = (int)$value;
577
578 } else if ($value === '') {
579 if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
580 $value = 0; // prevent '' problems in numeric fields
581 }
7aa9e45f
EL
582 // Any float value being stored in varchar or text field is converted to string to avoid
583 // any implicit conversion by MySQL
584 } else if (is_float($value) and ($column->meta_type == 'C' or $column->meta_type == 'X')) {
585 $value = "$value";
8fb501e9
EL
586 }
587 // workaround for problem with wrong enums in mysql - TODO: Out in Moodle 2.1
588 if (!empty($column->enums)) {
589 if (is_null($value) and !$column->not_null) {
590 // ok - nulls allowed
591 } else {
592 if (!in_array((string)$value, $column->enums)) {
593 throw new dml_write_exception('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.');
594 }
595 }
596 }
597 return $value;
598 }
599
7466a42f 600 /**
601 * Is db in unicode mode?
602 * @return bool
603 */
604 public function setup_is_unicodedb() {
a6283e91 605 $sql = "SHOW LOCAL VARIABLES LIKE 'character_set_database'";
606 $this->query_start($sql, null, SQL_QUERY_AUX);
607 $result = $this->mysqli->query($sql);
608 $this->query_end($result);
609
cd4f1859 610 $return = false;
a6283e91 611 if ($result) {
cd4f1859
PS
612 while($row = $result->fetch_assoc()) {
613 if (isset($row['Value'])) {
614 $return = (strtoupper($row['Value']) === 'UTF8' or strtoupper($row['Value']) === 'UTF-8');
615 }
616 break;
617 }
7466a42f 618 $result->close();
7466a42f 619 }
cd4f1859
PS
620
621 if (!$return) {
622 return false;
623 }
624
625 $sql = "SHOW LOCAL VARIABLES LIKE 'collation_database'";
626 $this->query_start($sql, null, SQL_QUERY_AUX);
627 $result = $this->mysqli->query($sql);
628 $this->query_end($result);
629
630 $return = false;
631 if ($result) {
632 while($row = $result->fetch_assoc()) {
633 if (isset($row['Value'])) {
634 $return = (strpos($row['Value'], 'latin1') !== 0);
635 }
636 break;
637 }
638 $result->close();
639 }
640
641 return $return;
7466a42f 642 }
643
7466a42f 644 /**
645 * Do NOT use in code, to be used by database_manager only!
646 * @param string $sql query
22d77567 647 * @return bool true
648 * @throws dml_exception if error
7466a42f 649 */
650 public function change_database_structure($sql) {
117679db 651 $this->reset_caches();
a6283e91 652
653 $this->query_start($sql, null, SQL_QUERY_STRUCTURE);
654 $result = $this->mysqli->query($sql);
655 $this->query_end($result);
656
12e09c6b 657 return true;
7466a42f 658 }
659
660 /**
661 * Very ugly hack which emulates bound parameters in queries
662 * because prepared statements do not use query cache.
663 */
664 protected function emulate_bound_params($sql, array $params=null) {
665 if (empty($params)) {
666 return $sql;
667 }
668 /// ok, we have verified sql statement with ? and correct number of params
148c65bf
PS
669 $parts = explode('?', $sql);
670 $return = array_shift($parts);
7466a42f 671 foreach ($params as $param) {
672 if (is_bool($param)) {
673 $return .= (int)$param;
674 } else if (is_null($param)) {
675 $return .= 'NULL';
70ff13be
PS
676 } else if (is_number($param)) {
677 $return .= "'".$param."'"; // we have to always use strings because mysql is using weird automatic int casting
c1a6529b 678 } else if (is_float($param)) {
7466a42f 679 $return .= $param;
680 } else {
681 $param = $this->mysqli->real_escape_string($param);
682 $return .= "'$param'";
683 }
148c65bf 684 $return .= array_shift($parts);
7466a42f 685 }
686 return $return;
687 }
688
689 /**
690 * Execute general sql query. Should be used only when no other method suitable.
691 * Do NOT use this to make changes in db structure, use database_manager::execute_sql() instead!
692 * @param string $sql query
693 * @param array $params query parameters
22d77567 694 * @return bool true
695 * @throws dml_exception if error
7466a42f 696 */
697 public function execute($sql, array $params=null) {
698 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
699
700 if (strpos($sql, ';') !== false) {
22d77567 701 throw new coding_exception('moodle_database::execute() Multiple sql statements found or bound parameters not used properly in query!');
7466a42f 702 }
703
704 $rawsql = $this->emulate_bound_params($sql, $params);
705
a6283e91 706 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 707 $result = $this->mysqli->query($rawsql);
a6283e91 708 $this->query_end($result);
7466a42f 709
22d77567 710 if ($result === true) {
7466a42f 711 return true;
712
713 } else {
714 $result->close();
715 return true;
716 }
717 }
718
719 /**
720 * Get a number of records as a moodle_recordset using a SQL statement.
721 *
722 * Since this method is a little less readable, use of it should be restricted to
723 * code where it's possible there might be large datasets being returned. For known
724 * small datasets use get_records_sql - it leads to simpler code.
725 *
726 * The return type is as for @see function get_recordset.
727 *
728 * @param string $sql the SQL select query to execute.
729 * @param array $params array of sql parameters
730 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
731 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
5212b07f 732 * @return moodle_recordset instance
22d77567 733 * @throws dml_exception if error
7466a42f 734 */
735 public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
088a4df8 736 $limitfrom = (int)$limitfrom;
737 $limitnum = (int)$limitnum;
3ff8bf26 738 $limitfrom = ($limitfrom < 0) ? 0 : $limitfrom;
739 $limitnum = ($limitnum < 0) ? 0 : $limitnum;
740
7466a42f 741 if ($limitfrom or $limitnum) {
7466a42f 742 if ($limitnum < 1) {
743 $limitnum = "18446744073709551615";
744 }
0487f9e2 745 $sql .= " LIMIT $limitfrom, $limitnum";
7466a42f 746 }
747
0487f9e2 748 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
749 $rawsql = $this->emulate_bound_params($sql, $params);
750
a6283e91 751 $this->query_start($sql, $params, SQL_QUERY_SELECT);
7466a42f 752 // no MYSQLI_USE_RESULT here, it would block write ops on affected tables
e0eda209 753 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
754 $this->query_end($result);
7466a42f 755
e0eda209 756 return $this->create_recordset($result);
7466a42f 757 }
758
759 protected function create_recordset($result) {
760 return new mysqli_native_moodle_recordset($result);
761 }
762
763 /**
764 * Get a number of records as an array of objects using a SQL statement.
765 *
766 * Return value as for @see function get_records.
767 *
768 * @param string $sql the SQL select query to execute. The first column of this SELECT statement
769 * must be a unique value (usually the 'id' field), as it will be used as the key of the
770 * returned array.
771 * @param array $params array of sql parameters
772 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
773 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
3503dcad 774 * @return array of objects, or empty array if no records were found
22d77567 775 * @throws dml_exception if error
7466a42f 776 */
777 public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
088a4df8 778 $limitfrom = (int)$limitfrom;
779 $limitnum = (int)$limitnum;
3ff8bf26 780 $limitfrom = ($limitfrom < 0) ? 0 : $limitfrom;
781 $limitnum = ($limitnum < 0) ? 0 : $limitnum;
782
7466a42f 783 if ($limitfrom or $limitnum) {
7466a42f 784 if ($limitnum < 1) {
785 $limitnum = "18446744073709551615";
786 }
0487f9e2 787 $sql .= " LIMIT $limitfrom, $limitnum";
7466a42f 788 }
789
0487f9e2 790 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
791 $rawsql = $this->emulate_bound_params($sql, $params);
792
a6283e91 793 $this->query_start($sql, $params, SQL_QUERY_SELECT);
7466a42f 794 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
a6283e91 795 $this->query_end($result);
7466a42f 796
7466a42f 797 $return = array();
a6283e91 798
7466a42f 799 while($row = $result->fetch_assoc()) {
800 $row = array_change_key_case($row, CASE_LOWER);
801 $id = reset($row);
758ba89a 802 if (isset($return[$id])) {
803 $colname = key($row);
804 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);
805 }
7466a42f 806 $return[$id] = (object)$row;
807 }
808 $result->close();
a6283e91 809
7466a42f 810 return $return;
811 }
812
813 /**
814 * Selects records and return values (first field) as an array using a SQL statement.
815 *
816 * @param string $sql The SQL query
817 * @param array $params array of sql parameters
3503dcad 818 * @return array of values
22d77567 819 * @throws dml_exception if error
7466a42f 820 */
821 public function get_fieldset_sql($sql, array $params=null) {
822 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
823 $rawsql = $this->emulate_bound_params($sql, $params);
824
a6283e91 825 $this->query_start($sql, $params, SQL_QUERY_SELECT);
7466a42f 826 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
a6283e91 827 $this->query_end($result);
7466a42f 828
7466a42f 829 $return = array();
a6283e91 830
7466a42f 831 while($row = $result->fetch_assoc()) {
832 $return[] = reset($row);
833 }
834 $result->close();
a6283e91 835
7466a42f 836 return $return;
837 }
838
839 /**
840 * Insert new record into database, as fast as possible, no safety checks, lobs not supported.
841 * @param string $table name
842 * @param mixed $params data record as object or array
843 * @param bool $returnit return it of inserted record
844 * @param bool $bulk true means repeated inserts expected
845 * @param bool $customsequence true if 'id' included in $params, disables $returnid
3503dcad 846 * @return bool|int true or new id
22d77567 847 * @throws dml_exception if error
7466a42f 848 */
849 public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) {
850 if (!is_array($params)) {
851 $params = (array)$params;
852 }
853
854 if ($customsequence) {
855 if (!isset($params['id'])) {
22d77567 856 throw new coding_exception('moodle_database::insert_record_raw() id field must be specified if custom sequences used.');
7466a42f 857 }
858 $returnid = false;
859 } else {
860 unset($params['id']);
861 }
862
863 if (empty($params)) {
22d77567 864 throw new coding_exception('moodle_database::insert_record_raw() no fields found.');
7466a42f 865 }
866
7466a42f 867 $fields = implode(',', array_keys($params));
868 $qms = array_fill(0, count($params), '?');
869 $qms = implode(',', $qms);
870
871 $sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($qms)";
76b6daf2 872
873 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
7466a42f 874 $rawsql = $this->emulate_bound_params($sql, $params);
875
a6283e91 876 $this->query_start($sql, $params, SQL_QUERY_INSERT);
7466a42f 877 $result = $this->mysqli->query($rawsql);
b4154c2d 878 $id = @$this->mysqli->insert_id; // must be called before query_end() which may insert log into db
a6283e91 879 $this->query_end($result);
7466a42f 880
b4154c2d 881 if (!$id) {
22d77567 882 throw new dml_write_exception('unknown error fetching inserted id');
7466a42f 883 }
884
885 if (!$returnid) {
886 return true;
887 } else {
888 return (int)$id;
889 }
890 }
891
892 /**
893 * Insert a record into a table and return the "id" field if required.
894 *
895 * Some conversions and safety checks are carried out. Lobs are supported.
896 * If the return ID isn't required, then this just reports success as true/false.
897 * $data is an object containing needed data
898 * @param string $table The database table to be inserted into
899 * @param object $data A data object with values for one or more fields in the record
900 * @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.
3503dcad 901 * @return bool|int true or new id
22d77567 902 * @throws dml_exception if error
7466a42f 903 */
904 public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {
d8fa8e40 905 $dataobject = (array)$dataobject;
7466a42f 906
907 $columns = $this->get_columns($table);
7466a42f 908 $cleaned = array();
909
910 foreach ($dataobject as $field=>$value) {
d8fa8e40
PS
911 if ($field === 'id') {
912 continue;
913 }
7466a42f 914 if (!isset($columns[$field])) {
915 continue;
916 }
917 $column = $columns[$field];
8fb501e9 918 $cleaned[$field] = $this->normalise_value($column, $value);
7466a42f 919 }
920
7466a42f 921 return $this->insert_record_raw($table, $cleaned, $returnid, $bulk);
922 }
923
924 /**
925 * Import a record into a table, id field is required.
926 * Safety checks are NOT carried out. Lobs are supported.
927 *
928 * @param string $table name of database table to be inserted into
929 * @param object $dataobject A data object with values for one or more fields in the record
22d77567 930 * @return bool true
931 * @throws dml_exception if error
7466a42f 932 */
933 public function import_record($table, $dataobject) {
d8fa8e40 934 $dataobject = (array)$dataobject;
7466a42f 935
7466a42f 936 $columns = $this->get_columns($table);
937 $cleaned = array();
938
939 foreach ($dataobject as $field=>$value) {
940 if (!isset($columns[$field])) {
941 continue;
942 }
943 $cleaned[$field] = $value;
944 }
945
946 return $this->insert_record_raw($table, $cleaned, false, true, true);
947 }
948
949 /**
950 * Update record in database, as fast as possible, no safety checks, lobs not supported.
951 * @param string $table name
952 * @param mixed $params data record as object or array
953 * @param bool true means repeated updates expected
22d77567 954 * @return bool true
955 * @throws dml_exception if error
7466a42f 956 */
957 public function update_record_raw($table, $params, $bulk=false) {
d8fa8e40
PS
958 $params = (array)$params;
959
7466a42f 960 if (!isset($params['id'])) {
22d77567 961 throw new coding_exception('moodle_database::update_record_raw() id field must be specified.');
7466a42f 962 }
963 $id = $params['id'];
964 unset($params['id']);
965
966 if (empty($params)) {
22d77567 967 throw new coding_exception('moodle_database::update_record_raw() no fields found.');
7466a42f 968 }
969
7466a42f 970 $sets = array();
971 foreach ($params as $field=>$value) {
972 $sets[] = "$field = ?";
973 }
974
975 $params[] = $id; // last ? in WHERE condition
976
977 $sets = implode(',', $sets);
978 $sql = "UPDATE {$this->prefix}$table SET $sets WHERE id=?";
76b6daf2 979
980 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
7466a42f 981 $rawsql = $this->emulate_bound_params($sql, $params);
982
a6283e91 983 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 984 $result = $this->mysqli->query($rawsql);
a6283e91 985 $this->query_end($result);
7466a42f 986
7466a42f 987 return true;
988 }
989
990 /**
991 * Update a record in a table
992 *
993 * $dataobject is an object containing needed data
994 * Relies on $dataobject having a variable "id" to
995 * specify the record to update
996 *
997 * @param string $table The database table to be checked against.
998 * @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
999 * @param bool true means repeated updates expected
22d77567 1000 * @return bool true
1001 * @throws dml_exception if error
7466a42f 1002 */
1003 public function update_record($table, $dataobject, $bulk=false) {
d8fa8e40 1004 $dataobject = (array)$dataobject;
7466a42f 1005
7466a42f 1006 $columns = $this->get_columns($table);
1007 $cleaned = array();
1008
1009 foreach ($dataobject as $field=>$value) {
1010 if (!isset($columns[$field])) {
1011 continue;
1012 }
8fb501e9
EL
1013 $column = $columns[$field];
1014 $cleaned[$field] = $this->normalise_value($column, $value);
7466a42f 1015 }
1016
1017 return $this->update_record_raw($table, $cleaned, $bulk);
1018 }
1019
1020 /**
1021 * Set a single field in every table record which match a particular WHERE clause.
1022 *
1023 * @param string $table The database table to be checked against.
1024 * @param string $newfield the field to set.
1025 * @param string $newvalue the value to set the field to.
1026 * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1027 * @param array $params array of sql parameters
22d77567 1028 * @return bool true
1029 * @throws dml_exception if error
7466a42f 1030 */
1031 public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {
1032 if ($select) {
1033 $select = "WHERE $select";
1034 }
1035 if (is_null($params)) {
1036 $params = array();
1037 }
1038 list($select, $params, $type) = $this->fix_sql_params($select, $params);
1039
8fb501e9
EL
1040 // Get column metadata
1041 $columns = $this->get_columns($table);
1042 $column = $columns[$newfield];
1043
1044 $normalised_value = $this->normalise_value($column, $newvalue);
1045
1046 if (is_null($normalised_value)) {
7466a42f 1047 $newfield = "$newfield = NULL";
1048 } else {
1049 $newfield = "$newfield = ?";
8fb501e9 1050 array_unshift($params, $normalised_value);
7466a42f 1051 }
1052 $sql = "UPDATE {$this->prefix}$table SET $newfield $select";
1053 $rawsql = $this->emulate_bound_params($sql, $params);
1054
a6283e91 1055 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 1056 $result = $this->mysqli->query($rawsql);
a6283e91 1057 $this->query_end($result);
7466a42f 1058
7466a42f 1059 return true;
1060 }
1061
1062 /**
1063 * Delete one or more records from a table which match a particular WHERE clause.
1064 *
1065 * @param string $table The database table to be checked against.
1066 * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria).
1067 * @param array $params array of sql parameters
22d77567 1068 * @return bool true
1069 * @throws dml_exception if error
7466a42f 1070 */
1071 public function delete_records_select($table, $select, array $params=null) {
1072 if ($select) {
1073 $select = "WHERE $select";
1074 }
1075 $sql = "DELETE FROM {$this->prefix}$table $select";
1076
1077 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
1078 $rawsql = $this->emulate_bound_params($sql, $params);
1079
a6283e91 1080 $this->query_start($sql, $params, SQL_QUERY_UPDATE);
7466a42f 1081 $result = $this->mysqli->query($rawsql);
a6283e91 1082 $this->query_end($result);
7466a42f 1083
7466a42f 1084 return true;
1085 }
1086
1087 public function sql_cast_char2int($fieldname, $text=false) {
1088 return ' CAST(' . $fieldname . ' AS SIGNED) ';
9882b2c8
EL
1089 }
1090
1091 public function sql_cast_char2real($fieldname, $text=false) {
1092 return ' CAST(' . $fieldname . ' AS DECIMAL) ';
7466a42f 1093 }
1094
6055f89d
PS
1095 /**
1096 * Returns 'LIKE' part of a query.
1097 *
1098 * @param string $fieldname usually name of the table column
1099 * @param string $param usually bound query parameter (?, :named)
1100 * @param bool $casesensitive use case sensitive search
1101 * @param bool $accensensitive use accent sensitive search (not all databases support accent insensitive)
16114b9d 1102 * @param bool $notlike true means "NOT LIKE"
6055f89d
PS
1103 * @param string $escapechar escape char for '%' and '_'
1104 * @return string SQL code fragment
1105 */
16114b9d 1106 public function sql_like($fieldname, $param, $casesensitive = true, $accentsensitive = true, $notlike = false, $escapechar = '\\') {
6055f89d
PS
1107 if (strpos($param, '%') !== false) {
1108 debugging('Potential SQL injection detected, sql_ilike() expects bound parameters (? or :named)');
1109 }
1110 $escapechar = $this->mysqli->real_escape_string($escapechar); // prevents problems with C-style escapes of enclosing '\'
1111
16114b9d 1112 $LIKE = $notlike ? 'NOT LIKE' : 'LIKE';
6055f89d 1113 if ($casesensitive) {
16114b9d 1114 return "$fieldname $LIKE $param COLLATE utf8_bin ESCAPE '$escapechar'";
6055f89d
PS
1115 } else {
1116 if ($accentsensitive) {
16114b9d 1117 return "LOWER($fieldname) $LIKE LOWER($param) COLLATE utf8_bin ESCAPE '$escapechar'";
6055f89d 1118 } else {
16114b9d 1119 return "$fieldname $LIKE $param ESCAPE '$escapechar'";
6055f89d
PS
1120 }
1121 }
1122 }
1123
215d7cb8
SH
1124 /**
1125 * Returns the proper SQL to do CONCAT between the elements passed
1126 * Can take many parameters
1127 *
1128 * @param string $str,... 1 or more fields/strings to concat
1129 *
1130 * @return string The concat sql
1131 */
7466a42f 1132 public function sql_concat() {
1133 $arr = func_get_args();
082ae821 1134 $s = implode(', ', $arr);
7466a42f 1135 if ($s === '') {
d5d0890c 1136 return "''";
7466a42f 1137 }
1138 return "CONCAT($s)";
1139 }
1140
215d7cb8
SH
1141 /**
1142 * Returns the proper SQL to do CONCAT between the elements passed
1143 * with a given separator
1144 *
1145 * @param string $separator The string to use as the separator
1146 * @param array $elements An array of items to concatenate
1147 * @return string The concat SQL
1148 */
7466a42f 1149 public function sql_concat_join($separator="' '", $elements=array()) {
082ae821 1150 $s = implode(', ', $elements);
1151
7466a42f 1152 if ($s === '') {
d5d0890c 1153 return "''";
7466a42f 1154 }
082ae821 1155 return "CONCAT_WS($separator, $s)";
7466a42f 1156 }
1157
7e0db2e2 1158 /**
1159 * Returns the SQL text to be used to calculate the length in characters of one expression.
1160 * @param string fieldname or expression to calculate its length in characters.
1161 * @return string the piece of SQL code to be used in the statement.
1162 */
1163 public function sql_length($fieldname) {
1164 return ' CHAR_LENGTH(' . $fieldname . ')';
1165 }
1166
7466a42f 1167 /**
02df9cfd 1168 * Does this driver support regex syntax when searching
7466a42f 1169 */
1170 public function sql_regex_supported() {
1171 return true;
1172 }
1173
1174 /**
1175 * Return regex positive or negative match sql
1176 * @param bool $positivematch
1177 * @return string or empty if not supported
1178 */
1179 public function sql_regex($positivematch=true) {
1180 return $positivematch ? 'REGEXP' : 'NOT REGEXP';
1181 }
fa76662b 1182
adff97c5 1183 public function sql_cast_2signed($fieldname) {
1184 return ' CAST(' . $fieldname . ' AS SIGNED) ';
1185 }
1186
7f79aaea 1187/// session locking
5e9dd017 1188 public function session_lock_supported() {
1189 return true;
1190 }
1191
3b1a9849 1192 public function get_session_lock($rowid) {
5e9dd017 1193 parent::get_session_lock($rowid);
3b1a9849 1194 $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
1195 $sql = "SELECT GET_LOCK('$fullname',120)";
1196 $this->query_start($sql, null, SQL_QUERY_AUX);
1197 $result = $this->mysqli->query($sql);
7f79aaea 1198 $this->query_end($result);
1199
1200 if ($result) {
1201 $arr = $result->fetch_assoc();
1202 $result->close();
1203
1204 if (reset($arr) == 1) {
5e9dd017 1205 return;
3b1a9849 1206 } else {
1207 // try again!
5e9dd017 1208 $this->get_session_lock($rowid);
7f79aaea 1209 }
1210 }
7f79aaea 1211 }
1212
3b1a9849 1213 public function release_session_lock($rowid) {
5e9dd017 1214 parent::release_session_lock($rowid);
3b1a9849 1215 $fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
1216 $sql = "SELECT RELEASE_LOCK('$fullname')";
1217 $this->query_start($sql, null, SQL_QUERY_AUX);
1218 $result = $this->mysqli->query($sql);
7f79aaea 1219 $this->query_end($result);
1220
1221 if ($result) {
7f79aaea 1222 $result->close();
7f79aaea 1223 }
7f79aaea 1224 }
1225
fa76662b 1226/// transactions
1227 /**
d5a8d9aa
PS
1228 * Are transactions supported?
1229 * It is not responsible to run productions servers
1230 * on databases without transaction support ;-)
1231 *
1232 * MyISAM does not support support transactions.
fa76662b 1233 *
d35ece6c
PS
1234 * You can override this via the dbtransactions option.
1235 *
d5a8d9aa 1236 * @return bool
fa76662b 1237 */
d5a8d9aa
PS
1238 protected function transactions_supported() {
1239 if (!is_null($this->transactions_supported)) {
1240 return $this->transactions_supported;
1241 }
1242
d35ece6c
PS
1243 // this is all just guessing, might be better to just specify it in config.php
1244 if (isset($this->dboptions['dbtransactions'])) {
1245 $this->transactions_supported = $this->dboptions['dbtransactions'];
1246 return $this->transactions_supported;
1247 }
1248
d5a8d9aa
PS
1249 $this->transactions_supported = false;
1250
d35ece6c 1251 $engine = $this->get_dbengine();
d5a8d9aa 1252
d35ece6c
PS
1253 // Only will accept transactions if using compatible storage engine (more engines can be added easily BDB, Falcon...)
1254 if (in_array($engine, array('InnoDB', 'INNOBASE', 'BDB', 'XtraDB', 'Aria', 'Falcon'))) {
1255 $this->transactions_supported = true;
dd67e10c 1256 }
dd67e10c 1257
d5a8d9aa
PS
1258 return $this->transactions_supported;
1259 }
1260
1261 /**
1262 * Driver specific start of real database transaction,
1263 * this can not be used directly in code.
1264 * @return void
1265 */
1266 protected function begin_transaction() {
1267 if (!$this->transactions_supported()) {
1268 return;
a1dda107 1269 }
dd67e10c 1270
a6283e91 1271 $sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED";
1272 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1273 $result = $this->mysqli->query($sql);
1274 $this->query_end($result);
1275
dd67e10c 1276 $sql = "START TRANSACTION";
a6283e91 1277 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1278 $result = $this->mysqli->query($sql);
1279 $this->query_end($result);
fa76662b 1280 }
1281
1282 /**
d5a8d9aa
PS
1283 * Driver specific commit of real database transaction,
1284 * this can not be used directly in code.
1285 * @return void
fa76662b 1286 */
d5a8d9aa
PS
1287 protected function commit_transaction() {
1288 if (!$this->transactions_supported()) {
1289 return;
a1dda107 1290 }
d5a8d9aa 1291
a6283e91 1292 $sql = "COMMIT";
1293 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1294 $result = $this->mysqli->query($sql);
1295 $this->query_end($result);
fa76662b 1296 }
1297
1298 /**
d5a8d9aa
PS
1299 * Driver specific abort of real database transaction,
1300 * this can not be used directly in code.
1301 * @return void
fa76662b 1302 */
d5a8d9aa
PS
1303 protected function rollback_transaction() {
1304 if (!$this->transactions_supported()) {
1305 return;
a1dda107 1306 }
d5a8d9aa 1307
a6283e91 1308 $sql = "ROLLBACK";
1309 $this->query_start($sql, NULL, SQL_QUERY_AUX);
1310 $result = $this->mysqli->query($sql);
1311 $this->query_end($result);
1312
fa76662b 1313 return true;
1314 }
7466a42f 1315}