Skip to content

Commit 98b3610

Browse files
committed
ResultSet: parameters are passed to statement via bindValue with correct type [Closes #63]
1 parent 78a0c89 commit 98b3610

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/Database/ResultSet.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,15 @@ public function __construct(Connection $connection, $queryString, array $params)
6464
if (substr($queryString, 0, 2) === '::') {
6565
$connection->getPdo()->{substr($queryString, 2)}();
6666
} elseif ($queryString !== NULL) {
67+
static $types = array('boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT,
68+
'resource' => PDO::PARAM_LOB, 'NULL' => PDO::PARAM_NULL);
6769
$this->pdoStatement = $connection->getPdo()->prepare($queryString);
70+
foreach ($params as $key => $value) {
71+
$type = gettype($value);
72+
$this->pdoStatement->bindValue(is_int($key) ? $key + 1 : $key, $value, isset($types[$type]) ? $types[$type] : PDO::PARAM_STR);
73+
}
6874
$this->pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
69-
$this->pdoStatement->execute($params);
75+
$this->pdoStatement->execute();
7076
}
7177
} catch (\PDOException $e) {
7278
$e = $this->supplementalDriver->convertException($e);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Database\ResultSet parameters
5+
* @dataProvider? databases.ini mysql
6+
*/
7+
8+
use Tester\Assert,
9+
Nette\Utils\DateTime;
10+
11+
require __DIR__ . '/connect.inc.php'; // create $connection
12+
13+
$res = $connection->fetch('SELECT ? AS c1, ? AS c2, ? AS c3, ? as c4', fopen(__FILE__, 'r'), TRUE, NULL, 123);
14+
15+
Assert::same(
16+
array('c1' => file_get_contents(__FILE__), 'c2' => 1, 'c3' => NULL, 'c4' => 123),
17+
(array) $res
18+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Database\ResultSet parameters
5+
* @dataProvider? databases.ini postgresql
6+
*/
7+
8+
use Tester\Assert,
9+
Nette\Utils\DateTime;
10+
11+
require __DIR__ . '/connect.inc.php'; // create $connection
12+
13+
$res = $connection->fetch('SELECT ? AS c1, ? AS c2, ? AS c3', TRUE, NULL, 123);
14+
15+
Assert::same(
16+
array('c1' => TRUE, 'c2' => NULL, 'c3' => 123),
17+
(array) $res
18+
);

0 commit comments

Comments
 (0)