From 2fe2f5e2b67df1c8a1d3e43ce3f6d82bbc3d39da Mon Sep 17 00:00:00 2001 From: Harald Leithner Date: Sun, 16 Jun 2024 12:14:28 +0200 Subject: [PATCH 1/3] Simplified and fixed broken multi name parameter code --- src/Mysqli/MysqliStatement.php | 24 +++++++++--------------- src/Sqlsrv/SqlsrvStatement.php | 18 ++++++------------ 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/Mysqli/MysqliStatement.php b/src/Mysqli/MysqliStatement.php index f689c0b4..5c25a837 100644 --- a/src/Mysqli/MysqliStatement.php +++ b/src/Mysqli/MysqliStatement.php @@ -158,7 +158,7 @@ public function prepareParameterKeyMapping($sql) $quoteChar = ''; $literal = ''; $mapping = []; - $replace = []; + $position = 0; $matches = []; $pattern = '/([:][a-zA-Z0-9_]+)/'; @@ -198,15 +198,14 @@ public function prepareParameterKeyMapping($sql) } if (isset($mapping[$match[0]])) { - $mapping[$match[0]] = is_array($mapping[$match[0]]) ? $mapping[$match[0]] : [$mapping[$match[0]]]; - $mapping[$match[0]][] = \count($mapping); - } else { - $mapping[$match[0]] = \count($mapping); + $mapping[$match[0]][] = []; } - $endOfPlaceholder = $match[1] + strlen($match[0]); + + $mapping[$match[0]][] = $position++; + $endOfPlaceholder = $match[1] + strlen($match[0]); $beginOfNextPlaceholder = $matches[0][$i + 1][1] ?? strlen($substring); $beginOfNextPlaceholder -= $endOfPlaceholder; - $literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder); + $literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder); } } else { $literal .= $substring; @@ -378,14 +377,9 @@ public function execute(?array $parameters = null) foreach ($this->bindedValues as $key => &$value) { $paramKey = $this->parameterKeyMapping[$key]; - if (is_scalar($this->parameterKeyMapping[$key])) { - $params[$paramKey] =& $value; - $types[$paramKey] = $this->typesKeyMapping[$key]; - } else { - foreach ($paramKey as $currentKey) { - $params[$currentKey] =& $value; - $types[$currentKey] = $this->typesKeyMapping[$key]; - } + foreach ($paramKey as $currentKey) { + $params[$currentKey] =& $value; + $types[$currentKey] = $this->typesKeyMapping[$key]; } } } else { diff --git a/src/Sqlsrv/SqlsrvStatement.php b/src/Sqlsrv/SqlsrvStatement.php index 9bff16df..c1785bea 100644 --- a/src/Sqlsrv/SqlsrvStatement.php +++ b/src/Sqlsrv/SqlsrvStatement.php @@ -163,7 +163,7 @@ public function prepareParameterKeyMapping($sql) $quoteChar = ''; $literal = ''; $mapping = []; - $replace = []; + $position = 0; $matches = []; $pattern = '/([:][a-zA-Z0-9_]+)/'; @@ -203,16 +203,14 @@ public function prepareParameterKeyMapping($sql) } if (isset($mapping[$match[0]])) { - $mapping[$match[0]] = is_array($mapping[$match[0]]) ? $mapping[$match[0]] : [$mapping[$match[0]]]; - $mapping[$match[0]][] = \count($mapping); - } else { - $mapping[$match[0]] = \count($mapping); + $mapping[$match[0]][] = []; } + $mapping[$match[0]][] = $position++; $endOfPlaceholder = $match[1] + strlen($match[0]); $beginOfNextPlaceholder = $matches[0][$i + 1][1] ?? strlen($substring); $beginOfNextPlaceholder -= $endOfPlaceholder; - $literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder); + $literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder); } } else { $literal .= $substring; @@ -492,12 +490,8 @@ private function prepare() if (isset($this->parameterKeyMapping[$key])) { $paramKey = $this->parameterKeyMapping[$key]; - if (is_scalar($this->parameterKeyMapping[$key])) { - $params[$paramKey] = $variable; - } else { - foreach ($paramKey as $currentKey) { - $params[$currentKey] = $variable; - } + foreach ($paramKey as $currentKey) { + $params[$currentKey] = $variable; } } else { $params[] = $variable; From 1f2e7f1ba9499c171b4872abff89df3c666e3abc Mon Sep 17 00:00:00 2001 From: Harald Leithner Date: Sun, 16 Jun 2024 13:25:19 +0200 Subject: [PATCH 2/3] Fix missing ! and tests --- Tests/Mysqli/MysqliPreparedStatementTest.php | 4 ++-- Tests/Sqlsrv/SqlsrvPreparedStatementTest.php | 4 ++-- src/Mysqli/MysqliStatement.php | 2 +- src/Sqlsrv/SqlsrvStatement.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/Mysqli/MysqliPreparedStatementTest.php b/Tests/Mysqli/MysqliPreparedStatementTest.php index 3921b3dc..34fe60a7 100644 --- a/Tests/Mysqli/MysqliPreparedStatementTest.php +++ b/Tests/Mysqli/MysqliPreparedStatementTest.php @@ -116,8 +116,8 @@ public function testPrepareParameterKeyMappingWithSingleKey() $this->assertEquals( [ - ':search' => 0, - ':search2' => 1, + ':search' => [0], + ':search2' => [1], ], $parameterKeyMapping ); diff --git a/Tests/Sqlsrv/SqlsrvPreparedStatementTest.php b/Tests/Sqlsrv/SqlsrvPreparedStatementTest.php index 6afd8706..8625fdf7 100644 --- a/Tests/Sqlsrv/SqlsrvPreparedStatementTest.php +++ b/Tests/Sqlsrv/SqlsrvPreparedStatementTest.php @@ -118,8 +118,8 @@ public function testPrepareParameterKeyMappingWithSingleKey() $this->assertEquals( [ - ':search' => 0, - ':search2' => 1, + ':search' => [0], + ':search2' => [1], ], $parameterKeyMapping ); diff --git a/src/Mysqli/MysqliStatement.php b/src/Mysqli/MysqliStatement.php index 5c25a837..9e9c6f61 100644 --- a/src/Mysqli/MysqliStatement.php +++ b/src/Mysqli/MysqliStatement.php @@ -197,7 +197,7 @@ public function prepareParameterKeyMapping($sql) $literal .= substr($substring, 0, $match[1]); } - if (isset($mapping[$match[0]])) { + if (!isset($mapping[$match[0]])) { $mapping[$match[0]][] = []; } diff --git a/src/Sqlsrv/SqlsrvStatement.php b/src/Sqlsrv/SqlsrvStatement.php index c1785bea..18c2923e 100644 --- a/src/Sqlsrv/SqlsrvStatement.php +++ b/src/Sqlsrv/SqlsrvStatement.php @@ -202,7 +202,7 @@ public function prepareParameterKeyMapping($sql) $literal .= substr($substring, 0, $match[1]); } - if (isset($mapping[$match[0]])) { + if (!isset($mapping[$match[0]])) { $mapping[$match[0]][] = []; } From f2b34be9261b8a0dedd535bec9ee538b95996c14 Mon Sep 17 00:00:00 2001 From: Harald Leithner Date: Sun, 16 Jun 2024 13:38:54 +0200 Subject: [PATCH 3/3] Fix copy-paste error --- src/Mysqli/MysqliStatement.php | 2 +- src/Sqlsrv/SqlsrvStatement.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mysqli/MysqliStatement.php b/src/Mysqli/MysqliStatement.php index 9e9c6f61..afda7b72 100644 --- a/src/Mysqli/MysqliStatement.php +++ b/src/Mysqli/MysqliStatement.php @@ -198,7 +198,7 @@ public function prepareParameterKeyMapping($sql) } if (!isset($mapping[$match[0]])) { - $mapping[$match[0]][] = []; + $mapping[$match[0]] = []; } $mapping[$match[0]][] = $position++; diff --git a/src/Sqlsrv/SqlsrvStatement.php b/src/Sqlsrv/SqlsrvStatement.php index 18c2923e..e93599fe 100644 --- a/src/Sqlsrv/SqlsrvStatement.php +++ b/src/Sqlsrv/SqlsrvStatement.php @@ -203,7 +203,7 @@ public function prepareParameterKeyMapping($sql) } if (!isset($mapping[$match[0]])) { - $mapping[$match[0]][] = []; + $mapping[$match[0]] = []; } $mapping[$match[0]][] = $position++;