Skip to content

Commit

Permalink
SqlPreprocessor: scalar are always passed via bindValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 17, 2016
1 parent a01c1e1 commit b2067a2
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 110 deletions.
28 changes: 6 additions & 22 deletions src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,16 @@ public function callback($m)
private function formatValue($value, $mode = NULL)
{
if (!$mode || $mode === 'auto') {
if (is_string($value)) {
if (strlen($value) > 20) {
$this->remaining[] = $value;
return '?';

} else {
return $this->connection->quote($value);
}

} elseif (is_int($value)) {
return (string) $value;

} elseif (is_float($value)) {
return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');

} elseif (is_bool($value)) {
return $this->driver->formatBool($value);
if (is_scalar($value) || is_resource($value)) {
$this->remaining[] = $value;
return '?';

} elseif ($value === NULL) {
return 'NULL';

} elseif ($value instanceof Table\IRow) {
return $this->formatValue($value->getPrimary());
$this->remaining[] = $value->getPrimary();
return '?';

} elseif ($value instanceof SqlLiteral) {
$prep = clone $this;
Expand All @@ -150,10 +137,7 @@ private function formatValue($value, $mode = NULL)
return $this->driver->formatDateInterval($value);

} elseif (is_object($value) && method_exists($value, '__toString')) {
return $this->formatValue((string) $value);

} elseif (is_resource($value)) {
$this->remaining[] = $value;
$this->remaining[] = (string) $value;
return '?';
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Connection.preprocess.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ require __DIR__ . '/connect.inc.php'; // create $connection

Assert::same(['SELECT name FROM author', []], $connection->preprocess('SELECT name FROM author'));

Assert::same(["SELECT 'string'", []], $connection->preprocess('SELECT ?', 'string'));
Assert::same(["SELECT ?", ['string']], $connection->preprocess('SELECT ?', 'string'));
9 changes: 6 additions & 3 deletions tests/Database/Connection.query.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
test(function () use ($connection) {
$res = $connection->query('SELECT id FROM author WHERE id = ?', 11);
Assert::type(Nette\Database\ResultSet::class, $res);
Assert::same('SELECT id FROM author WHERE id = 11', $res->getQueryString());
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
Assert::same([11], $res->getParameters());
});


test(function () use ($connection) {
$res = $connection->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
Assert::same([11, 12], $res->getParameters());
});


test(function () use ($connection) {
$res = $connection->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
Assert::same([11, 12], $res->getParameters());
});
9 changes: 6 additions & 3 deletions tests/Database/Context.query.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
test(function () use ($context) {
$res = $context->query('SELECT id FROM author WHERE id = ?', 11);
Assert::type(Nette\Database\ResultSet::class, $res);
Assert::same('SELECT id FROM author WHERE id = 11', $res->getQueryString());
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
Assert::same([11], $res->getParameters());
});


test(function () use ($context) {
$res = $context->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
Assert::same([11, 12], $res->getParameters());
});


test(function () use ($context) {
$res = $context->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
Assert::same([11, 12], $res->getParameters());
});
Loading

0 comments on commit b2067a2

Please sign in to comment.