diff options
author | Deven Bansod <devenbansod@users.noreply.github.com> | 2019-02-26 21:01:50 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-26 21:01:50 +0530 |
commit | b3e7bfc2f054fe1f8a8d33d3cc9275f1178ec341 (patch) | |
tree | 1cb979652150dc9caf4bd1732fe12062d77ca8dc | |
parent | 3b531b0d936c7e58cbc74c0ec55c70ec5918a533 (diff) | |
parent | ef95576797473973d159e662303cbc3df048302d (diff) | |
download | sql-parser-b3e7bfc2f054fe1f8a8d33d3cc9275f1178ec341.zip sql-parser-b3e7bfc2f054fe1f8a8d33d3cc9275f1178ec341.tar.gz sql-parser-b3e7bfc2f054fe1f8a8d33d3cc9275f1178ec341.tar.bz2 |
Merge pull request #228 from mobtitude/fix-whitespaces
Fix redundant whitespaces - Issue #227
-rw-r--r-- | src/Statement.php | 4 | ||||
-rw-r--r-- | src/Statements/InsertStatement.php | 4 | ||||
-rw-r--r-- | src/Statements/ReplaceStatement.php | 3 | ||||
-rw-r--r-- | tests/Builder/CreateStatementTest.php | 2 | ||||
-rw-r--r-- | tests/Builder/InsertStatementTest.php | 21 | ||||
-rw-r--r-- | tests/Builder/ReplaceStatementTest.php | 18 | ||||
-rw-r--r-- | tests/Builder/SelectStatementTest.php | 22 | ||||
-rw-r--r-- | tests/Builder/StatementTest.php | 2 | ||||
-rw-r--r-- | tests/Builder/TransactionStatementTest.php | 4 |
9 files changed, 52 insertions, 28 deletions
diff --git a/src/Statement.php b/src/Statement.php index 41cf3e6..c88722e 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -173,12 +173,12 @@ abstract class Statement // 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); } } diff --git a/src/Statements/InsertStatement.php b/src/Statements/InsertStatement.php index 4230033..265929d 100644 --- a/src/Statements/InsertStatement.php +++ b/src/Statements/InsertStatement.php @@ -110,8 +110,8 @@ class InsertStatement extends Statement */ public function build() { - $ret = 'INSERT ' . $this->options - . ' INTO ' . $this->into; + $ret = 'INSERT ' . $this->options; + $ret = trim($ret) . ' INTO ' . $this->into; if (! is_null($this->values) && count($this->values) > 0) { $ret .= ' VALUES ' . Array2d::build($this->values); diff --git a/src/Statements/ReplaceStatement.php b/src/Statements/ReplaceStatement.php index c5903c0..5bb5e12 100644 --- a/src/Statements/ReplaceStatement.php +++ b/src/Statements/ReplaceStatement.php @@ -87,7 +87,8 @@ class ReplaceStatement extends Statement */ public function build() { - $ret = 'REPLACE ' . $this->options . ' INTO ' . $this->into; + $ret = 'REPLACE ' . $this->options; + $ret = trim($ret) . ' INTO ' . $this->into; if (! is_null($this->values) && count($this->values) > 0) { $ret .= ' VALUES ' . Array2d::build($this->values); diff --git a/tests/Builder/CreateStatementTest.php b/tests/Builder/CreateStatementTest.php index 328d381..b2fefb2 100644 --- a/tests/Builder/CreateStatementTest.php +++ b/tests/Builder/CreateStatementTest.php @@ -334,7 +334,7 @@ EOT '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() ); } diff --git a/tests/Builder/InsertStatementTest.php b/tests/Builder/InsertStatementTest.php index a2b2ef8..cf46143 100644 --- a/tests/Builder/InsertStatementTest.php +++ b/tests/Builder/InsertStatementTest.php @@ -16,7 +16,7 @@ class InsertStatementTest extends TestCase ); $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() ); @@ -27,7 +27,7 @@ class InsertStatementTest extends TestCase ); $stmt = $parser->statements[0]; $this->assertEquals( - 'INSERT INTO tbl(`order`) VALUES (1)', + 'INSERT INTO tbl(`order`) VALUES (1)', $stmt->build() ); @@ -38,7 +38,7 @@ class InsertStatementTest extends TestCase ); $stmt = $parser->statements[0]; $this->assertEquals( - 'INSERT INTO tbl SET FOO = 1', + 'INSERT INTO tbl SET FOO = 1', $stmt->build() ); @@ -49,7 +49,7 @@ class InsertStatementTest extends TestCase ); $stmt = $parser->statements[0]; $this->assertEquals( - 'INSERT INTO tbl SELECT * FROM bar ', + 'INSERT INTO tbl SELECT * FROM bar', $stmt->build() ); @@ -60,7 +60,18 @@ class InsertStatementTest extends TestCase ); $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() + ); + + /* Assertion 6 */ + /* INSERT [OPTIONS] INTO ... */ + $parser = new Parser( + 'INSERT DELAYED IGNORE INTO tbl SELECT * FROM bar' + ); + $stmt = $parser->statements[0]; + $this->assertEquals( + 'INSERT DELAYED IGNORE INTO tbl SELECT * FROM bar', $stmt->build() ); } diff --git a/tests/Builder/ReplaceStatementTest.php b/tests/Builder/ReplaceStatementTest.php index 2610ac0..ee5b95a 100644 --- a/tests/Builder/ReplaceStatementTest.php +++ b/tests/Builder/ReplaceStatementTest.php @@ -15,7 +15,7 @@ class ReplaceStatementTest extends TestCase ); $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() ); } @@ -27,7 +27,7 @@ class ReplaceStatementTest extends TestCase ); $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() ); } @@ -39,7 +39,19 @@ class ReplaceStatementTest extends TestCase ); $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() + ); + } + + public function testBuilderSelectDelayed() + { + $parser = new Parser( + 'REPLACE DELAYED INTO tbl(col1, col2, col3) SELECT col1, col2, col3 FROM tbl2' + ); + $stmt = $parser->statements[0]; + $this->assertEquals( + 'REPLACE DELAYED INTO tbl(`col1`, `col2`, `col3`) SELECT col1, col2, col3 FROM tbl2', $stmt->build() ); } diff --git a/tests/Builder/SelectStatementTest.php b/tests/Builder/SelectStatementTest.php index eaf80c3..5d9836f 100644 --- a/tests/Builder/SelectStatementTest.php +++ b/tests/Builder/SelectStatementTest.php @@ -17,8 +17,8 @@ class SelectStatementTest extends TestCase $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() ); } @@ -29,7 +29,7 @@ class SelectStatementTest extends TestCase $stmt = $parser->statements[0]; $this->assertEquals( - 'SELECT 1 UNION SELECT 2 ', + 'SELECT 1 UNION SELECT 2', $stmt->build() ); } @@ -45,10 +45,10 @@ class SelectStatementTest extends TestCase $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() ); } @@ -56,7 +56,7 @@ class SelectStatementTest extends TestCase 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]; @@ -66,7 +66,7 @@ class SelectStatementTest extends TestCase ); /* 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]; @@ -79,10 +79,10 @@ class SelectStatementTest extends TestCase 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]; @@ -94,7 +94,7 @@ class SelectStatementTest extends TestCase 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]; @@ -106,7 +106,7 @@ class SelectStatementTest extends TestCase 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]; diff --git a/tests/Builder/StatementTest.php b/tests/Builder/StatementTest.php index da16602..b75b5d5 100644 --- a/tests/Builder/StatementTest.php +++ b/tests/Builder/StatementTest.php @@ -34,7 +34,7 @@ class StatementTest extends TestCase '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 ); } diff --git a/tests/Builder/TransactionStatementTest.php b/tests/Builder/TransactionStatementTest.php index 4bdfe70..5cd28c6 100644 --- a/tests/Builder/TransactionStatementTest.php +++ b/tests/Builder/TransactionStatementTest.php @@ -20,8 +20,8 @@ class TransactionStatementTest extends TestCase $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() ); |