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