OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 ** pointer to a string constant whose value is the same as the | 42 ** pointer to a string constant whose value is the same as the |
43 ** SQLITE_SOURCE_ID C preprocessor macro. | 43 ** SQLITE_SOURCE_ID C preprocessor macro. |
44 */ | 44 */ |
45 const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } | 45 const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
46 | 46 |
47 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function | 47 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function |
48 ** returns an integer equal to SQLITE_VERSION_NUMBER. | 48 ** returns an integer equal to SQLITE_VERSION_NUMBER. |
49 */ | 49 */ |
50 int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } | 50 int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
51 | 51 |
52 /* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns | 52 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns |
53 ** zero if and only if SQLite was compiled mutexing code omitted due to | 53 ** zero if and only if SQLite was compiled with mutexing code omitted due to |
54 ** the SQLITE_THREADSAFE compile-time option being set to 0. | 54 ** the SQLITE_THREADSAFE compile-time option being set to 0. |
55 */ | 55 */ |
56 int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } | 56 int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
57 | 57 |
58 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) | 58 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
59 /* | 59 /* |
60 ** If the following function pointer is not NULL and if | 60 ** If the following function pointer is not NULL and if |
61 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing | 61 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing |
62 ** I/O active are written using this function. These messages | 62 ** I/O active are written using this function. These messages |
63 ** are intended for debugging activity only. | 63 ** are intended for debugging activity only. |
64 */ | 64 */ |
65 void (*sqlite3IoTrace)(const char*, ...) = 0; | 65 void (*sqlite3IoTrace)(const char*, ...) = 0; |
66 #endif | 66 #endif |
67 | 67 |
68 /* | 68 /* |
69 ** If the following global variable points to a string which is the | 69 ** If the following global variable points to a string which is the |
70 ** name of a directory, then that directory will be used to store | 70 ** name of a directory, then that directory will be used to store |
71 ** temporary files. | 71 ** temporary files. |
72 ** | 72 ** |
73 ** See also the "PRAGMA temp_store_directory" SQL command. | 73 ** See also the "PRAGMA temp_store_directory" SQL command. |
74 */ | 74 */ |
75 char *sqlite3_temp_directory = 0; | 75 char *sqlite3_temp_directory = 0; |
76 | 76 |
77 /* | 77 /* |
| 78 ** If the following global variable points to a string which is the |
| 79 ** name of a directory, then that directory will be used to store |
| 80 ** all database files specified with a relative pathname. |
| 81 ** |
| 82 ** See also the "PRAGMA data_store_directory" SQL command. |
| 83 */ |
| 84 char *sqlite3_data_directory = 0; |
| 85 |
| 86 /* |
78 ** Initialize SQLite. | 87 ** Initialize SQLite. |
79 ** | 88 ** |
80 ** This routine must be called to initialize the memory allocation, | 89 ** This routine must be called to initialize the memory allocation, |
81 ** VFS, and mutex subsystems prior to doing any serious work with | 90 ** VFS, and mutex subsystems prior to doing any serious work with |
82 ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT | 91 ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT |
83 ** this routine will be called automatically by key routines such as | 92 ** this routine will be called automatically by key routines such as |
84 ** sqlite3_open(). | 93 ** sqlite3_open(). |
85 ** | 94 ** |
86 ** This routine is a no-op except on its very first call for the process, | 95 ** This routine is a no-op except on its very first call for the process, |
87 ** or for the first call after a call to sqlite3_shutdown. | 96 ** or for the first call after a call to sqlite3_shutdown. |
(...skipping 11 matching lines...) Expand all Loading... |
99 ** thread. Then while the initial invocation of this routine by X is | 108 ** thread. Then while the initial invocation of this routine by X is |
100 ** incomplete, it is required that: | 109 ** incomplete, it is required that: |
101 ** | 110 ** |
102 ** * Calls to this routine from Y must block until the outer-most | 111 ** * Calls to this routine from Y must block until the outer-most |
103 ** call by X completes. | 112 ** call by X completes. |
104 ** | 113 ** |
105 ** * Recursive calls to this routine from thread X return immediately | 114 ** * Recursive calls to this routine from thread X return immediately |
106 ** without blocking. | 115 ** without blocking. |
107 */ | 116 */ |
108 int sqlite3_initialize(void){ | 117 int sqlite3_initialize(void){ |
109 sqlite3_mutex *pMaster; /* The main static mutex */ | 118 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ |
110 int rc; /* Result code */ | 119 int rc; /* Result code */ |
| 120 #ifdef SQLITE_EXTRA_INIT |
| 121 int bRunExtraInit = 0; /* Extra initialization needed */ |
| 122 #endif |
111 | 123 |
112 #ifdef SQLITE_OMIT_WSD | 124 #ifdef SQLITE_OMIT_WSD |
113 rc = sqlite3_wsd_init(4096, 24); | 125 rc = sqlite3_wsd_init(4096, 24); |
114 if( rc!=SQLITE_OK ){ | 126 if( rc!=SQLITE_OK ){ |
115 return rc; | 127 return rc; |
116 } | 128 } |
117 #endif | 129 #endif |
118 | 130 |
119 /* If SQLite is already completely initialized, then this call | 131 /* If SQLite is already completely initialized, then this call |
120 ** to sqlite3_initialize() should be a no-op. But the initialization | 132 ** to sqlite3_initialize() should be a no-op. But the initialization |
(...skipping 12 matching lines...) Expand all Loading... |
133 */ | 145 */ |
134 rc = sqlite3MutexInit(); | 146 rc = sqlite3MutexInit(); |
135 if( rc ) return rc; | 147 if( rc ) return rc; |
136 | 148 |
137 /* Initialize the malloc() system and the recursive pInitMutex mutex. | 149 /* Initialize the malloc() system and the recursive pInitMutex mutex. |
138 ** This operation is protected by the STATIC_MASTER mutex. Note that | 150 ** This operation is protected by the STATIC_MASTER mutex. Note that |
139 ** MutexAlloc() is called for a static mutex prior to initializing the | 151 ** MutexAlloc() is called for a static mutex prior to initializing the |
140 ** malloc subsystem - this implies that the allocation of a static | 152 ** malloc subsystem - this implies that the allocation of a static |
141 ** mutex must not require support from the malloc subsystem. | 153 ** mutex must not require support from the malloc subsystem. |
142 */ | 154 */ |
143 pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 155 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
144 sqlite3_mutex_enter(pMaster); | 156 sqlite3_mutex_enter(pMaster); |
145 sqlite3GlobalConfig.isMutexInit = 1; | 157 sqlite3GlobalConfig.isMutexInit = 1; |
146 if( !sqlite3GlobalConfig.isMallocInit ){ | 158 if( !sqlite3GlobalConfig.isMallocInit ){ |
147 rc = sqlite3MallocInit(); | 159 rc = sqlite3MallocInit(); |
148 } | 160 } |
149 if( rc==SQLITE_OK ){ | 161 if( rc==SQLITE_OK ){ |
150 sqlite3GlobalConfig.isMallocInit = 1; | 162 sqlite3GlobalConfig.isMallocInit = 1; |
151 if( !sqlite3GlobalConfig.pInitMutex ){ | 163 if( !sqlite3GlobalConfig.pInitMutex ){ |
152 sqlite3GlobalConfig.pInitMutex = | 164 sqlite3GlobalConfig.pInitMutex = |
153 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); | 165 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 rc = sqlite3PcacheInitialize(); | 203 rc = sqlite3PcacheInitialize(); |
192 } | 204 } |
193 if( rc==SQLITE_OK ){ | 205 if( rc==SQLITE_OK ){ |
194 sqlite3GlobalConfig.isPCacheInit = 1; | 206 sqlite3GlobalConfig.isPCacheInit = 1; |
195 rc = sqlite3OsInit(); | 207 rc = sqlite3OsInit(); |
196 } | 208 } |
197 if( rc==SQLITE_OK ){ | 209 if( rc==SQLITE_OK ){ |
198 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, | 210 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, |
199 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); | 211 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); |
200 sqlite3GlobalConfig.isInit = 1; | 212 sqlite3GlobalConfig.isInit = 1; |
| 213 #ifdef SQLITE_EXTRA_INIT |
| 214 bRunExtraInit = 1; |
| 215 #endif |
201 } | 216 } |
202 sqlite3GlobalConfig.inProgress = 0; | 217 sqlite3GlobalConfig.inProgress = 0; |
203 } | 218 } |
204 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); | 219 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); |
205 | 220 |
206 /* Go back under the static mutex and clean up the recursive | 221 /* Go back under the static mutex and clean up the recursive |
207 ** mutex to prevent a resource leak. | 222 ** mutex to prevent a resource leak. |
208 */ | 223 */ |
209 sqlite3_mutex_enter(pMaster); | 224 sqlite3_mutex_enter(pMaster); |
210 sqlite3GlobalConfig.nRefInitMutex--; | 225 sqlite3GlobalConfig.nRefInitMutex--; |
(...skipping 16 matching lines...) Expand all Loading... |
227 u64 x = (((u64)1)<<63)-1; | 242 u64 x = (((u64)1)<<63)-1; |
228 double y; | 243 double y; |
229 assert(sizeof(x)==8); | 244 assert(sizeof(x)==8); |
230 assert(sizeof(x)==sizeof(y)); | 245 assert(sizeof(x)==sizeof(y)); |
231 memcpy(&y, &x, 8); | 246 memcpy(&y, &x, 8); |
232 assert( sqlite3IsNaN(y) ); | 247 assert( sqlite3IsNaN(y) ); |
233 } | 248 } |
234 #endif | 249 #endif |
235 #endif | 250 #endif |
236 | 251 |
| 252 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT |
| 253 ** compile-time option. |
| 254 */ |
| 255 #ifdef SQLITE_EXTRA_INIT |
| 256 if( bRunExtraInit ){ |
| 257 int SQLITE_EXTRA_INIT(const char*); |
| 258 rc = SQLITE_EXTRA_INIT(0); |
| 259 } |
| 260 #endif |
| 261 |
237 return rc; | 262 return rc; |
238 } | 263 } |
239 | 264 |
240 /* | 265 /* |
241 ** Undo the effects of sqlite3_initialize(). Must not be called while | 266 ** Undo the effects of sqlite3_initialize(). Must not be called while |
242 ** there are outstanding database connections or memory allocations or | 267 ** there are outstanding database connections or memory allocations or |
243 ** while any part of SQLite is otherwise in use in any thread. This | 268 ** while any part of SQLite is otherwise in use in any thread. This |
244 ** routine is not threadsafe. But it is safe to invoke this routine | 269 ** routine is not threadsafe. But it is safe to invoke this routine |
245 ** on when SQLite is already shut down. If SQLite is already shut down | 270 ** on when SQLite is already shut down. If SQLite is already shut down |
246 ** when this routine is invoked, then this routine is a harmless no-op. | 271 ** when this routine is invoked, then this routine is a harmless no-op. |
247 */ | 272 */ |
248 int sqlite3_shutdown(void){ | 273 int sqlite3_shutdown(void){ |
249 if( sqlite3GlobalConfig.isInit ){ | 274 if( sqlite3GlobalConfig.isInit ){ |
| 275 #ifdef SQLITE_EXTRA_SHUTDOWN |
| 276 void SQLITE_EXTRA_SHUTDOWN(void); |
| 277 SQLITE_EXTRA_SHUTDOWN(); |
| 278 #endif |
250 sqlite3_os_end(); | 279 sqlite3_os_end(); |
251 sqlite3_reset_auto_extension(); | 280 sqlite3_reset_auto_extension(); |
252 sqlite3GlobalConfig.isInit = 0; | 281 sqlite3GlobalConfig.isInit = 0; |
253 } | 282 } |
254 if( sqlite3GlobalConfig.isPCacheInit ){ | 283 if( sqlite3GlobalConfig.isPCacheInit ){ |
255 sqlite3PcacheShutdown(); | 284 sqlite3PcacheShutdown(); |
256 sqlite3GlobalConfig.isPCacheInit = 0; | 285 sqlite3GlobalConfig.isPCacheInit = 0; |
257 } | 286 } |
258 if( sqlite3GlobalConfig.isMallocInit ){ | 287 if( sqlite3GlobalConfig.isMallocInit ){ |
259 sqlite3MallocEnd(); | 288 sqlite3MallocEnd(); |
260 sqlite3GlobalConfig.isMallocInit = 0; | 289 sqlite3GlobalConfig.isMallocInit = 0; |
| 290 |
| 291 #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES |
| 292 /* The heap subsystem has now been shutdown and these values are supposed |
| 293 ** to be NULL or point to memory that was obtained from sqlite3_malloc(), |
| 294 ** which would rely on that heap subsystem; therefore, make sure these |
| 295 ** values cannot refer to heap memory that was just invalidated when the |
| 296 ** heap subsystem was shutdown. This is only done if the current call to |
| 297 ** this function resulted in the heap subsystem actually being shutdown. |
| 298 */ |
| 299 sqlite3_data_directory = 0; |
| 300 sqlite3_temp_directory = 0; |
| 301 #endif |
261 } | 302 } |
262 if( sqlite3GlobalConfig.isMutexInit ){ | 303 if( sqlite3GlobalConfig.isMutexInit ){ |
263 sqlite3MutexEnd(); | 304 sqlite3MutexEnd(); |
264 sqlite3GlobalConfig.isMutexInit = 0; | 305 sqlite3GlobalConfig.isMutexInit = 0; |
265 } | 306 } |
266 | 307 |
267 return SQLITE_OK; | 308 return SQLITE_OK; |
268 } | 309 } |
269 | 310 |
270 /* | 311 /* |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 } | 389 } |
349 case SQLITE_CONFIG_PAGECACHE: { | 390 case SQLITE_CONFIG_PAGECACHE: { |
350 /* Designate a buffer for page cache memory space */ | 391 /* Designate a buffer for page cache memory space */ |
351 sqlite3GlobalConfig.pPage = va_arg(ap, void*); | 392 sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
352 sqlite3GlobalConfig.szPage = va_arg(ap, int); | 393 sqlite3GlobalConfig.szPage = va_arg(ap, int); |
353 sqlite3GlobalConfig.nPage = va_arg(ap, int); | 394 sqlite3GlobalConfig.nPage = va_arg(ap, int); |
354 break; | 395 break; |
355 } | 396 } |
356 | 397 |
357 case SQLITE_CONFIG_PCACHE: { | 398 case SQLITE_CONFIG_PCACHE: { |
358 /* Specify an alternative page cache implementation */ | 399 /* no-op */ |
359 sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*); | 400 break; |
| 401 } |
| 402 case SQLITE_CONFIG_GETPCACHE: { |
| 403 /* now an error */ |
| 404 rc = SQLITE_ERROR; |
360 break; | 405 break; |
361 } | 406 } |
362 | 407 |
363 case SQLITE_CONFIG_GETPCACHE: { | 408 case SQLITE_CONFIG_PCACHE2: { |
364 if( sqlite3GlobalConfig.pcache.xInit==0 ){ | 409 /* Specify an alternative page cache implementation */ |
| 410 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 411 break; |
| 412 } |
| 413 case SQLITE_CONFIG_GETPCACHE2: { |
| 414 if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
365 sqlite3PCacheSetDefault(); | 415 sqlite3PCacheSetDefault(); |
366 } | 416 } |
367 *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache; | 417 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
368 break; | 418 break; |
369 } | 419 } |
370 | 420 |
371 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) | 421 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
372 case SQLITE_CONFIG_HEAP: { | 422 case SQLITE_CONFIG_HEAP: { |
373 /* Designate a buffer for heap memory space */ | 423 /* Designate a buffer for heap memory space */ |
374 sqlite3GlobalConfig.pHeap = va_arg(ap, void*); | 424 sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
375 sqlite3GlobalConfig.nHeap = va_arg(ap, int); | 425 sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
376 sqlite3GlobalConfig.mnReq = va_arg(ap, int); | 426 sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
377 | 427 |
378 if( sqlite3GlobalConfig.mnReq<1 ){ | 428 if( sqlite3GlobalConfig.mnReq<1 ){ |
379 sqlite3GlobalConfig.mnReq = 1; | 429 sqlite3GlobalConfig.mnReq = 1; |
380 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ | 430 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ |
381 /* cap min request size at 2^12 */ | 431 /* cap min request size at 2^12 */ |
382 sqlite3GlobalConfig.mnReq = (1<<12); | 432 sqlite3GlobalConfig.mnReq = (1<<12); |
383 } | 433 } |
384 | 434 |
385 if( sqlite3GlobalConfig.pHeap==0 ){ | 435 if( sqlite3GlobalConfig.pHeap==0 ){ |
386 /* If the heap pointer is NULL, then restore the malloc implementation | 436 /* If the heap pointer is NULL, then restore the malloc implementation |
387 ** back to NULL pointers too. This will cause the malloc to go | 437 ** back to NULL pointers too. This will cause the malloc to go |
388 ** back to its default implementation when sqlite3_initialize() is | 438 ** back to its default implementation when sqlite3_initialize() is |
389 ** run. | 439 ** run. |
390 */ | 440 */ |
391 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); | 441 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
392 }else{ | 442 }else{ |
393 /* The heap pointer is not NULL, then install one of the | 443 /* The heap pointer is not NULL, then install one of the |
394 ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor | 444 ** mem5.c/mem3.c methods. The enclosing #if guarantees at |
395 ** ENABLE_MEMSYS5 is defined, return an error. | 445 ** least one of these methods is currently enabled. |
396 */ | 446 */ |
397 #ifdef SQLITE_ENABLE_MEMSYS3 | 447 #ifdef SQLITE_ENABLE_MEMSYS3 |
398 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); | 448 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
399 #endif | 449 #endif |
400 #ifdef SQLITE_ENABLE_MEMSYS5 | 450 #ifdef SQLITE_ENABLE_MEMSYS5 |
401 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); | 451 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
402 #endif | 452 #endif |
403 } | 453 } |
404 break; | 454 break; |
405 } | 455 } |
406 #endif | 456 #endif |
407 | 457 |
408 case SQLITE_CONFIG_LOOKASIDE: { | 458 case SQLITE_CONFIG_LOOKASIDE: { |
409 sqlite3GlobalConfig.szLookaside = va_arg(ap, int); | 459 sqlite3GlobalConfig.szLookaside = va_arg(ap, int); |
410 sqlite3GlobalConfig.nLookaside = va_arg(ap, int); | 460 sqlite3GlobalConfig.nLookaside = va_arg(ap, int); |
411 break; | 461 break; |
412 } | 462 } |
413 | 463 |
414 /* Record a pointer to the logger funcction and its first argument. | 464 /* Record a pointer to the logger function and its first argument. |
415 ** The default is NULL. Logging is disabled if the function pointer is | 465 ** The default is NULL. Logging is disabled if the function pointer is |
416 ** NULL. | 466 ** NULL. |
417 */ | 467 */ |
418 case SQLITE_CONFIG_LOG: { | 468 case SQLITE_CONFIG_LOG: { |
419 /* MSVC is picky about pulling func ptrs from va lists. | 469 /* MSVC is picky about pulling func ptrs from va lists. |
420 ** http://support.microsoft.com/kb/47961 | 470 ** http://support.microsoft.com/kb/47961 |
421 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); | 471 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); |
422 */ | 472 */ |
423 typedef void(*LOGFUNC_t)(void*,int,const char*); | 473 typedef void(*LOGFUNC_t)(void*,int,const char*); |
424 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); | 474 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); |
425 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); | 475 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); |
426 break; | 476 break; |
427 } | 477 } |
428 | 478 |
| 479 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames |
| 480 ** can be changed at start-time using the |
| 481 ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 482 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 483 */ |
| 484 case SQLITE_CONFIG_URI: { |
| 485 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 486 break; |
| 487 } |
| 488 |
| 489 case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 490 sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 491 break; |
| 492 } |
| 493 |
| 494 #ifdef SQLITE_ENABLE_SQLLOG |
| 495 case SQLITE_CONFIG_SQLLOG: { |
| 496 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int); |
| 497 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t); |
| 498 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *); |
| 499 break; |
| 500 } |
| 501 #endif |
| 502 |
| 503 case SQLITE_CONFIG_MMAP_SIZE: { |
| 504 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); |
| 505 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); |
| 506 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ |
| 507 mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 508 } |
| 509 sqlite3GlobalConfig.mxMmap = mxMmap; |
| 510 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 511 if( szMmap>mxMmap) szMmap = mxMmap; |
| 512 sqlite3GlobalConfig.szMmap = szMmap; |
| 513 break; |
| 514 } |
| 515 |
| 516 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) |
| 517 case SQLITE_CONFIG_WIN32_HEAPSIZE: { |
| 518 sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 519 break; |
| 520 } |
| 521 #endif |
| 522 |
429 default: { | 523 default: { |
430 rc = SQLITE_ERROR; | 524 rc = SQLITE_ERROR; |
431 break; | 525 break; |
432 } | 526 } |
433 } | 527 } |
434 va_end(ap); | 528 va_end(ap); |
435 return rc; | 529 return rc; |
436 } | 530 } |
437 | 531 |
438 /* | 532 /* |
(...skipping 12 matching lines...) Expand all Loading... |
451 if( db->lookaside.nOut ){ | 545 if( db->lookaside.nOut ){ |
452 return SQLITE_BUSY; | 546 return SQLITE_BUSY; |
453 } | 547 } |
454 /* Free any existing lookaside buffer for this handle before | 548 /* Free any existing lookaside buffer for this handle before |
455 ** allocating a new one so we don't have to have space for | 549 ** allocating a new one so we don't have to have space for |
456 ** both at the same time. | 550 ** both at the same time. |
457 */ | 551 */ |
458 if( db->lookaside.bMalloced ){ | 552 if( db->lookaside.bMalloced ){ |
459 sqlite3_free(db->lookaside.pStart); | 553 sqlite3_free(db->lookaside.pStart); |
460 } | 554 } |
461 /* The size of a lookaside slot needs to be larger than a pointer | 555 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger |
462 ** to be useful. | 556 ** than a pointer to be useful. |
463 */ | 557 */ |
| 558 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ |
464 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; | 559 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; |
465 if( cnt<0 ) cnt = 0; | 560 if( cnt<0 ) cnt = 0; |
466 if( sz==0 || cnt==0 ){ | 561 if( sz==0 || cnt==0 ){ |
467 sz = 0; | 562 sz = 0; |
468 pStart = 0; | 563 pStart = 0; |
469 }else if( pBuf==0 ){ | 564 }else if( pBuf==0 ){ |
470 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ | |
471 sqlite3BeginBenignMalloc(); | 565 sqlite3BeginBenignMalloc(); |
472 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ | 566 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ |
473 sqlite3EndBenignMalloc(); | 567 sqlite3EndBenignMalloc(); |
| 568 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz; |
474 }else{ | 569 }else{ |
475 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ | |
476 pStart = pBuf; | 570 pStart = pBuf; |
477 } | 571 } |
478 db->lookaside.pStart = pStart; | 572 db->lookaside.pStart = pStart; |
479 db->lookaside.pFree = 0; | 573 db->lookaside.pFree = 0; |
480 db->lookaside.sz = (u16)sz; | 574 db->lookaside.sz = (u16)sz; |
481 if( pStart ){ | 575 if( pStart ){ |
482 int i; | 576 int i; |
483 LookasideSlot *p; | 577 LookasideSlot *p; |
484 assert( sz > (int)sizeof(LookasideSlot*) ); | 578 assert( sz > (int)sizeof(LookasideSlot*) ); |
485 p = (LookasideSlot*)pStart; | 579 p = (LookasideSlot*)pStart; |
486 for(i=cnt-1; i>=0; i--){ | 580 for(i=cnt-1; i>=0; i--){ |
487 p->pNext = db->lookaside.pFree; | 581 p->pNext = db->lookaside.pFree; |
488 db->lookaside.pFree = p; | 582 db->lookaside.pFree = p; |
489 p = (LookasideSlot*)&((u8*)p)[sz]; | 583 p = (LookasideSlot*)&((u8*)p)[sz]; |
490 } | 584 } |
491 db->lookaside.pEnd = p; | 585 db->lookaside.pEnd = p; |
492 db->lookaside.bEnabled = 1; | 586 db->lookaside.bEnabled = 1; |
493 db->lookaside.bMalloced = pBuf==0 ?1:0; | 587 db->lookaside.bMalloced = pBuf==0 ?1:0; |
494 }else{ | 588 }else{ |
495 db->lookaside.pEnd = 0; | 589 db->lookaside.pStart = db; |
| 590 db->lookaside.pEnd = db; |
496 db->lookaside.bEnabled = 0; | 591 db->lookaside.bEnabled = 0; |
497 db->lookaside.bMalloced = 0; | 592 db->lookaside.bMalloced = 0; |
498 } | 593 } |
499 return SQLITE_OK; | 594 return SQLITE_OK; |
500 } | 595 } |
501 | 596 |
502 /* | 597 /* |
503 ** Return the mutex associated with a database connection. | 598 ** Return the mutex associated with a database connection. |
504 */ | 599 */ |
505 sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ | 600 sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
506 return db->mutex; | 601 return db->mutex; |
507 } | 602 } |
508 | 603 |
509 /* | 604 /* |
| 605 ** Free up as much memory as we can from the given database |
| 606 ** connection. |
| 607 */ |
| 608 int sqlite3_db_release_memory(sqlite3 *db){ |
| 609 int i; |
| 610 sqlite3_mutex_enter(db->mutex); |
| 611 sqlite3BtreeEnterAll(db); |
| 612 for(i=0; i<db->nDb; i++){ |
| 613 Btree *pBt = db->aDb[i].pBt; |
| 614 if( pBt ){ |
| 615 Pager *pPager = sqlite3BtreePager(pBt); |
| 616 sqlite3PagerShrink(pPager); |
| 617 } |
| 618 } |
| 619 sqlite3BtreeLeaveAll(db); |
| 620 sqlite3_mutex_leave(db->mutex); |
| 621 return SQLITE_OK; |
| 622 } |
| 623 |
| 624 /* |
510 ** Configuration settings for an individual database connection | 625 ** Configuration settings for an individual database connection |
511 */ | 626 */ |
512 int sqlite3_db_config(sqlite3 *db, int op, ...){ | 627 int sqlite3_db_config(sqlite3 *db, int op, ...){ |
513 va_list ap; | 628 va_list ap; |
514 int rc; | 629 int rc; |
515 va_start(ap, op); | 630 va_start(ap, op); |
516 switch( op ){ | 631 switch( op ){ |
517 case SQLITE_DBCONFIG_LOOKASIDE: { | 632 case SQLITE_DBCONFIG_LOOKASIDE: { |
518 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ | 633 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ |
519 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ | 634 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 }else{ | 706 }else{ |
592 rc = nKey1 - nKey2; | 707 rc = nKey1 - nKey2; |
593 } | 708 } |
594 } | 709 } |
595 return rc; | 710 return rc; |
596 } | 711 } |
597 | 712 |
598 /* | 713 /* |
599 ** Another built-in collating sequence: NOCASE. | 714 ** Another built-in collating sequence: NOCASE. |
600 ** | 715 ** |
601 ** This collating sequence is intended to be used for "case independant | 716 ** This collating sequence is intended to be used for "case independent |
602 ** comparison". SQLite's knowledge of upper and lower case equivalents | 717 ** comparison". SQLite's knowledge of upper and lower case equivalents |
603 ** extends only to the 26 characters used in the English language. | 718 ** extends only to the 26 characters used in the English language. |
604 ** | 719 ** |
605 ** At the moment there is only a UTF-8 implementation. | 720 ** At the moment there is only a UTF-8 implementation. |
606 */ | 721 */ |
607 static int nocaseCollatingFunc( | 722 static int nocaseCollatingFunc( |
608 void *NotUsed, | 723 void *NotUsed, |
609 int nKey1, const void *pKey1, | 724 int nKey1, const void *pKey1, |
610 int nKey2, const void *pKey2 | 725 int nKey2, const void *pKey2 |
611 ){ | 726 ){ |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 if( pDestructor ){ | 781 if( pDestructor ){ |
667 pDestructor->nRef--; | 782 pDestructor->nRef--; |
668 if( pDestructor->nRef==0 ){ | 783 if( pDestructor->nRef==0 ){ |
669 pDestructor->xDestroy(pDestructor->pUserData); | 784 pDestructor->xDestroy(pDestructor->pUserData); |
670 sqlite3DbFree(db, pDestructor); | 785 sqlite3DbFree(db, pDestructor); |
671 } | 786 } |
672 } | 787 } |
673 } | 788 } |
674 | 789 |
675 /* | 790 /* |
| 791 ** Disconnect all sqlite3_vtab objects that belong to database connection |
| 792 ** db. This is called when db is being closed. |
| 793 */ |
| 794 static void disconnectAllVtab(sqlite3 *db){ |
| 795 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 796 int i; |
| 797 sqlite3BtreeEnterAll(db); |
| 798 for(i=0; i<db->nDb; i++){ |
| 799 Schema *pSchema = db->aDb[i].pSchema; |
| 800 if( db->aDb[i].pSchema ){ |
| 801 HashElem *p; |
| 802 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ |
| 803 Table *pTab = (Table *)sqliteHashData(p); |
| 804 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab); |
| 805 } |
| 806 } |
| 807 } |
| 808 sqlite3VtabUnlockList(db); |
| 809 sqlite3BtreeLeaveAll(db); |
| 810 #else |
| 811 UNUSED_PARAMETER(db); |
| 812 #endif |
| 813 } |
| 814 |
| 815 /* |
| 816 ** Return TRUE if database connection db has unfinalized prepared |
| 817 ** statements or unfinished sqlite3_backup objects. |
| 818 */ |
| 819 static int connectionIsBusy(sqlite3 *db){ |
| 820 int j; |
| 821 assert( sqlite3_mutex_held(db->mutex) ); |
| 822 if( db->pVdbe ) return 1; |
| 823 for(j=0; j<db->nDb; j++){ |
| 824 Btree *pBt = db->aDb[j].pBt; |
| 825 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1; |
| 826 } |
| 827 return 0; |
| 828 } |
| 829 |
| 830 /* |
676 ** Close an existing SQLite database | 831 ** Close an existing SQLite database |
677 */ | 832 */ |
678 int sqlite3_close(sqlite3 *db){ | 833 static int sqlite3Close(sqlite3 *db, int forceZombie){ |
679 HashElem *i; /* Hash table iterator */ | |
680 int j; | |
681 | |
682 if( !db ){ | 834 if( !db ){ |
| 835 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or |
| 836 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */ |
683 return SQLITE_OK; | 837 return SQLITE_OK; |
684 } | 838 } |
685 if( !sqlite3SafetyCheckSickOrOk(db) ){ | 839 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
686 return SQLITE_MISUSE_BKPT; | 840 return SQLITE_MISUSE_BKPT; |
687 } | 841 } |
688 sqlite3_mutex_enter(db->mutex); | 842 sqlite3_mutex_enter(db->mutex); |
689 | 843 |
690 /* Force xDestroy calls on all virtual tables */ | 844 /* Force xDisconnect calls on all virtual tables */ |
691 sqlite3ResetInternalSchema(db, -1); | 845 disconnectAllVtab(db); |
692 | 846 |
693 /* If a transaction is open, the ResetInternalSchema() call above | 847 /* If a transaction is open, the disconnectAllVtab() call above |
694 ** will not have called the xDisconnect() method on any virtual | 848 ** will not have called the xDisconnect() method on any virtual |
695 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() | 849 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() |
696 ** call will do so. We need to do this before the check for active | 850 ** call will do so. We need to do this before the check for active |
697 ** SQL statements below, as the v-table implementation may be storing | 851 ** SQL statements below, as the v-table implementation may be storing |
698 ** some prepared statements internally. | 852 ** some prepared statements internally. |
699 */ | 853 */ |
700 sqlite3VtabRollback(db); | 854 sqlite3VtabRollback(db); |
701 | 855 |
702 /* If there are any outstanding VMs, return SQLITE_BUSY. */ | 856 /* Legacy behavior (sqlite3_close() behavior) is to return |
703 if( db->pVdbe ){ | 857 ** SQLITE_BUSY if the connection can not be closed immediately. |
704 sqlite3Error(db, SQLITE_BUSY, | 858 */ |
705 "unable to close due to unfinalised statements"); | 859 if( !forceZombie && connectionIsBusy(db) ){ |
| 860 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized " |
| 861 "statements or unfinished backups"); |
706 sqlite3_mutex_leave(db->mutex); | 862 sqlite3_mutex_leave(db->mutex); |
707 return SQLITE_BUSY; | 863 return SQLITE_BUSY; |
708 } | 864 } |
709 assert( sqlite3SafetyCheckSickOrOk(db) ); | |
710 | 865 |
711 for(j=0; j<db->nDb; j++){ | 866 #ifdef SQLITE_ENABLE_SQLLOG |
712 Btree *pBt = db->aDb[j].pBt; | 867 if( sqlite3GlobalConfig.xSqllog ){ |
713 if( pBt && sqlite3BtreeIsInBackup(pBt) ){ | 868 /* Closing the handle. Fourth parameter is passed the value 2. */ |
714 sqlite3Error(db, SQLITE_BUSY, | 869 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); |
715 "unable to close due to unfinished backup operation"); | |
716 sqlite3_mutex_leave(db->mutex); | |
717 return SQLITE_BUSY; | |
718 } | |
719 } | 870 } |
| 871 #endif |
| 872 |
| 873 /* Convert the connection into a zombie and then close it. |
| 874 */ |
| 875 db->magic = SQLITE_MAGIC_ZOMBIE; |
| 876 sqlite3LeaveMutexAndCloseZombie(db); |
| 877 return SQLITE_OK; |
| 878 } |
| 879 |
| 880 /* |
| 881 ** Two variations on the public interface for closing a database |
| 882 ** connection. The sqlite3_close() version returns SQLITE_BUSY and |
| 883 ** leaves the connection option if there are unfinalized prepared |
| 884 ** statements or unfinished sqlite3_backups. The sqlite3_close_v2() |
| 885 ** version forces the connection to become a zombie if there are |
| 886 ** unclosed resources, and arranges for deallocation when the last |
| 887 ** prepare statement or sqlite3_backup closes. |
| 888 */ |
| 889 int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); } |
| 890 int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); } |
| 891 |
| 892 |
| 893 /* |
| 894 ** Close the mutex on database connection db. |
| 895 ** |
| 896 ** Furthermore, if database connection db is a zombie (meaning that there |
| 897 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and |
| 898 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has |
| 899 ** finished, then free all resources. |
| 900 */ |
| 901 void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ |
| 902 HashElem *i; /* Hash table iterator */ |
| 903 int j; |
| 904 |
| 905 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects |
| 906 ** or if the connection has not yet been closed by sqlite3_close_v2(), |
| 907 ** then just leave the mutex and return. |
| 908 */ |
| 909 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){ |
| 910 sqlite3_mutex_leave(db->mutex); |
| 911 return; |
| 912 } |
| 913 |
| 914 /* If we reach this point, it means that the database connection has |
| 915 ** closed all sqlite3_stmt and sqlite3_backup objects and has been |
| 916 ** passed to sqlite3_close (meaning that it is a zombie). Therefore, |
| 917 ** go ahead and free all resources. |
| 918 */ |
| 919 |
| 920 /* If a transaction is open, roll it back. This also ensures that if |
| 921 ** any database schemas have been modified by an uncommitted transaction |
| 922 ** they are reset. And that the required b-tree mutex is held to make |
| 923 ** the pager rollback and schema reset an atomic operation. */ |
| 924 sqlite3RollbackAll(db, SQLITE_OK); |
720 | 925 |
721 /* Free any outstanding Savepoint structures. */ | 926 /* Free any outstanding Savepoint structures. */ |
722 sqlite3CloseSavepoints(db); | 927 sqlite3CloseSavepoints(db); |
723 | 928 |
| 929 /* Close all database connections */ |
724 for(j=0; j<db->nDb; j++){ | 930 for(j=0; j<db->nDb; j++){ |
725 struct Db *pDb = &db->aDb[j]; | 931 struct Db *pDb = &db->aDb[j]; |
726 if( pDb->pBt ){ | 932 if( pDb->pBt ){ |
| 933 if( pDb->pSchema ){ |
| 934 /* Must clear the KeyInfo cache. See ticket [e4a18565a36884b00edf] */ |
| 935 sqlite3BtreeEnter(pDb->pBt); |
| 936 for(i=sqliteHashFirst(&pDb->pSchema->idxHash); i; i=sqliteHashNext(i)){ |
| 937 Index *pIdx = sqliteHashData(i); |
| 938 sqlite3KeyInfoUnref(pIdx->pKeyInfo); |
| 939 pIdx->pKeyInfo = 0; |
| 940 } |
| 941 sqlite3BtreeLeave(pDb->pBt); |
| 942 } |
727 sqlite3BtreeClose(pDb->pBt); | 943 sqlite3BtreeClose(pDb->pBt); |
728 pDb->pBt = 0; | 944 pDb->pBt = 0; |
729 if( j!=1 ){ | 945 if( j!=1 ){ |
730 pDb->pSchema = 0; | 946 pDb->pSchema = 0; |
731 } | 947 } |
732 } | 948 } |
733 } | 949 } |
734 sqlite3ResetInternalSchema(db, -1); | 950 /* Clear the TEMP schema separately and last */ |
| 951 if( db->aDb[1].pSchema ){ |
| 952 sqlite3SchemaClear(db->aDb[1].pSchema); |
| 953 } |
| 954 sqlite3VtabUnlockList(db); |
| 955 |
| 956 /* Free up the array of auxiliary databases */ |
| 957 sqlite3CollapseDatabaseArray(db); |
| 958 assert( db->nDb<=2 ); |
| 959 assert( db->aDb==db->aDbStatic ); |
735 | 960 |
736 /* Tell the code in notify.c that the connection no longer holds any | 961 /* Tell the code in notify.c that the connection no longer holds any |
737 ** locks and does not require any further unlock-notify callbacks. | 962 ** locks and does not require any further unlock-notify callbacks. |
738 */ | 963 */ |
739 sqlite3ConnectionClosed(db); | 964 sqlite3ConnectionClosed(db); |
740 | 965 |
741 assert( db->nDb<=2 ); | |
742 assert( db->aDb==db->aDbStatic ); | |
743 for(j=0; j<ArraySize(db->aFunc.a); j++){ | 966 for(j=0; j<ArraySize(db->aFunc.a); j++){ |
744 FuncDef *pNext, *pHash, *p; | 967 FuncDef *pNext, *pHash, *p; |
745 for(p=db->aFunc.a[j]; p; p=pHash){ | 968 for(p=db->aFunc.a[j]; p; p=pHash){ |
746 pHash = p->pHash; | 969 pHash = p->pHash; |
747 while( p ){ | 970 while( p ){ |
748 functionDestroy(db, p); | 971 functionDestroy(db, p); |
749 pNext = p->pNext; | 972 pNext = p->pNext; |
750 sqlite3DbFree(db, p); | 973 sqlite3DbFree(db, p); |
751 p = pNext; | 974 p = pNext; |
752 } | 975 } |
(...skipping 14 matching lines...) Expand all Loading... |
767 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ | 990 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ |
768 Module *pMod = (Module *)sqliteHashData(i); | 991 Module *pMod = (Module *)sqliteHashData(i); |
769 if( pMod->xDestroy ){ | 992 if( pMod->xDestroy ){ |
770 pMod->xDestroy(pMod->pAux); | 993 pMod->xDestroy(pMod->pAux); |
771 } | 994 } |
772 sqlite3DbFree(db, pMod); | 995 sqlite3DbFree(db, pMod); |
773 } | 996 } |
774 sqlite3HashClear(&db->aModule); | 997 sqlite3HashClear(&db->aModule); |
775 #endif | 998 #endif |
776 | 999 |
777 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ | 1000 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */ |
778 if( db->pErr ){ | 1001 sqlite3ValueFree(db->pErr); |
779 sqlite3ValueFree(db->pErr); | |
780 } | |
781 sqlite3CloseExtensions(db); | 1002 sqlite3CloseExtensions(db); |
| 1003 #if SQLITE_USER_AUTHENTICATION |
| 1004 sqlite3_free(db->auth.zAuthUser); |
| 1005 sqlite3_free(db->auth.zAuthPW); |
| 1006 #endif |
782 | 1007 |
783 db->magic = SQLITE_MAGIC_ERROR; | 1008 db->magic = SQLITE_MAGIC_ERROR; |
784 | 1009 |
785 /* The temp-database schema is allocated differently from the other schema | 1010 /* The temp-database schema is allocated differently from the other schema |
786 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). | 1011 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). |
787 ** So it needs to be freed here. Todo: Why not roll the temp schema into | 1012 ** So it needs to be freed here. Todo: Why not roll the temp schema into |
788 ** the same sqliteMalloc() as the one that allocates the database | 1013 ** the same sqliteMalloc() as the one that allocates the database |
789 ** structure? | 1014 ** structure? |
790 */ | 1015 */ |
791 sqlite3DbFree(db, db->aDb[1].pSchema); | 1016 sqlite3DbFree(db, db->aDb[1].pSchema); |
792 sqlite3_mutex_leave(db->mutex); | 1017 sqlite3_mutex_leave(db->mutex); |
793 db->magic = SQLITE_MAGIC_CLOSED; | 1018 db->magic = SQLITE_MAGIC_CLOSED; |
794 sqlite3_mutex_free(db->mutex); | 1019 sqlite3_mutex_free(db->mutex); |
795 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ | 1020 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ |
796 if( db->lookaside.bMalloced ){ | 1021 if( db->lookaside.bMalloced ){ |
797 sqlite3_free(db->lookaside.pStart); | 1022 sqlite3_free(db->lookaside.pStart); |
798 } | 1023 } |
799 sqlite3_free(db); | 1024 sqlite3_free(db); |
800 return SQLITE_OK; | |
801 } | 1025 } |
802 | 1026 |
803 /* | 1027 /* |
804 ** Rollback all database files. | 1028 ** Rollback all database files. If tripCode is not SQLITE_OK, then |
| 1029 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit |
| 1030 ** breaker") and made to return tripCode if there are any further |
| 1031 ** attempts to use that cursor. Read cursors remain open and valid |
| 1032 ** but are "saved" in case the table pages are moved around. |
805 */ | 1033 */ |
806 void sqlite3RollbackAll(sqlite3 *db){ | 1034 void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
807 int i; | 1035 int i; |
808 int inTrans = 0; | 1036 int inTrans = 0; |
| 1037 int schemaChange; |
809 assert( sqlite3_mutex_held(db->mutex) ); | 1038 assert( sqlite3_mutex_held(db->mutex) ); |
810 sqlite3BeginBenignMalloc(); | 1039 sqlite3BeginBenignMalloc(); |
| 1040 |
| 1041 /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). |
| 1042 ** This is important in case the transaction being rolled back has |
| 1043 ** modified the database schema. If the b-tree mutexes are not taken |
| 1044 ** here, then another shared-cache connection might sneak in between |
| 1045 ** the database rollback and schema reset, which can cause false |
| 1046 ** corruption reports in some cases. */ |
| 1047 sqlite3BtreeEnterAll(db); |
| 1048 schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0; |
| 1049 |
811 for(i=0; i<db->nDb; i++){ | 1050 for(i=0; i<db->nDb; i++){ |
812 if( db->aDb[i].pBt ){ | 1051 Btree *p = db->aDb[i].pBt; |
813 if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){ | 1052 if( p ){ |
| 1053 if( sqlite3BtreeIsInTrans(p) ){ |
814 inTrans = 1; | 1054 inTrans = 1; |
815 } | 1055 } |
816 sqlite3BtreeRollback(db->aDb[i].pBt); | 1056 sqlite3BtreeRollback(p, tripCode, !schemaChange); |
817 db->aDb[i].inTrans = 0; | |
818 } | 1057 } |
819 } | 1058 } |
820 sqlite3VtabRollback(db); | 1059 sqlite3VtabRollback(db); |
821 sqlite3EndBenignMalloc(); | 1060 sqlite3EndBenignMalloc(); |
822 | 1061 |
823 if( db->flags&SQLITE_InternChanges ){ | 1062 if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){ |
824 sqlite3ExpirePreparedStatements(db); | 1063 sqlite3ExpirePreparedStatements(db); |
825 sqlite3ResetInternalSchema(db, -1); | 1064 sqlite3ResetAllSchemasOfConnection(db); |
826 } | 1065 } |
| 1066 sqlite3BtreeLeaveAll(db); |
827 | 1067 |
828 /* Any deferred constraint violations have now been resolved. */ | 1068 /* Any deferred constraint violations have now been resolved. */ |
829 db->nDeferredCons = 0; | 1069 db->nDeferredCons = 0; |
| 1070 db->nDeferredImmCons = 0; |
| 1071 db->flags &= ~SQLITE_DeferFKs; |
830 | 1072 |
831 /* If one has been configured, invoke the rollback-hook callback */ | 1073 /* If one has been configured, invoke the rollback-hook callback */ |
832 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ | 1074 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ |
833 db->xRollbackCallback(db->pRollbackArg); | 1075 db->xRollbackCallback(db->pRollbackArg); |
834 } | 1076 } |
835 } | 1077 } |
836 | 1078 |
837 /* | 1079 /* |
| 1080 ** Return a static string containing the name corresponding to the error code |
| 1081 ** specified in the argument. |
| 1082 */ |
| 1083 #if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST) |
| 1084 const char *sqlite3ErrName(int rc){ |
| 1085 const char *zName = 0; |
| 1086 int i, origRc = rc; |
| 1087 for(i=0; i<2 && zName==0; i++, rc &= 0xff){ |
| 1088 switch( rc ){ |
| 1089 case SQLITE_OK: zName = "SQLITE_OK"; break; |
| 1090 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; |
| 1091 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; |
| 1092 case SQLITE_PERM: zName = "SQLITE_PERM"; break; |
| 1093 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; |
| 1094 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break; |
| 1095 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; |
| 1096 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break; |
| 1097 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break; |
| 1098 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; |
| 1099 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break; |
| 1100 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; |
| 1101 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; |
| 1102 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break; |
| 1103 case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break; |
| 1104 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break; |
| 1105 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break; |
| 1106 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; |
| 1107 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; |
| 1108 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break; |
| 1109 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break; |
| 1110 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break; |
| 1111 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break; |
| 1112 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break; |
| 1113 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break; |
| 1114 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break; |
| 1115 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break; |
| 1116 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break; |
| 1117 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break; |
| 1118 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break; |
| 1119 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break; |
| 1120 case SQLITE_IOERR_CHECKRESERVEDLOCK: |
| 1121 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; |
| 1122 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break; |
| 1123 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break; |
| 1124 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break; |
| 1125 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break; |
| 1126 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break; |
| 1127 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break; |
| 1128 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break; |
| 1129 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break; |
| 1130 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break; |
| 1131 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break; |
| 1132 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break; |
| 1133 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break; |
| 1134 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; |
| 1135 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break; |
| 1136 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break; |
| 1137 case SQLITE_FULL: zName = "SQLITE_FULL"; break; |
| 1138 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; |
| 1139 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break; |
| 1140 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break; |
| 1141 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break; |
| 1142 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break; |
| 1143 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; |
| 1144 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; |
| 1145 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; |
| 1146 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; |
| 1147 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; |
| 1148 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break; |
| 1149 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break; |
| 1150 case SQLITE_CONSTRAINT_FOREIGNKEY: |
| 1151 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break; |
| 1152 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break; |
| 1153 case SQLITE_CONSTRAINT_PRIMARYKEY: |
| 1154 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break; |
| 1155 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break; |
| 1156 case SQLITE_CONSTRAINT_COMMITHOOK: |
| 1157 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break; |
| 1158 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break; |
| 1159 case SQLITE_CONSTRAINT_FUNCTION: |
| 1160 zName = "SQLITE_CONSTRAINT_FUNCTION"; break; |
| 1161 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break; |
| 1162 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; |
| 1163 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; |
| 1164 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; |
| 1165 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; |
| 1166 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; |
| 1167 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; |
| 1168 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break; |
| 1169 case SQLITE_ROW: zName = "SQLITE_ROW"; break; |
| 1170 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break; |
| 1171 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break; |
| 1172 case SQLITE_NOTICE_RECOVER_ROLLBACK: |
| 1173 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break; |
| 1174 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break; |
| 1175 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break; |
| 1176 case SQLITE_DONE: zName = "SQLITE_DONE"; break; |
| 1177 } |
| 1178 } |
| 1179 if( zName==0 ){ |
| 1180 static char zBuf[50]; |
| 1181 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc); |
| 1182 zName = zBuf; |
| 1183 } |
| 1184 return zName; |
| 1185 } |
| 1186 #endif |
| 1187 |
| 1188 /* |
838 ** Return a static string that describes the kind of error specified in the | 1189 ** Return a static string that describes the kind of error specified in the |
839 ** argument. | 1190 ** argument. |
840 */ | 1191 */ |
841 const char *sqlite3ErrStr(int rc){ | 1192 const char *sqlite3ErrStr(int rc){ |
842 static const char* const aMsg[] = { | 1193 static const char* const aMsg[] = { |
843 /* SQLITE_OK */ "not an error", | 1194 /* SQLITE_OK */ "not an error", |
844 /* SQLITE_ERROR */ "SQL logic error or missing database", | 1195 /* SQLITE_ERROR */ "SQL logic error or missing database", |
845 /* SQLITE_INTERNAL */ 0, | 1196 /* SQLITE_INTERNAL */ 0, |
846 /* SQLITE_PERM */ "access permission denied", | 1197 /* SQLITE_PERM */ "access permission denied", |
847 /* SQLITE_ABORT */ "callback requested query abort", | 1198 /* SQLITE_ABORT */ "callback requested query abort", |
(...skipping 13 matching lines...) Expand all Loading... |
861 /* SQLITE_TOOBIG */ "string or blob too big", | 1212 /* SQLITE_TOOBIG */ "string or blob too big", |
862 /* SQLITE_CONSTRAINT */ "constraint failed", | 1213 /* SQLITE_CONSTRAINT */ "constraint failed", |
863 /* SQLITE_MISMATCH */ "datatype mismatch", | 1214 /* SQLITE_MISMATCH */ "datatype mismatch", |
864 /* SQLITE_MISUSE */ "library routine called out of sequence", | 1215 /* SQLITE_MISUSE */ "library routine called out of sequence", |
865 /* SQLITE_NOLFS */ "large file support is disabled", | 1216 /* SQLITE_NOLFS */ "large file support is disabled", |
866 /* SQLITE_AUTH */ "authorization denied", | 1217 /* SQLITE_AUTH */ "authorization denied", |
867 /* SQLITE_FORMAT */ "auxiliary database format error", | 1218 /* SQLITE_FORMAT */ "auxiliary database format error", |
868 /* SQLITE_RANGE */ "bind or column index out of range", | 1219 /* SQLITE_RANGE */ "bind or column index out of range", |
869 /* SQLITE_NOTADB */ "file is encrypted or is not a database", | 1220 /* SQLITE_NOTADB */ "file is encrypted or is not a database", |
870 }; | 1221 }; |
871 rc &= 0xff; | 1222 const char *zErr = "unknown error"; |
872 if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){ | 1223 switch( rc ){ |
873 return aMsg[rc]; | 1224 case SQLITE_ABORT_ROLLBACK: { |
874 }else{ | 1225 zErr = "abort due to ROLLBACK"; |
875 return "unknown error"; | 1226 break; |
| 1227 } |
| 1228 default: { |
| 1229 rc &= 0xff; |
| 1230 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){ |
| 1231 zErr = aMsg[rc]; |
| 1232 } |
| 1233 break; |
| 1234 } |
876 } | 1235 } |
| 1236 return zErr; |
877 } | 1237 } |
878 | 1238 |
879 /* | 1239 /* |
880 ** This routine implements a busy callback that sleeps and tries | 1240 ** This routine implements a busy callback that sleeps and tries |
881 ** again until a timeout value is reached. The timeout value is | 1241 ** again until a timeout value is reached. The timeout value is |
882 ** an integer number of milliseconds passed in as the first | 1242 ** an integer number of milliseconds passed in as the first |
883 ** argument. | 1243 ** argument. |
884 */ | 1244 */ |
885 static int sqliteDefaultBusyCallback( | 1245 static int sqliteDefaultBusyCallback( |
886 void *ptr, /* Database connection */ | 1246 void *ptr, /* Database connection */ |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
946 */ | 1306 */ |
947 int sqlite3_busy_handler( | 1307 int sqlite3_busy_handler( |
948 sqlite3 *db, | 1308 sqlite3 *db, |
949 int (*xBusy)(void*,int), | 1309 int (*xBusy)(void*,int), |
950 void *pArg | 1310 void *pArg |
951 ){ | 1311 ){ |
952 sqlite3_mutex_enter(db->mutex); | 1312 sqlite3_mutex_enter(db->mutex); |
953 db->busyHandler.xFunc = xBusy; | 1313 db->busyHandler.xFunc = xBusy; |
954 db->busyHandler.pArg = pArg; | 1314 db->busyHandler.pArg = pArg; |
955 db->busyHandler.nBusy = 0; | 1315 db->busyHandler.nBusy = 0; |
| 1316 db->busyTimeout = 0; |
956 sqlite3_mutex_leave(db->mutex); | 1317 sqlite3_mutex_leave(db->mutex); |
957 return SQLITE_OK; | 1318 return SQLITE_OK; |
958 } | 1319 } |
959 | 1320 |
960 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | 1321 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
961 /* | 1322 /* |
962 ** This routine sets the progress callback for an Sqlite database to the | 1323 ** This routine sets the progress callback for an Sqlite database to the |
963 ** given callback function with the given argument. The progress callback will | 1324 ** given callback function with the given argument. The progress callback will |
964 ** be invoked every nOps opcodes. | 1325 ** be invoked every nOps opcodes. |
965 */ | 1326 */ |
966 void sqlite3_progress_handler( | 1327 void sqlite3_progress_handler( |
967 sqlite3 *db, | 1328 sqlite3 *db, |
968 int nOps, | 1329 int nOps, |
969 int (*xProgress)(void*), | 1330 int (*xProgress)(void*), |
970 void *pArg | 1331 void *pArg |
971 ){ | 1332 ){ |
972 sqlite3_mutex_enter(db->mutex); | 1333 sqlite3_mutex_enter(db->mutex); |
973 if( nOps>0 ){ | 1334 if( nOps>0 ){ |
974 db->xProgress = xProgress; | 1335 db->xProgress = xProgress; |
975 db->nProgressOps = nOps; | 1336 db->nProgressOps = (unsigned)nOps; |
976 db->pProgressArg = pArg; | 1337 db->pProgressArg = pArg; |
977 }else{ | 1338 }else{ |
978 db->xProgress = 0; | 1339 db->xProgress = 0; |
979 db->nProgressOps = 0; | 1340 db->nProgressOps = 0; |
980 db->pProgressArg = 0; | 1341 db->pProgressArg = 0; |
981 } | 1342 } |
982 sqlite3_mutex_leave(db->mutex); | 1343 sqlite3_mutex_leave(db->mutex); |
983 } | 1344 } |
984 #endif | 1345 #endif |
985 | 1346 |
986 | 1347 |
987 /* | 1348 /* |
988 ** This routine installs a default busy handler that waits for the | 1349 ** This routine installs a default busy handler that waits for the |
989 ** specified number of milliseconds before returning 0. | 1350 ** specified number of milliseconds before returning 0. |
990 */ | 1351 */ |
991 int sqlite3_busy_timeout(sqlite3 *db, int ms){ | 1352 int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
992 if( ms>0 ){ | 1353 if( ms>0 ){ |
| 1354 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
993 db->busyTimeout = ms; | 1355 db->busyTimeout = ms; |
994 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); | |
995 }else{ | 1356 }else{ |
996 sqlite3_busy_handler(db, 0, 0); | 1357 sqlite3_busy_handler(db, 0, 0); |
997 } | 1358 } |
998 return SQLITE_OK; | 1359 return SQLITE_OK; |
999 } | 1360 } |
1000 | 1361 |
1001 /* | 1362 /* |
1002 ** Cause any pending operation to stop at its earliest opportunity. | 1363 ** Cause any pending operation to stop at its earliest opportunity. |
1003 */ | 1364 */ |
1004 void sqlite3_interrupt(sqlite3 *db){ | 1365 void sqlite3_interrupt(sqlite3 *db){ |
(...skipping 13 matching lines...) Expand all Loading... |
1018 int nArg, | 1379 int nArg, |
1019 int enc, | 1380 int enc, |
1020 void *pUserData, | 1381 void *pUserData, |
1021 void (*xFunc)(sqlite3_context*,int,sqlite3_value **), | 1382 void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
1022 void (*xStep)(sqlite3_context*,int,sqlite3_value **), | 1383 void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
1023 void (*xFinal)(sqlite3_context*), | 1384 void (*xFinal)(sqlite3_context*), |
1024 FuncDestructor *pDestructor | 1385 FuncDestructor *pDestructor |
1025 ){ | 1386 ){ |
1026 FuncDef *p; | 1387 FuncDef *p; |
1027 int nName; | 1388 int nName; |
| 1389 int extraFlags; |
1028 | 1390 |
1029 assert( sqlite3_mutex_held(db->mutex) ); | 1391 assert( sqlite3_mutex_held(db->mutex) ); |
1030 if( zFunctionName==0 || | 1392 if( zFunctionName==0 || |
1031 (xFunc && (xFinal || xStep)) || | 1393 (xFunc && (xFinal || xStep)) || |
1032 (!xFunc && (xFinal && !xStep)) || | 1394 (!xFunc && (xFinal && !xStep)) || |
1033 (!xFunc && (!xFinal && xStep)) || | 1395 (!xFunc && (!xFinal && xStep)) || |
1034 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || | 1396 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || |
1035 (255<(nName = sqlite3Strlen30( zFunctionName))) ){ | 1397 (255<(nName = sqlite3Strlen30( zFunctionName))) ){ |
1036 return SQLITE_MISUSE_BKPT; | 1398 return SQLITE_MISUSE_BKPT; |
1037 } | 1399 } |
| 1400 |
| 1401 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC ); |
| 1402 extraFlags = enc & SQLITE_DETERMINISTIC; |
| 1403 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY); |
1038 | 1404 |
1039 #ifndef SQLITE_OMIT_UTF16 | 1405 #ifndef SQLITE_OMIT_UTF16 |
1040 /* If SQLITE_UTF16 is specified as the encoding type, transform this | 1406 /* If SQLITE_UTF16 is specified as the encoding type, transform this |
1041 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the | 1407 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
1042 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. | 1408 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
1043 ** | 1409 ** |
1044 ** If SQLITE_ANY is specified, add three versions of the function | 1410 ** If SQLITE_ANY is specified, add three versions of the function |
1045 ** to the hash table. | 1411 ** to the hash table. |
1046 */ | 1412 */ |
1047 if( enc==SQLITE_UTF16 ){ | 1413 if( enc==SQLITE_UTF16 ){ |
1048 enc = SQLITE_UTF16NATIVE; | 1414 enc = SQLITE_UTF16NATIVE; |
1049 }else if( enc==SQLITE_ANY ){ | 1415 }else if( enc==SQLITE_ANY ){ |
1050 int rc; | 1416 int rc; |
1051 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, | 1417 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags, |
1052 pUserData, xFunc, xStep, xFinal, pDestructor); | 1418 pUserData, xFunc, xStep, xFinal, pDestructor); |
1053 if( rc==SQLITE_OK ){ | 1419 if( rc==SQLITE_OK ){ |
1054 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, | 1420 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags, |
1055 pUserData, xFunc, xStep, xFinal, pDestructor); | 1421 pUserData, xFunc, xStep, xFinal, pDestructor); |
1056 } | 1422 } |
1057 if( rc!=SQLITE_OK ){ | 1423 if( rc!=SQLITE_OK ){ |
1058 return rc; | 1424 return rc; |
1059 } | 1425 } |
1060 enc = SQLITE_UTF16BE; | 1426 enc = SQLITE_UTF16BE; |
1061 } | 1427 } |
1062 #else | 1428 #else |
1063 enc = SQLITE_UTF8; | 1429 enc = SQLITE_UTF8; |
1064 #endif | 1430 #endif |
1065 | 1431 |
1066 /* Check if an existing function is being overridden or deleted. If so, | 1432 /* Check if an existing function is being overridden or deleted. If so, |
1067 ** and there are active VMs, then return SQLITE_BUSY. If a function | 1433 ** and there are active VMs, then return SQLITE_BUSY. If a function |
1068 ** is being overridden/deleted but there are no active VMs, allow the | 1434 ** is being overridden/deleted but there are no active VMs, allow the |
1069 ** operation to continue but invalidate all precompiled statements. | 1435 ** operation to continue but invalidate all precompiled statements. |
1070 */ | 1436 */ |
1071 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0); | 1437 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0); |
1072 if( p && p->iPrefEnc==enc && p->nArg==nArg ){ | 1438 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){ |
1073 if( db->activeVdbeCnt ){ | 1439 if( db->nVdbeActive ){ |
1074 sqlite3Error(db, SQLITE_BUSY, | 1440 sqlite3ErrorWithMsg(db, SQLITE_BUSY, |
1075 "unable to delete/modify user-function due to active statements"); | 1441 "unable to delete/modify user-function due to active statements"); |
1076 assert( !db->mallocFailed ); | 1442 assert( !db->mallocFailed ); |
1077 return SQLITE_BUSY; | 1443 return SQLITE_BUSY; |
1078 }else{ | 1444 }else{ |
1079 sqlite3ExpirePreparedStatements(db); | 1445 sqlite3ExpirePreparedStatements(db); |
1080 } | 1446 } |
1081 } | 1447 } |
1082 | 1448 |
1083 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); | 1449 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); |
1084 assert(p || db->mallocFailed); | 1450 assert(p || db->mallocFailed); |
1085 if( !p ){ | 1451 if( !p ){ |
1086 return SQLITE_NOMEM; | 1452 return SQLITE_NOMEM; |
1087 } | 1453 } |
1088 | 1454 |
1089 /* If an older version of the function with a configured destructor is | 1455 /* If an older version of the function with a configured destructor is |
1090 ** being replaced invoke the destructor function here. */ | 1456 ** being replaced invoke the destructor function here. */ |
1091 functionDestroy(db, p); | 1457 functionDestroy(db, p); |
1092 | 1458 |
1093 if( pDestructor ){ | 1459 if( pDestructor ){ |
1094 pDestructor->nRef++; | 1460 pDestructor->nRef++; |
1095 } | 1461 } |
1096 p->pDestructor = pDestructor; | 1462 p->pDestructor = pDestructor; |
1097 p->flags = 0; | 1463 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags; |
| 1464 testcase( p->funcFlags & SQLITE_DETERMINISTIC ); |
1098 p->xFunc = xFunc; | 1465 p->xFunc = xFunc; |
1099 p->xStep = xStep; | 1466 p->xStep = xStep; |
1100 p->xFinalize = xFinal; | 1467 p->xFinalize = xFinal; |
1101 p->pUserData = pUserData; | 1468 p->pUserData = pUserData; |
1102 p->nArg = (u16)nArg; | 1469 p->nArg = (u16)nArg; |
1103 return SQLITE_OK; | 1470 return SQLITE_OK; |
1104 } | 1471 } |
1105 | 1472 |
1106 /* | 1473 /* |
1107 ** Create new user functions. | 1474 ** Create new user functions. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1192 ** should call this routine to make sure the global function exists. | 1559 ** should call this routine to make sure the global function exists. |
1193 ** A global function must exist in order for name resolution to work | 1560 ** A global function must exist in order for name resolution to work |
1194 ** properly. | 1561 ** properly. |
1195 */ | 1562 */ |
1196 int sqlite3_overload_function( | 1563 int sqlite3_overload_function( |
1197 sqlite3 *db, | 1564 sqlite3 *db, |
1198 const char *zName, | 1565 const char *zName, |
1199 int nArg | 1566 int nArg |
1200 ){ | 1567 ){ |
1201 int nName = sqlite3Strlen30(zName); | 1568 int nName = sqlite3Strlen30(zName); |
1202 int rc; | 1569 int rc = SQLITE_OK; |
1203 sqlite3_mutex_enter(db->mutex); | 1570 sqlite3_mutex_enter(db->mutex); |
1204 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ | 1571 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
1205 sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, | 1572 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
1206 0, sqlite3InvalidFunction, 0, 0, 0); | 1573 0, sqlite3InvalidFunction, 0, 0, 0); |
1207 } | 1574 } |
1208 rc = sqlite3ApiExit(db, SQLITE_OK); | 1575 rc = sqlite3ApiExit(db, rc); |
1209 sqlite3_mutex_leave(db->mutex); | 1576 sqlite3_mutex_leave(db->mutex); |
1210 return rc; | 1577 return rc; |
1211 } | 1578 } |
1212 | 1579 |
1213 #ifndef SQLITE_OMIT_TRACE | 1580 #ifndef SQLITE_OMIT_TRACE |
1214 /* | 1581 /* |
1215 ** Register a trace function. The pArg from the previously registered trace | 1582 ** Register a trace function. The pArg from the previously registered trace |
1216 ** is returned. | 1583 ** is returned. |
1217 ** | 1584 ** |
1218 ** A NULL trace function means that no tracing is executes. A non-NULL | 1585 ** A NULL trace function means that no tracing is executes. A non-NULL |
(...skipping 25 matching lines...) Expand all Loading... |
1244 void *pOld; | 1611 void *pOld; |
1245 sqlite3_mutex_enter(db->mutex); | 1612 sqlite3_mutex_enter(db->mutex); |
1246 pOld = db->pProfileArg; | 1613 pOld = db->pProfileArg; |
1247 db->xProfile = xProfile; | 1614 db->xProfile = xProfile; |
1248 db->pProfileArg = pArg; | 1615 db->pProfileArg = pArg; |
1249 sqlite3_mutex_leave(db->mutex); | 1616 sqlite3_mutex_leave(db->mutex); |
1250 return pOld; | 1617 return pOld; |
1251 } | 1618 } |
1252 #endif /* SQLITE_OMIT_TRACE */ | 1619 #endif /* SQLITE_OMIT_TRACE */ |
1253 | 1620 |
1254 /*** EXPERIMENTAL *** | 1621 /* |
1255 ** | 1622 ** Register a function to be invoked when a transaction commits. |
1256 ** Register a function to be invoked when a transaction comments. | |
1257 ** If the invoked function returns non-zero, then the commit becomes a | 1623 ** If the invoked function returns non-zero, then the commit becomes a |
1258 ** rollback. | 1624 ** rollback. |
1259 */ | 1625 */ |
1260 void *sqlite3_commit_hook( | 1626 void *sqlite3_commit_hook( |
1261 sqlite3 *db, /* Attach the hook to this database */ | 1627 sqlite3 *db, /* Attach the hook to this database */ |
1262 int (*xCallback)(void*), /* Function to invoke on each commit */ | 1628 int (*xCallback)(void*), /* Function to invoke on each commit */ |
1263 void *pArg /* Argument to the function */ | 1629 void *pArg /* Argument to the function */ |
1264 ){ | 1630 ){ |
1265 void *pOld; | 1631 void *pOld; |
1266 sqlite3_mutex_enter(db->mutex); | 1632 sqlite3_mutex_enter(db->mutex); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1402 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){ | 1768 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){ |
1403 return SQLITE_MISUSE; | 1769 return SQLITE_MISUSE; |
1404 } | 1770 } |
1405 | 1771 |
1406 sqlite3_mutex_enter(db->mutex); | 1772 sqlite3_mutex_enter(db->mutex); |
1407 if( zDb && zDb[0] ){ | 1773 if( zDb && zDb[0] ){ |
1408 iDb = sqlite3FindDbName(db, zDb); | 1774 iDb = sqlite3FindDbName(db, zDb); |
1409 } | 1775 } |
1410 if( iDb<0 ){ | 1776 if( iDb<0 ){ |
1411 rc = SQLITE_ERROR; | 1777 rc = SQLITE_ERROR; |
1412 sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb); | 1778 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb); |
1413 }else{ | 1779 }else{ |
1414 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); | 1780 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); |
1415 sqlite3Error(db, rc, 0); | 1781 sqlite3Error(db, rc); |
1416 } | 1782 } |
1417 rc = sqlite3ApiExit(db, rc); | 1783 rc = sqlite3ApiExit(db, rc); |
1418 sqlite3_mutex_leave(db->mutex); | 1784 sqlite3_mutex_leave(db->mutex); |
1419 return rc; | 1785 return rc; |
1420 #endif | 1786 #endif |
1421 } | 1787 } |
1422 | 1788 |
1423 | 1789 |
1424 /* | 1790 /* |
1425 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points | 1791 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1518 if( !db ){ | 1884 if( !db ){ |
1519 return sqlite3ErrStr(SQLITE_NOMEM); | 1885 return sqlite3ErrStr(SQLITE_NOMEM); |
1520 } | 1886 } |
1521 if( !sqlite3SafetyCheckSickOrOk(db) ){ | 1887 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
1522 return sqlite3ErrStr(SQLITE_MISUSE_BKPT); | 1888 return sqlite3ErrStr(SQLITE_MISUSE_BKPT); |
1523 } | 1889 } |
1524 sqlite3_mutex_enter(db->mutex); | 1890 sqlite3_mutex_enter(db->mutex); |
1525 if( db->mallocFailed ){ | 1891 if( db->mallocFailed ){ |
1526 z = sqlite3ErrStr(SQLITE_NOMEM); | 1892 z = sqlite3ErrStr(SQLITE_NOMEM); |
1527 }else{ | 1893 }else{ |
| 1894 testcase( db->pErr==0 ); |
1528 z = (char*)sqlite3_value_text(db->pErr); | 1895 z = (char*)sqlite3_value_text(db->pErr); |
1529 assert( !db->mallocFailed ); | 1896 assert( !db->mallocFailed ); |
1530 if( z==0 ){ | 1897 if( z==0 ){ |
1531 z = sqlite3ErrStr(db->errCode); | 1898 z = sqlite3ErrStr(db->errCode); |
1532 } | 1899 } |
1533 } | 1900 } |
1534 sqlite3_mutex_leave(db->mutex); | 1901 sqlite3_mutex_leave(db->mutex); |
1535 return z; | 1902 return z; |
1536 } | 1903 } |
1537 | 1904 |
(...skipping 21 matching lines...) Expand all Loading... |
1559 } | 1926 } |
1560 if( !sqlite3SafetyCheckSickOrOk(db) ){ | 1927 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
1561 return (void *)misuse; | 1928 return (void *)misuse; |
1562 } | 1929 } |
1563 sqlite3_mutex_enter(db->mutex); | 1930 sqlite3_mutex_enter(db->mutex); |
1564 if( db->mallocFailed ){ | 1931 if( db->mallocFailed ){ |
1565 z = (void *)outOfMem; | 1932 z = (void *)outOfMem; |
1566 }else{ | 1933 }else{ |
1567 z = sqlite3_value_text16(db->pErr); | 1934 z = sqlite3_value_text16(db->pErr); |
1568 if( z==0 ){ | 1935 if( z==0 ){ |
1569 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), | 1936 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode)); |
1570 SQLITE_UTF8, SQLITE_STATIC); | |
1571 z = sqlite3_value_text16(db->pErr); | 1937 z = sqlite3_value_text16(db->pErr); |
1572 } | 1938 } |
1573 /* A malloc() may have failed within the call to sqlite3_value_text16() | 1939 /* A malloc() may have failed within the call to sqlite3_value_text16() |
1574 ** above. If this is the case, then the db->mallocFailed flag needs to | 1940 ** above. If this is the case, then the db->mallocFailed flag needs to |
1575 ** be cleared before returning. Do this directly, instead of via | 1941 ** be cleared before returning. Do this directly, instead of via |
1576 ** sqlite3ApiExit(), to avoid setting the database handle error message. | 1942 ** sqlite3ApiExit(), to avoid setting the database handle error message. |
1577 */ | 1943 */ |
1578 db->mallocFailed = 0; | 1944 db->mallocFailed = 0; |
1579 } | 1945 } |
1580 sqlite3_mutex_leave(db->mutex); | 1946 sqlite3_mutex_leave(db->mutex); |
(...skipping 18 matching lines...) Expand all Loading... |
1599 if( db && !sqlite3SafetyCheckSickOrOk(db) ){ | 1965 if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
1600 return SQLITE_MISUSE_BKPT; | 1966 return SQLITE_MISUSE_BKPT; |
1601 } | 1967 } |
1602 if( !db || db->mallocFailed ){ | 1968 if( !db || db->mallocFailed ){ |
1603 return SQLITE_NOMEM; | 1969 return SQLITE_NOMEM; |
1604 } | 1970 } |
1605 return db->errCode; | 1971 return db->errCode; |
1606 } | 1972 } |
1607 | 1973 |
1608 /* | 1974 /* |
| 1975 ** Return a string that describes the kind of error specified in the |
| 1976 ** argument. For now, this simply calls the internal sqlite3ErrStr() |
| 1977 ** function. |
| 1978 */ |
| 1979 const char *sqlite3_errstr(int rc){ |
| 1980 return sqlite3ErrStr(rc); |
| 1981 } |
| 1982 |
| 1983 /* |
| 1984 ** Invalidate all cached KeyInfo objects for database connection "db" |
| 1985 */ |
| 1986 static void invalidateCachedKeyInfo(sqlite3 *db){ |
| 1987 Db *pDb; /* A single database */ |
| 1988 int iDb; /* The database index number */ |
| 1989 HashElem *k; /* For looping over tables in pDb */ |
| 1990 Table *pTab; /* A table in the database */ |
| 1991 Index *pIdx; /* Each index */ |
| 1992 |
| 1993 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
| 1994 if( pDb->pBt==0 ) continue; |
| 1995 sqlite3BtreeEnter(pDb->pBt); |
| 1996 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
| 1997 pTab = (Table*)sqliteHashData(k); |
| 1998 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1999 if( pIdx->pKeyInfo && pIdx->pKeyInfo->db==db ){ |
| 2000 sqlite3KeyInfoUnref(pIdx->pKeyInfo); |
| 2001 pIdx->pKeyInfo = 0; |
| 2002 } |
| 2003 } |
| 2004 } |
| 2005 sqlite3BtreeLeave(pDb->pBt); |
| 2006 } |
| 2007 } |
| 2008 |
| 2009 /* |
1609 ** Create a new collating function for database "db". The name is zName | 2010 ** Create a new collating function for database "db". The name is zName |
1610 ** and the encoding is enc. | 2011 ** and the encoding is enc. |
1611 */ | 2012 */ |
1612 static int createCollation( | 2013 static int createCollation( |
1613 sqlite3* db, | 2014 sqlite3* db, |
1614 const char *zName, | 2015 const char *zName, |
1615 u8 enc, | 2016 u8 enc, |
1616 u8 collType, | |
1617 void* pCtx, | 2017 void* pCtx, |
1618 int(*xCompare)(void*,int,const void*,int,const void*), | 2018 int(*xCompare)(void*,int,const void*,int,const void*), |
1619 void(*xDel)(void*) | 2019 void(*xDel)(void*) |
1620 ){ | 2020 ){ |
1621 CollSeq *pColl; | 2021 CollSeq *pColl; |
1622 int enc2; | 2022 int enc2; |
1623 int nName = sqlite3Strlen30(zName); | |
1624 | 2023 |
1625 assert( sqlite3_mutex_held(db->mutex) ); | 2024 assert( sqlite3_mutex_held(db->mutex) ); |
1626 | 2025 |
1627 /* If SQLITE_UTF16 is specified as the encoding type, transform this | 2026 /* If SQLITE_UTF16 is specified as the encoding type, transform this |
1628 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the | 2027 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
1629 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. | 2028 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
1630 */ | 2029 */ |
1631 enc2 = enc; | 2030 enc2 = enc; |
1632 testcase( enc2==SQLITE_UTF16 ); | 2031 testcase( enc2==SQLITE_UTF16 ); |
1633 testcase( enc2==SQLITE_UTF16_ALIGNED ); | 2032 testcase( enc2==SQLITE_UTF16_ALIGNED ); |
1634 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ | 2033 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ |
1635 enc2 = SQLITE_UTF16NATIVE; | 2034 enc2 = SQLITE_UTF16NATIVE; |
1636 } | 2035 } |
1637 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){ | 2036 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){ |
1638 return SQLITE_MISUSE_BKPT; | 2037 return SQLITE_MISUSE_BKPT; |
1639 } | 2038 } |
1640 | 2039 |
1641 /* Check if this call is removing or replacing an existing collation | 2040 /* Check if this call is removing or replacing an existing collation |
1642 ** sequence. If so, and there are active VMs, return busy. If there | 2041 ** sequence. If so, and there are active VMs, return busy. If there |
1643 ** are no active VMs, invalidate any pre-compiled statements. | 2042 ** are no active VMs, invalidate any pre-compiled statements. |
1644 */ | 2043 */ |
1645 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); | 2044 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); |
1646 if( pColl && pColl->xCmp ){ | 2045 if( pColl && pColl->xCmp ){ |
1647 if( db->activeVdbeCnt ){ | 2046 if( db->nVdbeActive ){ |
1648 sqlite3Error(db, SQLITE_BUSY, | 2047 sqlite3ErrorWithMsg(db, SQLITE_BUSY, |
1649 "unable to delete/modify collation sequence due to active statements"); | 2048 "unable to delete/modify collation sequence due to active statements"); |
1650 return SQLITE_BUSY; | 2049 return SQLITE_BUSY; |
1651 } | 2050 } |
1652 sqlite3ExpirePreparedStatements(db); | 2051 sqlite3ExpirePreparedStatements(db); |
| 2052 invalidateCachedKeyInfo(db); |
1653 | 2053 |
1654 /* If collation sequence pColl was created directly by a call to | 2054 /* If collation sequence pColl was created directly by a call to |
1655 ** sqlite3_create_collation, and not generated by synthCollSeq(), | 2055 ** sqlite3_create_collation, and not generated by synthCollSeq(), |
1656 ** then any copies made by synthCollSeq() need to be invalidated. | 2056 ** then any copies made by synthCollSeq() need to be invalidated. |
1657 ** Also, collation destructor - CollSeq.xDel() - function may need | 2057 ** Also, collation destructor - CollSeq.xDel() - function may need |
1658 ** to be called. | 2058 ** to be called. |
1659 */ | 2059 */ |
1660 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ | 2060 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ |
1661 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName); | 2061 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName); |
1662 int j; | 2062 int j; |
1663 for(j=0; j<3; j++){ | 2063 for(j=0; j<3; j++){ |
1664 CollSeq *p = &aColl[j]; | 2064 CollSeq *p = &aColl[j]; |
1665 if( p->enc==pColl->enc ){ | 2065 if( p->enc==pColl->enc ){ |
1666 if( p->xDel ){ | 2066 if( p->xDel ){ |
1667 p->xDel(p->pUser); | 2067 p->xDel(p->pUser); |
1668 } | 2068 } |
1669 p->xCmp = 0; | 2069 p->xCmp = 0; |
1670 } | 2070 } |
1671 } | 2071 } |
1672 } | 2072 } |
1673 } | 2073 } |
1674 | 2074 |
1675 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); | 2075 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); |
1676 if( pColl==0 ) return SQLITE_NOMEM; | 2076 if( pColl==0 ) return SQLITE_NOMEM; |
1677 pColl->xCmp = xCompare; | 2077 pColl->xCmp = xCompare; |
1678 pColl->pUser = pCtx; | 2078 pColl->pUser = pCtx; |
1679 pColl->xDel = xDel; | 2079 pColl->xDel = xDel; |
1680 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); | 2080 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); |
1681 pColl->type = collType; | 2081 sqlite3Error(db, SQLITE_OK); |
1682 sqlite3Error(db, SQLITE_OK, 0); | |
1683 return SQLITE_OK; | 2082 return SQLITE_OK; |
1684 } | 2083 } |
1685 | 2084 |
1686 | 2085 |
1687 /* | 2086 /* |
1688 ** This array defines hard upper bounds on limit values. The | 2087 ** This array defines hard upper bounds on limit values. The |
1689 ** initializer must be kept in sync with the SQLITE_LIMIT_* | 2088 ** initializer must be kept in sync with the SQLITE_LIMIT_* |
1690 ** #defines in sqlite3.h. | 2089 ** #defines in sqlite3.h. |
1691 */ | 2090 */ |
1692 static const int aHardLimit[] = { | 2091 static const int aHardLimit[] = { |
1693 SQLITE_MAX_LENGTH, | 2092 SQLITE_MAX_LENGTH, |
1694 SQLITE_MAX_SQL_LENGTH, | 2093 SQLITE_MAX_SQL_LENGTH, |
1695 SQLITE_MAX_COLUMN, | 2094 SQLITE_MAX_COLUMN, |
1696 SQLITE_MAX_EXPR_DEPTH, | 2095 SQLITE_MAX_EXPR_DEPTH, |
1697 SQLITE_MAX_COMPOUND_SELECT, | 2096 SQLITE_MAX_COMPOUND_SELECT, |
1698 SQLITE_MAX_VDBE_OP, | 2097 SQLITE_MAX_VDBE_OP, |
1699 SQLITE_MAX_FUNCTION_ARG, | 2098 SQLITE_MAX_FUNCTION_ARG, |
1700 SQLITE_MAX_ATTACHED, | 2099 SQLITE_MAX_ATTACHED, |
1701 SQLITE_MAX_LIKE_PATTERN_LENGTH, | 2100 SQLITE_MAX_LIKE_PATTERN_LENGTH, |
1702 SQLITE_MAX_VARIABLE_NUMBER, | 2101 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */ |
1703 SQLITE_MAX_TRIGGER_DEPTH, | 2102 SQLITE_MAX_TRIGGER_DEPTH, |
| 2103 SQLITE_MAX_WORKER_THREADS, |
1704 }; | 2104 }; |
1705 | 2105 |
1706 /* | 2106 /* |
1707 ** Make sure the hard limits are set to reasonable values | 2107 ** Make sure the hard limits are set to reasonable values |
1708 */ | 2108 */ |
1709 #if SQLITE_MAX_LENGTH<100 | 2109 #if SQLITE_MAX_LENGTH<100 |
1710 # error SQLITE_MAX_LENGTH must be at least 100 | 2110 # error SQLITE_MAX_LENGTH must be at least 100 |
1711 #endif | 2111 #endif |
1712 #if SQLITE_MAX_SQL_LENGTH<100 | 2112 #if SQLITE_MAX_SQL_LENGTH<100 |
1713 # error SQLITE_MAX_SQL_LENGTH must be at least 100 | 2113 # error SQLITE_MAX_SQL_LENGTH must be at least 100 |
1714 #endif | 2114 #endif |
1715 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH | 2115 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH |
1716 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH | 2116 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH |
1717 #endif | 2117 #endif |
1718 #if SQLITE_MAX_COMPOUND_SELECT<2 | 2118 #if SQLITE_MAX_COMPOUND_SELECT<2 |
1719 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 | 2119 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 |
1720 #endif | 2120 #endif |
1721 #if SQLITE_MAX_VDBE_OP<40 | 2121 #if SQLITE_MAX_VDBE_OP<40 |
1722 # error SQLITE_MAX_VDBE_OP must be at least 40 | 2122 # error SQLITE_MAX_VDBE_OP must be at least 40 |
1723 #endif | 2123 #endif |
1724 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000 | 2124 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000 |
1725 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000 | 2125 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000 |
1726 #endif | 2126 #endif |
1727 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62 | 2127 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125 |
1728 # error SQLITE_MAX_ATTACHED must be between 0 and 62 | 2128 # error SQLITE_MAX_ATTACHED must be between 0 and 125 |
1729 #endif | 2129 #endif |
1730 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 | 2130 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 |
1731 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 | 2131 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 |
1732 #endif | 2132 #endif |
1733 #if SQLITE_MAX_COLUMN>32767 | 2133 #if SQLITE_MAX_COLUMN>32767 |
1734 # error SQLITE_MAX_COLUMN must not exceed 32767 | 2134 # error SQLITE_MAX_COLUMN must not exceed 32767 |
1735 #endif | 2135 #endif |
1736 #if SQLITE_MAX_TRIGGER_DEPTH<1 | 2136 #if SQLITE_MAX_TRIGGER_DEPTH<1 |
1737 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 | 2137 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 |
1738 #endif | 2138 #endif |
| 2139 #if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50 |
| 2140 # error SQLITE_MAX_WORKER_THREADS must be between 0 and 50 |
| 2141 #endif |
1739 | 2142 |
1740 | 2143 |
1741 /* | 2144 /* |
1742 ** Change the value of a limit. Report the old value. | 2145 ** Change the value of a limit. Report the old value. |
1743 ** If an invalid limit index is supplied, report -1. | 2146 ** If an invalid limit index is supplied, report -1. |
1744 ** Make no changes but still report the old value if the | 2147 ** Make no changes but still report the old value if the |
1745 ** new limit is negative. | 2148 ** new limit is negative. |
1746 ** | 2149 ** |
1747 ** A new lower limit does not shrink existing constructs. | 2150 ** A new lower limit does not shrink existing constructs. |
1748 ** It merely prevents new constructs that exceed the limit | 2151 ** It merely prevents new constructs that exceed the limit |
(...skipping 13 matching lines...) Expand all Loading... |
1762 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); | 2165 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); |
1763 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); | 2166 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); |
1764 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); | 2167 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); |
1765 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); | 2168 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); |
1766 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); | 2169 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); |
1767 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); | 2170 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); |
1768 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== | 2171 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== |
1769 SQLITE_MAX_LIKE_PATTERN_LENGTH ); | 2172 SQLITE_MAX_LIKE_PATTERN_LENGTH ); |
1770 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); | 2173 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); |
1771 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); | 2174 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); |
1772 assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) ); | 2175 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS ); |
| 2176 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) ); |
1773 | 2177 |
1774 | 2178 |
1775 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ | 2179 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
1776 return -1; | 2180 return -1; |
1777 } | 2181 } |
1778 oldLimit = db->aLimit[limitId]; | 2182 oldLimit = db->aLimit[limitId]; |
1779 if( newLimit>=0 ){ /* IMP: R-52476-28732 */ | 2183 if( newLimit>=0 ){ /* IMP: R-52476-28732 */ |
1780 if( newLimit>aHardLimit[limitId] ){ | 2184 if( newLimit>aHardLimit[limitId] ){ |
1781 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ | 2185 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ |
1782 } | 2186 } |
1783 db->aLimit[limitId] = newLimit; | 2187 db->aLimit[limitId] = newLimit; |
1784 } | 2188 } |
1785 return oldLimit; /* IMP: R-53341-35419 */ | 2189 return oldLimit; /* IMP: R-53341-35419 */ |
1786 } | 2190 } |
1787 | 2191 |
1788 /* | 2192 /* |
| 2193 ** This function is used to parse both URIs and non-URI filenames passed by the |
| 2194 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database |
| 2195 ** URIs specified as part of ATTACH statements. |
| 2196 ** |
| 2197 ** The first argument to this function is the name of the VFS to use (or |
| 2198 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx" |
| 2199 ** query parameter. The second argument contains the URI (or non-URI filename) |
| 2200 ** itself. When this function is called the *pFlags variable should contain |
| 2201 ** the default flags to open the database handle with. The value stored in |
| 2202 ** *pFlags may be updated before returning if the URI filename contains |
| 2203 ** "cache=xxx" or "mode=xxx" query parameters. |
| 2204 ** |
| 2205 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to |
| 2206 ** the VFS that should be used to open the database file. *pzFile is set to |
| 2207 ** point to a buffer containing the name of the file to open. It is the |
| 2208 ** responsibility of the caller to eventually call sqlite3_free() to release |
| 2209 ** this buffer. |
| 2210 ** |
| 2211 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg |
| 2212 ** may be set to point to a buffer containing an English language error |
| 2213 ** message. It is the responsibility of the caller to eventually release |
| 2214 ** this buffer by calling sqlite3_free(). |
| 2215 */ |
| 2216 int sqlite3ParseUri( |
| 2217 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */ |
| 2218 const char *zUri, /* Nul-terminated URI to parse */ |
| 2219 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */ |
| 2220 sqlite3_vfs **ppVfs, /* OUT: VFS to use */ |
| 2221 char **pzFile, /* OUT: Filename component of URI */ |
| 2222 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */ |
| 2223 ){ |
| 2224 int rc = SQLITE_OK; |
| 2225 unsigned int flags = *pFlags; |
| 2226 const char *zVfs = zDefaultVfs; |
| 2227 char *zFile; |
| 2228 char c; |
| 2229 int nUri = sqlite3Strlen30(zUri); |
| 2230 |
| 2231 assert( *pzErrMsg==0 ); |
| 2232 |
| 2233 if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) |
| 2234 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 2235 ){ |
| 2236 char *zOpt; |
| 2237 int eState; /* Parser state when parsing URI */ |
| 2238 int iIn; /* Input character index */ |
| 2239 int iOut = 0; /* Output character index */ |
| 2240 int nByte = nUri+2; /* Bytes of space to allocate */ |
| 2241 |
| 2242 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen |
| 2243 ** method that there may be extra parameters following the file-name. */ |
| 2244 flags |= SQLITE_OPEN_URI; |
| 2245 |
| 2246 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&'); |
| 2247 zFile = sqlite3_malloc(nByte); |
| 2248 if( !zFile ) return SQLITE_NOMEM; |
| 2249 |
| 2250 iIn = 5; |
| 2251 #ifndef SQLITE_ALLOW_URI_AUTHORITY |
| 2252 /* Discard the scheme and authority segments of the URI. */ |
| 2253 if( zUri[5]=='/' && zUri[6]=='/' ){ |
| 2254 iIn = 7; |
| 2255 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++; |
| 2256 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){ |
| 2257 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", |
| 2258 iIn-7, &zUri[7]); |
| 2259 rc = SQLITE_ERROR; |
| 2260 goto parse_uri_out; |
| 2261 } |
| 2262 } |
| 2263 #endif |
| 2264 |
| 2265 /* Copy the filename and any query parameters into the zFile buffer. |
| 2266 ** Decode %HH escape codes along the way. |
| 2267 ** |
| 2268 ** Within this loop, variable eState may be set to 0, 1 or 2, depending |
| 2269 ** on the parsing context. As follows: |
| 2270 ** |
| 2271 ** 0: Parsing file-name. |
| 2272 ** 1: Parsing name section of a name=value query parameter. |
| 2273 ** 2: Parsing value section of a name=value query parameter. |
| 2274 */ |
| 2275 eState = 0; |
| 2276 while( (c = zUri[iIn])!=0 && c!='#' ){ |
| 2277 iIn++; |
| 2278 if( c=='%' |
| 2279 && sqlite3Isxdigit(zUri[iIn]) |
| 2280 && sqlite3Isxdigit(zUri[iIn+1]) |
| 2281 ){ |
| 2282 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4); |
| 2283 octet += sqlite3HexToInt(zUri[iIn++]); |
| 2284 |
| 2285 assert( octet>=0 && octet<256 ); |
| 2286 if( octet==0 ){ |
| 2287 /* This branch is taken when "%00" appears within the URI. In this |
| 2288 ** case we ignore all text in the remainder of the path, name or |
| 2289 ** value currently being parsed. So ignore the current character |
| 2290 ** and skip to the next "?", "=" or "&", as appropriate. */ |
| 2291 while( (c = zUri[iIn])!=0 && c!='#' |
| 2292 && (eState!=0 || c!='?') |
| 2293 && (eState!=1 || (c!='=' && c!='&')) |
| 2294 && (eState!=2 || c!='&') |
| 2295 ){ |
| 2296 iIn++; |
| 2297 } |
| 2298 continue; |
| 2299 } |
| 2300 c = octet; |
| 2301 }else if( eState==1 && (c=='&' || c=='=') ){ |
| 2302 if( zFile[iOut-1]==0 ){ |
| 2303 /* An empty option name. Ignore this option altogether. */ |
| 2304 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++; |
| 2305 continue; |
| 2306 } |
| 2307 if( c=='&' ){ |
| 2308 zFile[iOut++] = '\0'; |
| 2309 }else{ |
| 2310 eState = 2; |
| 2311 } |
| 2312 c = 0; |
| 2313 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){ |
| 2314 c = 0; |
| 2315 eState = 1; |
| 2316 } |
| 2317 zFile[iOut++] = c; |
| 2318 } |
| 2319 if( eState==1 ) zFile[iOut++] = '\0'; |
| 2320 zFile[iOut++] = '\0'; |
| 2321 zFile[iOut++] = '\0'; |
| 2322 |
| 2323 /* Check if there were any options specified that should be interpreted |
| 2324 ** here. Options that are interpreted here include "vfs" and those that |
| 2325 ** correspond to flags that may be passed to the sqlite3_open_v2() |
| 2326 ** method. */ |
| 2327 zOpt = &zFile[sqlite3Strlen30(zFile)+1]; |
| 2328 while( zOpt[0] ){ |
| 2329 int nOpt = sqlite3Strlen30(zOpt); |
| 2330 char *zVal = &zOpt[nOpt+1]; |
| 2331 int nVal = sqlite3Strlen30(zVal); |
| 2332 |
| 2333 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){ |
| 2334 zVfs = zVal; |
| 2335 }else{ |
| 2336 struct OpenMode { |
| 2337 const char *z; |
| 2338 int mode; |
| 2339 } *aMode = 0; |
| 2340 char *zModeType = 0; |
| 2341 int mask = 0; |
| 2342 int limit = 0; |
| 2343 |
| 2344 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){ |
| 2345 static struct OpenMode aCacheMode[] = { |
| 2346 { "shared", SQLITE_OPEN_SHAREDCACHE }, |
| 2347 { "private", SQLITE_OPEN_PRIVATECACHE }, |
| 2348 { 0, 0 } |
| 2349 }; |
| 2350 |
| 2351 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE; |
| 2352 aMode = aCacheMode; |
| 2353 limit = mask; |
| 2354 zModeType = "cache"; |
| 2355 } |
| 2356 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){ |
| 2357 static struct OpenMode aOpenMode[] = { |
| 2358 { "ro", SQLITE_OPEN_READONLY }, |
| 2359 { "rw", SQLITE_OPEN_READWRITE }, |
| 2360 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE }, |
| 2361 { "memory", SQLITE_OPEN_MEMORY }, |
| 2362 { 0, 0 } |
| 2363 }; |
| 2364 |
| 2365 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE |
| 2366 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY; |
| 2367 aMode = aOpenMode; |
| 2368 limit = mask & flags; |
| 2369 zModeType = "access"; |
| 2370 } |
| 2371 |
| 2372 if( aMode ){ |
| 2373 int i; |
| 2374 int mode = 0; |
| 2375 for(i=0; aMode[i].z; i++){ |
| 2376 const char *z = aMode[i].z; |
| 2377 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){ |
| 2378 mode = aMode[i].mode; |
| 2379 break; |
| 2380 } |
| 2381 } |
| 2382 if( mode==0 ){ |
| 2383 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal); |
| 2384 rc = SQLITE_ERROR; |
| 2385 goto parse_uri_out; |
| 2386 } |
| 2387 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){ |
| 2388 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s", |
| 2389 zModeType, zVal); |
| 2390 rc = SQLITE_PERM; |
| 2391 goto parse_uri_out; |
| 2392 } |
| 2393 flags = (flags & ~mask) | mode; |
| 2394 } |
| 2395 } |
| 2396 |
| 2397 zOpt = &zVal[nVal+1]; |
| 2398 } |
| 2399 |
| 2400 }else{ |
| 2401 zFile = sqlite3_malloc(nUri+2); |
| 2402 if( !zFile ) return SQLITE_NOMEM; |
| 2403 memcpy(zFile, zUri, nUri); |
| 2404 zFile[nUri] = '\0'; |
| 2405 zFile[nUri+1] = '\0'; |
| 2406 flags &= ~SQLITE_OPEN_URI; |
| 2407 } |
| 2408 |
| 2409 *ppVfs = sqlite3_vfs_find(zVfs); |
| 2410 if( *ppVfs==0 ){ |
| 2411 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs); |
| 2412 rc = SQLITE_ERROR; |
| 2413 } |
| 2414 parse_uri_out: |
| 2415 if( rc!=SQLITE_OK ){ |
| 2416 sqlite3_free(zFile); |
| 2417 zFile = 0; |
| 2418 } |
| 2419 *pFlags = flags; |
| 2420 *pzFile = zFile; |
| 2421 return rc; |
| 2422 } |
| 2423 |
| 2424 |
| 2425 /* |
1789 ** This routine does the work of opening a database on behalf of | 2426 ** This routine does the work of opening a database on behalf of |
1790 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" | 2427 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
1791 ** is UTF-8 encoded. | 2428 ** is UTF-8 encoded. |
1792 */ | 2429 */ |
1793 static int openDatabase( | 2430 static int openDatabase( |
1794 const char *zFilename, /* Database filename UTF-8 encoded */ | 2431 const char *zFilename, /* Database filename UTF-8 encoded */ |
1795 sqlite3 **ppDb, /* OUT: Returned database handle */ | 2432 sqlite3 **ppDb, /* OUT: Returned database handle */ |
1796 unsigned flags, /* Operational flags */ | 2433 unsigned int flags, /* Operational flags */ |
1797 const char *zVfs /* Name of the VFS to use */ | 2434 const char *zVfs /* Name of the VFS to use */ |
1798 ){ | 2435 ){ |
1799 sqlite3 *db; | 2436 sqlite3 *db; /* Store allocated handle here */ |
1800 int rc; | 2437 int rc; /* Return code */ |
1801 int isThreadsafe; | 2438 int isThreadsafe; /* True for threadsafe connections */ |
| 2439 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 2440 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
1802 | 2441 |
1803 *ppDb = 0; | 2442 *ppDb = 0; |
1804 #ifndef SQLITE_OMIT_AUTOINIT | 2443 #ifndef SQLITE_OMIT_AUTOINIT |
1805 rc = sqlite3_initialize(); | 2444 rc = sqlite3_initialize(); |
1806 if( rc ) return rc; | 2445 if( rc ) return rc; |
1807 #endif | 2446 #endif |
1808 | 2447 |
1809 /* Only allow sensible combinations of bits in the flags argument. | 2448 /* Only allow sensible combinations of bits in the flags argument. |
1810 ** Throw an error if any non-sense combination is used. If we | 2449 ** Throw an error if any non-sense combination is used. If we |
1811 ** do not block illegal combinations here, it could trigger | 2450 ** do not block illegal combinations here, it could trigger |
1812 ** assert() statements in deeper layers. Sensible combinations | 2451 ** assert() statements in deeper layers. Sensible combinations |
1813 ** are: | 2452 ** are: |
1814 ** | 2453 ** |
1815 ** 1: SQLITE_OPEN_READONLY | 2454 ** 1: SQLITE_OPEN_READONLY |
1816 ** 2: SQLITE_OPEN_READWRITE | 2455 ** 2: SQLITE_OPEN_READWRITE |
1817 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | 2456 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1818 */ | 2457 */ |
1819 assert( SQLITE_OPEN_READONLY == 0x01 ); | 2458 assert( SQLITE_OPEN_READONLY == 0x01 ); |
1820 assert( SQLITE_OPEN_READWRITE == 0x02 ); | 2459 assert( SQLITE_OPEN_READWRITE == 0x02 ); |
1821 assert( SQLITE_OPEN_CREATE == 0x04 ); | 2460 assert( SQLITE_OPEN_CREATE == 0x04 ); |
1822 testcase( (1<<(flags&7))==0x02 ); /* READONLY */ | 2461 testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
1823 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ | 2462 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
1824 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ | 2463 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
1825 if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE; | 2464 if( ((1<<(flags&7)) & 0x46)==0 ){ |
| 2465 return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ |
| 2466 } |
1826 | 2467 |
1827 if( sqlite3GlobalConfig.bCoreMutex==0 ){ | 2468 if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
1828 isThreadsafe = 0; | 2469 isThreadsafe = 0; |
1829 }else if( flags & SQLITE_OPEN_NOMUTEX ){ | 2470 }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
1830 isThreadsafe = 0; | 2471 isThreadsafe = 0; |
1831 }else if( flags & SQLITE_OPEN_FULLMUTEX ){ | 2472 }else if( flags & SQLITE_OPEN_FULLMUTEX ){ |
1832 isThreadsafe = 1; | 2473 isThreadsafe = 1; |
1833 }else{ | 2474 }else{ |
1834 isThreadsafe = sqlite3GlobalConfig.bFullMutex; | 2475 isThreadsafe = sqlite3GlobalConfig.bFullMutex; |
1835 } | 2476 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1874 } | 2515 } |
1875 } | 2516 } |
1876 sqlite3_mutex_enter(db->mutex); | 2517 sqlite3_mutex_enter(db->mutex); |
1877 db->errMask = 0xff; | 2518 db->errMask = 0xff; |
1878 db->nDb = 2; | 2519 db->nDb = 2; |
1879 db->magic = SQLITE_MAGIC_BUSY; | 2520 db->magic = SQLITE_MAGIC_BUSY; |
1880 db->aDb = db->aDbStatic; | 2521 db->aDb = db->aDbStatic; |
1881 | 2522 |
1882 assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); | 2523 assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); |
1883 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); | 2524 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); |
| 2525 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS; |
1884 db->autoCommit = 1; | 2526 db->autoCommit = 1; |
1885 db->nextAutovac = -1; | 2527 db->nextAutovac = -1; |
| 2528 db->szMmap = sqlite3GlobalConfig.szMmap; |
1886 db->nextPagesize = 0; | 2529 db->nextPagesize = 0; |
1887 db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger | 2530 db->nMaxSorterMmap = 0x7FFFFFFF; |
| 2531 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill |
| 2532 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX |
| 2533 | SQLITE_AutoIndex |
| 2534 #endif |
1888 #if SQLITE_DEFAULT_FILE_FORMAT<4 | 2535 #if SQLITE_DEFAULT_FILE_FORMAT<4 |
1889 | SQLITE_LegacyFileFmt | 2536 | SQLITE_LegacyFileFmt |
1890 #endif | 2537 #endif |
1891 #ifdef SQLITE_ENABLE_LOAD_EXTENSION | 2538 #ifdef SQLITE_ENABLE_LOAD_EXTENSION |
1892 | SQLITE_LoadExtension | 2539 | SQLITE_LoadExtension |
1893 #endif | 2540 #endif |
1894 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS | 2541 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS |
1895 | SQLITE_RecTriggers | 2542 | SQLITE_RecTriggers |
1896 #endif | 2543 #endif |
1897 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS | 2544 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS |
1898 | SQLITE_ForeignKeys | 2545 | SQLITE_ForeignKeys |
1899 #endif | 2546 #endif |
1900 ; | 2547 ; |
1901 sqlite3HashInit(&db->aCollSeq); | 2548 sqlite3HashInit(&db->aCollSeq); |
1902 #ifndef SQLITE_OMIT_VIRTUALTABLE | 2549 #ifndef SQLITE_OMIT_VIRTUALTABLE |
1903 sqlite3HashInit(&db->aModule); | 2550 sqlite3HashInit(&db->aModule); |
1904 #endif | 2551 #endif |
1905 | 2552 |
1906 db->pVfs = sqlite3_vfs_find(zVfs); | |
1907 if( !db->pVfs ){ | |
1908 rc = SQLITE_ERROR; | |
1909 sqlite3Error(db, rc, "no such vfs: %s", zVfs); | |
1910 goto opendb_out; | |
1911 } | |
1912 | |
1913 /* Add the default collation sequence BINARY. BINARY works for both UTF-8 | 2553 /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
1914 ** and UTF-16, so add a version for each to avoid any unnecessary | 2554 ** and UTF-16, so add a version for each to avoid any unnecessary |
1915 ** conversions. The only error that can occur here is a malloc() failure. | 2555 ** conversions. The only error that can occur here is a malloc() failure. |
1916 */ | 2556 */ |
1917 createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0, | 2557 createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); |
1918 binCollFunc, 0); | 2558 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); |
1919 createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0, | 2559 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); |
1920 binCollFunc, 0); | 2560 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
1921 createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0, | |
1922 binCollFunc, 0); | |
1923 createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1, | |
1924 binCollFunc, 0); | |
1925 if( db->mallocFailed ){ | 2561 if( db->mallocFailed ){ |
1926 goto opendb_out; | 2562 goto opendb_out; |
1927 } | 2563 } |
1928 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); | 2564 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
1929 assert( db->pDfltColl!=0 ); | 2565 assert( db->pDfltColl!=0 ); |
1930 | 2566 |
1931 /* Also add a UTF-8 case-insensitive collation sequence. */ | 2567 /* Also add a UTF-8 case-insensitive collation sequence. */ |
1932 createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0, | 2568 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
1933 nocaseCollatingFunc, 0); | 2569 |
| 2570 /* Parse the filename/URI argument. */ |
| 2571 db->openFlags = flags; |
| 2572 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 2573 if( rc!=SQLITE_OK ){ |
| 2574 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| 2575 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg); |
| 2576 sqlite3_free(zErrMsg); |
| 2577 goto opendb_out; |
| 2578 } |
1934 | 2579 |
1935 /* Open the backend database driver */ | 2580 /* Open the backend database driver */ |
1936 db->openFlags = flags; | 2581 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0, |
1937 rc = sqlite3BtreeOpen(zFilename, db, &db->aDb[0].pBt, 0, | |
1938 flags | SQLITE_OPEN_MAIN_DB); | 2582 flags | SQLITE_OPEN_MAIN_DB); |
1939 if( rc!=SQLITE_OK ){ | 2583 if( rc!=SQLITE_OK ){ |
1940 if( rc==SQLITE_IOERR_NOMEM ){ | 2584 if( rc==SQLITE_IOERR_NOMEM ){ |
1941 rc = SQLITE_NOMEM; | 2585 rc = SQLITE_NOMEM; |
1942 } | 2586 } |
1943 sqlite3Error(db, rc, 0); | 2587 sqlite3Error(db, rc); |
1944 goto opendb_out; | 2588 goto opendb_out; |
1945 } | 2589 } |
| 2590 sqlite3BtreeEnter(db->aDb[0].pBt); |
1946 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); | 2591 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); |
| 2592 sqlite3BtreeLeave(db->aDb[0].pBt); |
1947 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); | 2593 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); |
1948 | 2594 |
1949 | |
1950 /* The default safety_level for the main database is 'full'; for the temp | 2595 /* The default safety_level for the main database is 'full'; for the temp |
1951 ** database it is 'NONE'. This matches the pager layer defaults. | 2596 ** database it is 'NONE'. This matches the pager layer defaults. |
1952 */ | 2597 */ |
1953 db->aDb[0].zName = "main"; | 2598 db->aDb[0].zName = "main"; |
1954 db->aDb[0].safety_level = 3; | 2599 db->aDb[0].safety_level = 3; |
1955 db->aDb[1].zName = "temp"; | 2600 db->aDb[1].zName = "temp"; |
1956 db->aDb[1].safety_level = 1; | 2601 db->aDb[1].safety_level = 1; |
1957 | 2602 |
1958 db->magic = SQLITE_MAGIC_OPEN; | 2603 db->magic = SQLITE_MAGIC_OPEN; |
1959 if( db->mallocFailed ){ | 2604 if( db->mallocFailed ){ |
1960 goto opendb_out; | 2605 goto opendb_out; |
1961 } | 2606 } |
1962 | 2607 |
1963 /* Register all built-in functions, but do not attempt to read the | 2608 /* Register all built-in functions, but do not attempt to read the |
1964 ** database schema yet. This is delayed until the first time the database | 2609 ** database schema yet. This is delayed until the first time the database |
1965 ** is accessed. | 2610 ** is accessed. |
1966 */ | 2611 */ |
1967 sqlite3Error(db, SQLITE_OK, 0); | 2612 sqlite3Error(db, SQLITE_OK); |
1968 sqlite3RegisterBuiltinFunctions(db); | 2613 sqlite3RegisterBuiltinFunctions(db); |
1969 | 2614 |
1970 /* Load automatic extensions - extensions that have been registered | 2615 /* Load automatic extensions - extensions that have been registered |
1971 ** using the sqlite3_automatic_extension() API. | 2616 ** using the sqlite3_automatic_extension() API. |
1972 */ | 2617 */ |
1973 sqlite3AutoLoadExtensions(db); | |
1974 rc = sqlite3_errcode(db); | 2618 rc = sqlite3_errcode(db); |
1975 if( rc!=SQLITE_OK ){ | 2619 if( rc==SQLITE_OK ){ |
1976 goto opendb_out; | 2620 sqlite3AutoLoadExtensions(db); |
| 2621 rc = sqlite3_errcode(db); |
| 2622 if( rc!=SQLITE_OK ){ |
| 2623 goto opendb_out; |
| 2624 } |
1977 } | 2625 } |
1978 | 2626 |
1979 #ifdef SQLITE_ENABLE_FTS1 | 2627 #ifdef SQLITE_ENABLE_FTS1 |
1980 if( !db->mallocFailed ){ | 2628 if( !db->mallocFailed ){ |
1981 extern int sqlite3Fts1Init(sqlite3*); | 2629 extern int sqlite3Fts1Init(sqlite3*); |
1982 rc = sqlite3Fts1Init(db); | 2630 rc = sqlite3Fts1Init(db); |
1983 } | 2631 } |
1984 #endif | 2632 #endif |
1985 | 2633 |
1986 #ifdef SQLITE_ENABLE_FTS2 | 2634 #ifdef SQLITE_ENABLE_FTS2 |
(...skipping 22 matching lines...) Expand all Loading... |
2009 rc = sqlite3IcuInit(db); | 2657 rc = sqlite3IcuInit(db); |
2010 } | 2658 } |
2011 #endif | 2659 #endif |
2012 | 2660 |
2013 #ifdef SQLITE_ENABLE_RTREE | 2661 #ifdef SQLITE_ENABLE_RTREE |
2014 if( !db->mallocFailed && rc==SQLITE_OK){ | 2662 if( !db->mallocFailed && rc==SQLITE_OK){ |
2015 rc = sqlite3RtreeInit(db); | 2663 rc = sqlite3RtreeInit(db); |
2016 } | 2664 } |
2017 #endif | 2665 #endif |
2018 | 2666 |
2019 sqlite3Error(db, rc, 0); | |
2020 | |
2021 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking | 2667 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking |
2022 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking | 2668 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking |
2023 ** mode. Doing nothing at all also makes NORMAL the default. | 2669 ** mode. Doing nothing at all also makes NORMAL the default. |
2024 */ | 2670 */ |
2025 #ifdef SQLITE_DEFAULT_LOCKING_MODE | 2671 #ifdef SQLITE_DEFAULT_LOCKING_MODE |
2026 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; | 2672 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; |
2027 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), | 2673 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), |
2028 SQLITE_DEFAULT_LOCKING_MODE); | 2674 SQLITE_DEFAULT_LOCKING_MODE); |
2029 #endif | 2675 #endif |
2030 | 2676 |
| 2677 if( rc ) sqlite3Error(db, rc); |
| 2678 |
2031 /* Enable the lookaside-malloc subsystem */ | 2679 /* Enable the lookaside-malloc subsystem */ |
2032 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, | 2680 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, |
2033 sqlite3GlobalConfig.nLookaside); | 2681 sqlite3GlobalConfig.nLookaside); |
2034 | 2682 |
2035 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); | 2683 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); |
2036 | 2684 |
2037 opendb_out: | 2685 opendb_out: |
| 2686 sqlite3_free(zOpen); |
2038 if( db ){ | 2687 if( db ){ |
2039 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0
); | 2688 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0
); |
2040 sqlite3_mutex_leave(db->mutex); | 2689 sqlite3_mutex_leave(db->mutex); |
2041 } | 2690 } |
2042 rc = sqlite3_errcode(db); | 2691 rc = sqlite3_errcode(db); |
| 2692 assert( db!=0 || rc==SQLITE_NOMEM ); |
2043 if( rc==SQLITE_NOMEM ){ | 2693 if( rc==SQLITE_NOMEM ){ |
2044 sqlite3_close(db); | 2694 sqlite3_close(db); |
2045 db = 0; | 2695 db = 0; |
2046 }else if( rc!=SQLITE_OK ){ | 2696 }else if( rc!=SQLITE_OK ){ |
2047 db->magic = SQLITE_MAGIC_SICK; | 2697 db->magic = SQLITE_MAGIC_SICK; |
2048 } | 2698 } |
2049 *ppDb = db; | 2699 *ppDb = db; |
| 2700 #ifdef SQLITE_ENABLE_SQLLOG |
| 2701 if( sqlite3GlobalConfig.xSqllog ){ |
| 2702 /* Opening a db handle. Fourth parameter is passed 0. */ |
| 2703 void *pArg = sqlite3GlobalConfig.pSqllogArg; |
| 2704 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); |
| 2705 } |
| 2706 #endif |
2050 return sqlite3ApiExit(0, rc); | 2707 return sqlite3ApiExit(0, rc); |
2051 } | 2708 } |
2052 | 2709 |
2053 /* | 2710 /* |
2054 ** Open a new database handle. | 2711 ** Open a new database handle. |
2055 */ | 2712 */ |
2056 int sqlite3_open( | 2713 int sqlite3_open( |
2057 const char *zFilename, | 2714 const char *zFilename, |
2058 sqlite3 **ppDb | 2715 sqlite3 **ppDb |
2059 ){ | 2716 ){ |
2060 return openDatabase(zFilename, ppDb, | 2717 return openDatabase(zFilename, ppDb, |
2061 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); | 2718 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
2062 } | 2719 } |
2063 int sqlite3_open_v2( | 2720 int sqlite3_open_v2( |
2064 const char *filename, /* Database filename (UTF-8) */ | 2721 const char *filename, /* Database filename (UTF-8) */ |
2065 sqlite3 **ppDb, /* OUT: SQLite db handle */ | 2722 sqlite3 **ppDb, /* OUT: SQLite db handle */ |
2066 int flags, /* Flags */ | 2723 int flags, /* Flags */ |
2067 const char *zVfs /* Name of VFS module to use */ | 2724 const char *zVfs /* Name of VFS module to use */ |
2068 ){ | 2725 ){ |
2069 return openDatabase(filename, ppDb, flags, zVfs); | 2726 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs); |
2070 } | 2727 } |
2071 | 2728 |
2072 #ifndef SQLITE_OMIT_UTF16 | 2729 #ifndef SQLITE_OMIT_UTF16 |
2073 /* | 2730 /* |
2074 ** Open a new database handle. | 2731 ** Open a new database handle. |
2075 */ | 2732 */ |
2076 int sqlite3_open16( | 2733 int sqlite3_open16( |
2077 const void *zFilename, | 2734 const void *zFilename, |
2078 sqlite3 **ppDb | 2735 sqlite3 **ppDb |
2079 ){ | 2736 ){ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2113 int sqlite3_create_collation( | 2770 int sqlite3_create_collation( |
2114 sqlite3* db, | 2771 sqlite3* db, |
2115 const char *zName, | 2772 const char *zName, |
2116 int enc, | 2773 int enc, |
2117 void* pCtx, | 2774 void* pCtx, |
2118 int(*xCompare)(void*,int,const void*,int,const void*) | 2775 int(*xCompare)(void*,int,const void*,int,const void*) |
2119 ){ | 2776 ){ |
2120 int rc; | 2777 int rc; |
2121 sqlite3_mutex_enter(db->mutex); | 2778 sqlite3_mutex_enter(db->mutex); |
2122 assert( !db->mallocFailed ); | 2779 assert( !db->mallocFailed ); |
2123 rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0); | 2780 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0); |
2124 rc = sqlite3ApiExit(db, rc); | 2781 rc = sqlite3ApiExit(db, rc); |
2125 sqlite3_mutex_leave(db->mutex); | 2782 sqlite3_mutex_leave(db->mutex); |
2126 return rc; | 2783 return rc; |
2127 } | 2784 } |
2128 | 2785 |
2129 /* | 2786 /* |
2130 ** Register a new collation sequence with the database handle db. | 2787 ** Register a new collation sequence with the database handle db. |
2131 */ | 2788 */ |
2132 int sqlite3_create_collation_v2( | 2789 int sqlite3_create_collation_v2( |
2133 sqlite3* db, | 2790 sqlite3* db, |
2134 const char *zName, | 2791 const char *zName, |
2135 int enc, | 2792 int enc, |
2136 void* pCtx, | 2793 void* pCtx, |
2137 int(*xCompare)(void*,int,const void*,int,const void*), | 2794 int(*xCompare)(void*,int,const void*,int,const void*), |
2138 void(*xDel)(void*) | 2795 void(*xDel)(void*) |
2139 ){ | 2796 ){ |
2140 int rc; | 2797 int rc; |
2141 sqlite3_mutex_enter(db->mutex); | 2798 sqlite3_mutex_enter(db->mutex); |
2142 assert( !db->mallocFailed ); | 2799 assert( !db->mallocFailed ); |
2143 rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDe
l); | 2800 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
2144 rc = sqlite3ApiExit(db, rc); | 2801 rc = sqlite3ApiExit(db, rc); |
2145 sqlite3_mutex_leave(db->mutex); | 2802 sqlite3_mutex_leave(db->mutex); |
2146 return rc; | 2803 return rc; |
2147 } | 2804 } |
2148 | 2805 |
2149 #ifndef SQLITE_OMIT_UTF16 | 2806 #ifndef SQLITE_OMIT_UTF16 |
2150 /* | 2807 /* |
2151 ** Register a new collation sequence with the database handle db. | 2808 ** Register a new collation sequence with the database handle db. |
2152 */ | 2809 */ |
2153 int sqlite3_create_collation16( | 2810 int sqlite3_create_collation16( |
2154 sqlite3* db, | 2811 sqlite3* db, |
2155 const void *zName, | 2812 const void *zName, |
2156 int enc, | 2813 int enc, |
2157 void* pCtx, | 2814 void* pCtx, |
2158 int(*xCompare)(void*,int,const void*,int,const void*) | 2815 int(*xCompare)(void*,int,const void*,int,const void*) |
2159 ){ | 2816 ){ |
2160 int rc = SQLITE_OK; | 2817 int rc = SQLITE_OK; |
2161 char *zName8; | 2818 char *zName8; |
2162 sqlite3_mutex_enter(db->mutex); | 2819 sqlite3_mutex_enter(db->mutex); |
2163 assert( !db->mallocFailed ); | 2820 assert( !db->mallocFailed ); |
2164 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); | 2821 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
2165 if( zName8 ){ | 2822 if( zName8 ){ |
2166 rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare,
0); | 2823 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
2167 sqlite3DbFree(db, zName8); | 2824 sqlite3DbFree(db, zName8); |
2168 } | 2825 } |
2169 rc = sqlite3ApiExit(db, rc); | 2826 rc = sqlite3ApiExit(db, rc); |
2170 sqlite3_mutex_leave(db->mutex); | 2827 sqlite3_mutex_leave(db->mutex); |
2171 return rc; | 2828 return rc; |
2172 } | 2829 } |
2173 #endif /* SQLITE_OMIT_UTF16 */ | 2830 #endif /* SQLITE_OMIT_UTF16 */ |
2174 | 2831 |
2175 /* | 2832 /* |
2176 ** Register a collation sequence factory callback with the database handle | 2833 ** Register a collation sequence factory callback with the database handle |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2216 int sqlite3_global_recover(void){ | 2873 int sqlite3_global_recover(void){ |
2217 return SQLITE_OK; | 2874 return SQLITE_OK; |
2218 } | 2875 } |
2219 #endif | 2876 #endif |
2220 | 2877 |
2221 /* | 2878 /* |
2222 ** Test to see whether or not the database connection is in autocommit | 2879 ** Test to see whether or not the database connection is in autocommit |
2223 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on | 2880 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
2224 ** by default. Autocommit is disabled by a BEGIN statement and reenabled | 2881 ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
2225 ** by the next COMMIT or ROLLBACK. | 2882 ** by the next COMMIT or ROLLBACK. |
2226 ** | |
2227 ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** | |
2228 */ | 2883 */ |
2229 int sqlite3_get_autocommit(sqlite3 *db){ | 2884 int sqlite3_get_autocommit(sqlite3 *db){ |
2230 return db->autoCommit; | 2885 return db->autoCommit; |
2231 } | 2886 } |
2232 | 2887 |
2233 /* | 2888 /* |
2234 ** The following routines are subtitutes for constants SQLITE_CORRUPT, | 2889 ** The following routines are substitutes for constants SQLITE_CORRUPT, |
2235 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error | 2890 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error |
2236 ** constants. They server two purposes: | 2891 ** constants. They serve two purposes: |
2237 ** | 2892 ** |
2238 ** 1. Serve as a convenient place to set a breakpoint in a debugger | 2893 ** 1. Serve as a convenient place to set a breakpoint in a debugger |
2239 ** to detect when version error conditions occurs. | 2894 ** to detect when version error conditions occurs. |
2240 ** | 2895 ** |
2241 ** 2. Invoke sqlite3_log() to provide the source code location where | 2896 ** 2. Invoke sqlite3_log() to provide the source code location where |
2242 ** a low-level error is first detected. | 2897 ** a low-level error is first detected. |
2243 */ | 2898 */ |
2244 int sqlite3CorruptError(int lineno){ | 2899 int sqlite3CorruptError(int lineno){ |
2245 testcase( sqlite3GlobalConfig.xLog!=0 ); | 2900 testcase( sqlite3GlobalConfig.xLog!=0 ); |
2246 sqlite3_log(SQLITE_CORRUPT, | 2901 sqlite3_log(SQLITE_CORRUPT, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2345 ** 1. The specified column name was rowid", "oid" or "_rowid_" | 3000 ** 1. The specified column name was rowid", "oid" or "_rowid_" |
2346 ** and there is no explicitly declared IPK column. | 3001 ** and there is no explicitly declared IPK column. |
2347 ** | 3002 ** |
2348 ** 2. The table is not a view and the column name identified an | 3003 ** 2. The table is not a view and the column name identified an |
2349 ** explicitly declared column. Copy meta information from *pCol. | 3004 ** explicitly declared column. Copy meta information from *pCol. |
2350 */ | 3005 */ |
2351 if( pCol ){ | 3006 if( pCol ){ |
2352 zDataType = pCol->zType; | 3007 zDataType = pCol->zType; |
2353 zCollSeq = pCol->zColl; | 3008 zCollSeq = pCol->zColl; |
2354 notnull = pCol->notNull!=0; | 3009 notnull = pCol->notNull!=0; |
2355 primarykey = pCol->isPrimKey!=0; | 3010 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0; |
2356 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; | 3011 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; |
2357 }else{ | 3012 }else{ |
2358 zDataType = "INTEGER"; | 3013 zDataType = "INTEGER"; |
2359 primarykey = 1; | 3014 primarykey = 1; |
2360 } | 3015 } |
2361 if( !zCollSeq ){ | 3016 if( !zCollSeq ){ |
2362 zCollSeq = "BINARY"; | 3017 zCollSeq = "BINARY"; |
2363 } | 3018 } |
2364 | 3019 |
2365 error_out: | 3020 error_out: |
2366 sqlite3BtreeLeaveAll(db); | 3021 sqlite3BtreeLeaveAll(db); |
2367 | 3022 |
2368 /* Whether the function call succeeded or failed, set the output parameters | 3023 /* Whether the function call succeeded or failed, set the output parameters |
2369 ** to whatever their local counterparts contain. If an error did occur, | 3024 ** to whatever their local counterparts contain. If an error did occur, |
2370 ** this has the effect of zeroing all output parameters. | 3025 ** this has the effect of zeroing all output parameters. |
2371 */ | 3026 */ |
2372 if( pzDataType ) *pzDataType = zDataType; | 3027 if( pzDataType ) *pzDataType = zDataType; |
2373 if( pzCollSeq ) *pzCollSeq = zCollSeq; | 3028 if( pzCollSeq ) *pzCollSeq = zCollSeq; |
2374 if( pNotNull ) *pNotNull = notnull; | 3029 if( pNotNull ) *pNotNull = notnull; |
2375 if( pPrimaryKey ) *pPrimaryKey = primarykey; | 3030 if( pPrimaryKey ) *pPrimaryKey = primarykey; |
2376 if( pAutoinc ) *pAutoinc = autoinc; | 3031 if( pAutoinc ) *pAutoinc = autoinc; |
2377 | 3032 |
2378 if( SQLITE_OK==rc && !pTab ){ | 3033 if( SQLITE_OK==rc && !pTab ){ |
2379 sqlite3DbFree(db, zErrMsg); | 3034 sqlite3DbFree(db, zErrMsg); |
2380 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, | 3035 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, |
2381 zColumnName); | 3036 zColumnName); |
2382 rc = SQLITE_ERROR; | 3037 rc = SQLITE_ERROR; |
2383 } | 3038 } |
2384 sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); | 3039 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg); |
2385 sqlite3DbFree(db, zErrMsg); | 3040 sqlite3DbFree(db, zErrMsg); |
2386 rc = sqlite3ApiExit(db, rc); | 3041 rc = sqlite3ApiExit(db, rc); |
2387 sqlite3_mutex_leave(db->mutex); | 3042 sqlite3_mutex_leave(db->mutex); |
2388 return rc; | 3043 return rc; |
2389 } | 3044 } |
2390 #endif | 3045 #endif |
2391 | 3046 |
2392 /* | 3047 /* |
2393 ** Sleep for a little while. Return the amount of time slept. | 3048 ** Sleep for a little while. Return the amount of time slept. |
2394 */ | 3049 */ |
(...skipping 18 matching lines...) Expand all Loading... |
2413 db->errMask = onoff ? 0xffffffff : 0xff; | 3068 db->errMask = onoff ? 0xffffffff : 0xff; |
2414 sqlite3_mutex_leave(db->mutex); | 3069 sqlite3_mutex_leave(db->mutex); |
2415 return SQLITE_OK; | 3070 return SQLITE_OK; |
2416 } | 3071 } |
2417 | 3072 |
2418 /* | 3073 /* |
2419 ** Invoke the xFileControl method on a particular database. | 3074 ** Invoke the xFileControl method on a particular database. |
2420 */ | 3075 */ |
2421 int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ | 3076 int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
2422 int rc = SQLITE_ERROR; | 3077 int rc = SQLITE_ERROR; |
2423 int iDb; | 3078 Btree *pBtree; |
| 3079 |
2424 sqlite3_mutex_enter(db->mutex); | 3080 sqlite3_mutex_enter(db->mutex); |
2425 if( zDbName==0 ){ | 3081 pBtree = sqlite3DbNameToBtree(db, zDbName); |
2426 iDb = 0; | 3082 if( pBtree ){ |
2427 }else{ | 3083 Pager *pPager; |
2428 for(iDb=0; iDb<db->nDb; iDb++){ | 3084 sqlite3_file *fd; |
2429 if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break; | 3085 sqlite3BtreeEnter(pBtree); |
| 3086 pPager = sqlite3BtreePager(pBtree); |
| 3087 assert( pPager!=0 ); |
| 3088 fd = sqlite3PagerFile(pPager); |
| 3089 assert( fd!=0 ); |
| 3090 if( op==SQLITE_FCNTL_FILE_POINTER ){ |
| 3091 *(sqlite3_file**)pArg = fd; |
| 3092 rc = SQLITE_OK; |
| 3093 }else if( fd->pMethods ){ |
| 3094 rc = sqlite3OsFileControl(fd, op, pArg); |
| 3095 }else{ |
| 3096 rc = SQLITE_NOTFOUND; |
2430 } | 3097 } |
2431 } | 3098 sqlite3BtreeLeave(pBtree); |
2432 if( iDb<db->nDb ){ | |
2433 Btree *pBtree = db->aDb[iDb].pBt; | |
2434 if( pBtree ){ | |
2435 Pager *pPager; | |
2436 sqlite3_file *fd; | |
2437 sqlite3BtreeEnter(pBtree); | |
2438 pPager = sqlite3BtreePager(pBtree); | |
2439 assert( pPager!=0 ); | |
2440 fd = sqlite3PagerFile(pPager); | |
2441 assert( fd!=0 ); | |
2442 if( op==SQLITE_FCNTL_FILE_POINTER ){ | |
2443 *(sqlite3_file**)pArg = fd; | |
2444 rc = SQLITE_OK; | |
2445 }else if( fd->pMethods ){ | |
2446 rc = sqlite3OsFileControl(fd, op, pArg); | |
2447 }else{ | |
2448 rc = SQLITE_NOTFOUND; | |
2449 } | |
2450 sqlite3BtreeLeave(pBtree); | |
2451 } | |
2452 } | 3099 } |
2453 sqlite3_mutex_leave(db->mutex); | 3100 sqlite3_mutex_leave(db->mutex); |
2454 return rc; | 3101 return rc; |
2455 } | 3102 } |
2456 | 3103 |
2457 /* | 3104 /* |
2458 ** Interface to the testing logic. | 3105 ** Interface to the testing logic. |
2459 */ | 3106 */ |
2460 int sqlite3_test_control(int op, ...){ | 3107 int sqlite3_test_control(int op, ...){ |
2461 int rc = 0; | 3108 int rc = 0; |
(...skipping 19 matching lines...) Expand all Loading... |
2481 sqlite3PrngRestoreState(); | 3128 sqlite3PrngRestoreState(); |
2482 break; | 3129 break; |
2483 } | 3130 } |
2484 | 3131 |
2485 /* | 3132 /* |
2486 ** Reset the PRNG back to its uninitialized state. The next call | 3133 ** Reset the PRNG back to its uninitialized state. The next call |
2487 ** to sqlite3_randomness() will reseed the PRNG using a single call | 3134 ** to sqlite3_randomness() will reseed the PRNG using a single call |
2488 ** to the xRandomness method of the default VFS. | 3135 ** to the xRandomness method of the default VFS. |
2489 */ | 3136 */ |
2490 case SQLITE_TESTCTRL_PRNG_RESET: { | 3137 case SQLITE_TESTCTRL_PRNG_RESET: { |
2491 sqlite3PrngResetState(); | 3138 sqlite3_randomness(0,0); |
2492 break; | 3139 break; |
2493 } | 3140 } |
2494 | 3141 |
2495 /* | 3142 /* |
2496 ** sqlite3_test_control(BITVEC_TEST, size, program) | 3143 ** sqlite3_test_control(BITVEC_TEST, size, program) |
2497 ** | 3144 ** |
2498 ** Run a test against a Bitvec object of size. The program argument | 3145 ** Run a test against a Bitvec object of size. The program argument |
2499 ** is an array of integers that defines the test. Return -1 on a | 3146 ** is an array of integers that defines the test. Return -1 on a |
2500 ** memory allocation error, 0 on success, or non-zero for an error. | 3147 ** memory allocation error, 0 on success, or non-zero for an error. |
2501 ** See the sqlite3BitvecBuiltinTest() for additional information. | 3148 ** See the sqlite3BitvecBuiltinTest() for additional information. |
2502 */ | 3149 */ |
2503 case SQLITE_TESTCTRL_BITVEC_TEST: { | 3150 case SQLITE_TESTCTRL_BITVEC_TEST: { |
2504 int sz = va_arg(ap, int); | 3151 int sz = va_arg(ap, int); |
2505 int *aProg = va_arg(ap, int*); | 3152 int *aProg = va_arg(ap, int*); |
2506 rc = sqlite3BitvecBuiltinTest(sz, aProg); | 3153 rc = sqlite3BitvecBuiltinTest(sz, aProg); |
2507 break; | 3154 break; |
2508 } | 3155 } |
2509 | 3156 |
2510 /* | 3157 /* |
| 3158 ** sqlite3_test_control(FAULT_INSTALL, xCallback) |
| 3159 ** |
| 3160 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called, |
| 3161 ** if xCallback is not NULL. |
| 3162 ** |
| 3163 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0) |
| 3164 ** is called immediately after installing the new callback and the return |
| 3165 ** value from sqlite3FaultSim(0) becomes the return from |
| 3166 ** sqlite3_test_control(). |
| 3167 */ |
| 3168 case SQLITE_TESTCTRL_FAULT_INSTALL: { |
| 3169 /* MSVC is picky about pulling func ptrs from va lists. |
| 3170 ** http://support.microsoft.com/kb/47961 |
| 3171 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int)); |
| 3172 */ |
| 3173 typedef int(*TESTCALLBACKFUNC_t)(int); |
| 3174 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t); |
| 3175 rc = sqlite3FaultSim(0); |
| 3176 break; |
| 3177 } |
| 3178 |
| 3179 /* |
2511 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) | 3180 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) |
2512 ** | 3181 ** |
2513 ** Register hooks to call to indicate which malloc() failures | 3182 ** Register hooks to call to indicate which malloc() failures |
2514 ** are benign. | 3183 ** are benign. |
2515 */ | 3184 */ |
2516 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { | 3185 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { |
2517 typedef void (*void_function)(void); | 3186 typedef void (*void_function)(void); |
2518 void_function xBenignBegin; | 3187 void_function xBenignBegin; |
2519 void_function xBenignEnd; | 3188 void_function xBenignEnd; |
2520 xBenignBegin = va_arg(ap, void_function); | 3189 xBenignBegin = va_arg(ap, void_function); |
2521 xBenignEnd = va_arg(ap, void_function); | 3190 xBenignEnd = va_arg(ap, void_function); |
2522 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); | 3191 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); |
2523 break; | 3192 break; |
2524 } | 3193 } |
2525 | 3194 |
2526 /* | 3195 /* |
2527 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) | 3196 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) |
2528 ** | 3197 ** |
2529 ** Set the PENDING byte to the value in the argument, if X>0. | 3198 ** Set the PENDING byte to the value in the argument, if X>0. |
2530 ** Make no changes if X==0. Return the value of the pending byte | 3199 ** Make no changes if X==0. Return the value of the pending byte |
2531 ** as it existing before this routine was called. | 3200 ** as it existing before this routine was called. |
2532 ** | 3201 ** |
2533 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in | 3202 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in |
2534 ** an incompatible database file format. Changing the PENDING byte | 3203 ** an incompatible database file format. Changing the PENDING byte |
2535 ** while any database connection is open results in undefined and | 3204 ** while any database connection is open results in undefined and |
2536 ** dileterious behavior. | 3205 ** deleterious behavior. |
2537 */ | 3206 */ |
2538 case SQLITE_TESTCTRL_PENDING_BYTE: { | 3207 case SQLITE_TESTCTRL_PENDING_BYTE: { |
2539 rc = PENDING_BYTE; | 3208 rc = PENDING_BYTE; |
2540 #ifndef SQLITE_OMIT_WSD | 3209 #ifndef SQLITE_OMIT_WSD |
2541 { | 3210 { |
2542 unsigned int newVal = va_arg(ap, unsigned int); | 3211 unsigned int newVal = va_arg(ap, unsigned int); |
2543 if( newVal ) sqlite3PendingByte = newVal; | 3212 if( newVal ) sqlite3PendingByte = newVal; |
2544 } | 3213 } |
2545 #endif | 3214 #endif |
2546 break; | 3215 break; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2591 ** }else{ | 3260 ** }else{ |
2592 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. | 3261 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. |
2593 ** } | 3262 ** } |
2594 */ | 3263 */ |
2595 case SQLITE_TESTCTRL_ALWAYS: { | 3264 case SQLITE_TESTCTRL_ALWAYS: { |
2596 int x = va_arg(ap,int); | 3265 int x = va_arg(ap,int); |
2597 rc = ALWAYS(x); | 3266 rc = ALWAYS(x); |
2598 break; | 3267 break; |
2599 } | 3268 } |
2600 | 3269 |
| 3270 /* |
| 3271 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER); |
| 3272 ** |
| 3273 ** The integer returned reveals the byte-order of the computer on which |
| 3274 ** SQLite is running: |
| 3275 ** |
| 3276 ** 1 big-endian, determined at run-time |
| 3277 ** 10 little-endian, determined at run-time |
| 3278 ** 432101 big-endian, determined at compile-time |
| 3279 ** 123410 little-endian, determined at compile-time |
| 3280 */ |
| 3281 case SQLITE_TESTCTRL_BYTEORDER: { |
| 3282 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN; |
| 3283 break; |
| 3284 } |
| 3285 |
2601 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) | 3286 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) |
2602 ** | 3287 ** |
2603 ** Set the nReserve size to N for the main database on the database | 3288 ** Set the nReserve size to N for the main database on the database |
2604 ** connection db. | 3289 ** connection db. |
2605 */ | 3290 */ |
2606 case SQLITE_TESTCTRL_RESERVE: { | 3291 case SQLITE_TESTCTRL_RESERVE: { |
2607 sqlite3 *db = va_arg(ap, sqlite3*); | 3292 sqlite3 *db = va_arg(ap, sqlite3*); |
2608 int x = va_arg(ap,int); | 3293 int x = va_arg(ap,int); |
2609 sqlite3_mutex_enter(db->mutex); | 3294 sqlite3_mutex_enter(db->mutex); |
2610 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); | 3295 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); |
2611 sqlite3_mutex_leave(db->mutex); | 3296 sqlite3_mutex_leave(db->mutex); |
2612 break; | 3297 break; |
2613 } | 3298 } |
2614 | 3299 |
2615 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) | 3300 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) |
2616 ** | 3301 ** |
2617 ** Enable or disable various optimizations for testing purposes. The | 3302 ** Enable or disable various optimizations for testing purposes. The |
2618 ** argument N is a bitmask of optimizations to be disabled. For normal | 3303 ** argument N is a bitmask of optimizations to be disabled. For normal |
2619 ** operation N should be 0. The idea is that a test program (like the | 3304 ** operation N should be 0. The idea is that a test program (like the |
2620 ** SQL Logic Test or SLT test module) can run the same SQL multiple times | 3305 ** SQL Logic Test or SLT test module) can run the same SQL multiple times |
2621 ** with various optimizations disabled to verify that the same answer | 3306 ** with various optimizations disabled to verify that the same answer |
2622 ** is obtained in every case. | 3307 ** is obtained in every case. |
2623 */ | 3308 */ |
2624 case SQLITE_TESTCTRL_OPTIMIZATIONS: { | 3309 case SQLITE_TESTCTRL_OPTIMIZATIONS: { |
2625 sqlite3 *db = va_arg(ap, sqlite3*); | 3310 sqlite3 *db = va_arg(ap, sqlite3*); |
2626 int x = va_arg(ap,int); | 3311 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff); |
2627 db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask); | |
2628 break; | 3312 break; |
2629 } | 3313 } |
2630 | 3314 |
2631 #ifdef SQLITE_N_KEYWORD | 3315 #ifdef SQLITE_N_KEYWORD |
2632 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) | 3316 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) |
2633 ** | 3317 ** |
2634 ** If zWord is a keyword recognized by the parser, then return the | 3318 ** If zWord is a keyword recognized by the parser, then return the |
2635 ** number of keywords. Or if zWord is not a keyword, return 0. | 3319 ** number of keywords. Or if zWord is not a keyword, return 0. |
2636 ** | 3320 ** |
2637 ** This test feature is only available in the amalgamation since | 3321 ** This test feature is only available in the amalgamation since |
2638 ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite | 3322 ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite |
2639 ** is built using separate source files. | 3323 ** is built using separate source files. |
2640 */ | 3324 */ |
2641 case SQLITE_TESTCTRL_ISKEYWORD: { | 3325 case SQLITE_TESTCTRL_ISKEYWORD: { |
2642 const char *zWord = va_arg(ap, const char*); | 3326 const char *zWord = va_arg(ap, const char*); |
2643 int n = sqlite3Strlen30(zWord); | 3327 int n = sqlite3Strlen30(zWord); |
2644 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; | 3328 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; |
2645 break; | 3329 break; |
2646 } | 3330 } |
2647 #endif | 3331 #endif |
2648 | 3332 |
2649 /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ) | |
2650 ** | |
2651 ** Return the size of a pcache header in bytes. | |
2652 */ | |
2653 case SQLITE_TESTCTRL_PGHDRSZ: { | |
2654 rc = sizeof(PgHdr); | |
2655 break; | |
2656 } | |
2657 | |
2658 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); | 3333 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); |
2659 ** | 3334 ** |
2660 ** Pass pFree into sqlite3ScratchFree(). | 3335 ** Pass pFree into sqlite3ScratchFree(). |
2661 ** If sz>0 then allocate a scratch buffer into pNew. | 3336 ** If sz>0 then allocate a scratch buffer into pNew. |
2662 */ | 3337 */ |
2663 case SQLITE_TESTCTRL_SCRATCHMALLOC: { | 3338 case SQLITE_TESTCTRL_SCRATCHMALLOC: { |
2664 void *pFree, **ppNew; | 3339 void *pFree, **ppNew; |
2665 int sz; | 3340 int sz; |
2666 sz = va_arg(ap, int); | 3341 sz = va_arg(ap, int); |
2667 ppNew = va_arg(ap, void**); | 3342 ppNew = va_arg(ap, void**); |
2668 pFree = va_arg(ap, void*); | 3343 pFree = va_arg(ap, void*); |
2669 if( sz ) *ppNew = sqlite3ScratchMalloc(sz); | 3344 if( sz ) *ppNew = sqlite3ScratchMalloc(sz); |
2670 sqlite3ScratchFree(pFree); | 3345 sqlite3ScratchFree(pFree); |
2671 break; | 3346 break; |
2672 } | 3347 } |
2673 | 3348 |
| 3349 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); |
| 3350 ** |
| 3351 ** If parameter onoff is non-zero, configure the wrappers so that all |
| 3352 ** subsequent calls to localtime() and variants fail. If onoff is zero, |
| 3353 ** undo this setting. |
| 3354 */ |
| 3355 case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 3356 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); |
| 3357 break; |
| 3358 } |
| 3359 |
| 3360 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); |
| 3361 ** |
| 3362 ** Set or clear a flag that indicates that the database file is always well- |
| 3363 ** formed and never corrupt. This flag is clear by default, indicating that |
| 3364 ** database files might have arbitrary corruption. Setting the flag during |
| 3365 ** testing causes certain assert() statements in the code to be activated |
| 3366 ** that demonstrat invariants on well-formed database files. |
| 3367 */ |
| 3368 case SQLITE_TESTCTRL_NEVER_CORRUPT: { |
| 3369 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int); |
| 3370 break; |
| 3371 } |
| 3372 |
| 3373 |
| 3374 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr); |
| 3375 ** |
| 3376 ** Set the VDBE coverage callback function to xCallback with context |
| 3377 ** pointer ptr. |
| 3378 */ |
| 3379 case SQLITE_TESTCTRL_VDBE_COVERAGE: { |
| 3380 #ifdef SQLITE_VDBE_COVERAGE |
| 3381 typedef void (*branch_callback)(void*,int,u8,u8); |
| 3382 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback); |
| 3383 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*); |
| 3384 #endif |
| 3385 break; |
| 3386 } |
| 3387 |
| 3388 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */ |
| 3389 case SQLITE_TESTCTRL_SORTER_MMAP: { |
| 3390 sqlite3 *db = va_arg(ap, sqlite3*); |
| 3391 db->nMaxSorterMmap = va_arg(ap, int); |
| 3392 break; |
| 3393 } |
| 3394 |
| 3395 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT); |
| 3396 ** |
| 3397 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if |
| 3398 ** not. |
| 3399 */ |
| 3400 case SQLITE_TESTCTRL_ISINIT: { |
| 3401 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR; |
| 3402 break; |
| 3403 } |
2674 } | 3404 } |
2675 va_end(ap); | 3405 va_end(ap); |
2676 #endif /* SQLITE_OMIT_BUILTIN_TEST */ | 3406 #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
2677 return rc; | 3407 return rc; |
2678 } | 3408 } |
| 3409 |
| 3410 /* |
| 3411 ** This is a utility routine, useful to VFS implementations, that checks |
| 3412 ** to see if a database file was a URI that contained a specific query |
| 3413 ** parameter, and if so obtains the value of the query parameter. |
| 3414 ** |
| 3415 ** The zFilename argument is the filename pointer passed into the xOpen() |
| 3416 ** method of a VFS implementation. The zParam argument is the name of the |
| 3417 ** query parameter we seek. This routine returns the value of the zParam |
| 3418 ** parameter if it exists. If the parameter does not exist, this routine |
| 3419 ** returns a NULL pointer. |
| 3420 */ |
| 3421 const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ |
| 3422 if( zFilename==0 ) return 0; |
| 3423 zFilename += sqlite3Strlen30(zFilename) + 1; |
| 3424 while( zFilename[0] ){ |
| 3425 int x = strcmp(zFilename, zParam); |
| 3426 zFilename += sqlite3Strlen30(zFilename) + 1; |
| 3427 if( x==0 ) return zFilename; |
| 3428 zFilename += sqlite3Strlen30(zFilename) + 1; |
| 3429 } |
| 3430 return 0; |
| 3431 } |
| 3432 |
| 3433 /* |
| 3434 ** Return a boolean value for a query parameter. |
| 3435 */ |
| 3436 int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){ |
| 3437 const char *z = sqlite3_uri_parameter(zFilename, zParam); |
| 3438 bDflt = bDflt!=0; |
| 3439 return z ? sqlite3GetBoolean(z, bDflt) : bDflt; |
| 3440 } |
| 3441 |
| 3442 /* |
| 3443 ** Return a 64-bit integer value for a query parameter. |
| 3444 */ |
| 3445 sqlite3_int64 sqlite3_uri_int64( |
| 3446 const char *zFilename, /* Filename as passed to xOpen */ |
| 3447 const char *zParam, /* URI parameter sought */ |
| 3448 sqlite3_int64 bDflt /* return if parameter is missing */ |
| 3449 ){ |
| 3450 const char *z = sqlite3_uri_parameter(zFilename, zParam); |
| 3451 sqlite3_int64 v; |
| 3452 if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){ |
| 3453 bDflt = v; |
| 3454 } |
| 3455 return bDflt; |
| 3456 } |
| 3457 |
| 3458 /* |
| 3459 ** Return the Btree pointer identified by zDbName. Return NULL if not found. |
| 3460 */ |
| 3461 Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){ |
| 3462 int i; |
| 3463 for(i=0; i<db->nDb; i++){ |
| 3464 if( db->aDb[i].pBt |
| 3465 && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0) |
| 3466 ){ |
| 3467 return db->aDb[i].pBt; |
| 3468 } |
| 3469 } |
| 3470 return 0; |
| 3471 } |
| 3472 |
| 3473 /* |
| 3474 ** Return the filename of the database associated with a database |
| 3475 ** connection. |
| 3476 */ |
| 3477 const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
| 3478 Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 3479 return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 3480 } |
| 3481 |
| 3482 /* |
| 3483 ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 3484 ** no such database exists. |
| 3485 */ |
| 3486 int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 3487 Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 3488 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 3489 } |
OLD | NEW |