forked from pgspider/sqlite_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_query.c
More file actions
359 lines (307 loc) · 7.92 KB
/
sqlite_query.c
File metadata and controls
359 lines (307 loc) · 7.92 KB
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
/*-------------------------------------------------------------------------
*
* SQLite Foreign Data Wrapper for PostgreSQL
*
* Portions Copyright (c) 2018, TOSHIBA CORPORATION
*
* IDENTIFICATION
* sqlite_query.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "sqlite_fdw.h"
#include <stdio.h>
#include <sqlite3.h>
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "nodes/makefuncs.h"
#include "storage/ipc.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/hsearch.h"
#include "utils/syscache.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/timestamp.h"
#include "utils/formatting.h"
#include "utils/memutils.h"
#include "utils/guc.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/reloptions.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/vacuum.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/cost.h"
#include "optimizer/paths.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/var.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "parser/parsetree.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "postmaster/syslogger.h"
#include "storage/fd.h"
#include "catalog/pg_type.h"
#define DATE_SQLITE_PG(x, y) \
do { \
x->year = y.tm_year; \
x->month = y.tm_mon; \
x->day= y.tm_mday; \
x->hour = y.tm_hour; \
x->minute = y.tm_min; \
x->second = y.tm_sec; \
} while(0);
static int dec_bin(int n);
static int bin_dec(int n);
/*
* convert_sqlite_to_pg: Convert Sqlite data into PostgreSQL's compatible data types
*/
Datum
sqlite_convert_to_pg(Oid pgtyp, int pgtypmod, sqlite3_stmt * stmt, int attnum)
{
Datum value_datum = 0;
Datum valueDatum = 0;
regproc typeinput;
HeapTuple tuple;
int typemod;
char str[MAXDATELEN];
/* get the type's output function */
tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(pgtyp));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for type%u", pgtyp);
typeinput = ((Form_pg_type) GETSTRUCT(tuple))->typinput;
typemod = ((Form_pg_type) GETSTRUCT(tuple))->typtypmod;
ReleaseSysCache(tuple);
switch (pgtyp)
{
/*
* Sqlite gives BIT / BIT(n) data type as decimal value. The only
* way to retrieve this value is to use BIN, OCT or HEX function
* in Sqlite, otherwise sqlite client shows the actual decimal
* value, which could be a non - printable character. For exmple
* in Sqlite
*
* CREATE TABLE t (b BIT(8)); INSERT INTO t SET b = b'1001';
* SELECT BIN(b) FROM t; +--------+ | BIN(b) | +--------+ | 1001 |
* +--------+
*
* PostgreSQL expacts all binary data to be composed of either '0'
* or '1'. Sqlite gives value 9 hence PostgreSQL reports error.
* The solution is to convert the decimal number into equivalent
* binary string.
*/
case BYTEAOID:
{
int blobsize = sqlite3_column_bytes(stmt, attnum);
value_datum = (Datum) palloc0(blobsize + VARHDRSZ);
memcpy(VARDATA(value_datum), sqlite3_column_blob(stmt, attnum), blobsize);
SET_VARSIZE(value_datum, blobsize + VARHDRSZ);
return PointerGetDatum(value_datum);
}
case VARBITOID:
case BITOID:
sprintf(str, "%d", dec_bin(sqlite3_column_int(stmt, attnum)));
valueDatum = CStringGetDatum((char *) str);
break;
case INT2OID:
{
int value = sqlite3_column_int(stmt, attnum);
return Int16GetDatum(value);
}
case INT4OID:
{
int value = sqlite3_column_int(stmt, attnum);
return Int32GetDatum(value);
}
case INT8OID:
{
sqlite3_int64 value = sqlite3_column_int64(stmt, attnum);
return Int64GetDatum(value);
}
case FLOAT4OID:
{
double value = sqlite3_column_double(stmt, attnum);
return Float4GetDatum((float4) value);
break;
}
case FLOAT8OID:
{
double value = sqlite3_column_double(stmt, attnum);
return Float8GetDatum((float8) value);
break;
}
default:
valueDatum = CStringGetDatum((char *) sqlite3_column_text(stmt, attnum));
}
/* convert string value to appropriate type value */
value_datum = OidFunctionCall3(typeinput, valueDatum, ObjectIdGetDatum(InvalidOid), Int32GetDatum(typemod));
return value_datum;
}
/*
* 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(Oid type, int attnum, Datum value, sqlite3_stmt * stmt, bool *isnull)
{
int ret = SQLITE_OK;
attnum++;
/* Avoid to bind buffer in case value is NULL */
if (*isnull)
{
sqlite3_bind_null(stmt, attnum);
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 valueDatum = DirectFunctionCall1(numeric_float8, value);
float8 dat = DatumGetFloat8(valueDatum);
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:
{
/* Bind as text because SQLite does not have these types */
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
ret = sqlite3_bind_text(stmt, attnum, outputString, -1, SQLITE_TRANSIENT);
break;
}
case VARBITOID:
case BITOID:
{
int32 dat;
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
dat = bin_dec(atoi(outputString));
ret = sqlite3_bind_int(stmt, attnum, dat);
break;
}
case BYTEAOID:
{
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);
}
ret = sqlite3_bind_blob(stmt, attnum, dat, len, SQLITE_TRANSIENT);
break;
}
default:
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("cannot convert constant value to Sqlite value %u", type),
errhint("Constant value data type: %u", type)));
break;
}
}
if (ret != SQLITE_OK)
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: %u", type)));
}
static
int
dec_bin(int n)
{
int rem,
i = 1;
int bin = 0;
while (n != 0)
{
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
static int
bin_dec(int n)
{
int dec = 0;
int i = 0;
int rem;
while (n != 0)
{
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}