try:
coltype = ischema_names[coltype]
except KeyError:
- raise exceptions.AssertionError("Cant get coltype for type '%s' on colname '%s'" % (coltype, colname))
+ raise exceptions.AssertionError("Can't get coltype for type '%s' on colname '%s'" % (coltype, colname))
colargs = []
if default is not None:
def last_inserted_ids(self):
if self.context.last_inserted_ids is None:
- raise exceptions.InvalidRequestError("no INSERT executed, or cant use cursor.lastrowid without Postgres OIDs enabled")
+ raise exceptions.InvalidRequestError("no INSERT executed, or can't use cursor.lastrowid without Postgres OIDs enabled")
else:
return self.context.last_inserted_ids
raise NotImplementedError()
def oid_column_name(self, column):
- """Return the oid column name for this dialect, or None if the dialect cant/wont support OID/ROWID.
+ """Return the oid column name for this dialect, or None if the dialect can't/won't support OID/ROWID.
The Column instance which represents OID for the query being
compiled is passed, so that the dialect can inspect the column
if pool is None:
dbapi = kwargs.pop('module', dialect.dbapi())
if dbapi is None:
- raise exceptions.InvalidRequestError("Cant get DBAPI module for dialect '%s'" % dialect)
+ raise exceptions.InvalidRequestError("Can't get DBAPI module for dialect '%s'" % dialect)
def connect():
try:
mapperutil.BinaryVisitor(visit_binary).traverse(self.primaryjoin)
if len(self.foreign_keys) == 0:
- raise exceptions.ArgumentError("Cant locate any foreign key columns in primary join condition '%s' for relationship '%s'. Specify 'foreign_keys' argument to indicate which columns in the join condition are foreign." %(str(self.primaryjoin), str(self)))
+ raise exceptions.ArgumentError(
+ "Can't locate any foreign key columns in primary join "
+ "condition '%s' for relationship '%s'. Specify "
+ "'foreign_keys' argument to indicate which columns in "
+ "the join condition are foreign." %(str(self.primaryjoin), str(self)))
if self.secondaryjoin is not None:
mapperutil.BinaryVisitor(visit_binary).traverse(self.secondaryjoin)
manytoone = len([c for c in self.foreign_keys if self.parent.unjoined_table.c.contains_column(c)])
if not onetomany and not manytoone:
- raise exceptions.ArgumentError("Cant determine relation direction for relationship '%s' - foreign key columns are not present in neither the parent nor the child's mapped tables" %(str(self)))
+ raise exceptions.ArgumentError(
+ "Can't determine relation direction for relationship '%s' "
+ "- foreign key columns are not present in neither the "
+ "parent nor the child's mapped tables" %(str(self)))
elif onetomany and manytoone:
- raise exceptions.ArgumentError("Cant determine relation direction for relationship '%s' - foreign key columns are present in both the parent and the child's mapped tables. Specify 'foreign_keys' argument." %(str(self)))
+ raise exceptions.ArgumentError(
+ "Can't determine relation direction for relationship '%s' "
+ "- foreign key columns are present in both the parent and "
+ "the child's mapped tables. Specify 'foreign_keys' "
+ "argument." % (str(self)))
elif onetomany:
self.direction = sync.ONETOMANY
elif manytoone:
parent = prop.parent.primary_mapper()
self.kwargs.setdefault('viewonly', prop.viewonly)
self.kwargs.setdefault('post_update', prop.post_update)
- relation = PropertyLoader(parent, prop.secondary, pj, sj, backref=prop.key, is_backref=True, **self.kwargs)
+ relation = PropertyLoader(parent, prop.secondary, pj, sj,
+ backref=prop.key, is_backref=True,
+ **self.kwargs)
mapper._compile_property(self.key, relation);
elif not isinstance(mapper.props[self.key], PropertyLoader):
- raise exceptions.ArgumentError("Cant create backref '%s' on mapper '%s'; an incompatible property of that name already exists" % (self.key, str(mapper)))
+ raise exceptions.ArgumentError(
+ "Can't create backref '%s' on mapper '%s'; an incompatible "
+ "property of that name already exists" % (self.key, str(mapper)))
else:
# else set one of us as the "backreference"
parent = prop.parent.primary_mapper()
if parent.class_ is not mapper.props[self.key]._get_target_class():
- raise exceptions.ArgumentError("Backrefs do not match: backref '%s' expects to connect to %s, but found a backref already connected to %s" % (self.key, str(parent.class_), str(mapper.props[self.key].mapper.class_)))
+ raise exceptions.ArgumentError(
+ "Backrefs do not match: backref '%s' expects to connect to %s, "
+ "but found a backref already connected to %s" %
+ (self.key, str(parent.class_), str(mapper.props[self.key].mapper.class_)))
if not mapper.props[self.key].is_backref:
prop.is_backref=True
if not prop.viewonly:
return None
p = search_for_prop(start or self.mapper)
if p is None:
- raise exceptions.InvalidRequestError("Cant locate property named '%s'" % key)
+ raise exceptions.InvalidRequestError("Can't locate property named '%s'" % key)
return [keys, p]
def _col_aggregate(self, col, func):
echo_uow = property(_get_echo_uow,_set_echo_uow)
def create_transaction(self, **kwargs):
- """Return a new SessionTransaction corresponding to an
+ """Return a new ``SessionTransaction`` corresponding to an
existing or new transaction.
- If the transaction is new, the returned SessionTransaction
+ If the transaction is new, the returned ``SessionTransaction``
will have commit control over the underlying transaction, else
will have rollback control only.
"""
return self.get_bind(mapper).connect(**kwargs)
def connection(self, mapper, **kwargs):
- """Return a Connection corresponding to the given mapper.
+ """Return a ``Connection`` corresponding to the given mapper.
Used by the ``execute()`` method which performs select
- operations for Mapper and Query.
+ operations for ``Mapper`` and ``Query``.
- If this Session is transactional, the connection will be in
+ If this ``Session`` is transactional, the connection will be in
the context of this session's transaction. Otherwise, the
- connection is returned by the contextual_connect method, which
+ connection is returned by the ``contextual_connect()`` method, which
some Engines override to return a thread-local connection, and
- will have close_with_result set to True.
+ will have `close_with_result` set to `True`.
The given `**kwargs` will be sent to the engine's
``contextual_connect()`` method, if no transaction is in
return self.get_bind(mapper).contextual_connect(**kwargs)
def execute(self, mapper, clause, params, **kwargs):
- """Using the given mapper to identify the appropriate Engine
- or Connection to be used for statement execution, execute the
- given ClauseElement using the provided parameter dictionary.
+ """Using the given mapper to identify the appropriate ``Engine``
+ or ``Connection`` to be used for statement execution, execute the
+ given ``ClauseElement`` using the provided parameter dictionary.
- Return a ResultProxy corresponding to the execution's results.
+ Return a ``ResultProxy`` corresponding to the execution's results.
- If this method allocates a new Connection for the operation,
- then the ResultProxy's ``close()`` method will release the
- resources of the underlying Connection, otherwise its a no-op.
+ If this method allocates a new ``Connection`` for the operation,
+ then the ``ResultProxy`` 's ``close()`` method will release the
+ resources of the underlying ``Connection``, otherwise its a no-op.
"""
return self.connection(mapper, close_with_result=True).execute(clause, params, **kwargs)
self.transaction.close()
def clear(self):
- """Remove all object instances from this Session.
+ """Remove all object instances from this ``Session``.
This is equivalent to calling ``expunge()`` for all objects in
- this Session.
+ this ``Session``.
"""
for instance in self:
self.uow.echo = echo
def mapper(self, class_, entity_name=None):
- """Given an Class, return the primary Mapper responsible for
+ """Given a ``Class``, return the primary ``Mapper`` responsible for
persisting it."""
return _class_mapper(class_, entity_name = entity_name)
def bind_mapper(self, mapper, bindto):
- """Bind the given `Mapper` to the given Engine or Connection.
+ """Bind the given `mapper` to the given ``Engine`` or ``Connection``.
- All subsequent operations involving this Mapper will use the
+ All subsequent operations involving this ``Mapper`` will use the
given `bindto`.
"""
self.binds[mapper] = bindto
def bind_table(self, table, bindto):
- """Bind the given `Table` to the given Engine or Connection.
+ """Bind the given `table` to the given ``Engine`` or ``Connection``.
- All subsequent operations involving this Table will use the
+ All subsequent operations involving this ``Table`` will use the
given `bindto`.
"""
self.binds[table] = bindto
def get_bind(self, mapper):
- """Return the Engine or Connection which is used to execute
- statements on behalf of the given `Mapper`.
+ """Return the ``Engine`` or ``Connection`` which is used to execute
+ statements on behalf of the given `mapper`.
Calling ``connect()`` on the return result will always result
- in a Connection object. This method disregards any
- SessionTransaction that may be in progress.
+ in a ``Connection`` object. This method disregards any
+ ``SessionTransaction`` that may be in progress.
The order of searching is as follows:
- 1. if an Engine or Connection was bound to this Mapper
- specifically within this Session, return that Engine or
- Connection.
+ 1. if an ``Engine`` or ``Connection`` was bound to this ``Mapper``
+ specifically within this ``Session``, return that ``Engine`` or
+ ``Connection``.
- 2. if an Engine or Connection was bound to this Mapper's
- underlying Table within this Session (i.e. not to the Table
- directly), return that Engine or Conneciton.
+ 2. if an ``Engine`` or ``Connection`` was bound to this `mapper` 's
+ underlying ``Table`` within this ``Session`` (i.e. not to the ``Table``
+ directly), return that ``Engine`` or ``Connection``.
- 3. if an Engine or Connection was bound to this Session,
- return that Engine or Connection.
+ 3. if an ``Engine`` or ``Connection`` was bound to this ``Session``,
+ return that ``Engine`` or ``Connection``.
- 4. finally, return the Engine which was bound directly to the
- Table's MetaData object.
+ 4. finally, return the ``Engine`` which was bound directly to the
+ ``Table`` 's ``MetaData`` object.
- If no Engine is bound to the Table, an exception is raised.
+ If no ``Engine`` is bound to the ``Table``, an exception is raised.
"""
if mapper is None:
return e
def query(self, mapper_or_class, entity_name=None, **kwargs):
- """Return a new Query object corresponding to this Session and
+ """Return a new ``Query`` object corresponding to this ``Session`` and
the mapper, or the classes' primary mapper.
"""
to the database.
`objects` is a list or tuple of objects specifically to be
- flushed; if None, all new and modified objects are flushed.
+ flushed; if ``None``, all new and modified objects are flushed.
"""
self.uow.flush(self, objects)
def get(self, class_, ident, **kwargs):
"""Return an instance of the object based on the given
- identifier, or None if not found.
+ identifier, or ``None`` if not found.
The `ident` argument is a scalar or tuple of primary key
column values in the order of the table def's primary key
If not found, raises an exception. The method will **remove
all pending changes** to the object already existing in the
- Session. The `ident` argument is a scalar or tuple of primary
+ ``Session``. The `ident` argument is a scalar or tuple of primary
key columns in the order of the table def's primary key
columns.
The `entity_name` keyword argument may also be specified which
- further qualifies the underlying Mapper used to perform the
+ further qualifies the underlying ``Mapper`` used to perform the
query.
"""
return ret
def expunge(self, object):
- """Remove the given object from this Session.
+ """Remove the given `object` from this ``Session``.
This will free all internal references to the object.
Cascading will be applied according to the *expunge* cascade
self._unattach(c)
def save(self, object, entity_name=None):
- """Add a transient (unsaved) instance to this Session.
+ """Add a transient (unsaved) instance to this ``Session``.
This operation cascades the `save_or_update` method to
associated instances if the relation is mapped with
``cascade="save-update"``.
The `entity_name` keyword argument will further qualify the
- specific Mapper used to handle this instance.
+ specific ``Mapper`` used to handle this instance.
"""
self._save_impl(object, entity_name=entity_name)
- _object_mapper(object).cascade_callable('save-update', object, lambda c, e:self._save_or_update_impl(c, e), halt_on=lambda c:c in self)
+ _object_mapper(object).cascade_callable('save-update', object,
+ lambda c, e:self._save_or_update_impl(c, e),
+ halt_on=lambda c:c in self)
def update(self, object, entity_name=None):
"""Bring the given detached (saved) instance into this
- Session.
+ ``Session``.
If there is a persistent instance with the same identifier
- already associated with this Session, an exception is thrown.
+ already associated with this ``Session``, an exception is thrown.
This operation cascades the `save_or_update` method to
associated instances if the relation is mapped with
"""
self._update_impl(object, entity_name=entity_name)
- _object_mapper(object).cascade_callable('save-update', object, lambda c, e:self._save_or_update_impl(c, e), halt_on=lambda c:c in self)
+ _object_mapper(object).cascade_callable('save-update', object,
+ lambda c, e:self._save_or_update_impl(c, e),
+ halt_on=lambda c:c in self)
def save_or_update(self, object, entity_name=None):
- """Save or update the given object into this Session.
+ """Save or update the given object into this ``Session``.
The presence of an `_instance_key` attribute on the instance
determines whether to ``save()`` or ``update()`` the instance.
"""
self._save_or_update_impl(object, entity_name=entity_name)
- _object_mapper(object).cascade_callable('save-update', object, lambda c, e:self._save_or_update_impl(c, e), halt_on=lambda c:c in self)
+ _object_mapper(object).cascade_callable('save-update', object,
+ lambda c, e:self._save_or_update_impl(c, e),
+ halt_on=lambda c:c in self)
def _save_or_update_impl(self, object, entity_name=None):
key = getattr(object, '_instance_key', None)
self.uow.register_deleted(c)
def merge(self, object, entity_name=None, _recursive=None):
- """Copy the state of the given object onto the persistent
+ """Copy the state of the given `object` onto the persistent
object with the same identifier.
If there is no persistent instance currently associated with
Valid call signatures:
- identity_key(class\_, ident, entity_name=None)
- class\_
- mapped class (must be a positional argument)
-
- ident
- primary key, if the key is composite this is a tuple
-
- entity_name
- optional entity name
-
- identity_key(instance=instance)
- instance
- object instance (must be given as a keyword arg)
-
- identity_key(class\_, row=row, entity_name=None)
- class\_
- mapped class (must be a positional argument)
-
- row
- result proxy row (must be given as a keyword arg)
-
- entity_name
- optional entity name (must be given as a keyword arg)
+ * ``identity_key(class, ident, entity_name=None)``
+
+ class
+ mapped class (must be a positional argument)
+
+ ident
+ primary key, if the key is composite this is a tuple
+
+ entity_name
+ optional entity name
+
+ * ``identity_key(instance=instance)``
+
+ instance
+ object instance (must be given as a keyword arg)
+
+ * ``identity_key(class, row=row, entity_name=None)``
+
+ class
+ mapped class (must be a positional argument)
+
+ row
+ result proxy row (must be given as a keyword arg)
+
+ entity_name
+ optional entity name (must be given as a keyword arg)
"""
+
if args:
if len(args) == 1:
class_ = args[0]
def _save_impl(self, object, **kwargs):
if hasattr(object, '_instance_key'):
if not self.identity_map.has_key(object._instance_key):
- raise exceptions.InvalidRequestError("Instance '%s' is a detached instance or is already persistent in a different Session" % repr(object))
+ raise exceptions.InvalidRequestError("Instance '%s' is a detached instance "
+ "or is already persistent in a "
+ "different Session" % repr(object))
else:
m = _class_mapper(object.__class__, entity_name=kwargs.get('entity_name', None))
# this would be a nice exception to raise...however this is incompatible with a contextual
# session which puts all objects into the session upon construction.
#if m._is_orphan(object):
- # raise exceptions.InvalidRequestError("Instance '%s' is an orphan, and must be attached to a parent object to be saved" % (repr(object)))
+ # raise exceptions.InvalidRequestError("Instance '%s' is an orphan, "
+ # "and must be attached to a parent "
+ # "object to be saved" % (repr(object)))
m._assign_entity_name(object)
self._register_pending(object)
self.uow.register_deleted(obj)
def _attach(self, obj):
- """Attach the given object to this Session."""
+ """Attach the given object to this ``Session``."""
if getattr(obj, '_sa_session_id', None) != self.hash_key:
old = getattr(obj, '_sa_session_id', None)
if old is not None and _sessions.has_key(old):
- raise exceptions.InvalidRequestError("Object '%s' is already attached to session '%s' (this is '%s')" % (repr(obj), old, id(self)))
+ raise exceptions.InvalidRequestError("Object '%s' is already attached "
+ "to session '%s' (this is '%s')" %
+ (repr(obj), old, id(self)))
# auto-removal from the old session is disabled. but if we decide to
# turn it back on, do it as below: gingerly since _sessions is a WeakValueDict
def _validate_persistent(self, obj):
"""Validate that the given object is persistent within this
- Session.
+ ``Session``.
"""
self.uow._validate_obj(obj)
return self.identity_map.has_key(key)
dirty = property(lambda s:s.uow.locate_dirty(),
- doc="A Set of all objects marked as 'dirty' within this Session")
+ doc="A ``Set`` of all objects marked as 'dirty' within this ``Session``")
+
deleted = property(lambda s:s.uow.deleted,
- doc="A Set of all objects marked as 'deleted' within this Session")
+ doc="A ``Set`` of all objects marked as 'deleted' within this ``Session``")
+
new = property(lambda s:s.uow.new,
- doc="A Set of all objects marked as 'new' within this Session.")
+ doc="A ``Set`` of all objects marked as 'new' within this ``Session``.")
+
identity_map = property(lambda s:s.uow.identity_map,
- doc="A dictionary consisting of all objects within this Session keyed to their _instance_key value.")
+ doc="A dictionary consisting of all objects "
+ "within this ``Session`` keyed to their `_instance_key` value.")
def import_instance(self, *args, **kwargs):
"""Deprecated. A synynom for ``merge()``."""
+
return self.merge(*args, **kwargs)
# this is the AttributeManager instance used to provide attribute behavior on objects.
_sessions = weakref.WeakValueDictionary()
def object_session(obj):
- """Return the Session to which the given object is bound, or None if none."""
+ """Return the ``Session`` to which the given object is bound, or ``None`` if none."""
hashkey = getattr(obj, '_sa_session_id', None)
if hashkey is not None:
def init(self):
super(EagerLoader, self).init()
if self.parent.isa(self.mapper):
- raise exceptions.ArgumentError("Error creating eager relationship '%s' on parent class '%s' to child class '%s': Cant use eager loading on a self referential relationship." % (self.key, repr(self.parent.class_), repr(self.mapper.class_)))
+ raise exceptions.ArgumentError(
+ "Error creating eager relationship '%s' on parent class '%s' "
+ "to child class '%s': Cant use eager loading on a self "
+ "referential relationship." %
+ (self.key, repr(self.parent.class_), repr(self.mapper.class_)))
self.parent._eager_loaders.add(self.parent_property)
self.clauses = {}
def register_new(self, obj):
if hasattr(obj, '_instance_key'):
- raise InvalidRequestError("Object '%s' already has an identity - it cant be registered as new" % repr(obj))
+ raise InvalidRequestError("Object '%s' already has an identity - it can't be registered as new" % repr(obj))
if obj not in self.new:
self.new.add(obj)
obj._sa_insert_order = len(self.new)
from sqlalchemy import sql, util, exceptions
-all_cascades = util.Set(["delete", "delete-orphan", "all", "merge", "expunge", "save-update", "refresh-expire", "none"])
+all_cascades = util.Set(["delete", "delete-orphan", "all", "merge",
+ "expunge", "save-update", "refresh-expire", "none"])
class CascadeOptions(object):
"""Keeps track of the options sent to relation().cascade"""
self.merge = "merge" in values or "all" in values
self.expunge = "expunge" in values or "all" in values
self.refresh_expire = "refresh-expire" in values or "all" in values
-
for x in values:
if x not in all_cascades:
raise exceptions.ArgumentError("Invalid cascade option '%s'" % x)
return getattr(self, item.replace("-", "_"), False)
def __repr__(self):
- return "CascadeOptions(arg=%s)" % repr(",".join([x for x in ['delete', 'save_update', 'merge', 'expunge', 'delete_orphan', 'refresh-expire'] if getattr(self, x, False) is True]))
+ return "CascadeOptions(arg=%s)" % repr(",".join(
+ [x for x in ['delete', 'save_update', 'merge', 'expunge',
+ 'delete_orphan', 'refresh-expire']
+ if getattr(self, x, False) is True]))
def polymorphic_union(table_map, typecolname, aliasname='p_union'):
"""Create a ``UNION`` statement used by a polymorphic mapper.
- See the SQLAlchemy advanced mapping docs for an example of how
+ See the `SQLAlchemy` advanced mapping docs for an example of how
this is used.
"""
result = []
for type, table in table_map.iteritems():
if typecolname is not None:
- result.append(sql.select([col(name, table) for name in colnames] + [sql.literal_column("'%s'" % type).label(typecolname)], from_obj=[table]))
+ result.append(sql.select([col(name, table) for name in colnames] +
+ [sql.literal_column("'%s'" % type).label(typecolname)],
+ from_obj=[table]))
else:
result.append(sql.select([col(name, table) for name in colnames], from_obj=[table]))
return sql.union_all(*result).alias(aliasname)
class TranslatingDict(dict):
- """A dictionary that stores ColumnElement objects as keys.
+ """A dictionary that stores ``ColumnElement`` objects as keys.
- Incoming ColumnElement keys are translated against those of an
- underling FromClause for all operations. This way the columns
- from any Selectable that is derived from or underlying this
- TranslatingDict's selectable can be used as keys.
+ Incoming ``ColumnElement`` keys are translated against those of an
+ underling ``FromClause`` for all operations. This way the columns
+ from any ``Selectable`` that is derived from or underlying this
+ ``TranslatingDict`` 's selectable can be used as keys.
"""
def __init__(self, selectable):
for c in cc.clauses:
c.parens = False
return cc
-
+
def cast(clause, totype, **kwargs):
- """return CAST function CAST(clause AS totype)
-
- Use with a sqlalchemy.types.TypeEngine object, i.e
- ``cast(table.c.unit_price * table.c.qty, Numeric(10,4))``
- or ``cast(table.c.timestamp, DATE)``
-
+ """Return ``CAST`` function.
+
+ Equivalent of SQL ``CAST(clause AS totype)``.
+
+ Use with a ``sqlalchemy.types.TypeEngine`` object, i.e::
+
+ cast(table.c.unit_price * table.c.qty, Numeric(10,4))
+
+ or::
+
+ cast(table.c.timestamp, DATE)
"""
+
return _Cast(clause, totype, **kwargs)
def extract(field, expr):
- """return extract(field FROM expr)"""
+ """Return ``extract(field FROM expr)``."""
+
expr = _BinaryClause(text(field), expr, "FROM")
return func.extract(expr)
-
def exists(*args, **kwargs):
return _Exists(*args, **kwargs)
with Selectable objects.
""")
-
def _one_fkey(self):
if len(self._foreign_keys):
return list(self._foreign_keys)[0]
else:
return None
+
foreign_key = property(_one_fkey)
def _get_orig_set(self):
if len(s) == 0:
s.add(self)
self.__orig_set = s
+
orig_set = property(_get_orig_set, _set_orig_set,
doc=\
"""A Set containing TableClause-bound, non-proxied ColumnElements
""")
def shares_lineage(self, othercolumn):
- """Return True if the given ``ColumnElement`` has a common ancestor to this ``ColumnElement``."""
+ """Return True if the given ``ColumnElement`` has a common
+ ancestor to this ``ColumnElement``.
+ """
for c in self.orig_set:
if c in othercolumn.orig_set:
if typemap is not None:
for key in typemap.keys():
typemap[key] = sqltypes.to_instance(typemap[key])
+
def repl(m):
self.bindparams[m.group(1)] = bindparam(m.group(1))
return ":%s" % m.group(1)
- # scan the string and search for bind parameter names, add them
+
+ # scan the string and search for bind parameter names, add them
# to the list of bindparams
self.text = re.compile(r'(?<!:):([\w_]+)', re.S).sub(repl, text)
if bindparams is not None:
for b in bindparams:
self.bindparams[b.key] = b
+
columns = property(lambda s:[])
def get_children(self, **kwargs):
def _get_from_objects(self):
return []
+
def supports_execution(self):
return True
-
+
class _Null(ColumnElement):
- """represents the NULL keyword in a SQL statement. public contstructor is the
- null() function."""
+ """Represent the NULL keyword in a SQL statement.
+
+ Public constructor is the ``null()`` function.
+ """
+
def __init__(self):
self.type = sqltypes.NULLTYPE
+
def accept_visitor(self, visitor):
visitor.visit_null(self)
+
def _get_from_objects(self):
return []
class ClauseList(ClauseElement):
- """describes a list of clauses. by default, is comma-separated,
- such as a column listing."""
+ """Describe a list of clauses.
+
+ By default, is comma-separated, such as a column listing.
+ """
+
def __init__(self, *clauses, **kwargs):
self.clauses = []
for c in clauses:
if c is None: continue
self.append(c)
self.parens = kwargs.get('parens', False)
+
def __iter__(self):
return iter(self.clauses)
+
def copy_container(self):
clauses = [clause.copy_container() for clause in self.clauses]
return ClauseList(parens=self.parens, *clauses)
+
def append(self, clause):
if _is_literal(clause):
clause = _TextClause(str(clause))
self.clauses.append(clause)
+
def get_children(self, **kwargs):
return self.clauses
+
def accept_visitor(self, visitor):
visitor.visit_clauselist(self)
+
def _get_from_objects(self):
f = []
for c in self.clauses:
f += c._get_from_objects()
return f
+
def compare(self, other):
- """compares this ClauseList to the given ClauseList, including
- a comparison of all the clause items."""
+ """Compare this ``ClauseList`` to the given ``ClauseList``,
+ including a comparison of all the clause items.
+ """
+
if isinstance(other, ClauseList) and len(self.clauses) == len(other.clauses):
for i in range(0, len(self.clauses)):
if not self.clauses[i].compare(other.clauses[i]):
self.left.parens = True
if isinstance(self.right, _BinaryClause) or hasattr(self.right, '_selectable'):
self.right.parens = True
+
def copy_container(self):
return self.__class__(self.left.copy_container(), self.right.copy_container(), self.operator)
+
def _get_from_objects(self):
return self.left._get_from_objects() + self.right._get_from_objects()
+
def get_children(self, **kwargs):
return self.left, self.right
+
def accept_visitor(self, visitor):
visitor.visit_binary(self)
+
def swap(self):
c = self.left
self.left = self.right
self.right = c
+
def compare(self, other):
- """compares this _BinaryClause against the given _BinaryClause."""
+ """Compare this ``_BinaryClause`` against the given ``_BinaryClause``."""
+
return (
- isinstance(other, _BinaryClause) and self.operator == other.operator and
+ isinstance(other, _BinaryClause) and self.operator == other.operator and
self.left.compare(other.left) and self.right.compare(other.right)
)
class _BinaryExpression(_BinaryClause, ColumnElement):
- """represents a binary expression, which can be in a WHERE criterion or in the column list
- of a SELECT. By adding "ColumnElement" to its inherited list, it becomes a Selectable
- unit which can be placed in the column list of a SELECT."""
+ """Represent a binary expression, which can be in a ``WHERE``
+ criterion or in the column list of a ``SELECT``.
+
+ By adding ``ColumnElement`` to its inherited list, it becomes a
+ ``Selectable`` unit which can be placed in the column list of a
+ ``SELECT``."""
+
pass
class _BooleanExpression(_BinaryExpression):
- """represents a boolean expression."""
+ """Represent a boolean expression."""
+
def __init__(self, *args, **kwargs):
self.negate = kwargs.pop('negate', None)
super(_BooleanExpression, self).__init__(*args, **kwargs)
+
def _negate(self):
if self.negate is not None:
return _BooleanExpression(self.left, self.right, self.negate, negate=self.operator, type=self.type)
constraints.add(fk.constraint)
self.foreignkey = fk.parent
if len(crit) == 0:
- raise exceptions.ArgumentError("Cant find any foreign key relationships between '%s' and '%s'" % (primary.name, secondary.name))
+ raise exceptions.ArgumentError("Can't find any foreign key relationships "
+ "between '%s' and '%s'" % (primary.name, secondary.name))
elif len(constraints) > 1:
- raise exceptions.ArgumentError("Cant determine join between '%s' and '%s'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly." % (primary.name, secondary.name))
+ raise exceptions.ArgumentError("Can't determine join between '%s' and '%s'; "
+ "tables have more than one foreign key "
+ "constraint relationship between them. "
+ "Please specify the 'onclause' of this "
+ "join explicitly." % (primary.name, secondary.name))
elif len(crit) == 1:
return (crit[0])
else:
def get_children(self, **kwargs):
return self.left, self.right, self.onclause
+
def accept_visitor(self, visitor):
visitor.visit_join(self)
def get_children(self, **kwargs):
return self.obj,
+
def accept_visitor(self, visitor):
visitor.visit_label(self)
self.is_literal = is_literal
def _get_label(self):
- """generate a 'label' for this column.
+ """Generate a 'label' for this column.
- the label is a product of the parent table name and column name, and
- is treated as a unique identifier of this Column across all Tables and derived
- selectables for a particular metadata collection.
+ The label is a product of the parent table name and column
+ name, and is treated as a unique identifier of this ``Column``
+ across all ``Tables`` and derived selectables for a particular
+ metadata collection.
"""
# for a "literal" column, we've no idea what the text is
if self.is_scalar and not hasattr(self, 'type'):
self.type = column.type
- # if the column is a Select statement itself,
+ # if the column is a Select statement itself,
# accept visitor
self.__correlator.traverse(column)
-
+
# visit the FROM objects of the column looking for more Selects
for f in column._get_from_objects():
if f is not self:
self.__correlator.traverse(f)
self._process_froms(column, False)
+
def _make_proxy(self, selectable, name):
if self.is_scalar:
return self._raw_columns[0]._make_proxy(selectable, name)
else:
raise exceptions.InvalidRequestError("Not a scalar select statement")
+
def label(self, name):
if not self.is_scalar:
raise exceptions.InvalidRequestError("Not a scalar select statement")
else:
return label(name, self)
-
+
def _exportable_columns(self):
return [c for c in self._raw_columns if isinstance(c, Selectable)]
return column._make_proxy(self, name=column._label)
else:
return column._make_proxy(self)
-
+
def _process_froms(self, elem, asfrom):
for f in elem._get_from_objects():
self.__froms.add(f)
else:
return f
- froms = property(_calc_froms, doc="""A collection containing all elements of the FROM clause""")
+ froms = property(_calc_froms,
+ doc="""A collection containing all elements
+ of the ``FROM`` clause.""")
def get_children(self, column_collections=True, **kwargs):
return (column_collections and list(self.columns) or []) + \