MCPcopy
hub / github.com/django/django / Coalesce

Class Coalesce

django/db/models/functions/comparison.py:70–101  ·  view source on GitHub ↗

Return, from left to right, the first non-null expression.

Source from the content-addressed store, hash-verified

68
69
70class Coalesce(Func):
71 """Return, from left to right, the first non-null expression."""
72
73 function = "COALESCE"
74
75 def __init__(self, *expressions, **extra):
76 if len(expressions) < 2:
77 raise ValueError("Coalesce must take at least two expressions")
78 super().__init__(*expressions, **extra)
79
80 @property
81 def empty_result_set_value(self):
82 for expression in self.get_source_expressions():
83 result = expression.empty_result_set_value
84 if result is NotImplemented or result is not None:
85 return result
86 return None
87
88 def as_oracle(self, compiler, connection, **extra_context):
89 # Oracle prohibits mixing TextField, JSONField (NCLOB) and CharField
90 # (NVARCHAR2), so convert all fields to NCLOB when that type is
91 # expected.
92 if self.output_field.get_internal_type() in ("TextField", "JSONField"):
93 clone = self.copy()
94 clone.set_source_expressions(
95 [
96 Func(expression, function="TO_NCLOB")
97 for expression in self.get_source_expressions()
98 ]
99 )
100 return super(Coalesce, clone).as_sql(compiler, connection, **extra_context)
101 return self.as_sql(compiler, connection, **extra_context)
102
103
104class Collate(Func):

Callers 15

as_sqlMethod · 0.90
resolve_expressionMethod · 0.90
_save_tableMethod · 0.90
validateMethod · 0.90
coalesceMethod · 0.90
test_basicMethod · 0.90
test_mixed_valuesMethod · 0.90
test_orderingMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_basicMethod · 0.72
test_mixed_valuesMethod · 0.72
test_orderingMethod · 0.72
test_empty_querysetMethod · 0.72
test_functionsMethod · 0.72