@@ -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,12 +167,27 @@ 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+ $ 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+
168185 $ currentToken .= $ character ;
169186
170187 if (!$ isEscaped && in_array ($ character , ['" ' , "' " ], true )) {
171188 // Checking if the previous or next value is a ' to handle the weird SQL strings
172189 // This will not check if the amount of quotes is even
173- $ nextCharacter = $ this -> getCharacterAtIndex ( $ sql2 , $ index + 1 ) ;
190+ $ nextCharacter = $ splitString [ $ index + 1 ] ?? '' ;
174191 if ($ character === "' " && $ nextCharacter === "' " ) {
175192 $ isEscaped = true ;
176193 $ escapedQuotesCount ++;
@@ -188,6 +205,12 @@ protected function scan($sql2)
188205 } elseif (!$ stringStartCharacter ) {
189206 // If there is no start character already we have found the beginning of a new string
190207 $ 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+ }
191214 }
192215 }
193216 $ isEscaped = $ character === '\\' ;
@@ -203,12 +226,10 @@ protected function scan($sql2)
203226 return $ tokens ;
204227 }
205228
206- private function getCharacterAtIndex ( $ string, $ index )
229+ private function parseBrackets ( string $ query , int $ index ): int
207230 {
208- if ($ index < strlen ($ string )) {
209- return $ string [$ index ];
210- }
231+ $ endPosition = strpos ($ query , '] ' , $ index ) + 1 ;
211232
212- return '' ;
233+ return $ endPosition - $ index ;
213234 }
214235}
0 commit comments