MDL-14679 dml/native_mysqli new experimental native driver for mysqli - not fully...
[moodle.git] / lib / dml / mysqli_native_moodle_database.php
CommitLineData
7466a42f 1<?php //$Id$
2
3require_once($CFG->libdir.'/dml/moodle_database.php');
4require_once($CFG->libdir.'/dml/mysqli_native_moodle_recordset.php');
5
6/**
7 * Native mysqli class representing moodle database interface.
8 * @package dml
9 */
10class mysqli_native_moodle_database extends moodle_database {
11
12 protected $mysqli=null;
13
14 /**
15 * Detects if all needed PHP stuff installed.
16 * Note: can be used before connect()
17 * @return mixed true if ok, string if something
18 */
19 public function driver_installed() {
20 if (!extension_loaded('mysqli')) {
21 return get_string('mysqliextensionisnotpresentinphp', 'install');
22 }
23 return true;
24 }
25
26 /**
27 * Returns database family type - describes SQL dialect
28 * Note: can be used before connect()
29 * @return string db family name (mysql, postgres, mssql, oracle, etc.)
30 */
31 public function get_dbfamily() {
32 return 'mysql';
33 }
34
35 /**
36 * Returns more specific database driver type
37 * Note: can be used before connect()
38 * @return string db type mysql, mysqli, postgres7
39 */
40 protected function get_dbtype() {
41 return 'mysqli';
42 }
43
44 /**
45 * Returns general database library name
46 * Note: can be used before connect()
47 * @return string db type adodb, pdo, native
48 */
49 protected function get_dblibrary() {
50 return 'native';
51 }
52
53 /**
54 * Returns localised database type name
55 * Note: can be used before connect()
56 * @return string
57 */
58 public function get_name() {
59 return get_string('nativemysqli', 'install'); //TODO: localise
60 }
61
62 /**
63 * Returns localised database description
64 * Note: can be used before connect()
65 * @return string
66 */
67 public function get_configuration_hints() {
68 return get_string('databasesettingssub_mysqlinative', 'install'); //TODO: localise
69 }
70
71 /**
72 * Connect to db
73 * Must be called before other methods.
74 * @param string $dbhost
75 * @param string $dbuser
76 * @param string $dbpass
77 * @param string $dbname
78 * @param bool $dbpersist
79 * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
80 * @param array $dboptions driver specific options
81 * @return bool success
82 */
83 public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, array $dboptions=null) {
84 global $CFG;
85
86 $this->store_settings($dbhost, $dbuser, $dbpass, $dbname, false, $prefix, $dboptions);
87
88 $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
89 if ($this->mysqli->connect_error) {
90 return false;
91 }
92 $this->mysqli->set_charset('utf8');
93 return true;
94 }
95
96 /**
97 * Close database connection and release all resources
98 * and memory (especially circular memory references).
99 * Do NOT use connect() again, create a new instance if needed.
100 */
101 public function dispose() {
102 if ($this->mysqli) {
103 $this->mysqli->close();
104 $this->mysqli = null;
105 }
106 parent::dispose();
107 }
108
109 /**
110 * Returns database server info array
111 * @return array
112 */
113 public function get_server_info() {
114 return array('description'=>$this->mysqli->server_info, 'version'=>$this->mysqli->server_info);
115 }
116
117 /**
118 * Returns supported query parameter types
119 * @return bitmask
120 */
121 protected function allowed_param_types() {
122 return SQL_PARAMS_QM;
123 }
124
125 /**
126 * Returns last error reported by database engine.
127 */
128 public function get_last_error() {
129 return $this->mysqli->error;
130 }
131
132 /**
133 * Return tables in database WITHOUT current prefix
134 * @return array of table names in lowercase and without prefix
135 */
136 public function get_tables() {
137 $this->reads++;
138 $tables = array();
139 if ($result = $this->mysqli->query("SHOW TABLES")) {
140 while ($arr = $result->fetch_assoc()) {
141 $tablename = reset($arr);
142 if (strpos($tablename, $this->prefix) !== 0) {
143 continue;
144 }
145 $tablename = substr($tablename, strlen($this->prefix));
146 $tables[$tablename] = $tablename;
147 }
148 $result->close();
149 }
150 return $tables;
151 }
152
153 /**
154 * Return table indexes - everything lowercased
155 * @return array of arrays
156 */
157 public function get_indexes($table) {
158 $preflen = strlen($this->prefix);
159 $indexes = array();
160 if ($result = $this->mysqli->query("SHOW INDEXES FROM {$this->prefix}$table")) {
161 while ($res = $result->fetch_object()) {
162 if ($res->Key_name === 'PRIMARY') {
163 continue;
164 }
165 if (!isset($indexes[$res->Key_name])) {
166 $indexes[$res->Key_name] = array('unique'=>empty($res->Non_unique), 'columns'=>array());
167 }
168 $indexes[$res->Key_name]['columns'][$res->Seq_in_index-1] = $res->Column_name;
169 }
170 $result->close();
171 }
172 return $indexes;
173 }
174
175 /**
176 * Returns datailed information about columns in table. This information is cached internally.
177 * @param string $table name
178 * @param bool $usecache
179 * @return array array of database_column_info objects indexed with column names
180 */
181 public function get_columns($table, $usecache=true) {
182 if ($usecache and isset($this->columns[$table])) {
183 return $this->columns[$table];
184 }
185
186 $this->columns[$table] = array();
187
188 if (!$rawcolumns = $this->get_records_sql("SHOW COLUMNS FROM {".$table."}")) {
189 return array();
190 }
191
192 foreach ($rawcolumns as $rawcolumn) {
193 $info = new object();
194 $info->name = $rawcolumn->field;
195 $matches = null;
196
197 if (preg_match('/varchar\((\d+)\)/i', $rawcolumn->type, $matches)) {
198 $info->type = 'varchar';
199 $info->meta_type = 'C';
200 $info->max_length = $matches[1];
201 $info->scale = null;
202 $info->not_null = ($rawcolumn->null === 'NO');
203 $info->default_value = $rawcolumn->default;
204 $info->has_default = is_null($info->default_value) ? false : true;
205 $info->primary_key = ($rawcolumn->key === 'PRI');
206 $info->binary = false;
207 $info->unsigned = null;
208 $info->auto_increment= false;
209 $info->unique = null;
210
211 } else if (preg_match('/([a-z]*int[a-z]*)\((\d+)\)/i', $rawcolumn->type, $matches)) {
212 $info->type = $matches[1];
213 $info->primary_key = ($rawcolumn->key === 'PRI');
214 if ($info->primary_key) {
215 $info->meta_type = 'R';
216 $info->max_length = $matches[2];
217 $info->scale = null;
218 $info->not_null = ($rawcolumn->null === 'NO');
219 $info->default_value = $rawcolumn->default;
220 $info->has_default = is_null($info->default_value) ? false : true;
221 $info->binary = false;
222 $info->unsigned = (stripos($rawcolumn->type, 'unsigned') !== false);
223 $info->auto_increment= true;
224 $info->unique = true;
225 } else {
226 $info->meta_type = 'I';
227 $info->max_length = $matches[2];
228 $info->scale = null;
229 $info->not_null = ($rawcolumn->null === 'NO');
230 $info->default_value = $rawcolumn->default;
231 $info->has_default = is_null($info->default_value) ? false : true;
232 $info->binary = false;
233 $info->unsigned = (stripos($rawcolumn->type, 'unsigned') !== false);
234 $info->auto_increment= false;
235 $info->unique = null;
236 }
237
238 } else if (preg_match('/decimal\((\d+),(\d+)\)/i', $rawcolumn->type, $matches)) {
239 $info->type = 'decimal';
240 $info->meta_type = 'N';
241 $info->max_length = $matches[1];
242 $info->scale = $matches[2];
243 $info->not_null = ($rawcolumn->null === 'NO');
244 $info->default_value = $rawcolumn->default;
245 $info->has_default = is_null($info->default_value) ? false : true;
246 $info->primary_key = ($rawcolumn->key === 'PRI');
247 $info->binary = false;
248 $info->unsigned = null;
249 $info->auto_increment= false;
250 $info->unique = null;
251
252 } else if (preg_match('/([a-z]*text)/i', $rawcolumn->type, $matches)) {
253 $info->type = $matches[1];
254 $info->meta_type = 'X';
255 $info->max_length = -1;
256 $info->scale = null;
257 $info->not_null = ($rawcolumn->null === 'NO');
258 $info->default_value = $rawcolumn->default;
259 $info->has_default = is_null($info->default_value) ? false : true;
260 $info->primary_key = ($rawcolumn->key === 'PRI');
261 $info->binary = false;
262 $info->unsigned = null;
263 $info->auto_increment= false;
264 $info->unique = null;
265
266 } else if (preg_match('/enum\((.*)\)/i', $rawcolumn->type, $matches)) {
267 $info->type = 'enum';
268 $info->meta_type = 'C';
269 $info->enums = array();
270 $info->max_length = 0;
271 $values = $matches[1];
272 $values = explode(',', $values);
273 $textlib = textlib_get_instance();
274 foreach ($values as $val) {
275 $val = trim($val, "'");
276 $length = $textlib->strlen($val);
277 $info->enums[] = $val;
278 $info->max_length = ($info->max_length < $length) ? $length : $info->max_length;
279 }
280 $info->scale = null;
281 $info->not_null = ($rawcolumn->null === 'NO');
282 $info->default_value = $rawcolumn->default;
283 $info->has_default = is_null($info->default_value) ? false : true;
284 $info->primary_key = ($rawcolumn->key === 'PRI');
285 $info->binary = false;
286 $info->unsigned = null;
287 $info->auto_increment= false;
288 $info->unique = null;
289 }
290
291 $this->columns[$table][$info->name] = new database_column_info($info);
292 }
293
294 return $this->columns[$table];
295 }
296
297 /**
298 * Reset a sequence to the id field of a table.
299 * @param string $table name of table
300 * @return success
301 */
302 public function reset_sequence($table) {
303 // From http://dev.mysql.com/doc/refman/5.0/en/alter-table.html
304 if (!$this->get_manager()->table_exists($table)) {
305 return false;
306 }
307 $value = (int)$this->get_field_sql('SELECT MAX(id) FROM {'.$table.'}');
308 $value++;
309 return $this->change_database_structure("ALTER TABLE $this->prefix$table AUTO_INCREMENT = $value");
310 }
311
312 /**
313 * Is db in unicode mode?
314 * @return bool
315 */
316 public function setup_is_unicodedb() {
317 $this->reads++;
318 if ($result = $this->mysqli->query("SHOW LOCAL VARIABLES LIKE 'character_set_database'")) {
319 $result->close();
320 return true;
321 }
322 return false;
323 }
324
325 /**
326 * Enable/disable very detailed debugging
327 * TODO: do we need levels?
328 * @param bool $state
329 */
330 public function set_debug($state) {
331 //TODO
332 }
333
334 /**
335 * Returns debug status
336 * @return bool $state
337 */
338 public function get_debug() {
339 //TODO
340 }
341
342 /**
343 * Enable/disable detailed sql logging
344 * TODO: do we need levels?
345 * @param bool $state
346 */
347 public function set_logging($state) {
348 //TODO
349 }
350
351 /**
352 * Do NOT use in code, to be used by database_manager only!
353 * @param string $sql query
354 * @return bool success
355 */
356 public function change_database_structure($sql) {
357 return ($this->mysqli->query($sql) === TRUE);
358 }
359
360 /**
361 * Very ugly hack which emulates bound parameters in queries
362 * because prepared statements do not use query cache.
363 */
364 protected function emulate_bound_params($sql, array $params=null) {
365 if (empty($params)) {
366 return $sql;
367 }
368 /// ok, we have verified sql statement with ? and correct number of params
369 $return = strtok($sql, '?');
370 foreach ($params as $param) {
371 if (is_bool($param)) {
372 $return .= (int)$param;
373 } else if (is_null($param)) {
374 $return .= 'NULL';
375 } else if (is_numeric($param)) {
376 $return .= $param;
377 } else {
378 $param = $this->mysqli->real_escape_string($param);
379 $return .= "'$param'";
380 }
381 $return .= strtok('?');
382 }
383 return $return;
384 }
385
386 /**
387 * Execute general sql query. Should be used only when no other method suitable.
388 * Do NOT use this to make changes in db structure, use database_manager::execute_sql() instead!
389 * @param string $sql query
390 * @param array $params query parameters
391 * @return bool success
392 */
393 public function execute($sql, array $params=null) {
394 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
395
396 if (strpos($sql, ';') !== false) {
397 debugging('Error: Multiple sql statements found or bound parameters not used properly in query!');
398 return false;
399 }
400
401 $rawsql = $this->emulate_bound_params($sql, $params);
402
403 $this->writes++;
404
405 $result = $this->mysqli->query($rawsql);
406
407 if ($result === false) {
408 $this->report_error($sql, $params);
409 return false;
410
411 } else if ($result === true) {
412 return true;
413
414 } else {
415 $result->close();
416 return true;
417 }
418 }
419
420 /**
421 * Get a number of records as a moodle_recordset using a SQL statement.
422 *
423 * Since this method is a little less readable, use of it should be restricted to
424 * code where it's possible there might be large datasets being returned. For known
425 * small datasets use get_records_sql - it leads to simpler code.
426 *
427 * The return type is as for @see function get_recordset.
428 *
429 * @param string $sql the SQL select query to execute.
430 * @param array $params array of sql parameters
431 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
432 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
433 * @return mixed an moodle_recorset object, or false if an error occured.
434 */
435 public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
436 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
437 $rawsql = $this->emulate_bound_params($sql, $params);
438
439 if ($limitfrom or $limitnum) {
440 $limitfrom = (int)$limitfrom;
441 $limitnum = (int)$limitnum;
442 if ($limitnum < 1) {
443 $limitnum = "18446744073709551615";
444 }
445 $rawsql .= " LIMIT $limitfrom, $limitnum";
446 }
447
448 $this->reads++;
449 // no MYSQLI_USE_RESULT here, it would block write ops on affected tables
450 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
451
452 if ($result === false) {
453 $this->report_error($sql, $params);
454 return false;
455 }
456
457 return $this->create_recordset($result);
458 }
459
460 protected function create_recordset($result) {
461 return new mysqli_native_moodle_recordset($result);
462 }
463
464 /**
465 * Get a number of records as an array of objects using a SQL statement.
466 *
467 * Return value as for @see function get_records.
468 *
469 * @param string $sql the SQL select query to execute. The first column of this SELECT statement
470 * must be a unique value (usually the 'id' field), as it will be used as the key of the
471 * returned array.
472 * @param array $params array of sql parameters
473 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
474 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
475 * @return mixed an array of objects, or empty array if no records were found, or false if an error occured.
476 */
477 public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
478 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
479 $rawsql = $this->emulate_bound_params($sql, $params);
480
481 if ($limitfrom or $limitnum) {
482 $limitfrom = (int)$limitfrom;
483 $limitnum = (int)$limitnum;
484 if ($limitnum < 1) {
485 $limitnum = "18446744073709551615";
486 }
487 $rawsql .= " LIMIT $limitfrom, $limitnum";
488 }
489
490 $this->reads++;
491 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
492
493 if ($result === false) {
494 $this->report_error($sql, $params);
495 return false;
496 }
497
498 $return = array();
499
500 while($row = $result->fetch_assoc()) {
501 $row = array_change_key_case($row, CASE_LOWER);
502 $id = reset($row);
503 $return[$id] = (object)$row;
504 }
505 $result->close();
506
507 return $return;
508 }
509
510 /**
511 * Selects records and return values (first field) as an array using a SQL statement.
512 *
513 * @param string $sql The SQL query
514 * @param array $params array of sql parameters
515 * @return mixed array of values or false if an error occured
516 */
517 public function get_fieldset_sql($sql, array $params=null) {
518 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
519 $rawsql = $this->emulate_bound_params($sql, $params);
520
521 $this->reads++;
522 $result = $this->mysqli->query($rawsql, MYSQLI_STORE_RESULT);
523
524 if ($result === false) {
525 $this->report_error($sql, $params);
526 return false;
527 }
528
529 $return = array();
530
531 while($row = $result->fetch_assoc()) {
532 $return[] = reset($row);
533 }
534 $result->close();
535
536 return $return;
537 }
538
539 /**
540 * Insert new record into database, as fast as possible, no safety checks, lobs not supported.
541 * @param string $table name
542 * @param mixed $params data record as object or array
543 * @param bool $returnit return it of inserted record
544 * @param bool $bulk true means repeated inserts expected
545 * @param bool $customsequence true if 'id' included in $params, disables $returnid
546 * @return mixed success or new id
547 */
548 public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false) {
549 if (!is_array($params)) {
550 $params = (array)$params;
551 }
552
553 if ($customsequence) {
554 if (!isset($params['id'])) {
555 return false;
556 }
557 $returnid = false;
558 } else {
559 unset($params['id']);
560 }
561
562 if (empty($params)) {
563 return false;
564 }
565
566 $this->writes++;
567
568 $fields = implode(',', array_keys($params));
569 $qms = array_fill(0, count($params), '?');
570 $qms = implode(',', $qms);
571
572 $sql = "INSERT INTO {$this->prefix}$table ($fields) VALUES($qms)";
573 $rawsql = $this->emulate_bound_params($sql, $params);
574
575 $result = $this->mysqli->query($rawsql);
576
577 if ($result === false) {
578 $this->report_error($sql, $params);
579 return false;
580 }
581
582 if (!$id = $this->mysqli->insert_id) {
583 return false;
584 }
585
586 if (!$returnid) {
587 return true;
588 } else {
589 return (int)$id;
590 }
591 }
592
593 /**
594 * Insert a record into a table and return the "id" field if required.
595 *
596 * Some conversions and safety checks are carried out. Lobs are supported.
597 * If the return ID isn't required, then this just reports success as true/false.
598 * $data is an object containing needed data
599 * @param string $table The database table to be inserted into
600 * @param object $data A data object with values for one or more fields in the record
601 * @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.
602 * @return mixed success or new ID
603 */
604 public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {
605 if (!is_object($dataobject)) {
606 $dataobject = (object)$dataobject;
607 }
608
609 $columns = $this->get_columns($table);
610
611 unset($dataobject->id);
612 $cleaned = array();
613
614 foreach ($dataobject as $field=>$value) {
615 if (!isset($columns[$field])) {
616 continue;
617 }
618 $column = $columns[$field];
619 if (is_bool($value)) {
620 $value = (int)$value; // prevent "false" problems
621 }
622 if (!empty($column->enums)) {
623 // workaround for problem with wrong enums in mysql
624 if (is_null($value) and !$column->not_null) {
625 // ok - nulls allowed
626 } else {
627 if (!in_array((string)$value, $column->enums)) {
628 debugging('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.');
629 return false;
630 }
631 }
632 }
633 $cleaned[$field] = $value;
634 }
635
636 if (empty($cleaned)) {
637 return false;
638 }
639
640 return $this->insert_record_raw($table, $cleaned, $returnid, $bulk);
641 }
642
643 /**
644 * Import a record into a table, id field is required.
645 * Safety checks are NOT carried out. Lobs are supported.
646 *
647 * @param string $table name of database table to be inserted into
648 * @param object $dataobject A data object with values for one or more fields in the record
649 * @return bool success
650 */
651 public function import_record($table, $dataobject) {
652 $dataobject = (object)$dataobject;
653
654 if (empty($dataobject->id)) {
655 return false;
656 }
657
658 $columns = $this->get_columns($table);
659 $cleaned = array();
660
661 foreach ($dataobject as $field=>$value) {
662 if (!isset($columns[$field])) {
663 continue;
664 }
665 $cleaned[$field] = $value;
666 }
667
668 return $this->insert_record_raw($table, $cleaned, false, true, true);
669 }
670
671 /**
672 * Update record in database, as fast as possible, no safety checks, lobs not supported.
673 * @param string $table name
674 * @param mixed $params data record as object or array
675 * @param bool true means repeated updates expected
676 * @return bool success
677 */
678 public function update_record_raw($table, $params, $bulk=false) {
679 if (!is_array($params)) {
680 $params = (array)$params;
681 }
682 if (!isset($params['id'])) {
683 return false;
684 }
685 $id = $params['id'];
686 unset($params['id']);
687
688 if (empty($params)) {
689 return false;
690 }
691
692 $this->writes++;
693
694 $sets = array();
695 foreach ($params as $field=>$value) {
696 $sets[] = "$field = ?";
697 }
698
699 $params[] = $id; // last ? in WHERE condition
700
701 $sets = implode(',', $sets);
702 $sql = "UPDATE {$this->prefix}$table SET $sets WHERE id=?";
703 $rawsql = $this->emulate_bound_params($sql, $params);
704
705 $result = $this->mysqli->query($rawsql);
706
707 if ($result === false) {
708 $this->report_error($sql, $params);
709 return false;
710 }
711
712 return true;
713 }
714
715 /**
716 * Update a record in a table
717 *
718 * $dataobject is an object containing needed data
719 * Relies on $dataobject having a variable "id" to
720 * specify the record to update
721 *
722 * @param string $table The database table to be checked against.
723 * @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
724 * @param bool true means repeated updates expected
725 * @return bool success
726 */
727 public function update_record($table, $dataobject, $bulk=false) {
728 if (!is_object($dataobject)) {
729 $dataobject = (object)$dataobject;
730 }
731
732 if (!isset($dataobject->id) ) {
733 return false;
734 }
735
736 $columns = $this->get_columns($table);
737 $cleaned = array();
738
739 foreach ($dataobject as $field=>$value) {
740 if (!isset($columns[$field])) {
741 continue;
742 }
743 if (is_bool($value)) {
744 $value = (int)$value; // prevent "false" problems
745 }
746 $cleaned[$field] = $value;
747 }
748
749 return $this->update_record_raw($table, $cleaned, $bulk);
750 }
751
752 /**
753 * Set a single field in every table record which match a particular WHERE clause.
754 *
755 * @param string $table The database table to be checked against.
756 * @param string $newfield the field to set.
757 * @param string $newvalue the value to set the field to.
758 * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
759 * @param array $params array of sql parameters
760 * @return bool success
761 */
762 public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {
763 if ($select) {
764 $select = "WHERE $select";
765 }
766 if (is_null($params)) {
767 $params = array();
768 }
769 list($select, $params, $type) = $this->fix_sql_params($select, $params);
770
771 if (is_bool($newvalue)) {
772 $newvalue = (int)$newvalue; // prevent "false" problems
773 }
774 if (is_null($newvalue)) {
775 $newfield = "$newfield = NULL";
776 } else {
777 $newfield = "$newfield = ?";
778 array_unshift($params, $newvalue);
779 }
780 $sql = "UPDATE {$this->prefix}$table SET $newfield $select";
781 $rawsql = $this->emulate_bound_params($sql, $params);
782
783 $this->writes++;
784 $result = $this->mysqli->query($rawsql);
785
786 if ($result === false) {
787 $this->report_error($sql, $params);
788 return false;
789 }
790
791 return true;
792 }
793
794 /**
795 * Delete one or more records from a table which match a particular WHERE clause.
796 *
797 * @param string $table The database table to be checked against.
798 * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria).
799 * @param array $params array of sql parameters
800 * @return returns success.
801 */
802 public function delete_records_select($table, $select, array $params=null) {
803 if ($select) {
804 $select = "WHERE $select";
805 }
806 $sql = "DELETE FROM {$this->prefix}$table $select";
807
808 list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
809 $rawsql = $this->emulate_bound_params($sql, $params);
810
811 $this->writes++;
812 $result = $this->mysqli->query($rawsql);
813
814 if ($result === false) {
815 $this->report_error($sql, $params);
816 return false;
817 }
818
819 return true;
820 }
821
822 public function sql_cast_char2int($fieldname, $text=false) {
823 return ' CAST(' . $fieldname . ' AS SIGNED) ';
824 }
825
826 public function sql_concat() {
827 $arr = func_get_args();
828 $s = implode(',', $arr);
829 if ($s === '') {
830 //TODO: error
831 }
832 return "CONCAT($s)";
833 }
834
835 public function sql_concat_join($separator="' '", $elements=array()) {
836 for ($n=count($elements)-1; $n > 0 ; $n--) {
837 array_splice($elements, $n, 0, $separator);
838 }
839 $s = implode(',', $elements);
840 if ($s === '') {
841 //TODO: error
842 }
843 return "CONCAT ($s)";
844 }
845
846 public function sql_substr() {
847 return "SUBSTRING";
848 }
849
850
851 /**
852 * Does this driver suppoer regex syntax when searching
853 */
854 public function sql_regex_supported() {
855 return true;
856 }
857
858 /**
859 * Return regex positive or negative match sql
860 * @param bool $positivematch
861 * @return string or empty if not supported
862 */
863 public function sql_regex($positivematch=true) {
864 return $positivematch ? 'REGEXP' : 'NOT REGEXP';
865 }
866}