forked from EnterpriseDB/mysql_fdw
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mysql_query.c
542 lines (478 loc) · 13 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
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
/*-------------------------------------------------------------------------
*
* mysql_query.c
* Type handling for remote MySQL servers
*
* Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2004-2022, EnterpriseDB Corporation.
*
* IDENTIFICATION
* mysql_query.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
/*
* Must be included before mysql.h as it has some conflicting definitions like
* list_length, etc.
*/
#include "mysql_fdw.h"
#include <mysql.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "mysql_query.h"
#if PG_VERSION_NUM < 120000
#include "optimizer/var.h"
#else
#include "optimizer/optimizer.h"
#endif
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datetime.h"
#include "utils/lsyscache.h"
#include "utils/guc.h"
#include "utils/syscache.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 char *dec_bin(unsigned long number, int sz);
static int bin_dec(int binarynumber);
/*
* convert_mysql_to_pg:
* Convert MySQL data into PostgreSQL's compatible data types
*/
Datum
mysql_convert_to_pg(Oid pgtyp, int pgtypmod, mysql_column * column, MYSQL_FIELD field)
{
Datum value_datum;
Datum valueDatum;
regproc typeinput;
HeapTuple tuple;
char *str = palloc0(MAXDATELEN);
bytea *result;
/* 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;
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 BYTEAOID:
result = (bytea *) palloc(column->length + VARHDRSZ);
memcpy(VARDATA(result), VARDATA(column->value), column->length);
SET_VARSIZE(result, column->length + VARHDRSZ);
return PointerGetDatum(result);
case BITOID:
{
/*
* For bit type, need to convert each character to binary 8
* bit.
*/
char *outputString = (char *) column->value;
int i;
if (field.type == MYSQL_TYPE_BIT)
{
for (i = 0; i < strlen(outputString); i++)
{
unsigned long v = (unsigned long) outputString[i];
sprintf(str, "%s%s", str, dec_bin(v, 8));
}
}
else
{
unsigned long value = atoll(outputString);
sprintf(str, "%s", dec_bin(value, 64));
}
/*
* pgtypmod records type-specific data supplied at table creation time.
* BIT(8) -> pgtypmod = 8
* VARCHAR(12) -> pgtypmod = 12
*
* The value will generally be -1 for types that do not need typmod.
* BIT_OR, BIT_AND, TEXT, ... -> pgtypmod = -1
*
* Therefore, remove leading zero until to the pgtymod length.
*/
while (str[0] == '0')
{
if (pgtypmod != -1 && strlen(str) == pgtypmod)
break;
str++;
}
if (strcmp(str, "") == 0)
str[0] = '0';
valueDatum = CStringGetDatum((char *) str);
break;
}
case TIMESTAMPTZOID:
{
int tz;
struct pg_tm tm;
fsec_t fsec;
TimestampTz tsvalue;
value_datum = OidFunctionCall3(typeinput, CStringGetDatum((char *) column->value),
ObjectIdGetDatum(pgtyp),
Int32GetDatum(pgtypmod));
/*
* Timestamp value from Mysql already in UTC zone, however,
* this time zone does not specify in timestamp value. It make
* timestamptz casting of Postgres does not work well, it treats
* mysql timestamp as local timestamp (timestamp in current
* database time zone). So, calculate the current database's time
* zone offset to adjust the Timestamptz value
*/
tsvalue = DatumGetTimestampTz(value_datum);
timestamp2tm(tsvalue, NULL, &tm, &fsec, NULL, NULL);
tz = DetermineTimeZoneOffset(&tm, session_timezone);
tsvalue -= tz * USECS_PER_SEC;
return TimestampTzGetDatum(tsvalue);
}
default:
valueDatum = CStringGetDatum((char *) column->value);
}
value_datum = OidFunctionCall3(typeinput, valueDatum,
ObjectIdGetDatum(pgtyp),
Int32GetDatum(pgtypmod));
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;
/* TODO: We may have to add more type of array */
case INT2ARRAYOID:
case INT4ARRAYOID:
case TEXTARRAYOID:
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
case JSONOID:
case ANYENUMOID:
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;
case BYTEAOID:
return MYSQL_TYPE_BLOB;
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));
#if MYSQL_VERSION_ID < 80000 || MARIADB_VERSION_ID >= 100000
binds[attnum].is_null = (my_bool *) isnull;
#else
binds[attnum].is_null = isnull;
#endif
/* Avoid to bind buffer in case value is NULL */
if (*isnull)
return;
/*
* If type is an enum, use ANYENUMOID. We will send string containing the
* enum value to the MySQL.
*/
if (type_is_enum(type))
type = ANYENUMOID;
/* Assign the buffer type if value is not null */
binds[attnum].buffer_type = mysql_from_pgtyp(type);
switch (type)
{
case INT2OID:
{
int16 dat = DatumGetInt16(value);
int16 *bufptr = palloc(sizeof(int16));
memcpy(bufptr, (char *) &dat, sizeof(int16));
binds[attnum].buffer = bufptr;
}
break;
case INT4OID:
{
int32 dat = DatumGetInt32(value);
int32 *bufptr = palloc(sizeof(int32));
memcpy(bufptr, (char *) &dat, sizeof(int32));
binds[attnum].buffer = bufptr;
}
break;
case INT8OID:
{
int64 dat = DatumGetInt64(value);
int64 *bufptr = palloc(sizeof(int64));
memcpy(bufptr, (char *) &dat, sizeof(int64));
binds[attnum].buffer = bufptr;
}
break;
case FLOAT4OID:
{
float4 dat = DatumGetFloat4(value);
float4 *bufptr = palloc(sizeof(float4));
memcpy(bufptr, (char *) &dat, sizeof(float4));
binds[attnum].buffer = bufptr;
}
break;
case FLOAT8OID:
{
float8 dat = DatumGetFloat8(value);
float8 *bufptr = palloc(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 = palloc(sizeof(float8));
memcpy(bufptr, (char *) &dat, sizeof(float8));
binds[attnum].buffer = bufptr;
}
break;
case BOOLOID:
{
int32 dat = DatumGetInt32(value);
int32 *bufptr = palloc(sizeof(int32));
memcpy(bufptr, (char *) &dat, sizeof(int32));
binds[attnum].buffer = bufptr;
}
break;
/* TODO: We may have to add more type of array */
case INT2ARRAYOID:
case INT4ARRAYOID:
case TEXTARRAYOID:
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
case JSONOID:
case ANYENUMOID:
{
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;
case BYTEAOID:
{
int len;
char *dat = NULL;
char *bufptr;
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);
}
bufptr = palloc(len);
memcpy(bufptr, (char *) dat, len);
binds[attnum].buffer = bufptr;
binds[attnum].buffer_length = len;
}
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)
*/
void
mysql_bind_result(Oid pgtyp, int pgtypmod, MYSQL_FIELD * field,
mysql_column * column)
{
MYSQL_BIND *mbind = column->mysql_bind;
#if MYSQL_VERSION_ID < 80000 || MARIADB_VERSION_ID >= 100000
mbind->is_null = (my_bool *) & column->is_null;
mbind->error = (my_bool *) & column->error;
#else
mbind->is_null = &column->is_null;
mbind->error = &column->error;
#endif
mbind->length = &column->length;
switch (pgtyp)
{
case BYTEAOID:
mbind->buffer_type = MYSQL_TYPE_BLOB;
/* Leave room at front for bytea buffer length prefix */
column->value = (Datum) palloc0(MAX_BLOB_WIDTH + VARHDRSZ);
mbind->buffer = VARDATA(column->value);
mbind->buffer_length = MAX_BLOB_WIDTH;
break;
default:
mbind->buffer_type = MYSQL_TYPE_VAR_STRING;
column->value = (Datum) palloc0(MAXDATALEN);
mbind->buffer = (char *) column->value;
mbind->buffer_length = MAXDATALEN;
}
}
static char *
dec_bin(unsigned long number, int sz)
{
char *str = palloc0(64 + 1);
char *p = str + 64;
int i;
for (i = 0; i < sz; i++)
*(--p) = (number >> i & 1) ? '1' : '0';
return p;
}
static int
bin_dec(int binarynumber)
{
int dec = 0;
int i = 0;
int rem;
while (binarynumber != 0)
{
rem = binarynumber % 10;
binarynumber /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}