From aed0b67bc9c30357b6fc980527611c83d242e5c5 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Mon, 19 Aug 2024 12:16:43 -0500 Subject: [PATCH] Refactor SQL query result into its own object --- src/pgwui_sql/templates/sql.mak | 69 +++++++++------------------------ src/pgwui_sql/views/sql.py | 44 ++++++++++----------- 2 files changed, 39 insertions(+), 74 deletions(-) diff --git a/src/pgwui_sql/templates/sql.mak b/src/pgwui_sql/templates/sql.mak index 5859f37..b63ba17 100644 --- a/src/pgwui_sql/templates/sql.mak +++ b/src/pgwui_sql/templates/sql.mak @@ -111,27 +111,6 @@ -<%def name="result_table(rows=[], status=[])" filter="trim"> - ## Passing the result rows and processing them here avoids duplicating - ## the results in RAM. - - ${caller.body()} - - % for row in rows: - ${self.render_row(row.data)} - % endfor - -
-

- % for status_row in status: - % if not loop.first: -
- % endif - ${status_row.data} - % endfor -

- - <%def name="sql_error()">

${caller.body()}

@@ -144,39 +123,27 @@ command_result = [] status_result = [] %> -
% for result_row in result_rows: - <% type = result_row.type %> - % if type == 'data': - <% command_result.append(result_row) %> - % elif type == 'status': - <% status_result.append(result_row) %> - % else: - % if new_result: - <%self:result_table - rows="${command_result}" status="${status_result}"> - ${self.render_heading(new_result.data)} - -
- <% - command_result = [] - status_result = [] - %> - % endif - % if type == 'new_result': - <% new_result = result_row %> - % elif type == 'error': - <%self:sql_error> - ${result_row.data} - - % endif +
+ % if result_row.statusmessage is not None: +

${result_row.statusmessage.data.split(' ')[0]}

% endif - % endfor - <%self:result_table - rows="${command_result}" status="${status_result}"> - ${self.render_heading(new_result.data)} - + % if result_row.rows: + + % if result_row.heading is not None: + ${self.render_heading(result_row.heading.data)} + % endif + + % for row in result_row.rows: + ${self.render_row(row.data)} + % endfor + +
+ % endif + +

${result_row.rowcount.data}

+ % endfor
diff --git a/src/pgwui_sql/views/sql.py b/src/pgwui_sql/views/sql.py index 59884ba..2c29db8 100644 --- a/src/pgwui_sql/views/sql.py +++ b/src/pgwui_sql/views/sql.py @@ -81,28 +81,29 @@ class SQLForm(pgwui_core.forms.UploadFormBaseMixin, @attrs.define() class SQLResult(): + rows = attrs.field(factory=list) + statusmessage = attrs.field(default=None) + rowcount = attrs.field(default=None) + heading = attrs.field(default=None) + + +@attrs.define() +class ResultRow(): 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] + def build_heading_row(self, cur): + self.data = [col.name for col in cur.description] return self def build_statusmessage_row(self, cur): - self.type = 'status' self.data = cur.statusmessage return self def build_rowcount_row(self, cur): - self.type = 'status' self.data = f'{cur.rowcount} rows affected' return self def build_data_row(self, data): - self.type = 'data' self.data = data return self @@ -178,13 +179,11 @@ class SQLHandler(pgwui_core.core.SessionDBHandler): descr=err.diag.message_primary, detail=self.format_detail(err, stmt_text)) - def get_result_rows(self, cur, sql_results): - first = True + def get_result_rows(self, cur): + rows = [] while (row := cur.fetchone()) is not None: - if first: - sql_results.append(SQLResult().build_new_result_row(cur, True)) - first = False - sql_results.append(SQLResult().build_data_row(row)) + rows.append(ResultRow().build_data_row(row)) + return rows def cleanup(self): ''' @@ -198,14 +197,13 @@ class SQLHandler(pgwui_core.core.SessionDBHandler): sql_results = self.sql_results nextset = True while nextset is True: - have_rows = cur.rownumber is not None - if have_rows: - self.get_result_rows(cur, sql_results) - 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)) + sql_result = SQLResult() + if cur.rownumber is not None: + sql_result.heading = ResultRow().build_heading_row(cur) + sql_result.rows = self.get_result_rows(cur) + sql_result.statusmessage = ResultRow().build_statusmessage_row(cur) + sql_result.rowcount = ResultRow().build_rowcount_row(cur) + sql_results.append(sql_result) nextset = cur.nextset() def factory(self, ue): -- 2.34.1