\SqlFormatter
SQL Formatter is a collection of utilities for debugging SQL queries.
It includes methods for formatting, syntax highlighting, removing comments, etc.
- Author: Jeremy Dorn <jeremy@jeremydorn.com>
- Copyright: 2013 Jeremy Dorn
- License: http://opensource.org/licenses/MIT
Synopsis
class SqlFormatter
{
- // constants
- 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 = 0;
- const TOKEN_VALUE = 1;
- // members
- protected array $reserved;
- protected array $reserved_toplevel;
- protected array $reserved_newline;
- protected array $boundaries;
- public string $quote_attributes;
- public string $backtick_quote_attributes;
- public string $reserved_attributes;
- public string $boundary_attributes;
- public string $number_attributes;
- public string $word_attributes;
- public string $error_attributes;
- public string $comment_attributes;
- public string $pre_attributes;
- public $cli;
- public string $cli_quote;
- public string $cli_backtick_quote;
- public string $cli_reserved;
- public string $cli_boundary;
- public string $cli_number;
- public string $cli_word;
- public string $cli_error;
- public string $cli_comment;
- public string $tab;
- public $use_pre;
- protected $init;
- protected $regex_boundaries;
- protected $regex_reserved;
- protected $regex_reserved_newline;
- protected $regex_reserved_toplevel;
- public integer $max_cachekey_size;
- protected array $token_cache;
- protected integer $cache_hits;
- protected integer $cache_misses;
- // methods
- public static Array getCacheStats()
- protected static void init()
- protected static Array getNextToken()
- protected static Array tokenize()
- public static String format()
- public static String highlight()
- public static Array splitQuery()
- public static String removeComments()
- protected static String highlightToken()
- protected static String highlightQuote()
- protected static String highlightBacktickQuote()
- protected static String highlightReservedWord()
- protected static String highlightBoundary()
- protected static String highlightNumber()
- protected static String highlightError()
- protected static String highlightComment()
- protected static String highlightWord()
- private static int sortLength()
- private static String quote_regex()
- private static String output()
- private static void is_cli()
Constants
Name | Value |
---|---|
TOKEN_TYPE_WHITESPACE | 0 |
TOKEN_TYPE_WORD | 1 |
TOKEN_TYPE_QUOTE | 2 |
TOKEN_TYPE_BACKTICK_QUOTE | 3 |
TOKEN_TYPE_RESERVED | 4 |
TOKEN_TYPE_RESERVED_TOPLEVEL | 5 |
TOKEN_TYPE_RESERVED_NEWLINE | 6 |
TOKEN_TYPE_BOUNDARY | 7 |
TOKEN_TYPE_COMMENT | 8 |
TOKEN_TYPE_BLOCK_COMMENT | 9 |
TOKEN_TYPE_NUMBER | 10 |
TOKEN_TYPE_ERROR | 11 |
TOKEN_TYPE | 0 |
TOKEN_VALUE | 1 |
Members
protected
- $boundaries
- $cache_hits
- $cache_misses
- $init
- $regex_boundaries
- $regex_reserved
- $regex_reserved_newline
- $regex_reserved_toplevel
- $reserved
- $reserved_newline
- $reserved_toplevel
- $token_cache
public
- $backtick_quote_attributes
- $boundary_attributes
- $cli
- $cli_backtick_quote
- $cli_boundary
- $cli_comment
- $cli_error
- $cli_number
- $cli_quote
- $cli_reserved
- $cli_word
- $comment_attributes
- $error_attributes
- $max_cachekey_size
- $number_attributes
- $pre_attributes
- $quote_attributes
- $reserved_attributes
- $tab
- $use_pre
- $word_attributes
Methods
private
- is_cli()
- output() — Helper function for building string output
- quote_regex() — Helper function for building regular expressions for reserved words and boundary characters
- sortLength() — Helper function for sorting the list of reserved words by length
protected
- getNextToken() — Return the next token and token type in a SQL string.
- highlightBacktickQuote() — Highlights a backtick quoted string
- highlightBoundary() — Highlights a boundary token
- highlightComment() — Highlights a comment
- highlightError() — Highlights an error
- highlightNumber() — Highlights a number
- highlightQuote() — Highlights a quoted string
- highlightReservedWord() — Highlights a reserved word
- highlightToken() — Highlights a token depending on its type.
- highlightWord() — Highlights a word token
- init() — Stuff that only needs to be done once. Builds regular expressions and sorts the reserved words.
- tokenize() — Takes a SQL string and breaks it into tokens.
public
- format() — Format the whitespace in a SQL string to make it easier to read.
- getCacheStats() — Get stats about the token cache
- highlight() — Add syntax highlighting to a SQL string
- removeComments() — Remove all comments from a SQL string
- splitQuery() — Split a SQL string into multiple queries.