Line data Source code
1 : #include "common.h"
2 :
3 : /* Test cursors */
4 :
5 10 : TEST_MAIN()
6 : {
7 : #define ROWS 3
8 : #define C_LEN 10
9 :
10 : SQLUINTEGER n[ROWS];
11 : char c[ROWS][C_LEN];
12 : SQLLEN c_len[ROWS], n_len[ROWS];
13 :
14 : SQLUSMALLINT statuses[ROWS];
15 : SQLUSMALLINT i;
16 : SQLULEN num_row;
17 : int i_test;
18 :
19 : typedef struct _test
20 : {
21 : SQLUSMALLINT type;
22 : SQLINTEGER irow;
23 : int start;
24 : int num;
25 : } TEST;
26 :
27 : static const TEST tests[] = {
28 : {SQL_FETCH_NEXT, 0, 1, 3},
29 : {SQL_FETCH_NEXT, 0, 4, 2},
30 : {SQL_FETCH_PRIOR, 0, 1, 3},
31 : {SQL_FETCH_NEXT, 0, 4, 2},
32 : {SQL_FETCH_NEXT, 0, -1, -1},
33 : {SQL_FETCH_FIRST, 0, 1, 3},
34 : {SQL_FETCH_ABSOLUTE, 3, 3, 3},
35 : {SQL_FETCH_RELATIVE, 1, 4, 2},
36 : {SQL_FETCH_LAST, 0, 3, 3}
37 : };
38 10 : const int num_tests = TDS_VECTOR_SIZE(tests);
39 :
40 10 : odbc_use_version3 = 1;
41 :
42 10 : odbc_connect();
43 10 : odbc_check_cursor();
44 :
45 : /* create test table */
46 8 : odbc_command("IF OBJECT_ID('tempdb..#test') IS NOT NULL DROP TABLE #test");
47 8 : odbc_command("CREATE TABLE #test(i int, c varchar(6))");
48 8 : odbc_command("INSERT INTO #test(i, c) VALUES(1, 'a')");
49 8 : odbc_command("INSERT INTO #test(i, c) VALUES(2, 'bb')");
50 8 : odbc_command("INSERT INTO #test(i, c) VALUES(3, 'ccc')");
51 8 : odbc_command("INSERT INTO #test(i, c) VALUES(4, 'dddd')");
52 8 : odbc_command("INSERT INTO #test(i, c) VALUES(5, 'eeeee')");
53 :
54 : /* set cursor options */
55 8 : odbc_reset_statement();
56 8 : CHKSetStmtAttr(SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_ROWVER, 0, "S");
57 8 : CHKSetStmtAttr(SQL_ATTR_CURSOR_SCROLLABLE, (SQLPOINTER) SQL_SCROLLABLE, 0, "S");
58 8 : CHKSetStmtAttr(SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_DYNAMIC, 0, "S");
59 8 : CHKSetStmtAttr(SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) ROWS, 0, "S");
60 8 : CHKSetStmtAttr(SQL_ATTR_ROW_STATUS_PTR, (SQLPOINTER) statuses, 0, "S");
61 8 : CHKSetStmtAttr(SQL_ATTR_ROWS_FETCHED_PTR, &num_row, 0, "S");
62 :
63 : /* */
64 8 : CHKExecDirect(T("SELECT i, c FROM #test"), SQL_NTS, "S");
65 :
66 : /* bind some rows at a time */
67 8 : CHKBindCol(1, SQL_C_ULONG, n, 0, n_len, "S");
68 8 : CHKBindCol(2, SQL_C_CHAR, c, C_LEN, c_len, "S");
69 :
70 88 : for (i_test = 0; i_test < num_tests; ++i_test) {
71 72 : const TEST *t = &tests[i_test];
72 :
73 72 : printf("Test %d\n", i_test + 1);
74 :
75 72 : if (t->start == -1) {
76 8 : CHKFetchScroll(t->type, t->irow, "No");
77 : } else {
78 64 : CHKFetchScroll(t->type, t->irow, "S");
79 :
80 64 : if (t->start < 1) {
81 0 : fprintf(stderr, "Rows not expected\n");
82 0 : exit(1);
83 : }
84 :
85 : /* print, just for debug */
86 168 : for (i = 0; i < num_row; ++i)
87 168 : printf("row %d i %d c %s\n", (int) (i + 1), (int) n[i], c[i]);
88 64 : printf("---\n");
89 :
90 64 : if (num_row != t->num) {
91 0 : fprintf(stderr, "Expected %d rows, got %d\n", t->num, (int) num_row);
92 0 : exit(1);
93 : }
94 :
95 168 : for (i = 0; i < num_row; ++i) {
96 : char name[10];
97 :
98 168 : memset(name, 0, sizeof(name));
99 168 : memset(name, 'a' - 1 + i + t->start, i + t->start);
100 168 : if (n[i] != i + t->start || c_len[i] != strlen(name) || strcmp(c[i], name) != 0) {
101 0 : fprintf(stderr, "Wrong row returned\n");
102 0 : fprintf(stderr, "\tn %d %d\n", (int) n[i], i + t->start);
103 0 : fprintf(stderr, "\tc len %d %d\n", (int) c_len[i], (int) strlen(name));
104 0 : fprintf(stderr, "\tc %s %s\n", c[i], name);
105 0 : exit(1);
106 : }
107 : }
108 : }
109 : }
110 :
111 8 : odbc_reset_statement();
112 :
113 8 : odbc_disconnect();
114 8 : return 0;
115 : }
|