/** * Add a query to the internal query log. Only works if the * 'logging' config option is set to true. * * This works by manually binding the parameters to the query - the * query isn't executed like this (PDO normally passes the query and * parameters to the database which takes care of the binding) but * doing it this way makes the logged queries more readable. * @param string $query * @param array $parameters An array of parameters to be bound in to the query * @param string $connection_name Which connection to use * @param float $query_time Query time * @return bool */ protected static function _logQuery($query, $parameters, $connection_name, $query_time) { // If logging is not enabled, do nothing if (!static::$_config[$connection_name]['logging']) { return false; } if (!isset(static::$_query_log[$connection_name])) { static::$_query_log[$connection_name] = array(); } static::$_last_query = array('unbound' => $query, 'parameters' => $parameters); $parameters_with_non_int_keys = array(); //holds named parameters // Strip out any non-integer indexes from the parameters foreach ($parameters as $key => $value) { if (!is_int($key)) { $parameters_with_non_int_keys[$key] = $value; unset($parameters[$key]); } } if (count($parameters) > 0 && count($parameters_with_non_int_keys) <= 0) { //Deal with only question mark place holders // Escape the parameters $parameters = array_map(array(static::getDb($connection_name), 'quote'), $parameters); // Avoid %format collision for vsprintf $query = str_replace("%", "%%", $query); // Replace placeholders in the query for vsprintf if (false !== strpos($query, "'") || false !== strpos($query, '"')) { $query = StringHelper::strReplaceOutsideQuotes("?", "%s", $query); } else { $query = str_replace("?", "%s", $query); } // Replace the question marks in the query with the parameters $bound_query = vsprintf($query, $parameters); } else { if (count($parameters_with_non_int_keys) > 0 && count($parameters) <= 0) { //Deal with only named place holders // Escape the parameters $parameters_with_non_int_keys = array_map(array(static::getDb($connection_name), 'quote'), $parameters_with_non_int_keys); // Avoid %format collision for vsprintf $query = str_replace("%", "%%", $query); $re_indexed_parameters_with_non_int_keys_for_vsprintf = array(); foreach ($parameters_with_non_int_keys as $key => $value) { $new_index = strpos($query, ":{$key}"); if ($new_index !== false) { $re_indexed_parameters_with_non_int_keys_for_vsprintf[$new_index] = $value; } // Replace placeholders in the query for vsprintf if (false !== strpos($query, "'") || false !== strpos($query, '"')) { $query = StringHelper::strReplaceOutsideQuotes(":{$key}", "%s", $query); } else { $query = str_replace(":{$key}", "%s", $query); } } // Replace the named placeholders in the query with the parameters $bound_query = vsprintf($query, $re_indexed_parameters_with_non_int_keys_for_vsprintf); } else { //Either no parameters to bind to query (which is good) or there are //mixed types of parameters (ie. named and question marked in the //same query which will eventually lead to a PDO Exception). $bound_query = $query; } } static::$_last_query['bound'] = $bound_query; static::$_query_log[$connection_name][] = static::$_last_query; if (is_callable(static::$_config[$connection_name]['logger'])) { $logger = static::$_config[$connection_name]['logger']; $logger("Bound Query:" . PHP_EOL . static::$_last_query['bound'], "Unbound Query:" . PHP_EOL . static::$_last_query['unbound'], "Query Parameters:" . PHP_EOL . var_export(static::$_last_query['parameters'], true), $query_time); } return true; }
/** * Add a query to the internal query log. Only works if the * 'logging' config option is set to true. * * This works by manually binding the parameters to the query - the * query isn't executed like this (PDO normally passes the query and * parameters to the database which takes care of the binding) but * doing it this way makes the logged queries more readable. * * @param string $query * @param array $parameters An array of parameters to be bound in to the query * @param string $connection_name Which connection to use * @param float $query_time Query time * * @return bool */ protected static function _log_query($query, $parameters, $connection_name, $query_time) { // If logging is not enabled, do nothing if (!static::$_config[$connection_name]['logging']) { return false; } if (!isset(static::$_query_log[$connection_name])) { static::$_query_log[$connection_name] = array(); } // Strip out any non-integer indexes from the parameters foreach ($parameters as $key => $value) { if (!is_int($key)) { unset($parameters[$key]); } } if (count($parameters) > 0) { // Escape the parameters $parameters = array_map(array(static::get_db($connection_name), 'quote'), $parameters); // Avoid %format collision for vsprintf $query = str_replace('%', '%%', $query); // Replace placeholders in the query for vsprintf if (false !== strpos($query, "'") || false !== strpos($query, '"')) { $query = IdiormString::str_replace_outside_quotes('?', '%s', $query); } else { $query = str_replace('?', '%s', $query); } // Replace the question marks in the query with the parameters $bound_query = vsprintf($query, $parameters); } else { $bound_query = $query; } static::$_last_query = $bound_query; static::$_query_log[$connection_name][] = $bound_query; if (is_callable(static::$_config[$connection_name]['logger'])) { $logger = static::$_config[$connection_name]['logger']; $logger($bound_query, $query_time); } return true; }