Fix so single-file downloads work
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 5 Oct 2024 00:59:00 +0000 (19:59 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 5 Oct 2024 00:59:00 +0000 (19:59 -0500)
src/pgwui_sql/views/sql.py

index c000e7f67c75ba986aac527f922bde35ad17eca4..2c6b6654266edd36058884a50c3982fb5a4a883b 100644 (file)
@@ -302,23 +302,22 @@ class SQLResultsHandler(pgwui_core.core.SessionDBHandler):
             return csv.writer(fd, dialect=csv.excel_tab)
 
     def write_sql(self):
-        if self.uf['include_sql']:
-            if self.uf['download_as'] == MANY_FILES_VALUE:
-                with zipfile.Path(
-                    self.zip_fd,
-                    at=self.zip_at_pathname('statements.txt')
-                ).open(mode='wb') as fd:
-                    fd.write(self.uf['sql'].encode())
-                return None
-            else:
-                writer = self.make_csv_writer()
+        if self.uf['download_as'] == MANY_FILES_VALUE:
+            with zipfile.Path(
+                self.zip_fd,
+                at=self.zip_at_pathname('statements.txt')
+            ).open(mode='wb') as fd:
+                fd.write(self.uf['sql'].encode())
+        else:
+            writer = self.make_csv_writer()
+            if self.uf['include_sql']:
                 # Strip trailing whitespace from sql because otherwise,
                 # after import to a spreadsheet with cells a single row tall,
                 # only the empty line after the last EOL is shown and the
                 # first cell of the sheet looks empty instead of looking like
                 # it contains the sql.
                 writer.writerow((self.uf['sql'].rstrip(),))
-                return writer
+            return writer
 
     def write_resultset(self, cur, writer, null_rep):
         # Rather than report the statusmessage first, which requires
@@ -335,7 +334,7 @@ class SQLResultsHandler(pgwui_core.core.SessionDBHandler):
         # Optimized to minimize RAM usage
         null_rep = self.uf['null_rep']
         self.open_tfile()
-        writer = self.write_sql()
+        csv_writer = self.write_sql()
 
         nextset = True
         download_as = self.uf['download_as']
@@ -350,7 +349,7 @@ class SQLResultsHandler(pgwui_core.core.SessionDBHandler):
                         cur, self.make_csv_writer(fd), null_rep)
                 stmt_no += 1
             else:
-                self.write_resultset(cur, writer, null_rep)
+                self.write_resultset(cur, csv_writer, null_rep)
 
             nextset = cur.nextset()