Line data Source code
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
15 8 : main(void)
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 8 : CS_INT count, row_count = 0;
25 : CS_DATAFMT datafmt;
26 : CS_SMALLINT ind;
27 8 : 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 8 : printf("%s: Testing ct_cursor()\n", __FILE__);
36 :
37 : if (verbose) {
38 8 : printf("Trying login\n");
39 : }
40 8 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
41 :
42 8 : check_call(ct_cmd_alloc, (conn, &cmd2));
43 :
44 8 : check_call(run_command, (cmd, "CREATE TABLE #test_table (col1 char(4))"));
45 8 : check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('AAA')"));
46 8 : check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('BBB')"));
47 8 : check_call(run_command, (cmd, "INSERT #test_table (col1) VALUES ('CCC')"));
48 :
49 8 : check_call(run_command, (cmd2, "CREATE TABLE #test_table2 (col1 char(4))"));
50 8 : check_call(run_command, (cmd2, "INSERT #test_table2 (col1) VALUES ('---')"));
51 :
52 : if (verbose) {
53 8 : printf("Trying declare, rows , open in one SEND\n");
54 : }
55 :
56 8 : strcpy(text, "select col1 from #test_table where 1 = 1");
57 8 : strcpy(name, "c1");
58 :
59 8 : check_call(ct_cursor, (cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED));
60 :
61 8 : check_call(ct_cursor, (cmd, CS_CURSOR_ROWS, name, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1));
62 :
63 8 : check_call(ct_cursor, (cmd, CS_CURSOR_OPEN, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED));
64 :
65 8 : check_call(ct_send, (cmd));
66 :
67 48 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
68 32 : switch ((int) result_type) {
69 :
70 : case CS_CMD_SUCCEED:
71 : case CS_CMD_DONE:
72 : case CS_CMD_FAIL:
73 : case CS_STATUS_RESULT:
74 : break;
75 :
76 8 : case CS_CURSOR_RESULT:
77 :
78 8 : check_call(ct_cmd_props, (cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL));
79 8 : if (props_value & CS_CURSTAT_DECLARED) {
80 0 : fprintf(stderr, "ct_cmd_props claims cursor is in DECLARED state when it should be OPEN\n");
81 0 : return 1;
82 : }
83 8 : if (!(props_value & CS_CURSTAT_OPEN)) {
84 0 : fprintf(stderr, "ct_cmd_props claims cursor is not in OPEN state when it should be \n");
85 0 : return 1;
86 : }
87 8 : if (props_value & CS_CURSTAT_CLOSED) {
88 0 : fprintf(stderr, "ct_cmd_props claims cursor is in CLOSED state when it should be OPEN\n");
89 0 : return 1;
90 : }
91 :
92 8 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
93 :
94 8 : if (num_cols != 1) {
95 0 : fprintf(stderr, "unexpected num of columns = %d \n", num_cols);
96 0 : return 1;
97 : }
98 :
99 8 : for (i = 0; i < num_cols; i++) {
100 :
101 : /* here we can finally test for the return status column */
102 8 : check_call(ct_describe, (cmd, i + 1, &datafmt));
103 :
104 8 : if (datafmt.status & CS_RETURN) {
105 0 : printf("ct_describe() column %d \n", i);
106 : }
107 :
108 8 : datafmt.datatype = CS_CHAR_TYPE;
109 8 : datafmt.format = CS_FMT_NULLTERM;
110 8 : datafmt.maxlength = 6;
111 8 : datafmt.count = 1;
112 8 : datafmt.locale = NULL;
113 8 : check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
114 : }
115 : row_count = 0;
116 32 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
117 8 : || (ret == CS_ROW_FAIL)) {
118 :
119 24 : if (row_count == 0) {
120 8 : for (j = 0; j < num_cols; j++) {
121 8 : printf("\n%s\n", datafmt.name);
122 : }
123 8 : printf("------\n\n");
124 : }
125 :
126 24 : for (j = 0; j < num_cols; j++) {
127 24 : printf("%s\n\n", col1);
128 24 : row_count++;
129 : }
130 :
131 24 : ret = update_second_table(cmd2, col1);
132 24 : if (ret)
133 : return ret;
134 : }
135 :
136 8 : switch ((int) ret) {
137 : case CS_END_DATA:
138 : break;
139 0 : case CS_ROW_FAIL:
140 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
141 0 : return 1;
142 0 : case CS_FAIL:
143 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
144 0 : return 1;
145 0 : default:
146 0 : fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret);
147 0 : return 1;
148 : }
149 : break;
150 :
151 0 : case CS_COMPUTE_RESULT:
152 0 : fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
153 0 : return 1;
154 0 : default:
155 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
156 0 : return 1;
157 : }
158 : }
159 :
160 :
161 8 : check_call(ct_cursor, (cmd, CS_CURSOR_CLOSE, name, CS_NULLTERM, NULL, CS_UNUSED, CS_DEALLOC));
162 :
163 8 : check_call(ct_send, (cmd));
164 :
165 20 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
166 4 : if (result_type == CS_CMD_FAIL) {
167 0 : fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
168 0 : return 1;
169 : }
170 : }
171 8 : if (results_ret != CS_END_RESULTS) {
172 0 : fprintf(stderr, "ct_results() returned BAD.\n");
173 0 : return 1;
174 : }
175 :
176 8 : check_call(ct_cmd_props, (cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL));
177 :
178 8 : if (props_value != CS_CURSTAT_NONE) {
179 0 : fprintf(stderr, "ct_cmd_props() CS_CUR_STATUS != CS_CURSTAT_NONE \n");
180 0 : return 1;
181 : }
182 :
183 : if (verbose) {
184 8 : printf("Trying declare, rows, open one at a time \n");
185 : }
186 :
187 8 : strcpy(text, "select col1 from #test_table where 2 = 2");
188 :
189 8 : check_call(ct_cursor, (cmd, CS_CURSOR_DECLARE, name, 3, text, CS_NULLTERM, CS_UNUSED));
190 :
191 8 : check_call(ct_send, (cmd));
192 :
193 20 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
194 4 : if (result_type == CS_CMD_FAIL) {
195 0 : fprintf(stderr, "ct_results(4) result_type CS_CMD_FAIL.\n");
196 0 : return 1;
197 : }
198 : }
199 8 : if (results_ret != CS_END_RESULTS) {
200 0 : fprintf(stderr, "ct_results() returned BAD.\n");
201 0 : return 1;
202 : }
203 :
204 8 : check_call(ct_cursor, (cmd, CS_CURSOR_ROWS, name, 3, NULL, CS_UNUSED, (CS_INT) 1));
205 :
206 8 : check_call(ct_send, (cmd));
207 :
208 20 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
209 4 : if (result_type == CS_CMD_FAIL) {
210 0 : fprintf(stderr, "ct_results(5) result_type CS_CMD_FAIL.\n");
211 0 : return 1;
212 : }
213 : }
214 8 : if (results_ret != CS_END_RESULTS) {
215 0 : fprintf(stderr, "ct_results() returned BAD.\n");
216 0 : return 1;
217 : }
218 :
219 8 : check_call(ct_cursor, (cmd, CS_CURSOR_OPEN, name, 3, text, 26, CS_UNUSED));
220 :
221 8 : check_call(ct_send, (cmd));
222 :
223 40 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
224 24 : switch ((int) result_type) {
225 :
226 : case CS_CMD_SUCCEED:
227 : break;
228 : case CS_CMD_DONE:
229 : break;
230 0 : case CS_CMD_FAIL:
231 0 : fprintf(stderr, "ct_results(6) result_type CS_CMD_FAIL.\n");
232 0 : break;
233 0 : case CS_STATUS_RESULT:
234 0 : printf("ct_results: CS_STATUS_RESULT detected for sp_who\n");
235 :
236 8 : case CS_CURSOR_RESULT:
237 8 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
238 :
239 8 : if (num_cols != 1) {
240 0 : fprintf(stderr, "unexpected num of columns = %d \n", num_cols);
241 0 : return 1;
242 : }
243 :
244 8 : for (i = 0; i < num_cols; i++) {
245 :
246 : /* here we can finally test for the return status column */
247 8 : check_call(ct_describe, (cmd, i + 1, &datafmt));
248 :
249 8 : if (datafmt.status & CS_RETURN) {
250 0 : printf("ct_describe() column %d \n", i);
251 : }
252 :
253 8 : datafmt.datatype = CS_CHAR_TYPE;
254 8 : datafmt.format = CS_FMT_NULLTERM;
255 8 : datafmt.maxlength = 6;
256 8 : datafmt.count = 1;
257 8 : datafmt.locale = NULL;
258 8 : check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
259 : }
260 : row_count = 0;
261 32 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
262 8 : || (ret == CS_ROW_FAIL)) {
263 :
264 24 : if (row_count == 0) {
265 8 : for (j = 0; j < num_cols; j++) {
266 8 : printf("\n%s\n", datafmt.name);
267 : }
268 8 : printf("------\n\n");
269 : }
270 :
271 24 : for (j = 0; j < num_cols; j++) {
272 24 : printf("%s\n\n", col1);
273 24 : row_count++;
274 : }
275 : }
276 :
277 :
278 8 : switch ((int) ret) {
279 : case CS_END_DATA:
280 : break;
281 0 : case CS_ROW_FAIL:
282 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
283 0 : return 1;
284 0 : case CS_FAIL:
285 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
286 0 : return 1;
287 0 : default:
288 0 : fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret);
289 0 : return 1;
290 : }
291 : break;
292 :
293 0 : case CS_COMPUTE_RESULT:
294 0 : fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
295 0 : return 1;
296 0 : default:
297 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
298 0 : return 1;
299 : }
300 : }
301 :
302 :
303 8 : check_call(ct_cursor, (cmd, CS_CURSOR_CLOSE, name, 3, NULL, CS_UNUSED, CS_UNUSED));
304 :
305 8 : check_call(ct_send, (cmd));
306 :
307 20 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
308 4 : if (result_type == CS_CMD_FAIL) {
309 0 : fprintf(stderr, "ct_results(7) result_type CS_CMD_FAIL.\n");
310 0 : return 1;
311 : }
312 : }
313 8 : if (results_ret != CS_END_RESULTS) {
314 0 : fprintf(stderr, "ct_results() returned BAD.\n");
315 0 : return 1;
316 : }
317 8 : check_call(ct_cursor, (cmd, CS_CURSOR_DEALLOC, name, 3, NULL, CS_UNUSED, CS_UNUSED));
318 :
319 8 : check_call(ct_send, (cmd));
320 :
321 20 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
322 4 : if (result_type == CS_CMD_FAIL) {
323 0 : fprintf(stderr, "ct_results(8) result_type CS_CMD_FAIL.\n");
324 0 : return 1;
325 : }
326 : }
327 8 : if (results_ret != CS_END_RESULTS) {
328 0 : fprintf(stderr, "ct_results() returned BAD.\n");
329 0 : return 1;
330 : }
331 :
332 : if (verbose) {
333 8 : printf("Running normal select command after cursor operations\n");
334 : }
335 :
336 8 : check_call(ct_command, (cmd, CS_LANG_CMD, "select col1 from #test_table", CS_NULLTERM, CS_UNUSED));
337 8 : check_call(ct_send, (cmd));
338 32 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
339 16 : switch ((int) result_type) {
340 : case CS_CMD_SUCCEED:
341 : break;
342 : case CS_CMD_DONE:
343 : break;
344 0 : case CS_CMD_FAIL:
345 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
346 0 : return 1;
347 8 : case CS_ROW_RESULT:
348 8 : datafmt.datatype = CS_CHAR_TYPE;
349 8 : datafmt.format = CS_FMT_NULLTERM;
350 8 : datafmt.maxlength = 6;
351 8 : datafmt.count = 1;
352 8 : datafmt.locale = NULL;
353 8 : check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
354 :
355 40 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
356 8 : || (ret == CS_ROW_FAIL)) {
357 24 : row_count += count;
358 24 : if (ret == CS_ROW_FAIL) {
359 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
360 0 : return 1;
361 24 : } else if (ret == CS_SUCCEED) {
362 : ;
363 : } else {
364 : break;
365 : }
366 : }
367 8 : switch ((int) ret) {
368 : case CS_END_DATA:
369 : break;
370 0 : case CS_FAIL:
371 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
372 0 : return 1;
373 0 : default:
374 0 : fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret);
375 0 : return 1;
376 : }
377 : break;
378 0 : case CS_COMPUTE_RESULT:
379 0 : fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
380 0 : return 1;
381 0 : default:
382 0 : fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type);
383 0 : return 1;
384 : }
385 : }
386 8 : switch ((int) results_ret) {
387 : case CS_END_RESULTS:
388 : break;
389 0 : case CS_FAIL:
390 0 : fprintf(stderr, "ct_results() failed.\n");
391 0 : return 1;
392 : break;
393 0 : default:
394 0 : fprintf(stderr, "ct_results() unexpected return.\n");
395 0 : return 1;
396 : }
397 : if (verbose) {
398 8 : printf("Trying logout\n");
399 : }
400 :
401 8 : ct_cmd_drop(cmd2);
402 :
403 8 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
404 :
405 : if (verbose) {
406 8 : printf("Test suceeded\n");
407 : }
408 8 : return 0;
409 : }
410 :
411 : static int
412 24 : update_second_table(CS_COMMAND * cmd2, char *value)
413 : {
414 :
415 : CS_RETCODE ret;
416 : CS_RETCODE results_ret;
417 : CS_INT result_type;
418 24 : CS_INT count, row_count = 0;
419 : CS_DATAFMT datafmt;
420 : CS_SMALLINT ind;
421 : CS_CHAR col1[6];
422 : CS_INT datalength;
423 : CS_CHAR text[128];
424 :
425 24 : sprintf(text, "update #test_table2 set col1 = '%s' ", value);
426 24 : ret = run_command(cmd2, text);
427 24 : if (ret != CS_SUCCEED)
428 : return 1;
429 :
430 24 : ret = ct_command(cmd2, CS_LANG_CMD, "select col1 from #test_table2", CS_NULLTERM, CS_UNUSED);
431 24 : if (ret != CS_SUCCEED) {
432 0 : fprintf(stderr, "ct_command() failed\n");
433 0 : return 1;
434 : }
435 24 : ret = ct_send(cmd2);
436 24 : if (ret != CS_SUCCEED) {
437 0 : fprintf(stderr, "ct_send() failed\n");
438 0 : return 1;
439 : }
440 72 : while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
441 48 : switch ((int) result_type) {
442 : case CS_CMD_SUCCEED:
443 : break;
444 : case CS_CMD_DONE:
445 : break;
446 0 : case CS_CMD_FAIL:
447 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
448 0 : return 1;
449 24 : case CS_ROW_RESULT:
450 24 : datafmt.datatype = CS_CHAR_TYPE;
451 24 : datafmt.format = CS_FMT_NULLTERM;
452 24 : datafmt.maxlength = 6;
453 24 : datafmt.count = 1;
454 24 : datafmt.locale = NULL;
455 24 : ret = ct_bind(cmd2, 1, &datafmt, col1, &datalength, &ind);
456 24 : if (ret != CS_SUCCEED) {
457 0 : fprintf(stderr, "ct_bind() failed\n");
458 0 : return 1;
459 : }
460 :
461 48 : while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
462 24 : || (ret == CS_ROW_FAIL)) {
463 24 : row_count += count;
464 24 : if (ret == CS_ROW_FAIL) {
465 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
466 0 : return 1;
467 24 : } else if (ret == CS_SUCCEED) {
468 : ;
469 : } else {
470 : break;
471 : }
472 : }
473 24 : switch ((int) ret) {
474 : case CS_END_DATA:
475 : break;
476 0 : case CS_FAIL:
477 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
478 0 : return 1;
479 0 : default:
480 0 : fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret);
481 0 : return 1;
482 : }
483 : break;
484 0 : case CS_COMPUTE_RESULT:
485 0 : fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
486 0 : return 1;
487 0 : default:
488 0 : fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type);
489 0 : return 1;
490 : }
491 : }
492 24 : switch ((int) results_ret) {
493 : case CS_END_RESULTS:
494 : break;
495 0 : case CS_FAIL:
496 0 : fprintf(stderr, "ct_results() failed.\n");
497 0 : return 1;
498 : break;
499 0 : default:
500 0 : fprintf(stderr, "ct_results() unexpected return.\n");
501 0 : return 1;
502 : }
503 : return 0;
504 : }
505 :
|