forked from credativ/informix_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifx_conv.c
3333 lines (2972 loc) · 78.9 KB
/
ifx_conv.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
/*-------------------------------------------------------------------------
*
* ifx_conv.c
* Datatype conversion routines and helper functions.
*
* Be cautious about memory allocations outside
* our memory context. Informix ESQL/C APIs allocate memory
* under the hood of our PostgreSQL memory contexts. You *must*
* call ifxRewindCallstack before re-throwing any PostgreSQL
* elog's, otherwise you likely leak memory.
*
* Copyright (c) 2012, credativ GmbH
*
* IDENTIFICATION
* informix_fdw/ifx_conv.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#if PG_VERSION_NUM >= 90300
#include "access/htup_details.h"
#endif
#include "catalog/pg_cast.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_type.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "nodes/nodeFuncs.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "utils/builtins.h"
#include "utils/formatting.h"
#include "utils/lsyscache.h"
#include "utils/numeric.h"
#if PG_VERSION_NUM >= 90500
#include "utils/ruleutils.h"
#endif
#include "utils/syscache.h"
#include "ifx_fdw.h"
#include "ifx_node_utils.h"
/*******************************************************************************
* Helper functions
*/
static regproc getTypeCastFunction(IfxFdwExecutionState *state,
Oid sourceOid, Oid targetOid);
static regproc getTypeInputFunction(IfxFdwExecutionState *state,
Oid inputOid);
static void
deparse_predicate_node(IfxPushdownOprContext *context,
IfxPushdownOprInfo *info);
static char *getIfxOperatorIdent(IfxPushdownOprInfo *pushdownInfo);
static char * getConstValue(Const *constNode);
static void rewriteInExprContext(Const *arrayConst,
IfxPushdownInOprContext *cxt);
void deparse_node_list_for_InExpr(IfxPushdownOprContext *context,
IfxPushdownInOprContext *in_cxt,
IfxPushdownOprInfo *info);
static regproc getTypeOutputFunction(Oid inputOid);
#if PG_VERSION_NUM >= 90300
static inline char *interval_to_cstring(IfxFdwExecutionState *state,
TupleTableSlot *slot,
int attnum,
IfxFormatMode mode);
#endif
/*******************************************************************************
* Implementation starts here
*/
/*
* convertIfxDateString()
*
* Converts an informix formatted date string into a PostgreSQL
* DATE datum. Conversion is supported to
*
* DATE
* TEXT
* VARCHAR
* BPCHAR
*/
Datum convertIfxDateString(IfxFdwExecutionState *state, int attnum)
{
Datum result;
Oid inputOid;
char *val;
regproc typeinputfunc;
/*
* Init...
*/
result = PointerGetDatum(NULL);
switch(PG_ATTRTYPE_P(state, attnum))
{
case TEXTOID:
case VARCHAROID:
case BPCHAROID:
case DATEOID:
inputOid = PG_ATTRTYPE_P(state, attnum);
break;
default:
{
/* oops, unexpected datum conversion */
IFX_ATTR_SETNOTVALID_P(state, attnum);
return result;
}
}
val = (char *)palloc0(IFX_DATE_BUFFER_LEN);
if (ifxGetDateAsString(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum), val) == NULL)
{
/*
* Got a SQL null value or conversion error. Leave it up to
* the caller to look what's wrong (at least, we can't error
* out at this place, since the caller need's the chance to
* clean up itself).
*/
return result;
}
/*
* Catch errors from subsequent function calls...
*/
PG_TRY();
{
/*
* Try the conversion.
*/
typeinputfunc = getTypeInputFunction(state, inputOid);
result = OidFunctionCall3(typeinputfunc,
CStringGetDatum(val),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(PG_ATTRTYPEMOD_P(state, attnum)));
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
return result;
}
/*
* convertIfxInterval()
*
* Converts an Informix Interval data value into a PostgreSQL datum.
*
* Conversion is supported to
* TEXT
* BPCHAR
* VARCHAR
* INTERVAL
*/
Datum convertIfxInterval(IfxFdwExecutionState *state, int attnum)
{
Datum result;
char *val;
Oid inputOid;
regproc typeinputfunc;
result = PointerGetDatum(NULL);
switch (PG_ATTRTYPE_P(state, attnum))
{
case TEXTOID:
case VARCHAROID:
case BPCHAROID:
case INTERVALOID:
inputOid = PG_ATTRTYPE_P(state, attnum);
break;
default:
{
/* oops, unexpected datum conversion */
IFX_ATTR_SETNOTVALID_P(state, attnum);
return result;
}
}
/*
* We get the Informix INTERVAL value as a formatted character string.
* Prepare a buffer for it and call the appropiate conversion function
* from our Informix API...
*/
val = (char *) palloc0(IFX_DATETIME_BUFFER_LEN);
if (ifxGetIntervalAsString(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum), val) == NULL)
{
/*
* Got a SQL null value or conversion error. Leave it up to the
* caller to look what's wrong. We can't error out at this stage,
* since we need to give the caller a chance to clean up itself.
*/
return result;
}
PG_TRY();
{
/*
* Call the target type input function, but keep an eye on
* possible failing conversions. The caller needs to be informed
* somehting went wrong...
*/
typeinputfunc = getTypeInputFunction(state, inputOid);
result = OidFunctionCall3(typeinputfunc,
CStringGetDatum(val),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(PG_ATTRTYPEMOD_P(state, attnum)));
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
/* ...and we're done */
return result;
}
/*
* convertIfxTimestamp()
*
* Converts a given Informix DATETIME value into
* a PostgreSQL timestamp.
*/
Datum convertIfxTimestampString(IfxFdwExecutionState *state, int attnum)
{
Datum result;
Oid inputOid;
char *val;
regproc typeinputfunc;
/*
* Init ...
*/
result = PointerGetDatum(NULL);
switch (PG_ATTRTYPE_P(state, attnum))
{
case TEXTOID:
case VARCHAROID:
case BPCHAROID:
case TIMEOID:
case DATEOID:
case TIMESTAMPOID:
case TIMESTAMPTZOID:
inputOid = PG_ATTRTYPE_P(state, attnum);
break;
default:
{
/* oops, unexpected datum conversion */
IFX_ATTR_SETNOTVALID_P(state, attnum);
return result;
}
}
/*
* We get the Informix DTIME value as a ANSI SQL
* formatted character string. Prepare a buffer for it
* and call the appropiate conversion function from our
* Informix API...
*/
val = (char *) palloc0(IFX_DATETIME_BUFFER_LEN);
if (ifxGetTimestampAsString(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum), val) == NULL)
{
/*
* Got a SQL null value or conversion error. Leave it up to
* the caller to look what's wrong (at least, we can't error
* out at this place, since the caller need's the chance to
* clean up itself).
*/
return result;
}
PG_TRY();
{
/*
* Get the input function and try the conversion. We just pass
* the character string into the specific type input function.
*/
typeinputfunc = getTypeInputFunction(state, inputOid);
result = OidFunctionCall3(typeinputfunc,
CStringGetDatum(val),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(PG_ATTRTYPEMOD_P(state, attnum)));
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
return result;
}
/*
* convertIfxFloat()
*
* Converts a float value into a PostgreSQL numeric or float value.
*/
Datum convertIfxFloat(IfxFdwExecutionState *state, int attnum)
{
Datum result;
regproc typinputfunc;
result = PointerGetDatum(NULL);
switch(PG_ATTRTYPE_P(state, attnum))
{
case FLOAT4OID:
case FLOAT8OID:
case NUMERICOID:
{
char *val;
val = (char *) palloc0(IFX_MAX_FLOAT_DIGITS + 1);
/*
* Call must handle NULL column value or invalid data conversion.
*/
if (ifxGetFloatAsString(&state->stmt_info,
PG_MAPPED_IFX_ATTNUM(state, attnum),
val) == NULL)
{
/* caller should handle indicator */
break;
}
PG_TRY();
{
typinputfunc = getTypeInputFunction(state,
PG_ATTRTYPE_P(state, attnum));
/*
* Convert float text representation into target type.
*
* If the source type is a NUMERICOID datum, we have to
* take care to apply its typmods...
*/
if (PG_ATTRTYPE_P(state, attnum) != NUMERICOID)
{
result = OidFunctionCall2(typinputfunc,
CStringGetDatum(val),
ObjectIdGetDatum(InvalidOid));
}
else
{
result = OidFunctionCall3(typinputfunc,
CStringGetDatum(val),
ObjectIdGetDatum(InvalidOid),
PG_ATTRTYPEMOD_P(state, attnum));
}
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
break;
}
default:
{
IFX_ATTR_SETNOTVALID_P(state, attnum);
break;
}
}
return result;
}
/*
* convertIfxDecimal()
*
* Converts a decimal value into a PostgreSQL numeric datum.
* Note that convertIfxDecimal() works by converting the
* character representation of a dec_t value formerly retrieved
* from an informix column. This we must be aware of any locale
* settings here.
*
* Currently, we support conversion to numeric types only.
*/
Datum convertIfxDecimal(IfxFdwExecutionState *state, int attnum)
{
Datum result;
Oid inputOid;
char *val;
regproc typinputfunc;
/*
* Init...
*/
result = PointerGetDatum(NULL);
switch(PG_ATTRTYPE_P(state, attnum))
{
case TEXTOID:
case VARCHAROID:
case NUMERICOID:
case CASHOID:
{
inputOid = PG_ATTRTYPE_P(state, attnum);
break;
}
default:
{
IFX_ATTR_SETNOTVALID_P(state, attnum);
return result;
}
}
val = (char *) palloc0(IFX_DECIMAL_BUF_LEN + 1);
/*
* Get the value from the informix column and check wether
* the character string is valid. Don't go further, if not...
*/
if (ifxGetDecimal(&state->stmt_info,
PG_MAPPED_IFX_ATTNUM(state, attnum), val) == NULL)
{
/* caller should handle indicator */
return result;
}
/*
* Type input function known and target column looks compatible,
* let's try the conversion...
*/
PG_TRY();
{
/*
* Get the type input function.
*/
typinputfunc = getTypeInputFunction(state, inputOid);
/*
* Watch out for typemods
*/
result = OidFunctionCall3(typinputfunc,
CStringGetDatum(val),
ObjectIdGetDatum(InvalidOid),
Int32GetDatum(PG_ATTRTYPEMOD_P(state, attnum)));
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
return result;
}
/*
* convertIfxInt()
*
* Converts either an 2-, 4-, or 8-byte informix integer value
* into a corresponding PostgreSQL datum. The target type
* range is checked and conversion refused if it doesn't
* match. We also support conversion into either TEXT, VARCHAR
* and bpchar.
*/
Datum convertIfxInt(IfxFdwExecutionState *state, int attnum)
{
Datum result = PointerGetDatum(NULL);
PgAttrDef pg_def;
/*
* Setup stuff...
*/
pg_def = state->pgAttrDefs[attnum];
/*
* Do the conversion...
*/
switch(pg_def.atttypid)
{
case INT2OID:
{
int16 val;
/* accepts int2 only */
if (IFX_ATTRTYPE_P(state, attnum) != IFX_SMALLINT)
{
IFX_ATTR_SETNOTVALID_P(state, attnum);
return PointerGetDatum(NULL);
}
val = ifxGetInt2(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum));
result = Int16GetDatum(val);
break;
}
case INT4OID:
{
int val;
/* accepts int2 and int4/serial */
if ((IFX_ATTRTYPE_P(state, attnum) != IFX_SMALLINT)
&& (IFX_ATTRTYPE_P(state, attnum) != IFX_INTEGER)
&& (IFX_ATTRTYPE_P(state, attnum) != IFX_SERIAL))
{
IFX_ATTR_SETNOTVALID_P(state, attnum);
return PointerGetDatum(NULL);
}
val = ifxGetInt4(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum));
result = Int32GetDatum(val);
break;
}
case INT8OID:
/*
* Note that the informix int8 value retrieved by
* ifxGetInt8() is converted into its *character*
* representation. We leave it up to the typinput
* routine to convert it back to a PostgreSQL BIGINT.
* So fall through and do the work below.
*/
case TEXTOID:
case VARCHAROID:
case BPCHAROID:
{
/*
* Try the conversion...
*/
PG_TRY();
{
if ((IFX_ATTRTYPE_P(state, attnum) == IFX_INT8)
|| (IFX_ATTRTYPE_P(state, attnum) == IFX_SERIAL8)
|| (IFX_ATTRTYPE_P(state, attnum) == IFX_BIGSERIAL)
|| (IFX_ATTRTYPE_P(state, attnum) == IFX_INFX_INT8))
{
char *buf;
regproc typinputfunc;
buf = (char *) palloc0(IFX_INT8_CHAR_LEN + 1);
/*
* Extract the value from the sqlvar tuple.
*
* We are forced to special case IFX_INFX_INT8 here, too,
* BIGINT and INT8 are incompatible.
*/
switch (IFX_ATTRTYPE_P(state, attnum))
{
case IFX_INT8:
case IFX_SERIAL8:
/* INT8 */
buf = ifxGetInt8(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum), buf);
break;
case IFX_INFX_INT8:
case IFX_BIGSERIAL:
/* BIGINT */
buf = ifxGetBigInt(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum), buf);
break;
default:
pfree(buf);
buf = NULL;
}
/*
* Check wether we have a valid buffer returned from the
* conversion routine. We might get a NULL pointer in case Informix
* failed to convert the INT8 into a proper int8 value. We might then
* get a NOT NULL indicator, but still stumple across the conversion error.
*/
if (state->stmt_info.ifxAttrDefs[PG_MAPPED_IFX_ATTNUM(state, attnum)].indicator
== INDICATOR_NOT_NULL)
{
/*
* Check for null pointer in buf. This is not expected
* and means an error occured.
*/
if (buf == NULL)
{
ifxRewindCallstack(&(state->stmt_info));
elog(ERROR,
"could not convert informix int8 value");
}
/*
* Finally call the type input function and we're
* done.
*/
typinputfunc = getTypeInputFunction(state, PG_ATTRTYPE_P(state, attnum));
result = OidFunctionCall2(typinputfunc,
CStringGetDatum(pstrdup(buf)),
ObjectIdGetDatum(InvalidOid));
}
}
else
{
/*
* We have a compatible integer type here
* and a character target type. In this case
* we simply call the cast function of the designated
* target type and let it do the legwork...
*/
regproc typcastfunc;
Oid sourceOid;
/*
* need the source type OID
*
* XXX: we might better do it earlier when
* retrieving the IFX types ???
*/
if ((IFX_ATTRTYPE_P(state, attnum) == IFX_INTEGER)
|| (IFX_ATTRTYPE_P(state, attnum) == IFX_SERIAL))
sourceOid = INT4OID;
else
/* only INT2 left... */
sourceOid = INT2OID;
/*
* Execute the cast function and we're done...
*/
typcastfunc = getTypeCastFunction(state, sourceOid,
PG_ATTRTYPE_P(state, attnum));
if (sourceOid == INT4OID)
result = OidFunctionCall1(typcastfunc,
Int32GetDatum(ifxGetInt4(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum))));
else
/* only INT2 left... */
result = OidFunctionCall1(typcastfunc,
Int16GetDatum(ifxGetInt2(&(state->stmt_info),
PG_MAPPED_IFX_ATTNUM(state, attnum))));
}
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
break;
}
default:
{
IFX_ATTR_SETNOTVALID_P(state, attnum);
return PointerGetDatum(NULL);
}
}
return result;
}
#if PG_VERSION_NUM >= 90300
/*
* Converts an interval column attribute into its string
* representation. interval_to_cstring() throws an error in case no
* valid format string for the given interval value can be generated.
* This could happen for example when creating a FMT_IFX format
* string with an interval which is outside the allowed range
* Informix accepts.
*
* This function always returns the interval in ANSI format without
* any fractions (YYYY-MM or DD HH24:MI:SS), depending on the format
* mode.
*
* NOTE: The function possibly throws an ereport(ERROR, ...) inside,
* so the caller should be prepared to deal with conversion
* errors.
*/
static inline char *interval_to_cstring(IfxFdwExecutionState *state,
TupleTableSlot *slot,
int attnum,
IfxFormatMode mode)
{
char *format = NULL;
Datum datval;
IfxTemporalRange range;
bool isnull = false;
datval = slot_getattr(slot, attnum + 1, &isnull);
/* NOTE: The minimal field identifier we support is TU_SECONDS! */
range = ifxGetTemporalQualifier(&(state->stmt_info),
IFX_ATTR_PARAM_ID(state, attnum));
range.precision = IFX_TU_SECOND;
format = ifxGetIntervalFormatString(range, mode);
/* In case we cannot retrieve a valid format string, abort. */
if (format == NULL)
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_ATTRIBUTE_VALUE),
errmsg("cannot get a format string for interval attribute %d",
attnum)));
}
datval = DirectFunctionCall2(interval_to_char,
datval,
PointerGetDatum(cstring_to_text(format)));
return text_to_cstring(DatumGetTextP(datval));
}
/*
* Store the given string value into the specified
* SQLDA handle, depending on wich target type we have.
*/
void setIfxText(IfxFdwExecutionState *state,
TupleTableSlot *slot,
int attnum)
{
/*
* Sanity check, SQLDA available ?
*/
Assert(state->stmt_info.sqlda != NULL);
/*
* In case we have a null value, set the indicator value accordingly.
*/
IFX_SET_INDICATOR_P(state, attnum,
((slot->tts_isnull[attnum]) ? INDICATOR_NULL : INDICATOR_NOT_NULL));
switch(IFX_ATTRTYPE_P(state, attnum))
{
case IFX_TEXT:
case IFX_BYTES:
break;
default:
break;
}
}
/*
* setIfxFloat()
*
* Converts a float datum into a character string
* suitable to be converted into a compatible
* Informix datatype.
*
* All float data conversion to informix are required
* to happen as a valid float value in its character representation,
* ifxSetupDataBufferAligned() strictly employs CSTRINGTYPE for
* this type conversion.
*/
void setIfxFloat(IfxFdwExecutionState *state,
TupleTableSlot *slot,
int attnum)
{
regproc typout;
char *strval;
IfxAttrDef ifxval;
Datum datum;
bool isnull = false;
/* Sanity check, SQLDA available? */
Assert(state->stmt_info.sqlda != NULL);
strval = NULL;
datum = slot_getattr(slot, attnum + 1, &isnull);
/*
* Take care for NULL values.
*/
if (! IFX_ATTR_ISNULL_P(state, IFX_ATTR_PARAM_ID(state, attnum))
&& ! isnull
&& IFX_ATTR_IS_VALID_P(state, IFX_ATTR_PARAM_ID(state, attnum)))
{
/*
* We must carefully check for any conversion errors to do
* the cleanup right away.
*
* We just call the type output function to get the
* character representation for the current source value.
*/
PG_TRY();
{
typout = getTypeOutputFunction(PG_ATTRTYPE_P(state, attnum));
strval = DatumGetCString(OidFunctionCall1(typout,
datum));
/*
* If the typ output function succeed, we have a
* valid buffer, pass it down to try to store them
* as a FLOAT value.
*/
if (strval == NULL)
{
ifxRewindCallstack(&(state->stmt_info));
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("informix_fdw: unexpected NULL value for attribute \"%d\" in float string representation",
attnum)));
}
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
}
/*
* Assign the value to the Informix SQLDA structure.
* ifxSetFloat() takes enough care for NULL values, but be sure
* to trap any conversion errors.
*/
ifxSetFloat(&(state->stmt_info),
IFX_ATTR_PARAM_ID(state, attnum),
strval);
/* Check... */
ifxval = state->stmt_info.ifxAttrDefs[IFX_ATTR_PARAM_ID(state, attnum)];
if ( (ifxval.indicator == INDICATOR_NOT_VALID)
&& (ifxval.converrcode != 0) )
{
switch (ifxval.converrcode)
{
case IFX_CONVERSION_OVERFLOW:
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_STRING_LENGTH_OR_BUFFER_LENGTH),
errmsg("informix_fdw: overflow during float datatype conversion (attnum \"%d\")",
attnum)));
break;
default:
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_ATTRIBUTE_VALUE),
errmsg("informix_fdw: conversion error \"%d\"(attnum \"%d\")",
ifxval.converrcode, attnum)));
break;
}
}
return;
}
/*
* setIfxDecimal()
*
* Converts a decimal value from PostgreSQL into
* an Informix value and stores it into the specified attribute.
*
* Given a PostgreSQL numeric value, we don't check
* the scale and precision of the target type wether it does fit
* the given value. We rely on the Informix routines to return
* an appropiate error in this case.
*/
void setIfxDecimal(IfxFdwExecutionState *state,
TupleTableSlot *slot,
int attnum)
{
regproc typout;
char *strval;
Datum datum;
bool isnull;
/* Sanity check, SQLDA available? */
Assert(state->stmt_info.sqlda != NULL);
strval = NULL;
datum = slot_getattr(slot, attnum + 1, &isnull);
/*
* Take care of NULL datums.
*/
if (! IFX_ATTR_ISNULL_P(state, IFX_ATTR_PARAM_ID(state, attnum))
&& ! isnull
&& IFX_ATTR_IS_VALID_P(state, IFX_ATTR_PARAM_ID(state, attnum)))
{
/*
* Take care of a possible failure during conversion.
*/
PG_TRY();
{
/*
* We need to take care wether we deal with a CASHOID datum
* or any other NUMERICOID datum. If the former occurs, we
* first cast to a decimal value, since Informix can't deal
* with a locale aware representation of a MONEY value (that's why
* we can't use the type output function for a CASHOID datum
* directly).
*
* In any case we expect the source datum to be converted
* into a character string. Call the datum output function to
* get it's text representation.
*/
if (PG_ATTRTYPE_P(state, attnum) == CASHOID)
{
regproc castfunc = getTypeCastFunction(state, CASHOID, NUMERICOID);
Datum castval = OidFunctionCall1(castfunc, datum);
typout = getTypeOutputFunction(NUMERICOID);
strval = DatumGetCString(OidFunctionCall1(typout,
castval));
}
else
{
typout = getTypeOutputFunction(PG_ATTRTYPE_P(state, attnum));
strval = DatumGetCString(OidFunctionCall1(typout,
datum));
}
}
PG_CATCH();
{
ifxRewindCallstack(&(state->stmt_info));
PG_RE_THROW();
}
PG_END_TRY();
/*
* strval must be a valid character string at this point.
*/
if (strval == NULL)
{
ifxRewindCallstack(&(state->stmt_info));
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("informix_fdw: unexpected NULL value for attribute \"%d\" in numeric string representation",
attnum)));
}
}
/*
* Assign the value into the Informix SQLDA structure. ifxSetDecimal()
* takes enough care for NULL values, so no additional checks required.
*/
switch(IFX_ATTRTYPE_P(state, IFX_ATTR_PARAM_ID(state, attnum)))
{
case IFX_MONEY:
case IFX_DECIMAL:
{
ifxSetDecimal(&(state->stmt_info),
IFX_ATTR_PARAM_ID(state, attnum),
strval);
break;
}
default:
{
/* unsupported conversion */
ereport(ERROR,
(errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("informix_fdw: unsupported conversion from type OID \"%u\" to informix type \"%d\"",
PG_ATTRTYPE_P(state, attnum),
IFX_ATTRTYPE_P(state, IFX_ATTR_PARAM_ID(state, attnum)))));
}
}
}
/*
* Transforms an PostgreSQL interval type
* into an compatible Informix Interval value.
*
* Internally, setIfxInterval() tries to convert the
* given PostgreSQL attribute value into a valid formatted
* character string representing an interval suitable to be
* passed to Informix. E.g:
*
* YEAR TO MONTH: YYYY-MM
* DAY TO FRACTION: DD HH24:MI:SS
*
* Currently, setIfxInterval doesn't convert fractions from a given
* interval value and ignores them.
*/
void setIfxInterval(IfxFdwExecutionState *state,
TupleTableSlot *slot,
int attnum)
{
/* Sanity check, SQLDA available? */