.. _sqlexpression_text:
-Using Text
-===========
+Using Textual SQL
+=================
Our last example really became a handful to type. Going from what one
understands to be a textual SQL expression into a Python construct which
using datatypes that need special handling as they are received in Python,
or we'd like to compose our :func:`~.expression.text` object into a larger
expression, we may also wish to use the :meth:`~.TextClause.columns` method
-in order to specify column return types and names.
+in order to specify column return types and names:
+
+.. sourcecode:: pycon+sql
+
+ >>> s = text(
+ ... "SELECT users.fullname || ', ' || addresses.email_address AS title "
+ ... "FROM users, addresses "
+ ... "WHERE users.id = addresses.user_id "
+ ... "AND users.name BETWEEN :x AND :y "
+ ... "AND (addresses.email_address LIKE :e1 "
+ ... "OR addresses.email_address LIKE :e2)")
+ >>> s = s.columns(title=String)
+ >>> s = s.bindparams(x='m', y='z', e1='%@aol.com', e2='%@msn.com')
+ >>> conn.execute(s).fetchall() # doctest:+NORMALIZE_WHITESPACE
+ SELECT users.fullname || ', ' || addresses.email_address AS title
+ FROM users, addresses
+ WHERE users.id = addresses.user_id AND users.name BETWEEN ? AND ? AND
+ (addresses.email_address LIKE ? OR addresses.email_address LIKE ?)
+ ('m', 'z', '%@aol.com', '%@msn.com')
+ {stop}[(u'Wendy Williams, wendy@aol.com',)]
+
:func:`~.expression.text` can also be used freely within a
:func:`~.expression.select` object, which accepts :func:`~.expression.text`
the less flexibility and ability for manipulation/transformation
the statement will have.
+.. seealso::
+
+ :ref:`orm_tutorial_literal_sql` - integrating ORM-level queries with
+ :func:`.text`
+
.. versionchanged:: 1.0.0
The :func:`.select` construct emits warnings when string SQL
fragments are coerced to :func:`.text`, and :func:`.text` should
.. _orm_tutorial_literal_sql:
-Using Literal SQL
+Using Textual SQL
-----------------
Literal strings can be used flexibly with
.. seealso::
- :ref:`sqlexpression_text` - Core description of textual segments. The
- behavior of the ORM :class:`.Query` object with regards to
- :func:`.text` and related constructs is very similar to that of the
- Core :func:`.select` object.
+ :ref:`sqlexpression_text` - The :func:`.text` construct explained
+ from the perspective of Core-only queries.
.. versionchanged:: 1.0.0
The :class:`.Query` construct emits warnings when string SQL