MDL-17859 implemented caching in get_tables()

This commit is contained in:
skodak 2009-01-12 18:10:50 +00:00
parent 1045a0074c
commit 117679db37
9 changed files with 29 additions and 20 deletions

View File

@ -345,7 +345,7 @@ class mysql_sql_generator extends sql_generator {
$tablename = $xmldb_table->getName($xmldb_table);
/// Fetch all the columns in the table
if (!$columns = $this->mdb->get_columns($tablename, false)) {
if (!$columns = $this->mdb->get_columns($tablename)) {
return array();
}

View File

@ -109,7 +109,7 @@ abstract class adodb_moodle_database extends moodle_database {
* Return tables in database WITHOUT current prefix
* @return array of table names in lowercase and without prefix
*/
public function get_tables() {
public function get_tables($usecache=true) {
$this->query_start("--adodb-MetaTables", null, SQL_QUERY_AUX);
$metatables = $this->adodb->MetaTables();
$this->query_end(true);
@ -192,7 +192,7 @@ abstract class adodb_moodle_database extends moodle_database {
* @throws dml_exception if error
*/
public function change_database_structure($sql) {
$this->reset_columns();
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$rs = $this->adodb->Execute($sql);

View File

@ -40,6 +40,7 @@ abstract class moodle_database {
protected $database_manager;
protected $columns = array(); // I wish we had a shared memory cache for this :-(
protected $tables = null;
// db connection options
protected $dbhost;
@ -246,6 +247,7 @@ abstract class moodle_database {
$this->database_manager = null;
}
$this->columns = array();
$this->tables = null;
}
/**
@ -606,7 +608,7 @@ abstract class moodle_database {
* Return tables in database WITHOUT current prefix
* @return array of table names in lowercase and without prefix
*/
public abstract function get_tables();
public abstract function get_tables($usecache=true);
/**
* Return table indexes - everything lowercased
@ -627,8 +629,9 @@ abstract class moodle_database {
* @param string $table - empty means all, or one if name of table given
* @return void
*/
public function reset_columns() {
public function reset_caches() {
$this->columns = array();
$this->tables = null;
}
/**

View File

@ -177,8 +177,11 @@ class mysqli_native_moodle_database extends moodle_database {
* Return tables in database WITHOUT current prefix
* @return array of table names in lowercase and without prefix
*/
public function get_tables() {
$tables = array();
public function get_tables($usecache=true) {
if ($usecache and $this->tables !== null) {
return $this->tables;
}
$this->tables = array();
$sql = "SHOW TABLES";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = $this->mysqli->query($sql);
@ -192,11 +195,11 @@ class mysqli_native_moodle_database extends moodle_database {
}
$tablename = substr($tablename, strlen($this->prefix));
}
$tables[$tablename] = $tablename;
$this->tables[$tablename] = $tablename;
}
$result->close();
}
return $tables;
return $this->tables;
}
/**
@ -393,7 +396,7 @@ class mysqli_native_moodle_database extends moodle_database {
* @throws dml_exception if error
*/
public function change_database_structure($sql) {
$this->reset_columns();
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = $this->mysqli->query($sql);

View File

@ -235,7 +235,7 @@ class oci_native_moodle_database extends moodle_database {
* Return tables in database WITHOUT current prefix
* @return array of table names in lowercase and without prefix
*/
public function get_tables() {
public function get_tables($usecache=true) {
$tables = array();
$prefix = str_replace('_', "\\_", strtoupper($this->prefix));
$sql = "SELECT TABLE_NAME
@ -516,7 +516,7 @@ class oci_native_moodle_database extends moodle_database {
* @throws dml_exception if error
*/
public function change_database_structure($sql) {
$this->reset_columns();
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$stmt = $this->parse_query($sql);

View File

@ -151,7 +151,7 @@ abstract class pdo_moodle_database extends moodle_database {
$this->debug_query($sql);
}
$this->pdb->exec($sql);
$this->reset_columns();
$this->reset_caches();
return true;
} catch (PDOException $ex) {
$this->lastError = $ex->getMessage();

View File

@ -228,8 +228,11 @@ class pgsql_native_moodle_database extends moodle_database {
* Return tables in database WITHOUT current prefix
* @return array of table names in lowercase and without prefix
*/
public function get_tables() {
$tables = array();
public function get_tables($usecache=true) {
if ($usecache and $this->tables !== null) {
return $this->tables;
}
$this->tables = array();
$prefix = str_replace('_', '\\\\_', $this->prefix);
$sql = "SELECT tablename
FROM pg_catalog.pg_tables
@ -245,11 +248,11 @@ class pgsql_native_moodle_database extends moodle_database {
continue;
}
$tablename = substr($tablename, strlen($this->prefix));
$tables[$tablename] = $tablename;
$this->tables[$tablename] = $tablename;
}
pg_free_result($result);
}
return $tables;
return $this->tables;
}
/**
@ -493,7 +496,7 @@ class pgsql_native_moodle_database extends moodle_database {
* @throws dml_exception if error
*/
public function change_database_structure($sql) {
$this->reset_columns();
$this->reset_caches();
$this->query_start($sql, null, SQL_QUERY_STRUCTURE);
$result = pg_query($this->pgsql, $sql);

View File

@ -1964,7 +1964,7 @@ class moodle_database_for_testing extends moodle_database {
public function get_server_info(){}
protected function allowed_param_types(){}
public function get_last_error(){}
public function get_tables(){}
public function get_tables($usecache=true){}
public function get_indexes($table){}
public function get_columns($table, $usecache=true){}
public function set_debug($state){}

View File

@ -108,7 +108,7 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
* Return tables in database WITHOUT current prefix
* @return array of table names in lowercase and without prefix
*/
public function get_tables() {
public function get_tables($usecache=true) {
$tables = array();
$sql = 'SELECT name FROM sqlite_master WHERE type="table" UNION ALL SELECT name FROM sqlite_temp_master WHERE type="table" ORDER BY name';