summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/engine
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-12-10 21:38:46 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2010-12-10 21:38:46 -0500
commit66e5de30f2e01593182058091075780b41411a78 (patch)
tree3adbec3b043af37d2767e4512aa0ac7429c72300 /lib/sqlalchemy/engine
parent3c0af7f9c71adff2c52787c52e048c197db6e41c (diff)
downloadsqlalchemy-66e5de30f2e01593182058091075780b41411a78.tar.gz
- initial stab at using executemany() for inserts in the ORM when possible
Diffstat (limited to 'lib/sqlalchemy/engine')
-rw-r--r--lib/sqlalchemy/engine/base.py33
-rw-r--r--lib/sqlalchemy/engine/default.py60
2 files changed, 47 insertions, 46 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index dad075b34..4a00ebda2 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -629,24 +629,6 @@ class ExecutionContext(object):
raise NotImplementedError()
- def last_inserted_params(self):
- """Return a dictionary of the full parameter dictionary for the last
- compiled INSERT statement.
-
- Includes any ColumnDefaults or Sequences that were pre-executed.
- """
-
- raise NotImplementedError()
-
- def last_updated_params(self):
- """Return a dictionary of the full parameter dictionary for the last
- compiled UPDATE statement.
-
- Includes any ColumnDefaults that were pre-executed.
- """
-
- raise NotImplementedError()
-
def lastrow_has_defaults(self):
"""Return True if the last INSERT or UPDATE row contained
inlined or database-side defaults.
@@ -2466,13 +2448,6 @@ class ResultProxy(object):
did not explicitly specify returning().
"""
- if not self.context.isinsert:
- raise exc.InvalidRequestError(
- "Statement is not an insert() expression construct.")
- elif self.context._is_explicit_returning:
- raise exc.InvalidRequestError(
- "Can't call inserted_primary_key when returning() "
- "is used.")
return self.context._inserted_primary_key
@@ -2481,15 +2456,15 @@ class ResultProxy(object):
"""Return the primary key for the row just inserted."""
return self.inserted_primary_key
-
+
def last_updated_params(self):
"""Return ``last_updated_params()`` from the underlying
ExecutionContext.
See ExecutionContext for details.
"""
-
- return self.context.last_updated_params()
+
+ return self.context.last_updated_params
def last_inserted_params(self):
"""Return ``last_inserted_params()`` from the underlying
@@ -2498,7 +2473,7 @@ class ResultProxy(object):
See ExecutionContext for details.
"""
- return self.context.last_inserted_params()
+ return self.context.last_inserted_params
def lastrow_has_defaults(self):
"""Return ``lastrow_has_defaults()`` from the underlying
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py
index 0717a8fef..63b9e44b3 100644
--- a/lib/sqlalchemy/engine/default.py
+++ b/lib/sqlalchemy/engine/default.py
@@ -635,13 +635,7 @@ class DefaultExecutionContext(base.ExecutionContext):
ipk.append(row[c])
self._inserted_primary_key = ipk
-
- def last_inserted_params(self):
- return self._last_inserted_params
-
- def last_updated_params(self):
- return self._last_updated_params
-
+
def lastrow_has_defaults(self):
return hasattr(self, 'postfetch_cols') and len(self.postfetch_cols)
@@ -714,7 +708,32 @@ class DefaultExecutionContext(base.ExecutionContext):
return None
else:
return self._exec_default(column.onupdate)
-
+
+ @util.memoized_property
+ def _inserted_primary_key(self):
+
+ if not self.isinsert:
+ raise exc.InvalidRequestError(
+ "Statement is not an insert() expression construct.")
+ elif self._is_explicit_returning:
+ raise exc.InvalidRequestError(
+ "Can't call inserted_primary_key when returning() "
+ "is used.")
+
+
+ # lazyily evaluate inserted_primary_key for executemany.
+ # for execute(), its already in __dict__.
+ if self.executemany:
+ return [
+ [compiled_parameters.get(c.key, None)
+ for c in self.compiled.\
+ statement.table.primary_key
+ ] for compiled_parameters in self.compiled_parameters
+ ]
+ else:
+ # _inserted_primary_key should be calced here
+ assert False
+
def __process_defaults(self):
"""Generate default values for compiled insert/update statements,
and generate inserted_primary_key collection.
@@ -746,6 +765,11 @@ class DefaultExecutionContext(base.ExecutionContext):
param[c.key] = val
del self.current_parameters
+ if self.isinsert:
+ self.last_inserted_params = self.compiled_parameters
+ else:
+ self.last_updated_params = self.compiled_parameters
+
else:
self.current_parameters = compiled_parameters = \
self.compiled_parameters[0]
@@ -759,18 +783,20 @@ class DefaultExecutionContext(base.ExecutionContext):
if val is not None:
compiled_parameters[c.key] = val
del self.current_parameters
-
- if self.isinsert:
+
+ if self.isinsert and not self._is_explicit_returning:
self._inserted_primary_key = [
- compiled_parameters.get(c.key, None)
- for c in self.compiled.\
+ self.compiled_parameters[0].get(c.key, None)
+ for c in self.compiled.\
statement.table.primary_key
- ]
- self._last_inserted_params = compiled_parameters
+ ]
+
+ if self.isinsert:
+ self.last_inserted_params = compiled_parameters
else:
- self._last_updated_params = compiled_parameters
+ self.last_updated_params = compiled_parameters
- self.postfetch_cols = self.compiled.postfetch
- self.prefetch_cols = self.compiled.prefetch
+ self.postfetch_cols = self.compiled.postfetch
+ self.prefetch_cols = self.compiled.prefetch
DefaultDialect.execution_ctx_cls = DefaultExecutionContext