From: moodler Date: Thu, 29 Jan 2004 15:27:21 +0000 (+0000) Subject: IMPORTANT! CRUCIAL CHANGE TO MAJOR FUNCTION! X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=0892f7bd01b49a5ef68f13ca8454b40d8bcea997;p=moodle.git IMPORTANT! CRUCIAL CHANGE TO MAJOR FUNCTION! 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. --- diff --git a/lib/datalib.php b/lib/datalib.php index af126b19a7..c7329f4b51 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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;