From 3f17d7091b05eb1d433b43aa6e86459f949d1e25 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Mon, 20 Jan 2014 14:06:44 +0800 Subject: [PATCH] MDL-43761 refactor db manager code to send arrays of SQL to the dml driver --- lib/ddl/database_manager.php | 15 ++++------- lib/dml/moodle_database.php | 4 +-- lib/dml/mssql_native_moodle_database.php | 23 +++++++++++----- lib/dml/mysqli_native_moodle_database.php | 23 +++++++++++----- lib/dml/oci_native_moodle_database.php | 27 ++++++++++++------- lib/dml/pdo_moodle_database.php | 32 ++++++++++++++++------- lib/dml/pgsql_native_moodle_database.php | 25 ++++++++++++------ lib/dml/sqlsrv_native_moodle_database.php | 23 +++++++++++----- 8 files changed, 112 insertions(+), 60 deletions(-) diff --git a/lib/ddl/database_manager.php b/lib/ddl/database_manager.php index 1112bafb203..741f7904363 100644 --- a/lib/ddl/database_manager.php +++ b/lib/ddl/database_manager.php @@ -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); } /** diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 070f0cb9eff..34673255ea7 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -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); diff --git a/lib/dml/mssql_native_moodle_database.php b/lib/dml/mssql_native_moodle_database.php index 0a324b1c176..9b8e98fbfca 100644 --- a/lib/dml/mssql_native_moodle_database.php +++ b/lib/dml/mssql_native_moodle_database.php @@ -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; } diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index 3cb62a33ea5..ff6ab53444f 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -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; } diff --git a/lib/dml/oci_native_moodle_database.php b/lib/dml/oci_native_moodle_database.php index 00f79d6576c..8e99da42b41 100644 --- a/lib/dml/oci_native_moodle_database.php +++ b/lib/dml/oci_native_moodle_database.php @@ -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; } diff --git a/lib/dml/pdo_moodle_database.php b/lib/dml/pdo_moodle_database.php index 53fd8d5c74e..29cdd65b5de 100644 --- a/lib/dml/pdo_moodle_database.php +++ b/lib/dml/pdo_moodle_database.php @@ -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) { diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 2ee1635843c..b87286117ca 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -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; } diff --git a/lib/dml/sqlsrv_native_moodle_database.php b/lib/dml/sqlsrv_native_moodle_database.php index f6cab36050f..5110a32dd88 100644 --- a/lib/dml/sqlsrv_native_moodle_database.php +++ b/lib/dml/sqlsrv_native_moodle_database.php @@ -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; }