Represent a ``CASE`` expression. :class:`.Case` is produced using the :func:`.case` factory function, as in:: from sqlalchemy import case stmt = select(users_table).where( case( (users_table.c.name == "wendy", "W"), (users_table.
| 3581 | |
| 3582 | |
| 3583 | class Case(ColumnElement[_T]): |
| 3584 | """Represent a ``CASE`` expression. |
| 3585 | |
| 3586 | :class:`.Case` is produced using the :func:`.case` factory function, |
| 3587 | as in:: |
| 3588 | |
| 3589 | from sqlalchemy import case |
| 3590 | |
| 3591 | stmt = select(users_table).where( |
| 3592 | case( |
| 3593 | (users_table.c.name == "wendy", "W"), |
| 3594 | (users_table.c.name == "jack", "J"), |
| 3595 | else_="E", |
| 3596 | ) |
| 3597 | ) |
| 3598 | |
| 3599 | Details on :class:`.Case` usage is at :func:`.case`. |
| 3600 | |
| 3601 | .. seealso:: |
| 3602 | |
| 3603 | :func:`.case` |
| 3604 | |
| 3605 | """ |
| 3606 | |
| 3607 | __visit_name__ = "case" |
| 3608 | |
| 3609 | _traverse_internals: _TraverseInternalsType = [ |
| 3610 | ("value", InternalTraversal.dp_clauseelement), |
| 3611 | ("whens", InternalTraversal.dp_clauseelement_tuples), |
| 3612 | ("else_", InternalTraversal.dp_clauseelement), |
| 3613 | ] |
| 3614 | |
| 3615 | # for case(), the type is derived from the whens. so for the moment |
| 3616 | # users would have to cast() the case to get a specific type |
| 3617 | |
| 3618 | whens: List[typing_Tuple[ColumnElement[bool], ColumnElement[_T]]] |
| 3619 | else_: Optional[ColumnElement[_T]] |
| 3620 | value: Optional[ColumnElement[Any]] |
| 3621 | |
| 3622 | def __init__( |
| 3623 | self, |
| 3624 | *whens: Union[ |
| 3625 | typing_Tuple[_ColumnExpressionArgument[bool], Any], |
| 3626 | Mapping[Any, Any], |
| 3627 | ], |
| 3628 | value: Optional[Any] = None, |
| 3629 | else_: Optional[Any] = None, |
| 3630 | ): |
| 3631 | new_whens: Iterable[Any] = coercions._expression_collection_was_a_list( |
| 3632 | "whens", "case", whens |
| 3633 | ) |
| 3634 | try: |
| 3635 | new_whens = util.dictlike_iteritems(new_whens) |
| 3636 | except TypeError: |
| 3637 | pass |
| 3638 | |
| 3639 | self.whens = [ |
| 3640 | ( |