1 /*
2 * Purpose: Test bcp in, with dbvarylen()
3 * Functions: bcp_colfmt bcp_columns bcp_exec bcp_init dbvarylen
4 */
5
6 #include "common.h"
7 #include <assert.h>
8
9 int
main(int argc,char * argv[])10 main(int argc, char *argv[])
11 {
12 int failed = 0;
13 LOGINREC *login;
14 DBPROCESS *dbproc;
15 int i;
16 RETCODE ret;
17 int big_endian = 1;
18
19 char *out_file = "t0017.out";
20 static const char in_file_le[] = FREETDS_SRCDIR "/t0017.in";
21 static const char in_file_be[] = FREETDS_SRCDIR "/t0017.in.be";
22 const char *in_file = in_file_le;
23 const char *err_file = "t0017.err";
24 DBINT rows_copied;
25 int num_cols = 0;
26 int col_type[256];
27 DBBOOL col_varylen[256];
28 int prefix_len;
29
30 if (((char *) &big_endian)[0] == 1)
31 big_endian = 0;
32 if (big_endian)
33 in_file = in_file_be;
34
35 setbuf(stdout, NULL);
36 setbuf(stderr, NULL);
37
38 set_malloc_options();
39
40 read_login_info(argc, argv);
41 printf("Starting %s\n", argv[0]);
42 dbinit();
43
44 dberrhandle(syb_err_handler);
45 dbmsghandle(syb_msg_handler);
46
47 printf("About to logon ... ");
48
49 login = dblogin();
50 assert(login);
51 BCP_SETL(login, TRUE);
52 DBSETLPWD(login, PASSWORD);
53 DBSETLUSER(login, USER);
54 DBSETLAPP(login, "t0017");
55 printf("done\n");
56
57 printf("Opening \"%s\" for \"%s\" ... ", SERVER, USER);
58 dbproc = dbopen(login, SERVER);
59 assert(dbproc);
60 if (strlen(DATABASE)) {
61 dbuse(dbproc, DATABASE);
62 }
63 dbloginfree(login);
64 printf("done\n");
65
66 printf("Creating table ... ");
67 sql_cmd(dbproc);
68 dbsqlexec(dbproc);
69 while (dbresults(dbproc) != NO_MORE_RESULTS) {
70 /* nop */
71 }
72 printf("done\n");
73
74 sql_cmd(dbproc);
75 dbsqlexec(dbproc);
76 while (dbresults(dbproc) != NO_MORE_RESULTS) {
77 /* nop */
78 }
79
80 /* BCP out */
81 printf("bcp_init... ");
82 ret = bcp_init(dbproc, "#dblib0017", out_file, err_file, DB_OUT);
83 if (ret != SUCCEED)
84 failed = 1;
85 printf("done\n");
86
87 printf("Issuing SELECT ... ");
88 sql_cmd(dbproc);
89 dbsqlexec(dbproc);
90 printf("done\nFetching metadata ... ");
91 if (dbresults(dbproc) != FAIL) {
92 num_cols = dbnumcols(dbproc);
93 for (i = 0; i < num_cols; ++i) {
94 col_type[i] = dbcoltype(dbproc, i + 1);
95 col_varylen[i] = dbvarylen(dbproc, i + 1);
96 }
97 while (dbnextrow(dbproc) != NO_MORE_ROWS) {
98 }
99 }
100 printf("done\n");
101
102 printf("bcp_columns ... ");
103 ret = bcp_columns(dbproc, num_cols);
104 if (ret != SUCCEED)
105 failed = 1;
106 for (i = 0; i < num_cols; i++) {
107 prefix_len = 0;
108 if (col_type[i] == SYBIMAGE || col_type[i] == SYBTEXT) {
109 prefix_len = 4;
110 } else if (col_varylen[i]) {
111 prefix_len = 1;
112 }
113 printf("bind %d prefix %d col_type %s\n", i, prefix_len, col_type[i] == SYBIMAGE ? "image" : "other");
114 ret = bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i + 1);
115 if (ret == FAIL) {
116 fprintf(stderr, "return from bcp_colfmt = %d\n", ret);
117 failed = 1;
118 }
119 }
120 printf("done\n");
121
122 rows_copied = -1;
123 printf("bcp_exec ... ");
124 ret = bcp_exec(dbproc, &rows_copied);
125 if (ret != SUCCEED || rows_copied != 1)
126 failed = 1;
127
128 printf("%d rows copied\n", rows_copied);
129
130 /* delete rows */
131 sql_cmd(dbproc);
132 dbsqlexec(dbproc);
133 while (dbresults(dbproc) != NO_MORE_RESULTS) {
134 /* nop */
135 }
136
137 /*
138 * BCP in
139 */
140 printf("bcp_init... ");
141 ret = bcp_init(dbproc, "#dblib0017", in_file, err_file, DB_IN);
142 if (ret != SUCCEED)
143 failed = 1;
144 printf("done\n");
145
146 printf("Issuing SELECT ... ");
147 sql_cmd(dbproc);
148 dbsqlexec(dbproc);
149 printf("done\nFetching metadata ... ");
150 if (dbresults(dbproc) != FAIL) {
151 num_cols = dbnumcols(dbproc);
152 for (i = 0; i < num_cols; i++) {
153 col_type[i] = dbcoltype(dbproc, i + 1);
154 col_varylen[i] = dbvarylen(dbproc, i + 1);
155 }
156 while (dbnextrow(dbproc) != NO_MORE_ROWS) {
157 }
158 }
159 while (dbresults(dbproc) != NO_MORE_RESULTS) {
160 /* nop */
161 }
162 printf("done\n");
163
164 printf("bcp_columns ... ");
165 ret = bcp_columns(dbproc, num_cols);
166 if (ret != SUCCEED)
167 failed = 1;
168 for (i = 0; i < num_cols; i++) {
169 prefix_len = 0;
170 if (col_type[i] == SYBIMAGE || col_type[i] == SYBTEXT) {
171 prefix_len = 4;
172 } else if (col_varylen[i]) {
173 prefix_len = 1;
174 }
175 ret = bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i + 1);
176 if (ret == FAIL) {
177 fprintf(stderr, "return from bcp_colfmt = %d\n", ret);
178 failed = 1;
179 }
180 }
181 printf("done\n");
182
183 printf("bcp_exec ... ");
184 rows_copied = -1;
185 ret = bcp_exec(dbproc, &rows_copied);
186 if (ret != SUCCEED || rows_copied != 1)
187 failed = 1;
188 printf("done\n");
189
190
191 /* test we inserted correctly row */
192 if (!failed) {
193 sql_cmd(dbproc);
194 dbsqlexec(dbproc);
195 while (dbresults(dbproc) != NO_MORE_RESULTS) {
196 while ((ret=dbnextrow(dbproc)) != NO_MORE_ROWS) {
197 fprintf(stderr, "Invalid dbnextrow result %d executing query\n", ret);
198 failed = 1;
199 }
200 }
201 }
202
203 printf("%d rows copied\n", rows_copied);
204 dbclose(dbproc);
205 dbexit();
206
207 printf("dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
208 return failed ? 1 : 0;
209 }
210