1 : /*
2 : * Purpose: Test data retrieval accuracy and cancelling results
3 : * Functions: dbbind dbcancel dbcmd dbcolname dbnextrow dbnumcols dbresults dbsqlexec
4 : */
5 :
6 : #if HAVE_CONFIG_H
7 : #include <config.h>
8 : #endif /* HAVE_CONFIG_H */
9 :
10 : #include <stdio.h>
11 :
12 : #if HAVE_STDLIB_H
13 : #include <stdlib.h>
14 : #endif /* HAVE_STDLIB_H */
15 :
16 : #if HAVE_STRING_H
17 : #include <string.h>
18 : #endif /* HAVE_STRING_H */
19 :
20 : #include <sqlfront.h>
21 : #include <sqldb.h>
22 :
23 : #include "common.h"
24 :
25 : static char software_version[] = "$Id: t0005.c,v 1.18 2005/05/23 08:06:25 freddy77 Exp $";
26 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
27 :
28 : int
29 : main(int argc, char **argv)
30 2 : {
31 2 : const int rows_to_add = 50;
32 : LOGINREC *login;
33 : DBPROCESS *dbproc;
34 : int i;
35 : char teststr[1024];
36 : DBINT testint;
37 : char cmd[1024];
38 2 : int failed = 0;
39 :
40 2 : set_malloc_options();
41 :
42 2 : read_login_info(argc, argv);
43 2 : fprintf(stdout, "Start\n");
44 2 : add_bread_crumb();
45 :
46 :
47 2 : dbinit();
48 :
49 2 : add_bread_crumb();
50 2 : dberrhandle(syb_err_handler);
51 2 : dbmsghandle(syb_msg_handler);
52 :
53 2 : fprintf(stdout, "About to logon\n");
54 :
55 2 : add_bread_crumb();
56 2 : login = dblogin();
57 2 : DBSETLPWD(login, PASSWORD);
58 2 : DBSETLUSER(login, USER);
59 2 : DBSETLAPP(login, "t0005");
60 2 : DBSETLHOST(login, "ntbox.dntis.ro");
61 :
62 2 : fprintf(stdout, "About to open\n");
63 :
64 2 : add_bread_crumb();
65 2 : dbproc = dbopen(login, SERVER);
66 2 : if (strlen(DATABASE))
67 2 : dbuse(dbproc, DATABASE);
68 2 : add_bread_crumb();
69 2 : dbloginfree(login);
70 2 : add_bread_crumb();
71 :
72 2 : add_bread_crumb();
73 :
74 2 : fprintf(stdout, "creating table\n");
75 2 : if (SUCCEED != dbcmd(dbproc, "create table #dblib0005 " "(i int not null, s char(10) not null)")) {
76 0 : fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
77 0 : failed = 1;
78 : }
79 :
80 2 : if (SUCCEED != dbsqlexec(dbproc)) {
81 0 : fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
82 0 : failed = 1;
83 : }
84 :
85 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
86 : /* nop */
87 : }
88 :
89 2 : fprintf(stdout, "insert\n");
90 100 : for (i = 1; i < rows_to_add; i++) {
91 98 : sprintf(cmd, "insert into #dblib0005 values (%d, 'row %04d')", i, i);
92 98 : dbcmd(dbproc, cmd);
93 98 : dbsqlexec(dbproc);
94 196 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
95 : /* nop */
96 : }
97 : }
98 :
99 :
100 2 : sprintf(cmd, "select * from #dblib0005 where i < 5 order by i");
101 2 : fprintf(stdout, "%s\n", cmd);
102 2 : dbcmd(dbproc, cmd);
103 2 : dbsqlexec(dbproc);
104 2 : add_bread_crumb();
105 :
106 :
107 2 : if (dbresults(dbproc) != SUCCEED) {
108 0 : add_bread_crumb();
109 0 : fprintf(stdout, "Was expecting a result set.");
110 0 : exit(1);
111 : }
112 2 : add_bread_crumb();
113 :
114 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
115 4 : add_bread_crumb();
116 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
117 4 : add_bread_crumb();
118 : }
119 :
120 2 : add_bread_crumb();
121 2 : dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
122 2 : add_bread_crumb();
123 2 : dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
124 2 : add_bread_crumb();
125 :
126 2 : add_bread_crumb();
127 :
128 10 : for (i = 1; i < 5; i++) {
129 : char expected[1024];
130 :
131 8 : sprintf(expected, "row %04d", i);
132 :
133 8 : add_bread_crumb();
134 :
135 :
136 8 : testint = -1;
137 8 : strcpy(teststr, "bogus");
138 :
139 8 : add_bread_crumb();
140 8 : if (REG_ROW != dbnextrow(dbproc)) {
141 0 : fprintf(stderr, "Failed. Expected a row\n");
142 0 : exit(1);
143 : }
144 8 : add_bread_crumb();
145 8 : if (testint != i) {
146 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
147 0 : abort();
148 : }
149 8 : if (0 != strncmp(teststr, expected, strlen(expected))) {
150 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
151 0 : abort();
152 : }
153 8 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
154 : }
155 :
156 2 : fprintf(stdout, "This query should succeeded as we have fetched exactly the.\n");
157 2 : fprintf(stdout, "number of rows in the result set\n");
158 :
159 2 : sprintf(cmd, "select * from #dblib0005 where i < 6 order by i");
160 2 : fprintf(stdout, "%s\n", cmd);
161 2 : if (SUCCEED != dbcmd(dbproc, cmd)) {
162 0 : fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
163 0 : failed = 1;
164 : }
165 2 : if (SUCCEED != dbsqlexec(dbproc)) {
166 0 : fprintf(stderr, "%s:%d: dbsqlexec should have succeeded but didn't\n", __FILE__, __LINE__);
167 0 : failed = 1;
168 : }
169 2 : add_bread_crumb();
170 :
171 2 : if (dbresults(dbproc) != SUCCEED) {
172 0 : add_bread_crumb();
173 0 : fprintf(stdout, "Was expecting a result set.");
174 0 : exit(1);
175 : }
176 2 : add_bread_crumb();
177 :
178 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
179 4 : add_bread_crumb();
180 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
181 4 : add_bread_crumb();
182 : }
183 :
184 2 : add_bread_crumb();
185 2 : dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
186 2 : add_bread_crumb();
187 2 : dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
188 2 : add_bread_crumb();
189 :
190 2 : add_bread_crumb();
191 :
192 10 : for (i = 1; i < 5; i++) {
193 : char expected[1024];
194 :
195 8 : sprintf(expected, "row %04d", i);
196 :
197 8 : add_bread_crumb();
198 :
199 :
200 8 : testint = -1;
201 8 : strcpy(teststr, "bogus");
202 :
203 8 : add_bread_crumb();
204 8 : if (REG_ROW != dbnextrow(dbproc)) {
205 0 : fprintf(stderr, "Failed. Expected a row\n");
206 0 : exit(1);
207 : }
208 8 : add_bread_crumb();
209 8 : if (testint != i) {
210 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
211 0 : abort();
212 : }
213 8 : if (0 != strncmp(teststr, expected, strlen(expected))) {
214 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
215 0 : abort();
216 : }
217 8 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
218 : }
219 :
220 2 : fprintf(stdout, "This query should fail as we have not fetched all the\n");
221 2 : fprintf(stdout, "rows in the result set\n");
222 :
223 2 : sprintf(cmd, "select * from #dblib0005 where i > 950 order by i");
224 2 : fprintf(stdout, "%s\n", cmd);
225 2 : if (SUCCEED != dbcmd(dbproc, cmd)) {
226 0 : fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
227 0 : failed = 1;
228 : }
229 2 : if (SUCCEED == dbsqlexec(dbproc)) {
230 0 : fprintf(stderr, "%s:%d: dbsqlexec should have failed but didn't\n", __FILE__, __LINE__);
231 0 : failed = 1;
232 : }
233 :
234 :
235 2 : fprintf(stdout, "calling dbcancel to flush results\n");
236 2 : dbcancel(dbproc);
237 :
238 2 : fprintf(stdout, "Dropping proc\n");
239 2 : add_bread_crumb();
240 2 : sprintf(cmd, "drop procedure t0005_proc");
241 2 : dbcmd(dbproc, cmd);
242 2 : add_bread_crumb();
243 2 : dbsqlexec(dbproc);
244 2 : add_bread_crumb();
245 2 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
246 : /* nop */
247 : }
248 2 : add_bread_crumb();
249 :
250 2 : fprintf(stdout, "creating proc\n");
251 2 : sprintf(cmd, "create proc t0005_proc (@b int out) as\nbegin\n"
252 : "select * from #dblib0005 where i < 6 order by i\n" "select @b = 42\n" "end\n");
253 :
254 2 : fprintf(stdout, "%s\n", cmd);
255 :
256 2 : dbcmd(dbproc, cmd);
257 2 : if (dbsqlexec(dbproc) == FAIL) {
258 0 : add_bread_crumb();
259 0 : fprintf(stderr, "%s:%d: failed to create procedure\n", __FILE__, __LINE__);
260 0 : failed = 1;
261 : }
262 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
263 : /* nop */
264 : }
265 :
266 2 : fprintf(stdout, "calling proc\n");
267 2 : sprintf(cmd, "declare @myout int exec t0005_proc @b = @myout output");
268 2 : fprintf(stdout, "%s\n", cmd);
269 :
270 2 : dbcmd(dbproc, cmd);
271 2 : if (dbsqlexec(dbproc) == FAIL) {
272 0 : add_bread_crumb();
273 0 : fprintf(stderr, "%s:%d: failed to call procedure\n", __FILE__, __LINE__);
274 0 : failed = 1;
275 : }
276 2 : if (dbresults(dbproc) != SUCCEED) {
277 0 : add_bread_crumb();
278 0 : fprintf(stdout, "Was expecting a result set.");
279 0 : exit(1);
280 : }
281 2 : add_bread_crumb();
282 :
283 6 : for (i = 1; i <= dbnumcols(dbproc); i++) {
284 4 : add_bread_crumb();
285 4 : printf("col %d is %s\n", i, dbcolname(dbproc, i));
286 4 : add_bread_crumb();
287 : }
288 :
289 2 : add_bread_crumb();
290 2 : dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
291 2 : add_bread_crumb();
292 2 : dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
293 2 : add_bread_crumb();
294 :
295 12 : for (i = 1; i < 6; i++) {
296 : char expected[1024];
297 :
298 10 : sprintf(expected, "row %04d", i);
299 :
300 10 : add_bread_crumb();
301 :
302 :
303 10 : testint = -1;
304 10 : strcpy(teststr, "bogus");
305 :
306 10 : add_bread_crumb();
307 10 : if (REG_ROW != dbnextrow(dbproc)) {
308 0 : fprintf(stderr, "Failed. Expected a row\n");
309 0 : exit(1);
310 : }
311 10 : add_bread_crumb();
312 10 : if (testint != i) {
313 0 : fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
314 0 : abort();
315 : }
316 10 : if (0 != strncmp(teststr, expected, strlen(expected))) {
317 0 : fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
318 0 : abort();
319 : }
320 10 : printf("Read a row of data -> %d %s\n", (int) testint, teststr);
321 : }
322 2 : add_bread_crumb();
323 :
324 2 : fprintf(stdout, "This next command should succeed as we have fetched exactly the.\n");
325 2 : fprintf(stdout, "number of rows in the result set\n");
326 :
327 2 : sprintf(cmd, "select getdate()");
328 2 : fprintf(stdout, "%s\n", cmd);
329 2 : if (SUCCEED != dbcmd(dbproc, cmd)) {
330 0 : fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
331 0 : failed = 1;
332 : }
333 2 : if (SUCCEED != dbsqlexec(dbproc)) {
334 0 : fprintf(stderr, "%s:%d: dbsqlexec should have succeeded but didn't\n", __FILE__, __LINE__);
335 0 : failed = 1;
336 : }
337 2 : add_bread_crumb();
338 :
339 2 : dbcancel(dbproc);
340 :
341 2 : dbcmd(dbproc, "drop procedure t0005_proc");
342 2 : dbsqlexec(dbproc);
343 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
344 : /* nop */
345 : }
346 :
347 2 : dbexit();
348 2 : add_bread_crumb();
349 :
350 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
351 2 : free_bread_crumb();
352 2 : return failed ? 1 : 0;
353 : }
|