Move sql execution into upload handler's cleanup() method
authorKarl O. Pinc <kop@karlpinc.com>
Sun, 18 Aug 2024 18:11:36 +0000 (13:11 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sun, 18 Aug 2024 18:11:36 +0000 (13:11 -0500)
The data line processor is designed to work with lines of data,
and we don't have that.

src/pgwui_sql/views/sql.py

index cf84b5ad6485265524ffb6bff42b7e3a7e922668..143f3a5f433c447f940ea6cfdfec9a937a61027b 100644 (file)
@@ -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',