Author: Jeff Roberson (ridgerunner@fluxbb.org)
Author: Simon Holywell (treffynnon@php.net)
Example #1
0
 /**
  * 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 (!self::$_config[$connection_name]['logging']) {
         return false;
     }
     if (!isset(self::$_query_log[$connection_name])) {
         self::$_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(self::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;
     }
     self::$_last_query = $bound_query;
     self::$_query_log[$connection_name][] = $bound_query;
     if (is_callable(self::$_config[$connection_name]['logger'])) {
         $logger = self::$_config[$connection_name]['logger'];
         $logger($bound_query, $query_time);
     }
     return true;
 }
Example #2
0
 /**
  * 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.
  */
 protected static function _log_query($query, $parameters)
 {
     // If logging is not enabled, do nothing
     if (!self::$_config['logging']) {
         return false;
     }
     if (count($parameters) > 0) {
         // Escape the parameters
         $parameters = array_map(array(self::$_db, '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;
     }
     self::$_last_query = $bound_query;
     self::$_query_log[] = $bound_query;
     return true;
 }