? MDL-22076.patch.txt ? dml/pgsql_native_moodle_temptables.php Index: ddl/mssql_sql_generator.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/ddl/mssql_sql_generator.php,v retrieving revision 1.22 diff -u -r1.22 mssql_sql_generator.php --- ddl/mssql_sql_generator.php 21 Mar 2010 19:36:50 -0000 1.22 +++ ddl/mssql_sql_generator.php 13 Apr 2010 10:31:32 -0000 @@ -72,16 +72,6 @@ public $rename_key_sql = null; //SQL sentence to rename one key //TABLENAME, OLDKEYNAME, NEWKEYNAME are dinamically replaced - private $temptables; // Control existing temptables (mssql_native_moodle_temptables object) - - /** - * Creates one new XMLDBmssql - */ - public function __construct($mdb, $temptables = null) { - $this->temptables = $temptables; - parent::__construct($mdb); - } - /** * Reset a sequence to the id field of a table. * @param string $table name of table or xmldb_table object Index: ddl/mysql_sql_generator.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/ddl/mysql_sql_generator.php,v retrieving revision 1.19 diff -u -r1.19 mysql_sql_generator.php --- ddl/mysql_sql_generator.php 1 Nov 2009 11:35:28 -0000 1.19 +++ ddl/mysql_sql_generator.php 13 Apr 2010 10:31:32 -0000 @@ -75,16 +75,6 @@ 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, $temptables = null) { - $this->temptables = $temptables; - parent::__construct($mdb); - } - /** * Reset a sequence to the id field of a table. * @param string $table name of table or xmldb_table object @@ -120,7 +110,7 @@ * to drop it (inside one array) */ public function getDropTempTableSQL($xmldb_table) { - $sqlarr = parent::getDropTableSQL($xmldb_table); + $sqlarr = $this->getDropTableSQL($xmldb_table); $sqlarr = preg_replace('/^DROP TABLE/', "DROP TEMPORARY TABLE", $sqlarr); $this->temptables->delete_temptable($xmldb_table->getName()); return $sqlarr; Index: ddl/oracle_sql_generator.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/ddl/oracle_sql_generator.php,v retrieving revision 1.23 diff -u -r1.23 oracle_sql_generator.php --- ddl/oracle_sql_generator.php 18 Mar 2010 01:50:32 -0000 1.23 +++ ddl/oracle_sql_generator.php 13 Apr 2010 10:31:32 -0000 @@ -58,16 +58,6 @@ public $alter_column_sql = 'ALTER TABLE TABLENAME MODIFY (COLUMNSPECS)'; //The SQL template to alter columns - private $temptables; // Control existing temptables (oci_native_moodle_temptables object) - - /** - * Creates one new XMLDBoci8po - */ - public function __construct($mdb, $temptables = null) { - $this->temptables = $temptables; - parent::__construct($mdb); - } - /** * Reset a sequence to the id field of a table. * @param string $table name of table or xmldb_table object Index: ddl/postgres_sql_generator.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/ddl/postgres_sql_generator.php,v retrieving revision 1.15 diff -u -r1.15 postgres_sql_generator.php --- ddl/postgres_sql_generator.php 1 Nov 2009 11:35:28 -0000 1.15 +++ ddl/postgres_sql_generator.php 13 Apr 2010 10:31:32 -0000 @@ -55,13 +55,6 @@ //TABLENAME, OLDKEYNAME, NEWKEYNAME are dinamically replaced /** - * Creates one new XMLDBpostgres7 - */ - public function __construct($mdb) { - parent::__construct($mdb); - } - - /** * Reset a sequence to the id field of a table. * @param string $table name of table or xmldb_table object * @return array sql commands to execute @@ -81,6 +74,27 @@ } /** + * 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/', "CREATE TEMPORARY TABLE", $sqlarr); + return $sqlarr; + } + + /** + * Given one correct xmldb_table and the new name, returns the SQL statements + * to drop it (inside one array) + */ + public function getDropTempTableSQL($xmldb_table) { + $sqlarr = $this->getDropTableSQL($xmldb_table); + $this->temptables->delete_temptable($xmldb_table->getName()); + return $sqlarr; + } + + /** * Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type */ public function getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null) { Index: ddl/sql_generator.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/ddl/sql_generator.php,v retrieving revision 1.27 diff -u -r1.27 sql_generator.php --- ddl/sql_generator.php 22 Mar 2010 09:27:35 -0000 1.27 +++ ddl/sql_generator.php 13 Apr 2010 10:31:32 -0000 @@ -125,14 +125,17 @@ public $mdb; + protected $temptables; // Control existing temptables + /** * Creates new sql_generator * @param object moodle_database instance */ - public function __construct($mdb) { + public function __construct($mdb, $temptables = null) { $this->prefix = $mdb->get_prefix(); $this->reserved_words = $this->getReservedWords(); $this->mdb = $mdb; // this creates circular reference - the other link must be unset when closing db + $this->temptables = $temptables; } /** @@ -180,13 +183,6 @@ } /** - * Reset a sequence to the id field of a table. - * @param string $table name of table - * @return success - */ - public abstract function getResetSequenceSQL($tablename); - - /** * This function will return the SQL code needed to create db tables and statements */ public function getCreateStructureSQL($xmldb_structure) { @@ -360,16 +356,6 @@ } /** - * Given one correct xmldb_table, returns the SQL statements - * to create temporary table (inside one array) - */ - public function getCreateTempTableSQL($xmldb_table) { - $sqlarr = $this->getCreateTableSQL($xmldb_table); - $sqlarr = preg_replace('/^CREATE TABLE/', "CREATE TEMPORARY TABLE", $sqlarr); - return $sqlarr; - } - - /** * Given one correct xmldb_index, returns the SQL statements * needed to create it (in array) */ @@ -604,14 +590,6 @@ } /** - * Given one correct xmldb_table and the new name, returns the SQL statements - * to drop it (inside one array) - */ - public function getDropTempTableSQL($xmldb_table) { - return $this->getDropTableSQL($xmldb_table); - } - - /** * Given one xmldb_table and one xmldb_field, return the SQL statements needded to add the field to the table */ public function getAddFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL) { @@ -1130,6 +1108,25 @@ /// ALL THESE FUNCTION MUST BE CUSTOMISED BY ALL THE XMLDGenerator classes /** + * Reset a sequence to the id field of a table. + * @param string $table name of table + * @return success + */ + public abstract function getResetSequenceSQL($tablename); + + /** + * Given one correct xmldb_table, returns the SQL statements + * to create temporary table (inside one array) + */ + abstract public function getCreateTempTableSQL($xmldb_table); + + /** + * Given one correct xmldb_table and the new name, returns the SQL statements + * to drop it (inside one array) + */ + abstract public function getDropTempTableSQL($xmldb_table); + + /** * Given one XMLDB Type, lenght and decimals, returns the DB proper SQL type */ public abstract function getTypeSQL($xmldb_type, $xmldb_length=null, $xmldb_decimals=null); Index: dml/moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/moodle_database.php,v retrieving revision 1.109 diff -u -r1.109 moodle_database.php --- dml/moodle_database.php 22 Mar 2010 18:06:59 -0000 1.109 +++ dml/moodle_database.php 13 Apr 2010 10:31:33 -0000 @@ -63,6 +63,8 @@ /** @var database_manager db manager which allows db structure modifications */ protected $database_manager; + /** @var moodle_temptables temptables manager to provide cross-db support for temp tables */ + protected $temptables; /** @var array cache of column info */ protected $columns = array(); // I wish we had a shared memory cache for this :-( /** @var array cache of table info */ @@ -300,6 +302,10 @@ session_get_instance()->write_close(); $this->used_for_db_sessions = false; } + if ($this->temptables) { + $this->temptables->dispose(); + $this->temptables = null; + } if ($this->database_manager) { $this->database_manager->dispose(); $this->database_manager = null; @@ -787,7 +793,7 @@ $classname = $this->get_dbfamily().'_sql_generator'; require_once("$CFG->libdir/ddl/$classname.php"); - $generator = new $classname($this); + $generator = new $classname($this, $this->temptables); $this->database_manager = new database_manager($this, $generator); } Index: dml/moodle_temptables.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/moodle_temptables.php,v retrieving revision 1.2 diff -u -r1.2 moodle_temptables.php --- dml/moodle_temptables.php 28 Sep 2009 18:10:07 -0000 1.2 +++ dml/moodle_temptables.php 13 Apr 2010 10:31:33 -0000 @@ -116,4 +116,19 @@ } return null; } + + /** + * Dispose the temptables stuff, checking for wrong situations, informing and recovering from them + */ + public function dispose() { + // We shouldn't have any temp table registered at the end of the script. + // So we error_log that and, at the same time, drop all the pending temptables + if ($temptables = $this->get_temptables()) { + error_log('Potential coding error - existing temptables found when dispossing database. Must be dropped!'); + foreach ($temptables as $temptable) { + $this->mdb->get_manager()->drop_temp_table(new xmldb_table($temptable)); + } + } + $this->mdb = null; + } } Index: dml/mssql_native_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/mssql_native_moodle_database.php,v retrieving revision 1.16 diff -u -r1.16 mssql_native_moodle_database.php --- dml/mssql_native_moodle_database.php 25 Mar 2010 11:17:01 -0000 1.16 +++ dml/mssql_native_moodle_database.php 13 Apr 2010 10:31:33 -0000 @@ -35,7 +35,6 @@ class mssql_native_moodle_database extends moodle_database { protected $mssql = null; - private $temptables; // Control existing temptables (mssql_moodle_temptables object) protected $last_error_reporting; // To handle mssql driver default verbosity @@ -88,29 +87,6 @@ } /** - * Returns sql generator used for db manipulation. - * Used mostly in upgrade.php scripts. mssql overrides it - * in order to share the mssql_native_moodle_temptables - * between the driver and the generator - * - * @return object database_manager instance - */ - public function get_manager() { - global $CFG; - - if (!$this->database_manager) { - require_once($CFG->libdir.'/ddllib.php'); - - $classname = $this->get_dbfamily().'_sql_generator'; - require_once("$CFG->libdir/ddl/$classname.php"); - $generator = new $classname($this, $this->temptables); - - $this->database_manager = new database_manager($this, $generator); - } - return $this->database_manager; - } - - /** * Returns localised database configuration help. * Note: can be used before connect() * @return string Index: dml/mysqli_native_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/mysqli_native_moodle_database.php,v retrieving revision 1.47 diff -u -r1.47 mysqli_native_moodle_database.php --- dml/mysqli_native_moodle_database.php 7 Nov 2009 08:52:57 -0000 1.47 +++ dml/mysqli_native_moodle_database.php 13 Apr 2010 10:31:33 -0000 @@ -35,7 +35,6 @@ class mysqli_native_moodle_database extends moodle_database { protected $mysqli = null; - private $temptables; // Control existing temptables (mysql_moodle_temptables object) private $transactions_supported = null; @@ -125,29 +124,6 @@ } /** - * Returns sql generator used for db manipulation. - * Used mostly in upgrade.php scripts. mysql overrides it - * in order to share the mysqli_native_moodle_temptables - * between the driver and the generator - * - * @return object database_manager instance - */ - public function get_manager() { - global $CFG; - - if (!$this->database_manager) { - require_once($CFG->libdir.'/ddllib.php'); - - $classname = $this->get_dbfamily().'_sql_generator'; - require_once("$CFG->libdir/ddl/$classname.php"); - $generator = new $classname($this, $this->temptables); - - $this->database_manager = new database_manager($this, $generator); - } - return $this->database_manager; - } - - /** * Returns localised database configuration help. * Note: can be used before connect() * @return string Index: dml/mysqli_native_moodle_temptables.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/mysqli_native_moodle_temptables.php,v retrieving revision 1.1 diff -u -r1.1 mysqli_native_moodle_temptables.php --- dml/mysqli_native_moodle_temptables.php 31 Aug 2009 16:14:44 -0000 1.1 +++ dml/mysqli_native_moodle_temptables.php 13 Apr 2010 10:31:33 -0000 @@ -16,7 +16,7 @@ // along with Moodle. If not, see . /** - * MSSQL specific temptables store. Needed because temporary tables + * MYSQL specific temptables store. Needed because temporary tables * are named diferently than normal tables. Also used to be able to retrieve * temp table names included in the get_tables() method od the DB. * Index: dml/oci_native_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/oci_native_moodle_database.php,v retrieving revision 1.36 diff -u -r1.36 oci_native_moodle_database.php --- dml/oci_native_moodle_database.php 25 Mar 2010 11:17:01 -0000 1.36 +++ dml/oci_native_moodle_database.php 13 Apr 2010 10:31:34 -0000 @@ -38,7 +38,6 @@ class oci_native_moodle_database extends moodle_database { protected $oci = null; - private $temptables; // Control existing temptables (oci_native_moodle_temptables object) private $last_stmt_error = null; // To store stmt errors and enable get_last_error() to detect them private $commit_status = null; // default value initialised in connect method, we need the driver to be present @@ -98,30 +97,6 @@ } /** - * Returns sql generator used for db manipulation. - * Used mostly in upgrade.php scripts. oci overrides it - * in order to share the oci_native_moodle_temptables - * between the driver and the generator - * - * @return object database_manager instance - */ - public function get_manager() { - global $CFG; - - if (!$this->database_manager) { - require_once($CFG->libdir.'/ddllib.php'); - - $classname = $this->get_dbfamily().'_sql_generator'; - require_once("$CFG->libdir/ddl/$classname.php"); - $generator = new $classname($this, $this->temptables); - - $this->database_manager = new database_manager($this, $generator); - } - return $this->database_manager; - } - - - /** * Returns localised database configuration help. * Note: can be used before connect() * @return string Index: dml/pgsql_native_moodle_database.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/dml/pgsql_native_moodle_database.php,v retrieving revision 1.47 diff -u -r1.47 pgsql_native_moodle_database.php --- dml/pgsql_native_moodle_database.php 25 Mar 2010 11:17:01 -0000 1.47 +++ dml/pgsql_native_moodle_database.php 13 Apr 2010 10:31:34 -0000 @@ -27,6 +27,7 @@ require_once($CFG->libdir.'/dml/moodle_database.php'); require_once($CFG->libdir.'/dml/pgsql_native_moodle_recordset.php'); +require_once($CFG->libdir.'/dml/pgsql_native_moodle_temptables.php'); /** * Native pgsql class representing moodle database interface. @@ -181,6 +182,9 @@ throw new dml_connection_exception('Can not read bytea type.'); } + // Connection stabilished and configured, going to instantiate the temptables controller + $this->temptables = new pgsql_native_moodle_temptables($this); + return true; }