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