1 : /*
2 : * Purpose: Test bcp in, with dbvarylen()
3 : * Functions: bcp_colfmt bcp_columns bcp_exec bcp_init dbvarylen
4 : */
5 :
6 : #if HAVE_CONFIG_H
7 : #include <config.h>
8 : #endif /* HAVE_CONFIG_H */
9 :
10 : #include <stdio.h>
11 :
12 : #if HAVE_STDLIB_H
13 : #include <stdlib.h>
14 : #endif /* HAVE_STDLIB_H */
15 :
16 : #if HAVE_STRING_H
17 : #include <string.h>
18 : #endif /* HAVE_STRING_H */
19 :
20 : #include <sqlfront.h>
21 : #include <sqldb.h>
22 :
23 : #include "common.h"
24 :
25 : static char software_version[] = "$Id: t0017.c,v 1.20.2.1 2006/01/29 21:56:24 freddy77 Exp $";
26 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
27 : int failed = 0;
28 :
29 :
30 : int
31 : main(int argc, char *argv[])
32 2 : {
33 : LOGINREC *login;
34 : DBPROCESS *dbproc;
35 : int i;
36 : RETCODE ret;
37 :
38 : #if 0
39 : char *out_file = "t0017.out";
40 : #endif
41 2 : const char *in_file = FREETDS_SRCDIR "/t0017.in";
42 2 : const char *err_file = "t0017.err";
43 : DBINT rows_copied;
44 2 : int num_cols = 0;
45 : int col_type[256];
46 : DBBOOL col_varylen[256];
47 : int prefix_len;
48 :
49 2 : set_malloc_options();
50 :
51 2 : read_login_info(argc, argv);
52 2 : fprintf(stdout, "Start\n");
53 2 : dbinit();
54 :
55 2 : dberrhandle(syb_err_handler);
56 2 : dbmsghandle(syb_msg_handler);
57 :
58 2 : fprintf(stdout, "About to logon\n");
59 :
60 2 : login = dblogin();
61 2 : BCP_SETL(login, TRUE);
62 2 : DBSETLPWD(login, PASSWORD);
63 2 : DBSETLUSER(login, USER);
64 2 : DBSETLAPP(login, "t0017");
65 :
66 2 : fprintf(stdout, "About to open, PASSWORD: %s, USER: %s, SERVER: %s\n", "", "", ""); /* PASSWORD, USER, SERVER); */
67 :
68 2 : dbproc = dbopen(login, SERVER);
69 2 : if (strlen(DATABASE)) {
70 2 : dbuse(dbproc, DATABASE);
71 : }
72 2 : dbloginfree(login);
73 2 : fprintf(stdout, "After logon\n");
74 :
75 2 : fprintf(stdout, "Creating table\n");
76 2 : dbcmd(dbproc, "create table #dblib0017 (c1 int null, c2 text)");
77 2 : dbsqlexec(dbproc);
78 4 : while (dbresults(dbproc) != NO_MORE_RESULTS) {
79 : /* nop */
80 : }
81 :
82 : /* BCP in */
83 :
84 2 : ret = bcp_init(dbproc, "#dblib0017", in_file, err_file, DB_IN);
85 2 : if (ret != SUCCEED)
86 0 : failed = 1;
87 :
88 2 : fprintf(stderr, "select\n");
89 2 : dbcmd(dbproc, "select * from #dblib0017 where 0=1");
90 2 : dbsqlexec(dbproc);
91 2 : if (dbresults(dbproc) != FAIL) {
92 2 : num_cols = dbnumcols(dbproc);
93 6 : for (i = 0; i < num_cols; i++) {
94 4 : col_type[i] = dbcoltype(dbproc, i + 1);
95 4 : col_varylen[i] = dbvarylen(dbproc, i + 1);
96 : }
97 2 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
98 : }
99 : }
100 :
101 2 : ret = bcp_columns(dbproc, num_cols);
102 2 : if (ret != SUCCEED)
103 0 : failed = 1;
104 6 : for (i = 0; i < num_cols; i++) {
105 4 : prefix_len = 0;
106 4 : if (col_type[i] == SYBIMAGE) {
107 0 : prefix_len = 4;
108 4 : } else if (col_varylen[i]) {
109 4 : prefix_len = 1;
110 : }
111 4 : ret = bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i + 1);
112 4 : if (ret == FAIL) {
113 0 : fprintf(stdout, "return from bcp_colfmt = %d\n", ret);
114 0 : failed = 1;
115 : }
116 : }
117 :
118 2 : ret = bcp_exec(dbproc, &rows_copied);
119 2 : if (ret != SUCCEED)
120 0 : failed = 1;
121 :
122 : #if 0
123 : /* BCP out */
124 : ret = bcp_init(dbproc, "#dblib0017", out_file, err_file, DB_OUT);
125 :
126 : fprintf(stderr, "select\n");
127 : dbcmd(dbproc, "select * from #dblib0017 where 0=1");
128 : dbsqlexec(dbproc);
129 : while (dbresults(dbproc) == SUCCEED) {
130 : num_cols = dbnumcols(dbproc);
131 : for (i = 0; i < num_cols; i++)
132 : col_type[i] = dbcoltype(dbproc, i + 1);
133 : while (dbnextrow(dbproc) != NO_MORE_ROWS) {
134 : }
135 : }
136 :
137 : ret = bcp_columns(dbproc, num_cols);
138 : for (i = 0; i < num_cols; i++) {
139 : prefix_len = 0;
140 : if (col_type[i] == SYBIMAGE) {
141 : prefix_len = 4;
142 : } else if (!is_fixed_type(col_type[i])) {
143 : prefix_len = 1;
144 : }
145 : bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i);
146 : }
147 :
148 : ret = bcp_exec(dbproc, &rows_copied);
149 : #endif
150 :
151 2 : fprintf(stdout, "%d rows copied\n", rows_copied);
152 2 : dbclose(dbproc);
153 2 : dbexit();
154 :
155 2 : fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
156 2 : return failed ? 1 : 0;
157 : }
|