Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 23... Línea 23...
23
 */
23
 */
Línea 24... Línea 24...
24
 
24
 
Línea 25... Línea 25...
25
defined('MOODLE_INTERNAL') || die();
25
defined('MOODLE_INTERNAL') || die();
26
 
26
 
27
require_once(__DIR__.'/moodle_database.php');
27
require_once(__DIR__.'/moodle_database.php');
28
require_once(__DIR__.'/moodle_read_slave_trait.php');
28
require_once(__DIR__.'/moodle_read_replica_trait.php');
Línea 29... Línea 29...
29
require_once(__DIR__.'/mysqli_native_moodle_recordset.php');
29
require_once(__DIR__.'/mysqli_native_moodle_recordset.php');
30
require_once(__DIR__.'/mysqli_native_moodle_temptables.php');
30
require_once(__DIR__.'/mysqli_native_moodle_temptables.php');
Línea 35... Línea 35...
35
 * @package    core_dml
35
 * @package    core_dml
36
 * @copyright  2008 Petr Skoda (http://skodak.org)
36
 * @copyright  2008 Petr Skoda (http://skodak.org)
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
38
 */
39
class mysqli_native_moodle_database extends moodle_database {
39
class mysqli_native_moodle_database extends moodle_database {
40
    use moodle_read_slave_trait {
40
    use moodle_read_replica_trait {
41
        can_use_readonly as read_slave_can_use_readonly;
41
        can_use_readonly as read_replica_can_use_readonly;
42
    }
42
    }
Línea 43... Línea 43...
43
 
43
 
44
    /** @var array $sslmodes */
44
    /** @var array $sslmodes */
45
    private static $sslmodes = [
45
    private static $sslmodes = [
Línea 63... Línea 63...
63
     * @param string $dbpass
63
     * @param string $dbpass
64
     * @param string $dbname
64
     * @param string $dbname
65
     * @return bool success
65
     * @return bool success
66
     * @throws dml_exception A DML specific exception is thrown for any errors.
66
     * @throws dml_exception A DML specific exception is thrown for any errors.
67
     */
67
     */
68
    public function create_database($dbhost, $dbuser, $dbpass, $dbname, array $dboptions=null) {
68
    public function create_database($dbhost, $dbuser, $dbpass, $dbname, ?array $dboptions=null) {
69
        $driverstatus = $this->driver_installed();
69
        $driverstatus = $this->driver_installed();
Línea 70... Línea 70...
70
 
70
 
71
        if ($driverstatus !== true) {
71
        if ($driverstatus !== true) {
72
            throw new dml_exception('dbdriverproblem', $driverstatus);
72
            throw new dml_exception('dbdriverproblem', $driverstatus);
Línea 133... Línea 133...
133
    }
133
    }
Línea 134... Línea 134...
134
 
134
 
135
    /**
135
    /**
136
     * Returns database family type - describes SQL dialect
136
     * Returns database family type - describes SQL dialect
137
     * Note: can be used before connect()
137
     * Note: can be used before connect()
138
     * @return string db family name (mysql, postgres, mssql, oracle, etc.)
138
     * @return string db family name (mysql, postgres, mssql, etc.)
139
     */
139
     */
140
    public function get_dbfamily() {
140
    public function get_dbfamily() {
141
        return 'mysql';
141
        return 'mysql';
Línea 142... Línea 142...
142
    }
142
    }
143
 
143
 
144
    /**
144
    /**
145
     * Returns more specific database driver type
145
     * Returns more specific database driver type
146
     * Note: can be used before connect()
146
     * Note: can be used before connect()
147
     * @return string db type mysqli, pgsql, oci, mssql, sqlsrv
147
     * @return string db type mysqli, pgsql, mssql, sqlsrv
148
     */
148
     */
149
    protected function get_dbtype() {
149
    protected function get_dbtype() {
Línea 499... Línea 499...
499
    public function get_configuration_help() {
499
    public function get_configuration_help() {
500
        return get_string('nativemysqlihelp', 'install');
500
        return get_string('nativemysqlihelp', 'install');
501
    }
501
    }
Línea 502... Línea 502...
502
 
502
 
503
    /**
-
 
504
     * Diagnose database and tables, this function is used
-
 
505
     * to verify database and driver settings, db engine types, etc.
-
 
506
     *
-
 
507
     * @return string null means everything ok, string means problem found.
-
 
508
     */
-
 
509
    public function diagnose() {
-
 
510
        $sloppymyisamfound = false;
-
 
511
        $prefix = str_replace('_', '\\_', $this->prefix);
-
 
512
        $sql = "SELECT COUNT('x')
-
 
513
                  FROM INFORMATION_SCHEMA.TABLES
-
 
514
                 WHERE table_schema = DATABASE()
-
 
515
                       AND table_name LIKE BINARY '$prefix%'
-
 
516
                       AND Engine = 'MyISAM'";
-
 
517
        $this->query_start($sql, null, SQL_QUERY_AUX);
-
 
518
        $result = $this->mysqli->query($sql);
-
 
519
        $this->query_end($result);
-
 
520
        if ($result) {
-
 
521
            if ($arr = $result->fetch_assoc()) {
-
 
522
                $count = reset($arr);
-
 
523
                if ($count) {
-
 
524
                    $sloppymyisamfound = true;
-
 
525
                }
-
 
526
            }
-
 
527
            $result->close();
-
 
528
        }
-
 
529
 
-
 
530
        if ($sloppymyisamfound) {
-
 
531
            return get_string('myisamproblem', 'error');
-
 
532
        } else {
-
 
533
            return null;
-
 
534
        }
-
 
535
    }
-
 
536
 
-
 
537
    /**
503
    /**
538
     * Connect to db
504
     * Connect to db
539
     * @param string $dbhost The database host.
505
     * @param string $dbhost The database host.
540
     * @param string $dbuser The database username.
506
     * @param string $dbuser The database username.
541
     * @param string $dbpass The database username's password.
507
     * @param string $dbpass The database username's password.
Línea 544... Línea 510...
544
     * @param array $dboptions driver specific options
510
     * @param array $dboptions driver specific options
545
     * @return bool success
511
     * @return bool success
546
     * @throws moodle_exception
512
     * @throws moodle_exception
547
     * @throws dml_connection_exception if error
513
     * @throws dml_connection_exception if error
548
     */
514
     */
549
    public function raw_connect(string $dbhost, string $dbuser, string $dbpass, string $dbname, $prefix, array $dboptions=null): bool {
515
    public function raw_connect(string $dbhost, string $dbuser, string $dbpass, string $dbname, $prefix, ?array $dboptions=null): bool {
550
        $driverstatus = $this->driver_installed();
516
        $driverstatus = $this->driver_installed();
Línea 551... Línea 517...
551
 
517
 
552
        if ($driverstatus !== true) {
518
        if ($driverstatus !== true) {
553
            throw new dml_exception('dbdriverproblem', $driverstatus);
519
            throw new dml_exception('dbdriverproblem', $driverstatus);
Línea 684... Línea 650...
684
     * @param int $type type of query
650
     * @param int $type type of query
685
     * @param string $sql
651
     * @param string $sql
686
     * @return bool
652
     * @return bool
687
     */
653
     */
688
    protected function can_use_readonly(int $type, string $sql): bool {
654
    protected function can_use_readonly(int $type, string $sql): bool {
689
        // ... *_LOCK queries always go to master.
655
        // ... *_LOCK queries always go to primary.
690
        if (preg_match('/\b(GET|RELEASE)_LOCK/i', $sql)) {
656
        if (preg_match('/\b(GET|RELEASE)_LOCK/i', $sql)) {
691
            return false;
657
            return false;
692
        }
658
        }
Línea 693... Línea 659...
693
 
659
 
694
        return $this->read_slave_can_use_readonly($type, $sql);
660
        return $this->read_replica_can_use_readonly($type, $sql);
Línea 695... Línea 661...
695
    }
661
    }
696
 
662
 
697
    /**
663
    /**
Línea 1243... Línea 1209...
1243
 
1209
 
1244
    /**
1210
    /**
1245
     * Very ugly hack which emulates bound parameters in queries
1211
     * Very ugly hack which emulates bound parameters in queries
1246
     * because prepared statements do not use query cache.
1212
     * because prepared statements do not use query cache.
1247
     */
1213
     */
1248
    protected function emulate_bound_params($sql, array $params=null) {
1214
    protected function emulate_bound_params($sql, ?array $params=null) {
1249
        if (empty($params)) {
1215
        if (empty($params)) {
1250
            return $sql;
1216
            return $sql;
1251
        }
1217
        }
1252
        // ok, we have verified sql statement with ? and correct number of params
1218
        // ok, we have verified sql statement with ? and correct number of params
Línea 1276... Línea 1242...
1276
     * @param string $sql query
1242
     * @param string $sql query
1277
     * @param array $params query parameters
1243
     * @param array $params query parameters
1278
     * @return bool true
1244
     * @return bool true
1279
     * @throws dml_exception A DML specific exception is thrown for any errors.
1245
     * @throws dml_exception A DML specific exception is thrown for any errors.
1280
     */
1246
     */
1281
    public function execute($sql, array $params=null) {
1247
    public function execute($sql, ?array $params=null) {
1282
        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
1248
        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
Línea 1283... Línea 1249...
1283
 
1249
 
1284
        if (strpos($sql, ';') !== false) {
1250
        if (strpos($sql, ';') !== false) {
1285
            throw new coding_exception('moodle_database::execute() Multiple sql statements found or bound parameters not used properly in query!');
1251
            throw new coding_exception('moodle_database::execute() Multiple sql statements found or bound parameters not used properly in query!');
Línea 1315... Línea 1281...
1315
     * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
1281
     * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
1316
     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1282
     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1317
     * @return moodle_recordset instance
1283
     * @return moodle_recordset instance
1318
     * @throws dml_exception A DML specific exception is thrown for any errors.
1284
     * @throws dml_exception A DML specific exception is thrown for any errors.
1319
     */
1285
     */
1320
    public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
1286
    public function get_recordset_sql($sql, ?array $params=null, $limitfrom=0, $limitnum=0) {
Línea 1321... Línea 1287...
1321
 
1287
 
Línea 1322... Línea 1288...
1322
        list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum);
1288
        list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum);
1323
 
1289
 
Línea 1377... Línea 1343...
1377
     * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
1343
     * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
1378
     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1344
     * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1379
     * @return array of objects, or empty array if no records were found
1345
     * @return array of objects, or empty array if no records were found
1380
     * @throws dml_exception A DML specific exception is thrown for any errors.
1346
     * @throws dml_exception A DML specific exception is thrown for any errors.
1381
     */
1347
     */
1382
    public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {
1348
    public function get_records_sql($sql, ?array $params=null, $limitfrom=0, $limitnum=0) {
Línea 1383... Línea 1349...
1383
 
1349
 
Línea 1384... Línea 1350...
1384
        list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum);
1350
        list($limitfrom, $limitnum) = $this->normalise_limit_from_num($limitfrom, $limitnum);
1385
 
1351
 
Línea 1419... Línea 1385...
1419
     * @param string $sql The SQL query
1385
     * @param string $sql The SQL query
1420
     * @param array $params array of sql parameters
1386
     * @param array $params array of sql parameters
1421
     * @return array of values
1387
     * @return array of values
1422
     * @throws dml_exception A DML specific exception is thrown for any errors.
1388
     * @throws dml_exception A DML specific exception is thrown for any errors.
1423
     */
1389
     */
1424
    public function get_fieldset_sql($sql, array $params=null) {
1390
    public function get_fieldset_sql($sql, ?array $params=null) {
1425
        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
1391
        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);
1426
        $rawsql = $this->emulate_bound_params($sql, $params);
1392
        $rawsql = $this->emulate_bound_params($sql, $params);
Línea 1427... Línea 1393...
1427
 
1393
 
1428
        $this->query_start($sql, $params, SQL_QUERY_SELECT);
1394
        $this->query_start($sql, $params, SQL_QUERY_SELECT);
Línea 1762... Línea 1728...
1762
     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1728
     * @param string $select A fragment of SQL to be used in a where clause in the SQL call.
1763
     * @param array $params array of sql parameters
1729
     * @param array $params array of sql parameters
1764
     * @return bool true
1730
     * @return bool true
1765
     * @throws dml_exception A DML specific exception is thrown for any errors.
1731
     * @throws dml_exception A DML specific exception is thrown for any errors.
1766
     */
1732
     */
1767
    public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {
1733
    public function set_field_select($table, $newfield, $newvalue, $select, ?array $params=null) {
1768
        if ($select) {
1734
        if ($select) {
1769
            $select = "WHERE $select";
1735
            $select = "WHERE $select";
1770
        }
1736
        }
1771
        if (is_null($params)) {
1737
        if (is_null($params)) {
1772
            $params = array();
1738
            $params = array();
Línea 1803... Línea 1769...
1803
     * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria).
1769
     * @param string $select A fragment of SQL to be used in a where clause in the SQL call (used to define the selection criteria).
1804
     * @param array $params array of sql parameters
1770
     * @param array $params array of sql parameters
1805
     * @return bool true
1771
     * @return bool true
1806
     * @throws dml_exception A DML specific exception is thrown for any errors.
1772
     * @throws dml_exception A DML specific exception is thrown for any errors.
1807
     */
1773
     */
1808
    public function delete_records_select($table, $select, array $params=null) {
1774
    public function delete_records_select($table, $select, ?array $params=null) {
1809
        if ($select) {
1775
        if ($select) {
1810
            $select = "WHERE $select";
1776
            $select = "WHERE $select";
1811
        }
1777
        }
1812
        $fixedtable = $this->fix_table_name($table);
1778
        $fixedtable = $this->fix_table_name($table);
1813
        $sql = "DELETE FROM $fixedtable $select";
1779
        $sql = "DELETE FROM $fixedtable $select";