Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
3 : * Copyright (C) 2010, 2011 Frediano Ziglio
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Library General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Library General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Library General Public
16 : * License along with this library; if not, write to the
17 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 : * Boston, MA 02111-1307, USA.
19 : */
20 :
21 : #include <config.h>
22 :
23 : #include <stdarg.h>
24 : #include <stdio.h>
25 : #include <assert.h>
26 :
27 : #if HAVE_STRING_H
28 : #include <string.h>
29 : #endif /* HAVE_STRING_H */
30 :
31 : #if HAVE_STDLIB_H
32 : #include <stdlib.h>
33 : #endif /* HAVE_STDLIB_H */
34 :
35 : #if HAVE_UNISTD_H
36 : #include <unistd.h>
37 : #endif /* HAVE_UNISTD_H */
38 :
39 : #ifdef _WIN32
40 : #include <io.h>
41 : #endif
42 :
43 : #include <freetds/tds.h>
44 : #include <freetds/iconv.h>
45 : #include <freetds/convert.h>
46 : #include <freetds/bytes.h>
47 : #include <freetds/utils/string.h>
48 : #include <freetds/encodings.h>
49 : #include <freetds/replacements.h>
50 : #include <sybfront.h>
51 : #include <sybdb.h>
52 : #include <syberror.h>
53 : #include <dblib.h>
54 :
55 : #define HOST_COL_CONV_ERROR 1
56 : #define HOST_COL_NULL_ERROR 2
57 :
58 : #ifdef HAVE_FSEEKO
59 : typedef off_t offset_type;
60 : #elif defined(_WIN32) || defined(_WIN64)
61 : /* win32 version */
62 : typedef __int64 offset_type;
63 : # if defined(HAVE__FSEEKI64) && defined(HAVE__FTELLI64)
64 : # define fseeko(f,o,w) _fseeki64((f),o,w)
65 : # define ftello(f) _ftelli64((f))
66 : # else
67 : # define fseeko(f,o,w) (_lseeki64(fileno(f),o,w) == -1 ? -1 : 0)
68 : # define ftello(f) _telli64(fileno(f))
69 : # endif
70 : #else
71 : /* use old version */
72 : #define fseeko(f,o,w) fseek(f,o,w)
73 : #define ftello(f) ftell(f)
74 : typedef long offset_type;
75 : #endif
76 :
77 : static void _bcp_free_storage(DBPROCESS * dbproc);
78 : static void _bcp_free_columns(DBPROCESS * dbproc);
79 : static void _bcp_null_error(TDSBCPINFO *bcpinfo, int index, int offset);
80 : static TDSRET _bcp_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset);
81 : static TDSRET _bcp_no_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset);
82 :
83 : static int rtrim(char *, int);
84 : static int rtrim_u16(uint16_t *str, int len, uint16_t space);
85 : static STATUS _bcp_read_hostfile(DBPROCESS * dbproc, FILE * hostfile, bool *row_error, bool skip);
86 : static int _bcp_readfmt_colinfo(DBPROCESS * dbproc, char *buf, BCP_HOSTCOLINFO * ci);
87 : static int _bcp_get_term_var(const BYTE * pdata, const BYTE * term, int term_len);
88 :
89 : /*
90 : * "If a host file is being used ... the default data formats are as follows:
91 : *
92 : * > The order, type, length and number of the columns in the host file are
93 : * assumed to be identical to the order, type and number of the columns in the database table.
94 : * > If a given database column's data is fixed-length,
95 : * then the host file's data column will also be fixed-length.
96 : * > If a given database column's data is variable-length or may contain null values,
97 : * the host file's data column will be prefixed by
98 : * a 4-byte length value for SYBTEXT and SYBIMAGE data types, and
99 : * a 1-byte length value for all other types.
100 : * > There are no terminators of any kind between host file columns."
101 : */
102 :
103 : static void
104 280 : init_hostfile_columns(DBPROCESS *dbproc)
105 : {
106 280 : const int ncols = dbproc->bcpinfo->bindinfo->num_cols;
107 : RETCODE erc;
108 : int icol;
109 :
110 280 : if (ncols == 0)
111 : return;
112 :
113 280 : if ((erc = bcp_columns(dbproc, ncols)) != SUCCEED) {
114 0 : assert(erc == SUCCEED);
115 : return;
116 : }
117 :
118 1796 : for (icol = 0; icol < ncols; icol++) {
119 1516 : const TDSCOLUMN *pcol = dbproc->bcpinfo->bindinfo->columns[icol];
120 1516 : int prefixlen = 0, termlen = 0;
121 :
122 1516 : switch (pcol->column_type) {
123 : case SYBTEXT:
124 : case SYBIMAGE:
125 : prefixlen = 4;
126 : break;
127 1416 : default:
128 1416 : prefixlen = dbvarylen(dbproc, icol+1)? 1 : 0;
129 : }
130 :
131 1516 : erc = bcp_colfmt(dbproc, icol+1, pcol->column_type, prefixlen, pcol->column_size, NULL, termlen, icol+1);
132 :
133 1516 : assert(erc == SUCCEED);
134 : if (erc != SUCCEED)
135 : return;
136 : }
137 : }
138 :
139 :
140 : /**
141 : * \ingroup dblib_bcp
142 : * \brief Prepare for bulk copy operation on a table
143 : *
144 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
145 : * \param tblname the name of the table receiving or providing the data.
146 : * \param hfile the data file opposite the table, if any.
147 : * \param errfile the "error file" captures messages and, if errors are encountered,
148 : * copies of any rows that could not be written to the table.
149 : * \param direction one of
150 : * - \b DB_IN writing to the table
151 : * - \b DB_OUT writing to the host file
152 : * .
153 : * \remarks bcp_init() sets the host file data format and acquires the table metadata.
154 : * It is called before the other bulk copy functions.
155 : *
156 : * When writing to a table, bcp can use as its data source a data file (\a hfile),
157 : * or program data in an application's variables. In the latter case, call bcp_bind()
158 : * to associate your data with the appropriate table column.
159 : * \return SUCCEED or FAIL.
160 : * \sa BCP_SETL(), bcp_bind(), bcp_done(), bcp_exec()
161 : */
162 : RETCODE
163 318 : bcp_init(DBPROCESS * dbproc, const char *tblname, const char *hfile, const char *errfile, int direction)
164 : {
165 318 : tdsdump_log(TDS_DBG_FUNC, "bcp_init(%p, %s, %s, %s, %d)\n",
166 : dbproc, tblname? tblname:"NULL", hfile? hfile:"NULL", errfile? errfile:"NULL", direction);
167 318 : CHECK_CONN(FAIL);
168 :
169 : /*
170 : * Validate other parameters
171 : */
172 318 : if (dbproc->tds_socket->conn->tds_version < 0x500) {
173 0 : dbperror(dbproc, SYBETDSVER, 0);
174 0 : return FAIL;
175 : }
176 :
177 318 : if (tblname == NULL) {
178 0 : dbperror(dbproc, SYBEBCITBNM, 0);
179 0 : return FAIL;
180 : }
181 :
182 362 : if (direction != DB_QUERYOUT && !IS_TDS7_PLUS(dbproc->tds_socket->conn) &&
183 44 : strlen(tblname) > 92) { /* 30.30.30 for Sybase */
184 0 : dbperror(dbproc, SYBEBCITBLEN, 0);
185 0 : return FAIL;
186 : }
187 :
188 318 : if (direction != DB_IN && direction != DB_OUT && direction != DB_QUERYOUT) {
189 0 : dbperror(dbproc, SYBEBDIO, 0);
190 0 : return FAIL;
191 : }
192 :
193 : /* Free previously allocated storage in dbproc & initialise flags, etc. */
194 318 : _bcp_free_storage(dbproc);
195 :
196 : /* Allocate storage */
197 318 : dbproc->bcpinfo = tds_alloc_bcpinfo();
198 318 : if (dbproc->bcpinfo == NULL)
199 : goto memory_error;
200 :
201 318 : if (!tds_dstr_copy(&dbproc->bcpinfo->tablename, tblname))
202 : goto memory_error;
203 :
204 318 : dbproc->bcpinfo->direction = direction;
205 :
206 318 : dbproc->bcpinfo->xfer_init = false;
207 318 : dbproc->bcpinfo->bind_count = 0;
208 :
209 318 : if (TDS_FAILED(tds_bcp_init(dbproc->tds_socket, dbproc->bcpinfo))) {
210 : /* TODO return proper error */
211 : /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */
212 0 : dbperror(dbproc, SYBEBCNT, 0);
213 0 : return FAIL;
214 : }
215 :
216 : /* Prepare default hostfile columns */
217 :
218 318 : if (hfile == NULL) {
219 38 : dbproc->hostfileinfo = NULL;
220 38 : return SUCCEED;
221 : }
222 :
223 280 : dbproc->hostfileinfo = tds_new0(BCP_HOSTFILEINFO, 1);
224 :
225 280 : if (dbproc->hostfileinfo == NULL)
226 : goto memory_error;
227 280 : dbproc->hostfileinfo->maxerrs = 10;
228 280 : dbproc->hostfileinfo->firstrow = 1;
229 280 : if ((dbproc->hostfileinfo->hostfile = strdup(hfile)) == NULL)
230 : goto memory_error;
231 :
232 280 : if (errfile != NULL)
233 280 : if ((dbproc->hostfileinfo->errorfile = strdup(errfile)) == NULL)
234 : goto memory_error;
235 :
236 280 : init_hostfile_columns(dbproc);
237 :
238 280 : return SUCCEED;
239 :
240 0 : memory_error:
241 0 : _bcp_free_storage(dbproc);
242 0 : dbperror(dbproc, SYBEMEM, ENOMEM);
243 0 : return FAIL;
244 : }
245 :
246 :
247 : /**
248 : * \ingroup dblib_bcp
249 : * \brief Set the length of a host variable to be written to a table.
250 : *
251 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
252 : * \param varlen size of the variable, in bytes, or
253 : * - \b 0 indicating NULL
254 : * - \b -1 indicating size is determined by the prefix or terminator.
255 : * (If both a prefix and a terminator are present, bcp is supposed to use the smaller of the
256 : * two. This feature might or might not actually work.)
257 : * \param table_column the number of the column in the table (starting with 1, not zero).
258 : *
259 : * \return SUCCEED or FAIL.
260 : * \sa bcp_bind(), bcp_colptr(), bcp_sendrow()
261 : */
262 : RETCODE
263 130 : bcp_collen(DBPROCESS * dbproc, DBINT varlen, int table_column)
264 : {
265 : TDSCOLUMN *bcpcol;
266 :
267 130 : tdsdump_log(TDS_DBG_FUNC, "bcp_collen(%p, %d, %d)\n", dbproc, varlen, table_column);
268 :
269 130 : CHECK_CONN(FAIL);
270 130 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL); /* not initialized */
271 130 : DBPERROR_RETURN(dbproc->bcpinfo->direction != DB_IN, SYBEBCPN) /* not inbound */
272 130 : DBPERROR_RETURN(dbproc->hostfileinfo != NULL, SYBEBCPI) /* cannot have data file */
273 130 : CHECK_PARAMETER(0 < table_column &&
274 : table_column <= dbproc->bcpinfo->bindinfo->num_cols, SYBECNOR, FAIL);
275 :
276 130 : bcpcol = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
277 :
278 : /* Sybase library does not check for NULL here, only sending, so don't
279 : * check and send SYBEBCNN */
280 130 : bcpcol->column_bindlen = varlen;
281 :
282 130 : return SUCCEED;
283 : }
284 :
285 : /**
286 : * \ingroup dblib_bcp
287 : * \brief Indicate how many columns are to be found in the datafile.
288 : *
289 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
290 : * \param host_colcount count of columns in the datafile, irrespective of how many you intend to use.
291 : * \remarks This function describes the file as it is, not how it will be used.
292 : *
293 : * \return SUCCEED or FAIL. It's hard to see how it could fail.
294 : * \sa bcp_colfmt()
295 : */
296 : RETCODE
297 560 : bcp_columns(DBPROCESS * dbproc, int host_colcount)
298 : {
299 : int i;
300 :
301 560 : tdsdump_log(TDS_DBG_FUNC, "bcp_columns(%p, %d)\n", dbproc, host_colcount);
302 560 : CHECK_CONN(FAIL);
303 560 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
304 560 : CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
305 :
306 560 : if (host_colcount < 1) {
307 0 : dbperror(dbproc, SYBEBCFO, 0);
308 0 : return FAIL;
309 : }
310 :
311 560 : _bcp_free_columns(dbproc);
312 :
313 560 : dbproc->hostfileinfo->host_columns = tds_new0(BCP_HOSTCOLINFO *, host_colcount);
314 560 : if (dbproc->hostfileinfo->host_columns == NULL) {
315 0 : dbperror(dbproc, SYBEMEM, ENOMEM);
316 0 : return FAIL;
317 : }
318 :
319 560 : dbproc->hostfileinfo->host_colcount = host_colcount;
320 :
321 3592 : for (i = 0; i < host_colcount; i++) {
322 3032 : dbproc->hostfileinfo->host_columns[i] = tds_new0(BCP_HOSTCOLINFO, 1);
323 3032 : if (dbproc->hostfileinfo->host_columns[i] == NULL) {
324 0 : dbproc->hostfileinfo->host_colcount = i;
325 0 : _bcp_free_columns(dbproc);
326 0 : dbperror(dbproc, SYBEMEM, ENOMEM);
327 0 : return FAIL;
328 : }
329 : }
330 :
331 : return SUCCEED;
332 : }
333 :
334 : /**
335 : * \ingroup dblib_bcp
336 : * \brief Specify the format of a datafile prior to writing to a table.
337 : *
338 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
339 : * \param host_colnum datafile column number (starting with 1, not zero).
340 : * \param host_type dataype token describing the data type in \a host_colnum. E.g. SYBCHAR for character data.
341 : * \param host_prefixlen size of the prefix in the datafile column, if any. For delimited files: zero.
342 : * May be 0, 1, 2, or 4 bytes. The prefix will be read as an integer (not a character string) from the
343 : * data file, and will be interpreted the data size of that column, in bytes.
344 : * \param host_collen maximum size of datafile column, exclusive of any prefix/terminator. Just the data, ma'am.
345 : * Special values:
346 : * - \b 0 indicates NULL.
347 : * - \b -1 for fixed-length non-null datatypes
348 : * - \b -1 for variable-length datatypes (e.g. SYBCHAR) where the length is established
349 : * by a prefix/terminator.
350 : * \param host_term the sequence of characters that will serve as a column terminator (delimiter) in the datafile.
351 : * Often a tab character, but can be any string of any length. Zero indicates no terminator.
352 : * Special characters:
353 : * - \b '\\0' terminator is an ASCII NUL.
354 : * - \b '\\t' terminator is an ASCII TAB.
355 : * - \b '\\n' terminator is an ASCII NL.
356 : * \param host_termlen the length of \a host_term, in bytes.
357 : * \param table_colnum Nth column, starting at 1, in the table that maps to \a host_colnum.
358 : * If there is a column in the datafile that does not map to a table column, set \a table_colnum to zero.
359 : *
360 : *\remarks bcp_colfmt() is called once for each column in the datafile, as specified with bcp_columns().
361 : * In so doing, you describe to FreeTDS how to parse each line of your datafile, and where to send each field.
362 : *
363 : * When a prefix or terminator is used with variable-length data, \a host_collen may have one of three values:
364 : * - \b positive indicating the maximum number of bytes to be used
365 : * - \b 0 indicating NULL
366 : * - \b -1 indicating no maximum; all data, as described by the prefix/terminator will be used.
367 : * .
368 : * \return SUCCEED or FAIL.
369 : * \sa bcp_batch(), bcp_bind(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(),
370 : * bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_sendrow
371 : */
372 : RETCODE
373 3032 : bcp_colfmt(DBPROCESS * dbproc, int host_colnum, int host_type, int host_prefixlen, DBINT host_collen, const BYTE * host_term,
374 : int host_termlen, int table_colnum)
375 : {
376 : BCP_HOSTCOLINFO *hostcol;
377 3032 : BYTE *terminator = NULL;
378 :
379 3032 : tdsdump_log(TDS_DBG_FUNC, "bcp_colfmt(%p, %d, %d, %d, %d, %p, %d, %d)\n",
380 : dbproc, host_colnum, host_type, host_prefixlen, (int) host_collen, host_term, host_termlen, table_colnum);
381 3032 : CHECK_CONN(FAIL);
382 3032 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
383 3032 : CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
384 :
385 : /* Microsoft specifies a "file_termlen" of zero if there's no terminator */
386 3032 : if (dbproc->msdblib && host_termlen == 0)
387 : host_termlen = -1;
388 :
389 3032 : if (host_termlen < 0)
390 0 : host_termlen = -1;
391 :
392 3032 : if (dbproc->hostfileinfo->host_colcount == 0) {
393 0 : dbperror(dbproc, SYBEBCBC, 0);
394 0 : return FAIL;
395 : }
396 :
397 3032 : if (host_colnum < 1) {
398 0 : dbperror(dbproc, SYBEBCFO, 0);
399 0 : return FAIL;
400 : }
401 :
402 3032 : if (host_colnum > dbproc->hostfileinfo->host_colcount) {
403 0 : dbperror(dbproc, SYBECNOR, 0);
404 0 : return FAIL;
405 : }
406 :
407 3032 : if (host_prefixlen != 0 && host_prefixlen != 1 && host_prefixlen != 2 && host_prefixlen != 4 && host_prefixlen != -1) {
408 0 : dbperror(dbproc, SYBEBCPREF, 0);
409 0 : return FAIL;
410 : }
411 :
412 : /* if column is not copied you cannot specify destination type */
413 3032 : if (table_colnum <= 0 && host_type == 0) {
414 0 : dbperror(dbproc, SYBEBCPCTYP, 0);
415 0 : return FAIL;
416 : }
417 :
418 6064 : if (table_colnum > 0 && !is_tds_type_valid(host_type)) {
419 0 : dbperror(dbproc, SYBEUDTY, 0);
420 0 : return FAIL;
421 : }
422 :
423 3032 : if (host_type && host_prefixlen == 0 && host_collen == -1 && host_termlen == -1 && !is_fixed_type(host_type)) {
424 0 : dbperror(dbproc, SYBEVDPT, 0);
425 0 : return FAIL;
426 : }
427 :
428 3032 : if (host_collen < -1) {
429 0 : dbperror(dbproc, SYBEBCHLEN, 0);
430 0 : return FAIL;
431 : }
432 :
433 : /* No official error message. Fix and warn. */
434 3032 : if (is_fixed_type(host_type) && (host_collen != -1 && host_collen != 0)) {
435 644 : tdsdump_log(TDS_DBG_FUNC,
436 : "bcp_colfmt: changing host_collen to -1 from %d for fixed type %d.\n",
437 : host_collen, host_type);
438 : host_collen = -1;
439 : }
440 :
441 : /*
442 : * If there's a positive terminator length, we need a valid terminator pointer.
443 : * If the terminator length is 0 or -1, then there's no terminator.
444 : */
445 3032 : if (host_term == NULL && host_termlen > 0) {
446 0 : dbperror(dbproc, SYBEVDPT, 0); /* "all variable-length data must have either a length-prefix ..." */
447 0 : return FAIL;
448 : }
449 :
450 3032 : hostcol = dbproc->hostfileinfo->host_columns[host_colnum - 1];
451 :
452 : /* TODO add precision scale and join with bcp_colfmt_ps */
453 3032 : if (host_term && host_termlen > 0) {
454 1476 : if ((terminator = tds_new(BYTE, host_termlen)) == NULL) {
455 0 : dbperror(dbproc, SYBEMEM, errno);
456 0 : return FAIL;
457 : }
458 1476 : memcpy(terminator, host_term, host_termlen);
459 : }
460 3032 : hostcol->host_column = host_colnum;
461 3032 : hostcol->datatype = host_type ? (TDS_SERVER_TYPE) host_type : TDS_INVALID_TYPE;
462 3032 : hostcol->prefix_len = host_prefixlen;
463 3032 : hostcol->column_len = host_collen;
464 3032 : free(hostcol->terminator);
465 3032 : hostcol->terminator = terminator;
466 3032 : hostcol->term_len = host_termlen;
467 3032 : hostcol->tab_colnum = table_colnum;
468 :
469 3032 : return SUCCEED;
470 : }
471 :
472 : /**
473 : * \ingroup dblib_bcp
474 : * \brief Specify the format of a host file for bulk copy purposes,
475 : * with precision and scale support for numeric and decimal columns.
476 : *
477 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
478 : * \param host_colnum datafile column number (starting with 1, not zero).
479 : * \param host_type dataype token describing the data type in \a host_colnum. E.g. SYBCHAR for character data.
480 : * \param host_prefixlen size of the prefix in the datafile column, if any. For delimited files: zero.
481 : * May be 0, 1, 2, or 4 bytes. The prefix will be read as an integer (not a character string) from the
482 : * data file, and will be interpreted the data size of that column, in bytes.
483 : * \param host_collen maximum size of datafile column, exclusive of any prefix/terminator. Just the data, ma'am.
484 : * Special values:
485 : * - \b 0 indicates NULL.
486 : * - \b -1 for fixed-length non-null datatypes
487 : * - \b -1 for variable-length datatypes (e.g. SYBCHAR) where the length is established
488 : * by a prefix/terminator.
489 : * \param host_term the sequence of characters that will serve as a column terminator (delimiter) in the datafile.
490 : * Often a tab character, but can be any string of any length. Zero indicates no terminator.
491 : * Special characters:
492 : * - \b '\\0' terminator is an ASCII NUL.
493 : * - \b '\\t' terminator is an ASCII TAB.
494 : * - \b '\\n' terminator is an ASCII NL.
495 : * \param host_termlen the length of \a host_term, in bytes.
496 : * \param table_colnum Nth column, starting at 1, in the table that maps to \a host_colnum.
497 : * If there is a column in the datafile that does not map to a table column, set \a table_colnum to zero.
498 : * \param typeinfo something
499 : * \todo Not implemented.
500 : * \return SUCCEED or FAIL.
501 : * \sa bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(),
502 : * bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_sendrow
503 : */
504 : RETCODE
505 0 : bcp_colfmt_ps(DBPROCESS * dbproc, int host_colnum, int host_type,
506 : int host_prefixlen, DBINT host_collen, BYTE * host_term, int host_termlen, int table_colnum, DBTYPEINFO * typeinfo)
507 : {
508 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_colfmt_ps(%p, %d, %d, %d, %d, %p, %d, %d, %p)\n",
509 : dbproc, host_colnum, host_type, host_prefixlen, (int) host_collen,
510 : host_term, host_termlen, table_colnum, typeinfo);
511 0 : CHECK_CONN(FAIL);
512 0 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
513 :
514 : /* dbperror(dbproc, , 0); Illegal precision specified */
515 :
516 : /* TODO see bcp_colfmt */
517 : return FAIL;
518 : }
519 :
520 :
521 : /**
522 : * \ingroup dblib_bcp
523 : * \brief Set BCP options for uploading a datafile
524 : *
525 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
526 : * \param field symbolic constant indicating the option to be set, one of:
527 : * - \b BCPMAXERRS Maximum errors tolerated before quitting. The default is 10.
528 : * - \b BCPFIRST The first row to read in the datafile. The default is 1.
529 : * - \b BCPLAST The last row to read in the datafile. The default is to copy all rows. A value of
530 : * -1 resets this field to its default?
531 : * - \b BCPBATCH The number of rows per batch. Default is 0, meaning a single batch.
532 : * \param value The value for \a field.
533 : *
534 : * \remarks These options control the behavior of bcp_exec().
535 : * When writing to a table from application host memory variables,
536 : * program logic controls error tolerance and batch size.
537 : *
538 : * \return SUCCEED or FAIL.
539 : * \sa bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_done(), bcp_exec(), bcp_options()
540 : */
541 : RETCODE
542 0 : bcp_control(DBPROCESS * dbproc, int field, DBINT value)
543 : {
544 0 : tdsdump_log(TDS_DBG_FUNC, "bcp_control(%p, %d, %d)\n", dbproc, field, value);
545 0 : CHECK_CONN(FAIL);
546 0 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
547 :
548 0 : if (field == BCPKEEPIDENTITY) {
549 0 : dbproc->bcpinfo->identity_insert_on = (value != 0);
550 0 : return SUCCEED;
551 : }
552 :
553 0 : CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
554 :
555 0 : switch (field) {
556 :
557 0 : case BCPMAXERRS:
558 0 : if (value < 1)
559 0 : value = 10;
560 0 : dbproc->hostfileinfo->maxerrs = value;
561 0 : break;
562 0 : case BCPFIRST:
563 0 : if (value < 1)
564 0 : value = 1;
565 0 : dbproc->hostfileinfo->firstrow = value;
566 0 : break;
567 0 : case BCPLAST:
568 0 : dbproc->hostfileinfo->lastrow = value;
569 0 : break;
570 0 : case BCPBATCH:
571 0 : dbproc->hostfileinfo->batch = value;
572 0 : break;
573 :
574 0 : default:
575 0 : dbperror(dbproc, SYBEIFNB, 0);
576 0 : return FAIL;
577 : }
578 : return SUCCEED;
579 : }
580 :
581 : /*
582 : * \ingroup dblib_bcp
583 : * \brief Get BCP batch option
584 : *
585 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
586 : * \remarks This function is specific to FreeTDS.
587 : *
588 : * \return the value that was set by bcp_control.
589 : * \sa bcp_batch(), bcp_control()
590 : */
591 : int
592 0 : bcp_getbatchsize(DBPROCESS * dbproc)
593 : {
594 0 : CHECK_CONN(-1);
595 0 : CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBCPI, -1);
596 0 : return dbproc->hostfileinfo->batch;
597 : }
598 :
599 : /**
600 : * \ingroup dblib_bcp
601 : * \brief Set "hints" for uploading a file. A FreeTDS-only function.
602 : *
603 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
604 : * \param option symbolic constant indicating the option to be set, one of:
605 : * - \b BCPLABELED Not implemented.
606 : * - \b BCPHINTS The hint to be passed when the bulk-copy begins.
607 : * \param value The string constant for \a option a/k/a the hint. One of:
608 : * - \b ORDER The data are ordered in accordance with the table's clustered index.
609 : * - \b ROWS_PER_BATCH The batch size
610 : * - \b KILOBYTES_PER_BATCH The approximate number of kilobytes to use for a batch size
611 : * - \b TABLOCK Lock the table
612 : * - \b CHECK_CONSTRAINTS Apply constraints
613 : * - \b FIRE_TRIGGERS Fire any INSERT triggers on the target table
614 : * \param valuelen The strlen of \a value.
615 : *
616 : * \return SUCCEED or FAIL.
617 : * \sa bcp_control(),
618 : * bcp_exec(),
619 : * \todo Simplify. Remove \a valuelen.
620 : */
621 : RETCODE
622 0 : bcp_options(DBPROCESS * dbproc, int option, BYTE * value, int valuelen)
623 : {
624 : int i;
625 : static const char *const hints[] = {
626 : "ORDER", "ROWS_PER_BATCH", "KILOBYTES_PER_BATCH", "TABLOCK", "CHECK_CONSTRAINTS",
627 : "FIRE_TRIGGERS", "KEEP_NULLS", NULL
628 : };
629 :
630 0 : tdsdump_log(TDS_DBG_FUNC, "bcp_options(%p, %d, %p, %d)\n", dbproc, option, value, valuelen);
631 0 : CHECK_CONN(FAIL);
632 0 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
633 0 : CHECK_NULP(value, "bcp_options", 3, FAIL);
634 :
635 0 : switch (option) {
636 0 : case BCPLABELED:
637 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED bcp option: BCPLABELED\n");
638 : break;
639 0 : case BCPHINTS:
640 0 : if (!value || valuelen <= 0)
641 : break;
642 :
643 0 : for (i = 0; hints[i]; i++) { /* look up hint */
644 0 : if (strncasecmp((char *) value, hints[i], strlen(hints[i])) == 0) {
645 0 : if (!tds_dstr_copy(&dbproc->bcpinfo->hint, hints[i]))
646 : return FAIL;
647 0 : return SUCCEED;
648 : }
649 : }
650 0 : tdsdump_log(TDS_DBG_FUNC, "failed, no such hint\n");
651 : break;
652 0 : default:
653 0 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED bcp option: %u\n", option);
654 : break;
655 : }
656 0 : return FAIL;
657 : }
658 :
659 : /**
660 : * \ingroup dblib_bcp
661 : * \brief Override bcp_bind() by pointing to a different host variable.
662 : *
663 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
664 : * \param colptr The pointer, the address of your variable.
665 : * \param table_column The 1-based column ordinal in the table.
666 : * \remarks Use between calls to bcp_sendrow(). After calling bcp_colptr(),
667 : * subsequent calls to bcp_sendrow() will bind to the new address.
668 : * \return SUCCEED or FAIL.
669 : * \sa bcp_bind(), bcp_collen(), bcp_sendrow()
670 : */
671 : RETCODE
672 0 : bcp_colptr(DBPROCESS * dbproc, BYTE * colptr, int table_column)
673 : {
674 : TDSCOLUMN *curcol;
675 :
676 0 : tdsdump_log(TDS_DBG_FUNC, "bcp_colptr(%p, %p, %d)\n", dbproc, colptr, table_column);
677 0 : CHECK_CONN(FAIL);
678 0 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
679 0 : CHECK_PARAMETER(dbproc->bcpinfo->bindinfo, SYBEBCPI, FAIL);
680 : /* colptr can be NULL */
681 :
682 0 : if (dbproc->bcpinfo->direction != DB_IN) {
683 0 : dbperror(dbproc, SYBEBCPN, 0);
684 0 : return FAIL;
685 : }
686 0 : if (table_column <= 0 || table_column > dbproc->bcpinfo->bindinfo->num_cols) {
687 0 : dbperror(dbproc, SYBEBCPN, 0);
688 0 : return FAIL;
689 : }
690 :
691 0 : curcol = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
692 0 : curcol->column_varaddr = (TDS_CHAR *)colptr;
693 :
694 0 : return SUCCEED;
695 : }
696 :
697 :
698 : /**
699 : * \ingroup dblib_bcp
700 : * \brief See if BCP_SETL() was used to set the LOGINREC for BCP work.
701 : *
702 : * \param login Address of the LOGINREC variable to be passed to dbopen().
703 : *
704 : * \return TRUE or FALSE.
705 : * \sa BCP_SETL(), bcp_init(), dblogin(), dbopen()
706 : */
707 : DBBOOL
708 10 : bcp_getl(LOGINREC * login)
709 : {
710 10 : TDSLOGIN *tdsl = login->tds_login;
711 :
712 10 : tdsdump_log(TDS_DBG_FUNC, "bcp_getl(%p)\n", login);
713 :
714 10 : return (tdsl->bulk_copy);
715 : }
716 :
717 : /**
718 : * Convert column for output (usually to a file)
719 : * Conversion is slightly different from input as:
720 : * - date is formatted differently;
721 : * - you have to set properly numeric while on input the column metadata are
722 : * used;
723 : * - we need to make sure buffer is always at least a minimum bytes.
724 : */
725 : static int
726 1428 : _bcp_convert_out(DBPROCESS * dbproc, TDSCOLUMN *curcol, BCP_HOSTCOLINFO *hostcol, TDS_UCHAR **p_data, const char *bcpdatefmt)
727 : {
728 : BYTE *src;
729 : int srclen;
730 : int buflen;
731 1428 : int srctype = tds_get_conversion_type(curcol->column_type, curcol->column_size);
732 :
733 1428 : src = curcol->column_data;
734 1428 : if (is_blob_col(curcol))
735 102 : src = (BYTE *) ((TDSBLOB *) src)->textvalue;
736 :
737 1428 : srclen = curcol->column_cur_size;
738 :
739 : /*
740 : * if we are converting datetime to string, need to override any
741 : * date time formats already established
742 : */
743 1428 : if (is_datetime_type(srctype) && is_ascii_type(hostcol->datatype)) {
744 : TDSDATEREC when;
745 :
746 28 : tds_datecrack(srctype, src, &when);
747 28 : buflen = (int)tds_strftime((TDS_CHAR *)(*p_data), 256,
748 : bcpdatefmt, &when, 3);
749 1400 : } else if (srclen == 0 && is_variable_type(curcol->column_type)
750 46 : && is_ascii_type(hostcol->datatype)) {
751 : /*
752 : * An empty string is denoted in the output file by a single ASCII NUL
753 : * byte that we request by specifying a destination length of -1. (Not
754 : * to be confused with a database NULL, which is denoted in the output
755 : * file with an empty string!)
756 : */
757 46 : (*p_data)[0] = 0;
758 46 : buflen = 1;
759 1354 : } else if (is_numeric_type(hostcol->datatype)) {
760 0 : TDS_NUMERIC *num = (TDS_NUMERIC *) (*p_data);
761 0 : if (is_numeric_type(srctype)) {
762 0 : TDS_NUMERIC *nsrc = (TDS_NUMERIC *) src;
763 0 : num->precision = nsrc->precision;
764 0 : num->scale = nsrc->scale;
765 : } else {
766 0 : num->precision = 18;
767 0 : num->scale = 0;
768 : }
769 0 : buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) num);
770 0 : if (buflen > 0)
771 0 : buflen = tds_numeric_bytes_per_prec[num->precision] + 2;
772 1354 : } else if (!is_variable_type(hostcol->datatype)) {
773 10 : buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) (*p_data));
774 : } else {
775 : CONV_RESULT cr;
776 :
777 : /*
778 : * For null columns, the above work to determine the output buffer size is moot,
779 : * because bcpcol->data_size is zero, so dbconvert() won't write anything,
780 : * and returns zero.
781 : */
782 1344 : buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) &cr);
783 1344 : if (buflen < 0)
784 0 : return buflen;
785 :
786 1344 : if (buflen >= 256) {
787 30 : free(*p_data);
788 30 : *p_data = (TDS_UCHAR *) cr.c;
789 : } else {
790 1314 : memcpy(*p_data, cr.c, buflen);
791 1314 : free(cr.c);
792 : }
793 :
794 : /*
795 : * Special case: When outputting database varchar data
796 : * (either varchar or nullable char) conversion may have
797 : * trimmed trailing blanks such that nothing is left.
798 : * In this case we need to put a single blank to the output file.
799 : */
800 1344 : if (is_char_type(curcol->column_type) && srclen > 0 && buflen == 0) {
801 0 : strcpy ((char *) (*p_data), " ");
802 0 : buflen = 1;
803 : }
804 : }
805 : return buflen;
806 : }
807 :
808 : static int
809 0 : bcp_cache_prefix_len(BCP_HOSTCOLINFO *hostcol, const TDSCOLUMN *curcol)
810 : {
811 : int plen;
812 :
813 0 : if (is_blob_type(hostcol->datatype))
814 : plen = 4;
815 0 : else if (is_numeric_type(hostcol->datatype))
816 : plen = 1;
817 0 : else if (!is_fixed_type(hostcol->datatype))
818 : plen = 2;
819 0 : else if (curcol->column_nullable)
820 : plen = 1;
821 : else
822 0 : plen = 0;
823 : /* cache */
824 0 : return hostcol->prefix_len = plen;
825 : }
826 :
827 : static RETCODE
828 1568 : bcp_write_prefix(FILE *hostfile, BCP_HOSTCOLINFO *hostcol, TDSCOLUMN *curcol, int buflen)
829 : {
830 : union {
831 : TDS_TINYINT ti;
832 : TDS_SMALLINT si;
833 : TDS_INT li;
834 : } u;
835 : int plen;
836 :
837 : /* compute prefix len if needed */
838 1568 : if ((plen = hostcol->prefix_len) == -1)
839 0 : plen = bcp_cache_prefix_len(hostcol, curcol);
840 :
841 : /* output prefix to file */
842 1568 : switch (plen) {
843 : default:
844 : return SUCCEED;
845 10 : case 1:
846 10 : u.ti = buflen;
847 10 : break;
848 0 : case 2:
849 0 : u.si = buflen;
850 0 : break;
851 10 : case 4:
852 10 : u.li = buflen;
853 10 : break;
854 : }
855 20 : if (fwrite(&u, plen, 1, hostfile) == 1)
856 : return SUCCEED;
857 :
858 0 : return FAIL;
859 : }
860 :
861 : /**
862 : * \ingroup dblib_bcp_internal
863 : * \brief
864 : *
865 : *
866 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
867 : * \param rows_copied
868 : *
869 : * \return SUCCEED or FAIL.
870 : * \sa BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
871 : */
872 : static RETCODE
873 140 : _bcp_exec_out(DBPROCESS * dbproc, DBINT * rows_copied)
874 : {
875 140 : FILE *hostfile = NULL;
876 140 : TDS_UCHAR *data = NULL;
877 : int i;
878 :
879 : TDSSOCKET *tds;
880 : TDSRESULTINFO *resinfo;
881 140 : TDSCOLUMN *curcol = NULL;
882 : BCP_HOSTCOLINFO *hostcol;
883 : int buflen;
884 :
885 : TDS_INT result_type;
886 :
887 : int row_of_query;
888 : int rows_written;
889 : const char *bcpdatefmt;
890 : TDSRET tdsret;
891 :
892 140 : tdsdump_log(TDS_DBG_FUNC, "_bcp_exec_out(%p, %p)\n", dbproc, rows_copied);
893 140 : assert(dbproc);
894 140 : assert(rows_copied);
895 :
896 140 : tds = dbproc->tds_socket;
897 140 : assert(tds);
898 :
899 140 : bcpdatefmt = getenv("FREEBCP_DATEFMT");
900 140 : if (!bcpdatefmt)
901 140 : bcpdatefmt = "%Y-%m-%d %H:%M:%S.%z";
902 :
903 140 : if (dbproc->bcpinfo->direction == DB_QUERYOUT ) {
904 0 : if (TDS_FAILED(tds_submit_query(tds, tds_dstr_cstr(&dbproc->bcpinfo->tablename))))
905 : return FAIL;
906 : } else {
907 : /* TODO quote if needed */
908 280 : if (TDS_FAILED(tds_submit_queryf(tds, "select * from %s", tds_dstr_cstr(&dbproc->bcpinfo->tablename))))
909 : return FAIL;
910 : }
911 :
912 140 : tdsret = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS);
913 140 : if (TDS_FAILED(tdsret))
914 : return FAIL;
915 :
916 140 : if (!tds->res_info) {
917 : /* TODO flush/cancel to keep consistent state */
918 : return FAIL;
919 : }
920 :
921 140 : resinfo = tds->res_info;
922 :
923 140 : row_of_query = 0;
924 140 : rows_written = 0;
925 :
926 : /* allocate at least 256 bytes */
927 : /* allocate data for buffer conversion */
928 140 : data = tds_new(TDS_UCHAR, 256);
929 140 : if (!data) {
930 0 : dbperror(dbproc, SYBEMEM, errno);
931 : goto Cleanup;
932 : }
933 :
934 : /*
935 : * TODO above we allocate many buffer just to convert and store
936 : * to file.. avoid all that passages...
937 : */
938 :
939 140 : if (!(hostfile = fopen(dbproc->hostfileinfo->hostfile, "w"))) {
940 0 : dbperror(dbproc, SYBEBCUO, errno);
941 0 : goto Cleanup;
942 : }
943 :
944 : /* fetch a row of data from the server */
945 :
946 434 : while (tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)
947 : == TDS_SUCCESS) {
948 :
949 434 : if (result_type != TDS_ROW_RESULT && result_type != TDS_COMPUTE_RESULT)
950 : break;
951 :
952 294 : row_of_query++;
953 :
954 : /* skip rows outside of the firstrow/lastrow range, if specified */
955 294 : if (dbproc->hostfileinfo->firstrow > row_of_query ||
956 : row_of_query > TDS_MAX(dbproc->hostfileinfo->lastrow, 0x7FFFFFFF))
957 0 : continue;
958 :
959 : /* Go through the hostfile columns, finding those that relate to database columns. */
960 1568 : for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
961 1568 : hostcol = dbproc->hostfileinfo->host_columns[i];
962 1568 : if (hostcol->tab_colnum < 1 || hostcol->tab_colnum > resinfo->num_cols)
963 0 : continue;
964 :
965 1568 : curcol = resinfo->columns[hostcol->tab_colnum - 1];
966 :
967 1568 : if (curcol->column_cur_size < 0) {
968 : buflen = 0;
969 : } else {
970 1428 : buflen = _bcp_convert_out(dbproc, curcol, hostcol, &data, bcpdatefmt);
971 : }
972 1428 : if (buflen < 0) {
973 0 : _dblib_convert_err(dbproc, buflen);
974 0 : goto Cleanup;
975 : }
976 :
977 : /* The prefix */
978 1568 : if (bcp_write_prefix(hostfile, hostcol, curcol, buflen) != SUCCEED)
979 : goto write_error;
980 :
981 : /* The data */
982 1568 : if (hostcol->column_len != -1) {
983 0 : buflen = TDS_MIN(buflen, hostcol->column_len);
984 : }
985 :
986 1568 : if (buflen > 0) {
987 1428 : if (fwrite(data, buflen, 1, hostfile) != 1)
988 : goto write_error;
989 : }
990 :
991 : /* The terminator */
992 1568 : if (hostcol->terminator && hostcol->term_len > 0) {
993 1548 : if (fwrite(hostcol->terminator, hostcol->term_len, 1, hostfile) != 1)
994 : goto write_error;
995 : }
996 : }
997 294 : rows_written++;
998 : }
999 140 : if (fclose(hostfile) != 0) {
1000 0 : dbperror(dbproc, SYBEBCUC, errno);
1001 0 : goto Cleanup;
1002 : }
1003 140 : hostfile = NULL;
1004 :
1005 140 : if (row_of_query + 1 < dbproc->hostfileinfo->firstrow) {
1006 : /*
1007 : * The table which bulk-copy is attempting to
1008 : * copy to a host-file is shorter than the
1009 : * number of rows which bulk-copy was instructed to skip.
1010 : */
1011 : /* TODO reset TDSSOCKET state */
1012 0 : dbperror(dbproc, SYBETTS, 0);
1013 : goto Cleanup;
1014 : }
1015 :
1016 140 : *rows_copied = rows_written;
1017 140 : free(data);
1018 140 : return SUCCEED;
1019 :
1020 0 : write_error:
1021 0 : dbperror(dbproc, SYBEBCWE, errno);
1022 :
1023 0 : Cleanup:
1024 0 : if (hostfile)
1025 0 : fclose(hostfile);
1026 0 : free(data);
1027 0 : return FAIL;
1028 : }
1029 :
1030 : static STATUS
1031 140 : _bcp_check_eof(DBPROCESS * dbproc, FILE *file, int icol)
1032 : {
1033 140 : int errnum = errno;
1034 :
1035 140 : tdsdump_log(TDS_DBG_FUNC, "_bcp_check_eof(%p, %p, %d)\n", dbproc, file, icol);
1036 140 : assert(dbproc);
1037 140 : assert(file);
1038 :
1039 140 : if (feof(file)) {
1040 140 : if (icol == 0) {
1041 140 : tdsdump_log(TDS_DBG_FUNC, "Normal end-of-file reached while loading bcp data file.\n");
1042 : return NO_MORE_ROWS;
1043 : }
1044 0 : dbperror(dbproc, SYBEBEOF, errnum);
1045 0 : return FAIL;
1046 : }
1047 0 : dbperror(dbproc, SYBEBCRE, errnum);
1048 0 : return FAIL;
1049 : }
1050 :
1051 : /**
1052 : * Convert column for input to a table
1053 : */
1054 : static TDSRET
1055 4238 : _bcp_convert_in(DBPROCESS *dbproc, TDS_SERVER_TYPE srctype, const TDS_CHAR *src, TDS_UINT srclen,
1056 : TDS_SERVER_TYPE desttype, BCPCOLDATA *coldata)
1057 : {
1058 4238 : bool variable = true;
1059 : CONV_RESULT cr, *p_cr;
1060 : TDS_INT len;
1061 :
1062 4238 : coldata->is_null = false;
1063 :
1064 4238 : if (!is_variable_type(desttype)) {
1065 3040 : variable = false;
1066 3040 : p_cr = (CONV_RESULT *) coldata->data;
1067 : } else {
1068 : p_cr = &cr;
1069 : }
1070 :
1071 4238 : len = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, desttype, p_cr);
1072 4238 : if (len < 0) {
1073 0 : _dblib_convert_err(dbproc, len);
1074 0 : return TDS_FAIL;
1075 : }
1076 :
1077 4238 : coldata->datalen = len;
1078 4238 : if (variable) {
1079 1198 : free(coldata->data);
1080 1198 : coldata->data = (TDS_UCHAR *) cr.c;
1081 : }
1082 : return TDS_SUCCESS;
1083 : }
1084 :
1085 : static void
1086 4238 : rtrim_bcpcol(TDSCOLUMN *bcpcol)
1087 : {
1088 : /* trim trailing blanks from character data */
1089 4238 : if (is_ascii_type(bcpcol->on_server.column_type)) {
1090 : /* A single NUL byte indicates an empty string. */
1091 1046 : if (bcpcol->bcp_column_data->datalen == 1
1092 200 : && bcpcol->bcp_column_data->data[0] == '\0') {
1093 24 : bcpcol->bcp_column_data->datalen = 0;
1094 24 : return;
1095 : }
1096 2044 : bcpcol->bcp_column_data->datalen = rtrim((char *) bcpcol->bcp_column_data->data,
1097 : bcpcol->bcp_column_data->datalen);
1098 1022 : return;
1099 : }
1100 :
1101 : /* unicode part */
1102 3192 : if (is_unicode_type(bcpcol->on_server.column_type)) {
1103 : uint16_t *data, space;
1104 :
1105 68 : if (!bcpcol->char_conv || bcpcol->char_conv->to.charset.min_bytes_per_char != 2)
1106 6 : return;
1107 :
1108 68 : data = (uint16_t *) bcpcol->bcp_column_data->data;
1109 : /* A single NUL byte indicates an empty string. */
1110 68 : if (bcpcol->bcp_column_data->datalen == 2 && data[0] == 0) {
1111 6 : bcpcol->bcp_column_data->datalen = 0;
1112 6 : return;
1113 : }
1114 62 : switch (bcpcol->char_conv->to.charset.canonic) {
1115 0 : case TDS_CHARSET_UTF_16BE:
1116 : case TDS_CHARSET_UCS_2BE:
1117 0 : TDS_PUT_A2BE(&space, 0x20);
1118 0 : break;
1119 62 : case TDS_CHARSET_UTF_16LE:
1120 : case TDS_CHARSET_UCS_2LE:
1121 62 : TDS_PUT_A2LE(&space, 0x20);
1122 62 : break;
1123 : default:
1124 : return;
1125 : }
1126 124 : bcpcol->bcp_column_data->datalen = rtrim_u16(data, bcpcol->bcp_column_data->datalen, space);
1127 : }
1128 : }
1129 :
1130 : /**
1131 : * \ingroup dblib_bcp_internal
1132 : * \brief
1133 : *
1134 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1135 : * \param hostfile
1136 : * \param row_error
1137 : *
1138 : * \return MORE_ROWS, NO_MORE_ROWS, or FAIL.
1139 : * \sa BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
1140 : */
1141 : static STATUS
1142 434 : _bcp_read_hostfile(DBPROCESS * dbproc, FILE * hostfile, bool *row_error, bool skip)
1143 : {
1144 : int i;
1145 :
1146 434 : tdsdump_log(TDS_DBG_FUNC, "_bcp_read_hostfile(%p, %p, %p, %d)\n", dbproc, hostfile, row_error, skip);
1147 434 : assert(dbproc);
1148 434 : assert(hostfile);
1149 434 : assert(row_error);
1150 :
1151 : /* for each host file column defined by calls to bcp_colfmt */
1152 :
1153 3570 : for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
1154 1708 : TDSCOLUMN *bcpcol = NULL;
1155 : BCP_HOSTCOLINFO *hostcol;
1156 : TDS_CHAR *coldata;
1157 1708 : int collen = 0;
1158 1708 : bool data_is_null = false;
1159 : offset_type col_start;
1160 :
1161 1708 : tdsdump_log(TDS_DBG_FUNC, "parsing host column %d\n", i + 1);
1162 1708 : hostcol = dbproc->hostfileinfo->host_columns[i];
1163 :
1164 1708 : hostcol->column_error = 0;
1165 :
1166 : /*
1167 : * If this host file column contains table data,
1168 : * find the right element in the table/column list.
1169 : */
1170 1708 : if (hostcol->tab_colnum > 0) {
1171 1708 : if (hostcol->tab_colnum > dbproc->bcpinfo->bindinfo->num_cols) {
1172 0 : tdsdump_log(TDS_DBG_FUNC, "error: file wider than table: %d/%d\n",
1173 : i+1, dbproc->bcpinfo->bindinfo->num_cols);
1174 0 : dbperror(dbproc, SYBEBEOF, 0);
1175 0 : return FAIL;
1176 : }
1177 1708 : tdsdump_log(TDS_DBG_FUNC, "host column %d uses bcpcol %d (%p)\n",
1178 : i+1, hostcol->tab_colnum, bcpcol);
1179 1708 : bcpcol = dbproc->bcpinfo->bindinfo->columns[hostcol->tab_colnum - 1];
1180 1708 : assert(bcpcol != NULL);
1181 : }
1182 :
1183 : /* detect prefix len */
1184 1708 : if (bcpcol && hostcol->prefix_len == -1)
1185 0 : bcp_cache_prefix_len(hostcol, bcpcol);
1186 :
1187 : /* a prefix length, if extant, specifies how many bytes to read */
1188 1708 : if (hostcol->prefix_len > 0) {
1189 : union {
1190 : TDS_TINYINT ti;
1191 : TDS_SMALLINT si;
1192 : TDS_INT li;
1193 : } u;
1194 :
1195 30 : switch (hostcol->prefix_len) {
1196 20 : case 1:
1197 20 : if (fread(&u.ti, 1, 1, hostfile) != 1)
1198 20 : return _bcp_check_eof(dbproc, hostfile, i);
1199 10 : collen = u.ti ? u.ti : -1;
1200 10 : break;
1201 0 : case 2:
1202 0 : if (fread(&u.si, 2, 1, hostfile) != 1)
1203 0 : return _bcp_check_eof(dbproc, hostfile, i);
1204 0 : collen = u.si;
1205 0 : break;
1206 10 : case 4:
1207 10 : if (fread(&u.li, 4, 1, hostfile) != 1)
1208 0 : return _bcp_check_eof(dbproc, hostfile, i);
1209 10 : collen = u.li;
1210 10 : break;
1211 0 : default:
1212 : /* FIXME return error, remember that prefix_len can be 3 */
1213 0 : assert(hostcol->prefix_len <= 4);
1214 : break;
1215 : }
1216 :
1217 : /* TODO test all NULL types */
1218 : /* TODO for < -1 error */
1219 20 : if (collen <= -1) {
1220 : data_is_null = true;
1221 : collen = 0;
1222 : }
1223 : }
1224 :
1225 : /* if (Max) column length specified take that into consideration. (Meaning what, exactly?) */
1226 :
1227 1698 : if (!data_is_null && hostcol->column_len >= 0) {
1228 0 : if (hostcol->column_len == 0)
1229 : data_is_null = true;
1230 0 : else if (collen)
1231 0 : collen = TDS_MIN(hostcol->column_len, collen);
1232 : else
1233 : collen = hostcol->column_len;
1234 : }
1235 :
1236 1698 : tdsdump_log(TDS_DBG_FUNC, "prefix_len = %d collen = %d \n", hostcol->prefix_len, collen);
1237 :
1238 : /* Fixed Length data - this overrides anything else specified */
1239 :
1240 1698 : if (is_fixed_type(hostcol->datatype))
1241 10 : collen = tds_get_size_by_type(hostcol->datatype);
1242 :
1243 1698 : col_start = ftello(hostfile);
1244 :
1245 : /*
1246 : * The data file either contains prefixes stating the length, or is delimited.
1247 : * If delimited, we "measure" the field by looking for the terminator, then read it,
1248 : * and set collen to the field's post-iconv size.
1249 : */
1250 1698 : if (hostcol->term_len > 0) { /* delimited data file */
1251 : size_t col_bytes;
1252 : TDSRET conv_res;
1253 :
1254 : /*
1255 : * Read and convert the data
1256 : */
1257 1678 : coldata = NULL;
1258 3356 : conv_res = tds_bcp_fread(dbproc->tds_socket, bcpcol ? bcpcol->char_conv : NULL, hostfile,
1259 1678 : (const char *) hostcol->terminator, hostcol->term_len, &coldata, &col_bytes);
1260 :
1261 1678 : if (TDS_FAILED(conv_res)) {
1262 0 : tdsdump_log(TDS_DBG_FUNC, "col %d: error converting %ld bytes!\n",
1263 : (i+1), (long) collen);
1264 0 : *row_error = true;
1265 0 : free(coldata);
1266 0 : dbperror(dbproc, SYBEBCOR, 0);
1267 0 : return FAIL;
1268 : }
1269 :
1270 1678 : if (conv_res == TDS_NO_MORE_RESULTS) {
1271 130 : free(coldata);
1272 130 : return _bcp_check_eof(dbproc, hostfile, i);
1273 : }
1274 :
1275 1548 : if (col_bytes > 0x7fffffffl) {
1276 0 : free(coldata);
1277 0 : *row_error = true;
1278 0 : tdsdump_log(TDS_DBG_FUNC, "data from file is too large!\n");
1279 0 : dbperror(dbproc, SYBEBCOR, 0);
1280 0 : return FAIL;
1281 : }
1282 :
1283 1548 : collen = (int)col_bytes;
1284 1548 : if (collen == 0)
1285 140 : data_is_null = true;
1286 :
1287 : /*
1288 : * TODO:
1289 : * Dates are a problem. In theory, we should be able to read non-English dates, which
1290 : * would contain non-ASCII characters. One might suppose we should convert date
1291 : * strings to ISO-8859-1 (or another canonical form) here, because tds_convert() can't be
1292 : * expected to deal with encodings. But instead date strings are read verbatim and
1293 : * passed to tds_convert() without even waving to iconv(). For English dates, this works,
1294 : * because English dates expressed as UTF-8 strings are indistinguishable from the ASCII.
1295 : */
1296 : } else { /* unterminated field */
1297 :
1298 20 : coldata = tds_new(TDS_CHAR, 1 + collen);
1299 20 : if (coldata == NULL) {
1300 0 : *row_error = true;
1301 0 : dbperror(dbproc, SYBEMEM, errno);
1302 0 : return FAIL;
1303 : }
1304 :
1305 20 : coldata[collen] = 0;
1306 20 : if (collen) {
1307 : /*
1308 : * Read and convert the data
1309 : * TODO: Call tds_bcp_fread() instead of fread(3).
1310 : * The columns should each have their iconv cd set, and noncharacter data
1311 : * should have -1 as the iconv cd, causing tds_bcp_fread() to not attempt
1312 : * any conversion. We do not need a datatype switch here to decide what to do.
1313 : * As of 0.62, this *should* actually work. All that remains is to change the
1314 : * call and test it.
1315 : */
1316 20 : tdsdump_log(TDS_DBG_FUNC, "Reading %d bytes from hostfile.\n", collen);
1317 20 : if (fread(coldata, collen, 1, hostfile) != 1) {
1318 0 : free(coldata);
1319 0 : return _bcp_check_eof(dbproc, hostfile, i);
1320 : }
1321 : }
1322 : }
1323 :
1324 : /*
1325 : * At this point, however the field was read, however big it was, its address is coldata and its size is collen.
1326 : */
1327 1568 : tdsdump_log(TDS_DBG_FUNC, "Data read from hostfile: collen is now %d, data_is_null is %d\n", collen, data_is_null);
1328 1568 : if (!skip && bcpcol) {
1329 1568 : if (data_is_null) {
1330 140 : bcpcol->bcp_column_data->is_null = true;
1331 140 : bcpcol->bcp_column_data->datalen = 0;
1332 : } else {
1333 : TDSRET rc;
1334 : TDS_SERVER_TYPE desttype;
1335 :
1336 1428 : desttype = tds_get_conversion_type(bcpcol->column_type, bcpcol->column_size);
1337 :
1338 1428 : rc = _bcp_convert_in(dbproc, hostcol->datatype, (const TDS_CHAR*) coldata, collen,
1339 : desttype, bcpcol->bcp_column_data);
1340 1428 : if (TDS_FAILED(rc)) {
1341 0 : hostcol->column_error = HOST_COL_CONV_ERROR;
1342 0 : *row_error = true;
1343 0 : tdsdump_log(TDS_DBG_FUNC,
1344 : "_bcp_read_hostfile failed to convert %d bytes at offset 0x%" PRIx64 " in the data file.\n",
1345 : collen, (TDS_INT8) col_start);
1346 : }
1347 :
1348 1428 : rtrim_bcpcol(bcpcol);
1349 : }
1350 : #if USING_SYBEBCNN
1351 : if (!hostcol->column_error) {
1352 : if (bcpcol->bcp_column_data->datalen <= 0) { /* Are we trying to insert a NULL ? */
1353 : if (!bcpcol->column_nullable) {
1354 : /* too bad if the column is not nullable */
1355 : hostcol->column_error = HOST_COL_NULL_ERROR;
1356 : *row_error = true;
1357 : dbperror(dbproc, SYBEBCNN, 0);
1358 : }
1359 : }
1360 : }
1361 : #endif
1362 : }
1363 1568 : free(coldata);
1364 : }
1365 : return MORE_ROWS;
1366 : }
1367 :
1368 : /**
1369 : * \ingroup dblib_bcp
1370 : * \brief Write data in host variables to the table.
1371 : *
1372 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1373 : *
1374 : * \remarks Call bcp_bind() first to describe the variables to be used.
1375 : * Use bcp_batch() to commit sets of rows.
1376 : * After sending the last row call bcp_done().
1377 : * \return SUCCEED or FAIL.
1378 : * \sa bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(),
1379 : * bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_moretext(), bcp_options()
1380 : */
1381 : RETCODE
1382 180 : bcp_sendrow(DBPROCESS * dbproc)
1383 : {
1384 : TDSSOCKET *tds;
1385 :
1386 180 : tdsdump_log(TDS_DBG_FUNC, "bcp_sendrow(%p)\n", dbproc);
1387 180 : CHECK_CONN(FAIL);
1388 180 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
1389 :
1390 180 : tds = dbproc->tds_socket;
1391 :
1392 180 : if (dbproc->bcpinfo->direction != DB_IN) {
1393 0 : dbperror(dbproc, SYBEBCPN, 0);
1394 0 : return FAIL;
1395 : }
1396 :
1397 180 : if (dbproc->hostfileinfo != NULL) {
1398 0 : dbperror(dbproc, SYBEBCPB, 0);
1399 0 : return FAIL;
1400 : }
1401 :
1402 : /*
1403 : * The first time sendrow is called after bcp_init,
1404 : * there is a certain amount of initialisation to be done.
1405 : */
1406 180 : if (!dbproc->bcpinfo->xfer_init) {
1407 :
1408 : /* The start_copy function retrieves details of the table's columns */
1409 28 : if (TDS_FAILED(tds_bcp_start_copy_in(tds, dbproc->bcpinfo))) {
1410 0 : dbperror(dbproc, SYBEBULKINSERT, 0);
1411 0 : return FAIL;
1412 : }
1413 :
1414 28 : dbproc->bcpinfo->xfer_init = true;
1415 :
1416 : }
1417 :
1418 180 : dbproc->bcpinfo->parent = dbproc;
1419 180 : return TDS_FAILED(tds_bcp_send_record(dbproc->tds_socket, dbproc->bcpinfo,
1420 180 : _bcp_get_col_data, _bcp_null_error, 0)) ? FAIL : SUCCEED;
1421 : }
1422 :
1423 :
1424 : /**
1425 : * \ingroup dblib_bcp_internal
1426 : * \brief
1427 : *
1428 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1429 : * \param rows_copied
1430 : *
1431 : * \return SUCCEED or FAIL.
1432 : * \sa BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
1433 : */
1434 : static RETCODE
1435 140 : _bcp_exec_in(DBPROCESS * dbproc, DBINT * rows_copied)
1436 : {
1437 140 : FILE *hostfile, *errfile = NULL;
1438 140 : TDSSOCKET *tds = dbproc->tds_socket;
1439 : BCP_HOSTCOLINFO *hostcol;
1440 : STATUS ret;
1441 :
1442 : int i, row_of_hostfile, rows_written_so_far;
1443 : int row_error_count;
1444 : bool row_error;
1445 : offset_type row_start, row_end;
1446 : offset_type error_row_size;
1447 140 : const size_t chunk_size = 0x20000u;
1448 :
1449 140 : tdsdump_log(TDS_DBG_FUNC, "_bcp_exec_in(%p, %p)\n", dbproc, rows_copied);
1450 : assert(dbproc);
1451 140 : assert(rows_copied);
1452 :
1453 140 : *rows_copied = 0;
1454 :
1455 140 : if (!(hostfile = fopen(dbproc->hostfileinfo->hostfile, "r"))) {
1456 0 : dbperror(dbproc, SYBEBCUO, 0);
1457 0 : return FAIL;
1458 : }
1459 :
1460 140 : if (TDS_FAILED(tds_bcp_start_copy_in(tds, dbproc->bcpinfo))) {
1461 0 : fclose(hostfile);
1462 0 : return FAIL;
1463 : }
1464 :
1465 140 : row_of_hostfile = 0;
1466 140 : rows_written_so_far = 0;
1467 :
1468 140 : row_error_count = 0;
1469 140 : dbproc->bcpinfo->parent = dbproc;
1470 :
1471 : for (;;) {
1472 : bool skip;
1473 :
1474 434 : row_start = ftello(hostfile);
1475 434 : row_error = false;
1476 :
1477 434 : row_of_hostfile++;
1478 :
1479 : if (row_of_hostfile > TDS_MAX(dbproc->hostfileinfo->lastrow, 0x7FFFFFFF))
1480 : break;
1481 :
1482 434 : skip = dbproc->hostfileinfo->firstrow > row_of_hostfile;
1483 434 : ret = _bcp_read_hostfile(dbproc, hostfile, &row_error, skip);
1484 434 : if (ret != MORE_ROWS)
1485 : break;
1486 :
1487 294 : if (row_error) {
1488 : int count;
1489 :
1490 0 : if (errfile == NULL && dbproc->hostfileinfo->errorfile) {
1491 0 : if (!(errfile = fopen(dbproc->hostfileinfo->errorfile, "w"))) {
1492 0 : fclose(hostfile);
1493 0 : dbperror(dbproc, SYBEBUOE, 0);
1494 0 : return FAIL;
1495 : }
1496 : }
1497 :
1498 0 : if (errfile != NULL) {
1499 : char *row_in_error = NULL;
1500 :
1501 0 : for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
1502 0 : hostcol = dbproc->hostfileinfo->host_columns[i];
1503 0 : if (hostcol->column_error == HOST_COL_CONV_ERROR) {
1504 0 : count = fprintf(errfile,
1505 : "#@ data conversion error on host data file Row %d Column %d\n",
1506 : row_of_hostfile, i + 1);
1507 0 : if( count < 0 ) {
1508 0 : dbperror(dbproc, SYBEBWEF, errno);
1509 : }
1510 0 : } else if (hostcol->column_error == HOST_COL_NULL_ERROR) {
1511 0 : count = fprintf(errfile, "#@ Attempt to bulk-copy a NULL value into Server column"
1512 : " which does not accept NULL values. Row %d, Column %d\n",
1513 : row_of_hostfile, i + 1);
1514 0 : if( count < 0 ) {
1515 0 : dbperror(dbproc, SYBEBWEF, errno);
1516 : }
1517 :
1518 : }
1519 : }
1520 :
1521 0 : row_end = ftello(hostfile);
1522 :
1523 : /* error data can be very long so split in chunks */
1524 0 : error_row_size = row_end - row_start;
1525 0 : fseeko(hostfile, row_start, SEEK_SET);
1526 :
1527 0 : while (error_row_size > 0) {
1528 0 : size_t chunk = TDS_MIN((size_t) error_row_size, chunk_size);
1529 :
1530 0 : if (!row_in_error) {
1531 0 : if ((row_in_error = tds_new(char, chunk)) == NULL) {
1532 0 : dbperror(dbproc, SYBEMEM, errno);
1533 : }
1534 : }
1535 :
1536 0 : if (fread(row_in_error, chunk, 1, hostfile) != 1) {
1537 0 : tdsdump_log(TDS_DBG_ERROR, "BILL fread failed after fseek\n");
1538 : }
1539 0 : count = (int)fwrite(row_in_error, chunk, 1, errfile);
1540 0 : if( (size_t)count < chunk ) {
1541 0 : dbperror(dbproc, SYBEBWEF, errno);
1542 : }
1543 0 : error_row_size -= chunk;
1544 : }
1545 0 : free(row_in_error);
1546 :
1547 0 : fseeko(hostfile, row_end, SEEK_SET);
1548 0 : count = fprintf(errfile, "\n");
1549 0 : if( count < 0 ) {
1550 0 : dbperror(dbproc, SYBEBWEF, errno);
1551 : }
1552 : }
1553 0 : row_error_count++;
1554 0 : if (row_error_count >= dbproc->hostfileinfo->maxerrs)
1555 : break;
1556 0 : continue;
1557 : }
1558 :
1559 294 : if (skip)
1560 0 : continue;
1561 :
1562 294 : if (TDS_SUCCEED(tds_bcp_send_record(dbproc->tds_socket, dbproc->bcpinfo,
1563 : _bcp_no_get_col_data, _bcp_null_error, 0))) {
1564 :
1565 294 : rows_written_so_far++;
1566 :
1567 294 : if (dbproc->hostfileinfo->batch > 0 && rows_written_so_far == dbproc->hostfileinfo->batch) {
1568 0 : if (TDS_FAILED(tds_bcp_done(tds, &rows_written_so_far))) {
1569 0 : if (errfile)
1570 0 : fclose(errfile);
1571 0 : fclose(hostfile);
1572 0 : return FAIL;
1573 : }
1574 :
1575 0 : *rows_copied += rows_written_so_far;
1576 0 : rows_written_so_far = 0;
1577 :
1578 0 : dbperror(dbproc, SYBEBBCI, 0); /* batch copied to server */
1579 :
1580 0 : tds_bcp_start(tds, dbproc->bcpinfo);
1581 : }
1582 : }
1583 : }
1584 :
1585 140 : if (row_error_count == 0 && row_of_hostfile < dbproc->hostfileinfo->firstrow) {
1586 : /* "The BCP hostfile '%1!' contains only %2! rows. */
1587 0 : dbperror(dbproc, SYBEBCSA, 0, dbproc->hostfileinfo->hostfile, row_of_hostfile);
1588 : }
1589 :
1590 140 : if (errfile && 0 != fclose(errfile) ) {
1591 0 : dbperror(dbproc, SYBEBUCE, 0);
1592 : }
1593 :
1594 140 : if (fclose(hostfile) != 0) {
1595 0 : dbperror(dbproc, SYBEBCUC, 0);
1596 0 : ret = FAIL;
1597 : }
1598 :
1599 140 : tds_bcp_done(tds, &rows_written_so_far);
1600 140 : *rows_copied += rows_written_so_far;
1601 :
1602 140 : return ret == NO_MORE_ROWS? SUCCEED : FAIL; /* (ret is returned from _bcp_read_hostfile) */
1603 : }
1604 :
1605 : /**
1606 : * \ingroup dblib_bcp
1607 : * \brief Write a datafile to a table.
1608 : *
1609 : *
1610 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1611 : * \param rows_copied bcp_exec will write the count of rows successfully written to this address.
1612 : * If \a rows_copied is NULL, it will be ignored by db-lib.
1613 : *
1614 : * \return SUCCEED or FAIL.
1615 : * \sa bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(),
1616 : * bcp_control(), bcp_done(), bcp_init(), bcp_sendrow()
1617 : */
1618 : RETCODE
1619 280 : bcp_exec(DBPROCESS * dbproc, DBINT *rows_copied)
1620 : {
1621 : DBINT dummy_copied;
1622 280 : RETCODE ret = FAIL;
1623 :
1624 280 : tdsdump_log(TDS_DBG_FUNC, "bcp_exec(%p, %p)\n", dbproc, rows_copied);
1625 280 : CHECK_CONN(FAIL);
1626 280 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
1627 280 : CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBCVH, FAIL);
1628 :
1629 280 : if (rows_copied == NULL) /* NULL means we should ignore it */
1630 0 : rows_copied = &dummy_copied;
1631 :
1632 280 : if (dbproc->bcpinfo->direction == DB_OUT || dbproc->bcpinfo->direction == DB_QUERYOUT) {
1633 140 : ret = _bcp_exec_out(dbproc, rows_copied);
1634 140 : } else if (dbproc->bcpinfo->direction == DB_IN) {
1635 140 : ret = _bcp_exec_in(dbproc, rows_copied);
1636 : }
1637 280 : _bcp_free_storage(dbproc);
1638 :
1639 280 : return ret;
1640 : }
1641 :
1642 : /**
1643 : * \ingroup dblib_bcp_internal
1644 : * \brief
1645 : *
1646 : * \param buffer
1647 : * \param size
1648 : * \param f
1649 : *
1650 : * \return SUCCEED or FAIL.
1651 : * \sa BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
1652 : */
1653 : static char *
1654 0 : _bcp_fgets(char *buffer, int size, FILE *f)
1655 : {
1656 0 : char *p = fgets(buffer, size, f);
1657 0 : if (p == NULL)
1658 : return p;
1659 :
1660 : /* discard newline */
1661 0 : p = strchr(buffer, 0) - 1;
1662 0 : if (p >= buffer && *p == '\n')
1663 0 : *p = 0;
1664 : return buffer;
1665 : }
1666 :
1667 : /**
1668 : * \ingroup dblib_bcp
1669 : * \brief Read a format definition file.
1670 : *
1671 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1672 : * \param filename Name that will be passed to fopen(3).
1673 : *
1674 : * \remarks Reads a format file and calls bcp_columns() and bcp_colfmt() as needed.
1675 : *
1676 : * \return SUCCEED or FAIL.
1677 : * \sa bcp_colfmt(), bcp_colfmt_ps(), bcp_columns(), bcp_writefmt()
1678 : */
1679 : RETCODE
1680 0 : bcp_readfmt(DBPROCESS * dbproc, const char filename[])
1681 : {
1682 : BCP_HOSTCOLINFO hostcol[1];
1683 : FILE *ffile;
1684 : char buffer[1024];
1685 0 : float lf_version = 0.0;
1686 0 : int li_numcols = 0;
1687 0 : int colinfo_count = 0;
1688 :
1689 0 : tdsdump_log(TDS_DBG_FUNC, "bcp_readfmt(%p, %s)\n", dbproc, filename? filename:"NULL");
1690 0 : CHECK_CONN(FAIL);
1691 0 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
1692 0 : CHECK_NULP(filename, "bcp_readfmt", 2, FAIL);
1693 :
1694 0 : memset(hostcol, 0, sizeof(hostcol));
1695 :
1696 0 : if ((ffile = fopen(filename, "r")) == NULL) {
1697 0 : dbperror(dbproc, SYBEBUOF, 0);
1698 0 : goto Cleanup;
1699 : }
1700 :
1701 0 : if ((_bcp_fgets(buffer, sizeof(buffer), ffile)) != NULL) {
1702 0 : lf_version = (float)atof(buffer);
1703 0 : } else if (ferror(ffile)) {
1704 0 : dbperror(dbproc, SYBEBRFF, errno);
1705 0 : goto Cleanup;
1706 : }
1707 :
1708 0 : if ((_bcp_fgets(buffer, sizeof(buffer), ffile)) != NULL) {
1709 0 : li_numcols = atoi(buffer);
1710 0 : } else if (ferror(ffile)) {
1711 0 : dbperror(dbproc, SYBEBRFF, errno);
1712 0 : goto Cleanup;
1713 : }
1714 :
1715 0 : if (li_numcols <= 0)
1716 : goto Cleanup;
1717 :
1718 0 : if (bcp_columns(dbproc, li_numcols) == FAIL)
1719 : goto Cleanup;
1720 :
1721 : do {
1722 0 : memset(hostcol, 0, sizeof(hostcol));
1723 :
1724 0 : if (_bcp_fgets(buffer, sizeof(buffer), ffile) == NULL)
1725 : goto Cleanup;
1726 :
1727 0 : if (!_bcp_readfmt_colinfo(dbproc, buffer, hostcol))
1728 : goto Cleanup;
1729 :
1730 0 : if (bcp_colfmt(dbproc, hostcol->host_column, hostcol->datatype,
1731 : hostcol->prefix_len, hostcol->column_len,
1732 0 : hostcol->terminator, hostcol->term_len, hostcol->tab_colnum) == FAIL) {
1733 : goto Cleanup;
1734 : }
1735 :
1736 0 : TDS_ZERO_FREE(hostcol->terminator);
1737 0 : } while (++colinfo_count < li_numcols);
1738 :
1739 0 : if (ferror(ffile)) {
1740 0 : dbperror(dbproc, SYBEBRFF, errno);
1741 0 : goto Cleanup;
1742 : }
1743 :
1744 0 : if (fclose(ffile) != 0) {
1745 0 : dbperror(dbproc, SYBEBUCF, 0);
1746 : /* even if failure is returned ffile is no more valid */
1747 0 : ffile = NULL;
1748 0 : goto Cleanup;
1749 : }
1750 0 : ffile = NULL;
1751 :
1752 0 : if (colinfo_count != li_numcols)
1753 : goto Cleanup;
1754 :
1755 : return SUCCEED;
1756 :
1757 0 : Cleanup:
1758 0 : TDS_ZERO_FREE(hostcol->terminator);
1759 0 : _bcp_free_columns(dbproc);
1760 0 : if (ffile)
1761 0 : fclose(ffile);
1762 : return FAIL;
1763 : }
1764 :
1765 : /**
1766 : * \ingroup dblib_bcp_internal
1767 : * \brief
1768 : *
1769 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1770 : * \param buf
1771 : * \param ci
1772 : *
1773 : * \return SUCCEED or FAIL.
1774 : * \sa BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
1775 : */
1776 : static int
1777 0 : _bcp_readfmt_colinfo(DBPROCESS * dbproc, char *buf, BCP_HOSTCOLINFO * ci)
1778 : {
1779 : char *tok;
1780 : int whichcol;
1781 : char term[30];
1782 : int i;
1783 : char *lasts;
1784 :
1785 : enum nextcol
1786 : {
1787 : HOST_COLUMN,
1788 : DATATYPE,
1789 : PREFIX_LEN,
1790 : COLUMN_LEN,
1791 : TERMINATOR,
1792 : TAB_COLNUM,
1793 : NO_MORE_COLS
1794 : };
1795 :
1796 0 : assert(dbproc);
1797 0 : assert(buf);
1798 0 : assert(ci);
1799 0 : tdsdump_log(TDS_DBG_FUNC, "_bcp_readfmt_colinfo(%p, %s, %p)\n", dbproc, buf, ci);
1800 :
1801 0 : tok = strtok_r(buf, " \t", &lasts);
1802 0 : whichcol = HOST_COLUMN;
1803 :
1804 : /* TODO use a better way to get an int atoi is very error prone */
1805 0 : while (tok != NULL && whichcol != NO_MORE_COLS) {
1806 0 : switch (whichcol) {
1807 :
1808 0 : case HOST_COLUMN:
1809 0 : ci->host_column = atoi(tok);
1810 :
1811 0 : if (ci->host_column < 1) {
1812 0 : dbperror(dbproc, SYBEBIHC, 0);
1813 0 : return (FALSE);
1814 : }
1815 :
1816 : whichcol = DATATYPE;
1817 : break;
1818 :
1819 0 : case DATATYPE:
1820 0 : if (strcmp(tok, "SYBCHAR") == 0)
1821 0 : ci->datatype = SYBCHAR;
1822 0 : else if (strcmp(tok, "SYBTEXT") == 0)
1823 0 : ci->datatype = SYBTEXT;
1824 0 : else if (strcmp(tok, "SYBBINARY") == 0)
1825 0 : ci->datatype = SYBBINARY;
1826 0 : else if (strcmp(tok, "SYBIMAGE") == 0)
1827 0 : ci->datatype = SYBIMAGE;
1828 0 : else if (strcmp(tok, "SYBINT1") == 0)
1829 0 : ci->datatype = SYBINT1;
1830 0 : else if (strcmp(tok, "SYBINT2") == 0)
1831 0 : ci->datatype = SYBINT2;
1832 0 : else if (strcmp(tok, "SYBINT4") == 0)
1833 0 : ci->datatype = SYBINT4;
1834 0 : else if (strcmp(tok, "SYBINT8") == 0)
1835 0 : ci->datatype = SYBINT8;
1836 0 : else if (strcmp(tok, "SYBFLT8") == 0)
1837 0 : ci->datatype = SYBFLT8;
1838 0 : else if (strcmp(tok, "SYBREAL") == 0)
1839 0 : ci->datatype = SYBREAL;
1840 0 : else if (strcmp(tok, "SYBBIT") == 0)
1841 0 : ci->datatype = SYBBIT;
1842 0 : else if (strcmp(tok, "SYBNUMERIC") == 0)
1843 0 : ci->datatype = SYBNUMERIC;
1844 0 : else if (strcmp(tok, "SYBDECIMAL") == 0)
1845 0 : ci->datatype = SYBDECIMAL;
1846 0 : else if (strcmp(tok, "SYBMONEY") == 0)
1847 0 : ci->datatype = SYBMONEY;
1848 0 : else if (strcmp(tok, "SYBMONEY4") == 0)
1849 0 : ci->datatype = SYBMONEY4;
1850 0 : else if (strcmp(tok, "SYBDATETIME") == 0)
1851 0 : ci->datatype = SYBDATETIME;
1852 0 : else if (strcmp(tok, "SYBDATETIME4") == 0)
1853 0 : ci->datatype = SYBDATETIME4;
1854 : /* TODO SQL* for MS
1855 : SQLNCHAR SQLBIGINT SQLTINYINT SQLSMALLINT
1856 : SQLUNIQUEID SQLVARIANT SQLUDT */
1857 : else {
1858 0 : dbperror(dbproc, SYBEBUDF, 0);
1859 0 : return (FALSE);
1860 : }
1861 :
1862 : whichcol = PREFIX_LEN;
1863 : break;
1864 :
1865 0 : case PREFIX_LEN:
1866 0 : ci->prefix_len = atoi(tok);
1867 0 : whichcol = COLUMN_LEN;
1868 0 : break;
1869 0 : case COLUMN_LEN:
1870 0 : ci->column_len = atoi(tok);
1871 0 : whichcol = TERMINATOR;
1872 0 : break;
1873 0 : case TERMINATOR:
1874 :
1875 0 : if (*tok++ != '\"')
1876 : return (FALSE);
1877 :
1878 0 : for (i = 0; *tok != '\"' && i < sizeof(term); i++) {
1879 0 : if (*tok == '\\') {
1880 0 : tok++;
1881 0 : switch (*tok) {
1882 0 : case 't':
1883 0 : term[i] = '\t';
1884 0 : break;
1885 0 : case 'n':
1886 0 : term[i] = '\n';
1887 0 : break;
1888 0 : case 'r':
1889 0 : term[i] = '\r';
1890 0 : break;
1891 0 : case '\\':
1892 0 : term[i] = '\\';
1893 0 : break;
1894 0 : case '0':
1895 0 : term[i] = '\0';
1896 0 : break;
1897 : default:
1898 : return (FALSE);
1899 : }
1900 0 : tok++;
1901 : } else
1902 0 : term[i] = *tok++;
1903 : }
1904 :
1905 0 : if (*tok != '\"')
1906 : return (FALSE);
1907 :
1908 0 : ci->term_len = i;
1909 0 : TDS_ZERO_FREE(ci->terminator);
1910 0 : if (i > 0) {
1911 0 : if ((ci->terminator = tds_new(BYTE, i)) == NULL) {
1912 0 : dbperror(dbproc, SYBEMEM, errno);
1913 0 : return FALSE;
1914 : }
1915 0 : memcpy(ci->terminator, term, i);
1916 : }
1917 :
1918 : whichcol = TAB_COLNUM;
1919 : break;
1920 :
1921 0 : case TAB_COLNUM:
1922 0 : ci->tab_colnum = atoi(tok);
1923 0 : whichcol = NO_MORE_COLS;
1924 0 : break;
1925 :
1926 : }
1927 0 : tok = strtok_r(NULL, " \t", &lasts);
1928 : }
1929 0 : if (whichcol == NO_MORE_COLS)
1930 : return (TRUE);
1931 : else
1932 0 : return (FALSE);
1933 : }
1934 :
1935 : #if defined(DBLIB_UNIMPLEMENTED)
1936 : /**
1937 : * \ingroup dblib_bcp
1938 : * \brief Write a format definition file. Not Implemented.
1939 : *
1940 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1941 : * \param filename Name that would be passed to fopen(3).
1942 : *
1943 : * \remarks Reads a format file and calls bcp_columns() and bcp_colfmt() as needed.
1944 : * \a FreeTDS includes freebcp, a utility to copy data to or from a host file.
1945 : *
1946 : * \todo For completeness, \a freebcp ought to be able to create format files, but that functionality
1947 : * is currently lacking, as is bcp_writefmt().
1948 : * \todo See the vendors' documentation for the format of these files.
1949 : *
1950 : * \return SUCCEED or FAIL.
1951 : * \sa bcp_colfmt(), bcp_colfmt_ps(), bcp_columns(), bcp_readfmt()
1952 : */
1953 : RETCODE
1954 : bcp_writefmt(DBPROCESS * dbproc, const char filename[])
1955 : {
1956 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_writefmt(%p, %s)\n", dbproc, filename? filename:"NULL");
1957 : CHECK_CONN(FAIL);
1958 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
1959 : CHECK_NULP(filename, "bcp_writefmt", 2, FAIL);
1960 :
1961 : #if 0
1962 : dbperror(dbproc, SYBEBUFF, errno); /* bcp: Unable to create format file */
1963 : dbperror(dbproc, SYBEBWFF, errno); /* I/O error while writing bcp format file */
1964 : #endif
1965 :
1966 : return FAIL;
1967 : }
1968 :
1969 : /**
1970 : * \ingroup dblib_bcp
1971 : * \brief Write some text or image data to the server. Not implemented, sadly.
1972 : *
1973 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
1974 : * \param size How much to write, in bytes.
1975 : * \param text Address of the data to be written.
1976 : * \remarks For a SYBTEXT or SYBIMAGE column, bcp_bind() can be called with
1977 : * a NULL varaddr parameter. If it is, bcp_sendrow() will return control
1978 : * to the application after the non-text data have been sent. The application then calls
1979 : * bcp_moretext() -- usually in a loop -- to send the text data in manageable chunks.
1980 : * \todo implement bcp_moretext().
1981 : * \return SUCCEED or FAIL.
1982 : * \sa bcp_bind(), bcp_sendrow(), dbmoretext(), dbwritetext()
1983 : */
1984 : RETCODE
1985 : bcp_moretext(DBPROCESS * dbproc, DBINT size, BYTE * text)
1986 : {
1987 : tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_moretext(%p, %d, %p)\n", dbproc, size, text);
1988 : CHECK_CONN(FAIL);
1989 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
1990 : CHECK_NULP(text, "bcp_moretext", 3, FAIL);
1991 :
1992 : #if 0
1993 : dbperror(dbproc, SYBEBCMTXT, 0);
1994 : /* bcp_moretext may be used only when there is at least one text or image column in the server table */
1995 : dbperror(dbproc, SYBEBTMT, 0);
1996 : /* Attempt to send too much text data via the bcp_moretext call */
1997 : #endif
1998 : return FAIL;
1999 : }
2000 : #endif
2001 :
2002 : /**
2003 : * \ingroup dblib_bcp
2004 : * \brief Commit a set of rows to the table.
2005 : *
2006 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2007 : * \remarks If not called, bcp_done() will cause the rows to be saved.
2008 : * \return Count of rows saved, or -1 on error.
2009 : * \sa bcp_bind(), bcp_done(), bcp_sendrow()
2010 : */
2011 : DBINT
2012 10 : bcp_batch(DBPROCESS * dbproc)
2013 : {
2014 10 : int rows_copied = 0;
2015 :
2016 10 : tdsdump_log(TDS_DBG_FUNC, "bcp_batch(%p)\n", dbproc);
2017 10 : CHECK_CONN(-1);
2018 10 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, -1);
2019 :
2020 10 : if (TDS_FAILED(tds_bcp_done(dbproc->tds_socket, &rows_copied)))
2021 : return -1;
2022 :
2023 10 : tds_bcp_start(dbproc->tds_socket, dbproc->bcpinfo);
2024 :
2025 10 : return rows_copied;
2026 : }
2027 :
2028 : /**
2029 : * \ingroup dblib_bcp
2030 : * \brief Conclude the transfer of data from program variables.
2031 : *
2032 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2033 : * \remarks Do not overlook this function. According to Sybase, failure to call bcp_done()
2034 : * "will result in unpredictable errors".
2035 : * \return As with bcp_batch(), the count of rows saved, or -1 on error.
2036 : * \sa bcp_batch(), bcp_bind(), bcp_moretext(), bcp_sendrow()
2037 : */
2038 : DBINT
2039 38 : bcp_done(DBPROCESS * dbproc)
2040 : {
2041 : int rows_copied;
2042 :
2043 38 : tdsdump_log(TDS_DBG_FUNC, "bcp_done(%p)\n", dbproc);
2044 38 : CHECK_CONN(-1);
2045 :
2046 38 : if (!(dbproc->bcpinfo))
2047 : return -1;
2048 :
2049 38 : if (TDS_FAILED(tds_bcp_done(dbproc->tds_socket, &rows_copied)))
2050 : return -1;
2051 :
2052 20 : _bcp_free_storage(dbproc);
2053 :
2054 20 : return rows_copied;
2055 : }
2056 :
2057 : /**
2058 : * \ingroup dblib_bcp
2059 : * \brief Bind a program host variable to a database column
2060 : *
2061 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2062 : * \param varaddr address of host variable
2063 : * \param prefixlen length of any prefix found at the beginning of \a varaddr, in bytes.
2064 : Use zero for fixed-length datatypes.
2065 : * \param varlen bytes of data in \a varaddr. Zero for NULL, -1 for fixed-length datatypes.
2066 : * \param terminator byte sequence that marks the end of the data in \a varaddr
2067 : * \param termlen length of \a terminator
2068 : * \param vartype datatype of the host variable
2069 : * \param table_column Nth column, starting at 1, in the table.
2070 : *
2071 : * \remarks The order of operation is:
2072 : * - bcp_init() with \a hfile == NULL and \a direction == DB_IN.
2073 : * - bcp_bind(), once per column you want to write to
2074 : * - bcp_batch(), optionally, to commit a set of rows
2075 : * - bcp_done()
2076 : * \return SUCCEED or FAIL.
2077 : * \sa bcp_batch(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(),
2078 : * bcp_done(), bcp_exec(), bcp_moretext(), bcp_sendrow()
2079 : */
2080 : RETCODE
2081 324 : bcp_bind(DBPROCESS * dbproc, BYTE * varaddr, int prefixlen, DBINT varlen,
2082 : BYTE * terminator, int termlen, int db_vartype, int table_column)
2083 : {
2084 : TDS_SERVER_TYPE vartype;
2085 : TDSCOLUMN *colinfo;
2086 :
2087 324 : tdsdump_log(TDS_DBG_FUNC, "bcp_bind(%p, %p, %d, %d -- %p, %d, %s, %d)\n",
2088 : dbproc, varaddr, prefixlen, varlen,
2089 : terminator, termlen, dbprtype(db_vartype), table_column);
2090 324 : CHECK_CONN(FAIL);
2091 324 : CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
2092 648 : DBPERROR_RETURN(db_vartype != 0 && !is_tds_type_valid(db_vartype), SYBEUDTY);
2093 324 : vartype = (TDS_SERVER_TYPE) db_vartype;
2094 :
2095 324 : if (dbproc->hostfileinfo != NULL) {
2096 0 : dbperror(dbproc, SYBEBCPB, 0);
2097 0 : return FAIL;
2098 : }
2099 :
2100 324 : if (dbproc->bcpinfo->direction != DB_IN) {
2101 0 : dbperror(dbproc, SYBEBCPN, 0);
2102 0 : return FAIL;
2103 : }
2104 :
2105 324 : if (varlen < -1) {
2106 0 : dbperror(dbproc, SYBEBCVLEN, 0);
2107 0 : return FAIL;
2108 : }
2109 :
2110 324 : if (prefixlen != 0 && prefixlen != 1 && prefixlen != 2 && prefixlen != 4) {
2111 0 : dbperror(dbproc, SYBEBCBPREF, 0);
2112 0 : return FAIL;
2113 : }
2114 :
2115 324 : if (prefixlen == 0 && varlen == -1 && termlen == -1 && !is_fixed_type(vartype)) {
2116 0 : tdsdump_log(TDS_DBG_FUNC, "bcp_bind(): non-fixed type %d requires prefix or terminator\n", vartype);
2117 : return FAIL;
2118 : }
2119 :
2120 324 : if (is_fixed_type(vartype) && (varlen != -1 && varlen != 0)) {
2121 0 : dbperror(dbproc, SYBEBCIT, 0);
2122 0 : return FAIL;
2123 : }
2124 :
2125 324 : if (table_column <= 0 || table_column > dbproc->bcpinfo->bindinfo->num_cols) {
2126 0 : dbperror(dbproc, SYBECNOR, 0);
2127 0 : return FAIL;
2128 : }
2129 :
2130 324 : if (varaddr == NULL && (prefixlen != 0 || termlen != 0)) {
2131 0 : dbperror(dbproc, SYBEBCBNPR, 0);
2132 0 : return FAIL;
2133 : }
2134 :
2135 324 : colinfo = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
2136 :
2137 : /* If varaddr is NULL and varlen greater than 0, the table column type must be SYBTEXT or SYBIMAGE
2138 : and the program variable type must be SYBTEXT, SYBCHAR, SYBIMAGE or SYBBINARY */
2139 324 : if (varaddr == NULL && varlen > 0) {
2140 0 : int fOK = (colinfo->column_type == SYBTEXT || colinfo->column_type == SYBIMAGE) &&
2141 0 : (vartype == SYBTEXT || vartype == SYBCHAR || vartype == SYBIMAGE || vartype == SYBBINARY );
2142 : if( !fOK ) {
2143 0 : dbperror(dbproc, SYBEBCBNTYP, 0);
2144 0 : tdsdump_log(TDS_DBG_FUNC, "bcp_bind: SYBEBCBNTYP: column=%d and vartype=%d (should fail?)\n",
2145 0 : colinfo->column_type, vartype);
2146 : /* return FAIL; */
2147 : }
2148 : }
2149 :
2150 324 : colinfo->column_varaddr = (char *)varaddr;
2151 324 : colinfo->column_bindtype = vartype;
2152 324 : colinfo->column_bindlen = varlen;
2153 324 : colinfo->bcp_prefix_len = prefixlen;
2154 :
2155 324 : TDS_ZERO_FREE(colinfo->bcp_terminator);
2156 324 : colinfo->bcp_term_len = 0;
2157 324 : if (termlen > 0) {
2158 54 : if ((colinfo->bcp_terminator = tds_new(TDS_CHAR, termlen)) == NULL) {
2159 0 : dbperror(dbproc, SYBEMEM, errno);
2160 0 : return FAIL;
2161 : }
2162 54 : memcpy(colinfo->bcp_terminator, terminator, termlen);
2163 54 : colinfo->bcp_term_len = termlen;
2164 : }
2165 :
2166 : return SUCCEED;
2167 : }
2168 :
2169 : static void
2170 2 : _bcp_null_error(TDSBCPINFO *bcpinfo, int index TDS_UNUSED, int offset TDS_UNUSED)
2171 : {
2172 2 : DBPROCESS *dbproc = (DBPROCESS *) bcpinfo->parent;
2173 2 : dbperror(dbproc, SYBEBCNN, 0);
2174 2 : }
2175 :
2176 : /**
2177 : * \ingroup dblib_bcp_internal
2178 : * \brief For a bcp in from program variables, get the data from the host variable
2179 : *
2180 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2181 : * \param bindcol
2182 : *
2183 : * \return TDS_SUCCESS or TDS_FAIL.
2184 : * \sa _bcp_add_fixed_columns, _bcp_add_variable_columns, _bcp_send_bcp_record
2185 : */
2186 : static TDSRET
2187 4138 : _bcp_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset TDS_UNUSED)
2188 : {
2189 : TDS_SERVER_TYPE coltype, desttype;
2190 : int collen;
2191 : int bytes_read;
2192 : BYTE *dataptr;
2193 4138 : DBPROCESS *dbproc = (DBPROCESS *) bcpinfo->parent;
2194 : TDSRET rc;
2195 :
2196 4138 : tdsdump_log(TDS_DBG_FUNC, "_bcp_get_col_data(%p, %p)\n", bcpinfo, bindcol);
2197 4138 : CHECK_CONN(TDS_FAIL);
2198 4138 : CHECK_NULP(bindcol, "_bcp_get_col_data", 2, TDS_FAIL);
2199 :
2200 4138 : dataptr = (BYTE *) bindcol->column_varaddr;
2201 :
2202 4138 : collen = 0;
2203 :
2204 : /* If a prefix length specified, read the correct amount of data. */
2205 :
2206 4138 : if (bindcol->bcp_prefix_len > 0) {
2207 :
2208 0 : switch (bindcol->bcp_prefix_len) {
2209 0 : case 1:
2210 0 : collen = TDS_GET_UA1(dataptr);
2211 0 : dataptr += 1;
2212 0 : break;
2213 0 : case 2:
2214 0 : collen = (TDS_SMALLINT) TDS_GET_UA2(dataptr);
2215 0 : dataptr += 2;
2216 0 : break;
2217 0 : case 4:
2218 0 : collen = (TDS_INT) TDS_GET_UA4(dataptr);
2219 0 : dataptr += 4;
2220 0 : break;
2221 : }
2222 0 : if (collen <= 0)
2223 : goto null_data;
2224 : }
2225 :
2226 : /* if (Max) column length specified take that into consideration. */
2227 :
2228 4138 : if (bindcol->column_bindlen >= 0) {
2229 2800 : if (bindcol->column_bindlen == 0)
2230 : goto null_data;
2231 1500 : if (collen)
2232 0 : collen = TDS_MIN(bindcol->column_bindlen, collen);
2233 : else
2234 : collen = bindcol->column_bindlen;
2235 : }
2236 :
2237 2838 : desttype = tds_get_conversion_type(bindcol->column_type, bindcol->column_size);
2238 :
2239 2838 : coltype = bindcol->column_bindtype == 0 ? desttype : (TDS_SERVER_TYPE) bindcol->column_bindtype;
2240 :
2241 : /* Fixed Length data - this overrides anything else specified */
2242 2838 : if (is_fixed_type(coltype))
2243 750 : collen = tds_get_size_by_type(coltype);
2244 :
2245 : /* read the data, finally */
2246 :
2247 2838 : if (bindcol->bcp_term_len > 0) { /* terminated field */
2248 88 : bytes_read = _bcp_get_term_var(dataptr, (BYTE *)bindcol->bcp_terminator, bindcol->bcp_term_len);
2249 :
2250 88 : if (collen <= 0 || bytes_read < collen)
2251 88 : collen = bytes_read;
2252 :
2253 88 : if (collen == 0)
2254 : goto null_data;
2255 : }
2256 :
2257 2810 : if (collen < 0)
2258 0 : collen = (int) strlen((char *) dataptr);
2259 :
2260 2810 : rc = _bcp_convert_in(dbproc, coltype, (const TDS_CHAR*) dataptr, collen,
2261 : desttype, bindcol->bcp_column_data);
2262 2810 : if (TDS_FAILED(rc))
2263 : return rc;
2264 2810 : rtrim_bcpcol(bindcol);
2265 :
2266 2810 : return TDS_SUCCESS;
2267 :
2268 1328 : null_data:
2269 1328 : bindcol->bcp_column_data->datalen = 0;
2270 1328 : bindcol->bcp_column_data->is_null = true;
2271 1328 : return TDS_SUCCESS;
2272 : }
2273 :
2274 : /**
2275 : * Function to read data from file. I this case is empty as data
2276 : * are already on bcp_column_data
2277 : */
2278 : static TDSRET
2279 1578 : _bcp_no_get_col_data(TDSBCPINFO *bcpinfo TDS_UNUSED, TDSCOLUMN *bindcol TDS_UNUSED, int offset TDS_UNUSED)
2280 : {
2281 1578 : return TDS_SUCCESS;
2282 : }
2283 :
2284 : /**
2285 : * Get the data for bcp-in from program variables, where the program data
2286 : * have been identified as character terminated,
2287 : * This is a low-level, internal function. Call it correctly.
2288 : */
2289 : /**
2290 : * \ingroup dblib_bcp_internal
2291 : * \brief
2292 : *
2293 : * \param pdata
2294 : * \param term
2295 : * \param term_len
2296 : *
2297 : * \return data length.
2298 : */
2299 : static int
2300 88 : _bcp_get_term_var(const BYTE * pdata, const BYTE * term, int term_len)
2301 : {
2302 : int bufpos;
2303 :
2304 88 : assert(term_len > 0);
2305 :
2306 : /* if bufpos becomes negative, we probably failed to find the terminator */
2307 330 : for (bufpos = 0; bufpos >= 0 && memcmp(pdata, term, term_len) != 0; pdata++) {
2308 330 : bufpos++;
2309 : }
2310 :
2311 : assert(bufpos >= 0);
2312 88 : return bufpos;
2313 : }
2314 :
2315 : /**
2316 : * \ingroup dblib_bcp_internal
2317 : * \brief trim a string of trailing blanks
2318 : *
2319 : * Replaces spaces at the end of a string with NULs
2320 : * \param str pointer to a character buffer (not null-terminated)
2321 : * \param len size of the \a str in bytes
2322 : *
2323 : * \return modified length
2324 : */
2325 : static int
2326 : rtrim(char *str, int len)
2327 : {
2328 1022 : char *p = str + len - 1;
2329 :
2330 1516 : while (p > str && *p == ' ') {
2331 494 : *p-- = '\0';
2332 : }
2333 1022 : return (int)(1 + p - str);
2334 : }
2335 :
2336 : static int
2337 : rtrim_u16(uint16_t *str, int len, uint16_t space)
2338 : {
2339 62 : uint16_t *p = str + len / 2 - 1;
2340 :
2341 62 : while (p > str && *p == space) {
2342 0 : *p-- = '\0';
2343 : }
2344 62 : return (int)(1 + p - str) * 2;
2345 : }
2346 :
2347 : /**
2348 : * \ingroup dblib_bcp_internal
2349 : * \brief
2350 : *
2351 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2352 : */
2353 : static void
2354 840 : _bcp_free_columns(DBPROCESS * dbproc)
2355 : {
2356 : int i;
2357 :
2358 840 : tdsdump_log(TDS_DBG_FUNC, "_bcp_free_columns(%p)\n", dbproc);
2359 840 : assert(dbproc && dbproc->hostfileinfo);
2360 :
2361 840 : if (dbproc->hostfileinfo->host_columns) {
2362 3032 : for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
2363 3032 : TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns[i]->terminator);
2364 3032 : TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns[i]);
2365 : }
2366 560 : TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns);
2367 560 : dbproc->hostfileinfo->host_colcount = 0;
2368 : }
2369 840 : }
2370 :
2371 : /**
2372 : * \ingroup dblib_bcp_internal
2373 : * \brief
2374 : *
2375 : * \param dbproc contains all information needed by db-lib to manage communications with the server.
2376 : *
2377 : * \sa bcp_done(), bcp_exec(), bcp_init()
2378 : */
2379 : static void
2380 618 : _bcp_free_storage(DBPROCESS * dbproc)
2381 : {
2382 618 : tdsdump_log(TDS_DBG_FUNC, "_bcp_free_storage(%p)\n", dbproc);
2383 618 : assert(dbproc);
2384 :
2385 618 : if (dbproc->hostfileinfo) {
2386 280 : TDS_ZERO_FREE(dbproc->hostfileinfo->hostfile);
2387 280 : TDS_ZERO_FREE(dbproc->hostfileinfo->errorfile);
2388 280 : _bcp_free_columns(dbproc);
2389 280 : TDS_ZERO_FREE(dbproc->hostfileinfo);
2390 : }
2391 :
2392 618 : tds_free_bcpinfo(dbproc->bcpinfo);
2393 618 : dbproc->bcpinfo = NULL;
2394 618 : }
2395 :
|