Skip to content

Commit

Permalink
Database: implemented placeholder for delimited table name [closes ne…
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach authored and simara-esports committed Feb 23, 2015
1 parent a98aad6 commit d9da178
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Database/SqlPreprocessor.php
Expand Up @@ -75,7 +75,7 @@ public function process($params)
} else {
$res[] = Nette\Utils\Strings::replace(
$param,
'~\'.*?\'|".*?"|\?|\b(?:INSERT|REPLACE|UPDATE|WHERE|HAVING|ORDER BY|GROUP BY)\b|/\*.*?\*/|--[^\n]*~si',
'~\'.*?\'|".*?"|\?(?:name)?|\b(?:INSERT|REPLACE|UPDATE|WHERE|HAVING|ORDER BY|GROUP BY)\b|/\*.*?\*/|--[^\n]*~si',
array($this, 'callback')
);
}
Expand All @@ -92,11 +92,15 @@ public function callback($m)
if ($m[0] === "'" || $m[0] === '"' || $m[0] === '/' || $m[0] === '-') { // string or comment
return $m;

} elseif ($m === '?') { // placeholder
} elseif ($m[0] === '?') { // placeholder
if ($this->counter >= count($this->params)) {
throw new Nette\InvalidArgumentException('There are more placeholders than passed parameters.');
}
return $this->formatValue($this->params[$this->counter++]);
if ($m === '?') {
return $this->formatValue($this->params[$this->counter++]);
} else {
return $this->driver->delimite($this->params[$this->counter++]);
}

} else { // command
$this->arrayMode = $this->arrayModes[strtoupper($m)];
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Table/SqlBuilder.php
Expand Up @@ -662,7 +662,7 @@ protected function buildLeftJoinConditions($allLeftJoinConditions) {
protected function tryDelimite($s)
{
$driver = $this->driver;
$delimited = preg_replace_callback('#(?<=[^\w`"\[]|^)[a-z_][a-z0-9_]*(?=[^\w`"(\]]|\z)#i', function($m) use ($driver) {
$delimited = preg_replace_callback('#(?<=[^\w`"\[?]|^)[a-z_][a-z0-9_]*(?=[^\w`"(\]]|\z)#i', function($m) use ($driver) {
return strtoupper($m[0]) === $m[0] ? $m[0] : $driver->delimite($m[0]);
}, $s);

Expand Down
7 changes: 7 additions & 0 deletions tests/Database/SqlPreprocessor.phpt
Expand Up @@ -66,6 +66,13 @@ test(function() use ($preprocessor) { // IN
});


test(function() use ($preprocessor) {
list($sql, $params) = $preprocessor->process(array('SELECT id FROM author WHERE ?name = ? OR ?name = ?', 'id', 12, 'number', 23));
Assert::same( reformat('SELECT id FROM author WHERE [id] = 12 OR [number] = 23'), $sql );
Assert::same( array(), $params );
});


test(function() use ($preprocessor) { // comments
list($sql, $params) = $preprocessor->process(array("SELECT id --?\nFROM author WHERE id = ?", 11));
Assert::same( "SELECT id --?\nFROM author WHERE id = 11", $sql );
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/Table/SqlBuilder.addWhere().phpt
Expand Up @@ -23,6 +23,15 @@ test(function() use ($context) { // test paramateres with NULL
});


test(function() use ($context) { // ?name
$sqlBuilder = new SqlBuilder('book', $context);
$sqlBuilder->addWhere('?name ?', 'id', 3);
$sqlBuilder->addWhere('?name = ?', 'number', 4);
$sqlBuilder->addWhere('?name ?', 'number', NULL);
Assert::same(reformat('SELECT * FROM [book] WHERE (?name = ?) AND (?name = ?) AND (?name IS NULL)'), $sqlBuilder->buildSelectQuery());
});


test(function() use ($context) { // test Selection as a parameter
$sqlBuilder = new SqlBuilder('book', $context);
$sqlBuilder->addWhere('id', $context->table('book'));
Expand Down

0 comments on commit d9da178

Please sign in to comment.