-
Notifications
You must be signed in to change notification settings - Fork 36
/
sqliteodbc.c
16664 lines (15643 loc) · 394 KB
/
sqliteodbc.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
/**
* @file sqliteodbc.c
* SQLite ODBC Driver main module.
*
* $Id: sqliteodbc.c,v 1.227 2023/10/23 13:33:58 chw Exp chw $
*
* Copyright (c) 2001-2023 Christian Werner <chw@ch-werner.de>
* OS/2 Port Copyright (c) 2004 Lorne R. Sunley <lsunley@mb.sympatico.ca>
*
* See the file "license.terms" for information on usage
* and redistribution of this file and for a
* DISCLAIMER OF ALL WARRANTIES.
*/
#include "sqliteodbc.h"
#ifdef SQLITE_UTF8
#ifndef WITHOUT_WINTERFACE
#define WINTERFACE
#define WCHARSUPPORT
#endif
#endif
#ifdef SQLITE_UTF8
#if !defined(_WIN32) && !defined(_WIN64)
#if !defined(WCHARSUPPORT) && defined(HAVE_SQLWCHAR) && (HAVE_SQLWCHAR)
#define WCHARSUPPORT
#endif
#endif
#endif
#if defined(WINTERFACE)
#include <sqlucode.h>
#endif
#if defined(_WIN32) || defined(_WIN64)
#include "resource.h"
#define ODBC_INI "ODBC.INI"
#ifndef DRIVER_VER_INFO
#define DRIVER_VER_INFO VERSION
#endif
#else
#ifdef __OS2__
#define ODBC_INI "ODBC.INI"
#else
#define ODBC_INI ".odbc.ini"
#endif
#endif
#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 3)))
#define CANT_PASS_VALIST_AS_CHARPTR 1
#endif
#ifdef _WIN64
#undef CANT_PASS_VALIST_AS_CHARPTR
#define CANT_PASS_VALIST_AS_CHARPTR 1
#endif
#ifdef CANT_PASS_VALIST_AS_CHARPTR
#define MAX_PARAMS_FOR_VPRINTF 32
#endif
#ifndef DRIVER_VER_INFO
#define DRIVER_VER_INFO "0.0"
#endif
#ifndef COLATTRIBUTE_LAST_ARG_TYPE
#ifdef _WIN64
#define COLATTRIBUTE_LAST_ARG_TYPE SQLLEN *
#else
#define COLATTRIBUTE_LAST_ARG_TYPE SQLPOINTER
#endif
#endif
#ifndef SETSTMTOPTION_LAST_ARG_TYPE
#define SETSTMTOPTION_LAST_ARG_TYPE SQLROWCOUNT
#endif
#undef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#undef max
#define max(a, b) ((a) < (b) ? (b) : (a))
#ifndef PTRDIFF_T
#define PTRDIFF_T int
#endif
#define array_size(x) (sizeof (x) / sizeof (x[0]))
#define stringify1(s) #s
#define stringify(s) stringify1(s)
#define verinfo(maj, min, lev) ((maj) << 16 | (min) << 8 | (lev))
/* Column types for static string column descriptions (SQLTables etc.) */
#if defined(WINTERFACE) && !defined(_WIN32) && !defined(_WIN64)
#define SCOL_VARCHAR SQL_WVARCHAR
#define SCOL_CHAR SQL_WCHAR
#else
#define SCOL_VARCHAR SQL_VARCHAR
#define SCOL_CHAR SQL_CHAR
#endif
#define ENV_MAGIC 0x53544145
#define DBC_MAGIC 0x53544144
#define DEAD_MAGIC 0xdeadbeef
#ifdef MEMORY_DEBUG
static void *
xmalloc_(int n, char *file, int line)
{
int nn = n + 4 * sizeof (long);
long *p;
p = malloc(nn);
if (!p) {
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "malloc\t%d\tNULL\t%s:%d\n", n, file, line);
#endif
return NULL;
}
p[0] = 0xdead1234;
nn = nn / sizeof (long) - 1;
p[1] = n;
p[nn] = 0xdead5678;
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "malloc\t%d\t%p\t%s:%d\n", n, &p[2], file, line);
#endif
return (void *) &p[2];
}
static void *
xrealloc_(void *old, int n, char *file, int line)
{
int nn = n + 4 * sizeof (long), nnn;
long *p, *pp;
if (n == 0 || !old) {
return xmalloc_(n, file, line);
}
p = &((long *) old)[-2];
if (p[0] != 0xdead1234) {
fprintf(stderr, "*** low end corruption @ %p\n", old);
abort();
}
nnn = p[1] + 4 * sizeof (long);
nnn = nnn / sizeof (long) - 1;
if (p[nnn] != 0xdead5678) {
fprintf(stderr, "*** high end corruption @ %p\n", old);
abort();
}
pp = realloc(p, nn);
if (!pp) {
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "realloc\t%p,%d\tNULL\t%s:%d\n", old, n, file, line);
#endif
return NULL;
}
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "realloc\t%p,%d\t%p\t%s:%d\n", old, n, &pp[2], file, line);
#endif
p = pp;
p[1] = n;
nn = nn / sizeof (long) - 1;
p[nn] = 0xdead5678;
return (void *) &p[2];
}
static void
xfree_(void *x, char *file, int line)
{
long *p;
int n;
if (!x) {
return;
}
p = &((long *) x)[-2];
if (p[0] != 0xdead1234) {
fprintf(stderr, "*** low end corruption @ %p\n", x);
abort();
}
n = p[1] + 4 * sizeof (long);
n = n / sizeof (long) - 1;
if (p[n] != 0xdead5678) {
fprintf(stderr, "*** high end corruption @ %p\n", x);
abort();
}
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "free\t%p\t\t%s:%d\n", x, file, line);
#endif
free(p);
}
static void
xfree__(void *x)
{
xfree_(x, "unknown location", 0);
}
static char *
xstrdup_(const char *str, char *file, int line)
{
char *p;
if (!str) {
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "strdup\tNULL\tNULL\t%s:%d\n", file, line);
#endif
return NULL;
}
p = xmalloc_(strlen(str) + 1, file, line);
if (p) {
strcpy(p, str);
}
#if (MEMORY_DEBUG > 1)
fprintf(stderr, "strdup\t%p\t%p\t%s:%d\n", str, p, file, line);
#endif
return p;
}
#define xmalloc(x) xmalloc_(x, __FILE__, __LINE__)
#define xrealloc(x,y) xrealloc_(x, y, __FILE__, __LINE__)
#define xfree(x) xfree_(x, __FILE__, __LINE__)
#define xstrdup(x) xstrdup_(x, __FILE__, __LINE__)
#else
#define xmalloc(x) malloc(x)
#define xrealloc(x,y) realloc(x, y)
#define xfree(x) free(x)
#define xstrdup(x) strdup_(x)
#endif
#if defined(_WIN32) || defined(_WIN64)
#define vsnprintf _vsnprintf
#define snprintf _snprintf
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
static HINSTANCE NEAR hModule; /* Saved module handle for resources */
#endif
#if defined(_WIN32) || defined(_WIN64)
/*
* SQLHENV, SQLHDBC, and SQLHSTMT synchronization
* is done using a critical section in ENV structure.
*/
#define HDBC_LOCK(hdbc) \
{ \
DBC *d; \
\
if ((hdbc) == SQL_NULL_HDBC) { \
return SQL_INVALID_HANDLE; \
} \
d = (DBC *) (hdbc); \
if (d->magic != DBC_MAGIC) { \
return SQL_INVALID_HANDLE; \
} \
EnterCriticalSection(&d->cs); \
d->owner = GetCurrentThreadId(); \
}
#define HDBC_UNLOCK(hdbc) \
if ((hdbc) != SQL_NULL_HDBC) { \
DBC *d; \
\
d = (DBC *) (hdbc); \
if (d->magic == DBC_MAGIC) { \
d->owner = 0; \
LeaveCriticalSection(&d->cs); \
} \
}
#define HSTMT_LOCK(hstmt) \
{ \
DBC *d; \
\
if ((hstmt) == SQL_NULL_HSTMT) { \
return SQL_INVALID_HANDLE; \
} \
d = (DBC *) ((STMT *) (hstmt))->dbc; \
if (d->magic != DBC_MAGIC) { \
return SQL_INVALID_HANDLE; \
} \
EnterCriticalSection(&d->cs); \
d->owner = GetCurrentThreadId(); \
}
#define HSTMT_UNLOCK(hstmt) \
if ((hstmt) != SQL_NULL_HSTMT) { \
DBC *d; \
\
d = (DBC *) ((STMT *) (hstmt))->dbc; \
if (d->magic == DBC_MAGIC) { \
d->owner = 0; \
LeaveCriticalSection(&d->cs); \
} \
}
#else
/*
* On UN*X assume that we are single-threaded or
* the driver manager provides serialization for us.
*
* In iODBC (3.52.x) serialization can be turned
* on using the DSN property "ThreadManager=yes".
*
* In unixODBC that property is named
* "Threading=0-3" and takes one of these values:
*
* 0 - no protection
* 1 - statement level protection
* 2 - connection level protection
* 3 - environment level protection
*
* unixODBC 2.2.11 uses environment level protection
* by default when it has been built with pthread
* support.
*/
#define HDBC_LOCK(hdbc)
#define HDBC_UNLOCK(hdbc)
#define HSTMT_LOCK(hdbc)
#define HSTMT_UNLOCK(hdbc)
#endif
/*
* tolower() replacement w/o locale
*/
static const char upper_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char lower_chars[] = "abcdefghijklmnopqrstuvwxyz";
static int
TOLOWER(int c)
{
if (c) {
char *p = strchr(upper_chars, c);
if (p) {
c = lower_chars[p - upper_chars];
}
}
return c;
}
/*
* isdigit() replacement w/o ctype.h
*/
static const char digit_chars[] = "0123456789";
#define ISDIGIT(c) \
((c) && strchr(digit_chars, (c)) != NULL)
/*
* isspace() replacement w/o ctype.h
*/
static const char space_chars[] = " \f\n\r\t\v";
#define ISSPACE(c) \
((c) && strchr(space_chars, (c)) != NULL)
/*
* Characters in named parameters
*/
static const char id_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"_0123456789";
#define ISIDCHAR(c) \
((c) && strchr(id_chars, (c)) != NULL)
/*
* Forward declarations of static functions.
*/
static void freedyncols(STMT *s);
static void freeresult(STMT *s, int clrcols);
static void unbindcols(STMT *s);
static SQLRETURN drvexecute(SQLHSTMT stmt, int initial);
static SQLRETURN freestmt(HSTMT stmt);
static SQLRETURN mkbindcols(STMT *s, int ncols);
static SQLRETURN setupparbuf(STMT *s, BINDPARM *p);
static SQLRETURN starttran(STMT *s);
static SQLRETURN substparam(STMT *s, int pnum, char **outp);
#if (defined(_WIN32) || defined(_WIN64)) && defined(WINTERFACE)
/* MS Access hack part 1 (reserved error -7748) */
static COL *statSpec2P, *statSpec3P;
#endif
#if (MEMORY_DEBUG < 1)
/**
* Duplicate string using xmalloc().
* @param str string to be duplicated
* @result pointer to new string or NULL
*/
static char *
strdup_(const char *str)
{
char *p = NULL;
if (str) {
p = xmalloc(strlen(str) + 1);
if (p) {
strcpy(p, str);
}
}
return p;
}
#endif
#ifdef WCHARSUPPORT
/**
* Return length of UNICODE string.
* @param str UNICODE string
* @result length of string in characters
*/
static int
uc_strlen(SQLWCHAR *str)
{
int len = 0;
if (str) {
while (*str) {
++len;
++str;
}
}
return len;
}
/**
* Copy UNICODE string like strncpy().
* @param dest destination area
* @param src source area
* @param len length of source area in characters
* @return pointer to destination area
*/
static SQLWCHAR *
uc_strncpy(SQLWCHAR *dest, SQLWCHAR *src, int len)
{
int i = 0;
while (i < len) {
if (!src[i]) {
break;
}
dest[i] = src[i];
++i;
}
if (i < len) {
dest[i] = 0;
}
return dest;
}
/**
* Make UNICODE string from UTF8 string into buffer.
* @param str UTF8 string to be converted
* @param len length in characters of str or -1
* @param uc destination area to receive UNICODE string
* @param ucLen byte length of destination area
*/
static void
uc_from_utf_buf(unsigned char *str, int len, SQLWCHAR *uc, int ucLen)
{
ucLen = ucLen / sizeof (SQLWCHAR);
if (!uc || ucLen < 0) {
return;
}
if (len < 0) {
len = ucLen * 5;
}
uc[0] = 0;
if (str) {
int i = 0;
while (i < len && *str && i < ucLen) {
unsigned char c = str[0];
if (c < 0x80) {
uc[i++] = c;
++str;
} else if (c <= 0xc1 || c >= 0xf5) {
/* illegal, ignored */
++str;
} else if (c < 0xe0) {
if ((str[1] & 0xc0) == 0x80) {
unsigned long t = ((c & 0x1f) << 6) | (str[1] & 0x3f);
uc[i++] = t;
str += 2;
} else {
uc[i++] = c;
++str;
}
} else if (c < 0xf0) {
if ((str[1] & 0xc0) == 0x80 && (str[2] & 0xc0) == 0x80) {
unsigned long t = ((c & 0x0f) << 12) |
((str[1] & 0x3f) << 6) | (str[2] & 0x3f);
uc[i++] = t;
str += 3;
} else {
uc[i++] = c;
++str;
}
} else if (c < 0xf8) {
if ((str[1] & 0xc0) == 0x80 && (str[2] & 0xc0) == 0x80 &&
(str[3] & 0xc0) == 0x80) {
unsigned long t = ((c & 0x03) << 18) |
((str[1] & 0x3f) << 12) | ((str[2] & 0x3f) << 6) |
(str[3] & 0x3f);
if (sizeof (SQLWCHAR) == 2 * sizeof (char) &&
t >= 0x10000) {
t -= 0x10000;
uc[i++] = 0xd800 | ((t >> 10) & 0x3ff);
if (i >= ucLen) {
break;
}
t = 0xdc00 | (t & 0x3ff);
}
uc[i++] = t;
str += 4;
} else {
uc[i++] = c;
++str;
}
} else {
/* ignore */
++str;
}
}
if (i < ucLen) {
uc[i] = 0;
}
}
}
/**
* Make UNICODE string from UTF8 string.
* @param str UTF8 string to be converted
* @param len length of UTF8 string
* @return alloc'ed UNICODE string to be free'd by uc_free()
*/
static SQLWCHAR *
uc_from_utf(unsigned char *str, int len)
{
SQLWCHAR *uc = NULL;
int ucLen;
if (str) {
if (len == SQL_NTS) {
len = strlen((char *) str);
}
ucLen = sizeof (SQLWCHAR) * (len + 1);
uc = xmalloc(ucLen);
if (uc) {
uc_from_utf_buf(str, len, uc, ucLen);
}
}
return uc;
}
/**
* Make UTF8 string from UNICODE string.
* @param str UNICODE string to be converted
* @param len length of UNICODE string in bytes
* @return alloc'ed UTF8 string to be free'd by uc_free()
*/
static char *
uc_to_utf(SQLWCHAR *str, int len)
{
int i;
char *cp, *ret = NULL;
if (!str) {
return ret;
}
if (len == SQL_NTS) {
len = uc_strlen(str);
} else {
len = len / sizeof (SQLWCHAR);
}
cp = xmalloc(len * 6 + 1);
if (!cp) {
return ret;
}
ret = cp;
for (i = 0; i < len; i++) {
unsigned long c = str[i];
if (sizeof (SQLWCHAR) == 2 * sizeof (char)) {
c &= 0xffff;
}
if (c < 0x80) {
*cp++ = c;
} else if (c < 0x800) {
*cp++ = 0xc0 | ((c >> 6) & 0x1f);
*cp++ = 0x80 | (c & 0x3f);
} else if (c < 0x10000) {
if (sizeof (SQLWCHAR) == 2 * sizeof (char) &&
c >= 0xd800 && c <= 0xdbff && i + 1 < len) {
unsigned long c2 = str[i + 1] & 0xffff;
if (c2 >= 0xdc00 && c2 <= 0xdfff) {
c = (((c & 0x3ff) << 10) | (c2 & 0x3ff)) + 0x10000;
*cp++ = 0xf0 | ((c >> 18) & 0x07);
*cp++ = 0x80 | ((c >> 12) & 0x3f);
*cp++ = 0x80 | ((c >> 6) & 0x3f);
*cp++ = 0x80 | (c & 0x3f);
++i;
continue;
}
}
*cp++ = 0xe0 | ((c >> 12) & 0x0f);
*cp++ = 0x80 | ((c >> 6) & 0x3f);
*cp++ = 0x80 | (c & 0x3f);
} else if (c <= 0x10ffff) {
*cp++ = 0xf0 | ((c >> 18) & 0x07);
*cp++ = 0x80 | ((c >> 12) & 0x3f);
*cp++ = 0x80 | ((c >> 6) & 0x3f);
*cp++ = 0x80 | (c & 0x3f);
}
}
*cp = '\0';
return ret;
}
#endif
#ifdef WINTERFACE
/**
* Make UTF8 string from UNICODE string.
* @param str UNICODE string to be converted
* @param len length of UNICODE string in characters
* @return alloc'ed UTF8 string to be free'd by uc_free()
*/
static char *
uc_to_utf_c(SQLWCHAR *str, int len)
{
if (len != SQL_NTS) {
len = len * sizeof (SQLWCHAR);
}
return uc_to_utf(str, len);
}
#endif
#if defined(WINTERFACE) || defined(WCHARSUPPORT)
/**
* Free converted UTF8 or UNICODE string.
*/
static void
uc_free(void *str)
{
if (str) {
xfree(str);
}
}
#endif
#ifdef USE_DLOPEN_FOR_GPPS
#include <dlfcn.h>
#define SQLGetPrivateProfileString(A,B,C,D,E,F) drvgpps(d,A,B,C,D,E,F)
/*
* EXPERIMENTAL: SQLGetPrivateProfileString infrastructure using
* dlopen(), in theory this makes the driver independent from the
* driver manager, i.e. the same driver binary can run with iODBC
* and unixODBC.
*/
static void
drvgetgpps(DBC *d)
{
void *lib;
int (*gpps)();
lib = dlopen("libodbcinst.so.2", RTLD_LAZY);
if (!lib) {
lib = dlopen("libodbcinst.so.1", RTLD_LAZY);
}
if (!lib) {
lib = dlopen("libodbcinst.so", RTLD_LAZY);
}
if (!lib) {
lib = dlopen("libiodbcinst.so.2", RTLD_LAZY);
}
if (!lib) {
lib = dlopen("libiodbcinst.so", RTLD_LAZY);
}
if (lib) {
gpps = (int (*)()) dlsym(lib, "SQLGetPrivateProfileString");
if (!gpps) {
dlclose(lib);
return;
}
d->instlib = lib;
d->gpps = gpps;
}
}
static void
drvrelgpps(DBC *d)
{
if (d->instlib) {
dlclose(d->instlib);
d->instlib = 0;
}
}
static int
drvgpps(DBC *d, char *sect, char *ent, char *def, char *buf,
int bufsiz, char *fname)
{
if (d->gpps) {
return d->gpps(sect, ent, def, buf, bufsiz, fname);
}
strncpy(buf, def, bufsiz);
buf[bufsiz - 1] = '\0';
return 1;
}
#else
#include <odbcinst.h>
#define drvgetgpps(d)
#define drvrelgpps(d)
#endif
/**
* Set error message and SQL state on DBC
* @param d database connection pointer
* @param naterr native error code
* @param msg error message
* @param st SQL state
*/
#if defined(__GNUC__) && (__GNUC__ >= 2)
static void setstatd(DBC *, int, char *, char *, ...)
__attribute__((format (printf, 3, 5)));
#endif
static void
setstatd(DBC *d, int naterr, char *msg, char *st, ...)
{
va_list ap;
if (!d) {
return;
}
d->naterr = naterr;
d->logmsg[0] = '\0';
if (msg) {
int count;
va_start(ap, st);
count = vsnprintf((char *) d->logmsg, sizeof (d->logmsg), msg, ap);
va_end(ap);
if (count < 0) {
d->logmsg[sizeof (d->logmsg) - 1] = '\0';
}
}
if (!st) {
st = "?????";
}
strncpy(d->sqlstate, st, 5);
d->sqlstate[5] = '\0';
}
/**
* Set error message and SQL state on statement
* @param s statement pointer
* @param naterr native error code
* @param msg error message
* @param st SQL state
*/
#if defined(__GNUC__) && (__GNUC__ >= 2)
static void setstat(STMT *, int, char *, char *, ...)
__attribute__((format (printf, 3, 5)));
#endif
static void
setstat(STMT *s, int naterr, char *msg, char *st, ...)
{
va_list ap;
if (!s) {
return;
}
s->naterr = naterr;
s->logmsg[0] = '\0';
if (msg) {
int count;
va_start(ap, st);
count = vsnprintf((char *) s->logmsg, sizeof (s->logmsg), msg, ap);
va_end(ap);
if (count < 0) {
s->logmsg[sizeof (s->logmsg) - 1] = '\0';
}
}
if (!st) {
st = "?????";
}
strncpy(s->sqlstate, st, 5);
s->sqlstate[5] = '\0';
}
/**
* Report IM001 (not implemented) SQL error code for HDBC.
* @param dbc database connection handle
* @result ODBC error code
*/
static SQLRETURN
drvunimpldbc(HDBC dbc)
{
DBC *d;
if (dbc == SQL_NULL_HDBC) {
return SQL_INVALID_HANDLE;
}
d = (DBC *) dbc;
setstatd(d, -1, "not supported", "IM001");
return SQL_ERROR;
}
/**
* Report IM001 (not implemented) SQL error code for HSTMT.
* @param stmt statement handle
* @result ODBC error code
*/
static SQLRETURN
drvunimplstmt(HSTMT stmt)
{
STMT *s;
if (stmt == SQL_NULL_HSTMT) {
return SQL_INVALID_HANDLE;
}
s = (STMT *) stmt;
setstat(s, -1, "not supported", "IM001");
return SQL_ERROR;
}
/**
* Free memory given pointer to memory pointer.
* @param x pointer to pointer to memory to be free'd
*/
static void
freep(void *x)
{
if (x && ((char **) x)[0]) {
xfree(((char **) x)[0]);
((char **) x)[0] = NULL;
}
}
/**
* Report S1000 (out of memory) SQL error given STMT.
* @param s statement pointer
* @result ODBC error code
*/
static SQLRETURN
nomem(STMT *s)
{
setstat(s, -1, "out of memory", (*s->ov3) ? "HY000" : "S1000");
return SQL_ERROR;
}
/**
* Report S1000 (not connected) SQL error given STMT.
* @param s statement pointer
* @result ODBC error code
*/
static SQLRETURN
noconn(STMT *s)
{
setstat(s, -1, "not connected", (*s->ov3) ? "HY000" : "S1000");
return SQL_ERROR;
}
/**
* Internal locale neutral strtod function.
* @param data pointer to string
* @param endp pointer for ending character
* @result double value
*/
#if defined(HAVE_SQLITEATOF) && (HAVE_SQLITEATOF)
extern double sqliteAtoF(char *data, char **endp);
#define ln_strtod(A,B) sqliteAtoF(A,B)
#else
#if defined(HAVE_LOCALECONV) || defined(_WIN32) || defined(_WIN64)
static double
ln_strtod(const char *data, char **endp)
{
struct lconv *lc = 0;
char buf[128], *p, *end;
double value;
lc = localeconv();
if (lc && lc->decimal_point && lc->decimal_point[0] &&
lc->decimal_point[0] != '.') {
strncpy(buf, data, sizeof (buf) - 1);
buf[sizeof (buf) - 1] = '\0';
p = strchr(buf, '.');
if (p) {
*p = lc->decimal_point[0];
}
p = buf;
} else {
p = (char *) data;
}
value = strtod(p, &end);
end = (char *) data + (end - p);
if (endp) {
*endp = end;
}
return value;
}
#else
#define ln_strtod(A,B) strtod(A,B)
#endif
#endif
#if !defined(HAVE_SQLITEMPRINTF) || !(HAVE_SQLITEMPRINTF)
/**
* Internal locale neutral sprintf("%g") function.
* @param buf pointer to string
* @param value double value
*/
static void
ln_sprintfg(char *buf, double value)
{
#if defined(HAVE_LOCALECONV) || defined(_WIN32) || defined(_WIN64)
struct lconv *lc = 0;
char *p;
sprintf(buf, "%.16g", value);
lc = localeconv();
if (lc && lc->decimal_point && lc->decimal_point[0] &&
lc->decimal_point[0] != '.') {
p = strchr(buf, lc->decimal_point[0]);
if (p) {
*p = '.';
}
}
#else
sprintf(buf, "%.16g", value);
#endif
}
#endif
/**
* Strip quotes from quoted string in-place.