summaryrefslogtreecommitdiff
path: root/test/src/cursor-name-test.c
blob: e185399b0392a0b68d143e491ab636da7135b3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;
}