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 : #if HAVE_CONFIG_H
11 : #include <config.h>
12 : #endif /* HAVE_CONFIG_H */
13 :
14 : #include <assert.h>
15 : #include <stdio.h>
16 :
17 : #if HAVE_STDLIB_H
18 : #include <stdlib.h>
19 : #endif /* HAVE_STDLIB_H */
20 :
21 : #if HAVE_STRING_H
22 : #include <string.h>
23 : #endif /* HAVE_STRING_H */
24 :
25 : #include <sqlfront.h>
26 : #include <sqldb.h>
27 :
28 : #include "common.h"
29 :
30 : static char software_version[] = "$Id: t0002.c,v 1.19 2005/04/16 23:57:39 jklowden Exp $";
31 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
32 :
33 : int failed = 0;
34 :
35 : static void
36 : verify(int i, int testint, char *teststr)
37 190 : {
38 : char expected[1024];
39 :
40 190 : sprintf(expected, "row %03d", i);
41 :
42 190 : if (testint != i) {
43 0 : failed = 1;
44 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, testint);
45 0 : abort();
46 : }
47 190 : if (0 != strncmp(teststr, expected, strlen(expected))) {
48 0 : failed = 1;
49 0 : fprintf(stderr, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
50 0 : abort();
51 : }
52 190 : printf("Read a row of data -> %d %s\n", testint, teststr);
53 190 : }
54 :
55 : int
56 : main(int argc, char **argv)
57 2 : {
58 : LOGINREC *login;
59 : DBPROCESS *dbproc;
60 : DBINT testint;
61 : STATUS rc;
62 : int i, iresults;
63 : char teststr[1024];
64 :
65 2 : const int buffer_count = 10;
66 2 : const int rows_to_add = 50;
67 2 : const char tablename[] = "#dblib0002";
68 : const char drop_if_exists[] = "if exists ( select 1 "
69 : "from tempdb..sysobjects "
70 : "where id = object_id('tempdb..%s') )\n"
71 2 : "\tdrop table %s\n";
72 :
73 2 : set_malloc_options();
74 :
75 2 : read_login_info(argc, argv);
76 :
77 2 : fprintf(stdout, "Start\n");
78 2 : add_bread_crumb();
79 :
80 : /* Fortify_EnterScope(); */
81 2 : dbinit();
82 :
83 2 : add_bread_crumb();
84 2 : dberrhandle(syb_err_handler);
85 2 : dbmsghandle(syb_msg_handler);
86 :
87 2 : fprintf(stdout, "About to logon\n");
88 :
89 2 : add_bread_crumb();
90 2 : login = dblogin();
91 2 : DBSETLPWD(login, PASSWORD);
92 2 : DBSETLUSER(login, USER);
93 2 : DBSETLAPP(login, "t0002");
94 :
95 2 : fprintf(stdout, "About to open %s..%s\n", SERVER, DATABASE);
96 :
97 2 : add_bread_crumb();
98 2 : dbproc = dbopen(login, SERVER);
99 2 : if (strlen(DATABASE))
100 2 : dbuse(dbproc, DATABASE);
101 2 : add_bread_crumb();
102 2 : dbloginfree(login);
103 2 : add_bread_crumb();
104 :
105 2 : fprintf(stdout, "Setting row buffer to 10 rows\n");
106 2 : dbsetopt(dbproc, DBBUFFER, "10", 0);
107 2 : add_bread_crumb();
108 :
109 2 : add_bread_crumb();
110 2 : fprintf(stdout, drop_if_exists, tablename, tablename);
111 2 : dbfcmd(dbproc, drop_if_exists, tablename, tablename);
112 2 : add_bread_crumb();
113 2 : dbsqlexec(dbproc);
114 2 : add_bread_crumb();
115 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
116 : /* nop */
117 : }
118 2 : if (dbresults(dbproc) != NO_MORE_RESULTS) {
119 0 : fprintf(stdout, "Failed: dbresults call after NO_MORE_RESULTS should return NO_MORE_RESULTS.\n");
120 0 : failed = 1;
121 : }
122 2 : add_bread_crumb();
123 :
124 2 : fprintf(stdout, "create table %s (i int not null, s char(10) not null)\n", tablename);
125 2 : dbfcmd(dbproc, "create table %s (i int not null, s char(10) not null)", tablename);
126 2 : dbsqlexec(dbproc);
127 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
128 : /* nop */
129 : }
130 :
131 2 : fprintf(stdout, "insert into %s [%d rows]\n", tablename, rows_to_add);
132 102 : for (i = 1; i <= rows_to_add; i++) {
133 : char cmd[1024];
134 :
135 100 : sprintf(cmd, "insert into %s values (%d, 'row %03d')", tablename, i, i);
136 100 : dbcmd(dbproc, cmd);
137 100 : dbsqlexec(dbproc);
138 200 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
139 : /* nop */
140 : }
141 : }
142 :
143 2 : fprintf(stdout, "select * from %s order by i\n", tablename);
144 2 : dbfcmd(dbproc, "select * from %s order by i\n", tablename);
145 2 : dbfcmd(dbproc, "select * from %s order by i\n", tablename); /* two result sets */
146 2 : dbsqlexec(dbproc);
147 2 : add_bread_crumb();
148 :
149 :
150 6 : for (iresults=1; iresults <= 2; iresults++ ) {
151 4 : fprintf(stdout, "fetching resultset %i\n", iresults);
152 4 : if (dbresults(dbproc) != SUCCEED) {
153 0 : add_bread_crumb();
154 0 : fprintf(stderr, "Was expecting a result set %d.\n", iresults);
155 0 : if( iresults == 2 )
156 0 : fprintf(stderr, "Buffering with multiple resultsets is broken.\n");
157 0 : exit(1);
158 : }
159 4 : add_bread_crumb();
160 :
161 12 : for (i = 1; i <= dbnumcols(dbproc); i++) {
162 8 : add_bread_crumb();
163 8 : printf("col %d is [%s]\n", i, dbcolname(dbproc, i));
164 8 : add_bread_crumb();
165 : }
166 :
167 4 : add_bread_crumb();
168 4 : dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
169 4 : add_bread_crumb();
170 4 : dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
171 4 : add_bread_crumb();
172 :
173 : /* Fetch a result set */
174 : /* Second resultset stops at row 40 */
175 26 : for (i=0; i < rows_to_add - (iresults == 2 ? buffer_count : 0);) {
176 :
177 18 : fprintf(stdout, "clearing %d rows from buffer\n", buffer_count);
178 18 : dbclrbuf(dbproc, buffer_count);
179 :
180 : do {
181 : int rc;
182 :
183 180 : i++;
184 180 : add_bread_crumb();
185 180 : if (REG_ROW != (rc = dbnextrow(dbproc))) {
186 0 : failed = 1;
187 0 : fprintf(stderr, "Failed: Expected a row (%s:%d)\n", __FILE__, __LINE__);
188 0 : if (rc == BUF_FULL)
189 0 : fprintf(stderr, "Failed: dbnextrow returned BUF_FULL (%d). Fix dbclrbuf.\n", rc);
190 0 : exit(1);
191 : }
192 180 : add_bread_crumb();
193 180 : verify(i, testint, teststr);
194 180 : } while (i % buffer_count);
195 :
196 18 : if (iresults == 1 && i == rows_to_add) {
197 : /* The buffer should be full */
198 2 : assert(BUF_FULL == dbnextrow(dbproc));
199 : }
200 :
201 : }
202 4 : if (iresults == 1) {
203 2 : fprintf(stdout, "clearing %d rows from buffer\n", buffer_count);
204 2 : dbclrbuf(dbproc, buffer_count);
205 2 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
206 0 : abort(); /* All rows were read: should not enter loop */
207 : }
208 : }
209 : }
210 :
211 : /*
212 : * Now test the buffered rows.
213 : * Should be operating on rows 31-40 of 2nd resultset
214 : */
215 2 : rc = dbgetrow(dbproc, 1);
216 2 : add_bread_crumb();
217 2 : if(rc != NO_MORE_ROWS) /* row 1 is not among the 31-40 in the buffer */
218 0 : fprintf(stderr, "Failed: dbgetrow returned %d.\n", rc);
219 2 : assert(rc == NO_MORE_ROWS);
220 :
221 2 : rc = dbgetrow(dbproc, 31);
222 2 : add_bread_crumb();
223 2 : if(rc != REG_ROW)
224 0 : fprintf(stderr, "Failed: dbgetrow returned %d.\n", rc);
225 2 : assert(rc == REG_ROW);
226 2 : verify(31, testint, teststr); /* first buffered row should be 31 */
227 :
228 2 : rc = dbnextrow(dbproc);
229 2 : add_bread_crumb();
230 2 : if(rc != REG_ROW)
231 0 : fprintf(stderr, "Failed: dbgetrow returned %d.\n", rc);
232 2 : assert(rc == REG_ROW);
233 2 : verify(32, testint, teststr); /* next buffered row should be 32 */
234 :
235 2 : rc = dbgetrow(dbproc, 11);
236 2 : add_bread_crumb();
237 2 : assert(rc == NO_MORE_ROWS); /* only 10 (not 11) rows buffered */
238 :
239 2 : rc = dbgetrow(dbproc, 40);
240 2 : add_bread_crumb();
241 2 : assert(rc == REG_ROW);
242 2 : verify(40, testint, teststr); /* last buffered row should be 40 */
243 :
244 : /* Attempt dbnextrow when buffer has no space (10 out of 10 in use). */
245 2 : rc = dbnextrow(dbproc);
246 2 : assert(rc == BUF_FULL);
247 :
248 2 : dbclrbuf(dbproc, 3); /* remove rows 31, 32, and 33 */
249 :
250 2 : rc = dbnextrow(dbproc);
251 2 : add_bread_crumb();
252 2 : assert(rc == REG_ROW);
253 2 : verify(41, testint, teststr); /* fetch row from database, should be 41 */
254 :
255 2 : rc = dbnextrow(dbproc);
256 2 : add_bread_crumb();
257 2 : assert(rc == REG_ROW);
258 2 : verify(42, testint, teststr); /* fetch row from database, should be 42 */
259 :
260 : /* buffer contains 9 rows (34-42) try removing 10 rows */
261 2 : dbclrbuf(dbproc, buffer_count);
262 :
263 18 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
264 : /* waste rows 43-50 */
265 : }
266 :
267 2 : dbclose(dbproc); /* close while buffer not cleared: OK */
268 :
269 2 : add_bread_crumb();
270 2 : dbexit();
271 2 : add_bread_crumb();
272 :
273 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
274 2 : free_bread_crumb();
275 2 : return failed ? 1 : 0;
276 : }
|