Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for FragmentInterface in array conditions #147

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 23 additions & 8 deletions src/Driver/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,10 @@ protected function condition(QueryParameters $params, Quoter $q, array $context)

$placeholder = '?';
if ($value->isArray()) {
if ($operator === '=') {
$operator = 'IN';
} elseif ($operator === '!=') {
$operator = 'NOT IN';
}
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
return $this->arrayToInOperator($params, $q, $value->getValue(), $operator === 'IN' || $operator === '=');
roxblnfk marked this conversation as resolved.
Show resolved Hide resolved
}

$placeholder = '(' . rtrim(str_repeat('? ,', count($value->getValue())), ', ') . ')';
$params->push($value);
} elseif ($value->isNull()) {
if ($value->isNull()) {
if ($operator === '=') {
$operator = 'IS';
} elseif ($operator === '!=') {
Expand Down Expand Up @@ -521,4 +516,24 @@ protected function optional(string $prefix, string $expression, string $postfix

return $prefix . $expression . $postfix;
}

private function arrayToInOperator(QueryParameters $params, Quoter $q, array $values, bool $in): string
{
$operator = $in ? 'IN' : 'NOT IN';

$placeholders = $simpleParams = [];
foreach ($values as $value) {
if ($value instanceof FragmentInterface) {
$placeholders[] = $this->fragment($params, $q, $value);
} else {
$placeholders[] = '?';
$simpleParams[] = $value;
}
}
if ($simpleParams !== []) {
$params->push(new Parameter($simpleParams));
}

return \sprintf('%s(%s)', $operator, \implode(',', $placeholders));
}
}
19 changes: 17 additions & 2 deletions src/Driver/CompilerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,27 @@ private function hashParam(QueryParameters $params, ParameterInterface $param):
return 'N';
}

$params->push($param);

if ($param->isArray()) {
$simpleParams = [];
foreach ($param->getValue() as $value) {
if ($value instanceof FragmentInterface) {
foreach ($value->getTokens()['parameters'] as $fragmentParam) {
$params->push($fragmentParam);
}
} else {
$simpleParams[] = $value;
}
}

if ($simpleParams !== []) {
$params->push(new Parameter($simpleParams));
}

return 'A' . count($param->getValue());
}

$params->push($param);

return '?';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2165,4 +2165,86 @@ public function testSelectWithFragmentedColumns(): void
$select
);
}

public function testWhereInWithoutSpecifiedOperator(): void
{
$select = $this->database
->select()
->from(['users'])
->where(
'uuid',
new Parameter(['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013'])
);

$this->assertSameQuery('SELECT * FROM {users} WHERE {uuid} IN (?, ?)', $select);

$this->assertSameParameters(
['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013'],
$select,
);
}

public function testWhereInWithEqualSpecifiedOperator(): void
{
$select = $this->database
->select()
->from(['users'])
->where(
'uuid',
'=',
new Parameter(['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013'])
);

$this->assertSameQuery('SELECT * FROM {users} WHERE {uuid} IN (?, ?)', $select);

$this->assertSameParameters(
['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013'],
$select,
);
}

public function testWhereInWithNotEqualSpecifiedOperator(): void
{
$select = $this->database
->select()
->from(['users'])
->where(
'uuid',
'!=',
new Parameter(['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013'])
);

$this->assertSameQuery('SELECT * FROM {users} WHERE {uuid} NOT IN (?, ?)', $select);

$this->assertSameParameters(
['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013'],
$select,
);
}

public function testFragmentInWhereInClause(): void
{
$select = $this->database
->select()
->from(['users'])
->where('uuid', new Fragment('UUID_TO_BIN(?)', '12345678-1234-1234-1234-123456789012'))
->andWhere('uuid', 'IN', [
new Fragment('UUID_TO_BIN(?)', '12345678-1234-1234-1234-123456789013'),
new Fragment('UUID_TO_BIN(?)', '12345678-1234-1234-1234-123456789014'),
]);

$this->assertSameQuery(
'SELECT * FROM {users} WHERE {uuid} = UUID_TO_BIN (?) AND {uuid} IN (UUID_TO_BIN (?), UUID_TO_BIN (?))',
$select
);

$this->assertSameParameters(
[
'12345678-1234-1234-1234-123456789012',
'12345678-1234-1234-1234-123456789013',
'12345678-1234-1234-1234-123456789014',
],
$select
);
}
}
Loading