Skip to content

Commit

Permalink
MDL-17859 implemented caching in get_tables()
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 12, 2009
1 parent 1045a00 commit 117679d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/ddl/mysql_sql_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
$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();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/dml/adodb_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function get_server_info() {
* 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);
Expand Down Expand Up @@ -192,7 +192,7 @@ public function get_last_error() {
* @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);
Expand Down
7 changes: 5 additions & 2 deletions lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -246,6 +247,7 @@ public function dispose() {
$this->database_manager = null;
}
$this->columns = array();
$this->tables = null;
}

/**
Expand Down Expand Up @@ -606,7 +608,7 @@ public function fix_sql_params($sql, array $params=null) {
* 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
Expand All @@ -627,8 +629,9 @@ public abstract function get_columns($table, $usecache=true);
* @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;
}

/**
Expand Down
13 changes: 8 additions & 5 deletions lib/dml/mysqli_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,11 @@ public function get_last_error() {
* 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);
Expand All @@ -192,11 +195,11 @@ public function get_tables() {
}
$tablename = substr($tablename, strlen($this->prefix));
}
$tables[$tablename] = $tablename;
$this->tables[$tablename] = $tablename;
}
$result->close();
}
return $tables;
return $this->tables;
}

/**
Expand Down Expand Up @@ -393,7 +396,7 @@ public function setup_is_unicodedb() {
* @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);
Expand Down
4 changes: 2 additions & 2 deletions lib/dml/oci_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected function parse_query($sql) {
* 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
Expand Down Expand Up @@ -516,7 +516,7 @@ public function setup_is_unicodedb() {
* @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);
Expand Down
2 changes: 1 addition & 1 deletion lib/dml/pdo_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function change_database_structure($sql) {
$this->debug_query($sql);
}
$this->pdb->exec($sql);
$this->reset_columns();
$this->reset_caches();
return true;
} catch (PDOException $ex) {
$this->lastError = $ex->getMessage();
Expand Down
13 changes: 8 additions & 5 deletions lib/dml/pgsql_native_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,11 @@ public function get_last_error() {
* 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
Expand All @@ -245,11 +248,11 @@ public function get_tables() {
continue;
}
$tablename = substr($tablename, strlen($this->prefix));
$tables[$tablename] = $tablename;
$this->tables[$tablename] = $tablename;
}
pg_free_result($result);
}
return $tables;
return $this->tables;
}

/**
Expand Down Expand Up @@ -493,7 +496,7 @@ public function setup_is_unicodedb() {
* @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);
Expand Down
2 changes: 1 addition & 1 deletion lib/dml/simpletest/testdml.php
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,7 @@ public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dbop
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){}
Expand Down
2 changes: 1 addition & 1 deletion lib/dml/sqlite3_pdo_moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function get_dbfilepath() {
* 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';
Expand Down

0 comments on commit 117679d

Please sign in to comment.