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();