Line data Source code
1 : #include "common.h"
2 :
3 : /* Testing: Retrieve CS_TEXT_TYPE using ct_bind() */
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[4];
17 : CS_INT datalength[4];
18 : CS_SMALLINT ind[4];
19 10 : CS_INT count, row_count = 0;
20 :
21 : CS_CHAR name[4][1024];
22 :
23 10 : name[0][0] = 0;
24 10 : name[1][0] = 0;
25 10 : name[2][0] = 0;
26 10 : name[3][0] = 0;
27 :
28 10 : printf("%s: Retrieve CS_CHAR_TYPE using ct_bind()\n", __FILE__);
29 : if (verbose) {
30 : printf("Trying login\n");
31 : }
32 10 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
33 :
34 10 : check_call(ct_command, (cmd, CS_LANG_CMD, "SELECT CONVERT(VARCHAR(7),'1234') AS test, "
35 : "CONVERT(VARCHAR(7),'') AS test2, CONVERT(VARCHAR(7),NULL) AS test3, "
36 : "CONVERT(NUMERIC(38,2), 123.45) as test4", CS_NULLTERM, CS_UNUSED));
37 10 : check_call(ct_send, (cmd));
38 10 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
39 20 : switch ((int) result_type) {
40 : case CS_CMD_SUCCEED:
41 : break;
42 : case CS_CMD_DONE:
43 : break;
44 0 : case CS_CMD_FAIL:
45 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
46 0 : return 1;
47 10 : case CS_ROW_RESULT:
48 10 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
49 10 : if (num_cols != 4) {
50 0 : fprintf(stderr, "num_cols %d != 4", num_cols);
51 0 : return 1;
52 : }
53 10 : check_call(ct_describe, (cmd, 1, &datafmt[0]));
54 10 : datafmt[0].format = CS_FMT_NULLTERM;
55 10 : ++datafmt[0].maxlength;
56 10 : if (datafmt[0].maxlength > 1024) {
57 0 : datafmt[0].maxlength = 1024;
58 : }
59 :
60 10 : check_call(ct_describe, (cmd, 2, &datafmt[1]));
61 10 : datafmt[1].format = CS_FMT_NULLTERM;
62 10 : ++datafmt[1].maxlength;
63 10 : if (datafmt[1].maxlength > 1024) {
64 0 : datafmt[1].maxlength = 1024;
65 : }
66 :
67 10 : check_call(ct_describe, (cmd, 3, &datafmt[2]));
68 10 : datafmt[2].format = CS_FMT_NULLTERM;
69 10 : ++datafmt[2].maxlength;
70 10 : if (datafmt[2].maxlength > 1024) {
71 0 : datafmt[2].maxlength = 1024;
72 : }
73 :
74 10 : check_call(ct_describe, (cmd, 4, &datafmt[3]));
75 10 : datafmt[3].format = CS_FMT_NULLTERM;
76 10 : if (datafmt[3].maxlength != sizeof(CS_NUMERIC)) {
77 0 : fprintf(stderr, "wrong maxlength for numeric\n");
78 0 : return 1;
79 : }
80 10 : ++datafmt[3].maxlength;
81 : if (datafmt[3].maxlength > 1024) {
82 : datafmt[3].maxlength = 1024;
83 : }
84 :
85 10 : check_call(ct_bind, (cmd, 1, &datafmt[0], name[0], &datalength[0], &ind[0]));
86 10 : check_call(ct_bind, (cmd, 2, &datafmt[1], name[1], &datalength[1], &ind[1]));
87 10 : check_call(ct_bind, (cmd, 3, &datafmt[2], name[2], &datalength[2], &ind[2]));
88 10 : check_call(ct_bind, (cmd, 4, &datafmt[3], name[3], &datalength[3], &ind[3]));
89 :
90 10 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
91 10 : || (ret == CS_ROW_FAIL)) {
92 10 : row_count += count;
93 10 : if (ret == CS_ROW_FAIL) {
94 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
95 0 : return 1;
96 : } else { /* ret == CS_SUCCEED */
97 : if (verbose) {
98 : printf("name = '%s'\n", name[0]);
99 : }
100 10 : if (ind[0] != 0) {
101 0 : fprintf(stderr, "Returned NULL\n");
102 0 : return 1;
103 : }
104 10 : if (strcmp(name[0], "1234")) {
105 0 : fprintf(stderr, "Bad return:\n'%s'\n! =\n'%s'\n", name[0], "1234");
106 0 : return 1;
107 : }
108 10 : if (datalength[0] != strlen(name[0]) + 1) {
109 0 : fprintf(stderr, "Bad length:\n'%ld'\n! =\n'%d'\n", (long) strlen(name[0]) + 1, datalength[0]);
110 0 : return 1;
111 : }
112 10 : if (ind[1] != 0) {
113 0 : fprintf(stderr, "Returned NULL\n");
114 0 : return 1;
115 : }
116 : /* empty are retunerd as a single space in TDS4.x and TDS5 */
117 10 : if (strcmp(name[1], "") && strcmp(name[1], " ")) {
118 0 : fprintf(stderr, "Bad return:\n'%s'\n! =\n'%s'\n", name[1], "");
119 0 : return 1;
120 : }
121 10 : if (datalength[1] != strlen(name[1]) + 1) {
122 0 : fprintf(stderr, "Col 2 bad length:\n'%ld'\n! =\n'%d'\n", (long) strlen(name[1]) + 1, datalength[1]);
123 0 : return 1;
124 : }
125 10 : if (ind[2] == 0) {
126 0 : fprintf(stderr, "Col 3 returned not NULL (ind %d len %d)\n", (int) ind[2], (int) datalength[2]);
127 0 : return 1;
128 : }
129 10 : if (strcmp(name[2], "")) {
130 0 : fprintf(stderr, "Col 3 bad return:\n'%s'\n! =\n'%s'\n", name[2], "");
131 0 : return 1;
132 : }
133 10 : if (datalength[2] != 0) {
134 0 : fprintf(stderr, "Col 3 bad length:\n'%ld'\n! =\n'%d'\n", (long) 0, datalength[2]);
135 0 : return 1;
136 : }
137 : }
138 : }
139 10 : switch ((int) ret) {
140 : case CS_END_DATA:
141 : break;
142 0 : case CS_FAIL:
143 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
144 0 : return 1;
145 0 : default:
146 0 : fprintf(stderr, "ct_fetch() unexpected return.\n");
147 0 : return 1;
148 : }
149 : break;
150 0 : case CS_COMPUTE_RESULT:
151 0 : fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
152 0 : return 1;
153 0 : default:
154 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
155 0 : return 1;
156 : }
157 : }
158 10 : switch ((int) results_ret) {
159 : case CS_END_RESULTS:
160 : break;
161 0 : case CS_FAIL:
162 0 : fprintf(stderr, "ct_results() failed.\n");
163 0 : return 1;
164 : break;
165 0 : default:
166 0 : fprintf(stderr, "ct_results() unexpected return.\n");
167 0 : return 1;
168 : }
169 :
170 : if (verbose) {
171 : printf("Trying logout\n");
172 : }
173 10 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
174 :
175 10 : return 0;
176 : }
|