summaryrefslogtreecommitdiff
path: root/test/src/cursor-commit-test.c
blob: 169974da3bcbf1d8315f608532970a0208eb038d (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
/*
 * This test case tests for a bug in result set caching, with
 * UseDeclareFetch=1, that was fixed. The bug occurred when a cursor was
 * closed, due to transaction commit, before any rows were fetched from
 * it. That set the "base" of the internal cached rowset incorrectly,
 * off by one.
 */

#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;
	SQLCHAR		charval[100];
	SQLLEN		len;
	int			row;

	test_connect();

	/* Start a transaction */
	rc = SQLSetConnectAttr(conn, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, SQL_IS_UINTEGER);

	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 = SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE,
						(SQLPOINTER) SQL_CURSOR_STATIC, SQL_IS_UINTEGER);
	CHECK_STMT_RESULT(rc, "SQLSetStmtAttr failed", hstmt);

	/*
	 * Begin executing a query
	 */
	rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT g FROM generate_series(1,3) g", SQL_NTS);
	CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);

	rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, &charval, sizeof(charval), &len);
	CHECK_STMT_RESULT(rc, "SQLBindCol failed", hstmt);

	/* Commit. This implicitly closes the cursor in the server. */
	rc = SQLEndTran(SQL_HANDLE_DBC, conn, SQL_COMMIT);
	if (!SQL_SUCCEEDED(rc))
	{
		print_diag("failed to commit", SQL_HANDLE_DBC, conn);
		exit(1);
	}

	rc = SQLFetchScroll(hstmt, SQL_FETCH_FIRST, 0);
	CHECK_STMT_RESULT(rc, "SQLFetchScroll(FIRST) failed", hstmt);

	if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
		printf("first row: %s\n", charval);

	row = 1;
	while (1)
	{
		rc = SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 0);
		if (rc == SQL_NO_DATA)
			break;

		row++;

		if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
			printf("row %d: %s\n", row, charval);
		else
		{
			print_diag("SQLFetchScroll failed", SQL_HANDLE_STMT, hstmt);
			exit(1);
		}
	}

	rc = SQLFreeStmt(hstmt, SQL_CLOSE);
	CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);

	/* Clean up */
	test_disconnect();

	return 0;
}