Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Database/Table/SqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ protected function addCondition($condition, array $params, array &$conditions, a

$replace = null;
$placeholderNum = 0;
foreach ($params as $arg) {
while (count($params)) {
$arg = array_shift($params);
preg_match('#(?:.*?\?.*?){' . $placeholderNum . '}(((?:&|\||^|~|\+|-|\*|/|%|\(|,|<|>|=|(?<=\W|^)(?:REGEXP|ALL|AND|ANY|BETWEEN|EXISTS|IN|[IR]?LIKE|OR|NOT|SOME|INTERVAL))\s*)?(?:\(\?\)|\?))#s', $condition, $match, PREG_OFFSET_CAPTURE);
$hasOperator = ($match[1][0] === '?' && $match[1][1] === 0) ? true : !empty($match[2][0]);

Expand Down Expand Up @@ -353,8 +354,11 @@ protected function addCondition($condition, array $params, array &$conditions, a

if ($this->driver->isSupported(ISupplementalDriver::SUPPORT_SUBSELECT)) {
$arg = null;
$replace = $match[2][0] . '(' . $clone->getSql() . ')';
$conditionsParameters = array_merge($conditionsParameters, $clone->getSqlBuilder()->getParameters());
$subSelectPlaceholderCount = substr_count($clone->getSql(), '?');
$replace = $match[2][0] . '(' . $clone->getSql() . (!$subSelectPlaceholderCount && count($clone->getSqlBuilder()->getParameters()) === 1 ? ' ?' : '') . ')';
if (count($clone->getSqlBuilder()->getParameters())) {
array_unshift($params, ...$clone->getSqlBuilder()->getParameters());
}
} else {
$arg = [];
foreach ($clone as $row) {
Expand Down
12 changes: 11 additions & 1 deletion tests/Database/Table/SqlBuilder.addWhere().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ test(function () use ($context) { // test more Selection as a parameter
});


test(function () use ($context) { // test more Selection as one of more argument
$sqlBuilder = new SqlBuilder('book', $context);
$sqlBuilder->addWhere('id ? AND id ?', $context->table('book')->where('id', 2), $context->table('book_tag')->select('book_id'));
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?) AND `id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book] WHERE ([id] = ?)) AND [id] IN (SELECT [book_id] FROM [book_tag]))',
]), $sqlBuilder->buildSelectQuery());
});


test(function () use ($context) { // test more ActiveRow as a parameter
$sqlBuilder = new SqlBuilder('book', $context);
$books = $context->table('book')->where('id', [1, 2])->fetchPairs('id');
Expand All @@ -73,7 +83,7 @@ test(function () use ($context) { // test Selection with parameters as a paramet
$schemaSupported = $context->getConnection()->getSupplementalDriver()->isSupported(ISupplementalDriver::SUPPORT_SCHEMA);
Assert::equal(reformat([
'mysql' => 'SELECT * FROM `book` WHERE (`id` IN (?))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book] LEFT JOIN ' . ($schemaSupported ? '[public].[book_tag] ' : '') . '[book_tag] ON [book].[id] = [book_tag].[book_id] HAVING COUNT([book_tag].[tag_id]) >))',
'SELECT * FROM [book] WHERE ([id] IN (SELECT [id] FROM [book] LEFT JOIN ' . ($schemaSupported ? '[public].[book_tag] ' : '') . '[book_tag] ON [book].[id] = [book_tag].[book_id] HAVING COUNT([book_tag].[tag_id]) > ?))',
]), $sqlBuilder->buildSelectQuery());
Assert::count(1, $sqlBuilder->getParameters());
});
Expand Down