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