From a4b6d764dd26c897d3588502791d9a749f0d0de0 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Sun, 29 Apr 2007 20:31:47 +0000 Subject: [PATCH] ddllib: Introducing create_temp_table() Close sibling to create_table() - will create a temporary table. Two aspects of its usage are interesting - you _must_ truncate the table after creation as you may be "inheriting" an existing temp table from the former user of your persistent connection - you must use the tablename returned to support MSSQL and any other DB that needs special names for temp tables A couple of TODOs - I just don't know how to actually munge the tablename for MSSQL (help! Eloooooyyyy!) - Surely Eloy will have something better than the preg_replace() ;-) --- lib/ddllib.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/lib/ddllib.php b/lib/ddllib.php index 033df6a3c1..81946bf473 100644 --- a/lib/ddllib.php +++ b/lib/ddllib.php @@ -615,6 +615,67 @@ function drop_table($table, $continue=true, $feedback=true) { return execute_sql_arr($sqlarr, $continue, $feedback); } +/** + * This function will create the temporary table passed as argument with all its + * fields/keys/indexes/sequences, everything based in the XMLDB object + * + * TRUNCATE the table immediately after creation. A previous process using + * the same persistent connection may have created the temp table and failed to + * drop it. In that case, the table will exist, and create_temp_table() will + * will succeed. + * + * NOTE: The return value is the tablename - some DBs (MSSQL at least) use special + * names for temp tables. + * + * @uses $CFG, $db + * @param XMLDBTable table object (full specs are required) + * @param boolean continue to specify if must continue on error (true) or stop (false) + * @param boolean feedback to specify to show status info (true) or not (false) + * @return string tablename on success, false on error + */ +function create_temp_table($table, $continue=true, $feedback=true) { + + global $CFG, $db; + + $status = true; + + if (strtolower(get_class($table)) != 'xmldbtable') { + return false; + } + + + $temporary = 'TEMPORARY'; + switch (strtolower($CFG->dbfamily)) { + case 'mssql': + // TODO: somehow change the name to have a # + $temporary = ''; + break; + case 'oracle': + $temporary = 'GLOBAL TEMPORARY'; + break; + } + +/// Check table doesn't exist + if (table_exists($table)) { + debugging('Table ' . $table->getName() . ' exists. Create skipped', DEBUG_DEVELOPER); + return $table->getName(); //Table exists, nothing to do + } + + if(!$sqlarr = $table->getCreateTableSQL($CFG->dbtype, $CFG->prefix, false)) { + return $table->getName(); //Empty array = nothing to do = no error + } + + if (!empty($temporary)) { + $sqlarr = preg_replace('/^CREATE/', "CREATE $temporary", $sqlarr); + } + + if (execute_sql_arr($sqlarr, $continue, $feedback)) { + return $table->getName(); + } else { + return false; + } +} + /** * This function will rename the table passed as argument * Before renaming the index, the function will check it exists -- 2.39.5