psqlodbc.git / summary / log / commit / refs
commit 81cfb058094de1d4e04044f1a5828ac70d626cf2
Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Fri Dec 05 18:45:36 2014 +0000
Add test case for SQLSetCursorName and SQLGetCursorName.
The test case also contains tests for UPDATE ... WHERE CURRENT OF, but
it's disabled because the driver doesn't actually support it. I wrote the
test before I realized that, and didn't want to throw it away, in case we
ever want to add support for it.
It also contains an #ifdef'd out test for cursor names that contain quotes.
The driver doesn't handle that correctly, but I didn't want to fix it right
now. No sane application does that anyway.
Makefile.am | 6 ++-
test/expected/cursor-name.out | 4 ++
test/src/cursor-name-test.c | 112 ++++++++++++++++++++++++++++++++++++++++++
test/tests | 1 +
4 files changed, 121 insertions(+), 2 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 4b06c8d7..f5e912ad 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -99,11 +99,12 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \
test/expected/cte.out \
test/expected/cursor-commit.out \
test/expected/cursor-movement.out \
- test/expected/declare-fetch-commit-test.out \
+ test/expected/cursor-name.out \
test/expected/cursors.out \
test/expected/cursors_1.out \
test/expected/cvtnulldate.out \
test/expected/dataatexecution.out \
+ test/expected/declare-fetch-commit-test.out \
test/expected/deprecated.out \
test/expected/diagnostic.out \
test/expected/error-rollback.out \
@@ -144,10 +145,11 @@ EXTRA_DIST = license.txt readme.txt readme_winbuild.txt \
test/src/cte-test.c \
test/src/cursor-commit-test.c \
test/src/cursor-movement-test.c \
- test/src/declare-fetch-commit-test.c \
+ test/src/cursor-name-test.c \
test/src/cursors-test.c \
test/src/cvtnulldate-test.c \
test/src/dataatexecution-test.c \
+ test/src/declare-fetch-commit-test.c \
test/src/deprecated-test.c \
test/src/diagnostic-test.c \
test/src/error-rollback-test.c \
diff --git a/test/expected/cursor-name.out b/test/expected/cursor-name.out
new file mode 100644
index 00000000..e2195fcd
--- /dev/null
+++ b/test/expected/cursor-name.out
@@ -0,0 +1,4 @@
+\! "./src/cursor-name-test"
+connected
+cursor name prefix: SQL_CUR
+disconnecting
diff --git a/test/src/cursor-name-test.c b/test/src/cursor-name-test.c
new file mode 100644
index 00000000..e185399b
--- /dev/null
+++ b/test/src/cursor-name-test.c
@@ -0,0 +1,112 @@
+#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;
+ HSTMT hstmt2 = SQL_NULL_HSTMT;
+ char cursornamebuf[20];
+ SQLSMALLINT cursornamelen;
+ char buf[40];
+ SQLLEN ind;
+
+ /**** Test cursor names ****/
+
+ test_connect();
+
+ rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);
+ if (!SQL_SUCCEEDED(rc))
+ {
+ print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+ exit(1);
+ }
+ rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt2);
+ if (!SQL_SUCCEEDED(rc))
+ {
+ print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
+ exit(1);
+ }
+
+ /* first create a test table we can update */
+ rc = SQLExecDirect(hstmt,
+ (SQLCHAR *) "create temporary table cursortesttbl as "
+ "SELECT id, 'foo' || id as t FROM generate_series(1,10) id",
+ SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+ rc = SQLGetCursorName(hstmt, cursornamebuf, sizeof(cursornamebuf), &cursornamelen);
+ CHECK_STMT_RESULT(rc, "SQLGetCursorName failed", hstmt);
+
+ rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+ CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+ /* Per the ODBC spec, the cursor name should begin with SQL_CUR */
+ cursornamebuf[strlen("SQL_CUR")] = '\0';
+ printf("cursor name prefix: %s\n", cursornamebuf);
+
+ /* Use a custom name */
+ rc = SQLSetCursorName(hstmt, (SQLCHAR *) "my_test_cursor", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLSetCursorName failed", hstmt);
+
+ /*
+ * Fetch a large result set without cursor (in Declare/fetch mode, it will
+ * be fetched in chunks)
+ */
+ rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT * FROM cursortesttbl WHERE id < 9", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+
+ /* Fetch until we find the row with id = 5 */
+ do {
+ rc = SQLFetch(hstmt);
+ CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt);
+
+ rc = SQLGetData(hstmt, 1, SQL_C_CHAR, buf, sizeof(buf), &ind);
+ CHECK_STMT_RESULT(rc, "SQLGetData failed", hstmt);
+ } while(strcmp(buf, "5") != 0);
+
+ /* use the cursor's name for update */
+ /*
+ * XXX: Disabled, because the driver doesn't support UPDATE ... WHERE
+ * CURRENT OF. It works with some options (Fetch=1), but it's not reported
+ * as supported so there's no guarantees.
+ */
+#ifdef NOT_SUPPORTED
+ printf("Updating row with id=5...\n");
+ rc = SQLExecDirect(hstmt2, (SQLCHAR *) "UPDATE cursortesttbl SET t = 'updated' || id WHERE CURRENT OF my_test_cursor", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt2);
+
+ rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+ CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+ rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT * FROM cursortesttbl ORDER BY id FOR UPDATE", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+ print_result(hstmt);
+#endif
+
+ rc = SQLFreeStmt(hstmt, SQL_CLOSE);
+ CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
+
+ /****
+ * Test a cursor name that requires quoting. The ODBC spec recommends
+ * not to use such a name, but we better handle it gracefully anyway.
+ *
+ * FIXME: the driver doesn't actually handle this.
+ */
+#ifdef BROKEN
+ rc = SQLSetCursorName(hstmt, (SQLCHAR *) "my nasty \"quoted\" cur'sor", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLSetCursorName failed", hstmt);
+
+ rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT * FROM cursortesttbl WHERE id < 4", SQL_NTS);
+ CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
+ print_result(hstmt);
+#endif
+
+ /* Clean up */
+ test_disconnect();
+
+ return 0;
+}
diff --git a/test/tests b/test/tests
index 49fcd95b..9df6c4eb 100644
--- a/test/tests
+++ b/test/tests
@@ -29,6 +29,7 @@ TESTBINS = src/connect-test \
src/cursors-test \
src/cursor-movement-test \
src/cursor-commit-test \
+ src/cursor-name-test \
src/bookmark-test \
src/declare-fetch-commit-test \
src/positioned-update-test \
[parent: 3a82c9d56699]