Skip to content

Commit f8b891e

Browse files
committed
Fixing tests for brackets
1 parent 3315407 commit f8b891e

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/PHPCR/Util/QOM/Sql2Scanner.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ protected function scan($sql2)
149149
$stringStartCharacter = false;
150150
$isEscaped = false;
151151
$escapedQuotesCount = 0;
152-
foreach (\str_split($sql2) as $index => $character) {
152+
$splitString = \str_split($sql2);
153+
for ($index = 0; $index < count($splitString); $index++) {
154+
$character = $splitString[$index];
153155
if (!$stringStartCharacter && in_array($character, [' ', "\t", "\n"], true)) {
154156
if ($currentToken !== '') {
155157
$tokens[] = $currentToken;
@@ -165,6 +167,19 @@ protected function scan($sql2)
165167
$currentToken = '';
166168
continue;
167169
}
170+
171+
// Handling the squared brackets in queries
172+
if (!$isEscaped && $character === '[') {
173+
if ($currentToken !== '') {
174+
$tokens[] = $currentToken;
175+
}
176+
$stringSize = $this->parseBrackets($sql2, $index);
177+
$tokens[] = substr($sql2, $index, $stringSize);
178+
// We need to subtract one here because the for loop will automatically increment the index
179+
$index += $stringSize - 1;
180+
continue;
181+
}
182+
168183
$currentToken .= $character;
169184

170185
if (!$isEscaped && in_array($character, ['"', "'"], true)) {
@@ -217,4 +232,11 @@ private function getCharacterAtIndex($string, $index)
217232

218233
return '';
219234
}
235+
236+
private function parseBrackets(string $query, int $index): int
237+
{
238+
$endPosition = strpos($query, ']', $index) + 1;
239+
240+
return $endPosition - $index;
241+
}
220242
}

tests/PHPCR/Tests/Util/QOM/Sql2ScannerTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ public function testSquareBrackets()
135135
'(',
136136
'file',
137137
',',
138-
'[',
139-
'"/home node"',
140-
']',
138+
'["/home node"]',
141139
')',
142140
];
143141

@@ -155,8 +153,7 @@ public function testSquareBracketsWithoutQuotes()
155153
'(',
156154
'file',
157155
',',
158-
'[/home',
159-
'node]',
156+
'[/home node]',
160157
')',
161158
];
162159

0 commit comments

Comments
 (0)