Skip to content

Commit 457d094

Browse files
committed
Changed implementation of build() methods to generate more consistent SQL.
This commit fixes implementation of `build()` methods to produce more consistent SQL code without redundant spaces.
1 parent dc4c38a commit 457d094

9 files changed

+27
-27
lines changed

src/Statement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ public function build()
173173

174174
// Checking if the name of the clause should be added.
175175
if ($type & 2) {
176-
$query .= $name . ' ';
176+
$query = trim($query) . ' ' . $name;
177177
}
178178

179179
// Checking if the result of the builder should be added.
180180
if ($type & 1) {
181-
$query .= $class::build($this->$field) . ' ';
181+
$query = trim($query) . ' ' . $class::build($this->$field);
182182
}
183183
}
184184

src/Statements/InsertStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class InsertStatement extends Statement
111111
public function build()
112112
{
113113
$ret = 'INSERT ' . $this->options
114-
. ' INTO ' . $this->into;
114+
. 'INTO ' . $this->into;
115115

116116
if (! is_null($this->values) && count($this->values) > 0) {
117117
$ret .= ' VALUES ' . Array2d::build($this->values);

src/Statements/ReplaceStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ReplaceStatement extends Statement
8787
*/
8888
public function build()
8989
{
90-
$ret = 'REPLACE ' . $this->options . ' INTO ' . $this->into;
90+
$ret = 'REPLACE ' . $this->options . 'INTO ' . $this->into;
9191

9292
if (! is_null($this->values) && count($this->values) > 0) {
9393
$ret .= ' VALUES ' . Array2d::build($this->values);

tests/Builder/CreateStatementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public function testBuildSelect()
334334
'CREATE TABLE new_tbl SELECT * FROM orig_tbl'
335335
);
336336
$this->assertEquals(
337-
'CREATE TABLE new_tbl SELECT * FROM orig_tbl ',
337+
'CREATE TABLE new_tbl SELECT * FROM orig_tbl',
338338
$parser->statements[0]->build()
339339
);
340340
}

tests/Builder/InsertStatementTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testBuilder()
1616
);
1717
$stmt = $parser->statements[0];
1818
$this->assertEquals(
19-
'INSERT INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
19+
'INSERT INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
2020
$stmt->build()
2121
);
2222

@@ -27,7 +27,7 @@ public function testBuilder()
2727
);
2828
$stmt = $parser->statements[0];
2929
$this->assertEquals(
30-
'INSERT INTO tbl(`order`) VALUES (1)',
30+
'INSERT INTO tbl(`order`) VALUES (1)',
3131
$stmt->build()
3232
);
3333

@@ -38,7 +38,7 @@ public function testBuilder()
3838
);
3939
$stmt = $parser->statements[0];
4040
$this->assertEquals(
41-
'INSERT INTO tbl SET FOO = 1',
41+
'INSERT INTO tbl SET FOO = 1',
4242
$stmt->build()
4343
);
4444

@@ -49,7 +49,7 @@ public function testBuilder()
4949
);
5050
$stmt = $parser->statements[0];
5151
$this->assertEquals(
52-
'INSERT INTO tbl SELECT * FROM bar ',
52+
'INSERT INTO tbl SELECT * FROM bar',
5353
$stmt->build()
5454
);
5555

@@ -60,7 +60,7 @@ public function testBuilder()
6060
);
6161
$stmt = $parser->statements[0];
6262
$this->assertEquals(
63-
'INSERT INTO tbl SELECT * FROM bar ON DUPLICATE KEY UPDATE baz = 1',
63+
'INSERT INTO tbl SELECT * FROM bar ON DUPLICATE KEY UPDATE baz = 1',
6464
$stmt->build()
6565
);
6666
}

tests/Builder/ReplaceStatementTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testBuilder()
1515
);
1616
$stmt = $parser->statements[0];
1717
$this->assertEquals(
18-
'REPLACE INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
18+
'REPLACE INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
1919
$stmt->build()
2020
);
2121
}
@@ -27,7 +27,7 @@ public function testBuilderSet()
2727
);
2828
$stmt = $parser->statements[0];
2929
$this->assertEquals(
30-
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SET col1 = 1, col2 = "str", col3 = 3.14',
30+
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SET col1 = 1, col2 = "str", col3 = 3.14',
3131
$stmt->build()
3232
);
3333
}
@@ -39,7 +39,7 @@ public function testBuilderSelect()
3939
);
4040
$stmt = $parser->statements[0];
4141
$this->assertEquals(
42-
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SELECT col1, col2, col3 FROM tbl2 ',
42+
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SELECT col1, col2, col3 FROM tbl2',
4343
$stmt->build()
4444
);
4545
}

tests/Builder/SelectStatementTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function testBuilder()
1717
$stmt = $parser->statements[0];
1818

1919
$this->assertEquals(
20-
'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) '
21-
. 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c) ',
20+
'SELECT * FROM t1 LEFT JOIN (t2, t3, t4) '
21+
. 'ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)',
2222
$stmt->build()
2323
);
2424
}
@@ -29,7 +29,7 @@ public function testBuilderUnion()
2929
$stmt = $parser->statements[0];
3030

3131
$this->assertEquals(
32-
'SELECT 1 UNION SELECT 2 ',
32+
'SELECT 1 UNION SELECT 2',
3333
$stmt->build()
3434
);
3535
}
@@ -45,18 +45,18 @@ public function testBuilderAlias()
4545
$stmt = $parser->statements[0];
4646

4747
$this->assertEquals(
48-
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
48+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
4949
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
5050
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
51-
. 'ORDER BY scb.id DESC LIMIT 0, 300 ',
51+
. 'ORDER BY scb.id DESC LIMIT 0, 300',
5252
$stmt->build()
5353
);
5454
}
5555

5656
public function testBuilderEndOptions()
5757
{
5858
/* Assertion 1 */
59-
$query = 'SELECT pid, name2 FROM tablename WHERE pid = 20 FOR UPDATE ';
59+
$query = 'SELECT pid, name2 FROM tablename WHERE pid = 20 FOR UPDATE';
6060
$parser = new Parser($query);
6161
$stmt = $parser->statements[0];
6262

@@ -66,7 +66,7 @@ public function testBuilderEndOptions()
6666
);
6767

6868
/* Assertion 2 */
69-
$query = 'SELECT pid, name2 FROM tablename WHERE pid = 20 LOCK IN SHARE MODE ';
69+
$query = 'SELECT pid, name2 FROM tablename WHERE pid = 20 LOCK IN SHARE MODE';
7070
$parser = new Parser($query);
7171
$stmt = $parser->statements[0];
7272

@@ -79,10 +79,10 @@ public function testBuilderEndOptions()
7979
public function testBuilderIntoOptions()
8080
{
8181
/* Assertion 1 */
82-
$query = 'SELECT a, b, a+b INTO OUTFILE "/tmp/result.txt"'
82+
$query = 'SELECT a, b, a+b INTO OUTFILE "/tmp/result.txt"'
8383
. ' COLUMNS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\''
8484
. ' LINES TERMINATED BY \'\n\''
85-
. ' FROM test_table ';
85+
. ' FROM test_table';
8686
$parser = new Parser($query);
8787
$stmt = $parser->statements[0];
8888

@@ -94,7 +94,7 @@ public function testBuilderIntoOptions()
9494

9595
public function testBuildGroupBy()
9696
{
97-
$query = 'SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ';
97+
$query = 'SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country';
9898
$parser = new Parser($query);
9999
$stmt = $parser->statements[0];
100100

@@ -106,7 +106,7 @@ public function testBuildGroupBy()
106106

107107
public function testBuildIndexHint()
108108
{
109-
$query = 'SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0 ';
109+
$query = 'SELECT * FROM address FORCE INDEX (idx_fk_city_id) IGNORE KEY FOR GROUP BY (a, b,c) WHERE city_id<0';
110110
$parser = new Parser($query);
111111
$stmt = $parser->statements[0];
112112

tests/Builder/StatementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testBuilder()
3434
'SELECT DISTINCT `sakila`.`film`.`film_id` AS `fid`, COUNT(film_id) ' .
3535
'FROM `film`, `actor` ' .
3636
'WHERE film_id > 10 OR actor.age > 25 ' .
37-
'LIMIT 10, 1 ',
37+
'LIMIT 10, 1',
3838
(string) $stmt
3939
);
4040
}

tests/Builder/TransactionStatementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function testBuilder()
2020

2121
$this->assertEquals(
2222
'START TRANSACTION;' .
23-
'SELECT @A:=SUM(salary) FROM table1 WHERE type=1 ;' .
24-
'UPDATE table2 SET summary = @A WHERE type=1 ;' .
23+
'SELECT @A:=SUM(salary) FROM table1 WHERE type=1;' .
24+
'UPDATE table2 SET summary = @A WHERE type=1;' .
2525
'COMMIT',
2626
$stmt->build()
2727
);

0 commit comments

Comments
 (0)