Add test for fetching deleted/updated rows in an updateable cursor.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 5 Dec 2014 19:17:21 +0000 (21:17 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 5 Dec 2014 19:17:21 +0000 (21:17 +0200)
test/expected/positioned-update.out
test/src/positioned-update-test.c

index 0a03550557a302bd7ad2ed8505f1a4c0f677e521..57554b63a0f826cd52f61a09ea7e836d6b5c9a6f 100644 (file)
@@ -7,7 +7,15 @@ Opening a cursor for update, and fetching 10 rows
 3  3
 4  4
 5  5
+
 Updating result set
+
+Re-fetching the rows in the result set
+6  6
+105    5
+4  4
+
+Querying the table again
 Result set:
 1  1
 2  2
index 5c6ee0710f4a3c1125b2c667f61f99b54f48f466..4e58b1a44573a74af3040eb4da8e06012bf7ef80 100644 (file)
@@ -4,10 +4,33 @@
 
 #include "common.h"
 
+void
+printCurrentRow(HSTMT hstmt)
+{
+   char        buf[40];
+   int         col;
+   SQLLEN      ind;
+   int         rc;
+
+   for (col = 1; col <= 2; col++)
+   {
+       rc = SQLGetData(hstmt, col, SQL_C_CHAR, buf, sizeof(buf), &ind);
+       if (!SQL_SUCCEEDED(rc))
+       {
+           print_diag("SQLGetData failed", SQL_HANDLE_STMT, hstmt);
+           exit(1);
+       }
+       if (ind == SQL_NULL_DATA)
+           strcpy(buf, "NULL");
+       printf("%s%s", (col > 1) ? "\t" : "", buf);
+   }
+   printf("\n");
+}
+
 int main(int argc, char **argv)
 {
-   int rc;
-   HSTMT hstmt = SQL_NULL_HSTMT;
+   int         rc;
+   HSTMT       hstmt = SQL_NULL_HSTMT;
    int         i;
    SQLINTEGER  colvalue;
    SQLLEN      indColvalue;
@@ -55,23 +78,8 @@ int main(int argc, char **argv)
            break;
        if (rc == SQL_SUCCESS)
        {
-           char buf[40];
-           int col;
-           SQLLEN ind;
-
-           for (col = 1; col <= 2; col++)
-           {
-               rc = SQLGetData(hstmt, col, SQL_C_CHAR, buf, sizeof(buf), &ind);
-               if (!SQL_SUCCEEDED(rc))
-               {
-                   print_diag("SQLGetData failed", SQL_HANDLE_STMT, hstmt);
-                   exit(1);
-               }
-               if (ind == SQL_NULL_DATA)
-                   strcpy(buf, "NULL");
-               printf("%s%s", (col > 1) ? "\t" : "", buf);
-           }
-           printf("\n");
+
+           printCurrentRow(hstmt);
        }
        else
        {
@@ -81,7 +89,7 @@ int main(int argc, char **argv)
    }
 
    /* Do a positioned update and delete */
-   printf("Updating result set\n");
+   printf("\nUpdating result set\n");
    colvalue += 100;
    rc = SQLSetPos(hstmt, 1, SQL_UPDATE, SQL_LOCK_NO_CHANGE);
    CHECK_STMT_RESULT(rc, "SQLSetPos failed", hstmt);
@@ -92,10 +100,28 @@ int main(int argc, char **argv)
    rc = SQLSetPos(hstmt, 1, SQL_DELETE, SQL_LOCK_NO_CHANGE);
    CHECK_STMT_RESULT(rc, "SQLSetPos failed", hstmt);
 
+
+   /**** See if the updates are reflected in the still-open result set ***/
+   printf("\nRe-fetching the rows in the result set\n");
+
+   rc = SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 0);
+   CHECK_STMT_RESULT(rc, "SQLFetchScroll failed", hstmt);
+   printCurrentRow(hstmt);
+
+   rc = SQLFetchScroll(hstmt, SQL_FETCH_PRIOR, -1);
+   CHECK_STMT_RESULT(rc, "SQLFetchScroll failed", hstmt);
+   printCurrentRow(hstmt);
+
+   rc = SQLFetchScroll(hstmt, SQL_FETCH_PRIOR, -1);
+   CHECK_STMT_RESULT(rc, "SQLFetchScroll failed", hstmt);
+   printCurrentRow(hstmt);
+
    rc = SQLFreeStmt(hstmt, SQL_CLOSE);
    CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
 
-   /* See if the update took effect */
+
+   /**** See if the updates really took effect ****/
+   printf("\nQuerying the table again\n");
    rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT * FROM pos_update_test ORDER BY orig", SQL_NTS);
    CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
    print_result(hstmt);