@@ -39,7 +39,7 @@ class SqlFormatter
39
39
'CHARSET ' , 'CHECK ' , 'CHECKSUM ' , 'COLLATE ' , 'COLLATION ' , 'COLUMN ' , 'COLUMNS ' , 'COMMENT ' , 'COMMIT ' , 'COMMITTED ' , 'COMPRESSED ' , 'CONCURRENT ' ,
40
40
'CONSTRAINT ' , 'CONTAINS ' , 'CONVERT ' , 'CREATE ' , 'CROSS ' , 'CURRENT_TIMESTAMP ' , 'DATABASE ' , 'DATABASES ' , 'DAY ' , 'DAY_HOUR ' , 'DAY_MINUTE ' ,
41
41
'DAY_SECOND ' , 'DEFAULT ' , 'DEFINER ' , 'DELAYED ' , 'DELETE ' , 'DESC ' , 'DESCRIBE ' , 'DETERMINISTIC ' , 'DISTINCT ' , 'DISTINCTROW ' , 'DIV ' ,
42
- 'DO ' , 'DUMPFILE ' , 'DUPLICATE ' , 'DYNAMIC ' , 'ELSE ' , 'ENCLOSED ' , 'END ' , 'ENGINE ' , 'ENGINE_TYPE ' , 'ENGINES ' , 'ESCAPE ' , 'ESCAPED ' , 'EVENTS ' , 'EXEC ' ,
42
+ 'DO ' , 'DUMPFILE ' , 'DUPLICATE ' , 'DYNAMIC ' , 'ELSE ' , 'ENCLOSED ' , 'END ' , 'ENGINE ' , 'ENGINE_TYPE ' , 'ENGINES ' , 'ESCAPE ' , 'ESCAPED ' , 'EVENTS ' , 'EXEC ' ,
43
43
'EXECUTE ' , 'EXISTS ' , 'EXPLAIN ' , 'EXTENDED ' , 'FAST ' , 'FIELDS ' , 'FILE ' , 'FIRST ' , 'FIXED ' , 'FLUSH ' , 'FOR ' , 'FORCE ' , 'FOREIGN ' , 'FULL ' , 'FULLTEXT ' ,
44
44
'FUNCTION ' , 'GLOBAL ' , 'GRANT ' , 'GRANTS ' , 'GROUP_CONCAT ' , 'HEAP ' , 'HIGH_PRIORITY ' , 'HOSTS ' , 'HOUR ' , 'HOUR_MINUTE ' ,
45
45
'HOUR_SECOND ' , 'IDENTIFIED ' , 'IF ' , 'IFNULL ' , 'IGNORE ' , 'IN ' , 'INDEX ' , 'INDEXES ' , 'INFILE ' , 'INSERT ' , 'INSERT_ID ' , 'INSERT_METHOD ' , 'INTERVAL ' ,
@@ -122,6 +122,13 @@ class SqlFormatter
122
122
// If not defined, it will be determined automatically
123
123
public static $ cli ;
124
124
125
+ // Comments
126
+ public static $ commentTokens = [
127
+ ['# ' ],
128
+ ['-- ' ],
129
+ ['/* ' , '*/ ' ]
130
+ ];
131
+
125
132
// For CLI syntax highlighting
126
133
public static $ cli_quote = "\x1b[34;1m " ;
127
134
public static $ cli_backtick_quote = "\x1b[35;1m " ;
@@ -214,38 +221,33 @@ protected static function getNextToken($string, $previous = null)
214
221
}
215
222
216
223
// Comment
217
- if ($ string [0 ] === '# ' || (isset ($ string [1 ])&&($ string [0 ]==='- ' &&$ string [1 ]==='- ' ) || ($ string [0 ]==='/ ' &&isset ($ string [1 ])&&$ string [1 ]==='* ' ))) {
218
- // Comment until end of line
219
- if ($ string [0 ] === '- ' || $ string [0 ] === '# ' ) {
220
- $ last = strpos ($ string , "\n" );
221
- $ type = self ::TOKEN_TYPE_COMMENT ;
222
- } else { // Comment until closing comment tag or end of query
223
- if (strpos ($ string , "*/ " , 2 ) !== false ) {
224
- $ last = strpos ($ string , "*/ " , 2 ) + 2 ;
225
- } else {
226
- $ last = strlen ($ string );
227
- }
228
- $ type = self ::TOKEN_TYPE_BLOCK_COMMENT ;
229
- }
230
-
231
- if ($ last === false ) {
232
- $ last = strlen ($ string );
224
+ $ found = false ;
225
+ foreach (self ::$ commentTokens as $ comment ) {
226
+ $ start = $ comment [0 ];
227
+ $ end = isset ($ comment [1 ]) ? $ comment [1 ] : "\n" ;
228
+ $ found = substr ($ string , 0 , strlen ($ start )) === $ start ;
229
+ if ($ found ) {
230
+ break ;
233
231
}
234
-
232
+ }
233
+ if ($ found ) {
234
+ /** @var string $start */
235
+ /** @var string $end */
236
+ $ endPos = strpos ($ string , $ end , strlen ($ start ));
237
+ $ last = $ endPos ?
238
+ ($ endPos + ($ end === "\n" ? 0 : strlen ($ end ))) : strlen ($ string );
235
239
return array (
236
240
self ::TOKEN_VALUE => substr ($ string , 0 , $ last ),
237
- self ::TOKEN_TYPE => $ type
241
+ self ::TOKEN_TYPE => $ end === "\n" ? self :: TOKEN_TYPE_COMMENT : self :: TOKEN_TYPE_BLOCK_COMMENT
238
242
);
239
243
}
240
244
241
245
// Quoted String
242
246
if ($ string [0 ]==='" ' || $ string [0 ]==='\'' || $ string [0 ]==='` ' || $ string [0 ]==='[ ' ) {
243
- $ return = array (
247
+ return array (
244
248
self ::TOKEN_TYPE => (($ string [0 ]==='` ' || $ string [0 ]==='[ ' )? self ::TOKEN_TYPE_BACKTICK_QUOTE : self ::TOKEN_TYPE_QUOTE ),
245
249
self ::TOKEN_VALUE => self ::getQuotedString ($ string )
246
250
);
247
-
248
- return $ return ;
249
251
}
250
252
251
253
// User-defined Variable
@@ -254,7 +256,7 @@ protected static function getNextToken($string, $previous = null)
254
256
self ::TOKEN_VALUE => null ,
255
257
self ::TOKEN_TYPE => self ::TOKEN_TYPE_VARIABLE
256
258
);
257
-
259
+
258
260
// If the variable name is quoted
259
261
if ($ string [1 ]==='" ' || $ string [1 ]==='\'' || $ string [1 ]==='` ' ) {
260
262
$ ret [self ::TOKEN_VALUE ] = $ string [0 ].self ::getQuotedString (substr ($ string ,1 ));
@@ -266,7 +268,7 @@ protected static function getNextToken($string, $previous = null)
266
268
$ ret [self ::TOKEN_VALUE ] = $ matches [1 ];
267
269
}
268
270
}
269
-
271
+
270
272
if ($ ret [self ::TOKEN_VALUE ] !== null ) return $ ret ;
271
273
}
272
274
@@ -336,7 +338,7 @@ protected static function getNextToken($string, $previous = null)
336
338
protected static function getQuotedString ($ string )
337
339
{
338
340
$ ret = null ;
339
-
341
+
340
342
// This checks for the following patterns:
341
343
// 1. backtick quoted string using `` to escape
342
344
// 2. square bracket quoted string (SQL Server) using ]] to escape
@@ -345,7 +347,7 @@ protected static function getQuotedString($string)
345
347
if ( preg_match ('/^(((`[^`]*($|`))+)|((\[[^\]]*($|\]))(\][^\]]*($|\]))*)|(("[^" \\\\]*(?: \\\\.[^" \\\\]*)*("|$))+)|(( \'[^ \'\\\\]*(?: \\\\.[^ \'\\\\]*)*( \'|$))+))/s ' , $ string , $ matches )) {
346
348
$ ret = $ matches [1 ];
347
349
}
348
-
350
+
349
351
return $ ret ;
350
352
}
351
353
@@ -699,14 +701,14 @@ public static function format($string, $highlight=true)
699
701
if ($ token [self ::TOKEN_VALUE ] === '( ' || $ token [self ::TOKEN_VALUE ] === '. ' ) {
700
702
$ return = rtrim ($ return ,' ' );
701
703
}
702
-
704
+
703
705
// If this is the "-" of a negative number, it shouldn't have a space after it
704
706
if ($ token [self ::TOKEN_VALUE ] === '- ' && isset ($ tokens [$ i +1 ]) && $ tokens [$ i +1 ][self ::TOKEN_TYPE ] === self ::TOKEN_TYPE_NUMBER && isset ($ tokens [$ i -1 ])) {
705
707
$ prev = $ tokens [$ i -1 ][self ::TOKEN_TYPE ];
706
708
if ($ prev !== self ::TOKEN_TYPE_QUOTE && $ prev !== self ::TOKEN_TYPE_BACKTICK_QUOTE && $ prev !== self ::TOKEN_TYPE_WORD && $ prev !== self ::TOKEN_TYPE_NUMBER ) {
707
709
$ return = rtrim ($ return ,' ' );
708
710
}
709
- }
711
+ }
710
712
}
711
713
712
714
// If there are unmatched parentheses
0 commit comments