PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/daycapture_venv/lib/python3.8/site-packages/django/db/models/functions/math.py

https://gitlab.com/llndhlov/journly
Python | 183 lines | 118 code | 58 blank | 7 comment | 6 complexity | e293f4c92779057695776fa146008b7d MD5 | raw file
  1. import math
  2. from django.db.models.expressions import Func
  3. from django.db.models.fields import FloatField, IntegerField
  4. from django.db.models.functions import Cast
  5. from django.db.models.functions.mixins import (
  6. FixDecimalInputMixin, NumericOutputFieldMixin,
  7. )
  8. from django.db.models.lookups import Transform
  9. class Abs(Transform):
  10. function = 'ABS'
  11. lookup_name = 'abs'
  12. class ACos(NumericOutputFieldMixin, Transform):
  13. function = 'ACOS'
  14. lookup_name = 'acos'
  15. class ASin(NumericOutputFieldMixin, Transform):
  16. function = 'ASIN'
  17. lookup_name = 'asin'
  18. class ATan(NumericOutputFieldMixin, Transform):
  19. function = 'ATAN'
  20. lookup_name = 'atan'
  21. class ATan2(NumericOutputFieldMixin, Func):
  22. function = 'ATAN2'
  23. arity = 2
  24. def as_sqlite(self, compiler, connection, **extra_context):
  25. if not getattr(connection.ops, 'spatialite', False) or connection.ops.spatial_version >= (5, 0, 0):
  26. return self.as_sql(compiler, connection)
  27. # This function is usually ATan2(y, x), returning the inverse tangent
  28. # of y / x, but it's ATan2(x, y) on SpatiaLite < 5.0.0.
  29. # Cast integers to float to avoid inconsistent/buggy behavior if the
  30. # arguments are mixed between integer and float or decimal.
  31. # https://www.gaia-gis.it/fossil/libspatialite/tktview?name=0f72cca3a2
  32. clone = self.copy()
  33. clone.set_source_expressions([
  34. Cast(expression, FloatField()) if isinstance(expression.output_field, IntegerField)
  35. else expression for expression in self.get_source_expressions()[::-1]
  36. ])
  37. return clone.as_sql(compiler, connection, **extra_context)
  38. class Ceil(Transform):
  39. function = 'CEILING'
  40. lookup_name = 'ceil'
  41. def as_oracle(self, compiler, connection, **extra_context):
  42. return super().as_sql(compiler, connection, function='CEIL', **extra_context)
  43. class Cos(NumericOutputFieldMixin, Transform):
  44. function = 'COS'
  45. lookup_name = 'cos'
  46. class Cot(NumericOutputFieldMixin, Transform):
  47. function = 'COT'
  48. lookup_name = 'cot'
  49. def as_oracle(self, compiler, connection, **extra_context):
  50. return super().as_sql(compiler, connection, template='(1 / TAN(%(expressions)s))', **extra_context)
  51. class Degrees(NumericOutputFieldMixin, Transform):
  52. function = 'DEGREES'
  53. lookup_name = 'degrees'
  54. def as_oracle(self, compiler, connection, **extra_context):
  55. return super().as_sql(
  56. compiler, connection,
  57. template='((%%(expressions)s) * 180 / %s)' % math.pi,
  58. **extra_context
  59. )
  60. class Exp(NumericOutputFieldMixin, Transform):
  61. function = 'EXP'
  62. lookup_name = 'exp'
  63. class Floor(Transform):
  64. function = 'FLOOR'
  65. lookup_name = 'floor'
  66. class Ln(NumericOutputFieldMixin, Transform):
  67. function = 'LN'
  68. lookup_name = 'ln'
  69. class Log(FixDecimalInputMixin, NumericOutputFieldMixin, Func):
  70. function = 'LOG'
  71. arity = 2
  72. def as_sqlite(self, compiler, connection, **extra_context):
  73. if not getattr(connection.ops, 'spatialite', False):
  74. return self.as_sql(compiler, connection)
  75. # This function is usually Log(b, x) returning the logarithm of x to
  76. # the base b, but on SpatiaLite it's Log(x, b).
  77. clone = self.copy()
  78. clone.set_source_expressions(self.get_source_expressions()[::-1])
  79. return clone.as_sql(compiler, connection, **extra_context)
  80. class Mod(FixDecimalInputMixin, NumericOutputFieldMixin, Func):
  81. function = 'MOD'
  82. arity = 2
  83. class Pi(NumericOutputFieldMixin, Func):
  84. function = 'PI'
  85. arity = 0
  86. def as_oracle(self, compiler, connection, **extra_context):
  87. return super().as_sql(compiler, connection, template=str(math.pi), **extra_context)
  88. class Power(NumericOutputFieldMixin, Func):
  89. function = 'POWER'
  90. arity = 2
  91. class Radians(NumericOutputFieldMixin, Transform):
  92. function = 'RADIANS'
  93. lookup_name = 'radians'
  94. def as_oracle(self, compiler, connection, **extra_context):
  95. return super().as_sql(
  96. compiler, connection,
  97. template='((%%(expressions)s) * %s / 180)' % math.pi,
  98. **extra_context
  99. )
  100. class Random(NumericOutputFieldMixin, Func):
  101. function = 'RANDOM'
  102. arity = 0
  103. def as_mysql(self, compiler, connection, **extra_context):
  104. return super().as_sql(compiler, connection, function='RAND', **extra_context)
  105. def as_oracle(self, compiler, connection, **extra_context):
  106. return super().as_sql(compiler, connection, function='DBMS_RANDOM.VALUE', **extra_context)
  107. def as_sqlite(self, compiler, connection, **extra_context):
  108. return super().as_sql(compiler, connection, function='RAND', **extra_context)
  109. def get_group_by_cols(self, alias=None):
  110. return []
  111. class Round(Transform):
  112. function = 'ROUND'
  113. lookup_name = 'round'
  114. class Sign(Transform):
  115. function = 'SIGN'
  116. lookup_name = 'sign'
  117. class Sin(NumericOutputFieldMixin, Transform):
  118. function = 'SIN'
  119. lookup_name = 'sin'
  120. class Sqrt(NumericOutputFieldMixin, Transform):
  121. function = 'SQRT'
  122. lookup_name = 'sqrt'
  123. class Tan(NumericOutputFieldMixin, Transform):
  124. function = 'TAN'
  125. lookup_name = 'tan'