diff options
author | Jeremy Dorn <jeremy@jeremydorn.com> | 2012-05-12 08:59:50 -0700 |
---|---|---|
committer | Jeremy Dorn <jeremy@jeremydorn.com> | 2012-05-12 08:59:50 -0700 |
commit | a90c9d4a49ac2d58d9737aecfeb117c2091abfe0 (patch) | |
tree | 01315938f1252f25ed9a245b6b4659cfe586571d | |
parent | 4c6d7f11e8766ba49abf27afbcc0793df50ca7db (diff) | |
download | sql-formatter-a90c9d4a49ac2d58d9737aecfeb117c2091abfe0.zip sql-formatter-a90c9d4a49ac2d58d9737aecfeb117c2091abfe0.tar.gz sql-formatter-a90c9d4a49ac2d58d9737aecfeb117c2091abfe0.tar.bz2 |
Added new method 'highlight' that only does syntax highlighting and
preserves original whitespace.
-rw-r--r-- | README.md | 15 | ||||
-rw-r--r-- | SqlFormatter.php | 57 | ||||
-rw-r--r-- | examples/examples.php | 7 |
3 files changed, 78 insertions, 1 deletions
@@ -28,9 +28,22 @@ Sample usage: <?php require_once('SqlFormatter.php'); - echo SqlFormatter::format("SELECT * FROM Table LIMIT 10"); + echo SqlFormatter::format("SELECT * FROM MyTable LIMIT 10"); ?> Sample output:  + +Syntax Highlighting Only +------------------------- + +There is also a static method 'highlight' that only does syntax highlighting +and preserves all original whitespace. + +This is useful for sql that is already well formatted and just needs to be a little +easier to read. + + <?php + echo SqlFormatter::highlight("SELECT * FROM MyTable LIMIT 10"); + ?> diff --git a/SqlFormatter.php b/SqlFormatter.php index 7c9b37d..d7db83d 100644 --- a/SqlFormatter.php +++ b/SqlFormatter.php @@ -307,5 +307,62 @@ class SqlFormatter { return "<pre style='background:white;'>".trim($return)."</pre>"; } + + public static function highlight($string) { + $old_string_len = strlen($string) + 1; + + //keep processing the string until it is empty + while(strlen($string)) { + //if the string stopped shrinking, there was a problem + if($old_string_len <= strlen($string)) { + throw new Exception("SQL PARSE ERROR"); + } + $old_string_len = strlen($string); + + //get the next token and the token type + $type = null; + $raw_token = self::getNextToken($string,$type); + $next_token = htmlentities($raw_token); + + //advance the string forward + $string = substr($string,strlen($raw_token)); + + switch($type) { + case 'backtick quote': + $return .= "<span style='".self::$backtick_quote_style."'>".$next_token."</span>"; + break; + case 'quote': + $return .= "<span style='".self::$quote_style."'>".$next_token."</span>"; + break; + case 'reserved': + case 'special reserved': + $return .= "<span style='".self::$reserved_style."'>".$next_token."</span>"; + break; + case '(': + $return .= '('; + break; + case ')': + $return .= ")"; + break; + case 'number': + $return .= "<span style='".self::$number_style."'>".$next_token."</span>"; + break; + case 'boundary': + case '.': + case ',': + $return .= "<span style='".self::$boundary_style."'>".$next_token."</span>"; + break; + case 'comment': + case 'block comment': + $return .= "<span style='".self::$comment_style."'>".$next_token."</span>"; + break; + default: + $return .= "<span style='".self::$default_style."'>".$next_token."</span>"; + } + } + + return "<pre style='background:white;'>".trim($return)."</pre>"; + + } } ?> diff --git a/examples/examples.php b/examples/examples.php index df198c3..f8bf524 100644 --- a/examples/examples.php +++ b/examples/examples.php @@ -27,8 +27,15 @@ $statements = array( as temp, DateCreated as Created FROM MyTable;", ); +echo "<h1>Formatting</h1>"; foreach($statements as $sql) { echo "<hr />"; echo SqlFormatter::format($sql); } + +echo "<h1>Syntax Highlighting Only</h1>"; +foreach($statements as $sql) { + echo "<hr />"; + echo SqlFormatter::highlight($sql); +} ?> |