Skip to content

Commit

Permalink
MDL-15181 temporary tables support. No more differences in table_exis…
Browse files Browse the repository at this point in the history
…ts() +

implemented temptables store for mysql.
  • Loading branch information
stronk7 committed Aug 31, 2009
1 parent ed996ed commit 4ff402d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 61 deletions.
9 changes: 4 additions & 5 deletions lib/ddl/database_manager.php
Expand Up @@ -93,14 +93,13 @@ protected function execute_sql($sql) {
* 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);
}

/**
Expand Down Expand Up @@ -495,7 +494,7 @@ public function create_table(xmldb_table $xmldb_table) {
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);
}

Expand All @@ -518,7 +517,7 @@ public function create_temp_table(xmldb_table $xmldb_table) {
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());
}

Expand Down
3 changes: 1 addition & 2 deletions lib/ddl/mssql_sql_generator.php
Expand Up @@ -138,8 +138,7 @@ public function getCreateTempTableSQL($xmldb_table) {
*/
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;
}

Expand Down
57 changes: 6 additions & 51 deletions lib/ddl/mysql_sql_generator.php
Expand Up @@ -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);
}

Expand All @@ -101,61 +104,12 @@ public function getResetSequenceSQL($table) {
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;
Expand All @@ -168,6 +122,7 @@ public function getCreateTempTableSQL($xmldb_table) {
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;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ddl/simpletest/testddl.php
Expand Up @@ -1254,7 +1254,7 @@ public function test_temp_tables() {
// 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'];
Expand Down
3 changes: 1 addition & 2 deletions lib/ddl/sql_generator.php
Expand Up @@ -162,10 +162,9 @@ public function getEndedStatements($input) {
* 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 {
Expand Down

0 comments on commit 4ff402d

Please sign in to comment.