Handle SQL statements that return no result
authorKarl O. Pinc <kop@karlpinc.com>
Sun, 18 Aug 2024 19:43:04 +0000 (14:43 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sun, 18 Aug 2024 19:43:04 +0000 (14:43 -0500)
src/pgwui_sql/templates/sql.mak
src/pgwui_sql/views/sql.py

index 1e08a759116aa366d6a6689bf8be88407c912a9f..464ac42af01f1e58dae2bd5fdf3a02e8733c1640 100644 (file)
@@ -84,6 +84,9 @@
 </%def>
 
 <%def name="render_heading(headings)">
+  <% if headings is None:
+       return STOP_RENDERING
+  %>
   <thead>
     <tr>
       % for heading in headings:
   </tr>
 </%def>
 
-<%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.
   <table>
   <%
   if not result_rows:
      return STOP_RENDERING
-  heading = None
+  new_result = None
   command_result = []
   status_result = []
   %>
       % 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)}
           </%self:result_table>
-     
           <%
           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}
       % 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)}
     </%self:result_table>
   % endif
 </%def>
index c269048b73770f8d2346f08ff85d45706a71fbd7..6bf005cb2249a103ef39c0384c0b6a30287f2f05 100644 (file)
@@ -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()