Skip to content

Commit

Permalink
MDL-15181 temp table support in ddl/dml
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 7, 2008
1 parent b922e86 commit 21a7e26
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/ddl/mysql_sql_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function __construct($mdb) {
* @return boolean true/false
*/
public function table_exists($table, $temptable=false) {
global $CFG;

if (!$temptable) {
return parent::table_exists($table, $temptable);
}
Expand All @@ -99,16 +101,38 @@ public function table_exists($table, $temptable=false) {
$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?
if ($this->mdb->execute("DESCRIBE {".$tablename."}") === 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) {
$sqlarr = $this->getCreateTableSQL($xmldb_table);
$sqlarr = preg_replace('/^CREATE TABLE (.*)/s', 'CREATE TEMPORARY TABLE $1 TYPE=MyISAM', $sqlarr);
return $sqlarr;
}

/**
* Given one correct xmldb_table and the new name, returns the SQL statements
* to drop it (inside one array)
Expand Down
2 changes: 1 addition & 1 deletion lib/ddl/oracle_sql_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct($mdb) {
*/
public function getCreateTempTableSQL($xmldb_table) {
$sqlarr = $this->getCreateTableSQL($xmldb_table);
$sqlarr = preg_replace('/^CREATE TABLE/', "CREATE GLOBAL TEMPORARY TABLE", $sqlarr);
$sqlarr = preg_replace('/^CREATE TABLE (.*)/s', 'CREATE GLOBAL TEMPORARY TABLE $1 ON COMMIT PRESERVE ROWS', $sqlarr);
return $sqlarr;
}

Expand Down
1 change: 1 addition & 0 deletions lib/ddl/sql_generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function table_exists($table, $temptable=false) {
/// get all tables in moodle database
$tables = $this->mdb->get_tables();
$exists = in_array($tablename, $tables);

/// Re-set original debug
$this->mdb->set_debug($olddbdebug);

Expand Down

0 comments on commit 21a7e26

Please sign in to comment.