Line data Source code
1 : #include "common.h"
2 :
3 : /* Testing: Retrieving SQL_VARIANT */
4 : int
5 10 : main(void)
6 : {
7 : CS_CONTEXT *ctx;
8 : CS_CONNECTION *conn;
9 : CS_COMMAND *cmd;
10 10 : 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 10 : CS_INT count, row_count = 0;
21 :
22 : CS_CHAR select[1024];
23 :
24 : CS_CHAR col1[128];
25 : const char *expected[10];
26 10 : unsigned num_expected = 0;
27 :
28 10 : unsigned rows = 0;
29 :
30 10 : memset(expected, 0, sizeof(expected));
31 :
32 10 : printf("%s: Retrieve SQL_VARIANT column\n", __FILE__);
33 : if (verbose) {
34 : printf("Trying login\n");
35 : }
36 10 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
37 :
38 10 : strcpy(select, "CREATE TABLE #ctlib0009 (n int, col1 sql_variant null)");
39 :
40 10 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
41 :
42 10 : check_call(ct_send, (cmd));
43 :
44 10 : check_call(ct_results, (cmd, &result_type));
45 :
46 10 : 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 8 : check_call(run_command, (cmd, "insert into #ctlib0009 values (1, 123)"));
60 8 : expected[num_expected++] = "123";
61 :
62 8 : check_call(run_command, (cmd, "insert into #ctlib0009 values (2, NULL)"));
63 8 : expected[num_expected++] = "";
64 :
65 8 : check_call(run_command, (cmd, "insert into #ctlib0009 values (3, 'hello')"));
66 8 : expected[num_expected++] = "hello";
67 :
68 8 : check_call(run_command, (cmd, "insert into #ctlib0009 values (4, 123.456)"));
69 8 : expected[num_expected++] = "123.456";
70 :
71 8 : strcpy(select, "select col1 from #ctlib0009 order by n");
72 :
73 8 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
74 :
75 8 : check_call(ct_send, (cmd));
76 :
77 8 : ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) servermsg_cb);
78 8 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
79 16 : printf("ct_results returned %s type\n", res_type_str(result_type));
80 16 : 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 8 : case CS_ROW_RESULT:
89 8 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
90 8 : if (num_cols != 1) {
91 0 : fprintf(stderr, "num_cols %d != 1", num_cols);
92 0 : return 1;
93 : }
94 :
95 8 : check_call(ct_describe, (cmd, 1, &datafmt));
96 8 : datafmt.format = CS_FMT_UNUSED;
97 8 : if (datafmt.maxlength > sizeof(col1)) {
98 8 : datafmt.maxlength = sizeof(col1);
99 : }
100 8 : check_call(ct_bind, (cmd, 1, &datafmt, col1, &datalength, &ind));
101 :
102 48 : while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
103 8 : || (ret == CS_ROW_FAIL)) {
104 32 : row_count += count;
105 32 : 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 32 : col1[datalength] = 0;
110 32 : printf("col1 = %s\n", col1);
111 32 : assert(strcmp(col1, expected[rows]) == 0);
112 32 : ++rows;
113 : }
114 : }
115 :
116 :
117 8 : 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 8 : 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 8 : 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 8 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
155 :
156 8 : return 0;
157 : }
|