function execute_query($query, $dblink) {
global $CONFIG, $dbcalls;
- // remove newlines so logs are easier to read
- $query = preg_replace("/[\r\n]/", "", $query);
if ($query == NULL) {
throw new DatabaseException(elgg_echo('DatabaseException:InvalidQuery'));
}
/**
* Handles returning data from a query, running it through a callback function,
- * and caching the results.
+ * and caching the results. This is for R queries (from CRUD).
*
* @access private
*
function elgg_query_runner($query, $callback = null, $single = false) {
global $CONFIG, $DB_QUERY_CACHE;
+ $query = elgg_format_query($query);
+
// since we want to cache results of running the callback, we need to
// need to namespace the query with the callback, and single result request.
$hash = (string)$callback . (string)$single . $query;
$cached_query = $DB_QUERY_CACHE[$hash];
if ($cached_query !== FALSE) {
- elgg_log("$query results returned from cache (hash: $hash)", 'NOTICE');
+ elgg_log("DB query $query results returned from cache (hash: $hash)", 'NOTICE');
return $cached_query;
}
}
}
if (empty($return)) {
- elgg_log("DB query \"$query\" returned no results.", 'NOTICE');
+ elgg_log("DB query $query returned no results.", 'NOTICE');
}
// Cache result
if ($DB_QUERY_CACHE) {
$DB_QUERY_CACHE[$hash] = $return;
- elgg_log("$query results cached (hash: $hash)", 'NOTICE');
+ elgg_log("DB query $query results cached (hash: $hash)", 'NOTICE');
}
return $return;
function insert_data($query) {
global $CONFIG, $DB_QUERY_CACHE;
+ $query = elgg_format_query($query);
+ elgg_log("DB query $query", 'NOTICE');
+
$dblink = get_db_link('write');
// Invalidate query cache
function update_data($query) {
global $CONFIG, $DB_QUERY_CACHE;
+ $query = elgg_format_query($query);
+ elgg_log("DB query $query", 'NOTICE');
+
$dblink = get_db_link('write');
// Invalidate query cache
function delete_data($query) {
global $CONFIG, $DB_QUERY_CACHE;
+ $query = elgg_format_query($query);
+ elgg_log("DB query $query", 'NOTICE');
+
$dblink = get_db_link('write');
// Invalidate query cache
}
}
+/**
+ * Format a query string for logging
+ *
+ * @param string $query Query string
+ * @return string
+ */
+function elgg_format_query($query) {
+ // remove newlines and extra spaces so logs are easier to read
+ return preg_replace('/\s\s+/', ' ', $query);
+}
+
/**
* Sanitise a string for database use, but with the option of escaping extra characters.
*