Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Add support for empty values set with IN predicate #223

Merged
merged 3 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions src/Sql/Predicate/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct($identifier = null, $valueSet = null)
if ($identifier) {
$this->setIdentifier($identifier);
}
if ($valueSet) {
if ($valueSet !== null) {
$this->setValueSet($valueSet);
}
}
Expand Down Expand Up @@ -122,9 +122,10 @@ public function getExpressionData()
foreach ($values as $argument) {
list($replacements[], $types[]) = $this->normalizeArgument($argument, self::TYPE_VALUE);
}
$values = count($values) > 0 ? array_fill(0, count($values), '%s') : [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this? array_fill() returns [] if $values === []. Please remove this change, the previous code is ok.

$specification = vsprintf(
$this->specification,
[$identifierSpecFragment, '(' . implode(', ', array_fill(0, count($values), '%s')) . ')']
[$identifierSpecFragment, '(' . implode(', ', $values) . ')']
);
}

Expand Down
19 changes: 19 additions & 0 deletions test/Sql/Predicate/InTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public function testCanPassIdentifierAndValueSetToConstructor()
$this->assertEquals([1, 2], $in->getValueSet());
}

public function testCanPassIdentifierAndEmptyValueSetToConstructor()
{
$in = new In('foo.bar', []);
$this->assertEquals('foo.bar', $in->getIdentifier());
$this->assertEquals([], $in->getValueSet());
}

public function testIdentifierIsMutable()
{
$in = new In();
Expand Down Expand Up @@ -82,6 +89,18 @@ public function testGetExpressionDataWithSubselect()
$this->assertEquals($expected, $in->getExpressionData());
}

public function testGetExpressionDataWithEmptyValues()
{
$select = new Select;
$in = new In('foo', []);
$expected = [[
'%s IN ()',
['foo'],
[$in::TYPE_IDENTIFIER]
]];
$this->assertEquals($expected, $in->getExpressionData());
}

public function testGetExpressionDataWithSubselectAndIdentifier()
{
$select = new Select;
Expand Down