LCOV - code coverage report
Current view: top level - src/tds - bulk.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 689 752 91.6 %
Date: 2026-11-16 21:40:19 Functions: 36 36 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             : #if HAVE_FCNTL_H
      40             : #include <fcntl.h>
      41             : #endif /* HAVE_FCNTL_H */
      42             : 
      43             : #ifdef _WIN32
      44             : #include <io.h>
      45             : #endif
      46             : 
      47             : #include <assert.h>
      48             : 
      49             : #include <freetds/tds.h>
      50             : #include <freetds/tds/checks.h>
      51             : #include <freetds/bytes.h>
      52             : #include <freetds/tds/iconv.h>
      53             : #include <freetds/tds/stream.h>
      54             : #include <freetds/tds/convert.h>
      55             : #include <freetds/utils/string.h>
      56             : #include <freetds/utils/ascii.h>
      57             : #include <freetds/replacements.h>
      58             : #include <freetds/enum_cap.h>
      59             : 
      60             : /**
      61             :  * Holds clause buffer
      62             :  */
      63             : typedef struct tds_pbcb
      64             : {
      65             :         /** buffer */
      66             :         char *pb;
      67             :         /** buffer length */
      68             :         unsigned int cb;
      69             :         /** true is buffer came from malloc */
      70             :         bool from_malloc;
      71             : } TDSPBCB;
      72             : 
      73             : static TDSRET tds7_bcp_send_colmetadata(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
      74             : static TDSRET tds_bcp_start_insert_stmt(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
      75             : static int tds5_bcp_add_fixed_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
      76             :                                       int offset, unsigned char * rowbuffer, int start);
      77             : static int tds5_bcp_add_variable_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
      78             :                                          int offset, TDS_UCHAR *rowbuffer, int start, int *pncols);
      79             : static void tds_bcp_row_free(TDSRESULTINFO* result, unsigned char *row);
      80             : static TDSRET tds5_process_insert_bulk_reply(TDSSOCKET * tds, TDSBCPINFO *bcpinfo);
      81             : static TDSRET probe_sap_locking(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
      82             : static TDSRET tds5_get_col_data_or_dflt(tds_bcp_get_col_data get_col_data,
      83             :                                         TDSBCPINFO * bulk, TDSCOLUMN * bcpcol, int offset, int colnum);
      84             : 
      85             : /**
      86             :  * Initialize BCP information.
      87             :  * Query structure of the table to server.
      88             :  * \tds
      89             :  * \param bcpinfo BCP information to initialize. Structure should be allocate
      90             :  *        and table name and direction should be already set.
      91             :  */
      92             : TDSRET
      93        1360 : tds_bcp_init(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
      94             : {
      95             :         TDSRESULTINFO *resinfo;
      96        1360 :         TDSRESULTINFO *bindinfo = NULL;
      97             :         TDSCOLUMN *curcol;
      98             :         TDS_INT result_type;
      99             :         int i;
     100             :         TDSRET rc;
     101             :         const char *fmt;
     102             : 
     103             :         /* FIXME don't leave state in processing state */
     104             : 
     105             :         /* Check table locking type. Do this first, because the code in bcp_init() which
     106             :          * calls us seems to depend on the information from the columns query still being
     107             :          * active in the context.
     108             :          */
     109        1360 :         probe_sap_locking(tds, bcpinfo);
     110             : 
     111             :         /* TODO quote tablename if needed */
     112        1360 :         if (bcpinfo->direction != TDS_BCP_QUERYOUT)
     113             :                 fmt = "SET FMTONLY ON select * from %s SET FMTONLY OFF";
     114             :         else
     115          10 :                 fmt = "SET FMTONLY ON %s SET FMTONLY OFF";
     116             : 
     117        2720 :         if (TDS_FAILED(rc=tds_submit_queryf(tds, fmt, tds_dstr_cstr(&bcpinfo->tablename))))
     118             :                 /* TODO return an error ?? */
     119             :                 /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */
     120             :                 return rc;
     121             : 
     122             :         /* TODO possibly stop at ROWFMT and copy before going to idle */
     123             :         /* TODO check what happen if table is not present, cleanup on error */
     124        6800 :         while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS))
     125             :                    == TDS_SUCCESS)
     126        5440 :                 continue;
     127        1360 :         TDS_PROPAGATE(rc);
     128             : 
     129             :         /* copy the results info from the TDS socket */
     130        1360 :         if (!tds->res_info)
     131             :                 return TDS_FAIL;
     132             : 
     133        1360 :         resinfo = tds->res_info;
     134        1360 :         if ((bindinfo = tds_alloc_results(resinfo->num_cols)) == NULL) {
     135             :                 rc = TDS_FAIL;
     136             :                 goto cleanup;
     137             :         }
     138             : 
     139             :         /* Copy the column metadata */
     140             :         rc = TDS_FAIL;
     141        5188 :         for (i = 0; i < bindinfo->num_cols; i++) {
     142             : 
     143        5188 :                 curcol = bindinfo->columns[i];
     144             :                 
     145             :                 /*
     146             :                  * TODO use memcpy ??
     147             :                  * curcol and resinfo->columns[i] are both TDSCOLUMN.  
     148             :                  * Why not "curcol = resinfo->columns[i];"?  Because the rest of TDSCOLUMN (below column_timestamp)
     149             :                  * isn't being used.  Perhaps this "upper" part of TDSCOLUMN should be a substructure.
     150             :                  * Or, see if the "lower" part is unused (and zeroed out) at this point, and just do one assignment.
     151             :                  */
     152        5188 :                 curcol->funcs = resinfo->columns[i]->funcs;
     153        5188 :                 curcol->column_type = resinfo->columns[i]->column_type;
     154        5188 :                 curcol->column_usertype = resinfo->columns[i]->column_usertype;
     155        5188 :                 curcol->column_flags = resinfo->columns[i]->column_flags;
     156        5188 :                 if (curcol->column_varint_size == 0)
     157        5188 :                         curcol->column_cur_size = resinfo->columns[i]->column_cur_size;
     158             :                 else
     159           0 :                         curcol->column_cur_size = -1;
     160        5188 :                 curcol->column_size = resinfo->columns[i]->column_size;
     161        5188 :                 curcol->column_varint_size = resinfo->columns[i]->column_varint_size;
     162        5188 :                 curcol->column_prec = resinfo->columns[i]->column_prec;
     163        5188 :                 curcol->column_scale = resinfo->columns[i]->column_scale;
     164        5188 :                 curcol->on_server = resinfo->columns[i]->on_server;
     165        5188 :                 curcol->char_conv = resinfo->columns[i]->char_conv;
     166        5188 :                 if (!tds_dstr_dup(&curcol->column_name, &resinfo->columns[i]->column_name))
     167             :                         goto cleanup;
     168        5188 :                 if (!tds_dstr_dup(&curcol->table_column_name, &resinfo->columns[i]->table_column_name))
     169             :                         goto cleanup;
     170        5188 :                 curcol->column_nullable = resinfo->columns[i]->column_nullable;
     171        5188 :                 curcol->column_identity = resinfo->columns[i]->column_identity;
     172        5188 :                 curcol->column_timestamp = resinfo->columns[i]->column_timestamp;
     173        5188 :                 curcol->column_computed = resinfo->columns[i]->column_computed;
     174             :                 
     175        5188 :                 memcpy(curcol->column_collation, resinfo->columns[i]->column_collation, 5);
     176        5188 :                 curcol->use_iconv_out = 0;
     177             : 
     178             :                 /* From MS documentation:
     179             :                  * Note that for INSERT BULK operations, XMLTYPE is to be sent as NVARCHAR(N) or NVARCHAR(MAX)
     180             :                  * data type. An error is produced if XMLTYPE is specified.
     181             :                  */
     182        5188 :                 if (curcol->on_server.column_type == SYBMSXML) {
     183           8 :                         curcol->on_server.column_type = XSYBNVARCHAR;
     184           8 :                         curcol->column_type = SYBVARCHAR;
     185           8 :                         memcpy(curcol->column_collation, tds->conn->collation, 5);
     186             :                 }
     187             : 
     188        5188 :                 if (is_numeric_type(curcol->column_type)) {
     189         302 :                         curcol->bcp_column_data = tds_alloc_bcp_column_data(sizeof(TDS_NUMERIC));
     190         302 :                         ((TDS_NUMERIC *) curcol->bcp_column_data->data)->precision = curcol->column_prec;
     191         302 :                         ((TDS_NUMERIC *) curcol->bcp_column_data->data)->scale = curcol->column_scale;
     192             :                 } else {
     193        4886 :                         curcol->bcp_column_data = 
     194        4886 :                                 tds_alloc_bcp_column_data(TDS_MAX(curcol->column_size,curcol->on_server.column_size));
     195             :                 }
     196        5188 :                 if (!curcol->bcp_column_data)
     197             :                         goto cleanup;
     198             :         }
     199             : 
     200        1360 :         if (bcpinfo->identity_insert_on) {
     201             : 
     202           0 :                 rc = tds_submit_queryf(tds, "set identity_insert %s on", tds_dstr_cstr(&bcpinfo->tablename));
     203           0 :                 if (TDS_FAILED(rc))
     204             :                         goto cleanup;
     205             : 
     206             :                 /* TODO use tds_process_simple_query */
     207           0 :                 while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS))
     208             :                            == TDS_SUCCESS) {
     209             :                 }
     210           0 :                 if (rc != TDS_NO_MORE_RESULTS)
     211             :                         goto cleanup;
     212             :         }
     213             : 
     214        1360 :         bcpinfo->bindinfo = bindinfo;
     215        1360 :         bcpinfo->bind_count = 0;
     216             : 
     217        1360 :         return TDS_SUCCESS;
     218             : 
     219           0 : cleanup:
     220           0 :         tds_free_results(bindinfo);
     221           0 :         return rc;
     222             : }
     223             : 
     224             : /**
     225             :  * Detect if table we're writing to uses 'datarows' lockmode.
     226             :  * \tds
     227             :  * \param bcpinfo BCP information already prepared
     228             :  * \return TDS_SUCCESS or TDS_FAIL.
     229             :  */
     230             : static TDSRET
     231        1360 : probe_sap_locking(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
     232             : {
     233             :         TDSRET rc;
     234             :         unsigned int value;
     235             :         bool value_found;
     236             :         TDS_INT resulttype;
     237             :         const char *full_tablename, *rdot, *tablename;
     238             : 
     239             :         /* Only needed for inward data */
     240        1360 :         if (bcpinfo->direction != TDS_BCP_IN)
     241             :                 return TDS_SUCCESS;
     242             : 
     243             :         /* Only needed for SAP ASE versions which support datarows-locking. */
     244        1338 :         if (!TDS_IS_SYBASE(tds) || !tds_capability_has_req(tds->conn, TDS_REQ_DOL_BULK))
     245             :                 return TDS_SUCCESS;
     246             : 
     247             :         /* A request to probe database.owner.tablename needs to check database.owner.sysobjects for tablename
     248             :          * (it doesn't work to check sysobjects for database.owner.tablename) */
     249         428 :         full_tablename = tds_dstr_cstr(&bcpinfo->tablename);
     250         214 :         rdot = strrchr(full_tablename, '.');
     251             : 
     252         214 :         if (rdot != NULL)
     253           2 :                 tablename = rdot + 1;
     254             :         else
     255             :                 tablename = full_tablename;
     256             : 
     257             :         /* Temporary tables attributes are stored differently */
     258         214 :         if (rdot == NULL && tablename[0] == '#') {
     259          38 :                 rc = tds_submit_queryf(tds, "select sysstat2 from tempdb..sysobjects where id = object_id('tempdb..%s')",
     260             :                                        tablename);
     261             :         } else {
     262         178 :                 rc = tds_submit_queryf(tds, "select sysstat2 from %.*ssysobjects where type='U' and name='%s'",
     263           2 :                                        (int) (rdot ? (rdot - full_tablename + 1) : 0), full_tablename, tablename);
     264             :         }
     265         214 :         TDS_PROPAGATE(rc);
     266             : 
     267             :         value = 0;
     268             :         value_found = false;
     269             : 
     270         642 :         while ((rc = tds_process_tokens(tds, &resulttype, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
     271         428 :                 const unsigned int stop_mask = TDS_RETURN_DONE | TDS_RETURN_ROW;
     272             : 
     273         428 :                 if (resulttype != TDS_ROW_RESULT)
     274         214 :                         continue;
     275             : 
     276             :                 /* We must keep processing result tokens (even if we've found what we're looking for) so that the
     277             :                  * stream is ready for subsequent queries. */
     278         428 :                 while ((rc = tds_process_tokens(tds, &resulttype, NULL, stop_mask)) == TDS_SUCCESS) {
     279             :                         TDS_INT res;
     280             : 
     281         428 :                         if (resulttype != TDS_ROW_RESULT)
     282             :                                 break;
     283             : 
     284             :                         /* Get INT4 from column 0 */
     285         214 :                         if (!tds->current_results || tds->current_results->num_cols < 1)
     286           0 :                                 continue;
     287             : 
     288         214 :                         if (!tds_try_get_int_col(tds, tds->current_results->columns[0], &res))
     289           0 :                                 continue;
     290             : 
     291         214 :                         value = res;
     292         214 :                         value_found = true;
     293             :                 }
     294             :         }
     295         214 :         TDS_PROPAGATE(rc);
     296             : 
     297             :         /* No valid result - Treat this as meaning the feature is lacking; it could be an old Sybase version for example */
     298         214 :         if (!value_found) {
     299           0 :                 tdsdump_log(TDS_DBG_INFO1, "[DOL BULK] No valid result returned by probe.\n");
     300             :                 return TDS_SUCCESS;
     301             :         }
     302             : 
     303             :         /* Log and analyze result.
     304             :          * Sysstat2 flag values:
     305             :          *    https://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.help.ase.16.0/doc/html/title.html
     306             :          * 0x08000 - datarows locked
     307             :          * 0x20000 - clustered index present. (Not recommended for performance reasons to datarows-lock with clustered index) */
     308         214 :         tdsdump_log(TDS_DBG_INFO1, "%x = sysstat2 for '%s'", value, full_tablename);
     309             : 
     310         214 :         if (0x8000 & value) {
     311           6 :                 bcpinfo->datarows_locking = true;
     312           6 :                 tdsdump_log(TDS_DBG_INFO1, "Table has datarows-locking; enabling DOL BULK format.\n");
     313             : 
     314           6 :                 if (0x20000 & value)
     315           0 :                         tdsdump_log(TDS_DBG_WARN, "Table also has clustered index: bulk insert performance may be degraded.\n");
     316             :         }
     317             :         return TDS_SUCCESS;
     318             : }
     319             : 
     320             : /**
     321             :  * Help to build query to be sent to server.
     322             :  * Append column declaration to the query.
     323             :  * Only for TDS 7.0+.
     324             :  * \tds
     325             :  * \param[out] clause output string
     326             :  * \param bcpcol column to append
     327             :  * \param first  true if column is the first
     328             :  * \return TDS_SUCCESS or TDS_FAIL.
     329             :  */
     330             : static TDSRET
     331        2720 : tds7_build_bulk_insert_stmt(TDSSOCKET * tds, TDSPBCB * clause, TDSCOLUMN * bcpcol, int first)
     332             : {
     333             :         char column_type[128];
     334             : 
     335        2720 :         tdsdump_log(TDS_DBG_FUNC, "tds7_build_bulk_insert_stmt(%p, %p, %p, %d)\n", tds, clause, bcpcol, first);
     336             : 
     337        2720 :         if (TDS_FAILED(tds_get_column_declaration(tds, bcpcol, column_type))) {
     338           0 :                 tdserror(tds_get_ctx(tds), tds, TDSEBPROBADTYP, errno);
     339           0 :                 tdsdump_log(TDS_DBG_FUNC, "error: cannot build bulk insert statement. unrecognized server datatype %d\n",
     340             :                             bcpcol->on_server.column_type);
     341             :                 return TDS_FAIL;
     342             :         }
     343             : 
     344        2720 :         if (IS_TDS71_PLUS(tds->conn) && bcpcol->char_conv) {
     345        1014 :                 strcat(column_type, " COLLATE ");
     346        1014 :                 strcat(column_type, tds_canonical_collate_name(bcpcol->char_conv->to.charset.canonic));
     347             :         }
     348             : 
     349        5440 :         if (clause->cb < strlen(clause->pb)
     350        8160 :             + tds_quote_id(tds, NULL, tds_dstr_cstr(&bcpcol->column_name), tds_dstr_len(&bcpcol->column_name))
     351        2720 :             + strlen(column_type)
     352        2720 :             + ((first) ? 2u : 4u)) {
     353           0 :                 char *temp = tds_new(char, 2 * clause->cb);
     354             : 
     355           0 :                 if (!temp) {
     356           0 :                         tdserror(tds_get_ctx(tds), tds, TDSEMEM, errno);
     357           0 :                         return TDS_FAIL;
     358             :                 }
     359           0 :                 strcpy(temp, clause->pb);
     360           0 :                 if (clause->from_malloc)
     361           0 :                         free(clause->pb);
     362           0 :                 clause->from_malloc = true;
     363           0 :                 clause->pb = temp;
     364           0 :                 clause->cb *= 2;
     365             :         }
     366             : 
     367        2720 :         if (!first)
     368        1922 :                 strcat(clause->pb, ", ");
     369             : 
     370        8160 :         tds_quote_id(tds, strchr(clause->pb, 0), tds_dstr_cstr(&bcpcol->column_name), tds_dstr_len(&bcpcol->column_name));
     371        2720 :         strcat(clause->pb, " ");
     372        2720 :         strcat(clause->pb, column_type);
     373             : 
     374        2720 :         return TDS_SUCCESS;
     375             : }
     376             : 
     377             : static const char *
     378           2 : uc_str(const char *s)
     379             : {
     380           2 :         size_t n = strcspn(s, "abcdefghijklmnopqrstuvwxyz");
     381             : 
     382           2 :         if (s[n] == '\0')
     383             :                 return s;       /* already all uppercase */
     384             : 
     385           2 :         return tds_ascii_strupr(strdup(s));
     386             : }
     387             : 
     388             : /**
     389             :  * Prepare the query to be sent to server to request BCP information
     390             :  * \tds
     391             :  * \param bcpinfo BCP information
     392             :  */
     393             : static TDSRET
     394         984 : tds_bcp_start_insert_stmt(TDSSOCKET * tds, TDSBCPINFO * bcpinfo)
     395             : {
     396             :         char *query;
     397             : 
     398         984 :         if (IS_TDS7_PLUS(tds->conn)) {
     399             :                 int i, firstcol, erc;
     400             :                 char *hint;
     401             :                 TDSCOLUMN *bcpcol;
     402             :                 TDSPBCB colclause;
     403         798 :                 char clause_buffer[4096] = { 0 };
     404        1596 :                 bool triggers_checked = tds_dstr_isempty(&bcpinfo->hint);        /* Done on demand */
     405             : 
     406         798 :                 colclause.pb = clause_buffer;
     407         798 :                 colclause.cb = sizeof(clause_buffer);
     408         798 :                 colclause.from_malloc = false;
     409             : 
     410             :                 /* TODO avoid asprintf, use always malloc-ed buffer */
     411         798 :                 firstcol = 1;
     412             : 
     413        3518 :                 for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     414        2720 :                         bcpcol = bcpinfo->bindinfo->columns[i];
     415             : 
     416        2720 :                         if (bcpcol->column_timestamp)
     417           0 :                                 continue;
     418        2720 :                         if (!bcpinfo->identity_insert_on && bcpcol->column_identity)
     419           0 :                                 continue;
     420        2720 :                         if (bcpcol->column_computed) {
     421           2 :                                 if (!triggers_checked) {
     422           4 :                                         const char *uc_hint = uc_str(tds_dstr_cstr(&bcpinfo->hint));
     423             : 
     424           2 :                                         if (strstr(uc_hint, "FIRE_TRIGGERS"))
     425           2 :                                                 bcpinfo->with_triggers = true;
     426             : 
     427           4 :                                         if (uc_hint != tds_dstr_cstr(&bcpinfo->hint))
     428           2 :                                                 free((char *) uc_hint);
     429             : 
     430             :                                         triggers_checked = true;
     431             :                                 }
     432           2 :                                 if (!bcpinfo->with_triggers)
     433           0 :                                         continue;
     434             :                         }
     435        2720 :                         tds7_build_bulk_insert_stmt(tds, &colclause, bcpcol, firstcol);
     436        2720 :                         firstcol = 0;
     437             :                 }
     438             : 
     439        1596 :                 if (!tds_dstr_isempty(&bcpinfo->hint)) {
     440         188 :                         if (asprintf(&hint, " with (%s)", tds_dstr_cstr(&bcpinfo->hint)) < 0)
     441           0 :                                 hint = NULL;
     442             :                 } else {
     443         704 :                         hint = strdup("");
     444             :                 }
     445         798 :                 if (!hint) {
     446           0 :                         if (colclause.from_malloc)
     447           0 :                                 TDS_ZERO_FREE(colclause.pb);
     448           0 :                         return TDS_FAIL;
     449             :                 }
     450             : 
     451        1596 :                 erc = asprintf(&query, "insert bulk %s (%s)%s", tds_dstr_cstr(&bcpinfo->tablename), colclause.pb, hint);
     452             : 
     453         798 :                 free(hint);
     454         798 :                 if (colclause.from_malloc)
     455           0 :                         TDS_ZERO_FREE(colclause.pb);    /* just for good measure; not used beyond this point */
     456             : 
     457         798 :                 if (erc < 0)
     458             :                         return TDS_FAIL;
     459             :         } else {
     460             :                 /* NOTE: if we use "with nodescribe" for following inserts server do not send describe */
     461             :                 /* NOTE: "insert bulk" is an undocumented feature for MSSQL and ASE. */
     462         372 :                 if (asprintf(&query, "insert bulk %s", tds_dstr_cstr(&bcpinfo->tablename)) < 0)
     463             :                         return TDS_FAIL;
     464             :         }
     465             : 
     466             :         /* save the statement for later... */
     467         984 :         bcpinfo->insert_stmt = query;
     468             : 
     469         984 :         return TDS_SUCCESS;
     470             : }
     471             : 
     472             : /**
     473             :  * Check if a column is not sent to server.
     474             :  * Timestamp columns and identity columns (unless identity_insert is enabled)
     475             :  * are not sent.
     476             :  */
     477             : static bool
     478             : tds7_bcp_column_skipped(TDSBCPINFO *bcpinfo, TDSCOLUMN *bcpcol)
     479             : {
     480      227538 :         return (!bcpinfo->identity_insert_on && bcpcol->column_identity) || bcpcol->column_timestamp
     481      455076 :                 || (bcpcol->column_computed && !bcpinfo->with_triggers);
     482             : }
     483             : 
     484             : /**
     485             :  * Check if a column can be sent using tds_bcp_send_blob_data (TDS 7+).
     486             :  * Data are sent as they are so Unicode columns are not supported.
     487             :  */
     488             : static bool
     489             : tds7_bcp_column_deferrable(TDSCOLUMN *bcpcol)
     490             : {
     491         154 :         return is_blob_col(bcpcol) && !is_unicode_type(bcpcol->on_server.column_type);
     492             : }
     493             : 
     494             : /**
     495             :  * Send header of a column whose data will be sent using tds_bcp_send_blob_data.
     496             :  */
     497             : static void
     498         154 : tds7_put_deferred_header(TDSSOCKET *tds, TDSCOLUMN *bindcol)
     499             : {
     500         154 :         if (bindcol->column_varint_size == 8) {
     501             :                 /* PLP, length unknown, data sent in chunks */
     502          44 :                 tds_put_int8(tds, -2);
     503          44 :                 return;
     504             :         }
     505         110 :         if (type_has_textptr(bindcol->on_server.column_type)) {
     506             :                 static const unsigned char textptr[] = {
     507             :                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     508             :                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
     509             :                 };
     510         110 :                 tds_put_byte(tds, 16);
     511         110 :                 tds_put_n(tds, textptr, 16);
     512         110 :                 tds_put_n(tds, textptr, 8);
     513             :         }
     514         110 :         tds_put_int(tds, bindcol->bcp_column_data->datalen);
     515             : }
     516             : 
     517             : /**
     518             :  * Send columns of a row, starting from a given column.
     519             :  * Data must be already retrieved.
     520             :  * Stops at the first deferred column.
     521             :  */
     522             : static TDSRET
     523       50068 : tds7_send_columns(TDSSOCKET *tds, TDSBCPINFO *bcpinfo, int start_col)
     524             : {
     525             :         int i;
     526             : 
     527      160088 :         for (i = start_col; i < bcpinfo->bindinfo->num_cols; i++) {
     528             : 
     529             :                 TDS_INT save_size, save_col_size;
     530             :                 unsigned char *save_data;
     531             :                 TDSBLOB blob;
     532             :                 TDSCOLUMN *bindcol;
     533             :                 TDSRET rc;
     534             : 
     535      110174 :                 bindcol = bcpinfo->bindinfo->columns[i];
     536             : 
     537      220348 :                 if (tds7_bcp_column_skipped(bcpinfo, bindcol))
     538           0 :                         continue;
     539             : 
     540      110174 :                 if (bindcol->bcp_column_data->deferred) {
     541         154 :                         tds7_put_deferred_header(tds, bindcol);
     542         154 :                         bcpinfo->blob_pending = true;
     543         154 :                         bcpinfo->blob_col = i;
     544         154 :                         bcpinfo->blob_left = bindcol->bcp_column_data->datalen;
     545         308 :                         return TDS_SUCCESS;
     546             :                 }
     547             : 
     548      110020 :                 save_col_size = bindcol->column_size;
     549      110020 :                 save_size = bindcol->column_cur_size;
     550      110020 :                 save_data = bindcol->column_data;
     551      110020 :                 assert(bindcol->column_data == NULL);
     552      110020 :                 if (bindcol->bcp_column_data->is_null) {
     553        4742 :                         bindcol->column_cur_size = -1;
     554      105278 :                 } else if (is_blob_col(bindcol)) {
     555         136 :                         bindcol->column_cur_size = bindcol->bcp_column_data->datalen;
     556         136 :                         memset(&blob, 0, sizeof(blob));
     557         136 :                         blob.textvalue = (TDS_CHAR *) bindcol->bcp_column_data->data;
     558         136 :                         bindcol->column_data = (unsigned char *) &blob;
     559             :                 } else {
     560             :                         /* data are already in server format */
     561      105142 :                         bindcol->column_cur_size = TDS_MIN(bindcol->bcp_column_data->datalen, bindcol->on_server.column_size);
     562      105142 :                         bindcol->column_data = bindcol->bcp_column_data->data;
     563      105142 :                         bindcol->column_size = TDS_MAX(bindcol->column_size, bindcol->on_server.column_size);
     564             :                 }
     565      110020 :                 rc = bindcol->funcs->put_data(tds, bindcol, true);
     566      110020 :                 bindcol->column_size = save_col_size;
     567      110020 :                 bindcol->column_cur_size = save_size;
     568      110020 :                 bindcol->column_data = save_data;
     569             : 
     570      110020 :                 TDS_PROPAGATE(rc);
     571             :         }
     572             :         return TDS_SUCCESS;
     573             : }
     574             : 
     575             : static TDSRET
     576       50024 : tds7_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
     577             : {
     578             :         int i;
     579             : 
     580             :         /*
     581             :          * Retrieve all data before sending, a deferred column can stop
     582             :          * sending in the middle of the row.
     583             :          */
     584      160252 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     585      110324 :                 TDSCOLUMN *bindcol = bcpinfo->bindinfo->columns[i];
     586             :                 TDSRET rc;
     587             : 
     588      220648 :                 if (tds7_bcp_column_skipped(bcpinfo, bindcol))
     589           0 :                         continue;
     590             : 
     591      110324 :                 bindcol->bcp_column_data->deferred = false;
     592      110324 :                 rc = get_col_data(bcpinfo, bindcol, i, offset);
     593      110324 :                 if (TDS_FAILED(rc)) {
     594          96 :                         tdsdump_log(TDS_DBG_INFO1, "get_col_data (column %d) failed\n", i + 1);
     595             :                         return rc;
     596             :                 }
     597      110228 :                 tdsdump_log(TDS_DBG_INFO1, "gotten column %d length %d null %d deferred %d\n",
     598             :                             i + 1, bindcol->bcp_column_data->datalen, bindcol->bcp_column_data->is_null,
     599             :                             bindcol->bcp_column_data->deferred);
     600             : 
     601      110228 :                 if (bindcol->bcp_column_data->is_null) {
     602        4742 :                         if (!bindcol->column_nullable && !is_nullable_type(bindcol->on_server.column_type)) {
     603           0 :                                 if (null_error)
     604           0 :                                         null_error(bcpinfo, i, offset);
     605             :                                 return TDS_FAIL;
     606             :                         }
     607      105640 :                 } else if (bindcol->bcp_column_data->deferred && !tds7_bcp_column_deferrable(bindcol)) {
     608           0 :                         tdsdump_log(TDS_DBG_ERROR, "column %d cannot be deferred\n", i + 1);
     609             :                         return TDS_FAIL;
     610             :                 }
     611             :         }
     612             : 
     613       49928 :         tds_put_byte(tds, TDS_ROW_TOKEN);       /* 0xd1 */
     614       49928 :         return tds7_send_columns(tds, bcpinfo, 0);
     615             : }
     616             : 
     617             : /**
     618             :  * Send text/image columns of a row (TDS 5.0), starting from a given column.
     619             :  * Stops at the first deferred column.
     620             :  * \param get_col_data function to retrieve data, NULL if data are already retrieved
     621             :  */
     622             : static TDSRET
     623       12512 : tds5_send_blobs(TDSSOCKET *tds, TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, int offset, int start_col)
     624             : {
     625             :         int i;
     626             : 
     627       40546 :         for (i = start_col; i < bcpinfo->bindinfo->num_cols; i++) {
     628       28066 :                 TDSCOLUMN *bindcol = bcpinfo->bindinfo->columns[i];
     629             : 
     630       28066 :                 if (!type_has_textptr(bindcol->on_server.column_type))
     631       28012 :                         continue;
     632             : 
     633             :                 /* Elide trailing NULLs */
     634          54 :                 if (bindcol->bcp_column_data->is_null) {
     635             :                         int j;
     636             : 
     637          24 :                         for (j = i + 1; j < bcpinfo->bindinfo->num_cols; ++j) {
     638          14 :                                 TDSCOLUMN *bindcol2 = bcpinfo->bindinfo->columns[j];
     639             : 
     640          14 :                                 if (type_has_textptr(bindcol2->column_type) && !bindcol2->bcp_column_data->is_null)
     641             :                                         break;
     642             :                         }
     643          14 :                         if (j == bcpinfo->bindinfo->num_cols)
     644             :                                 break;
     645             :                 }
     646             : 
     647          44 :                 if (get_col_data)
     648          36 :                         TDS_PROPAGATE(tds5_get_col_data_or_dflt(get_col_data, bcpinfo, bindcol, offset, i));
     649             : 
     650             :                 /* unknown but zero */
     651          44 :                 tds_put_smallint(tds, 0);
     652          44 :                 TDS_PUT_BYTE(tds, bindcol->on_server.column_type);
     653          44 :                 tds_put_byte(tds, 0xff - bcpinfo->blob_count);
     654             :                 /*
     655             :                  * offset of txptr we stashed during variable
     656             :                  * column processing
     657             :                  */
     658          44 :                 tds_put_smallint(tds, bindcol->column_textpos);
     659          44 :                 tds_put_int(tds, bindcol->bcp_column_data->datalen);
     660          44 :                 if (bindcol->bcp_column_data->deferred) {
     661          22 :                         bcpinfo->blob_pending = true;
     662          22 :                         bcpinfo->blob_col = i;
     663          22 :                         bcpinfo->blob_left = bindcol->bcp_column_data->datalen;
     664          22 :                         return TDS_SUCCESS;
     665             :                 }
     666          22 :                 tds_put_n(tds, bindcol->bcp_column_data->data, bindcol->bcp_column_data->datalen);
     667          22 :                 bcpinfo->blob_count++;
     668             :         }
     669             :         return TDS_SUCCESS;
     670             : }
     671             : 
     672             : static TDSRET
     673       12518 : tds5_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
     674             : {
     675       12518 :         int row_pos = 0;
     676             :         int row_sz_pos;
     677             :         int var_cols_pos;
     678       12518 :         int var_cols_written = 0;
     679       12518 :         TDS_INT old_record_size = bcpinfo->bindinfo->row_size;
     680       12518 :         unsigned char *record = bcpinfo->bindinfo->current_row;
     681             : 
     682       12518 :         memset(record, '\0', old_record_size);  /* zero the rowbuffer */
     683             : 
     684             :         /* SAP ASE Datarows-locked tables expect additional 4 blank bytes before everything else */
     685       12518 :         if (bcpinfo->datarows_locking)
     686          44 :                 row_pos += 4;
     687             : 
     688             :         /*
     689             :          * offset 0 = number of var columns
     690             :          * offset 1 = row number.  zeroed (datasever assigns)
     691             :          */
     692       12518 :         var_cols_pos = row_pos;
     693       12518 :         row_pos += 2;
     694             : 
     695       12518 :         if ((row_pos = tds5_bcp_add_fixed_columns(bcpinfo, get_col_data, null_error, offset, record, row_pos)) < 0)
     696             :                 return TDS_FAIL;
     697             : 
     698       12512 :         row_sz_pos = row_pos;
     699             : 
     700             :         /* potential variable columns to write */
     701             : 
     702       12512 :         row_pos = tds5_bcp_add_variable_columns(bcpinfo, get_col_data, null_error, offset, record, row_pos, &var_cols_written);
     703       12512 :         if (row_pos < 0)
     704             :                 return TDS_FAIL;
     705             : 
     706             : 
     707       12492 :         if (var_cols_written) {
     708       12422 :                 TDS_PUT_UA2LE(&record[row_sz_pos], row_pos);
     709       12422 :                 record[var_cols_pos] = var_cols_written;
     710             :         }
     711             : 
     712       12492 :         tdsdump_log(TDS_DBG_INFO1, "old_record_size = %d new size = %d \n", old_record_size, row_pos);
     713             : 
     714       12492 :         tds_put_smallint(tds, row_pos);
     715       12492 :         tds_put_n(tds, record, row_pos);
     716             : 
     717             :         /* row is done, now handle any text/image data */
     718       12492 :         bcpinfo->blob_count = 0;
     719       12492 :         return tds5_send_blobs(tds, bcpinfo, get_col_data, offset, 0);
     720             : }
     721             : 
     722             : /**
     723             :  * Send one row of data to server
     724             :  * \tds
     725             :  * \param bcpinfo BCP information
     726             :  * \param get_col_data function to call to retrieve data to be sent
     727             :  * \param ignored function to call if we try to send NULL if not allowed (not used)
     728             :  * \param offset passed to get_col_data and null_error to specify the row to get
     729             :  * \return TDS_SUCCESS or TDS_FAIL.
     730             :  */
     731             : TDSRET
     732       62542 : tds_bcp_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo,
     733             :                     tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
     734             : {
     735             :         TDSRET rc;
     736             :         TDSFREEZE row;
     737             : 
     738       62542 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_send_bcp_record(%p, %p, %p, %p, %d)\n", tds, bcpinfo, get_col_data, null_error, offset);
     739             : 
     740             :         /* previous row not completed */
     741       62542 :         if (bcpinfo->blob_pending)
     742             :                 return TDS_FAIL;
     743             : 
     744       62542 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
     745             :                 return TDS_FAIL;
     746             : 
     747       62542 :         tds_freeze(tds, &row, 0);
     748       62542 :         if (IS_TDS7_PLUS(tds->conn))
     749       50024 :                 rc = tds7_send_record(tds, bcpinfo, get_col_data, null_error, offset);
     750             :         else
     751       12518 :                 rc = tds5_send_record(tds, bcpinfo, get_col_data, null_error, offset);
     752             : 
     753       62542 :         if (TDS_SUCCEED(rc)) {
     754             :                 /* row will be completed by tds_bcp_send_blob_data */
     755       62420 :                 if (!bcpinfo->blob_pending)
     756       62308 :                         bcpinfo->rows_sent++;
     757       62420 :                 tds_freeze_close(&row);
     758             :         } else {
     759         122 :                 bcpinfo->blob_pending = false;
     760         122 :                 tds_freeze_abort(&row);
     761             :         }
     762             : 
     763       62542 :         tds_set_state(tds, TDS_SENDING);
     764       62542 :         return rc;
     765             : }
     766             : 
     767             : /**
     768             :  * Send data for a deferred blob column.
     769             :  * tds_bcp_send_record stops sending a row at the first deferred column
     770             :  * (see BCPCOLDATA::deferred), data for that column are then sent with
     771             :  * this function, possibly in multiple chunks. When all data for the
     772             :  * column are sent the rest of the row is sent, stopping at the next
     773             :  * deferred column, if any.
     774             :  * Data are sent as they are, they must be already in server format.
     775             :  * \tds
     776             :  * \param bcpinfo BCP information
     777             :  * \param data    data to send
     778             :  * \param len     length of data, cannot exceed data still to be sent
     779             :  *                for the column (bcpinfo->blob_left)
     780             :  * \return TDS_SUCCESS or TDS_FAIL.
     781             :  */
     782             : TDSRET
     783         576 : tds_bcp_send_blob_data(TDSSOCKET *tds, TDSBCPINFO *bcpinfo, const void *data, TDS_INT len)
     784             : {
     785             :         TDSCOLUMN *bindcol;
     786         576 :         TDSRET rc = TDS_SUCCESS;
     787             : 
     788         576 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_send_blob_data(%p, %p, %p, %d)\n", tds, bcpinfo, data, len);
     789             : 
     790         576 :         if (!bcpinfo->blob_pending || len < 0 || len > bcpinfo->blob_left || (len > 0 && !data))
     791             :                 return TDS_FAIL;
     792             : 
     793         576 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
     794             :                 return TDS_FAIL;
     795             : 
     796         576 :         bindcol = bcpinfo->bindinfo->columns[bcpinfo->blob_col];
     797         576 :         if (len > 0) {
     798             :                 /* PLP chunk */
     799         576 :                 if (IS_TDS7_PLUS(tds->conn) && bindcol->column_varint_size == 8)
     800         144 :                         tds_put_int(tds, len);
     801         576 :                 tds_put_n(tds, data, len);
     802         576 :                 bcpinfo->blob_left -= len;
     803             :         }
     804             : 
     805         576 :         if (bcpinfo->blob_left == 0) {
     806             :                 /* column completed, send the rest of the row */
     807         160 :                 bcpinfo->blob_pending = false;
     808         160 :                 if (IS_TDS7_PLUS(tds->conn)) {
     809             :                         /* PLP terminator */
     810         140 :                         if (bindcol->column_varint_size == 8)
     811          40 :                                 tds_put_int(tds, 0);
     812         140 :                         rc = tds7_send_columns(tds, bcpinfo, bcpinfo->blob_col + 1);
     813             :                 } else {
     814          20 :                         bcpinfo->blob_count++;
     815          20 :                         rc = tds5_send_blobs(tds, bcpinfo, NULL, 0, bcpinfo->blob_col + 1);
     816             :                 }
     817         160 :                 if (TDS_SUCCEED(rc) && !bcpinfo->blob_pending)
     818          96 :                         bcpinfo->rows_sent++;
     819             :         }
     820             : 
     821         576 :         tds_set_state(tds, TDS_SENDING);
     822         576 :         return rc;
     823             : }
     824             : 
     825             : static inline void
     826             : tds5_swap_data(const TDSCOLUMN *col TDS_UNUSED, void *p TDS_UNUSED)
     827             : {
     828             : #ifdef WORDS_BIGENDIAN
     829             :         tds_swap_datatype(tds_get_conversion_type(col->on_server.column_type, col->column_size), p);
     830             : #endif
     831             : }
     832             : 
     833             : /**
     834             :  * Add fixed size columns to the row
     835             :  * \param bcpinfo BCP information
     836             :  * \param get_col_data function to call to retrieve data to be sent
     837             :  * \param ignored function to call if we try to send NULL if not allowed (not used)
     838             :  * \param offset passed to get_col_data and null_error to specify the row to get
     839             :  * \param rowbuffer row buffer to write to
     840             :  * \param start row buffer last end position
     841             :  * \returns new row length or -1 on error.
     842             :  */
     843             : static int
     844       12518 : tds5_bcp_add_fixed_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
     845             :                            int offset, unsigned char * rowbuffer, int start)
     846             : {
     847             :         TDS_NUMERIC *num;
     848       12518 :         int row_pos = start;
     849             :         int cpbytes;
     850             :         int i;
     851       12518 :         int bitleft = 0, bitpos = 0;
     852             : 
     853       12518 :         assert(bcpinfo);
     854       12518 :         assert(rowbuffer);
     855             : 
     856       12518 :         tdsdump_log(TDS_DBG_FUNC, "tds5_bcp_add_fixed_columns(%p, %p, %p, %d, %p, %d)\n",
     857             :                     bcpinfo, get_col_data, null_error, offset, rowbuffer, start);
     858             : 
     859       28112 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     860             : 
     861       28118 :                 TDSCOLUMN *const bcpcol = bcpinfo->bindinfo->columns[i];
     862       28118 :                 const TDS_INT column_size = bcpcol->on_server.column_size;
     863             : 
     864             :                 /* if possible check information from server */
     865       28118 :                 if (bcpinfo->sybase_count > i) {
     866       28118 :                         if (bcpinfo->sybase_colinfo[i].offset < 0)
     867       14350 :                                 continue;
     868             :                 } else {
     869           0 :                         if (is_nullable_type(bcpcol->on_server.column_type) || bcpcol->column_nullable)
     870           0 :                                 continue;
     871             :                 }
     872             : 
     873       13768 :                 tdsdump_log(TDS_DBG_FUNC, "tds5_bcp_add_fixed_columns column %d (%s) is a fixed column\n", i + 1,
     874             :                             tds_dstr_cstr(&bcpcol->column_name));
     875             : 
     876       13768 :                 if (TDS_FAILED(tds5_get_col_data_or_dflt(get_col_data, bcpinfo, bcpcol, offset, i))) {
     877           6 :                         tdsdump_log(TDS_DBG_INFO1, "get_col_data (column %d) failed\n", i + 1);
     878             :                         return -1;
     879             :                 }
     880             : 
     881             :                 /* We have no way to send a NULL at this point, return error to client */
     882       13762 :                 if (bcpcol->bcp_column_data->is_null) {
     883           0 :                         tdsdump_log(TDS_DBG_ERROR, "tds5_bcp_add_fixed_columns column %d is a null column\n", i + 1);
     884             :                         /* No value or default value available and NULL not allowed. */
     885           0 :                         if (null_error)
     886           0 :                                 null_error(bcpinfo, i, offset);
     887             :                         return -1;
     888             :                 }
     889             : 
     890       13762 :                 if (is_numeric_type(bcpcol->on_server.column_type)) {
     891         220 :                         num = (TDS_NUMERIC *) bcpcol->bcp_column_data->data;
     892         220 :                         cpbytes = tds_numeric_bytes_per_prec[num->precision];
     893         220 :                         memcpy(&rowbuffer[row_pos], num->array, cpbytes);
     894       13542 :                 } else if (bcpcol->column_type == SYBBIT) {
     895             :                         /* all bit are collapsed together */
     896         178 :                         if (!bitleft) {
     897         126 :                                 bitpos = row_pos++;
     898         126 :                                 bitleft = 8;
     899         126 :                                 rowbuffer[bitpos] = 0;
     900             :                         }
     901         178 :                         if (bcpcol->bcp_column_data->data[0])
     902         146 :                                 rowbuffer[bitpos] |= 256 >> bitleft;
     903         178 :                         --bitleft;
     904         178 :                         continue;
     905             :                 } else {
     906       13364 :                         cpbytes = bcpcol->bcp_column_data->datalen > column_size ?
     907             :                                   column_size : bcpcol->bcp_column_data->datalen;
     908       13364 :                         memcpy(&rowbuffer[row_pos], bcpcol->bcp_column_data->data, cpbytes);
     909       13364 :                         tds5_swap_data(bcpcol, &rowbuffer[row_pos]);
     910             : 
     911             :                         /* CHAR data may need padding out to the database length with blanks */
     912             :                         /* TODO check binary !!! */
     913       13364 :                         if (bcpcol->column_type == SYBCHAR && cpbytes < column_size)
     914         120 :                                 memset(rowbuffer + row_pos + cpbytes, ' ', column_size - cpbytes);
     915             :                 }
     916             : 
     917       13584 :                 row_pos += column_size;
     918             :         }
     919             :         return row_pos;
     920             : }
     921             : 
     922             : /**
     923             :  * Add variable size columns to the row
     924             :  *
     925             :  * \param bcpinfo BCP information already prepared
     926             :  * \param get_col_data function to call to retrieve data to be sent
     927             :  * \param null_error function to call if we try to send NULL if not allowed
     928             :  * \param offset passed to get_col_data and null_error to specify the row to get
     929             :  * \param rowbuffer The row image that will be sent to the server. 
     930             :  * \param start Where to begin copying data into the rowbuffer. 
     931             :  * \param pncols Address of output variable holding the count of columns added to the rowbuffer.  
     932             :  * 
     933             :  * \return length of (potentially modified) rowbuffer, or -1.
     934             :  */
     935             : static int
     936       12512 : tds5_bcp_add_variable_columns(TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error,
     937             :                               int offset, TDS_UCHAR* rowbuffer, int start, int *pncols)
     938             : {
     939             :         TDS_USMALLINT offsets[256];
     940             :         unsigned int i, row_pos;
     941       12512 :         unsigned int ncols = 0;
     942             : 
     943       12512 :         assert(bcpinfo);
     944       12512 :         assert(rowbuffer);
     945       12512 :         assert(pncols);
     946             : 
     947       12512 :         tdsdump_log(TDS_DBG_FUNC, "%4s %8s %18s %18s %8s\n",  "col", 
     948             :                                                                 "type", 
     949             :                                                                 "is_nullable_type", 
     950             :                                                                 "column_nullable", 
     951             :                                                                 "is null" );
     952       28110 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     953       28110 :                 TDSCOLUMN *bcpcol = bcpinfo->bindinfo->columns[i];
     954       28110 :                 tdsdump_log(TDS_DBG_FUNC, "%4d %8d %18s %18s %8s\n",  i,
     955             :                                                                         bcpcol->on_server.column_type,
     956             :                                                                         is_nullable_type(bcpcol->on_server.column_type)? "yes" : "no",
     957             :                                                                         bcpcol->column_nullable? "yes" : "no",
     958             :                                                                         bcpcol->bcp_column_data->is_null? "yes" : "no" );
     959             :         }
     960             : 
     961             :         /* the first two bytes of the rowbuffer are reserved to hold the entire record length */
     962       12512 :         row_pos = start + 2;
     963       12512 :         offsets[0] = row_pos;
     964             : 
     965       12512 :         tdsdump_log(TDS_DBG_FUNC, "%4s %8s %8s %8s\n", "col", "ncols", "row_pos", "cpbytes");
     966             : 
     967       28088 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
     968       28108 :                 unsigned int cpbytes = 0;
     969       28108 :                 TDSCOLUMN *bcpcol = bcpinfo->bindinfo->columns[i];
     970             : 
     971             :                 /*
     972             :                  * Is this column of "variable" type, i.e. NULLable
     973             :                  * or naturally variable length e.g. VARCHAR
     974             :                  */
     975       28108 :                 if (bcpinfo->sybase_count > (TDS_INT) i) {
     976       28108 :                         if (bcpinfo->sybase_colinfo[i].offset >= 0)
     977       13760 :                                 continue;
     978             :                 } else {
     979           0 :                         if (!is_nullable_type(bcpcol->on_server.column_type) && !bcpcol->column_nullable)
     980           0 :                                 continue;
     981             :                 }
     982             : 
     983       14348 :                 tdsdump_log(TDS_DBG_FUNC, "%4d %8d %8d %8d\n", i, ncols, row_pos, cpbytes);
     984             : 
     985       14348 :                 if (TDS_FAILED(tds5_get_col_data_or_dflt(get_col_data, bcpinfo, bcpcol, offset, i)))
     986             :                         return -1;
     987             : 
     988             :                 /* If it's a NOT NULL column, and we have no data, throw an error.
     989             :                  * This is the behavior for Sybase, this function is only used for Sybase */
     990       14328 :                 if (!bcpcol->column_nullable && bcpcol->bcp_column_data->is_null) {
     991             :                         /* No value or default value available and NULL not allowed. */
     992           0 :                         if (null_error)
     993           0 :                                 null_error(bcpinfo, i, offset);
     994             :                         return -1;
     995             :                 }
     996             : 
     997             :                 /* move the column buffer into the rowbuffer */
     998       14328 :                 if (!bcpcol->bcp_column_data->is_null) {
     999       13206 :                         if (type_has_textptr(bcpcol->on_server.column_type)) {
    1000          40 :                                 cpbytes = 16;
    1001          40 :                                 bcpcol->column_textpos = row_pos;               /* save for data write */
    1002       13166 :                         } else if (is_numeric_type(bcpcol->on_server.column_type)) {
    1003          70 :                                 TDS_NUMERIC *num = (TDS_NUMERIC *) bcpcol->bcp_column_data->data;
    1004          70 :                                 cpbytes = tds_numeric_bytes_per_prec[num->precision];
    1005          70 :                                 memcpy(&rowbuffer[row_pos], num->array, cpbytes);
    1006       13096 :                         } else if ((bcpcol->column_type == SYBVARCHAR || bcpcol->column_type == SYBCHAR)
    1007       12500 :                                    && bcpcol->bcp_column_data->datalen == 0) {
    1008          20 :                                 cpbytes = 1;
    1009          20 :                                 rowbuffer[row_pos] = ' ';
    1010             :                         } else {
    1011       26152 :                                 cpbytes = bcpcol->bcp_column_data->datalen > bcpcol->column_size ?
    1012       13076 :                                         bcpcol->column_size : bcpcol->bcp_column_data->datalen;
    1013             :                                 /*
    1014             :                                  * Sybase stores variable binary data without trailing
    1015             :                                  * zeroes (keeping at least a byte), do the same as
    1016             :                                  * a normal insert.
    1017             :                                  */
    1018       13076 :                                 if (is_binary_type(bcpcol->on_server.column_type))
    1019         616 :                                         while (cpbytes > 1 && bcpcol->bcp_column_data->data[cpbytes - 1] == 0)
    1020             :                                                 --cpbytes;
    1021       13076 :                                 memcpy(&rowbuffer[row_pos], bcpcol->bcp_column_data->data, cpbytes);
    1022       13076 :                                 tds5_swap_data(bcpcol, &rowbuffer[row_pos]);
    1023             :                         }
    1024        1122 :                 } else if (type_has_textptr(bcpcol->column_type)) {
    1025          18 :                         bcpcol->column_textpos = row_pos;
    1026             :                 }
    1027             : 
    1028       14328 :                 row_pos += cpbytes;
    1029       14328 :                 offsets[++ncols] = row_pos;
    1030       14328 :                 tdsdump_dump_buf(TDS_DBG_NETWORK, "BCP row buffer so far", rowbuffer,  row_pos);
    1031             :         }
    1032             : 
    1033       12492 :         tdsdump_log(TDS_DBG_FUNC, "%4d %8d %8d\n", i, ncols, row_pos);
    1034             : 
    1035             :         /*
    1036             :          * The rowbuffer ends with an offset table and, optionally, an adjustment table.  
    1037             :          * The offset table has 1-byte elements that describe the locations of the start of each column in
    1038             :          * the rowbuffer.  If the largest offset is greater than 255, another table -- the adjustment table --
    1039             :          * is inserted just before the offset table.  It holds the high bytes. 
    1040             :          * 
    1041             :          * Both tables are laid out in reverse:
    1042             :          *      #elements, offset N+1, offset N, offset N-1, ... offset 0
    1043             :          * E.g. for 2 columns you have 4 data points:
    1044             :          *      1.  How many elements (4)
    1045             :          *      2.  Start of column 3 (non-existent, "one off the end")
    1046             :          *      3.  Start of column 2
    1047             :          *      4.  Start of column 1
    1048             :          *  The length of each column is computed by subtracting its start from the its successor's start. 
    1049             :          *
    1050             :          * The algorithm below computes both tables. If the adjustment table isn't needed, the 
    1051             :          * effect is to overwrite it with the offset table.  
    1052             :          */
    1053       13602 :         while (ncols && offsets[ncols] == offsets[ncols-1])
    1054             :                 ncols--;        /* trailing NULL columns are not sent and are not included in the offset table */
    1055             : 
    1056       24874 :         if (ncols && !bcpinfo->datarows_locking) {
    1057       12382 :                 TDS_UCHAR *poff = rowbuffer + row_pos;
    1058       12382 :                 unsigned int pfx_top = offsets[ncols] / 256u;
    1059             : 
    1060       12382 :                 tdsdump_log(TDS_DBG_FUNC, "ncols=%u poff=%p [%u]\n", ncols, poff, offsets[ncols]);
    1061             : 
    1062       12382 :                 if (offsets[ncols] / 256u == offsets[ncols - 1] / 256u)
    1063       12380 :                         *poff++ = ncols + 1;
    1064             :                 /* this is some kind of run-length-prefix encoding */
    1065       12392 :                 while (pfx_top) {
    1066             :                         unsigned int n_pfx = 1;
    1067             : 
    1068         142 :                         for (i = 0; i <= ncols; ++i)
    1069         142 :                                 if (offsets[i] / 256u < pfx_top)
    1070          90 :                                         ++n_pfx;
    1071          10 :                         *poff++ = n_pfx;
    1072          10 :                         --pfx_top;
    1073             :                 }
    1074             :    
    1075       12382 :                 tdsdump_log(TDS_DBG_FUNC, "poff=%p\n", poff);
    1076             : 
    1077       25478 :                 for (i=0; i <= ncols; i++)
    1078       25478 :                         *poff++ = offsets[ncols-i] & 0xFF;
    1079       12382 :                 row_pos = (unsigned int)(poff - rowbuffer);
    1080             :         } else {                /* Datarows-locking */
    1081             :                 unsigned int col;
    1082             : 
    1083         230 :                 for (col = ncols; col-- > 0;) {
    1084             :                         /* The DOL BULK format has a much simpler row table -- it's just a 2-byte length for every
    1085             :                          * non-fixed column (does not have the extra "offset after the end" that the basic format has) */
    1086         120 :                         rowbuffer[row_pos++] = offsets[col] % 256u;
    1087         120 :                         rowbuffer[row_pos++] = offsets[col] / 256u;
    1088             : 
    1089         120 :                         tdsdump_log(TDS_DBG_FUNC, "[DOL BULK offset table] col=%u offset=%u\n", col, offsets[col]);
    1090             :                 }
    1091             :         }
    1092             : 
    1093       12492 :         tdsdump_log(TDS_DBG_FUNC, "%4d %8d %8d\n", i, ncols, row_pos);
    1094       12492 :         tdsdump_dump_buf(TDS_DBG_NETWORK, "BCP row buffer", rowbuffer,  row_pos);
    1095             : 
    1096       12492 :         *pncols = ncols;
    1097             : 
    1098       12492 :         return ncols == 0? start : row_pos;
    1099             : }
    1100             : 
    1101             : /**
    1102             :  * Send BCP metadata to server.
    1103             :  * Only for TDS 7.0+.
    1104             :  * \tds
    1105             :  * \param bcpinfo BCP information
    1106             :  * \return TDS_SUCCESS or TDS_FAIL.
    1107             :  */
    1108             : static TDSRET
    1109         856 : tds7_bcp_send_colmetadata(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
    1110             : {
    1111             :         TDSCOLUMN *bcpcol;
    1112             :         int i, num_cols;
    1113             : 
    1114         856 :         tdsdump_log(TDS_DBG_FUNC, "tds7_bcp_send_colmetadata(%p, %p)\n", tds, bcpinfo);
    1115         856 :         assert(tds && bcpinfo);
    1116             : 
    1117         856 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1118             :                 return TDS_FAIL;
    1119             : 
    1120             :         /* 
    1121             :          * Deep joy! For TDS 7 we have to send a colmetadata message followed by row data
    1122             :          */
    1123         856 :         tds_put_byte(tds, TDS7_RESULT_TOKEN);   /* 0x81 */
    1124             : 
    1125         856 :         num_cols = 0;
    1126        4376 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
    1127        3520 :                 bcpcol = bcpinfo->bindinfo->columns[i];
    1128        7040 :                 if (tds7_bcp_column_skipped(bcpinfo, bcpcol))
    1129           0 :                         continue;
    1130        3520 :                 num_cols++;
    1131             :         }
    1132             : 
    1133         856 :         tds_put_smallint(tds, num_cols);
    1134             : 
    1135        4376 :         for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
    1136             :                 size_t converted_len;
    1137             :                 const char *converted_name;
    1138             : 
    1139        3520 :                 bcpcol = bcpinfo->bindinfo->columns[i];
    1140             : 
    1141        7040 :                 if (tds7_bcp_column_skipped(bcpinfo, bcpcol))
    1142           0 :                         continue;
    1143             : 
    1144        3520 :                 if (IS_TDS72_PLUS(tds->conn))
    1145        1848 :                         tds_put_int(tds, bcpcol->column_usertype);
    1146             :                 else
    1147        1672 :                         tds_put_smallint(tds, bcpcol->column_usertype);
    1148        3520 :                 tds_put_smallint(tds, bcpcol->column_flags);
    1149        3520 :                 TDS_PUT_BYTE(tds, bcpcol->on_server.column_type);
    1150             : 
    1151        3520 :                 assert(bcpcol->funcs);
    1152        3520 :                 bcpcol->funcs->put_info(tds, bcpcol);
    1153             : 
    1154             :                 /* TODO put this in put_info. It seems that parameter format is
    1155             :                  * different from BCP format
    1156             :                  */
    1157        3520 :                 if (type_has_textptr(bcpcol->on_server.column_type)) {
    1158         480 :                         converted_name = tds_convert_string(tds, tds->conn->char_convs[client2ucs2],
    1159         160 :                                                             tds_dstr_cstr(&bcpinfo->tablename),
    1160         320 :                                                             (int) tds_dstr_len(&bcpinfo->tablename), &converted_len);
    1161         160 :                         if (!converted_name) {
    1162           0 :                                 tds_connection_close(tds->conn);
    1163           0 :                                 return TDS_FAIL;
    1164             :                         }
    1165             : 
    1166             :                         /* UTF-16 length is always size / 2 even for 4 byte letters (yes, 1 letter of length 2) */
    1167         160 :                         TDS_PUT_SMALLINT(tds, converted_len / 2);
    1168         160 :                         tds_put_n(tds, converted_name, converted_len);
    1169             : 
    1170         320 :                         tds_convert_string_free(tds_dstr_cstr(&bcpinfo->tablename), converted_name);
    1171             :                 }
    1172             : 
    1173       10560 :                 converted_name = tds_convert_string(tds, tds->conn->char_convs[client2ucs2],
    1174        3520 :                                                     tds_dstr_cstr(&bcpcol->column_name),
    1175        7040 :                                                     (int) tds_dstr_len(&bcpcol->column_name), &converted_len);
    1176        3520 :                 if (!converted_name) {
    1177           0 :                         tds_connection_close(tds->conn);
    1178           0 :                         return TDS_FAIL;
    1179             :                 }
    1180             : 
    1181             :                 /* UTF-16 length is always size / 2 even for 4 byte letters (yes, 1 letter of length 2) */
    1182        3520 :                 TDS_PUT_BYTE(tds, converted_len / 2);
    1183        3520 :                 tds_put_n(tds, converted_name, converted_len);
    1184             : 
    1185        7040 :                 tds_convert_string_free(tds_dstr_cstr(&bcpcol->column_name), converted_name);
    1186             :         }
    1187             : 
    1188         856 :         tds_set_state(tds, TDS_SENDING);
    1189         856 :         return TDS_SUCCESS;
    1190             : }
    1191             : 
    1192             : /**
    1193             :  * Tell we finished sending BCP data to server
    1194             :  * \tds
    1195             :  * \param[out] rows_copied number of rows copied to server
    1196             :  */
    1197             : TDSRET
    1198        1118 : tds_bcp_done(TDSSOCKET *tds, int *rows_copied)
    1199             : {
    1200        1118 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_done(%p, %p)\n", tds, rows_copied);
    1201             : 
    1202        1118 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1203             :                 return TDS_FAIL;
    1204             : 
    1205         988 :         tds_flush_packet(tds);
    1206             : 
    1207         988 :         tds_set_state(tds, TDS_PENDING);
    1208             : 
    1209         988 :         TDS_PROPAGATE(tds_process_simple_query(tds));
    1210             : 
    1211         980 :         if (rows_copied)
    1212         980 :                 *rows_copied = tds->rows_affected;
    1213             : 
    1214             :         return TDS_SUCCESS;
    1215             : }
    1216             : 
    1217             : /**
    1218             :  * Cancel sending BCP data to server.
    1219             :  * Data not already sent are discarded, data already sent in the
    1220             :  * current batch are not inserted.
    1221             :  * \tds
    1222             :  */
    1223             : TDSRET
    1224          66 : tds_bcp_cancel(TDSSOCKET *tds)
    1225             : {
    1226             :         TDSRET rc;
    1227             : 
    1228          66 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_cancel(%p)\n", tds);
    1229             : 
    1230          66 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1231             :                 return TDS_FAIL;
    1232             : 
    1233             :         /* discard data not sent yet */
    1234          66 :         tds_init_write_buf(tds);
    1235             : 
    1236             :         /*
    1237             :          * Packets could be already sent, for MSSQL terminate the
    1238             :          * message with the ignore flag so the server discards it.
    1239             :          * Sybase accepts the cancel request in the middle of a message.
    1240             :          */
    1241          66 :         rc = TDS_SUCCESS;
    1242          66 :         if (IS_TDS7_PLUS(tds->conn))
    1243          54 :                 rc = tds_write_packet(tds, TDS_STATUS_EOM | TDS_STATUS_IGNORE);
    1244          66 :         tds_set_state(tds, TDS_PENDING);
    1245          66 :         TDS_PROPAGATE(rc);
    1246             : 
    1247          66 :         TDS_PROPAGATE(tds_send_cancel(tds));
    1248          66 :         return tds_process_cancel(tds);
    1249             : }
    1250             : 
    1251             : /**
    1252             :  * Start sending BCP data to server.
    1253             :  * Initialize stream to accept data.
    1254             :  * \tds
    1255             :  * \param bcpinfo BCP information already prepared
    1256             :  */
    1257             : TDSRET
    1258        1054 : tds_bcp_start(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
    1259             : {
    1260             :         TDSRET rc;
    1261             : 
    1262        1054 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_start(%p, %p)\n", tds, bcpinfo);
    1263             : 
    1264        1054 :         if (!IS_TDS50_PLUS(tds->conn))
    1265             :                 return TDS_FAIL;
    1266             : 
    1267        1054 :         bcpinfo->blob_pending = false;
    1268             : 
    1269        1054 :         TDS_PROPAGATE(tds_submit_query(tds, bcpinfo->insert_stmt));
    1270             : 
    1271             :         /* set we want to switch to bulk state */
    1272        1054 :         tds->bulk_query = true;
    1273             : 
    1274             :         /*
    1275             :          * In TDS 5 we get the column information as a result set from the "insert bulk" command.
    1276             :          */
    1277        1054 :         if (IS_TDS50(tds->conn))
    1278         198 :                 rc = tds5_process_insert_bulk_reply(tds, bcpinfo);
    1279             :         else
    1280         856 :                 rc = tds_process_simple_query(tds);
    1281        1054 :         TDS_PROPAGATE(rc);
    1282             : 
    1283        1054 :         tds->out_flag = TDS_BULK;
    1284        1054 :         if (tds_set_state(tds, TDS_SENDING) != TDS_SENDING)
    1285             :                 return TDS_FAIL;
    1286             : 
    1287        1054 :         if (IS_TDS7_PLUS(tds->conn))
    1288         856 :                 tds7_bcp_send_colmetadata(tds, bcpinfo);
    1289             :         
    1290             :         return TDS_SUCCESS;
    1291             : }
    1292             : 
    1293             : enum {
    1294             :         /* list of columns we need, 0-nnn */
    1295             :         BULKCOL_colcnt,
    1296             :         BULKCOL_colid,
    1297             :         BULKCOL_type,
    1298             :         BULKCOL_length,
    1299             :         BULKCOL_status,
    1300             :         BULKCOL_offset,
    1301             :         BULKCOL_dflt,
    1302             : 
    1303             :         /* number of columns needed */
    1304             :         BULKCOL_COUNT,
    1305             : 
    1306             :         /* bitmask to have them all */
    1307             :         BULKCOL_ALL = (1 << BULKCOL_COUNT) -1,
    1308             : };
    1309             : 
    1310             : static int
    1311        2448 : tds5_bulk_insert_column(const char *name)
    1312             : {
    1313             : #define BULKCOL(n) do {\
    1314             :         if (strcmp(name, #n) == 0) \
    1315             :                 return BULKCOL_ ## n; \
    1316             : } while(0)
    1317             : 
    1318        2448 :         switch (name[0]) {
    1319         396 :         case 'c':
    1320         396 :                 BULKCOL(colcnt);
    1321         198 :                 BULKCOL(colid);
    1322             :                 break;
    1323         198 :         case 'd':
    1324         198 :                 BULKCOL(dflt);
    1325             :                 break;
    1326         198 :         case 't':
    1327         198 :                 BULKCOL(type);
    1328             :                 break;
    1329         198 :         case 'l':
    1330         198 :                 BULKCOL(length);
    1331             :                 break;
    1332         396 :         case 's':
    1333         396 :                 BULKCOL(status);
    1334             :                 break;
    1335         198 :         case 'o':
    1336         198 :                 BULKCOL(offset);
    1337             :                 break;
    1338             :         }
    1339             : #undef BULKCOL
    1340        1062 :         return -1;
    1341             : }
    1342             : 
    1343             : static void
    1344          12 : tds5_read_bulk_defaults(TDSSOCKET *tds TDS_UNUSED, TDSRESULTINFO *res_info, TDSBCPINFO *bcpinfo)
    1345             : {
    1346             :         int i;
    1347          12 :         TDS5COLINFO *syb_info = bcpinfo->sybase_colinfo;
    1348          12 :         TDS5COLINFO *const syb_info_end = syb_info + bcpinfo->sybase_count;
    1349             : 
    1350             :         /* testing of behaviour of --ignoredefaults in ASE BCP client appears
    1351             :          * to show it behaves exactly as if defaults did not exist. For example
    1352             :          * a null value or omitted value for a default non-null column will
    1353             :          * give an error, instead of using the default. */
    1354          26 :         if (!tds_dstr_isempty(&bcpinfo->hint) && strstr(tds_dstr_cstr(&bcpinfo->hint), "KEEP_NULLS"))
    1355             :                 return;
    1356             : 
    1357          10 :         tdsdump_log(TDS_DBG_INFO1, "Reading default value row.\n");
    1358             : 
    1359          66 :         for (i = 0; i < res_info->num_cols; ++i, ++syb_info) {
    1360          66 :                 TDSCOLUMN *col = res_info->columns[i];
    1361          66 :                 TDS_UCHAR *src = col->column_data;
    1362          66 :                 TDS_INT len = col->column_cur_size;
    1363             : 
    1364          66 :                 if (type_has_textptr(col->column_type))
    1365           0 :                         src = (unsigned char *) ((TDSBLOB *) src)->textvalue;
    1366             : 
    1367             :                 /* find next column having a default */
    1368             :                 for (;;) {
    1369             :                         /* avoid overflows */
    1370         130 :                         if (syb_info >= syb_info_end)
    1371             :                                 return;
    1372             : 
    1373         130 :                         if (syb_info->dflt)
    1374             :                                 break;
    1375          64 :                         ++syb_info;
    1376             :                 }
    1377             : 
    1378             : #if defined(TDS_DEBUG_DATA)
    1379             :                 if (TDS_UNLIKELY(tds_write_dump))
    1380             :                         tdsdump_col(tds_get_ctx(tds), col);
    1381             : #endif
    1382             : 
    1383          66 :                 syb_info->dflt_size = len;
    1384          66 :                 if (TDS_RESIZE(syb_info->dflt_value, len))
    1385          66 :                         memcpy(syb_info->dflt_value, src, len);
    1386             :         }
    1387             : }
    1388             : 
    1389             : static TDSRET
    1390         198 : tds5_process_insert_bulk_reply(TDSSOCKET * tds, TDSBCPINFO *bcpinfo)
    1391             : {
    1392             :         TDS_INT res_type;
    1393             :         TDS_INT done_flags;
    1394             :         TDSRET  rc;
    1395         198 :         TDSRET  ret = TDS_SUCCESS;
    1396         198 :         bool row_match = false;
    1397             :         TDSRESULTINFO *res_info;
    1398             :         int icol;
    1399             :         unsigned col_flags;
    1400             :         /* position of the columns in the row */
    1401             :         int cols_pos[BULKCOL_COUNT];
    1402             :         int cols_values[BULKCOL_COUNT];
    1403             :         TDS5COLINFO *colinfo;
    1404             : 
    1405             : #if ENABLE_EXTRA_CHECKS
    1406         198 :         int num_defs = 0;
    1407             : #endif
    1408             : 
    1409         198 :         CHECK_TDS_EXTRA(tds);
    1410             : 
    1411        1684 :         while ((rc = tds_process_tokens(tds, &res_type, &done_flags, TDS_RETURN_DONE|TDS_RETURN_ROWFMT|TDS_RETURN_ROW)) == TDS_SUCCESS) {
    1412        1288 :                 switch (res_type) {
    1413         210 :                 case TDS_ROWFMT_RESULT:
    1414             :                         /* check if it's the resultset with column information and save column positions */
    1415         210 :                         row_match = false;
    1416         210 :                         col_flags = 0;
    1417         210 :                         res_info = tds->current_results;
    1418         210 :                         if (!res_info)
    1419           0 :                                 continue;
    1420        2448 :                         for (icol = 0; icol < res_info->num_cols; ++icol) {
    1421        2448 :                                 const TDSCOLUMN *col = res_info->columns[icol];
    1422        4896 :                                 int scol = tds5_bulk_insert_column(tds_dstr_cstr(&col->column_name));
    1423        2448 :                                 if (scol < 0)
    1424        1062 :                                         continue;
    1425        1386 :                                 cols_pos[scol] = icol;
    1426        1386 :                                 col_flags |= 1 << scol;
    1427             :                         }
    1428         210 :                         if (col_flags == BULKCOL_ALL)
    1429         198 :                                 row_match = true;
    1430             :                         break;
    1431         868 :                 case TDS_ROW_RESULT:
    1432             :                         /* get the results */
    1433         868 :                         col_flags = 0;
    1434         868 :                         res_info = tds->current_results;
    1435         868 :                         if (!res_info)
    1436           0 :                                 continue;
    1437         868 :                         if (!row_match) {
    1438          12 :                                 tds_extra_assert(res_info->num_cols == num_defs);
    1439          12 :                                 tds5_read_bulk_defaults(tds, res_info, bcpinfo);
    1440          12 :                                 continue;
    1441             :                         }
    1442        5992 :                         for (icol = 0; icol < BULKCOL_COUNT; ++icol) {
    1443        5992 :                                 const TDSCOLUMN *col = res_info->columns[cols_pos[icol]];
    1444        5992 :                                 TDS_INT value = 0;
    1445             : 
    1446             :                                 /* NULL is read as 0 */
    1447        5992 :                                 if (col->column_cur_size >= 0 && !tds_try_get_int_col(tds, col, &value))
    1448             :                                         break;
    1449        5992 :                                 col_flags |= 1 << icol;
    1450        5992 :                                 cols_values[icol] = value;
    1451             :                         }
    1452             :                         /* save information */
    1453        1712 :                         if (col_flags != BULKCOL_ALL ||
    1454        1712 :                                 cols_values[BULKCOL_colcnt] < 1 ||
    1455         856 :                                 cols_values[BULKCOL_colcnt] > 4096 || /* limit of columns accepted */
    1456        1712 :                                 cols_values[BULKCOL_colid] < 1 ||
    1457             :                                 cols_values[BULKCOL_colid] > cols_values[BULKCOL_colcnt]) {
    1458             :                                 rc = TDS_FAIL;
    1459             :                                 break;
    1460             :                         }
    1461         856 :                         if (bcpinfo->sybase_colinfo == NULL) {
    1462         186 :                                 bcpinfo->sybase_colinfo = calloc(cols_values[BULKCOL_colcnt], sizeof(*bcpinfo->sybase_colinfo));
    1463         186 :                                 if (bcpinfo->sybase_colinfo == NULL) {
    1464             :                                         rc = TDS_FAIL;
    1465             :                                         break;
    1466             :                                 }
    1467         186 :                                 bcpinfo->sybase_count = cols_values[BULKCOL_colcnt];
    1468             :                         }
    1469             :                         /* bound check, colcnt could have changed from row to row */
    1470         856 :                         if (cols_values[BULKCOL_colid] > bcpinfo->sybase_count) {
    1471             :                                 rc = TDS_FAIL;
    1472             :                                 break;
    1473             :                         }
    1474         856 :                         colinfo = &bcpinfo->sybase_colinfo[cols_values[BULKCOL_colid] - 1];
    1475         856 :                         colinfo->type = cols_values[BULKCOL_type];
    1476         856 :                         colinfo->status = cols_values[BULKCOL_status];
    1477         856 :                         colinfo->offset = cols_values[BULKCOL_offset];
    1478         856 :                         colinfo->length = cols_values[BULKCOL_length];
    1479         856 :                         colinfo->dflt = !!cols_values[BULKCOL_dflt];
    1480             : #if ENABLE_EXTRA_CHECKS
    1481         856 :                         if (colinfo->dflt)
    1482          72 :                                 ++num_defs;
    1483             : #endif
    1484         856 :                         tdsdump_log(TDS_DBG_INFO1, "gotten row information %d type %d length %d status %d offset %d\n",
    1485             :                                     cols_values[BULKCOL_colid], colinfo->type, colinfo->length, colinfo->status, colinfo->offset);
    1486             :                         break;
    1487         210 :                 case TDS_DONE_RESULT:
    1488             :                 case TDS_DONEPROC_RESULT:
    1489             :                 case TDS_DONEINPROC_RESULT:
    1490         210 :                         if ((done_flags & TDS_DONE_ERROR) != 0)
    1491           0 :                                 ret = TDS_FAIL;
    1492             :                 default:
    1493             :                         break;
    1494             :                 }
    1495             :         }
    1496         198 :         if (TDS_FAILED(rc))
    1497           0 :                 ret = rc;
    1498             : 
    1499         198 :         return ret;
    1500             : }
    1501             : 
    1502             : static TDSRET
    1503       28152 : tds5_get_col_data_or_dflt(tds_bcp_get_col_data get_col_data, TDSBCPINFO *bulk, TDSCOLUMN *bcpcol, int offset, int colnum)
    1504             : {
    1505             :         TDSRET ret;
    1506             :         BCPCOLDATA *coldata;
    1507             : 
    1508       28152 :         bcpcol->bcp_column_data->deferred = false;
    1509       28152 :         ret = get_col_data(bulk, bcpcol, colnum, offset);
    1510             :         /* errors must be reported, defaults are used only for NULL values */
    1511       28152 :         TDS_PROPAGATE(ret);
    1512       28128 :         coldata = bcpcol->bcp_column_data;
    1513       28128 :         if (coldata->deferred && !coldata->is_null
    1514          36 :             && (!type_has_textptr(bcpcol->on_server.column_type) || is_unicode_type(bcpcol->on_server.column_type))) {
    1515           0 :                 tdsdump_log(TDS_DBG_ERROR, "column %d cannot be deferred\n", colnum + 1);
    1516             :                 return TDS_FAIL;
    1517             :         }
    1518       28128 :         if (coldata->is_null && bulk->sybase_colinfo != NULL && !type_has_textptr(bcpcol->column_type)) {
    1519        1390 :                 const TDS5COLINFO *syb_info = &bulk->sybase_colinfo[colnum];
    1520             : 
    1521        1390 :                 if (!syb_info->dflt) {
    1522             :                         /* leave to NULL */
    1523        1106 :                         if (!bcpcol->column_nullable)
    1524             :                                 return TDS_FAIL;
    1525             :                 } else {
    1526         284 :                         if (!TDS_RESIZE(coldata->data, syb_info->dflt_size))
    1527             :                                 return TDS_FAIL;
    1528             : 
    1529         284 :                         memcpy(coldata->data, syb_info->dflt_value, syb_info->dflt_size);
    1530         284 :                         coldata->datalen = syb_info->dflt_size;
    1531         284 :                         coldata->is_null = false;
    1532             :                 }
    1533             :                 return TDS_SUCCESS;
    1534             :         }
    1535             :         return ret;
    1536             : }
    1537             : 
    1538             : /**
    1539             :  * Free row data allocated in the result set.
    1540             :  */
    1541             : static void 
    1542         186 : tds_bcp_row_free(TDSRESULTINFO* result, unsigned char *row TDS_UNUSED)
    1543             : {
    1544         186 :         result->row_size = 0;
    1545         186 :         TDS_ZERO_FREE(result->current_row);
    1546         186 : }
    1547             : 
    1548             : /**
    1549             :  * Start bulk copy to server
    1550             :  * \tds
    1551             :  * \param bcpinfo BCP information already prepared
    1552             :  */
    1553             : TDSRET
    1554         984 : tds_bcp_start_copy_in(TDSSOCKET *tds, TDSBCPINFO *bcpinfo)
    1555             : {
    1556             :         TDSCOLUMN *bcpcol;
    1557             :         int i;
    1558         984 :         int fixed_col_len_tot     = 0;
    1559         984 :         int variable_col_len_tot  = 0;
    1560         984 :         int column_bcp_data_size  = 0;
    1561         984 :         int bcp_record_size       = 0;
    1562             :         TDSRET rc;
    1563             :         TDS_INT var_cols;
    1564             :         
    1565         984 :         tdsdump_log(TDS_DBG_FUNC, "tds_bcp_start_copy_in(%p, %p)\n", tds, bcpinfo);
    1566             : 
    1567         984 :         TDS_PROPAGATE(tds_bcp_start_insert_stmt(tds, bcpinfo));
    1568             : 
    1569         984 :         rc = tds_bcp_start(tds, bcpinfo);
    1570         984 :         if (TDS_FAILED(rc)) {
    1571             :                 /* TODO, in CTLib was _ctclient_msg(blkdesc->con, "blk_rowxfer", 2, 5, 1, 140, ""); */
    1572             :                 return rc;
    1573             :         }
    1574             : 
    1575             :         /* 
    1576             :          * TDS5 BCP is coded to pack all columns into an internal buffer and then
    1577             :          * send the buffer (as opposed to TDS7 BCP which just calls wire put functions
    1578             :          * for each column). So we have to work out how big a buffer to allocate.
    1579             :          */
    1580         984 :         var_cols = 0;
    1581             : 
    1582         984 :         if (IS_TDS50(tds->conn)) {
    1583         670 :                 for (i = 0; i < bcpinfo->bindinfo->num_cols; i++) {
    1584             :         
    1585         670 :                         bcpcol = bcpinfo->bindinfo->columns[i];
    1586             : 
    1587             :                         /*
    1588             :                          * work out storage required for this datatype
    1589             :                          * blobs always require 16, numerics vary, the
    1590             :                          * rest can be taken from the server
    1591             :                          */
    1592             : 
    1593         670 :                         if (type_has_textptr(bcpcol->on_server.column_type))
    1594             :                                 column_bcp_data_size  = 16;
    1595         644 :                         else if (is_numeric_type(bcpcol->on_server.column_type))
    1596          46 :                                 column_bcp_data_size  = tds_numeric_bytes_per_prec[bcpcol->column_prec];
    1597             :                         else
    1598         598 :                                 column_bcp_data_size  = bcpcol->column_size;
    1599             : 
    1600             :                         /*
    1601             :                          * now add that size into either fixed or variable
    1602             :                          * column totals...
    1603             :                          */
    1604             : 
    1605         670 :                         if (is_nullable_type(bcpcol->on_server.column_type) || bcpcol->column_nullable) {
    1606         414 :                                 var_cols++;
    1607         414 :                                 variable_col_len_tot += column_bcp_data_size;
    1608             :                         } else {
    1609         256 :                                 fixed_col_len_tot += column_bcp_data_size;
    1610             :                         }
    1611             :                 }
    1612             : 
    1613             :                 /* this formula taken from sybase manual...
    1614             :                  * (with extra 4 bytes possibly for datarows locked) */
    1615             : 
    1616         372 :                 bcp_record_size =       4 +
    1617         372 :                                                         (bcpinfo->datarows_locking ? 4 : 0) +
    1618         186 :                                                         fixed_col_len_tot +
    1619         186 :                                                         variable_col_len_tot +
    1620         372 :                                                         ( (int)(variable_col_len_tot / 256 ) + 1 ) +
    1621         186 :                                                         (var_cols + 1) +
    1622             :                                                         2;
    1623             : 
    1624         186 :                 tdsdump_log(TDS_DBG_FUNC, "current_record_size = %d\n", bcpinfo->bindinfo->row_size);
    1625         186 :                 tdsdump_log(TDS_DBG_FUNC, "bcp_record_size     = %d\n", bcp_record_size);
    1626             : 
    1627         186 :                 if (bcp_record_size > bcpinfo->bindinfo->row_size) {
    1628         186 :                         if (!TDS_RESIZE(bcpinfo->bindinfo->current_row, bcp_record_size)) {
    1629           0 :                                 tdsdump_log(TDS_DBG_FUNC, "could not realloc current_row\n");
    1630             :                                 return TDS_FAIL;
    1631             :                         }
    1632         186 :                         bcpinfo->bindinfo->row_free = tds_bcp_row_free;
    1633         186 :                         bcpinfo->bindinfo->row_size = bcp_record_size;
    1634             :                 }
    1635             :         }
    1636             : 
    1637             :         return TDS_SUCCESS;
    1638             : }
    1639             : 
    1640             : /** \cond HIDDEN_SYMBOLS */
    1641             : #if defined(_WIN32) && defined(HAVE__LOCK_FILE) && defined(HAVE__UNLOCK_FILE)
    1642             : #define TDS_HAVE_STDIO_LOCKED 1
    1643             : #define flockfile(s) _lock_file(s)
    1644             : #define funlockfile(s) _unlock_file(s)
    1645             : #define getc_unlocked(s) _getc_nolock(s)
    1646             : #define feof_unlocked(s) _feof_nolock(s)
    1647             : #endif
    1648             : 
    1649             : #ifndef TDS_HAVE_STDIO_LOCKED
    1650             : #undef getc_unlocked
    1651             : #undef feof_unlocked
    1652             : #undef flockfile
    1653             : #undef funlockfile
    1654             : #define getc_unlocked(s) getc(s)
    1655             : #define feof_unlocked(s) feof(s)
    1656             : #define flockfile(s) do { } while(0)
    1657             : #define funlockfile(s) do { } while(0)
    1658             : #endif
    1659             : /** \endcond */
    1660             : 
    1661             : /** Check if a circular buffer matches a string of same length. Requires length > 0.  */
    1662             : static bool
    1663     1897800 : cbuf_match(const char *cbuf, size_t cbuf_size, size_t cpos, const char *match)
    1664             : {
    1665     1897800 :         if (cbuf_size == 1)
    1666      251770 :                 return cbuf[0] == match[0];
    1667     1646030 :         if (cbuf_size == 2)
    1668     1646030 :                 return cbuf[cpos] == match[0] && cbuf[!cpos] == match[1];
    1669             : 
    1670           0 :         return !memcmp(cbuf + cpos, match, cbuf_size - cpos)
    1671           0 :                 && !memcmp(cbuf, match + cbuf_size - cpos, cpos);
    1672             : }
    1673             : 
    1674             : /**
    1675             :  * Reads a chunk of data from file stream checking for terminator
    1676             :  * \param stream file stream
    1677             :  * \param ptr buffer where to read data
    1678             :  * \param len length of buffer
    1679             :  */
    1680             : static int
    1681        6090 : tds_file_stream_read(TDSINSTREAM *stream, void *ptr, size_t len)
    1682             : {
    1683        6090 :         TDSFILESTREAM *s = (TDSFILESTREAM *) stream;
    1684             :         char ch;
    1685        6090 :         char *p = (char *) ptr;
    1686             : 
    1687     1904340 :         while (len) {
    1688     1897800 :                 if (cbuf_match(s->cbuf, s->term_len, s->cpos, s->terminator))
    1689        5640 :                         return p - (char *) ptr;
    1690             : 
    1691             :                 /* It didn't; output from the circular buffer and input a new character */
    1692     1892160 :                 if (s->inpos < s->inlen)
    1693     1888680 :                         ch = s->inbuf[s->inpos++];
    1694        3480 :                 else if (tds_file_stream_read_raw(s, &ch, 1) != 1)
    1695             :                         return -1;
    1696             : 
    1697     1892160 :                 *p++ = s->cbuf[s->cpos];
    1698     1892160 :                 --len;
    1699             : 
    1700     1892160 :                 s->cbuf[s->cpos++] = ch;
    1701     1892160 :                 if (s->cpos == s->term_len)
    1702     1069680 :                         s->cpos = 0;
    1703             :         }
    1704         450 :         return p - (char *) ptr;
    1705             : }
    1706             : 
    1707             : /* Read raw data from stream (no terminator or iconv) */
    1708             : size_t
    1709        6868 : tds_file_stream_read_raw(TDSFILESTREAM *stream, void *ptr, size_t n)
    1710             : {
    1711        6868 :         char *cptr = ptr;
    1712             : 
    1713       20388 :         while (n) {
    1714             :                 size_t chunk;
    1715             : 
    1716             :                 /* Buffer some more data if we consumed it all */
    1717        6868 :                 if (stream->inlen == stream->inpos) {
    1718        4682 :                         stream->inpos = 0;
    1719        4682 :                         stream->inlen = fread(stream->inbuf, 1, sizeof(stream->inbuf), stream->f);
    1720        4682 :                         if (stream->inlen == 0)
    1721             :                                 break;
    1722        4466 :                         stream->offset += stream->inlen;
    1723             :                 }
    1724             : 
    1725             :                 /* Output data from buffer */
    1726        6652 :                 chunk = TDS_MIN(stream->inlen - stream->inpos, n);
    1727             : 
    1728        6652 :                 if (chunk > 0) {
    1729        6652 :                         memcpy(cptr, stream->inbuf + stream->inpos, chunk);
    1730        6652 :                         cptr += chunk;
    1731        6652 :                         stream->inpos += chunk;
    1732        6652 :                         n -= chunk;
    1733             :                 }
    1734             :         }
    1735             : 
    1736        6868 :         return cptr - (char *) ptr;
    1737             : }
    1738             : 
    1739             : #if ENABLE_EXTRA_CHECKS && (defined(__MINGW32__) || defined(_MSC_VER))
    1740             : static void
    1741             : check_binary_mode(FILE *f)
    1742             : {
    1743             :         const int mode = _setmode(fileno(f), _O_BINARY);
    1744             : 
    1745             : #ifndef _O_WTEXT
    1746             : #define _O_WTEXT 0
    1747             : #endif
    1748             : #ifndef _O_U16TEXT
    1749             : #define _O_U16TEXT 0
    1750             : #endif
    1751             : #ifndef _O_U8TEXT
    1752             : #define _O_U8TEXT 0
    1753             : #endif
    1754             :         const int text_modes = _O_TEXT | _O_WTEXT | _O_U16TEXT | _O_U8TEXT;
    1755             : 
    1756             :         assert((mode & _O_BINARY) != 0);
    1757             :         assert((mode & text_modes) == 0);
    1758             :         _setmode(fileno(f), mode);
    1759             : }
    1760             : #else
    1761             : #define check_binary_mode(f) do {} while(0)
    1762             : #endif
    1763             : 
    1764             : TDSRET
    1765         236 : tds_file_stream_init(TDSFILESTREAM *stream, FILE *f)
    1766             : {
    1767             :         offset_type offset;
    1768             : 
    1769         236 :         memset(stream, 0, sizeof(*stream));
    1770         236 :         if (!f)
    1771             :                 return TDS_FAIL;
    1772             : 
    1773             :         check_binary_mode(f);
    1774             : 
    1775         236 :         offset = ftello(f);
    1776         236 :         if (offset == -1) {
    1777           0 :                 fclose(f);
    1778           0 :                 return TDS_FAIL;
    1779             :         }
    1780             : 
    1781         236 :         stream->f = f;
    1782         236 :         stream->stream.read = tds_file_stream_read;
    1783         236 :         stream->offset = offset;
    1784         236 :         return TDS_SUCCESS;
    1785             : }
    1786             : 
    1787             : TDSRET
    1788         226 : tds_file_stream_close(TDSFILESTREAM *stream)
    1789             : {
    1790         226 :         int ret = stream->f ? fclose(stream->f) : 0;
    1791             : 
    1792         226 :         memset(stream, 0, sizeof(*stream));
    1793         226 :         return ret;
    1794             : }
    1795             : 
    1796             : /** Sets the terminator and also performs an initial population of the circular buffer */
    1797             : static TDSRET
    1798        3188 : tds_file_stream_use_terminator(TDSFILESTREAM *stream, const char *term, size_t term_len)
    1799             : {
    1800             :         size_t readed;
    1801             : 
    1802        3188 :         if (term_len > sizeof(stream->cbuf))
    1803             :                 return TDS_FAIL;
    1804             : 
    1805        6180 :         stream->terminator = term;
    1806        6180 :         stream->term_len = term_len;
    1807        6180 :         stream->cpos = 0;
    1808             : 
    1809        3188 :         if (term_len == 0)
    1810             :                 return TDS_SUCCESS;
    1811             : 
    1812             :         /* Have to have initial data to populate the circular buffer (if the file contains
    1813             :          * less data than the length of 1 expected terminator, it means file is corrupt)
    1814             :          */
    1815        3188 :         readed = tds_file_stream_read_raw(stream, stream->cbuf, term_len);
    1816        3188 :         if (readed != term_len) {
    1817         196 :                 if (readed == 0 && feof(stream->f))
    1818             :                         return TDS_NO_MORE_RESULTS;
    1819             :                 return TDS_FAIL;
    1820             :         }
    1821             : 
    1822             :         return TDS_SUCCESS;
    1823             : }
    1824             : 
    1825             : TDSRET
    1826          50 : tds_file_stream_seek_set(TDSFILESTREAM *stream, offset_type seek_to)
    1827             : {
    1828          50 :         if (fseeko(stream->f, seek_to, SEEK_SET) != 0)
    1829             :                 return TDS_FAIL;
    1830             : 
    1831          50 :         stream->offset = seek_to;
    1832          50 :         stream->inpos = 0;
    1833          50 :         stream->inlen = 0;
    1834          50 :         return TDS_SUCCESS;
    1835             : }
    1836             : 
    1837             : offset_type
    1838        3350 : tds_file_stream_tell(TDSFILESTREAM *stream)
    1839             : {
    1840        3350 :         return stream->offset - (stream->inlen - stream->inpos);
    1841             : }
    1842             : 
    1843             : /**
    1844             :  * Read a data file, passing the data through iconv().
    1845             :  * \retval TDS_SUCCESS  success
    1846             :  * \retval TDS_FAIL     error reading the column
    1847             :  * \retval TDS_NO_MORE_RESULTS end of file detected
    1848             :  */
    1849             : TDSRET
    1850        3188 : tds_bcp_fread(TDSSOCKET *tds, TDSICONV *char_conv, TDSFILESTREAM *stream,
    1851             :               const char *terminator, size_t term_len, char **outbuf, size_t *outbytes)
    1852             : {
    1853             :         TDSRET res;
    1854             :         TDSDYNAMICSTREAM w;
    1855             : 
    1856             :         /* Prepare input stream, returning TDS_NO_MORE_RESULTS if there
    1857             :          * is not enough data in the stream for even one terminator -- this
    1858             :          * indicates a clean end-of-file being reached, as opposed to
    1859             :          * ending the file while looking for a terminator, which will generate
    1860             :          * a TDS_FAIL from tds_copy_stream().
    1861             :          */
    1862        3188 :         res = tds_file_stream_use_terminator(stream, terminator, term_len);
    1863        3188 :         if (res != TDS_SUCCESS)
    1864             :                 return res;
    1865             : 
    1866             :         /* prepare output streams */
    1867        2992 :         TDS_PROPAGATE(tds_dynamic_stream_init(&w, (void **) outbuf, 0));
    1868             : 
    1869             :         /* convert/copy from input stream to output one */
    1870        2992 :         flockfile(stream->f);
    1871        2992 :         if (char_conv == NULL)
    1872        1216 :                 res = tds_copy_stream(&stream->stream, &w.stream);
    1873             :         else
    1874        1776 :                 res = tds_convert_stream(tds, char_conv, to_server, &stream->stream, &w.stream);
    1875        2992 :         funlockfile(stream->f);
    1876             : 
    1877             :         /* Avoid any dangling pointers */
    1878        2992 :         tds_file_stream_use_terminator(stream, NULL, 0);
    1879             : 
    1880        2992 :         TDS_PROPAGATE(res);
    1881             : 
    1882        2992 :         *outbytes = w.size;
    1883             : 
    1884             :         /* terminate buffer */
    1885        2992 :         if (!w.stream.buf_len)
    1886             :                 return TDS_FAIL;
    1887             : 
    1888        2992 :         ((char *) w.stream.buffer)[0] = 0;
    1889        2992 :         w.stream.write(&w.stream, 1);
    1890             : 
    1891        2992 :         return res;
    1892             : }
    1893             : 
    1894             : /**
    1895             :  * Start writing writetext request.
    1896             :  * This request start a bulk session.
    1897             :  * \tds
    1898             :  * \param objname table name
    1899             :  * \param textptr TEXTPTR (see sql documentation)
    1900             :  * \param timestamp data timestamp
    1901             :  * \param with_log is log is enabled during insert
    1902             :  * \param size bytes to be inserted
    1903             :  */
    1904             : TDSRET
    1905          42 : tds_writetext_start(TDSSOCKET *tds, const char *objname, const char *textptr, const char *timestamp, int with_log, TDS_UINT size)
    1906             : {
    1907             :         TDSRET rc;
    1908             : 
    1909             :         /* TODO mssql does not like timestamp */
    1910          42 :         rc = tds_submit_queryf(tds,
    1911             :                               "writetext bulk %s 0x%s timestamp = 0x%s%s",
    1912             :                               objname, textptr, timestamp, with_log ? " with log" : "");
    1913          42 :         TDS_PROPAGATE(rc);
    1914             : 
    1915             :         /* set we want to switch to bulk state */
    1916          42 :         tds->bulk_query = true;
    1917             : 
    1918             :         /* read the end token */
    1919          42 :         TDS_PROPAGATE(tds_process_simple_query(tds));
    1920             : 
    1921          42 :         tds->out_flag = TDS_BULK;
    1922          42 :         if (tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1923             :                 return TDS_FAIL;
    1924             : 
    1925          42 :         tds_put_int(tds, size);
    1926             : 
    1927          42 :         tds_set_state(tds, TDS_SENDING);
    1928          42 :         return TDS_SUCCESS;
    1929             : }
    1930             : 
    1931             : /**
    1932             :  * Send some data in the writetext request started by tds_writetext_start.
    1933             :  * You should write in total (with multiple calls to this function) all
    1934             :  * bytes declared calling tds_writetext_start.
    1935             :  * \tds
    1936             :  * \param text data to write
    1937             :  * \param size data size in bytes
    1938             :  */
    1939             : TDSRET
    1940         312 : tds_writetext_continue(TDSSOCKET *tds, const TDS_UCHAR *text, TDS_UINT size)
    1941             : {
    1942         312 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1943             :                 return TDS_FAIL;
    1944             : 
    1945             :         /* TODO check size left */
    1946         312 :         tds_put_n(tds, text, size);
    1947             : 
    1948         312 :         tds_set_state(tds, TDS_SENDING);
    1949         312 :         return TDS_SUCCESS;
    1950             : }
    1951             : 
    1952             : /**
    1953             :  * Finish sending writetext data.
    1954             :  * \tds
    1955             :  */
    1956             : TDSRET
    1957          42 : tds_writetext_end(TDSSOCKET *tds)
    1958             : {
    1959          42 :         if (tds->out_flag != TDS_BULK || tds_set_state(tds, TDS_WRITING) != TDS_WRITING)
    1960             :                 return TDS_FAIL;
    1961             : 
    1962          42 :         tds_flush_packet(tds);
    1963          42 :         tds_set_state(tds, TDS_PENDING);
    1964          42 :         return TDS_SUCCESS;
    1965             : }

Generated by: LCOV version 1.13