Skip to content

Commit

Permalink
ResultSet: parameters are passed to statement via bindValue with corr…
Browse files Browse the repository at this point in the history
…ect type [Closes #63]
  • Loading branch information
dg committed Apr 22, 2015
1 parent 78a0c89 commit 98b3610
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ public function __construct(Connection $connection, $queryString, array $params)
if (substr($queryString, 0, 2) === '::') {
$connection->getPdo()->{substr($queryString, 2)}();
} elseif ($queryString !== NULL) {
static $types = array('boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT,
'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL);
$this->pdoStatement = $connection->getPdo()->prepare($queryString);
foreach ($params as $key => $value) {
$type = gettype($value);
$this->pdoStatement->bindValue(is_int($key) ? $key + 1 : $key, $value, isset($types[$type]) ? $types[$type] : PDO::PARAM_STR);
}
$this->pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
$this->pdoStatement->execute($params);
$this->pdoStatement->execute();
}
} catch (\PDOException $e) {
$e = $this->supplementalDriver->convertException($e);
Expand Down
18 changes: 18 additions & 0 deletions tests/Database/ResultSet.parameters.mysql.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* Test: Nette\Database\ResultSet parameters
* @dataProvider? databases.ini mysql
*/

use Tester\Assert,
Nette\Utils\DateTime;

require __DIR__ . '/connect.inc.php'; // create $connection

$res = $connection->fetch('SELECT ? AS c1, ? AS c2, ? AS c3, ? as c4', fopen(__FILE__, 'r'), TRUE, NULL, 123);

Assert::same(
array('c1' => file_get_contents(__FILE__), 'c2' => 1, 'c3' => NULL, 'c4' => 123),
(array) $res
);
18 changes: 18 additions & 0 deletions tests/Database/ResultSet.parameters.postgre.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* Test: Nette\Database\ResultSet parameters
* @dataProvider? databases.ini postgresql
*/

use Tester\Assert,
Nette\Utils\DateTime;

require __DIR__ . '/connect.inc.php'; // create $connection

$res = $connection->fetch('SELECT ? AS c1, ? AS c2, ? AS c3', TRUE, NULL, 123);

Assert::same(
array('c1' => TRUE, 'c2' => NULL, 'c3' => 123),
(array) $res
);

0 comments on commit 98b3610

Please sign in to comment.