Produce a SQL statement that is cached as a lambda. The Python code object within the lambda is scanned for both Python literals that will become bound parameters as well as closure variables that refer to Core or ORM constructs that may vary. The lambda itself will be invoked onl
(
lmb: _StmtLambdaType,
enable_tracking: bool = True,
track_closure_variables: bool = True,
track_on: Optional[object] = None,
global_track_bound_values: bool = True,
track_bound_values: bool = True,
lambda_cache: Optional[_LambdaCacheType] = None,
)
| 79 | |
| 80 | |
| 81 | def lambda_stmt( |
| 82 | lmb: _StmtLambdaType, |
| 83 | enable_tracking: bool = True, |
| 84 | track_closure_variables: bool = True, |
| 85 | track_on: Optional[object] = None, |
| 86 | global_track_bound_values: bool = True, |
| 87 | track_bound_values: bool = True, |
| 88 | lambda_cache: Optional[_LambdaCacheType] = None, |
| 89 | ) -> StatementLambdaElement: |
| 90 | """Produce a SQL statement that is cached as a lambda. |
| 91 | |
| 92 | The Python code object within the lambda is scanned for both Python |
| 93 | literals that will become bound parameters as well as closure variables |
| 94 | that refer to Core or ORM constructs that may vary. The lambda itself |
| 95 | will be invoked only once per particular set of constructs detected. |
| 96 | |
| 97 | E.g.:: |
| 98 | |
| 99 | from sqlalchemy import lambda_stmt |
| 100 | |
| 101 | stmt = lambda_stmt(lambda: table.select()) |
| 102 | stmt += lambda s: s.where(table.c.id == 5) |
| 103 | |
| 104 | result = connection.execute(stmt) |
| 105 | |
| 106 | The object returned is an instance of :class:`_sql.StatementLambdaElement`. |
| 107 | |
| 108 | .. versionadded:: 1.4 |
| 109 | |
| 110 | :param lmb: a Python function, typically a lambda, which takes no arguments |
| 111 | and returns a SQL expression construct |
| 112 | :param enable_tracking: when False, all scanning of the given lambda for |
| 113 | changes in closure variables or bound parameters is disabled. Use for |
| 114 | a lambda that produces the identical results in all cases with no |
| 115 | parameterization. |
| 116 | :param track_closure_variables: when False, changes in closure variables |
| 117 | within the lambda will not be scanned. Use for a lambda where the |
| 118 | state of its closure variables will never change the SQL structure |
| 119 | returned by the lambda. |
| 120 | :param track_bound_values: when False, bound parameter tracking will |
| 121 | be disabled for the given lambda. Use for a lambda that either does |
| 122 | not produce any bound values, or where the initial bound values never |
| 123 | change. |
| 124 | :param global_track_bound_values: when False, bound parameter tracking |
| 125 | will be disabled for the entire statement including additional links |
| 126 | added via the :meth:`_sql.StatementLambdaElement.add_criteria` method. |
| 127 | :param lambda_cache: a dictionary or other mapping-like object where |
| 128 | information about the lambda's Python code as well as the tracked closure |
| 129 | variables in the lambda itself will be stored. Defaults |
| 130 | to a global LRU cache. This cache is independent of the "compiled_cache" |
| 131 | used by the :class:`_engine.Connection` object. |
| 132 | |
| 133 | .. seealso:: |
| 134 | |
| 135 | :ref:`engine_lambda_caching` |
| 136 | |
| 137 | |
| 138 | """ |