summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Holywell <[email protected]>2014-06-27 13:47:35 +0100
committerSimon Holywell <[email protected]>2014-06-27 13:47:35 +0100
commitad5d6b1100307d7fb6cb94c9944918dfe3977718 (patch)
tree1b1d35ceaefaf901d2eedffc424e2f98eebbed06
parenta7f20ee1b89123b68d47e9d17e9964c495c183d2 (diff)
parentb65268cea3ecd96f278e51a2936d0d7ab0286c18 (diff)
Merge pull request #223 from m92o/named-placeholders-logging
Support named placeholders logging and test
-rw-r--r--idiorm.php38
-rw-r--r--test/QueryBuilderPsr1Test53.php6
-rw-r--r--test/QueryBuilderTest.php6
-rw-r--r--test/bootstrap.php7
4 files changed, 36 insertions, 21 deletions
diff --git a/idiorm.php b/idiorm.php
index b3f3fb7..0e305fd 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -456,29 +456,33 @@
self::$_query_log[$connection_name] = array();
}
- // Strip out any non-integer indexes from the parameters
- foreach($parameters as $key => $value) {
- if (!is_int($key)) unset($parameters[$key]);
- }
-
- if (count($parameters) > 0) {
+ if (empty($parameters)) {
+ $bound_query = $query;
+ } else {
// Escape the parameters
$parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters);
- // Avoid %format collision for vsprintf
- $query = str_replace("%", "%%", $query);
+ if (array_values($parameters) === $parameters) {
+ // ? placeholders
+ // Avoid %format collision for vsprintf
+ $query = str_replace("%", "%%", $query);
- // Replace placeholders in the query for vsprintf
- if(false !== strpos($query, "'") || false !== strpos($query, '"')) {
- $query = IdiormString::str_replace_outside_quotes("?", "%s", $query);
+ // Replace placeholders in the query for vsprintf
+ if(false !== strpos($query, "'") || false !== strpos($query, '"')) {
+ $query = IdiormString::str_replace_outside_quotes("?", "%s", $query);
+ } else {
+ $query = str_replace("?", "%s", $query);
+ }
+
+ // Replace the question marks in the query with the parameters
+ $bound_query = vsprintf($query, $parameters);
} else {
- $query = str_replace("?", "%s", $query);
+ // named placeholders
+ foreach ($parameters as $key => $val) {
+ $query = str_replace($key, $val, $query);
+ }
+ $bound_query = $query;
}
-
- // Replace the question marks in the query with the parameters
- $bound_query = vsprintf($query, $parameters);
- } else {
- $bound_query = $query;
}
self::$_last_query = $bound_query;
diff --git a/test/QueryBuilderPsr1Test53.php b/test/QueryBuilderPsr1Test53.php
index 0aa8930..3b267eb 100644
--- a/test/QueryBuilderPsr1Test53.php
+++ b/test/QueryBuilderPsr1Test53.php
@@ -274,6 +274,12 @@ class QueryBuilderPsr1Test53 extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, ORM::getLastQuery());
}
+ public function testRawQueryWithNamedPlaceholders() {
+ ORM::forTable('widget')->rawQuery('SELECT `w`.* FROM `widget` w WHERE `name` = :name AND `age` = :age', array(':name' => 'Fred', ':age' => 5))->findMany();
+ $expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'";
+ $this->assertEquals($expected, ORM::getLastQuery());
+ }
+
public function testSimpleResultColumn() {
ORM::forTable('widget')->select('name')->findMany();
$expected = "SELECT `name` FROM `widget`";
diff --git a/test/QueryBuilderTest.php b/test/QueryBuilderTest.php
index 9c4ef97..54320d0 100644
--- a/test/QueryBuilderTest.php
+++ b/test/QueryBuilderTest.php
@@ -304,6 +304,12 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, ORM::get_last_query());
}
+ public function testRawQueryWithNamedPlaceholders() {
+ ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w WHERE `name` = :name AND `age` = :age', array(':name' => 'Fred', ':age' => 5))->find_many();
+ $expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'";
+ $this->assertEquals($expected, ORM::get_last_query());
+ }
+
public function testSimpleResultColumn() {
ORM::for_table('widget')->select('name')->find_many();
$expected = "SELECT `name` FROM `widget`";
diff --git a/test/bootstrap.php b/test/bootstrap.php
index 3b3cace..02d9725 100644
--- a/test/bootstrap.php
+++ b/test/bootstrap.php
@@ -46,15 +46,14 @@ class MockPDOStatement extends PDOStatement {
/**
* Add data to arrays
*/
- public function bindParam($index, $value, $type)
+ public function bindParam($key, $value, $type)
{
- // Do check on index, and type
- if (!is_int($index)) throw new Exception('Incorrect parameter type. Expected $index to be an integer.');
+ // Do check on type
if (!is_int($type) || ($type != PDO::PARAM_STR && $type != PDO::PARAM_NULL && $type != PDO::PARAM_BOOL && $type != PDO::PARAM_INT))
throw new Exception('Incorrect parameter type. Expected $type to be an integer.');
// Add param to array
- $this->bindParams[$index - 1] = $value;
+ $this->bindParams[is_int($key) ? --$key : $key] = $value;
}
/**