Line data Source code
1 : #include "common.h"
2 :
3 : /* protos */
4 : static int do_fetch(CS_COMMAND * cmd);
5 : static CS_RETCODE do_results(CS_COMMAND * cmd, CS_INT * results);
6 :
7 : /* defines */
8 : #define NUMROWS 5
9 :
10 : /* Testing: Test order of ct_results() */
11 10 : TEST_MAIN()
12 : {
13 : CS_CONTEXT *ctx;
14 : CS_CONNECTION *conn;
15 : CS_COMMAND *cmd;
16 10 : int i, verbose = 0;
17 :
18 : CS_RETCODE results_ret;
19 :
20 : char query[1024];
21 10 : CS_INT insert_results[] = { CS_CMD_SUCCEED, CS_CMD_DONE };
22 10 : CS_INT update_results[] = { CS_CMD_SUCCEED, CS_CMD_DONE };
23 10 : CS_INT select_results[] = { CS_ROW_RESULT, CS_CMD_DONE };
24 :
25 10 : printf("%s: Check ordering of returns from cs_results()\n", __FILE__);
26 : if (verbose) {
27 : printf("Trying login\n");
28 : }
29 10 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
30 :
31 10 : check_call(run_command, (cmd, "CREATE TABLE #t0004 (id int)"));
32 60 : for (i = 0; i < NUMROWS; i++) {
33 50 : sprintf(query, "INSERT #t0004 (id) VALUES (%d)", i);
34 :
35 50 : check_call(ct_command, (cmd, CS_LANG_CMD, query, CS_NULLTERM, CS_UNUSED));
36 50 : check_call(ct_send, (cmd));
37 :
38 50 : results_ret = do_results(cmd, insert_results);
39 50 : switch ((int) results_ret) {
40 : case CS_END_RESULTS:
41 : break;
42 0 : case CS_FAIL:
43 0 : fprintf(stderr, "ct_results() failed.\n");
44 0 : return 1;
45 : break;
46 0 : default:
47 0 : fprintf(stderr, "ct_results() unexpected return.\n");
48 0 : return 1;
49 : }
50 : }
51 :
52 10 : check_call(ct_command, (cmd, CS_LANG_CMD, "UPDATE #t0004 SET id = id + 1", CS_NULLTERM, CS_UNUSED));
53 10 : check_call(ct_send, (cmd));
54 :
55 10 : results_ret = do_results(cmd, update_results);
56 10 : switch ((int) results_ret) {
57 : case CS_END_RESULTS:
58 : break;
59 0 : case CS_FAIL:
60 0 : fprintf(stderr, "ct_results() failed.\n");
61 0 : return 1;
62 : break;
63 0 : default:
64 0 : fprintf(stderr, "ct_results() unexpected return.\n");
65 0 : return 1;
66 : }
67 :
68 : /* single row select */
69 10 : check_call(ct_command, (cmd, CS_LANG_CMD, "SELECT * FROM #t0004 WHERE id = 1", CS_NULLTERM, CS_UNUSED));
70 10 : check_call(ct_send, (cmd));
71 :
72 10 : results_ret = do_results(cmd, select_results);
73 10 : switch ((int) results_ret) {
74 : case CS_END_RESULTS:
75 : break;
76 0 : case CS_FAIL:
77 0 : fprintf(stderr, "ct_results() failed.\n");
78 0 : return 1;
79 : break;
80 0 : default:
81 0 : fprintf(stderr, "ct_results() unexpected return.\n");
82 0 : return 1;
83 : }
84 : if (verbose) {
85 : printf("Trying logout\n");
86 : }
87 10 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
88 :
89 10 : return 0;
90 : }
91 :
92 : static int
93 10 : do_fetch(CS_COMMAND * cmd)
94 : {
95 10 : CS_INT count, row_count = 0;
96 : CS_RETCODE ret;
97 :
98 30 : while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
99 10 : row_count += count;
100 : }
101 10 : if (ret == CS_ROW_FAIL) {
102 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
103 0 : return 1;
104 10 : } else if (ret == CS_END_DATA) {
105 : return 0;
106 : } else {
107 0 : fprintf(stderr, "ct_fetch() unexpected return %d on row %d.\n", ret, row_count);
108 0 : return 1;
109 : }
110 : }
111 :
112 : static CS_RETCODE
113 70 : do_results(CS_COMMAND * cmd, CS_INT * results)
114 : {
115 : int result_num;
116 : CS_RETCODE results_ret, result_type;
117 70 : bool done = false;
118 :
119 70 : result_num = 0;
120 280 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
121 140 : printf("result_ret %d result_type %d\n", results_ret, result_type);
122 140 : if (result_type == CS_STATUS_RESULT)
123 0 : continue;
124 140 : if (done) {
125 0 : fputs("No further results were expected\n", stderr);
126 0 : return CS_FAIL;
127 : }
128 140 : if (result_type != results[result_num]) {
129 0 : fprintf(stderr, "ct_results() expected %d received %d\n", results[result_num], result_type);
130 0 : return CS_FAIL;
131 : }
132 140 : switch ((int) result_type) {
133 10 : case CS_ROW_RESULT:
134 10 : if (do_fetch(cmd)) {
135 : return CS_FAIL;
136 : }
137 : break;
138 70 : case CS_CMD_DONE:
139 70 : done = true;
140 70 : break;
141 : }
142 140 : result_num++;
143 : }
144 70 : if ( !done ) {
145 0 : fputs("Never saw CS_CMD_DONE\n", stderr);
146 0 : return CS_FAIL;
147 : }
148 : return results_ret;
149 : }
|