]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- inline some code and turn some instance-level defaults into class level
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 29 Jan 2010 02:01:11 +0000 (02:01 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 29 Jan 2010 02:01:11 +0000 (02:01 +0000)
lib/sqlalchemy/dialects/postgresql/psycopg2.py
lib/sqlalchemy/engine/base.py
lib/sqlalchemy/engine/default.py
lib/sqlalchemy/sql/expression.py
lib/sqlalchemy/types.py

index 54283581d6bfd0ed4da86562983ff1d4c467566a..bb6562deafdf485350929754c2c3d8f5e7556087 100644 (file)
@@ -112,22 +112,20 @@ SERVER_SIDE_CURSOR_RE = re.compile(
 class PostgreSQL_psycopg2ExecutionContext(PGExecutionContext):
     def create_cursor(self):
         # TODO: coverage for server side cursors + select.for_update()
-        stream_results_option = self.execution_options.get('stream_results')
-        is_server_side = (
-            # Enabled for this statement ...
-            (stream_results_option or
-             # ... or enabled for all statements
-             (self.dialect.server_side_cursors and
-              # ... and not explicitly disabled for this one.
-              (stream_results_option or stream_results_option is None))
-             ) and (
-                # But don't use SS-cursors when autocommit is on ...
-                (not self.should_autocommit and
-                 self.compiled and isinstance(self.compiled.statement, expression.Selectable))
-                or (
-                    # ... or if it's not even a SELECT.
-                    (not self.compiled or isinstance(self.compiled.statement, expression._TextClause))
-                    and self.statement and SERVER_SIDE_CURSOR_RE.match(self.statement))))
+        
+        if self.dialect.server_side_cursors:
+            is_server_side = \
+                self.execution_options.get('stream_results', True) and (
+                    (self.compiled and isinstance(self.compiled.statement, expression.Selectable) \
+                    or \
+                    (
+                        (not self.compiled or 
+                        isinstance(self.compiled.statement, expression._TextClause)) 
+                        and self.statement and SERVER_SIDE_CURSOR_RE.match(self.statement))
+                    )
+                )
+        else:
+            is_server_side = self.execution_options.get('stream_results', False)
 
         self.__is_server_side = is_server_side
         if is_server_side:
index 3d192a9be6fba3bd5b9c2aad9b0465bf8f5c6dc3..6e4a34219573173510db4bb78a13cbf98690764b 100644 (file)
@@ -732,7 +732,7 @@ class Connection(Connectable):
         self.engine = engine
         self.__connection = connection or engine.raw_connection()
         self.__transaction = None
-        self.__close_with_result = close_with_result
+        self.should_close_with_result = close_with_result
         self.__savepoint_seq = 0
         self.__branch = _branch
         self.__invalid = False
@@ -798,14 +798,6 @@ class Connection(Connectable):
                 return self.__connection
             raise exc.InvalidRequestError("This Connection is closed")
 
-    @property
-    def should_close_with_result(self):
-        """Indicates if this Connection should be closed when a corresponding
-        ResultProxy is closed; this is essentially an auto-release mode.
-        """
-
-        return self.__close_with_result
-
     @property
     def info(self):
         """A collection of per-DB-API connection instance properties."""
@@ -1080,7 +1072,7 @@ class Connection(Connectable):
     def _execute_default(self, default, multiparams, params):
         ctx = self.__create_execution_context()
         ret = ctx._exec_default(default)
-        if self.__close_with_result:
+        if self.should_close_with_result:
             self.close()
         return ret
 
@@ -1161,7 +1153,7 @@ class Connection(Connectable):
                 if cursor:
                     cursor.close()
                 self._autorollback()
-                if self.__close_with_result:
+                if self.should_close_with_result:
                     self.close()
             # Py3K
             #raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect) from e
index e2a03d22769461627715b900494ca8741260ca7b..44c9689423c8c1a21c4204b583a46cf4ad696587 100644 (file)
@@ -224,32 +224,62 @@ class DefaultDialect(base.Dialect):
 
 
 class DefaultExecutionContext(base.ExecutionContext):
+    execution_options = util.frozendict()
+    isinsert = False
+    isupdate = False
+    isdelete = False
+    executemany = False
+    result_map = None
+    compiled = None
+    statement = None
     
-    def __init__(self, dialect, connection, compiled_sql=None, compiled_ddl=None, statement=None, parameters=None):
+    def __init__(self, 
+                    dialect, 
+                    connection, 
+                    compiled_sql=None, 
+                    compiled_ddl=None, 
+                    statement=None, 
+                    parameters=None):
+        
         self.dialect = dialect
         self._connection = self.root_connection = connection
         self.engine = connection.engine
         
         if compiled_ddl is not None:
             self.compiled = compiled = compiled_ddl
+
+            if compiled.statement._execution_options:
+                self.execution_options = compiled.statement._execution_options
+            if connection._execution_options:
+                self.execution_options = self.execution_options.union(
+                                                    connection._execution_options
+                                                    )
+
             if not dialect.supports_unicode_statements:
                 self.statement = unicode(compiled).encode(self.dialect.encoding)
             else:
                 self.statement = unicode(compiled)
-            self.isinsert = self.isupdate = self.isdelete = self.executemany = False
-            self.result_map = None
+                
             self.cursor = self.create_cursor()
             self.compiled_parameters = []
             self.parameters = [self._default_params]
+            
         elif compiled_sql is not None:
             self.compiled = compiled = compiled_sql
 
+            if not compiled.can_execute:
+                raise exc.ArgumentError("Not an executable clause: %s" % compiled)
+
+            if compiled.statement._execution_options:
+                self.execution_options = compiled.statement._execution_options
+            if connection._execution_options:
+                self.execution_options = self.execution_options.union(
+                                                        connection._execution_options
+                                                        )
+
             # compiled clauseelement.  process bind params, process table defaults,
             # track collections used by ResultProxy to target and process results
 
-            if not compiled.can_execute:
-                raise exc.ArgumentError("Not an executable clause: %s" % compiled)
-            
             self.processors = dict(
                 (key, value) for key, value in
                 ( (compiled.bind_names[bindparam],
@@ -270,9 +300,10 @@ class DefaultExecutionContext(base.ExecutionContext):
 
             if not parameters:
                 self.compiled_parameters = [compiled.construct_params()]
-                self.executemany = False
             else:
-                self.compiled_parameters = [compiled.construct_params(m, _group_number=grp) for grp,m in enumerate(parameters)]
+                self.compiled_parameters = [compiled.construct_params(m, _group_number=grp) for
+                                            grp,m in enumerate(parameters)]
+                                            
                 self.executemany = len(parameters) > 1
 
             self.cursor = self.create_cursor()
@@ -281,38 +312,32 @@ class DefaultExecutionContext(base.ExecutionContext):
             self.parameters = self.__convert_compiled_params(self.compiled_parameters)
         elif statement is not None:
             # plain text statement
-            self.result_map = self.compiled = None
+            if connection._execution_options:
+                self.execution_options = self.execution_options.union(connection._execution_options)
             self.parameters = self.__encode_param_keys(parameters)
             self.executemany = len(parameters) > 1
             if isinstance(statement, unicode) and not dialect.supports_unicode_statements:
                 self.statement = statement.encode(self.dialect.encoding)
             else:
                 self.statement = statement
-            self.isinsert = self.isupdate = self.isdelete = False
             self.cursor = self.create_cursor()
         else:
             # no statement. used for standalone ColumnDefault execution.
-            self.statement = self.compiled = None
-            self.isinsert = self.isupdate = self.isdelete = self.executemany = False
+            if connection._execution_options:
+                self.execution_options = self.execution_options.union(connection._execution_options)
             self.cursor = self.create_cursor()
-
-    @util.memoized_property
-    def execution_options(self):
         
-        if self.compiled:
-            return self.compiled.statement._execution_options.union(
-                            self._connection._execution_options)
-        else:
-            return self._connection._execution_options
             
     @util.memoized_property
     def should_autocommit(self):
-        autocommit = self.execution_options.get('autocommit', expression.PARSE_AUTOCOMMIT)
+        autocommit = self.execution_options.get('autocommit', 
+                                                not self.compiled and 
+                                                self.statement and
+                                                expression.PARSE_AUTOCOMMIT 
+                                                or False)
+                                                
         if autocommit is expression.PARSE_AUTOCOMMIT:
-            if self.statement:
-                return self.should_autocommit_text(self.statement)
-            else:
-                return False
+            return self.should_autocommit_text(self.statement)
         else:
             return autocommit
             
index 5edb6e47fedfeef7844b79f3fd8a9913962d92be..f0f55ed72583ed143dbae719ea7e5b98b7543ebe 100644 (file)
@@ -3925,7 +3925,8 @@ class _UpdateBase(_Executable, ClauseElement):
             if m:
                 self._returning = kwargs.pop(k)
                 util.warn_deprecated(
-                    "The %r argument is deprecated.  Please use statement.returning(col1, col2, ...)" % k
+                    "The %r argument is deprecated.  Please "
+                    "use statement.returning(col1, col2, ...)" % k
                 )
         return kwargs
     
@@ -4007,6 +4008,8 @@ class Insert(_ValuesBase):
     
     _prefixes = ()
     
+    kwargs = util.frozendict()
+    
     def __init__(self, 
                 table, 
                 values=None, 
@@ -4022,8 +4025,9 @@ class Insert(_ValuesBase):
         self._returning = returning
         if prefixes:
             self._prefixes = tuple([_literal_as_text(p) for p in prefixes])
-            
-        self.kwargs = self._process_deprecated_kw(kwargs)
+        
+        if kwargs:
+            self.kwargs = self._process_deprecated_kw(kwargs)
 
     def get_children(self, **kwargs):
         if self.select is not None:
index b4e9ba0cd0cc070d25ffdad3fd224ae4de068c15..2f7575d3b07b51ee92b4b7c148d79c9c2c84f856 100644 (file)
@@ -388,9 +388,9 @@ def to_instance(typeobj):
     if typeobj is None:
         return NULLTYPE
 
-    try:
+    if util.callable(typeobj):
         return typeobj()
-    except TypeError:
+    else:
         return typeobj
 
 def adapt_type(typeobj, colspecs):