FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tds.h
Go to the documentation of this file.
1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
3  * Copyright (C) 2010, 2011 Frediano Ziglio
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _tds_h_
22 #define _tds_h_
23 
24 /* $Id: tds.h,v 1.352.2.4 2011-08-12 16:29:36 freddy77 Exp $ */
25 
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <time.h>
29 
30 #if HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif /* HAVE_NET_INET_IN_H */
33 #if HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif /* HAVE_ARPA_INET_H */
36 
37 /* forward declaration */
38 typedef struct tdsiconvinfo TDSICONV;
39 typedef struct tds_socket TDSSOCKET;
40 
41 #include "tdsver.h"
42 #include "tds_sysdep_public.h"
43 #ifdef _FREETDS_LIBRARY_SOURCE
44 #include "tds_sysdep_private.h"
45 #endif /* _FREETDS_LIBRARY_SOURCE */
46 
47 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
48 #pragma GCC visibility push(hidden)
49 #endif
50 
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #if 0
55 }
56 #endif
57 #endif
58 
65 {
66  const char *freetds_version; /* release version of FreeTDS */
67  const char *sysconfdir; /* location of freetds.conf */
68  const char *last_update; /* latest software_version date among the modules */
69  int msdblib; /* for MS style dblib */
70  int sybase_compat; /* enable increased Open Client binary compatibility */
71  int threadsafe; /* compile for thread safety default=no */
72  int libiconv; /* search for libiconv in DIR/include and DIR/lib */
73  const char *tdsver; /* TDS protocol version (4.2/4.6/5.0/7.0/8.0) 5.0 */
74  int iodbc; /* build odbc driver against iODBC in DIR */
75  int unixodbc; /* build odbc driver against unixODBC in DIR */
76 
78 
79 typedef struct tds_dstr {
80  char *dstr_s;
81  size_t dstr_size;
82 } DSTR;
83 
99 /*
100  * All references to data that touch the wire should use the following typedefs.
101  *
102  * If you have problems on 64-bit machines and the code is
103  * using a native datatype, please change it to use
104  * these. (In the TDS layer only, the API layers have their
105  * own typedefs which equate to these).
106  */
107 typedef char TDS_CHAR; /* 8-bit char */
108 typedef unsigned char TDS_UCHAR; /* 8-bit uchar */
109 typedef unsigned char TDS_TINYINT; /* 8-bit unsigned */
110 typedef tds_sysdep_int16_type TDS_SMALLINT; /* 16-bit int */
111 typedef unsigned tds_sysdep_int16_type TDS_USMALLINT; /* 16-bit unsigned */
112 typedef tds_sysdep_int32_type TDS_INT; /* 32-bit int */
113 typedef unsigned tds_sysdep_int32_type TDS_UINT; /* 32-bit unsigned */
114 typedef tds_sysdep_real32_type TDS_REAL; /* 32-bit real */
115 typedef tds_sysdep_real64_type TDS_FLOAT; /* 64-bit real */
116 typedef tds_sysdep_int64_type TDS_INT8; /* 64-bit integer */
117 typedef unsigned tds_sysdep_int64_type TDS_UINT8; /* 64-bit unsigned */
118 typedef tds_sysdep_intptr_type TDS_INTPTR;
119 
120 typedef struct tdsnumeric
121 {
122  unsigned char precision;
123  unsigned char scale;
124  unsigned char array[33];
125 } TDS_NUMERIC;
126 
127 typedef struct tdsoldmoney
128 {
129  TDS_INT mnyhigh;
130  TDS_UINT mnylow;
131 } TDS_OLD_MONEY;
132 
133 typedef union tdsmoney
134 {
136  TDS_INT8 mny;
137 } TDS_MONEY;
138 
139 typedef struct tdsmoney4
140 {
141  TDS_INT mny4;
142 } TDS_MONEY4;
143 
144 typedef struct tdsdatetime
145 {
146  TDS_INT dtdays;
147  TDS_INT dttime;
148 } TDS_DATETIME;
149 
150 typedef struct tdsdatetime4
151 {
152  TDS_USMALLINT days;
153  TDS_USMALLINT minutes;
154 } TDS_DATETIME4;
155 
156 typedef struct tdsvarbinary
157 {
158  TDS_SMALLINT len;
159  TDS_CHAR array[256];
160 } TDS_VARBINARY;
161 typedef struct tdsvarchar
162 {
163  TDS_SMALLINT len;
164  TDS_CHAR array[256];
165 } TDS_VARCHAR;
166 
167 typedef struct tdsunique
168 {
169  TDS_UINT Data1;
170  TDS_USMALLINT Data2;
171  TDS_USMALLINT Data3;
172  TDS_UCHAR Data4[8];
173 } TDS_UNIQUE;
174 
176 typedef struct tdsdaterec
177 {
178  TDS_INT year;
179  TDS_INT quarter;
180  TDS_INT month;
181  TDS_INT day;
182  TDS_INT dayofyear;
183  TDS_INT week;
184  TDS_INT weekday;
185  TDS_INT hour;
186  TDS_INT minute;
187  TDS_INT second;
188  TDS_INT millisecond;
189  TDS_INT tzone;
190 } TDSDATEREC;
191 
197 extern const int tds_numeric_bytes_per_prec[];
198 
199 #define TDS_SUCCEED 1
200 #define TDS_FAIL 0
201 #define TDS_NO_MORE_RESULTS 2
202 #define TDS_CANCELLED 3
203 
204 #define TDS_INT_CONTINUE 1
205 #define TDS_INT_CANCEL 2
206 #define TDS_INT_TIMEOUT 3
207 
208 
209 #define TDS_NO_COUNT -1
210 
211 #define TDS_ROW_RESULT 4040
212 #define TDS_PARAM_RESULT 4042
213 #define TDS_STATUS_RESULT 4043
214 #define TDS_MSG_RESULT 4044
215 #define TDS_COMPUTE_RESULT 4045
216 #define TDS_CMD_DONE 4046
217 #define TDS_CMD_SUCCEED 4047
218 #define TDS_CMD_FAIL 4048
219 #define TDS_ROWFMT_RESULT 4049
220 #define TDS_COMPUTEFMT_RESULT 4050
221 #define TDS_DESCRIBE_RESULT 4051
222 #define TDS_DONE_RESULT 4052
223 #define TDS_DONEPROC_RESULT 4053
224 #define TDS_DONEINPROC_RESULT 4054
225 #define TDS_OTHERS_RESULT 4055
226 
227 enum tds_token_results
228 {
229  TDS_TOKEN_RES_OTHERS,
230  TDS_TOKEN_RES_ROWFMT,
231  TDS_TOKEN_RES_COMPUTEFMT,
232  TDS_TOKEN_RES_PARAMFMT,
233  TDS_TOKEN_RES_DONE,
234  TDS_TOKEN_RES_ROW,
235  TDS_TOKEN_RES_COMPUTE,
236  TDS_TOKEN_RES_PROC,
237  TDS_TOKEN_RES_MSG
238 };
239 
240 #define TDS_TOKEN_FLAG(flag) TDS_RETURN_##flag = (1 << (TDS_TOKEN_RES_##flag*2)), TDS_STOPAT_##flag = (2 << (TDS_TOKEN_RES_##flag*2))
241 
242 enum tds_token_flags
243 {
244  TDS_HANDLE_ALL = 0,
245  TDS_TOKEN_FLAG(OTHERS),
246  TDS_TOKEN_FLAG(ROWFMT),
247  TDS_TOKEN_FLAG(COMPUTEFMT),
248  TDS_TOKEN_FLAG(PARAMFMT),
249  TDS_TOKEN_FLAG(DONE),
250  TDS_TOKEN_FLAG(ROW),
251  TDS_TOKEN_FLAG(COMPUTE),
252  TDS_TOKEN_FLAG(PROC),
253  TDS_TOKEN_FLAG(MSG),
254  TDS_TOKEN_RESULTS = TDS_RETURN_ROWFMT|TDS_RETURN_COMPUTEFMT|TDS_RETURN_DONE|TDS_STOPAT_ROW|TDS_STOPAT_COMPUTE|TDS_RETURN_PROC,
255  TDS_TOKEN_TRAILING = TDS_STOPAT_ROWFMT|TDS_STOPAT_COMPUTEFMT|TDS_STOPAT_ROW|TDS_STOPAT_COMPUTE|TDS_STOPAT_MSG|TDS_STOPAT_OTHERS
256 };
257 
262 {
265  , TDS_DONE_ERROR = 0x02
266  , TDS_DONE_INXACT = 0x04
267  , TDS_DONE_PROC = 0x08
268  , TDS_DONE_COUNT = 0x10
270  , TDS_DONE_EVENT = 0x40 /* part of an event notification. */
271  , TDS_DONE_SRVERROR = 0x100
273  /* after the above flags, a TDS_DONE packet has a field describing the state of the transaction */
274  , TDS_DONE_NO_TRAN = 0 /* No transaction in effect */
275  , TDS_DONE_TRAN_SUCCEED = 1 /* Transaction completed successfully */
276  , TDS_DONE_TRAN_PROGRESS= 2 /* Transaction in progress */
277  , TDS_DONE_STMT_ABORT = 3 /* A statement aborted */
278  , TDS_DONE_TRAN_ABORT = 4 /* Transaction aborted */
279 };
280 
281 
282 /*
283  * TDSERRNO is emitted by libtds to the client library's error handler
284  * (which may in turn call the client's error handler).
285  * These match the db-lib msgno, because the same values have the same meaning
286  * in db-lib and ODBC. ct-lib maps them to ct-lib numbers (todo).
287  */
288 typedef enum { TDSEOK = TDS_SUCCEED,
289  TDSEVERDOWN = 100,
290  TDSEICONVIU = 2400,
291  TDSEICONVAVAIL = 2401,
292  TDSEICONVO = 2402,
293  TDSEICONVI = 2403,
294  TDSEICONV2BIG = 2404,
295  TDSEPORTINSTANCE = 2500,
296  TDSESYNC = 20001,
297  TDSEFCON = 20002,
298  TDSETIME = 20003,
299  TDSEREAD = 20004,
300  TDSEWRIT = 20006,
301  TDSESOCK = 20008,
302  TDSECONN = 20009,
303  TDSEMEM = 20010,
304  TDSEINTF = 20012, /* Server name not found in interface file */
305  TDSEUHST = 20013, /* Unknown host machine name. */
306  TDSEPWD = 20014,
307  TDSESEOF = 20017,
308  TDSERPND = 20019,
309  TDSEBTOK = 20020,
310  TDSEOOB = 20022,
311  TDSECLOS = 20056,
312  TDSEUSCT = 20058,
313  TDSEUTDS = 20146,
314  TDSEEUNR = 20185,
315  TDSECAP = 20203,
316  TDSENEG = 20210,
317  TDSEUMSG = 20212,
318  TDSECAPTYP = 20213,
319  TDSEBPROBADTYP = 20250,
320  TDSECLOSEIN = 20292
321 } TDSERRNO;
322 
323 #define TDS5_PARAMFMT2_TOKEN 32 /* 0x20 */
324 #define TDS_LANGUAGE_TOKEN 33 /* 0x21 TDS 5.0 only */
325 #define TDS_ORDERBY2_TOKEN 34 /* 0x22 */
326 #define TDS_ROWFMT2_TOKEN 97 /* 0x61 TDS 5.0 only */
327 #define TDS_LOGOUT_TOKEN 113 /* 0x71 TDS 5.0 only? ct_close() */
328 #define TDS_RETURNSTATUS_TOKEN 121 /* 0x79 */
329 #define TDS_PROCID_TOKEN 124 /* 0x7C TDS 4.2 only - TDS_PROCID */
330 #define TDS7_RESULT_TOKEN 129 /* 0x81 TDS 7.0 only */
331 #define TDS7_COMPUTE_RESULT_TOKEN 136 /* 0x88 TDS 7.0 only */
332 #define TDS_COLNAME_TOKEN 160 /* 0xA0 TDS 4.2 only */
333 #define TDS_COLFMT_TOKEN 161 /* 0xA1 TDS 4.2 only - TDS_COLFMT */
334 #define TDS_DYNAMIC2_TOKEN 163 /* 0xA3 */
335 #define TDS_TABNAME_TOKEN 164 /* 0xA4 */
336 #define TDS_COLINFO_TOKEN 165 /* 0xA5 */
337 #define TDS_OPTIONCMD_TOKEN 166 /* 0xA6 */
338 #define TDS_COMPUTE_NAMES_TOKEN 167 /* 0xA7 */
339 #define TDS_COMPUTE_RESULT_TOKEN 168 /* 0xA8 */
340 #define TDS_ORDERBY_TOKEN 169 /* 0xA9 TDS_ORDER */
341 #define TDS_ERROR_TOKEN 170 /* 0xAA */
342 #define TDS_INFO_TOKEN 171 /* 0xAB */
343 #define TDS_PARAM_TOKEN 172 /* 0xAC RETURNVALUE? */
344 #define TDS_LOGINACK_TOKEN 173 /* 0xAD */
345 #define TDS_CONTROL_TOKEN 174 /* 0xAE TDS_CONTROL */
346 #define TDS_ROW_TOKEN 209 /* 0xD1 */
347 #define TDS_NBC_ROW_TOKEN 210 /* 0xD2 as of TDS 7.3.B */ /* not implemented */
348 #define TDS_CMP_ROW_TOKEN 211 /* 0xD3 */
349 #define TDS5_PARAMS_TOKEN 215 /* 0xD7 TDS 5.0 only */
350 #define TDS_CAPABILITY_TOKEN 226 /* 0xE2 */
351 #define TDS_ENVCHANGE_TOKEN 227 /* 0xE3 */
352 #define TDS_EED_TOKEN 229 /* 0xE5 */
353 #define TDS_DBRPC_TOKEN 230 /* 0xE6 */
354 #define TDS5_DYNAMIC_TOKEN 231 /* 0xE7 TDS 5.0 only */
355 #define TDS5_PARAMFMT_TOKEN 236 /* 0xEC TDS 5.0 only */
356 #define TDS_AUTH_TOKEN 237 /* 0xED TDS 7.0 only */
357 #define TDS_RESULT_TOKEN 238 /* 0xEE */
358 #define TDS_DONE_TOKEN 253 /* 0xFD TDS_DONE */
359 #define TDS_DONEPROC_TOKEN 254 /* 0xFE TDS_DONEPROC */
360 #define TDS_DONEINPROC_TOKEN 255 /* 0xFF TDS_DONEINPROC */
361 
362 /* CURSOR support: TDS 5.0 only*/
363 #define TDS_CURCLOSE_TOKEN 128 /* 0x80 TDS 5.0 only */
364 #define TDS_CURDELETE_TOKEN 129 /* 0x81 TDS 5.0 only */
365 #define TDS_CURFETCH_TOKEN 130 /* 0x82 TDS 5.0 only */
366 #define TDS_CURINFO_TOKEN 131 /* 0x83 TDS 5.0 only */
367 #define TDS_CUROPEN_TOKEN 132 /* 0x84 TDS 5.0 only */
368 #define TDS_CURDECLARE_TOKEN 134 /* 0x86 TDS 5.0 only */
369 
370 enum {
371  TDS_CUR_ISTAT_UNUSED = 0x00,
372  TDS_CUR_ISTAT_DECLARED = 0x01,
373  TDS_CUR_ISTAT_OPEN = 0x02,
374  TDS_CUR_ISTAT_CLOSED = 0x04,
375  TDS_CUR_ISTAT_RDONLY = 0x08,
376  TDS_CUR_ISTAT_UPDATABLE = 0x10,
377  TDS_CUR_ISTAT_ROWCNT = 0x20,
378  TDS_CUR_ISTAT_DEALLOC = 0x40
379 };
380 
381 /* http://jtds.sourceforge.net/apiCursors.html */
382 /* Cursor scroll option, must be one of 0x01 - 0x10, OR'd with other bits */
383 enum {
384  TDS_CUR_TYPE_KEYSET = 0x0001, /* default */
385  TDS_CUR_TYPE_DYNAMIC = 0x0002,
386  TDS_CUR_TYPE_FORWARD = 0x0004,
387  TDS_CUR_TYPE_STATIC = 0x0008,
388  TDS_CUR_TYPE_FASTFORWARDONLY = 0x0010,
389  TDS_CUR_TYPE_PARAMETERIZED = 0x1000,
390  TDS_CUR_TYPE_AUTO_FETCH = 0x2000
391 };
392 
393 enum {
394  TDS_CUR_CONCUR_READ_ONLY = 1,
395  TDS_CUR_CONCUR_SCROLL_LOCKS = 2,
396  TDS_CUR_CONCUR_OPTIMISTIC = 4, /* default */
397  TDS_CUR_CONCUR_OPTIMISTIC_VALUES = 8
398 };
399 
400 /* TDS 4/5 login*/
401 #define TDS_MAXNAME 30 /* maximum login name lenghts */
402 #define TDS_PROGNLEN 10 /* maximum program lenght */
403 #define TDS_PKTLEN 6 /* maximum packet lenght in login */
404 
405 /* environment type field */
406 #define TDS_ENV_DATABASE 1
407 #define TDS_ENV_LANG 2
408 #define TDS_ENV_CHARSET 3
409 #define TDS_ENV_PACKSIZE 4
410 #define TDS_ENV_LCID 5
411 #define TDS_ENV_SQLCOLLATION 7
412 #define TDS_ENV_BEGINTRANS 8
413 #define TDS_ENV_COMMITTRANS 9
414 #define TDS_ENV_ROLLBACKTRANS 10
415 
416 /* string types */
417 #define TDS_NULLTERM -9
418 
419 /* Microsoft internal stored procedure id's */
420 
421 #define TDS_SP_CURSOR 1
422 #define TDS_SP_CURSOROPEN 2
423 #define TDS_SP_CURSORPREPARE 3
424 #define TDS_SP_CURSOREXECUTE 4
425 #define TDS_SP_CURSORPREPEXEC 5
426 #define TDS_SP_CURSORUNPREPARE 6
427 #define TDS_SP_CURSORFETCH 7
428 #define TDS_SP_CURSOROPTION 8
429 #define TDS_SP_CURSORCLOSE 9
430 #define TDS_SP_EXECUTESQL 10
431 #define TDS_SP_PREPARE 11
432 #define TDS_SP_EXECUTE 12
433 #define TDS_SP_PREPEXEC 13
434 #define TDS_SP_PREPEXECRPC 14
435 #define TDS_SP_UNPREPARE 15
436 /*
437  * <rant> Sybase does an awful job of this stuff, non null ints of size 1 2
438  * and 4 have there own codes but nullable ints are lumped into INTN
439  * sheesh! </rant>
440  */
441 typedef enum
442 {
443  SYBCHAR = 47, /* 0x2F */
444 #define SYBCHAR SYBCHAR
445  SYBVARCHAR = 39, /* 0x27 */
446 #define SYBVARCHAR SYBVARCHAR
447  SYBINTN = 38, /* 0x26 */
448 #define SYBINTN SYBINTN
449  SYBINT1 = 48, /* 0x30 */
450 #define SYBINT1 SYBINT1
451  SYBINT2 = 52, /* 0x34 */
452 #define SYBINT2 SYBINT2
453  SYBINT4 = 56, /* 0x38 */
454 #define SYBINT4 SYBINT4
455  SYBFLT8 = 62, /* 0x3E */
456 #define SYBFLT8 SYBFLT8
457  SYBDATETIME = 61, /* 0x3D */
458 #define SYBDATETIME SYBDATETIME
459  SYBBIT = 50, /* 0x32 */
460 #define SYBBIT SYBBIT
461  SYBTEXT = 35, /* 0x23 */
462 #define SYBTEXT SYBTEXT
463  SYBNTEXT = 99, /* 0x63 */
464 #define SYBNTEXT SYBNTEXT
465  SYBIMAGE = 34, /* 0x22 */
466 #define SYBIMAGE SYBIMAGE
467  SYBMONEY4 = 122, /* 0x7A */
468 #define SYBMONEY4 SYBMONEY4
469  SYBMONEY = 60, /* 0x3C */
470 #define SYBMONEY SYBMONEY
471  SYBDATETIME4 = 58, /* 0x3A */
472 #define SYBDATETIME4 SYBDATETIME4
473  SYBREAL = 59, /* 0x3B */
474 #define SYBREAL SYBREAL
475  SYBBINARY = 45, /* 0x2D */
476 #define SYBBINARY SYBBINARY
477  SYBVOID = 31, /* 0x1F */
478 #define SYBVOID SYBVOID
479  SYBVARBINARY = 37, /* 0x25 */
480 #define SYBVARBINARY SYBVARBINARY
481  SYBBITN = 104, /* 0x68 */
482 #define SYBBITN SYBBITN
483  SYBNUMERIC = 108, /* 0x6C */
484 #define SYBNUMERIC SYBNUMERIC
485  SYBDECIMAL = 106, /* 0x6A */
486 #define SYBDECIMAL SYBDECIMAL
487  SYBFLTN = 109, /* 0x6D */
488 #define SYBFLTN SYBFLTN
489  SYBMONEYN = 110, /* 0x6E */
490 #define SYBMONEYN SYBMONEYN
491  SYBDATETIMN = 111, /* 0x6F */
492 #define SYBDATETIMN SYBDATETIMN
493 
494 /*
495  * MS only types
496  */
497  SYBNVARCHAR = 103, /* 0x67 */
498 #define SYBNVARCHAR SYBNVARCHAR
499  SYBINT8 = 127, /* 0x7F */
500 #define SYBINT8 SYBINT8
501  XSYBCHAR = 175, /* 0xAF */
502 #define XSYBCHAR XSYBCHAR
503  XSYBVARCHAR = 167, /* 0xA7 */
504 #define XSYBVARCHAR XSYBVARCHAR
505  XSYBNVARCHAR = 231, /* 0xE7 */
506 #define XSYBNVARCHAR XSYBNVARCHAR
507  XSYBNCHAR = 239, /* 0xEF */
508 #define XSYBNCHAR XSYBNCHAR
509  XSYBVARBINARY = 165, /* 0xA5 */
510 #define XSYBVARBINARY XSYBVARBINARY
511  XSYBBINARY = 173, /* 0xAD */
512 #define XSYBBINARY XSYBBINARY
513  SYBUNIQUE = 36, /* 0x24 */
514 #define SYBUNIQUE SYBUNIQUE
515  SYBVARIANT = 98, /* 0x62 */
516 #define SYBVARIANT SYBVARIANT
517  SYBMSUDT = 240, /* 0xF0 */
518 #define SYBMSUDT SYBMSUDT
519  SYBMSXML = 241, /* 0xF1 */
520 #define SYBMSXML SYBMSXML
521 
522 /*
523  * Sybase only types
524  */
525  SYBLONGBINARY = 225, /* 0xE1 */
526 #define SYBLONGBINARY SYBLONGBINARY
527  SYBUINT1 = 64, /* 0x40 */
528 #define SYBUINT1 SYBUINT1
529  SYBUINT2 = 65, /* 0x41 */
530 #define SYBUINT2 SYBUINT2
531  SYBUINT4 = 66, /* 0x42 */
532 #define SYBUINT4 SYBUINT4
533  SYBUINT8 = 67, /* 0x43 */
534 #define SYBUINT8 SYBUINT8
535  SYBBLOB = 36, /* 0x24 */
536 #define SYBBLOB SYBBLOB
537  SYBBOUNDARY = 104, /* 0x68 */
538 #define SYBBOUNDARY SYBBOUNDARY
539  SYBDATE = 49, /* 0x31 */
540 #define SYBDATE SYBDATE
541  SYBDATEN = 123, /* 0x7B */
542 #define SYBDATEN SYBDATEN
543  SYB5INT8 = 191, /* 0xBF */
544 #define SYB5INT8 SYB5INT8
545  SYBINTERVAL = 46, /* 0x2E */
546 #define SYBINTERVAL SYBINTERVAL
547  SYBLONGCHAR = 175, /* 0xAF */
548 #define SYBLONGCHAR SYBLONGCHAR
549  SYBSENSITIVITY = 103, /* 0x67 */
550 #define SYBSENSITIVITY SYBSENSITIVITY
551  SYBSINT1 = 176, /* 0xB0 */
552 #define SYBSINT1 SYBSINT1
553  SYBTIME = 51, /* 0x33 */
554 #define SYBTIME SYBTIME
555  SYBTIMEN = 147, /* 0x93 */
556 #define SYBTIMEN SYBTIMEN
557  SYBUINTN = 68, /* 0x44 */
558 #define SYBUINTN SYBUINTN
559  SYBUNITEXT = 174, /* 0xAE */
560 #define SYBUNITEXT SYBUNITEXT
561  SYBXML = 163, /* 0xA3 */
562 #define SYBXML SYBXML
563 
564 } TDS_SERVER_TYPE;
565 
566 
567 typedef enum
568 {
569  USER_UNICHAR_TYPE = 34, /* 0x22 */
570  USER_UNIVARCHAR_TYPE = 35 /* 0x23 */
571 } TDS_USER_TYPE;
572 
573 #define SYBAOPCNT 0x4b
574 #define SYBAOPCNTU 0x4c
575 #define SYBAOPSUM 0x4d
576 #define SYBAOPSUMU 0x4e
577 #define SYBAOPAVG 0x4f
578 #define SYBAOPAVGU 0x50
579 #define SYBAOPMIN 0x51
580 #define SYBAOPMAX 0x52
581 
582 /* mssql2k compute operator */
583 #define SYBAOPCNT_BIG 0x09
584 #define SYBAOPSTDEV 0x30
585 #define SYBAOPSTDEVP 0x31
586 #define SYBAOPVAR 0x32
587 #define SYBAOPVARP 0x33
588 #define SYBAOPCHECKSUM_AGG 0x72
589 
590 
594 typedef enum
595 {
596  TDS_OPT_SET = 1 /* Set an option. */
597  , TDS_OPT_DEFAULT = 2 /* Set option to its default value. */
598  , TDS_OPT_LIST = 3 /* Request current setting of a specific option. */
599  , TDS_OPT_INFO = 4 /* Report current setting of a specific option. */
601 
602 typedef enum
603 {
604  TDS_OPT_DATEFIRST = 1 /* 0x01 */
605  , TDS_OPT_TEXTSIZE = 2 /* 0x02 */
606  , TDS_OPT_STAT_TIME = 3 /* 0x03 */
607  , TDS_OPT_STAT_IO = 4 /* 0x04 */
608  , TDS_OPT_ROWCOUNT = 5 /* 0x05 */
609  , TDS_OPT_NATLANG = 6 /* 0x06 */
610  , TDS_OPT_DATEFORMAT = 7 /* 0x07 */
611  , TDS_OPT_ISOLATION = 8 /* 0x08 */
612  , TDS_OPT_AUTHON = 9 /* 0x09 */
613  , TDS_OPT_CHARSET = 10 /* 0x0a */
614  , TDS_OPT_SHOWPLAN = 13 /* 0x0d */
615  , TDS_OPT_NOEXEC = 14 /* 0x0e */
616  , TDS_OPT_ARITHIGNOREON = 15 /* 0x0f */
617  , TDS_OPT_ARITHABORTON = 17 /* 0x11 */
618  , TDS_OPT_PARSEONLY = 18 /* 0x12 */
619  , TDS_OPT_GETDATA = 20 /* 0x14 */
620  , TDS_OPT_NOCOUNT = 21 /* 0x15 */
621  , TDS_OPT_FORCEPLAN = 23 /* 0x17 */
622  , TDS_OPT_FORMATONLY = 24 /* 0x18 */
623  , TDS_OPT_CHAINXACTS = 25 /* 0x19 */
624  , TDS_OPT_CURCLOSEONXACT = 26 /* 0x1a */
625  , TDS_OPT_FIPSFLAG = 27 /* 0x1b */
626  , TDS_OPT_RESTREES = 28 /* 0x1c */
627  , TDS_OPT_IDENTITYON = 29 /* 0x1d */
628  , TDS_OPT_CURREAD = 30 /* 0x1e */
629  , TDS_OPT_CURWRITE = 31 /* 0x1f */
630  , TDS_OPT_IDENTITYOFF = 32 /* 0x20 */
631  , TDS_OPT_AUTHOFF = 33 /* 0x21 */
632  , TDS_OPT_ANSINULL = 34 /* 0x22 */
633  , TDS_OPT_QUOTED_IDENT = 35 /* 0x23 */
634  , TDS_OPT_ARITHIGNOREOFF = 36 /* 0x24 */
635  , TDS_OPT_ARITHABORTOFF = 37 /* 0x25 */
636  , TDS_OPT_TRUNCABORT = 38 /* 0x26 */
637 } TDS_OPTION;
638 
639 typedef union tds_option_arg
640 {
641  TDS_TINYINT ti;
642  TDS_INT i;
643  TDS_CHAR *c;
645 
646 enum {
647  TDS_OPT_ARITHOVERFLOW = 0x01,
648  TDS_OPT_NUMERICTRUNC = 0x02
649 };
650 
651 enum TDS_OPT_DATEFIRST_CHOICE
652 {
653  TDS_OPT_MONDAY = 1, TDS_OPT_TUESDAY = 2, TDS_OPT_WEDNESDAY = 3, TDS_OPT_THURSDAY = 4, TDS_OPT_FRIDAY = 5, TDS_OPT_SATURDAY =
654  6, TDS_OPT_SUNDAY = 7
655 };
656 
657 enum TDS_OPT_DATEFORMAT_CHOICE
658 {
659  TDS_OPT_FMTMDY = 1, TDS_OPT_FMTDMY = 2, TDS_OPT_FMTYMD = 3, TDS_OPT_FMTYDM = 4, TDS_OPT_FMTMYD = 5, TDS_OPT_FMTDYM = 6
660 };
661 enum TDS_OPT_ISOLATION_CHOICE
662 {
663  TDS_OPT_LEVEL1 = 1, TDS_OPT_LEVEL3 = 3
664 };
665 
666 typedef enum tds_packet_type
667 {
668  TDS_QUERY = 1,
669  TDS_LOGIN = 2,
670  TDS_RPC = 3,
671  TDS_REPLY = 4,
672  TDS_CANCEL = 6,
673  TDS_BULK = 7,
674  TDS_NORMAL = 15,
675  TDS7_LOGIN = 16,
676  TDS7_AUTH = 17,
677  TDS8_PRELOGIN = 18
678 } TDS_PACKET_TYPE;
679 
680 typedef enum tds_encryption_level {
681  TDS_ENCRYPTION_OFF, TDS_ENCRYPTION_REQUEST, TDS_ENCRYPTION_REQUIRE
682 } TDS_ENCRYPTION_LEVEL;
683 
684 #define TDS_ZERO_FREE(x) do {free((x)); (x) = NULL;} while(0)
685 #define TDS_VECTOR_SIZE(x) (sizeof(x)/sizeof(x[0]))
686 
687 #if defined(__GNUC__) && __GNUC__ >= 3
688 # define TDS_LIKELY(x) __builtin_expect(!!(x), 1)
689 # define TDS_UNLIKELY(x) __builtin_expect(!!(x), 0)
690 #else
691 # define TDS_LIKELY(x) (x)
692 # define TDS_UNLIKELY(x) (x)
693 #endif
694 
695 /*
696  * TODO use system macros for optimization
697  * See mcrypt for reference and linux kernel source for optimization
698  * check if unaligned access and use fast write/read when implemented
699  */
700 #define TDS_BYTE_SWAP16(value) \
701  (((((unsigned short)value)<<8) & 0xFF00) | \
702  ((((unsigned short)value)>>8) & 0x00FF))
703 
704 #define TDS_BYTE_SWAP32(value) \
705  (((((unsigned long)value)<<24) & 0xFF000000) | \
706  ((((unsigned long)value)<< 8) & 0x00FF0000) | \
707  ((((unsigned long)value)>> 8) & 0x0000FF00) | \
708  ((((unsigned long)value)>>24) & 0x000000FF))
709 
710 #define is_end_token(x) (x==TDS_DONE_TOKEN || \
711  x==TDS_DONEPROC_TOKEN || \
712  x==TDS_DONEINPROC_TOKEN)
713 
714 #define is_hard_end_token(x) (x==TDS_DONE_TOKEN || \
715  x==TDS_DONEPROC_TOKEN)
716 
717 #define is_msg_token(x) (x==TDS_INFO_TOKEN || \
718  x==TDS_ERROR_TOKEN || \
719  x==TDS_EED_TOKEN)
720 
721 #define is_result_token(x) (x==TDS_RESULT_TOKEN || \
722  x==TDS_ROWFMT2_TOKEN || \
723  x==TDS7_RESULT_TOKEN || \
724  x==TDS_COLFMT_TOKEN || \
725  x==TDS_COLNAME_TOKEN || \
726  x==TDS_RETURNSTATUS_TOKEN)
727 
728 /* FIXME -- not a complete list */
729 #define is_fixed_type(x) (x==SYBINT1 || \
730  x==SYBINT2 || \
731  x==SYBINT4 || \
732  x==SYBINT8 || \
733  x==SYBREAL || \
734  x==SYBFLT8 || \
735  x==SYBDATETIME || \
736  x==SYBDATETIME4 || \
737  x==SYBBIT || \
738  x==SYBMONEY || \
739  x==SYBMONEY4 || \
740  x==SYBVOID || \
741  x==SYBUNIQUE)
742 #define is_nullable_type(x) ( \
743  x==SYBBITN || \
744  x==SYBINTN || \
745  x==SYBFLTN || \
746  x==SYBMONEYN || \
747  x==SYBDATETIMN || \
748  x==SYBVARCHAR || \
749  x==SYBVARBINARY || \
750  x==SYBTEXT || \
751  x==SYBNTEXT || \
752  x==SYBIMAGE)
753 
754 #define is_variable_type(x) ( \
755  (x)==SYBTEXT || \
756  (x)==SYBIMAGE || \
757  (x)==SYBNTEXT || \
758  (x)==SYBCHAR || \
759  (x)==SYBVARCHAR || \
760  (x)==SYBBINARY || \
761  (x)==SYBVARBINARY || \
762  (x)==SYBLONGBINARY || \
763  (x)==XSYBCHAR || \
764  (x)==XSYBVARCHAR || \
765  (x)==XSYBNVARCHAR || \
766  (x)==XSYBNCHAR)
767 
768 #define is_blob_type(x) (x==SYBTEXT || x==SYBIMAGE || x==SYBNTEXT)
769 #define is_blob_col(x) ((x)->column_varint_size > 2)
770 /* large type means it has a two byte size field */
771 /* define is_large_type(x) (x>128) */
772 #define is_numeric_type(x) (x==SYBNUMERIC || x==SYBDECIMAL)
773 #define is_unicode_type(x) (x==XSYBNVARCHAR || x==XSYBNCHAR || x==SYBNTEXT || x==SYBMSXML)
774 #define is_collate_type(x) (x==XSYBVARCHAR || x==XSYBCHAR || x==SYBTEXT || x==XSYBNVARCHAR || x==XSYBNCHAR || x==SYBNTEXT)
775 #define is_ascii_type(x) ( x==XSYBCHAR || x==XSYBVARCHAR || x==SYBTEXT || x==SYBCHAR || x==SYBVARCHAR)
776 #define is_char_type(x) (is_unicode_type(x) || is_ascii_type(x))
777 #define is_similar_type(x, y) ((is_char_type(x) && is_char_type(y)) || ((is_unicode_type(x) && is_unicode_type(y))))
778 
779 
780 #define TDS_MAX_CAPABILITY 22
781 #define MAXPRECISION 77
782 #define TDS_MAX_CONN 4096
783 #define TDS_MAX_DYNID_LEN 30
784 
785 /* defaults to use if no others are found */
786 #define TDS_DEF_SERVER "SYBASE"
787 #define TDS_DEF_BLKSZ 512
788 #define TDS_DEF_CHARSET "iso_1"
789 #define TDS_DEF_LANG "us_english"
790 #if TDS42
791 #define TDS_DEFAULT_VERSION 0x402
792 #define TDS_DEF_PORT 1433
793 #elif TDS46
794 #define TDS_DEFAULT_VERSION 0x406
795 #define TDS_DEF_PORT 4000
796 #elif TDS70
797 #define TDS_DEFAULT_VERSION 0x700
798 #define TDS_DEF_PORT 1433
799 #elif TDS71
800 #define TDS_DEFAULT_VERSION 0x701
801 #define TDS_DEF_PORT 1433
802 #elif TDS72
803 #define TDS_DEFAULT_VERSION 0x702
804 #define TDS_DEF_PORT 1433
805 #else
806 #define TDS_DEFAULT_VERSION 0x500
807 #define TDS_DEF_PORT 4000
808 #endif
809 
810 /* normalized strings from freetds.conf file */
811 #define TDS_STR_VERSION "tds version"
812 #define TDS_STR_BLKSZ "initial block size"
813 #define TDS_STR_SWAPDT "swap broken dates"
814 #define TDS_STR_DUMPFILE "dump file"
815 #define TDS_STR_DEBUGLVL "debug level"
816 #define TDS_STR_DEBUGFLAGS "debug flags"
817 #define TDS_STR_TIMEOUT "timeout"
818 #define TDS_STR_QUERY_TIMEOUT "query timeout"
819 #define TDS_STR_CONNTIMEOUT "connect timeout"
820 #define TDS_STR_HOSTNAME "hostname"
821 #define TDS_STR_HOST "host"
822 #define TDS_STR_PORT "port"
823 #define TDS_STR_TEXTSZ "text size"
824 /* for big endian hosts */
825 #define TDS_STR_EMUL_LE "emulate little endian"
826 #define TDS_STR_CHARSET "charset"
827 #define TDS_STR_CLCHARSET "client charset"
828 #define TDS_STR_LANGUAGE "language"
829 #define TDS_STR_APPENDMODE "dump file append"
830 #define TDS_STR_DATEFMT "date format"
831 #define TDS_STR_INSTANCE "instance"
832 #define TDS_STR_ASA_DATABASE "asa database"
833 #define TDS_STR_ENCRYPTION "encryption"
834 #define TDS_STR_USENTLMV2 "use ntlmv2"
835 /* conf values */
836 #define TDS_STR_ENCRYPTION_OFF "off"
837 #define TDS_STR_ENCRYPTION_REQUEST "request"
838 #define TDS_STR_ENCRYPTION_REQUIRE "require"
839 /* Defines to enable optional GSSAPI delegation */
840 #define TDS_GSSAPI_DELEGATION "enable gssapi delegation"
841 /* Kerberos realm name */
842 #define TDS_STR_REALM "realm"
843 
844 
845 /* TODO do a better check for alignment than this */
846 typedef union
847 {
848  void *p;
849  int i;
851 
852 #define TDS_ALIGN_SIZE sizeof(tds_align_struct)
853 
854 #define TDS_MAX_LOGIN_STR_SZ 128
855 typedef struct tds_login
856 {
857  DSTR server_name;
858  int port;
859  TDS_USMALLINT tds_version; /* TDS version */
860  int block_size;
861  DSTR language; /* e.g. us-english */
862  DSTR server_charset; /* e.g. iso_1 */
863  TDS_INT connect_timeout;
864  DSTR client_host_name;
865  DSTR app_name;
866  DSTR user_name;
867  DSTR password;
868 
869  DSTR library; /* Ct-Library, DB-Library, TDS-Library or ODBC */
870  TDS_TINYINT encryption_level;
871 
872  TDS_INT query_timeout;
873  unsigned char capabilities[TDS_MAX_CAPABILITY];
874  DSTR client_charset;
875  DSTR database;
876  unsigned int bulk_copy:1;
877  unsigned int suppress_language:1;
878 } TDSLOGIN;
879 
880 typedef struct tds_connection
881 {
882  /* first part of structure is the same of login one */
884  int port;
885  TDS_USMALLINT tds_version;
886  int block_size;
887  DSTR language;
889  TDS_INT connect_timeout;
890  DSTR client_host_name;
891  DSTR server_host_name;
893  DSTR app_name;
896  DSTR library;
897  TDS_TINYINT encryption_level;
898 
899  TDS_INT query_timeout;
900  unsigned char capabilities[TDS_MAX_CAPABILITY];
901  unsigned char option_flag2;
902  DSTR client_charset;
903 
905  DSTR instance_name;
906  DSTR database;
907  DSTR dump_file;
908  int debug_flags;
909  int text_size;
910  unsigned int broken_dates:1;
911  unsigned int emul_little_endian:1;
912  unsigned int bulk_copy:1;
913  unsigned int suppress_language:1;
914  unsigned int gssapi_use_delegation:1;
915  unsigned int use_ntlmv2:1;
916 } TDSCONNECTION;
917 
918 typedef struct tds_locale
919 {
920  char *language;
921  char *server_charset;
922  char *date_fmt;
923 } TDSLOCALE;
924 
929 typedef struct tds_blob
930 {
931  TDS_CHAR *textvalue;
932  TDS_CHAR textptr[16];
933  TDS_CHAR timestamp[8];
934 } TDSBLOB;
935 
939 typedef struct tds_variant
940 {
941  /* this MUST have same position and place of textvalue in tds_blob */
942  TDS_CHAR *data;
943  TDS_INT size;
944  TDS_INT data_len;
945  TDS_UCHAR type;
946  TDS_UCHAR collation[5];
947 } TDSVARIANT;
948 
952 typedef struct
953 {
954  TDS_USMALLINT locale_id; /* master..syslanguages.lcid */
955  TDS_USMALLINT flags;
956  TDS_UCHAR charset_id; /* or zero */
958 
959 /* SF stands for "sort flag" */
960 #define TDS_SF_BIN (TDS_USMALLINT) 0x100
961 #define TDS_SF_WIDTH_INSENSITIVE (TDS_USMALLINT) 0x080
962 #define TDS_SF_KATATYPE_INSENSITIVE (TDS_USMALLINT) 0x040
963 #define TDS_SF_ACCENT_SENSITIVE (TDS_USMALLINT) 0x020
964 #define TDS_SF_CASE_INSENSITIVE (TDS_USMALLINT) 0x010
965 
966 /* UT stands for user type */
967 #define TDS_UT_TIMESTAMP 80
968 
969 
974 typedef struct tds_encoding
975 {
976  const char *name;
977  unsigned char min_bytes_per_char;
978  unsigned char max_bytes_per_char;
979  unsigned char canonic;
980 } TDS_ENCODING;
981 
982 typedef struct tds_bcpcoldata
983 {
984  TDS_UCHAR *data;
985  TDS_INT datalen;
986  TDS_INT is_null;
987 } BCPCOLDATA;
988 
989 
990 enum
991 { TDS_SYSNAME_SIZE = 512 };
992 
996 typedef struct tds_column
997 {
998  TDS_SMALLINT column_type;
1003  TDS_INT column_usertype;
1004  TDS_INT column_flags;
1005 
1006  TDS_INT column_size;
1008  TDS_TINYINT column_varint_size;
1010  TDS_TINYINT column_prec;
1011  TDS_TINYINT column_scale;
1013  TDS_SMALLINT column_namelen;
1014  TDS_SMALLINT table_namelen;
1015  struct
1016  {
1017  TDS_SMALLINT column_type;
1018  TDS_INT column_size;
1019  } on_server;
1020 
1023  TDS_CHAR table_name[TDS_SYSNAME_SIZE];
1024  TDS_CHAR column_name[TDS_SYSNAME_SIZE];
1025  char * table_column_name;
1026 
1027  unsigned char *column_data;
1028  void (*column_data_free)(struct tds_column *column);
1029  unsigned int column_nullable:1;
1030  unsigned int column_writeable:1;
1031  unsigned int column_identity:1;
1032  unsigned int column_key:1;
1033  unsigned int column_hidden:1;
1034  unsigned int column_output:1;
1035  unsigned int column_timestamp:1;
1036  TDS_UCHAR column_collation[5];
1037 
1038  /* additional fields flags for compute results */
1039  TDS_TINYINT column_operator;
1040  TDS_SMALLINT column_operand;
1041 
1042  /* FIXME this is data related, not column */
1045 
1046  /* related to binding or info stored by client libraries */
1047  /* FIXME find a best place to store these data, some are unused */
1048  TDS_SMALLINT column_bindtype;
1049  TDS_SMALLINT column_bindfmt;
1050  TDS_UINT column_bindlen;
1051  TDS_SMALLINT *column_nullbind;
1052  TDS_CHAR *column_varaddr;
1053  TDS_INT *column_lenbind;
1054  TDS_INT column_textpos;
1055  TDS_INT column_text_sqlgetdatapos;
1056  TDS_CHAR column_text_sqlputdatainfo;
1057 
1058  BCPCOLDATA *bcp_column_data;
1068  TDS_INT bcp_term_len;
1069  TDS_CHAR *bcp_terminator;
1070 } TDSCOLUMN;
1071 
1072 
1074 typedef struct tds_result_info
1075 {
1076  /* TODO those fields can became a struct */
1077  TDS_SMALLINT num_cols;
1078  TDSCOLUMN **columns;
1079  TDS_INT row_size;
1080  TDS_INT ref_count;
1081  unsigned char *current_row;
1082  void (*row_free)(struct tds_result_info* result, unsigned char *row);
1083 
1084  TDS_SMALLINT rows_exist;
1085  /* TODO remove ?? used only in dblib */
1086  TDS_INT row_count;
1087  /* TODO remove ?? used only in dblib */
1088  TDS_TINYINT more_results;
1089  TDS_SMALLINT computeid;
1090  TDS_SMALLINT *bycolumns;
1091  TDS_SMALLINT by_cols;
1092 } TDSRESULTINFO;
1093 
1095 typedef enum _TDS_STATE
1096 {
1102 } TDS_STATE;
1103 
1104 #define TDS_DBG_LOGIN __FILE__, ((__LINE__ << 4) | 11)
1105 #define TDS_DBG_HEADER __FILE__, ((__LINE__ << 4) | 10)
1106 #define TDS_DBG_FUNC __FILE__, ((__LINE__ << 4) | 7)
1107 #define TDS_DBG_INFO2 __FILE__, ((__LINE__ << 4) | 6)
1108 #define TDS_DBG_INFO1 __FILE__, ((__LINE__ << 4) | 5)
1109 #define TDS_DBG_NETWORK __FILE__, ((__LINE__ << 4) | 4)
1110 #define TDS_DBG_WARN __FILE__, ((__LINE__ << 4) | 3)
1111 #define TDS_DBG_ERROR __FILE__, ((__LINE__ << 4) | 2)
1112 #define TDS_DBG_SEVERE __FILE__, ((__LINE__ << 4) | 1)
1113 
1114 #define TDS_DBGFLAG_FUNC 0x80
1115 #define TDS_DBGFLAG_INFO2 0x40
1116 #define TDS_DBGFLAG_INFO1 0x20
1117 #define TDS_DBGFLAG_NETWORK 0x10
1118 #define TDS_DBGFLAG_WARN 0x08
1119 #define TDS_DBGFLAG_ERROR 0x04
1120 #define TDS_DBGFLAG_SEVERE 0x02
1121 #define TDS_DBGFLAG_ALL 0xfff
1122 #define TDS_DBGFLAG_LOGIN 0x0800
1123 #define TDS_DBGFLAG_HEADER 0x0400
1124 #define TDS_DBGFLAG_PID 0x1000
1125 #define TDS_DBGFLAG_TIME 0x2000
1126 #define TDS_DBGFLAG_SOURCE 0x4000
1127 #define TDS_DBGFLAG_THREAD 0x8000
1128 
1129 #if 0
1130 
1135 enum TDS_DBG_LOG_STATE
1136 {
1137  TDS_DBG_LOGIN = (1 << 0)
1139  , TDS_DBG_API = (1 << 1)
1140  , TDS_DBG_ASYNC = (1 << 2)
1141  , TDS_DBG_DIAG = (1 << 3)
1142  , TDS_DBG_error = (1 << 4)
1143  /* TODO: ^^^^^ make upper case when old #defines (above) are removed */
1144  /* Log FreeTDS runtime/logic error occurs. */
1145  , TDS_DBG_PACKET = (1 << 5)
1146  , TDS_DBG_LIBTDS = (1 << 6)
1147  , TDS_DBG_CONFIG = (1 << 7)
1148  , TDS_DBG_DEFAULT = 0xFE
1149 };
1150 #endif
1151 
1152 typedef struct tds_result_info TDSCOMPUTEINFO;
1153 
1154 typedef TDSRESULTINFO TDSPARAMINFO;
1155 
1156 typedef struct tds_message
1157 {
1158  TDS_CHAR *server;
1159  TDS_CHAR *message;
1160  TDS_CHAR *proc_name;
1161  TDS_CHAR *sql_state;
1162  TDS_UINT msgno;
1163  TDS_INT line_number;
1164  /* -1 .. 255 */
1165  TDS_SMALLINT state;
1166  TDS_TINYINT priv_msg_type;
1167  TDS_TINYINT severity;
1168  /* for library-generated errors */
1169  int oserr;
1170 } TDSMESSAGE;
1171 
1172 typedef struct tds_upd_col
1173 {
1174  struct tds_upd_col *next;
1175  TDS_INT colnamelength;
1176  char * columnname;
1177 } TDSUPDCOL;
1178 
1179 typedef enum {
1180  TDS_CURSOR_STATE_UNACTIONED = 0 /* initial value */
1181  , TDS_CURSOR_STATE_REQUESTED = 1 /* called by ct_cursor */
1182  , TDS_CURSOR_STATE_SENT = 2 /* sent to server */
1183  , TDS_CURSOR_STATE_ACTIONED = 3 /* acknowledged by server */
1184 } TDS_CURSOR_STATE;
1185 
1186 typedef struct tds_cursor_status
1187 {
1188  TDS_CURSOR_STATE declare;
1189  TDS_CURSOR_STATE cursor_row;
1190  TDS_CURSOR_STATE open;
1191  TDS_CURSOR_STATE fetch;
1192  TDS_CURSOR_STATE close;
1193  TDS_CURSOR_STATE dealloc;
1195 
1196 typedef enum tds_cursor_operation
1197 {
1198  TDS_CURSOR_POSITION = 0,
1199  TDS_CURSOR_UPDATE = 1,
1200  TDS_CURSOR_DELETE = 2,
1201  TDS_CURSOR_INSERT = 4
1202 } TDS_CURSOR_OPERATION;
1203 
1204 typedef enum tds_cursor_fetch
1205 {
1206  TDS_CURSOR_FETCH_NEXT = 1,
1207  TDS_CURSOR_FETCH_PREV,
1208  TDS_CURSOR_FETCH_FIRST,
1209  TDS_CURSOR_FETCH_LAST,
1210  TDS_CURSOR_FETCH_ABSOLUTE,
1211  TDS_CURSOR_FETCH_RELATIVE
1212 } TDS_CURSOR_FETCH;
1213 
1217 typedef struct tds_cursor
1218 {
1219  struct tds_cursor *next;
1220  TDS_INT ref_count;
1221  TDS_TINYINT cursor_name_len;
1222  char *cursor_name;
1223  TDS_INT cursor_id;
1224  TDS_TINYINT options;
1225  TDS_TINYINT hasargs;
1226  TDS_USMALLINT query_len;
1227  char *query;
1228  /* TODO for updatable columns */
1229  /* TDS_TINYINT number_upd_cols; */
1230  /* TDSUPDCOL *cur_col_list; */
1231  TDS_INT cursor_rows;
1232  /* TDSPARAMINFO *params; */
1234  TDS_SMALLINT srv_status;
1235  TDSRESULTINFO *res_info;
1236  TDS_INT type, concurrency;
1237 } TDSCURSOR;
1238 
1242 typedef struct tds_env
1243 {
1244  int block_size;
1245  char *language;
1246  char *charset;
1247  char *database;
1248 } TDSENV;
1249 
1253 typedef struct tds_dynamic
1254 {
1255  struct tds_dynamic *next;
1261  char id[30];
1262  /* int dyn_state; */ /* TODO use it */
1264  TDS_INT num_id;
1281  char *query;
1282 } TDSDYNAMIC;
1283 
1284 typedef enum {
1285  TDS_MULTIPLE_QUERY,
1286  TDS_MULTIPLE_EXECUTE,
1287  TDS_MULTIPLE_RPC
1288 } TDS_MULTIPLE_TYPE;
1289 
1290 typedef struct tds_multiple
1291 {
1292  TDS_MULTIPLE_TYPE type;
1293  unsigned int flags;
1294 } TDSMULTIPLE;
1295 
1296 /* forward declaration */
1297 typedef struct tds_context TDSCONTEXT;
1298 typedef int (*err_handler_t) (const TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
1299 
1301 {
1302  TDSLOCALE *locale;
1303  void *parent;
1304  /* handlers */
1305  int (*msg_handler) (const TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
1306  int (*err_handler) (const TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
1307  int (*int_handler) (void *);
1308 };
1309 
1310 enum TDS_ICONV_ENTRY
1311 {
1312  client2ucs2
1313  , client2server_chardata
1314  , iso2server_metadata
1315  , initial_char_conv_count /* keep last */
1316 };
1317 
1318 typedef struct tds_authentication
1319 {
1320  TDS_UCHAR *packet;
1321  int packet_len;
1322  int (*free)(TDSSOCKET * tds, struct tds_authentication * auth);
1323  int (*handle_next)(TDSSOCKET * tds, struct tds_authentication * auth, size_t len);
1325 
1330 {
1331  TDS_SYS_SOCKET s;
1333  TDS_USMALLINT tds_version;
1334  TDS_UINT product_version;
1335  char *product_name;
1336 
1337  unsigned char capabilities[TDS_MAX_CAPABILITY];
1338  unsigned int broken_dates:1;
1339  unsigned int emul_little_endian:1;
1340  unsigned int use_iconv:1;
1341  unsigned int tds71rev1:1;
1342 
1343  unsigned char *in_buf;
1344  unsigned char *out_buf;
1345  unsigned int in_buf_max;
1346  unsigned in_pos;
1347  unsigned out_pos;
1348  unsigned in_len;
1350  unsigned char in_flag;
1351  unsigned char out_flag;
1352  void *parent;
1353 
1360  TDSRESULTINFO *res_info;
1361  TDS_INT num_comp_info;
1362  TDSCOMPUTEINFO **comp_info;
1363  TDSPARAMINFO *param_info;
1366  TDS_TINYINT has_status;
1367  TDS_INT ret_status;
1368  TDS_STATE state;
1369 
1370  volatile
1371  unsigned char in_cancel;
1373  TDS_INT8 rows_affected;
1374  TDS_INT query_timeout;
1375  TDSENV env;
1376 
1380  const TDSCONTEXT *tds_ctx;
1381  int char_conv_count;
1382  TDSICONV **char_convs;
1383 
1386  int spid;
1387  TDS_UCHAR collation[5];
1388  TDS_UCHAR tds9_transaction[8];
1389  void (*env_chg_func) (TDSSOCKET * tds, int type, char *oldval, char *newval);
1390  int internal_sp_called;
1391 
1392  void *tls_session;
1393  void *tls_credentials;
1394  TDSAUTHENTICATION *authentication;
1395  int option_value;
1396 };
1397 
1398 int tds_init_write_buf(TDSSOCKET * tds);
1399 void tds_free_result_info(TDSRESULTINFO * info);
1400 void tds_free_socket(TDSSOCKET * tds);
1401 void tds_free_connection(TDSCONNECTION * connection);
1402 void tds_free_all_results(TDSSOCKET * tds);
1403 void tds_free_results(TDSRESULTINFO * res_info);
1404 void tds_free_param_results(TDSPARAMINFO * param_info);
1405 void tds_free_param_result(TDSPARAMINFO * param_info);
1406 void tds_free_msg(TDSMESSAGE * message);
1407 void tds_cursor_deallocated(TDSSOCKET *tds, TDSCURSOR *cursor);
1408 void tds_release_cursor(TDSSOCKET *tds, TDSCURSOR *cursor);
1409 void tds_free_bcp_column_data(BCPCOLDATA * coldata);
1410 
1411 int tds_put_n(TDSSOCKET * tds, const void *buf, size_t n);
1412 int tds_put_string(TDSSOCKET * tds, const char *buf, int len);
1413 int tds_put_int(TDSSOCKET * tds, TDS_INT i);
1414 int tds_put_int8(TDSSOCKET * tds, TDS_INT8 i);
1415 int tds_put_smallint(TDSSOCKET * tds, TDS_SMALLINT si);
1417 #define tds_put_tinyint(tds, ti) tds_put_byte(tds,ti)
1418 int tds_put_byte(TDSSOCKET * tds, unsigned char c);
1419 TDSRESULTINFO *tds_alloc_results(int num_cols);
1420 TDSCOMPUTEINFO **tds_alloc_compute_results(TDSSOCKET * tds, int num_cols, int by_cols);
1421 TDSCONTEXT *tds_alloc_context(void * parent);
1422 void tds_free_context(TDSCONTEXT * locale);
1423 TDSSOCKET *tds_alloc_socket(TDSCONTEXT * context, int bufsize);
1424 
1425 /* config.c */
1426 int tds_default_port(int major, int minor);
1428 typedef void (*TDSCONFPARSE) (const char *option, const char *value, void *param);
1429 int tds_read_conf_section(FILE * in, const char *section, TDSCONFPARSE tds_conf_parse, void *parse_param);
1430 int tds_read_conf_file(TDSCONNECTION * connection, const char *server);
1431 void tds_parse_conf_section(const char *option, const char *value, void *param);
1433 void tds_fix_connection(TDSCONNECTION * connection);
1434 TDS_USMALLINT tds_config_verstr(const char *tdsver, TDSCONNECTION * connection);
1435 int tds_lookup_host(const char *servername, char *ip);
1436 int tds_set_interfaces_file_loc(const char *interfloc);
1437 extern const char STD_DATETIME_FMT[];
1438 int tds_config_boolean(const char *value);
1439 
1440 TDSLOCALE *tds_get_locale(void);
1441 int tds_alloc_row(TDSRESULTINFO * res_info);
1442 int tds_alloc_compute_row(TDSCOMPUTEINFO * res_info);
1443 BCPCOLDATA * tds_alloc_bcp_column_data(int column_size);
1444 unsigned char *tds7_crypt_pass(const unsigned char *clear_pass, size_t len, unsigned char *crypt_pass);
1445 TDSDYNAMIC *tds_lookup_dynamic(TDSSOCKET * tds, const char *id);
1446 /*@observer@*/ const char *tds_prtype(int token);
1447 int tds_get_varint_size(TDSSOCKET * tds, int datatype);
1448 int tds_get_cardinal_type(int datatype, int usertype);
1449 
1450 
1451 
1452 /* iconv.c */
1453 void tds_iconv_open(TDSSOCKET * tds, const char *charset);
1454 void tds_iconv_close(TDSSOCKET * tds);
1455 void tds_srv_charset_changed(TDSSOCKET * tds, const char *charset);
1456 void tds7_srv_charset_changed(TDSSOCKET * tds, int sql_collate, int lcid);
1457 int tds_iconv_alloc(TDSSOCKET * tds);
1458 void tds_iconv_free(TDSSOCKET * tds);
1459 TDSICONV *tds_iconv_from_collate(TDSSOCKET * tds, TDS_UCHAR collate[5]);
1460 
1461 /* threadsafe.c */
1462 char *tds_timestamp_str(char *str, int maxlen);
1463 struct tm *tds_localtime_r(const time_t *timep, struct tm *result);
1464 struct hostent *tds_gethostbyname_r(const char *servername, struct hostent *result, char *buffer, int buflen, int *h_errnop);
1465 struct hostent *tds_gethostbyaddr_r(const char *addr, int len, int type, struct hostent *result, char *buffer, int buflen,
1466  int *h_errnop);
1467 struct servent *tds_getservbyname_r(const char *name, const char *proto, struct servent *result, char *buffer, int buflen);
1468 #ifdef INADDR_NONE
1469 const char *tds_inet_ntoa_r(struct in_addr iaddr, char *ip, size_t len);
1470 #endif
1471 char *tds_get_homedir(void);
1472 
1473 /* mem.c */
1475 void tds_free_input_params(TDSDYNAMIC * dyn);
1476 void tds_free_dynamic(TDSSOCKET * tds, TDSDYNAMIC * dyn);
1477 TDSSOCKET *tds_realloc_socket(TDSSOCKET * tds, size_t bufsize);
1478 char *tds_alloc_client_sqlstate(int msgno);
1479 char *tds_alloc_lookup_sqlstate(TDSSOCKET * tds, int msgno);
1480 TDSLOGIN *tds_alloc_login(void);
1481 TDSDYNAMIC *tds_alloc_dynamic(TDSSOCKET * tds, const char *id);
1482 void tds_free_login(TDSLOGIN * login);
1484 TDSLOCALE *tds_alloc_locale(void);
1485 void *tds_alloc_param_data(TDSCOLUMN * curparam);
1486 void tds_free_locale(TDSLOCALE * locale);
1487 TDSCURSOR * tds_alloc_cursor(TDSSOCKET * tds, const char *name, TDS_INT namelen, const char *query, TDS_INT querylen);
1488 void tds_free_row(TDSRESULTINFO * res_info, unsigned char *row);
1489 
1490 /* login.c */
1491 void tds_set_packet(TDSLOGIN * tds_login, int packet_size);
1492 void tds_set_port(TDSLOGIN * tds_login, int port);
1493 void tds_set_passwd(TDSLOGIN * tds_login, const char *password);
1494 void tds_set_bulk(TDSLOGIN * tds_login, TDS_TINYINT enabled);
1495 void tds_set_user(TDSLOGIN * tds_login, const char *username);
1496 void tds_set_app(TDSLOGIN * tds_login, const char *application);
1497 void tds_set_host(TDSLOGIN * tds_login, const char *hostname);
1498 void tds_set_library(TDSLOGIN * tds_login, const char *library);
1499 void tds_set_server(TDSLOGIN * tds_login, const char *server);
1500 void tds_set_client_charset(TDSLOGIN * tds_login, const char *charset);
1501 void tds_set_language(TDSLOGIN * tds_login, const char *language);
1502 void tds_set_database_name(TDSLOGIN * tds_login, const char *dbname);
1503 void tds_set_version(TDSLOGIN * tds_login, TDS_TINYINT major_ver, TDS_TINYINT minor_ver);
1504 void tds_set_capabilities(TDSLOGIN * tds_login, unsigned char *capabilities, int size);
1505 int tds_connect_and_login(TDSSOCKET * tds, TDSCONNECTION * connection);
1506 
1507 /* query.c */
1508 int tds_submit_query(TDSSOCKET * tds, const char *query);
1509 int tds_submit_query_params(TDSSOCKET * tds, const char *query, TDSPARAMINFO * params);
1510 int tds_submit_queryf(TDSSOCKET * tds, const char *queryf, ...);
1511 int tds_submit_prepare(TDSSOCKET * tds, const char *query, const char *id, TDSDYNAMIC ** dyn_out, TDSPARAMINFO * params);
1512 int tds_submit_execdirect(TDSSOCKET * tds, const char *query, TDSPARAMINFO * params);
1513 int tds8_submit_prepexec(TDSSOCKET * tds, const char *query, const char *id, TDSDYNAMIC ** dyn_out, TDSPARAMINFO * params);
1514 int tds_submit_execute(TDSSOCKET * tds, TDSDYNAMIC * dyn);
1515 int tds_send_cancel(TDSSOCKET * tds);
1516 const char *tds_next_placeholder(const char *start);
1517 int tds_count_placeholders(const char *query);
1518 int tds_needs_unprepare(TDSSOCKET * tds, TDSDYNAMIC * dyn);
1519 int tds_submit_unprepare(TDSSOCKET * tds, TDSDYNAMIC * dyn);
1520 int tds_submit_rpc(TDSSOCKET * tds, const char *rpc_name, TDSPARAMINFO * params);
1521 int tds_submit_optioncmd(TDSSOCKET * tds, TDS_OPTION_CMD command, TDS_OPTION option, TDS_OPTION_ARG *param, TDS_INT param_size);
1522 int tds_quote_id(TDSSOCKET * tds, char *buffer, const char *id, int idlen);
1523 int tds_quote_string(TDSSOCKET * tds, char *buffer, const char *str, int len);
1524 const char *tds_skip_comment(const char *s);
1525 const char *tds_skip_quoted(const char *s);
1526 
1527 int tds_cursor_declare(TDSSOCKET * tds, TDSCURSOR * cursor, TDSPARAMINFO *params, int *send);
1528 int tds_cursor_setrows(TDSSOCKET * tds, TDSCURSOR * cursor, int *send);
1529 int tds_cursor_open(TDSSOCKET * tds, TDSCURSOR * cursor, TDSPARAMINFO *params, int *send);
1530 int tds_cursor_fetch(TDSSOCKET * tds, TDSCURSOR * cursor, TDS_CURSOR_FETCH fetch_type, TDS_INT i_row);
1531 int tds_cursor_get_cursor_info(TDSSOCKET * tds, TDSCURSOR * cursor, TDS_UINT * row_number, TDS_UINT * row_count);
1532 int tds_cursor_close(TDSSOCKET * tds, TDSCURSOR * cursor);
1533 int tds_cursor_dealloc(TDSSOCKET * tds, TDSCURSOR * cursor);
1534 int tds_cursor_update(TDSSOCKET * tds, TDSCURSOR * cursor, TDS_CURSOR_OPERATION op, TDS_INT i_row, TDSPARAMINFO * params);
1535 int tds_cursor_setname(TDSSOCKET * tds, TDSCURSOR * cursor);
1536 
1537 int tds_multiple_init(TDSSOCKET *tds, TDSMULTIPLE *multiple, TDS_MULTIPLE_TYPE type);
1538 int tds_multiple_done(TDSSOCKET *tds, TDSMULTIPLE *multiple);
1539 int tds_multiple_query(TDSSOCKET *tds, TDSMULTIPLE *multiple, const char *query, TDSPARAMINFO * params);
1540 int tds_multiple_execute(TDSSOCKET *tds, TDSMULTIPLE *multiple, TDSDYNAMIC * dyn);
1541 
1542 /* token.c */
1543 int tds_process_cancel(TDSSOCKET * tds);
1544 #ifdef WORDS_BIGENDIAN
1545 void tds_swap_datatype(int coltype, unsigned char *buf);
1546 #endif
1547 void tds_swap_numeric(TDS_NUMERIC *num);
1548 int tds_get_token_size(int marker);
1551 int tds5_send_optioncmd(TDSSOCKET * tds, TDS_OPTION_CMD tds_command, TDS_OPTION tds_option, TDS_OPTION_ARG * tds_argument,
1552  TDS_INT * tds_argsize);
1553 int tds_process_tokens(TDSSOCKET * tds, /*@out@*/ TDS_INT * result_type, /*@out@*/ int *done_flags, unsigned flag);
1554 
1555 /* data.c */
1556 void tds_set_param_type(TDSSOCKET * tds, TDSCOLUMN * curcol, TDS_SERVER_TYPE type);
1557 void tds_set_column_type(TDSSOCKET * tds, TDSCOLUMN * curcol, int type);
1558 TDS_INT tds_data_get_info(TDSSOCKET *tds, TDSCOLUMN *col);
1559 
1560 
1561 /* tds_convert.c */
1562 TDS_INT tds_datecrack(TDS_INT datetype, const void *di, TDSDATEREC * dr);
1563 int tds_get_conversion_type(int srctype, int colsize);
1564 extern const char tds_hex_digits[];
1565 
1566 /* write.c */
1567 int tds_flush_packet(TDSSOCKET * tds);
1568 int tds_put_buf(TDSSOCKET * tds, const unsigned char *buf, int dsize, int ssize);
1569 
1570 /* read.c */
1571 unsigned char tds_get_byte(TDSSOCKET * tds);
1572 void tds_unget_byte(TDSSOCKET * tds);
1573 unsigned char tds_peek(TDSSOCKET * tds);
1574 TDS_SMALLINT tds_get_smallint(TDSSOCKET * tds);
1575 TDS_INT tds_get_int(TDSSOCKET * tds);
1576 TDS_INT8 tds_get_int8(TDSSOCKET * tds);
1577 int tds_get_string(TDSSOCKET * tds, int string_len, char *dest, size_t dest_size);
1578 int tds_get_char_data(TDSSOCKET * tds, char *dest, size_t wire_size, TDSCOLUMN * curcol);
1579 void *tds_get_n(TDSSOCKET * tds, /*@out@*/ /*@null@*/ void *dest, int n);
1580 int tds_get_size_by_type(int servertype);
1581 
1582 
1583 /* util.c */
1584 int tdserror (const TDSCONTEXT * tds_ctx, TDSSOCKET * tds, int msgno, int errnum);
1586 void tds_set_parent(TDSSOCKET * tds, void *the_parent);
1587 int tds_swap_bytes(unsigned char *buf, int bytes);
1588 int tds_version(TDSSOCKET * tds_socket, char *pversion_string);
1589 unsigned int tds_gettime_ms(void);
1590 int tds_get_req_capability(TDSSOCKET * tds, int cap);
1591 
1592 /* log.c */
1593 void tdsdump_off(void);
1594 void tdsdump_on(void);
1595 int tdsdump_isopen(void);
1596 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
1597 #pragma GCC visibility pop
1598 #endif
1599 int tdsdump_open(const char *filename);
1600 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
1601 #pragma GCC visibility push(hidden)
1602 #endif
1603 void tdsdump_close(void);
1604 void tdsdump_dump_buf(const char* file, unsigned int level_line, const char *msg, const void *buf, size_t length);
1605 void tdsdump_col(const TDSCOLUMN *col);
1606 #undef tdsdump_log
1607 void tdsdump_log(const char* file, unsigned int level_line, const char *fmt, ...)
1608 #if defined(__GNUC__) && __GNUC__ >= 2
1609  __attribute__ ((__format__ (__printf__, 3, 4)))
1610 #endif
1611 ;
1612 #define tdsdump_log if (TDS_UNLIKELY(tds_write_dump)) tdsdump_log
1613 
1614 extern int tds_write_dump;
1615 extern int tds_debug_flags;
1616 extern int tds_g_append_mode;
1617 
1618 /* net.c */
1619 int tds_lastpacket(TDSSOCKET * tds);
1620 TDSERRNO tds_open_socket(TDSSOCKET * tds, const char *ip_addr, unsigned int port, int timeout, int *p_oserr);
1621 int tds_close_socket(TDSSOCKET * tds);
1622 int tds_read_packet(TDSSOCKET * tds);
1623 int tds_write_packet(TDSSOCKET * tds, unsigned char final);
1624 int tds7_get_instance_ports(FILE *output, const char *ip_addr);
1625 int tds7_get_instance_port(const char *ip_addr, const char *instance);
1626 int tds_ssl_init(TDSSOCKET *tds);
1627 void tds_ssl_deinit(TDSSOCKET *tds);
1628 const char *tds_prwsaerror(int erc);
1629 
1630 
1631 
1632 /* vstrbuild.c */
1633 int tds_vstrbuild(char *buffer, int buflen, int *resultlen, char *text, int textlen, const char *formats, int formatlen,
1634  va_list ap);
1635 
1636 /* numeric.c */
1637 char *tds_money_to_string(const TDS_MONEY * money, char *s);
1638 TDS_INT tds_numeric_to_string(const TDS_NUMERIC * numeric, char *s);
1639 TDS_INT tds_numeric_change_prec_scale(TDS_NUMERIC * numeric, unsigned char new_prec, unsigned char new_scale);
1640 
1641 /* getmac.c */
1642 void tds_getmac(TDS_SYS_SOCKET s, unsigned char mac[6]);
1643 
1644 #ifndef HAVE_SSPI
1646 TDSAUTHENTICATION * tds_gss_get_auth(TDSSOCKET * tds);
1647 #else
1648 TDSAUTHENTICATION * tds_sspi_get_auth(TDSSOCKET * tds);
1649 #endif
1650 
1651 /* bulk.c */
1652 
1655 {
1656  TDS_BCP_IN = 1,
1657  TDS_BCP_OUT = 2,
1658  TDS_BCP_QUERYOUT = 3
1659 };
1660 
1661 typedef struct tds_bcpinfo
1662 {
1663  const char *hint;
1664  void *parent;
1665  TDS_CHAR *tablename;
1666  TDS_CHAR *insert_stmt;
1667  TDS_INT direction;
1668  TDS_INT identity_insert_on;
1669  TDS_INT xfer_init;
1670  TDS_INT var_cols;
1671  TDS_INT bind_count;
1672  TDSRESULTINFO *bindinfo;
1673 } TDSBCPINFO;
1674 
1675 int tds_bcp_init(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
1676 typedef int (*tds_bcp_get_col_data) (TDSBCPINFO *bulk, TDSCOLUMN *bcpcol, int offset);
1677 typedef void (*tds_bcp_null_error) (TDSBCPINFO *bulk, int index, int offset);
1678 int tds_bcp_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset);
1679 int tds_bcp_done(TDSSOCKET *tds, int *rows_copied);
1680 int tds_bcp_start(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
1681 int tds_bcp_start_copy_in(TDSSOCKET *tds, TDSBCPINFO *bcpinfo);
1682 
1683 int tds_writetext_start(TDSSOCKET *tds, const char *objname, const char *textptr, const char *timestamp, int with_log, TDS_UINT size);
1684 int tds_writetext_continue(TDSSOCKET *tds, const TDS_UCHAR *text, TDS_UINT size);
1685 int tds_writetext_end(TDSSOCKET *tds);
1686 
1687 
1688 #define IS_TDS42(x) (x->tds_version==0x402)
1689 #define IS_TDS46(x) (x->tds_version==0x406)
1690 #define IS_TDS50(x) (x->tds_version==0x500)
1691 #define IS_TDS70(x) (x->tds_version==0x700)
1692 #define IS_TDS71(x) (x->tds_version==0x701)
1693 #define IS_TDS72(x) (x->tds_version==0x702)
1694 
1695 #define IS_TDS7_PLUS(x) ((x)->tds_version>=0x700)
1696 #define IS_TDS71_PLUS(x) ((x)->tds_version>=0x701)
1697 #define IS_TDS72_PLUS(x) ((x)->tds_version>=0x702)
1698 
1699 #define TDS_MAJOR(x) ((x)->tds_version >> 8)
1700 #define TDS_MINOR(x) ((x)->tds_version & 0xff)
1701 
1702 #define IS_TDSDEAD(x) (((x) == NULL) || TDS_IS_SOCKET_INVALID((x)->s))
1703 
1705 #define TDS_IS_SYBASE(x) (!(x->product_version & 0x80000000u))
1706 
1707 #define TDS_IS_MSSQL(x) ((x->product_version & 0x80000000u)!=0)
1708 
1712 #define TDS_MS_VER(maj,min,x) (0x80000000u|((maj)<<24)|((min)<<16)|(x))
1713 
1714 /* TODO test if not similar to ms one*/
1716 #define TDS_SYB_VER(maj,min,x) (((maj)<<24)|((min)<<16)|(x)<<8)
1717 
1718 #ifdef __cplusplus
1719 #if 0
1720 {
1721 #endif
1722 }
1723 #endif
1724 
1725 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
1726 #pragma GCC visibility pop
1727 #endif
1728 
1729 #define TDS_PUT_INT(tds,v) tds_put_int((tds), ((TDS_INT)(v)))
1730 #define TDS_PUT_SMALLINT(tds,v) tds_put_smallint((tds), ((TDS_SMALLINT)(v)))
1731 #define TDS_PUT_BYTE(tds,v) tds_put_byte((tds), ((unsigned char)(v)))
1732 
1733 #endif /* _tds_h_ */
no connection
Definition: tds.h:1101
TDS_INT bcp_prefix_len
The length, in bytes, of any length prefix this column may have.
Definition: tds.h:1067
void tdsdump_dump_buf(const char *file, unsigned int level_line, const char *msg, const void *buf, size_t length)
Dump the contents of data into the log file in a human readable format.
Definition: log.c:280
unsigned char out_flag
output buffer type
Definition: tds.h:1351
int tds_read_conf_section(FILE *in, const char *section, TDSCONFPARSE tds_conf_parse, void *parse_param)
Read a section of configuration file (INI style file)
Definition: config.c:473
Information for a server connection.
Definition: tds.h:1329
int tds_get_char_data(TDSSOCKET *tds, char *dest, size_t wire_size, TDSCOLUMN *curcol)
Fetch character data the wire.
Definition: read.c:225
TDS_SMALLINT column_type
This type can be different from wire type because conversion (e.g.
Definition: tds.h:998
struct tds_encoding TDS_ENCODING
Information relevant to libiconv.
TDSCONNECTION * connection
config for login stuff.
Definition: tds.h:1384
final result set, command completed successfully.
Definition: tds.h:263
A structure to hold all the compile-time settings.
Definition: tds.h:64
Definition: tds.h:1290
Information about blobs (e.g.
Definition: tds.h:929
Used by tds_datecrack.
Definition: tds.h:176
int tds_count_placeholders(const char *query)
Count the number of placeholders in query.
Definition: query.c:496
TDS_INT ref_count
reference counter so client can retain safely a pointer
Definition: tds.h:1220
TDSCURSOR * cursors
linked list of cursors allocated for this connection
Definition: tds.h:1365
TDS_INT week
1 - 54 (can be 54 in leap year)
Definition: tds.h:183
unsigned char * out_buf
output buffer
Definition: tds.h:1344
TDS_INT type
row fetched from this cursor
Definition: tds.h:1236
TDS_TINYINT cursor_name_len
length of cursor name > 0 and <= 30
Definition: tds.h:1221
Current environment as reported by the server.
Definition: tds.h:1242
TDS_CURSOR_STATUS status
cursor parameter
Definition: tds.h:1233
error occurred
Definition: tds.h:265
struct tds_dynamic TDSDYNAMIC
Holds information for a dynamic (also called prepared) query.
TDS_INT millisecond
0-999
Definition: tds.h:188
Definition: tds.h:1318
Holds information for a dynamic (also called prepared) query.
Definition: tds.h:1253
TDS_INT8 rows_affected
rows updated/deleted/inserted/selected, TDS_NO_COUNT if not valid
Definition: tds.h:1373
int tds_submit_execute(TDSSOCKET *tds, TDSDYNAMIC *dyn)
tds_submit_execute() sends a previously prepared dynamic statement to the server. ...
Definition: query.c:1815
char * dbname(DBPROCESS *dbproc)
Get name of current database.
Definition: dblib.c:6818
DSTR ip_addr
ip of server
Definition: tds.h:904
const TDS_COMPILETIME_SETTINGS * tds_get_compiletime_settings(void)
Return a structure capturing the compile-time settings provided to the configure script.
Definition: config.c:1171
TDS_INT tds_datecrack(TDS_INT datetype, const void *di, TDSDATEREC *dr)
Convert from db date format to a structured date format.
Definition: convert.c:2925
void tds_free_param_result(TDSPARAMINFO *param_info)
Delete latest parameter.
Definition: mem.c:274
int tds_submit_query(TDSSOCKET *tds, const char *query)
tds_submit_query() sends a language string to the database server for processing. ...
Definition: query.c:189
Definition: tds.h:127
const char * tds_skip_quoted(const char *s)
Skip quoting string (like 'sfsf', "dflkdj" or [dfkjd])
Definition: query.c:441
volatile unsigned char in_cancel
indicate we are waiting a cancel reply; discard tokens till acknowledge
Definition: tds.h:1371
struct tds_blob TDSBLOB
Information about blobs (e.g.
TDS_OPTION_CMD
options that can be sent with a TDS_OPTIONCMD token
Definition: tds.h:594
struct tds_env TDSENV
Current environment as reported by the server.
DSTR server_name
server name (in freetds.conf)
Definition: tds.h:883
TDS_SMALLINT tds_get_smallint(TDSSOCKET *tds)
Get an int16 from the server.
Definition: read.c:106
int tds_alloc_row(TDSRESULTINFO *res_info)
Allocate space for row store return NULL on out of memory.
Definition: mem.c:468
TDS_INT quarter
quarter (0-3)
Definition: tds.h:179
Holds informations about a cursor.
Definition: tds.h:1217
TDSPARAMINFO * tds_alloc_param_result(TDSPARAMINFO *old_param)
Adds a output parameter to TDSPARAMINFO.
Definition: mem.c:234
void * tds_alloc_param_data(TDSCOLUMN *curparam)
Allocate data for a parameter.
Definition: mem.c:319
int tds_set_interfaces_file_loc(const char *interfloc)
Set the full name of interface file.
Definition: config.c:800
TDS_INT num_id
numeric id for mssql7+
Definition: tds.h:1264
void tdsdump_off(void)
Temporarily turn off logging.
Definition: log.c:93
TDS_SMALLINT column_namelen
length of column name
Definition: tds.h:1013
int tds8_submit_prepexec(TDSSOCKET *tds, const char *query, const char *id, TDSDYNAMIC **dyn_out, TDSPARAMINFO *params)
tds8_submit_prepexec() creates a temporary stored procedure in the server.
Definition: query.c:1292
int tds7_get_instance_port(const char *ip_addr, const char *instance)
Get port of given instance.
Definition: net.c:924
TDS_TINYINT hasargs
cursor parameters exists ?
Definition: tds.h:1225
int tds_put_string(TDSSOCKET *tds, const char *buf, int len)
Output a string to wire automatic translate string to unicode if needed.
Definition: write.c:98
TDS_SYS_SOCKET s
tcp socket, INVALID_SOCKET if not connected
Definition: tds.h:1331
int tds_get_conversion_type(int srctype, int colsize)
Return type suitable for conversions (convert all nullable types to fixed type)
Definition: tds_types.h:121
TDS_TINYINT column_varint_size
size of length when reading from wire (0, 1, 2 or 4)
Definition: tds.h:1008
client is reading data
Definition: tds.h:1100
int port
port of database service
Definition: tds.h:884
Hold information for any results.
Definition: tds.h:1074
count field in packet is valid
Definition: tds.h:268
TDS_INT day
day of month (1-31)
Definition: tds.h:181
int emulated
this dynamic query cannot be prepared so libTDS have to construct a simple query. ...
Definition: tds.h:1279
int tds_process_tokens(TDSSOCKET *tds, TDS_INT *result_type, int *done_flags, unsigned flag)
process all streams.
Definition: token.c:518
int tds_bcp_send_record(TDSSOCKET *tds, TDSBCPINFO *bcpinfo, tds_bcp_get_col_data get_col_data, tds_bcp_null_error null_error, int offset)
Definition: bulk.c:412
TDSRESULTINFO * current_results
Current query information.
Definition: tds.h:1359
struct tds_variant TDSVARIANT
Store variant informations.
int tds_send_cancel(TDSSOCKET *tds)
tds_send_cancel() sends an empty packet (8 byte header only) tds_process_cancel should be called dire...
Definition: query.c:2155
Definition: tds.h:918
enum _TDS_STATE TDS_STATE
values for tds->state
TDS_INT cursor_id
cursor id returned by the server after cursor declare
Definition: tds.h:1223
struct tds_cursor TDSCURSOR
Holds informations about a cursor.
int tds_get_token_size(int marker)
tds_get_token_size() returns the size of a fixed length token used by tds_process_cancel() to determi...
Definition: token.c:2919
unsigned in_pos
current position in in_buf
Definition: tds.h:1346
TDS_INT year
year
Definition: tds.h:178
int tds_submit_prepare(TDSSOCKET *tds, const char *query, const char *id, TDSDYNAMIC **dyn_out, TDSPARAMINFO *params)
tds_submit_prepare() creates a temporary stored procedure in the server.
Definition: query.c:1010
void tdsdump_on(void)
Turn logging back on.
Definition: log.c:105
int tds_flush_packet(TDSSOCKET *tds)
Flush packet to server.
Definition: write.c:295
char * query
saved query, we need to know original query if prepare is impossible
Definition: tds.h:1281
struct tdsdaterec TDSDATEREC
Used by tds_datecrack.
TDSLOCALE * tds_get_locale(void)
Get locale information.
Definition: locale.c:58
unsigned out_pos
current position in out_buf
Definition: tds.h:1347
Definition: tds.h:161
Definition: tds.h:1661
TDS_TINYINT options
read only|updatable
Definition: tds.h:1224
int tds_read_conf_file(TDSCONNECTION *connection, const char *server)
Read configuration info for given server return 0 on error.
Definition: config.c:338
TDS_USMALLINT query_len
SQL query length.
Definition: tds.h:1226
void tdsdump_close(void)
Close the TDS dump log file.
Definition: log.c:221
Store variant informations.
Definition: tds.h:939
TDSICONV * tds_iconv_from_collate(TDSSOCKET *tds, TDS_UCHAR collate[5])
Get iconv information from a LCID (to support different column encoding under MSSQL2K) ...
Definition: iconv.c:1514
unsigned in_len
input buffer length
Definition: tds.h:1348
TDS_INT ret_status
return status from store procedure
Definition: tds.h:1367
char * query
SQL query.
Definition: tds.h:1227
void tdsdump_log(const char *file, unsigned int level_line, const char *fmt,...)
Write a message to the debug log.
Definition: log.c:377
TDS_INT cursor_rows
< number of updatable columns
Definition: tds.h:1231
TDSDYNAMIC * tds_lookup_dynamic(TDSSOCKET *tds, const char *id)
Find a dynamic given string id.
Definition: token.c:2751
void tdsdump_col(const TDSCOLUMN *col)
Write a column value to the debug log.
Definition: log.c:432
Definition: tds.h:1300
Definition: tds.h:639
int tds7_get_instance_ports(FILE *output, const char *ip_addr)
Get port of all instances.
Definition: net.c:784
Definition: tds.h:79
TDS_INT tds_get_int(TDSSOCKET *tds)
Get an int32 from the server.
Definition: read.c:123
TDS_INT dayofyear
day of year (1-366)
Definition: tds.h:182
Definition: tds.h:150
void * tds_get_n(TDSSOCKET *tds, void *dest, int n)
Get N bytes from the buffer and return them in the already allocated space given to us...
Definition: read.c:286
int tds_get_string(TDSSOCKET *tds, int string_len, char *dest, size_t dest_size)
Fetch a string from the wire.
Definition: read.c:182
int tds_process_cancel(TDSSOCKET *tds)
Definition: token.c:2718
TDS_STATE tds_set_state(TDSSOCKET *tds, TDS_STATE state)
Set state of TDS connection, with logging and checking.
Definition: util.c:84
unsigned char in_flag
input buffer type
Definition: tds.h:1350
void tds_unget_byte(TDSSOCKET *tds)
Unget will always work as long as you don't call it twice in a row.
Definition: read.c:86
no data expected
Definition: tds.h:1097
unsigned char * in_buf
input buffer
Definition: tds.h:1343
TDS_INT hour
0-23
Definition: tds.h:185
Definition: tds.h:120
int tds_lookup_host(const char *servername, char *ip)
Get the IP address for a hostname.
Definition: config.c:826
int tds_submit_unprepare(TDSSOCKET *tds, TDSDYNAMIC *dyn)
Send a unprepare request for a prepared query.
Definition: query.c:1929
Definition: tds.h:133
void tds_free_dynamic(TDSSOCKET *tds, TDSDYNAMIC *dyn)
Frees dynamic statement and remove from TDS.
Definition: mem.c:196
TDS_USMALLINT tds_config_verstr(const char *tdsver, TDSCONNECTION *connection)
Set TDS version from given string.
Definition: config.c:766
TDSPARAMINFO * res_info
query results
Definition: tds.h:1265
TDSICONV * char_conv
refers to previously allocated iconv information
Definition: tds.h:1021
TDS_TINYINT column_scale
scale for decimal/numeric
Definition: tds.h:1011
TDS_TINYINT has_status
true is ret_status is valid
Definition: tds.h:1366
acknowledging an attention command (usually a cancel)
Definition: tds.h:269
int tdsdump_open(const char *filename)
Create and truncate a human readable dump file for the TDS traffic.
Definition: log.c:128
TDSPARAMINFO * params
query parameters.
Definition: tds.h:1273
Definition: tds.h:156
DSTR password
password of account login
Definition: tds.h:895
unsigned char * tds7_crypt_pass(const unsigned char *clear_pass, size_t len, unsigned char *crypt_pass)
tds7_crypt_pass() – 'encrypt' TDS 7.0 style passwords.
Definition: login.c:932
int tdserror(const TDSCONTEXT *tds_ctx, TDSSOCKET *tds, int msgno, int errnum)
Call the client library's error handler (for library-generated errors only)
Definition: util.c:319
Definition: tds.h:1172
TDS_TINYINT column_prec
precision for decimal/numeric
Definition: tds.h:1010
TDS_INT column_size
maximun size of data.
Definition: tds.h:1006
DSTR user_name
account for login
Definition: tds.h:894
int tds_cursor_dealloc(TDSSOCKET *tds, TDSCURSOR *cursor)
Send a deallocation request to server libTDS care for all deallocation stuff (memory and server curso...
Definition: query.c:3016
cilent is waiting for data
Definition: tds.h:1099
int tds_version(TDSSOCKET *tds_socket, char *pversion_string)
Returns the version of the TDS protocol in effect for the link as a decimal integer.
Definition: util.c:186
TDS_INT weekday
day of week (0-6, 0 = sunday)
Definition: tds.h:184
Definition: tds.h:846
struct tds_result_info TDSRESULTINFO
Hold information for any results.
client is sending request
Definition: tds.h:1098
int tds_process_simple_query(TDSSOCKET *tds)
Process results for simple query as "SET TEXTSIZE" or "USE dbname" If the statement returns results...
Definition: token.c:840
int tds_quote_id(TDSSOCKET *tds, char *buffer, const char *id, int idlen)
Quote an id.
Definition: query.c:2215
char * tds_timestamp_str(char *str, int maxlen)
Definition: threadsafe.c:114
tds_end
Flags returned in TDS_DONE token.
Definition: tds.h:261
TDS_INT second
0-59
Definition: tds.h:187
TDSDYNAMIC * tds_alloc_dynamic(TDSSOCKET *tds, const char *id)
Allocate a dynamic statement.
Definition: mem.c:136
void tds_fix_connection(TDSCONNECTION *connection)
Fix configuration after reading it.
Definition: config.c:277
char * cursor_name
name of the cursor
Definition: tds.h:1222
int tds_get_varint_size(TDSSOCKET *tds, int datatype)
tds_get_varint_size() returns the size of a variable length integer returned in a TDS 7...
Definition: tds_types.h:54
Definition: tds.h:139
int tds_process_login_tokens(TDSSOCKET *tds)
tds_process_login_tokens() is called after sending the login packet to the server.
Definition: token.c:317
Definition: tds.h:1156
void tds_set_column_type(TDSSOCKET *tds, TDSCOLUMN *curcol, int type)
Set type of column initializing all dependency.
Definition: data.c:46
Definition: tdsiconv.h:100
Definition: tds.h:982
transaction in progress
Definition: tds.h:266
void tds_set_server(TDSLOGIN *tds_login, const char *server)
Set the servername in a TDSLOGIN structure.
Definition: login.c:122
DSTR server_realm_name
server realm name (in freetds.conf)
Definition: tds.h:892
struct tds_column TDSCOLUMN
Metadata about columns in regular and compute rows.
int tds_quote_string(TDSSOCKET *tds, char *buffer, const char *str, int len)
Quote a string.
Definition: query.c:2260
int tds_submit_query_params(TDSSOCKET *tds, const char *query, TDSPARAMINFO *params)
tds_submit_query_params() sends a language string to the database server for processing.
Definition: query.c:277
int tds_submit_execdirect(TDSSOCKET *tds, const char *query, TDSPARAMINFO *params)
Submit a prepared query with parameters.
Definition: query.c:1153
TDSDYNAMIC * cur_dyn
dynamic structure in use
Definition: tds.h:1377
int tds_get_size_by_type(int servertype)
Return the number of bytes needed by specified type.
Definition: tds_types.h:10
Definition: tds.h:880
TDSDYNAMIC * dyns
list of dynamic allocate for this connection
Definition: tds.h:1378
void tds_free_input_params(TDSDYNAMIC *dyn)
Frees all allocated input parameters of a dynamic statement.
Definition: mem.c:178
Metadata about columns in regular and compute rows.
Definition: tds.h:996
TDS_UINT product_version
version of product (Sybase/MS and full version)
Definition: tds.h:1334
const char * tds_next_placeholder(const char *start)
Get position of next placeholder.
Definition: query.c:461
more results follow
Definition: tds.h:264
_TDS_STATE
values for tds->state
Definition: tds.h:1095
int tds_submit_rpc(TDSSOCKET *tds, const char *rpc_name, TDSPARAMINFO *params)
tds_submit_rpc() call a RPC from server.
Definition: query.c:2054
Definition: tds.h:144
struct tds_cursor * next
next in linked list, keep first
Definition: tds.h:1219
TDS_INT month
month number (0-11)
Definition: tds.h:180
void tds_set_param_type(TDSSOCKET *tds, TDSCOLUMN *curcol, TDS_SERVER_TYPE type)
Set type of column initializing all dependency.
Definition: data.c:67
TDSCURSOR * cur_cursor
cursor in use
Definition: tds.h:1364
TDSAUTHENTICATION * tds_ntlm_get_auth(TDSSOCKET *tds)
Build a NTLMSPP packet to send to server.
Definition: challenge.c:736
TDS_INT minute
0-59
Definition: tds.h:186
unsigned int in_buf_max
allocated input buffer
Definition: tds.h:1345
int tds_read_packet(TDSSOCKET *tds)
Read in one 'packet' from the server.
Definition: net.c:517
struct tds_compiletime_settings TDS_COMPILETIME_SETTINGS
A structure to hold all the compile-time settings.
SQL server server error.
Definition: tds.h:271
Definition: tds.h:1186
int tds_iconv_alloc(TDSSOCKET *tds)
Allocate iconv stuff.
Definition: iconv.c:269
char * tds_get_homedir(void)
Get user home directory.
Definition: threadsafe.c:523
const int tds_numeric_bytes_per_prec[]
The following little table is indexed by precision and will tell us the number of bytes required to s...
Definition: numeric.c:60
TDSCONNECTION * tds_alloc_connection(TDSLOCALE *locale)
Allocate space for configure structure and initialize with default values.
Definition: mem.c:805
TDS 8.0 collation informations.
Definition: tds.h:952
Information relevant to libiconv.
Definition: tds.h:974
TDS_INT column_cur_size
size written in variable (ie: char, text, binary).
Definition: tds.h:1044
tds_bcp_directions
bcp direction
Definition: tds.h:1654
TDSCONNECTION * tds_read_config_info(TDSSOCKET *tds, TDSLOGIN *login, TDSLOCALE *locale)
tds_read_config_info() will fill the tds connection structure based on configuration information gath...
Definition: config.c:157
DSTR server_charset
charset of server
Definition: tds.h:888
struct tds_dynamic * next
next in linked list, keep first
Definition: tds.h:1255
results are from a stored procedure
Definition: tds.h:267
Definition: tds.h:167
Definition: tds.h:855