1 #include "common.h"
2 
3 #include <common/test_assert.h>
4 
5 /* Test SQLFetchScroll with no bound columns */
6 
7 static int bind_all = 0;
8 static int normal_fetch = 0;
9 static int use_cursors = 1;
10 
Test(void)11 static void Test(void)
12 {
13 #define ROWS 5
14 	struct data_t {
15 		SQLINTEGER i;
16 		SQLLEN ind_i;
17 		char c[20];
18 		SQLLEN ind_c;
19 	} data[ROWS];
20 	SQLUSMALLINT statuses[ROWS];
21 	SQLLEN num_row;
22 
23 	odbc_reset_statement();
24 
25 	/* this should not fail or return warnings */
26 	if (use_cursors) {
27 		CHKSetStmtAttr(SQL_ATTR_CONCURRENCY, int2ptr(SQL_CONCUR_READ_ONLY), 0, "S");
28 		CHKSetStmtAttr(SQL_ATTR_CURSOR_TYPE, int2ptr(SQL_CURSOR_STATIC), 0, "S");
29 	}
30 	CHKPrepare(T("SELECT c, i FROM #cursor6_test"), SQL_NTS, "S");
31 	CHKExecute("S");
32 	CHKSetStmtAttr(SQL_ATTR_ROW_BIND_TYPE, int2ptr(sizeof(data[0])), 0, "S");
33 	CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, int2ptr(ROWS), 0, "S");
34 	CHKSetStmtAttr(SQL_ATTR_ROW_STATUS_PTR, statuses, 0, "S");
35 	CHKSetStmtAttr(SQL_ATTR_ROWS_FETCHED_PTR, &num_row, 0, "S");
36 	if (bind_all)
37 		CHKBindCol(1, SQL_C_CHAR, &data[0].c, sizeof(data[0].c), &data[0].ind_c, "S");
38 	CHKBindCol(2, SQL_C_LONG, &data[0].i, sizeof(data[0].i), &data[0].ind_i, "S");
39 
40 #define FILL(s, n) do { \
41 	int _n; for (_n = 0; _n < sizeof(s)/sizeof(s[0]); ++_n) s[_n] = n; \
42 } while(0)
43 	FILL(statuses, 9876);
44 	num_row = -3;
45 	data[0].i = (SQLINTEGER) 0xdeadbeef;
46 	data[1].i = (SQLINTEGER) 0xdeadbeef;
47 	if (normal_fetch)
48 		CHKFetch("S");
49 	else
50 		CHKFetchScroll(SQL_FETCH_NEXT, 0, "S");
51 
52 	/* now check row numbers */
53 	printf("num_row %ld statuses[0] %d statuses[1] %d odbc3 %d\n", (long int) num_row,
54 		(int) statuses[0], (int) statuses[1], odbc_use_version3);
55 
56 	if (odbc_use_version3 || !normal_fetch) {
57 		if (num_row != ROWS || statuses[0] != SQL_ROW_SUCCESS || statuses[1] != SQL_ROW_SUCCESS) {
58 			fprintf(stderr, "result error 1\n");
59 			exit(1);
60 		}
61 	} else {
62 		if (data[0].i != 1 || data[1].i != 0xdeadbeef) {
63 			fprintf(stderr, "result error 2\n");
64 			exit(1);
65 		}
66 	}
67 
68 	FILL(statuses, 8765);
69 	num_row = -3;
70 	if (normal_fetch)
71 		CHKFetch("S");
72 	else
73 		CHKFetchScroll(SQL_FETCH_NEXT, 0, "S");
74 }
75 
Init(void)76 static void Init(void)
77 {
78 	int i;
79 	char sql[128];
80 
81 	odbc_command("CREATE TABLE #cursor6_test (i INT, c VARCHAR(20))");
82 	for (i = 1; i <= 10; ++i) {
83 		sprintf(sql, "INSERT INTO #cursor6_test(i,c) VALUES(%d, 'a%db%dc%d')", i, i, i, i);
84 		odbc_command(sql);
85 	}
86 
87 }
88 
89 int
main(int argc,char * argv[])90 main(int argc, char *argv[])
91 {
92 	odbc_use_version3 = 1;
93 	odbc_connect();
94 
95 	odbc_check_cursor();
96 
97 	Init();
98 
99 #define ALL(n) for (n = 0; n < 2; ++n)
100 	ALL(use_cursors)
101 		ALL(bind_all)
102 			ALL(normal_fetch)
103 				Test();
104 
105 	odbc_disconnect();
106 
107 	odbc_use_version3 = 0;
108 
109 	odbc_connect();
110 	Init();
111 
112 	ALL(use_cursors)
113 		ALL(bind_all)
114 			ALL(normal_fetch)
115 				Test();
116 
117 	odbc_disconnect();
118 
119 	return 0;
120 }
121