-
Notifications
You must be signed in to change notification settings - Fork 36
/
sqlite+tcc.c
408 lines (396 loc) · 9.66 KB
/
sqlite+tcc.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
/*
** 2006 August 22
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** Experimental combination of SQLite 3.3.x and TinyCC 0.9.25
** using the new SQLite loadable extension mechanism.
**
** This module creates an SQLite scalar function 'tcc_compile'
** which takes one argument which is a string made up of
** C source code to be on-the-fly compiled using TinyCC.
** The SQLite API is visible during compilation when
** 'sqlite3.h' is included.
**
** select tcc_compile(c_string, opt, ...)
**
** c_string string containing C code
** opt ... zero or more compile options as strings
** -Lpath add library path
** -lfile add library
** -Ipath add include path
** -ipath add system include path
** everything else starting with '-' is ignored
** everything else is taken as C source file to be
** compiled
**
** WARNING: TinyCC's libtcc.a must be created with necessary
** compiler switches to be linked into a shared object. This
** usually means that "-fPIC" must be added to compiler flags
** in TinyCC's Makefile for the targets libtcc.o/libtcc.a.
*/
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include "libtcc.h"
#include "sqlite3ext.h"
#define APIOFF(n) ((int) ((char *) &((sqlite3_api_routines *) 0)->n))
#define SYM(n) { "sqlite3_"#n, APIOFF(n) }
#define SYM2(n, n2) { "sqlite3_"#n, APIOFF(n2) }
#define SYM_END { 0, 0 }
static struct {
const char *name; /* TCC name for tcc_add_symbol() */
int offs; /* Offset into struct sqlite3_api_routines */
} symtab[] = {
SYM(aggregate_context),
SYM(aggregate_count),
SYM(bind_blob),
SYM(bind_double),
SYM(bind_int),
SYM(bind_int64),
SYM(bind_null),
SYM(bind_parameter_count),
SYM(bind_parameter_index),
SYM(bind_parameter_name),
SYM(bind_text),
SYM(bind_text16),
SYM(bind_value),
SYM(busy_handler),
SYM(busy_timeout),
SYM(changes),
SYM(close),
SYM(collation_needed),
SYM(collation_needed16),
SYM(column_blob),
SYM(column_bytes),
SYM(column_bytes16),
SYM(column_count),
SYM(column_database_name),
SYM(column_database_name16),
SYM(column_decltype),
SYM(column_decltype16),
SYM(column_double),
SYM(column_int),
SYM(column_int64),
SYM(column_name),
SYM(column_name16),
SYM(column_origin_name),
SYM(column_origin_name16),
SYM(column_table_name),
SYM(column_table_name16),
SYM(column_text),
SYM(column_text16),
SYM(column_type),
SYM(column_value),
SYM(commit_hook),
SYM(complete),
SYM(complete16),
SYM(create_collation),
SYM(create_collation16),
SYM(create_function),
SYM(create_function16),
SYM(create_module),
SYM(data_count),
SYM(db_handle),
SYM(declare_vtab),
SYM(enable_shared_cache),
SYM(errcode),
SYM(errmsg),
SYM(errmsg16),
SYM(exec),
#if 0
SYM(expired),
#endif
SYM(finalize),
SYM(free),
SYM(free_table),
SYM(get_autocommit),
SYM(get_auxdata),
SYM(get_table),
#if 0
SYM(global_recover),
#endif
SYM2(interrupt, interruptx),
SYM(last_insert_rowid),
SYM(libversion),
SYM(libversion_number),
SYM(malloc),
SYM(mprintf),
SYM(open),
SYM(open16),
SYM(prepare),
SYM(prepare16),
SYM(profile),
SYM(progress_handler),
SYM(realloc),
SYM(reset),
SYM(result_blob),
SYM(result_double),
SYM(result_error),
SYM(result_error16),
SYM(result_int),
SYM(result_int64),
SYM(result_null),
SYM(result_text),
SYM(result_text16),
SYM(result_text16be),
SYM(result_text16le),
SYM(result_value),
SYM(rollback_hook),
SYM(set_authorizer),
SYM(set_auxdata),
#if SQLITE_VERSION_NUMBER >= 3022000
SYM2(snprintf, xsnprintf),
#else
SYM(snprintf),
#endif
SYM(step),
SYM(table_column_metadata),
SYM(thread_cleanup),
SYM(total_changes),
SYM(trace),
#if 0
SYM(transfer_bindings),
#endif
SYM(update_hook),
SYM(user_data),
SYM(value_blob),
SYM(value_bytes),
SYM(value_bytes16),
SYM(value_double),
SYM(value_int),
SYM(value_int64),
SYM(value_numeric_type),
SYM(value_text),
SYM(value_text16),
SYM(value_text16be),
SYM(value_text16le),
SYM(value_type),
SYM(vmprintf),
#if defined(HAVE_SQLITE3OVERLOADFUNCTION) && HAVE_SQLITE3OVERLOADFUNCTION
SYM(overload_function),
#endif
#if defined(HAVE_SQLITE3PREPAREV2) && HAVE_SQLITE3PREPAREV2
SYM(prepare_v2),
SYM(prepare16_v2),
#endif
#if defined(HAVE_SQLITE3CLEARBINDINGS) && HAVE_SQLITE3CLEARBINDINGS
SYM(clear_bindings),
#endif
#if defined(HAVE_SQLITE3CREATEMODULEV2) && HAVE_SQLITE3CREATEMODULEV2
SYM(create_module_v2)
#endif
#if defined(HAVE_SQLITE3VFS) && HAVE_SQLITE3VFS
SYM(bind_zeroblob),
SYM(blob_bytes),
SYM(blob_close),
SYM(blob_open),
SYM(blob_read),
SYM(blob_write),
SYM(create_collation_v2),
SYM(memory_highwater),
SYM(memory_used),
SYM(mutex_alloc),
SYM(mutex_enter),
SYM(mutex_free),
SYM(mutex_leave),
SYM(mutex_enter),
SYM(mutex_try),
SYM(open_v2),
SYM(release_memory),
SYM(result_error_nomem),
SYM(result_error_toobig),
SYM(sleep),
SYM(soft_heap_limit),
SYM(vfs_find),
SYM(vfs_register),
SYM(vfs_unregister),
SYM(file_control),
SYM2(threadsafe, xthreadsafe),
SYM(result_zeroblob),
SYM(result_error_code),
SYM(test_control),
SYM(randomness),
SYM(context_db_handle),
SYM(extended_result_codes),
SYM(limit),
SYM(next_stmt),
SYM(sql),
SYM(status),
SYM(backup_finish),
SYM(backup_init),
SYM(backup_pagecount),
SYM(backup_remaining),
SYM(backup_step),
SYM(compileoption_get),
SYM(compileoption_used),
SYM(create_function_v2),
SYM(db_config),
SYM(db_mutes),
SYM(db_status),
SYM(extended_errcode),
SYM(long),
SYM(soft_heap_limit64),
SYM(sourceid),
SYM(stmt_status),
SYM(strnicmp),
SYM(unlock_notify),
SYM(wal_autocheckpoint),
SYM(wal_checkpoint),
SYM(wal_hook),
#endif
SYM_END
};
#ifdef _WIN32
/* TCC is not thread safe, provide a guard */
static CRITICAL_SECTION tcc_mutex;
static HANDLE tcc_hinst;
#endif
/* Module pointer to SQLite3 API function vector. */
static SQLITE_EXTENSION_INIT1
static void tcc_compile(
sqlite3_context *ctx,
int argc,
sqlite3_value **argv
){
TCCState *t;
int i;
unsigned long val;
void (*xInit)(void *);
void *code;
sqlite3 *db = sqlite3_user_data(ctx);
if( argc<1 ){
sqlite3_result_error(ctx, "need at least one argument", -1);
return;
}
#ifdef _WIN32
EnterCriticalSection(&tcc_mutex);
#endif
t = tcc_new();
if( !t ){
sqlite3_result_error(ctx, "no compile context", -1);
#ifdef _WIN32
LeaveCriticalSection(&tcc_mutex);
#endif
return;
}
#ifdef _WIN32
tcc_set_lib_path_mod(t, tcc_hinst);
#endif
tcc_set_output_type(t, TCC_OUTPUT_MEMORY);
for( i=1; i<argc; i++ ){
char *p = (char *) sqlite3_value_text(argv[i]);
if( p && p[0]=='-' && p[1]=='L' && p[2] ){
tcc_add_library_path(t, p+2);
}else if( p && p[0]=='-' && p[1]=='l' && p[2] ){
tcc_add_library(t, p+2);
}else if( p && p[0]=='-' && p[1]=='I' && p[2] ){
tcc_add_include_path(t, p+2);
}else if( p && p[0]=='-' && p[1]=='i' && p[2] ){
tcc_add_sysinclude_path(t, p+2);
}else if( p && p[0]!='-'){
tcc_add_file(t, p);
}
}
if( tcc_compile_string(t, (char *) sqlite3_value_text(argv[0])) ){
sqlite3_result_error(ctx, "compile error", -1);
goto error;
}
for( i=0; symtab[i].name; i++ ){
val = ((unsigned long *) ((char *) sqlite3_api + symtab[i].offs))[0];
if( val ){
tcc_add_symbol(t, symtab[i].name, (void *) val);
} else {
#ifdef _WIN32
char msg[512];
sprintf(msg, "API function '%s' not available.", symtab[i].name);
MessageBox(NULL, msg, "SQLITE+TCC", MB_ICONEXCLAMATION | MB_OK |
MB_TASKMODAL | MB_SETFOREGROUND);
#else
fprintf(stderr, "API function '%s' not available\n", symtab[i].name);
#endif
}
}
i = tcc_relocate( t, 0 );
if( i<=0 ){
sqlite3_result_error(ctx, "link error", -1);
goto error;
}
if( (code = malloc(i))==0 ){
sqlite3_result_error(ctx, "link error, out of memory", -1);
goto error;
}
if( tcc_relocate(t, code)<0 ){
free(code);
sqlite3_result_error(ctx, "link error", -1);
goto error;
}
val = (unsigned long) tcc_get_symbol(t, "init");
if( val==0 ){
free(code);
sqlite3_result_error(ctx, "no init function", -1);
goto error;
}
#ifdef _WIN32
LeaveCriticalSection(&tcc_mutex);
#endif
xInit = (void *) val;
xInit(db);
return;
error:
tcc_delete(t);
#ifdef _WIN32
LeaveCriticalSection(&tcc_mutex);
#endif
}
int sqlite3_extension_init(
sqlite3 *db, /* SQLite3 database connection */
char **pzErrMsg, /* Put error message here if not 0 */
const sqlite3_api_routines *api /* SQLite3 API entries */
){
SQLITE_EXTENSION_INIT2(api);
if( sqlite3_create_function(db, "tcc_compile", -1, SQLITE_UTF8, db,
tcc_compile, 0, 0) != SQLITE_OK ){
if( pzErrMsg ){
*pzErrMsg = sqlite3_mprintf("cannot create function");
}
return SQLITE_ERROR;
}
return SQLITE_OK;
}
#ifdef _WIN32
BOOL APIENTRY LibMain(
HANDLE hinst,
DWORD reason,
LPVOID reserved
){
static int initialized = 0;
static long lock;
if( reason==DLL_PROCESS_ATTACH ){
while( !initialized ){
if( InterlockedIncrement(&lock)==1 ){
InitializeCriticalSection(&tcc_mutex);
tcc_hinst = hinst;
++initialized;
}else{
Sleep(1);
}
}
}
return TRUE;
}
int __stdcall DllMain(
HANDLE hinst,
DWORD reason,
LPVOID reserved
){
return LibMain(hinst, reason, reserved);
}
#endif