Line data Source code
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 : /* Testing: Client Messages */
12 : int
13 8 : main(void)
14 : {
15 : CS_CONTEXT *ctx;
16 : CS_CONNECTION *conn;
17 : CS_COMMAND *cmd;
18 8 : int verbose = 0;
19 :
20 : CS_RETCODE ret;
21 : CS_RETCODE results_ret;
22 : CS_INT result_type;
23 : CS_INT num_cols;
24 :
25 : CS_DATAFMT datafmt;
26 : CS_INT datalength[2];
27 : CS_SMALLINT ind[2];
28 8 : CS_INT count, row_count = 0;
29 : CS_INT cv;
30 : int i;
31 : CS_CHAR select[1024];
32 :
33 : CS_INT col1[2];
34 : CS_CHAR col2[2][5];
35 : CS_INT num_msgs, totmsgs;
36 : CS_CLIENTMSG client_message;
37 8 : int result = 1;
38 :
39 8 : printf("%s: Retrieve data using array binding \n", __FILE__);
40 : if (verbose) {
41 : printf("Trying login\n");
42 : }
43 8 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
44 :
45 8 : check_call(ct_diag, (conn, CS_INIT, CS_UNUSED, CS_UNUSED, NULL));
46 :
47 8 : totmsgs = 1;
48 8 : check_call(ct_diag, (conn, CS_MSGLIMIT, CS_CLIENTMSG_TYPE, CS_UNUSED, &totmsgs));
49 :
50 8 : printf("Maximum message limit is set to: %d\n", totmsgs);
51 :
52 8 : check_call(run_command, (cmd, "CREATE TABLE #ctlibarray (col1 int not null, col2 char(4) not null, col3 datetime not null)"));
53 8 : check_call(run_command, (cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan 1 2002 10:00:00AM')"));
54 8 : check_call(run_command, (cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan 2 2002 10:00:00AM')"));
55 :
56 8 : strcpy(select, "select col1, col2 from #ctlibarray order by col1 ");
57 :
58 8 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
59 :
60 8 : check_call(ct_send, (cmd));
61 :
62 32 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
63 16 : switch ((int) result_type) {
64 : case CS_CMD_SUCCEED:
65 : break;
66 : case CS_CMD_DONE:
67 : break;
68 0 : case CS_CMD_FAIL:
69 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
70 0 : return 1;
71 8 : case CS_ROW_RESULT:
72 :
73 8 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
74 8 : if (num_cols != 2) {
75 0 : fprintf(stderr, "num_cols %d != 2", num_cols);
76 0 : return 1;
77 : }
78 :
79 8 : check_call(ct_describe, (cmd, 1, &datafmt));
80 8 : datafmt.format = CS_FMT_UNUSED;
81 8 : if (datafmt.maxlength > 1024) {
82 0 : datafmt.maxlength = 1024;
83 : }
84 :
85 8 : datafmt.count = 2;
86 :
87 8 : check_call(ct_bind, (cmd, 1, &datafmt, &col1[0], datalength, ind));
88 :
89 8 : check_call(ct_describe, (cmd, 2, &datafmt));
90 :
91 8 : datafmt.format = CS_FMT_NULLTERM;
92 8 : datafmt.maxlength = 5;
93 8 : datafmt.count = 4;
94 :
95 8 : ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
96 8 : if (ret != CS_SUCCEED) {
97 :
98 8 : check_call(ct_diag, (conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs));
99 :
100 8 : printf("Number of client messages returned: %d\n", num_msgs);
101 :
102 16 : for (i = 0; i < num_msgs; i++) {
103 :
104 8 : check_call(ct_diag, (conn, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message));
105 :
106 8 : clientmsg_cb(ctx, conn, &client_message);
107 : }
108 :
109 8 : check_call(ct_diag, (conn, CS_CLEAR, CS_CLIENTMSG_TYPE, CS_UNUSED, NULL));
110 :
111 8 : check_call(ct_diag, (conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs));
112 8 : if (num_msgs != 0) {
113 0 : fprintf(stderr, "cs_diag(CS_CLEAR) failed there are still %d messages on queue\n",
114 : num_msgs);
115 0 : return 1;
116 : }
117 :
118 : /* we catch error, good */
119 8 : result = 0;
120 : } else {
121 0 : fprintf(stderr, "ct_bind() succeeded while it shouldn't\n");
122 0 : return 1;
123 : }
124 8 : datafmt.count = 2;
125 8 : check_call(ct_bind, (cmd, 2, &datafmt, &col2[0], datalength, ind));
126 8 : count = 0;
127 24 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
128 8 : || (ret == CS_ROW_FAIL)) {
129 8 : row_count += count;
130 8 : if (ret == CS_ROW_FAIL) {
131 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
132 0 : return 1;
133 : } else { /* ret == CS_SUCCEED */
134 8 : printf("ct_fetch returned %d rows\n", count);
135 24 : for (cv = 0; cv < count; cv++)
136 16 : printf("col1 = %d col2= '%s'\n", col1[cv], col2[cv]);
137 : }
138 8 : count = 0;
139 : }
140 :
141 :
142 8 : switch ((int) ret) {
143 : case CS_END_DATA:
144 : break;
145 0 : case CS_FAIL:
146 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
147 0 : return 1;
148 0 : default:
149 0 : fprintf(stderr, "ct_fetch() unexpected return.\n");
150 0 : return 1;
151 : }
152 : break;
153 :
154 0 : default:
155 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
156 0 : return 1;
157 : }
158 : }
159 8 : switch ((int) results_ret) {
160 : case CS_END_RESULTS:
161 : break;
162 0 : case CS_FAIL:
163 0 : fprintf(stderr, "ct_results() failed.\n");
164 0 : return 1;
165 : break;
166 0 : default:
167 0 : fprintf(stderr, "ct_results() unexpected return.\n");
168 0 : return 1;
169 : }
170 :
171 : if (verbose) {
172 : printf("Trying logout\n");
173 : }
174 8 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
175 :
176 8 : return result;
177 : }
|