Prevent cursors being closed prematurely
authorAdrian Grucza <adrian.grucza@iress.com>
Fri, 23 Jun 2023 00:15:24 +0000 (09:15 +0900)
committerHiroshi Inoue <h-inoue@dream.email.ne.jp>
Fri, 23 Jun 2023 00:15:26 +0000 (09:15 +0900)
qresult.c
test/expected/fetch-refcursors.out
test/src/fetch-refcursors-test.c

index 40ad3b7607e8147b5239fbb917c6b7ae49770694..f4e5fd493d9d35086b3adde1b97e2af3dd97de65 100644 (file)
--- a/qresult.c
+++ b/qresult.c
@@ -99,14 +99,24 @@ QR_set_cursor(QResultClass *self, const char *name)
    }
    else
    {
-       QResultClass *res;
-
        self->cursor_name = NULL;
-       for (res = QR_nextr(self); NULL != res; res = QR_nextr(res))
+
+       /*
+       * The isSqlServr() check below was added because the code was freeing
+       * cursors prematurely when other results with open cursors exist. The
+       * code was originally added for a scenario using SQL Server linked
+       * servers in commit c07342d22d82ea6293d27057840babfc2ff6d750.
+       */
+       if (isSqlServr())
        {
-           if (NULL != res->cursor_name)
-               free(res->cursor_name);
-           res->cursor_name = NULL;
+           QResultClass *res;
+
+           for (res = QR_nextr(self); NULL != res; res = QR_nextr(res))
+           {
+               if (NULL != res->cursor_name)
+                   free(res->cursor_name);
+               res->cursor_name = NULL;
+           }
        }
    }
 }
index bfe6f85e6019f80e5550bf825eab3028c29b4e4e..a594d4d11f5905f95ee7009627831fb50cf5f859 100644 (file)
@@ -2,25 +2,25 @@ Creating procedure 'refproc'
 connected
 disconnecting
 
--- TEST using FetchRefcursors=0, autocommit=1, numresults=2
+-- TEST using Fetch=1;FetchRefcursors=0, autocommit=1, numresults=2
 connected
 Output param num_cursor is 2
 --1 Result set:
 2  <unnamed portal 1>  <unnamed portal 2>
 disconnecting
 
--- TEST using FetchRefcursors=1, autocommit=1, numresults=2
+-- TEST using Fetch=1;FetchRefcursors=1, autocommit=1, numresults=2
 connected
 SQLExecute failed
 HY000=Query must be executed in a transaction when FetchRefcursors setting is enabled.
 
--- TEST using FetchRefcursors=1, autocommit=0, numresults=0
+-- TEST using Fetch=1;FetchRefcursors=1, autocommit=0, numresults=0
 connected
 Output param num_cursor is 0
 --1 Result set:
 disconnecting
 
--- TEST using FetchRefcursors=1, autocommit=0, numresults=1
+-- TEST using Fetch=1;FetchRefcursors=1, autocommit=0, numresults=1
 connected
 Output param num_cursor is 1
 --1 Result set:
@@ -29,7 +29,7 @@ Output param num_cursor is 1
 3  foobar
 disconnecting
 
--- TEST using FetchRefcursors=1, autocommit=0, numresults=2
+-- TEST using Fetch=1;FetchRefcursors=1, autocommit=0, numresults=2
 connected
 Output param num_cursor is 2
 --1 Result set:
@@ -42,7 +42,7 @@ bar   2
 foo    1
 disconnecting
 
--- TEST using FetchRefcursors=1, autocommit=0, numresults=3
+-- TEST using Fetch=1;FetchRefcursors=1, autocommit=0, numresults=3
 connected
 Output param num_cursor is 2
 --1 Result set:
index 01e58e4f5f11361415c266535d8bd2e9c5856441..94539494235458cb0b707c191592886506e796f8 100644 (file)
@@ -108,12 +108,13 @@ int main(int argc, char **argv)
 {
    setup_procedure();
 
-   refcursor_test("FetchRefcursors=0", SQL_AUTOCOMMIT_ON, 2);
-   refcursor_test("FetchRefcursors=1", SQL_AUTOCOMMIT_ON, 2);
-   refcursor_test("FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 0);
-   refcursor_test("FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 1);
-   refcursor_test("FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 2);
-   refcursor_test("FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 3);
+   /* Using a fetch cache size of 1 to test multiple fetches per cursor */
+   refcursor_test("Fetch=1;FetchRefcursors=0", SQL_AUTOCOMMIT_ON, 2);
+   refcursor_test("Fetch=1;FetchRefcursors=1", SQL_AUTOCOMMIT_ON, 2);
+   refcursor_test("Fetch=1;FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 0);
+   refcursor_test("Fetch=1;FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 1);
+   refcursor_test("Fetch=1;FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 2);
+   refcursor_test("Fetch=1;FetchRefcursors=1", SQL_AUTOCOMMIT_OFF, 3);
 
    return 0;
 }