diff --git a/CHANGELOG.md b/CHANGELOG.md index b3314ada..56e7eb9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG for 0.9.0 RC ====================== +2.4.2 (26.08.2019) +----- +- IS NULL and IS NOT NULL normalized across all database drivers + 2.4.1 (13.08.2019) ----- - CS: @invisible renamed to @internal diff --git a/src/Driver/Compiler.php b/src/Driver/Compiler.php index bb82af38..1a634648 100644 --- a/src/Driver/Compiler.php +++ b/src/Driver/Compiler.php @@ -72,7 +72,7 @@ public function getPrefix(): string * @param string|FragmentInterface $identifier Identifier can include simple column operations * and functions, having "." in it will * automatically force table prefix to first value. - * @param bool $isTable Set to true to let quote method know that + * @param bool $isTable Set to true to let quote method know that * identified is related to table name. * * @return string @@ -559,6 +559,15 @@ protected function prepareOperator($parameter, string $operator): string return $operator; } + if ($parameter->getType() == \PDO::PARAM_NULL) { + switch ($operator) { + case '=': + return 'IS'; + case '!=': + return 'IS NOT'; + } + } + if ($operator != '=' || is_scalar($parameter->getValue())) { //Doing nothing for non equal operators return $operator; diff --git a/src/Driver/MySQL/MySQLCompiler.php b/src/Driver/MySQL/MySQLCompiler.php index 6b28b6dc..78e72193 100644 --- a/src/Driver/MySQL/MySQLCompiler.php +++ b/src/Driver/MySQL/MySQLCompiler.php @@ -10,7 +10,6 @@ namespace Spiral\Database\Driver\MySQL; use Spiral\Database\Driver\Compiler as AbstractCompiler; -use Spiral\Database\Injection\ParameterInterface; /** * MySQL syntax specific compiler. @@ -41,31 +40,4 @@ protected function compileLimit(int $limit, int $offset): string return trim($statement); } - - /** - * Resolve operator value based on value value. ;). - * - * @param mixed $parameter - * @param string $operator - * - * @return string - */ - protected function prepareOperator($parameter, string $operator): string - { - if (!$parameter instanceof ParameterInterface) { - //Probably fragment - return $operator; - } - - if ($parameter->getType() == \PDO::PARAM_NULL) { - switch ($operator) { - case '=': - return 'IS'; - case '!=': - return 'IS NOT'; - } - } - - return parent::prepareOperator($parameter, $operator); - } } diff --git a/src/Driver/SQLite/SQLiteCompiler.php b/src/Driver/SQLite/SQLiteCompiler.php index 73fa2764..74f2a86a 100644 --- a/src/Driver/SQLite/SQLiteCompiler.php +++ b/src/Driver/SQLite/SQLiteCompiler.php @@ -10,7 +10,6 @@ namespace Spiral\Database\Driver\SQLite; use Spiral\Database\Driver\Compiler as AbstractCompiler; -use Spiral\Database\Injection\ParameterInterface; /** * SQLite specific syntax compiler. @@ -74,31 +73,4 @@ protected function compileLimit(int $limit, int $offset): string return trim($statement); } - - /** - * Resolve operator value based on value value. ;). - * - * @param mixed $parameter - * @param string $operator - * - * @return string - */ - protected function prepareOperator($parameter, string $operator): string - { - if (!$parameter instanceof ParameterInterface) { - //Probably fragment - return $operator; - } - - if ($parameter->getType() == \PDO::PARAM_NULL) { - switch ($operator) { - case '=': - return 'IS'; - case '!=': - return 'IS NOT'; - } - } - - return parent::prepareOperator($parameter, $operator); - } -} +} \ No newline at end of file diff --git a/tests/Database/SelectQueryTest.php b/tests/Database/SelectQueryTest.php index bda617a1..24c7359a 100644 --- a/tests/Database/SelectQueryTest.php +++ b/tests/Database/SelectQueryTest.php @@ -85,7 +85,7 @@ public function testSelectWithSimpleWhereNull() $select = $this->database->select()->distinct()->from(['users'])->where('name', null); $this->assertSameQuery( - "SELECT DISTINCT * FROM {users} WHERE {name} = ?", + "SELECT DISTINCT * FROM {users} WHERE {name} IS ?", $select ); } @@ -95,7 +95,7 @@ public function testSelectWithSimpleWhereNotNull() $select = $this->database->select()->distinct()->from(['users'])->where('name', '!=', null); $this->assertSameQuery( - "SELECT DISTINCT * FROM {users} WHERE {name} != ?", + "SELECT DISTINCT * FROM {users} WHERE {name} IS NOT ?", $select ); } @@ -2020,4 +2020,26 @@ public function testJoinQuery() $select ); } + + public function testDirectIsNull() + { + $select = $this->database->select()->from(['users']) + ->where('name', 'is', null); + + $this->assertSameQuery( + "SELECT * FROM {users} WHERE {name} IS ?", + $select + ); + } + + public function testDirectIsNot() + { + $select = $this->database->select()->from(['users']) + ->where('name', 'is not', null); + + $this->assertSameQuery( + "SELECT * FROM {users} WHERE {name} IS NOT ?", + $select + ); + } } \ No newline at end of file