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