]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added accessors to ResultProxy "returns_rows", "is_insert"
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 16 Mar 2011 14:36:48 +0000 (10:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 16 Mar 2011 14:36:48 +0000 (10:36 -0400)
[ticket:2089]

CHANGES
lib/sqlalchemy/engine/base.py
test/sql/test_query.py

diff --git a/CHANGES b/CHANGES
index 5ca828660b199a711efef8719946565a3465dce9..f8383fdaf9799e3c66681dbfcad77d9344959752 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -465,6 +465,9 @@ CHANGES
     of an insert()/update() statement will also use the new
     compilation rules. [ticket:2042]
 
+  - Added accessors to ResultProxy "returns_rows", "is_insert"
+    [ticket:2089]
+
 - postgresql
   - When explicit sequence execution derives the name 
     of the auto-generated sequence of a SERIAL column, 
index 972b06ce8a0bb328d8c8681160a4c3f5d61d6965..e88ad8265896d8968ae194da9274cfd0534e0c88 100644 (file)
@@ -2468,6 +2468,32 @@ class ResultProxy(object):
         """
         return self._saved_cursor.lastrowid
 
+    @property
+    def returns_rows(self):
+        """True if this :class:`.ResultProxy` returns rows.
+        
+        I.e. if it is legal to call the methods 
+        :meth:`~.ResultProxy.fetchone`, 
+        :meth:`~.ResultProxy.fetchmany`
+        :meth:`~.ResultProxy.fetchall`.
+
+        """
+        return self._metadata is not None
+
+    @property
+    def is_insert(self):
+        """True if this :class:`.ResultProxy` is the result
+        of a executing an expression language compiled 
+        :func:`.expression.insert` construct.
+        
+        When True, this implies that the 
+        :attr:`inserted_primary_key` attribute is accessible,
+        assuming the statement did not include
+        a user defined "returning" construct.
+        
+        """
+        return self.context.isinsert
+
     def _cursor_description(self):
         """May be overridden by subclasses."""
 
index 8f7c4c6a7bd665cff65e7e3919f7b5a3fadd7351..da5b05d0a852edc0190ea90115531a76af028e42 100644 (file)
@@ -706,6 +706,36 @@ class QueryTest(TestBase):
 
         eq_(r.lastrowid, 1)
 
+    def test_returns_rows_flag_insert(self):
+        r = testing.db.execute(
+            users.insert(),
+            {'user_id':1, 'user_name':'ed'}
+        )
+        assert r.is_insert
+        assert not r.returns_rows
+
+    def test_returns_rows_flag_update(self):
+        r = testing.db.execute(
+            users.update().values(user_name='fred')
+        )
+        assert not r.is_insert
+        assert not r.returns_rows
+
+    def test_returns_rows_flag_select(self):
+        r = testing.db.execute(
+            users.select()
+        )
+        assert not r.is_insert
+        assert r.returns_rows
+
+    @testing.requires.returning
+    def test_returns_rows_flag_insert_returning(self):
+        r = testing.db.execute(
+            users.insert().returning(users.c.user_id),
+            {'user_id':1, 'user_name':'ed'}
+        )
+        assert r.is_insert
+        assert r.returns_rows
 
     def test_graceful_fetch_on_non_rows(self):
         """test that calling fetchone() etc. on a result that doesn't