Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les fondamentaux
Voir le programme détaillé
Classe « TextClause »

Méthode sqlalchemy.TextClause.columns

Signature de la méthode columns

def columns(self, *cols: '_ColumnExpressionArgument[Any]', **types: '_TypeEngineArgument[Any]') -> 'TextualSelect' 

Description

help(TextClause.columns)

Turn this :class:`_expression.TextClause` object into a
:class:`_expression.TextualSelect`
object that serves the same role as a SELECT
statement.

The :class:`_expression.TextualSelect` is part of the
:class:`_expression.SelectBase`
hierarchy and can be embedded into another statement by using the
:meth:`_expression.TextualSelect.subquery` method to produce a
:class:`.Subquery`
object, which can then be SELECTed from.

This function essentially bridges the gap between an entirely
textual SELECT statement and the SQL expression language concept
of a "selectable"::

    from sqlalchemy.sql import column, text

    stmt = text("SELECT id, name FROM some_table")
    stmt = stmt.columns(column("id"), column("name")).subquery("st")

    stmt = (
        select(mytable)
        .select_from(mytable.join(stmt, mytable.c.name == stmt.c.name))
        .where(stmt.c.id > 5)
    )

Above, we pass a series of :func:`_expression.column` elements to the
:meth:`_expression.TextClause.columns` method positionally.  These
:func:`_expression.column`
elements now become first class elements upon the
:attr:`_expression.TextualSelect.selected_columns` column collection,
which then
become part of the :attr:`.Subquery.c` collection after
:meth:`_expression.TextualSelect.subquery` is invoked.

The column expressions we pass to
:meth:`_expression.TextClause.columns` may
also be typed; when we do so, these :class:`.TypeEngine` objects become
the effective return type of the column, so that SQLAlchemy's
result-set-processing systems may be used on the return values.
This is often needed for types such as date or boolean types, as well
as for unicode processing on some dialect configurations::

    stmt = text("SELECT id, name, timestamp FROM some_table")
    stmt = stmt.columns(
        column("id", Integer),
        column("name", Unicode),
        column("timestamp", DateTime),
    )

    for id, name, timestamp in connection.execute(stmt):
        print(id, name, timestamp)

As a shortcut to the above syntax, keyword arguments referring to
types alone may be used, if only type conversion is needed::

    stmt = text("SELECT id, name, timestamp FROM some_table")
    stmt = stmt.columns(id=Integer, name=Unicode, timestamp=DateTime)

    for id, name, timestamp in connection.execute(stmt):
        print(id, name, timestamp)

The positional form of :meth:`_expression.TextClause.columns`
also provides the
unique feature of **positional column targeting**, which is
particularly useful when using the ORM with complex textual queries. If
we specify the columns from our model to
:meth:`_expression.TextClause.columns`,
the result set will match to those columns positionally, meaning the
name or origin of the column in the textual SQL doesn't matter::

    stmt = text(
        "SELECT users.id, addresses.id, users.id, "
        "users.name, addresses.email_address AS email "
        "FROM users JOIN addresses ON users.id=addresses.user_id "
        "WHERE users.id = 1"
    ).columns(
        User.id,
        Address.id,
        Address.user_id,
        User.name,
        Address.email_address,
    )

    query = (
        session.query(User)
        .from_statement(stmt)
        .options(contains_eager(User.addresses))
    )

The :meth:`_expression.TextClause.columns` method provides a direct
route to calling :meth:`_expression.FromClause.subquery` as well as
:meth:`_expression.SelectBase.cte`
against a textual SELECT statement::

    stmt = stmt.columns(id=Integer, name=String).cte("st")

    stmt = select(sometable).where(sometable.c.id == stmt.c.id)

:param \*cols: A series of :class:`_expression.ColumnElement` objects,
 typically
 :class:`_schema.Column` objects from a :class:`_schema.Table`
 or ORM level
 column-mapped attributes, representing a set of columns that this
 textual string will SELECT from.

:param \**types: A mapping of string names to :class:`.TypeEngine`
 type objects indicating the datatypes to use for names that are
 SELECTed from the textual string.  Prefer to use the ``*cols``
 argument as it also indicates positional ordering.



Vous êtes un professionnel et vous avez besoin d'une formation ? Sensibilisation à
l'Intelligence Artificielle
Voir le programme détaillé