]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17859 implemented caching in get_tables()
authorskodak <skodak>
Mon, 12 Jan 2009 18:10:50 +0000 (18:10 +0000)
committerskodak <skodak>
Mon, 12 Jan 2009 18:10:50 +0000 (18:10 +0000)
lib/ddl/mysql_sql_generator.php
lib/dml/adodb_moodle_database.php
lib/dml/moodle_database.php
lib/dml/mysqli_native_moodle_database.php
lib/dml/oci_native_moodle_database.php
lib/dml/pdo_moodle_database.php
lib/dml/pgsql_native_moodle_database.php
lib/dml/simpletest/testdml.php
lib/dml/sqlite3_pdo_moodle_database.php

index cf99b7b5bfa45297a81239416f582ac04255a87c..98a8551a4692e927d76009121b9df36af4edc19e 100644 (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();
         }
 
index 7d11b5cf8cde6d829f7c02beb69dcc8876adc9ec..6ea73e3cd013a48017ca1758b9b8a57e7099e5ea 100644 (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);
index 5e72d65d34bf93d515049689d26f84a07e9ada53..833b341b3481c4a93639cad200ccd4faff61a2bf 100644 (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;
     }
 
     /**
index 0ae308261845538397f1aeb47e6cee4ba9913957..8934f2727ded00a5f7fbccc3baad678198ad3754 100644 (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);
index 8291877f6504f7c2df59ab31e246c03ceeb77350..d4adcad881ce1b2cf55046e580d58f08fa9acdd6 100644 (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);
index 0714ab9420f6b2dba5912363ac8346854cb865a3..647544d0558268a52dc9f38ad4f7e817d233107d 100644 (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();
index 7259b9195475233e258bf34fae17d0d06dc2ec6b..70884332fb00c935d2d167d2fc8341d5c5d1d4f3 100644 (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);
index b6d64b9a065a43fc0d9ed05eea75f6555e659397..de18602e2eb172ae6afac7cbd9c9f5ea2b097171 100755 (executable)
@@ -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){}
index 3633898f167a265477d9a3826df132448e09a1f6..c97d458e2be25f1e51baaad865fd75edd76c8a2c 100644 (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';