Main Page Modules Compound List File List Compound Members File Members Related Pages

# tds.h

Go to the documentation of this file.

    00001 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
    00002  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004  Brian Bruns
    00003  *
    00004  * This library is free software; you can redistribute it and/or
    00005  * modify it under the terms of the GNU Library General Public
    00006  * License as published by the Free Software Foundation; either
    00007  * version 2 of the License, or (at your option) any later version.
    00008  *
    00009  * This library is distributed in the hope that it will be useful,
    00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012  * Library General Public License for more details.
    00013  *
    00014  * You should have received a copy of the GNU Library General Public
    00015  * License along with this library; if not, write to the
    00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00017  * Boston, MA 02111-1307, USA.
    00018  */
    00019
    00020 #ifndef _tds_h_
    00021 #define _tds_h_
    00022
    00023 static const char rcsid_tds_h[] = "$Id: tds.h,v 1.192 2004/10/28 12:42:12 freddy77 Exp $";
    00024 static const void *const no_unused_tds_h_warn[] = { rcsid_tds_h, no_unused_tds_h_warn };
    00025
    00026 #include <stdio.h>
    00027 #include <stdarg.h>
    00028 #include <time.h>
    00029
    00030 /* forward declaration */
    00031 typedef struct tdsiconvinfo TDSICONV;
    00032 typedef struct tds_socket TDSSOCKET;
    00033
    00034 #include "tdsver.h"
    00035 #include "tds_sysdep_public.h"
    00036 #ifdef _FREETDS_LIBRARY_SOURCE
    00037 #include "tds_sysdep_private.h"
    00038 #endif /* _FREETDS_LIBRARY_SOURCE */
    00039
    00040 #ifdef __cplusplus
    00041 extern "C"
    00042 {
    00043 #if 0
    00044 }
    00045 #endif
    00046 #endif
    00047
    00053 typedef struct _tds_compiletime_settings
    00054 {
    00055         const char *freetds_version;    /* release version of FreeTDS */
    00056         const char *last_update;        /* latest software_version date among the modules */
    00057         int msdblib;            /* for MS style dblib */
    00058         int sybase_compat;      /* enable increased Open Client binary compatibility */
    00059         int threadsafe;         /* compile for thread safety default=no */
    00060         int libiconv;           /* search for libiconv in DIR/include and DIR/lib */
    00061         const char *tdsver;     /* TDS protocol version (4.2/4.6/5.0/7.0/8.0) 5.0 */
    00062         int iodbc;              /* build odbc driver against iODBC in DIR */
    00063         int unixodbc;           /* build odbc driver against unixODBC in DIR */
    00064
    00065 } TDS_COMPILETIME_SETTINGS;
    00066
    00067 struct DSTR_STRUCT {
    00068         /* keep always at last */
    00069         char dstr_s[1];
    00070 };
    00071 typedef struct DSTR_STRUCT *DSTR;
    00072
    00088 /*
    00089  * All references to data that touch the wire should use the following typedefs.
    00090  *
    00091  * If you have problems on 64-bit machines and the code is
    00092  * using a native datatype, please change it to use
    00093  * these. (In the TDS layer only, the API layers have their
    00094  * own typedefs which equate to these).
    00095  */
    00096 typedef char TDS_CHAR;                                  /*  8-bit char     */
    00097 typedef unsigned char TDS_UCHAR;                        /*  8-bit uchar    */
    00098 typedef unsigned char TDS_TINYINT;                      /*  8-bit unsigned */
    00099 typedef tds_sysdep_int16_type TDS_SMALLINT;             /* 16-bit int      */
    00100 typedef unsigned tds_sysdep_int16_type TDS_USMALLINT;   /* 16-bit unsigned */
    00101 typedef tds_sysdep_int32_type TDS_INT;                  /* 32-bit int      */
    00102 typedef unsigned tds_sysdep_int32_type TDS_UINT;        /* 32-bit unsigned */
    00103 typedef tds_sysdep_real32_type TDS_REAL;                /* 32-bit real     */
    00104 typedef tds_sysdep_real64_type TDS_FLOAT;               /* 64-bit real     */
    00105 typedef tds_sysdep_int64_type TDS_INT8;                 /* 64-bit integer  */
    00106 typedef unsigned tds_sysdep_int64_type TDS_UINT8;       /* 64-bit unsigned */
    00107 typedef tds_sysdep_intptr_type TDS_INTPTR;
    00108
    00109 typedef struct tdsnumeric
    00110 {
    00111         unsigned char precision;
    00112         unsigned char scale;
    00113         unsigned char array[33];
    00114 } TDS_NUMERIC;
    00115
    00116 typedef struct tdsoldmoney
    00117 {
    00118         TDS_INT mnyhigh;
    00119         TDS_UINT mnylow;
    00120 } TDS_OLD_MONEY;
    00121
    00122 typedef union tdsmoney
    00123 {
    00124         TDS_OLD_MONEY tdsoldmoney;
    00125         TDS_INT8 mny;
    00126 } TDS_MONEY;
    00127
    00128 typedef struct tdsmoney4
    00129 {
    00130         TDS_INT mny4;
    00131 } TDS_MONEY4;
    00132
    00133 typedef struct tdsdatetime
    00134 {
    00135         TDS_INT dtdays;
    00136         TDS_INT dttime;
    00137 } TDS_DATETIME;
    00138
    00139 typedef struct tdsdatetime4
    00140 {
    00141         TDS_USMALLINT days;
    00142         TDS_USMALLINT minutes;
    00143 } TDS_DATETIME4;
    00144
    00145 typedef struct tdsvarbinary
    00146 {
    00147         TDS_INT len;
    00148         TDS_CHAR array[256];
    00149 } TDS_VARBINARY;
    00150 typedef struct tdsvarchar
    00151 {
    00152         TDS_INT len;
    00153         TDS_CHAR array[256];
    00154 } TDS_VARCHAR;
    00155
    00156 typedef struct tdsunique
    00157 {
    00158         TDS_UINT Data1;
    00159         TDS_USMALLINT Data2;
    00160         TDS_USMALLINT Data3;
    00161         TDS_UCHAR Data4[8];
    00162 } TDS_UNIQUE;
    00163
    00165 typedef struct tdsdaterec
    00166 {
    00167         TDS_INT year;
    00168         TDS_INT month;
    00169         TDS_INT day;
    00170         TDS_INT dayofyear;
    00171         TDS_INT weekday;
    00172         TDS_INT hour;
    00173         TDS_INT minute;
    00174         TDS_INT second;
    00175         TDS_INT millisecond;
    00176         TDS_INT tzone;
    00177 } TDSDATEREC;
    00178
    00184 extern const int tds_numeric_bytes_per_prec[];
    00185
    00186 #define TDS_SUCCEED          1
    00187 #define TDS_FAIL             0
    00188 #define TDS_NO_MORE_RESULTS  2
    00189
    00190 #define TDS_REG_ROW          -1
    00191 #define TDS_NO_MORE_ROWS     -2
    00192 #define TDS_COMP_ROW         -3
    00193 #define TDS_END_ROW          -4
    00194
    00195 #define TDS_INT_EXIT 0
    00196 #define TDS_INT_CONTINUE 1
    00197 #define TDS_INT_CANCEL 2
    00198 #define TDS_INT_TIMEOUT 3
    00199
    00200
    00201 #define TDS_NO_COUNT         -1
    00202
    00203 #define TDS_ROW_RESULT        4040
    00204 #define TDS_PARAM_RESULT      4042
    00205 #define TDS_STATUS_RESULT     4043
    00206 #define TDS_MSG_RESULT        4044
    00207 #define TDS_COMPUTE_RESULT    4045
    00208 #define TDS_CMD_DONE          4046
    00209 #define TDS_CMD_SUCCEED       4047
    00210 #define TDS_CMD_FAIL          4048
    00211 #define TDS_ROWFMT_RESULT     4049
    00212 #define TDS_COMPUTEFMT_RESULT 4050
    00213 #define TDS_DESCRIBE_RESULT   4051
    00214 #define TDS_DONE_RESULT       4052
    00215 #define TDS_DONEPROC_RESULT   4053
    00216 #define TDS_DONEINPROC_RESULT 4054
    00217
    00218 enum tds_end
    00219 {
    00220           TDS_DONE_FINAL        = 0x00  /* final result set, command completed successfully. */
    00221         , TDS_DONE_MORE_RESULTS = 0x01  /* more results follow */
    00222         , TDS_DONE_ERROR        = 0x02  /* error occurred */
    00223         , TDS_DONE_INXACT       = 0x04  /* transaction in progress */
    00224         , TDS_DONE_PROC         = 0x08  /* results are from a stored procedure */
    00225         , TDS_DONE_COUNT        = 0x10  /* count field in packet is valid */
    00226         , TDS_DONE_CANCELLED    = 0x20  /* acknowledging an attention command (usually a cancel) */
    00227         , TDS_DONE_EVENT        = 0x40  /* part of an event notification. */
    00228         , TDS_DONE_SRVERROR     = 0x100 /* SQL server server error */
    00229
    00230         /* after the above flags, a TDS_DONE packet has a field describing the state of the transaction */
    00231         , TDS_DONE_NO_TRAN      = 0     /* No transaction in effect */
    00232         , TDS_DONE_TRAN_SUCCEED = 1     /* Transaction completed successfully */
    00233         , TDS_DONE_TRAN_PROGRESS= 2     /* Transaction in progress */
    00234         , TDS_DONE_STMT_ABORT   = 3     /* A statement aborted */
    00235         , TDS_DONE_TRAN_ABORT   = 4     /* Transaction aborted */
    00236 };
    00237
    00238 /*
    00239  * TDS_ERROR indicates a successful processing, but that a TDS_ERROR_TOKEN or TDS_EED_TOKEN error was encountered.
    00240  * TDS_FAIL indicates an unrecoverable failure.
    00241  */
    00242 #define TDS_ERROR            3
    00243 #define TDS_DONT_RETURN      42
    00244
    00245 #define TDS5_PARAMFMT2_TOKEN       32   /* 0x20 */
    00246 #define TDS_LANGUAGE_TOKEN         33   /* 0x21    TDS 5.0 only              */
    00247 #define TDS_ORDERBY2_TOKEN         34   /* 0x22 */
    00248 #define TDS_ROWFMT2_TOKEN          97   /* 0x61    TDS 5.0 only              */
    00249 #define TDS_LOGOUT_TOKEN          113   /* 0x71    TDS 5.0 only? ct_close()  */
    00250 #define TDS_RETURNSTATUS_TOKEN    121   /* 0x79                              */
    00251 #define TDS_PROCID_TOKEN          124   /* 0x7C    TDS 4.2 only - TDS_PROCID */
    00252 #define TDS7_RESULT_TOKEN         129   /* 0x81    TDS 7.0 only              */
    00253 #define TDS7_COMPUTE_RESULT_TOKEN 136   /* 0x88    TDS 7.0 only              */
    00254 #define TDS_COLNAME_TOKEN         160   /* 0xA0    TDS 4.2 only              */
    00255 #define TDS_COLFMT_TOKEN          161   /* 0xA1    TDS 4.2 only - TDS_COLFMT */
    00256 #define TDS_DYNAMIC2_TOKEN        163   /* 0xA3 */
    00257 #define TDS_TABNAME_TOKEN         164   /* 0xA4 */
    00258 #define TDS_COLINFO_TOKEN         165   /* 0xA5 */
    00259 #define TDS_OPTIONCMD_TOKEN       166   /* 0xA6 */
    00260 #define TDS_COMPUTE_NAMES_TOKEN   167   /* 0xA7 */
    00261 #define TDS_COMPUTE_RESULT_TOKEN  168   /* 0xA8 */
    00262 #define TDS_ORDERBY_TOKEN         169   /* 0xA9    TDS_ORDER                 */
    00263 #define TDS_ERROR_TOKEN           170   /* 0xAA                              */
    00264 #define TDS_INFO_TOKEN            171   /* 0xAB                              */
    00265 #define TDS_PARAM_TOKEN           172   /* 0xAC    RETURNVALUE?              */
    00266 #define TDS_LOGINACK_TOKEN        173   /* 0xAD                              */
    00267 #define TDS_CONTROL_TOKEN         174   /* 0xAE    TDS_CONTROL               */
    00268 #define TDS_ROW_TOKEN             209   /* 0xD1                              */
    00269 #define TDS_CMP_ROW_TOKEN         211   /* 0xD3                              */
    00270 #define TDS5_PARAMS_TOKEN         215   /* 0xD7    TDS 5.0 only              */
    00271 #define TDS_CAPABILITY_TOKEN      226   /* 0xE2                              */
    00272 #define TDS_ENVCHANGE_TOKEN       227   /* 0xE3                              */
    00273 #define TDS_EED_TOKEN             229   /* 0xE5                              */
    00274 #define TDS_DBRPC_TOKEN           230   /* 0xE6                              */
    00275 #define TDS5_DYNAMIC_TOKEN        231   /* 0xE7    TDS 5.0 only              */
    00276 #define TDS5_PARAMFMT_TOKEN       236   /* 0xEC    TDS 5.0 only              */
    00277 #define TDS_AUTH_TOKEN            237   /* 0xED                              */
    00278 #define TDS_RESULT_TOKEN          238   /* 0xEE                              */
    00279 #define TDS_DONE_TOKEN            253   /* 0xFD    TDS_DONE                  */
    00280 #define TDS_DONEPROC_TOKEN        254   /* 0xFE    TDS_DONEPROC              */
    00281 #define TDS_DONEINPROC_TOKEN      255   /* 0xFF    TDS_DONEINPROC            */
    00282
    00283 /* CURSOR support: TDS 5.0 only*/
    00284 #define TDS_CURCLOSE_TOKEN        128  /* 0x80    TDS 5.0 only              */
    00285 #define TDS_CURFETCH_TOKEN        130  /* 0x82    TDS 5.0 only              */
    00286 #define TDS_CURINFO_TOKEN         131  /* 0x83    TDS 5.0 only              */
    00287 #define TDS_CUROPEN_TOKEN         132  /* 0x84    TDS 5.0 only              */
    00288 #define TDS_CURDECLARE_TOKEN      134  /* 0x86    TDS 5.0 only              */
    00289
    00290 enum {
    00291         TDS_CUR_ISTAT_UNUSED    = 0x00,
    00292         TDS_CUR_ISTAT_DECLARED  = 0x01,
    00293         TDS_CUR_ISTAT_OPEN      = 0x02,
    00294         TDS_CUR_ISTAT_CLOSED    = 0x04,
    00295         TDS_CUR_ISTAT_RDONLY    = 0x08,
    00296         TDS_CUR_ISTAT_UPDATABLE = 0x10,
    00297         TDS_CUR_ISTAT_ROWCNT    = 0x20,
    00298         TDS_CUR_ISTAT_DEALLOC   = 0x40
    00299 };
    00300
    00301 /*
    00302  * Cursor Declare, SetRows, Open and Close all return 0x83 token.
    00303  * But only SetRows includes the rowcount (4 byte) in the stream.
    00304  * So for Setrows we read the rowcount from the stream and not for others.
    00305  * These values are useful to determine when to read the rowcount from the packet
    00306  */
    00307 #define IS_DECLARE  100
    00308 #define IS_CURROW   200
    00309 #define IS_OPEN     300
    00310 #define IS_CLOSE    400
    00311
    00312 /* states for tds_process_messages() */
    00313 #define PROCESS_ROWS    0
    00314 #define PROCESS_RESULTS 1
    00315 #define CANCEL_PROCESS  2
    00316 #define GOTO_1ST_ROW    3
    00317 #define LOGIN           4
    00318
    00319 /* environment type field */
    00320 #define TDS_ENV_DATABASE        1
    00321 #define TDS_ENV_LANG            2
    00322 #define TDS_ENV_CHARSET         3
    00323 #define TDS_ENV_PACKSIZE        4
    00324 #define TDS_ENV_LCID            5
    00325 #define TDS_ENV_SQLCOLLATION    7
    00326
    00327 /* string types */
    00328 #define TDS_NULLTERM -9
    00329
    00330 /* Microsoft internal stored procedure id's */
    00331
    00332 #define TDS_SP_CURSOR           1
    00333 #define TDS_SP_CURSOROPEN       2
    00334 #define TDS_SP_CURSORPREPARE    3
    00335 #define TDS_SP_CURSOREXECUTE    4
    00336 #define TDS_SP_CURSORPREPEXEC   5
    00337 #define TDS_SP_CURSORUNPREPARE  6
    00338 #define TDS_SP_CURSORFETCH      7
    00339 #define TDS_SP_CURSOROPTION     8
    00340 #define TDS_SP_CURSORCLOSE      9
    00341 #define TDS_SP_EXECUTESQL      10
    00342 #define TDS_SP_PREPARE         11
    00343 #define TDS_SP_EXECUTE         12
    00344 #define TDS_SP_PREPEXEC        13
    00345 #define TDS_SP_PREPEXECRPC     14
    00346 #define TDS_SP_UNPREPARE       15
    00347 /*
    00348  * <rant> Sybase does an awful job of this stuff, non null ints of size 1 2
    00349  * and 4 have there own codes but nullable ints are lumped into INTN
    00350  * sheesh! </rant>
    00351  */
    00352 typedef enum
    00353 {
    00354         SYBCHAR = 47,           /* 0x2F */
    00355 #define SYBCHAR SYBCHAR
    00356         SYBVARCHAR = 39,        /* 0x27 */
    00357 #define SYBVARCHAR      SYBVARCHAR
    00358         SYBINTN = 38,           /* 0x26 */
    00359 #define SYBINTN SYBINTN
    00360         SYBINT1 = 48,           /* 0x30 */
    00361 #define SYBINT1 SYBINT1
    00362         SYBINT2 = 52,           /* 0x34 */
    00363 #define SYBINT2 SYBINT2
    00364         SYBINT4 = 56,           /* 0x38 */
    00365 #define SYBINT4 SYBINT4
    00366         SYBINT8 = 127,          /* 0x7F */
    00367 #define SYBINT8 SYBINT8
    00368         SYBFLT8 = 62,           /* 0x3E */
    00369 #define SYBFLT8 SYBFLT8
    00370         SYBDATETIME = 61,       /* 0x3D */
    00371 #define SYBDATETIME     SYBDATETIME
    00372         SYBBIT = 50,            /* 0x32 */
    00373 #define SYBBIT  SYBBIT
    00374         SYBTEXT = 35,           /* 0x23 */
    00375 #define SYBTEXT SYBTEXT
    00376         SYBNTEXT = 99,          /* 0x63 */
    00377 #define SYBNTEXT        SYBNTEXT
    00378         SYBIMAGE = 34,          /* 0x22 */
    00379 #define SYBIMAGE        SYBIMAGE
    00380         SYBMONEY4 = 122,        /* 0x7A */
    00381 #define SYBMONEY4       SYBMONEY4
    00382         SYBMONEY = 60,          /* 0x3C */
    00383 #define SYBMONEY        SYBMONEY
    00384         SYBDATETIME4 = 58,      /* 0x3A */
    00385 #define SYBDATETIME4    SYBDATETIME4
    00386         SYBREAL = 59,           /* 0x3B */
    00387 #define SYBREAL SYBREAL
    00388         SYBBINARY = 45,         /* 0x2D */
    00389 #define SYBBINARY       SYBBINARY
    00390         SYBVOID = 31,           /* 0x1F */
    00391 #define SYBVOID SYBVOID
    00392         SYBVARBINARY = 37,      /* 0x25 */
    00393 #define SYBVARBINARY    SYBVARBINARY
    00394         SYBNVARCHAR = 103,      /* 0x67 */
    00395 #define SYBNVARCHAR     SYBNVARCHAR
    00396         SYBBITN = 104,          /* 0x68 */
    00397 #define SYBBITN SYBBITN
    00398         SYBNUMERIC = 108,       /* 0x6C */
    00399 #define SYBNUMERIC      SYBNUMERIC
    00400         SYBDECIMAL = 106,       /* 0x6A */
    00401 #define SYBDECIMAL      SYBDECIMAL
    00402         SYBFLTN = 109,          /* 0x6D */
    00403 #define SYBFLTN SYBFLTN
    00404         SYBMONEYN = 110,        /* 0x6E */
    00405 #define SYBMONEYN       SYBMONEYN
    00406         SYBDATETIMN = 111,      /* 0x6F */
    00407 #define SYBDATETIMN     SYBDATETIMN
    00408         XSYBCHAR = 175,         /* 0xAF */
    00409 #define XSYBCHAR        XSYBCHAR
    00410         XSYBVARCHAR = 167,      /* 0xA7 */
    00411 #define XSYBVARCHAR     XSYBVARCHAR
    00412         XSYBNVARCHAR = 231,     /* 0xE7 */
    00413 #define XSYBNVARCHAR    XSYBNVARCHAR
    00414         XSYBNCHAR = 239,        /* 0xEF */
    00415 #define XSYBNCHAR       XSYBNCHAR
    00416         XSYBVARBINARY = 165,    /* 0xA5 */
    00417 #define XSYBVARBINARY   XSYBVARBINARY
    00418         XSYBBINARY = 173,       /* 0xAD */
    00419 #define XSYBBINARY      XSYBBINARY
    00420         SYBLONGBINARY = 225,    /* 0xE1 */
    00421 #define SYBLONGBINARY   SYBLONGBINARY
    00422         SYBSINT1 = 64,          /* 0x40 */
    00423 #define SYBSINT1        SYBSINT1
    00424         SYBUINT2 = 65,          /* 0x41 */
    00425 #define SYBUINT2        SYBUINT2
    00426         SYBUINT4 = 66,          /* 0x42 */
    00427 #define SYBUINT4        SYBUINT4
    00428         SYBUINT8 = 67,          /* 0x43 */
    00429 #define SYBUINT8        SYBUINT8
    00430
    00431         SYBUNIQUE = 36,         /* 0x24 */
    00432 #define SYBUNIQUE       SYBUNIQUE
    00433         SYBVARIANT = 98         /* 0x62 */
    00434 #define SYBVARIANT      SYBVARIANT
    00435 } TDS_SERVER_TYPE;
    00436
    00437
    00438 typedef enum
    00439 {
    00440         USER_UNICHAR_TYPE = 34,         /* 0x22 */
    00441         USER_UNIVARCHAR_TYPE = 35       /* 0x23 */
    00442 } TDS_USER_TYPE;
    00443
    00444 #define SYBAOPCNT  0x4b
    00445 #define SYBAOPCNTU 0x4c
    00446 #define SYBAOPSUM  0x4d
    00447 #define SYBAOPSUMU 0x4e
    00448 #define SYBAOPAVG  0x4f
    00449 #define SYBAOPAVGU 0x50
    00450 #define SYBAOPMIN  0x51
    00451 #define SYBAOPMAX  0x52
    00452
    00453 /* mssql2k compute operator */
    00454 #define SYBAOPCNT_BIG           0x09
    00455 #define SYBAOPSTDEV             0x30
    00456 #define SYBAOPSTDEVP            0x31
    00457 #define SYBAOPVAR               0x32
    00458 #define SYBAOPVARP              0x33
    00459 #define SYBAOPCHECKSUM_AGG      0x72
    00460
    00461
    00465 typedef enum
    00466 {
    00467           TDS_OPT_SET = 1       /* Set an option. */
    00468         , TDS_OPT_DEFAULT = 2   /* Set option to its default value. */
    00469         , TDS_OPT_LIST = 3      /* Request current setting of a specific option. */
    00470         , TDS_OPT_INFO = 4      /* Report current setting of a specific option. */
    00471 } TDS_OPTION_CMD;
    00472
    00473 typedef enum
    00474 {
    00475           TDS_OPT_DATEFIRST = 1         /* 0x01 */
    00476         , TDS_OPT_TEXTSIZE = 2          /* 0x02 */
    00477         , TDS_OPT_STAT_TIME = 3         /* 0x03 */
    00478         , TDS_OPT_STAT_IO = 4           /* 0x04 */
    00479         , TDS_OPT_ROWCOUNT = 5          /* 0x05 */
    00480         , TDS_OPT_NATLANG = 6           /* 0x06 */
    00481         , TDS_OPT_DATEFORMAT = 7        /* 0x07 */
    00482         , TDS_OPT_ISOLATION = 8         /* 0x08 */
    00483         , TDS_OPT_AUTHON = 9            /* 0x09 */
    00484         , TDS_OPT_CHARSET = 10          /* 0x0a */
    00485         , TDS_OPT_SHOWPLAN = 13         /* 0x0d */
    00486         , TDS_OPT_NOEXEC = 14           /* 0x0e */
    00487         , TDS_OPT_ARITHIGNOREON = 15    /* 0x0f */
    00488         , TDS_OPT_ARITHABORTON = 17     /* 0x11 */
    00489         , TDS_OPT_PARSEONLY = 18        /* 0x12 */
    00490         , TDS_OPT_GETDATA = 20          /* 0x14 */
    00491         , TDS_OPT_NOCOUNT = 21          /* 0x15 */
    00492         , TDS_OPT_FORCEPLAN = 23        /* 0x17 */
    00493         , TDS_OPT_FORMATONLY = 24       /* 0x18 */
    00494         , TDS_OPT_CHAINXACTS = 25       /* 0x19 */
    00495         , TDS_OPT_CURCLOSEONXACT = 26   /* 0x1a */
    00496         , TDS_OPT_FIPSFLAG = 27         /* 0x1b */
    00497         , TDS_OPT_RESTREES = 28         /* 0x1c */
    00498         , TDS_OPT_IDENTITYON = 29       /* 0x1d */
    00499         , TDS_OPT_CURREAD = 30          /* 0x1e */
    00500         , TDS_OPT_CURWRITE = 31         /* 0x1f */
    00501         , TDS_OPT_IDENTITYOFF = 32      /* 0x20 */
    00502         , TDS_OPT_AUTHOFF = 33          /* 0x21 */
    00503         , TDS_OPT_ANSINULL = 34         /* 0x22 */
    00504         , TDS_OPT_QUOTED_IDENT = 35     /* 0x23 */
    00505         , TDS_OPT_ARITHIGNOREOFF = 36   /* 0x24 */
    00506         , TDS_OPT_ARITHABORTOFF = 37    /* 0x25 */
    00507         , TDS_OPT_TRUNCABORT = 38       /* 0x26 */
    00508 } TDS_OPTION;
    00509
    00510 typedef union tds_option_arg
    00511 {
    00512         TDS_TINYINT ti;
    00513         TDS_INT i;
    00514         TDS_CHAR *c;
    00515 } TDS_OPTION_ARG;
    00516
    00517 enum {
    00518         TDS_OPT_ARITHOVERFLOW = 0x01,
    00519         TDS_OPT_NUMERICTRUNC = 0x02
    00520 };
    00521
    00522 enum TDS_OPT_DATEFIRST_CHOICE
    00523 {
    00524         TDS_OPT_MONDAY = 1, TDS_OPT_TUESDAY = 2, TDS_OPT_WEDNESDAY = 3, TDS_OPT_THURSDAY = 4, TDS_OPT_FRIDAY = 5, TDS_OPT_SATURDAY =
    00525                 6, TDS_OPT_SUNDAY = 7
    00526 };
    00527
    00528 enum TDS_OPT_DATEFORMAT_CHOICE
    00529 {
    00530         TDS_OPT_FMTMDY = 1, TDS_OPT_FMTDMY = 2, TDS_OPT_FMTYMD = 3, TDS_OPT_FMTYDM = 4, TDS_OPT_FMTMYD = 5, TDS_OPT_FMTDYM = 6
    00531 };
    00532 enum TDS_OPT_ISOLATION_CHOICE
    00533 {
    00534         TDS_OPT_LEVEL1 = 1, TDS_OPT_LEVEL3 = 3
    00535 };
    00536
    00537 #define TDS_ZERO_FREE(x) do {free((x)); (x) = NULL;} while(0)
    00538 #define TDS_VECTOR_SIZE(x) (sizeof(x)/sizeof(x[0]))
    00539
    00540 /*
    00541  * TODO use system macros for optimization
    00542  * See mcrypt for reference and linux kernel source for optimization
    00543  * check if unaligned access and use fast write/read when implemented
    00544  */
    00545 #define TDS_BYTE_SWAP16(value)                 \
    00546          (((((unsigned short)value)<<8) & 0xFF00)   | \
    00547           ((((unsigned short)value)>>8) & 0x00FF))
    00548
    00549 #define TDS_BYTE_SWAP32(value)                     \
    00550          (((((unsigned long)value)<<24) & 0xFF000000)  | \
    00551           ((((unsigned long)value)<< 8) & 0x00FF0000)  | \
    00552           ((((unsigned long)value)>> 8) & 0x0000FF00)  | \
    00553           ((((unsigned long)value)>>24) & 0x000000FF))
    00554
    00555 #define is_end_token(x) (x==TDS_DONE_TOKEN    || \
    00556                         x==TDS_DONEPROC_TOKEN    || \
    00557                         x==TDS_DONEINPROC_TOKEN)
    00558
    00559 #define is_hard_end_token(x) (x==TDS_DONE_TOKEN    || \
    00560                         x==TDS_DONEPROC_TOKEN)
    00561
    00562 #define is_msg_token(x) (x==TDS_INFO_TOKEN    || \
    00563                         x==TDS_ERROR_TOKEN    || \
    00564                         x==TDS_EED_TOKEN)
    00565
    00566 #define is_result_token(x) (x==TDS_RESULT_TOKEN || \
    00567                         x==TDS_ROWFMT2_TOKEN    || \
    00568                         x==TDS7_RESULT_TOKEN    || \
    00569                         x==TDS_COLFMT_TOKEN     || \
    00570                         x==TDS_COLNAME_TOKEN    || \
    00571                         x==TDS_RETURNSTATUS_TOKEN)
    00572
    00573 /* FIX ME -- not a complete list */
    00574 #define is_fixed_type(x) (x==SYBINT1    || \
    00575                         x==SYBINT2      || \
    00576                         x==SYBINT4      || \
    00577                         x==SYBINT8      || \
    00578                         x==SYBREAL       || \
    00579                         x==SYBFLT8      || \
    00580                         x==SYBDATETIME  || \
    00581                         x==SYBDATETIME4 || \
    00582                         x==SYBBIT       || \
    00583                         x==SYBMONEY     || \
    00584                         x==SYBMONEY4    || \
    00585                         x==SYBUNIQUE)
    00586 #define is_nullable_type(x) ( \
    00587                         x==SYBBITN      || \
    00588                      x==SYBINTN      || \
    00589                      x==SYBFLTN      || \
    00590                      x==SYBMONEYN    || \
    00591                      x==SYBDATETIMN  || \
    00592                      x==SYBVARCHAR   || \
    00593                      x==SYBBINARY    || \
    00594                      x==SYBVARBINARY || \
    00595                      x==SYBTEXT      || \
    00596                      x==SYBNTEXT     || \
    00597                      x==SYBIMAGE)
    00598
    00599 #define is_blob_type(x) (x==SYBTEXT || x==SYBIMAGE || x==SYBNTEXT)
    00600 /* large type means it has a two byte size field */
    00601 /* define is_large_type(x) (x>128) */
    00602 #define is_numeric_type(x) (x==SYBNUMERIC || x==SYBDECIMAL)
    00603 #define is_unicode_type(x) (x==XSYBNVARCHAR || x==XSYBNCHAR || x==SYBNTEXT)
    00604 #define is_collate_type(x) (x==XSYBVARCHAR || x==XSYBCHAR || x==SYBTEXT || x==XSYBNVARCHAR || x==XSYBNCHAR || x==SYBNTEXT)
    00605 #define is_ascii_type(x) ( x==XSYBCHAR || x==XSYBVARCHAR || x==SYBTEXT || x==SYBCHAR || x==SYBVARCHAR)
    00606 #define is_binary_type(x) (x==SYBLONGBINARY)
    00607 #define is_char_type(x) (is_unicode_type(x) || is_ascii_type(x))
    00608 #define is_similar_type(x, y) ((is_char_type(x) && is_char_type(y)) || ((is_unicode_type(x) && is_unicode_type(y))))
    00609
    00610
    00611 #define TDS_MAX_CAPABILITY      22
    00612 #define MAXPRECISION            80
    00613 #define TDS_MAX_CONN            4096
    00614 #define TDS_MAX_DYNID_LEN       30
    00615
    00616 /* defaults to use if no others are found */
    00617 #define TDS_DEF_SERVER          "SYBASE"
    00618 #define TDS_DEF_BLKSZ           512
    00619 #define TDS_DEF_CHARSET         "iso_1"
    00620 #define TDS_DEF_LANG            "us_english"
    00621 #if TDS42
    00622 #define TDS_DEF_MAJOR           4
    00623 #define TDS_DEF_MINOR           2
    00624 #define TDS_DEF_PORT            1433
    00625 #elif TDS46
    00626 #define TDS_DEF_MAJOR           4
    00627 #define TDS_DEF_MINOR           6
    00628 #define TDS_DEF_PORT            4000
    00629 #elif TDS70
    00630 #define TDS_DEF_MAJOR           7
    00631 #define TDS_DEF_MINOR           0
    00632 #define TDS_DEF_PORT            1433
    00633 #elif TDS80
    00634 #define TDS_DEF_MAJOR           8
    00635 #define TDS_DEF_MINOR           0
    00636 #define TDS_DEF_PORT            1433
    00637 #else
    00638 #define TDS_DEF_MAJOR           5
    00639 #define TDS_DEF_MINOR           0
    00640 #define TDS_DEF_PORT            4000
    00641 #endif
    00642
    00643 /* normalized strings from freetds.conf file */
    00644 #define TDS_STR_VERSION  "tds version"
    00645 #define TDS_STR_BLKSZ    "initial block size"
    00646 #define TDS_STR_SWAPDT   "swap broken dates"
    00647 #define TDS_STR_SWAPMNY  "swap broken money"
    00648 #define TDS_STR_TRYSVR   "try server login"
    00649 #define TDS_STR_TRYDOM   "try domain login"
    00650 #define TDS_STR_DOMAIN   "nt domain"
    00651 #define TDS_STR_XDOMAUTH "cross domain login"
    00652 #define TDS_STR_DUMPFILE "dump file"
    00653 #define TDS_STR_DEBUGLVL "debug level"
    00654 #define TDS_STR_TIMEOUT  "timeout"
    00655 #define TDS_STR_CONNTIMEOUT "connect timeout"
    00656 #define TDS_STR_HOSTNAME "hostname"
    00657 #define TDS_STR_HOST     "host"
    00658 #define TDS_STR_PORT     "port"
    00659 #define TDS_STR_TEXTSZ   "text size"
    00660 /* for big endian hosts */
    00661 #define TDS_STR_EMUL_LE "emulate little endian"
    00662 #define TDS_STR_CHARSET "charset"
    00663 #define TDS_STR_CLCHARSET       "client charset"
    00664 #define TDS_STR_LANGUAGE        "language"
    00665 #define TDS_STR_APPENDMODE      "dump file append"
    00666 #define TDS_STR_DATEFMT "date format"
    00667
    00668 /* TODO do a better check for alignment than this */
    00669 typedef union
    00670 {
    00671         void *p;
    00672         int i;
    00673 } tds_align_struct;
    00674
    00675 #define TDS_ALIGN_SIZE sizeof(tds_align_struct)
    00676
    00677 #define TDS_MAX_LOGIN_STR_SZ 30
    00678 typedef struct tds_login
    00679 {
    00680         DSTR server_name;
    00681         int port;
    00682         TDS_TINYINT major_version;      /* TDS version */
    00683         TDS_TINYINT minor_version;      /* TDS version */
    00684         int block_size;
    00685         DSTR language;          /* ie us-english */
    00686         DSTR server_charset;    /*  ie iso_1 */
    00687         TDS_INT connect_timeout;
    00688         DSTR host_name;
    00689         DSTR app_name;
    00690         DSTR user_name;
    00691         DSTR password;
    00692
    00693         DSTR library;   /* Ct-Library, DB-Library,  TDS-Library or ODBC */
    00694         TDS_TINYINT bulk_copy;
    00695         TDS_TINYINT suppress_language;
    00696         TDS_TINYINT encrypted;
    00697
    00698         TDS_INT query_timeout;
    00699         TDS_INT longquery_timeout;
    00700         void (*longquery_func) (void *param);
    00701         void *longquery_param;
    00702         unsigned char capabilities[TDS_MAX_CAPABILITY];
    00703         DSTR client_charset;
    00704 } TDSLOGIN;
    00705
    00706 typedef struct tds_connection
    00707 {
    00708         /* first part of structure is the same of login one */
    00709         DSTR server_name;
    00710         int port;
    00711         TDS_TINYINT major_version;
    00712         TDS_TINYINT minor_version;
    00713         int block_size;
    00714         DSTR language;
    00715         DSTR server_charset;
    00716         TDS_INT connect_timeout;
    00717         DSTR host_name;
    00718         DSTR app_name;
    00719         DSTR user_name;
    00720         DSTR password;
    00721         DSTR library;
    00722         TDS_TINYINT bulk_copy;
    00723         TDS_TINYINT suppress_language;
    00724         TDS_TINYINT encrypted;
    00725
    00726         TDS_INT query_timeout;
    00727         TDS_INT longquery_timeout;
    00728         void (*longquery_func) (void *param);
    00729         void *longquery_param;
    00730         unsigned char capabilities[TDS_MAX_CAPABILITY];
    00731         DSTR client_charset;
    00732
    00733         DSTR ip_addr;
    00734         DSTR database;
    00735         DSTR dump_file;
    00736         DSTR default_domain;
    00737         int timeout;
    00738         int debug_level;
    00739         int text_size;
    00740         int broken_dates;
    00741         int broken_money;
    00742         int try_server_login;
    00743         int try_domain_login;
    00744         int xdomain_auth;
    00745         int emul_little_endian;
    00746 } TDSCONNECTION;
    00747
    00748 typedef struct tds_locale
    00749 {
    00750         char *language;
    00751         char *char_set;
    00752         char *date_fmt;
    00753 } TDSLOCALE;
    00754
    00759 typedef struct tds_blob
    00760 {
    00761         TDS_CHAR *textvalue;
    00762         TDS_CHAR textptr[16];
    00763         TDS_CHAR timestamp[8];
    00764 } TDSBLOB;
    00765
    00769 typedef struct
    00770 {
    00771         TDS_USMALLINT locale_id;        /* master..syslanguages.lcid */
    00772         TDS_USMALLINT flags;
    00773         TDS_UCHAR charset_id;           /* or zero */
    00774 } TDS8_COLLATION;
    00775
    00776 /* SF stands for "sort flag" */
    00777 #define TDS_SF_BIN                   (TDS_USMALLINT) 0x100
    00778 #define TDS_SF_WIDTH_INSENSITIVE     (TDS_USMALLINT) 0x080
    00779 #define TDS_SF_KATATYPE_INSENSITIVE  (TDS_USMALLINT) 0x040
    00780 #define TDS_SF_ACCENT_SENSITIVE      (TDS_USMALLINT) 0x020
    00781 #define TDS_SF_CASE_INSENSITIVE      (TDS_USMALLINT) 0x010
    00782
    00783 /* UT stands for user type */
    00784 #define TDS_UT_TIMESTAMP             80
    00785
    00786
    00791 typedef struct _tds_encoding
    00792 {
    00793         const char *name;
    00794         unsigned char min_bytes_per_char;
    00795         unsigned char max_bytes_per_char;
    00796 } TDS_ENCODING;
    00797
    00798 typedef struct _tds_bcpcoldata
    00799 {
    00800         TDS_UCHAR *data;
    00801         TDS_INT    datalen;
    00802         TDS_INT    null_column;
    00803 } BCPCOLDATA;
    00804
    00805
    00806 enum
    00807 { TDS_SYSNAME_SIZE = 512 };
    00808
    00812 typedef struct tds_column
    00813 {
    00814         TDS_SMALLINT column_type;
    00819         TDS_INT column_usertype;
    00820         TDS_INT column_flags;
    00821
    00822         TDS_INT column_size;
    00824         TDS_TINYINT column_varint_size;
    00826         TDS_TINYINT column_prec;
    00827         TDS_TINYINT column_scale;
    00829         TDS_TINYINT column_namelen;
    00830         TDS_TINYINT table_namelen;
    00831         struct
    00832         {
    00833                 TDS_SMALLINT column_type;
    00834                 TDS_INT column_size;
    00835         } on_server;
    00836
    00837         const TDSICONV *char_conv;
    00839         TDS_CHAR table_name[TDS_SYSNAME_SIZE];
    00840         TDS_CHAR column_name[TDS_SYSNAME_SIZE];
    00841
    00842         TDS_INT column_offset;
    00843         unsigned int column_nullable:1;
    00844         unsigned int column_writeable:1;
    00845         unsigned int column_identity:1;
    00846         unsigned int column_key:1;
    00847         unsigned int column_hidden:1;
    00848         unsigned int column_output:1;
    00849         unsigned int column_timestamp:1;
    00850         TDS_UCHAR column_collation[5];
    00851
    00852         /* additional fields flags for compute results */
    00853         TDS_TINYINT column_operator;
    00854         TDS_SMALLINT column_operand;
    00855
    00856         /* FIXME this is data related, not column */
    00858         TDS_INT column_cur_size;
    00859
    00860         /* related to binding or info stored by client libraries */
    00861         /* FIXME find a best place to store these data, some are unused */
    00862         TDS_SMALLINT column_bindtype;
    00863         TDS_SMALLINT column_bindfmt;
    00864         TDS_UINT column_bindlen;
    00865         TDS_SMALLINT *column_nullbind;
    00866         TDS_CHAR *column_varaddr;
    00867         TDS_INT *column_lenbind;
    00868         TDS_INT column_textpos;
    00869         TDS_INT column_text_sqlgetdatapos;
    00870
    00871         BCPCOLDATA *bcp_column_data;
    00872         TDS_INT bcp_prefix_len;
    00873         TDS_INT bcp_term_len;
    00874         TDS_CHAR *bcp_terminator;
    00875 } TDSCOLUMN;
    00876
    00877
    00879 typedef struct tds_result_info
    00880 {
    00881         /* TODO those fields can became a struct */
    00882         TDS_SMALLINT num_cols;
    00883         TDSCOLUMN **columns;
    00884         TDS_INT row_size;
    00885         int null_info_size;
    00886         unsigned char *current_row;
    00887
    00888         TDS_SMALLINT rows_exist;
    00889         TDS_INT row_count;
    00890         TDS_SMALLINT computeid;
    00891         TDS_TINYINT more_results;
    00892         TDS_TINYINT *bycolumns;
    00893         TDS_SMALLINT by_cols;
    00894 } TDSRESULTINFO;
    00895
    00896 /* values for tds->state */
    00897 enum
    00898 {
    00899         TDS_QUERYING,
    00900         TDS_PENDING,
    00901         TDS_IDLE,
    00902         TDS_CANCELED,
    00903         TDS_DEAD
    00904 };
    00905
    00906 #define TDS_DBG_FUNC    7
    00907 #define TDS_DBG_INFO2   6
    00908 #define TDS_DBG_INFO1   5
    00909 #define TDS_DBG_NETWORK 4
    00910 #define TDS_DBG_WARN    3
    00911 #define TDS_DBG_ERROR   2
    00912 #define TDS_DBG_SEVERE  1
    00913
    00919 enum TDS_DBG_LOG_STATE
    00920 {
    00921           TDS_DBG_LOGIN = 1             /* for diagnosing login problems;
    00922                                            otherwise the username/password information is suppressed. */
    00923         , TDS_DBG_API =    (1 << 1)     /* Log calls to client libraries */
    00924         , TDS_DBG_ASYNC =  (1 << 2)     /* Log asynchronous function starts or completes. */
    00925         , TDS_DBG_DIAG =   (1 << 3)     /* Log client- and server-generated messages */
    00926         , TDS_DBG_error =  (1 << 4)
    00927         /* TODO:  ^^^^^ make upper case when old #defines (above) are removed */
    00928         /* Log FreeTDS runtime/logic error occurs. */
    00929         , TDS_DBG_PACKET = (1 << 5)     /* Log hex dump of packets to/from the server. */
    00930         , TDS_DBG_LIBTDS = (1 << 6)     /* Log calls to (and in) libtds */
    00931         , TDS_DBG_CONFIG = (1 << 7)     /* replaces TDSDUMPCONFIG */
    00932         , TDS_DBG_DEFAULT = 0xFE        /* all above except login packets */
    00933 };
    00934
    00935 typedef struct tds_result_info TDSCOMPUTEINFO;
    00936
    00937 typedef TDSRESULTINFO TDSPARAMINFO;
    00938
    00939 typedef struct tds_message
    00940 {
    00941         TDS_SMALLINT priv_msg_type;
    00942         TDS_SMALLINT line_number;
    00943         TDS_UINT msg_number;
    00944         TDS_SMALLINT msg_state;
    00945         TDS_SMALLINT msg_level;
    00946         TDS_CHAR *server;
    00947         TDS_CHAR *message;
    00948         TDS_CHAR *proc_name;
    00949         TDS_CHAR *sql_state;
    00950 } TDSMESSAGE;
    00951
    00952 typedef struct tds_upd_col
    00953 {
    00954         struct tds_upd_col *next;
    00955         TDS_INT colnamelength;
    00956         char * columnname;
    00957 } TDSUPDCOL;
    00958
    00959 typedef enum {
    00960           TDS_CURSOR_STATE_UNACTIONED = 0
    00961         , TDS_CURSOR_STATE_REQUESTED = 1        /* called by ct_cursor */
    00962         , TDS_CURSOR_STATE_SENT = 2             /* sent to server and ack */
    00963 } TDS_CURSOR_STATE;
    00964
    00965 typedef struct _tds_cursor_status
    00966 {
    00967         TDS_CURSOR_STATE declare;
    00968         TDS_CURSOR_STATE cursor_row;
    00969         TDS_CURSOR_STATE open;
    00970         TDS_CURSOR_STATE fetch;
    00971         TDS_CURSOR_STATE close;
    00972         TDS_CURSOR_STATE dealloc;
    00973 } TDS_CURSOR_STATUS;
    00974
    00975 typedef struct _tds_cursor
    00976 {
    00977         TDS_INT length;                 /* total length of the remaining datastream */
    00978         TDS_TINYINT cursor_name_len;    /* length of cursor name > 0 and <= 30  */
    00979         char *cursor_name;              /* name of the cursor */
    00980         TDS_INT cursor_id;              /* cursor id returned by the server after cursor declare */
    00981         TDS_INT client_cursor_id;       /* cursor id used internally by client side */
    00982         TDS_TINYINT options;            /* read only|updatable */
    00983         TDS_TINYINT hasargs;            /* cursor parameters exists ? */
    00984         TDS_USMALLINT query_len;        /* SQL query length */
    00985         char *query;                    /* SQL query */
    00986         /* TODO for updatable columns */
    00987         TDS_TINYINT number_upd_cols;    /* number of updatable columns */
    00988         TDS_INT cursor_rows;            /* number of cursor rows to fetch */
    00989         /*TODO when cursor has parameters*/
    00990         /*TDS_PARAM *param_list;         cursor parameter */
    00991         TDSUPDCOL *cur_col_list;        /* updatable column list */
    00992         TDS_CURSOR_STATUS status;
    00993         TDSRESULTINFO *res_info;
    00994         struct _tds_cursor *next;
    00995 } TDSCURSOR;
    00996
    00997 /*
    00998  * Current environment as reported by the server
    00999  */
    01000 typedef struct tds_env
    01001 {
    01002         int block_size;
    01003         char *language;
    01004         char *charset;
    01005         char *database;
    01006 } TDSENV;
    01007
    01008 typedef struct tds_dynamic
    01009 {
    01010         char id[30];
    01011         int dyn_state;
    01013         TDS_INT num_id;
    01014         TDSPARAMINFO *res_info;
    01015         TDSPARAMINFO *params;
    01016         int emulated;
    01018         char *query;
    01019 } TDSDYNAMIC;
    01020
    01021 /* forward declaration */
    01022 typedef struct tds_context TDSCONTEXT;
    01023
    01024 struct tds_context
    01025 {
    01026         TDSLOCALE *locale;
    01027         void *parent;
    01028         /* handler */
    01029         int (*msg_handler) (TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
    01030         int (*err_handler) (TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
    01031 };
    01032
    01033 enum TDS_ICONV_ENTRY
    01034 {
    01035           client2ucs2
    01036         , client2server_chardata
    01037         , iso2server_metadata
    01038         , initial_char_conv_count       /* keep last */
    01039 };
    01040
    01041 struct tds_socket
    01042 {
    01043         /* fixed and connect time */
    01044         TDS_SYS_SOCKET s;
    01045         TDS_SMALLINT major_version;
    01046         TDS_SMALLINT minor_version;
    01048         TDS_UINT product_version;
    01049         char *product_name;
    01050         unsigned char capabilities[TDS_MAX_CAPABILITY];
    01051         unsigned char broken_dates;
    01052         unsigned char option_flag2;
    01053         /* in/out buffers */
    01054         unsigned char *in_buf;
    01055         unsigned char *out_buf;
    01056         unsigned int in_buf_max;
    01057         unsigned in_pos;
    01058         unsigned out_pos;
    01059         unsigned in_len;
    01060         unsigned out_len;
    01061         unsigned char in_flag;
    01062         unsigned char out_flag;
    01063         unsigned char last_packet;
    01064         void *parent;
    01065         /*
    01066          * info about current query.
    01067          * Contain information in process, even normal results and compute.
    01068          * This pointer shouldn't be freed.
    01069          */
    01070         TDSRESULTINFO *current_results;
    01071         TDSRESULTINFO *res_info;
    01072         TDS_INT num_comp_info;
    01073         TDSCOMPUTEINFO **comp_info;
    01074         TDSPARAMINFO *param_info;
    01075         TDSCURSOR *cursor;
    01076         TDS_TINYINT has_status;
    01077         TDS_INT ret_status;
    01078         TDS_TINYINT state;
    01079         int rows_affected;
    01080         /* timeout stuff from Jeff */
    01081         TDS_INT timeout;
    01082         TDS_INT longquery_timeout;
    01083         void (*longquery_func) (void *param);
    01084         void *longquery_param;
    01085         time_t queryStarttime;
    01086         TDSENV *env;
    01087         /* dynamic placeholder stuff */
    01088         int num_dyns;
    01089         TDSDYNAMIC *cur_dyn;
    01090         TDSDYNAMIC **dyns;
    01091         int emul_little_endian;
    01092         char *date_fmt;
    01093         TDSCONTEXT *tds_ctx;
    01094         int char_conv_count;
    01095         TDSICONV **char_convs;
    01096
    01098         TDSCONNECTION *connection;
    01099         int spid;
    01100         TDS_UCHAR collation[5];
    01101         void (*env_chg_func) (TDSSOCKET * tds, int type, char *oldval, char *newval);
    01102         int (*chkintr) (TDSSOCKET * tds);
    01103         int (*hndlintr) (TDSSOCKET * tds);
    01104         int internal_sp_called;
    01105         int client_cursor_id;       /* cursor id used internally by client side */
    01106 };
    01107
    01108 void tds_set_longquery_handler(TDSLOGIN * tds_login, void (*longquery_func) (void *param), void *longquery_param);
    01109 int tds_init_write_buf(TDSSOCKET * tds);
    01110 void tds_free_result_info(TDSRESULTINFO * info);
    01111 void tds_free_socket(TDSSOCKET * tds);
    01112 void tds_free_connection(TDSCONNECTION * connection);
    01113 void tds_free_all_results(TDSSOCKET * tds);
    01114 void tds_free_results(TDSRESULTINFO * res_info);
    01115 void tds_free_param_results(TDSPARAMINFO * param_info);
    01116 void tds_free_msg(TDSMESSAGE * message);
    01117 void tds_free_cursor(TDSSOCKET * tds, TDS_INT cursor_id);
    01118 void tds_free_bcp_column_data(BCPCOLDATA * coldata);
    01119
    01120 int tds_put_n(TDSSOCKET * tds, const void *buf, int n);
    01121 int tds_put_string(TDSSOCKET * tds, const char *buf, int len);
    01122 int tds_put_int(TDSSOCKET * tds, TDS_INT i);
    01123 int tds_put_int8(TDSSOCKET * tds, TDS_INT8 i);
    01124 int tds_put_smallint(TDSSOCKET * tds, TDS_SMALLINT si);
    01126 #define tds_put_tinyint(tds, ti) tds_put_byte(tds,ti)
    01127 int tds_put_byte(TDSSOCKET * tds, unsigned char c);
    01128 TDSRESULTINFO *tds_alloc_results(int num_cols);
    01129 TDSCOMPUTEINFO **tds_alloc_compute_results(TDS_INT * num_comp_results, TDSCOMPUTEINFO ** ci, int num_cols, int by_cols);
    01130 TDSCONTEXT *tds_alloc_context(void);
    01131 void tds_free_context(TDSCONTEXT * locale);
    01132 TDSSOCKET *tds_alloc_socket(TDSCONTEXT * context, int bufsize);
    01133
    01134 /* config.c */
    01135 const TDS_COMPILETIME_SETTINGS *tds_get_compiletime_settings(void);
    01136 typedef void (*TDSCONFPARSE) (const char *option, const char *value, void *param);
    01137 int tds_read_conf_section(FILE * in, const char *section, TDSCONFPARSE tds_conf_parse, void *parse_param);
    01138 int tds_read_conf_file(TDSCONNECTION * connection, const char *server);
    01139 TDSCONNECTION *tds_read_config_info(TDSSOCKET * tds, TDSLOGIN * login, TDSLOCALE * locale);
    01140 void tds_fix_connection(TDSCONNECTION * connection);
    01141 void tds_config_verstr(const char *tdsver, TDSCONNECTION * connection);
    01142 void tds_lookup_host(const char *servername, char *ip);
    01143 int tds_set_interfaces_file_loc(const char *interfloc);
    01144
    01145 TDSLOCALE *tds_get_locale(void);
    01146 unsigned char *tds_alloc_row(TDSRESULTINFO * res_info);
    01147 unsigned char *tds_alloc_compute_row(TDSCOMPUTEINFO * res_info);
    01148 BCPCOLDATA * tds_alloc_bcp_column_data(int column_size);
    01149 int tds_alloc_get_string(TDSSOCKET * tds, char **string, int len);
    01150 void tds_set_null(unsigned char *current_row, int column);
    01151 void tds_clr_null(unsigned char *current_row, int column);
    01152 int tds_get_null(unsigned char *current_row, int column);
    01153 unsigned char *tds7_crypt_pass(const unsigned char *clear_pass, int len, unsigned char *crypt_pass);
    01154 TDSDYNAMIC *tds_lookup_dynamic(TDSSOCKET * tds, char *id);
    01155 const char *tds_prtype(int token);
    01156
    01157
    01158
    01159 /* iconv.c */
    01160 void tds_iconv_open(TDSSOCKET * tds, const char *charset);
    01161 void tds_iconv_close(TDSSOCKET * tds);
    01162 void tds_srv_charset_changed(TDSSOCKET * tds, const char *charset);
    01163 void tds7_srv_charset_changed(TDSSOCKET * tds, int sql_collate, int lcid);
    01164 int tds_iconv_alloc(TDSSOCKET * tds);
    01165 void tds_iconv_free(TDSSOCKET * tds);
    01166 TDSICONV *tds_iconv_from_collate(TDSSOCKET * tds, int sql_collate, int lcid);
    01167
    01168 /* threadsafe.c */
    01169 char *tds_timestamp_str(char *str, int maxlen);
    01170 struct hostent *tds_gethostbyname_r(const char *servername, struct hostent *result, char *buffer, int buflen, int *h_errnop);
    01171 struct hostent *tds_gethostbyaddr_r(const char *addr, int len, int type, struct hostent *result, char *buffer, int buflen,
    01172                                     int *h_errnop);
    01173 struct servent *tds_getservbyname_r(const char *name, const char *proto, struct servent *result, char *buffer, int buflen);
    01174 char *tds_get_homedir(void);
    01175
    01176 /* mem.c */
    01177 TDSPARAMINFO *tds_alloc_param_result(TDSPARAMINFO * old_param);
    01178 void tds_free_input_params(TDSDYNAMIC * dyn);
    01179 void tds_free_all_dynamic(TDSSOCKET * tds);
    01180 void tds_free_dynamic(TDSSOCKET * tds, TDSDYNAMIC * dyn);
    01181 TDSSOCKET *tds_realloc_socket(TDSSOCKET * tds, int bufsize);
    01182 void tds_free_compute_result(TDSCOMPUTEINFO * comp_info);
    01183 void tds_free_compute_results(TDSCOMPUTEINFO ** comp_info, TDS_INT num_comp);
    01184 unsigned char *tds_alloc_param_row(TDSPARAMINFO * info, TDSCOLUMN * curparam);
    01185 char *tds_alloc_client_sqlstate(int msgnum);
    01186 char *tds_alloc_lookup_sqlstate(TDSSOCKET * tds, int msgnum);
    01187 TDSLOGIN *tds_alloc_login(void);
    01188 TDSDYNAMIC *tds_alloc_dynamic(TDSSOCKET * tds, const char *id);
    01189 void tds_free_login(TDSLOGIN * login);
    01190 TDSCONNECTION *tds_alloc_connection(TDSLOCALE * locale);
    01191 TDSLOCALE *tds_alloc_locale(void);
    01192 void tds_free_locale(TDSLOCALE * locale);
    01193 TDSCURSOR * tds_alloc_cursor(TDSSOCKET * tds, const char *name, TDS_INT namelen, const char *query, TDS_INT querylen);
    01194
    01195 /* login.c */
    01196 int tds7_send_auth(TDSSOCKET * tds, const unsigned char *challenge);
    01197 void tds_set_packet(TDSLOGIN * tds_login, int packet_size);
    01198 void tds_set_port(TDSLOGIN * tds_login, int port);
    01199 void tds_set_passwd(TDSLOGIN * tds_login, const char *password);
    01200 void tds_set_bulk(TDSLOGIN * tds_login, TDS_TINYINT enabled);
    01201 void tds_set_user(TDSLOGIN * tds_login, const char *username);
    01202 void tds_set_app(TDSLOGIN * tds_login, const char *application);
    01203 void tds_set_host(TDSLOGIN * tds_login, const char *hostname);
    01204 void tds_set_library(TDSLOGIN * tds_login, const char *library);
    01205 void tds_set_server(TDSLOGIN * tds_login, const char *server);
    01206 void tds_set_client_charset(TDSLOGIN * tds_login, const char *charset);
    01207 void tds_set_language(TDSLOGIN * tds_login, const char *language);
    01208 void tds_set_version(TDSLOGIN * tds_login, short major_ver, short minor_ver);
    01209 void tds_set_capabilities(TDSLOGIN * tds_login, unsigned char *capabilities, int size);
    01210 int tds_connect(TDSSOCKET * tds, TDSCONNECTION * connection);
    01211
    01212 /* query.c */
    01213 int tds_submit_query(TDSSOCKET * tds, const char *query);
    01214 int tds_submit_query_params(TDSSOCKET * tds, const char *query, TDSPARAMINFO * params);
    01215 int tds_submit_queryf(TDSSOCKET * tds, const char *queryf, ...);
    01216 int tds_submit_prepare(TDSSOCKET * tds, const char *query, const char *id, TDSDYNAMIC ** dyn_out, TDSPARAMINFO * params);
    01217 int tds_submit_execdirect(TDSSOCKET * tds, const char *query, TDSPARAMINFO * params);
    01218 int tds_submit_execute(TDSSOCKET * tds, TDSDYNAMIC * dyn);
    01219 int tds_send_cancel(TDSSOCKET * tds);
    01220 const char *tds_next_placeholders(const char *start);
    01221 int tds_count_placeholders(const char *query);
    01222 int tds_get_dynid(TDSSOCKET * tds, char **id);
    01223 int tds_submit_unprepare(TDSSOCKET * tds, TDSDYNAMIC * dyn);
    01224 int tds_submit_rpc(TDSSOCKET * tds, const char *rpc_name, TDSPARAMINFO * params);
    01225 int tds_quote_id(TDSSOCKET * tds, char *buffer, const char *id, int idlen);
    01226 int tds_quote_string(TDSSOCKET * tds, char *buffer, const char *str, int len);
    01227 const char *tds_skip_quoted(const char *s);
    01228 int tds_cursor_declare(TDSSOCKET * tds, TDS_INT cursor_id, int *send);
    01229 int tds_cursor_setrows(TDSSOCKET * tds, TDS_INT cursor_id, int *send);
    01230 int tds_cursor_open(TDSSOCKET * tds, TDS_INT cursor_id, int *send);
    01231 int tds_cursor_fetch(TDSSOCKET * tds, TDS_INT client_cursor_id);
    01232 int tds_cursor_close(TDSSOCKET * tds, TDS_INT cursor_id);
    01233 int tds_cursor_dealloc(TDSSOCKET * tds, TDS_INT cursor_id);
    01234
    01235 /* token.c */
    01236 int tds_process_cancel(TDSSOCKET * tds);
    01237 void tds_swap_datatype(int coltype, unsigned char *buf);
    01238 int tds_get_token_size(int marker);
    01239 int tds_process_login_tokens(TDSSOCKET * tds);
    01240 void tds_add_row_column_size(TDSRESULTINFO * info, TDSCOLUMN * curcol);
    01241 int tds_process_simple_query(TDSSOCKET * tds);
    01242 int tds5_send_optioncmd(TDSSOCKET * tds, TDS_OPTION_CMD tds_command, TDS_OPTION tds_option, TDS_OPTION_ARG * tds_argument,
    01243                         TDS_INT * tds_argsize);
    01244 int tds_process_result_tokens(TDSSOCKET * tds, TDS_INT * result_type, int *done_flags);
    01245 int tds_process_row_tokens(TDSSOCKET * tds, TDS_INT * rowtype, TDS_INT * computeid);
    01246 int tds_process_row_tokens_ct(TDSSOCKET * tds, TDS_INT * rowtype, TDS_INT *computeid);
    01247 int tds_process_trailing_tokens(TDSSOCKET * tds);
    01248 int tds_client_msg(TDSCONTEXT * tds_ctx, TDSSOCKET * tds, int msgnum, int level, int state, int line, const char *message);
    01249 int tds_do_until_done(TDSSOCKET * tds);
    01250
    01251 /* data.c */
    01252 void tds_set_param_type(TDSSOCKET * tds, TDSCOLUMN * curcol, TDS_SERVER_TYPE type);
    01253 void tds_set_column_type(TDSCOLUMN * curcol, int type);
    01254
    01255
    01256 /* tds_convert.c */
    01257 TDS_INT tds_datecrack(TDS_INT datetype, const void *di, TDSDATEREC * dr);
    01258 int tds_get_conversion_type(int srctype, int colsize);
    01259 extern const char tds_hex_digits[];
    01260
    01261 /* write.c */
    01262 int tds_flush_packet(TDSSOCKET * tds);
    01263 int tds_put_buf(TDSSOCKET * tds, const unsigned char *buf, int dsize, int ssize);
    01264
    01265 /* read.c */
    01266 unsigned char tds_get_byte(TDSSOCKET * tds);
    01267 void tds_unget_byte(TDSSOCKET * tds);
    01268 unsigned char tds_peek(TDSSOCKET * tds);
    01269 TDS_SMALLINT tds_get_smallint(TDSSOCKET * tds);
    01270 TDS_INT tds_get_int(TDSSOCKET * tds);
    01271 int tds_get_string(TDSSOCKET * tds, int string_len, char *dest, size_t dest_size);
    01272 int tds_get_char_data(TDSSOCKET * tds, char *dest, size_t wire_size, TDSCOLUMN * curcol);
    01273 void *tds_get_n(TDSSOCKET * tds, void *dest, int n);
    01274 int tds_get_size_by_type(int servertype);
    01275 int tds_read_packet(TDSSOCKET * tds);
    01276
    01277 /* util.c */
    01278 void tds_set_parent(TDSSOCKET * tds, void *the_parent);
    01279 void *tds_get_parent(TDSSOCKET * tds);
    01280 void tds_ctx_set_parent(TDSCONTEXT * ctx, void *the_parent);
    01281 void *tds_ctx_get_parent(TDSCONTEXT * ctx);
    01282 int tds_swap_bytes(unsigned char *buf, int bytes);
    01283 int tds_version(TDSSOCKET * tds_socket, char *pversion_string);
    01284 void tdsdump_off(void);
    01285 void tdsdump_on(void);
    01286 int tdsdump_open(const char *filename);
    01287 int tdsdump_append(void);
    01288 void tdsdump_close(void);
    01289 void tdsdump_dump_buf(int debug_lvl, const char *msg, const void *buf, int length);
    01290 void tdsdump_log(int dbg_lvl, const char *fmt, ...)
    01291 #if defined(__GNUC__) && __GNUC__ >= 2
    01292         __attribute__ ((__format__ (__printf__, 2, 3)))
    01293 #endif
    01294 ;
    01295 int tds_close_socket(TDSSOCKET * tds);
    01296
    01297 /* vstrbuild.c */
    01298 int tds_vstrbuild(char *buffer, int buflen, int *resultlen, char *text, int textlen, const char *formats, int formatlen,
    01299                   va_list ap);
    01300
    01301 /* numeric.c */
    01302 char *tds_money_to_string(const TDS_MONEY * money, char *s);
    01303 TDS_INT tds_numeric_to_string(const TDS_NUMERIC * numeric, char *s);
    01304
    01305 /* getmac.c */
    01306 void tds_getmac(int s, unsigned char mac[6]);
    01307
    01308 typedef struct tds_answer
    01309 {
    01310         unsigned char lm_resp[24];
    01311         unsigned char nt_resp[24];
    01312 } TDSANSWER;
    01313 void tds_answer_challenge(const char *passwd, const unsigned char *challenge, TDSANSWER * answer);
    01314
    01315 #define IS_TDS42(x) (x->major_version==4 && x->minor_version==2)
    01316 #define IS_TDS46(x) (x->major_version==4 && x->minor_version==6)
    01317 #define IS_TDS50(x) (x->major_version==5 && x->minor_version==0)
    01318 #define IS_TDS70(x) (x->major_version==7 && x->minor_version==0)
    01319 #define IS_TDS80(x) (x->major_version==8 && x->minor_version==0)
    01320
    01321 #define IS_TDS7_PLUS(x) ( IS_TDS70(x) || IS_TDS80(x) )
    01322
    01323 #define IS_TDSDEAD(x) (((x) == NULL) || TDS_IS_SOCKET_INVALID((x)->s))
    01324
    01326 #define TDS_IS_SYBASE(x) (!(x->product_version & 0x80000000u))
    01327
    01328 #define TDS_IS_MSSQL(x) ((x->product_version & 0x80000000u)!=0)
    01329
    01333 #define TDS_MS_VER(maj,min,x) (0x80000000u|((maj)<<24)|((min)<<16)|(x))
    01334
    01335 /* TODO test if not similar to ms one*/
    01337 #define TDS_SYB_VER(maj,min,x) (((maj)<<24)|((min)<<16)|(x)<<8)
    01338
    01339 #ifdef __cplusplus
    01340 #if 0
    01341 {
    01342 #endif
    01343 }
    01344 #endif
    01345
    01346 #endif /* _tds_h_ */