module documentation

Defines the public namespace for SQL expression constructs.

Prior to version 0.9, this module contained all of "elements", "dml", "default_comparator" and "selectable". The module was broken up and most "factory" functions were moved to be grouped with their associated class.

Class ​Alias Represents an table or selectable alias (AS).
Class ​Aliased​Returns​Rows Base class of aliases against tables, subqueries, and other selectables.
Class ​Cache​Key The key used to identify a SQL statement construct in the SQL compilation cache.
Class ​Clause​Element Base class for elements of a programmatically constructed SQL expression.
Class ​Column​Collection Collection of _expression.ColumnElement instances, typically for _sql.FromClause objects.
Class ​Column​Element Represent a column-oriented SQL expression suitable for usage in the "columns" clause, WHERE clause etc. of a statement.
Class ​Compound​Select Forms the basis of UNION, UNION ALL, and other SELECT-based set operations.
Class custom​_op Represent a 'custom' operator.
Class ​Delete Represent a DELETE construct.
Class ​From​Clause Represent an element that can be used within the FROM clause of a SELECT statement.
Class ​Insert Represent an INSERT construct.
Class ​Join Represent a JOIN construct between two _expression.FromClause elements.
Class ​Lambda​Element A SQL construct where the state is stored as an un-invoked lambda.
Class ​Lateral Represent a LATERAL subquery.
Class quoted​_name Represent a SQL identifier combined with quoting preferences.
Class ​Select Represents a SELECT statement.
Class ​Selectable Mark a class as being selectable.
Class ​Statement​Lambda​Element Represent a composable SQL statement as a _sql.LambdaElement.
Class ​Subquery Represent a subquery of a SELECT.
Class ​Table​Clause Represents a minimal "table" construct.
Class ​Table​Sample Represent a TABLESAMPLE clause.
Class ​Table​Valued​Alias An alias against a "table valued" SQL function.
Class ​Update Represent an Update construct.
Class ​Values Represent a VALUES construct that can be used as a FROM element in a statement.
Function between Produce a BETWEEN predicate clause.
Function collate Return the clause expression COLLATE collation.
Function lambda​_stmt Produce a SQL statement that is cached as a lambda.
Function literal Return a literal clause, bound to a bind parameter.
Function literal​_column Produce a .ColumnClause object that has the :paramref:`_expression.column.is_literal` flag set to True.
Function not​_ Return a negation of the given clause, i.e. NOT(clause).
Function outparam Create an 'OUT' parameter for usage in functions (stored procedures), for databases which support them.
Variable alias Undocumented
Variable all​_ Undocumented
Variable and​_ Undocumented
Variable any​_ Undocumented
Variable asc Undocumented
Variable bindparam Undocumented
Variable case Undocumented
Variable cast Undocumented
Variable column Undocumented
Variable cte Undocumented
Variable delete Undocumented
Variable desc Undocumented
Variable distinct Undocumented
Variable except​_ Undocumented
Variable except​_all Undocumented
Variable exists Undocumented
Variable extract Undocumented
Variable false Undocumented
Variable func Undocumented
Variable funcfilter Undocumented
Variable insert Undocumented
Variable intersect Undocumented
Variable intersect​_all Undocumented
Variable join Undocumented
Variable label Undocumented
Variable lateral Undocumented
Variable modifier Undocumented
Variable null Undocumented
Variable nulls​_first Undocumented
Variable nulls​_last Undocumented
Variable or​_ Undocumented
Variable outerjoin Undocumented
Variable over Undocumented
Variable select Undocumented
Variable table Undocumented
Variable tablesample Undocumented
Variable text Undocumented
Variable true Undocumented
Variable tuple​_ Undocumented
Variable type​_coerce Undocumented
Variable union Undocumented
Variable union​_all Undocumented
Variable update Undocumented
Variable values Undocumented
Variable within​_group Undocumented
def between(expr, lower_bound, upper_bound, symmetric=False):

Produce a BETWEEN predicate clause.

E.g.:

from sqlalchemy import between
stmt = select(users_table).where(between(users_table.c.id, 5, 7))

Would produce SQL resembling:

SELECT id, name FROM user WHERE id BETWEEN :id_1 AND :id_2

The .between function is a standalone version of the _expression.ColumnElement.between method available on all SQL expressions, as in:

stmt = select(users_table).where(users_table.c.id.between(5, 7))

All arguments passed to .between, including the left side column expression, are coerced from Python scalar values if a the value is not a _expression.ColumnElement subclass. For example, three fixed values can be compared as in:

print(between(5, 3, 7))

Which would produce:

:param_1 BETWEEN :param_2 AND :param_3

See Also

_expression.ColumnElement.between

Parameters
expra column expression, typically a _expression.ColumnElement instance or alternatively a Python scalar expression to be coerced into a column expression, serving as the left side of the BETWEEN expression.
lower​_bounda column or Python scalar expression serving as the lower bound of the right side of the BETWEEN expression.
upper​_bounda column or Python scalar expression serving as the upper bound of the right side of the BETWEEN expression.
symmetric

if True, will render " BETWEEN SYMMETRIC ". Note that not all databases support this syntax.

New in version 0.9.5.
def collate(expression, collation):

Return the clause expression COLLATE collation.

e.g.:

collate(mycolumn, 'utf8_bin')

produces:

mycolumn COLLATE utf8_bin

The collation expression is also quoted if it is a case sensitive identifier, e.g. contains uppercase characters.

Changed in version 1.2: quoting is automatically applied to COLLATE expressions if they are case sensitive.
def lambda_stmt(lmb, enable_tracking=True, track_closure_variables=True, track_on=None, global_track_bound_values=True, track_bound_values=True, lambda_cache=None):

Produce a SQL statement that is cached as a lambda.

The Python code object within the lambda is scanned for both Python literals that will become bound parameters as well as closure variables that refer to Core or ORM constructs that may vary. The lambda itself will be invoked only once per particular set of constructs detected.

E.g.:

from sqlalchemy import lambda_stmt

stmt = lambda_stmt(lambda: table.select())
stmt += lambda s: s.where(table.c.id == 5)

result = connection.execute(stmt)

The object returned is an instance of _sql.StatementLambdaElement.

New in version 1.4.
Parameters
lmba Python function, typically a lambda, which takes no arguments and returns a SQL expression construct
enable​_trackingwhen False, all scanning of the given lambda for changes in closure variables or bound parameters is disabled. Use for a lambda that produces the identical results in all cases with no parameterization.
track​_closure​_variableswhen False, changes in closure variables within the lambda will not be scanned. Use for a lambda where the state of its closure variables will never change the SQL structure returned by the lambda.
track​_onUndocumented
global​_track​_bound​_valueswhen False, bound parameter tracking will be disabled for the entire statement including additional links added via the _sql.StatementLambdaElement.add_criteria method.
track​_bound​_valueswhen False, bound parameter tracking will be disabled for the given lambda. Use for a lambda that either does not produce any bound values, or where the initial bound values never change.
lambda​_cachea dictionary or other mapping-like object where information about the lambda's Python code as well as the tracked closure variables in the lambda itself will be stored. Defaults to a global LRU cache. This cache is independent of the "compiled_cache" used by the _engine.Connection object.
def literal(value, type_=None):

Return a literal clause, bound to a bind parameter.

Literal clauses are created automatically when non- _expression.ClauseElement objects (such as strings, ints, dates, etc.) are used in a comparison operation with a _expression.ColumnElement subclass, such as a ~sqlalchemy.schema.Column object. Use this function to force the generation of a literal clause, which will be created as a BindParameter with a bound value.

Parameters
valuethe value to be bound. Can be any Python object supported by the underlying DB-API, or is translatable via the given type argument.
type​_an optional ~sqlalchemy.types.TypeEngine which will provide bind-parameter translation for this literal.
def literal_column(text, type_=None):

Produce a .ColumnClause object that has the :paramref:`_expression.column.is_literal` flag set to True.

_expression.literal_column is similar to _expression.column, except that it is more often used as a "standalone" column expression that renders exactly as stated; while _expression.column stores a string name that will be assumed to be part of a table and may be quoted as such, _expression.literal_column can be that, or any other arbitrary column-oriented expression.

See Also

_expression.column

_expression.text

:ref:`sqlexpression_literal_column`

Parameters
textthe text of the expression; can be any SQL expression. Quoting rules will not be applied. To specify a column-name expression which should be subject to quoting rules, use the column function.
type​_an optional ~sqlalchemy.types.TypeEngine object which will provide result-set translation and additional expression semantics for this column. If left as None the type will be .NullType.
def not_(clause):

Return a negation of the given clause, i.e. NOT(clause).

The ~ operator is also overloaded on all _expression.ColumnElement subclasses to produce the same result.

def outparam(key, type_=None):

Create an 'OUT' parameter for usage in functions (stored procedures), for databases which support them.

The outparam can be used like a regular function parameter. The "output" value will be available from the ~sqlalchemy.engine.CursorResult object via its out_parameters attribute, which returns a dictionary containing the values.

alias =

Undocumented

all_ =

Undocumented

and_ =

Undocumented

any_ =

Undocumented

asc =

Undocumented

bindparam =

Undocumented

case =

Undocumented

cast =

Undocumented

column =

Undocumented

cte =

Undocumented

delete =

Undocumented

desc =

Undocumented

distinct =

Undocumented

except_ =

Undocumented

except_all =

Undocumented

exists =

Undocumented

extract =

Undocumented

false =

Undocumented

func =

Undocumented

funcfilter =

Undocumented

insert =

Undocumented

intersect =

Undocumented

intersect_all =

Undocumented

join =

Undocumented

label =

Undocumented

lateral =

Undocumented

modifier =

Undocumented

null =

Undocumented

nulls_first =

Undocumented

nulls_last =

Undocumented

or_ =

Undocumented

outerjoin =

Undocumented

over =

Undocumented

select =

Undocumented

table =

Undocumented

tablesample =

Undocumented

text =

Undocumented

true =

Undocumented

tuple_ =

Undocumented

type_coerce =

Undocumented

union =

Undocumented

union_all =

Undocumented

update =

Undocumented

values =

Undocumented

within_group =

Undocumented