LCOV - code coverage report
Current view: top level - src/tds - bulk.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 570 632 90.2 %
Date: 2026-03-02 06:50:21 Functions: 24 24 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2008-2010  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /**
      21             :  * \file
      22             :  * \brief Handle bulk copy
      23             :  */
      24             : 
      25             : #include <config.h>
      26             : 
      27             : #if HAVE_STRING_H
      28             : #include <string.h>
      29             : #endif /* HAVE_STRING_H */
      30             : 
      31             : #if HAVE_ERRNO_H
      32             : #include <errno.h>
      33             : #endif /* HAVE_ERRNO_H */
      34             : 
      35             : #if HAVE_STDLIB_H
      36             : #include <stdlib.h>
      37             : #endif /* HAVE_STDLIB_H */
      38             : 
      39             : #include <assert.h>
      40             : 
      41             : #include <freetds/tds.h>
      42             : #include <freetds/checks.h>
      43             : #include <freetds/bytes.h>
      44             : #include <freetds/iconv.h>
      45             : #include <freetds/stream.h>
      46             : #include <freetds/convert.h>
      47             : #include <freetds/utils/string.h>
      48             : #include <freetds/utils/ascii.h>
      49             : #include <freetds/replacements.h>
      50             : #include <freetds/enum_cap.h>
      51             : 
      52             : /**
      53             :  * Holds clause buffer
      54             :  */
      55             : typedef struct tds_pbcb
      56             : {
      57             :         /** buffer */
      58             :         char *pb;
      59             :         /** buffer length */
      60             :         unsigned int cb;
      61             :         /** true is buffer came from malloc */
      62             :         bool from_malloc;
      63             : } TDSPBCB;
      64             : 
      65             : static TDSRET tds7_bcp_send_colmetadata(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
      66             : static TDSRET tds_bcp_start_insert_stmt(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
      67             : static int tds5_bcp_add_fixed_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
      68             :                                       int offset, unsigned char * rowbuffer, int start);
      69             : static int tds5_bcp_add_variable_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
      70             :                                          int offset, TDS_UCHAR *rowbuffer, int start, int *pncols);
      71             : static void tds_bcp_row_free(TDSRESULTINFO* result, unsigned char *row);
      72             : static TDSRET tds5_process_insert_bulk_reply(TDSSOCKET * tds, TDSBCPINFO *bcpinfo);
      73             : static TDSRET probe_sap_locking(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
      74             : static TDSRET tds5_get_col_data_or_dflt(tds_bcp_get_col_data get_col_data,
      75             :                                         TDSBCPINFO * bulk, TDSCOLUMN * bcpcol, int offset, int colnum);
      76             : 
      77             : /**
      78             :  * Initialize BCP information.
      79             :  * Query structure of the table to server.
      80             :  * \tds
      81             :  * \param bcpinfo BCP information to initialize. Structure should be allocate
      82             :  *        and table name and direction should be already set.
      83             :  */
      84             : TDSRET
      85         522 : tds_bcp_init(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
      86             : {
      87             :         TDSRESULTINFO *resinfo;
      88         522 :         TDSRESULTINFO *bindinfo = NULL;
      89             :         TDSCOLUMN *curcol;
      90             :         TDS_INT result_type;
      91             :         int i;
      92             :         TDSRET rc;
      93             :         const char *fmt;
      94             : 
      95             :         /* FIXME don't leave state in processing state */
      96             : 
      97             :         /* Check table locking type. Do this first, because the code in bcp_init() which
      98             :          * calls us seems to depend on the information from the columns query still being
      99             :          * active in the context.
     100             :          */
     101         522 :         probe_sap_locking(tds, bcpinfo);
     102             : 
     103             :         /* TODO quote tablename if needed */
     104         522 :         if (bcpinfo->direction != TDS_BCP_QUERYOUT)
     105             :                 fmt = "SET FMTONLY ON select * from %s SET FMTONLY OFF";
     106             :         else
     107          10 :                 fmt = "SET FMTONLY ON %s SET FMTONLY OFF";
     108             : 
     109        1044 :         if (TDS_FAILED(rc=tds_submit_queryf(tds, fmt, tds_dstr_cstr(&bcpinfo->tablename))))
     110             :                 /* TODO return an error ?? */
     111             :                 /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */
     112             :                 return rc;
     113             : 
     114             :         /* TODO possibly stop at ROWFMT and copy before going to idle */
     115             :         /* TODO check what happen if table is not present, cleanup on error */
     116        2610 :         while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS))
     117             :                    == TDS_SUCCESS)
     118        2088 :                 continue;
     119         522 :         TDS_PROPAGATE(rc);
     120             : 
     121             :         /* copy the results info from the TDS socket */
     122         522 :         if (!tds->res_info)
     123             :                 return TDS_FAIL;
     124             : 
     125         522 :         resinfo = tds->res_info;
     126         522 :         if ((bindinfo = tds_alloc_results(resinfo->num_cols)) == NULL) {
     127             :                 rc = TDS_FAIL;
     128             :                 goto cleanup;
     129             :         }
     130             : 
     131         522 :         bindinfo->row_size = resinfo->row_size;
     132             : 
     133             :         /* Copy the column metadata */
     134         522 :         rc = TDS_FAIL;
     135        3872 :         for (i = 0; i < bindinfo->num_cols; i++) {
     136             : 
     137        3350 :                 curcol = bindinfo->columns[i];
     138             :                 
     139             :                 /*
     140             :                  * TODO use memcpy ??
     141             :                  * curcol and resinfo->columns[i] are both TDSCOLUMN.  
     142             :                  * Why not "curcol = resinfo->columns[i];"?  Because the rest of TDSCOLUMN (below column_timestamp)
     143             :                  * isn't being used.  Perhaps this "upper" part of TDSCOLUMN should be a substructure.
     144             :                  * Or, see if the "lower" part is unused (and zeroed out) at this point, and just do one assignment.
     145             :                  */
     146        3350 :                 curcol->funcs = resinfo->columns[i]->funcs;
     147        3350 :                 curcol->column_type = resinfo->columns[i]->column_type;
     148        3350 :                 curcol->column_usertype = resinfo->columns[i]->column_usertype;
     149        3350 :                 curcol->column_flags = resinfo->columns[i]->column_flags;
     150        3350 :                 if (curcol->column_varint_size == 0)
     151        3350 :                         curcol->column_cur_size = resinfo->columns[i]->column_cur_size;
     152             :                 else
     153           0 :                         curcol->column_cur_size = -1;
     154        3350 :                 curcol->column_size = resinfo->columns[i]->column_size;
     155        3350 :                 curcol->column_varint_size = resinfo->columns[i]->column_varint_size;
     156        3350 :                 curcol->column_prec = resinfo->columns[i]->column_prec;
     157        3350 :                 curcol->column_scale = resinfo->columns[i]->column_scale;
     158        3350 :                 curcol->on_server = resinfo->columns[i]->on_server;
     159        3350 :                 curcol->char_conv = resinfo->columns[i]->char_conv;
     160        3350 :                 if (!tds_dstr_dup(&curcol->column_name, &resinfo->columns[i]->column_name))
     161             :                         goto cleanup;
     162        3350 :                 if (!tds_dstr_dup(&curcol->table_column_name, &resinfo->columns[i]->table_column_name))
     163             :                         goto cleanup;
     164        3350 :                 curcol->column_nullable = resinfo->columns[i]->column_nullable;
     165        3350 :                 curcol->column_identity = resinfo->columns[i]->column_identity;
     166        3350 :                 curcol->column_timestamp = resinfo->columns[i]->column_timestamp;
     167        3350 :                 curcol->column_computed = resinfo->columns[i]->column_computed;
     168             :                 
     169        3350 :                 memcpy(curcol->column_collation, resinfo->columns[i]->column_collation, 5);
     170        3350 :                 curcol->use_iconv_out = 0;
     171             : 
     172             :                 /* From MS documentation:
     173             :                  * Note that for INSERT BULK operations, XMLTYPE is to be sent as NVARCHAR(N) or NVARCHAR(MAX)
     174             :                  * data type. An error is produced if XMLTYPE is specified.
     175             :                  */
     176        3350 :                 if (curcol->on_server.column_type == SYBMSXML) {
     177           8 :                         curcol->on_server.column_type = XSYBNVARCHAR;
     178           8 :                         curcol->column_type = SYBVARCHAR;
     179           8 :                         memcpy(curcol->column_collation, tds->conn->collation, 5);
     180             :                 }
     181             : 
     182        3350 :                 if (is_numeric_type(curcol->column_type)) {
     183         232 :                         curcol->bcp_column_data = tds_alloc_bcp_column_data(sizeof(TDS_NUMERIC));
     184         232 :                         ((TDS_NUMERIC *) curcol->bcp_column_data->data)->precision = curcol->column_prec;
     185         232 :                         ((TDS_NUMERIC *) curcol->bcp_column_data->data)->scale = curcol->column_scale;
     186             :                 } else {
     187        3118 :                         curcol->bcp_column_data = 
     188        3118 :                                 tds_alloc_bcp_column_data(TDS_MAX(curcol->column_size,curcol->on_server.column_size));
     189             :                 }
     190        3350 :                 if (!curcol->bcp_column_data)
     191             :                         goto cleanup;
     192             :         }
     193             : 
     194         522 :         if (!IS_TDS7_PLUS(tds->conn)) {
     195          90 :                 bindinfo->current_row = tds_new(unsigned char, bindinfo->row_size);
     196          90 :                 if (!bindinfo->current_row)
     197             :                         goto cleanup;
     198          90 :                 bindinfo->row_free = tds_bcp_row_free;
     199             :         }
     200             : 
     201         522 :         if (bcpinfo->identity_insert_on) {
     202             : 
     203           0 :                 rc = tds_submit_queryf(tds, "set identity_insert %s on", tds_dstr_cstr(&bcpinfo->tablename));
     204           0 :                 if (TDS_FAILED(rc))
     205             :                         goto cleanup;
     206             : 
     207             :                 /* TODO use tds_process_simple_query */
     208           0 :                 while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS))
     209             :                            == TDS_SUCCESS) {
     210             :                 }
     211           0 :                 if (rc != TDS_NO_MORE_RESULTS)
     212             :                         goto cleanup;
     213             :         }
     214             : 
     215         522 :         bcpinfo->bindinfo = bindinfo;
     216         522 :         bcpinfo->bind_count = 0;
     217             : 
     218         522 :         return TDS_SUCCESS;
     219             : 
     220           0 : cleanup:
     221           0 :         tds_free_results(bindinfo);
     222           0 :         return rc;
     223             : }
     224             : 
     225             : /**
     226             :  * Detect if table we're writing to uses 'datarows' lockmode.
     227             :  * \tds
     228             :  * \param bcpinfo BCP information already prepared
     229             :  * \return TDS_SUCCESS or TDS_FAIL.
     230             :  */
     231             : static TDSRET
     232         522 : probe_sap_locking(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
     233             : {
     234             :         TDSRET rc;
     235             :         unsigned int value;
     236             :         bool value_found;
     237             :         TDS_INT resulttype;
     238             :         const char *full_tablename, *rdot, *tablename;
     239             : 
     240             :         /* Only needed for inward data */
     241         522 :         if (bcpinfo->direction != TDS_BCP_IN)
     242             :                 return TDS_SUCCESS;
     243             : 
     244             :         /* Only needed for SAP ASE versions which support datarows-locking. */
     245         398 :         if (!TDS_IS_SYBASE(tds) || !tds_capability_has_req(tds->conn, TDS_REQ_DOL_BULK))
     246             :                 return TDS_SUCCESS;
     247             : 
     248             :         /* A request to probe database.owner.tablename needs to check database.owner.sysobjects for tablename
     249             :          * (it doesn't work to check sysobjects for database.owner.tablename) */
     250         120 :         full_tablename = tds_dstr_cstr(&bcpinfo->tablename);
     251          60 :         rdot = strrchr(full_tablename, '.');
     252             : 
     253          60 :         if (rdot != NULL)
     254           2 :                 tablename = rdot + 1;
     255             :         else
     256             :                 tablename = full_tablename;
     257             : 
     258          60 :         TDS_PROPAGATE(tds_submit_queryf(tds, "select sysstat2 from %.*ssysobjects where type='U' and name='%s'",
     259             :                                         (int) (rdot ? (rdot - full_tablename + 1) : 0), full_tablename, tablename));
     260             : 
     261             :         value = 0;
     262             :         value_found = false;
     263             : 
     264         180 :         while ((rc = tds_process_tokens(tds, &resulttype, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
     265         120 :                 const unsigned int stop_mask = TDS_RETURN_DONE | TDS_RETURN_ROW;
     266             : 
     267         120 :                 if (resulttype != TDS_ROW_RESULT)
     268          86 :                         continue;
     269             : 
     270             :                 /* We must keep processing result tokens (even if we've found what we're looking for) so that the
     271             :                  * stream is ready for subsequent queries. */
     272          68 :                 while ((rc = tds_process_tokens(tds, &resulttype, NULL, stop_mask)) == TDS_SUCCESS) {
     273             :                         TDSCOLUMN *col;
     274             :                         TDS_SERVER_TYPE ctype;
     275             :                         CONV_RESULT dres;
     276             :                         TDS_INT res;
     277             : 
     278          68 :                         if (resulttype != TDS_ROW_RESULT)
     279             :                                 break;
     280             : 
     281             :                         /* Get INT4 from column 0 */
     282          34 :                         if (!tds->current_results || tds->current_results->num_cols < 1)
     283           0 :                                 continue;
     284             : 
     285          34 :                         col = tds->current_results->columns[0];
     286          34 :                         if (col->column_cur_size < 0)
     287           0 :                                 continue;
     288             : 
     289          34 :                         ctype = tds_get_conversion_type(col->column_type, col->column_size);
     290          34 :                         res = tds_convert(tds_get_ctx(tds), ctype, col->column_data, col->column_cur_size, SYBINT4, &dres);
     291          34 :                         if (res < 0)
     292           0 :                                 continue;
     293             : 
     294          34 :                         value = dres.i;
     295          34 :                         value_found = true;
     296             :                 }
     297             :         }
     298          60 :         TDS_PROPAGATE(rc);
     299             : 
     300             :         /* No valid result - Treat this as meaning the feature is lacking; it could be an old Sybase version for example */
     301          60 :         if (!value_found) {
     302          26 :                 tdsdump_log(TDS_DBG_INFO1, "[DOL BULK] No valid result returned by probe.\n");
     303             :                 return TDS_SUCCESS;
     304             :         }
     305             : 
     306             :         /* Log and analyze result.
     307             :          * Sysstat2 flag values:
     308             :          *    https://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.help.ase.16.0/doc/html/title.html
     309             :          * 0x08000 - datarows locked
     310             :          * 0x20000 - clustered index present. (Not recommended for performance reasons to datarows-lock with clustered index) */
     311          34 :         tdsdump_log(TDS_DBG_INFO1, "%x = sysstat2 for '%s'", value, full_tablename);
     312             : 
     313          34 :         if (0x8000 & value) {
     314           2 :                 bcpinfo->datarows_locking = true;
     315           2 :                 tdsdump_log(TDS_DBG_INFO1, "Table has datarows-locking; enabling DOL BULK format.\n");
     316             : 
     317           2 :                 if (0x20000 & value)
     318           0 :                         tdsdump_log(TDS_DBG_WARN, "Table also has clustered index: bulk insert performance may be degraded.\n");
     319             :         }
     320             :         return TDS_SUCCESS;
     321             : }
     322             : 
     323             : /**
     324             :  * Help to build query to be sent to server.
     325             :  * Append column declaration to the query.
     326             :  * Only for TDS 7.0+.
     327             :  * \tds
     328             :  * \param[out] clause output string
     329             :  * \param bcpcol column to append
     330             :  * \param first  true if column is the first
     331             :  * \return TDS_SUCCESS or TDS_FAIL.
     332             :  */
     333             : static TDSRET
     334        1764 : tds7_build_bulk_insert_stmt(TDSSOCKET * tds, TDSPBCB * clause, TDSCOLUMN * bcpcol, int first)
     335             : {
     336             :         char column_type[128];
     337             : 
     338        1764 :         tdsdump_log(TDS_DBG_FUNC, "tds7_build_bulk_insert_stmt(%p, %p, %p, %d)\n", tds, clause, bcpcol, first);
     339             : 
     340        1764 :         if (TDS_FAILED(tds_get_column_declaration(tds, bcpcol, column_type))) {
     341           0 :                 tdserror(tds_get_ctx(tds), tds, TDSEBPROBADTYP, errno);
     342           0 :                 tdsdump_log(TDS_DBG_FUNC, "error: cannot build bulk insert statement. unrecognized server datatype %d\n",
     343             :                             bcpcol->on_server.column_type);
     344             :                 return TDS_FAIL;
     345             :         }
     346             : 
     347        1764 :         if (IS_TDS71_PLUS(tds->conn) && bcpcol->char_conv) {
     348         630 :                 strcat(column_type, " COLLATE ");
     349         630 :                 strcat(column_type, tds_canonical_collate_name(bcpcol->char_conv->to.charset.canonic));
     350             :         }
     351             : 
     352        3528 :         if (clause->cb < strlen(clause->pb)
     353        5292 :             + tds_quote_id(tds, NULL, tds_dstr_cstr(&bcpcol->column_name), tds_dstr_len(&bcpcol->column_name))
     354        1764 :             + strlen(column_type)
     355        1764 :             + ((first) ? 2u : 4u)) {
     356           0 :                 char *temp = tds_new(char, 2 * clause->cb);
     357             : 
     358           0 :                 if (!temp) {
     359           0 :                         tdserror(tds_get_ctx(tds), tds, TDSEMEM, errno);
     360           0 :                         return TDS_FAIL;
     361             :                 }
     362           0 :                 strcpy(temp, clause->pb);
     363           0 :                 if (clause->from_malloc)
     364           0 :                         free(clause->pb);
     365           0 :                 clause->from_malloc = true;
     366           0 :                 clause->pb = temp;
     367           0 :                 clause->cb *= 2;
     368             :         }
     369             : 
     370        1764 :         if (!first)
     371        1486 :                 strcat(clause->pb, ", ");
     372             : 
     373        5292 :         tds_quote_id(tds, strchr(clause->pb, 0), tds_dstr_cstr(&bcpcol->column_name), tds_dstr_len(&bcpcol->column_name));
     374        1764 :         strcat(clause->pb, " ");
     375        1764 :         strcat(clause->pb, column_type);
     376             : 
     377        1764 :         return TDS_SUCCESS;
     378             : }
     379             : 
     380             : static const char *
     381           2 : uc_str(const char *s)
     382             : {
     383           2 :         size_t n = strcspn(s, "abcdefghijklmnopqrstuvwxyz");
     384             : 
     385           2 :         if (s[n] == '\0')
     386             :                 return s;       /* already all uppercase */
     387             : 
     388           0 :         return tds_ascii_strupr(strdup(s));
     389             : }
     390             : 
     391             : /**
     392             :  * Prepare the query to be sent to server to request BCP information
     393             :  * \tds
     394             :  * \param bcpinfo BCP information
     395             :  */
     396             : static TDSRET
     397         338 : tds_bcp_start_insert_stmt(TDSSOCKET * tds, TDSBCPINFO * bcpinfo)
     398             : {
     399             :         char *query;
     400             : 
     401         338 :         if (IS_TDS7_PLUS(tds->conn)) {
     402             :                 int i, firstcol, erc;
     403             :                 char *hint;
     404             :                 TDSCOLUMN *bcpcol;
     405             :                 TDSPBCB colclause;
     406         278 :                 char clause_buffer[4096] = { 0 };
     407         556 :                 bool triggers_checked = tds_dstr_isempty(&bcpinfo->hint);        /* Done on demand */
     408             : 
     409         278 :                 colclause.pb = clause_buffer;
     410         278 :                 colclause.cb = sizeof(clause_buffer);
     411         278 :                 colclause.from_malloc = false;
     412             : 
     413             :                 /* TODO avoid asprintf, use always malloc-ed buffer */
     414         278 :                 firstcol = 1;
     415             : 
     416        2042 :                 for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     417        1764 :                         bcpcol = bcpinfo->bindinfo->columns[i];
     418             : 
     419        1764 :                         if (bcpcol->column_timestamp)
     420           0 :                                 continue;
     421        1764 :                         if (!bcpinfo->identity_insert_on && bcpcol->column_identity)
     422           0 :                                 continue;
     423        1764 :                         if (bcpcol->column_computed) {
     424           2 :                                 if (!triggers_checked) {
     425           4 :                                         const char *uc_hint = uc_str(tds_dstr_cstr(&bcpinfo->hint));
     426             : 
     427           2 :                                         if (strstr(uc_hint, "FIRE_TRIGGERS"))
     428           2 :                                                 bcpinfo->with_triggers = true;
     429             : 
     430           4 :                                         if (uc_hint != tds_dstr_cstr(&bcpinfo->hint))
     431           0 :                                                 free((char *) uc_hint);
     432             : 
     433             :                                         triggers_checked = true;
     434             :                                 }
     435           2 :                                 if (!bcpinfo->with_triggers)
     436           0 :                                         continue;
     437             :                         }
     438        1764 :                         tds7_build_bulk_insert_stmt(tds, &colclause, bcpcol, firstcol);
     439        1764 :                         firstcol = 0;
     440             :                 }
     441             : 
     442         556 :                 if (!tds_dstr_isempty(&bcpinfo->hint)) {
     443         172 :                         if (asprintf(&hint, " with (%s)", tds_dstr_cstr(&bcpinfo->hint)) < 0)
     444           0 :                                 hint = NULL;
     445             :                 } else {
     446         192 :                         hint = strdup("");
     447             :                 }
     448         278 :                 if (!hint) {
     449           0 :                         if (colclause.from_malloc)
     450           0 :                                 TDS_ZERO_FREE(colclause.pb);
     451           0 :                         return TDS_FAIL;
     452             :                 }
     453             : 
     454         556 :                 erc = asprintf(&query, "insert bulk %s (%s)%s", tds_dstr_cstr(&bcpinfo->tablename), colclause.pb, hint);
     455             : 
     456         278 :                 free(hint);
     457         278 :                 if (colclause.from_malloc)
     458           0 :                         TDS_ZERO_FREE(colclause.pb);    /* just for good measure; not used beyond this point */
     459             : 
     460         278 :                 if (erc < 0)
     461             :                         return TDS_FAIL;
     462             :         } else {
     463             :                 /* NOTE: if we use "with nodescribe" for following inserts server do not send describe */
     464         120 :                 if (asprintf(&query, "insert bulk %s", tds_dstr_cstr(&bcpinfo->tablename)) < 0)
     465             :                         return TDS_FAIL;
     466             :         }
     467             : 
     468             :         /* save the statement for later... */
     469         338 :         bcpinfo->insert_stmt = query;
     470             : 
     471         338 :         return TDS_SUCCESS;
     472             : }
     473             : 
     474             : static TDSRET
     475         866 : tds7_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo,
     476             :                  tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
     477             : {
     478             :         int i;
     479             : 
     480         866 :         tds_put_byte(tds, TDS_ROW_TOKEN);   /* 0xd1 */
     481       12718 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     482             : 
     483             :                 TDS_INT save_size;
     484             :                 unsigned char *save_data;
     485             :                 TDSBLOB blob;
     486             :                 TDSCOLUMN  *bindcol;
     487             :                 TDSRET rc;
     488             : 
     489       11852 :                 bindcol = bcpinfo->bindinfo->columns[i];
     490             : 
     491             :                 /*
     492             :                  * Don't send the (meta)data for timestamp columns or
     493             :                  * identity columns unless indentity_insert is enabled.
     494             :                  */
     495             : 
     496       11852 :                 if ((!bcpinfo->identity_insert_on && bindcol->column_identity) || bindcol->column_timestamp
     497       11852 :                     || (bindcol->column_computed && !bcpinfo->with_triggers)) {
     498           0 :                         continue;
     499             :                 }
     500             : 
     501       11852 :                 rc = get_col_data(bcpinfo, bindcol, offset);
     502       11852 :                 if (TDS_FAILED(rc)) {
     503           0 :                         tdsdump_log(TDS_DBG_INFO1, "get_col_data (column %d) failed\n", i + 1);
     504           0 :                         return rc;
     505             :                 }
     506       11852 :                 tdsdump_log(TDS_DBG_INFO1, "gotten column %d length %d null %d\n",
     507             :                                 i + 1, bindcol->bcp_column_data->datalen, bindcol->bcp_column_data->is_null);
     508             : 
     509       11852 :                 save_size = bindcol->column_cur_size;
     510       11852 :                 save_data = bindcol->column_data;
     511       11852 :                 assert(bindcol->column_data == NULL);
     512       11852 :                 if (bindcol->bcp_column_data->is_null) {
     513        4544 :                         if (!bindcol->column_nullable && !is_nullable_type(bindcol->on_server.column_type)) {
     514           0 :                                 if (null_error)
     515           0 :                                         null_error(bcpinfo, i, offset);
     516             :                                 return TDS_FAIL;
     517             :                         }
     518        4544 :                         bindcol->column_cur_size = -1;
     519        7308 :                 } else if (is_blob_col(bindcol)) {
     520         108 :                         bindcol->column_cur_size = bindcol->bcp_column_data->datalen;
     521         108 :                         memset(&blob, 0, sizeof(blob));
     522         108 :                         blob.textvalue = (TDS_CHAR *) bindcol->bcp_column_data->data;
     523         108 :                         bindcol->column_data = (unsigned char *) &blob;
     524             :                 } else {
     525        7200 :                         bindcol->column_cur_size = bindcol->bcp_column_data->datalen;
     526        7200 :                         bindcol->column_data = bindcol->bcp_column_data->data;
     527             :                 }
     528       11852 :                 rc = bindcol->funcs->put_data(tds, bindcol, true);
     529       11852 :                 bindcol->column_cur_size = save_size;
     530       11852 :                 bindcol->column_data = save_data;
     531             : 
     532       11852 :                 TDS_PROPAGATE(rc);
     533             :         }
     534             :         return TDS_SUCCESS;
     535             : }
     536             : 
     537             : static TDSRET
     538         214 : tds5_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo,
     539             :                  tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
     540             : {
     541         214 :         int row_pos = 0;
     542             :         int row_sz_pos;
     543         214 :         int blob_cols = 0;
     544             :         int var_cols_pos;
     545         214 :         int var_cols_written = 0;
     546         214 :         TDS_INT  old_record_size = bcpinfo->bindinfo->row_size;
     547         214 :         unsigned char *record = bcpinfo->bindinfo->current_row;
     548             :         int i;
     549             : 
     550         214 :         memset(record, '\0', old_record_size);  /* zero the rowbuffer */
     551             : 
     552             :         /* SAP ASE Datarows-locked tables expect additional 4 blank bytes before everything else */
     553         214 :         if (bcpinfo->datarows_locking)
     554          20 :                 row_pos += 4;
     555             : 
     556             :         /*
     557             :          * offset 0 = number of var columns
     558             :          * offset 1 = row number.  zeroed (datasever assigns)
     559             :          */
     560         214 :         var_cols_pos = row_pos;
     561         214 :         row_pos += 2;
     562             : 
     563         214 :         if ((row_pos = tds5_bcp_add_fixed_columns(bcpinfo, get_col_data, null_error, offset, record, row_pos)) < 0)
     564             :                 return TDS_FAIL;
     565             : 
     566         212 :         row_sz_pos = row_pos;
     567             : 
     568             :         /* potential variable columns to write */
     569             : 
     570         212 :         row_pos = tds5_bcp_add_variable_columns(bcpinfo, get_col_data, null_error, offset, record, row_pos, &var_cols_written);
     571         212 :         if (row_pos < 0)
     572             :                 return TDS_FAIL;
     573             : 
     574             : 
     575         212 :         if (var_cols_written) {
     576         190 :                 TDS_PUT_UA2LE(&record[row_sz_pos], row_pos);
     577         190 :                 record[var_cols_pos] = var_cols_written;
     578             :         }
     579             : 
     580         212 :         tdsdump_log(TDS_DBG_INFO1, "old_record_size = %d new size = %d \n", old_record_size, row_pos);
     581             : 
     582         212 :         tds_put_smallint(tds, row_pos);
     583         212 :         tds_put_n(tds, record, row_pos);
     584             : 
     585             :         /* row is done, now handle any text/image data */
     586             : 
     587         212 :         blob_cols = 0;
     588             : 
     589        3614 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     590        3406 :                 TDSCOLUMN  *bindcol = bcpinfo->bindinfo->columns[i];
     591        3406 :                 if (type_has_textptr(bindcol->on_server.column_type)) {
     592             :                         TDSRET rc;
     593             :                         /* Elide trailing NULLs */
     594          20 :                         if (bindcol->bcp_column_data->is_null) {
     595             :                                 int j;
     596             : 
     597           8 :                                 for (j = i + 1; j < bcpinfo->bindinfo->num_cols; ++j) {
     598           4 :                                         TDSCOLUMN *bindcol2 = bcpinfo->bindinfo->columns[j];
     599             : 
     600           4 :                                         if (type_has_textptr(bindcol2->column_type) && !bindcol2->bcp_column_data->is_null)
     601             :                                                 break;
     602             :                                 }
     603           6 :                                 if (j == bcpinfo->bindinfo->num_cols)
     604             :                                         break;
     605             :                         }
     606             : 
     607          16 :                         rc = tds5_get_col_data_or_dflt(get_col_data, bcpinfo, bindcol, offset, i);
     608          16 :                         TDS_PROPAGATE(rc);
     609             : 
     610             :                         /* unknown but zero */
     611          16 :                         tds_put_smallint(tds, 0);
     612          16 :                         TDS_PUT_BYTE(tds, bindcol->on_server.column_type);
     613          16 :                         tds_put_byte(tds, 0xff - blob_cols);
     614             :                         /*
     615             :                          * offset of txptr we stashed during variable
     616             :                          * column processing
     617             :                          */
     618          16 :                         tds_put_smallint(tds, bindcol->column_textpos);
     619          16 :                         tds_put_int(tds, bindcol->bcp_column_data->datalen);
     620          16 :                         tds_put_n(tds, bindcol->bcp_column_data->data, bindcol->bcp_column_data->datalen);
     621          16 :                         blob_cols++;
     622             : 
     623             :                 }
     624             :         }
     625             :         return TDS_SUCCESS;
     626             : }
     627             : 
     628             : /**
     629             :  * Send one row of data to server
     630             :  * \tds
     631             :  * \param bcpinfo BCP information
     632             :  * \param get_col_data function to call to retrieve data to be sent
     633             :  * \param ignored function to call if we try to send NULL if not allowed (not used)
     634             :  * \param offset passed to get_col_data and null_error to specify the row to get
     635             :  * \return TDS_SUCCESS or TDS_FAIL.
     636             :  */
     637             : TDSRET
     638        1080 : tds_bcp_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo,
     639             :                     tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
     640             : {
     641             :         TDSRET rc;
     642             : 
     643        1080 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_send_bcp_record(%p, %p, %p, %p, %d)\n",
     644             :                     tds, bcpinfo, get_col_data, null_error, offset);
     645             : 
     646        1080 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
     647             :                 return TDS_FAIL;
     648             : 
     649        1080 :         if (IS_TDS7_PLUS(tds->conn))
     650         866 :                 rc = tds7_send_record(tds, bcpinfo, get_col_data, null_error, offset);
     651             :         else
     652         214 :                 rc = tds5_send_record(tds, bcpinfo, get_col_data, null_error, offset);
     653             : 
     654        1080 :         tds_set_state(tds, TDS_SENDING);
     655        1080 :         return rc;
     656             : }
     657             : 
     658             : static inline void
     659             : tds5_swap_data(const TDSCOLUMN *col TDS_UNUSED, void *p TDS_UNUSED)
     660             : {
     661             : #ifdef WORDS_BIGENDIAN
     662             :         tds_swap_datatype(tds_get_conversion_type(col->on_server.column_type, col->column_size), p);
     663             : #endif
     664             : }
     665             : 
     666             : /**
     667             :  * Add fixed size columns to the row
     668             :  * \param bcpinfo BCP information
     669             :  * \param get_col_data function to call to retrieve data to be sent
     670             :  * \param ignored function to call if we try to send NULL if not allowed (not used)
     671             :  * \param offset passed to get_col_data and null_error to specify the row to get
     672             :  * \param rowbuffer row buffer to write to
     673             :  * \param start row buffer last end position
     674             :  * \returns new row length or -1 on error.
     675             :  */
     676             : static int
     677         214 : tds5_bcp_add_fixed_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
     678             :                            int offset, unsigned char * rowbuffer, int start)
     679             : {
     680             :         TDS_NUMERIC *num;
     681         214 :         int row_pos = start;
     682             :         int cpbytes;
     683             :         int i;
     684         214 :         int bitleft = 0, bitpos = 0;
     685             : 
     686         214 :         assert(bcpinfo);
     687         214 :         assert(rowbuffer);
     688             : 
     689         214 :         tdsdump_log(TDS_DBG_FUNC, "tds5_bcp_add_fixed_columns(%p, %p, %p, %d, %p, %d)\n",
     690             :                     bcpinfo, get_col_data, null_error, offset, rowbuffer, start);
     691             : 
     692        3410 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     693             : 
     694        3412 :                 TDSCOLUMN *const bcpcol = bcpinfo->bindinfo->columns[i];
     695        3412 :                 const TDS_INT column_size = bcpcol->on_server.column_size;
     696             : 
     697             :                 /* if possible check information from server */
     698        3412 :                 if (bcpinfo->sybase_count > i) {
     699        3412 :                         if (bcpinfo->sybase_colinfo[i].offset < 0)
     700        1836 :                                 continue;
     701             :                 } else {
     702           0 :                         if (is_nullable_type(bcpcol->on_server.column_type) || bcpcol->column_nullable)
     703           0 :                                 continue;
     704             :                 }
     705             : 
     706        1576 :                 tdsdump_log(TDS_DBG_FUNC, "tds5_bcp_add_fixed_columns column %d (%s) is a fixed column\n", i + 1,
     707             :                             tds_dstr_cstr(&bcpcol->column_name));
     708             : 
     709        1576 :                 if (TDS_FAILED(tds5_get_col_data_or_dflt(get_col_data, bcpinfo, bcpcol, offset, i))) {
     710           2 :                         tdsdump_log(TDS_DBG_INFO1, "get_col_data (column %d) failed\n", i + 1);
     711             :                         return -1;
     712             :                 }
     713             : 
     714             :                 /* We have no way to send a NULL at this point, return error to client */
     715        1574 :                 if (bcpcol->bcp_column_data->is_null) {
     716           0 :                         tdsdump_log(TDS_DBG_ERROR, "tds5_bcp_add_fixed_columns column %d is a null column\n", i + 1);
     717             :                         /* No value or default value available and NULL not allowed. */
     718           0 :                         if (null_error)
     719           0 :                                 null_error(bcpinfo, i, offset);
     720             :                         return -1;
     721             :                 }
     722             : 
     723        1574 :                 if (is_numeric_type(bcpcol->on_server.column_type)) {
     724         220 :                         num = (TDS_NUMERIC *) bcpcol->bcp_column_data->data;
     725         220 :                         cpbytes = tds_numeric_bytes_per_prec[num->precision];
     726         220 :                         memcpy(&rowbuffer[row_pos], num->array, cpbytes);
     727        1354 :                 } else if (bcpcol->column_type == SYBBIT) {
     728             :                         /* all bit are collapsed together */
     729         178 :                         if (!bitleft) {
     730         126 :                                 bitpos = row_pos++;
     731         126 :                                 bitleft = 8;
     732         126 :                                 rowbuffer[bitpos] = 0;
     733             :                         }
     734         178 :                         if (bcpcol->bcp_column_data->data[0])
     735         146 :                                 rowbuffer[bitpos] |= 256 >> bitleft;
     736         178 :                         --bitleft;
     737         178 :                         continue;
     738             :                 } else {
     739        1176 :                         cpbytes = bcpcol->bcp_column_data->datalen > column_size ?
     740             :                                   column_size : bcpcol->bcp_column_data->datalen;
     741        1176 :                         memcpy(&rowbuffer[row_pos], bcpcol->bcp_column_data->data, cpbytes);
     742        1176 :                         tds5_swap_data(bcpcol, &rowbuffer[row_pos]);
     743             : 
     744             :                         /* CHAR data may need padding out to the database length with blanks */
     745             :                         /* TODO check binary !!! */
     746        1176 :                         if (bcpcol->column_type == SYBCHAR && cpbytes < column_size)
     747         118 :                                 memset(rowbuffer + row_pos + cpbytes, ' ', column_size - cpbytes);
     748             :                 }
     749             : 
     750        1396 :                 row_pos += column_size;
     751             :         }
     752             :         return row_pos;
     753             : }
     754             : 
     755             : /**
     756             :  * Add variable size columns to the row
     757             :  *
     758             :  * \param bcpinfo BCP information already prepared
     759             :  * \param get_col_data function to call to retrieve data to be sent
     760             :  * \param null_error function to call if we try to send NULL if not allowed
     761             :  * \param offset passed to get_col_data and null_error to specify the row to get
     762             :  * \param rowbuffer The row image that will be sent to the server. 
     763             :  * \param start Where to begin copying data into the rowbuffer. 
     764             :  * \param pncols Address of output variable holding the count of columns added to the rowbuffer.  
     765             :  * 
     766             :  * \return length of (potentially modified) rowbuffer, or -1.
     767             :  */
     768             : static int
     769         212 : tds5_bcp_add_variable_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
     770             :                               int offset, TDS_UCHAR* rowbuffer, int start, int *pncols)
     771             : {
     772             :         TDS_USMALLINT offsets[256];
     773             :         unsigned int i, row_pos;
     774         212 :         unsigned int ncols = 0;
     775             : 
     776         212 :         assert(bcpinfo);
     777         212 :         assert(rowbuffer);
     778         212 :         assert(pncols);
     779             : 
     780         212 :         tdsdump_log(TDS_DBG_FUNC, "%4s %8s %18s %18s %8s\n",  "col", 
     781             :                                                                 "type", 
     782             :                                                                 "is_nullable_type", 
     783             :                                                                 "column_nullable", 
     784             :                                                                 "is null" );
     785        3408 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     786        3408 :                 TDSCOLUMN *bcpcol = bcpinfo->bindinfo->columns[i];
     787        3408 :                 tdsdump_log(TDS_DBG_FUNC, "%4d %8d %18s %18s %8s\n",  i,
     788             :                                                                         bcpcol->on_server.column_type,
     789             :                                                                         is_nullable_type(bcpcol->on_server.column_type)? "yes" : "no",
     790             :                                                                         bcpcol->column_nullable? "yes" : "no",
     791             :                                                                         bcpcol->bcp_column_data->is_null? "yes" : "no" );
     792             :         }
     793             : 
     794             :         /* the first two bytes of the rowbuffer are reserved to hold the entire record length */
     795         212 :         row_pos = start + 2;
     796         212 :         offsets[0] = row_pos;
     797             : 
     798         212 :         tdsdump_log(TDS_DBG_FUNC, "%4s %8s %8s %8s\n", "col", "ncols", "row_pos", "cpbytes");
     799             : 
     800        3408 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     801        3408 :                 unsigned int cpbytes = 0;
     802        3408 :                 TDSCOLUMN *bcpcol = bcpinfo->bindinfo->columns[i];
     803             : 
     804             :                 /*
     805             :                  * Is this column of "variable" type, i.e. NULLable
     806             :                  * or naturally variable length e.g. VARCHAR
     807             :                  */
     808        3408 :                 if (bcpinfo->sybase_count > (TDS_INT) i) {
     809        3408 :                         if (bcpinfo->sybase_colinfo[i].offset >= 0)
     810        1572 :                                 continue;
     811             :                 } else {
     812           0 :                         if (!is_nullable_type(bcpcol->on_server.column_type) && !bcpcol->column_nullable)
     813           0 :                                 continue;
     814             :                 }
     815             : 
     816        1836 :                 tdsdump_log(TDS_DBG_FUNC, "%4d %8d %8d %8d\n", i, ncols, row_pos, cpbytes);
     817             : 
     818        1836 :                 if (TDS_FAILED(tds5_get_col_data_or_dflt(get_col_data, bcpinfo, bcpcol, offset, i)))
     819             :                         return -1;
     820             : 
     821             :                 /* If it's a NOT NULL column, and we have no data, throw an error.
     822             :                  * This is the behavior for Sybase, this function is only used for Sybase */
     823        1836 :                 if (!bcpcol->column_nullable && bcpcol->bcp_column_data->is_null) {
     824             :                         /* No value or default value available and NULL not allowed. */
     825           0 :                         if (null_error)
     826           0 :                                 null_error(bcpinfo, i, offset);
     827             :                         return -1;
     828             :                 }
     829             : 
     830             :                 /* move the column buffer into the rowbuffer */
     831        1836 :                 if (!bcpcol->bcp_column_data->is_null) {
     832         476 :                         if (type_has_textptr(bcpcol->on_server.column_type)) {
     833          14 :                                 cpbytes = 16;
     834          14 :                                 bcpcol->column_textpos = row_pos;               /* save for data write */
     835         462 :                         } else if (is_numeric_type(bcpcol->on_server.column_type)) {
     836          24 :                                 TDS_NUMERIC *num = (TDS_NUMERIC *) bcpcol->bcp_column_data->data;
     837          24 :                                 cpbytes = tds_numeric_bytes_per_prec[num->precision];
     838          24 :                                 memcpy(&rowbuffer[row_pos], num->array, cpbytes);
     839         438 :                         } else if ((bcpcol->column_type == SYBVARCHAR || bcpcol->column_type == SYBCHAR)
     840         314 :                                    && bcpcol->bcp_column_data->datalen == 0) {
     841          20 :                                 cpbytes = 1;
     842          20 :                                 rowbuffer[row_pos] = ' ';
     843             :                         } else {
     844         836 :                                 cpbytes = bcpcol->bcp_column_data->datalen > bcpcol->column_size ?
     845         418 :                                 bcpcol->column_size : bcpcol->bcp_column_data->datalen;
     846         418 :                                 memcpy(&rowbuffer[row_pos], bcpcol->bcp_column_data->data, cpbytes);
     847         418 :                                 tds5_swap_data(bcpcol, &rowbuffer[row_pos]);
     848             :                         }
     849        1360 :                 } else if (type_has_textptr(bcpcol->column_type)) {
     850           8 :                         bcpcol->column_textpos = row_pos;
     851             :                 }
     852             : 
     853        1836 :                 row_pos += cpbytes;
     854        1836 :                 offsets[++ncols] = row_pos;
     855        1836 :                 tdsdump_dump_buf(TDS_DBG_NETWORK, "BCP row buffer so far", rowbuffer,  row_pos);
     856             :         }
     857             : 
     858         212 :         tdsdump_log(TDS_DBG_FUNC, "%4d %8d %8d\n", i, ncols, row_pos);
     859             : 
     860             :         /*
     861             :          * The rowbuffer ends with an offset table and, optionally, an adjustment table.  
     862             :          * The offset table has 1-byte elements that describe the locations of the start of each column in
     863             :          * the rowbuffer.  If the largest offset is greater than 255, another table -- the adjustment table --
     864             :          * is inserted just before the offset table.  It holds the high bytes. 
     865             :          * 
     866             :          * Both tables are laid out in reverse:
     867             :          *      #elements, offset N+1, offset N, offset N-1, ... offset 0
     868             :          * E.g. for 2 columns you have 4 data points:
     869             :          *      1.  How many elements (4)
     870             :          *      2.  Start of column 3 (non-existent, "one off the end")
     871             :          *      3.  Start of column 2
     872             :          *      4.  Start of column 1
     873             :          *  The length of each column is computed by subtracting its start from the its successor's start. 
     874             :          *
     875             :          * The algorithm below computes both tables. If the adjustment table isn't needed, the 
     876             :          * effect is to overwrite it with the offset table.  
     877             :          */
     878        1570 :         while (ncols && offsets[ncols] == offsets[ncols-1])
     879             :                 ncols--;        /* trailing NULL columns are not sent and are not included in the offset table */
     880             : 
     881         382 :         if (ncols && !bcpinfo->datarows_locking) {
     882         170 :                 TDS_UCHAR *poff = rowbuffer + row_pos;
     883         170 :                 unsigned int pfx_top = offsets[ncols] >> 8;
     884             : 
     885         170 :                 tdsdump_log(TDS_DBG_FUNC, "ncols=%u poff=%p [%u]\n", ncols, poff, offsets[ncols]);
     886             : 
     887         170 :                 if (offsets[ncols] / 256 == offsets[ncols - 1] / 256)
     888         168 :                         *poff++ = ncols + 1;
     889             :                 /* this is some kind of run-length-prefix encoding */
     890         180 :                 while (pfx_top) {
     891             :                         unsigned int n_pfx = 1;
     892             : 
     893         142 :                         for (i = 0; i <= ncols ; ++i)
     894         142 :                                 if ((offsets[i] >> 8) < pfx_top)
     895          90 :                                         ++n_pfx;
     896          10 :                         *poff++ = n_pfx;
     897          10 :                         --pfx_top;
     898             :                 }
     899             :    
     900         170 :                 tdsdump_log(TDS_DBG_FUNC, "poff=%p\n", poff);
     901             : 
     902         628 :                 for (i=0; i <= ncols; i++)
     903         628 :                         *poff++ = offsets[ncols-i] & 0xFF;
     904         170 :                 row_pos = (unsigned int)(poff - rowbuffer);
     905             :         } else {                /* Datarows-locking */
     906             :                 unsigned int col;
     907             : 
     908          62 :                 for (col = ncols; col-- > 0;) {
     909             :                         /* The DOL BULK format has a much simpler row table -- it's just a 2-byte length for every
     910             :                          * non-fixed column (does not have the extra "offset after the end" that the basic format has) */
     911          20 :                         rowbuffer[row_pos++] = offsets[col] % 256;
     912          20 :                         rowbuffer[row_pos++] = offsets[col] / 256;
     913             : 
     914          20 :                         tdsdump_log(TDS_DBG_FUNC, "[DOL BULK offset table] col=%u offset=%u\n", col, offsets[col]);
     915             :                 }
     916             :         }
     917             : 
     918         212 :         tdsdump_log(TDS_DBG_FUNC, "%4d %8d %8d\n", i, ncols, row_pos);
     919         212 :         tdsdump_dump_buf(TDS_DBG_NETWORK, "BCP row buffer", rowbuffer,  row_pos);
     920             : 
     921         212 :         *pncols = ncols;
     922             : 
     923         212 :         return ncols == 0? start : row_pos;
     924             : }
     925             : 
     926             : /**
     927             :  * Send BCP metadata to server.
     928             :  * Only for TDS 7.0+.
     929             :  * \tds
     930             :  * \param bcpinfo BCP information
     931             :  * \return TDS_SUCCESS or TDS_FAIL.
     932             :  */
     933             : static TDSRET
     934         306 : tds7_bcp_send_colmetadata(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
     935             : {
     936             :         TDSCOLUMN *bcpcol;
     937             :         int i, num_cols;
     938             : 
     939         306 :         tdsdump_log(TDS_DBG_FUNC, "tds7_bcp_send_colmetadata(%p, %p)\n", tds, bcpinfo);
     940         306 :         assert(tds && bcpinfo);
     941             : 
     942         306 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
     943             :                 return TDS_FAIL;
     944             : 
     945             :         /* 
     946             :          * Deep joy! For TDS 7 we have to send a colmetadata message followed by row data
     947             :          */
     948         306 :         tds_put_byte(tds, TDS7_RESULT_TOKEN);   /* 0x81 */
     949             : 
     950         306 :         num_cols = 0;
     951        2782 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     952        2476 :                 bcpcol = bcpinfo->bindinfo->columns[i];
     953        2476 :                 if ((!bcpinfo->identity_insert_on && bcpcol->column_identity) || bcpcol->column_timestamp
     954        2476 :                     || (bcpcol->column_computed && !bcpinfo->with_triggers)) {
     955           0 :                         continue;
     956             :                 }
     957        2476 :                 num_cols++;
     958             :         }
     959             : 
     960         306 :         tds_put_smallint(tds, num_cols);
     961             : 
     962        2782 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     963             :                 size_t converted_len;
     964             :                 const char *converted_name;
     965             : 
     966        2476 :                 bcpcol = bcpinfo->bindinfo->columns[i];
     967             : 
     968             :                 /*
     969             :                  * dont send the (meta)data for timestamp columns, or
     970             :                  * identity columns (unless indentity_insert is enabled
     971             :                  */
     972             : 
     973        2476 :                 if ((!bcpinfo->identity_insert_on && bcpcol->column_identity) || bcpcol->column_timestamp
     974        2476 :                     || (bcpcol->column_computed && !bcpinfo->with_triggers)) {
     975           0 :                         continue;
     976             :                 }
     977             : 
     978        2476 :                 if (IS_TDS72_PLUS(tds->conn))
     979        1280 :                         tds_put_int(tds, bcpcol->column_usertype);
     980             :                 else
     981        1196 :                         tds_put_smallint(tds, bcpcol->column_usertype);
     982        2476 :                 tds_put_smallint(tds, bcpcol->column_flags);
     983        2476 :                 TDS_PUT_BYTE(tds, bcpcol->on_server.column_type);
     984             : 
     985        2476 :                 assert(bcpcol->funcs);
     986        2476 :                 bcpcol->funcs->put_info(tds, bcpcol);
     987             : 
     988             :                 /* TODO put this in put_info. It seems that parameter format is
     989             :                  * different from BCP format
     990             :                  */
     991        2476 :                 if (type_has_textptr(bcpcol->on_server.column_type)) {
     992         180 :                         converted_name = tds_convert_string(tds, tds->conn->char_convs[client2ucs2],
     993          60 :                                                             tds_dstr_cstr(&bcpinfo->tablename),
     994         120 :                                                             (int) tds_dstr_len(&bcpinfo->tablename), &converted_len);
     995          60 :                         if (!converted_name) {
     996           0 :                                 tds_connection_close(tds->conn);
     997           0 :                                 return TDS_FAIL;
     998             :                         }
     999             : 
    1000             :                         /* UTF-16 length is always size / 2 even for 4 byte letters (yes, 1 letter of length 2) */
    1001          60 :                         TDS_PUT_SMALLINT(tds, converted_len / 2);
    1002          60 :                         tds_put_n(tds, converted_name, converted_len);
    1003             : 
    1004         120 :                         tds_convert_string_free(tds_dstr_cstr(&bcpinfo->tablename), converted_name);
    1005             :                 }
    1006             : 
    1007        7428 :                 converted_name = tds_convert_string(tds, tds->conn->char_convs[client2ucs2],
    1008        2476 :                                                     tds_dstr_cstr(&bcpcol->column_name),
    1009        4952 :                                                     (int) tds_dstr_len(&bcpcol->column_name), &converted_len);
    1010        2476 :                 if (!converted_name) {
    1011           0 :                         tds_connection_close(tds->conn);
    1012           0 :                         return TDS_FAIL;
    1013             :                 }
    1014             : 
    1015             :                 /* UTF-16 length is always size / 2 even for 4 byte letters (yes, 1 letter of length 2) */
    1016        2476 :                 TDS_PUT_BYTE(tds, converted_len / 2);
    1017        2476 :                 tds_put_n(tds, converted_name, converted_len);
    1018             : 
    1019        4952 :                 tds_convert_string_free(tds_dstr_cstr(&bcpcol->column_name), converted_name);
    1020             :         }
    1021             : 
    1022         306 :         tds_set_state(tds, TDS_SENDING);
    1023         306 :         return TDS_SUCCESS;
    1024             : }
    1025             : 
    1026             : /**
    1027             :  * Tell we finished sending BCP data to server
    1028             :  * \tds
    1029             :  * \param[out] rows_copied number of rows copied to server
    1030             :  */
    1031             : TDSRET
    1032         382 : tds_bcp_done(TDSSOCKET *tds, int *rows_copied)
    1033             : {
    1034         382 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_done(%p, %p)\n", tds, rows_copied);
    1035             : 
    1036         382 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1037             :                 return TDS_FAIL;
    1038             : 
    1039         372 :         tds_flush_packet(tds);
    1040             : 
    1041         372 :         tds_set_state(tds, TDS_PENDING);
    1042             : 
    1043         372 :         TDS_PROPAGATE(tds_process_simple_query(tds));
    1044             : 
    1045         364 :         if (rows_copied)
    1046         364 :                 *rows_copied = tds->rows_affected;
    1047             : 
    1048             :         return TDS_SUCCESS;
    1049             : }
    1050             : 
    1051             : /**
    1052             :  * Start sending BCP data to server.
    1053             :  * Initialize stream to accept data.
    1054             :  * \tds
    1055             :  * \param bcpinfo BCP information already prepared
    1056             :  */
    1057             : TDSRET
    1058         372 : tds_bcp_start(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
    1059             : {
    1060             :         TDSRET rc;
    1061             : 
    1062         372 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_start(%p, %p)\n", tds, bcpinfo);
    1063             : 
    1064         372 :         if (!IS_TDS50_PLUS(tds->conn))
    1065             :                 return TDS_FAIL;
    1066             : 
    1067         372 :         TDS_PROPAGATE(tds_submit_query(tds, bcpinfo->insert_stmt));
    1068             : 
    1069             :         /* set we want to switch to bulk state */
    1070         372 :         tds->bulk_query = true;
    1071             : 
    1072             :         /*
    1073             :          * In TDS 5 we get the column information as a result set from the "insert bulk" command.
    1074             :          */
    1075         372 :         if (IS_TDS50(tds->conn))
    1076          66 :                 rc = tds5_process_insert_bulk_reply(tds, bcpinfo);
    1077             :         else
    1078         306 :                 rc = tds_process_simple_query(tds);
    1079         372 :         TDS_PROPAGATE(rc);
    1080             : 
    1081         372 :         tds->out_flag = TDS_BULK;
    1082         372 :         if (tds_set_state(tds, TDS_SENDING) != TDS_SENDING)
    1083             :                 return TDS_FAIL;
    1084             : 
    1085         372 :         if (IS_TDS7_PLUS(tds->conn))
    1086         306 :                 tds7_bcp_send_colmetadata(tds, bcpinfo);
    1087             :         
    1088             :         return TDS_SUCCESS;
    1089             : }
    1090             : 
    1091             : enum {
    1092             :         /* list of columns we need, 0-nnn */
    1093             :         BULKCOL_colcnt,
    1094             :         BULKCOL_colid,
    1095             :         BULKCOL_type,
    1096             :         BULKCOL_length,
    1097             :         BULKCOL_status,
    1098             :         BULKCOL_offset,
    1099             :         BULKCOL_dflt,
    1100             : 
    1101             :         /* number of columns needed */
    1102             :         BULKCOL_COUNT,
    1103             : 
    1104             :         /* bitmask to have them all */
    1105             :         BULKCOL_ALL = (1 << BULKCOL_COUNT) -1,
    1106             : };
    1107             : 
    1108             : static int
    1109         796 : tds5_bulk_insert_column(const char *name)
    1110             : {
    1111             : #define BULKCOL(n) do {\
    1112             :         if (strcmp(name, #n) == 0) \
    1113             :                 return BULKCOL_ ## n; \
    1114             : } while(0)
    1115             : 
    1116         796 :         switch (name[0]) {
    1117         132 :         case 'c':
    1118         132 :                 BULKCOL(colcnt);
    1119          66 :                 BULKCOL(colid);
    1120             :                 break;
    1121          66 :         case 'd':
    1122          66 :                 BULKCOL(dflt);
    1123             :                 break;
    1124          66 :         case 't':
    1125          66 :                 BULKCOL(type);
    1126             :                 break;
    1127          66 :         case 'l':
    1128          66 :                 BULKCOL(length);
    1129             :                 break;
    1130         132 :         case 's':
    1131         132 :                 BULKCOL(status);
    1132             :                 break;
    1133          66 :         case 'o':
    1134          66 :                 BULKCOL(offset);
    1135             :                 break;
    1136             :         }
    1137             : #undef BULKCOL
    1138         334 :         return -1;
    1139             : }
    1140             : 
    1141             : static void
    1142           2 : tds5_read_bulk_defaults(TDSRESULTINFO *res_info, TDSBCPINFO *bcpinfo)
    1143             : {
    1144             :         int i;
    1145           2 :         TDS5COLINFO *syb_info = bcpinfo->sybase_colinfo;
    1146           2 :         TDS5COLINFO *const syb_info_end = syb_info + bcpinfo->sybase_count;
    1147             : 
    1148           6 :         for (i = 0; i < res_info->num_cols; ++i, ++syb_info) {
    1149           4 :                 TDSCOLUMN *col = res_info->columns[i];
    1150           4 :                 TDS_UCHAR *src = col->column_data;
    1151           4 :                 TDS_INT len = col->column_cur_size;
    1152             : 
    1153           4 :                 if (type_has_textptr(col->column_type))
    1154           0 :                         src = (unsigned char *) ((TDSBLOB *) src)->textvalue;
    1155             : 
    1156             :                 /* find next column having a default */
    1157             :                 for (;;) {
    1158             :                         /* avoid overflows */
    1159           8 :                         if (syb_info >= syb_info_end)
    1160             :                                 return;
    1161             : 
    1162           8 :                         if (syb_info->dflt)
    1163             :                                 break;
    1164           4 :                         ++syb_info;
    1165             :                 }
    1166             : 
    1167           4 :                 syb_info->dflt_size = len;
    1168           4 :                 if (TDS_RESIZE(syb_info->dflt_value, len))
    1169           4 :                         memcpy(syb_info->dflt_value, src, len);
    1170             :         }
    1171             : }
    1172             : 
    1173             : static TDSRET
    1174          66 : tds5_process_insert_bulk_reply(TDSSOCKET * tds, TDSBCPINFO *bcpinfo)
    1175             : {
    1176             :         TDS_INT res_type;
    1177             :         TDS_INT done_flags;
    1178             :         TDSRET  rc;
    1179          66 :         TDSRET  ret = TDS_SUCCESS;
    1180          66 :         bool row_match = false;
    1181             :         TDSRESULTINFO *res_info;
    1182             :         int icol;
    1183             :         unsigned col_flags;
    1184             :         /* position of the columns in the row */
    1185             :         int cols_pos[BULKCOL_COUNT];
    1186             :         int cols_values[BULKCOL_COUNT];
    1187             :         TDS5COLINFO *colinfo;
    1188             : 
    1189             : #if ENABLE_EXTRA_CHECKS
    1190          66 :         int num_defs = 0;
    1191             : #endif
    1192             : 
    1193          66 :         CHECK_TDS_EXTRA(tds);
    1194             : 
    1195         886 :         while ((rc = tds_process_tokens(tds, &res_type, &done_flags, TDS_RETURN_DONE|TDS_RETURN_ROWFMT|TDS_RETURN_ROW)) == TDS_SUCCESS) {
    1196         754 :                 switch (res_type) {
    1197          68 :                 case TDS_ROWFMT_RESULT:
    1198             :                         /* check if it's the resultset with column information and save column positions */
    1199          68 :                         row_match = false;
    1200          68 :                         col_flags = 0;
    1201          68 :                         res_info = tds->current_results;
    1202          68 :                         if (!res_info)
    1203           0 :                                 continue;
    1204         796 :                         for (icol = 0; icol < res_info->num_cols; ++icol) {
    1205         796 :                                 const TDSCOLUMN *col = res_info->columns[icol];
    1206        1592 :                                 int scol = tds5_bulk_insert_column(tds_dstr_cstr(&col->column_name));
    1207         796 :                                 if (scol < 0)
    1208         334 :                                         continue;
    1209         462 :                                 cols_pos[scol] = icol;
    1210         462 :                                 col_flags |= 1 << scol;
    1211             :                         }
    1212          68 :                         if (col_flags == BULKCOL_ALL)
    1213          66 :                                 row_match = true;
    1214             :                         break;
    1215         618 :                 case TDS_ROW_RESULT:
    1216             :                         /* get the results */
    1217         618 :                         col_flags = 0;
    1218         618 :                         res_info = tds->current_results;
    1219         618 :                         if (!res_info)
    1220           0 :                                 continue;
    1221         618 :                         if (!row_match) {
    1222           2 :                                 tds_extra_assert(res_info->num_cols == num_defs);
    1223           2 :                                 tds5_read_bulk_defaults(res_info, bcpinfo);
    1224           2 :                                 continue;
    1225             :                         }
    1226        4312 :                         for (icol = 0; icol < BULKCOL_COUNT; ++icol) {
    1227        4312 :                                 const TDSCOLUMN *col = res_info->columns[cols_pos[icol]];
    1228        4312 :                                 int ctype = tds_get_conversion_type(col->on_server.column_type, col->column_size);
    1229        4312 :                                 unsigned char *src = col->column_data;
    1230        4312 :                                 int srclen = col->column_cur_size;
    1231             :                                 CONV_RESULT dres;
    1232             : 
    1233        4312 :                                 if (tds_convert(tds_get_ctx(tds), ctype, src, srclen, SYBINT4, &dres) < 0)
    1234             :                                         break;
    1235        4312 :                                 col_flags |= 1 << icol;
    1236        4312 :                                 cols_values[icol] = dres.i;
    1237             :                         }
    1238             :                         /* save information */
    1239        1232 :                         if (col_flags != BULKCOL_ALL ||
    1240        1232 :                                 cols_values[BULKCOL_colcnt] < 1 ||
    1241         616 :                                 cols_values[BULKCOL_colcnt] > 4096 || /* limit of columns accepted */
    1242        1232 :                                 cols_values[BULKCOL_colid] < 1 ||
    1243             :                                 cols_values[BULKCOL_colid] > cols_values[BULKCOL_colcnt]) {
    1244             :                                 rc = TDS_FAIL;
    1245             :                                 break;
    1246             :                         }
    1247         616 :                         if (bcpinfo->sybase_colinfo == NULL) {
    1248          60 :                                 bcpinfo->sybase_colinfo = calloc(cols_values[BULKCOL_colcnt], sizeof(*bcpinfo->sybase_colinfo));
    1249          60 :                                 if (bcpinfo->sybase_colinfo == NULL) {
    1250             :                                         rc = TDS_FAIL;
    1251             :                                         break;
    1252             :                                 }
    1253          60 :                                 bcpinfo->sybase_count = cols_values[BULKCOL_colcnt];
    1254             :                         }
    1255             :                         /* bound check, colcnt could have changed from row to row */
    1256         616 :                         if (cols_values[BULKCOL_colid] > bcpinfo->sybase_count) {
    1257             :                                 rc = TDS_FAIL;
    1258             :                                 break;
    1259             :                         }
    1260         616 :                         colinfo = &bcpinfo->sybase_colinfo[cols_values[BULKCOL_colid] - 1];
    1261         616 :                         colinfo->type = cols_values[BULKCOL_type];
    1262         616 :                         colinfo->status = cols_values[BULKCOL_status];
    1263         616 :                         colinfo->offset = cols_values[BULKCOL_offset];
    1264         616 :                         colinfo->length = cols_values[BULKCOL_length];
    1265         616 :                         colinfo->dflt = !!cols_values[BULKCOL_dflt];
    1266             : #if ENABLE_EXTRA_CHECKS
    1267         616 :                         if (colinfo->dflt)
    1268           4 :                                 ++num_defs;
    1269             : #endif
    1270         616 :                         tdsdump_log(TDS_DBG_INFO1, "gotten row information %d type %d length %d status %d offset %d\n",
    1271             :                                     cols_values[BULKCOL_colid], colinfo->type, colinfo->length, colinfo->status, colinfo->offset);
    1272             :                         break;
    1273          68 :                 case TDS_DONE_RESULT:
    1274             :                 case TDS_DONEPROC_RESULT:
    1275             :                 case TDS_DONEINPROC_RESULT:
    1276          68 :                         if ((done_flags & TDS_DONE_ERROR) != 0)
    1277           0 :                                 ret = TDS_FAIL;
    1278             :                 default:
    1279             :                         break;
    1280             :                 }
    1281             :         }
    1282          66 :         if (TDS_FAILED(rc))
    1283           0 :                 ret = rc;
    1284             : 
    1285          66 :         return ret;
    1286             : }
    1287             : 
    1288             : static TDSRET
    1289        3428 : tds5_get_col_data_or_dflt(tds_bcp_get_col_data get_col_data, TDSBCPINFO *bulk, TDSCOLUMN *bcpcol, int offset, int colnum)
    1290             : {
    1291             :         TDSRET ret;
    1292             :         BCPCOLDATA *coldata;
    1293             : 
    1294        3428 :         ret = get_col_data(bulk, bcpcol, offset);
    1295        3428 :         coldata = bcpcol->bcp_column_data;
    1296        3428 :         if (coldata->is_null && bulk->sybase_colinfo != NULL && !type_has_textptr(bcpcol->column_type)) {
    1297        1360 :                 const TDS5COLINFO *syb_info = &bulk->sybase_colinfo[colnum];
    1298             : 
    1299        1360 :                 if (!syb_info->dflt) {
    1300             :                         /* leave to NULL */
    1301        1354 :                         if (!bcpcol->column_nullable)
    1302             :                                 return TDS_FAIL;
    1303             :                 } else {
    1304           6 :                         if (!TDS_RESIZE(coldata->data, syb_info->dflt_size))
    1305             :                                 return TDS_FAIL;
    1306             : 
    1307           6 :                         memcpy(coldata->data, syb_info->dflt_value, syb_info->dflt_size);
    1308           6 :                         coldata->datalen = syb_info->dflt_size;
    1309           6 :                         coldata->is_null = false;
    1310             :                 }
    1311             :                 return TDS_SUCCESS;
    1312             :         }
    1313             :         return ret;
    1314             : }
    1315             : 
    1316             : /**
    1317             :  * Free row data allocated in the result set.
    1318             :  */
    1319             : static void 
    1320          90 : tds_bcp_row_free(TDSRESULTINFO* result, unsigned char *row TDS_UNUSED)
    1321             : {
    1322          90 :         result->row_size = 0;
    1323          90 :         TDS_ZERO_FREE(result->current_row);
    1324          90 : }
    1325             : 
    1326             : /**
    1327             :  * Start bulk copy to server
    1328             :  * \tds
    1329             :  * \param bcpinfo BCP information already prepared
    1330             :  */
    1331             : TDSRET
    1332         338 : tds_bcp_start_copy_in(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
    1333             : {
    1334             :         TDSCOLUMN *bcpcol;
    1335             :         int i;
    1336         338 :         int fixed_col_len_tot     = 0;
    1337         338 :         int variable_col_len_tot  = 0;
    1338         338 :         int column_bcp_data_size  = 0;
    1339         338 :         int bcp_record_size       = 0;
    1340             :         TDSRET rc;
    1341             :         TDS_INT var_cols;
    1342             :         
    1343         338 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_start_copy_in(%p, %p)\n", tds, bcpinfo);
    1344             : 
    1345         338 :         TDS_PROPAGATE(tds_bcp_start_insert_stmt(tds, bcpinfo));
    1346             : 
    1347         338 :         rc = tds_bcp_start(tds, bcpinfo);
    1348         338 :         if (TDS_FAILED(rc)) {
    1349             :                 /* TODO, in CTLib was _ctclient_msg(blkdesc->con, "blk_rowxfer", 2, 5, 1, 140, ""); */
    1350             :                 return rc;
    1351             :         }
    1352             : 
    1353             :         /* 
    1354             :          * Work out the number of "variable" columns.  These are either nullable or of 
    1355             :          * varying length type e.g. varchar.   
    1356             :          */
    1357         338 :         var_cols = 0;
    1358             : 
    1359         338 :         if (IS_TDS50(tds->conn)) {
    1360         446 :                 for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
    1361             :         
    1362         446 :                         bcpcol = bcpinfo->bindinfo->columns[i];
    1363             : 
    1364             :                         /*
    1365             :                          * work out storage required for this datatype
    1366             :                          * blobs always require 16, numerics vary, the
    1367             :                          * rest can be taken from the server
    1368             :                          */
    1369             : 
    1370         446 :                         if (type_has_textptr(bcpcol->on_server.column_type))
    1371             :                                 column_bcp_data_size  = 16;
    1372         436 :                         else if (is_numeric_type(bcpcol->on_server.column_type))
    1373          42 :                                 column_bcp_data_size  = tds_numeric_bytes_per_prec[bcpcol->column_prec];
    1374             :                         else
    1375         394 :                                 column_bcp_data_size  = bcpcol->column_size;
    1376             : 
    1377             :                         /*
    1378             :                          * now add that size into either fixed or variable
    1379             :                          * column totals...
    1380             :                          */
    1381             : 
    1382         446 :                         if (is_nullable_type(bcpcol->on_server.column_type) || bcpcol->column_nullable) {
    1383         250 :                                 var_cols++;
    1384         250 :                                 variable_col_len_tot += column_bcp_data_size;
    1385             :                         } else {
    1386         196 :                                 fixed_col_len_tot += column_bcp_data_size;
    1387             :                         }
    1388             :                 }
    1389             : 
    1390             :                 /* this formula taken from sybase manual... */
    1391             : 
    1392         120 :                 bcp_record_size =       4 +
    1393          60 :                                                         fixed_col_len_tot +
    1394          60 :                                                         variable_col_len_tot +
    1395         120 :                                                         ( (int)(variable_col_len_tot / 256 ) + 1 ) +
    1396          60 :                                                         (var_cols + 1) +
    1397             :                                                         2;
    1398             : 
    1399          60 :                 tdsdump_log(TDS_DBG_FUNC, "current_record_size = %d\n", bcpinfo->bindinfo->row_size);
    1400          60 :                 tdsdump_log(TDS_DBG_FUNC, "bcp_record_size     = %d\n", bcp_record_size);
    1401             : 
    1402          60 :                 if (bcp_record_size > bcpinfo->bindinfo->row_size) {
    1403          24 :                         if (!TDS_RESIZE(bcpinfo->bindinfo->current_row, bcp_record_size)) {
    1404           0 :                                 tdsdump_log(TDS_DBG_FUNC, "could not realloc current_row\n");
    1405             :                                 return TDS_FAIL;
    1406             :                         }
    1407          24 :                         bcpinfo->bindinfo->row_free = tds_bcp_row_free;
    1408          24 :                         bcpinfo->bindinfo->row_size = bcp_record_size;
    1409             :                 }
    1410             :         }
    1411             : 
    1412             :         return TDS_SUCCESS;
    1413             : }
    1414             : 
    1415             : /** input stream to read a file */
    1416             : typedef struct tds_file_stream {
    1417             :         /** common fields, must be the first field */
    1418             :         TDSINSTREAM stream;
    1419             :         /** file to read from */
    1420             :         FILE *f;
    1421             : 
    1422             :         /** terminator */
    1423             :         const char *terminator;
    1424             :         /** terminator length in bytes */
    1425             :         size_t term_len;
    1426             : 
    1427             :         /** buffer for store bytes readed that could be the terminator */
    1428             :         char *left;
    1429             :         size_t left_pos;
    1430             : } TDSFILESTREAM;
    1431             : 
    1432             : /** \cond HIDDEN_SYMBOLS */
    1433             : #if defined(_WIN32) && defined(HAVE__LOCK_FILE) && defined(HAVE__UNLOCK_FILE)
    1434             : #define TDS_HAVE_STDIO_LOCKED 1
    1435             : #define flockfile(s) _lock_file(s)
    1436             : #define funlockfile(s) _unlock_file(s)
    1437             : #define getc_unlocked(s) _getc_nolock(s)
    1438             : #define feof_unlocked(s) _feof_nolock(s)
    1439             : #endif
    1440             : 
    1441             : #ifndef TDS_HAVE_STDIO_LOCKED
    1442             : #undef getc_unlocked
    1443             : #undef feof_unlocked
    1444             : #undef flockfile
    1445             : #undef funlockfile
    1446             : #define getc_unlocked(s) getc(s)
    1447             : #define feof_unlocked(s) feof(s)
    1448             : #define flockfile(s) do { } while(0)
    1449             : #define funlockfile(s) do { } while(0)
    1450             : #endif
    1451             : /** \endcond */
    1452             : 
    1453             : /**
    1454             :  * Reads a chunk of data from file stream checking for terminator
    1455             :  * \param stream file stream
    1456             :  * \param ptr buffer where to read data
    1457             :  * \param len length of buffer
    1458             :  */
    1459             : static int
    1460        5328 : tds_file_stream_read(TDSINSTREAM *stream, void *ptr, size_t len)
    1461             : {
    1462        5328 :         TDSFILESTREAM *s = (TDSFILESTREAM *) stream;
    1463             :         int c;
    1464        5328 :         char *p = (char *) ptr;
    1465             : 
    1466     1901032 :         while (len) {
    1467     1895254 :                 if (memcmp(s->left, s->terminator - s->left_pos, s->term_len) == 0)
    1468        4878 :                         return p - (char *) ptr;
    1469             : 
    1470     3780752 :                 c = getc_unlocked(s->f);
    1471     1890376 :                 if (c == EOF)
    1472             :                         return -1;
    1473             : 
    1474     1890376 :                 *p++ = s->left[s->left_pos];
    1475     1890376 :                 --len;
    1476             : 
    1477     1890376 :                 s->left[s->left_pos++] = c;
    1478     1890376 :                 s->left_pos %= s->term_len;
    1479             :         }
    1480         450 :         return p - (char *) ptr;
    1481             : }
    1482             : 
    1483             : /**
    1484             :  * Read a data file, passing the data through iconv().
    1485             :  * \retval TDS_SUCCESS  success
    1486             :  * \retval TDS_FAIL     error reading the column
    1487             :  * \retval TDS_NO_MORE_RESULTS end of file detected
    1488             :  */
    1489             : TDSRET
    1490        2708 : tds_bcp_fread(TDSSOCKET * tds, TDSICONV * char_conv, FILE * stream, const char *terminator, size_t term_len, char **outbuf, size_t * outbytes)
    1491             : {
    1492             :         TDSRET res;
    1493             :         TDSFILESTREAM r;
    1494             :         TDSDYNAMICSTREAM w;
    1495             :         size_t readed;
    1496             : 
    1497             :         /* prepare streams */
    1498        2708 :         r.stream.read = tds_file_stream_read;
    1499        2708 :         r.f = stream;
    1500        2708 :         r.term_len = term_len;
    1501        2708 :         r.left = tds_new0(char, term_len*3);
    1502        2708 :         r.left_pos = 0;
    1503        2708 :         if (!r.left) return TDS_FAIL;
    1504             : 
    1505             :         /* copy terminator twice, let terminator points to second copy */
    1506        2708 :         memcpy(r.left + term_len, terminator, term_len);
    1507        2708 :         memcpy(r.left + term_len*2u, terminator, term_len);
    1508        2708 :         r.terminator = r.left + term_len*2u;
    1509             : 
    1510             :         /* read initial buffer to test with terminator */
    1511        2708 :         readed = fread(r.left, 1, term_len, stream);
    1512        2708 :         if (readed != term_len) {
    1513         154 :                 free(r.left);
    1514         154 :                 if (readed == 0 && feof(stream))
    1515             :                         return TDS_NO_MORE_RESULTS;
    1516             :                 return TDS_FAIL;
    1517             :         }
    1518             : 
    1519        2554 :         res = tds_dynamic_stream_init(&w, (void**) outbuf, 0);
    1520        2554 :         if (TDS_FAILED(res)) {
    1521           0 :                 free(r.left);
    1522           0 :                 return res;
    1523             :         }
    1524             : 
    1525             :         /* convert/copy from input stream to output one */
    1526        2554 :         flockfile(stream);
    1527        2554 :         if (char_conv == NULL)
    1528         956 :                 res = tds_copy_stream(&r.stream, &w.stream);
    1529             :         else
    1530        1598 :                 res = tds_convert_stream(tds, char_conv, to_server, &r.stream, &w.stream);
    1531        2554 :         funlockfile(stream);
    1532        2554 :         free(r.left);
    1533             : 
    1534        2554 :         TDS_PROPAGATE(res);
    1535             : 
    1536        2554 :         *outbytes = w.size;
    1537             : 
    1538             :         /* terminate buffer */
    1539        2554 :         if (!w.stream.buf_len)
    1540             :                 return TDS_FAIL;
    1541             : 
    1542        2554 :         ((char *) w.stream.buffer)[0] = 0;
    1543        2554 :         w.stream.write(&w.stream, 1);
    1544             : 
    1545        2554 :         return res;
    1546             : }
    1547             : 
    1548             : /**
    1549             :  * Start writing writetext request.
    1550             :  * This request start a bulk session.
    1551             :  * \tds
    1552             :  * \param objname table name
    1553             :  * \param textptr TEXTPTR (see sql documentation)
    1554             :  * \param timestamp data timestamp
    1555             :  * \param with_log is log is enabled during insert
    1556             :  * \param size bytes to be inserted
    1557             :  */
    1558             : TDSRET
    1559          42 : tds_writetext_start(TDSSOCKET *tds, const char *objname, const char *textptr, const char *timestamp, int with_log, TDS_UINT size)
    1560             : {
    1561             :         TDSRET rc;
    1562             : 
    1563             :         /* TODO mssql does not like timestamp */
    1564          42 :         rc = tds_submit_queryf(tds,
    1565             :                               "writetext bulk %s 0x%s timestamp = 0x%s%s",
    1566             :                               objname, textptr, timestamp, with_log ? " with log" : "");
    1567          42 :         TDS_PROPAGATE(rc);
    1568             : 
    1569             :         /* set we want to switch to bulk state */
    1570          42 :         tds->bulk_query = true;
    1571             : 
    1572             :         /* read the end token */
    1573          42 :         TDS_PROPAGATE(tds_process_simple_query(tds));
    1574             : 
    1575          42 :         tds->out_flag = TDS_BULK;
    1576          42 :         if (tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1577             :                 return TDS_FAIL;
    1578             : 
    1579          42 :         tds_put_int(tds, size);
    1580             : 
    1581          42 :         tds_set_state(tds, TDS_SENDING);
    1582          42 :         return TDS_SUCCESS;
    1583             : }
    1584             : 
    1585             : /**
    1586             :  * Send some data in the writetext request started by tds_writetext_start.
    1587             :  * You should write in total (with multiple calls to this function) all
    1588             :  * bytes declared calling tds_writetext_start.
    1589             :  * \tds
    1590             :  * \param text data to write
    1591             :  * \param size data size in bytes
    1592             :  */
    1593             : TDSRET
    1594         312 : tds_writetext_continue(TDSSOCKET *tds, const TDS_UCHAR *text, TDS_UINT size)
    1595             : {
    1596         312 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1597             :                 return TDS_FAIL;
    1598             : 
    1599             :         /* TODO check size left */
    1600         312 :         tds_put_n(tds, text, size);
    1601             : 
    1602         312 :         tds_set_state(tds, TDS_SENDING);
    1603         312 :         return TDS_SUCCESS;
    1604             : }
    1605             : 
    1606             : /**
    1607             :  * Finish sending writetext data.
    1608             :  * \tds
    1609             :  */
    1610             : TDSRET
    1611          42 : tds_writetext_end(TDSSOCKET *tds)
    1612             : {
    1613          42 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1614             :                 return TDS_FAIL;
    1615             : 
    1616          42 :         tds_flush_packet(tds);
    1617          42 :         tds_set_state(tds, TDS_PENDING);
    1618          42 :         return TDS_SUCCESS;
    1619             : }

Generated by: LCOV version 1.13