MCPcopy
hub / github.com/tortoise/tortoise-orm / Case

Class Case

tortoise/expressions.py:704–734  ·  view source on GitHub ↗

Case expression. :param args: When objects :param default: value for 'CASE WHEN ... THEN ... ELSE END'

Source from the content-addressed store, hash-verified

702
703
704class Case(Expression):
705 """
706 Case expression.
707
708 :param args: When objects
709 :param default: value for 'CASE WHEN ... THEN ... ELSE <default> END'
710 """
711
712 def __init__(
713 self,
714 *args: When,
715 default: str | F | CombinedExpression | Function | None = None,
716 ) -> None:
717 self.args = args
718 self.default = default
719
720 def resolve(self, resolve_context: ResolveContext) -> ResolveResult:
721 case = PypikaCase()
722 for arg in self.args:
723 if not isinstance(arg, When):
724 raise TypeError("expected When objects as args")
725 when = arg.resolve(resolve_context)
726 when_term = cast(_WhenThen, when.term)
727 case = case.when(when_term.when, when_term.then)
728
729 if isinstance(self.default, Expression):
730 case = case.else_(self.default.resolve(resolve_context).term)
731 else:
732 case = case.else_(Term.wrap_constant(self.default))
733
734 return ResolveResult(term=case)

Callers 15

test_single_whenFunction · 0.90
test_multi_whenFunction · 0.90
test_q_object_whenFunction · 0.90
test_F_thenFunction · 0.90
test_AE_thenFunction · 0.90
test_func_thenFunction · 0.90

Calls

no outgoing calls

Tested by 15

test_single_whenFunction · 0.72
test_multi_whenFunction · 0.72
test_q_object_whenFunction · 0.72
test_F_thenFunction · 0.72
test_AE_thenFunction · 0.72
test_func_thenFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…