summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-01-29 02:01:11 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2010-01-29 02:01:11 +0000
commita04c4dc42cf4c3c35163b59a78f05efe47547dc0 (patch)
tree4d21f99e6d5ada6c701ea77e824163482f818896 /lib/sqlalchemy
parente78cee66186b8851a5018e32f6935ca72be0cf7e (diff)
downloadsqlalchemy-a04c4dc42cf4c3c35163b59a78f05efe47547dc0.tar.gz
- inline some code and turn some instance-level defaults into class level
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/psycopg2.py30
-rw-r--r--lib/sqlalchemy/engine/base.py14
-rw-r--r--lib/sqlalchemy/engine/default.py75
-rw-r--r--lib/sqlalchemy/sql/expression.py10
-rw-r--r--lib/sqlalchemy/types.py4
5 files changed, 76 insertions, 57 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
index 54283581d..bb6562dea 100644
--- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py
+++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py
@@ -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:
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index 3d192a9be..6e4a34219 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -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
@@ -799,14 +799,6 @@ class Connection(Connectable):
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
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py
index e2a03d227..44c968942 100644
--- a/lib/sqlalchemy/engine/default.py
+++ b/lib/sqlalchemy/engine/default.py
@@ -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
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 5edb6e47f..f0f55ed72 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -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:
diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py
index b4e9ba0cd..2f7575d3b 100644
--- a/lib/sqlalchemy/types.py
+++ b/lib/sqlalchemy/types.py
@@ -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):