forked from pgspider/sqlite_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_data_norm.c
More file actions
321 lines (302 loc) · 8.52 KB
/
sqlite_data_norm.c
File metadata and controls
321 lines (302 loc) · 8.52 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
/*-------------------------------------------------------------------------
*
* SQLite Foreign Data Wrapper for PostgreSQL
*
* SQLite functions for data normalization
* This function is useful for mixed affinity inputs for PostgreSQL
* data column. Also some UUID functions are implemented here according
* the uuid SQLite exension, Public Domain
* https://www.sqlite.org/src/file/ext/misc/uuid.c
*
* IDENTIFICATION
* sqlite_data_norm.c
*
*-------------------------------------------------------------------------
*/
/*
* This SQLite extension implements functions that handling RFC-4122 UUIDs
* Three SQL functions are implemented:
*
* gen_random_uuid() - generate a version 4 UUID as a string
* uuid_str(X) - convert a UUID X into a well-formed UUID string
* uuid_blob(X) - convert a UUID X into a 16-byte blob
*
* The output from gen_random_uuid() and uuid_str(X) are always well-formed
* RFC-4122 UUID strings in this format:
*
* xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
*
* All of the 'x', 'M', and 'N' values are lower-case hexadecimal digits.
* The M digit indicates the "version". For uuid4()-generated UUIDs, the
* version is always "4" (a random UUID). The upper three bits of N digit
* are the "variant". This library only supports variant 1 (indicated
* by values of N between '8' and 'b') as those are overwhelming the most
* common. Other variants are for legacy compatibility only.
*
* The output of uuid_blob(X) is always a 16-byte blob. The UUID input
* string is converted in network byte order (big-endian) in accordance
* with RFC-4122 specifications for variant-1 UUIDs. Note that network
* byte order is *always* used, even if the input self-identifies as a
* variant-2 UUID.
*
* The input X to the uuid_str() and uuid_blob() functions can be either
* a string or a BLOB. If it is a BLOB it must be exactly 16 bytes in
* length or else a NULL is returned. If the input is a string it must
* consist of 32 hexadecimal digits, upper or lower case, optionally
* surrounded by {...} and with optional "-" characters interposed in the
* middle. The flexibility of input is inspired by the PostgreSQL
* implementation of UUID functions that accept in all of the following
* formats:
*
* A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
* {a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
* a0eebc999c0b4ef8bb6d6bb9bd380a11
* a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
* {a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}
*
* If any of the above inputs are passed into uuid_str(), the output will
* always be in the canonical RFC-4122 format:
*
* a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
*
* If the X input string has too few or too many digits or contains
* stray characters other than {, }, or -, then NULL is returned.
*/
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "sqlite3.h"
#include "postgres.h"
#include "sqlite_fdw.h"
static void error_helper(sqlite3* db, int rc);
#if !defined(SQLITE_ASCII) && !defined(SQLITE_EBCDIC)
#define SQLITE_ASCII 1
#endif
/*
* Translate a single byte of Hex into an integer.
* This routine only works if h really is a valid hexadecimal
* character: 0..9a..fA..F
*/
static unsigned char
sqlite_fdw_data_norm_UuidHexToInt(int h)
{
assert((h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F'));
#ifdef SQLITE_ASCII
h += 9 * (1 & (h >> 6));
#endif
#ifdef SQLITE_EBCDIC
h += 9 * (1 & ~(h >> 4));
#endif
return (unsigned char)(h & 0xf);
}
/*
* Attempt to parse a zero-terminated input string zs into a binary
* UUID. Return 1 on success, or 0 if the input string is not
* parsable.
*/
static int
sqlite_fdw_uuid_blob (const unsigned char* s0, unsigned char* Blob)
{
int i;
unsigned char* s = (unsigned char*)s0;
if (s[0] == '{')
s++;
for (i = 0; i < 16; i++)
{
if (s[0] == '-')
s++;
if (isxdigit(s[0]) && isxdigit(s[1]))
{
Blob[i] = (sqlite_fdw_data_norm_UuidHexToInt(s[0]) << 4) + sqlite_fdw_data_norm_UuidHexToInt(s[1]);
s += 2;
}
else
{
return 0;
}
}
if (s[0] == '}')
s++;
return s[0] == 0;
}
/*
* uuid_generate generates a version 4 UUID as a string
*
*static void uuid_generate(sqlite3_context* context, int argc, sqlite3_value** argv)
*{
* unsigned char aBlob[16];
* unsigned char zs[37];
* sqlite3_randomness(16, aBlob);
* aBlob[6] = (aBlob[6] & 0x0f) + 0x40;
* aBlob[8] = (aBlob[8] & 0x3f) + 0x80;
* sqlite_fdw_data_norm_uuid_blob_to_str(aBlob, zs);
* sqlite3_result_text(context, (char*)zs, 36, SQLITE_TRANSIENT);
*}
*/
/*
* uuid_str converts a UUID X into a well-formed UUID string.
* X can be either a string or a blob.
*
* static void uuid_str(sqlite3_context* context, int argc, sqlite3_value** argv) {
* unsigned char aBlob[16];
* unsigned char zs[37];
* const unsigned char* pBlob;
* (void)argc;
* pBlob = sqlite_fdw_data_norm_uuid_input_to_blob(argv[0], aBlob);
* if (pBlob == 0)
* return;
* sqlite_fdw_data_norm_uuid_blob_to_str(pBlob, zs);
* sqlite3_result_text(context, (char*)zs, 36, SQLITE_TRANSIENT);
*}
*/
/*
* uuid_blob normalize text or blob UUID argv[0] into a 16-byte blob.
*/
static void
sqlite_fdw_data_norm_uuid(sqlite3_context* context, int argc, sqlite3_value** argv)
{
unsigned char aBlob[16];
sqlite3_value* arg = argv[0];
if (sqlite3_value_type(argv[0]) == SQLITE3_TEXT)
{
const unsigned char* txt = sqlite3_value_text(arg);
if (sqlite_fdw_uuid_blob(txt, aBlob))
{
sqlite3_result_blob(context, aBlob, 16, SQLITE_TRANSIENT);
return;
}
}
sqlite3_result_value(context, arg);
}
/*
* ISO:SQL valid boolean values with text affinity such as Y, no, f, t, oN etc.
* will be treated as boolean like in PostgreSQL console input
*/
static void
sqlite_fdw_data_norm_bool(sqlite3_context* context, int argc, sqlite3_value** argv)
{
sqlite3_value* arg = argv[0];
int dt = sqlite3_value_type(arg);
const char* t;
int l;
if (dt == SQLITE_INTEGER)
{
/* The fastest call because expected very often */
sqlite3_result_value(context, arg);
return;
}
if (dt != SQLITE3_TEXT && dt != SQLITE_BLOB )
{
/* NULL, FLOAT */
sqlite3_result_value(context, arg);
return;
}
l = sqlite3_value_bytes(arg);
if (l > 5)
{
sqlite3_result_value(context, arg);
return;
}
t = (const char*)sqlite3_value_text(arg);
if ( l == 1 )
{
if (strcasecmp(t, "t") == 0)
{
sqlite3_result_int(context, 1);
return;
}
if (strcasecmp(t, "f") == 0)
{
sqlite3_result_int(context, 0);
return;
}
if (strcasecmp(t, "y") == 0)
{
sqlite3_result_int(context, 1);
return;
}
if (strcasecmp(t, "n") == 0)
{
sqlite3_result_int(context, 0);
return;
}
/* rare but possible cases */
if (strcasecmp(t, "1") == 0)
{
sqlite3_result_int(context, 1);
return;
}
if (strcasecmp(t, "0") == 0)
{
sqlite3_result_int(context, 0);
return;
}
}
else if ( l == 2 )
{
if (strcasecmp(t, "on") == 0)
{
sqlite3_result_int(context, 1);
return;
}
if (strcasecmp(t, "no") == 0)
{
sqlite3_result_int(context, 0);
return;
}
}
else if ( l == 3 )
{
if (strcasecmp(t, "yes") == 0)
{
sqlite3_result_int(context, 1);
return;
}
if (strcasecmp(t, "off") == 0)
{
sqlite3_result_int(context, 0);
return;
}
}
else if ( l == 4 && strcasecmp(t, "true") == 0)
{
sqlite3_result_int(context, 1);
return;
}
else if ( l == 5 && strcasecmp(t, "false") == 0)
{
sqlite3_result_int(context, 0);
return;
}
sqlite3_result_value(context, arg);
}
/*
* Makes pg error from SQLite error.
* Interrupts normal executing, no need return after place of calling
*/
static void
error_helper(sqlite3* db, int rc)
{
const char * err = sqlite3_errmsg(db);
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_ESTABLISH_CONNECTION),
errmsg("failed to create data unifying functions for SQLite DB"),
errhint("%s \n SQLite code %d", err, rc)));
}
void
sqlite_fdw_data_norm_functs_init(sqlite3* db)
{
static const int det_flags = SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC;
int rc = sqlite3_create_function(db, "sqlite_fdw_uuid_blob", 1, det_flags, 0, sqlite_fdw_data_norm_uuid, 0, 0);
if (rc != SQLITE_OK)
error_helper(db, rc);
rc = sqlite3_create_function(db, "sqlite_fdw_bool", 1, det_flags, 0, sqlite_fdw_data_norm_bool, 0, 0);
if (rc != SQLITE_OK)
error_helper(db, rc);
/* no rc because in future SQLite releases it can be added UUID generation function
* PostgreSQL 13+, no gen_random_uuid() before
* static const int flags = SQLITE_UTF8 | SQLITE_INNOCUOUS;
* sqlite3_create_function(db, "uuid_generate_v4", 0, flags, 0, uuid_generate, 0, 0);
* sqlite3_create_function(db, "gen_random_uuid", 1, flags, 0, uuid_generate, 0, 0);
*/
}