LCOV - code coverage report
Current view: top level - src/dblib - dblib.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 1389 2594 53.5 %
Date: 2025-07-05 12:09:11 Functions: 97 161 60.2 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  Brian Bruns
       3             :  * Copyright (C) 2006-2015  Frediano Ziglio
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or
       6             :  * modify it under the terms of the GNU Library General Public
       7             :  * License as published by the Free Software Foundation; either
       8             :  * version 2 of the License, or (at your option) any later version.
       9             :  *
      10             :  * This library is distributed in the hope that it will be useful,
      11             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13             :  * Library General Public License for more details.
      14             :  *
      15             :  * You should have received a copy of the GNU Library General Public
      16             :  * License along with this library; if not, write to the
      17             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      18             :  * Boston, MA 02111-1307, USA.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : 
      23             : #include <stdarg.h>
      24             : 
      25             : #include <freetds/time.h>
      26             : 
      27             : #include <assert.h>
      28             : #include <stdio.h>
      29             : 
      30             : #if HAVE_STDLIB_H
      31             : #include <stdlib.h>
      32             : #endif /* HAVE_STDLIB_H */
      33             : 
      34             : #if HAVE_STRING_H
      35             : #include <string.h>
      36             : #endif /* HAVE_STRING_H */
      37             : 
      38             : #if HAVE_UNISTD_H
      39             : #include <unistd.h>
      40             : #endif /* HAVE_UNISTD_H */
      41             : 
      42             : #if HAVE_ERRNO_H
      43             : # include <errno.h>
      44             : #endif /* HAVE_ERRNO_H */
      45             : 
      46             : /** 
      47             :  * \ingroup dblib_core
      48             :  * \remarks Either SYBDBLIB or MSDBLIB (not both) must be defined. 
      49             :  *      This affects how certain application-addressable 
      50             :  *      strucures are defined.  
      51             :  */
      52             : #include <freetds/tds.h>
      53             : #include <freetds/thread.h>
      54             : #include <freetds/convert.h>
      55             : #include <freetds/utils/string.h>
      56             : #include <freetds/data.h>
      57             : #include <freetds/replacements.h>
      58             : #include <sybfront.h>
      59             : #include <sybdb.h>
      60             : #include <syberror.h>
      61             : #include <dblib.h>
      62             : 
      63             : #ifndef MAX
      64             : #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
      65             : #endif
      66             : 
      67             : static RETCODE _dbresults(DBPROCESS * dbproc);
      68             : static BYTE *_dbcoldata(TDSCOLUMN *colinfo);
      69             : static int _get_printable_size(TDSCOLUMN * colinfo);
      70             : static char *_dbprdate(char *timestr);
      71             : static int _dbnullable(DBPROCESS * dbproc, int column);
      72             : static const char *tds_prdatatype(int datatype_token);
      73             : 
      74             : static int default_err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr);
      75             : 
      76             : void copy_data_to_host_var(DBPROCESS *, TDS_SERVER_TYPE, const BYTE *, int, BYTE *, DBINT, int, DBINT *);
      77             : RETCODE dbgetnull(DBPROCESS *dbproc, int bindtype, int varlen, BYTE* varaddr);
      78             : 
      79             : /**
      80             :  * \file dblib.c
      81             :  * Main implementation file for \c db-lib.
      82             :  */
      83             : /**
      84             :  * \file bcp.c
      85             :  * Implementation of \c db-lib bulk copy functions.
      86             :  */
      87             : /**
      88             :  * \defgroup dblib_api The db-lib API
      89             :  * Functions callable by \c db-lib client programs
      90             :  *
      91             :  * The \c db_lib interface is implemented by both Sybase and Microsoft.  FreeTDS seeks to implement 
      92             :  * first the intersection of the functions defined by the vendors.  
      93             :  */
      94             :  
      95             : /**
      96             :  * \ingroup dblib_api
      97             :  * \defgroup dblib_core Primary functions
      98             :  * Core functions needed by most db-lib programs.  
      99             : */
     100             : /**
     101             :  * \ingroup dblib_api
     102             :  * \defgroup dblib_rpc Remote Procedure functions
     103             :  * Functions used with stored procedures.  
     104             :  * Especially useful for OUTPUT parameters, because modern Microsoft servers do not 
     105             :  * return output parameter data to the client unless the procedure was invoked
     106             :  * with dbrpcsend().  
     107             :  */
     108             : /**
     109             :  * \ingroup dblib_api
     110             :  * \defgroup dblib_bcp Bulk copy functions
     111             :  * Functions to bulk-copy (a/k/a \em bcp) data to/from the database.  
     112             :  */
     113             : /**
     114             :  * \ingroup dblib_bcp
     115             :  * \defgroup dblib_bcp_internal Internal bcp functions
     116             :  * Static functions internal to the bcp library.  
     117             :  */
     118             : /**
     119             :  * \ingroup dblib_api
     120             :  * \defgroup dblib_money Money functions 
     121             :  * Functions to manipulate the MONEY datatype.  
     122             :  */
     123             : /**
     124             :  * \ingroup dblib_api
     125             :  * \defgroup dblib_datetime Datetime functions 
     126             :  * Functions to manipulate DBDATETIME structures.  Defined by Sybase only.  
     127             :  * These are not implemented:
     128             :  *      - dbdate4cmp()
     129             :  *      - dbdate4zero()
     130             :  *      - dbdatechar()
     131             :  *      - dbdatename()
     132             :  *      - dbdateorder()
     133             :  *      - dbdatepart()
     134             :  *      - dbdatezero()
     135             :  *      - dbdayname()
     136             :  */
     137             : /**
     138             :  * \ingroup dblib_api
     139             :  * \defgroup dblib_internal Internals
     140             :  * Functions called within \c db-lib for self-help.  
     141             :  * These functions are of interest only to people hacking on the FreeTDS db-lib implementation.  
     142             :  */
     143             : /**
     144             :  * \ingroup dblib_api
     145             :  * \defgroup dblib_unimplemented Unimplemented
     146             :  * Functions thus far not implemented in the FreeTDS db-lib implementation.  
     147             :  * While some of these are simply awaiting someone with time and skill (and inclination)
     148             :  * it might be noted here that the old browse functions (e.g. dbcolbrowse()) 
     149             :  * are on the never-to-do list.  
     150             :  * They were defined by Sybase and were superseded long ago, although they're still
     151             :  * present in Microsoft's implementation.  
     152             :  * They were never popular and today better alternatives are available.  
     153             :  * For completeness, they are:
     154             :  *      - dbcolbrowse()
     155             :  *      - dbcolsource()
     156             :  *      - dbfreequal()
     157             :  *      - dbqual()
     158             :  *      - dbtabbrowse()
     159             :  *      - dbtabcount()
     160             :  *      - dbtabname()
     161             :  *      - dbtabsource()
     162             :  *      - dbtsnewlen()
     163             :  *      - dbtsnewval()
     164             :  *      - dbtsput()
     165             :  */
     166             : 
     167             : /* info/err message handler functions (or rather pointers to them) */
     168             : MHANDLEFUNC _dblib_msg_handler = NULL;
     169             : EHANDLEFUNC _dblib_err_handler = default_err_handler;
     170             : 
     171             : /** \internal
     172             :  * \dblib_internal
     173             :  * \remarks A db-lib connection has an implicit TDS context. 
     174             :  */
     175             : typedef struct dblib_context
     176             : {
     177             :         /** reference count, time dbinit called */
     178             :         int ref_count;
     179             : 
     180             :         /** libTDS context */
     181             :         TDSCONTEXT *tds_ctx;
     182             :         /** libTDS context reference counter */
     183             :         int tds_ctx_ref_count;
     184             : 
     185             :         /* save all connection in a list */
     186             :         TDSSOCKET **connection_list;
     187             :         int connection_list_size;
     188             :         int connection_list_size_represented;
     189             :         char *recftos_filename;
     190             :         int recftos_filenum;
     191             :         int login_timeout;      /**< not used unless positive */
     192             :         int query_timeout;      /**< not used unless positive */
     193             : }
     194             : DBLIBCONTEXT;
     195             : 
     196             : static DBLIBCONTEXT g_dblib_ctx;
     197             : static tds_mutex dblib_mutex = TDS_MUTEX_INITIALIZER;
     198             : 
     199             : static int g_dblib_version =
     200             : #if TDS50
     201             :         DBVERSION_100;
     202             : #elif TDS71
     203             :         DBVERSION_71;
     204             : #elif TDS72
     205             :         DBVERSION_72;
     206             : #elif TDS73
     207             :         DBVERSION_73;
     208             : #elif TDS74
     209             :         DBVERSION_74;
     210             : #else
     211             :         DBVERSION_UNKNOWN;
     212             : #endif
     213             : 
     214             : 
     215             : static int
     216         864 : dblib_add_connection(DBLIBCONTEXT * ctx, TDSSOCKET * tds)
     217             : {
     218         864 :         int i = 0;
     219         864 :         const int list_size = ctx->connection_list_size_represented;
     220             : 
     221         864 :         tdsdump_log(TDS_DBG_FUNC, "dblib_add_connection(%p, %p)\n", ctx, tds);
     222             : 
     223        1466 :         while (i < list_size && ctx->connection_list[i])
     224         602 :                 i++;
     225         864 :         if (i == list_size) {
     226             :                 return 1;
     227             :         } else {
     228         854 :                 ctx->connection_list[i] = tds;
     229         854 :                 return 0;
     230             :         }
     231             : }
     232             : 
     233             : static void
     234         456 : dblib_del_connection(DBLIBCONTEXT * ctx, TDSSOCKET * tds)
     235             : {
     236         456 :         int i = 0;
     237         456 :         const int list_size = ctx->connection_list_size;
     238             : 
     239         456 :         tdsdump_log(TDS_DBG_FUNC, "dblib_del_connection(%p, %p)\n", ctx, tds);
     240             : 
     241       41966 :         while (i < list_size && ctx->connection_list[i] != tds)
     242       41510 :                 i++;
     243         456 :         if (i == list_size) {
     244             :                 /* connection wasn't on the free list...now what */
     245             :         } else {
     246             :                 /* remove it */
     247         446 :                 ctx->connection_list[i] = NULL;
     248             :         }
     249         456 : }
     250             : 
     251             : static TDSCONTEXT*
     252        1366 : dblib_get_tds_ctx(void)
     253             : {
     254        1366 :         tdsdump_log(TDS_DBG_FUNC, "dblib_get_tds_ctx(void)\n");
     255             : 
     256        1366 :         tds_mutex_lock(&dblib_mutex);
     257        1366 :         ++g_dblib_ctx.tds_ctx_ref_count;
     258        1366 :         if (g_dblib_ctx.tds_ctx == NULL) {
     259         502 :                 g_dblib_ctx.tds_ctx = tds_alloc_context(&g_dblib_ctx);
     260             : 
     261             :                 /*
     262             :                  * Set the functions in the TDS layer to point to the correct handler functions
     263             :                  */
     264         502 :                 g_dblib_ctx.tds_ctx->msg_handler = _dblib_handle_info_message;
     265         502 :                 g_dblib_ctx.tds_ctx->err_handler = _dblib_handle_err_message;
     266         502 :                 g_dblib_ctx.tds_ctx->int_handler = _dblib_check_and_handle_interrupt;
     267             : 
     268         502 :                 if (g_dblib_ctx.tds_ctx->locale && !g_dblib_ctx.tds_ctx->locale->datetime_fmt) {
     269             :                         /* set default in case there's no locale file */
     270             :                         static const char datetime_format[] = "%b %e %Y %l:%M:%S:%z%p";
     271           0 :                         g_dblib_ctx.tds_ctx->locale->datetime_fmt = strdup(datetime_format);
     272             :                 }
     273             :         }
     274        1366 :         tds_mutex_unlock(&dblib_mutex);
     275        1366 :         return g_dblib_ctx.tds_ctx;
     276             : }
     277             : 
     278             : static void
     279         928 : dblib_release_tds_ctx(int count)
     280             : {
     281         928 :         tdsdump_log(TDS_DBG_FUNC, "dblib_release_tds_ctx(%d)\n", count);
     282             : 
     283         928 :         tds_mutex_lock(&dblib_mutex);
     284         928 :         g_dblib_ctx.tds_ctx_ref_count -= count;
     285         928 :         if (g_dblib_ctx.tds_ctx_ref_count <= 0) {
     286         472 :                 tds_free_context(g_dblib_ctx.tds_ctx);
     287         472 :                 g_dblib_ctx.tds_ctx = NULL;
     288             :         }
     289         928 :         tds_mutex_unlock(&dblib_mutex);
     290         928 : }
     291             : 
     292             : #include "buffering.h"
     293             : 
     294             : static void
     295        3198 : db_env_chg(TDSSOCKET * tds, int type, char *oldval, char *newval)
     296             : {
     297             :         DBPROCESS *dbproc;
     298             : 
     299        3198 :         assert(oldval != NULL && newval != NULL);
     300        3198 :         if (strlen(oldval) == 1 && *oldval == 1)
     301           0 :                 oldval = "(0x1)";
     302             :                 
     303        3198 :         tdsdump_log(TDS_DBG_FUNC, "db_env_chg(%p, %d, %s, %s)\n", tds, type, oldval, newval);
     304             : 
     305        3198 :         if (!tds || !tds_get_parent(tds))
     306             :                 return;
     307        3198 :         dbproc = (DBPROCESS *) tds_get_parent(tds);
     308             : 
     309        3198 :         dbproc->envchange_rcv |= (1 << (type - 1));
     310        3198 :         switch (type) {
     311        1664 :         case TDS_ENV_DATABASE:
     312        1664 :                 strlcpy(dbproc->dbcurdb, newval, sizeof(dbproc->dbcurdb));
     313        1664 :                 break;
     314           0 :         case TDS_ENV_CHARSET:
     315           0 :                 strlcpy(dbproc->servcharset, newval, sizeof(dbproc->servcharset));
     316           0 :                 break;
     317             :         default:
     318             :                 break;
     319             :         }
     320             :         return;
     321             : }
     322             : 
     323             : /** \internal
     324             :  * \ingroup dblib_internal
     325             :  * \brief Sanity checks for column-oriented functions.  
     326             :  * 
     327             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     328             :  * \param pcolinfo address of pointer to a TDSCOLUMN structure.
     329             :  * \remarks Makes sure dbproc and the requested column are valid.  
     330             :  *      Calls dbperror() if not.  
     331             :  * \returns appropriate error or SUCCEED
     332             :  */
     333             : static TDSCOLUMN*
     334       11604 : dbcolptr(DBPROCESS* dbproc, int column)
     335             : {
     336             :         TDSSOCKET *tds;
     337             :         TDSRESULTINFO *info;
     338             : 
     339       11604 :         if (!dbproc) {
     340           0 :                 dbperror(dbproc, SYBENULL, 0);
     341           0 :                 return NULL;
     342             :         }
     343       11604 :         tds = dbproc->tds_socket;
     344       11604 :         if (IS_TDSDEAD(tds)) {
     345           0 :                 dbperror(dbproc, SYBEDDNE, 0);
     346           0 :                 return NULL;
     347             :         }
     348       11604 :         info = tds->res_info;
     349       11604 :         if (!info)
     350             :                 return NULL;
     351       11344 :         if (column < 1 || column > info->num_cols) {
     352           0 :                 dbperror(dbproc, SYBECNOR, 0);
     353           0 :                 return NULL;
     354             :         } 
     355             :         
     356       11344 :         return info->columns[column - 1];
     357             : }
     358             : 
     359             : static TDSCOLUMN*
     360          64 : dbacolptr(DBPROCESS* dbproc, int computeid, int column, bool is_bind)
     361             : {
     362             :         unsigned int i;
     363             :         TDSSOCKET *tds;
     364             :         TDSCOMPUTEINFO *info;
     365             : 
     366          64 :         if (!dbproc) {
     367           0 :                 dbperror(dbproc, SYBENULL, 0);
     368           0 :                 return NULL;
     369             :         }
     370          64 :         tds = dbproc->tds_socket;
     371          64 :         if (IS_TDSDEAD(tds)) {
     372           0 :                 dbperror(dbproc, SYBEDDNE, 0);
     373           0 :                 return NULL;
     374             :         }
     375          32 :         for (i = 0;; ++i) {
     376         128 :                 if (i >= tds->num_comp_info) {
     377             :                         /* Attempt to bind user variable to a non-existent compute row */
     378           0 :                         if (is_bind)
     379           0 :                                 dbperror(dbproc, SYBEBNCR, 0);
     380             :                         return NULL;
     381             :                 }
     382          96 :                 info = tds->comp_info[i];
     383          96 :                 if (info->computeid == computeid)
     384             :                         break;
     385             :         }
     386             :         /* Fail if either the compute id or the column number is invalid. */
     387          64 :         if (column < 1 || column > info->num_cols) {
     388           0 :                 dbperror(dbproc, is_bind ? SYBEABNC : SYBECNOR, 0);
     389           0 :                 return NULL;
     390             :         }
     391             : 
     392          64 :         return info->columns[column - 1];
     393             : }
     394             : 
     395             : /*
     396             :  * Default null substitution values
     397             :  * Binding Type         Null Substitution Value
     398             :  * TINYBIND             0
     399             :  * SMALLBIND            0
     400             :  * INTBIND              0
     401             :  * CHARBIND             Empty string (padded with blanks)
     402             :  * STRINGBIND           Empty string (padded with blanks, null-terminated)
     403             :  * NTBSTRINGBIND        Empty string (null-terminated)
     404             :  * VARYCHARBIND         Empty string
     405             :  * BINARYBIND           Empty array (padded with zeros)
     406             :  * VARYBINBIND          Empty array
     407             :  * DATETIMEBIND         8 bytes of zeros
     408             :  * SMALLDATETIMEBIND    8 bytes of zeros
     409             :  * MONEYBIND            $0.00
     410             :  * SMALLMONEYBIND       $0.00
     411             :  * FLT8BIND             0.0
     412             :  * REALBIND             0.0
     413             :  * DECIMALBIND          0.0 (with default scale and precision)
     414             :  * NUMERICBIND          0.0 (with default scale and precision)
     415             :  * BOUNDARYBIND         Empty string (null-terminated)
     416             :  * SENSITIVITYBIND      Empty string (null-terminated)
     417             :  */
     418             : 
     419             : static const DBBIT              null_BIT = 0;
     420             : static const DBTINYINT          null_TINYINT = 0;
     421             : static const DBSMALLINT         null_SMALLINT = 0;
     422             : static const DBINT              null_INT = 0;
     423             : static const DBBIGINT           null_BIGINT = 0;
     424             : static const DBFLT8             null_FLT8 = 0;
     425             : static const DBREAL             null_REAL = 0;
     426             : 
     427             : static const DBCHAR             null_CHAR = '\0';
     428             : static const DBVARYCHAR         null_VARYCHAR = { 0, {0} };
     429             : 
     430             : static const DBDATETIME         null_DATETIME = { 0, 0 };
     431             : static const DBDATETIME4        null_SMALLDATETIME = { 0, 0 };
     432             : static const DBMONEY            null_MONEY = { 0, 0 };
     433             : static const DBMONEY4           null_SMALLMONEY = {0};
     434             : static const DBNUMERIC          null_NUMERIC = { 0, 0, {0} };
     435             : static const TDS_DATETIMEALL    null_DATETIMEALL = { 0, 0, 0, 0 };
     436             : 
     437             : static NULLREP default_null_representations[MAXBINDTYPES] = {
     438             :         /* CHARBIND          0  */        {         NULL, 0 }
     439             :         /* STRINGBIND        1  */      , {         NULL, 0 }
     440             :         /* NTBSTRINGBIND     2  */      , { (BYTE*) &null_CHAR, sizeof(null_CHAR) }
     441             :         /* VARYCHARBIND      3  */      , { (BYTE*) &null_VARYCHAR, sizeof(null_VARYCHAR) }
     442             :         /* VARYBINBIND       4  */      , { (BYTE*) &null_VARYCHAR, sizeof(null_VARYCHAR) }
     443             :         /* no such bind      5  */      , {         NULL, 0 }                   
     444             :         /* TINYBIND          6  */      , {         &null_TINYINT, sizeof(null_TINYINT) }
     445             :         /* SMALLBIND         7  */      , { (BYTE*) &null_SMALLINT, sizeof(null_SMALLINT) }
     446             :         /* INTBIND           8  */      , { (BYTE*) &null_INT, sizeof(null_INT) }
     447             :         /* FLT8BIND          9  */      , { (BYTE*) &null_FLT8, sizeof(null_FLT8) }
     448             :         /* REALBIND          10 */      , { (BYTE*) &null_REAL, sizeof(null_REAL) }
     449             :         /* DATETIMEBIND      11 */      , { (BYTE*) &null_DATETIME, sizeof(null_DATETIME) }
     450             :         /* SMALLDATETIMEBIND 12 */      , { (BYTE*) &null_SMALLDATETIME, sizeof(null_SMALLDATETIME) }
     451             :         /* MONEYBIND         13 */      , { (BYTE*) &null_MONEY, sizeof(null_MONEY) }
     452             :         /* SMALLMONEYBIND    14 */      , { (BYTE*) &null_SMALLMONEY, sizeof(null_SMALLMONEY) }
     453             :         /* BINARYBIND        15 */      , {         NULL, 0 }               
     454             :         /* BITBIND           16 */      , {         &null_BIT, sizeof(null_BIT) }
     455             :         /* NUMERICBIND       17 */      , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
     456             :         /* DECIMALBIND       18 */      , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
     457             :         /* SRCNUMERICBIND    19 */      , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
     458             :         /* SRCDECIMALBIND    20 */      , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
     459             :         /* DATEBIND          21 */      , { (BYTE*) &null_INT, sizeof(null_INT) }
     460             :         /* TIMEBIND          22 */      , { (BYTE*) &null_INT, sizeof(null_INT) }
     461             :         /* BIGDATETIMEBIND   23 */      , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
     462             :         /* BIGTIMEBIND       24 */      , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
     463             :         /*                   25 */      , {         NULL, 0 }
     464             :         /*                   26 */      , {         NULL, 0 }
     465             :         /*                   27 */      , {         NULL, 0 }
     466             :         /*                   28 */      , {         NULL, 0 }
     467             :         /*                   29 */      , {         NULL, 0 }
     468             :         /* BIGINTBIND        30 */      , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
     469             :         /* DATETIME2BIND     31 */      , { (BYTE*) &null_DATETIMEALL, sizeof(null_DATETIMEALL) }
     470             :         /* MAXBINDTYPES      32 */
     471             : };
     472             : 
     473             : static int
     474         122 : dbbindtype(int datatype)
     475             : {
     476         122 :         switch (datatype) {
     477             :         case SYBIMAGE:
     478             :         case SYBVARBINARY:
     479             :         case SYBBINARY:         return BINARYBIND;
     480             :         
     481           0 :         case SYBBIT:            return BITBIND;
     482             : 
     483          22 :         case SYBLONGCHAR:
     484             :         case SYBTEXT:
     485             :         case SYBVARCHAR:
     486          22 :         case SYBCHAR:           return NTBSTRINGBIND;
     487             :         
     488           0 :         case SYBDATETIME:       return DATETIMEBIND;
     489           0 :         case SYBDATETIME4:      return SMALLDATETIMEBIND;
     490             : 
     491           0 :         case SYBDATE:           return DATEBIND;
     492           0 :         case SYBTIME:           return TIMEBIND;
     493             : 
     494           0 :         case SYB5BIGDATETIME:   return BIGDATETIMEBIND;
     495           0 :         case SYB5BIGTIME:       return BIGTIMEBIND;
     496             : 
     497           0 :         case SYBDECIMAL:        return DECIMALBIND;
     498           0 :         case SYBNUMERIC:        return NUMERICBIND;
     499             :         
     500           0 :         case SYBFLT8:           return FLT8BIND;
     501           0 :         case SYBREAL:           return REALBIND;
     502             : 
     503           0 :         case SYBINT1:           return TINYBIND;
     504           0 :         case SYBINT2:           return SMALLBIND;
     505           0 :         case SYBINT4:           return INTBIND;
     506           0 :         case SYBINT8:           return BIGINTBIND;
     507             : 
     508           0 :         case SYBMONEY:          return MONEYBIND;
     509           0 :         case SYBMONEY4:         return SMALLMONEYBIND;
     510             : 
     511           0 :         case SYBMSDATE:
     512             :         case SYBMSTIME:
     513             :         case SYBMSDATETIME2:
     514             :         case SYBMSDATETIMEOFFSET:
     515           0 :                                 return DATETIME2BIND;
     516             : 
     517             :         default:
     518           0 :                 assert(0 == "no such datatype");
     519             :         }
     520             :         
     521             :         return 0;
     522             : }
     523             : 
     524             : /** \internal
     525             :  * dbbind() says: "Note that if varlen is 0, no padding takes place"
     526             :  * dbgetnull() will not pad varaddr unless varlen is positive.  
     527             :  * Vartype              Program Type    Padding         Terminator
     528             :  * -------------------  --------------  --------------  ----------
     529             :  * CHARBIND             DBCHAR          blanks          none
     530             :  * STRINGBIND           DBCHAR          blanks          \0
     531             :  * NTBSTRINGBIND        DBCHAR          none            \0
     532             :  * VARYCHARBIND         DBVARYCHAR      none            none
     533             :  * BOUNDARYBIND         DBCHAR          none            \0
     534             :  * SENSITIVITYBIND      DBCHAR          none            \0
     535             :  */
     536             : RETCODE
     537         420 : dbgetnull(DBPROCESS *dbproc, int bindtype, int varlen, BYTE* varaddr)
     538             : {
     539         420 :         NULLREP *pnullrep = default_null_representations + bindtype;
     540             : 
     541         420 :         tdsdump_log(TDS_DBG_FUNC, "dbgetnull(%p, %d, %d, %p)\n", dbproc, bindtype, varlen, varaddr);
     542             : 
     543         420 :         CHECK_PARAMETER(varaddr, SYBENULL, FAIL);
     544         420 :         CHECK_PARAMETER(0 <= bindtype && bindtype < MAXBINDTYPES, SYBEBTYP, FAIL);
     545             : 
     546             :         /* dbproc can be NULL */
     547         420 :         if (NULL != dbproc) {
     548         320 :                 assert(dbproc->nullreps);
     549         320 :                 pnullrep = dbproc->nullreps + bindtype;
     550             :         }
     551             :         
     552             :         /* 
     553             :          * Fixed types: ignore varlen
     554             :          * Other types: ignore varlen if <= 0, else varlen must be >= pnullrep->len.
     555             :          */
     556             :         switch (bindtype) {
     557          10 :         case DATETIMEBIND:
     558             :         case DATETIME2BIND:
     559             :         case DECIMALBIND:
     560             :         case SRCDECIMALBIND:
     561             :         case FLT8BIND:
     562             :         case INTBIND:
     563             :         case MONEYBIND:
     564             :         case NUMERICBIND:
     565             :         case SRCNUMERICBIND:
     566             :         case REALBIND:
     567             :         case SMALLBIND:
     568             :         case SMALLDATETIMEBIND:
     569             :         case SMALLMONEYBIND:
     570             :         case TINYBIND:
     571             :         case BIGINTBIND:
     572             :         case BITBIND:
     573             :         case TIMEBIND:
     574             :         case DATEBIND:
     575             :         case BIGDATETIMEBIND:
     576             :         case BIGTIMEBIND:
     577          10 :                 memcpy(varaddr, pnullrep->bindval, pnullrep->len);
     578          10 :                 return SUCCEED;
     579         410 :         case CHARBIND:
     580             :         case STRINGBIND:
     581             :         case NTBSTRINGBIND:
     582             :         case BINARYBIND:
     583             :         case VARYCHARBIND:
     584             :         case VARYBINBIND:
     585         410 :                 if (pnullrep->bindval && (varlen <= 0 || (size_t)varlen >= pnullrep->len)) {
     586         232 :                         memcpy(varaddr, pnullrep->bindval, pnullrep->len);
     587             :                 }
     588             :                 break;
     589           0 :         default:
     590           0 :                 dbperror(dbproc, SYBEBTYP, 0);
     591           0 :                 return FAIL;
     592             :         }
     593             : 
     594             :         /* 
     595             :          * For variable-length types, nonpositive varlen indicates 
     596             :          * buffer is "big enough" but also not to pad.
     597             :          * Apply terminator (if applicable) and go home.  
     598             :          */
     599         410 :         if (varlen <= 0) {
     600         258 :                 varlen = pnullrep->len;
     601         258 :                 switch (bindtype) {
     602         188 :                 case STRINGBIND:
     603             :                 case NTBSTRINGBIND:
     604         188 :                         ++varlen;
     605         188 :                         break;
     606             : #if 0
     607             :                 case BOUNDARYBIND:
     608             :                 case SENSITIVITYBIND:
     609             : #endif
     610             :                 }
     611             :         }
     612             :         
     613         410 :         if (varlen < (long)pnullrep->len) {
     614           0 :                 tdsdump_log(TDS_DBG_FUNC, "dbgetnull: error: not setting varaddr(%p) because %d < %lu\n",
     615             :                                         varaddr, varlen, (unsigned long int) pnullrep->len);
     616             :                 return FAIL;
     617             :         } 
     618             :                 
     619         410 :         tdsdump_log(TDS_DBG_FUNC, "varaddr(%p) varlen %d < %lu?\n",
     620             :                                 varaddr, varlen, (unsigned long int) pnullrep->len);
     621             : 
     622         410 :         assert(varlen >= 0);
     623             : 
     624             :         /*
     625             :          * CHARBIND             Empty string (padded with blanks)
     626             :          * STRINGBIND           Empty string (padded with blanks, null-terminated)
     627             :          * NTBSTRINGBIND        Empty string (unpadded, null-terminated)
     628             :          * BINARYBIND           Empty array (padded with zeros)
     629             :          */
     630         410 :         varaddr += pnullrep->len;
     631         410 :         varlen  -= (int)pnullrep->len;
     632         410 :         if (varlen > 0) {
     633         326 :                 switch (bindtype) {
     634          48 :                 case CHARBIND:
     635          48 :                         memset(varaddr, ' ', varlen);
     636          48 :                         break;
     637          40 :                 case STRINGBIND:
     638          40 :                         memset(varaddr, ' ', varlen);
     639          40 :                         varaddr[varlen-1] = '\0';
     640          40 :                         break;
     641         148 :                 case NTBSTRINGBIND:
     642         148 :                         varaddr[0] = '\0';
     643         148 :                         break;
     644          90 :                 case BINARYBIND:
     645          90 :                         memset(varaddr, 0, varlen);
     646          90 :                         break;
     647             :                 case VARYCHARBIND:
     648             :                 case VARYBINBIND:
     649             :                         break;
     650             :                 default:
     651           0 :                         assert(!"unknown bindtype");
     652             :                 }
     653             :         }       
     654             :         return SUCCEED;
     655             : }
     656             : 
     657             : /**
     658             :  * \ingroup dblib_core
     659             :  * \brief Initialize db-lib.  
     660             :  *
     661             :  * \remarks Call this function before trying to use db-lib in any way.  
     662             :  * Allocates various internal structures and reads \c locales.conf (if any) to determine the default
     663             :  * date format.  
     664             :  * \retval SUCCEED normal.  
     665             :  * \retval FAIL cannot allocate an array of \c TDS_MAX_CONN \c TDSSOCKET pointers.  
     666             :  */
     667             : RETCODE
     668         502 : dbinit(void)
     669             : {
     670         502 :         _dblib_err_handler = default_err_handler;
     671             : 
     672         502 :         tds_mutex_lock(&dblib_mutex);
     673             : 
     674         502 :         tdsdump_log(TDS_DBG_FUNC, "dbinit(void)\n");
     675             : 
     676         502 :         if (++g_dblib_ctx.ref_count != 1) {
     677           0 :                 tds_mutex_unlock(&dblib_mutex);
     678           0 :                 return SUCCEED;
     679             :         }
     680             :         /* 
     681             :          * DBLIBCONTEXT stores a list of current connections so they may be closed with dbexit() 
     682             :          */
     683             : 
     684         502 :         g_dblib_ctx.connection_list = tds_new0(TDSSOCKET *, TDS_MAX_CONN);
     685         502 :         if (g_dblib_ctx.connection_list == NULL) {
     686           0 :                 tdsdump_log(TDS_DBG_FUNC, "dbinit: out of memory\n");
     687           0 :                 tds_mutex_unlock(&dblib_mutex);
     688           0 :                 return FAIL;
     689             :         }
     690         502 :         g_dblib_ctx.connection_list_size = TDS_MAX_CONN;
     691         502 :         g_dblib_ctx.connection_list_size_represented = TDS_MAX_CONN;
     692             : 
     693         502 :         g_dblib_ctx.login_timeout = -1;
     694         502 :         g_dblib_ctx.query_timeout = -1;
     695             : 
     696         502 :         tds_mutex_unlock(&dblib_mutex);
     697             : 
     698         502 :         dblib_get_tds_ctx();
     699             : 
     700         502 :         return SUCCEED;
     701             : }
     702             : 
     703             : /**
     704             :  * \ingroup dblib_core
     705             :  * \brief Allocate a \c LOGINREC structure.  
     706             :  *
     707             :  * \remarks A \c LOGINREC structure is passed to \c dbopen() to create a connection to the database. 
     708             :  *      Does not communicate to the server; interacts strictly with library.  
     709             :  * \retval NULL the \c LOGINREC cannot be allocated.
     710             :  * \retval LOGINREC* to valid memory, otherwise.  
     711             :  */
     712             : LOGINREC *
     713         832 : dblogin(void)
     714             : {
     715             :         LOGINREC *loginrec;
     716             : 
     717         832 :         tdsdump_log(TDS_DBG_FUNC, "dblogin(void)\n");
     718             : 
     719         832 :         if ((loginrec = tds_new0(LOGINREC, 1)) == NULL) {
     720           0 :                 dbperror(NULL, SYBEMEM, errno);
     721           0 :                 return NULL;
     722             :         }
     723         832 :         if ((loginrec->tds_login = tds_alloc_login(true)) == NULL) {
     724           0 :                 dbperror(NULL, SYBEMEM, errno);
     725           0 :                 free(loginrec);
     726           0 :                 return NULL;
     727             :         }
     728             : 
     729             :         /* set default values for loginrec */
     730         832 :         if (!tds_set_library(loginrec->tds_login, "DB-Library")) {
     731           0 :                 dbperror(NULL, SYBEMEM, errno);
     732           0 :                 tds_free_login(loginrec->tds_login);
     733           0 :                 free(loginrec);
     734           0 :                 return NULL;
     735             :         }
     736             : 
     737             :         return loginrec;
     738             : }
     739             : 
     740             : /**
     741             :  * \ingroup dblib_core
     742             :  * \brief free the \c LOGINREC
     743             :  *
     744             :  */
     745             : void
     746         802 : dbloginfree(LOGINREC * login)
     747             : {
     748         802 :         tdsdump_log(TDS_DBG_FUNC, "dbloginfree(%p)\n", login);
     749             : 
     750         802 :         if (login) {
     751         802 :                 tds_free_login(login->tds_login);
     752         802 :                 free(login);
     753             :         }
     754         802 : }
     755             : 
     756             : /** \internal
     757             :  * \ingroup dblib_internal 
     758             :  * \brief Set the value of a string in a \c LOGINREC structure.  
     759             :  *
     760             :  * Called by various macros to populate \a login.  
     761             :  * \param login the \c LOGINREC* to modify.
     762             :  * \param value the value to set it to.  
     763             :  * \param which the field to set.  
     764             :  * \retval SUCCEED the value was set.
     765             :  * \retval FAIL \c DBSETHID or other invalid \a which was tried.  
     766             :  */
     767             : RETCODE
     768        2836 : dbsetlname(LOGINREC * login, const char *value, int which)
     769             : {
     770             :         bool copy_ret;
     771        2836 :         const char *value_nonull = value ? value : "";
     772             : 
     773        2836 :         tdsdump_log(TDS_DBG_FUNC, "dbsetlname(%p, %s, %d)\n", login, value, which);
     774             : 
     775        2836 :         if (login == NULL) {
     776           0 :                 dbperror(NULL, SYBEASNL, 0);
     777           0 :                 return FAIL;
     778             :         }
     779             : 
     780        2836 :         if (TDS_MAX_LOGIN_STR_SZ < strlen(value_nonull)) {
     781           0 :                 dbperror(NULL, SYBENTLL, 0);
     782           0 :                 return FAIL;
     783             :         }
     784             : 
     785        2836 :         switch (which) {
     786         330 :         case DBSETHOST:
     787         330 :                 copy_ret = tds_set_host(login->tds_login, value_nonull);
     788         330 :                 break;
     789         822 :         case DBSETUSER:
     790         822 :                 copy_ret = tds_set_user(login->tds_login, value_nonull);
     791         822 :                 break;
     792         822 :         case DBSETPWD:
     793         822 :                 copy_ret = tds_set_passwd(login->tds_login, value_nonull);
     794         822 :                 break;
     795         822 :         case DBSETAPP:
     796         822 :                 copy_ret = tds_set_app(login->tds_login, value_nonull);
     797         822 :                 break;
     798          10 :         case DBSETCHARSET:
     799             :                 /* TODO NULL == no conversion desired */
     800          10 :                 copy_ret = tds_set_client_charset(login->tds_login, value_nonull);
     801          10 :                 break;
     802           0 :         case DBSETNATLANG:
     803           0 :                 copy_ret = tds_set_language(login->tds_login, value_nonull);
     804           0 :                 break;
     805          30 :         case DBSETDBNAME:
     806          30 :                 copy_ret = !!tds_dstr_copy(&login->tds_login->database, value_nonull);
     807          30 :                 break;
     808           0 :         case DBSETSERVERPRINCIPAL:
     809           0 :                 copy_ret = !!tds_dstr_copy(&login->tds_login->server_spn, value_nonull);
     810           0 :                 break;
     811           0 :         case DBSETENCRYPTION:
     812           0 :                 copy_ret = tds_parse_conf_section(TDS_STR_ENCRYPTION, value_nonull, login->tds_login);
     813           0 :                 break;
     814           0 :         default:
     815           0 :                 dbperror(NULL, SYBEASUL, 0); /* Attempt to set unknown LOGINREC field */
     816           0 :                 return FAIL;
     817             :                 break;
     818             :         }
     819             : 
     820        2836 :         if (!copy_ret)
     821             :                 return FAIL;
     822        2836 :         return SUCCEED;
     823             : }
     824             : 
     825             : /** \internal
     826             :  * \ingroup dblib_internal
     827             :  * \brief Set an integer value in a \c LOGINREC structure.  
     828             :  *
     829             :  * Called by various macros to populate \a login.  
     830             :  * \param login the \c LOGINREC* to modify.
     831             :  * \param value the value to set it to.  
     832             :  * \param which the field to set.  
     833             :  * \retval SUCCEED the value was set.
     834             :  * \retval FAIL anything other than \c DBSETPACKET was passed for \a which.  
     835             :  */
     836             : RETCODE
     837          10 : dbsetllong(LOGINREC * login, long value, int which)
     838             : {
     839          10 :         tdsdump_log(TDS_DBG_FUNC, "dbsetllong(%p, %ld, %d)\n", login, value, which);
     840             : 
     841          10 :         if( login == NULL ) {
     842           0 :                 dbperror(NULL, SYBEASNL, 0);
     843           0 :                 return FAIL;
     844             :         }
     845             : 
     846          10 :         switch (which) {
     847          10 :         case DBSETPACKET:
     848          10 :                 if (0 <= value && value <= 999999) { 
     849           0 :                         tds_set_packet(login->tds_login, value);
     850           0 :                         return SUCCEED;
     851             :                 }
     852          10 :                 dbperror(0, SYBEBADPK, 0, (int) value, (int) login->tds_login->block_size);
     853          10 :                 return FAIL;
     854             :                 break;
     855           0 :         default:
     856           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetllong() which = %d\n", which);
     857             :                 return FAIL;
     858             :                 break;
     859             :         }
     860             : }
     861             : 
     862             : /** \internal
     863             :  * \ingroup dblib_internal
     864             :  * \brief Set an integer value in a \c LOGINREC structure.  
     865             :  *
     866             :  * Called by various macros to populate \a login.  
     867             :  * \param login the \c LOGINREC* to modify.
     868             :  * \param value the value to set it to.  
     869             :  * \param which the field to set.  
     870             :  * \retval SUCCEED the value was set.
     871             :  * \retval FAIL if invalid field in \a which or invalid \a value.  
     872             :  */
     873             : RETCODE
     874           0 : dbsetlshort(LOGINREC * login, int value, int which)
     875             : {
     876           0 :         tdsdump_log(TDS_DBG_FUNC, "dbsetlshort(%p, %d, %d)\n", login, value, which);
     877             : 
     878           0 :         if( login == NULL ) {
     879           0 :                 dbperror(NULL, SYBEASNL, 0);
     880           0 :                 return FAIL;
     881             :         }
     882             : 
     883           0 :         switch (which) {
     884           0 :         case DBSETPORT:
     885           0 :                 tds_set_port(login->tds_login, value);
     886           0 :                 return SUCCEED;
     887             :         /* case DBSETHIER: */
     888           0 :         default:
     889           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetlshort() which = %d\n", which);
     890             :                 return FAIL;
     891             :                 break;
     892             :         }
     893             : }
     894             : 
     895             : /** \internal
     896             :  * \ingroup dblib_internal
     897             :  * \brief Set a boolean value in a \c LOGINREC structure.  
     898             :  *
     899             :  * Called by various macros to populate \a login.  
     900             :  * \param login the \c LOGINREC* to modify.
     901             :  * \param value the value to set it to.  
     902             :  * \param which the field to set. 
     903             :  * \remark Only DBSETBCP is implemented.  
     904             :  * \retval SUCCEED the value was set.
     905             :  * \retval FAIL invalid value passed for \a which.  
     906             :  * \todo DBSETNOSHORT, DBSETENCRYPT, DBSETLABELED
     907             :  */
     908             : RETCODE
     909          50 : dbsetlbool(LOGINREC * login, int value, int which)
     910             : {
     911             :         bool b_value;
     912             : 
     913          50 :         tdsdump_log(TDS_DBG_FUNC, "dbsetlbool(%p, %d, %d)\n", login, value, which);
     914             : 
     915          50 :         if (login == NULL) {
     916           0 :                 dbperror(NULL, SYBEASNL, 0);
     917           0 :                 return FAIL;
     918             :         }
     919             : 
     920          50 :         b_value = (value != 0);
     921             : 
     922          50 :         switch (which) {
     923          50 :         case DBSETBCP:
     924          50 :                 tds_set_bulk(login->tds_login, b_value);
     925          50 :                 return SUCCEED;
     926           0 :         case DBSETUTF16:
     927           0 :                 login->tds_login->use_utf16 = b_value;
     928           0 :                 return SUCCEED;
     929           0 :         case DBSETNTLMV2:
     930           0 :                 login->tds_login->use_ntlmv2 = b_value;
     931           0 :                 login->tds_login->use_ntlmv2_specified = 1;
     932           0 :                 return SUCCEED;
     933           0 :         case DBSETREADONLY:
     934           0 :                 login->tds_login->readonly_intent = b_value;
     935           0 :                 return SUCCEED;
     936           0 :         case DBSETNETWORKAUTH:
     937           0 :                 login->network_auth = b_value;
     938           0 :                 return SUCCEED;
     939           0 :         case DBSETMUTUALAUTH:
     940           0 :                 login->tds_login->mutual_authentication = b_value;
     941           0 :                 return SUCCEED;
     942           0 :         case DBSETDELEGATION:
     943           0 :                 login->tds_login->gssapi_use_delegation = b_value;
     944           0 :                 return SUCCEED;
     945           0 :         case DBSETENCRYPT:
     946             :         case DBSETLABELED:
     947             :         default:
     948           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetlbool() which = %d\n", which);
     949             :                 return FAIL;
     950             :                 break;
     951             :         }
     952             : }
     953             : 
     954             : /**
     955             :  * \ingroup dblib_core
     956             :  * \brief Set TDS version for future connections
     957             :  *
     958             :  */
     959             : RETCODE 
     960           0 : dbsetlversion(LOGINREC * login, BYTE version)
     961             : {
     962           0 :         tdsdump_log(TDS_DBG_FUNC, "dbsetlversion(%p, %x)\n", login, version);
     963             : 
     964           0 :         if( login == NULL ) {
     965           0 :                 dbperror(NULL, SYBEASNL, 0);
     966           0 :                 return FAIL;
     967             :         }
     968             : 
     969           0 :         assert(login->tds_login != NULL);
     970             : 
     971           0 :         switch (version) {
     972           0 :         case DBVERSION_UNKNOWN:
     973           0 :                 tds_set_version(login->tds_login, 0, 0);
     974           0 :                 return SUCCEED;
     975           0 :         case DBVERSION_42:
     976           0 :                 tds_set_version(login->tds_login, 4, 2);
     977           0 :                 return SUCCEED;
     978           0 :         case DBVERSION_100:
     979           0 :                 tds_set_version(login->tds_login, 5, 0);
     980           0 :                 return SUCCEED;
     981           0 :         case DBVERSION_70:
     982           0 :                 tds_set_version(login->tds_login, 7, 0);
     983           0 :                 return SUCCEED;
     984           0 :         case DBVERSION_71:
     985           0 :                 tds_set_version(login->tds_login, 7, 1);
     986           0 :                 return SUCCEED;
     987           0 :         case DBVERSION_72:
     988           0 :                 tds_set_version(login->tds_login, 7, 2);
     989           0 :                 return SUCCEED;
     990           0 :         case DBVERSION_73:
     991           0 :                 tds_set_version(login->tds_login, 7, 3);
     992           0 :                 return SUCCEED;
     993           0 :         case DBVERSION_74:
     994           0 :                 tds_set_version(login->tds_login, 7, 4);
     995           0 :                 return SUCCEED;
     996             :         }
     997             : 
     998             :         return FAIL;
     999             : }
    1000             : 
    1001             : static void
    1002       36072 : dbstring_free(DBSTRING ** dbstrp)
    1003             : {
    1004             :         DBSTRING *curr, *next;
    1005             :         /* tdsdump_log(TDS_DBG_FUNC, "dbstring_free(%p)\n", dbstrp); */
    1006             : 
    1007       36072 :         if (!dbstrp)
    1008             :                 return;
    1009             : 
    1010       36072 :         curr = *dbstrp;
    1011       36072 :         *dbstrp = NULL;
    1012       77178 :         for (; curr; ) {
    1013        5034 :                 next = curr->strnext;
    1014        5034 :                 free(curr->strtext);
    1015        5034 :                 free(curr);
    1016        5034 :                 curr = next;
    1017             :         }
    1018             : }
    1019             : 
    1020             : static RETCODE
    1021        5214 : dbstring_concat(DBSTRING ** dbstrp, const char *p)
    1022             : {
    1023        5214 :         DBSTRING **strp = dbstrp;
    1024             : 
    1025             :         /* tdsdump_log(TDS_DBG_FUNC, "dbstring_concat(%p, %s)\n", *dbstrp, p); */
    1026             : 
    1027       10428 :         while (*strp != NULL) {
    1028           0 :                 strp = &((*strp)->strnext);
    1029             :         }
    1030        5214 :         if ((*strp = tds_new(DBSTRING, 1)) == NULL) {
    1031           0 :                 dbperror(NULL, SYBEMEM, errno);
    1032           0 :                 return FAIL;
    1033             :         }
    1034        5214 :         (*strp)->strtotlen = (DBINT)strlen(p);
    1035        5214 :         if (((*strp)->strtext = tds_new(BYTE, (*strp)->strtotlen)) == NULL) {
    1036           0 :                 TDS_ZERO_FREE(*strp);
    1037           0 :                 dbperror(NULL, SYBEMEM, errno);
    1038           0 :                 return FAIL;
    1039             :         }
    1040        5214 :         memcpy((*strp)->strtext, p, (*strp)->strtotlen);
    1041        5214 :         (*strp)->strnext = NULL;
    1042        5214 :         return SUCCEED;
    1043             : }
    1044             : 
    1045             : static RETCODE
    1046             : dbstring_assign(DBSTRING ** dbstrp, const char *p)
    1047             : {
    1048             :         /* tdsdump_log(TDS_DBG_FUNC, "dbstring_assign(%p, %s)\n", *dbstrp, p); */
    1049             : 
    1050        5214 :         dbstring_free(dbstrp);
    1051        5214 :         return dbstring_concat(dbstrp, p);
    1052             : }
    1053             : 
    1054             : static DBINT
    1055             : dbstring_length(DBSTRING * dbstr)
    1056             : {
    1057           0 :         DBINT len = 0;
    1058             :         DBSTRING *next;
    1059             : 
    1060             :         /* tdsdump_log(TDS_DBG_FUNC, "dbstring_length(%p)\n", dbstr); */
    1061             : 
    1062           0 :         for (next = dbstr; next != NULL; next = next->strnext) {
    1063           0 :                 len += next->strtotlen;
    1064             :         }
    1065             :         return len;
    1066             : }
    1067             : 
    1068             : static int
    1069             : dbstring_getchar(DBSTRING * dbstr, int i)
    1070             : {
    1071             : 
    1072             :         /* tdsdump_log(TDS_DBG_FUNC, "dbstring_getchar(%p, %d)\n", dbstr, i); */
    1073             : 
    1074        6160 :         if (dbstr == NULL) {
    1075             :                 return -1;
    1076             :         }
    1077        4600 :         if (i < 0) {
    1078             :                 return -1;
    1079             :         }
    1080        4600 :         if (i < dbstr->strtotlen) {
    1081        3040 :                 return dbstr->strtext[i];
    1082             :         }
    1083        1560 :         return dbstring_getchar(dbstr->strnext, i - dbstr->strtotlen);
    1084             : }
    1085             : 
    1086             : static char *
    1087           0 : dbstring_get(DBSTRING * dbstr)
    1088             : {
    1089             :         DBSTRING *next;
    1090             :         int len;
    1091             :         char *ret;
    1092             :         char *cp;
    1093             : 
    1094             :         /* tdsdump_log(TDS_DBG_FUNC, "dbstring_get(%p)\n", dbstr); */
    1095             : 
    1096           0 :         if (dbstr == NULL) {
    1097             :                 return NULL;
    1098             :         }
    1099           0 :         len = dbstring_length(dbstr);
    1100           0 :         if ((ret = tds_new(char, len + 1)) == NULL) {
    1101           0 :                 dbperror(NULL, SYBEMEM, errno);
    1102           0 :                 return NULL;
    1103             :         }
    1104             :         cp = ret;
    1105           0 :         for (next = dbstr; next != NULL; next = next->strnext) {
    1106           0 :                 memcpy(cp, next->strtext, next->strtotlen);
    1107           0 :                 cp += next->strtotlen;
    1108             :         }
    1109           0 :         *cp = '\0';
    1110           0 :         return ret;
    1111             : }
    1112             : 
    1113             : static const char *const opttext[DBNUMOPTIONS] = {
    1114             :         "parseonly",
    1115             :         "estimate",
    1116             :         "showplan",
    1117             :         "noexec",
    1118             :         "arithignore",
    1119             :         "nocount",
    1120             :         "arithabort",
    1121             :         "textlimit",
    1122             :         "browse",
    1123             :         "offsets",
    1124             :         "statistics",
    1125             :         "errlvl",
    1126             :         "confirm",
    1127             :         "spid",
    1128             :         "buffer",
    1129             :         "noautofree",
    1130             :         "rowcount",
    1131             :         "textsize",
    1132             :         "language",
    1133             :         "dateformat",
    1134             :         "prpad",
    1135             :         "prcolsep",
    1136             :         "prlinelen",
    1137             :         "prlinesep",
    1138             :         "lfconvert",
    1139             :         "datefirst",
    1140             :         "chained",
    1141             :         "fipsflagger",
    1142             :         "transaction isolation level",
    1143             :         "auth",
    1144             :         "identity_insert",
    1145             :         "no_identity_column",
    1146             :         "cnv_date2char_short",
    1147             :         "client cursors",
    1148             :         "set time",
    1149             :         "quoted_identifier"
    1150             : };
    1151             : 
    1152             : static DBOPTION *
    1153         864 : init_dboptions(void)
    1154             : {
    1155             :         DBOPTION *dbopts;
    1156             :         int i;
    1157             : 
    1158         864 :         if ((dbopts = tds_new0(DBOPTION, DBNUMOPTIONS)) == NULL) {
    1159           0 :                 dbperror(NULL, SYBEMEM, errno);
    1160           0 :                 return NULL;
    1161             :         }
    1162       31104 :         for (i = 0; i < DBNUMOPTIONS; i++) {
    1163       31104 :                 dbopts[i].text = opttext[i];
    1164       31104 :                 dbopts[i].param = NULL;
    1165       31104 :                 dbopts[i].factive = FALSE;
    1166             :         }
    1167        1728 :         dbstring_assign(&(dbopts[DBPRPAD].param), " ");
    1168        1728 :         dbstring_assign(&(dbopts[DBPRCOLSEP].param), " ");
    1169        1728 :         dbstring_assign(&(dbopts[DBPRLINELEN].param), "80");
    1170        1728 :         dbstring_assign(&(dbopts[DBPRLINESEP].param), "\n");
    1171        1728 :         dbstring_assign(&(dbopts[DBCLIENTCURSORS].param), " ");
    1172        1728 :         dbstring_assign(&(dbopts[DBSETTIME].param), " ");
    1173         864 :         return dbopts;
    1174             : }
    1175             : 
    1176             : /** \internal
    1177             :  * \ingroup dblib_internal
    1178             :  * \brief Form a connection with the server.
    1179             :  *   
    1180             :  * Called by the \c dbopen() macro, normally.  If FreeTDS was configured with \c --enable-msdblib, this
    1181             :  * function is called by (exported) \c dbopen() function.  \c tdsdbopen is so-named to avoid
    1182             :  * namespace conflicts with other database libraries that use the same function name.  
    1183             :  * \param login \c LOGINREC* carrying the account information.
    1184             :  * \param server name of the dataserver to connect to.  
    1185             :  * \return valid pointer on successful login.  
    1186             :  * \retval NULL insufficient memory, unable to connect for any reason.
    1187             :  * \sa dbopen()
    1188             :  * \todo use \c asprintf() to avoid buffer overflow.
    1189             :  * \todo separate error messages for \em no-such-server and \em no-such-user. 
    1190             :  */
    1191             : DBPROCESS *
    1192         864 : tdsdbopen(LOGINREC * login, const char *server, int msdblib)
    1193             : {
    1194         864 :         DBPROCESS *dbproc = NULL;
    1195             :         TDSLOGIN *connection;
    1196             :         int add_connection_res;
    1197             : 
    1198         864 :         tds_dir_char *tdsdump = tds_dir_getenv(TDS_DIR("TDSDUMP"));
    1199         864 :         if (tdsdump && *tdsdump) {
    1200           0 :                 tdsdump_open(tdsdump);
    1201           0 :                 tdsdump_log(TDS_DBG_FUNC, "tdsdbopen(%p, %s, [%s])\n", login, server? server : "0x0", msdblib? "microsoft" : "sybase");
    1202             :         }
    1203             : 
    1204             :         /*
    1205             :          * Sybase supports the DSQUERY environment variable and falls back to "SYBASE" if server is NULL. 
    1206             :          * Microsoft uses a NULL or "" server to indicate a local server.  
    1207             :          * FIXME: support local server for win32.  
    1208             :          */
    1209         864 :         if (!server && !msdblib) {
    1210           0 :                 if ((server = getenv("TDSQUERY")) == NULL)
    1211           0 :                         if ((server = getenv("DSQUERY")) == NULL)
    1212           0 :                                 server = "SYBASE";
    1213           0 :                 tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: servername set to %s\n", server);
    1214             :         }
    1215             : 
    1216         864 :         if ((dbproc = tds_new0(DBPROCESS, 1)) == NULL) {
    1217           0 :                 dbperror(NULL, SYBEMEM, errno);
    1218           0 :                 return NULL;
    1219             :         }
    1220         864 :         dbproc->msdblib = msdblib;
    1221             : 
    1222         864 :         dbproc->dbopts = init_dboptions();
    1223         864 :         if (dbproc->dbopts == NULL) {
    1224           0 :                 free(dbproc);
    1225           0 :                 return NULL;
    1226             :         }
    1227         864 :         tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: dbproc->dbopts = %p\n", dbproc->dbopts);
    1228             :         
    1229         864 :         dbproc->dboptcmd = NULL;
    1230         864 :         dbproc->avail_flag = TRUE;
    1231         864 :         dbproc->command_state = DBCMDNONE;
    1232             : 
    1233         864 :         if (!tds_set_server(login->tds_login, server)) {
    1234           0 :                 dbperror(NULL, SYBEMEM, 0);
    1235           0 :                 free(dbproc);
    1236           0 :                 return NULL;
    1237             :         }
    1238         864 :         tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: tds_set_server(%p, \"%s\")\n", login->tds_login, server);
    1239             : 
    1240         864 :         if ((dbproc->tds_socket = tds_alloc_socket(dblib_get_tds_ctx(), 512)) == NULL ){
    1241           0 :                 dbperror(NULL, SYBEMEM, 0);
    1242           0 :                 free(dbproc);
    1243           0 :                 return NULL;
    1244             :         }
    1245             :         
    1246             : 
    1247         864 :         tds_set_parent(dbproc->tds_socket, dbproc);
    1248             : 
    1249         864 :         dbproc->tds_socket->env_chg_func = db_env_chg;
    1250         864 :         dbproc->envchange_rcv = 0;
    1251             : 
    1252         864 :         dbproc->dbcurdb[0] = '\0';
    1253         864 :         dbproc->servcharset[0] = '\0';
    1254             : 
    1255         864 :         tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: About to call tds_read_config_info...\n");
    1256             : 
    1257         864 :         tds_mutex_lock(&dblib_mutex);
    1258         864 :         add_connection_res = dblib_add_connection(&g_dblib_ctx, dbproc->tds_socket);
    1259         864 :         tds_mutex_unlock(&dblib_mutex);
    1260         864 :         if (add_connection_res) {
    1261          10 :                 dbperror(dbproc, SYBEDBPS, 0);
    1262          10 :                 dbclose(dbproc);
    1263          10 :                 return NULL;
    1264             :         }
    1265             : 
    1266         854 :         connection = tds_read_config_info(dbproc->tds_socket, login->tds_login, g_dblib_ctx.tds_ctx->locale);
    1267         854 :         if (!connection) {
    1268           0 :                 dbclose(dbproc);
    1269           0 :                 return NULL;
    1270             :         }
    1271         854 :         connection->option_flag2 &= ~TDS_ODBC_ON;        /* we're not an ODBC driver */
    1272         854 :         tds_fix_login(connection);              /* initialize from Environment variables */
    1273             : 
    1274         854 :         dbproc->chkintr = NULL;
    1275         854 :         dbproc->hndlintr = NULL;
    1276             : 
    1277         854 :         tds_mutex_lock(&dblib_mutex);
    1278             : 
    1279             :         /* override connection timeout if dbsetlogintime() was called */
    1280         854 :         if (g_dblib_ctx.login_timeout > 0) {
    1281          20 :                 connection->connect_timeout = g_dblib_ctx.login_timeout;
    1282             :         }
    1283             : 
    1284             :         /* override query timeout if dbsettime() was called */
    1285         854 :         if (g_dblib_ctx.query_timeout > 0) {
    1286          10 :                 connection->query_timeout = g_dblib_ctx.query_timeout;
    1287             :         }
    1288             : 
    1289         854 :         tds_mutex_unlock(&dblib_mutex);
    1290             : 
    1291         854 :         tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: Calling tds_connect_and_login(%p, %p)\n",
    1292             :                 dbproc->tds_socket, connection);
    1293             : 
    1294         854 :         if (login->network_auth) {
    1295           0 :                 tds_dstr_empty(&connection->user_name);
    1296           0 :                 tds_dstr_empty(&connection->password);
    1297             :         }
    1298             : 
    1299         854 :         if (TDS_FAILED(tds_connect_and_login(dbproc->tds_socket, connection))) {
    1300           0 :                 tdsdump_log(TDS_DBG_ERROR, "tdsdbopen: tds_connect_and_login failed for \"%s\"!\n",
    1301           0 :                         tds_dstr_cstr(&connection->server_name));
    1302           0 :                 tds_free_login(connection);
    1303           0 :                 dbclose(dbproc);
    1304           0 :                 return NULL;
    1305             :         }
    1306         854 :         tds_free_login(connection);
    1307             : 
    1308         854 :         dbproc->dbbuf = NULL;
    1309         854 :         dbproc->dbbufsz = 0;
    1310             : 
    1311             :         /* set the DBBUFFER capacity to nil */
    1312         854 :         buffer_set_capacity(dbproc, 0);
    1313             : 
    1314         854 :         memcpy(dbproc->nullreps, default_null_representations, sizeof(default_null_representations));
    1315             : 
    1316         854 :         tds_mutex_lock(&dblib_mutex);
    1317             : 
    1318         854 :         if (g_dblib_ctx.recftos_filename != NULL) {
    1319           0 :                 char *temp_filename = NULL;
    1320           0 :                 const int len = asprintf(&temp_filename, "%s.%d", 
    1321             :                                          g_dblib_ctx.recftos_filename, g_dblib_ctx.recftos_filenum);
    1322           0 :                 if (len >= 0) {
    1323           0 :                         dbproc->ftos = fopen(temp_filename, "w");
    1324           0 :                         free(temp_filename);
    1325           0 :                         if (dbproc->ftos != NULL) {
    1326             :                                 char timestr[256];
    1327           0 :                                 fprintf(dbproc->ftos, "/* dbopen() at %s */\n", _dbprdate(timestr));
    1328           0 :                                 fflush(dbproc->ftos);
    1329           0 :                                 g_dblib_ctx.recftos_filenum++;
    1330             :                         }
    1331             :                 }
    1332             :         }
    1333             :         
    1334         854 :         tds_mutex_unlock(&dblib_mutex);
    1335             : 
    1336         854 :         tdsdump_log(TDS_DBG_FUNC, "tdsdbopen: Returning dbproc = %p\n", dbproc);
    1337             : 
    1338             :         return dbproc;
    1339             : }
    1340             : 
    1341             : /**
    1342             :  * \ingroup dblib_core
    1343             :  * \brief \c printf-like way to form SQL to send to the server.  
    1344             :  *
    1345             :  * Forms a command string and writes to the command buffer with dbcmd().  
    1346             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1347             :  * \param fmt <tt> man vasprintf</tt> for details.  
    1348             :  * \retval SUCCEED success.
    1349             :  * \retval FAIL insufficient memory, or dbcmd() failed.
    1350             :  * \sa dbcmd(), dbfreebuf(), dbgetchar(), dbopen(), dbstrcpy(), dbstrlen().
    1351             :  */
    1352             : RETCODE
    1353         860 : dbfcmd(DBPROCESS * dbproc, const char *fmt, ...)
    1354             : {
    1355             :         va_list ap;
    1356             :         char *s;
    1357             :         int len;
    1358             :         RETCODE ret;
    1359             : 
    1360         860 :         tdsdump_log(TDS_DBG_FUNC, "dbfcmd(%p, %s, ...)\n", dbproc, fmt);
    1361         860 :         CHECK_CONN(FAIL);
    1362         860 :         CHECK_NULP(fmt, "dbfcmd", 2, FAIL);
    1363             :         
    1364         860 :         va_start(ap, fmt);
    1365         860 :         len = vasprintf(&s, fmt, ap);
    1366         860 :         va_end(ap);
    1367             : 
    1368         860 :         if (len < 0) {
    1369           0 :                 dbperror(dbproc, SYBEMEM, errno);
    1370           0 :                 return FAIL;
    1371             :         }
    1372             : 
    1373         860 :         ret = dbcmd(dbproc, s);
    1374         860 :         free(s);
    1375             : 
    1376         860 :         return ret;
    1377             : }
    1378             : 
    1379             : /**
    1380             :  * \ingroup dblib_core
    1381             :  * \brief \c Append SQL to the command buffer.  
    1382             :  *
    1383             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1384             :  * \param cmdstring SQL to append to the command buffer.  
    1385             :  * \retval SUCCEED success.
    1386             :  * \retval FAIL insufficient memory.  
    1387             :  * \remarks set command state to \c  DBCMDPEND unless the command state is DBCMDSENT, in which case 
    1388             :  * it frees the command buffer.  This latter may or may not be the Right Thing to do.  
    1389             :  * \sa dbfcmd(), dbfreebuf(), dbgetchar(), dbopen(), dbstrcpy(), dbstrlen().
    1390             :  */
    1391             : RETCODE
    1392       24992 : dbcmd(DBPROCESS * dbproc, const char cmdstring[])
    1393             : {
    1394             :         size_t cmd_len, buf_len, newsz;
    1395             : 
    1396       24992 :         tdsdump_log(TDS_DBG_FUNC, "dbcmd(%p, %s)\n", dbproc, cmdstring);
    1397       24992 :         CHECK_CONN(FAIL);
    1398       24992 :         CHECK_NULP(cmdstring, "dbcmd", 2, FAIL);
    1399             : 
    1400       24992 :         dbproc->avail_flag = FALSE;
    1401             : 
    1402       24992 :         tdsdump_log(TDS_DBG_FUNC, "dbcmd() bufsz = %d\n", dbproc->dbbufsz);
    1403             : 
    1404       24992 :         if (dbproc->command_state == DBCMDSENT) {
    1405       19778 :                 if (!dbproc->noautofree) {
    1406       19778 :                         dbfreebuf(dbproc);
    1407             :                 }
    1408             :         }
    1409             : 
    1410       24992 :         buf_len = (dbproc->dbbufsz == 0) ? 0 : dbproc->dbbufsz - 1;
    1411       24992 :         cmd_len = strlen(cmdstring);
    1412       24992 :         newsz = buf_len + cmd_len + 1;
    1413       24992 :         if (newsz > 0x7fffffffu || !TDS_RESIZE(dbproc->dbbuf, newsz)) {
    1414           0 :                 dbperror(dbproc, SYBEMEM, errno);
    1415           0 :                 return FAIL;
    1416             :         }
    1417       24992 :         memcpy(dbproc->dbbuf + buf_len, cmdstring, cmd_len);
    1418       24992 :         dbproc->dbbuf[newsz - 1] = 0;
    1419       24992 :         dbproc->dbbufsz = (int) newsz;
    1420             : 
    1421       24992 :         dbproc->command_state = DBCMDPEND;
    1422             : 
    1423       24992 :         return SUCCEED;
    1424             : }
    1425             : 
    1426             : /**
    1427             :  * \ingroup dblib_core
    1428             :  * \brief send the SQL command to the server and wait for an answer.  
    1429             :  * 
    1430             :  * Please be patient.  This function waits for the server to respond.   \c dbsqlexec is equivalent
    1431             :  * to dbsqlsend() followed by dbsqlok(). 
    1432             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1433             :  * \retval SUCCEED query was processed without errors.
    1434             :  * \retval FAIL was returned by dbsqlsend() or dbsqlok().
    1435             :  * \sa dbcmd(), dbfcmd(), dbnextrow(), dbresults(), dbretstatus(), dbsettime(), dbsqlok(), dbsqlsend()
    1436             :  */
    1437             : RETCODE
    1438       20322 : dbsqlexec(DBPROCESS * dbproc)
    1439             : {
    1440       20322 :         RETCODE rc = FAIL;
    1441             : 
    1442       20322 :         tdsdump_log(TDS_DBG_FUNC, "dbsqlexec(%p)\n", dbproc);
    1443       20322 :         CHECK_CONN(FAIL);
    1444             : 
    1445       20322 :         if (SUCCEED == (rc = dbsqlsend(dbproc))) {
    1446       20242 :                 rc = dbsqlok(dbproc);
    1447             :         }
    1448             :         return rc;
    1449             : }
    1450             : 
    1451             : /**
    1452             :  * \ingroup dblib_core
    1453             :  * \brief Change current database. 
    1454             :  * 
    1455             :  * Analagous to the unix command \c cd, dbuse() makes \a name the default database.  Waits for an answer
    1456             :  * from the server.  
    1457             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1458             :  * \param name database to use.
    1459             :  * \retval SUCCEED query was processed without errors.
    1460             :  * \retval FAIL query was not processed
    1461             :  * \sa dbchange(), dbname().
    1462             :  */
    1463             : RETCODE
    1464         804 : dbuse(DBPROCESS * dbproc, const char *name)
    1465             : {
    1466             :         RETCODE rc;
    1467             :         char *query;
    1468             : 
    1469         804 :         tdsdump_log(TDS_DBG_FUNC, "dbuse(%p, %s)\n", dbproc, name);
    1470         804 :         CHECK_CONN(FAIL);
    1471         804 :         CHECK_NULP(name, "dbuse", 2, FAIL);
    1472             : 
    1473             :         /* quote name */
    1474         804 :         query = tds_new(char, tds_quote_id(dbproc->tds_socket, NULL, name, -1) + 6);
    1475         804 :         if (!query) {
    1476           0 :                 dbperror(dbproc, SYBEMEM, errno);
    1477           0 :                 return FAIL;
    1478             :         }
    1479         804 :         strcpy(query, "use ");
    1480             :         /* TODO PHP suggest to quote by yourself with []... what should I do ?? quote or not ?? */
    1481         804 :         if (name[0] == '[' && name[strlen(name)-1] == ']')
    1482           0 :                 strcat(query, name);
    1483             :         else
    1484         804 :                 tds_quote_id(dbproc->tds_socket, query + 4, name, -1);
    1485             : 
    1486         804 :         rc = SUCCEED;
    1487         804 :         if ((dbcmd(dbproc, query) == FAIL)
    1488         804 :             || (dbsqlexec(dbproc) == FAIL)
    1489         804 :             || (dbresults(dbproc) == FAIL)
    1490         804 :             || (dbcanquery(dbproc) == FAIL))
    1491             :                 rc = FAIL;
    1492         804 :         free(query);
    1493         804 :         return rc;
    1494             : }
    1495             : 
    1496             : /**
    1497             :  * \ingroup dblib_core
    1498             :  * \brief Close a connection to the server and free associated resources.  
    1499             :  * 
    1500             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1501             :  * \sa dbexit(), dbopen().
    1502             :  */
    1503             : void
    1504         834 : dbclose(DBPROCESS * dbproc)
    1505             : {
    1506             :         TDSSOCKET *tds;
    1507             :         int i;
    1508             :         char timestr[256];
    1509             : 
    1510         834 :         tdsdump_log(TDS_DBG_FUNC, "dbclose(%p)\n", dbproc);   
    1511         834 :         CHECK_PARAMETER(dbproc, SYBENULL, );
    1512             : 
    1513         834 :         tds = dbproc->tds_socket;
    1514         834 :         if (tds) {
    1515             :                 /* 
    1516             :                  * this MUST be done before socket destruction
    1517             :                  * it is possible that a TDSSOCKET is allocated on same position
    1518             :                  */
    1519         456 :                 tds_mutex_lock(&dblib_mutex);
    1520         456 :                 dblib_del_connection(&g_dblib_ctx, dbproc->tds_socket);
    1521         456 :                 tds_mutex_unlock(&dblib_mutex);
    1522             : 
    1523         456 :                 tds_close_socket(tds);
    1524         456 :                 tds_free_socket(tds);
    1525         456 :                 dblib_release_tds_ctx(1);
    1526             :         }
    1527         834 :         buffer_free(&(dbproc->row_buf));
    1528             : 
    1529         834 :         if (dbproc->ftos != NULL) {
    1530           0 :                 fprintf(dbproc->ftos, "/* dbclose() at %s */\n", _dbprdate(timestr));
    1531           0 :                 fclose(dbproc->ftos);
    1532             :         }
    1533             : 
    1534         834 :         tds_free_bcpinfo(dbproc->bcpinfo);
    1535         834 :         if (dbproc->hostfileinfo) {
    1536           0 :                 free(dbproc->hostfileinfo->hostfile);
    1537           0 :                 free(dbproc->hostfileinfo->errorfile);
    1538           0 :                 if (dbproc->hostfileinfo->host_columns) {
    1539           0 :                         for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
    1540           0 :                                 free(dbproc->hostfileinfo->host_columns[i]->terminator);
    1541           0 :                                 free(dbproc->hostfileinfo->host_columns[i]);
    1542             :                         }
    1543           0 :                         free(dbproc->hostfileinfo->host_columns);
    1544             :                 }
    1545             :         }
    1546             : 
    1547       30024 :         for (i = 0; i < DBNUMOPTIONS; i++) {
    1548       30024 :                 dbstring_free(&(dbproc->dbopts[i].param));
    1549             :         }
    1550         834 :         free(dbproc->dbopts);
    1551             : 
    1552         834 :         dbstring_free(&(dbproc->dboptcmd));
    1553             : 
    1554       27522 :         for (i=0; i < MAXBINDTYPES; i++) {
    1555       26688 :                 if (dbproc->nullreps[i].bindval != default_null_representations[i].bindval)
    1556         250 :                         free((BYTE*)dbproc->nullreps[i].bindval);
    1557             :         }
    1558             : 
    1559         834 :         dbfreebuf(dbproc);
    1560         834 :         free(dbproc);
    1561             : }
    1562             : 
    1563             : /**
    1564             :  * \ingroup dblib_core
    1565             :  * \brief Close server connections and free all related structures.  
    1566             :  * 
    1567             :  * \sa dbclose(), dbinit(), dbopen().
    1568             :  * \todo breaks if ctlib/dblib used in same process.
    1569             :  */
    1570             : void
    1571         472 : dbexit(void)
    1572             : {
    1573             :         TDSSOCKET *tds;
    1574             :         DBPROCESS *dbproc;
    1575         472 :         int i, list_size, count = 1;
    1576             : 
    1577         472 :         tdsdump_log(TDS_DBG_FUNC, "dbexit(void)\n");
    1578             : 
    1579         472 :         tds_mutex_lock(&dblib_mutex);
    1580             : 
    1581         472 :         if (--g_dblib_ctx.ref_count != 0) {
    1582           0 :                 tds_mutex_unlock(&dblib_mutex);
    1583           0 :                 return;
    1584             :         }
    1585             : 
    1586         472 :         list_size = g_dblib_ctx.connection_list_size;
    1587             : 
    1588     1933784 :         for (i = 0; i < list_size; i++) {
    1589     1933312 :                 tds = g_dblib_ctx.connection_list[i];
    1590     1933312 :                 g_dblib_ctx.connection_list[i] = NULL;
    1591     1933312 :                 if (tds) {
    1592         378 :                         ++count;
    1593         378 :                         dbproc = (DBPROCESS *) tds_get_parent(tds);
    1594         378 :                         tds_close_socket(tds);
    1595         378 :                         tds_free_socket(tds);
    1596         378 :                         if (dbproc) {
    1597             :                                 /* avoid locking in dbclose */
    1598         378 :                                 dbproc->tds_socket = NULL;
    1599         378 :                                 dbclose(dbproc);
    1600             :                         }
    1601             :                 }
    1602             :         }
    1603         472 :         if (g_dblib_ctx.connection_list) {
    1604         472 :                 TDS_ZERO_FREE(g_dblib_ctx.connection_list);
    1605         472 :                 g_dblib_ctx.connection_list_size = 0;
    1606         472 :                 g_dblib_ctx.connection_list_size_represented = 0;
    1607             :         }
    1608             : 
    1609         472 :         tds_mutex_unlock(&dblib_mutex);
    1610             : 
    1611         472 :         dblib_release_tds_ctx(count);
    1612             : }
    1613             : 
    1614             : static const char *
    1615             : prdbresults_state(int retcode)
    1616             : {
    1617           0 :         switch(retcode) {
    1618             :         case _DB_RES_INIT:              return "_DB_RES_INIT";
    1619           0 :         case _DB_RES_RESULTSET_EMPTY:   return "_DB_RES_RESULTSET_EMPTY";
    1620           0 :         case _DB_RES_RESULTSET_ROWS:    return "_DB_RES_RESULTSET_ROWS";
    1621           0 :         case _DB_RES_NEXT_RESULT:       return "_DB_RES_NEXT_RESULT";
    1622           0 :         case _DB_RES_NO_MORE_RESULTS:   return "_DB_RES_NO_MORE_RESULTS";
    1623           0 :         case _DB_RES_SUCCEED:           return "_DB_RES_SUCCEED";
    1624             :         default: break;
    1625             :         }
    1626             :         return "??";
    1627             : }
    1628             : 
    1629             : static const char *
    1630             : prdbretcode(RETCODE retcode)
    1631             : {
    1632           0 :         switch(retcode) {
    1633             :         case REG_ROW:           return "REG_ROW/MORE_ROWS";
    1634           0 :         case NO_MORE_ROWS:      return "NO_MORE_ROWS";
    1635           0 :         case BUF_FULL:          return "BUF_FULL";
    1636           0 :         case NO_MORE_RESULTS:   return "NO_MORE_RESULTS";
    1637           0 :         case SUCCEED:           return "SUCCEED";
    1638           0 :         case FAIL:              return "FAIL";
    1639             :         default: break;
    1640             :         }
    1641             :         return "??";
    1642             : }
    1643             : 
    1644             : static const char *
    1645             : prretcode(int retcode)
    1646             : {
    1647           0 :         switch(retcode) {
    1648             :         case TDS_SUCCESS:               return "TDS_SUCCESS";
    1649           0 :         case TDS_FAIL:                  return "TDS_FAIL";
    1650           0 :         case TDS_NO_MORE_RESULTS:       return "TDS_NO_MORE_RESULTS";
    1651           0 :         case TDS_CANCELLED:             return "TDS_CANCELLED";
    1652             :         default: break;
    1653             :         }
    1654             :         return "??";
    1655             : }
    1656             : 
    1657             : static const char *
    1658           0 : prresult_type(int result_type)
    1659             : {
    1660           0 :         switch(result_type) {
    1661             :         case TDS_ROW_RESULT:            return "TDS_ROW_RESULT";
    1662           0 :         case TDS_PARAM_RESULT:          return "TDS_PARAM_RESULT";
    1663           0 :         case TDS_STATUS_RESULT:         return "TDS_STATUS_RESULT";
    1664           0 :         case TDS_MSG_RESULT:            return "TDS_MSG_RESULT";
    1665           0 :         case TDS_COMPUTE_RESULT:        return "TDS_COMPUTE_RESULT";
    1666           0 :         case TDS_CMD_DONE:              return "TDS_CMD_DONE";
    1667           0 :         case TDS_CMD_SUCCEED:           return "TDS_CMD_SUCCEED";
    1668           0 :         case TDS_CMD_FAIL:              return "TDS_CMD_FAIL";
    1669           0 :         case TDS_ROWFMT_RESULT:         return "TDS_ROWFMT_RESULT";
    1670           0 :         case TDS_COMPUTEFMT_RESULT:     return "TDS_COMPUTEFMT_RESULT";
    1671           0 :         case TDS_DESCRIBE_RESULT:       return "TDS_DESCRIBE_RESULT";
    1672           0 :         case TDS_DONE_RESULT:           return "TDS_DONE_RESULT";
    1673           0 :         case TDS_DONEPROC_RESULT:       return "TDS_DONEPROC_RESULT";
    1674           0 :         case TDS_DONEINPROC_RESULT:     return "TDS_DONEINPROC_RESULT";
    1675           0 :         case TDS_OTHERS_RESULT:         return "TDS_OTHERS_RESULT";
    1676             :         default: break;
    1677             :         }
    1678           0 :         return "??";
    1679             : }
    1680             : 
    1681             : /**
    1682             :  * \ingroup dblib_core
    1683             :  * \brief Set up query results.  
    1684             :  *
    1685             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1686             :  * \retval SUCCEED Some results are available.
    1687             :  * \retval FAIL query was not processed successfully by the server
    1688             :  * \retval NO_MORE_RESULTS query produced no results. 
    1689             :  *
    1690             :  * \remarks Call dbresults() after calling dbsqlexec() or dbsqlok(), or dbrpcsend() returns SUCCEED.  Unless
    1691             :  *      one of them fails, dbresults will return either SUCCEED or NO_MORE_RESULTS.  
    1692             :  *
    1693             :  *      The meaning of \em results is very specific and not very intuitive.  Results are created by either
    1694             :  *      - a SELECT statement
    1695             :  *      - a stored procedure
    1696             :  *
    1697             :  *      When dbresults returns SUCCEED, therefore, it indicates the server processed the query successfully and 
    1698             :  *      that one or more of these is present:
    1699             :  *      - metadata -- dbnumcols() returns 1 or more
    1700             :  *      - data -- dbnextrow() returns SUCCEED
    1701             :  *      - return status -- dbhasretstat() returns TRUE
    1702             :  *      - output parameters -- dbnumrets() returns 1 or more
    1703             :  *
    1704             :  *      If none of the above are present, dbresults() returns NO_MORE_RESULTS.  
    1705             :  *      
    1706             :  *      SUCCEED does not imply that DBROWS() will return TRUE or even that dbnumcols() will return nonzero.  
    1707             :  *      A general algorithm for reading results will call dbresults() until it return NO_MORE_RESULTS (or FAIL).  
    1708             :  *      An application should check for all the above kinds of results within the dbresults() loop.  
    1709             :  * 
    1710             :  * \sa dbsqlexec(), dbsqlok(), dbrpcsend(), dbcancel(), DBROWS(), dbnextrow(), dbnumcols(), dbhasretstat(), dbretstatus(), dbnumrets()
    1711             :  */
    1712             : RETCODE
    1713       29658 : dbresults(DBPROCESS * dbproc)
    1714             : {
    1715       29658 :         RETCODE erc = _dbresults(dbproc);
    1716             : 
    1717       29658 :         tdsdump_log(TDS_DBG_FUNC, "dbresults returning %d (%s)\n", erc, prdbretcode(erc));
    1718       29658 :         return erc;
    1719             : }
    1720             : 
    1721             : static RETCODE
    1722       29658 : _dbresults(DBPROCESS * dbproc)
    1723             : {
    1724             :         TDSSOCKET *tds;
    1725       29658 :         int result_type = 0, done_flags;
    1726             : 
    1727       29658 :         tdsdump_log(TDS_DBG_FUNC, "dbresults(%p)\n", dbproc);
    1728       29658 :         CHECK_CONN(FAIL);
    1729             : 
    1730       29658 :         tds = dbproc->tds_socket;
    1731             : 
    1732       29658 :         tdsdump_log(TDS_DBG_FUNC, "dbresults: dbresults_state is %d (%s)\n", 
    1733           0 :                                         dbproc->dbresults_state, prdbresults_state(dbproc->dbresults_state));
    1734       29658 :         switch ( dbproc->dbresults_state ) {
    1735        8126 :         case _DB_RES_SUCCEED:
    1736        8126 :                 dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    1737        8126 :                 return SUCCEED;
    1738             :                 break;
    1739           0 :         case _DB_RES_RESULTSET_ROWS:
    1740           0 :                 dbperror(dbproc, SYBERPND, 0); /* dbresults called while rows outstanding.... */
    1741           0 :                 return FAIL;
    1742             :                 break;
    1743             :         case _DB_RES_NO_MORE_RESULTS:
    1744             :                 return NO_MORE_RESULTS;
    1745             :                 break;
    1746             :         default:
    1747             :                 break;
    1748             :         }
    1749             : 
    1750             :         for (;;) {
    1751       31110 :                 TDSRET retcode = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS);
    1752             : 
    1753       31110 :                 tdsdump_log(TDS_DBG_FUNC, "dbresults() tds_process_tokens returned %d (%s),\n\t\t\tresult_type %d (%s)\n",
    1754             :                                                 retcode, prretcode(retcode), result_type, prresult_type(result_type));
    1755             : 
    1756       31110 :                 switch (retcode) {
    1757             : 
    1758       22746 :                 case TDS_SUCCESS:
    1759             : 
    1760       22746 :                         switch (result_type) {
    1761             :         
    1762         298 :                         case TDS_ROWFMT_RESULT:
    1763         298 :                                 buffer_free(&dbproc->row_buf);
    1764         298 :                                 buffer_alloc(dbproc);
    1765         298 :                                 dbproc->dbresults_state = _DB_RES_RESULTSET_EMPTY;
    1766         298 :                                 break;
    1767             :         
    1768             :                         case TDS_COMPUTEFMT_RESULT:
    1769             :                                 break;
    1770             :         
    1771       12080 :                         case TDS_ROW_RESULT:
    1772             :                         case TDS_COMPUTE_RESULT:
    1773             :         
    1774       12080 :                                 dbproc->dbresults_state = _DB_RES_RESULTSET_ROWS;
    1775       12080 :                                 return SUCCEED;
    1776             :                                 break;
    1777             :         
    1778        1086 :                         case TDS_DONE_RESULT:
    1779             :                         case TDS_DONEPROC_RESULT:
    1780        1086 :                                 tdsdump_log(TDS_DBG_FUNC, "dbresults(): dbresults_state is %d (%s)\n", 
    1781           0 :                                                 dbproc->dbresults_state, prdbresults_state(dbproc->dbresults_state));
    1782             : 
    1783             :                                 /* A done token signifies the end of a logical command.
    1784             :                                  * There are three possibilities:
    1785             :                                  * 1. Simple command with no result set, i.e. update, delete, insert
    1786             :                                  * 2. Command with result set but no rows
    1787             :                                  * 3. Command with result set and rows
    1788             :                                  */
    1789        1086 :                                 switch (dbproc->dbresults_state) {
    1790             : 
    1791         764 :                                 case _DB_RES_INIT:
    1792             :                                 case _DB_RES_NEXT_RESULT:
    1793         764 :                                         dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    1794         764 :                                         if (done_flags & TDS_DONE_ERROR)
    1795             :                                                 return FAIL;
    1796         764 :                                         if (result_type == TDS_DONE_RESULT) {
    1797         668 :                                                 tds_free_all_results(tds);
    1798         668 :                                                 return SUCCEED;
    1799             :                                         }
    1800             :                                         break;
    1801             : 
    1802         322 :                                 case _DB_RES_RESULTSET_EMPTY:
    1803             :                                 case _DB_RES_RESULTSET_ROWS:
    1804         322 :                                         dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    1805         322 :                                         return SUCCEED;
    1806             :                                         break;
    1807             :                                 default:
    1808           0 :                                         assert(0);
    1809             :                                         break;
    1810             :                                 }
    1811             :                                 break;
    1812             : 
    1813        9240 :                         case TDS_DONEINPROC_RESULT:
    1814             :                                 /* 
    1815             :                                  * Return SUCCEED on a command within a stored procedure
    1816             :                                  * only if the command returned a result set. 
    1817             :                                  */
    1818        9240 :                                 switch (dbproc->dbresults_state) {
    1819        9228 :                                 case _DB_RES_INIT:  
    1820             :                                 case _DB_RES_NEXT_RESULT: 
    1821        9228 :                                         dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    1822        9228 :                                         break;
    1823          12 :                                 case _DB_RES_RESULTSET_EMPTY :
    1824             :                                 case _DB_RES_RESULTSET_ROWS : 
    1825          12 :                                         dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    1826          12 :                                         return SUCCEED;
    1827             :                                         break;
    1828             :                                 case _DB_RES_NO_MORE_RESULTS:
    1829             :                                 case _DB_RES_SUCCEED:
    1830             :                                         break;
    1831             :                                 }
    1832             :                                 break;
    1833             : 
    1834             :                         case TDS_STATUS_RESULT:
    1835             :                         case TDS_MSG_RESULT:
    1836             :                         case TDS_DESCRIBE_RESULT:
    1837             :                         case TDS_PARAM_RESULT:
    1838             :                         default:
    1839             :                                 break;
    1840             :                         }
    1841             : 
    1842             :                         break;
    1843             : 
    1844        8364 :                 case TDS_NO_MORE_RESULTS:
    1845        8364 :                         dbproc->dbresults_state = _DB_RES_NO_MORE_RESULTS;
    1846        8364 :                         return NO_MORE_RESULTS;
    1847             :                         break;
    1848             : 
    1849           0 :                 default:
    1850           0 :                         assert(TDS_FAILED(retcode));
    1851           0 :                         dbproc->dbresults_state = _DB_RES_INIT;
    1852           0 :                         return FAIL;
    1853             :                         break;
    1854             :                 }
    1855             :         }
    1856             : }
    1857             : 
    1858             : 
    1859             : /**
    1860             :  * \ingroup dblib_core
    1861             :  * \brief Return number of regular columns in a result set.  
    1862             :  * 
    1863             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1864             :  * \sa dbcollen(), dbcolname(), dbnumalts().
    1865             :  */
    1866             : int
    1867        3966 : dbnumcols(DBPROCESS * dbproc)
    1868             : {
    1869        3966 :         tdsdump_log(TDS_DBG_FUNC, "dbnumcols(%p)\n", dbproc);
    1870        3966 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    1871             : 
    1872        3966 :         if (dbproc && dbproc->tds_socket && dbproc->tds_socket->res_info)
    1873        3706 :                 return dbproc->tds_socket->res_info->num_cols;
    1874             :         return 0;
    1875             : }
    1876             : 
    1877             : /**
    1878             :  * \ingroup dblib_core
    1879             :  * \brief Return name of a regular result column.
    1880             :  * 
    1881             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1882             :  * \param column Nth in the result set, starting with 1.  
    1883             :  * \return pointer to ASCII null-terminated string, the name of the column. 
    1884             :  * \retval NULL \a column is not in range.
    1885             :  * \sa dbcollen(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
    1886             :  * \bug Relies on ASCII column names, post iconv conversion.  
    1887             :  *      Will not work as described for UTF-8 or UCS-2 clients.  
    1888             :  *      But maybe it shouldn't.  
    1889             :  */
    1890             : char *
    1891        2858 : dbcolname(DBPROCESS * dbproc, int column)
    1892             : {
    1893             :         TDSCOLUMN *colinfo;
    1894             :         
    1895        2858 :         tdsdump_log(TDS_DBG_FUNC, "dbcolname(%p, %d)\n", dbproc, column);
    1896             : 
    1897        2858 :         colinfo = dbcolptr(dbproc, column);
    1898        2858 :         if (!colinfo)
    1899             :                 return NULL;
    1900             :                 
    1901        5716 :         return tds_dstr_buf(&colinfo->column_name);
    1902             : }
    1903             : 
    1904             : /**
    1905             :  * \ingroup dblib_core
    1906             :  * \brief Return name of a computed result column.
    1907             :  *
    1908             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1909             :  * \param computeid identifies which one of potientially many compute rows is meant. The first compute
    1910             :  * clause has \a computeid == 1.
    1911             :  * \param column Nth in the result set, starting with 1.
    1912             :  * \return pointer to ASCII null-terminated string, the name of the column.
    1913             :  * \retval NULL \a column is not in range.
    1914             :  * \sa dbcollen(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
    1915             :  */
    1916             : const char *
    1917           0 : dbacolname(DBPROCESS * dbproc, int computeid, int column)
    1918             : {
    1919             :         TDSCOLUMN *colinfo;
    1920             : 
    1921           0 :         tdsdump_log(TDS_DBG_FUNC, "dbacolname(%p, %d, %d)\n", dbproc, computeid, column);
    1922             : 
    1923           0 :         colinfo = dbacolptr(dbproc, computeid, column, true);
    1924           0 :         if (!colinfo)
    1925             :                 return NULL;
    1926             : 
    1927           0 :         return tds_dstr_cstr(&colinfo->column_name);
    1928             : }
    1929             : 
    1930             : /**
    1931             :  * \ingroup dblib_core
    1932             :  * \brief Read a row from the row buffer.
    1933             :  * 
    1934             :  * When row buffering is enabled (DBBUFFER option is on), the client can use dbgetrow() to re-read a row previously fetched 
    1935             :  * with dbnextrow().  The effect is to move the row pointer -- analogous to fseek() -- back to \a row.  
    1936             :  * Calls to dbnextrow() read from \a row + 1 until the buffer is exhausted, at which point it resumes
    1937             :  * its normal behavior, except that as each row is fetched from the server, it is added to the row
    1938             :  * buffer (in addition to being returned to the client).  When the buffer is filled, dbnextrow()  returns 
    1939             :  * \c FAIL until the buffer is at least partially emptied with dbclrbuf().
    1940             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1941             :  * \param row Nth row to read, starting with 1.
    1942             :  * \retval REG_ROW returned row is a regular row.
    1943             :  * \returns computeid when returned row is a compute row.
    1944             :  * \retval NO_MORE_ROWS no such row in the row buffer.  Current row is unchanged.
    1945             :  * \retval FAIL unsuccessful; row buffer may be full.  
    1946             :  * \sa dbaltbind(), dbbind(), dbclrbuf(), DBCURROW(), DBFIRSTROW(), DBLASTROW(), dbnextrow(), dbsetrow().
    1947             :  */
    1948             : RETCODE
    1949          60 : dbgetrow(DBPROCESS * dbproc, DBINT row)
    1950             : {
    1951          60 :         RETCODE result = FAIL;
    1952          60 :         const int idx = buffer_row2idx(&dbproc->row_buf, row);
    1953             : 
    1954          60 :         tdsdump_log(TDS_DBG_FUNC, "dbgetrow(%p, %d)\n", dbproc, row);
    1955          60 :         CHECK_CONN(FAIL);
    1956             : 
    1957          60 :         if (-1 == idx)
    1958             :                 return NO_MORE_ROWS;
    1959             : 
    1960          40 :         dbproc->row_buf.current = idx;
    1961          40 :         buffer_transfer_bound_data(&dbproc->row_buf, TDS_ROW_RESULT, 0, dbproc, idx);
    1962          40 :         result = REG_ROW;
    1963             : 
    1964          40 :         return result;
    1965             : }
    1966             : 
    1967             : /**
    1968             :  * \ingroup dblib_core
    1969             :  * \brief Define substitution values to be used when binding null values.
    1970             :  * 
    1971             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1972             :  * \param bindtype      type of binding to which the substitute value will apply. 
    1973             :  * \param bindlen       size of the substitute value you are supplying, in bytes. 
    1974             :  *                      Ignored except for CHARBIND and BINARYBIND. 
    1975             :  * \param bindval       pointer to a buffer containing the substitute value.
    1976             :  * \retval SUCCEED query was processed without errors.
    1977             :  * \retval FAIL query was not processed
    1978             :  * \sa dbaltbind(), dbbind(), dbconvert(), dbnullbind().
    1979             :  */
    1980             : RETCODE
    1981         400 : dbsetnull(DBPROCESS * dbproc, int bindtype, int bindlen, BYTE *bindval)
    1982             : {
    1983             :         BYTE *pval;
    1984             :         
    1985         400 :         tdsdump_log(TDS_DBG_FUNC, "dbsetnull(%p, %d, %d, %p)\n", dbproc, bindtype, bindlen, bindval);
    1986             :         
    1987         400 :         CHECK_CONN(FAIL);
    1988         400 :         CHECK_PARAMETER(bindval, SYBENBVP, FAIL);
    1989             :         
    1990         400 :         switch (bindtype) {
    1991         330 :         case DATETIMEBIND:
    1992             :         case DECIMALBIND:
    1993             :         case SRCDECIMALBIND:
    1994             :         case FLT8BIND:
    1995             :         case INTBIND:
    1996             :         case MONEYBIND:
    1997             :         case NUMERICBIND:
    1998             :         case SRCNUMERICBIND:
    1999             :         case REALBIND:
    2000             :         case SMALLBIND:
    2001             :         case SMALLDATETIMEBIND:
    2002             :         case SMALLMONEYBIND:
    2003             :         case TINYBIND:
    2004             :         case BIGINTBIND:
    2005             :         case DATEBIND:
    2006             :         case TIMEBIND:
    2007             :         case BIGDATETIMEBIND:
    2008             :         case BIGTIMEBIND:
    2009         330 :                 bindlen = (int)default_null_representations[bindtype].len;
    2010         330 :                 break;
    2011             : 
    2012          70 :         case CHARBIND:
    2013             :         case BINARYBIND:
    2014          70 :                 CHECK_PARAMETER(bindlen >= 0, SYBEBBL, FAIL);
    2015             :                 break;
    2016             :                 
    2017           0 :         case NTBSTRINGBIND:     bindlen = (int)strlen((char *) bindval);
    2018           0 :                 break;
    2019           0 :         case STRINGBIND:        bindlen = (int)strlen((char *) bindval);
    2020           0 :                 break;
    2021           0 :         case VARYBINBIND:       bindlen = ((DBVARYBIN*) bindval)->len;
    2022           0 :                 break;
    2023           0 :         case VARYCHARBIND:      bindlen = ((DBVARYCHAR*) bindval)->len;
    2024           0 :                 break;
    2025             : 
    2026             : #if 0
    2027             :         case SENSITIVITYBIND:
    2028             :         case BOUNDARYBIND:
    2029             : #endif
    2030           0 :         default:
    2031           0 :                 dbperror(dbproc, SYBEBTYP, 0);
    2032           0 :                 return FAIL;
    2033             :         }
    2034             : 
    2035         400 :         if ((pval = tds_new(BYTE, bindlen)) == NULL) {
    2036           0 :                 dbperror(dbproc, SYBEMEM, errno);
    2037           0 :                 return FAIL;
    2038             :         }
    2039             :         
    2040             :         /* free any prior allocation */
    2041         400 :         if (dbproc->nullreps[bindtype].bindval != default_null_representations[bindtype].bindval)
    2042         380 :                 free((BYTE*)dbproc->nullreps[bindtype].bindval);
    2043             : 
    2044         400 :         memcpy(pval, bindval, bindlen);
    2045             :         
    2046         400 :         dbproc->nullreps[bindtype].bindval = pval;
    2047         400 :         dbproc->nullreps[bindtype].len = bindlen;
    2048             : 
    2049         400 :         tdsdump_dump_buf(TDS_DBG_NETWORK, "null representation set ", pval,  bindlen);
    2050             :         return SUCCEED;
    2051             : }
    2052             : 
    2053             : /**
    2054             :  * \ingroup dblib_core
    2055             :  * \brief Make a buffered row "current" without fetching it into bound variables.  
    2056             :  * 
    2057             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2058             :  * \retval MORE_ROWS row found
    2059             :  * \retval NO_MORE_ROWS row not found
    2060             :  * \retval FAIL \a dbproc is dead or not enabled
    2061             :  * \sa dbaltbind(), dbbind(), dbcanquery(), dbclrbuf(), dbgetrow(), dbnextrow(), dbprrow().
    2062             :  */
    2063             : STATUS
    2064           0 : dbsetrow(DBPROCESS * dbproc, DBINT row)
    2065             : {
    2066           0 :         const int idx = buffer_row2idx(&dbproc->row_buf, row);
    2067             : 
    2068           0 :         tdsdump_log(TDS_DBG_FUNC, "dbsetrow(%p, %d)\n", dbproc, row);
    2069           0 :         CHECK_CONN(FAIL);
    2070             : 
    2071           0 :         if (-1 == idx)
    2072             :                 return NO_MORE_ROWS;
    2073             :                 
    2074           0 :         dbproc->row_buf.current = idx;
    2075             :                 
    2076             :         /* FIXME: should determine REG_ROW or compute_id; */
    2077           0 :         return REG_ROW; 
    2078             : }
    2079             : 
    2080             : /**
    2081             :  * \ingroup dblib_core
    2082             :  * \brief Read result row into the row buffer and into any bound host variables.
    2083             :  * 
    2084             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2085             :  * \retval REG_ROW regular row has been read.
    2086             :  * \returns computeid when a compute row is read. 
    2087             :  * \retval BUF_FULL reading next row would cause the buffer to be exceeded (and buffering is turned on).
    2088             :  * No row was read from the server
    2089             :  * \sa dbaltbind(), dbbind(), dbcanquery(), dbclrbuf(), dbgetrow(), dbprrow(), dbsetrow().
    2090             :  */
    2091             : struct pivot_t;
    2092             : STATUS
    2093      220520 : dbnextrow(DBPROCESS * dbproc)
    2094             : {
    2095             :         TDSRESULTINFO *resinfo;
    2096             :         TDSSOCKET *tds;
    2097      220520 :         STATUS result = FAIL;
    2098             :         TDS_INT res_type;
    2099             :         TDS_INT computeid;
    2100             :         int idx; /* row buffer index.  Unless DBUFFER is on, idx will always be 0. */
    2101             :         struct pivot_t *pivot;
    2102             : 
    2103      220520 :         tdsdump_log(TDS_DBG_FUNC, "dbnextrow(%p)\n", dbproc);
    2104      220520 :         CHECK_CONN(FAIL);
    2105             : 
    2106      220520 :         tds = dbproc->tds_socket;
    2107      220520 :         resinfo = tds->res_info;
    2108             : 
    2109      220520 :         tdsdump_log(TDS_DBG_FUNC, "dbnextrow() dbresults_state = %d (%s)\n", 
    2110           0 :                                         dbproc->dbresults_state, prdbresults_state(dbproc->dbresults_state));
    2111             : 
    2112      220520 :         if (!resinfo || dbproc->dbresults_state != _DB_RES_RESULTSET_ROWS) {
    2113             :                 /* no result set or result set empty (no rows) */
    2114         772 :                 tdsdump_log(TDS_DBG_FUNC, "leaving dbnextrow() returning %d (NO_MORE_ROWS)\n", NO_MORE_ROWS);
    2115         772 :                 return dbproc->row_type = NO_MORE_ROWS;
    2116             :         }
    2117             : 
    2118             :         /*
    2119             :          * Try to get the dbproc->row_buf.current item from the buffered rows, if any.  
    2120             :          * Else read from the stream, unless the buffer is exhausted.  
    2121             :          * If no rows are read, DBROWTYPE() will report NO_MORE_ROWS. 
    2122             :          */
    2123      219748 :         dbproc->row_type = NO_MORE_ROWS; 
    2124      219748 :         computeid = REG_ROW;
    2125      219748 :         if (-1 != (idx = buffer_current_index(dbproc))) {                       
    2126             :                 /*
    2127             :                  * Cool, the item we want is already there
    2128             :                  */
    2129          10 :                 result = dbproc->row_type = REG_ROW;
    2130          10 :                 res_type = TDS_ROW_RESULT;
    2131             :                 
    2132      219738 :         } else if (buffer_is_full(&dbproc->row_buf)) {
    2133             :                 
    2134         120 :                 result = BUF_FULL;
    2135         120 :                 res_type = TDS_ROWFMT_RESULT;
    2136             :         
    2137      219618 :         } else if ((pivot = dbrows_pivoted(dbproc)) != NULL) {
    2138             :         
    2139           0 :                 tdsdump_log(TDS_DBG_FUNC, "returning pivoted row\n");
    2140           0 :                 return dbnextrow_pivoted(dbproc, pivot);
    2141             : 
    2142             :         } else {
    2143      219618 :                 const int mask = TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE;
    2144      219618 :                 TDS_INT8 row_count = TDS_NO_COUNT;
    2145      219618 :                 bool rows_set = false;
    2146      219618 :                 buffer_save_row(dbproc);
    2147             : 
    2148             :                 /* Get the row from the TDS stream.  */
    2149         718 : again:
    2150      220336 :                 switch (tds_process_tokens(tds, &res_type, NULL, mask)) {
    2151      220306 :                 case TDS_SUCCESS:
    2152      220306 :                         if (res_type == TDS_ROW_RESULT || res_type == TDS_COMPUTE_RESULT) {
    2153      207718 :                                 if (res_type == TDS_COMPUTE_RESULT)
    2154          32 :                                         computeid = tds->current_results->computeid;
    2155             :                                 /* Add the row to the row buffer, whose capacity is always at least 1 */
    2156      207718 :                                 resinfo = tds->current_results;
    2157      207718 :                                 idx = buffer_add_row(dbproc, resinfo);
    2158      207718 :                                 assert(idx != -1);
    2159      207718 :                                 result = dbproc->row_type = (res_type == TDS_ROW_RESULT)? REG_ROW : computeid;
    2160             : #if 0 /* TODO */
    2161             :                                 tds_process_tokens(tds, &res_type, NULL, TDS_TOKEN_TRAILING);
    2162             : #endif
    2163      207718 :                                 break;
    2164             :                         }
    2165             :                         /* allows to process trailing tokens */
    2166       12588 :                         if (res_type == TDS_DONEINPROC_RESULT) {
    2167         718 :                                 if (!rows_set)
    2168         462 :                                         row_count = tds->rows_affected;
    2169             :                                 rows_set = true;
    2170             :                                 goto again;
    2171             :                         }
    2172             :                         /* fall through */
    2173             :                 case TDS_NO_MORE_RESULTS:
    2174       11900 :                         dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    2175       11900 :                         result = NO_MORE_ROWS;
    2176       11900 :                         break;
    2177           0 :                 default:
    2178           0 :                         tdsdump_log(TDS_DBG_FUNC, "unexpected: leaving dbnextrow() returning FAIL\n");
    2179             :                         return FAIL;
    2180             :                         break;
    2181             :                 }
    2182      219618 :                 if (rows_set)
    2183         462 :                         tds->rows_affected = row_count;
    2184             :         }
    2185             : 
    2186      219748 :         if (res_type == TDS_ROW_RESULT || res_type == TDS_COMPUTE_RESULT) {
    2187             :                 /*
    2188             :                  * Transfer the data from the row buffer to the bound variables.  
    2189             :                  */
    2190      207728 :                 buffer_transfer_bound_data(&dbproc->row_buf, res_type, computeid, dbproc, idx);
    2191             :         }
    2192             :         
    2193      219748 :         if (res_type == TDS_COMPUTE_RESULT) {
    2194          32 :                 tdsdump_log(TDS_DBG_FUNC, "leaving dbnextrow() returning compute_id %d\n", result);
    2195             :         } else {
    2196      219716 :                 tdsdump_log(TDS_DBG_FUNC, "leaving dbnextrow() returning %d (%s)\n", result, prdbretcode(result));
    2197             :         }
    2198             :         return result;
    2199             : } /* dbnextrow()  */
    2200             : 
    2201             : static TDS_SERVER_TYPE
    2202             : dblib_bound_type(int bindtype)
    2203             : {
    2204             :         switch (bindtype) {
    2205             :         case CHARBIND:
    2206             :         case STRINGBIND:
    2207             :         case NTBSTRINGBIND:
    2208             :                 return SYBCHAR;
    2209             :                 break;
    2210             :         case FLT8BIND:
    2211             :                 return SYBFLT8;
    2212             :                 break;
    2213             :         case REALBIND:
    2214             :                 return SYBREAL;
    2215             :                 break;
    2216             :         case INTBIND:
    2217             :                 return SYBINT4;
    2218             :                 break;
    2219             :         case SMALLBIND:
    2220             :                 return SYBINT2;
    2221             :                 break;
    2222             :         case TINYBIND:
    2223             :                 return SYBINT1;
    2224             :                 break;
    2225             :         case BIGINTBIND:
    2226             :                 return SYBINT8;
    2227             :                 break;
    2228             :         case DATETIMEBIND:
    2229             :                 return SYBDATETIME;
    2230             :                 break;
    2231             :         case SMALLDATETIMEBIND:
    2232             :                 return SYBDATETIME4;
    2233             :                 break;
    2234             :         case DATEBIND:
    2235             :                 return SYBDATE;
    2236             :                 break;
    2237             :         case TIMEBIND:
    2238             :                 return SYBTIME;
    2239             :                 break;
    2240             :         case BIGDATETIMEBIND:
    2241             :                 return SYB5BIGDATETIME;
    2242             :                 break;
    2243             :         case BIGTIMEBIND:
    2244             :                 return SYB5BIGTIME;
    2245             :                 break;
    2246             :         case MONEYBIND:
    2247             :                 return SYBMONEY;
    2248             :                 break;
    2249             :         case SMALLMONEYBIND:
    2250             :                 return SYBMONEY4;
    2251             :                 break;
    2252             :         case BINARYBIND:
    2253             :                 return SYBBINARY;
    2254             :                 break;
    2255             :         case VARYBINBIND:
    2256             :                 return SYBVARBINARY;
    2257             :                 break;
    2258             :         case VARYCHARBIND:
    2259             :                 return SYBVARCHAR;
    2260             :                 break;
    2261             :         case BITBIND:
    2262             :                 return SYBBIT;
    2263             :                 break;
    2264             :         case NUMERICBIND:
    2265             :         case SRCNUMERICBIND:
    2266             :         case DECIMALBIND:
    2267             :         case SRCDECIMALBIND:
    2268             :                 return SYBNUMERIC;
    2269             :                 break;
    2270             :         case DATETIME2BIND:
    2271             :                 return SYBMSDATETIMEOFFSET;
    2272             :                 break;
    2273             :         default:
    2274             :                 return TDS_INVALID_TYPE;
    2275             :         }
    2276             : }
    2277             : 
    2278             : /**
    2279             :  * \ingroup dblib_core
    2280             :  * \brief Convert one datatype to another.
    2281             :  *
    2282             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2283             :  * \param srctype datatype of the data to convert. 
    2284             :  * \param src buffer to convert
    2285             :  * \param srclen length of \a src
    2286             :  * \param desttype target datatype
    2287             :  * \param dest output buffer
    2288             :  * \param destlen size of \a dest
    2289             :  * \returns     On success, the count of output bytes in \a dest, else -1. On failure, it will call any user-supplied error handler. 
    2290             :  * \remarks      Causes of failure: 
    2291             :  *              - No such conversion unavailable. 
    2292             :  *              - Character data output was truncated, or numerical data overflowed or lost precision. 
    2293             :  *              - In converting character data to one of the numeric types, the string could not be interpreted as a number.  
    2294             :  *              
    2295             :  * Conversion functions are handled in the TDS layer.
    2296             :  * 
    2297             :  * The main reason for this is that \c ct-lib and \c ODBC (and presumably \c DBI) need
    2298             :  * to be able to do conversions between datatypes. This is possible because
    2299             :  * the format of complex data (dates, money, numeric, decimal) is defined by
    2300             :  * its representation on the wire; thus what we call \c DBMONEY is exactly its
    2301             :  * format on the wire. CLIs that need a different representation (ODBC?) 
    2302             :  * need to convert from this format anyway, so the code would already be in
    2303             :  * place.
    2304             :  * 
    2305             :  * Each datatype is also defined by its Server-type so all CLIs should be 
    2306             :  * able to map native types to server types as well.
    2307             :  *
    2308             :  * tds_convert() copies from src to dest and returns the output data length,
    2309             :  * period.  All padding and termination is the responsibility of the API library
    2310             :  * and is done post-conversion.  The peculiar rule in dbconvert() is that
    2311             :  * a \a destlen of -1 and a \a desttype of \c SYBCHAR means the output buffer
    2312             :  * should be null-terminated.
    2313             :  *  
    2314             :  * \sa dbaltbind(), dbaltbind_ps(), dbbind(), dbbind_ps(), dbconvert_ps(), dberrhandle(), dbsetnull(), dbsetversion(), dbwillconvert().
    2315             :  * \todo What happens if client does not reset values? 
    2316             :  * \todo Microsoft and Sybase define this function differently.  
    2317             :  */
    2318             : DBINT
    2319        2312 : dbconvert_ps(DBPROCESS * dbproc, int db_srctype, const BYTE * src, DBINT srclen,
    2320             :              int db_desttype, BYTE * dest, DBINT destlen, DBTYPEINFO * typeinfo)
    2321             : {
    2322             :         CONV_RESULT dres;
    2323             :         DBINT ret;
    2324             :         int i;
    2325             :         int len;
    2326             :         TDS_SERVER_TYPE srctype, desttype;
    2327             : 
    2328        2312 :         tdsdump_log(TDS_DBG_FUNC, "dbconvert_ps(%p, %s, %p, %d, %s, %p, %d, %p)\n",
    2329             :                     dbproc, tds_prdatatype(db_srctype), src, srclen,
    2330             :                     tds_prdatatype(db_desttype), dest, destlen, typeinfo);
    2331             :         /* dbproc and src can be NULLs */
    2332        2312 :         CHECK_PARAMETER(dest, SYBEACNV, -1);
    2333             : 
    2334        2312 :         DBPERROR_RETURN(!is_tds_type_valid(db_srctype), SYBEUDTY);
    2335        2312 :         srctype = (TDS_SERVER_TYPE) db_srctype;
    2336        2312 :         DBPERROR_RETURN(!is_tds_type_valid(db_desttype), SYBEUDTY);
    2337        2312 :         desttype = (TDS_SERVER_TYPE) db_desttype;
    2338             : 
    2339        2312 :         if (is_numeric_type(desttype)) {
    2340         480 :                 TDS_NUMERIC *d = &dres.n;
    2341             : 
    2342         480 :                 if (typeinfo == NULL) {
    2343           0 :                         if (is_numeric_type(srctype)) {
    2344           0 :                                 DBNUMERIC *s = (DBNUMERIC *) src;
    2345           0 :                                 d->precision = s->precision;
    2346           0 :                                 d->scale = s->scale;
    2347             :                         } else {
    2348           0 :                                 d->precision = 18;
    2349           0 :                                 d->scale = 0;
    2350             :                         }
    2351             :                 } else {
    2352         480 :                         d->precision = typeinfo->precision;
    2353         480 :                         d->scale = typeinfo->scale;
    2354             :                 }
    2355             :         }
    2356             : 
    2357        2312 :         if (0 == destlen) 
    2358             :                 return 0; 
    2359             : 
    2360        2312 :         if (src == NULL || srclen == 0) {
    2361         122 :                 int bind = dbbindtype(desttype);
    2362         122 :                 int size = tds_get_size_by_type(desttype);
    2363             : 
    2364         122 :                 if (bind == NTBSTRINGBIND) {
    2365          22 :                         if (destlen > 0) {
    2366             :                                 size = destlen;
    2367             :                                 bind = CHARBIND;
    2368             :                         } else { 
    2369          14 :                                 size = 1;
    2370          14 :                                 bind = NTBSTRINGBIND;
    2371             :                         }
    2372         100 :                 } else if (bind == BINARYBIND) {
    2373         100 :                         if (destlen > 0)
    2374             :                                 size = destlen;
    2375             :                         else
    2376          10 :                                 size = 0;
    2377             :                 }
    2378             : 
    2379         122 :                 dbgetnull(dbproc, bind, size, dest);
    2380         122 :                 return size;
    2381             :         }
    2382             : 
    2383             :         /* srclen of -1 means the source data is definitely NULL terminated */
    2384        2190 :         if (srclen == -1)
    2385         480 :                 srclen = (int)strlen((const char *) src);
    2386             : 
    2387             :         /* often times we are asked to convert a data type to itself */
    2388        2190 :         if (srctype == desttype && !is_numeric_type(desttype)) {
    2389         150 :                 ret = -2;  /* to make sure we always set it */
    2390         150 :                 tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() srctype == desttype\n");
    2391         150 :                 switch (desttype) {
    2392             : 
    2393          10 :                 case SYBBINARY:
    2394             :                 case SYBVARBINARY:
    2395             :                 case SYBIMAGE:
    2396          10 :                         if (srclen > destlen && destlen >= 0) {
    2397           0 :                                 dbperror(dbproc, SYBECOFL, 0);
    2398           0 :                                 ret = -1;
    2399             :                         } else {
    2400          10 :                                 memcpy(dest, src, srclen);
    2401          10 :                                 if (srclen < destlen)
    2402           0 :                                         memset(dest + srclen, 0, destlen - srclen);
    2403             :                                 ret = srclen;
    2404             :                         }
    2405             :                         break;
    2406             : 
    2407         140 :                 case SYBCHAR:
    2408             :                 case SYBVARCHAR:
    2409             :                 case SYBTEXT:
    2410             :                         /* srclen of -1 means the source data is definitely NULL terminated */
    2411         140 :                         if (srclen == -1)
    2412           0 :                                 srclen = (int)strlen((const char *) src);
    2413             : 
    2414         140 :                         switch (destlen) {
    2415             :                         case  0:        /* nothing to copy */
    2416             :                                 ret = 0;
    2417             :                                 break;
    2418             :                         case -1:        /* rtrim and null terminate */
    2419          80 :                                 while (srclen && src[srclen - 1] == ' ') {
    2420          20 :                                         --srclen;
    2421             :                                 }
    2422             :                                 /* fall thru */
    2423             :                         case -2:        /* just null terminate */
    2424          80 :                                 memcpy(dest, src, srclen);
    2425          80 :                                 dest[srclen] = '\0';
    2426          80 :                                 ret = srclen;
    2427          80 :                                 break;
    2428          60 :                         default:
    2429          60 :                                 assert(destlen > 0);
    2430          60 :                                 if (destlen < 0 || srclen > destlen) {
    2431          20 :                                         dbperror(dbproc, SYBECOFL, 0);
    2432          20 :                                         ret = -1;
    2433             :                                 } else {
    2434          40 :                                         memcpy(dest, src, srclen);
    2435          40 :                                         if (srclen < destlen)
    2436          20 :                                                 memset(dest + srclen, ' ', destlen - srclen);
    2437             :                                         ret = srclen;
    2438             :                                 }
    2439             :                                 break;
    2440             :                         }
    2441             :                         break;
    2442           0 :                 case SYBINT1:
    2443             :                 case SYBINT2:
    2444             :                 case SYBINT4:
    2445             :                 case SYBINT8:
    2446             :                 case SYBFLT8:
    2447             :                 case SYBREAL:
    2448             :                 case SYBBIT:
    2449             :                 case SYBBITN:
    2450             :                 case SYBMONEY:
    2451             :                 case SYBMONEY4:
    2452             :                 case SYBDATETIME:
    2453             :                 case SYBDATETIME4:
    2454             :                 case SYBDATE:
    2455             :                 case SYBTIME:
    2456             :                 case SYB5BIGDATETIME:
    2457             :                 case SYB5BIGTIME:
    2458             :                 case SYBUNIQUE:
    2459           0 :                         ret = tds_get_size_by_type(desttype);
    2460           0 :                         memcpy(dest, src, ret);
    2461           0 :                         break;
    2462             : 
    2463           0 :                 case SYBMSDATE:
    2464             :                 case SYBMSTIME:
    2465             :                 case SYBMSDATETIME2:
    2466             :                 case SYBMSDATETIMEOFFSET:
    2467           0 :                         ret = sizeof(TDS_DATETIMEALL);
    2468           0 :                         memcpy(dest, src, ret);
    2469             :                         break;
    2470             : 
    2471             :                 default:
    2472             :                         ret = -1;
    2473             :                         break;
    2474             :                 }
    2475         130 :                 assert(ret > -2);
    2476             :                 return ret;
    2477             :         }
    2478             :         /* end srctype == desttype */
    2479             : 
    2480             :         /*
    2481             :          * Character types need no conversion.  Just move the data.
    2482             :          */
    2483        2040 :         if (is_similar_type(srctype, desttype)) {
    2484         662 :                 if (src && dest && srclen > 0 && destlen >= srclen) {
    2485         662 :                         memcpy(dest, src, srclen);
    2486         662 :                         return srclen;
    2487             :                 }
    2488             :         }
    2489             : 
    2490        1378 :         tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() calling tds_convert\n");
    2491             : 
    2492        1378 :         len = tds_convert(g_dblib_ctx.tds_ctx, srctype, src, srclen, desttype, &dres);
    2493        1378 :         tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() called tds_convert returned %d\n", len);
    2494             : 
    2495        1378 :         if (len < 0) {
    2496           0 :                 _dblib_convert_err(dbproc, len);
    2497           0 :                 return -1;
    2498             :         }
    2499             : 
    2500        1378 :         switch (desttype) {
    2501           0 :         case SYBBINARY:
    2502             :         case SYBVARBINARY:
    2503             :         case SYBIMAGE:
    2504           0 :                 if (len > destlen && destlen >= 0) {
    2505           0 :                         dbperror(dbproc, SYBECOFL, 0);
    2506           0 :                         ret = -1;
    2507             :                 } else {
    2508           0 :                         memcpy(dest, dres.ib, len);
    2509           0 :                         if (len < destlen)
    2510           0 :                                 memset(dest + len, 0, destlen - len);
    2511             :                         ret = len;
    2512             :                 }
    2513           0 :                 free(dres.ib);
    2514           0 :                 break;
    2515         480 :         case SYBINT1:
    2516             :         case SYBINT2:
    2517             :         case SYBINT4:
    2518             :         case SYBINT8:
    2519             :         case SYBFLT8:
    2520             :         case SYBREAL:
    2521             :         case SYBBIT:
    2522             :         case SYBBITN:
    2523             :         case SYBMONEY:
    2524             :         case SYBMONEY4:
    2525             :         case SYBDATETIME:
    2526             :         case SYBDATETIME4:
    2527             :         case SYBTIME:
    2528             :         case SYBDATE:
    2529             :         case SYB5BIGDATETIME:
    2530             :         case SYB5BIGTIME:
    2531             :         case SYBUNIQUE:
    2532             :         case SYBMSDATE:
    2533             :         case SYBMSTIME:
    2534             :         case SYBMSDATETIME2:
    2535             :         case SYBMSDATETIMEOFFSET:
    2536             :         case SYBNUMERIC:
    2537             :         case SYBDECIMAL:
    2538         480 :                 memcpy(dest, &(dres.ti), len);
    2539         480 :                 ret = len;
    2540         480 :                 break;
    2541         898 :         case SYBCHAR:
    2542             :         case SYBVARCHAR:
    2543             :         case SYBTEXT:
    2544         898 :                 tdsdump_log(TDS_DBG_INFO1, "dbconvert_ps() outputting %d bytes character data destlen = %d \n", len, destlen);
    2545             : 
    2546         898 :                 if (destlen < -2)
    2547             :                         destlen = 0;    /* failure condition */
    2548             : 
    2549         898 :                 switch (destlen) {
    2550             :                 case 0:
    2551             :                         ret = -1;
    2552             :                         break;
    2553         228 :                 case -1:        /* rtrim and null terminate */
    2554         228 :                         for (i = len - 1; i >= 0 && dres.c[i] == ' '; --i) {
    2555           0 :                                 len = i;
    2556             :                         }
    2557         228 :                         memcpy(dest, dres.c, len);
    2558         228 :                         dest[len] = '\0';
    2559         228 :                         ret = len;
    2560         228 :                         break;
    2561           0 :                 case -2:        /* just null terminate */
    2562           0 :                         memcpy(dest, dres.c, len);
    2563           0 :                         dest[len] = 0;
    2564           0 :                         ret = len;
    2565           0 :                         break;
    2566         670 :                 default:
    2567         670 :                         assert(destlen > 0);
    2568         670 :                         if (destlen < 0 || len > destlen) {
    2569           0 :                                 dbperror(dbproc, SYBECOFL, 0);
    2570           0 :                                 ret = -1;
    2571           0 :                                 tdsdump_log(TDS_DBG_INFO1, "%d bytes type %d -> %d, destlen %d < %d required\n",
    2572             :                                             srclen, srctype, desttype, destlen, len);
    2573             :                                 break;
    2574             :                         }
    2575             :                         /* else pad with blanks */
    2576         670 :                         memcpy(dest, dres.c, len);
    2577         670 :                         if (len < destlen)
    2578         670 :                                 memset(dest + len, ' ', destlen - len);
    2579             :                         ret = len;
    2580             : 
    2581             :                         break;
    2582             :                 }
    2583             : 
    2584         898 :                 free(dres.c);
    2585             : 
    2586         898 :                 break;
    2587           0 :         default:
    2588           0 :                 tdsdump_log(TDS_DBG_INFO1, "error: dbconvert_ps(): unrecognized desttype %d \n", desttype);
    2589             :                 ret = -1;
    2590             :                 break;
    2591             : 
    2592             :         }
    2593             :         return (ret);
    2594             : }
    2595             : 
    2596             : /**
    2597             :  * \ingroup dblib_core
    2598             :  * \brief cf. dbconvert_ps(), above
    2599             :  * 
    2600             :  * \em Sybase: Convert numeric types.
    2601             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2602             :  * \param srctype datatype of the data to convert. 
    2603             :  * \param src buffer to convert
    2604             :  * \param srclen length of \a src
    2605             :  * \param desttype target datatype
    2606             :  * \param dest output buffer
    2607             :  * \param destlen size of \a dest
    2608             :  * \param typeinfo address of a \c DBTYPEINFO structure that governs the precision & scale of the output, may be \c NULL.
    2609             :  * \sa dbaltbind(), dbaltbind_ps(), dbbind(), dbbind_ps(), dbconvert_ps(), dberrhandle(), dbsetnull(), dbsetversion(), dbwillconvert().
    2610             :  */
    2611             : DBINT
    2612        1832 : dbconvert(DBPROCESS * dbproc,
    2613             :           int srctype, const BYTE * src, DBINT srclen, int desttype, BYTE * dest, DBINT destlen)
    2614             : {
    2615        1832 :         DBTYPEINFO ti, *pti = NULL;
    2616             : 
    2617        1832 :         tdsdump_log(TDS_DBG_FUNC, "dbconvert(%p)\n", dbproc);
    2618             :         /* dbproc can be NULL*/
    2619             : 
    2620        1832 :         DBPERROR_RETURN(!is_tds_type_valid(desttype), SYBEUDTY);
    2621             : 
    2622        1832 :         if (is_numeric_type(desttype)) {
    2623             :                 DBNUMERIC *num;
    2624             : 
    2625             :                 /* FIXME what happen if client do not reset values ??? */
    2626           0 :                 if (dbproc->msdblib) {
    2627           0 :                         num = (DBNUMERIC *) dest;
    2628           0 :                         ti.precision = num->precision;
    2629           0 :                         ti.scale = num->scale;
    2630           0 :                         pti = &ti;
    2631             :                 } else {
    2632             :                         /* for Sybase passing NULL as DBTYPEINFO is fine */
    2633             :                 }
    2634             :         }
    2635             : 
    2636        1832 :         return dbconvert_ps(dbproc, srctype, src, srclen, desttype, dest, destlen, pti);
    2637             : }
    2638             : 
    2639             : /**
    2640             :  * \ingroup dblib_core
    2641             :  * \brief Tie a host variable to a resultset column.
    2642             :  * 
    2643             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2644             :  * \param column Nth column, starting at 1.
    2645             :  * \param vartype datatype of the host variable that will receive the data
    2646             :  * \param varlen size of host variable pointed to \a varaddr
    2647             :  * \param varaddr address of host variable
    2648             :  * \retval SUCCEED everything worked.
    2649             :  * \retval FAIL no such \a column or no such conversion possible, or target buffer too small.
    2650             :  * \sa 
    2651             :  */
    2652             : RETCODE
    2653       21718 : dbbind(DBPROCESS * dbproc, int column, int vartype, DBINT varlen, BYTE * varaddr)
    2654             : {
    2655       21718 :         TDSCOLUMN *colinfo = NULL;
    2656             :         TDSRESULTINFO* results;
    2657             :         TDS_SERVER_TYPE srctype, desttype;
    2658             : 
    2659       21718 :         tdsdump_log(TDS_DBG_FUNC, "dbbind(%p, %d, %d, %d, %p)\n", dbproc, column, vartype, varlen, varaddr);
    2660       21718 :         CHECK_CONN(FAIL);
    2661       21718 :         CHECK_PARAMETER(varaddr, SYBEABNV, FAIL);
    2662             :         
    2663       21718 :         results = dbproc->tds_socket->res_info;
    2664             :         
    2665       21718 :         if (results == NULL || results->num_cols < column || column < 1) {
    2666           0 :                 dbperror(dbproc, SYBEABNC, 0);
    2667           0 :                 return FAIL;
    2668             :         }
    2669             :         
    2670       21718 :         if (varlen < 0) {
    2671         154 :                 switch (vartype) {
    2672          90 :                 case CHARBIND:
    2673             :                 case STRINGBIND:
    2674             :                 case NTBSTRINGBIND:
    2675             :                 case VARYCHARBIND:
    2676             :                 case VARYBINBIND:
    2677             :                         /* 
    2678             :                          * No message for this error.  Documentation doesn't define varlen < 0, but 
    2679             :                          * experimentation with Sybase db-lib shows it's accepted as if zero. 
    2680             :                          */
    2681          90 :                         tdsdump_log(TDS_DBG_FUNC, "dbbind: setting varlen (%d) to 0\n", varlen);
    2682             :                         varlen = 0;
    2683             :                         break;
    2684             :                 }
    2685             :         }
    2686             : 
    2687       21628 :         if (0 == varlen) {              /* "Note that if varlen is 0, no padding takes place." */
    2688       21354 :                 switch (vartype) {
    2689       10906 :                 case CHARBIND:
    2690             :                 case STRINGBIND:
    2691             :                 case NTBSTRINGBIND:
    2692       10906 :                         varlen = -1;
    2693       10906 :                         break;
    2694             :                 default:
    2695             :                         break;          /* dbconvert: "The destlen is ignored for all fixed-length, non-NULL data types." */
    2696             :                 }
    2697         364 :         }
    2698             : 
    2699       21718 :         dbproc->avail_flag = FALSE;
    2700             : 
    2701       21718 :         colinfo = dbproc->tds_socket->res_info->columns[column - 1];
    2702       21718 :         srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
    2703       21718 :         desttype = dblib_bound_type(vartype);
    2704       21718 :         if (desttype == TDS_INVALID_TYPE) {
    2705           0 :                 dbperror(dbproc, SYBEBTYP, 0);
    2706           0 :                 return FAIL;
    2707             :         }
    2708             : 
    2709       21718 :         if (!dbwillconvert(srctype, desttype)) {
    2710           0 :                 dbperror(dbproc, SYBEABMT, 0);
    2711           0 :                 return FAIL;
    2712             :         }
    2713             : 
    2714       21718 :         colinfo->column_varaddr = (char *) varaddr;
    2715       21718 :         colinfo->column_bindtype = vartype;
    2716       21718 :         colinfo->column_bindlen = varlen;
    2717             : 
    2718       21718 :         return SUCCEED;
    2719             : }                               /* dbbind()  */
    2720             : 
    2721             : /**
    2722             :  * \ingroup dblib_core
    2723             :  * \brief set name and location of the \c interfaces file FreeTDS should use to look up a servername.
    2724             :  * 
    2725             :  * Does not affect lookups or location of \c freetds.conf.  
    2726             :  * \param filename name of \c interfaces. It can be NULL (reset to default)
    2727             :  * \sa dbopen()
    2728             :  */
    2729             : void
    2730           0 : dbsetifile(char *filename)
    2731             : {
    2732           0 :         tdsdump_log(TDS_DBG_FUNC, "dbsetifile(%s)\n", filename? filename : "0x00");
    2733             : 
    2734           0 :         tds_set_interfaces_file_loc(filename);
    2735           0 : }
    2736             : 
    2737             : /**
    2738             :  * \ingroup dblib_core
    2739             :  * \brief Tie a null-indicator to a regular result column.
    2740             :  * 
    2741             :  * 
    2742             :  * When a row is fetched, the indicator variable tells the state of the column's data.  
    2743             :  * 
    2744             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2745             :  * \param column Nth column in the result set, starting with 1.
    2746             :  * \param indicator address of host variable.
    2747             :  * \retval SUCCEED variable accepted.
    2748             :  * \retval FAIL \a indicator is NULL or \a column is out of range. 
    2749             :  * \remarks Contents of \a indicator are set with \c dbnextrow().  Possible values are:
    2750             :  * -  0 \a column bound successfully
    2751             :  * - -1 \a column is NULL.
    2752             :  * - >0 true length of data, had \a column not been truncated due to insufficient space in the columns bound host variable .  
    2753             :  * \sa dbanullbind(), dbbind(), dbdata(), dbdatlen(), dbnextrow().
    2754             :  */
    2755             : RETCODE
    2756         240 : dbnullbind(DBPROCESS * dbproc, int column, DBINT * indicator)
    2757             : {
    2758             :         TDSCOLUMN *colinfo;
    2759             : 
    2760         240 :         tdsdump_log(TDS_DBG_FUNC, "dbnullbind(%p, %d, %p)\n", dbproc, column, indicator);
    2761             : 
    2762         240 :         colinfo = dbcolptr(dbproc, column);
    2763         240 :         if (!colinfo)
    2764             :                 return FAIL; /* dbcolptr sent SYBECNOR, Column number out of range */
    2765             : 
    2766         240 :         colinfo->column_nullbind = (TDS_SMALLINT *)indicator;
    2767         240 :         return SUCCEED;
    2768             : }
    2769             : 
    2770             : /**
    2771             :  * \ingroup dblib_core
    2772             :  * \brief Tie a null-indicator to a compute result column.
    2773             :  * 
    2774             :  * 
    2775             :  * When a row is fetched, the indicator variable tells the state of the column's data.  
    2776             :  * 
    2777             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2778             :  * \param computeid identifies which one of potientially many compute rows is meant. The first compute
    2779             :  * clause has \a computeid == 1.  
    2780             :  * \param column Nth column in the result set, starting with 1.
    2781             :  * \param indicator address of host variable.
    2782             :  * \retval SUCCEED variable accepted.
    2783             :  * \retval FAIL \a indicator is NULL or \a column is out of range. 
    2784             :  * \remarks Contents of \a indicator are set with \c dbnextrow().  Possible values are:
    2785             :  * -  0 \a column bound successfully
    2786             :  * - -1 \a column is NULL.
    2787             :  * - >0 true length of data, had \a column not been truncated due to insufficient space in the columns bound host variable .  
    2788             :  * \sa dbadata(), dbadlen(), dbaltbind(), dbnextrow(), dbnullbind().
    2789             :  * \todo Never fails, but only because failure conditions aren't checked.  
    2790             :  */
    2791             : RETCODE
    2792           0 : dbanullbind(DBPROCESS * dbproc, int computeid, int column, DBINT * indicator)
    2793             : {
    2794             :         TDSCOLUMN *curcol;
    2795             : 
    2796           0 :         tdsdump_log(TDS_DBG_FUNC, "dbanullbind(%p, %d, %d, %p)\n", dbproc, computeid, column, indicator);
    2797             : 
    2798           0 :         curcol = dbacolptr(dbproc, computeid, column, true);
    2799           0 :         if (!curcol)
    2800             :                 return FAIL;
    2801             : 
    2802             :         /*
    2803             :          *  XXX Need to check for possibly problems before assuming
    2804             :          *  everything is okay
    2805             :          */
    2806           0 :         curcol->column_nullbind = (TDS_SMALLINT *)indicator;
    2807             : 
    2808           0 :         return SUCCEED;
    2809             : }
    2810             : 
    2811             : /**
    2812             :  * \ingroup dblib_core
    2813             :  * \brief Indicates whether or not the count returned by dbcount is real (Microsoft-compatibility feature).
    2814             :  * 
    2815             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2816             :  * \returns TRUE if the count returned by dbcount is real or FALSE if the count returned by dbcount is not real.
    2817             :  * \sa DBCOUNT(), dbcount().
    2818             :  */
    2819             : BOOL
    2820           0 : dbiscount(DBPROCESS * dbproc)
    2821             : {
    2822           0 :         tdsdump_log(TDS_DBG_FUNC, "dbiscount(%p)\n", dbproc);
    2823           0 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    2824             : 
    2825           0 :         return dbproc->tds_socket && dbproc->tds_socket->rows_affected != TDS_NO_COUNT;
    2826             : }
    2827             : 
    2828             : /**
    2829             :  * \ingroup dblib_core
    2830             :  * \brief Get count of rows processed
    2831             :  *
    2832             :  *
    2833             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2834             :  * \returns
    2835             :  *      - for insert/update/delete, count of rows affected.
    2836             :  *      - for select, count of rows returned, after all rows have been fetched.
    2837             :  * \sa DBCOUNT(), dbnextrow(), dbresults().
    2838             :  */
    2839             : DBINT
    2840        1294 : dbcount(DBPROCESS * dbproc)
    2841             : {
    2842        1294 :         tdsdump_log(TDS_DBG_FUNC, "dbcount(%p)\n", dbproc);
    2843        1294 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    2844             : 
    2845        1294 :         if (!dbproc->tds_socket || dbproc->tds_socket->rows_affected == TDS_NO_COUNT)
    2846             :                 return -1;
    2847         796 :         return (DBINT)dbproc->tds_socket->rows_affected;
    2848             : }
    2849             : 
    2850             : /**
    2851             :  * \ingroup dblib_core
    2852             :  * \brief Clear \a n rows from the row buffer.
    2853             :  * 
    2854             :  * 
    2855             :  * \param dbproc contains all information needed by db-lib to manage communications with the server. 
    2856             :  * \param n number of rows to remove, >= 0.
    2857             :  * \sa dbgetrow(), dbnextrow(), dbsetopt().
    2858             :  */
    2859             : void
    2860         200 : dbclrbuf(DBPROCESS * dbproc, DBINT n)
    2861             : {
    2862         200 :         tdsdump_log(TDS_DBG_FUNC, "dbclrbuf(%p, %d)\n", dbproc, n);
    2863         200 :         CHECK_PARAMETER(dbproc, SYBENULL, );
    2864             : 
    2865         200 :         if (n <= 0)
    2866             :                 return;
    2867             : 
    2868         200 :         if (dbproc->dbopts[DBBUFFER].factive) {
    2869         160 :                 DBPROC_ROWBUF * buf = &(dbproc->row_buf);
    2870         160 :                 int count = buffer_count(buf);
    2871         160 :                 if (n >= count)
    2872         140 :                         n = count - 1;
    2873         160 :                 buffer_delete_rows(&(dbproc->row_buf), n);
    2874             :         }
    2875             : }
    2876             : 
    2877             : /**
    2878             :  * \ingroup dblib_core
    2879             :  * \brief Test whether or not a datatype can be converted to another datatype
    2880             :  * 
    2881             :  * \param srctype type converting from
    2882             :  * \param desttype type converting to
    2883             :  * \remarks dbwillconvert() lies sometimes.  Some datatypes \em should be convertible but aren't yet in our implementation.  
    2884             :  *          Legal unimplemented conversions return \em TRUE.  
    2885             :  * \retval TRUE convertible, or should be.
    2886             :  * \retval FAIL not convertible.  
    2887             :  * \sa dbaltbind(), dbbind(), dbconvert(), dbconvert_ps(), \c src/dblib/unittests/convert().c().
    2888             :  */
    2889             : DBBOOL
    2890       22594 : dbwillconvert(int srctype, int desttype)
    2891             : {
    2892       22594 :         tdsdump_log(TDS_DBG_FUNC, "dbwillconvert(%s, %s)\n", tds_prdatatype(srctype), tds_prdatatype(desttype));
    2893       22594 :         return tds_willconvert(srctype, desttype) ? TRUE : FALSE;
    2894             : }
    2895             : 
    2896             : static int
    2897        1448 : dblib_coltype(TDSCOLUMN *colinfo)
    2898             : {
    2899        1448 :         switch (colinfo->column_type) {
    2900             :         case SYBVARCHAR:
    2901             :                 return SYBCHAR;
    2902           0 :         case SYBVARBINARY:
    2903             :                 return SYBBINARY;
    2904           0 :         case SYBLONGCHAR:
    2905             :         case SYBUNITEXT:
    2906             :         case SYBMSXML:
    2907             :                 return SYBTEXT;
    2908             :         /* these types are handled by tds_get_conversion_type */
    2909             :         case SYBBITN:
    2910             :         case SYBDATEN:
    2911             :         case SYBDATETIMN:
    2912             :         case SYBFLTN:
    2913             :         case SYBINTN:
    2914             :         case SYBMONEYN:
    2915             :         case SYBTIMEN:
    2916             :         case SYBUINTN:
    2917             :         case SYBMSTABLE:
    2918             :                 break;
    2919             :         /* these types are supported */
    2920             :         case SYBCHAR:
    2921             :         case SYBINT1:
    2922             :         case SYBINT2:
    2923             :         case SYBINT4:
    2924             :         case SYBINT8:
    2925             :         case SYBFLT8:
    2926             :         case SYBDATETIME:
    2927             :         case SYBBIT:
    2928             :         case SYBTEXT:
    2929             :         case SYBNTEXT:
    2930             :         case SYBIMAGE:
    2931             :         case SYBMONEY4:
    2932             :         case SYBMONEY:
    2933             :         case SYBDATETIME4:
    2934             :         case SYBREAL:
    2935             :         case SYBBINARY:
    2936             :         case SYBVOID:
    2937             :         case SYBNUMERIC:
    2938             :         case SYBDECIMAL:
    2939             :         case SYBNVARCHAR:
    2940             :         case SYBDATE:
    2941             :         case SYBTIME:
    2942             :         case SYB5BIGDATETIME:
    2943             :         case SYB5BIGTIME:
    2944             :         case SYBMSDATE:
    2945             :         case SYBMSTIME:
    2946             :         case SYBMSDATETIME2:
    2947             :         case SYBMSDATETIMEOFFSET:
    2948             :                 break;
    2949             :         /* exclude cardinal types */
    2950             :         case XSYBVARBINARY:
    2951             :         case XSYBBINARY:
    2952             :         case XSYBNVARCHAR:
    2953             :         case XSYBVARCHAR:
    2954             :         case XSYBNCHAR:
    2955             :         case SYB5INT8:
    2956             :                 break;
    2957             : #if !ENABLE_EXTRA_CHECKS
    2958             :         /* TODO these should be handled in some ways */
    2959             :         case SYBUNIQUE: /* or SYBBLOB */
    2960             :         case SYBVARIANT:
    2961             :         case SYBMSUDT:
    2962             :         case SYBLONGBINARY:
    2963             :         case SYBUINT1:
    2964             :         case SYBUINT2:
    2965             :         case SYBUINT4:
    2966             :         case SYBUINT8:
    2967             :         case SYBINTERVAL:
    2968             :         case SYBSINT1:
    2969             :         case SYBXML:
    2970             :                 break;
    2971             : #endif
    2972             :         }
    2973         328 :         return tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
    2974             : }
    2975             : 
    2976             : /**
    2977             :  * \ingroup dblib_core
    2978             :  * \brief Get the datatype of a regular result set column. 
    2979             :  * 
    2980             :  * 
    2981             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2982             :  * \param column Nth in the result set, starting from 1.
    2983             :  * \returns \c SYB* datetype token value, or zero if \a column out of range
    2984             :  * \sa dbcollen(), dbcolname(), dbdata(), dbdatlen(), dbnumcols(), dbprtype(), dbvarylen().
    2985             :  */
    2986             : int
    2987        1562 : dbcoltype(DBPROCESS * dbproc, int column)
    2988             : {
    2989             :         TDSCOLUMN *colinfo;
    2990             : 
    2991        1562 :         tdsdump_log(TDS_DBG_FUNC, "dbcoltype(%p, %d)\n", dbproc, column);
    2992             : 
    2993        1562 :         colinfo = dbcolptr(dbproc, column);
    2994        1562 :         if (!colinfo)
    2995             :                 return -1;
    2996             : 
    2997        1432 :         return dblib_coltype(colinfo);
    2998             : }
    2999             : 
    3000             : /**
    3001             :  * \ingroup dblib_core
    3002             :  * \brief Get user-defined datatype of a regular result column.
    3003             :  * 
    3004             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3005             :  * \param column Nth in the result set, starting from 1.
    3006             :  * \returns \c SYB* datetype token value, or -1 if \a column out of range
    3007             :  * \sa dbaltutype(), dbcoltype().
    3008             :  */
    3009             : int
    3010           0 : dbcolutype(DBPROCESS * dbproc, int column)
    3011             : {
    3012             :         TDSCOLUMN *colinfo;
    3013             : 
    3014           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcolutype(%p, %d)\n", dbproc, column);
    3015             : 
    3016           0 :         colinfo = dbcolptr(dbproc, column);
    3017           0 :         if (!colinfo)
    3018             :                 return -1;
    3019             : 
    3020           0 :         return colinfo->column_usertype;
    3021             : }
    3022             : 
    3023             : /**
    3024             :  * \ingroup dblib_core
    3025             :  * \brief Get precision and scale information for a regular result column.
    3026             :  * 
    3027             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3028             :  * \param column Nth in the result set, starting from 1.
    3029             :  * \return Pointer to a DBTYPEINFO structure .  NULL \a column is out of range.
    3030             :  * \sa dbcollen(), dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols(), dbprtype(), dbvarylen().
    3031             :  */
    3032             : DBTYPEINFO *
    3033           0 : dbcoltypeinfo(DBPROCESS * dbproc, int column)
    3034             : {
    3035             :         /* moved typeinfo from static into dbproc structure to make thread safe.  (mlilback 11/7/01) */
    3036             :         TDSCOLUMN *colinfo;
    3037             : 
    3038           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcoltypeinfo(%p, %d)\n", dbproc, column);
    3039             : 
    3040           0 :         colinfo = dbcolptr(dbproc, column);
    3041           0 :         if (!colinfo)
    3042             :                 return NULL;
    3043             : 
    3044           0 :         dbproc->typeinfo.precision = colinfo->column_prec;
    3045           0 :         dbproc->typeinfo.scale = colinfo->column_scale;
    3046           0 :         return &dbproc->typeinfo;
    3047             : }
    3048             : 
    3049             : /**
    3050             :  * \brief Get a bunch of column attributes with a single call (Microsoft-compatibility feature).  
    3051             :  * 
    3052             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3053             :  * \param type must be CI_REGULAR or CI_ALTERNATE (CI_CURSOR is defined by the vendor, but is not yet implemented).
    3054             :  * \param column Nth in the result set, starting from 1.
    3055             :  * \param computeid (ignored)
    3056             :  * \param pdbcol address of structure to be populated by this function.  
    3057             :  * \return SUCCEED or FAIL. 
    3058             :  * \sa dbcolbrowse(), dbqual(), dbtabbrowse(), dbtabcount(), dbtabname(), dbtabsource(), dbtsnewlen(), dbtsnewval(), dbtsput().
    3059             :  * \todo Support cursor rows. 
    3060             :  */
    3061             : RETCODE
    3062           0 : dbcolinfo(DBPROCESS *dbproc, CI_TYPE type, DBINT column, DBINT computeid, DBCOL *pdbcol)
    3063             : {
    3064             :         DBTYPEINFO *ps;
    3065             :         TDSCOMPUTEINFO *info;
    3066             :         TDSCOLUMN *colinfo;
    3067             :         unsigned int i;
    3068             : 
    3069           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcolinfo(%p, %d, %d, %d, %p)\n", dbproc, type, column, computeid, pdbcol);
    3070             : 
    3071           0 :         colinfo = dbcolptr(dbproc, column);
    3072           0 :         if (!colinfo)
    3073             :                 return FAIL;
    3074             :                 
    3075           0 :         CHECK_NULP(pdbcol, "dbcolinfo", 5, FAIL);
    3076             : 
    3077           0 :         if (type == CI_REGULAR) {
    3078             : 
    3079           0 :                 strlcpy(pdbcol->Name, dbcolname(dbproc, column), sizeof(pdbcol->Name));
    3080           0 :                 strlcpy(pdbcol->ActualName, dbcolname(dbproc, column), sizeof(pdbcol->ActualName));
    3081           0 :                 strlcpy(pdbcol->TableName, tds_dstr_cstr(&colinfo->table_name), sizeof(pdbcol->TableName));
    3082             : 
    3083           0 :                 pdbcol->Type = dbcoltype(dbproc, column);
    3084           0 :                 pdbcol->UserType = dbcolutype(dbproc, column);
    3085           0 :                 pdbcol->MaxLength = dbcollen(dbproc, column);
    3086           0 :                 pdbcol->Null = _dbnullable(dbproc, column);
    3087           0 :                 pdbcol->VarLength = dbvarylen(dbproc, column);
    3088             : 
    3089           0 :                 ps = dbcoltypeinfo(dbproc, column);
    3090             : 
    3091           0 :                 if( ps ) {
    3092           0 :                         pdbcol->Precision = ps->precision;
    3093           0 :                         pdbcol->Scale = ps->scale;
    3094             :                 }
    3095             : 
    3096           0 :                 pdbcol->Updatable = colinfo->column_writeable ? TRUE : FALSE;
    3097           0 :                 pdbcol->Identity = colinfo->column_identity ? TRUE : FALSE;
    3098             : 
    3099           0 :                 return SUCCEED;
    3100             :         }
    3101             :   
    3102           0 :         if (type == CI_ALTERNATE) {
    3103             : 
    3104           0 :                 if (computeid == 0)
    3105             :                         return FAIL;
    3106             : 
    3107           0 :                 for (i = 0;; ++i) {
    3108           0 :                         if (i >= dbproc->tds_socket->num_comp_info)
    3109             :                                 return FAIL;
    3110           0 :                         info = dbproc->tds_socket->comp_info[i];
    3111           0 :                         if (info->computeid == computeid)
    3112             :                                 break;
    3113             :                 }
    3114             : 
    3115             :                 /* if either the compute id or the column number are invalid, return -1 */
    3116           0 :                 if (column < 1 || column > info->num_cols)
    3117             :                         return FAIL;
    3118             : 
    3119           0 :                 colinfo = info->columns[column - 1];
    3120             : 
    3121           0 :                 strlcpy(pdbcol->Name, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->Name));
    3122           0 :                 strlcpy(pdbcol->ActualName, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->ActualName));
    3123           0 :                 strlcpy(pdbcol->TableName, tds_dstr_cstr(&colinfo->table_name), sizeof(pdbcol->TableName));
    3124             : 
    3125           0 :                 pdbcol->Type = dbalttype(dbproc, computeid, column);
    3126           0 :                 pdbcol->UserType = dbaltutype(dbproc, computeid, column);
    3127           0 :                 pdbcol->MaxLength = dbaltlen(dbproc, computeid, column);
    3128           0 :                 if (colinfo->column_nullable) 
    3129           0 :                         pdbcol->Null = TRUE;
    3130             :                 else
    3131           0 :                         pdbcol->Null = FALSE;
    3132             : 
    3133           0 :                 pdbcol->VarLength = FALSE;
    3134             : 
    3135           0 :                 if (colinfo->column_nullable
    3136           0 :                     || is_nullable_type(colinfo->column_type))
    3137           0 :                         pdbcol->VarLength = TRUE;
    3138             : 
    3139           0 :                 pdbcol->Precision = colinfo->column_prec;
    3140           0 :                 pdbcol->Scale = colinfo->column_scale;
    3141             :   
    3142           0 :                 pdbcol->Updatable = colinfo->column_writeable ? TRUE : FALSE ;
    3143           0 :                 pdbcol->Identity = colinfo->column_identity ? TRUE : FALSE ;
    3144             : 
    3145           0 :                 return SUCCEED;
    3146             :         }
    3147             : 
    3148             :         return FAIL;
    3149             : }
    3150             :  
    3151             : /**
    3152             :  * \ingroup dblib_core
    3153             :  * \brief Get base database column name for a result set column.  
    3154             :  * 
    3155             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3156             :  * \param column Nth in the result set, starting from 1.
    3157             :  * \return pointer to ASCII null-terminated string, the name of the column. On error, NULL.
    3158             :  * \sa dbcolbrowse(), dbqual(), dbtabbrowse(), dbtabcount(), dbtabname(), dbtabsource(), dbtsnewlen(), dbtsnewval(), dbtsput().
    3159             :  */
    3160             : char *
    3161           0 : dbcolsource(DBPROCESS * dbproc, int column)
    3162             : {
    3163             :         TDSCOLUMN *colinfo;
    3164             : 
    3165           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcolsource(%p, %d)\n", dbproc, column);
    3166             : 
    3167           0 :         colinfo = dbcolptr(dbproc, column);
    3168           0 :         if (!colinfo)
    3169             :                 return NULL;
    3170             : 
    3171           0 :         return tds_dstr_buf(tds_dstr_isempty(&colinfo->table_column_name) ?
    3172             :                 &colinfo->column_name :
    3173             :                 &colinfo->table_column_name);
    3174             : }
    3175             : 
    3176             : /**
    3177             :  * \ingroup dblib_core
    3178             :  * \brief Get size of a regular result column.
    3179             :  * 
    3180             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3181             :  * \param column Nth in the result set, starting from 1.
    3182             :  * \return size of the column (not of data in any particular row).  On error, -1.
    3183             :  * \sa dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
    3184             :  */
    3185             : DBINT
    3186           0 : dbcollen(DBPROCESS * dbproc, int column)
    3187             : {
    3188             :         TDSCOLUMN *colinfo;
    3189             : 
    3190           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcollen(%p, %d)\n", dbproc, column);
    3191             : 
    3192           0 :         colinfo = dbcolptr(dbproc, column);
    3193           0 :         if (!colinfo)
    3194             :                 return -1;
    3195             : 
    3196           0 :         return colinfo->column_size;
    3197             : }
    3198             : 
    3199             : /**
    3200             :  * \ingroup dblib_core
    3201             :  * \brief Get size of a result column needed to print column.
    3202             :  *
    3203             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3204             :  * \param column Nth in the result set, starting from 1.
    3205             :  * \return size of the column in characters (not of data in any particular row).  On error, -1.
    3206             :  * \sa dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols().
    3207             :  */
    3208             : DBINT
    3209           0 : dbprcollen(DBPROCESS * dbproc, int column)
    3210             : {
    3211             :         TDSCOLUMN *colinfo;
    3212             : 
    3213           0 :         tdsdump_log(TDS_DBG_FUNC, "dbprcollen(%p, %d)\n", dbproc, column);
    3214             : 
    3215           0 :         colinfo = dbcolptr(dbproc, column);
    3216           0 :         if (!colinfo)
    3217             :                 return 0;
    3218             : 
    3219           0 :         return _get_printable_size(colinfo);
    3220             : }
    3221             : 
    3222             : 
    3223             : /* dbvarylen(), pkleef@openlinksw.com 01/21/02 */
    3224             : /**
    3225             :  * \ingroup dblib_core
    3226             :  * \brief Determine whether a column can vary in size.
    3227             :  * 
    3228             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3229             :  * \param column Nth in the result set, starting from 1.
    3230             :  * \retval TRUE datatype of column can vary in size, or is nullable. 
    3231             :  * \retval FALSE datatype of column is fixed and is not nullable. 
    3232             :  * \sa dbcollen(), dbcolname(), dbcoltype(), dbdata(), dbdatlen(), dbnumcols(), dbprtype().
    3233             :  */
    3234             : DBINT
    3235        1456 : dbvarylen(DBPROCESS * dbproc, int column)
    3236             : {
    3237             :         TDSCOLUMN *colinfo;
    3238             : 
    3239        1456 :         tdsdump_log(TDS_DBG_FUNC, "dbvarylen(%p, %d)\n", dbproc, column);
    3240             : 
    3241        1456 :         colinfo = dbcolptr(dbproc, column);
    3242        1456 :         if (!colinfo)
    3243             :                 return FALSE;
    3244             : 
    3245        1456 :         if (colinfo->column_nullable)
    3246             :                 return TRUE;
    3247             : 
    3248        1180 :         switch (colinfo->column_type) {
    3249             :                 /* variable length fields */
    3250             :         case SYBNVARCHAR:
    3251             :         case SYBVARBINARY:
    3252             :         case SYBVARCHAR:
    3253             :         case XSYBVARCHAR:
    3254             :         case XSYBNVARCHAR:
    3255             :         case XSYBVARBINARY:
    3256             :                 return TRUE;
    3257             : 
    3258             :                 /* types that can be null */
    3259             :         case SYBBITN:
    3260             :         case SYBDATETIMN:
    3261             :         case SYBDECIMAL:
    3262             :         case SYBFLTN:
    3263             :         case SYBINTN:
    3264             :         case SYBMONEYN:
    3265             :         case SYBNUMERIC:
    3266             :         case SYBDATEN:
    3267             :         case SYBTIMEN:
    3268             :         case SYBUINTN:
    3269             :         case SYB5BIGDATETIME:
    3270             :         case SYB5BIGTIME:
    3271             :         case SYBMSDATE:
    3272             :         case SYBMSTIME:
    3273             :         case SYBMSDATETIME2:
    3274             :         case SYBMSDATETIMEOFFSET:
    3275             :         case SYBVARIANT:
    3276             :         case SYBUNIQUE:
    3277             :                 return TRUE;
    3278             : 
    3279             :                 /* blob types */
    3280             :         case SYBIMAGE:
    3281             :         case SYBNTEXT:
    3282             :         case SYBTEXT:
    3283             :         case SYBLONGBINARY:
    3284             :         /* case SYBBLOB: */ /* same as SYBUNIQUE */
    3285             :         case SYB5INT8:
    3286             :         case SYBUNITEXT:
    3287             :         case SYBXML:
    3288             :         case SYBMSUDT:
    3289             :         case SYBMSXML:
    3290             :                 return TRUE;
    3291             :         case SYBMSTABLE:
    3292             :                 return TRUE;
    3293             : 
    3294           0 :         case SYBLONGCHAR:
    3295             :         /* case XSYBCHAR: */ /* same as SYBLONGCHAR */
    3296           0 :                 if (colinfo->column_varint_size >= 4)
    3297             :                         return TRUE;
    3298             :                 break;
    3299             : 
    3300             :                 /* with defined size*/
    3301             :         case SYBCHAR:
    3302             :         case SYBBINARY:
    3303             :         case XSYBNCHAR:
    3304             :         case XSYBBINARY:
    3305             :                 break;
    3306             : 
    3307             :                 /* fixed */
    3308             :         case SYBINT1:
    3309             :         case SYBINT2:
    3310             :         case SYBINT4:
    3311             :         case SYBFLT8:
    3312             :         case SYBDATETIME:
    3313             :         case SYBBIT:
    3314             :         case SYBMONEY4:
    3315             :         case SYBMONEY:
    3316             :         case SYBDATETIME4:
    3317             :         case SYBREAL:
    3318             :         case SYBINT8:
    3319             :         case SYBUINT1:
    3320             :         case SYBUINT2:
    3321             :         case SYBUINT4:
    3322             :         case SYBUINT8:
    3323             :         case SYBSINT1:
    3324             :         case SYBTIME:
    3325             :         case SYBDATE:
    3326             :         case SYBVOID:
    3327             :         case SYBINTERVAL:
    3328             :                 break;
    3329             :         }
    3330         628 :         return FALSE;
    3331             : }
    3332             : 
    3333             : /**
    3334             :  * \ingroup dblib_core
    3335             :  * \brief   Get size of current row's data in a regular result column.  
    3336             :  * 
    3337             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3338             :  * \param column Nth in the result set, starting from 1.
    3339             :  * \return size of the data, in bytes.
    3340             :  * \sa dbcollen(), dbcolname(), dbcoltype(), dbdata(), dbnumcols().
    3341             :  */
    3342             : DBINT
    3343        2344 : dbdatlen(DBPROCESS * dbproc, int column)
    3344             : {
    3345             :         DBINT len;
    3346             :         TDSCOLUMN *colinfo;
    3347             : 
    3348        2344 :         tdsdump_log(TDS_DBG_FUNC, "dbdatlen(%p, %d)\n", dbproc, column);
    3349             : 
    3350        2344 :         colinfo = dbcolptr(dbproc, column);
    3351        2344 :         if (!colinfo)
    3352             :                 return -1;      
    3353             : 
    3354        2344 :         len = (colinfo->column_cur_size < 0)? 0 : colinfo->column_cur_size;
    3355             : 
    3356        2344 :         tdsdump_log(TDS_DBG_FUNC, "dbdatlen() type = %d, len= %d\n", colinfo->column_type, len);
    3357             : 
    3358             :         return len;
    3359             : }
    3360             : 
    3361             : /**
    3362             :  * \ingroup dblib_core
    3363             :  * \brief Get address of data in a regular result column.
    3364             :  * 
    3365             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3366             :  * \param column Nth in the result set, starting from 1.
    3367             :  * \return pointer the data, or NULL if data are NULL, or if \a column is out of range.  
    3368             :  * \sa dbbind(), dbcollen(), dbcolname(), dbcoltype(), dbdatlen(), dbnumcols().
    3369             :  */
    3370             : BYTE *
    3371        3002 : dbdata(DBPROCESS * dbproc, int column)
    3372             : {
    3373        3002 :         tdsdump_log(TDS_DBG_FUNC, "dbdata(%p, %d)\n", dbproc, column);
    3374             : 
    3375        6004 :         return _dbcoldata(dbcolptr(dbproc, column));
    3376             : }
    3377             : 
    3378             : /** \internal
    3379             :  * \ingroup dblib_internal
    3380             :  * \brief Return data from a column
    3381             :  *
    3382             :  * \param colinfo contains information on a result column.
    3383             :  * \return pointer to the data, or NULL if data are NULL
    3384             :  * \sa dbdata(), dbretdata()
    3385             :  */
    3386             : static BYTE *
    3387             : _dbcoldata(TDSCOLUMN *colinfo)
    3388             : {
    3389             :         BYTE *res;
    3390             :         static const BYTE empty[1] = { 0 };
    3391             : 
    3392        3072 :         if (!colinfo || colinfo->column_cur_size < 0)
    3393             :                 return NULL;
    3394             : 
    3395        2528 :         res = colinfo->column_data;
    3396        2528 :         if (is_blob_col(colinfo))
    3397         274 :                 res = (BYTE *) ((TDSBLOB *) res)->textvalue;
    3398        2528 :         if (!res)
    3399             :                 return (BYTE *) empty;
    3400             :         return res;
    3401             : }
    3402             : 
    3403             : /**
    3404             :  * \ingroup dblib_core
    3405             :  * \brief Cancel the current command batch.
    3406             :  * 
    3407             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3408             :  * \retval SUCCEED always.
    3409             :  * \sa dbcanquery(), dbnextrow(), dbresults(), dbsetinterrupt(), dbsqlexec(), dbsqlok(), dbsqlsend().
    3410             :  * \todo Check for failure and return accordingly.
    3411             :  */
    3412             : RETCODE
    3413       10320 : dbcancel(DBPROCESS * dbproc)
    3414             : {
    3415             :         TDSSOCKET *tds;
    3416             : 
    3417       10320 :         tdsdump_log(TDS_DBG_FUNC, "dbcancel(%p)\n", dbproc);
    3418       10320 :         CHECK_CONN(FAIL);
    3419             : 
    3420       10320 :         tds = dbproc->tds_socket;
    3421             : 
    3422       10320 :         tds_send_cancel(tds);
    3423       10320 :         tds_process_cancel(tds);
    3424             : 
    3425       10320 :         return SUCCEED;
    3426             : }
    3427             : 
    3428             : /**
    3429             :  * \ingroup dblib_core
    3430             :  * \brief Determine size buffer required to hold the results returned by dbsprhead(), dbsprline(), and  dbspr1row().
    3431             :  * 
    3432             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3433             :  * \return size of buffer requirement, in bytes.  
    3434             :  * \remarks An esoteric function.
    3435             :  * \sa dbprhead(), dbprrow(), dbspr1row(), dbsprhead(), dbsprline().
    3436             :  */
    3437             : DBINT
    3438           0 : dbspr1rowlen(DBPROCESS * dbproc)
    3439             : {
    3440             :         TDSSOCKET *tds;
    3441           0 :         int col, len = 0;
    3442             : 
    3443           0 :         tdsdump_log(TDS_DBG_FUNC, "dbspr1rowlen(%p)\n", dbproc);
    3444           0 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    3445           0 :         CHECK_PARAMETER(dbproc->tds_socket, SYBEDDNE, 0);
    3446             : 
    3447             :         tds = dbproc->tds_socket;
    3448             :         
    3449           0 :         for (col = 0; col < tds->res_info->num_cols; col++) {
    3450           0 :                 TDSCOLUMN *colinfo = tds->res_info->columns[col];
    3451           0 :                 int collen = _get_printable_size(colinfo);
    3452           0 :                 int namlen = tds_dstr_len(&colinfo->column_name);
    3453             :                 
    3454           0 :                 len += collen > namlen ? collen : namlen;
    3455             :                 
    3456           0 :                 if (col > 0)         /* allow for the space between columns */
    3457           0 :                         len += dbstring_length(dbproc->dbopts[DBPRCOLSEP].param);
    3458             :         }
    3459             :         
    3460           0 :         return ++len;   /* allow for the nul */
    3461             : }
    3462             : 
    3463             : /**
    3464             :  * \ingroup dblib_core
    3465             :  * \brief Print a regular result row to a buffer.  
    3466             :  * 
    3467             :  * Fills a buffer with one data row, represented as a null-terminated ASCII string.  Helpful for debugging.  
    3468             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3469             :  * \param buffer \em output: Address of a buffer to hold ASCII null-terminated string.
    3470             :  * \param buf_len size of \a buffer, in bytes. 
    3471             :  * \retval SUCCEED on success.
    3472             :  * \retval FAIL trouble encountered.  
    3473             :  * \sa dbclropt(), dbisopt(), dbprhead(), dbprrow(), dbspr1rowlen(), dbsprhead(), dbsprline().
    3474             :  */
    3475             : RETCODE
    3476           0 : dbspr1row(DBPROCESS * dbproc, char *buffer, DBINT buf_len)
    3477             : {
    3478             :         TDSSOCKET *tds;
    3479             :         TDSDATEREC when;
    3480             :         int i, c, col;
    3481             :         DBINT len;
    3482             : 
    3483           0 :         tdsdump_log(TDS_DBG_FUNC, "dbspr1row(%p, %s, %d)\n", dbproc, buffer, buf_len);
    3484           0 :         CHECK_CONN(FAIL);
    3485           0 :         CHECK_NULP(buffer, "dbspr1row", 2, FAIL);
    3486             : 
    3487             :         if (!dbproc->tds_socket)
    3488             :                 return FAIL;
    3489             : 
    3490             :         tds = dbproc->tds_socket;
    3491             : 
    3492           0 :         for (col = 0; col < tds->res_info->num_cols; col++) {
    3493             :                 int padlen, collen, namlen;
    3494           0 :                 TDSCOLUMN *colinfo = tds->res_info->columns[col];
    3495           0 :                 if (colinfo->column_cur_size < 0) {
    3496           0 :                         len = 4;
    3497           0 :                         if (buf_len <= len) {
    3498             :                                 return FAIL;
    3499             :                         }
    3500           0 :                         strcpy(buffer, "NULL");
    3501             :                 } else {
    3502             :                         TDS_SERVER_TYPE desttype, srctype;
    3503             : 
    3504           0 :                         desttype = dblib_bound_type(STRINGBIND);
    3505           0 :                         srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
    3506           0 :                         if (is_datetime_type(srctype)) {
    3507           0 :                                 tds_datecrack(srctype, dbdata(dbproc, col + 1), &when);
    3508           0 :                                 len = (int)tds_strftime(buffer, buf_len, STD_DATETIME_FMT, &when, 3);
    3509             :                         } else {
    3510           0 :                                 len = dbconvert(dbproc, srctype, dbdata(dbproc, col + 1), dbdatlen(dbproc, col + 1), 
    3511             :                                                 desttype, (BYTE *) buffer, buf_len);
    3512             :                         }
    3513           0 :                         if (len == -1) {
    3514             :                                 return FAIL;
    3515             :                         }
    3516             :                 }
    3517           0 :                 buffer += len;
    3518           0 :                 buf_len -= len;
    3519           0 :                 collen = _get_printable_size(colinfo);
    3520           0 :                 namlen = tds_dstr_len(&colinfo->column_name);
    3521           0 :                 padlen = (collen > namlen ? collen : namlen) - len;
    3522           0 :                 if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) == -1) {
    3523           0 :                         c = ' ';
    3524             :                 }
    3525           0 :                 for (; padlen > 0; padlen--) {
    3526           0 :                         if (buf_len < 1) {
    3527             :                                 return FAIL;
    3528             :                         }
    3529           0 :                         *buffer++ = c;
    3530           0 :                         buf_len--;
    3531             :                 }
    3532           0 :                 if ((col + 1) < tds->res_info->num_cols) {
    3533             :                         i = 0;
    3534           0 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
    3535           0 :                                 if (buf_len < 1) {
    3536             :                                         return FAIL;
    3537             :                                 }
    3538           0 :                                 *buffer++ = c;
    3539           0 :                                 buf_len--;
    3540           0 :                                 i++;
    3541             :                         }
    3542             :                 }
    3543             :         }
    3544           0 :         if (buf_len < 1) {
    3545             :                 return FAIL;
    3546             :         }
    3547           0 :         *buffer = '\0';
    3548           0 :         return SUCCEED;
    3549             : }
    3550             : 
    3551             : /**
    3552             :  * \ingroup dblib_core
    3553             :  * \brief Print a result set to stdout. 
    3554             :  * 
    3555             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3556             :  * \sa dbbind(), dbnextrow(), dbprhead(), dbresults(), dbspr1row(), dbsprhead(), dbsprline(). 
    3557             :  */
    3558             : RETCODE
    3559          60 : dbprrow(DBPROCESS * dbproc)
    3560             : {
    3561             :         TDSCOLUMN *colinfo;
    3562             :         TDSRESULTINFO *resinfo;
    3563             :         TDSSOCKET *tds;
    3564             :         int i, col, collen, namlen, len;
    3565             :         char dest[8192];
    3566             :         int desttype, srctype;
    3567             :         TDSDATEREC when;
    3568             :         STATUS status;
    3569             :         int padlen;
    3570             :         int c;
    3571             :         int selcol;
    3572             :         int linechar;
    3573             :         int op;
    3574             :         const char *opname;
    3575             : 
    3576             :         /* these are for compute rows */
    3577             :         DBINT computeid, num_cols, colid;
    3578          60 :         TDS_SMALLINT *col_printlens = NULL;
    3579             : 
    3580          60 :         tdsdump_log(TDS_DBG_FUNC, "dbprrow(%p)\n", dbproc);
    3581          60 :         CHECK_CONN(FAIL);
    3582             : 
    3583             :         tds = dbproc->tds_socket;
    3584             : 
    3585         410 :         while ((status = dbnextrow(dbproc)) != NO_MORE_ROWS) {
    3586             : 
    3587         350 :                 if (status == FAIL) {
    3588           0 :                         free(col_printlens);
    3589           0 :                         return FAIL;
    3590             :                 }
    3591             : 
    3592         350 :                 if (status == REG_ROW) {
    3593             : 
    3594         350 :                         resinfo = tds->res_info;
    3595             : 
    3596         350 :                         if (col_printlens == NULL) {
    3597          20 :                                 if ((col_printlens = tds_new0(TDS_SMALLINT, resinfo->num_cols)) == NULL) {
    3598           0 :                                         dbperror(dbproc, SYBEMEM, errno);
    3599           0 :                                         return FAIL;
    3600             :                                 }
    3601             :                         }
    3602             : 
    3603        1750 :                         for (col = 0; col < resinfo->num_cols; col++) {
    3604        1400 :                                 colinfo = resinfo->columns[col];
    3605        1400 :                                 if (colinfo->column_cur_size < 0) {
    3606          60 :                                         len = 4;
    3607          60 :                                         strcpy(dest, "NULL");
    3608             :                                 } else {
    3609        1340 :                                         desttype = dblib_bound_type(STRINGBIND);
    3610        1340 :                                         srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
    3611        1340 :                                         if (is_datetime_type(srctype)) {
    3612           0 :                                                 tds_datecrack(srctype, dbdata(dbproc, col + 1), &when);
    3613           0 :                                                 len = (int)tds_strftime(dest, sizeof(dest), STD_DATETIME_FMT, &when, 3);
    3614             :                                         } else {
    3615        1340 :                                                 len = dbconvert(dbproc, srctype, dbdata(dbproc, col + 1), dbdatlen(dbproc, col + 1),
    3616             :                                                                 desttype, (BYTE *) dest, sizeof(dest));
    3617             :                                         }
    3618             :                                 }
    3619             : 
    3620        1400 :                                 printf("%.*s", len, dest);
    3621        1400 :                                 collen = _get_printable_size(colinfo);
    3622        2800 :                                 namlen = tds_dstr_len(&colinfo->column_name);
    3623        1400 :                                 padlen = (collen > namlen ? collen : namlen) - len;
    3624             : 
    3625        2800 :                                 c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0);
    3626       10618 :                                 for (; c > -1 && padlen > 0; padlen--) {
    3627        9218 :                                         putchar(c);
    3628             :                                 }
    3629             : 
    3630        1400 :                                 if ((col + 1) < resinfo->num_cols) {
    3631             :                                         i = 0;
    3632        4200 :                                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
    3633             :                                                 putchar(c);
    3634             :                                         }
    3635             :                                 }
    3636        1400 :                                 col_printlens[col] = collen;
    3637             :                         }
    3638             :                         i = 0;
    3639        1400 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
    3640             :                                 putchar(c);
    3641             :                         }
    3642             : 
    3643           0 :                 } else if (col_printlens == NULL) {
    3644             :                         return FAIL;
    3645             :                 } else {
    3646             : 
    3647             :                         computeid = status;
    3648             : 
    3649           0 :                         for (i = 0;; ++i) {
    3650           0 :                                 if (i >= tds->num_comp_info) {
    3651           0 :                                         free(col_printlens);
    3652           0 :                                         return FAIL;
    3653             :                                 }
    3654           0 :                                 resinfo = tds->comp_info[i];
    3655           0 :                                 if (resinfo->computeid == computeid)
    3656             :                                         break;
    3657             :                         }
    3658             : 
    3659           0 :                         num_cols = dbnumalts(dbproc, computeid);
    3660           0 :                         tdsdump_log(TDS_DBG_FUNC, "dbprrow num compute cols = %d\n", num_cols);
    3661             : 
    3662             :                         i = 0;
    3663           0 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
    3664             :                                 putchar(c);
    3665             :                         }
    3666           0 :                         for (selcol = col = 1; col <= num_cols; col++) {
    3667           0 :                                 tdsdump_log(TDS_DBG_FUNC, "dbprrow calling dbaltcolid(%d,%d)\n", computeid, col);
    3668           0 :                                 colid = dbaltcolid(dbproc, computeid, col);
    3669             :                                 /*
    3670             :                                  * The pad character is pointed to by dbopts[DBPRPAD].param.  If that pointer 
    3671             :                                  * is NULL -- meaning padding is turned off -- dbstring_getchar returns -1. 
    3672             :                                  */
    3673           0 :                                 while (selcol < colid) {
    3674           0 :                                         for (i = 0; i < col_printlens[selcol - 1]; i++) {
    3675           0 :                                                 if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) >= 0)
    3676             :                                                         putchar(c); 
    3677             :                                         }
    3678           0 :                                         selcol++;
    3679           0 :                                         i = 0;
    3680           0 :                                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
    3681             :                                                 putchar(c);
    3682             :                                         }
    3683             :                                 }
    3684           0 :                                 op = dbaltop(dbproc, computeid, col);
    3685           0 :                                 opname = dbprtype(op);
    3686           0 :                                 printf("%s", opname);
    3687           0 :                                 for (i = 0; i < ((long) col_printlens[selcol - 1] - (long) strlen(opname)); i++) {
    3688           0 :                                         if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) >= 0)
    3689             :                                                 putchar(c); 
    3690             :                                 }
    3691           0 :                                 selcol++;
    3692           0 :                                 if ((colid + 1) < num_cols) {
    3693             :                                         i = 0;
    3694           0 :                                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
    3695             :                                                 putchar(c);
    3696             :                                         }
    3697             :                                 }
    3698             :                         }
    3699             :                         i = 0;
    3700           0 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
    3701             :                                 putchar(c);
    3702             :                         }
    3703             : 
    3704           0 :                         for (selcol = col = 1; col <= num_cols; col++) {
    3705           0 :                                 tdsdump_log(TDS_DBG_FUNC, "dbprrow calling dbaltcolid(%d,%d)\n", computeid, col);
    3706           0 :                                 colid = dbaltcolid(dbproc, computeid, col);
    3707           0 :                                 while (selcol < colid) {
    3708           0 :                                         for (i = 0; i < col_printlens[selcol - 1]; i++) {
    3709           0 :                                                 putchar(' ');
    3710             :                                         }
    3711           0 :                                         selcol++;
    3712           0 :                                         i = 0;
    3713           0 :                                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
    3714             :                                                 putchar(c);
    3715             :                                         }
    3716             :                                 }
    3717           0 :                                 if (resinfo->by_cols > 0) {
    3718             :                                         linechar = '-';
    3719             :                                 } else {
    3720           0 :                                         linechar = '=';
    3721             :                                 }
    3722           0 :                                 for (i = 0; i < col_printlens[colid - 1]; i++)
    3723           0 :                                         putchar(linechar);
    3724           0 :                                 selcol++;
    3725           0 :                                 if ((colid + 1) < num_cols) {
    3726             :                                         i = 0;
    3727           0 :                                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
    3728             :                                                 putchar(c);
    3729             :                                         }
    3730             :                                 }
    3731             :                         }
    3732             :                         i = 0;
    3733           0 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
    3734             :                                 putchar(c);
    3735             :                         }
    3736             : 
    3737           0 :                         for (selcol = col = 1; col <= num_cols; col++) {
    3738           0 :                                 colinfo = resinfo->columns[col - 1];
    3739             : 
    3740           0 :                                 desttype = dblib_bound_type(STRINGBIND);
    3741           0 :                                 srctype = dbalttype(dbproc, computeid, col);
    3742             : 
    3743           0 :                                 if (is_datetime_type(srctype)) {
    3744           0 :                                         tds_datecrack(srctype, dbadata(dbproc, computeid, col), &when);
    3745           0 :                                         len = (int)tds_strftime(dest, sizeof(dest), STD_DATETIME_FMT, &when, 3);
    3746             :                                 } else {
    3747           0 :                                         len = dbconvert(dbproc, srctype, dbadata(dbproc, computeid, col), -1, desttype,
    3748             :                                                         (BYTE *) dest, sizeof(dest));
    3749             :                                 }
    3750             : 
    3751           0 :                                 tdsdump_log(TDS_DBG_FUNC, "dbprrow calling dbaltcolid(%d,%d)\n", computeid, col);
    3752           0 :                                 colid = dbaltcolid(dbproc, computeid, col);
    3753           0 :                                 tdsdump_log(TDS_DBG_FUNC, "dbprrow select column = %d\n", colid);
    3754             : 
    3755           0 :                                 while (selcol < colid) {
    3756           0 :                                         for (i = 0; i < col_printlens[selcol - 1]; i++) {
    3757           0 :                                                 putchar(' ');
    3758             :                                         }
    3759           0 :                                         selcol++;
    3760           0 :                                         i = 0;
    3761           0 :                                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
    3762             :                                                 putchar(c);
    3763             :                                         }
    3764             :                                 }
    3765           0 :                                 printf("%.*s", len, dest);
    3766           0 :                                 collen = _get_printable_size(colinfo);
    3767           0 :                                 namlen = tds_dstr_len(&colinfo->column_name);
    3768           0 :                                 padlen = (collen > namlen ? collen : namlen) - len;
    3769           0 :                                 if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) == -1) {
    3770           0 :                                         c = ' ';
    3771             :                                 }
    3772           0 :                                 for (; padlen > 0; padlen--) {
    3773           0 :                                         putchar(c);
    3774             :                                 }
    3775           0 :                                 selcol++;
    3776           0 :                                 if ((colid + 1) < num_cols) {
    3777             :                                         i = 0;
    3778           0 :                                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i++)) != -1) {
    3779             :                                                 putchar(c);
    3780             :                                         }
    3781             :                                 }
    3782             :                         }
    3783             :                         i = 0;
    3784           0 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i++)) != -1) {
    3785             :                                 putchar(c);
    3786             :                         }
    3787             :                 }
    3788             :         }
    3789             : 
    3790          60 :         free(col_printlens);
    3791             : 
    3792          60 :         return SUCCEED;
    3793             : }
    3794             : 
    3795             : static int
    3796        1560 : _get_printable_size(TDSCOLUMN * colinfo)
    3797             : {
    3798        1560 :         switch (tds_get_conversion_type(colinfo->column_type, colinfo->column_size)) {
    3799             :         case SYBUINT1:
    3800             :         case SYBINT1:
    3801             :                 return 3;
    3802           0 :         case SYBSINT1:
    3803           0 :                 return 4;
    3804           0 :         case SYBUINT2:
    3805           0 :                 return 5;
    3806         522 :         case SYBINT2:
    3807         522 :                 return 6;
    3808           0 :         case SYBUINT4:
    3809           0 :                 return 10;
    3810         258 :         case SYBINT4:
    3811         258 :                 return 11;
    3812           0 :         case SYBUINT8:
    3813           0 :                 return 20;
    3814           0 :         case SYBINT8:
    3815           0 :                 return 21;
    3816         780 :         case SYBVARCHAR:
    3817             :         case SYBCHAR:
    3818             :         case SYBTEXT:
    3819             :         case SYBNTEXT:
    3820             :         case SYBUNITEXT:
    3821             :         case SYBNVARCHAR:
    3822             :         case SYBLONGCHAR:
    3823         780 :                 return colinfo->column_size;
    3824           0 :         case SYBBINARY:
    3825             :         case SYBIMAGE:
    3826             :         case SYBLONGBINARY:
    3827             :         case SYBVARBINARY:
    3828           0 :                 return colinfo->column_size * 2u;
    3829           0 :         case SYBFLT8:
    3830             :         case SYBREAL:
    3831           0 :                 return 11;      /* FIX ME -- we do not track precision */
    3832           0 :         case SYBMONEY4:
    3833           0 :                 return 12;
    3834           0 :         case SYBMONEY:
    3835           0 :                 return 22;
    3836           0 :         case SYB5BIGDATETIME:
    3837             :         case SYBDATETIME:
    3838             :         case SYBDATETIME4:
    3839           0 :                 return 26;
    3840           0 :         case SYBTIME:
    3841             :         case SYB5BIGTIME:
    3842           0 :                 return 15;
    3843           0 :         case SYBMSTIME:
    3844           0 :                 return 16;
    3845           0 :         case SYBDATE:
    3846             :         case SYBMSDATE:
    3847           0 :                 return 10;
    3848           0 :         case SYBMSDATETIME2:
    3849           0 :                 return 27;
    3850           0 :         case SYBMSDATETIMEOFFSET:
    3851           0 :                 return 33;
    3852           0 :         case SYBUNIQUE:
    3853           0 :                 return 36;
    3854           0 :         case SYBBIT:
    3855           0 :                 return 1;
    3856           0 :         case SYBNUMERIC:
    3857             :         case SYBDECIMAL:
    3858           0 :                 return colinfo->column_prec + 2;
    3859             :                 /* FIX ME -- not all types present */
    3860           0 :         case SYBVOID: /* or 4 bytes for "null" ? */
    3861             :         default:
    3862           0 :                 return 0;
    3863             :         }
    3864             : }
    3865             : 
    3866             : /**
    3867             :  * \ingroup dblib_core
    3868             :  * \brief Get formatted string for underlining dbsprhead() column names.  
    3869             :  * 
    3870             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3871             :  * \param buffer output buffer
    3872             :  * \param buf_len size of \a buffer
    3873             :  * \param line_char character to use to represent underlining.
    3874             :  * \retval SUCCEED \a buffer filled.
    3875             :  * \retval FAIL insufficient space in \a buffer, usually.
    3876             :  * \sa dbprhead(), dbprrow(), dbspr1row(), dbspr1rowlen(), dbsprhead(). 
    3877             :  */
    3878             : RETCODE
    3879           0 : dbsprline(DBPROCESS * dbproc, char *buffer, DBINT buf_len, DBCHAR line_char)
    3880             : {
    3881             :         TDSCOLUMN *colinfo;
    3882             :         TDSRESULTINFO *resinfo;
    3883             :         TDSSOCKET *tds;
    3884             :         int i, col, len, collen, namlen;
    3885             :         int c;
    3886             : 
    3887           0 :         tdsdump_log(TDS_DBG_FUNC, "dbsprline(%p, %s, %d, '%c')\n", dbproc, buffer, buf_len, line_char);
    3888           0 :         CHECK_CONN(FAIL);
    3889           0 :         CHECK_NULP(buffer, "dbsprline", 2, FAIL);
    3890             : 
    3891           0 :         tds = dbproc->tds_socket;
    3892           0 :         resinfo = tds->res_info;
    3893             : 
    3894           0 :         for (col = 0; col < resinfo->num_cols; col++) {
    3895           0 :                 colinfo = resinfo->columns[col];
    3896           0 :                 collen = _get_printable_size(colinfo);
    3897           0 :                 namlen = tds_dstr_len(&colinfo->column_name);
    3898           0 :                 len = collen > namlen ? collen : namlen;
    3899           0 :                 for (i = 0; i < len; i++) {
    3900           0 :                         if (buf_len < 1) {
    3901             :                                 return FAIL;
    3902             :                         }
    3903           0 :                         *buffer++ = line_char;
    3904           0 :                         buf_len--;
    3905             :                 }
    3906           0 :                 if ((col + 1) < resinfo->num_cols) {
    3907             :                         i = 0;
    3908           0 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
    3909           0 :                                 if (buf_len < 1) {
    3910             :                                         return FAIL;
    3911             :                                 }
    3912           0 :                                 *buffer++ = c;
    3913           0 :                                 buf_len--;
    3914           0 :                                 i++;
    3915             :                         }
    3916             :                 }
    3917             :         }
    3918           0 :         if (buf_len < 1) {
    3919             :                 return FAIL;
    3920             :         }
    3921           0 :         *buffer = '\0';
    3922           0 :         return SUCCEED;
    3923             : }
    3924             : 
    3925             : /**
    3926             :  * \ingroup dblib_core
    3927             :  * \brief Print result set headings to a buffer.
    3928             :  * 
    3929             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3930             :  * \param buffer output buffer
    3931             :  * \param buf_len size of \a buffer
    3932             :  * \retval SUCCEED \a buffer filled.
    3933             :  * \retval FAIL insufficient space in \a buffer, usually.
    3934             :  * \sa dbprhead(), dbprrow(), dbsetopt(), dbspr1row(), dbspr1rowlen(), dbsprline().
    3935             :  */
    3936             : RETCODE
    3937           0 : dbsprhead(DBPROCESS * dbproc, char *buffer, DBINT buf_len)
    3938             : {
    3939             :         TDSCOLUMN *colinfo;
    3940             :         TDSRESULTINFO *resinfo;
    3941             :         TDSSOCKET *tds;
    3942             :         int i, col, collen, namlen;
    3943             :         int padlen;
    3944             :         int c;
    3945             : 
    3946           0 :         tdsdump_log(TDS_DBG_FUNC, "dbsprhead(%p, %p, %d)\n", dbproc, buffer, buf_len);
    3947           0 :         CHECK_CONN(FAIL);
    3948           0 :         CHECK_NULP(buffer, "dbsprhead", 2, FAIL);
    3949             : 
    3950           0 :         tds = dbproc->tds_socket;
    3951           0 :         resinfo = tds->res_info;
    3952             : 
    3953           0 :         for (col = 0; col < resinfo->num_cols; col++) {
    3954           0 :                 colinfo = resinfo->columns[col];
    3955           0 :                 collen = _get_printable_size(colinfo);
    3956           0 :                 namlen = tds_dstr_len(&colinfo->column_name);
    3957           0 :                 padlen = (collen > namlen ? collen : namlen) - namlen;
    3958           0 :                 if (buf_len < namlen) {
    3959             :                         return FAIL;
    3960             :                 }
    3961           0 :                 memcpy(buffer, tds_dstr_cstr(&colinfo->column_name), namlen);
    3962           0 :                 buffer += namlen;
    3963           0 :                 buf_len -= namlen;
    3964           0 :                 if ((c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0)) == -1) {
    3965           0 :                         c = ' ';
    3966             :                 }
    3967           0 :                 for (; padlen > 0; padlen--) {
    3968           0 :                         if (buf_len < 1) {
    3969             :                                 return FAIL;
    3970             :                         }
    3971           0 :                         *buffer++ = c;
    3972           0 :                         buf_len--;
    3973             :                 }
    3974           0 :                 if ((col + 1) < resinfo->num_cols) {
    3975             :                         i = 0;
    3976           0 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
    3977           0 :                                 if (buf_len < 1) {
    3978             :                                         return FAIL;
    3979             :                                 }
    3980           0 :                                 *buffer++ = c;
    3981           0 :                                 buf_len--;
    3982           0 :                                 i++;
    3983             :                         }
    3984             :                 }
    3985             :         }
    3986           0 :         if (buf_len < 1) {
    3987             :                 return FAIL;
    3988             :         }
    3989           0 :         *buffer = '\0';
    3990           0 :         return SUCCEED;
    3991             : }
    3992             : 
    3993             : /**
    3994             :  * \ingroup dblib_core
    3995             :  * \brief Print result set headings to stdout.
    3996             :  * 
    3997             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    3998             :  * \sa 
    3999             :  */
    4000             : void
    4001          60 : dbprhead(DBPROCESS * dbproc)
    4002             : {
    4003             :         TDSCOLUMN *colinfo;
    4004             :         TDSRESULTINFO *resinfo;
    4005             :         TDSSOCKET *tds;
    4006             :         int i, col, len, collen, namlen;
    4007             :         int padlen;
    4008             :         int c;
    4009             : 
    4010          60 :         tdsdump_log(TDS_DBG_FUNC, "dbprhead(%p)\n", dbproc);
    4011          60 :         CHECK_PARAMETER(dbproc, SYBENULL, );
    4012             : 
    4013          60 :         tds = dbproc->tds_socket;
    4014          60 :         resinfo = tds->res_info;
    4015          60 :         if (resinfo == NULL) {
    4016             :                 return;
    4017             :         }
    4018         100 :         for (col = 0; col < resinfo->num_cols; col++) {
    4019          80 :                 colinfo = resinfo->columns[col];
    4020          80 :                 collen = _get_printable_size(colinfo);
    4021         160 :                 namlen = tds_dstr_len(&colinfo->column_name);
    4022          80 :                 padlen = (collen > namlen ? collen : namlen) - namlen;
    4023         160 :                 printf("%s", tds_dstr_cstr(&colinfo->column_name));
    4024             : 
    4025         160 :                 c = dbstring_getchar(dbproc->dbopts[DBPRPAD].param, 0);
    4026          80 :                 if (c == -1) {
    4027           0 :                         c = ' ';
    4028             :                 }
    4029         600 :                 for (; padlen > 0; padlen--) {
    4030         600 :                         putchar(c);
    4031             :                 }
    4032             : 
    4033          80 :                 if ((col + 1) < resinfo->num_cols) {
    4034             :                         i = 0;
    4035         240 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
    4036          60 :                                 putchar(c);
    4037          60 :                                 i++;
    4038             :                         }
    4039             :                 }
    4040             :         }
    4041             :         i = 0;
    4042          80 :         while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i)) != -1) {
    4043          20 :                 putchar(c);
    4044          20 :                 i++;
    4045             :         }
    4046         100 :         for (col = 0; col < resinfo->num_cols; col++) {
    4047          80 :                 colinfo = resinfo->columns[col];
    4048          80 :                 collen = _get_printable_size(colinfo);
    4049         160 :                 namlen = tds_dstr_len(&colinfo->column_name);
    4050          80 :                 len = collen > namlen ? collen : namlen;
    4051        1040 :                 for (i = 0; i < len; i++)
    4052         960 :                         putchar('-');
    4053          80 :                 if ((col + 1) < resinfo->num_cols) {
    4054             :                         i = 0;
    4055         240 :                         while ((c = dbstring_getchar(dbproc->dbopts[DBPRCOLSEP].param, i)) != -1) {
    4056          60 :                                 putchar(c);
    4057          60 :                                 i++;
    4058             :                         }
    4059             :                 }
    4060             :         }
    4061             :         i = 0;
    4062          80 :         while ((c = dbstring_getchar(dbproc->dbopts[DBPRLINESEP].param, i)) != -1) {
    4063          20 :                 putchar(c);
    4064          20 :                 i++;
    4065             :         }
    4066             : }
    4067             : 
    4068             : /** \internal
    4069             :  * \ingroup dblib_internal
    4070             :  * \brief Indicate whether a query returned rows.
    4071             :  * 
    4072             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4073             :  * \sa DBROWS(), DBCMDROW(), dbnextrow(), dbresults(), DBROWTYPE().
    4074             :  */
    4075             : RETCODE
    4076         522 : dbrows(DBPROCESS * dbproc)
    4077             : {
    4078             :         TDSSOCKET *tds;
    4079             : 
    4080         522 :         tdsdump_log(TDS_DBG_FUNC, "dbrows(%p)\n", dbproc);
    4081         522 :         CHECK_CONN(FAIL);
    4082             : 
    4083         522 :         if (!(tds=dbproc->tds_socket))
    4084             :                 return FAIL;
    4085             : 
    4086         522 :         return (tds->res_info && tds->res_info->rows_exist)? SUCCEED : FAIL;
    4087             : }
    4088             : 
    4089             : #if defined(DBLIB_UNIMPLEMENTED)
    4090             : /**
    4091             :  * \ingroup dblib_core
    4092             :  * \brief Set the default character set for an application.
    4093             :  * 
    4094             :  * \param language ASCII null-terminated string.  
    4095             :  * \sa dbsetdeflang(), dbsetdefcharset(), dblogin(), dbopen().
    4096             :  * \retval SUCCEED Always.  
    4097             :  * \todo Unimplemented.
    4098             :  */
    4099             : RETCODE
    4100             : dbsetdeflang(char *language)
    4101             : {
    4102             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetdeflang(%s)\n", language);
    4103             :         CHECK_PARAMETER_NOPROC(language, SYBENULP);
    4104             :         return SUCCEED;
    4105             : }
    4106             : #endif
    4107             : 
    4108             : /**
    4109             :  * \ingroup dblib_core
    4110             :  * \brief Get TDS packet size for the connection.
    4111             :  * 
    4112             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4113             :  * \return TDS packet size, in bytes.  
    4114             :  * \sa DBSETLPACKET()
    4115             :  */
    4116             : int
    4117           0 : dbgetpacket(DBPROCESS * dbproc)
    4118             : {
    4119             :         TDSSOCKET *tds;
    4120             : 
    4121           0 :         tdsdump_log(TDS_DBG_FUNC, "dbgetpacket(%p)\n", dbproc);
    4122           0 :         CHECK_PARAMETER(dbproc, SYBENULL, TDS_DEF_BLKSZ);
    4123             : 
    4124           0 :         tds = dbproc->tds_socket;
    4125           0 :         if (!tds) {
    4126             :                 return TDS_DEF_BLKSZ;
    4127             :         } else {
    4128           0 :                 return tds->conn->env.block_size;
    4129             :         }
    4130             : }
    4131             : 
    4132             : /**
    4133             :  * \ingroup dblib_core
    4134             :  * \brief Set maximum simultaneous connections db-lib will open to the server.
    4135             :  * 
    4136             :  * \param maxprocs Limit for process.
    4137             :  * \retval SUCCEED Always.  
    4138             :  * \sa dbgetmaxprocs(), dbopen()
    4139             :  */
    4140             : RETCODE
    4141         250 : dbsetmaxprocs(int maxprocs)
    4142             : {
    4143             :         int i, j;
    4144             :         TDSSOCKET **old_list;
    4145             : 
    4146         250 :         tdsdump_log(TDS_DBG_FUNC, "UNTESTED dbsetmaxprocs(%d)\n", maxprocs);
    4147             : 
    4148             :         /* not too few elements */
    4149         250 :         if (maxprocs <= 0)
    4150             :                 return FAIL;
    4151             : 
    4152         250 :         tds_mutex_lock(&dblib_mutex);
    4153             : 
    4154         250 :         old_list = g_dblib_ctx.connection_list;
    4155             : 
    4156             :         /* "compress" array */
    4157     1024250 :         for (i = 0, j = 0; i < g_dblib_ctx.connection_list_size; ++i) {
    4158     1024000 :                 if (!old_list[i])
    4159     1024000 :                         continue;
    4160           0 :                 if (i != j) {
    4161           0 :                         old_list[j] = old_list[i];
    4162           0 :                         old_list[i] = NULL;
    4163             :                 }
    4164           0 :                 ++j;
    4165             :         }
    4166             :         /* do not restrict too much, j here contains minimun size */
    4167         250 :         if (maxprocs < j)
    4168           0 :                 maxprocs = j;
    4169             : 
    4170             :         /*
    4171             :          * Don't reallocate less memory.  
    4172             :          * If maxprocs is less than was initially allocated, just reduce the represented list size.  
    4173             :          * If larger, reallocate and copy.
    4174             :          * We probably should check for valid connections beyond the new max.
    4175             :          */
    4176         250 :         if (maxprocs <= g_dblib_ctx.connection_list_size) {
    4177         250 :                 g_dblib_ctx.connection_list_size_represented = maxprocs;
    4178         250 :                 tds_mutex_unlock(&dblib_mutex);
    4179         250 :                 return SUCCEED;
    4180             :         }
    4181             : 
    4182           0 :         g_dblib_ctx.connection_list = tds_new0(TDSSOCKET *, maxprocs);
    4183             : 
    4184           0 :         if (g_dblib_ctx.connection_list == NULL) {
    4185           0 :                 g_dblib_ctx.connection_list = old_list;
    4186           0 :                 tds_mutex_unlock(&dblib_mutex);
    4187           0 :                 dbperror(NULL, SYBEMEM, errno);
    4188           0 :                 return FAIL;
    4189             :         }
    4190             : 
    4191           0 :         for (i = 0; i < g_dblib_ctx.connection_list_size; i++) {
    4192           0 :                 g_dblib_ctx.connection_list[i] = old_list[i];
    4193             :         }
    4194             : 
    4195           0 :         g_dblib_ctx.connection_list_size = maxprocs;
    4196           0 :         g_dblib_ctx.connection_list_size_represented = maxprocs;
    4197             : 
    4198           0 :         tds_mutex_unlock(&dblib_mutex);
    4199             : 
    4200           0 :         free(old_list);
    4201             : 
    4202           0 :         return SUCCEED;
    4203             : }
    4204             : 
    4205             : /**
    4206             :  * \ingroup dblib_core
    4207             :  * \brief get maximum simultaneous connections db-lib will open to the server.
    4208             :  * 
    4209             :  * \return Current maximum.  
    4210             :  * \sa dbsetmaxprocs(), dbopen()
    4211             :  */
    4212             : int
    4213           0 : dbgetmaxprocs(void)
    4214             : {
    4215             :         int r;
    4216             : 
    4217           0 :         tdsdump_log(TDS_DBG_FUNC, "dbgetmaxprocs(void)\n");
    4218             : 
    4219           0 :         tds_mutex_lock(&dblib_mutex);
    4220           0 :         r = g_dblib_ctx.connection_list_size_represented;
    4221           0 :         tds_mutex_unlock(&dblib_mutex);
    4222           0 :         return r;
    4223             : }
    4224             : 
    4225             : /**
    4226             :  * \ingroup dblib_core
    4227             :  * \brief Set maximum seconds db-lib waits for a server response to query.  
    4228             :  * 
    4229             :  * \param seconds New limit for application.  
    4230             :  * \retval SUCCEED Always.  
    4231             :  * \sa dberrhandle(), DBGETTIME(), dbsetlogintime(), dbsqlexec(), dbsqlok(), dbsqlsend().
    4232             :  */
    4233             : RETCODE
    4234          70 : dbsettime(int seconds)
    4235             : {
    4236             :         TDSSOCKET **tds;
    4237             :         int i;
    4238             :         DBPROCESS *dbproc;
    4239          70 :         tdsdump_log(TDS_DBG_FUNC, "dbsettime(%d)\n", seconds);
    4240             : 
    4241          70 :         tds_mutex_lock(&dblib_mutex);
    4242          70 :         g_dblib_ctx.query_timeout = seconds;
    4243             :         
    4244          70 :         tds = g_dblib_ctx.connection_list;
    4245      286790 :         for (i = 0; i <  TDS_MAX_CONN; i++) {
    4246      286720 :                 if (tds[i]) {
    4247         100 :                         dbproc = (DBPROCESS *) tds_get_parent(tds[i]);
    4248         100 :                         if (!dbisopt(dbproc, DBSETTIME, 0))
    4249          80 :                                 tds[i]->query_timeout = seconds;
    4250             :                 }
    4251             :         }
    4252             :         
    4253          70 :         tds_mutex_unlock(&dblib_mutex);
    4254          70 :         return SUCCEED;
    4255             : }
    4256             : 
    4257             : /**
    4258             :  * \ingroup dblib_core
    4259             :  * \brief Get maximum seconds db-lib waits for a server response to query.  
    4260             :  * 
    4261             :  * \retval query timeout limit, in seconds
    4262             :  * \sa dberrhandle(), DBSETTIME(), dbsetlogintime(), dbsqlexec(), dbsqlok(), dbsqlsend().
    4263             :  */
    4264             : int
    4265           0 : dbgettime(void)
    4266             : {
    4267           0 :         tdsdump_log(TDS_DBG_FUNC, "dbgettime()\n");
    4268             : 
    4269           0 :         return g_dblib_ctx.query_timeout;
    4270             : }
    4271             : 
    4272             : /**
    4273             :  * \ingroup dblib_core
    4274             :  * \brief Set maximum seconds db-lib waits for a server response to a login attempt.  
    4275             :  * 
    4276             :  * \param seconds New limit for application.  
    4277             :  * \retval SUCCEED Always.  
    4278             :  * \sa dberrhandle(), dbsettime()
    4279             :  */
    4280             : RETCODE
    4281          20 : dbsetlogintime(int seconds)
    4282             : {
    4283          20 :         tdsdump_log(TDS_DBG_FUNC, "dbsetlogintime(%d)\n", seconds);
    4284             : 
    4285          20 :         tds_mutex_lock(&dblib_mutex);
    4286          20 :         g_dblib_ctx.login_timeout = seconds;
    4287          20 :         tds_mutex_unlock(&dblib_mutex);
    4288          20 :         return SUCCEED;
    4289             : }
    4290             : 
    4291             : /** \internal
    4292             :  * \ingroup dblib_internal
    4293             :  * \brief See if the current command can return rows.
    4294             :  * 
    4295             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4296             :  * \retval SUCCEED Yes, it can.  
    4297             :  * \retval FAIL No, it can't.
    4298             :  * \remarks Use   DBCMDROW() macro instead.  
    4299             :  * \sa DBCMDROW(), dbnextrow(), dbresults(), DBROWS(), DBROWTYPE().
    4300             :  */
    4301             : RETCODE
    4302           0 : dbcmdrow(DBPROCESS * dbproc)
    4303             : {
    4304             :         TDSSOCKET *tds;
    4305             : 
    4306           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcmdrow(%p)\n", dbproc);
    4307           0 :         CHECK_CONN(FAIL);
    4308             : 
    4309           0 :         tds = dbproc->tds_socket;
    4310           0 :         if (tds->res_info)
    4311             :                 return SUCCEED;
    4312           0 :         return FAIL;
    4313             : }
    4314             : 
    4315             : /**
    4316             :  * \ingroup dblib_core
    4317             :  * \brief Get column ID of a compute column.
    4318             :  * 
    4319             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4320             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    4321             :  * \param column Nth column in \a computeid, starting from 1.
    4322             :  * \return Nth column in the base result set, on which \a column was computed.  
    4323             :  * \sa dbadata(), dbadlen(), dbaltlen(), dbgetrow(), dbnextrow(), dbnumalts(), dbprtype(). 
    4324             :  */
    4325             : int
    4326          16 : dbaltcolid(DBPROCESS * dbproc, int computeid, int column)
    4327             : {
    4328             :         TDSCOLUMN *curcol;
    4329             : 
    4330          16 :         tdsdump_log(TDS_DBG_FUNC, "dbaltcolid(%p, %d, %d)\n", dbproc, computeid, column);
    4331             : 
    4332          16 :         curcol = dbacolptr(dbproc, computeid, column, false);
    4333          16 :         if (!curcol)
    4334             :                 return -1;
    4335             : 
    4336          16 :         return curcol->column_operand;
    4337             : }
    4338             : 
    4339             : /**
    4340             :  * \ingroup dblib_core
    4341             :  * \brief Get size of data in a compute column.
    4342             :  * 
    4343             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4344             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    4345             :  * \param column Nth column in \a computeid, starting from 1.
    4346             :  * \return size of the data, in bytes.
    4347             :  * \retval -1 no such \a column or \a computeid. 
    4348             :  * \retval 0 data are NULL.
    4349             :  * \sa dbadata(), dbaltlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumalts().
    4350             :  */
    4351             : DBINT
    4352           0 : dbadlen(DBPROCESS * dbproc, int computeid, int column)
    4353             : {
    4354             :         TDSCOLUMN *colinfo;
    4355             :         DBINT len;
    4356             : 
    4357           0 :         tdsdump_log(TDS_DBG_FUNC, "dbadlen(%p, %d, %d)\n", dbproc, computeid, column);
    4358             : 
    4359           0 :         colinfo = dbacolptr(dbproc, computeid, column, false);
    4360           0 :         if (!colinfo)
    4361             :                 return -1;
    4362             : 
    4363           0 :         len = colinfo->column_cur_size < 0? 0 : colinfo->column_cur_size;
    4364             : 
    4365           0 :         tdsdump_log(TDS_DBG_FUNC, "leaving dbadlen() type = %d, returning %d\n", colinfo->column_type, len);
    4366             : 
    4367             :         return len;
    4368             : }
    4369             : 
    4370             : /**
    4371             :  * \ingroup dblib_core
    4372             :  * \brief Get datatype for a compute column.
    4373             :  * 
    4374             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4375             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    4376             :  * \param column Nth column in \a computeid, starting from 1.
    4377             :  * \return \c SYB* dataype token.
    4378             :  * \retval -1 no such \a column or \a computeid. 
    4379             :  * \sa dbadata(), dbadlen(), dbaltlen(), dbnextrow(), dbnumalts(), dbprtype().
    4380             :  */
    4381             : int
    4382          16 : dbalttype(DBPROCESS * dbproc, int computeid, int column)
    4383             : {
    4384             :         TDSCOLUMN *colinfo;
    4385             : 
    4386          16 :         tdsdump_log(TDS_DBG_FUNC, "dbalttype(%p, %d, %d)\n", dbproc, computeid, column);
    4387             : 
    4388          16 :         colinfo = dbacolptr(dbproc, computeid, column, false);
    4389          16 :         if (!colinfo)
    4390             :                 return -1;
    4391             : 
    4392          16 :         return dblib_coltype(colinfo);
    4393             : }
    4394             : 
    4395             : /**
    4396             :  * \ingroup dblib_core
    4397             :  * \brief Bind a compute column to a program variable.
    4398             :  * 
    4399             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4400             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    4401             :  * \param column Nth column in \a computeid, starting from 1.
    4402             :  * \param vartype datatype of the host variable that will receive the data
    4403             :  * \param varlen size of host variable pointed to \a varaddr
    4404             :  * \param varaddr address of host variable
    4405             :  * \retval SUCCEED everything worked.
    4406             :  * \retval FAIL no such \a computeid or \a column, or no such conversion possible, or target buffer too small.
    4407             :  * \sa dbadata(), dbaltbind_ps(), dbanullbind(), dbbind(), dbbind_ps(), dbconvert(),
    4408             :  *      dbconvert_ps(), dbnullbind(), dbsetnull(), dbsetversion(), dbwillconvert().
    4409             :  */
    4410             : RETCODE
    4411          16 : dbaltbind(DBPROCESS * dbproc, int computeid, int column, int vartype, DBINT varlen, BYTE * varaddr)
    4412             : {
    4413             :         TDS_SERVER_TYPE srctype, desttype;
    4414          16 :         TDSCOLUMN *colinfo = NULL;
    4415             : 
    4416          16 :         tdsdump_log(TDS_DBG_FUNC, "dbaltbind(%p, %d, %d, %d, %d, %p)\n", dbproc, computeid, column, vartype, varlen, varaddr);
    4417             : 
    4418          16 :         colinfo = dbacolptr(dbproc, computeid, column, true);
    4419          16 :         if (!colinfo)
    4420             :                 return FAIL;
    4421          16 :         CHECK_PARAMETER(varaddr, SYBEABNV, FAIL);
    4422             : 
    4423          16 :         dbproc->avail_flag = FALSE;
    4424             : 
    4425          16 :         srctype = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
    4426          16 :         desttype = dblib_bound_type(vartype);
    4427          16 :         if (desttype == TDS_INVALID_TYPE) {
    4428           0 :                 dbperror(dbproc, SYBEBTYP, 0);
    4429           0 :                 return FAIL;
    4430             :         }
    4431             : 
    4432          16 :         if (!dbwillconvert(srctype, desttype)) {
    4433           0 :                 dbperror(dbproc, SYBEAAMT, 0);
    4434           0 :                 return FAIL;
    4435             :         }
    4436             : 
    4437          16 :         colinfo->column_varaddr = (char *) varaddr;
    4438          16 :         colinfo->column_bindtype = vartype;
    4439          16 :         colinfo->column_bindlen = varlen;
    4440             : 
    4441          16 :         return SUCCEED;
    4442             : }
    4443             : 
    4444             : 
    4445             : /**
    4446             :  * \ingroup dblib_core
    4447             :  * \brief Get address of compute column data.  
    4448             :  * 
    4449             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4450             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    4451             :  * \param column Nth column in \a computeid, starting from 1.
    4452             :  * \return pointer to columns's data buffer.  
    4453             :  * \retval NULL no such \a computeid or \a column.  
    4454             :  * \sa dbadlen(), dbaltbind(), dbaltlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumalts().
    4455             :  */
    4456             : BYTE *
    4457           0 : dbadata(DBPROCESS * dbproc, int computeid, int column)
    4458             : {
    4459             :         TDSCOLUMN *colinfo;
    4460             : 
    4461           0 :         tdsdump_log(TDS_DBG_FUNC, "dbadata(%p, %d, %d)\n", dbproc, computeid, column);
    4462             : 
    4463           0 :         colinfo = dbacolptr(dbproc, computeid, column, false);
    4464           0 :         if (!colinfo)
    4465             :                 return NULL;
    4466             : 
    4467           0 :         if (is_blob_col(colinfo)) {
    4468           0 :                 return (BYTE *) ((TDSBLOB *) colinfo->column_data)->textvalue;
    4469             :         }
    4470             : 
    4471           0 :         return (BYTE *) colinfo->column_data;
    4472             : }
    4473             : 
    4474             : /**
    4475             :  * \ingroup dblib_core
    4476             :  * \brief Get aggregation operator for a compute column.
    4477             :  * 
    4478             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4479             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    4480             :  * \param column Nth column in \a computeid, starting from 1.
    4481             :  * \return token value for the type of the compute column's aggregation operator.
    4482             :  * \retval -1 no such \a computeid or \a column.  
    4483             :  * \sa dbadata(), dbadlen(), dbaltlen(), dbnextrow(), dbnumalts(), dbprtype().
    4484             :  */
    4485             : int
    4486          16 : dbaltop(DBPROCESS * dbproc, int computeid, int column)
    4487             : {
    4488             :         TDSCOLUMN *curcol;
    4489             : 
    4490          16 :         tdsdump_log(TDS_DBG_FUNC, "dbaltop(%p, %d, %d)\n", dbproc, computeid, column);
    4491             : 
    4492          16 :         if ((curcol=dbacolptr(dbproc, computeid, column, false)) == NULL)
    4493             :                 return -1;
    4494             : 
    4495          16 :         return curcol->column_operator;
    4496             : }
    4497             : 
    4498             : /**
    4499             :  * \ingroup dblib_core
    4500             :  * \brief Set db-lib or server option.
    4501             :  * 
    4502             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4503             :  * \param option option to set.  
    4504             :  * \param char_param value to set \a option to, if it wants a null-teminated ASCII string.  
    4505             :  * \param int_param  value to set \a option to, if it wants an integer value.  
    4506             :  * \retval SUCCEED everything worked.
    4507             :  * \retval FAIL no such \a option, or insufficient memory, or unimplemented.  
    4508             :  * \remarks Many are unimplemented.
    4509             :  * \sa dbclropt(), dbisopt().
    4510             :  * \todo Implement more options.  
    4511             :  */
    4512             : RETCODE
    4513          90 : dbsetopt(DBPROCESS * dbproc, int option, const char *char_param, int int_param)
    4514             : {
    4515             :         char *cmd;
    4516             :         RETCODE rc;
    4517             :         int i;
    4518             : 
    4519          90 :         tdsdump_log(TDS_DBG_FUNC, "dbsetopt(%p, %d, %s, %d)\n", dbproc, option, char_param, int_param);
    4520          90 :         CHECK_CONN(FAIL);
    4521          90 :         CHECK_NULP(char_param, "dbsetopt", 3, FAIL);
    4522             : 
    4523          90 :         if ((option < 0) || (option >= DBNUMOPTIONS)) {
    4524           0 :                 dbperror(dbproc, SYBEUNOP, 0);
    4525           0 :                 return FAIL;
    4526             :         }
    4527             : 
    4528          90 :         rc = FAIL;
    4529          90 :         switch (option) {
    4530           0 :         case DBARITHABORT:
    4531             :         case DBARITHIGNORE:
    4532             :         case DBCHAINXACTS:
    4533             :         case DBFIPSFLAG:
    4534             :         case DBISOLATION:
    4535             :         case DBNOCOUNT:
    4536             :         case DBNOEXEC:
    4537             :         case DBPARSEONLY:
    4538             :         case DBSHOWPLAN:
    4539             :         case DBSTORPROCID:
    4540             :         case DBQUOTEDIDENT:
    4541             :                 /* server options (on/off) */
    4542           0 :                 if (asprintf(&cmd, "set %s on\n", dbproc->dbopts[option].text) < 0) {
    4543             :                         return FAIL;
    4544             :                 }
    4545           0 :                 rc = dbstring_concat(&(dbproc->dboptcmd), cmd);
    4546           0 :                 free(cmd);
    4547           0 :                 break;
    4548           0 :         case DBNATLANG:
    4549             :         case DBDATEFIRST:
    4550             :         case DBDATEFORMAT:
    4551             :                 /* server options (char_param) */
    4552           0 :                 if (asprintf(&cmd, "set %s %s\n", dbproc->dbopts[option].text, char_param) < 0) {
    4553             :                         return FAIL;
    4554             :                 }
    4555           0 :                 rc = dbstring_concat(&(dbproc->dboptcmd), cmd);
    4556           0 :                 free(cmd);
    4557           0 :                 break;
    4558             :         case DBOFFSET:
    4559             :                 /* server option */
    4560             :                 /* requires param
    4561             :                  * "select", "from", "table", "order", "compute",
    4562             :                  * "statement", "procedure", "execute", or "param"
    4563             :                  */
    4564             :                 rc = SUCCEED;
    4565             :                 break;
    4566             :         case DBROWCOUNT:
    4567             :                 /* server option */
    4568             :                 /* requires param "0" to "2147483647" */
    4569             :                 rc = SUCCEED;
    4570             :                 break;
    4571             :         case DBSTAT:
    4572             :                 /* server option */
    4573             :                 /* requires param "io" or "time" */
    4574             :                 rc = SUCCEED;
    4575             :                 break;
    4576             :         case DBTEXTLIMIT:
    4577             :                 /* dblib option */
    4578             :                 /* requires param "0" to "2147483647" */
    4579             :                 /* dblib do not return more than this length from text/image */
    4580             :                 /* TODO required for PHP */
    4581             :                 rc = SUCCEED;
    4582             :                 break;
    4583             :         case DBTEXTSIZE:
    4584             :                 /* server option */
    4585             :                 /* requires param "0" to "2147483647" */
    4586             :                 /* limit text/image from network */
    4587             :                 if (!char_param)
    4588             :                         char_param = "0";
    4589           0 :                 i = atoi(char_param);
    4590           0 :                 if (i < 0 || i > 2147483647)
    4591             :                         return FAIL;
    4592           0 :                 if (asprintf(&cmd, "set textsize %d\n", i) < 0)
    4593             :                         return FAIL;
    4594           0 :                 rc = dbstring_concat(&(dbproc->dboptcmd), cmd);
    4595           0 :                 free(cmd);
    4596           0 :                 break;
    4597             :         case DBAUTH:
    4598             :                 /* ??? */
    4599             :                 rc = SUCCEED;
    4600             :                 break;
    4601             :         case DBNOAUTOFREE:
    4602             :                 /* dblib option */
    4603             :                 rc = SUCCEED;
    4604             :                 break;
    4605             :         case DBBUFFER:
    4606             :                 /* 
    4607             :                  * Requires param "2" to "2147483647" 
    4608             :                  * (0 or 1 is an error, < 0 yields the default 100) 
    4609             :                  */
    4610             :                 {
    4611             :                         int nrows; 
    4612             :                         
    4613             :                         /* 100 is the default, according to Microsoft */
    4614             :                         if( !char_param )
    4615             :                                 char_param = "100";
    4616             : 
    4617          60 :                         nrows = atoi(char_param);
    4618             : 
    4619          60 :                         nrows = (nrows < 0 )? 100 : nrows;
    4620             : 
    4621          60 :                         if( 1 < nrows && nrows <= 2147483647 ) {
    4622          60 :                                 buffer_set_capacity(dbproc, nrows);
    4623          60 :                                 rc = SUCCEED;
    4624             :                         }
    4625             :                 }
    4626             :                 break;
    4627           0 :         case DBPRCOLSEP:
    4628             :         case DBPRLINELEN:
    4629             :         case DBPRLINESEP:
    4630           0 :                 rc = dbstring_assign(&(dbproc->dbopts[option].param), char_param);
    4631           0 :                 break;
    4632           0 :         case DBPRPAD:
    4633             :                 /*
    4634             :                  * "If the character is not specified, the ASCII space character is used." 
    4635             :                  * A NULL pointer to the pad character signifies that padding is turned off. 
    4636             :                  */
    4637           0 :                 if (int_param) {
    4638           0 :                         rc = dbstring_assign(&(dbproc->dbopts[option].param), char_param? char_param : " ");
    4639             :                 } else {
    4640           0 :                         rc = dbstring_assign(&(dbproc->dbopts[option].param), NULL);
    4641             :                 }
    4642             :                 break;
    4643             :         case DBSETTIME:
    4644             :                 if (char_param) {
    4645          30 :                         i = atoi(char_param);
    4646          30 :                         if (0 < i) {
    4647          60 :                                 rc = dbstring_assign(&(dbproc->dbopts[option].param), char_param);
    4648          30 :                                 if (rc == SUCCEED) {
    4649          30 :                                         dbproc->tds_socket->query_timeout = i;
    4650             :                                 }
    4651             :                         }
    4652             :                 }
    4653             :                 break;
    4654           0 :         default:
    4655           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetopt(option = %d)\n", option);
    4656             :                 return FAIL;
    4657             :         }
    4658          30 :         if (rc == SUCCEED)
    4659          90 :                 dbproc->dbopts[option].factive = 1;
    4660             :         return rc;
    4661             : }
    4662             : 
    4663             : /**
    4664             :  * \ingroup dblib_core
    4665             :  * \brief Set interrupt handler for db-lib to use while blocked against a read from the server.
    4666             :  * 
    4667             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4668             :  * \param chkintr
    4669             :  * \param hndlintr
    4670             :  * \sa dbcancel(), dbgetuserdata(), dbsetuserdata(), dbsetbusy(), dbsetidle().
    4671             :  */
    4672             : void
    4673          20 : dbsetinterrupt(DBPROCESS * dbproc, DB_DBCHKINTR_FUNC chkintr, DB_DBHNDLINTR_FUNC hndlintr)
    4674             : {
    4675          20 :         tdsdump_log(TDS_DBG_FUNC, "dbsetinterrupt(%p, %p, %p)\n", dbproc, chkintr, hndlintr);
    4676          20 :         CHECK_PARAMETER(dbproc, SYBENULL, );
    4677             : 
    4678          20 :         dbproc->chkintr = chkintr;
    4679          20 :         dbproc->hndlintr = hndlintr;
    4680             : }
    4681             : 
    4682             : /**
    4683             :  * \ingroup dblib_rpc
    4684             :  * \brief Determine if query generated a return status number.
    4685             :  * 
    4686             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4687             :  * \retval TRUE fetch return status with dbretstatus().  
    4688             :  * \retval FALSE no return status.  
    4689             :  * \sa dbnextrow(), dbresults(), dbretdata(), dbretstatus(), dbrpcinit(), dbrpcparam(), dbrpcsend().
    4690             :  */
    4691             : DBBOOL
    4692          62 : dbhasretstat(DBPROCESS * dbproc)
    4693             : {
    4694             :         TDSSOCKET *tds;
    4695             : 
    4696          62 :         tdsdump_log(TDS_DBG_FUNC, "dbhasretstat(%p)\n", dbproc);
    4697          62 :         CHECK_PARAMETER(dbproc, SYBENULL, FALSE);
    4698             : 
    4699          62 :         tds = dbproc->tds_socket;
    4700          62 :         if (tds->has_status) {
    4701             :                 return TRUE;
    4702             :         } else {
    4703          30 :                 return FALSE;
    4704             :         }
    4705             : }
    4706             : 
    4707             : /**
    4708             :  * \ingroup dblib_rpc
    4709             :  * \brief Fetch status value returned by query or remote procedure call.
    4710             :  * 
    4711             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4712             :  * \return return value 
    4713             :  * \sa dbhasretstat(), dbnextrow(), dbresults(), dbretdata(), dbrpcinit(), dbrpcparam(), dbrpcsend().
    4714             :  */
    4715             : DBINT
    4716         518 : dbretstatus(DBPROCESS * dbproc)
    4717             : {
    4718         518 :         tdsdump_log(TDS_DBG_FUNC, "dbretstatus(%p)\n", dbproc);
    4719         518 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    4720             : 
    4721         518 :         return dbproc->tds_socket->ret_status;
    4722             : }
    4723             : 
    4724             : /**
    4725             :  * \ingroup dblib_rpc
    4726             :  * \brief Get count of output parameters filled by a stored procedure.
    4727             :  * 
    4728             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4729             :  * \return How many, possibly zero.  
    4730             :  * \remarks This name sounds funny.  
    4731             :  * \sa 
    4732             :  */
    4733             : int
    4734         806 : dbnumrets(DBPROCESS * dbproc)
    4735             : {
    4736             :         TDSSOCKET *tds;
    4737             :         TDS_INT result_type;
    4738             : 
    4739         806 :         tdsdump_log(TDS_DBG_FUNC, "dbnumrets(%p)\n", dbproc);
    4740         806 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    4741             : 
    4742         806 :         tds = dbproc->tds_socket;
    4743             : 
    4744         806 :         tdsdump_log(TDS_DBG_FUNC, "dbnumrets() finds %d columns\n", (tds->param_info? tds->param_info->num_cols : 0));
    4745             : 
    4746             :         /* try to fetch output parameters and return status, if we have not already done so */
    4747         806 :         if (!tds->param_info) 
    4748         508 :                 tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_TRAILING);
    4749             :                 
    4750         806 :         if (!tds->param_info)
    4751             :                 return 0;
    4752             : 
    4753         300 :         return tds->param_info->num_cols;
    4754             : }
    4755             : 
    4756             : /**
    4757             :  * \ingroup dblib_rpc
    4758             :  * \brief Get name of an output parameter filled by a stored procedure.
    4759             :  * 
    4760             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4761             :  * \param retnum Nth parameter between \c 1 and the return value from \c dbnumrets().
    4762             :  * \returns ASCII null-terminated string, \c NULL if no such \a retnum.  
    4763             :  * \sa dbnextrow(), dbnumrets(), dbresults(), dbretdata(), dbretlen(), dbrettype(), dbrpcinit(), dbrpcparam().
    4764             :  */
    4765             : char *
    4766          66 : dbretname(DBPROCESS * dbproc, int retnum)
    4767             : {
    4768             :         TDSPARAMINFO *param_info;
    4769             : 
    4770          66 :         tdsdump_log(TDS_DBG_FUNC, "dbretname(%p, %d)\n", dbproc, retnum);
    4771          66 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    4772             : 
    4773          66 :         if (!dbproc->tds_socket)
    4774             :                 return NULL;
    4775             : 
    4776          66 :         dbnumrets(dbproc);
    4777             : 
    4778          66 :         param_info = dbproc->tds_socket->param_info;
    4779          66 :         if (!param_info || !param_info->columns || retnum < 1 || retnum > param_info->num_cols)
    4780             :                 return NULL;
    4781         132 :         return tds_dstr_buf(&param_info->columns[retnum - 1]->column_name);
    4782             : }
    4783             : 
    4784             : /**
    4785             :  * \ingroup dblib_rpc
    4786             :  * \brief Get value of an output parameter filled by a stored procedure.
    4787             :  * 
    4788             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4789             :  * \param retnum Nth parameter between \c 1 and the return value from \c dbnumrets().
    4790             :  * \returns Address of a return parameter value, or \c NULL if no such \a retnum.  
    4791             :  * \sa dbnextrow(), dbnumrets(), dbresults(), dbretlen(), dbretname(), dbrettype(), dbrpcinit(), dbrpcparam().
    4792             :  * \todo Handle blobs.
    4793             :  */
    4794             : BYTE *
    4795          70 : dbretdata(DBPROCESS * dbproc, int retnum)
    4796             : {
    4797             :         TDSPARAMINFO *param_info;
    4798             : 
    4799          70 :         tdsdump_log(TDS_DBG_FUNC, "dbretdata(%p, %d)\n", dbproc, retnum);
    4800          70 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    4801             : 
    4802          70 :         dbnumrets(dbproc);
    4803             : 
    4804          70 :         param_info = dbproc->tds_socket->param_info;
    4805          70 :         if (!param_info || !param_info->columns || retnum < 1 || retnum > param_info->num_cols)
    4806             :                 return NULL;
    4807             : 
    4808          70 :         return _dbcoldata(param_info->columns[retnum - 1]);
    4809             : }
    4810             : 
    4811             : /**
    4812             :  * \ingroup dblib_rpc
    4813             :  * \brief Get size of an output parameter filled by a stored procedure.
    4814             :  * 
    4815             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4816             :  * \param retnum Nth parameter between \c 1 and the return value from \c dbnumrets().
    4817             :  * \returns Size of a return parameter value, or \c NULL if no such \a retnum.  
    4818             :  * \sa dbnextrow(), dbnumrets(), dbresults(), dbretdata(), dbretname(), dbrettype(), dbrpcinit(), dbrpcparam().
    4819             :  */
    4820             : int
    4821          66 : dbretlen(DBPROCESS * dbproc, int retnum)
    4822             : {
    4823             :         TDSCOLUMN *column;
    4824             :         TDSPARAMINFO *param_info;
    4825             :         TDSSOCKET *tds;
    4826             : 
    4827          66 :         tdsdump_log(TDS_DBG_FUNC, "dbretlen(%p, %d)\n", dbproc, retnum);
    4828          66 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    4829             : 
    4830          66 :         dbnumrets(dbproc);
    4831             : 
    4832          66 :         tds = dbproc->tds_socket;
    4833          66 :         param_info = tds->param_info;
    4834          66 :         if (!param_info || !param_info->columns || retnum < 1 || retnum > param_info->num_cols)
    4835             :                 return -1;
    4836             : 
    4837          66 :         column = param_info->columns[retnum - 1];
    4838          66 :         if (column->column_cur_size < 0)
    4839             :                 return 0;
    4840             : 
    4841          52 :         return column->column_cur_size;
    4842             : }
    4843             : 
    4844             : /**
    4845             :  * \ingroup dblib_core
    4846             :  * \brief Wait for results of a query from the server.  
    4847             :  * 
    4848             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4849             :  * \retval SUCCEED everything worked, fetch results with \c dbnextresults().
    4850             :  * \retval FAIL SQL syntax error, typically.  
    4851             :  * \sa dbcmd(), dbfcmd(), DBIORDESC(), DBIOWDESC(), dbmoretext(), dbnextrow(),
    4852             :         dbpoll(), DBRBUF(), dbresults(), dbretstatus(), dbrpcsend(), dbsettime(), dbsqlexec(),
    4853             :         dbsqlsend(), dbwritetext().
    4854             :  */
    4855             : RETCODE
    4856       20614 : dbsqlok(DBPROCESS * dbproc)
    4857             : {
    4858             :         TDSSOCKET *tds;
    4859             :         TDS_INT result_type;
    4860       20614 :         RETCODE return_code = SUCCEED;
    4861             : 
    4862       20614 :         tdsdump_log(TDS_DBG_FUNC, "dbsqlok(%p)\n", dbproc);
    4863       20614 :         CHECK_CONN(FAIL);
    4864             : 
    4865       20614 :         tds = dbproc->tds_socket;
    4866             : 
    4867             :         /*
    4868             :          * dbsqlok has been called after dbmoretext()
    4869             :          * This is the trigger to send the text data.
    4870             :          */
    4871             : 
    4872       20614 :         if (dbproc->text_sent) {
    4873           0 :                 tds_flush_packet(tds);
    4874           0 :                 dbproc->text_sent = 0;
    4875             :         }
    4876             : 
    4877             :         /* 
    4878             :          * See what the next packet from the server is.
    4879             :          * We want to skip any messages which are not processable. 
    4880             :          * We're looking for a result token or a done token.
    4881             :          */
    4882         296 :         for (;;) {
    4883             :                 TDSRET tds_code;
    4884       20910 :                 int done_flags = 0;
    4885             : 
    4886             :                 /* 
    4887             :                  * If we hit an end token -- e.g. if the command
    4888             :                  * submitted returned no data (like an insert) -- then
    4889             :                  * we process the end token to extract the status code. 
    4890             :                  */
    4891       20910 :                 tdsdump_log(TDS_DBG_FUNC, "dbsqlok() not done, calling tds_process_tokens()\n");
    4892             : 
    4893       20910 :                 tds_code = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS);
    4894             : 
    4895             :                 /* 
    4896             :                  * The error flag may be set for any intervening DONEINPROC packet, in particular
    4897             :                  * by a RAISERROR statement.  Microsoft db-lib returns FAIL in that case. 
    4898             :                  */
    4899       20910 :                 if (done_flags & TDS_DONE_ERROR) {
    4900          70 :                         return_code = FAIL;
    4901             :                 }
    4902             :                 
    4903       20910 :                 switch (tds_code) {
    4904             :                 case TDS_NO_MORE_RESULTS:
    4905       20614 :                         return SUCCEED;
    4906             :                         break;
    4907             : 
    4908       20846 :                 case TDS_SUCCESS:
    4909       20846 :                         switch (result_type) {
    4910       12142 :                         case TDS_ROWFMT_RESULT:
    4911       12142 :                                 buffer_free(&dbproc->row_buf);
    4912       12142 :                                 buffer_alloc(dbproc);
    4913       12142 :                         case TDS_COMPUTEFMT_RESULT:
    4914       12142 :                                 dbproc->dbresults_state = _DB_RES_RESULTSET_EMPTY;
    4915       12142 :                         case TDS_COMPUTE_RESULT:
    4916             :                         case TDS_ROW_RESULT:
    4917       12142 :                                 tdsdump_log(TDS_DBG_FUNC, "dbsqlok() found result token\n");
    4918             :                                 return SUCCEED;
    4919             :                                 break;
    4920             :                         case TDS_DONEINPROC_RESULT:
    4921             :                                 break;
    4922        8408 :                         case TDS_DONE_RESULT:
    4923             :                         case TDS_DONEPROC_RESULT:
    4924        8408 :                                 tdsdump_log(TDS_DBG_FUNC, "dbsqlok() end status is %d (%s)\n", return_code, prdbretcode(return_code));
    4925             : #if 1
    4926        8408 :                                 if (done_flags & TDS_DONE_ERROR) {
    4927             : 
    4928          70 :                                         if (done_flags & TDS_DONE_MORE_RESULTS) {
    4929           0 :                                                 dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    4930             :                                         } else {
    4931          70 :                                                 dbproc->dbresults_state = _DB_RES_NO_MORE_RESULTS;
    4932             :                                         }
    4933             : 
    4934             :                                 } else {
    4935        8338 :                                         tdsdump_log(TDS_DBG_FUNC, "dbsqlok() end status was success\n");
    4936             : 
    4937        8338 :                                         dbproc->dbresults_state = _DB_RES_SUCCEED;
    4938             :                                 }
    4939             : 
    4940             :                                 return return_code;
    4941             :                                 break;
    4942             : #else
    4943             :                                 int retcode = (done_flags & TDS_DONE_ERROR)? FAIL : SUCCEED;
    4944             :                                 dbproc->dbresults_state = (done_flags & TDS_DONE_MORE_RESULTS)?
    4945             :                                         _DB_RES_NEXT_RESULT : _DB_RES_NO_MORE_RESULTS;
    4946             : 
    4947             :                                 tdsdump_log(TDS_DBG_FUNC, "dbsqlok: returning %s with %s (%#x)\n", 
    4948             :                                                 prdbretcode(retcode), prdbresults_state(dbproc->dbresults_state), done_flags);
    4949             :                                                 
    4950             :                                 if (retcode == SUCCEED && (done_flags & TDS_DONE_MORE_RESULTS))
    4951             :                                         continue;
    4952             :                                          
    4953             :                                 return retcode;
    4954             : #endif
    4955          46 :                         default:
    4956          46 :                                 tdsdump_log(TDS_DBG_FUNC, "%s %d: logic error: tds_process_tokens result_type %d\n", 
    4957             :                                                 __FILE__, __LINE__, result_type);
    4958             :                                 break;
    4959             :                         }
    4960             :                         break;
    4961             : 
    4962          40 :                 default:
    4963          40 :                         assert(TDS_FAILED(tds_code));
    4964             :                         return FAIL;
    4965             :                         break;
    4966             :                 }
    4967             :         }
    4968             : 
    4969             :         return SUCCEED;
    4970             : }
    4971             : 
    4972             : /**
    4973             :  * \ingroup dblib_core
    4974             :  * \brief Get count of columns in a compute row.
    4975             :  * 
    4976             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    4977             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    4978             :  * \return number of columns, else -1 if no such \a computeid.  
    4979             :  * \sa dbadata(), dbadlen(), dbaltlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumcols(). 
    4980             :  */
    4981             : int
    4982          16 : dbnumalts(DBPROCESS * dbproc, int computeid)
    4983             : {
    4984             :         TDSSOCKET *tds;
    4985             :         TDSCOMPUTEINFO *info;
    4986             :         TDS_SMALLINT compute_id;
    4987             :         int i;
    4988             : 
    4989          16 :         tdsdump_log(TDS_DBG_FUNC, "dbnumalts(%p, %d)\n", dbproc, computeid);
    4990          16 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    4991             : 
    4992          16 :         tds = dbproc->tds_socket;
    4993          16 :         compute_id = computeid;
    4994             : 
    4995          24 :         for (i = 0;; ++i) {
    4996          32 :                 if (i >= tds->num_comp_info)
    4997             :                         return -1;
    4998          24 :                 info = tds->comp_info[i];
    4999          24 :                 if (info->computeid == compute_id)
    5000             :                         break;
    5001             :         }
    5002             : 
    5003          16 :         return info->num_cols;
    5004             : }
    5005             : 
    5006             : /**
    5007             :  * \ingroup dblib_core
    5008             :  * \brief Get count of \c COMPUTE clauses for a result set.
    5009             :  * 
    5010             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5011             :  * \return number of compute clauses for the current query, possibly zero.  
    5012             :  * \sa dbnumalts(), dbresults().
    5013             :  */
    5014             : int
    5015           0 : dbnumcompute(DBPROCESS * dbproc)
    5016             : {
    5017             :         TDSSOCKET *tds;
    5018             : 
    5019           0 :         tdsdump_log(TDS_DBG_FUNC, "dbnumcompute(%p)\n", dbproc);
    5020           0 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    5021             : 
    5022           0 :         tds = dbproc->tds_socket;
    5023             : 
    5024           0 :         return tds->num_comp_info;
    5025             : }
    5026             : 
    5027             : 
    5028             : /**
    5029             :  * \ingroup dblib_core
    5030             :  * \brief Get \c bylist for a compute row.
    5031             :  * 
    5032             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5033             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    5034             :  * \param size \em output: size of \c bylist buffer whose address is returned, possibly zero.  
    5035             :  * \return address of \c bylist for \a computeid.  
    5036             :  * \retval NULL no such \a computeid.
    5037             :  * \remarks Do not free returned pointer.  
    5038             :  * \sa dbadata(), dbadlen(), dbaltlen(), dbalttype(), dbcolname(), dbgetrow(), dbnextrow().
    5039             :  */
    5040             : BYTE *
    5041           0 : dbbylist(DBPROCESS * dbproc, int computeid, int *size)
    5042             : {
    5043             :         TDSSOCKET *tds;
    5044             :         TDSCOMPUTEINFO *info;
    5045             :         int i;
    5046           0 :         const TDS_SMALLINT byte_flag = -0x8000;
    5047             : 
    5048           0 :         tdsdump_log(TDS_DBG_FUNC, "dbbylist(%p, %d, %p)\n", dbproc, computeid, size);
    5049           0 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    5050             : 
    5051           0 :         tds = dbproc->tds_socket;
    5052             : 
    5053           0 :         for (i = 0;; ++i) {
    5054           0 :                 if (i >= tds->num_comp_info) {
    5055           0 :                         if (size)
    5056           0 :                                 *size = 0;
    5057             :                         return NULL;
    5058             :                 }
    5059           0 :                 info = tds->comp_info[i];
    5060           0 :                 if (info->computeid == computeid)
    5061             :                         break;
    5062             :         }
    5063             : 
    5064           0 :         if (size)
    5065           0 :                 *size = info->by_cols;
    5066             : 
    5067             :         /*
    5068             :          * libtds stores this information using TDS_SMALLINT so we 
    5069             :          * have to convert it. We can do this because libtds just
    5070             :          * stores these data.
    5071             :          */
    5072           0 :         if (info->by_cols > 0 && info->bycolumns[0] != byte_flag) {
    5073             :                 int n;
    5074           0 :                 TDS_TINYINT *p = (TDS_TINYINT*) malloc(sizeof(info->bycolumns[0]) + info->by_cols);
    5075           0 :                 if (!p) {
    5076           0 :                         dbperror(dbproc, SYBEMEM, errno);
    5077           0 :                         return NULL;
    5078             :                 }
    5079           0 :                 for (n = 0; n < info->by_cols; ++n)
    5080           0 :                         p[sizeof(info->bycolumns[0]) + n] = info->bycolumns[n] > 255 ? 255 : info->bycolumns[n];
    5081           0 :                 *((TDS_SMALLINT *)p) = byte_flag;
    5082           0 :                 free(info->bycolumns);
    5083           0 :                 info->bycolumns = (TDS_SMALLINT *) p;
    5084             :         }
    5085           0 :         return (BYTE *) (&info->bycolumns[1]);
    5086             : }
    5087             : 
    5088             : /** \internal
    5089             :  * \ingroup dblib_internal
    5090             :  * \brief Check if \a dbproc is an ex-parrot.  
    5091             :  * 
    5092             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5093             :  * \retval TRUE process has been marked \em dead.
    5094             :  * \retval FALSE process is OK.  
    5095             :  * \remarks dbdead() does not communicate with the server.  
    5096             :  *      Unless a previously db-lib marked \a dbproc \em dead, dbdead() returns \c FALSE.  
    5097             :  * \sa dberrhandle().
    5098             :  */
    5099             : DBBOOL
    5100          10 : dbdead(DBPROCESS * dbproc)
    5101             : {
    5102          10 :         tdsdump_log(TDS_DBG_FUNC, "dbdead(%p) [%s]\n", dbproc, dbproc? IS_TDSDEAD(dbproc->tds_socket)? "dead":"alive" : "quite dead");
    5103             : 
    5104          10 :         if( NULL == dbproc ) 
    5105             :                 return TRUE;
    5106             : 
    5107          10 :         if (IS_TDSDEAD(dbproc->tds_socket))
    5108             :                 return TRUE;
    5109             : 
    5110          10 :         return FALSE;
    5111             : }
    5112             : 
    5113             : /** \internal
    5114             :  * \ingroup dblib_internal
    5115             :  * \brief default error handler for db-lib (handles library-generated errors)
    5116             :  * 
    5117             :  * The default error handler doesn't print anything.  If you want to see your messages printed, 
    5118             :  * install an error handler.  If you think that should be an optional compile- or run-time default, 
    5119             :  * submit a patch.  It could be done.  
    5120             :  * 
    5121             :  * \sa DBDEAD(), dberrhandle().
    5122             :  */
    5123             : /* Thus saith Sybase:
    5124             :  *     "If the user does not supply an error handler (or passes a NULL pointer to 
    5125             :  *      dberrhandle), DB-Library will exhibit its default error-handling 
    5126             :  *      behavior: It will abort the program if the error has made the affected 
    5127             :  *      DBPROCESS unusable (the user can call DBDEAD to determine whether 
    5128             :  *      or not a DBPROCESS has become unusable). If the error has not made the 
    5129             :  *      DBPROCESS unusable, DB-Library will simply return an error code to its caller." 
    5130             :  *
    5131             :  * It is not the error handler, however, that aborts anything.  It is db-lib, cf. dbperror().  
    5132             :  */ 
    5133             : static int
    5134          10 : default_err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
    5135             : {
    5136          10 :         tdsdump_log(TDS_DBG_FUNC, "default_err_handler %p, %d, %d, %d, %p, %p", dbproc, severity, dberr, oserr, dberrstr, oserrstr);
    5137             : 
    5138          10 :         if (DBDEAD(dbproc) && (!dbproc || !dbproc->msdblib)) {
    5139             :                 return INT_EXIT;
    5140             :         }
    5141             :         
    5142          10 :         if (!dbproc || !dbproc->msdblib) {   /* i.e. Sybase behavior */
    5143          10 :                 switch(dberr) {
    5144             :                 case SYBETIME:
    5145             :                         return INT_EXIT;
    5146             :                 default: 
    5147             :                         break;
    5148             :                 }
    5149           0 :         }
    5150          10 :         return INT_CANCEL;
    5151             : }
    5152             : 
    5153             : /**
    5154             :  * \ingroup dblib_core
    5155             :  * \brief Set an error handler, for messages from db-lib.
    5156             :  * 
    5157             :  * \param handler pointer to callback function that will handle errors.
    5158             :  *        Pass NULL to restore the default handler.  
    5159             :  * \return address of prior handler, or NULL if none was previously installed. 
    5160             :  * \sa DBDEAD(), dbmsghandle().
    5161             :  */
    5162             : EHANDLEFUNC
    5163        1072 : dberrhandle(EHANDLEFUNC handler)
    5164             : {
    5165        1072 :         EHANDLEFUNC old_handler = _dblib_err_handler;
    5166             : 
    5167        1072 :         tdsdump_log(TDS_DBG_FUNC, "dberrhandle(%p)\n", handler);
    5168             : 
    5169        1072 :         _dblib_err_handler = handler? handler : default_err_handler;
    5170             :         
    5171        1072 :         return (old_handler == default_err_handler)? NULL : old_handler;
    5172             : }
    5173             : 
    5174             : /**
    5175             :  * \ingroup dblib_core
    5176             :  * \brief Set a message handler, for messages from the server.
    5177             :  * 
    5178             :  * \param handler address of the function that will process the messages.
    5179             :  * \sa DBDEAD(), dberrhandle().
    5180             :  */
    5181             : MHANDLEFUNC
    5182        1092 : dbmsghandle(MHANDLEFUNC handler)
    5183             : {
    5184        1092 :         MHANDLEFUNC retFun = _dblib_msg_handler;
    5185             : 
    5186        1092 :         tdsdump_log(TDS_DBG_FUNC, "dbmsghandle(%p)\n", handler);
    5187             : 
    5188        1092 :         _dblib_msg_handler = handler;
    5189        1092 :         return retFun;
    5190             : }
    5191             : 
    5192             : #if defined(DBLIB_UNIMPLEMENTED)
    5193             : /**
    5194             :  * \ingroup dblib_money
    5195             :  * \brief Add two DBMONEY values.
    5196             :  * 
    5197             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5198             :  * \param m1 first operand.
    5199             :  * \param m2 other operand. 
    5200             :  * \param sum \em output: result of computation.  
    5201             :  * \retval SUCCEED Always.  
    5202             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5203             :  * \todo Unimplemented.
    5204             :  */
    5205             : RETCODE
    5206             : dbmnyadd(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * sum)
    5207             : {
    5208             :         tdsdump_log(TDS_DBG_FUNC, "dbmnyadd(%p, %p, %p, %p)\n", dbproc, m1, m2, sum);
    5209             :         CHECK_CONN(FAIL);
    5210             :         CHECK_NULP(m1,  "dbmnyadd", 2, FAIL);
    5211             :         CHECK_NULP(m2,  "dbmnyadd", 3, FAIL);
    5212             :         CHECK_NULP(sum, "dbmnyadd", 4, FAIL);
    5213             : 
    5214             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyadd()\n");
    5215             :         return SUCCEED;
    5216             : }
    5217             : 
    5218             : 
    5219             : /**
    5220             :  * \ingroup dblib_money
    5221             :  * \brief Subtract two DBMONEY values.
    5222             :  * 
    5223             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5224             :  * \param m1 first operand.
    5225             :  * \param m2 other operand, subtracted from \a m1. 
    5226             :  * \param difference \em output: result of computation.  
    5227             :  * \retval SUCCEED Always.  
    5228             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5229             :  * \todo Unimplemented.
    5230             :  */
    5231             : RETCODE
    5232             : dbmnysub(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * difference)
    5233             : {
    5234             :         tdsdump_log(TDS_DBG_FUNC, "dbmnysub(%p, %p, %p, %p)\n", dbproc, m1, m2, difference);
    5235             :         CHECK_CONN(FAIL);
    5236             :         CHECK_NULP(m1, "dbmnysub", 2, FAIL);
    5237             :         CHECK_NULP(m2, "dbmnysub", 3, FAIL);
    5238             :         CHECK_NULP(difference, "dbmnysub", 4, FAIL);
    5239             : 
    5240             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnysyb()\n");
    5241             :         return SUCCEED;
    5242             : }
    5243             : 
    5244             : /**
    5245             :  * \ingroup dblib_money
    5246             :  * \brief Multiply two DBMONEY values.
    5247             :  * 
    5248             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5249             :  * \param m1 first operand.
    5250             :  * \param m2 other operand. 
    5251             :  * \param prod \em output: result of computation.  
    5252             :  * \retval SUCCEED Always.  
    5253             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5254             :  * \todo Unimplemented.
    5255             :  */
    5256             : RETCODE
    5257             : dbmnymul(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * prod)
    5258             : {
    5259             :         tdsdump_log(TDS_DBG_FUNC, "dbmnymul(%p, %p, %p, %p)\n", dbproc, m1, m2, prod);
    5260             :         CHECK_CONN(FAIL);
    5261             :         CHECK_NULP(m1, "dbmnymul", 2, FAIL);
    5262             :         CHECK_NULP(m2, "dbmnymul", 3, FAIL);
    5263             :         CHECK_NULP(prod, "dbmnymul", 4, FAIL);
    5264             : 
    5265             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnymul()\n");
    5266             :         return SUCCEED;
    5267             : }
    5268             : 
    5269             : /**
    5270             :  * \ingroup dblib_money
    5271             :  * \brief Divide two DBMONEY values.
    5272             :  * 
    5273             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5274             :  * \param m1 dividend.
    5275             :  * \param m2 divisor. 
    5276             :  * \param quotient \em output: result of computation.  
    5277             :  * \retval SUCCEED Always.  
    5278             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5279             :  * \todo Unimplemented.
    5280             :  */
    5281             : RETCODE
    5282             : dbmnydivide(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2, DBMONEY * quotient)
    5283             : {
    5284             :         tdsdump_log(TDS_DBG_FUNC, "dbmnydivide(%p, %p, %p, %p)\n", dbproc, m1, m2, quotient);
    5285             :         CHECK_CONN(FAIL);
    5286             :         CHECK_NULP(m1, "dbmnydivide", 2, FAIL);
    5287             :         CHECK_NULP(m2, "dbmnydivide", 3, FAIL);
    5288             :         CHECK_NULP(quotient, "dbmnydivide", 4, FAIL);
    5289             : 
    5290             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnydivide()\n");
    5291             :         return SUCCEED;
    5292             : }
    5293             : #endif
    5294             : 
    5295             : /**
    5296             :  * \ingroup dblib_money
    5297             :  * \brief Compare two DBMONEY values.
    5298             :  * 
    5299             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5300             :  * \param m1 some money.
    5301             :  * \param m2 some other money. 
    5302             :  * \retval 0 m1 == m2. 
    5303             :  * \retval -1 m1 < m2.
    5304             :  * \retval  1 m1 > m2.
    5305             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5306             :  */
    5307             : int
    5308           0 : dbmnycmp(DBPROCESS * dbproc, DBMONEY * m1, DBMONEY * m2)
    5309             : {
    5310           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnycmp(%p, %p, %p)\n", dbproc, m1, m2);
    5311           0 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    5312           0 :         CHECK_NULP(m1, "dbmnycmp", 2, 0);
    5313           0 :         CHECK_NULP(m2, "dbmnycmp", 3, 0);
    5314             : 
    5315           0 :         if (m1->mnyhigh < m2->mnyhigh) {
    5316             :                 return -1;
    5317             :         }
    5318           0 :         if (m1->mnyhigh > m2->mnyhigh) {
    5319             :                 return 1;
    5320             :         }
    5321           0 :         if (m1->mnylow < m2->mnylow) {
    5322             :                 return -1;
    5323             :         }
    5324           0 :         if (m1->mnylow > m2->mnylow) {
    5325             :                 return 1;
    5326             :         }
    5327           0 :         return 0;
    5328             : }
    5329             : 
    5330             : #if defined(DBLIB_UNIMPLEMENTED)
    5331             : /**
    5332             :  * \ingroup dblib_money
    5333             :  * \brief Multiply a DBMONEY value by a positive integer, and add an amount. 
    5334             :  * 
    5335             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5336             :  * \param amount starting amount of money, also holds output.
    5337             :  * \param multiplier amount to multiply \a amount by. 
    5338             :  * \param addend amount to add to \a amount, after multiplying by \a multiplier. 
    5339             :  * \retval SUCCEED Always.  
    5340             :  * \remarks This function is goofy.  
    5341             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5342             :  * \todo Unimplemented.
    5343             :  */
    5344             : RETCODE
    5345             : dbmnyscale(DBPROCESS * dbproc, DBMONEY * amount, int multiplier, int addend)
    5346             : {
    5347             :         tdsdump_log(TDS_DBG_FUNC, "dbmnyscale(%p, %p, %d, %d)\n", dbproc, amount, multiplier, addend);
    5348             :         CHECK_CONN(FAIL);
    5349             :         CHECK_NULP(amount, "dbmnyscale", 2, FAIL);
    5350             : 
    5351             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyscale()\n");
    5352             :         return SUCCEED;
    5353             : }
    5354             : #endif
    5355             : 
    5356             : /**
    5357             :  * \ingroup dblib_money
    5358             :  * \brief Set a DBMONEY value to zero.
    5359             :  * 
    5360             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5361             :  * \param dest address of a DBMONEY structure.  
    5362             :  * \retval SUCCEED unless \a amount is NULL.  
    5363             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5364             :  */
    5365             : RETCODE
    5366           0 : dbmnyzero(DBPROCESS * dbproc, DBMONEY * dest)
    5367             : {
    5368           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnyzero(%p, %p)\n", dbproc, dest);
    5369           0 :         CHECK_CONN(FAIL);
    5370           0 :         CHECK_NULP(dest, "dbmnyzero", 2, FAIL);
    5371             : 
    5372           0 :         dest->mnylow = 0;
    5373           0 :         dest->mnyhigh = 0;
    5374           0 :         return SUCCEED;
    5375             : }
    5376             : 
    5377             : /**
    5378             :  * \ingroup dblib_money
    5379             :  * \brief Get maximum positive DBMONEY value supported.
    5380             :  * 
    5381             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5382             :  * \param amount address of a DBMONEY structure.  
    5383             :  * \retval SUCCEED Always.  
    5384             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5385             :  */
    5386             : RETCODE
    5387           0 : dbmnymaxpos(DBPROCESS * dbproc, DBMONEY * amount)
    5388             : {
    5389           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnymaxpos(%p, %p)\n", dbproc, amount);
    5390           0 :         CHECK_CONN(FAIL);
    5391           0 :         CHECK_NULP(amount, "dbmnymaxpos", 2, FAIL);
    5392             : 
    5393           0 :         amount->mnylow = 0xFFFFFFFFlu;
    5394           0 :         amount->mnyhigh = 0x7FFFFFFFl;
    5395           0 :         return SUCCEED;
    5396             : }
    5397             : 
    5398             : /**
    5399             :  * \ingroup dblib_money
    5400             :  * \brief Get maximum negative DBMONEY value supported.
    5401             :  * 
    5402             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5403             :  * \param amount address of a DBMONEY structure.  
    5404             :  * \retval SUCCEED Always.  
    5405             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5406             :  */
    5407             : RETCODE
    5408           0 : dbmnymaxneg(DBPROCESS * dbproc, DBMONEY * amount)
    5409             : {
    5410           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnymaxneg(%p, %p)\n", dbproc, amount);
    5411           0 :         CHECK_CONN(FAIL);
    5412           0 :         CHECK_NULP(amount, "dbmnymaxneg", 2, FAIL);
    5413             : 
    5414           0 :         amount->mnylow = 0;
    5415           0 :         amount->mnyhigh = -0x7FFFFFFFL - 1;
    5416           0 :         return SUCCEED;
    5417             : }
    5418             : 
    5419             : #if defined(DBLIB_UNIMPLEMENTED)
    5420             : /**
    5421             :  * \ingroup dblib_money
    5422             :  * \brief Get the least significant digit of a DBMONEY value, represented as a character.
    5423             :  * 
    5424             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5425             :  * \param mnyptr \em input the money amount, \em and \em output: \a mnyptr divided by 10.  
    5426             :  * \param digit the character value (between '0' and '9') of the rightmost digit in \a mnyptr.  
    5427             :  * \param zero \em output: \c TRUE if \a mnyptr is zero on output, else \c FALSE.  
    5428             :  * \retval SUCCEED Always.  
    5429             :  * \sa dbconvert(), dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5430             :  * \remarks Unimplemented and likely to remain so.  We'd be amused to learn anyone wants this function.  
    5431             :  * \todo Unimplemented.
    5432             :  */
    5433             : RETCODE
    5434             : dbmnyndigit(DBPROCESS * dbproc, DBMONEY * mnyptr, DBCHAR * digit, DBBOOL * zero)
    5435             : {
    5436             :         tdsdump_log(TDS_DBG_FUNC, "dbmnyndigit(%p, %p, %s, %p)\n", dbproc, mnyptr, digit, zero);
    5437             :         CHECK_CONN(FAIL);
    5438             :         CHECK_NULP(mnyptr, "dbmnyndigit", 2, FAIL);
    5439             :         CHECK_NULP(digit, "dbmnyndigit", 3, FAIL);
    5440             :         CHECK_NULP(zero, "dbmnyndigit", 4, FAIL);
    5441             : 
    5442             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyndigit()\n");
    5443             :         return SUCCEED;
    5444             : }
    5445             : 
    5446             : /**
    5447             :  * \ingroup dblib_money
    5448             :  * \brief Prepare a DBMONEY value for use with dbmnyndigit().
    5449             :  * 
    5450             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5451             :  * \param amount address of a DBMONEY structure.  
    5452             :  * \param trim number of digits to trim from \a amount.
    5453             :  * \param negative \em output: \c TRUE if \a amount < 0.  
    5454             :  * \retval SUCCEED Always.  
    5455             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5456             :  * \todo Unimplemented.
    5457             :  */
    5458             : RETCODE
    5459             : dbmnyinit(DBPROCESS * dbproc, DBMONEY * amount, int trim, DBBOOL * negative)
    5460             : {
    5461             :         tdsdump_log(TDS_DBG_FUNC, "dbmnyinit(%p, %p, %d, %p)\n", dbproc, amount, trim, negative);
    5462             :         CHECK_CONN(FAIL);
    5463             :         CHECK_NULP(amount, "dbmnyinit", 2, FAIL);
    5464             :         CHECK_NULP(negative, "dbmnyinit", 4, FAIL);
    5465             : 
    5466             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnyinit()\n");
    5467             :         return SUCCEED;
    5468             : }
    5469             : 
    5470             : 
    5471             : /**
    5472             :  * \ingroup dblib_money
    5473             :  * \brief Divide a DBMONEY value by a positive integer.
    5474             :  * 
    5475             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5476             :  * \param amount address of a DBMONEY structure.  
    5477             :  * \param divisor of \a amount.
    5478             :  * \param remainder \em output: modulo of integer division.
    5479             :  * \retval SUCCEED Always.  
    5480             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5481             :  * \todo Unimplemented.
    5482             :  */
    5483             : RETCODE
    5484             : dbmnydown(DBPROCESS * dbproc, DBMONEY * amount, int divisor, int *remainder)
    5485             : {
    5486             :         tdsdump_log(TDS_DBG_FUNC, "dbmnydown(%p, %p, %d, %p)\n", dbproc, amount, divisor, remainder);
    5487             :         CHECK_CONN(FAIL);
    5488             :         CHECK_NULP(amount, "dbmnydown", 2, FAIL);
    5489             :         CHECK_NULP(remainder, "dbmnydown", 4, FAIL);
    5490             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmnydown()\n");
    5491             :         return SUCCEED;
    5492             : }
    5493             : #endif
    5494             : 
    5495             : /**
    5496             :  * \ingroup dblib_money
    5497             :  * \brief Add $0.0001 to a DBMONEY value.
    5498             :  * 
    5499             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5500             :  * \param amount address of a DBMONEY structure.  
    5501             :  * \retval SUCCEED or FAIL if overflow or amount NULL.  
    5502             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5503             :  */
    5504             : RETCODE
    5505           0 : dbmnyinc(DBPROCESS * dbproc, DBMONEY * amount)
    5506             : {
    5507           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnyinc(%p, %p)\n", dbproc, amount);
    5508           0 :         CHECK_CONN(FAIL);
    5509           0 :         CHECK_NULP(amount, "dbmnyinc", 2, FAIL);
    5510             : 
    5511           0 :         if (amount->mnylow != 0xFFFFFFFFlu) {
    5512           0 :                 ++amount->mnylow;
    5513           0 :                 return SUCCEED;
    5514             :         }
    5515           0 :         if (amount->mnyhigh == 0x7FFFFFFFl)
    5516             :                 return FAIL;
    5517           0 :         amount->mnylow = 0;
    5518           0 :         ++amount->mnyhigh;
    5519           0 :         return SUCCEED;
    5520             : }
    5521             : 
    5522             : 
    5523             : /**
    5524             :  * \ingroup dblib_money
    5525             :  * \brief Subtract $0.0001 from a DBMONEY value.
    5526             :  *
    5527             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5528             :  * \param amount address of a DBMONEY structure.  
    5529             :  * \retval SUCCEED or FAIL if overflow or amount NULL.  
    5530             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5531             :  */
    5532             : RETCODE
    5533           0 : dbmnydec(DBPROCESS * dbproc, DBMONEY * amount)
    5534             : {
    5535           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnydec(%p, %p)\n", dbproc, amount);
    5536           0 :         CHECK_CONN(FAIL);
    5537           0 :         CHECK_NULP(amount, "dbmnydec", 2, FAIL);
    5538             : 
    5539           0 :         if (amount->mnylow != 0) {
    5540           0 :                 --amount->mnylow;
    5541           0 :                 return SUCCEED;
    5542             :         }
    5543           0 :         if (amount->mnyhigh == -0x7FFFFFFFL - 1)
    5544             :                 return FAIL;
    5545           0 :         amount->mnylow = 0xFFFFFFFFlu;
    5546           0 :         --amount->mnyhigh;
    5547           0 :         return SUCCEED;
    5548             : }
    5549             : 
    5550             : /**
    5551             :  * \ingroup dblib_money
    5552             :  * \brief Negate a DBMONEY value.
    5553             :  * 
    5554             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5555             :  * \param src address of a DBMONEY structure.  
    5556             :  * \param dest \em output: result of negation. 
    5557             :  * \retval SUCCEED or FAIL if overflow or src/dest NULL.
    5558             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5559             :  */
    5560             : RETCODE
    5561           0 : dbmnyminus(DBPROCESS * dbproc, DBMONEY * src, DBMONEY * dest)
    5562             : {
    5563           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnyminus(%p, %p, %p)\n", dbproc, src, dest);
    5564           0 :         CHECK_CONN(FAIL);
    5565           0 :         CHECK_NULP(src, "dbmnyminus", 2, FAIL);
    5566           0 :         CHECK_NULP(dest, "dbmnyminus", 3, FAIL);
    5567             : 
    5568           0 :         if (src->mnyhigh == -0x7FFFFFFFL - 1  &&  src->mnylow == 0)
    5569             :                 return FAIL;
    5570           0 :         dest->mnyhigh = -src->mnyhigh;
    5571           0 :         dest->mnylow = (~src->mnylow) + 1u;
    5572           0 :         return SUCCEED;
    5573             : }
    5574             : 
    5575             : 
    5576             : /**
    5577             :  * \ingroup dblib_money
    5578             :  * \brief Negate a DBMONEY4 value.
    5579             :  * 
    5580             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5581             :  * \param src address of a DBMONEY4 structure.  
    5582             :  * \param dest \em output: result of negation. 
    5583             :  * \retval SUCCEED usually.  
    5584             :  * \retval FAIL  on overflow.  
    5585             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5586             :  */
    5587             : RETCODE
    5588           0 : dbmny4minus(DBPROCESS * dbproc, DBMONEY4 * src, DBMONEY4 * dest)
    5589             : {
    5590             :         DBMONEY4 zero;
    5591             : 
    5592           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmny4minus(%p, %p, %p)\n", dbproc, src, dest);
    5593           0 :         CHECK_CONN(FAIL);
    5594           0 :         CHECK_NULP(src, "dbmny4minus", 2, FAIL);
    5595           0 :         CHECK_NULP(dest, "dbmny4minus", 3, FAIL);
    5596             : 
    5597           0 :         dbmny4zero(dbproc, &zero);
    5598           0 :         return (dbmny4sub(dbproc, &zero, src, dest));
    5599             : }
    5600             : 
    5601             : /**
    5602             :  * \ingroup dblib_money
    5603             :  * \brief Zero a DBMONEY4 value.
    5604             :  * 
    5605             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5606             :  * \param dest address of a DBMONEY structure.  
    5607             :  * \retval SUCCEED usually.  
    5608             :  * \retval FAIL  \a dest is NULL.  
    5609             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5610             :  */
    5611             : RETCODE
    5612           0 : dbmny4zero(DBPROCESS * dbproc, DBMONEY4 * dest)
    5613             : {
    5614           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmny4zero(%p, %p)\n", dbproc, dest);
    5615           0 :         CHECK_CONN(FAIL);
    5616           0 :         CHECK_NULP(dest, "dbmny4zero", 2, FAIL);
    5617             : 
    5618           0 :         dest->mny4 = 0;
    5619           0 :         return SUCCEED;
    5620             : }
    5621             : 
    5622             : /**
    5623             :  * \ingroup dblib_money
    5624             :  * \brief Add two DBMONEY4 values.
    5625             :  * 
    5626             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5627             :  * \param m1 first operand.
    5628             :  * \param m2 other operand. 
    5629             :  * \param sum \em output: result of computation.  
    5630             :  * \retval SUCCEED usually.  
    5631             :  * \retval FAIL  on overflow.  
    5632             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5633             :  */
    5634             : RETCODE
    5635           0 : dbmny4add(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * sum)
    5636             : {
    5637           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmny4add(%p, %p, %p, %p)\n", dbproc, m1, m2, sum);
    5638           0 :         CHECK_CONN(FAIL);
    5639           0 :         CHECK_NULP(m1, "dbmny4add", 2, FAIL);
    5640           0 :         CHECK_NULP(m2, "dbmny4add", 3, FAIL);
    5641           0 :         CHECK_NULP(sum, "dbmny4add", 4, FAIL);
    5642             : 
    5643           0 :         sum->mny4 = m1->mny4 + m2->mny4;
    5644           0 :         if (((m1->mny4 < 0) && (m2->mny4 < 0) && (sum->mny4 >= 0))
    5645           0 :             || ((m1->mny4 > 0) && (m2->mny4 > 0) && (sum->mny4 <= 0))) {
    5646             :                 /* overflow */
    5647           0 :                 sum->mny4 = 0;
    5648           0 :                 return FAIL;
    5649             :         }
    5650             :         return SUCCEED;
    5651             : }
    5652             : 
    5653             : /**
    5654             :  * \ingroup dblib_money
    5655             :  * \brief Subtract two DBMONEY4 values.
    5656             :  * 
    5657             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5658             :  * \param m1 first operand.
    5659             :  * \param m2 other operand, subtracted from \a m1. 
    5660             :  * \param diff \em output: result of computation.  
    5661             :  * \retval SUCCEED usually.  
    5662             :  * \retval FAIL  on overflow.  
    5663             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5664             :  */
    5665             : RETCODE
    5666           0 : dbmny4sub(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * diff)
    5667             : {
    5668             : 
    5669           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmny4sub(%p, %p, %p, %p)\n", dbproc, m1, m2, diff);
    5670           0 :         CHECK_CONN(FAIL);
    5671           0 :         CHECK_NULP(m1, "dbmny4sub", 2, FAIL);
    5672           0 :         CHECK_NULP(m2, "dbmny4sub", 3, FAIL);
    5673           0 :         CHECK_NULP(diff, "dbmny4sub", 4, FAIL);
    5674             : 
    5675           0 :         diff->mny4 = m1->mny4 - m2->mny4;
    5676           0 :         if (((m1->mny4 <= 0) && (m2->mny4 > 0) && (diff->mny4 > 0))
    5677           0 :             || ((m1->mny4 >= 0) && (m2->mny4 < 0) && (diff->mny4 < 0))) {
    5678             :                 /* overflow */
    5679           0 :                 diff->mny4 = 0;
    5680           0 :                 return FAIL;
    5681             :         }
    5682             :         return SUCCEED;
    5683             : }
    5684             : 
    5685             : #if defined(DBLIB_UNIMPLEMENTED)
    5686             : /**
    5687             :  * \ingroup dblib_money
    5688             :  * \brief Multiply two DBMONEY4 values.
    5689             :  * 
    5690             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5691             :  * \param m1 first operand.
    5692             :  * \param m2 other operand. 
    5693             :  * \param prod \em output: result of computation.  
    5694             :  * \retval SUCCEED usually.  
    5695             :  * \retval FAIL a parameter is NULL.  
    5696             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5697             :  * \todo Unimplemented.
    5698             :  */
    5699             : RETCODE
    5700             : dbmny4mul(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * prod)
    5701             : {
    5702             : 
    5703             :         tdsdump_log(TDS_DBG_FUNC, "dbmny4mul(%p, %p, %p, %p)\n", dbproc, m1, m2, prod);
    5704             :         CHECK_CONN(FAIL);
    5705             :         CHECK_NULP(m1, "dbmny4mul", 2, FAIL);
    5706             :         CHECK_NULP(m2, "dbmny4mul", 3, FAIL);
    5707             :         CHECK_NULP(prod, "dbmny4mul", 4, FAIL);
    5708             : 
    5709             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmny4mul()\n");
    5710             :         return FAIL;
    5711             : }
    5712             : 
    5713             : /**
    5714             :  * \ingroup dblib_money
    5715             :  * \brief Divide two DBMONEY4 values.
    5716             :  * 
    5717             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5718             :  * \param m1 dividend.
    5719             :  * \param m2 divisor. 
    5720             :  * \param quotient \em output: result of computation.  
    5721             :  * \retval SUCCEED usually.  
    5722             :  * \retval FAIL a parameter is NULL.  
    5723             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5724             :  * \todo Unimplemented.
    5725             :  */
    5726             : RETCODE
    5727             : dbmny4divide(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2, DBMONEY4 * quotient)
    5728             : {
    5729             : 
    5730             :         tdsdump_log(TDS_DBG_FUNC, "dbmny4divide(%p, %p, %p, %p)\n", dbproc, m1, m2, quotient);
    5731             :         CHECK_CONN(FAIL);
    5732             :         CHECK_NULP(m1, "dbmny4divide", 2, FAIL);
    5733             :         CHECK_NULP(m2, "dbmny4divide", 3, FAIL);
    5734             :         CHECK_NULP(quotient, "dbmny4divide", 4, FAIL);
    5735             : 
    5736             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbmny4divide()\n");
    5737             :         return FAIL;
    5738             : }
    5739             : #endif
    5740             : 
    5741             : /**
    5742             :  * \ingroup dblib_money
    5743             :  * \brief Compare two DBMONEY4 values.
    5744             :  * 
    5745             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5746             :  * \param m1 some money.
    5747             :  * \param m2 some other money. 
    5748             :  * \retval 0 m1 == m2. 
    5749             :  * \retval -1 m1 < m2.
    5750             :  * \retval  1 m1 > m2.
    5751             :  * \sa dbmnyadd(), dbmnysub(), dbmnymul(), dbmnydivide(), dbmnyminus(), dbmny4add(), dbmny4sub(), dbmny4mul(), dbmny4divide(), dbmny4minus().
    5752             :  */
    5753             : int
    5754           0 : dbmny4cmp(DBPROCESS * dbproc, DBMONEY4 * m1, DBMONEY4 * m2)
    5755             : {
    5756             : 
    5757           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmny4cmp(%p, %p, %p)\n", dbproc, m1, m2);
    5758           0 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    5759           0 :         CHECK_NULP(m1, "dbmny4cmp", 2, 0);
    5760           0 :         CHECK_NULP(m2, "dbmny4cmp", 3, 0);
    5761             : 
    5762           0 :         if (m1->mny4 < m2->mny4) {
    5763             :                 return -1;
    5764             :         }
    5765           0 :         if (m1->mny4 > m2->mny4) {
    5766             :                 return 1;
    5767             :         }
    5768           0 :         return 0;
    5769             : }
    5770             : 
    5771             : /**
    5772             :  * \ingroup dblib_money
    5773             :  * \brief Copy a DBMONEY4 value.
    5774             :  * 
    5775             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5776             :  * \param src address of a DBMONEY4 structure.  
    5777             :  * \param dest \em output: new money. 
    5778             :  * \retval SUCCEED or FAIL if src/dest NULL.
    5779             :  * \sa dbmnycopy(), dbmnyminus(), dbmny4minus(). 
    5780             :  */
    5781             : RETCODE
    5782           0 : dbmny4copy(DBPROCESS * dbproc, DBMONEY4 * src, DBMONEY4 * dest)
    5783             : {
    5784             : 
    5785           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmny4copy(%p, %p, %p)\n", dbproc, src, dest);
    5786           0 :         CHECK_CONN(FAIL);
    5787           0 :         CHECK_NULP(src, "dbmny4copy", 2, FAIL);
    5788           0 :         CHECK_NULP(dest, "dbmny4copy", 3, FAIL);
    5789             : 
    5790           0 :         dest->mny4 = src->mny4;
    5791           0 :         return SUCCEED;
    5792             : }
    5793             : 
    5794             : /**
    5795             :  * \ingroup dblib_datetime
    5796             :  * \brief Compare DBDATETIME values, similar to strcmp(3).
    5797             :  * 
    5798             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5799             :  * \param d1 a \c DBDATETIME structure address
    5800             :  * \param d2 another \c DBDATETIME structure address
    5801             :  * \retval   0 d1 = d2.
    5802             :  * \retval  -1 d1 < d2.
    5803             :  * \retval   1 d1 > d2.
    5804             :  * \sa dbdate4cmp(), dbmnycmp(), dbmny4cmp().
    5805             :  */
    5806             : int
    5807           0 : dbdatecmp(DBPROCESS * dbproc, DBDATETIME * d1, DBDATETIME * d2)
    5808             : {
    5809           0 :         tdsdump_log(TDS_DBG_FUNC, "dbdatecmp(%p, %p, %p)\n", dbproc, d1, d2);
    5810           0 :         CHECK_CONN(FAIL);
    5811           0 :         CHECK_NULP(d1, "dbdatecmp", 2, 0);
    5812           0 :         CHECK_NULP(d2, "dbdatecmp", 3, 0);
    5813             : 
    5814           0 :         if (d1->dtdays == d2->dtdays) {
    5815           0 :                 if (d1->dttime == d2->dttime)
    5816             :                         return 0;
    5817           0 :                 return d1->dttime > d2->dttime ? 1 : -1;
    5818             :         }
    5819             : 
    5820             :         /* date 1 is before 1900 */
    5821           0 :         if (d1->dtdays > 2958463) {
    5822             : 
    5823           0 :                 if (d2->dtdays > 2958463) /* date 2 is before 1900 */
    5824           0 :                         return d1->dtdays > d2->dtdays ? 1 : -1;
    5825             :                 return -1;
    5826             :         }
    5827             : 
    5828             :         /* date 1 is after 1900 */
    5829           0 :         if (d2->dtdays < 2958463) /* date 2 is after 1900 */
    5830           0 :                 return d1->dtdays > d2->dtdays ? 1 : -1;
    5831             :         return 1;
    5832             : }
    5833             : 
    5834             : static RETCODE
    5835          24 : dblib_datecrack(DBPROCESS * dbproc, BOOL nano_precision, DBDATEREC * output, int type, const void * data)
    5836             : {
    5837             :         TDSDATEREC dr;
    5838          24 :         struct tds_sybase_dbdaterec *di = (struct tds_sybase_dbdaterec*) output;
    5839             : 
    5840          24 :         tdsdump_log(TDS_DBG_FUNC, "dblib_datecrack(%p, %d, %p, %d, %p)\n", dbproc, nano_precision, output, type, data);
    5841          24 :         CHECK_NULP(output, "dbdatecrack", 2, FAIL);
    5842          24 :         CHECK_PARAMETER(data, SYBENDTP, FAIL);
    5843             : 
    5844          24 :         if (TDS_FAILED(tds_datecrack(type, data, &dr)))
    5845             :                 return FAIL;
    5846             : 
    5847          24 :         di->dateyear = dr.year;
    5848          24 :         di->quarter = dr.quarter;
    5849          24 :         di->datemonth = dr.month;
    5850          24 :         di->datedmonth = dr.day;
    5851          24 :         di->datedyear = dr.dayofyear;
    5852          24 :         di->datedweek = dr.weekday;
    5853          24 :         di->datehour = dr.hour;
    5854          24 :         di->dateminute = dr.minute;
    5855          24 :         di->datesecond = dr.second;
    5856          24 :         di->datetzone = dr.timezone;
    5857          24 :         if (nano_precision)
    5858             :                 /* here we are writing to nanosecond field */
    5859           4 :                 di->datemsecond = dr.decimicrosecond * 100u;
    5860             :         else
    5861          20 :                 di->datemsecond = dr.decimicrosecond / 10000u;
    5862             :         /* Revert to compiled-in default if dbproc can't be used to find the runtime override. */
    5863          24 :         if (dbproc ? dbproc->msdblib : dblib_msdblib) {
    5864           0 :                 ++di->quarter;
    5865           0 :                 ++di->datemonth;
    5866           0 :                 ++di->datedweek;
    5867             :         }
    5868             :         return SUCCEED;
    5869             : }
    5870             : 
    5871             : /**
    5872             :  * \ingroup dblib_core
    5873             :  * \brief Break a DBDATETIME value into useful pieces.
    5874             :  *
    5875             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5876             :  * \param di \em output: structure to contain the exploded parts of \a datetime.
    5877             :  * \param datetime \em input: \c DBDATETIME to be converted.
    5878             :  * \retval SUCCEED always.
    5879             :  * \remarks The members of \a di have different names, depending on whether \c --with-msdblib was configured.
    5880             :  *
    5881             :  * If DBPROCESS is NULL, dbdatecrack() uses the compiled in default
    5882             :  * value of MSDBLIB as of when libsybdb was compiled, irrespective of its value when the
    5883             :  * application is compiled.  This can lead to incorrect results because Sybase and Microsoft use different
    5884             :  * ranges -- [0,11] vs. [1,12] -- for the month.
    5885             :  *
    5886             :  * \sa dbconvert(), dbdata(), dbdatechar(), dbdatename(), dbdatepart(), tdsdbopen().
    5887             :  */
    5888             : RETCODE
    5889          20 : dbdatecrack(DBPROCESS * dbproc, DBDATEREC * di, DBDATETIME * datetime)
    5890             : {
    5891          20 :         return dblib_datecrack(dbproc, FALSE, di, SYBDATETIME, datetime);
    5892             : }
    5893             : 
    5894             : /**
    5895             :  * \ingroup dblib_core
    5896             :  * \brief Break any kind of date or time value into useful pieces.
    5897             :  *
    5898             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5899             :  * \param di \em output: structure to contain the exploded parts of \a datetime.
    5900             :  * \param type \em input: \c type of date/time value returned by dbcoltype().
    5901             :  * \param data \em input: \c date/time value to be converted.
    5902             :  * \retval SUCCEED always.
    5903             :  * \remarks The members of \a di have different names, depending on whether \c --with-msdblib was configured.
    5904             :  *
    5905             :  * This is an extension to dbdatecrack(), see it for more information.
    5906             :  *
    5907             :  * \sa dbdatecrack(), dbconvert(), dbdata(), dbdatechar(), dbdatename(), dbdatepart(), tdsdbopen().
    5908             :  */
    5909             : RETCODE
    5910           4 : dbanydatecrack(DBPROCESS * dbproc, DBDATEREC2 * di, int type, const void *data)
    5911             : {
    5912           4 :         return dblib_datecrack(dbproc, TRUE, (DBDATEREC *) di, type, data);
    5913             : }
    5914             : 
    5915             : #if defined(DBLIB_UNIMPLEMENTED)
    5916             : /**
    5917             :  * \ingroup dblib_core
    5918             :  * \brief Clear remote passwords from the LOGINREC structure.
    5919             :  * 
    5920             :  * \param login structure to pass to dbopen().
    5921             :  * \sa dblogin(), dbopen(), dbrpwset(), DBSETLAPP(), DBSETLHOST(), DBSETLPWD(), DBSETLUSER().  
    5922             :  * \remarks Useful for remote stored procedure calls, but not in high demand from FreeTDS.  
    5923             :  * \todo Unimplemented.
    5924             :  */
    5925             : void
    5926             : dbrpwclr(LOGINREC * login)
    5927             : {
    5928             :         tdsdump_log(TDS_DBG_FUNC, "dbrpwclr(%p)\n", login);
    5929             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbrpwclr()\n");
    5930             : }
    5931             : 
    5932             : /**
    5933             :  * \ingroup dblib_core
    5934             :  * \brief Add a remote password to the LOGINREC structure.
    5935             :  * 
    5936             :  * \param login structure to pass to dbopen().
    5937             :  * \param srvname server for which \a password should be used.  
    5938             :  * \param password you guessed it, let's hope no else does.  
    5939             :  * \param pwlen count of \a password, in bytes.
    5940             :  * \remarks Useful for remote stored procedure calls, but not in high demand from FreeTDS.  
    5941             :  * \sa dblogin(), dbopen(), dbrpwclr(), DBSETLAPP(), DBSETLHOST(), DBSETLPWD(), DBSETLUSER().  
    5942             :  * \todo Unimplemented.
    5943             :  */
    5944             : RETCODE
    5945             : dbrpwset(LOGINREC * login, char *srvname, char *password, int pwlen)
    5946             : {
    5947             :         tdsdump_log(TDS_DBG_FUNC, "dbrpwset(%p, %s, %s, %d)\n", login, srvname, password, pwlen);
    5948             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbrpwset()\n");
    5949             :         return SUCCEED;
    5950             : }
    5951             : #endif
    5952             : 
    5953             : /**
    5954             :  * \ingroup dblib_core
    5955             :  * \brief Get server process ID for a \c DBPROCESS.
    5956             :  * 
    5957             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5958             :  * \return \em "spid", the server's process ID.  
    5959             :  * \sa dbopen().  
    5960             :  */
    5961             : int
    5962          10 : dbspid(DBPROCESS * dbproc)
    5963             : {
    5964          10 :         tdsdump_log(TDS_DBG_FUNC, "dbspid(%p)\n", dbproc);
    5965          10 :         CHECK_CONN(-1);
    5966             : 
    5967          10 :         return dbproc->tds_socket->conn->spid;
    5968             : }
    5969             : 
    5970             : /**
    5971             :  * \ingroup dblib_core
    5972             :  * \brief Associate client-allocated (and defined) data with a \c DBPROCESS.   
    5973             :  * 
    5974             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5975             :  * \param ptr address of client-defined data.
    5976             :  * \remarks \a ptr is the location of user data that \c db-lib will associate with \a dbproc. 
    5977             :  * The client allocates the buffer addressed by \a ptr.  \c db-lib never examines or uses the information; 
    5978             :  * it just stashes the pointer for later retrieval by the application with \c dbgetuserdata().  
    5979             :  * \sa dbgetuserdata().  
    5980             :  */
    5981             : void
    5982         262 : dbsetuserdata(DBPROCESS * dbproc, BYTE * ptr)
    5983             : {
    5984         262 :         tdsdump_log(TDS_DBG_FUNC, "dbsetuserdata(%p, %p)\n", dbproc, ptr);
    5985         262 :         CHECK_PARAMETER(dbproc, SYBENULL, );
    5986             : 
    5987         262 :         dbproc->user_data = ptr;
    5988             : }
    5989             : 
    5990             : /**
    5991             :  * \ingroup dblib_core
    5992             :  * \brief Get address of user-allocated data from a \c DBPROCESS.   
    5993             :  * 
    5994             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    5995             :  * \return address of user-defined data that \c db-lib associated with \a dbproc when the client called  dbsetuserdata(). 
    5996             :  * \retval undefined (probably \c NULL) dbsetuserdata() was not previously called.  
    5997             :  * \sa dbsetuserdata().  
    5998             :  */
    5999             : BYTE *
    6000         196 : dbgetuserdata(DBPROCESS * dbproc)
    6001             : {
    6002         196 :         tdsdump_log(TDS_DBG_FUNC, "dbgetuserdata(%p)\n", dbproc);
    6003         196 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    6004             : 
    6005         196 :         return dbproc->user_data;
    6006             : }
    6007             : 
    6008             : /**
    6009             :  * \ingroup dblib_core
    6010             :  * \brief Specify a db-lib version level.
    6011             :  * 
    6012             :  * \param version anything, really. 
    6013             :  * \retval SUCCEED Always.  
    6014             :  * \remarks No effect on behavior of \c db-lib in \c FreeTDS.  
    6015             :  * \sa 
    6016             :  */
    6017             : RETCODE
    6018          40 : dbsetversion(DBINT version)
    6019             : {
    6020          40 :         tdsdump_log(TDS_DBG_FUNC, "dbsetversion(%d)\n", version);
    6021             : 
    6022          40 :         switch (version) {
    6023          40 :         case DBVERSION_42:
    6024             :         case DBVERSION_46:
    6025             :         case DBVERSION_100:
    6026             :         case DBVERSION_70:
    6027             :         case DBVERSION_71:
    6028             :         case DBVERSION_72:
    6029             :         case DBVERSION_73:
    6030             :         case DBVERSION_74:
    6031          40 :                 g_dblib_version = version;
    6032          40 :                 return SUCCEED;
    6033             :         default:
    6034             :                 break;
    6035             :         }
    6036             :         
    6037           0 :         dbperror(NULL, SYBEIVERS, 0);
    6038           0 :         return FAIL;
    6039             : }
    6040             : 
    6041             : /**
    6042             :  * \ingroup dblib_money
    6043             :  * \brief Copy a DBMONEY value.
    6044             :  * 
    6045             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6046             :  * \param src address of a DBMONEY structure.  
    6047             :  * \param dest \em output: new money. 
    6048             :  * \retval SUCCEED always, unless \a src or \a dest is \c NULL.  
    6049             :  * \sa 
    6050             :  */
    6051             : RETCODE
    6052           0 : dbmnycopy(DBPROCESS * dbproc, DBMONEY * src, DBMONEY * dest)
    6053             : {
    6054           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmnycopy(%p, %p, %p)\n", dbproc, src, dest);
    6055           0 :         CHECK_CONN(FAIL);
    6056           0 :         CHECK_NULP(src, "dbmnycopy", 2, FAIL);
    6057           0 :         CHECK_NULP(dest, "dbmnycopy", 3, FAIL);
    6058             : 
    6059           0 :         dest->mnylow = src->mnylow;
    6060           0 :         dest->mnyhigh = src->mnyhigh;
    6061           0 :         return SUCCEED;
    6062             : }
    6063             : 
    6064             : 
    6065             : /**
    6066             :  * \ingroup dblib_core
    6067             :  * \brief Cancel the query currently being retrieved, discarding all pending rows.  
    6068             :  * 
    6069             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6070             :  * \sa 
    6071             :  */
    6072             : RETCODE
    6073         814 : dbcanquery(DBPROCESS * dbproc)
    6074             : {
    6075             :         TDSRET rc;
    6076             :         TDS_INT result_type;
    6077             : 
    6078         814 :         tdsdump_log(TDS_DBG_FUNC, "dbcanquery(%p)\n", dbproc);
    6079         814 :         CHECK_CONN(FAIL);
    6080             : 
    6081             :         /* Just throw away all pending rows from the last query */
    6082             : 
    6083         814 :         rc = tds_process_tokens(dbproc->tds_socket, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE);
    6084             : 
    6085         814 :         if (TDS_FAILED(rc))
    6086             :                 return FAIL;
    6087             : 
    6088         814 :         dbproc->dbresults_state = _DB_RES_NEXT_RESULT;
    6089             : 
    6090         814 :         return SUCCEED;
    6091             : }
    6092             : 
    6093             : /**
    6094             :  * \ingroup dblib_core
    6095             :  * \brief Erase the command buffer, in case \c DBNOAUTOFREE was set with dbsetopt(). 
    6096             :  * 
    6097             :  * 
    6098             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6099             :  * \sa dbcmd(), dbfcmd(), dbgetchar(), dbsqlexec(), dbsqlsend(), dbsetopt(), dbstrcpy(), dbstrlen().  
    6100             :  */
    6101             : void
    6102       20612 : dbfreebuf(DBPROCESS * dbproc)
    6103             : {
    6104       20612 :         tdsdump_log(TDS_DBG_FUNC, "dbfreebuf(%p)\n", dbproc);
    6105       20612 :         CHECK_PARAMETER(dbproc, SYBENULL, );
    6106             : 
    6107       20612 :         if (dbproc->dbbuf)
    6108       20592 :                 TDS_ZERO_FREE(dbproc->dbbuf);
    6109       20612 :         dbproc->dbbufsz = 0;
    6110             : }
    6111             : 
    6112             : /**
    6113             :  * \ingroup dblib_core
    6114             :  * \brief Reset an option.
    6115             :  * 
    6116             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6117             :  * \param option to be turned off.
    6118             :  * \param param clearing some options requires a parameter, believe it or not.  
    6119             :  * \retval SUCCEED \a option and \a parameter seem sane.
    6120             :  * \retval FAIL no such \a option.
    6121             :  * \remarks Only the following options are recognized:
    6122             :         - DBARITHABORT
    6123             :         - DBARITHIGNORE
    6124             :         - DBCHAINXACTS
    6125             :         - DBFIPSFLAG
    6126             :         - DBISOLATION
    6127             :         - DBNOCOUNT
    6128             :         - DBNOEXEC
    6129             :         - DBPARSEONLY
    6130             :         - DBSHOWPLAN
    6131             :         - DBSTORPROCID
    6132             :         - DBQUOTEDIDENT
    6133             :         - DBSETTIME
    6134             :  * \sa dbisopt(), dbsetopt().
    6135             :  */
    6136             : RETCODE
    6137          20 : dbclropt(DBPROCESS * dbproc, int option, const char param[])
    6138             : {
    6139             :         char *cmd;
    6140             : 
    6141          20 :         tdsdump_log(TDS_DBG_FUNC, "dbclropt(%p, %d, %s)\n", dbproc, option, param);
    6142          20 :         CHECK_CONN(FAIL);
    6143          20 :         if (option != DBSETTIME) {
    6144           0 :                 CHECK_NULP(param, "dbclropt", 3, FAIL);
    6145             :         }
    6146             : 
    6147          20 :         if ((option < 0) || (option >= DBNUMOPTIONS)) {
    6148             :                 return FAIL;
    6149             :         }
    6150          20 :         dbproc->dbopts[option].factive = 0;
    6151             :         switch (option) {
    6152           0 :         case DBARITHABORT:
    6153             :         case DBARITHIGNORE:
    6154             :         case DBCHAINXACTS:
    6155             :         case DBFIPSFLAG:
    6156             :         case DBISOLATION:
    6157             :         case DBNOCOUNT:
    6158             :         case DBNOEXEC:
    6159             :         case DBPARSEONLY:
    6160             :         case DBSHOWPLAN:
    6161             :         case DBSTORPROCID:
    6162             :         case DBQUOTEDIDENT:
    6163             :                 /* server options (on/off) */
    6164           0 :                 if (asprintf(&cmd, "set %s off\n", dbproc->dbopts[option].text) < 0) {
    6165             :                         return FAIL;
    6166             :                 }
    6167           0 :                 dbstring_concat(&(dbproc->dboptcmd), cmd);
    6168           0 :                 free(cmd);
    6169           0 :                 break;
    6170           0 :         case DBBUFFER:
    6171           0 :                 buffer_set_capacity(dbproc, 1); /* frees row_buf->rows */
    6172           0 :                 return SUCCEED;
    6173             :                 break;
    6174          20 :         case DBSETTIME:
    6175          20 :                 tds_mutex_lock(&dblib_mutex);
    6176             :                 /*
    6177             :                  * Use global value of query timeout set by dbsettime() if any,
    6178             :                  * otherwise set it to zero just like tds_init_socket() does.
    6179             :                  */
    6180          20 :                 if (g_dblib_ctx.query_timeout > 0) {
    6181          10 :                         dbproc->tds_socket->query_timeout = g_dblib_ctx.query_timeout;
    6182             :                 } else {
    6183          10 :                         dbproc->tds_socket->query_timeout = 0;
    6184             :                 }
    6185          20 :                 tds_mutex_unlock(&dblib_mutex);
    6186          20 :                 return SUCCEED;
    6187             :                 break;
    6188             :         default:
    6189             :                 break;
    6190             :         }
    6191           0 :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbclropt(option = %d)\n", option);
    6192             :         return FAIL;
    6193             : }
    6194             : 
    6195             : /**
    6196             :  * \ingroup dblib_core
    6197             :  * \brief Get value of an option
    6198             :  * 
    6199             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6200             :  * \param option the option
    6201             :  * \param param a parameter to \a option. 
    6202             :  * \sa dbclropt(), dbsetopt().
    6203             :  */
    6204             : DBBOOL
    6205         180 : dbisopt(DBPROCESS * dbproc, int option, const char param[])
    6206             : {
    6207         180 :         tdsdump_log(TDS_DBG_FUNC, "dbisopt(%p, %d, %s)\n", dbproc, option, param);
    6208         180 :         CHECK_PARAMETER(dbproc, SYBENULL, FALSE);
    6209             :         /* sometimes param can be NULL */
    6210             :         
    6211         180 :         if ((option < 0) || (option >= DBNUMOPTIONS)) {
    6212             :                 return FALSE;
    6213             :         }
    6214         180 :         return dbproc->dbopts[option].factive;
    6215             : }
    6216             : 
    6217             : /** \internal
    6218             :  * \ingroup dblib_internal
    6219             :  * \brief Get number of the row currently being read.
    6220             :  * 
    6221             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6222             :  * \return ostensibly the row number, or 0 if no rows have been read yet.  
    6223             :  * \retval 0 Always.  
    6224             :  * \sa DBCURROW(), dbclrbuf(), DBFIRSTROW(), dbgetrow(), DBLASTROW(), dbnextrow(), dbsetopt(),.  
    6225             :  * \todo Unimplemented.
    6226             :  */
    6227             : DBINT
    6228           0 : dbcurrow(DBPROCESS * dbproc)
    6229             : {
    6230           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcurrow(%p)\n", dbproc);
    6231           0 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    6232           0 :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbcurrow()\n");
    6233             :         return 0;
    6234             : }
    6235             : 
    6236             : 
    6237             : /** \internal
    6238             :  * \ingroup dblib_internal
    6239             :  * \brief Get returned row's type.
    6240             :  * 
    6241             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6242             :  * \sa DBROWTYPE().  
    6243             :  */
    6244             : STATUS
    6245           0 : dbrowtype(DBPROCESS * dbproc)
    6246             : {
    6247           0 :         tdsdump_log(TDS_DBG_FUNC, "dbrowtype(%p)\n", dbproc);
    6248           0 :         CHECK_PARAMETER(dbproc, SYBENULL, NO_MORE_ROWS);
    6249           0 :         return dbproc->row_type;
    6250             : }
    6251             : 
    6252             : 
    6253             : /** \internal
    6254             :  * \ingroup dblib_internal
    6255             :  * \brief Get number of the row just returned.
    6256             :  * 
    6257             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6258             :  * \sa DBCURROW().
    6259             :  * \todo Unimplemented.
    6260             :  */
    6261             : int
    6262           0 : dbcurcmd(DBPROCESS * dbproc)
    6263             : {
    6264           0 :         tdsdump_log(TDS_DBG_FUNC, "dbcurcmd(%p)\n", dbproc);
    6265           0 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    6266           0 :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbcurcmd()\n");
    6267             :         return 0;
    6268             : }
    6269             : 
    6270             : 
    6271             : /** 
    6272             :  * \ingroup dblib_core
    6273             :  * \brief See if more commands are to be processed.
    6274             :  * 
    6275             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6276             :  * \sa DBMORECMDS(). DBCMDROW(), dbresults(), DBROWS(), DBROWTYPE().  
    6277             :  */
    6278             : RETCODE
    6279          30 : dbmorecmds(DBPROCESS * dbproc)
    6280             : {
    6281          30 :         tdsdump_log(TDS_DBG_FUNC, "dbmorecmds(%p)\n", dbproc);
    6282          30 :         CHECK_CONN(FAIL);
    6283             : 
    6284          30 :         if (dbproc->tds_socket->res_info == NULL) {
    6285             :                 return FAIL;
    6286             :         }
    6287             : 
    6288          30 :         if (!dbproc->tds_socket->res_info->more_results) {
    6289          20 :                 tdsdump_log(TDS_DBG_FUNC, "more_results is false; returns FAIL\n");
    6290             :                 return FAIL;
    6291             :         }
    6292             :         
    6293          10 :         tdsdump_log(TDS_DBG_FUNC, "more_results is true; returns SUCCEED\n");
    6294             :         
    6295             :         return SUCCEED;
    6296             : }
    6297             : 
    6298             : /**
    6299             :  * \ingroup dblib_rpc
    6300             :  * \brief Get datatype of a stored procedure's return parameter.
    6301             :  * 
    6302             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6303             :  * \param retnum Nth return parameter, between 1 and \c dbnumrets().  
    6304             :  * \return SYB* datatype token, or -1 if \a retnum is out of range. 
    6305             :  * \sa dbnextrow(), dbnumrets(), dbprtype(), dbresults(), dbretdata(), dbretlen(), dbretname(), dbrpcinit(), dbrpcparam(). 
    6306             :  */
    6307             : int
    6308          66 : dbrettype(DBPROCESS * dbproc, int retnum)
    6309             : {
    6310             :         TDSCOLUMN *colinfo;
    6311             : 
    6312          66 :         tdsdump_log(TDS_DBG_FUNC, "dbrettype(%p, %d)\n", dbproc, retnum);
    6313          66 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    6314          66 :         assert(dbproc->tds_socket);
    6315          66 :         assert(dbproc->tds_socket->param_info);
    6316             : 
    6317          66 :         if (retnum < 1 || retnum > dbproc->tds_socket->param_info->num_cols)
    6318             :                 return -1;
    6319             : 
    6320          66 :         colinfo = dbproc->tds_socket->param_info->columns[retnum - 1];
    6321             : 
    6322          66 :         return tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
    6323             : }
    6324             : 
    6325             : /**
    6326             :  * \ingroup dblib_core
    6327             :  * \brief Get size of the command buffer, in bytes. 
    6328             :  * 
    6329             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6330             :  * \sa dbcmd(), dbfcmd(), dbfreebuf(), dbgetchar(), dbstrcpy(). 
    6331             :  */
    6332             : int
    6333           0 : dbstrlen(DBPROCESS * dbproc)
    6334             : {
    6335           0 :         tdsdump_log(TDS_DBG_FUNC, "dbstrlen(%p)\n", dbproc);
    6336           0 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    6337             :         
    6338           0 :         return dbproc->dbbufsz;
    6339             : }
    6340             : 
    6341             : 
    6342             : /**
    6343             :  * \ingroup dblib_core
    6344             :  * \brief Get address of a position in the command buffer.
    6345             :  * 
    6346             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6347             :  * \param pos offset within the command buffer, starting at \em 0.   
    6348             :  * \remarks A bit overspecialized, this one.  
    6349             :  * \sa dbcmd(), dbfcmd(), dbfreebuf(), dbstrcpy(), dbstrlen(),
    6350             :  */
    6351             : char *
    6352         490 : dbgetchar(DBPROCESS * dbproc, int pos)
    6353             : {
    6354         490 :         tdsdump_log(TDS_DBG_FUNC, "dbgetchar(%p, %d)\n", dbproc, pos);
    6355         490 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    6356         490 :         tdsdump_log(TDS_DBG_FUNC, "dbgetchar() bufsz = %d, pos = %d\n", dbproc->dbbufsz, pos);
    6357             : 
    6358         490 :         if (dbproc->dbbufsz > 0) {
    6359         490 :                 if (pos >= 0 && pos < (dbproc->dbbufsz - 1))
    6360         480 :                         return (char *) &dbproc->dbbuf[pos];
    6361             :                 return NULL;
    6362             :         }
    6363             :         return NULL;
    6364             : }
    6365             : 
    6366             : /**
    6367             :  * \ingroup dblib_core
    6368             :  * \brief Get a copy of a chunk of the command buffer.
    6369             :  * 
    6370             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6371             :  * \param start position in the command buffer to start copying from, starting from \em 0.  
    6372             :  *      If start is past the end of the command buffer, dbstrcpy() inserts a null terminator at dest[0].
    6373             :  * \param numbytes number of bytes to copy. 
    6374             :         - If -1, dbstrcpy() copies the whole command buffer.  
    6375             :         - If  0 dbstrcpy() writes a \c NULL to dest[0]. 
    6376             :         - If the command buffer contains fewer than \a numbytes (taking \a start into account) dbstrcpy() 
    6377             :         copies the rest of it.  
    6378             :  * \param dest \em output: the buffer to write to.  Make sure it's big enough.  
    6379             :  * \retval SUCCEED the inputs were valid and \a dest was affected.  
    6380             :  * \retval FAIL \a start < 0 or \a numbytes < -1.  
    6381             :  * \sa dbcmd(), dbfcmd(), dbfreebuf(), dbgetchar(), dbstrlen().  
    6382             :  */
    6383             : RETCODE
    6384           0 : dbstrcpy(DBPROCESS * dbproc, int start, int numbytes, char *dest)
    6385             : {
    6386           0 :         tdsdump_log(TDS_DBG_FUNC, "dbstrcpy(%p, %d, %d, %s)\n", dbproc, start, numbytes, dest);
    6387           0 :         CHECK_CONN(FAIL);
    6388           0 :         CHECK_NULP(dest, "dbstrcpy", 4, FAIL);
    6389             : 
    6390           0 :         if (start < 0) {
    6391           0 :                 dbperror(dbproc, SYBENSIP, 0);
    6392           0 :                 return FAIL;
    6393             :         }
    6394           0 :         if (numbytes < -1) {
    6395           0 :                 dbperror(dbproc, SYBEBNUM, 0);
    6396           0 :                 return FAIL;
    6397             :         }
    6398           0 :         dest[0] = 0;            /* start with empty string being returned */
    6399           0 :         if (dbproc->dbbufsz > 0 && start < dbproc->dbbufsz) {
    6400           0 :                 if (numbytes == -1)
    6401           0 :                         numbytes = dbproc->dbbufsz - start;
    6402           0 :                 if (start + numbytes > dbproc->dbbufsz)
    6403           0 :                         numbytes = dbproc->dbbufsz - start;
    6404           0 :                 memcpy(dest, (char *) &dbproc->dbbuf[start], numbytes);
    6405           0 :                 dest[numbytes] = '\0';
    6406             :         }
    6407             :         return SUCCEED;
    6408             : }
    6409             : 
    6410             : /**
    6411             :  * \ingroup dblib_core
    6412             :  * \brief safely quotes character values in SQL text.  
    6413             :  * 
    6414             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6415             :  * \param src input string.
    6416             :  * \param srclen length of \a src in bytes, or -1 to indicate it's null-terminated.  
    6417             :  * \param dest \em output: client-provided output buffer. 
    6418             :  * \param destlen size of \a dest in bytes, or -1 to indicate it's "big enough" and the data should be null-terminated.  
    6419             :  * \param quotetype
    6420             :         - \c DBSINGLE Doubles all single quotes (').
    6421             :         - \c DBDOUBLE Doubles all double quotes (").
    6422             :         - \c DBBOTH   Doubles all single and double quotes.
    6423             :  * \retval SUCCEED everything worked.
    6424             :  * \retval FAIL no such \a quotetype, or insufficient room in \a dest.
    6425             :  * \sa dbcmd(), dbfcmd(). 
    6426             :  */
    6427             : RETCODE
    6428          50 : dbsafestr(DBPROCESS * dbproc, const char *src, DBINT srclen, char *dest, DBINT destlen, int quotetype)
    6429             : {
    6430          50 :         int i, j = 0;
    6431          50 :         bool squote = false, dquote = false;
    6432             : 
    6433          50 :         tdsdump_log(TDS_DBG_FUNC, "dbsafestr(%p, %s, %d, %s, %d, %d)\n", dbproc, src, srclen, dest, destlen, quotetype);
    6434          50 :         CHECK_NULP(src, "dbsafestr", 2, FAIL);
    6435          50 :         CHECK_NULP(dest, "dbsafestr", 4, FAIL);
    6436             : 
    6437             :         /* check parameters */
    6438          50 :         if (srclen < -1 || destlen < -1)
    6439             :                 return FAIL;
    6440             : 
    6441          50 :         if (srclen == -1)
    6442          50 :                 srclen = (int)strlen(src);
    6443             : 
    6444          50 :         if (quotetype == DBSINGLE || quotetype == DBBOTH)
    6445          40 :                 squote = true;
    6446          50 :         if (quotetype == DBDOUBLE || quotetype == DBBOTH)
    6447          30 :                 dquote = true;
    6448             : 
    6449             :         /* return FAIL if invalid quotetype */
    6450          50 :         if (!dquote && !squote)
    6451             :                 return FAIL;
    6452             : 
    6453             : 
    6454        1790 :         for (i = 0; i < srclen; i++) {
    6455             : 
    6456             :                 /* dbsafestr returns fail if the deststr is not big enough */
    6457             :                 /* need one char + one for terminator */
    6458        1800 :                 if (destlen >= 0 && j >= destlen)
    6459             :                         return FAIL;
    6460             : 
    6461        1790 :                 if (squote && src[i] == '\'')
    6462          40 :                         dest[j++] = '\'';
    6463        1750 :                 else if (dquote && src[i] == '\"')
    6464          30 :                         dest[j++] = '\"';
    6465             : 
    6466        1790 :                 if (destlen >= 0 && j >= destlen)
    6467             :                         return FAIL;
    6468             : 
    6469        1790 :                 dest[j++] = src[i];
    6470             :         }
    6471             : 
    6472          40 :         if (destlen >= 0 && j >= destlen)
    6473             :                 return FAIL;
    6474             : 
    6475          30 :         dest[j] = '\0';
    6476          30 :         return SUCCEED;
    6477             : }
    6478             : 
    6479             : /**
    6480             :  * \ingroup dblib_core
    6481             :  * \brief Print a token value's name to a buffer
    6482             :  * 
    6483             :  * \param token server SYB* value, e.g. SYBINT.  
    6484             : 
    6485             :  * \return ASCII null-terminated string.  
    6486             :  * \sa dbaltop(), dbalttype(), dbcoltype(), dbrettype().
    6487             :  */
    6488             : const char *
    6489          24 : dbprtype(int token)
    6490             : {
    6491          24 :         tdsdump_log(TDS_DBG_FUNC, "dbprtype(%d)\n", token);
    6492          24 :         return tds_prtype(token);
    6493             : }
    6494             : 
    6495             : /**
    6496             :  * \ingroup dblib_core
    6497             :  * \brief describe table column attributes with a single call (Freetds-only API function modelled on dbcolinfo)
    6498             :  * 
    6499             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6500             :  * \param column Nth in the result set, starting from 1.
    6501             :  * \param pdbcol address of structure to be populated by this function.  
    6502             :  * \return SUCCEED or FAIL. 
    6503             :  * \sa dbcolinfo().
    6504             :  */
    6505             : RETCODE
    6506          30 : dbtablecolinfo(DBPROCESS *dbproc, DBINT column, DBCOL *pdbcol)
    6507             : {
    6508             :         TDSCOLUMN *colinfo;
    6509             : 
    6510          30 :         tdsdump_log(TDS_DBG_FUNC, "dbtablecolinfo(%p, %d, %p)\n", dbproc, column, pdbcol);
    6511          30 :         CHECK_CONN(FAIL);
    6512          30 :         CHECK_NULP(pdbcol, "dbtablecolinfo", 3, FAIL);
    6513          30 :         DBPERROR_RETURN(pdbcol->SizeOfStruct != sizeof(DBCOL)
    6514             :                         && pdbcol->SizeOfStruct != sizeof(DBCOL2), SYBECOLSIZE);
    6515             : 
    6516          30 :         colinfo = dbcolptr(dbproc, column);
    6517          30 :         if (!colinfo)
    6518             :                 return FAIL;
    6519             : 
    6520          60 :         strlcpy(pdbcol->Name, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->Name));
    6521          60 :         strlcpy(pdbcol->ActualName, tds_dstr_cstr(&colinfo->column_name), sizeof(pdbcol->ActualName));
    6522          60 :         strlcpy(pdbcol->TableName, tds_dstr_cstr(&colinfo->table_name), sizeof(pdbcol->TableName));
    6523             : 
    6524          30 :         pdbcol->Type = tds_get_conversion_type(colinfo->column_type, colinfo->column_size);
    6525          30 :         pdbcol->UserType = colinfo->column_usertype;
    6526          30 :         pdbcol->MaxLength = colinfo->column_size;
    6527          30 :         if (colinfo->column_nullable)
    6528           0 :                 pdbcol->Null = TRUE;
    6529             :         else
    6530          30 :                 pdbcol->Null = FALSE;
    6531             : 
    6532          30 :         pdbcol->VarLength = FALSE;
    6533             : 
    6534          30 :         if (colinfo->column_nullable
    6535          30 :             || is_nullable_type(colinfo->column_type))
    6536           0 :                 pdbcol->VarLength = TRUE;
    6537             :         
    6538          30 :         pdbcol->Precision = colinfo->column_prec;
    6539          30 :         pdbcol->Scale = colinfo->column_scale;
    6540             : 
    6541          30 :         pdbcol->Updatable = colinfo->column_writeable ? TRUE : FALSE;
    6542          30 :         pdbcol->Identity = colinfo->column_identity ? TRUE : FALSE;
    6543             : 
    6544          30 :         if (pdbcol->SizeOfStruct >= sizeof(DBCOL2)) {
    6545          30 :                 DBCOL2 *col = (DBCOL2 *) pdbcol;
    6546             :                 TDSRET rc;
    6547             : 
    6548          30 :                 col->ServerType = colinfo->on_server.column_type;
    6549          30 :                 col->ServerMaxLength = colinfo->on_server.column_size;
    6550             : 
    6551          30 :                 rc = tds_get_column_declaration(dbproc->tds_socket, colinfo, col->ServerTypeDeclaration);
    6552          30 :                 if (TDS_FAILED(rc))
    6553             :                         return FAIL;
    6554             :         }
    6555             : 
    6556             :         return SUCCEED;
    6557             : }
    6558             : 
    6559             : /**
    6560             :  * \ingroup dblib_core
    6561             :  * \brief Get text timestamp for a column in the current row.
    6562             :  * 
    6563             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6564             :  * \param column number of the column in the \c SELECT statement, starting at 1.
    6565             :  * \return timestamp for \a column, may be NULL.
    6566             :  * \sa dbtxptr(), dbwritetext().  
    6567             :  */
    6568             : DBBINARY *
    6569          56 : dbtxtimestamp(DBPROCESS * dbproc, int column)
    6570             : {
    6571             :         TDSCOLUMN *colinfo;
    6572             :         TDSBLOB *blob;
    6573             : 
    6574          56 :         tdsdump_log(TDS_DBG_FUNC, "dbtxtimestamp(%p, %d)\n", dbproc, column);
    6575             : 
    6576          56 :         colinfo = dbcolptr(dbproc, column);
    6577          56 :         if (!colinfo || !is_blob_col(colinfo))
    6578             :                 return NULL;
    6579             : 
    6580          56 :         blob = (TDSBLOB *) colinfo->column_data;
    6581             : 
    6582             :         /* test if valid */
    6583          56 :         if (!blob->valid_ptr)
    6584             :                 return NULL;
    6585             : 
    6586          48 :         return (DBBINARY *) blob->timestamp;
    6587             : }
    6588             : 
    6589             : /**
    6590             :  * \ingroup dblib_core
    6591             :  * \brief Get text pointer for a column in the current row.
    6592             :  * 
    6593             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6594             :  * \param column number of the column in the \c SELECT statement, starting at 1.
    6595             :  * \return text pointer for \a column, may be NULL.
    6596             :  * \sa dbtxtimestamp(), dbwritetext(). 
    6597             :  */
    6598             : DBBINARY *
    6599          56 : dbtxptr(DBPROCESS * dbproc, int column)
    6600             : {
    6601             :         TDSCOLUMN *colinfo;
    6602             :         TDSBLOB *blob;
    6603             : 
    6604          56 :         tdsdump_log(TDS_DBG_FUNC, "dbtxptr(%p, %d)\n", dbproc, column);
    6605             : 
    6606          56 :         colinfo = dbcolptr(dbproc, column);
    6607          56 :         if (!colinfo || !is_blob_col(colinfo))
    6608             :                 return NULL;
    6609             : 
    6610          56 :         blob = (TDSBLOB *) colinfo->column_data;
    6611             : 
    6612             :         /* test if valid */
    6613          56 :         if (!blob->valid_ptr)
    6614             :                 return NULL;
    6615             : 
    6616          48 :         return (DBBINARY *) blob->textptr;
    6617             : }
    6618             : 
    6619             : /**
    6620             :  * \ingroup dblib_core
    6621             :  * \brief Send text or image data to the server.
    6622             :  * 
    6623             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6624             :  * \param objname table name
    6625             :  * \param textptr text pointer to be modified, obtained from dbtxptr(). 
    6626             :  * \param textptrlen \em Ignored.  Supposed to be \c DBTXPLEN.
    6627             :  * \param timestamp text timestamp to be modified, obtained from dbtxtimestamp() or dbtxtsnewval(), may be \c NULL.
    6628             :  * \param log \c TRUE if the operation is to be recorded in the transaction log.
    6629             :  * \param size overall size of the data (in total, not just for this call), in bytes.  A guideline, must not overstate the case.  
    6630             :  * \param text the chunk of data to write.  
    6631             :  * \retval SUCCEED everything worked.
    6632             :  * \retval FAIL not sent, possibly because \a timestamp is invalid or was changed in the database since it was fetched.  
    6633             :  * \sa dbmoretext(), dbtxptr(), dbtxtimestamp(), dbwritetext(), dbtxtsput().  
    6634             :  */
    6635             : RETCODE
    6636          48 : dbwritetext(DBPROCESS * dbproc, char *objname, DBBINARY * textptr, DBTINYINT textptrlen, DBBINARY * timestamp, DBBOOL log,
    6637             :             DBINT size, BYTE * text)
    6638             : {
    6639             :         char textptr_string[35];        /* 16 * 2 + 2 (0x) + 1 */
    6640             :         char timestamp_string[19];      /* 8 * 2 + 2 (0x) + 1 */
    6641             :         TDS_INT result_type;
    6642             : 
    6643          48 :         tdsdump_log(TDS_DBG_FUNC, "dbwritetext(%p, %s, %p, %d, %p, %d)\n", 
    6644             :                                   dbproc, objname, textptr, textptrlen, timestamp, log);
    6645          48 :         CHECK_CONN(FAIL);
    6646          48 :         CHECK_NULP(objname, "dbwritetext", 2, FAIL);
    6647          48 :         CHECK_NULP(textptr, "dbwritetext", 3, FAIL);
    6648          48 :         CHECK_NULP(timestamp, "dbwritetext", 5, FAIL);
    6649          48 :         CHECK_PARAMETER(size, SYBEZTXT, FAIL);
    6650             : 
    6651             :         if (IS_TDSDEAD(dbproc->tds_socket))
    6652             :                 return FAIL;
    6653             : 
    6654          48 :         if (textptrlen > DBTXPLEN)
    6655             :                 return FAIL;
    6656             : 
    6657          48 :         dbconvert(dbproc, SYBBINARY, (BYTE *) textptr, textptrlen, SYBCHAR, (BYTE *) textptr_string, -1);
    6658          48 :         dbconvert(dbproc, SYBBINARY, (BYTE *) timestamp, 8, SYBCHAR, (BYTE *) timestamp_string, -1);
    6659             : 
    6660          48 :         dbproc->dbresults_state = _DB_RES_INIT;
    6661             : 
    6662          48 :         if (dbproc->tds_socket->state == TDS_PENDING) {
    6663           0 :                 const TDSRET ret = tds_process_tokens(dbproc->tds_socket, &result_type, NULL, TDS_TOKEN_TRAILING);
    6664           0 :                 if (ret != TDS_NO_MORE_RESULTS) {
    6665           0 :                         dbperror(dbproc, SYBERPND, 0);
    6666           0 :                         dbproc->command_state = DBCMDSENT;
    6667           0 :                         return FAIL;
    6668             :                 }
    6669             :         }
    6670             :         
    6671          48 :         if (TDS_FAILED(tds_writetext_start(dbproc->tds_socket, objname, 
    6672             :                 textptr_string, timestamp_string, (log == TRUE), size)))
    6673             :                 return FAIL;
    6674             : 
    6675          48 :         if (!text) {
    6676          24 :                 dbproc->text_size = size;
    6677          24 :                 dbproc->text_sent = 0;
    6678          24 :                 return SUCCEED;
    6679             :         }
    6680             : 
    6681          24 :         tds_writetext_continue(dbproc->tds_socket, text, size);
    6682          24 :         tds_writetext_end(dbproc->tds_socket);
    6683          24 :         dbproc->text_sent = 0;
    6684             : 
    6685          24 :         if (dbsqlok(dbproc) == SUCCEED && dbresults(dbproc) == SUCCEED)
    6686             :                 return SUCCEED;
    6687             :         return FAIL;
    6688             : }
    6689             : 
    6690             : /**
    6691             :  * \ingroup dblib_core
    6692             :  * \brief Fetch part of a text or image value from the server.
    6693             :  * 
    6694             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6695             :  * \param buf \em output: buffer into which text will be placed.
    6696             :  * \param bufsize size of \a buf, in bytes. 
    6697             :  * \return 
    6698             :         - \c >0 count of bytes placed in \a buf.
    6699             :         - \c  0 end of row.  
    6700             :         - \c -1 \em error, no result set ready for \a dbproc.
    6701             :         - \c NO_MORE_ROWS all rows read, no further data. 
    6702             :  * \sa dbmoretext(), dbnextrow(), dbwritetext(). 
    6703             :  */
    6704             : STATUS
    6705         816 : dbreadtext(DBPROCESS * dbproc, void *buf, DBINT bufsize)
    6706             : {
    6707             :         TDSSOCKET *tds;
    6708             :         TDSCOLUMN *curcol;
    6709             :         int cpbytes, bytes_avail;
    6710             :         TDS_INT result_type;
    6711             :         TDSRESULTINFO *resinfo;
    6712             : 
    6713         816 :         tdsdump_log(TDS_DBG_FUNC, "dbreadtext(%p, %p, %d)\n", dbproc, buf, bufsize);
    6714         816 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    6715         816 :         CHECK_NULP(buf, "dbreadtext", 2, -1);
    6716             : 
    6717         816 :         tds = dbproc->tds_socket;
    6718             : 
    6719         816 :         if (!tds || !tds->res_info || !tds->res_info->columns[0])
    6720             :                 return -1;
    6721             : 
    6722         816 :         resinfo = tds->res_info;
    6723         816 :         curcol = resinfo->columns[0];
    6724             : 
    6725             :         /*
    6726             :          * if the current position is beyond the end of the text
    6727             :          * set pos to 0 and return 0 to denote the end of the 
    6728             :          * text 
    6729             :          */
    6730         816 :         if (curcol->column_textpos && curcol->column_textpos >= curcol->column_cur_size) {
    6731          48 :                 curcol->column_textpos = 0;
    6732          48 :                 return 0;
    6733             :         }
    6734             : 
    6735             :         /*
    6736             :          * if pos is 0 (first time through or last call exhausted the text)
    6737             :          * then read another row
    6738             :          */
    6739             : 
    6740         768 :         if (curcol->column_textpos == 0) {
    6741          96 :                 const int mask = TDS_STOPAT_ROWFMT|TDS_STOPAT_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE;
    6742          96 :                 buffer_save_row(dbproc);
    6743          96 :                 switch (tds_process_tokens(dbproc->tds_socket, &result_type, NULL, mask)) {
    6744          96 :                 case TDS_SUCCESS:
    6745          96 :                         if (result_type == TDS_ROW_RESULT || result_type == TDS_COMPUTE_RESULT)
    6746             :                                 break;
    6747             :                 case TDS_NO_MORE_RESULTS:
    6748             :                         return NO_MORE_ROWS;
    6749           0 :                 default:
    6750           0 :                         return -1;
    6751             :                 }
    6752         720 :         }
    6753             : 
    6754             :         /* find the number of bytes to return */
    6755         720 :         bytes_avail = MAX(curcol->column_cur_size, 0) - curcol->column_textpos;
    6756         720 :         cpbytes = bytes_avail > bufsize ? bufsize : bytes_avail;
    6757         720 :         memcpy(buf, &((TDSBLOB *) curcol->column_data)->textvalue[curcol->column_textpos], cpbytes);
    6758         720 :         curcol->column_textpos += cpbytes;
    6759         720 :         return cpbytes;
    6760             : }
    6761             : 
    6762             : /**
    6763             :  * \ingroup dblib_core
    6764             :  * \brief Send chunk of a text/image value to the server.
    6765             :  * 
    6766             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6767             :  * \param size count of bytes to send.  
    6768             :  * \param text textpointer, obtained from dbtxptr.
    6769             :  * \retval SUCCEED always.
    6770             :  * \sa dbtxptr(), dbtxtimestamp(), dbwritetext(). 
    6771             :  * \todo Check return value of called functions and return \c FAIL if appropriate. 
    6772             :  */
    6773             : RETCODE
    6774         360 : dbmoretext(DBPROCESS * dbproc, DBINT size, const BYTE text[])
    6775             : {
    6776         360 :         tdsdump_log(TDS_DBG_FUNC, "dbmoretext(%p, %d, %p)\n", dbproc, size, text);
    6777         360 :         CHECK_CONN(FAIL);
    6778         360 :         CHECK_NULP(text, "dbmoretext", 3, FAIL);
    6779             : 
    6780         360 :         assert(dbproc->text_size >= dbproc->text_sent);
    6781             : 
    6782             :         /* TODO this test should be inside tds_writetext_continue, currently not */
    6783         360 :         if (size < 0 || size > dbproc->text_size - dbproc->text_sent)
    6784             :                 return FAIL;
    6785             : 
    6786         360 :         if (size) {
    6787         360 :                 if (TDS_FAILED(tds_writetext_continue(dbproc->tds_socket, text, size)))
    6788             :                         return FAIL;
    6789         360 :                 dbproc->text_sent += size;
    6790             : 
    6791         360 :                 if (dbproc->text_sent == dbproc->text_size) {
    6792          24 :                         tds_writetext_end(dbproc->tds_socket);
    6793          24 :                         dbproc->text_sent = 0;
    6794             :                 }
    6795             :         }
    6796             : 
    6797             :         return SUCCEED;
    6798             : }
    6799             : 
    6800             : /**
    6801             :  * \ingroup dblib_core
    6802             :  * \brief Record to a file all SQL commands sent to the server
    6803             :  * 
    6804             :  * \param filename name of file to write to. 
    6805             :  * \remarks Files are named \em filename.n, where n is an integer, starting with 0, and incremented with each callto dbopen().  
    6806             :  * \sa dbopen(), TDSDUMP environment variable(). 
    6807             :  */
    6808             : void
    6809           0 : dbrecftos(const char filename[])
    6810             : {
    6811             :         char *f;
    6812             : 
    6813           0 :         tdsdump_log(TDS_DBG_FUNC, "dbrecftos(%s)\n", filename);
    6814           0 :         if (filename == NULL) { 
    6815           0 :                 dbperror(NULL, SYBENULP, 0); 
    6816           0 :                 return;
    6817             :         }
    6818             :         
    6819           0 :         f = strdup(filename);
    6820           0 :         if (!f) {
    6821           0 :                 dbperror(NULL, SYBEMEM, 0); 
    6822           0 :                 return;
    6823             :         }
    6824             :         
    6825           0 :         tds_mutex_lock(&dblib_mutex);
    6826           0 :         free(g_dblib_ctx.recftos_filename);
    6827           0 :         g_dblib_ctx.recftos_filename = f;
    6828           0 :         g_dblib_ctx.recftos_filenum = 0;
    6829           0 :         tds_mutex_unlock(&dblib_mutex);
    6830             : }
    6831             : 
    6832             : /** \internal
    6833             :  * \ingroup dblib_internal
    6834             :  * \brief Get the TDS version in use for \a dbproc.  
    6835             :  * 
    6836             :  * 
    6837             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6838             :  * \return a \c DBTDS* token.  
    6839             :  * \remarks The integer values of the constants are counterintuitive.  
    6840             :  * \sa DBTDS().
    6841             :  */
    6842             : int
    6843         452 : dbtds(DBPROCESS * dbproc)
    6844             : {
    6845         452 :         tdsdump_log(TDS_DBG_FUNC, "dbtds(%p)\n", dbproc);
    6846         452 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    6847             : 
    6848         452 :         if (dbproc->tds_socket) {
    6849         452 :                 switch (dbproc->tds_socket->conn->tds_version) {
    6850             :                 case 0x402:
    6851             :                         return DBTDS_4_2;
    6852           0 :                 case 0x406:
    6853           0 :                         return DBTDS_4_6;
    6854          74 :                 case 0x500:
    6855          74 :                         return DBTDS_5_0;
    6856           0 :                 case 0x700:
    6857           0 :                         return DBTDS_7_0;
    6858         176 :                 case 0x701:
    6859         176 :                         return DBTDS_7_1;
    6860           0 :                 case 0x702:
    6861           0 :                         return DBTDS_7_2;
    6862         100 :                 case 0x703:
    6863         100 :                         return DBTDS_7_3;
    6864         102 :                 case 0x704:
    6865         102 :                         return DBTDS_7_4;
    6866           0 :                 case 0x800:
    6867           0 :                         return DBTDS_8_0_;
    6868           0 :                 default:
    6869           0 :                         return DBTDS_UNKNOWN;
    6870             :                 }
    6871             :         }
    6872             :         return -1;
    6873             : }
    6874             : 
    6875             : /**
    6876             :  * \ingroup dblib_core
    6877             :  * \brief See which version of db-lib is in use.
    6878             :  * 
    6879             :  * \return null-terminated ASCII string representing the version of db-lib.  
    6880             :  * \remarks FreeTDS returns the CVS version string of dblib.c.  
    6881             :  * \sa 
    6882             :  */
    6883             : const char *
    6884           0 : dbversion(void)
    6885             : {
    6886           0 :         tdsdump_log(TDS_DBG_FUNC, "dbversion(void)\n");
    6887           0 :         return TDS_VERSION_NO;
    6888             : }
    6889             : 
    6890             : #if defined(DBLIB_UNIMPLEMENTED)
    6891             : /**
    6892             :  * \ingroup dblib_core
    6893             :  * \brief Set the default character set.  
    6894             :  * 
    6895             :  * \param charset null-terminated ASCII string, matching a row in master..syscharsets.  
    6896             :  * \sa dbsetdeflang(), dbsetdefcharset(), dblogin(), dbopen(). 
    6897             :  * \todo Unimplemented.
    6898             :  */
    6899             : RETCODE
    6900             : dbsetdefcharset(char *charset)
    6901             : {
    6902             :         tdsdump_log(TDS_DBG_FUNC, "dbsetdefcharset(%s)\n", charset);
    6903             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetdefcharset()\n");
    6904             :         return SUCCEED;
    6905             : }
    6906             : 
    6907             : /**
    6908             :  * \ingroup dblib_core
    6909             :  * \brief Ready execution of a registered procedure.
    6910             :  * 
    6911             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6912             :  * \param procedure_name to call.
    6913             :  * \param namelen size of \a procedure_name, in bytes.
    6914             :  * \sa dbregparam(), dbregexec(), dbregwatch(), dbreglist(), dbregwatchlist
    6915             :  * \todo Unimplemented.
    6916             :  */
    6917             : RETCODE
    6918             : dbreginit(DBPROCESS * dbproc, DBCHAR * procedure_name, DBSMALLINT namelen)
    6919             : {
    6920             :         tdsdump_log(TDS_DBG_FUNC, "dbreginit(%p, %s, %d)\n", dbproc, procedure_name, namelen);
    6921             :         CHECK_CONN(FAIL);
    6922             :         CHECK_NULP(procedure_name, "dbreginit", 2, FAIL);
    6923             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbreginit()\n");
    6924             :         return SUCCEED;
    6925             : }
    6926             : 
    6927             : 
    6928             : /**
    6929             :  * \ingroup dblib_core
    6930             :  * \brief Get names of Open Server registered procedures.
    6931             :  * 
    6932             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6933             :  * \sa dbregparam(), dbregexec(), dbregwatch(), dbreglist(), dbregwatchlist(). 
    6934             :  * \todo Unimplemented.
    6935             :  */
    6936             : RETCODE
    6937             : dbreglist(DBPROCESS * dbproc)
    6938             : {
    6939             :         tdsdump_log(TDS_DBG_FUNC, "dbreglist(%p)\n", dbproc);
    6940             :         CHECK_CONN(FAIL);
    6941             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbreglist()\n");
    6942             :         return SUCCEED;
    6943             : }
    6944             : 
    6945             : 
    6946             : /**
    6947             :  * \ingroup dblib_core
    6948             :  * \brief  Describe parameter of registered procedure .
    6949             :  * 
    6950             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6951             :  * \param param_name
    6952             :  * \param type \c SYB* datatype. 
    6953             :  * \param datalen size of \a data.
    6954             :  * \param data address of buffer holding value for the parameter.  
    6955             :  * \sa dbreginit(), dbregexec(), dbnpdefine(), dbnpcreate(), dbregwatch(). 
    6956             :  * \todo Unimplemented.
    6957             :  */
    6958             : RETCODE
    6959             : dbregparam(DBPROCESS * dbproc, char *param_name, int type, DBINT datalen, BYTE * data)
    6960             : {
    6961             :         tdsdump_log(TDS_DBG_FUNC, "dbregparam(%p, %s, %d, %d, %p)\n", dbproc, param_name, type, datalen, data);
    6962             :         CHECK_CONN(FAIL);
    6963             :         CHECK_NULP(param_name, "dbregparam", 2, FAIL);
    6964             :         CHECK_NULP(data, "dbregparam", 5, FAIL);
    6965             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbregparam()\n");
    6966             :         return SUCCEED;
    6967             : }
    6968             : 
    6969             : 
    6970             : /**
    6971             :  * \ingroup dblib_core
    6972             :  * \brief Execute a registered procedure.
    6973             :  * 
    6974             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6975             :  * \param options
    6976             :  * \sa dbreginit(), dbregparam(), dbregwatch(), dbregnowatch
    6977             :  * \todo Unimplemented.
    6978             :  */
    6979             : RETCODE
    6980             : dbregexec(DBPROCESS * dbproc, DBUSMALLINT options)
    6981             : {
    6982             :         tdsdump_log(TDS_DBG_FUNC, "dbregexec(%p, %d)\n", dbproc, options);
    6983             :         CHECK_CONN(FAIL);
    6984             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbregexec()\n");
    6985             :         return SUCCEED;
    6986             : }
    6987             : #endif
    6988             : 
    6989             : 
    6990             : /**
    6991             :  * \ingroup dblib_datetime
    6992             :  * \brief Get name of a month, in some human language.
    6993             :  * 
    6994             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    6995             :  * \param language \em ignored.
    6996             :  * \param monthnum number of the month, starting with 1.
    6997             :  * \param shortform set to \c TRUE for a three letter output ("Jan" - "Dec"), else zero.  
    6998             :  * \return address of null-terminated ASCII string, or \c NULL on error. 
    6999             :  * \sa db12hour(), dbdateorder(), dbdayname(), DBSETLNATLANG(), dbsetopt().  
    7000             :  */
    7001             : const char *
    7002           0 : dbmonthname(DBPROCESS * dbproc, char *language, int monthnum, DBBOOL shortform)
    7003             : {
    7004             :         static const char shortmon[][4] = {
    7005             :                 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    7006             :         };
    7007             :         static const char longmon[][12] = {
    7008             :                 "January", "February", "March", "April", "May", "June",
    7009             :                 "July", "August", "September", "October", "November", "December"
    7010             :         };
    7011             : 
    7012           0 :         tdsdump_log(TDS_DBG_FUNC, "dbmonthname(%p, %s, %d, %d)\n", dbproc, language, monthnum, shortform);
    7013           0 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    7014           0 :         CHECK_NULP(language, "dbmonthname", 2, NULL);
    7015             : 
    7016           0 :         if (monthnum < 1 || monthnum > 12)
    7017             :                 return NULL;
    7018           0 :         return (shortform) ? shortmon[monthnum - 1] : longmon[monthnum - 1];
    7019             : }
    7020             : 
    7021             : /**
    7022             :  * \ingroup dblib_core
    7023             :  * \brief See if a command caused the current database to change.
    7024             :  * 
    7025             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7026             :  * \return name of new database, if changed, as a null-terminated ASCII string, else \c NULL.
    7027             : 
    7028             :  * \sa dbname(), dbresults(), dbsqlexec(), dbsqlsend(), dbuse().  
    7029             :  */
    7030             : char *
    7031           0 : dbchange(DBPROCESS * dbproc)
    7032             : {
    7033           0 :         tdsdump_log(TDS_DBG_FUNC, "dbchange(%p)\n", dbproc);
    7034           0 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    7035             : 
    7036           0 :         if (dbproc->envchange_rcv & (1 << (TDS_ENV_DATABASE - 1))) {
    7037           0 :                 return dbproc->dbcurdb;
    7038             :         }
    7039             :         return NULL;
    7040             : }
    7041             : 
    7042             : /**
    7043             :  * \ingroup dblib_core
    7044             :  * \brief Get name of current database.
    7045             :  * 
    7046             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7047             :  * \return current database name, as null-terminated ASCII string.
    7048             :  * \sa dbchange(), dbuse().  
    7049             :  */
    7050             : char *
    7051           0 : dbname(DBPROCESS * dbproc)
    7052             : {
    7053           0 :         tdsdump_log(TDS_DBG_FUNC, "dbname(%p)\n", dbproc);
    7054           0 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    7055           0 :         return dbproc->dbcurdb;
    7056             : }
    7057             : 
    7058             : /**
    7059             :  * \ingroup dblib_core
    7060             :  * \brief Get \c syscharset name of the server character set.
    7061             :  * 
    7062             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7063             :  * \return name of server's charset, as null-terminated ASCII string.
    7064             :  * \sa dbcharsetconv(), dbgetcharset(), DBSETLCHARSET().  
    7065             :  */
    7066             : char *
    7067           0 : dbservcharset(DBPROCESS * dbproc)
    7068             : {
    7069             : 
    7070           0 :         tdsdump_log(TDS_DBG_FUNC, "dbservcharset(%p)\n", dbproc);
    7071           0 :         CHECK_PARAMETER(dbproc, SYBENULL, NULL);
    7072             : 
    7073           0 :         return dbproc->servcharset;
    7074             : }
    7075             : 
    7076             : /**
    7077             :  * \ingroup dblib_core
    7078             :  * \brief Transmit the command buffer to the server.  \em Non-blocking, does not wait for a response.
    7079             :  * 
    7080             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7081             :  * \retval SUCCEED SQL sent.
    7082             :  * \retval FAIL protocol problem, unless dbsqlsend() when it's not supposed to be (in which case a db-lib error
    7083             :  message will be emitted).  
    7084             :  * \sa dbcmd(), dbfcmd(), DBIORDESC(), DBIOWDESC(), dbnextrow(), dbpoll(), dbresults(), dbsettime(), dbsqlexec(), dbsqlok().  
    7085             :  */
    7086             : RETCODE
    7087       20372 : dbsqlsend(DBPROCESS * dbproc)
    7088             : {
    7089             :         TDSSOCKET *tds;
    7090             :         char *cmdstr;
    7091             :         TDSRET rc;
    7092             :         TDS_INT result_type;
    7093             :         char timestr[256];
    7094             : 
    7095       20372 :         tdsdump_log(TDS_DBG_FUNC, "dbsqlsend(%p)\n", dbproc);
    7096       20372 :         CHECK_CONN(FAIL);
    7097             : 
    7098       20372 :         tds = dbproc->tds_socket;
    7099             : 
    7100       20372 :         if (tds->state == TDS_PENDING) {
    7101             : 
    7102         130 :                 if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_TRAILING) != TDS_NO_MORE_RESULTS) {
    7103          70 :                         dbperror(dbproc, SYBERPND, 0);
    7104          70 :                         dbproc->command_state = DBCMDSENT;
    7105          70 :                         return FAIL;
    7106             :                 }
    7107             :         }
    7108             : 
    7109       20302 :         if (dbproc->dboptcmd) {
    7110           0 :                 if ((cmdstr = dbstring_get(dbproc->dboptcmd)) == NULL) {
    7111           0 :                         dbperror(dbproc, SYBEASEC, 0); /* Attempt to send an empty command buffer to the server */
    7112           0 :                         return FAIL;
    7113             :                 }
    7114           0 :                 rc = tds_submit_query(dbproc->tds_socket, cmdstr);
    7115           0 :                 free(cmdstr);
    7116           0 :                 dbstring_free(&(dbproc->dboptcmd));
    7117           0 :                 if (TDS_FAILED(rc)) {
    7118             :                         return FAIL;
    7119             :                 }
    7120           0 :                 dbproc->avail_flag = FALSE;
    7121           0 :                 dbproc->envchange_rcv = 0;
    7122           0 :                 dbproc->dbresults_state = _DB_RES_INIT;
    7123           0 :                 while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS))
    7124             :                        == TDS_SUCCESS);
    7125           0 :                 if (rc != TDS_NO_MORE_RESULTS) {
    7126             :                         return FAIL;
    7127             :                 }
    7128             :         }
    7129       20302 :         dbproc->more_results = TRUE;
    7130             : 
    7131       20302 :         if (dbproc->ftos != NULL) {
    7132           0 :                 fprintf(dbproc->ftos, "%s\n", dbproc->dbbuf);
    7133           0 :                 fprintf(dbproc->ftos, "go /* %s */\n", _dbprdate(timestr));
    7134           0 :                 fflush(dbproc->ftos);
    7135             :         }
    7136             : 
    7137       20302 :         if (TDS_FAILED(tds_submit_query(dbproc->tds_socket, (char *) dbproc->dbbuf))) {
    7138             :                 return FAIL;
    7139             :         }
    7140       20292 :         dbproc->avail_flag = FALSE;
    7141       20292 :         dbproc->envchange_rcv = 0;
    7142       20292 :         dbproc->dbresults_state = _DB_RES_INIT;
    7143       20292 :         dbproc->command_state = DBCMDSENT;
    7144       20292 :         return SUCCEED;
    7145             : }
    7146             : 
    7147             : /**
    7148             :  * \ingroup dblib_core
    7149             :  * \brief Get user-defined datatype of a compute column.
    7150             :  * 
    7151             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7152             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    7153             :  * \param column Nth column in \a computeid, starting from 1.
    7154             :  * \returns user-defined datatype of compute column, else -1.
    7155             :  * \sa dbalttype(), dbcolutype().
    7156             :  */
    7157             : DBINT
    7158           0 : dbaltutype(DBPROCESS * dbproc, int computeid, int column)
    7159             : {
    7160             :         TDSCOLUMN *colinfo;
    7161             : 
    7162           0 :         tdsdump_log(TDS_DBG_FUNC, "dbaltutype(%p, %d, %d)\n", dbproc, computeid, column);
    7163             : 
    7164           0 :         colinfo = dbacolptr(dbproc, computeid, column, false);
    7165           0 :         if (!colinfo)
    7166             :                 return -1;
    7167             : 
    7168           0 :         return colinfo->column_usertype;
    7169             : }
    7170             : 
    7171             : /**
    7172             :  * \ingroup dblib_core
    7173             :  * \brief Get size of data in compute column.
    7174             :  * 
    7175             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7176             :  * \param computeid of \c COMPUTE clause to which we're referring. 
    7177             :  * \param column Nth column in \a computeid, starting from 1.
    7178             :  * \sa dbadata(), dbadlen(), dbalttype(), dbgetrow(), dbnextrow(), dbnumalts().
    7179             :  */
    7180             : DBINT
    7181           0 : dbaltlen(DBPROCESS * dbproc, int computeid, int column)
    7182             : {
    7183             :         TDSCOLUMN *colinfo;
    7184             : 
    7185           0 :         tdsdump_log(TDS_DBG_FUNC, "dbaltlen(%p, %d, %d)\n", dbproc, computeid, column);
    7186             : 
    7187           0 :         colinfo = dbacolptr(dbproc, computeid, column, false);
    7188           0 :         if (!colinfo)
    7189             :                 return -1;
    7190             : 
    7191           0 :         return colinfo->column_size;
    7192             : 
    7193             : }
    7194             : 
    7195             : /**
    7196             :  * \ingroup dblib_core
    7197             :  * \brief See if a server response has arrived.
    7198             :  * 
    7199             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7200             :  * \param milliseconds how long to wait for the server before returning:
    7201             :         - \c  0 return immediately.
    7202             :         - \c -1 do not return until the server responds or a system interrupt occurs.
    7203             :  * \param ready_dbproc \em output: DBPROCESS for which a response arrived, of \c NULL. 
    7204             :  * \param return_reason \em output: 
    7205             :         - \c DBRESULT server responded.
    7206             :         - \c DBNOTIFICATION registered procedure notification has arrived. dbpoll() the registered handler, if
    7207             :                 any, before it returns.
    7208             :         - \c DBTIMEOUT \a milliseconds elapsed before the server responded.
    7209             :         - \c DBINTERRUPT operating-system interrupt occurred before the server responded.
    7210             :  * \retval SUCCEED everything worked.
    7211             :  * \retval FAIL a server connection died.
    7212             :  * \sa  DBIORDESC(), DBRBUF(), dbresults(), dbreghandle(), dbsqlok(). 
    7213             :  * \todo Unimplemented.
    7214             :  */
    7215             : #if defined(DBLIB_UNIMPLEMENTED)
    7216             : RETCODE
    7217             : dbpoll(DBPROCESS * dbproc, long milliseconds, DBPROCESS ** ready_dbproc, int *return_reason)
    7218             : {
    7219             :         tdsdump_log(TDS_DBG_FUNC, "dbpoll(%p, %ld, %p, %p)\n", dbproc, milliseconds, ready_dbproc, return_reason);
    7220             :         CHECK_CONN(FAIL);
    7221             :         CHECK_NULP(ready_dbproc, "dbpoll", 3, FAIL);
    7222             :         CHECK_NULP(return_reason, "dbpoll", 4, FAIL);
    7223             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbpoll()\n");
    7224             :         return SUCCEED;
    7225             : }
    7226             : #endif
    7227             : 
    7228             : /** \internal
    7229             :  * \ingroup dblib_internal
    7230             :  * \brief Get number of the first row in the row buffer.
    7231             :  * 
    7232             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7233             :  * \sa DBFIRSTROW(), dbclrbuf(), DBCURROW(), dbgetrow(), DBLASTROW(), dbnextrow(), dbsetopt(). 
    7234             :  */
    7235             : DBINT
    7236           0 : dbfirstrow(DBPROCESS * dbproc)
    7237             : {
    7238           0 :         tdsdump_log(TDS_DBG_FUNC, "dbfirstrow(%p)\n", dbproc);
    7239           0 :         CHECK_CONN(0);
    7240           0 :         return buffer_idx2row(&dbproc->row_buf, dbproc->row_buf.tail);
    7241             : }
    7242             : 
    7243             : /** \internal
    7244             :  * \ingroup dblib_internal
    7245             :  * \brief Get number of the last row in the row buffer.
    7246             :  * 
    7247             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7248             :  * \sa DBLASTROW(), dbclrbuf(), DBCURROW(), DBFIRSTROW(), dbgetrow(), dbnextrow(), dbsetopt(). 
    7249             :  */
    7250             : DBINT
    7251           0 : dblastrow(DBPROCESS * dbproc)
    7252             : {
    7253             :         int idx;
    7254             : 
    7255           0 :         tdsdump_log(TDS_DBG_FUNC, "dblastrow(%p)\n", dbproc);
    7256           0 :         CHECK_PARAMETER(dbproc, SYBENULL, 0);
    7257           0 :         idx = dbproc->row_buf.head;
    7258           0 :         if (dbproc->row_buf.head != dbproc->row_buf.tail) {
    7259           0 :                 if (--idx < 0) 
    7260           0 :                         idx = dbproc->row_buf.capacity - 1;
    7261             :         }
    7262           0 :         assert(idx >= 0);
    7263           0 :         return buffer_idx2row(&dbproc->row_buf, idx);
    7264             : }
    7265             : 
    7266             : 
    7267             : /** \internal
    7268             :  * \ingroup dblib_internal
    7269             :  * \brief Get file descriptor of the socket used by a \c DBPROCESS to read data coming from the server. (!)
    7270             :  * 
    7271             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7272             :  * \sa dbcmd(), DBIORDESC(), DBIOWDESC(), dbnextrow(), dbpoll(), DBRBUF(), dbresults(), dbsqlok(), dbsqlsend().  
    7273             :  */
    7274             : int
    7275           0 : dbiordesc(DBPROCESS * dbproc)
    7276             : {
    7277           0 :         tdsdump_log(TDS_DBG_FUNC, "dbiordesc(%p)\n", dbproc);
    7278           0 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    7279           0 :         return (int) tds_get_s(dbproc->tds_socket);
    7280             : }
    7281             : 
    7282             : 
    7283             : /** \internal
    7284             :  * \ingroup dblib_internal
    7285             :  * \brief Get file descriptor of the socket used by a \c DBPROCESS to write data coming to the server. (!)
    7286             :  * 
    7287             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7288             :  * \sa dbcmd(), DBIORDESC(), DBIOWDESC(), dbnextrow(), dbpoll(), DBRBUF(), dbresults(), dbsqlok(), dbsqlsend().  
    7289             :  */
    7290             : int
    7291          20 : dbiowdesc(DBPROCESS * dbproc)
    7292             : {
    7293          20 :         tdsdump_log(TDS_DBG_FUNC, "dbiowdesc(%p)\n", dbproc);
    7294          20 :         CHECK_PARAMETER(dbproc, SYBENULL, -1);
    7295             : 
    7296          20 :         return (int) tds_get_s(dbproc->tds_socket);
    7297             : }
    7298             : 
    7299             : DBBOOL
    7300           0 : dbisavail(DBPROCESS * dbproc)
    7301             : {
    7302           0 :         tdsdump_log(TDS_DBG_FUNC, "dbisavail(%p)\n", dbproc);
    7303           0 :         CHECK_PARAMETER(dbproc, SYBENULL, FALSE);
    7304           0 :         return dbproc->avail_flag;
    7305             : }
    7306             : 
    7307             : 
    7308             : /** \internal
    7309             :  * \ingroup dblib_internal
    7310             :  * \brief Mark a \c DBPROCESS as "available".
    7311             :  * 
    7312             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7313             :  * \remarks Basically bogus.  \c FreeTDS behaves the way Sybase's implementation does, but so what?  
    7314             :         Many \c db-lib functions set the \c DBPROCESS to "not available", but only 
    7315             :         dbsetavail() resets it to "available".  
    7316             :  * \sa DBISAVAIL(). DBSETAVAIL().
    7317             :  */
    7318             : void
    7319           0 : dbsetavail(DBPROCESS * dbproc)
    7320             : {
    7321           0 :         tdsdump_log(TDS_DBG_FUNC, "dbsetavail(%p)\n", dbproc);
    7322           0 :         CHECK_PARAMETER(dbproc, SYBENULL, );
    7323           0 :         dbproc->avail_flag = TRUE;
    7324             : }
    7325             : 
    7326             : 
    7327             : /**
    7328             :  * \ingroup dblib_core
    7329             :  * \brief Build a printable string from text containing placeholders for variables.
    7330             :  * 
    7331             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    7332             :  * \param charbuf \em output: buffer that will contain the ASCII null-terminated string built by \c dbstrbuild().
    7333             :  * \param bufsize size of \a charbuf, in bytes. 
    7334             :  * \param text null-terminated ASCII string, with \em placeholders for variables. \em A Placeholder is a
    7335             :  *      three-byte string, made up of: 
    7336             :         - '%' a percent sign
    7337             :         - 0-9 an integer (designates the argument number to use, starting with 1.)
    7338             :         - '!' an exclamation point
    7339             :  * \param formats null-terminated ASCII sprintf-style string.  Has one format specifier for each placeholder in \a text.
    7340             :  * \remarks Following \a formats are the arguments, the values to substitute for the placeholders.  
    7341             :  * \sa dbconvert(), dbdatename(), dbdatepart().  
    7342             :  */
    7343             : RETCODE
    7344           0 : dbstrbuild(DBPROCESS * dbproc, char *charbuf, int bufsize, char *text, char *formats, ...)
    7345             : {
    7346             :         va_list ap;
    7347             :         TDSRET rc;
    7348             :         int resultlen;
    7349             : 
    7350           0 :         tdsdump_log(TDS_DBG_FUNC, "dbstrbuild(%p, %s, %d, %s, %s, ...)\n", dbproc, charbuf, bufsize, text, formats);
    7351           0 :         CHECK_NULP(charbuf, "dbstrbuild", 2, FAIL);
    7352           0 :         CHECK_NULP(text, "dbstrbuild", 4, FAIL);
    7353           0 :         CHECK_NULP(formats, "dbstrbuild", 5, FAIL);
    7354             : 
    7355           0 :         va_start(ap, formats);
    7356           0 :         rc = tds_vstrbuild(charbuf, bufsize, &resultlen, text, TDS_NULLTERM, formats, TDS_NULLTERM, ap);
    7357           0 :         charbuf[resultlen] = '\0';
    7358           0 :         va_end(ap);
    7359           0 :         return TDS_SUCCEED(rc) ? SUCCEED : FAIL;
    7360             : }
    7361             : 
    7362             : static char *
    7363           0 : _dbprdate(char *timestr)
    7364             : {
    7365           0 :         time_t currtime = time(NULL);
    7366             :         
    7367           0 :         assert(timestr);
    7368             : 
    7369           0 :         strcpy(timestr, asctime(gmtime(&currtime)));
    7370           0 :         timestr[strlen(timestr) - 1] = '\0';    /* remove newline */
    7371           0 :         return timestr;
    7372             : 
    7373             : }
    7374             : 
    7375             : static DBINT
    7376           0 : _dbnullable(DBPROCESS * dbproc, int column)
    7377             : {
    7378             :         TDSCOLUMN *colinfo;
    7379             :         TDSRESULTINFO *resinfo;
    7380             : 
    7381           0 :         assert(dbproc && dbproc->tds_socket);
    7382           0 :         resinfo = dbproc->tds_socket->res_info;
    7383           0 :         if (!resinfo || column < 1 || column > resinfo->num_cols)
    7384             :                 return FALSE;
    7385           0 :         colinfo = resinfo->columns[column - 1];
    7386             : 
    7387           0 :         if (colinfo->column_nullable) 
    7388             :                 return TRUE;
    7389           0 :         return FALSE;
    7390             : }
    7391             : 
    7392             : /** Returns type in string. Used for debugging purpose */
    7393             : static const char *
    7394           0 : tds_prdatatype(int datatype_token)
    7395             : {
    7396           0 :         switch ((TDS_SERVER_TYPE) datatype_token) {
    7397             :         case SYBCHAR:           return "SYBCHAR";
    7398           0 :         case SYBVARCHAR:        return "SYBVARCHAR";
    7399           0 :         case SYBINTN:           return "SYBINTN";
    7400           0 :         case SYBINT1:           return "SYBINT1";
    7401           0 :         case SYBINT2:           return "SYBINT2";
    7402           0 :         case SYBINT4:           return "SYBINT4";
    7403           0 :         case SYBINT8:           return "SYBINT8";
    7404           0 :         case SYBFLT8:           return "SYBFLT8";
    7405           0 :         case SYBDATETIME:       return "SYBDATETIME";
    7406           0 :         case SYBBIT:            return "SYBBIT";
    7407           0 :         case SYBTEXT:           return "SYBTEXT";
    7408           0 :         case SYBNTEXT:          return "SYBNTEXT";
    7409           0 :         case SYBIMAGE:          return "SYBIMAGE";
    7410           0 :         case SYBMONEY4:         return "SYBMONEY4";
    7411           0 :         case SYBMONEY:          return "SYBMONEY";
    7412           0 :         case SYBDATETIME4:      return "SYBDATETIME4";
    7413           0 :         case SYBREAL:           return "SYBREAL";
    7414           0 :         case SYBBINARY:         return "SYBBINARY";
    7415           0 :         case SYBVOID:           return "SYBVOID";
    7416           0 :         case SYBVARBINARY:      return "SYBVARBINARY";
    7417           0 :         case SYBNVARCHAR:       return "SYBNVARCHAR";
    7418           0 :         case SYBBITN:           return "SYBBITN";
    7419           0 :         case SYBNUMERIC:        return "SYBNUMERIC";
    7420           0 :         case SYBDECIMAL:        return "SYBDECIMAL";
    7421           0 :         case SYBFLTN:           return "SYBFLTN";
    7422           0 :         case SYBMONEYN:         return "SYBMONEYN";
    7423           0 :         case SYBDATETIMN:       return "SYBDATETIMN";
    7424           0 :         case XSYBCHAR:          return "XSYBCHAR";
    7425           0 :         case XSYBVARCHAR:       return "XSYBVARCHAR";
    7426           0 :         case XSYBNVARCHAR:      return "XSYBNVARCHAR";
    7427           0 :         case XSYBNCHAR:         return "XSYBNCHAR";
    7428           0 :         case XSYBVARBINARY:     return "XSYBVARBINARY";
    7429           0 :         case XSYBBINARY:        return "XSYBBINARY";
    7430           0 :         case SYBLONGBINARY:     return "SYBLONGBINARY";
    7431           0 :         case SYBSINT1:          return "SYBSINT1";
    7432           0 :         case SYBUINT2:          return "SYBUINT2";
    7433           0 :         case SYBUINT4:          return "SYBUINT4";
    7434           0 :         case SYBUINT8:          return "SYBUINT8";
    7435           0 :         case SYBUNIQUE:         return "SYBUNIQUE";
    7436           0 :         case SYBVARIANT:        return "SYBVARIANT";
    7437           0 :         case SYBMSXML:          return "SYBMSXML";
    7438           0 :         case SYBMSDATE:         return "SYBMSDATE";
    7439           0 :         case SYBMSTIME:         return "SYBMSTIME";
    7440           0 :         case SYBMSDATETIME2:    return "SYBMSDATETIME2";
    7441           0 :         case SYBMSDATETIMEOFFSET: return "SYBMSDATETIMEOFFSET";
    7442           0 :         case SYBDATE:           return "SYBDATE";
    7443           0 :         case SYBTIME:           return "SYBTIME";
    7444           0 :         case SYB5BIGDATETIME:   return "SYBBIGDATETIME";
    7445           0 :         case SYB5BIGTIME:       return "SYBBIGTIME";
    7446           0 :         case SYBMSUDT:          return "SYBMSUDT";
    7447           0 :         case SYBUINT1:          return "SYBUINT1";
    7448           0 :         case SYBDATEN:          return "SYBDATEN";
    7449           0 :         case SYB5INT8:          return "SYB5INT8";
    7450           0 :         case SYBINTERVAL:       return "SYBINTERVAL";
    7451           0 :         case SYBTIMEN:          return "SYBTIMEN";
    7452           0 :         case SYBUINTN:          return "SYBUINTN";
    7453           0 :         case SYBUNITEXT:        return "SYBUNITEXT";
    7454           0 :         case SYBXML:            return "SYBXML";
    7455           0 :         case SYBMSTABLE:        return "SYBMSTABLE";
    7456             :         }
    7457           0 :         return "(unknown)";
    7458             : }
    7459             : #if 1
    7460             : void
    7461      410744 : copy_data_to_host_var(DBPROCESS * dbproc, TDS_SERVER_TYPE srctype, const BYTE * src, DBINT srclen, 
    7462             :                       BYTE * dest, DBINT destlen,
    7463             :                       int bindtype, DBINT *indicator)
    7464             : {
    7465             :         CONV_RESULT dres;
    7466             :         DBINT ret;
    7467             :         int len;
    7468      410744 :         DBINT indicator_value = 0;
    7469             : 
    7470      410744 :         bool limited_dest_space = false;
    7471      410744 :         TDS_SERVER_TYPE desttype = dblib_bound_type(bindtype);
    7472             : 
    7473      410744 :         tdsdump_log(TDS_DBG_FUNC, "copy_data_to_host_var(%d [%s] len %d => %d [%s] len %d)\n", 
    7474             :                      srctype, tds_prdatatype(srctype), srclen, desttype, tds_prdatatype(desttype), destlen);
    7475      820990 :         CHECK_NULP(src, "copy_data_to_host_var", 3, );
    7476      410744 :         CHECK_NULP(dest, "copy_data_to_host_var", 6, );
    7477      410744 :         if (desttype == TDS_INVALID_TYPE)
    7478             :                 return;
    7479             :         /* indicator can be NULL */
    7480             : 
    7481      410744 :         assert(srclen >= 0);
    7482             : 
    7483      410744 :         if (destlen > 0) {
    7484         290 :                 limited_dest_space = true;
    7485             :         }
    7486             : 
    7487             :         /* oft times we are asked to convert a data type to itself */
    7488             : 
    7489      410744 :         if (desttype == SYBNUMERIC) {
    7490         240 :                 DBNUMERIC *num = NULL;          /* num->scale is unsigned */
    7491             : 
    7492             :                 /* only MS, use always source */
    7493         240 :                 if (bindtype == SRCNUMERICBIND || bindtype == SRCDECIMALBIND) {
    7494         120 :                         if (is_numeric_type(srctype))
    7495             :                                 num = (DBNUMERIC*) src;
    7496             :                         else
    7497          40 :                                 num = (DBNUMERIC*) dest;
    7498         120 :                 } else if (dbproc->msdblib) {
    7499             :                         /* MS by default use only destination information */
    7500             :                         num = (DBNUMERIC*) dest;
    7501             :                 } else {
    7502             :                         /* Sybase, dbbind means source or default */
    7503             :                         /* TODO if dbbind_ps is used is more complicated */
    7504          60 :                         if (is_numeric_type(srctype))
    7505             :                                 num = (DBNUMERIC*) src;
    7506             :                 }
    7507             :                 if (!num) {
    7508          20 :                         dres.n.precision = 18;
    7509          20 :                         dres.n.scale = 0;
    7510             :                 } else {
    7511         220 :                         dres.n.precision = num->precision;
    7512         220 :                         dres.n.scale = num->scale;
    7513             :                 }
    7514      411850 :         } else if ((srctype == desttype) ||
    7515        2434 :                 (is_similar_type(srctype, desttype))) {
    7516             : 
    7517      410246 :                 tdsdump_log(TDS_DBG_INFO1, "copy_data_to_host_var() srctype == desttype\n");
    7518      410246 :                 switch (desttype) {
    7519             : 
    7520          10 :                 case SYBVARBINARY:
    7521             :                 case SYBBINARY:
    7522             :                 case SYBIMAGE:
    7523          10 :                         if (bindtype == VARYBINBIND) {
    7524          10 :                                 DBVARYBIN *bin = (DBVARYBIN*) dest;
    7525          10 :                                 if (limited_dest_space) {
    7526          10 :                                         if (srclen > sizeof(bin->array)) {
    7527           0 :                                                 dbperror(dbproc, SYBECOFL, 0);
    7528           0 :                                                 indicator_value = srclen;
    7529           0 :                                                 srclen = sizeof(bin->array);
    7530             :                                         }
    7531             :                                 }
    7532          10 :                                 memcpy(bin->array, src, srclen);
    7533          10 :                                 bin->len = srclen;
    7534          10 :                                 break;
    7535             :                         }
    7536             : 
    7537           0 :                         if (srclen > destlen && destlen >= 0) {
    7538           0 :                                 dbperror(dbproc, SYBECOFL, 0);
    7539             :                         } else {
    7540           0 :                                 memcpy(dest, src, srclen);
    7541           0 :                                 if (srclen < destlen)
    7542           0 :                                         memset(dest + srclen, 0, destlen - srclen);
    7543             :                         }
    7544             :                         break;
    7545             : 
    7546      205622 :                 case SYBCHAR:
    7547             :                 case SYBVARCHAR:
    7548             :                 case SYBTEXT:
    7549             : 
    7550      205622 :                         switch (bindtype) {
    7551             :                                 case NTBSTRINGBIND: /* strip trailing blanks, null term */
    7552         574 :                                         while (srclen && src[srclen - 1] == ' ') {
    7553         252 :                                                 --srclen;
    7554             :                                         }
    7555         322 :                                         if (limited_dest_space) {
    7556          40 :                                                 if (srclen + 1 > destlen) {
    7557          10 :                                                         dbperror(dbproc, SYBECOFL, 0);
    7558          10 :                                                         indicator_value = srclen + 1;
    7559          10 :                                                         srclen = destlen - 1;
    7560             :                                                 }
    7561             :                                         }
    7562         322 :                                         memcpy(dest, src, srclen);
    7563         322 :                                         dest[srclen] = '\0';
    7564         322 :                                         break;
    7565      205240 :                                 case STRINGBIND:   /* pad with blanks, NUL term */
    7566      205240 :                                         if (limited_dest_space) {
    7567          30 :                                                 if (srclen + 1 > destlen) {
    7568          20 :                                                         dbperror(dbproc, SYBECOFL, 0);
    7569          20 :                                                         indicator_value = srclen + 1;
    7570          20 :                                                         srclen = destlen - 1;
    7571             :                                                 }
    7572             :                                         } else {
    7573      205210 :                                                 destlen = srclen + 1;
    7574             :                                         }
    7575      205240 :                                         memcpy(dest, src, srclen);
    7576      205240 :                                         if (srclen < destlen)
    7577      205240 :                                                 memset(dest + srclen, ' ', destlen - srclen - 1);
    7578      205240 :                                         dest[destlen - 1] = '\0';
    7579      205240 :                                         break;
    7580          40 :                                 case CHARBIND:   /* pad with blanks, NO NUL term */
    7581          40 :                                         if (limited_dest_space) {
    7582          30 :                                                 if (srclen > destlen) {
    7583          10 :                                                         dbperror(dbproc, SYBECOFL, 0);
    7584          10 :                                                         indicator_value = srclen;
    7585          10 :                                                         srclen = destlen;
    7586             :                                                 }
    7587             :                                         } else {
    7588             :                                                 destlen = srclen; 
    7589             :                                         }
    7590          40 :                                         memcpy(dest, src, srclen);
    7591          40 :                                         if (srclen < destlen)
    7592          10 :                                                 memset(dest + srclen, ' ', destlen - srclen);
    7593             :                                         break;
    7594          20 :                                 case VARYCHARBIND: /* strip trailing blanks, NO NUL term */
    7595          20 :                                         if (limited_dest_space) {
    7596          20 :                                                 if (srclen > destlen) {
    7597           0 :                                                         dbperror(dbproc, SYBECOFL, 0);
    7598           0 :                                                         indicator_value = srclen;
    7599           0 :                                                         srclen = destlen;
    7600             :                                                 }
    7601             :                                         }
    7602          20 :                                         memcpy(((DBVARYCHAR *)dest)->str, src, srclen);
    7603          20 :                                         ((DBVARYCHAR *)dest)->len = srclen;
    7604          20 :                                         break;
    7605             :                         } 
    7606             :                         break;
    7607      204614 :                 case SYBINT1:
    7608             :                 case SYBINT2:
    7609             :                 case SYBINT4:
    7610             :                 case SYBINT8:
    7611             :                 case SYBFLT8:
    7612             :                 case SYBREAL:
    7613             :                 case SYBBIT:
    7614             :                 case SYBBITN:
    7615             :                 case SYBMONEY:
    7616             :                 case SYBMONEY4:
    7617             :                 case SYBDATETIME:
    7618             :                 case SYBDATETIME4:
    7619             :                 case SYBDATE:
    7620             :                 case SYBTIME:
    7621             :                 case SYB5BIGDATETIME:
    7622             :                 case SYB5BIGTIME:
    7623             :                 case SYBUNIQUE:
    7624      204614 :                         ret = tds_get_size_by_type(desttype);
    7625      204614 :                         memcpy(dest, src, ret);
    7626      204614 :                         break;
    7627             : 
    7628           0 :                 case SYBMSDATE:
    7629             :                 case SYBMSTIME:
    7630             :                 case SYBMSDATETIME2:
    7631             :                 case SYBMSDATETIMEOFFSET:
    7632           0 :                         ret = sizeof(TDS_DATETIMEALL);
    7633           0 :                         memcpy(dest, src, ret);
    7634           0 :                         break;
    7635             : 
    7636             :                 default:
    7637             :                         break;
    7638             :                 }
    7639      410246 :                 if (indicator)
    7640         136 :                         *indicator = indicator_value;
    7641             : 
    7642             :                 return;
    7643             : 
    7644             :         } /* end srctype == desttype */
    7645             : 
    7646         498 :         len = tds_convert(g_dblib_ctx.tds_ctx, srctype, src, srclen, desttype, &dres);
    7647             : 
    7648         498 :         tdsdump_log(TDS_DBG_INFO1, "copy_data_to_host_var(): tds_convert returned %d\n", len);
    7649             : 
    7650         498 :         if (len < 0) {
    7651           0 :                 _dblib_convert_err(dbproc, len);
    7652           0 :                 return;
    7653             :         }
    7654             : 
    7655         498 :         switch (desttype) {
    7656          60 :         case SYBVARBINARY:
    7657             :         case SYBBINARY:
    7658             :         case SYBIMAGE:
    7659          60 :                 if (bindtype == VARYBINBIND) {
    7660          30 :                         if (limited_dest_space) {
    7661          30 :                                 if (len > sizeof(((DBVARYBIN *)dest)->array)) {
    7662           0 :                                         dbperror(dbproc, SYBECOFL, 0);
    7663           0 :                                         indicator_value = len;
    7664           0 :                                         len = sizeof(((DBVARYBIN *)dest)->array);
    7665             :                                 }
    7666             :                         }
    7667          30 :                         memcpy(((DBVARYBIN *)dest)->array, dres.c, len);
    7668          30 :                         ((DBVARYBIN *)dest)->len = len;
    7669             :                 } else {
    7670          30 :                         if (len > destlen && destlen >= 0) {
    7671           0 :                                 dbperror(dbproc, SYBECOFL, 0);
    7672             :                         } else {
    7673          30 :                                 memcpy(dest, dres.ib, len);
    7674          30 :                                 if (len < destlen)
    7675          10 :                                         memset(dest + len, 0, destlen - len);
    7676             :                         }
    7677             :                 }
    7678          60 :                 TDS_ZERO_FREE(dres.ib);
    7679          60 :                 break;
    7680         250 :         case SYBINT1:
    7681             :         case SYBINT2:
    7682             :         case SYBINT4:
    7683             :         case SYBINT8:
    7684             :         case SYBFLT8:
    7685             :         case SYBREAL:
    7686             :         case SYBBIT:
    7687             :         case SYBBITN:
    7688             :         case SYBMONEY:
    7689             :         case SYBMONEY4:
    7690             :         case SYBDATETIME:
    7691             :         case SYBDATETIME4:
    7692             :         case SYBDATE:
    7693             :         case SYBTIME:
    7694             :         case SYBNUMERIC:
    7695             :         case SYBDECIMAL:
    7696             :         case SYBUNIQUE:
    7697             :         case SYBMSDATE:
    7698             :         case SYBMSTIME:
    7699             :         case SYB5BIGDATETIME:
    7700             :         case SYB5BIGTIME:
    7701             :         case SYBMSDATETIME2:
    7702             :         case SYBMSDATETIMEOFFSET:
    7703         250 :                 memcpy(dest, &(dres.ti), len);
    7704         250 :                 break;
    7705         188 :         case SYBCHAR:
    7706             :         case SYBVARCHAR:
    7707             :         case SYBTEXT:
    7708         188 :                 tdsdump_log(TDS_DBG_INFO1, "copy_data_to_host_var() outputs %d bytes char data destlen = %d \n", len, destlen);
    7709         188 :                 switch (bindtype) {
    7710             :                         case NTBSTRINGBIND: /* strip trailing blanks, null term */
    7711          40 :                                 while (len && dres.c[len - 1] == ' ') {
    7712           0 :                                         --len;
    7713             :                                 }
    7714          40 :                                 if (limited_dest_space) {
    7715          30 :                                         if (len + 1 > destlen) {
    7716          10 :                                                 dbperror(dbproc, SYBECOFL, 0);
    7717          10 :                                                 len = destlen - 1;
    7718             :                                         }
    7719             :                                 }
    7720          40 :                                 memcpy(dest, dres.c, len);
    7721          40 :                                 dest[len] = '\0';
    7722          40 :                                 break;
    7723          98 :                         case STRINGBIND:   /* pad with blanks, null term */
    7724          98 :                                 if (limited_dest_space) {
    7725          30 :                                         if (len + 1 > destlen) {
    7726          10 :                                                 dbperror(dbproc, SYBECOFL, 0);
    7727          10 :                                                 len = destlen - 1;
    7728             :                                         }
    7729             :                                 } else {
    7730          68 :                                         destlen = len + 1;
    7731             :                                 }
    7732          98 :                                 memcpy(dest, dres.c, len);
    7733          98 :                                 if (len < destlen)
    7734          98 :                                         memset(dest + len, ' ', destlen - len - 1);
    7735          98 :                                 dest[destlen - 1] = '\0';
    7736          98 :                                 break;
    7737          50 :                         case CHARBIND:   /* pad with blanks, NO null term */
    7738          50 :                                 if (limited_dest_space) {
    7739          40 :                                         if (len > destlen) {
    7740          10 :                                                 dbperror(dbproc, SYBECOFL, 0);
    7741          10 :                                                 indicator_value = len;
    7742          10 :                                                 len = destlen;
    7743             :                                         }
    7744             :                                 } else {
    7745             :                                         destlen = len; 
    7746             :                                 }
    7747          50 :                                 memcpy(dest, dres.c, len);
    7748          50 :                                 if (len < destlen)
    7749          20 :                                         memset(dest + len, ' ', destlen - len);
    7750             :                                 break;
    7751           0 :                         case VARYCHARBIND: /* strip trailing blanks, NO null term */
    7752           0 :                                 if (limited_dest_space) {
    7753           0 :                                         if (len > sizeof(((DBVARYCHAR *)dest)->str)) {
    7754           0 :                                                 dbperror(dbproc, SYBECOFL, 0);
    7755           0 :                                                 indicator_value = len;
    7756           0 :                                                 len = sizeof(((DBVARYCHAR *)dest)->str);
    7757             :                                         }
    7758             :                                 } 
    7759           0 :                                 memcpy(((DBVARYCHAR *)dest)->str, dres.c, len);
    7760           0 :                                 ((DBVARYCHAR *)dest)->len = len;
    7761           0 :                                 break;
    7762             :                 } 
    7763             : 
    7764         188 :                 free(dres.c);
    7765         188 :                 break;
    7766           0 :         default:
    7767           0 :                 tdsdump_log(TDS_DBG_INFO1, "error: copy_data_to_host_var(): unrecognized desttype %d \n", desttype);
    7768             :                 break;
    7769             : 
    7770             :         }
    7771         498 :         if (indicator)
    7772           0 :                 *indicator = indicator_value;
    7773             : }
    7774             : #endif
    7775             : 
    7776             : /** \internal
    7777             :  * \ingroup dblib_internal
    7778             :  * \remarks member msgno Vendor-defined message number
    7779             :  * \remarks member severity Is passed to the error handler 
    7780             :  * \remarks member msgtext Text of message
    7781             :  */
    7782             : typedef struct _dblib_error_message 
    7783             : {
    7784             :         DBINT msgno;
    7785             :         int severity;
    7786             :         const char *msgtext;
    7787             : } DBLIB_ERROR_MESSAGE;
    7788             : 
    7789             : /*
    7790             :  * The msgtext member holds up to two strings.  The first one is the message text, which may contain placeholders.  
    7791             :  * The second one, if it exists, is the format string for dbstrbuild().  Messages containing no placeholders still need
    7792             :  * an extra NULL to indicate a zero-length format string. 
    7793             :  */
    7794             : static const DBLIB_ERROR_MESSAGE dblib_error_messages[] = 
    7795             :         { { SYBEVERDOWN,           EXINFO,      "TDS version downgraded to 7.1!\0" }
    7796             :         , { SYBEICONVIU,     EXCONVERSION,      "Some character(s) could not be converted into client's character set\0" }
    7797             :         , { SYBEICONVAVAIL,  EXCONVERSION,      "Character set conversion is not available between client character set '%1!' and "
    7798             :                                                 "server character set '%2!'\0%s %s" }
    7799             :         , { SYBEICONVO,      EXCONVERSION,      "Error converting characters into server's character set. Some character(s) could "
    7800             :                                                 "not be converted\0" }
    7801             :         , { SYBEICONVI,      EXCONVERSION,      "Some character(s) could not be converted into client's character set.  Unconverted "
    7802             :                                                 "bytes were changed to question marks ('?')\0" }
    7803             :         , { SYBEICONV2BIG,   EXCONVERSION,      "Buffer overflow converting characters from client into server's character set\0" }
    7804             :         
    7805             :         
    7806             :         , { SYBEPORT,              EXUSER,      "Both port and instance specified\0" }
    7807             :         , { SYBETDSVER,            EXUSER,      "Cannot bcp with TDSVER < 5.0\0" }
    7808             :         , { SYBEAAMT,           EXPROGRAM,      "User attempted a dbaltbind with mismatched column and variable types\0" }
    7809             :         , { SYBEABMT,           EXPROGRAM,      "User attempted a dbbind with mismatched column and variable types\0" }
    7810             :         , { SYBEABNC,           EXPROGRAM,      "Attempt to bind to a non-existent column\0" }
    7811             :         , { SYBEABNP,           EXPROGRAM,      "Attempt to bind using NULL pointers\0" }
    7812             :         , { SYBEABNV,           EXPROGRAM,      "Attempt to bind to a NULL program variable\0" }
    7813             :         , { SYBEACNV,        EXCONVERSION,      "Attempt to do data-conversion with NULL destination variable.\0" }
    7814             :         , { SYBEADST,       EXCONSISTENCY,      "International Release: Error in attempting to determine the size of a pair of "
    7815             :                                                 "translation tables\0" }
    7816             :         , { SYBEAICF,       EXCONSISTENCY,      "International Release: Error in attempting to install custom format\0" }
    7817             :         , { SYBEALTT,       EXCONSISTENCY,      "International Release: Error in attempting to load a pair of translation tables\0" }
    7818             :         , { SYBEAOLF,          EXRESOURCE,      "International Release: Error in attempting to open a localization file\0" }
    7819             :         , { SYBEAPCT,       EXCONSISTENCY,      "International Release: Error in attempting to perform a character set translation\0" }
    7820             :         , { SYBEAPUT,           EXPROGRAM,      "Attempt to print unknown token\0" }
    7821             :         , { SYBEARDI,          EXRESOURCE,      "International Release: Error in attempting to read datetime information from a "
    7822             :                                                 "localization file\0" }
    7823             :         , { SYBEARDL,          EXRESOURCE,      "International Release: Error in attempting to read the dblib.loc localization file\0" }
    7824             :         , { SYBEASEC,           EXPROGRAM,      "Attempt to send an empty command buffer to the server\0" }
    7825             :         , { SYBEASNL,           EXPROGRAM,      "Attempt to set fields in a null LOGINREC\0" }
    7826             :         , { SYBEASTL,           EXPROGRAM,      "Synchronous I/O attempted at AST level\0" }
    7827             :         , { SYBEASUL,           EXPROGRAM,      "Attempt to set unknown LOGINREC field\0" }
    7828             :         , { SYBEAUTN,           EXPROGRAM,      "Attempt to update the timestamp of a table that has no timestamp column\0" }
    7829             :         , { SYBEBADPK,             EXINFO,      "Packet size of %1! not supported -- size of %2! used instead!\0%d %d" }
    7830             :         , { SYBEBBCI,              EXINFO,      "Batch successfully bulk copied to the server\0" }
    7831             :         , { SYBEBBL,            EXPROGRAM,      "Bad bindlen parameter passed to dbsetnull\0" }
    7832             :         , { SYBEBCBC,           EXPROGRAM,      "bcp_columns must be called before bcp_colfmt and bcp_colfmt_ps\0" }
    7833             :         , { SYBEBCBNPR,         EXPROGRAM,      "bcp_bind: if varaddr is NULL, prefixlen must be 0 "
    7834             :                                                 "and no terminator should be specified\0" }
    7835             :         , { SYBEBCBNTYP,        EXPROGRAM,      "bcp_bind: if varaddr is NULL and varlen greater than 0, the table column type "
    7836             :                                                 "must be SYBTEXT or SYBIMAGE and the program variable type must be SYBTEXT, SYBCHAR, "
    7837             :                                                 "SYBIMAGE or SYBBINARY\0" }
    7838             :         , { SYBEBCBPREF,        EXPROGRAM,      "Illegal prefix length. Legal values are 0, 1, 2 or 4\0" }
    7839             :         , { SYBEBCFO,              EXUSER,      "bcp host files must contain at least one column\0" }
    7840             :         , { SYBEBCHLEN,         EXPROGRAM,      "host_collen should be greater than or equal to -1\0" }
    7841             :         , { SYBEBCIS,       EXCONSISTENCY,      "Attempt to bulk copy an illegally-sized column value to the server\0" }
    7842             :         , { SYBEBCIT,           EXPROGRAM,      "It is illegal to use BCP terminators with program variables other than SYBCHAR, "
    7843             :                                                 "SYBBINARY, SYBTEXT, or SYBIMAGE\0" }
    7844             :         , { SYBEBCITBLEN,       EXPROGRAM,      "bcp_init: tblname parameter is too long\0" }
    7845             :         , { SYBEBCITBNM,        EXPROGRAM,      "bcp_init: tblname parameter cannot be NULL\0" }
    7846             :         , { SYBEBCMTXT,         EXPROGRAM,      "bcp_moretext may be used only when there is at least one text or image column in "
    7847             :                                                 "the server table\0" }
    7848             :         , { SYBEBCNL,          EXNONFATAL,      "Negative length-prefix found in BCP data-file\0" }
    7849             :         , { SYBEBCNN,              EXUSER,      "Attempt to bulk copy a NULL value into a Server column "
    7850             :                                                 "which does not accept null values\0" }
    7851             :         , { SYBEBCNT,              EXUSER,      "Attempt to use Bulk Copy with a non-existent Server table\0" }
    7852             :         , { SYBEBCOR,       EXCONSISTENCY,      "Attempt to bulk copy an oversized row to the server\0" }
    7853             :         , { SYBEBCPB,           EXPROGRAM,      "bcp_bind, bcp_moretext and bcp_sendrow may not be used after bcp_init has been "
    7854             :                                                 "passed a non-NULL input file name\0" }
    7855             :         , { SYBEBCPCTYP,        EXPROGRAM,      "bcp_colfmt: If table_colnum is 0, host_type cannot be 0\0" }
    7856             :         , { SYBEBCPI,           EXPROGRAM,      "bcp_init must be called before any other bcp routines\0" }
    7857             :         , { SYBEBCPN,           EXPROGRAM,      "bcp_bind, bcp_collen, bcp_colptr, bcp_moretext and bcp_sendrow may be used only "
    7858             :                                                 "after bcp_init has been called with the copy direction set to DB_IN\0" }
    7859             :         , { SYBEBCPREC,        EXNONFATAL,      "Column %1!: Illegal precision value encountered\0%d" }
    7860             :         , { SYBEBCPREF,         EXPROGRAM,      "Illegal prefix length. Legal values are -1, 0, 1, 2 or 4\0" }
    7861             :         , { SYBEBCRE,          EXNONFATAL,      "I/O error while reading bcp datafile\0" }
    7862             :         , { SYBEBCRO,              EXINFO,      "The BCP hostfile '%1!' contains only %2! rows. It was impossible to read the "
    7863             :                                                 "requested %3! rows\0%s %d %d" }
    7864             :         , { SYBEBCSA,              EXUSER,      "The BCP hostfile '%1!' contains only %2! rows. "
    7865             :                                                 "Skipping all of these rows is not allowed\0%s %d" }
    7866             :         , { SYBEBCSET,      EXCONSISTENCY,      "Unknown character-set encountered\0" }
    7867             :         , { SYBEBCSI,           EXPROGRAM,      "Host-file columns may be skipped only when copying into the Server\0" }
    7868             :         , { SYBEBCSNDROW,       EXPROGRAM,      "bcp_sendrow may not be called unless all text data for the previous row has been "
    7869             :                                                 "sent using bcp_moretext\0" }
    7870             :         , { SYBEBCSNTYP,        EXPROGRAM,      "column number %1!: if varaddr is NULL and varlen greater than 0, the table column "
    7871             :                                                 "type must be SYBTEXT or SYBIMAGE and the program variable type must be SYBTEXT, "
    7872             :                                                 "SYBCHAR, SYBIMAGE or SYBBINARY\0%d" }
    7873             :         , { SYBEBCUC,          EXRESOURCE,      "bcp: Unable to close host datafile\0" }
    7874             :         , { SYBEBCUO,          EXRESOURCE,      "bcp: Unable to open host datafile\0" }
    7875             :         , { SYBEBCVH,           EXPROGRAM,      "bcp_exec may be called only after bcp_init has been passed a valid host file\0" }
    7876             :         , { SYBEBCVLEN,         EXPROGRAM,      "varlen should be greater than or equal to -1\0" }
    7877             :         , { SYBEBCWE,          EXNONFATAL,      "I/O error while writing bcp datafile\0" }
    7878             :         , { SYBEBDIO,           EXPROGRAM,      "Bad bulk copy direction. Must be either IN or OUT\0" }
    7879             :         , { SYBEBEOF,          EXNONFATAL,      "Unexpected EOF encountered in bcp datafile\0" }
    7880             :         , { SYBEBIHC,           EXPROGRAM,      "Incorrect host-column number found in bcp format file\0" }
    7881             :         , { SYBEBIVI,           EXPROGRAM,      "bcp_columns, bcp_colfmt and bcp_colfmt_ps may be used only after bcp_init has been "
    7882             :                                                 "passed a valid input file\0" }
    7883             :         , { SYBEBNCR,           EXPROGRAM,      "Attempt to bind user variable to a non-existent compute row\0" }
    7884             :         , { SYBEBNUM,           EXPROGRAM,      "Bad numbytes parameter passed to dbstrcpy\0" }
    7885             :         , { SYBEBPKS,           EXPROGRAM,      "In DBSETLPACKET, the packet size parameter must be between 0 and 999999\0" }
    7886             :         , { SYBEBPREC,          EXPROGRAM,      "Illegal precision specified\0" }
    7887             :         , { SYBEBPROBADDEF, EXCONSISTENCY,      "bcp protocol error: illegal default column id received\0" }
    7888             :         , { SYBEBPROCOL,    EXCONSISTENCY,      "bcp protocol error: returned column count differs from the actual number of "
    7889             :                                                 "columns received\0" }
    7890             :         , { SYBEBPRODEF,    EXCONSISTENCY,      "bcp protocol error: expected default information and got none\0" }
    7891             :         , { SYBEBPRODEFID,  EXCONSISTENCY,      "bcp protocol error: default column id and actual column id are not same\0" }
    7892             :         , { SYBEBPRODEFTYP, EXCONSISTENCY,      "bcp protocol error: default value datatype differs from column datatype\0" }
    7893             :         , { SYBEBPROEXTDEF, EXCONSISTENCY,      "bcp protocol error: more than one row of default information received\0" }
    7894             :         , { SYBEBPROEXTRES, EXCONSISTENCY,      "bcp protocol error: unexpected set of results received\0" }
    7895             :         , { SYBEBPRONODEF,  EXCONSISTENCY,      "bcp protocol error: default value received for column that does not have default\0" }
    7896             :         , { SYBEBPRONUMDEF, EXCONSISTENCY,      "bcp protocol error: expected number of defaults differs from the actual number of "
    7897             :                                                 "defaults received\0" }
    7898             :         , { SYBEBRFF,          EXRESOURCE,      "I/O error while reading bcp format file\0" }
    7899             :         , { SYBEBSCALE,         EXPROGRAM,      "Illegal scale specified\0" }
    7900             :         , { SYBEBTMT,           EXPROGRAM,      "Attempt to send too much text data via the bcp_moretext call\0" }
    7901             :         , { SYBEBTOK,              EXCOMM,      "Bad token from the server: Datastream processing out of sync\0" }
    7902             :         , { SYBEBTYP,           EXPROGRAM,      "Unknown bind type passed to DB-Library function\0" }
    7903             :         , { SYBEBTYPSRV,        EXPROGRAM,      "Datatype is not supported by the server\0" }
    7904             :         , { SYBEBUCE,          EXRESOURCE,      "bcp: Unable to close error file\0" }
    7905             :         , { SYBEBUCF,           EXPROGRAM,      "bcp: Unable to close format file\0" }
    7906             :         , { SYBEBUDF,           EXPROGRAM,      "bcp: Unrecognized datatype found in format file\0" }
    7907             :         , { SYBEBUFF,           EXPROGRAM,      "bcp: Unable to create format file\0" }
    7908             :         , { SYBEBUFL,       EXCONSISTENCY,      "DB-Library internal error-send buffer length corrupted\0" }
    7909             :         , { SYBEBUOE,          EXRESOURCE,      "bcp: Unable to open error file\0" }
    7910             :         , { SYBEBUOF,           EXPROGRAM,      "bcp: Unable to open format file\0" }
    7911             :         , { SYBEBWEF,          EXNONFATAL,      "I/O error while writing bcp error file\0" }
    7912             :         , { SYBEBWFF,          EXRESOURCE,      "I/O error while writing bcp format file\0" }
    7913             :         , { SYBECAP,               EXCOMM,      "DB-Library capabilities not accepted by the Server\0" }
    7914             :         , { SYBECAPTYP,            EXCOMM,      "Unexpected capability type in CAPABILITY datastream\0" }
    7915             :         , { SYBECDNS,       EXCONSISTENCY,      "Datastream indicates that a compute column is derived from a non-existent select "
    7916             :                                                 "list member\0" }
    7917             :         , { SYBECDOMAIN,     EXCONVERSION,      "Source field value is not within the domain of legal values\0" }
    7918             :         , { SYBECINTERNAL,   EXCONVERSION,      "Internal Conversion error\0" }
    7919             :         , { SYBECLOS,              EXCOMM,      "Error in closing network connection\0" }
    7920             :         , { SYBECLPR,        EXCONVERSION,      "Data conversion resulted in loss of precision\0" }
    7921             :         , { SYBECNOR,           EXPROGRAM,      "Column number out of range\0" }
    7922             :         , { SYBECNOV,        EXCONVERSION,      "Attempt to set variable to NULL resulted in overflow\0" }
    7923             :         , { SYBECOFL,        EXCONVERSION,      "Data conversion resulted in overflow\0" }
    7924             :         , { SYBECONN,              EXCOMM,      "Unable to connect: TDS server is unavailable or does not exist\0" }
    7925             :         , { SYBECRNC,           EXPROGRAM,      "The current row is not a result of compute clause %1!, so it is illegal to attempt "
    7926             :                                                 "to extract that data from this row\0%d" }
    7927             :         , { SYBECRSAGR,         EXPROGRAM,      "Aggregate functions are not allowed in a cursor statement\0" }
    7928             :         , { SYBECRSBROL,        EXPROGRAM,      "Backward scrolling cannot be used in a forward scrolling cursor\0" }
    7929             :         , { SYBECRSBSKEY,       EXPROGRAM,      "Keyset cannot be scrolled backward in mixed cursors with a previous fetch type\0" }
    7930             :         , { SYBECRSBUFR,        EXPROGRAM,      "Row buffering should not be turned on when using cursor APIs\0" }
    7931             :         , { SYBECRSDIS,         EXPROGRAM,      "Cursor statement contains one of the disallowed phrases compute, union, for "
    7932             :                                                 "browse, or select into\0" }
    7933             :         , { SYBECRSFLAST,       EXPROGRAM,      "Fetch type LAST requires fully keyset driven cursors\0" }
    7934             :         , { SYBECRSFRAND,       EXPROGRAM,      "Fetch types RANDOM and RELATIVE can only be used within the keyset of keyset "
    7935             :                                                 "driven cursors\0" }
    7936             :         , { SYBECRSFROWN,       EXPROGRAM,      "Row number to be fetched is outside valid range\0" }
    7937             :         , { SYBECRSFTYPE,      EXRESOURCE,      "Unknown fetch type\0" }
    7938             :         , { SYBECRSINV,         EXPROGRAM,      "Invalid cursor statement\0" }
    7939             :         , { SYBECRSINVALID,    EXRESOURCE,      "The cursor handle is invalid\0" }
    7940             :         , { SYBECRSMROWS,      EXRESOURCE,      "Multiple rows are returned, only one is expected while retrieving dbname\0" }
    7941             :         , { SYBECRSNOBIND,      EXPROGRAM,      "Cursor bind must be called prior to dbcursor invocation\0" }
    7942             :         , { SYBECRSNOCOUNT,     EXPROGRAM,      "The DBNOCOUNT option should not be turned on "
    7943             :                                                 "when doing updates or deletes with dbcursor\0" }
    7944             :         , { SYBECRSNOFREE,      EXPROGRAM,      "The DBNOAUTOFREE option should not be turned on when using cursor APIs\0" }
    7945             :         , { SYBECRSNOIND,       EXPROGRAM,      "One of the tables involved in the cursor statement does not have a unique index\0" }
    7946             :         , { SYBECRSNOKEYS,     EXRESOURCE,      "The entire keyset must be defined for KEYSET type cursors\0" }
    7947             :         , { SYBECRSNOLEN,      EXRESOURCE,      "No unique index found\0" }
    7948             :         , { SYBECRSNOPTCC,     EXRESOURCE,      "No OPTCC was found\0" }
    7949             :         , { SYBECRSNORDER,     EXRESOURCE,      "The order of clauses must be from, where, and order by\0" }
    7950             :         , { SYBECRSNORES,       EXPROGRAM,      "Cursor statement generated no results\0" }
    7951             :         , { SYBECRSNROWS,      EXRESOURCE,      "No rows returned, at least one is expected\0" }
    7952             :         , { SYBECRSNOTABLE,    EXRESOURCE,      "Table name is NULL\0" }
    7953             :         , { SYBECRSNOUPD,       EXPROGRAM,      "Update or delete operation did not affect any rows\0" }
    7954             :         , { SYBECRSNOWHERE,     EXPROGRAM,      "A WHERE clause is not allowed in a cursor update or insert\0" }
    7955             :         , { SYBECRSNUNIQUE,    EXRESOURCE,      "No unique keys associated with this view\0" }
    7956             :         , { SYBECRSORD,         EXPROGRAM,      "Only fully keyset driven cursors can have order by, group by, or having phrases\0" }
    7957             :         , { SYBECRSRO,          EXPROGRAM,      "Data locking or modifications cannot be made in a READONLY cursor\0" }
    7958             :         , { SYBECRSSET,         EXPROGRAM,      "A SET clause is required for a cursor update or insert\0" }
    7959             :         , { SYBECRSTAB,         EXPROGRAM,      "Table name must be determined in operations involving data locking or modifications\0" }
    7960             :         , { SYBECRSVAR,        EXRESOURCE,      "There is no valid address associated with this bind\0" }
    7961             :         , { SYBECRSVIEW,        EXPROGRAM,      "A view cannot be joined with another table or a view in a cursor statement\0" }
    7962             :         , { SYBECRSVIIND,       EXPROGRAM,      "The view used in the cursor statement does not include all the unique index "
    7963             :                                                 "columns of the underlying tables\0" }
    7964             :         , { SYBECRSUPDNB,       EXPROGRAM,      "Update or insert operations cannot use bind variables when binding type is NOBIND\0" }
    7965             :         , { SYBECRSUPDTAB,      EXPROGRAM,      "Update or insert operations using bind variables require single table cursors\0" }
    7966             :         , { SYBECSYN,        EXCONVERSION,      "Attempt to convert data stopped by syntax error in source field\0" }
    7967             :         , { SYBECUFL,        EXCONVERSION,      "Data conversion resulted in underflow\0" }
    7968             :         , { SYBECWLL,           EXPROGRAM,      "Attempt to set column width less than 1\0" }
    7969             :         , { SYBEDBPS,          EXRESOURCE,      "Maximum number of DBPROCESSes already allocated\0" }
    7970             :         , { SYBEDDNE,              EXCOMM,      "DBPROCESS is dead or not enabled\0" }
    7971             :         , { SYBEDIVZ,              EXUSER,      "Attempt to divide by $0.00 in function %1!\0%s" }
    7972             :         , { SYBEDNTI,           EXPROGRAM,      "Attempt to use dbtxtsput to put a new text timestamp into a column whose datatype "
    7973             :                                                 "is neither SYBTEXT nor SYBIMAGE\0" }
    7974             :         , { SYBEDPOR,           EXPROGRAM,      "Out-of-range datepart constant\0" }
    7975             :         , { SYBEDVOR,           EXPROGRAM,      "Day values must be between 1 and 7\0" }
    7976             :         , { SYBEECAN,              EXINFO,      "Attempted to cancel unrequested event notification\0" }
    7977             :         , { SYBEEINI,              EXINFO,      "Must call dbreginit before dbregexec\0" }
    7978             :         , { SYBEETD,            EXPROGRAM,      "Failure to send the expected amount of TEXT or IMAGE data via dbmoretext\0" }
    7979             :         , { SYBEEUNR,              EXCOMM,      "Unsolicited event notification received\0" }
    7980             :         , { SYBEEVOP,              EXINFO,      "Called dbregwatch with a bad options parameter\0" }
    7981             :         , { SYBEEVST,              EXINFO,      "Must initiate a transaction before calling dbregparam\0" }
    7982             :         , { SYBEFCON,              EXCOMM,      "TDS server connection failed\0" }
    7983             :         , { SYBEFRES,             EXFATAL,      "Challenge-Response function failed\0" }
    7984             :         , { SYBEFSHD,          EXRESOURCE,      "Error in attempting to find the Sybase home directory\0" }
    7985             :         , { SYBEFUNC,           EXPROGRAM,      "Functionality not supported at the specified version level\0" }
    7986             :         , { SYBEICN,            EXPROGRAM,      "Invalid computeid or compute column number\0" }
    7987             :         , { SYBEIDCL,       EXCONSISTENCY,      "Illegal datetime column length returned by TDS server. Legal datetime lengths "
    7988             :                                                 "are 4 and 8 bytes\0" }
    7989             :         , { SYBEIDECCL,     EXCONSISTENCY,      "Invalid decimal column length returned by the server\0" }
    7990             :         , { SYBEIFCL,       EXCONSISTENCY,      "Illegal floating-point column length returned by TDS server. Legal "
    7991             :                                                 "floating-point lengths are 4 and 8 bytes\0" }
    7992             :         , { SYBEIFNB,           EXPROGRAM,      "Illegal field number passed to bcp_control\0" }
    7993             :         , { SYBEIICL,       EXCONSISTENCY,      "Illegal integer column length returned by TDS server. Legal integer lengths "
    7994             :                                                 "are 1, 2, and 4 bytes\0" }
    7995             :         , { SYBEIMCL,       EXCONSISTENCY,      "Illegal money column length returned by TDS server. Legal money lengths are 4 "
    7996             :                                                 "and 8 bytes\0" }
    7997             :         , { SYBEINLN,              EXUSER,      "Interface file: unexpected end-of-line\0" }
    7998             :         , { SYBEINTF,              EXUSER,      "Server name not found in configuration files\0" }
    7999             :         , { SYBEINUMCL,     EXCONSISTENCY,      "Invalid numeric column length returned by the server\0" }
    8000             :         , { SYBEIPV,               EXINFO,      "%1! is an illegal value for the %2! parameter of %3!\0%d %s %s" }
    8001             :         , { SYBEISOI,       EXCONSISTENCY,      "International Release: Invalid sort-order information found\0" }
    8002             :         , { SYBEISRVPREC,   EXCONSISTENCY,      "Illegal precision value returned by the server\0" }
    8003             :         , { SYBEISRVSCL,    EXCONSISTENCY,      "Illegal scale value returned by the server\0" }
    8004             :         , { SYBEITIM,           EXPROGRAM,      "Illegal timeout value specified\0" }
    8005             :         , { SYBEIVERS,          EXPROGRAM,      "Illegal version level specified\0" }
    8006             :         , { SYBEKBCI,              EXINFO,      "1000 rows sent to the server\0" }
    8007             :         , { SYBEKBCO,              EXINFO,      "1000 rows successfully bulk copied to host file\0" }
    8008             :         , { SYBEMEM,           EXRESOURCE,      "Unable to allocate sufficient memory\0" }
    8009             :         , { SYBEMOV,               EXUSER,      "Money arithmetic resulted in overflow in function %1!\0%s" }
    8010             :         , { SYBEMPLL,              EXUSER,      "Attempt to set maximum number of DBPROCESSes lower than 1\0" }
    8011             :         , { SYBEMVOR,           EXPROGRAM,      "Month values must be between 1 and 12\0" }
    8012             :         , { SYBENBUF,              EXINFO,      "Called dbsendpassthru with a NULL buf parameter\0" }
    8013             :         , { SYBENBVP,           EXPROGRAM,      "Cannot pass dbsetnull a NULL bindval pointer\0" }
    8014             :         , { SYBENDC,            EXPROGRAM,      "Cannot have negative component in date in numeric form\0" }
    8015             :         , { SYBENDTP,           EXPROGRAM,      "Called dbdatecrack with NULL datetime parameter\0" }
    8016             :         , { SYBENEG,               EXCOMM,      "Negotiated login attempt failed\0" }
    8017             :         , { SYBENHAN,              EXINFO,      "Called dbrecvpassthru with a NULL handle parameter\0" }
    8018             :         , { SYBENMOB,           EXPROGRAM,      "No such member of order by clause\0" }
    8019             :         , { SYBENOEV,              EXINFO,      "DBPOLL can not be called when registered procedure notifications have been disabled\0" }
    8020             :         , { SYBENPRM,           EXPROGRAM,      "NULL parameter not allowed for this dboption\0" }
    8021             :         , { SYBENSIP,           EXPROGRAM,      "Negative starting index passed to dbstrcpy\0" }
    8022             :         , { SYBENTLL,              EXUSER,      "Name too long for LOGINREC field\0" }
    8023             :         , { SYBENTTN,           EXPROGRAM,      "Attempt to use dbtxtsput to put a new text timestamp into a non-existent data row\0" }
    8024             :         , { SYBENULL,              EXINFO,      "NULL DBPROCESS pointer passed to DB-Library\0" }
    8025             :         , { SYBENULP,           EXPROGRAM,      "Called %1! with parameter %2! NULL\0%s %d" }
    8026             :         , { SYBENXID,          EXNONFATAL,      "The Server did not grant us a distributed-transaction ID\0" }
    8027             :         , { SYBEONCE,           EXPROGRAM,      "Function can be called only once\0" }
    8028             :         , { SYBEOOB,               EXCOMM,      "Error in sending out-of-band data to the server\0" }
    8029             :         , { SYBEOPIN,          EXNONFATAL,      "Could not open interface file\0" }
    8030             :         , { SYBEOPNA,          EXNONFATAL,      "Option is not available with current server\0" }
    8031             :         , { SYBEOREN,              EXINFO,      "International Release: Warning: an out-of-range error-number was encountered in "
    8032             :                                                 "dblib.loc. The maximum permissible error-number is defined as DBERRCOUNT in sybdb.h\0" }
    8033             :         , { SYBEORPF,              EXUSER,      "Attempt to set remote password would overflow "
    8034             :                                                 "the login record's remote password field\0" }
    8035             :         , { SYBEPOLL,              EXINFO,      "There is already an active dbpoll\0" }
    8036             :         , { SYBEPRTF,              EXINFO,      "dbtracestring may only be called from a printfunc\0" }
    8037             :         , { SYBEPWD,               EXUSER,      "Login incorrect\0" }
    8038             :         , { SYBERDCN,        EXCONVERSION,      "Requested data conversion does not exist\0" }
    8039             :         , { SYBERDNR,           EXPROGRAM,      "Attempt to retrieve data from a non-existent row\0" }
    8040             :         , { SYBEREAD,              EXCOMM,      "Read from the server failed\0" }
    8041             :         , { SYBERESP,           EXPROGRAM,      "Response function address passed to dbresponse must be non-NULL\0" }
    8042             :         , { SYBERPCS,              EXINFO,      "Must call dbrpcinit before dbrpcparam or dbrpcsend\0" }
    8043             :         , { SYBERPIL,           EXPROGRAM,      "It is illegal to pass -1 to dbrpcparam for the datalen of parameters which are of "
    8044             :                                                 "type SYBCHAR, SYBVARCHAR, SYBBINARY, or SYBVARBINARY\0" }
    8045             :         , { SYBERPNA,          EXNONFATAL,      "The RPC facility is available only when using a server whose version number is 4.0 "
    8046             :                                                 "or later\0" }
    8047             :         , { SYBERPND,           EXPROGRAM,      "Attempt to initiate a new TDS server operation with results pending\0" }
    8048             :         , { SYBERPNULL,         EXPROGRAM,      "value parameter for dbrpcparam can be NULL, only if the datalen parameter is 0\0" }
    8049             :         , { SYBERPTXTIM,        EXPROGRAM,      "RPC parameters cannot be of type text or image\0" }
    8050             :         , { SYBERPUL,           EXPROGRAM,      "When passing a SYBINTN, SYBDATETIMN, SYBMONEYN, or SYBFLTN parameter via "
    8051             :                                                 "dbrpcparam, it is necessary to specify the parameter's maximum or actual length so "
    8052             :                                                 "that DB-Library can recognize it as a SYINT1, SYBINT2, SYBINT4, SYBMONEY, SYBMONEY4, "
    8053             :                                                 "and so on\0" }
    8054             :         , { SYBERTCC,           EXPROGRAM,      "dbreadtext may not be used to receive the results of a query that contains a "
    8055             :                                                 "COMPUTE clause\0" }
    8056             :         , { SYBERTSC,           EXPROGRAM,      "dbreadtext may be used only to receive the results of a query that contains a "
    8057             :                                                 "single result column\0" }
    8058             :         , { SYBERXID,          EXNONFATAL,      "The Server did not recognize our distributed-transaction ID\0" }
    8059             :         , { SYBESECURE,         EXPROGRAM,      "Secure SQL Server function not supported in this version\0" }
    8060             :         , { SYBESEFA,           EXPROGRAM,      "DBSETNOTIFS cannot be called if connections are present\0" }
    8061             :         , { SYBESEOF,              EXCOMM,      "Unexpected EOF from the server\0" }
    8062             :         , { SYBESFOV,           EXPROGRAM,      "International Release: dbsafestr overflowed its destination buffer\0" }
    8063             :         , { SYBESMSG,            EXSERVER,      "General TDS server error: Check messages from the server\0" }
    8064             :         , { SYBESOCK,              EXCOMM,      "Unable to open socket\0" }
    8065             :         , { SYBESPID,           EXPROGRAM,      "Called dbspid with a NULL dbproc\0" }
    8066             :         , { SYBESYNC,              EXCOMM,      "Read attempted while out of synchronization with TDS server\0" }
    8067             :         , { SYBETEXS,              EXINFO,      "Called dbmoretext with a bad size parameter\0" }
    8068             :         , { SYBETIME,              EXTIME,      "TDS server connection timed out\0" }
    8069             :         , { SYBETMCF,           EXPROGRAM,      "Attempt to install too many custom formats via dbfmtinstall\0" }
    8070             :         , { SYBETMTD,           EXPROGRAM,      "Attempt to send too much TEXT data via the dbmoretext call\0" }
    8071             :         , { SYBETPAR,           EXPROGRAM,      "No SYBTEXT or SYBIMAGE parameters were defined\0" }
    8072             :         , { SYBETPTN,              EXUSER,      "Syntax error: only two periods are permitted in table names\0" }
    8073             :         , { SYBETRAC,              EXINFO,      "Attempted to turn off a trace flag that was not on\0" }
    8074             :         , { SYBETRAN,              EXINFO,      "DBPROCESS is being used for another transaction\0" }
    8075             :         , { SYBETRAS,              EXINFO,      "DB-Library internal error - trace structure not found\0" }
    8076             :         , { SYBETRSN,              EXINFO,      "Bad numbytes parameter passed to dbtracestring\0" }
    8077             :         , { SYBETSIT,              EXINFO,      "Attempt to call dbtsput with an invalid timestamp\0" }
    8078             :         , { SYBETTS,               EXUSER,      "The table which bulk copy is attempting to copy to a host file is shorter than the "
    8079             :                                                 "number of rows which bulk copy was instructed to skip\0" }
    8080             :         , { SYBETYPE,              EXINFO,      "Invalid argument type given to Hyper/DB-Library\0" }
    8081             :         , { SYBEUCPT,              EXUSER,      "Unrecognized custom-format parameter-type encountered in dbstrbuild\0" }
    8082             :         , { SYBEUCRR,       EXCONSISTENCY,      "Internal software error: Unknown connection result reported by dbpasswd\0" }
    8083             :         , { SYBEUDTY,       EXCONSISTENCY,      "Unknown datatype encountered\0" }
    8084             :         , { SYBEUFDS,              EXUSER,      "Unrecognized format encountered in dbstrbuild\0" }
    8085             :         , { SYBEUFDT,       EXCONSISTENCY,      "Unknown fixed-length datatype encountered\0" }
    8086             :         , { SYBEUHST,              EXUSER,      "Unknown host machine name\0" }
    8087             :         , { SYBEUMSG,              EXCOMM,      "Unknown message-id in MSG datastream\0" }
    8088             :         , { SYBEUNAM,             EXFATAL,      "Unable to get current user name from operating system\0" }
    8089             :         , { SYBEUNOP,          EXNONFATAL,      "Unknown option passed to dbsetopt\0" }
    8090             :         , { SYBEUNT,               EXUSER,      "Unknown network type found in interface file\0" }
    8091             :         , { SYBEURCI,          EXRESOURCE,      "International Release: Unable to read copyright information from the DB-Library "
    8092             :                                                 "localization file\0" }
    8093             :         , { SYBEUREI,          EXRESOURCE,      "International Release: Unable to read error information from the DB-Library "
    8094             :                                                 "localization file\0" }
    8095             :         , { SYBEUREM,          EXRESOURCE,      "International Release: Unable to read error mnemonic from the DB-Library "
    8096             :                                                 "localization file\0" }
    8097             :         , { SYBEURES,          EXRESOURCE,      "International Release: Unable to read error string from the DB-Library "
    8098             :                                                 "localization file. 401 Error severities\0" }
    8099             :         , { SYBEURMI,          EXRESOURCE,      "International Release: Unable to read money-format information from the DB-Library "
    8100             :                                                 "localization file\0" }
    8101             :         , { SYBEUSCT,              EXCOMM,      "Unable to set communications timer\0" }
    8102             :         , { SYBEUTDS,              EXCOMM,      "Unrecognized TDS version received from the server\0" }
    8103             :         , { SYBEUVBF,           EXPROGRAM,      "Attempt to read an unknown version of bcp format file\0" }
    8104             :         , { SYBEUVDT,       EXCONSISTENCY,      "Unknown variable-length datatype encountered\0" }
    8105             :         , { SYBEVDPT,              EXUSER,      "For bulk copy, all variable-length data must have either a length-prefix or a "
    8106             :                                                 "terminator specified\0" }
    8107             :         , { SYBEWAID,       EXCONSISTENCY,      "DB-Library internal error: ALTFMT following ALTNAME has wrong id\0" }
    8108             :         , { SYBEWRIT,              EXCOMM,      "Write to the server failed\0" }
    8109             :         , { SYBEXOCI,          EXNONFATAL,      "International Release: A character-set translation overflowed its destination "
    8110             :                                                 "buffer while using bcp to copy data from a host-file to the server\0" }
    8111             :         , { SYBEXTDN,           EXPROGRAM,      "Warning: the xlt_todisp parameter to dbfree_xlate was NULL. The space associated "
    8112             :                                                 "with the xlt_tosrv parameter has been freed\0" }
    8113             :         , { SYBEXTN,            EXPROGRAM,      "The xlt_tosrv and xlt_todisp parameters to dbfree_xlate were NULL\0" }
    8114             :         , { SYBEXTSN,           EXPROGRAM,      "Warning: the xlt_tosrv parameter to dbfree_xlate was NULL. The space associated "
    8115             :                                                 "with the xlt_todisp parameter has been freed\0" }
    8116             :         , { SYBEZTXT,              EXINFO,      "Attempt to send zero length TEXT or IMAGE to dataserver via dbwritetext\0" }
    8117             :         , { SYBECOLSIZE,           EXINFO,      "Invalid column information structure size\0" }
    8118             :         };
    8119             : 
    8120             : /**  \internal
    8121             :  * \ingroup dblib_internal
    8122             :  * \brief Call client-installed error handler
    8123             :  * 
    8124             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    8125             :  * \param msgno identifies the error message to be passed to the client's handler.
    8126             :  * \param errnum identifies the OS error (errno), if any.  Use 0 if not applicable.  
    8127             :  * \returns the handler's return code, subject to correction and adjustment for vendor style:
    8128             :  *      - INT_CANCEL    The db-lib function that encountered the error will return FAIL.  
    8129             :  *      - INT_TIMEOUT   The db-lib function will cancel the operation and return FAIL.  \a dbproc remains useable.  
    8130             :  *      - INT_CONTINUE  The db-lib function will retry the operation.  
    8131             :  * \remarks 
    8132             :  *      The client-installed handler may also return INT_EXIT.  If Sybase semantics are used, this function notifies
    8133             :  *      the user and calls exit(3).  If Microsoft semantics are used, this function returns INT_CANCEL.  
    8134             :  *
    8135             :  *      If the client-installed handler returns something other than these four INT_* values, or returns timeout-related
    8136             :  *      value for anything but SYBETIME, it's treated here as INT_EXIT (see above).  
    8137             :  *
    8138             :  * Instead of sprinkling error text all over db-lib, we consolidate it here, 
    8139             :  * where it can be translated (one day), and where it can be mapped to the TDS error number.  
    8140             :  * The libraries don't use consistent error numbers or messages, so when libtds has to emit 
    8141             :  * an error message, it can't include the text.  It can pass its error number to a client-library
    8142             :  * function, which will interpret it, add the text, call the application's installed handler
    8143             :  * (if any) and return the handler's return code back to the caller.  
    8144             :  * 
    8145             :  * The call stack may look something like this:
    8146             :  *
    8147             :  * -#   application
    8148             :  * -#           db-lib function (encounters error)
    8149             :  * -#           dbperror
    8150             :  * -#   error handler (installed by application)
    8151             :  *
    8152             :  * The error handling in this case is unambiguous: the caller invokes this function, the client's handler returns its 
    8153             :  * instruction, which the caller receives.  Quite often the caller will get INT_CANCEL, in which case it should put its 
    8154             :  * house in order and return FAIL.  
    8155             :  *
    8156             :  * The call stack may otherwise look something like this:
    8157             :  *                      
    8158             :  * -#   application
    8159             :  * -#           db-lib function
    8160             :  * -#                   libtds function (encounters error)
    8161             :  * -#           _dblib_handle_err_message
    8162             :  * -#           dbperror
    8163             :  * -#   error handler (installed by application)
    8164             :  *
    8165             :  * Because different client libraries specify their handler semantics differently, 
    8166             :  * and because libtds doesn't know which client library is in charge of any given connection, it cannot interpret the 
    8167             :  * raw return code from a db-lib error handler.  For these reasons, 
    8168             :  * libtds calls _dblib_handle_err_message, which translates between libtds and db-lib semantics.  
    8169             :  * \sa dberrhandle(), _dblib_handle_err_message().
    8170             :  */
    8171             : int
    8172         262 : dbperror(DBPROCESS *dbproc, DBINT msgno, long errnum, ...)
    8173             : {
    8174             :         static const char int_exit_text[] = "FreeTDS: db-lib: exiting because client error handler returned %s for msgno %d\n";
    8175             :         static const char int_invalid_text[] = "%s (%d) received from client-installed error handler for nontimeout for error %d."
    8176             :                                                "  Treating as INT_EXIT\n";
    8177             :         static const DBLIB_ERROR_MESSAGE default_message = { 0, EXCONSISTENCY, "unrecognized msgno" };
    8178         262 :         DBLIB_ERROR_MESSAGE constructed_message = { 0, EXCONSISTENCY, NULL };
    8179         262 :         const DBLIB_ERROR_MESSAGE *msg = &default_message;
    8180             :         
    8181         262 :         int i, rc = INT_CANCEL;
    8182         262 :         const char *os_msgtext = strerror(errnum), *rc_name = "logic error";
    8183             :         char rc_buf[16];
    8184             : 
    8185         262 :         tdsdump_log(TDS_DBG_FUNC, "dbperror(%p, %d, %ld)\n", dbproc, msgno, errnum);  /* dbproc can be NULL */
    8186             : 
    8187             : #ifdef _WIN32
    8188             :         /*
    8189             :          * Unfortunately MinGW uses the "old" msvcrt.dll (Visual C++ 2005 uses
    8190             :          * a newer version) which does not set errno when allocation functions
    8191             :          * cannot allocate memory, so we do it for them.
    8192             :          */
    8193             :         if (msgno == SYBEMEM)
    8194             :                 errnum = ENOMEM;
    8195             : #endif
    8196             : 
    8197         262 :         if (os_msgtext == NULL)
    8198           0 :                 os_msgtext = "no OS error";
    8199             :         
    8200         262 :         assert(_dblib_err_handler != NULL);     /* always installed by dbinit() or dberrhandle() */
    8201             : 
    8202             :         /* look up the error message */
    8203       44874 :         for (i=0; i < TDS_VECTOR_SIZE(dblib_error_messages); i++ ) {
    8204       45136 :                 if (dblib_error_messages[i].msgno == msgno) {
    8205             : 
    8206             :                         /* 
    8207             :                          * See if the message has placeholders.  If so, build a message string on the heap.  
    8208             :                          * The presence of placeholders is indicated by the existence of a "string after the string", 
    8209             :                          * i.e., a format string (for dbstrbuild) after a null "terminator" in the message. 
    8210             :                          * On error -- can't allocate, can't build the string -- give up and call the client handler anyway. 
    8211             :                          */
    8212         262 :                         const char * ptext = dblib_error_messages[i].msgtext;
    8213         262 :                         const char * pformats = ptext + strlen(ptext) + 1;
    8214         262 :                         msg = &dblib_error_messages[i];
    8215         262 :                         assert(*(pformats - 1) == '\0'); 
    8216         262 :                         if(*pformats != '\0') {
    8217             :                                 va_list ap;
    8218          10 :                                 int result_len, len = 2 * (int)strlen(ptext);
    8219          10 :                                 char * buffer = tds_new0(char, len);
    8220             : 
    8221          10 :                                 if (buffer == NULL)
    8222             :                                         break;
    8223          10 :                                 va_start(ap, errnum);
    8224          10 :                                 rc = tds_vstrbuild(buffer, len, &result_len, ptext, TDS_NULLTERM, pformats, TDS_NULLTERM, ap);
    8225          10 :                                 buffer[result_len] = '\0';
    8226          10 :                                 va_end(ap);
    8227          10 :                                 if (TDS_FAILED(rc)) {
    8228           0 :                                         free(buffer);
    8229           0 :                                         break;
    8230             :                                 }
    8231          10 :                                 constructed_message.msgtext = buffer;
    8232          10 :                                 constructed_message.severity = msg->severity;
    8233          10 :                                 msg = &constructed_message;
    8234             :                         }
    8235             :                         break;
    8236             :                 }
    8237             :         }
    8238             : 
    8239         262 :         if (dbproc && dbproc->tds_socket && dbproc->tds_socket->login) {
    8240           0 :                 DSTR server_name_dstr = dbproc->tds_socket->login->server_name;
    8241           0 :                 if (!tds_dstr_isempty(&server_name_dstr)) {
    8242           0 :                         char * buffer = NULL;
    8243           0 :                         if (asprintf(&buffer, "%s (%s)", msg->msgtext,
    8244             :                                      tds_dstr_cstr(&server_name_dstr)) >= 0) {
    8245           0 :                                 free((char*) constructed_message.msgtext);
    8246           0 :                                 constructed_message.msgtext = buffer;
    8247           0 :                                 constructed_message.severity = msg->severity;
    8248           0 :                                 msg = &constructed_message;
    8249             :                         }
    8250             :                 }
    8251             :         }
    8252             : 
    8253         262 :         tdsdump_log(TDS_DBG_FUNC, "dbperror: Calling dblib_err_handler with msgno = %d; msg->msgtext = \"%s\"\n",
    8254             :                 msgno, msg->msgtext);
    8255             : 
    8256             :         /* call the error handler */
    8257         262 :         rc = (*_dblib_err_handler)(dbproc, msg->severity, msgno, errnum, (char*) msg->msgtext, (char*) os_msgtext);
    8258         262 :         switch (rc) {
    8259             :         case INT_EXIT:
    8260             :                 rc_name = "INT_EXIT"; 
    8261             :                 break;
    8262          40 :         case INT_CONTINUE:      
    8263          40 :                 rc_name = "INT_CONTINUE";
    8264          40 :                 break;
    8265         202 :         case INT_CANCEL:
    8266         202 :                 rc_name = "INT_CANCEL";
    8267         202 :                 break;
    8268          20 :         case INT_TIMEOUT:
    8269          20 :                 rc_name = "INT_TIMEOUT";
    8270          20 :                 break;
    8271           0 :         default:
    8272           0 :                 rc_name = "invalid";
    8273           0 :                 break;
    8274             :         }
    8275         262 :         tdsdump_log(TDS_DBG_FUNC, "dbperror: dblib_err_handler for msgno = %d; msg->msgtext = \"%s\" -- returns %d (%s)\n",
    8276             :                 msgno, msg->msgtext, rc, rc_name);
    8277             : 
    8278             :         /* we're done with the dynamic string now. */
    8279         262 :         free((char*) constructed_message.msgtext);
    8280             :         
    8281             :         /* Timeout return codes are errors for non-timeout conditions. */
    8282         262 :         if (msgno != SYBETIME) {
    8283         192 :                 switch (rc) {
    8284           0 :                 case INT_CONTINUE:
    8285           0 :                         tdsdump_log(TDS_DBG_SEVERE, int_invalid_text, "INT_CONTINUE", rc, msgno);
    8286             :                         rc = INT_EXIT;
    8287             :                         break;
    8288           0 :                 case INT_TIMEOUT:
    8289           0 :                         tdsdump_log(TDS_DBG_SEVERE, int_invalid_text, "INT_TIMEOUT", rc, msgno);
    8290             :                         rc = INT_EXIT;
    8291             :                         break;
    8292             :                 default:
    8293             :                         break;
    8294             :                 }
    8295          70 :         }
    8296             : 
    8297             :         /* 
    8298             :          * Sybase exits on INT_EXIT; Microsoft converts to INT_CANCEL.
    8299             :          * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dblibc/dbc_pdc04c_6v39.asp
    8300             :          */
    8301         262 :         switch (rc) {
    8302          40 :         case INT_CONTINUE:
    8303             :                 /* Microsoft does not define INT_TIMEOUT.  Instead, two consecutive INT_CONTINUEs yield INT_CANCEL. */
    8304          40 :                 if (dbproc && dbproc->msdblib && ++dbproc->ntimeouts >=2) {
    8305           0 :                         dbproc->ntimeouts = 0;
    8306           0 :                         rc = INT_CANCEL;
    8307             :                 }       /* fall through */
    8308             :         case INT_CANCEL:
    8309             :         case INT_TIMEOUT:
    8310             :                 return rc;      /* normal case */
    8311             :                 break;
    8312           0 :         default:
    8313           0 :                 sprintf(rc_buf, "%d", rc);
    8314           0 :                 rc_name = rc_buf;
    8315           0 :                 tdsdump_log(TDS_DBG_SEVERE, int_invalid_text, "Invalid return code", rc, msgno);
    8316             :                 /* fall through */
    8317             :         case INT_EXIT:
    8318           0 :                 if (dbproc && dbproc->msdblib) {
    8319             :                         /* Microsoft behavior */
    8320             :                         return INT_CANCEL;
    8321             :                 }
    8322             :                 /* fprintf(stderr, int_exit_text, rc_name, msgno); */
    8323           0 :                 tdsdump_log(TDS_DBG_SEVERE, int_exit_text, rc_name, msgno);
    8324             :                 break;
    8325             :         }
    8326           0 :         exit(EXIT_FAILURE);
    8327             :         return rc; /* not reached */
    8328             : }
    8329             : 

Generated by: LCOV version 1.13