diff options
author | Dan Ungureanu <udan1107@gmail.com> | 2015-07-05 19:38:03 +0300 |
---|---|---|
committer | Dan Ungureanu <udan1107@gmail.com> | 2015-07-05 19:38:03 +0300 |
commit | 06c0a645e9d19b1f899c3883ae9b11e5c50bc31e (patch) | |
tree | e0b6b2402fc219357800517e8f6e7ead8a6b1b65 /tests/Utils/QueryTest.php | |
parent | b3eff80030f9bd6d90e65360eb89e18a1be298b2 (diff) | |
download | sql-parser-06c0a645e9d19b1f899c3883ae9b11e5c50bc31e.zip sql-parser-06c0a645e9d19b1f899c3883ae9b11e5c50bc31e.tar.gz sql-parser-06c0a645e9d19b1f899c3883ae9b11e5c50bc31e.tar.bz2 |
Improved condition parsing and query utilities.
Diffstat (limited to 'tests/Utils/QueryTest.php')
-rw-r--r-- | tests/Utils/QueryTest.php | 84 |
1 files changed, 80 insertions, 4 deletions
diff --git a/tests/Utils/QueryTest.php b/tests/Utils/QueryTest.php index 918c5b5..2cd3df7 100644 --- a/tests/Utils/QueryTest.php +++ b/tests/Utils/QueryTest.php @@ -251,6 +251,8 @@ class QueryTest extends TestCase public function testGetAll() { + $this->assertEquals(array(), Query::getAll('')); + $query = 'SELECT *, actor.actor_id, sakila2.film.* FROM sakila2.city, sakila2.film, actor'; $parser = new Parser($query); @@ -263,7 +265,7 @@ class QueryTest extends TestCase 'select_expr' => array('*'), 'select_tables' => array( array('actor', null), - array('film', 'sakila2') + array('film', 'sakila2'), ) ) ), @@ -281,7 +283,24 @@ class QueryTest extends TestCase 'select_expr' => array('*'), 'select_tables' => array( array('actor', 'sakila'), - array('film', null) + array('film', null), + ) + ) + ), + Query::getAll($query) + ); + + $query = 'SELECT a.actor_id FROM sakila.actor AS a, film'; + $parser = new Parser($query); + $this->assertEquals( + array_merge( + Query::getFlags($parser->statements[0], true), + array( + 'parser' => $parser, + 'statement' => $parser->statements[0], + 'select_expr' => array(), + 'select_tables' => array( + array('actor', 'sakila'), ) ) ), @@ -289,9 +308,24 @@ class QueryTest extends TestCase ); } - public function testGetAllEmpty() + public function testGetClause() { - $this->assertEquals(array(), Query::getAll('')); + $parser = new Parser( + 'SELECT c.city_id, c.country_id ' . + 'FROM `city` ' . + 'WHERE city_id < 1 ' . + 'ORDER BY city_id ASC ' . + 'LIMIT 0, 1 ' . + 'INTO OUTFILE "/dev/null"' + ); + $this->assertEquals( + 'WHERE city_id < 1 ORDER BY city_id ASC', + Query::getClause( + $parser->statements[0], + $parser->list, + 'LIMIT', 'FROM' + ) + ); } public function testReplaceClause() @@ -321,4 +355,46 @@ class QueryTest extends TestCase ) ); } + + public function testRepalceClauses() + { + $this->assertEquals('', Query::replaceClauses(null, null, array())); + + $parser = new Parser('SELECT *, (SELECT 1) FROM film LIMIT 0, 10;'); + $this->assertEquals( + 'SELECT *, (SELECT 1) FROM film WHERE film_id > 0 LIMIT 0, 10', + Query::replaceClauses( + $parser->statements[0], + $parser->list, + array( + array('WHERE', 'WHERE film_id > 0'), + ) + ) + ); + + $parser = new Parser( + 'SELECT c.city_id, c.country_id ' . + 'FROM `city` ' . + 'WHERE city_id < 1 ' . + 'ORDER BY city_id ASC ' . + 'LIMIT 0, 1 ' . + 'INTO OUTFILE "/dev/null"' + ); + $this->assertEquals( + 'SELECT c.city_id, c.country_id ' . + 'FROM city AS c ' . + 'ORDER BY city_id ASC ' . + 'LIMIT 0, 10 ' . + 'INTO OUTFILE "/dev/null"', + Query::replaceClauses( + $parser->statements[0], + $parser->list, + array( + array('FROM', 'FROM city AS c'), + array('WHERE', ''), + array('LIMIT', 'LIMIT 0, 10'), + ) + ) + ); + } } |