drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2006 June 7 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code used to dynamically load extensions into |
| 13 | ** the SQLite library. |
| 14 | */ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 15 | |
danielk1977 | 7c9aaa7 | 2008-01-01 05:49:07 +0000 | [diff] [blame] | 16 | #ifndef SQLITE_CORE |
| 17 | #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ |
| 18 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 19 | #include "sqlite3ext.h" |
drh | f1952c5 | 2006-06-08 15:48:00 +0000 | [diff] [blame] | 20 | #include "sqliteInt.h" |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 21 | #include <string.h> |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 22 | |
mlcreech | 9323f76 | 2008-03-19 23:52:34 +0000 | [diff] [blame] | 23 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 24 | /* |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 25 | ** Some API routines are omitted when various features are |
| 26 | ** excluded from a build of SQLite. Substitute a NULL pointer |
| 27 | ** for any missing APIs. |
| 28 | */ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 29 | #ifndef SQLITE_ENABLE_COLUMN_METADATA |
| 30 | # define sqlite3_column_database_name 0 |
| 31 | # define sqlite3_column_database_name16 0 |
| 32 | # define sqlite3_column_table_name 0 |
| 33 | # define sqlite3_column_table_name16 0 |
| 34 | # define sqlite3_column_origin_name 0 |
| 35 | # define sqlite3_column_origin_name16 0 |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 38 | #ifdef SQLITE_OMIT_AUTHORIZATION |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 39 | # define sqlite3_set_authorizer 0 |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #ifdef SQLITE_OMIT_UTF16 |
drh | af30469 | 2007-04-23 23:56:31 +0000 | [diff] [blame] | 43 | # define sqlite3_bind_text16 0 |
| 44 | # define sqlite3_collation_needed16 0 |
| 45 | # define sqlite3_column_decltype16 0 |
| 46 | # define sqlite3_column_name16 0 |
| 47 | # define sqlite3_column_text16 0 |
| 48 | # define sqlite3_complete16 0 |
| 49 | # define sqlite3_create_collation16 0 |
| 50 | # define sqlite3_create_function16 0 |
| 51 | # define sqlite3_errmsg16 0 |
| 52 | # define sqlite3_open16 0 |
| 53 | # define sqlite3_prepare16 0 |
| 54 | # define sqlite3_prepare16_v2 0 |
| 55 | # define sqlite3_result_error16 0 |
| 56 | # define sqlite3_result_text16 0 |
| 57 | # define sqlite3_result_text16be 0 |
| 58 | # define sqlite3_result_text16le 0 |
| 59 | # define sqlite3_value_text16 0 |
| 60 | # define sqlite3_value_text16be 0 |
| 61 | # define sqlite3_value_text16le 0 |
| 62 | # define sqlite3_column_database_name16 0 |
| 63 | # define sqlite3_column_table_name16 0 |
| 64 | # define sqlite3_column_origin_name16 0 |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | #ifdef SQLITE_OMIT_COMPLETE |
| 68 | # define sqlite3_complete 0 |
| 69 | # define sqlite3_complete16 0 |
| 70 | #endif |
| 71 | |
dan | bb2b441 | 2011-04-06 17:54:31 +0000 | [diff] [blame] | 72 | #ifdef SQLITE_OMIT_DECLTYPE |
| 73 | # define sqlite3_column_decltype16 0 |
| 74 | # define sqlite3_column_decltype 0 |
| 75 | #endif |
| 76 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 77 | #ifdef SQLITE_OMIT_PROGRESS_CALLBACK |
| 78 | # define sqlite3_progress_handler 0 |
| 79 | #endif |
| 80 | |
| 81 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 82 | # define sqlite3_create_module 0 |
danielk1977 | b7865fb | 2007-06-27 11:10:03 +0000 | [diff] [blame] | 83 | # define sqlite3_create_module_v2 0 |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 84 | # define sqlite3_declare_vtab 0 |
dan | 0ea2517 | 2011-06-28 07:15:43 +0000 | [diff] [blame] | 85 | # define sqlite3_vtab_config 0 |
| 86 | # define sqlite3_vtab_on_conflict 0 |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 87 | #endif |
| 88 | |
drh | e31a1fb | 2007-01-26 13:08:24 +0000 | [diff] [blame] | 89 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 90 | # define sqlite3_enable_shared_cache 0 |
| 91 | #endif |
| 92 | |
drh | 087ec07 | 2016-07-25 00:05:56 +0000 | [diff] [blame] | 93 | #if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED) |
drh | e31a1fb | 2007-01-26 13:08:24 +0000 | [diff] [blame] | 94 | # define sqlite3_profile 0 |
| 95 | # define sqlite3_trace 0 |
| 96 | #endif |
| 97 | |
| 98 | #ifdef SQLITE_OMIT_GET_TABLE |
| 99 | # define sqlite3_free_table 0 |
| 100 | # define sqlite3_get_table 0 |
| 101 | #endif |
| 102 | |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 103 | #ifdef SQLITE_OMIT_INCRBLOB |
| 104 | #define sqlite3_bind_zeroblob 0 |
| 105 | #define sqlite3_blob_bytes 0 |
| 106 | #define sqlite3_blob_close 0 |
| 107 | #define sqlite3_blob_open 0 |
| 108 | #define sqlite3_blob_read 0 |
| 109 | #define sqlite3_blob_write 0 |
dan | 0ea2517 | 2011-06-28 07:15:43 +0000 | [diff] [blame] | 110 | #define sqlite3_blob_reopen 0 |
danielk1977 | a15db35 | 2007-09-14 16:20:00 +0000 | [diff] [blame] | 111 | #endif |
| 112 | |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 113 | #if defined(SQLITE_OMIT_TRACE) |
| 114 | # define sqlite3_trace_v2 0 |
| 115 | #endif |
| 116 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 117 | /* |
| 118 | ** The following structure contains pointers to all SQLite API routines. |
| 119 | ** A pointer to this structure is passed into extensions when they are |
| 120 | ** loaded so that the extension can make calls back into the SQLite |
| 121 | ** library. |
| 122 | ** |
| 123 | ** When adding new APIs, add them to the bottom of this structure |
| 124 | ** in order to preserve backwards compatibility. |
| 125 | ** |
| 126 | ** Extensions that use newer APIs should first call the |
| 127 | ** sqlite3_libversion_number() to make sure that the API they |
| 128 | ** intend to use is supported by the library. Extensions should |
| 129 | ** also check to make sure that the pointer to the function is |
| 130 | ** not NULL before calling it. |
| 131 | */ |
drh | de24478 | 2008-04-10 16:01:10 +0000 | [diff] [blame] | 132 | static const sqlite3_api_routines sqlite3Apis = { |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 133 | sqlite3_aggregate_context, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 134 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 135 | sqlite3_aggregate_count, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 136 | #else |
| 137 | 0, |
| 138 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 139 | sqlite3_bind_blob, |
| 140 | sqlite3_bind_double, |
| 141 | sqlite3_bind_int, |
| 142 | sqlite3_bind_int64, |
| 143 | sqlite3_bind_null, |
| 144 | sqlite3_bind_parameter_count, |
| 145 | sqlite3_bind_parameter_index, |
| 146 | sqlite3_bind_parameter_name, |
| 147 | sqlite3_bind_text, |
| 148 | sqlite3_bind_text16, |
drh | c1be632 | 2006-06-27 00:14:27 +0000 | [diff] [blame] | 149 | sqlite3_bind_value, |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 150 | sqlite3_busy_handler, |
| 151 | sqlite3_busy_timeout, |
| 152 | sqlite3_changes, |
| 153 | sqlite3_close, |
| 154 | sqlite3_collation_needed, |
| 155 | sqlite3_collation_needed16, |
| 156 | sqlite3_column_blob, |
| 157 | sqlite3_column_bytes, |
| 158 | sqlite3_column_bytes16, |
| 159 | sqlite3_column_count, |
| 160 | sqlite3_column_database_name, |
| 161 | sqlite3_column_database_name16, |
| 162 | sqlite3_column_decltype, |
| 163 | sqlite3_column_decltype16, |
| 164 | sqlite3_column_double, |
| 165 | sqlite3_column_int, |
| 166 | sqlite3_column_int64, |
| 167 | sqlite3_column_name, |
| 168 | sqlite3_column_name16, |
| 169 | sqlite3_column_origin_name, |
| 170 | sqlite3_column_origin_name16, |
| 171 | sqlite3_column_table_name, |
| 172 | sqlite3_column_table_name16, |
| 173 | sqlite3_column_text, |
| 174 | sqlite3_column_text16, |
| 175 | sqlite3_column_type, |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 176 | sqlite3_column_value, |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 177 | sqlite3_commit_hook, |
| 178 | sqlite3_complete, |
| 179 | sqlite3_complete16, |
| 180 | sqlite3_create_collation, |
| 181 | sqlite3_create_collation16, |
| 182 | sqlite3_create_function, |
| 183 | sqlite3_create_function16, |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 184 | sqlite3_create_module, |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 185 | sqlite3_data_count, |
| 186 | sqlite3_db_handle, |
danielk1977 | d6e8dd0 | 2006-06-15 15:38:41 +0000 | [diff] [blame] | 187 | sqlite3_declare_vtab, |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 188 | sqlite3_enable_shared_cache, |
| 189 | sqlite3_errcode, |
| 190 | sqlite3_errmsg, |
| 191 | sqlite3_errmsg16, |
| 192 | sqlite3_exec, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 193 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 194 | sqlite3_expired, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 195 | #else |
| 196 | 0, |
| 197 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 198 | sqlite3_finalize, |
| 199 | sqlite3_free, |
| 200 | sqlite3_free_table, |
| 201 | sqlite3_get_autocommit, |
| 202 | sqlite3_get_auxdata, |
| 203 | sqlite3_get_table, |
drh | e31a1fb | 2007-01-26 13:08:24 +0000 | [diff] [blame] | 204 | 0, /* Was sqlite3_global_recover(), but that function is deprecated */ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 205 | sqlite3_interrupt, |
| 206 | sqlite3_last_insert_rowid, |
| 207 | sqlite3_libversion, |
| 208 | sqlite3_libversion_number, |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 209 | sqlite3_malloc, |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 210 | sqlite3_mprintf, |
| 211 | sqlite3_open, |
| 212 | sqlite3_open16, |
| 213 | sqlite3_prepare, |
| 214 | sqlite3_prepare16, |
| 215 | sqlite3_profile, |
| 216 | sqlite3_progress_handler, |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 217 | sqlite3_realloc, |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 218 | sqlite3_reset, |
| 219 | sqlite3_result_blob, |
| 220 | sqlite3_result_double, |
| 221 | sqlite3_result_error, |
| 222 | sqlite3_result_error16, |
| 223 | sqlite3_result_int, |
| 224 | sqlite3_result_int64, |
| 225 | sqlite3_result_null, |
| 226 | sqlite3_result_text, |
| 227 | sqlite3_result_text16, |
| 228 | sqlite3_result_text16be, |
| 229 | sqlite3_result_text16le, |
| 230 | sqlite3_result_value, |
| 231 | sqlite3_rollback_hook, |
| 232 | sqlite3_set_authorizer, |
| 233 | sqlite3_set_auxdata, |
| 234 | sqlite3_snprintf, |
| 235 | sqlite3_step, |
| 236 | sqlite3_table_column_metadata, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 237 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 238 | sqlite3_thread_cleanup, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 239 | #else |
| 240 | 0, |
| 241 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 242 | sqlite3_total_changes, |
| 243 | sqlite3_trace, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 244 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 245 | sqlite3_transfer_bindings, |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 246 | #else |
| 247 | 0, |
| 248 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 249 | sqlite3_update_hook, |
| 250 | sqlite3_user_data, |
| 251 | sqlite3_value_blob, |
| 252 | sqlite3_value_bytes, |
| 253 | sqlite3_value_bytes16, |
| 254 | sqlite3_value_double, |
| 255 | sqlite3_value_int, |
| 256 | sqlite3_value_int64, |
| 257 | sqlite3_value_numeric_type, |
| 258 | sqlite3_value_text, |
| 259 | sqlite3_value_text16, |
| 260 | sqlite3_value_text16be, |
| 261 | sqlite3_value_text16le, |
| 262 | sqlite3_value_type, |
| 263 | sqlite3_vmprintf, |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 264 | /* |
| 265 | ** The original API set ends here. All extensions can call any |
| 266 | ** of the APIs above provided that the pointer is not NULL. But |
| 267 | ** before calling APIs that follow, extension should check the |
| 268 | ** sqlite3_libversion_number() to make sure they are dealing with |
| 269 | ** a library that is new enough to support that API. |
| 270 | ************************************************************************* |
| 271 | */ |
shess | 7409310 | 2006-09-22 23:38:21 +0000 | [diff] [blame] | 272 | sqlite3_overload_function, |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | ** Added after 3.3.13 |
| 276 | */ |
| 277 | sqlite3_prepare_v2, |
| 278 | sqlite3_prepare16_v2, |
drh | a92993c | 2007-03-29 18:46:00 +0000 | [diff] [blame] | 279 | sqlite3_clear_bindings, |
drh | 6bf0ae7 | 2007-07-20 10:33:58 +0000 | [diff] [blame] | 280 | |
| 281 | /* |
| 282 | ** Added for 3.4.1 |
| 283 | */ |
| 284 | sqlite3_create_module_v2, |
| 285 | |
drh | 428e282 | 2007-08-30 16:23:19 +0000 | [diff] [blame] | 286 | /* |
| 287 | ** Added for 3.5.0 |
| 288 | */ |
| 289 | sqlite3_bind_zeroblob, |
| 290 | sqlite3_blob_bytes, |
| 291 | sqlite3_blob_close, |
| 292 | sqlite3_blob_open, |
| 293 | sqlite3_blob_read, |
| 294 | sqlite3_blob_write, |
| 295 | sqlite3_create_collation_v2, |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 296 | sqlite3_file_control, |
drh | 428e282 | 2007-08-30 16:23:19 +0000 | [diff] [blame] | 297 | sqlite3_memory_highwater, |
| 298 | sqlite3_memory_used, |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 299 | #ifdef SQLITE_MUTEX_OMIT |
drh | ad3e78b | 2007-08-30 20:09:27 +0000 | [diff] [blame] | 300 | 0, |
| 301 | 0, |
| 302 | 0, |
| 303 | 0, |
| 304 | 0, |
| 305 | #else |
drh | 61f6dc6 | 2007-08-30 17:15:37 +0000 | [diff] [blame] | 306 | sqlite3_mutex_alloc, |
| 307 | sqlite3_mutex_enter, |
| 308 | sqlite3_mutex_free, |
| 309 | sqlite3_mutex_leave, |
| 310 | sqlite3_mutex_try, |
drh | ad3e78b | 2007-08-30 20:09:27 +0000 | [diff] [blame] | 311 | #endif |
drh | 428e282 | 2007-08-30 16:23:19 +0000 | [diff] [blame] | 312 | sqlite3_open_v2, |
| 313 | sqlite3_release_memory, |
| 314 | sqlite3_result_error_nomem, |
| 315 | sqlite3_result_error_toobig, |
| 316 | sqlite3_sleep, |
| 317 | sqlite3_soft_heap_limit, |
| 318 | sqlite3_vfs_find, |
| 319 | sqlite3_vfs_register, |
| 320 | sqlite3_vfs_unregister, |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | ** Added for 3.5.8 |
| 324 | */ |
drh | e5dd20a | 2008-03-19 19:55:55 +0000 | [diff] [blame] | 325 | sqlite3_threadsafe, |
| 326 | sqlite3_result_zeroblob, |
| 327 | sqlite3_result_error_code, |
| 328 | sqlite3_test_control, |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 329 | sqlite3_randomness, |
drh | fa4a4b9 | 2008-03-19 21:45:51 +0000 | [diff] [blame] | 330 | sqlite3_context_db_handle, |
drh | 8b6abed | 2008-06-19 15:06:24 +0000 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | ** Added for 3.6.0 |
| 334 | */ |
| 335 | sqlite3_extended_result_codes, |
| 336 | sqlite3_limit, |
| 337 | sqlite3_next_stmt, |
| 338 | sqlite3_sql, |
| 339 | sqlite3_status, |
drh | fd7ebbf | 2010-10-11 13:12:04 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | ** Added for 3.7.4 |
| 343 | */ |
| 344 | sqlite3_backup_finish, |
| 345 | sqlite3_backup_init, |
| 346 | sqlite3_backup_pagecount, |
| 347 | sqlite3_backup_remaining, |
| 348 | sqlite3_backup_step, |
drh | 4e49c77 | 2010-10-11 17:57:41 +0000 | [diff] [blame] | 349 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
drh | fd7ebbf | 2010-10-11 13:12:04 +0000 | [diff] [blame] | 350 | sqlite3_compileoption_get, |
| 351 | sqlite3_compileoption_used, |
drh | 4e49c77 | 2010-10-11 17:57:41 +0000 | [diff] [blame] | 352 | #else |
| 353 | 0, |
| 354 | 0, |
| 355 | #endif |
drh | fd7ebbf | 2010-10-11 13:12:04 +0000 | [diff] [blame] | 356 | sqlite3_create_function_v2, |
| 357 | sqlite3_db_config, |
| 358 | sqlite3_db_mutex, |
| 359 | sqlite3_db_status, |
| 360 | sqlite3_extended_errcode, |
| 361 | sqlite3_log, |
| 362 | sqlite3_soft_heap_limit64, |
| 363 | sqlite3_sourceid, |
| 364 | sqlite3_stmt_status, |
| 365 | sqlite3_strnicmp, |
drh | 4e49c77 | 2010-10-11 17:57:41 +0000 | [diff] [blame] | 366 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
drh | fd7ebbf | 2010-10-11 13:12:04 +0000 | [diff] [blame] | 367 | sqlite3_unlock_notify, |
drh | 4e49c77 | 2010-10-11 17:57:41 +0000 | [diff] [blame] | 368 | #else |
| 369 | 0, |
| 370 | #endif |
| 371 | #ifndef SQLITE_OMIT_WAL |
drh | fd7ebbf | 2010-10-11 13:12:04 +0000 | [diff] [blame] | 372 | sqlite3_wal_autocheckpoint, |
| 373 | sqlite3_wal_checkpoint, |
| 374 | sqlite3_wal_hook, |
drh | 4e49c77 | 2010-10-11 17:57:41 +0000 | [diff] [blame] | 375 | #else |
| 376 | 0, |
| 377 | 0, |
| 378 | 0, |
| 379 | #endif |
dan | 0ea2517 | 2011-06-28 07:15:43 +0000 | [diff] [blame] | 380 | sqlite3_blob_reopen, |
| 381 | sqlite3_vtab_config, |
| 382 | sqlite3_vtab_on_conflict, |
drh | 1d59d03 | 2013-03-01 23:40:26 +0000 | [diff] [blame] | 383 | sqlite3_close_v2, |
| 384 | sqlite3_db_filename, |
| 385 | sqlite3_db_readonly, |
| 386 | sqlite3_db_release_memory, |
| 387 | sqlite3_errstr, |
| 388 | sqlite3_stmt_busy, |
| 389 | sqlite3_stmt_readonly, |
| 390 | sqlite3_stricmp, |
| 391 | sqlite3_uri_boolean, |
| 392 | sqlite3_uri_int64, |
| 393 | sqlite3_uri_parameter, |
| 394 | sqlite3_vsnprintf, |
drh | 0807cc2 | 2014-09-09 18:41:32 +0000 | [diff] [blame] | 395 | sqlite3_wal_checkpoint_v2, |
| 396 | /* Version 3.8.7 and later */ |
| 397 | sqlite3_auto_extension, |
| 398 | sqlite3_bind_blob64, |
drh | bbf483f | 2014-09-09 20:30:24 +0000 | [diff] [blame] | 399 | sqlite3_bind_text64, |
drh | 0807cc2 | 2014-09-09 18:41:32 +0000 | [diff] [blame] | 400 | sqlite3_cancel_auto_extension, |
| 401 | sqlite3_load_extension, |
| 402 | sqlite3_malloc64, |
| 403 | sqlite3_msize, |
| 404 | sqlite3_realloc64, |
| 405 | sqlite3_reset_auto_extension, |
| 406 | sqlite3_result_blob64, |
drh | bbf483f | 2014-09-09 20:30:24 +0000 | [diff] [blame] | 407 | sqlite3_result_text64, |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 408 | sqlite3_strglob, |
| 409 | /* Version 3.8.11 and later */ |
| 410 | (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup, |
drh | 6bacdc2 | 2015-07-24 17:14:03 +0000 | [diff] [blame] | 411 | sqlite3_value_free, |
dan | 80c0302 | 2015-07-24 17:36:34 +0000 | [diff] [blame] | 412 | sqlite3_result_zeroblob64, |
drh | cf94f17 | 2015-09-10 20:40:21 +0000 | [diff] [blame] | 413 | sqlite3_bind_zeroblob64, |
drh | 58a8a92 | 2015-10-12 04:56:12 +0000 | [diff] [blame] | 414 | /* Version 3.9.0 and later */ |
drh | cf94f17 | 2015-09-10 20:40:21 +0000 | [diff] [blame] | 415 | sqlite3_value_subtype, |
drh | 7be53fe | 2015-12-03 13:43:07 +0000 | [diff] [blame] | 416 | sqlite3_result_subtype, |
| 417 | /* Version 3.10.0 and later */ |
| 418 | sqlite3_status64, |
| 419 | sqlite3_strlike, |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 420 | sqlite3_db_cacheflush, |
| 421 | /* Version 3.12.0 and later */ |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 422 | sqlite3_system_errno, |
| 423 | /* Version 3.14.0 and later */ |
| 424 | sqlite3_trace_v2, |
| 425 | sqlite3_expanded_sql |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 426 | }; |
| 427 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 428 | /* |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 429 | ** Attempt to load an SQLite extension library contained in the file |
drh | 428397c | 2006-06-17 13:21:32 +0000 | [diff] [blame] | 430 | ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| 431 | ** default entry point name (sqlite3_extension_init) is used. Use |
| 432 | ** of the default name is recommended. |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 433 | ** |
| 434 | ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. |
| 435 | ** |
| 436 | ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with |
| 437 | ** error message text. The calling function should free this memory |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 438 | ** by calling sqlite3DbFree(db, ). |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 439 | */ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 440 | static int sqlite3LoadExtension( |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 441 | sqlite3 *db, /* Load the extension into this database connection */ |
| 442 | const char *zFile, /* Name of the shared library containing extension */ |
drh | 428397c | 2006-06-17 13:21:32 +0000 | [diff] [blame] | 443 | const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 444 | char **pzErrMsg /* Put error message here if not 0 */ |
| 445 | ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 446 | sqlite3_vfs *pVfs = db->pVfs; |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 447 | void *handle; |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 448 | sqlite3_loadext_entry xInit; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 449 | char *zErrmsg = 0; |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 450 | const char *zEntry; |
| 451 | char *zAltEntry = 0; |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 452 | void **aHandle; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 453 | u64 nMsg = 300 + sqlite3Strlen30(zFile); |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 454 | int ii; |
drh | c1502e2 | 2016-05-28 17:23:08 +0000 | [diff] [blame] | 455 | int rc; |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 456 | |
| 457 | /* Shared library endings to try if zFile cannot be loaded as written */ |
| 458 | static const char *azEndings[] = { |
| 459 | #if SQLITE_OS_WIN |
| 460 | "dll" |
| 461 | #elif defined(__APPLE__) |
| 462 | "dylib" |
| 463 | #else |
| 464 | "so" |
| 465 | #endif |
| 466 | }; |
| 467 | |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 468 | |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 469 | if( pzErrMsg ) *pzErrMsg = 0; |
drh | 191dd06 | 2016-04-21 01:30:09 +0000 | [diff] [blame] | 470 | |
| 471 | /* Ticket #1863. To avoid a creating security problems for older |
| 472 | ** applications that relink against newer versions of SQLite, the |
| 473 | ** ability to run load_extension is turned off by default. One |
| 474 | ** must call either sqlite3_enable_load_extension(db) or |
| 475 | ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0) |
| 476 | ** to turn on extension loading. |
| 477 | */ |
| 478 | if( (db->flags & SQLITE_LoadExtension)==0 ){ |
| 479 | if( pzErrMsg ){ |
| 480 | *pzErrMsg = sqlite3_mprintf("not authorized"); |
| 481 | } |
| 482 | return SQLITE_ERROR; |
| 483 | } |
| 484 | |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 485 | zEntry = zProc ? zProc : "sqlite3_extension_init"; |
drh | 191dd06 | 2016-04-21 01:30:09 +0000 | [diff] [blame] | 486 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 487 | handle = sqlite3OsDlOpen(pVfs, zFile); |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 488 | #if SQLITE_OS_UNIX || SQLITE_OS_WIN |
| 489 | for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){ |
| 490 | char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 491 | if( zAltFile==0 ) return SQLITE_NOMEM_BKPT; |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 492 | handle = sqlite3OsDlOpen(pVfs, zAltFile); |
| 493 | sqlite3_free(zAltFile); |
| 494 | } |
| 495 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 496 | if( handle==0 ){ |
| 497 | if( pzErrMsg ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 498 | *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 499 | if( zErrmsg ){ |
| 500 | sqlite3_snprintf(nMsg, zErrmsg, |
| 501 | "unable to open shared library [%s]", zFile); |
| 502 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 503 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 504 | } |
| 505 | return SQLITE_ERROR; |
| 506 | } |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 507 | xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 508 | |
| 509 | /* If no entry point was specified and the default legacy |
| 510 | ** entry point name "sqlite3_extension_init" was not found, then |
| 511 | ** construct an entry point name "sqlite3_X_init" where the X is |
| 512 | ** replaced by the lowercase value of every ASCII alphabetic |
| 513 | ** character in the filename after the last "/" upto the first ".", |
| 514 | ** and eliding the first three characters if they are "lib". |
| 515 | ** Examples: |
| 516 | ** |
| 517 | ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init |
| 518 | ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init |
| 519 | */ |
| 520 | if( xInit==0 && zProc==0 ){ |
| 521 | int iFile, iEntry, c; |
| 522 | int ncFile = sqlite3Strlen30(zFile); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 523 | zAltEntry = sqlite3_malloc64(ncFile+30); |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 524 | if( zAltEntry==0 ){ |
| 525 | sqlite3OsDlClose(pVfs, handle); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 526 | return SQLITE_NOMEM_BKPT; |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 527 | } |
| 528 | memcpy(zAltEntry, "sqlite3_", 8); |
| 529 | for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} |
| 530 | iFile++; |
| 531 | if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; |
| 532 | for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ |
| 533 | if( sqlite3Isalpha(c) ){ |
| 534 | zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; |
| 535 | } |
| 536 | } |
| 537 | memcpy(zAltEntry+iEntry, "_init", 6); |
| 538 | zEntry = zAltEntry; |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 539 | xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 540 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 541 | if( xInit==0 ){ |
| 542 | if( pzErrMsg ){ |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 543 | nMsg += sqlite3Strlen30(zEntry); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 544 | *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 545 | if( zErrmsg ){ |
| 546 | sqlite3_snprintf(nMsg, zErrmsg, |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 547 | "no entry point [%s] in shared library [%s]", zEntry, zFile); |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 548 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
drh | 50d654d | 2009-06-03 01:24:54 +0000 | [diff] [blame] | 549 | } |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 550 | } |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 551 | sqlite3OsDlClose(pVfs, handle); |
| 552 | sqlite3_free(zAltEntry); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 553 | return SQLITE_ERROR; |
drh | c288e44 | 2013-04-18 22:56:42 +0000 | [diff] [blame] | 554 | } |
| 555 | sqlite3_free(zAltEntry); |
drh | c1502e2 | 2016-05-28 17:23:08 +0000 | [diff] [blame] | 556 | rc = xInit(db, &zErrmsg, &sqlite3Apis); |
| 557 | if( rc ){ |
| 558 | if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 559 | if( pzErrMsg ){ |
| 560 | *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); |
| 561 | } |
| 562 | sqlite3_free(zErrmsg); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 563 | sqlite3OsDlClose(pVfs, handle); |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 564 | return SQLITE_ERROR; |
| 565 | } |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 566 | |
| 567 | /* Append the new shared library handle to the db->aExtension array. */ |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 568 | aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 569 | if( aHandle==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 570 | return SQLITE_NOMEM_BKPT; |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 571 | } |
| 572 | if( db->nExtension>0 ){ |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 573 | memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 574 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 575 | sqlite3DbFree(db, db->aExtension); |
danielk1977 | 69e777f | 2006-06-14 10:38:02 +0000 | [diff] [blame] | 576 | db->aExtension = aHandle; |
| 577 | |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 578 | db->aExtension[db->nExtension++] = handle; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 579 | return SQLITE_OK; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 580 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 581 | int sqlite3_load_extension( |
| 582 | sqlite3 *db, /* Load the extension into this database connection */ |
| 583 | const char *zFile, /* Name of the shared library containing extension */ |
| 584 | const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ |
| 585 | char **pzErrMsg /* Put error message here if not 0 */ |
| 586 | ){ |
| 587 | int rc; |
| 588 | sqlite3_mutex_enter(db->mutex); |
| 589 | rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg); |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 590 | rc = sqlite3ApiExit(db, rc); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 591 | sqlite3_mutex_leave(db->mutex); |
| 592 | return rc; |
| 593 | } |
drh | f1952c5 | 2006-06-08 15:48:00 +0000 | [diff] [blame] | 594 | |
| 595 | /* |
| 596 | ** Call this routine when the database connection is closing in order |
| 597 | ** to clean up loaded extensions |
| 598 | */ |
| 599 | void sqlite3CloseExtensions(sqlite3 *db){ |
| 600 | int i; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 601 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | f1952c5 | 2006-06-08 15:48:00 +0000 | [diff] [blame] | 602 | for(i=0; i<db->nExtension; i++){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 603 | sqlite3OsDlClose(db->pVfs, db->aExtension[i]); |
drh | f1952c5 | 2006-06-08 15:48:00 +0000 | [diff] [blame] | 604 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 605 | sqlite3DbFree(db, db->aExtension); |
drh | f1952c5 | 2006-06-08 15:48:00 +0000 | [diff] [blame] | 606 | } |
| 607 | |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 608 | /* |
| 609 | ** Enable or disable extension loading. Extension loading is disabled by |
| 610 | ** default so as not to open security holes in older applications. |
| 611 | */ |
| 612 | int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 613 | sqlite3_mutex_enter(db->mutex); |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 614 | if( onoff ){ |
drh | 191dd06 | 2016-04-21 01:30:09 +0000 | [diff] [blame] | 615 | db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc; |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 616 | }else{ |
drh | 191dd06 | 2016-04-21 01:30:09 +0000 | [diff] [blame] | 617 | db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc); |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 618 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 619 | sqlite3_mutex_leave(db->mutex); |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 620 | return SQLITE_OK; |
| 621 | } |
| 622 | |
drh | 98365be | 2016-09-15 19:15:19 +0000 | [diff] [blame] | 623 | #endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 624 | |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 625 | /* |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 626 | ** The following object holds the list of automatically loaded |
| 627 | ** extensions. |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 628 | ** |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 629 | ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER |
| 630 | ** mutex must be held while accessing this list. |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 631 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 632 | typedef struct sqlite3AutoExtList sqlite3AutoExtList; |
| 633 | static SQLITE_WSD struct sqlite3AutoExtList { |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 634 | u32 nExt; /* Number of entries in aExt[] */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 635 | void (**aExt)(void); /* Pointers to the extension init functions */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 636 | } sqlite3Autoext = { 0, 0 }; |
| 637 | |
| 638 | /* The "wsdAutoext" macro will resolve to the autoextension |
| 639 | ** state vector. If writable static data is unsupported on the target, |
| 640 | ** we have to locate the state vector at run-time. In the more common |
| 641 | ** case where writable static data is supported, wsdStat can refer directly |
| 642 | ** to the "sqlite3Autoext" state vector declared above. |
| 643 | */ |
| 644 | #ifdef SQLITE_OMIT_WSD |
| 645 | # define wsdAutoextInit \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 646 | sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext) |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 647 | # define wsdAutoext x[0] |
| 648 | #else |
| 649 | # define wsdAutoextInit |
| 650 | # define wsdAutoext sqlite3Autoext |
| 651 | #endif |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 652 | |
| 653 | |
| 654 | /* |
| 655 | ** Register a statically linked extension that is automatically |
| 656 | ** loaded by every new database connection. |
| 657 | */ |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 658 | int sqlite3_auto_extension( |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 659 | void (*xInit)(void) |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 660 | ){ |
danielk1977 | 00f0faf | 2008-07-08 14:17:35 +0000 | [diff] [blame] | 661 | int rc = SQLITE_OK; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 662 | #ifndef SQLITE_OMIT_AUTOINIT |
danielk1977 | 00f0faf | 2008-07-08 14:17:35 +0000 | [diff] [blame] | 663 | rc = sqlite3_initialize(); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 664 | if( rc ){ |
| 665 | return rc; |
| 666 | }else |
drh | e265b08 | 2008-05-01 17:03:49 +0000 | [diff] [blame] | 667 | #endif |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 668 | { |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 669 | u32 i; |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 670 | #if SQLITE_THREADSAFE |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 671 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 672 | #endif |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 673 | wsdAutoextInit; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 674 | sqlite3_mutex_enter(mutex); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 675 | for(i=0; i<wsdAutoext.nExt; i++){ |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 676 | if( wsdAutoext.aExt[i]==xInit ) break; |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 677 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 678 | if( i==wsdAutoext.nExt ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 679 | u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 680 | void (**aNew)(void); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 681 | aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 682 | if( aNew==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 683 | rc = SQLITE_NOMEM_BKPT; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 684 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 685 | wsdAutoext.aExt = aNew; |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 686 | wsdAutoext.aExt[wsdAutoext.nExt] = xInit; |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 687 | wsdAutoext.nExt++; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | sqlite3_mutex_leave(mutex); |
| 691 | assert( (rc&0xff)==rc ); |
| 692 | return rc; |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 693 | } |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 697 | ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the |
| 698 | ** set of routines that is invoked for each new database connection, if it |
| 699 | ** is currently on the list. If xInit is not on the list, then this |
| 700 | ** routine is a no-op. |
| 701 | ** |
| 702 | ** Return 1 if xInit was found on the list and removed. Return 0 if xInit |
| 703 | ** was not on the list. |
| 704 | */ |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 705 | int sqlite3_cancel_auto_extension( |
drh | 32c83c8 | 2016-08-01 14:35:48 +0000 | [diff] [blame] | 706 | void (*xInit)(void) |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 707 | ){ |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 708 | #if SQLITE_THREADSAFE |
| 709 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 710 | #endif |
| 711 | int i; |
| 712 | int n = 0; |
| 713 | wsdAutoextInit; |
| 714 | sqlite3_mutex_enter(mutex); |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 715 | for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ |
drh | b0df540 | 2016-08-01 17:06:44 +0000 | [diff] [blame] | 716 | if( wsdAutoext.aExt[i]==xInit ){ |
drh | 425e27d | 2013-07-15 17:02:28 +0000 | [diff] [blame] | 717 | wsdAutoext.nExt--; |
| 718 | wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; |
| 719 | n++; |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | sqlite3_mutex_leave(mutex); |
| 724 | return n; |
| 725 | } |
| 726 | |
| 727 | /* |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 728 | ** Reset the automatic extension loading mechanism. |
| 729 | */ |
| 730 | void sqlite3_reset_auto_extension(void){ |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 731 | #ifndef SQLITE_OMIT_AUTOINIT |
| 732 | if( sqlite3_initialize()==SQLITE_OK ) |
drh | e265b08 | 2008-05-01 17:03:49 +0000 | [diff] [blame] | 733 | #endif |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 734 | { |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 735 | #if SQLITE_THREADSAFE |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 736 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 737 | #endif |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 738 | wsdAutoextInit; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 739 | sqlite3_mutex_enter(mutex); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 740 | sqlite3_free(wsdAutoext.aExt); |
| 741 | wsdAutoext.aExt = 0; |
| 742 | wsdAutoext.nExt = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 743 | sqlite3_mutex_leave(mutex); |
| 744 | } |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* |
| 748 | ** Load all automatic extensions. |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 749 | ** |
| 750 | ** If anything goes wrong, set an error in the database connection. |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 751 | */ |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 752 | void sqlite3AutoLoadExtensions(sqlite3 *db){ |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 753 | u32 i; |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 754 | int go = 1; |
drh | e5077c1 | 2011-12-13 04:08:36 +0000 | [diff] [blame] | 755 | int rc; |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 756 | sqlite3_loadext_entry xInit; |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 757 | |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 758 | wsdAutoextInit; |
| 759 | if( wsdAutoext.nExt==0 ){ |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 760 | /* Common case: early out without every having to acquire a mutex */ |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 761 | return; |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 762 | } |
| 763 | for(i=0; go; i++){ |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 764 | char *zErrmsg; |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 765 | #if SQLITE_THREADSAFE |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 766 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e265b08 | 2008-05-01 17:03:49 +0000 | [diff] [blame] | 767 | #endif |
drh | 98365be | 2016-09-15 19:15:19 +0000 | [diff] [blame] | 768 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 769 | const sqlite3_api_routines *pThunk = 0; |
| 770 | #else |
| 771 | const sqlite3_api_routines *pThunk = &sqlite3Apis; |
| 772 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 773 | sqlite3_mutex_enter(mutex); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 774 | if( i>=wsdAutoext.nExt ){ |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 775 | xInit = 0; |
| 776 | go = 0; |
| 777 | }else{ |
mistachkin | 44e95d4 | 2016-07-28 22:23:26 +0000 | [diff] [blame] | 778 | xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i]; |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 779 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 780 | sqlite3_mutex_leave(mutex); |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 781 | zErrmsg = 0; |
drh | 98365be | 2016-09-15 19:15:19 +0000 | [diff] [blame] | 782 | if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){ |
drh | 13f40da | 2014-08-22 18:00:11 +0000 | [diff] [blame] | 783 | sqlite3ErrorWithMsg(db, rc, |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 784 | "automatic extension loading failed: %s", zErrmsg); |
| 785 | go = 0; |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 786 | } |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 787 | sqlite3_free(zErrmsg); |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 788 | } |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 789 | } |