diff options
author | Jeremy Dorn <jeremy@jeremy.(none)> | 2012-12-18 13:46:03 -0800 |
---|---|---|
committer | Jeremy Dorn <jeremy@jeremy.(none)> | 2012-12-18 13:46:03 -0800 |
commit | 15ab7a899e33f42682f74804d340a6995711a0d4 (patch) | |
tree | 86c4273d76c4f2d1a65273bd33aec792d5a15ab1 /lib | |
parent | 251b032a1ae858063c4859430bee11b106bdd134 (diff) | |
download | sql-formatter-15ab7a899e33f42682f74804d340a6995711a0d4.zip sql-formatter-15ab7a899e33f42682f74804d340a6995711a0d4.tar.gz sql-formatter-15ab7a899e33f42682f74804d340a6995711a0d4.tar.bz2 |
Another optimization. Was performing an array merge for every format, changed it to only do it once.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/SqlFormatter.php | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/lib/SqlFormatter.php b/lib/SqlFormatter.php index 1579afc..96ea79e 100644 --- a/lib/SqlFormatter.php +++ b/lib/SqlFormatter.php @@ -70,8 +70,11 @@ class SqlFormatter // The tab character to use when formatting SQL public static $tab = ' '; - // This flag tells us if the reserved word list is sorted already - protected static $reserved_sorted; + // This flag tells us if SqlFormatted has been initialized + protected static $init; + + // This is a combination of all the boundary characters and all the whitespace characters + protected static $all_boundaries; /** * Return the next token and token type in a SQL string. @@ -192,13 +195,15 @@ class SqlFormatter ); } - // Sort reserved word list from longest word to shortest - if (!self::$reserved_sorted) { + if (!self::$init) { + //Sort reserved word list from longest word to shortest usort(self::$reserved, array('SqlFormatter','sortLength')); - self::$reserved_sorted = true; - } - $all_boundaries = array_merge(self::$boundaries, self::$whitespace); + //Combine boundary characters and whitespace + self::$all_boundaries = array_merge(self::$boundaries, self::$whitespace); + + self::$init = true; + } //a reserved word cannot be preceded by a '.' //this makes it so in "mytable.from", "from" is not considered a reserved word @@ -208,7 +213,7 @@ class SqlFormatter foreach (self::$reserved as $word) { // If(strlen($test < strlen($word))) continue; if (substr($test, 0, strlen($word)) === $word) { - if (isset($string[strlen($word)]) && !in_array($string[strlen($word)], $all_boundaries)) continue; + if (isset($string[strlen($word)]) && !in_array($string[strlen($word)], self::$all_boundaries)) continue; if (in_array($word, self::$special_reserved)) $type = 'special reserved'; else $type = 'reserved'; @@ -222,7 +227,7 @@ class SqlFormatter // Look for first word separator for ($i = 1, $length = strlen($string); $i < $length; $i++) { - if (in_array($string[$i], $all_boundaries)) { + if (in_array($string[$i], self::$all_boundaries)) { break; } } |