From: stronk7 Date: Mon, 28 Sep 2009 18:10:07 +0000 (+0000) Subject: MDL-14679 temporary tables: modify approach so name changes are X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=4b1ee2b3c4bd0917920b6f420fb1a40d16be9a36;p=moodle.git MDL-14679 temporary tables: modify approach so name changes are now phisically stored and not only calculated on the fly. Oracle will need that. --- diff --git a/lib/dml/moodle_temptables.php b/lib/dml/moodle_temptables.php index 3f358fc7f9..9a524e63d6 100644 --- a/lib/dml/moodle_temptables.php +++ b/lib/dml/moodle_temptables.php @@ -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; } } diff --git a/lib/dml/mssql_native_moodle_database.php b/lib/dml/mssql_native_moodle_database.php index 8b2d257872..fac9a8ecd0 100644 --- a/lib/dml/mssql_native_moodle_database.php +++ b/lib/dml/mssql_native_moodle_database.php @@ -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; diff --git a/lib/dml/mssql_native_moodle_temptables.php b/lib/dml/mssql_native_moodle_temptables.php index b29634cdb7..39825c2cc9 100644 --- a/lib/dml/mssql_native_moodle_temptables.php +++ b/lib/dml/mssql_native_moodle_temptables.php @@ -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; } + }