Better query logging

This commit is contained in:
Timothy Warren 2013-02-28 12:22:55 -05:00
parent e10eebb302
commit 6959b614ab
4 changed files with 48 additions and 28 deletions

View File

@ -37,7 +37,7 @@ abstract class DB_PDO extends PDO {
// Last query executed // Last query executed
public $last_query; public $last_query;
// Prefix to apply to table namesa // Prefix to apply to table namesa
public $table_prefix = ''; public $table_prefix = '';
@ -62,7 +62,7 @@ abstract class DB_PDO extends PDO {
$this->util = new $class($this); $this->util = new $class($this);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set additional driver options, if they exist // Set additional driver options, if they exist
if ( ! empty($driver_options) && is_array($driver_options)) if ( ! empty($driver_options) && is_array($driver_options))
{ {
@ -196,21 +196,21 @@ abstract class DB_PDO extends PDO {
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Quote database table name, and set prefix * Quote database table name, and set prefix
* *
* @param string $table * @param string $table
* @return string * @return string
*/ */
public function quote_table($table) public function quote_table($table)
{ {
// If there isn't a prefix set, just quote the table name // If there isn't a prefix set, just quote the table name
if (empty($this->table_prefix)) if (empty($this->table_prefix))
{ {
return $this->quote_ident($table); return $this->quote_ident($table);
} }
// Split indentifier by period, will split into: // Split indentifier by period, will split into:
// database.schema.table OR // database.schema.table OR
// schema.table OR // schema.table OR
@ -218,25 +218,25 @@ abstract class DB_PDO extends PDO {
// table // table
$idents = (array) explode('.', $table); $idents = (array) explode('.', $table);
$segments = count($idents); $segments = count($idents);
// Reference the last item in the split string // Reference the last item in the split string
$last =& $idents[$segments - 1]; $last =& $idents[$segments - 1];
// Quote the last item // Quote the last item
$last = $this->_prefix($last); $last = $this->_prefix($last);
// Rejoin // Rejoin
$table = implode('.', $idents); $table = implode('.', $idents);
// Finally, quote the table // Finally, quote the table
return $this->quote_ident($table); return $this->quote_ident($table);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets the table prefix on the passed string * Sets the table prefix on the passed string
* *
* @param string $str * @param string $str
* @return string * @return string
*/ */
@ -247,10 +247,10 @@ abstract class DB_PDO extends PDO {
{ {
return $str; return $str;
} }
return $this->table_prefix.$str; return $this->table_prefix.$str;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**

View File

@ -116,6 +116,8 @@ class Query_Builder implements iQuery_Builder {
// Instantiate the Query Parser // Instantiate the Query Parser
$this->parser = new Query_Parser(); $this->parser = new Query_Parser();
$this->queries['total_time'] = 0;
// Make things just slightly shorter // Make things just slightly shorter
$this->sql = $this->db->sql; $this->sql = $this->db->sql;
} }
@ -1190,10 +1192,31 @@ class Query_Builder implements iQuery_Builder {
$sql = $this->_compile($type, $table); $sql = $this->_compile($type, $table);
$vals = array_merge($this->values, (array) $this->where_values); $vals = array_merge($this->values, (array) $this->where_values);
$start_time = microtime(TRUE);
$res = ($simple) $res = ($simple)
? $this->db->query($sql) ? $this->db->query($sql)
: $this->db->prepare_execute($sql, $vals); : $this->db->prepare_execute($sql, $vals);
$end_time = microtime(TRUE);
$total_time = number_format($end_time - $start_time, 5);
// Add the interpreted query to the list of executed queries
$esql = str_replace('?', '%s', $sql);
array_unshift($vals, $esql);
$this->queries[] = array(
'time' => $total_time,
'sql' => call_user_func_array('sprintf', $vals),
);
$this->queries['total_time'] += $total_time;
array_shift($vals);
// Set the last query to get rowcounts properly
$this->db->last_query = $sql;
$this->reset_query(); $this->reset_query();
return $res; return $res;
@ -1300,14 +1323,6 @@ class Query_Builder implements iQuery_Builder {
$sql = $this->sql->limit($sql, $this->limit, $this->offset); $sql = $this->sql->limit($sql, $this->limit, $this->offset);
} }
// Add the query to the list of executed queries
$this->queries[] = $sql;
// Set the last query to get rowcounts properly
$this->db->last_query = $sql;
// echo $sql . '<br />';
return $sql; return $sql;
} }
} }

View File

@ -18,16 +18,21 @@
*/ */
abstract class QBTest extends UnitTestCase { abstract class QBTest extends UnitTestCase {
public function __destruct()
{
// echo '<pre>' . print_r($this->db->queries, TRUE) . '</pre>';
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Get Tests // ! Get Tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestQueryFunctionAlias() public function TestQueryFunctionAlias()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
$db = Query(); $db = Query();
$this->assertReference($this->db, $db); $this->assertReference($this->db, $db);
} }

Binary file not shown.