]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-14679 temporary tables: modify approach so name changes are
authorstronk7 <stronk7>
Mon, 28 Sep 2009 18:10:07 +0000 (18:10 +0000)
committerstronk7 <stronk7>
Mon, 28 Sep 2009 18:10:07 +0000 (18:10 +0000)
now phisically stored and not only calculated on the fly. Oracle
will need that.

lib/dml/moodle_temptables.php
lib/dml/mssql_native_moodle_database.php
lib/dml/mssql_native_moodle_temptables.php

index 3f358fc7f9579446e68c0990a388578617b63e9f..9a524e63d623ba9f55189e672f2e2cd91aeec1f3 100644 (file)
@@ -46,7 +46,7 @@ class moodle_temptables {
 
     protected $mdb;        // circular reference, to be able to use DB facilities here if needed
     protected $prefix;     // prefix to be used for all the DB objects
-    protected $temptables; // simple array of 'tablename' => 'tablename'
+    protected $temptables; // simple array of moodle, not prefixed 'tablename' => DB, final (prefixed) 'tablename'
 
     /**
      * Creates new moodle_temptables instance
@@ -61,6 +61,12 @@ class moodle_temptables {
     /**
      * Add one temptable to the store
      *
+     * Given one moodle temptable name (without prefix), add it to the store, with the
+     * key being the original moodle name and the value being the real db temptable name
+     * already prefixed
+     *
+     * Override and use this *only* if the database requires modification in the table name.
+     *
      * @param string $tablename name without prefix of the table created as temptable
      */
     public function add_temptable($tablename) {
@@ -84,7 +90,7 @@ class moodle_temptables {
      * @return array containing all the tablenames in the store (tablename both key and value)
      */
     public function get_temptables() {
-        return $this->temptables;
+        return array_keys($this->temptables);
     }
 
     /**
@@ -93,19 +99,21 @@ class moodle_temptables {
      * @param string $tablename name without prefix of the table we are asking about
      * @return bool true if the table is a temp table (based in the store info), false if not
      */
-    function is_temptable ($tablename) {
+    public function is_temptable($tablename) {
         return !empty($this->temptables[$tablename]);
     }
 
     /**
      * Given one tablename (no prefix), return the name of the corresponding temporary table,
-     * usually the same name but some databases could require changes (like '#' prefix in mssql)
-     *
-     * Override and use this *only* if the database requires modification in the table name.
+     * If the table isn't a "registered" temp table, returns null
      *
-     * @param string $tablename name without prefix which corresponding temp tablename nees to calculate
+     * @param string $tablename name without prefix which corresponding temp tablename needs to know
+     * @return mixed DB name of the temp table or null if it isn't a temp table
      */
     public function get_correct_name($tablename) {
-        return $this->prefix . $tablename; // No change in name is the usual behaviour
+        if ($this->is_temptable($tablename)) {
+            return $this->temptables[$tablename];
+        }
+        return null;
     }
 }
index 8b2d257872a9822a71d613c88903efe17746350c..fac9a8ecd013ba1b6dbe5c9946e8ae9ed1f655ba 100644 (file)
@@ -312,7 +312,11 @@ class mssql_native_moodle_database extends moodle_database {
         if (preg_match_all('/\{([a-z][a-z0-9_]*)\}/', $sql, $matches)) {
             foreach($matches[0] as $key=>$match) {
                 $name = $matches[1][$key];
-                $sql = str_replace($match, $this->temptables->get_correct_name($name), $sql);
+                if ($this->temptables->is_temptable($name)) {
+                    $sql = str_replace($match, $this->temptables->get_correct_name($name), $sql);
+                } else {
+                    $sql = str_replace($match, $this->prefix.$name, $sql);
+                }
             }
         }
         return $sql;
index b29634cdb70570e24ab4b93587cea685a788e856..39825c2cc912eff1c420f0070ca3ac18cba6388b 100644 (file)
@@ -31,18 +31,22 @@ require_once($CFG->libdir.'/dml/moodle_temptables.php');
 class mssql_native_moodle_temptables extends moodle_temptables {
 
     /**
-     * Override the method to return the correct real name (prefix = '#') of
-     * temporary mssql databases. Widely used in the corresponding mssql sql
-     * generator and database driver
+     * Add one temptable to the store.
      *
-     * @param string $tablename name without prefix which corresponding temp tablename nees to calculate
+     * Overriden because MSSQL requires to add # for local (session) temporary
+     * tables before the prefix.
+     *
+     * Given one moodle temptable name (without prefix), add it to the store, with the
+     * key being the original moodle name and the value being the real db temptable name
+     * already prefixed
+     *
+     * Override and use this *only* if the database requires modification in the table name.
+     *
+     * @param string $tablename name without prefix of the table created as temptable
      */
-    public function get_correct_name($tablename) {
-        // TODO: throw exception if not exists
-        if (!empty($this->temptables[$tablename])) {
-            return '#' . $this->prefix . $tablename;
-        } else {
-            return $this->prefix . $tablename;
-        }
+    public function add_temptable($tablename) {
+        // TODO: throw exception if exists: if ($this->is_temptable...
+        $this->temptables[$tablename] = '#' . $this->prefix . $tablename;
     }
+
 }