MDL-51374 core_dml: change database layer to pass table name as parameter

This commit is contained in:
Simey Lameze 2016-03-15 14:41:48 +08:00
parent 26e7eceefc
commit 87a3e50192
11 changed files with 165 additions and 65 deletions

View file

@ -70,10 +70,11 @@ class database_manager {
* This function will execute an array of SQL commands. * This function will execute an array of SQL commands.
* *
* @param string[] $sqlarr Array of sql statements to execute. * @param string[] $sqlarr Array of sql statements to execute.
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @throws ddl_change_structure_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_arr(array $sqlarr) { protected function execute_sql_arr(array $sqlarr, $tablenames = null) {
$this->mdb->change_database_structure($sqlarr); $this->mdb->change_database_structure($sqlarr, $tablenames);
} }
/** /**
@ -107,6 +108,12 @@ class database_manager {
public function reset_sequence($table) { public function reset_sequence($table) {
if (!is_string($table) and !($table instanceof xmldb_table)) { if (!is_string($table) and !($table instanceof xmldb_table)) {
throw new ddl_exception('ddlunknownerror', NULL, 'incorrect table parameter!'); throw new ddl_exception('ddlunknownerror', NULL, 'incorrect table parameter!');
} else {
if ($table instanceof xmldb_table) {
$tablename = $table->getName();
} else {
$tablename = $table;
}
} }
// Do not test if table exists because it is slow // Do not test if table exists because it is slow
@ -115,7 +122,7 @@ class database_manager {
throw new ddl_exception('ddlunknownerror', null, 'table reset sequence sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'table reset sequence sql not generated');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($tablename));
} }
/** /**
@ -322,8 +329,7 @@ class database_manager {
if (!$sqlarr = $this->generator->getDropTableSQL($xmldb_table)) { if (!$sqlarr = $this->generator->getDropTableSQL($xmldb_table)) {
throw new ddl_exception('ddlunknownerror', null, 'table drop sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'table drop sql not generated');
} }
$this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
$this->execute_sql_arr($sqlarr);
} }
/** /**
@ -409,7 +415,14 @@ class database_manager {
if (!$sqlarr = $this->generator->getCreateStructureSQL($xmldb_structure)) { if (!$sqlarr = $this->generator->getCreateStructureSQL($xmldb_structure)) {
return; // nothing to do return; // nothing to do
} }
$this->execute_sql_arr($sqlarr);
$tablenames = array();
foreach ($xmldb_structure as $xmldb_table) {
if ($xmldb_table instanceof xmldb_table) {
$tablenames[] = $xmldb_table->getName();
}
}
$this->execute_sql_arr($sqlarr, $tablenames);
} }
/** /**
@ -428,7 +441,7 @@ class database_manager {
if (!$sqlarr = $this->generator->getCreateTableSQL($xmldb_table)) { if (!$sqlarr = $this->generator->getCreateTableSQL($xmldb_table)) {
throw new ddl_exception('ddlunknownerror', null, 'table create sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'table create sql not generated');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -451,8 +464,7 @@ class database_manager {
if (!$sqlarr = $this->generator->getCreateTempTableSQL($xmldb_table)) { if (!$sqlarr = $this->generator->getCreateTempTableSQL($xmldb_table)) {
throw new ddl_exception('ddlunknownerror', null, 'temp table create sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'temp table create sql not generated');
} }
$this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
$this->execute_sql_arr($sqlarr);
} }
/** /**
@ -530,7 +542,7 @@ class database_manager {
if (!$sqlarr = $this->generator->getAddFieldSQL($xmldb_table, $xmldb_field)) { if (!$sqlarr = $this->generator->getAddFieldSQL($xmldb_table, $xmldb_field)) {
throw new ddl_exception('ddlunknownerror', null, 'addfield sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'addfield sql not generated');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -555,7 +567,7 @@ class database_manager {
throw new ddl_exception('ddlunknownerror', null, 'drop_field sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'drop_field sql not generated');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -580,7 +592,7 @@ class database_manager {
return; // probably nothing to do return; // probably nothing to do
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -643,7 +655,7 @@ class database_manager {
return; //Empty array = nothing to do = no error return; //Empty array = nothing to do = no error
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -687,7 +699,7 @@ class database_manager {
return; //Empty array = nothing to do = no error return; //Empty array = nothing to do = no error
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -741,7 +753,7 @@ class database_manager {
return; //Empty array = nothing to do = no error return; //Empty array = nothing to do = no error
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -760,7 +772,7 @@ class database_manager {
return; //Empty array = nothing to do = no error return; //Empty array = nothing to do = no error
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -784,7 +796,7 @@ class database_manager {
throw new ddl_exception('ddlunknownerror', null, 'Some DBs do not support key renaming (MySQL, PostgreSQL, MsSQL). Rename skipped'); throw new ddl_exception('ddlunknownerror', null, 'Some DBs do not support key renaming (MySQL, PostgreSQL, MsSQL). Rename skipped');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -811,7 +823,7 @@ class database_manager {
throw new ddl_exception('ddlunknownerror', null, 'add_index sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'add_index sql not generated');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -838,7 +850,7 @@ class database_manager {
throw new ddl_exception('ddlunknownerror', null, 'drop_index sql not generated'); throw new ddl_exception('ddlunknownerror', null, 'drop_index sql not generated');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**
@ -870,7 +882,7 @@ class database_manager {
throw new ddl_exception('ddlunknownerror', null, 'Some DBs do not support index renaming (MySQL). Rename skipped'); throw new ddl_exception('ddlunknownerror', null, 'Some DBs do not support index renaming (MySQL). Rename skipped');
} }
$this->execute_sql_arr($sqlarr); $this->execute_sql_arr($sqlarr, array($xmldb_table->getName()));
} }
/** /**

View file

@ -226,9 +226,12 @@ abstract class sql_generator {
$tablename = $table->getName(); $tablename = $table->getName();
} }
if ($this->temptables->is_temptable($tablename)) {
return true;
}
// Get all tables in moodle database. // Get all tables in moodle database.
$tables = $this->mdb->get_tables(); $tables = $this->mdb->get_tables();
return isset($tables[$tablename]); return isset($tables[$tablename]);
} }

View file

@ -1049,13 +1049,32 @@ abstract class moodle_database {
/** /**
* Resets the internal column details cache * Resets the internal column details cache
*
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return void * @return void
*/ */
public function reset_caches() { public function reset_caches($tablenames = null) {
$this->tables = null; if (!empty($tablenames)) {
// Purge MUC as well. $temptablepurged = false;
$this->get_metacache()->purge(); $dbmetapurged = false;
$this->metacache = null; foreach ($tablenames as $tablename) {
if ($temptablepurged === false && $this->temptables->is_temptable($tablename)) {
$this->get_temp_tables_cache()->purge();
$temptablepurged = true;
} else if ($dbmetapurged === false) {
$this->tables = null;
$this->get_metacache()->purge();
$this->metacache = null;
$dbmetapurged = true;
}
}
} else {
$this->get_temp_tables_cache()->purge();
$this->tables = null;
// Purge MUC as well.
$this->get_metacache()->purge();
$this->metacache = null;
}
} }
/** /**
@ -1124,10 +1143,11 @@ abstract class moodle_database {
/** /**
* Do NOT use in code, this is for use by database_manager only! * Do NOT use in code, this is for use by database_manager only!
* @param string|array $sql query or array of queries * @param string|array $sql query or array of queries
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return bool true * @return bool true
* @throws ddl_change_structure_exception A DDL 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); public abstract function change_database_structure($sql, $tablenames = null);
/** /**
* Executes a general sql query. Should be used only when no other method suitable. * Executes a general sql query. Should be used only when no other method suitable.

View file

@ -438,8 +438,14 @@ class mssql_native_moodle_database extends moodle_database {
public function get_columns($table, $usecache=true) { public function get_columns($table, $usecache=true) {
if ($usecache) { if ($usecache) {
if ($data = $this->get_metacache()->get($table)) { if ($this->temptables->is_temptable($table)) {
return $data; if ($data = $this->get_temp_tables_cache()->get($table)) {
return $data;
}
} else {
if ($data = $this->get_metacache()->get($table)) {
return $data;
}
} }
} }
@ -534,7 +540,11 @@ class mssql_native_moodle_database extends moodle_database {
$this->free_result($result); $this->free_result($result);
if ($usecache) { if ($usecache) {
$this->get_metacache()->set($table, $structure); if ($this->temptables->is_temptable($table)) {
$this->get_temp_tables_cache()->set($table, $structure);
} else {
$this->get_metacache()->set($table, $structure);
}
} }
return $structure; return $structure;
@ -634,10 +644,11 @@ class mssql_native_moodle_database extends moodle_database {
/** /**
* Do NOT use in code, to be used by database_manager only! * Do NOT use in code, to be used by database_manager only!
* @param string|array $sql query * @param string|array $sql query
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return bool true * @return bool true
* @throws ddl_change_structure_exception A DDL 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) { public function change_database_structure($sql, $tablenames = null) {
$this->get_manager(); // Includes DDL exceptions classes ;-) $this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql; $sqls = (array)$sql;
@ -648,11 +659,11 @@ class mssql_native_moodle_database extends moodle_database {
$this->query_end($result); $this->query_end($result);
} }
} catch (ddl_change_structure_exception $e) { } catch (ddl_change_structure_exception $e) {
$this->reset_caches(); $this->reset_caches($tablenames);
throw $e; throw $e;
} }
$this->reset_caches(); $this->reset_caches($tablenames);
return true; return true;
} }

View file

@ -576,10 +576,15 @@ class mysqli_native_moodle_database extends moodle_database {
* @return database_column_info[] array of database_column_info objects indexed with column names * @return database_column_info[] array of database_column_info objects indexed with column names
*/ */
public function get_columns($table, $usecache=true) { public function get_columns($table, $usecache=true) {
if ($usecache) { if ($usecache) {
if ($data = $this->get_metacache()->get($table)) { if ($this->temptables->is_temptable($table)) {
return $data; if ($data = $this->get_temp_tables_cache()->get($table)) {
return $data;
}
} else {
if ($data = $this->get_metacache()->get($table)) {
return $data;
}
} }
} }
@ -684,7 +689,11 @@ class mysqli_native_moodle_database extends moodle_database {
} }
if ($usecache) { if ($usecache) {
$this->get_metacache()->set($table, $structure); if ($this->temptables->is_temptable($table)) {
$this->get_temp_tables_cache()->set($table, $structure);
} else {
$this->get_metacache()->set($table, $structure);
}
} }
return $structure; return $structure;
@ -887,10 +896,11 @@ class mysqli_native_moodle_database extends moodle_database {
/** /**
* Do NOT use in code, to be used by database_manager only! * Do NOT use in code, to be used by database_manager only!
* @param string|array $sql query * @param string|array $sql query
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return bool true * @return bool true
* @throws ddl_change_structure_exception A DDL 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) { public function change_database_structure($sql, $tablenames = null) {
$this->get_manager(); // Includes DDL exceptions classes ;-) $this->get_manager(); // Includes DDL exceptions classes ;-)
if (is_array($sql)) { if (is_array($sql)) {
$sql = implode("\n;\n", $sql); $sql = implode("\n;\n", $sql);
@ -913,11 +923,11 @@ class mysqli_native_moodle_database extends moodle_database {
while (@$this->mysqli->more_results()) { while (@$this->mysqli->more_results()) {
@$this->mysqli->next_result(); @$this->mysqli->next_result();
} }
$this->reset_caches(); $this->reset_caches($tablenames);
throw $e; throw $e;
} }
$this->reset_caches(); $this->reset_caches($tablenames);
return true; return true;
} }

View file

@ -470,8 +470,14 @@ class oci_native_moodle_database extends moodle_database {
public function get_columns($table, $usecache=true) { public function get_columns($table, $usecache=true) {
if ($usecache) { if ($usecache) {
if ($data = $this->get_metacache()->get($table)) { if ($this->temptables->is_temptable($table)) {
return $data; if ($data = $this->get_temp_tables_cache()->get($table)) {
return $data;
}
} else {
if ($data = $this->get_metacache()->get($table)) {
return $data;
}
} }
} }
@ -662,7 +668,11 @@ class oci_native_moodle_database extends moodle_database {
} }
if ($usecache) { if ($usecache) {
$this->get_metacache()->set($table, $structure); if ($this->temptables->is_temptable($table)) {
$this->get_temp_tables_cache()->set($table, $structure);
} else {
$this->get_metacache()->set($table, $structure);
}
} }
return $structure; return $structure;
@ -887,10 +897,11 @@ class oci_native_moodle_database extends moodle_database {
/** /**
* Do NOT use in code, to be used by database_manager only! * Do NOT use in code, to be used by database_manager only!
* @param string|array $sql query * @param string|array $sql query
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return bool true * @return bool true
* @throws ddl_change_structure_exception A DDL 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) { public function change_database_structure($sql, $tablenames = null) {
$this->get_manager(); // Includes DDL exceptions classes ;-) $this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql; $sqls = (array)$sql;
@ -903,11 +914,11 @@ class oci_native_moodle_database extends moodle_database {
oci_free_statement($stmt); oci_free_statement($stmt);
} }
} catch (ddl_change_structure_exception $e) { } catch (ddl_change_structure_exception $e) {
$this->reset_caches(); $this->reset_caches($tablenames);
throw $e; throw $e;
} }
$this->reset_caches(); $this->reset_caches($tablenames);
return true; return true;
} }

View file

@ -175,10 +175,11 @@ abstract class pdo_moodle_database extends moodle_database {
/** /**
* Do NOT use in code, to be used by database_manager only! * Do NOT use in code, to be used by database_manager only!
* @param string|array $sql query * @param string|array $sql query
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return bool true * @return bool true
* @throws ddl_change_structure_exception A DDL 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) { public function change_database_structure($sql, $tablenames = null) {
$this->get_manager(); // Includes DDL exceptions classes ;-) $this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql; $sqls = (array)$sql;
@ -196,11 +197,11 @@ abstract class pdo_moodle_database extends moodle_database {
$this->query_end($result); $this->query_end($result);
} }
} catch (ddl_change_structure_exception $e) { } catch (ddl_change_structure_exception $e) {
$this->reset_caches(); $this->reset_caches($tablenames);
throw $e; throw $e;
} }
$this->reset_caches(); $this->reset_caches($tablenames);
return true; return true;
} }

View file

@ -390,8 +390,14 @@ class pgsql_native_moodle_database extends moodle_database {
*/ */
public function get_columns($table, $usecache=true) { public function get_columns($table, $usecache=true) {
if ($usecache) { if ($usecache) {
if ($data = $this->get_metacache()->get($table)) { if ($this->temptables->is_temptable($table)) {
return $data; if ($data = $this->get_temp_tables_cache()->get($table)) {
return $data;
}
} else {
if ($data = $this->get_metacache()->get($table)) {
return $data;
}
} }
} }
@ -594,7 +600,11 @@ class pgsql_native_moodle_database extends moodle_database {
pg_free_result($result); pg_free_result($result);
if ($usecache) { if ($usecache) {
$this->get_metacache()->set($table, $structure); if ($this->temptables->is_temptable($table)) {
$this->get_temp_tables_cache()->set($table, $structure);
} else {
$this->get_metacache()->set($table, $structure);
}
} }
return $structure; return $structure;
@ -650,10 +660,11 @@ class pgsql_native_moodle_database extends moodle_database {
/** /**
* Do NOT use in code, to be used by database_manager only! * Do NOT use in code, to be used by database_manager only!
* @param string|array $sql query * @param string|array $sql query
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return bool true * @return bool true
* @throws ddl_change_structure_exception A DDL 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) { public function change_database_structure($sql, $tablenames = null) {
$this->get_manager(); // Includes DDL exceptions classes ;-) $this->get_manager(); // Includes DDL exceptions classes ;-)
if (is_array($sql)) { if (is_array($sql)) {
$sql = implode("\n;\n", $sql); $sql = implode("\n;\n", $sql);
@ -673,11 +684,11 @@ class pgsql_native_moodle_database extends moodle_database {
$result = @pg_query($this->pgsql, "ROLLBACK"); $result = @pg_query($this->pgsql, "ROLLBACK");
@pg_free_result($result); @pg_free_result($result);
} }
$this->reset_caches(); $this->reset_caches($tablenames);
throw $e; throw $e;
} }
$this->reset_caches(); $this->reset_caches($tablenames);
return true; return true;
} }

View file

@ -201,8 +201,14 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
public function get_columns($table, $usecache=true) { public function get_columns($table, $usecache=true) {
if ($usecache) { if ($usecache) {
if ($data = $this->get_metacache()->get($table)) { if ($this->temptables->is_temptable($table)) {
return $data; if ($data = $this->get_temp_tables_cache()->get($table)) {
return $data;
}
} else {
if ($data = $this->get_metacache()->get($table)) {
return $data;
}
} }
} }
@ -298,7 +304,11 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
} }
if ($usecache) { if ($usecache) {
$this->get_metacache()->set($table, $structure); if ($this->temptables->is_temptable($table)) {
$this->get_temp_tables_cache()->set($table, $structure);
} else {
$this->get_metacache()->set($table, $structure);
}
} }
return $structure; return $structure;

View file

@ -508,8 +508,14 @@ class sqlsrv_native_moodle_database extends moodle_database {
*/ */
public function get_columns($table, $usecache = true) { public function get_columns($table, $usecache = true) {
if ($usecache) { if ($usecache) {
if ($data = $this->get_metacache()->get($table)) { if ($this->temptables->is_temptable($table)) {
return $data; if ($data = $this->get_temp_tables_cache()->get($table)) {
return $data;
}
} else {
if ($data = $this->get_metacache()->get($table)) {
return $data;
}
} }
} }
@ -604,7 +610,11 @@ class sqlsrv_native_moodle_database extends moodle_database {
$this->free_result($result); $this->free_result($result);
if ($usecache) { if ($usecache) {
$this->get_metacache()->set($table, $structure); if ($this->temptables->is_temptable($table)) {
$this->get_temp_tables_cache()->set($table, $structure);
} else {
$this->get_metacache()->set($table, $structure);
}
} }
return $structure; return $structure;
@ -714,10 +724,11 @@ class sqlsrv_native_moodle_database extends moodle_database {
/** /**
* Do NOT use in code, to be used by database_manager only! * Do NOT use in code, to be used by database_manager only!
* @param string|array $sql query * @param string|array $sql query
* @param array|null $tablenames an array of xmldb table names affected by this request.
* @return bool true * @return bool true
* @throws ddl_change_structure_exception A DDL 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) { public function change_database_structure($sql, $tablenames = null) {
$this->get_manager(); // Includes DDL exceptions classes ;-) $this->get_manager(); // Includes DDL exceptions classes ;-)
$sqls = (array)$sql; $sqls = (array)$sql;
@ -728,11 +739,11 @@ class sqlsrv_native_moodle_database extends moodle_database {
$this->query_end($result); $this->query_end($result);
} }
} catch (ddl_change_structure_exception $e) { } catch (ddl_change_structure_exception $e) {
$this->reset_caches(); $this->reset_caches($tablenames);
throw $e; throw $e;
} }
$this->reset_caches(); $this->reset_caches($tablenames);
return true; return true;
} }

View file

@ -5510,7 +5510,7 @@ class moodle_database_for_testing extends moodle_database {
protected function normalise_value($column, $value) {} protected function normalise_value($column, $value) {}
public function set_debug($state) {} public function set_debug($state) {}
public function get_debug() {} public function get_debug() {}
public function change_database_structure($sql) {} public function change_database_structure($sql, $tablenames = null) {}
public function execute($sql, array $params=null) {} public function execute($sql, array $params=null) {}
public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {} public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {}
public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {} public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0) {}