diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index 29f1a1c..dc98350 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -86,9 +86,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); } /** @@ -978,6 +975,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 * @@ -991,7 +1008,21 @@ public function statement($sql, array $bindings = []) { $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]; } diff --git a/tests/Pecee/Pixie/QueryBuilderTest.php b/tests/Pecee/Pixie/QueryBuilderTest.php index 0e57744..cc340d4 100644 --- a/tests/Pecee/Pixie/QueryBuilderTest.php +++ b/tests/Pecee/Pixie/QueryBuilderTest.php @@ -82,10 +82,10 @@ public function testRawQuery() { $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 ); }