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 f689c0b4..afda7b72 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_]+)/'; @@ -197,16 +197,15 @@ public function prepareParameterKeyMapping($sql) $literal .= substr($substring, 0, $match[1]); } - 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); + if (!isset($mapping[$match[0]])) { + $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..e93599fe 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_]+)/'; @@ -202,17 +202,15 @@ public function prepareParameterKeyMapping($sql) $literal .= substr($substring, 0, $match[1]); } - 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); + if (!isset($mapping[$match[0]])) { + $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;