]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15181 temporary tables support. No more differences in table_exists() +
authorstronk7 <stronk7>
Mon, 31 Aug 2009 15:47:46 +0000 (15:47 +0000)
committerstronk7 <stronk7>
Mon, 31 Aug 2009 15:47:46 +0000 (15:47 +0000)
implemented temptables store for mysql.

lib/ddl/database_manager.php
lib/ddl/mssql_sql_generator.php
lib/ddl/mysql_sql_generator.php
lib/ddl/simpletest/testddl.php
lib/ddl/sql_generator.php

index 9a10dfdf23b969650075a9c745a2605fa1c6dfdb..9212bf86d3249197754b4fbd78e2979440be7f3b 100644 (file)
@@ -93,14 +93,13 @@ class database_manager {
      * Given one xmldb_table, check if it exists in DB (true/false)
      *
      * @param mixed the table to be searched (string name or xmldb_table instance)
-     * @param bool temp table (might need different checks)
      * @return boolean true/false
      */
-    public function table_exists($table, $temptable=false) {
+    public function table_exists($table) {
         if (!is_string($table) and !($table instanceof xmldb_table)) {
             throw new ddl_exception('ddlunknownerror', NULL, 'incorrect table parameter!');
         }
-        return $this->generator->table_exists($table, $temptable);
+        return $this->generator->table_exists($table);
     }
 
     /**
@@ -495,7 +494,7 @@ class database_manager {
     public function create_temp_table(xmldb_table $xmldb_table) {
 
     /// Check table doesn't exist
-        if ($this->table_exists($xmldb_table, true)) {
+        if ($this->table_exists($xmldb_table)) {
             $this->drop_temp_table($xmldb_table);
         }
 
@@ -518,7 +517,7 @@ class database_manager {
     public function drop_temp_table(xmldb_table $xmldb_table) {
 
     /// Check table doesn't exist
-        if (!$this->table_exists($xmldb_table, true)) {
+        if (!$this->table_exists($xmldb_table)) {
             throw new ddl_table_missing_exception($xmldb_table->getName());
         }
 
index ecafb15bd4f65c6ce266f9a376eae912a1ca7868..02bd2bbef8e6a44efc7715b5d81e6ae5b84a637b 100644 (file)
@@ -138,8 +138,7 @@ class mssql_sql_generator extends sql_generator {
      */
     public function getDropTempTableSQL($xmldb_table) {
         $sqlarr = $this->getDropTableSQL($xmldb_table);
-        $tablename = $xmldb_table->getName();
-        $this->temptables->delete_temptable($tablename);
+        $this->temptables->delete_temptable($xmldb_table->getName());
         return $sqlarr;
     }
 
index 8c74293dcaa3d3b2e6938d3418cd9542941d3abe..cc80d17b9315bc0ce011ae7941685c81aec5510d 100644 (file)
@@ -75,10 +75,13 @@ class mysql_sql_generator extends sql_generator {
     public $rename_key_sql = null; //SQL sentence to rename one key (MySQL doesn't support this!)
                                       //TABLENAME, OLDKEYNAME, NEWKEYNAME are dinamically replaced
 
+    private $temptables; // Control existing temptables (mssql_native_moodle_temptables object)
+
     /**
      * Creates one new XMLDBmysql
      */
-    public function __construct($mdb) {
+    public function __construct($mdb, $temptables = null) {
+        $this->temptables = $temptables;
         parent::__construct($mdb);
     }
 
@@ -101,61 +104,12 @@ class mysql_sql_generator extends sql_generator {
         return array("ALTER TABLE $this->prefix$tablename AUTO_INCREMENT = $value");
     }
 
-
-    /**
-     * Given one xmldb_table, check if it exists in DB (true/false)
-     *
-     * @param mixed the table to be searched (string name or xmldb_table instance)
-     * @param bool temp table (might need different checks)
-     * @return boolean true/false
-     */
-    public function table_exists($table, $temptable=false) {
-        global $CFG;
-
-        if (!$temptable) {
-            return parent::table_exists($table, $temptable);
-        }
-
-        if (is_string($table)) {
-            $tablename = $table;
-        } else {
-        /// Calculate the name of the table
-            $tablename = $table->getName();
-        }
-
-    /// Do this function silenty (to avoid output in install/upgrade process)
-        $olddbdebug = $this->mdb->get_debug();
-        $this->mdb->set_debug(false);
-        $oldcfgdebug = $CFG->debug;
-        $CFG->debug = 0;
-
-        // ugly hack - mysql does not list temporary tables :-(
-        // this polutes the db log with errors :-(
-        // TODO: is there a better way?
-        try {
-            $result = $this->mdb->execute("DESCRIBE {".$tablename."}");
-        } catch (Exception $e) {
-            $result = false;
-        }
-
-        if ($result === false) {
-            $exists = false;
-        } else {
-            $exists = true;
-        }
-
-    /// Re-set original debug
-        $this->mdb->set_debug($olddbdebug);
-        $CFG->debug = $oldcfgdebug;
-
-        return $exists;
-    }
-
     /**
      * Given one correct xmldb_table, returns the SQL statements
      * to create temporary table (inside one array)
      */
     public function getCreateTempTableSQL($xmldb_table) {
+        $this->temptables->add_temptable($xmldb_table->getName());
         $sqlarr = $this->getCreateTableSQL($xmldb_table);
         $sqlarr = preg_replace('/^CREATE TABLE (.*)/s', 'CREATE TEMPORARY TABLE $1 TYPE=MyISAM', $sqlarr);
         return $sqlarr;
@@ -168,6 +122,7 @@ class mysql_sql_generator extends sql_generator {
     public function getDropTempTableSQL($xmldb_table) {
         $sqlarr = parent::getDropTableSQL($xmldb_table);
         $sqlarr = preg_replace('/^DROP TABLE/', "DROP TEMPORARY TABLE", $sqlarr);
+        $this->temptables->delete_temptable($xmldb_table->getName());
         return $sqlarr;
     }
 
index b0e5069ad7b26ede2b7239398234ad58a93b275f..1d840e3ad7f7dc0ac77fb44c53d58e287aaf3f90 100755 (executable)
@@ -1254,7 +1254,7 @@ class ddl_test extends UnitTestCase {
         // Create temp table0
         $table0 = $this->tables['test_table0'];
         $dbman->create_temp_table($table0);
-        $this->assertTrue($dbman->table_exists('test_table0', true));
+        $this->assertTrue($dbman->table_exists('test_table0'));
 
         // Create another temp table1
         $table1 = $this->tables['test_table1'];
index 4e5ec8959a50c00ced7ca8a0b705ea1e5bc95e08..5b13f5fbcbd0df6beb0ce5e89e630071622c2ba5 100644 (file)
@@ -162,10 +162,9 @@ abstract class sql_generator {
      * Given one xmldb_table, check if it exists in DB (true/false)
      *
      * @param mixed the table to be searched (string name or xmldb_table instance)
-     * @param bool temp table (might need different checks)
      * @return boolean true/false
      */
-    public function table_exists($table, $temptable=false) {
+    public function table_exists($table) {
         if (is_string($table)) {
             $tablename = $table;
         } else {