File tree 4 files changed +23
-1
lines changed
4 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ Little tool meant to format a SQL query to be more readable.
2
2
3
3
Install the folder on a web server with PHP installed and call index.php in a browser.
4
4
5
- Current features :
5
+ Current features (see unit tests) :
6
6
- FROM and WHERE start on a new line
7
7
- 'SELECT', 'FROM' and 'WHERE' keywords are uppercased
8
8
Original file line number Diff line number Diff line change 2
2
3
3
class SqlFormatter
4
4
{
5
+ private $ _select ;
6
+ private $ _from ;
7
+ private $ _where ;
8
+
5
9
public function format ($ sql )
6
10
{
7
11
$ this ->_separateSelectAndFrom ($ sql );
8
12
$ this ->_separateFromAndWhere ();
13
+ $ this ->_formatFrom ();
9
14
return $ this ->_assemble ();
10
15
}
11
16
17
+ private function _formatFrom ()
18
+ {
19
+ $ this ->_from = str_ireplace (' left join ' , "\nLEFT JOIN " , $ this ->_from );
20
+ }
21
+
12
22
private function _assemble ()
13
23
{
14
24
$ result = $ this ->_select . "\n" . $ this ->_from ;
Original file line number Diff line number Diff line change @@ -41,4 +41,16 @@ public function keywordsAreUppercased()
41
41
$ actual = $ o ->format ($ sql );
42
42
$ this ->assertEquals ($ expected , $ actual );
43
43
}
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
+ }
44
56
}
You can’t perform that action at this time.
0 commit comments