2019-01-09 08:21:31 +08:00
|
|
|
from django.db.models.expressions import Func, Value
|
|
|
|
from django.db.models.fields import IntegerField
|
2017-10-14 03:23:00 +08:00
|
|
|
from django.db.models.functions import Coalesce
|
2019-01-09 08:21:31 +08:00
|
|
|
from django.db.models.lookups import Transform
|
2014-12-29 06:15:00 +08:00
|
|
|
|
2014-11-22 11:14:43 +08:00
|
|
|
|
2018-04-04 00:24:04 +08:00
|
|
|
class BytesToCharFieldConversionMixin:
|
|
|
|
"""
|
|
|
|
Convert CharField results from bytes to str.
|
|
|
|
|
|
|
|
MySQL returns long data types (bytes) instead of chars when it can't
|
|
|
|
determine the length of the result string. For example:
|
|
|
|
LPAD(column1, CHAR_LENGTH(column2), ' ')
|
|
|
|
returns the LONGTEXT (bytes) instead of VARCHAR.
|
|
|
|
"""
|
|
|
|
def convert_value(self, value, expression, connection):
|
|
|
|
if connection.features.db_functions_convert_bytes_to_str:
|
|
|
|
if self.output_field.get_internal_type() == 'CharField' and isinstance(value, bytes):
|
|
|
|
return value.decode()
|
|
|
|
return super().convert_value(value, expression, connection)
|
|
|
|
|
|
|
|
|
2018-02-23 23:23:22 +08:00
|
|
|
class Chr(Transform):
|
|
|
|
function = 'CHR'
|
|
|
|
lookup_name = 'chr'
|
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_mysql(self, compiler, connection, **extra_context):
|
2018-02-23 23:23:22 +08:00
|
|
|
return super().as_sql(
|
2018-02-08 15:09:00 +08:00
|
|
|
compiler, connection, function='CHAR',
|
|
|
|
template='%(function)s(%(expressions)s USING utf16)',
|
|
|
|
**extra_context
|
2018-02-23 23:23:22 +08:00
|
|
|
)
|
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_oracle(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(
|
|
|
|
compiler, connection,
|
|
|
|
template='%(function)s(%(expressions)s USING NCHAR_CS)',
|
|
|
|
**extra_context
|
|
|
|
)
|
2018-02-23 23:23:22 +08:00
|
|
|
|
|
|
|
def as_sqlite(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(compiler, connection, function='CHAR', **extra_context)
|
|
|
|
|
|
|
|
|
2014-11-22 11:14:43 +08:00
|
|
|
class ConcatPair(Func):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Concatenate two arguments together. This is used by `Concat` because not
|
|
|
|
all backend databases support more than two arguments.
|
2014-11-22 11:14:43 +08:00
|
|
|
"""
|
|
|
|
function = 'CONCAT'
|
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_sqlite(self, compiler, connection, **extra_context):
|
2015-10-14 07:07:42 +08:00
|
|
|
coalesced = self.coalesce()
|
2016-03-16 23:05:24 +08:00
|
|
|
return super(ConcatPair, coalesced).as_sql(
|
2018-02-08 15:09:00 +08:00
|
|
|
compiler, connection, template='%(expressions)s', arg_joiner=' || ',
|
|
|
|
**extra_context
|
2016-03-16 23:05:24 +08:00
|
|
|
)
|
2014-11-22 11:14:43 +08:00
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_mysql(self, compiler, connection, **extra_context):
|
2015-04-09 05:26:09 +08:00
|
|
|
# Use CONCAT_WS with an empty separator so that NULLs are ignored.
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().as_sql(
|
2018-02-08 15:09:00 +08:00
|
|
|
compiler, connection, function='CONCAT_WS',
|
|
|
|
template="%(function)s('', %(expressions)s)",
|
|
|
|
**extra_context
|
2016-03-16 23:05:24 +08:00
|
|
|
)
|
2014-11-22 11:14:43 +08:00
|
|
|
|
|
|
|
def coalesce(self):
|
|
|
|
# null on either side results in null for expression, wrap with coalesce
|
2015-10-14 07:07:42 +08:00
|
|
|
c = self.copy()
|
2019-01-14 05:58:09 +08:00
|
|
|
c.set_source_expressions([
|
2015-10-14 07:07:42 +08:00
|
|
|
Coalesce(expression, Value('')) for expression in c.get_source_expressions()
|
2019-01-14 05:58:09 +08:00
|
|
|
])
|
2015-10-14 07:07:42 +08:00
|
|
|
return c
|
2014-11-22 11:14:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Concat(Func):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Concatenate text fields together. Backends that result in an entire
|
2014-11-22 11:14:43 +08:00
|
|
|
null expression when any arguments are null will wrap each argument in
|
2017-01-25 07:04:12 +08:00
|
|
|
coalesce functions to ensure a non-null result.
|
2014-11-22 11:14:43 +08:00
|
|
|
"""
|
|
|
|
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)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(paired, **extra)
|
2014-11-22 11:14:43 +08:00
|
|
|
|
|
|
|
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:]))
|
|
|
|
|
|
|
|
|
2018-02-23 23:23:22 +08:00
|
|
|
class Left(Func):
|
|
|
|
function = 'LEFT'
|
|
|
|
arity = 2
|
|
|
|
|
|
|
|
def __init__(self, expression, length, **extra):
|
|
|
|
"""
|
|
|
|
expression: the name of a field, or an expression returning a string
|
|
|
|
length: the number of characters to return from the start of the string
|
|
|
|
"""
|
|
|
|
if not hasattr(length, 'resolve_expression'):
|
|
|
|
if length < 1:
|
|
|
|
raise ValueError("'length' must be greater than 0.")
|
|
|
|
super().__init__(expression, length, **extra)
|
|
|
|
|
|
|
|
def get_substr(self):
|
|
|
|
return Substr(self.source_expressions[0], Value(1), self.source_expressions[1])
|
|
|
|
|
2019-01-14 05:59:26 +08:00
|
|
|
def as_oracle(self, compiler, connection, **extra_context):
|
2018-02-23 23:23:22 +08:00
|
|
|
return self.get_substr().as_oracle(compiler, connection, **extra_context)
|
|
|
|
|
2019-01-14 05:59:26 +08:00
|
|
|
def as_sqlite(self, compiler, connection, **extra_context):
|
|
|
|
return self.get_substr().as_sqlite(compiler, connection, **extra_context)
|
2018-02-23 23:23:22 +08:00
|
|
|
|
|
|
|
|
2015-08-03 10:30:06 +08:00
|
|
|
class Length(Transform):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Return the number of characters in the expression."""
|
2014-11-22 11:14:43 +08:00
|
|
|
function = 'LENGTH'
|
2015-08-03 10:30:06 +08:00
|
|
|
lookup_name = 'length'
|
2019-01-09 08:21:31 +08:00
|
|
|
output_field = IntegerField()
|
2014-11-22 11:14:43 +08:00
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_mysql(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(compiler, connection, function='CHAR_LENGTH', **extra_context)
|
2014-11-22 11:14:43 +08:00
|
|
|
|
|
|
|
|
2015-08-03 10:30:06 +08:00
|
|
|
class Lower(Transform):
|
2014-11-22 11:14:43 +08:00
|
|
|
function = 'LOWER'
|
2015-08-03 10:30:06 +08:00
|
|
|
lookup_name = 'lower'
|
2014-11-22 11:14:43 +08:00
|
|
|
|
|
|
|
|
2018-04-04 00:24:04 +08:00
|
|
|
class LPad(BytesToCharFieldConversionMixin, Func):
|
2018-03-20 00:35:16 +08:00
|
|
|
function = 'LPAD'
|
|
|
|
|
|
|
|
def __init__(self, expression, length, fill_text=Value(' '), **extra):
|
2018-07-01 04:49:20 +08:00
|
|
|
if not hasattr(length, 'resolve_expression') and length is not None and length < 0:
|
2018-03-20 00:35:16 +08:00
|
|
|
raise ValueError("'length' must be greater or equal to 0.")
|
|
|
|
super().__init__(expression, length, fill_text, **extra)
|
|
|
|
|
|
|
|
|
2018-03-16 03:57:23 +08:00
|
|
|
class LTrim(Transform):
|
|
|
|
function = 'LTRIM'
|
|
|
|
lookup_name = 'ltrim'
|
|
|
|
|
|
|
|
|
2019-02-21 17:52:51 +08:00
|
|
|
class MD5(Transform):
|
|
|
|
function = 'MD5'
|
|
|
|
lookup_name = 'md5'
|
|
|
|
|
|
|
|
def as_oracle(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(
|
|
|
|
compiler,
|
|
|
|
connection,
|
|
|
|
template=(
|
|
|
|
"LOWER(RAWTOHEX(STANDARD_HASH(UTL_I18N.STRING_TO_RAW("
|
|
|
|
"%(expressions)s, 'AL32UTF8'), '%(function)s')))"
|
|
|
|
),
|
|
|
|
**extra_context,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-02-23 23:23:22 +08:00
|
|
|
class Ord(Transform):
|
|
|
|
function = 'ASCII'
|
|
|
|
lookup_name = 'ord'
|
|
|
|
output_field = IntegerField()
|
|
|
|
|
|
|
|
def as_mysql(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(compiler, connection, function='ORD', **extra_context)
|
|
|
|
|
|
|
|
def as_sqlite(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(compiler, connection, function='UNICODE', **extra_context)
|
|
|
|
|
|
|
|
|
2018-04-04 01:36:12 +08:00
|
|
|
class Repeat(BytesToCharFieldConversionMixin, Func):
|
|
|
|
function = 'REPEAT'
|
|
|
|
|
|
|
|
def __init__(self, expression, number, **extra):
|
2018-07-01 04:49:20 +08:00
|
|
|
if not hasattr(number, 'resolve_expression') and number is not None and number < 0:
|
2018-04-04 01:36:12 +08:00
|
|
|
raise ValueError("'number' must be greater or equal to 0.")
|
|
|
|
super().__init__(expression, number, **extra)
|
|
|
|
|
|
|
|
def as_oracle(self, compiler, connection, **extra_context):
|
|
|
|
expression, number = self.source_expressions
|
2018-07-01 04:49:20 +08:00
|
|
|
length = None if number is None else Length(expression) * number
|
|
|
|
rpad = RPad(expression, length, expression)
|
2018-04-04 01:36:12 +08:00
|
|
|
return rpad.as_sql(compiler, connection, **extra_context)
|
|
|
|
|
|
|
|
|
2018-01-18 09:46:15 +08:00
|
|
|
class Replace(Func):
|
|
|
|
function = 'REPLACE'
|
|
|
|
|
|
|
|
def __init__(self, expression, text, replacement=Value(''), **extra):
|
|
|
|
super().__init__(expression, text, replacement, **extra)
|
|
|
|
|
|
|
|
|
2019-01-09 08:12:10 +08:00
|
|
|
class Reverse(Transform):
|
|
|
|
function = 'REVERSE'
|
|
|
|
lookup_name = 'reverse'
|
|
|
|
|
|
|
|
def as_oracle(self, compiler, connection, **extra_context):
|
|
|
|
# REVERSE in Oracle is undocumented and doesn't support multi-byte
|
|
|
|
# strings. Use a special subquery instead.
|
|
|
|
return super().as_sql(
|
|
|
|
compiler, connection,
|
|
|
|
template=(
|
|
|
|
'(SELECT LISTAGG(s) WITHIN GROUP (ORDER BY n DESC) FROM '
|
|
|
|
'(SELECT LEVEL n, SUBSTR(%(expressions)s, LEVEL, 1) s '
|
|
|
|
'FROM DUAL CONNECT BY LEVEL <= LENGTH(%(expressions)s)) '
|
|
|
|
'GROUP BY %(expressions)s)'
|
|
|
|
),
|
|
|
|
**extra_context
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-02-23 23:23:22 +08:00
|
|
|
class Right(Left):
|
|
|
|
function = 'RIGHT'
|
|
|
|
|
|
|
|
def get_substr(self):
|
|
|
|
return Substr(self.source_expressions[0], self.source_expressions[1] * Value(-1))
|
|
|
|
|
|
|
|
|
2018-03-20 00:35:16 +08:00
|
|
|
class RPad(LPad):
|
|
|
|
function = 'RPAD'
|
|
|
|
|
|
|
|
|
2018-03-16 03:57:23 +08:00
|
|
|
class RTrim(Transform):
|
|
|
|
function = 'RTRIM'
|
|
|
|
lookup_name = 'rtrim'
|
|
|
|
|
|
|
|
|
2017-02-21 14:43:38 +08:00
|
|
|
class StrIndex(Func):
|
|
|
|
"""
|
|
|
|
Return a positive integer corresponding to the 1-indexed position of the
|
|
|
|
first occurrence of a substring inside another string, or 0 if the
|
|
|
|
substring is not found.
|
|
|
|
"""
|
|
|
|
function = 'INSTR'
|
|
|
|
arity = 2
|
2019-01-09 08:21:31 +08:00
|
|
|
output_field = IntegerField()
|
2017-02-21 14:43:38 +08:00
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_postgresql(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(compiler, connection, function='STRPOS', **extra_context)
|
2017-02-21 14:43:38 +08:00
|
|
|
|
|
|
|
|
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")
|
|
|
|
expressions = [expression, pos]
|
|
|
|
if length is not None:
|
|
|
|
expressions.append(length)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*expressions, **extra)
|
2014-11-22 11:14:43 +08:00
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_sqlite(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(compiler, connection, function='SUBSTR', **extra_context)
|
2014-11-22 11:14:43 +08:00
|
|
|
|
2018-02-08 15:09:00 +08:00
|
|
|
def as_oracle(self, compiler, connection, **extra_context):
|
|
|
|
return super().as_sql(compiler, connection, function='SUBSTR', **extra_context)
|
2014-11-22 11:14:43 +08:00
|
|
|
|
|
|
|
|
2018-03-16 03:57:23 +08:00
|
|
|
class Trim(Transform):
|
|
|
|
function = 'TRIM'
|
|
|
|
lookup_name = 'trim'
|
|
|
|
|
|
|
|
|
2015-08-03 10:30:06 +08:00
|
|
|
class Upper(Transform):
|
2014-11-22 11:14:43 +08:00
|
|
|
function = 'UPPER'
|
2015-08-03 10:30:06 +08:00
|
|
|
lookup_name = 'upper'
|