MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / bindparam

Function bindparam

lib/sqlalchemy/sql/_elements_constructors.py:527–791  ·  view source on GitHub ↗

r"""Produce a "bound expression". The return value is an instance of :class:`.BindParameter`; this is a :class:`_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 stat

(
    key: Optional[str],
    value: Any = _NoArg.NO_ARG,
    type_: Optional[_TypeEngineArgument[_T]] = None,
    unique: bool = False,
    required: Union[bool, Literal[_NoArg.NO_ARG]] = _NoArg.NO_ARG,
    quote: Optional[bool] = None,
    callable_: Optional[Callable[[], Any]] = None,
    expanding: bool = False,
    isoutparam: bool = False,
    literal_execute: bool = False,
)

Source from the content-addressed store, hash-verified

525
526
527def bindparam(
528 key: Optional[str],
529 value: Any = _NoArg.NO_ARG,
530 type_: Optional[_TypeEngineArgument[_T]] = None,
531 unique: bool = False,
532 required: Union[bool, Literal[_NoArg.NO_ARG]] = _NoArg.NO_ARG,
533 quote: Optional[bool] = None,
534 callable_: Optional[Callable[[], Any]] = None,
535 expanding: bool = False,
536 isoutparam: bool = False,
537 literal_execute: bool = False,
538) -> BindParameter[_T]:
539 r"""Produce a "bound expression".
540
541 The return value is an instance of :class:`.BindParameter`; this
542 is a :class:`_expression.ColumnElement`
543 subclass which represents a so-called
544 "placeholder" value in a SQL expression, the value of which is
545 supplied at the point at which the statement in executed against a
546 database connection.
547
548 In SQLAlchemy, the :func:`.bindparam` construct has
549 the ability to carry along the actual value that will be ultimately
550 used at expression time. In this way, it serves not just as
551 a "placeholder" for eventual population, but also as a means of
552 representing so-called "unsafe" values which should not be rendered
553 directly in a SQL statement, but rather should be passed along
554 to the :term:`DBAPI` as values which need to be correctly escaped
555 and potentially handled for type-safety.
556
557 When using :func:`.bindparam` explicitly, the use case is typically
558 one of traditional deferment of parameters; the :func:`.bindparam`
559 construct accepts a name which can then be referred to at execution
560 time::
561
562 from sqlalchemy import bindparam
563
564 stmt = select(users_table).where(
565 users_table.c.name == bindparam("username")
566 )
567
568 The above statement, when rendered, will produce SQL similar to:
569
570 .. sourcecode:: sql
571
572 SELECT id, name FROM user WHERE name = :username
573
574 In order to populate the value of ``:username`` above, the value
575 would typically be applied at execution time to a method
576 like :meth:`_engine.Connection.execute`::
577
578 result = connection.execute(stmt, {"username": "wendy"})
579
580 Explicit use of :func:`.bindparam` is also common when producing
581 UPDATE or DELETE statements that are to be invoked multiple times,
582 where the WHERE criterion of the statement is to change on each
583 invocation, such as::
584

Callers 15

insertMethod · 0.90
test_plain_selectMethod · 0.90
test_unionMethod · 0.90
test_update_5Method · 0.90
test_expanding_inMethod · 0.90

Calls 1

BindParameterClass · 0.85

Tested by 15

test_plain_selectMethod · 0.72
test_unionMethod · 0.72
test_update_5Method · 0.72
test_expanding_inMethod · 0.72