Refs #28643 -- Added NullIf database function.

Thanks Nick Pope, Mariusz Felisiak, and Tim Graham for reviews.
This commit is contained in:
Mads Jensen 2018-01-14 22:00:16 +01:00 committed by Tim Graham
parent 217f4456d8
commit 4b9d72210f
6 changed files with 78 additions and 3 deletions

View file

@ -1,5 +1,5 @@
"""Database functions that do comparisons or type conversions."""
from django.db.models.expressions import Func
from django.db.models.expressions import Func, Value
class Cast(Func):
@ -103,3 +103,14 @@ class Least(Func):
def as_sqlite(self, compiler, connection, **extra_context):
"""Use the MIN function on SQLite."""
return super().as_sqlite(compiler, connection, function='MIN', **extra_context)
class NullIf(Func):
function = 'NULLIF'
arity = 2
def as_oracle(self, compiler, connection, **extra_context):
expression1 = self.get_source_expressions()[0]
if isinstance(expression1, Value) and expression1.value is None:
raise ValueError('Oracle does not allow Value(None) for expression1.')
return super().as_sql(compiler, connection, **extra_context)