text.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. from django.db.models.expressions import Func, Value
  2. from django.db.models.fields import IntegerField
  3. from django.db.models.functions import Coalesce
  4. from django.db.models.lookups import Transform
  5. from django.db.utils import NotSupportedError
  6. class BytesToCharFieldConversionMixin:
  7. """
  8. Convert CharField results from bytes to str.
  9. MySQL returns long data types (bytes) instead of chars when it can't
  10. determine the length of the result string. For example:
  11. LPAD(column1, CHAR_LENGTH(column2), ' ')
  12. returns the LONGTEXT (bytes) instead of VARCHAR.
  13. """
  14. def convert_value(self, value, expression, connection):
  15. if connection.features.db_functions_convert_bytes_to_str:
  16. if self.output_field.get_internal_type() == 'CharField' and isinstance(value, bytes):
  17. return value.decode()
  18. return super().convert_value(value, expression, connection)
  19. class MySQLSHA2Mixin:
  20. def as_mysql(self, compiler, connection, **extra_content):
  21. return super().as_sql(
  22. compiler,
  23. connection,
  24. template='SHA2(%%(expressions)s, %s)' % self.function[3:],
  25. **extra_content,
  26. )
  27. class OracleHashMixin:
  28. def as_oracle(self, compiler, connection, **extra_context):
  29. return super().as_sql(
  30. compiler,
  31. connection,
  32. template=(
  33. "LOWER(RAWTOHEX(STANDARD_HASH(UTL_I18N.STRING_TO_RAW("
  34. "%(expressions)s, 'AL32UTF8'), '%(function)s')))"
  35. ),
  36. **extra_context,
  37. )
  38. class PostgreSQLSHAMixin:
  39. def as_postgresql(self, compiler, connection, **extra_content):
  40. return super().as_sql(
  41. compiler,
  42. connection,
  43. template="ENCODE(DIGEST(%(expressions)s, '%(function)s'), 'hex')",
  44. function=self.function.lower(),
  45. **extra_content,
  46. )
  47. class Chr(Transform):
  48. function = 'CHR'
  49. lookup_name = 'chr'
  50. def as_mysql(self, compiler, connection, **extra_context):
  51. return super().as_sql(
  52. compiler, connection, function='CHAR',
  53. template='%(function)s(%(expressions)s USING utf16)',
  54. **extra_context
  55. )
  56. def as_oracle(self, compiler, connection, **extra_context):
  57. return super().as_sql(
  58. compiler, connection,
  59. template='%(function)s(%(expressions)s USING NCHAR_CS)',
  60. **extra_context
  61. )
  62. def as_sqlite(self, compiler, connection, **extra_context):
  63. return super().as_sql(compiler, connection, function='CHAR', **extra_context)
  64. class ConcatPair(Func):
  65. """
  66. Concatenate two arguments together. This is used by `Concat` because not
  67. all backend databases support more than two arguments.
  68. """
  69. function = 'CONCAT'
  70. def as_sqlite(self, compiler, connection, **extra_context):
  71. coalesced = self.coalesce()
  72. return super(ConcatPair, coalesced).as_sql(
  73. compiler, connection, template='%(expressions)s', arg_joiner=' || ',
  74. **extra_context
  75. )
  76. def as_mysql(self, compiler, connection, **extra_context):
  77. # Use CONCAT_WS with an empty separator so that NULLs are ignored.
  78. return super().as_sql(
  79. compiler, connection, function='CONCAT_WS',
  80. template="%(function)s('', %(expressions)s)",
  81. **extra_context
  82. )
  83. def coalesce(self):
  84. # null on either side results in null for expression, wrap with coalesce
  85. c = self.copy()
  86. c.set_source_expressions([
  87. Coalesce(expression, Value('')) for expression in c.get_source_expressions()
  88. ])
  89. return c
  90. class Concat(Func):
  91. """
  92. Concatenate text fields together. Backends that result in an entire
  93. null expression when any arguments are null will wrap each argument in
  94. coalesce functions to ensure a non-null result.
  95. """
  96. function = None
  97. template = "%(expressions)s"
  98. def __init__(self, *expressions, **extra):
  99. if len(expressions) < 2:
  100. raise ValueError('Concat must take at least two expressions')
  101. paired = self._paired(expressions)
  102. super().__init__(paired, **extra)
  103. def _paired(self, expressions):
  104. # wrap pairs of expressions in successive concat functions
  105. # exp = [a, b, c, d]
  106. # -> ConcatPair(a, ConcatPair(b, ConcatPair(c, d))))
  107. if len(expressions) == 2:
  108. return ConcatPair(*expressions)
  109. return ConcatPair(expressions[0], self._paired(expressions[1:]))
  110. class Left(Func):
  111. function = 'LEFT'
  112. arity = 2
  113. def __init__(self, expression, length, **extra):
  114. """
  115. expression: the name of a field, or an expression returning a string
  116. length: the number of characters to return from the start of the string
  117. """
  118. if not hasattr(length, 'resolve_expression'):
  119. if length < 1:
  120. raise ValueError("'length' must be greater than 0.")
  121. super().__init__(expression, length, **extra)
  122. def get_substr(self):
  123. return Substr(self.source_expressions[0], Value(1), self.source_expressions[1])
  124. def as_oracle(self, compiler, connection, **extra_context):
  125. return self.get_substr().as_oracle(compiler, connection, **extra_context)
  126. def as_sqlite(self, compiler, connection, **extra_context):
  127. return self.get_substr().as_sqlite(compiler, connection, **extra_context)
  128. class Length(Transform):
  129. """Return the number of characters in the expression."""
  130. function = 'LENGTH'
  131. lookup_name = 'length'
  132. output_field = IntegerField()
  133. def as_mysql(self, compiler, connection, **extra_context):
  134. return super().as_sql(compiler, connection, function='CHAR_LENGTH', **extra_context)
  135. class Lower(Transform):
  136. function = 'LOWER'
  137. lookup_name = 'lower'
  138. class LPad(BytesToCharFieldConversionMixin, Func):
  139. function = 'LPAD'
  140. def __init__(self, expression, length, fill_text=Value(' '), **extra):
  141. if not hasattr(length, 'resolve_expression') and length is not None and length < 0:
  142. raise ValueError("'length' must be greater or equal to 0.")
  143. super().__init__(expression, length, fill_text, **extra)
  144. class LTrim(Transform):
  145. function = 'LTRIM'
  146. lookup_name = 'ltrim'
  147. class MD5(OracleHashMixin, Transform):
  148. function = 'MD5'
  149. lookup_name = 'md5'
  150. class Ord(Transform):
  151. function = 'ASCII'
  152. lookup_name = 'ord'
  153. output_field = IntegerField()
  154. def as_mysql(self, compiler, connection, **extra_context):
  155. return super().as_sql(compiler, connection, function='ORD', **extra_context)
  156. def as_sqlite(self, compiler, connection, **extra_context):
  157. return super().as_sql(compiler, connection, function='UNICODE', **extra_context)
  158. class Repeat(BytesToCharFieldConversionMixin, Func):
  159. function = 'REPEAT'
  160. def __init__(self, expression, number, **extra):
  161. if not hasattr(number, 'resolve_expression') and number is not None and number < 0:
  162. raise ValueError("'number' must be greater or equal to 0.")
  163. super().__init__(expression, number, **extra)
  164. def as_oracle(self, compiler, connection, **extra_context):
  165. expression, number = self.source_expressions
  166. length = None if number is None else Length(expression) * number
  167. rpad = RPad(expression, length, expression)
  168. return rpad.as_sql(compiler, connection, **extra_context)
  169. class Replace(Func):
  170. function = 'REPLACE'
  171. def __init__(self, expression, text, replacement=Value(''), **extra):
  172. super().__init__(expression, text, replacement, **extra)
  173. class Reverse(Transform):
  174. function = 'REVERSE'
  175. lookup_name = 'reverse'
  176. def as_oracle(self, compiler, connection, **extra_context):
  177. # REVERSE in Oracle is undocumented and doesn't support multi-byte
  178. # strings. Use a special subquery instead.
  179. return super().as_sql(
  180. compiler, connection,
  181. template=(
  182. '(SELECT LISTAGG(s) WITHIN GROUP (ORDER BY n DESC) FROM '
  183. '(SELECT LEVEL n, SUBSTR(%(expressions)s, LEVEL, 1) s '
  184. 'FROM DUAL CONNECT BY LEVEL <= LENGTH(%(expressions)s)) '
  185. 'GROUP BY %(expressions)s)'
  186. ),
  187. **extra_context
  188. )
  189. class Right(Left):
  190. function = 'RIGHT'
  191. def get_substr(self):
  192. return Substr(self.source_expressions[0], self.source_expressions[1] * Value(-1))
  193. class RPad(LPad):
  194. function = 'RPAD'
  195. class RTrim(Transform):
  196. function = 'RTRIM'
  197. lookup_name = 'rtrim'
  198. class SHA1(OracleHashMixin, PostgreSQLSHAMixin, Transform):
  199. function = 'SHA1'
  200. lookup_name = 'sha1'
  201. class SHA224(MySQLSHA2Mixin, PostgreSQLSHAMixin, Transform):
  202. function = 'SHA224'
  203. lookup_name = 'sha224'
  204. def as_oracle(self, compiler, connection, **extra_context):
  205. raise NotSupportedError('SHA224 is not supported on Oracle.')
  206. class SHA256(MySQLSHA2Mixin, OracleHashMixin, PostgreSQLSHAMixin, Transform):
  207. function = 'SHA256'
  208. lookup_name = 'sha256'
  209. class SHA384(MySQLSHA2Mixin, OracleHashMixin, PostgreSQLSHAMixin, Transform):
  210. function = 'SHA384'
  211. lookup_name = 'sha384'
  212. class SHA512(MySQLSHA2Mixin, OracleHashMixin, PostgreSQLSHAMixin, Transform):
  213. function = 'SHA512'
  214. lookup_name = 'sha512'
  215. class StrIndex(Func):
  216. """
  217. Return a positive integer corresponding to the 1-indexed position of the
  218. first occurrence of a substring inside another string, or 0 if the
  219. substring is not found.
  220. """
  221. function = 'INSTR'
  222. arity = 2
  223. output_field = IntegerField()
  224. def as_postgresql(self, compiler, connection, **extra_context):
  225. return super().as_sql(compiler, connection, function='STRPOS', **extra_context)
  226. class Substr(Func):
  227. function = 'SUBSTRING'
  228. def __init__(self, expression, pos, length=None, **extra):
  229. """
  230. expression: the name of a field, or an expression returning a string
  231. pos: an integer > 0, or an expression returning an integer
  232. length: an optional number of characters to return
  233. """
  234. if not hasattr(pos, 'resolve_expression'):
  235. if pos < 1:
  236. raise ValueError("'pos' must be greater than 0")
  237. expressions = [expression, pos]
  238. if length is not None:
  239. expressions.append(length)
  240. super().__init__(*expressions, **extra)
  241. def as_sqlite(self, compiler, connection, **extra_context):
  242. return super().as_sql(compiler, connection, function='SUBSTR', **extra_context)
  243. def as_oracle(self, compiler, connection, **extra_context):
  244. return super().as_sql(compiler, connection, function='SUBSTR', **extra_context)
  245. class Trim(Transform):
  246. function = 'TRIM'
  247. lookup_name = 'trim'
  248. class Upper(Transform):
  249. function = 'UPPER'
  250. lookup_name = 'upper'