r"""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
(
self,
*cols: _OnlyColumnArgument[Any],
**types: _TypeEngineArgument[Any],
)
| 2458 | |
| 2459 | @util.preload_module("sqlalchemy.sql.selectable") |
| 2460 | def columns( |
| 2461 | self, |
| 2462 | *cols: _OnlyColumnArgument[Any], |
| 2463 | **types: _TypeEngineArgument[Any], |
| 2464 | ) -> TextualSelect: |
| 2465 | r"""Turn this :class:`_expression.TextClause` object into a |
| 2466 | :class:`_expression.TextualSelect` |
| 2467 | object that serves the same role as a SELECT |
| 2468 | statement. |
| 2469 | |
| 2470 | The :class:`_expression.TextualSelect` is part of the |
| 2471 | :class:`_expression.SelectBase` |
| 2472 | hierarchy and can be embedded into another statement by using the |
| 2473 | :meth:`_expression.TextualSelect.subquery` method to produce a |
| 2474 | :class:`.Subquery` |
| 2475 | object, which can then be SELECTed from. |
| 2476 | |
| 2477 | This function essentially bridges the gap between an entirely |
| 2478 | textual SELECT statement and the SQL expression language concept |
| 2479 | of a "selectable":: |
| 2480 | |
| 2481 | from sqlalchemy.sql import column, text |
| 2482 | |
| 2483 | stmt = text("SELECT id, name FROM some_table") |
| 2484 | stmt = stmt.columns(column("id"), column("name")).subquery("st") |
| 2485 | |
| 2486 | stmt = ( |
| 2487 | select(mytable) |
| 2488 | .select_from(mytable.join(stmt, mytable.c.name == stmt.c.name)) |
| 2489 | .where(stmt.c.id > 5) |
| 2490 | ) |
| 2491 | |
| 2492 | Above, we pass a series of :func:`_expression.column` elements to the |
| 2493 | :meth:`_expression.TextClause.columns` method positionally. These |
| 2494 | :func:`_expression.column` |
| 2495 | elements now become first class elements upon the |
| 2496 | :attr:`_expression.TextualSelect.selected_columns` column collection, |
| 2497 | which then |
| 2498 | become part of the :attr:`.Subquery.c` collection after |
| 2499 | :meth:`_expression.TextualSelect.subquery` is invoked. |
| 2500 | |
| 2501 | The column expressions we pass to |
| 2502 | :meth:`_expression.TextClause.columns` may |
| 2503 | also be typed; when we do so, these :class:`.TypeEngine` objects become |
| 2504 | the effective return type of the column, so that SQLAlchemy's |
| 2505 | result-set-processing systems may be used on the return values. |
| 2506 | This is often needed for types such as date or boolean types, as well |
| 2507 | as for unicode processing on some dialect configurations:: |
| 2508 | |
| 2509 | stmt = text("SELECT id, name, timestamp FROM some_table") |
| 2510 | stmt = stmt.columns( |
| 2511 | column("id", Integer), |
| 2512 | column("name", Unicode), |
| 2513 | column("timestamp", DateTime), |
| 2514 | ) |
| 2515 | |
| 2516 | for id, name, timestamp in connection.execute(stmt): |
| 2517 | print(id, name, timestamp) |
nothing calls this directly
no test coverage detected