-
Notifications
You must be signed in to change notification settings - Fork 15
/
convert.c
6554 lines (5998 loc) · 160 KB
/
convert.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
/*-------
* Module: convert.c
*
* Description: This module contains routines related to
* converting parameters and columns into requested data types.
* Parameters are converted from their SQL_C data types into
* the appropriate postgres type. Columns are converted from
* their postgres type (SQL type) into the appropriate SQL_C
* data type.
*
* Classes: n/a
*
* API functions: none
*
* Comments: See "readme.txt" for copyright and license information.
*-------
*/
/* Multibyte support Eiji Tokuya 2001-03-15 */
#include "convert.h"
#include "unicode_support.h"
#include "misc.h"
#ifdef WIN32
#include <float.h>
#define HAVE_LOCALE_H
#endif /* WIN32 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "multibyte.h"
#include <time.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include "statement.h"
#include "qresult.h"
#include "bind.h"
#include "pgtypes.h"
#include "lobj.h"
#include "connection.h"
#include "catfunc.h"
#include "pgapifunc.h"
CSTR NAN_STRING = "NaN";
CSTR INFINITY_STRING = "Infinity";
CSTR MINFINITY_STRING = "-Infinity";
#if defined(WIN32) || defined(__CYGWIN__)
#define TIMEZONE_GLOBAL _timezone
#define TZNAME_GLOBAL _tzname
#define DAYLIGHT_GLOBAL _daylight
#elif defined(HAVE_INT_TIMEZONE)
#define TIMEZONE_GLOBAL timezone
#define TZNAME_GLOBAL tzname
#define DAYLIGHT_GLOBAL daylight
#endif
/*
* How to map ODBC scalar functions {fn func(args)} to Postgres.
* This is just a simple substitution. List augmented from:
* http://www.merant.com/datadirect/download/docs/odbc16/Odbcref/rappc.htm
* - thomas 2000-04-03
*/
static const struct
{
/*
* There's a horrible hack in odbc_name field: if it begins with a %,
* the digit after the % indicates the number of arguments. Otherwise,
* the entry matches any number of args.
*/
char *odbc_name;
char *pgsql_name;
} mapFuncs[] = {
/* { "ASCII", "ascii" }, built_in */
{"CHAR", "chr($*)" },
{"CONCAT", "concat($1::text, $2::text)" },
/* { "DIFFERENCE", "difference" }, how to ? */
{"INSERT", "substring($1 from 1 for $2 - 1) || $4 || substring($1 from $2 + $3)" },
{"LCASE", "lower($*)" },
/* {"LEFT", "left" }, built_in */
{"%2LOCATE", "strpos($2, $1)" }, /* 2 parameters */
{"%3LOCATE", "strpos(substring($2 from $3), $1) + $3 - 1" }, /* 3 parameters */
{"LENGTH", "char_length($*)"},
/* { "LTRIM", "ltrim" }, built_in */
/* {"RIGHT", "right" }, built_in */
{"SPACE", "repeat(' ', $1)" },
/* { "REPEAT", "repeat" }, built_in */
/* { "REPLACE", "replace" }, ??? */
/* { "RTRIM", "rtrim" }, built_in */
/* { "SOUNDEX", "soundex" }, how to ? */
{"SUBSTRING", "substr($*)" },
{"UCASE", "upper($*)" },
/* { "ABS", "abs" }, built_in */
/* { "ACOS", "acos" }, built_in */
/* { "ASIN", "asin" }, built_in */
/* { "ATAN", "atan" }, built_in */
/* { "ATAN2", "atan2" }, built_in */
{"CEILING", "ceil($*)" },
/* { "COS", "cos" }, built_in */
/* { "COT", "cot" }, built_in */
/* { "DEGREES", "degrees" }, built_in */
/* { "EXP", "exp" }, built_in */
/* { "FLOOR", "floor" }, built_in */
{"LOG", "ln($*)" },
{"LOG10", "log($*)" },
/* { "MOD", "mod" }, built_in */
/* { "PI", "pi" }, built_in */
/* {"POWER", "power" }, built_in */
/* { "RADIANS", "radians" }, built_in */
{"%0RAND", "random()" }, /* 0 parameters */
{"%1RAND", "(setseed($1) * .0 + random())" }, /* 1 parameters */
/* { "ROUND", "round" }, built_in */
/* { "SIGN", "sign" }, built_in */
/* { "SIN", "sin" }, built_in */
/* { "SQRT", "sqrt" }, built_in */
/* { "TAN", "tan" }, built_in */
{"TRUNCATE", "trunc($*)" },
{"CURRENT_DATE", "current_date" },
{"CURRENT_TIME", "current_time" },
{"CURRENT_TIMESTAMP", "current_timestamp" },
{"LOCALTIME", "localtime" },
{"LOCALTIMESTAMP", "localtimestamp" },
{"CURRENT_USER", "cast(current_user as text)" },
{"SESSION_USER", "cast(session_user as text)" },
{"CURDATE", "current_date" },
{"CURTIME", "current_time" },
{"DAYNAME", "to_char($1, 'Day')" },
{"DAYOFMONTH", "cast(extract(day from $1) as integer)" },
{"DAYOFWEEK", "(cast(extract(dow from $1) as integer) + 1)" },
{"DAYOFYEAR", "cast(extract(doy from $1) as integer)" },
{"HOUR", "cast(extract(hour from $1) as integer)" },
{"MINUTE", "cast(extract(minute from $1) as integer)" },
{"MONTH", "cast(extract(month from $1) as integer)" },
{"MONTHNAME", " to_char($1, 'Month')" },
/* { "NOW", "now" }, built_in */
{"QUARTER", "cast(extract(quarter from $1) as integer)" },
{"SECOND", "cast(extract(second from $1) as integer)" },
{"WEEK", "cast(extract(week from $1) as integer)" },
{"YEAR", "cast(extract(year from $1) as integer)" },
// TIMESTAMPADD()
{"TIMESTAMPADD(SQL_TSI_YEAR", "($3+make_interval(years := $2))" },
{"TIMESTAMPADD(SQL_TSI_MONTH", "($3+make_interval(months := $2))" },
{"TIMESTAMPADD(SQL_TSI_WEEK", "($3+make_interval(weeks := $2))" },
{"TIMESTAMPADD(SQL_TSI_DAY", "($3+make_interval(days := $2))" },
{"TIMESTAMPADD(SQL_TSI_HOUR", "($3+make_interval(hours := $2))" },
{"TIMESTAMPADD(SQL_TSI_MINUTE", "($3+make_interval(mins := $2))" },
{"TIMESTAMPADD(SQL_TSI_SECOND", "($3+make_interval(secs := $2))" },
{"TIMESTAMPADD(SQL_TSI_FRAC_SECOND", "($3+make_interval(secs := $2::float / 1000000))" },
// TIMESTAMPDIFF()
/* doesn't work properly?
{"TIMESTAMPDIFF(SQL_TSI_YEAR", "cast(extract(year from ($3 - $2)) as integer)" },
{"TIMESTAMPDIFF(SQL_TSI_MONTH", "cast(extract(month from ($3 - $2)) as integer)" },
{"TIMESTAMPDIFF(SQL_TSI_QUARTER", "cast(extract(quarter from ($3 - $2)) as integer)" },
{"TIMESTAMPDIFF(SQL_TSI_WEEK", "cast(extract(week from ($3 - $2)) as integer)" },
*/
/*
{"TIMESTAMPDIFF(SQL_TSI_DAY", "cast(extract(day from ($3 - $2)) as integer)" },
{"TIMESTAMPDIFF(SQL_TSI_HOUR", "cast(extract(hour from ($3 - $2)) as integer)" },
{"TIMESTAMPDIFF(SQL_TSI_HOUR", "(extract(epoch from $3) - extract(epoch from $2)) / 3600" },
{"TIMESTAMPDIFF(SQL_TSI_MINUTE", "cast(extract(minute from ($3 - $2)) as integer)" },
{"TIMESTAMPDIFF(SQL_TSI_SECOND", "cast(extract(second from ($3 - $2)) as integer)" },
*/
{"TIMESTAMPDIFF(SQL_TSI_DAY", "cast((extract(epoch from $3) - extract(epoch from $2)) / (24*60*60) as int)" },
{"TIMESTAMPDIFF(SQL_TSI_HOUR", "cast((extract(epoch from $3) - extract(epoch from $2)) / 3600 as int)" },
{"TIMESTAMPDIFF(SQL_TSI_MINUTE", "cast((extract(epoch from $3) - extract(epoch from $2)) / 60 as int)" },
{"TIMESTAMPDIFF(SQL_TSI_SECOND", "cast((extract(epoch from $3) - extract(epoch from $2)) as int)" },
{"TIMESTAMPDIFF(SQL_TSI_FRAC_SECOND", "mod(cast(extract(second from ($3 - $2)) as numeric), 1.0) * 1000000" },
/* { "EXTRACT", "extract" }, built_in */
/* { "DATABASE", "database" }, */
{"IFNULL", "coalesce($*)" },
{"USER", "cast(current_user as text)" },
{0, 0}
};
typedef struct
{
int infinity;
int m;
int d;
int y;
int hh;
int mm;
int ss;
int fr;
} SIMPLE_TIME;
static const char *mapFunction(const char *func, int param_count, const char * keyword);
static BOOL convert_money(const char *s, char *sout, size_t soutmax);
static char parse_datetime(const char *buf, SIMPLE_TIME *st);
size_t convert_linefeeds(const char *s, char *dst, size_t max, BOOL convlf, BOOL *changed);
static size_t convert_from_pgbinary(const char *value, char *rgbValue, SQLLEN cbValueMax);
static int convert_lo(StatementClass *stmt, const void *value, SQLSMALLINT fCType,
PTR rgbValue, SQLLEN cbValueMax, SQLLEN *pcbValue);
static int conv_from_octal(const char *s);
static SQLLEN pg_bin2hex(const char *src, char *dst, SQLLEN length);
#ifdef UNICODE_SUPPORT
static SQLLEN pg_bin2whex(const char *src, SQLWCHAR *dst, SQLLEN length);
#endif /* UNICODE_SUPPORT */
/*---------
* A Guide for date/time/timestamp conversions
*
* field_type fCType Output
* ---------- ------ ----------
* PG_TYPE_DATE SQL_C_DEFAULT SQL_C_DATE
* PG_TYPE_DATE SQL_C_DATE SQL_C_DATE
* PG_TYPE_DATE SQL_C_TIMESTAMP SQL_C_TIMESTAMP (time = 0 (midnight))
* PG_TYPE_TIME SQL_C_DEFAULT SQL_C_TIME
* PG_TYPE_TIME SQL_C_TIME SQL_C_TIME
* PG_TYPE_TIME SQL_C_TIMESTAMP SQL_C_TIMESTAMP (date = current date)
* PG_TYPE_ABSTIME SQL_C_DEFAULT SQL_C_TIMESTAMP
* PG_TYPE_ABSTIME SQL_C_DATE SQL_C_DATE (time is truncated)
* PG_TYPE_ABSTIME SQL_C_TIME SQL_C_TIME (date is truncated)
* PG_TYPE_ABSTIME SQL_C_TIMESTAMP SQL_C_TIMESTAMP
*---------
*/
/*
* Macros for unsigned long handling.
*/
#ifdef WIN32
#define ATOI32U(val) strtoul(val, NULL, 10)
#elif defined(HAVE_STRTOUL)
#define ATOI32U(val) strtoul(val, NULL, 10)
#else /* HAVE_STRTOUL */
#define ATOI32U atol
#endif /* WIN32 */
/*
* Macros for BIGINT handling.
*/
#ifdef ODBCINT64
#ifdef WIN32
#define ATOI64(val) _strtoi64(val, NULL, 10)
#define ATOI64U(val) _strtoui64(val, NULL, 10)
#elif (SIZEOF_LONG == 8)
#define ATOI64(val) strtol(val, NULL, 10)
#define ATOI64U(val) strtoul(val, NULL, 10)
#else
#if defined(HAVE_STRTOLL)
#define ATOI64(val) strtoll(val, NULL, 10)
#define ATOI64U(val) strtoull(val, NULL, 10)
#else
static ODBCINT64 ATOI64(const char *val)
{
ODBCINT64 ll;
sscanf(val, "%lld", &ll);
return ll;
}
static unsigned ODBCINT64 ATOI64U(const char *val)
{
unsigned ODBCINT64 ll;
sscanf(val, "%llu", &ll);
return ll;
}
#endif /* HAVE_STRTOLL */
#endif /* WIN32 */
#endif /* ODBCINT64 */
static void ResolveNumericParam(const SQL_NUMERIC_STRUCT *ns, char *chrform);
static void parse_to_numeric_struct(const char *wv, SQL_NUMERIC_STRUCT *ns, BOOL *overflow);
/*
* TIMESTAMP <-----> SIMPLE_TIME
* precision support since 7.2.
* time zone support is unavailable(the stuff is unreliable)
*/
static BOOL
timestamp2stime(const char *str, SIMPLE_TIME *st, BOOL *bZone, int *zone)
{
char rest[64], bc[16],
*ptr;
int scnt,
i;
int y, m, d, hh, mm, ss;
#ifdef TIMEZONE_GLOBAL
long timediff;
#endif
BOOL withZone = *bZone;
*bZone = FALSE;
*zone = 0;
st->fr = 0;
st->infinity = 0;
rest[0] = '\0';
bc[0] = '\0';
if ((scnt = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d%31s %15s", &y, &m, &d, &hh, &mm, &ss, rest, bc)) < 6)
{
if (scnt == 3) /* date */
{
st->y = y;
st->m = m;
st->d = d;
st->hh = 0;
st->mm = 0;
st->ss = 0;
return TRUE;
}
if ((scnt = sscanf(str, "%2d:%2d:%2d%31s %15s", &hh, &mm, &ss, rest, bc)) < 3)
return FALSE;
else
{
st->hh = hh;
st->mm = mm;
st->ss = ss;
if (scnt == 3) /* time */
return TRUE;
}
}
else
{
st->y = y;
st->m = m;
st->d = d;
st->hh = hh;
st->mm = mm;
st->ss = ss;
if (scnt == 6)
return TRUE;
}
switch (rest[0])
{
case '+':
*bZone = TRUE;
*zone = atoi(&rest[1]);
break;
case '-':
*bZone = TRUE;
*zone = -atoi(&rest[1]);
break;
case '.':
if ((ptr = strchr(rest, '+')) != NULL)
{
*bZone = TRUE;
*zone = atoi(&ptr[1]);
*ptr = '\0';
}
else if ((ptr = strchr(rest, '-')) != NULL)
{
*bZone = TRUE;
*zone = -atoi(&ptr[1]);
*ptr = '\0';
}
for (i = 1; i < 10; i++)
{
if (!isdigit((UCHAR) rest[i]))
break;
}
for (; i < 10; i++)
rest[i] = '0';
rest[i] = '\0';
st->fr = atoi(&rest[1]);
break;
case 'B':
if (stricmp(rest, "BC") == 0)
st->y *= -1;
return TRUE;
default:
return TRUE;
}
if (stricmp(bc, "BC") == 0)
{
st->y *= -1;
}
if (!withZone || !*bZone || st->y < 1970)
return TRUE;
#ifdef TIMEZONE_GLOBAL
if (!TZNAME_GLOBAL[0] || !TZNAME_GLOBAL[0][0])
{
*bZone = FALSE;
return TRUE;
}
timediff = TIMEZONE_GLOBAL + (*zone) * 3600;
if (!DAYLIGHT_GLOBAL && timediff == 0) /* the same timezone */
return TRUE;
else
{
struct tm tm,
*tm2;
time_t time0;
*bZone = FALSE;
tm.tm_year = st->y - 1900;
tm.tm_mon = st->m - 1;
tm.tm_mday = st->d;
tm.tm_hour = st->hh;
tm.tm_min = st->mm;
tm.tm_sec = st->ss;
tm.tm_isdst = -1;
time0 = mktime(&tm);
if (time0 < 0)
return TRUE;
if (tm.tm_isdst > 0)
timediff -= 3600;
if (timediff == 0) /* the same time zone */
return TRUE;
time0 -= timediff;
#ifdef HAVE_LOCALTIME_R
if (time0 >= 0 && (tm2 = localtime_r(&time0, &tm)) != NULL)
#else
if (time0 >= 0 && (tm2 = localtime(&time0)) != NULL)
#endif /* HAVE_LOCALTIME_R */
{
st->y = tm2->tm_year + 1900;
st->m = tm2->tm_mon + 1;
st->d = tm2->tm_mday;
st->hh = tm2->tm_hour;
st->mm = tm2->tm_min;
st->ss = tm2->tm_sec;
*bZone = TRUE;
}
}
#endif /* TIMEZONE_GLOBAL */
return TRUE;
}
static int
stime2timestamp(const SIMPLE_TIME *st, char *str, size_t bufsize, BOOL bZone,
int precision)
{
char precstr[16],
zonestr[16];
int i;
precstr[0] = '\0';
if (st->infinity > 0)
{
return snprintf(str, bufsize, "%s", INFINITY_STRING);
}
else if (st->infinity < 0)
{
return snprintf(str, bufsize, "%s", MINFINITY_STRING);
}
if (precision > 0 && st->fr)
{
SPRINTF_FIXED(precstr, ".%09d", st->fr);
if (precision < 9)
precstr[precision + 1] = '\0';
else if (precision > 9)
precision = 9;
for (i = precision; i > 0; i--)
{
if (precstr[i] != '0')
break;
precstr[i] = '\0';
}
if (i == 0)
precstr[i] = '\0';
}
zonestr[0] = '\0';
#ifdef TIMEZONE_GLOBAL
if (bZone && TZNAME_GLOBAL[0] && TZNAME_GLOBAL[0][0] && st->y >= 1970)
{
long zoneint;
struct tm tm;
time_t time0;
zoneint = TIMEZONE_GLOBAL;
if (DAYLIGHT_GLOBAL && st->y >= 1900)
{
tm.tm_year = st->y - 1900;
tm.tm_mon = st->m - 1;
tm.tm_mday = st->d;
tm.tm_hour = st->hh;
tm.tm_min = st->mm;
tm.tm_sec = st->ss;
tm.tm_isdst = -1;
time0 = mktime(&tm);
if (time0 >= 0 && tm.tm_isdst > 0)
zoneint -= 3600;
}
if (zoneint > 0)
SPRINTF_FIXED(zonestr, "-%02d", (int) zoneint / 3600);
else
SPRINTF_FIXED(zonestr, "+%02d", -(int) zoneint / 3600);
}
#endif /* TIMEZONE_GLOBAL */
if (st->y < 0)
return snprintf(str, bufsize, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d%s%s BC", -st->y, st->m, st->d, st->hh, st->mm, st->ss, precstr, zonestr);
else
return snprintf(str, bufsize, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d%s%s", st->y, st->m, st->d, st->hh, st->mm, st->ss, precstr, zonestr);
}
static
SQLINTERVAL interval2itype(SQLSMALLINT ctype)
{
SQLINTERVAL sqlitv = 0;
switch (ctype)
{
case SQL_C_INTERVAL_YEAR:
sqlitv = SQL_IS_YEAR;
break;
case SQL_C_INTERVAL_MONTH:
sqlitv = SQL_IS_MONTH;
break;
case SQL_C_INTERVAL_YEAR_TO_MONTH:
sqlitv = SQL_IS_YEAR_TO_MONTH;
break;
case SQL_C_INTERVAL_DAY:
sqlitv = SQL_IS_DAY;
break;
case SQL_C_INTERVAL_HOUR:
sqlitv = SQL_IS_HOUR;
break;
case SQL_C_INTERVAL_DAY_TO_HOUR:
sqlitv = SQL_IS_DAY_TO_HOUR;
break;
case SQL_C_INTERVAL_MINUTE:
sqlitv = SQL_IS_MINUTE;
break;
case SQL_C_INTERVAL_DAY_TO_MINUTE:
sqlitv = SQL_IS_DAY_TO_MINUTE;
break;
case SQL_C_INTERVAL_HOUR_TO_MINUTE:
sqlitv = SQL_IS_HOUR_TO_MINUTE;
break;
case SQL_C_INTERVAL_SECOND:
sqlitv = SQL_IS_SECOND;
break;
case SQL_C_INTERVAL_DAY_TO_SECOND:
sqlitv = SQL_IS_DAY_TO_SECOND;
break;
case SQL_C_INTERVAL_HOUR_TO_SECOND:
sqlitv = SQL_IS_HOUR_TO_SECOND;
break;
case SQL_C_INTERVAL_MINUTE_TO_SECOND:
sqlitv = SQL_IS_MINUTE_TO_SECOND;
break;
}
return sqlitv;
}
/*
* Interval data <-----> SQL_INTERVAL_STRUCT
*/
static int getPrecisionPart(int precision, const char * precPart)
{
char fraction[] = "000000000";
int fracs = sizeof(fraction) - 1;
size_t cpys;
if (precision < 0)
precision = 6; /* default */
if (precision == 0)
return 0;
cpys = strlen(precPart);
if (cpys > fracs)
cpys = fracs;
memcpy(fraction, precPart, cpys);
fraction[precision] = '\0';
return atoi(fraction);
}
static BOOL
interval2istruct(SQLSMALLINT ctype, int precision, const char *str, SQL_INTERVAL_STRUCT *st)
{
char lit1[64], lit2[64];
int scnt, years, mons, days, hours, minutes, seconds;
BOOL sign;
SQLINTERVAL itype = interval2itype(ctype);
memset(st, 0, sizeof(SQL_INTERVAL_STRUCT));
if ((scnt = sscanf(str, "%d-%d", &years, &mons)) >=2)
{
if (SQL_IS_YEAR_TO_MONTH == itype)
{
sign = years < 0 ? SQL_TRUE : SQL_FALSE;
st->interval_type = itype;
st->interval_sign = sign;
st->intval.year_month.year = sign ? (-years) : years;
st->intval.year_month.month = mons;
return TRUE;
}
return FALSE;
}
else if (scnt = sscanf(str, "%d %02d:%02d:%02d.%09s", &days, &hours, &minutes, &seconds, lit2), 5 == scnt || 4 == scnt)
{
sign = days < 0 ? SQL_TRUE : SQL_FALSE;
st->interval_type = itype;
st->interval_sign = sign;
st->intval.day_second.day = sign ? (-days) : days;
st->intval.day_second.hour = hours;
st->intval.day_second.minute = minutes;
st->intval.day_second.second = seconds;
if (scnt > 4)
st->intval.day_second.fraction = getPrecisionPart(precision, lit2);
return TRUE;
}
else if ((scnt = sscanf(str, "%d %10s %d %10s", &years, lit1, &mons, lit2)) >=4)
{
if (strnicmp(lit1, "year", 4) == 0 &&
strnicmp(lit2, "mon", 2) == 0 &&
(SQL_IS_MONTH == itype ||
SQL_IS_YEAR_TO_MONTH == itype))
{
sign = years < 0 ? SQL_TRUE : SQL_FALSE;
st->interval_type = itype;
st->interval_sign = sign;
st->intval.year_month.year = sign ? (-years) : years;
st->intval.year_month.month = sign ? (-mons) : mons;
return TRUE;
}
return FALSE;
}
if ((scnt = sscanf(str, "%d %10s %d", &years, lit1, &days)) == 2)
{
sign = years < 0 ? SQL_TRUE : SQL_FALSE;
if (SQL_IS_YEAR == itype &&
(stricmp(lit1, "year") == 0 ||
stricmp(lit1, "years") == 0))
{
st->interval_type = itype;
st->interval_sign = sign;
st->intval.year_month.year = sign ? (-years) : years;
return TRUE;
}
if (SQL_IS_MONTH == itype &&
(stricmp(lit1, "mon") == 0 ||
stricmp(lit1, "mons") == 0))
{
st->interval_type = itype;
st->interval_sign = sign;
st->intval.year_month.month = sign ? (-years) : years;
return TRUE;
}
if (SQL_IS_DAY == itype &&
(stricmp(lit1, "day") == 0 ||
stricmp(lit1, "days") == 0))
{
st->interval_type = itype;
st->interval_sign = sign;
st->intval.day_second.day = sign ? (-years) : years;
return TRUE;
}
return FALSE;
}
if (itype == SQL_IS_YEAR || itype == SQL_IS_MONTH || itype == SQL_IS_YEAR_TO_MONTH)
{
/* these formats should've been handled above already */
return FALSE;
}
scnt = sscanf(str, "%d %10s %02d:%02d:%02d.%09s", &days, lit1, &hours, &minutes, &seconds, lit2);
if (scnt == 5 || scnt == 6)
{
if (strnicmp(lit1, "day", 3) != 0)
return FALSE;
sign = days < 0 ? SQL_TRUE : SQL_FALSE;
st->interval_type = itype;
st->interval_sign = sign;
st->intval.day_second.day = sign ? (-days) : days;
st->intval.day_second.hour = sign ? (-hours) : hours;
st->intval.day_second.minute = minutes;
st->intval.day_second.second = seconds;
if (scnt > 5)
st->intval.day_second.fraction = getPrecisionPart(precision, lit2);
return TRUE;
}
scnt = sscanf(str, "%02d:%02d:%02d.%09s", &hours, &minutes, &seconds, lit2);
if (scnt == 3 || scnt == 4)
{
sign = hours < 0 ? SQL_TRUE : SQL_FALSE;
st->interval_type = itype;
st->interval_sign = sign;
st->intval.day_second.hour = sign ? (-hours) : hours;
st->intval.day_second.minute = minutes;
st->intval.day_second.second = seconds;
if (scnt > 3)
st->intval.day_second.fraction = getPrecisionPart(precision, lit2);
return TRUE;
}
return FALSE;
}
#ifdef HAVE_LOCALE_H
/*
* Get the decimal point of the current locale.
*
* XXX: This isn't thread-safe, if another thread changes the locale with
* setlocale() concurrently. There are two problems with that:
*
* 1. The pointer returned by localeconv(), or the lc->decimal_point string,
* might be invalidated by calls in other threads. Until someone comes up
* with a thread-safe version of localeconv(), there isn't much we can do
* about that. (libc implementations that return a static buffer (like glibc)
* happen to be safe from the lconv struct being invalidated, but the
* decimal_point string might still not point to a static buffer).
*
* 2. The between the call to sprintf() and get_current_decimal_point(), the
* decimal point might change. That would cause set_server_decimal_point()
* to fail to recognize a decimal separator, and we might send a numeric
* string to the server that the server won't recognize. This would cause
* the query to fail in the server.
*
* XXX: we only take into account the first byte of the decimal separator.
*/
static char get_current_decimal_point(void)
{
struct lconv *lc = localeconv();
return lc->decimal_point[0];
}
/*
* Modify the string representation of a numeric/float value, converting the
* decimal point from '.' to the correct decimal separator of the current
* locale.
*/
static void set_server_decimal_point(char *num, SQLLEN len)
{
char current_decimal_point = get_current_decimal_point();
char *str;
SQLLEN i;
if ('.' == current_decimal_point)
return;
i = 0;
for (str = num; '\0' != *str; str++)
{
if (*str == current_decimal_point)
{
*str = '.';
break;
}
if (len != SQL_NTS && i++ >= len)
break;
}
}
/*
* Inverse of set_server_decimal_point.
*/
static void set_client_decimal_point(char *num)
{
char current_decimal_point = get_current_decimal_point();
char *str;
if ('.' == current_decimal_point)
return;
for (str = num; '\0' != *str; str++)
{
if (*str == '.')
{
*str = current_decimal_point;
break;
}
}
}
#else
static void set_server_decimal_point(char *num) {}
static void set_client_decimal_point(char *num, BOOL) {}
#endif /* HAVE_LOCALE_H */
/* This is called by SQLFetch() */
int
copy_and_convert_field_bindinfo(StatementClass *stmt, OID field_type, int atttypmod, void *value, int col)
{
ARDFields *opts = SC_get_ARDF(stmt);
BindInfoClass *bic;
SQLULEN offset = opts->row_offset_ptr ? *opts->row_offset_ptr : 0;
if (opts->allocated <= col)
extend_column_bindings(opts, col + 1);
bic = &(opts->bindings[col]);
SC_set_current_col(stmt, -1);
return copy_and_convert_field(stmt, field_type, atttypmod, value,
bic->returntype, bic->precision,
(PTR) (bic->buffer + offset), bic->buflen,
LENADDR_SHIFT(bic->used, offset), LENADDR_SHIFT(bic->indicator, offset));
}
/*
* Is 'str' a valid integer literal, consisting only of ASCII characters
* 0-9 ?
*
* Also, *negative is set to TRUE if the value was negative.
*
* We don't check for overflow here. This is just to decide if we need to
* quote the value.
*/
static BOOL
valid_int_literal(const char *str, SQLLEN len, BOOL *negative)
{
SQLLEN i = 0;
/* Check there is a minus sign in front */
if ((len == SQL_NTS || len > 0) && str[0] == '-')
{
i++;
*negative = TRUE;
}
else
*negative = FALSE;
/*
* Must begin with a digit. This also rejects empty strings and '-'.
*/
if (i == len || !(str[i] >= '0' && str[i] <= '9'))
return FALSE;
for (; str[i] && (len == SQL_NTS || i < len); i++)
{
if (!(str[i] >= '0' && str[i] <= '9'))
return FALSE;
}
return TRUE;
}
static double get_double_value(const char *str)
{
if (stricmp(str, NAN_STRING) == 0)
#ifdef NAN
return (double) NAN;
#else
{
double a = .0;
return .0 / a;
}
#endif /* NAN */
else if (stricmp(str, INFINITY_STRING) == 0)
#ifdef INFINITY
return (double) INFINITY;
#else
return (double) (HUGE_VAL * HUGE_VAL);
#endif /* INFINITY */
else if (stricmp(str, MINFINITY_STRING) == 0)
#ifdef INFINITY
return (double) -INFINITY;
#else
return (double) -(HUGE_VAL * HUGE_VAL);
#endif /* INFINITY */
return atof(str);
}
static int char2guid(const char *str, SQLGUID *g)
{
/*
* SQLGUID.Data1 is an "unsigned long" on some platforms, and
* "unsigned int" on others. For format "%08X", it should be an
* "unsigned int", so use a temporary variable for it.
*/
unsigned int Data1;
if (sscanf(str,
"%08X-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX",
&Data1,
&g->Data2, &g->Data3,
&g->Data4[0], &g->Data4[1], &g->Data4[2], &g->Data4[3],
&g->Data4[4], &g->Data4[5], &g->Data4[6], &g->Data4[7]) < 11)
return COPY_GENERAL_ERROR;
g->Data1 = Data1;
return COPY_OK;
}
static int effective_fraction(int fraction, int *width)
{
for (*width = 9; fraction % 10 == 0; (*width)--, fraction /= 10)
;
return fraction;
}
static int
get_terminator_len(SQLSMALLINT fCType)
{
switch (fCType)
{
#ifdef UNICODE_SUPPORT
case SQL_C_WCHAR:
return WCLEN;
#endif /* UNICODE_SUPPORT */
case SQL_C_BINARY:
return 0;
}
/* SQL_C_CHAR or INTERNAL_ASIS_TYPE */
return 1;
}
static SQLLEN
get_adjust_len(SQLSMALLINT fCType, SQLLEN len)
{
switch (fCType)
{
#ifdef UNICODE_SUPPORT
case SQL_C_WCHAR:
return (len / WCLEN) * WCLEN;
#endif /* UNICODE_SUPPORT */
}
return len;
}
#define BYTEA_PROCESS_ESCAPE 1
#define BYTEA_PROCESS_BINARY 2
static int
setup_getdataclass(SQLLEN * const length_return, const char ** const ptr_return,
int *needbuflen_return, GetDataClass * const pgdc, const char *neut_str,
const OID field_type, const SQLSMALLINT fCType,
const SQLLEN cbValueMax, const ConnectionClass * const conn)
{
SQLLEN len = (-2);
const char *ptr = NULL;
int needbuflen = 0;
int result = COPY_OK;
BOOL lf_conv = conn->connInfo.lf_conversion;
int bytea_process_kind = 0;
BOOL already_processed = FALSE;
BOOL changed = FALSE;
int len_for_wcs_term = 0;
#ifdef UNICODE_SUPPORT
char *allocbuf = NULL;
int unicode_count = -1;
BOOL localize_needed = FALSE;
BOOL hybrid = FALSE;
#endif /* UNICODE_SUPPORT */
if (PG_TYPE_BYTEA == field_type)
{
if (SQL_C_BINARY == fCType)
bytea_process_kind = BYTEA_PROCESS_BINARY;
else if (0 == strnicmp(neut_str, "\\x", 2)) /* hex format */
neut_str += 2;
else
bytea_process_kind = BYTEA_PROCESS_ESCAPE;
}
#ifdef UNICODE_SUPPORT
if (0 == bytea_process_kind)
{
if (get_convtype() > 0) /* conversion between the current locale is available */
{
BOOL wcs_debug = conn->connInfo.wcs_debug;
BOOL same_encoding = (conn->ccsc == pg_CS_code(conn->locale_encoding));
BOOL is_utf8 = (UTF8 == conn->ccsc);
switch (field_type)
{
case PG_TYPE_UNKNOWN:
case PG_TYPE_BPCHAR:
case PG_TYPE_VARCHAR:
case PG_TYPE_TEXT:
case PG_TYPE_BPCHARARRAY:
case PG_TYPE_VARCHARARRAY:
case PG_TYPE_TEXTARRAY:
if (SQL_C_CHAR == fCType || SQL_C_BINARY == fCType)
localize_needed = (!same_encoding || wcs_debug);
if (SQL_C_WCHAR == fCType)
hybrid = (!is_utf8 || (same_encoding && wcs_debug));
}
MYLOG(0, "localize=%d hybrid=%d is_utf8=%d same_encoding=%d wcs_debug=%d\n", localize_needed, hybrid, is_utf8, same_encoding, wcs_debug);
}
}
if (fCType == SQL_C_WCHAR)
{
if (BYTEA_PROCESS_ESCAPE == bytea_process_kind)
unicode_count = convert_from_pgbinary(neut_str, NULL, 0) * 2;
else if (hybrid)
{
MYLOG(0, "hybrid estimate\n");
if ((unicode_count = bindcol_hybrid_estimate(neut_str, lf_conv, &allocbuf)) < 0)
{
result = COPY_INVALID_STRING_CONVERSION;
goto cleanup;
}
}
else /* normally */
{
unicode_count = utf8_to_ucs2_lf(neut_str, SQL_NTS, lf_conv, NULL, 0, FALSE);
}
len = WCLEN * unicode_count;
already_processed = changed = TRUE;
}
else if (localize_needed)
{
if ((len = bindcol_localize_estimate(neut_str, lf_conv, &allocbuf)) < 0)
{
result = COPY_INVALID_STRING_CONVERSION;