-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathdblib.c
8327 lines (7410 loc) · 250 KB
/
dblib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* FreeTDS - Library of routines accessing Sybase and Microsoft databases
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Brian Bruns
* Copyright (C) 2006-2015 Frediano Ziglio
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <stdarg.h>
#include <freetds/time.h>
#include <assert.h>
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if HAVE_ERRNO_H
# include <errno.h>
#endif /* HAVE_ERRNO_H */
/**
* \ingroup dblib_core
* \remarks Either SYBDBLIB or MSDBLIB (not both) must be defined.
* This affects how certain application-addressable
* strucures are defined.
*/
#include <freetds/tds.h>
#include <freetds/thread.h>
#include <freetds/convert.h>
#include <freetds/utils/string.h>
#include <freetds/data.h>
#include <freetds/replacements.h>
#include <sybfront.h>
#include <sybdb.h>
#include <syberror.h>
#include <dblib.h>
static RETCODE _dbresults(DBPROCESS * dbproc);
static BYTE *_dbcoldata(TDSCOLUMN *colinfo);
static int _get_printable_size(TDSCOLUMN * colinfo);
static char *_dbprdate(char *timestr);
static int _dbnullable(DBPROCESS * dbproc, int column);
static const char *tds_prdatatype(int datatype_token);
static int default_err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr);
void copy_data_to_host_var(DBPROCESS *, TDS_SERVER_TYPE, const BYTE *, int, BYTE *, DBINT, int, DBINT *);
RETCODE dbgetnull(DBPROCESS *dbproc, int bindtype, int varlen, BYTE* varaddr);
/**
* \file dblib.c
* Main implementation file for \c db-lib.
*/
/**
* \file bcp.c
* Implementation of \c db-lib bulk copy functions.
*/
/**
* \defgroup dblib_api The db-lib API
* Functions callable by \c db-lib client programs
*
* The \c db_lib interface is implemented by both Sybase and Microsoft. FreeTDS seeks to implement
* first the intersection of the functions defined by the vendors.
*/
/**
* \ingroup dblib_api
* \defgroup dblib_core Primary functions
* Core functions needed by most db-lib programs.
*/
/**
* \ingroup dblib_api
* \defgroup dblib_rpc Remote Procedure functions
* Functions used with stored procedures.
* Especially useful for OUTPUT parameters, because modern Microsoft servers do not
* return output parameter data to the client unless the procedure was invoked
* with dbrpcsend().
*/
/**
* \ingroup dblib_api
* \defgroup dblib_bcp Bulk copy functions
* Functions to bulk-copy (a/k/a \em bcp) data to/from the database.
*/
/**
* \ingroup dblib_bcp
* \defgroup dblib_bcp_internal Internal bcp functions
* Static functions internal to the bcp library.
*/
/**
* \ingroup dblib_api
* \defgroup dblib_money Money functions
* Functions to manipulate the MONEY datatype.
*/
/**
* \ingroup dblib_api
* \defgroup dblib_datetime Datetime functions
* Functions to manipulate DBDATETIME structures. Defined by Sybase only.
* These are not implemented:
* - dbdate4cmp()
* - dbdate4zero()
* - dbdatechar()
* - dbdatename()
* - dbdateorder()
* - dbdatepart()
* - dbdatezero()
* - dbdayname()
*/
/**
* \ingroup dblib_api
* \defgroup dblib_internal Internals
* Functions called within \c db-lib for self-help.
* These functions are of interest only to people hacking on the FreeTDS db-lib implementation.
*/
/**
* \ingroup dblib_api
* \defgroup dblib_unimplemented Unimplemented
* Functions thus far not implemented in the FreeTDS db-lib implementation.
* While some of these are simply awaiting someone with time and skill (and inclination)
* it might be noted here that the old browse functions (e.g. dbcolbrowse())
* are on the never-to-do list.
* They were defined by Sybase and were superseded long ago, although they're still
* present in Microsoft's implementation.
* They were never popular and today better alternatives are available.
* For completeness, they are:
* - dbcolbrowse()
* - dbcolsource()
* - dbfreequal()
* - dbqual()
* - dbtabbrowse()
* - dbtabcount()
* - dbtabname()
* - dbtabsource()
* - dbtsnewlen()
* - dbtsnewval()
* - dbtsput()
*/
/* info/err message handler functions (or rather pointers to them) */
MHANDLEFUNC _dblib_msg_handler = NULL;
EHANDLEFUNC _dblib_err_handler = default_err_handler;
/** \internal
* \dblib_internal
* \remarks A db-lib connection has an implicit TDS context.
*/
typedef struct dblib_context
{
/** reference count, time dbinit called */
int ref_count;
/** libTDS context */
TDSCONTEXT *tds_ctx;
/** libTDS context reference counter */
int tds_ctx_ref_count;
/* save all connection in a list */
TDSSOCKET **connection_list;
int connection_list_size;
int connection_list_size_represented;
char *recftos_filename;
int recftos_filenum;
int login_timeout; /**< not used unless positive */
int query_timeout; /**< not used unless positive */
}
DBLIBCONTEXT;
static DBLIBCONTEXT g_dblib_ctx;
static tds_mutex dblib_mutex = TDS_MUTEX_INITIALIZER;
static int g_dblib_version =
#if TDS50
DBVERSION_100;
#elif TDS71
DBVERSION_71;
#elif TDS72
DBVERSION_72;
#elif TDS73
DBVERSION_73;
#elif TDS74
DBVERSION_74;
#else
DBVERSION_UNKNOWN;
#endif
static int
dblib_add_connection(DBLIBCONTEXT * ctx, TDSSOCKET * tds)
{
int i = 0;
const int list_size = ctx->connection_list_size_represented;
tdsdump_log(TDS_DBG_FUNC, "dblib_add_connection(%p, %p)\n", ctx, tds);
while (i < list_size && ctx->connection_list[i])
i++;
if (i == list_size) {
return 1;
} else {
ctx->connection_list[i] = tds;
return 0;
}
}
static void
dblib_del_connection(DBLIBCONTEXT * ctx, TDSSOCKET * tds)
{
int i = 0;
const int list_size = ctx->connection_list_size;
tdsdump_log(TDS_DBG_FUNC, "dblib_del_connection(%p, %p)\n", ctx, tds);
while (i < list_size && ctx->connection_list[i] != tds)
i++;
if (i == list_size) {
/* connection wasn't on the free list...now what */
} else {
/* remove it */
ctx->connection_list[i] = NULL;
}
}
static TDSCONTEXT*
dblib_get_tds_ctx(void)
{
tdsdump_log(TDS_DBG_FUNC, "dblib_get_tds_ctx(void)\n");
tds_mutex_lock(&dblib_mutex);
++g_dblib_ctx.tds_ctx_ref_count;
if (g_dblib_ctx.tds_ctx == NULL) {
g_dblib_ctx.tds_ctx = tds_alloc_context(&g_dblib_ctx);
/*
* Set the functions in the TDS layer to point to the correct handler functions
*/
g_dblib_ctx.tds_ctx->msg_handler = _dblib_handle_info_message;
g_dblib_ctx.tds_ctx->err_handler = _dblib_handle_err_message;
g_dblib_ctx.tds_ctx->int_handler = _dblib_check_and_handle_interrupt;
if (g_dblib_ctx.tds_ctx->locale && !g_dblib_ctx.tds_ctx->locale->datetime_fmt) {
/* set default in case there's no locale file */
static const char datetime_format[] = "%b %e %Y %l:%M:%S:%z%p";
g_dblib_ctx.tds_ctx->locale->datetime_fmt = strdup(datetime_format);
}
}
tds_mutex_unlock(&dblib_mutex);
return g_dblib_ctx.tds_ctx;
}
static void
dblib_release_tds_ctx(int count)
{
tdsdump_log(TDS_DBG_FUNC, "dblib_release_tds_ctx(%d)\n", count);
tds_mutex_lock(&dblib_mutex);
g_dblib_ctx.tds_ctx_ref_count -= count;
if (g_dblib_ctx.tds_ctx_ref_count <= 0) {
tds_free_context(g_dblib_ctx.tds_ctx);
g_dblib_ctx.tds_ctx = NULL;
}
tds_mutex_unlock(&dblib_mutex);
}
#include "buffering.h"
static void
db_env_chg(TDSSOCKET * tds, int type, char *oldval, char *newval)
{
DBPROCESS *dbproc;
assert(oldval != NULL && newval != NULL);
if (strlen(oldval) == 1 && *oldval == 1)
oldval = "(0x1)";
tdsdump_log(TDS_DBG_FUNC, "db_env_chg(%p, %d, %s, %s)\n", tds, type, oldval, newval);
if (!tds || !tds_get_parent(tds))
return;
dbproc = (DBPROCESS *) tds_get_parent(tds);
dbproc->envchange_rcv |= (1 << (type - 1));
switch (type) {
case TDS_ENV_DATABASE:
strlcpy(dbproc->dbcurdb, newval, sizeof(dbproc->dbcurdb));
break;
case TDS_ENV_CHARSET:
strlcpy(dbproc->servcharset, newval, sizeof(dbproc->servcharset));
break;
default:
break;
}
return;
}
/** \internal
* \ingroup dblib_internal
* \brief Sanity checks for column-oriented functions.
*
* \param dbproc contains all information needed by db-lib to manage communications with the server.
* \param pcolinfo address of pointer to a TDSCOLUMN structure.
* \remarks Makes sure dbproc and the requested column are valid.
* Calls dbperror() if not.
* \returns appropriate error or SUCCEED
*/
static TDSCOLUMN*
dbcolptr(DBPROCESS* dbproc, int column)
{
TDSSOCKET *tds;
TDSRESULTINFO *info;
if (!dbproc) {
dbperror(dbproc, SYBENULL, 0);
return NULL;
}
tds = dbproc->tds_socket;
if (IS_TDSDEAD(tds)) {
dbperror(dbproc, SYBEDDNE, 0);
return NULL;
}
info = tds->res_info;
if (!info)
return NULL;
if (column < 1 || column > info->num_cols) {
dbperror(dbproc, SYBECNOR, 0);
return NULL;
}
return info->columns[column - 1];
}
static TDSCOLUMN*
dbacolptr(DBPROCESS* dbproc, int computeid, int column, bool is_bind)
{
unsigned int i;
TDSSOCKET *tds;
TDSCOMPUTEINFO *info;
if (!dbproc) {
dbperror(dbproc, SYBENULL, 0);
return NULL;
}
tds = dbproc->tds_socket;
if (IS_TDSDEAD(tds)) {
dbperror(dbproc, SYBEDDNE, 0);
return NULL;
}
for (i = 0;; ++i) {
if (i >= tds->num_comp_info) {
/* Attempt to bind user variable to a non-existent compute row */
if (is_bind)
dbperror(dbproc, SYBEBNCR, 0);
return NULL;
}
info = tds->comp_info[i];
if (info->computeid == computeid)
break;
}
/* Fail if either the compute id or the column number is invalid. */
if (column < 1 || column > info->num_cols) {
dbperror(dbproc, is_bind ? SYBEABNC : SYBECNOR, 0);
return NULL;
}
return info->columns[column - 1];
}
/*
* Default null substitution values
* Binding Type Null Substitution Value
* TINYBIND 0
* SMALLBIND 0
* INTBIND 0
* CHARBIND Empty string (padded with blanks)
* STRINGBIND Empty string (padded with blanks, null-terminated)
* NTBSTRINGBIND Empty string (null-terminated)
* VARYCHARBIND Empty string
* BINARYBIND Empty array (padded with zeros)
* VARYBINBIND Empty array
* DATETIMEBIND 8 bytes of zeros
* SMALLDATETIMEBIND 8 bytes of zeros
* MONEYBIND $0.00
* SMALLMONEYBIND $0.00
* FLT8BIND 0.0
* REALBIND 0.0
* DECIMALBIND 0.0 (with default scale and precision)
* NUMERICBIND 0.0 (with default scale and precision)
* BOUNDARYBIND Empty string (null-terminated)
* SENSITIVITYBIND Empty string (null-terminated)
*/
static const DBBIT null_BIT = 0;
static const DBTINYINT null_TINYINT = 0;
static const DBSMALLINT null_SMALLINT = 0;
static const DBINT null_INT = 0;
static const DBBIGINT null_BIGINT = 0;
static const DBFLT8 null_FLT8 = 0;
static const DBREAL null_REAL = 0;
static const DBCHAR null_CHAR = '\0';
static const DBVARYCHAR null_VARYCHAR = { 0, {0} };
static const DBDATETIME null_DATETIME = { 0, 0 };
static const DBDATETIME4 null_SMALLDATETIME = { 0, 0 };
static const DBMONEY null_MONEY = { 0, 0 };
static const DBMONEY4 null_SMALLMONEY = {0};
static const DBNUMERIC null_NUMERIC = { 0, 0, {0} };
static const TDS_DATETIMEALL null_DATETIMEALL = { 0, 0, 0, 0 };
static NULLREP default_null_representations[MAXBINDTYPES] = {
/* CHARBIND 0 */ { NULL, 0 }
/* STRINGBIND 1 */ , { NULL, 0 }
/* NTBSTRINGBIND 2 */ , { (BYTE*) &null_CHAR, sizeof(null_CHAR) }
/* VARYCHARBIND 3 */ , { (BYTE*) &null_VARYCHAR, sizeof(null_VARYCHAR) }
/* VARYBINBIND 4 */ , { (BYTE*) &null_VARYCHAR, sizeof(null_VARYCHAR) }
/* no such bind 5 */ , { NULL, 0 }
/* TINYBIND 6 */ , { &null_TINYINT, sizeof(null_TINYINT) }
/* SMALLBIND 7 */ , { (BYTE*) &null_SMALLINT, sizeof(null_SMALLINT) }
/* INTBIND 8 */ , { (BYTE*) &null_INT, sizeof(null_INT) }
/* FLT8BIND 9 */ , { (BYTE*) &null_FLT8, sizeof(null_FLT8) }
/* REALBIND 10 */ , { (BYTE*) &null_REAL, sizeof(null_REAL) }
/* DATETIMEBIND 11 */ , { (BYTE*) &null_DATETIME, sizeof(null_DATETIME) }
/* SMALLDATETIMEBIND 12 */ , { (BYTE*) &null_SMALLDATETIME, sizeof(null_SMALLDATETIME) }
/* MONEYBIND 13 */ , { (BYTE*) &null_MONEY, sizeof(null_MONEY) }
/* SMALLMONEYBIND 14 */ , { (BYTE*) &null_SMALLMONEY, sizeof(null_SMALLMONEY) }
/* BINARYBIND 15 */ , { NULL, 0 }
/* BITBIND 16 */ , { &null_BIT, sizeof(null_BIT) }
/* NUMERICBIND 17 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
/* DECIMALBIND 18 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
/* SRCNUMERICBIND 19 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
/* SRCDECIMALBIND 20 */ , { (BYTE*) &null_NUMERIC, sizeof(null_NUMERIC) }
/* DATEBIND 21 */ , { (BYTE*) &null_INT, sizeof(null_INT) }
/* TIMEBIND 22 */ , { (BYTE*) &null_INT, sizeof(null_INT) }
/* BIGDATETIMEBIND 23 */ , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
/* BIGTIMEBIND 24 */ , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
/* 25 */ , { NULL, 0 }
/* 26 */ , { NULL, 0 }
/* 27 */ , { NULL, 0 }
/* 28 */ , { NULL, 0 }
/* 29 */ , { NULL, 0 }
/* BIGINTBIND 30 */ , { (BYTE*) &null_BIGINT, sizeof(null_BIGINT) }
/* DATETIME2BIND 31 */ , { (BYTE*) &null_DATETIMEALL, sizeof(null_DATETIMEALL) }
/* MAXBINDTYPES 32 */
};
static int
dbbindtype(int datatype)
{
switch (datatype) {
case SYBIMAGE:
case SYBVARBINARY:
case SYBBINARY: return BINARYBIND;
case SYBBIT: return BITBIND;
case SYBLONGCHAR:
case SYBTEXT:
case SYBVARCHAR:
case SYBCHAR: return NTBSTRINGBIND;
case SYBDATETIME: return DATETIMEBIND;
case SYBDATETIME4: return SMALLDATETIMEBIND;
case SYBDATE: return DATEBIND;
case SYBTIME: return TIMEBIND;
case SYB5BIGDATETIME: return BIGDATETIMEBIND;
case SYB5BIGTIME: return BIGTIMEBIND;
case SYBDECIMAL: return DECIMALBIND;
case SYBNUMERIC: return NUMERICBIND;
case SYBFLT8: return FLT8BIND;
case SYBREAL: return REALBIND;
case SYBINT1: return TINYBIND;
case SYBINT2: return SMALLBIND;
case SYBINT4: return INTBIND;
case SYBINT8: return BIGINTBIND;
case SYBMONEY: return MONEYBIND;
case SYBMONEY4: return SMALLMONEYBIND;
case SYBMSDATE:
case SYBMSTIME:
case SYBMSDATETIME2:
case SYBMSDATETIMEOFFSET:
return DATETIME2BIND;
default:
assert(0 == "no such datatype");
}
return 0;
}
/** \internal
* dbbind() says: "Note that if varlen is 0, no padding takes place"
* dbgetnull() will not pad varaddr unless varlen is positive.
* Vartype Program Type Padding Terminator
* ------------------- -------------- -------------- ----------
* CHARBIND DBCHAR blanks none
* STRINGBIND DBCHAR blanks \0
* NTBSTRINGBIND DBCHAR none \0
* VARYCHARBIND DBVARYCHAR none none
* BOUNDARYBIND DBCHAR none \0
* SENSITIVITYBIND DBCHAR none \0
*/
RETCODE
dbgetnull(DBPROCESS *dbproc, int bindtype, int varlen, BYTE* varaddr)
{
NULLREP *pnullrep = default_null_representations + bindtype;
tdsdump_log(TDS_DBG_FUNC, "dbgetnull(%p, %d, %d, %p)\n", dbproc, bindtype, varlen, varaddr);
CHECK_PARAMETER(varaddr, SYBENULL, FAIL);
CHECK_PARAMETER(0 <= bindtype && bindtype < MAXBINDTYPES, SYBEBTYP, FAIL);
/* dbproc can be NULL */
if (NULL != dbproc) {
assert(dbproc->nullreps);
pnullrep = dbproc->nullreps + bindtype;
}
/*
* Fixed types: ignore varlen
* Other types: ignore varlen if <= 0, else varlen must be >= pnullrep->len.
*/
switch (bindtype) {
case DATETIMEBIND:
case DATETIME2BIND:
case DECIMALBIND:
case SRCDECIMALBIND:
case FLT8BIND:
case INTBIND:
case MONEYBIND:
case NUMERICBIND:
case SRCNUMERICBIND:
case REALBIND:
case SMALLBIND:
case SMALLDATETIMEBIND:
case SMALLMONEYBIND:
case TINYBIND:
case BIGINTBIND:
case BITBIND:
case TIMEBIND:
case DATEBIND:
case BIGDATETIMEBIND:
case BIGTIMEBIND:
memcpy(varaddr, pnullrep->bindval, pnullrep->len);
return SUCCEED;
case CHARBIND:
case STRINGBIND:
case NTBSTRINGBIND:
case BINARYBIND:
case VARYCHARBIND:
case VARYBINBIND:
if (pnullrep->bindval && (varlen <= 0 || (size_t)varlen >= pnullrep->len)) {
memcpy(varaddr, pnullrep->bindval, pnullrep->len);
}
break;
default:
dbperror(dbproc, SYBEBTYP, 0);
return FAIL;
}
/*
* For variable-length types, nonpositive varlen indicates
* buffer is "big enough" but also not to pad.
* Apply terminator (if applicable) and go home.
*/
if (varlen <= 0) {
varlen = pnullrep->len;
switch (bindtype) {
case STRINGBIND:
case NTBSTRINGBIND:
++varlen;
break;
#if 0
case BOUNDARYBIND:
case SENSITIVITYBIND:
#endif
}
}
if (varlen < (long)pnullrep->len) {
tdsdump_log(TDS_DBG_FUNC, "dbgetnull: error: not setting varaddr(%p) because %d < %lu\n",
varaddr, varlen, (unsigned long int) pnullrep->len);
return FAIL;
}
tdsdump_log(TDS_DBG_FUNC, "varaddr(%p) varlen %d < %lu?\n",
varaddr, varlen, (unsigned long int) pnullrep->len);
assert(varlen >= 0);
/*
* CHARBIND Empty string (padded with blanks)
* STRINGBIND Empty string (padded with blanks, null-terminated)
* NTBSTRINGBIND Empty string (unpadded, null-terminated)
* BINARYBIND Empty array (padded with zeros)
*/
varaddr += pnullrep->len;
varlen -= (int)pnullrep->len;
if (varlen > 0) {
switch (bindtype) {
case CHARBIND:
memset(varaddr, ' ', varlen);
break;
case STRINGBIND:
memset(varaddr, ' ', varlen);
varaddr[varlen-1] = '\0';
break;
case NTBSTRINGBIND:
varaddr[0] = '\0';
break;
case BINARYBIND:
memset(varaddr, 0, varlen);
break;
case VARYCHARBIND:
case VARYBINBIND:
break;
default:
assert(!"unknown bindtype");
}
}
return SUCCEED;
}
/**
* \ingroup dblib_core
* \brief Initialize db-lib.
*
* \remarks Call this function before trying to use db-lib in any way.
* Allocates various internal structures and reads \c locales.conf (if any) to determine the default
* date format.
* \retval SUCCEED normal.
* \retval FAIL cannot allocate an array of \c TDS_MAX_CONN \c TDSSOCKET pointers.
*/
RETCODE
dbinit(void)
{
_dblib_err_handler = default_err_handler;
tds_mutex_lock(&dblib_mutex);
tdsdump_log(TDS_DBG_FUNC, "dbinit(void)\n");
if (++g_dblib_ctx.ref_count != 1) {
tds_mutex_unlock(&dblib_mutex);
return SUCCEED;
}
/*
* DBLIBCONTEXT stores a list of current connections so they may be closed with dbexit()
*/
g_dblib_ctx.connection_list = tds_new0(TDSSOCKET *, TDS_MAX_CONN);
if (g_dblib_ctx.connection_list == NULL) {
tdsdump_log(TDS_DBG_FUNC, "dbinit: out of memory\n");
tds_mutex_unlock(&dblib_mutex);
return FAIL;
}
g_dblib_ctx.connection_list_size = TDS_MAX_CONN;
g_dblib_ctx.connection_list_size_represented = TDS_MAX_CONN;
g_dblib_ctx.login_timeout = -1;
g_dblib_ctx.query_timeout = -1;
tds_mutex_unlock(&dblib_mutex);
dblib_get_tds_ctx();
return SUCCEED;
}
/**
* \ingroup dblib_core
* \brief Allocate a \c LOGINREC structure.
*
* \remarks A \c LOGINREC structure is passed to \c dbopen() to create a connection to the database.
* Does not communicate to the server; interacts strictly with library.
* \retval NULL the \c LOGINREC cannot be allocated.
* \retval LOGINREC* to valid memory, otherwise.
*/
LOGINREC *
dblogin(void)
{
LOGINREC *loginrec;
tdsdump_log(TDS_DBG_FUNC, "dblogin(void)\n");
if ((loginrec = tds_new0(LOGINREC, 1)) == NULL) {
dbperror(NULL, SYBEMEM, errno);
return NULL;
}
if ((loginrec->tds_login = tds_alloc_login(true)) == NULL) {
dbperror(NULL, SYBEMEM, errno);
free(loginrec);
return NULL;
}
/* set default values for loginrec */
if (!tds_set_library(loginrec->tds_login, "DB-Library")) {
dbperror(NULL, SYBEMEM, errno);
free(loginrec);
return NULL;
}
return loginrec;
}
/**
* \ingroup dblib_core
* \brief free the \c LOGINREC
*
*/
void
dbloginfree(LOGINREC * login)
{
tdsdump_log(TDS_DBG_FUNC, "dbloginfree(%p)\n", login);
if (login) {
tds_free_login(login->tds_login);
TDS_ZERO_FREE(login);
}
}
/** \internal
* \ingroup dblib_internal
* \brief Set the value of a string in a \c LOGINREC structure.
*
* Called by various macros to populate \a login.
* \param login the \c LOGINREC* to modify.
* \param value the value to set it to.
* \param which the field to set.
* \retval SUCCEED the value was set.
* \retval FAIL \c DBSETHID or other invalid \a which was tried.
*/
RETCODE
dbsetlname(LOGINREC * login, const char *value, int which)
{
bool copy_ret;
const char *value_nonull = value ? value : "";
tdsdump_log(TDS_DBG_FUNC, "dbsetlname(%p, %s, %d)\n", login, value, which);
if (login == NULL) {
dbperror(NULL, SYBEASNL, 0);
return FAIL;
}
if (TDS_MAX_LOGIN_STR_SZ < strlen(value_nonull)) {
dbperror(NULL, SYBENTLL, 0);
return FAIL;
}
switch (which) {
case DBSETHOST:
copy_ret = tds_set_host(login->tds_login, value_nonull);
break;
case DBSETUSER:
copy_ret = tds_set_user(login->tds_login, value_nonull);
break;
case DBSETPWD:
copy_ret = tds_set_passwd(login->tds_login, value_nonull);
break;
case DBSETAPP:
copy_ret = tds_set_app(login->tds_login, value_nonull);
break;
case DBSETCHARSET:
/* TODO NULL == no conversion desired */
copy_ret = tds_set_client_charset(login->tds_login, value_nonull);
break;
case DBSETNATLANG:
copy_ret = tds_set_language(login->tds_login, value_nonull);
break;
case DBSETDBNAME:
copy_ret = !!tds_dstr_copy(&login->tds_login->database, value_nonull);
break;
case DBSETSERVERPRINCIPAL:
copy_ret = !!tds_dstr_copy(&login->tds_login->server_spn, value_nonull);
break;
case DBSETENCRYPTION:
copy_ret = tds_parse_conf_section(TDS_STR_ENCRYPTION, value_nonull, login->tds_login);
break;
default:
dbperror(NULL, SYBEASUL, 0); /* Attempt to set unknown LOGINREC field */
return FAIL;
break;
}
if (!copy_ret)
return FAIL;
return SUCCEED;
}
/** \internal
* \ingroup dblib_internal
* \brief Set an integer value in a \c LOGINREC structure.
*
* Called by various macros to populate \a login.
* \param login the \c LOGINREC* to modify.
* \param value the value to set it to.
* \param which the field to set.
* \retval SUCCEED the value was set.
* \retval FAIL anything other than \c DBSETPACKET was passed for \a which.
*/
RETCODE
dbsetllong(LOGINREC * login, long value, int which)
{
tdsdump_log(TDS_DBG_FUNC, "dbsetllong(%p, %ld, %d)\n", login, value, which);
if( login == NULL ) {
dbperror(NULL, SYBEASNL, 0);
return FAIL;
}
switch (which) {
case DBSETPACKET:
if (0 <= value && value <= 999999) {
tds_set_packet(login->tds_login, value);
return SUCCEED;
}
dbperror(0, SYBEBADPK, 0, (int) value, (int) login->tds_login->block_size);
return FAIL;
break;
default:
tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetllong() which = %d\n", which);
return FAIL;
break;
}
}
/** \internal
* \ingroup dblib_internal
* \brief Set an integer value in a \c LOGINREC structure.
*
* Called by various macros to populate \a login.
* \param login the \c LOGINREC* to modify.
* \param value the value to set it to.
* \param which the field to set.
* \retval SUCCEED the value was set.
* \retval FAIL if invalid field in \a which or invalid \a value.
*/
RETCODE
dbsetlshort(LOGINREC * login, int value, int which)
{
tdsdump_log(TDS_DBG_FUNC, "dbsetlshort(%p, %d, %d)\n", login, value, which);
if( login == NULL ) {
dbperror(NULL, SYBEASNL, 0);
return FAIL;
}
switch (which) {
case DBSETPORT:
tds_set_port(login->tds_login, value);
return SUCCEED;
/* case DBSETHIER: */
default:
tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetlshort() which = %d\n", which);
return FAIL;
break;
}
}
/** \internal
* \ingroup dblib_internal
* \brief Set a boolean value in a \c LOGINREC structure.
*
* Called by various macros to populate \a login.
* \param login the \c LOGINREC* to modify.
* \param value the value to set it to.
* \param which the field to set.
* \remark Only DBSETBCP is implemented.
* \retval SUCCEED the value was set.
* \retval FAIL invalid value passed for \a which.
* \todo DBSETNOSHORT, DBSETENCRYPT, DBSETLABELED
*/
RETCODE
dbsetlbool(LOGINREC * login, int value, int which)
{
bool b_value;
tdsdump_log(TDS_DBG_FUNC, "dbsetlbool(%p, %d, %d)\n", login, value, which);
if (login == NULL) {
dbperror(NULL, SYBEASNL, 0);
return FAIL;
}
b_value = (value != 0);
switch (which) {
case DBSETBCP:
tds_set_bulk(login->tds_login, b_value);
return SUCCEED;
case DBSETUTF16:
login->tds_login->use_utf16 = b_value;
return SUCCEED;
case DBSETNTLMV2:
login->tds_login->use_ntlmv2 = b_value;
login->tds_login->use_ntlmv2_specified = 1;
return SUCCEED;
case DBSETREADONLY:
login->tds_login->readonly_intent = b_value;
return SUCCEED;
case DBSETNETWORKAUTH:
login->network_auth = b_value;
return SUCCEED;
case DBSETMUTUALAUTH:
login->tds_login->mutual_authentication = b_value;
return SUCCEED;
case DBSETDELEGATION:
login->tds_login->gssapi_use_delegation = b_value;
return SUCCEED;
case DBSETENCRYPT:
case DBSETLABELED:
default:
tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED dbsetlbool() which = %d\n", which);
return FAIL;
break;
}
}
/**
* \ingroup dblib_core
* \brief Set TDS version for future connections
*
*/
RETCODE
dbsetlversion(LOGINREC * login, BYTE version)
{
tdsdump_log(TDS_DBG_FUNC, "dbsetlversion(%p, %x)\n", login, version);
if( login == NULL ) {
dbperror(NULL, SYBEASNL, 0);
return FAIL;
}
assert(login->tds_login != NULL);
switch (version) {
case DBVERSION_UNKNOWN:
tds_set_version(login->tds_login, 0, 0);
return SUCCEED;
case DBVERSION_42:
tds_set_version(login->tds_login, 4, 2);
return SUCCEED;
case DBVERSION_100:
tds_set_version(login->tds_login, 5, 0);
return SUCCEED;
case DBVERSION_70:
tds_set_version(login->tds_login, 7, 0);
return SUCCEED;
case DBVERSION_71:
tds_set_version(login->tds_login, 7, 1);
return SUCCEED;
case DBVERSION_72:
tds_set_version(login->tds_login, 7, 2);
return SUCCEED;
case DBVERSION_73:
tds_set_version(login->tds_login, 7, 3);
return SUCCEED;
case DBVERSION_74:
tds_set_version(login->tds_login, 7, 4);
return SUCCEED;
}
return FAIL;
}
static void
dbstring_free(DBSTRING ** dbstrp)
{
DBSTRING *curr, *next;
/* tdsdump_log(TDS_DBG_FUNC, "dbstring_free(%p)\n", dbstrp); */