Took ".number_format($end-$start,5)." seconds total, ".number_format(($end-$start)/$num,5)." seconds per query, ".number_format(1000*($end-$start)/$chars,5)." seconds per 1000 characters
";
40 |
41 | echo "
9 | * @copyright 2013 Jeremy Dorn
10 | * @license http://opensource.org/licenses/MIT
11 | * @link http://github.com/jdorn/sql-formatter
12 | * @version 1.2.18
13 | */
14 | class SqlFormatter
15 | {
16 | // Constants for token types
17 | const TOKEN_TYPE_WHITESPACE = 0;
18 | const TOKEN_TYPE_WORD = 1;
19 | const TOKEN_TYPE_QUOTE = 2;
20 | const TOKEN_TYPE_BACKTICK_QUOTE = 3;
21 | const TOKEN_TYPE_RESERVED = 4;
22 | const TOKEN_TYPE_RESERVED_TOPLEVEL = 5;
23 | const TOKEN_TYPE_RESERVED_NEWLINE = 6;
24 | const TOKEN_TYPE_BOUNDARY = 7;
25 | const TOKEN_TYPE_COMMENT = 8;
26 | const TOKEN_TYPE_BLOCK_COMMENT = 9;
27 | const TOKEN_TYPE_NUMBER = 10;
28 | const TOKEN_TYPE_ERROR = 11;
29 | const TOKEN_TYPE_VARIABLE = 12;
30 |
31 | // Constants for different components of a token
32 | const TOKEN_TYPE = 0;
33 | const TOKEN_VALUE = 1;
34 |
35 | // Reserved words (for syntax highlighting)
36 | protected static $reserved = array(
37 | 'ACCESSIBLE', 'ACTION', 'AGAINST', 'AGGREGATE', 'ALGORITHM', 'ALL', 'ALTER', 'ANALYSE', 'ANALYZE', 'AS', 'ASC',
38 | 'AUTOCOMMIT', 'AUTO_INCREMENT', 'BACKUP', 'BEGIN', 'BETWEEN', 'BINLOG', 'BOTH', 'CASCADE', 'CASE', 'CHANGE', 'CHANGED', 'CHARACTER SET',
39 | 'CHARSET', 'CHECK', 'CHECKSUM', 'COLLATE', 'COLLATION', 'COLUMN', 'COLUMNS', 'COMMENT', 'COMMIT', 'COMMITTED', 'COMPRESSED', 'CONCURRENT',
40 | 'CONSTRAINT', 'CONTAINS', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_TIMESTAMP', 'DATABASE', 'DATABASES', 'DAY', 'DAY_HOUR', 'DAY_MINUTE',
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',
43 | 'EXECUTE', 'EXISTS', 'EXPLAIN', 'EXTENDED', 'FAST', 'FIELDS', 'FILE', 'FIRST', 'FIXED', 'FLUSH', 'FOR', 'FORCE', 'FOREIGN', 'FULL', 'FULLTEXT',
44 | 'FUNCTION', 'GLOBAL', 'GRANT', 'GRANTS', 'GROUP_CONCAT', 'HEAP', 'HIGH_PRIORITY', 'HOSTS', 'HOUR', 'HOUR_MINUTE',
45 | 'HOUR_SECOND', 'IDENTIFIED', 'IF', 'IFNULL', 'IGNORE', 'IN', 'INDEX', 'INDEXES', 'INFILE', 'INSERT', 'INSERT_ID', 'INSERT_METHOD', 'INTERVAL',
46 | 'INTO', 'INVOKER', 'IS', 'ISOLATION', 'KEY', 'KEYS', 'KILL', 'LAST_INSERT_ID', 'LEADING', 'LEVEL', 'LIKE', 'LINEAR',
47 | 'LINES', 'LOAD', 'LOCAL', 'LOCK', 'LOCKS', 'LOGS', 'LOW_PRIORITY', 'MARIA', 'MASTER', 'MASTER_CONNECT_RETRY', 'MASTER_HOST', 'MASTER_LOG_FILE',
48 | 'MATCH','MAX_CONNECTIONS_PER_HOUR', 'MAX_QUERIES_PER_HOUR', 'MAX_ROWS', 'MAX_UPDATES_PER_HOUR', 'MAX_USER_CONNECTIONS',
49 | 'MEDIUM', 'MERGE', 'MINUTE', 'MINUTE_SECOND', 'MIN_ROWS', 'MODE', 'MODIFY',
50 | 'MONTH', 'MRG_MYISAM', 'MYISAM', 'NAMES', 'NATURAL', 'NOT', 'NOW()','NULL', 'OFFSET', 'ON', 'OPEN', 'OPTIMIZE', 'OPTION', 'OPTIONALLY',
51 | 'ON UPDATE', 'ON DELETE', 'OUTFILE', 'PACK_KEYS', 'PAGE', 'PARTIAL', 'PARTITION', 'PARTITIONS', 'PASSWORD', 'PRIMARY', 'PRIVILEGES', 'PROCEDURE',
52 | 'PROCESS', 'PROCESSLIST', 'PURGE', 'QUICK', 'RANGE', 'RAID0', 'RAID_CHUNKS', 'RAID_CHUNKSIZE','RAID_TYPE', 'READ', 'READ_ONLY',
53 | 'READ_WRITE', 'REFERENCES', 'REGEXP', 'RELOAD', 'RENAME', 'REPAIR', 'REPEATABLE', 'REPLACE', 'REPLICATION', 'RESET', 'RESTORE', 'RESTRICT',
54 | 'RETURN', 'RETURNS', 'REVOKE', 'RLIKE', 'ROLLBACK', 'ROW', 'ROWS', 'ROW_FORMAT', 'SECOND', 'SECURITY', 'SEPARATOR',
55 | 'SERIALIZABLE', 'SESSION', 'SHARE', 'SHOW', 'SHUTDOWN', 'SLAVE', 'SONAME', 'SOUNDS', 'SQL', 'SQL_AUTO_IS_NULL', 'SQL_BIG_RESULT',
56 | 'SQL_BIG_SELECTS', 'SQL_BIG_TABLES', 'SQL_BUFFER_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQL_LOG_BIN', 'SQL_LOG_OFF', 'SQL_LOG_UPDATE',
57 | 'SQL_LOW_PRIORITY_UPDATES', 'SQL_MAX_JOIN_SIZE', 'SQL_QUOTE_SHOW_CREATE', 'SQL_SAFE_UPDATES', 'SQL_SELECT_LIMIT', 'SQL_SLAVE_SKIP_COUNTER',
58 | 'SQL_SMALL_RESULT', 'SQL_WARNINGS', 'SQL_CACHE', 'SQL_NO_CACHE', 'START', 'STARTING', 'STATUS', 'STOP', 'STORAGE',
59 | 'STRAIGHT_JOIN', 'STRING', 'STRIPED', 'SUPER', 'TABLE', 'TABLES', 'TEMPORARY', 'TERMINATED', 'THEN', 'TO', 'TRAILING', 'TRANSACTIONAL', 'TRUE',
60 | 'TRUNCATE', 'TYPE', 'TYPES', 'UNCOMMITTED', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'USAGE', 'USE', 'USING', 'VARIABLES',
61 | 'VIEW', 'WHEN', 'WITH', 'WORK', 'WRITE', 'YEAR_MONTH'
62 | );
63 |
64 | // For SQL formatting
65 | // These keywords will all be on their own line
66 | protected static $reserved_toplevel = array(
67 | 'SELECT', 'FROM', 'WHERE', 'SET', 'ORDER BY', 'GROUP BY', 'LIMIT', 'DROP',
68 | 'VALUES', 'UPDATE', 'HAVING', 'ADD', 'AFTER', 'ALTER TABLE', 'DELETE FROM', 'UNION ALL', 'UNION', 'EXCEPT', 'INTERSECT'
69 | );
70 |
71 | protected static $reserved_newline = array(
72 | 'LEFT OUTER JOIN', 'RIGHT OUTER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'OUTER JOIN', 'INNER JOIN', 'JOIN', 'XOR', 'OR', 'AND'
73 | );
74 |
75 | protected static $functions = array (
76 | 'ABS', 'ACOS', 'ADDDATE', 'ADDTIME', 'AES_DECRYPT', 'AES_ENCRYPT', 'AREA', 'ASBINARY', 'ASCII', 'ASIN', 'ASTEXT', 'ATAN', 'ATAN2',
77 | 'AVG', 'BDMPOLYFROMTEXT', 'BDMPOLYFROMWKB', 'BDPOLYFROMTEXT', 'BDPOLYFROMWKB', 'BENCHMARK', 'BIN', 'BIT_AND', 'BIT_COUNT', 'BIT_LENGTH',
78 | 'BIT_OR', 'BIT_XOR', 'BOUNDARY', 'BUFFER', 'CAST', 'CEIL', 'CEILING', 'CENTROID', 'CHAR', 'CHARACTER_LENGTH', 'CHARSET', 'CHAR_LENGTH',
79 | 'COALESCE', 'COERCIBILITY', 'COLLATION', 'COMPRESS', 'CONCAT', 'CONCAT_WS', 'CONNECTION_ID', 'CONTAINS', 'CONV', 'CONVERT', 'CONVERT_TZ',
80 | 'CONVEXHULL', 'COS', 'COT', 'COUNT', 'CRC32', 'CROSSES', 'CURDATE', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER',
81 | 'CURTIME', 'DATABASE', 'DATE', 'DATEDIFF', 'DATE_ADD', 'DATE_DIFF', 'DATE_FORMAT', 'DATE_SUB', 'DAY', 'DAYNAME', 'DAYOFMONTH', 'DAYOFWEEK',
82 | 'DAYOFYEAR', 'DECODE', 'DEFAULT', 'DEGREES', 'DES_DECRYPT', 'DES_ENCRYPT', 'DIFFERENCE', 'DIMENSION', 'DISJOINT', 'DISTANCE', 'ELT', 'ENCODE',
83 | 'ENCRYPT', 'ENDPOINT', 'ENVELOPE', 'EQUALS', 'EXP', 'EXPORT_SET', 'EXTERIORRING', 'EXTRACT', 'EXTRACTVALUE', 'FIELD', 'FIND_IN_SET', 'FLOOR',
84 | 'FORMAT', 'FOUND_ROWS', 'FROM_DAYS', 'FROM_UNIXTIME', 'GEOMCOLLFROMTEXT', 'GEOMCOLLFROMWKB', 'GEOMETRYCOLLECTION', 'GEOMETRYCOLLECTIONFROMTEXT',
85 | 'GEOMETRYCOLLECTIONFROMWKB', 'GEOMETRYFROMTEXT', 'GEOMETRYFROMWKB', 'GEOMETRYN', 'GEOMETRYTYPE', 'GEOMFROMTEXT', 'GEOMFROMWKB', 'GET_FORMAT',
86 | 'GET_LOCK', 'GLENGTH', 'GREATEST', 'GROUP_CONCAT', 'GROUP_UNIQUE_USERS', 'HEX', 'HOUR', 'IF', 'IFNULL', 'INET_ATON', 'INET_NTOA', 'INSERT', 'INSTR',
87 | 'INTERIORRINGN', 'INTERSECTION', 'INTERSECTS', 'INTERVAL', 'ISCLOSED', 'ISEMPTY', 'ISNULL', 'ISRING', 'ISSIMPLE', 'IS_FREE_LOCK', 'IS_USED_LOCK',
88 | 'LAST_DAY', 'LAST_INSERT_ID', 'LCASE', 'LEAST', 'LEFT', 'LENGTH', 'LINEFROMTEXT', 'LINEFROMWKB', 'LINESTRING', 'LINESTRINGFROMTEXT', 'LINESTRINGFROMWKB',
89 | 'LN', 'LOAD_FILE', 'LOCALTIME', 'LOCALTIMESTAMP', 'LOCATE', 'LOG', 'LOG10', 'LOG2', 'LOWER', 'LPAD', 'LTRIM', 'MAKEDATE', 'MAKETIME', 'MAKE_SET',
90 | 'MASTER_POS_WAIT', 'MAX', 'MBRCONTAINS', 'MBRDISJOINT', 'MBREQUAL', 'MBRINTERSECTS', 'MBROVERLAPS', 'MBRTOUCHES', 'MBRWITHIN', 'MD5', 'MICROSECOND',
91 | 'MID', 'MIN', 'MINUTE', 'MLINEFROMTEXT', 'MLINEFROMWKB', 'MOD', 'MONTH', 'MONTHNAME', 'MPOINTFROMTEXT', 'MPOINTFROMWKB', 'MPOLYFROMTEXT', 'MPOLYFROMWKB',
92 | 'MULTILINESTRING', 'MULTILINESTRINGFROMTEXT', 'MULTILINESTRINGFROMWKB', 'MULTIPOINT', 'MULTIPOINTFROMTEXT', 'MULTIPOINTFROMWKB', 'MULTIPOLYGON',
93 | 'MULTIPOLYGONFROMTEXT', 'MULTIPOLYGONFROMWKB', 'NAME_CONST', 'NULLIF', 'NUMGEOMETRIES', 'NUMINTERIORRINGS', 'NUMPOINTS', 'OCT', 'OCTET_LENGTH',
94 | 'OLD_PASSWORD', 'ORD', 'OVERLAPS', 'PASSWORD', 'PERIOD_ADD', 'PERIOD_DIFF', 'PI', 'POINT', 'POINTFROMTEXT', 'POINTFROMWKB', 'POINTN', 'POINTONSURFACE',
95 | 'POLYFROMTEXT', 'POLYFROMWKB', 'POLYGON', 'POLYGONFROMTEXT', 'POLYGONFROMWKB', 'POSITION', 'POW', 'POWER', 'QUARTER', 'QUOTE', 'RADIANS', 'RAND',
96 | 'RELATED', 'RELEASE_LOCK', 'REPEAT', 'REPLACE', 'REVERSE', 'RIGHT', 'ROUND', 'ROW_COUNT', 'RPAD', 'RTRIM', 'SCHEMA', 'SECOND', 'SEC_TO_TIME',
97 | 'SESSION_USER', 'SHA', 'SHA1', 'SIGN', 'SIN', 'SLEEP', 'SOUNDEX', 'SPACE', 'SQRT', 'SRID', 'STARTPOINT', 'STD', 'STDDEV', 'STDDEV_POP', 'STDDEV_SAMP',
98 | 'STRCMP', 'STR_TO_DATE', 'SUBDATE', 'SUBSTR', 'SUBSTRING', 'SUBSTRING_INDEX', 'SUBTIME', 'SUM', 'SYMDIFFERENCE', 'SYSDATE', 'SYSTEM_USER', 'TAN',
99 | 'TIME', 'TIMEDIFF', 'TIMESTAMP', 'TIMESTAMPADD', 'TIMESTAMPDIFF', 'TIME_FORMAT', 'TIME_TO_SEC', 'TOUCHES', 'TO_DAYS', 'TRIM', 'TRUNCATE', 'UCASE',
100 | 'UNCOMPRESS', 'UNCOMPRESSED_LENGTH', 'UNHEX', 'UNIQUE_USERS', 'UNIX_TIMESTAMP', 'UPDATEXML', 'UPPER', 'USER', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP',
101 | 'UUID', 'VARIANCE', 'VAR_POP', 'VAR_SAMP', 'VERSION', 'WEEK', 'WEEKDAY', 'WEEKOFYEAR', 'WITHIN', 'X', 'Y', 'YEAR', 'YEARWEEK'
102 | );
103 |
104 | // Punctuation that can be used as a boundary between other tokens
105 | protected static $boundaries = array(',', ';',':', ')', '(', '.', '=', '<', '>', '+', '-', '*', '/', '!', '^', '%', '|', '&', '#');
106 |
107 | // For HTML syntax highlighting
108 | // Styles applied to different token types
109 | public static $quote_attributes = 'style="color: blue;"';
110 | public static $backtick_quote_attributes = 'style="color: purple;"';
111 | public static $reserved_attributes = 'style="font-weight:bold;"';
112 | public static $boundary_attributes = '';
113 | public static $number_attributes = 'style="color: green;"';
114 | public static $word_attributes = 'style="color: #333;"';
115 | public static $error_attributes = 'style="background-color: red;"';
116 | public static $comment_attributes = 'style="color: #aaa;"';
117 | public static $variable_attributes = 'style="color: orange;"';
118 | public static $pre_attributes = 'style="color: black; background-color: white;"';
119 |
120 | // Boolean - whether or not the current environment is the CLI
121 | // This affects the type of syntax highlighting
122 | // If not defined, it will be determined automatically
123 | public static $cli;
124 |
125 | // For CLI syntax highlighting
126 | public static $cli_quote = "\x1b[34;1m";
127 | public static $cli_backtick_quote = "\x1b[35;1m";
128 | public static $cli_reserved = "\x1b[37m";
129 | public static $cli_boundary = "";
130 | public static $cli_number = "\x1b[32;1m";
131 | public static $cli_word = "";
132 | public static $cli_error = "\x1b[31;1;7m";
133 | public static $cli_comment = "\x1b[30;1m";
134 | public static $cli_functions = "\x1b[37m";
135 | public static $cli_variable = "\x1b[36;1m";
136 |
137 | // The tab character to use when formatting SQL
138 | public static $tab = ' ';
139 |
140 | // This flag tells us if queries need to be enclosed in tags
141 | public static $use_pre = true;
142 |
143 | // This flag tells us if SqlFormatted has been initialized
144 | protected static $init;
145 |
146 | // Regular expressions for tokenizing
147 | protected static $regex_boundaries;
148 | protected static $regex_reserved;
149 | protected static $regex_reserved_newline;
150 | protected static $regex_reserved_toplevel;
151 | protected static $regex_function;
152 |
153 | // Cache variables
154 | // Only tokens shorter than this size will be cached. Somewhere between 10 and 20 seems to work well for most cases.
155 | public static $max_cachekey_size = 15;
156 | protected static $token_cache = array();
157 | protected static $cache_hits = 0;
158 | protected static $cache_misses = 0;
159 |
160 | /**
161 | * Get stats about the token cache
162 | * @return Array An array containing the keys 'hits', 'misses', 'entries', and 'size' in bytes
163 | */
164 | public static function getCacheStats()
165 | {
166 | return array(
167 | 'hits'=>self::$cache_hits,
168 | 'misses'=>self::$cache_misses,
169 | 'entries'=>count(self::$token_cache),
170 | 'size'=>strlen(serialize(self::$token_cache))
171 | );
172 | }
173 |
174 | /**
175 | * Stuff that only needs to be done once. Builds regular expressions and sorts the reserved words.
176 | */
177 | protected static function init()
178 | {
179 | if (self::$init) return;
180 |
181 | // Sort reserved word list from longest word to shortest, 3x faster than usort
182 | $reservedMap = array_combine(self::$reserved, array_map('strlen', self::$reserved));
183 | arsort($reservedMap);
184 | self::$reserved = array_keys($reservedMap);
185 |
186 | // Set up regular expressions
187 | self::$regex_boundaries = '('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$boundaries)).')';
188 | self::$regex_reserved = '('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$reserved)).')';
189 | self::$regex_reserved_toplevel = str_replace(' ','\\s+','('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$reserved_toplevel)).')');
190 | self::$regex_reserved_newline = str_replace(' ','\\s+','('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$reserved_newline)).')');
191 |
192 | self::$regex_function = '('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$functions)).')';
193 |
194 | self::$init = true;
195 | }
196 |
197 | /**
198 | * Return the next token and token type in a SQL string.
199 | * Quoted strings, comments, reserved words, whitespace, and punctuation are all their own tokens.
200 | *
201 | * @param String $string The SQL string
202 | * @param array $previous The result of the previous getNextToken() call
203 | *
204 | * @return Array An associative array containing the type and value of the token.
205 | */
206 | protected static function getNextToken($string, $previous = null)
207 | {
208 | // Whitespace
209 | if (preg_match('/^\s+/',$string,$matches)) {
210 | return array(
211 | self::TOKEN_VALUE => $matches[0],
212 | self::TOKEN_TYPE=>self::TOKEN_TYPE_WHITESPACE
213 | );
214 | }
215 |
216 | // Comment
217 | if ($string[0] === '#' || (isset($string[1])&&($string[0]==='-'&&$string[1]==='-') || ($string[0]==='/'&&$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
223 | $last = strpos($string, "*/", 2) + 2;
224 | $type = self::TOKEN_TYPE_BLOCK_COMMENT;
225 | }
226 |
227 | if ($last === false) {
228 | $last = strlen($string);
229 | }
230 |
231 | return array(
232 | self::TOKEN_VALUE => substr($string, 0, $last),
233 | self::TOKEN_TYPE => $type
234 | );
235 | }
236 |
237 | // Quoted String
238 | if ($string[0]==='"' || $string[0]==='\'' || $string[0]==='`' || $string[0]==='[') {
239 | $return = array(
240 | self::TOKEN_TYPE => (($string[0]==='`' || $string[0]==='[')? self::TOKEN_TYPE_BACKTICK_QUOTE : self::TOKEN_TYPE_QUOTE),
241 | self::TOKEN_VALUE => self::getQuotedString($string)
242 | );
243 |
244 | return $return;
245 | }
246 |
247 | // User-defined Variable
248 | if (($string[0] === '@' || $string[0] === ':') && isset($string[1])) {
249 | $ret = array(
250 | self::TOKEN_VALUE => null,
251 | self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE
252 | );
253 |
254 | // If the variable name is quoted
255 | if ($string[1]==='"' || $string[1]==='\'' || $string[1]==='`') {
256 | $ret[self::TOKEN_VALUE] = $string[0].self::getQuotedString(substr($string,1));
257 | }
258 | // Non-quoted variable name
259 | else {
260 | preg_match('/^('.$string[0].'[a-zA-Z0-9\._\$]+)/',$string,$matches);
261 | if ($matches) {
262 | $ret[self::TOKEN_VALUE] = $matches[1];
263 | }
264 | }
265 |
266 | if($ret[self::TOKEN_VALUE] !== null) return $ret;
267 | }
268 |
269 | // Number (decimal, binary, or hex)
270 | if (preg_match('/^([0-9]+(\.[0-9]+)?|0x[0-9a-fA-F]+|0b[01]+)($|\s|"\'`|'.self::$regex_boundaries.')/',$string,$matches)) {
271 | return array(
272 | self::TOKEN_VALUE => $matches[1],
273 | self::TOKEN_TYPE=>self::TOKEN_TYPE_NUMBER
274 | );
275 | }
276 |
277 | // Boundary Character (punctuation and symbols)
278 | if (preg_match('/^('.self::$regex_boundaries.')/',$string,$matches)) {
279 | return array(
280 | self::TOKEN_VALUE => $matches[1],
281 | self::TOKEN_TYPE => self::TOKEN_TYPE_BOUNDARY
282 | );
283 | }
284 |
285 | // A reserved word cannot be preceded by a '.'
286 | // this makes it so in "mytable.from", "from" is not considered a reserved word
287 | if (!$previous || !isset($previous[self::TOKEN_VALUE]) || $previous[self::TOKEN_VALUE] !== '.') {
288 | $upper = strtoupper($string);
289 | // Top Level Reserved Word
290 | if (preg_match('/^('.self::$regex_reserved_toplevel.')($|\s|'.self::$regex_boundaries.')/', $upper,$matches)) {
291 | return array(
292 | self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED_TOPLEVEL,
293 | self::TOKEN_VALUE=>substr($string,0,strlen($matches[1]))
294 | );
295 | }
296 | // Newline Reserved Word
297 | if (preg_match('/^('.self::$regex_reserved_newline.')($|\s|'.self::$regex_boundaries.')/', $upper,$matches)) {
298 | return array(
299 | self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED_NEWLINE,
300 | self::TOKEN_VALUE=>substr($string,0,strlen($matches[1]))
301 | );
302 | }
303 | // Other Reserved Word
304 | if (preg_match('/^('.self::$regex_reserved.')($|\s|'.self::$regex_boundaries.')/', $upper,$matches)) {
305 | return array(
306 | self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED,
307 | self::TOKEN_VALUE=>substr($string,0,strlen($matches[1]))
308 | );
309 | }
310 | }
311 |
312 | // A function must be suceeded by '('
313 | // this makes it so "count(" is considered a function, but "count" alone is not
314 | $upper = strtoupper($string);
315 | // function
316 | if (preg_match('/^('.self::$regex_function.'[(]|\s|[)])/', $upper,$matches)) {
317 | return array(
318 | self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED,
319 | self::TOKEN_VALUE=>substr($string,0,strlen($matches[1])-1)
320 | );
321 | }
322 |
323 | // Non reserved word
324 | preg_match('/^(.*?)($|\s|["\'`]|'.self::$regex_boundaries.')/',$string,$matches);
325 |
326 | return array(
327 | self::TOKEN_VALUE => $matches[1],
328 | self::TOKEN_TYPE => self::TOKEN_TYPE_WORD
329 | );
330 | }
331 |
332 | protected static function getQuotedString($string)
333 | {
334 | $ret = null;
335 |
336 | // This checks for the following patterns:
337 | // 1. backtick quoted string using `` to escape
338 | // 2. square bracket quoted string (SQL Server) using ]] to escape
339 | // 3. double quoted string using "" or \" to escape
340 | // 4. single quoted string using '' or \' to escape
341 | if ( preg_match('/^(((`[^`]*($|`))+)|((\[[^\]]*($|\]))(\][^\]]*($|\]))*)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) {
342 | $ret = $matches[1];
343 | }
344 |
345 | return $ret;
346 | }
347 |
348 | /**
349 | * Takes a SQL string and breaks it into tokens.
350 | * Each token is an associative array with type and value.
351 | *
352 | * @param String $string The SQL string
353 | *
354 | * @return Array An array of tokens.
355 | */
356 | protected static function tokenize($string)
357 | {
358 | self::init();
359 |
360 | $tokens = array();
361 |
362 | // Used for debugging if there is an error while tokenizing the string
363 | $original_length = strlen($string);
364 |
365 | // Used to make sure the string keeps shrinking on each iteration
366 | $old_string_len = strlen($string) + 1;
367 |
368 | $token = null;
369 |
370 | $current_length = strlen($string);
371 |
372 | // Keep processing the string until it is empty
373 | while ($current_length) {
374 | // If the string stopped shrinking, there was a problem
375 | if ($old_string_len <= $current_length) {
376 | $tokens[] = array(
377 | self::TOKEN_VALUE=>$string,
378 | self::TOKEN_TYPE=>self::TOKEN_TYPE_ERROR
379 | );
380 |
381 | return $tokens;
382 | }
383 | $old_string_len = $current_length;
384 |
385 | // Determine if we can use caching
386 | if ($current_length >= self::$max_cachekey_size) {
387 | $cacheKey = substr($string,0,self::$max_cachekey_size);
388 | } else {
389 | $cacheKey = false;
390 | }
391 |
392 | // See if the token is already cached
393 | if ($cacheKey && isset(self::$token_cache[$cacheKey])) {
394 | // Retrieve from cache
395 | $token = self::$token_cache[$cacheKey];
396 | $token_length = strlen($token[self::TOKEN_VALUE]);
397 | self::$cache_hits++;
398 | } else {
399 | // Get the next token and the token type
400 | $token = self::getNextToken($string, $token);
401 | $token_length = strlen($token[self::TOKEN_VALUE]);
402 | self::$cache_misses++;
403 |
404 | // If the token is shorter than the max length, store it in cache
405 | if ($cacheKey && $token_length < self::$max_cachekey_size) {
406 | self::$token_cache[$cacheKey] = $token;
407 | }
408 | }
409 |
410 | $tokens[] = $token;
411 |
412 | // Advance the string
413 | $string = substr($string, $token_length);
414 |
415 | $current_length -= $token_length;
416 | }
417 |
418 | return $tokens;
419 | }
420 |
421 | /**
422 | * Format the whitespace in a SQL string to make it easier to read.
423 | *
424 | * @param String $string The SQL string
425 | * @param boolean $highlight If true, syntax highlighting will also be performed
426 | *
427 | * @return String The SQL string with HTML styles and formatting wrapped in a tag
428 | */
429 | public static function format($string, $highlight=true)
430 | {
431 | // This variable will be populated with formatted html
432 | $return = '';
433 |
434 | // Use an actual tab while formatting and then switch out with self::$tab at the end
435 | $tab = "\t";
436 |
437 | $indent_level = 0;
438 | $newline = false;
439 | $inline_parentheses = false;
440 | $increase_special_indent = false;
441 | $increase_block_indent = false;
442 | $indent_types = array();
443 | $added_newline = false;
444 | $inline_count = 0;
445 | $inline_indented = false;
446 | $clause_limit = false;
447 |
448 | // Tokenize String
449 | $original_tokens = self::tokenize($string);
450 |
451 | // Remove existing whitespace
452 | $tokens = array();
453 | foreach ($original_tokens as $i=>$token) {
454 | if ($token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
455 | $token['i'] = $i;
456 | $tokens[] = $token;
457 | }
458 | }
459 |
460 | // Format token by token
461 | foreach ($tokens as $i=>$token) {
462 | // Get highlighted token if doing syntax highlighting
463 | if ($highlight) {
464 | $highlighted = self::highlightToken($token);
465 | } else { // If returning raw text
466 | $highlighted = $token[self::TOKEN_VALUE];
467 | }
468 |
469 | // If we are increasing the special indent level now
470 | if ($increase_special_indent) {
471 | $indent_level++;
472 | $increase_special_indent = false;
473 | array_unshift($indent_types,'special');
474 | }
475 | // If we are increasing the block indent level now
476 | if ($increase_block_indent) {
477 | $indent_level++;
478 | $increase_block_indent = false;
479 | array_unshift($indent_types,'block');
480 | }
481 |
482 | // If we need a new line before the token
483 | if ($newline) {
484 | $return .= "\n" . str_repeat($tab, $indent_level);
485 | $newline = false;
486 | $added_newline = true;
487 | } else {
488 | $added_newline = false;
489 | }
490 |
491 | // Display comments directly where they appear in the source
492 | if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
493 | if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
494 | $indent = str_repeat($tab,$indent_level);
495 | $return .= "\n" . $indent;
496 | $highlighted = str_replace("\n","\n".$indent,$highlighted);
497 | }
498 |
499 | $return .= $highlighted;
500 | $newline = true;
501 | continue;
502 | }
503 |
504 | if ($inline_parentheses) {
505 | // End of inline parentheses
506 | if ($token[self::TOKEN_VALUE] === ')') {
507 | $return = rtrim($return,' ');
508 |
509 | if ($inline_indented) {
510 | array_shift($indent_types);
511 | $indent_level --;
512 | $return .= "\n" . str_repeat($tab, $indent_level);
513 | }
514 |
515 | $inline_parentheses = false;
516 |
517 | $return .= $highlighted . ' ';
518 | continue;
519 | }
520 |
521 | if ($token[self::TOKEN_VALUE] === ',') {
522 | if ($inline_count >= 30) {
523 | $inline_count = 0;
524 | $newline = true;
525 | }
526 | }
527 |
528 | $inline_count += strlen($token[self::TOKEN_VALUE]);
529 | }
530 |
531 | // Opening parentheses increase the block indent level and start a new line
532 | if ($token[self::TOKEN_VALUE] === '(') {
533 | // First check if this should be an inline parentheses block
534 | // Examples are "NOW()", "COUNT(*)", "int(10)", key(`somecolumn`), DECIMAL(7,2)
535 | // Allow up to 3 non-whitespace tokens inside inline parentheses
536 | $length = 0;
537 | for ($j=1;$j<=250;$j++) {
538 | // Reached end of string
539 | if (!isset($tokens[$i+$j])) break;
540 |
541 | $next = $tokens[$i+$j];
542 |
543 | // Reached closing parentheses, able to inline it
544 | if ($next[self::TOKEN_VALUE] === ')') {
545 | $inline_parentheses = true;
546 | $inline_count = 0;
547 | $inline_indented = false;
548 | break;
549 | }
550 |
551 | // Reached an invalid token for inline parentheses
552 | if ($next[self::TOKEN_VALUE]===';' || $next[self::TOKEN_VALUE]==='(') {
553 | break;
554 | }
555 |
556 | // Reached an invalid token type for inline parentheses
557 | if ($next[self::TOKEN_TYPE]===self::TOKEN_TYPE_RESERVED_TOPLEVEL || $next[self::TOKEN_TYPE]===self::TOKEN_TYPE_RESERVED_NEWLINE || $next[self::TOKEN_TYPE]===self::TOKEN_TYPE_COMMENT || $next[self::TOKEN_TYPE]===self::TOKEN_TYPE_BLOCK_COMMENT) {
558 | break;
559 | }
560 |
561 | $length += strlen($next[self::TOKEN_VALUE]);
562 | }
563 |
564 | if ($inline_parentheses && $length > 30) {
565 | $increase_block_indent = true;
566 | $inline_indented = true;
567 | $newline = true;
568 | }
569 |
570 | // Take out the preceding space unless there was whitespace there in the original query
571 | if (isset($original_tokens[$token['i']-1]) && $original_tokens[$token['i']-1][self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
572 | $return = rtrim($return,' ');
573 | }
574 |
575 | if (!$inline_parentheses) {
576 | $increase_block_indent = true;
577 | // Add a newline after the parentheses
578 | $newline = true;
579 | }
580 |
581 | }
582 |
583 | // Closing parentheses decrease the block indent level
584 | elseif ($token[self::TOKEN_VALUE] === ')') {
585 | // Remove whitespace before the closing parentheses
586 | $return = rtrim($return,' ');
587 |
588 | $indent_level--;
589 |
590 | // Reset indent level
591 | while ($j=array_shift($indent_types)) {
592 | if ($j==='special') {
593 | $indent_level--;
594 | } else {
595 | break;
596 | }
597 | }
598 |
599 | if ($indent_level < 0) {
600 | // This is an error
601 | $indent_level = 0;
602 |
603 | if ($highlight) {
604 | $return .= "\n".self::highlightError($token[self::TOKEN_VALUE]);
605 | continue;
606 | }
607 | }
608 |
609 | // Add a newline before the closing parentheses (if not already added)
610 | if (!$added_newline) {
611 | $return .= "\n" . str_repeat($tab, $indent_level);
612 | }
613 | }
614 |
615 | // Top level reserved words start a new line and increase the special indent level
616 | elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_TOPLEVEL) {
617 | $increase_special_indent = true;
618 |
619 | // If the last indent type was 'special', decrease the special indent for this round
620 | reset($indent_types);
621 | if (current($indent_types)==='special') {
622 | $indent_level--;
623 | array_shift($indent_types);
624 | }
625 |
626 | // Add a newline after the top level reserved word
627 | $newline = true;
628 | // Add a newline before the top level reserved word (if not already added)
629 | if (!$added_newline) {
630 | $return .= "\n" . str_repeat($tab, $indent_level);
631 | }
632 | // If we already added a newline, redo the indentation since it may be different now
633 | else {
634 | $return = rtrim($return,$tab).str_repeat($tab, $indent_level);
635 | }
636 |
637 | // If the token may have extra whitespace
638 | if (strpos($token[self::TOKEN_VALUE],' ')!==false || strpos($token[self::TOKEN_VALUE],"\n")!==false || strpos($token[self::TOKEN_VALUE],"\t")!==false) {
639 | $highlighted = preg_replace('/\s+/',' ',$highlighted);
640 | }
641 | //if SQL 'LIMIT' clause, start variable to reset newline
642 | if ($token[self::TOKEN_VALUE] === 'LIMIT' && !$inline_parentheses) {
643 | $clause_limit = true;
644 | }
645 | }
646 |
647 | // Checks if we are out of the limit clause
648 | elseif ($clause_limit && $token[self::TOKEN_VALUE] !== "," && $token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_NUMBER && $token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
649 | $clause_limit = false;
650 | }
651 |
652 | // Commas start a new line (unless within inline parentheses or SQL 'LIMIT' clause)
653 | elseif ($token[self::TOKEN_VALUE] === ',' && !$inline_parentheses) {
654 | //If the previous TOKEN_VALUE is 'LIMIT', resets new line
655 | if ($clause_limit === true) {
656 | $newline = false;
657 | $clause_limit = false;
658 | }
659 | // All other cases of commas
660 | else {
661 | $newline = true;
662 | }
663 | }
664 |
665 | // Newline reserved words start a new line
666 | elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_NEWLINE) {
667 | // Add a newline before the reserved word (if not already added)
668 | if (!$added_newline) {
669 | $return .= "\n" . str_repeat($tab, $indent_level);
670 | }
671 |
672 | // If the token may have extra whitespace
673 | if (strpos($token[self::TOKEN_VALUE],' ')!==false || strpos($token[self::TOKEN_VALUE],"\n")!==false || strpos($token[self::TOKEN_VALUE],"\t")!==false) {
674 | $highlighted = preg_replace('/\s+/',' ',$highlighted);
675 | }
676 | }
677 |
678 | // Multiple boundary characters in a row should not have spaces between them (not including parentheses)
679 | elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BOUNDARY) {
680 | if (isset($tokens[$i-1]) && $tokens[$i-1][self::TOKEN_TYPE] === self::TOKEN_TYPE_BOUNDARY) {
681 | if (isset($original_tokens[$token['i']-1]) && $original_tokens[$token['i']-1][self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
682 | $return = rtrim($return,' ');
683 | }
684 | }
685 | }
686 |
687 | // If the token shouldn't have a space before it
688 | if ($token[self::TOKEN_VALUE] === '.' || $token[self::TOKEN_VALUE] === ',' || $token[self::TOKEN_VALUE] === ';') {
689 | $return = rtrim($return, ' ');
690 | }
691 |
692 | $return .= $highlighted.' ';
693 |
694 | // If the token shouldn't have a space after it
695 | if ($token[self::TOKEN_VALUE] === '(' || $token[self::TOKEN_VALUE] === '.') {
696 | $return = rtrim($return,' ');
697 | }
698 |
699 | // If this is the "-" of a negative number, it shouldn't have a space after it
700 | if($token[self::TOKEN_VALUE] === '-' && isset($tokens[$i+1]) && $tokens[$i+1][self::TOKEN_TYPE] === self::TOKEN_TYPE_NUMBER && isset($tokens[$i-1])) {
701 | $prev = $tokens[$i-1][self::TOKEN_TYPE];
702 | if($prev !== self::TOKEN_TYPE_QUOTE && $prev !== self::TOKEN_TYPE_BACKTICK_QUOTE && $prev !== self::TOKEN_TYPE_WORD && $prev !== self::TOKEN_TYPE_NUMBER) {
703 | $return = rtrim($return,' ');
704 | }
705 | }
706 | }
707 |
708 | // If there are unmatched parentheses
709 | if ($highlight && array_search('block',$indent_types) !== false) {
710 | $return .= "\n".self::highlightError("WARNING: unclosed parentheses or section");
711 | }
712 |
713 | // Replace tab characters with the configuration tab character
714 | $return = trim(str_replace("\t",self::$tab,$return));
715 |
716 | if ($highlight) {
717 | $return = self::output($return);
718 | }
719 |
720 | return $return;
721 | }
722 |
723 | /**
724 | * Add syntax highlighting to a SQL string
725 | *
726 | * @param String $string The SQL string
727 | *
728 | * @return String The SQL string with HTML styles applied
729 | */
730 | public static function highlight($string)
731 | {
732 | $tokens = self::tokenize($string);
733 |
734 | $return = '';
735 |
736 | foreach ($tokens as $token) {
737 | $return .= self::highlightToken($token);
738 | }
739 |
740 | return self::output($return);
741 | }
742 |
743 | /**
744 | * Split a SQL string into multiple queries.
745 | * Uses ";" as a query delimiter.
746 | *
747 | * @param String $string The SQL string
748 | *
749 | * @return Array An array of individual query strings without trailing semicolons
750 | */
751 | public static function splitQuery($string)
752 | {
753 | $queries = array();
754 | $current_query = '';
755 | $empty = true;
756 |
757 | $tokens = self::tokenize($string);
758 |
759 | foreach ($tokens as $token) {
760 | // If this is a query separator
761 | if ($token[self::TOKEN_VALUE] === ';') {
762 | if (!$empty) {
763 | $queries[] = $current_query.';';
764 | }
765 | $current_query = '';
766 | $empty = true;
767 | continue;
768 | }
769 |
770 | // If this is a non-empty character
771 | if ($token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE && $token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_COMMENT && $token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_BLOCK_COMMENT) {
772 | $empty = false;
773 | }
774 |
775 | $current_query .= $token[self::TOKEN_VALUE];
776 | }
777 |
778 | if (!$empty) {
779 | $queries[] = trim($current_query);
780 | }
781 |
782 | return $queries;
783 | }
784 |
785 | /**
786 | * Remove all comments from a SQL string
787 | *
788 | * @param String $string The SQL string
789 | *
790 | * @return String The SQL string without comments
791 | */
792 | public static function removeComments($string)
793 | {
794 | $result = '';
795 |
796 | $tokens = self::tokenize($string);
797 |
798 | foreach ($tokens as $token) {
799 | // Skip comment tokens
800 | if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
801 | continue;
802 | }
803 |
804 | $result .= $token[self::TOKEN_VALUE];
805 | }
806 | $result = self::format( $result,false);
807 |
808 | return $result;
809 | }
810 |
811 | /**
812 | * Compress a query by collapsing white space and removing comments
813 | *
814 | * @param String $string The SQL string
815 | *
816 | * @return String The SQL string without comments
817 | */
818 | public static function compress($string)
819 | {
820 | $result = '';
821 |
822 | $tokens = self::tokenize($string);
823 |
824 | $whitespace = true;
825 | foreach ($tokens as $token) {
826 | // Skip comment tokens
827 | if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
828 | continue;
829 | }
830 | // Remove extra whitespace in reserved words (e.g "OUTER JOIN" becomes "OUTER JOIN")
831 | elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_NEWLINE || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_TOPLEVEL) {
832 | $token[self::TOKEN_VALUE] = preg_replace('/\s+/',' ',$token[self::TOKEN_VALUE]);
833 | }
834 |
835 | if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_WHITESPACE) {
836 | // If the last token was whitespace, don't add another one
837 | if ($whitespace) {
838 | continue;
839 | } else {
840 | $whitespace = true;
841 | // Convert all whitespace to a single space
842 | $token[self::TOKEN_VALUE] = ' ';
843 | }
844 | } else {
845 | $whitespace = false;
846 | }
847 |
848 | $result .= $token[self::TOKEN_VALUE];
849 | }
850 |
851 | return rtrim($result);
852 | }
853 |
854 | /**
855 | * Highlights a token depending on its type.
856 | *
857 | * @param Array $token An associative array containing type and value.
858 | *
859 | * @return String HTML code of the highlighted token.
860 | */
861 | protected static function highlightToken($token)
862 | {
863 | $type = $token[self::TOKEN_TYPE];
864 |
865 | if (self::is_cli()) {
866 | $token = $token[self::TOKEN_VALUE];
867 | } else {
868 | if (defined('ENT_IGNORE')) {
869 | $token = htmlentities($token[self::TOKEN_VALUE],ENT_COMPAT | ENT_IGNORE ,'UTF-8');
870 | } else {
871 | $token = htmlentities($token[self::TOKEN_VALUE],ENT_COMPAT,'UTF-8');
872 | }
873 | }
874 |
875 | if ($type===self::TOKEN_TYPE_BOUNDARY) {
876 | return self::highlightBoundary($token);
877 | } elseif ($type===self::TOKEN_TYPE_WORD) {
878 | return self::highlightWord($token);
879 | } elseif ($type===self::TOKEN_TYPE_BACKTICK_QUOTE) {
880 | return self::highlightBacktickQuote($token);
881 | } elseif ($type===self::TOKEN_TYPE_QUOTE) {
882 | return self::highlightQuote($token);
883 | } elseif ($type===self::TOKEN_TYPE_RESERVED) {
884 | return self::highlightReservedWord($token);
885 | } elseif ($type===self::TOKEN_TYPE_RESERVED_TOPLEVEL) {
886 | return self::highlightReservedWord($token);
887 | } elseif ($type===self::TOKEN_TYPE_RESERVED_NEWLINE) {
888 | return self::highlightReservedWord($token);
889 | } elseif ($type===self::TOKEN_TYPE_NUMBER) {
890 | return self::highlightNumber($token);
891 | } elseif ($type===self::TOKEN_TYPE_VARIABLE) {
892 | return self::highlightVariable($token);
893 | } elseif ($type===self::TOKEN_TYPE_COMMENT || $type===self::TOKEN_TYPE_BLOCK_COMMENT) {
894 | return self::highlightComment($token);
895 | }
896 |
897 | return $token;
898 | }
899 |
900 | /**
901 | * Highlights a quoted string
902 | *
903 | * @param String $value The token's value
904 | *
905 | * @return String HTML code of the highlighted token.
906 | */
907 | protected static function highlightQuote($value)
908 | {
909 | if (self::is_cli()) {
910 | return self::$cli_quote . $value . "\x1b[0m";
911 | } else {
912 | return '' . $value . '';
913 | }
914 | }
915 |
916 | /**
917 | * Highlights a backtick quoted string
918 | *
919 | * @param String $value The token's value
920 | *
921 | * @return String HTML code of the highlighted token.
922 | */
923 | protected static function highlightBacktickQuote($value)
924 | {
925 | if (self::is_cli()) {
926 | return self::$cli_backtick_quote . $value . "\x1b[0m";
927 | } else {
928 | return '' . $value . '';
929 | }
930 | }
931 |
932 | /**
933 | * Highlights a reserved word
934 | *
935 | * @param String $value The token's value
936 | *
937 | * @return String HTML code of the highlighted token.
938 | */
939 | protected static function highlightReservedWord($value)
940 | {
941 | if (self::is_cli()) {
942 | return self::$cli_reserved . $value . "\x1b[0m";
943 | } else {
944 | return '' . $value . '';
945 | }
946 | }
947 |
948 | /**
949 | * Highlights a boundary token
950 | *
951 | * @param String $value The token's value
952 | *
953 | * @return String HTML code of the highlighted token.
954 | */
955 | protected static function highlightBoundary($value)
956 | {
957 | if ($value==='(' || $value===')') return $value;
958 |
959 | if (self::is_cli()) {
960 | return self::$cli_boundary . $value . "\x1b[0m";
961 | } else {
962 | return '' . $value . '';
963 | }
964 | }
965 |
966 | /**
967 | * Highlights a number
968 | *
969 | * @param String $value The token's value
970 | *
971 | * @return String HTML code of the highlighted token.
972 | */
973 | protected static function highlightNumber($value)
974 | {
975 | if (self::is_cli()) {
976 | return self::$cli_number . $value . "\x1b[0m";
977 | } else {
978 | return '' . $value . '';
979 | }
980 | }
981 |
982 | /**
983 | * Highlights an error
984 | *
985 | * @param String $value The token's value
986 | *
987 | * @return String HTML code of the highlighted token.
988 | */
989 | protected static function highlightError($value)
990 | {
991 | if (self::is_cli()) {
992 | return self::$cli_error . $value . "\x1b[0m";
993 | } else {
994 | return '' . $value . '';
995 | }
996 | }
997 |
998 | /**
999 | * Highlights a comment
1000 | *
1001 | * @param String $value The token's value
1002 | *
1003 | * @return String HTML code of the highlighted token.
1004 | */
1005 | protected static function highlightComment($value)
1006 | {
1007 | if (self::is_cli()) {
1008 | return self::$cli_comment . $value . "\x1b[0m";
1009 | } else {
1010 | return '' . $value . '';
1011 | }
1012 | }
1013 |
1014 | /**
1015 | * Highlights a word token
1016 | *
1017 | * @param String $value The token's value
1018 | *
1019 | * @return String HTML code of the highlighted token.
1020 | */
1021 | protected static function highlightWord($value)
1022 | {
1023 | if (self::is_cli()) {
1024 | return self::$cli_word . $value . "\x1b[0m";
1025 | } else {
1026 | return '' . $value . '';
1027 | }
1028 | }
1029 |
1030 | /**
1031 | * Highlights a variable token
1032 | *
1033 | * @param String $value The token's value
1034 | *
1035 | * @return String HTML code of the highlighted token.
1036 | */
1037 | protected static function highlightVariable($value)
1038 | {
1039 | if (self::is_cli()) {
1040 | return self::$cli_variable . $value . "\x1b[0m";
1041 | } else {
1042 | return '' . $value . '';
1043 | }
1044 | }
1045 |
1046 | /**
1047 | * Helper function for building regular expressions for reserved words and boundary characters
1048 | *
1049 | * @param String $a The string to be quoted
1050 | *
1051 | * @return String The quoted string
1052 | */
1053 | private static function quote_regex($a)
1054 | {
1055 | return preg_quote($a,'/');
1056 | }
1057 |
1058 | /**
1059 | * Helper function for building string output
1060 | *
1061 | * @param String $string The string to be quoted
1062 | *
1063 | * @return String The quoted string
1064 | */
1065 | private static function output($string)
1066 | {
1067 | if (self::is_cli()) {
1068 | return $string."\n";
1069 | } else {
1070 | $string=trim($string);
1071 | if (!self::$use_pre) {
1072 | return $string;
1073 | }
1074 |
1075 | return '' . $string . '
';
1076 | }
1077 | }
1078 |
1079 | private static function is_cli()
1080 | {
1081 | if (isset(self::$cli)) return self::$cli;
1082 | else return php_sapi_name() === 'cli';
1083 | }
1084 |
1085 | }
1086 |
--------------------------------------------------------------------------------
/tests/highlight.html:
--------------------------------------------------------------------------------
1 | SELECT customer_id, customer_name, COUNT(order_id) as total
2 | FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id
3 | GROUP BY customer_id, customer_name
4 | HAVING COUNT(order_id) > 5
5 | ORDER BY COUNT(order_id) DESC;
6 |
7 | UPDATE customers
8 | SET totalorders = ordersummary.total
9 | FROM (SELECT customer_id, count(order_id) As total
10 | FROM orders GROUP BY customer_id) As ordersummary
11 | WHERE customers.customer_id = ordersummary.customer_id
12 |
13 | SELECT * FROM sometable
14 | UNION ALL
15 | SELECT * FROM someothertable;
16 |
17 | SET NAMES 'utf8';
18 |
19 | CREATE TABLE `PREFIX_address` (
20 | `id_address` int(10) unsigned NOT NULL auto_increment,
21 | `id_country` int(10) unsigned NOT NULL,
22 | `id_state` int(10) unsigned default NULL,
23 | `id_customer` int(10) unsigned NOT NULL default '0',
24 | `id_manufacturer` int(10) unsigned NOT NULL default '0',
25 | `id_supplier` int(10) unsigned NOT NULL default '0',
26 | `id_warehouse` int(10) unsigned NOT NULL default '0',
27 | `alias` varchar(32) NOT NULL,
28 | `company` varchar(64) default NULL,
29 | `lastname` varchar(32) NOT NULL,
30 | `firstname` varchar(32) NOT NULL,
31 | `address1` varchar(128) NOT NULL,
32 | `address2` varchar(128) default NULL,
33 | `postcode` varchar(12) default NULL,
34 | `city` varchar(64) NOT NULL,
35 | `other` text,
36 | `phone` varchar(16) default NULL,
37 | `phone_mobile` varchar(16) default NULL,
38 | `vat_number` varchar(32) default NULL,
39 | `dni` varchar(16) DEFAULT NULL,
40 | `date_add` datetime NOT NULL,
41 | `date_upd` datetime NOT NULL,
42 | `active` tinyint(1) unsigned NOT NULL default '1',
43 | `deleted` tinyint(1) unsigned NOT NULL default '0',
44 | PRIMARY KEY (`id_address`),
45 | KEY `address_customer` (`id_customer`),
46 | KEY `id_country` (`id_country`),
47 | KEY `id_state` (`id_state`),
48 | KEY `id_manufacturer` (`id_manufacturer`),
49 | KEY `id_supplier` (`id_supplier`),
50 | KEY `id_warehouse` (`id_warehouse`)
51 | ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8
52 |
53 | CREATE TABLE `PREFIX_alias` (
54 | `id_alias` int(10) unsigned NOT NULL auto_increment,
55 | `alias` varchar(255) NOT NULL,
56 | `search` varchar(255) NOT NULL,
57 | `active` tinyint(1) NOT NULL default '1',
58 | PRIMARY KEY (`id_alias`),
59 | UNIQUE KEY `alias` (`alias`)
60 | ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8
61 |
62 | CREATE TABLE `PREFIX_carrier` (
63 | `id_carrier` int(10) unsigned NOT NULL AUTO_INCREMENT,
64 | `id_reference` int(10) unsigned NOT NULL,
65 | `id_tax_rules_group` int(10) unsigned DEFAULT '0',
66 | `name` varchar(64) NOT NULL,
67 | `url` varchar(255) DEFAULT NULL,
68 | `active` tinyint(1) unsigned NOT NULL DEFAULT '0',
69 | `deleted` tinyint(1) unsigned NOT NULL DEFAULT '0',
70 | `shipping_handling` tinyint(1) unsigned NOT NULL DEFAULT '1',
71 | `range_behavior` tinyint(1) unsigned NOT NULL DEFAULT '0',
72 | `is_module` tinyint(1) unsigned NOT NULL DEFAULT '0',
73 | `is_free` tinyint(1) unsigned NOT NULL DEFAULT '0',
74 | `shipping_external` tinyint(1) unsigned NOT NULL DEFAULT '0',
75 | `need_range` tinyint(1) unsigned NOT NULL DEFAULT '0',
76 | `external_module_name` varchar(64) DEFAULT NULL,
77 | `shipping_method` int(2) NOT NULL DEFAULT '0',
78 | `position` int(10) unsigned NOT NULL default '0',
79 | `max_width` int(10) DEFAULT 0,
80 | `max_height` int(10) DEFAULT 0,
81 | `max_depth` int(10) DEFAULT 0,
82 | `max_weight` int(10) DEFAULT 0,
83 | `grade` int(10) DEFAULT 0,
84 | PRIMARY KEY (`id_carrier`),
85 | KEY `deleted` (`deleted`,`active`),
86 | KEY `id_tax_rules_group` (`id_tax_rules_group`)
87 | ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8
88 |
89 | CREATE TABLE IF NOT EXISTS `PREFIX_specific_price_rule` (
90 | `id_specific_price_rule` int(10) unsigned NOT NULL AUTO_INCREMENT,
91 | `name` VARCHAR(255) NOT NULL,
92 | `id_shop` int(11) unsigned NOT NULL DEFAULT '1',
93 | `id_currency` int(10) unsigned NOT NULL,
94 | `id_country` int(10) unsigned NOT NULL,
95 | `id_group` int(10) unsigned NOT NULL,
96 | `from_quantity` mediumint(8) unsigned NOT NULL,
97 | `price` DECIMAL(20,6),
98 | `reduction` decimal(20,6) NOT NULL,
99 | `reduction_type` enum('amount','percentage') NOT NULL,
100 | `from` datetime NOT NULL,
101 | `to` datetime NOT NULL,
102 | PRIMARY KEY (`id_specific_price_rule`),
103 | KEY `id_product` (`id_shop`,`id_currency`,`id_country`,`id_group`,`from_quantity`,`from`,`to`)
104 | ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8
105 |
106 | UPDATE `PREFIX_configuration` SET value = '6' WHERE name = 'PS_SEARCH_WEIGHT_PNAME'
107 |
108 | UPDATE `PREFIX_hook_module` SET position = 1
109 | WHERE
110 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayPayment') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'cheque')
111 | OR
112 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayPaymentReturn') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'cheque')
113 | OR
114 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayHome') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'homeslider')
115 | OR
116 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionAuthentication') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'statsdata')
117 | OR
118 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionShopDataDuplication') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'homeslider')
119 | OR
120 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayTop') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'blocklanguages')
121 | OR
122 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionCustomerAccountAdd') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'statsdata')
123 | OR
124 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayCustomerAccount') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'favoriteproducts')
125 | OR
126 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayAdminStatsModules') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'statsvisits')
127 | OR
128 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayAdminStatsGraphEngine') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'graphvisifire')
129 | OR
130 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayAdminStatsGridEngine') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'gridhtml')
131 | OR
132 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayLeftColumnProduct') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'blocksharefb')
133 | OR
134 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionSearch') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'statssearch')
135 | OR
136 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionCategoryAdd') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'blockcategories')
137 | OR
138 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionCategoryUpdate') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'blockcategories')
139 | OR
140 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionCategoryDelete') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'blockcategories')
141 | OR
142 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'actionAdminMetaSave') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'blockcategories')
143 | OR
144 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayMyAccountBlock') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'favoriteproducts')
145 | OR
146 | id_hook = (SELECT id_hook FROM `PREFIX_hook` WHERE name = 'displayFooter') AND id_module = (SELECT id_module FROM `PREFIX_module` WHERE name = 'blockreinsurance')
147 |
148 | ALTER TABLE `PREFIX_employee` ADD `bo_color` varchar(32) default NULL AFTER `stats_date_to`
149 |
150 | INSERT INTO `PREFIX_cms_category_lang` VALUES(1, 3, 'Inicio', '', 'home', NULL, NULL, NULL)
151 |
152 | INSERT INTO `PREFIX_cms_category` VALUES(1, 0, 0, 1, NOW(), NOW(),0)
153 |
154 | UPDATE `PREFIX_cms_category` SET `position` = 0
155 |
156 | ALTER TABLE `PREFIX_customer` ADD `note` text AFTER `secure_key`
157 |
158 | ALTER TABLE `PREFIX_contact` ADD `customer_service` tinyint(1) NOT NULL DEFAULT 0 AFTER `email`
159 |
160 | INSERT INTO `PREFIX_specific_price` (`id_product`, `id_shop`, `id_currency`, `id_country`, `id_group`, `priority`, `price`, `from_quantity`, `reduction`, `reduction_type`, `from`, `to`)
161 | ( SELECT dq.`id_product`, 1, 1, 0, 1, 0, 0.00, dq.`quantity`, IF(dq.`id_discount_type` = 2, dq.`value`, dq.`value` / 100), IF (dq.`id_discount_type` = 2, 'amount', 'percentage'), '0000-00-00 00:00:00', '0000-00-00 00:00:00'
162 | FROM `PREFIX_discount_quantity` dq
163 | INNER JOIN `PREFIX_product` p ON (p.`id_product` = dq.`id_product`)
164 | )
165 |
166 | DROP TABLE `PREFIX_discount_quantity`
167 |
168 | INSERT INTO `PREFIX_specific_price` (`id_product`, `id_shop`, `id_currency`, `id_country`, `id_group`, `priority`, `price`, `from_quantity`, `reduction`, `reduction_type`, `from`, `to`) (
169 | SELECT
170 | p.`id_product`,
171 | 1,
172 | 0,
173 | 0,
174 | 0,
175 | 0,
176 | 0.00,
177 | 1,
178 | IF(p.`reduction_price` > 0, p.`reduction_price`, p.`reduction_percent` / 100),
179 | IF(p.`reduction_price` > 0, 'amount', 'percentage'),
180 | IF (p.`reduction_from` = p.`reduction_to`, '0000-00-00 00:00:00', p.`reduction_from`),
181 | IF (p.`reduction_from` = p.`reduction_to`, '0000-00-00 00:00:00', p.`reduction_to`)
182 | FROM `PREFIX_product` p
183 | WHERE p.`reduction_price` OR p.`reduction_percent`
184 | )
185 |
186 | ALTER TABLE `PREFIX_product`
187 | DROP `reduction_price`,
188 | DROP `reduction_percent`,
189 | DROP `reduction_from`,
190 | DROP `reduction_to`
191 |
192 | INSERT INTO `PREFIX_configuration` (`name`, `value`, `date_add`, `date_upd`) VALUES
193 | ('PS_SPECIFIC_PRICE_PRIORITIES', 'id_shop;id_currency;id_country;id_group', NOW(), NOW()),
194 | ('PS_TAX_DISPLAY', 0, NOW(), NOW()),
195 | ('PS_SMARTY_FORCE_COMPILE', 1, NOW(), NOW()),
196 | ('PS_DISTANCE_UNIT', 'km', NOW(), NOW()),
197 | ('PS_STORES_DISPLAY_CMS', 0, NOW(), NOW()),
198 | ('PS_STORES_DISPLAY_FOOTER', 0, NOW(), NOW()),
199 | ('PS_STORES_SIMPLIFIED', 0, NOW(), NOW()),
200 | ('PS_STATSDATA_CUSTOMER_PAGESVIEWS', 1, NOW(), NOW()),
201 | ('PS_STATSDATA_PAGESVIEWS', 1, NOW(), NOW()),
202 | ('PS_STATSDATA_PLUGINS', 1, NOW(), NOW())
203 |
204 | INSERT INTO `PREFIX_configuration` (`name`, `value`, `date_add`, `date_upd`) VALUES ('PS_CONDITIONS_CMS_ID', IFNULL((SELECT `id_cms` FROM `PREFIX_cms` WHERE `id_cms` = 3), 0), NOW(), NOW())
205 |
206 | CREATE TEMPORARY TABLE `PREFIX_configuration_tmp` (
207 | `value` text
208 | )
209 |
210 | SET @defaultOOS = (SELECT value FROM `PREFIX_configuration` WHERE name = 'PS_ORDER_OUT_OF_STOCK')
211 |
212 | UPDATE `PREFIX_product` p SET `cache_default_attribute` = 0 WHERE `id_product` NOT IN (SELECT `id_product` FROM `PREFIX_product_attribute`)
213 |
214 | INSERT INTO `PREFIX_hook` (`name`, `title`, `description`, `position`) VALUES ('processCarrier', 'Carrier Process', NULL, 0)
215 |
216 | INSERT INTO `PREFIX_stock_mvt_reason_lang` (`id_stock_mvt_reason`, `id_lang`, `name`) VALUES
217 | (1, 1, 'Order'),
218 | (1, 2, 'Commande'),
219 | (2, 1, 'Missing Stock Movement'),
220 | (2, 2, 'Mouvement de stock manquant'),
221 | (3, 1, 'Restocking'),
222 | (3, 2, 'Réassort')
223 |
224 | INSERT INTO `PREFIX_meta_lang` (`id_lang`, `id_meta`, `title`, `url_rewrite`) VALUES
225 | (1, (SELECT `id_meta` FROM `PREFIX_meta` WHERE `page` = 'authentication'), 'Authentication', 'authentication'),
226 | (2, (SELECT `id_meta` FROM `PREFIX_meta` WHERE `page` = 'authentication'), 'Authentification', 'authentification'),
227 | (3, (SELECT `id_meta` FROM `PREFIX_meta` WHERE `page` = 'authentication'), 'Autenticación', 'autenticacion')
228 |
229 | LOCK TABLES `admin_assert` WRITE
230 |
231 | UNLOCK TABLES
232 |
233 | DROP TABLE IF EXISTS `admin_role`
234 |
235 | SELECT * FROM
236 | -- This is another comment
237 | MyTable # One final comment
238 | /* This is a block comment
239 | */ WHERE 1 = 2;
240 |
241 | SELECT -- This is a test
242 |
243 | SELECT Test FROM Test WHERE
244 | (
245 | MyColumn = 1 )) AND ((( SomeOtherColumn = 2);
246 |
247 | SELECT * LIMIT 1; SELECT a,b,c,d FROM e LIMIT 1, 2; SELECT 1,2,3 WHERE a in (1,2,3,4,5) and b=5;
248 |
249 | SELECT count - 50
250 | WHERE a-50 = b
251 | WHERE 1 and - 50
252 | WHERE -50 = a
253 | WHERE a = -50
254 | WHERE 1 /*test*/ - 50
255 | WHERE 1 and -50;
256 |
257 | SELECT @ and b;
258 |
259 | SELECT @"weird variable name";
260 |
261 | SELECT "no closing quote
262 |
263 | SELECT [sqlserver] FROM [escap[e]]d style];
--------------------------------------------------------------------------------