class BindParameter(roles.InElementRole, ColumnElement):
Known subclasses: sqlalchemy.sql.selectable._OffsetLimitParam
Represent a "bound expression".
.BindParameter
is invoked explicitly using the
.bindparam
function, as in:
from sqlalchemy import bindparam stmt = select(users_table).\ where(users_table.c.name == bindparam('username'))
Detailed discussion of how .BindParameter
is used is
at .bindparam
.
See Also
.bindparam
Method | __getstate__ |
execute a deferred value for serialization purposes. |
Method | __init__ |
Produce a "bound expression". |
Method | __repr__ |
Undocumented |
Method | __setstate__ |
Undocumented |
Method | _clone |
Create a shallow copy of this ClauseElement. |
Method | _convert_to_unique |
Undocumented |
Method | _gen_cache_key |
return an optional cache key. |
Method | _negate_in_binary |
a hook to allow the right side of a binary expression to respond to a negation of the binary expression. |
Method | _with_binary_element_type |
in the context of binary expression, convert the type of this object to the one given. |
Method | _with_value |
Return a copy of this .BindParameter with the given value set. |
Method | render_literal_execute |
Produce a copy of this bound parameter that will enable the :paramref:`_sql.BindParameter.literal_execute` flag. |
Class Variable | __visit_name__ |
Undocumented |
Class Variable | _is_bind_parameter |
Undocumented |
Class Variable | _traverse_internals |
Undocumented |
Class Variable | inherit_cache |
Indicate if this .HasCacheKey instance should make use of the cache key generation scheme used by its immediate superclass. |
Instance Variable | _identifying_key |
Undocumented |
Instance Variable | _is_crud |
Undocumented |
Instance Variable | _key_is_anon |
Undocumented |
Instance Variable | _orig_key |
Undocumented |
Instance Variable | callable |
Undocumented |
Instance Variable | expand_op |
Undocumented |
Instance Variable | expanding |
Undocumented |
Instance Variable | isoutparam |
Undocumented |
Instance Variable | key |
The 'key' that in some circumstances refers to this object in a Python namespace. |
Instance Variable | literal_execute |
Undocumented |
Instance Variable | required |
Undocumented |
Instance Variable | type |
Undocumented |
Instance Variable | unique |
Undocumented |
Instance Variable | value |
Undocumented |
Property | effective_value |
Return the value of this bound parameter, taking into account if the callable parameter was set. |
Inherited from InElementRole
:
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via InElementRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from ColumnElement
:
Method | cast |
Produce a type cast, i.e. CAST(<expression> AS <type>). |
Method | label |
Produce a column label, i.e. <columnname> AS <name>. |
Method | operate |
Operate on an argument. |
Method | reverse_operate |
Reverse operate on an argument. |
Method | self_group |
Apply a 'grouping' to this _expression.ClauseElement . |
Method | shares_lineage |
Return True if the given _expression.ColumnElement has a common ancestor to this _expression.ColumnElement . |
Class Variable | foreign_keys |
Undocumented |
Class Variable | primary_key |
Undocumented |
Method | __getattr__ |
Undocumented |
Method | _anon_label |
Undocumented |
Method | _bind_param |
Undocumented |
Method | _compare_name_for_result |
Return True if the given column element compares to this one when targeting within a result row. |
Method | _dedupe_anon_label_idx |
label to apply to a column that is anon labeled, but repeated in the SELECT, so that we have to make an "extra anon" label that disambiguates it from the previous appearance. |
Method | _dedupe_anon_tq_label_idx |
Undocumented |
Method | _expression_label |
a suggested label to use in the case that the column has no name, which should be used if possible as the explicit 'AS <label>' where this expression would normally have an anon label. |
Method | _make_proxy |
Create a new _expression.ColumnElement representing this _expression.ColumnElement as it appears in the select list of a descending selectable. |
Method | _negate |
Undocumented |
Method | _proxy_key |
Undocumented |
Method | _tq_key_label |
A label-based version of 'key' that in some circumstances refers to this object in a Python namespace. |
Method | _uncached_proxy_set |
An 'uncached' version of proxy set. |
Method | comparator |
Undocumented |
Class Variable | _allow_label_resolve |
A flag that can be flipped to prevent a column from being resolvable by string label name. |
Class Variable | _alt_names |
Undocumented |
Class Variable | _is_implicitly_boolean |
Undocumented |
Class Variable | _proxies |
Undocumented |
Class Variable | _render_label_in_columns_clause |
No summary |
Class Variable | _tq_label |
The named label that can be used to target this column in a result set in a "table qualified" context. |
Property | _anon_key_label |
Provides a constant 'anonymous key label' for this ColumnElement. |
Property | _anon_name_label |
Provides a constant 'anonymous label' for this ColumnElement. |
Property | _anon_tq_key_label |
Undocumented |
Property | _anon_tq_label |
Undocumented |
Property | _key_label |
legacy; renamed to _tq_key_label |
Property | _label |
legacy; renamed to _tq_label |
Property | _non_anon_label |
the 'name' that naturally applies this element when rendered in SQL. |
Property | _select_iterable |
Undocumented |
Property | anon_key_label |
Undocumented |
Property | anon_label |
Undocumented |
Property | base_columns |
Undocumented |
Property | expression |
Return a column expression. |
Property | proxy_set |
Undocumented |
Inherited from ColumnArgumentOrKeyRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, ColumnArgumentOrKeyRole
, ColumnArgumentRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from StatementOptionRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, StatementOptionRole
, StructuralRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from WhereHavingRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from AllowsLambdaRole
(via ColumnElement
, WhereHavingRole
, OnClauseRole
):
Class Variable | allows_lambda |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, WhereHavingRole
, OnClauseRole
, StructuralRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from BinaryElementRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, BinaryElementRole
, ExpressionElementRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from OrderByRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from AllowsLambdaRole
(via ColumnElement
, OrderByRole
):
Class Variable | allows_lambda |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, OrderByRole
, ByOfRole
, ColumnListRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from ColumnsClauseRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from AllowsLambdaRole
(via ColumnElement
, ColumnsClauseRole
):
Class Variable | allows_lambda |
Undocumented |
Inherited from UsesInspection
(via ColumnElement
, ColumnsClauseRole
):
Class Variable | _post_inspect |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, ColumnsClauseRole
, ColumnListRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from LimitOffsetRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, LimitOffsetRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from DMLColumnRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, DMLColumnRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from DDLConstraintColumnRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, DDLConstraintColumnRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from DDLExpressionRole
(via ColumnElement
):
Class Variable | _role_name |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, DDLExpressionRole
, StructuralRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from ColumnOperators
(via ColumnElement
):
Method | __add__ |
Implement the + operator. |
Method | __contains__ |
Undocumented |
Method | __div__ |
Implement the / operator. |
Method | __eq__ |
Implement the == operator. |
Method | __ge__ |
Implement the >= operator. |
Method | __getitem__ |
Implement the [] operator. |
Method | __gt__ |
Implement the > operator. |
Method | __le__ |
Implement the <= operator. |
Method | __lshift__ |
implement the << operator. |
Method | __lt__ |
Implement the < operator. |
Method | __mod__ |
Implement the % operator. |
Method | __mul__ |
Implement the * operator. |
Method | __ne__ |
Implement the != operator. |
Method | __neg__ |
Implement the - operator. |
Method | __radd__ |
Implement the + operator in reverse. |
Method | __rdiv__ |
Implement the / operator in reverse. |
Method | __rmod__ |
Implement the % operator in reverse. |
Method | __rmul__ |
Implement the * operator in reverse. |
Method | __rshift__ |
implement the >> operator. |
Method | __rsub__ |
Implement the - operator in reverse. |
Method | __rtruediv__ |
Implement the // operator in reverse. |
Method | __sub__ |
Implement the - operator. |
Method | __truediv__ |
Implement the // operator. |
Method | all_ |
Produce an _expression.all_ clause against the parent object. |
Method | any_ |
Produce an _expression.any_ clause against the parent object. |
Method | asc |
Produce a _expression.asc clause against the parent object. |
Method | between |
Produce a _expression.between clause against the parent object, given the lower and upper range. |
Method | collate |
Produce a _expression.collate clause against the parent object, given the collation string. |
Method | concat |
Implement the 'concat' operator. |
Method | contains |
Implement the 'contains' operator. |
Method | desc |
Produce a _expression.desc clause against the parent object. |
Method | distinct |
Produce a _expression.distinct clause against the parent object. |
Method | endswith |
Implement the 'endswith' operator. |
Method | ilike |
Implement the ilike operator, e.g. case insensitive LIKE. |
Method | in_ |
Implement the in operator. |
Method | is_ |
Implement the IS operator. |
Method | is_distinct_from |
Implement the IS DISTINCT FROM operator. |
Method | is_not |
Implement the IS NOT operator. |
Method | is_not_distinct_from |
Implement the IS NOT DISTINCT FROM operator. |
Method | like |
Implement the like operator. |
Method | match |
Implements a database-specific 'match' operator. |
Method | not_ilike |
implement the NOT ILIKE operator. |
Method | not_in |
implement the NOT IN operator. |
Method | not_like |
implement the NOT LIKE operator. |
Method | nulls_first |
Produce a _expression.nulls_first clause against the parent object. |
Method | nulls_last |
Produce a _expression.nulls_last clause against the parent object. |
Method | regexp_match |
Implements a database-specific 'regexp match' operator. |
Method | regexp_replace |
Implements a database-specific 'regexp replace' operator. |
Method | startswith |
Implement the startswith operator. |
Class Variable | __slots__ |
Undocumented |
Class Variable | timetuple |
Hack, allows datetime objects to be compared on the LHS. |
Inherited from Operators
(via ColumnElement
, ColumnOperators
):
Method | __and__ |
Implement the & operator. |
Method | __invert__ |
Implement the ~ operator. |
Method | __or__ |
Implement the | operator. |
Method | bool_op |
Return a custom boolean operator. |
Method | op |
Produce a generic operator function. |
Inherited from ClauseElement
(via ColumnElement
):
Method | compare |
Compare this _expression.ClauseElement to the given _expression.ClauseElement . |
Method | compile |
Compile this SQL expression. |
Method | params |
Return a copy with _expression.bindparam elements replaced. |
Method | unique_params |
Return a copy with _expression.bindparam elements replaced. |
Method | __bool__ |
Undocumented |
Method | __invert__ |
Undocumented |
Method | __str__ |
Undocumented |
Method | _cloned_set |
Return the set consisting all cloned ancestors of this ClauseElement. |
Method | _compile_w_cache |
Undocumented |
Method | _compiler |
Return a compiler appropriate for this ClauseElement, given a Dialect. |
Method | _execute_on_connection |
Undocumented |
Method | _replace_params |
Undocumented |
Method | _set_propagate_attrs |
Undocumented |
Method | _ungroup |
Return this _expression.ClauseElement without any groupings. |
Class Variable | _cache_key_traversal |
Undocumented |
Class Variable | _from_objects |
Undocumented |
Class Variable | _is_clause_list |
Undocumented |
Class Variable | _is_clone_of |
Undocumented |
Class Variable | _is_from_clause |
Undocumented |
Class Variable | _is_from_container |
Undocumented |
Class Variable | _is_immutable |
Undocumented |
Class Variable | _is_lambda_element |
Undocumented |
Class Variable | _is_returns_rows |
Undocumented |
Class Variable | _is_select_container |
Undocumented |
Class Variable | _is_select_statement |
Undocumented |
Class Variable | _is_singleton_constant |
Undocumented |
Class Variable | _is_text_clause |
Undocumented |
Class Variable | _is_textual |
Undocumented |
Class Variable | _order_by_label_element |
Undocumented |
Class Variable | bind |
Undocumented |
Class Variable | description |
Undocumented |
Class Variable | is_clause_element |
Undocumented |
Class Variable | is_selectable |
Undocumented |
Class Variable | stringify_dialect |
Undocumented |
Class Variable | supports_execution |
Undocumented |
Instance Variable | _propagate_attrs |
like annotations, however these propagate outwards liberally as SQL constructs are built, and are set up at construction time. |
Property | _constructor |
return the 'constructor' for this ClauseElement. |
Property | entity_namespace |
Undocumented |
Inherited from SQLRole
(via ColumnElement
, ClauseElement
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from SupportsWrappingAnnotations
(via ColumnElement
, ClauseElement
):
Method | _annotate |
return a copy of this ClauseElement with annotations updated by the given dictionary. |
Method | _deannotate |
return a copy of this _expression.ClauseElement with annotations removed. |
Method | _with_annotations |
return a copy of this ClauseElement with annotations replaced by the given dictionary. |
Inherited from SupportsAnnotations
(via ColumnElement
, ClauseElement
, SupportsWrappingAnnotations
):
Property | _annotations_cache_key |
Undocumented |
Inherited from MemoizedHasCacheKey
(via ColumnElement
, ClauseElement
):
Method | _generate_cache_key |
return a cache key. |
Inherited from HasCacheKey
(via ColumnElement
, ClauseElement
, MemoizedHasCacheKey
):
Class Method | _generate_cache_attrs |
generate cache key dispatcher for a new class. |
Class Method | _generate_cache_key_for_object |
Undocumented |
Class Variable | __slots__ |
Undocumented |
Class Variable | _hierarchy_supports_caching |
private attribute which may be set to False to prevent the inherit_cache warning from being emitted for a hierarchy of subclasses. |
Class Variable | _is_has_cache_key |
Undocumented |
Inherited from HasCopyInternals
(via ColumnElement
, ClauseElement
):
Method | _copy_internals |
Reassign internal elements to be clones of themselves. |
Inherited from Traversible
(via ColumnElement
, ClauseElement
):
Method | get_children |
Return immediate child .visitors.Traversible elements of this .visitors.Traversible . |
Method | __class_getitem__ |
Undocumented |
Produce a "bound expression".
The return value is an instance of .BindParameter
; this
is a _expression.ColumnElement
subclass which represents a so-called
"placeholder" value in a SQL expression, the value of which is
supplied at the point at which the statement in executed against a
database connection.
In SQLAlchemy, the .bindparam
construct has
the ability to carry along the actual value that will be ultimately
used at expression time. In this way, it serves not just as
a "placeholder" for eventual population, but also as a means of
representing so-called "unsafe" values which should not be rendered
directly in a SQL statement, but rather should be passed along
to the :term:`DBAPI` as values which need to be correctly escaped
and potentially handled for type-safety.
When using .bindparam
explicitly, the use case is typically
one of traditional deferment of parameters; the .bindparam
construct accepts a name which can then be referred to at execution
time:
from sqlalchemy import bindparam stmt = select(users_table).\ where(users_table.c.name == bindparam('username'))
The above statement, when rendered, will produce SQL similar to:
SELECT id, name FROM user WHERE name = :username
In order to populate the value of :username above, the value
would typically be applied at execution time to a method
like _engine.Connection.execute
:
result = connection.execute(stmt, username='wendy')
Explicit use of .bindparam
is also common when producing
UPDATE or DELETE statements that are to be invoked multiple times,
where the WHERE criterion of the statement is to change on each
invocation, such as:
stmt = (users_table.update(). where(user_table.c.name == bindparam('username')). values(fullname=bindparam('fullname')) ) connection.execute( stmt, [{"username": "wendy", "fullname": "Wendy Smith"}, {"username": "jack", "fullname": "Jack Jones"}, ] )
SQLAlchemy's Core expression system makes wide use of
.bindparam
in an implicit sense. It is typical that Python
literal values passed to virtually all SQL expression functions are
coerced into fixed .bindparam
constructs. For example, given
a comparison operation such as:
expr = users_table.c.name == 'Wendy'
The above expression will produce a .BinaryExpression
construct, where the left side is the _schema.Column
object
representing the name column, and the right side is a
.BindParameter
representing the literal value:
print(repr(expr.right)) BindParameter('%(4327771088 name)s', 'Wendy', type_=String())
The expression above will render SQL such as:
user.name = :name_1
Where the :name_1 parameter name is an anonymous name. The actual string Wendy is not in the rendered string, but is carried along where it is later used within statement execution. If we invoke a statement like the following:
stmt = select(users_table).where(users_table.c.name == 'Wendy') result = connection.execute(stmt)
We would see SQL logging output as:
SELECT "user".id, "user".name FROM "user" WHERE "user".name = %(name_1)s {'name_1': 'Wendy'}
Above, we see that Wendy is passed as a parameter to the database, while the placeholder :name_1 is rendered in the appropriate form for the target database, in this case the PostgreSQL database.
Similarly, .bindparam
is invoked automatically when working
with :term:`CRUD` statements as far as the "VALUES" portion is
concerned. The _expression.insert
construct produces an
INSERT expression which will, at statement execution time, generate
bound placeholders based on the arguments passed, as in:
stmt = users_table.insert() result = connection.execute(stmt, name='Wendy')
The above will produce SQL output as:
INSERT INTO "user" (name) VALUES (%(name)s) {'name': 'Wendy'}
The _expression.Insert
construct, at
compilation/execution time, rendered a single .bindparam
mirroring the column name name as a result of the single name
parameter we passed to the _engine.Connection.execute
method.
Parameters | |
key | the key (e.g. the name) for this bind param.
Will be used in the generated
SQL statement for dialects that use named parameters. This
value may be modified when part of a compilation operation,
if other BindParameter objects exist with the same
key, or if its length is too long and truncation is
required. |
value | Initial value for this bind param. Will be used at statement execution time as the value for this parameter passed to the DBAPI, if no other value is indicated to the statement execution method for this particular parameter name. Defaults to None. |
type_ | A The type of a |
unique | if True, the key name of this .BindParameter will be
modified if another .BindParameter of the same name
already has been located within the containing
expression. This flag is used generally by the internals
when producing so-called "anonymous" bound expressions, it
isn't generally applicable to explicitly-named .bindparam
constructs. |
required | If True, a value is required at execution time. If not passed, it defaults to True if neither :paramref:`.bindparam.value` or :paramref:`.bindparam.callable` were passed. If either of these parameters are present, then :paramref:`.bindparam.required` defaults to False. |
quote | True if this parameter name requires quoting and is not currently known as a SQLAlchemy reserved word; this currently only applies to the Oracle backend, where bound names must sometimes be quoted. |
callable_ | A callable function that takes the place of "value". The function will be called at statement execution time to determine the ultimate value. Used for scenarios where the actual bind value cannot be determined at the point at which the clause construct is created, but embedded bind values are still desirable. |
expanding | if True, this parameter will be treated as an "expanding" parameter at execution time; the parameter value is expected to be a sequence, rather than a scalar value, and the string SQL statement will be transformed on a per-execution basis to accommodate the sequence with a variable number of parameter slots passed to the DBAPI. This is to allow statement caching to be used in conjunction with an IN clause. Note The "expanding" feature does not support "executemany"- style parameter sets.
New in version 1.2.
Changed in version 1.3: the "expanding" bound parameter feature now
supports empty lists.
|
isoutparam | if True, the parameter should be treated like a stored procedure "OUT" parameter. This applies to backends such as Oracle which support OUT parameters. |
literal_execute | if True, the bound parameter will be rendered in the compile phase with a special "POSTCOMPILE" token, and the SQLAlchemy compiler will render the final value of the parameter into the SQL statement at statement execution time, omitting the value from the parameter dictionary / list passed to DBAPI cursor.execute(). This produces a similar effect as that of using the literal_binds, compilation flag, however takes place as the statement is sent to the DBAPI cursor.execute() method, rather than when the statement is compiled. The primary use of this capability is for rendering LIMIT / OFFSET clauses for database drivers that can't accommodate for bound parameters in these contexts, while allowing SQL constructs to be cacheable at the compilation level. New in version 1.4: Added "post compile" bound parameters See Also |
_compared_to_operator | Undocumented |
_compared_to_type | Undocumented |
_is_crud | Undocumented |
Create a shallow copy of this ClauseElement.
This method may be used by a generative API. Its also used as part of the "deep" copy afforded by a traversal that combines the _copy_internals() method.
return an optional cache key.
The cache key is a tuple which can contain any series of objects that are hashable and also identifies this object uniquely within the presence of a larger SQL expression or statement, for the purposes of caching the resulting query.
The cache key should be based on the SQL compiled structure that would ultimately be produced. That is, two structures that are composed in exactly the same way should produce the same cache key; any difference in the structures that would affect the SQL string or the type handlers should result in a different cache key.
If a structure cannot produce a useful cache key, the NO_CACHE symbol should be added to the anon_map and the method should return None.
a hook to allow the right side of a binary expression to respond to a negation of the binary expression.
Used for the special case of expanding bind parameter with IN.
in the context of binary expression, convert the type of this object to the one given.
applies only to _expression.ColumnElement
classes.
Produce a copy of this bound parameter that will enable the :paramref:`_sql.BindParameter.literal_execute` flag.
The :paramref:`_sql.BindParameter.literal_execute` flag will have the effect of the parameter rendered in the compiled SQL string using [POSTCOMPILE] form, which is a special form that is converted to be a rendering of the literal value of the parameter at SQL execution time. The rationale is to support caching of SQL statement strings that can embed per-statement literal values, such as LIMIT and OFFSET parameters, in the final SQL string that is passed to the DBAPI. Dialects in particular may want to use this method within custom compilation schemes.
See Also
bool
=
sqlalchemy.sql.selectable._OffsetLimitParam
Indicate if this .HasCacheKey
instance should make use of the
cache key generation scheme used by its immediate superclass.
The attribute defaults to None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value to False, except that a warning is also emitted.
This flag can be set to True on a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.
See Also
:ref:`compilerext_caching` - General guideslines for setting the
.HasCacheKey.inherit_cache
attribute for third-party or user
defined SQL constructs.