Skip to content

Commit 93167f3

Browse files
committed
Improved format of INSERT queries.
Signed-off-by: Dan Ungureanu <udan1107@gmail.com>
1 parent 867a928 commit 93167f3

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/Utils/Formatter.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ class Formatter
2828
*/
2929
public $options;
3030

31+
/**
32+
* Clauses that are usually short.
33+
*
34+
* These clauses share the line with the next clause.
35+
*
36+
* E.g. if INSERT was not here, the formatter would produce:
37+
*
38+
* INSERT
39+
* INTO foo
40+
* VALUES(0, 0, 0),(1, 1, 1);
41+
*
42+
* Instead of:
43+
*
44+
* INSERT INTO foo
45+
* VALUES(0, 0, 0),(1, 1, 1)
46+
*
47+
* @var array
48+
*/
49+
public static $SHORT_CLAUSES = array(
50+
'INSERT' => true,
51+
);
52+
3153
/**
3254
* Clauses that must be inlined.
3355
*
@@ -37,6 +59,7 @@ class Formatter
3759
*/
3860
public static $INLINE_CLAUSES = array(
3961
'CREATE' => true,
62+
'INTO' => true,
4063
'LIMIT' => true,
4164
'PARTITION BY' => true,
4265
'PARTITION' => true,
@@ -380,7 +403,7 @@ public function formatList($list)
380403

381404
// Checking if this clause ended.
382405
if ($tmp = static::isClause($curr)) {
383-
if ($tmp == 2 || $this->options['clause_newline']) {
406+
if (($tmp == 2 || $this->options['clause_newline']) && empty(self::$SHORT_CLAUSES[$lastClause])) {
384407
$lineEnded = true;
385408
if ($this->options['parts_newline'] && $indent > 0) {
386409
--$indent;

tests/Utils/FormatterTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,17 +430,11 @@ public function formatQueries()
430430
),
431431
'insert' => array(
432432
'query' => 'insert into foo values (0, 0, 0), (1, 1, 1)',
433-
'text' => 'INSERT' . "\n" .
434-
'INTO' . "\n" .
435-
' foo' . "\n" .
433+
'text' => 'INSERT INTO foo' . "\n" .
436434
'VALUES(0, 0, 0),(1, 1, 1)',
437-
'cli' => "\x1b[35mINSERT" . "\n" .
438-
"\x1b[35mINTO" . "\n" .
439-
" \x1b[39mfoo" . "\n" .
435+
'cli' => "\x1b[35mINSERT \x1b[35mINTO \x1b[39mfoo" . "\n" .
440436
"\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",
441-
'html' => '<span class="sql-reserved">INSERT</span>' . '<br/>' .
442-
'<span class="sql-reserved">INTO</span>' . '<br/>' .
443-
'&nbsp;&nbsp;&nbsp;&nbsp;foo' . '<br/>' .
437+
'html' => '<span class="sql-reserved">INSERT</span> <span class="sql-reserved">INTO</span> foo' . '<br/>' .
444438
'<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>)',
445439
),
446440
'string as alias' => array(

0 commit comments

Comments
 (0)