From 44dfc187bc5571e21826d8f01896d54f6be94c90 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 27 Apr 2020 17:48:11 +0200 Subject: [PATCH 1/2] Not Like: add failing test case (unimplemented) --- tests/Mouf/Database/MagicQueryTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Mouf/Database/MagicQueryTest.php b/tests/Mouf/Database/MagicQueryTest.php index ab49d05..dc2e737 100644 --- a/tests/Mouf/Database/MagicQueryTest.php +++ b/tests/Mouf/Database/MagicQueryTest.php @@ -85,6 +85,9 @@ public function testStandardSelect() $sql = 'SELECT * FROM users WHERE status not in (:status)'; $this->assertEquals("SELECT * FROM users WHERE status NOT IN ('2','4')", self::simplifySql($magicQuery->build($sql, ['status' => [2, 4]]))); + $sql = 'SELECT * FROM users WHERE email not like :email'; + $this->assertEquals("SELECT * FROM users WHERE email NOT LIKE '%@example.com'", self::simplifySql($magicQuery->build($sql, ['email' => '%@example.com']))); + $sql = 'SELECT * FROM myTable where someField BETWEEN :value1 AND :value2'; $this->assertEquals("SELECT * FROM myTable WHERE someField BETWEEN '2' AND '4'", self::simplifySql($magicQuery->build($sql, ['value1' => 2, 'value2' => 4]))); $this->assertEquals("SELECT * FROM myTable WHERE someField >= '2'", self::simplifySql($magicQuery->build($sql, ['value1' => 2]))); From 894563230f8afe62cc4b6c30a89c433b50aed1d9 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 27 Apr 2020 17:50:14 +0200 Subject: [PATCH 2/2] Not Like: Implementation --- src/SQLParser/Node/NodeFactory.php | 9 ++++++++- src/SQLParser/Node/NotLike.php | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/SQLParser/Node/NotLike.php diff --git a/src/SQLParser/Node/NodeFactory.php b/src/SQLParser/Node/NodeFactory.php index f69b327..423049d 100644 --- a/src/SQLParser/Node/NodeFactory.php +++ b/src/SQLParser/Node/NodeFactory.php @@ -497,7 +497,7 @@ public static function mapArrayToNodeObjectList(array $items) array('<<', '>>'), array('&'), array('|'), - array('=' /*(comparison)*/, '<=>', '>=', '>', '<=', '<', '<>', '!=', 'IS', 'LIKE', 'REGEXP', 'IN', 'IS NOT', 'NOT IN'), + array('=' /*(comparison)*/, '<=>', '>=', '>', '<=', '<', '<>', '!=', 'IS', 'LIKE', 'REGEXP', 'IN', 'IS NOT', 'NOT IN', 'NOT LIKE'), array('AND_FROM_BETWEEN'), array('THEN'), array('WHEN'), @@ -520,6 +520,7 @@ public static function mapArrayToNodeObjectList(array $items) 'IS' => 'SQLParser\Node\Is', 'IS NOT' => 'SQLParser\Node\IsNot', 'LIKE' => 'SQLParser\Node\Like', + 'NOT LIKE' => 'SQLParser\Node\NotLike', 'REGEXP' => 'SQLParser\Node\Regexp', 'IN' => 'SQLParser\Node\In', 'NOT IN' => 'SQLParser\Node\NotIn', @@ -578,6 +579,12 @@ public static function simplify($nodes) $notIn->setValue('NOT IN'); $newNodes[] = $notIn; ++$i; + } elseif ($node instanceof Operator && isset($nodes[$i + 1]) && $nodes[$i + 1] instanceof Operator + && strtoupper($node->getValue()) == 'NOT' && strtoupper($nodes[$i + 1]->getValue()) == 'LIKE') { + $notLike = new Operator(); + $notLike->setValue('NOT LIKE'); + $newNodes[] = $notLike; + ++$i; } else { $newNodes[] = $node; } diff --git a/src/SQLParser/Node/NotLike.php b/src/SQLParser/Node/NotLike.php new file mode 100644 index 0000000..5258399 --- /dev/null +++ b/src/SQLParser/Node/NotLike.php @@ -0,0 +1,21 @@ + + */ +class NotLike extends AbstractTwoOperandsOperator +{ + /** + * Returns the symbol for this operator. + * + * @return string + */ + protected function getOperatorSymbol() + { + return 'NOT LIKE'; + } +}