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 static char software_version[] = "$Id: ct_cursor.c 487476 2015-12-17 19:48:39Z ucko $";
15 static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
16
17 static int update_second_table(CS_COMMAND * cmd2, char *value);
18
19 int
main(int argc,char ** argv)20 main(int argc, char **argv)
21 {
22 CS_CONTEXT *ctx;
23 CS_CONNECTION *conn;
24 CS_COMMAND *cmd;
25 CS_COMMAND *cmd2;
26 CS_RETCODE ret;
27 CS_RETCODE results_ret;
28 CS_INT result_type;
29 CS_INT count, row_count = 0;
30 CS_DATAFMT datafmt;
31 CS_SMALLINT ind;
32 int verbose = 1;
33 CS_CHAR name[3];
34 CS_CHAR col1[6];
35 CS_INT datalength;
36 CS_CHAR text[128];
37 CS_INT num_cols, i, j;
38 CS_INT props_value;
39
40 fprintf(stdout, "%s: Testing ct_cursor()\n", __FILE__);
41
42 if (verbose) {
43 fprintf(stdout, "Trying login\n");
44 }
45 ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
46 if (ret != CS_SUCCEED) {
47 fprintf(stderr, "Login failed\n");
48 return 1;
49 }
50
51 ret = ct_cmd_alloc(conn, &cmd2);
52 if (ret != CS_SUCCEED) {
53 if (verbose) {
54 fprintf(stderr, "Command Alloc failed!\n");
55 }
56 return ret;
57 }
58
59 ret = run_command(cmd, "CREATE TABLE #test_table (col1 char(4))");
60 if (ret != CS_SUCCEED)
61 return 1;
62
63 ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('AAA')");
64 if (ret != CS_SUCCEED)
65 return 1;
66 ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('BBB')");
67 if (ret != CS_SUCCEED)
68 return 1;
69 ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('CCC')");
70 if (ret != CS_SUCCEED)
71 return 1;
72
73 ret = run_command(cmd2, "CREATE TABLE #test_table2 (col1 char(4))");
74 if (ret != CS_SUCCEED)
75 return 1;
76
77 ret = run_command(cmd2, "INSERT #test_table2 (col1) VALUES ('---')");
78 if (ret != CS_SUCCEED)
79 return 1;
80
81 if (verbose) {
82 fprintf(stdout, "Trying declare, rows , open in one SEND\n");
83 }
84
85 strcpy(text, "select col1 from #test_table where 1 = 1");
86 strcpy(name, "c1");
87
88 ret = ct_cursor(cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);
89
90 if (ret != CS_SUCCEED) {
91 fprintf(stderr, "ct_cursor declare failed\n");
92 return 1;
93 }
94
95 ret = ct_cursor(cmd, CS_CURSOR_ROWS, name, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1);
96 if (ret != CS_SUCCEED) {
97 fprintf(stderr, "ct_cursor set cursor rows failed");
98 return 1;
99 }
100
101 ret = ct_cursor(cmd, CS_CURSOR_OPEN, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);
102
103 if (ret != CS_SUCCEED) {
104 fprintf(stderr, "ct_cursor open failed\n");
105 return 1;
106 }
107
108 ret = ct_send(cmd);
109
110 if (ret != CS_SUCCEED) {
111 fprintf(stderr, "ct_send failed\n");
112 }
113
114 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
115 switch ((int) result_type) {
116
117 case CS_CMD_SUCCEED:
118 case CS_CMD_DONE:
119 case CS_CMD_FAIL:
120 case CS_STATUS_RESULT:
121 break;
122
123 case CS_CURSOR_RESULT:
124
125 ret = ct_cmd_props(cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL);
126 if (ret != CS_SUCCEED) {
127 fprintf(stderr, "ct_cmd_props() failed\n");
128 return 1;
129 }
130 if (props_value & CS_CURSTAT_DECLARED) {
131 fprintf(stderr, "ct_cmd_props claims cursor is in DECLARED state when it should be OPEN\n");
132 return 1;
133 }
134 if (!(props_value & CS_CURSTAT_OPEN)) {
135 fprintf(stderr, "ct_cmd_props claims cursor is not in OPEN state when it should be \n");
136 return 1;
137 }
138 if (props_value & CS_CURSTAT_CLOSED) {
139 fprintf(stderr, "ct_cmd_props claims cursor is in CLOSED state when it should be OPEN\n");
140 return 1;
141 }
142
143 ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
144
145 if (ret != CS_SUCCEED) {
146 fprintf(stderr, "ct_res_info() failed");
147 return 1;
148 }
149
150 if (num_cols != 1) {
151 fprintf(stderr, "unexpected num of columns = %d \n", num_cols);
152 return 1;
153 }
154
155 for (i = 0; i < num_cols; i++) {
156
157 /* here we can finally test for the return status column */
158 ret = ct_describe(cmd, i + 1, &datafmt);
159
160 if (ret != CS_SUCCEED) {
161 fprintf(stderr, "ct_describe() failed for column %d\n", i);
162 return 1;
163 }
164
165 if (datafmt.status & CS_RETURN) {
166 fprintf(stdout, "ct_describe() column %d \n", i);
167 }
168
169 datafmt.datatype = CS_CHAR_TYPE;
170 datafmt.format = CS_FMT_NULLTERM;
171 datafmt.maxlength = 6;
172 datafmt.count = 1;
173 datafmt.locale = NULL;
174 ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind);
175 if (ret != CS_SUCCEED) {
176 fprintf(stderr, "ct_bind() failed\n");
177 return 1;
178 }
179
180 }
181 row_count = 0;
182 while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
183 || (ret == CS_ROW_FAIL)) {
184
185 if (row_count == 0) {
186 for (j = 0; j < num_cols; j++) {
187 fprintf(stdout, "\n%s\n", datafmt.name);
188 }
189 fprintf(stdout, "------\n\n");
190 }
191
192 for (j = 0; j < num_cols; j++) {
193 fprintf(stdout, "%s\n\n", col1);
194 row_count++;
195 }
196
197 ret = update_second_table(cmd2, col1);
198 if (ret)
199 return ret;
200 }
201
202 switch ((int) ret) {
203 case CS_END_DATA:
204 break;
205 case CS_ROW_FAIL:
206 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
207 return 1;
208 case CS_FAIL:
209 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
210 return 1;
211 default:
212 fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret);
213 return 1;
214 }
215 break;
216
217 case CS_COMPUTE_RESULT:
218 fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
219 return 1;
220 default:
221 fprintf(stderr, "ct_results() unexpected result_type.\n");
222 return 1;
223 }
224 }
225
226
227 ret = ct_cursor(cmd, CS_CURSOR_CLOSE, name, CS_NULLTERM, NULL, CS_UNUSED, CS_DEALLOC);
228
229 if (ret != CS_SUCCEED) {
230 fprintf(stderr, "ct_cursor(close) failed\n");
231 return ret;
232 }
233
234 if ((ret = ct_send(cmd)) != CS_SUCCEED) {
235 fprintf(stderr, "BILL ct_send() failed\n");
236 return ret;
237 }
238
239 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
240 if (result_type == CS_CMD_FAIL) {
241 fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
242 return 1;
243 }
244 }
245 if (results_ret != CS_END_RESULTS) {
246 fprintf(stderr, "ct_results() returned BAD.\n");
247 return 1;
248 }
249
250 ret = ct_cmd_props(cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL);
251 if (ret != CS_SUCCEED) {
252 fprintf(stderr, "ct_cmd_props() failed");
253 return 1;
254 }
255
256 if (props_value != CS_CURSTAT_NONE) {
257 fprintf(stderr, "ct_cmd_props() CS_CUR_STATUS != CS_CURSTAT_NONE \n");
258 return 1;
259 }
260
261 if (verbose) {
262 fprintf(stdout, "Trying declare, rows, open one at a time \n");
263 }
264
265 strcpy(text, "select col1 from #test_table where 2 = 2");
266
267 ret = ct_cursor(cmd, CS_CURSOR_DECLARE, name, 3, text, CS_NULLTERM, CS_UNUSED);
268
269 if (ret != CS_SUCCEED) {
270 fprintf(stderr, "ct_cursor declare failed\n");
271 return 1;
272 }
273
274 ret = ct_send(cmd);
275
276 if (ret != CS_SUCCEED) {
277 fprintf(stderr, "ct_send failed\n");
278 }
279
280 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
281 if (result_type == CS_CMD_FAIL) {
282 fprintf(stderr, "ct_results(4) result_type CS_CMD_FAIL.\n");
283 return 1;
284 }
285 }
286 if (results_ret != CS_END_RESULTS) {
287 fprintf(stderr, "ct_results() returned BAD.\n");
288 return 1;
289 }
290
291 ret = ct_cursor(cmd, CS_CURSOR_ROWS, name, 3, NULL, CS_UNUSED, (CS_INT) 1);
292 if (ret != CS_SUCCEED) {
293 fprintf(stderr, "ct_cursor set cursor rows failed");
294 return 1;
295 }
296
297 ret = ct_send(cmd);
298
299 if (ret != CS_SUCCEED) {
300 fprintf(stderr, "ct_send failed\n");
301 }
302
303 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
304 if (result_type == CS_CMD_FAIL) {
305 fprintf(stderr, "ct_results(5) result_type CS_CMD_FAIL.\n");
306 return 1;
307 }
308 }
309 if (results_ret != CS_END_RESULTS) {
310 fprintf(stderr, "ct_results() returned BAD.\n");
311 return 1;
312 }
313
314 ret = ct_cursor(cmd, CS_CURSOR_OPEN, name, 3, text, 26, CS_UNUSED);
315
316 if (ret != CS_SUCCEED) {
317 fprintf(stderr, "ct_cursor open failed\n");
318 return 1;
319 }
320
321 ret = ct_send(cmd);
322
323 if (ret != CS_SUCCEED) {
324 fprintf(stderr, "ct_send failed\n");
325 }
326
327 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
328 switch ((int) result_type) {
329
330 case CS_CMD_SUCCEED:
331 break;
332 case CS_CMD_DONE:
333 break;
334 case CS_CMD_FAIL:
335 fprintf(stderr, "ct_results(6) result_type CS_CMD_FAIL.\n");
336 break;
337 case CS_STATUS_RESULT:
338 fprintf(stdout, "ct_results: CS_STATUS_RESULT detected for sp_who\n");
339
340 case CS_CURSOR_RESULT:
341 ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
342
343 if (ret != CS_SUCCEED) {
344 fprintf(stderr, "ct_res_info() failed");
345 return 1;
346 }
347
348 if (num_cols != 1) {
349 fprintf(stderr, "unexpected num of columns = %d \n", num_cols);
350 return 1;
351 }
352
353 for (i = 0; i < num_cols; i++) {
354
355 /* here we can finally test for the return status column */
356 ret = ct_describe(cmd, i + 1, &datafmt);
357
358 if (ret != CS_SUCCEED) {
359 fprintf(stderr, "ct_describe() failed for column %d\n", i);
360 return 1;
361 }
362
363 if (datafmt.status & CS_RETURN) {
364 fprintf(stdout, "ct_describe() column %d \n", i);
365 }
366
367 datafmt.datatype = CS_CHAR_TYPE;
368 datafmt.format = CS_FMT_NULLTERM;
369 datafmt.maxlength = 6;
370 datafmt.count = 1;
371 datafmt.locale = NULL;
372 ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind);
373 if (ret != CS_SUCCEED) {
374 fprintf(stderr, "ct_bind() failed\n");
375 return 1;
376 }
377 }
378 row_count = 0;
379 while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
380 || (ret == CS_ROW_FAIL)) {
381
382 if (row_count == 0) {
383 for (j = 0; j < num_cols; j++) {
384 fprintf(stdout, "\n%s\n", datafmt.name);
385 }
386 fprintf(stdout, "------\n\n");
387 }
388
389 for (j = 0; j < num_cols; j++) {
390 fprintf(stdout, "%s\n\n", col1);
391 row_count++;
392 }
393 }
394
395
396 switch ((int) ret) {
397 case CS_END_DATA:
398 break;
399 case CS_ROW_FAIL:
400 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
401 return 1;
402 case CS_FAIL:
403 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
404 return 1;
405 default:
406 fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret);
407 return 1;
408 }
409 break;
410
411 case CS_COMPUTE_RESULT:
412 fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
413 return 1;
414 default:
415 fprintf(stderr, "ct_results() unexpected result_type.\n");
416 return 1;
417 }
418 }
419
420
421 ret = ct_cursor(cmd, CS_CURSOR_CLOSE, name, 3, NULL, CS_UNUSED, CS_UNUSED);
422
423 if (ret != CS_SUCCEED) {
424 fprintf(stderr, "ct_cursor(close) failed\n");
425 return ret;
426 }
427
428 if ((ret = ct_send(cmd)) != CS_SUCCEED) {
429 fprintf(stderr, "ct_send() failed\n");
430 return ret;
431 }
432
433 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
434 if (result_type == CS_CMD_FAIL) {
435 fprintf(stderr, "ct_results(7) result_type CS_CMD_FAIL.\n");
436 return 1;
437 }
438 }
439 if (results_ret != CS_END_RESULTS) {
440 fprintf(stderr, "ct_results() returned BAD.\n");
441 return 1;
442 }
443 ret = ct_cursor(cmd, CS_CURSOR_DEALLOC, name, 3, NULL, CS_UNUSED, CS_UNUSED);
444
445 if (ret != CS_SUCCEED) {
446 fprintf(stderr, "ct_cursor(dealloc) failed\n");
447 return ret;
448 }
449
450 if ((ret = ct_send(cmd)) != CS_SUCCEED) {
451 fprintf(stderr, "ct_send() failed\n");
452 return ret;
453 }
454
455 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
456 if (result_type == CS_CMD_FAIL) {
457 fprintf(stderr, "ct_results(8) result_type CS_CMD_FAIL.\n");
458 return 1;
459 }
460 }
461 if (results_ret != CS_END_RESULTS) {
462 fprintf(stderr, "ct_results() returned BAD.\n");
463 return 1;
464 }
465
466 if (verbose) {
467 fprintf(stdout, "Running normal select command after cursor operations\n");
468 }
469
470 ret = ct_command(cmd, CS_LANG_CMD, "select col1 from #test_table", CS_NULLTERM, CS_UNUSED);
471 if (ret != CS_SUCCEED) {
472 fprintf(stderr, "ct_command() failed\n");
473 return 1;
474 }
475 ret = ct_send(cmd);
476 if (ret != CS_SUCCEED) {
477 fprintf(stderr, "ct_send() failed\n");
478 return 1;
479 }
480 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
481 switch ((int) result_type) {
482 case CS_CMD_SUCCEED:
483 break;
484 case CS_CMD_DONE:
485 break;
486 case CS_CMD_FAIL:
487 fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
488 return 1;
489 case CS_ROW_RESULT:
490 datafmt.datatype = CS_CHAR_TYPE;
491 datafmt.format = CS_FMT_NULLTERM;
492 datafmt.maxlength = 6;
493 datafmt.count = 1;
494 datafmt.locale = NULL;
495 ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind);
496 if (ret != CS_SUCCEED) {
497 fprintf(stderr, "ct_bind() failed\n");
498 return 1;
499 }
500
501 while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
502 || (ret == CS_ROW_FAIL)) {
503 row_count += count;
504 if (ret == CS_ROW_FAIL) {
505 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
506 return 1;
507 } else if (ret == CS_SUCCEED) {
508 ;
509 } else {
510 break;
511 }
512 }
513 switch ((int) ret) {
514 case CS_END_DATA:
515 break;
516 case CS_FAIL:
517 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
518 return 1;
519 default:
520 fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret);
521 return 1;
522 }
523 break;
524 case CS_COMPUTE_RESULT:
525 fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
526 return 1;
527 default:
528 fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type);
529 return 1;
530 }
531 }
532 switch ((int) results_ret) {
533 case CS_END_RESULTS:
534 break;
535 case CS_FAIL:
536 fprintf(stderr, "ct_results() failed.\n");
537 return 1;
538 break;
539 default:
540 fprintf(stderr, "ct_results() unexpected return.\n");
541 return 1;
542 }
543 if (verbose) {
544 fprintf(stdout, "Trying logout\n");
545 }
546
547 ct_cmd_drop(cmd2);
548
549 ret = try_ctlogout(ctx, conn, cmd, verbose);
550 if (ret != CS_SUCCEED) {
551 fprintf(stderr, "Logout failed\n");
552 return 2;
553 }
554
555 if (verbose) {
556 fprintf(stdout, "Test suceeded\n");
557 }
558 return 0;
559 }
560
561 static int
update_second_table(CS_COMMAND * cmd2,char * value)562 update_second_table(CS_COMMAND * cmd2, char *value)
563 {
564
565 CS_RETCODE ret;
566 CS_RETCODE results_ret;
567 CS_INT result_type;
568 CS_INT count, row_count = 0;
569 CS_DATAFMT datafmt;
570 CS_SMALLINT ind;
571 CS_CHAR col1[6];
572 CS_INT datalength;
573 CS_CHAR text[128];
574
575 sprintf(text, "update #test_table2 set col1 = '%s' ", value);
576 ret = run_command(cmd2, text);
577 if (ret != CS_SUCCEED)
578 return 1;
579
580 ret = ct_command(cmd2, CS_LANG_CMD, "select col1 from #test_table2", CS_NULLTERM, CS_UNUSED);
581 if (ret != CS_SUCCEED) {
582 fprintf(stderr, "ct_command() failed\n");
583 return 1;
584 }
585 ret = ct_send(cmd2);
586 if (ret != CS_SUCCEED) {
587 fprintf(stderr, "ct_send() failed\n");
588 return 1;
589 }
590 while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
591 switch ((int) result_type) {
592 case CS_CMD_SUCCEED:
593 break;
594 case CS_CMD_DONE:
595 break;
596 case CS_CMD_FAIL:
597 fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
598 return 1;
599 case CS_ROW_RESULT:
600 datafmt.datatype = CS_CHAR_TYPE;
601 datafmt.format = CS_FMT_NULLTERM;
602 datafmt.maxlength = 6;
603 datafmt.count = 1;
604 datafmt.locale = NULL;
605 ret = ct_bind(cmd2, 1, &datafmt, col1, &datalength, &ind);
606 if (ret != CS_SUCCEED) {
607 fprintf(stderr, "ct_bind() failed\n");
608 return 1;
609 }
610
611 while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
612 || (ret == CS_ROW_FAIL)) {
613 row_count += count;
614 if (ret == CS_ROW_FAIL) {
615 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
616 return 1;
617 } else if (ret == CS_SUCCEED) {
618 ;
619 } else {
620 break;
621 }
622 }
623 switch ((int) ret) {
624 case CS_END_DATA:
625 break;
626 case CS_FAIL:
627 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
628 return 1;
629 default:
630 fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret);
631 return 1;
632 }
633 break;
634 case CS_COMPUTE_RESULT:
635 fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
636 return 1;
637 default:
638 fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type);
639 return 1;
640 }
641 }
642 switch ((int) results_ret) {
643 case CS_END_RESULTS:
644 break;
645 case CS_FAIL:
646 fprintf(stderr, "ct_results() failed.\n");
647 return 1;
648 break;
649 default:
650 fprintf(stderr, "ct_results() unexpected return.\n");
651 return 1;
652 }
653 return 0;
654 }
655
656