1 #include <config.h>
2
3 #include <stdarg.h>
4 #include <stdio.h>
5
6 #if HAVE_STRING_H
7 #include <string.h>
8 #endif /* HAVE_STRING_H */
9
10 #include <ctpublic.h>
11 #include "common.h"
12
13 #include <common/test_assert.h>
14
15 static char software_version[] = "$Id: array_bind.c 487476 2015-12-17 19:48:39Z ucko $";
16 static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
17
18 /* Testing: array binding of result set */
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 int verbose = 0;
26
27 CS_RETCODE ret;
28 CS_RETCODE results_ret;
29 CS_INT result_type;
30 CS_INT num_cols;
31
32 CS_DATAFMT datafmt;
33 CS_INT datalength[2];
34 CS_SMALLINT ind[2];
35 CS_INT count, row_count = 0;
36 CS_INT cv;
37
38 CS_CHAR select[1024];
39
40 CS_INT col1[2];
41 CS_CHAR col2[2][5];
42 CS_CHAR col3[2][32];
43
44
45 fprintf(stdout, "%s: Retrieve data using array binding \n", __FILE__);
46 if (verbose) {
47 fprintf(stdout, "Trying login\n");
48 }
49 ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
50 if (ret != CS_SUCCEED) {
51 fprintf(stderr, "Login failed\n");
52 return 1;
53 }
54
55 ret = run_command(cmd, "CREATE TABLE #ctlibarray (col1 int not null, col2 char(4) not null, col3 datetime not null)");
56 if (ret != CS_SUCCEED)
57 return 1;
58
59 ret = run_command(cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan 1 2002 10:00:00AM')");
60 if (ret != CS_SUCCEED)
61 return 1;
62
63 ret = run_command(cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan 2 2002 10:00:00AM')");
64 if (ret != CS_SUCCEED)
65 return 1;
66
67 ret = run_command(cmd, "insert into #ctlibarray values (3, 'CCCC', 'Jan 3 2002 10:00:00AM')");
68 if (ret != CS_SUCCEED)
69 return 1;
70
71 ret = run_command(cmd, "insert into #ctlibarray values (8, 'DDDD', 'Jan 4 2002 10:00:00AM')");
72 if (ret != CS_SUCCEED)
73 return 1;
74
75 ret = run_command(cmd, "insert into #ctlibarray values (9, 'EEEE', 'Jan 5 2002 10:00:00AM')");
76 if (ret != CS_SUCCEED)
77 return 1;
78
79
80 strcpy(select, "select col1, col2, col3 from #ctlibarray order by col1 ");
81
82 ret = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED);
83
84 if (ret != CS_SUCCEED) {
85 fprintf(stderr, "ct_command(%s) failed\n", select);
86 return 1;
87 }
88
89 ret = ct_send(cmd);
90 if (ret != CS_SUCCEED) {
91 fprintf(stderr, "ct_send() failed\n");
92 return 1;
93 }
94
95 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
96 switch ((int) result_type) {
97 case CS_CMD_SUCCEED:
98 break;
99 case CS_CMD_DONE:
100 break;
101 case CS_CMD_FAIL:
102 fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
103 return 1;
104 case CS_ROW_RESULT:
105
106 ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
107 if (ret != CS_SUCCEED) {
108 fprintf(stderr, "ct_res_info() failed");
109 return 1;
110 }
111 if (num_cols != 3) {
112 fprintf(stderr, "num_cols %d != 3", num_cols);
113 return 1;
114 }
115
116 ret = ct_describe(cmd, 1, &datafmt);
117 if (ret != CS_SUCCEED) {
118 fprintf(stderr, "ct_describe() failed");
119 return 1;
120 }
121 datafmt.format = CS_FMT_UNUSED;
122 if (datafmt.maxlength > 1024) {
123 datafmt.maxlength = 1024;
124 }
125
126 datafmt.count = 2;
127
128 ret = ct_bind(cmd, 1, &datafmt, &col1[0], datalength, ind);
129 if (ret != CS_SUCCEED) {
130 fprintf(stderr, "ct_bind() failed\n");
131 return 1;
132 }
133
134 ret = ct_describe(cmd, 2, &datafmt);
135 if (ret != CS_SUCCEED) {
136 fprintf(stderr, "ct_describe() failed");
137 return 1;
138 }
139
140 datafmt.format = CS_FMT_NULLTERM;
141 datafmt.maxlength = 5;
142 datafmt.count = 2;
143
144 ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
145 if (ret != CS_SUCCEED) {
146 fprintf(stderr, "ct_bind() failed\n");
147 return 1;
148 }
149
150 ret = ct_describe(cmd, 3, &datafmt);
151 if (ret != CS_SUCCEED) {
152 fprintf(stderr, "ct_describe() failed");
153 return 1;
154 }
155
156 datafmt.datatype = CS_CHAR_TYPE;
157 datafmt.format = CS_FMT_NULLTERM;
158 datafmt.maxlength = 32;
159 datafmt.count = 2;
160
161 ret = ct_bind(cmd, 3, &datafmt, &col3[0], datalength, ind);
162 if (ret != CS_SUCCEED) {
163 fprintf(stderr, "ct_bind() failed\n");
164 return 1;
165 }
166
167 count = 0;
168 while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
169 || (ret == CS_ROW_FAIL)) {
170 row_count += count;
171 if (ret == CS_ROW_FAIL) {
172 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
173 return 1;
174 } else { /* ret == CS_SUCCEED */
175 fprintf(stdout, "ct_fetch returned %d rows\n", count);
176 for (cv = 0; cv < count; cv++)
177 fprintf(stdout, "col1 = %d col2= '%s', col3 = '%s'\n", col1[cv], col2[cv],
178 col3[cv]);
179 }
180 count = 0;
181 }
182
183
184 switch ((int) ret) {
185 case CS_END_DATA:
186 break;
187 case CS_FAIL:
188 fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
189 return 1;
190 default:
191 fprintf(stderr, "ct_fetch() unexpected return.\n");
192 return 1;
193 }
194 break;
195
196 default:
197 fprintf(stderr, "ct_results() unexpected result_type.\n");
198 return 1;
199 }
200 }
201 switch ((int) results_ret) {
202 case CS_END_RESULTS:
203 break;
204 case CS_FAIL:
205 fprintf(stderr, "ct_results() failed.\n");
206 return 1;
207 break;
208 default:
209 fprintf(stderr, "ct_results() unexpected return.\n");
210 return 1;
211 }
212
213 if (verbose) {
214 fprintf(stdout, "Trying logout\n");
215 }
216 ret = try_ctlogout(ctx, conn, cmd, verbose);
217 if (ret != CS_SUCCEED) {
218 fprintf(stderr, "Logout failed\n");
219 return 1;
220 }
221
222 return 0;
223 }
224