From: skodak Date: Mon, 12 Jan 2009 18:10:50 +0000 (+0000) Subject: MDL-17859 implemented caching in get_tables() X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=117679db37186b0c292cf8ac09986507632315e5;p=moodle.git MDL-17859 implemented caching in get_tables() --- diff --git a/lib/ddl/mysql_sql_generator.php b/lib/ddl/mysql_sql_generator.php index cf99b7b5bf..98a8551a46 100644 --- a/lib/ddl/mysql_sql_generator.php +++ b/lib/ddl/mysql_sql_generator.php @@ -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(); } diff --git a/lib/dml/adodb_moodle_database.php b/lib/dml/adodb_moodle_database.php index 7d11b5cf8c..6ea73e3cd0 100644 --- a/lib/dml/adodb_moodle_database.php +++ b/lib/dml/adodb_moodle_database.php @@ -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); diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 5e72d65d34..833b341b34 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -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; } /** diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index 0ae3082618..8934f2727d 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -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); diff --git a/lib/dml/oci_native_moodle_database.php b/lib/dml/oci_native_moodle_database.php index 8291877f65..d4adcad881 100644 --- a/lib/dml/oci_native_moodle_database.php +++ b/lib/dml/oci_native_moodle_database.php @@ -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); diff --git a/lib/dml/pdo_moodle_database.php b/lib/dml/pdo_moodle_database.php index 0714ab9420..647544d055 100644 --- a/lib/dml/pdo_moodle_database.php +++ b/lib/dml/pdo_moodle_database.php @@ -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(); diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 7259b91954..70884332fb 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -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); diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php index b6d64b9a06..de18602e2e 100755 --- a/lib/dml/simpletest/testdml.php +++ b/lib/dml/simpletest/testdml.php @@ -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){} diff --git a/lib/dml/sqlite3_pdo_moodle_database.php b/lib/dml/sqlite3_pdo_moodle_database.php index 3633898f16..c97d458e2b 100644 --- a/lib/dml/sqlite3_pdo_moodle_database.php +++ b/lib/dml/sqlite3_pdo_moodle_database.php @@ -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';