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