diff --git a/db_sql_format.php b/db_sql_format.php index a83e5273ee..610b3e22aa 100644 --- a/db_sql_format.php +++ b/db_sql_format.php @@ -6,13 +6,15 @@ * @package PhpMyAdmin */ +/** + * Loading common files. Used to check for authorization, localization and to + * load the parsing library. + */ require_once 'libraries/common.inc.php'; -require_once 'libraries/sql-formatter/lib/SqlFormatter.php'; -$query = isset($_POST['sql']) ? $_POST['sql'] : ''; +$query = !empty($_POST['sql']) ? $_POST['sql'] : ''; -SqlFormatter::$tab = "\t"; -$query = SqlFormatter::format($query, false); +$query = SqlParser\Utils\Formatter::format($query); $response = PMA_Response::getInstance(); $response->addJSON("sql", $query); diff --git a/libraries/sql-formatter/LICENSE.txt b/libraries/sql-formatter/LICENSE.txt deleted file mode 100644 index e822279b46..0000000000 --- a/libraries/sql-formatter/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2013 Jeremy Dorn - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/libraries/sql-formatter/README.md b/libraries/sql-formatter/README.md deleted file mode 100644 index a144323cf8..0000000000 --- a/libraries/sql-formatter/README.md +++ /dev/null @@ -1,185 +0,0 @@ -SqlFormatter -============= - -A lightweight php class for formatting sql statements. - -It can automatically indent and add line breaks in addition to syntax highlighting. - -History -============ - -I found myself having to debug auto-generated SQL statements all the time and -wanted some way to easily output formatted HTML without having to include a -huge library or copy and paste into online formatters. - -I was originally planning to extract the formatting code from PhpMyAdmin, -but that was 10,000+ lines of code and used global variables. - -I saw that other people had the same problem and used Stack Overflow user -losif's answer as a starting point. http://stackoverflow.com/a/3924147 - -Usage -============ - -The SqlFormatter class has a static method 'format' which takes a SQL string -as input and returns a formatted HTML block inside a pre tag. - -Sample usage: - -```php -= NOW()) ) - GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10"; - -echo SqlFormatter::format($query); -``` - -Output: - -![](http://jdorn.github.com/sql-formatter/format-highlight.png) - -Formatting Only -------------------------- -If you don't want syntax highlighting and only want the indentations and -line breaks, pass in false as the second parameter. - -This is useful for outputting to error logs or other non-html formats. - -```php - - * @author Florin Patan - * @copyright 2013 Jeremy Dorn - * @license http://opensource.org/licenses/MIT - * @link http://github.com/jdorn/sql-formatter - * @version 1.2.18 - */ -class SqlFormatter -{ - // Constants for token types - const TOKEN_TYPE_WHITESPACE = 0; - const TOKEN_TYPE_WORD = 1; - const TOKEN_TYPE_QUOTE = 2; - const TOKEN_TYPE_BACKTICK_QUOTE = 3; - const TOKEN_TYPE_RESERVED = 4; - const TOKEN_TYPE_RESERVED_TOPLEVEL = 5; - const TOKEN_TYPE_RESERVED_NEWLINE = 6; - const TOKEN_TYPE_BOUNDARY = 7; - const TOKEN_TYPE_COMMENT = 8; - const TOKEN_TYPE_BLOCK_COMMENT = 9; - const TOKEN_TYPE_NUMBER = 10; - const TOKEN_TYPE_ERROR = 11; - const TOKEN_TYPE_VARIABLE = 12; - - // Constants for different components of a token - const TOKEN_TYPE = 0; - const TOKEN_VALUE = 1; - - // Reserved words (for syntax highlighting) - protected static $reserved = array( - 'ACCESSIBLE', 'ACTION', 'AGAINST', 'AGGREGATE', 'ALGORITHM', 'ALL', 'ALTER', 'ANALYSE', 'ANALYZE', 'AS', 'ASC', - 'AUTOCOMMIT', 'AUTO_INCREMENT', 'BACKUP', 'BEGIN', 'BETWEEN', 'BINLOG', 'BOTH', 'CASCADE', 'CASE', 'CHANGE', 'CHANGED', 'CHARACTER SET', - 'CHARSET', 'CHECK', 'CHECKSUM', 'COLLATE', 'COLLATION', 'COLUMN', 'COLUMNS', 'COMMENT', 'COMMIT', 'COMMITTED', 'COMPRESSED', 'CONCURRENT', - 'CONSTRAINT', 'CONTAINS', 'CONVERT', 'CREATE', 'CROSS', 'CURRENT_TIMESTAMP', 'DATABASE', 'DATABASES', 'DAY', 'DAY_HOUR', 'DAY_MINUTE', - 'DAY_SECOND', 'DEFAULT', 'DEFINER', 'DELAYED', 'DELETE', 'DESC', 'DESCRIBE', 'DETERMINISTIC', 'DISTINCT', 'DISTINCTROW', 'DIV', - 'DO', 'DUMPFILE', 'DUPLICATE', 'DYNAMIC', 'ELSE', 'ENCLOSED', 'END', 'ENGINE', 'ENGINE_TYPE', 'ENGINES', 'ESCAPE', 'ESCAPED', 'EVENTS', 'EXEC', - 'EXECUTE', 'EXISTS', 'EXPLAIN', 'EXTENDED', 'FAST', 'FIELDS', 'FILE', 'FIRST', 'FIXED', 'FLUSH', 'FOR', 'FORCE', 'FOREIGN', 'FULL', 'FULLTEXT', - 'FUNCTION', 'GLOBAL', 'GRANT', 'GRANTS', 'GROUP_CONCAT', 'HEAP', 'HIGH_PRIORITY', 'HOSTS', 'HOUR', 'HOUR_MINUTE', - 'HOUR_SECOND', 'IDENTIFIED', 'IF', 'IFNULL', 'IGNORE', 'IN', 'INDEX', 'INDEXES', 'INFILE', 'INSERT', 'INSERT_ID', 'INSERT_METHOD', 'INTERVAL', - 'INTO', 'INVOKER', 'IS', 'ISOLATION', 'KEY', 'KEYS', 'KILL', 'LAST_INSERT_ID', 'LEADING', 'LEVEL', 'LIKE', 'LINEAR', - 'LINES', 'LOAD', 'LOCAL', 'LOCK', 'LOCKS', 'LOGS', 'LOW_PRIORITY', 'MARIA', 'MASTER', 'MASTER_CONNECT_RETRY', 'MASTER_HOST', 'MASTER_LOG_FILE', - 'MATCH','MAX_CONNECTIONS_PER_HOUR', 'MAX_QUERIES_PER_HOUR', 'MAX_ROWS', 'MAX_UPDATES_PER_HOUR', 'MAX_USER_CONNECTIONS', - 'MEDIUM', 'MERGE', 'MINUTE', 'MINUTE_SECOND', 'MIN_ROWS', 'MODE', 'MODIFY', - 'MONTH', 'MRG_MYISAM', 'MYISAM', 'NAMES', 'NATURAL', 'NOT', 'NOW()','NULL', 'OFFSET', 'ON', 'OPEN', 'OPTIMIZE', 'OPTION', 'OPTIONALLY', - 'ON UPDATE', 'ON DELETE', 'OUTFILE', 'PACK_KEYS', 'PAGE', 'PARTIAL', 'PARTITION', 'PARTITIONS', 'PASSWORD', 'PRIMARY', 'PRIVILEGES', 'PROCEDURE', - 'PROCESS', 'PROCESSLIST', 'PURGE', 'QUICK', 'RANGE', 'RAID0', 'RAID_CHUNKS', 'RAID_CHUNKSIZE','RAID_TYPE', 'READ', 'READ_ONLY', - 'READ_WRITE', 'REFERENCES', 'REGEXP', 'RELOAD', 'RENAME', 'REPAIR', 'REPEATABLE', 'REPLACE', 'REPLICATION', 'RESET', 'RESTORE', 'RESTRICT', - 'RETURN', 'RETURNS', 'REVOKE', 'RLIKE', 'ROLLBACK', 'ROW', 'ROWS', 'ROW_FORMAT', 'SECOND', 'SECURITY', 'SEPARATOR', - 'SERIALIZABLE', 'SESSION', 'SHARE', 'SHOW', 'SHUTDOWN', 'SLAVE', 'SONAME', 'SOUNDS', 'SQL', 'SQL_AUTO_IS_NULL', 'SQL_BIG_RESULT', - 'SQL_BIG_SELECTS', 'SQL_BIG_TABLES', 'SQL_BUFFER_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQL_LOG_BIN', 'SQL_LOG_OFF', 'SQL_LOG_UPDATE', - 'SQL_LOW_PRIORITY_UPDATES', 'SQL_MAX_JOIN_SIZE', 'SQL_QUOTE_SHOW_CREATE', 'SQL_SAFE_UPDATES', 'SQL_SELECT_LIMIT', 'SQL_SLAVE_SKIP_COUNTER', - 'SQL_SMALL_RESULT', 'SQL_WARNINGS', 'SQL_CACHE', 'SQL_NO_CACHE', 'START', 'STARTING', 'STATUS', 'STOP', 'STORAGE', - 'STRAIGHT_JOIN', 'STRING', 'STRIPED', 'SUPER', 'TABLE', 'TABLES', 'TEMPORARY', 'TERMINATED', 'THEN', 'TO', 'TRAILING', 'TRANSACTIONAL', 'TRUE', - 'TRUNCATE', 'TYPE', 'TYPES', 'UNCOMMITTED', 'UNIQUE', 'UNLOCK', 'UNSIGNED', 'USAGE', 'USE', 'USING', 'VARIABLES', - 'VIEW', 'WHEN', 'WITH', 'WORK', 'WRITE', 'YEAR_MONTH' - ); - - // For SQL formatting - // These keywords will all be on their own line - protected static $reserved_toplevel = array( - 'SELECT', 'FROM', 'WHERE', 'SET', 'ORDER BY', 'GROUP BY', 'LIMIT', 'DROP', - 'VALUES', 'UPDATE', 'HAVING', 'ADD', 'AFTER', 'ALTER TABLE', 'DELETE FROM', 'UNION ALL', 'UNION', 'EXCEPT', 'INTERSECT' - ); - - protected static $reserved_newline = array( - 'LEFT OUTER JOIN', 'RIGHT OUTER JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'OUTER JOIN', 'INNER JOIN', 'JOIN', 'XOR', 'OR', 'AND' - ); - - protected static $functions = array ( - 'ABS', 'ACOS', 'ADDDATE', 'ADDTIME', 'AES_DECRYPT', 'AES_ENCRYPT', 'AREA', 'ASBINARY', 'ASCII', 'ASIN', 'ASTEXT', 'ATAN', 'ATAN2', - 'AVG', 'BDMPOLYFROMTEXT', 'BDMPOLYFROMWKB', 'BDPOLYFROMTEXT', 'BDPOLYFROMWKB', 'BENCHMARK', 'BIN', 'BIT_AND', 'BIT_COUNT', 'BIT_LENGTH', - 'BIT_OR', 'BIT_XOR', 'BOUNDARY', 'BUFFER', 'CAST', 'CEIL', 'CEILING', 'CENTROID', 'CHAR', 'CHARACTER_LENGTH', 'CHARSET', 'CHAR_LENGTH', - 'COALESCE', 'COERCIBILITY', 'COLLATION', 'COMPRESS', 'CONCAT', 'CONCAT_WS', 'CONNECTION_ID', 'CONTAINS', 'CONV', 'CONVERT', 'CONVERT_TZ', - 'CONVEXHULL', 'COS', 'COT', 'COUNT', 'CRC32', 'CROSSES', 'CURDATE', 'CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP', 'CURRENT_USER', - 'CURTIME', 'DATABASE', 'DATE', 'DATEDIFF', 'DATE_ADD', 'DATE_DIFF', 'DATE_FORMAT', 'DATE_SUB', 'DAY', 'DAYNAME', 'DAYOFMONTH', 'DAYOFWEEK', - 'DAYOFYEAR', 'DECODE', 'DEFAULT', 'DEGREES', 'DES_DECRYPT', 'DES_ENCRYPT', 'DIFFERENCE', 'DIMENSION', 'DISJOINT', 'DISTANCE', 'ELT', 'ENCODE', - 'ENCRYPT', 'ENDPOINT', 'ENVELOPE', 'EQUALS', 'EXP', 'EXPORT_SET', 'EXTERIORRING', 'EXTRACT', 'EXTRACTVALUE', 'FIELD', 'FIND_IN_SET', 'FLOOR', - 'FORMAT', 'FOUND_ROWS', 'FROM_DAYS', 'FROM_UNIXTIME', 'GEOMCOLLFROMTEXT', 'GEOMCOLLFROMWKB', 'GEOMETRYCOLLECTION', 'GEOMETRYCOLLECTIONFROMTEXT', - 'GEOMETRYCOLLECTIONFROMWKB', 'GEOMETRYFROMTEXT', 'GEOMETRYFROMWKB', 'GEOMETRYN', 'GEOMETRYTYPE', 'GEOMFROMTEXT', 'GEOMFROMWKB', 'GET_FORMAT', - 'GET_LOCK', 'GLENGTH', 'GREATEST', 'GROUP_CONCAT', 'GROUP_UNIQUE_USERS', 'HEX', 'HOUR', 'IF', 'IFNULL', 'INET_ATON', 'INET_NTOA', 'INSERT', 'INSTR', - 'INTERIORRINGN', 'INTERSECTION', 'INTERSECTS', 'INTERVAL', 'ISCLOSED', 'ISEMPTY', 'ISNULL', 'ISRING', 'ISSIMPLE', 'IS_FREE_LOCK', 'IS_USED_LOCK', - 'LAST_DAY', 'LAST_INSERT_ID', 'LCASE', 'LEAST', 'LEFT', 'LENGTH', 'LINEFROMTEXT', 'LINEFROMWKB', 'LINESTRING', 'LINESTRINGFROMTEXT', 'LINESTRINGFROMWKB', - 'LN', 'LOAD_FILE', 'LOCALTIME', 'LOCALTIMESTAMP', 'LOCATE', 'LOG', 'LOG10', 'LOG2', 'LOWER', 'LPAD', 'LTRIM', 'MAKEDATE', 'MAKETIME', 'MAKE_SET', - 'MASTER_POS_WAIT', 'MAX', 'MBRCONTAINS', 'MBRDISJOINT', 'MBREQUAL', 'MBRINTERSECTS', 'MBROVERLAPS', 'MBRTOUCHES', 'MBRWITHIN', 'MD5', 'MICROSECOND', - 'MID', 'MIN', 'MINUTE', 'MLINEFROMTEXT', 'MLINEFROMWKB', 'MOD', 'MONTH', 'MONTHNAME', 'MPOINTFROMTEXT', 'MPOINTFROMWKB', 'MPOLYFROMTEXT', 'MPOLYFROMWKB', - 'MULTILINESTRING', 'MULTILINESTRINGFROMTEXT', 'MULTILINESTRINGFROMWKB', 'MULTIPOINT', 'MULTIPOINTFROMTEXT', 'MULTIPOINTFROMWKB', 'MULTIPOLYGON', - 'MULTIPOLYGONFROMTEXT', 'MULTIPOLYGONFROMWKB', 'NAME_CONST', 'NULLIF', 'NUMGEOMETRIES', 'NUMINTERIORRINGS', 'NUMPOINTS', 'OCT', 'OCTET_LENGTH', - 'OLD_PASSWORD', 'ORD', 'OVERLAPS', 'PASSWORD', 'PERIOD_ADD', 'PERIOD_DIFF', 'PI', 'POINT', 'POINTFROMTEXT', 'POINTFROMWKB', 'POINTN', 'POINTONSURFACE', - 'POLYFROMTEXT', 'POLYFROMWKB', 'POLYGON', 'POLYGONFROMTEXT', 'POLYGONFROMWKB', 'POSITION', 'POW', 'POWER', 'QUARTER', 'QUOTE', 'RADIANS', 'RAND', - 'RELATED', 'RELEASE_LOCK', 'REPEAT', 'REPLACE', 'REVERSE', 'RIGHT', 'ROUND', 'ROW_COUNT', 'RPAD', 'RTRIM', 'SCHEMA', 'SECOND', 'SEC_TO_TIME', - 'SESSION_USER', 'SHA', 'SHA1', 'SIGN', 'SIN', 'SLEEP', 'SOUNDEX', 'SPACE', 'SQRT', 'SRID', 'STARTPOINT', 'STD', 'STDDEV', 'STDDEV_POP', 'STDDEV_SAMP', - 'STRCMP', 'STR_TO_DATE', 'SUBDATE', 'SUBSTR', 'SUBSTRING', 'SUBSTRING_INDEX', 'SUBTIME', 'SUM', 'SYMDIFFERENCE', 'SYSDATE', 'SYSTEM_USER', 'TAN', - 'TIME', 'TIMEDIFF', 'TIMESTAMP', 'TIMESTAMPADD', 'TIMESTAMPDIFF', 'TIME_FORMAT', 'TIME_TO_SEC', 'TOUCHES', 'TO_DAYS', 'TRIM', 'TRUNCATE', 'UCASE', - 'UNCOMPRESS', 'UNCOMPRESSED_LENGTH', 'UNHEX', 'UNIQUE_USERS', 'UNIX_TIMESTAMP', 'UPDATEXML', 'UPPER', 'USER', 'UTC_DATE', 'UTC_TIME', 'UTC_TIMESTAMP', - 'UUID', 'VARIANCE', 'VAR_POP', 'VAR_SAMP', 'VERSION', 'WEEK', 'WEEKDAY', 'WEEKOFYEAR', 'WITHIN', 'X', 'Y', 'YEAR', 'YEARWEEK' - ); - - // Punctuation that can be used as a boundary between other tokens - protected static $boundaries = array(',', ';',':', ')', '(', '.', '=', '<', '>', '+', '-', '*', '/', '!', '^', '%', '|', '&', '#'); - - // For HTML syntax highlighting - // Styles applied to different token types - public static $quote_attributes = 'style="color: blue;"'; - public static $backtick_quote_attributes = 'style="color: purple;"'; - public static $reserved_attributes = 'style="font-weight:bold;"'; - public static $boundary_attributes = ''; - public static $number_attributes = 'style="color: green;"'; - public static $word_attributes = 'style="color: #333;"'; - public static $error_attributes = 'style="background-color: red;"'; - public static $comment_attributes = 'style="color: #aaa;"'; - public static $variable_attributes = 'style="color: orange;"'; - public static $pre_attributes = 'style="color: black; background-color: white;"'; - - // Boolean - whether or not the current environment is the CLI - // This affects the type of syntax highlighting - // If not defined, it will be determined automatically - public static $cli; - - // For CLI syntax highlighting - public static $cli_quote = "\x1b[34;1m"; - public static $cli_backtick_quote = "\x1b[35;1m"; - public static $cli_reserved = "\x1b[37m"; - public static $cli_boundary = ""; - public static $cli_number = "\x1b[32;1m"; - public static $cli_word = ""; - public static $cli_error = "\x1b[31;1;7m"; - public static $cli_comment = "\x1b[30;1m"; - public static $cli_functions = "\x1b[37m"; - public static $cli_variable = "\x1b[36;1m"; - - // The tab character to use when formatting SQL - public static $tab = ' '; - - // This flag tells us if queries need to be enclosed in
 tags
-    public static $use_pre = true;
-
-    // This flag tells us if SqlFormatted has been initialized
-    protected static $init;
-
-    // Regular expressions for tokenizing
-    protected static $regex_boundaries;
-    protected static $regex_reserved;
-    protected static $regex_reserved_newline;
-    protected static $regex_reserved_toplevel;
-    protected static $regex_function;
-
-    // Cache variables
-    // Only tokens shorter than this size will be cached.  Somewhere between 10 and 20 seems to work well for most cases.
-    public static $max_cachekey_size = 15;
-    protected static $token_cache = array();
-    protected static $cache_hits = 0;
-    protected static $cache_misses = 0;
-
-    /**
-     * Get stats about the token cache
-     * @return Array An array containing the keys 'hits', 'misses', 'entries', and 'size' in bytes
-     */
-    public static function getCacheStats()
-    {
-        return array(
-            'hits'=>self::$cache_hits,
-            'misses'=>self::$cache_misses,
-            'entries'=>count(self::$token_cache),
-            'size'=>strlen(serialize(self::$token_cache))
-        );
-    }
-
-    /**
-     * Stuff that only needs to be done once.  Builds regular expressions and sorts the reserved words.
-     */
-    protected static function init()
-    {
-        if (self::$init) return;
-
-        // Sort reserved word list from longest word to shortest, 3x faster than usort
-        $reservedMap = array_combine(self::$reserved, array_map('strlen', self::$reserved));
-        arsort($reservedMap);
-        self::$reserved = array_keys($reservedMap);
-
-        // Set up regular expressions
-        self::$regex_boundaries = '('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$boundaries)).')';
-        self::$regex_reserved = '('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$reserved)).')';
-        self::$regex_reserved_toplevel = str_replace(' ','\\s+','('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$reserved_toplevel)).')');
-        self::$regex_reserved_newline = str_replace(' ','\\s+','('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$reserved_newline)).')');
-
-        self::$regex_function = '('.implode('|',array_map(array(__CLASS__, 'quote_regex'),self::$functions)).')';
-
-        self::$init = true;
-    }
-
-    /**
-     * Return the next token and token type in a SQL string.
-     * Quoted strings, comments, reserved words, whitespace, and punctuation are all their own tokens.
-     *
-     * @param String $string   The SQL string
-     * @param array  $previous The result of the previous getNextToken() call
-     *
-     * @return Array An associative array containing the type and value of the token.
-     */
-    protected static function getNextToken($string, $previous = null)
-    {
-        // Whitespace
-        if (preg_match('/^\s+/',$string,$matches)) {
-            return array(
-                self::TOKEN_VALUE => $matches[0],
-                self::TOKEN_TYPE=>self::TOKEN_TYPE_WHITESPACE
-            );
-        }
-
-        // Comment
-        if ($string[0] === '#' || (isset($string[1])&&($string[0]==='-'&&$string[1]==='-') || ($string[0]==='/'&&$string[1]==='*'))) {
-            // Comment until end of line
-            if ($string[0] === '-' || $string[0] === '#') {
-                $last = strpos($string, "\n");
-                $type = self::TOKEN_TYPE_COMMENT;
-            } else { // Comment until closing comment tag
-                $last = strpos($string, "*/", 2) + 2;
-                $type = self::TOKEN_TYPE_BLOCK_COMMENT;
-            }
-
-            if ($last === false) {
-                $last = strlen($string);
-            }
-
-            return array(
-                self::TOKEN_VALUE => substr($string, 0, $last),
-                self::TOKEN_TYPE  => $type
-            );
-        }
-
-        // Quoted String
-        if ($string[0]==='"' || $string[0]==='\'' || $string[0]==='`' || $string[0]==='[') {
-            $return = array(
-                self::TOKEN_TYPE => (($string[0]==='`' || $string[0]==='[')? self::TOKEN_TYPE_BACKTICK_QUOTE : self::TOKEN_TYPE_QUOTE),
-                self::TOKEN_VALUE => self::getQuotedString($string)
-            );
-
-            return $return;
-        }
-
-        // User-defined Variable
-        if ($string[0] === '@' && isset($string[1])) {
-            $ret = array(
-                self::TOKEN_VALUE => null,
-                self::TOKEN_TYPE => self::TOKEN_TYPE_VARIABLE
-            );
-            
-            // If the variable name is quoted
-            if ($string[1]==='"' || $string[1]==='\'' || $string[1]==='`') {
-                $ret[self::TOKEN_VALUE] = '@'.self::getQuotedString(substr($string,1));
-            }
-            // Non-quoted variable name
-            else {
-                preg_match('/^(@[a-zA-Z0-9\._\$]+)/',$string,$matches);
-                if ($matches) {
-                    $ret[self::TOKEN_VALUE] = $matches[1];
-                }
-            }
-            
-            if($ret[self::TOKEN_VALUE] !== null) return $ret;
-        }
-
-        // Number (decimal, binary, or hex)
-        if (preg_match('/^([0-9]+(\.[0-9]+)?|0x[0-9a-fA-F]+|0b[01]+)($|\s|"\'`|'.self::$regex_boundaries.')/',$string,$matches)) {
-            return array(
-                self::TOKEN_VALUE => $matches[1],
-                self::TOKEN_TYPE=>self::TOKEN_TYPE_NUMBER
-            );
-        }
-
-        // Boundary Character (punctuation and symbols)
-        if (preg_match('/^('.self::$regex_boundaries.')/',$string,$matches)) {
-            return array(
-                self::TOKEN_VALUE => $matches[1],
-                self::TOKEN_TYPE  => self::TOKEN_TYPE_BOUNDARY
-            );
-        }
-
-        // A reserved word cannot be preceded by a '.'
-        // this makes it so in "mytable.from", "from" is not considered a reserved word
-        if (!$previous || !isset($previous[self::TOKEN_VALUE]) || $previous[self::TOKEN_VALUE] !== '.') {
-            $upper = strtoupper($string);
-            // Top Level Reserved Word
-            if (preg_match('/^('.self::$regex_reserved_toplevel.')($|\s|'.self::$regex_boundaries.')/', $upper,$matches)) {
-                return array(
-                    self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED_TOPLEVEL,
-                    self::TOKEN_VALUE=>substr($string,0,strlen($matches[1]))
-                );
-            }
-            // Newline Reserved Word
-            if (preg_match('/^('.self::$regex_reserved_newline.')($|\s|'.self::$regex_boundaries.')/', $upper,$matches)) {
-                return array(
-                    self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED_NEWLINE,
-                    self::TOKEN_VALUE=>substr($string,0,strlen($matches[1]))
-                );
-            }
-            // Other Reserved Word
-            if (preg_match('/^('.self::$regex_reserved.')($|\s|'.self::$regex_boundaries.')/', $upper,$matches)) {
-                return array(
-                    self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED,
-                    self::TOKEN_VALUE=>substr($string,0,strlen($matches[1]))
-                );
-            }
-        }
-
-        // A function must be suceeded by '('
-        // this makes it so "count(" is considered a function, but "count" alone is not
-        $upper = strtoupper($string);
-        // function
-        if (preg_match('/^('.self::$regex_function.'[(]|\s|[)])/', $upper,$matches)) {
-            return array(
-                self::TOKEN_TYPE=>self::TOKEN_TYPE_RESERVED,
-                self::TOKEN_VALUE=>substr($string,0,strlen($matches[1])-1)
-            );
-        }
-
-        // Non reserved word
-        preg_match('/^(.*?)($|\s|["\'`]|'.self::$regex_boundaries.')/',$string,$matches);
-
-        return array(
-            self::TOKEN_VALUE => $matches[1],
-            self::TOKEN_TYPE  => self::TOKEN_TYPE_WORD
-        );
-    }
-
-    /**
-     * Get quoted string
-     *
-     * @param $string String to quote
-     *
-     * @return string String quoted
-     */
-    protected static function getQuotedString($string)
-    {
-        $ret = null;
-
-        // This checks for the following patterns:
-        // 1. backtick quoted string using `` to escape
-        // 2. square bracket quoted string (SQL Server) using ]] to escape
-        // 3. double quoted string using "" or \" to escape
-        // 4. single quoted string using '' or \' to escape
-        if ( preg_match('/^(((`[^`]*($|`))+)|((\[[^\]]*($|\]))(\][^\]]*($|\]))*)|(("[^"\\\\]*(?:\\\\.[^"\\\\]*)*("|$))+)|((\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*(\'|$))+))/s', $string, $matches)) {
-            $ret = $matches[1];
-        }
-        
-        return $ret;
-    }
-
-    /**
-     * Takes a SQL string and breaks it into tokens.
-     * Each token is an associative array with type and value.
-     *
-     * @param String $string The SQL string
-     *
-     * @return Array An array of tokens.
-     */
-    protected static function tokenize($string)
-    {
-        self::init();
-
-        $tokens = array();
-
-        // Used for debugging if there is an error while tokenizing the string
-        $original_length = strlen($string);
-
-        // Used to make sure the string keeps shrinking on each iteration
-        $old_string_len = strlen($string) + 1;
-
-        $token = null;
-
-        $current_length = strlen($string);
-
-        // Keep processing the string until it is empty
-        while ($current_length) {
-            // If the string stopped shrinking, there was a problem
-            if ($old_string_len <= $current_length) {
-                $tokens[] = array(
-                    self::TOKEN_VALUE=>$string,
-                    self::TOKEN_TYPE=>self::TOKEN_TYPE_ERROR
-                );
-
-                return $tokens;
-            }
-            $old_string_len =  $current_length;
-
-            // Determine if we can use caching
-            if ($current_length >= self::$max_cachekey_size) {
-                $cacheKey = substr($string,0,self::$max_cachekey_size);
-            } else {
-                $cacheKey = false;
-            }
-
-            // See if the token is already cached
-            if ($cacheKey && isset(self::$token_cache[$cacheKey])) {
-                // Retrieve from cache
-                $token = self::$token_cache[$cacheKey];
-                $token_length = strlen($token[self::TOKEN_VALUE]);
-                self::$cache_hits++;
-            } else {
-                // Get the next token and the token type
-                $token = self::getNextToken($string, $token);
-                $token_length = strlen($token[self::TOKEN_VALUE]);
-                self::$cache_misses++;
-
-                // If the token is shorter than the max length, store it in cache
-                if ($cacheKey && $token_length < self::$max_cachekey_size) {
-                    self::$token_cache[$cacheKey] = $token;
-                }
-            }
-
-            $tokens[] = $token;
-
-            // Advance the string
-            $string = substr($string, $token_length);
-
-            $current_length -= $token_length;
-        }
-
-        return $tokens;
-    }
-
-    /**
-     * Format the whitespace in a SQL string to make it easier to read.
-     *
-     * @param String  $string    The SQL string
-     * @param boolean $highlight If true, syntax highlighting will also be performed
-     *
-     * @return String The SQL string with HTML styles and formatting wrapped in a 
 tag
-     */
-    public static function format($string, $highlight=true)
-    {
-        // This variable will be populated with formatted html
-        $return = '';
-
-        // Use an actual tab while formatting and then switch out with self::$tab at the end
-        $tab = "\t";
-
-        $indent_level = 0;
-        $newline = false;
-        $inline_parentheses = false;
-        $increase_special_indent = false;
-        $increase_block_indent = false;
-        $indent_types = array();
-        $added_newline = false;
-        $inline_count = 0;
-        $inline_indented = false;
-        $clause_limit = false;
-
-        // Tokenize String
-        $original_tokens = self::tokenize($string);
-
-        // Remove existing whitespace
-        $tokens = array();
-        foreach ($original_tokens as $i=>$token) {
-            if ($token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
-                $token['i'] = $i;
-                $tokens[] = $token;
-            }
-        }
-
-        // Format token by token
-        foreach ($tokens as $i=>$token) {
-            // Get highlighted token if doing syntax highlighting
-            if ($highlight) {
-                $highlighted = self::highlightToken($token);
-            } else { // If returning raw text
-                $highlighted = $token[self::TOKEN_VALUE];
-            }
-
-            // If we are increasing the special indent level now
-            if ($increase_special_indent) {
-                $indent_level++;
-                $increase_special_indent = false;
-                array_unshift($indent_types,'special');
-            }
-            // If we are increasing the block indent level now
-            if ($increase_block_indent) {
-                $indent_level++;
-                $increase_block_indent = false;
-                array_unshift($indent_types,'block');
-            }
-
-            // If we need a new line before the token
-            if ($newline) {
-                $return .= "\n" . str_repeat($tab, $indent_level);
-                $newline = false;
-                $added_newline = true;
-            } else {
-                $added_newline = false;
-            }
-
-            // Display comments directly where they appear in the source
-            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
-                if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
-                    $indent = str_repeat($tab,$indent_level);
-                    $return .= "\n" . $indent;
-                    $highlighted = str_replace("\n","\n".$indent,$highlighted);
-                }
-
-                $return .= $highlighted;
-                $newline = true;
-                continue;
-            }
-
-            if ($inline_parentheses) {
-                // End of inline parentheses
-                if ($token[self::TOKEN_VALUE] === ')') {
-                    $return = rtrim($return,' ');
-
-                    if ($inline_indented) {
-                        array_shift($indent_types);
-                        $indent_level --;
-                        $return .= "\n" . str_repeat($tab, $indent_level);
-                    }
-
-                    $inline_parentheses = false;
-
-                    $return .= $highlighted . ' ';
-                    continue;
-                }
-
-                if ($token[self::TOKEN_VALUE] === ',') {
-                    if ($inline_count >= 30) {
-                        $inline_count = 0;
-                        $newline = true;
-                    }
-                }
-
-                $inline_count += strlen($token[self::TOKEN_VALUE]);
-            }
-
-            // Opening parentheses increase the block indent level and start a new line
-            if ($token[self::TOKEN_VALUE] === '(') {
-                // First check if this should be an inline parentheses block
-                // Examples are "NOW()", "COUNT(*)", "int(10)", key(`somecolumn`), DECIMAL(7,2)
-                // Allow up to 3 non-whitespace tokens inside inline parentheses
-                $length = 0;
-                for ($j=1;$j<=250;$j++) {
-                    // Reached end of string
-                    if (!isset($tokens[$i+$j])) break;
-
-                    $next = $tokens[$i+$j];
-
-                    // Reached closing parentheses, able to inline it
-                    if ($next[self::TOKEN_VALUE] === ')') {
-                        $inline_parentheses = true;
-                        $inline_count = 0;
-                        $inline_indented = false;
-                        break;
-                    }
-
-                    // Reached an invalid token for inline parentheses
-                    if ($next[self::TOKEN_VALUE]===';' || $next[self::TOKEN_VALUE]==='(') {
-                        break;
-                    }
-
-                    // Reached an invalid token type for inline parentheses
-                    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) {
-                        break;
-                    }
-
-                    $length += strlen($next[self::TOKEN_VALUE]);
-                }
-
-                if ($inline_parentheses && $length > 30) {
-                    $increase_block_indent = true;
-                    $inline_indented = true;
-                    $newline = true;
-                }
-
-                // Take out the preceding space unless there was whitespace there in the original query
-                if (isset($original_tokens[$token['i']-1]) && $original_tokens[$token['i']-1][self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
-                    $return = rtrim($return,' ');
-                }
-
-                if (!$inline_parentheses) {
-                    $increase_block_indent = true;
-                    // Add a newline after the parentheses
-                    $newline = true;
-                }
-
-            }
-
-            // Closing parentheses decrease the block indent level
-            elseif ($token[self::TOKEN_VALUE] === ')') {
-                // Remove whitespace before the closing parentheses
-                $return = rtrim($return,' ');
-
-                $indent_level--;
-
-                // Reset indent level
-                while ($j=array_shift($indent_types)) {
-                    if ($j==='special') {
-                        $indent_level--;
-                    } else {
-                        break;
-                    }
-                }
-
-                if ($indent_level < 0) {
-                    // This is an error
-                    $indent_level = 0;
-
-                    if ($highlight) {
-                        $return .= "\n".self::highlightError($token[self::TOKEN_VALUE]);
-                        continue;
-                    }
-                }
-
-                // Add a newline before the closing parentheses (if not already added)
-                if (!$added_newline) {
-                    $return .= "\n" . str_repeat($tab, $indent_level);
-                }
-            }
-
-            // Top level reserved words start a new line and increase the special indent level
-            elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_TOPLEVEL) {
-                $increase_special_indent = true;
-
-                // If the last indent type was 'special', decrease the special indent for this round
-                reset($indent_types);
-                if (current($indent_types)==='special') {
-                    $indent_level--;
-                    array_shift($indent_types);
-                }
-
-                // Add a newline after the top level reserved word
-                $newline = true;
-                // Add a newline before the top level reserved word (if not already added)
-                if (!$added_newline) {
-                    $return .= "\n" . str_repeat($tab, $indent_level);
-                }
-                // If we already added a newline, redo the indentation since it may be different now
-                else {
-                    $return = rtrim($return,$tab).str_repeat($tab, $indent_level);
-                }
-
-                // If the token may have extra whitespace
-                if (strpos($token[self::TOKEN_VALUE],' ')!==false || strpos($token[self::TOKEN_VALUE],"\n")!==false || strpos($token[self::TOKEN_VALUE],"\t")!==false) {
-                    $highlighted = preg_replace('/\s+/',' ',$highlighted);
-                }
-                //if SQL 'LIMIT' clause, start variable to reset newline
-                if ($token[self::TOKEN_VALUE] === 'LIMIT' && !$inline_parentheses) {
-                    $clause_limit = true;
-                }
-            }
-
-            // Checks if we are out of the limit clause
-            elseif ($clause_limit && $token[self::TOKEN_VALUE] !== "," && $token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_NUMBER && $token[self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
-                $clause_limit = false;
-            }
-
-            // Commas start a new line (unless within inline parentheses or SQL 'LIMIT' clause)
-            elseif ($token[self::TOKEN_VALUE] === ',' && !$inline_parentheses) {
-                //If the previous TOKEN_VALUE is 'LIMIT', resets new line
-                if ($clause_limit === true) {
-                    $newline = false;
-                    $clause_limit = false;
-                }
-                // All other cases of commas
-                else {
-                    $newline = true;
-                }
-            }
-
-            // Newline reserved words start a new line
-            elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_NEWLINE) {
-                // Add a newline before the reserved word (if not already added)
-                if (!$added_newline) {
-                    $return .= "\n" . str_repeat($tab, $indent_level);
-                }
-
-                // If the token may have extra whitespace
-                if (strpos($token[self::TOKEN_VALUE],' ')!==false || strpos($token[self::TOKEN_VALUE],"\n")!==false || strpos($token[self::TOKEN_VALUE],"\t")!==false) {
-                    $highlighted = preg_replace('/\s+/',' ',$highlighted);
-                }
-            }
-
-            // Multiple boundary characters in a row should not have spaces between them (not including parentheses)
-            elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BOUNDARY) {
-                if (isset($tokens[$i-1]) && $tokens[$i-1][self::TOKEN_TYPE] === self::TOKEN_TYPE_BOUNDARY) {
-                    if (isset($original_tokens[$token['i']-1]) && $original_tokens[$token['i']-1][self::TOKEN_TYPE] !== self::TOKEN_TYPE_WHITESPACE) {
-                        $return = rtrim($return,' ');
-                    }
-                }
-            }
-
-            // If the token shouldn't have a space before it
-            if ($token[self::TOKEN_VALUE] === '.' || $token[self::TOKEN_VALUE] === ',' || $token[self::TOKEN_VALUE] === ';') {
-                $return = rtrim($return, ' ');
-            }
-
-            $return .= $highlighted.' ';
-
-            // If the token shouldn't have a space after it
-            if ($token[self::TOKEN_VALUE] === '(' || $token[self::TOKEN_VALUE] === '.') {
-                $return = rtrim($return,' ');
-            }
-            
-            // If this is the "-" of a negative number, it shouldn't have a space after it
-            if($token[self::TOKEN_VALUE] === '-' && isset($tokens[$i+1]) && $tokens[$i+1][self::TOKEN_TYPE] === self::TOKEN_TYPE_NUMBER && isset($tokens[$i-1])) {
-                $prev = $tokens[$i-1][self::TOKEN_TYPE];
-                if($prev !== self::TOKEN_TYPE_QUOTE && $prev !== self::TOKEN_TYPE_BACKTICK_QUOTE && $prev !== self::TOKEN_TYPE_WORD && $prev !== self::TOKEN_TYPE_NUMBER) {
-                    $return = rtrim($return,' ');
-                }
-            } 
-        }
-
-        // If there are unmatched parentheses
-        if ($highlight && array_search('block',$indent_types) !== false) {
-            $return .= "\n".self::highlightError("WARNING: unclosed parentheses or section");
-        }
-
-        // Replace tab characters with the configuration tab character
-        $return = trim(str_replace("\t",self::$tab,$return));
-
-        if ($highlight) {
-            $return = self::output($return);
-        }
-
-        return $return;
-    }
-
-    /**
-     * Add syntax highlighting to a SQL string
-     *
-     * @param String $string The SQL string
-     *
-     * @return String The SQL string with HTML styles applied
-     */
-    public static function highlight($string)
-    {
-        $tokens = self::tokenize($string);
-
-        $return = '';
-
-        foreach ($tokens as $token) {
-            $return .= self::highlightToken($token);
-        }
-
-        return self::output($return);
-    }
-
-    /**
-     * Split a SQL string into multiple queries.
-     * Uses ";" as a query delimiter.
-     *
-     * @param String $string The SQL string
-     *
-     * @return Array An array of individual query strings without trailing semicolons
-     */
-    public static function splitQuery($string)
-    {
-        $queries = array();
-        $current_query = '';
-        $empty = true;
-
-        $tokens = self::tokenize($string);
-
-        foreach ($tokens as $token) {
-            // If this is a query separator
-            if ($token[self::TOKEN_VALUE] === ';') {
-                if (!$empty) {
-                    $queries[] = $current_query.';';
-                }
-                $current_query = '';
-                $empty = true;
-                continue;
-            }
-
-            // If this is a non-empty character
-            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) {
-                $empty = false;
-            }
-
-            $current_query .= $token[self::TOKEN_VALUE];
-        }
-
-        if (!$empty) {
-            $queries[] = trim($current_query);
-        }
-
-        return $queries;
-    }
-
-    /**
-     * Remove all comments from a SQL string
-     *
-     * @param String $string The SQL string
-     *
-     * @return String The SQL string without comments
-     */
-    public static function removeComments($string)
-    {
-        $result = '';
-
-        $tokens = self::tokenize($string);
-
-        foreach ($tokens as $token) {
-            // Skip comment tokens
-            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
-                continue;
-            }
-
-            $result .= $token[self::TOKEN_VALUE];
-        }
-        $result = self::format( $result,false);
-
-        return $result;
-    }
-
-    /**
-     * Compress a query by collapsing white space and removing comments
-     *
-     * @param String $string The SQL string
-     *
-     * @return String The SQL string without comments
-     */
-    public static function compress($string)
-    {
-        $result = '';
-
-        $tokens = self::tokenize($string);
-
-        $whitespace = true;
-        foreach ($tokens as $token) {
-            // Skip comment tokens
-            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
-                continue;
-            }
-            // Remove extra whitespace in reserved words (e.g "OUTER     JOIN" becomes "OUTER JOIN")
-            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) {
-                $token[self::TOKEN_VALUE] = preg_replace('/\s+/',' ',$token[self::TOKEN_VALUE]);
-            }
-
-            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_WHITESPACE) {
-                // If the last token was whitespace, don't add another one
-                if ($whitespace) {
-                    continue;
-                } else {
-                    $whitespace = true;
-                    // Convert all whitespace to a single space
-                    $token[self::TOKEN_VALUE] = ' ';
-                }
-            } else {
-                $whitespace = false;
-            }
-
-            $result .= $token[self::TOKEN_VALUE];
-        }
-
-        return rtrim($result);
-    }
-
-    /**
-     * Highlights a token depending on its type.
-     *
-     * @param Array $token An associative array containing type and value.
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightToken($token)
-    {
-        $type = $token[self::TOKEN_TYPE];
-
-        if (self::is_cli()) {
-            $token = $token[self::TOKEN_VALUE];
-        } else {
-            if (defined('ENT_IGNORE')) {
-              $token = htmlentities($token[self::TOKEN_VALUE],ENT_COMPAT | ENT_IGNORE ,'UTF-8');
-            } else {
-              $token = htmlentities($token[self::TOKEN_VALUE],ENT_COMPAT,'UTF-8');
-            }
-        }
-
-        if ($type===self::TOKEN_TYPE_BOUNDARY) {
-            return self::highlightBoundary($token);
-        } elseif ($type===self::TOKEN_TYPE_WORD) {
-            return self::highlightWord($token);
-        } elseif ($type===self::TOKEN_TYPE_BACKTICK_QUOTE) {
-            return self::highlightBacktickQuote($token);
-        } elseif ($type===self::TOKEN_TYPE_QUOTE) {
-            return self::highlightQuote($token);
-        } elseif ($type===self::TOKEN_TYPE_RESERVED) {
-            return self::highlightReservedWord($token);
-        } elseif ($type===self::TOKEN_TYPE_RESERVED_TOPLEVEL) {
-            return self::highlightReservedWord($token);
-        } elseif ($type===self::TOKEN_TYPE_RESERVED_NEWLINE) {
-            return self::highlightReservedWord($token);
-        } elseif ($type===self::TOKEN_TYPE_NUMBER) {
-            return self::highlightNumber($token);
-        } elseif ($type===self::TOKEN_TYPE_VARIABLE) {
-            return self::highlightVariable($token);
-        } elseif ($type===self::TOKEN_TYPE_COMMENT || $type===self::TOKEN_TYPE_BLOCK_COMMENT) {
-            return self::highlightComment($token);
-        }
-
-        return $token;
-    }
-
-    /**
-     * Highlights a quoted string
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightQuote($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_quote . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights a backtick quoted string
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightBacktickQuote($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_backtick_quote . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights a reserved word
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightReservedWord($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_reserved . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights a boundary token
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightBoundary($value)
-    {
-        if ($value==='(' || $value===')') return $value;
-
-        if (self::is_cli()) {
-            return self::$cli_boundary . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights a number
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightNumber($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_number . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights an error
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightError($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_error . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights a comment
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightComment($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_comment . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights a word token
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightWord($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_word . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Highlights a variable token
-     *
-     * @param String $value The token's value
-     *
-     * @return String HTML code of the highlighted token.
-     */
-    protected static function highlightVariable($value)
-    {
-        if (self::is_cli()) {
-            return self::$cli_variable . $value . "\x1b[0m";
-        } else {
-            return '' . $value . '';
-        }
-    }
-
-    /**
-     * Helper function for building regular expressions for reserved words and boundary characters
-     *
-     * @param String $a The string to be quoted
-     *
-     * @return String The quoted string
-     */
-    private static function quote_regex($a)
-    {
-        return preg_quote($a,'/');
-    }
-
-    /**
-     * Helper function for building string output
-     *
-     * @param String $string The string to be quoted
-     *
-     * @return String The quoted string
-     */
-    private static function output($string)
-    {
-        if (self::is_cli()) {
-            return $string."\n";
-        } else {
-            $string=trim($string);
-            if (!self::$use_pre) {
-                return $string;
-            }
-
-            return '
' . $string . '
'; - } - } - - /** - * Check if the script is ran by CLI - * - * @return bool - */ - private static function is_cli() - { - if (isset(self::$cli)) return self::$cli; - else return php_sapi_name() === 'cli'; - } - -} diff --git a/libraries/sql-parser/src/Component.php b/libraries/sql-parser/src/Component.php index 7291ca5e31..1678706d11 100644 --- a/libraries/sql-parser/src/Component.php +++ b/libraries/sql-parser/src/Component.php @@ -51,6 +51,8 @@ namespace SqlParser { * @param TokensList $list The list of tokens that are being parsed. * @param array $options Parameters for parsing. * + * @throws \Exception Not implemented yet. + * * @return mixed */ public static function parse( @@ -69,6 +71,8 @@ namespace SqlParser { * * @param mixed $component The component to be built. * + * @throws \Exception Not implemented yet. + * * @return string */ public static function build($component) diff --git a/libraries/sql-parser/src/Components/UnionKeyword.php b/libraries/sql-parser/src/Components/UnionKeyword.php index 5a247bc46f..cbdb529138 100644 --- a/libraries/sql-parser/src/Components/UnionKeyword.php +++ b/libraries/sql-parser/src/Components/UnionKeyword.php @@ -9,9 +9,7 @@ namespace SqlParser\Components; use SqlParser\Component; -use SqlParser\Parser; -use SqlParser\Token; -use SqlParser\TokensList; +use SqlParser\Statements\SelectStatement; /** * `UNION` keyword builder. diff --git a/libraries/sql-parser/src/Context.php b/libraries/sql-parser/src/Context.php index dd912fb279..eb7f29976b 100644 --- a/libraries/sql-parser/src/Context.php +++ b/libraries/sql-parser/src/Context.php @@ -416,6 +416,10 @@ abstract class Context */ public static function load($context = '') { + /** + * @var Context $context + */ + if (empty($context)) { $context = self::$defaultContext; } @@ -467,6 +471,7 @@ abstract class Context ); continue; } + // Last generated context was valid (did not throw any exceptions). // So we return it, to let the user know what context was loaded. return $context; diff --git a/libraries/sql-parser/src/Lexer.php b/libraries/sql-parser/src/Lexer.php index 719a955222..697218c969 100644 --- a/libraries/sql-parser/src/Lexer.php +++ b/libraries/sql-parser/src/Lexer.php @@ -251,22 +251,31 @@ namespace SqlParser { $this->str[$this->last], $this->last ); - } elseif (($token->type === Token::TYPE_SYMBOL) + } elseif (($lastToken !== null) + && ($token->type === Token::TYPE_SYMBOL) && ($token->flags & Token::FLAG_SYMBOL_VARIABLE) - && ($lastToken !== null) + && (($lastToken->type === Token::TYPE_STRING) + || (($lastToken->type === Token::TYPE_SYMBOL) + && ($lastToken->flags & Token::FLAG_SYMBOL_BACKTICK))) ) { // Handles ```... FROM 'user'@'%' ...```. - if ((($lastToken->type === Token::TYPE_SYMBOL) - && ($lastToken->flags & Token::FLAG_SYMBOL_BACKTICK)) - || ($lastToken->type === Token::TYPE_STRING) - ) { - $lastToken->token .= $token->token; - $lastToken->type = Token::TYPE_SYMBOL; - $lastToken->flags = Token::FLAG_SYMBOL_USER; - $lastToken->value .= '@' . $token->value; - continue; - } + $lastToken->token .= $token->token; + $lastToken->type = Token::TYPE_SYMBOL; + $lastToken->flags = Token::FLAG_SYMBOL_USER; + $lastToken->value .= '@' . $token->value; + continue; + } elseif (($lastToken !== null) + && ($token->type === Token::TYPE_KEYWORD) + && ($lastToken->type === Token::TYPE_OPERATOR) + && ($lastToken->value === '.') + ) { + // Handles ```... tbl.FROM ...```. In this case, FROM is not + // a reserved word. + $token->type = Token::TYPE_NONE; + $token->flags = 0; + $token->value = $token->token; } + $token->position = $lastIdx; $list->tokens[$list->count++] = $token; @@ -306,6 +315,16 @@ namespace SqlParser { while ((++$this->last < $this->len) && (!Context::isWhitespace($this->str[$this->last]))) { $this->delimiter .= $this->str[$this->last]; } + + if (empty($this->delimiter)) { + $this->error( + __('Expected delimiter.'), + '', + $this->last + ); + $this->delimiter = ';'; + } + --$this->last; // Saving the delimiter and its token. @@ -598,7 +617,7 @@ namespace SqlParser { } elseif (($this->last + 1 < $this->len) && ($this->str[$this->last] === '0') && (($this->str[$this->last + 1] === 'x') - || ($this->str[$this->last + 1] === 'X')) + || ($this->str[$this->last + 1] === 'X')) ) { $token .= $this->str[$this->last++]; $state = 2; diff --git a/libraries/sql-parser/src/Parser.php b/libraries/sql-parser/src/Parser.php index 911e71be4b..b44ecc6638 100644 --- a/libraries/sql-parser/src/Parser.php +++ b/libraries/sql-parser/src/Parser.php @@ -51,7 +51,12 @@ namespace SqlParser { */ public static $STATEMENT_PARSERS = array( + // MySQL Utility Statements 'EXPLAIN' => 'SqlParser\\Statements\\ExplainStatement', + 'DESCRIBE' => 'SqlParser\\Statements\\ExplainStatement', + 'HELP' => '', + 'USE' => '', + 'STATUS' => '', // Table Maintenance Statements // https://dev.mysql.com/doc/refman/5.7/en/table-maintenance-sql.html @@ -65,7 +70,7 @@ namespace SqlParser { // Database Administration Statements // https://dev.mysql.com/doc/refman/5.7/en/sql-syntax-server-administration.html - 'SET' => '', + 'SET' => 'SqlParser\\Statements\\SetStatement', 'SHOW' => 'SqlParser\\Statements\\ShowStatement', // Data Definition Statements. @@ -327,7 +332,7 @@ namespace SqlParser { /** * Last transaction. - * @var TransactionStatement + * @var TransactionStatement $lastTransaction */ $lastTransaction = null; @@ -386,10 +391,15 @@ namespace SqlParser { // Checking if it is a known statement that can be parsed. if (empty(static::$STATEMENT_PARSERS[$token->value])) { - $this->error( - __('Unrecognized statement type.'), - $token - ); + if (!isset(static::$STATEMENT_PARSERS[$token->value])) { + // A statement is considered recognized if the parser + // is aware that it is a statement, but it does not have + // a parser for it yet. + $this->error( + __('Unrecognized statement type.'), + $token + ); + } // Skipping to the end of this statement. $list->getNextOfType(Token::TYPE_DELIMITER); // @@ -425,6 +435,10 @@ namespace SqlParser { && ($lastStatement instanceof SelectStatement) && ($statement instanceof SelectStatement) ) { + /** + * This SELECT statement. + * @var SelectStatement $statement + */ /** * Last SELECT statement. @@ -449,6 +463,10 @@ namespace SqlParser { // Handles transactions. if ($statement instanceof TransactionStatement) { + + /** + * @var TransactionStatement $statement + */ if ($statement->type === TransactionStatement::TYPE_BEGIN) { $lastTransaction = $statement; $this->statements[] = $statement; diff --git a/libraries/sql-parser/src/Statement.php b/libraries/sql-parser/src/Statement.php index 17a6002ad4..fabb82d7f1 100644 --- a/libraries/sql-parser/src/Statement.php +++ b/libraries/sql-parser/src/Statement.php @@ -124,7 +124,7 @@ abstract class Statement /** * The builder (parser) of this clause. - * @var string $class + * @var Component $class */ $class = Parser::$KEYWORD_PARSERS[$name]['class']; @@ -165,14 +165,11 @@ abstract class Statement public function parse(Parser $parser, TokensList $list) { /** - * Whether the beginning of this statement was previously parsed. - * - * This is used to delimit statements that don't use the usual - * delimiters. - * - * @var bool + * Array containing all list of clauses parsed. + * This is used to check for duplicates. + * @var array */ - $parsedBeginning = false; + $parsedClauses = array(); // This may be corrected by the parser. $this->first = $list->idx; @@ -216,7 +213,7 @@ abstract class Statement /** * The name of the class that is used for parsing. - * @var string $class + * @var Component $class */ $class = null; @@ -232,6 +229,20 @@ abstract class Statement */ $options = array(); + // Looking for duplicated clauses. + if ((!empty(Parser::$KEYWORD_PARSERS[$token->value])) + || (!empty(Parser::$STATEMENT_PARSERS[$token->value])) + ) { + if (!empty($parsedClauses[$token->value])) { + $parser->error( + __('This type of clause was previously parsed.'), $token + ); + break; + } + $parsedClauses[$token->value] = true; + } + + // Checking if this is the beginning of a clause. if (!empty(Parser::$KEYWORD_PARSERS[$token->value])) { $class = Parser::$KEYWORD_PARSERS[$token->value]['class']; $field = Parser::$KEYWORD_PARSERS[$token->value]['field']; @@ -240,17 +251,22 @@ abstract class Statement } } + // Checking if this is the beginning of the statement. if (!empty(Parser::$STATEMENT_PARSERS[$token->value])) { - if ($parsedBeginning) { - // New statement has started. We let the parser construct a - // new statement and parse that one + if ((!empty(static::$CLAUSES)) // Undefined for some statements. + && (empty(static::$CLAUSES[$token->value])) + ) { + // Some keywords (e.g. `SET`) may be the beginning of a + // statement and a clause. + // If such keyword was found and it cannot be a clause of + // this statement it means it is a new statement, but no + // delimiter was found between them. $parser->error( __('A new statement was found, but no delimiter between them.'), $token ); break; } - $parsedBeginning = true; if (!$parsedOptions) { if (empty(static::$OPTIONS[$token->value])) { // Skipping keyword because if it is not a option. diff --git a/libraries/sql-parser/src/Statements/ReplaceStatement.php b/libraries/sql-parser/src/Statements/ReplaceStatement.php index b6a7bb4edb..10c5e6312a 100644 --- a/libraries/sql-parser/src/Statements/ReplaceStatement.php +++ b/libraries/sql-parser/src/Statements/ReplaceStatement.php @@ -45,6 +45,22 @@ class ReplaceStatement extends Statement 'DELAYED' => 1, ); + /** + * The clauses of this statement, in order. + * + * @see Statement::$CLAUSES + * + * @var array + */ + public static $CLAUSES = array( + 'REPLACE' => array('REPLACE', 2), + // Used for options. + '_OPTIONS' => array('_OPTIONS', 1), + 'INTO' => array('FROM', 3), + 'VALUES' => array('VALUES', 1), + 'SET' => array('PARTITION', 3), + ); + /** * Tables used as target for this statement. * diff --git a/libraries/sql-parser/src/Statements/SetStatement.php b/libraries/sql-parser/src/Statements/SetStatement.php new file mode 100644 index 0000000000..d4128533a8 --- /dev/null +++ b/libraries/sql-parser/src/Statements/SetStatement.php @@ -0,0 +1,43 @@ + + * @license http://opensource.org/licenses/GPL-2.0 GNU Public License + */ +class SetStatement extends Statement +{ + + /** + * The clauses of this statement, in order. + * + * @see Statement::$CLAUSES + * + * @var array + */ + public static $CLAUSES = array( + 'SET' => array('SET', 3), + ); + + /** + * The updated values. + * + * @var SetOperation[] + */ + public $set; +} diff --git a/libraries/sql-parser/src/Statements/TransactionStatement.php b/libraries/sql-parser/src/Statements/TransactionStatement.php index d7c9dbfedf..0ae83fec3f 100644 --- a/libraries/sql-parser/src/Statements/TransactionStatement.php +++ b/libraries/sql-parser/src/Statements/TransactionStatement.php @@ -10,9 +10,7 @@ namespace SqlParser\Statements; use SqlParser\Parser; use SqlParser\Statement; -use SqlParser\Token; use SqlParser\TokensList; -use SqlParser\Components\Expression; use SqlParser\Components\OptionsArray; /** @@ -51,7 +49,7 @@ class TransactionStatement extends Statement /** * The list of statements in this transaction. * - * @var Statements[] + * @var Statement[] */ public $statements; @@ -110,6 +108,9 @@ class TransactionStatement extends Statement $ret = OptionsArray::build($this->options); if ($this->type === TransactionStatement::TYPE_BEGIN) { foreach ($this->statements as $statement) { + /** + * @var SelectStatement $statement + */ $ret .= ';' . $statement->build(); } $ret .= ';' . $this->end->build(); diff --git a/libraries/sql-parser/src/UtfString.php b/libraries/sql-parser/src/UtfString.php index cac93ab8cc..5f4ba12f9d 100644 --- a/libraries/sql-parser/src/UtfString.php +++ b/libraries/sql-parser/src/UtfString.php @@ -209,7 +209,7 @@ class UtfString implements \ArrayAccess /** * Returns the contained string. * - * @return strin + * @return string */ public function __toString() { diff --git a/libraries/sql-parser/src/Utils/Formatter.php b/libraries/sql-parser/src/Utils/Formatter.php new file mode 100644 index 0000000000..5d590c3b65 --- /dev/null +++ b/libraries/sql-parser/src/Utils/Formatter.php @@ -0,0 +1,512 @@ + + * @license http://opensource.org/licenses/GPL-2.0 GNU Public License + */ +class Formatter +{ + + /** + * The formatting options. + * + * @var array + */ + public $options; + + /** + * Clauses that must be inlined. + * + * These clauses usually are short and it's nicer to have them inline. + * + * @var array + */ + public static $INLINE_CLAUSES = array( + 'CREATE' => true, + 'PROCEDURE' => true, + 'LIMIT' => true, + ); + + /** + * Constructor. + * + * @param array $options The formatting options. + */ + public function __construct(array $options = array()) + { + // The specified formatting options are merged with the default values. + $this->options = array_merge( + array( + + /** + * The format of the result. + * @var string The type ('text', 'cli' or 'html') + */ + 'type' => php_sapi_name() == 'cli' ? 'cli' : 'text', + + /** + * The line ending used. + * By default, for text this is "\n" and for HTML this is "
". + * @var string + */ + 'line_ending' => $this->options['type'] == 'html' ? '
' : "\n", + + /** + * The string used for indentation. + * @var string + */ + 'indentation' => " ", + + /** + * Whether comments should be removed or not. + * @var bool + */ + 'remove_comments' => false, + + /** + * Whether each clause should be on a new line. + * @var bool + */ + 'clause_newline' => true, + + /** + * Whether each part should be on a new line. + * Parts are delimited by brackets and commas. + * @var bool + */ + 'parts_newline' => true, + + /** + * Whether each part of each clause should be indented. + * @var bool + */ + 'indent_parts' => true, + + /** + * The styles used for HTML formatting. + * array($type, $flags, $span, $callback) + * @var array[] + */ + 'formats' => array( + array( + 'type' => Token::TYPE_KEYWORD, + 'flags' => Token::FLAG_KEYWORD_RESERVED, + 'html' => 'class="sql-reserved"', + 'cli' => "\e[35m", + 'function' => 'strtoupper', + ), + array( + 'type' => Token::TYPE_KEYWORD, + 'flags' => 0, + 'html' => 'class="sql-keyword"', + 'cli' => "\e[95m", + 'function' => 'strtoupper', + ), + array( + 'type' => Token::TYPE_COMMENT, + 'flags' => 0, + 'html' => 'class="sql-comment"', + 'cli' => "\e[37m", + 'function' => '', + ), + array( + 'type' => Token::TYPE_BOOL, + 'flags' => 0, + 'html' => 'class="sql-atom"', + 'cli' => "\e[36m", + 'function' => 'strtoupper', + ), + array( + 'type' => Token::TYPE_NUMBER, + 'flags' => 0, + 'html' => 'class="sql-number"', + 'cli' => "\e[92m", + 'function' => 'strtolower', + ), + array( + 'type' => Token::TYPE_STRING, + 'flags' => 0, + 'html' => 'class="sql-string"', + 'cli' => "\e[91m", + 'function' => '', + ), + array( + 'type' => Token::TYPE_SYMBOL, + 'flags' => 0, + 'html' => 'class="sql-variable"', + 'cli' => "\e[36m", + 'function' => '', + ), + ) + ), + $options + ); + + // `parts_newline` requires `clause_newline` + $this->options['parts_newline'] &= $this->options['clause_newline']; + } + + /** + * Formats the given list of tokens. + * + * @param TokensList $list The list of tokens. + * + * @return string + */ + public function formatList($list) + { + + /** + * The query to be returned. + * @var string $ret + */ + $ret = ''; + + /** + * The indentation level. + * @var int $indent + */ + $indent = 0; + + /** + * Whether the line ended. + * @var bool $lineEnded + */ + $lineEnded = false; + + /** + * The name of the last clause. + * @var string $lastClause + */ + $lastClause = ''; + + /** + * A stack that keeps track of the indentation level every time a new + * block is found. + * @var array $blocksIndentation + */ + $blocksIndentation = array(); + + /** + * A stack that keeps track of the line endings every time a new block + * is found. + * @var array $blocksLineEndings + */ + $blocksLineEndings = array(); + + /** + * Whether clause's options were formatted. + * @var bool $formattedOptions + */ + $formattedOptions = false; + + /** + * Previously parsed token. + * @var Token $prev + */ + $prev = null; + + /** + * Comments are being formatted separately to maintain the whitespaces + * before and after them. + * @var string $comment + */ + $comment = ''; + + // In order to be able to format the queries correctly, the next token + // must be taken into consideration. The loop below uses two pointers, + // `$prev` and `$curr` which store two consecutive tokens. + // Actually, at every iteration the previous token is being used. + for ($list->idx = 0; $list->idx < $list->count; ++$list->idx) { + + /** + * Token parsed at this moment. + * @var Token $curr + */ + $curr = $list->tokens[$list->idx]; + + if ($curr->type === Token::TYPE_WHITESPACE) { + + // Whitespaces are skipped because the formatter adds its own. + continue; + } elseif ($curr->type === Token::TYPE_COMMENT) { + + // Whether the comments should be parsed. + if (!empty($this->options['remove_comments'])) { + continue; + } + + if ($list->tokens[$list->idx - 1]->type === Token::TYPE_WHITESPACE) { + // The whitespaces before and after are preserved for + // formatting reasons. + $comment .= $list->tokens[$list->idx - 1]->token; + } + $comment .= $this->toString($curr); + if (($list->tokens[$list->idx + 1]->type === Token::TYPE_WHITESPACE) + && ($list->tokens[$list->idx + 2]->type !== Token::TYPE_COMMENT) + ) { + // Adding the next whitespace only there is no comment that + // follows it immediately which may cause adding a + // whitespace twice. + $comment .= $list->tokens[$list->idx + 1]->token; + } + + // Everything was handled here, no need to continue. + continue; + } + + // Checking if pointers were initialized. + if ($prev !== null) { + + // Checking if a new clause started. + if (static::isClause($prev)) { + $lastClause = $prev->value; + $formattedOptions = false; + } + + // The options of a clause should stay on the same line and everything that follows. + if (($this->options['parts_newline']) + && (!$formattedOptions) + && (empty(self::$INLINE_CLAUSES[$lastClause])) + && ($curr->type != Token::TYPE_KEYWORD) + ) { + $formattedOptions = true; + $lineEnded = true; + ++$indent; + } + + // Checking if this clause ended. + if ($tmp = static::isClause($curr)) { + if (($tmp == 2) || ($this->options['clause_newline'])) { + $lineEnded = true; + if ($this->options['parts_newline']) { + --$indent; + } + } + } + + // Indenting BEGIN ... END blocks. + if (($prev->type === Token::TYPE_KEYWORD) && ($prev->value === 'BEGIN')) { + $lineEnded = true; + array_push($blocksIndentation, $indent); + ++$indent; + } elseif (($curr->type === Token::TYPE_KEYWORD) && ($curr->value === 'END')) { + $lineEnded = true; + $indent = array_pop($blocksIndentation); + } + + // Formatting fragments delimited by comma. + if (($prev->type === Token::TYPE_OPERATOR) && ($prev->value === ',')) { + // Fragments delimited by a comma are broken into multiple + // pieces only if the clause if the clause is not inlined or + // this fragment is between brackets that were on new line. + if (((empty(self::$INLINE_CLAUSES[$lastClause])) + && ($this->options['parts_newline'])) + || (end($blocksLineEndings) === true) + ) { + $lineEnded = true; + } + } + + // Handling brackets. + // Brackets are indented only if the length of the fragment between + // them is longer than 30 characters. + if (($prev->type === Token::TYPE_OPERATOR) && ($prev->value === '(')) { + array_push($blocksIndentation, $indent); + if (static::getGroupLength($list) > 30) { + ++$indent; + $lineEnded = true; + } + array_push($blocksLineEndings, $lineEnded); + } elseif (($curr->type === Token::TYPE_OPERATOR) && ($curr->value === ')')) { + $indent = array_pop($blocksIndentation); + $lineEnded |= array_pop($blocksLineEndings); + } + + // Delimiter must be placed on the same line with the last + // clause. + if ($curr->type === Token::TYPE_DELIMITER) { + $lineEnded = false; + } + + // Adding the token. + $ret .= $this->toString($prev); + + // Finishing the line. + if ($lineEnded) { + if ($indent < 0) { + // TODO: Make sure this never occurs and delete it. + $indent = 0; + } + + if ($curr->type !== Token::TYPE_COMMENT) { + $ret .= $this->options['line_ending'] + . str_repeat($this->options['indentation'], $indent); + } + $lineEnded = false; + } else { + // If the line ended there is no point in adding whitespaces. + // Also, some tokens do not have spaces before or after them. + if (!((($prev->type === Token::TYPE_OPERATOR) && (($prev->value === '.') || ($prev->value === '('))) + // No space after . ( + || (($curr->type === Token::TYPE_OPERATOR) && (($curr->value === '.') || ($curr->value === ',') || ($curr->value === '(') || ($curr->value === ')'))) + // No space before . , ) + || (($curr->type === Token::TYPE_DELIMITER)) && (mb_strlen($curr->value, 'UTF-8') < 2)) + // A space after delimiters that are longer than 2 characters. + || ($prev->value === 'DELIMITER') + ) { + $ret .= ' '; + } + } + } + + if (!empty($comment)) { + $ret .= $comment; + $comment = ''; + } + + // Saving the next token as the one that will be processed during + // the next iteration. + $prev = $curr; + } + + return $ret; + } + + /** + * Tries to print the query and returns the result. + * + * @param Token $token The token to be printed. + * + * @return string + */ + public function toString($token) + { + $text = $token->token; + + foreach ($this->options['formats'] as $format) { + if (($token->type === $format['type']) + && (($token->flags & $format['flags']) === $format['flags']) + ) { + + // Running transformation function. + if (!empty($format['function'])) { + $func = $format['function']; + $text = $func($text); + } + + // Formatting HTML. + if ($this->options['type'] === 'html') { + return '' . $text . ''; + } elseif ($this->options['type'] === 'cli') { + return $format['cli'] . $text; + } + + break; + } + } + + if ($this->options['type'] === 'cli') { + return "\e[39m" . $text; + } + return $text; + } + + /** + * Formats a query. + * + * @param string $query The query to be formatted + * @param array $options The formatting options. + * + * @return string The formatted string. + */ + public static function format($query, array $options = array()) + { + $lexer = new Lexer($query); + $formatter = new Formatter($options); + return $formatter->formatList($lexer->list); + } + + /** + * Computes the length of a group. + * + * A group is delimited by a pair of brackets. + * + * @param TokensList $list The list of tokens. + * + * @return int + */ + public static function getGroupLength($list) + { + /** + * The number of opening brackets found. + * This counter starts at one because by the time this function called, + * the list already advanced one position and the opening bracket was + * already parsed. + * @var int + */ + $count = 1; + + /** + * The length of this group. + * @var int + */ + $length = 0; + + for ($idx = $list->idx; $idx < $list->count; ++$idx) { + // Counting the brackets. + if ($list->tokens[$idx]->type === Token::TYPE_OPERATOR) { + if ($list->tokens[$idx]->value === '(') { + ++$count; + } elseif ($list->tokens[$idx]->value === ')') { + --$count; + if ($count == 0) { + break; + } + } + } + + // Keeping track of this group's length. + $length += mb_strlen($list->tokens[$idx]->value, 'UTF-8'); + } + + return $length; + } + + /** + * Checks if a token is a statement or a clause inside a statement. + * + * @param Token $token The token to be checked. + * + * @return int|bool + */ + public static function isClause($token) + { + if ((($token->type === Token::TYPE_NONE) && (strtoupper($token->token) === 'DELIMITER')) + || (($token->type === Token::TYPE_KEYWORD) && (isset(Parser::$STATEMENT_PARSERS[$token->value]))) + ) { + return 2; + } elseif (($token->type === Token::TYPE_KEYWORD) && (isset(Parser::$KEYWORD_PARSERS[$token->value]))) { + return 1; + } + return false; + } +} diff --git a/lint.php b/lint.php index 7a13d317b1..9aef982888 100644 --- a/lint.php +++ b/lint.php @@ -28,7 +28,7 @@ require_once 'libraries/Linter.class.php'; * * @var string */ -$sql_query = $_REQUEST['sql_query']; +$sql_query = !empty($_POST['sql_query']) ? $_POST['sql_query'] : ''; // Disabling standard response. $response = PMA_Response::getInstance(); diff --git a/test/libraries/sql-formatter/lib/SqlFormatter_test.php b/test/libraries/sql-formatter/lib/SqlFormatter_test.php deleted file mode 100644 index 7412e1e87a..0000000000 --- a/test/libraries/sql-formatter/lib/SqlFormatter_test.php +++ /dev/null @@ -1,130 +0,0 @@ - 5 ORDER BY COUNT(order_id) DESC;", -"SELECT - customer_id, - customer_name, - COUNT(order_id) as total -FROM - customers - INNER JOIN orders ON customers.customer_id = orders.customer_id -GROUP BY - customer_id, - customer_name -HAVING - COUNT(order_id) > 5 -ORDER BY - COUNT(order_id) DESC;" -), - -array( -"SELECT a,b as c FROM `ab`; UPDATE `cd` SET `col` = REPLACE(col, 'find', 'replace') -WHERE row_id in (SELECT row_id FROM new_table WHERE col = 's' AND col2 = '3') LIMIT 256", -"SELECT - a, - b as c -FROM - `ab`; -UPDATE - `cd` -SET - `col` = REPLACE(col, 'find', 'replace') -WHERE - row_id in ( - SELECT - row_id - FROM - new_table - WHERE - col = 's' - AND col2 = '3' - ) -LIMIT - 256" -), - -array( -"INSERT INTO `a_long_table_name_it_is_really_log_but_still_not_that_long` -(a, b, c, d, e, f, g, a, b, c, d, e, f, c, d, e) -VALUES (1, 0, '', 1, NOW(), NOW(), 0), -(1, 0, 'helloabcdefgijk', 1, 'hello_world_again', NOW(), 0)", -"INSERT INTO `a_long_table_name_it_is_really_log_but_still_not_that_long` ( - a, b, c, d, e, f, g, a, b, c, d, e, f, c, d, e -) -VALUES - (1, 0, '', 1, NOW(), NOW(), 0), - ( - 1, 0, 'helloabcdefgijk', 1, 'hello_world_again', - NOW(), 0 - )" -), - -array( -"ALTER TABLE `PREFIX_product` DROP `reduction_price`,DROP `reduction_percent`, -DROP `reduction_from`, DROP `reduction_to`", -"ALTER TABLE - `PREFIX_product` -DROP - `reduction_price`, -DROP - `reduction_percent`, -DROP - `reduction_from`, -DROP - `reduction_to`" -), - ); - } - - /** - * Test for SqlFormatter::format - * - * @return void - * - * @dataProvider formatDataProvider - */ - public function testSqlFormatter_format($query, $expected) - { - SqlFormatter::$tab = "\t"; - $this->assertEquals( - $expected, - SqlFormatter::format($query, false) - ); - } -}