Skip to content

Commit 133e3d0

Browse files
authored
Merge pull request #303 from joomla-framework/3.x/feature/rewrite-multiple-prepared-parameters
Rewrite multiple prepared parameters
2 parents 915bf8b + f2b34be commit 133e3d0

File tree

4 files changed

+21
-33
lines changed

4 files changed

+21
-33
lines changed

Tests/Mysqli/MysqliPreparedStatementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public function testPrepareParameterKeyMappingWithSingleKey()
116116

117117
$this->assertEquals(
118118
[
119-
':search' => 0,
120-
':search2' => 1,
119+
':search' => [0],
120+
':search2' => [1],
121121
],
122122
$parameterKeyMapping
123123
);

Tests/Sqlsrv/SqlsrvPreparedStatementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public function testPrepareParameterKeyMappingWithSingleKey()
118118

119119
$this->assertEquals(
120120
[
121-
':search' => 0,
122-
':search2' => 1,
121+
':search' => [0],
122+
':search2' => [1],
123123
],
124124
$parameterKeyMapping
125125
);

src/Mysqli/MysqliStatement.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function prepareParameterKeyMapping($sql)
158158
$quoteChar = '';
159159
$literal = '';
160160
$mapping = [];
161-
$replace = [];
161+
$position = 0;
162162
$matches = [];
163163
$pattern = '/([:][a-zA-Z0-9_]+)/';
164164

@@ -197,16 +197,15 @@ public function prepareParameterKeyMapping($sql)
197197
$literal .= substr($substring, 0, $match[1]);
198198
}
199199

200-
if (isset($mapping[$match[0]])) {
201-
$mapping[$match[0]] = is_array($mapping[$match[0]]) ? $mapping[$match[0]] : [$mapping[$match[0]]];
202-
$mapping[$match[0]][] = \count($mapping);
203-
} else {
204-
$mapping[$match[0]] = \count($mapping);
200+
if (!isset($mapping[$match[0]])) {
201+
$mapping[$match[0]] = [];
205202
}
206-
$endOfPlaceholder = $match[1] + strlen($match[0]);
203+
204+
$mapping[$match[0]][] = $position++;
205+
$endOfPlaceholder = $match[1] + strlen($match[0]);
207206
$beginOfNextPlaceholder = $matches[0][$i + 1][1] ?? strlen($substring);
208207
$beginOfNextPlaceholder -= $endOfPlaceholder;
209-
$literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder);
208+
$literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder);
210209
}
211210
} else {
212211
$literal .= $substring;
@@ -378,14 +377,9 @@ public function execute(?array $parameters = null)
378377
foreach ($this->bindedValues as $key => &$value) {
379378
$paramKey = $this->parameterKeyMapping[$key];
380379

381-
if (is_scalar($this->parameterKeyMapping[$key])) {
382-
$params[$paramKey] =& $value;
383-
$types[$paramKey] = $this->typesKeyMapping[$key];
384-
} else {
385-
foreach ($paramKey as $currentKey) {
386-
$params[$currentKey] =& $value;
387-
$types[$currentKey] = $this->typesKeyMapping[$key];
388-
}
380+
foreach ($paramKey as $currentKey) {
381+
$params[$currentKey] =& $value;
382+
$types[$currentKey] = $this->typesKeyMapping[$key];
389383
}
390384
}
391385
} else {

src/Sqlsrv/SqlsrvStatement.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function prepareParameterKeyMapping($sql)
163163
$quoteChar = '';
164164
$literal = '';
165165
$mapping = [];
166-
$replace = [];
166+
$position = 0;
167167
$matches = [];
168168
$pattern = '/([:][a-zA-Z0-9_]+)/';
169169

@@ -202,17 +202,15 @@ public function prepareParameterKeyMapping($sql)
202202
$literal .= substr($substring, 0, $match[1]);
203203
}
204204

205-
if (isset($mapping[$match[0]])) {
206-
$mapping[$match[0]] = is_array($mapping[$match[0]]) ? $mapping[$match[0]] : [$mapping[$match[0]]];
207-
$mapping[$match[0]][] = \count($mapping);
208-
} else {
209-
$mapping[$match[0]] = \count($mapping);
205+
if (!isset($mapping[$match[0]])) {
206+
$mapping[$match[0]] = [];
210207
}
211208

209+
$mapping[$match[0]][] = $position++;
212210
$endOfPlaceholder = $match[1] + strlen($match[0]);
213211
$beginOfNextPlaceholder = $matches[0][$i + 1][1] ?? strlen($substring);
214212
$beginOfNextPlaceholder -= $endOfPlaceholder;
215-
$literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder);
213+
$literal .= '?' . substr($substring, $endOfPlaceholder, $beginOfNextPlaceholder);
216214
}
217215
} else {
218216
$literal .= $substring;
@@ -492,12 +490,8 @@ private function prepare()
492490
if (isset($this->parameterKeyMapping[$key])) {
493491
$paramKey = $this->parameterKeyMapping[$key];
494492

495-
if (is_scalar($this->parameterKeyMapping[$key])) {
496-
$params[$paramKey] = $variable;
497-
} else {
498-
foreach ($paramKey as $currentKey) {
499-
$params[$currentKey] = $variable;
500-
}
493+
foreach ($paramKey as $currentKey) {
494+
$params[$currentKey] = $variable;
501495
}
502496
} else {
503497
$params[] = $variable;

0 commit comments

Comments
 (0)