Skip to content

Commit

Permalink
Changed implementation of build() methods to generate more consistent…
Browse files Browse the repository at this point in the history
… SQL.

This commit fixes implementation of `build()` methods to produce more consistent SQL code without redundant spaces.
  • Loading branch information
pszalko committed Feb 15, 2019
1 parent dc4c38a commit 457d094
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ public function build()

// Checking if the name of the clause should be added.
if ($type & 2) {
$query .= $name . ' ';
$query = trim($query) . ' ' . $name;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/Statements/InsertStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class InsertStatement extends Statement
public function build()
{
$ret = 'INSERT ' . $this->options
. ' INTO ' . $this->into;
. 'INTO ' . $this->into;

if (! is_null($this->values) && count($this->values) > 0) {
$ret .= ' VALUES ' . Array2d::build($this->values);
Expand Down
2 changes: 1 addition & 1 deletion src/Statements/ReplaceStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ReplaceStatement extends Statement
*/
public function build()
{
$ret = 'REPLACE ' . $this->options . ' INTO ' . $this->into;
$ret = 'REPLACE ' . $this->options . 'INTO ' . $this->into;

if (! is_null($this->values) && count($this->values) > 0) {
$ret .= ' VALUES ' . Array2d::build($this->values);
Expand Down
2 changes: 1 addition & 1 deletion tests/Builder/CreateStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function testBuildSelect()
'CREATE TABLE new_tbl SELECT * FROM orig_tbl'
);
$this->assertEquals(
'CREATE TABLE new_tbl SELECT * FROM orig_tbl ',
'CREATE TABLE new_tbl SELECT * FROM orig_tbl',
$parser->statements[0]->build()
);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Builder/InsertStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testBuilder()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
'INSERT INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
$stmt->build()
);

Expand All @@ -27,7 +27,7 @@ public function testBuilder()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl(`order`) VALUES (1)',
'INSERT INTO tbl(`order`) VALUES (1)',
$stmt->build()
);

Expand All @@ -38,7 +38,7 @@ public function testBuilder()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl SET FOO = 1',
'INSERT INTO tbl SET FOO = 1',
$stmt->build()
);

Expand All @@ -49,7 +49,7 @@ public function testBuilder()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl SELECT * FROM bar ',
'INSERT INTO tbl SELECT * FROM bar',
$stmt->build()
);

Expand All @@ -60,7 +60,7 @@ public function testBuilder()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'INSERT INTO tbl SELECT * FROM bar ON DUPLICATE KEY UPDATE baz = 1',
'INSERT INTO tbl SELECT * FROM bar ON DUPLICATE KEY UPDATE baz = 1',
$stmt->build()
);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Builder/ReplaceStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testBuilder()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'REPLACE INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
'REPLACE INTO tbl(`col1`, `col2`, `col3`) VALUES (1, "str", 3.14)',
$stmt->build()
);
}
Expand All @@ -27,7 +27,7 @@ public function testBuilderSet()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SET col1 = 1, col2 = "str", col3 = 3.14',
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SET col1 = 1, col2 = "str", col3 = 3.14',
$stmt->build()
);
}
Expand All @@ -39,7 +39,7 @@ public function testBuilderSelect()
);
$stmt = $parser->statements[0];
$this->assertEquals(
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SELECT col1, col2, col3 FROM tbl2 ',
'REPLACE INTO tbl(`col1`, `col2`, `col3`) SELECT col1, col2, col3 FROM tbl2',
$stmt->build()
);
}
Expand Down
22 changes: 11 additions & 11 deletions tests/Builder/SelectStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function testBuilder()
$stmt = $parser->statements[0];

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

$this->assertEquals(
'SELECT 1 UNION SELECT 2 ',
'SELECT 1 UNION SELECT 2',
$stmt->build()
);
}
Expand All @@ -45,18 +45,18 @@ public function testBuilderAlias()
$stmt = $parser->statements[0];

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

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

Expand All @@ -66,7 +66,7 @@ public function testBuilderEndOptions()
);

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

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

Expand All @@ -94,7 +94,7 @@ public function testBuilderIntoOptions()

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

Expand All @@ -106,7 +106,7 @@ public function testBuildGroupBy()

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Builder/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testBuilder()
'SELECT DISTINCT `sakila`.`film`.`film_id` AS `fid`, COUNT(film_id) ' .
'FROM `film`, `actor` ' .
'WHERE film_id > 10 OR actor.age > 25 ' .
'LIMIT 10, 1 ',
'LIMIT 10, 1',
(string) $stmt
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Builder/TransactionStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function testBuilder()

$this->assertEquals(
'START TRANSACTION;' .
'SELECT @A:=SUM(salary) FROM table1 WHERE type=1 ;' .
'UPDATE table2 SET summary = @A WHERE type=1 ;' .
'SELECT @A:=SUM(salary) FROM table1 WHERE type=1;' .
'UPDATE table2 SET summary = @A WHERE type=1;' .
'COMMIT',
$stmt->build()
);
Expand Down

0 comments on commit 457d094

Please sign in to comment.