From 6959b614ab8d8b23554f7b08e702ecb0c3a7b4e7 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 28 Feb 2013 12:22:55 -0500 Subject: [PATCH] Better query logging --- classes/db_pdo.php | 34 +++++++++++++++++----------------- classes/query_builder.php | 31 +++++++++++++++++++++++-------- tests/core/db_qb_test.php | 11 ++++++++--- tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/classes/db_pdo.php b/classes/db_pdo.php index 202880e..8e26a0c 100644 --- a/classes/db_pdo.php +++ b/classes/db_pdo.php @@ -37,7 +37,7 @@ abstract class DB_PDO extends PDO { // Last query executed public $last_query; - + // Prefix to apply to table namesa public $table_prefix = ''; @@ -62,7 +62,7 @@ abstract class DB_PDO extends PDO { $this->util = new $class($this); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - + // Set additional driver options, if they exist 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 - * + * * @param string $table * @return string */ public function quote_table($table) - { + { // If there isn't a prefix set, just quote the table name if (empty($this->table_prefix)) { return $this->quote_ident($table); } - + // Split indentifier by period, will split into: // database.schema.table OR // schema.table OR @@ -218,25 +218,25 @@ abstract class DB_PDO extends PDO { // table $idents = (array) explode('.', $table); $segments = count($idents); - + // Reference the last item in the split string $last =& $idents[$segments - 1]; - + // Quote the last item $last = $this->_prefix($last); - + // Rejoin - $table = implode('.', $idents); - + $table = implode('.', $idents); + // Finally, quote the table return $this->quote_ident($table); } - + // -------------------------------------------------------------------------- - - /** + + /** * Sets the table prefix on the passed string - * + * * @param string $str * @return string */ @@ -247,10 +247,10 @@ abstract class DB_PDO extends PDO { { return $str; } - + return $this->table_prefix.$str; } - + // -------------------------------------------------------------------------- /** diff --git a/classes/query_builder.php b/classes/query_builder.php index 6ec348b..4c28c0d 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -116,6 +116,8 @@ class Query_Builder implements iQuery_Builder { // Instantiate the Query Parser $this->parser = new Query_Parser(); + $this->queries['total_time'] = 0; + // Make things just slightly shorter $this->sql = $this->db->sql; } @@ -1190,10 +1192,31 @@ class Query_Builder implements iQuery_Builder { $sql = $this->_compile($type, $table); $vals = array_merge($this->values, (array) $this->where_values); + $start_time = microtime(TRUE); + $res = ($simple) ? $this->db->query($sql) : $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(); return $res; @@ -1300,14 +1323,6 @@ class Query_Builder implements iQuery_Builder { $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 . '
'; - return $sql; } } diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index 32e1588..0bba7fa 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -18,16 +18,21 @@ */ abstract class QBTest extends UnitTestCase { + public function __destruct() + { + // echo '
' . print_r($this->db->queries, TRUE) . '
'; + } + // -------------------------------------------------------------------------- // ! Get Tests // -------------------------------------------------------------------------- - + public function TestQueryFunctionAlias() { if (empty($this->db)) return; - + $db = Query(); - + $this->assertReference($this->db, $db); } diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 6d31427097cc41687f32a9b52b9dd41d8cd5b9dc..a7155a803c8660394ece15f1d0161e07ef8295dc 100755 GIT binary patch delta 1087 zcmZ9L&ubGw6vyA}k2c8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