Skip to content

Commit 7f70f1a

Browse files
author
James William Pye
committed
Fix DB-API rowcount setting after execute.
Per report by Mike Bayer. http://pgfoundry.org/tracker/index.php?func=detail&aid=1010643&group_id=1000094&atid=442
1 parent 25adc3e commit 7f70f1a

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

postgresql/documentation/changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Changes
2121
* Make DB-API connect() inherit defaults like postgresql.open()
2222
* Make DB-API extend PG-API so that DB-API connections will now have direct
2323
access to PG-API features.
24+
* Fix DB-API setting of rowcount after execute. [Reported by Mike Bayer; 1010643]
2425

2526

2627
0.8.2
@@ -29,6 +30,7 @@ Changes
2930
* Correct StoredProcedure's statement production. It was falsely using a
3031
a mechanism that would use SQL predefined type names instead of
3132
qualified names. [Reported by Dallas Morisett]
33+
* Fix DB-API setting of rowcount after execute. [Reported by Mike Bayer; 1010643]
3234

3335

3436
0.8.1 released on 2009-04-30

postgresql/driver/dbapi20.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,17 @@ def execute(self, statement, parameters = ()):
255255
c = ps.chunks(*pxf(parameters))
256256
if ps._output is not None and len(ps._output) > 0:
257257
# name, relationId, columnNumber, typeId, typlen, typmod, format
258+
self.rowcount = -1
258259
self.description = tuple([
259260
(self.database.typio.decode(x[0]), dbapi_type(x[3]),
260261
None, None, None, None, None)
261262
for x in ps._output
262263
])
263264
self.__portals.insert(0, Portal(c))
264265
else:
266+
self.rowcount = c.count()
267+
if self.rowcount is None:
268+
self.rowcount = -1
265269
self.description = None
266270
# execute bumps any current portal
267271
if self.__portals:
@@ -272,9 +276,10 @@ def executemany(self, statement, parameters):
272276
sql, pxf, nparams = self._convert_query(statement)
273277
ps = self.database.prepare(sql)
274278
if ps._input is not None:
275-
ps.load(map(pxf, parameters))
279+
ps.load_rows(map(pxf, parameters))
276280
else:
277-
ps.load(parameters)
281+
ps.load_rows(parameters)
282+
self.rowcount = -1
278283
return self
279284

280285
def close(self):

postgresql/test/test_dbapi20.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,24 +271,33 @@ def test_rowcount(self):
271271
try:
272272
cur = con.cursor()
273273
self.executeDDL1(cur)
274-
self.assertEqual(cur.rowcount,-1,
274+
self.assertEqual(cur.rowcount, -1,
275275
'cursor.rowcount should be -1 after executing no-result '
276276
'statements'
277277
)
278278
cur.execute("insert into %sbooze values ('Victoria Bitter')" % (
279279
self.table_prefix
280280
))
281-
self.failUnless(cur.rowcount in (-1,1),
281+
self.failUnlessEqual(cur.rowcount, 1,
282282
'cursor.rowcount should == number or rows inserted, or '
283283
'set to -1 after executing an insert statement'
284284
)
285+
cur.execute("insert into %sbooze select 'Victoria Bitter' WHERE FALSE" % (
286+
self.table_prefix
287+
))
288+
self.failUnlessEqual(cur.rowcount, 0)
289+
cur.execute("insert into %sbooze select 'First' UNION ALL select 'second'" % (
290+
self.table_prefix
291+
))
292+
self.failUnlessEqual(cur.rowcount, 2)
293+
285294
cur.execute("select name from %sbooze" % self.table_prefix)
286-
self.failUnless(cur.rowcount in (-1,1),
295+
self.failUnlessEqual(cur.rowcount, -1,
287296
'cursor.rowcount should == number of rows returned, or '
288297
'set to -1 after executing a select statement'
289298
)
290299
self.executeDDL2(cur)
291-
self.assertEqual(cur.rowcount,-1,
300+
self.assertEqual(cur.rowcount, -1,
292301
'cursor.rowcount not being reset to -1 after executing '
293302
'no-result statements'
294303
)

0 commit comments

Comments
 (0)