Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comparison.py 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Database functions that do comparisons or type conversions."""
  2. from django.db.models import Func
  3. class Cast(Func):
  4. """Coerce an expression to a new field type."""
  5. function = 'CAST'
  6. template = '%(function)s(%(expressions)s AS %(db_type)s)'
  7. def __init__(self, expression, output_field):
  8. super().__init__(expression, output_field=output_field)
  9. def as_sql(self, compiler, connection, **extra_context):
  10. extra_context['db_type'] = self.output_field.cast_db_type(connection)
  11. return super().as_sql(compiler, connection, **extra_context)
  12. def as_mysql(self, compiler, connection):
  13. # MySQL doesn't support explicit cast to float.
  14. template = '(%(expressions)s + 0.0)' if self.output_field.get_internal_type() == 'FloatField' else None
  15. return self.as_sql(compiler, connection, template=template)
  16. def as_postgresql(self, compiler, connection):
  17. # CAST would be valid too, but the :: shortcut syntax is more readable.
  18. # 'expressions' is wrapped in parentheses in case it's a complex
  19. # expression.
  20. return self.as_sql(compiler, connection, template='(%(expressions)s)::%(db_type)s')
  21. class Coalesce(Func):
  22. """Return, from left to right, the first non-null expression."""
  23. function = 'COALESCE'
  24. def __init__(self, *expressions, **extra):
  25. if len(expressions) < 2:
  26. raise ValueError('Coalesce must take at least two expressions')
  27. super().__init__(*expressions, **extra)
  28. def as_oracle(self, compiler, connection):
  29. # Oracle prohibits mixing TextField (NCLOB) and CharField (NVARCHAR2),
  30. # so convert all fields to NCLOB when that type is expected.
  31. if self.output_field.get_internal_type() == 'TextField':
  32. class ToNCLOB(Func):
  33. function = 'TO_NCLOB'
  34. expressions = [
  35. ToNCLOB(expression) for expression in self.get_source_expressions()
  36. ]
  37. clone = self.copy()
  38. clone.set_source_expressions(expressions)
  39. return super(Coalesce, clone).as_sql(compiler, connection)
  40. return self.as_sql(compiler, connection)
  41. class Greatest(Func):
  42. """
  43. Return the maximum expression.
  44. If any expression is null the return value is database-specific:
  45. On PostgreSQL, the maximum not-null expression is returned.
  46. On MySQL, Oracle, and SQLite, if any expression is null, null is returned.
  47. """
  48. function = 'GREATEST'
  49. def __init__(self, *expressions, **extra):
  50. if len(expressions) < 2:
  51. raise ValueError('Greatest must take at least two expressions')
  52. super().__init__(*expressions, **extra)
  53. def as_sqlite(self, compiler, connection):
  54. """Use the MAX function on SQLite."""
  55. return super().as_sqlite(compiler, connection, function='MAX')
  56. class Least(Func):
  57. """
  58. Return the minimum expression.
  59. If any expression is null the return value is database-specific:
  60. On PostgreSQL, return the minimum not-null expression.
  61. On MySQL, Oracle, and SQLite, if any expression is null, return null.
  62. """
  63. function = 'LEAST'
  64. def __init__(self, *expressions, **extra):
  65. if len(expressions) < 2:
  66. raise ValueError('Least must take at least two expressions')
  67. super().__init__(*expressions, **extra)
  68. def as_sqlite(self, compiler, connection):
  69. """Use the MIN function on SQLite."""
  70. return super().as_sqlite(compiler, connection, function='MIN')