diff options
author | Deven Bansod <devenbansod.bits@gmail.com> | 2019-03-12 22:38:13 +0530 |
---|---|---|
committer | Deven Bansod <devenbansod.bits@gmail.com> | 2019-03-12 22:54:40 +0530 |
commit | 1d8f85c695ad3777e403eefdcf0f59fcba207b78 (patch) | |
tree | 657548b58221ac9c0eed78112363db3f74ec8747 | |
parent | a404e855c90692efea8e9e77004be56fc0d7c8e2 (diff) | |
download | sql-parser-1d8f85c695ad3777e403eefdcf0f59fcba207b78.zip sql-parser-1d8f85c695ad3777e403eefdcf0f59fcba207b78.tar.gz sql-parser-1d8f85c695ad3777e403eefdcf0f59fcba207b78.tar.bz2 |
fix: incorrect lastClauseIdx in Util::getQuery
-rw-r--r-- | src/Utils/Query.php | 4 | ||||
-rw-r--r-- | tests/Utils/QueryTest.php | 87 |
2 files changed, 88 insertions, 3 deletions
diff --git a/src/Utils/Query.php b/src/Utils/Query.php index 662404b..7b14ded 100644 --- a/src/Utils/Query.php +++ b/src/Utils/Query.php @@ -594,7 +594,7 @@ class Query $clauseIdx = $clauses[$clauseType]; $firstClauseIdx = $clauseIdx; - $lastClauseIdx = $clauseIdx + 1; + $lastClauseIdx = $clauseIdx; // Determining the behavior of this function. if ($type === -1) { @@ -603,7 +603,7 @@ class Query } elseif ($type === 1) { $firstClauseIdx = $clauseIdx + 1; $lastClauseIdx = 10000; // Something big enough. - } elseif (is_string($type)) { + } elseif (is_string($type) && isset($clauses[$type])) { if ($clauses[$type] > $clauseIdx) { $firstClauseIdx = $clauseIdx + 1; $lastClauseIdx = $clauses[$type] - 1; diff --git a/tests/Utils/QueryTest.php b/tests/Utils/QueryTest.php index 578a8c8..5c0cc55 100644 --- a/tests/Utils/QueryTest.php +++ b/tests/Utils/QueryTest.php @@ -452,6 +452,7 @@ class QueryTest extends TestCase public function testGetClause() { + /* Assertion 1 */ $parser = new Parser( 'SELECT c.city_id, c.country_id ' . 'FROM `city` ' . @@ -461,12 +462,96 @@ class QueryTest extends TestCase 'INTO OUTFILE "/dev/null"' ); $this->assertEquals( + '0, 1 INTO OUTFILE "/dev/null"', + Query::getClause( + $parser->statements[0], + $parser->list, + 'LIMIT', + 0 + ) + ); + // Assert it returns all clauses between FROM and LIMIT + $this->assertEquals( 'WHERE city_id < 1 ORDER BY city_id ASC', Query::getClause( $parser->statements[0], $parser->list, + 'FROM', + 'LIMIT' + ) + ); + // Assert it returns all clauses between SELECT and LIMIT + $this->assertEquals( + 'FROM `city` WHERE city_id < 1 ORDER BY city_id ASC', + Query::getClause( + $parser->statements[0], + $parser->list, 'LIMIT', - 'FROM' + 'SELECT' + ) + ); + + /* Assertion 2 */ + $parser = new Parser( + 'DELETE FROM `renewal` ' . + 'WHERE number = "1DB" AND actionDate <= CURRENT_DATE() ' . + 'ORDER BY id ASC ' . + 'LIMIT 1' + ); + $this->assertEquals( + 'number = "1DB" AND actionDate <= CURRENT_DATE()', + Query::getClause( + $parser->statements[0], + $parser->list, + 'WHERE' + ) + ); + $this->assertEquals( + '1', + Query::getClause( + $parser->statements[0], + $parser->list, + 'LIMIT' + ) + ); + $this->assertEquals( + 'id ASC', + Query::getClause( + $parser->statements[0], + $parser->list, + 'ORDER BY' + ) + ); + + /* Assertion 3 */ + $parser = new Parser( + 'UPDATE `renewal` SET `some_column` = 1 ' . + 'WHERE number = "1DB" AND actionDate <= CURRENT_DATE() ' . + 'ORDER BY id ASC ' . + 'LIMIT 1' + ); + $this->assertEquals( + 'number = "1DB" AND actionDate <= CURRENT_DATE()', + Query::getClause( + $parser->statements[0], + $parser->list, + 'WHERE' + ) + ); + $this->assertEquals( + '1', + Query::getClause( + $parser->statements[0], + $parser->list, + 'LIMIT' + ) + ); + $this->assertEquals( + 'id ASC', + Query::getClause( + $parser->statements[0], + $parser->list, + 'ORDER BY' ) ); } |