1 #include <config.h>
2
3 #include <stdio.h>
4 #include <ctpublic.h>
5 #include "common.h"
6
7 #include <common/test_assert.h>
8
9 static char software_version[] = "$Id: t0004.c 554204 2017-12-28 19:03:36Z ucko $";
10 static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
11
12 /* protos */
13 int do_fetch(CS_COMMAND * cmd);
14 CS_RETCODE do_results(CS_COMMAND * cmd, CS_INT * results);
15
16 /* defines */
17 #define NUMROWS 5
18
19 /* Testing: Test order of ct_results() */
20 int
main(int argc,char ** argv)21 main(int argc, char **argv)
22 {
23 CS_CONTEXT *ctx;
24 CS_CONNECTION *conn;
25 CS_COMMAND *cmd;
26 int i, verbose = 0;
27
28 CS_RETCODE ret;
29 CS_RETCODE results_ret;
30
31 char query[1024];
32 CS_INT insert_results[] = { CS_CMD_SUCCEED, CS_CMD_DONE };
33 CS_INT update_results[] = { CS_CMD_SUCCEED, CS_CMD_DONE };
34 CS_INT select_results[] = { CS_ROW_RESULT, CS_CMD_DONE };
35
36 fprintf(stdout, "%s: Check ordering of returns from cs_results()\n", __FILE__);
37 if (verbose) {
38 fprintf(stdout, "Trying login\n");
39 }
40 ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
41 if (ret != CS_SUCCEED) {
42 fprintf(stderr, "Login failed\n");
43 return 1;
44 }
45
46 ret = run_command(cmd, "CREATE TABLE #t0004 (id int)");
47 if (ret != CS_SUCCEED)
48 return 1;
49 for (i = 0; i < NUMROWS; i++) {
50 sprintf(query, "INSERT #t0004 (id) VALUES (%d)", i);
51
52 ret = ct_command(cmd, CS_LANG_CMD, query, CS_NULLTERM, CS_UNUSED);
53 if (ret != CS_SUCCEED) {
54 fprintf(stderr, "ct_command() failed\n");
55 return 1;
56 }
57 ret = ct_send(cmd);
58 if (ret != CS_SUCCEED) {
59 fprintf(stderr, "ct_send() failed\n");
60 return 1;
61 }
62
63 results_ret = do_results(cmd, insert_results);
64 switch ((int) results_ret) {
65 case CS_END_RESULTS:
66 break;
67 case CS_FAIL:
68 fprintf(stderr, "ct_results() failed.\n");
69 return 1;
70 break;
71 default:
72 fprintf(stderr, "ct_results() unexpected return.\n");
73 return 1;
74 }
75 }
76
77 ret = ct_command(cmd, CS_LANG_CMD, "UPDATE #t0004 SET id = id + 1", CS_NULLTERM, CS_UNUSED);
78 if (ret != CS_SUCCEED) {
79 fprintf(stderr, "ct_command() failed\n");
80 return 1;
81 }
82 ret = ct_send(cmd);
83 if (ret != CS_SUCCEED) {
84 fprintf(stderr, "ct_send() failed\n");
85 return 1;
86 }
87
88 results_ret = do_results(cmd, update_results);
89 switch ((int) results_ret) {
90 case CS_END_RESULTS:
91 break;
92 case CS_FAIL:
93 fprintf(stderr, "ct_results() failed.\n");
94 return 1;
95 break;
96 default:
97 fprintf(stderr, "ct_results() unexpected return.\n");
98 return 1;
99 }
100
101 /* single row select */
102 ret = ct_command(cmd, CS_LANG_CMD, "SELECT * FROM #t0004 WHERE id = 1", CS_NULLTERM, CS_UNUSED);
103 if (ret != CS_SUCCEED) {
104 fprintf(stderr, "ct_command() failed\n");
105 return 1;
106 }
107 ret = ct_send(cmd);
108 if (ret != CS_SUCCEED) {
109 fprintf(stderr, "ct_send() failed\n");
110 return 1;
111 }
112
113 results_ret = do_results(cmd, select_results);
114 switch ((int) results_ret) {
115 case CS_END_RESULTS:
116 break;
117 case CS_FAIL:
118 fprintf(stderr, "ct_results() failed.\n");
119 return 1;
120 break;
121 default:
122 fprintf(stderr, "ct_results() unexpected return.\n");
123 return 1;
124 }
125 if (verbose) {
126 fprintf(stdout, "Trying logout\n");
127 }
128 ret = try_ctlogout(ctx, conn, cmd, verbose);
129 if (ret != CS_SUCCEED) {
130 fprintf(stderr, "Logout failed\n");
131 return 1;
132 }
133
134 return 0;
135 }
136
137 int
do_fetch(CS_COMMAND * cmd)138 do_fetch(CS_COMMAND * cmd)
139 {
140 CS_INT count, row_count = 0;
141 CS_RETCODE ret;
142
143 while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
144 row_count += count;
145 }
146 if (ret == CS_ROW_FAIL) {
147 fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
148 return 1;
149 } else if (ret == CS_END_DATA) {
150 return 0;
151 } else {
152 fprintf(stderr, "ct_fetch() unexpected return %d on row %d.\n", ret, row_count);
153 return 1;
154 }
155 }
156
157 CS_RETCODE
do_results(CS_COMMAND * cmd,CS_INT * results)158 do_results(CS_COMMAND * cmd, CS_INT * results)
159 {
160 int result_num;
161 CS_RETCODE results_ret, result_type;
162 int done = 0;
163
164 result_num = 0;
165 while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
166 printf("result_ret %d result_type %d\n", results_ret, result_type);
167 if (result_type == CS_STATUS_RESULT)
168 continue;
169 if (done) {
170 fputs("No further results were expected\n", stderr);
171 return CS_FAIL;
172 }
173 if (result_type != results[result_num]) {
174 fprintf(stderr, "ct_results() expected %d received %d\n", results[result_num], result_type);
175 return CS_FAIL;
176 }
177 switch ((int) result_type) {
178 case CS_ROW_RESULT:
179 if (do_fetch(cmd)) {
180 return CS_FAIL;
181 }
182 break;
183 case CS_CMD_DONE:
184 done = 1;
185 break;
186 }
187 result_num++;
188 }
189 if ( !done ) {
190 fputs("Never saw CS_CMD_DONE\n", stderr);
191 return CS_FAIL;
192 }
193 return results_ret;
194 }
195