Skip to content

Commit b9cfa7e

Browse files
committed
Handle 'None' query results (new in ibm_db 3.2.7) and convert them to an empty list (how it was in ibm_db_3.2.6)
1 parent afb148d commit b9cfa7e

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/DatabaseLibrary/query.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def query(
8989
omit_trailing_semicolon=db_connection.omit_trailing_semicolon,
9090
)
9191
all_rows = cur.fetchall()
92+
if all_rows is None:
93+
all_rows = []
9294
self._commit_if_needed(db_connection, no_transaction)
9395
col_names = [c[0] for c in cur.description]
9496
self._log_query_results(col_names, all_rows)
@@ -144,6 +146,8 @@ def row_count(
144146
omit_trailing_semicolon=db_connection.omit_trailing_semicolon,
145147
)
146148
data = cur.fetchall()
149+
if data is None:
150+
data = []
147151
self._commit_if_needed(db_connection, no_transaction)
148152
col_names = [c[0] for c in cur.description]
149153
if db_connection.module_name in ["sqlite3", "ibm_db", "ibm_db_dbi", "pyodbc", "jaydebeapi"]:
@@ -803,6 +807,10 @@ def _log_query_results(self, col_names, result_rows, log_head: Optional[int] = N
803807

804808
if log_head is None:
805809
log_head = self.LOG_QUERY_RESULTS_HEAD
810+
811+
if result_rows is None:
812+
result_rows = []
813+
806814
cell_border_and_align = "border: 1px solid rgb(160 160 160);padding: 8px 10px;text-align: center;"
807815
table_border = "2px solid rgb(140 140 140)"
808816
row_index_background_color = "#d6ecd4"
@@ -816,25 +824,26 @@ def _log_query_results(self, col_names, result_rows, log_head: Optional[int] = N
816824
msg += f'<th scope="col" style="background-color: #505050; color: #fff;{cell_border_and_align}">{col}</th>'
817825
msg += "</tr>"
818826
table_truncated = False
819-
if result_rows is not None:
820-
for i, row in enumerate(result_rows):
821-
if log_head and i >= log_head:
822-
table_truncated = True
823-
break
824-
row_style = ""
825-
if i % 2 == 0:
826-
row_style = ' style="background-color: var(--secondary-color, #eee)"'
827-
msg += f"<tr{row_style}>"
828-
msg += f'<th scope="row" style="color:{row_index_text_color}; background-color: {row_index_background_color};{cell_border_and_align}">{i}</th>'
829-
for cell in row:
830-
try:
831-
cell_string = str(cell)
832-
except TypeError as e:
833-
cell_string = f"Unable printing the value: {e}"
834-
msg += f'<td style="{cell_border_and_align}">{cell_string}</td>'
835-
msg += "</tr>"
836-
msg += "</table>"
837-
if table_truncated:
838-
msg += f'<p style="font-weight: bold;">Log limit of {log_head} rows was reached, the table was truncated</p>'
839-
msg += "</div>"
840-
logger.info(msg, html=True)
827+
for i, row in enumerate(result_rows):
828+
if log_head and i >= log_head:
829+
table_truncated = True
830+
break
831+
row_style = ""
832+
if i % 2 == 0:
833+
row_style = ' style="background-color: var(--secondary-color, #eee)"'
834+
msg += f"<tr{row_style}>"
835+
msg += f'<th scope="row" style="color:{row_index_text_color}; background-color: {row_index_background_color};{cell_border_and_align}">{i}</th>'
836+
for cell in row:
837+
try:
838+
cell_string = str(cell)
839+
except TypeError as e:
840+
cell_string = f"Unable printing the value: {e}"
841+
msg += f'<td style="{cell_border_and_align}">{cell_string}</td>'
842+
msg += "</tr>"
843+
msg += "</table>"
844+
if table_truncated:
845+
msg += (
846+
f'<p style="font-weight: bold;">Log limit of {log_head} rows was reached, the table was truncated</p>'
847+
)
848+
msg += "</div>"
849+
logger.info(msg, html=True)

test/tests/common_tests/basic_tests.robot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,8 @@ Verify Query - Row Count foobar table 0 row
157157
[Setup] Create Foobar Table And Insert Data
158158
Delete All Rows From Table foobar
159159
Row Count Is 0 SELECT * FROM foobar
160+
161+
Query Returns Zero Results
162+
[Documentation] Tests that nothing crashes when there are zero results
163+
${results}= Query SELECT * FROM Person WHERE id < 0
164+
Should Be Empty ${results}

0 commit comments

Comments
 (0)