Skip to content
Snippets Groups Projects
Commit b7051107 authored by Jared Hancock's avatar Jared Hancock
Browse files

mysqli: Allow db_query to callback before logging

Allow a callback function to be used to determine if an error should be
logged and an alert mailed
parent bcbebd0b
No related branches found
No related tags found
No related merge requests found
......@@ -128,7 +128,10 @@ class GenericAttachments {
.',object_id='.db_input($this->getId())
.',file_id='.db_input($fileId)
.',inline='.db_input($inline ? 1 : 0);
if (db_query($sql))
// File may already be associated with the draft (in the
// event it was deleted and re-added)
if (db_query($sql, function($errno) { return $errno != 1062; })
|| db_errno() == 1062)
$i[] = $fileId;
}
}
......
......@@ -121,7 +121,22 @@ function db_create_database($database, $charset='utf8',
$database, $charset, $collate));
}
// execute sql query
/**
* Function: db_query
* Execute sql query
*
* Parameters:
* $query - (string) SQL query (with parameters)
* $logError - (mixed):
* - (bool) true or false if error should be logged and alert email sent
* - (callable) to receive error number and return true or false if
* error should be logged and alert email sent. The callable is only
* invoked if the query fails.
*
* Returns:
* (mixed) MysqliResource if SELECT query succeeds, true if an INSERT,
* UPDATE, or DELETE succeeds, false or null if the query fails.
*/
function db_query($query, $logError=true) {
global $ost, $__db;
......@@ -134,6 +149,10 @@ function db_query($query, $logError=true) {
} while (!$res && --$tries && $__db->errno == 1213);
if(!$res && $logError && $ost) { //error reporting
// Allow $logError() callback to determine if logging is necessary
if (is_callable($logError) && !($logError($__db->errno)))
return $res;
$msg='['.$query.']'."\n\n".db_error();
$ost->logDBError('DB Error #'.db_errno(), $msg);
//echo $msg; #uncomment during debuging or dev.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment