Skip to content

Commit 762c326

Browse files
fetchall and fetchmany will returns empty list with no rows (#1028)
Signed-off-by: Balram Choudhary <bchoudhary@rocketsoftware.com>
1 parent c6549b3 commit 762c326

3 files changed

Lines changed: 175 additions & 6 deletions

File tree

ibm_db.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17328,9 +17328,9 @@ static PyObject *ibm_db_fetchmany(PyObject *self, PyObject *args)
1732817328
}
1732917329
if (PyList_Size(result_list) == 0)
1733017330
{
17331-
LogMsg(DEBUG, "No rows fetched, returning None");
17332-
Py_XDECREF(result_list);
17333-
Py_RETURN_NONE;
17331+
LogMsg(DEBUG, "No rows fetched, returning empty list");
17332+
LogMsg(INFO, "exit fetchmany()");
17333+
return result_list;
1733417334
}
1733517335
snprintf(messageStr, sizeof(messageStr), "Returning %zd rows", PyList_Size(result_list));
1733617336
LogMsg(DEBUG, messageStr);
@@ -17376,9 +17376,9 @@ static PyObject *ibm_db_fetchall(PyObject *self, PyObject *args)
1737617376
}
1737717377
if (PyList_Size(result_list) == 0)
1737817378
{
17379-
LogMsg(DEBUG, "No rows fetched, returning None");
17380-
Py_XDECREF(result_list);
17381-
Py_RETURN_NONE;
17379+
LogMsg(DEBUG, "No rows fetched, returning empty list");
17380+
LogMsg(INFO, "exit fetchall()");
17381+
return result_list;
1738217382
}
1738317383
snprintf(messageStr, sizeof(messageStr), "Returning %zd rows", PyList_Size(result_list));
1738417384
LogMsg(DEBUG, messageStr);
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#
2+
# Licensed Materials - Property of IBM
3+
#
4+
# (c) Copyright IBM Corp. 2025
5+
#
6+
7+
from __future__ import print_function
8+
import sys
9+
import unittest
10+
import ibm_db
11+
import ibm_db_dbi
12+
import config
13+
from testfunctions import IbmDbTestFunctions
14+
15+
16+
class IbmDbTestCase(unittest.TestCase):
17+
18+
def test_319_FetchMethods_DBI(self):
19+
obj = IbmDbTestFunctions()
20+
obj.assert_expect(self.run_test_319)
21+
22+
def run_test_319(self):
23+
conn = ibm_db.connect(config.database, config.user, config.password)
24+
25+
ibm_db.autocommit(conn, ibm_db.SQL_AUTOCOMMIT_OFF)
26+
27+
dbi_conn = ibm_db_dbi.Connection(conn)
28+
29+
# Drop the test table, in case it exists
30+
drop = 'DROP TABLE dbiEmployees'
31+
try:
32+
cur = dbi_conn.cursor()
33+
cur.execute(drop)
34+
except:
35+
pass
36+
37+
# Create the test table
38+
create = 'CREATE TABLE dbiEmployees (id INT PRIMARY KEY NOT NULL, name VARCHAR(255), age INT)'
39+
cur = dbi_conn.cursor()
40+
cur.execute(create)
41+
42+
# Insert rows into the table
43+
insert_sql = 'INSERT INTO dbiEmployees (id, name, age) VALUES (?, ?, ?)'
44+
employees = [
45+
(1, 'Alice', 30),
46+
(2, 'Bob', 24),
47+
(3, 'Charlie', 29),
48+
(4, 'Diana', 35),
49+
(5, 'Eve', 40)
50+
]
51+
for emp in employees:
52+
cur.execute(insert_sql, emp)
53+
54+
dbi_conn.commit()
55+
56+
# 1) fetchone example
57+
cur1 = dbi_conn.cursor()
58+
cur1.execute('SELECT * FROM dbiEmployees ORDER BY id')
59+
row = cur1.fetchone()
60+
print("fetchone():", row)
61+
62+
# 2) fetchmany example
63+
cur2 = dbi_conn.cursor()
64+
cur2.execute('SELECT * FROM dbiEmployees ORDER BY id')
65+
rows = cur2.fetchmany(2)
66+
print("fetchmany(2):", rows)
67+
68+
# 3) fetchall example
69+
cur3 = dbi_conn.cursor()
70+
cur3.execute('SELECT * FROM dbiEmployees ORDER BY id')
71+
all_rows = cur3.fetchall()
72+
print("fetchall():", all_rows)
73+
74+
#__END__
75+
#__LUW_EXPECTED__
76+
#fetchone(): (1, 'Alice', 30)
77+
#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)]
78+
#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)]
79+
#__ZOS_EXPECTED__
80+
#fetchone(): (1, 'Alice', 30)
81+
#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)]
82+
#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)]
83+
#__SYSTEMI_EXPECTED__
84+
#fetchone(): (1, 'Alice', 30)
85+
#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)]
86+
#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)]
87+
#__IDS_EXPECTED__
88+
#fetchone(): (1, 'Alice', 30)
89+
#fetchmany(2): [(1, 'Alice', 30), (2, 'Bob', 24)]
90+
#fetchall(): [(1, 'Alice', 30), (2, 'Bob', 24), (3, 'Charlie', 29), (4, 'Diana', 35), (5, 'Eve', 40)]
91+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#
2+
# Licensed Materials - Property of IBM
3+
#
4+
# (c) Copyright IBM Corp. 2025
5+
#
6+
7+
from __future__ import print_function
8+
import sys
9+
import unittest
10+
import ibm_db
11+
import ibm_db_dbi
12+
import config
13+
from testfunctions import IbmDbTestFunctions
14+
15+
16+
class IbmDbTestCase(unittest.TestCase):
17+
18+
def test_320_EmptyFetchMethods_DBI(self):
19+
obj = IbmDbTestFunctions()
20+
obj.assert_expect(self.run_test_320)
21+
22+
def run_test_320(self):
23+
conn = ibm_db.connect(config.database, config.user, config.password)
24+
25+
ibm_db.autocommit(conn, ibm_db.SQL_AUTOCOMMIT_OFF)
26+
27+
dbi_conn = ibm_db_dbi.Connection(conn)
28+
29+
# Drop the test table, if exists
30+
drop = 'DROP TABLE dbiEmployeesEmpty'
31+
try:
32+
cur = dbi_conn.cursor()
33+
cur.execute(drop)
34+
except:
35+
pass
36+
37+
# Create the test table (empty)
38+
create = 'CREATE TABLE dbiEmployeesEmpty (id INT PRIMARY KEY NOT NULL, name VARCHAR(255), age INT)'
39+
cur = dbi_conn.cursor()
40+
cur.execute(create)
41+
42+
dbi_conn.commit()
43+
44+
# 1) fetchone example on empty table
45+
cur1 = dbi_conn.cursor()
46+
cur1.execute('SELECT * FROM dbiEmployeesEmpty ORDER BY id')
47+
row = cur1.fetchone()
48+
print("fetchone():", row)
49+
50+
# 2) fetchmany example on empty table
51+
cur2 = dbi_conn.cursor()
52+
cur2.execute('SELECT * FROM dbiEmployeesEmpty ORDER BY id')
53+
rows = cur2.fetchmany(2)
54+
print("fetchmany(2):", rows)
55+
56+
# 3) fetchall example on empty table
57+
cur3 = dbi_conn.cursor()
58+
cur3.execute('SELECT * FROM dbiEmployeesEmpty ORDER BY id')
59+
all_rows = cur3.fetchall()
60+
print("fetchall():", all_rows)
61+
62+
#__END__
63+
#__LUW_EXPECTED__
64+
#fetchone(): None
65+
#fetchmany(2): []
66+
#fetchall(): []
67+
#__ZOS_EXPECTED__
68+
#fetchone(): None
69+
#fetchmany(2): []
70+
#fetchall(): []
71+
#__SYSTEMI_EXPECTED__
72+
#fetchone(): None
73+
#fetchmany(2): []
74+
#fetchall(): []
75+
#__IDS_EXPECTED__
76+
#fetchone(): None
77+
#fetchmany(2): []
78+
#fetchall(): []

0 commit comments

Comments
 (0)