Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
- IS NULL and IS NOT NULL normalized across all database drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Aug 27, 2019
1 parent f22600a commit 20991f8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 60 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/Driver/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 0 additions & 28 deletions src/Driver/MySQL/MySQLCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
30 changes: 1 addition & 29 deletions src/Driver/SQLite/SQLiteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
26 changes: 24 additions & 2 deletions tests/Database/SelectQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -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
);
}
Expand Down Expand Up @@ -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
);
}
}

0 comments on commit 20991f8

Please sign in to comment.