Skip to content

Commit

Permalink
Reverted auto PDO parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
skipperbent committed Dec 11, 2017
1 parent a0ef29a commit 8020d5a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
44 changes: 39 additions & 5 deletions src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
*
Expand All @@ -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];
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/Pecee/Pixie/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function callbackMock() {
}

public function setUp() {
$this->container = new Container();

$this->mockPdoStatement = $this->getMockBuilder(\PDOStatement::class)->getMock();

Expand Down

0 comments on commit 8020d5a

Please sign in to comment.