From 42c509d19b19d2b9ff2d01171740fc1d88c69ccd Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Fri, 5 Dec 2014 21:17:21 +0200 Subject: [PATCH] Add test for fetching deleted/updated rows in an updateable cursor. --- test/expected/positioned-update.out | 8 ++++ test/src/positioned-update-test.c | 68 ++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/test/expected/positioned-update.out b/test/expected/positioned-update.out index 0a03550..57554b6 100644 --- a/test/expected/positioned-update.out +++ b/test/expected/positioned-update.out @@ -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 diff --git a/test/src/positioned-update-test.c b/test/src/positioned-update-test.c index 5c6ee07..4e58b1a 100644 --- a/test/src/positioned-update-test.c +++ b/test/src/positioned-update-test.c @@ -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); -- 2.39.5