Line data Source code
1 : #include "common.h"
2 :
3 : /* Testing: data truncation behavior of ct_fetch */
4 : int
5 8 : main(int argc, char *argv[])
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 8 : CS_INT copied = 0;
19 8 : CS_SMALLINT ind = 0;
20 8 : CS_INT count, row_count = 0;
21 :
22 : CS_CHAR select[1024];
23 :
24 8 : char *addr = NULL;
25 :
26 8 : printf("%s: test data truncation behavior of ct_fetch \n", __FILE__);
27 : if (verbose) {
28 : printf("Trying login\n");
29 : }
30 8 : check_call(try_ctlogin_with_options, (argc, argv, &ctx, &conn, &cmd, verbose));
31 8 : verbose += common_pwd.fverbose;
32 :
33 8 : strcpy(select, "select name from systypes where datalength(name) > 2*9 order by datalength(name)");
34 8 : printf("%s\n", select);
35 :
36 8 : check_call(ct_command, (cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED));
37 :
38 8 : check_call(ct_send, (cmd));
39 :
40 32 : while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
41 16 : switch ((int) result_type) {
42 : case CS_CMD_SUCCEED:
43 : break;
44 : case CS_CMD_DONE:
45 : break;
46 0 : case CS_CMD_FAIL:
47 0 : fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
48 0 : free(addr);
49 0 : return 1;
50 8 : case CS_ROW_RESULT:
51 :
52 8 : check_call(ct_res_info, (cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL));
53 8 : fprintf(stderr, "%d column%s\n", num_cols, num_cols==1? "":"s");
54 :
55 8 : check_call(ct_describe, (cmd, 1, &datafmt));
56 :
57 8 : fprintf(stderr, "type = %d\n", datafmt.datatype);
58 8 : fprintf(stderr, "maxlength = %d\n", datafmt.maxlength);
59 8 : if (datafmt.datatype == CS_CHAR_TYPE) {
60 8 : fprintf(stderr, "CS_CHAR_TYPE\n");
61 8 : datafmt.format = CS_FMT_NULLTERM;
62 8 : addr = (char *) malloc(datafmt.maxlength);
63 : }
64 :
65 8 : fprintf(stderr, "binding column 1 (%s)\n", datafmt.name);
66 :
67 : /* set maxlength to something short to test truncation behavior */
68 8 : if (common_pwd.maxlength)
69 0 : datafmt.maxlength = common_pwd.maxlength;
70 :
71 8 : check_call(ct_bind, (cmd, 1, &datafmt, addr, &copied, &ind));
72 :
73 8 : fprintf(stderr, "fetching rows with datafmt.maxlength = %d\n", datafmt.maxlength);
74 :
75 8 : while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) != CS_END_DATA) {
76 :
77 28 : fprintf(stderr, "ct_fetch() row %d returned %d.\n", row_count, (int) ret);
78 28 : addr[copied] = '\0';
79 28 : fprintf(stderr, "copied %d bytes: [%s]\n", copied, addr);
80 28 : row_count += count;
81 :
82 28 : switch (ret) {
83 28 : case CS_SUCCEED:
84 28 : printf("ct_fetch returned %d row%s\n", count, count==1? "":"s");
85 28 : break;
86 0 : case CS_ROW_FAIL:
87 0 : fprintf(stderr, "error: ct_fetch() returned CS_ROW_FAIL on row %d.\n", row_count);
88 0 : free(addr);
89 0 : return 1;
90 0 : case CS_CANCELED:
91 0 : fprintf(stderr, "error: ct_fetch() returned CS_CANCELED??\n");
92 0 : free(addr);
93 0 : return 1;
94 0 : case CS_FAIL:
95 0 : fprintf(stderr, "error: ct_fetch() returned CS_FAIL.\n");
96 0 : free(addr);
97 0 : return 1;
98 0 : default:
99 0 : fprintf(stderr, "error: ct_fetch() unexpected return.\n");
100 0 : free(addr);
101 0 : return 1;
102 : }
103 : }
104 :
105 : break;
106 : }
107 : }
108 :
109 8 : switch ((int) results_ret) {
110 : case CS_END_RESULTS:
111 : break;
112 0 : case CS_FAIL:
113 0 : fprintf(stderr, "ct_results() failed.\n");
114 0 : free(addr);
115 0 : return 1;
116 : break;
117 0 : default:
118 0 : fprintf(stderr, "ct_results() unexpected return.\n");
119 0 : free(addr);
120 0 : return 1;
121 : }
122 :
123 8 : if (verbose) {
124 0 : printf("Trying logout\n");
125 : }
126 8 : check_call(try_ctlogout, (ctx, conn, cmd, verbose));
127 :
128 8 : free(addr);
129 8 : return 0;
130 : }
|