diff --git a/core/oql/expression.class.inc.php b/core/oql/expression.class.inc.php index ea797a2b5..aa53587fa 100644 --- a/core/oql/expression.class.inc.php +++ b/core/oql/expression.class.inc.php @@ -575,9 +575,15 @@ class BinaryExpression extends Expression case 'LIKE': $sType = 'like'; break; + case 'NOT LIKE': + $sType = 'notlike'; + break; case 'IN': $sType = 'in'; break; + case 'NOT IN': + $sType = 'notin'; + break; default: throw new Exception("Operator '$sOperator' not yet supported"); } @@ -642,11 +648,27 @@ class BinaryExpression extends Expression case 'like': $sEscaped = preg_quote($mRight, '/'); $sEscaped = str_replace(array('%', '_', '\\\\.*', '\\\\.'), array('.*', '.', '%', '_'), $sEscaped); - $result = (int) preg_match("/$sEscaped/i", $mLeft); + $pregRes = preg_match("/$sEscaped/i", $mLeft); + if ($pregRes === false) { + throw new Exception("Error in regular expression '$sEscaped'"); + } + $result = ($pregRes === 1); + break; + case 'notlike': + $sEscaped = preg_quote($mRight, '/'); + $sEscaped = str_replace(array('%', '_', '\\\\.*', '\\\\.'), array('.*', '.', '%', '_'), $sEscaped); + $pregRes = preg_match("/$sEscaped/i", $mLeft); + if ($pregRes === false) { + throw new Exception("Error in regular expression '$sEscaped'"); + } + $result = ($pregRes !== 1); break; case 'in': $result = in_array($mLeft, $mRight); break; + case 'notin': + $result = !in_array($mLeft, $mRight); + break; } return $result; } diff --git a/tests/php-unit-tests/unitary-tests/core/ExpressionEvaluateTest.php b/tests/php-unit-tests/unitary-tests/core/ExpressionEvaluateTest.php index 6b9847fe0..2c0951e4c 100644 --- a/tests/php-unit-tests/unitary-tests/core/ExpressionEvaluateTest.php +++ b/tests/php-unit-tests/unitary-tests/core/ExpressionEvaluateTest.php @@ -131,6 +131,8 @@ class ExpressionEvaluateTest extends ItopDataTestCase array("'the quick brown dog' LIKE '%QU_ICK%'", 0), array('"400 (km/h)" LIKE "400%"', 1), array('"400 (km/h)" LIKE "100%"', 0), + array('"400 (km/h)" NOT LIKE "400%"', 0), + array('"400 (km/h)" NOT LIKE "100%"', 1), array('"2020-06-12" > "2020-06-11"', 1), array('"2020-06-12" < "2020-06-11"', 0), array('" 2020-06-12" > "2020-06-11"', 0), // Leading spaces => a string @@ -142,7 +144,14 @@ class ExpressionEvaluateTest extends ItopDataTestCase array('"2020-06-12 00:00:00" = "2020-06-12"', 0), // IN operator + array("'a' IN ('a')", true), + array("'a' IN ('b')", false), array("'a' IN ('a', 'b')", true), + array("'z' IN ('a', 'b')", false), + array("'a' NOT IN ('a')", false), + array("'a' NOT IN ('b')", true), + array("'a' NOT IN ('a', 'b')", false), + array("'z' NOT IN ('a', 'b')", true), // Logical operators array('0 AND 0', 0),