Skip to content

Commit d845de5

Browse files
committed
LEFT JOINs start on a new line
1 parent 600228f commit d845de5

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Little tool meant to format a SQL query to be more readable.
22

33
Install the folder on a web server with PHP installed and call index.php in a browser.
44

5-
Current features :
5+
Current features (see unit tests):
66
- FROM and WHERE start on a new line
77
- 'SELECT', 'FROM' and 'WHERE' keywords are uppercased
88

sqlFormatter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
class SqlFormatter
44
{
5+
private $_select;
6+
private $_from;
7+
private $_where;
8+
59
public function format($sql)
610
{
711
$this->_separateSelectAndFrom($sql);
812
$this->_separateFromAndWhere();
13+
$this->_formatFrom();
914
return $this->_assemble();
1015
}
1116

17+
private function _formatFrom()
18+
{
19+
$this->_from = str_ireplace(' left join', "\nLEFT JOIN", $this->_from);
20+
}
21+
1222
private function _assemble()
1323
{
1424
$result = $this->_select . "\n" . $this->_from;

tests/.test.php.swp

-12 KB
Binary file not shown.

tests/test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,16 @@ public function keywordsAreUppercased()
4141
$actual = $o->format($sql);
4242
$this->assertEquals($expected, $actual);
4343
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function leftJoinStartsOnNewLine()
49+
{
50+
$sql = "SELECT a\nFROM t LEFT JOIN b ON t.a = b.c WHERE b = c";
51+
$expected = "SELECT a\nFROM t\nLEFT JOIN b ON t.a = b.c\nWHERE b = c";
52+
$o = new SqlFormatter();
53+
$actual = $o->format($sql);
54+
$this->assertEquals($expected, $actual);
55+
}
4456
}

0 commit comments

Comments
 (0)