mirror of
https://github.com/moodle/moodle.git
synced 2025-03-29 03:52:33 +01:00
348 lines
12 KiB
PHP
348 lines
12 KiB
PHP
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
/**
|
|
* Legacy MySQL database class using adodb backend
|
|
*
|
|
* TODO: delete before branching 2.0
|
|
*
|
|
* @package moodlecore
|
|
* @subpackage DML
|
|
* @copyright 2008 Petr Skoda (http://skodak.org)
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
require_once($CFG->libdir.'/dml/moodle_database.php');
|
|
require_once($CFG->libdir.'/dml/adodb_moodle_database.php');
|
|
|
|
/**
|
|
* Legacy MySQL database class using adodb backend
|
|
*/
|
|
class mysqli_adodb_moodle_database extends adodb_moodle_database {
|
|
|
|
/**
|
|
* Attempt to create the database
|
|
* @param string $dbhost
|
|
* @param string $dbuser
|
|
* @param string $dbpass
|
|
* @param string $dbname
|
|
* @return bool success
|
|
* @throws dml_exception if error
|
|
*/
|
|
public function create_database($dbhost, $dbuser, $dbpass, $dbname, array $dboptions=null) {
|
|
$this->adodb->database = ''; // reset database name cached by ADODB. Trick from MDL-9609
|
|
if ($this->adodb->Connect($dbhost, $dbuser, $dbpass)) { /// Try to connect without DB
|
|
if ($this->adodb->Execute("CREATE DATABASE $dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci")) {
|
|
$this->adodb->Disconnect();
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Detects if all needed PHP stuff installed.
|
|
* Do not connect to connect to db if this test fails.
|
|
* @return mixed true if ok, string if something
|
|
*/
|
|
public function driver_installed() {
|
|
if (!extension_loaded('mysqli')) {
|
|
return get_string('mysqliextensionisnotpresentinphp', 'install');
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected function preconfigure_dbconnection() {
|
|
if (!defined('ADODB_ASSOC_CASE')) {
|
|
define ('ADODB_ASSOC_CASE', 2);
|
|
}
|
|
}
|
|
|
|
protected function configure_dbconnection() {
|
|
$this->adodb->SetFetchMode(ADODB_FETCH_ASSOC);
|
|
|
|
$sql = "SET NAMES 'utf8'";
|
|
$this->query_start($sql, null, SQL_QUERY_AUX);
|
|
$result = $this->adodb->Execute($sql);
|
|
$this->query_end($result);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Returns database family type
|
|
* @return string db family name (mysql, postgres, mssql, oracle, etc.)
|
|
*/
|
|
public function get_dbfamily() {
|
|
return 'mysql';
|
|
}
|
|
|
|
/**
|
|
* Returns database type
|
|
* @return string db type mysql, mysqli, postgres7
|
|
*/
|
|
protected function get_dbtype() {
|
|
return 'mysqli';
|
|
}
|
|
|
|
/**
|
|
* Returns localised database description
|
|
* Note: can be used before connect()
|
|
* @return string
|
|
*/
|
|
public function get_configuration_hints() {
|
|
return get_string('databasesettingssub_mysqli', 'install');
|
|
}
|
|
|
|
/**
|
|
* Returns supported query parameter types
|
|
* @return bitmask
|
|
*/
|
|
protected function allowed_param_types() {
|
|
return SQL_PARAMS_QM;
|
|
}
|
|
|
|
/**
|
|
* This method will introspect inside DB to detect it it's a UTF-8 DB or no
|
|
* Used from setup.php to set correctly "set names" when the installation
|
|
* process is performed without the initial and beautiful installer
|
|
* @return bool true if db in unicode mode
|
|
*/
|
|
function setup_is_unicodedb() {
|
|
|
|
$sql = "SHOW LOCAL VARIABLES LIKE 'character_set_database'";
|
|
$this->query_start($sql, null, SQL_QUERY_AUX);
|
|
$rs = $this->adodb->Execute($sql);
|
|
$this->query_end($rs);
|
|
|
|
if ($rs && !$rs->EOF) {
|
|
$records = $rs->GetAssoc(true);
|
|
$encoding = $records['character_set_database']['Value'];
|
|
if (strtoupper($encoding) == 'UTF8') {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
/* Tries to change default db encoding to utf8, if empty db
|
|
* @return bool sucecss
|
|
* @throws dml_exception if error
|
|
*/
|
|
public function change_db_encoding() {
|
|
// try forcing utf8 collation, if mysql db and no tables present
|
|
$this->query_start("--adodb-MetaTables", null, SQL_QUERY_AUX);
|
|
// TODO: maybe add separate empty database test because this ignores tables without prefix
|
|
$metatables = $this->adodb->MetaTables();
|
|
$this->query_end(true);
|
|
|
|
if (!$metatables) {
|
|
$sql = "ALTER DATABASE $this->dbname CHARACTER SET utf8";
|
|
$this->query_start($sql, null, SQL_QUERY_AUX);
|
|
$rs = $this->adodb->Execute($sql);
|
|
$this->query_end($rs);
|
|
if ($this->setup_is_unicodedb()) {
|
|
$this->configure_dbconnection();
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Insert a record into a table and return the "id" field if required,
|
|
* Some conversions and safety checks are carried out. Lobs are supported.
|
|
* If the return ID isn't required, then this just reports success as true/false.
|
|
* $data is an object containing needed data
|
|
* @param string $table The database table to be inserted into
|
|
* @param object $data A data object with values for one or more fields in the record
|
|
* @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.
|
|
* @param bool $bulk true means repeated inserts expected
|
|
* @return true or new id
|
|
* @throws dml_exception if error
|
|
*/
|
|
public function insert_record($table, $dataobject, $returnid=true, $bulk=false) {
|
|
if (!is_object($dataobject)) {
|
|
$dataobject = (object)$dataobject;
|
|
}
|
|
|
|
$columns = $this->get_columns($table);
|
|
|
|
unset($dataobject->id);
|
|
$cleaned = array();
|
|
|
|
foreach ($dataobject as $field=>$value) {
|
|
if (!isset($columns[$field])) {
|
|
continue;
|
|
}
|
|
$column = $columns[$field];
|
|
if (is_bool($value)) {
|
|
$value = (int)$value; // prevent "false" problems
|
|
}
|
|
if (!empty($column->enums)) {
|
|
// workaround for problem with wrong enums in mysql
|
|
if (is_null($value) and !$column->not_null) {
|
|
// ok - nulls allowed
|
|
} else {
|
|
if (!in_array((string)$value, $column->enums)) {
|
|
throw new dml_write_exception('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.');
|
|
}
|
|
}
|
|
}
|
|
$cleaned[$field] = $value;
|
|
}
|
|
|
|
return $this->insert_record_raw($table, $cleaned, $returnid, $bulk);
|
|
}
|
|
|
|
/**
|
|
* Update a record in a table
|
|
*
|
|
* $dataobject is an object containing needed data
|
|
* Relies on $dataobject having a variable "id" to
|
|
* specify the record to update
|
|
*
|
|
* @param string $table The database table to be checked against.
|
|
* @param object $dataobject An object with contents equal to fieldname=>fieldvalue. Must have an entry for 'id' to map to the table specified.
|
|
* @param bool true means repeated updates expected
|
|
* @return bool true
|
|
* @throws dml_exception if error
|
|
*/
|
|
public function update_record($table, $dataobject, $bulk=false) {
|
|
if (!is_object($dataobject)) {
|
|
$dataobject = (object)$dataobject;
|
|
}
|
|
|
|
$columns = $this->get_columns($table);
|
|
$cleaned = array();
|
|
|
|
foreach ($dataobject as $field=>$value) {
|
|
if (!isset($columns[$field])) {
|
|
continue;
|
|
}
|
|
if (is_bool($value)) {
|
|
$value = (int)$value; // prevent "false" problems
|
|
}
|
|
$cleaned[$field] = $value;
|
|
}
|
|
|
|
return $this->update_record_raw($table, $cleaned, $bulk);
|
|
}
|
|
|
|
/**
|
|
* Set a single field in every table row where the select statement evaluates to true.
|
|
*
|
|
* @param string $table The database table to be checked against.
|
|
* @param string $newfield the field to set.
|
|
* @param string $newvalue the value to set the field to.
|
|
* @param string $select A fragment of SQL to be used in a where clause in the SQL call.
|
|
* @param array $params array of sql parameters
|
|
* @return bool true
|
|
* @throws dml_exception if error
|
|
*/
|
|
public function set_field_select($table, $newfield, $newvalue, $select, array $params=null) {
|
|
if ($select) {
|
|
$select = "WHERE $select";
|
|
}
|
|
if (is_null($params)) {
|
|
$params = array();
|
|
}
|
|
list($select, $params, $type) = $this->fix_sql_params($select, $params);
|
|
|
|
if (is_bool($newvalue)) {
|
|
$newvalue = (int)$newvalue; // prevent "false" problems
|
|
}
|
|
if (is_null($newvalue)) {
|
|
$newfield = "$newfield = NULL";
|
|
} else {
|
|
$newfield = "$newfield = ?";
|
|
array_unshift($params, $newvalue);
|
|
}
|
|
$sql = "UPDATE {$this->prefix}$table SET $newfield $select";
|
|
|
|
$this->query_start($sql, $params, SQL_QUERY_UPDATE);
|
|
$rs = $this->adodb->Execute($sql, $params);
|
|
$this->query_end($rs);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
public function sql_cast_char2int($fieldname, $text=false) {
|
|
return ' CAST(' . $fieldname . ' AS SIGNED) ';
|
|
}
|
|
|
|
/**
|
|
* Returns the SQL text to be used to calculate the length in characters of one expression.
|
|
* @param string fieldname or expression to calculate its length in characters.
|
|
* @return string the piece of SQL code to be used in the statement.
|
|
*/
|
|
public function sql_length($fieldname) {
|
|
return ' CHAR_LENGTH(' . $fieldname . ')';
|
|
}
|
|
|
|
/**
|
|
* Does this driver suppoer regex syntax when searching
|
|
*/
|
|
public function sql_regex_supported() {
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Return regex positive or negative match sql
|
|
* @param bool $positivematch
|
|
* @return string or empty if not supported
|
|
*/
|
|
public function sql_regex($positivematch=true) {
|
|
return $positivematch ? 'REGEXP' : 'NOT REGEXP';
|
|
}
|
|
|
|
public function sql_cast_2signed($fieldname) {
|
|
return ' CAST(' . $fieldname . ' AS SIGNED) ';
|
|
}
|
|
|
|
/**
|
|
* Import a record into a table, id field is required.
|
|
* Basic safety checks only. Lobs are supported.
|
|
* @param string $table name of database table to be inserted into
|
|
* @param mixed $dataobject object or array with fields in the record
|
|
* @return bool true
|
|
* @throws dml_exception if error
|
|
*/
|
|
public function import_record($table, $dataobject) {
|
|
$dataobject = (object)$dataobject;
|
|
|
|
$columns = $this->get_columns($table);
|
|
$cleaned = array();
|
|
|
|
foreach ($dataobject as $field=>$value) {
|
|
if (!isset($columns[$field])) {
|
|
continue;
|
|
}
|
|
$cleaned[$field] = $value;
|
|
}
|
|
|
|
return $this->insert_record_raw($table, $cleaned, false, true, true);
|
|
}
|
|
}
|