mirror of
https://github.com/cosmocode/dokuwiki-plugin-struct.git
synced 2025-08-06 10:01:14 +00:00
Merge pull request #681 from cosmocode/SQL-placeholders
Use named parameters in SQL queries
This commit is contained in:
@ -23,29 +23,4 @@ class QueryBuilderTest extends StructTest
|
|||||||
$qb->addLeftJoin('second', 'fourth', 'fourth', 'second.foo=fourth.foo');
|
$qb->addLeftJoin('second', 'fourth', 'fourth', 'second.foo=fourth.foo');
|
||||||
$this->assertEquals(['first', 'second', 'fourth', 'third'], array_keys($qb->from));
|
$this->assertEquals(['first', 'second', 'fourth', 'third'], array_keys($qb->from));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_placeholders()
|
|
||||||
{
|
|
||||||
$qb = new QueryBuilder();
|
|
||||||
|
|
||||||
|
|
||||||
$foo = $qb->addValue('foo');
|
|
||||||
$bar = $qb->addValue('bar');
|
|
||||||
|
|
||||||
$input = "this is $foo and $bar and $foo again";
|
|
||||||
$expect = "this is ? and ? and ? again";
|
|
||||||
$values = ['foo', 'bar', 'foo'];
|
|
||||||
|
|
||||||
$output = $qb->fixPlaceholders($input);
|
|
||||||
|
|
||||||
$this->assertEquals($expect, $output[0]);
|
|
||||||
$this->assertEquals($values, $output[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_placeholderfail()
|
|
||||||
{
|
|
||||||
$this->expectException(StructException::class);
|
|
||||||
$qb = new QueryBuilder();
|
|
||||||
$qb->fixPlaceholders('this has unknown placeholder :!!val7!!:');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ abstract class StructTest extends \DokuWikiTest
|
|||||||
*/
|
*/
|
||||||
protected function cleanWS($string)
|
protected function cleanWS($string)
|
||||||
{
|
{
|
||||||
return preg_replace('/\s+/s', '', $string);
|
return preg_replace(['/\s+/s', '/\:val(\d{1,3})/'], ['', '?'], $string);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,11 +8,6 @@ class QueryBuilder extends meta\QueryBuilder
|
|||||||
{
|
{
|
||||||
public $from;
|
public $from;
|
||||||
|
|
||||||
public function fixPlaceholders($sql)
|
|
||||||
{
|
|
||||||
return parent::fixPlaceholders($sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* for debugging where statements
|
* for debugging where statements
|
||||||
*
|
*
|
||||||
@ -20,6 +15,6 @@ class QueryBuilder extends meta\QueryBuilder
|
|||||||
*/
|
*/
|
||||||
public function getWhereSQL()
|
public function getWhereSQL()
|
||||||
{
|
{
|
||||||
return $this->fixPlaceholders($this->filters()->toSQL());
|
return [$this->filters()->toSQL(), array_values($this->values)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,7 +183,7 @@ class QueryBuilder
|
|||||||
static $count = 0;
|
static $count = 0;
|
||||||
$count++;
|
$count++;
|
||||||
|
|
||||||
$placeholder = ":!!val$count!!:"; // sqlite plugin does not support named parameters, yet so we have simulate it
|
$placeholder = ":val$count";
|
||||||
$this->values[$placeholder] = $value;
|
$this->values[$placeholder] = $value;
|
||||||
return $placeholder;
|
return $placeholder;
|
||||||
}
|
}
|
||||||
@ -241,33 +241,7 @@ class QueryBuilder
|
|||||||
'ORDER BY ' . implode(",\n", $this->orderby) . "\n";
|
'ORDER BY ' . implode(",\n", $this->orderby) . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->fixPlaceholders($sql);
|
return [$sql, array_values($this->values)];
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replaces the named placeholders with ? placeholders
|
|
||||||
*
|
|
||||||
* Until the sqlite plugin can use named placeholder properly
|
|
||||||
*
|
|
||||||
* @param string $sql
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
protected function fixPlaceholders($sql)
|
|
||||||
{
|
|
||||||
$vals = [];
|
|
||||||
|
|
||||||
while (preg_match('/(:!!val\d+!!:)/', $sql, $m)) {
|
|
||||||
$pl = $m[1];
|
|
||||||
|
|
||||||
if (!array_key_exists($pl, $this->values)) {
|
|
||||||
throw new StructException('Placeholder not found');
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql = preg_replace("/$pl/", '?', $sql, 1);
|
|
||||||
$vals[] = $this->values[$pl];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [$sql, $vals];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user