1 #include <config.h>
2
3 #include <stdio.h>
4
5 #ifdef HAVE_STRING_H
6 #include <string.h>
7 #endif
8
9 #include <ctpublic.h>
10 #include "common.h"
11
12 #include <common/test_assert.h>
13
14 int
main(int argc,char ** argv)15 main(int argc, char **argv)
16 {
17 CS_CONTEXT *ctx;
18 CS_CONNECTION *conn;
19 CS_COMMAND *cmd;
20 CS_COMMAND *cmd2;
21 CS_COMMAND *cmd3;
22 CS_RETCODE ret;
23 CS_RETCODE ret2;
24 CS_RETCODE results_ret;
25 CS_INT result_type;
26 CS_INT count, count2, row_count = 0;
27 CS_DATAFMT datafmt;
28 CS_DATAFMT datafmt2;
29 CS_SMALLINT ind;
30 int verbose = 1;
31 CS_CHAR *name = "c1";
32 CS_CHAR *name2 = "c2";
33 CS_CHAR col1[6];
34 CS_CHAR col2[6];
35 CS_INT datalength;
36 CS_CHAR text[128];
37 CS_INT num_cols, i;
38
39 memset(col1, 0, sizeof(col1));
40 memset(col2, 0, sizeof(col2));
41
42 fprintf(stdout, "%s: use multiple cursors on the same connection\n", __FILE__);
43
44 if (verbose) {
45 fprintf(stdout, "Trying login\n");
46 }
47 ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
48 if (ret != CS_SUCCEED) {
49 fprintf(stderr, "Login failed\n");
50 return 1;
51 }
52
53 ret = ct_cmd_alloc(conn, &cmd2);
54 if (ret != CS_SUCCEED) {
55 if (verbose) {
56 fprintf(stderr, "Command Alloc failed!\n");
57 }
58 return ret;
59 }
60
61 ret = ct_cmd_alloc(conn, &cmd3);
62 if (ret != CS_SUCCEED) {
63 if (verbose) {
64 fprintf(stderr, "Command Alloc failed!\n");
65 }
66 return ret;
67 }
68
69 ret = run_command(cmd, "CREATE TABLE #test_table (col1 char(4))");
70 if (ret != CS_SUCCEED)
71 return 1;
72
73 ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('AAA')");
74 if (ret != CS_SUCCEED)
75 return 1;
76 ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('BBB')");
77 if (ret != CS_SUCCEED)
78 return 1;
79 ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('CCC')");
80 if (ret != CS_SUCCEED)
81 return 1;
82
83 ret = run_command(cmd, "CREATE TABLE #test_table2 (col1 char(4))");
84 if (ret != CS_SUCCEED)
85 return 1;
86
87 ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('XXX')");
88 if (ret != CS_SUCCEED)
89 return 1;
90
91 ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('YYY')");
92 if (ret != CS_SUCCEED)
93 return 1;
94
95 ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('ZZZ')");
96
97
98 if (ret != CS_SUCCEED)
99 return 1;
100
101
102 if (verbose) {
103 fprintf(stdout, "opening first cursor on connection\n");
104 }
105
106 strcpy(text, "select col1 from #test_table order by col1");
107
108 ret = ct_cursor(cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);
109
110 if (ret != CS_SUCCEED) {
111 fprintf(stderr, "ct_cursor declare failed\n");
112 return 1;
113 }
114
115 ret = ct_cursor(cmd, CS_CURSOR_ROWS, name, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1);
116 if (ret != CS_SUCCEED) {
117 fprintf(stderr, "ct_cursor set cursor rows failed");
118 return 1;
119 }
120
121 ret = ct_cursor(cmd, CS_CURSOR_OPEN, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);
122
123 if (ret != CS_SUCCEED) {
124 fprintf(stderr, "ct_cursor open failed\n");
125 return 1;
126 }
127
128 ret = ct_send(cmd);
129
130 if (ret != CS_SUCCEED) {
131 fprintf(stderr, "ct_send failed\n");
132 return 1;
133 }
134
135 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
136 switch ((int) result_type) {
137
138 case CS_CMD_FAIL:
139 fprintf(stderr, "ct_results failed\n");
140 return 1;
141 case CS_CMD_SUCCEED:
142 case CS_CMD_DONE:
143 case CS_STATUS_RESULT:
144 break;
145
146 case CS_CURSOR_RESULT:
147 ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
148
149 if (ret != CS_SUCCEED) {
150 fprintf(stderr, "ct_res_info() failed");
151 return 1;
152 }
153
154 if (num_cols != 1) {
155 fprintf(stderr, "unexpected num of columns = %d \n", num_cols);
156 return 1;
157 }
158
159 for (i = 0; i < num_cols; i++) {
160
161 /* here we can finally test for the return status column */
162 ret = ct_describe(cmd, i + 1, &datafmt);
163
164 if (ret != CS_SUCCEED) {
165 fprintf(stderr, "ct_describe() failed for column %d\n", i);
166 return 1;
167 }
168
169 if (datafmt.status & CS_RETURN) {
170 fprintf(stdout, "ct_describe() column %d \n", i);
171 }
172
173 datafmt.datatype = CS_CHAR_TYPE;
174 datafmt.format = CS_FMT_NULLTERM;
175 datafmt.maxlength = 6;
176 datafmt.count = 1;
177 datafmt.locale = NULL;
178 ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind);
179 if (ret != CS_SUCCEED) {
180 fprintf(stderr, "ct_bind() failed\n");
181 return 1;
182 }
183
184 }
185 break;
186
187 case CS_COMPUTE_RESULT:
188 fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
189 return 1;
190 default:
191 fprintf(stderr, "ct_results() unexpected result_type.\n");
192 return 1;
193 }
194 }
195
196 if (verbose) {
197 fprintf(stdout, "opening second cursor on connection\n");
198 }
199
200 strcpy(text, "select col1 from #test_table2 order by col1");
201
202 ret = ct_cursor(cmd2, CS_CURSOR_DECLARE, name2, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);
203
204 if (ret != CS_SUCCEED) {
205 fprintf(stderr, "ct_cursor declare failed\n");
206 return 1;
207 }
208
209 ret = ct_cursor(cmd2, CS_CURSOR_ROWS, name2, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1);
210 if (ret != CS_SUCCEED) {
211 fprintf(stderr, "ct_cursor set cursor rows failed");
212 return 1;
213 }
214
215 ret = ct_cursor(cmd2, CS_CURSOR_OPEN, name2, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);
216
217 if (ret != CS_SUCCEED) {
218 fprintf(stderr, "ct_cursor open failed\n");
219 return 1;
220 }
221
222 ret = ct_send(cmd2);
223
224 if (ret != CS_SUCCEED) {
225 fprintf(stderr, "ct_send failed\n");
226 return 1;
227 }
228
229 while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
230 switch ((int) result_type) {
231
232 case CS_CMD_FAIL:
233 fprintf(stderr, "ct_results failed\n");
234 return 1;
235 case CS_CMD_SUCCEED:
236 case CS_CMD_DONE:
237 case CS_STATUS_RESULT:
238 break;
239
240 case CS_CURSOR_RESULT:
241 ret = ct_res_info(cmd2, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
242
243 if (ret != CS_SUCCEED) {
244 fprintf(stderr, "ct_res_info() failed");
245 return 1;
246 }
247
248 if (num_cols != 1) {
249 fprintf(stderr, "unexpected num of columns = %d \n", num_cols);
250 return 1;
251 }
252
253 for (i = 0; i < num_cols; i++) {
254
255 /* here we can finally test for the return status column */
256 ret = ct_describe(cmd2, i + 1, &datafmt2);
257
258 if (ret != CS_SUCCEED) {
259 fprintf(stderr, "ct_describe() failed for column %d\n", i);
260 return 1;
261 }
262
263 if (datafmt2.status & CS_RETURN) {
264 fprintf(stdout, "ct_describe() column %d \n", i);
265 }
266
267 datafmt2.datatype = CS_CHAR_TYPE;
268 datafmt2.format = CS_FMT_NULLTERM;
269 datafmt2.maxlength = 6;
270 datafmt2.count = 1;
271 datafmt2.locale = NULL;
272 ret = ct_bind(cmd2, 1, &datafmt2, col2, &datalength, &ind);
273 if (ret != CS_SUCCEED) {
274 fprintf(stderr, "ct_bind() failed\n");
275 return 1;
276 }
277
278 }
279 break;
280
281 case CS_COMPUTE_RESULT:
282 fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
283 return 1;
284 default:
285 fprintf(stderr, "ct_results() unexpected result_type.\n");
286 return 1;
287 }
288 }
289
290 row_count = 0;
291
292 while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
293 || (ret == CS_ROW_FAIL)) {
294
295 ret2 = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count2);
296 if (ret == CS_SUCCEED && ret2 == CS_SUCCEED) {
297 printf("%s\t\t\t%s\n", col1, col2);
298 }
299 }
300
301 switch ((int) ret) {
302 case CS_END_DATA:
303 break;
304 case CS_ROW_FAIL:
305 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
306 return 1;
307 case CS_FAIL:
308 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
309 return 1;
310 default:
311 fprintf(stderr, "ct_fetch() unexpected return.\n");
312 return 1;
313 }
314
315 if (verbose) {
316 fprintf(stdout, "closing first cursor on connection\n");
317 }
318
319 ret = ct_cursor(cmd, CS_CURSOR_CLOSE, name, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);
320
321 if (ret != CS_SUCCEED) {
322 fprintf(stderr, "ct_cursor(close) failed\n");
323 return ret;
324 }
325
326 if ((ret = ct_send(cmd)) != CS_SUCCEED) {
327 fprintf(stderr, "BILL ct_send() failed\n");
328 return ret;
329 }
330
331 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
332 if (result_type == CS_CMD_FAIL) {
333 fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
334 return 1;
335 }
336 }
337 if (results_ret != CS_END_RESULTS) {
338 fprintf(stderr, "ct_results() returned BAD.\n");
339 return 1;
340 }
341
342 ret = ct_cursor(cmd, CS_CURSOR_DEALLOC, name, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);
343
344 if (ret != CS_SUCCEED) {
345 fprintf(stderr, "ct_cursor(dealloc) failed\n");
346 return ret;
347 }
348
349 if ((ret = ct_send(cmd)) != CS_SUCCEED) {
350 fprintf(stderr, "ct_send() failed\n");
351 return ret;
352 }
353
354 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
355 if (result_type == CS_CMD_FAIL) {
356 fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n");
357 return 1;
358 }
359 }
360 if (results_ret != CS_END_RESULTS) {
361 fprintf(stderr, "ct_results() returned BAD.\n");
362 return 1;
363 }
364
365 if (verbose) {
366 fprintf(stdout, "closing second cursor on connection\n");
367 }
368
369 ret = ct_cursor(cmd2, CS_CURSOR_CLOSE, name2, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);
370
371 if (ret != CS_SUCCEED) {
372 fprintf(stderr, "ct_cursor(close) failed\n");
373 return ret;
374 }
375
376 if ((ret = ct_send(cmd2)) != CS_SUCCEED) {
377 fprintf(stderr, "BILL ct_send() failed\n");
378 return ret;
379 }
380
381 while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
382 if (result_type == CS_CMD_FAIL) {
383 fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
384 return 1;
385 }
386 }
387 if (results_ret != CS_END_RESULTS) {
388 fprintf(stderr, "ct_results() returned BAD.\n");
389 return 1;
390 }
391
392 ret = ct_cursor(cmd2, CS_CURSOR_DEALLOC, name2, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);
393
394 if (ret != CS_SUCCEED) {
395 fprintf(stderr, "ct_cursor(dealloc) failed\n");
396 return ret;
397 }
398
399 if ((ret = ct_send(cmd2)) != CS_SUCCEED) {
400 fprintf(stderr, "ct_send() failed\n");
401 return ret;
402 }
403
404 while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
405 if (result_type == CS_CMD_FAIL) {
406 fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n");
407 return 1;
408 }
409 }
410 if (results_ret != CS_END_RESULTS) {
411 fprintf(stderr, "ct_results() returned BAD.\n");
412 return 1;
413 }
414
415 ct_cmd_drop(cmd2);
416 ct_cmd_drop(cmd3);
417
418 if (verbose) {
419 fprintf(stdout, "Trying logout\n");
420 }
421 ret = try_ctlogout(ctx, conn, cmd, verbose);
422 if (ret != CS_SUCCEED) {
423 fprintf(stderr, "Logout failed\n");
424 return 2;
425 }
426
427 if (verbose) {
428 fprintf(stdout, "Test succeded\n");
429 }
430 return 0;
431 }
432