From b65268cea3ecd96f278e51a2936d0d7ab0286c18 Mon Sep 17 00:00:00 2001 From: Kunio Murasawa Date: Tue, 24 Jun 2014 10:37:35 +0900 Subject: [PATCH] Support named placeholders logging and test --- idiorm.php | 38 ++++++++++++++++++--------------- test/QueryBuilderPsr1Test53.php | 6 ++++++ test/QueryBuilderTest.php | 6 ++++++ test/bootstrap.php | 7 +++--- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/idiorm.php b/idiorm.php index 80e9a4de..49b400ff 100644 --- a/idiorm.php +++ b/idiorm.php @@ -456,29 +456,33 @@ protected static function _log_query($query, $parameters, $connection_name, $que 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 0aa89303..3b267eba 100644 --- a/test/QueryBuilderPsr1Test53.php +++ b/test/QueryBuilderPsr1Test53.php @@ -274,6 +274,12 @@ public function testRawQueryWithParameters() { $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 9c4ef973..54320d00 100644 --- a/test/QueryBuilderTest.php +++ b/test/QueryBuilderTest.php @@ -304,6 +304,12 @@ public function testRawQueryWithParameters() { $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 3b3cacea..02d97252 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -46,15 +46,14 @@ public function execute($params = NULL) { /** * 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; } /**