From 57c80fe4f99ae02284244f0a192fbd91c72c3ec7 Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Tue, 1 May 2018 10:53:02 +0200
Subject: [PATCH] pgsql: Make affected_rows() and status_command_complete()
 wait for a meaningful result.

---
 lib/modules/Sql.pmod/pgsql_util.pmod | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/lib/modules/Sql.pmod/pgsql_util.pmod b/lib/modules/Sql.pmod/pgsql_util.pmod
index ae84c2b826..83f5ce348c 100644
--- a/lib/modules/Sql.pmod/pgsql_util.pmod
+++ b/lib/modules/Sql.pmod/pgsql_util.pmod
@@ -749,26 +749,34 @@ class sql_result {
 
   //! Returns the command-complete status for this query.
   //!
-  //! @seealso
-  //!  @[affected_rows()]
+  //! @note
+  //!  This method should normally be called after EOF has been reached.
+  //!  If it is called before, all unfetched result rows will be discarded.
   //!
   //! @note
   //! This function is PostgreSQL-specific.
+  //!
+  //!  @[affected_rows()], @[eof()]
   /*semi*/final string status_command_complete() {
+    if (!statuscmdcomplete)
+      while (fetch_row_array());
     return statuscmdcomplete;
   }
 
   //! Returns the number of affected rows by this query.
   //!
-  //! @seealso
-  //!  @[status_command_complete()]
+  //! @note
+  //!  This method should normally be called after EOF has been reached.
+  //!  If it is called before, all unfetched result rows will be discarded.
   //!
   //! @note
   //! This function is PostgreSQL-specific.
+  //!
+  //! @seealso
+  //!  @[status_command_complete()], @[eof()]
   /*semi*/final int affected_rows() {
     int rows;
-    if (statuscmdcomplete)
-      sscanf(statuscmdcomplete, "%*s %d %d", rows, rows);
+    sscanf(status_command_complete(), "%*s %d %d", rows, rows);
     return rows;
   }
 
-- 
GitLab