Line data Source code
1 : #include "common.h"
2 :
3 : /* Testing: array binding of result set */
4 10 : TEST_MAIN()
5 : {
6 : CS_CONTEXT *ctx;
7 : CS_CONNECTION *conn;
8 : CS_COMMAND *cmd;
9 10 : int verbose = 0;
10 :
11 : CS_RETCODE ret;
12 : CS_RETCODE results_ret;
13 : CS_INT result_type;
14 : CS_INT num_cols;
15 :
16 : CS_DATAFMT datafmt;
17 : CS_INT datalength[2];
18 : CS_SMALLINT ind[2];
19 10 : CS_INT count, row_count = 0;
20 : CS_INT cv;
21 :
22 : CS_CHAR select[1024];
23 :
24 : CS_INT col1[2];
25 : CS_CHAR col2[2][5];
26 : CS_CHAR col3[2][32];
27 :
28 :
29 10 : printf("%s: Retrieve data using array binding \n", __FILE__);
30 : if (verbose) {
31 : printf("Trying login\n");
32 : }
33 10 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
34 :
35 10 : check_call(run_command, (cmd, "CREATE TABLE #ctlibarray (col1 int not null, col2 char(4) not null, col3 datetime not null)"));
36 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan 1 2002 10:00:00AM')"));
37 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan 2 2002 10:00:00AM')"));
38 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (3, 'CCCC', 'Jan 3 2002 10:00:00AM')"));
39 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (8, 'DDDD', 'Jan 4 2002 10:00:00AM')"));
40 10 : check_call(run_command, (cmd, "insert into #ctlibarray values (9, 'EEEE', 'Jan 5 2002 10:00:00AM')"));
41 :
42 :
43 10 : strcpy(select, "select col1, col2, col3 from #ctlibarray order by col1 ");
44 :
45 10 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
46 :
47 10 : check_call(ct_send, (cmd));
48 :
49 40 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
50 20 : switch ((int) result_type) {
51 : case CS_CMD_SUCCEED:
52 : break;
53 : case CS_CMD_DONE:
54 : break;
55 0 : case CS_CMD_FAIL:
56 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
57 0 : return 1;
58 10 : case CS_ROW_RESULT:
59 :
60 10 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
61 10 : if (num_cols != 3) {
62 0 : fprintf(stderr, "num_cols %d != 3", num_cols);
63 0 : return 1;
64 : }
65 :
66 10 : check_call(ct_describe, (cmd, 1, &datafmt));
67 10 : datafmt.format = CS_FMT_UNUSED;
68 10 : if (datafmt.maxlength > 1024) {
69 0 : datafmt.maxlength = 1024;
70 : }
71 :
72 10 : datafmt.count = 2;
73 :
74 10 : check_call(ct_bind, (cmd, 1, &datafmt, &col1[0], datalength, ind));
75 :
76 10 : check_call(ct_describe, (cmd, 2, &datafmt));
77 :
78 10 : datafmt.format = CS_FMT_NULLTERM;
79 10 : datafmt.maxlength = 5;
80 10 : datafmt.count = 2;
81 :
82 10 : check_call(ct_bind, (cmd, 2, &datafmt, &col2[0], datalength, ind));
83 :
84 10 : check_call(ct_describe, (cmd, 3, &datafmt));
85 :
86 10 : datafmt.datatype = CS_CHAR_TYPE;
87 10 : datafmt.format = CS_FMT_NULLTERM;
88 10 : datafmt.maxlength = 32;
89 10 : datafmt.count = 2;
90 :
91 10 : check_call(ct_bind, (cmd, 3, &datafmt, &col3[0], datalength, ind));
92 :
93 10 : count = 0;
94 50 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
95 10 : || (ret == CS_ROW_FAIL)) {
96 30 : row_count += count;
97 30 : if (ret == CS_ROW_FAIL) {
98 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
99 0 : return 1;
100 : } else { /* ret == CS_SUCCEED */
101 30 : printf("ct_fetch returned %d rows\n", count);
102 80 : for (cv = 0; cv < count; cv++)
103 50 : printf("col1 = %d col2= '%s', col3 = '%s'\n", col1[cv], col2[cv],
104 50 : col3[cv]);
105 : }
106 30 : count = 0;
107 : }
108 :
109 :
110 10 : switch ((int) ret) {
111 : case CS_END_DATA:
112 : break;
113 0 : case CS_FAIL:
114 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
115 0 : return 1;
116 0 : default:
117 0 : fprintf(stderr, "ct_fetch() unexpected return.\n");
118 0 : return 1;
119 : }
120 : break;
121 :
122 0 : default:
123 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
124 0 : return 1;
125 : }
126 : }
127 10 : switch ((int) results_ret) {
128 : case CS_END_RESULTS:
129 : break;
130 0 : case CS_FAIL:
131 0 : fprintf(stderr, "ct_results() failed.\n");
132 0 : return 1;
133 : break;
134 0 : default:
135 0 : fprintf(stderr, "ct_results() unexpected return.\n");
136 0 : return 1;
137 : }
138 :
139 : if (verbose) {
140 : printf("Trying logout\n");
141 : }
142 10 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
143 :
144 10 : return 0;
145 : }
|