Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/src/main.c

Issue 1610543003: [sql] Import reference version of SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 **
11 ************************************************************************* 11 *************************************************************************
12 ** Main file for the SQLite library. The routines in this file 12 ** Main file for the SQLite library. The routines in this file
13 ** implement the programmer interface to the library. Routines in 13 ** implement the programmer interface to the library. Routines in
14 ** other files are for internal use by SQLite and should not be 14 ** other files are for internal use by SQLite and should not be
15 ** accessed by users of the library. 15 ** accessed by users of the library.
16 */ 16 */
17 #include "sqliteInt.h" 17 #include "sqliteInt.h"
18 18
19 #ifdef SQLITE_ENABLE_FTS3 19 #ifdef SQLITE_ENABLE_FTS3
20 # include "fts3.h" 20 # include "fts3.h"
21 #endif 21 #endif
22 #ifdef SQLITE_ENABLE_RTREE 22 #ifdef SQLITE_ENABLE_RTREE
23 # include "rtree.h" 23 # include "rtree.h"
24 #endif 24 #endif
25 #ifdef SQLITE_ENABLE_ICU 25 #ifdef SQLITE_ENABLE_ICU
26 # include "sqliteicu.h" 26 # include "sqliteicu.h"
27 #endif 27 #endif
28 #ifdef SQLITE_ENABLE_JSON1
29 int sqlite3Json1Init(sqlite3*);
30 #endif
31 #ifdef SQLITE_ENABLE_FTS5
32 int sqlite3Fts5Init(sqlite3*);
33 #endif
28 34
29 #ifndef SQLITE_AMALGAMATION 35 #ifndef SQLITE_AMALGAMATION
30 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant 36 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
31 ** contains the text of SQLITE_VERSION macro. 37 ** contains the text of SQLITE_VERSION macro.
32 */ 38 */
33 const char sqlite3_version[] = SQLITE_VERSION; 39 const char sqlite3_version[] = SQLITE_VERSION;
34 #endif 40 #endif
35 41
36 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns 42 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
37 ** a pointer to the to the sqlite3_version[] string constant. 43 ** a pointer to the to the sqlite3_version[] string constant.
(...skipping 10 matching lines...) Expand all
48 ** returns an integer equal to SQLITE_VERSION_NUMBER. 54 ** returns an integer equal to SQLITE_VERSION_NUMBER.
49 */ 55 */
50 int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } 56 int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
51 57
52 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns 58 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
53 ** zero if and only if SQLite was compiled with mutexing code omitted due to 59 ** 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. 60 ** the SQLITE_THREADSAFE compile-time option being set to 0.
55 */ 61 */
56 int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } 62 int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
57 63
64 /*
65 ** When compiling the test fixture or with debugging enabled (on Win32),
66 ** this variable being set to non-zero will cause OSTRACE macros to emit
67 ** extra diagnostic information.
68 */
69 #ifdef SQLITE_HAVE_OS_TRACE
70 # ifndef SQLITE_DEBUG_OS_TRACE
71 # define SQLITE_DEBUG_OS_TRACE 0
72 # endif
73 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
74 #endif
75
58 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) 76 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
59 /* 77 /*
60 ** If the following function pointer is not NULL and if 78 ** If the following function pointer is not NULL and if
61 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing 79 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
62 ** I/O active are written using this function. These messages 80 ** I/O active are written using this function. These messages
63 ** are intended for debugging activity only. 81 ** are intended for debugging activity only.
64 */ 82 */
65 void (*sqlite3IoTrace)(const char*, ...) = 0; 83 SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
66 #endif 84 #endif
67 85
68 /* 86 /*
69 ** If the following global variable points to a string which is the 87 ** 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 88 ** name of a directory, then that directory will be used to store
71 ** temporary files. 89 ** temporary files.
72 ** 90 **
73 ** See also the "PRAGMA temp_store_directory" SQL command. 91 ** See also the "PRAGMA temp_store_directory" SQL command.
74 */ 92 */
75 char *sqlite3_temp_directory = 0; 93 char *sqlite3_temp_directory = 0;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 int bRunExtraInit = 0; /* Extra initialization needed */ 139 int bRunExtraInit = 0; /* Extra initialization needed */
122 #endif 140 #endif
123 141
124 #ifdef SQLITE_OMIT_WSD 142 #ifdef SQLITE_OMIT_WSD
125 rc = sqlite3_wsd_init(4096, 24); 143 rc = sqlite3_wsd_init(4096, 24);
126 if( rc!=SQLITE_OK ){ 144 if( rc!=SQLITE_OK ){
127 return rc; 145 return rc;
128 } 146 }
129 #endif 147 #endif
130 148
149 /* If the following assert() fails on some obscure processor/compiler
150 ** combination, the work-around is to set the correct pointer
151 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
152 assert( SQLITE_PTRSIZE==sizeof(char*) );
153
131 /* If SQLite is already completely initialized, then this call 154 /* If SQLite is already completely initialized, then this call
132 ** to sqlite3_initialize() should be a no-op. But the initialization 155 ** to sqlite3_initialize() should be a no-op. But the initialization
133 ** must be complete. So isInit must not be set until the very end 156 ** must be complete. So isInit must not be set until the very end
134 ** of this routine. 157 ** of this routine.
135 */ 158 */
136 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; 159 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
137 160
138 /* Make sure the mutex subsystem is initialized. If unable to 161 /* Make sure the mutex subsystem is initialized. If unable to
139 ** initialize the mutex subsystem, return early with the error. 162 ** initialize the mutex subsystem, return early with the error.
140 ** If the system is so sick that we are unable to allocate a mutex, 163 ** If the system is so sick that we are unable to allocate a mutex,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 ** to the xInit method, so the xInit method need not be threadsafe. 213 ** to the xInit method, so the xInit method need not be threadsafe.
191 ** 214 **
192 ** The following mutex is what serializes access to the appdef pcache xInit 215 ** The following mutex is what serializes access to the appdef pcache xInit
193 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the 216 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
194 ** call to sqlite3PcacheInitialize(). 217 ** call to sqlite3PcacheInitialize().
195 */ 218 */
196 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); 219 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
197 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ 220 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
198 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); 221 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
199 sqlite3GlobalConfig.inProgress = 1; 222 sqlite3GlobalConfig.inProgress = 1;
223 #ifdef SQLITE_ENABLE_SQLLOG
224 {
225 extern void sqlite3_init_sqllog(void);
226 sqlite3_init_sqllog();
227 }
228 #endif
200 memset(pHash, 0, sizeof(sqlite3GlobalFunctions)); 229 memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
201 sqlite3RegisterGlobalFunctions(); 230 sqlite3RegisterGlobalFunctions();
202 if( sqlite3GlobalConfig.isPCacheInit==0 ){ 231 if( sqlite3GlobalConfig.isPCacheInit==0 ){
203 rc = sqlite3PcacheInitialize(); 232 rc = sqlite3PcacheInitialize();
204 } 233 }
205 if( rc==SQLITE_OK ){ 234 if( rc==SQLITE_OK ){
206 sqlite3GlobalConfig.isPCacheInit = 1; 235 sqlite3GlobalConfig.isPCacheInit = 1;
207 rc = sqlite3OsInit(); 236 rc = sqlite3OsInit();
208 } 237 }
209 if( rc==SQLITE_OK ){ 238 if( rc==SQLITE_OK ){
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 293
265 /* 294 /*
266 ** Undo the effects of sqlite3_initialize(). Must not be called while 295 ** Undo the effects of sqlite3_initialize(). Must not be called while
267 ** there are outstanding database connections or memory allocations or 296 ** there are outstanding database connections or memory allocations or
268 ** while any part of SQLite is otherwise in use in any thread. This 297 ** while any part of SQLite is otherwise in use in any thread. This
269 ** routine is not threadsafe. But it is safe to invoke this routine 298 ** routine is not threadsafe. But it is safe to invoke this routine
270 ** on when SQLite is already shut down. If SQLite is already shut down 299 ** on when SQLite is already shut down. If SQLite is already shut down
271 ** when this routine is invoked, then this routine is a harmless no-op. 300 ** when this routine is invoked, then this routine is a harmless no-op.
272 */ 301 */
273 int sqlite3_shutdown(void){ 302 int sqlite3_shutdown(void){
303 #ifdef SQLITE_OMIT_WSD
304 int rc = sqlite3_wsd_init(4096, 24);
305 if( rc!=SQLITE_OK ){
306 return rc;
307 }
308 #endif
309
274 if( sqlite3GlobalConfig.isInit ){ 310 if( sqlite3GlobalConfig.isInit ){
275 #ifdef SQLITE_EXTRA_SHUTDOWN 311 #ifdef SQLITE_EXTRA_SHUTDOWN
276 void SQLITE_EXTRA_SHUTDOWN(void); 312 void SQLITE_EXTRA_SHUTDOWN(void);
277 SQLITE_EXTRA_SHUTDOWN(); 313 SQLITE_EXTRA_SHUTDOWN();
278 #endif 314 #endif
279 sqlite3_os_end(); 315 sqlite3_os_end();
280 sqlite3_reset_auto_extension(); 316 sqlite3_reset_auto_extension();
281 sqlite3GlobalConfig.isInit = 0; 317 sqlite3GlobalConfig.isInit = 0;
282 } 318 }
283 if( sqlite3GlobalConfig.isPCacheInit ){ 319 if( sqlite3GlobalConfig.isPCacheInit ){
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 int rc = SQLITE_OK; 358 int rc = SQLITE_OK;
323 359
324 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while 360 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
325 ** the SQLite library is in use. */ 361 ** the SQLite library is in use. */
326 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT; 362 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
327 363
328 va_start(ap, op); 364 va_start(ap, op);
329 switch( op ){ 365 switch( op ){
330 366
331 /* Mutex configuration options are only available in a threadsafe 367 /* Mutex configuration options are only available in a threadsafe
332 ** compile. 368 ** compile.
333 */ 369 */
334 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 370 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
335 case SQLITE_CONFIG_SINGLETHREAD: { 371 case SQLITE_CONFIG_SINGLETHREAD: {
336 /* Disable all mutexing */ 372 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
337 sqlite3GlobalConfig.bCoreMutex = 0; 373 ** Single-thread. */
338 sqlite3GlobalConfig.bFullMutex = 0; 374 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
375 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
339 break; 376 break;
340 } 377 }
378 #endif
379 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
341 case SQLITE_CONFIG_MULTITHREAD: { 380 case SQLITE_CONFIG_MULTITHREAD: {
342 /* Disable mutexing of database connections */ 381 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
343 /* Enable mutexing of core data structures */ 382 ** Multi-thread. */
344 sqlite3GlobalConfig.bCoreMutex = 1; 383 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
345 sqlite3GlobalConfig.bFullMutex = 0; 384 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
346 break; 385 break;
347 } 386 }
387 #endif
388 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
348 case SQLITE_CONFIG_SERIALIZED: { 389 case SQLITE_CONFIG_SERIALIZED: {
349 /* Enable all mutexing */ 390 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
350 sqlite3GlobalConfig.bCoreMutex = 1; 391 ** Serialized. */
351 sqlite3GlobalConfig.bFullMutex = 1; 392 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
393 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
352 break; 394 break;
353 } 395 }
396 #endif
397 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
354 case SQLITE_CONFIG_MUTEX: { 398 case SQLITE_CONFIG_MUTEX: {
355 /* Specify an alternative mutex implementation */ 399 /* Specify an alternative mutex implementation */
356 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); 400 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
357 break; 401 break;
358 } 402 }
403 #endif
404 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
359 case SQLITE_CONFIG_GETMUTEX: { 405 case SQLITE_CONFIG_GETMUTEX: {
360 /* Retrieve the current mutex implementation */ 406 /* Retrieve the current mutex implementation */
361 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; 407 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
362 break; 408 break;
363 } 409 }
364 #endif 410 #endif
365 411
366
367 case SQLITE_CONFIG_MALLOC: { 412 case SQLITE_CONFIG_MALLOC: {
368 /* Specify an alternative malloc implementation */ 413 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
414 ** single argument which is a pointer to an instance of the
415 ** sqlite3_mem_methods structure. The argument specifies alternative
416 ** low-level memory allocation routines to be used in place of the memory
417 ** allocation routines built into SQLite. */
369 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); 418 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
370 break; 419 break;
371 } 420 }
372 case SQLITE_CONFIG_GETMALLOC: { 421 case SQLITE_CONFIG_GETMALLOC: {
373 /* Retrieve the current malloc() implementation */ 422 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
423 ** single argument which is a pointer to an instance of the
424 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
425 ** filled with the currently defined memory allocation routines. */
374 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); 426 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
375 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; 427 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
376 break; 428 break;
377 } 429 }
378 case SQLITE_CONFIG_MEMSTATUS: { 430 case SQLITE_CONFIG_MEMSTATUS: {
379 /* Enable or disable the malloc status collection */ 431 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
432 ** single argument of type int, interpreted as a boolean, which enables
433 ** or disables the collection of memory allocation statistics. */
380 sqlite3GlobalConfig.bMemstat = va_arg(ap, int); 434 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
381 break; 435 break;
382 } 436 }
383 case SQLITE_CONFIG_SCRATCH: { 437 case SQLITE_CONFIG_SCRATCH: {
384 /* Designate a buffer for scratch memory space */ 438 /* EVIDENCE-OF: R-08404-60887 There are three arguments to
439 ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
440 ** which the scratch allocations will be drawn, the size of each scratch
441 ** allocation (sz), and the maximum number of scratch allocations (N). */
385 sqlite3GlobalConfig.pScratch = va_arg(ap, void*); 442 sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
386 sqlite3GlobalConfig.szScratch = va_arg(ap, int); 443 sqlite3GlobalConfig.szScratch = va_arg(ap, int);
387 sqlite3GlobalConfig.nScratch = va_arg(ap, int); 444 sqlite3GlobalConfig.nScratch = va_arg(ap, int);
388 break; 445 break;
389 } 446 }
390 case SQLITE_CONFIG_PAGECACHE: { 447 case SQLITE_CONFIG_PAGECACHE: {
391 /* Designate a buffer for page cache memory space */ 448 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
449 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
450 ** the size of each page cache line (sz), and the number of cache lines
451 ** (N). */
392 sqlite3GlobalConfig.pPage = va_arg(ap, void*); 452 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
393 sqlite3GlobalConfig.szPage = va_arg(ap, int); 453 sqlite3GlobalConfig.szPage = va_arg(ap, int);
394 sqlite3GlobalConfig.nPage = va_arg(ap, int); 454 sqlite3GlobalConfig.nPage = va_arg(ap, int);
395 break; 455 break;
396 } 456 }
457 case SQLITE_CONFIG_PCACHE_HDRSZ: {
458 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
459 ** a single parameter which is a pointer to an integer and writes into
460 ** that integer the number of extra bytes per page required for each page
461 ** in SQLITE_CONFIG_PAGECACHE. */
462 *va_arg(ap, int*) =
463 sqlite3HeaderSizeBtree() +
464 sqlite3HeaderSizePcache() +
465 sqlite3HeaderSizePcache1();
466 break;
467 }
397 468
398 case SQLITE_CONFIG_PCACHE: { 469 case SQLITE_CONFIG_PCACHE: {
399 /* no-op */ 470 /* no-op */
400 break; 471 break;
401 } 472 }
402 case SQLITE_CONFIG_GETPCACHE: { 473 case SQLITE_CONFIG_GETPCACHE: {
403 /* now an error */ 474 /* now an error */
404 rc = SQLITE_ERROR; 475 rc = SQLITE_ERROR;
405 break; 476 break;
406 } 477 }
407 478
408 case SQLITE_CONFIG_PCACHE2: { 479 case SQLITE_CONFIG_PCACHE2: {
409 /* Specify an alternative page cache implementation */ 480 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
481 ** single argument which is a pointer to an sqlite3_pcache_methods2
482 ** object. This object specifies the interface to a custom page cache
483 ** implementation. */
410 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); 484 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
411 break; 485 break;
412 } 486 }
413 case SQLITE_CONFIG_GETPCACHE2: { 487 case SQLITE_CONFIG_GETPCACHE2: {
488 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
489 ** single argument which is a pointer to an sqlite3_pcache_methods2
490 ** object. SQLite copies of the current page cache implementation into
491 ** that object. */
414 if( sqlite3GlobalConfig.pcache2.xInit==0 ){ 492 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
415 sqlite3PCacheSetDefault(); 493 sqlite3PCacheSetDefault();
416 } 494 }
417 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; 495 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
418 break; 496 break;
419 } 497 }
420 498
499 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
500 ** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
501 ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
421 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 502 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
422 case SQLITE_CONFIG_HEAP: { 503 case SQLITE_CONFIG_HEAP: {
423 /* Designate a buffer for heap memory space */ 504 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
505 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
506 ** number of bytes in the memory buffer, and the minimum allocation size.
507 */
424 sqlite3GlobalConfig.pHeap = va_arg(ap, void*); 508 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
425 sqlite3GlobalConfig.nHeap = va_arg(ap, int); 509 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
426 sqlite3GlobalConfig.mnReq = va_arg(ap, int); 510 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
427 511
428 if( sqlite3GlobalConfig.mnReq<1 ){ 512 if( sqlite3GlobalConfig.mnReq<1 ){
429 sqlite3GlobalConfig.mnReq = 1; 513 sqlite3GlobalConfig.mnReq = 1;
430 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ 514 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
431 /* cap min request size at 2^12 */ 515 /* cap min request size at 2^12 */
432 sqlite3GlobalConfig.mnReq = (1<<12); 516 sqlite3GlobalConfig.mnReq = (1<<12);
433 } 517 }
434 518
435 if( sqlite3GlobalConfig.pHeap==0 ){ 519 if( sqlite3GlobalConfig.pHeap==0 ){
436 /* If the heap pointer is NULL, then restore the malloc implementation 520 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
437 ** back to NULL pointers too. This will cause the malloc to go 521 ** is NULL, then SQLite reverts to using its default memory allocator
438 ** back to its default implementation when sqlite3_initialize() is 522 ** (the system malloc() implementation), undoing any prior invocation of
439 ** run. 523 ** SQLITE_CONFIG_MALLOC.
524 **
525 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
526 ** revert to its default implementation when sqlite3_initialize() is run
440 */ 527 */
441 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); 528 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
442 }else{ 529 }else{
443 /* The heap pointer is not NULL, then install one of the 530 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
444 ** mem5.c/mem3.c methods. The enclosing #if guarantees at 531 ** alternative memory allocator is engaged to handle all of SQLites
445 ** least one of these methods is currently enabled. 532 ** memory allocation needs. */
446 */
447 #ifdef SQLITE_ENABLE_MEMSYS3 533 #ifdef SQLITE_ENABLE_MEMSYS3
448 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); 534 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
449 #endif 535 #endif
450 #ifdef SQLITE_ENABLE_MEMSYS5 536 #ifdef SQLITE_ENABLE_MEMSYS5
451 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); 537 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
452 #endif 538 #endif
453 } 539 }
454 break; 540 break;
455 } 541 }
456 #endif 542 #endif
(...skipping 18 matching lines...) Expand all
475 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); 561 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
476 break; 562 break;
477 } 563 }
478 564
479 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames 565 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
480 ** can be changed at start-time using the 566 ** can be changed at start-time using the
481 ** sqlite3_config(SQLITE_CONFIG_URI,1) or 567 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
482 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. 568 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
483 */ 569 */
484 case SQLITE_CONFIG_URI: { 570 case SQLITE_CONFIG_URI: {
571 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
572 ** argument of type int. If non-zero, then URI handling is globally
573 ** enabled. If the parameter is zero, then URI handling is globally
574 ** disabled. */
485 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); 575 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
486 break; 576 break;
487 } 577 }
488 578
489 case SQLITE_CONFIG_COVERING_INDEX_SCAN: { 579 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
580 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
581 ** option takes a single integer argument which is interpreted as a
582 ** boolean in order to enable or disable the use of covering indices for
583 ** full table scans in the query optimizer. */
490 sqlite3GlobalConfig.bUseCis = va_arg(ap, int); 584 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
491 break; 585 break;
492 } 586 }
493 587
494 #ifdef SQLITE_ENABLE_SQLLOG 588 #ifdef SQLITE_ENABLE_SQLLOG
495 case SQLITE_CONFIG_SQLLOG: { 589 case SQLITE_CONFIG_SQLLOG: {
496 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int); 590 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
497 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t); 591 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
498 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *); 592 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
499 break; 593 break;
500 } 594 }
501 #endif 595 #endif
502 596
503 case SQLITE_CONFIG_MMAP_SIZE: { 597 case SQLITE_CONFIG_MMAP_SIZE: {
598 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
599 ** integer (sqlite3_int64) values that are the default mmap size limit
600 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
601 ** mmap size limit. */
504 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); 602 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
505 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); 603 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
604 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
605 ** negative, then that argument is changed to its compile-time default.
606 **
607 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
608 ** silently truncated if necessary so that it does not exceed the
609 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
610 ** compile-time option.
611 */
506 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ 612 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
507 mxMmap = SQLITE_MAX_MMAP_SIZE; 613 mxMmap = SQLITE_MAX_MMAP_SIZE;
508 } 614 }
509 sqlite3GlobalConfig.mxMmap = mxMmap;
510 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; 615 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
511 if( szMmap>mxMmap) szMmap = mxMmap; 616 if( szMmap>mxMmap) szMmap = mxMmap;
617 sqlite3GlobalConfig.mxMmap = mxMmap;
512 sqlite3GlobalConfig.szMmap = szMmap; 618 sqlite3GlobalConfig.szMmap = szMmap;
513 break; 619 break;
514 } 620 }
515 621
516 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) 622 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
517 case SQLITE_CONFIG_WIN32_HEAPSIZE: { 623 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
624 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
625 ** unsigned integer value that specifies the maximum size of the created
626 ** heap. */
518 sqlite3GlobalConfig.nHeap = va_arg(ap, int); 627 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
519 break; 628 break;
520 } 629 }
521 #endif 630 #endif
522 631
632 case SQLITE_CONFIG_PMASZ: {
633 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
634 break;
635 }
636
523 default: { 637 default: {
524 rc = SQLITE_ERROR; 638 rc = SQLITE_ERROR;
525 break; 639 break;
526 } 640 }
527 } 641 }
528 va_end(ap); 642 va_end(ap);
529 return rc; 643 return rc;
530 } 644 }
531 645
532 /* 646 /*
533 ** Set up the lookaside buffers for a database connection. 647 ** Set up the lookaside buffers for a database connection.
534 ** Return SQLITE_OK on success. 648 ** Return SQLITE_OK on success.
535 ** If lookaside is already active, return SQLITE_BUSY. 649 ** If lookaside is already active, return SQLITE_BUSY.
536 ** 650 **
537 ** The sz parameter is the number of bytes in each lookaside slot. 651 ** The sz parameter is the number of bytes in each lookaside slot.
538 ** The cnt parameter is the number of slots. If pStart is NULL the 652 ** The cnt parameter is the number of slots. If pStart is NULL the
539 ** space for the lookaside memory is obtained from sqlite3_malloc(). 653 ** space for the lookaside memory is obtained from sqlite3_malloc().
540 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for 654 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
541 ** the lookaside memory. 655 ** the lookaside memory.
542 */ 656 */
543 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ 657 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
658 #ifndef SQLITE_OMIT_LOOKASIDE
544 void *pStart; 659 void *pStart;
545 if( db->lookaside.nOut ){ 660 if( db->lookaside.nOut ){
546 return SQLITE_BUSY; 661 return SQLITE_BUSY;
547 } 662 }
548 /* Free any existing lookaside buffer for this handle before 663 /* Free any existing lookaside buffer for this handle before
549 ** allocating a new one so we don't have to have space for 664 ** allocating a new one so we don't have to have space for
550 ** both at the same time. 665 ** both at the same time.
551 */ 666 */
552 if( db->lookaside.bMalloced ){ 667 if( db->lookaside.bMalloced ){
553 sqlite3_free(db->lookaside.pStart); 668 sqlite3_free(db->lookaside.pStart);
(...skipping 30 matching lines...) Expand all
584 } 699 }
585 db->lookaside.pEnd = p; 700 db->lookaside.pEnd = p;
586 db->lookaside.bEnabled = 1; 701 db->lookaside.bEnabled = 1;
587 db->lookaside.bMalloced = pBuf==0 ?1:0; 702 db->lookaside.bMalloced = pBuf==0 ?1:0;
588 }else{ 703 }else{
589 db->lookaside.pStart = db; 704 db->lookaside.pStart = db;
590 db->lookaside.pEnd = db; 705 db->lookaside.pEnd = db;
591 db->lookaside.bEnabled = 0; 706 db->lookaside.bEnabled = 0;
592 db->lookaside.bMalloced = 0; 707 db->lookaside.bMalloced = 0;
593 } 708 }
709 #endif /* SQLITE_OMIT_LOOKASIDE */
594 return SQLITE_OK; 710 return SQLITE_OK;
595 } 711 }
596 712
597 /* 713 /*
598 ** Return the mutex associated with a database connection. 714 ** Return the mutex associated with a database connection.
599 */ 715 */
600 sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ 716 sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
717 #ifdef SQLITE_ENABLE_API_ARMOR
718 if( !sqlite3SafetyCheckOk(db) ){
719 (void)SQLITE_MISUSE_BKPT;
720 return 0;
721 }
722 #endif
601 return db->mutex; 723 return db->mutex;
602 } 724 }
603 725
604 /* 726 /*
605 ** Free up as much memory as we can from the given database 727 ** Free up as much memory as we can from the given database
606 ** connection. 728 ** connection.
607 */ 729 */
608 int sqlite3_db_release_memory(sqlite3 *db){ 730 int sqlite3_db_release_memory(sqlite3 *db){
609 int i; 731 int i;
732
733 #ifdef SQLITE_ENABLE_API_ARMOR
734 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
735 #endif
610 sqlite3_mutex_enter(db->mutex); 736 sqlite3_mutex_enter(db->mutex);
611 sqlite3BtreeEnterAll(db); 737 sqlite3BtreeEnterAll(db);
612 for(i=0; i<db->nDb; i++){ 738 for(i=0; i<db->nDb; i++){
613 Btree *pBt = db->aDb[i].pBt; 739 Btree *pBt = db->aDb[i].pBt;
614 if( pBt ){ 740 if( pBt ){
615 Pager *pPager = sqlite3BtreePager(pBt); 741 Pager *pPager = sqlite3BtreePager(pBt);
616 sqlite3PagerShrink(pPager); 742 sqlite3PagerShrink(pPager);
617 } 743 }
618 } 744 }
619 sqlite3BtreeLeaveAll(db); 745 sqlite3BtreeLeaveAll(db);
620 sqlite3_mutex_leave(db->mutex); 746 sqlite3_mutex_leave(db->mutex);
621 return SQLITE_OK; 747 return SQLITE_OK;
622 } 748 }
623 749
624 /* 750 /*
751 ** Flush any dirty pages in the pager-cache for any attached database
752 ** to disk.
753 */
754 int sqlite3_db_cacheflush(sqlite3 *db){
755 int i;
756 int rc = SQLITE_OK;
757 int bSeenBusy = 0;
758
759 #ifdef SQLITE_ENABLE_API_ARMOR
760 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
761 #endif
762 sqlite3_mutex_enter(db->mutex);
763 sqlite3BtreeEnterAll(db);
764 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
765 Btree *pBt = db->aDb[i].pBt;
766 if( pBt && sqlite3BtreeIsInTrans(pBt) ){
767 Pager *pPager = sqlite3BtreePager(pBt);
768 rc = sqlite3PagerFlush(pPager);
769 if( rc==SQLITE_BUSY ){
770 bSeenBusy = 1;
771 rc = SQLITE_OK;
772 }
773 }
774 }
775 sqlite3BtreeLeaveAll(db);
776 sqlite3_mutex_leave(db->mutex);
777 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
778 }
779
780 /*
625 ** Configuration settings for an individual database connection 781 ** Configuration settings for an individual database connection
626 */ 782 */
627 int sqlite3_db_config(sqlite3 *db, int op, ...){ 783 int sqlite3_db_config(sqlite3 *db, int op, ...){
628 va_list ap; 784 va_list ap;
629 int rc; 785 int rc;
630 va_start(ap, op); 786 va_start(ap, op);
631 switch( op ){ 787 switch( op ){
632 case SQLITE_DBCONFIG_LOOKASIDE: { 788 case SQLITE_DBCONFIG_LOOKASIDE: {
633 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ 789 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
634 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ 790 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 ** If the padFlag argument is not NULL then space padding at the end 845 ** If the padFlag argument is not NULL then space padding at the end
690 ** of strings is ignored. This implements the RTRIM collation. 846 ** of strings is ignored. This implements the RTRIM collation.
691 */ 847 */
692 static int binCollFunc( 848 static int binCollFunc(
693 void *padFlag, 849 void *padFlag,
694 int nKey1, const void *pKey1, 850 int nKey1, const void *pKey1,
695 int nKey2, const void *pKey2 851 int nKey2, const void *pKey2
696 ){ 852 ){
697 int rc, n; 853 int rc, n;
698 n = nKey1<nKey2 ? nKey1 : nKey2; 854 n = nKey1<nKey2 ? nKey1 : nKey2;
855 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
856 ** strings byte by byte using the memcmp() function from the standard C
857 ** library. */
699 rc = memcmp(pKey1, pKey2, n); 858 rc = memcmp(pKey1, pKey2, n);
700 if( rc==0 ){ 859 if( rc==0 ){
701 if( padFlag 860 if( padFlag
702 && allSpaces(((char*)pKey1)+n, nKey1-n) 861 && allSpaces(((char*)pKey1)+n, nKey1-n)
703 && allSpaces(((char*)pKey2)+n, nKey2-n) 862 && allSpaces(((char*)pKey2)+n, nKey2-n)
704 ){ 863 ){
705 /* Leave rc unchanged at 0 */ 864 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
865 ** spaces at the end of either string do not change the result. In other
866 ** words, strings will compare equal to one another as long as they
867 ** differ only in the number of spaces at the end.
868 */
706 }else{ 869 }else{
707 rc = nKey1 - nKey2; 870 rc = nKey1 - nKey2;
708 } 871 }
709 } 872 }
710 return rc; 873 return rc;
711 } 874 }
712 875
713 /* 876 /*
714 ** Another built-in collating sequence: NOCASE. 877 ** Another built-in collating sequence: NOCASE.
715 ** 878 **
(...skipping 14 matching lines...) Expand all
730 if( 0==r ){ 893 if( 0==r ){
731 r = nKey1-nKey2; 894 r = nKey1-nKey2;
732 } 895 }
733 return r; 896 return r;
734 } 897 }
735 898
736 /* 899 /*
737 ** Return the ROWID of the most recent insert 900 ** Return the ROWID of the most recent insert
738 */ 901 */
739 sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ 902 sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
903 #ifdef SQLITE_ENABLE_API_ARMOR
904 if( !sqlite3SafetyCheckOk(db) ){
905 (void)SQLITE_MISUSE_BKPT;
906 return 0;
907 }
908 #endif
740 return db->lastRowid; 909 return db->lastRowid;
741 } 910 }
742 911
743 /* 912 /*
744 ** Return the number of changes in the most recent call to sqlite3_exec(). 913 ** Return the number of changes in the most recent call to sqlite3_exec().
745 */ 914 */
746 int sqlite3_changes(sqlite3 *db){ 915 int sqlite3_changes(sqlite3 *db){
916 #ifdef SQLITE_ENABLE_API_ARMOR
917 if( !sqlite3SafetyCheckOk(db) ){
918 (void)SQLITE_MISUSE_BKPT;
919 return 0;
920 }
921 #endif
747 return db->nChange; 922 return db->nChange;
748 } 923 }
749 924
750 /* 925 /*
751 ** Return the number of changes since the database handle was opened. 926 ** Return the number of changes since the database handle was opened.
752 */ 927 */
753 int sqlite3_total_changes(sqlite3 *db){ 928 int sqlite3_total_changes(sqlite3 *db){
929 #ifdef SQLITE_ENABLE_API_ARMOR
930 if( !sqlite3SafetyCheckOk(db) ){
931 (void)SQLITE_MISUSE_BKPT;
932 return 0;
933 }
934 #endif
754 return db->nTotalChange; 935 return db->nTotalChange;
755 } 936 }
756 937
757 /* 938 /*
758 ** Close all open savepoints. This function only manipulates fields of the 939 ** Close all open savepoints. This function only manipulates fields of the
759 ** database handle object, it does not close any savepoints that may be open 940 ** database handle object, it does not close any savepoints that may be open
760 ** at the b-tree/pager level. 941 ** at the b-tree/pager level.
761 */ 942 */
762 void sqlite3CloseSavepoints(sqlite3 *db){ 943 void sqlite3CloseSavepoints(sqlite3 *db){
763 while( db->pSavepoint ){ 944 while( db->pSavepoint ){
(...skipping 23 matching lines...) Expand all
787 } 968 }
788 } 969 }
789 970
790 /* 971 /*
791 ** Disconnect all sqlite3_vtab objects that belong to database connection 972 ** Disconnect all sqlite3_vtab objects that belong to database connection
792 ** db. This is called when db is being closed. 973 ** db. This is called when db is being closed.
793 */ 974 */
794 static void disconnectAllVtab(sqlite3 *db){ 975 static void disconnectAllVtab(sqlite3 *db){
795 #ifndef SQLITE_OMIT_VIRTUALTABLE 976 #ifndef SQLITE_OMIT_VIRTUALTABLE
796 int i; 977 int i;
978 HashElem *p;
797 sqlite3BtreeEnterAll(db); 979 sqlite3BtreeEnterAll(db);
798 for(i=0; i<db->nDb; i++){ 980 for(i=0; i<db->nDb; i++){
799 Schema *pSchema = db->aDb[i].pSchema; 981 Schema *pSchema = db->aDb[i].pSchema;
800 if( db->aDb[i].pSchema ){ 982 if( db->aDb[i].pSchema ){
801 HashElem *p;
802 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ 983 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
803 Table *pTab = (Table *)sqliteHashData(p); 984 Table *pTab = (Table *)sqliteHashData(p);
804 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab); 985 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
805 } 986 }
806 } 987 }
807 } 988 }
989 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
990 Module *pMod = (Module *)sqliteHashData(p);
991 if( pMod->pEpoTab ){
992 sqlite3VtabDisconnect(db, pMod->pEpoTab);
993 }
994 }
808 sqlite3VtabUnlockList(db); 995 sqlite3VtabUnlockList(db);
809 sqlite3BtreeLeaveAll(db); 996 sqlite3BtreeLeaveAll(db);
810 #else 997 #else
811 UNUSED_PARAMETER(db); 998 UNUSED_PARAMETER(db);
812 #endif 999 #endif
813 } 1000 }
814 1001
815 /* 1002 /*
816 ** Return TRUE if database connection db has unfinalized prepared 1003 ** Return TRUE if database connection db has unfinalized prepared
817 ** statements or unfinished sqlite3_backup objects. 1004 ** statements or unfinished sqlite3_backup objects.
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 ** the pager rollback and schema reset an atomic operation. */ 1110 ** the pager rollback and schema reset an atomic operation. */
924 sqlite3RollbackAll(db, SQLITE_OK); 1111 sqlite3RollbackAll(db, SQLITE_OK);
925 1112
926 /* Free any outstanding Savepoint structures. */ 1113 /* Free any outstanding Savepoint structures. */
927 sqlite3CloseSavepoints(db); 1114 sqlite3CloseSavepoints(db);
928 1115
929 /* Close all database connections */ 1116 /* Close all database connections */
930 for(j=0; j<db->nDb; j++){ 1117 for(j=0; j<db->nDb; j++){
931 struct Db *pDb = &db->aDb[j]; 1118 struct Db *pDb = &db->aDb[j];
932 if( pDb->pBt ){ 1119 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 }
943 sqlite3BtreeClose(pDb->pBt); 1120 sqlite3BtreeClose(pDb->pBt);
944 pDb->pBt = 0; 1121 pDb->pBt = 0;
945 if( j!=1 ){ 1122 if( j!=1 ){
946 pDb->pSchema = 0; 1123 pDb->pSchema = 0;
947 } 1124 }
948 } 1125 }
949 } 1126 }
950 /* Clear the TEMP schema separately and last */ 1127 /* Clear the TEMP schema separately and last */
951 if( db->aDb[1].pSchema ){ 1128 if( db->aDb[1].pSchema ){
952 sqlite3SchemaClear(db->aDb[1].pSchema); 1129 sqlite3SchemaClear(db->aDb[1].pSchema);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
985 } 1162 }
986 sqlite3DbFree(db, pColl); 1163 sqlite3DbFree(db, pColl);
987 } 1164 }
988 sqlite3HashClear(&db->aCollSeq); 1165 sqlite3HashClear(&db->aCollSeq);
989 #ifndef SQLITE_OMIT_VIRTUALTABLE 1166 #ifndef SQLITE_OMIT_VIRTUALTABLE
990 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ 1167 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
991 Module *pMod = (Module *)sqliteHashData(i); 1168 Module *pMod = (Module *)sqliteHashData(i);
992 if( pMod->xDestroy ){ 1169 if( pMod->xDestroy ){
993 pMod->xDestroy(pMod->pAux); 1170 pMod->xDestroy(pMod->pAux);
994 } 1171 }
1172 sqlite3VtabEponymousTableClear(db, pMod);
995 sqlite3DbFree(db, pMod); 1173 sqlite3DbFree(db, pMod);
996 } 1174 }
997 sqlite3HashClear(&db->aModule); 1175 sqlite3HashClear(&db->aModule);
998 #endif 1176 #endif
999 1177
1000 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */ 1178 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
1001 sqlite3ValueFree(db->pErr); 1179 sqlite3ValueFree(db->pErr);
1002 sqlite3CloseExtensions(db); 1180 sqlite3CloseExtensions(db);
1003 #if SQLITE_USER_AUTHENTICATION 1181 #if SQLITE_USER_AUTHENTICATION
1004 sqlite3_free(db->auth.zAuthUser); 1182 sqlite3_free(db->auth.zAuthUser);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1073 /* If one has been configured, invoke the rollback-hook callback */ 1251 /* If one has been configured, invoke the rollback-hook callback */
1074 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ 1252 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
1075 db->xRollbackCallback(db->pRollbackArg); 1253 db->xRollbackCallback(db->pRollbackArg);
1076 } 1254 }
1077 } 1255 }
1078 1256
1079 /* 1257 /*
1080 ** Return a static string containing the name corresponding to the error code 1258 ** Return a static string containing the name corresponding to the error code
1081 ** specified in the argument. 1259 ** specified in the argument.
1082 */ 1260 */
1083 #if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST) 1261 #if defined(SQLITE_NEED_ERR_NAME)
1084 const char *sqlite3ErrName(int rc){ 1262 const char *sqlite3ErrName(int rc){
1085 const char *zName = 0; 1263 const char *zName = 0;
1086 int i, origRc = rc; 1264 int i, origRc = rc;
1087 for(i=0; i<2 && zName==0; i++, rc &= 0xff){ 1265 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
1088 switch( rc ){ 1266 switch( rc ){
1089 case SQLITE_OK: zName = "SQLITE_OK"; break; 1267 case SQLITE_OK: zName = "SQLITE_OK"; break;
1090 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; 1268 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
1091 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; 1269 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
1092 case SQLITE_PERM: zName = "SQLITE_PERM"; break; 1270 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
1093 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; 1271 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
1239 /* 1417 /*
1240 ** This routine implements a busy callback that sleeps and tries 1418 ** This routine implements a busy callback that sleeps and tries
1241 ** again until a timeout value is reached. The timeout value is 1419 ** again until a timeout value is reached. The timeout value is
1242 ** an integer number of milliseconds passed in as the first 1420 ** an integer number of milliseconds passed in as the first
1243 ** argument. 1421 ** argument.
1244 */ 1422 */
1245 static int sqliteDefaultBusyCallback( 1423 static int sqliteDefaultBusyCallback(
1246 void *ptr, /* Database connection */ 1424 void *ptr, /* Database connection */
1247 int count /* Number of times table has been busy */ 1425 int count /* Number of times table has been busy */
1248 ){ 1426 ){
1249 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP) 1427 #if SQLITE_OS_WIN || HAVE_USLEEP
1250 static const u8 delays[] = 1428 static const u8 delays[] =
1251 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; 1429 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
1252 static const u8 totals[] = 1430 static const u8 totals[] =
1253 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; 1431 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
1254 # define NDELAY ArraySize(delays) 1432 # define NDELAY ArraySize(delays)
1255 sqlite3 *db = (sqlite3 *)ptr; 1433 sqlite3 *db = (sqlite3 *)ptr;
1256 int timeout = db->busyTimeout; 1434 int timeout = db->busyTimeout;
1257 int delay, prior; 1435 int delay, prior;
1258 1436
1259 assert( count>=0 ); 1437 assert( count>=0 );
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1302 1480
1303 /* 1481 /*
1304 ** This routine sets the busy callback for an Sqlite database to the 1482 ** This routine sets the busy callback for an Sqlite database to the
1305 ** given callback function with the given argument. 1483 ** given callback function with the given argument.
1306 */ 1484 */
1307 int sqlite3_busy_handler( 1485 int sqlite3_busy_handler(
1308 sqlite3 *db, 1486 sqlite3 *db,
1309 int (*xBusy)(void*,int), 1487 int (*xBusy)(void*,int),
1310 void *pArg 1488 void *pArg
1311 ){ 1489 ){
1490 #ifdef SQLITE_ENABLE_API_ARMOR
1491 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1492 #endif
1312 sqlite3_mutex_enter(db->mutex); 1493 sqlite3_mutex_enter(db->mutex);
1313 db->busyHandler.xFunc = xBusy; 1494 db->busyHandler.xFunc = xBusy;
1314 db->busyHandler.pArg = pArg; 1495 db->busyHandler.pArg = pArg;
1315 db->busyHandler.nBusy = 0; 1496 db->busyHandler.nBusy = 0;
1316 db->busyTimeout = 0; 1497 db->busyTimeout = 0;
1317 sqlite3_mutex_leave(db->mutex); 1498 sqlite3_mutex_leave(db->mutex);
1318 return SQLITE_OK; 1499 return SQLITE_OK;
1319 } 1500 }
1320 1501
1321 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1502 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1322 /* 1503 /*
1323 ** This routine sets the progress callback for an Sqlite database to the 1504 ** This routine sets the progress callback for an Sqlite database to the
1324 ** given callback function with the given argument. The progress callback will 1505 ** given callback function with the given argument. The progress callback will
1325 ** be invoked every nOps opcodes. 1506 ** be invoked every nOps opcodes.
1326 */ 1507 */
1327 void sqlite3_progress_handler( 1508 void sqlite3_progress_handler(
1328 sqlite3 *db, 1509 sqlite3 *db,
1329 int nOps, 1510 int nOps,
1330 int (*xProgress)(void*), 1511 int (*xProgress)(void*),
1331 void *pArg 1512 void *pArg
1332 ){ 1513 ){
1514 #ifdef SQLITE_ENABLE_API_ARMOR
1515 if( !sqlite3SafetyCheckOk(db) ){
1516 (void)SQLITE_MISUSE_BKPT;
1517 return;
1518 }
1519 #endif
1333 sqlite3_mutex_enter(db->mutex); 1520 sqlite3_mutex_enter(db->mutex);
1334 if( nOps>0 ){ 1521 if( nOps>0 ){
1335 db->xProgress = xProgress; 1522 db->xProgress = xProgress;
1336 db->nProgressOps = (unsigned)nOps; 1523 db->nProgressOps = (unsigned)nOps;
1337 db->pProgressArg = pArg; 1524 db->pProgressArg = pArg;
1338 }else{ 1525 }else{
1339 db->xProgress = 0; 1526 db->xProgress = 0;
1340 db->nProgressOps = 0; 1527 db->nProgressOps = 0;
1341 db->pProgressArg = 0; 1528 db->pProgressArg = 0;
1342 } 1529 }
1343 sqlite3_mutex_leave(db->mutex); 1530 sqlite3_mutex_leave(db->mutex);
1344 } 1531 }
1345 #endif 1532 #endif
1346 1533
1347 1534
1348 /* 1535 /*
1349 ** This routine installs a default busy handler that waits for the 1536 ** This routine installs a default busy handler that waits for the
1350 ** specified number of milliseconds before returning 0. 1537 ** specified number of milliseconds before returning 0.
1351 */ 1538 */
1352 int sqlite3_busy_timeout(sqlite3 *db, int ms){ 1539 int sqlite3_busy_timeout(sqlite3 *db, int ms){
1540 #ifdef SQLITE_ENABLE_API_ARMOR
1541 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1542 #endif
1353 if( ms>0 ){ 1543 if( ms>0 ){
1354 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); 1544 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
1355 db->busyTimeout = ms; 1545 db->busyTimeout = ms;
1356 }else{ 1546 }else{
1357 sqlite3_busy_handler(db, 0, 0); 1547 sqlite3_busy_handler(db, 0, 0);
1358 } 1548 }
1359 return SQLITE_OK; 1549 return SQLITE_OK;
1360 } 1550 }
1361 1551
1362 /* 1552 /*
1363 ** Cause any pending operation to stop at its earliest opportunity. 1553 ** Cause any pending operation to stop at its earliest opportunity.
1364 */ 1554 */
1365 void sqlite3_interrupt(sqlite3 *db){ 1555 void sqlite3_interrupt(sqlite3 *db){
1556 #ifdef SQLITE_ENABLE_API_ARMOR
1557 if( !sqlite3SafetyCheckOk(db) ){
1558 (void)SQLITE_MISUSE_BKPT;
1559 return;
1560 }
1561 #endif
1366 db->u1.isInterrupted = 1; 1562 db->u1.isInterrupted = 1;
1367 } 1563 }
1368 1564
1369 1565
1370 /* 1566 /*
1371 ** This function is exactly the same as sqlite3_create_function(), except 1567 ** This function is exactly the same as sqlite3_create_function(), except
1372 ** that it is designed to be called by internal code. The difference is 1568 ** that it is designed to be called by internal code. The difference is
1373 ** that if a malloc() fails in sqlite3_create_function(), an error code 1569 ** that if a malloc() fails in sqlite3_create_function(), an error code
1374 ** is returned and the mallocFailed flag cleared. 1570 ** is returned and the mallocFailed flag cleared.
1375 */ 1571 */
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
1493 int nArg, 1689 int nArg,
1494 int enc, 1690 int enc,
1495 void *p, 1691 void *p,
1496 void (*xFunc)(sqlite3_context*,int,sqlite3_value **), 1692 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1497 void (*xStep)(sqlite3_context*,int,sqlite3_value **), 1693 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1498 void (*xFinal)(sqlite3_context*), 1694 void (*xFinal)(sqlite3_context*),
1499 void (*xDestroy)(void *) 1695 void (*xDestroy)(void *)
1500 ){ 1696 ){
1501 int rc = SQLITE_ERROR; 1697 int rc = SQLITE_ERROR;
1502 FuncDestructor *pArg = 0; 1698 FuncDestructor *pArg = 0;
1699
1700 #ifdef SQLITE_ENABLE_API_ARMOR
1701 if( !sqlite3SafetyCheckOk(db) ){
1702 return SQLITE_MISUSE_BKPT;
1703 }
1704 #endif
1503 sqlite3_mutex_enter(db->mutex); 1705 sqlite3_mutex_enter(db->mutex);
1504 if( xDestroy ){ 1706 if( xDestroy ){
1505 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); 1707 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
1506 if( !pArg ){ 1708 if( !pArg ){
1507 xDestroy(p); 1709 xDestroy(p);
1508 goto out; 1710 goto out;
1509 } 1711 }
1510 pArg->xDestroy = xDestroy; 1712 pArg->xDestroy = xDestroy;
1511 pArg->pUserData = p; 1713 pArg->pUserData = p;
1512 } 1714 }
(...skipping 16 matching lines...) Expand all
1529 const void *zFunctionName, 1731 const void *zFunctionName,
1530 int nArg, 1732 int nArg,
1531 int eTextRep, 1733 int eTextRep,
1532 void *p, 1734 void *p,
1533 void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 1735 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
1534 void (*xStep)(sqlite3_context*,int,sqlite3_value**), 1736 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
1535 void (*xFinal)(sqlite3_context*) 1737 void (*xFinal)(sqlite3_context*)
1536 ){ 1738 ){
1537 int rc; 1739 int rc;
1538 char *zFunc8; 1740 char *zFunc8;
1741
1742 #ifdef SQLITE_ENABLE_API_ARMOR
1743 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
1744 #endif
1539 sqlite3_mutex_enter(db->mutex); 1745 sqlite3_mutex_enter(db->mutex);
1540 assert( !db->mallocFailed ); 1746 assert( !db->mallocFailed );
1541 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); 1747 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
1542 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); 1748 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
1543 sqlite3DbFree(db, zFunc8); 1749 sqlite3DbFree(db, zFunc8);
1544 rc = sqlite3ApiExit(db, rc); 1750 rc = sqlite3ApiExit(db, rc);
1545 sqlite3_mutex_leave(db->mutex); 1751 sqlite3_mutex_leave(db->mutex);
1546 return rc; 1752 return rc;
1547 } 1753 }
1548 #endif 1754 #endif
(...skipping 11 matching lines...) Expand all
1560 ** A global function must exist in order for name resolution to work 1766 ** A global function must exist in order for name resolution to work
1561 ** properly. 1767 ** properly.
1562 */ 1768 */
1563 int sqlite3_overload_function( 1769 int sqlite3_overload_function(
1564 sqlite3 *db, 1770 sqlite3 *db,
1565 const char *zName, 1771 const char *zName,
1566 int nArg 1772 int nArg
1567 ){ 1773 ){
1568 int nName = sqlite3Strlen30(zName); 1774 int nName = sqlite3Strlen30(zName);
1569 int rc = SQLITE_OK; 1775 int rc = SQLITE_OK;
1776
1777 #ifdef SQLITE_ENABLE_API_ARMOR
1778 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
1779 return SQLITE_MISUSE_BKPT;
1780 }
1781 #endif
1570 sqlite3_mutex_enter(db->mutex); 1782 sqlite3_mutex_enter(db->mutex);
1571 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ 1783 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
1572 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, 1784 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
1573 0, sqlite3InvalidFunction, 0, 0, 0); 1785 0, sqlite3InvalidFunction, 0, 0, 0);
1574 } 1786 }
1575 rc = sqlite3ApiExit(db, rc); 1787 rc = sqlite3ApiExit(db, rc);
1576 sqlite3_mutex_leave(db->mutex); 1788 sqlite3_mutex_leave(db->mutex);
1577 return rc; 1789 return rc;
1578 } 1790 }
1579 1791
1580 #ifndef SQLITE_OMIT_TRACE 1792 #ifndef SQLITE_OMIT_TRACE
1581 /* 1793 /*
1582 ** Register a trace function. The pArg from the previously registered trace 1794 ** Register a trace function. The pArg from the previously registered trace
1583 ** is returned. 1795 ** is returned.
1584 ** 1796 **
1585 ** A NULL trace function means that no tracing is executes. A non-NULL 1797 ** A NULL trace function means that no tracing is executes. A non-NULL
1586 ** trace is a pointer to a function that is invoked at the start of each 1798 ** trace is a pointer to a function that is invoked at the start of each
1587 ** SQL statement. 1799 ** SQL statement.
1588 */ 1800 */
1589 void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ 1801 void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
1590 void *pOld; 1802 void *pOld;
1803
1804 #ifdef SQLITE_ENABLE_API_ARMOR
1805 if( !sqlite3SafetyCheckOk(db) ){
1806 (void)SQLITE_MISUSE_BKPT;
1807 return 0;
1808 }
1809 #endif
1591 sqlite3_mutex_enter(db->mutex); 1810 sqlite3_mutex_enter(db->mutex);
1592 pOld = db->pTraceArg; 1811 pOld = db->pTraceArg;
1593 db->xTrace = xTrace; 1812 db->xTrace = xTrace;
1594 db->pTraceArg = pArg; 1813 db->pTraceArg = pArg;
1595 sqlite3_mutex_leave(db->mutex); 1814 sqlite3_mutex_leave(db->mutex);
1596 return pOld; 1815 return pOld;
1597 } 1816 }
1598 /* 1817 /*
1599 ** Register a profile function. The pArg from the previously registered 1818 ** Register a profile function. The pArg from the previously registered
1600 ** profile function is returned. 1819 ** profile function is returned.
1601 ** 1820 **
1602 ** A NULL profile function means that no profiling is executes. A non-NULL 1821 ** A NULL profile function means that no profiling is executes. A non-NULL
1603 ** profile is a pointer to a function that is invoked at the conclusion of 1822 ** profile is a pointer to a function that is invoked at the conclusion of
1604 ** each SQL statement that is run. 1823 ** each SQL statement that is run.
1605 */ 1824 */
1606 void *sqlite3_profile( 1825 void *sqlite3_profile(
1607 sqlite3 *db, 1826 sqlite3 *db,
1608 void (*xProfile)(void*,const char*,sqlite_uint64), 1827 void (*xProfile)(void*,const char*,sqlite_uint64),
1609 void *pArg 1828 void *pArg
1610 ){ 1829 ){
1611 void *pOld; 1830 void *pOld;
1831
1832 #ifdef SQLITE_ENABLE_API_ARMOR
1833 if( !sqlite3SafetyCheckOk(db) ){
1834 (void)SQLITE_MISUSE_BKPT;
1835 return 0;
1836 }
1837 #endif
1612 sqlite3_mutex_enter(db->mutex); 1838 sqlite3_mutex_enter(db->mutex);
1613 pOld = db->pProfileArg; 1839 pOld = db->pProfileArg;
1614 db->xProfile = xProfile; 1840 db->xProfile = xProfile;
1615 db->pProfileArg = pArg; 1841 db->pProfileArg = pArg;
1616 sqlite3_mutex_leave(db->mutex); 1842 sqlite3_mutex_leave(db->mutex);
1617 return pOld; 1843 return pOld;
1618 } 1844 }
1619 #endif /* SQLITE_OMIT_TRACE */ 1845 #endif /* SQLITE_OMIT_TRACE */
1620 1846
1621 /* 1847 /*
1622 ** Register a function to be invoked when a transaction commits. 1848 ** Register a function to be invoked when a transaction commits.
1623 ** If the invoked function returns non-zero, then the commit becomes a 1849 ** If the invoked function returns non-zero, then the commit becomes a
1624 ** rollback. 1850 ** rollback.
1625 */ 1851 */
1626 void *sqlite3_commit_hook( 1852 void *sqlite3_commit_hook(
1627 sqlite3 *db, /* Attach the hook to this database */ 1853 sqlite3 *db, /* Attach the hook to this database */
1628 int (*xCallback)(void*), /* Function to invoke on each commit */ 1854 int (*xCallback)(void*), /* Function to invoke on each commit */
1629 void *pArg /* Argument to the function */ 1855 void *pArg /* Argument to the function */
1630 ){ 1856 ){
1631 void *pOld; 1857 void *pOld;
1858
1859 #ifdef SQLITE_ENABLE_API_ARMOR
1860 if( !sqlite3SafetyCheckOk(db) ){
1861 (void)SQLITE_MISUSE_BKPT;
1862 return 0;
1863 }
1864 #endif
1632 sqlite3_mutex_enter(db->mutex); 1865 sqlite3_mutex_enter(db->mutex);
1633 pOld = db->pCommitArg; 1866 pOld = db->pCommitArg;
1634 db->xCommitCallback = xCallback; 1867 db->xCommitCallback = xCallback;
1635 db->pCommitArg = pArg; 1868 db->pCommitArg = pArg;
1636 sqlite3_mutex_leave(db->mutex); 1869 sqlite3_mutex_leave(db->mutex);
1637 return pOld; 1870 return pOld;
1638 } 1871 }
1639 1872
1640 /* 1873 /*
1641 ** Register a callback to be invoked each time a row is updated, 1874 ** Register a callback to be invoked each time a row is updated,
1642 ** inserted or deleted using this database connection. 1875 ** inserted or deleted using this database connection.
1643 */ 1876 */
1644 void *sqlite3_update_hook( 1877 void *sqlite3_update_hook(
1645 sqlite3 *db, /* Attach the hook to this database */ 1878 sqlite3 *db, /* Attach the hook to this database */
1646 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), 1879 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
1647 void *pArg /* Argument to the function */ 1880 void *pArg /* Argument to the function */
1648 ){ 1881 ){
1649 void *pRet; 1882 void *pRet;
1883
1884 #ifdef SQLITE_ENABLE_API_ARMOR
1885 if( !sqlite3SafetyCheckOk(db) ){
1886 (void)SQLITE_MISUSE_BKPT;
1887 return 0;
1888 }
1889 #endif
1650 sqlite3_mutex_enter(db->mutex); 1890 sqlite3_mutex_enter(db->mutex);
1651 pRet = db->pUpdateArg; 1891 pRet = db->pUpdateArg;
1652 db->xUpdateCallback = xCallback; 1892 db->xUpdateCallback = xCallback;
1653 db->pUpdateArg = pArg; 1893 db->pUpdateArg = pArg;
1654 sqlite3_mutex_leave(db->mutex); 1894 sqlite3_mutex_leave(db->mutex);
1655 return pRet; 1895 return pRet;
1656 } 1896 }
1657 1897
1658 /* 1898 /*
1659 ** Register a callback to be invoked each time a transaction is rolled 1899 ** Register a callback to be invoked each time a transaction is rolled
1660 ** back by this database connection. 1900 ** back by this database connection.
1661 */ 1901 */
1662 void *sqlite3_rollback_hook( 1902 void *sqlite3_rollback_hook(
1663 sqlite3 *db, /* Attach the hook to this database */ 1903 sqlite3 *db, /* Attach the hook to this database */
1664 void (*xCallback)(void*), /* Callback function */ 1904 void (*xCallback)(void*), /* Callback function */
1665 void *pArg /* Argument to the function */ 1905 void *pArg /* Argument to the function */
1666 ){ 1906 ){
1667 void *pRet; 1907 void *pRet;
1908
1909 #ifdef SQLITE_ENABLE_API_ARMOR
1910 if( !sqlite3SafetyCheckOk(db) ){
1911 (void)SQLITE_MISUSE_BKPT;
1912 return 0;
1913 }
1914 #endif
1668 sqlite3_mutex_enter(db->mutex); 1915 sqlite3_mutex_enter(db->mutex);
1669 pRet = db->pRollbackArg; 1916 pRet = db->pRollbackArg;
1670 db->xRollbackCallback = xCallback; 1917 db->xRollbackCallback = xCallback;
1671 db->pRollbackArg = pArg; 1918 db->pRollbackArg = pArg;
1672 sqlite3_mutex_leave(db->mutex); 1919 sqlite3_mutex_leave(db->mutex);
1673 return pRet; 1920 return pRet;
1674 } 1921 }
1675 1922
1676 #ifndef SQLITE_OMIT_WAL 1923 #ifndef SQLITE_OMIT_WAL
1677 /* 1924 /*
(...skipping 26 matching lines...) Expand all
1704 ** The callback registered by this function replaces any existing callback 1951 ** The callback registered by this function replaces any existing callback
1705 ** registered using sqlite3_wal_hook(). Likewise, registering a callback 1952 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
1706 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism 1953 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
1707 ** configured by this function. 1954 ** configured by this function.
1708 */ 1955 */
1709 int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ 1956 int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
1710 #ifdef SQLITE_OMIT_WAL 1957 #ifdef SQLITE_OMIT_WAL
1711 UNUSED_PARAMETER(db); 1958 UNUSED_PARAMETER(db);
1712 UNUSED_PARAMETER(nFrame); 1959 UNUSED_PARAMETER(nFrame);
1713 #else 1960 #else
1961 #ifdef SQLITE_ENABLE_API_ARMOR
1962 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1963 #endif
1714 if( nFrame>0 ){ 1964 if( nFrame>0 ){
1715 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); 1965 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
1716 }else{ 1966 }else{
1717 sqlite3_wal_hook(db, 0, 0); 1967 sqlite3_wal_hook(db, 0, 0);
1718 } 1968 }
1719 #endif 1969 #endif
1720 return SQLITE_OK; 1970 return SQLITE_OK;
1721 } 1971 }
1722 1972
1723 /* 1973 /*
1724 ** Register a callback to be invoked each time a transaction is written 1974 ** Register a callback to be invoked each time a transaction is written
1725 ** into the write-ahead-log by this database connection. 1975 ** into the write-ahead-log by this database connection.
1726 */ 1976 */
1727 void *sqlite3_wal_hook( 1977 void *sqlite3_wal_hook(
1728 sqlite3 *db, /* Attach the hook to this db handle */ 1978 sqlite3 *db, /* Attach the hook to this db handle */
1729 int(*xCallback)(void *, sqlite3*, const char*, int), 1979 int(*xCallback)(void *, sqlite3*, const char*, int),
1730 void *pArg /* First argument passed to xCallback() */ 1980 void *pArg /* First argument passed to xCallback() */
1731 ){ 1981 ){
1732 #ifndef SQLITE_OMIT_WAL 1982 #ifndef SQLITE_OMIT_WAL
1733 void *pRet; 1983 void *pRet;
1984 #ifdef SQLITE_ENABLE_API_ARMOR
1985 if( !sqlite3SafetyCheckOk(db) ){
1986 (void)SQLITE_MISUSE_BKPT;
1987 return 0;
1988 }
1989 #endif
1734 sqlite3_mutex_enter(db->mutex); 1990 sqlite3_mutex_enter(db->mutex);
1735 pRet = db->pWalArg; 1991 pRet = db->pWalArg;
1736 db->xWalCallback = xCallback; 1992 db->xWalCallback = xCallback;
1737 db->pWalArg = pArg; 1993 db->pWalArg = pArg;
1738 sqlite3_mutex_leave(db->mutex); 1994 sqlite3_mutex_leave(db->mutex);
1739 return pRet; 1995 return pRet;
1740 #else 1996 #else
1741 return 0; 1997 return 0;
1742 #endif 1998 #endif
1743 } 1999 }
1744 2000
1745 /* 2001 /*
1746 ** Checkpoint database zDb. 2002 ** Checkpoint database zDb.
1747 */ 2003 */
1748 int sqlite3_wal_checkpoint_v2( 2004 int sqlite3_wal_checkpoint_v2(
1749 sqlite3 *db, /* Database handle */ 2005 sqlite3 *db, /* Database handle */
1750 const char *zDb, /* Name of attached database (or NULL) */ 2006 const char *zDb, /* Name of attached database (or NULL) */
1751 int eMode, /* SQLITE_CHECKPOINT_* value */ 2007 int eMode, /* SQLITE_CHECKPOINT_* value */
1752 int *pnLog, /* OUT: Size of WAL log in frames */ 2008 int *pnLog, /* OUT: Size of WAL log in frames */
1753 int *pnCkpt /* OUT: Total number of frames checkpointed */ 2009 int *pnCkpt /* OUT: Total number of frames checkpointed */
1754 ){ 2010 ){
1755 #ifdef SQLITE_OMIT_WAL 2011 #ifdef SQLITE_OMIT_WAL
1756 return SQLITE_OK; 2012 return SQLITE_OK;
1757 #else 2013 #else
1758 int rc; /* Return code */ 2014 int rc; /* Return code */
1759 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ 2015 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
1760 2016
2017 #ifdef SQLITE_ENABLE_API_ARMOR
2018 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
2019 #endif
2020
1761 /* Initialize the output variables to -1 in case an error occurs. */ 2021 /* Initialize the output variables to -1 in case an error occurs. */
1762 if( pnLog ) *pnLog = -1; 2022 if( pnLog ) *pnLog = -1;
1763 if( pnCkpt ) *pnCkpt = -1; 2023 if( pnCkpt ) *pnCkpt = -1;
1764 2024
1765 assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE ); 2025 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
1766 assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART ); 2026 assert( SQLITE_CHECKPOINT_FULL==1 );
1767 assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART ); 2027 assert( SQLITE_CHECKPOINT_RESTART==2 );
1768 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){ 2028 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
2029 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
2030 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
2031 ** mode: */
1769 return SQLITE_MISUSE; 2032 return SQLITE_MISUSE;
1770 } 2033 }
1771 2034
1772 sqlite3_mutex_enter(db->mutex); 2035 sqlite3_mutex_enter(db->mutex);
1773 if( zDb && zDb[0] ){ 2036 if( zDb && zDb[0] ){
1774 iDb = sqlite3FindDbName(db, zDb); 2037 iDb = sqlite3FindDbName(db, zDb);
1775 } 2038 }
1776 if( iDb<0 ){ 2039 if( iDb<0 ){
1777 rc = SQLITE_ERROR; 2040 rc = SQLITE_ERROR;
1778 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb); 2041 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
1779 }else{ 2042 }else{
2043 db->busyHandler.nBusy = 0;
1780 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); 2044 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
1781 sqlite3Error(db, rc); 2045 sqlite3Error(db, rc);
1782 } 2046 }
1783 rc = sqlite3ApiExit(db, rc); 2047 rc = sqlite3ApiExit(db, rc);
1784 sqlite3_mutex_leave(db->mutex); 2048 sqlite3_mutex_leave(db->mutex);
1785 return rc; 2049 return rc;
1786 #endif 2050 #endif
1787 } 2051 }
1788 2052
1789 2053
1790 /* 2054 /*
1791 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points 2055 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
1792 ** to contains a zero-length string, all attached databases are 2056 ** to contains a zero-length string, all attached databases are
1793 ** checkpointed. 2057 ** checkpointed.
1794 */ 2058 */
1795 int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ 2059 int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
1796 return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0); 2060 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
2061 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
2062 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
1797 } 2063 }
1798 2064
1799 #ifndef SQLITE_OMIT_WAL 2065 #ifndef SQLITE_OMIT_WAL
1800 /* 2066 /*
1801 ** Run a checkpoint on database iDb. This is a no-op if database iDb is 2067 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
1802 ** not currently open in WAL mode. 2068 ** not currently open in WAL mode.
1803 ** 2069 **
1804 ** If a transaction is open on the database being checkpointed, this 2070 ** If a transaction is open on the database being checkpointed, this
1805 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 2071 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
1806 ** an error occurs while running the checkpoint, an SQLite error code is 2072 ** an error occurs while running the checkpoint, an SQLite error code is
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1861 ** 3 any memory (return 1) 2127 ** 3 any memory (return 1)
1862 */ 2128 */
1863 int sqlite3TempInMemory(const sqlite3 *db){ 2129 int sqlite3TempInMemory(const sqlite3 *db){
1864 #if SQLITE_TEMP_STORE==1 2130 #if SQLITE_TEMP_STORE==1
1865 return ( db->temp_store==2 ); 2131 return ( db->temp_store==2 );
1866 #endif 2132 #endif
1867 #if SQLITE_TEMP_STORE==2 2133 #if SQLITE_TEMP_STORE==2
1868 return ( db->temp_store!=1 ); 2134 return ( db->temp_store!=1 );
1869 #endif 2135 #endif
1870 #if SQLITE_TEMP_STORE==3 2136 #if SQLITE_TEMP_STORE==3
2137 UNUSED_PARAMETER(db);
1871 return 1; 2138 return 1;
1872 #endif 2139 #endif
1873 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 2140 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
2141 UNUSED_PARAMETER(db);
1874 return 0; 2142 return 0;
1875 #endif 2143 #endif
1876 } 2144 }
1877 2145
1878 /* 2146 /*
1879 ** Return UTF-8 encoded English language explanation of the most recent 2147 ** Return UTF-8 encoded English language explanation of the most recent
1880 ** error. 2148 ** error.
1881 */ 2149 */
1882 const char *sqlite3_errmsg(sqlite3 *db){ 2150 const char *sqlite3_errmsg(sqlite3 *db){
1883 const char *z; 2151 const char *z;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1974 /* 2242 /*
1975 ** Return a string that describes the kind of error specified in the 2243 ** Return a string that describes the kind of error specified in the
1976 ** argument. For now, this simply calls the internal sqlite3ErrStr() 2244 ** argument. For now, this simply calls the internal sqlite3ErrStr()
1977 ** function. 2245 ** function.
1978 */ 2246 */
1979 const char *sqlite3_errstr(int rc){ 2247 const char *sqlite3_errstr(int rc){
1980 return sqlite3ErrStr(rc); 2248 return sqlite3ErrStr(rc);
1981 } 2249 }
1982 2250
1983 /* 2251 /*
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 /*
2010 ** Create a new collating function for database "db". The name is zName 2252 ** Create a new collating function for database "db". The name is zName
2011 ** and the encoding is enc. 2253 ** and the encoding is enc.
2012 */ 2254 */
2013 static int createCollation( 2255 static int createCollation(
2014 sqlite3* db, 2256 sqlite3* db,
2015 const char *zName, 2257 const char *zName,
2016 u8 enc, 2258 u8 enc,
2017 void* pCtx, 2259 void* pCtx,
2018 int(*xCompare)(void*,int,const void*,int,const void*), 2260 int(*xCompare)(void*,int,const void*,int,const void*),
2019 void(*xDel)(void*) 2261 void(*xDel)(void*)
(...skipping 22 matching lines...) Expand all
2042 ** are no active VMs, invalidate any pre-compiled statements. 2284 ** are no active VMs, invalidate any pre-compiled statements.
2043 */ 2285 */
2044 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); 2286 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
2045 if( pColl && pColl->xCmp ){ 2287 if( pColl && pColl->xCmp ){
2046 if( db->nVdbeActive ){ 2288 if( db->nVdbeActive ){
2047 sqlite3ErrorWithMsg(db, SQLITE_BUSY, 2289 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
2048 "unable to delete/modify collation sequence due to active statements"); 2290 "unable to delete/modify collation sequence due to active statements");
2049 return SQLITE_BUSY; 2291 return SQLITE_BUSY;
2050 } 2292 }
2051 sqlite3ExpirePreparedStatements(db); 2293 sqlite3ExpirePreparedStatements(db);
2052 invalidateCachedKeyInfo(db);
2053 2294
2054 /* If collation sequence pColl was created directly by a call to 2295 /* If collation sequence pColl was created directly by a call to
2055 ** sqlite3_create_collation, and not generated by synthCollSeq(), 2296 ** sqlite3_create_collation, and not generated by synthCollSeq(),
2056 ** then any copies made by synthCollSeq() need to be invalidated. 2297 ** then any copies made by synthCollSeq() need to be invalidated.
2057 ** Also, collation destructor - CollSeq.xDel() - function may need 2298 ** Also, collation destructor - CollSeq.xDel() - function may need
2058 ** to be called. 2299 ** to be called.
2059 */ 2300 */
2060 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ 2301 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
2061 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName); 2302 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
2062 int j; 2303 int j;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
2147 ** Make no changes but still report the old value if the 2388 ** Make no changes but still report the old value if the
2148 ** new limit is negative. 2389 ** new limit is negative.
2149 ** 2390 **
2150 ** A new lower limit does not shrink existing constructs. 2391 ** A new lower limit does not shrink existing constructs.
2151 ** It merely prevents new constructs that exceed the limit 2392 ** It merely prevents new constructs that exceed the limit
2152 ** from forming. 2393 ** from forming.
2153 */ 2394 */
2154 int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ 2395 int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
2155 int oldLimit; 2396 int oldLimit;
2156 2397
2398 #ifdef SQLITE_ENABLE_API_ARMOR
2399 if( !sqlite3SafetyCheckOk(db) ){
2400 (void)SQLITE_MISUSE_BKPT;
2401 return -1;
2402 }
2403 #endif
2157 2404
2158 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME 2405 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
2159 ** there is a hard upper bound set at compile-time by a C preprocessor 2406 ** there is a hard upper bound set at compile-time by a C preprocessor
2160 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to 2407 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
2161 ** "_MAX_".) 2408 ** "_MAX_".)
2162 */ 2409 */
2163 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); 2410 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
2164 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); 2411 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
2165 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); 2412 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
2166 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); 2413 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
2223 ){ 2470 ){
2224 int rc = SQLITE_OK; 2471 int rc = SQLITE_OK;
2225 unsigned int flags = *pFlags; 2472 unsigned int flags = *pFlags;
2226 const char *zVfs = zDefaultVfs; 2473 const char *zVfs = zDefaultVfs;
2227 char *zFile; 2474 char *zFile;
2228 char c; 2475 char c;
2229 int nUri = sqlite3Strlen30(zUri); 2476 int nUri = sqlite3Strlen30(zUri);
2230 2477
2231 assert( *pzErrMsg==0 ); 2478 assert( *pzErrMsg==0 );
2232 2479
2233 if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) 2480 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
2481 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
2234 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ 2482 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
2235 ){ 2483 ){
2236 char *zOpt; 2484 char *zOpt;
2237 int eState; /* Parser state when parsing URI */ 2485 int eState; /* Parser state when parsing URI */
2238 int iIn; /* Input character index */ 2486 int iIn; /* Input character index */
2239 int iOut = 0; /* Output character index */ 2487 int iOut = 0; /* Output character index */
2240 int nByte = nUri+2; /* Bytes of space to allocate */ 2488 u64 nByte = nUri+2; /* Bytes of space to allocate */
2241 2489
2242 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 2490 /* 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. */ 2491 ** method that there may be extra parameters following the file-name. */
2244 flags |= SQLITE_OPEN_URI; 2492 flags |= SQLITE_OPEN_URI;
2245 2493
2246 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&'); 2494 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
2247 zFile = sqlite3_malloc(nByte); 2495 zFile = sqlite3_malloc64(nByte);
2248 if( !zFile ) return SQLITE_NOMEM; 2496 if( !zFile ) return SQLITE_NOMEM;
2249 2497
2250 iIn = 5; 2498 iIn = 5;
2251 #ifndef SQLITE_ALLOW_URI_AUTHORITY 2499 #ifdef SQLITE_ALLOW_URI_AUTHORITY
2500 if( strncmp(zUri+5, "///", 3)==0 ){
2501 iIn = 7;
2502 /* The following condition causes URIs with five leading / characters
2503 ** like file://///host/path to be converted into UNCs like //host/path.
2504 ** The correct URI for that UNC has only two or four leading / characters
2505 ** file://host/path or file:////host/path. But 5 leading slashes is a
2506 ** common error, we are told, so we handle it as a special case. */
2507 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
2508 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
2509 iIn = 16;
2510 }
2511 #else
2252 /* Discard the scheme and authority segments of the URI. */ 2512 /* Discard the scheme and authority segments of the URI. */
2253 if( zUri[5]=='/' && zUri[6]=='/' ){ 2513 if( zUri[5]=='/' && zUri[6]=='/' ){
2254 iIn = 7; 2514 iIn = 7;
2255 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++; 2515 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
2256 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){ 2516 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
2257 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", 2517 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
2258 iIn-7, &zUri[7]); 2518 iIn-7, &zUri[7]);
2259 rc = SQLITE_ERROR; 2519 rc = SQLITE_ERROR;
2260 goto parse_uri_out; 2520 goto parse_uri_out;
2261 } 2521 }
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
2391 goto parse_uri_out; 2651 goto parse_uri_out;
2392 } 2652 }
2393 flags = (flags & ~mask) | mode; 2653 flags = (flags & ~mask) | mode;
2394 } 2654 }
2395 } 2655 }
2396 2656
2397 zOpt = &zVal[nVal+1]; 2657 zOpt = &zVal[nVal+1];
2398 } 2658 }
2399 2659
2400 }else{ 2660 }else{
2401 zFile = sqlite3_malloc(nUri+2); 2661 zFile = sqlite3_malloc64(nUri+2);
2402 if( !zFile ) return SQLITE_NOMEM; 2662 if( !zFile ) return SQLITE_NOMEM;
2403 memcpy(zFile, zUri, nUri); 2663 memcpy(zFile, zUri, nUri);
2404 zFile[nUri] = '\0'; 2664 zFile[nUri] = '\0';
2405 zFile[nUri+1] = '\0'; 2665 zFile[nUri+1] = '\0';
2406 flags &= ~SQLITE_OPEN_URI; 2666 flags &= ~SQLITE_OPEN_URI;
2407 } 2667 }
2408 2668
2409 *ppVfs = sqlite3_vfs_find(zVfs); 2669 *ppVfs = sqlite3_vfs_find(zVfs);
2410 if( *ppVfs==0 ){ 2670 if( *ppVfs==0 ){
2411 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs); 2671 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
(...skipping 20 matching lines...) Expand all
2432 sqlite3 **ppDb, /* OUT: Returned database handle */ 2692 sqlite3 **ppDb, /* OUT: Returned database handle */
2433 unsigned int flags, /* Operational flags */ 2693 unsigned int flags, /* Operational flags */
2434 const char *zVfs /* Name of the VFS to use */ 2694 const char *zVfs /* Name of the VFS to use */
2435 ){ 2695 ){
2436 sqlite3 *db; /* Store allocated handle here */ 2696 sqlite3 *db; /* Store allocated handle here */
2437 int rc; /* Return code */ 2697 int rc; /* Return code */
2438 int isThreadsafe; /* True for threadsafe connections */ 2698 int isThreadsafe; /* True for threadsafe connections */
2439 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ 2699 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
2440 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ 2700 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
2441 2701
2702 #ifdef SQLITE_ENABLE_API_ARMOR
2703 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
2704 #endif
2442 *ppDb = 0; 2705 *ppDb = 0;
2443 #ifndef SQLITE_OMIT_AUTOINIT 2706 #ifndef SQLITE_OMIT_AUTOINIT
2444 rc = sqlite3_initialize(); 2707 rc = sqlite3_initialize();
2445 if( rc ) return rc; 2708 if( rc ) return rc;
2446 #endif 2709 #endif
2447 2710
2448 /* Only allow sensible combinations of bits in the flags argument. 2711 /* Only allow sensible combinations of bits in the flags argument.
2449 ** Throw an error if any non-sense combination is used. If we 2712 ** Throw an error if any non-sense combination is used. If we
2450 ** do not block illegal combinations here, it could trigger 2713 ** do not block illegal combinations here, it could trigger
2451 ** assert() statements in deeper layers. Sensible combinations 2714 ** assert() statements in deeper layers. Sensible combinations
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
2525 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS; 2788 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
2526 db->autoCommit = 1; 2789 db->autoCommit = 1;
2527 db->nextAutovac = -1; 2790 db->nextAutovac = -1;
2528 db->szMmap = sqlite3GlobalConfig.szMmap; 2791 db->szMmap = sqlite3GlobalConfig.szMmap;
2529 db->nextPagesize = 0; 2792 db->nextPagesize = 0;
2530 db->nMaxSorterMmap = 0x7FFFFFFF; 2793 db->nMaxSorterMmap = 0x7FFFFFFF;
2531 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill 2794 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
2532 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX 2795 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
2533 | SQLITE_AutoIndex 2796 | SQLITE_AutoIndex
2534 #endif 2797 #endif
2798 #if SQLITE_DEFAULT_CKPTFULLFSYNC
2799 | SQLITE_CkptFullFSync
2800 #endif
2535 #if SQLITE_DEFAULT_FILE_FORMAT<4 2801 #if SQLITE_DEFAULT_FILE_FORMAT<4
2536 | SQLITE_LegacyFileFmt 2802 | SQLITE_LegacyFileFmt
2537 #endif 2803 #endif
2538 #ifdef SQLITE_ENABLE_LOAD_EXTENSION 2804 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
2539 | SQLITE_LoadExtension 2805 | SQLITE_LoadExtension
2540 #endif 2806 #endif
2541 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS 2807 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
2542 | SQLITE_RecTriggers 2808 | SQLITE_RecTriggers
2543 #endif 2809 #endif
2544 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS 2810 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
2545 | SQLITE_ForeignKeys 2811 | SQLITE_ForeignKeys
2546 #endif 2812 #endif
2813 #if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
2814 | SQLITE_ReverseOrder
2815 #endif
2816 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
2817 | SQLITE_CellSizeCk
2818 #endif
2547 ; 2819 ;
2548 sqlite3HashInit(&db->aCollSeq); 2820 sqlite3HashInit(&db->aCollSeq);
2549 #ifndef SQLITE_OMIT_VIRTUALTABLE 2821 #ifndef SQLITE_OMIT_VIRTUALTABLE
2550 sqlite3HashInit(&db->aModule); 2822 sqlite3HashInit(&db->aModule);
2551 #endif 2823 #endif
2552 2824
2553 /* Add the default collation sequence BINARY. BINARY works for both UTF-8 2825 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
2554 ** and UTF-16, so add a version for each to avoid any unnecessary 2826 ** and UTF-16, so add a version for each to avoid any unnecessary
2555 ** conversions. The only error that can occur here is a malloc() failure. 2827 ** conversions. The only error that can occur here is a malloc() failure.
2828 **
2829 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
2830 ** functions:
2556 */ 2831 */
2557 createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); 2832 createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
2558 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); 2833 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
2559 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); 2834 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
2835 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
2560 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); 2836 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
2561 if( db->mallocFailed ){ 2837 if( db->mallocFailed ){
2562 goto opendb_out; 2838 goto opendb_out;
2563 } 2839 }
2564 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); 2840 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
2841 ** strings is BINARY.
2842 */
2843 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0);
2565 assert( db->pDfltColl!=0 ); 2844 assert( db->pDfltColl!=0 );
2566 2845
2567 /* Also add a UTF-8 case-insensitive collation sequence. */
2568 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
2569
2570 /* Parse the filename/URI argument. */ 2846 /* Parse the filename/URI argument. */
2571 db->openFlags = flags; 2847 db->openFlags = flags;
2572 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); 2848 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
2573 if( rc!=SQLITE_OK ){ 2849 if( rc!=SQLITE_OK ){
2574 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; 2850 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
2575 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg); 2851 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
2576 sqlite3_free(zErrMsg); 2852 sqlite3_free(zErrMsg);
2577 goto opendb_out; 2853 goto opendb_out;
2578 } 2854 }
2579 2855
2580 /* Open the backend database driver */ 2856 /* Open the backend database driver */
2581 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0, 2857 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
2582 flags | SQLITE_OPEN_MAIN_DB); 2858 flags | SQLITE_OPEN_MAIN_DB);
2583 if( rc!=SQLITE_OK ){ 2859 if( rc!=SQLITE_OK ){
2584 if( rc==SQLITE_IOERR_NOMEM ){ 2860 if( rc==SQLITE_IOERR_NOMEM ){
2585 rc = SQLITE_NOMEM; 2861 rc = SQLITE_NOMEM;
2586 } 2862 }
2587 sqlite3Error(db, rc); 2863 sqlite3Error(db, rc);
2588 goto opendb_out; 2864 goto opendb_out;
2589 } 2865 }
2590 sqlite3BtreeEnter(db->aDb[0].pBt); 2866 sqlite3BtreeEnter(db->aDb[0].pBt);
2591 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); 2867 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
2868 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
2592 sqlite3BtreeLeave(db->aDb[0].pBt); 2869 sqlite3BtreeLeave(db->aDb[0].pBt);
2593 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); 2870 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
2594 2871
2595 /* The default safety_level for the main database is 'full'; for the temp 2872 /* The default safety_level for the main database is 'full'; for the temp
2596 ** database it is 'NONE'. This matches the pager layer defaults. 2873 ** database it is 'NONE'. This matches the pager layer defaults.
2597 */ 2874 */
2598 db->aDb[0].zName = "main"; 2875 db->aDb[0].zName = "main";
2599 db->aDb[0].safety_level = 3; 2876 db->aDb[0].safety_level = 3;
2600 db->aDb[1].zName = "temp"; 2877 db->aDb[1].zName = "temp";
2601 db->aDb[1].safety_level = 1; 2878 db->aDb[1].safety_level = 1;
(...skipping 29 matching lines...) Expand all
2631 } 2908 }
2632 #endif 2909 #endif
2633 2910
2634 #ifdef SQLITE_ENABLE_FTS2 2911 #ifdef SQLITE_ENABLE_FTS2
2635 if( !db->mallocFailed && rc==SQLITE_OK ){ 2912 if( !db->mallocFailed && rc==SQLITE_OK ){
2636 extern int sqlite3Fts2Init(sqlite3*); 2913 extern int sqlite3Fts2Init(sqlite3*);
2637 rc = sqlite3Fts2Init(db); 2914 rc = sqlite3Fts2Init(db);
2638 } 2915 }
2639 #endif 2916 #endif
2640 2917
2641 #ifdef SQLITE_ENABLE_FTS3 2918 #ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */
2642 if( !db->mallocFailed && rc==SQLITE_OK ){ 2919 if( !db->mallocFailed && rc==SQLITE_OK ){
2643 rc = sqlite3Fts3Init(db); 2920 rc = sqlite3Fts3Init(db);
2644 } 2921 }
2645 #endif 2922 #endif
2646 2923
2647 #ifdef DEFAULT_ENABLE_RECOVER 2924 #ifdef SQLITE_ENABLE_FTS5
2648 /* Initialize recover virtual table for testing. */
2649 extern int recoverVtableInit(sqlite3 *db);
2650 if( !db->mallocFailed && rc==SQLITE_OK ){ 2925 if( !db->mallocFailed && rc==SQLITE_OK ){
2651 rc = recoverVtableInit(db); 2926 rc = sqlite3Fts5Init(db);
2652 } 2927 }
2653 #endif 2928 #endif
2654 2929
2655 #ifdef SQLITE_ENABLE_ICU 2930 #ifdef SQLITE_ENABLE_ICU
2656 if( !db->mallocFailed && rc==SQLITE_OK ){ 2931 if( !db->mallocFailed && rc==SQLITE_OK ){
2657 rc = sqlite3IcuInit(db); 2932 rc = sqlite3IcuInit(db);
2658 } 2933 }
2659 #endif 2934 #endif
2660 2935
2661 #ifdef SQLITE_ENABLE_RTREE 2936 #ifdef SQLITE_ENABLE_RTREE
2662 if( !db->mallocFailed && rc==SQLITE_OK){ 2937 if( !db->mallocFailed && rc==SQLITE_OK){
2663 rc = sqlite3RtreeInit(db); 2938 rc = sqlite3RtreeInit(db);
2664 } 2939 }
2665 #endif 2940 #endif
2666 2941
2942 #ifdef SQLITE_ENABLE_DBSTAT_VTAB
2943 if( !db->mallocFailed && rc==SQLITE_OK){
2944 rc = sqlite3DbstatRegister(db);
2945 }
2946 #endif
2947
2948 #ifdef SQLITE_ENABLE_JSON1
2949 if( !db->mallocFailed && rc==SQLITE_OK){
2950 rc = sqlite3Json1Init(db);
2951 }
2952 #endif
2953
2667 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking 2954 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
2668 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking 2955 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
2669 ** mode. Doing nothing at all also makes NORMAL the default. 2956 ** mode. Doing nothing at all also makes NORMAL the default.
2670 */ 2957 */
2671 #ifdef SQLITE_DEFAULT_LOCKING_MODE 2958 #ifdef SQLITE_DEFAULT_LOCKING_MODE
2672 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; 2959 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
2673 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), 2960 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
2674 SQLITE_DEFAULT_LOCKING_MODE); 2961 SQLITE_DEFAULT_LOCKING_MODE);
2675 #endif 2962 #endif
2676 2963
2677 if( rc ) sqlite3Error(db, rc); 2964 if( rc ) sqlite3Error(db, rc);
2678 2965
2679 /* Enable the lookaside-malloc subsystem */ 2966 /* Enable the lookaside-malloc subsystem */
2680 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, 2967 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
2681 sqlite3GlobalConfig.nLookaside); 2968 sqlite3GlobalConfig.nLookaside);
2682 2969
2683 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); 2970 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
2684 2971
2685 opendb_out: 2972 opendb_out:
2686 sqlite3_free(zOpen);
2687 if( db ){ 2973 if( db ){
2688 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); 2974 assert( db->mutex!=0 || isThreadsafe==0
2975 || sqlite3GlobalConfig.bFullMutex==0 );
2689 sqlite3_mutex_leave(db->mutex); 2976 sqlite3_mutex_leave(db->mutex);
2690 } 2977 }
2691 rc = sqlite3_errcode(db); 2978 rc = sqlite3_errcode(db);
2692 assert( db!=0 || rc==SQLITE_NOMEM ); 2979 assert( db!=0 || rc==SQLITE_NOMEM );
2693 if( rc==SQLITE_NOMEM ){ 2980 if( rc==SQLITE_NOMEM ){
2694 sqlite3_close(db); 2981 sqlite3_close(db);
2695 db = 0; 2982 db = 0;
2696 }else if( rc!=SQLITE_OK ){ 2983 }else if( rc!=SQLITE_OK ){
2697 db->magic = SQLITE_MAGIC_SICK; 2984 db->magic = SQLITE_MAGIC_SICK;
2698 } 2985 }
2699 *ppDb = db; 2986 *ppDb = db;
2700 #ifdef SQLITE_ENABLE_SQLLOG 2987 #ifdef SQLITE_ENABLE_SQLLOG
2701 if( sqlite3GlobalConfig.xSqllog ){ 2988 if( sqlite3GlobalConfig.xSqllog ){
2702 /* Opening a db handle. Fourth parameter is passed 0. */ 2989 /* Opening a db handle. Fourth parameter is passed 0. */
2703 void *pArg = sqlite3GlobalConfig.pSqllogArg; 2990 void *pArg = sqlite3GlobalConfig.pSqllogArg;
2704 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); 2991 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
2705 } 2992 }
2706 #endif 2993 #endif
2707 return sqlite3ApiExit(0, rc); 2994 #if defined(SQLITE_HAS_CODEC)
2995 if( rc==SQLITE_OK ){
2996 const char *zHexKey = sqlite3_uri_parameter(zOpen, "hexkey");
2997 if( zHexKey && zHexKey[0] ){
2998 u8 iByte;
2999 int i;
3000 char zKey[40];
3001 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zHexKey[i]); i++){
3002 iByte = (iByte<<4) + sqlite3HexToInt(zHexKey[i]);
3003 if( (i&1)!=0 ) zKey[i/2] = iByte;
3004 }
3005 sqlite3_key_v2(db, 0, zKey, i/2);
3006 }
3007 }
3008 #endif
3009 sqlite3_free(zOpen);
3010 return rc & 0xff;
2708 } 3011 }
2709 3012
2710 /* 3013 /*
2711 ** Open a new database handle. 3014 ** Open a new database handle.
2712 */ 3015 */
2713 int sqlite3_open( 3016 int sqlite3_open(
2714 const char *zFilename, 3017 const char *zFilename,
2715 sqlite3 **ppDb 3018 sqlite3 **ppDb
2716 ){ 3019 ){
2717 return openDatabase(zFilename, ppDb, 3020 return openDatabase(zFilename, ppDb,
(...skipping 13 matching lines...) Expand all
2731 ** Open a new database handle. 3034 ** Open a new database handle.
2732 */ 3035 */
2733 int sqlite3_open16( 3036 int sqlite3_open16(
2734 const void *zFilename, 3037 const void *zFilename,
2735 sqlite3 **ppDb 3038 sqlite3 **ppDb
2736 ){ 3039 ){
2737 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ 3040 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
2738 sqlite3_value *pVal; 3041 sqlite3_value *pVal;
2739 int rc; 3042 int rc;
2740 3043
2741 assert( zFilename ); 3044 #ifdef SQLITE_ENABLE_API_ARMOR
2742 assert( ppDb ); 3045 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
3046 #endif
2743 *ppDb = 0; 3047 *ppDb = 0;
2744 #ifndef SQLITE_OMIT_AUTOINIT 3048 #ifndef SQLITE_OMIT_AUTOINIT
2745 rc = sqlite3_initialize(); 3049 rc = sqlite3_initialize();
2746 if( rc ) return rc; 3050 if( rc ) return rc;
2747 #endif 3051 #endif
3052 if( zFilename==0 ) zFilename = "\000\000";
2748 pVal = sqlite3ValueNew(0); 3053 pVal = sqlite3ValueNew(0);
2749 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); 3054 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
2750 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); 3055 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
2751 if( zFilename8 ){ 3056 if( zFilename8 ){
2752 rc = openDatabase(zFilename8, ppDb, 3057 rc = openDatabase(zFilename8, ppDb,
2753 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); 3058 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
2754 assert( *ppDb || rc==SQLITE_NOMEM ); 3059 assert( *ppDb || rc==SQLITE_NOMEM );
2755 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ 3060 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
2756 ENC(*ppDb) = SQLITE_UTF16NATIVE; 3061 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
2757 } 3062 }
2758 }else{ 3063 }else{
2759 rc = SQLITE_NOMEM; 3064 rc = SQLITE_NOMEM;
2760 } 3065 }
2761 sqlite3ValueFree(pVal); 3066 sqlite3ValueFree(pVal);
2762 3067
2763 return sqlite3ApiExit(0, rc); 3068 return rc & 0xff;
2764 } 3069 }
2765 #endif /* SQLITE_OMIT_UTF16 */ 3070 #endif /* SQLITE_OMIT_UTF16 */
2766 3071
2767 /* 3072 /*
2768 ** Register a new collation sequence with the database handle db. 3073 ** Register a new collation sequence with the database handle db.
2769 */ 3074 */
2770 int sqlite3_create_collation( 3075 int sqlite3_create_collation(
2771 sqlite3* db, 3076 sqlite3* db,
2772 const char *zName, 3077 const char *zName,
2773 int enc, 3078 int enc,
2774 void* pCtx, 3079 void* pCtx,
2775 int(*xCompare)(void*,int,const void*,int,const void*) 3080 int(*xCompare)(void*,int,const void*,int,const void*)
2776 ){ 3081 ){
2777 int rc; 3082 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
2778 sqlite3_mutex_enter(db->mutex);
2779 assert( !db->mallocFailed );
2780 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
2781 rc = sqlite3ApiExit(db, rc);
2782 sqlite3_mutex_leave(db->mutex);
2783 return rc;
2784 } 3083 }
2785 3084
2786 /* 3085 /*
2787 ** Register a new collation sequence with the database handle db. 3086 ** Register a new collation sequence with the database handle db.
2788 */ 3087 */
2789 int sqlite3_create_collation_v2( 3088 int sqlite3_create_collation_v2(
2790 sqlite3* db, 3089 sqlite3* db,
2791 const char *zName, 3090 const char *zName,
2792 int enc, 3091 int enc,
2793 void* pCtx, 3092 void* pCtx,
2794 int(*xCompare)(void*,int,const void*,int,const void*), 3093 int(*xCompare)(void*,int,const void*,int,const void*),
2795 void(*xDel)(void*) 3094 void(*xDel)(void*)
2796 ){ 3095 ){
2797 int rc; 3096 int rc;
3097
3098 #ifdef SQLITE_ENABLE_API_ARMOR
3099 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
3100 #endif
2798 sqlite3_mutex_enter(db->mutex); 3101 sqlite3_mutex_enter(db->mutex);
2799 assert( !db->mallocFailed ); 3102 assert( !db->mallocFailed );
2800 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); 3103 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
2801 rc = sqlite3ApiExit(db, rc); 3104 rc = sqlite3ApiExit(db, rc);
2802 sqlite3_mutex_leave(db->mutex); 3105 sqlite3_mutex_leave(db->mutex);
2803 return rc; 3106 return rc;
2804 } 3107 }
2805 3108
2806 #ifndef SQLITE_OMIT_UTF16 3109 #ifndef SQLITE_OMIT_UTF16
2807 /* 3110 /*
2808 ** Register a new collation sequence with the database handle db. 3111 ** Register a new collation sequence with the database handle db.
2809 */ 3112 */
2810 int sqlite3_create_collation16( 3113 int sqlite3_create_collation16(
2811 sqlite3* db, 3114 sqlite3* db,
2812 const void *zName, 3115 const void *zName,
2813 int enc, 3116 int enc,
2814 void* pCtx, 3117 void* pCtx,
2815 int(*xCompare)(void*,int,const void*,int,const void*) 3118 int(*xCompare)(void*,int,const void*,int,const void*)
2816 ){ 3119 ){
2817 int rc = SQLITE_OK; 3120 int rc = SQLITE_OK;
2818 char *zName8; 3121 char *zName8;
3122
3123 #ifdef SQLITE_ENABLE_API_ARMOR
3124 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
3125 #endif
2819 sqlite3_mutex_enter(db->mutex); 3126 sqlite3_mutex_enter(db->mutex);
2820 assert( !db->mallocFailed ); 3127 assert( !db->mallocFailed );
2821 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); 3128 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
2822 if( zName8 ){ 3129 if( zName8 ){
2823 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); 3130 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
2824 sqlite3DbFree(db, zName8); 3131 sqlite3DbFree(db, zName8);
2825 } 3132 }
2826 rc = sqlite3ApiExit(db, rc); 3133 rc = sqlite3ApiExit(db, rc);
2827 sqlite3_mutex_leave(db->mutex); 3134 sqlite3_mutex_leave(db->mutex);
2828 return rc; 3135 return rc;
2829 } 3136 }
2830 #endif /* SQLITE_OMIT_UTF16 */ 3137 #endif /* SQLITE_OMIT_UTF16 */
2831 3138
2832 /* 3139 /*
2833 ** Register a collation sequence factory callback with the database handle 3140 ** Register a collation sequence factory callback with the database handle
2834 ** db. Replace any previously installed collation sequence factory. 3141 ** db. Replace any previously installed collation sequence factory.
2835 */ 3142 */
2836 int sqlite3_collation_needed( 3143 int sqlite3_collation_needed(
2837 sqlite3 *db, 3144 sqlite3 *db,
2838 void *pCollNeededArg, 3145 void *pCollNeededArg,
2839 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) 3146 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
2840 ){ 3147 ){
3148 #ifdef SQLITE_ENABLE_API_ARMOR
3149 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3150 #endif
2841 sqlite3_mutex_enter(db->mutex); 3151 sqlite3_mutex_enter(db->mutex);
2842 db->xCollNeeded = xCollNeeded; 3152 db->xCollNeeded = xCollNeeded;
2843 db->xCollNeeded16 = 0; 3153 db->xCollNeeded16 = 0;
2844 db->pCollNeededArg = pCollNeededArg; 3154 db->pCollNeededArg = pCollNeededArg;
2845 sqlite3_mutex_leave(db->mutex); 3155 sqlite3_mutex_leave(db->mutex);
2846 return SQLITE_OK; 3156 return SQLITE_OK;
2847 } 3157 }
2848 3158
2849 #ifndef SQLITE_OMIT_UTF16 3159 #ifndef SQLITE_OMIT_UTF16
2850 /* 3160 /*
2851 ** Register a collation sequence factory callback with the database handle 3161 ** Register a collation sequence factory callback with the database handle
2852 ** db. Replace any previously installed collation sequence factory. 3162 ** db. Replace any previously installed collation sequence factory.
2853 */ 3163 */
2854 int sqlite3_collation_needed16( 3164 int sqlite3_collation_needed16(
2855 sqlite3 *db, 3165 sqlite3 *db,
2856 void *pCollNeededArg, 3166 void *pCollNeededArg,
2857 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) 3167 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
2858 ){ 3168 ){
3169 #ifdef SQLITE_ENABLE_API_ARMOR
3170 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3171 #endif
2859 sqlite3_mutex_enter(db->mutex); 3172 sqlite3_mutex_enter(db->mutex);
2860 db->xCollNeeded = 0; 3173 db->xCollNeeded = 0;
2861 db->xCollNeeded16 = xCollNeeded16; 3174 db->xCollNeeded16 = xCollNeeded16;
2862 db->pCollNeededArg = pCollNeededArg; 3175 db->pCollNeededArg = pCollNeededArg;
2863 sqlite3_mutex_leave(db->mutex); 3176 sqlite3_mutex_leave(db->mutex);
2864 return SQLITE_OK; 3177 return SQLITE_OK;
2865 } 3178 }
2866 #endif /* SQLITE_OMIT_UTF16 */ 3179 #endif /* SQLITE_OMIT_UTF16 */
2867 3180
2868 #ifndef SQLITE_OMIT_DEPRECATED 3181 #ifndef SQLITE_OMIT_DEPRECATED
2869 /* 3182 /*
2870 ** This function is now an anachronism. It used to be used to recover from a 3183 ** This function is now an anachronism. It used to be used to recover from a
2871 ** malloc() failure, but SQLite now does this automatically. 3184 ** malloc() failure, but SQLite now does this automatically.
2872 */ 3185 */
2873 int sqlite3_global_recover(void){ 3186 int sqlite3_global_recover(void){
2874 return SQLITE_OK; 3187 return SQLITE_OK;
2875 } 3188 }
2876 #endif 3189 #endif
2877 3190
2878 /* 3191 /*
2879 ** Test to see whether or not the database connection is in autocommit 3192 ** Test to see whether or not the database connection is in autocommit
2880 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on 3193 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
2881 ** by default. Autocommit is disabled by a BEGIN statement and reenabled 3194 ** by default. Autocommit is disabled by a BEGIN statement and reenabled
2882 ** by the next COMMIT or ROLLBACK. 3195 ** by the next COMMIT or ROLLBACK.
2883 */ 3196 */
2884 int sqlite3_get_autocommit(sqlite3 *db){ 3197 int sqlite3_get_autocommit(sqlite3 *db){
3198 #ifdef SQLITE_ENABLE_API_ARMOR
3199 if( !sqlite3SafetyCheckOk(db) ){
3200 (void)SQLITE_MISUSE_BKPT;
3201 return 0;
3202 }
3203 #endif
2885 return db->autoCommit; 3204 return db->autoCommit;
2886 } 3205 }
2887 3206
2888 /* 3207 /*
2889 ** The following routines are substitutes for constants SQLITE_CORRUPT, 3208 ** The following routines are substitutes for constants SQLITE_CORRUPT,
2890 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error 3209 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
2891 ** constants. They serve two purposes: 3210 ** constants. They serve two purposes:
2892 ** 3211 **
2893 ** 1. Serve as a convenient place to set a breakpoint in a debugger 3212 ** 1. Serve as a convenient place to set a breakpoint in a debugger
2894 ** to detect when version error conditions occurs. 3213 ** to detect when version error conditions occurs.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2928 ** no-op. It is retained for historical compatibility. 3247 ** no-op. It is retained for historical compatibility.
2929 */ 3248 */
2930 void sqlite3_thread_cleanup(void){ 3249 void sqlite3_thread_cleanup(void){
2931 } 3250 }
2932 #endif 3251 #endif
2933 3252
2934 /* 3253 /*
2935 ** Return meta information about a specific column of a database table. 3254 ** Return meta information about a specific column of a database table.
2936 ** See comment in sqlite3.h (sqlite.h.in) for details. 3255 ** See comment in sqlite3.h (sqlite.h.in) for details.
2937 */ 3256 */
2938 #ifdef SQLITE_ENABLE_COLUMN_METADATA
2939 int sqlite3_table_column_metadata( 3257 int sqlite3_table_column_metadata(
2940 sqlite3 *db, /* Connection handle */ 3258 sqlite3 *db, /* Connection handle */
2941 const char *zDbName, /* Database name or NULL */ 3259 const char *zDbName, /* Database name or NULL */
2942 const char *zTableName, /* Table name */ 3260 const char *zTableName, /* Table name */
2943 const char *zColumnName, /* Column name */ 3261 const char *zColumnName, /* Column name */
2944 char const **pzDataType, /* OUTPUT: Declared data type */ 3262 char const **pzDataType, /* OUTPUT: Declared data type */
2945 char const **pzCollSeq, /* OUTPUT: Collation sequence name */ 3263 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
2946 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ 3264 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
2947 int *pPrimaryKey, /* OUTPUT: True if column part of PK */ 3265 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
2948 int *pAutoinc /* OUTPUT: True if column is auto-increment */ 3266 int *pAutoinc /* OUTPUT: True if column is auto-increment */
2949 ){ 3267 ){
2950 int rc; 3268 int rc;
2951 char *zErrMsg = 0; 3269 char *zErrMsg = 0;
2952 Table *pTab = 0; 3270 Table *pTab = 0;
2953 Column *pCol = 0; 3271 Column *pCol = 0;
2954 int iCol; 3272 int iCol = 0;
2955
2956 char const *zDataType = 0; 3273 char const *zDataType = 0;
2957 char const *zCollSeq = 0; 3274 char const *zCollSeq = 0;
2958 int notnull = 0; 3275 int notnull = 0;
2959 int primarykey = 0; 3276 int primarykey = 0;
2960 int autoinc = 0; 3277 int autoinc = 0;
2961 3278
3279
3280 #ifdef SQLITE_ENABLE_API_ARMOR
3281 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
3282 return SQLITE_MISUSE_BKPT;
3283 }
3284 #endif
3285
2962 /* Ensure the database schema has been loaded */ 3286 /* Ensure the database schema has been loaded */
2963 sqlite3_mutex_enter(db->mutex); 3287 sqlite3_mutex_enter(db->mutex);
2964 sqlite3BtreeEnterAll(db); 3288 sqlite3BtreeEnterAll(db);
2965 rc = sqlite3Init(db, &zErrMsg); 3289 rc = sqlite3Init(db, &zErrMsg);
2966 if( SQLITE_OK!=rc ){ 3290 if( SQLITE_OK!=rc ){
2967 goto error_out; 3291 goto error_out;
2968 } 3292 }
2969 3293
2970 /* Locate the table in question */ 3294 /* Locate the table in question */
2971 pTab = sqlite3FindTable(db, zTableName, zDbName); 3295 pTab = sqlite3FindTable(db, zTableName, zDbName);
2972 if( !pTab || pTab->pSelect ){ 3296 if( !pTab || pTab->pSelect ){
2973 pTab = 0; 3297 pTab = 0;
2974 goto error_out; 3298 goto error_out;
2975 } 3299 }
2976 3300
2977 /* Find the column for which info is requested */ 3301 /* Find the column for which info is requested */
2978 if( sqlite3IsRowid(zColumnName) ){ 3302 if( zColumnName==0 ){
2979 iCol = pTab->iPKey; 3303 /* Query for existance of table only */
2980 if( iCol>=0 ){
2981 pCol = &pTab->aCol[iCol];
2982 }
2983 }else{ 3304 }else{
2984 for(iCol=0; iCol<pTab->nCol; iCol++){ 3305 for(iCol=0; iCol<pTab->nCol; iCol++){
2985 pCol = &pTab->aCol[iCol]; 3306 pCol = &pTab->aCol[iCol];
2986 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ 3307 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
2987 break; 3308 break;
2988 } 3309 }
2989 } 3310 }
2990 if( iCol==pTab->nCol ){ 3311 if( iCol==pTab->nCol ){
2991 pTab = 0; 3312 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
2992 goto error_out; 3313 iCol = pTab->iPKey;
3314 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
3315 }else{
3316 pTab = 0;
3317 goto error_out;
3318 }
2993 } 3319 }
2994 } 3320 }
2995 3321
2996 /* The following block stores the meta information that will be returned 3322 /* The following block stores the meta information that will be returned
2997 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey 3323 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
2998 ** and autoinc. At this point there are two possibilities: 3324 ** and autoinc. At this point there are two possibilities:
2999 ** 3325 **
3000 ** 1. The specified column name was rowid", "oid" or "_rowid_" 3326 ** 1. The specified column name was rowid", "oid" or "_rowid_"
3001 ** and there is no explicitly declared IPK column. 3327 ** and there is no explicitly declared IPK column.
3002 ** 3328 **
3003 ** 2. The table is not a view and the column name identified an 3329 ** 2. The table is not a view and the column name identified an
3004 ** explicitly declared column. Copy meta information from *pCol. 3330 ** explicitly declared column. Copy meta information from *pCol.
3005 */ 3331 */
3006 if( pCol ){ 3332 if( pCol ){
3007 zDataType = pCol->zType; 3333 zDataType = pCol->zType;
3008 zCollSeq = pCol->zColl; 3334 zCollSeq = pCol->zColl;
3009 notnull = pCol->notNull!=0; 3335 notnull = pCol->notNull!=0;
3010 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0; 3336 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
3011 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; 3337 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
3012 }else{ 3338 }else{
3013 zDataType = "INTEGER"; 3339 zDataType = "INTEGER";
3014 primarykey = 1; 3340 primarykey = 1;
3015 } 3341 }
3016 if( !zCollSeq ){ 3342 if( !zCollSeq ){
3017 zCollSeq = "BINARY"; 3343 zCollSeq = sqlite3StrBINARY;
3018 } 3344 }
3019 3345
3020 error_out: 3346 error_out:
3021 sqlite3BtreeLeaveAll(db); 3347 sqlite3BtreeLeaveAll(db);
3022 3348
3023 /* Whether the function call succeeded or failed, set the output parameters 3349 /* Whether the function call succeeded or failed, set the output parameters
3024 ** to whatever their local counterparts contain. If an error did occur, 3350 ** to whatever their local counterparts contain. If an error did occur,
3025 ** this has the effect of zeroing all output parameters. 3351 ** this has the effect of zeroing all output parameters.
3026 */ 3352 */
3027 if( pzDataType ) *pzDataType = zDataType; 3353 if( pzDataType ) *pzDataType = zDataType;
3028 if( pzCollSeq ) *pzCollSeq = zCollSeq; 3354 if( pzCollSeq ) *pzCollSeq = zCollSeq;
3029 if( pNotNull ) *pNotNull = notnull; 3355 if( pNotNull ) *pNotNull = notnull;
3030 if( pPrimaryKey ) *pPrimaryKey = primarykey; 3356 if( pPrimaryKey ) *pPrimaryKey = primarykey;
3031 if( pAutoinc ) *pAutoinc = autoinc; 3357 if( pAutoinc ) *pAutoinc = autoinc;
3032 3358
3033 if( SQLITE_OK==rc && !pTab ){ 3359 if( SQLITE_OK==rc && !pTab ){
3034 sqlite3DbFree(db, zErrMsg); 3360 sqlite3DbFree(db, zErrMsg);
3035 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, 3361 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
3036 zColumnName); 3362 zColumnName);
3037 rc = SQLITE_ERROR; 3363 rc = SQLITE_ERROR;
3038 } 3364 }
3039 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg); 3365 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
3040 sqlite3DbFree(db, zErrMsg); 3366 sqlite3DbFree(db, zErrMsg);
3041 rc = sqlite3ApiExit(db, rc); 3367 rc = sqlite3ApiExit(db, rc);
3042 sqlite3_mutex_leave(db->mutex); 3368 sqlite3_mutex_leave(db->mutex);
3043 return rc; 3369 return rc;
3044 } 3370 }
3045 #endif
3046 3371
3047 /* 3372 /*
3048 ** Sleep for a little while. Return the amount of time slept. 3373 ** Sleep for a little while. Return the amount of time slept.
3049 */ 3374 */
3050 int sqlite3_sleep(int ms){ 3375 int sqlite3_sleep(int ms){
3051 sqlite3_vfs *pVfs; 3376 sqlite3_vfs *pVfs;
3052 int rc; 3377 int rc;
3053 pVfs = sqlite3_vfs_find(0); 3378 pVfs = sqlite3_vfs_find(0);
3054 if( pVfs==0 ) return 0; 3379 if( pVfs==0 ) return 0;
3055 3380
3056 /* This function works in milliseconds, but the underlying OsSleep() 3381 /* This function works in milliseconds, but the underlying OsSleep()
3057 ** API uses microseconds. Hence the 1000's. 3382 ** API uses microseconds. Hence the 1000's.
3058 */ 3383 */
3059 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); 3384 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
3060 return rc; 3385 return rc;
3061 } 3386 }
3062 3387
3063 /* 3388 /*
3064 ** Enable or disable the extended result codes. 3389 ** Enable or disable the extended result codes.
3065 */ 3390 */
3066 int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ 3391 int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
3392 #ifdef SQLITE_ENABLE_API_ARMOR
3393 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3394 #endif
3067 sqlite3_mutex_enter(db->mutex); 3395 sqlite3_mutex_enter(db->mutex);
3068 db->errMask = onoff ? 0xffffffff : 0xff; 3396 db->errMask = onoff ? 0xffffffff : 0xff;
3069 sqlite3_mutex_leave(db->mutex); 3397 sqlite3_mutex_leave(db->mutex);
3070 return SQLITE_OK; 3398 return SQLITE_OK;
3071 } 3399 }
3072 3400
3073 /* 3401 /*
3074 ** Invoke the xFileControl method on a particular database. 3402 ** Invoke the xFileControl method on a particular database.
3075 */ 3403 */
3076 int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ 3404 int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
3077 int rc = SQLITE_ERROR; 3405 int rc = SQLITE_ERROR;
3078 Btree *pBtree; 3406 Btree *pBtree;
3079 3407
3408 #ifdef SQLITE_ENABLE_API_ARMOR
3409 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3410 #endif
3080 sqlite3_mutex_enter(db->mutex); 3411 sqlite3_mutex_enter(db->mutex);
3081 pBtree = sqlite3DbNameToBtree(db, zDbName); 3412 pBtree = sqlite3DbNameToBtree(db, zDbName);
3082 if( pBtree ){ 3413 if( pBtree ){
3083 Pager *pPager; 3414 Pager *pPager;
3084 sqlite3_file *fd; 3415 sqlite3_file *fd;
3085 sqlite3BtreeEnter(pBtree); 3416 sqlite3BtreeEnter(pBtree);
3086 pPager = sqlite3BtreePager(pBtree); 3417 pPager = sqlite3BtreePager(pBtree);
3087 assert( pPager!=0 ); 3418 assert( pPager!=0 );
3088 fd = sqlite3PagerFile(pPager); 3419 fd = sqlite3PagerFile(pPager);
3089 assert( fd!=0 ); 3420 assert( fd!=0 );
3090 if( op==SQLITE_FCNTL_FILE_POINTER ){ 3421 if( op==SQLITE_FCNTL_FILE_POINTER ){
3091 *(sqlite3_file**)pArg = fd; 3422 *(sqlite3_file**)pArg = fd;
3092 rc = SQLITE_OK; 3423 rc = SQLITE_OK;
3424 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
3425 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
3426 rc = SQLITE_OK;
3427 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
3428 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
3429 rc = SQLITE_OK;
3093 }else if( fd->pMethods ){ 3430 }else if( fd->pMethods ){
3094 rc = sqlite3OsFileControl(fd, op, pArg); 3431 rc = sqlite3OsFileControl(fd, op, pArg);
3095 }else{ 3432 }else{
3096 rc = SQLITE_NOTFOUND; 3433 rc = SQLITE_NOTFOUND;
3097 } 3434 }
3098 sqlite3BtreeLeave(pBtree); 3435 sqlite3BtreeLeave(pBtree);
3099 } 3436 }
3100 sqlite3_mutex_leave(db->mutex); 3437 sqlite3_mutex_leave(db->mutex);
3101 return rc; 3438 return rc;
3102 } 3439 }
3103 3440
3104 /* 3441 /*
3105 ** Interface to the testing logic. 3442 ** Interface to the testing logic.
3106 */ 3443 */
3107 int sqlite3_test_control(int op, ...){ 3444 int sqlite3_test_control(int op, ...){
3108 int rc = 0; 3445 int rc = 0;
3109 #ifndef SQLITE_OMIT_BUILTIN_TEST 3446 #ifdef SQLITE_OMIT_BUILTIN_TEST
3447 UNUSED_PARAMETER(op);
3448 #else
3110 va_list ap; 3449 va_list ap;
3111 va_start(ap, op); 3450 va_start(ap, op);
3112 switch( op ){ 3451 switch( op ){
3113 3452
3114 /* 3453 /*
3115 ** Save the current state of the PRNG. 3454 ** Save the current state of the PRNG.
3116 */ 3455 */
3117 case SQLITE_TESTCTRL_PRNG_SAVE: { 3456 case SQLITE_TESTCTRL_PRNG_SAVE: {
3118 sqlite3PrngSaveState(); 3457 sqlite3PrngSaveState();
3119 break; 3458 break;
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
3394 3733
3395 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT); 3734 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
3396 ** 3735 **
3397 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if 3736 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
3398 ** not. 3737 ** not.
3399 */ 3738 */
3400 case SQLITE_TESTCTRL_ISINIT: { 3739 case SQLITE_TESTCTRL_ISINIT: {
3401 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR; 3740 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
3402 break; 3741 break;
3403 } 3742 }
3743
3744 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
3745 **
3746 ** This test control is used to create imposter tables. "db" is a pointer
3747 ** to the database connection. dbName is the database name (ex: "main" or
3748 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
3749 ** or off. "tnum" is the root page of the b-tree to which the imposter
3750 ** table should connect.
3751 **
3752 ** Enable imposter mode only when the schema has already been parsed. Then
3753 ** run a single CREATE TABLE statement to construct the imposter table in
3754 ** the parsed schema. Then turn imposter mode back off again.
3755 **
3756 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
3757 ** the schema to be reparsed the next time it is needed. This has the
3758 ** effect of erasing all imposter tables.
3759 */
3760 case SQLITE_TESTCTRL_IMPOSTER: {
3761 sqlite3 *db = va_arg(ap, sqlite3*);
3762 sqlite3_mutex_enter(db->mutex);
3763 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
3764 db->init.busy = db->init.imposterTable = va_arg(ap,int);
3765 db->init.newTnum = va_arg(ap,int);
3766 if( db->init.busy==0 && db->init.newTnum>0 ){
3767 sqlite3ResetAllSchemasOfConnection(db);
3768 }
3769 sqlite3_mutex_leave(db->mutex);
3770 break;
3771 }
3404 } 3772 }
3405 va_end(ap); 3773 va_end(ap);
3406 #endif /* SQLITE_OMIT_BUILTIN_TEST */ 3774 #endif /* SQLITE_OMIT_BUILTIN_TEST */
3407 return rc; 3775 return rc;
3408 } 3776 }
3409 3777
3410 /* 3778 /*
3411 ** This is a utility routine, useful to VFS implementations, that checks 3779 ** 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 3780 ** 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. 3781 ** parameter, and if so obtains the value of the query parameter.
3414 ** 3782 **
3415 ** The zFilename argument is the filename pointer passed into the xOpen() 3783 ** 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 3784 ** 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 3785 ** 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 3786 ** parameter if it exists. If the parameter does not exist, this routine
3419 ** returns a NULL pointer. 3787 ** returns a NULL pointer.
3420 */ 3788 */
3421 const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ 3789 const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
3422 if( zFilename==0 ) return 0; 3790 if( zFilename==0 || zParam==0 ) return 0;
3423 zFilename += sqlite3Strlen30(zFilename) + 1; 3791 zFilename += sqlite3Strlen30(zFilename) + 1;
3424 while( zFilename[0] ){ 3792 while( zFilename[0] ){
3425 int x = strcmp(zFilename, zParam); 3793 int x = strcmp(zFilename, zParam);
3426 zFilename += sqlite3Strlen30(zFilename) + 1; 3794 zFilename += sqlite3Strlen30(zFilename) + 1;
3427 if( x==0 ) return zFilename; 3795 if( x==0 ) return zFilename;
3428 zFilename += sqlite3Strlen30(zFilename) + 1; 3796 zFilename += sqlite3Strlen30(zFilename) + 1;
3429 } 3797 }
3430 return 0; 3798 return 0;
3431 } 3799 }
3432 3800
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3468 } 3836 }
3469 } 3837 }
3470 return 0; 3838 return 0;
3471 } 3839 }
3472 3840
3473 /* 3841 /*
3474 ** Return the filename of the database associated with a database 3842 ** Return the filename of the database associated with a database
3475 ** connection. 3843 ** connection.
3476 */ 3844 */
3477 const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ 3845 const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
3478 Btree *pBt = sqlite3DbNameToBtree(db, zDbName); 3846 Btree *pBt;
3847 #ifdef SQLITE_ENABLE_API_ARMOR
3848 if( !sqlite3SafetyCheckOk(db) ){
3849 (void)SQLITE_MISUSE_BKPT;
3850 return 0;
3851 }
3852 #endif
3853 pBt = sqlite3DbNameToBtree(db, zDbName);
3479 return pBt ? sqlite3BtreeGetFilename(pBt) : 0; 3854 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
3480 } 3855 }
3481 3856
3482 /* 3857 /*
3483 ** Return 1 if database is read-only or 0 if read/write. Return -1 if 3858 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
3484 ** no such database exists. 3859 ** no such database exists.
3485 */ 3860 */
3486 int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ 3861 int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
3487 Btree *pBt = sqlite3DbNameToBtree(db, zDbName); 3862 Btree *pBt;
3863 #ifdef SQLITE_ENABLE_API_ARMOR
3864 if( !sqlite3SafetyCheckOk(db) ){
3865 (void)SQLITE_MISUSE_BKPT;
3866 return -1;
3867 }
3868 #endif
3869 pBt = sqlite3DbNameToBtree(db, zDbName);
3488 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; 3870 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
3489 } 3871 }
3872
3873 #ifdef SQLITE_ENABLE_SNAPSHOT
3874 /*
3875 ** Obtain a snapshot handle for the snapshot of database zDb currently
3876 ** being read by handle db.
3877 */
3878 int sqlite3_snapshot_get(
3879 sqlite3 *db,
3880 const char *zDb,
3881 sqlite3_snapshot **ppSnapshot
3882 ){
3883 int rc = SQLITE_ERROR;
3884 #ifndef SQLITE_OMIT_WAL
3885 int iDb;
3886
3887 #ifdef SQLITE_ENABLE_API_ARMOR
3888 if( !sqlite3SafetyCheckOk(db) ){
3889 return SQLITE_MISUSE_BKPT;
3890 }
3891 #endif
3892 sqlite3_mutex_enter(db->mutex);
3893
3894 iDb = sqlite3FindDbName(db, zDb);
3895 if( iDb==0 || iDb>1 ){
3896 Btree *pBt = db->aDb[iDb].pBt;
3897 if( 0==sqlite3BtreeIsInTrans(pBt) ){
3898 rc = sqlite3BtreeBeginTrans(pBt, 0);
3899 if( rc==SQLITE_OK ){
3900 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
3901 }
3902 }
3903 }
3904
3905 sqlite3_mutex_leave(db->mutex);
3906 #endif /* SQLITE_OMIT_WAL */
3907 return rc;
3908 }
3909
3910 /*
3911 ** Open a read-transaction on the snapshot idendified by pSnapshot.
3912 */
3913 int sqlite3_snapshot_open(
3914 sqlite3 *db,
3915 const char *zDb,
3916 sqlite3_snapshot *pSnapshot
3917 ){
3918 int rc = SQLITE_ERROR;
3919 #ifndef SQLITE_OMIT_WAL
3920
3921 #ifdef SQLITE_ENABLE_API_ARMOR
3922 if( !sqlite3SafetyCheckOk(db) ){
3923 return SQLITE_MISUSE_BKPT;
3924 }
3925 #endif
3926 sqlite3_mutex_enter(db->mutex);
3927 if( db->autoCommit==0 ){
3928 int iDb;
3929 iDb = sqlite3FindDbName(db, zDb);
3930 if( iDb==0 || iDb>1 ){
3931 Btree *pBt = db->aDb[iDb].pBt;
3932 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){
3933 rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot);
3934 if( rc==SQLITE_OK ){
3935 rc = sqlite3BtreeBeginTrans(pBt, 0);
3936 sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0);
3937 }
3938 }
3939 }
3940 }
3941
3942 sqlite3_mutex_leave(db->mutex);
3943 #endif /* SQLITE_OMIT_WAL */
3944 return rc;
3945 }
3946
3947 /*
3948 ** Free a snapshot handle obtained from sqlite3_snapshot_get().
3949 */
3950 void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
3951 sqlite3_free(pSnapshot);
3952 }
3953 #endif /* SQLITE_ENABLE_SNAPSHOT */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/src/loadext.c ('k') | third_party/sqlite/sqlite-src-3100200/src/malloc.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698