MDL-43761 refactor db manager code to send arrays of SQL to the dml driver

This commit is contained in:
Petr Skoda 2014-01-20 14:06:44 +08:00 committed by Petr Škoda
parent 9788e26805
commit 3f17d7091b
8 changed files with 112 additions and 60 deletions

View File

@ -69,26 +69,21 @@ class database_manager {
/**
* This function will execute an array of SQL commands.
*
* @param array $sqlarr Array of sql statements to execute.
* @throws ddl_exception This exception is thrown if any error is found.
* @param string[] $sqlarr Array of sql statements to execute.
* @throws ddl_change_structure_exception This exception is thrown if any error is found.
*/
protected function execute_sql_arr(array $sqlarr) {
foreach ($sqlarr as $sql) {
$this->execute_sql($sql);
}
$this->mdb->change_database_structure($sqlarr);
}
/**
* Execute a given sql command string.
*
* @param string $sql The sql string you wish to be executed.
* @throws ddl_exception This exception is thrown if any error is found.
* @throws ddl_change_structure_exception This exception is thrown if any error is found.
*/
protected function execute_sql($sql) {
if (!$this->mdb->change_database_structure($sql)) {
// in case driver does not throw exceptions yet ;-)
throw new ddl_change_structure_exception($this->mdb->get_last_error(), $sql);
}
$this->mdb->change_database_structure($sql);
}
/**

View File

@ -1079,9 +1079,9 @@ abstract class moodle_database {
/**
* Do NOT use in code, this is for use by database_manager only!
* @param string $sql query
* @param string|array $sql query or array of queries
* @return bool true
* @throws dml_exception A DML specific exception is thrown for any errors.
* @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
*/
public abstract function change_database_structure($sql);

View File

@ -595,17 +595,26 @@ class mssql_native_moodle_database extends moodle_database {
/**
* Do NOT use in code, to be used by database_manager only!
* @param string $sql query
* @param string|array $sql query
* @return bool true
* @throws dml_exception A DML specific exception is thrown for any errors.
* @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
*/
public function change_database_structure($sql) {
$this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql;
try {
foreach ($sqls as $sql) {
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = mssql_query($sql, $this->mssql);
$this->query_end($result);
}
} catch (ddl_change_structure_exception $e) {
$this->reset_caches();
throw $e;
}
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = mssql_query($sql, $this->mssql);
$this->query_end($result);
return true;
}

View File

@ -821,17 +821,26 @@ class mysqli_native_moodle_database extends moodle_database {
/**
* Do NOT use in code, to be used by database_manager only!
* @param string $sql query
* @param string|array $sql query
* @return bool true
* @throws dml_exception A DML specific exception is thrown for any errors.
* @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
*/
public function change_database_structure($sql) {
$this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql;
try {
foreach ($sqls as $sql) {
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = $this->mysqli->query($sql);
$this->query_end($result);
}
} catch (ddl_change_structure_exception $e) {
$this->reset_caches();
throw $e;
}
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = $this->mysqli->query($sql);
$this->query_end($result);
return true;
}

View File

@ -888,19 +888,28 @@ class oci_native_moodle_database extends moodle_database {
/**
* Do NOT use in code, to be used by database_manager only!
* @param string $sql query
* @param string|array $sql query
* @return bool true
* @throws dml_exception A DML specific exception is thrown for any errors.
* @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
*/
public function change_database_structure($sql) {
$this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql;
try {
foreach ($sqls as $sql) {
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$stmt = $this->parse_query($sql);
$result = oci_execute($stmt, $this->commit_status);
$this->query_end($result, $stmt);
oci_free_statement($stmt);
}
} catch (ddl_change_structure_exception $e) {
$this->reset_caches();
throw $e;
}
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$stmt = $this->parse_query($sql);
$result = oci_execute($stmt, $this->commit_status);
$this->query_end($result, $stmt);
oci_free_statement($stmt);
return true;
}

View File

@ -174,22 +174,34 @@ abstract class pdo_moodle_database extends moodle_database {
/**
* Do NOT use in code, to be used by database_manager only!
* @param string $sql query
* @return bool success
* @param string|array $sql query
* @return bool true
* @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
*/
public function change_database_structure($sql) {
$result = true;
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql;
try {
$this->pdb->exec($sql);
foreach ($sqls as $sql) {
$result = true;
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
try {
$this->pdb->exec($sql);
} catch (PDOException $ex) {
$this->lastError = $ex->getMessage();
$result = false;
}
$this->query_end($result);
}
} catch (ddl_change_structure_exception $e) {
$this->reset_caches();
} catch (PDOException $ex) {
$this->lastError = $ex->getMessage();
$result = false;
throw $e;
}
$this->query_end($result);
return $result;
$this->reset_caches();
return true;
}
public function delete_records_select($table, $select, array $params=null) {

View File

@ -625,18 +625,27 @@ class pgsql_native_moodle_database extends moodle_database {
/**
* Do NOT use in code, to be used by database_manager only!
* @param string $sql query
* @param string|array $sql query
* @return bool true
* @throws dml_exception A DML specific exception is thrown for any errors.
* @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
*/
public function change_database_structure($sql) {
$this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql;
try {
foreach ($sqls as $sql) {
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);
pg_free_result($result);
}
} catch (ddl_change_structure_exception $e) {
$this->reset_caches();
throw $e;
}
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);
pg_free_result($result);
return true;
}

View File

@ -669,17 +669,26 @@ class sqlsrv_native_moodle_database extends moodle_database {
/**
* Do NOT use in code, to be used by database_manager only!
* @param string $sql query
* @param string|array $sql query
* @return bool true
* @throws dml_exception A DML specific exception is thrown for any errors.
* @throws ddl_change_structure_exception A DDL specific exception is thrown for any errors.
*/
public function change_database_structure($sql) {
$this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql;
try {
foreach ($sqls as $sql) {
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = sqlsrv_query($this->sqlsrv, $sql);
$this->query_end($result);
}
} catch (ddl_change_structure_exception $e) {
$this->reset_caches();
throw $e;
}
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = sqlsrv_query($this->sqlsrv, $sql);
$this->query_end($result);
return true;
}