-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsqlite_query.c
1094 lines (1029 loc) · 30 KB
/
sqlite_query.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
/*-------------------------------------------------------------------------
*
* SQLite Foreign Data Wrapper for PostgreSQL
*
* Portions Copyright (c) 2018, TOSHIBA CORPORATION
*
* IDENTIFICATION
* sqlite_query.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "sqlite_fdw.h"
#ifdef SQLITE_FDW_GIS_ENABLE
#include <spatialite.h>
#endif
#include <sqlite3.h>
#include "catalog/pg_type_d.h"
#include "commands/defrem.h"
#include "mb/pg_wchar.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/inet.h"
#include "utils/lsyscache.h"
#include "utils/timestamp.h"
#include "utils/uuid.h"
static char *
get_column_option_string(Oid relid, int varattno, char *optionname);
static char *
sqlite_text_value_to_pg_db_encoding(sqlite3_value *val);
static char *
int642binstr(sqlite3_int64 num, char *s, size_t len);
/*
* sqlite_value_to_pg_error
* Human readable message about disallowed combination of PostgreSQL columnn
* data type and SQLite data value affinity
*/
static void
sqlite_value_to_pg_error()
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("SQLite value is not compatible with PostgreSQL column data type")));
}
/*
* pg_column_void_text_error
* Human readable message about disallowed void text for the PostgreSQL columnn
*/
static void
pg_column_void_text_error()
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("Void text disallowed this column")));
}
/*
* convert_sqlite_to_pg
* Converts SQLite value into PostgreSQL's Datum
*/
NullableDatum
sqlite_convert_to_pg(Form_pg_attribute att, sqlite3_value * val, AttInMetadata *attinmeta, AttrNumber attnum, int sqlite_value_affinity, int AffinityBehaviourFlags)
{
Oid pgtyp = att->atttypid;
Datum value_datum = 0;
char *valstr = NULL;
/* Compute always, void text and void BLOB is special cases */
int value_byte_size_blob_or_utf8 = sqlite3_value_bytes(val);
switch (pgtyp)
{
case BOOLOID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
int value = sqlite3_value_int(val);
return (struct NullableDatum){BoolGetDatum(value), false};
}
case SQLITE_FLOAT:
case SQLITE_BLOB:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
sqlite_value_to_pg_error();
else
pg_column_void_text_error();
break;
}
default:
{
sqlite_value_to_pg_error();
break;
}
}
break;
}
case BYTEAOID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER:
case SQLITE_FLOAT:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE_BLOB: /* <-- proper and recommended SQLite affinity of value for pgtyp */
case SQLITE3_TEXT: /* treated as UTF-8 text BLOB */
{
value_datum = (Datum) palloc0(value_byte_size_blob_or_utf8 + VARHDRSZ);
memcpy(VARDATA(value_datum), sqlite3_value_blob(val), value_byte_size_blob_or_utf8);
SET_VARSIZE(value_datum, value_byte_size_blob_or_utf8 + VARHDRSZ);
return (struct NullableDatum) {PointerGetDatum((const void *)value_datum), false};
}
}
break;
}
case INT2OID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
sqlite_int64 i64v = sqlite3_value_int64(val);
Datum d = DirectFunctionCall1(int82, Int64GetDatum((int64) i64v));
return (struct NullableDatum) {d, false};
}
case SQLITE_FLOAT:
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
sqlite_value_to_pg_error();
else
pg_column_void_text_error();
break;
}
}
break;
}
case INT4OID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
sqlite_int64 i64v = sqlite3_value_int64(val);
Datum d = DirectFunctionCall1(int84, Int64GetDatum((int64) i64v));
return (struct NullableDatum) {d, false};
}
case SQLITE_FLOAT: /* TODO: This code is untill mod() pushdowning fix here */
{
int value = sqlite3_value_int(val);
elog(DEBUG2, "sqlite_fdw : real aff. was readed for pg int32");
return (struct NullableDatum) {Int32GetDatum(value), false};
}
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
sqlite_value_to_pg_error();
else
pg_column_void_text_error();
break;
}
}
break;
}
case INT8OID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
sqlite3_int64 value = sqlite3_value_int64(val);
return (struct NullableDatum) {Int64GetDatum(value), false};
}
case SQLITE_FLOAT: /* TODO: This code is untill mod() pushdowning fix here */
{
double value = sqlite3_value_double(val);
Datum d = DirectFunctionCall1(dtoi8, Float8GetDatum((float8) value));
elog(DEBUG2, "sqlite_fdw : real aff. was readed for pg int64");
return (struct NullableDatum) {d, false};
}
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
sqlite_value_to_pg_error();
else
pg_column_void_text_error();
break;
}
}
break;
}
case FLOAT4OID:
{
switch (sqlite_value_affinity)
{
case SQLITE_FLOAT: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
double value = sqlite3_value_double(val);
Datum d = DirectFunctionCall1(dtof, Float8GetDatum((float8)value));
return (struct NullableDatum) {d, false};
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
{
const char* text_value = (const char*) sqlite3_value_text(val);
if (strcasecmp(text_value, "NaN") == 0)
return (struct NullableDatum) {Float8GetDatum(NAN), false};
else
sqlite_value_to_pg_error();
}
else
pg_column_void_text_error();
break;
}
case SQLITE_INTEGER:
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
}
break;
}
case FLOAT8OID:
{
switch (sqlite_value_affinity)
{
case SQLITE_FLOAT: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
double value = sqlite3_value_double(val);
return (struct NullableDatum) {Float8GetDatum((float8) value), false};
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
{
const char* text_value = (const char*) sqlite3_value_text(val);
if (strcasecmp(text_value, "NaN") == 0)
return (struct NullableDatum) {Float8GetDatum(NAN), false};
else
sqlite_value_to_pg_error();
}
else
pg_column_void_text_error();
break;
}
case SQLITE_INTEGER:
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
}
break;
}
case TIMESTAMPOID:
case TIMESTAMPTZOID:
{
/*
* We add this conversion to allow add INTEGER/FLOAT SQLite
* Columns be added as TimeStamp in PostgreSQL. We just
* calling PostgreSQL function "to_timestamp(double value)""
* to convert each registry returned from INT/FLOAT value to
* TimeStamp string, so PosgtreSQL can handle/show without
* problems. If it's a TEXT SQLite column...we let them to the
* "regular" process because its already implemented and
* working properly.
*/
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER:
{
Timestamp value = (Timestamp)sqlite3_value_int64(val);
return (struct NullableDatum) {TimestampGetDatum(value), false};
}
case SQLITE_FLOAT:
{
double value = sqlite3_value_double(val);
Datum d = DirectFunctionCall1(float8_timestamptz, Float8GetDatum((float8) value));
return (struct NullableDatum) {d, false};
}
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
valstr = sqlite_text_value_to_pg_db_encoding(val);
/* !!! use valstr later! */
else
pg_column_void_text_error();
break;
}
}
break;
}
case NUMERICOID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER:
case SQLITE_FLOAT: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
double value = sqlite3_value_double(val);
valstr = DatumGetCString(DirectFunctionCall1(float8out, Float8GetDatum((float8) value)));
break; /* !!! use valstr later! */
}
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
{
const char* text_value = (const char*) sqlite3_value_text(val);
if (strcasecmp(text_value, "NaN") == 0)
return (struct NullableDatum) {Float8GetDatum(NAN), false};
else
sqlite_value_to_pg_error();
}
else
pg_column_void_text_error();
break;
}
}
break;
}
case UUIDOID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER:
case SQLITE_FLOAT:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE_BLOB: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
if (value_byte_size_blob_or_utf8 != UUID_LEN)
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("PostgreSQL uuid data type allows only %d bytes SQLite blob value", UUID_LEN)));
break;
}
else
{
const unsigned char * sqlite_blob = 0;
pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
sqlite_blob = sqlite3_value_blob(val);
memcpy(retval->data, sqlite_blob, UUID_LEN);
return (struct NullableDatum){UUIDPGetDatum(retval), false};
break;
}
}
/*
* SQLite UUID output always normalized to blob.
* In sqlite_data_norm.c there is special additional C function.
*/
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
sqlite_value_to_pg_error();
else
pg_column_void_text_error();
break;
}
default:
{
sqlite_value_to_pg_error();
break;
}
}
break;
}
case MACADDROID:
case MACADDR8OID:
{
switch (sqlite_value_affinity)
{
/*
* SQLite MAC address output always normalized to int64.
* In sqlite_data_norm.c there is special additional C function.
*/
case SQLITE_INTEGER: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
sqlite3_int64 value = sqlite3_value_int64(val);
if (pgtyp == MACADDROID)
{
macaddr* retval;
/* maximal int64 for macaddr 6b */
const sqlite3_uint64 max = (1ULL << (6 * CHAR_BIT)) - 1ULL;
if (value > max )
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("int64 for macaddr out of range")));
retval = (macaddr *) palloc(sizeof(macaddr));
retval->a = (value >> (CHAR_BIT * 5)) & 0xFF;
retval->b = (value >> (CHAR_BIT * 4)) & 0xFF;
retval->c = (value >> (CHAR_BIT * 3)) & 0xFF;
retval->d = (value >> (CHAR_BIT * 2)) & 0xFF;
retval->e = (value >> (CHAR_BIT * 1)) & 0xFF;
retval->f = (value >> (CHAR_BIT * 0)) & 0xFF;
return (struct NullableDatum){MacaddrPGetDatum(retval), false};
}
else
{
macaddr8 * retval = (macaddr8 *) palloc(sizeof(macaddr8));
retval->a = (value >> (CHAR_BIT * 7)) & 0xFF;
retval->b = (value >> (CHAR_BIT * 6)) & 0xFF;
retval->c = (value >> (CHAR_BIT * 5)) & 0xFF;
retval->d = (value >> (CHAR_BIT * 4)) & 0xFF;
retval->e = (value >> (CHAR_BIT * 3)) & 0xFF;
retval->f = (value >> (CHAR_BIT * 2)) & 0xFF;
retval->g = (value >> (CHAR_BIT * 1)) & 0xFF;
retval->h = (value >> (CHAR_BIT * 0)) & 0xFF;
return (struct NullableDatum){Macaddr8PGetDatum(retval), false};
}
}
case SQLITE_FLOAT:
case SQLITE_BLOB:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
sqlite_value_to_pg_error();
else
pg_column_void_text_error();
break;
}
default:
{
sqlite_value_to_pg_error();
break;
}
}
break;
}
case VARBITOID:
case BITOID:
{
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
char * buffer = (char *) palloc0(SQLITE_FDW_BIT_DATATYPE_BUF_SIZE);
sqlite3_int64 sqlti = sqlite3_value_int64(val);
buffer = int642binstr(sqlti, buffer, SQLITE_FDW_BIT_DATATYPE_BUF_SIZE);
valstr = buffer;
elog(DEBUG4, "sqlite_fdw : BIT buf l=%ld v = %s", SQLITE_FDW_BIT_DATATYPE_BUF_SIZE, buffer);
break;
}
case SQLITE_FLOAT:
case SQLITE_BLOB:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE3_TEXT:
{
if (value_byte_size_blob_or_utf8)
sqlite_value_to_pg_error();
else
pg_column_void_text_error();
break;
}
}
break;
}
case BPCHAROID:
case VARCHAROID:
case CHAROID:
case TEXTOID:
case JSONOID:
case JSONBOID:
case NAMEOID:
case DATEOID:
case TIMEOID:
{
valstr = sqlite_text_value_to_pg_db_encoding(val);
break;
}
default:
{
/*
* PostGIS data types can be supported only by name
* This is very rare and not fast algorythm branch
*/
char *pg_dataTypeName = TypeNameToString(makeTypeNameFromOid(att->atttypid, att->atttypmod));
NameData pgColND = att->attname;
if (listed_datatype(pg_dataTypeName, postGisSpecificTypes))
{
elog(DEBUG4, "sqlite_fdw : is postGisSpecificType %s", pg_dataTypeName);
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("This data type is PostGIS specific and is not supported by SpatiaLite or sqlite_fdw"),
errhint("Data type: \"%s\" in column \"%.*s\"", pg_dataTypeName, (int)sizeof(pgColND.data), pgColND.data)));
}
if (listed_datatype(pg_dataTypeName, postGisSQLiteCompatibleTypes))
{
elog(DEBUG4, "sqlite_fdw : is postGisSQLiteCompatibleType");
switch (sqlite_value_affinity)
{
case SQLITE_INTEGER:
case SQLITE_FLOAT:
default:
{
sqlite_value_to_pg_error();
break;
}
case SQLITE_BLOB: /* <-- proper and recommended SQLite affinity of value for pgtyp */
{
#ifdef SQLITE_FDW_GIS_ENABLE
unsigned const char * p_blob = sqlite3_value_blob(val);
valstr = SpatiaLiteAsPostGISgeom (
(struct blobOutput){(const char *)p_blob, value_byte_size_blob_or_utf8},
att
);
/* !!! use valstr later! Hex Input */
#else
Oid atttypid = att->atttypid;
int32 atttypmod = att->atttypmod;
/*
* If a domain has been declared as bytea, it can support PostGIS data type
*/
atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
if (atttypid == BYTEAOID)
{
value_datum = (Datum) palloc0(value_byte_size_blob_or_utf8 + VARHDRSZ);
memcpy(VARDATA(value_datum), sqlite3_value_blob(val), value_byte_size_blob_or_utf8);
SET_VARSIZE(value_datum, value_byte_size_blob_or_utf8 + VARHDRSZ);
return (struct NullableDatum) {PointerGetDatum((const void *)value_datum), false};
}
else
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("This PostGIS data type is supported by SpatiaLite, but FDW compiled without GIS data support"),
errhint("Data type: \"%s\" in column \"%.*s\"", pg_dataTypeName, (int)sizeof(pgColND.data), pgColND.data)));
#endif
break;
}
case SQLITE3_TEXT:
{
/* WKT data transport is not implemented */
sqlite_value_to_pg_error();
break;
}
}
}
else
{
/* common, not PostGIS case */
valstr = sqlite_text_value_to_pg_db_encoding(val);
}
}
break;
}
/* convert string value to appropriate type value */
value_datum = InputFunctionCall(&attinmeta->attinfuncs[attnum],
valstr,
attinmeta->attioparams[attnum],
attinmeta->atttypmods[attnum]);
return (struct NullableDatum){value_datum, false};
}
/*
* sqlite_datum_to_blob
* Common part of extracting and preparing PostgreSQL bytea data
* for SQLite binding as blob
*/
blobOutput
sqlite_datum_to_blob (Datum value)
{
int len;
char *dat = NULL;
char *result = DatumGetPointer(value);
if (VARATT_IS_1B(result))
{
len = VARSIZE_1B(result) - VARHDRSZ_SHORT;
dat = VARDATA_1B(result);
}
else
{
len = VARSIZE_4B(result) - VARHDRSZ;
dat = VARDATA_4B(result);
}
return (struct blobOutput){dat, len};
}
/*
* get_column_option_string
* By Oid of relation and varattno returns value of requested option
* of foreign table
*/
static char *
get_column_option_string(Oid relid, int varattno, char *optionname)
{
char *coloptionvalue = NULL;
List *options;
ListCell *lc;
options = GetForeignColumnOptions(relid, varattno);
foreach(lc, options)
{
DefElem *def = (DefElem *) lfirst(lc);
if (strcmp(def->defname, optionname) == 0)
{
coloptionvalue = defGetString(def);
break;
}
}
return coloptionvalue;
}
/*
* bind_sql_var
* Bind the values provided as DatumBind the values and nulls
* to modify the target table (INSERT/UPDATE)
*/
void
sqlite_bind_sql_var(Form_pg_attribute att, int attnum, Datum value, sqlite3_stmt * stmt, bool *isnull, Oid relid)
{
int ret = SQLITE_OK;
Oid type = att->atttypid;
int32 pgtypmod = att->atttypmod;
attnum++;
elog(DEBUG2, "sqlite_fdw : %s %d type=%u relid=%u typmod=%d ", __func__, attnum, type, relid, pgtypmod);
if (*isnull)
{
ret = sqlite3_bind_null(stmt, attnum);
if (ret != SQLITE_OK)
elog(ERROR, "sqlite3_bind_null failed with rc=%d", ret);
return;
}
switch (type)
{
case INT2OID:
{
int16 dat = DatumGetInt16(value);
ret = sqlite3_bind_int(stmt, attnum, dat);
break;
}
case INT4OID:
{
int32 dat = DatumGetInt32(value);
ret = sqlite3_bind_int(stmt, attnum, dat);
break;
}
case INT8OID:
{
int64 dat = DatumGetInt64(value);
ret = sqlite3_bind_int64(stmt, attnum, dat);
break;
}
case FLOAT4OID:
{
float4 dat = DatumGetFloat4(value);
ret = sqlite3_bind_double(stmt, attnum, (double) dat);
break;
}
case FLOAT8OID:
{
float8 dat = DatumGetFloat8(value);
ret = sqlite3_bind_double(stmt, attnum, dat);
break;
}
case NUMERICOID:
{
Datum value_datum = DirectFunctionCall1(numeric_float8, value);
float8 dat = DatumGetFloat8(value_datum);
ret = sqlite3_bind_double(stmt, attnum, dat);
break;
}
case BOOLOID:
{
int32 dat = DatumGetInt32(value);
ret = sqlite3_bind_int(stmt, attnum, dat);
break;
}
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
case JSONOID:
case NAMEOID:
case TIMEOID:
case TIMESTAMPOID:
case TIMESTAMPTZOID:
case DATEOID:
{
/* Bind as text because SQLite does not have these types */
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
int pg_database_encoding = GetDatabaseEncoding(); /* very fast call, see PostgreSQL mbutils.c */
char *utf8_text_value = NULL;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
if (pg_database_encoding == PG_UTF8)
utf8_text_value = outputString;
else
utf8_text_value = (char *) pg_do_encoding_conversion((unsigned char *) outputString, strlen(outputString), pg_database_encoding, PG_UTF8);
ret = sqlite3_bind_text(stmt, attnum, utf8_text_value, -1, SQLITE_TRANSIENT);
break;
}
case BYTEAOID:
{
blobOutput b = sqlite_datum_to_blob(value);
ret = sqlite3_bind_blob(stmt, attnum, b.dat, b.len, SQLITE_TRANSIENT);
break;
}
case UUIDOID:
{
int sqlite_aff = SQLITE_NULL;
if (relid)
{
char * optv = get_column_option_string (relid, attnum, "column_type");
elog(DEBUG3, "sqlite_fdw : col type %s ", optv);
sqlite_aff = sqlite_affinity_code(optv);
}
if (sqlite_aff == SQLITE_BLOB)
{
unsigned char *dat = palloc0(UUID_LEN);
pg_uuid_t* pg_uuid = DatumGetUUIDP(value);
elog(DEBUG2, "sqlite_fdw : bind uuid as blob");
memcpy(dat, pg_uuid->data, UUID_LEN);
ret = sqlite3_bind_blob(stmt, attnum, dat, UUID_LEN, SQLITE_TRANSIENT);
}
else
{
/* uuid as text */
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
/* uuid text belongs to ASCII subset, no need to translate encoding */
ret = sqlite3_bind_text(stmt, attnum, outputString, -1, SQLITE_TRANSIENT);
}
break;
}
case MACADDROID:
case MACADDR8OID:
{
int sqlite_aff = SQLITE_INTEGER; /* default mac addr affinity */
if (relid)
{
char * optv = get_column_option_string (relid, attnum, "column_type");
elog(DEBUG3, "sqlite_fdw : column_type affinity %s ", optv);
sqlite_aff = sqlite_affinity_code(optv);
}
if (sqlite_aff == SQLITE3_TEXT)
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
elog(DEBUG2, "sqlite_fdw : bind mac as text");
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
/* MAC text belongs to ASCII subset, no need to translate encoding */
ret = sqlite3_bind_text(stmt, attnum, outputString, -1, SQLITE_TRANSIENT);
}
else if (sqlite_aff == SQLITE_BLOB)
{
if (type == MACADDROID)
{
unsigned char *mca = palloc0(MACADDR_LEN);
macaddr *m = DatumGetMacaddrP(value);
elog(DEBUG2, "sqlite_fdw : bind mac as blob");
mca[0] = m->a;
mca[1] = m->b;
mca[2] = m->c;
mca[3] = m->d;
mca[4] = m->e;
mca[5] = m->f;
ret = sqlite3_bind_blob(stmt, attnum, mca, MACADDR_LEN, SQLITE_TRANSIENT);
}
else
{
unsigned char *mca = palloc0(MACADDR8_LEN);
macaddr8 *m = DatumGetMacaddr8P(value);
elog(DEBUG2, "sqlite_fdw : bind mac8 as blob");
mca[0] = m->a;
mca[1] = m->b;
mca[2] = m->c;
mca[3] = m->d;
mca[4] = m->e;
mca[5] = m->f;
mca[6] = m->g;
mca[7] = m->h;
ret = sqlite3_bind_blob(stmt, attnum, mca, MACADDR8_LEN, SQLITE_TRANSIENT);
}
}
else /* default, INTEGER */
{
sqlite3_uint64 dat = 0;
if (type == MACADDROID)
{
macaddr *m = DatumGetMacaddrP(value);
dat = ((sqlite3_int64)(m->a) << (CHAR_BIT*5))
+ ((sqlite3_int64)(m->b) << (CHAR_BIT*4))
+ ((sqlite3_int64)(m->c) << (CHAR_BIT*3))
+ ((sqlite3_int64)(m->d) << (CHAR_BIT*2))
+ ((sqlite3_int64)(m->e) << (CHAR_BIT*1))
+ ((sqlite3_int64)(m->f) << (CHAR_BIT*0));
elog(DEBUG2, "sqlite_fdw : bind mac6 as integer %lld", dat);
}
else
{
macaddr8 *m = DatumGetMacaddr8P(value);
dat = ((sqlite3_int64)(m->a) << (CHAR_BIT*7))
+ ((sqlite3_int64)(m->b) << (CHAR_BIT*6))
+ ((sqlite3_int64)(m->c) << (CHAR_BIT*5))
+ ((sqlite3_int64)(m->d) << (CHAR_BIT*4))
+ ((sqlite3_int64)(m->e) << (CHAR_BIT*3))
+ ((sqlite3_int64)(m->f) << (CHAR_BIT*2))
+ ((sqlite3_int64)(m->g) << (CHAR_BIT*1))
+ ((sqlite3_int64)(m->h) << (CHAR_BIT*0));
elog(DEBUG2, "sqlite_fdw : bind mac8 as integer %lld", dat);
}
ret = sqlite3_bind_int64(stmt, attnum, dat);
}
break;
}
case VARBITOID:
case BITOID:
{
sqlite3_int64 dat;
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
elog(DEBUG4, "sqlite_fdw : BIT bind %s", outputString);
if (strlen(outputString) > SQLITE_FDW_BIT_DATATYPE_BUF_SIZE - 1 )
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("SQLite FDW dosens't support very long bit/varbit data"),
errhint("bit length %ld, maximum %ld", strlen(outputString), SQLITE_FDW_BIT_DATATYPE_BUF_SIZE - 1)));
}
dat = binstr2int64(outputString);
ret = sqlite3_bind_int64(stmt, attnum, dat);
break;
}
default:
{
NameData pgColND = att->attname;
char *pg_dataTypeName = TypeNameToString(makeTypeNameFromOid(type, pgtypmod));
/*
* PostGIS data types can be supported only by name
* This is very rare and not fast algorythm branch
*/
if (listed_datatype(pg_dataTypeName, postGisSQLiteCompatibleTypes))
{
#ifdef SQLITE_FDW_GIS_ENABLE
blobOutput b = PostGISgeomAsSpatiaLite(value, att);
ret = sqlite3_bind_blob(stmt, attnum, b.dat, b.len, SQLITE_TRANSIENT);
#else
Oid atttypid = att->atttypid;
int32 atttypmod = att->atttypmod;
/*
* If a domain has been declared as bytea,
* it can support PostGIS data type
*/
atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
if (atttypid == BYTEAOID)
{
blobOutput b = sqlite_datum_to_blob(value);
ret = sqlite3_bind_blob(stmt, attnum, b.dat, b.len, SQLITE_TRANSIENT);
}
else
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("This PostGIS data type is supported by SpatiaLite, but FDW compiled without GIS data support"),
errhint("Data type: \"%s\" in column \"%.*s\"", pg_dataTypeName, (int)sizeof(pgColND.data), pgColND.data)));
#endif
break;
}
else if (listed_datatype(pg_dataTypeName, postGisSpecificTypes))
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("This data type is PostGIS specific and have not any SpatiaLite value"),
errhint("Data type: \"%s\" in column \"%.*s\"", pg_dataTypeName, (int)sizeof(pgColND.data), pgColND.data)));
else
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("cannot convert constant value to SQLite value"),
errhint("Constant value data type: \"%s\" in column \"%.*s\"", pg_dataTypeName, (int)sizeof(pgColND.data), pgColND.data)));
break;
}
}
if (ret != SQLITE_OK)
{
char *pg_dataTypeName = TypeNameToString(makeTypeNameFromOid(type, pgtypmod));
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("Can't convert constant value to SQLite: %s",
sqlite3_errmsg(sqlite3_db_handle(stmt))),
errhint("Constant value data type: %s", pg_dataTypeName)));
}
}
/*
* sqlite_text_value_to_pg_db_encoding
* Converts SQLite text to PostgreSQL text with database encoding
*/
static char *
sqlite_text_value_to_pg_db_encoding(sqlite3_value *val)
{
int pg_database_encoding = GetDatabaseEncoding(); /* very fast call, see PostgreSQL mbutils.c */
char *utf8_text_value;
/* Text from this SQLite function is always UTF-8,
* see https://www.sqlite.org/c3ref/column_blob.html
*/
utf8_text_value = (char *) sqlite3_value_text(val);
if (pg_database_encoding == PG_UTF8)
return utf8_text_value;
else