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.
| 3298 | |
| 3299 | |
| 3300 | class Case(ColumnElement[_T]): |
| 3301 | """Represent a ``CASE`` expression. |
| 3302 | |
| 3303 | :class:`.Case` is produced using the :func:`.case` factory function, |
| 3304 | as in:: |
| 3305 | |
| 3306 | from sqlalchemy import case |
| 3307 | |
| 3308 | stmt = select(users_table).where( |
| 3309 | case( |
| 3310 | (users_table.c.name == "wendy", "W"), |
| 3311 | (users_table.c.name == "jack", "J"), |
| 3312 | else_="E", |
| 3313 | ) |
| 3314 | ) |
| 3315 | |
| 3316 | Details on :class:`.Case` usage is at :func:`.case`. |
| 3317 | |
| 3318 | .. seealso:: |
| 3319 | |
| 3320 | :func:`.case` |
| 3321 | |
| 3322 | """ |
| 3323 | |
| 3324 | __visit_name__ = "case" |
| 3325 | |
| 3326 | _traverse_internals: _TraverseInternalsType = [ |
| 3327 | ("value", InternalTraversal.dp_clauseelement), |
| 3328 | ("whens", InternalTraversal.dp_clauseelement_tuples), |
| 3329 | ("else_", InternalTraversal.dp_clauseelement), |
| 3330 | ] |
| 3331 | |
| 3332 | # for case(), the type is derived from the whens. so for the moment |
| 3333 | # users would have to cast() the case to get a specific type |
| 3334 | |
| 3335 | whens: List[typing_Tuple[ColumnElement[bool], ColumnElement[_T]]] |
| 3336 | else_: Optional[ColumnElement[_T]] |
| 3337 | value: Optional[ColumnElement[Any]] |
| 3338 | |
| 3339 | def __init__( |
| 3340 | self, |
| 3341 | *whens: Union[ |
| 3342 | typing_Tuple[_ColumnExpressionArgument[bool], Any], |
| 3343 | Mapping[Any, Any], |
| 3344 | ], |
| 3345 | value: Optional[Any] = None, |
| 3346 | else_: Optional[Any] = None, |
| 3347 | ): |
| 3348 | new_whens: Iterable[Any] = coercions._expression_collection_was_a_list( |
| 3349 | "whens", "case", whens |
| 3350 | ) |
| 3351 | try: |
| 3352 | new_whens = util.dictlike_iteritems(new_whens) |
| 3353 | except TypeError: |
| 3354 | pass |
| 3355 | |
| 3356 | self.whens = [ |
| 3357 | ( |