Skip to content

Commit 3fc1456

Browse files
norbedg
authored andcommitted
SqlBuilder: The same conditions with different parameter is not skipped [Closes #109, #14]
1 parent db8f1df commit 3fc1456

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Database/Table/SqlBuilder.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function addWhere($condition, ...$params)
213213
return $this->addWhereComposition($condition, $params[0]);
214214
}
215215

216-
$hash = md5($condition . json_encode($params));
216+
$hash = $this->getConditionHash($condition, $params);
217217
if (isset($this->conditions[$hash])) {
218218
return FALSE;
219219
}
@@ -553,6 +553,21 @@ protected function addWhereComposition(array $columns, array $parameters)
553553
}
554554

555555

556+
private function getConditionHash($condition, $parameters)
557+
{
558+
foreach ($parameters as & $parameter) {
559+
if ($parameter instanceof Selection) {
560+
$parameter = $this->getConditionHash($parameter->getSql(), $parameter->sqlBuilder->getParameters());
561+
} elseif ($parameter instanceof SqlLiteral) {
562+
$parameter = $this->getConditionHash($parameter->__toString(), $parameter->getParameters());
563+
} elseif (is_object($parameter) && method_exists($parameter, '__toString')) {
564+
$parameter = $parameter->__toString();
565+
}
566+
}
567+
return md5($condition . json_encode($parameters));
568+
}
569+
570+
556571
private function getCachedTableList()
557572
{
558573
if (!$this->cacheTableList) {

tests/Database/Table/SqlBuilder.addWhere().phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ test(function () use ($context) { // test Selection as a parameter
4242
]), $sqlBuilder->buildSelectQuery());
4343
});
4444

45+
46+
test(function () use ($context) { // test more Selection as a parameter
47+
$sqlBuilder = new SqlBuilder('book', $context);
48+
$sqlBuilder->addWhere('id', $context->table('book'));
49+
$sqlBuilder->addWhere('id', $context->table('book_tag')->select('book_id'));
50+
Assert::equal(reformat([
51+
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?)) AND (`id` IN (?))',
52+
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book])) AND ([id] IN (SELECT [book_id] FROM [book_tag]))',
53+
]), $sqlBuilder->buildSelectQuery());
54+
});
55+
56+
57+
test(function () use ($context) { // test more ActiveRow as a parameter
58+
$sqlBuilder = new SqlBuilder('book', $context);
59+
$books = $context->table('book')->where('id', [1,2])->fetchPairs('id');
60+
$sqlBuilder->addWhere('id ?', $books[1]);
61+
$sqlBuilder->addWhere('id ?', $books[2]);
62+
Assert::equal(reformat([
63+
'SELECT * FROM [book] WHERE ([id] = ?) AND ([id] = ?)',
64+
]), $sqlBuilder->buildSelectQuery());
65+
});
66+
67+
4568
test(function () use ($context) { // test Selection with parameters as a parameter
4669
$sqlBuilder = new SqlBuilder('book', $context);
4770
$sqlBuilder->addWhere('id', $context->table('book')->having('COUNT(:book_tag.tag_id) >', 1));
@@ -53,6 +76,7 @@ test(function () use ($context) { // test Selection with parameters as a paramet
5376
Assert::count(1, $sqlBuilder->getParameters());
5477
});
5578

79+
5680
test(function () use ($context) { // test Selection with column as a parameter
5781
$sqlBuilder = new SqlBuilder('book', $context);
5882
$sqlBuilder->addWhere('id', $context->table('book')->select('id'));

0 commit comments

Comments
 (0)