Mike Bayer avatar Mike Bayer committed e04e100

- [bug] The ResultProxy methods inserted_primary_key,
last_updated_params(), last_inserted_params(),
postfetch_cols(), prefetch_cols() all
assert that the given statement is a compiled
construct, and is an insert() or update()
statement as is appropriate, else
raise InvalidRequestError. [ticket:2498]

- ResultProxy.last_inserted_ids is removed,
replaced by inserted_primary_key.

Comments (0)

Files changed (5)

     the task of specifying in-Python ForeignKey 
     or ForeignKeyConstraint declarations.
 
+  - [bug] The ResultProxy methods inserted_primary_key,
+    last_updated_params(), last_inserted_params(),
+    postfetch_cols(), prefetch_cols() all
+    assert that the given statement is a compiled
+    construct, and is an insert() or update()
+    statement as is appropriate, else
+    raise InvalidRequestError.  [ticket:2498]
+
+  - ResultProxy.last_inserted_ids is removed,
+    replaced by inserted_primary_key.
+
 - sql
   - [feature] The Inspector object can now be 
     acquired using the new inspect() service,

lib/sqlalchemy/engine/base.py

     should_autocommit
       True if the statement is a "committable" statement.
 
+    prefetch_cols
+      a list of Column objects for which a client-side default 
+      was fired off.  Applies to inserts and updates.
+
     postfetch_cols
       a list of Column objects for which a server-side default or
       inline SQL expression value was fired off.  Applies to inserts
         where it is appropriate.  It's behavior is not 
         consistent across backends.
 
-        Usage of this method is normally unnecessary; the
+        Usage of this method is normally unnecessary when 
+        using insert() expression constructs; the
         :attr:`~ResultProxy.inserted_primary_key` attribute provides a
         tuple of primary key values for a newly inserted row,
         regardless of database backend.
         supports "returning" and the insert statement executed
         with the "implicit returning" enabled.
 
+        Raises :class:`.InvalidRequestError` if the executed
+        statement is not a compiled expression construct
+        or is not an insert() construct.
+
         """
 
-        if not self.context.isinsert:
+        if not self.context.compiled:
             raise exc.InvalidRequestError(
-                        "Statement is not an insert() expression construct.")
+                        "Statement is not a compiled "
+                        "expression construct.")
+        elif 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() "
+                        "Can't call inserted_primary_key "
+                        "when returning() "
                         "is used.")
 
         return self.context.inserted_primary_key
 
-    @util.deprecated("0.6", "Use :attr:`.ResultProxy.inserted_primary_key`")
-    def last_inserted_ids(self):
-        """Return the primary key for the row just inserted."""
-
-        return self.inserted_primary_key
-
     def last_updated_params(self):
         """Return the collection of updated parameters from this
         execution.
 
+        Raises :class:`.InvalidRequestError` if the executed
+        statement is not a compiled expression construct
+        or is not an update() construct.
+
         """
-        if self.context.executemany:
+        if not self.context.compiled:
+            raise exc.InvalidRequestError(
+                        "Statement is not a compiled "
+                        "expression construct.")
+        elif not self.context.isupdate:
+            raise exc.InvalidRequestError(
+                        "Statement is not an update() "
+                        "expression construct.")
+        elif self.context.executemany:
             return self.context.compiled_parameters
         else:
             return self.context.compiled_parameters[0]
         """Return the collection of inserted parameters from this
         execution.
 
+        Raises :class:`.InvalidRequestError` if the executed
+        statement is not a compiled expression construct
+        or is not an insert() construct.
+
         """
-        if self.context.executemany:
+        if not self.context.compiled:
+            raise exc.InvalidRequestError(
+                        "Statement is not a compiled "
+                        "expression construct.")
+        elif not self.context.isinsert:
+            raise exc.InvalidRequestError(
+                        "Statement is not an insert() "
+                        "expression construct.")
+        elif self.context.executemany:
             return self.context.compiled_parameters
         else:
             return self.context.compiled_parameters[0]
 
     def lastrow_has_defaults(self):
         """Return ``lastrow_has_defaults()`` from the underlying
-        ExecutionContext.
-
-        See ExecutionContext for details.
+        :class:`.ExecutionContext`.
+
+        See :class:`.ExecutionContext` for details.
+        
         """
 
         return self.context.lastrow_has_defaults()
 
     def postfetch_cols(self):
-        """Return ``postfetch_cols()`` from the underlying ExecutionContext.
-
-        See ExecutionContext for details.
+        """Return ``postfetch_cols()`` from the underlying :class:`.ExecutionContext`.
+
+        See :class:`.ExecutionContext` for details.
+
+        Raises :class:`.InvalidRequestError` if the executed
+        statement is not a compiled expression construct
+        or is not an insert() or update() construct.
+        
         """
 
+        if not self.context.compiled:
+            raise exc.InvalidRequestError(
+                        "Statement is not a compiled "
+                        "expression construct.")
+        elif not self.context.isinsert and not self.context.isupdate:
+            raise exc.InvalidRequestError(
+                        "Statement is not an insert() or update() "
+                        "expression construct.")
         return self.context.postfetch_cols
 
     def prefetch_cols(self):
+        """Return ``prefetch_cols()`` from the underlying :class:`.ExecutionContext`.
+
+        See :class:`.ExecutionContext` for details.
+
+        Raises :class:`.InvalidRequestError` if the executed
+        statement is not a compiled expression construct
+        or is not an insert() or update() construct.
+        
+        """
+
+        if not self.context.compiled:
+            raise exc.InvalidRequestError(
+                        "Statement is not a compiled "
+                        "expression construct.")
+        elif not self.context.isinsert and not self.context.isupdate:
+            raise exc.InvalidRequestError(
+                        "Statement is not an insert() or update() "
+                        "expression construct.")
         return self.context.prefetch_cols
 
     def supports_sane_rowcount(self):

lib/sqlalchemy/engine/default.py

     result_map = None
     compiled = None
     statement = None
+    postfetch_cols = None
+    prefetch_cols = None
     _is_implicit_returning = False
     _is_explicit_returning = False
 

test/engine/test_execute.py

         writer.writerow(row)
         assert s.getvalue().strip() == '1,Test'
 
+    @testing.requires.selectone
+    def test_empty_accessors(self):
+        statements = [
+            (
+                "select 1",
+                [
+                    lambda r: r.last_inserted_params(),
+                    lambda r: r.last_updated_params(),
+                    lambda r: r.prefetch_cols(),
+                    lambda r: r.postfetch_cols(),
+                    lambda r : r.inserted_primary_key
+                ],
+                "Statement is not a compiled expression construct."
+            ),
+            (
+                select([1]),
+                [
+                    lambda r: r.last_inserted_params(),
+                    lambda r : r.inserted_primary_key
+                ],
+                r"Statement is not an insert\(\) expression construct."
+            ),
+            (
+                select([1]),
+                [
+                    lambda r: r.last_updated_params(),
+                ],
+                r"Statement is not an update\(\) expression construct."
+            ),
+            (
+                select([1]),
+                [
+                    lambda r: r.prefetch_cols(),
+                    lambda r : r.postfetch_cols()
+                ],
+                r"Statement is not an insert\(\) "
+                r"or update\(\) expression construct."
+            ),
+        ]
+
+        for stmt, meths, msg in statements:
+            r = testing.db.execute(stmt)
+            try:
+                for meth in meths:
+                    assert_raises_message(
+                        tsa.exc.InvalidRequestError,
+                        msg,
+                        meth, r
+                    )
+
+            finally:
+                r.close()
+
+
 class AlternateResultProxyTest(fixtures.TestBase):
     __requires__ = ('sqlite', )
 

test/lib/requires.py

         skip_if(lambda: testing.against('postgresql') \
                 and not testing.db.scalar('SHOW LC_COLLATE').startswith('en'))
     )
+
+def selectone(fn):
+    """target driver must support the literal statement 'select 1'"""
+    return _chain_decorators_on(
+        fn,
+        skip_if(lambda: testing.against('oracle'), 
+            "non-standard SELECT scalar syntax")
+    )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.