1 #include <config.h>
2 
3 #if HAVE_STRING_H
4 #include <string.h>
5 #endif /* HAVE_STRING_H */
6 
7 #include <stdio.h>
8 #include <ctpublic.h>
9 #include "common.h"
10 
11 #include <common/test_assert.h>
12 
13 /* Testing: Client Messages */
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 	int verbose = 0;
21 
22 	CS_RETCODE ret;
23 	CS_RETCODE results_ret;
24 	CS_INT result_type;
25 	CS_INT num_cols;
26 
27 	CS_DATAFMT datafmt;
28 	CS_INT datalength[2];
29 	CS_SMALLINT ind[2];
30 	CS_INT count, row_count = 0;
31 	CS_INT cv;
32 	int i;
33 	CS_CHAR select[1024];
34 
35 	CS_INT col1[2];
36 	CS_CHAR col2[2][5];
37 	CS_INT num_msgs, totmsgs;
38 	CS_CLIENTMSG client_message;
39 	int result = 1;
40 
41 	fprintf(stdout, "%s: Retrieve data using array binding \n", __FILE__);
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 	if (ct_diag(conn, CS_INIT, CS_UNUSED, CS_UNUSED, NULL) != CS_SUCCEED) {
52 		fprintf(stderr, "ct_diag(CS_INIT) failed\n");
53 		return 1;
54 	}
55 
56 	totmsgs = 1;
57 	if (ct_diag(conn, CS_MSGLIMIT, CS_CLIENTMSG_TYPE, CS_UNUSED, &totmsgs) != CS_SUCCEED) {
58 		fprintf(stderr, "ct_diag(CS_MSGLIMIT) failed\n");
59 		return 1;
60 	}
61 
62 	fprintf(stdout, "Maximum message limit is set to: %d\n", totmsgs);
63 
64 	ret = run_command(cmd, "CREATE TABLE #ctlibarray (col1 int not null,  col2 char(4) not null, col3 datetime not null)");
65 	if (ret != CS_SUCCEED)
66 		return 1;
67 
68 	ret = run_command(cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan  1 2002 10:00:00AM')");
69 	if (ret != CS_SUCCEED)
70 		return 1;
71 
72 	ret = run_command(cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan  2 2002 10:00:00AM')");
73 	if (ret != CS_SUCCEED)
74 		return 1;
75 
76 	strcpy(select, "select col1, col2 from #ctlibarray order by col1 ");
77 
78 	ret = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED);
79 
80 	if (ret != CS_SUCCEED) {
81 		fprintf(stderr, "ct_command(%s) failed\n", select);
82 		return 1;
83 	}
84 
85 	ret = ct_send(cmd);
86 	if (ret != CS_SUCCEED) {
87 		fprintf(stderr, "ct_send() failed\n");
88 		return 1;
89 	}
90 
91 	while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
92 		switch ((int) result_type) {
93 		case CS_CMD_SUCCEED:
94 			break;
95 		case CS_CMD_DONE:
96 			break;
97 		case CS_CMD_FAIL:
98 			fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
99 			return 1;
100 		case CS_ROW_RESULT:
101 
102 			ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
103 			if (ret != CS_SUCCEED) {
104 				fprintf(stderr, "ct_res_info() failed");
105 				return 1;
106 			}
107 			if (num_cols != 2) {
108 				fprintf(stderr, "num_cols %d != 2", num_cols);
109 				return 1;
110 			}
111 
112 			ret = ct_describe(cmd, 1, &datafmt);
113 			if (ret != CS_SUCCEED) {
114 				fprintf(stderr, "ct_describe() failed");
115 				return 1;
116 			}
117 			datafmt.format = CS_FMT_UNUSED;
118 			if (datafmt.maxlength > 1024) {
119 				datafmt.maxlength = 1024;
120 			}
121 
122 			datafmt.count = 2;
123 
124 			ret = ct_bind(cmd, 1, &datafmt, &col1[0], datalength, ind);
125 			if (ret != CS_SUCCEED) {
126 				fprintf(stderr, "ct_bind() failed\n");
127 				return 1;
128 			}
129 
130 			ret = ct_describe(cmd, 2, &datafmt);
131 			if (ret != CS_SUCCEED) {
132 				fprintf(stderr, "ct_describe() failed");
133 				return 1;
134 			}
135 
136 			datafmt.format = CS_FMT_NULLTERM;
137 			datafmt.maxlength = 5;
138 			datafmt.count = 4;
139 
140 			ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
141 			if (ret != CS_SUCCEED) {
142 
143 				if (ct_diag(conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
144 					fprintf(stderr, "ct_diag(CS_STATUS) failed\n");
145 					return 1;
146 				}
147 
148 				fprintf(stdout, "Number of client messages returned: %d\n", num_msgs);
149 
150 				for (i = 0; i < num_msgs; i++) {
151 
152 					if (ct_diag(conn, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message) != CS_SUCCEED) {
153 						fprintf(stderr, "cs_diag(CS_GET) failed\n");
154 						return 1;
155 					}
156 
157 					clientmsg_cb(ctx, conn, &client_message);
158 
159 				}
160 
161 				if (ct_diag(conn, CS_CLEAR, CS_CLIENTMSG_TYPE, CS_UNUSED, NULL) != CS_SUCCEED) {
162 					fprintf(stderr, "cs_diag(CS_CLEAR) failed\n");
163 					return 1;
164 				}
165 
166 				if (ct_diag(conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
167 					fprintf(stderr, "cs_diag(CS_STATUS) failed\n");
168 					return 1;
169 				}
170 				if (num_msgs != 0) {
171 					fprintf(stderr, "cs_diag(CS_CLEAR) failed there are still %d messages on queue\n",
172 						num_msgs);
173 					return 1;
174 				}
175 
176 				/* we catch error, good */
177 				result = 0;
178 			} else {
179 				fprintf(stderr, "ct_bind() succeeded while it shouldn't\n");
180 				return 1;
181 			}
182 			datafmt.count = 2;
183 			ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
184 			if (ret != CS_SUCCEED) {
185 				fprintf(stderr, "ct_bind() failed\n");
186 				return 1;
187 			}
188 			count = 0;
189 			while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
190 			       || (ret == CS_ROW_FAIL)) {
191 				row_count += count;
192 				if (ret == CS_ROW_FAIL) {
193 					fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
194 					return 1;
195 				} else {	/* ret == CS_SUCCEED */
196 					fprintf(stdout, "ct_fetch returned %d rows\n", count);
197 					for (cv = 0; cv < count; cv++)
198 						fprintf(stdout, "col1 = %d col2= '%s'\n", col1[cv], col2[cv]);
199 				}
200 				count = 0;
201 			}
202 
203 
204 			switch ((int) ret) {
205 			case CS_END_DATA:
206 				break;
207 			case CS_FAIL:
208 				fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
209 				return 1;
210 			default:
211 				fprintf(stderr, "ct_fetch() unexpected return.\n");
212 				return 1;
213 			}
214 			break;
215 
216 		default:
217 			fprintf(stderr, "ct_results() unexpected result_type.\n");
218 			return 1;
219 		}
220 	}
221 	switch ((int) results_ret) {
222 	case CS_END_RESULTS:
223 		break;
224 	case CS_FAIL:
225 		fprintf(stderr, "ct_results() failed.\n");
226 		return 1;
227 		break;
228 	default:
229 		fprintf(stderr, "ct_results() unexpected return.\n");
230 		return 1;
231 	}
232 
233 	if (verbose) {
234 		fprintf(stdout, "Trying logout\n");
235 	}
236 	ret = try_ctlogout(ctx, conn, cmd, verbose);
237 	if (ret != CS_SUCCEED) {
238 		fprintf(stderr, "Logout failed\n");
239 		return 1;
240 	}
241 
242 	return result;
243 }
244