Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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