else:
return schema + "." + name
-class _TableSingleton(visitors.VisitableType):
- """A metaclass used by the ``Table`` object to provide singleton behavior."""
+class Table(SchemaItem, expression.TableClause):
+ """Represent a table in a database.
+
+ e.g.::
+
+ mytable = Table("mytable", metadata,
+ Column('mytable_id', Integer, primary_key=True),
+ Column('value', String(50))
+ )
+
+ The Table object constructs a unique instance of itself based on its
+ name within the given MetaData object. Constructor
+ arguments are as follows:
+
+ :param name: The name of this table as represented in the database.
+
+ This property, along with the *schema*, indicates the *singleton
+ identity* of this table in relation to its parent :class:`MetaData`.
+ Additional calls to :class:`Table` with the same name, metadata,
+ and schema name will return the same :class:`Table` object.
+
+ Names which contain no upper case characters
+ will be treated as case insensitive names, and will not be quoted
+ unless they are a reserved word. Names with any number of upper
+ case characters will be quoted and sent exactly. Note that this
+ behavior applies even for databases which standardize upper
+ case names as case insensitive such as Oracle.
+
+ :param metadata: a :class:`MetaData` object which will contain this
+ table. The metadata is used as a point of association of this table
+ with other tables which are referenced via foreign key. It also
+ may be used to associate this table with a particular
+ :class:`~sqlalchemy.engine.base.Connectable`.
+
+ :param \*args: Additional positional arguments are used primarily
+ to add the list of :class:`Column` objects contained within this table.
+ Similar to the style of a CREATE TABLE statement, other :class:`SchemaItem`
+ constructs may be added here, including :class:`PrimaryKeyConstraint`,
+ and :class:`ForeignKeyConstraint`.
+
+ :param autoload: Defaults to False: the Columns for this table should be reflected
+ from the database. Usually there will be no Column objects in the
+ constructor if this property is set.
+
+ :param autoload_with: If autoload==True, this is an optional Engine or Connection
+ instance to be used for the table reflection. If ``None``, the
+ underlying MetaData's bound connectable will be used.
+
+ :param include_columns: A list of strings indicating a subset of columns to be loaded via
+ the ``autoload`` operation; table columns who aren't present in
+ this list will not be represented on the resulting ``Table``
+ object. Defaults to ``None`` which indicates all columns should
+ be reflected.
+
+ :param info: A dictionary which defaults to ``{}``. A space to store application
+ specific data. This must be a dictionary.
+
+ :param mustexist: When ``True``, indicates that this Table must already
+ be present in the given :class:`MetaData`` collection.
+
+ :param prefixes:
+ A list of strings to insert after CREATE in the CREATE TABLE
+ statement. They will be separated by spaces.
+
+ :param quote: Force quoting of this table's name on or off, corresponding
+ to ``True`` or ``False``. When left at its default of ``None``,
+ the column identifier will be quoted according to whether the name is
+ case sensitive (identifiers with at least one upper case character are
+ treated as case sensitive), or if it's a reserved word. This flag
+ is only needed to force quoting of a reserved word which is not known
+ by the SQLAlchemy dialect.
+
+ :param quote_schema: same as 'quote' but applies to the schema identifier.
+
+ :param schema: The *schema name* for this table, which is required if the table
+ resides in a schema other than the default selected schema for the
+ engine's database connection. Defaults to ``None``.
+
+ :param useexisting: When ``True``, indicates that if this Table is already
+ present in the given :class:`MetaData`, apply further arguments within
+ the constructor to the existing :class:`Table`. If this flag is not
+ set, an error is raised when the parameters of an existing :class:`Table`
+ are overwritten.
+
+ """
+
+ __visit_name__ = 'table'
- def __call__(self, name, metadata, *args, **kwargs):
- schema = kwargs.get('schema', None)
- useexisting = kwargs.pop('useexisting', False)
- mustexist = kwargs.pop('mustexist', False)
+ ddl_events = ('before-create', 'after-create', 'before-drop', 'after-drop')
+
+ def __new__(cls, name, metadata, *args, **kw):
+ schema = kw.get('schema', None)
+ useexisting = kw.pop('useexisting', False)
+ mustexist = kw.pop('mustexist', False)
key = _get_table_key(name, schema)
- try:
- table = metadata.tables[key]
+ if key in metadata.tables:
if not useexisting and bool(args):
raise exc.InvalidRequestError(
"Table '%s' is already defined for this MetaData instance. "
"Specify 'useexisting=True' to redefine options and "
"columns on an existing Table object." % key)
- else:
- table._init_existing(*args, **kwargs)
+ table = metadata.tables[key]
+ table._init_existing(*args, **kw)
return table
- except KeyError:
+ else:
if mustexist:
raise exc.InvalidRequestError(
"Table '%s' not defined" % (key))
+ metadata.tables[key] = table = object.__new__(cls)
try:
- return type.__call__(self, name, metadata, *args, **kwargs)
+ table._init(name, metadata, *args, **kw)
+ return table
except:
- if key in metadata.tables:
- del metadata.tables[key]
+ metadata.tables.pop(key)
raise
-
-
-class Table(SchemaItem, expression.TableClause):
- """Represent a table in a database."""
-
- __metaclass__ = _TableSingleton
-
- __visit_name__ = 'table'
-
- ddl_events = ('before-create', 'after-create', 'before-drop', 'after-drop')
-
- def __init__(self, name, metadata, *args, **kwargs):
- """
- Construct a Table.
-
- :param name: The name of this table as represented in the database.
-
- This property, along with the *schema*, indicates the *singleton
- identity* of this table in relation to its parent :class:`MetaData`.
- Additional calls to :class:`Table` with the same name, metadata,
- and schema name will return the same :class:`Table` object.
-
- Names which contain no upper case characters
- will be treated as case insensitive names, and will not be quoted
- unless they are a reserved word. Names with any number of upper
- case characters will be quoted and sent exactly. Note that this
- behavior applies even for databases which standardize upper
- case names as case insensitive such as Oracle.
-
- :param metadata: a :class:`MetaData` object which will contain this
- table. The metadata is used as a point of association of this table
- with other tables which are referenced via foreign key. It also
- may be used to associate this table with a particular
- :class:`~sqlalchemy.engine.base.Connectable`.
-
- :param \*args: Additional positional arguments are used primarily
- to add the list of :class:`Column` objects contained within this table.
- Similar to the style of a CREATE TABLE statement, other :class:`SchemaItem`
- constructs may be added here, including :class:`PrimaryKeyConstraint`,
- and :class:`ForeignKeyConstraint`.
-
- :param autoload: Defaults to False: the Columns for this table should be reflected
- from the database. Usually there will be no Column objects in the
- constructor if this property is set.
-
- :param autoload_with: If autoload==True, this is an optional Engine or Connection
- instance to be used for the table reflection. If ``None``, the
- underlying MetaData's bound connectable will be used.
-
- :param include_columns: A list of strings indicating a subset of columns to be loaded via
- the ``autoload`` operation; table columns who aren't present in
- this list will not be represented on the resulting ``Table``
- object. Defaults to ``None`` which indicates all columns should
- be reflected.
-
- :param info: A dictionary which defaults to ``{}``. A space to store application
- specific data. This must be a dictionary.
-
- :param mustexist: When ``True``, indicates that this Table must already
- be present in the given :class:`MetaData`` collection.
-
- :param prefixes:
- A list of strings to insert after CREATE in the CREATE TABLE
- statement. They will be separated by spaces.
-
- :param quote: Force quoting of this table's name on or off, corresponding
- to ``True`` or ``False``. When left at its default of ``None``,
- the column identifier will be quoted according to whether the name is
- case sensitive (identifiers with at least one upper case character are
- treated as case sensitive), or if it's a reserved word. This flag
- is only needed to force quoting of a reserved word which is not known
- by the SQLAlchemy dialect.
-
- :param quote_schema: same as 'quote' but applies to the schema identifier.
-
- :param schema: The *schema name* for this table, which is required if the table
- resides in a schema other than the default selected schema for the
- engine's database connection. Defaults to ``None``.
-
- :param useexisting: When ``True``, indicates that if this Table is already
- present in the given :class:`MetaData`, apply further arguments within
- the constructor to the existing :class:`Table`. If this flag is not
- set, an error is raised when the parameters of an existing :class:`Table`
- are overwritten.
-
- """
+
+ def __init__(self, *args, **kw):
+ # __init__ is overridden to prevent __new__ from
+ # calling the superclass constructor.
+ pass
+
+ def _init(self, name, metadata, *args, **kwargs):
super(Table, self).__init__(name)
self.metadata = metadata
self.schema = kwargs.pop('schema', None)
autoload_with = kwargs.pop('autoload_with', None)
include_columns = kwargs.pop('include_columns', None)
- self._set_parent(metadata)
-
self.quote = kwargs.pop('quote', None)
self.quote_schema = kwargs.pop('quote_schema', None)
if kwargs.get('info'):
if len([k for k in kwargs
if not re.match(r'^(?:%s)_' % '|'.join(dialects.__all__), k)]):
raise TypeError(
- "Invalid argument(s) for Table: %s" % repr(kwargs.keys()))
+ "Invalid argument(s) for Table: %r" % kwargs.keys())
self.kwargs.update(kwargs)
def _set_primary_key(self, pk):
def bind(self):
"""Return the connectable associated with this Table."""
- m = self.metadata
- return m and m.bind or None
+ return self.metadata and self.metadata.bind or None
def append_column(self, column):
"""Append a ``Column`` to this ``Table``."""
self, column_collections=column_collections, **kwargs)
else:
if column_collections:
- return [c for c in self.columns]
+ return list(self.columns)
else:
return []
raise exc.ArgumentError("this Column already has a table!")
if self.key in table._columns:
- # note the column being replaced, if any
- self._pre_existing_column = table._columns.get(self.key)
+ col = table._columns.get(self.key)
+ for fk in col.foreign_keys:
+ col.foreign_keys.remove(fk)
+ table.foreign_keys.remove(fk)
+ table.constraints.remove(fk.constraint)
table._columns.replace(self)
selectable.columns.add(c)
if self.primary_key:
selectable.primary_key.add(c)
- [c._init_items(f) for f in fk]
+ for f in fk:
+ c._init_items(f)
return c
def get_children(self, schema_visitor=False, **kwargs):
raise exc.InvalidRequestError("This ForeignKey already has a parent !")
self.parent = column
- if hasattr(self.parent, '_pre_existing_column'):
- # remove existing FK which matches us
- for fk in self.parent._pre_existing_column.foreign_keys:
- if fk.target_fullname == self.target_fullname:
- self.parent.table.foreign_keys.remove(fk)
- self.parent.table.constraints.remove(fk.constraint)
-
if self.constraint is None and isinstance(self.parent.table, Table):
self.constraint = ForeignKeyConstraint(
[], [], use_alter=self.use_alter, name=self.name,
__visit_name__ = property(_visit_name)
def __repr__(self):
- return "ColumnDefault(%s)" % repr(self.arg)
+ return "ColumnDefault(%r)" % self.arg
class Sequence(DefaultGenerator):
"""Represents a named database sequence."""
self.unique = kwargs.pop('unique', False)
self.kwargs = kwargs
- self._init_items(*columns)
-
- def _init_items(self, *args):
- for column in args:
+ for column in columns:
column = _to_schema_column(column)
if self.table is None:
self._set_parent(column.table)
Similar functionality is also available via the ``select()``
method on any :class:`~sqlalchemy.sql.expression.FromClause`.
- The returned object is an instance of :class:`~sqlalchemy.sql.expression.Select`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression.Select`.
All arguments which accept ``ClauseElement`` arguments also accept
string arguments, which will be converted as appropriate into
"""
if 'scalar' in kwargs:
- util.warn_deprecated('scalar option is deprecated; see docs for details')
+ util.warn_deprecated(
+ 'scalar option is deprecated; see docs for details')
scalar = kwargs.pop('scalar', False)
s = Select(columns, whereclause=whereclause, from_obj=from_obj, **kwargs)
if scalar:
return s
def subquery(alias, *args, **kwargs):
- """Return an :class:`~sqlalchemy.sql.expression.Alias` object derived from a :class:`~sqlalchemy.sql.expression.Select`.
+ """Return an :class:`~sqlalchemy.sql.expression.Alias` object derived
+ from a :class:`~sqlalchemy.sql.expression.Select`.
name
alias name
\*args, \**kwargs
- all other arguments are delivered to the :func:`~sqlalchemy.sql.expression.select`
- function.
+ all other arguments are delivered to the
+ :func:`~sqlalchemy.sql.expression.select` function.
"""
return Select(*args, **kwargs).alias(alias)
table columns. Note that the :meth:`~Insert.values()` generative method
may also be used for this.
- :param prefixes: A list of modifier keywords to be inserted between INSERT and INTO.
- Alternatively, the :meth:`~Insert.prefix_with` generative method may be used.
+ :param prefixes: A list of modifier keywords to be inserted between INSERT
+ and INTO. Alternatively, the :meth:`~Insert.prefix_with` generative method
+ may be used.
- :param inline:
- if True, SQL defaults will be compiled 'inline' into the statement
- and not pre-executed.
+ :param inline: if True, SQL defaults will be compiled 'inline' into the
+ statement and not pre-executed.
If both `values` and compile-time bind parameters are present, the
compile-time bind parameters override the information specified
:param table: The table to be updated.
- :param whereclause: A ``ClauseElement`` describing the ``WHERE`` condition of the
- ``UPDATE`` statement. Note that the :meth:`~Update.where()` generative
- method may also be used for this.
+ :param whereclause: A ``ClauseElement`` describing the ``WHERE`` condition
+ of the ``UPDATE`` statement. Note that the :meth:`~Update.where()`
+ generative method may also be used for this.
:param values:
A dictionary which specifies the ``SET`` conditions of the
against the ``UPDATE`` statement.
"""
- return Update(table, whereclause=whereclause, values=values, inline=inline, **kwargs)
+ return Update(
+ table,
+ whereclause=whereclause,
+ values=values,
+ inline=inline,
+ **kwargs)
def delete(table, whereclause = None, **kwargs):
"""Return a :class:`~sqlalchemy.sql.expression.Delete` clause element.
:param table: The table to be updated.
- :param whereclause: A :class:`ClauseElement` describing the ``WHERE`` condition of the
- ``UPDATE`` statement. Note that the :meth:`~Delete.where()` generative method
- may be used instead.
+ :param whereclause: A :class:`ClauseElement` describing the ``WHERE``
+ condition of the ``UPDATE`` statement. Note that the :meth:`~Delete.where()`
+ generative method may be used instead.
"""
return Delete(table, whereclause, **kwargs)
"""Join a list of clauses together using the ``AND`` operator.
The ``&`` operator is also overloaded on all
- :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses to produce the same
- result.
+ :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses to produce the
+ same result.
"""
if len(clauses) == 1:
"""Join a list of clauses together using the ``OR`` operator.
The ``|`` operator is also overloaded on all
- :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses to produce the same
- result.
+ :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses to produce the
+ same result.
"""
if len(clauses) == 1:
"""Return a negation of the given clause, i.e. ``NOT(clause)``.
The ``~`` operator is also overloaded on all
- :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses to produce the same
- result.
+ :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses to produce the
+ same result.
"""
return operators.inv(_literal_as_binds(clause))
Equivalent of SQL ``clausetest BETWEEN clauseleft AND clauseright``.
- The ``between()`` method on all :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses
- provides similar functionality.
+ The ``between()`` method on all
+ :class:`~sqlalchemy.sql.expression._CompareMixin` subclasses provides
+ similar functionality.
"""
ctest = _literal_as_binds(ctest)
def union(*selects, **kwargs):
"""Return a ``UNION`` of multiple selectables.
- The returned object is an instance of :class:`~sqlalchemy.sql.expression.CompoundSelect`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression.CompoundSelect`.
A similar ``union()`` method is available on all
:class:`~sqlalchemy.sql.expression.FromClause` subclasses.
def union_all(*selects, **kwargs):
"""Return a ``UNION ALL`` of multiple selectables.
- The returned object is an instance of :class:`~sqlalchemy.sql.expression.CompoundSelect`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression.CompoundSelect`.
A similar ``union_all()`` method is available on all
:class:`~sqlalchemy.sql.expression.FromClause` subclasses.
def except_(*selects, **kwargs):
"""Return an ``EXCEPT`` of multiple selectables.
- The returned object is an instance of :class:`~sqlalchemy.sql.expression.CompoundSelect`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression.CompoundSelect`.
\*selects
a list of :class:`~sqlalchemy.sql.expression.Select` instances.
def except_all(*selects, **kwargs):
"""Return an ``EXCEPT ALL`` of multiple selectables.
- The returned object is an instance of :class:`~sqlalchemy.sql.expression.CompoundSelect`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression.CompoundSelect`.
\*selects
a list of :class:`~sqlalchemy.sql.expression.Select` instances.
def intersect(*selects, **kwargs):
"""Return an ``INTERSECT`` of multiple selectables.
- The returned object is an instance of :class:`~sqlalchemy.sql.expression.CompoundSelect`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression.CompoundSelect`.
\*selects
a list of :class:`~sqlalchemy.sql.expression.Select` instances.
def intersect_all(*selects, **kwargs):
"""Return an ``INTERSECT ALL`` of multiple selectables.
- The returned object is an instance of :class:`~sqlalchemy.sql.expression.CompoundSelect`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression.CompoundSelect`.
\*selects
a list of :class:`~sqlalchemy.sql.expression.Select` instances.
def alias(selectable, alias=None):
"""Return an :class:`~sqlalchemy.sql.expression.Alias` object.
- An ``Alias`` represents any :class:`~sqlalchemy.sql.expression.FromClause` with
- an alternate name assigned within SQL, typically using the ``AS``
+ An ``Alias`` represents any :class:`~sqlalchemy.sql.expression.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
return _BindParamClause(None, value, type_=type_, unique=True)
def label(name, obj):
- """Return a :class:`~sqlalchemy.sql.expression._Label` object for the given :class:`~sqlalchemy.sql.expression.ColumnElement`.
+ """Return a :class:`~sqlalchemy.sql.expression._Label` object for the given
+ :class:`~sqlalchemy.sql.expression.ColumnElement`.
A label changes the name of an element in the columns clause of a
``SELECT`` statement, typically via the ``AS`` SQL keyword.
return _Label(name, obj)
def column(text, type_=None):
- """Return a textual column clause, as would be in the columns clause of a ``SELECT`` statement.
+ """Return a textual column clause, as would be in the columns clause of a
+ ``SELECT`` statement.
- The object returned is an instance of :class:`~sqlalchemy.sql.expression.ColumnClause`,
- which represents the "syntactical" portion of the schema-level
- :class:`~sqlalchemy.schema.Column` object.
+ The object returned is an instance of
+ :class:`~sqlalchemy.sql.expression.ColumnClause`, which represents the
+ "syntactical" portion of the schema-level :class:`~sqlalchemy.schema.Column`
+ object.
text
the name of the column. Quoting rules will be applied to the
:func:`~sqlalchemy.sql.expression.column` function.
type\_
- an optional :class:`~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.
+ an optional :class:`~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.
"""
return ColumnClause(text, type_=type_, is_literal=True)
return _BindParamClause(key, value, type_=type_, unique=unique, shortname=shortname)
def outparam(key, type_=None):
- """Create an 'OUT' parameter for usage in functions (stored procedures), for databases which support them.
+ """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
attribute, which returns a dictionary containing the values.
"""
- return _BindParamClause(key, None, type_=type_, unique=False, isoutparam=True)
+ return _BindParamClause(
+ key, None, type_=type_, unique=False, isoutparam=True)
def text(text, bind=None, *args, **kwargs):
"""Create literal text to be inserted into a query.
return _TextClause(text, bind=bind, *args, **kwargs)
def null():
- """Return a :class:`_Null` object, which compiles to ``NULL`` in a sql statement."""
-
+ """Return a :class:`_Null` object, which compiles to ``NULL`` in a sql
+ statement.
+
+ """
return _Null()
class _FunctionGenerator(object):
if func is not None:
return func(*c, **o)
- return Function(self.__names[-1], packagenames=self.__names[0:-1], *c, **o)
+ return Function(
+ self.__names[-1], packagenames=self.__names[0:-1], *c, **o)
# "func" global - i.e. func.count()
func = _FunctionGenerator()
return element._clone()
def _expand_cloned(elements):
- """expand the given set of ClauseElements to be the set of all 'cloned' predecessors."""
-
+ """expand the given set of ClauseElements to be the set of all 'cloned'
+ predecessors.
+
+ """
return itertools.chain(*[x._cloned_set for x in elements])
def _cloned_intersection(a, b):
return CompoundSelect(keyword, *selects, **kwargs)
def _is_literal(element):
- return not isinstance(element, Visitable) and not hasattr(element, '__clause_element__')
+ return not isinstance(element, Visitable) and \
+ not hasattr(element, '__clause_element__')
def _from_objects(*elements):
return itertools.chain(*[element._from_objects for element in elements])
return element
def _corresponding_column_or_error(fromclause, column, require_embedded=False):
- c = fromclause.corresponding_column(column, require_embedded=require_embedded)
+ c = fromclause.corresponding_column(column,
+ require_embedded=require_embedded)
if not c:
- raise exc.InvalidRequestError("Given column '%s', attached to table '%s', "
+ raise exc.InvalidRequestError(
+ "Given column '%s', attached to table '%s', "
"failed to locate a corresponding column from table '%s'"
- % (column, getattr(column, 'table', None), fromclause.description))
+ %
+ (column,
+ getattr(column, 'table', None),fromclause.description)
+ )
return c
def is_column(col):
"""True if ``col`` is an instance of ``ColumnElement``."""
+
return isinstance(col, ColumnElement)
class ClauseElement(Visitable):
- """Base class for elements of a programmatically constructed SQL expression."""
-
+ """Base class for elements of a programmatically constructed SQL
+ expression.
+
+ """
__visit_name__ = 'clause'
_annotations = {}
@util.memoized_property
def _cloned_set(self):
- """Return the set consisting all cloned anscestors of this ClauseElement.
+ """Return the set consisting all cloned anscestors of this
+ ClauseElement.
Includes this ClauseElement. This accessor tends to be used for
FromClause objects to identify 'equivalent' FROM clauses, regardless
return d
def _annotate(self, values):
- """return a copy of this ClauseElement with the given annotations dictionary."""
-
+ """return a copy of this ClauseElement with the given annotations
+ dictionary.
+
+ """
global Annotated
if Annotated is None:
from sqlalchemy.sql.util import Annotated
return Annotated(self, values)
def _deannotate(self):
- """return a copy of this ClauseElement with an empty annotations dictionary."""
+ """return a copy of this ClauseElement with an empty annotations
+ dictionary.
+
+ """
return self._clone()
def unique_params(self, *optionaldict, **kwargs):
if len(optionaldict) == 1:
kwargs.update(optionaldict[0])
elif len(optionaldict) > 1:
- raise exc.ArgumentError("params() takes zero or one positional dictionary argument")
+ raise exc.ArgumentError(
+ "params() takes zero or one positional dictionary argument")
def visit_bindparam(bind):
if bind.key in kwargs:
# execute(), however.
@property
def bind(self):
- """Returns the Engine or Connection to which this ClauseElement is bound, or None if none found."""
-
+ """Returns the Engine or Connection to which this ClauseElement is
+ bound, or None if none found.
+
+ """
if self._bind is not None:
return self._bind
return e._execute_clauseelement(self, multiparams, params)
def scalar(self, *multiparams, **params):
- """Compile and execute this ``ClauseElement``, returning the result's scalar representation."""
-
+ """Compile and execute this ``ClauseElement``, returning the result's
+ scalar representation.
+
+ """
return self.execute(*multiparams, **params).scalar()
def compile(self, bind=None, dialect=None, **kw):
"""Compile this SQL expression.
The return value is a :class:`~sqlalchemy.engine.Compiled` object.
- Calling `str()` or `unicode()` on the returned value will yield
- a string representation of the result. The :class:`~sqlalchemy.engine.Compiled`
- object also can return a dictionary of bind parameter names and
- values using the `params` accessor.
+ Calling `str()` or `unicode()` on the returned value will yield a string
+ representation of the result. The :class:`~sqlalchemy.engine.Compiled`
+ object also can return a dictionary of bind parameter names and values
+ using the `params` accessor.
:param bind: An ``Engine`` or ``Connection`` from which a
``Compiled`` will be acquired. This argument
if hasattr(self, 'negation_clause'):
return self.negation_clause
else:
- return _UnaryExpression(self.self_group(against=operators.inv), operator=operators.inv, negate=None)
+ return _UnaryExpression(
+ self.self_group(against=operators.inv),
+ operator=operators.inv,
+ negate=None)
def __repr__(self):
friendly = getattr(self, 'description', None)
def __init__(self, *cols):
super(ColumnCollection, self).__init__()
- [self.add(c) for c in cols]
+ self.update((c.key, c) for c in cols)
def __str__(self):
return repr([str(c) for c in self])
__visit_name__ = 'selectable'
class FromClause(Selectable):
- """Represent an element that can be used within the ``FROM`` clause of a ``SELECT`` statement."""
-
+ """Represent an element that can be used within the ``FROM``
+ clause of a ``SELECT`` statement.
+
+ """
__visit_name__ = 'fromclause'
named_with_column = False
_hide_froms = []
col = list(self.primary_key)[0]
else:
col = list(self.columns)[0]
- return select([func.count(col).label('tbl_row_count')], whereclause, from_obj=[self], **params)
+ return select(
+ [func.count(col).label('tbl_row_count')],
+ whereclause,
+ from_obj=[self],
+ **params)
def select(self, whereclause=None, **params):
"""return a SELECT of this ``FromClause``."""
return fromclause in self._cloned_set
def replace_selectable(self, old, alias):
- """replace all occurences of FromClause 'old' with the given Alias object, returning a copy of this ``FromClause``."""
-
+ """replace all occurences of FromClause 'old' with the given Alias
+ object, returning a copy of this ``FromClause``.
+
+ """
global ClauseAdapter
if ClauseAdapter is None:
from sqlalchemy.sql.util import ClauseAdapter
col, intersect = c, i
elif len(i) > len(intersect):
# 'c' has a larger field of correspondence than 'col'.
- # i.e. selectable.c.a1_x->a1.c.x->table.c.x matches a1.c.x->table.c.x better than
+ # i.e. selectable.c.a1_x->a1.c.x->table.c.x matches
+ # a1.c.x->table.c.x better than
# selectable.c.x->table.c.x does.
col, intersect = c, i
elif i == intersect:
# they have the same field of correspondence.
- # see which proxy_set has fewer columns in it, which indicates a
- # closer relationship with the root column. Also take into account the
- # "weight" attribute which CompoundSelect() uses to give higher precedence to
- # columns based on vertical position in the compound statement, and discard columns
- # that have no reference to the target column (also occurs with CompoundSelect)
+ # see which proxy_set has fewer columns in it, which indicates
+ # a closer relationship with the root column. Also take into
+ # account the "weight" attribute which CompoundSelect() uses to
+ # give higher precedence to columns based on vertical position
+ # in the compound statement, and discard columns that have no
+ # reference to the target column (also occurs with
+ # CompoundSelect)
col_distance = util.reduce(operator.add,
- [sc._annotations.get('weight', 1) for sc in col.proxy_set if sc.shares_lineage(column)]
+ [sc._annotations.get('weight', 1)
+ for sc in col.proxy_set
+ if sc.shares_lineage(column)]
)
c_distance = util.reduce(operator.add,
- [sc._annotations.get('weight', 1) for sc in c.proxy_set if sc.shares_lineage(column)]
+ [sc._annotations.get('weight', 1)
+ for sc in c.proxy_set
+ if sc.shares_lineage(column)]
)
- if \
- c_distance < col_distance:
+ if c_distance < col_distance:
col, intersect = c, i
return col
the same type.
"""
- return isinstance(other, _BindParamClause) and other.type.__class__ == self.type.__class__ and self.value == other.value
+ return isinstance(other, _BindParamClause) and \
+ other.type.__class__ == self.type.__class__ and \
+ self.value == other.value
def __getstate__(self):
"""execute a deferred value for serialization purposes."""
return d
def __repr__(self):
- return "_BindParamClause(%s, %s, type_=%s)" % (repr(self.key), repr(self.value), repr(self.type))
+ return "_BindParamClause(%r, %r, type_=%r)" % (
+ self.key, self.value, self.type
+ )
class _TypeClause(ClauseElement):
"""Handle a type keyword in a SQL statement.
_hide_froms = []
- def __init__(self, text = "", bind=None, bindparams=None, typemap=None, autocommit=False):
+ def __init__(self, text = "", bind=None,
+ bindparams=None, typemap=None, autocommit=False):
self._bind = bind
self.bindparams = {}
self.typemap = typemap
return list(itertools.chain(*[c._from_objects for c in self.clauses]))
def self_group(self, against=None):
- if self.group and self.operator is not against and operators.is_precedent(self.operator, against):
+ if self.group and self.operator is not against and \
+ operators.is_precedent(self.operator, against):
return _Grouping(self)
else:
return self
pass
if value:
- whenlist = [(_literal_as_binds(c).self_group(), _literal_as_binds(r)) for (c, r) in whens]
+ whenlist = [
+ (_literal_as_binds(c).self_group(), _literal_as_binds(r)) for (c, r) in whens
+ ]
else:
- whenlist = [(_no_literals(c).self_group(), _literal_as_binds(r)) for (c, r) in whens]
+ whenlist = [
+ (_no_literals(c).self_group(), _literal_as_binds(r)) for (c, r) in whens
+ ]
if whenlist:
type_ = list(whenlist[-1])[-1].type
return e
def select_from(self, clause):
- """return a new exists() construct with the given expression set as its FROM clause."""
-
+ """return a new exists() construct with the given expression set as its FROM
+ clause.
+
+ """
e = self._clone()
e.element = self.element.select_from(clause).self_group()
return e
def where(self, clause):
- """return a new exists() 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.
+
+ """
e = self._clone()
e.element = self.element.where(clause).self_group()
return e
id(self.right))
def is_derived_from(self, fromclause):
- return fromclause is self or self.left.is_derived_from(fromclause) or self.right.is_derived_from(fromclause)
+ return fromclause is self or \
+ self.left.is_derived_from(fromclause) or\
+ self.right.is_derived_from(fromclause)
def self_group(self, against=None):
return _FromGrouping(self)
def __init__(self, name, element, type_=None):
while isinstance(element, _Label):
element = element.element
- self.name = self.key = self._label = name or _generated_label("%%(%d %s)s" % (id(self), getattr(element, 'name', 'anon')))
+ self.name = self.key = self._label = name or \
+ _generated_label("%%(%d %s)s" % (
+ id(self), getattr(element, 'name', 'anon'))
+ )
self._element = element
self._type = type_
self.quote = element.quote
@util.memoized_property
def type(self):
- return sqltypes.to_instance(self._type or getattr(self._element, 'type', None))
+ return sqltypes.to_instance(
+ self._type or getattr(self._element, 'type', None)
+ )
@util.memoized_property
def element(self):
# propagate the "is_literal" flag only if we are keeping our name,
# otherwise its considered to be a label
is_literal = self.is_literal and (name is None or name == self.name)
- c = ColumnClause(name or self.name, selectable=selectable, type_=self.type, is_literal=is_literal)
+ c = ColumnClause(
+ name or self.name,
+ selectable=selectable,
+ type_=self.type,
+ is_literal=is_literal
+ )
c.proxies = [self]
if attach:
selectable.columns[c.name] = c
col = list(self.primary_key)[0]
else:
col = list(self.columns)[0]
- return select([func.count(col).label('tbl_row_count')], whereclause, from_obj=[self], **params)
+ return select(
+ [func.count(col).label('tbl_row_count')],
+ whereclause,
+ from_obj=[self],
+ **params)
def insert(self, values=None, inline=False, **kwargs):
"""Generate an :func:`~sqlalchemy.sql.expression.insert()` construct."""
def update(self, whereclause=None, values=None, inline=False, **kwargs):
"""Generate an :func:`~sqlalchemy.sql.expression.update()` construct."""
- return update(self, whereclause=whereclause, values=values, inline=inline, **kwargs)
+ return update(self, whereclause=whereclause,
+ values=values, inline=inline, **kwargs)
def delete(self, whereclause=None, **kwargs):
"""Generate a :func:`~sqlalchemy.sql.expression.delete()` construct."""
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 :class:`~sqlalchemy.sql.expression._ScalarSelect`.
+ The returned object is an instance of
+ :class:`~sqlalchemy.sql.expression._ScalarSelect`.
"""
return _ScalarSelect(self)
def apply_labels(self):
"""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.
+ 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.
"""
self.use_labels = True
return list(self.inner_columns)[0]._make_proxy(selectable, name)
class CompoundSelect(_SelectBaseMixin, FromClause):
- """Forms the basis of ``UNION``, ``UNION ALL``, and other SELECT-based set operations."""
+ """Forms the basis of ``UNION``, ``UNION ALL``, and other
+ SELECT-based set operations."""
__visit_name__ = 'compound_select'
elif len(s.c) != numcols:
raise exc.ArgumentError(
"All selectables passed to CompoundSelect must "
- "have identical numbers of columns; select #%d has %d columns, select #%d has %d" %
+ "have identical numbers of columns; select #%d has %d columns,"
+ " select #%d has %d" %
(1, len(self.selects[0].c), n+1, len(s.c))
)
def _populate_column_collection(self):
for cols in zip(*[s.c for s in self.selects]):
- proxy = cols[0]._make_proxy(self, name=self.use_labels and cols[0]._label or None)
+ proxy = cols[0]._make_proxy(
+ self, name=self.use_labels and cols[0]._label or None)
- # place a 'weight' annotation corresponding to how low in the list of select()s
- # the column occurs, so that the corresponding_column() operation
+ # place a 'weight' annotation corresponding to how low in the list of
+ # select()s the column occurs, so that the corresponding_column() operation
# can resolve conflicts
proxy.proxies = [c._annotate({'weight':i + 1}) for i, c in enumerate(cols)]
__visit_name__ = 'select'
- def __init__(self, columns, whereclause=None, from_obj=None, distinct=False, having=None, correlate=True, prefixes=None, **kwargs):
+ def __init__(self,
+ columns,
+ whereclause=None,
+ from_obj=None,
+ distinct=False,
+ having=None,
+ correlate=True,
+ prefixes=None,
+ **kwargs):
"""Construct a Select object.
The public constructor for Select is the
if columns:
self._raw_columns = [
- isinstance(c, _ScalarSelect) and c.self_group(against=operators.comma_op) or c
- for c in
- [_literal_as_column(c) for c in columns]
+ isinstance(c, _ScalarSelect) and
+ c.self_group(against=operators.comma_op) or c
+ for c in [_literal_as_column(c) for c in columns]
]
self._froms.update(_from_objects(*self._raw_columns))
be rendered into the columns clause of the resulting SELECT statement.
"""
-
return itertools.chain(*[c._select_iterable for c in self._raw_columns])
def is_derived_from(self, fromclause):
return (column_collections and list(self.columns) or []) + \
self._raw_columns + list(self._froms) + \
- [x for x in (self._whereclause, self._having, self._order_by_clause, self._group_by_clause) if x is not None]
+ [x for x in
+ (self._whereclause, self._having,
+ self._order_by_clause, self._group_by_clause)
+ if x is not None]
@_generative
def column(self, column):
- """return a new select() construct with the given column expression added to its columns clause."""
+ """return a new select() construct with the given column expression
+ added to its columns clause.
+
+ """
column = _literal_as_column(column)
@_generative
def with_only_columns(self, columns):
- """return a new select() construct with its columns clause replaced with the given columns."""
+ """return a new select() construct with its columns clause replaced
+ with the given columns.
+
+ """
self._raw_columns = [
- isinstance(c, _ScalarSelect) and c.self_group(against=operators.comma_op) or c
- for c in
- [_literal_as_column(c) for c in columns]
+ isinstance(c, _ScalarSelect) and
+ c.self_group(against=operators.comma_op) or c
+ for c in [_literal_as_column(c) for c in columns]
]
@_generative
def where(self, whereclause):
- """return a new select() construct with the given expression added to its WHERE clause, joined
- to the existing clause via AND, if any."""
+ """return a new select() construct with the given expression added to its
+ WHERE clause, joined to the existing clause via AND, if any.
+
+ """
self.append_whereclause(whereclause)
@_generative
def having(self, having):
- """return a new select() construct with the given expression added to its HAVING clause, joined
- to the existing clause via AND, if any."""
-
+ """return a new select() construct with the given expression added to its HAVING
+ clause, joined to the existing clause via AND, if any.
+
+ """
self.append_having(having)
@_generative
def distinct(self):
- """return a new select() construct which will apply DISTINCT to its columns clause."""
-
+ """return a new select() construct which will apply DISTINCT to its columns
+ clause.
+
+ """
self._distinct = True
@_generative
def prefix_with(self, clause):
- """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 which will apply the given expression to the
+ start of its columns clause, not using any commas.
+ """
clause = _literal_as_text(clause)
self._prefixes = self._prefixes + [clause]
@_generative
def select_from(self, fromclause):
- """return a new select() construct with the given FROM expression applied to its list of
- FROM objects."""
+ """return a new select() construct with the given FROM expression applied to its
+ list of FROM objects.
+ """
fromclause = _literal_as_text(fromclause)
self._froms = self._froms.union([fromclause])
@_generative
def correlate(self, *fromclauses):
- """return a new select() construct which will correlate the given FROM clauses 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, correlation is disabled for the returned select().
+ """return a new select() construct which will correlate the given FROM clauses 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, correlation is disabled for the returned select().
"""
self._should_correlate = False
self._correlate = self._correlate.union([fromclause])
def append_column(self, column):
- """append the given column expression to the columns clause of this select() construct."""
-
+ """append the given column expression to the columns clause of this select()
+ construct.
+
+ """
column = _literal_as_column(column)
if isinstance(column, _ScalarSelect):
self._reset_exported()
def append_prefix(self, clause):
- """append the given columns clause prefix expression to this select() construct."""
-
+ """append the given columns clause prefix expression to this select()
+ construct.
+
+ """
clause = _literal_as_text(clause)
self._prefixes = self._prefixes.union([clause])
self._having = _literal_as_text(having)
def append_from(self, fromclause):
- """append the given FromClause expression to this select() construct's FROM clause.
+ """append the given FromClause expression to this select() construct's FROM
+ clause.
"""
if _is_literal(fromclause):
return union(self, other, **kwargs)
def union_all(self, other, **kwargs):
- """return a SQL UNION ALL of this select() construct against the given selectable."""
-
+ """return a SQL UNION ALL of this select() construct against the given
+ selectable.
+
+ """
return union_all(self, other, **kwargs)
def except_(self, other, **kwargs):
return except_(self, other, **kwargs)
def except_all(self, other, **kwargs):
- """return a SQL EXCEPT ALL of this select() construct against the given selectable."""
-
+ """return a SQL EXCEPT ALL of this select() construct against the given
+ selectable.
+
+ """
return except_all(self, other, **kwargs)
def intersect(self, other, **kwargs):
- """return a SQL INTERSECT of this select() construct against the given selectable."""
-
+ """return a SQL INTERSECT of this select() construct against the given
+ selectable.
+
+ """
return intersect(self, other, **kwargs)
def intersect_all(self, other, **kwargs):
- """return a SQL INTERSECT ALL of this select() construct against the given selectable."""
-
+ """return a SQL INTERSECT ALL of this select() construct against the given
+ selectable.
+
+ """
return intersect_all(self, other, **kwargs)
def bind(self):
return parameters
def params(self, *arg, **kw):
- raise NotImplementedError("params() is not supported for INSERT/UPDATE/DELETE statements."
- " To set the values for an INSERT or UPDATE statement, use stmt.values(**parameters).")
+ raise NotImplementedError(
+ "params() is not supported for INSERT/UPDATE/DELETE statements."
+ " To set the values for an INSERT or UPDATE statement, use"
+ " stmt.values(**parameters).")
def bind(self):
return self._bind or self.table.bind
@_generative
def values(self, *args, **kwargs):
- """specify the VALUES clause for an INSERT statement, or the SET clause for an UPDATE.
+ """specify the VALUES clause for an INSERT statement, or the SET clause for an
+ UPDATE.
\**kwargs
key=<somevalue> arguments
\*args
- A single dictionary can be sent as the first positional argument. This allows
- non-string based keys, such as Column objects, to be used.
+ A single dictionary can be sent as the first positional argument. This
+ allows non-string based keys, such as Column objects, to be used.
"""
if args:
"""
__visit_name__ = 'insert'
- def __init__(self, table, values=None, inline=False, bind=None, prefixes=None, **kwargs):
+ def __init__(self,
+ table,
+ values=None,
+ inline=False,
+ bind=None,
+ prefixes=None,
+ **kwargs):
_ValuesBase.__init__(self, table, values)
self._bind = bind
self.select = None
"""
__visit_name__ = 'update'
- def __init__(self, table, whereclause, values=None, inline=False, bind=None, **kwargs):
+ def __init__(self,
+ table,
+ whereclause,
+ values=None,
+ inline=False,
+ bind=None,
+ **kwargs):
_ValuesBase.__init__(self, table, values)
self._bind = bind
if whereclause:
@_generative
def where(self, whereclause):
- """return a new update() construct with the given expression added to its WHERE clause, joined
- to the existing clause via AND, if any."""
-
+ """return a new update() construct with the given expression added to its WHERE
+ clause, joined to the existing clause via AND, if any.
+
+ """
if self._whereclause is not None:
self._whereclause = and_(self._whereclause, _literal_as_text(whereclause))
else: