summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/SqlFormatter.php44
1 files changed, 43 insertions, 1 deletions
diff --git a/lib/SqlFormatter.php b/lib/SqlFormatter.php
index fec46e3..3a511a7 100644
--- a/lib/SqlFormatter.php
+++ b/lib/SqlFormatter.php
@@ -9,7 +9,7 @@
* @copyright 2013 Jeremy Dorn
* @license http://opensource.org/licenses/MIT
* @link http://github.com/jdorn/sql-formatter
- * @version 1.2.9
+ * @version 1.2.10
*/
class SqlFormatter
{
@@ -701,6 +701,48 @@ class SqlFormatter
}
/**
+ * 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;
+ }
+
+ 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.