Skip to content
Open
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
50 changes: 44 additions & 6 deletions flight/database/SimplePdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,44 @@ public function fetchRow(string $sql, array $params = []): ?Collection
*/
protected function processInStatementSql(string $sql, array $params = []): array
{
// First, find all placeholders (?) in the original SQL and their positions
$originalSql_ = $sql;
$params_ = $params;
$newParams = [];

// First, find all placeholders with named parameter inside IN pattern
$pattern = '/IN\s*\(\s*(:[a-zA-Z][a-zA-Z0-9]*)\s*\)/i';
$inPositions = [];
if (preg_match_all($pattern, $originalSql_, $matches, PREG_OFFSET_CAPTURE)) {
foreach ($matches[1] as $k => $match)
$inPositions[$match[0]] = $matches[0][$k];
}
// and process each param
foreach($inPositions as $param => $match) {
$param_key = substr($param, 1);
if (!isset($params[$param_key])
|| !is_array($params[$param_key])) // Not defined param or single value, keep as-is
continue;
if (empty($params[$param_key]))
// Empty array
$originalSql_ = str_replace($match, 'IN (NULL)', $originalSql_);
else {
$i = 0;
$keys = [];
foreach($params[$param_key] as $val) {
$keys[] = "{$param}{$i}";
$newParams["{$param_key}{$i}"] = $val;
$i++;
}
$originalSql_ = str_replace($match, "IN (". implode(", ", $keys) . ")", $originalSql_);
}
unset($params_[$param_key]);
}
$params_ = array_merge($params_, $newParams);

// Then, find all placeholders (?) in the original SQL and their positions
// We need to track which are IN(?) patterns vs regular ?
$originalSql = $sql;
$originalSql = $originalSql_;
$newParams = [];
$paramIndex = 0;

// Find all ? positions and whether they're part of IN(?)
$pattern = '/IN\s*\(\s*\?\s*\)/i';
Expand Down Expand Up @@ -144,9 +177,10 @@ protected function processInStatementSql(string $sql, array $params = []): array
$offset = 0;

// Process each param
for ($i = 0; $i < count($params); $i++) {
$i = 0;
foreach($params_ as $key => $val) {
if (isset($inParamIndices[$i])) {
$value = $params[$i];
$value = $params_[$i];

// Find the next IN(?) in the remaining SQL
if (preg_match($pattern, $newSql, $match, PREG_OFFSET_CAPTURE, $offset)) {
Expand All @@ -172,8 +206,12 @@ protected function processInStatementSql(string $sql, array $params = []): array
}
}
} else {
$newParams[] = $params[$i];
if (is_numeric($key))
$newParams[] = $params_[$i];
else
$newParams[$key] = $val;
}
$i++;
}

return ['sql' => $newSql, 'params' => $newParams];
Expand Down
84 changes: 84 additions & 0 deletions tests/SimplePdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ public function testFetchAllWithParams(): void
$this->assertCount(2, $rows); // John and Jane
}

public function testFetchAllWithNamedParams(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE name LIKE :name', [ 'name' => 'J%']);
$this->assertCount(2, $rows); // John and Jane
}

// =========================================================================
// fetchColumn Tests
// =========================================================================
Expand Down Expand Up @@ -258,24 +264,55 @@ public function testInStatementWithArrayOfIntegers(): void
$this->assertCount(2, $rows);
}

public function testInStatementWithNamedArrayOfIntegers(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE id IN(:ids)', [ 'ids' => [1, 2]]);
$this->assertCount(2, $rows);
}

public function testInStatementWithArrayOfStrings(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE name IN(?)', [['John', 'Jane']]);
$this->assertCount(2, $rows);
}

public function testInStatementWithNamedArrayOfStrings(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE name IN(:names)', ['names' => ['John', 'Jane']]);
$this->assertCount(2, $rows);
}

public function testInStatementWithEmptyArray(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE id IN(?)', [[]]);
$this->assertCount(0, $rows); // IN(NULL) matches nothing
}

public function testInStatementWithNamedEmptyArray(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE id IN(:empty)', [ 'empty' => []]);
$this->assertCount(0, $rows); // IN(NULL) matches nothing
}

public function testInStatementWithSingleValue(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE id IN(?)', [1]);
$this->assertCount(1, $rows);
}

public function testInStatementWithSingleNamedValue(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE id IN(:ids)', ['ids' => 1]);
$this->assertCount(1, $rows);
}

public function testInStatementWithNamedSingleValue(): void
{
$rows = $this->db->fetchAll('SELECT * FROM users WHERE id IN(:val)', ['val' => 1]);
$this->assertCount(1, $rows);
}


public function testMultipleInStatements(): void
{
$rows = $this->db->fetchAll(
Expand All @@ -285,6 +322,24 @@ public function testMultipleInStatements(): void
$this->assertCount(2, $rows); // John (id=1) and Bob (id=3)
}

public function testMultipleInStatementsNamed(): void
{
$rows = $this->db->fetchAll(
'SELECT * FROM users WHERE id IN(:ids) AND name IN(:names)',
['ids' => [1, 2, 3], 'names' => ['John', 'Bob']]
);
$this->assertCount(2, $rows); // John (id=1) and Bob (id=3)
}

public function testMultipleInStatementsMixed(): void
{
$rows = $this->db->fetchAll(
'SELECT * FROM users WHERE id IN(?) AND name IN(:names)',
[[1, 2, 3], 'names' => ['John', 'Bob']]
);
$this->assertCount(2, $rows); // John (id=1) and Bob (id=3)
}

public function testInStatementWithOtherParams(): void
{
$rows = $this->db->fetchAll(
Expand All @@ -294,6 +349,35 @@ public function testInStatementWithOtherParams(): void
$this->assertCount(2, $rows);
}

public function testInStatementWithOtherNamedParams(): void
{
$rows = $this->db->fetchAll(
'SELECT * FROM users WHERE id > :id AND name IN(:names)',
['id' => 0, 'names' => ['John', 'Jane']]
);
$this->assertCount(2, $rows);
}


public function testInStatementWithMixedParams1(): void
{
$rows = $this->db->fetchAll(
'SELECT * FROM users WHERE id > ? AND name IN(:names)',
['id' => 0, 'names' => ['John', 'Jane']]
);
$this->assertCount(2, $rows);
}

public function testInStatementWithMixedParams2(): void
{
$rows = $this->db->fetchAll(
'SELECT * FROM users WHERE id > :id AND name IN(?)',
['id' => 0, ['John', 'Jane']]
);
$this->assertCount(2, $rows);
}


// =========================================================================
// insert() Tests
// =========================================================================
Expand Down
Loading