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

@ -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,6 +18,11 @@
*/ */
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
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

Binary file not shown.