From a3ff2dc294d87a70d7e2439cac91cac530222623 Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 4 Feb 2008 23:53:26 +0000 Subject: [PATCH] MDL-13038 update_record now works with boolean false; merged from MOODLE_19_STABLE --- lib/dmllib.php | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/dmllib.php b/lib/dmllib.php index 8f885070ba..51885e5994 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -1661,38 +1661,34 @@ function update_record($table, $dataobject) { if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; // Pull out data matching these fields - $ddd = array(); + $update = array(); foreach ($columns as $column) { - if ($column->name <> 'id' and array_key_exists($column->name, $data)) { - $ddd[$column->name] = $data[$column->name]; + if ($column->name == 'id') { + continue; } - } - - // Construct SQL queries - $numddd = count($ddd); - $count = 0; - $update = ''; - -/// Only if we have fields to be updated (this will prevent both wrong updates + -/// updates of only LOBs in Oracle - if ($numddd) { - foreach ($ddd as $key => $value) { - $count++; - if ($value === NULL) { - $update .= $key .' = NULL'; // previously NULLs were not updated + if (array_key_exists($column->name, $data)) { + $key = $column->name; + $value = $data[$key]; + if (is_null($value)) { + $update[] = "$key = NULL"; // previously NULLs were not updated + } else if (is_bool($value)) { + $value = (int)$value; + $update[] = "$key = $value"; // lets keep pg happy, '' is not correct smallint MDL-13038 } else { - $update .= $key .' = \''. $value .'\''; // All incoming data is already quoted - } - if ($count < $numddd) { - $update .= ', '; + $update[] = "$key = '$value'"; // All incoming data is already quoted } } + } - if (!$rs = $db->Execute('UPDATE '. $CFG->prefix . $table .' SET '. $update .' WHERE id = \''. $dataobject->id .'\'')) { - debugging($db->ErrorMsg() .'

UPDATE '. $CFG->prefix . $table .' SET '. s($update) .' WHERE id = \''. $dataobject->id .'\''); +/// Only if we have fields to be updated (this will prevent both wrong updates + +/// updates of only LOBs in Oracle + if ($update) { + $query = "UPDATE {$CFG->prefix}{$table} SET ".implode(',', $update)." WHERE id = {$dataobject->id}"; + if (!$rs = $db->Execute($query)) { + debugging($db->ErrorMsg() .'

'.s($query)); if (!empty($CFG->dblogerror)) { $debug=array_shift(debug_backtrace()); - error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: UPDATE $CFG->prefix$table SET $update WHERE id = '$dataobject->id'"); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $query"); } return false; } -- 2.39.5