psqlodbc.git / summary / log / commit / refs
commit 02be52cdfd9de4e6faa87e31e52b332803fdf061
Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon Nov 03 09:05:01 2014 +0000
Add test case.
This tests a codepath in QR_next_tuple that had no coverage before.
Makefile.am | 2 +
test/expected/declare-fetch-commit.out | 9 +++
test/src/declare-fetch-commit-test.c | 100 +++++++++++++++++++++++++++++++++
test/tests | 1 +
4 files changed, 112 insertions(+)
diff --git a/Makefile.am b/Makefile.am
index b30e4336..9bb09481 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -96,6 +96,7 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \
test/expected/connect.out \
test/expected/cte.out \
test/expected/cursor-commit.out \
+ test/expected/declare-fetch-commit-test.out \
test/expected/cursors.out \
test/expected/cursors_1.out \
test/expected/cvtnulldate.out \
@@ -135,6 +136,7 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \
test/src/connect-test.c \
test/src/cte-test.c \
test/src/cursor-commit-test.c \
+ test/src/declare-fetch-commit-test.c \
test/src/cursors-test.c \
test/src/cvtnulldate-test.c \
test/src/dataatexecution-test.c \
diff --git a/test/expected/declare-fetch-commit.out b/test/expected/declare-fetch-commit.out
new file mode 100644
index 00000000..ea80bf16
--- /dev/null
+++ b/test/expected/declare-fetch-commit.out
@@ -0,0 +1,9 @@
+\! "./src/declare-fetch-commit-test"
+connected
+Result set:
+1
+2
+3
+4
+5
+disconnecting
diff --git a/test/src/declare-fetch-commit-test.c b/test/src/declare-fetch-commit-test.c
new file mode 100644
index 00000000..48e012bc
--- /dev/null
+++ b/test/src/declare-fetch-commit-test.c
@@ -0,0 +1,100 @@
+/*
+ * Test what happens when the transaction is committed while fetching from a result
+ * set.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common.h"
+
+int main(int argc, char **argv)
+{
+ int rc;
+ HSTMT hstmt = SQL_NULL_HSTMT;
+ int rows;
+
+ /****
+ * Run this test with UseDeclareFetch = 1 and Fetch=1, so that we fetch
+ * one row at a time
+ */
+ test_connect_ext("UseDeclareFetch=1;Fetch=1");
+
+ rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);
+ if (!SQL_SUCCEEDED(rc))
+ {
+ print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+ exit(1);
+ }
+
+ /* Disable autocommit, and execute a dummy UPDATE to start a transaction */
+ rc = SQLSetConnectAttr(conn,
+ SQL_ATTR_AUTOCOMMIT,
+ (SQLPOINTER)SQL_AUTOCOMMIT_OFF,
+ SQL_IS_UINTEGER);
+ CHECK_STMT_RESULT(rc, "SQLSetConnectAttr failed", hstmt);
+
+ SQLExecDirect(hstmt, (SQLCHAR *) "update testtbl1 set t = t where id = 123456", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+ rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+ CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+ /**** Start a cursor ****/
+
+ /* Prepare a statement */
+ rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT * FROM generate_series(1, 5) g ", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
+
+ /* Execute */
+ rc = SQLExecute(hstmt);
+ CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);
+
+ /* Fetch result */
+
+ printf("Result set:\n");
+ rows = 0;
+ while(1)
+ {
+ rc = SQLFetch(hstmt);
+ if (rc == SQL_NO_DATA)
+ break;
+ if (rc == SQL_SUCCESS)
+ {
+ char buf[40];
+ SQLLEN ind;
+
+ rc = SQLGetData(hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), &ind);
+ if (!SQL_SUCCEEDED(rc))
+ {
+ print_diag("SQLGetData failed", SQL_HANDLE_STMT, hstmt);
+ exit(1);
+ }
+ printf("%s", buf);
+
+ printf("\n");
+ }
+ else
+ {
+ print_diag("SQLFetch failed", SQL_HANDLE_STMT, hstmt);
+ exit(1);
+ }
+
+ /* In the middle of the result set, COMMIT */
+ if (rows == 2)
+ {
+ rc = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_COMMIT);
+ CHECK_STMT_RESULT(rc, "SQLEndTran failed\n", hstmt);
+ }
+ rows++;
+ }
+
+ rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+ CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+ /* Clean up */
+ test_disconnect();
+
+ return 0;
+}
diff --git a/test/tests b/test/tests
index 75c929dc..269780b1 100644
--- a/test/tests
+++ b/test/tests
@@ -26,6 +26,7 @@ TESTBINS = src/connect-test \
src/quotes-test \
src/cursors-test \
src/cursor-commit-test \
+ src/declare-fetch-commit-test \
src/positioned-update-test \
src/catalogfunctions-test \
src/bindcol-test \
[parent: bfc9c7dcf31f]