Support executing SQL that returns rows
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 14 Sep 2024 23:04:33 +0000 (18:04 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 14 Sep 2024 23:04:40 +0000 (18:04 -0500)
src/pgwui_core/sql.py

index 0e5e870e3f16486c173f1a3307ebeed2347d50c3..baa9bd367e0ef1055eafd54e338d2ddd683d049c 100644 (file)
@@ -41,10 +41,23 @@ class SQLCommand():
       ec(ex) Produces the exception to raise an instance of on failure
               Input:
                 ex  The exception raised by psycopg3
+      fetcher(cur) Function to return the result(s) of the statement(s),
+                   or None (the default) when the statement(s) produce
+                   no results.
+                   When SQL statements return results, the usual libpq
+                   rules must be followed; all results must be retrieved
+                   from the cursor before it can be reused and more statements
+                   executed.
+                     Input:
+                       cur  A psycopg3 cursor object.
+      result       If a fetcher() is supplied, its return value is saved
+                   in this attribute.  Defaults to None.
     '''
     stmt = attrs.field()
     args = attrs.field()
     ec = attrs.field(default=None)
+    fetcher = attrs.field(default=None)
+    result = attrs.field(default=None)
 
     def _explain_encoding_error(self, ex):
         '''Return a SQLEncodingError instance
@@ -75,6 +88,7 @@ class SQLCommand():
         Side effects:
           Does something in the db.
           Can raise a psycopg3 error
+          Can call the "fetcher" attribute to set the "results" attribute
         '''
         try:
             cur.execute(self.stmt, self.args)
@@ -91,6 +105,9 @@ class SQLCommand():
                 raise ex
             raise self.ec(ex)
 
+        if self.fetcher:
+            self.result = self.fetcher(cur)
+
 
 @attrs.define(slots=False)
 class LogSQLCommand(SQLCommand):
@@ -102,6 +119,17 @@ class LogSQLCommand(SQLCommand):
       ec(ex) Produces the exception to raise an instance of on failure
               Input:
                 ex  The exception raised by psycopg3
+      fetcher(cur) Function to return the result(s) of the statement(s),
+                   or None (the default) when the statement(s) produce
+                   no results.
+                   When SQL statements return results, the usual libpq
+                   rules must be followed; all results must be retrieved
+                   from the cursor before it can be reused and more statements
+                   executed.
+                     Input:
+                       cur  A psycopg3 cursor object.
+      result       If a fetcher() is supplied, its return value is saved
+                   in this attribute.  Defaults to None.
       log_success
              Logs success
       log_failure(ex)
@@ -122,6 +150,7 @@ class LogSQLCommand(SQLCommand):
         Side effects:
           Does something in the db.
           Can raise a psycopg3 error
+          Can call the "fetcher" attribute to set the "results" attribute
         '''
         try:
             super().execute(cur)
@@ -136,7 +165,7 @@ class LogSQLCommand(SQLCommand):
 
 class SQLData(core.DBData):
     '''
-    SQL statements returning no data that execute in the db.
+    SQL statements that execute in the db.
 
     Attributes:
       stmts  List of SQLCommand instances