summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDeven Bansod <devenbansod.bits@gmail.com>2019-03-13 11:04:24 +0530
committerDeven Bansod <devenbansod.bits@gmail.com>2019-03-13 11:04:24 +0530
commit24b4e009d5926a55030e7e1e6536ac462e74843f (patch)
treeeba2e7e3f43086bdb57ac3c7234e34c3e892c34d
parent43f3802923b22da9b52c61796eabe13b2d2629a0 (diff)
parent8748a3d033c627f2dc88cf83e4afb6f124fb6a58 (diff)
downloadsql-parser-24b4e009d5926a55030e7e1e6536ac462e74843f.zip
sql-parser-24b4e009d5926a55030e7e1e6536ac462e74843f.tar.gz
sql-parser-24b4e009d5926a55030e7e1e6536ac462e74843f.tar.bz2
Merge branch 'master' of https://github.com/phpmyadmin/sql-parser
-rw-r--r--src/Utils/Query.php4
-rw-r--r--tests/Utils/QueryTest.php87
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'
)
);
}