summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Desportes <williamdes@wdes.fr>2019-03-12 18:58:00 +0100
committerGitHub <noreply@github.com>2019-03-12 18:58:00 +0100
commit8748a3d033c627f2dc88cf83e4afb6f124fb6a58 (patch)
tree657548b58221ac9c0eed78112363db3f74ec8747
parenta404e855c90692efea8e9e77004be56fc0d7c8e2 (diff)
parent1d8f85c695ad3777e403eefdcf0f59fcba207b78 (diff)
downloadsql-parser-8748a3d033c627f2dc88cf83e4afb6f124fb6a58.zip
sql-parser-8748a3d033c627f2dc88cf83e4afb6f124fb6a58.tar.gz
sql-parser-8748a3d033c627f2dc88cf83e4afb6f124fb6a58.tar.bz2
Merge pull request #233 from devenbansod/fix/pma/14995
fix: incorrect lastClauseIdx in Utils::getQuery
-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'
)
);
}