Represent a composable SQL statement as a :class:`_sql.LambdaElement`. The :class:`_sql.StatementLambdaElement` is constructed using the :func:`_sql.lambda_stmt` function:: from sqlalchemy import lambda_stmt stmt = lambda_stmt(lambda: select(table)) Once constructed,
| 498 | |
| 499 | |
| 500 | class StatementLambdaElement( |
| 501 | roles.AllowsLambdaRole, LambdaElement, Executable |
| 502 | ): |
| 503 | """Represent a composable SQL statement as a :class:`_sql.LambdaElement`. |
| 504 | |
| 505 | The :class:`_sql.StatementLambdaElement` is constructed using the |
| 506 | :func:`_sql.lambda_stmt` function:: |
| 507 | |
| 508 | |
| 509 | from sqlalchemy import lambda_stmt |
| 510 | |
| 511 | stmt = lambda_stmt(lambda: select(table)) |
| 512 | |
| 513 | Once constructed, additional criteria can be built onto the statement |
| 514 | by adding subsequent lambdas, which accept the existing statement |
| 515 | object as a single parameter:: |
| 516 | |
| 517 | stmt += lambda s: s.where(table.c.col == parameter) |
| 518 | |
| 519 | .. versionadded:: 1.4 |
| 520 | |
| 521 | .. seealso:: |
| 522 | |
| 523 | :ref:`engine_lambda_caching` |
| 524 | |
| 525 | """ |
| 526 | |
| 527 | if TYPE_CHECKING: |
| 528 | |
| 529 | def __init__( |
| 530 | self, |
| 531 | fn: _StmtLambdaType, |
| 532 | role: Type[SQLRole], |
| 533 | opts: Union[Type[LambdaOptions], LambdaOptions] = LambdaOptions, |
| 534 | apply_propagate_attrs: Optional[ClauseElement] = None, |
| 535 | ): ... |
| 536 | |
| 537 | def __add__( |
| 538 | self, other: _StmtLambdaElementType[Any] |
| 539 | ) -> StatementLambdaElement: |
| 540 | return self.add_criteria(other) |
| 541 | |
| 542 | def add_criteria( |
| 543 | self, |
| 544 | other: _StmtLambdaElementType[Any], |
| 545 | enable_tracking: bool = True, |
| 546 | track_on: Optional[Any] = None, |
| 547 | track_closure_variables: bool = True, |
| 548 | track_bound_values: bool = True, |
| 549 | ) -> StatementLambdaElement: |
| 550 | """Add new criteria to this :class:`_sql.StatementLambdaElement`. |
| 551 | |
| 552 | E.g.:: |
| 553 | |
| 554 | >>> def my_stmt(parameter): |
| 555 | ... stmt = lambda_stmt( |
| 556 | ... lambda: select(table.c.x, table.c.y), |
| 557 | ... ) |