Line data Source code
1 : /*
2 : * Purpose: Test buffering
3 : * Functions: dbclrbuf dbgetrow dbsetopt
4 : */
5 : #if 0
6 : # Find functions with:
7 : sed -ne'/db/ s/.*\(db[[:alnum:]_]*\)(.*/\1/gp' src/dblib/unittests/t0002.c |sort -u |fmt
8 : #endif
9 :
10 : #include "common.h"
11 : #include <assert.h>
12 :
13 : int failed = 0;
14 :
15 : static void
16 808 : verify(int i, int testint, char *teststr)
17 : {
18 : char expected[1024];
19 :
20 808 : sprintf(expected, "row %03d", i);
21 :
22 808 : if (testint != i) {
23 0 : failed = 1;
24 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, testint);
25 0 : abort();
26 : }
27 808 : if (0 != strncmp(teststr, expected, strlen(expected))) {
28 0 : failed = 1;
29 0 : fprintf(stderr, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
30 0 : abort();
31 : }
32 808 : printf("Read a row of data -> %d %s\n", testint, teststr);
33 808 : }
34 :
35 : int
36 8 : main(int argc, char **argv)
37 : {
38 : LOGINREC *login;
39 : DBPROCESS *dbproc;
40 : DBINT testint;
41 : STATUS rc;
42 : int i, iresults;
43 : char teststr[1024];
44 : int rows_in_buffer, limit_rows;
45 :
46 8 : const int buffer_count = 10;
47 8 : const int rows_to_add = 50;
48 :
49 8 : set_malloc_options();
50 :
51 8 : read_login_info(argc, argv);
52 :
53 8 : printf("Starting %s\n", argv[0]);
54 :
55 : /* Fortify_EnterScope(); */
56 8 : dbinit();
57 :
58 8 : dberrhandle(syb_err_handler);
59 8 : dbmsghandle(syb_msg_handler);
60 :
61 8 : printf("About to logon\n");
62 :
63 8 : login = dblogin();
64 8 : DBSETLPWD(login, PASSWORD);
65 8 : DBSETLUSER(login, USER);
66 8 : DBSETLAPP(login, "t0002");
67 :
68 8 : printf("About to open %s..%s\n", SERVER, DATABASE);
69 :
70 8 : dbproc = dbopen(login, SERVER);
71 8 : if (strlen(DATABASE))
72 8 : dbuse(dbproc, DATABASE);
73 8 : dbloginfree(login);
74 :
75 8 : printf("Setting row buffer to 10 rows\n");
76 : #ifdef MICROSOFT_DBLIB
77 : dbsetopt(dbproc, DBBUFFER, "10");
78 : #else
79 8 : dbsetopt(dbproc, DBBUFFER, "10", 0);
80 : #endif
81 :
82 8 : sql_cmd(dbproc); /* drop table if exists */
83 8 : dbsqlexec(dbproc);
84 8 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
85 : /* nop */
86 : }
87 8 : if (dbresults(dbproc) != NO_MORE_RESULTS) {
88 0 : printf("Failed: dbresults call after NO_MORE_RESULTS should return NO_MORE_RESULTS.\n");
89 0 : failed = 1;
90 : }
91 :
92 8 : sql_cmd(dbproc); /* create table */
93 8 : dbsqlexec(dbproc);
94 8 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
95 : /* nop */
96 : }
97 :
98 400 : for (i = 1; i <= rows_to_add; i++) {
99 400 : sql_cmd(dbproc);
100 400 : dbsqlexec(dbproc);
101 400 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
102 : /* nop */
103 : }
104 : }
105 :
106 8 : sql_cmd(dbproc); /* two result sets */
107 8 : dbsqlexec(dbproc);
108 :
109 24 : for (iresults=1; iresults <= 2; iresults++ ) {
110 16 : printf("fetching resultset %i\n", iresults);
111 16 : if (dbresults(dbproc) != SUCCEED) {
112 0 : fprintf(stderr, "Was expecting a result set %d.\n", iresults);
113 0 : if( iresults == 2 )
114 0 : fprintf(stderr, "Buffering with multiple resultsets is broken.\n");
115 0 : exit(1);
116 : }
117 : rows_in_buffer = 0;
118 :
119 32 : for (i = 1; i <= dbnumcols(dbproc); i++)
120 32 : printf("col %d is [%s]\n", i, dbcolname(dbproc, i));
121 :
122 16 : dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
123 16 : dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
124 :
125 : /* Fetch a result set */
126 : /* Second resultset stops at row 46 */
127 16 : limit_rows = rows_to_add - (iresults == 2 ? 4 : 0);
128 120 : for (i=0; i < limit_rows;) {
129 :
130 88 : printf("clearing %d rows from buffer\n", rows_in_buffer ? buffer_count - 1 : buffer_count);
131 : #ifdef MICROSOFT_DBLIB
132 : if (i == 0) {
133 : rc = dbnextrow(dbproc);
134 : assert(rc == REG_ROW);
135 : ++i;
136 : rows_in_buffer = 1;
137 : }
138 : #endif
139 88 : dbclrbuf(dbproc, buffer_count);
140 88 : rows_in_buffer = rows_in_buffer ? 1 : 0;
141 :
142 : do {
143 : int rc;
144 :
145 768 : i++;
146 768 : if (REG_ROW != (rc = dbnextrow(dbproc))) {
147 0 : failed = 1;
148 0 : fprintf(stderr, "Failed: Expected a row (%s:%d)\n", __FILE__, __LINE__);
149 0 : if (rc == BUF_FULL)
150 0 : fprintf(stderr, "Failed: dbnextrow returned BUF_FULL (%d). Fix dbclrbuf.\n", rc);
151 0 : exit(1);
152 : }
153 768 : ++rows_in_buffer;
154 768 : verify(i, testint, teststr);
155 768 : } while (rows_in_buffer < buffer_count && i < limit_rows);
156 :
157 88 : if (rows_in_buffer == buffer_count) {
158 : /* The buffer should be full */
159 80 : assert(BUF_FULL == dbnextrow(dbproc));
160 : }
161 : }
162 16 : if (iresults == 1) {
163 8 : printf("clearing %d rows from buffer\n", buffer_count);
164 8 : dbclrbuf(dbproc, buffer_count);
165 8 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
166 0 : abort(); /* All rows were read: should not enter loop */
167 : }
168 : }
169 : }
170 8 : printf("\n");
171 :
172 : /*
173 : * Now test the buffered rows.
174 : * Should be operating on rows 37-46 of 2nd resultset
175 : */
176 8 : rc = dbgetrow(dbproc, 1);
177 8 : if(rc != NO_MORE_ROWS) /* row 1 is not among the 31-40 in the buffer */
178 0 : fprintf(stderr, "Failed: dbgetrow returned %d.\n", rc);
179 8 : assert(rc == NO_MORE_ROWS);
180 :
181 8 : rc = dbgetrow(dbproc, 37);
182 8 : if(rc != REG_ROW)
183 0 : fprintf(stderr, "Failed: dbgetrow returned %d.\n", rc);
184 8 : assert(rc == REG_ROW);
185 8 : verify(37, testint, teststr); /* first buffered row should be 37 */
186 :
187 8 : rc = dbnextrow(dbproc);
188 8 : if(rc != REG_ROW)
189 0 : fprintf(stderr, "Failed: dbgetrow returned %d.\n", rc);
190 8 : assert(rc == REG_ROW);
191 8 : verify(38, testint, teststr); /* next buffered row should be 38 */
192 :
193 8 : rc = dbgetrow(dbproc, 11);
194 8 : assert(rc == NO_MORE_ROWS); /* only 10 (not 11) rows buffered */
195 :
196 8 : rc = dbgetrow(dbproc, 46);
197 8 : assert(rc == REG_ROW);
198 8 : verify(46, testint, teststr); /* last buffered row should be 46 */
199 :
200 : /* Attempt dbnextrow when buffer has no space (10 out of 10 in use). */
201 8 : rc = dbnextrow(dbproc);
202 8 : assert(rc == BUF_FULL);
203 :
204 8 : dbclrbuf(dbproc, 3); /* remove rows 37, 38, and 39 */
205 :
206 8 : rc = dbnextrow(dbproc);
207 8 : assert(rc == REG_ROW);
208 8 : verify(47, testint, teststr); /* fetch row from database, should be 47 */
209 :
210 8 : rc = dbnextrow(dbproc);
211 8 : assert(rc == REG_ROW);
212 8 : verify(48, testint, teststr); /* fetch row from database, should be 48 */
213 :
214 : /* buffer contains 8 rows (40-47) try removing 10 rows */
215 8 : dbclrbuf(dbproc, buffer_count);
216 :
217 8 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
218 : /* waste rows 49-50 */
219 : }
220 :
221 8 : dbclose(dbproc); /* close while buffer not cleared: OK */
222 :
223 8 : dbexit();
224 :
225 8 : printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
226 8 : return failed ? 1 : 0;
227 : }
|