Line data Source code
1 : #include "common.h"
2 :
3 : /* Testing: Retrieving SQL_VARIANT */
4 : int
5 8 : main(void)
6 : {
7 : CS_CONTEXT *ctx;
8 : CS_CONNECTION *conn;
9 : CS_COMMAND *cmd;
10 8 : int verbose = 0;
11 :
12 : CS_RETCODE ret;
13 : CS_RETCODE results_ret;
14 : CS_INT result_type;
15 : CS_INT num_cols;
16 :
17 : CS_DATAFMT datafmt;
18 : CS_INT datalength;
19 : CS_SMALLINT ind;
20 8 : CS_INT count, row_count = 0;
21 :
22 : CS_CHAR select[1024];
23 :
24 : CS_CHAR col1[128];
25 : const char *expected[10];
26 8 : unsigned num_expected = 0;
27 :
28 8 : unsigned rows = 0;
29 :
30 8 : memset(expected, 0, sizeof(expected));
31 :
32 8 : printf("%s: Retrieve SQL_VARIANT column\n", __FILE__);
33 : if (verbose) {
34 : printf("Trying login\n");
35 : }
36 8 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
37 :
38 8 : strcpy(select, "CREATE TABLE #ctlib0009 (n int, col1 sql_variant null)");
39 :
40 8 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
41 :
42 8 : check_call(ct_send, (cmd));
43 :
44 8 : check_call(ct_results, (cmd, &result_type));
45 :
46 8 : switch (result_type) {
47 2 : case CS_CMD_FAIL:
48 2 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL, probably not MSSQL.\n");
49 2 : try_ctlogout(ctx, conn, cmd, verbose);
50 2 : return 0;
51 : case CS_CMD_SUCCEED:
52 : break;
53 0 : default:
54 0 : fprintf(stderr, "ct_results() unexpected return %d.\n", result_type);
55 0 : try_ctlogout(ctx, conn, cmd, verbose);
56 0 : return 1;
57 : }
58 :
59 6 : check_call(run_command, (cmd, "insert into #ctlib0009 values (1, 123)"));
60 6 : expected[num_expected++] = "123";
61 :
62 6 : check_call(run_command, (cmd, "insert into #ctlib0009 values (2, NULL)"));
63 6 : expected[num_expected++] = "";
64 :
65 6 : check_call(run_command, (cmd, "insert into #ctlib0009 values (3, 'hello')"));
66 6 : expected[num_expected++] = "hello";
67 :
68 6 : check_call(run_command, (cmd, "insert into #ctlib0009 values (4, 123.456)"));
69 6 : expected[num_expected++] = "123.456";
70 :
71 6 : strcpy(select, "select col1 from #ctlib0009 order by n");
72 :
73 6 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
74 :
75 6 : check_call(ct_send, (cmd));
76 :
77 6 : ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) servermsg_cb);
78 6 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
79 12 : printf("ct_results returned %s type\n", res_type_str(result_type));
80 12 : switch ((int) result_type) {
81 : case CS_CMD_SUCCEED:
82 : break;
83 : case CS_CMD_DONE:
84 : break;
85 0 : case CS_CMD_FAIL:
86 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
87 0 : return 1;
88 6 : case CS_ROW_RESULT:
89 6 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
90 6 : if (num_cols != 1) {
91 0 : fprintf(stderr, "num_cols %d != 1", num_cols);
92 0 : return 1;
93 : }
94 :
95 6 : check_call(ct_describe, (cmd, 1, &datafmt));
96 6 : datafmt.format = CS_FMT_UNUSED;
97 6 : if (datafmt.maxlength > sizeof(col1)) {
98 6 : datafmt.maxlength = sizeof(col1);
99 : }
100 6 : check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
101 :
102 36 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
103 6 : || (ret == CS_ROW_FAIL)) {
104 24 : row_count += count;
105 24 : if (ret == CS_ROW_FAIL) {
106 0 : fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
107 0 : return 1;
108 : } else { /* ret == CS_SUCCEED */
109 24 : col1[datalength] = 0;
110 24 : printf("col1 = %s\n", col1);
111 24 : assert(strcmp(col1, expected[rows]) == 0);
112 24 : ++rows;
113 : }
114 : }
115 :
116 :
117 6 : switch ((int) ret) {
118 : case CS_END_DATA:
119 : break;
120 0 : case CS_FAIL:
121 0 : fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
122 0 : return 1;
123 0 : default:
124 0 : fprintf(stderr, "ct_fetch() unexpected return.\n");
125 0 : return 1;
126 : }
127 : break;
128 :
129 0 : default:
130 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
131 0 : return 1;
132 : }
133 : }
134 6 : switch ((int) results_ret) {
135 : case CS_END_RESULTS:
136 : break;
137 0 : case CS_FAIL:
138 0 : fprintf(stderr, "ct_results() failed.\n");
139 0 : return 1;
140 : break;
141 0 : default:
142 0 : fprintf(stderr, "ct_results() unexpected return.\n");
143 0 : return 1;
144 : }
145 :
146 6 : if (rows != 4) {
147 0 : fprintf(stderr, "wrong number of rows: normal %u, expected 4\n", rows);
148 0 : return 1;
149 : }
150 :
151 : if (verbose) {
152 : printf("Trying logout\n");
153 : }
154 6 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
155 :
156 6 : return 0;
157 : }
|