Line data Source code
1 : /*
2 : * Purpose: Test bcp in and out, and specifically bcp_colfmt()
3 : * Functions: bcp_colfmt bcp_columns bcp_exec bcp_init
4 : */
5 :
6 : #include "common.h"
7 :
8 : #include <freetds/bool.h>
9 : #include <freetds/replacements.h>
10 :
11 : static bool failed = false;
12 :
13 : static void
14 0 : failure(const char *fmt, ...)
15 : {
16 : va_list ap;
17 :
18 0 : failed = true;
19 :
20 0 : va_start(ap, fmt);
21 0 : vfprintf(stderr, fmt, ap);
22 0 : va_end(ap);
23 0 : }
24 :
25 : #define INFILE_NAME "t0016"
26 : #define TABLE_NAME "#dblib0016"
27 :
28 : static void test_file(const char *fn);
29 : static bool compare_files(const char *fn1, const char *fn2);
30 : static unsigned count_file_rows(FILE *f);
31 : static size_t fgets_raw(char *s, int len, FILE * f);
32 : static DBPROCESS *dbproc;
33 :
34 10 : TEST_MAIN()
35 : {
36 : LOGINREC *login;
37 : char in_file[30];
38 : unsigned int n;
39 :
40 10 : setbuf(stdout, NULL);
41 10 : setbuf(stderr, NULL);
42 :
43 10 : set_malloc_options();
44 :
45 10 : read_login_info(argc, argv);
46 10 : printf("Starting %s\n", argv[0]);
47 10 : dbsetversion(DBVERSION_100);
48 10 : dbinit();
49 :
50 10 : dberrhandle(syb_err_handler);
51 10 : dbmsghandle(syb_msg_handler);
52 :
53 10 : printf("About to logon\n");
54 :
55 10 : login = dblogin();
56 10 : BCP_SETL(login, TRUE);
57 10 : DBSETLPWD(login, PASSWORD);
58 10 : DBSETLUSER(login, USER);
59 10 : DBSETLAPP(login, "t0016");
60 10 : DBSETLCHARSET(login, "utf8");
61 :
62 10 : dbproc = dbopen(login, SERVER);
63 10 : if (strlen(DATABASE)) {
64 10 : dbuse(dbproc, DATABASE);
65 : }
66 10 : dbloginfree(login);
67 10 : printf("After logon\n");
68 :
69 10 : strcpy(in_file, INFILE_NAME);
70 190 : for (n = 1; n <= 100; ++n) {
71 190 : test_file(in_file);
72 190 : sprintf(in_file, "%s_%d", INFILE_NAME, n);
73 190 : if (sql_reopen(in_file) != SUCCEED)
74 : break;
75 : }
76 :
77 10 : dbclose(dbproc);
78 10 : dbexit();
79 :
80 10 : printf("dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
81 10 : return failed ? 1 : 0;
82 : }
83 :
84 : static bool got_error = false;
85 :
86 : static int
87 46 : ignore_msg_handler(DBPROCESS * dbproc TDS_UNUSED, DBINT msgno TDS_UNUSED, int state TDS_UNUSED, int severity TDS_UNUSED,
88 : char *text TDS_UNUSED, char *server TDS_UNUSED, char *proc TDS_UNUSED, int line TDS_UNUSED)
89 : {
90 46 : got_error = true;
91 46 : return 0;
92 : }
93 :
94 : static int
95 46 : ignore_err_handler(DBPROCESS * dbproc TDS_UNUSED, int severity TDS_UNUSED, int dberr TDS_UNUSED,
96 : int oserr TDS_UNUSED, char *dberrstr TDS_UNUSED, char *oserrstr TDS_UNUSED)
97 : {
98 46 : got_error = true;
99 46 : return INT_CANCEL;
100 : }
101 :
102 : static char line1[1024*16];
103 : static char line2[1024*16];
104 :
105 : static void
106 190 : test_file(const char *fn)
107 : {
108 : int i;
109 : RETCODE ret;
110 190 : int num_cols = 0;
111 190 : const char *out_file = "t0016.out";
112 190 : const char *err_file = "t0016.err";
113 : DBINT rows_copied;
114 190 : unsigned num_rows = 2;
115 : char table_name[64];
116 : char hints[64];
117 :
118 : FILE *input_file;
119 :
120 : char sql_file[256];
121 : char in_file[256];
122 : char exp_file[256];
123 :
124 190 : snprintf(sql_file, sizeof(sql_file), "%s/%s.sql", FREETDS_SRCDIR, fn);
125 190 : snprintf(in_file, sizeof(in_file), "%s/%s.in", FREETDS_SRCDIR, fn);
126 190 : snprintf(exp_file, sizeof(exp_file), "%s/%s.exp", FREETDS_SRCDIR, fn);
127 :
128 190 : strlcpy(table_name, TABLE_NAME, sizeof(table_name));
129 190 : hints[0] = 0;
130 :
131 190 : input_file = fopen(in_file, "r");
132 190 : if (!input_file) {
133 0 : sprintf(in_file, "%s.in", fn);
134 0 : sprintf(exp_file, "%s.exp", fn);
135 0 : input_file = fopen(in_file, "rb");
136 : }
137 190 : if (!input_file) {
138 0 : fprintf(stderr, "could not open %s\n", in_file);
139 0 : exit(1);
140 : }
141 190 : num_rows = count_file_rows(input_file);
142 190 : fclose(input_file);
143 :
144 190 : input_file = fopen(exp_file, "r");
145 190 : if (!input_file)
146 170 : strcpy(exp_file, in_file);
147 : else
148 20 : fclose(input_file);
149 :
150 190 : input_file = fopen(sql_file, "r");
151 190 : assert(input_file);
152 : for (;;) {
153 : const char *param;
154 210 : size_t len = fgets_raw(line1, sizeof(line1), input_file);
155 :
156 210 : if (len && line1[len - 1] == '\n')
157 210 : line1[len - 1] = '\0';
158 210 : if (len == 0 || strncmp(line1, "-- PARAM:", 9) != 0)
159 : break;
160 20 : param = line1 + 9;
161 20 : if (strncmp(param, "table ", 6) == 0) {
162 10 : param += 6;
163 10 : strlcpy(table_name, param, sizeof(table_name));
164 10 : } else if (strncmp(param, "hints ", 6) == 0) {
165 10 : param += 6;
166 10 : strlcpy(hints, param, sizeof(hints));
167 : } else {
168 0 : fprintf(stderr, "invalid parameter: %s\n", param);
169 0 : exit(1);
170 : }
171 : }
172 190 : fclose(input_file);
173 :
174 190 : dberrhandle(ignore_err_handler);
175 190 : dbmsghandle(ignore_msg_handler);
176 :
177 190 : printf("Creating table '%s'\n", table_name);
178 190 : got_error = false;
179 190 : sql_cmd(dbproc);
180 190 : dbsqlexec(dbproc);
181 598 : while (dbresults(dbproc) != NO_MORE_RESULTS)
182 218 : continue;
183 :
184 190 : dberrhandle(syb_err_handler);
185 190 : dbmsghandle(syb_msg_handler);
186 :
187 190 : if (got_error)
188 36 : return;
189 :
190 154 : ret = sql_cmd(dbproc);
191 154 : printf("return from dbcmd = %d\n", ret);
192 :
193 154 : ret = dbsqlexec(dbproc);
194 154 : printf("return from dbsqlexec = %d\n", ret);
195 :
196 154 : if (dbresults(dbproc) != FAIL) {
197 154 : num_cols = dbnumcols(dbproc);
198 154 : printf("Number of columns = %d\n", num_cols);
199 :
200 308 : while (dbnextrow(dbproc) != NO_MORE_ROWS)
201 0 : continue;
202 : }
203 :
204 : /* BCP in */
205 :
206 154 : printf("bcp_init with in_file as '%s'\n", in_file);
207 154 : ret = bcp_init(dbproc, table_name, in_file, (char*) err_file, DB_IN);
208 154 : if (ret != SUCCEED)
209 0 : failure("bcp_init failed\n");
210 :
211 154 : if (hints[0])
212 2 : bcp_options(dbproc, BCPHINTS, (BYTE *) hints, strlen(hints));
213 :
214 154 : printf("return from bcp_init = %d\n", ret);
215 :
216 154 : ret = bcp_columns(dbproc, num_cols);
217 154 : if (ret != SUCCEED)
218 0 : failure("bcp_columns failed\n");
219 154 : printf("return from bcp_columns = %d\n", ret);
220 :
221 820 : for (i = 1; i < num_cols; i++) {
222 666 : if ((ret = bcp_colfmt(dbproc, i, SYBCHAR, 0, -1, (BYTE *) "\t", sizeof(char), i)) == FAIL)
223 0 : failure("return from bcp_colfmt = %d\n", ret);
224 : }
225 :
226 154 : if ((ret = bcp_colfmt(dbproc, num_cols, SYBCHAR, 0, -1, (BYTE *) "\n", sizeof(char), num_cols)) == FAIL)
227 0 : failure("return from bcp_colfmt = %d\n", ret);
228 :
229 :
230 154 : ret = bcp_exec(dbproc, &rows_copied);
231 154 : if (ret != SUCCEED || rows_copied != num_rows)
232 0 : failure("bcp_exec failed\n");
233 :
234 154 : printf("%d rows copied in\n", rows_copied);
235 :
236 : /* BCP out */
237 :
238 154 : rows_copied = 0;
239 154 : ret = bcp_init(dbproc, table_name, (char *) out_file, (char *) err_file, DB_OUT);
240 154 : if (ret != SUCCEED)
241 0 : failure("bcp_int failed\n");
242 :
243 154 : printf("select\n");
244 154 : sql_cmd(dbproc);
245 154 : dbsqlexec(dbproc);
246 :
247 154 : if (dbresults(dbproc) != FAIL) {
248 154 : num_cols = dbnumcols(dbproc);
249 308 : while (dbnextrow(dbproc) != NO_MORE_ROWS)
250 0 : continue;
251 : }
252 :
253 154 : ret = bcp_columns(dbproc, num_cols);
254 :
255 820 : for (i = 1; i < num_cols; i++) {
256 666 : if ((ret = bcp_colfmt(dbproc, i, SYBCHAR, 0, -1, (BYTE *) "\t", sizeof(char), i)) == FAIL)
257 0 : failure("return from bcp_colfmt = %d\n", ret);
258 : }
259 :
260 154 : if ((ret = bcp_colfmt(dbproc, num_cols, SYBCHAR, 0, -1, (BYTE *) "\n", sizeof(char), num_cols)) == FAIL)
261 0 : failure("return from bcp_colfmt = %d\n", ret);
262 :
263 154 : ret = bcp_exec(dbproc, &rows_copied);
264 154 : if (ret != SUCCEED || rows_copied != num_rows)
265 0 : failure("bcp_exec failed\n");
266 :
267 154 : printf("%d rows copied out\n", rows_copied);
268 :
269 154 : printf("Dropping table '%s'\n", table_name);
270 154 : sql_cmd(dbproc);
271 154 : dbsqlexec(dbproc);
272 466 : while (dbresults(dbproc) != NO_MORE_RESULTS)
273 158 : continue;
274 :
275 154 : if (failed)
276 : return;
277 :
278 154 : if (compare_files(exp_file, out_file))
279 154 : printf("Input and output files are equal\n");
280 : else
281 0 : failed = true;
282 : }
283 :
284 : static size_t
285 1910 : fgets_raw(char *s, int len, FILE *f)
286 : {
287 1910 : char *p = s;
288 :
289 766594 : while (len > 1) {
290 764684 : int c = getc(f);
291 764684 : if (c == EOF) {
292 498 : if (ferror(f))
293 : return 0;
294 : break;
295 : }
296 764186 : *p++ = c;
297 764186 : --len;
298 764186 : if (c == '\n')
299 : break;
300 : }
301 1910 : if (len > 0)
302 1910 : *p = 0;
303 1910 : return p - s;
304 : }
305 :
306 : static bool
307 154 : compare_files(const char *fn1, const char *fn2)
308 : {
309 154 : bool equal = true;
310 : FILE *f1, *f2;
311 : size_t s1, s2;
312 :
313 : /* check input and output should be the same */
314 154 : f1 = fopen(fn1, "r");
315 154 : f2 = fopen(fn2, "r");
316 154 : if (f1 != NULL && f2 != NULL) {
317 : int line = 1;
318 :
319 366 : for (;; ++line) {
320 886 : s1 = fgets_raw(line1, sizeof(line1), f1);
321 520 : s2 = fgets_raw(line2, sizeof(line2), f2);
322 :
323 : /* EOF or error of one */
324 520 : if (!!s1 != !!s2) {
325 0 : equal = false;
326 0 : failure("error reading a file or EOF of a file\n");
327 0 : break;
328 : }
329 :
330 : /* EOF or error of both */
331 520 : if (!s1) {
332 154 : if (feof(f1) && feof(f2))
333 : break;
334 0 : equal = false;
335 0 : failure("error reading a file\n");
336 0 : break;
337 : }
338 :
339 366 : if (s1 != s2 || memcmp(line1, line2, s1) != 0) {
340 0 : equal = false;
341 0 : failure("File different at line %d\n"
342 : " input: %s"
343 : " output: %s",
344 : line, line1, line2);
345 : }
346 : }
347 : } else {
348 0 : equal = false;
349 0 : failure("error opening files\n");
350 : }
351 154 : if (f1)
352 154 : fclose(f1);
353 154 : if (f2)
354 154 : fclose(f2);
355 :
356 154 : return equal;
357 : }
358 :
359 : static unsigned
360 190 : count_file_rows(FILE *f)
361 : {
362 : size_t s;
363 190 : unsigned rows = 1;
364 190 : char last = '\n';
365 :
366 190 : assert(f);
367 :
368 660 : while ((s = fgets_raw(line1, sizeof(line1), f)) != 0) {
369 470 : last = line1[s-1];
370 470 : if (last == '\n')
371 470 : ++rows;
372 : }
373 190 : if (last == '\n')
374 190 : --rows;
375 190 : assert(!ferror(f));
376 190 : return rows;
377 : }
|