N°5791 - Handle NOT IN and NOT LIKE too

This commit is contained in:
Romain Quetiez
2025-01-10 16:21:12 +01:00
parent ec324bb28e
commit 93dba0644d
2 changed files with 32 additions and 1 deletions

View File

@@ -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;
}

View File

@@ -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),