-
Notifications
You must be signed in to change notification settings - Fork 1
/
pgtypes.c
2124 lines (1865 loc) · 48.6 KB
/
pgtypes.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: pgtypes.c
*
* Description: This module contains routines for getting information
* about the supported Postgres data types. Only the
* function pgtype_to_sqltype() returns an unknown condition.
* All other functions return a suitable default so that
* even data types that are not directly supported can be
* used (it is handled as char data).
*
* Classes: n/a
*
* API functions: none
*
* Comments: See "readme.txt" for copyright and license information.
*--------
*/
#include "pgtypes.h"
#include "dlg_specific.h"
#include "statement.h"
#include "connection.h"
#include "environ.h"
#include "qresult.h"
#define EXPERIMENTAL_CURRENTLY
SQLSMALLINT ansi_to_wtype(const ConnectionClass *self, SQLSMALLINT ansitype)
{
#ifndef UNICODE_SUPPORT
return ansitype;
#else
if (!ALLOW_WCHAR(self))
return ansitype;
switch (ansitype)
{
case SQL_CHAR:
return SQL_WCHAR;
case SQL_VARCHAR:
return SQL_WVARCHAR;
case SQL_LONGVARCHAR:
return SQL_WLONGVARCHAR;
}
return ansitype;
#endif /* UNICODE_SUPPORT */
}
Int4 getCharColumnSize(const StatementClass *stmt, OID type, int col, int handle_unknown_size_as);
/*
* these are the types we support. all of the pgtype_ functions should
* return values for each one of these.
* Even types not directly supported are handled as character types
* so all types should work (points, etc.)
*/
/*
* ALL THESE TYPES ARE NO LONGER REPORTED in SQLGetTypeInfo. Instead, all
* the SQL TYPES are reported and mapped to a corresponding Postgres Type
*/
/*
OID pgtypes_defined[][2] = {
{PG_TYPE_CHAR, 0}
,{PG_TYPE_CHAR2, 0}
,{PG_TYPE_CHAR4, 0}
,{PG_TYPE_CHAR8, 0}
,{PG_TYPE_CHAR16, 0}
,{PG_TYPE_NAME, 0}
,{PG_TYPE_VARCHAR, 0}
,{PG_TYPE_BPCHAR, 0}
,{PG_TYPE_DATE, 0}
,{PG_TYPE_TIME, 0}
,{PG_TYPE_TIME_WITH_TMZONE, 0}
,{PG_TYPE_DATETIME, 0}
,{PG_TYPE_ABSTIME, 0}
,{PG_TYPE_TIMESTAMP_NO_TMZONE, 0}
,{PG_TYPE_TIMESTAMP, 0}
,{PG_TYPE_TEXT, 0}
,{PG_TYPE_INT2, 0}
,{PG_TYPE_INT4, 0}
,{PG_TYPE_FLOAT4, 0}
,{PG_TYPE_FLOAT8, 0}
,{PG_TYPE_OID, 0}
,{PG_TYPE_MONEY, 0}
,{PG_TYPE_BOOL, 0}
,{PG_TYPE_BYTEA, 0}
,{PG_TYPE_NUMERIC, 0}
,{PG_TYPE_XID, 0}
,{PG_TYPE_LO_UNDEFINED, 0}
,{0, 0} };
*/
/* These are NOW the SQL Types reported in SQLGetTypeInfo. */
SQLSMALLINT sqlTypes[] = {
SQL_BIGINT,
/* SQL_BINARY, -- Commented out because VarBinary is more correct. */
SQL_BIT,
SQL_CHAR,
SQL_TYPE_DATE,
SQL_DATE,
SQL_DECIMAL,
SQL_DOUBLE,
SQL_FLOAT,
SQL_INTEGER,
SQL_LONGVARBINARY,
SQL_LONGVARCHAR,
SQL_NUMERIC,
SQL_REAL,
SQL_SMALLINT,
SQL_TYPE_TIME,
SQL_TYPE_TIMESTAMP,
SQL_TIME,
SQL_TIMESTAMP,
SQL_TINYINT,
SQL_VARBINARY,
SQL_VARCHAR,
#ifdef UNICODE_SUPPORT
SQL_WCHAR,
SQL_WVARCHAR,
SQL_WLONGVARCHAR,
#endif /* UNICODE_SUPPORT */
SQL_GUID,
/* AFAIK SQL_INTERVAL types cause troubles in some spplications */
#ifdef PG_INTERVAL_AS_SQL_INTERVAL
SQL_INTERVAL_MONTH,
SQL_INTERVAL_YEAR,
SQL_INTERVAL_YEAR_TO_MONTH,
SQL_INTERVAL_DAY,
SQL_INTERVAL_HOUR,
SQL_INTERVAL_MINUTE,
SQL_INTERVAL_SECOND,
SQL_INTERVAL_DAY_TO_HOUR,
SQL_INTERVAL_DAY_TO_MINUTE,
SQL_INTERVAL_DAY_TO_SECOND,
SQL_INTERVAL_HOUR_TO_MINUTE,
SQL_INTERVAL_HOUR_TO_SECOND,
SQL_INTERVAL_MINUTE_TO_SECOND,
#endif /* PG_INTERVAL_AS_SQL_INTERVAL */
0
};
#ifdef ODBCINT64
#define ALLOWED_C_BIGINT SQL_C_SBIGINT
/* #define ALLOWED_C_BIGINT SQL_C_CHAR */ /* Delphi should be either ? */
#else
#define ALLOWED_C_BIGINT SQL_C_CHAR
#endif
OID
pg_true_type(const ConnectionClass *conn, OID type, OID basetype)
{
if (0 == basetype)
return type;
else if (0 == type)
return basetype;
else if (type == conn->lobj_type)
return type;
return basetype;
}
#define MONTH_BIT (1 << 17)
#define YEAR_BIT (1 << 18)
#define DAY_BIT (1 << 19)
#define HOUR_BIT (1 << 26)
#define MINUTE_BIT (1 << 27)
#define SECOND_BIT (1 << 28)
static SQLSMALLINT
get_interval_type(Int4 atttypmod, const char **name)
{
MYLOG(0, "entering atttypmod=%x\n", atttypmod);
if ((-1) == atttypmod)
return 0;
if (0 != (YEAR_BIT & atttypmod))
{
if (0 != (MONTH_BIT & atttypmod))
{
if (name)
*name = "interval year to month";
return SQL_INTERVAL_YEAR_TO_MONTH;
}
if (name)
*name = "interval year";
return SQL_INTERVAL_YEAR;
}
else if (0 != (MONTH_BIT & atttypmod))
{
if (name)
*name = "interval month";
return SQL_INTERVAL_MONTH;
}
else if (0 != (DAY_BIT & atttypmod))
{
if (0 != (SECOND_BIT & atttypmod))
{
if (name)
*name = "interval day to second";
return SQL_INTERVAL_DAY_TO_SECOND;
}
else if (0 != (MINUTE_BIT & atttypmod))
{
if (name)
*name = "interval day to minute";
return SQL_INTERVAL_DAY_TO_MINUTE;
}
else if (0 != (HOUR_BIT & atttypmod))
{
if (name)
*name = "interval day to hour";
return SQL_INTERVAL_DAY_TO_HOUR;
}
if (name)
*name = "interval day";
return SQL_INTERVAL_DAY;
}
else if (0 != (HOUR_BIT & atttypmod))
{
if (0 != (SECOND_BIT & atttypmod))
{
if (name)
*name = "interval hour to second";
return SQL_INTERVAL_HOUR_TO_SECOND;
}
else if (0 != (MINUTE_BIT & atttypmod))
{
if (name)
*name = "interval hour to minute";
return SQL_INTERVAL_HOUR_TO_MINUTE;
}
if (name)
*name = "interval hour";
return SQL_INTERVAL_HOUR;
}
else if (0 != (MINUTE_BIT & atttypmod))
{
if (0 != (SECOND_BIT & atttypmod))
{
if (name)
*name = "interval minute to second";
return SQL_INTERVAL_MINUTE_TO_SECOND;
}
if (name)
*name = "interval minute";
return SQL_INTERVAL_MINUTE;
}
else if (0 != (SECOND_BIT & atttypmod))
{
if (name)
*name = "interval second";
return SQL_INTERVAL_SECOND;
}
if (name)
*name = "interval";
return 0;
}
static Int4
getCharColumnSizeX(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longestlen, int handle_unknown_size_as)
{
int p = -1, maxsize;
const ConnInfo *ci = &(conn->connInfo);
MYLOG(0, "entering type=%d, atttypmod=%d, adtsize_or=%d, unknown = %d\n", type, atttypmod, adtsize_or_longestlen, handle_unknown_size_as);
/* Assign Maximum size based on parameters */
switch (type)
{
case PG_TYPE_TEXT:
if (ci->drivers.text_as_longvarchar)
maxsize = ci->drivers.max_longvarchar_size;
else
maxsize = ci->drivers.max_varchar_size;
break;
case PG_TYPE_VARCHAR:
case PG_TYPE_BPCHAR:
maxsize = ci->drivers.max_varchar_size;
break;
default:
if (ci->drivers.unknowns_as_longvarchar)
maxsize = ci->drivers.max_longvarchar_size;
else
maxsize = ci->drivers.max_varchar_size;
break;
}
#ifdef UNICODE_SUPPORT
if (CC_is_in_unicode_driver(conn) &&
isSqlServr() &&
maxsize > 4000)
maxsize = 4000;
#endif /* UNICODE_SUPPORT */
if (maxsize == TEXT_FIELD_SIZE + 1) /* magic length for testing */
maxsize = 0;
/*
* Static ColumnSize (i.e., the Maximum ColumnSize of the datatype) This
* has nothing to do with a result set.
*/
MYLOG(DETAIL_LOG_LEVEL, "!!! atttypmod < 0 ?\n");
if (atttypmod < 0 && adtsize_or_longestlen < 0)
return maxsize;
MYLOG(DETAIL_LOG_LEVEL, "!!! adtsize_or_logngest=%d\n", adtsize_or_longestlen);
p = adtsize_or_longestlen; /* longest */
/*
* Catalog Result Sets -- use assigned column width (i.e., from
* set_tuplefield_string)
*/
MYLOG(DETAIL_LOG_LEVEL, "!!! catalog_result=%d\n", handle_unknown_size_as);
if (UNKNOWNS_AS_LONGEST == handle_unknown_size_as)
{
MYLOG(0, "LONGEST: p = %d\n", p);
if (p > 0 &&
(atttypmod < 0 || atttypmod > p))
return p;
}
if (TYPE_MAY_BE_ARRAY(type))
{
if (p > 0)
return p;
return maxsize;
}
/* Size is unknown -- handle according to parameter */
if (atttypmod > 0) /* maybe the length is known */
{
return atttypmod;
}
/* The type is really unknown */
switch (handle_unknown_size_as)
{
case UNKNOWNS_AS_DONTKNOW:
return -1;
case UNKNOWNS_AS_LONGEST:
case UNKNOWNS_AS_MAX:
break;
default:
return -1;
}
if (maxsize <= 0)
return maxsize;
switch (type)
{
case PG_TYPE_BPCHAR:
case PG_TYPE_VARCHAR:
case PG_TYPE_TEXT:
return maxsize;
}
if (p > maxsize)
maxsize = p;
return maxsize;
}
/*
* Specify when handle_unknown_size_as parameter is unused
*/
#define UNUSED_HANDLE_UNKNOWN_SIZE_AS (-2)
static SQLSMALLINT
getNumericDecimalDigitsX(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longest, int UNUSED_handle_unknown_size_as)
{
Int4 default_decimal_digits = 6;
MYLOG(0, "entering type=%d, atttypmod=%d\n", type, atttypmod);
if (atttypmod < 0 && adtsize_or_longest < 0)
return default_decimal_digits;
if (atttypmod > -1)
return (atttypmod & 0xffff);
if (adtsize_or_longest <= 0)
return default_decimal_digits;
adtsize_or_longest >>= 16; /* extract the scale part */
return adtsize_or_longest;
}
static Int4 /* PostgreSQL restritiction */
getNumericColumnSizeX(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longest, int handle_unknown_size_as)
{
Int4 default_column_size = 28;
const ConnInfo *ci = &(conn->connInfo);
MYLOG(0, "entering type=%d, typmod=%d\n", type, atttypmod);
if (atttypmod > -1)
return (atttypmod >> 16) & 0xffff;
switch (ci->numeric_as)
{
case SQL_VARCHAR:
return ci->drivers.max_varchar_size;
case SQL_LONGVARCHAR:
return ci->drivers.max_longvarchar_size;
case SQL_DOUBLE:
return PG_DOUBLE_DIGITS;
}
switch (handle_unknown_size_as)
{
case UNKNOWNS_AS_DONTKNOW:
return SQL_NO_TOTAL;
}
if (adtsize_or_longest <= 0)
return default_column_size;
adtsize_or_longest %= (1 << 16); /* extract the precision part */
switch (handle_unknown_size_as)
{
case UNKNOWNS_AS_MAX:
return adtsize_or_longest > default_column_size ? adtsize_or_longest : default_column_size;
default:
if (adtsize_or_longest < 10)
adtsize_or_longest = 10;
}
return adtsize_or_longest;
}
static SQLSMALLINT
getTimestampDecimalDigitsX(const ConnectionClass *conn, OID type, int atttypmod)
{
MYLOG(0, "type=%d, atttypmod=%d\n", type, atttypmod);
return (atttypmod > -1 ? atttypmod : 6);
}
static SQLSMALLINT
getTimestampColumnSizeX(const ConnectionClass *conn, OID type, int atttypmod)
{
Int4 fixed, scale;
MYLOG(0, "entering type=%d, atttypmod=%d\n", type, atttypmod);
switch (type)
{
case PG_TYPE_TIME:
fixed = 8;
break;
case PG_TYPE_TIME_WITH_TMZONE:
fixed = 11;
break;
case PG_TYPE_TIMESTAMP_NO_TMZONE:
fixed = 19;
break;
default:
if (USE_ZONE)
fixed = 22;
else
fixed = 19;
break;
}
scale = getTimestampDecimalDigitsX(conn, type, atttypmod);
return (scale > 0) ? fixed + 1 + scale : fixed;
}
static SQLSMALLINT
getIntervalDecimalDigits(OID type, int atttypmod)
{
Int4 prec;
MYLOG(0, "entering type=%d, atttypmod=%d\n", type, atttypmod);
if ((atttypmod & SECOND_BIT) == 0)
return 0;
return (prec = atttypmod & 0xffff) == 0xffff ? 6 : prec ;
}
static SQLSMALLINT
getIntervalColumnSize(OID type, int atttypmod)
{
Int4 ttl, leading_precision = 9, scale;
MYLOG(0, "entering type=%d, atttypmod=%d\n", type, atttypmod);
ttl = leading_precision;
switch (get_interval_type(atttypmod, NULL))
{
case 0:
ttl = 25;
break;
case SQL_INTERVAL_YEAR:
ttl = 16;
break;
case SQL_INTERVAL_MONTH:
ttl = 16;
break;
case SQL_INTERVAL_DAY:
ttl = 16;
break;
case SQL_INTERVAL_DAY_TO_SECOND:
case SQL_INTERVAL_DAY_TO_MINUTE:
case SQL_INTERVAL_DAY_TO_HOUR:
ttl = 25;
break;
case SQL_INTERVAL_HOUR_TO_SECOND:
case SQL_INTERVAL_HOUR_TO_MINUTE:
case SQL_INTERVAL_HOUR:
ttl = 17;
break;
case SQL_INTERVAL_MINUTE_TO_SECOND:
case SQL_INTERVAL_MINUTE:
ttl = 15;
break;
case SQL_INTERVAL_YEAR_TO_MONTH:
ttl = 24;
break;
}
scale = getIntervalDecimalDigits(type, atttypmod);
return (scale > 0) ? ttl + 1 + scale : ttl;
}
SQLSMALLINT
pgtype_attr_to_concise_type(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longestlen, int handle_unknown_size_as)
{
const ConnInfo *ci = &(conn->connInfo);
EnvironmentClass *env = (EnvironmentClass *) CC_get_env(conn);
#ifdef PG_INTERVAL_AS_SQL_INTERVAL
SQLSMALLINT sqltype;
#endif /* PG_INTERVAL_AS_SQL_INTERVAL */
BOOL bLongVarchar, bFixed = FALSE;
switch (type)
{
case PG_TYPE_CHAR:
return ansi_to_wtype(conn, SQL_CHAR);
case PG_TYPE_NAME:
case PG_TYPE_REFCURSOR:
return ansi_to_wtype(conn, SQL_VARCHAR);
case PG_TYPE_BPCHAR:
bFixed = TRUE;
case PG_TYPE_VARCHAR:
if (getCharColumnSizeX(conn, type, atttypmod, adtsize_or_longestlen, handle_unknown_size_as) > ci->drivers.max_varchar_size)
bLongVarchar = TRUE;
else
bLongVarchar = FALSE;
return ansi_to_wtype(conn, bLongVarchar ? SQL_LONGVARCHAR : (bFixed ? SQL_CHAR : SQL_VARCHAR));
case PG_TYPE_TEXT:
bLongVarchar = ci->drivers.text_as_longvarchar;
if (bLongVarchar)
{
int column_size = getCharColumnSizeX(conn, type, atttypmod, adtsize_or_longestlen, handle_unknown_size_as);
if (column_size > 0 &&
column_size <= ci->drivers.max_varchar_size)
bLongVarchar = FALSE;
}
return ansi_to_wtype(conn, bLongVarchar ? SQL_LONGVARCHAR : SQL_VARCHAR);
case PG_TYPE_BYTEA:
if (ci->bytea_as_longvarbinary)
return SQL_LONGVARBINARY;
else
return SQL_VARBINARY;
case PG_TYPE_LO_UNDEFINED:
return SQL_LONGVARBINARY;
case PG_TYPE_INT2:
return SQL_SMALLINT;
case PG_TYPE_OID:
case PG_TYPE_XID:
case PG_TYPE_INT4:
return SQL_INTEGER;
/* Change this to SQL_BIGINT for ODBC v3 bjm 2001-01-23 */
case PG_TYPE_INT8:
if (ci->int8_as != 0)
return ci->int8_as;
if (conn->ms_jet)
return SQL_NUMERIC; /* maybe a little better than SQL_VARCHAR */
return SQL_BIGINT;
case PG_TYPE_NUMERIC:
if (-1 == atttypmod && DEFAULT_NUMERIC_AS != ci->numeric_as)
return ci->numeric_as;
return SQL_NUMERIC;
case PG_TYPE_FLOAT4:
return SQL_REAL;
case PG_TYPE_FLOAT8:
return SQL_FLOAT;
case PG_TYPE_DATE:
if (EN_is_odbc3(env))
return SQL_TYPE_DATE;
return SQL_DATE;
case PG_TYPE_TIME:
if (EN_is_odbc3(env))
return SQL_TYPE_TIME;
return SQL_TIME;
case PG_TYPE_ABSTIME:
case PG_TYPE_DATETIME:
case PG_TYPE_TIMESTAMP_NO_TMZONE:
case PG_TYPE_TIMESTAMP:
if (EN_is_odbc3(env))
return SQL_TYPE_TIMESTAMP;
return SQL_TIMESTAMP;
case PG_TYPE_MONEY:
return SQL_FLOAT;
case PG_TYPE_BOOL:
return ci->drivers.bools_as_char ? SQL_VARCHAR : SQL_BIT;
case PG_TYPE_XML:
return ansi_to_wtype(conn, SQL_LONGVARCHAR);
case PG_TYPE_INET:
case PG_TYPE_CIDR:
case PG_TYPE_MACADDR:
return ansi_to_wtype(conn, SQL_VARCHAR);
case PG_TYPE_UUID:
return SQL_GUID;
case PG_TYPE_INTERVAL:
#ifdef PG_INTERVAL_AS_SQL_INTERVAL
if (sqltype = get_interval_type(atttypmod, NULL), 0 != sqltype)
return sqltype;
#endif /* PG_INTERVAL_AS_SQL_INTERVAL */
return ansi_to_wtype(conn, SQL_VARCHAR);
case PG_TYPE_BIT:
if (1 == atttypmod)
return SQL_BIT;
else
return SQL_VARCHAR;
default:
/*
* first, check to see if 'type' is in list. If not, look up
* with query. Add oid, name to list. If it's already in
* list, just return.
*/
/* hack until permanent type is available */
if (type == conn->lobj_type)
return SQL_LONGVARBINARY;
bLongVarchar = ci->drivers.unknowns_as_longvarchar;
if (bLongVarchar)
{
int column_size = getCharColumnSizeX(conn, type, atttypmod, adtsize_or_longestlen, handle_unknown_size_as);
if (column_size > 0 &&
column_size <= ci->drivers.max_varchar_size)
bLongVarchar = FALSE;
}
#ifdef EXPERIMENTAL_CURRENTLY
return ansi_to_wtype(conn, bLongVarchar ? SQL_LONGVARCHAR : SQL_VARCHAR);
#endif /* EXPERIMENTAL_CURRENTLY */
return bLongVarchar ? SQL_LONGVARCHAR : SQL_VARCHAR;
}
}
SQLSMALLINT
pgtype_attr_to_sqldesctype(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longestlen, int handle_unknown_size_as)
{
SQLSMALLINT rettype;
#ifdef PG_INTERVAL_AS_SQL_INTERVAL
if (PG_TYPE_INTERVAL == type)
return SQL_INTERVAL;
#endif /* PG_INTERVAL_AS_SQL_INTERVAL */
switch (rettype = pgtype_attr_to_concise_type(conn, type, atttypmod, adtsize_or_longestlen, handle_unknown_size_as))
{
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
case SQL_TYPE_TIMESTAMP:
return SQL_DATETIME;
}
return rettype;
}
SQLSMALLINT
pgtype_attr_to_datetime_sub(const ConnectionClass *conn, OID type, int atttypmod)
{
SQLSMALLINT rettype;
switch (rettype = pgtype_attr_to_concise_type(conn, type, atttypmod, PG_ADT_UNSET, PG_UNKNOWNS_UNSET))
{
case SQL_TYPE_DATE:
return SQL_CODE_DATE;
case SQL_TYPE_TIME:
return SQL_CODE_TIME;
case SQL_TYPE_TIMESTAMP:
return SQL_CODE_TIMESTAMP;
case SQL_INTERVAL_MONTH:
case SQL_INTERVAL_YEAR:
case SQL_INTERVAL_YEAR_TO_MONTH:
case SQL_INTERVAL_DAY:
case SQL_INTERVAL_HOUR:
case SQL_INTERVAL_MINUTE:
case SQL_INTERVAL_SECOND:
case SQL_INTERVAL_DAY_TO_HOUR:
case SQL_INTERVAL_DAY_TO_MINUTE:
case SQL_INTERVAL_DAY_TO_SECOND:
case SQL_INTERVAL_HOUR_TO_MINUTE:
case SQL_INTERVAL_HOUR_TO_SECOND:
case SQL_INTERVAL_MINUTE_TO_SECOND:
return rettype - 100;
}
return -1;
}
SQLSMALLINT
pgtype_attr_to_ctype(const ConnectionClass *conn, OID type, int atttypmod)
{
const ConnInfo *ci = &(conn->connInfo);
EnvironmentClass *env = (EnvironmentClass *) CC_get_env(conn);
#ifdef PG_INTERVAL_AS_SQL_INTERVAL
SQLSMALLINT ctype;
#endif /* PG_INTERVAL_A_SQL_INTERVAL */
switch (type)
{
case PG_TYPE_INT8:
if (!conn->ms_jet)
return ALLOWED_C_BIGINT;
return SQL_C_CHAR;
case PG_TYPE_NUMERIC:
return SQL_C_CHAR;
case PG_TYPE_INT2:
return SQL_C_SSHORT;
case PG_TYPE_OID:
case PG_TYPE_XID:
return SQL_C_ULONG;
case PG_TYPE_INT4:
return SQL_C_SLONG;
case PG_TYPE_FLOAT4:
return SQL_C_FLOAT;
case PG_TYPE_FLOAT8:
return SQL_C_DOUBLE;
case PG_TYPE_DATE:
if (EN_is_odbc3(env))
return SQL_C_TYPE_DATE;
return SQL_C_DATE;
case PG_TYPE_TIME:
if (EN_is_odbc3(env))
return SQL_C_TYPE_TIME;
return SQL_C_TIME;
case PG_TYPE_ABSTIME:
case PG_TYPE_DATETIME:
case PG_TYPE_TIMESTAMP_NO_TMZONE:
case PG_TYPE_TIMESTAMP:
if (EN_is_odbc3(env))
return SQL_C_TYPE_TIMESTAMP;
return SQL_C_TIMESTAMP;
case PG_TYPE_MONEY:
return SQL_C_FLOAT;
case PG_TYPE_BOOL:
return ci->drivers.bools_as_char ? SQL_C_CHAR : SQL_C_BIT;
case PG_TYPE_BYTEA:
return SQL_C_BINARY;
case PG_TYPE_LO_UNDEFINED:
return SQL_C_BINARY;
case PG_TYPE_BPCHAR:
case PG_TYPE_VARCHAR:
case PG_TYPE_TEXT:
return ansi_to_wtype(conn, SQL_C_CHAR);
case PG_TYPE_UUID:
if (!conn->ms_jet)
return SQL_C_GUID;
return ansi_to_wtype(conn, SQL_C_CHAR);
case PG_TYPE_INTERVAL:
#ifdef PG_INTERVAL_AS_SQL_INTERVAL
if (ctype = get_interval_type(atttypmod, NULL), 0 != ctype)
return ctype;
#endif /* PG_INTERVAL_AS_SQL_INTERVAL */
return ansi_to_wtype(conn, SQL_CHAR);
default:
/* hack until permanent type is available */
if (type == conn->lobj_type)
return SQL_C_BINARY;
/* Experimental, Does this work ? */
#ifdef EXPERIMENTAL_CURRENTLY
return ansi_to_wtype(conn, SQL_C_CHAR);
#endif /* EXPERIMENTAL_CURRENTLY */
return SQL_C_CHAR;
}
}
const char *
pgtype_attr_to_name(const ConnectionClass *conn, OID type, int atttypmod, BOOL auto_increment)
{
const char *tname = NULL;
switch (type)
{
case PG_TYPE_CHAR:
return "char";
case PG_TYPE_INT8:
return auto_increment ? "bigserial" : "int8";
case PG_TYPE_NUMERIC:
return "numeric";
case PG_TYPE_VARCHAR:
return "varchar";
case PG_TYPE_BPCHAR:
return "char";
case PG_TYPE_TEXT:
return "text";
case PG_TYPE_NAME:
return "name";
case PG_TYPE_REFCURSOR:
return "refcursor";
case PG_TYPE_INT2:
return "int2";
case PG_TYPE_OID:
return OID_NAME;
case PG_TYPE_XID:
return "xid";
case PG_TYPE_INT4:
MYLOG(DETAIL_LOG_LEVEL, "pgtype_to_name int4\n");
return auto_increment ? "serial" : "int4";
case PG_TYPE_FLOAT4:
return "float4";
case PG_TYPE_FLOAT8:
return "float8";
case PG_TYPE_DATE:
return "date";
case PG_TYPE_TIME:
return "time";
case PG_TYPE_ABSTIME:
return "abstime";
case PG_TYPE_DATETIME:
return "timestamptz";
case PG_TYPE_TIMESTAMP_NO_TMZONE:
return "timestamp without time zone";
case PG_TYPE_TIMESTAMP:
return "timestamp";
case PG_TYPE_MONEY:
return "money";
case PG_TYPE_BOOL:
return "bool";
case PG_TYPE_BYTEA:
return "bytea";
case PG_TYPE_XML:
return "xml";
case PG_TYPE_MACADDR:
return "macaddr";
case PG_TYPE_INET:
return "inet";
case PG_TYPE_CIDR:
return "cidr";
case PG_TYPE_UUID:
return "uuid";
case PG_TYPE_VOID:
return "void";
case PG_TYPE_INT2VECTOR:
return "int2vector";
case PG_TYPE_OIDVECTOR:
return "oidvector";
case PG_TYPE_ANY:
return "any";
case PG_TYPE_INTERVAL:
get_interval_type(atttypmod, &tname);
return tname;
case PG_TYPE_LO_UNDEFINED:
return PG_TYPE_LO_NAME;
default:
/* hack until permanent type is available */
if (type == conn->lobj_type)
return PG_TYPE_LO_NAME;
/*
* "unknown" can actually be used in alter table because it is
* a real PG type!
*/
return "unknown";
}
}
Int4 /* PostgreSQL restriction */
pgtype_attr_column_size(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longest, int handle_unknown_size_as)
{
const ConnInfo *ci = &(conn->connInfo);
MYLOG(0, "entering type=%d, atttypmod=%d, adtsize_or=%d, unknown = %d\n", type, atttypmod, adtsize_or_longest, handle_unknown_size_as);
switch (type)
{
case PG_TYPE_CHAR:
return 1;
case PG_TYPE_NAME:
case PG_TYPE_REFCURSOR:
{
int value = 0;
if (PG_VERSION_GT(conn, 7.4))
{
/*
* 'conn' argument is marked as const,
* because this function just reads
* stuff from the already-filled in
* fields in the struct. But this is
* an exception: CC_get_max_idlen() can
* send a SHOW query to the backend to
* get the identifier length. Thus cast
* away the const here.
*/
value = CC_get_max_idlen((ConnectionClass *) conn);
}
#ifdef NAME_FIELD_SIZE
else
value = NAME_FIELD_SIZE;
#endif /* NAME_FIELD_SIZE */
if (0 == value)
value = NAMEDATALEN_V73;
return value;
}
case PG_TYPE_INT2:
return 5;
case PG_TYPE_OID:
case PG_TYPE_XID:
case PG_TYPE_INT4:
return 10;
case PG_TYPE_INT8:
return 19; /* signed */
case PG_TYPE_NUMERIC:
return getNumericColumnSizeX(conn, type, atttypmod, adtsize_or_longest, handle_unknown_size_as);
case PG_TYPE_MONEY:
return 10;
case PG_TYPE_FLOAT4:
return PG_REAL_DIGITS;
case PG_TYPE_FLOAT8:
return PG_DOUBLE_DIGITS;
case PG_TYPE_DATE:
return 10;
case PG_TYPE_TIME:
return 8;
case PG_TYPE_ABSTIME:
case PG_TYPE_TIMESTAMP:
return 22;
case PG_TYPE_DATETIME:
case PG_TYPE_TIMESTAMP_NO_TMZONE:
/* return 22; */
return getTimestampColumnSizeX(conn, type, atttypmod);
case PG_TYPE_BOOL:
return ci->drivers.bools_as_char ? PG_WIDTH_OF_BOOLS_AS_CHAR : 1;
case PG_TYPE_MACADDR:
return 17;
case PG_TYPE_INET:
case PG_TYPE_CIDR:
return sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128");
case PG_TYPE_UUID:
return sizeof("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
case PG_TYPE_LO_UNDEFINED:
return SQL_NO_TOTAL;
case PG_TYPE_INTERVAL:
return getIntervalColumnSize(type, atttypmod);
default:
if (type == conn->lobj_type) /* hack until permanent
* type is available */
return SQL_NO_TOTAL;
if (PG_TYPE_BYTEA == type && ci->bytea_as_longvarbinary)
return SQL_NO_TOTAL;
/* Handle Character types and unknown types */
return getCharColumnSizeX(conn, type, atttypmod, adtsize_or_longest, handle_unknown_size_as);
}
}
SQLSMALLINT
pgtype_attr_precision(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longest, int handle_unknown_size_as)
{
switch (type)
{
case PG_TYPE_NUMERIC:
return getNumericColumnSizeX(conn, type, atttypmod, adtsize_or_longest, handle_unknown_size_as);
case PG_TYPE_TIME:
case PG_TYPE_DATETIME:
case PG_TYPE_TIMESTAMP_NO_TMZONE:
return getTimestampDecimalDigitsX(conn, type, atttypmod);
#ifdef PG_INTERVAL_AS_SQL_INTERVAL
case PG_TYPE_INTERVAL:
return getIntervalDecimalDigits(type, atttypmod);
#endif /* PG_INTERVAL_AS_SQL_INTERVAL */
}
return -1;
}
Int4
pgtype_attr_display_size(const ConnectionClass *conn, OID type, int atttypmod, int adtsize_or_longestlen, int handle_unknown_size_as)