Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding DboSource::getLog() and test case.

  • Loading branch information...
commit c0a19ced14694830d8cf876973a8c48a0f2b6807 1 parent 4ff668b
Mark Story markstory authored
19 cake/libs/model/datasources/dbo_source.php
View
@@ -476,16 +476,27 @@ function hasResult() {
}
/**
- * Outputs the contents of the queries log.
+ * Get the query log as an array.
*
- * @param boolean $sorted
- */
- function showLog($sorted = false) {
+ * @param boolean $sorted Get the queries sorted by time taken, defaults to false.
+ * @return array Array of queries run as an array
+ **/
+ function getLog($sorted = false) {
if ($sorted) {
$log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC);
} else {
$log = $this->_queriesLog;
}
+ return $log;
+ }
+
+/**
+ * Outputs the contents of the queries log.
+ *
+ * @param boolean $sorted Get the queries sorted by time taken, defaults to false
+ */
+ function showLog($sorted = false) {
+ $log = $this->getLog($sorted);
if ($this->_queriesCnt > 1) {
$text = 'queries';
28 cake/tests/cases/libs/model/datasources/dbo_source.test.php
View
@@ -3620,11 +3620,11 @@ function testBuildColumn() {
function testHasAny() {
$this->testDb->hasAny($this->Model, array());
$expected = 'SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE 1 = 1';
- $this->assertEqual($this->testDb->simulated[0], $expected);
+ $this->assertEqual($this->testDb->simulated[1], $expected);
$this->testDb->hasAny($this->Model, array('TestModel.name' => 'harry'));
$expected = "SELECT COUNT(`TestModel`.`id`) AS count FROM `test_models` AS `TestModel` WHERE `TestModel`.`name` = 'harry'";
- $this->assertEqual($this->testDb->simulated[1], $expected);
+ $this->assertEqual($this->testDb->simulated[2], $expected);
}
/**
@@ -3970,6 +3970,30 @@ function testLog() {
}
/**
+ * test getting the query log as an array.
+ *
+ * @return void
+ **/
+ function testGetLog() {
+ $this->testDb->logQuery('Query 1');
+ $this->testDb->logQuery('Query 2');
+
+ $oldError = $this->testDb->error;
+ $this->testDb->error = true;
+ $result = $this->testDb->logQuery('Error 1');
+ $this->assertFalse($result);
+ $this->testDb->error = $oldError;
+
+ $log = $this->testDb->getLog();
+ $expected = array('query' => 'Query 1', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
+ $this->assertEqual($log[0], $expected);
+ $expected = array('query' => 'Query 2', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
+ $this->assertEqual($log[1], $expected);
+ $expected = array('query' => 'Error 1', 'error' => true, 'affected' => '', 'numRows' => '', 'took' => '');
+ $this->assertEqual($log[2], $expected);
+ }
+
+/**
* test ShowQuery generation of regular and error messages
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.