summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Dorn <jeremy@jeremydorn.com>2012-05-22 22:45:09 -0700
committerJeremy Dorn <jeremy@jeremydorn.com>2012-05-22 22:45:09 -0700
commit638725ce8ebcfe244c31f24a7c1a40b609eb8ff3 (patch)
treefc612666860ff6dfb151584352b6fa047a2089f1
parentb763e808e64ffc830ace9b8b8e6f218062ff9254 (diff)
downloadsql-formatter-638725ce8ebcfe244c31f24a7c1a40b609eb8ff3.zip
sql-formatter-638725ce8ebcfe244c31f24a7c1a40b609eb8ff3.tar.gz
sql-formatter-638725ce8ebcfe244c31f24a7c1a40b609eb8ff3.tar.bz2
Fixed bugs with splitQuery. Updated README and examples.
Fixed undefined variable error with highlight method.
-rw-r--r--README.md54
-rw-r--r--SqlFormatter.php8
-rw-r--r--examples/examples.php27
3 files changed, 78 insertions, 11 deletions
diff --git a/README.md b/README.md
index 21b13f5..806152f 100644
--- a/README.md
+++ b/README.md
@@ -25,11 +25,12 @@ as input and returns a formatted HTML block inside a pre tag.
Sample usage:
- <?php
- require_once('SqlFormatter.php');
-
- echo SqlFormatter::format("SELECT * FROM MyTable LIMIT 10");
- ?>
+```php
+<?php
+require_once('SqlFormatter.php');
+
+echo SqlFormatter::format("SELECT * FROM MyTable LIMIT 10");
+```
Sample output:
@@ -44,6 +45,43 @@ 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");
- ?>
+```php
+<?php
+echo SqlFormatter::highlight("SELECT * FROM MyTable LIMIT 10");
+```
+
+Split SQL String into Queries
+--------------------------
+
+Another feature, which is unrelated to formatting, is the ability to break up a SQL string into multiple queries.
+
+For Example:
+
+```sql
+DROP TABLE IF EXISTS MyTable;
+CREATE TABLE MyTable ( id int );
+INSERT INTO MyTable (id)
+ VALUES
+ (1),(2),(3),(4);
+SELECT * FROM MyTable;
+```
+
+```php
+$queries = SqlFormatter::splitQuery($sql);
+```
+$queries is now an array of the 4 queries without trailing semicolons.
+
+Why not just use explode(';',$sql) or regular expressions? The following example sql and others like it
+are impossible to split correctly using regular expressions.
+
+```sql
+SELECT ";"; SELECT ";\"; a;";
+SELECT ";
+ abc";
+SELECT a,b #comment;
+FROM test;
+```
+
+The splitQuery method will still fail in the following cases:
+* The DELIMITER command can be used to change the delimiter from the default ';' to something else.
+* The CREATE PROCEDURE command
diff --git a/SqlFormatter.php b/SqlFormatter.php
index e02d839..74f1fab 100644
--- a/SqlFormatter.php
+++ b/SqlFormatter.php
@@ -311,6 +311,8 @@ class SqlFormatter {
public static function highlight($string) {
$old_string_len = strlen($string) + 1;
+ $return = '';
+
//keep processing the string until it is empty
while(strlen($string)) {
//if the string stopped shrinking, there was a problem
@@ -382,14 +384,14 @@ class SqlFormatter {
//get the next token and the token type
$type = null;
$raw_token = self::getNextToken($string,$type);
- $next_token = htmlentities($raw_token);
+ $next_token = $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;
+ if(trim($current_query)) $queries[] = trim($current_query);
$current_query = '';
continue;
}
@@ -398,7 +400,7 @@ class SqlFormatter {
}
if(trim($current_query)) {
- $queries[] = $current_query;
+ $queries[] = trim($current_query);
}
diff --git a/examples/examples.php b/examples/examples.php
index f8bf524..869be4a 100644
--- a/examples/examples.php
+++ b/examples/examples.php
@@ -38,4 +38,31 @@ foreach($statements as $sql) {
echo "<hr />";
echo SqlFormatter::highlight($sql);
}
+
+
+$split_statements = array(
+ 'DROP TABLE IF EXISTS MyTable;
+ CREATE TABLE MyTable ( id int );
+ INSERT INTO MyTable (id)
+ VALUES
+ (1),(2),(3),(4);
+ SELECT * FROM MyTable;',
+
+ 'SELECT ";"; SELECT ";\"; a;";
+ SELECT ";
+ abc";
+ SELECT a,b #comment;
+ FROM test;'
+);
+
+echo "<h1>Splitting Queries</h1>";
+foreach($split_statements as $sql) {
+ echo "<hr />";
+ $queries = SqlFormatter::splitQuery($sql);
+ echo "<ol>";
+ foreach($queries as $query) {
+ echo "<li>".SqlFormatter::highlight($query)."</li>";
+ }
+ echo "</ol>";
+}
?>