Skip to content

Commit

Permalink
Merge pull request #43 from skipperbent/v4-development
Browse files Browse the repository at this point in the history
Version 4.1.1
  • Loading branch information
skipperbent authored Dec 11, 2017
2 parents 055dfe0 + 7c7c0f2 commit 131b3f4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
49 changes: 40 additions & 9 deletions src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -411,10 +408,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 +977,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 +1006,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 131b3f4

Please sign in to comment.