54 #define HOST_COL_CONV_ERROR 1
55 #define HOST_COL_NULL_ERROR 2
58 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
63 #elif defined(_WIN32) || defined(_WIN64)
66 # if defined(HAVE__FSEEKI64) && defined(HAVE__FTELLI64)
67 # define fseeko(f,o,w) _fseeki64((f),o,w)
68 # define ftello(f) _ftelli64((f))
70 # define fseeko(f,o,w) (_lseeki64(fileno(f),o,w) == -1 ? -1 : 0)
71 # define ftello(f) _telli64(fileno(f))
75 #define fseeko(f,o,w) fseek(f,o,w)
76 #define ftello(f) ftell(f)
85 static int rtrim(
char *,
int);
119 for (icol = 0; icol < ncols; icol++) {
121 int prefixlen = 0, termlen = 0;
167 dbproc, tblname? tblname:
"NULL", hfile? hfile:
"NULL", errfile? errfile:
"NULL", direction);
178 if (tblname ==
NULL) {
272 table_column <= dbproc->bcpinfo->bindinfo->num_cols,
SYBECNOR,
FAIL);
305 if (host_colcount < 1) {
320 for (
i = 0;
i < host_colcount;
i++) {
373 int host_termlen,
int table_colnum)
379 dbproc, host_colnum, host_type, host_prefixlen, (
int) host_collen, host_term, host_termlen, table_colnum);
388 if (host_termlen < 0)
396 if (host_colnum < 1) {
406 if (host_prefixlen != 0 && host_prefixlen != 1 && host_prefixlen != 2 && host_prefixlen != 4 && host_prefixlen != -1) {
412 if (table_colnum <= 0 && host_type == 0) {
422 if (host_type && host_prefixlen == 0 && host_collen == -1 && host_termlen == -1 && !
is_fixed_type(host_type)) {
427 if (host_collen < -1) {
433 if (
is_fixed_type(host_type) && (host_collen != -1 && host_collen != 0)) {
435 "bcp_colfmt: changing host_collen to -1 from %d for fixed type %d.\n",
436 host_collen, host_type);
444 if (host_term ==
NULL && host_termlen > 0) {
452 if (host_term && host_termlen > 0) {
505 int host_prefixlen,
DBINT host_collen,
BYTE * host_term,
int host_termlen,
int table_colnum,
DBTYPEINFO * typeinfo)
616 static const char *
const hints[] = {
617 "ORDER",
"ROWS_PER_BATCH",
"KILOBYTES_PER_BATCH",
"TABLOCK",
"CHECK_CONSTRAINTS",
"FIRE_TRIGGERS",
NULL
630 if (!
value || valuelen <= 0)
633 for (
i = 0; hints[
i];
i++) {
737 bcpdatefmt, &when, 3);
779 memcpy(*p_data, cr.
c, buflen);
790 strcpy ((
char *) (*p_data),
" ");
844 if (fwrite(&u, plen, 1, hostfile) == 1)
864 FILE *hostfile =
NULL;
878 const char *bcpdatefmt;
888 bcpdatefmt = getenv(
"FREEBCP_DATEFMT");
890 bcpdatefmt =
"%Y-%m-%d %H:%M:%S.%z";
976 if (fwrite(data, buflen, 1, hostfile) != 1)
988 if (fclose(hostfile) != 0) {
1005 *rows_copied = rows_written;
1097 int i, collen, data_is_null;
1143 if (fread(&ti, 1, 1, hostfile) != 1)
1145 collen = ti ? ti : -1;
1148 if (fread(&
si, 2, 1, hostfile) != 1)
1153 if (fread(&li, 4, 1, hostfile) != 1)
1173 if (!data_is_null && hostcol->
column_len >= 0) {
1191 col_start =
ftello(hostfile);
1211 (
i+1), (
long) collen);
1223 if (col_bytes > 0x7fffffffl) {
1231 collen = (
int)col_bytes;
1247 if (coldata ==
NULL) {
1253 coldata[collen] = 0;
1265 if (fread(coldata, collen, 1, hostfile) != 1) {
1275 tdsdump_log(
TDS_DBG_FUNC,
"Data read from hostfile: collen is now %d, data_is_null is %d\n", collen, data_is_null);
1291 "_bcp_read_hostfile failed to convert %d bytes at offset 0x%" PRIx64 " in the data file.\n",
1393 FILE *hostfile, *errfile =
NULL;
1398 int i, row_of_hostfile, rows_written_so_far;
1399 int row_error, row_error_count;
1420 row_of_hostfile = 0;
1421 rows_written_so_far = 0;
1423 row_start =
ftello(hostfile);
1424 row_error_count = 0;
1443 if (errfile !=
NULL) {
1444 char *row_in_error =
NULL;
1449 count = fprintf(errfile,
1450 "#@ data conversion error on host data file Row %d Column %d\n",
1451 row_of_hostfile,
i + 1);
1456 count = fprintf(errfile,
"#@ Attempt to bulk-copy a NULL value into Server column"
1457 " which does not accept NULL values. Row %d, Column %d\n",
1458 row_of_hostfile,
i + 1);
1466 row_end =
ftello(hostfile);
1469 error_row_size = row_end - row_start;
1472 while (error_row_size > 0) {
1473 size_t chunk = ((size_t) error_row_size
1476 : (size_t) error_row_size;
1478 if (!row_in_error) {
1479 if ((row_in_error =
tds_new(
char, chunk)) ==
NULL) {
1484 if (fread(row_in_error, chunk, 1, hostfile) != 1) {
1486 "BILL fread failed after fseek\n");
1488 count = (
int)fwrite(row_in_error, chunk, 1, errfile);
1489 if( (
size_t)count < chunk ) {
1492 error_row_size -= chunk;
1497 count = fprintf(errfile,
"\n");
1511 rows_written_so_far++;
1521 *rows_copied += rows_written_so_far;
1522 rows_written_so_far = 0;
1532 row_start =
ftello(hostfile);
1536 if( row_error_count == 0 && row_of_hostfile < dbproc->hostfileinfo->firstrow ) {
1541 if (errfile && 0 != fclose(errfile) ) {
1545 if (fclose(hostfile) != 0) {
1551 *rows_copied += rows_written_so_far;
1580 if (rows_copied ==
NULL)
1581 rows_copied = &dummy_copied;
1612 p = strchr(
buffer, 0) - 1;
1613 if (p >=
buffer && *p ==
'\n')
1638 int colinfo_count = 0;
1645 memset(hostcol, 0,
sizeof(hostcol));
1647 if ((ffile = fopen(filename,
"r")) ==
NULL) {
1654 }
else if (ferror(ffile)) {
1660 li_numcols = atoi(
buffer);
1661 }
else if (ferror(ffile)) {
1666 if (li_numcols <= 0)
1673 memset(hostcol, 0,
sizeof(hostcol));
1688 }
while (++colinfo_count < li_numcols);
1690 if (ferror(ffile)) {
1695 if (fclose(ffile) != 0) {
1703 if (colinfo_count != li_numcols)
1753 whichcol = HOST_COLUMN;
1756 while (tok !=
NULL && whichcol != NO_MORE_COLS) {
1767 whichcol = DATATYPE;
1771 if (
strcmp(tok,
"SYBCHAR") == 0)
1773 else if (
strcmp(tok,
"SYBTEXT") == 0)
1775 else if (
strcmp(tok,
"SYBBINARY") == 0)
1777 else if (
strcmp(tok,
"SYBIMAGE") == 0)
1779 else if (
strcmp(tok,
"SYBINT1") == 0)
1781 else if (
strcmp(tok,
"SYBINT2") == 0)
1783 else if (
strcmp(tok,
"SYBINT4") == 0)
1785 else if (
strcmp(tok,
"SYBINT8") == 0)
1787 else if (
strcmp(tok,
"SYBFLT8") == 0)
1789 else if (
strcmp(tok,
"SYBREAL") == 0)
1791 else if (
strcmp(tok,
"SYBBIT") == 0)
1793 else if (
strcmp(tok,
"SYBNUMERIC") == 0)
1795 else if (
strcmp(tok,
"SYBDECIMAL") == 0)
1797 else if (
strcmp(tok,
"SYBMONEY") == 0)
1799 else if (
strcmp(tok,
"SYBMONEY4") == 0)
1801 else if (
strcmp(tok,
"SYBDATETIME") == 0)
1803 else if (
strcmp(tok,
"SYBDATETIME4") == 0)
1813 whichcol = PREFIX_LEN;
1818 whichcol = COLUMN_LEN;
1829 for (
i = 0; *tok !=
'\"' &&
i <
sizeof(term);
i++) {
1869 whichcol = TAB_COLNUM;
1874 whichcol = NO_MORE_COLS;
1880 if (whichcol == NO_MORE_COLS)
1886 #if defined(DBLIB_UNIMPLEMENTED)
1965 int rows_copied = 0;
2033 BYTE *
terminator,
int termlen,
int db_vartype,
int table_column)
2039 dbproc, varaddr, prefixlen, varlen,
2061 if (prefixlen != 0 && prefixlen != 1 && prefixlen != 2 && prefixlen != 4) {
2066 if (prefixlen == 0 && varlen == -1 && termlen == -1 && !
is_fixed_type(vartype)) {
2071 if (
is_fixed_type(vartype) && (varlen != -1 && varlen != 0)) {
2081 if (varaddr ==
NULL && (prefixlen != 0 || termlen != 0)) {
2090 if (varaddr ==
NULL && varlen > 0) {
2195 collen = (bytes_read < collen) ? bytes_read : collen;
2197 collen = bytes_read;
2204 collen = (
int) strlen((
char *) dataptr);
2252 for (bufpos = 0; bufpos >= 0 && memcmp(pdata, term, term_len) != 0; pdata++) {
2275 while (p >
str && *p ==
' ') {
2278 return (
int)(1 + p -
str);
static const int chunk_size
static const char si[8][64]
static void init_hostfile_columns(DBPROCESS *dbproc)
static int _bcp_convert_out(DBPROCESS *dbproc, TDSCOLUMN *curcol, BCP_HOSTCOLINFO *hostcol, TDS_UCHAR **p_data, const char *bcpdatefmt)
Convert column for output (usually to a file) Conversion is slightly different from input as:
static TDSRET _bcp_convert_in(DBPROCESS *dbproc, TDS_SERVER_TYPE srctype, const TDS_CHAR *src, TDS_UINT srclen, TDS_SERVER_TYPE desttype, BCPCOLDATA *coldata)
Convert column for input to a table.
static STATUS _bcp_check_eof(DBPROCESS *dbproc, FILE *file, int icol)
#define HOST_COL_NULL_ERROR
static int bcp_cache_prefix_len(BCP_HOSTCOLINFO *hostcol, const TDSCOLUMN *curcol)
#define HOST_COL_CONV_ERROR
int bcp_getbatchsize(DBPROCESS *dbproc)
static RETCODE bcp_write_prefix(FILE *hostfile, BCP_HOSTCOLINFO *hostcol, TDSCOLUMN *curcol, int buflen)
static TDSRET _bcp_no_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset)
Function to read data from file.
void _dblib_convert_err(DBPROCESS *dbproc, TDS_INT len)
#define DBPERROR_RETURN(x, msg)
#define CHECK_NULP(x, func, param_num, ret)
#define CHECK_PARAMETER(x, msg, ret)
static STATUS _bcp_read_hostfile(DBPROCESS *dbproc, FILE *hostfile, int *row_error)
static TDSRET _bcp_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset)
For a bcp in from program variables, get the data from the host variable.
static int rtrim(char *, int)
trim a string of trailing blanks
static RETCODE _bcp_exec_in(DBPROCESS *dbproc, DBINT *rows_copied)
static void _bcp_free_storage(DBPROCESS *dbproc)
static int _bcp_get_term_var(BYTE *pdata, BYTE *term, int term_len)
Get the data for bcp-in from program variables, where the program data have been identified as charac...
static void _bcp_free_columns(DBPROCESS *dbproc)
static RETCODE _bcp_exec_out(DBPROCESS *dbproc, DBINT *rows_copied)
static char * _bcp_fgets(char *buffer, int size, FILE *f)
static int _bcp_readfmt_colinfo(DBPROCESS *dbproc, char *buf, BCP_HOSTCOLINFO *ci)
RETCODE bcp_colfmt(DBPROCESS *dbproc, int host_colnum, int host_type, int host_prefixlen, DBINT host_collen, const BYTE *host_term, int host_termlen, int table_colnum)
Specify the format of a datafile prior to writing to a table.
DBBOOL bcp_getl(LOGINREC *login)
See if BCP_SETL() was used to set the LOGINREC for BCP work.
RETCODE bcp_control(DBPROCESS *dbproc, int field, DBINT value)
Set BCP options for uploading a datafile.
RETCODE bcp_columns(DBPROCESS *dbproc, int host_colcount)
Indicate how many columns are to be found in the datafile.
RETCODE bcp_colptr(DBPROCESS *dbproc, BYTE *colptr, int table_column)
Override bcp_bind() by pointing to a different host variable.
RETCODE bcp_bind(DBPROCESS *dbproc, BYTE *varaddr, int prefixlen, DBINT varlen, BYTE *terminator, int termlen, int db_vartype, int table_column)
Bind a program host variable to a database column.
RETCODE bcp_exec(DBPROCESS *dbproc, DBINT *rows_copied)
Write a datafile to a table.
DBINT bcp_done(DBPROCESS *dbproc)
Conclude the transfer of data from program variables.
RETCODE bcp_readfmt(DBPROCESS *dbproc, const char filename[])
Read a format definition file.
DBINT bcp_batch(DBPROCESS *dbproc)
Commit a set of rows to the table.
RETCODE bcp_init(DBPROCESS *dbproc, const char *tblname, const char *hfile, const char *errfile, int direction)
Prepare for bulk copy operation on a table.
RETCODE bcp_collen(DBPROCESS *dbproc, DBINT varlen, int table_column)
Set the length of a host variable to be written to a table.
RETCODE bcp_sendrow(DBPROCESS *dbproc)
Write data in host variables to the table.
RETCODE bcp_colfmt_ps(DBPROCESS *dbproc, int host_colnum, int host_type, int host_prefixlen, DBINT host_collen, BYTE *host_term, int host_termlen, int table_colnum, DBTYPEINFO *typeinfo)
Specify the format of a host file for bulk copy purposes, with precision and scale support for numeri...
RETCODE bcp_options(DBPROCESS *dbproc, int option, BYTE *value, int valuelen)
Set "hints" for uploading a file.
DBINT dbvarylen(DBPROCESS *dbproc, int column)
Determine whether a column can vary in size.
const char * dbprtype(int token)
Print a token value's name to a buffer.
static const char * tds_dstr_cstr(DSTR *s)
Returns a C version (NUL terminated string) of dstr.
DSTR * tds_dstr_copy(DSTR *s, const char *src) TDS_WUR
copy a string from another
unsigned int
A callback function used to compare two keys in a database.
if(yy_accept[yy_current_state])
static void text(MDB_val *v)
const struct ncbi::grid::netcache::search::fields::SIZE size
int strcmp(const char *str1, const char *str2)
static const char TERMINATOR
double f(double x_, const double &y_)
static pcre_uint8 * buffer
#define tds_alloc_bcpinfo
#define tds_bcp_start_copy_in
#define tds_get_size_by_type
#define tds_numeric_bytes_per_prec
#define tds_submit_queryf
#define tds_get_conversion_type
#define tds_process_tokens
#define tds_bcp_send_record
static const char * str(char *buf, int n)
BCP_HOSTCOLINFO ** host_columns
TDS_INT identity_insert_on
Information about blobs (e.g.
Metadata about columns in regular and compute rows.
TDS_INT bcp_prefix_len
The length, in bytes, of any length prefix this column may have.
TDS_CHAR * bcp_terminator
TDS_INT column_size
maximun size of data.
BCPCOLDATA * bcp_column_data
TDS_SMALLINT column_bindtype
unsigned char * column_data
TDS_SERVER_TYPE column_type
This type can be different from wire type because conversion (e.g.
unsigned int column_nullable
TDSICONV * char_conv
refers to previously allocated iconv information
TDS_CHAR * column_varaddr
struct tds_column::@124 on_server
TDS_INT column_cur_size
size written in variable (ie: char, text, binary).
const TDSCONTEXT * tds_ctx
TDS_USMALLINT tds_version
BCP_HOSTFILEINFO * hostfileinfo
int msdblib
boolean use ms behaviour
unsigned int bulk_copy
if bulk copy should be enabled
Hold information for any results.
Information for a server connection.
Main include file for libtds.
#define is_numeric_type(x)
tds_sysdep_int32_type TDS_INT
#define is_variable_type(x)
#define TDS_NO_MORE_RESULTS
#define is_datetime_type(x)
return true if type is a datetime (but not date or time)
static bool is_tds_type_valid(int type)
tds_sysdep_int64_type TDS_INT8
#define TDS_COMPUTE_RESULT
unsigned char TDS_TINYINT
tds_sysdep_int16_type TDS_SMALLINT
#define tds_new0(type, n)
tds_sysdep_uint32_type TDS_UINT