diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-12-11 23:28:01 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-12-11 23:28:01 +0000 |
commit | b22edf1d8a16467a8d5625f4ddf7dca9403bdfe0 (patch) | |
tree | 785fb5bad86f24a267bb42d72a52cbe3c2f747d7 /lib/sqlalchemy/sql | |
parent | 8d3fab1250fdb9e5cf389fcfa6a8c5f7f0ebbded (diff) | |
download | sqlalchemy-b22edf1d8a16467a8d5625f4ddf7dca9403bdfe0.tar.gz |
- turn __visit_name__ into an explicit member.
[ticket:1244]
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r-- | lib/sqlalchemy/sql/expression.py | 49 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/functions.py | 4 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/util.py | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/visitors.py | 21 |
4 files changed, 60 insertions, 20 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 49629069c..6bda4c82c 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -961,6 +961,8 @@ def is_column(col): class ClauseElement(Visitable): """Base class for elements of a programmatically constructed SQL expression.""" + __visit_name__ = 'clause' + _annotations = {} supports_execution = False _from_objects = [] @@ -1567,6 +1569,7 @@ class ColumnElement(ClauseElement, _CompareMixin): """ + __visit_name__ = 'column' primary_key = False foreign_keys = [] quote = None @@ -1734,6 +1737,7 @@ class ColumnSet(util.OrderedSet): class Selectable(ClauseElement): """mark a class as being selectable""" + __visit_name__ = 'selectable' class FromClause(Selectable): """Represent an element that can be used within the ``FROM`` clause of a ``SELECT`` statement.""" @@ -2062,6 +2066,8 @@ class _Null(ColumnElement): """ + __visit_name__ = 'null' + def __init__(self): self.type = sqltypes.NULLTYPE @@ -2211,6 +2217,8 @@ class _Function(_CalculatedClause, FromClause): """ + __visit_name__ = 'function' + def __init__(self, name, *clauses, **kwargs): self.packagenames = kwargs.get('packagenames', None) or [] self.name = name @@ -2237,6 +2245,8 @@ class _Function(_CalculatedClause, FromClause): class _Cast(ColumnElement): + __visit_name__ = 'cast' + def __init__(self, clause, totype, **kwargs): self.type = sqltypes.to_instance(totype) self.clause = _literal_as_binds(clause, None) @@ -2255,6 +2265,9 @@ class _Cast(ColumnElement): class _UnaryExpression(ColumnElement): + + __visit_name__ = 'unary' + def __init__(self, element, operator=None, modifier=None, type_=None, negate=None): self.operator = operator self.modifier = modifier @@ -2304,6 +2317,8 @@ class _UnaryExpression(ColumnElement): class _BinaryExpression(ColumnElement): """Represent an expression that is ``LEFT <operator> RIGHT``.""" + __visit_name__ = 'binary' + def __init__(self, left, right, operator, type_=None, negate=None, modifiers=None): self.left = _literal_as_text(left).self_group(against=operator) self.right = _literal_as_text(right).self_group(against=operator) @@ -2408,6 +2423,7 @@ class Join(FromClause): off all ``FromClause`` subclasses. """ + __visit_name__ = 'join' def __init__(self, left, right, onclause=None, isouter=False): self.left = _selectable(left) @@ -2528,6 +2544,7 @@ class Alias(FromClause): """ + __visit_name__ = 'alias' named_with_column = True def __init__(self, selectable, alias=None): @@ -2584,6 +2601,8 @@ class Alias(FromClause): class _Grouping(ColumnElement): """Represent a grouping within a column expression""" + __visit_name__ = 'grouping' + def __init__(self, element): self.element = element self.type = getattr(element, 'type', None) @@ -2656,6 +2675,8 @@ class _Label(ColumnElement): """ + __visit_name__ = 'label' + def __init__(self, name, element, type_=None): while isinstance(element, _Label): element = element.element @@ -2729,6 +2750,8 @@ class ColumnClause(_Immutable, ColumnElement): ``ColumnClause``. """ + __visit_name__ = 'column' + def __init__(self, text, selectable=None, type_=None, is_literal=False): self.key = self.name = text self.table = selectable @@ -2801,6 +2824,8 @@ class TableClause(_Immutable, FromClause): """ + __visit_name__ = 'table' + named_with_column = True def __init__(self, name, *columns): @@ -2994,7 +3019,6 @@ class _SelectBaseMixin(object): class _ScalarSelect(_Grouping): - __visit_name__ = 'grouping' _from_objects = [] def __init__(self, element): @@ -3020,6 +3044,8 @@ class _ScalarSelect(_Grouping): class CompoundSelect(_SelectBaseMixin, FromClause): """Forms the basis of ``UNION``, ``UNION ALL``, and other SELECT-based set operations.""" + __visit_name__ = 'compound_select' + def __init__(self, keyword, *selects, **kwargs): self._should_correlate = kwargs.pop('correlate', False) self.keyword = keyword @@ -3087,6 +3113,9 @@ class Select(_SelectBaseMixin, FromClause): ability to execute themselves and return a result set. """ + + __visit_name__ = 'select' + def __init__(self, columns, whereclause=None, from_obj=None, distinct=False, having=None, correlate=True, prefixes=None, **kwargs): """Construct a Select object. @@ -3444,6 +3473,8 @@ class Select(_SelectBaseMixin, FromClause): class _UpdateBase(ClauseElement): """Form the base for ``INSERT``, ``UPDATE``, and ``DELETE`` statements.""" + __visit_name__ = 'update_base' + supports_execution = True _autocommit = True @@ -3477,6 +3508,9 @@ class _UpdateBase(ClauseElement): bind = property(bind, _set_bind) class _ValuesBase(_UpdateBase): + + __visit_name__ = 'values_base' + def __init__(self, table, values): self.table = table self.parameters = self._process_colparams(values) @@ -3512,6 +3546,8 @@ class Insert(_ValuesBase): The ``Insert`` object is created using the :func:`insert()` function. """ + __visit_name__ = 'insert' + def __init__(self, table, values=None, inline=False, bind=None, prefixes=None, **kwargs): _ValuesBase.__init__(self, table, values) self._bind = bind @@ -3550,6 +3586,8 @@ class Update(_ValuesBase): The ``Update`` object is created using the :func:`update()` function. """ + __visit_name__ = 'update' + def __init__(self, table, whereclause, values=None, inline=False, bind=None, **kwargs): _ValuesBase.__init__(self, table, values) self._bind = bind @@ -3589,6 +3627,8 @@ class Delete(_UpdateBase): """ + __visit_name__ = 'delete' + def __init__(self, table, whereclause, bind=None, **kwargs): self._bind = bind self.table = table @@ -3619,6 +3659,7 @@ class Delete(_UpdateBase): self._whereclause = clone(self._whereclause) class _IdentifiedClause(ClauseElement): + __visit_name__ = 'identified' supports_execution = True _autocommit = False quote = None @@ -3627,10 +3668,10 @@ class _IdentifiedClause(ClauseElement): self.ident = ident class SavepointClause(_IdentifiedClause): - pass + __visit_name__ = 'savepoint' class RollbackToSavepointClause(_IdentifiedClause): - pass + __visit_name__ = 'rollback_to_savepoint' class ReleaseSavepointClause(_IdentifiedClause): - pass + __visit_name__ = 'release_savepoint' diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index 8b62910ac..b57b242f5 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -6,10 +6,6 @@ from sqlalchemy.sql import operators from sqlalchemy.sql.visitors import VisitableType class _GenericMeta(VisitableType): - def __init__(cls, clsname, bases, dict): - cls.__visit_name__ = 'function' - type.__init__(cls, clsname, bases, dict) - def __call__(self, *args, **kwargs): args = [_literal_as_binds(c) for c in args] return type.__call__(self, *args, **kwargs) diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py index 547753b28..9b9b9ec09 100644 --- a/lib/sqlalchemy/sql/util.py +++ b/lib/sqlalchemy/sql/util.py @@ -121,7 +121,7 @@ def join_condition(a, b, ignore_nonexistent_tables=False): else: return sql.and_(*crit) - + class Annotated(object): """clones a ClauseElement and applies an 'annotations' dictionary. @@ -146,7 +146,9 @@ class Annotated(object): try: cls = annotated_classes[element.__class__] except KeyError: - raise KeyError("Class %s has not defined an Annotated subclass" % element.__class__) + cls = annotated_classes[element.__class__] = type.__new__(type, + "Annotated%s" % element.__class__.__name__, + (Annotated, element.__class__), {}) return object.__new__(cls) def __init__(self, element, values): diff --git a/lib/sqlalchemy/sql/visitors.py b/lib/sqlalchemy/sql/visitors.py index 5d1d53cf8..17b9c59d5 100644 --- a/lib/sqlalchemy/sql/visitors.py +++ b/lib/sqlalchemy/sql/visitors.py @@ -28,21 +28,22 @@ __all__ = ['VisitableType', 'Visitable', 'ClauseVisitor', 'cloned_traverse', 'replacement_traverse'] class VisitableType(type): - """Metaclass which applies a `__visit_name__` attribute and - `_compiler_dispatch` method to classes. + """Metaclass which checks for a `__visit_name__` attribute and + applies `_compiler_dispatch` method to classes. """ - def __init__(cls, clsname, bases, dict): - if not '__visit_name__' in cls.__dict__: - m = re.match(r'_?(\w+?)(?:Expression|Clause|Element|$)', clsname) - x = m.group(1) - x = re.sub(r'(?!^)[A-Z]', lambda m:'_'+m.group(0).lower(), x) - cls.__visit_name__ = x.lower() + def __init__(cls, clsname, bases, clsdict): + if cls.__name__ == 'Visitable': + super(VisitableType, cls).__init__(clsname, bases, clsdict) + return + + assert hasattr(cls, '__visit_name__'), "`Visitable` descendants " \ + "should define `__visit_name__`" # set up an optimized visit dispatch function # for use by the compiler - visit_name = cls.__dict__["__visit_name__"] + visit_name = cls.__visit_name__ if isinstance(visit_name, str): getter = operator.attrgetter("visit_%s" % visit_name) def _compiler_dispatch(self, visitor, **kw): @@ -53,7 +54,7 @@ class VisitableType(type): cls._compiler_dispatch = _compiler_dispatch - super(VisitableType, cls).__init__(clsname, bases, dict) + super(VisitableType, cls).__init__(clsname, bases, clsdict) class Visitable(object): """Base class for visitable objects, applies the |