Line data Source code
1 : #include "common.h"
2 :
3 : #include <stdarg.h>
4 :
5 : static bool verbose = false;
6 :
7 : static CS_CONTEXT *ctx;
8 : static CS_CONNECTION *conn;
9 : static CS_COMMAND *cmd;
10 : static CS_COMMAND *cmd2;
11 :
12 : static void
13 10 : cleanup(void)
14 : {
15 10 : if (verbose) {
16 0 : printf("Trying logout\n");
17 : }
18 :
19 10 : if (cmd2)
20 10 : ct_cmd_drop(cmd2);
21 :
22 10 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
23 10 : }
24 :
25 : static void
26 542 : chk(int check, const char *fmt, ...)
27 : {
28 : va_list ap;
29 :
30 542 : if (check)
31 542 : return;
32 :
33 0 : va_start(ap, fmt);
34 0 : vfprintf(stderr, fmt, ap);
35 0 : va_end(ap);
36 :
37 0 : cleanup();
38 0 : exit(1);
39 : }
40 :
41 : enum
42 : { BIG_SIZE = 20000 };
43 :
44 : static void
45 84 : consume_results(CS_COMMAND *command, const char *what)
46 : {
47 : CS_RETCODE ret;
48 : CS_INT res_type;
49 :
50 320 : while ((ret = ct_results(command, &res_type)) == CS_SUCCEED) {
51 152 : switch ((int) res_type) {
52 : case CS_CMD_SUCCEED:
53 : case CS_CMD_DONE:
54 : break;
55 0 : default:
56 0 : chk(0, "%s: unexpected ct_results result type: %d\n", what, (int) res_type);
57 : }
58 : }
59 84 : chk(ret == CS_END_RESULTS, "%s: ct_results() unexpected return %d.\n", what, (int) ret);
60 84 : }
61 :
62 : /*
63 : * Prepare an INSERT with a blob parameter and execute it with a mix
64 : * of NULL and not NULL values, then check what got stored.
65 : */
66 : static void
67 14 : test_blob_param(const char *sql_type, CS_INT param_type)
68 : {
69 : enum
70 : { NUM_ROWS = 4 };
71 : static const int sizes[NUM_ROWS] = { -1, 5, -1, BIG_SIZE };
72 : char sql[256];
73 : char *data, *out;
74 : CS_DATAFMT datafmt;
75 : CS_INT id, id_len, data_len;
76 : CS_SMALLINT id_ind, data_ind;
77 : CS_RETCODE ret;
78 : CS_INT res_type, count;
79 14 : int i, rows = 0;
80 :
81 14 : printf("Testing blob parameter of type %s\n", sql_type);
82 :
83 14 : data = (char *) malloc(BIG_SIZE);
84 14 : out = (char *) malloc(BIG_SIZE);
85 14 : chk(data != NULL && out != NULL, "out of memory\n");
86 14 : memset(data, 'x', BIG_SIZE);
87 :
88 14 : run_command(cmd, "IF OBJECT_ID('tempdb..#ct_dynamic_blob') IS NOT NULL DROP TABLE #ct_dynamic_blob");
89 14 : sprintf(sql, "CREATE TABLE #ct_dynamic_blob (id INT NOT NULL, t %s NULL)", sql_type);
90 14 : check_call(run_command, (cmd, sql));
91 :
92 14 : check_call(ct_dynamic, (cmd, CS_PREPARE, "blob", CS_NULLTERM,
93 : "INSERT INTO #ct_dynamic_blob (id, t) VALUES (?, ?)", CS_NULLTERM));
94 14 : check_call(ct_send, (cmd));
95 14 : consume_results(cmd, "prepare");
96 :
97 70 : for (i = 0; i < NUM_ROWS; i++) {
98 56 : check_call(ct_dynamic, (cmd, CS_EXECUTE, "blob", CS_NULLTERM, NULL, CS_UNUSED));
99 :
100 56 : id = i + 1;
101 56 : id_len = sizeof(id);
102 56 : id_ind = 0;
103 56 : memset(&datafmt, 0, sizeof(datafmt));
104 56 : datafmt.datatype = CS_INT_TYPE;
105 56 : datafmt.status = CS_INPUTVALUE;
106 56 : check_call(ct_setparam, (cmd, &datafmt, &id, &id_len, &id_ind));
107 :
108 56 : data_len = sizes[i] < 0 ? 0 : sizes[i];
109 56 : data_ind = sizes[i] < 0 ? -1 : 0;
110 56 : memset(&datafmt, 0, sizeof(datafmt));
111 56 : datafmt.datatype = param_type;
112 56 : datafmt.status = CS_INPUTVALUE;
113 56 : datafmt.maxlength = BIG_SIZE;
114 56 : check_call(ct_setparam, (cmd, &datafmt, data, &data_len, &data_ind));
115 :
116 56 : check_call(ct_send, (cmd));
117 56 : consume_results(cmd, "execute");
118 : }
119 :
120 14 : check_call(ct_dynamic, (cmd, CS_DEALLOC, "blob", CS_NULLTERM, NULL, CS_UNUSED));
121 14 : check_call(ct_send, (cmd));
122 14 : consume_results(cmd, "deallocate");
123 :
124 : /*
125 : * Check stored values.
126 : * Fetch the column itself as ASE 15.0 returns 0 for DATALENGTH of a
127 : * NULL TEXT and does not accept IS NULL on it.
128 : */
129 14 : check_call(ct_command, (cmd, CS_LANG_CMD, "SET TEXTSIZE 65536 SELECT id, t FROM #ct_dynamic_blob ORDER BY id",
130 : CS_NULLTERM, CS_UNUSED));
131 14 : check_call(ct_send, (cmd));
132 84 : while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
133 56 : switch ((int) res_type) {
134 : case CS_CMD_SUCCEED:
135 : case CS_CMD_DONE:
136 : break;
137 14 : case CS_ROW_RESULT:
138 14 : memset(&datafmt, 0, sizeof(datafmt));
139 14 : datafmt.datatype = CS_INT_TYPE;
140 14 : datafmt.maxlength = sizeof(CS_INT);
141 14 : datafmt.count = 1;
142 14 : check_call(ct_bind, (cmd, 1, &datafmt, &id, NULL, &id_ind));
143 14 : memset(&datafmt, 0, sizeof(datafmt));
144 14 : datafmt.datatype = param_type;
145 14 : datafmt.maxlength = BIG_SIZE;
146 14 : datafmt.count = 1;
147 14 : check_call(ct_bind, (cmd, 2, &datafmt, out, &data_len, &data_ind));
148 14 : memset(out, 0, BIG_SIZE);
149 84 : while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
150 56 : chk(rows < NUM_ROWS && id == rows + 1, "%s: unexpected row id %d\n", sql_type, (int) id);
151 56 : if (sizes[rows] < 0)
152 28 : chk(data_ind == -1, "%s: row %d expected NULL\n", sql_type, (int) id);
153 : else
154 28 : chk(data_ind == 0 && data_len == sizes[rows] && memcmp(out, data, data_len) == 0,
155 : "%s: row %d wrong data, length %d expected %d\n", sql_type, (int) id,
156 : (int) data_len, sizes[rows]);
157 56 : memset(out, 0, BIG_SIZE);
158 56 : ++rows;
159 : }
160 14 : chk(ret == CS_END_DATA, "ct_fetch() unexpected return %d.\n", (int) ret);
161 14 : break;
162 0 : default:
163 0 : chk(0, "invalid ct_results result type: %d\n", (int) res_type);
164 : }
165 : }
166 14 : chk(ret == CS_END_RESULTS, "ct_results() unexpected return %d.\n", (int) ret);
167 14 : chk(rows == NUM_ROWS, "%s: got %d rows, expected %d\n", sql_type, rows, NUM_ROWS);
168 :
169 14 : check_call(run_command, (cmd, "DROP TABLE #ct_dynamic_blob"));
170 14 : free(out);
171 14 : free(data);
172 14 : }
173 :
174 10 : TEST_MAIN()
175 : {
176 : CS_RETCODE ret;
177 : CS_RETCODE results_ret;
178 : CS_CHAR cmdbuf[4096];
179 : CS_CHAR name[257];
180 : CS_INT datalength;
181 : CS_SMALLINT ind;
182 : CS_INT count;
183 : CS_INT num_cols;
184 10 : CS_INT row_count = 0;
185 : CS_DATAFMT datafmt;
186 : CS_DATAFMT descfmt;
187 : CS_INT intvar;
188 : CS_INT intvarsize;
189 : CS_SMALLINT intvarind;
190 : CS_INT res_type;
191 : CS_INT tds_version;
192 : bool test_blobs;
193 :
194 : int i;
195 :
196 10 : if (argc > 1 && (0 == strcmp(argv[1], "-v")))
197 0 : verbose = true;
198 :
199 10 : printf("%s: use ct_dynamic to prepare and execute a statement\n", __FILE__);
200 10 : if (verbose) {
201 0 : printf("Trying login\n");
202 : }
203 10 : check_call(try_ctlogin, (&ctx, &conn, &cmd, verbose));
204 10 : error_to_stdout = true;
205 :
206 10 : check_call(ct_cmd_alloc, (conn, &cmd2));
207 :
208 : /* do not test error */
209 10 : run_command(cmd, "IF OBJECT_ID('tempdb..#ct_dynamic') IS NOT NULL DROP table #ct_dynamic");
210 :
211 10 : strcpy(cmdbuf, "create table #ct_dynamic (id numeric identity not null, \
212 : name varchar(30), age int, cost money, bdate datetime, fval float) ");
213 :
214 10 : check_call(run_command, (cmd, cmdbuf));
215 :
216 10 : strcpy(cmdbuf, "insert into #ct_dynamic ( name , age , cost , bdate , fval ) ");
217 10 : strcat(cmdbuf, "values ('Bill', 44, 2000.00, 'May 21 1960', 60.97 ) ");
218 :
219 10 : check_call(run_command, (cmd, cmdbuf));
220 :
221 10 : strcpy(cmdbuf, "insert into #ct_dynamic ( name , age , cost , bdate , fval ) ");
222 10 : strcat(cmdbuf, "values ('Freddy', 32, 1000.00, 'Jan 21 1972', 70.97 ) ");
223 :
224 10 : check_call(run_command, (cmd, cmdbuf));
225 :
226 10 : strcpy(cmdbuf, "insert into #ct_dynamic ( name , age , cost , bdate , fval ) ");
227 10 : strcat(cmdbuf, "values ('James', 42, 5000.00, 'May 21 1962', 80.97 ) ");
228 :
229 10 : check_call(run_command, (cmd, cmdbuf));
230 :
231 10 : strcpy(cmdbuf, "select name from #ct_dynamic where age = ?");
232 :
233 10 : check_call(ct_dynamic, (cmd, CS_PREPARE, "age", CS_NULLTERM, cmdbuf, CS_NULLTERM));
234 :
235 10 : check_call(ct_send, (cmd));
236 :
237 40 : while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
238 20 : switch ((int) res_type) {
239 :
240 : case CS_CMD_SUCCEED:
241 : case CS_CMD_DONE:
242 : break;
243 :
244 : case CS_CMD_FAIL:
245 : break;
246 :
247 0 : default:
248 0 : chk(0, "invalid ct_results result type: %d\n", res_type);
249 : }
250 : }
251 10 : chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);
252 :
253 10 : check_call(ct_dynamic, (cmd, CS_DESCRIBE_INPUT, "age", CS_NULLTERM, NULL, CS_UNUSED));
254 :
255 10 : check_call(ct_send, (cmd));
256 :
257 40 : while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
258 20 : switch ((int) res_type) {
259 :
260 10 : case CS_DESCRIBE_RESULT:
261 10 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
262 :
263 12 : for (i = 1; i <= num_cols; i++) {
264 2 : check_call(ct_describe, (cmd, i, &descfmt));
265 2 : fprintf(stderr, "CS_DESCRIBE_INPUT parameter %d :\n", i);
266 2 : if (descfmt.namelen == 0)
267 2 : fprintf(stderr, "\t\tNo name...\n");
268 : else
269 0 : fprintf(stderr, "\t\tName = %*.*s\n", descfmt.namelen, descfmt.namelen, descfmt.name);
270 2 : fprintf(stderr, "\t\tType = %d\n", descfmt.datatype);
271 2 : fprintf(stderr, "\t\tLength = %d\n", descfmt.maxlength);
272 : }
273 : break;
274 :
275 : case CS_CMD_SUCCEED:
276 : case CS_CMD_DONE:
277 : break;
278 :
279 : case CS_CMD_FAIL:
280 : break;
281 :
282 0 : default:
283 0 : chk(0, "invalid ct_results result type: %d\n", res_type);
284 : }
285 : }
286 :
287 10 : check_call(ct_dynamic, (cmd, CS_DESCRIBE_OUTPUT, "age", CS_NULLTERM, NULL, CS_UNUSED));
288 :
289 10 : check_call(ct_send, (cmd));
290 :
291 40 : while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
292 20 : switch ((int) res_type) {
293 :
294 10 : case CS_DESCRIBE_RESULT:
295 10 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
296 10 : chk(num_cols == 1, "CS_DESCRIBE_OUTPUT showed %d columns , expected 1\n", num_cols);
297 :
298 20 : for (i = 1; i <= num_cols; i++) {
299 10 : check_call(ct_describe, (cmd, i, &descfmt));
300 :
301 10 : if (descfmt.namelen == 0)
302 0 : fprintf(stderr, "\t\tNo name...\n");
303 : else
304 10 : fprintf(stderr, "\t\tName = %*.*s\n", descfmt.namelen, descfmt.namelen, descfmt.name);
305 10 : fprintf(stderr, "\t\tType = %d\n", descfmt.datatype);
306 10 : fprintf(stderr, "\t\tLength = %d\n", descfmt.maxlength);
307 : }
308 : break;
309 :
310 : case CS_CMD_SUCCEED:
311 : case CS_CMD_DONE:
312 : break;
313 :
314 : case CS_CMD_FAIL:
315 : break;
316 :
317 0 : default:
318 0 : chk(0, "invalid ct_results result type: %d\n", res_type);
319 : }
320 : }
321 :
322 : /* execute dynamic on a second command to check it still works */
323 10 : check_call(ct_dynamic, (cmd2, CS_EXECUTE, "age", CS_NULLTERM, NULL, CS_UNUSED));
324 :
325 10 : intvar = 44;
326 10 : intvarsize = 4;
327 10 : intvarind = 0;
328 :
329 10 : datafmt.name[0] = 0;
330 10 : datafmt.namelen = 0;
331 10 : datafmt.datatype = CS_INT_TYPE;
332 10 : datafmt.status = CS_INPUTVALUE;
333 :
334 10 : check_call(ct_setparam, (cmd2, &datafmt, (CS_VOID *) & intvar, &intvarsize, &intvarind));
335 :
336 10 : check_call(ct_send, (cmd2));
337 :
338 10 : while ((results_ret = ct_results(cmd2, &res_type)) == CS_SUCCEED) {
339 40 : chk(res_type != CS_CMD_FAIL, "1: ct_results() result_type CS_CMD_FAIL.\n");
340 40 : chk(res_type != CS_COMPUTE_RESULT, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
341 :
342 40 : switch ((int) res_type) {
343 : case CS_CMD_SUCCEED:
344 : break;
345 : case CS_CMD_DONE:
346 : break;
347 10 : case CS_ROW_RESULT:
348 10 : datafmt.datatype = CS_CHAR_TYPE;
349 10 : datafmt.format = CS_FMT_NULLTERM;
350 10 : datafmt.maxlength = 256;
351 10 : datafmt.count = 1;
352 10 : datafmt.locale = NULL;
353 10 : check_call(ct_bind, (cmd2, 1, &datafmt, name, &datalength, &ind));
354 :
355 30 : while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
356 10 : || (ret == CS_ROW_FAIL)) {
357 10 : row_count += count;
358 10 : chk(ret != CS_ROW_FAIL, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
359 10 : if (ret == CS_SUCCEED) {
360 10 : chk(!strcmp(name, "Bill"), "fetched value '%s' expected 'Bill'\n", name);
361 : } else {
362 : break;
363 : }
364 : }
365 10 : chk(ret == CS_END_DATA, "ct_fetch() unexpected return %d.\n", (int) ret);
366 10 : break;
367 0 : default:
368 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
369 0 : return 1;
370 : }
371 : }
372 10 : chk(results_ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) results_ret);
373 :
374 10 : intvar = 32;
375 :
376 10 : check_call(ct_send, (cmd2));
377 :
378 10 : while ((results_ret = ct_results(cmd2, &res_type)) == CS_SUCCEED) {
379 40 : chk(res_type != CS_CMD_FAIL, "2: ct_results() result_type CS_CMD_FAIL.\n");
380 40 : chk(res_type != CS_COMPUTE_RESULT, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
381 :
382 40 : switch ((int) res_type) {
383 : case CS_CMD_SUCCEED:
384 : break;
385 : case CS_CMD_DONE:
386 : break;
387 10 : case CS_ROW_RESULT:
388 10 : datafmt.datatype = CS_CHAR_TYPE;
389 10 : datafmt.format = CS_FMT_NULLTERM;
390 10 : datafmt.maxlength = 256;
391 10 : datafmt.count = 1;
392 10 : datafmt.locale = NULL;
393 10 : check_call(ct_bind, (cmd2, 1, &datafmt, name, &datalength, &ind));
394 :
395 30 : while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
396 10 : || (ret == CS_ROW_FAIL)) {
397 10 : row_count += count;
398 10 : chk(ret != CS_ROW_FAIL, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
399 :
400 10 : if (ret == CS_SUCCEED) {
401 10 : chk(!strcmp(name, "Freddy"), "fetched value '%s' expected 'Freddy'\n", name);
402 : } else {
403 : break;
404 : }
405 : }
406 10 : chk(ret == CS_END_DATA, "ct_fetch() unexpected return %d.\n", (int) ret);
407 10 : break;
408 0 : default:
409 0 : fprintf(stderr, "ct_results() unexpected result_type.\n");
410 0 : return 1;
411 : }
412 : }
413 10 : chk(results_ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) results_ret);
414 :
415 10 : check_call(ct_dynamic, (cmd, CS_DEALLOC, "age", CS_NULLTERM, NULL, CS_UNUSED));
416 :
417 10 : check_call(ct_send, (cmd));
418 :
419 24 : while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
420 4 : switch ((int) res_type) {
421 :
422 : case CS_CMD_SUCCEED:
423 : case CS_CMD_DONE:
424 : break;
425 :
426 : case CS_CMD_FAIL:
427 : break;
428 :
429 0 : default:
430 0 : chk(0, "invalid ct_results result type: %d\n", res_type);
431 : }
432 : }
433 10 : chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);
434 :
435 : /*
436 : * check we can prepare again dynamic with same name after deallocation
437 : */
438 10 : strcpy(cmdbuf, "select name from #ct_dynamic where age = ?");
439 10 : check_call(ct_dynamic, (cmd, CS_PREPARE, "age", CS_NULLTERM, cmdbuf, CS_NULLTERM));
440 :
441 10 : check_call(ct_send, (cmd));
442 :
443 40 : while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
444 20 : switch ((int) res_type) {
445 :
446 : case CS_CMD_SUCCEED:
447 : case CS_CMD_DONE:
448 : break;
449 :
450 : case CS_CMD_FAIL:
451 : break;
452 :
453 0 : default:
454 0 : chk(0, "invalid ct_results result type: %d\n", res_type);
455 : }
456 : }
457 10 : chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);
458 :
459 10 : ct_dynamic(cmd, CS_DEALLOC, "invalid", CS_NULLTERM, NULL, CS_UNUSED);
460 10 : ct_send(cmd);
461 20 : while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED)
462 0 : chk(res_type != CS_ROW_RESULT, "Rows not expected\n");
463 10 : chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);
464 :
465 : /*
466 : * Blob parameters, mixing NULL and not NULL values.
467 : * Under MSSQL the statement is prepared before parameters are known.
468 : * Before TDS 7.2 they are declared as varchar(4000) so the server
469 : * truncates longer values and rejects IMAGE parameters.
470 : */
471 10 : check_call(ct_con_props, (conn, CS_GET, CS_TDS_VERSION, &tds_version, CS_UNUSED, NULL));
472 10 : test_blobs = true;
473 : #ifdef CS_TDS_71
474 10 : if (tds_version == CS_TDS_70 || tds_version == CS_TDS_71)
475 : test_blobs = false;
476 : #endif
477 : if (test_blobs) {
478 6 : test_blob_param("TEXT", CS_TEXT_TYPE);
479 6 : test_blob_param("IMAGE", CS_IMAGE_TYPE);
480 : /*
481 : * Sybase only. Use text, CS_UNITEXT_TYPE data are client charset
482 : * for FreeTDS but UTF-16 for SAP library.
483 : */
484 6 : if (tds_version == CS_TDS_50)
485 2 : test_blob_param("UNITEXT", CS_TEXT_TYPE);
486 : }
487 :
488 10 : check_call(ct_dynamic, (cmd2, CS_EXECUTE, "age", CS_NULLTERM, NULL, CS_UNUSED));
489 :
490 10 : intvar = 32;
491 10 : intvarsize = 4;
492 10 : intvarind = 0;
493 :
494 10 : datafmt.name[0] = 0;
495 10 : datafmt.namelen = 0;
496 10 : datafmt.datatype = CS_INT_TYPE;
497 10 : datafmt.status = CS_INPUTVALUE;
498 :
499 10 : check_call(ct_setparam, (cmd2, &datafmt, (CS_VOID *) & intvar, &intvarsize, &intvarind));
500 10 : check_call(ct_send, (cmd2));
501 :
502 : /* all tests succeeded */
503 :
504 10 : cleanup();
505 10 : return 0;
506 : }
|