Skip to content

Commit

Permalink
Merge pull request #42 from skipperbent/v3-development
Browse files Browse the repository at this point in the history
Version 3.5.0
  • Loading branch information
skipperbent authored Dec 11, 2017
2 parents 4934d9e + 462747d commit 89ec531
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
39 changes: 35 additions & 4 deletions src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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];
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Pecee/Pixie/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down

0 comments on commit 89ec531

Please sign in to comment.