summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Ungureanu <udan1107@gmail.com>2017-05-12 19:48:32 +0300
committerDan Ungureanu <udan11@users.noreply.github.com>2017-05-16 21:58:07 +0300
commit93167f30e093869e501939b7f0d42cf65965b2f9 (patch)
tree4797c4a891a67ebf5acf381449646a1122a4103f
parent867a9285327d26e4e23b5d098974c8fc3447bc06 (diff)
downloadsql-parser-93167f30e093869e501939b7f0d42cf65965b2f9.zip
sql-parser-93167f30e093869e501939b7f0d42cf65965b2f9.tar.gz
sql-parser-93167f30e093869e501939b7f0d42cf65965b2f9.tar.bz2
Improved format of INSERT queries.
Signed-off-by: Dan Ungureanu <udan1107@gmail.com>
-rw-r--r--src/Utils/Formatter.php25
-rw-r--r--tests/Utils/FormatterTest.php12
2 files changed, 27 insertions, 10 deletions
diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php
index 9926db1..9de9d99 100644
--- a/src/Utils/Formatter.php
+++ b/src/Utils/Formatter.php
@@ -29,6 +29,28 @@ class Formatter
public $options;
/**
+ * Clauses that are usually short.
+ *
+ * These clauses share the line with the next clause.
+ *
+ * E.g. if INSERT was not here, the formatter would produce:
+ *
+ * INSERT
+ * INTO foo
+ * VALUES(0, 0, 0),(1, 1, 1);
+ *
+ * Instead of:
+ *
+ * INSERT INTO foo
+ * VALUES(0, 0, 0),(1, 1, 1)
+ *
+ * @var array
+ */
+ public static $SHORT_CLAUSES = array(
+ 'INSERT' => true,
+ );
+
+ /**
* Clauses that must be inlined.
*
* These clauses usually are short and it's nicer to have them inline.
@@ -37,6 +59,7 @@ class Formatter
*/
public static $INLINE_CLAUSES = array(
'CREATE' => true,
+ 'INTO' => true,
'LIMIT' => true,
'PARTITION BY' => true,
'PARTITION' => true,
@@ -380,7 +403,7 @@ class Formatter
// Checking if this clause ended.
if ($tmp = static::isClause($curr)) {
- if ($tmp == 2 || $this->options['clause_newline']) {
+ if (($tmp == 2 || $this->options['clause_newline']) && empty(self::$SHORT_CLAUSES[$lastClause])) {
$lineEnded = true;
if ($this->options['parts_newline'] && $indent > 0) {
--$indent;
diff --git a/tests/Utils/FormatterTest.php b/tests/Utils/FormatterTest.php
index 12e9912..f90281d 100644
--- a/tests/Utils/FormatterTest.php
+++ b/tests/Utils/FormatterTest.php
@@ -430,17 +430,11 @@ class FormatTest extends TestCase
),
'insert' => array(
'query' => 'insert into foo values (0, 0, 0), (1, 1, 1)',
- 'text' => 'INSERT' . "\n" .
- 'INTO' . "\n" .
- ' foo' . "\n" .
+ 'text' => 'INSERT INTO foo' . "\n" .
'VALUES(0, 0, 0),(1, 1, 1)',
- 'cli' => "\x1b[35mINSERT" . "\n" .
- "\x1b[35mINTO" . "\n" .
- " \x1b[39mfoo" . "\n" .
+ 'cli' => "\x1b[35mINSERT \x1b[35mINTO \x1b[39mfoo" . "\n" .
"\x1b[35mVALUES\x1b[39m(\x1b[92m0\x1b[39m, \x1b[92m0\x1b[39m, \x1b[92m0\x1b[39m)\x1b[39m,\x1b[39m(\x1b[92m1\x1b[39m, \x1b[92m1\x1b[39m, \x1b[92m1\x1b[39m)" . "\x1b[0m",
- 'html' => '<span class="sql-reserved">INSERT</span>' . '<br/>' .
- '<span class="sql-reserved">INTO</span>' . '<br/>' .
- '&nbsp;&nbsp;&nbsp;&nbsp;foo' . '<br/>' .
+ 'html' => '<span class="sql-reserved">INSERT</span> <span class="sql-reserved">INTO</span> foo' . '<br/>' .
'<span class="sql-reserved">VALUES</span>(<span class="sql-number">0</span>, <span class="sql-number">0</span>, <span class="sql-number">0</span>),(<span class="sql-number">1</span>, <span class="sql-number">1</span>, <span class="sql-number">1</span>)',
),
'string as alias' => array(