Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7f3cdd

Browse files
committed
Issue #21718: Merge from 3.5
2 parents 1dc3c89 + 6afe858 commit d7f3cdd

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

Lib/sqlite3/test/types.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,45 @@ def CheckCursorDescriptionNoRow(self):
274274
self.cur.execute("select * from test where 0 = 1")
275275
self.assertEqual(self.cur.description[0][0], "x")
276276

277+
def CheckCursorDescriptionInsert(self):
278+
self.cur.execute("insert into test values (1)")
279+
self.assertIsNone(self.cur.description)
280+
281+
282+
@unittest.skipIf(sqlite.sqlite_version_info < (3, 8, 3), "CTEs not supported")
283+
class CommonTableExpressionTests(unittest.TestCase):
284+
285+
def setUp(self):
286+
self.con = sqlite.connect(":memory:")
287+
self.cur = self.con.cursor()
288+
self.cur.execute("create table test(x foo)")
289+
290+
def tearDown(self):
291+
self.cur.close()
292+
self.con.close()
293+
294+
def CheckCursorDescriptionCTESimple(self):
295+
self.cur.execute("with one as (select 1) select * from one")
296+
self.assertIsNotNone(self.cur.description)
297+
self.assertEqual(self.cur.description[0][0], "1")
298+
299+
def CheckCursorDescriptionCTESMultipleColumns(self):
300+
self.cur.execute("insert into test values(1)")
301+
self.cur.execute("insert into test values(2)")
302+
self.cur.execute("with testCTE as (select * from test) select * from testCTE")
303+
self.assertIsNotNone(self.cur.description)
304+
self.assertEqual(self.cur.description[0][0], "x")
305+
306+
def CheckCursorDescriptionCTE(self):
307+
self.cur.execute("insert into test values (1)")
308+
self.cur.execute("with bar as (select * from test) select * from test where x = 1")
309+
self.assertIsNotNone(self.cur.description)
310+
self.assertEqual(self.cur.description[0][0], "x")
311+
self.cur.execute("with bar as (select * from test) select * from test where x = 2")
312+
self.assertIsNotNone(self.cur.description)
313+
self.assertEqual(self.cur.description[0][0], "x")
314+
315+
277316
class ObjectAdaptationTests(unittest.TestCase):
278317
def cast(obj):
279318
return float(obj)
@@ -372,7 +411,8 @@ def suite():
372411
adaptation_suite = unittest.makeSuite(ObjectAdaptationTests, "Check")
373412
bin_suite = unittest.makeSuite(BinaryConverterTests, "Check")
374413
date_suite = unittest.makeSuite(DateTimeTests, "Check")
375-
return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, bin_suite, date_suite))
414+
cte_suite = unittest.makeSuite(CommonTableExpressionTests, "Check")
415+
return unittest.TestSuite((sqlite_type_suite, decltypes_type_suite, colnames_type_suite, adaptation_suite, bin_suite, date_suite, cte_suite))
376416

377417
def test():
378418
runner = unittest.TextTestRunner()

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- Issue #21718: cursor.description is now available for queries using CTEs.
40+
3941
- Issue #27819: In distutils sdists, simply produce the "gztar" (gzipped tar
4042
format) distributions on all platforms unless "formats" is supplied.
4143

Modules/_sqlite/cursor.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,11 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
645645
goto error;
646646
}
647647

648-
if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) {
649-
if (self->description == Py_None) {
650-
Py_BEGIN_ALLOW_THREADS
651-
numcols = sqlite3_column_count(self->statement->st);
652-
Py_END_ALLOW_THREADS
653-
648+
if (rc == SQLITE_ROW || rc == SQLITE_DONE) {
649+
Py_BEGIN_ALLOW_THREADS
650+
numcols = sqlite3_column_count(self->statement->st);
651+
Py_END_ALLOW_THREADS
652+
if (self->description == Py_None && numcols > 0) {
654653
Py_SETREF(self->description, PyTuple_New(numcols));
655654
if (!self->description) {
656655
goto error;

0 commit comments

Comments
 (0)