forked from EnterpriseDB/mysql_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_query.c
445 lines (382 loc) · 10 KB
/
mysql_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
/*-------------------------------------------------------------------------
*
* mysql_query.c
* Foreign-data wrapper for remote MySQL servers
*
* Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
*
* Portions Copyright (c) 2004-2014, EnterpriseDB Corporation.
*
* IDENTIFICATION
* mysql_query.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "mysql_fdw.h"
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <mysql.h>
#include "access/reloptions.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/vacuum.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "nodes/makefuncs.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "storage/ipc.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "utils/date.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 "access/htup_details.h"
#include "access/sysattr.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/vacuum.h"
#include "foreign/fdwapi.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/prep.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/var.h"
#include "parser/parsetree.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "optimizer/pathnode.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/planmain.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "postmaster/syslogger.h"
#include "storage/fd.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
#include "mysql_fdw.h"
#include "mysql_query.h"
#define DATE_MYSQL_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 int32 mysql_from_pgtyp(Oid type);
static int dec_bin(int n);
static int bin_dec(int n);
/*
* convert_mysql_to_pg: Convert MySQL data into PostgreSQL's compatible data types
*/
Datum
mysql_convert_to_pg(Oid pgtyp, int pgtypmod, Datum data, MySQLFdwExecState *festate)
{
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)
{
/*
* MySQL 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 MySQL, otherwise
* mysql client shows the actual decimal value, which could be a non - printable character.
* For exmple in MySQL
*
* 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'. MySQL gives
* value 9 hence PostgreSQL reports error. The solution is to convert the decimal number
* into equivalent binary string.
*/
case BITOID:
sprintf(str, "%d", dec_bin(*((int*)data)));
valueDatum = CStringGetDatum((char*)str);
break;
default:
valueDatum = CStringGetDatum((char*)data);
}
value_datum = OidFunctionCall3(typeinput, valueDatum, ObjectIdGetDatum(InvalidOid), Int32GetDatum(typemod));
return value_datum;
}
/*
* mysql_from_pgtyp: Give MySQL data type for PG type
*/
static int32
mysql_from_pgtyp(Oid type)
{
switch(type)
{
case INT2OID:
return MYSQL_TYPE_SHORT;
case INT4OID:
return MYSQL_TYPE_LONG;
case INT8OID:
return MYSQL_TYPE_LONGLONG;
case FLOAT4OID:
return MYSQL_TYPE_FLOAT;
case FLOAT8OID:
return MYSQL_TYPE_DOUBLE;
case NUMERICOID:
return MYSQL_TYPE_DOUBLE;
case BOOLOID:
return MYSQL_TYPE_LONG;
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
return MYSQL_TYPE_STRING;
case NAMEOID:
return MYSQL_TYPE_STRING;
case DATEOID:
return MYSQL_TYPE_DATE;
case TIMEOID:
case TIMESTAMPOID:
case TIMESTAMPTZOID:
return MYSQL_TYPE_TIMESTAMP;
case BITOID:
return MYSQL_TYPE_LONG;
default:
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("cannot convert constant value to MySQL value"),
errhint("Constant value data type: %u", type)));
break;
}
}
}
/*
* bind_sql_var:
* Bind the values provided as DatumBind the values and nulls to modify the target table (INSERT/UPDATE)
*/
void
mysql_bind_sql_var(Oid type, int attnum, Datum value, MYSQL_BIND *binds, bool *isnull)
{
/* Clear the bind buffer and attributes */
memset(&binds[attnum], 0x0, sizeof(MYSQL_BIND));
binds[attnum].buffer_type = mysql_from_pgtyp(type);
binds[attnum].is_null = isnull;
/* Avoid to bind buffer in case value is NULL */
if (*isnull)
return;
switch(type)
{
case INT2OID:
{
int16 dat = DatumGetInt16(value);
int16 *bufptr = palloc0(sizeof(int16));
memcpy(bufptr, (char*)&dat, sizeof(int16));
binds[attnum].buffer = bufptr;
break;
}
case INT4OID:
{
int32 dat = DatumGetInt32(value);
int32 *bufptr = palloc0(sizeof(int32));
memcpy(bufptr, (char*)&dat, sizeof(int32));
binds[attnum].buffer = bufptr;
break;
}
case INT8OID:
{
int64 dat = DatumGetInt64(value);
int64 *bufptr = palloc0(sizeof(int64));
memcpy(bufptr, (char*)&dat, sizeof(int64));
binds[attnum].buffer = bufptr;
break;
}
case FLOAT4OID:
{
float4 dat = DatumGetFloat4(value);
float4 *bufptr = palloc0(sizeof(float4));
memcpy(bufptr, (char*)&dat, sizeof(float4));
binds[attnum].buffer = bufptr;
break;
}
case FLOAT8OID:
{
float8 dat = DatumGetFloat8(value);
float8 *bufptr = palloc0(sizeof(float8));
memcpy(bufptr, (char*)&dat, sizeof(float8));
binds[attnum].buffer = bufptr;
break;
}
case NUMERICOID:
{
Datum valueDatum = DirectFunctionCall1(numeric_float8, value);
float8 dat = DatumGetFloat8(valueDatum);
float8 *bufptr = palloc0(sizeof(float8));
memcpy(bufptr, (char*)&dat, sizeof(float8));
binds[attnum].buffer = bufptr;
break;
}
case BOOLOID:
{
int32 dat = DatumGetInt32(value);
int32 *bufptr = palloc0(sizeof(int32));
memcpy(bufptr, (char*)&dat, sizeof(int32));
binds[attnum].buffer = bufptr;
break;
}
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
binds[attnum].buffer = outputString;
binds[attnum].buffer_length = strlen(outputString);
break;
}
case NAMEOID:
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
binds[attnum].buffer=outputString;
binds[attnum].buffer_length=strlen(outputString);
break;
}
case DATEOID:
{
int tz;
struct pg_tm tt, *tm = &tt;
fsec_t fsec;
const char *tzn;
Datum valueDatum = DirectFunctionCall1(date_timestamp, value);
Timestamp valueTimestamp = DatumGetTimestamp(valueDatum);
MYSQL_TIME* ts = palloc0(sizeof(MYSQL_TIME));
timestamp2tm(valueTimestamp, &tz, tm, &fsec, &tzn, pg_tzset("UTC"));
DATE_MYSQL_PG(ts, tt);
binds[attnum].buffer = ts;
binds[attnum].buffer_length=sizeof(MYSQL_TIME);
break;
}
case TIMEOID:
case TIMESTAMPOID:
case TIMESTAMPTZOID:
{
Timestamp valueTimestamp = DatumGetTimestamp(value);
MYSQL_TIME* ts = palloc0(sizeof(MYSQL_TIME));
int tz;
struct pg_tm tt,
*tm = &tt;
fsec_t fsec;
const char *tzn;
timestamp2tm(valueTimestamp, &tz, tm, &fsec, &tzn, pg_tzset("UTC"));
DATE_MYSQL_PG(ts, tt);
binds[attnum].buffer = ts;
binds[attnum].buffer_length = sizeof(MYSQL_TIME);
break;
}
case BITOID:
{
int32 dat;
int32 *bufptr = palloc0(sizeof(int32));
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(type, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
dat = bin_dec(atoi(outputString));
memcpy(bufptr, (char*)&dat, sizeof(int32));
binds[attnum].buffer = bufptr;
break;
}
default:
{
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("cannot convert constant value to MySQL value"),
errhint("Constant value data type: %u", type)));
break;
}
}
}
/*
* mysql_bind_result: Bind the value and null pointers to get
* the data from remote mysql table (SELECT)
*/
Datum
mysql_bind_result(int attnum, Datum *value, bool *isnull, MYSQL_BIND *result_values)
{
result_values[attnum].is_null = isnull;
result_values[attnum].buffer_type = MYSQL_TYPE_VAR_STRING;
/* TODO: Set the maximum data length based on the type of MySQL's column */
value = palloc0(MAXDATALEN);
result_values[attnum].buffer = value;
result_values[attnum].buffer_length = MAXDATALEN;
return (Datum)value;
}
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;
}