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

Generated by: LCOV version 1.13