From: martinlanghoff Date: Tue, 2 Oct 2007 08:34:10 +0000 (+0000) Subject: dmllib: more consistent err handling for execute_sql() and others X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ba2984bf2be92a51fe053301c75b94ae9f5450f2;p=moodle.git dmllib: more consistent err handling for execute_sql() and others Silent errors in execute_sql() have been causing us lots of grief. Turns out that if you have dblogerror set, it _will_ write it to the logs. DMLlib wasn't consistent. Most "modern" codepaths were calling both debugging() and the dblogerror idiom. So we make all calls to $db->Execute() consistent. (Some exceptions remain, mostly for hardcoded SQL, such as the SET statements when we connect...) --- diff --git a/lib/dmllib.php b/lib/dmllib.php index e8dd30674f..4adf3f2bb4 100644 --- a/lib/dmllib.php +++ b/lib/dmllib.php @@ -86,11 +86,11 @@ function execute_sql($command, $feedback=true) { if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; - $result = $db->Execute($command); + $rs = $db->Execute($command); $db->debug = $olddebug; - if ($result) { + if ($rs) { if ($feedback) { notify(get_string('success'), 'notifysuccess'); } @@ -99,6 +99,8 @@ function execute_sql($command, $feedback=true) { if ($feedback) { notify('' . get_string('error') . ''); } + // these two may go to difference places + debugging($db->ErrorMsg() .'

'. $sql); if (!empty($CFG->dblogerror)) { $debug=array_shift(debug_backtrace()); error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $command"); @@ -1272,7 +1274,16 @@ function set_field_select($table, $newfield, $newvalue, $select, $localcall = fa } /// Arriving here, standard update - return $db->Execute('UPDATE '. $CFG->prefix . $table .' SET '.$update.' '.$select); + $rs = $db->Execute('UPDATE '. $CFG->prefix . $table .' SET '.$update.' '.$select); + if (!$rs) { + debugging($db->ErrorMsg() .'

'. $sql); + if (!empty($CFG->dblogerror)) { + $debug=array_shift(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } + return false; + } + return $rs; } /** @@ -1311,7 +1322,16 @@ function delete_records($table, $field1='', $value1='', $field2='', $value2='', $select = where_clause($field1, $value1, $field2, $value2, $field3, $value3); - return $db->Execute('DELETE FROM '. $CFG->prefix . $table .' '. $select); + $rs = $db->Execute('DELETE FROM '. $CFG->prefix . $table .' '. $select); + if (!$rs) { + debugging($db->ErrorMsg() .'

'. $sql); + if (!empty($CFG->dblogerror)) { + $debug=array_shift(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } + return false; + } + return $rs; } /** @@ -1339,7 +1359,16 @@ function delete_records_select($table, $select='') { $select = 'WHERE '.$select; } - return $db->Execute('DELETE FROM '. $CFG->prefix . $table .' '. $select); + $rs = $db->Execute('DELETE FROM '. $CFG->prefix . $table .' '. $select); + if (!$rs) { + debugging($db->ErrorMsg() .'

'. $sql); + if (!empty($CFG->dblogerror)) { + $debug=array_shift(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } + return false; + } + return $rs; } /** @@ -2029,6 +2058,11 @@ function column_type($table, $column) { if (defined('MDL_PERFDB')) { global $PERF ; $PERF->dbqueries++; }; if(!$rs = $db->Execute('SELECT '.$column.' FROM '.$CFG->prefix.$table.' WHERE 1=2')) { + debugging($db->ErrorMsg() .'

'. $sql); + if (!empty($CFG->dblogerror)) { + $debug=array_shift(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } return false; }