Defines the base components of SQL expression trees.
All components are derived from a common base class ClauseElement. Common behaviors are organized based on class hierarchies, in some cases via mixins.
All object construction from this package occurs via functions which in some cases will construct composite ClauseElement structures together, and in other cases simply return a single ClauseElement constructed directly. The function interface affords a more "DSL-ish" feel to constructing SQL expressions and also allows future class reorganizations.
Even though classes are not constructed directly from the outside, most classes which have additional public methods are considered to be public (i.e. have no leading underscore). Other classes which are "semi-public" are marked with a single leading underscore; these classes usually have few or no public methods and are less guaranteed to stay the same in future releases.
Return an Alias object.
An Alias represents any FromClause with an alternate name assigned within SQL, typically using the AS clause when generated, e.g. SELECT * FROM table AS aliasname.
Similar functionality is available via the alias() method available on all FromClause subclasses.
- selectable
- any FromClause subclass, such as a table, select statement, etc..
- alias
- string name to be assigned as the alias. If None, a random name will be generated.
Join a list of clauses together using the AND operator.
The & operator is also overloaded on all _CompareMixin subclasses to produce the same result.
Return a BETWEEN predicate clause.
Equivalent of SQL clausetest BETWEEN clauseleft AND clauseright.
The between() method on all _CompareMixin subclasses provides similar functionality.
Create a bind parameter clause with the given key.
Produce a CASE statement.
Return a CAST function.
Equivalent of SQL CAST(clause AS totype).
Use with a TypeEngine subclass, i.e:
cast(table.c.unit_price * table.c.qty, Numeric(10,4))
or:
cast(table.c.timestamp, DATE)
Return a textual column clause, as would be in the columns clause of a SELECT statement.
The object returned is an instance of _ColumnClause, which represents the "syntactical" portion of the schema-level Column object.
Return an EXCEPT of multiple selectables.
The returned object is an instance of CompoundSelect.
Return an EXCEPT ALL of multiple selectables.
The returned object is an instance of CompoundSelect.
Return an Insert clause element.
Similar functionality is available via the insert() method on Table.
If both values and compile-time bind parameters are present, the compile-time bind parameters override the information specified within values on a per-key basis.
The keys within values can be either Column objects or their string identifiers. Each key may reference one of:
If a SELECT statement is specified which references this INSERT statement's table, the statement will be correlated against the INSERT statement.
Return an INTERSECT of multiple selectables.
The returned object is an instance of CompoundSelect.
Return an INTERSECT ALL of multiple selectables.
The returned object is an instance of CompoundSelect.
Return a JOIN clause element (regular inner join).
The returned object is an instance of Join.
Similar functionality is also available via the join() method on any FromClause.
To chain joins together, use the join() or outerjoin() methods on the resulting Join object.
Return a _Label object for the given ColumnElement.
A label changes the name of an element in the columns clause of a SELECT statement, typically via the AS SQL keyword.
This functionality is more conveniently available via the label() method on ColumnElement.
Return a literal clause, bound to a bind parameter.
Literal clauses are created automatically when non- ClauseElement objects (such as strings, ints, dates, etc.) are used in a comparison operation with a _CompareMixin subclass, such as a Column object. Use this function to force the generation of a literal clause, which will be created as a _BindParamClause with a bound value.
Return a textual column expression, as would be in the columns clause of a SELECT statement.
The object returned supports further expressions in the same way as any other column object, including comparison, math and string operations. The type_ parameter is important to determine proper expression behavior (such as, '+' means string concatenation or numerical addition based on the type).
Return a negation of the given clause, i.e. NOT(clause).
The ~ operator is also overloaded on all _CompareMixin subclasses to produce the same result.
Join a list of clauses together using the OR operator.
The | operator is also overloaded on all _CompareMixin subclasses to produce the same result.
Return an OUTER JOIN clause element.
The returned object is an instance of Join.
Similar functionality is also available via the outerjoin() method on any FromClause.
To chain joins together, use the join() or outerjoin() methods on the resulting Join object.
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 ResultProxy object via its out_parameters attribute, which returns a dictionary containing the values.
Returns a SELECT clause element.
Similar functionality is also available via the select() method on any FromClause.
The returned object is an instance of Select.
All arguments which accept ClauseElement arguments also accept string arguments, which will be converted as appropriate into either text() or literal_column() constructs.
A list of ClauseElement objects, typically ColumnElement objects or subclasses, which will form the columns clause of the resulting statement. For all members which are instances of Selectable, the individual ColumnElement members of the Selectable will be added individually to the columns clause. For example, specifying a Table instance will result in all the contained Column objects within to be added to the columns clause.
This argument is not present on the form of select() available on Table.
Additional parameters include:
Return a Table object.
This is a primitive version of the Table object, which is a subclass of this object.
Create literal text to be inserted into a query.
When constructing a query from a select(), update(), insert() or delete(), using plain strings for argument values will usually result in text objects being created automatically. Use this function when creating textual clauses outside of other ClauseElement objects, or optionally wherever plain text is to be used.
Return a UNION of multiple selectables.
The returned object is an instance of CompoundSelect.
A similar union() method is available on all FromClause subclasses.
Return a UNION ALL of multiple selectables.
The returned object is an instance of CompoundSelect.
A similar union_all() method is available on all FromClause subclasses.
Return an Update clause element.
Similar functionality is available via the update() method on Table.
If both values and compile-time bind parameters are present, the compile-time bind parameters override the information specified within values on a per-key basis.
The keys within values can be either Column objects or their string identifiers. Each key may reference one of:
If a SELECT statement is specified which references this UPDATE statement's table, the statement will be correlated against the UPDATE statement.
Represents an table or selectable alias (AS).
Represents an alias, as typically applied to any table or sub-select within a SQL statement using the AS keyword (or without the keyword on certain databases such as Oracle).
This object is constructed from the alias() module level function as well as the alias() method available on all FromClause subclasses.
Represent an expression that is LEFT <operator> RIGHT.
Construct a new _BinaryExpression.
Represent a bind parameter.
Public constructor is the bindparam() function.
Construct a _BindParamClause.
Compare this _BindParamClause to the given clause.
Since compare() is meant to compare statement syntax, this method returns True if the two _BindParamClauses have just the same type.
Describe a calculated SQL expression that has a type, like CASE.
Extends ColumnElement to provide column-level comparison operators.
Base class for elements of a programmatically constructed SQL expression.
Returns the Engine or Connection to which this ClauseElement is bound, or None if none found.
Compare this ClauseElement to the given ClauseElement.
Subclasses should override the default behavior, which is a straight identity comparison.
Compile this SQL expression.
Uses the given Compiler, or the given AbstractDialect or Engine to create a Compiler. If no compiler arguments are given, tries to use the underlying Engine this ClauseElement is bound to to create a Compiler, if any.
Finally, if there is no bound Engine, uses an DefaultDialect to create a default Compiler.
parameters is a dictionary representing the default bind parameters to be used with the statement. If parameters is a list, it is assumed to be a list of dictionaries and the first dictionary in the list is used with which to compile against.
The bind parameters can in some cases determine the output of the compilation, such as for UPDATE and INSERT statements the bind parameters that are present determine the SET and VALUES clause of those statements.
Return immediate child elements of this ClauseElement.
This is used for visit traversal.
**kwargs may contain flags that change the collection that is returned, for example to return a subset of items in order to cut down on larger traversals, or to return child items from a different context (such as schema-level collections instead of clause-level).
Return a copy with bindparam() elments replaced.
Returns a copy of this ClauseElement with bindparam() elements replaced with values taken from the given dictionary:
>>> clause = column('x') + bindparam('foo') >>> print clause.compile().params {'foo':None} >>> print clause.params({'foo':7}).compile().params {'foo':7}
Compile and execute this ClauseElement, returning the result's scalar representation.
Return True if this clause element represents a complete executable statement.
Return a copy with bindparam() elments replaced.
Same functionality as params(), except adds unique=True to affected bind parameters so that multiple statements can be used.
Describe a list of clauses, separated by an operator.
By default, is comma-separated, such as a column listing.
Compare this ClauseList to the given ClauseList, including a comparison of all the clause items.
Represents a generic column expression from any textual string.
This includes columns associated with tables, aliases and select statements, but also any arbitrary text. May or may not be bound to an underlying Selectable. _ColumnClause is usually created publically via the column() function or the literal_column() function.
Construct a new _ColumnClause.
An ordered dictionary that stores a list of ColumnElement instances.
Overrides the __eq__() method to produce SQL clauses between sets of correlated columns.
Add a column to this collection.
The key attribute of the column will be used as the hash key for this dictionary.
add the given column to this collection, removing unaliased versions of this column as well as existing columns with the same key.
e.g.:
t = Table('sometable', Column('col1', Integer)) t.replace_unalised(Column('col1', Integer, key='columnone'))will remove the original 'col1' from the collection, and add the new column under the name 'columnname'.
Used by schema.Column to override columns during table reflection.
Represent an element that is usable within the "column clause" portion of a SELECT statement.
This includes columns associated with tables, aliases, and subqueries, expressions, function calls, SQL keywords such as NULL, literals, etc. ColumnElement is the ultimate base class for all such elements.
ColumnElement supports the ability to be a proxy element, which indicates that the ColumnElement may be associated with a Selectable which was derived from another Selectable. An example of a "derived" Selectable is an Alias of a Table.
A ColumnElement, by subclassing the _CompareMixin mixin class, provides the ability to generate new ClauseElement objects using Python expressions. See the _CompareMixin docstring for more details.
provides a constant 'anonymous label' for this ColumnElement.
This is a label() expression which will be named at compile time. The same label() is returned each time anon_label is called so that expressions can reference anon_label multiple times, producing the same label name at compile time.
the compiler uses this function automatically at compile time for expressions that are known to be 'unnamed' like binary expressions and function calls.
Return True if the given ColumnElement has a common ancestor to this ColumnElement.
Adapts a ClauseElement which may or may not be a ColumnElement subclass itself into an object which acts like a ColumnElement.
Defines comparison and math operations for ClauseElement instances.
Produce a BETWEEN clause, i.e. <column> BETWEEN <cleft> AND <cright>
Allow _CompareMixins to return the underlying ClauseElement, for non-ClauseElement _CompareMixins.
Allow _CompareMixins to return the appropriate object to be used in expressions.
Produce a column label, i.e. <columnname> AS <name>.
if 'name' is None, an anonymous label name will be generated.
produce a generic operator function.
e.g.:
somecolumn.op("*")(5)
produces:
somecolumn * 5
return a new delete() construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.
return a new exists() construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.
Represent an element that can be used within the FROM clause of a SELECT statement.
Given a ColumnElement, return the exported ColumnElement object from this Selectable which corresponds to that original Column via a common anscestor column.
return a SELECT COUNT generated against this FromClause.
a brief description of this FromClause.
Used primarily for error message formatting.
Return True if this FromClause is 'derived' from the given FromClause.
An example would be an Alias of a Table is derived from that Table.
return an outer join of this FromClause against another FromClause.
replace all occurences of FromClause 'old' with the given Alias object, returning a copy of this FromClause.
Describe a SQL function.
Extends _CalculatedClause, turn the clauselist into function arguments, also adds a packagenames argument.
Generate _Function objects based on getattr calls.
Represent a grouping within a column expression
Construct a new Insert.
Add a word or expression between INSERT and INTO. Generative.
If multiple prefixes are supplied, they will be separated with spaces.
represent a JOIN construct between two FromClause elements.
The public constructor function for Join is the module-level join() function, as well as the join() method available off all FromClause subclasses.
Create a Select out of this Join clause and return an Alias of it.
The Select is not correlating.
Returns the column list of this Join with all equivalently-named, equated columns folded into one column, where 'equated' means they are equated to each other in the ON clause of this join.
this method is used by select(fold_equivalents=True).
The primary usage for this is when generating UNIONs so that each selectable can have distinctly-named columns without the need for use_labels=True.
Create a Select from this Join.
Represents a column label (AS).
Represent a label, as typically applied to any column-level element using the AS sql keyword.
This object is constructed from the label() module level function as well as the label() method available on all ColumnElement subclasses.
Represent the NULL keyword in a SQL statement.
Public constructor is the null() function.
Represents a SELECT statement.
Select statements support appendable clauses, as well as the ability to execute themselves and return a result set.
Construct a Select object.
The public constructor for Select is the select() function; see that function for argument descriptions.
Additional generative and mutator methods are available on the _SelectBaseMixin superclass.
append the given column expression to the columns clause of this select() construct.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
append the given correlation expression to this select() construct.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
append the given FromClause expression to this select() construct's FROM clause.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
append the given expression to this select() construct's HAVING criterion.
The expression will be joined to existing HAVING criterion via AND.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
append the given columns clause prefix expression to this select() construct.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
append the given expression to this select() construct's WHERE criterion.
The expression will be joined to existing WHERE criterion via AND.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
return a new select() construct with the given column expression added to its columns clause.
return a new select() construct which will correlate the given FROM clause to that of an enclosing select(), if a match is found.
By "match", the given fromclause must be present in this select's list of FROM objects and also present in an enclosing select's list of FROM objects.
Calling this method turns off the select's default behavior of "auto-correlation". Normally, select() auto-correlates all of its FROM clauses to those of an embedded select when compiled.
If the fromclause is None, the select() will not correlate to anything.
return a SQL EXCEPT of this select() construct against the given selectable.
return a SQL EXCEPT ALL of this select() construct against the given selectable.
Return a list of all FromClause elements which will be applied to the FROM clause of the resulting statement.
return child elements as per the ClauseElement specification.
return a new select() construct with the given expression added to its HAVING clause, joined to the existing clause via AND, if any.
a collection of all ColumnElement expressions which would be rendered into the columns clause of the resulting SELECT statement.
return a SQL INTERSECT of this select() construct against the given selectable.
return a SQL INTERSECT ALL of this select() construct against the given selectable.
return a Set of all FromClause elements referenced by this Select.
This set is a superset of that returned by the froms property, which is specifically for those FromClause elements that would actually be rendered.
return a new select() construct which will apply the given expression to the start of its columns clause, not using any commas.
return a new select() construct with the given FROM expression applied to its list of FROM objects.
return a 'grouping' construct as per the ClauseElement specification.
This produces an element that can be embedded in an expression. Note that this method is called automatically as needed when constructing expressions.
return a SQL UNION of this select() construct against the given selectable.
return a SQL UNION ALL of this select() construct against the given selectable.
return a new select() construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.
Base class for Select and CompoundSelects.
Construct a new _SelectBaseMixin.
Append the given GROUP BY criterion applied to this selectable.
The criterion will be appended to any pre-existing GROUP BY criterion.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
Append the given ORDER BY criterion applied to this selectable.
The criterion will be appended to any pre-existing ORDER BY criterion.
Note that this mutates the Select construct such that derived attributes, such as the "primary_key", "oid_column", and child "froms" collection may be invalid if they have already been initialized. Consider the generative form of this method instead to prevent this issue.
return a new selectable with the 'use_labels' flag set to True.
This will result in column expressions being generated using labels against their table name, such as "SELECT somecolumn AS tablename_somecolumn". This allows selectables which contain multiple FROM clauses to produce a unique set of column names regardless of name conflicts among the individual FROM clauses.
return a 'scalar' representation of this selectable, which can be used as a column expression.
Typically, a select statement which has only one column in its columns clause is eligible to be used as a scalar expression.
The returned object is an instance of _ScalarSelect.
return a new selectable with the given list of GROUP BY criterion applied.
The criterion will be appended to any pre-existing GROUP BY criterion.
return a 'scalar' representation of this selectable, embedded as a subquery with a label.
See also as_scalar().
return a new selectable with the given list of ORDER BY criterion applied.
The criterion will be appended to any pre-existing ORDER BY criterion.
return a SELECT of this selectable.
This has the effect of embeddeding this select into a subquery that is selected from.
part of the ClauseElement contract; returns True in all cases for this class.
Represents a "table" construct.
Note that this represents tables only as another syntactical construct within SQL expressions; it does not provide schema-level functionality.
Represent a literal SQL text fragment.
Public constructor is the text() function.
Construct a new _TextClause.
Handle a type keyword in a SQL statement.
Used by the Case statement.
Construct a new _UnaryExpression.
Construct a new Update.
return a new update() construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.
Form the base for INSERT, UPDATE, and DELETE statements.