source: moodle/trunk/fuentes/lib/dml/moodle_database.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 104.5 KB
Line 
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Abstract database driver class.
19 *
20 * @package    core_dml
21 * @copyright  2008 Petr Skoda (http://skodak.org)
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once(__DIR__.'/database_column_info.php');
28require_once(__DIR__.'/moodle_recordset.php');
29require_once(__DIR__.'/moodle_transaction.php');
30
31/** SQL_PARAMS_NAMED - Bitmask, indicates :name type parameters are supported by db backend. */
32define('SQL_PARAMS_NAMED', 1);
33
34/** SQL_PARAMS_QM - Bitmask, indicates ? type parameters are supported by db backend. */
35define('SQL_PARAMS_QM', 2);
36
37/** SQL_PARAMS_DOLLAR - Bitmask, indicates $1, $2, ... type parameters are supported by db backend. */
38define('SQL_PARAMS_DOLLAR', 4);
39
40/** SQL_QUERY_SELECT - Normal select query, reading only. */
41define('SQL_QUERY_SELECT', 1);
42
43/** SQL_QUERY_INSERT - Insert select query, writing. */
44define('SQL_QUERY_INSERT', 2);
45
46/** SQL_QUERY_UPDATE - Update select query, writing. */
47define('SQL_QUERY_UPDATE', 3);
48
49/** SQL_QUERY_STRUCTURE - Query changing db structure, writing. */
50define('SQL_QUERY_STRUCTURE', 4);
51
52/** SQL_QUERY_AUX - Auxiliary query done by driver, setting connection config, getting table info, etc. */
53define('SQL_QUERY_AUX', 5);
54
55/**
56 * Abstract class representing moodle database interface.
57 * @link http://docs.moodle.org/dev/DML_functions
58 *
59 * @package    core_dml
60 * @copyright  2008 Petr Skoda (http://skodak.org)
61 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
62 */
63abstract class moodle_database {
64
65    /** @var database_manager db manager which allows db structure modifications. */
66    protected $database_manager;
67    /** @var moodle_temptables temptables manager to provide cross-db support for temp tables. */
68    protected $temptables;
69    /** @var array Cache of table info. */
70    protected $tables  = null;
71
72    // db connection options
73    /** @var string db host name. */
74    protected $dbhost;
75    /** @var string db host user. */
76    protected $dbuser;
77    /** @var string db host password. */
78    protected $dbpass;
79    /** @var string db name. */
80    protected $dbname;
81    /** @var string Prefix added to table names. */
82    protected $prefix;
83
84    /** @var array Database or driver specific options, such as sockets or TCP/IP db connections. */
85    protected $dboptions;
86
87    /** @var bool True means non-moodle external database used.*/
88    protected $external;
89
90    /** @var int The database reads (performance counter).*/
91    protected $reads = 0;
92    /** @var int The database writes (performance counter).*/
93    protected $writes = 0;
94    /** @var float Time queries took to finish, seconds with microseconds.*/
95    protected $queriestime = 0;
96
97    /** @var int Debug level. */
98    protected $debug  = 0;
99
100    /** @var string Last used query sql. */
101    protected $last_sql;
102    /** @var array Last query parameters. */
103    protected $last_params;
104    /** @var int Last query type. */
105    protected $last_type;
106    /** @var string Last extra info. */
107    protected $last_extrainfo;
108    /** @var float Last time in seconds with millisecond precision. */
109    protected $last_time;
110    /** @var bool Flag indicating logging of query in progress. This helps prevent infinite loops. */
111    private $loggingquery = false;
112
113    /** @var bool True if the db is used for db sessions. */
114    protected $used_for_db_sessions = false;
115
116    /** @var array Array containing open transactions. */
117    private $transactions = array();
118    /** @var bool Flag used to force rollback of all current transactions. */
119    private $force_rollback = false;
120
121    /** @var string MD5 of settings used for connection. Used by MUC as an identifier. */
122    private $settingshash;
123
124    /** @var cache_application for column info */
125    protected $metacache;
126
127    /** @var bool flag marking database instance as disposed */
128    protected $disposed;
129
130    /**
131     * @var int internal temporary variable used to fix params. Its used by {@link _fix_sql_params_dollar_callback()}.
132     */
133    private $fix_sql_params_i;
134    /**
135     * @var int internal temporary variable used to guarantee unique parameters in each request. Its used by {@link get_in_or_equal()}.
136     */
137    private $inorequaluniqueindex = 1;
138
139    /**
140     * Constructor - Instantiates the database, specifying if it's external (connect to other systems) or not (Moodle DB).
141     *              Note that this affects the decision of whether prefix checks must be performed or not.
142     * @param bool $external True means that an external database is used.
143     */
144    public function __construct($external=false) {
145        $this->external  = $external;
146    }
147
148    /**
149     * Destructor - cleans up and flushes everything needed.
150     */
151    public function __destruct() {
152        $this->dispose();
153    }
154
155    /**
156     * Detects if all needed PHP stuff are installed for DB connectivity.
157     * Note: can be used before connect()
158     * @return mixed True if requirements are met, otherwise a string if something isn't installed.
159     */
160    public abstract function driver_installed();
161
162    /**
163     * Returns database table prefix
164     * Note: can be used before connect()
165     * @return string The prefix used in the database.
166     */
167    public function get_prefix() {
168        return $this->prefix;
169    }
170
171    /**
172     * Loads and returns a database instance with the specified type and library.
173     *
174     * The loaded class is within lib/dml directory and of the form: $type.'_'.$library.'_moodle_database'
175     *
176     * @param string $type Database driver's type. (eg: mysqli, pgsql, mssql, sqldrv, oci, etc.)
177     * @param string $library Database driver's library (native, pdo, etc.)
178     * @param bool $external True if this is an external database.
179     * @return moodle_database driver object or null if error, for example of driver object see {@link mysqli_native_moodle_database}
180     */
181    public static function get_driver_instance($type, $library, $external = false) {
182        global $CFG;
183
184        $classname = $type.'_'.$library.'_moodle_database';
185        $libfile   = "$CFG->libdir/dml/$classname.php";
186
187        if (!file_exists($libfile)) {
188            return null;
189        }
190
191        require_once($libfile);
192        return new $classname($external);
193    }
194
195    /**
196     * Returns the database vendor.
197     * Note: can be used before connect()
198     * @return string The db vendor name, usually the same as db family name.
199     */
200    public function get_dbvendor() {
201        return $this->get_dbfamily();
202    }
203
204    /**
205     * Returns the database family type. (This sort of describes the SQL 'dialect')
206     * Note: can be used before connect()
207     * @return string The db family name (mysql, postgres, mssql, oracle, etc.)
208     */
209    public abstract function get_dbfamily();
210
211    /**
212     * Returns a more specific database driver type
213     * Note: can be used before connect()
214     * @return string The db type mysqli, pgsql, oci, mssql, sqlsrv
215     */
216    protected abstract function get_dbtype();
217
218    /**
219     * Returns the general database library name
220     * Note: can be used before connect()
221     * @return string The db library type -  pdo, native etc.
222     */
223    protected abstract function get_dblibrary();
224
225    /**
226     * Returns the localised database type name
227     * Note: can be used before connect()
228     * @return string
229     */
230    public abstract function get_name();
231
232    /**
233     * Returns the localised database configuration help.
234     * Note: can be used before connect()
235     * @return string
236     */
237    public abstract function get_configuration_help();
238
239    /**
240     * Returns the localised database description
241     * Note: can be used before connect()
242     * @deprecated since 2.6
243     * @return string
244     */
245    public function get_configuration_hints() {
246        debugging('$DB->get_configuration_hints() method is deprecated, use $DB->get_configuration_help() instead');
247        return $this->get_configuration_help();
248    }
249
250    /**
251     * Returns the db related part of config.php
252     * @return stdClass
253     */
254    public function export_dbconfig() {
255        $cfg = new stdClass();
256        $cfg->dbtype    = $this->get_dbtype();
257        $cfg->dblibrary = $this->get_dblibrary();
258        $cfg->dbhost    = $this->dbhost;
259        $cfg->dbname    = $this->dbname;
260        $cfg->dbuser    = $this->dbuser;
261        $cfg->dbpass    = $this->dbpass;
262        $cfg->prefix    = $this->prefix;
263        if ($this->dboptions) {
264            $cfg->dboptions = $this->dboptions;
265        }
266
267        return $cfg;
268    }
269
270    /**
271     * Diagnose database and tables, this function is used
272     * to verify database and driver settings, db engine types, etc.
273     *
274     * @return string null means everything ok, string means problem found.
275     */
276    public function diagnose() {
277        return null;
278    }
279
280    /**
281     * Connects to the database.
282     * Must be called before other methods.
283     * @param string $dbhost The database host.
284     * @param string $dbuser The database user to connect as.
285     * @param string $dbpass The password to use when connecting to the database.
286     * @param string $dbname The name of the database being connected to.
287     * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
288     * @param array $dboptions driver specific options
289     * @return bool true
290     * @throws dml_connection_exception if error
291     */
292    public abstract function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null);
293
294    /**
295     * Store various database settings
296     * @param string $dbhost The database host.
297     * @param string $dbuser The database user to connect as.
298     * @param string $dbpass The password to use when connecting to the database.
299     * @param string $dbname The name of the database being connected to.
300     * @param mixed $prefix string means moodle db prefix, false used for external databases where prefix not used
301     * @param array $dboptions driver specific options
302     * @return void
303     */
304    protected function store_settings($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null) {
305        $this->dbhost    = $dbhost;
306        $this->dbuser    = $dbuser;
307        $this->dbpass    = $dbpass;
308        $this->dbname    = $dbname;
309        $this->prefix    = $prefix;
310        $this->dboptions = (array)$dboptions;
311    }
312
313    /**
314     * Returns a hash for the settings used during connection.
315     *
316     * If not already requested it is generated and stored in a private property.
317     *
318     * @return string
319     */
320    protected function get_settings_hash() {
321        if (empty($this->settingshash)) {
322            $this->settingshash = md5($this->dbhost . $this->dbuser . $this->dbname . $this->prefix);
323        }
324        return $this->settingshash;
325    }
326
327    /**
328     * Attempt to create the database
329     * @param string $dbhost The database host.
330     * @param string $dbuser The database user to connect as.
331     * @param string $dbpass The password to use when connecting to the database.
332     * @param string $dbname The name of the database being connected to.
333     * @param array $dboptions An array of optional database options (eg: dbport)
334     *
335     * @return bool success True for successful connection. False otherwise.
336     */
337    public function create_database($dbhost, $dbuser, $dbpass, $dbname, array $dboptions=null) {
338        return false;
339    }
340
341    /**
342     * Returns transaction trace for debugging purposes.
343     * @private to be used by core only
344     * @return array or null if not in transaction.
345     */
346    public function get_transaction_start_backtrace() {
347        if (!$this->transactions) {
348            return null;
349        }
350        $lowesttransaction = end($this->transactions);
351        return $lowesttransaction->get_backtrace();
352    }
353
354    /**
355     * Closes the database connection and releases all resources
356     * and memory (especially circular memory references).
357     * Do NOT use connect() again, create a new instance if needed.
358     * @return void
359     */
360    public function dispose() {
361        if ($this->disposed) {
362            return;
363        }
364        $this->disposed = true;
365        if ($this->transactions) {
366            $this->force_transaction_rollback();
367        }
368
369        if ($this->temptables) {
370            $this->temptables->dispose();
371            $this->temptables = null;
372        }
373        if ($this->database_manager) {
374            $this->database_manager->dispose();
375            $this->database_manager = null;
376        }
377        $this->tables  = null;
378    }
379
380    /**
381     * This should be called before each db query.
382     * @param string $sql The query string.
383     * @param array $params An array of parameters.
384     * @param int $type The type of query. ( SQL_QUERY_SELECT | SQL_QUERY_AUX | SQL_QUERY_INSERT | SQL_QUERY_UPDATE | SQL_QUERY_STRUCTURE )
385     * @param mixed $extrainfo This is here for any driver specific extra information.
386     * @return void
387     */
388    protected function query_start($sql, array $params=null, $type, $extrainfo=null) {
389        if ($this->loggingquery) {
390            return;
391        }
392        $this->last_sql       = $sql;
393        $this->last_params    = $params;
394        $this->last_type      = $type;
395        $this->last_extrainfo = $extrainfo;
396        $this->last_time      = microtime(true);
397
398        switch ($type) {
399            case SQL_QUERY_SELECT:
400            case SQL_QUERY_AUX:
401                $this->reads++;
402                break;
403            case SQL_QUERY_INSERT:
404            case SQL_QUERY_UPDATE:
405            case SQL_QUERY_STRUCTURE:
406                $this->writes++;
407        }
408
409        $this->print_debug($sql, $params);
410    }
411
412    /**
413     * This should be called immediately after each db query. It does a clean up of resources.
414     * It also throws exceptions if the sql that ran produced errors.
415     * @param mixed $result The db specific result obtained from running a query.
416     * @throws dml_read_exception | dml_write_exception | ddl_change_structure_exception
417     * @return void
418     */
419    protected function query_end($result) {
420        if ($this->loggingquery) {
421            return;
422        }
423        if ($result !== false) {
424            $this->query_log();
425            // free memory
426            $this->last_sql    = null;
427            $this->last_params = null;
428            $this->print_debug_time();
429            return;
430        }
431
432        // remember current info, log queries may alter it
433        $type   = $this->last_type;
434        $sql    = $this->last_sql;
435        $params = $this->last_params;
436        $error  = $this->get_last_error();
437
438        $this->query_log($error);
439
440        switch ($type) {
441            case SQL_QUERY_SELECT:
442            case SQL_QUERY_AUX:
443                throw new dml_read_exception($error, $sql, $params);
444            case SQL_QUERY_INSERT:
445            case SQL_QUERY_UPDATE:
446                throw new dml_write_exception($error, $sql, $params);
447            case SQL_QUERY_STRUCTURE:
448                $this->get_manager(); // includes ddl exceptions classes ;-)
449                throw new ddl_change_structure_exception($error, $sql);
450        }
451    }
452
453    /**
454     * This logs the last query based on 'logall', 'logslow' and 'logerrors' options configured via $CFG->dboptions .
455     * @param string|bool $error or false if not error
456     * @return void
457     */
458    public function query_log($error=false) {
459        $logall    = !empty($this->dboptions['logall']);
460        $logslow   = !empty($this->dboptions['logslow']) ? $this->dboptions['logslow'] : false;
461        $logerrors = !empty($this->dboptions['logerrors']);
462        $iserror   = ($error !== false);
463
464        $time = $this->query_time();
465
466        // Will be shown or not depending on MDL_PERF values rather than in dboptions['log*].
467        $this->queriestime = $this->queriestime + $time;
468
469        if ($logall or ($logslow and ($logslow < ($time+0.00001))) or ($iserror and $logerrors)) {
470            $this->loggingquery = true;
471            try {
472                $backtrace = debug_backtrace();
473                if ($backtrace) {
474                    //remove query_log()
475                    array_shift($backtrace);
476                }
477                if ($backtrace) {
478                    //remove query_end()
479                    array_shift($backtrace);
480                }
481                $log = new stdClass();
482                $log->qtype      = $this->last_type;
483                $log->sqltext    = $this->last_sql;
484                $log->sqlparams  = var_export((array)$this->last_params, true);
485                $log->error      = (int)$iserror;
486                $log->info       = $iserror ? $error : null;
487                $log->backtrace  = format_backtrace($backtrace, true);
488                $log->exectime   = $time;
489                $log->timelogged = time();
490                $this->insert_record('log_queries', $log);
491            } catch (Exception $ignored) {
492            }
493            $this->loggingquery = false;
494        }
495    }
496
497    /**
498     * Returns the time elapsed since the query started.
499     * @return float Seconds with microseconds
500     */
501    protected function query_time() {
502        return microtime(true) - $this->last_time;
503    }
504
505    /**
506     * Returns database server info array
507     * @return array Array containing 'description' and 'version' at least.
508     */
509    public abstract function get_server_info();
510
511    /**
512     * Returns supported query parameter types
513     * @return int bitmask of accepted SQL_PARAMS_*
514     */
515    protected abstract function allowed_param_types();
516
517    /**
518     * Returns the last error reported by the database engine.
519     * @return string The error message.
520     */
521    public abstract function get_last_error();
522
523    /**
524     * Prints sql debug info
525     * @param string $sql The query which is being debugged.
526     * @param array $params The query parameters. (optional)
527     * @param mixed $obj The library specific object. (optional)
528     * @return void
529     */
530    protected function print_debug($sql, array $params=null, $obj=null) {
531        if (!$this->get_debug()) {
532            return;
533        }
534        if (CLI_SCRIPT) {
535            echo "--------------------------------\n";
536            echo $sql."\n";
537            if (!is_null($params)) {
538                echo "[".var_export($params, true)."]\n";
539            }
540            echo "--------------------------------\n";
541        } else {
542            echo "<hr />\n";
543            echo s($sql)."\n";
544            if (!is_null($params)) {
545                echo "[".s(var_export($params, true))."]\n";
546            }
547            echo "<hr />\n";
548        }
549    }
550
551    /**
552     * Prints the time a query took to run.
553     * @return void
554     */
555    protected function print_debug_time() {
556        if (!$this->get_debug()) {
557            return;
558        }
559        $time = $this->query_time();
560        $message = "Query took: {$time} seconds.\n";
561        if (CLI_SCRIPT) {
562            echo $message;
563            echo "--------------------------------\n";
564        } else {
565            echo s($message);
566            echo "<hr />\n";
567        }
568    }
569
570    /**
571     * Returns the SQL WHERE conditions.
572     * @param string $table The table name that these conditions will be validated against.
573     * @param array $conditions The conditions to build the where clause. (must not contain numeric indexes)
574     * @throws dml_exception
575     * @return array An array list containing sql 'where' part and 'params'.
576     */
577    protected function where_clause($table, array $conditions=null) {
578        // We accept nulls in conditions
579        $conditions = is_null($conditions) ? array() : $conditions;
580
581        if (empty($conditions)) {
582            return array('', array());
583        }
584
585        // Some checks performed under debugging only
586        if (debugging()) {
587            $columns = $this->get_columns($table);
588            if (empty($columns)) {
589                // no supported columns means most probably table does not exist
590                throw new dml_exception('ddltablenotexist', $table);
591            }
592            foreach ($conditions as $key=>$value) {
593                if (!isset($columns[$key])) {
594                    $a = new stdClass();
595                    $a->fieldname = $key;
596                    $a->tablename = $table;
597                    throw new dml_exception('ddlfieldnotexist', $a);
598                }
599                $column = $columns[$key];
600                if ($column->meta_type == 'X') {
601                    //ok so the column is a text column. sorry no text columns in the where clause conditions
602                    throw new dml_exception('textconditionsnotallowed', $conditions);
603                }
604            }
605        }
606
607        $allowed_types = $this->allowed_param_types();
608        $where = array();
609        $params = array();
610
611        foreach ($conditions as $key=>$value) {
612            if (is_int($key)) {
613                throw new dml_exception('invalidnumkey');
614            }
615            if (is_null($value)) {
616                $where[] = "$key IS NULL";
617            } else {
618                if ($allowed_types & SQL_PARAMS_NAMED) {
619                    // Need to verify key names because they can contain, originally,
620                    // spaces and other forbidden chars when using sql_xxx() functions and friends.
621                    $normkey = trim(preg_replace('/[^a-zA-Z0-9_-]/', '_', $key), '-_');
622                    if ($normkey !== $key) {
623                        debugging('Invalid key found in the conditions array.');
624                    }
625                    $where[] = "$key = :$normkey";
626                    $params[$normkey] = $value;
627                } else {
628                    $where[] = "$key = ?";
629                    $params[] = $value;
630                }
631            }
632        }
633        $where = implode(" AND ", $where);
634        return array($where, $params);
635    }
636
637    /**
638     * Returns SQL WHERE conditions for the ..._list group of methods.
639     *
640     * @param string $field the name of a field.
641     * @param array $values the values field might take.
642     * @return array An array containing sql 'where' part and 'params'
643     */
644    protected function where_clause_list($field, array $values) {
645        if (empty($values)) {
646            return array("1 = 2", array()); // Fake condition, won't return rows ever. MDL-17645
647        }
648
649        // Note: Do not use get_in_or_equal() because it can not deal with bools and nulls.
650
651        $params = array();
652        $select = "";
653        $values = (array)$values;
654        foreach ($values as $value) {
655            if (is_bool($value)) {
656                $value = (int)$value;
657            }
658            if (is_null($value)) {
659                $select = "$field IS NULL";
660            } else {
661                $params[] = $value;
662            }
663        }
664        if ($params) {
665            if ($select !== "") {
666                $select = "$select OR ";
667            }
668            $count = count($params);
669            if ($count == 1) {
670                $select = $select."$field = ?";
671            } else {
672                $qs = str_repeat(',?', $count);
673                $qs = ltrim($qs, ',');
674                $select = $select."$field IN ($qs)";
675            }
676        }
677        return array($select, $params);
678    }
679
680    /**
681     * Constructs 'IN()' or '=' sql fragment
682     * @param mixed $items A single value or array of values for the expression.
683     * @param int $type Parameter bounding type : SQL_PARAMS_QM or SQL_PARAMS_NAMED.
684     * @param string $prefix Named parameter placeholder prefix (a unique counter value is appended to each parameter name).
685     * @param bool $equal True means we want to equate to the constructed expression, false means we don't want to equate to it.
686     * @param mixed $onemptyitems This defines the behavior when the array of items provided is empty. Defaults to false,
687     *              meaning throw exceptions. Other values will become part of the returned SQL fragment.
688     * @throws coding_exception | dml_exception
689     * @return array A list containing the constructed sql fragment and an array of parameters.
690     */
691    public function get_in_or_equal($items, $type=SQL_PARAMS_QM, $prefix='param', $equal=true, $onemptyitems=false) {
692
693        // default behavior, throw exception on empty array
694        if (is_array($items) and empty($items) and $onemptyitems === false) {
695            throw new coding_exception('moodle_database::get_in_or_equal() does not accept empty arrays');
696        }
697        // handle $onemptyitems on empty array of items
698        if (is_array($items) and empty($items)) {
699            if (is_null($onemptyitems)) {             // Special case, NULL value
700                $sql = $equal ? ' IS NULL' : ' IS NOT NULL';
701                return (array($sql, array()));
702            } else {
703                $items = array($onemptyitems);        // Rest of cases, prepare $items for std processing
704            }
705        }
706
707        if ($type == SQL_PARAMS_QM) {
708            if (!is_array($items) or count($items) == 1) {
709                $sql = $equal ? '= ?' : '<> ?';
710                $items = (array)$items;
711                $params = array_values($items);
712            } else {
713                if ($equal) {
714                    $sql = 'IN ('.implode(',', array_fill(0, count($items), '?')).')';
715                } else {
716                    $sql = 'NOT IN ('.implode(',', array_fill(0, count($items), '?')).')';
717                }
718                $params = array_values($items);
719            }
720
721        } else if ($type == SQL_PARAMS_NAMED) {
722            if (empty($prefix)) {
723                $prefix = 'param';
724            }
725
726            if (!is_array($items)){
727                $param = $prefix.$this->inorequaluniqueindex++;
728                $sql = $equal ? "= :$param" : "<> :$param";
729                $params = array($param=>$items);
730            } else if (count($items) == 1) {
731                $param = $prefix.$this->inorequaluniqueindex++;
732                $sql = $equal ? "= :$param" : "<> :$param";
733                $item = reset($items);
734                $params = array($param=>$item);
735            } else {
736                $params = array();
737                $sql = array();
738                foreach ($items as $item) {
739                    $param = $prefix.$this->inorequaluniqueindex++;
740                    $params[$param] = $item;
741                    $sql[] = ':'.$param;
742                }
743                if ($equal) {
744                    $sql = 'IN ('.implode(',', $sql).')';
745                } else {
746                    $sql = 'NOT IN ('.implode(',', $sql).')';
747                }
748            }
749
750        } else {
751            throw new dml_exception('typenotimplement');
752        }
753        return array($sql, $params);
754    }
755
756    /**
757     * Converts short table name {tablename} to the real prefixed table name in given sql.
758     * @param string $sql The sql to be operated on.
759     * @return string The sql with tablenames being prefixed with $CFG->prefix
760     */
761    protected function fix_table_names($sql) {
762        return preg_replace('/\{([a-z][a-z0-9_]*)\}/', $this->prefix.'$1', $sql);
763    }
764
765    /**
766     * Internal private utitlity function used to fix parameters.
767     * Used with {@link preg_replace_callback()}
768     * @param array $match Refer to preg_replace_callback usage for description.
769     * @return string
770     */
771    private function _fix_sql_params_dollar_callback($match) {
772        $this->fix_sql_params_i++;
773        return "\$".$this->fix_sql_params_i;
774    }
775
776    /**
777     * Detects object parameters and throws exception if found
778     * @param mixed $value
779     * @return void
780     * @throws coding_exception if object detected
781     */
782    protected function detect_objects($value) {
783        if (is_object($value)) {
784            throw new coding_exception('Invalid database query parameter value', 'Objects are are not allowed: '.get_class($value));
785        }
786    }
787
788    /**
789     * Normalizes sql query parameters and verifies parameters.
790     * @param string $sql The query or part of it.
791     * @param array $params The query parameters.
792     * @return array (sql, params, type of params)
793     */
794    public function fix_sql_params($sql, array $params=null) {
795        $params = (array)$params; // mke null array if needed
796        $allowed_types = $this->allowed_param_types();
797
798        // convert table names
799        $sql = $this->fix_table_names($sql);
800
801        // cast booleans to 1/0 int and detect forbidden objects
802        foreach ($params as $key => $value) {
803            $this->detect_objects($value);
804            $params[$key] = is_bool($value) ? (int)$value : $value;
805        }
806
807        // NICOLAS C: Fixed regexp for negative backwards look-ahead of double colons. Thanks for Sam Marshall's help
808        $named_count = preg_match_all('/(?<!:):[a-z][a-z0-9_]*/', $sql, $named_matches); // :: used in pgsql casts
809        $dollar_count = preg_match_all('/\$[1-9][0-9]*/', $sql, $dollar_matches);
810        $q_count     = substr_count($sql, '?');
811
812        $count = 0;
813
814        if ($named_count) {
815            $type = SQL_PARAMS_NAMED;
816            $count = $named_count;
817
818        }
819        if ($dollar_count) {
820            if ($count) {
821                throw new dml_exception('mixedtypesqlparam');
822            }
823            $type = SQL_PARAMS_DOLLAR;
824            $count = $dollar_count;
825
826        }
827        if ($q_count) {
828            if ($count) {
829                throw new dml_exception('mixedtypesqlparam');
830            }
831            $type = SQL_PARAMS_QM;
832            $count = $q_count;
833
834        }
835
836        if (!$count) {
837             // ignore params
838            if ($allowed_types & SQL_PARAMS_NAMED) {
839                return array($sql, array(), SQL_PARAMS_NAMED);
840            } else if ($allowed_types & SQL_PARAMS_QM) {
841                return array($sql, array(), SQL_PARAMS_QM);
842            } else {
843                return array($sql, array(), SQL_PARAMS_DOLLAR);
844            }
845        }
846
847        if ($count > count($params)) {
848            $a = new stdClass;
849            $a->expected = $count;
850            $a->actual = count($params);
851            throw new dml_exception('invalidqueryparam', $a);
852        }
853
854        $target_type = $allowed_types;
855
856        if ($type & $allowed_types) { // bitwise AND
857            if ($count == count($params)) {
858                if ($type == SQL_PARAMS_QM) {
859                    return array($sql, array_values($params), SQL_PARAMS_QM); // 0-based array required
860                } else {
861                    //better do the validation of names below
862                }
863            }
864            // needs some fixing or validation - there might be more params than needed
865            $target_type = $type;
866        }
867
868        if ($type == SQL_PARAMS_NAMED) {
869            $finalparams = array();
870            foreach ($named_matches[0] as $key) {
871                $key = trim($key, ':');
872                if (!array_key_exists($key, $params)) {
873                    throw new dml_exception('missingkeyinsql', $key, '');
874                }
875                if (strlen($key) > 30) {
876                    throw new coding_exception(
877                            "Placeholder names must be 30 characters or shorter. '" .
878                            $key . "' is too long.", $sql);
879                }
880                $finalparams[$key] = $params[$key];
881            }
882            if ($count != count($finalparams)) {
883                throw new dml_exception('duplicateparaminsql');
884            }
885
886            if ($target_type & SQL_PARAMS_QM) {
887                $sql = preg_replace('/(?<!:):[a-z][a-z0-9_]*/', '?', $sql);
888                return array($sql, array_values($finalparams), SQL_PARAMS_QM); // 0-based required
889            } else if ($target_type & SQL_PARAMS_NAMED) {
890                return array($sql, $finalparams, SQL_PARAMS_NAMED);
891            } else {  // $type & SQL_PARAMS_DOLLAR
892                //lambda-style functions eat memory - we use globals instead :-(
893                $this->fix_sql_params_i = 0;
894                $sql = preg_replace_callback('/(?<!:):[a-z][a-z0-9_]*/', array($this, '_fix_sql_params_dollar_callback'), $sql);
895                return array($sql, array_values($finalparams), SQL_PARAMS_DOLLAR); // 0-based required
896            }
897
898        } else if ($type == SQL_PARAMS_DOLLAR) {
899            if ($target_type & SQL_PARAMS_DOLLAR) {
900                return array($sql, array_values($params), SQL_PARAMS_DOLLAR); // 0-based required
901            } else if ($target_type & SQL_PARAMS_QM) {
902                $sql = preg_replace('/\$[0-9]+/', '?', $sql);
903                return array($sql, array_values($params), SQL_PARAMS_QM); // 0-based required
904            } else { //$target_type & SQL_PARAMS_NAMED
905                $sql = preg_replace('/\$([0-9]+)/', ':param\\1', $sql);
906                $finalparams = array();
907                foreach ($params as $key=>$param) {
908                    $key++;
909                    $finalparams['param'.$key] = $param;
910                }
911                return array($sql, $finalparams, SQL_PARAMS_NAMED);
912            }
913
914        } else { // $type == SQL_PARAMS_QM
915            if (count($params) != $count) {
916                $params = array_slice($params, 0, $count);
917            }
918
919            if ($target_type & SQL_PARAMS_QM) {
920                return array($sql, array_values($params), SQL_PARAMS_QM); // 0-based required
921            } else if ($target_type & SQL_PARAMS_NAMED) {
922                $finalparams = array();
923                $pname = 'param0';
924                $parts = explode('?', $sql);
925                $sql = array_shift($parts);
926                foreach ($parts as $part) {
927                    $param = array_shift($params);
928                    $pname++;
929                    $sql .= ':'.$pname.$part;
930                    $finalparams[$pname] = $param;
931                }
932                return array($sql, $finalparams, SQL_PARAMS_NAMED);
933            } else {  // $type & SQL_PARAMS_DOLLAR
934                //lambda-style functions eat memory - we use globals instead :-(
935                $this->fix_sql_params_i = 0;
936                $sql = preg_replace_callback('/\?/', array($this, '_fix_sql_params_dollar_callback'), $sql);
937                return array($sql, array_values($params), SQL_PARAMS_DOLLAR); // 0-based required
938            }
939        }
940    }
941
942    /**
943     * Ensures that limit params are numeric and positive integers, to be passed to the database.
944     * We explicitly treat null, '' and -1 as 0 in order to provide compatibility with how limit
945     * values have been passed historically.
946     *
947     * @param int $limitfrom Where to start results from
948     * @param int $limitnum How many results to return
949     * @return array Normalised limit params in array($limitfrom, $limitnum)
950     */
951    protected function normalise_limit_from_num($limitfrom, $limitnum) {
952        global $CFG;
953
954        // We explicilty treat these cases as 0.
955        if ($limitfrom === null || $limitfrom === '' || $limitfrom === -1) {
956            $limitfrom = 0;
957        }
958        if ($limitnum === null || $limitnum === '' || $limitnum === -1) {
959            $limitnum = 0;
960        }
961
962        if ($CFG->debugdeveloper) {
963            if (!is_numeric($limitfrom)) {
964                $strvalue = var_export($limitfrom, true);
965                debugging("Non-numeric limitfrom parameter detected: $strvalue, did you pass the correct arguments?",
966                    DEBUG_DEVELOPER);
967            } else if ($limitfrom < 0) {
968                debugging("Negative limitfrom parameter detected: $limitfrom, did you pass the correct arguments?",
969                    DEBUG_DEVELOPER);
970            }
971
972            if (!is_numeric($limitnum)) {
973                $strvalue = var_export($limitnum, true);
974                debugging("Non-numeric limitnum parameter detected: $strvalue, did you pass the correct arguments?",
975                    DEBUG_DEVELOPER);
976            } else if ($limitnum < 0) {
977                debugging("Negative limitnum parameter detected: $limitnum, did you pass the correct arguments?",
978                    DEBUG_DEVELOPER);
979            }
980        }
981
982        $limitfrom = (int)$limitfrom;
983        $limitnum  = (int)$limitnum;
984        $limitfrom = max(0, $limitfrom);
985        $limitnum  = max(0, $limitnum);
986
987        return array($limitfrom, $limitnum);
988    }
989
990    /**
991     * Return tables in database WITHOUT current prefix.
992     * @param bool $usecache if true, returns list of cached tables.
993     * @return array of table names in lowercase and without prefix
994     */
995    public abstract function get_tables($usecache=true);
996
997    /**
998     * Return table indexes - everything lowercased.
999     * @param string $table The table we want to get indexes from.
1000     * @return array An associative array of indexes containing 'unique' flag and 'columns' being indexed
1001     */
1002    public abstract function get_indexes($table);
1003
1004    /**
1005     * Returns detailed information about columns in table. This information is cached internally.
1006     * @param string $table The table's name.
1007     * @param bool $usecache Flag to use internal cacheing. The default is true.
1008     * @return array of database_column_info objects indexed with column names
1009     */
1010    public abstract function get_columns($table, $usecache=true);
1011
1012    /**
1013     * Normalise values based on varying RDBMS's dependencies (booleans, LOBs...)
1014     *
1015     * @param database_column_info $column column metadata corresponding with the value we are going to normalise
1016     * @param mixed $value value we are going to normalise
1017     * @return mixed the normalised value
1018     */
1019    protected abstract function normalise_value($column, $value);
1020
1021    /**
1022     * Resets the internal column details cache
1023     * @return void
1024     */
1025    public function reset_caches() {
1026        $this->tables = null;
1027        // Purge MUC as well
1028        $identifiers = array('dbfamily' => $this->get_dbfamily(), 'settings' => $this->get_settings_hash());
1029        cache_helper::purge_by_definition('core', 'databasemeta', $identifiers);
1030    }
1031
1032    /**
1033     * Returns the sql generator used for db manipulation.
1034     * Used mostly in upgrade.php scripts.
1035     * @return database_manager The instance used to perform ddl operations.
1036     * @see lib/ddl/database_manager.php
1037     */
1038    public function get_manager() {
1039        global $CFG;
1040
1041        if (!$this->database_manager) {
1042            require_once($CFG->libdir.'/ddllib.php');
1043
1044            $classname = $this->get_dbfamily().'_sql_generator';
1045            require_once("$CFG->libdir/ddl/$classname.php");
1046            $generator = new $classname($this, $this->temptables);
1047
1048            $this->database_manager = new database_manager($this, $generator);
1049        }
1050        return $this->database_manager;
1051    }
1052
1053    /**
1054     * Attempts to change db encoding to UTF-8 encoding if possible.
1055     * @return bool True is successful.
1056     */
1057    public function change_db_encoding() {
1058        return false;
1059    }
1060
1061    /**
1062     * Checks to see if the database is in unicode mode?
1063     * @return bool
1064     */
1065    public function setup_is_unicodedb() {
1066        return true;
1067    }
1068
1069    /**
1070     * Enable/disable very detailed debugging.
1071     * @param bool $state
1072     * @return void
1073     */
1074    public function set_debug($state) {
1075        $this->debug = $state;
1076    }
1077
1078    /**
1079     * Returns debug status
1080     * @return bool $state
1081     */
1082    public function get_debug() {
1083        return $this->debug;
1084    }
1085
1086    /**
1087     * Enable/disable detailed sql logging
1088     *
1089     * @deprecated since Moodle 2.9
1090     * @todo MDL-49824 This will be deleted in Moodle 3.1.
1091     * @param bool $state
1092     */
1093    public function set_logging($state) {
1094        debugging('set_logging() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
1095    }
1096
1097    /**
1098     * Do NOT use in code, this is for use by database_manager only!
1099     * @param string|array $sql query or array of queries
1100     * @return bool true
1101     * @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
1102     */
1103    public abstract function change_database_structure($sql);
1104
1105    /**
1106     * Executes a general sql query. Should be used only when no other method suitable.
1107     * Do NOT use this to make changes in db structure, use database_manager methods instead!
1108     * @param string $sql query
1109     * @param array $params query parameters
1110     * @return bool true
1111     * @throws dml_exception A DML specific exception is thrown for any errors.
1112     */
1113    public abstract function execute($sql, array $params=null);
1114
1115    /**
1116     * Get a number of records as a moodle_recordset where all the given conditions met.
1117     *
1118     * Selects records from the table $table.
1119     *
1120     * If specified, only records meeting $conditions.
1121     *
1122     * If specified, the results will be sorted as specified by $sort. This
1123     * is added to the SQL as "ORDER BY $sort". Example values of $sort
1124     * might be "time ASC" or "time DESC".
1125     *
1126     * If $fields is specified, only those fields are returned.
1127     *
1128     * Since this method is a little less readable, use of it should be restricted to
1129     * code where it's possible there might be large datasets being returned.  For known
1130     * small datasets use get_records - it leads to simpler code.
1131     *
1132     * If you only want some of the records, specify $limitfrom and $limitnum.
1133     * The query will skip the first $limitfrom records (according to the sort
1134     * order) and then return the next $limitnum records. If either of $limitfrom
1135     * or $limitnum is specified, both must be present.
1136     *
1137     * The return value is a moodle_recordset
1138     * if the query succeeds. If an error occurs, false is returned.
1139     *
1140     * @param string $table the table to query.
1141     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1142     * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter).
1143     * @param string $fields a comma separated list of fields to return (optional, by default all fields are returned).
1144     * @param int $limitfrom return a subset of records, starting at this point (optional).
1145     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1146     * @return moodle_recordset A moodle_recordset instance
1147     * @throws dml_exception A DML specific exception is thrown for any errors.
1148     */
1149    public function get_recordset($table, array $conditions=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1150        list($select, $params) = $this->where_clause($table, $conditions);
1151        return $this->get_recordset_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum);
1152    }
1153
1154    /**
1155     * Get a number of records as a moodle_recordset where one field match one list of values.
1156     *
1157     * Only records where $field takes one of the values $values are returned.
1158     * $values must be an array of values.
1159     *
1160     * Other arguments and the return type are like {@link function get_recordset}.
1161     *
1162     * @param string $table the table to query.
1163     * @param string $field a field to check (optional).
1164     * @param array $values array of values the field must have
1165     * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter).
1166     * @param string $fields a comma separated list of fields to return (optional, by default all fields are returned).
1167     * @param int $limitfrom return a subset of records, starting at this point (optional).
1168     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1169     * @return moodle_recordset A moodle_recordset instance.
1170     * @throws dml_exception A DML specific exception is thrown for any errors.
1171     */
1172    public function get_recordset_list($table, $field, array $values, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1173        list($select, $params) = $this->where_clause_list($field, $values);
1174        return $this->get_recordset_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum);
1175    }
1176
1177    /**
1178     * Get a number of records as a moodle_recordset which match a particular WHERE clause.
1179     *
1180     * If given, $select is used as the SELECT parameter in the SQL query,
1181     * otherwise all records from the table are returned.
1182     *
1183     * Other arguments and the return type are like {@link function get_recordset}.
1184     *
1185     * @param string $table the table to query.
1186     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1187     * @param array $params array of sql parameters
1188     * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter).
1189     * @param string $fields a comma separated list of fields to return (optional, by default all fields are returned).
1190     * @param int $limitfrom return a subset of records, starting at this point (optional).
1191     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1192     * @return moodle_recordset A moodle_recordset instance.
1193     * @throws dml_exception A DML specific exception is thrown for any errors.
1194     */
1195    public function get_recordset_select($table, $select, array $params=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1196        $sql = "SELECT $fields FROM {".$table."}";
1197        if ($select) {
1198            $sql .= " WHERE $select";
1199        }
1200        if ($sort) {
1201            $sql .= " ORDER BY $sort";
1202        }
1203        return $this->get_recordset_sql($sql, $params, $limitfrom, $limitnum);
1204    }
1205
1206    /**
1207     * Get a number of records as a moodle_recordset using a SQL statement.
1208     *
1209     * Since this method is a little less readable, use of it should be restricted to
1210     * code where it's possible there might be large datasets being returned.  For known
1211     * small datasets use get_records_sql - it leads to simpler code.
1212     *
1213     * The return type is like {@link function get_recordset}.
1214     *
1215     * @param string $sql the SQL select query to execute.
1216     * @param array $params array of sql parameters
1217     * @param int $limitfrom return a subset of records, starting at this point (optional).
1218     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1219     * @return moodle_recordset A moodle_recordset instance.
1220     * @throws dml_exception A DML specific exception is thrown for any errors.
1221     */
1222    public abstract function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0);
1223
1224    /**
1225     * Get all records from a table.
1226     *
1227     * This method works around potential memory problems and may improve performance,
1228     * this method may block access to table until the recordset is closed.
1229     *
1230     * @param string $table Name of database table.
1231     * @return moodle_recordset A moodle_recordset instance {@link function get_recordset}.
1232     * @throws dml_exception A DML specific exception is thrown for any errors.
1233     */
1234    public function export_table_recordset($table) {
1235        return $this->get_recordset($table, array());
1236    }
1237
1238    /**
1239     * Get a number of records as an array of objects where all the given conditions met.
1240     *
1241     * If the query succeeds and returns at least one record, the
1242     * return value is an array of objects, one object for each
1243     * record found. The array key is the value from the first
1244     * column of the result set. The object associated with that key
1245     * has a member variable for each column of the results.
1246     *
1247     * @param string $table the table to query.
1248     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1249     * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter).
1250     * @param string $fields a comma separated list of fields to return (optional, by default
1251     *   all fields are returned). The first field will be used as key for the
1252     *   array so must be a unique field such as 'id'.
1253     * @param int $limitfrom return a subset of records, starting at this point (optional).
1254     * @param int $limitnum return a subset comprising this many records in total (optional, required if $limitfrom is set).
1255     * @return array An array of Objects indexed by first column.
1256     * @throws dml_exception A DML specific exception is thrown for any errors.
1257     */
1258    public function get_records($table, array $conditions=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1259        list($select, $params) = $this->where_clause($table, $conditions);
1260        return $this->get_records_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum);
1261    }
1262
1263    /**
1264     * Get a number of records as an array of objects where one field match one list of values.
1265     *
1266     * Return value is like {@link function get_records}.
1267     *
1268     * @param string $table The database table to be checked against.
1269     * @param string $field The field to search
1270     * @param array $values An array of values
1271     * @param string $sort Sort order (as valid SQL sort parameter)
1272     * @param string $fields A comma separated list of fields to be returned from the chosen table. If specified,
1273     *   the first field should be a unique one such as 'id' since it will be used as a key in the associative
1274     *   array.
1275     * @param int $limitfrom return a subset of records, starting at this point (optional).
1276     * @param int $limitnum return a subset comprising this many records in total (optional).
1277     * @return array An array of objects indexed by first column
1278     * @throws dml_exception A DML specific exception is thrown for any errors.
1279     */
1280    public function get_records_list($table, $field, array $values, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1281        list($select, $params) = $this->where_clause_list($field, $values);
1282        return $this->get_records_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum);
1283    }
1284
1285    /**
1286     * Get a number of records as an array of objects which match a particular WHERE clause.
1287     *
1288     * Return value is like {@link function get_records}.
1289     *
1290     * @param string $table The table to query.
1291     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1292     * @param array $params An array of sql parameters
1293     * @param string $sort An order to sort the results in (optional, a valid SQL ORDER BY parameter).
1294     * @param string $fields A comma separated list of fields to return
1295     *   (optional, by default all fields are returned). The first field will be used as key for the
1296     *   array so must be a unique field such as 'id'.
1297     * @param int $limitfrom return a subset of records, starting at this point (optional).
1298     * @param int $limitnum return a subset comprising this many records in total (optional, required if $limitfrom is set).
1299     * @return array of objects indexed by first column
1300     * @throws dml_exception A DML specific exception is thrown for any errors.
1301     */
1302    public function get_records_select($table, $select, array $params=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1303        if ($select) {
1304            $select = "WHERE $select";
1305        }
1306        if ($sort) {
1307            $sort = " ORDER BY $sort";
1308        }
1309        return $this->get_records_sql("SELECT $fields FROM {" . $table . "} $select $sort", $params, $limitfrom, $limitnum);
1310    }
1311
1312    /**
1313     * Get a number of records as an array of objects using a SQL statement.
1314     *
1315     * Return value is like {@link function get_records}.
1316     *
1317     * @param string $sql the SQL select query to execute. The first column of this SELECT statement
1318     *   must be a unique value (usually the 'id' field), as it will be used as the key of the
1319     *   returned array.
1320     * @param array $params array of sql parameters
1321     * @param int $limitfrom return a subset of records, starting at this point (optional).
1322     * @param int $limitnum return a subset comprising this many records in total (optional, required if $limitfrom is set).
1323     * @return array of objects indexed by first column
1324     * @throws dml_exception A DML specific exception is thrown for any errors.
1325     */
1326    public abstract function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0);
1327
1328    /**
1329     * Get the first two columns from a number of records as an associative array where all the given conditions met.
1330     *
1331     * Arguments are like {@link function get_recordset}.
1332     *
1333     * If no errors occur the return value
1334     * is an associative whose keys come from the first field of each record,
1335     * and whose values are the corresponding second fields.
1336     * False is returned if an error occurs.
1337     *
1338     * @param string $table the table to query.
1339     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1340     * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter).
1341     * @param string $fields a comma separated list of fields to return - the number of fields should be 2!
1342     * @param int $limitfrom return a subset of records, starting at this point (optional).
1343     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1344     * @return array an associative array
1345     * @throws dml_exception A DML specific exception is thrown for any errors.
1346     */
1347    public function get_records_menu($table, array $conditions=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1348        $menu = array();
1349        if ($records = $this->get_records($table, $conditions, $sort, $fields, $limitfrom, $limitnum)) {
1350            foreach ($records as $record) {
1351                $record = (array)$record;
1352                $key   = array_shift($record);
1353                $value = array_shift($record);
1354                $menu[$key] = $value;
1355            }
1356        }
1357        return $menu;
1358    }
1359
1360    /**
1361     * Get the first two columns from a number of records as an associative array which match a particular WHERE clause.
1362     *
1363     * Arguments are like {@link function get_recordset_select}.
1364     * Return value is like {@link function get_records_menu}.
1365     *
1366     * @param string $table The database table to be checked against.
1367     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1368     * @param array $params array of sql parameters
1369     * @param string $sort Sort order (optional) - a valid SQL order parameter
1370     * @param string $fields A comma separated list of fields to be returned from the chosen table - the number of fields should be 2!
1371     * @param int $limitfrom return a subset of records, starting at this point (optional).
1372     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1373     * @return array an associative array
1374     * @throws dml_exception A DML specific exception is thrown for any errors.
1375     */
1376    public function get_records_select_menu($table, $select, array $params=null, $sort='', $fields='*', $limitfrom=0, $limitnum=0) {
1377        $menu = array();
1378        if ($records = $this->get_records_select($table, $select, $params, $sort, $fields, $limitfrom, $limitnum)) {
1379            foreach ($records as $record) {
1380                $record = (array)$record;
1381                $key   = array_shift($record);
1382                $value = array_shift($record);
1383                $menu[$key] = $value;
1384            }
1385        }
1386        return $menu;
1387    }
1388
1389    /**
1390     * Get the first two columns from a number of records as an associative array using a SQL statement.
1391     *
1392     * Arguments are like {@link function get_recordset_sql}.
1393     * Return value is like {@link function get_records_menu}.
1394     *
1395     * @param string $sql The SQL string you wish to be executed.
1396     * @param array $params array of sql parameters
1397     * @param int $limitfrom return a subset of records, starting at this point (optional).
1398     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1399     * @return array an associative array
1400     * @throws dml_exception A DML specific exception is thrown for any errors.
1401     */
1402    public function get_records_sql_menu($sql, array $params=null, $limitfrom=0, $limitnum=0) {
1403        $menu = array();
1404        if ($records = $this->get_records_sql($sql, $params, $limitfrom, $limitnum)) {
1405            foreach ($records as $record) {
1406                $record = (array)$record;
1407                $key   = array_shift($record);
1408                $value = array_shift($record);
1409                $menu[$key] = $value;
1410            }
1411        }
1412        return $menu;
1413    }
1414
1415    /**
1416     * Get a single database record as an object where all the given conditions met.
1417     *
1418     * @param string $table The table to select from.
1419     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1420     * @param string $fields A comma separated list of fields to be returned from the chosen table.
1421     * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1422     *                        IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1423     *                        MUST_EXIST means we will throw an exception if no record or multiple records found.
1424     *
1425     * @todo MDL-30407 MUST_EXIST option should not throw a dml_exception, it should throw a different exception as it's a requested check.
1426     * @return mixed a fieldset object containing the first matching record, false or exception if error not found depending on mode
1427     * @throws dml_exception A DML specific exception is thrown for any errors.
1428     */
1429    public function get_record($table, array $conditions, $fields='*', $strictness=IGNORE_MISSING) {
1430        list($select, $params) = $this->where_clause($table, $conditions);
1431        return $this->get_record_select($table, $select, $params, $fields, $strictness);
1432    }
1433
1434    /**
1435     * Get a single database record as an object which match a particular WHERE clause.
1436     *
1437     * @param string $table The database table to be checked against.
1438     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1439     * @param array $params array of sql parameters
1440     * @param string $fields A comma separated list of fields to be returned from the chosen table.
1441     * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1442     *                        IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1443     *                        MUST_EXIST means throw exception if no record or multiple records found
1444     * @return stdClass|false a fieldset object containing the first matching record, false or exception if error not found depending on mode
1445     * @throws dml_exception A DML specific exception is thrown for any errors.
1446     */
1447    public function get_record_select($table, $select, array $params=null, $fields='*', $strictness=IGNORE_MISSING) {
1448        if ($select) {
1449            $select = "WHERE $select";
1450        }
1451        try {
1452            return $this->get_record_sql("SELECT $fields FROM {" . $table . "} $select", $params, $strictness);
1453        } catch (dml_missing_record_exception $e) {
1454            // create new exception which will contain correct table name
1455            throw new dml_missing_record_exception($table, $e->sql, $e->params);
1456        }
1457    }
1458
1459    /**
1460     * Get a single database record as an object using a SQL statement.
1461     *
1462     * The SQL statement should normally only return one record.
1463     * It is recommended to use get_records_sql() if more matches possible!
1464     *
1465     * @param string $sql The SQL string you wish to be executed, should normally only return one record.
1466     * @param array $params array of sql parameters
1467     * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1468     *                        IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1469     *                        MUST_EXIST means throw exception if no record or multiple records found
1470     * @return mixed a fieldset object containing the first matching record, false or exception if error not found depending on mode
1471     * @throws dml_exception A DML specific exception is thrown for any errors.
1472     */
1473    public function get_record_sql($sql, array $params=null, $strictness=IGNORE_MISSING) {
1474        $strictness = (int)$strictness; // we support true/false for BC reasons too
1475        if ($strictness == IGNORE_MULTIPLE) {
1476            $count = 1;
1477        } else {
1478            $count = 0;
1479        }
1480        if (!$records = $this->get_records_sql($sql, $params, 0, $count)) {
1481            // not found
1482            if ($strictness == MUST_EXIST) {
1483                throw new dml_missing_record_exception('', $sql, $params);
1484            }
1485            return false;
1486        }
1487
1488        if (count($records) > 1) {
1489            if ($strictness == MUST_EXIST) {
1490                throw new dml_multiple_records_exception($sql, $params);
1491            }
1492            debugging('Error: mdb->get_record() found more than one record!');
1493        }
1494
1495        $return = reset($records);
1496        return $return;
1497    }
1498
1499    /**
1500     * Get a single field value from a table record where all the given conditions met.
1501     *
1502     * @param string $table the table to query.
1503     * @param string $return the field to return the value of.
1504     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1505     * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1506     *                        IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1507     *                        MUST_EXIST means throw exception if no record or multiple records found
1508     * @return mixed the specified value false if not found
1509     * @throws dml_exception A DML specific exception is thrown for any errors.
1510     */
1511    public function get_field($table, $return, array $conditions, $strictness=IGNORE_MISSING) {
1512        list($select, $params) = $this->where_clause($table, $conditions);
1513        return $this->get_field_select($table, $return, $select, $params, $strictness);
1514    }
1515
1516    /**
1517     * Get a single field value from a table record which match a particular WHERE clause.
1518     *
1519     * @param string $table the table to query.
1520     * @param string $return the field to return the value of.
1521     * @param string $select A fragment of SQL to be used in a where clause returning one row with one column
1522     * @param array $params array of sql parameters
1523     * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1524     *                        IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1525     *                        MUST_EXIST means throw exception if no record or multiple records found
1526     * @return mixed the specified value false if not found
1527     * @throws dml_exception A DML specific exception is thrown for any errors.
1528     */
1529    public function get_field_select($table, $return, $select, array $params=null, $strictness=IGNORE_MISSING) {
1530        if ($select) {
1531            $select = "WHERE $select";
1532        }
1533        try {
1534            return $this->get_field_sql("SELECT $return FROM {" . $table . "} $select", $params, $strictness);
1535        } catch (dml_missing_record_exception $e) {
1536            // create new exception which will contain correct table name
1537            throw new dml_missing_record_exception($table, $e->sql, $e->params);
1538        }
1539    }
1540
1541    /**
1542     * Get a single field value (first field) using a SQL statement.
1543     *
1544     * @param string $sql The SQL query returning one row with one column
1545     * @param array $params array of sql parameters
1546     * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
1547     *                        IGNORE_MULTIPLE means return first, ignore multiple records found(not recommended);
1548     *                        MUST_EXIST means throw exception if no record or multiple records found
1549     * @return mixed the specified value false if not found
1550     * @throws dml_exception A DML specific exception is thrown for any errors.
1551     */
1552    public function get_field_sql($sql, array $params=null, $strictness=IGNORE_MISSING) {
1553        if (!$record = $this->get_record_sql($sql, $params, $strictness)) {
1554            return false;
1555        }
1556
1557        $record = (array)$record;
1558        return reset($record); // first column
1559    }
1560
1561    /**
1562     * Selects records and return values of chosen field as an array which match a particular WHERE clause.
1563     *
1564     * @param string $table the table to query.
1565     * @param string $return the field we are intered in
1566     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1567     * @param array $params array of sql parameters
1568     * @return array of values
1569     * @throws dml_exception A DML specific exception is thrown for any errors.
1570     */
1571    public function get_fieldset_select($table, $return, $select, array $params=null) {
1572        if ($select) {
1573            $select = "WHERE $select";
1574        }
1575        return $this->get_fieldset_sql("SELECT $return FROM {" . $table . "} $select", $params);
1576    }
1577
1578    /**
1579     * Selects records and return values (first field) as an array using a SQL statement.
1580     *
1581     * @param string $sql The SQL query
1582     * @param array $params array of sql parameters
1583     * @return array of values
1584     * @throws dml_exception A DML specific exception is thrown for any errors.
1585     */
1586    public abstract function get_fieldset_sql($sql, array $params=null);
1587
1588    /**
1589     * Insert new record into database, as fast as possible, no safety checks, lobs not supported.
1590     * @param string $table name
1591     * @param mixed $params data record as object or array
1592     * @param bool $returnid Returns id of inserted record.
1593     * @param bool $bulk true means repeated inserts expected
1594     * @param bool $customsequence true if 'id' included in $params, disables $returnid
1595     * @return bool|int true or new id
1596     * @throws dml_exception A DML specific exception is thrown for any errors.
1597     */
1598    public abstract function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false);
1599
1600    /**
1601     * Insert a record into a table and return the "id" field if required.
1602     *
1603     * Some conversions and safety checks are carried out. Lobs are supported.
1604     * If the return ID isn't required, then this just reports success as true/false.
1605     * $data is an object containing needed data
1606     * @param string $table The database table to be inserted into
1607     * @param object $dataobject A data object with values for one or more fields in the record
1608     * @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.
1609     * @param bool $bulk Set to true is multiple inserts are expected
1610     * @return bool|int true or new id
1611     * @throws dml_exception A DML specific exception is thrown for any errors.
1612     */
1613    public abstract function insert_record($table, $dataobject, $returnid=true, $bulk=false);
1614
1615    /**
1616     * Insert multiple records into database as fast as possible.
1617     *
1618     * Order of inserts is maintained, but the operation is not atomic,
1619     * use transactions if necessary.
1620     *
1621     * This method is intended for inserting of large number of small objects,
1622     * do not use for huge objects with text or binary fields.
1623     *
1624     * @since Moodle 2.7
1625     *
1626     * @param string $table  The database table to be inserted into
1627     * @param array|Traversable $dataobjects list of objects to be inserted, must be compatible with foreach
1628     * @return void does not return new record ids
1629     *
1630     * @throws coding_exception if data objects have different structure
1631     * @throws dml_exception A DML specific exception is thrown for any errors.
1632     */
1633    public function insert_records($table, $dataobjects) {
1634        if (!is_array($dataobjects) and !($dataobjects instanceof Traversable)) {
1635            throw new coding_exception('insert_records() passed non-traversable object');
1636        }
1637
1638        $fields = null;
1639        // Note: override in driver if there is a faster way.
1640        foreach ($dataobjects as $dataobject) {
1641            if (!is_array($dataobject) and !is_object($dataobject)) {
1642                throw new coding_exception('insert_records() passed invalid record object');
1643            }
1644            $dataobject = (array)$dataobject;
1645            if ($fields === null) {
1646                $fields = array_keys($dataobject);
1647            } else if ($fields !== array_keys($dataobject)) {
1648                throw new coding_exception('All dataobjects in insert_records() must have the same structure!');
1649            }
1650            $this->insert_record($table, $dataobject, false);
1651        }
1652    }
1653
1654    /**
1655     * Import a record into a table, id field is required.
1656     * Safety checks are NOT carried out. Lobs are supported.
1657     *
1658     * @param string $table name of database table to be inserted into
1659     * @param object $dataobject A data object with values for one or more fields in the record
1660     * @return bool true
1661     * @throws dml_exception A DML specific exception is thrown for any errors.
1662     */
1663    public abstract function import_record($table, $dataobject);
1664
1665    /**
1666     * Update record in database, as fast as possible, no safety checks, lobs not supported.
1667     * @param string $table name
1668     * @param mixed $params data record as object or array
1669     * @param bool $bulk True means repeated updates expected.
1670     * @return bool true
1671     * @throws dml_exception A DML specific exception is thrown for any errors.
1672     */
1673    public abstract function update_record_raw($table, $params, $bulk=false);
1674
1675    /**
1676     * Update a record in a table
1677     *
1678     * $dataobject is an object containing needed data
1679     * Relies on $dataobject having a variable "id" to
1680     * specify the record to update
1681     *
1682     * @param string $table The database table to be checked against.
1683     * @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
1684     * @param bool $bulk True means repeated updates expected.
1685     * @return bool true
1686     * @throws dml_exception A DML specific exception is thrown for any errors.
1687     */
1688    public abstract function update_record($table, $dataobject, $bulk=false);
1689
1690    /**
1691     * Set a single field in every table record where all the given conditions met.
1692     *
1693     * @param string $table The database table to be checked against.
1694     * @param string $newfield the field to set.
1695     * @param string $newvalue the value to set the field to.
1696     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1697     * @return bool true
1698     * @throws dml_exception A DML specific exception is thrown for any errors.
1699     */
1700    public function set_field($table, $newfield, $newvalue, array $conditions=null) {
1701        list($select, $params) = $this->where_clause($table, $conditions);
1702        return $this->set_field_select($table, $newfield, $newvalue, $select, $params);
1703    }
1704
1705    /**
1706     * Set a single field in every table record which match a particular WHERE clause.
1707     *
1708     * @param string $table The database table to be checked against.
1709     * @param string $newfield the field to set.
1710     * @param string $newvalue the value to set the field to.
1711     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1712     * @param array $params array of sql parameters
1713     * @return bool true
1714     * @throws dml_exception A DML specific exception is thrown for any errors.
1715     */
1716    public abstract function set_field_select($table, $newfield, $newvalue, $select, array $params=null);
1717
1718
1719    /**
1720     * Count the records in a table where all the given conditions met.
1721     *
1722     * @param string $table The table to query.
1723     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1724     * @return int The count of records returned from the specified criteria.
1725     * @throws dml_exception A DML specific exception is thrown for any errors.
1726     */
1727    public function count_records($table, array $conditions=null) {
1728        list($select, $params) = $this->where_clause($table, $conditions);
1729        return $this->count_records_select($table, $select, $params);
1730    }
1731
1732    /**
1733     * Count the records in a table which match a particular WHERE clause.
1734     *
1735     * @param string $table The database table to be checked against.
1736     * @param string $select A fragment of SQL to be used in a WHERE clause in the SQL call.
1737     * @param array $params array of sql parameters
1738     * @param string $countitem The count string to be used in the SQL call. Default is COUNT('x').
1739     * @return int The count of records returned from the specified criteria.
1740     * @throws dml_exception A DML specific exception is thrown for any errors.
1741     */
1742    public function count_records_select($table, $select, array $params=null, $countitem="COUNT('x')") {
1743        if ($select) {
1744            $select = "WHERE $select";
1745        }
1746        return $this->count_records_sql("SELECT $countitem FROM {" . $table . "} $select", $params);
1747    }
1748
1749    /**
1750     * Get the result of a SQL SELECT COUNT(...) query.
1751     *
1752     * Given a query that counts rows, return that count. (In fact,
1753     * given any query, return the first field of the first record
1754     * returned. However, this method should only be used for the
1755     * intended purpose.) If an error occurs, 0 is returned.
1756     *
1757     * @param string $sql The SQL string you wish to be executed.
1758     * @param array $params array of sql parameters
1759     * @return int the count
1760     * @throws dml_exception A DML specific exception is thrown for any errors.
1761     */
1762    public function count_records_sql($sql, array $params=null) {
1763        $count = $this->get_field_sql($sql, $params);
1764        if ($count === false or !is_number($count) or $count < 0) {
1765            throw new coding_exception("count_records_sql() expects the first field to contain non-negative number from COUNT(), '$count' found instead.");
1766        }
1767        return (int)$count;
1768    }
1769
1770    /**
1771     * Test whether a record exists in a table where all the given conditions met.
1772     *
1773     * @param string $table The table to check.
1774     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1775     * @return bool true if a matching record exists, else false.
1776     * @throws dml_exception A DML specific exception is thrown for any errors.
1777     */
1778    public function record_exists($table, array $conditions) {
1779        list($select, $params) = $this->where_clause($table, $conditions);
1780        return $this->record_exists_select($table, $select, $params);
1781    }
1782
1783    /**
1784     * Test whether any records exists in a table which match a particular WHERE clause.
1785     *
1786     * @param string $table The database table to be checked against.
1787     * @param string $select A fragment of SQL to be used in a WHERE clause in the SQL call.
1788     * @param array $params array of sql parameters
1789     * @return bool true if a matching record exists, else false.
1790     * @throws dml_exception A DML specific exception is thrown for any errors.
1791     */
1792    public function record_exists_select($table, $select, array $params=null) {
1793        if ($select) {
1794            $select = "WHERE $select";
1795        }
1796        return $this->record_exists_sql("SELECT 'x' FROM {" . $table . "} $select", $params);
1797    }
1798
1799    /**
1800     * Test whether a SQL SELECT statement returns any records.
1801     *
1802     * This function returns true if the SQL statement executes
1803     * without any errors and returns at least one record.
1804     *
1805     * @param string $sql The SQL statement to execute.
1806     * @param array $params array of sql parameters
1807     * @return bool true if the SQL executes without errors and returns at least one record.
1808     * @throws dml_exception A DML specific exception is thrown for any errors.
1809     */
1810    public function record_exists_sql($sql, array $params=null) {
1811        $mrs = $this->get_recordset_sql($sql, $params, 0, 1);
1812        $return = $mrs->valid();
1813        $mrs->close();
1814        return $return;
1815    }
1816
1817    /**
1818     * Delete the records from a table where all the given conditions met.
1819     * If conditions not specified, table is truncated.
1820     *
1821     * @param string $table the table to delete from.
1822     * @param array $conditions optional array $fieldname=>requestedvalue with AND in between
1823     * @return bool true.
1824     * @throws dml_exception A DML specific exception is thrown for any errors.
1825     */
1826    public function delete_records($table, array $conditions=null) {
1827        // truncate is drop/create (DDL), not transactional safe,
1828        // so we don't use the shortcut within them. MDL-29198
1829        if (is_null($conditions) && empty($this->transactions)) {
1830            return $this->execute("TRUNCATE TABLE {".$table."}");
1831        }
1832        list($select, $params) = $this->where_clause($table, $conditions);
1833        return $this->delete_records_select($table, $select, $params);
1834    }
1835
1836    /**
1837     * Delete the records from a table where one field match one list of values.
1838     *
1839     * @param string $table the table to delete from.
1840     * @param string $field The field to search
1841     * @param array $values array of values
1842     * @return bool true.
1843     * @throws dml_exception A DML specific exception is thrown for any errors.
1844     */
1845    public function delete_records_list($table, $field, array $values) {
1846        list($select, $params) = $this->where_clause_list($field, $values);
1847        return $this->delete_records_select($table, $select, $params);
1848    }
1849
1850    /**
1851     * Delete one or more records from a table which match a particular WHERE clause.
1852     *
1853     * @param string $table The database table to be checked against.
1854     * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria).
1855     * @param array $params array of sql parameters
1856     * @return bool true.
1857     * @throws dml_exception A DML specific exception is thrown for any errors.
1858     */
1859    public abstract function delete_records_select($table, $select, array $params=null);
1860
1861    /**
1862     * Returns the FROM clause required by some DBs in all SELECT statements.
1863     *
1864     * To be used in queries not having FROM clause to provide cross_db
1865     * Most DBs don't need it, hence the default is ''
1866     * @return string
1867     */
1868    public function sql_null_from_clause() {
1869        return '';
1870    }
1871
1872    /**
1873     * Returns the SQL text to be used in order to perform one bitwise AND operation
1874     * between 2 integers.
1875     *
1876     * NOTE: The SQL result is a number and can not be used directly in
1877     *       SQL condition, please compare it to some number to get a bool!!
1878     *
1879     * @param int $int1 First integer in the operation.
1880     * @param int $int2 Second integer in the operation.
1881     * @return string The piece of SQL code to be used in your statement.
1882     */
1883    public function sql_bitand($int1, $int2) {
1884        return '((' . $int1 . ') & (' . $int2 . '))';
1885    }
1886
1887    /**
1888     * Returns the SQL text to be used in order to perform one bitwise NOT operation
1889     * with 1 integer.
1890     *
1891     * @param int $int1 The operand integer in the operation.
1892     * @return string The piece of SQL code to be used in your statement.
1893     */
1894    public function sql_bitnot($int1) {
1895        return '(~(' . $int1 . '))';
1896    }
1897
1898    /**
1899     * Returns the SQL text to be used in order to perform one bitwise OR operation
1900     * between 2 integers.
1901     *
1902     * NOTE: The SQL result is a number and can not be used directly in
1903     *       SQL condition, please compare it to some number to get a bool!!
1904     *
1905     * @param int $int1 The first operand integer in the operation.
1906     * @param int $int2 The second operand integer in the operation.
1907     * @return string The piece of SQL code to be used in your statement.
1908     */
1909    public function sql_bitor($int1, $int2) {
1910        return '((' . $int1 . ') | (' . $int2 . '))';
1911    }
1912
1913    /**
1914     * Returns the SQL text to be used in order to perform one bitwise XOR operation
1915     * between 2 integers.
1916     *
1917     * NOTE: The SQL result is a number and can not be used directly in
1918     *       SQL condition, please compare it to some number to get a bool!!
1919     *
1920     * @param int $int1 The first operand integer in the operation.
1921     * @param int $int2 The second operand integer in the operation.
1922     * @return string The piece of SQL code to be used in your statement.
1923     */
1924    public function sql_bitxor($int1, $int2) {
1925        return '((' . $int1 . ') ^ (' . $int2 . '))';
1926    }
1927
1928    /**
1929     * Returns the SQL text to be used in order to perform module '%'
1930     * operation - remainder after division
1931     *
1932     * @param int $int1 The first operand integer in the operation.
1933     * @param int $int2 The second operand integer in the operation.
1934     * @return string The piece of SQL code to be used in your statement.
1935     */
1936    public function sql_modulo($int1, $int2) {
1937        return '((' . $int1 . ') % (' . $int2 . '))';
1938    }
1939
1940    /**
1941     * Returns the cross db correct CEIL (ceiling) expression applied to fieldname.
1942     * note: Most DBs use CEIL(), hence it's the default here.
1943     *
1944     * @param string $fieldname The field (or expression) we are going to ceil.
1945     * @return string The piece of SQL code to be used in your ceiling statement.
1946     */
1947    public function sql_ceil($fieldname) {
1948        return ' CEIL(' . $fieldname . ')';
1949    }
1950
1951    /**
1952     * Returns the SQL to be used in order to CAST one CHAR column to INTEGER.
1953     *
1954     * Be aware that the CHAR column you're trying to cast contains really
1955     * int values or the RDBMS will throw an error!
1956     *
1957     * @param string $fieldname The name of the field to be casted.
1958     * @param bool $text Specifies if the original column is one TEXT (CLOB) column (true). Defaults to false.
1959     * @return string The piece of SQL code to be used in your statement.
1960     */
1961    public function sql_cast_char2int($fieldname, $text=false) {
1962        return ' ' . $fieldname . ' ';
1963    }
1964
1965    /**
1966     * Returns the SQL to be used in order to CAST one CHAR column to REAL number.
1967     *
1968     * Be aware that the CHAR column you're trying to cast contains really
1969     * numbers or the RDBMS will throw an error!
1970     *
1971     * @param string $fieldname The name of the field to be casted.
1972     * @param bool $text Specifies if the original column is one TEXT (CLOB) column (true). Defaults to false.
1973     * @return string The piece of SQL code to be used in your statement.
1974     */
1975    public function sql_cast_char2real($fieldname, $text=false) {
1976        return ' ' . $fieldname . ' ';
1977    }
1978
1979    /**
1980     * Returns the SQL to be used in order to an UNSIGNED INTEGER column to SIGNED.
1981     *
1982     * (Only MySQL needs this. MySQL things that 1 * -1 = 18446744073709551615
1983     * if the 1 comes from an unsigned column).
1984     *
1985     * @deprecated since 2.3
1986     * @param string $fieldname The name of the field to be cast
1987     * @return string The piece of SQL code to be used in your statement.
1988     */
1989    public function sql_cast_2signed($fieldname) {
1990        return ' ' . $fieldname . ' ';
1991    }
1992
1993    /**
1994     * Returns the SQL text to be used to compare one TEXT (clob) column with
1995     * one varchar column, because some RDBMS doesn't support such direct
1996     * comparisons.
1997     *
1998     * @param string $fieldname The name of the TEXT field we need to order by
1999     * @param int $numchars Number of chars to use for the ordering (defaults to 32).
2000     * @return string The piece of SQL code to be used in your statement.
2001     */
2002    public function sql_compare_text($fieldname, $numchars=32) {
2003        return $this->sql_order_by_text($fieldname, $numchars);
2004    }
2005
2006    /**
2007     * Returns 'LIKE' part of a query.
2008     *
2009     * @param string $fieldname Usually the name of the table column.
2010     * @param string $param Usually the bound query parameter (?, :named).
2011     * @param bool $casesensitive Use case sensitive search when set to true (default).
2012     * @param bool $accentsensitive Use accent sensitive search when set to true (default). (not all databases support accent insensitive)
2013     * @param bool $notlike True means "NOT LIKE".
2014     * @param string $escapechar The escape char for '%' and '_'.
2015     * @return string The SQL code fragment.
2016     */
2017    public function sql_like($fieldname, $param, $casesensitive = true, $accentsensitive = true, $notlike = false, $escapechar = '\\') {
2018        if (strpos($param, '%') !== false) {
2019            debugging('Potential SQL injection detected, sql_like() expects bound parameters (? or :named)');
2020        }
2021        $LIKE = $notlike ? 'NOT LIKE' : 'LIKE';
2022        // by default ignore any sensitiveness - each database does it in a different way
2023        return "$fieldname $LIKE $param ESCAPE '$escapechar'";
2024    }
2025
2026    /**
2027     * Escape sql LIKE special characters like '_' or '%'.
2028     * @param string $text The string containing characters needing escaping.
2029     * @param string $escapechar The desired escape character, defaults to '\\'.
2030     * @return string The escaped sql LIKE string.
2031     */
2032    public function sql_like_escape($text, $escapechar = '\\') {
2033        $text = str_replace('_', $escapechar.'_', $text);
2034        $text = str_replace('%', $escapechar.'%', $text);
2035        return $text;
2036    }
2037
2038    /**
2039     * Returns the proper SQL to do CONCAT between the elements(fieldnames) passed.
2040     *
2041     * This function accepts variable number of string parameters.
2042     * All strings/fieldnames will used in the SQL concatenate statement generated.
2043     *
2044     * @return string The SQL to concatenate strings passed in.
2045     * @uses func_get_args()  and thus parameters are unlimited OPTIONAL number of additional field names.
2046     */
2047    public abstract function sql_concat();
2048
2049    /**
2050     * Returns the proper SQL to do CONCAT between the elements passed
2051     * with a given separator
2052     *
2053     * @param string $separator The separator desired for the SQL concatenating $elements.
2054     * @param array  $elements The array of strings to be concatenated.
2055     * @return string The SQL to concatenate the strings.
2056     */
2057    public abstract function sql_concat_join($separator="' '", $elements=array());
2058
2059    /**
2060     * Returns the proper SQL (for the dbms in use) to concatenate $firstname and $lastname
2061     *
2062     * @todo MDL-31233 This may not be needed here.
2063     *
2064     * @param string $first User's first name (default:'firstname').
2065     * @param string $last User's last name (default:'lastname').
2066     * @return string The SQL to concatenate strings.
2067     */
2068    function sql_fullname($first='firstname', $last='lastname') {
2069        return $this->sql_concat($first, "' '", $last);
2070    }
2071
2072    /**
2073     * Returns the SQL text to be used to order by one TEXT (clob) column, because
2074     * some RDBMS doesn't support direct ordering of such fields.
2075     *
2076     * Note that the use or queries being ordered by TEXT columns must be minimised,
2077     * because it's really slooooooow.
2078     *
2079     * @param string $fieldname The name of the TEXT field we need to order by.
2080     * @param int $numchars The number of chars to use for the ordering (defaults to 32).
2081     * @return string The piece of SQL code to be used in your statement.
2082     */
2083    public function sql_order_by_text($fieldname, $numchars=32) {
2084        return $fieldname;
2085    }
2086
2087    /**
2088     * Returns the SQL text to be used to calculate the length in characters of one expression.
2089     * @param string $fieldname The fieldname/expression to calculate its length in characters.
2090     * @return string the piece of SQL code to be used in the statement.
2091     */
2092    public function sql_length($fieldname) {
2093        return ' LENGTH(' . $fieldname . ')';
2094    }
2095
2096    /**
2097     * Returns the proper substr() SQL text used to extract substrings from DB
2098     * NOTE: this was originally returning only function name
2099     *
2100     * @param string $expr Some string field, no aggregates.
2101     * @param mixed $start Integer or expression evaluating to integer (1 based value; first char has index 1)
2102     * @param mixed $length Optional integer or expression evaluating to integer.
2103     * @return string The sql substring extraction fragment.
2104     */
2105    public function sql_substr($expr, $start, $length=false) {
2106        if (count(func_get_args()) < 2) {
2107            throw new coding_exception('moodle_database::sql_substr() requires at least two parameters', 'Originally this function was only returning name of SQL substring function, it now requires all parameters.');
2108        }
2109        if ($length === false) {
2110            return "SUBSTR($expr, $start)";
2111        } else {
2112            return "SUBSTR($expr, $start, $length)";
2113        }
2114    }
2115
2116    /**
2117     * Returns the SQL for returning searching one string for the location of another.
2118     *
2119     * Note, there is no guarantee which order $needle, $haystack will be in
2120     * the resulting SQL so when using this method, and both arguments contain
2121     * placeholders, you should use named placeholders.
2122     *
2123     * @param string $needle the SQL expression that will be searched for.
2124     * @param string $haystack the SQL expression that will be searched in.
2125     * @return string The required searching SQL part.
2126     */
2127    public function sql_position($needle, $haystack) {
2128        // Implementation using standard SQL.
2129        return "POSITION(($needle) IN ($haystack))";
2130    }
2131
2132    /**
2133     * This used to return empty string replacement character.
2134     *
2135     * @deprecated use bound parameter with empty string instead
2136     *
2137     * @return string An empty string.
2138     */
2139    function sql_empty() {
2140        debugging("sql_empty() is deprecated, please use empty string '' as sql parameter value instead", DEBUG_DEVELOPER);
2141        return '';
2142    }
2143
2144    /**
2145     * Returns the proper SQL to know if one field is empty.
2146     *
2147     * Note that the function behavior strongly relies on the
2148     * parameters passed describing the field so, please,  be accurate
2149     * when specifying them.
2150     *
2151     * Also, note that this function is not suitable to look for
2152     * fields having NULL contents at all. It's all for empty values!
2153     *
2154     * This function should be applied in all the places where conditions of
2155     * the type:
2156     *
2157     *     ... AND fieldname = '';
2158     *
2159     * are being used. Final result for text fields should be:
2160     *
2161     *     ... AND ' . sql_isempty('tablename', 'fieldname', true/false, true);
2162     *
2163     * and for varchar fields result should be:
2164     *
2165     *    ... AND fieldname = :empty; "; $params['empty'] = '';
2166     *
2167     * (see parameters description below)
2168     *
2169     * @param string $tablename Name of the table (without prefix). Not used for now but can be
2170     *                          necessary in the future if we want to use some introspection using
2171     *                          meta information against the DB. /// TODO ///
2172     * @param string $fieldname Name of the field we are going to check
2173     * @param bool $nullablefield For specifying if the field is nullable (true) or no (false) in the DB.
2174     * @param bool $textfield For specifying if it is a text (also called clob) field (true) or a varchar one (false)
2175     * @return string the sql code to be added to check for empty values
2176     */
2177    public function sql_isempty($tablename, $fieldname, $nullablefield, $textfield) {
2178        return " ($fieldname = '') ";
2179    }
2180
2181    /**
2182     * Returns the proper SQL to know if one field is not empty.
2183     *
2184     * Note that the function behavior strongly relies on the
2185     * parameters passed describing the field so, please,  be accurate
2186     * when specifying them.
2187     *
2188     * This function should be applied in all the places where conditions of
2189     * the type:
2190     *
2191     *     ... AND fieldname != '';
2192     *
2193     * are being used. Final result for text fields should be:
2194     *
2195     *     ... AND ' . sql_isnotempty('tablename', 'fieldname', true/false, true/false);
2196     *
2197     * and for varchar fields result should be:
2198     *
2199     *    ... AND fieldname != :empty; "; $params['empty'] = '';
2200     *
2201     * (see parameters description below)
2202     *
2203     * @param string $tablename Name of the table (without prefix). This is not used for now but can be
2204     *                          necessary in the future if we want to use some introspection using
2205     *                          meta information against the DB.
2206     * @param string $fieldname The name of the field we are going to check.
2207     * @param bool $nullablefield Specifies if the field is nullable (true) or not (false) in the DB.
2208     * @param bool $textfield Specifies if it is a text (also called clob) field (true) or a varchar one (false).
2209     * @return string The sql code to be added to check for non empty values.
2210     */
2211    public function sql_isnotempty($tablename, $fieldname, $nullablefield, $textfield) {
2212        return ' ( NOT ' . $this->sql_isempty($tablename, $fieldname, $nullablefield, $textfield) . ') ';
2213    }
2214
2215    /**
2216     * Returns true if this database driver supports regex syntax when searching.
2217     * @return bool True if supported.
2218     */
2219    public function sql_regex_supported() {
2220        return false;
2221    }
2222
2223    /**
2224     * Returns the driver specific syntax (SQL part) for matching regex positively or negatively (inverted matching).
2225     * Eg: 'REGEXP':'NOT REGEXP' or '~*' : '!~*'
2226     * @param bool $positivematch
2227     * @return string or empty if not supported
2228     */
2229    public function sql_regex($positivematch=true) {
2230        return '';
2231    }
2232
2233    /**
2234     * Returns the SQL that allows to find intersection of two or more queries
2235     *
2236     * @since Moodle 2.8
2237     *
2238     * @param array $selects array of SQL select queries, each of them only returns fields with the names from $fields
2239     * @param string $fields comma-separated list of fields (used only by some DB engines)
2240     * @return string SQL query that will return only values that are present in each of selects
2241     */
2242    public function sql_intersect($selects, $fields) {
2243        if (!count($selects)) {
2244            throw new coding_exception('sql_intersect() requires at least one element in $selects');
2245        } else if (count($selects) == 1) {
2246            return $selects[0];
2247        }
2248        static $aliascnt = 0;
2249        $rv = '('.$selects[0].')';
2250        for ($i = 1; $i < count($selects); $i++) {
2251            $rv .= " INTERSECT (".$selects[$i].')';
2252        }
2253        return $rv;
2254    }
2255
2256    /**
2257     * Does this driver support tool_replace?
2258     *
2259     * @since Moodle 2.6.1
2260     * @return bool
2261     */
2262    public function replace_all_text_supported() {
2263        return false;
2264    }
2265
2266    /**
2267     * Replace given text in all rows of column.
2268     *
2269     * @since Moodle 2.6.1
2270     * @param string $table name of the table
2271     * @param database_column_info $column
2272     * @param string $search
2273     * @param string $replace
2274     */
2275    public function replace_all_text($table, database_column_info $column, $search, $replace) {
2276        if (!$this->replace_all_text_supported()) {
2277            return;
2278        }
2279
2280        // NOTE: override this methods if following standard compliant SQL
2281        //       does not work for your driver.
2282
2283        $columnname = $column->name;
2284        $sql = "UPDATE {".$table."}
2285                       SET $columnname = REPLACE($columnname, ?, ?)
2286                     WHERE $columnname IS NOT NULL";
2287
2288        if ($column->meta_type === 'X') {
2289            $this->execute($sql, array($search, $replace));
2290
2291        } else if ($column->meta_type === 'C') {
2292            if (core_text::strlen($search) < core_text::strlen($replace)) {
2293                $colsize = $column->max_length;
2294                $sql = "UPDATE {".$table."}
2295                       SET $columnname = " . $this->sql_substr("REPLACE(" . $columnname . ", ?, ?)", 1, $colsize) . "
2296                     WHERE $columnname IS NOT NULL";
2297            }
2298            $this->execute($sql, array($search, $replace));
2299        }
2300    }
2301
2302    /**
2303     * Analyze the data in temporary tables to force statistics collection after bulk data loads.
2304     *
2305     * @return void
2306     */
2307    public function update_temp_table_stats() {
2308        $this->temptables->update_stats();
2309    }
2310
2311    /**
2312     * Checks and returns true if transactions are supported.
2313     *
2314     * It is not responsible to run productions servers
2315     * on databases without transaction support ;-)
2316     *
2317     * Override in driver if needed.
2318     *
2319     * @return bool
2320     */
2321    protected function transactions_supported() {
2322        // protected for now, this might be changed to public if really necessary
2323        return true;
2324    }
2325
2326    /**
2327     * Returns true if a transaction is in progress.
2328     * @return bool
2329     */
2330    public function is_transaction_started() {
2331        return !empty($this->transactions);
2332    }
2333
2334    /**
2335     * This is a test that throws an exception if transaction in progress.
2336     * This test does not force rollback of active transactions.
2337     * @return void
2338     * @throws dml_transaction_exception if stansaction active
2339     */
2340    public function transactions_forbidden() {
2341        if ($this->is_transaction_started()) {
2342            throw new dml_transaction_exception('This code can not be excecuted in transaction');
2343        }
2344    }
2345
2346    /**
2347     * On DBs that support it, switch to transaction mode and begin a transaction
2348     * you'll need to ensure you call allow_commit() on the returned object
2349     * or your changes *will* be lost.
2350     *
2351     * this is _very_ useful for massive updates
2352     *
2353     * Delegated database transactions can be nested, but only one actual database
2354     * transaction is used for the outer-most delegated transaction. This method
2355     * returns a transaction object which you should keep until the end of the
2356     * delegated transaction. The actual database transaction will
2357     * only be committed if all the nested delegated transactions commit
2358     * successfully. If any part of the transaction rolls back then the whole
2359     * thing is rolled back.
2360     *
2361     * @return moodle_transaction
2362     */
2363    public function start_delegated_transaction() {
2364        $transaction = new moodle_transaction($this);
2365        $this->transactions[] = $transaction;
2366        if (count($this->transactions) == 1) {
2367            $this->begin_transaction();
2368        }
2369        return $transaction;
2370    }
2371
2372    /**
2373     * Driver specific start of real database transaction,
2374     * this can not be used directly in code.
2375     * @return void
2376     */
2377    protected abstract function begin_transaction();
2378
2379    /**
2380     * Indicates delegated transaction finished successfully.
2381     * The real database transaction is committed only if
2382     * all delegated transactions committed.
2383     * @param moodle_transaction $transaction The transaction to commit
2384     * @return void
2385     * @throws dml_transaction_exception Creates and throws transaction related exceptions.
2386     */
2387    public function commit_delegated_transaction(moodle_transaction $transaction) {
2388        if ($transaction->is_disposed()) {
2389            throw new dml_transaction_exception('Transactions already disposed', $transaction);
2390        }
2391        // mark as disposed so that it can not be used again
2392        $transaction->dispose();
2393
2394        if (empty($this->transactions)) {
2395            throw new dml_transaction_exception('Transaction not started', $transaction);
2396        }
2397
2398        if ($this->force_rollback) {
2399            throw new dml_transaction_exception('Tried to commit transaction after lower level rollback', $transaction);
2400        }
2401
2402        if ($transaction !== $this->transactions[count($this->transactions) - 1]) {
2403            // one incorrect commit at any level rollbacks everything
2404            $this->force_rollback = true;
2405            throw new dml_transaction_exception('Invalid transaction commit attempt', $transaction);
2406        }
2407
2408        if (count($this->transactions) == 1) {
2409            // only commit the top most level
2410            $this->commit_transaction();
2411        }
2412        array_pop($this->transactions);
2413
2414        if (empty($this->transactions)) {
2415            \core\event\manager::database_transaction_commited();
2416            \core\message\manager::database_transaction_commited();
2417        }
2418    }
2419
2420    /**
2421     * Driver specific commit of real database transaction,
2422     * this can not be used directly in code.
2423     * @return void
2424     */
2425    protected abstract function commit_transaction();
2426
2427    /**
2428     * Call when delegated transaction failed, this rolls back
2429     * all delegated transactions up to the top most level.
2430     *
2431     * In many cases you do not need to call this method manually,
2432     * because all open delegated transactions are rolled back
2433     * automatically if exceptions not caught.
2434     *
2435     * @param moodle_transaction $transaction An instance of a moodle_transaction.
2436     * @param Exception|Throwable $e The related exception/throwable to this transaction rollback.
2437     * @return void This does not return, instead the exception passed in will be rethrown.
2438     */
2439    public function rollback_delegated_transaction(moodle_transaction $transaction, $e) {
2440        if (!($e instanceof Exception) && !($e instanceof Throwable)) {
2441            // PHP7 - we catch Throwables in phpunit but can't use that as the type hint in PHP5.
2442            $e = new \coding_exception("Must be given an Exception or Throwable object!");
2443        }
2444        if ($transaction->is_disposed()) {
2445            throw new dml_transaction_exception('Transactions already disposed', $transaction);
2446        }
2447        // mark as disposed so that it can not be used again
2448        $transaction->dispose();
2449
2450        // one rollback at any level rollbacks everything
2451        $this->force_rollback = true;
2452
2453        if (empty($this->transactions) or $transaction !== $this->transactions[count($this->transactions) - 1]) {
2454            // this may or may not be a coding problem, better just rethrow the exception,
2455            // because we do not want to loose the original $e
2456            throw $e;
2457        }
2458
2459        if (count($this->transactions) == 1) {
2460            // only rollback the top most level
2461            $this->rollback_transaction();
2462        }
2463        array_pop($this->transactions);
2464        if (empty($this->transactions)) {
2465            // finally top most level rolled back
2466            $this->force_rollback = false;
2467            \core\event\manager::database_transaction_rolledback();
2468            \core\message\manager::database_transaction_rolledback();
2469        }
2470        throw $e;
2471    }
2472
2473    /**
2474     * Driver specific abort of real database transaction,
2475     * this can not be used directly in code.
2476     * @return void
2477     */
2478    protected abstract function rollback_transaction();
2479
2480    /**
2481     * Force rollback of all delegated transaction.
2482     * Does not throw any exceptions and does not log anything.
2483     *
2484     * This method should be used only from default exception handlers and other
2485     * core code.
2486     *
2487     * @return void
2488     */
2489    public function force_transaction_rollback() {
2490        if ($this->transactions) {
2491            try {
2492                $this->rollback_transaction();
2493            } catch (dml_exception $e) {
2494                // ignore any sql errors here, the connection might be broken
2495            }
2496        }
2497
2498        // now enable transactions again
2499        $this->transactions = array();
2500        $this->force_rollback = false;
2501
2502        \core\event\manager::database_transaction_rolledback();
2503        \core\message\manager::database_transaction_rolledback();
2504    }
2505
2506    /**
2507     * Is session lock supported in this driver?
2508     * @return bool
2509     */
2510    public function session_lock_supported() {
2511        return false;
2512    }
2513
2514    /**
2515     * Obtains the session lock.
2516     * @param int $rowid The id of the row with session record.
2517     * @param int $timeout The maximum allowed time to wait for the lock in seconds.
2518     * @return void
2519     * @throws dml_exception A DML specific exception is thrown for any errors.
2520     */
2521    public function get_session_lock($rowid, $timeout) {
2522        $this->used_for_db_sessions = true;
2523    }
2524
2525    /**
2526     * Releases the session lock.
2527     * @param int $rowid The id of the row with session record.
2528     * @return void
2529     * @throws dml_exception A DML specific exception is thrown for any errors.
2530     */
2531    public function release_session_lock($rowid) {
2532    }
2533
2534    /**
2535     * Returns the number of reads done by this database.
2536     * @return int Number of reads.
2537     */
2538    public function perf_get_reads() {
2539        return $this->reads;
2540    }
2541
2542    /**
2543     * Returns the number of writes done by this database.
2544     * @return int Number of writes.
2545     */
2546    public function perf_get_writes() {
2547        return $this->writes;
2548    }
2549
2550    /**
2551     * Returns the number of queries done by this database.
2552     * @return int Number of queries.
2553     */
2554    public function perf_get_queries() {
2555        return $this->writes + $this->reads;
2556    }
2557
2558    /**
2559     * Time waiting for the database engine to finish running all queries.
2560     * @return float Number of seconds with microseconds
2561     */
2562    public function perf_get_queries_time() {
2563        return $this->queriestime;
2564    }
2565}
Note: See TracBrowser for help on using the repository browser.