Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/SQLParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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',
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions src/SQLParser/Node/NotLike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace SQLParser\Node;

/**
* This class represents an NOT LIKE operation in an SQL expression.
*
* @author David Négrier <[email protected]>
*/
class NotLike extends AbstractTwoOperandsOperator
{
/**
* Returns the symbol for this operator.
*
* @return string
*/
protected function getOperatorSymbol()
{
return 'NOT LIKE';
}
}
3 changes: 3 additions & 0 deletions tests/Mouf/Database/MagicQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])));
Expand Down