From 58e566176b3de4efd0713097b890a45ac53b9990 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sun, 18 Aug 2024 14:43:04 -0500 Subject: [PATCH] Handle SQL statements that return no result --- src/pgwui_sql/templates/sql.mak | 20 +++++++++++--------- src/pgwui_sql/views/sql.py | 21 ++++++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/pgwui_sql/templates/sql.mak b/src/pgwui_sql/templates/sql.mak index 1e08a75..464ac42 100644 --- a/src/pgwui_sql/templates/sql.mak +++ b/src/pgwui_sql/templates/sql.mak @@ -84,6 +84,9 @@ <%def name="render_heading(headings)"> + <% if headings is None: + return STOP_RENDERING + %> % for heading in headings: @@ -101,7 +104,7 @@ -<%def name="result_table(rows=[], status=[])"> +<%def name="result_table(rows=[], status=[])" filter="trim"> ## Passing the result rows and processing them here avoids duplicating ## the results in RAM. @@ -130,7 +133,7 @@ <% if not result_rows: return STOP_RENDERING - heading = None + new_result = None command_result = [] status_result = [] %> @@ -141,19 +144,18 @@ % elif type == 'status': <% status_result.append(result_row) %> % else: - % if heading: + % if new_result: <%self:result_table rows="${command_result}" status="${status_result}"> - ${self.render_heading(heading.data)} + ${self.render_heading(new_result.data)} - <% command_result = [] status_result = [] %> % endif - % if type == 'heading': - <% heading = result_row %> + % if type == 'new_result': + <% new_result = result_row %> % elif type == 'error': <%self:sql_error> ${result_row.data} @@ -162,10 +164,10 @@ % endif % endfor - % if heading: + % if new_result: <%self:result_table rows="${command_result}" status="${status_result}"> - ${self.render_heading(heading.data)} + ${self.render_heading(new_result.data)} % endif diff --git a/src/pgwui_sql/views/sql.py b/src/pgwui_sql/views/sql.py index c269048..6bf005c 100644 --- a/src/pgwui_sql/views/sql.py +++ b/src/pgwui_sql/views/sql.py @@ -82,6 +82,13 @@ class SQLResult(): data = attrs.field(default=None) type = attrs.field(default=None) + def build_new_result_row(self, cur, have_rows): + self.type = 'new_result' + if have_rows: + # The data contains the column headings + self.data = [col.name for col in cur.description] + return self + def build_statusmessage_row(self, cur): self.type = 'status' self.data = cur.statusmessage @@ -97,11 +104,6 @@ class SQLResult(): self.data = data return self - def build_heading_row(self, cur): - self.type = 'heading' - self.data = [col.name for col in cur.description] - return self - @attrs.define(slots=False) class SQLHandler(pgwui_core.core.SessionDBHandler): @@ -158,13 +160,18 @@ class SQLHandler(pgwui_core.core.SessionDBHandler): sql_results = self.sql_results nextset = True while nextset is True: - if cur.rownumber is not None: + have_rows = cur.rownumber is not None + if have_rows: first = True while (row := cur.fetchone()) is not None: if first: - sql_results.append(SQLResult().build_heading_row(cur)) + sql_results.append(SQLResult().build_new_result_row( + cur, have_rows)) first = False sql_results.append(SQLResult().build_data_row(row)) + else: + sql_results.append(SQLResult().build_new_result_row( + cur, have_rows)) sql_results.append(SQLResult().build_statusmessage_row(cur)) sql_results.append(SQLResult().build_rowcount_row(cur)) nextset = cur.nextset() -- 2.34.1