From 4e471fcf2e2097e037d8885a976bc7e7e00894ab Mon Sep 17 00:00:00 2001 From: "Laurent (Blerot)" Date: Thu, 12 Nov 2026 22:45:01 +0100 Subject: [PATCH 1/2] Adding named parameters support to sql requests --- flight/database/SimplePdo.php | 50 ++++++++++++++++++--- tests/SimplePdoTest.php | 84 +++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 6 deletions(-) diff --git a/flight/database/SimplePdo.php b/flight/database/SimplePdo.php index 4fd98fb7..a3cbb60d 100644 --- a/flight/database/SimplePdo.php +++ b/flight/database/SimplePdo.php @@ -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'; @@ -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)) { @@ -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]; diff --git a/tests/SimplePdoTest.php b/tests/SimplePdoTest.php index 0143381d..89c5bfbe 100644 --- a/tests/SimplePdoTest.php +++ b/tests/SimplePdoTest.php @@ -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 // ========================================================================= @@ -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( @@ -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( @@ -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 // ========================================================================= From 6d05336efb8dc13c0b09a592a4e1bb10638cf378 Mon Sep 17 00:00:00 2001 From: "Laurent (Blerot)" Date: Sat, 21 Nov 2026 14:00:34 +0100 Subject: [PATCH 2/2] adding exception for mixed named and unamed parameters refacto with that exception --- flight/database/SimplePdo.php | 95 +++++++++++++++++++++-------------- tests/SimplePdoTest.php | 8 +-- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/flight/database/SimplePdo.php b/flight/database/SimplePdo.php index a3cbb60d..4c6aeb21 100644 --- a/flight/database/SimplePdo.php +++ b/flight/database/SimplePdo.php @@ -90,10 +90,14 @@ public function fetchRow(string $sql, array $params = []): ?Collection /** * Don't worry about this guy. Converts stuff for IN statements + * either named or unamed parameters * * Ex: $row = $db->fetchAll("SELECT * FROM table WHERE id = ? AND something IN(?), [ $id, [1,2,3] ]); * Converts this to "SELECT * FROM table WHERE id = ? AND something IN(?,?,?)" - * + * + * Ex: $row = $db->fetchAll("SELECT * FROM table WHERE id = :id AND something IN(:something), [ 'id' => $id, 'something' => [1,2,3] ]); + * Converts this to "SELECT * FROM table WHERE id = :id AND something IN(:something0 , :something1 , :something2)" + * * @param string $sql the sql statement * @param array $params the params for the sql statement * @@ -101,43 +105,61 @@ public function fetchRow(string $sql, array $params = []): ?Collection */ protected function processInStatementSql(string $sql, array $params = []): array { - $originalSql_ = $sql; - $params_ = $params; - $newParams = []; + // 1. throw an exception if mixed parameters are found in sql + $pattern = '/(:[a-zA-Z][a-zA-Z0-9]*)/i'; + $named_params_nb = preg_match_all($pattern, $sql, $matches_named, PREG_OFFSET_CAPTURE); - // 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]; + $pattern = '/(\?)/i'; + $unnamed_params_nb = preg_match_all($pattern, $sql, $matches_unnamed, PREG_OFFSET_CAPTURE); + + if (($named_params_nb === false) + || ($unnamed_params_nb === false) + || (($named_params_nb != 0) && ($unnamed_params_nb != 0))) + { + throw new PDOException( + "processInStatementSql failed: mixed named and unamed parameters found." + ); } - // 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++; + + // 2. handle named parameters + if ($named_params_nb > 0) + { + $newSql = $sql; + $newParams = $params; + + // Find all placeholders with named parameter inside IN pattern + $pattern = '/IN\s*\(\s*(:[a-zA-Z][a-zA-Z0-9]*)\s*\)/i'; + if (preg_match_all($pattern, $sql, $matches, PREG_OFFSET_CAPTURE)) { + // Then process each param + foreach ($matches[1] as $k => $match) { + $param_key = substr($match[0], 1); + $inStatement = $matches[0][$k]; + 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 + $newSql = str_replace($inStatement, 'IN (NULL)', $newSql); + else { // replace IN parameters + $i = 0; + $keys = []; + foreach($params[$param_key] as $val) { + $keys[] = ":{$param_key}{$i}"; + $newParams["{$param_key}{$i}"] = $val; + $i++; + } + $newSql = str_replace($inStatement, "IN (". implode(", ", $keys) . ")", $newSql); + } + unset($newParams[$param_key]); } - $originalSql_ = str_replace($match, "IN (". implode(", ", $keys) . ")", $originalSql_); } - unset($params_[$param_key]); + + return ['sql' => $newSql, 'params' => $newParams]; } - $params_ = array_merge($params_, $newParams); - // Then, find all placeholders (?) in the original SQL and their positions + // 3. handle unnamed parameters + // First, find all placeholders (?) in the original SQL and their positions // We need to track which are IN(?) patterns vs regular ? - $originalSql = $originalSql_; + $originalSql = $sql; $newParams = []; // Find all ? positions and whether they're part of IN(?) @@ -177,10 +199,9 @@ protected function processInStatementSql(string $sql, array $params = []): array $offset = 0; // Process each param - $i = 0; - foreach($params_ as $key => $val) { + for ($i = 0; $i < count($params); $i++) { 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)) { @@ -206,12 +227,8 @@ protected function processInStatementSql(string $sql, array $params = []): array } } } else { - if (is_numeric($key)) - $newParams[] = $params_[$i]; - else - $newParams[$key] = $val; + $newParams[] = $params[$i]; } - $i++; } return ['sql' => $newSql, 'params' => $newParams]; diff --git a/tests/SimplePdoTest.php b/tests/SimplePdoTest.php index 89c5bfbe..fce14311 100644 --- a/tests/SimplePdoTest.php +++ b/tests/SimplePdoTest.php @@ -333,11 +333,11 @@ public function testMultipleInStatementsNamed(): void public function testMultipleInStatementsMixed(): void { + $this->expectException(PDOException::class); $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 @@ -361,20 +361,20 @@ public function testInStatementWithOtherNamedParams(): void public function testInStatementWithMixedParams1(): void { + $this->expectException(PDOException::class); $rows = $this->db->fetchAll( 'SELECT * FROM users WHERE id > ? AND name IN(:names)', - ['id' => 0, 'names' => ['John', 'Jane']] + [0, 'names' => ['John', 'Jane']] ); - $this->assertCount(2, $rows); } public function testInStatementWithMixedParams2(): void { + $this->expectException(PDOException::class); $rows = $this->db->fetchAll( 'SELECT * FROM users WHERE id > :id AND name IN(?)', ['id' => 0, ['John', 'Jane']] ); - $this->assertCount(2, $rows); }