MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / columns

Method columns

lib/sqlalchemy/sql/elements.py:2401–2540  ·  view source on GitHub ↗

r"""Turn this :class:`_expression.AbstractTextClause` 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` hierar

(
        self,
        *cols: _OnlyColumnArgument[Any],
        **types: _TypeEngineArgument[Any],
    )

Source from the content-addressed store, hash-verified

2399
2400 @util.preload_module("sqlalchemy.sql.selectable")
2401 def columns(
2402 self,
2403 *cols: _OnlyColumnArgument[Any],
2404 **types: _TypeEngineArgument[Any],
2405 ) -> TextualSelect:
2406 r"""Turn this :class:`_expression.AbstractTextClause` object into a
2407 :class:`_expression.TextualSelect`
2408 object that serves the same role as a SELECT
2409 statement.
2410
2411 The :class:`_expression.TextualSelect` is part of the
2412 :class:`_expression.SelectBase`
2413 hierarchy and can be embedded into another statement by using the
2414 :meth:`_expression.TextualSelect.subquery` method to produce a
2415 :class:`.Subquery`
2416 object, which can then be SELECTed from.
2417
2418 This function essentially bridges the gap between an entirely
2419 textual SELECT statement and the SQL expression language concept
2420 of a "selectable"::
2421
2422 from sqlalchemy.sql import column, text
2423
2424 stmt = text("SELECT id, name FROM some_table")
2425 stmt = stmt.columns(column("id"), column("name")).subquery("st")
2426
2427 stmt = (
2428 select(mytable)
2429 .select_from(mytable.join(stmt, mytable.c.name == stmt.c.name))
2430 .where(stmt.c.id > 5)
2431 )
2432
2433 Above, we pass a series of :func:`_expression.column` elements to the
2434 :meth:`_expression.AbstractTextClause.columns` method positionally.
2435 These :func:`_expression.column` elements now become first class
2436 elements upon the :attr:`_expression.TextualSelect.selected_columns`
2437 column collection, which then become part of the :attr:`.Subquery.c`
2438 collection after :meth:`_expression.TextualSelect.subquery` is invoked.
2439
2440 The column expressions we pass to
2441 :meth:`_expression.AbstractTextClause.columns` may also be typed; when
2442 we do so, these :class:`.TypeEngine` objects become the effective
2443 return type of the column, so that SQLAlchemy's result-set-processing
2444 systems may be used on the return values. This is often needed for
2445 types such as date or boolean types, as well as for unicode processing
2446 on some dialect configurations::
2447
2448 stmt = text("SELECT id, name, timestamp FROM some_table")
2449 stmt = stmt.columns(
2450 column("id", Integer),
2451 column("name", Unicode),
2452 column("timestamp", DateTime),
2453 )
2454
2455 for id, name, timestamp in connection.execute(stmt):
2456 print(id, name, timestamp)
2457
2458 As a shortcut to the above syntax, keyword arguments referring to

Callers

nothing calls this directly

Calls 5

ColumnClauseClass · 0.85
popMethod · 0.45
itemsMethod · 0.45
__new__Method · 0.45
_initMethod · 0.45

Tested by

no test coverage detected