]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Replace a big loop + dict lookup in Connection.execute() with a simple visitor
authorAlex Gaynor <alex.gaynor@gmail.com>
Wed, 25 Sep 2013 17:29:52 +0000 (10:29 -0700)
committerAlex Gaynor <alex.gaynor@gmail.com>
Wed, 25 Sep 2013 17:29:52 +0000 (10:29 -0700)
pattern

lib/sqlalchemy/engine/base.py
lib/sqlalchemy/sql/compiler.py
lib/sqlalchemy/sql/ddl.py
lib/sqlalchemy/sql/elements.py
lib/sqlalchemy/sql/functions.py
lib/sqlalchemy/sql/schema.py

index 9a10e829e2a1e91010699222d90774e58264b54b..93539cb1483c033340512b8a49f5d8f1b6bc0638 100644 (file)
@@ -652,17 +652,16 @@ class Connection(Connectable):
          DBAPI-agnostic way, use the :func:`~.expression.text` construct.
 
         """
-        for c in type(object).__mro__:
-            if c in Connection.executors:
-                return Connection.executors[c](
-                                                self,
-                                                object,
-                                                multiparams,
-                                                params)
-        else:
+        if isinstance(object, util.string_types[0]):
+            return self._execute_text(object, multiparams, params)
+        try:
+            meth = object._execute_on_connection
+        except AttributeError:
             raise exc.InvalidRequestError(
                                 "Unexecutable object type: %s" %
                                 type(object))
+        else:
+            return meth(self, multiparams, params)
 
     def _execute_function(self, func, multiparams, params):
         """Execute a sql.FunctionElement object."""
@@ -1038,16 +1037,6 @@ class Connection(Connectable):
             if self.should_close_with_result:
                 self.close()
 
-    # poor man's multimethod/generic function thingy
-    executors = {
-        expression.FunctionElement: _execute_function,
-        expression.ClauseElement: _execute_clauseelement,
-        Compiled: _execute_compiled,
-        schema.SchemaItem: _execute_default,
-        ddl.DDLElement: _execute_ddl,
-        util.string_types[0]: _execute_text
-    }
-
     def default_schema_name(self):
         return self.engine.dialect.get_default_schema_name(self)
 
index 8e3cf17292f1647ab5e7dc06bff3d0ce41f3f49e..f81886240c5b8e346b8fec034d63e9797a368b8b 100644 (file)
@@ -200,6 +200,9 @@ class Compiled(object):
         """Produce the internal string representation of this element."""
         pass
 
+    def _execute_on_connection(self, connection, multiparams, params):
+        return connection._execute_compiled(self, multiparams, params)
+
     @property
     def sql_compiler(self):
         """Return a Compiled that is capable of processing SQL expressions.
index 2a81b3394bd2d9edfb09ba60f4f9553fd722b4af..72ef0773240a35a2fc152919e418e628d49cce9d 100644 (file)
@@ -63,6 +63,9 @@ class DDLElement(Executable, _DDLCompiles):
     dialect = None
     callable_ = None
 
+    def _execute_on_connection(self, connection, multiparams, params):
+        return connection._execute_ddl(self, multiparams, params)
+
     def execute(self, bind=None, target=None):
         """Execute this DDL immediately.
 
index 92cbc3653312c6a6a8a5f675479ac40f0cbc5422..ea2132e676e5f25f88143af28c756978f2dd7368 100644 (file)
@@ -292,6 +292,9 @@ class ClauseElement(Visitable):
             # self
             return self
 
+    def _execute_on_connection(self, connection, multiparams, params):
+        return connection._execute_clauseelement(self, multiparams, params)
+
     def unique_params(self, *optionaldict, **kwargs):
         """Return a copy with :func:`bindparam()` elements replaced.
 
index 24f79466ca8c04f9cdc9f326acf67d3baacaf627..489be893437dd7a962f28d411fcc0369033ee35b 100644 (file)
@@ -60,6 +60,9 @@ class FunctionElement(Executable, ColumnElement, FromClause):
                                  group_contents=True, *args).\
                                  self_group()
 
+    def _execute_on_connection(self, connection, multiparams, params):
+        return connection._execute_function(self, multiparams, params)
+
     @property
     def columns(self):
         """Fulfill the 'columns' contract of :class:`.ColumnElement`.
index b190c3874d93bc822bcbdfa568d6b378bb09ce15..35bab8e9d2e327e5f6818603d632bc43726eaf09 100644 (file)
@@ -68,6 +68,9 @@ class SchemaItem(SchemaEventTarget, visitors.Visitable):
 
     __visit_name__ = 'schema_item'
 
+    def _execute_on_connection(self, connection, multiparams, params):
+        return connection._execute_default(self, multiparams, params)
+
     def _init_items(self, *args):
         """Initialize the list of child items for this SchemaItem."""