wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Diagnostics; |
||
3 | using HANDLE = System.IntPtr; |
||
4 | using System.Text; |
||
5 | |||
6 | namespace Community.CsharpSqlite |
||
7 | { |
||
8 | public partial class Sqlite3 |
||
9 | { |
||
10 | /* |
||
11 | ** 2006 June 7 |
||
12 | ** |
||
13 | ** The author disclaims copyright to this source code. In place of |
||
14 | ** a legal notice, here is a blessing: |
||
15 | ** |
||
16 | ** May you do good and not evil. |
||
17 | ** May you find forgiveness for yourself and forgive others. |
||
18 | ** May you share freely, never taking more than you give. |
||
19 | ** |
||
20 | ************************************************************************* |
||
21 | ** This file contains code used to dynamically load extensions into |
||
22 | ** the SQLite library. |
||
23 | ************************************************************************* |
||
24 | ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart |
||
25 | ** C#-SQLite is an independent reimplementation of the SQLite software library |
||
26 | ** |
||
27 | ** SQLITE_SOURCE_ID: 2011-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908e |
||
28 | ** |
||
29 | ************************************************************************* |
||
30 | */ |
||
31 | #if !SQLITE_CORE |
||
32 | //#define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ |
||
33 | const int SQLITE_CORE = 1; |
||
34 | #endif |
||
35 | //#include "sqlite3ext.h" |
||
36 | //#include "sqliteInt.h" |
||
37 | //#include <string.h> |
||
38 | |||
39 | #if !SQLITE_OMIT_LOAD_EXTENSION |
||
40 | |||
41 | /* |
||
42 | ** Some API routines are omitted when various features are |
||
43 | ** excluded from a build of SQLite. Substitute a NULL pointer |
||
44 | ** for any missing APIs. |
||
45 | */ |
||
46 | #if !SQLITE_ENABLE_COLUMN_METADATA |
||
47 | //# define sqlite3_column_database_name 0 |
||
48 | //# define sqlite3_column_database_name16 0 |
||
49 | //# define sqlite3_column_table_name 0 |
||
50 | //# define sqlite3_column_table_name16 0 |
||
51 | //# define sqlite3_column_origin_name 0 |
||
52 | //# define sqlite3_column_origin_name16 0 |
||
53 | //# define sqlite3_table_column_metadata 0 |
||
54 | #endif |
||
55 | |||
56 | #if SQLITE_OMIT_AUTHORIZATION |
||
57 | //# define sqlite3_set_authorizer 0 |
||
58 | #endif |
||
59 | |||
60 | #if SQLITE_OMIT_UTF16 |
||
61 | //# define sqlite3_bind_text16 0 |
||
62 | //# define sqlite3_collation_needed16 0 |
||
63 | //# define sqlite3_column_decltype16 0 |
||
64 | //# define sqlite3_column_name16 0 |
||
65 | //# define sqlite3_column_text16 0 |
||
66 | //# define sqlite3_complete16 0 |
||
67 | //# define sqlite3_create_collation16 0 |
||
68 | //# define sqlite3_create_function16 0 |
||
69 | //# define sqlite3_errmsg16 0 |
||
70 | static string sqlite3_errmsg16( sqlite3 db ) |
||
71 | { |
||
72 | return string.Empty; |
||
73 | } |
||
74 | //# define sqlite3_open16 0 |
||
75 | //# define sqlite3_prepare16 0 |
||
76 | //# define sqlite3_prepare16_v2 0 |
||
77 | //# define sqlite3_result_error16 0 |
||
78 | //# define sqlite3_result_text16 0 |
||
79 | static void sqlite3_result_text16( sqlite3_context pCtx, string z, int n, dxDel xDel ) |
||
80 | { |
||
81 | } |
||
82 | //# define sqlite3_result_text16be 0 |
||
83 | //# define sqlite3_result_text16le 0 |
||
84 | //# define sqlite3_value_text16 0 |
||
85 | //# define sqlite3_value_text16be 0 |
||
86 | //# define sqlite3_value_text16le 0 |
||
87 | //# define sqlite3_column_database_name16 0 |
||
88 | //# define sqlite3_column_table_name16 0 |
||
89 | //# define sqlite3_column_origin_name16 0 |
||
90 | #endif |
||
91 | |||
92 | #if SQLITE_OMIT_COMPLETE |
||
93 | //# define sqlite3_complete 0 |
||
94 | //# define sqlite3_complete16 0 |
||
95 | #endif |
||
96 | |||
97 | #if SQLITE_OMIT_DECLTYPE |
||
98 | //# define sqlite3_column_decltype16 0 |
||
99 | //# define sqlite3_column_decltype 0 |
||
100 | #endif |
||
101 | |||
102 | #if SQLITE_OMIT_PROGRESS_CALLBACK |
||
103 | //# define sqlite3_progress_handler 0 |
||
104 | static void sqlite3_progress_handler (sqlite3 db, int nOps, dxProgress xProgress, object pArg){} |
||
105 | #endif |
||
106 | |||
107 | #if SQLITE_OMIT_VIRTUALTABLE |
||
108 | //# define sqlite3_create_module 0 |
||
109 | //# define sqlite3_create_module_v2 0 |
||
110 | //# define sqlite3_declare_vtab 0 |
||
111 | #endif |
||
112 | |||
113 | #if SQLITE_OMIT_SHARED_CACHE |
||
114 | //# define sqlite3_enable_shared_cache 0 |
||
115 | #endif |
||
116 | |||
117 | #if SQLITE_OMIT_TRACE |
||
118 | //# define sqlite3_profile 0 |
||
119 | //# define sqlite3_trace 0 |
||
120 | #endif |
||
121 | |||
122 | #if SQLITE_OMIT_GET_TABLE |
||
123 | //# define //sqlite3_free_table 0 |
||
124 | //# define sqlite3_get_table 0 |
||
125 | static public int sqlite3_get_table( |
||
126 | sqlite3 db, /* An open database */ |
||
127 | string zSql, /* SQL to be evaluated */ |
||
128 | ref string[] pazResult, /* Results of the query */ |
||
129 | ref int pnRow, /* Number of result rows written here */ |
||
130 | ref int pnColumn, /* Number of result columns written here */ |
||
131 | ref string pzErrmsg /* Error msg written here */ |
||
132 | ) |
||
133 | { |
||
134 | return 0; |
||
135 | } |
||
136 | #endif |
||
137 | |||
138 | #if SQLITE_OMIT_INCRBLOB |
||
139 | //#define sqlite3_bind_zeroblob 0 |
||
140 | //#define sqlite3_blob_bytes 0 |
||
141 | //#define sqlite3_blob_close 0 |
||
142 | //#define sqlite3_blob_open 0 |
||
143 | //#define sqlite3_blob_read 0 |
||
144 | //#define sqlite3_blob_write 0 |
||
145 | #endif |
||
146 | |||
147 | /* |
||
148 | ** The following structure contains pointers to all SQLite API routines. |
||
149 | ** A pointer to this structure is passed into extensions when they are |
||
150 | ** loaded so that the extension can make calls back into the SQLite |
||
151 | ** library. |
||
152 | ** |
||
153 | ** When adding new APIs, add them to the bottom of this structure |
||
154 | ** in order to preserve backwards compatibility. |
||
155 | ** |
||
156 | ** Extensions that use newer APIs should first call the |
||
157 | ** sqlite3_libversion_number() to make sure that the API they |
||
158 | ** intend to use is supported by the library. Extensions should |
||
159 | ** also check to make sure that the pointer to the function is |
||
160 | ** not NULL before calling it. |
||
161 | */ |
||
162 | public class sqlite3_api_routines |
||
163 | { |
||
164 | public sqlite3 context_db_handle; |
||
165 | }; |
||
166 | |||
167 | static sqlite3_api_routines sqlite3Apis = new sqlite3_api_routines(); |
||
168 | //{ |
||
169 | // sqlite3_aggregate_context, |
||
170 | #if !SQLITE_OMIT_DEPRECATED |
||
171 | / sqlite3_aggregate_count, |
||
172 | #else |
||
173 | // 0, |
||
174 | #endif |
||
175 | // sqlite3_bind_blob, |
||
176 | // sqlite3_bind_double, |
||
177 | // sqlite3_bind_int, |
||
178 | // sqlite3_bind_int64, |
||
179 | // sqlite3_bind_null, |
||
180 | // sqlite3_bind_parameter_count, |
||
181 | // sqlite3_bind_parameter_index, |
||
182 | // sqlite3_bind_parameter_name, |
||
183 | // sqlite3_bind_text, |
||
184 | // sqlite3_bind_text16, |
||
185 | // sqlite3_bind_value, |
||
186 | // sqlite3_busy_handler, |
||
187 | // sqlite3_busy_timeout, |
||
188 | // sqlite3_changes, |
||
189 | // sqlite3_close, |
||
190 | // sqlite3_collation_needed, |
||
191 | // sqlite3_collation_needed16, |
||
192 | // sqlite3_column_blob, |
||
193 | // sqlite3_column_bytes, |
||
194 | // sqlite3_column_bytes16, |
||
195 | // sqlite3_column_count, |
||
196 | // sqlite3_column_database_name, |
||
197 | // sqlite3_column_database_name16, |
||
198 | // sqlite3_column_decltype, |
||
199 | // sqlite3_column_decltype16, |
||
200 | // sqlite3_column_double, |
||
201 | // sqlite3_column_int, |
||
202 | // sqlite3_column_int64, |
||
203 | // sqlite3_column_name, |
||
204 | // sqlite3_column_name16, |
||
205 | // sqlite3_column_origin_name, |
||
206 | // sqlite3_column_origin_name16, |
||
207 | // sqlite3_column_table_name, |
||
208 | // sqlite3_column_table_name16, |
||
209 | // sqlite3_column_text, |
||
210 | // sqlite3_column_text16, |
||
211 | // sqlite3_column_type, |
||
212 | // sqlite3_column_value, |
||
213 | // sqlite3_commit_hook, |
||
214 | // sqlite3_complete, |
||
215 | // sqlite3_complete16, |
||
216 | // sqlite3_create_collation, |
||
217 | // sqlite3_create_collation16, |
||
218 | // sqlite3_create_function, |
||
219 | // sqlite3_create_function16, |
||
220 | // sqlite3_create_module, |
||
221 | // sqlite3_data_count, |
||
222 | // sqlite3_db_handle, |
||
223 | // sqlite3_declare_vtab, |
||
224 | // sqlite3_enable_shared_cache, |
||
225 | // sqlite3_errcode, |
||
226 | // sqlite3_errmsg, |
||
227 | // sqlite3_errmsg16, |
||
228 | // sqlite3_exec, |
||
229 | #if !SQLITE_OMIT_DEPRECATED |
||
230 | //sqlite3_expired, |
||
231 | #else |
||
232 | //0, |
||
233 | #endif |
||
234 | // sqlite3_finalize, |
||
235 | // //sqlite3_free, |
||
236 | // //sqlite3_free_table, |
||
237 | // sqlite3_get_autocommit, |
||
238 | // sqlite3_get_auxdata, |
||
239 | // sqlite3_get_table, |
||
240 | // 0, /* Was sqlite3_global_recover(), but that function is deprecated */ |
||
241 | // sqlite3_interrupt, |
||
242 | // sqlite3_last_insert_rowid, |
||
243 | // sqlite3_libversion, |
||
244 | // sqlite3_libversion_number, |
||
245 | // sqlite3_malloc, |
||
246 | // sqlite3_mprintf, |
||
247 | // sqlite3_open, |
||
248 | // sqlite3_open16, |
||
249 | // sqlite3_prepare, |
||
250 | // sqlite3_prepare16, |
||
251 | // sqlite3_profile, |
||
252 | // sqlite3_progress_handler, |
||
253 | // sqlite3_realloc, |
||
254 | // sqlite3_reset, |
||
255 | // sqlite3_result_blob, |
||
256 | // sqlite3_result_double, |
||
257 | // sqlite3_result_error, |
||
258 | // sqlite3_result_error16, |
||
259 | // sqlite3_result_int, |
||
260 | // sqlite3_result_int64, |
||
261 | // sqlite3_result_null, |
||
262 | // sqlite3_result_text, |
||
263 | // sqlite3_result_text16, |
||
264 | // sqlite3_result_text16be, |
||
265 | // sqlite3_result_text16le, |
||
266 | // sqlite3_result_value, |
||
267 | // sqlite3_rollback_hook, |
||
268 | // sqlite3_set_authorizer, |
||
269 | // sqlite3_set_auxdata, |
||
270 | // sqlite3_snprintf, |
||
271 | // sqlite3_step, |
||
272 | // sqlite3_table_column_metadata, |
||
273 | #if !SQLITE_OMIT_DEPRECATED |
||
274 | //sqlite3_thread_cleanup, |
||
275 | #else |
||
276 | // 0, |
||
277 | #endif |
||
278 | // sqlite3_total_changes, |
||
279 | // sqlite3_trace, |
||
280 | #if !SQLITE_OMIT_DEPRECATED |
||
281 | //sqlite3_transfer_bindings, |
||
282 | #else |
||
283 | // 0, |
||
284 | #endif |
||
285 | // sqlite3_update_hook, |
||
286 | // sqlite3_user_data, |
||
287 | // sqlite3_value_blob, |
||
288 | // sqlite3_value_bytes, |
||
289 | // sqlite3_value_bytes16, |
||
290 | // sqlite3_value_double, |
||
291 | // sqlite3_value_int, |
||
292 | // sqlite3_value_int64, |
||
293 | // sqlite3_value_numeric_type, |
||
294 | // sqlite3_value_text, |
||
295 | // sqlite3_value_text16, |
||
296 | // sqlite3_value_text16be, |
||
297 | // sqlite3_value_text16le, |
||
298 | // sqlite3_value_type, |
||
299 | // sqlite3_vmprintf, |
||
300 | // /* |
||
301 | // ** The original API set ends here. All extensions can call any |
||
302 | // ** of the APIs above provided that the pointer is not NULL. But |
||
303 | // ** before calling APIs that follow, extension should check the |
||
304 | // ** sqlite3_libversion_number() to make sure they are dealing with |
||
305 | // ** a library that is new enough to support that API. |
||
306 | // ************************************************************************* |
||
307 | // */ |
||
308 | // sqlite3_overload_function, |
||
309 | |||
310 | // /* |
||
311 | // ** Added after 3.3.13 |
||
312 | // */ |
||
313 | // sqlite3_prepare_v2, |
||
314 | // sqlite3_prepare16_v2, |
||
315 | // sqlite3_clear_bindings, |
||
316 | |||
317 | // /* |
||
318 | // ** Added for 3.4.1 |
||
319 | // */ |
||
320 | // sqlite3_create_module_v2, |
||
321 | |||
322 | // /* |
||
323 | // ** Added for 3.5.0 |
||
324 | // */ |
||
325 | // sqlite3_bind_zeroblob, |
||
326 | // sqlite3_blob_bytes, |
||
327 | // sqlite3_blob_close, |
||
328 | // sqlite3_blob_open, |
||
329 | // sqlite3_blob_read, |
||
330 | // sqlite3_blob_write, |
||
331 | // sqlite3_create_collation_v2, |
||
332 | // sqlite3_file_control, |
||
333 | // sqlite3_memory_highwater, |
||
334 | // sqlite3_memory_used, |
||
335 | #if SQLITE_MUTEX_OMIT |
||
336 | // 0, |
||
337 | // 0, |
||
338 | // 0, |
||
339 | // 0, |
||
340 | // 0, |
||
341 | #else |
||
342 | // sqlite3MutexAlloc, |
||
343 | // sqlite3_mutex_enter, |
||
344 | // sqlite3_mutex_free, |
||
345 | // sqlite3_mutex_leave, |
||
346 | // sqlite3_mutex_try, |
||
347 | #endif |
||
348 | // sqlite3_open_v2, |
||
349 | // sqlite3_release_memory, |
||
350 | // sqlite3_result_error_nomem, |
||
351 | // sqlite3_result_error_toobig, |
||
352 | // sqlite3_sleep, |
||
353 | // sqlite3_soft_heap_limit, |
||
354 | // sqlite3_vfs_find, |
||
355 | // sqlite3_vfs_register, |
||
356 | // sqlite3_vfs_unregister, |
||
357 | |||
358 | // /* |
||
359 | // ** Added for 3.5.8 |
||
360 | // */ |
||
361 | // sqlite3_threadsafe, |
||
362 | // sqlite3_result_zeroblob, |
||
363 | // sqlite3_result_error_code, |
||
364 | // sqlite3_test_control, |
||
365 | // sqlite3_randomness, |
||
366 | // sqlite3_context_db_handle, |
||
367 | |||
368 | // /* |
||
369 | // ** Added for 3.6.0 |
||
370 | // */ |
||
371 | // sqlite3_extended_result_codes, |
||
372 | // sqlite3_limit, |
||
373 | // sqlite3_next_stmt, |
||
374 | // sqlite3_sql, |
||
375 | // sqlite3_status, |
||
376 | |||
377 | // /* |
||
378 | // ** Added for 3.7.4 |
||
379 | // */ |
||
380 | // sqlite3_backup_finish, |
||
381 | // sqlite3_backup_init, |
||
382 | // sqlite3_backup_pagecount, |
||
383 | // sqlite3_backup_remaining, |
||
384 | // sqlite3_backup_step, |
||
385 | //#if !SQLITE_OMIT_COMPILEOPTION_DIAGS |
||
386 | // sqlite3_compileoption_get, |
||
387 | // sqlite3_compileoption_used, |
||
388 | //#else |
||
389 | // 0, |
||
390 | // 0, |
||
391 | //#endif |
||
392 | // sqlite3_create_function_v2, |
||
393 | // sqlite3_db_config, |
||
394 | // sqlite3_db_mutex, |
||
395 | // sqlite3_db_status, |
||
396 | // sqlite3_extended_errcode, |
||
397 | // sqlite3_log, |
||
398 | // sqlite3_soft_heap_limit64, |
||
399 | // sqlite3_sourceid, |
||
400 | // sqlite3_stmt_status, |
||
401 | // sqlite3_strnicmp, |
||
402 | //#if SQLITE_ENABLE_UNLOCK_NOTIFY |
||
403 | // sqlite3_unlock_notify, |
||
404 | //#else |
||
405 | // 0, |
||
406 | //#endif |
||
407 | //#if !SQLITE_OMIT_WAL |
||
408 | // sqlite3_wal_autocheckpoint, |
||
409 | // sqlite3_wal_checkpoint, |
||
410 | // sqlite3_wal_hook, |
||
411 | //#else |
||
412 | // 0, |
||
413 | // 0, |
||
414 | // 0, |
||
415 | //#endif |
||
416 | //}; |
||
417 | |||
418 | /* |
||
419 | ** Attempt to load an SQLite extension library contained in the file |
||
420 | ** zFile. The entry point is zProc. zProc may be 0 in which case a |
||
421 | ** default entry point name (sqlite3_extension_init) is used. Use |
||
422 | ** of the default name is recommended. |
||
423 | ** |
||
424 | ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. |
||
425 | ** |
||
426 | ** If an error occurs and pzErrMsg is not 0, then fill pzErrMsg with |
||
427 | ** error message text. The calling function should free this memory |
||
428 | ** by calling sqlite3DbFree(db, ). |
||
429 | */ |
||
430 | static int sqlite3LoadExtension( |
||
431 | sqlite3 db, /* Load the extension into this database connection */ |
||
432 | string zFile, /* Name of the shared library containing extension */ |
||
433 | string zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ |
||
434 | ref string pzErrMsg /* Put error message here if not 0 */ |
||
435 | ) |
||
436 | { |
||
437 | sqlite3_vfs pVfs = db.pVfs; |
||
438 | HANDLE handle; |
||
439 | ////dxInit xInit; //int (*xInit)(sqlite3*,char**,const sqlite3_api_routines); |
||
440 | StringBuilder zErrmsg = new StringBuilder( 100 ); |
||
441 | //object aHandle; |
||
442 | const int nMsg = 300; |
||
443 | if ( pzErrMsg != null ) |
||
444 | pzErrMsg = null; |
||
445 | |||
446 | /* Ticket #1863. To avoid a creating security problems for older |
||
447 | ** applications that relink against newer versions of SQLite, the |
||
448 | ** ability to run load_extension is turned off by default. One |
||
449 | ** must call sqlite3_enable_load_extension() to turn on extension |
||
450 | ** loading. Otherwise you get the following error. |
||
451 | */ |
||
452 | if ( ( db.flags & SQLITE_LoadExtension ) == 0 ) |
||
453 | { |
||
454 | //if( pzErrMsg != null){ |
||
455 | pzErrMsg = sqlite3_mprintf( "not authorized" ); |
||
456 | //} |
||
457 | return SQLITE_ERROR; |
||
458 | } |
||
459 | |||
460 | if ( string.IsNullOrEmpty( zProc ) ) |
||
461 | { |
||
462 | zProc = "sqlite3_extension_init"; |
||
463 | } |
||
464 | |||
465 | handle = sqlite3OsDlOpen( pVfs, zFile ); |
||
466 | if ( handle == IntPtr.Zero ) |
||
467 | { |
||
468 | // if( pzErrMsg ){ |
||
469 | pzErrMsg = string.Empty;//*pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
||
470 | //if( zErrmsg !=null){ |
||
471 | sqlite3_snprintf( nMsg, zErrmsg, |
||
472 | "unable to open shared library [%s]", zFile ); |
||
473 | sqlite3OsDlError( pVfs, nMsg - 1, zErrmsg.ToString() ); |
||
474 | return SQLITE_ERROR; |
||
475 | } |
||
476 | //xInit = (int()(sqlite3*,char**,const sqlite3_api_routines)) |
||
477 | // sqlite3OsDlSym(pVfs, handle, zProc); |
||
478 | dxInit xInit = (dxInit)sqlite3OsDlSym( pVfs, handle, ref zProc ); |
||
479 | Debugger.Break(); // TODO -- |
||
480 | //if( xInit==0 ){ |
||
481 | // if( pzErrMsg ){ |
||
482 | // *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
||
483 | // if( zErrmsg ){ |
||
484 | // sqlite3_snprintf(nMsg, zErrmsg, |
||
485 | // "no entry point [%s] in shared library [%s]", zProc,zFile); |
||
486 | // sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
||
487 | // } |
||
488 | // sqlite3OsDlClose(pVfs, handle); |
||
489 | // } |
||
490 | // return SQLITE_ERROR; |
||
491 | // }else if( xInit(db, ref zErrmsg, sqlite3Apis) ){ |
||
492 | //// if( pzErrMsg !=null){ |
||
493 | // pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); |
||
494 | // //} |
||
495 | // sqlite3DbFree(db,ref zErrmsg); |
||
496 | // sqlite3OsDlClose(pVfs, ref handle); |
||
497 | // return SQLITE_ERROR; |
||
498 | // } |
||
499 | |||
500 | // /* Append the new shared library handle to the db.aExtension array. */ |
||
501 | // aHandle = sqlite3DbMallocZero(db, sizeof(handle)*db.nExtension+1); |
||
502 | // if( aHandle==null ){ |
||
503 | // return SQLITE_NOMEM; |
||
504 | // } |
||
505 | // if( db.nExtension>0 ){ |
||
506 | // memcpy(aHandle, db.aExtension, sizeof(handle)*(db.nExtension)); |
||
507 | // } |
||
508 | // sqlite3DbFree(db,ref db.aExtension); |
||
509 | // db.aExtension = aHandle; |
||
510 | |||
511 | // db.aExtension[db.nExtension++] = handle; |
||
512 | return SQLITE_OK; |
||
513 | } |
||
514 | |||
515 | static public int sqlite3_load_extension( |
||
516 | sqlite3 db, /* Load the extension into this database connection */ |
||
517 | string zFile, /* Name of the shared library containing extension */ |
||
518 | string zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ |
||
519 | ref string pzErrMsg /* Put error message here if not 0 */ |
||
520 | ) |
||
521 | { |
||
522 | int rc; |
||
523 | sqlite3_mutex_enter( db.mutex ); |
||
524 | rc = sqlite3LoadExtension( db, zFile, zProc, ref pzErrMsg ); |
||
525 | rc = sqlite3ApiExit( db, rc ); |
||
526 | sqlite3_mutex_leave( db.mutex ); |
||
527 | return rc; |
||
528 | } |
||
529 | |||
530 | /* |
||
531 | ** Call this routine when the database connection is closing in order |
||
532 | ** to clean up loaded extensions |
||
533 | */ |
||
534 | static void sqlite3CloseExtensions( sqlite3 db ) |
||
535 | { |
||
536 | int i; |
||
537 | Debug.Assert( sqlite3_mutex_held( db.mutex ) ); |
||
538 | for ( i = 0; i < db.nExtension; i++ ) |
||
539 | { |
||
540 | sqlite3OsDlClose( db.pVfs, (HANDLE)db.aExtension[i] ); |
||
541 | } |
||
542 | sqlite3DbFree( db, ref db.aExtension ); |
||
543 | } |
||
544 | |||
545 | /* |
||
546 | ** Enable or disable extension loading. Extension loading is disabled by |
||
547 | ** default so as not to open security holes in older applications. |
||
548 | */ |
||
549 | static public int sqlite3_enable_load_extension( sqlite3 db, int onoff ) |
||
550 | { |
||
551 | sqlite3_mutex_enter( db.mutex ); |
||
552 | if ( onoff != 0 ) |
||
553 | { |
||
554 | db.flags |= SQLITE_LoadExtension; |
||
555 | } |
||
556 | else |
||
557 | { |
||
558 | db.flags &= ~SQLITE_LoadExtension; |
||
559 | } |
||
560 | sqlite3_mutex_leave( db.mutex ); |
||
561 | return SQLITE_OK; |
||
562 | } |
||
563 | |||
564 | #endif //* SQLITE_OMIT_LOAD_EXTENSION */ |
||
565 | |||
566 | /* |
||
567 | ** The auto-extension code added regardless of whether or not extension |
||
568 | ** loading is supported. We need a dummy sqlite3Apis pointer for that |
||
569 | ** code if regular extension loading is not available. This is that |
||
570 | ** dummy pointer. |
||
571 | */ |
||
572 | #if SQLITE_OMIT_LOAD_EXTENSION |
||
573 | const sqlite3_api_routines sqlite3Apis = null; |
||
574 | #endif |
||
575 | |||
576 | |||
577 | /* |
||
578 | ** The following object holds the list of automatically loaded |
||
579 | ** extensions. |
||
580 | ** |
||
581 | ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER |
||
582 | ** mutex must be held while accessing this list. |
||
583 | */ |
||
584 | //typedef struct sqlite3AutoExtList sqlite3AutoExtList; |
||
585 | public class sqlite3AutoExtList |
||
586 | { |
||
587 | public int nExt = 0; /* Number of entries in aExt[] */ |
||
588 | public dxInit[] aExt = null; /* Pointers to the extension init functions */ |
||
589 | public sqlite3AutoExtList( int nExt, dxInit[] aExt ) |
||
590 | { |
||
591 | this.nExt = nExt; |
||
592 | this.aExt = aExt; |
||
593 | } |
||
594 | } |
||
595 | static sqlite3AutoExtList sqlite3Autoext = new sqlite3AutoExtList( 0, null ); |
||
596 | /* The "wsdAutoext" macro will resolve to the autoextension |
||
597 | ** state vector. If writable static data is unsupported on the target, |
||
598 | ** we have to locate the state vector at run-time. In the more common |
||
599 | ** case where writable static data is supported, wsdStat can refer directly |
||
600 | ** to the "sqlite3Autoext" state vector declared above. |
||
601 | */ |
||
602 | #if SQLITE_OMIT_WSD |
||
603 | //# define wsdAutoextInit \ |
||
604 | sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext) |
||
605 | //# define wsdAutoext x[0] |
||
606 | #else |
||
607 | //# define wsdAutoextInit |
||
608 | static void wsdAutoextInit() |
||
609 | { |
||
610 | } |
||
611 | //# define wsdAutoext sqlite3Autoext |
||
612 | static sqlite3AutoExtList wsdAutoext = sqlite3Autoext; |
||
613 | #endif |
||
614 | |||
615 | /* |
||
616 | ** Register a statically linked extension that is automatically |
||
617 | ** loaded by every new database connection. |
||
618 | */ |
||
619 | static int sqlite3_auto_extension( dxInit xInit ) |
||
620 | { |
||
621 | int rc = SQLITE_OK; |
||
622 | #if !SQLITE_OMIT_AUTOINIT |
||
623 | rc = sqlite3_initialize(); |
||
624 | if ( rc != 0 ) |
||
625 | { |
||
626 | return rc; |
||
627 | } |
||
628 | else |
||
629 | #endif |
||
630 | { |
||
631 | int i; |
||
632 | #if SQLITE_THREADSAFE |
||
633 | sqlite3_mutex mutex = sqlite3MutexAlloc( SQLITE_MUTEX_STATIC_MASTER ); |
||
634 | #else |
||
635 | sqlite3_mutex mutex = sqlite3MutexAlloc( SQLITE_MUTEX_STATIC_MASTER ); |
||
636 | #endif |
||
637 | wsdAutoextInit(); |
||
638 | sqlite3_mutex_enter( mutex ); |
||
639 | for ( i = 0; i < wsdAutoext.nExt; i++ ) |
||
640 | { |
||
641 | if ( wsdAutoext.aExt[i] == xInit ) |
||
642 | break; |
||
643 | } |
||
644 | //if( i==wsdAutoext.nExt ){ |
||
645 | // int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); |
||
646 | // void **aNew; |
||
647 | // aNew = sqlite3_realloc(wsdAutoext.aExt, nByte); |
||
648 | // if( aNew==0 ){ |
||
649 | // rc = SQLITE_NOMEM; |
||
650 | // }else{ |
||
651 | Array.Resize( ref wsdAutoext.aExt, wsdAutoext.nExt + 1 );// wsdAutoext.aExt = aNew; |
||
652 | wsdAutoext.aExt[wsdAutoext.nExt] = xInit; |
||
653 | wsdAutoext.nExt++; |
||
654 | //} |
||
655 | sqlite3_mutex_leave( mutex ); |
||
656 | Debug.Assert( ( rc & 0xff ) == rc ); |
||
657 | return rc; |
||
658 | } |
||
659 | } |
||
660 | |||
661 | /* |
||
662 | ** Reset the automatic extension loading mechanism. |
||
663 | */ |
||
664 | static void sqlite3_reset_auto_extension() |
||
665 | { |
||
666 | #if !SQLITE_OMIT_AUTOINIT |
||
667 | if ( sqlite3_initialize() == SQLITE_OK ) |
||
668 | #endif |
||
669 | { |
||
670 | #if SQLITE_THREADSAFE |
||
671 | sqlite3_mutex mutex = sqlite3MutexAlloc( SQLITE_MUTEX_STATIC_MASTER ); |
||
672 | #else |
||
673 | sqlite3_mutex mutex = sqlite3MutexAlloc( SQLITE_MUTEX_STATIC_MASTER ); |
||
674 | #endif |
||
675 | wsdAutoextInit(); |
||
676 | sqlite3_mutex_enter( mutex ); |
||
677 | #if SQLITE_OMIT_WSD |
||
678 | //sqlite3_free( ref wsdAutoext.aExt ); |
||
679 | wsdAutoext.aExt = null; |
||
680 | wsdAutoext.nExt = 0; |
||
681 | #else |
||
682 | //sqlite3_free( ref sqlite3Autoext.aExt ); |
||
683 | sqlite3Autoext.aExt = null; |
||
684 | sqlite3Autoext.nExt = 0; |
||
685 | #endif |
||
686 | sqlite3_mutex_leave( mutex ); |
||
687 | } |
||
688 | } |
||
689 | |||
690 | /* |
||
691 | ** Load all automatic extensions. |
||
692 | ** |
||
693 | ** If anything goes wrong, set an error in the database connection. |
||
694 | */ |
||
695 | static void sqlite3AutoLoadExtensions( sqlite3 db ) |
||
696 | { |
||
697 | int i; |
||
698 | bool go = true; |
||
699 | dxInit xInit;//)(sqlite3*,char**,const sqlite3_api_routines); |
||
700 | |||
701 | wsdAutoextInit(); |
||
702 | #if SQLITE_OMIT_WSD |
||
703 | if ( wsdAutoext.nExt == 0 ) |
||
704 | #else |
||
705 | if ( sqlite3Autoext.nExt == 0 ) |
||
706 | #endif |
||
707 | { |
||
708 | /* Common case: early out without every having to acquire a mutex */ |
||
709 | return; |
||
710 | } |
||
711 | for ( i = 0; go; i++ ) |
||
712 | { |
||
713 | string zErrmsg = string.Empty; |
||
714 | #if SQLITE_THREADSAFE |
||
715 | sqlite3_mutex mutex = sqlite3MutexAlloc( SQLITE_MUTEX_STATIC_MASTER ); |
||
716 | #else |
||
717 | sqlite3_mutex mutex = sqlite3MutexAlloc( SQLITE_MUTEX_STATIC_MASTER ); |
||
718 | #endif |
||
719 | sqlite3_mutex_enter( mutex ); |
||
720 | if ( i >= wsdAutoext.nExt ) |
||
721 | { |
||
722 | xInit = null; |
||
723 | go = false; |
||
724 | } |
||
725 | else |
||
726 | { |
||
727 | xInit = (dxInit) |
||
728 | wsdAutoext.aExt[i]; |
||
729 | } |
||
730 | sqlite3_mutex_leave( mutex ); |
||
731 | if ( xInit != null && xInit( db, ref zErrmsg, (sqlite3_api_routines)sqlite3Apis ) != 0 ) |
||
732 | { |
||
733 | sqlite3Error( db, SQLITE_ERROR, |
||
734 | "automatic extension loading failed: %s", zErrmsg ); |
||
735 | go = false; |
||
736 | } |
||
737 | sqlite3DbFree( db, ref zErrmsg ); |
||
738 | } |
||
739 | } |
||
740 | } |
||
741 | } |
||
742 |