Skip to content

Commit 696de1c

Browse files
authored
Merge pull request #59 from vimeo/support-not-statements
fix the negation evaluations in fake sql
2 parents 839b2fb + 465f970 commit 696de1c

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/Processor/Expression/BinaryOperatorEvaluator.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static function evaluate(
152152
return !$expr->negatedInt;
153153
}
154154

155-
return $l_value == $r_value ? 1 : 0 ^ $expr->negatedInt;
155+
return ($l_value == $r_value ? 1 : 0 ) ^ $expr->negatedInt;
156156

157157
case '<>':
158158
case '!=':
@@ -165,35 +165,35 @@ public static function evaluate(
165165
return $expr->negatedInt;
166166
}
167167

168-
return $l_value != $r_value ? 1 : 0 ^ $expr->negatedInt;
168+
return ($l_value != $r_value ? 1 : 0) ^ $expr->negatedInt;
169169

170170
case '>':
171171
if ($as_string) {
172-
return (string) $l_value > (string) $r_value ? 1 : 0 ^ $expr->negatedInt;
172+
return ((string) $l_value > (string) $r_value ? 1 : 0) ^ $expr->negatedInt;
173173
}
174174

175-
return (float) $l_value > (float) $r_value ? 1 : 0 ^ $expr->negatedInt;
175+
return ((float) $l_value > (float) $r_value ? 1 : 0 ) ^ $expr->negatedInt;
176176
// no break
177177
case '>=':
178178
if ($as_string) {
179-
return (string) $l_value >= (string) $r_value ? 1 : 0 ^ $expr->negatedInt;
179+
return ((string) $l_value >= (string) $r_value ? 1 : 0) ^ $expr->negatedInt;
180180
}
181181

182-
return (float) $l_value >= (float) $r_value ? 1 : 0 ^ $expr->negatedInt;
182+
return ((float) $l_value >= (float) $r_value ? 1 : 0) ^ $expr->negatedInt;
183183

184184
case '<':
185185
if ($as_string) {
186-
return (string) $l_value < (string) $r_value ? 1 : 0 ^ $expr->negatedInt;
186+
return ((string) $l_value < (string) $r_value ? 1 : 0) ^ $expr->negatedInt;
187187
}
188188

189-
return (float) $l_value < (float) $r_value ? 1 : 0 ^ $expr->negatedInt;
189+
return ((float) $l_value < (float) $r_value ? 1 : 0) ^ $expr->negatedInt;
190190

191191
case '<=':
192192
if ($as_string) {
193-
return (string) $l_value <= (string) $r_value ? 1 : 0 ^ $expr->negatedInt;
193+
return ((string) $l_value <= (string) $r_value ? 1 : 0) ^ $expr->negatedInt;
194194
}
195195

196-
return (float) $l_value <= (float) $r_value ? 1 : 0 ^ $expr->negatedInt;
196+
return ((float) $l_value <= (float) $r_value ? 1 : 0) ^ $expr->negatedInt;
197197
}
198198

199199
// PHPCS thinks there's a fallthrough here, but there provably is not

tests/EndToEndTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,46 @@ public function testSelectNullableFields()
12141214
$query->fetch(\PDO::FETCH_ASSOC)
12151215
);
12161216
}
1217+
1218+
public function testNegateOperationWithAnd()
1219+
{
1220+
// greater than
1221+
$pdo = self::getConnectionToFullDB(false);
1222+
$query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE `console` = :console AND NOT (`powerups` > :powerups)");
1223+
$query->bindValue(':console', 'nes');
1224+
$query->bindValue(':powerups', 3);
1225+
$query->execute();
1226+
1227+
$this->assertSame([['count' => 8]], $query->fetchAll(\PDO::FETCH_ASSOC));
1228+
1229+
// equals
1230+
$query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE `console` = :console AND NOT (`powerups` = :powerups)");
1231+
$query->bindValue(':console', 'nes');
1232+
$query->bindValue(':powerups', 0);
1233+
$query->execute();
1234+
1235+
$this->assertSame([['count' => 2]], $query->fetchAll(\PDO::FETCH_ASSOC));
1236+
}
1237+
1238+
public function testNegateOperationWithOr()
1239+
{
1240+
// greater than
1241+
$pdo = self::getConnectionToFullDB(false);
1242+
$query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE `console` = :console OR NOT (`powerups` > :powerups)");
1243+
$query->bindValue(':console', 'nes');
1244+
$query->bindValue(':powerups', 3);
1245+
$query->execute();
1246+
1247+
$this->assertSame([['count' => 16]], $query->fetchAll(\PDO::FETCH_ASSOC));
1248+
1249+
// equals
1250+
$query = $pdo->prepare("SELECT COUNT(*) as 'count' FROM `video_game_characters` WHERE `console` = :console OR NOT (`powerups` = :powerups)");
1251+
$query->bindValue(':console', 'nes');
1252+
$query->bindValue(':powerups', 0);
1253+
$query->execute();
1254+
1255+
$this->assertSame([['count' => 9]], $query->fetchAll(\PDO::FETCH_ASSOC));
1256+
}
12171257

12181258
private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO
12191259
{

0 commit comments

Comments
 (0)