📄 bcp.c
字号:
/* * =========================================================================== * PRODUCTION $Log: bcp.c,v $ * PRODUCTION Revision 1000.1 2003/11/17 22:20:13 gouriano * PRODUCTION PRODUCTION: UPGRADED [ORIGINAL] Dev-tree R1.2 * PRODUCTION * =========================================================================== *//* FreeTDS - Library of routines accessing Sybase and Microsoft databases * Copyright (C) 1998-1999 Brian Bruns * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */#include <tds_config.h>#include "tdsutil.h"#include "tds.h"#include "sybfront.h"#include "sybdb.h"#include "dblib.h"#include <unistd.h>#ifdef TARGET_API_MAC_OS8#include <stdlib.h>#include "tdsconvert.h"#endif/* was hard coded as 32768, but that made the local stack data size > 32K, which is not allowed on Mac OS 8/9. (mlilback, 11/7/01) */#ifdef TARGET_API_MAC_OS8#define ROWBUF_SIZE 31000#else#define ROWBUF_SIZE 32768#endifextern int (*g_dblib_msg_handler)();extern int (*g_dblib_err_handler)();extern const int g__numeric_bytes_per_prec[];static char software_version[] = "$Id: bcp.c,v 1000.1 2003/11/17 22:20:13 gouriano Exp $";static void *no_unused_var_warn[] = { software_version, no_unused_var_warn};static RETCODE _bcp_start_copy_in(DBPROCESS *);static RETCODE _bcp_build_bulk_insert_stmt(char *, BCP_COLINFO *, int);static RETCODE _bcp_start_new_batch(DBPROCESS *);static RETCODE _bcp_send_colmetadata(DBPROCESS *);static int _bcp_rtrim_varchar(char *, int);static int _bcp_err_handler(DBPROCESS * dbproc, int bcp_errno);#ifdef NCBI_FTDS/*** The following little table is indexed by precision-1 and will** tell us the number of bytes required to store the specified** precision (with the sign).** Support precision up to 77 digits*/static const int tds_numeric_bytes_per_prec[] = { -1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 21, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33};#endifRETCODE bcp_init(DBPROCESS *dbproc, char *tblname, char *hfile, char *errfile, int direction){ TDSSOCKET *tds = dbproc->tds_socket; BCP_COLINFO *bcpcol; TDSRESULTINFO *resinfo; int i; int rc; char query[256]; /* free allocated storage in dbproc & initialise flags, etc. */ _bcp_clear_storage(dbproc); /* check validity of parameters */ if (hfile != (char *) NULL) { dbproc->bcp_hostfile = (char *) malloc(strlen(hfile) + 1); strcpy(dbproc->bcp_hostfile, hfile); if (errfile != (char *) NULL) { dbproc->bcp_errorfile = (char *) malloc(strlen(errfile) + 1); strcpy(dbproc->bcp_errorfile, errfile); } else { dbproc->bcp_errorfile = (char *) NULL; } } else { dbproc->bcp_hostfile = (char *) NULL; dbproc->bcp_errorfile = (char *) NULL; dbproc->sendrow_init = 0; } if (tblname == (char *) NULL) { _bcp_err_handler(dbproc, BCPEBCITBNM); return (FAIL); } if (strlen(tblname) > 92) { /* 30.30.30 */ _bcp_err_handler(dbproc, BCPEBCITBLEN); return (FAIL); } dbproc->bcp_tablename = (char *) malloc(strlen(tblname) + 1); strcpy(dbproc->bcp_tablename, tblname); if (direction == DB_IN || direction == DB_OUT) dbproc->bcp_direction = direction; else { _bcp_err_handler(dbproc, BCPEBDIO); return (FAIL); } if (dbproc->bcp_direction == DB_IN) { sprintf(query,"select * from %s where 0 = 1", dbproc->bcp_tablename); if(tds_submit_query(tds,query) == TDS_FAIL) { return FAIL; } while((rc= tds_process_result_tokens(tds)) == TDS_SUCCEED); if (rc != TDS_NO_MORE_RESULTS) { return FAIL; } if (!tds->res_info) { return FAIL; } resinfo = tds->res_info; dbproc->bcp_colcount = resinfo->num_cols; dbproc->bcp_columns = (BCP_COLINFO **) malloc(resinfo->num_cols * sizeof(BCP_COLINFO *)); for (i = 0; i < dbproc->bcp_colcount; i++) { dbproc->bcp_columns[i] = (BCP_COLINFO *) malloc(sizeof(BCP_COLINFO)); bcpcol = dbproc->bcp_columns[i]; memset(bcpcol, '\0', sizeof(BCP_COLINFO)); bcpcol->tab_colnum = i + 1; /* turn offset into ordinal */ bcpcol->db_type = resinfo->columns[i]->column_type; bcpcol->db_length = resinfo->columns[i]->column_size; bcpcol->db_nullable = resinfo->columns[i]->column_nullable; if (is_numeric_type(bcpcol->db_type)) { bcpcol->data = (BYTE *) malloc(sizeof(TDS_NUMERIC)); ((TDS_NUMERIC *) bcpcol->data)->precision = resinfo->columns[i]->column_prec; ((TDS_NUMERIC *) bcpcol->data)->scale = resinfo->columns[i]->column_scale; } else if(is_blob_type(bcpcol->db_type)) { bcpcol->data= (BYTE*)malloc(16); memset(bcpcol->data, 0, 16); } else { bcpcol->data = (BYTE *) malloc(bcpcol->db_length); if (bcpcol->data == (BYTE *) NULL) { printf("could not allocate %d bytes of memory\n", bcpcol->db_length); } } bcpcol->data_size = 0; if (IS_TDS7_PLUS(tds)) { bcpcol->db_usertype = resinfo->columns[i]->column_usertype; bcpcol->db_flags = resinfo->columns[i]->column_flags; bcpcol->db_type_save = resinfo->columns[i]->column_type_save; bcpcol->db_prec = resinfo->columns[i]->column_prec; bcpcol->db_scale = resinfo->columns[i]->column_scale; memcpy(bcpcol->db_collate, resinfo->columns[i]->collation, 5); strcpy(bcpcol->db_name, resinfo->columns[i]->column_name); bcpcol->db_varint_size = resinfo->columns[i]->column_varint_size; bcpcol->db_unicodedata = resinfo->columns[i]->column_unicodedata; } } if(dbproc->bcp_hostfile == NULL) { dbproc->host_colcount = dbproc->bcp_colcount; dbproc->host_columns = (BCP_HOSTCOLINFO **) malloc(dbproc->host_colcount * sizeof(BCP_HOSTCOLINFO *)); for (i = 0; i < dbproc->host_colcount; i++) { dbproc->host_columns[i] = (BCP_HOSTCOLINFO *) malloc(sizeof(BCP_HOSTCOLINFO)); memset(dbproc->host_columns[i], '\0', sizeof(BCP_HOSTCOLINFO)); } } } return SUCCEED;}RETCODE bcp_collen(DBPROCESS *dbproc, DBINT varlen, int table_column){BCP_HOSTCOLINFO *hostcol; if (dbproc->bcp_direction == 0) { _bcp_err_handler(dbproc, BCPEBCPI); return FAIL; } if (dbproc->bcp_direction != DB_IN) { _bcp_err_handler(dbproc, BCPEBCPN); return FAIL; } if (table_column > dbproc->host_colcount) return FAIL; hostcol = dbproc->host_columns[table_column - 1]; hostcol->column_len = varlen; return SUCCEED;}RETCODE bcp_columns(DBPROCESS *dbproc, int host_colcount){int i; if (dbproc->bcp_direction == 0) { _bcp_err_handler(dbproc, BCPEBCPI); return FAIL; } if (dbproc->bcp_hostfile == (char *) NULL) { _bcp_err_handler(dbproc, BCPEBIVI); return FAIL; } if (host_colcount < 1) { _bcp_err_handler(dbproc, BCPEBCFO); return FAIL; } dbproc->host_colcount = host_colcount; dbproc->host_columns = (BCP_HOSTCOLINFO **) malloc(host_colcount * sizeof(BCP_HOSTCOLINFO *)); for (i = 0; i < host_colcount; i++) { dbproc->host_columns[i] = (BCP_HOSTCOLINFO *) malloc(sizeof(BCP_HOSTCOLINFO)); memset(dbproc->host_columns[i], '\0', sizeof(BCP_HOSTCOLINFO)); } return SUCCEED;}RETCODE bcp_colfmt(DBPROCESS *dbproc, int host_colnum, int host_type, int host_prefixlen, DBINT host_collen, BYTE *host_term, int host_termlen, int table_colnum){BCP_HOSTCOLINFO *hostcol;#ifdef MSDBLIB /* Microsoft specifies a "file_termlen" of zero if there's no terminator */ if (host_termlen == 0) host_termlen = -1;#endif if (dbproc->bcp_direction == 0) { _bcp_err_handler(dbproc, BCPEBCPI); return FAIL; } if (dbproc->bcp_hostfile == (char *) NULL) { _bcp_err_handler(dbproc, BCPEBIVI); return FAIL; } if (dbproc->host_colcount == 0) { _bcp_err_handler(dbproc, BCPEBCBC); return FAIL; } if (host_colnum < 1) return FAIL; if (host_prefixlen != 0 && host_prefixlen != 1 && host_prefixlen != 2 && host_prefixlen != 4 && host_prefixlen != -1 ) { _bcp_err_handler(dbproc, BCPEBCPREF); return FAIL; } if (table_colnum == 0 && host_type == 0) { _bcp_err_handler(dbproc, BCPEBCPCTYP); return FAIL; } if (host_prefixlen == 0 && host_collen == -1 && host_termlen == -1 && !is_fixed_type(host_type)) { _bcp_err_handler(dbproc, BCPEVDPT); return FAIL; } if (host_collen < -1) { _bcp_err_handler(dbproc, BCPEBCHLEN); return FAIL; } if (is_fixed_type(host_type) && (host_collen != -1 && host_collen != 0)) return FAIL; /* * If there's a positive terminator length, we need a valid terminator pointer. * If the terminator length is 0 or -1, then there's no terminator. * FIXME: look up the correct error code for a bad terminator pointer or length and return that before arriving here. */ /*assert ((host_termlen > 0)? (host_term != NULL) : 1);*/ hostcol = dbproc->host_columns[host_colnum - 1]; hostcol->host_column = host_colnum; hostcol->datatype = host_type; hostcol->prefix_len = host_prefixlen; hostcol->column_len = host_collen; if (host_term && host_termlen >= 0) { hostcol->terminator = (BYTE *) malloc(host_termlen + 1); memcpy(hostcol->terminator, host_term, host_termlen); } hostcol->term_len = host_termlen; hostcol->tab_colnum = table_colnum; return SUCCEED;}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){ if (dbproc->bcp_direction == 0) { _bcp_err_handler(dbproc, BCPEBCPI); return FAIL; } return SUCCEED;}RETCODE bcp_control(DBPROCESS *dbproc, int field, DBINT value){ if (dbproc->bcp_direction == 0) { _bcp_err_handler(dbproc, BCPEBCPI); return FAIL; } switch (field) { case BCPMAXERRS: dbproc->maxerrs = value; break; case BCPFIRST: dbproc->firstrow = value; break; case BCPLAST: dbproc->firstrow = value; break; case BCPBATCH: dbproc->bcpbatch = value; break; default: _bcp_err_handler(dbproc, BCPEIFNB); return FAIL; } return SUCCEED;}RETCODE bcp_options(DBPROCESS *dbproc, int option, BYTE *value, int valuelen){ int i; static const char *hints[] = { "ORDER" , "ROWS_PER_BATCH" , "KILOBYTES_PER_BATCH" , "TABLOCK" , "CHECK_CONSTRAINTS" , NULL }; if (!dbproc) return FAIL; switch (option) { case BCPLABELED: tdsdump_log (TDS_DBG_FUNC, "%L UNIMPLEMENTED bcp option: BCPLABELED\n"); return FAIL; case BCPHINTS: if (!value || valuelen <= 0) return FAIL; if (dbproc->bcp_hint != NULL) /* hint already set */ return FAIL; for(i=0; hints[i]; i++ ) { /* do we know about this hint? */ if (strncasecmp((char*) value, hints[i], strlen(hints[i])) == 0) break; } if (!hints[i]) { /* no such hint */ return FAIL; } /* * Store the bare hint, as passed from the application. * The process that constructs the "insert bulk" statement will incorporate the hint text. */ dbproc->bcp_hint = (char*) malloc(1+valuelen); memcpy(dbproc->bcp_hint, value, valuelen); dbproc->bcp_hint[valuelen] = '\0'; /* null terminate it */ break; default: tdsdump_log (TDS_DBG_FUNC, "%L UNIMPLEMENTED bcp option: %u\n", option); return FAIL; } return SUCCEED;}RETCODEbcp_colptr(DBPROCESS * dbproc, BYTE * colptr, int table_column){BCP_HOSTCOLINFO *hostcol; if (dbproc->bcp_direction == 0) { _bcp_err_handler(dbproc, BCPEBCPI); return FAIL; } if (dbproc->bcp_direction != DB_IN) { _bcp_err_handler(dbproc, BCPEBCPN); return FAIL; } if (table_column > dbproc->host_colcount) return FAIL; hostcol = dbproc->host_columns[table_column - 1]; hostcol->host_var = colptr; return SUCCEED;}DBBOOL bcp_getl(LOGINREC *login){TDSLOGIN *tdsl = (TDSLOGIN *) login->tds_login; return (tdsl->bulk_copy);}static RETCODE _bcp_exec_out(DBPROCESS *dbproc, DBINT *rows_copied){FILE *hostfile; int ret;int i; int j; int k; char *xx; TDSSOCKET *tds = dbproc->tds_socket;TDSRESULTINFO *resinfo;BCP_COLINFO *bcpcol;TDSCOLINFO *curcol;BCP_HOSTCOLINFO *hostcol;BYTE *src;int srctype;long len;int buflen;int destlen;BYTE *outbuf; char query[256];TDS_INT rowtype;TDS_INT computeid;TDS_INT result_type;TDS_TINYINT ti;TDS_SMALLINT si;TDS_INT li;TDSDATEREC when;int row_of_query;int rows_written; if (!(hostfile = fopen(dbproc->bcp_hostfile, "w"))) { _bcp_err_handler(dbproc, BCPEBCUO);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -