and :class:`~sqlalchemy.sql.compiler.DDLCompiler` both include a ``process()``
method which can be used for compilation of embedded attributes::
- class InsertFromSelect(ClauseElement):
+ from sqlalchemy.sql.expression import Executable, ClauseElement
+
+ class InsertFromSelect(Executable, ClauseElement):
def __init__(self, table, select):
self.table = table
self.select = select
``execute_at()`` method, allowing the construct to be invoked during CREATE
TABLE and DROP TABLE sequences.
+* :class:`~sqlalchemy.sql.expression.Executable` - This is a mixin which should be
+ used with any expression class that represents a "standalone" SQL statement that
+ can be passed directly to an ``execute()`` method. It is already implicit
+ within ``DDLElement`` and ``FunctionElement``.
+
"""
def compiles(class_, *specs):
def __init__(self, type):
self.type = type
-class _Executable(object):
+
+class _Generative(object):
+ """Allow a ClauseElement to generate itself via the
+ @_generative decorator.
+
+ """
+
+ def _generate(self):
+ s = self.__class__.__new__(self.__class__)
+ s.__dict__ = self.__dict__.copy()
+ return s
+
+
+class Executable(_Generative):
"""Mark a ClauseElement as supporting execution."""
supports_execution = True
"""
self._execution_options = self._execution_options.union(kw)
+# legacy, some outside users may be calling this
+_Executable = Executable
-class _TextClause(_Executable, ClauseElement):
+class _TextClause(Executable, ClauseElement):
"""Represent a literal SQL text fragment.
Public constructor is the :func:`text()` function.
__visit_name__ = 'textclause'
_bind_params_regex = re.compile(r'(?<![:\w\x5c]):(\w+)(?!:)', re.UNICODE)
- _execution_options = _Executable._execution_options.union({'autocommit':PARSE_AUTOCOMMIT})
+ _execution_options = Executable._execution_options.union({'autocommit':PARSE_AUTOCOMMIT})
@property
def _select_iterable(self):
else:
return None
- def _generate(self):
- s = self.__class__.__new__(self.__class__)
- s.__dict__ = self.__dict__.copy()
- return s
-
def _copy_internals(self, clone=_clone):
self.bindparams = dict((b.key, clone(b))
for b in self.bindparams.values())
def _from_objects(self):
return list(itertools.chain(*[x._from_objects for x in self.get_children()]))
-class FunctionElement(ColumnElement, FromClause):
+class FunctionElement(Executable, ColumnElement, FromClause):
"""Base for SQL function-oriented constructs."""
def __init__(self, *clauses, **kwargs):
util.reset_memoized(self, 'clauses')
def select(self):
- return select([self])
+ s = select([self])
+ if self._execution_options:
+ s = s.execution_options(**self._execution_options)
+ return s
def scalar(self):
- return select([self]).execute().scalar()
+ return self.select().execute().scalar()
def execute(self):
- return select([self]).execute()
+ return self.select().execute()
def _bind_param(self, obj):
return _BindParamClause(None, obj, _fallback_type=self.type, unique=True)
def _from_objects(self):
return [self]
-class _SelectBaseMixin(_Executable):
+class _SelectBaseMixin(Executable):
"""Base class for :class:`Select` and ``CompoundSelects``."""
def __init__(self,
self._execution_options = self._execution_options.union({'autocommit':True})
def _generate(self):
+ """Override the default _generate() method to also clear out exported collections."""
+
s = self.__class__.__new__(self.__class__)
s.__dict__ = self.__dict__.copy()
s._reset_exported()
self._bind = bind
bind = property(bind, _set_bind)
-class _UpdateBase(_Executable, ClauseElement):
+class _UpdateBase(Executable, ClauseElement):
"""Form the base for ``INSERT``, ``UPDATE``, and ``DELETE`` statements."""
__visit_name__ = 'update_base'
- _execution_options = _Executable._execution_options.union({'autocommit':True})
+ _execution_options = Executable._execution_options.union({'autocommit':True})
kwargs = util.frozendict()
- def _generate(self):
- s = self.__class__.__new__(self.__class__)
- s.__dict__ = self.__dict__.copy()
- return s
-
def _process_colparams(self, parameters):
if isinstance(parameters, (list, tuple)):
pp = {}
# TODO: coverage
self._whereclause = clone(self._whereclause)
-class _IdentifiedClause(_Executable, ClauseElement):
+class _IdentifiedClause(Executable, ClauseElement):
__visit_name__ = 'identified'
- _execution_options = _Executable._execution_options.union({'autocommit':False})
+ _execution_options = Executable._execution_options.union({'autocommit':False})
quote = None
def __init__(self, ident):