summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Dorn <jeremy@jeremydorn.com>2012-05-22 17:13:03 -0700
committerJeremy Dorn <jeremy@jeremydorn.com>2012-05-22 17:13:03 -0700
commitb763e808e64ffc830ace9b8b8e6f218062ff9254 (patch)
tree6c93b544e3a6b04f2b1bf26db20484273631c660
parent5c31c9f4a79d21f8b32baa41783efcd434b4fafb (diff)
downloadsql-formatter-b763e808e64ffc830ace9b8b8e6f218062ff9254.zip
sql-formatter-b763e808e64ffc830ace9b8b8e6f218062ff9254.tar.gz
sql-formatter-b763e808e64ffc830ace9b8b8e6f218062ff9254.tar.bz2
Adding a splitQuery method that splits a query string with multiple sql statements separated by ';'.
-rw-r--r--SqlFormatter.php40
1 files changed, 40 insertions, 0 deletions
diff --git a/SqlFormatter.php b/SqlFormatter.php
index 1302c1e..e02d839 100644
--- a/SqlFormatter.php
+++ b/SqlFormatter.php
@@ -362,7 +362,47 @@ class SqlFormatter {
}
return "<pre style='background:white;'>".trim($return)."</pre>";
+ }
+
+ public static function splitQuery($string) {
+ $queries = array();
+
+ $current_query = '';
+
+ $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));
+
+ //if this is a query separator
+ if($next_token === ';') {
+ if(trim($current_query)) $queries[] = $current_query;
+ $current_query = '';
+ continue;
+ }
+
+ $current_query .= $next_token;
+ }
+
+ if(trim($current_query)) {
+ $queries[] = $current_query;
+ }
+
+
+ return $queries;
}
}
?>