2014-11-22 11:14:43 +08:00
|
|
|
"""
|
|
|
|
Classes that represent database functions.
|
|
|
|
"""
|
2015-05-29 22:54:10 +08:00
|
|
|
from django.db.models import DateTimeField, IntegerField
|
2014-11-22 11:14:43 +08:00
|
|
|
from django.db.models.expressions import Func, Value
|
|
|
|
|
|
|
|
|
|
|
|
class Coalesce(Func):
|
|
|
|
"""
|
|
|
|
Chooses, from left to right, the first non-null expression and returns it.
|
|
|
|
"""
|
|
|
|
function = 'COALESCE'
|
|
|
|
|
|
|
|
def __init__(self, *expressions, **extra):
|
|
|
|
if len(expressions) < 2:
|
|
|
|
raise ValueError('Coalesce must take at least two expressions')
|
|
|
|
super(Coalesce, self).__init__(*expressions, **extra)
|
|
|
|
|
2014-12-29 06:15:00 +08:00
|
|
|
def as_oracle(self, compiler, connection):
|
|
|
|
# we can't mix TextField (NCLOB) and CharField (NVARCHAR), so convert
|
|
|
|
# all fields to NCLOB when we expect NCLOB
|
|
|
|
if self.output_field.get_internal_type() == 'TextField':
|
|
|
|
class ToNCLOB(Func):
|
|
|
|
function = 'TO_NCLOB'
|
|
|
|
|
|
|
|
expressions = [
|
|
|
|
ToNCLOB(expression) for expression in self.get_source_expressions()]
|
|
|
|
self.set_source_expressions(expressions)
|
|
|
|
return super(Coalesce, self).as_sql(compiler, connection)
|
|
|
|
|
2014-11-22 11:14:43 +08:00
|
|
|
|
|
|
|
class ConcatPair(Func):
|
|
|
|
"""
|
|
|
|
A helper class that concatenates two arguments together. This is used
|
|
|
|
by `Concat` because not all backend databases support more than two
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
function = 'CONCAT'
|
|
|
|
|
|
|
|
def __init__(self, left, right, **extra):
|
|
|
|
super(ConcatPair, self).__init__(left, right, **extra)
|
|
|
|
|
|
|
|
def as_sqlite(self, compiler, connection):
|
|
|
|
self.arg_joiner = ' || '
|
|
|
|
self.template = '%(expressions)s'
|
|
|
|
self.coalesce()
|
|
|
|
return super(ConcatPair, self).as_sql(compiler, connection)
|
|
|
|
|
|
|
|
def as_mysql(self, compiler, connection):
|
2015-04-09 05:26:09 +08:00
|
|
|
# Use CONCAT_WS with an empty separator so that NULLs are ignored.
|
|
|
|
self.function = 'CONCAT_WS'
|
|
|
|
self.template = "%(function)s('', %(expressions)s)"
|
2014-11-22 11:14:43 +08:00
|
|
|
return super(ConcatPair, self).as_sql(compiler, connection)
|
|
|
|
|
|
|
|
def coalesce(self):
|
|
|
|
# null on either side results in null for expression, wrap with coalesce
|
|
|
|
expressions = [
|
|
|
|
Coalesce(expression, Value('')) for expression in self.get_source_expressions()]
|
|
|
|
self.set_source_expressions(expressions)
|
|
|
|
|
|
|
|
|
|
|
|
class Concat(Func):
|
|
|
|
"""
|
|
|
|
Concatenates text fields together. Backends that result in an entire
|
|
|
|
null expression when any arguments are null will wrap each argument in
|
|
|
|
coalesce functions to ensure we always get a non-null result.
|
|
|
|
"""
|
|
|
|
function = None
|
|
|
|
template = "%(expressions)s"
|
|
|
|
|
|
|
|
def __init__(self, *expressions, **extra):
|
|
|
|
if len(expressions) < 2:
|
|
|
|
raise ValueError('Concat must take at least two expressions')
|
|
|
|
paired = self._paired(expressions)
|
|
|
|
super(Concat, self).__init__(paired, **extra)
|
|
|
|
|
|
|
|
def _paired(self, expressions):
|
|
|
|
# wrap pairs of expressions in successive concat functions
|
|
|
|
# exp = [a, b, c, d]
|
|
|
|
# -> ConcatPair(a, ConcatPair(b, ConcatPair(c, d))))
|
|
|
|
if len(expressions) == 2:
|
|
|
|
return ConcatPair(*expressions)
|
|
|
|
return ConcatPair(expressions[0], self._paired(expressions[1:]))
|
|
|
|
|
|
|
|
|
2015-05-09 19:55:03 +08:00
|
|
|
class Greatest(Func):
|
|
|
|
"""
|
|
|
|
Chooses the maximum expression and returns it.
|
|
|
|
|
|
|
|
If any expression is null the return value is database-specific:
|
|
|
|
On Postgres, the maximum not-null expression is returned.
|
2015-06-05 23:40:10 +08:00
|
|
|
On MySQL, Oracle, and SQLite, if any expression is null, null is returned.
|
2015-05-09 19:55:03 +08:00
|
|
|
"""
|
|
|
|
function = 'GREATEST'
|
|
|
|
|
|
|
|
def __init__(self, *expressions, **extra):
|
|
|
|
if len(expressions) < 2:
|
|
|
|
raise ValueError('Greatest must take at least two expressions')
|
|
|
|
super(Greatest, self).__init__(*expressions, **extra)
|
|
|
|
|
|
|
|
def as_sqlite(self, compiler, connection):
|
|
|
|
"""Use the MAX function on SQLite."""
|
|
|
|
return super(Greatest, self).as_sql(compiler, connection, function='MAX')
|
|
|
|
|
|
|
|
|
|
|
|
class Least(Func):
|
|
|
|
"""
|
|
|
|
Chooses the minimum expression and returns it.
|
|
|
|
|
|
|
|
If any expression is null the return value is database-specific:
|
|
|
|
On Postgres, the minimum not-null expression is returned.
|
2015-06-05 23:40:10 +08:00
|
|
|
On MySQL, Oracle, and SQLite, if any expression is null, null is returned.
|
2015-05-09 19:55:03 +08:00
|
|
|
"""
|
|
|
|
function = 'LEAST'
|
|
|
|
|
|
|
|
def __init__(self, *expressions, **extra):
|
|
|
|
if len(expressions) < 2:
|
|
|
|
raise ValueError('Least must take at least two expressions')
|
|
|
|
super(Least, self).__init__(*expressions, **extra)
|
|
|
|
|
|
|
|
def as_sqlite(self, compiler, connection):
|
|
|
|
"""Use the MIN function on SQLite."""
|
|
|
|
return super(Least, self).as_sql(compiler, connection, function='MIN')
|
|
|
|
|
|
|
|
|
2014-11-22 11:14:43 +08:00
|
|
|
class Length(Func):
|
|
|
|
"""Returns the number of characters in the expression"""
|
|
|
|
function = 'LENGTH'
|
|
|
|
|
|
|
|
def __init__(self, expression, **extra):
|
|
|
|
output_field = extra.pop('output_field', IntegerField())
|
|
|
|
super(Length, self).__init__(expression, output_field=output_field, **extra)
|
|
|
|
|
|
|
|
def as_mysql(self, compiler, connection):
|
|
|
|
self.function = 'CHAR_LENGTH'
|
|
|
|
return super(Length, self).as_sql(compiler, connection)
|
|
|
|
|
|
|
|
|
|
|
|
class Lower(Func):
|
|
|
|
function = 'LOWER'
|
|
|
|
|
|
|
|
def __init__(self, expression, **extra):
|
|
|
|
super(Lower, self).__init__(expression, **extra)
|
|
|
|
|
|
|
|
|
2015-05-29 22:54:10 +08:00
|
|
|
class Now(Func):
|
|
|
|
template = 'CURRENT_TIMESTAMP'
|
|
|
|
|
|
|
|
def __init__(self, output_field=None, **extra):
|
|
|
|
if output_field is None:
|
|
|
|
output_field = DateTimeField()
|
|
|
|
super(Now, self).__init__(output_field=output_field, **extra)
|
|
|
|
|
|
|
|
def as_postgresql(self, compiler, connection):
|
|
|
|
# Postgres' CURRENT_TIMESTAMP means "the time at the start of the
|
|
|
|
# transaction". We use STATEMENT_TIMESTAMP to be cross-compatible with
|
|
|
|
# other databases.
|
|
|
|
self.template = 'STATEMENT_TIMESTAMP()'
|
|
|
|
return self.as_sql(compiler, connection)
|
|
|
|
|
|
|
|
|
2014-11-22 11:14:43 +08:00
|
|
|
class Substr(Func):
|
|
|
|
function = 'SUBSTRING'
|
|
|
|
|
|
|
|
def __init__(self, expression, pos, length=None, **extra):
|
|
|
|
"""
|
|
|
|
expression: the name of a field, or an expression returning a string
|
|
|
|
pos: an integer > 0, or an expression returning an integer
|
|
|
|
length: an optional number of characters to return
|
|
|
|
"""
|
2015-01-20 08:47:13 +08:00
|
|
|
if not hasattr(pos, 'resolve_expression'):
|
2014-11-22 11:14:43 +08:00
|
|
|
if pos < 1:
|
|
|
|
raise ValueError("'pos' must be greater than 0")
|
|
|
|
pos = Value(pos)
|
|
|
|
expressions = [expression, pos]
|
|
|
|
if length is not None:
|
2015-01-20 08:47:13 +08:00
|
|
|
if not hasattr(length, 'resolve_expression'):
|
2014-11-22 11:14:43 +08:00
|
|
|
length = Value(length)
|
|
|
|
expressions.append(length)
|
|
|
|
super(Substr, self).__init__(*expressions, **extra)
|
|
|
|
|
|
|
|
def as_sqlite(self, compiler, connection):
|
|
|
|
self.function = 'SUBSTR'
|
|
|
|
return super(Substr, self).as_sql(compiler, connection)
|
|
|
|
|
|
|
|
def as_oracle(self, compiler, connection):
|
|
|
|
self.function = 'SUBSTR'
|
|
|
|
return super(Substr, self).as_sql(compiler, connection)
|
|
|
|
|
|
|
|
|
|
|
|
class Upper(Func):
|
|
|
|
function = 'UPPER'
|
|
|
|
|
|
|
|
def __init__(self, expression, **extra):
|
|
|
|
super(Upper, self).__init__(expression, **extra)
|