Skip to content

Commit a6c4119

Browse files
authored
Merge pull request #681 from cosmocode/SQL-placeholders
Use named parameters in SQL queries
2 parents c82b8ac + 150f5bc commit a6c4119

File tree

4 files changed

+4
-60
lines changed

4 files changed

+4
-60
lines changed

_test/QueryBuilderTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,4 @@ public function test_join()
2323
$qb->addLeftJoin('second', 'fourth', 'fourth', 'second.foo=fourth.foo');
2424
$this->assertEquals(['first', 'second', 'fourth', 'third'], array_keys($qb->from));
2525
}
26-
27-
public function test_placeholders()
28-
{
29-
$qb = new QueryBuilder();
30-
31-
32-
$foo = $qb->addValue('foo');
33-
$bar = $qb->addValue('bar');
34-
35-
$input = "this is $foo and $bar and $foo again";
36-
$expect = "this is ? and ? and ? again";
37-
$values = ['foo', 'bar', 'foo'];
38-
39-
$output = $qb->fixPlaceholders($input);
40-
41-
$this->assertEquals($expect, $output[0]);
42-
$this->assertEquals($values, $output[1]);
43-
}
44-
45-
public function test_placeholderfail()
46-
{
47-
$this->expectException(StructException::class);
48-
$qb = new QueryBuilder();
49-
$qb->fixPlaceholders('this has unknown placeholder :!!val7!!:');
50-
}
5126
}

_test/StructTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function getLang($key)
111111
*/
112112
protected function cleanWS($string)
113113
{
114-
return preg_replace('/\s+/s', '', $string);
114+
return preg_replace(['/\s+/s', '/\:val(\d{1,3})/'], ['', '?'], $string);
115115
}
116116

117117
/**

_test/mock/QueryBuilder.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@ class QueryBuilder extends meta\QueryBuilder
88
{
99
public $from;
1010

11-
public function fixPlaceholders($sql)
12-
{
13-
return parent::fixPlaceholders($sql);
14-
}
15-
1611
/**
1712
* for debugging where statements
1813
*
1914
* @return array ($sql, $opts)
2015
*/
2116
public function getWhereSQL()
2217
{
23-
return $this->fixPlaceholders($this->filters()->toSQL());
18+
return [$this->filters()->toSQL(), array_values($this->values)];
2419
}
2520
}

meta/QueryBuilder.php

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function addValue($value)
183183
static $count = 0;
184184
$count++;
185185

186-
$placeholder = ":!!val$count!!:"; // sqlite plugin does not support named parameters, yet so we have simulate it
186+
$placeholder = ":val$count";
187187
$this->values[$placeholder] = $value;
188188
return $placeholder;
189189
}
@@ -241,33 +241,7 @@ public function getSQL()
241241
'ORDER BY ' . implode(",\n", $this->orderby) . "\n";
242242
}
243243

244-
return $this->fixPlaceholders($sql);
245-
}
246-
247-
/**
248-
* Replaces the named placeholders with ? placeholders
249-
*
250-
* Until the sqlite plugin can use named placeholder properly
251-
*
252-
* @param string $sql
253-
* @return array
254-
*/
255-
protected function fixPlaceholders($sql)
256-
{
257-
$vals = [];
258-
259-
while (preg_match('/(:!!val\d+!!:)/', $sql, $m)) {
260-
$pl = $m[1];
261-
262-
if (!array_key_exists($pl, $this->values)) {
263-
throw new StructException('Placeholder not found');
264-
}
265-
266-
$sql = preg_replace("/$pl/", '?', $sql, 1);
267-
$vals[] = $this->values[$pl];
268-
}
269-
270-
return [$sql, $vals];
244+
return [$sql, array_values($this->values)];
271245
}
272246

273247
/**

0 commit comments

Comments
 (0)