]> git.mjollnir.org Git - moodle.git/commitdiff
IMPORTANT! CRUCIAL CHANGE TO MAJOR FUNCTION!
authormoodler <moodler>
Thu, 29 Jan 2004 15:27:21 +0000 (15:27 +0000)
committermoodler <moodler>
Thu, 29 Jan 2004 15:27:21 +0000 (15:27 +0000)
Petri Asikainen rewrote insert_record to use ADOdb functions.  It might
be very slightly slower but it gets rid of all the horrible not-quite-working
PostgreSQL workarounds the old one contained.

It worked for him on PostgreSQL 7.4 and for me on MySQL 3.23 and 4.0.15,
so I'm checking it in.

Please test it thoroughly on your test systems.

Since it writes data it has the potential to stuff things up, so be
careful on production systems for a few days.

lib/datalib.php

index af126b19a708faa15a428cccdebae4bd20509cb5..c7329f4b51a312f763e2d1d42d6d6b896dda85b6 100644 (file)
@@ -746,6 +746,48 @@ function delete_records_select($table, $select="") {
 * @param       type description
 */
 function insert_record($table, $dataobject, $returnid=true) {
+                                                                                                                
+    global $db, $CFG;
+
+    // Get empty record from table
+    $infosql = "SELECT * FROM $CFG->prefix$table WHERE id ='-1'";
+
+    // Execute the query and get the empty recordset
+    $rs = $db->Execute($infosql);
+                                                                                                                
+    // Convert data to array to hold the record data to insert
+    $record = (array)$dataobject;
+                                                                                                                
+    //Get insertsql from adodb
+    $insertSQL = $db->GetInsertSQL($rs, $record);
+                                                                                                                
+    if (! $rs = $db->Execute($insertSQL)) {
+        return false;
+    }
+                                                                                                                
+    if (!$returnid) {   // Return ID is not needed so just finish here.
+        return true;
+    }
+
+    switch ($CFG->dbtype) {
+        case "postgres7":
+            $oid = $db->Insert_ID();
+            if ($rs = $db->Execute("SELECT id FROM $CFG->prefix$table WHERE oid = $oid")) {
+                // every table needs to have a primary field named 'id' for this to work
+                if ($rs->RecordCount() == 1) {
+                    return $rs->fields[0];
+                }
+            }
+            return false;
+
+        default: 
+            return $db->Insert_ID();   // Should work on most databases, but not all!
+    }
+}
+
+
+function insert_record_old($table, $dataobject, $returnid=true) {
+// Will be deleted soon if there are no problems with the new one
 
     global $db, $CFG;