Main Page | Modules | Class List | Directories | File List | Class 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, 2005  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.245.2.1 2006/04/16 08:08:38 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_SMALLINT 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 #define TDS_CANCELLED        3
    00190
    00191 #define TDS_INT_EXIT 0
    00192 #define TDS_INT_CONTINUE 1
    00193 #define TDS_INT_CANCEL 2
    00194 #define TDS_INT_TIMEOUT 3
    00195
    00196
    00197 #define TDS_NO_COUNT         -1
    00198
    00199 #define TDS_ROW_RESULT        4040
    00200 #define TDS_PARAM_RESULT      4042
    00201 #define TDS_STATUS_RESULT     4043
    00202 #define TDS_MSG_RESULT        4044
    00203 #define TDS_COMPUTE_RESULT    4045
    00204 #define TDS_CMD_DONE          4046
    00205 #define TDS_CMD_SUCCEED       4047
    00206 #define TDS_CMD_FAIL          4048
    00207 #define TDS_ROWFMT_RESULT     4049
    00208 #define TDS_COMPUTEFMT_RESULT 4050
    00209 #define TDS_DESCRIBE_RESULT   4051
    00210 #define TDS_DONE_RESULT       4052
    00211 #define TDS_DONEPROC_RESULT   4053
    00212 #define TDS_DONEINPROC_RESULT 4054
    00213 #define TDS_OTHERS_RESULT     4055
    00214
    00215 enum tds_token_results
    00216 {
    00217         TDS_TOKEN_RES_OTHERS,
    00218         TDS_TOKEN_RES_ROWFMT,
    00219         TDS_TOKEN_RES_COMPUTEFMT,
    00220         TDS_TOKEN_RES_PARAMFMT,
    00221         TDS_TOKEN_RES_DONE,
    00222         TDS_TOKEN_RES_ROW,
    00223         TDS_TOKEN_RES_COMPUTE,
    00224         TDS_TOKEN_RES_PROC,
    00225         TDS_TOKEN_RES_MSG
    00226 };
    00227
    00228 #define TDS_TOKEN_FLAG(flag) TDS_RETURN_##flag = (1 << (TDS_TOKEN_RES_##flag*2)), TDS_STOPAT_##flag = (2 << (TDS_TOKEN_RES_##flag*2))
    00229
    00230 enum tds_token_flags
    00231 {
    00232         TDS_HANDLE_ALL = 0,
    00233         TDS_TOKEN_FLAG(OTHERS),
    00234         TDS_TOKEN_FLAG(ROWFMT),
    00235         TDS_TOKEN_FLAG(COMPUTEFMT),
    00236         TDS_TOKEN_FLAG(PARAMFMT),
    00237         TDS_TOKEN_FLAG(DONE),
    00238         TDS_TOKEN_FLAG(ROW),
    00239         TDS_TOKEN_FLAG(COMPUTE),
    00240         TDS_TOKEN_FLAG(PROC),
    00241         TDS_TOKEN_FLAG(MSG),
    00242         TDS_TOKEN_RESULTS = TDS_RETURN_ROWFMT|TDS_RETURN_COMPUTEFMT|TDS_RETURN_DONE|TDS_STOPAT_ROW|TDS_STOPAT_COMPUTE|TDS_RETURN_PROC,
    00243         TDS_TOKEN_TRAILING = TDS_STOPAT_ROWFMT|TDS_STOPAT_COMPUTEFMT|TDS_STOPAT_ROW|TDS_STOPAT_COMPUTE|TDS_STOPAT_MSG|TDS_STOPAT_OTHERS
    00244 };
    00245
    00246 enum tds_end
    00247 {
    00248           TDS_DONE_FINAL        = 0x00  /* final result set, command completed successfully. */
    00249         , TDS_DONE_MORE_RESULTS = 0x01  /* more results follow */
    00250         , TDS_DONE_ERROR        = 0x02  /* error occurred */
    00251         , TDS_DONE_INXACT       = 0x04  /* transaction in progress */
    00252         , TDS_DONE_PROC         = 0x08  /* results are from a stored procedure */
    00253         , TDS_DONE_COUNT        = 0x10  /* count field in packet is valid */
    00254         , TDS_DONE_CANCELLED    = 0x20  /* acknowledging an attention command (usually a cancel) */
    00255         , TDS_DONE_EVENT        = 0x40  /* part of an event notification. */
    00256         , TDS_DONE_SRVERROR     = 0x100 /* SQL server server error */
    00257
    00258         /* after the above flags, a TDS_DONE packet has a field describing the state of the transaction */
    00259         , TDS_DONE_NO_TRAN      = 0     /* No transaction in effect */
    00260         , TDS_DONE_TRAN_SUCCEED = 1     /* Transaction completed successfully */
    00261         , TDS_DONE_TRAN_PROGRESS= 2     /* Transaction in progress */
    00262         , TDS_DONE_STMT_ABORT   = 3     /* A statement aborted */
    00263         , TDS_DONE_TRAN_ABORT   = 4     /* Transaction aborted */
    00264 };
    00265
    00266 /*
    00267  * TDS_ERROR indicates a successful processing, but that a TDS_ERROR_TOKEN or TDS_EED_TOKEN error was encountered.
    00268  * TDS_FAIL indicates an unrecoverable failure.
    00269  */
    00270 #define TDS_ERROR            3
    00271 #define TDS_DONT_RETURN      42
    00272
    00273 #define TDS5_PARAMFMT2_TOKEN       32   /* 0x20 */
    00274 #define TDS_LANGUAGE_TOKEN         33   /* 0x21    TDS 5.0 only              */
    00275 #define TDS_ORDERBY2_TOKEN         34   /* 0x22 */
    00276 #define TDS_ROWFMT2_TOKEN          97   /* 0x61    TDS 5.0 only              */
    00277 #define TDS_LOGOUT_TOKEN          113   /* 0x71    TDS 5.0 only? ct_close()  */
    00278 #define TDS_RETURNSTATUS_TOKEN    121   /* 0x79                              */
    00279 #define TDS_PROCID_TOKEN          124   /* 0x7C    TDS 4.2 only - TDS_PROCID */
    00280 #define TDS7_RESULT_TOKEN         129   /* 0x81    TDS 7.0 only              */
    00281 #define TDS7_COMPUTE_RESULT_TOKEN 136   /* 0x88    TDS 7.0 only              */
    00282 #define TDS_COLNAME_TOKEN         160   /* 0xA0    TDS 4.2 only              */
    00283 #define TDS_COLFMT_TOKEN          161   /* 0xA1    TDS 4.2 only - TDS_COLFMT */
    00284 #define TDS_DYNAMIC2_TOKEN        163   /* 0xA3 */
    00285 #define TDS_TABNAME_TOKEN         164   /* 0xA4 */
    00286 #define TDS_COLINFO_TOKEN         165   /* 0xA5 */
    00287 #define TDS_OPTIONCMD_TOKEN       166   /* 0xA6 */
    00288 #define TDS_COMPUTE_NAMES_TOKEN   167   /* 0xA7 */
    00289 #define TDS_COMPUTE_RESULT_TOKEN  168   /* 0xA8 */
    00290 #define TDS_ORDERBY_TOKEN         169   /* 0xA9    TDS_ORDER                 */
    00291 #define TDS_ERROR_TOKEN           170   /* 0xAA                              */
    00292 #define TDS_INFO_TOKEN            171   /* 0xAB                              */
    00293 #define TDS_PARAM_TOKEN           172   /* 0xAC    RETURNVALUE?              */
    00294 #define TDS_LOGINACK_TOKEN        173   /* 0xAD                              */
    00295 #define TDS_CONTROL_TOKEN         174   /* 0xAE    TDS_CONTROL               */
    00296 #define TDS_ROW_TOKEN             209   /* 0xD1                              */
    00297 #define TDS_CMP_ROW_TOKEN         211   /* 0xD3                              */
    00298 #define TDS5_PARAMS_TOKEN         215   /* 0xD7    TDS 5.0 only              */
    00299 #define TDS_CAPABILITY_TOKEN      226   /* 0xE2                              */
    00300 #define TDS_ENVCHANGE_TOKEN       227   /* 0xE3                              */
    00301 #define TDS_EED_TOKEN             229   /* 0xE5                              */
    00302 #define TDS_DBRPC_TOKEN           230   /* 0xE6                              */
    00303 #define TDS5_DYNAMIC_TOKEN        231   /* 0xE7    TDS 5.0 only              */
    00304 #define TDS5_PARAMFMT_TOKEN       236   /* 0xEC    TDS 5.0 only              */
    00305 #define TDS_AUTH_TOKEN            237   /* 0xED                              */
    00306 #define TDS_RESULT_TOKEN          238   /* 0xEE                              */
    00307 #define TDS_DONE_TOKEN            253   /* 0xFD    TDS_DONE                  */
    00308 #define TDS_DONEPROC_TOKEN        254   /* 0xFE    TDS_DONEPROC              */
    00309 #define TDS_DONEINPROC_TOKEN      255   /* 0xFF    TDS_DONEINPROC            */
    00310
    00311 /* CURSOR support: TDS 5.0 only*/
    00312 #define TDS_CURCLOSE_TOKEN        128  /* 0x80    TDS 5.0 only              */
    00313 #define TDS_CURFETCH_TOKEN        130  /* 0x82    TDS 5.0 only              */
    00314 #define TDS_CURINFO_TOKEN         131  /* 0x83    TDS 5.0 only              */
    00315 #define TDS_CUROPEN_TOKEN         132  /* 0x84    TDS 5.0 only              */
    00316 #define TDS_CURDECLARE_TOKEN      134  /* 0x86    TDS 5.0 only              */
    00317
    00318 enum {
    00319         TDS_CUR_ISTAT_UNUSED    = 0x00,
    00320         TDS_CUR_ISTAT_DECLARED  = 0x01,
    00321         TDS_CUR_ISTAT_OPEN      = 0x02,
    00322         TDS_CUR_ISTAT_CLOSED    = 0x04,
    00323         TDS_CUR_ISTAT_RDONLY    = 0x08,
    00324         TDS_CUR_ISTAT_UPDATABLE = 0x10,
    00325         TDS_CUR_ISTAT_ROWCNT    = 0x20,
    00326         TDS_CUR_ISTAT_DEALLOC   = 0x40
    00327 };
    00328
    00329 /*
    00330  * Cursor Declare, SetRows, Open and Close all return 0x83 token.
    00331  * But only SetRows includes the rowcount (4 byte) in the stream.
    00332  * So for Setrows we read the rowcount from the stream and not for others.
    00333  * These values are useful to determine when to read the rowcount from the packet
    00334  */
    00335 #define IS_DECLARE  100
    00336 #define IS_CURROW   200
    00337 #define IS_OPEN     300
    00338 #define IS_CLOSE    400
    00339
    00340 /* states for tds_process_messages() */
    00341 #define PROCESS_ROWS    0
    00342 #define PROCESS_RESULTS 1
    00343 #define CANCEL_PROCESS  2
    00344 #define GOTO_1ST_ROW    3
    00345 #define LOGIN           4
    00346
    00347 /* environment type field */
    00348 #define TDS_ENV_DATABASE        1
    00349 #define TDS_ENV_LANG            2
    00350 #define TDS_ENV_CHARSET         3
    00351 #define TDS_ENV_PACKSIZE        4
    00352 #define TDS_ENV_LCID            5
    00353 #define TDS_ENV_SQLCOLLATION    7
    00354
    00355 /* string types */
    00356 #define TDS_NULLTERM -9
    00357
    00358 /* Microsoft internal stored procedure id's */
    00359
    00360 #define TDS_SP_CURSOR           1
    00361 #define TDS_SP_CURSOROPEN       2
    00362 #define TDS_SP_CURSORPREPARE    3
    00363 #define TDS_SP_CURSOREXECUTE    4
    00364 #define TDS_SP_CURSORPREPEXEC   5
    00365 #define TDS_SP_CURSORUNPREPARE  6
    00366 #define TDS_SP_CURSORFETCH      7
    00367 #define TDS_SP_CURSOROPTION     8
    00368 #define TDS_SP_CURSORCLOSE      9
    00369 #define TDS_SP_EXECUTESQL      10
    00370 #define TDS_SP_PREPARE         11
    00371 #define TDS_SP_EXECUTE         12
    00372 #define TDS_SP_PREPEXEC        13
    00373 #define TDS_SP_PREPEXECRPC     14
    00374 #define TDS_SP_UNPREPARE       15
    00375 /*
    00376  * <rant> Sybase does an awful job of this stuff, non null ints of size 1 2
    00377  * and 4 have there own codes but nullable ints are lumped into INTN
    00378  * sheesh! </rant>
    00379  */
    00380 typedef enum
    00381 {
    00382         SYBCHAR = 47,           /* 0x2F */
    00383 #define SYBCHAR SYBCHAR
    00384         SYBVARCHAR = 39,        /* 0x27 */
    00385 #define SYBVARCHAR      SYBVARCHAR
    00386         SYBINTN = 38,           /* 0x26 */
    00387 #define SYBINTN SYBINTN
    00388         SYBINT1 = 48,           /* 0x30 */
    00389 #define SYBINT1 SYBINT1
    00390         SYBINT2 = 52,           /* 0x34 */
    00391 #define SYBINT2 SYBINT2
    00392         SYBINT4 = 56,           /* 0x38 */
    00393 #define SYBINT4 SYBINT4
    00394         SYBINT8 = 127,          /* 0x7F */
    00395 #define SYBINT8 SYBINT8
    00396         SYBFLT8 = 62,           /* 0x3E */
    00397 #define SYBFLT8 SYBFLT8
    00398         SYBDATETIME = 61,       /* 0x3D */
    00399 #define SYBDATETIME     SYBDATETIME
    00400         SYBBIT = 50,            /* 0x32 */
    00401 #define SYBBIT  SYBBIT
    00402         SYBTEXT = 35,           /* 0x23 */
    00403 #define SYBTEXT SYBTEXT
    00404         SYBNTEXT = 99,          /* 0x63 */
    00405 #define SYBNTEXT        SYBNTEXT
    00406         SYBIMAGE = 34,          /* 0x22 */
    00407 #define SYBIMAGE        SYBIMAGE
    00408         SYBMONEY4 = 122,        /* 0x7A */
    00409 #define SYBMONEY4       SYBMONEY4
    00410         SYBMONEY = 60,          /* 0x3C */
    00411 #define SYBMONEY        SYBMONEY
    00412         SYBDATETIME4 = 58,      /* 0x3A */
    00413 #define SYBDATETIME4    SYBDATETIME4
    00414         SYBREAL = 59,           /* 0x3B */
    00415 #define SYBREAL SYBREAL
    00416         SYBBINARY = 45,         /* 0x2D */
    00417 #define SYBBINARY       SYBBINARY
    00418         SYBVOID = 31,           /* 0x1F */
    00419 #define SYBVOID SYBVOID
    00420         SYBVARBINARY = 37,      /* 0x25 */
    00421 #define SYBVARBINARY    SYBVARBINARY
    00422         SYBNVARCHAR = 103,      /* 0x67 */
    00423 #define SYBNVARCHAR     SYBNVARCHAR
    00424         SYBBITN = 104,          /* 0x68 */
    00425 #define SYBBITN SYBBITN
    00426         SYBNUMERIC = 108,       /* 0x6C */
    00427 #define SYBNUMERIC      SYBNUMERIC
    00428         SYBDECIMAL = 106,       /* 0x6A */
    00429 #define SYBDECIMAL      SYBDECIMAL
    00430         SYBFLTN = 109,          /* 0x6D */
    00431 #define SYBFLTN SYBFLTN
    00432         SYBMONEYN = 110,        /* 0x6E */
    00433 #define SYBMONEYN       SYBMONEYN
    00434         SYBDATETIMN = 111,      /* 0x6F */
    00435 #define SYBDATETIMN     SYBDATETIMN
    00436         XSYBCHAR = 175,         /* 0xAF */
    00437 #define XSYBCHAR        XSYBCHAR
    00438         XSYBVARCHAR = 167,      /* 0xA7 */
    00439 #define XSYBVARCHAR     XSYBVARCHAR
    00440         XSYBNVARCHAR = 231,     /* 0xE7 */
    00441 #define XSYBNVARCHAR    XSYBNVARCHAR
    00442         XSYBNCHAR = 239,        /* 0xEF */
    00443 #define XSYBNCHAR       XSYBNCHAR
    00444         XSYBVARBINARY = 165,    /* 0xA5 */
    00445 #define XSYBVARBINARY   XSYBVARBINARY
    00446         XSYBBINARY = 173,       /* 0xAD */
    00447 #define XSYBBINARY      XSYBBINARY
    00448         SYBLONGBINARY = 225,    /* 0xE1 */
    00449 #define SYBLONGBINARY   SYBLONGBINARY
    00450         SYBSINT1 = 64,          /* 0x40 */
    00451 #define SYBSINT1        SYBSINT1
    00452         SYBUINT2 = 65,          /* 0x41 */
    00453 #define SYBUINT2        SYBUINT2
    00454         SYBUINT4 = 66,          /* 0x42 */
    00455 #define SYBUINT4        SYBUINT4
    00456         SYBUINT8 = 67,          /* 0x43 */
    00457 #define SYBUINT8        SYBUINT8
    00458
    00459         SYBUNIQUE = 36,         /* 0x24 */
    00460 #define SYBUNIQUE       SYBUNIQUE
    00461         SYBVARIANT = 98         /* 0x62 */
    00462 #define SYBVARIANT      SYBVARIANT
    00463 } TDS_SERVER_TYPE;
    00464
    00465
    00466 typedef enum
    00467 {
    00468         USER_UNICHAR_TYPE = 34,         /* 0x22 */
    00469         USER_UNIVARCHAR_TYPE = 35       /* 0x23 */
    00470 } TDS_USER_TYPE;
    00471
    00472 #define SYBAOPCNT  0x4b
    00473 #define SYBAOPCNTU 0x4c
    00474 #define SYBAOPSUM  0x4d
    00475 #define SYBAOPSUMU 0x4e
    00476 #define SYBAOPAVG  0x4f
    00477 #define SYBAOPAVGU 0x50
    00478 #define SYBAOPMIN  0x51
    00479 #define SYBAOPMAX  0x52
    00480
    00481 /* mssql2k compute operator */
    00482 #define SYBAOPCNT_BIG           0x09
    00483 #define SYBAOPSTDEV             0x30
    00484 #define SYBAOPSTDEVP            0x31
    00485 #define SYBAOPVAR               0x32
    00486 #define SYBAOPVARP              0x33
    00487 #define SYBAOPCHECKSUM_AGG      0x72
    00488
    00489
    00493 typedef enum
    00494 {
    00495           TDS_OPT_SET = 1       /* Set an option. */
    00496         , TDS_OPT_DEFAULT = 2   /* Set option to its default value. */
    00497         , TDS_OPT_LIST = 3      /* Request current setting of a specific option. */
    00498         , TDS_OPT_INFO = 4      /* Report current setting of a specific option. */
    00499 } TDS_OPTION_CMD;
    00500
    00501 typedef enum
    00502 {
    00503           TDS_OPT_DATEFIRST = 1         /* 0x01 */
    00504         , TDS_OPT_TEXTSIZE = 2          /* 0x02 */
    00505         , TDS_OPT_STAT_TIME = 3         /* 0x03 */
    00506         , TDS_OPT_STAT_IO = 4           /* 0x04 */
    00507         , TDS_OPT_ROWCOUNT = 5          /* 0x05 */
    00508         , TDS_OPT_NATLANG = 6           /* 0x06 */
    00509         , TDS_OPT_DATEFORMAT = 7        /* 0x07 */
    00510         , TDS_OPT_ISOLATION = 8         /* 0x08 */
    00511         , TDS_OPT_AUTHON = 9            /* 0x09 */
    00512         , TDS_OPT_CHARSET = 10          /* 0x0a */
    00513         , TDS_OPT_SHOWPLAN = 13         /* 0x0d */
    00514         , TDS_OPT_NOEXEC = 14           /* 0x0e */
    00515         , TDS_OPT_ARITHIGNOREON = 15    /* 0x0f */
    00516         , TDS_OPT_ARITHABORTON = 17     /* 0x11 */
    00517         , TDS_OPT_PARSEONLY = 18        /* 0x12 */
    00518         , TDS_OPT_GETDATA = 20          /* 0x14 */
    00519         , TDS_OPT_NOCOUNT = 21          /* 0x15 */
    00520         , TDS_OPT_FORCEPLAN = 23        /* 0x17 */
    00521         , TDS_OPT_FORMATONLY = 24       /* 0x18 */
    00522         , TDS_OPT_CHAINXACTS = 25       /* 0x19 */
    00523         , TDS_OPT_CURCLOSEONXACT = 26   /* 0x1a */
    00524         , TDS_OPT_FIPSFLAG = 27         /* 0x1b */
    00525         , TDS_OPT_RESTREES = 28         /* 0x1c */
    00526         , TDS_OPT_IDENTITYON = 29       /* 0x1d */
    00527         , TDS_OPT_CURREAD = 30          /* 0x1e */
    00528         , TDS_OPT_CURWRITE = 31         /* 0x1f */
    00529         , TDS_OPT_IDENTITYOFF = 32      /* 0x20 */
    00530         , TDS_OPT_AUTHOFF = 33          /* 0x21 */
    00531         , TDS_OPT_ANSINULL = 34         /* 0x22 */
    00532         , TDS_OPT_QUOTED_IDENT = 35     /* 0x23 */
    00533         , TDS_OPT_ARITHIGNOREOFF = 36   /* 0x24 */
    00534         , TDS_OPT_ARITHABORTOFF = 37    /* 0x25 */
    00535         , TDS_OPT_TRUNCABORT = 38       /* 0x26 */
    00536 } TDS_OPTION;
    00537
    00538 typedef union tds_option_arg
    00539 {
    00540         TDS_TINYINT ti;
    00541         TDS_INT i;
    00542         TDS_CHAR *c;
    00543 } TDS_OPTION_ARG;
    00544
    00545 enum {
    00546         TDS_OPT_ARITHOVERFLOW = 0x01,
    00547         TDS_OPT_NUMERICTRUNC = 0x02
    00548 };
    00549
    00550 enum TDS_OPT_DATEFIRST_CHOICE
    00551 {
    00552         TDS_OPT_MONDAY = 1, TDS_OPT_TUESDAY = 2, TDS_OPT_WEDNESDAY = 3, TDS_OPT_THURSDAY = 4, TDS_OPT_FRIDAY = 5, TDS_OPT_SATURDAY =
    00553                 6, TDS_OPT_SUNDAY = 7
    00554 };
    00555
    00556 enum TDS_OPT_DATEFORMAT_CHOICE
    00557 {
    00558         TDS_OPT_FMTMDY = 1, TDS_OPT_FMTDMY = 2, TDS_OPT_FMTYMD = 3, TDS_OPT_FMTYDM = 4, TDS_OPT_FMTMYD = 5, TDS_OPT_FMTDYM = 6
    00559 };
    00560 enum TDS_OPT_ISOLATION_CHOICE
    00561 {
    00562         TDS_OPT_LEVEL1 = 1, TDS_OPT_LEVEL3 = 3
    00563 };
    00564
    00565 typedef enum tds_packet_type
    00566 {
    00567         TDS_QUERY = 1,
    00568         TDS_RPC = 3,
    00569         TDS_CANCEL = 6,
    00570         TDS_BULK = 7,
    00571         TDS_NORMAL = 15
    00572 } TDS_PACKET_TYPE;
    00573
    00574
    00575 #define TDS_ZERO_FREE(x) do {free((x)); (x) = NULL;} while(0)
    00576 #define TDS_VECTOR_SIZE(x) (sizeof(x)/sizeof(x[0]))
    00577
    00578 /*
    00579  * TODO use system macros for optimization
    00580  * See mcrypt for reference and linux kernel source for optimization
    00581  * check if unaligned access and use fast write/read when implemented
    00582  */
    00583 #define TDS_BYTE_SWAP16(value)                 \
    00584          (((((unsigned short)value)<<8) & 0xFF00)   | \
    00585           ((((unsigned short)value)>>8) & 0x00FF))
    00586
    00587 #define TDS_BYTE_SWAP32(value)                     \
    00588          (((((unsigned long)value)<<24) & 0xFF000000)  | \
    00589           ((((unsigned long)value)<< 8) & 0x00FF0000)  | \
    00590           ((((unsigned long)value)>> 8) & 0x0000FF00)  | \
    00591           ((((unsigned long)value)>>24) & 0x000000FF))
    00592
    00593 #define is_end_token(x) (x==TDS_DONE_TOKEN    || \
    00594                         x==TDS_DONEPROC_TOKEN    || \
    00595                         x==TDS_DONEINPROC_TOKEN)
    00596
    00597 #define is_hard_end_token(x) (x==TDS_DONE_TOKEN    || \
    00598                         x==TDS_DONEPROC_TOKEN)
    00599
    00600 #define is_msg_token(x) (x==TDS_INFO_TOKEN    || \
    00601                         x==TDS_ERROR_TOKEN    || \
    00602                         x==TDS_EED_TOKEN)
    00603
    00604 #define is_result_token(x) (x==TDS_RESULT_TOKEN || \
    00605                         x==TDS_ROWFMT2_TOKEN    || \
    00606                         x==TDS7_RESULT_TOKEN    || \
    00607                         x==TDS_COLFMT_TOKEN     || \
    00608                         x==TDS_COLNAME_TOKEN    || \
    00609                         x==TDS_RETURNSTATUS_TOKEN)
    00610
    00611 /* FIX ME -- not a complete list */
    00612 #define is_fixed_type(x) (x==SYBINT1    || \
    00613                         x==SYBINT2      || \
    00614                         x==SYBINT4      || \
    00615                         x==SYBINT8      || \
    00616                         x==SYBREAL      || \
    00617                         x==SYBFLT8      || \
    00618                         x==SYBDATETIME  || \
    00619                         x==SYBDATETIME4 || \
    00620                         x==SYBBIT       || \
    00621                         x==SYBMONEY     || \
    00622                         x==SYBMONEY4    || \
    00623                         x==SYBVOID      || \
    00624                         x==SYBUNIQUE)
    00625 #define is_nullable_type(x) ( \
    00626                         x==SYBBITN      || \
    00627                      x==SYBINTN      || \
    00628                      x==SYBFLTN      || \
    00629                      x==SYBMONEYN    || \
    00630                      x==SYBDATETIMN  || \
    00631                      x==SYBVARCHAR   || \
    00632                      x==SYBBINARY    || \
    00633                      x==SYBVARBINARY || \
    00634                      x==SYBTEXT      || \
    00635                      x==SYBNTEXT     || \
    00636                      x==SYBIMAGE)
    00637
    00638 #define is_blob_type(x) (x==SYBTEXT || x==SYBIMAGE || x==SYBNTEXT)
    00639 /* large type means it has a two byte size field */
    00640 /* define is_large_type(x) (x>128) */
    00641 #define is_numeric_type(x) (x==SYBNUMERIC || x==SYBDECIMAL)
    00642 #define is_unicode_type(x) (x==XSYBNVARCHAR || x==XSYBNCHAR || x==SYBNTEXT)
    00643 #define is_collate_type(x) (x==XSYBVARCHAR || x==XSYBCHAR || x==SYBTEXT || x==XSYBNVARCHAR || x==XSYBNCHAR || x==SYBNTEXT)
    00644 #define is_ascii_type(x) ( x==XSYBCHAR || x==XSYBVARCHAR || x==SYBTEXT || x==SYBCHAR || x==SYBVARCHAR)
    00645 #define is_binary_type(x) (x==SYBLONGBINARY)
    00646 #define is_char_type(x) (is_unicode_type(x) || is_ascii_type(x))
    00647 #define is_similar_type(x, y) ((is_char_type(x) && is_char_type(y)) || ((is_unicode_type(x) && is_unicode_type(y))))
    00648
    00649
    00650 #define TDS_MAX_CAPABILITY      22
    00651 #define MAXPRECISION            80
    00652 #define TDS_MAX_CONN            4096
    00653 #define TDS_MAX_DYNID_LEN       30
    00654
    00655 /* defaults to use if no others are found */
    00656 #define TDS_DEF_SERVER          "SYBASE"
    00657 #define TDS_DEF_BLKSZ           512
    00658 #define TDS_DEF_CHARSET         "iso_1"
    00659 #define TDS_DEF_LANG            "us_english"
    00660 #if TDS42
    00661 #define TDS_DEF_MAJOR           4
    00662 #define TDS_DEF_MINOR           2
    00663 #define TDS_DEF_PORT            1433
    00664 #elif TDS46
    00665 #define TDS_DEF_MAJOR           4
    00666 #define TDS_DEF_MINOR           6
    00667 #define TDS_DEF_PORT            4000
    00668 #elif TDS70
    00669 #define TDS_DEF_MAJOR           7
    00670 #define TDS_DEF_MINOR           0
    00671 #define TDS_DEF_PORT            1433
    00672 #elif TDS80
    00673 #define TDS_DEF_MAJOR           8
    00674 #define TDS_DEF_MINOR           0
    00675 #define TDS_DEF_PORT            1433
    00676 #else
    00677 #define TDS_DEF_MAJOR           5
    00678 #define TDS_DEF_MINOR           0
    00679 #define TDS_DEF_PORT            4000
    00680 #endif
    00681
    00682 /* normalized strings from freetds.conf file */
    00683 #define TDS_STR_VERSION  "tds version"
    00684 #define TDS_STR_BLKSZ    "initial block size"
    00685 #define TDS_STR_SWAPDT   "swap broken dates"
    00686 #define TDS_STR_SWAPMNY  "swap broken money"
    00687 #define TDS_STR_DUMPFILE "dump file"
    00688 #define TDS_STR_DEBUGLVL "debug level"
    00689 #define TDS_STR_DEBUGFLAGS "debug flags"
    00690 #define TDS_STR_TIMEOUT  "timeout"
    00691 #define TDS_STR_QUERY_TIMEOUT  "query timeout"
    00692 #define TDS_STR_CONNTIMEOUT "connect timeout"
    00693 #define TDS_STR_HOSTNAME "hostname"
    00694 #define TDS_STR_HOST     "host"
    00695 #define TDS_STR_PORT     "port"
    00696 #define TDS_STR_TEXTSZ   "text size"
    00697 /* for big endian hosts */
    00698 #define TDS_STR_EMUL_LE "emulate little endian"
    00699 #define TDS_STR_CHARSET "charset"
    00700 #define TDS_STR_CLCHARSET       "client charset"
    00701 #define TDS_STR_LANGUAGE        "language"
    00702 #define TDS_STR_APPENDMODE      "dump file append"
    00703 #define TDS_STR_DATEFMT "date format"
    00704 #define TDS_STR_INSTANCE "instance"
    00705
    00706 /* TODO do a better check for alignment than this */
    00707 typedef union
    00708 {
    00709         void *p;
    00710         int i;
    00711 } tds_align_struct;
    00712
    00713 #define TDS_ALIGN_SIZE sizeof(tds_align_struct)
    00714
    00715 #define TDS_MAX_LOGIN_STR_SZ 30
    00716 typedef struct tds_login
    00717 {
    00718         DSTR server_name;
    00719         DSTR server_addr;
    00720         int port;
    00721         TDS_TINYINT major_version;      /* TDS version */
    00722         TDS_TINYINT minor_version;      /* TDS version */
    00723         int block_size;
    00724         DSTR language;          /* ie us-english */
    00725         DSTR server_charset;    /*  ie iso_1 */
    00726         TDS_INT connect_timeout;
    00727         DSTR host_name;
    00728         DSTR app_name;
    00729         DSTR user_name;
    00730         DSTR password;
    00731
    00732         DSTR library;   /* Ct-Library, DB-Library,  TDS-Library or ODBC */
    00733         TDS_TINYINT bulk_copy;
    00734         TDS_TINYINT suppress_language;
    00735         TDS_TINYINT encrypted;
    00736
    00737         TDS_INT query_timeout;
    00738         unsigned char capabilities[TDS_MAX_CAPABILITY];
    00739         DSTR client_charset;
    00740 } TDSLOGIN;
    00741
    00742 typedef struct tds_connection
    00743 {
    00744         /* first part of structure is the same of login one */
    00745         DSTR server_name;
    00746         int port;
    00747         TDS_TINYINT major_version;
    00748         TDS_TINYINT minor_version;
    00749         int block_size;
    00750         DSTR language;
    00751         DSTR server_charset;
    00752         TDS_INT connect_timeout;
    00753         DSTR host_name;
    00754         DSTR app_name;
    00755         DSTR user_name;
    00756         DSTR password;
    00757         DSTR library;
    00758         TDS_TINYINT bulk_copy;
    00759         TDS_TINYINT suppress_language;
    00760         TDS_TINYINT encrypted;
    00761
    00762         TDS_INT query_timeout;
    00763         unsigned char capabilities[TDS_MAX_CAPABILITY];
    00764         DSTR client_charset;
    00765
    00766         DSTR ip_addr;
    00767         DSTR instance_name;
    00768         DSTR database;
    00769         DSTR dump_file;
    00770         int debug_flags;
    00771         int text_size;
    00772         int broken_dates;
    00773         int broken_money;
    00774         int emul_little_endian;
    00775 } TDSCONNECTION;
    00776
    00777 typedef struct tds_locale
    00778 {
    00779         char *language;
    00780         char *server_charset;
    00781         char *client_charset;
    00782         char *date_fmt;
    00783 } TDSLOCALE;
    00784
    00789 typedef struct tds_blob
    00790 {
    00791         TDS_CHAR *textvalue;
    00792         TDS_CHAR textptr[16];
    00793         TDS_CHAR timestamp[8];
    00794 } TDSBLOB;
    00795
    00799 typedef struct
    00800 {
    00801         TDS_USMALLINT locale_id;        /* master..syslanguages.lcid */
    00802         TDS_USMALLINT flags;
    00803         TDS_UCHAR charset_id;           /* or zero */
    00804 } TDS8_COLLATION;
    00805
    00806 /* SF stands for "sort flag" */
    00807 #define TDS_SF_BIN                   (TDS_USMALLINT) 0x100
    00808 #define TDS_SF_WIDTH_INSENSITIVE     (TDS_USMALLINT) 0x080
    00809 #define TDS_SF_KATATYPE_INSENSITIVE  (TDS_USMALLINT) 0x040
    00810 #define TDS_SF_ACCENT_SENSITIVE      (TDS_USMALLINT) 0x020
    00811 #define TDS_SF_CASE_INSENSITIVE      (TDS_USMALLINT) 0x010
    00812
    00813 /* UT stands for user type */
    00814 #define TDS_UT_TIMESTAMP             80
    00815
    00816
    00821 typedef struct _tds_encoding
    00822 {
    00823         const char *name;
    00824         unsigned char min_bytes_per_char;
    00825         unsigned char max_bytes_per_char;
    00826 } TDS_ENCODING;
    00827
    00828 typedef struct _tds_bcpcoldata
    00829 {
    00830         TDS_UCHAR *data;
    00831         TDS_INT    datalen;
    00832         TDS_INT    null_column;
    00833 } BCPCOLDATA;
    00834
    00835
    00836 enum
    00837 { TDS_SYSNAME_SIZE = 512 };
    00838
    00842 typedef struct tds_column
    00843 {
    00844         TDS_SMALLINT column_type;
    00849         TDS_INT column_usertype;
    00850         TDS_INT column_flags;
    00851
    00852         TDS_INT column_size;
    00854         TDS_TINYINT column_varint_size;
    00856         TDS_TINYINT column_prec;
    00857         TDS_TINYINT column_scale;
    00859         TDS_SMALLINT column_namelen;
    00860         TDS_SMALLINT table_namelen;
    00861         struct
    00862         {
    00863                 TDS_SMALLINT column_type;
    00864                 TDS_INT column_size;
    00865         } on_server;
    00866
    00867         const TDSICONV *char_conv;
    00869         TDS_CHAR table_name[TDS_SYSNAME_SIZE];
    00870         TDS_CHAR column_name[TDS_SYSNAME_SIZE];
    00871
    00872         TDS_INT column_offset;
    00873         unsigned int column_nullable:1;
    00874         unsigned int column_writeable:1;
    00875         unsigned int column_identity:1;
    00876         unsigned int column_key:1;
    00877         unsigned int column_hidden:1;
    00878         unsigned int column_output:1;
    00879         unsigned int column_timestamp:1;
    00880         TDS_UCHAR column_collation[5];
    00881
    00882         /* additional fields flags for compute results */
    00883         TDS_TINYINT column_operator;
    00884         TDS_SMALLINT column_operand;
    00885
    00886         /* FIXME this is data related, not column */
    00888         TDS_INT column_cur_size;
    00889
    00890         /* related to binding or info stored by client libraries */
    00891         /* FIXME find a best place to store these data, some are unused */
    00892         TDS_SMALLINT column_bindtype;
    00893         TDS_SMALLINT column_bindfmt;
    00894         TDS_UINT column_bindlen;
    00895         TDS_SMALLINT *column_nullbind;
    00896         TDS_CHAR *column_varaddr;
    00897         TDS_INT *column_lenbind;
    00898         TDS_INT column_textpos;
    00899         TDS_INT column_text_sqlgetdatapos;
    00900
    00901         BCPCOLDATA *bcp_column_data;
    00910         TDS_INT bcp_prefix_len;
    00911         TDS_INT bcp_term_len;
    00912         TDS_CHAR *bcp_terminator;
    00913 } TDSCOLUMN;
    00914
    00915
    00917 typedef struct tds_result_info
    00918 {
    00919         /* TODO those fields can became a struct */
    00920         TDS_SMALLINT num_cols;
    00921         TDSCOLUMN **columns;
    00922         TDS_INT row_size;
    00923         TDS_INT ref_count;
    00924         unsigned char *current_row;
    00925
    00926         TDS_SMALLINT rows_exist;
    00927         /* TODO remove ?? used only in dblib */
    00928         TDS_INT row_count;
    00929         /* TODO remove ?? used only in dblib */
    00930         TDS_TINYINT more_results;
    00931         TDS_SMALLINT computeid;
    00932         TDS_SMALLINT *bycolumns;
    00933         TDS_SMALLINT by_cols;
    00934 } TDSRESULTINFO;
    00935
    00936 /* values for tds->state */
    00937 typedef enum _TDS_STATE
    00938 {
    00939         TDS_IDLE,
    00940         TDS_QUERYING,
    00941         TDS_PENDING,
    00942         TDS_READING,
    00943         TDS_DEAD
    00944 } TDS_STATE;
    00945
    00946 #define TDS_DBG_FUNC    __FILE__, ((__LINE__ << 4) | 7)
    00947 #define TDS_DBG_INFO2   __FILE__, ((__LINE__ << 4) | 6)
    00948 #define TDS_DBG_INFO1   __FILE__, ((__LINE__ << 4) | 5)
    00949 #define TDS_DBG_NETWORK __FILE__, ((__LINE__ << 4) | 4)
    00950 #define TDS_DBG_WARN    __FILE__, ((__LINE__ << 4) | 3)
    00951 #define TDS_DBG_ERROR   __FILE__, ((__LINE__ << 4) | 2)
    00952 #define TDS_DBG_SEVERE  __FILE__, ((__LINE__ << 4) | 1)
    00953
    00954 #define TDS_DBGFLAG_FUNC    0x80
    00955 #define TDS_DBGFLAG_INFO2   0x40
    00956 #define TDS_DBGFLAG_INFO1   0x20
    00957 #define TDS_DBGFLAG_NETWORK 0x10
    00958 #define TDS_DBGFLAG_WARN    0x08
    00959 #define TDS_DBGFLAG_ERROR   0x04
    00960 #define TDS_DBGFLAG_SEVERE  0x02
    00961 #define TDS_DBGFLAG_ALLLVL  0xfff
    00962 #define TDS_DBGFLAG_PID     0x1000
    00963 #define TDS_DBGFLAG_TIME    0x2000
    00964 #define TDS_DBGFLAG_SOURCE  0x4000
    00965 #define TDS_DBGFLAG_THREAD  0x8000
    00966
    00972 enum TDS_DBG_LOG_STATE
    00973 {
    00974           TDS_DBG_LOGIN = 1             /* for diagnosing login problems;
    00975                                            otherwise the username/password information is suppressed. */
    00976         , TDS_DBG_API =    (1 << 1)     /* Log calls to client libraries */
    00977         , TDS_DBG_ASYNC =  (1 << 2)     /* Log asynchronous function starts or completes. */
    00978         , TDS_DBG_DIAG =   (1 << 3)     /* Log client- and server-generated messages */
    00979         , TDS_DBG_error =  (1 << 4)
    00980         /* TODO:  ^^^^^ make upper case when old #defines (above) are removed */
    00981         /* Log FreeTDS runtime/logic error occurs. */
    00982         , TDS_DBG_PACKET = (1 << 5)     /* Log hex dump of packets to/from the server. */
    00983         , TDS_DBG_LIBTDS = (1 << 6)     /* Log calls to (and in) libtds */
    00984         , TDS_DBG_CONFIG = (1 << 7)     /* replaces TDSDUMPCONFIG */
    00985         , TDS_DBG_DEFAULT = 0xFE        /* all above except login packets */
    00986 };
    00987
    00988 typedef struct tds_result_info TDSCOMPUTEINFO;
    00989
    00990 typedef TDSRESULTINFO TDSPARAMINFO;
    00991
    00992 typedef struct tds_message
    00993 {
    00994         TDS_SMALLINT priv_msg_type;
    00995         TDS_SMALLINT line_number;
    00996         TDS_UINT msgno;
    00997         TDS_SMALLINT state;
    00998         TDS_SMALLINT severity;
    00999         TDS_CHAR *server;
    01000         TDS_CHAR *message;
    01001         TDS_CHAR *proc_name;
    01002         TDS_CHAR *sql_state;
    01003 } TDSMESSAGE;
    01004
    01005 typedef struct tds_upd_col
    01006 {
    01007         struct tds_upd_col *next;
    01008         TDS_INT colnamelength;
    01009         char * columnname;
    01010 } TDSUPDCOL;
    01011
    01012 typedef enum {
    01013           TDS_CURSOR_STATE_UNACTIONED = 0   /* initial value */
    01014         , TDS_CURSOR_STATE_REQUESTED = 1        /* called by ct_cursor */
    01015         , TDS_CURSOR_STATE_SENT = 2             /* sent to server */
    01016         , TDS_CURSOR_STATE_ACTIONED = 3         /* acknowledged by server */
    01017 } TDS_CURSOR_STATE;
    01018
    01019 typedef struct _tds_cursor_status
    01020 {
    01021         TDS_CURSOR_STATE declare;
    01022         TDS_CURSOR_STATE cursor_row;
    01023         TDS_CURSOR_STATE open;
    01024         TDS_CURSOR_STATE fetch;
    01025         TDS_CURSOR_STATE close;
    01026         TDS_CURSOR_STATE dealloc;
    01027 } TDS_CURSOR_STATUS;
    01028
    01029 typedef struct _tds_cursor
    01030 {
    01031         struct _tds_cursor *next;
    01032         TDS_INT length;
    01033         TDS_TINYINT cursor_name_len;
    01034         char *cursor_name;
    01035         TDS_INT cursor_id;
    01036         TDS_TINYINT options;
    01037         TDS_TINYINT hasargs;
    01038         TDS_USMALLINT query_len;
    01039         char *query;
    01040         /* TODO for updatable columns */
    01041         TDS_TINYINT number_upd_cols;
    01042         TDS_INT cursor_rows;
    01043         /*TODO when cursor has parameters*/
    01044         /*TDS_PARAM *param_list;         cursor parameter */
    01045         TDSUPDCOL *cur_col_list;
    01046         TDS_CURSOR_STATUS status;
    01047         TDS_SMALLINT srv_status;
    01048         TDSRESULTINFO *res_info;
    01049 } TDSCURSOR;
    01050
    01051 /*
    01052  * Current environment as reported by the server
    01053  */
    01054 typedef struct tds_env
    01055 {
    01056         int block_size;
    01057         char *language;
    01058         char *charset;
    01059         char *database;
    01060 } TDSENV;
    01061
    01062 typedef struct tds_dynamic
    01063 {
    01064         struct tds_dynamic *next;
    01065         char id[30];
    01066         int dyn_state;
    01068         TDS_INT num_id;
    01069         TDSPARAMINFO *res_info;
    01070         TDSPARAMINFO *params;
    01071         int emulated;
    01073         char *query;
    01074 } TDSDYNAMIC;
    01075
    01076 typedef enum {
    01077         TDS_MULTIPLE_QUERY,
    01078         TDS_MULTIPLE_EXECUTE,
    01079         TDS_MULTIPLE_RPC
    01080 } TDS_MULTIPLE_TYPE;
    01081
    01082 typedef struct tds_multiple
    01083 {
    01084         TDS_MULTIPLE_TYPE type;
    01085         unsigned int flags;
    01086 } TDSMULTIPLE;
    01087
    01088 /* forward declaration */
    01089 typedef struct tds_context TDSCONTEXT;
    01090
    01091 struct tds_context
    01092 {
    01093         TDSLOCALE *locale;
    01094         void *parent;
    01095         /* handler */
    01096         int (*msg_handler) (const TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
    01097         int (*err_handler) (const TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
    01098 };
    01099
    01100 enum TDS_ICONV_ENTRY
    01101 {
    01102           client2ucs2
    01103         , client2server_chardata
    01104         , iso2server_metadata
    01105         , initial_char_conv_count       /* keep last */
    01106 };
    01107
    01108 struct tds_socket
    01109 {
    01110         /* fixed and connect time */
    01111         TDS_SYS_SOCKET s;
    01112         TDS_SMALLINT major_version;
    01113         TDS_SMALLINT minor_version;
    01115         TDS_UINT product_version;
    01116         char *product_name;
    01117         unsigned char capabilities[TDS_MAX_CAPABILITY];
    01118         unsigned char broken_dates;
    01119         unsigned char option_flag2;
    01120         /* in/out buffers */
    01121         unsigned char *in_buf;
    01122         unsigned char *out_buf;
    01123         unsigned int in_buf_max;
    01124         unsigned in_pos;
    01125         unsigned out_pos;
    01126         unsigned in_len;
    01127         /* TODO remove blocksize from env and use out_len ?? */
    01128 /*      unsigned out_len; */
    01129         unsigned char in_flag;
    01130         unsigned char out_flag;
    01131         unsigned char last_packet;
    01132         void *parent;
    01138         TDSRESULTINFO *current_results;
    01139         TDSRESULTINFO *res_info;
    01140         TDS_INT num_comp_info;
    01141         TDSCOMPUTEINFO **comp_info;
    01142         TDSPARAMINFO *param_info;
    01143         TDSCURSOR *cur_cursor;
    01144         TDSCURSOR *cursors;
    01145         TDS_TINYINT has_status;
    01146         TDS_INT ret_status;
    01147         TDS_STATE state;
    01148         volatile unsigned char in_cancel;
    01149         int rows_affected;
    01150         /* timeout stuff from Jeff */
    01151         TDS_INT query_timeout;
    01152         int (*query_timeout_func) (void *param, unsigned int total_timeout);
    01153         void *query_timeout_param;
    01154         time_t query_start_time;
    01155         TDSENV env;
    01156
    01157         /* dynamic placeholder stuff */
    01158         TDSDYNAMIC *cur_dyn;
    01159         TDSDYNAMIC *dyns;
    01161         int emul_little_endian;
    01162         char *date_fmt;
    01163         const TDSCONTEXT *tds_ctx;
    01164         int char_conv_count;
    01165         TDSICONV **char_convs;
    01166
    01168         TDSCONNECTION *connection;
    01169
    01170         int spid;
    01171         TDS_UCHAR collation[5];
    01172         void (*env_chg_func) (TDSSOCKET * tds, int type, char *oldval, char *newval);
    01173         int internal_sp_called;
    01174
    01175         void *tls_session;
    01176         void *tls_credentials;
    01177         int option_value;
    01178 };
    01179
    01180 int tds_init_write_buf(TDSSOCKET * tds);
    01181 void tds_free_result_info(TDSRESULTINFO * info);
    01182 void tds_free_socket(TDSSOCKET * tds);
    01183 void tds_free_connection(TDSCONNECTION * connection);
    01184 void tds_free_all_results(TDSSOCKET * tds);
    01185 void tds_free_results(TDSRESULTINFO * res_info);
    01186 void tds_free_param_results(TDSPARAMINFO * param_info);
    01187 void tds_free_param_result(TDSPARAMINFO * param_info);
    01188 void tds_free_msg(TDSMESSAGE * message);
    01189 void tds_free_cursor(TDSSOCKET * tds, TDSCURSOR * cursor);
    01190 void tds_free_bcp_column_data(BCPCOLDATA * coldata);
    01191
    01192 int tds_put_n(TDSSOCKET * tds, const void *buf, int n);
    01193 int tds_put_string(TDSSOCKET * tds, const char *buf, int len);
    01194 int tds_put_int(TDSSOCKET * tds, TDS_INT i);
    01195 int tds_put_int8(TDSSOCKET * tds, TDS_INT8 i);
    01196 int tds_put_smallint(TDSSOCKET * tds, TDS_SMALLINT si);
    01198 #define tds_put_tinyint(tds, ti) tds_put_byte(tds,ti)
    01199 int tds_put_byte(TDSSOCKET * tds, unsigned char c);
    01200 TDSRESULTINFO *tds_alloc_results(int num_cols);
    01201 TDSCOMPUTEINFO **tds_alloc_compute_results(TDSSOCKET * tds, int num_cols, int by_cols);
    01202 TDSCONTEXT *tds_alloc_context(void * parent);
    01203 void tds_free_context(TDSCONTEXT * locale);
    01204 TDSSOCKET *tds_alloc_socket(TDSCONTEXT * context, int bufsize);
    01205
    01206 /* config.c */
    01207 const TDS_COMPILETIME_SETTINGS *tds_get_compiletime_settings(void);
    01208 typedef void (*TDSCONFPARSE) (const char *option, const char *value, void *param);
    01209 int tds_read_conf_section(FILE * in, const char *section, TDSCONFPARSE tds_conf_parse, void *parse_param);
    01210 int tds_read_conf_file(TDSCONNECTION * connection, const char *server);
    01211 TDSCONNECTION *tds_read_config_info(TDSSOCKET * tds, TDSLOGIN * login, TDSLOCALE * locale);
    01212 void tds_fix_connection(TDSCONNECTION * connection);
    01213 void tds_config_verstr(const char *tdsver, TDSCONNECTION * connection);
    01214 void tds_lookup_host(const char *servername, char *ip);
    01215 int tds_set_interfaces_file_loc(const char *interfloc);
    01216
    01217 TDSLOCALE *tds_get_locale(void);
    01218 unsigned char *tds_alloc_row(TDSRESULTINFO * res_info);
    01219 unsigned char *tds_alloc_compute_row(TDSCOMPUTEINFO * res_info);
    01220 BCPCOLDATA * tds_alloc_bcp_column_data(int column_size);
    01221 int tds_alloc_get_string(TDSSOCKET * tds, char **string, int len);
    01222 unsigned char *tds7_crypt_pass(const unsigned char *clear_pass, int len, unsigned char *crypt_pass);
    01223 TDSDYNAMIC *tds_lookup_dynamic(TDSSOCKET * tds, char *id);
    01224 const char *tds_prtype(int token);
    01225
    01226
    01227
    01228 /* iconv.c */
    01229 void tds_iconv_open(TDSSOCKET * tds, const char *charset);
    01230 void tds_iconv_close(TDSSOCKET * tds);
    01231 void tds_srv_charset_changed(TDSSOCKET * tds, const char *charset);
    01232 void tds7_srv_charset_changed(TDSSOCKET * tds, int sql_collate, int lcid);
    01233 int tds_iconv_alloc(TDSSOCKET * tds);
    01234 void tds_iconv_free(TDSSOCKET * tds);
    01235 TDSICONV *tds_iconv_from_collate(TDSSOCKET * tds, int sql_collate, int lcid);
    01236
    01237 /* threadsafe.c */
    01238 char *tds_timestamp_str(char *str, int maxlen);
    01239 struct hostent *tds_gethostbyname_r(const char *servername, struct hostent *result, char *buffer, int buflen, int *h_errnop);
    01240 struct hostent *tds_gethostbyaddr_r(const char *addr, int len, int type, struct hostent *result, char *buffer, int buflen,
    01241                                     int *h_errnop);
    01242 struct servent *tds_getservbyname_r(const char *name, const char *proto, struct servent *result, char *buffer, int buflen);
    01243 #ifdef INADDR_NONE
    01244 const char *tds_inet_ntoa_r(struct in_addr iaddr, char *ip, size_t len);
    01245 #endif
    01246 char *tds_get_homedir(void);
    01247
    01248 /* mem.c */
    01249 TDSPARAMINFO *tds_alloc_param_result(TDSPARAMINFO * old_param);
    01250 void tds_free_input_params(TDSDYNAMIC * dyn);
    01251 void tds_free_dynamic(TDSSOCKET * tds, TDSDYNAMIC * dyn);
    01252 TDSSOCKET *tds_realloc_socket(TDSSOCKET * tds, int bufsize);
    01253 unsigned char *tds_alloc_param_row(TDSPARAMINFO * info, TDSCOLUMN * curparam);
    01254 char *tds_alloc_client_sqlstate(int msgno);
    01255 char *tds_alloc_lookup_sqlstate(TDSSOCKET * tds, int msgno);
    01256 TDSLOGIN *tds_alloc_login(void);
    01257 TDSDYNAMIC *tds_alloc_dynamic(TDSSOCKET * tds, const char *id);
    01258 void tds_free_login(TDSLOGIN * login);
    01259 TDSCONNECTION *tds_alloc_connection(TDSLOCALE * locale);
    01260 TDSLOCALE *tds_alloc_locale(void);
    01261 void tds_free_locale(TDSLOCALE * locale);
    01262 TDSCURSOR * tds_alloc_cursor(TDSSOCKET * tds, const char *name, TDS_INT namelen, const char *query, TDS_INT querylen);
    01263 void tds_free_row(const TDSRESULTINFO * res_info, unsigned char *row);
    01264
    01265 /* login.c */
    01266 int tds7_send_auth(TDSSOCKET * tds, const unsigned char *challenge);
    01267 void tds_set_packet(TDSLOGIN * tds_login, int packet_size);
    01268 void tds_set_port(TDSLOGIN * tds_login, int port);
    01269 void tds_set_passwd(TDSLOGIN * tds_login, const char *password);
    01270 void tds_set_bulk(TDSLOGIN * tds_login, TDS_TINYINT enabled);
    01271 void tds_set_user(TDSLOGIN * tds_login, const char *username);
    01272 void tds_set_app(TDSLOGIN * tds_login, const char *application);
    01273 void tds_set_host(TDSLOGIN * tds_login, const char *hostname);
    01274 void tds_set_server_addr(TDSLOGIN * tds_login, const char *server_addr);
    01275 void tds_set_library(TDSLOGIN * tds_login, const char *library);
    01276 void tds_set_server(TDSLOGIN * tds_login, const char *server);
    01277 void tds_set_client_charset(TDSLOGIN * tds_login, const char *charset);
    01278 void tds_set_language(TDSLOGIN * tds_login, const char *language);
    01279 void tds_set_version(TDSLOGIN * tds_login, short major_ver, short minor_ver);
    01280 void tds_set_capabilities(TDSLOGIN * tds_login, unsigned char *capabilities, int size);
    01281 int tds_connect(TDSSOCKET * tds, TDSCONNECTION * connection);
    01282
    01283 /* query.c */
    01284 int tds_submit_query(TDSSOCKET * tds, const char *query);
    01285 int tds_submit_query_params(TDSSOCKET * tds, const char *query, TDSPARAMINFO * params);
    01286 int tds_submit_queryf(TDSSOCKET * tds, const char *queryf, ...);
    01287 int tds_submit_prepare(TDSSOCKET * tds, const char *query, const char *id, TDSDYNAMIC ** dyn_out, TDSPARAMINFO * params);
    01288 int tds_submit_execdirect(TDSSOCKET * tds, const char *query, TDSPARAMINFO * params);
    01289 int tds_submit_execute(TDSSOCKET * tds, TDSDYNAMIC * dyn);
    01290 int tds_send_cancel(TDSSOCKET * tds);
    01291 const char *tds_next_placeholder(const char *start);
    01292 int tds_count_placeholders(const char *query);
    01293 int tds_get_dynid(TDSSOCKET * tds, char **id);
    01294 int tds_submit_unprepare(TDSSOCKET * tds, TDSDYNAMIC * dyn);
    01295 int tds_submit_rpc(TDSSOCKET * tds, const char *rpc_name, TDSPARAMINFO * params);
    01296 int tds_submit_optioncmd(TDSSOCKET * tds, TDS_OPTION_CMD command, TDS_OPTION option, TDS_OPTION_ARG *param, TDS_INT param_size);
    01297 int tds_quote_id(TDSSOCKET * tds, char *buffer, const char *id, int idlen);
    01298 int tds_quote_string(TDSSOCKET * tds, char *buffer, const char *str, int len);
    01299 const char *tds_skip_quoted(const char *s);
    01300
    01301 int tds_cursor_declare(TDSSOCKET * tds, TDSCURSOR * cursor, int *send);
    01302 int tds_cursor_setrows(TDSSOCKET * tds, TDSCURSOR * cursor, int *send);
    01303 int tds_cursor_open(TDSSOCKET * tds, TDSCURSOR * cursor, int *send);
    01304 int tds_cursor_fetch(TDSSOCKET * tds, TDSCURSOR * cursor);
    01305 int tds_cursor_close(TDSSOCKET * tds, TDSCURSOR * cursor);
    01306 int tds_cursor_dealloc(TDSSOCKET * tds, TDSCURSOR * cursor);
    01307
    01308 int tds_multiple_init(TDSSOCKET *tds, TDSMULTIPLE *multiple, TDS_MULTIPLE_TYPE type);
    01309 int tds_multiple_done(TDSSOCKET *tds, TDSMULTIPLE *multiple);
    01310 int tds_multiple_query(TDSSOCKET *tds, TDSMULTIPLE *multiple, const char *query, TDSPARAMINFO * params);
    01311 int tds_multiple_execute(TDSSOCKET *tds, TDSMULTIPLE *multiple, TDSDYNAMIC * dyn);
    01312
    01313 /* token.c */
    01314 int tds_process_cancel(TDSSOCKET * tds);
    01315 void tds_swap_datatype(int coltype, unsigned char *buf);
    01316 void tds_swap_numeric(TDS_NUMERIC *num);
    01317 int tds_get_token_size(int marker);
    01318 int tds_process_login_tokens(TDSSOCKET * tds);
    01319 void tds_add_row_column_size(TDSRESULTINFO * info, TDSCOLUMN * curcol);
    01320 int tds_process_simple_query(TDSSOCKET * tds);
    01321 int tds5_send_optioncmd(TDSSOCKET * tds, TDS_OPTION_CMD tds_command, TDS_OPTION tds_option, TDS_OPTION_ARG * tds_argument,
    01322                         TDS_INT * tds_argsize);
    01323 int tds_client_msg(const TDSCONTEXT * tds_ctx, TDSSOCKET * tds, int msgno, int severity, int state, int line, const char *message);
    01324 int tds_process_tokens(TDSSOCKET * tds, TDS_INT * result_type, int *done_flags, unsigned flag);
    01325
    01326 /* data.c */
    01327 void tds_set_param_type(TDSSOCKET * tds, TDSCOLUMN * curcol, TDS_SERVER_TYPE type);
    01328 void tds_set_column_type(TDSCOLUMN * curcol, int type);
    01329
    01330
    01331 /* tds_convert.c */
    01332 TDS_INT tds_datecrack(TDS_INT datetype, const void *di, TDSDATEREC * dr);
    01333 int tds_get_conversion_type(int srctype, int colsize);
    01334 extern const char tds_hex_digits[];
    01335
    01336 /* write.c */
    01337 int tds_flush_packet(TDSSOCKET * tds);
    01338 int tds_put_buf(TDSSOCKET * tds, const unsigned char *buf, int dsize, int ssize);
    01339
    01340 /* read.c */
    01341 unsigned char tds_get_byte(TDSSOCKET * tds);
    01342 void tds_unget_byte(TDSSOCKET * tds);
    01343 unsigned char tds_peek(TDSSOCKET * tds);
    01344 TDS_SMALLINT tds_get_smallint(TDSSOCKET * tds);
    01345 TDS_INT tds_get_int(TDSSOCKET * tds);
    01346 int tds_get_string(TDSSOCKET * tds, int string_len, char *dest, size_t dest_size);
    01347 int tds_get_char_data(TDSSOCKET * tds, char *dest, size_t wire_size, TDSCOLUMN * curcol);
    01348 void *tds_get_n(TDSSOCKET * tds, void *dest, int n);
    01349 int tds_get_size_by_type(int servertype);
    01350
    01351
    01352 /* util.c */
    01353 TDS_STATE tds_set_state(TDSSOCKET * tds, TDS_STATE state);
    01354 void tds_set_parent(TDSSOCKET * tds, void *the_parent);
    01355 void *tds_get_parent(TDSSOCKET * tds);
    01356 int tds_swap_bytes(unsigned char *buf, int bytes);
    01357 int tds_version(TDSSOCKET * tds_socket, char *pversion_string);
    01358 void tdsdump_off(void);
    01359 void tdsdump_on(void);
    01360 int tdsdump_open(const char *filename);
    01361 void tdsdump_close(void);
    01362 void tdsdump_dump_buf(const char* file, unsigned int level_line, const char *msg, const void *buf, int length);
    01363 void tdsdump_log(const char* file, unsigned int level_line, const char *fmt, ...)
    01364 #if defined(__GNUC__) && __GNUC__ >= 2
    01365         __attribute__ ((__format__ (__printf__, 3, 4)))
    01366 #endif
    01367 ;
    01368 extern int tds_debug_flags;
    01369
    01370 /* net.c */
    01371 int tds_open_socket(TDSSOCKET * tds, const char *ip_addr, unsigned int port, int timeout);
    01372 int tds_close_socket(TDSSOCKET * tds);
    01373 int tds_read_packet(TDSSOCKET * tds);
    01374 int tds_write_packet(TDSSOCKET * tds, unsigned char final);
    01375 int tds7_get_instance_port(const char *ip_addr, const char *instance);
    01376 int tds_ssl_init(TDSSOCKET *tds);
    01377 void tds_ssl_deinit(TDSSOCKET *tds);
    01378
    01379
    01380
    01381 /* vstrbuild.c */
    01382 int tds_vstrbuild(char *buffer, int buflen, int *resultlen, char *text, int textlen, const char *formats, int formatlen,
    01383                   va_list ap);
    01384
    01385 /* numeric.c */
    01386 char *tds_money_to_string(const TDS_MONEY * money, char *s);
    01387 TDS_INT tds_numeric_to_string(const TDS_NUMERIC * numeric, char *s);
    01388 TDS_INT tds_numeric_change_prec_scale(TDS_NUMERIC * numeric, unsigned char new_prec, unsigned char new_scale);
    01389
    01390 /* getmac.c */
    01391 void tds_getmac(int s, unsigned char mac[6]);
    01392
    01393 typedef struct tds_answer
    01394 {
    01395         unsigned char lm_resp[24];
    01396         unsigned char nt_resp[24];
    01397 } TDSANSWER;
    01398 void tds_answer_challenge(const char *passwd, const unsigned char *challenge, TDSANSWER * answer);
    01399
    01400 #define IS_TDS42(x) (x->major_version==4 && x->minor_version==2)
    01401 #define IS_TDS46(x) (x->major_version==4 && x->minor_version==6)
    01402 #define IS_TDS50(x) (x->major_version==5 && x->minor_version==0)
    01403 #define IS_TDS70(x) (x->major_version==7 && x->minor_version==0)
    01404 #define IS_TDS80(x) (x->major_version==8 && x->minor_version==0)
    01405
    01406 #define IS_TDS7_PLUS(x) ( IS_TDS70(x) || IS_TDS80(x) )
    01407
    01408 #define IS_TDSDEAD(x) (((x) == NULL) || TDS_IS_SOCKET_INVALID((x)->s))
    01409
    01411 #define TDS_IS_SYBASE(x) (!(x->product_version & 0x80000000u))
    01412
    01413 #define TDS_IS_MSSQL(x) ((x->product_version & 0x80000000u)!=0)
    01414
    01418 #define TDS_MS_VER(maj,min,x) (0x80000000u|((maj)<<24)|((min)<<16)|(x))
    01419
    01420 /* TODO test if not similar to ms one*/
    01422 #define TDS_SYB_VER(maj,min,x) (((maj)<<24)|((min)<<16)|(x)<<8)
    01423
    01424 #ifdef __cplusplus
    01425 #if 0
    01426 {
    01427 #endif
    01428 }
    01429 #endif
    01430
    01431 #endif /* _tds_h_ */