@@ -149,7 +149,9 @@ protected function scan($sql2)
149
149
$ stringStartCharacter = false ;
150
150
$ isEscaped = false ;
151
151
$ 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 ];
153
155
if (!$ stringStartCharacter && in_array ($ character , [' ' , "\t" , "\n" ], true )) {
154
156
if ($ currentToken !== '' ) {
155
157
$ tokens [] = $ currentToken ;
@@ -165,12 +167,27 @@ protected function scan($sql2)
165
167
$ currentToken = '' ;
166
168
continue ;
167
169
}
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
+ $ bracketContent = substr ($ sql2 , $ index + 1 , $ stringSize - 2 );
178
+ $ tokens [] = '[ ' .trim ($ bracketContent , '" ' ).'] ' ;
179
+
180
+ // We need to subtract one here because the for loop will automatically increment the index
181
+ $ index += $ stringSize - 1 ;
182
+ continue ;
183
+ }
184
+
168
185
$ currentToken .= $ character ;
169
186
170
187
if (!$ isEscaped && in_array ($ character , ['" ' , "' " ], true )) {
171
188
// Checking if the previous or next value is a ' to handle the weird SQL strings
172
189
// This will not check if the amount of quotes is even
173
- $ nextCharacter = $ this -> getCharacterAtIndex ( $ sql2 , $ index + 1 ) ;
190
+ $ nextCharacter = $ splitString [ $ index + 1 ] ?? '' ;
174
191
if ($ character === "' " && $ nextCharacter === "' " ) {
175
192
$ isEscaped = true ;
176
193
$ escapedQuotesCount ++;
@@ -188,6 +205,12 @@ protected function scan($sql2)
188
205
} elseif (!$ stringStartCharacter ) {
189
206
// If there is no start character already we have found the beginning of a new string
190
207
$ stringStartCharacter = $ character ;
208
+
209
+ // When tokenizing `AS"abc"` add the current token (AS) as token already
210
+ if (strlen ($ currentToken ) > 1 ) {
211
+ $ tokens [] = substr ($ currentToken , 0 , strlen ($ currentToken ) - 1 );
212
+ $ currentToken = $ character ;
213
+ }
191
214
}
192
215
}
193
216
$ isEscaped = $ character === '\\' ;
@@ -203,12 +226,10 @@ protected function scan($sql2)
203
226
return $ tokens ;
204
227
}
205
228
206
- private function getCharacterAtIndex ( $ string, $ index )
229
+ private function parseBrackets ( string $ query , int $ index ): int
207
230
{
208
- if ($ index < strlen ($ string )) {
209
- return $ string [$ index ];
210
- }
231
+ $ endPosition = strpos ($ query , '] ' , $ index ) + 1 ;
211
232
212
- return '' ;
233
+ return $ endPosition - $ index ;
213
234
}
214
235
}
0 commit comments