From 8020d5ad6521404b162cc481ee3006fcc16d84fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 11 Dec 2017 05:37:13 +0100 Subject: [PATCH 1/2] Reverted auto PDO parameters. --- .../QueryBuilder/QueryBuilderHandler.php | 44 ++++++++++++++++--- tests/Pecee/Pixie/QueryBuilderTest.php | 7 +-- tests/TestCase.php | 1 - 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index bd36926..2cd2cba 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -411,10 +411,10 @@ public function get(): array { ); } - $start = microtime(true); + $start = \microtime(true); $this->fireEvents(EventHandler::EVENT_BEFORE_SELECT, $queryObject); $result = \call_user_func_array([$this->pdoStatement, 'fetchAll'], $this->fetchParameters); - $executionTime += microtime(true) - $start; + $executionTime += \microtime(true) - $start; $this->pdoStatement = null; $this->fireEvents(EventHandler::EVENT_AFTER_SELECT, $queryObject, $result, $executionTime); @@ -980,6 +980,26 @@ public function setFetchMode($parameters = null) { return $this; } + /** + * Parse parameter type from value + * + * @param mixed $value + * + * @return int + */ + protected function parseParameterType($value) { + + if ($value === null) { + return PDO::PARAM_NULL; + } + + if (\is_int($value) === true || \is_bool($value) === true) { + return PDO::PARAM_INT; + } + + return PDO::PARAM_STR; + } + /** * Execute statement * @@ -989,13 +1009,27 @@ public function setFetchMode($parameters = null) { * @return array PDOStatement and execution time as float */ public function statement(string $sql, array $bindings = []): array { - $start = microtime(true); + $start = \microtime(true); $pdoStatement = $this->pdo->prepare($sql); - $pdoStatement->execute($bindings); + /** + * NOTE: + * PHP 5.6 & 7 bug: https://bugs.php.net/bug.php?id=38546 + * \PDO::PARAM_BOOL is not supported, use \PDO::PARAM_INT instead + */ + + foreach ($bindings as $key => $value) { + $pdoStatement->bindValue( + \is_int($key) ? $key + 1 : $key, + $value, + $this->parseParameterType($value) + ); + } + + $pdoStatement->execute(); - return [$pdoStatement, microtime(true) - $start]; + return [$pdoStatement, \microtime(true) - $start]; } /** diff --git a/tests/Pecee/Pixie/QueryBuilderTest.php b/tests/Pecee/Pixie/QueryBuilderTest.php index 163091e..d44d131 100644 --- a/tests/Pecee/Pixie/QueryBuilderTest.php +++ b/tests/Pecee/Pixie/QueryBuilderTest.php @@ -85,11 +85,12 @@ public function testRawQuery() { $query = 'select * from cb_my_table where id = ? and name = ? and hipster = null'; $bindings = [5, 'usman', null]; $queryArr = $this->builder->query($query, $bindings)->get(); + $this->assertEquals( - [ + array( $query, - [5, 'usman', null], - ], + array(array(5, \PDO::PARAM_INT), array('usman', \PDO::PARAM_STR), array(null, \PDO::PARAM_NULL)), + ), $queryArr ); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 7211d8f..fdb4ad3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -39,7 +39,6 @@ public function callbackMock() { } public function setUp() { - $this->container = new Container(); $this->mockPdoStatement = $this->getMockBuilder(\PDOStatement::class)->getMock(); From 7c7c0f2d512b4e6d5514339bd92573bcb742acb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Mon, 11 Dec 2017 05:37:47 +0100 Subject: [PATCH 2/2] Removed old statement --- src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index 2cd2cba..64203c6 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -88,9 +88,6 @@ public function __construct(Connection $connection = null) { $this->adapterInstance = new $adapterClass($this->connection); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - - // PDO will parse parameter datatypes automatically - $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); } /** @@ -102,7 +99,7 @@ public function __construct(Connection $connection = null) { * @return void */ protected function addStatement(string $key, $value) { - if (array_key_exists($key, $this->statements) === false) { + if (\array_key_exists($key, $this->statements) === false) { $this->statements[ $key ] = (array)$value; } else { $this->statements[ $key ] = array_merge($this->statements[ $key ], (array)$value);