LCOV - code coverage report
Current view: top level - src/dblib - dblib.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 1381 2576 53.6 %
Date: 2025-05-01 08:30:09 Functions: 97 160 60.6 %

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

Generated by: LCOV version 1.13