From a05ab22c099ce215d44a0cae429f59ec9e7bc9e4 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sun, 18 Aug 2024 13:11:36 -0500 Subject: [PATCH] Move sql execution into upload handler's cleanup() method The data line processor is designed to work with lines of data, and we don't have that. --- src/pgwui_sql/views/sql.py | 75 +++++++++++++++----------------------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/src/pgwui_sql/views/sql.py b/src/pgwui_sql/views/sql.py index cf84b5a..143f3a5 100644 --- a/src/pgwui_sql/views/sql.py +++ b/src/pgwui_sql/views/sql.py @@ -103,44 +103,12 @@ class SQLResult(): return self -class ExecuteSQL(pgwui_core.core.DataLineProcessor): - ''' - Attributes: - request A pyramid request instance - uf A GCUploadForm instance - session A pyramid session instance - ue - uh UploadHandler instance - cur - ''' - def eat(self, udl): - ''' - Execute a series of SQL statements. - The result goes into the upload handler (uh.sql_results), - interleaving errors with output. - - udl An UploadDataLine instance, contains all the sql statements - ''' - cur = self.cur - cur.execute(self.uf.data) - - sql_results = self.uh.sql_results - nextset = True - while nextset is True: - first = True - while (row := cur.fetchone()) is not None: - if first: - sql_results.append(SQLResult().build_header_row(cur)) - first = False - sql_results.append(SQLResult().build_data_row(row)) - sql_results.append(SQLResult().build_statusmessage_row(cur)) - sql_results.append(SQLResult().build_rowcount_row(cur)) - nextset = cur.nextset() - - @attrs.define(slots=False) class SQLHandler(pgwui_core.core.SessionDBHandler): ''' + Deliver no data to the upload engine, instead do all the SQL + execution here, in the cleanup method. + Attributes: request A pyramid request instance uf A SQLForm instance @@ -156,14 +124,10 @@ class SQLHandler(pgwui_core.core.SessionDBHandler): ''' return SQLForm().build(self, ip=SQLInitialPost(), fc=SQLWTForm) - def deliver_sql(self): - return self.uf['sql'] - def get_data(self): - '''Return thunks that delivers data, but we only need one thunk - because processing is done by the DataLineProcessor (SQLExecute + '''Return no data. Data is in lines and we have no lines. ''' - return (self.deliver_sql(),) + self.data = tuple() def write(self, result, errors): ''' @@ -182,6 +146,28 @@ class SQLHandler(pgwui_core.core.SessionDBHandler): return response + def cleanup(self): + ''' + Execute a series of SQL statements. + The result goes into the upload handler (uh.sql_results), + interleaving errors with output. + ''' + cur = self.cur + cur.execute(self.uf['sql']) + + sql_results = self.sql_results + nextset = True + while nextset is True: + first = True + while (row := cur.fetchone()) is not None: + if first: + sql_results.append(SQLResult().build_header_row(cur)) + first = False + sql_results.append(SQLResult().build_data_row(row)) + sql_results.append(SQLResult().build_statusmessage_row(cur)) + sql_results.append(SQLResult().build_rowcount_row(cur)) + nextset = cur.nextset() + def factory(self, ue): '''Make a db loader function from an UploadEngine. @@ -191,10 +177,9 @@ class SQLHandler(pgwui_core.core.SessionDBHandler): Assigns: self.ue, self.cur And, lots of changes to the db ''' - - super().factory(ue) - - return ExecuteSQL(ue, self) + self.ue = ue + self.cur = ue.cur + return pgwui_core.core.DataLineProcessor(ue, self) @view_config(route_name='pgwui_sql', -- 2.34.1