Skip to content

Commit

Permalink
Merge pull request #4443 from cakephp/3.0-simpler-string-interpolation
Browse files Browse the repository at this point in the history
3.0 - Using a simple string interpolation routine in QueryLogger
  • Loading branch information
markstory committed Aug 31, 2014
2 parents a6a2c89 + e656e30 commit 86b89e6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Database/Log/QueryLogger.php
Expand Up @@ -15,7 +15,6 @@
namespace Cake\Database\Log;

use Cake\Log\Log;
use Cake\Utility\String;

/**
* This class is a bridge used to write LoggedQuery objects into a real log.
Expand Down Expand Up @@ -64,7 +63,12 @@ protected function _interpolate($query) {
return is_string($p) ? "'$p'" : $p;
}, $query->params);

return String::insert($query->query, $params);
$keys = [];
foreach ($params as $key => $param) {
$keys[] = is_string($key) ? "/:$key/" : '/[?]/';
}

return preg_replace($keys, $params, $query->query, 1);
}

}
2 changes: 1 addition & 1 deletion tests/TestCase/Database/Log/QueryLoggerTest.php
Expand Up @@ -53,7 +53,7 @@ public function testStingInterpolation() {
$logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
$query = new LoggedQuery;
$query->query = 'SELECT a FROM b where a = :p1 AND b = :p2 AND c = :p3';
$query->params = ['p1' => 'string', 'p2' => 3, 'p3' => null];
$query->params = ['p1' => 'string', 'p3' => null, 'p2' => 3];

$logger->expects($this->once())->method('_log')->with($query);
$logger->log($query);
Expand Down

0 comments on commit 86b89e6

Please sign in to comment.