]> git.mjollnir.org Git - moodle.git/commitdiff
ddllib: Introducing create_temp_table()
authormartinlanghoff <martinlanghoff>
Sun, 29 Apr 2007 20:31:47 +0000 (20:31 +0000)
committermartinlanghoff <martinlanghoff>
Sun, 29 Apr 2007 20:31:47 +0000 (20:31 +0000)
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

index 033df6a3c12b183204c181aca055d403561f3812..81946bf4730190bd73db231cf2389b2c0f75d943 100644 (file)
@@ -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