Skip to content

Commit

Permalink
Merge pull request #2 from SoftwarePunt/fix-multiple-bound-arrays
Browse files Browse the repository at this point in the history
fix(Query): multiple IN()s in a one clause don't work
  • Loading branch information
roydejong authored Aug 23, 2023
2 parents 90cc397 + 3b2fe67 commit 3368f2c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/dataSources.local.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/instarecord.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions lib/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,9 @@ protected function processStatementParameters(string $statementText, array $para

if ($markerSkip <= 0) {
break;
} else {
$markerOffset += $markerIdx + 1;
}

$markerOffset = $markerIdx + 1;
$markerSkip--;
}

Expand All @@ -451,11 +450,10 @@ protected function processStatementParameters(string $statementText, array $para
$extraMarkersStr = "";

for ($i = 0; $i < $extraMarkers; $i++) {
$extraMarkersStr .= ", ?";
$extraMarkersStr .= ", #_TEMP_MARKER_#";
}

$statementText = substr_replace($statementText, $extraMarkersStr, $markerIdx + 1, 0);
$finalizedRow[0] = $statementText;
}

// Bind each parameter
Expand All @@ -468,6 +466,9 @@ protected function processStatementParameters(string $statementText, array $para
}
}

$statementText = str_replace('#_TEMP_MARKER_#', '?', $statementText);
$finalizedRow[0] = $statementText;

return $finalizedRow;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Database/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,29 @@ public function testWhereWithAnd()
$this->assertEquals('DELETE FROM fruits WHERE (type = ?) AND (color IN (?, ?)) AND (tastes_nice = 1);', $queryString);
}

public function testWhereWithMultipleINs()
{
$query = new Query(new Connection(new DatabaseConfig()));

$query = $query->delete()
->from('fruits')
->where('color = ? OR (color IN (?)) OR (color IN (?))',
'pink', ['red', 'blue', 'orange'], ['blurple', 'black']);

$queryString = $query->createStatementText();

$this->assertEquals('DELETE FROM fruits WHERE (color = ? OR (color IN (?, ?, ?)) OR (color IN (?, ?)));', $queryString);

$this->assertEquals([
'pink',
'red',
'blue',
'orange',
'blurple',
'black'
], $query->getBoundParametersForGeneratedStatement());
}

public function testHaving()
{
$query = new Query(new Connection(new DatabaseConfig()));
Expand Down

0 comments on commit 3368f2c

Please sign in to comment.