175897234Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 375897234Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 675897234Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 1075897234Sdrh ** 1175897234Sdrh ************************************************************************* 1275897234Sdrh ** Main file for the SQLite library. The routines in this file 1375897234Sdrh ** implement the programmer interface to the library. Routines in 1475897234Sdrh ** other files are for internal use by SQLite and should not be 1575897234Sdrh ** accessed by users of the library. 1675897234Sdrh */ 1775897234Sdrh #include "sqliteInt.h" 1858f1c8b7Sdrh 19820a9069Sdrh #ifdef SQLITE_ENABLE_FTS3 20820a9069Sdrh # include "fts3.h" 21820a9069Sdrh #endif 2258f1c8b7Sdrh #ifdef SQLITE_ENABLE_RTREE 2358f1c8b7Sdrh # include "rtree.h" 2458f1c8b7Sdrh #endif 2521540ae4Sdan #if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS) 261c826650Sdanielk1977 # include "sqliteicu.h" 271c826650Sdanielk1977 #endif 28c306e08aSdrh #ifdef SQLITE_ENABLE_JSON1 29c306e08aSdrh int sqlite3Json1Init(sqlite3*); 30c306e08aSdrh #endif 31c6603af7Sdrh #ifdef SQLITE_ENABLE_STMTVTAB 32c6603af7Sdrh int sqlite3StmtVtabInit(sqlite3*); 33f00f530bSdrh #endif 34c306e08aSdrh #ifdef SQLITE_ENABLE_FTS5 35c306e08aSdrh int sqlite3Fts5Init(sqlite3*); 36c306e08aSdrh #endif 3775897234Sdrh 38b3190c15Sdrh #ifndef SQLITE_AMALGAMATION 399f129f46Sdrh /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant 409f129f46Sdrh ** contains the text of SQLITE_VERSION macro. 419f129f46Sdrh */ 4224b03fd0Sdanielk1977 const char sqlite3_version[] = SQLITE_VERSION; 43b3190c15Sdrh #endif 449f129f46Sdrh 459f129f46Sdrh /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns 469f129f46Sdrh ** a pointer to the to the sqlite3_version[] string constant. 479f129f46Sdrh */ 484aec8b65Sdrh const char *sqlite3_libversion(void){ return sqlite3_version; } 499f129f46Sdrh 50c6aa3815Sdrh /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a 519f129f46Sdrh ** pointer to a string constant whose value is the same as the 52c6aa3815Sdrh ** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using 53c6aa3815Sdrh ** an edited copy of the amalgamation, then the last four characters of 54c6aa3815Sdrh ** the hash might be different from SQLITE_SOURCE_ID. 559f129f46Sdrh */ 5647baebc2Sdrh const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } 579f129f46Sdrh 589f129f46Sdrh /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function 599f129f46Sdrh ** returns an integer equal to SQLITE_VERSION_NUMBER. 609f129f46Sdrh */ 6199ba19eaSdanielk1977 int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } 629f129f46Sdrh 635dc2bcdaSdrh /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns 64b8a45bbdSdrh ** zero if and only if SQLite was compiled with mutexing code omitted due to 659f129f46Sdrh ** the SQLITE_THREADSAFE compile-time option being set to 0. 669f129f46Sdrh */ 67b67e8bf0Sdrh int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } 68b217a57eSdrh 6934cf2583Smistachkin /* 7034cf2583Smistachkin ** When compiling the test fixture or with debugging enabled (on Win32), 7134cf2583Smistachkin ** this variable being set to non-zero will cause OSTRACE macros to emit 7234cf2583Smistachkin ** extra diagnostic information. 7334cf2583Smistachkin */ 74fb383e92Smistachkin #ifdef SQLITE_HAVE_OS_TRACE 7534cf2583Smistachkin # ifndef SQLITE_DEBUG_OS_TRACE 7634cf2583Smistachkin # define SQLITE_DEBUG_OS_TRACE 0 7734cf2583Smistachkin # endif 7834cf2583Smistachkin int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; 7934cf2583Smistachkin #endif 8034cf2583Smistachkin 81e265b084Sdrh #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) 82b217a57eSdrh /* 83b0603416Sdrh ** If the following function pointer is not NULL and if 84b0603416Sdrh ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing 85b0603416Sdrh ** I/O active are written using this function. These messages 86b0603416Sdrh ** are intended for debugging activity only. 87b0603416Sdrh */ 889871a933Smistachkin SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0; 89e265b084Sdrh #endif 90b0603416Sdrh 91b0603416Sdrh /* 92a16313e5Sdrh ** If the following global variable points to a string which is the 93a16313e5Sdrh ** name of a directory, then that directory will be used to store 94a16313e5Sdrh ** temporary files. 95a16313e5Sdrh ** 96a16313e5Sdrh ** See also the "PRAGMA temp_store_directory" SQL command. 97a16313e5Sdrh */ 98a16313e5Sdrh char *sqlite3_temp_directory = 0; 99a16313e5Sdrh 100c5499befSdrh /* 101a112d140Smistachkin ** If the following global variable points to a string which is the 102a112d140Smistachkin ** name of a directory, then that directory will be used to store 103a112d140Smistachkin ** all database files specified with a relative pathname. 104a112d140Smistachkin ** 105a112d140Smistachkin ** See also the "PRAGMA data_store_directory" SQL command. 106a112d140Smistachkin */ 107a112d140Smistachkin char *sqlite3_data_directory = 0; 108a112d140Smistachkin 109a112d140Smistachkin /* 11040257ffdSdrh ** Initialize SQLite. 11140257ffdSdrh ** 11240257ffdSdrh ** This routine must be called to initialize the memory allocation, 11393ed56d9Sdrh ** VFS, and mutex subsystems prior to doing any serious work with 11440257ffdSdrh ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT 11540257ffdSdrh ** this routine will be called automatically by key routines such as 11640257ffdSdrh ** sqlite3_open(). 11740257ffdSdrh ** 11840257ffdSdrh ** This routine is a no-op except on its very first call for the process, 11940257ffdSdrh ** or for the first call after a call to sqlite3_shutdown. 12093ed56d9Sdrh ** 12193ed56d9Sdrh ** The first thread to call this routine runs the initialization to 12293ed56d9Sdrh ** completion. If subsequent threads call this routine before the first 12393ed56d9Sdrh ** thread has finished the initialization process, then the subsequent 12493ed56d9Sdrh ** threads must block until the first thread finishes with the initialization. 12593ed56d9Sdrh ** 12693ed56d9Sdrh ** The first thread might call this routine recursively. Recursive 12793ed56d9Sdrh ** calls to this routine should not block, of course. Otherwise the 12893ed56d9Sdrh ** initialization process would never complete. 12993ed56d9Sdrh ** 13093ed56d9Sdrh ** Let X be the first thread to enter this routine. Let Y be some other 13193ed56d9Sdrh ** thread. Then while the initial invocation of this routine by X is 13293ed56d9Sdrh ** incomplete, it is required that: 13393ed56d9Sdrh ** 13493ed56d9Sdrh ** * Calls to this routine from Y must block until the outer-most 13593ed56d9Sdrh ** call by X completes. 13693ed56d9Sdrh ** 13793ed56d9Sdrh ** * Recursive calls to this routine from thread X return immediately 13893ed56d9Sdrh ** without blocking. 13940257ffdSdrh */ 14040257ffdSdrh int sqlite3_initialize(void){ 14130ddce6fSdrh MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ 14293ed56d9Sdrh int rc; /* Result code */ 143ab80be99Sdrh #ifdef SQLITE_EXTRA_INIT 144ab80be99Sdrh int bRunExtraInit = 0; /* Extra initialization needed */ 145ab80be99Sdrh #endif 14671bc31c6Sdanielk1977 147075c23afSdanielk1977 #ifdef SQLITE_OMIT_WSD 148a8f83bfcSdanielk1977 rc = sqlite3_wsd_init(4096, 24); 149075c23afSdanielk1977 if( rc!=SQLITE_OK ){ 150075c23afSdanielk1977 return rc; 151075c23afSdanielk1977 } 152075c23afSdanielk1977 #endif 153075c23afSdanielk1977 1542b4905c8Sdrh /* If the following assert() fails on some obscure processor/compiler 1552b4905c8Sdrh ** combination, the work-around is to set the correct pointer 1562b4905c8Sdrh ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */ 1572b4905c8Sdrh assert( SQLITE_PTRSIZE==sizeof(char*) ); 1582b4905c8Sdrh 15993ed56d9Sdrh /* If SQLite is already completely initialized, then this call 16093ed56d9Sdrh ** to sqlite3_initialize() should be a no-op. But the initialization 16193ed56d9Sdrh ** must be complete. So isInit must not be set until the very end 16293ed56d9Sdrh ** of this routine. 16393ed56d9Sdrh */ 164075c23afSdanielk1977 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; 16571bc31c6Sdanielk1977 16693ed56d9Sdrh /* Make sure the mutex subsystem is initialized. If unable to 16793ed56d9Sdrh ** initialize the mutex subsystem, return early with the error. 16893ed56d9Sdrh ** If the system is so sick that we are unable to allocate a mutex, 16993ed56d9Sdrh ** there is not much SQLite is going to be able to do. 17093ed56d9Sdrh ** 17193ed56d9Sdrh ** The mutex subsystem must take care of serializing its own 17293ed56d9Sdrh ** initialization. 17393ed56d9Sdrh */ 174d025174fSdanielk1977 rc = sqlite3MutexInit(); 17593ed56d9Sdrh if( rc ) return rc; 17671bc31c6Sdanielk1977 17771bc31c6Sdanielk1977 /* Initialize the malloc() system and the recursive pInitMutex mutex. 17893ed56d9Sdrh ** This operation is protected by the STATIC_MASTER mutex. Note that 17993ed56d9Sdrh ** MutexAlloc() is called for a static mutex prior to initializing the 18093ed56d9Sdrh ** malloc subsystem - this implies that the allocation of a static 18193ed56d9Sdrh ** mutex must not require support from the malloc subsystem. 18271bc31c6Sdanielk1977 */ 18330ddce6fSdrh MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) 18471bc31c6Sdanielk1977 sqlite3_mutex_enter(pMaster); 185e1ab2193Sdan sqlite3GlobalConfig.isMutexInit = 1; 186075c23afSdanielk1977 if( !sqlite3GlobalConfig.isMallocInit ){ 18771bc31c6Sdanielk1977 rc = sqlite3MallocInit(); 18871bc31c6Sdanielk1977 } 18971bc31c6Sdanielk1977 if( rc==SQLITE_OK ){ 190075c23afSdanielk1977 sqlite3GlobalConfig.isMallocInit = 1; 191075c23afSdanielk1977 if( !sqlite3GlobalConfig.pInitMutex ){ 1929ac06509Sdrh sqlite3GlobalConfig.pInitMutex = 1939ac06509Sdrh sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); 194075c23afSdanielk1977 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ 195fad3039cSmistachkin rc = SQLITE_NOMEM_BKPT; 19671bc31c6Sdanielk1977 } 19771bc31c6Sdanielk1977 } 19877eb5bb6Sdanielk1977 } 19977eb5bb6Sdanielk1977 if( rc==SQLITE_OK ){ 200075c23afSdanielk1977 sqlite3GlobalConfig.nRefInitMutex++; 20171bc31c6Sdanielk1977 } 20271bc31c6Sdanielk1977 sqlite3_mutex_leave(pMaster); 20393ed56d9Sdrh 204e1ab2193Sdan /* If rc is not SQLITE_OK at this point, then either the malloc 205e1ab2193Sdan ** subsystem could not be initialized or the system failed to allocate 206e1ab2193Sdan ** the pInitMutex mutex. Return an error in either case. */ 20740257ffdSdrh if( rc!=SQLITE_OK ){ 20871bc31c6Sdanielk1977 return rc; 20940257ffdSdrh } 21071bc31c6Sdanielk1977 21193ed56d9Sdrh /* Do the rest of the initialization under the recursive mutex so 21293ed56d9Sdrh ** that we will be able to handle recursive calls into 21393ed56d9Sdrh ** sqlite3_initialize(). The recursive calls normally come through 21493ed56d9Sdrh ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other 21593ed56d9Sdrh ** recursive calls might also be possible. 216f759bb83Sdrh ** 217f759bb83Sdrh ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls 218f759bb83Sdrh ** to the xInit method, so the xInit method need not be threadsafe. 219f759bb83Sdrh ** 220f759bb83Sdrh ** The following mutex is what serializes access to the appdef pcache xInit 221f759bb83Sdrh ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the 222f759bb83Sdrh ** call to sqlite3PcacheInitialize(). 22371bc31c6Sdanielk1977 */ 224075c23afSdanielk1977 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); 225502b4e00Sdanielk1977 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ 226502b4e00Sdanielk1977 sqlite3GlobalConfig.inProgress = 1; 2275e3cefe3Sdrh #ifdef SQLITE_ENABLE_SQLLOG 228d83f7ca1Sdan { 2295e3cefe3Sdrh extern void sqlite3_init_sqllog(void); 2305e3cefe3Sdrh sqlite3_init_sqllog(); 231d83f7ca1Sdan } 232d83f7ca1Sdan #endif 23380738d9cSdrh memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions)); 23480738d9cSdrh sqlite3RegisterBuiltinFunctions(); 235e1ab2193Sdan if( sqlite3GlobalConfig.isPCacheInit==0 ){ 2368c0a791aSdanielk1977 rc = sqlite3PcacheInitialize(); 237e1ab2193Sdan } 2380bf9f7bcSdrh if( rc==SQLITE_OK ){ 239e1ab2193Sdan sqlite3GlobalConfig.isPCacheInit = 1; 2403d6e060bSdan rc = sqlite3OsInit(); 2410bf9f7bcSdrh } 2429c6396ecSdrh #ifdef SQLITE_ENABLE_DESERIALIZE 243ac442f41Sdrh if( rc==SQLITE_OK ){ 244ac442f41Sdrh rc = sqlite3MemdbInit(); 245ac442f41Sdrh } 246ac442f41Sdrh #endif 2470bf9f7bcSdrh if( rc==SQLITE_OK ){ 2485b775295Sdanielk1977 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 2495b775295Sdanielk1977 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); 2500bf9f7bcSdrh sqlite3GlobalConfig.isInit = 1; 251ab80be99Sdrh #ifdef SQLITE_EXTRA_INIT 252ab80be99Sdrh bRunExtraInit = 1; 253ab80be99Sdrh #endif 2548c0a791aSdanielk1977 } 255502b4e00Sdanielk1977 sqlite3GlobalConfig.inProgress = 0; 25640257ffdSdrh } 257075c23afSdanielk1977 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); 258dfb7b37dSshane 25993ed56d9Sdrh /* Go back under the static mutex and clean up the recursive 26093ed56d9Sdrh ** mutex to prevent a resource leak. 26193ed56d9Sdrh */ 26293ed56d9Sdrh sqlite3_mutex_enter(pMaster); 263075c23afSdanielk1977 sqlite3GlobalConfig.nRefInitMutex--; 264075c23afSdanielk1977 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ 265075c23afSdanielk1977 assert( sqlite3GlobalConfig.nRefInitMutex==0 ); 266075c23afSdanielk1977 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); 267075c23afSdanielk1977 sqlite3GlobalConfig.pInitMutex = 0; 26893ed56d9Sdrh } 26993ed56d9Sdrh sqlite3_mutex_leave(pMaster); 27093ed56d9Sdrh 27193ed56d9Sdrh /* The following is just a sanity check to make sure SQLite has 27293ed56d9Sdrh ** been compiled correctly. It is important to run this code, but 27393ed56d9Sdrh ** we don't want to run it too often and soak up CPU cycles for no 27493ed56d9Sdrh ** reason. So we run it once during initialization. 27593ed56d9Sdrh */ 276dfb7b37dSshane #ifndef NDEBUG 277fbd60f82Sshane #ifndef SQLITE_OMIT_FLOATING_POINT 278dfb7b37dSshane /* This section of code's only "output" is via assert() statements. */ 279dfb7b37dSshane if( rc==SQLITE_OK ){ 280dfb7b37dSshane u64 x = (((u64)1)<<63)-1; 281dfb7b37dSshane double y; 282dfb7b37dSshane assert(sizeof(x)==8); 283dfb7b37dSshane assert(sizeof(x)==sizeof(y)); 284dfb7b37dSshane memcpy(&y, &x, 8); 285dfb7b37dSshane assert( sqlite3IsNaN(y) ); 286dfb7b37dSshane } 287dfb7b37dSshane #endif 288fbd60f82Sshane #endif 289dfb7b37dSshane 290e4cf0b31Sdrh /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT 291e4cf0b31Sdrh ** compile-time option. 292e4cf0b31Sdrh */ 293e4cf0b31Sdrh #ifdef SQLITE_EXTRA_INIT 294ab80be99Sdrh if( bRunExtraInit ){ 295c7f94629Sdrh int SQLITE_EXTRA_INIT(const char*); 296c7f94629Sdrh rc = SQLITE_EXTRA_INIT(0); 297e4cf0b31Sdrh } 298e4cf0b31Sdrh #endif 299e4cf0b31Sdrh 30040257ffdSdrh return rc; 30140257ffdSdrh } 30240257ffdSdrh 30340257ffdSdrh /* 30440257ffdSdrh ** Undo the effects of sqlite3_initialize(). Must not be called while 30540257ffdSdrh ** there are outstanding database connections or memory allocations or 30640257ffdSdrh ** while any part of SQLite is otherwise in use in any thread. This 307d1a2440dSdrh ** routine is not threadsafe. But it is safe to invoke this routine 308d1a2440dSdrh ** on when SQLite is already shut down. If SQLite is already shut down 309d1a2440dSdrh ** when this routine is invoked, then this routine is a harmless no-op. 31040257ffdSdrh */ 31140257ffdSdrh int sqlite3_shutdown(void){ 312054450f0Smistachkin #ifdef SQLITE_OMIT_WSD 313054450f0Smistachkin int rc = sqlite3_wsd_init(4096, 24); 314054450f0Smistachkin if( rc!=SQLITE_OK ){ 315054450f0Smistachkin return rc; 316054450f0Smistachkin } 317054450f0Smistachkin #endif 318054450f0Smistachkin 319d1a2440dSdrh if( sqlite3GlobalConfig.isInit ){ 3209797706cSdrh #ifdef SQLITE_EXTRA_SHUTDOWN 3219797706cSdrh void SQLITE_EXTRA_SHUTDOWN(void); 3229797706cSdrh SQLITE_EXTRA_SHUTDOWN(); 3239797706cSdrh #endif 32440257ffdSdrh sqlite3_os_end(); 325bb77b753Sdrh sqlite3_reset_auto_extension(); 326075c23afSdanielk1977 sqlite3GlobalConfig.isInit = 0; 327d1a2440dSdrh } 328e1ab2193Sdan if( sqlite3GlobalConfig.isPCacheInit ){ 329e1ab2193Sdan sqlite3PcacheShutdown(); 330e1ab2193Sdan sqlite3GlobalConfig.isPCacheInit = 0; 331e1ab2193Sdan } 332e1ab2193Sdan if( sqlite3GlobalConfig.isMallocInit ){ 333e1ab2193Sdan sqlite3MallocEnd(); 334e1ab2193Sdan sqlite3GlobalConfig.isMallocInit = 0; 33586f89871Smistachkin 33686f89871Smistachkin #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES 33786f89871Smistachkin /* The heap subsystem has now been shutdown and these values are supposed 33886f89871Smistachkin ** to be NULL or point to memory that was obtained from sqlite3_malloc(), 33986f89871Smistachkin ** which would rely on that heap subsystem; therefore, make sure these 34086f89871Smistachkin ** values cannot refer to heap memory that was just invalidated when the 34186f89871Smistachkin ** heap subsystem was shutdown. This is only done if the current call to 34286f89871Smistachkin ** this function resulted in the heap subsystem actually being shutdown. 34386f89871Smistachkin */ 34486f89871Smistachkin sqlite3_data_directory = 0; 34586f89871Smistachkin sqlite3_temp_directory = 0; 34686f89871Smistachkin #endif 347e1ab2193Sdan } 348e1ab2193Sdan if( sqlite3GlobalConfig.isMutexInit ){ 349e1ab2193Sdan sqlite3MutexEnd(); 350e1ab2193Sdan sqlite3GlobalConfig.isMutexInit = 0; 351e1ab2193Sdan } 352e1ab2193Sdan 35340257ffdSdrh return SQLITE_OK; 35440257ffdSdrh } 35540257ffdSdrh 35640257ffdSdrh /* 35740257ffdSdrh ** This API allows applications to modify the global configuration of 35840257ffdSdrh ** the SQLite library at run-time. 35940257ffdSdrh ** 36040257ffdSdrh ** This routine should only be called when there are no outstanding 36140257ffdSdrh ** database connections or memory allocations. This routine is not 36240257ffdSdrh ** threadsafe. Failure to heed these warnings can lead to unpredictable 36340257ffdSdrh ** behavior. 36440257ffdSdrh */ 36540257ffdSdrh int sqlite3_config(int op, ...){ 36640257ffdSdrh va_list ap; 36740257ffdSdrh int rc = SQLITE_OK; 36840257ffdSdrh 36940257ffdSdrh /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while 37040257ffdSdrh ** the SQLite library is in use. */ 371413c3d36Sdrh if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT; 37240257ffdSdrh 37340257ffdSdrh va_start(ap, op); 37440257ffdSdrh switch( op ){ 37518472fa7Sdrh 37618472fa7Sdrh /* Mutex configuration options are only available in a threadsafe 37718472fa7Sdrh ** compile. 37818472fa7Sdrh */ 379d1dcb234Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ 38040257ffdSdrh case SQLITE_CONFIG_SINGLETHREAD: { 381682a6ef6Sdrh /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to 382682a6ef6Sdrh ** Single-thread. */ 383682a6ef6Sdrh sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */ 384682a6ef6Sdrh sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ 38540257ffdSdrh break; 38640257ffdSdrh } 387d1dcb234Sdrh #endif 388d1dcb234Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ 38940257ffdSdrh case SQLITE_CONFIG_MULTITHREAD: { 390682a6ef6Sdrh /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to 391682a6ef6Sdrh ** Multi-thread. */ 392682a6ef6Sdrh sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ 393682a6ef6Sdrh sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ 39440257ffdSdrh break; 39540257ffdSdrh } 396d1dcb234Sdrh #endif 397d1dcb234Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ 39840257ffdSdrh case SQLITE_CONFIG_SERIALIZED: { 399682a6ef6Sdrh /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to 400682a6ef6Sdrh ** Serialized. */ 401682a6ef6Sdrh sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ 402682a6ef6Sdrh sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */ 40340257ffdSdrh break; 40440257ffdSdrh } 405d1dcb234Sdrh #endif 406d1dcb234Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ 40718472fa7Sdrh case SQLITE_CONFIG_MUTEX: { 40818472fa7Sdrh /* Specify an alternative mutex implementation */ 40918472fa7Sdrh sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); 41018472fa7Sdrh break; 41118472fa7Sdrh } 412d1dcb234Sdrh #endif 413d1dcb234Sdrh #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */ 41418472fa7Sdrh case SQLITE_CONFIG_GETMUTEX: { 41518472fa7Sdrh /* Retrieve the current mutex implementation */ 41618472fa7Sdrh *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; 41718472fa7Sdrh break; 41818472fa7Sdrh } 41918472fa7Sdrh #endif 42018472fa7Sdrh 42140257ffdSdrh case SQLITE_CONFIG_MALLOC: { 4225279d343Sdrh /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a 4235279d343Sdrh ** single argument which is a pointer to an instance of the 4245279d343Sdrh ** sqlite3_mem_methods structure. The argument specifies alternative 4255279d343Sdrh ** low-level memory allocation routines to be used in place of the memory 4265279d343Sdrh ** allocation routines built into SQLite. */ 427075c23afSdanielk1977 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); 42840257ffdSdrh break; 42940257ffdSdrh } 43033589797Sdrh case SQLITE_CONFIG_GETMALLOC: { 4315279d343Sdrh /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a 4325279d343Sdrh ** single argument which is a pointer to an instance of the 4335279d343Sdrh ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is 4345279d343Sdrh ** filled with the currently defined memory allocation routines. */ 435075c23afSdanielk1977 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); 436075c23afSdanielk1977 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; 43733589797Sdrh break; 43833589797Sdrh } 439fec00eabSdrh case SQLITE_CONFIG_MEMSTATUS: { 4405279d343Sdrh /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes 4415279d343Sdrh ** single argument of type int, interpreted as a boolean, which enables 4425279d343Sdrh ** or disables the collection of memory allocation statistics. */ 4435279d343Sdrh sqlite3GlobalConfig.bMemstat = va_arg(ap, int); 44440257ffdSdrh break; 44540257ffdSdrh } 446b2a0f75cSdrh case SQLITE_CONFIG_SMALL_MALLOC: { 447b2a0f75cSdrh sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int); 44833589797Sdrh break; 44933589797Sdrh } 45033589797Sdrh case SQLITE_CONFIG_PAGECACHE: { 45115427279Sdrh /* EVIDENCE-OF: R-18761-36601 There are three arguments to 45215427279Sdrh ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem), 45315427279Sdrh ** the size of each page cache line (sz), and the number of cache lines 45415427279Sdrh ** (N). */ 455075c23afSdanielk1977 sqlite3GlobalConfig.pPage = va_arg(ap, void*); 456075c23afSdanielk1977 sqlite3GlobalConfig.szPage = va_arg(ap, int); 457075c23afSdanielk1977 sqlite3GlobalConfig.nPage = va_arg(ap, int); 45833589797Sdrh break; 45933589797Sdrh } 460def6889dSdrh case SQLITE_CONFIG_PCACHE_HDRSZ: { 4615279d343Sdrh /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes 4625279d343Sdrh ** a single parameter which is a pointer to an integer and writes into 4635279d343Sdrh ** that integer the number of extra bytes per page required for each page 4645279d343Sdrh ** in SQLITE_CONFIG_PAGECACHE. */ 465def6889dSdrh *va_arg(ap, int*) = 466def6889dSdrh sqlite3HeaderSizeBtree() + 467def6889dSdrh sqlite3HeaderSizePcache() + 468def6889dSdrh sqlite3HeaderSizePcache1(); 469def6889dSdrh break; 470def6889dSdrh } 4715099be5eSdanielk1977 472bc2ca9ebSdanielk1977 case SQLITE_CONFIG_PCACHE: { 47322e21ff4Sdan /* no-op */ 47422e21ff4Sdan break; 47522e21ff4Sdan } 47622e21ff4Sdan case SQLITE_CONFIG_GETPCACHE: { 47722e21ff4Sdan /* now an error */ 47822e21ff4Sdan rc = SQLITE_ERROR; 479bc2ca9ebSdanielk1977 break; 480bc2ca9ebSdanielk1977 } 481bc2ca9ebSdanielk1977 48222e21ff4Sdan case SQLITE_CONFIG_PCACHE2: { 4835279d343Sdrh /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a 4845279d343Sdrh ** single argument which is a pointer to an sqlite3_pcache_methods2 4855279d343Sdrh ** object. This object specifies the interface to a custom page cache 4865279d343Sdrh ** implementation. */ 48722e21ff4Sdan sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); 48822e21ff4Sdan break; 48922e21ff4Sdan } 49022e21ff4Sdan case SQLITE_CONFIG_GETPCACHE2: { 4915279d343Sdrh /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a 4925279d343Sdrh ** single argument which is a pointer to an sqlite3_pcache_methods2 4935279d343Sdrh ** object. SQLite copies of the current page cache implementation into 4945279d343Sdrh ** that object. */ 49522e21ff4Sdan if( sqlite3GlobalConfig.pcache2.xInit==0 ){ 496b232c232Sdrh sqlite3PCacheSetDefault(); 497b232c232Sdrh } 49822e21ff4Sdan *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; 499b232c232Sdrh break; 500b232c232Sdrh } 501b232c232Sdrh 5028790b6e8Sdrh /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only 5038790b6e8Sdrh ** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or 5048790b6e8Sdrh ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ 5055d414839Sdrh #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 50633589797Sdrh case SQLITE_CONFIG_HEAP: { 5075279d343Sdrh /* EVIDENCE-OF: R-19854-42126 There are three arguments to 5085279d343Sdrh ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the 5093ba689d8Sdrh ** number of bytes in the memory buffer, and the minimum allocation size. 5103ba689d8Sdrh */ 511075c23afSdanielk1977 sqlite3GlobalConfig.pHeap = va_arg(ap, void*); 512075c23afSdanielk1977 sqlite3GlobalConfig.nHeap = va_arg(ap, int); 513075c23afSdanielk1977 sqlite3GlobalConfig.mnReq = va_arg(ap, int); 5145099be5eSdanielk1977 515a6ec892bSshaneh if( sqlite3GlobalConfig.mnReq<1 ){ 516a6ec892bSshaneh sqlite3GlobalConfig.mnReq = 1; 517a6ec892bSshaneh }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ 518a6ec892bSshaneh /* cap min request size at 2^12 */ 519a6ec892bSshaneh sqlite3GlobalConfig.mnReq = (1<<12); 520a6ec892bSshaneh } 521a6ec892bSshaneh 522075c23afSdanielk1977 if( sqlite3GlobalConfig.pHeap==0 ){ 5238790b6e8Sdrh /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer) 5248790b6e8Sdrh ** is NULL, then SQLite reverts to using its default memory allocator 5258790b6e8Sdrh ** (the system malloc() implementation), undoing any prior invocation of 5268790b6e8Sdrh ** SQLITE_CONFIG_MALLOC. 5278790b6e8Sdrh ** 5288790b6e8Sdrh ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to 5298790b6e8Sdrh ** revert to its default implementation when sqlite3_initialize() is run 5308a42cbd3Sdrh */ 531075c23afSdanielk1977 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); 5328a42cbd3Sdrh }else{ 5338790b6e8Sdrh /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the 5348790b6e8Sdrh ** alternative memory allocator is engaged to handle all of SQLites 5358790b6e8Sdrh ** memory allocation needs. */ 5365099be5eSdanielk1977 #ifdef SQLITE_ENABLE_MEMSYS3 537075c23afSdanielk1977 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); 5388a42cbd3Sdrh #endif 5398a42cbd3Sdrh #ifdef SQLITE_ENABLE_MEMSYS5 540075c23afSdanielk1977 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); 5418a42cbd3Sdrh #endif 5428a42cbd3Sdrh } 5435099be5eSdanielk1977 break; 5445099be5eSdanielk1977 } 5455d414839Sdrh #endif 5465099be5eSdanielk1977 547633e6d57Sdrh case SQLITE_CONFIG_LOOKASIDE: { 548075c23afSdanielk1977 sqlite3GlobalConfig.szLookaside = va_arg(ap, int); 549075c23afSdanielk1977 sqlite3GlobalConfig.nLookaside = va_arg(ap, int); 550633e6d57Sdrh break; 551633e6d57Sdrh } 5523f280701Sdrh 553037933b8Smistachkin /* Record a pointer to the logger function and its first argument. 5543f280701Sdrh ** The default is NULL. Logging is disabled if the function pointer is 5553f280701Sdrh ** NULL. 5563f280701Sdrh */ 5573f280701Sdrh case SQLITE_CONFIG_LOG: { 558dc97a8cdSshaneh /* MSVC is picky about pulling func ptrs from va lists. 559dc97a8cdSshaneh ** http://support.microsoft.com/kb/47961 560dc97a8cdSshaneh ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); 561dc97a8cdSshaneh */ 562dc97a8cdSshaneh typedef void(*LOGFUNC_t)(void*,int,const char*); 563dc97a8cdSshaneh sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); 5643f280701Sdrh sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); 5653f280701Sdrh break; 5663f280701Sdrh } 567633e6d57Sdrh 56800729cbaSdrh /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames 56900729cbaSdrh ** can be changed at start-time using the 57000729cbaSdrh ** sqlite3_config(SQLITE_CONFIG_URI,1) or 57100729cbaSdrh ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. 57200729cbaSdrh */ 573cd74b611Sdan case SQLITE_CONFIG_URI: { 5744d9f188fSdrh /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single 5754d9f188fSdrh ** argument of type int. If non-zero, then URI handling is globally 5764d9f188fSdrh ** enabled. If the parameter is zero, then URI handling is globally 5774d9f188fSdrh ** disabled. */ 578cd74b611Sdan sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); 579cd74b611Sdan break; 580cd74b611Sdan } 581cd74b611Sdan 582de9a7b8aSdrh case SQLITE_CONFIG_COVERING_INDEX_SCAN: { 5834d9f188fSdrh /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN 5844d9f188fSdrh ** option takes a single integer argument which is interpreted as a 5854d9f188fSdrh ** boolean in order to enable or disable the use of covering indices for 5864d9f188fSdrh ** full table scans in the query optimizer. */ 587de9a7b8aSdrh sqlite3GlobalConfig.bUseCis = va_arg(ap, int); 588de9a7b8aSdrh break; 589de9a7b8aSdrh } 590de9a7b8aSdrh 591ac455939Sdan #ifdef SQLITE_ENABLE_SQLLOG 592ac455939Sdan case SQLITE_CONFIG_SQLLOG: { 593ac455939Sdan typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int); 594ac455939Sdan sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t); 595ac455939Sdan sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *); 596ac455939Sdan break; 597ac455939Sdan } 598ac455939Sdan #endif 599ac455939Sdan 6009b4c59faSdrh case SQLITE_CONFIG_MMAP_SIZE: { 6014d9f188fSdrh /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit 6024d9f188fSdrh ** integer (sqlite3_int64) values that are the default mmap size limit 6034d9f188fSdrh ** (the default setting for PRAGMA mmap_size) and the maximum allowed 6044d9f188fSdrh ** mmap size limit. */ 6059b4c59faSdrh sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); 606a1f42c7cSdrh sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); 6074d9f188fSdrh /* EVIDENCE-OF: R-53367-43190 If either argument to this option is 6088790b6e8Sdrh ** negative, then that argument is changed to its compile-time default. 6098790b6e8Sdrh ** 6108790b6e8Sdrh ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be 6118790b6e8Sdrh ** silently truncated if necessary so that it does not exceed the 6128790b6e8Sdrh ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE 6138790b6e8Sdrh ** compile-time option. 6148790b6e8Sdrh */ 6153ba689d8Sdrh if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ 6163ba689d8Sdrh mxMmap = SQLITE_MAX_MMAP_SIZE; 6173ba689d8Sdrh } 6189b4c59faSdrh if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; 6199b4c59faSdrh if( szMmap>mxMmap) szMmap = mxMmap; 6204d9f188fSdrh sqlite3GlobalConfig.mxMmap = mxMmap; 6219b4c59faSdrh sqlite3GlobalConfig.szMmap = szMmap; 622a1f42c7cSdrh break; 623a1f42c7cSdrh } 624a1f42c7cSdrh 6254d9f188fSdrh #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */ 626af8641bdSmistachkin case SQLITE_CONFIG_WIN32_HEAPSIZE: { 6274d9f188fSdrh /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit 6284d9f188fSdrh ** unsigned integer value that specifies the maximum size of the created 6294d9f188fSdrh ** heap. */ 630ac1f1045Smistachkin sqlite3GlobalConfig.nHeap = va_arg(ap, int); 631ac1f1045Smistachkin break; 632ac1f1045Smistachkin } 633ac1f1045Smistachkin #endif 634ac1f1045Smistachkin 6353bd1791dSdrh case SQLITE_CONFIG_PMASZ: { 6363bd1791dSdrh sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int); 6373bd1791dSdrh break; 6383bd1791dSdrh } 6393bd1791dSdrh 6408c71a98cSdrh case SQLITE_CONFIG_STMTJRNL_SPILL: { 6418c71a98cSdrh sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int); 6428c71a98cSdrh break; 6438c71a98cSdrh } 6448c71a98cSdrh 6452e3a5a81Sdan #ifdef SQLITE_ENABLE_SORTER_REFERENCES 646bbade8d1Sdrh case SQLITE_CONFIG_SORTERREF_SIZE: { 6472e3a5a81Sdan int iVal = va_arg(ap, int); 6482e3a5a81Sdan if( iVal<0 ){ 6492e3a5a81Sdan iVal = SQLITE_DEFAULT_SORTERREF_SIZE; 6502e3a5a81Sdan } 6512e3a5a81Sdan sqlite3GlobalConfig.szSorterRef = (u32)iVal; 6522e3a5a81Sdan break; 6532e3a5a81Sdan } 654bbade8d1Sdrh #endif /* SQLITE_ENABLE_SORTER_REFERENCES */ 6552e3a5a81Sdan 65640257ffdSdrh default: { 65740257ffdSdrh rc = SQLITE_ERROR; 65840257ffdSdrh break; 65940257ffdSdrh } 66040257ffdSdrh } 66140257ffdSdrh va_end(ap); 66240257ffdSdrh return rc; 66340257ffdSdrh } 66440257ffdSdrh 66540257ffdSdrh /* 666633e6d57Sdrh ** Set up the lookaside buffers for a database connection. 667633e6d57Sdrh ** Return SQLITE_OK on success. 668633e6d57Sdrh ** If lookaside is already active, return SQLITE_BUSY. 669e9d1c720Sdrh ** 670e9d1c720Sdrh ** The sz parameter is the number of bytes in each lookaside slot. 671e9d1c720Sdrh ** The cnt parameter is the number of slots. If pStart is NULL the 672e9d1c720Sdrh ** space for the lookaside memory is obtained from sqlite3_malloc(). 673e9d1c720Sdrh ** If pStart is not NULL then it is sz*cnt bytes of memory to use for 674e9d1c720Sdrh ** the lookaside memory. 675633e6d57Sdrh */ 676e9d1c720Sdrh static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ 6777877d9afSdrh #ifndef SQLITE_OMIT_LOOKASIDE 678633e6d57Sdrh void *pStart; 67952fb8e19Sdrh 68052fb8e19Sdrh if( sqlite3LookasideUsed(db,0)>0 ){ 681633e6d57Sdrh return SQLITE_BUSY; 682633e6d57Sdrh } 683f71f89e8Sshane /* Free any existing lookaside buffer for this handle before 684f71f89e8Sshane ** allocating a new one so we don't have to have space for 685f71f89e8Sshane ** both at the same time. 686f71f89e8Sshane */ 687f71f89e8Sshane if( db->lookaside.bMalloced ){ 688f71f89e8Sshane sqlite3_free(db->lookaside.pStart); 689f71f89e8Sshane } 69061a4bd5cSdrh /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger 69161a4bd5cSdrh ** than a pointer to be useful. 692f71f89e8Sshane */ 69361a4bd5cSdrh sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ 6941d34fdecSdrh if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; 695633e6d57Sdrh if( cnt<0 ) cnt = 0; 696f71f89e8Sshane if( sz==0 || cnt==0 ){ 697f71f89e8Sshane sz = 0; 698f71f89e8Sshane pStart = 0; 699f71f89e8Sshane }else if( pBuf==0 ){ 700633e6d57Sdrh sqlite3BeginBenignMalloc(); 7019dd55f51Sdrh pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ 702633e6d57Sdrh sqlite3EndBenignMalloc(); 7038225d66bSdrh if( pStart ) cnt = sqlite3MallocSize(pStart)/sz; 704e9d1c720Sdrh }else{ 705e9d1c720Sdrh pStart = pBuf; 706e9d1c720Sdrh } 7074cfb22f7Sdrh db->lookaside.pStart = pStart; 70852fb8e19Sdrh db->lookaside.pInit = 0; 7094cfb22f7Sdrh db->lookaside.pFree = 0; 7101bd10f8aSdrh db->lookaside.sz = (u16)sz; 711633e6d57Sdrh if( pStart ){ 712633e6d57Sdrh int i; 713633e6d57Sdrh LookasideSlot *p; 714de467985Sdrh assert( sz > (int)sizeof(LookasideSlot*) ); 71552fb8e19Sdrh db->lookaside.nSlot = cnt; 716633e6d57Sdrh p = (LookasideSlot*)pStart; 717633e6d57Sdrh for(i=cnt-1; i>=0; i--){ 71852fb8e19Sdrh p->pNext = db->lookaside.pInit; 71952fb8e19Sdrh db->lookaside.pInit = p; 720633e6d57Sdrh p = (LookasideSlot*)&((u8*)p)[sz]; 721633e6d57Sdrh } 722633e6d57Sdrh db->lookaside.pEnd = p; 7234a642b60Sdrh db->lookaside.bDisable = 0; 724f71f89e8Sshane db->lookaside.bMalloced = pBuf==0 ?1:0; 7254cfb22f7Sdrh }else{ 726b0e7704eSdrh db->lookaside.pStart = db; 727b0e7704eSdrh db->lookaside.pEnd = db; 7284a642b60Sdrh db->lookaside.bDisable = 1; 729f71f89e8Sshane db->lookaside.bMalloced = 0; 73052fb8e19Sdrh db->lookaside.nSlot = 0; 731633e6d57Sdrh } 7327877d9afSdrh #endif /* SQLITE_OMIT_LOOKASIDE */ 733633e6d57Sdrh return SQLITE_OK; 734633e6d57Sdrh } 735633e6d57Sdrh 736633e6d57Sdrh /* 7374413d0e9Sdrh ** Return the mutex associated with a database connection. 7384413d0e9Sdrh */ 7394413d0e9Sdrh sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ 7409ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 7419ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 7429ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 7439ca95730Sdrh return 0; 7449ca95730Sdrh } 7459ca95730Sdrh #endif 7464413d0e9Sdrh return db->mutex; 7474413d0e9Sdrh } 7484413d0e9Sdrh 7494413d0e9Sdrh /* 75009419b4bSdrh ** Free up as much memory as we can from the given database 75109419b4bSdrh ** connection. 75209419b4bSdrh */ 75309419b4bSdrh int sqlite3_db_release_memory(sqlite3 *db){ 75409419b4bSdrh int i; 7559ca95730Sdrh 7569ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 7579ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 7589ca95730Sdrh #endif 759d9bb3a97Sdan sqlite3_mutex_enter(db->mutex); 76009419b4bSdrh sqlite3BtreeEnterAll(db); 76109419b4bSdrh for(i=0; i<db->nDb; i++){ 76209419b4bSdrh Btree *pBt = db->aDb[i].pBt; 76309419b4bSdrh if( pBt ){ 76409419b4bSdrh Pager *pPager = sqlite3BtreePager(pBt); 76509419b4bSdrh sqlite3PagerShrink(pPager); 76609419b4bSdrh } 76709419b4bSdrh } 76809419b4bSdrh sqlite3BtreeLeaveAll(db); 769d9bb3a97Sdan sqlite3_mutex_leave(db->mutex); 77009419b4bSdrh return SQLITE_OK; 77109419b4bSdrh } 77209419b4bSdrh 77309419b4bSdrh /* 7746fa255fdSdan ** Flush any dirty pages in the pager-cache for any attached database 7756fa255fdSdan ** to disk. 7766fa255fdSdan */ 7776fa255fdSdan int sqlite3_db_cacheflush(sqlite3 *db){ 7786fa255fdSdan int i; 7796fa255fdSdan int rc = SQLITE_OK; 7806fa255fdSdan int bSeenBusy = 0; 7816fa255fdSdan 7826fa255fdSdan #ifdef SQLITE_ENABLE_API_ARMOR 7836fa255fdSdan if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 7846fa255fdSdan #endif 7856fa255fdSdan sqlite3_mutex_enter(db->mutex); 7866fa255fdSdan sqlite3BtreeEnterAll(db); 7876fa255fdSdan for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 7886fa255fdSdan Btree *pBt = db->aDb[i].pBt; 7896fa255fdSdan if( pBt && sqlite3BtreeIsInTrans(pBt) ){ 7906fa255fdSdan Pager *pPager = sqlite3BtreePager(pBt); 7916fa255fdSdan rc = sqlite3PagerFlush(pPager); 7926fa255fdSdan if( rc==SQLITE_BUSY ){ 7936fa255fdSdan bSeenBusy = 1; 7946fa255fdSdan rc = SQLITE_OK; 7956fa255fdSdan } 7966fa255fdSdan } 7976fa255fdSdan } 7986fa255fdSdan sqlite3BtreeLeaveAll(db); 7996fa255fdSdan sqlite3_mutex_leave(db->mutex); 8006fa255fdSdan return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc); 8016fa255fdSdan } 8026fa255fdSdan 8036fa255fdSdan /* 804633e6d57Sdrh ** Configuration settings for an individual database connection 805633e6d57Sdrh */ 806633e6d57Sdrh int sqlite3_db_config(sqlite3 *db, int op, ...){ 807633e6d57Sdrh va_list ap; 8086480aad4Sdrh int rc; 809633e6d57Sdrh va_start(ap, op); 810633e6d57Sdrh switch( op ){ 811da84dcaeSdrh case SQLITE_DBCONFIG_MAINDBNAME: { 812749e4a9fSdrh /* IMP: R-06824-28531 */ 813749e4a9fSdrh /* IMP: R-36257-52125 */ 814da84dcaeSdrh db->aDb[0].zDbSName = va_arg(ap,char*); 815da84dcaeSdrh rc = SQLITE_OK; 816da84dcaeSdrh break; 817da84dcaeSdrh } 818e9d1c720Sdrh case SQLITE_DBCONFIG_LOOKASIDE: { 8198b2b2e67Sdrh void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ 8209dd55f51Sdrh int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ 8219dd55f51Sdrh int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */ 822e9d1c720Sdrh rc = setupLookaside(db, pBuf, sz, cnt); 823633e6d57Sdrh break; 824633e6d57Sdrh } 8256480aad4Sdrh default: { 826e83cafd2Sdrh static const struct { 827e83cafd2Sdrh int op; /* The opcode */ 828e83cafd2Sdrh u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */ 829e83cafd2Sdrh } aFlagOp[] = { 830e83cafd2Sdrh { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys }, 831e83cafd2Sdrh { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger }, 832d42908fbSdrh { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer }, 833191dd061Sdrh { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension }, 834298af023Sdan { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose }, 835169dd928Sdrh { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG }, 83636e31c69Sdrh { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP }, 8377df01196Sdrh { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase }, 838e83cafd2Sdrh }; 83958ad580fSdrh unsigned int i; 8409dd55f51Sdrh rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ 841e83cafd2Sdrh for(i=0; i<ArraySize(aFlagOp); i++){ 842e83cafd2Sdrh if( aFlagOp[i].op==op ){ 843e83cafd2Sdrh int onoff = va_arg(ap, int); 844e83cafd2Sdrh int *pRes = va_arg(ap, int*); 845f6285fabSmistachkin u32 oldFlags = db->flags; 846e83cafd2Sdrh if( onoff>0 ){ 847e83cafd2Sdrh db->flags |= aFlagOp[i].mask; 848e83cafd2Sdrh }else if( onoff==0 ){ 849e83cafd2Sdrh db->flags &= ~aFlagOp[i].mask; 850e83cafd2Sdrh } 851e83cafd2Sdrh if( oldFlags!=db->flags ){ 852ba968dbfSdrh sqlite3ExpirePreparedStatements(db, 0); 853e83cafd2Sdrh } 854e83cafd2Sdrh if( pRes ){ 855e83cafd2Sdrh *pRes = (db->flags & aFlagOp[i].mask)!=0; 856e83cafd2Sdrh } 857e83cafd2Sdrh rc = SQLITE_OK; 858e83cafd2Sdrh break; 859e83cafd2Sdrh } 860e83cafd2Sdrh } 8616480aad4Sdrh break; 8626480aad4Sdrh } 863633e6d57Sdrh } 864633e6d57Sdrh va_end(ap); 865633e6d57Sdrh return rc; 866633e6d57Sdrh } 867633e6d57Sdrh 868a16313e5Sdrh 869a16313e5Sdrh /* 8709b5adfa2Sdrh ** Return true if the buffer z[0..n-1] contains all spaces. 8719b5adfa2Sdrh */ 8729b5adfa2Sdrh static int allSpaces(const char *z, int n){ 8735f3a367bSdrh while( n>0 && z[n-1]==' ' ){ n--; } 8749b5adfa2Sdrh return n==0; 8759b5adfa2Sdrh } 8769b5adfa2Sdrh 8779b5adfa2Sdrh /* 878d3d39e93Sdrh ** This is the default collating function named "BINARY" which is always 879d3d39e93Sdrh ** available. 8809b5adfa2Sdrh ** 8819b5adfa2Sdrh ** If the padFlag argument is not NULL then space padding at the end 8829b5adfa2Sdrh ** of strings is ignored. This implements the RTRIM collation. 883d3d39e93Sdrh */ 8842c336549Sdanielk1977 static int binCollFunc( 8859b5adfa2Sdrh void *padFlag, 886d3d39e93Sdrh int nKey1, const void *pKey1, 887d3d39e93Sdrh int nKey2, const void *pKey2 888d3d39e93Sdrh ){ 889d3d39e93Sdrh int rc, n; 890d3d39e93Sdrh n = nKey1<nKey2 ? nKey1 : nKey2; 8915e3b49bcSdrh /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares 8925e3b49bcSdrh ** strings byte by byte using the memcmp() function from the standard C 8935e3b49bcSdrh ** library. */ 89421766c0cSdan assert( pKey1 && pKey2 ); 895d3d39e93Sdrh rc = memcmp(pKey1, pKey2, n); 896d3d39e93Sdrh if( rc==0 ){ 8979b5adfa2Sdrh if( padFlag 8989b5adfa2Sdrh && allSpaces(((char*)pKey1)+n, nKey1-n) 8999b5adfa2Sdrh && allSpaces(((char*)pKey2)+n, nKey2-n) 9009b5adfa2Sdrh ){ 9015e3b49bcSdrh /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra 9025e3b49bcSdrh ** spaces at the end of either string do not change the result. In other 9035e3b49bcSdrh ** words, strings will compare equal to one another as long as they 9045e3b49bcSdrh ** differ only in the number of spaces at the end. 9055e3b49bcSdrh */ 9069b5adfa2Sdrh }else{ 907d3d39e93Sdrh rc = nKey1 - nKey2; 908d3d39e93Sdrh } 9099b5adfa2Sdrh } 910d3d39e93Sdrh return rc; 911d3d39e93Sdrh } 912d3d39e93Sdrh 913d3d39e93Sdrh /* 914bcd15938Sdrh ** Return true if CollSeq is the default built-in BINARY. 915bcd15938Sdrh */ 916bcd15938Sdrh int sqlite3IsBinary(const CollSeq *p){ 917bcd15938Sdrh assert( p==0 || p->xCmp!=binCollFunc || p->pUser!=0 918bcd15938Sdrh || strcmp(p->zName,"BINARY")==0 ); 919bcd15938Sdrh return p==0 || (p->xCmp==binCollFunc && p->pUser==0); 920bcd15938Sdrh } 921bcd15938Sdrh 922bcd15938Sdrh /* 923dc1bdc4fSdanielk1977 ** Another built-in collating sequence: NOCASE. 924dc1bdc4fSdanielk1977 ** 925f7b5496eSdrh ** This collating sequence is intended to be used for "case independent 926dc1bdc4fSdanielk1977 ** comparison". SQLite's knowledge of upper and lower case equivalents 927dc1bdc4fSdanielk1977 ** extends only to the 26 characters used in the English language. 928dc1bdc4fSdanielk1977 ** 929dc1bdc4fSdanielk1977 ** At the moment there is only a UTF-8 implementation. 9300202b29eSdanielk1977 */ 9310202b29eSdanielk1977 static int nocaseCollatingFunc( 9320202b29eSdanielk1977 void *NotUsed, 9330202b29eSdanielk1977 int nKey1, const void *pKey1, 9340202b29eSdanielk1977 int nKey2, const void *pKey2 9350202b29eSdanielk1977 ){ 9360202b29eSdanielk1977 int r = sqlite3StrNICmp( 937208f80a7Sdrh (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); 93862c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 9390202b29eSdanielk1977 if( 0==r ){ 9400202b29eSdanielk1977 r = nKey1-nKey2; 9410202b29eSdanielk1977 } 9420202b29eSdanielk1977 return r; 9430202b29eSdanielk1977 } 9440202b29eSdanielk1977 9450202b29eSdanielk1977 /* 946af9ff33aSdrh ** Return the ROWID of the most recent insert 947af9ff33aSdrh */ 9489bb575fdSdrh sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ 9499ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9509ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 9519ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9529ca95730Sdrh return 0; 9539ca95730Sdrh } 9549ca95730Sdrh #endif 955af9ff33aSdrh return db->lastRowid; 956af9ff33aSdrh } 957af9ff33aSdrh 958af9ff33aSdrh /* 9599c58b63cSdan ** Set the value returned by the sqlite3_last_insert_rowid() API function. 9609c58b63cSdan */ 9619c58b63cSdan void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){ 9629c58b63cSdan #ifdef SQLITE_ENABLE_API_ARMOR 9639c58b63cSdan if( !sqlite3SafetyCheckOk(db) ){ 9649c58b63cSdan (void)SQLITE_MISUSE_BKPT; 9659c58b63cSdan return; 9669c58b63cSdan } 9679c58b63cSdan #endif 9689c58b63cSdan sqlite3_mutex_enter(db->mutex); 9699c58b63cSdan db->lastRowid = iRowid; 9709c58b63cSdan sqlite3_mutex_leave(db->mutex); 9719c58b63cSdan } 9729c58b63cSdan 9739c58b63cSdan /* 97424b03fd0Sdanielk1977 ** Return the number of changes in the most recent call to sqlite3_exec(). 975c8d30ac1Sdrh */ 9769bb575fdSdrh int sqlite3_changes(sqlite3 *db){ 9779ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9789ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 9799ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9809ca95730Sdrh return 0; 9819ca95730Sdrh } 9829ca95730Sdrh #endif 983c8d30ac1Sdrh return db->nChange; 984c8d30ac1Sdrh } 985c8d30ac1Sdrh 986f146a776Srdc /* 987b28af71aSdanielk1977 ** Return the number of changes since the database handle was opened. 988f146a776Srdc */ 989b28af71aSdanielk1977 int sqlite3_total_changes(sqlite3 *db){ 9909ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 9919ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 9929ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 9939ca95730Sdrh return 0; 9949ca95730Sdrh } 9959ca95730Sdrh #endif 996b28af71aSdanielk1977 return db->nTotalChange; 997b0c374ffSrdc } 998b0c374ffSrdc 999c8d30ac1Sdrh /* 1000fd7f0452Sdanielk1977 ** Close all open savepoints. This function only manipulates fields of the 1001fd7f0452Sdanielk1977 ** database handle object, it does not close any savepoints that may be open 1002fd7f0452Sdanielk1977 ** at the b-tree/pager level. 1003fd7f0452Sdanielk1977 */ 1004fd7f0452Sdanielk1977 void sqlite3CloseSavepoints(sqlite3 *db){ 1005fd7f0452Sdanielk1977 while( db->pSavepoint ){ 1006fd7f0452Sdanielk1977 Savepoint *pTmp = db->pSavepoint; 1007fd7f0452Sdanielk1977 db->pSavepoint = pTmp->pNext; 1008fd7f0452Sdanielk1977 sqlite3DbFree(db, pTmp); 1009fd7f0452Sdanielk1977 } 1010fd7f0452Sdanielk1977 db->nSavepoint = 0; 1011bd43455cSdanielk1977 db->nStatement = 0; 1012fd7f0452Sdanielk1977 db->isTransactionSavepoint = 0; 1013fd7f0452Sdanielk1977 } 1014fd7f0452Sdanielk1977 1015fd7f0452Sdanielk1977 /* 1016d2199f0fSdan ** Invoke the destructor function associated with FuncDef p, if any. Except, 1017d2199f0fSdan ** if this is not the last copy of the function, do not invoke it. Multiple 1018d2199f0fSdan ** copies of a single function are created when create_function() is called 1019d2199f0fSdan ** with SQLITE_ANY as the encoding. 1020d2199f0fSdan */ 1021d2199f0fSdan static void functionDestroy(sqlite3 *db, FuncDef *p){ 102280738d9cSdrh FuncDestructor *pDestructor = p->u.pDestructor; 1023d2199f0fSdan if( pDestructor ){ 1024d2199f0fSdan pDestructor->nRef--; 1025d2199f0fSdan if( pDestructor->nRef==0 ){ 1026d2199f0fSdan pDestructor->xDestroy(pDestructor->pUserData); 1027d2199f0fSdan sqlite3DbFree(db, pDestructor); 1028d2199f0fSdan } 1029d2199f0fSdan } 1030d2199f0fSdan } 1031d2199f0fSdan 1032d2199f0fSdan /* 1033bba02a95Sdan ** Disconnect all sqlite3_vtab objects that belong to database connection 1034bba02a95Sdan ** db. This is called when db is being closed. 1035bba02a95Sdan */ 1036bba02a95Sdan static void disconnectAllVtab(sqlite3 *db){ 1037bba02a95Sdan #ifndef SQLITE_OMIT_VIRTUALTABLE 1038bba02a95Sdan int i; 103951be3873Sdrh HashElem *p; 1040bba02a95Sdan sqlite3BtreeEnterAll(db); 1041bba02a95Sdan for(i=0; i<db->nDb; i++){ 1042bba02a95Sdan Schema *pSchema = db->aDb[i].pSchema; 1043bba02a95Sdan if( db->aDb[i].pSchema ){ 1044bba02a95Sdan for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ 1045bba02a95Sdan Table *pTab = (Table *)sqliteHashData(p); 1046bba02a95Sdan if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab); 1047bba02a95Sdan } 1048bba02a95Sdan } 1049bba02a95Sdan } 105051be3873Sdrh for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){ 105151be3873Sdrh Module *pMod = (Module *)sqliteHashData(p); 105251be3873Sdrh if( pMod->pEpoTab ){ 105351be3873Sdrh sqlite3VtabDisconnect(db, pMod->pEpoTab); 105451be3873Sdrh } 105551be3873Sdrh } 1056d88e521fSdan sqlite3VtabUnlockList(db); 1057bba02a95Sdan sqlite3BtreeLeaveAll(db); 1058bba02a95Sdan #else 1059bba02a95Sdan UNUSED_PARAMETER(db); 1060bba02a95Sdan #endif 1061bba02a95Sdan } 1062bba02a95Sdan 1063bba02a95Sdan /* 1064167cd6abSdrh ** Return TRUE if database connection db has unfinalized prepared 1065167cd6abSdrh ** statements or unfinished sqlite3_backup objects. 1066167cd6abSdrh */ 1067167cd6abSdrh static int connectionIsBusy(sqlite3 *db){ 1068167cd6abSdrh int j; 1069167cd6abSdrh assert( sqlite3_mutex_held(db->mutex) ); 1070167cd6abSdrh if( db->pVdbe ) return 1; 1071167cd6abSdrh for(j=0; j<db->nDb; j++){ 1072167cd6abSdrh Btree *pBt = db->aDb[j].pBt; 1073167cd6abSdrh if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1; 1074167cd6abSdrh } 1075167cd6abSdrh return 0; 1076167cd6abSdrh } 1077167cd6abSdrh 1078167cd6abSdrh /* 107950e5dadfSdrh ** Close an existing SQLite database 108050e5dadfSdrh */ 1081167cd6abSdrh static int sqlite3Close(sqlite3 *db, int forceZombie){ 10825c4c7787Sdanielk1977 if( !db ){ 1083ddb17caeSdrh /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or 1084ddb17caeSdrh ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */ 108596d81f99Sdanielk1977 return SQLITE_OK; 10865c4c7787Sdanielk1977 } 10877e8b848aSdrh if( !sqlite3SafetyCheckSickOrOk(db) ){ 1088413c3d36Sdrh return SQLITE_MISUSE_BKPT; 1089e35ee196Sdanielk1977 } 1090605264d2Sdrh sqlite3_mutex_enter(db->mutex); 10913d2a529dSdrh if( db->mTrace & SQLITE_TRACE_CLOSE ){ 10923d2a529dSdrh db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0); 10933d2a529dSdrh } 1094e35ee196Sdanielk1977 1095bba02a95Sdan /* Force xDisconnect calls on all virtual tables */ 1096bba02a95Sdan disconnectAllVtab(db); 1097a04a34ffSdanielk1977 1098bba02a95Sdan /* If a transaction is open, the disconnectAllVtab() call above 109901256832Sdanielk1977 ** will not have called the xDisconnect() method on any virtual 110001256832Sdanielk1977 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() 110101256832Sdanielk1977 ** call will do so. We need to do this before the check for active 110201256832Sdanielk1977 ** SQL statements below, as the v-table implementation may be storing 110301256832Sdanielk1977 ** some prepared statements internally. 110401256832Sdanielk1977 */ 110567430817Sdan sqlite3VtabRollback(db); 110601256832Sdanielk1977 1107167cd6abSdrh /* Legacy behavior (sqlite3_close() behavior) is to return 1108167cd6abSdrh ** SQLITE_BUSY if the connection can not be closed immediately. 1109167cd6abSdrh */ 1110167cd6abSdrh if( !forceZombie && connectionIsBusy(db) ){ 111113f40da3Sdrh sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized " 1112167cd6abSdrh "statements or unfinished backups"); 111327641703Sdrh sqlite3_mutex_leave(db->mutex); 111496d81f99Sdanielk1977 return SQLITE_BUSY; 111596d81f99Sdanielk1977 } 1116e0048400Sdanielk1977 1117ac455939Sdan #ifdef SQLITE_ENABLE_SQLLOG 1118ac455939Sdan if( sqlite3GlobalConfig.xSqllog ){ 111971ba10d3Sdan /* Closing the handle. Fourth parameter is passed the value 2. */ 112071ba10d3Sdan sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); 1121ac455939Sdan } 1122ac455939Sdan #endif 1123ac455939Sdan 1124167cd6abSdrh /* Convert the connection into a zombie and then close it. 11254245c405Sdrh */ 11264245c405Sdrh db->magic = SQLITE_MAGIC_ZOMBIE; 11274245c405Sdrh sqlite3LeaveMutexAndCloseZombie(db); 11284245c405Sdrh return SQLITE_OK; 112994e92032Sdrh } 113094e92032Sdrh 1131167cd6abSdrh /* 1132167cd6abSdrh ** Two variations on the public interface for closing a database 1133167cd6abSdrh ** connection. The sqlite3_close() version returns SQLITE_BUSY and 1134167cd6abSdrh ** leaves the connection option if there are unfinalized prepared 1135167cd6abSdrh ** statements or unfinished sqlite3_backups. The sqlite3_close_v2() 1136167cd6abSdrh ** version forces the connection to become a zombie if there are 1137167cd6abSdrh ** unclosed resources, and arranges for deallocation when the last 1138167cd6abSdrh ** prepare statement or sqlite3_backup closes. 1139167cd6abSdrh */ 1140167cd6abSdrh int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); } 1141167cd6abSdrh int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); } 1142167cd6abSdrh 11434245c405Sdrh 11444245c405Sdrh /* 11454245c405Sdrh ** Close the mutex on database connection db. 11464245c405Sdrh ** 11474245c405Sdrh ** Furthermore, if database connection db is a zombie (meaning that there 1148167cd6abSdrh ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and 1149167cd6abSdrh ** every sqlite3_stmt has now been finalized and every sqlite3_backup has 1150167cd6abSdrh ** finished, then free all resources. 11514245c405Sdrh */ 11524245c405Sdrh void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ 11534245c405Sdrh HashElem *i; /* Hash table iterator */ 11544245c405Sdrh int j; 11554245c405Sdrh 11564245c405Sdrh /* If there are outstanding sqlite3_stmt or sqlite3_backup objects 1157167cd6abSdrh ** or if the connection has not yet been closed by sqlite3_close_v2(), 1158167cd6abSdrh ** then just leave the mutex and return. 11594245c405Sdrh */ 1160167cd6abSdrh if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){ 11610410302eSdanielk1977 sqlite3_mutex_leave(db->mutex); 11624245c405Sdrh return; 11630410302eSdanielk1977 } 11640410302eSdanielk1977 11654245c405Sdrh /* If we reach this point, it means that the database connection has 11664245c405Sdrh ** closed all sqlite3_stmt and sqlite3_backup objects and has been 116748864df9Smistachkin ** passed to sqlite3_close (meaning that it is a zombie). Therefore, 11684245c405Sdrh ** go ahead and free all resources. 11694245c405Sdrh */ 1170311019beSdanielk1977 1171617dc860Sdan /* If a transaction is open, roll it back. This also ensures that if 1172617dc860Sdan ** any database schemas have been modified by an uncommitted transaction 1173617dc860Sdan ** they are reset. And that the required b-tree mutex is held to make 1174617dc860Sdan ** the pager rollback and schema reset an atomic operation. */ 1175617dc860Sdan sqlite3RollbackAll(db, SQLITE_OK); 1176617dc860Sdan 117770a8ca3cSdrh /* Free any outstanding Savepoint structures. */ 117870a8ca3cSdrh sqlite3CloseSavepoints(db); 117970a8ca3cSdrh 11805efb3145Sdrh /* Close all database connections */ 1181001bbcbbSdrh for(j=0; j<db->nDb; j++){ 11824d189ca4Sdrh struct Db *pDb = &db->aDb[j]; 11834d189ca4Sdrh if( pDb->pBt ){ 1184ebdb81ddSdrh sqlite3BtreeClose(pDb->pBt); 1185ebdb81ddSdrh pDb->pBt = 0; 1186ebdb81ddSdrh if( j!=1 ){ 1187311019beSdanielk1977 pDb->pSchema = 0; 1188311019beSdanielk1977 } 1189113088ecSdrh } 1190001bbcbbSdrh } 11915efb3145Sdrh /* Clear the TEMP schema separately and last */ 11925efb3145Sdrh if( db->aDb[1].pSchema ){ 11935efb3145Sdrh sqlite3SchemaClear(db->aDb[1].pSchema); 11945efb3145Sdrh } 11955efb3145Sdrh sqlite3VtabUnlockList(db); 1196bba02a95Sdan 11975efb3145Sdrh /* Free up the array of auxiliary databases */ 11985efb3145Sdrh sqlite3CollapseDatabaseArray(db); 1199bba02a95Sdan assert( db->nDb<=2 ); 1200bba02a95Sdan assert( db->aDb==db->aDbStatic ); 1201404ca075Sdanielk1977 1202404ca075Sdanielk1977 /* Tell the code in notify.c that the connection no longer holds any 1203404ca075Sdanielk1977 ** locks and does not require any further unlock-notify callbacks. 1204404ca075Sdanielk1977 */ 1205404ca075Sdanielk1977 sqlite3ConnectionClosed(db); 1206404ca075Sdanielk1977 120780738d9cSdrh for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ 120880738d9cSdrh FuncDef *pNext, *p; 120980738d9cSdrh p = sqliteHashData(i); 121080738d9cSdrh do{ 1211d2199f0fSdan functionDestroy(db, p); 121270a8ca3cSdrh pNext = p->pNext; 121370a8ca3cSdrh sqlite3DbFree(db, p); 121470a8ca3cSdrh p = pNext; 121580738d9cSdrh }while( p ); 12168e0a2f90Sdrh } 121780738d9cSdrh sqlite3HashClear(&db->aFunc); 1218d8123366Sdanielk1977 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ 1219466be56bSdanielk1977 CollSeq *pColl = (CollSeq *)sqliteHashData(i); 1220a9808b31Sdanielk1977 /* Invoke any destructors registered for collation sequence user data. */ 1221a9808b31Sdanielk1977 for(j=0; j<3; j++){ 1222a9808b31Sdanielk1977 if( pColl[j].xDel ){ 1223a9808b31Sdanielk1977 pColl[j].xDel(pColl[j].pUser); 1224a9808b31Sdanielk1977 } 1225a9808b31Sdanielk1977 } 1226633e6d57Sdrh sqlite3DbFree(db, pColl); 1227466be56bSdanielk1977 } 1228d8123366Sdanielk1977 sqlite3HashClear(&db->aCollSeq); 1229b9bb7c18Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 1230d1ab1ba5Sdanielk1977 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ 1231d1ab1ba5Sdanielk1977 Module *pMod = (Module *)sqliteHashData(i); 1232832a58a6Sdanielk1977 if( pMod->xDestroy ){ 1233832a58a6Sdanielk1977 pMod->xDestroy(pMod->pAux); 1234832a58a6Sdanielk1977 } 123551be3873Sdrh sqlite3VtabEponymousTableClear(db, pMod); 1236633e6d57Sdrh sqlite3DbFree(db, pMod); 1237d1ab1ba5Sdanielk1977 } 1238b9bb7c18Sdrh sqlite3HashClear(&db->aModule); 1239b9bb7c18Sdrh #endif 1240466be56bSdanielk1977 124113f40da3Sdrh sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */ 1242bfd6cce5Sdanielk1977 sqlite3ValueFree(db->pErr); 1243f1952c5dSdrh sqlite3CloseExtensions(db); 1244d4530979Sdrh #if SQLITE_USER_AUTHENTICATION 1245d4530979Sdrh sqlite3_free(db->auth.zAuthUser); 1246d4530979Sdrh sqlite3_free(db->auth.zAuthPW); 1247d4530979Sdrh #endif 124896d81f99Sdanielk1977 124996d81f99Sdanielk1977 db->magic = SQLITE_MAGIC_ERROR; 1250311019beSdanielk1977 1251311019beSdanielk1977 /* The temp-database schema is allocated differently from the other schema 1252311019beSdanielk1977 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). 1253311019beSdanielk1977 ** So it needs to be freed here. Todo: Why not roll the temp schema into 1254311019beSdanielk1977 ** the same sqliteMalloc() as the one that allocates the database 1255311019beSdanielk1977 ** structure? 1256311019beSdanielk1977 */ 1257633e6d57Sdrh sqlite3DbFree(db, db->aDb[1].pSchema); 1258605264d2Sdrh sqlite3_mutex_leave(db->mutex); 12597e8b848aSdrh db->magic = SQLITE_MAGIC_CLOSED; 1260605264d2Sdrh sqlite3_mutex_free(db->mutex); 126152fb8e19Sdrh assert( sqlite3LookasideUsed(db,0)==0 ); 1262e9d1c720Sdrh if( db->lookaside.bMalloced ){ 1263633e6d57Sdrh sqlite3_free(db->lookaside.pStart); 1264e9d1c720Sdrh } 126517435752Sdrh sqlite3_free(db); 126675897234Sdrh } 126775897234Sdrh 126875897234Sdrh /* 12690f198a74Sdrh ** Rollback all database files. If tripCode is not SQLITE_OK, then 127047b7fc78Sdrh ** any write cursors are invalidated ("tripped" - as in "tripping a circuit 12710f198a74Sdrh ** breaker") and made to return tripCode if there are any further 127247b7fc78Sdrh ** attempts to use that cursor. Read cursors remain open and valid 127347b7fc78Sdrh ** but are "saved" in case the table pages are moved around. 1274001bbcbbSdrh */ 12750f198a74Sdrh void sqlite3RollbackAll(sqlite3 *db, int tripCode){ 1276001bbcbbSdrh int i; 1277f3f06bb3Sdanielk1977 int inTrans = 0; 127847b7fc78Sdrh int schemaChange; 1279e30f4426Sdrh assert( sqlite3_mutex_held(db->mutex) ); 12802d1d86fbSdanielk1977 sqlite3BeginBenignMalloc(); 1281617dc860Sdan 1282617dc860Sdan /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). 1283617dc860Sdan ** This is important in case the transaction being rolled back has 1284617dc860Sdan ** modified the database schema. If the b-tree mutexes are not taken 1285617dc860Sdan ** here, then another shared-cache connection might sneak in between 1286617dc860Sdan ** the database rollback and schema reset, which can cause false 1287617dc860Sdan ** corruption reports in some cases. */ 1288cd7b91a7Sdan sqlite3BtreeEnterAll(db); 12898257aa8dSdrh schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0; 1290617dc860Sdan 1291001bbcbbSdrh for(i=0; i<db->nDb; i++){ 12920f198a74Sdrh Btree *p = db->aDb[i].pBt; 12930f198a74Sdrh if( p ){ 12940f198a74Sdrh if( sqlite3BtreeIsInTrans(p) ){ 1295f3f06bb3Sdanielk1977 inTrans = 1; 1296f3f06bb3Sdanielk1977 } 129747b7fc78Sdrh sqlite3BtreeRollback(p, tripCode, !schemaChange); 1298001bbcbbSdrh } 1299001bbcbbSdrh } 1300a298e90dSdanielk1977 sqlite3VtabRollback(db); 13012d1d86fbSdanielk1977 sqlite3EndBenignMalloc(); 1302ae72d982Sdanielk1977 13037f32dc94Sdrh if( schemaChange ){ 1304ba968dbfSdrh sqlite3ExpirePreparedStatements(db, 0); 130581028a45Sdrh sqlite3ResetAllSchemasOfConnection(db); 1306001bbcbbSdrh } 1307cd7b91a7Sdan sqlite3BtreeLeaveAll(db); 1308001bbcbbSdrh 13091da40a38Sdan /* Any deferred constraint violations have now been resolved. */ 13101da40a38Sdan db->nDeferredCons = 0; 1311648e2643Sdrh db->nDeferredImmCons = 0; 1312648e2643Sdrh db->flags &= ~SQLITE_DeferFKs; 13131da40a38Sdan 131471fd80bfSdanielk1977 /* If one has been configured, invoke the rollback-hook callback */ 1315f3f06bb3Sdanielk1977 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ 131671fd80bfSdanielk1977 db->xRollbackCallback(db->pRollbackArg); 131771fd80bfSdanielk1977 } 131871fd80bfSdanielk1977 } 131971fd80bfSdanielk1977 1320001bbcbbSdrh /* 1321f2c1c99fSmistachkin ** Return a static string containing the name corresponding to the error code 1322f2c1c99fSmistachkin ** specified in the argument. 1323f2c1c99fSmistachkin */ 13245824d44bSmistachkin #if defined(SQLITE_NEED_ERR_NAME) 1325f2c1c99fSmistachkin const char *sqlite3ErrName(int rc){ 1326f2c1c99fSmistachkin const char *zName = 0; 132710269dc6Smistachkin int i, origRc = rc; 1328f2c1c99fSmistachkin for(i=0; i<2 && zName==0; i++, rc &= 0xff){ 1329f2c1c99fSmistachkin switch( rc ){ 1330f2c1c99fSmistachkin case SQLITE_OK: zName = "SQLITE_OK"; break; 1331f2c1c99fSmistachkin case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; 1332f2c1c99fSmistachkin case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; 1333f2c1c99fSmistachkin case SQLITE_PERM: zName = "SQLITE_PERM"; break; 1334f2c1c99fSmistachkin case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; 1335e84d8d32Smistachkin case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break; 1336f2c1c99fSmistachkin case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; 1337e84d8d32Smistachkin case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break; 1338f73819afSdan case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break; 1339f2c1c99fSmistachkin case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; 1340f2c1c99fSmistachkin case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break; 1341f2c1c99fSmistachkin case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; 1342f2c1c99fSmistachkin case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; 1343e84d8d32Smistachkin case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break; 13447e45e3a5Sdrh case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break; 1345e84d8d32Smistachkin case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break; 1346091a81b9Smistachkin case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break; 1347a688ca5eSdan case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break; 1348f2c1c99fSmistachkin case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; 1349f2c1c99fSmistachkin case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; 1350e84d8d32Smistachkin case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break; 1351e84d8d32Smistachkin case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break; 1352e84d8d32Smistachkin case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break; 1353e84d8d32Smistachkin case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break; 1354e84d8d32Smistachkin case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break; 1355e84d8d32Smistachkin case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break; 1356e84d8d32Smistachkin case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break; 1357e84d8d32Smistachkin case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break; 1358e84d8d32Smistachkin case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break; 1359e84d8d32Smistachkin case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break; 1360e84d8d32Smistachkin case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break; 1361e84d8d32Smistachkin case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break; 1362e84d8d32Smistachkin case SQLITE_IOERR_CHECKRESERVEDLOCK: 1363e84d8d32Smistachkin zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; 1364e84d8d32Smistachkin case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break; 1365e84d8d32Smistachkin case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break; 1366e84d8d32Smistachkin case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break; 1367e84d8d32Smistachkin case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break; 1368e84d8d32Smistachkin case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break; 1369e84d8d32Smistachkin case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break; 1370e84d8d32Smistachkin case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break; 1371e84d8d32Smistachkin case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break; 1372e84d8d32Smistachkin case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break; 1373e84d8d32Smistachkin case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break; 137416a2e7a0Smistachkin case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break; 1375d95a3d35Smistachkin case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break; 1376f2c1c99fSmistachkin case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; 1377e84d8d32Smistachkin case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break; 1378f2c1c99fSmistachkin case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break; 1379f2c1c99fSmistachkin case SQLITE_FULL: zName = "SQLITE_FULL"; break; 1380f2c1c99fSmistachkin case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; 1381e84d8d32Smistachkin case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break; 1382e84d8d32Smistachkin case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break; 1383e84d8d32Smistachkin case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break; 1384d95a3d35Smistachkin case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break; 1385f2c1c99fSmistachkin case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; 1386f2c1c99fSmistachkin case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; 1387f2c1c99fSmistachkin case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; 1388f2c1c99fSmistachkin case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; 1389f2c1c99fSmistachkin case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; 1390f2c1c99fSmistachkin case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break; 1391f2c1c99fSmistachkin case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break; 1392f2c1c99fSmistachkin case SQLITE_CONSTRAINT_FOREIGNKEY: 1393f2c1c99fSmistachkin zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break; 1394f2c1c99fSmistachkin case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break; 1395f2c1c99fSmistachkin case SQLITE_CONSTRAINT_PRIMARYKEY: 1396f2c1c99fSmistachkin zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break; 1397f2c1c99fSmistachkin case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break; 1398f2c1c99fSmistachkin case SQLITE_CONSTRAINT_COMMITHOOK: 1399f2c1c99fSmistachkin zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break; 1400f2c1c99fSmistachkin case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break; 1401f2c1c99fSmistachkin case SQLITE_CONSTRAINT_FUNCTION: 1402f2c1c99fSmistachkin zName = "SQLITE_CONSTRAINT_FUNCTION"; break; 1403f9c8ce3cSdrh case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break; 1404f2c1c99fSmistachkin case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; 1405f2c1c99fSmistachkin case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; 1406f2c1c99fSmistachkin case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; 1407f2c1c99fSmistachkin case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; 1408f2c1c99fSmistachkin case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; 1409f2c1c99fSmistachkin case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; 1410f2c1c99fSmistachkin case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break; 1411f2c1c99fSmistachkin case SQLITE_ROW: zName = "SQLITE_ROW"; break; 1412f2c1c99fSmistachkin case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break; 1413e84d8d32Smistachkin case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break; 1414e84d8d32Smistachkin case SQLITE_NOTICE_RECOVER_ROLLBACK: 1415e84d8d32Smistachkin zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break; 1416f2c1c99fSmistachkin case SQLITE_WARNING: zName = "SQLITE_WARNING"; break; 14178d56e205Sdrh case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break; 1418f2c1c99fSmistachkin case SQLITE_DONE: zName = "SQLITE_DONE"; break; 1419f2c1c99fSmistachkin } 1420f2c1c99fSmistachkin } 142110269dc6Smistachkin if( zName==0 ){ 142210269dc6Smistachkin static char zBuf[50]; 142310269dc6Smistachkin sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc); 142410269dc6Smistachkin zName = zBuf; 142510269dc6Smistachkin } 1426f2c1c99fSmistachkin return zName; 1427f2c1c99fSmistachkin } 142810269dc6Smistachkin #endif 1429f2c1c99fSmistachkin 1430f2c1c99fSmistachkin /* 1431c22bd47dSdrh ** Return a static string that describes the kind of error specified in the 1432c22bd47dSdrh ** argument. 1433247be43dSdrh */ 1434f20b21c8Sdanielk1977 const char *sqlite3ErrStr(int rc){ 143551c7d864Sdrh static const char* const aMsg[] = { 143651c7d864Sdrh /* SQLITE_OK */ "not an error", 1437a690ff36Sdrh /* SQLITE_ERROR */ "SQL logic error", 143851c7d864Sdrh /* SQLITE_INTERNAL */ 0, 143951c7d864Sdrh /* SQLITE_PERM */ "access permission denied", 1440ff4fa772Sdrh /* SQLITE_ABORT */ "query aborted", 144151c7d864Sdrh /* SQLITE_BUSY */ "database is locked", 144251c7d864Sdrh /* SQLITE_LOCKED */ "database table is locked", 144351c7d864Sdrh /* SQLITE_NOMEM */ "out of memory", 144451c7d864Sdrh /* SQLITE_READONLY */ "attempt to write a readonly database", 144551c7d864Sdrh /* SQLITE_INTERRUPT */ "interrupted", 144651c7d864Sdrh /* SQLITE_IOERR */ "disk I/O error", 144751c7d864Sdrh /* SQLITE_CORRUPT */ "database disk image is malformed", 14480b52b7d0Sdrh /* SQLITE_NOTFOUND */ "unknown operation", 144951c7d864Sdrh /* SQLITE_FULL */ "database or disk is full", 145051c7d864Sdrh /* SQLITE_CANTOPEN */ "unable to open database file", 14510dc7b74fSdan /* SQLITE_PROTOCOL */ "locking protocol", 1452e75be1aeSdrh /* SQLITE_EMPTY */ 0, 145351c7d864Sdrh /* SQLITE_SCHEMA */ "database schema has changed", 14544c8555fdSdrh /* SQLITE_TOOBIG */ "string or blob too big", 145551c7d864Sdrh /* SQLITE_CONSTRAINT */ "constraint failed", 145651c7d864Sdrh /* SQLITE_MISMATCH */ "datatype mismatch", 1457ff4fa772Sdrh /* SQLITE_MISUSE */ "bad parameter or other API misuse", 1458e75be1aeSdrh #ifdef SQLITE_DISABLE_LFS 145951c7d864Sdrh /* SQLITE_NOLFS */ "large file support is disabled", 1460e75be1aeSdrh #else 1461e75be1aeSdrh /* SQLITE_NOLFS */ 0, 1462e75be1aeSdrh #endif 146351c7d864Sdrh /* SQLITE_AUTH */ "authorization denied", 1464e75be1aeSdrh /* SQLITE_FORMAT */ 0, 1465ff4fa772Sdrh /* SQLITE_RANGE */ "column index out of range", 1466ff4fa772Sdrh /* SQLITE_NOTADB */ "file is not a database", 14672a86110aSmistachkin /* SQLITE_NOTICE */ "notification message", 14682a86110aSmistachkin /* SQLITE_WARNING */ "warning message", 146951c7d864Sdrh }; 147021021a5cSdrh const char *zErr = "unknown error"; 147121021a5cSdrh switch( rc ){ 147221021a5cSdrh case SQLITE_ABORT_ROLLBACK: { 147321021a5cSdrh zErr = "abort due to ROLLBACK"; 147421021a5cSdrh break; 1475247be43dSdrh } 14762a86110aSmistachkin case SQLITE_ROW: { 14772a86110aSmistachkin zErr = "another row available"; 14782a86110aSmistachkin break; 14792a86110aSmistachkin } 14802a86110aSmistachkin case SQLITE_DONE: { 14812a86110aSmistachkin zErr = "no more rows available"; 14822a86110aSmistachkin break; 14832a86110aSmistachkin } 148421021a5cSdrh default: { 148521021a5cSdrh rc &= 0xff; 148621021a5cSdrh if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){ 148721021a5cSdrh zErr = aMsg[rc]; 148821021a5cSdrh } 148921021a5cSdrh break; 149021021a5cSdrh } 149121021a5cSdrh } 149221021a5cSdrh return zErr; 1493247be43dSdrh } 1494247be43dSdrh 1495247be43dSdrh /* 14962dfbbcafSdrh ** This routine implements a busy callback that sleeps and tries 14972dfbbcafSdrh ** again until a timeout value is reached. The timeout value is 14982dfbbcafSdrh ** an integer number of milliseconds passed in as the first 14992dfbbcafSdrh ** argument. 1500f0119b2eSdrh ** 1501f0119b2eSdrh ** Return non-zero to retry the lock. Return zero to stop trying 1502f0119b2eSdrh ** and cause SQLite to return SQLITE_BUSY. 15032dfbbcafSdrh */ 1504daffd0e5Sdrh static int sqliteDefaultBusyCallback( 150597903fefSdrh void *ptr, /* Database connection */ 1506f0119b2eSdrh int count, /* Number of times table has been busy */ 1507f0119b2eSdrh sqlite3_file *pFile /* The file on which the lock occurred */ 15082dfbbcafSdrh ){ 15090ede9ebeSdrh #if SQLITE_OS_WIN || HAVE_USLEEP 1510f0119b2eSdrh /* This case is for systems that have support for sleeping for fractions of 1511f0119b2eSdrh ** a second. Examples: All windows systems, unix systems with usleep() */ 1512ee570fa4Sdrh static const u8 delays[] = 1513ee570fa4Sdrh { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; 1514ee570fa4Sdrh static const u8 totals[] = 1515ee570fa4Sdrh { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; 1516fcd71b60Sdrh # define NDELAY ArraySize(delays) 1517b4b47411Sdanielk1977 sqlite3 *db = (sqlite3 *)ptr; 1518f0119b2eSdrh int tmout = db->busyTimeout; 151997903fefSdrh int delay, prior; 15202dfbbcafSdrh 1521f0119b2eSdrh #ifdef SQLITE_ENABLE_SETLK_TIMEOUT 1522f0119b2eSdrh if( sqlite3OsFileControl(pFile,SQLITE_FCNTL_LOCK_TIMEOUT,&tmout)==SQLITE_OK ){ 1523f0119b2eSdrh if( count ){ 1524f0119b2eSdrh tmout = 0; 1525f0119b2eSdrh sqlite3OsFileControl(pFile, SQLITE_FCNTL_LOCK_TIMEOUT, &tmout); 1526f0119b2eSdrh return 0; 1527f0119b2eSdrh }else{ 1528f0119b2eSdrh return 1; 1529f0119b2eSdrh } 1530f0119b2eSdrh } 15315013c4b9Sdrh #else 15325013c4b9Sdrh UNUSED_PARAMETER(pFile); 1533f0119b2eSdrh #endif 1534ee570fa4Sdrh assert( count>=0 ); 1535ee570fa4Sdrh if( count < NDELAY ){ 1536ee570fa4Sdrh delay = delays[count]; 1537ee570fa4Sdrh prior = totals[count]; 1538d1bec47aSdrh }else{ 1539d1bec47aSdrh delay = delays[NDELAY-1]; 154068cb6192Sdrh prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); 15412dfbbcafSdrh } 1542f0119b2eSdrh if( prior + delay > tmout ){ 1543f0119b2eSdrh delay = tmout - prior; 15442dfbbcafSdrh if( delay<=0 ) return 0; 15452dfbbcafSdrh } 15462a6bdf6dSdanielk1977 sqlite3OsSleep(db->pVfs, delay*1000); 15472dfbbcafSdrh return 1; 15482dfbbcafSdrh #else 1549f0119b2eSdrh /* This case for unix systems that lack usleep() support. Sleeping 1550f0119b2eSdrh ** must be done in increments of whole seconds */ 1551482ea18fSdrh sqlite3 *db = (sqlite3 *)ptr; 1552f0119b2eSdrh int tmout = ((sqlite3 *)ptr)->busyTimeout; 15535013c4b9Sdrh UNUSED_PARAMETER(pFile); 1554f0119b2eSdrh if( (count+1)*1000 > tmout ){ 15552dfbbcafSdrh return 0; 15562dfbbcafSdrh } 1557482ea18fSdrh sqlite3OsSleep(db->pVfs, 1000000); 15582dfbbcafSdrh return 1; 15592dfbbcafSdrh #endif 15602dfbbcafSdrh } 15612dfbbcafSdrh 15622dfbbcafSdrh /* 1563a4afb65cSdrh ** Invoke the given busy handler. 1564a4afb65cSdrh ** 1565f0119b2eSdrh ** This routine is called when an operation failed to acquire a 1566f0119b2eSdrh ** lock on VFS file pFile. 1567f0119b2eSdrh ** 1568a4afb65cSdrh ** If this routine returns non-zero, the lock is retried. If it 1569a4afb65cSdrh ** returns 0, the operation aborts with an SQLITE_BUSY error. 1570a4afb65cSdrh */ 1571f0119b2eSdrh int sqlite3InvokeBusyHandler(BusyHandler *p, sqlite3_file *pFile){ 1572a4afb65cSdrh int rc; 157380262896Sdrh if( p->xBusyHandler==0 || p->nBusy<0 ) return 0; 1574f0119b2eSdrh if( p->bExtraFileArg ){ 1575f0119b2eSdrh /* Add an extra parameter with the pFile pointer to the end of the 1576f0119b2eSdrh ** callback argument list */ 1577f0119b2eSdrh int (*xTra)(void*,int,sqlite3_file*); 1578f0119b2eSdrh xTra = (int(*)(void*,int,sqlite3_file*))p->xBusyHandler; 1579f0119b2eSdrh rc = xTra(p->pBusyArg, p->nBusy, pFile); 1580f0119b2eSdrh }else{ 1581f0119b2eSdrh /* Legacy style busy handler callback */ 158280262896Sdrh rc = p->xBusyHandler(p->pBusyArg, p->nBusy); 1583f0119b2eSdrh } 1584a4afb65cSdrh if( rc==0 ){ 1585a4afb65cSdrh p->nBusy = -1; 1586a4afb65cSdrh }else{ 1587a4afb65cSdrh p->nBusy++; 1588a4afb65cSdrh } 1589a4afb65cSdrh return rc; 1590a4afb65cSdrh } 1591a4afb65cSdrh 1592a4afb65cSdrh /* 15932dfbbcafSdrh ** This routine sets the busy callback for an Sqlite database to the 15942dfbbcafSdrh ** given callback function with the given argument. 15952dfbbcafSdrh */ 1596f9d64d2cSdanielk1977 int sqlite3_busy_handler( 1597f9d64d2cSdanielk1977 sqlite3 *db, 15982a764eb0Sdanielk1977 int (*xBusy)(void*,int), 15992dfbbcafSdrh void *pArg 16002dfbbcafSdrh ){ 16019ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 160296c707a3Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 16039ca95730Sdrh #endif 1604e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 160580262896Sdrh db->busyHandler.xBusyHandler = xBusy; 160680262896Sdrh db->busyHandler.pBusyArg = pArg; 1607a4afb65cSdrh db->busyHandler.nBusy = 0; 1608f0119b2eSdrh db->busyHandler.bExtraFileArg = 0; 1609c0c7b5eeSdrh db->busyTimeout = 0; 1610e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 1611f9d64d2cSdanielk1977 return SQLITE_OK; 16122dfbbcafSdrh } 16132dfbbcafSdrh 1614348bb5d6Sdanielk1977 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK 1615348bb5d6Sdanielk1977 /* 1616348bb5d6Sdanielk1977 ** This routine sets the progress callback for an Sqlite database to the 1617348bb5d6Sdanielk1977 ** given callback function with the given argument. The progress callback will 1618348bb5d6Sdanielk1977 ** be invoked every nOps opcodes. 1619348bb5d6Sdanielk1977 */ 162024b03fd0Sdanielk1977 void sqlite3_progress_handler( 16219bb575fdSdrh sqlite3 *db, 1622348bb5d6Sdanielk1977 int nOps, 1623348bb5d6Sdanielk1977 int (*xProgress)(void*), 1624348bb5d6Sdanielk1977 void *pArg 1625348bb5d6Sdanielk1977 ){ 16269ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 16279ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 16289ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 16299ca95730Sdrh return; 16309ca95730Sdrh } 16319ca95730Sdrh #endif 1632e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 1633348bb5d6Sdanielk1977 if( nOps>0 ){ 1634348bb5d6Sdanielk1977 db->xProgress = xProgress; 16358f8c65f7Sdrh db->nProgressOps = (unsigned)nOps; 1636348bb5d6Sdanielk1977 db->pProgressArg = pArg; 1637348bb5d6Sdanielk1977 }else{ 1638348bb5d6Sdanielk1977 db->xProgress = 0; 1639348bb5d6Sdanielk1977 db->nProgressOps = 0; 1640348bb5d6Sdanielk1977 db->pProgressArg = 0; 1641348bb5d6Sdanielk1977 } 1642e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 1643348bb5d6Sdanielk1977 } 1644348bb5d6Sdanielk1977 #endif 1645348bb5d6Sdanielk1977 1646348bb5d6Sdanielk1977 16472dfbbcafSdrh /* 16482dfbbcafSdrh ** This routine installs a default busy handler that waits for the 16492dfbbcafSdrh ** specified number of milliseconds before returning 0. 16502dfbbcafSdrh */ 1651f9d64d2cSdanielk1977 int sqlite3_busy_timeout(sqlite3 *db, int ms){ 16529ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 16539ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 16549ca95730Sdrh #endif 16552dfbbcafSdrh if( ms>0 ){ 1656f0119b2eSdrh sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback, 1657f0119b2eSdrh (void*)db); 1658c0c7b5eeSdrh db->busyTimeout = ms; 1659f0119b2eSdrh db->busyHandler.bExtraFileArg = 1; 16602dfbbcafSdrh }else{ 166124b03fd0Sdanielk1977 sqlite3_busy_handler(db, 0, 0); 16622dfbbcafSdrh } 1663f9d64d2cSdanielk1977 return SQLITE_OK; 16642dfbbcafSdrh } 16654c504391Sdrh 16664c504391Sdrh /* 16674c504391Sdrh ** Cause any pending operation to stop at its earliest opportunity. 16684c504391Sdrh */ 16699bb575fdSdrh void sqlite3_interrupt(sqlite3 *db){ 16709ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 1671575242f6Sdrh if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){ 16729ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 16739ca95730Sdrh return; 16749ca95730Sdrh } 16759ca95730Sdrh #endif 1676881feaa0Sdrh db->u1.isInterrupted = 1; 16774c504391Sdrh } 1678fa86c412Sdrh 1679fa86c412Sdrh 1680fa86c412Sdrh /* 1681771151b6Sdanielk1977 ** This function is exactly the same as sqlite3_create_function(), except 1682771151b6Sdanielk1977 ** that it is designed to be called by internal code. The difference is 1683771151b6Sdanielk1977 ** that if a malloc() fails in sqlite3_create_function(), an error code 1684771151b6Sdanielk1977 ** is returned and the mallocFailed flag cleared. 1685fa86c412Sdrh */ 1686771151b6Sdanielk1977 int sqlite3CreateFunc( 16876590493dSdanielk1977 sqlite3 *db, 16886590493dSdanielk1977 const char *zFunctionName, 16896590493dSdanielk1977 int nArg, 1690d8123366Sdanielk1977 int enc, 16916590493dSdanielk1977 void *pUserData, 16922d80151fSdrh void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), 16936590493dSdanielk1977 void (*xStep)(sqlite3_context*,int,sqlite3_value **), 1694d2199f0fSdan void (*xFinal)(sqlite3_context*), 1695660af939Sdan void (*xValue)(sqlite3_context*), 1696660af939Sdan void (*xInverse)(sqlite3_context*,int,sqlite3_value **), 1697d2199f0fSdan FuncDestructor *pDestructor 16988e0a2f90Sdrh ){ 16990bce8354Sdrh FuncDef *p; 17004b59ab5eSdrh int nName; 17014a8ee3dfSdrh int extraFlags; 17026590493dSdanielk1977 1703e30f4426Sdrh assert( sqlite3_mutex_held(db->mutex) ); 17041e80ace4Sdrh assert( xValue==0 || xSFunc==0 ); 17051e80ace4Sdrh if( zFunctionName==0 /* Must have a valid name */ 17061e80ace4Sdrh || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */ 17071e80ace4Sdrh || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */ 17081e80ace4Sdrh || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */ 17091e80ace4Sdrh || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) 17101e80ace4Sdrh || (255<(nName = sqlite3Strlen30( zFunctionName))) 17111e80ace4Sdrh ){ 1712413c3d36Sdrh return SQLITE_MISUSE_BKPT; 17136590493dSdanielk1977 } 17146590493dSdanielk1977 17154a8ee3dfSdrh assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC ); 17164a8ee3dfSdrh extraFlags = enc & SQLITE_DETERMINISTIC; 17174a8ee3dfSdrh enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY); 17184a8ee3dfSdrh 171993758c8dSdanielk1977 #ifndef SQLITE_OMIT_UTF16 1720d8123366Sdanielk1977 /* If SQLITE_UTF16 is specified as the encoding type, transform this 1721d8123366Sdanielk1977 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the 1722d8123366Sdanielk1977 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. 1723d8123366Sdanielk1977 ** 1724d8123366Sdanielk1977 ** If SQLITE_ANY is specified, add three versions of the function 1725d8123366Sdanielk1977 ** to the hash table. 1726d8123366Sdanielk1977 */ 1727d8123366Sdanielk1977 if( enc==SQLITE_UTF16 ){ 1728d8123366Sdanielk1977 enc = SQLITE_UTF16NATIVE; 1729d8123366Sdanielk1977 }else if( enc==SQLITE_ANY ){ 1730d8123366Sdanielk1977 int rc; 17314a8ee3dfSdrh rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags, 1732660af939Sdan pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor); 1733e30f4426Sdrh if( rc==SQLITE_OK ){ 17344a8ee3dfSdrh rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags, 1735660af939Sdan pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor); 1736e30f4426Sdrh } 1737e30f4426Sdrh if( rc!=SQLITE_OK ){ 1738e30f4426Sdrh return rc; 1739e30f4426Sdrh } 1740d8123366Sdanielk1977 enc = SQLITE_UTF16BE; 1741d8123366Sdanielk1977 } 174293758c8dSdanielk1977 #else 174393758c8dSdanielk1977 enc = SQLITE_UTF8; 174493758c8dSdanielk1977 #endif 1745d8123366Sdanielk1977 17469636c4e1Sdanielk1977 /* Check if an existing function is being overridden or deleted. If so, 17479636c4e1Sdanielk1977 ** and there are active VMs, then return SQLITE_BUSY. If a function 17489636c4e1Sdanielk1977 ** is being overridden/deleted but there are no active VMs, allow the 17499636c4e1Sdanielk1977 ** operation to continue but invalidate all precompiled statements. 17509636c4e1Sdanielk1977 */ 175180738d9cSdrh p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0); 1752c7bf5716Sdrh if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){ 17534f7d3a5fSdrh if( db->nVdbeActive ){ 175413f40da3Sdrh sqlite3ErrorWithMsg(db, SQLITE_BUSY, 1755b309becdSdrh "unable to delete/modify user-function due to active statements"); 175617435752Sdrh assert( !db->mallocFailed ); 17579636c4e1Sdanielk1977 return SQLITE_BUSY; 17589636c4e1Sdanielk1977 }else{ 1759ba968dbfSdrh sqlite3ExpirePreparedStatements(db, 0); 17609636c4e1Sdanielk1977 } 17619636c4e1Sdanielk1977 } 17629636c4e1Sdanielk1977 176380738d9cSdrh p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1); 1764fa18beceSdanielk1977 assert(p || db->mallocFailed); 1765fa18beceSdanielk1977 if( !p ){ 1766fad3039cSmistachkin return SQLITE_NOMEM_BKPT; 1767fa18beceSdanielk1977 } 1768d2199f0fSdan 1769d2199f0fSdan /* If an older version of the function with a configured destructor is 1770d2199f0fSdan ** being replaced invoke the destructor function here. */ 1771d2199f0fSdan functionDestroy(db, p); 1772d2199f0fSdan 1773d2199f0fSdan if( pDestructor ){ 1774d2199f0fSdan pDestructor->nRef++; 1775d2199f0fSdan } 177680738d9cSdrh p->u.pDestructor = pDestructor; 17774a8ee3dfSdrh p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags; 17784a8ee3dfSdrh testcase( p->funcFlags & SQLITE_DETERMINISTIC ); 17792d80151fSdrh p->xSFunc = xSFunc ? xSFunc : xStep; 17806590493dSdanielk1977 p->xFinalize = xFinal; 1781660af939Sdan p->xValue = xValue; 1782660af939Sdan p->xInverse = xInverse; 17831350b030Sdrh p->pUserData = pUserData; 17841bd10f8aSdrh p->nArg = (u16)nArg; 17856590493dSdanielk1977 return SQLITE_OK; 17866590493dSdanielk1977 } 1787771151b6Sdanielk1977 1788771151b6Sdanielk1977 /* 1789660af939Sdan ** Worker function used by utf-8 APIs that create new functions: 1790660af939Sdan ** 1791660af939Sdan ** sqlite3_create_function() 1792660af939Sdan ** sqlite3_create_function_v2() 1793660af939Sdan ** sqlite3_create_window_function() 1794771151b6Sdanielk1977 */ 1795660af939Sdan static int createFunctionApi( 1796d2199f0fSdan sqlite3 *db, 1797d2199f0fSdan const char *zFunc, 1798d2199f0fSdan int nArg, 1799d2199f0fSdan int enc, 1800d2199f0fSdan void *p, 18012d80151fSdrh void (*xSFunc)(sqlite3_context*,int,sqlite3_value**), 1802d2199f0fSdan void (*xStep)(sqlite3_context*,int,sqlite3_value**), 1803d2199f0fSdan void (*xFinal)(sqlite3_context*), 1804660af939Sdan void (*xValue)(sqlite3_context*), 1805660af939Sdan void (*xInverse)(sqlite3_context*,int,sqlite3_value**), 1806d2199f0fSdan void(*xDestroy)(void*) 1807d2199f0fSdan ){ 1808bd2aaf9aSshaneh int rc = SQLITE_ERROR; 1809d2199f0fSdan FuncDestructor *pArg = 0; 18109ca95730Sdrh 18119ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 18129ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 18139ca95730Sdrh return SQLITE_MISUSE_BKPT; 18149ca95730Sdrh } 18159ca95730Sdrh #endif 1816d2199f0fSdan sqlite3_mutex_enter(db->mutex); 1817d2199f0fSdan if( xDestroy ){ 181892011847Sdrh pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor)); 18199508daa9Sdan if( !pArg ){ 182092011847Sdrh sqlite3OomFault(db); 18219508daa9Sdan xDestroy(p); 18229508daa9Sdan goto out; 18239508daa9Sdan } 182492011847Sdrh pArg->nRef = 0; 1825d2199f0fSdan pArg->xDestroy = xDestroy; 1826d2199f0fSdan pArg->pUserData = p; 1827d2199f0fSdan } 1828660af939Sdan rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, 1829660af939Sdan xSFunc, xStep, xFinal, xValue, xInverse, pArg 1830660af939Sdan ); 1831d2199f0fSdan if( pArg && pArg->nRef==0 ){ 1832d2199f0fSdan assert( rc!=SQLITE_OK ); 18339508daa9Sdan xDestroy(p); 183492011847Sdrh sqlite3_free(pArg); 1835d2199f0fSdan } 1836d2199f0fSdan 1837d2199f0fSdan out: 1838e30f4426Sdrh rc = sqlite3ApiExit(db, rc); 1839e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 1840e30f4426Sdrh return rc; 1841771151b6Sdanielk1977 } 1842771151b6Sdanielk1977 1843660af939Sdan /* 1844660af939Sdan ** Create new user functions. 1845660af939Sdan */ 1846660af939Sdan int sqlite3_create_function( 1847660af939Sdan sqlite3 *db, 1848660af939Sdan const char *zFunc, 1849660af939Sdan int nArg, 1850660af939Sdan int enc, 1851660af939Sdan void *p, 1852660af939Sdan void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), 1853660af939Sdan void (*xStep)(sqlite3_context*,int,sqlite3_value **), 1854660af939Sdan void (*xFinal)(sqlite3_context*) 1855660af939Sdan ){ 1856660af939Sdan return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep, 1857660af939Sdan xFinal, 0, 0, 0); 1858660af939Sdan } 1859660af939Sdan int sqlite3_create_function_v2( 1860660af939Sdan sqlite3 *db, 1861660af939Sdan const char *zFunc, 1862660af939Sdan int nArg, 1863660af939Sdan int enc, 1864660af939Sdan void *p, 1865660af939Sdan void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), 1866660af939Sdan void (*xStep)(sqlite3_context*,int,sqlite3_value **), 1867660af939Sdan void (*xFinal)(sqlite3_context*), 1868660af939Sdan void (*xDestroy)(void *) 1869660af939Sdan ){ 1870660af939Sdan return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep, 1871660af939Sdan xFinal, 0, 0, xDestroy); 1872660af939Sdan } 1873660af939Sdan int sqlite3_create_window_function( 1874660af939Sdan sqlite3 *db, 1875660af939Sdan const char *zFunc, 1876660af939Sdan int nArg, 1877660af939Sdan int enc, 1878660af939Sdan void *p, 1879660af939Sdan void (*xStep)(sqlite3_context*,int,sqlite3_value **), 1880660af939Sdan void (*xFinal)(sqlite3_context*), 1881660af939Sdan void (*xValue)(sqlite3_context*), 1882660af939Sdan void (*xInverse)(sqlite3_context*,int,sqlite3_value **), 1883660af939Sdan void (*xDestroy)(void *) 1884660af939Sdan ){ 1885660af939Sdan return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep, 1886660af939Sdan xFinal, xValue, xInverse, xDestroy); 1887660af939Sdan } 1888660af939Sdan 188993758c8dSdanielk1977 #ifndef SQLITE_OMIT_UTF16 18906590493dSdanielk1977 int sqlite3_create_function16( 18916590493dSdanielk1977 sqlite3 *db, 18926590493dSdanielk1977 const void *zFunctionName, 18936590493dSdanielk1977 int nArg, 18946590493dSdanielk1977 int eTextRep, 1895771151b6Sdanielk1977 void *p, 18962d80151fSdrh void (*xSFunc)(sqlite3_context*,int,sqlite3_value**), 18976590493dSdanielk1977 void (*xStep)(sqlite3_context*,int,sqlite3_value**), 18986590493dSdanielk1977 void (*xFinal)(sqlite3_context*) 18996590493dSdanielk1977 ){ 19006590493dSdanielk1977 int rc; 1901af9a7c22Sdrh char *zFunc8; 19029ca95730Sdrh 19039ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 19049ca95730Sdrh if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT; 19059ca95730Sdrh #endif 1906e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 190717435752Sdrh assert( !db->mallocFailed ); 1908b7dca7d7Sdan zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); 1909660af939Sdan rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0); 1910633e6d57Sdrh sqlite3DbFree(db, zFunc8); 1911e30f4426Sdrh rc = sqlite3ApiExit(db, rc); 1912e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 1913e30f4426Sdrh return rc; 19148e0a2f90Sdrh } 191593758c8dSdanielk1977 #endif 1916c9b84a1fSdrh 1917b7481e70Sdrh 1918b7481e70Sdrh /* 191992011847Sdrh ** The following is the implementation of an SQL function that always 192092011847Sdrh ** fails with an error message stating that the function is used in the 192192011847Sdrh ** wrong context. The sqlite3_overload_function() API might construct 192292011847Sdrh ** SQL function that use this routine so that the functions will exist 192392011847Sdrh ** for name resolution but are actually overloaded by the xFindFunction 192492011847Sdrh ** method of virtual tables. 192592011847Sdrh */ 192692011847Sdrh static void sqlite3InvalidFunction( 192792011847Sdrh sqlite3_context *context, /* The function calling context */ 192892011847Sdrh int NotUsed, /* Number of arguments to the function */ 192992011847Sdrh sqlite3_value **NotUsed2 /* Value of each argument */ 193092011847Sdrh ){ 193192011847Sdrh const char *zName = (const char*)sqlite3_user_data(context); 193292011847Sdrh char *zErr; 193392011847Sdrh UNUSED_PARAMETER2(NotUsed, NotUsed2); 193492011847Sdrh zErr = sqlite3_mprintf( 193592011847Sdrh "unable to use function %s in the requested context", zName); 193692011847Sdrh sqlite3_result_error(context, zErr, -1); 193792011847Sdrh sqlite3_free(zErr); 193892011847Sdrh } 193992011847Sdrh 194092011847Sdrh /* 1941b7481e70Sdrh ** Declare that a function has been overloaded by a virtual table. 1942b7481e70Sdrh ** 1943b7481e70Sdrh ** If the function already exists as a regular global function, then 1944b7481e70Sdrh ** this routine is a no-op. If the function does not exist, then create 1945b7481e70Sdrh ** a new one that always throws a run-time error. 1946b7481e70Sdrh ** 1947b7481e70Sdrh ** When virtual tables intend to provide an overloaded function, they 1948b7481e70Sdrh ** should call this routine to make sure the global function exists. 1949b7481e70Sdrh ** A global function must exist in order for name resolution to work 1950b7481e70Sdrh ** properly. 1951b7481e70Sdrh */ 1952b7481e70Sdrh int sqlite3_overload_function( 1953b7481e70Sdrh sqlite3 *db, 1954b7481e70Sdrh const char *zName, 1955b7481e70Sdrh int nArg 1956b7481e70Sdrh ){ 195792011847Sdrh int rc; 195892011847Sdrh char *zCopy; 19599ca95730Sdrh 19609ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 19619ca95730Sdrh if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ 19629ca95730Sdrh return SQLITE_MISUSE_BKPT; 19639ca95730Sdrh } 19649ca95730Sdrh #endif 1965e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 196692011847Sdrh rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0; 1967e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 196892011847Sdrh if( rc ) return SQLITE_OK; 196992011847Sdrh zCopy = sqlite3_mprintf(zName); 197092011847Sdrh if( zCopy==0 ) return SQLITE_NOMEM; 197192011847Sdrh return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8, 197292011847Sdrh zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free); 1973b7481e70Sdrh } 1974b7481e70Sdrh 197519e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 1976c9b84a1fSdrh /* 197718de4824Sdrh ** Register a trace function. The pArg from the previously registered trace 197818de4824Sdrh ** is returned. 197918de4824Sdrh ** 198018de4824Sdrh ** A NULL trace function means that no tracing is executes. A non-NULL 198118de4824Sdrh ** trace is a pointer to a function that is invoked at the start of each 198219e2d37fSdrh ** SQL statement. 198318de4824Sdrh */ 19843d2a529dSdrh #ifndef SQLITE_OMIT_DEPRECATED 19859bb575fdSdrh void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){ 1986e30f4426Sdrh void *pOld; 19879ca95730Sdrh 19889ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 19899ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 19909ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 19919ca95730Sdrh return 0; 19929ca95730Sdrh } 19939ca95730Sdrh #endif 1994e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 1995e30f4426Sdrh pOld = db->pTraceArg; 19961637a517Sdrh db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0; 1997fca760c8Sdrh db->xTrace = (int(*)(u32,void*,void*,void*))xTrace; 199818de4824Sdrh db->pTraceArg = pArg; 1999e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 200018de4824Sdrh return pOld; 20010d1a643aSdrh } 20023d2a529dSdrh #endif /* SQLITE_OMIT_DEPRECATED */ 20033d2a529dSdrh 20043d2a529dSdrh /* Register a trace callback using the version-2 interface. 20053d2a529dSdrh */ 20063d2a529dSdrh int sqlite3_trace_v2( 20073d2a529dSdrh sqlite3 *db, /* Trace this connection */ 2008fca760c8Sdrh unsigned mTrace, /* Mask of events to be traced */ 2009fca760c8Sdrh int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */ 20103d2a529dSdrh void *pArg /* Context */ 20113d2a529dSdrh ){ 20123d2a529dSdrh #ifdef SQLITE_ENABLE_API_ARMOR 20133d2a529dSdrh if( !sqlite3SafetyCheckOk(db) ){ 2014283a85caSmistachkin return SQLITE_MISUSE_BKPT; 20153d2a529dSdrh } 20163d2a529dSdrh #endif 20173d2a529dSdrh sqlite3_mutex_enter(db->mutex); 2018fb82820aSdrh if( mTrace==0 ) xTrace = 0; 2019fb82820aSdrh if( xTrace==0 ) mTrace = 0; 20203d2a529dSdrh db->mTrace = mTrace; 20213d2a529dSdrh db->xTrace = xTrace; 20223d2a529dSdrh db->pTraceArg = pArg; 20233d2a529dSdrh sqlite3_mutex_leave(db->mutex); 20243d2a529dSdrh return SQLITE_OK; 20253d2a529dSdrh } 20263d2a529dSdrh 2027087ec072Sdrh #ifndef SQLITE_OMIT_DEPRECATED 202819e2d37fSdrh /* 202919e2d37fSdrh ** Register a profile function. The pArg from the previously registered 203019e2d37fSdrh ** profile function is returned. 203119e2d37fSdrh ** 203219e2d37fSdrh ** A NULL profile function means that no profiling is executes. A non-NULL 203319e2d37fSdrh ** profile is a pointer to a function that is invoked at the conclusion of 203419e2d37fSdrh ** each SQL statement that is run. 203519e2d37fSdrh */ 203619e2d37fSdrh void *sqlite3_profile( 203719e2d37fSdrh sqlite3 *db, 203819e2d37fSdrh void (*xProfile)(void*,const char*,sqlite_uint64), 203919e2d37fSdrh void *pArg 204019e2d37fSdrh ){ 2041e30f4426Sdrh void *pOld; 20429ca95730Sdrh 20439ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 20449ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 20459ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 20469ca95730Sdrh return 0; 20479ca95730Sdrh } 20489ca95730Sdrh #endif 2049e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 2050e30f4426Sdrh pOld = db->pProfileArg; 205119e2d37fSdrh db->xProfile = xProfile; 205219e2d37fSdrh db->pProfileArg = pArg; 2053e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 205419e2d37fSdrh return pOld; 205519e2d37fSdrh } 2056087ec072Sdrh #endif /* SQLITE_OMIT_DEPRECATED */ 205719e2d37fSdrh #endif /* SQLITE_OMIT_TRACE */ 2058b0208ccaSpaul 2059cd565fd1Sdrh /* 2060cd565fd1Sdrh ** Register a function to be invoked when a transaction commits. 206171fd80bfSdanielk1977 ** If the invoked function returns non-zero, then the commit becomes a 2062aa940eacSdrh ** rollback. 2063aa940eacSdrh */ 206424b03fd0Sdanielk1977 void *sqlite3_commit_hook( 20659bb575fdSdrh sqlite3 *db, /* Attach the hook to this database */ 2066aa940eacSdrh int (*xCallback)(void*), /* Function to invoke on each commit */ 2067aa940eacSdrh void *pArg /* Argument to the function */ 2068aa940eacSdrh ){ 2069e30f4426Sdrh void *pOld; 20709ca95730Sdrh 20719ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 20729ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 20739ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 20749ca95730Sdrh return 0; 20759ca95730Sdrh } 20769ca95730Sdrh #endif 2077e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 2078e30f4426Sdrh pOld = db->pCommitArg; 2079aa940eacSdrh db->xCommitCallback = xCallback; 2080aa940eacSdrh db->pCommitArg = pArg; 2081e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 2082aa940eacSdrh return pOld; 2083aa940eacSdrh } 2084aa940eacSdrh 208594eb6a14Sdanielk1977 /* 208694eb6a14Sdanielk1977 ** Register a callback to be invoked each time a row is updated, 208794eb6a14Sdanielk1977 ** inserted or deleted using this database connection. 208894eb6a14Sdanielk1977 */ 208971fd80bfSdanielk1977 void *sqlite3_update_hook( 209094eb6a14Sdanielk1977 sqlite3 *db, /* Attach the hook to this database */ 209194eb6a14Sdanielk1977 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), 209294eb6a14Sdanielk1977 void *pArg /* Argument to the function */ 209394eb6a14Sdanielk1977 ){ 2094e30f4426Sdrh void *pRet; 20959ca95730Sdrh 20969ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 20979ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 20989ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 20999ca95730Sdrh return 0; 21009ca95730Sdrh } 21019ca95730Sdrh #endif 2102e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 2103e30f4426Sdrh pRet = db->pUpdateArg; 210494eb6a14Sdanielk1977 db->xUpdateCallback = xCallback; 210594eb6a14Sdanielk1977 db->pUpdateArg = pArg; 2106e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 210771fd80bfSdanielk1977 return pRet; 210894eb6a14Sdanielk1977 } 210994eb6a14Sdanielk1977 211071fd80bfSdanielk1977 /* 211171fd80bfSdanielk1977 ** Register a callback to be invoked each time a transaction is rolled 211271fd80bfSdanielk1977 ** back by this database connection. 211371fd80bfSdanielk1977 */ 211471fd80bfSdanielk1977 void *sqlite3_rollback_hook( 211571fd80bfSdanielk1977 sqlite3 *db, /* Attach the hook to this database */ 211671fd80bfSdanielk1977 void (*xCallback)(void*), /* Callback function */ 211771fd80bfSdanielk1977 void *pArg /* Argument to the function */ 211871fd80bfSdanielk1977 ){ 2119e30f4426Sdrh void *pRet; 21209ca95730Sdrh 21219ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 21229ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 21239ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 21249ca95730Sdrh return 0; 21259ca95730Sdrh } 21269ca95730Sdrh #endif 2127e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 2128e30f4426Sdrh pRet = db->pRollbackArg; 212971fd80bfSdanielk1977 db->xRollbackCallback = xCallback; 213071fd80bfSdanielk1977 db->pRollbackArg = pArg; 2131e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 213271fd80bfSdanielk1977 return pRet; 213371fd80bfSdanielk1977 } 2134aa940eacSdrh 21359b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 213646c47d46Sdan /* 213746c47d46Sdan ** Register a callback to be invoked each time a row is updated, 213846c47d46Sdan ** inserted or deleted using this database connection. 213946c47d46Sdan */ 214046c47d46Sdan void *sqlite3_preupdate_hook( 214146c47d46Sdan sqlite3 *db, /* Attach the hook to this database */ 214246c47d46Sdan void(*xCallback)( /* Callback function */ 214346c47d46Sdan void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64), 214446c47d46Sdan void *pArg /* First callback argument */ 214546c47d46Sdan ){ 214646c47d46Sdan void *pRet; 214746c47d46Sdan sqlite3_mutex_enter(db->mutex); 214846c47d46Sdan pRet = db->pPreUpdateArg; 214946c47d46Sdan db->xPreUpdateCallback = xCallback; 215046c47d46Sdan db->pPreUpdateArg = pArg; 215146c47d46Sdan sqlite3_mutex_leave(db->mutex); 215246c47d46Sdan return pRet; 215346c47d46Sdan } 21549b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 215546c47d46Sdan 2156586b9c8aSdan #ifndef SQLITE_OMIT_WAL 2157586b9c8aSdan /* 2158586b9c8aSdan ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). 21595def0843Sdrh ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file 21605def0843Sdrh ** is greater than sqlite3.pWalArg cast to an integer (the value configured by 2161b033d8b9Sdrh ** wal_autocheckpoint()). 2162586b9c8aSdan */ 2163b033d8b9Sdrh int sqlite3WalDefaultHook( 21645def0843Sdrh void *pClientData, /* Argument */ 2165b033d8b9Sdrh sqlite3 *db, /* Connection */ 21665def0843Sdrh const char *zDb, /* Database */ 2167b033d8b9Sdrh int nFrame /* Size of WAL */ 2168b033d8b9Sdrh ){ 21695def0843Sdrh if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){ 2170f5330357Sdrh sqlite3BeginBenignMalloc(); 21715def0843Sdrh sqlite3_wal_checkpoint(db, zDb); 2172f5330357Sdrh sqlite3EndBenignMalloc(); 21735def0843Sdrh } 21745def0843Sdrh return SQLITE_OK; 2175586b9c8aSdan } 2176b033d8b9Sdrh #endif /* SQLITE_OMIT_WAL */ 2177586b9c8aSdan 2178586b9c8aSdan /* 2179586b9c8aSdan ** Configure an sqlite3_wal_hook() callback to automatically checkpoint 2180586b9c8aSdan ** a database after committing a transaction if there are nFrame or 2181586b9c8aSdan ** more frames in the log file. Passing zero or a negative value as the 2182586b9c8aSdan ** nFrame parameter disables automatic checkpoints entirely. 2183586b9c8aSdan ** 2184586b9c8aSdan ** The callback registered by this function replaces any existing callback 2185586b9c8aSdan ** registered using sqlite3_wal_hook(). Likewise, registering a callback 2186586b9c8aSdan ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism 2187586b9c8aSdan ** configured by this function. 2188586b9c8aSdan */ 2189586b9c8aSdan int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ 2190bd2aaf9aSshaneh #ifdef SQLITE_OMIT_WAL 2191bd2aaf9aSshaneh UNUSED_PARAMETER(db); 2192bd2aaf9aSshaneh UNUSED_PARAMETER(nFrame); 2193bd2aaf9aSshaneh #else 21949ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 21959ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 21969ca95730Sdrh #endif 2197586b9c8aSdan if( nFrame>0 ){ 2198b033d8b9Sdrh sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); 2199586b9c8aSdan }else{ 2200586b9c8aSdan sqlite3_wal_hook(db, 0, 0); 2201586b9c8aSdan } 2202b033d8b9Sdrh #endif 2203586b9c8aSdan return SQLITE_OK; 2204586b9c8aSdan } 2205586b9c8aSdan 2206b0208ccaSpaul /* 22078d22a174Sdan ** Register a callback to be invoked each time a transaction is written 22088d22a174Sdan ** into the write-ahead-log by this database connection. 22098d22a174Sdan */ 2210833bf968Sdrh void *sqlite3_wal_hook( 22118d22a174Sdan sqlite3 *db, /* Attach the hook to this db handle */ 22128d22a174Sdan int(*xCallback)(void *, sqlite3*, const char*, int), 22138d22a174Sdan void *pArg /* First argument passed to xCallback() */ 22148d22a174Sdan ){ 2215b033d8b9Sdrh #ifndef SQLITE_OMIT_WAL 22168d22a174Sdan void *pRet; 22179ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 22189ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 22199ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 22209ca95730Sdrh return 0; 22219ca95730Sdrh } 22229ca95730Sdrh #endif 22238d22a174Sdan sqlite3_mutex_enter(db->mutex); 22247ed91f23Sdrh pRet = db->pWalArg; 22257ed91f23Sdrh db->xWalCallback = xCallback; 22267ed91f23Sdrh db->pWalArg = pArg; 22278d22a174Sdan sqlite3_mutex_leave(db->mutex); 22288d22a174Sdan return pRet; 2229b033d8b9Sdrh #else 2230b033d8b9Sdrh return 0; 2231b033d8b9Sdrh #endif 22328d22a174Sdan } 22338d22a174Sdan 22348d22a174Sdan /* 2235cdc1f049Sdan ** Checkpoint database zDb. 2236586b9c8aSdan */ 2237cdc1f049Sdan int sqlite3_wal_checkpoint_v2( 2238cdc1f049Sdan sqlite3 *db, /* Database handle */ 2239cdc1f049Sdan const char *zDb, /* Name of attached database (or NULL) */ 2240cdc1f049Sdan int eMode, /* SQLITE_CHECKPOINT_* value */ 2241cdc1f049Sdan int *pnLog, /* OUT: Size of WAL log in frames */ 2242cdc1f049Sdan int *pnCkpt /* OUT: Total number of frames checkpointed */ 2243cdc1f049Sdan ){ 2244b033d8b9Sdrh #ifdef SQLITE_OMIT_WAL 2245b033d8b9Sdrh return SQLITE_OK; 2246b033d8b9Sdrh #else 2247586b9c8aSdan int rc; /* Return code */ 22486fcff07fSdan int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ 2249586b9c8aSdan 22509ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 22519ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 22529ca95730Sdrh #endif 22539ca95730Sdrh 22549c5e3680Sdan /* Initialize the output variables to -1 in case an error occurs. */ 22559c5e3680Sdan if( pnLog ) *pnLog = -1; 22569c5e3680Sdan if( pnCkpt ) *pnCkpt = -1; 22579c5e3680Sdan 2258f26a1549Sdan assert( SQLITE_CHECKPOINT_PASSIVE==0 ); 2259f26a1549Sdan assert( SQLITE_CHECKPOINT_FULL==1 ); 2260f26a1549Sdan assert( SQLITE_CHECKPOINT_RESTART==2 ); 2261f26a1549Sdan assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); 2262f26a1549Sdan if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){ 2263bb9a378dSdrh /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint 2264bb9a378dSdrh ** mode: */ 2265cdc1f049Sdan return SQLITE_MISUSE; 2266cdc1f049Sdan } 2267cdc1f049Sdan 2268586b9c8aSdan sqlite3_mutex_enter(db->mutex); 2269af0cfd36Sdan if( zDb && zDb[0] ){ 2270586b9c8aSdan iDb = sqlite3FindDbName(db, zDb); 2271586b9c8aSdan } 2272586b9c8aSdan if( iDb<0 ){ 2273586b9c8aSdan rc = SQLITE_ERROR; 227413f40da3Sdrh sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb); 2275586b9c8aSdan }else{ 2276976b0033Sdan db->busyHandler.nBusy = 0; 2277cdc1f049Sdan rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); 227813f40da3Sdrh sqlite3Error(db, rc); 2279586b9c8aSdan } 2280b033d8b9Sdrh rc = sqlite3ApiExit(db, rc); 22817fb89906Sdan 22827fb89906Sdan /* If there are no active statements, clear the interrupt flag at this 22837fb89906Sdan ** point. */ 22847fb89906Sdan if( db->nVdbeActive==0 ){ 22857fb89906Sdan db->u1.isInterrupted = 0; 22867fb89906Sdan } 22877fb89906Sdan 2288586b9c8aSdan sqlite3_mutex_leave(db->mutex); 2289b033d8b9Sdrh return rc; 2290b033d8b9Sdrh #endif 2291586b9c8aSdan } 2292586b9c8aSdan 2293cdc1f049Sdan 2294cdc1f049Sdan /* 2295cdc1f049Sdan ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points 2296cdc1f049Sdan ** to contains a zero-length string, all attached databases are 2297cdc1f049Sdan ** checkpointed. 2298cdc1f049Sdan */ 2299cdc1f049Sdan int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ 2300bb9a378dSdrh /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to 2301bb9a378dSdrh ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */ 2302cdc1f049Sdan return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0); 2303cdc1f049Sdan } 2304cdc1f049Sdan 2305b033d8b9Sdrh #ifndef SQLITE_OMIT_WAL 2306586b9c8aSdan /* 2307586b9c8aSdan ** Run a checkpoint on database iDb. This is a no-op if database iDb is 2308586b9c8aSdan ** not currently open in WAL mode. 2309586b9c8aSdan ** 23106fcff07fSdan ** If a transaction is open on the database being checkpointed, this 23116fcff07fSdan ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If 23126fcff07fSdan ** an error occurs while running the checkpoint, an SQLite error code is 23136fcff07fSdan ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK. 2314586b9c8aSdan ** 2315586b9c8aSdan ** The mutex on database handle db should be held by the caller. The mutex 2316586b9c8aSdan ** associated with the specific b-tree being checkpointed is taken by 2317586b9c8aSdan ** this function while the checkpoint is running. 23186fcff07fSdan ** 23196fcff07fSdan ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are 23206fcff07fSdan ** checkpointed. If an error is encountered it is returned immediately - 23216fcff07fSdan ** no attempt is made to checkpoint any remaining databases. 2322a58f26f9Sdan ** 23237834551cSdan ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART 23247834551cSdan ** or TRUNCATE. 2325586b9c8aSdan */ 2326cdc1f049Sdan int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){ 23276fcff07fSdan int rc = SQLITE_OK; /* Return code */ 23286fcff07fSdan int i; /* Used to iterate through attached dbs */ 2329f2b8dd58Sdan int bBusy = 0; /* True if SQLITE_BUSY has been encountered */ 2330586b9c8aSdan 2331586b9c8aSdan assert( sqlite3_mutex_held(db->mutex) ); 23329c5e3680Sdan assert( !pnLog || *pnLog==-1 ); 23339c5e3680Sdan assert( !pnCkpt || *pnCkpt==-1 ); 2334586b9c8aSdan 23356fcff07fSdan for(i=0; i<db->nDb && rc==SQLITE_OK; i++){ 23366fcff07fSdan if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){ 2337cdc1f049Sdan rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt); 2338f2b8dd58Sdan pnLog = 0; 2339f2b8dd58Sdan pnCkpt = 0; 2340f2b8dd58Sdan if( rc==SQLITE_BUSY ){ 2341f2b8dd58Sdan bBusy = 1; 2342f2b8dd58Sdan rc = SQLITE_OK; 2343f2b8dd58Sdan } 23446fcff07fSdan } 23456fcff07fSdan } 2346586b9c8aSdan 2347f2b8dd58Sdan return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc; 2348586b9c8aSdan } 2349b033d8b9Sdrh #endif /* SQLITE_OMIT_WAL */ 2350586b9c8aSdan 2351586b9c8aSdan /* 2352d829335eSdanielk1977 ** This function returns true if main-memory should be used instead of 2353d829335eSdanielk1977 ** a temporary file for transient pager files and statement journals. 2354d829335eSdanielk1977 ** The value returned depends on the value of db->temp_store (runtime 2355d829335eSdanielk1977 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The 2356d829335eSdanielk1977 ** following table describes the relationship between these two values 2357d829335eSdanielk1977 ** and this functions return value. 2358d829335eSdanielk1977 ** 2359d829335eSdanielk1977 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database 2360d829335eSdanielk1977 ** ----------------- -------------- ------------------------------ 2361d829335eSdanielk1977 ** 0 any file (return 0) 2362d829335eSdanielk1977 ** 1 1 file (return 0) 2363d829335eSdanielk1977 ** 1 2 memory (return 1) 2364d829335eSdanielk1977 ** 1 0 file (return 0) 2365d829335eSdanielk1977 ** 2 1 file (return 0) 2366d829335eSdanielk1977 ** 2 2 memory (return 1) 2367d829335eSdanielk1977 ** 2 0 memory (return 1) 2368d829335eSdanielk1977 ** 3 any memory (return 1) 2369d829335eSdanielk1977 */ 23701c514148Sdrh int sqlite3TempInMemory(const sqlite3 *db){ 2371d829335eSdanielk1977 #if SQLITE_TEMP_STORE==1 2372d829335eSdanielk1977 return ( db->temp_store==2 ); 2373d829335eSdanielk1977 #endif 2374d829335eSdanielk1977 #if SQLITE_TEMP_STORE==2 2375d829335eSdanielk1977 return ( db->temp_store!=1 ); 2376d829335eSdanielk1977 #endif 2377d829335eSdanielk1977 #if SQLITE_TEMP_STORE==3 2378f5ed7ad6Sdrh UNUSED_PARAMETER(db); 2379d829335eSdanielk1977 return 1; 2380cf697396Sshane #endif 2381cf697396Sshane #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 2382f5ed7ad6Sdrh UNUSED_PARAMETER(db); 2383d829335eSdanielk1977 return 0; 238460a4b538Sshane #endif 2385d829335eSdanielk1977 } 2386d829335eSdanielk1977 2387d829335eSdanielk1977 /* 23884ad1713cSdanielk1977 ** Return UTF-8 encoded English language explanation of the most recent 23894ad1713cSdanielk1977 ** error. 23904ad1713cSdanielk1977 */ 23916622cce3Sdanielk1977 const char *sqlite3_errmsg(sqlite3 *db){ 2392c60d0446Sdrh const char *z; 2393f49661a4Sdrh if( !db ){ 2394fad3039cSmistachkin return sqlite3ErrStr(SQLITE_NOMEM_BKPT); 23954ad1713cSdanielk1977 } 2396d55d57edSdrh if( !sqlite3SafetyCheckSickOrOk(db) ){ 2397413c3d36Sdrh return sqlite3ErrStr(SQLITE_MISUSE_BKPT); 2398e35ee196Sdanielk1977 } 239927641703Sdrh sqlite3_mutex_enter(db->mutex); 2400238746a6Sdanielk1977 if( db->mallocFailed ){ 2401fad3039cSmistachkin z = sqlite3ErrStr(SQLITE_NOMEM_BKPT); 2402238746a6Sdanielk1977 }else{ 2403a3cc007dSdrh testcase( db->pErr==0 ); 24042646da7eSdrh z = (char*)sqlite3_value_text(db->pErr); 2405131c8bc0Sdanielk1977 assert( !db->mallocFailed ); 2406c60d0446Sdrh if( z==0 ){ 2407c60d0446Sdrh z = sqlite3ErrStr(db->errCode); 24086622cce3Sdanielk1977 } 2409238746a6Sdanielk1977 } 2410e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 2411c60d0446Sdrh return z; 2412bfd6cce5Sdanielk1977 } 24136622cce3Sdanielk1977 24146c62608fSdrh #ifndef SQLITE_OMIT_UTF16 24154ad1713cSdanielk1977 /* 24164ad1713cSdanielk1977 ** Return UTF-16 encoded English language explanation of the most recent 24174ad1713cSdanielk1977 ** error. 24184ad1713cSdanielk1977 */ 24196622cce3Sdanielk1977 const void *sqlite3_errmsg16(sqlite3 *db){ 24207c5c3cabSdanielk1977 static const u16 outOfMem[] = { 24217c5c3cabSdanielk1977 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0 24224ad1713cSdanielk1977 }; 24237c5c3cabSdanielk1977 static const u16 misuse[] = { 2424ff4fa772Sdrh 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ', 2425ff4fa772Sdrh 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ', 2426ff4fa772Sdrh 'm', 'i', 's', 'u', 's', 'e', 0 2427e35ee196Sdanielk1977 }; 24284ad1713cSdanielk1977 2429c60d0446Sdrh const void *z; 2430ae7fc49dSdanielk1977 if( !db ){ 24317c5c3cabSdanielk1977 return (void *)outOfMem; 24326622cce3Sdanielk1977 } 2433d55d57edSdrh if( !sqlite3SafetyCheckSickOrOk(db) ){ 24347c5c3cabSdanielk1977 return (void *)misuse; 2435c60d0446Sdrh } 2436e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 2437238746a6Sdanielk1977 if( db->mallocFailed ){ 2438238746a6Sdanielk1977 z = (void *)outOfMem; 2439238746a6Sdanielk1977 }else{ 2440c60d0446Sdrh z = sqlite3_value_text16(db->pErr); 2441c60d0446Sdrh if( z==0 ){ 244213f40da3Sdrh sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode)); 2443c60d0446Sdrh z = sqlite3_value_text16(db->pErr); 2444c60d0446Sdrh } 2445131c8bc0Sdanielk1977 /* A malloc() may have failed within the call to sqlite3_value_text16() 2446131c8bc0Sdanielk1977 ** above. If this is the case, then the db->mallocFailed flag needs to 2447131c8bc0Sdanielk1977 ** be cleared before returning. Do this directly, instead of via 2448131c8bc0Sdanielk1977 ** sqlite3ApiExit(), to avoid setting the database handle error message. 2449131c8bc0Sdanielk1977 */ 24504a642b60Sdrh sqlite3OomClear(db); 2451238746a6Sdanielk1977 } 2452e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 2453c60d0446Sdrh return z; 2454c60d0446Sdrh } 24556c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 24566622cce3Sdanielk1977 2457c60d0446Sdrh /* 24587ddad969Sdanielk1977 ** Return the most recent error code generated by an SQLite routine. If NULL is 24597ddad969Sdanielk1977 ** passed to this function, we assume a malloc() failed during sqlite3_open(). 2460c60d0446Sdrh */ 24616622cce3Sdanielk1977 int sqlite3_errcode(sqlite3 *db){ 24627c36d077Sdanielk1977 if( db && !sqlite3SafetyCheckSickOrOk(db) ){ 2463413c3d36Sdrh return SQLITE_MISUSE_BKPT; 2464c60d0446Sdrh } 246501495b99Sdrh if( !db || db->mallocFailed ){ 2466fad3039cSmistachkin return SQLITE_NOMEM_BKPT; 246701495b99Sdrh } 24684ac285a1Sdrh return db->errCode & db->errMask; 24696622cce3Sdanielk1977 } 247099dfe5ebSdrh int sqlite3_extended_errcode(sqlite3 *db){ 247199dfe5ebSdrh if( db && !sqlite3SafetyCheckSickOrOk(db) ){ 2472413c3d36Sdrh return SQLITE_MISUSE_BKPT; 247399dfe5ebSdrh } 247499dfe5ebSdrh if( !db || db->mallocFailed ){ 2475fad3039cSmistachkin return SQLITE_NOMEM_BKPT; 247699dfe5ebSdrh } 247799dfe5ebSdrh return db->errCode; 247899dfe5ebSdrh } 24791b9f2141Sdrh int sqlite3_system_errno(sqlite3 *db){ 24801b9f2141Sdrh return db ? db->iSysErrno : 0; 24811b9f2141Sdrh } 24826622cce3Sdanielk1977 24830e819f90Sdrh /* 24845dac8432Smistachkin ** Return a string that describes the kind of error specified in the 24855dac8432Smistachkin ** argument. For now, this simply calls the internal sqlite3ErrStr() 24865dac8432Smistachkin ** function. 24875dac8432Smistachkin */ 24885dac8432Smistachkin const char *sqlite3_errstr(int rc){ 24895dac8432Smistachkin return sqlite3ErrStr(rc); 24905dac8432Smistachkin } 24915dac8432Smistachkin 24925dac8432Smistachkin /* 24930e819f90Sdrh ** Create a new collating function for database "db". The name is zName 24940e819f90Sdrh ** and the encoding is enc. 24950e819f90Sdrh */ 24969a30cf65Sdanielk1977 static int createCollation( 24979a30cf65Sdanielk1977 sqlite3* db, 24989a30cf65Sdanielk1977 const char *zName, 2499cea72b2dSshane u8 enc, 25009a30cf65Sdanielk1977 void* pCtx, 2501a9808b31Sdanielk1977 int(*xCompare)(void*,int,const void*,int,const void*), 2502a9808b31Sdanielk1977 void(*xDel)(void*) 25039a30cf65Sdanielk1977 ){ 25049a30cf65Sdanielk1977 CollSeq *pColl; 25057d9bd4e1Sdrh int enc2; 25069a30cf65Sdanielk1977 2507e30f4426Sdrh assert( sqlite3_mutex_held(db->mutex) ); 25089a30cf65Sdanielk1977 25099a30cf65Sdanielk1977 /* If SQLITE_UTF16 is specified as the encoding type, transform this 25109a30cf65Sdanielk1977 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the 25119a30cf65Sdanielk1977 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. 25129a30cf65Sdanielk1977 */ 251351c7d864Sdrh enc2 = enc; 2514ebb32939Sdanielk1977 testcase( enc2==SQLITE_UTF16 ); 2515ebb32939Sdanielk1977 testcase( enc2==SQLITE_UTF16_ALIGNED ); 2516ebb32939Sdanielk1977 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ 25177d9bd4e1Sdrh enc2 = SQLITE_UTF16NATIVE; 25189a30cf65Sdanielk1977 } 251951c7d864Sdrh if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){ 2520413c3d36Sdrh return SQLITE_MISUSE_BKPT; 25219a30cf65Sdanielk1977 } 25229a30cf65Sdanielk1977 25239a30cf65Sdanielk1977 /* Check if this call is removing or replacing an existing collation 25249a30cf65Sdanielk1977 ** sequence. If so, and there are active VMs, return busy. If there 25259a30cf65Sdanielk1977 ** are no active VMs, invalidate any pre-compiled statements. 25269a30cf65Sdanielk1977 */ 2527c4a64facSdrh pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); 25289a30cf65Sdanielk1977 if( pColl && pColl->xCmp ){ 25294f7d3a5fSdrh if( db->nVdbeActive ){ 253013f40da3Sdrh sqlite3ErrorWithMsg(db, SQLITE_BUSY, 2531b309becdSdrh "unable to delete/modify collation sequence due to active statements"); 25329a30cf65Sdanielk1977 return SQLITE_BUSY; 25339a30cf65Sdanielk1977 } 2534ba968dbfSdrh sqlite3ExpirePreparedStatements(db, 0); 2535a9808b31Sdanielk1977 2536a9808b31Sdanielk1977 /* If collation sequence pColl was created directly by a call to 2537a9808b31Sdanielk1977 ** sqlite3_create_collation, and not generated by synthCollSeq(), 2538a9808b31Sdanielk1977 ** then any copies made by synthCollSeq() need to be invalidated. 2539a9808b31Sdanielk1977 ** Also, collation destructor - CollSeq.xDel() - function may need 2540a9808b31Sdanielk1977 ** to be called. 2541a9808b31Sdanielk1977 */ 2542a9808b31Sdanielk1977 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ 2543acbcb7e0Sdrh CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName); 2544a9808b31Sdanielk1977 int j; 2545a9808b31Sdanielk1977 for(j=0; j<3; j++){ 2546a9808b31Sdanielk1977 CollSeq *p = &aColl[j]; 2547a9808b31Sdanielk1977 if( p->enc==pColl->enc ){ 2548a9808b31Sdanielk1977 if( p->xDel ){ 2549a9808b31Sdanielk1977 p->xDel(p->pUser); 2550a9808b31Sdanielk1977 } 2551a9808b31Sdanielk1977 p->xCmp = 0; 2552a9808b31Sdanielk1977 } 2553a9808b31Sdanielk1977 } 2554a9808b31Sdanielk1977 } 25559a30cf65Sdanielk1977 } 25569a30cf65Sdanielk1977 2557c4a64facSdrh pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); 2558fad3039cSmistachkin if( pColl==0 ) return SQLITE_NOMEM_BKPT; 25599a30cf65Sdanielk1977 pColl->xCmp = xCompare; 25609a30cf65Sdanielk1977 pColl->pUser = pCtx; 2561a9808b31Sdanielk1977 pColl->xDel = xDel; 25621bd10f8aSdrh pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); 256313f40da3Sdrh sqlite3Error(db, SQLITE_OK); 25649a30cf65Sdanielk1977 return SQLITE_OK; 25659a30cf65Sdanielk1977 } 25669a30cf65Sdanielk1977 25679a30cf65Sdanielk1977 25686622cce3Sdanielk1977 /* 2569bb4957f8Sdrh ** This array defines hard upper bounds on limit values. The 2570bb4957f8Sdrh ** initializer must be kept in sync with the SQLITE_LIMIT_* 2571bb4957f8Sdrh ** #defines in sqlite3.h. 2572bb4957f8Sdrh */ 2573b1a6c3c1Sdrh static const int aHardLimit[] = { 2574bb4957f8Sdrh SQLITE_MAX_LENGTH, 2575bb4957f8Sdrh SQLITE_MAX_SQL_LENGTH, 2576bb4957f8Sdrh SQLITE_MAX_COLUMN, 2577bb4957f8Sdrh SQLITE_MAX_EXPR_DEPTH, 2578bb4957f8Sdrh SQLITE_MAX_COMPOUND_SELECT, 2579bb4957f8Sdrh SQLITE_MAX_VDBE_OP, 2580bb4957f8Sdrh SQLITE_MAX_FUNCTION_ARG, 2581bb4957f8Sdrh SQLITE_MAX_ATTACHED, 2582bb4957f8Sdrh SQLITE_MAX_LIKE_PATTERN_LENGTH, 25839f959b07Sdrh SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */ 2584417168adSdrh SQLITE_MAX_TRIGGER_DEPTH, 2585111544cbSdrh SQLITE_MAX_WORKER_THREADS, 2586bb4957f8Sdrh }; 2587bb4957f8Sdrh 2588bb4957f8Sdrh /* 2589bb4957f8Sdrh ** Make sure the hard limits are set to reasonable values 2590bb4957f8Sdrh */ 2591bb4957f8Sdrh #if SQLITE_MAX_LENGTH<100 2592bb4957f8Sdrh # error SQLITE_MAX_LENGTH must be at least 100 2593bb4957f8Sdrh #endif 2594bb4957f8Sdrh #if SQLITE_MAX_SQL_LENGTH<100 2595bb4957f8Sdrh # error SQLITE_MAX_SQL_LENGTH must be at least 100 2596bb4957f8Sdrh #endif 2597bb4957f8Sdrh #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH 2598bb4957f8Sdrh # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH 2599bb4957f8Sdrh #endif 2600bb4957f8Sdrh #if SQLITE_MAX_COMPOUND_SELECT<2 2601bb4957f8Sdrh # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 2602bb4957f8Sdrh #endif 2603bb4957f8Sdrh #if SQLITE_MAX_VDBE_OP<40 2604bb4957f8Sdrh # error SQLITE_MAX_VDBE_OP must be at least 40 2605bb4957f8Sdrh #endif 260680738d9cSdrh #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127 260780738d9cSdrh # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127 2608bb4957f8Sdrh #endif 2609d08b2798Sdrh #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125 2610d08b2798Sdrh # error SQLITE_MAX_ATTACHED must be between 0 and 125 2611bb4957f8Sdrh #endif 2612bb4957f8Sdrh #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 2613bb4957f8Sdrh # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 2614bb4957f8Sdrh #endif 261570a8ca3cSdrh #if SQLITE_MAX_COLUMN>32767 261670a8ca3cSdrh # error SQLITE_MAX_COLUMN must not exceed 32767 261770a8ca3cSdrh #endif 2618417168adSdrh #if SQLITE_MAX_TRIGGER_DEPTH<1 2619417168adSdrh # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 2620417168adSdrh #endif 2621111544cbSdrh #if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50 2622111544cbSdrh # error SQLITE_MAX_WORKER_THREADS must be between 0 and 50 2623111544cbSdrh #endif 2624bb4957f8Sdrh 2625bb4957f8Sdrh 2626bb4957f8Sdrh /* 2627bb4957f8Sdrh ** Change the value of a limit. Report the old value. 2628bb4957f8Sdrh ** If an invalid limit index is supplied, report -1. 2629bb4957f8Sdrh ** Make no changes but still report the old value if the 2630bb4957f8Sdrh ** new limit is negative. 2631bb4957f8Sdrh ** 2632bb4957f8Sdrh ** A new lower limit does not shrink existing constructs. 2633bb4957f8Sdrh ** It merely prevents new constructs that exceed the limit 2634bb4957f8Sdrh ** from forming. 2635bb4957f8Sdrh */ 2636bb4957f8Sdrh int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ 2637bb4957f8Sdrh int oldLimit; 26384e93f5bfSdrh 26399ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 26409ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 26419ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 26429ca95730Sdrh return -1; 26439ca95730Sdrh } 26449ca95730Sdrh #endif 26454e93f5bfSdrh 26464e93f5bfSdrh /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME 26474e93f5bfSdrh ** there is a hard upper bound set at compile-time by a C preprocessor 26484e93f5bfSdrh ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to 26494e93f5bfSdrh ** "_MAX_".) 26504e93f5bfSdrh */ 26514e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); 26524e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); 26534e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); 26544e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); 26554e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); 26564e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); 26574e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); 26584e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); 26594e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== 26604e93f5bfSdrh SQLITE_MAX_LIKE_PATTERN_LENGTH ); 26614e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); 26624e93f5bfSdrh assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); 2663111544cbSdrh assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS ); 2664111544cbSdrh assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) ); 26654e93f5bfSdrh 26664e93f5bfSdrh 2667521cc849Sdrh if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ 2668bb4957f8Sdrh return -1; 2669bb4957f8Sdrh } 2670bb4957f8Sdrh oldLimit = db->aLimit[limitId]; 26714e93f5bfSdrh if( newLimit>=0 ){ /* IMP: R-52476-28732 */ 2672f47ce56cSdrh if( newLimit>aHardLimit[limitId] ){ 26734e93f5bfSdrh newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ 2674bb4957f8Sdrh } 2675bb4957f8Sdrh db->aLimit[limitId] = newLimit; 2676bb4957f8Sdrh } 26774e93f5bfSdrh return oldLimit; /* IMP: R-53341-35419 */ 2678bb4957f8Sdrh } 2679bb4957f8Sdrh 2680bb4957f8Sdrh /* 2681286ab7c2Sdan ** This function is used to parse both URIs and non-URI filenames passed by the 2682286ab7c2Sdan ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database 2683286ab7c2Sdan ** URIs specified as part of ATTACH statements. 2684286ab7c2Sdan ** 2685286ab7c2Sdan ** The first argument to this function is the name of the VFS to use (or 2686286ab7c2Sdan ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx" 2687286ab7c2Sdan ** query parameter. The second argument contains the URI (or non-URI filename) 2688286ab7c2Sdan ** itself. When this function is called the *pFlags variable should contain 2689286ab7c2Sdan ** the default flags to open the database handle with. The value stored in 2690286ab7c2Sdan ** *pFlags may be updated before returning if the URI filename contains 2691286ab7c2Sdan ** "cache=xxx" or "mode=xxx" query parameters. 2692286ab7c2Sdan ** 2693286ab7c2Sdan ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to 2694286ab7c2Sdan ** the VFS that should be used to open the database file. *pzFile is set to 2695286ab7c2Sdan ** point to a buffer containing the name of the file to open. It is the 2696286ab7c2Sdan ** responsibility of the caller to eventually call sqlite3_free() to release 2697286ab7c2Sdan ** this buffer. 2698286ab7c2Sdan ** 2699286ab7c2Sdan ** If an error occurs, then an SQLite error code is returned and *pzErrMsg 2700286ab7c2Sdan ** may be set to point to a buffer containing an English language error 2701286ab7c2Sdan ** message. It is the responsibility of the caller to eventually release 2702286ab7c2Sdan ** this buffer by calling sqlite3_free(). 2703cd74b611Sdan */ 2704cd74b611Sdan int sqlite3ParseUri( 2705cd74b611Sdan const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */ 2706cd74b611Sdan const char *zUri, /* Nul-terminated URI to parse */ 2707522c26fbSdrh unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */ 2708cd74b611Sdan sqlite3_vfs **ppVfs, /* OUT: VFS to use */ 2709cd74b611Sdan char **pzFile, /* OUT: Filename component of URI */ 2710cd74b611Sdan char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */ 2711cd74b611Sdan ){ 271278e9dd2bSdan int rc = SQLITE_OK; 2713522c26fbSdrh unsigned int flags = *pFlags; 2714cd74b611Sdan const char *zVfs = zDefaultVfs; 2715cd74b611Sdan char *zFile; 2716de941c37Sdrh char c; 2717cd74b611Sdan int nUri = sqlite3Strlen30(zUri); 2718cd74b611Sdan 2719cd74b611Sdan assert( *pzErrMsg==0 ); 2720cd74b611Sdan 27218790b6e8Sdrh if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ 27228790b6e8Sdrh || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ 272300729cbaSdrh && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ 2724cd74b611Sdan ){ 27255de15374Sdan char *zOpt; 2726cd74b611Sdan int eState; /* Parser state when parsing URI */ 2727cd74b611Sdan int iIn; /* Input character index */ 2728cd74b611Sdan int iOut = 0; /* Output character index */ 2729f3cdcdccSdrh u64 nByte = nUri+2; /* Bytes of space to allocate */ 2730cd74b611Sdan 2731286ab7c2Sdan /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen 2732286ab7c2Sdan ** method that there may be extra parameters following the file-name. */ 2733286ab7c2Sdan flags |= SQLITE_OPEN_URI; 2734286ab7c2Sdan 2735286ab7c2Sdan for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&'); 2736f3cdcdccSdrh zFile = sqlite3_malloc64(nByte); 2737fad3039cSmistachkin if( !zFile ) return SQLITE_NOMEM_BKPT; 2738cd74b611Sdan 2739869c0409Sdrh iIn = 5; 27404a4b1389Sdrh #ifdef SQLITE_ALLOW_URI_AUTHORITY 27414a4b1389Sdrh if( strncmp(zUri+5, "///", 3)==0 ){ 27424a4b1389Sdrh iIn = 7; 27434a4b1389Sdrh /* The following condition causes URIs with five leading / characters 27444a4b1389Sdrh ** like file://///host/path to be converted into UNCs like //host/path. 27454a4b1389Sdrh ** The correct URI for that UNC has only two or four leading / characters 27464a4b1389Sdrh ** file://host/path or file:////host/path. But 5 leading slashes is a 27474a4b1389Sdrh ** common error, we are told, so we handle it as a special case. */ 27484a4b1389Sdrh if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; } 27494a4b1389Sdrh }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){ 27504a4b1389Sdrh iIn = 16; 27514a4b1389Sdrh } 27524a4b1389Sdrh #else 2753cd74b611Sdan /* Discard the scheme and authority segments of the URI. */ 2754cd74b611Sdan if( zUri[5]=='/' && zUri[6]=='/' ){ 2755cd74b611Sdan iIn = 7; 2756cd74b611Sdan while( zUri[iIn] && zUri[iIn]!='/' ) iIn++; 27573b18a9a3Sdan if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){ 27583b18a9a3Sdan *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", 27593b18a9a3Sdan iIn-7, &zUri[7]); 27603b18a9a3Sdan rc = SQLITE_ERROR; 27613b18a9a3Sdan goto parse_uri_out; 27623b18a9a3Sdan } 2763cd74b611Sdan } 2764869c0409Sdrh #endif 2765cd74b611Sdan 2766cd74b611Sdan /* Copy the filename and any query parameters into the zFile buffer. 2767cd74b611Sdan ** Decode %HH escape codes along the way. 2768cd74b611Sdan ** 2769cd74b611Sdan ** Within this loop, variable eState may be set to 0, 1 or 2, depending 2770cd74b611Sdan ** on the parsing context. As follows: 2771cd74b611Sdan ** 2772cd74b611Sdan ** 0: Parsing file-name. 2773cd74b611Sdan ** 1: Parsing name section of a name=value query parameter. 2774cd74b611Sdan ** 2: Parsing value section of a name=value query parameter. 2775cd74b611Sdan */ 2776cd74b611Sdan eState = 0; 2777de941c37Sdrh while( (c = zUri[iIn])!=0 && c!='#' ){ 2778de941c37Sdrh iIn++; 2779cd74b611Sdan if( c=='%' 2780cd74b611Sdan && sqlite3Isxdigit(zUri[iIn]) 2781cd74b611Sdan && sqlite3Isxdigit(zUri[iIn+1]) 2782cd74b611Sdan ){ 27836d49e256Sdan int octet = (sqlite3HexToInt(zUri[iIn++]) << 4); 27846d49e256Sdan octet += sqlite3HexToInt(zUri[iIn++]); 2785cd74b611Sdan 27866d49e256Sdan assert( octet>=0 && octet<256 ); 27876d49e256Sdan if( octet==0 ){ 27885c35e903Sdan #ifndef SQLITE_ENABLE_URI_00_ERROR 27895de15374Sdan /* This branch is taken when "%00" appears within the URI. In this 27905de15374Sdan ** case we ignore all text in the remainder of the path, name or 27915de15374Sdan ** value currently being parsed. So ignore the current character 27925de15374Sdan ** and skip to the next "?", "=" or "&", as appropriate. */ 2793de941c37Sdrh while( (c = zUri[iIn])!=0 && c!='#' 2794de941c37Sdrh && (eState!=0 || c!='?') 2795de941c37Sdrh && (eState!=1 || (c!='=' && c!='&')) 2796de941c37Sdrh && (eState!=2 || c!='&') 27975de15374Sdan ){ 27985de15374Sdan iIn++; 2799cd74b611Sdan } 28005de15374Sdan continue; 28015c35e903Sdan #else 28025c35e903Sdan /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */ 28035c35e903Sdan *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri"); 28045c35e903Sdan rc = SQLITE_ERROR; 28055c35e903Sdan goto parse_uri_out; 28065c35e903Sdan #endif 28075de15374Sdan } 28086d49e256Sdan c = octet; 28095de15374Sdan }else if( eState==1 && (c=='&' || c=='=') ){ 28105de15374Sdan if( zFile[iOut-1]==0 ){ 28115de15374Sdan /* An empty option name. Ignore this option altogether. */ 28125de15374Sdan while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++; 28135de15374Sdan continue; 28145de15374Sdan } 28155de15374Sdan if( c=='&' ){ 28165de15374Sdan zFile[iOut++] = '\0'; 28175de15374Sdan }else{ 28185de15374Sdan eState = 2; 28195de15374Sdan } 2820cd74b611Sdan c = 0; 28215de15374Sdan }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){ 28225de15374Sdan c = 0; 2823cd74b611Sdan eState = 1; 2824cd74b611Sdan } 2825cd74b611Sdan zFile[iOut++] = c; 2826cd74b611Sdan } 2827cd74b611Sdan if( eState==1 ) zFile[iOut++] = '\0'; 2828cd74b611Sdan zFile[iOut++] = '\0'; 2829cd74b611Sdan zFile[iOut++] = '\0'; 2830cd74b611Sdan 2831cd74b611Sdan /* Check if there were any options specified that should be interpreted 2832cd74b611Sdan ** here. Options that are interpreted here include "vfs" and those that 2833cd74b611Sdan ** correspond to flags that may be passed to the sqlite3_open_v2() 2834cd74b611Sdan ** method. */ 28355de15374Sdan zOpt = &zFile[sqlite3Strlen30(zFile)+1]; 2836cd74b611Sdan while( zOpt[0] ){ 2837cd74b611Sdan int nOpt = sqlite3Strlen30(zOpt); 2838cd74b611Sdan char *zVal = &zOpt[nOpt+1]; 2839cd74b611Sdan int nVal = sqlite3Strlen30(zVal); 2840cd74b611Sdan 2841286ab7c2Sdan if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){ 2842cd74b611Sdan zVfs = zVal; 2843cd74b611Sdan }else{ 284478e9dd2bSdan struct OpenMode { 284578e9dd2bSdan const char *z; 284678e9dd2bSdan int mode; 284778e9dd2bSdan } *aMode = 0; 284845caededSdrh char *zModeType = 0; 284945caededSdrh int mask = 0; 285045caededSdrh int limit = 0; 285178e9dd2bSdan 2852286ab7c2Sdan if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){ 285378e9dd2bSdan static struct OpenMode aCacheMode[] = { 285478e9dd2bSdan { "shared", SQLITE_OPEN_SHAREDCACHE }, 285578e9dd2bSdan { "private", SQLITE_OPEN_PRIVATECACHE }, 285678e9dd2bSdan { 0, 0 } 285778e9dd2bSdan }; 285878e9dd2bSdan 285978e9dd2bSdan mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE; 286078e9dd2bSdan aMode = aCacheMode; 286178e9dd2bSdan limit = mask; 286278e9dd2bSdan zModeType = "cache"; 286378e9dd2bSdan } 2864286ab7c2Sdan if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){ 286578e9dd2bSdan static struct OpenMode aOpenMode[] = { 286678e9dd2bSdan { "ro", SQLITE_OPEN_READONLY }, 286778e9dd2bSdan { "rw", SQLITE_OPEN_READWRITE }, 286878e9dd2bSdan { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE }, 28690b8dcfa2Sdan { "memory", SQLITE_OPEN_MEMORY }, 287078e9dd2bSdan { 0, 0 } 287178e9dd2bSdan }; 287278e9dd2bSdan 28739c67b2aaSdrh mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE 28749c67b2aaSdrh | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY; 287578e9dd2bSdan aMode = aOpenMode; 287678e9dd2bSdan limit = mask & flags; 287778e9dd2bSdan zModeType = "access"; 287878e9dd2bSdan } 287978e9dd2bSdan 288078e9dd2bSdan if( aMode ){ 288178e9dd2bSdan int i; 288278e9dd2bSdan int mode = 0; 288378e9dd2bSdan for(i=0; aMode[i].z; i++){ 288478e9dd2bSdan const char *z = aMode[i].z; 2885522c26fbSdrh if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){ 288678e9dd2bSdan mode = aMode[i].mode; 288778e9dd2bSdan break; 2888cd74b611Sdan } 2889cd74b611Sdan } 289078e9dd2bSdan if( mode==0 ){ 289178e9dd2bSdan *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal); 289278e9dd2bSdan rc = SQLITE_ERROR; 289378e9dd2bSdan goto parse_uri_out; 289478e9dd2bSdan } 28959c67b2aaSdrh if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){ 2896de941c37Sdrh *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s", 2897de941c37Sdrh zModeType, zVal); 289878e9dd2bSdan rc = SQLITE_PERM; 289978e9dd2bSdan goto parse_uri_out; 290078e9dd2bSdan } 290178e9dd2bSdan flags = (flags & ~mask) | mode; 2902cd74b611Sdan } 2903cd74b611Sdan } 2904cd74b611Sdan 2905cd74b611Sdan zOpt = &zVal[nVal+1]; 2906cd74b611Sdan } 2907cd74b611Sdan 2908cd74b611Sdan }else{ 2909f3cdcdccSdrh zFile = sqlite3_malloc64(nUri+2); 2910fad3039cSmistachkin if( !zFile ) return SQLITE_NOMEM_BKPT; 2911895decf6Sdan if( nUri ){ 2912cd74b611Sdan memcpy(zFile, zUri, nUri); 2913895decf6Sdan } 2914cd74b611Sdan zFile[nUri] = '\0'; 2915cd74b611Sdan zFile[nUri+1] = '\0'; 29164ab9d254Sdrh flags &= ~SQLITE_OPEN_URI; 2917cd74b611Sdan } 2918cd74b611Sdan 2919cd74b611Sdan *ppVfs = sqlite3_vfs_find(zVfs); 2920cd74b611Sdan if( *ppVfs==0 ){ 2921cd74b611Sdan *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs); 292278e9dd2bSdan rc = SQLITE_ERROR; 292378e9dd2bSdan } 292478e9dd2bSdan parse_uri_out: 292578e9dd2bSdan if( rc!=SQLITE_OK ){ 29265de15374Sdan sqlite3_free(zFile); 292778e9dd2bSdan zFile = 0; 2928cd74b611Sdan } 2929cd74b611Sdan *pFlags = flags; 2930cd74b611Sdan *pzFile = zFile; 293178e9dd2bSdan return rc; 2932cd74b611Sdan } 2933cd74b611Sdan 2934cd74b611Sdan 2935cd74b611Sdan /* 29364ad1713cSdanielk1977 ** This routine does the work of opening a database on behalf of 29374ad1713cSdanielk1977 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" 2938ee570fa4Sdrh ** is UTF-8 encoded. 29394ad1713cSdanielk1977 */ 29404ad1713cSdanielk1977 static int openDatabase( 29414ad1713cSdanielk1977 const char *zFilename, /* Database filename UTF-8 encoded */ 2942605264d2Sdrh sqlite3 **ppDb, /* OUT: Returned database handle */ 2943522c26fbSdrh unsigned int flags, /* Operational flags */ 2944605264d2Sdrh const char *zVfs /* Name of the VFS to use */ 29454ad1713cSdanielk1977 ){ 2946cd74b611Sdan sqlite3 *db; /* Store allocated handle here */ 2947cd74b611Sdan int rc; /* Return code */ 2948cd74b611Sdan int isThreadsafe; /* True for threadsafe connections */ 2949cd74b611Sdan char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ 2950cd74b611Sdan char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ 29514ad1713cSdanielk1977 29529ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 29539ca95730Sdrh if( ppDb==0 ) return SQLITE_MISUSE_BKPT; 29549ca95730Sdrh #endif 2955e0d0f8eeSdrh *ppDb = 0; 295640257ffdSdrh #ifndef SQLITE_OMIT_AUTOINIT 295740257ffdSdrh rc = sqlite3_initialize(); 295840257ffdSdrh if( rc ) return rc; 295940257ffdSdrh #endif 296040257ffdSdrh 2961039963adSdrh if( sqlite3GlobalConfig.bCoreMutex==0 ){ 29629a6284c1Sdanielk1977 isThreadsafe = 0; 2963039963adSdrh }else if( flags & SQLITE_OPEN_NOMUTEX ){ 2964039963adSdrh isThreadsafe = 0; 2965039963adSdrh }else if( flags & SQLITE_OPEN_FULLMUTEX ){ 2966039963adSdrh isThreadsafe = 1; 2967039963adSdrh }else{ 2968039963adSdrh isThreadsafe = sqlite3GlobalConfig.bFullMutex; 29699a6284c1Sdanielk1977 } 29708385becfSdan 2971f1f12688Sdrh if( flags & SQLITE_OPEN_PRIVATECACHE ){ 2972f4cfac9dSdrh flags &= ~SQLITE_OPEN_SHAREDCACHE; 2973f1f12688Sdrh }else if( sqlite3GlobalConfig.sharedCacheEnabled ){ 2974f1f12688Sdrh flags |= SQLITE_OPEN_SHAREDCACHE; 2975f1f12688Sdrh } 29769a6284c1Sdanielk1977 2977b25a9f46Sdrh /* Remove harmful bits from the flags parameter 2978b25a9f46Sdrh ** 2979b25a9f46Sdrh ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were 2980b25a9f46Sdrh ** dealt with in the previous code block. Besides these, the only 2981b25a9f46Sdrh ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, 298203e1b40bSdrh ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE, 298303e1b40bSdrh ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask 2984b25a9f46Sdrh ** off all other flags. 2985b25a9f46Sdrh */ 2986a4267dccSdrh flags &= ~( SQLITE_OPEN_DELETEONCLOSE | 2987b25a9f46Sdrh SQLITE_OPEN_EXCLUSIVE | 2988a4267dccSdrh SQLITE_OPEN_MAIN_DB | 2989a4267dccSdrh SQLITE_OPEN_TEMP_DB | 2990a4267dccSdrh SQLITE_OPEN_TRANSIENT_DB | 2991a4267dccSdrh SQLITE_OPEN_MAIN_JOURNAL | 2992a4267dccSdrh SQLITE_OPEN_TEMP_JOURNAL | 2993a4267dccSdrh SQLITE_OPEN_SUBJOURNAL | 29949a6284c1Sdanielk1977 SQLITE_OPEN_MASTER_JOURNAL | 2995039963adSdrh SQLITE_OPEN_NOMUTEX | 2996357b5f97Sdrh SQLITE_OPEN_FULLMUTEX | 2997357b5f97Sdrh SQLITE_OPEN_WAL 2998a4267dccSdrh ); 2999a4267dccSdrh 30004ad1713cSdanielk1977 /* Allocate the sqlite data structure */ 300117435752Sdrh db = sqlite3MallocZero( sizeof(sqlite3) ); 30024ad1713cSdanielk1977 if( db==0 ) goto opendb_out; 30038385becfSdan if( isThreadsafe 30048385becfSdan #ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS 30058385becfSdan || sqlite3GlobalConfig.bCoreMutex 30068385becfSdan #endif 30078385becfSdan ){ 300859f8c08eSdanielk1977 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); 3009605264d2Sdrh if( db->mutex==0 ){ 3010153c62c4Sdrh sqlite3_free(db); 3011153c62c4Sdrh db = 0; 3012605264d2Sdrh goto opendb_out; 3013605264d2Sdrh } 30148385becfSdan if( isThreadsafe==0 ){ 30158385becfSdan sqlite3MutexWarnOnContention(db->mutex); 30168385becfSdan } 301759f8c08eSdanielk1977 } 301827641703Sdrh sqlite3_mutex_enter(db->mutex); 30194ac285a1Sdrh db->errMask = 0xff; 30204ad1713cSdanielk1977 db->nDb = 2; 3021153c62c4Sdrh db->magic = SQLITE_MAGIC_BUSY; 30224ad1713cSdanielk1977 db->aDb = db->aDbStatic; 3023633e6d57Sdrh 3024bb4957f8Sdrh assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); 3025bb4957f8Sdrh memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); 30266b2129aaSdrh db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS; 30271d850a72Sdanielk1977 db->autoCommit = 1; 3028ddac25c7Sdrh db->nextAutovac = -1; 30299b4c59faSdrh db->szMmap = sqlite3GlobalConfig.szMmap; 3030f653d782Sdanielk1977 db->nextPagesize = 0; 30318930c2abSdan db->nMaxSorterMmap = 0x7FFFFFFF; 303240c3941cSdrh db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill 30331d1f07dfSdan #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX 3034986b3879Sdrh | SQLITE_AutoIndex 3035986b3879Sdrh #endif 3036883ad049Sdrh #if SQLITE_DEFAULT_CKPTFULLFSYNC 3037883ad049Sdrh | SQLITE_CkptFullFSync 3038883ad049Sdrh #endif 303976fe8032Sdrh #if SQLITE_DEFAULT_FILE_FORMAT<4 304076fe8032Sdrh | SQLITE_LegacyFileFmt 304176fe8032Sdrh #endif 304256424db4Sdrh #ifdef SQLITE_ENABLE_LOAD_EXTENSION 304356424db4Sdrh | SQLITE_LoadExtension 304456424db4Sdrh #endif 30455bde73c4Sdan #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS 30465e1a2788Sdan | SQLITE_RecTriggers 304776d462eeSdan #endif 3048a4bfd7fdSdrh #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS 3049a4bfd7fdSdrh | SQLITE_ForeignKeys 3050a4bfd7fdSdrh #endif 3051f5471925Sdrh #if defined(SQLITE_REVERSE_UNORDERED_SELECTS) 3052f5471925Sdrh | SQLITE_ReverseOrder 3053f5471925Sdrh #endif 30541421d980Sdrh #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) 30551421d980Sdrh | SQLITE_CellSizeCk 30561421d980Sdrh #endif 3057d42908fbSdrh #if defined(SQLITE_ENABLE_FTS3_TOKENIZER) 3058d42908fbSdrh | SQLITE_Fts3Tokenizer 3059d42908fbSdrh #endif 3060169dd928Sdrh #if defined(SQLITE_ENABLE_QPSG) 3061169dd928Sdrh | SQLITE_EnableQPSG 3062169dd928Sdrh #endif 306376fe8032Sdrh ; 3064e61922a6Sdrh sqlite3HashInit(&db->aCollSeq); 3065b9bb7c18Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 3066e61922a6Sdrh sqlite3HashInit(&db->aModule); 3067b9bb7c18Sdrh #endif 3068da184236Sdanielk1977 30690202b29eSdanielk1977 /* Add the default collation sequence BINARY. BINARY works for both UTF-8 30700202b29eSdanielk1977 ** and UTF-16, so add a version for each to avoid any unnecessary 30710202b29eSdanielk1977 ** conversions. The only error that can occur here is a malloc() failure. 30725e3b49bcSdrh ** 30735e3b49bcSdrh ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating 30745e3b49bcSdrh ** functions: 30750202b29eSdanielk1977 */ 3076f19aa5faSdrh createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0); 3077f19aa5faSdrh createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0); 3078f19aa5faSdrh createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0); 30795e3b49bcSdrh createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); 30804dd65e0fSdrh createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); 308177db4c05Sdrh if( db->mallocFailed ){ 30820202b29eSdanielk1977 goto opendb_out; 30830202b29eSdanielk1977 } 30845e3b49bcSdrh /* EVIDENCE-OF: R-08308-17224 The default collating function for all 30855e3b49bcSdrh ** strings is BINARY. 30865e3b49bcSdrh */ 3087f19aa5faSdrh db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0); 308877db4c05Sdrh assert( db->pDfltColl!=0 ); 30890202b29eSdanielk1977 3090ff4fa772Sdrh /* Parse the filename/URI argument 3091ff4fa772Sdrh ** 3092ff4fa772Sdrh ** Only allow sensible combinations of bits in the flags argument. 3093ff4fa772Sdrh ** Throw an error if any non-sense combination is used. If we 3094ff4fa772Sdrh ** do not block illegal combinations here, it could trigger 3095ff4fa772Sdrh ** assert() statements in deeper layers. Sensible combinations 3096ff4fa772Sdrh ** are: 3097ff4fa772Sdrh ** 3098ff4fa772Sdrh ** 1: SQLITE_OPEN_READONLY 3099ff4fa772Sdrh ** 2: SQLITE_OPEN_READWRITE 3100ff4fa772Sdrh ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE 3101ff4fa772Sdrh */ 310219432996Sdan db->openFlags = flags; 3103ff4fa772Sdrh assert( SQLITE_OPEN_READONLY == 0x01 ); 3104ff4fa772Sdrh assert( SQLITE_OPEN_READWRITE == 0x02 ); 3105ff4fa772Sdrh assert( SQLITE_OPEN_CREATE == 0x04 ); 3106ff4fa772Sdrh testcase( (1<<(flags&7))==0x02 ); /* READONLY */ 3107ff4fa772Sdrh testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ 3108ff4fa772Sdrh testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ 3109ff4fa772Sdrh if( ((1<<(flags&7)) & 0x46)==0 ){ 3110ff4fa772Sdrh rc = SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ 3111ff4fa772Sdrh }else{ 3112cd74b611Sdan rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); 3113ff4fa772Sdrh } 3114cd74b611Sdan if( rc!=SQLITE_OK ){ 31154a642b60Sdrh if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); 311613f40da3Sdrh sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg); 3117cd74b611Sdan sqlite3_free(zErrMsg); 3118cd74b611Sdan goto opendb_out; 3119cd74b611Sdan } 3120cd74b611Sdan 31214ad1713cSdanielk1977 /* Open the backend database driver */ 31223a6d8aecSdan rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0, 312375c014c3Sdrh flags | SQLITE_OPEN_MAIN_DB); 31244ad1713cSdanielk1977 if( rc!=SQLITE_OK ){ 312545e60aabSdanielk1977 if( rc==SQLITE_IOERR_NOMEM ){ 3126fad3039cSmistachkin rc = SQLITE_NOMEM_BKPT; 312745e60aabSdanielk1977 } 312813f40da3Sdrh sqlite3Error(db, rc); 31294ad1713cSdanielk1977 goto opendb_out; 31304ad1713cSdanielk1977 } 31310235a033Sdan sqlite3BtreeEnter(db->aDb[0].pBt); 313217435752Sdrh db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); 31339bd3cc46Sdrh if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db); 31340235a033Sdan sqlite3BtreeLeave(db->aDb[0].pBt); 313517435752Sdrh db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); 313614db2665Sdanielk1977 3137e872d513Sdrh /* The default safety_level for the main database is FULL; for the temp 3138e872d513Sdrh ** database it is OFF. This matches the pager layer defaults. 313953c0f748Sdanielk1977 */ 314069c33826Sdrh db->aDb[0].zDbSName = "main"; 3141c2ae2073Sdrh db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; 314269c33826Sdrh db->aDb[1].zDbSName = "temp"; 3143e872d513Sdrh db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF; 314453c0f748Sdanielk1977 3145832a58a6Sdanielk1977 db->magic = SQLITE_MAGIC_OPEN; 314617435752Sdrh if( db->mallocFailed ){ 3147832a58a6Sdanielk1977 goto opendb_out; 3148832a58a6Sdanielk1977 } 3149832a58a6Sdanielk1977 31508e227875Sdanielk1977 /* Register all built-in functions, but do not attempt to read the 31518e227875Sdanielk1977 ** database schema yet. This is delayed until the first time the database 31528e227875Sdanielk1977 ** is accessed. 31538e227875Sdanielk1977 */ 315413f40da3Sdrh sqlite3Error(db, SQLITE_OK); 315580738d9cSdrh sqlite3RegisterPerConnectionBuiltinFunctions(db); 3156c9e75fb2Sdan rc = sqlite3_errcode(db); 3157c9e75fb2Sdan 3158c9e75fb2Sdan #ifdef SQLITE_ENABLE_FTS5 3159c9e75fb2Sdan /* Register any built-in FTS5 module before loading the automatic 3160c9e75fb2Sdan ** extensions. This allows automatic extensions to register FTS5 3161c9e75fb2Sdan ** tokenizers and auxiliary functions. */ 3162c9e75fb2Sdan if( !db->mallocFailed && rc==SQLITE_OK ){ 3163c9e75fb2Sdan rc = sqlite3Fts5Init(db); 3164c9e75fb2Sdan } 3165c9e75fb2Sdan #endif 31664ad1713cSdanielk1977 31671409be69Sdrh /* Load automatic extensions - extensions that have been registered 31681409be69Sdrh ** using the sqlite3_automatic_extension() API. 31691409be69Sdrh */ 3170e5077c12Sdrh if( rc==SQLITE_OK ){ 31717aaa8786Sdrh sqlite3AutoLoadExtensions(db); 31727aaa8786Sdrh rc = sqlite3_errcode(db); 31737aaa8786Sdrh if( rc!=SQLITE_OK ){ 3174832a58a6Sdanielk1977 goto opendb_out; 3175832a58a6Sdanielk1977 } 3176e5077c12Sdrh } 31771409be69Sdrh 3178aa29c135Sdrh #ifdef SQLITE_ENABLE_FTS1 317917435752Sdrh if( !db->mallocFailed ){ 3180aa29c135Sdrh extern int sqlite3Fts1Init(sqlite3*); 3181832a58a6Sdanielk1977 rc = sqlite3Fts1Init(db); 3182aa29c135Sdrh } 3183aa29c135Sdrh #endif 3184aa29c135Sdrh 3185a26cf577Sshess #ifdef SQLITE_ENABLE_FTS2 318617435752Sdrh if( !db->mallocFailed && rc==SQLITE_OK ){ 3187a26cf577Sshess extern int sqlite3Fts2Init(sqlite3*); 3188832a58a6Sdanielk1977 rc = sqlite3Fts2Init(db); 3189a26cf577Sshess } 3190a26cf577Sshess #endif 3191a26cf577Sshess 319250065656Sdrh #ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */ 319369c4ae24Sshess if( !db->mallocFailed && rc==SQLITE_OK ){ 319469c4ae24Sshess rc = sqlite3Fts3Init(db); 319569c4ae24Sshess } 319669c4ae24Sshess #endif 319769c4ae24Sshess 319821540ae4Sdan #if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS) 319917435752Sdrh if( !db->mallocFailed && rc==SQLITE_OK ){ 3200832a58a6Sdanielk1977 rc = sqlite3IcuInit(db); 320183852accSdanielk1977 } 320283852accSdanielk1977 #endif 3203ebaecc14Sdanielk1977 3204ebaecc14Sdanielk1977 #ifdef SQLITE_ENABLE_RTREE 3205ebaecc14Sdanielk1977 if( !db->mallocFailed && rc==SQLITE_OK){ 3206ebaecc14Sdanielk1977 rc = sqlite3RtreeInit(db); 3207ebaecc14Sdanielk1977 } 3208ebaecc14Sdanielk1977 #endif 3209ebaecc14Sdanielk1977 3210a43c8c8aSdrh #ifdef SQLITE_ENABLE_DBPAGE_VTAB 3211a43c8c8aSdrh if( !db->mallocFailed && rc==SQLITE_OK){ 3212a43c8c8aSdrh rc = sqlite3DbpageRegister(db); 3213a43c8c8aSdrh } 3214a43c8c8aSdrh #endif 3215a43c8c8aSdrh 3216a3ab9d0cSdrh #ifdef SQLITE_ENABLE_DBSTAT_VTAB 3217a3ab9d0cSdrh if( !db->mallocFailed && rc==SQLITE_OK){ 32183e0327d5Sdrh rc = sqlite3DbstatRegister(db); 3219a3ab9d0cSdrh } 3220a3ab9d0cSdrh #endif 3221a3ab9d0cSdrh 32222f20e13bSdrh #ifdef SQLITE_ENABLE_JSON1 32232f20e13bSdrh if( !db->mallocFailed && rc==SQLITE_OK){ 32242f20e13bSdrh rc = sqlite3Json1Init(db); 32252f20e13bSdrh } 32262f20e13bSdrh #endif 32272f20e13bSdrh 3228c6603af7Sdrh #ifdef SQLITE_ENABLE_STMTVTAB 3229f00f530bSdrh if( !db->mallocFailed && rc==SQLITE_OK){ 3230c6603af7Sdrh rc = sqlite3StmtVtabInit(db); 3231f00f530bSdrh } 3232f00f530bSdrh #endif 3233f00f530bSdrh 32348fd5bd36Sdrh /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking 32358fd5bd36Sdrh ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking 32368fd5bd36Sdrh ** mode. Doing nothing at all also makes NORMAL the default. 32378fd5bd36Sdrh */ 32388fd5bd36Sdrh #ifdef SQLITE_DEFAULT_LOCKING_MODE 32398fd5bd36Sdrh db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; 32408fd5bd36Sdrh sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), 32418fd5bd36Sdrh SQLITE_DEFAULT_LOCKING_MODE); 32428fd5bd36Sdrh #endif 32438fd5bd36Sdrh 324413f40da3Sdrh if( rc ) sqlite3Error(db, rc); 3245f6b1a8e1Sdrh 3246633e6d57Sdrh /* Enable the lookaside-malloc subsystem */ 32471b67f3caSdrh setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, 32481b67f3caSdrh sqlite3GlobalConfig.nLookaside); 3249633e6d57Sdrh 32505a299f91Sdan sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); 3251586b9c8aSdan 32524ad1713cSdanielk1977 opendb_out: 3253afc91047Sdrh if( db ){ 32543ba689d8Sdrh assert( db->mutex!=0 || isThreadsafe==0 32553ba689d8Sdrh || sqlite3GlobalConfig.bFullMutex==0 ); 325627641703Sdrh sqlite3_mutex_leave(db->mutex); 3257f3a65f7eSdrh } 325859633aeeSdanielk1977 rc = sqlite3_errcode(db); 3259b07028f7Sdrh assert( db!=0 || rc==SQLITE_NOMEM ); 326059633aeeSdanielk1977 if( rc==SQLITE_NOMEM ){ 32617ddad969Sdanielk1977 sqlite3_close(db); 32627ddad969Sdanielk1977 db = 0; 326359633aeeSdanielk1977 }else if( rc!=SQLITE_OK ){ 326459633aeeSdanielk1977 db->magic = SQLITE_MAGIC_SICK; 326513073931Sdanielk1977 } 32664ad1713cSdanielk1977 *ppDb = db; 3267ac455939Sdan #ifdef SQLITE_ENABLE_SQLLOG 3268ac455939Sdan if( sqlite3GlobalConfig.xSqllog ){ 326971ba10d3Sdan /* Opening a db handle. Fourth parameter is passed 0. */ 327071ba10d3Sdan void *pArg = sqlite3GlobalConfig.pSqllogArg; 327171ba10d3Sdan sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); 3272ac455939Sdan } 3273ac455939Sdan #endif 327446e6ea02Sdrh #if defined(SQLITE_HAS_CODEC) 327546e6ea02Sdrh if( rc==SQLITE_OK ){ 3276ad012d09Sdrh const char *zKey; 3277d4fb1965Sdrh if( (zKey = sqlite3_uri_parameter(zOpen, "hexkey"))!=0 && zKey[0] ){ 327846e6ea02Sdrh u8 iByte; 327946e6ea02Sdrh int i; 3280ad012d09Sdrh char zDecoded[40]; 3281ad012d09Sdrh for(i=0, iByte=0; i<sizeof(zDecoded)*2 && sqlite3Isxdigit(zKey[i]); i++){ 3282ad012d09Sdrh iByte = (iByte<<4) + sqlite3HexToInt(zKey[i]); 3283ad012d09Sdrh if( (i&1)!=0 ) zDecoded[i/2] = iByte; 328446e6ea02Sdrh } 3285ad012d09Sdrh sqlite3_key_v2(db, 0, zDecoded, i/2); 3286ad012d09Sdrh }else if( (zKey = sqlite3_uri_parameter(zOpen, "key"))!=0 ){ 3287ad012d09Sdrh sqlite3_key_v2(db, 0, zKey, sqlite3Strlen30(zKey)); 328846e6ea02Sdrh } 328946e6ea02Sdrh } 329046e6ea02Sdrh #endif 3291095fb474Sdrh sqlite3_free(zOpen); 3292597d2b64Sdrh return rc & 0xff; 32934ad1713cSdanielk1977 } 32944ad1713cSdanielk1977 32954ad1713cSdanielk1977 /* 32964ad1713cSdanielk1977 ** Open a new database handle. 32974ad1713cSdanielk1977 */ 329880290863Sdanielk1977 int sqlite3_open( 32994ad1713cSdanielk1977 const char *zFilename, 33004f057f90Sdanielk1977 sqlite3 **ppDb 33014ad1713cSdanielk1977 ){ 3302605264d2Sdrh return openDatabase(zFilename, ppDb, 3303605264d2Sdrh SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); 3304605264d2Sdrh } 3305605264d2Sdrh int sqlite3_open_v2( 3306428e2826Sdrh const char *filename, /* Database filename (UTF-8) */ 3307605264d2Sdrh sqlite3 **ppDb, /* OUT: SQLite db handle */ 3308605264d2Sdrh int flags, /* Flags */ 3309605264d2Sdrh const char *zVfs /* Name of VFS module to use */ 3310605264d2Sdrh ){ 3311522c26fbSdrh return openDatabase(filename, ppDb, (unsigned int)flags, zVfs); 331283ab5a8fSdanielk1977 } 331383ab5a8fSdanielk1977 33146c62608fSdrh #ifndef SQLITE_OMIT_UTF16 33154ad1713cSdanielk1977 /* 33164ad1713cSdanielk1977 ** Open a new database handle. 33174ad1713cSdanielk1977 */ 33184ad1713cSdanielk1977 int sqlite3_open16( 33194ad1713cSdanielk1977 const void *zFilename, 33204f057f90Sdanielk1977 sqlite3 **ppDb 33214ad1713cSdanielk1977 ){ 3322bfd6cce5Sdanielk1977 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ 3323bfd6cce5Sdanielk1977 sqlite3_value *pVal; 332440257ffdSdrh int rc; 33254ad1713cSdanielk1977 33269ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 33279ca95730Sdrh if( ppDb==0 ) return SQLITE_MISUSE_BKPT; 33289ca95730Sdrh #endif 33294ad1713cSdanielk1977 *ppDb = 0; 333040257ffdSdrh #ifndef SQLITE_OMIT_AUTOINIT 333140257ffdSdrh rc = sqlite3_initialize(); 333240257ffdSdrh if( rc ) return rc; 333340257ffdSdrh #endif 33349ca95730Sdrh if( zFilename==0 ) zFilename = "\000\000"; 33351e536953Sdanielk1977 pVal = sqlite3ValueNew(0); 3336b21c8cd4Sdrh sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); 3337b21c8cd4Sdrh zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); 3338bfd6cce5Sdanielk1977 if( zFilename8 ){ 3339605264d2Sdrh rc = openDatabase(zFilename8, ppDb, 3340605264d2Sdrh SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); 3341afc91047Sdrh assert( *ppDb || rc==SQLITE_NOMEM ); 3342f51bf48bSdanielk1977 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ 33439bd3cc46Sdrh SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE; 33444ad1713cSdanielk1977 } 334540257ffdSdrh }else{ 3346fad3039cSmistachkin rc = SQLITE_NOMEM_BKPT; 3347bfd6cce5Sdanielk1977 } 3348bfd6cce5Sdanielk1977 sqlite3ValueFree(pVal); 33498e227875Sdanielk1977 3350597d2b64Sdrh return rc & 0xff; 33514ad1713cSdanielk1977 } 33526c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 33534ad1713cSdanielk1977 3354106bb236Sdanielk1977 /* 3355d8123366Sdanielk1977 ** Register a new collation sequence with the database handle db. 3356d8123366Sdanielk1977 */ 33570202b29eSdanielk1977 int sqlite3_create_collation( 33580202b29eSdanielk1977 sqlite3* db, 33590202b29eSdanielk1977 const char *zName, 3360466be56bSdanielk1977 int enc, 33610202b29eSdanielk1977 void* pCtx, 33620202b29eSdanielk1977 int(*xCompare)(void*,int,const void*,int,const void*) 33630202b29eSdanielk1977 ){ 33644f81bbb5Sdrh return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0); 3365a9808b31Sdanielk1977 } 3366a9808b31Sdanielk1977 3367a9808b31Sdanielk1977 /* 3368a9808b31Sdanielk1977 ** Register a new collation sequence with the database handle db. 3369a9808b31Sdanielk1977 */ 3370a393c036Sdanielk1977 int sqlite3_create_collation_v2( 3371a9808b31Sdanielk1977 sqlite3* db, 3372a9808b31Sdanielk1977 const char *zName, 3373a9808b31Sdanielk1977 int enc, 3374a9808b31Sdanielk1977 void* pCtx, 3375a9808b31Sdanielk1977 int(*xCompare)(void*,int,const void*,int,const void*), 3376a9808b31Sdanielk1977 void(*xDel)(void*) 3377a9808b31Sdanielk1977 ){ 3378a9808b31Sdanielk1977 int rc; 33799ca95730Sdrh 33809ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 33819ca95730Sdrh if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; 33829ca95730Sdrh #endif 3383e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 338417435752Sdrh assert( !db->mallocFailed ); 33854dd65e0fSdrh rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); 3386e30f4426Sdrh rc = sqlite3ApiExit(db, rc); 3387e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 3388e30f4426Sdrh return rc; 33890202b29eSdanielk1977 } 33900202b29eSdanielk1977 33916c62608fSdrh #ifndef SQLITE_OMIT_UTF16 3392d8123366Sdanielk1977 /* 3393d8123366Sdanielk1977 ** Register a new collation sequence with the database handle db. 3394d8123366Sdanielk1977 */ 33950202b29eSdanielk1977 int sqlite3_create_collation16( 33960202b29eSdanielk1977 sqlite3* db, 3397bda2e62cSmihailim const void *zName, 3398466be56bSdanielk1977 int enc, 33990202b29eSdanielk1977 void* pCtx, 34000202b29eSdanielk1977 int(*xCompare)(void*,int,const void*,int,const void*) 34010202b29eSdanielk1977 ){ 34029a30cf65Sdanielk1977 int rc = SQLITE_OK; 3403af9a7c22Sdrh char *zName8; 34049ca95730Sdrh 34059ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 34069ca95730Sdrh if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; 34079ca95730Sdrh #endif 3408e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 340917435752Sdrh assert( !db->mallocFailed ); 3410b7dca7d7Sdan zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); 34119a30cf65Sdanielk1977 if( zName8 ){ 34124dd65e0fSdrh rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); 3413633e6d57Sdrh sqlite3DbFree(db, zName8); 34149a30cf65Sdanielk1977 } 3415e30f4426Sdrh rc = sqlite3ApiExit(db, rc); 3416e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 3417e30f4426Sdrh return rc; 34180202b29eSdanielk1977 } 34196c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 34207cedc8d4Sdanielk1977 3421d8123366Sdanielk1977 /* 3422d8123366Sdanielk1977 ** Register a collation sequence factory callback with the database handle 3423d8123366Sdanielk1977 ** db. Replace any previously installed collation sequence factory. 3424d8123366Sdanielk1977 */ 34257cedc8d4Sdanielk1977 int sqlite3_collation_needed( 34267cedc8d4Sdanielk1977 sqlite3 *db, 34277cedc8d4Sdanielk1977 void *pCollNeededArg, 34287cedc8d4Sdanielk1977 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) 34297cedc8d4Sdanielk1977 ){ 34309ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 34319ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 34329ca95730Sdrh #endif 3433e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 34347cedc8d4Sdanielk1977 db->xCollNeeded = xCollNeeded; 34357cedc8d4Sdanielk1977 db->xCollNeeded16 = 0; 34367cedc8d4Sdanielk1977 db->pCollNeededArg = pCollNeededArg; 3437e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 34387cedc8d4Sdanielk1977 return SQLITE_OK; 34397cedc8d4Sdanielk1977 } 3440d8123366Sdanielk1977 34416c62608fSdrh #ifndef SQLITE_OMIT_UTF16 3442d8123366Sdanielk1977 /* 3443d8123366Sdanielk1977 ** Register a collation sequence factory callback with the database handle 3444d8123366Sdanielk1977 ** db. Replace any previously installed collation sequence factory. 3445d8123366Sdanielk1977 */ 34467cedc8d4Sdanielk1977 int sqlite3_collation_needed16( 34477cedc8d4Sdanielk1977 sqlite3 *db, 34487cedc8d4Sdanielk1977 void *pCollNeededArg, 34497cedc8d4Sdanielk1977 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) 34507cedc8d4Sdanielk1977 ){ 34519ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 34529ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 34539ca95730Sdrh #endif 3454e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 34557cedc8d4Sdanielk1977 db->xCollNeeded = 0; 34567cedc8d4Sdanielk1977 db->xCollNeeded16 = xCollNeeded16; 34577cedc8d4Sdanielk1977 db->pCollNeededArg = pCollNeededArg; 3458e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 34597cedc8d4Sdanielk1977 return SQLITE_OK; 34607cedc8d4Sdanielk1977 } 34616c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 34626b456a2bSdanielk1977 3463eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 34646b456a2bSdanielk1977 /* 34657ddad969Sdanielk1977 ** This function is now an anachronism. It used to be used to recover from a 34667ddad969Sdanielk1977 ** malloc() failure, but SQLite now does this automatically. 34676b456a2bSdanielk1977 */ 34687d97efbeSdrh int sqlite3_global_recover(void){ 3469261919ccSdanielk1977 return SQLITE_OK; 34706b456a2bSdanielk1977 } 34716b456a2bSdanielk1977 #endif 34723e1d8e63Sdrh 34733e1d8e63Sdrh /* 34743e1d8e63Sdrh ** Test to see whether or not the database connection is in autocommit 34753e1d8e63Sdrh ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on 34763e1d8e63Sdrh ** by default. Autocommit is disabled by a BEGIN statement and reenabled 34773e1d8e63Sdrh ** by the next COMMIT or ROLLBACK. 34783e1d8e63Sdrh */ 34793e1d8e63Sdrh int sqlite3_get_autocommit(sqlite3 *db){ 34809ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 34819ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 34829ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 34839ca95730Sdrh return 0; 34849ca95730Sdrh } 34859ca95730Sdrh #endif 34863e1d8e63Sdrh return db->autoCommit; 34873e1d8e63Sdrh } 348849285708Sdrh 348949285708Sdrh /* 349060ec914cSpeter.d.reid ** The following routines are substitutes for constants SQLITE_CORRUPT, 34918fd8413aSmistachkin ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error 349260ec914cSpeter.d.reid ** constants. They serve two purposes: 34939978c97eSdrh ** 34949978c97eSdrh ** 1. Serve as a convenient place to set a breakpoint in a debugger 34959978c97eSdrh ** to detect when version error conditions occurs. 34969978c97eSdrh ** 34979978c97eSdrh ** 2. Invoke sqlite3_log() to provide the source code location where 34989978c97eSdrh ** a low-level error is first detected. 349949285708Sdrh */ 3500eebf2f57Sdan int sqlite3ReportError(int iErr, int lineno, const char *zType){ 350132c49904Sdrh sqlite3_log(iErr, "%s at line %d of [%.10s]", 350232c49904Sdrh zType, lineno, 20+sqlite3_sourceid()); 350332c49904Sdrh return iErr; 350432c49904Sdrh } 35059978c97eSdrh int sqlite3CorruptError(int lineno){ 3506af46dc12Sdrh testcase( sqlite3GlobalConfig.xLog!=0 ); 3507eebf2f57Sdan return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption"); 350849285708Sdrh } 35099978c97eSdrh int sqlite3MisuseError(int lineno){ 3510af46dc12Sdrh testcase( sqlite3GlobalConfig.xLog!=0 ); 3511eebf2f57Sdan return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse"); 35129978c97eSdrh } 35139978c97eSdrh int sqlite3CantopenError(int lineno){ 3514af46dc12Sdrh testcase( sqlite3GlobalConfig.xLog!=0 ); 3515eebf2f57Sdan return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file"); 35169978c97eSdrh } 351732c49904Sdrh #ifdef SQLITE_DEBUG 3518cc97ca4cSdrh int sqlite3CorruptPgnoError(int lineno, Pgno pgno){ 3519cc97ca4cSdrh char zMsg[100]; 3520cc97ca4cSdrh sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno); 3521cc97ca4cSdrh testcase( sqlite3GlobalConfig.xLog!=0 ); 3522eebf2f57Sdan return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg); 3523cc97ca4cSdrh } 3524fad3039cSmistachkin int sqlite3NomemError(int lineno){ 3525fad3039cSmistachkin testcase( sqlite3GlobalConfig.xLog!=0 ); 3526eebf2f57Sdan return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM"); 3527fad3039cSmistachkin } 3528fad3039cSmistachkin int sqlite3IoerrnomemError(int lineno){ 3529fad3039cSmistachkin testcase( sqlite3GlobalConfig.xLog!=0 ); 3530eebf2f57Sdan return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error"); 3531fad3039cSmistachkin } 353232c49904Sdrh #endif 35337c1817e2Sdrh 3534eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 35356f7adc8aSdrh /* 35366f7adc8aSdrh ** This is a convenience routine that makes sure that all thread-specific 35376f7adc8aSdrh ** data for this thread has been deallocated. 3538dce8bdb8Sdrh ** 3539dce8bdb8Sdrh ** SQLite no longer uses thread-specific data so this routine is now a 3540dce8bdb8Sdrh ** no-op. It is retained for historical compatibility. 35416f7adc8aSdrh */ 35426f7adc8aSdrh void sqlite3_thread_cleanup(void){ 35436f7adc8aSdrh } 3544eec556d3Sshane #endif 3545deb802cdSdanielk1977 3546deb802cdSdanielk1977 /* 3547deb802cdSdanielk1977 ** Return meta information about a specific column of a database table. 3548deb802cdSdanielk1977 ** See comment in sqlite3.h (sqlite.h.in) for details. 3549deb802cdSdanielk1977 */ 3550deb802cdSdanielk1977 int sqlite3_table_column_metadata( 3551deb802cdSdanielk1977 sqlite3 *db, /* Connection handle */ 3552deb802cdSdanielk1977 const char *zDbName, /* Database name or NULL */ 3553deb802cdSdanielk1977 const char *zTableName, /* Table name */ 3554deb802cdSdanielk1977 const char *zColumnName, /* Column name */ 3555deb802cdSdanielk1977 char const **pzDataType, /* OUTPUT: Declared data type */ 3556deb802cdSdanielk1977 char const **pzCollSeq, /* OUTPUT: Collation sequence name */ 3557deb802cdSdanielk1977 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ 3558deb802cdSdanielk1977 int *pPrimaryKey, /* OUTPUT: True if column part of PK */ 35599be07166Sdrh int *pAutoinc /* OUTPUT: True if column is auto-increment */ 3560deb802cdSdanielk1977 ){ 3561deb802cdSdanielk1977 int rc; 3562deb802cdSdanielk1977 char *zErrMsg = 0; 3563deb802cdSdanielk1977 Table *pTab = 0; 3564deb802cdSdanielk1977 Column *pCol = 0; 35651a51ce78Smistachkin int iCol = 0; 3566deb802cdSdanielk1977 char const *zDataType = 0; 3567deb802cdSdanielk1977 char const *zCollSeq = 0; 3568deb802cdSdanielk1977 int notnull = 0; 3569deb802cdSdanielk1977 int primarykey = 0; 3570deb802cdSdanielk1977 int autoinc = 0; 3571deb802cdSdanielk1977 357296c707a3Sdrh 357396c707a3Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 357496c707a3Sdrh if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){ 357596c707a3Sdrh return SQLITE_MISUSE_BKPT; 357696c707a3Sdrh } 357796c707a3Sdrh #endif 357896c707a3Sdrh 3579deb802cdSdanielk1977 /* Ensure the database schema has been loaded */ 3580e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 35813cb3edc1Sdrh sqlite3BtreeEnterAll(db); 3582deb802cdSdanielk1977 rc = sqlite3Init(db, &zErrMsg); 3583deb802cdSdanielk1977 if( SQLITE_OK!=rc ){ 3584deb802cdSdanielk1977 goto error_out; 3585deb802cdSdanielk1977 } 3586deb802cdSdanielk1977 3587deb802cdSdanielk1977 /* Locate the table in question */ 3588deb802cdSdanielk1977 pTab = sqlite3FindTable(db, zTableName, zDbName); 3589deb802cdSdanielk1977 if( !pTab || pTab->pSelect ){ 3590deb802cdSdanielk1977 pTab = 0; 3591deb802cdSdanielk1977 goto error_out; 3592deb802cdSdanielk1977 } 3593deb802cdSdanielk1977 3594deb802cdSdanielk1977 /* Find the column for which info is requested */ 359545d1b206Sdrh if( zColumnName==0 ){ 359645d1b206Sdrh /* Query for existance of table only */ 3597deb802cdSdanielk1977 }else{ 3598deb802cdSdanielk1977 for(iCol=0; iCol<pTab->nCol; iCol++){ 3599deb802cdSdanielk1977 pCol = &pTab->aCol[iCol]; 3600deb802cdSdanielk1977 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ 3601deb802cdSdanielk1977 break; 3602deb802cdSdanielk1977 } 3603deb802cdSdanielk1977 } 3604deb802cdSdanielk1977 if( iCol==pTab->nCol ){ 360545d1b206Sdrh if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){ 360645d1b206Sdrh iCol = pTab->iPKey; 360745d1b206Sdrh pCol = iCol>=0 ? &pTab->aCol[iCol] : 0; 360845d1b206Sdrh }else{ 3609deb802cdSdanielk1977 pTab = 0; 3610deb802cdSdanielk1977 goto error_out; 3611deb802cdSdanielk1977 } 3612deb802cdSdanielk1977 } 361345d1b206Sdrh } 3614deb802cdSdanielk1977 3615deb802cdSdanielk1977 /* The following block stores the meta information that will be returned 3616deb802cdSdanielk1977 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey 3617deb802cdSdanielk1977 ** and autoinc. At this point there are two possibilities: 3618deb802cdSdanielk1977 ** 3619deb802cdSdanielk1977 ** 1. The specified column name was rowid", "oid" or "_rowid_" 3620deb802cdSdanielk1977 ** and there is no explicitly declared IPK column. 3621deb802cdSdanielk1977 ** 3622deb802cdSdanielk1977 ** 2. The table is not a view and the column name identified an 3623deb802cdSdanielk1977 ** explicitly declared column. Copy meta information from *pCol. 3624deb802cdSdanielk1977 */ 3625deb802cdSdanielk1977 if( pCol ){ 3626d7564865Sdrh zDataType = sqlite3ColumnType(pCol,0); 3627deb802cdSdanielk1977 zCollSeq = pCol->zColl; 36289be07166Sdrh notnull = pCol->notNull!=0; 3629a371ace4Sdrh primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0; 36307d10d5a6Sdrh autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; 3631deb802cdSdanielk1977 }else{ 3632deb802cdSdanielk1977 zDataType = "INTEGER"; 3633deb802cdSdanielk1977 primarykey = 1; 3634deb802cdSdanielk1977 } 3635deb802cdSdanielk1977 if( !zCollSeq ){ 3636f19aa5faSdrh zCollSeq = sqlite3StrBINARY; 3637deb802cdSdanielk1977 } 3638deb802cdSdanielk1977 3639deb802cdSdanielk1977 error_out: 364002b4e3b3Sdanielk1977 sqlite3BtreeLeaveAll(db); 3641deb802cdSdanielk1977 3642deb802cdSdanielk1977 /* Whether the function call succeeded or failed, set the output parameters 3643deb802cdSdanielk1977 ** to whatever their local counterparts contain. If an error did occur, 3644deb802cdSdanielk1977 ** this has the effect of zeroing all output parameters. 3645deb802cdSdanielk1977 */ 3646deb802cdSdanielk1977 if( pzDataType ) *pzDataType = zDataType; 3647deb802cdSdanielk1977 if( pzCollSeq ) *pzCollSeq = zCollSeq; 3648deb802cdSdanielk1977 if( pNotNull ) *pNotNull = notnull; 3649deb802cdSdanielk1977 if( pPrimaryKey ) *pPrimaryKey = primarykey; 3650deb802cdSdanielk1977 if( pAutoinc ) *pAutoinc = autoinc; 3651deb802cdSdanielk1977 3652deb802cdSdanielk1977 if( SQLITE_OK==rc && !pTab ){ 3653633e6d57Sdrh sqlite3DbFree(db, zErrMsg); 3654118640bdSdrh zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, 3655f089aa45Sdrh zColumnName); 3656deb802cdSdanielk1977 rc = SQLITE_ERROR; 3657deb802cdSdanielk1977 } 365813f40da3Sdrh sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg); 3659633e6d57Sdrh sqlite3DbFree(db, zErrMsg); 3660e30f4426Sdrh rc = sqlite3ApiExit(db, rc); 3661e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 3662f9cb7f58Sdrh return rc; 3663f9cb7f58Sdrh } 3664f9cb7f58Sdrh 3665f9cb7f58Sdrh /* 3666f9cb7f58Sdrh ** Sleep for a little while. Return the amount of time slept. 3667f9cb7f58Sdrh */ 3668f9cb7f58Sdrh int sqlite3_sleep(int ms){ 3669b4b47411Sdanielk1977 sqlite3_vfs *pVfs; 3670e30f4426Sdrh int rc; 3671d677b3d6Sdrh pVfs = sqlite3_vfs_find(0); 367240257ffdSdrh if( pVfs==0 ) return 0; 3673fee2d25aSdanielk1977 3674fee2d25aSdanielk1977 /* This function works in milliseconds, but the underlying OsSleep() 3675fee2d25aSdanielk1977 ** API uses microseconds. Hence the 1000's. 3676fee2d25aSdanielk1977 */ 3677e30f4426Sdrh rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); 3678e30f4426Sdrh return rc; 3679f9cb7f58Sdrh } 36804ac285a1Sdrh 36814ac285a1Sdrh /* 36824ac285a1Sdrh ** Enable or disable the extended result codes. 36834ac285a1Sdrh */ 36844ac285a1Sdrh int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ 36859ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 36869ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 36879ca95730Sdrh #endif 3688e30f4426Sdrh sqlite3_mutex_enter(db->mutex); 36894ac285a1Sdrh db->errMask = onoff ? 0xffffffff : 0xff; 3690e30f4426Sdrh sqlite3_mutex_leave(db->mutex); 36914ac285a1Sdrh return SQLITE_OK; 36924ac285a1Sdrh } 3693cc6bb3eaSdrh 3694cc6bb3eaSdrh /* 3695cc6bb3eaSdrh ** Invoke the xFileControl method on a particular database. 3696cc6bb3eaSdrh */ 3697cc6bb3eaSdrh int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ 3698cc6bb3eaSdrh int rc = SQLITE_ERROR; 3699421377e6Sdrh Btree *pBtree; 3700421377e6Sdrh 37019ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 37029ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; 37039ca95730Sdrh #endif 3704cc6bb3eaSdrh sqlite3_mutex_enter(db->mutex); 3705421377e6Sdrh pBtree = sqlite3DbNameToBtree(db, zDbName); 3706cc6bb3eaSdrh if( pBtree ){ 3707cc6bb3eaSdrh Pager *pPager; 370855176259Sdrh sqlite3_file *fd; 3709cc6bb3eaSdrh sqlite3BtreeEnter(pBtree); 3710cc6bb3eaSdrh pPager = sqlite3BtreePager(pBtree); 371155176259Sdrh assert( pPager!=0 ); 371255176259Sdrh fd = sqlite3PagerFile(pPager); 371355176259Sdrh assert( fd!=0 ); 37143eb08114Sdrh if( op==SQLITE_FCNTL_FILE_POINTER ){ 37153eb08114Sdrh *(sqlite3_file**)pArg = fd; 37163eb08114Sdrh rc = SQLITE_OK; 3717790f287cSdrh }else if( op==SQLITE_FCNTL_VFS_POINTER ){ 3718790f287cSdrh *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager); 3719790f287cSdrh rc = SQLITE_OK; 372021d61853Sdrh }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){ 372121d61853Sdrh *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager); 372221d61853Sdrh rc = SQLITE_OK; 3723ea99a31cSdrh }else if( op==SQLITE_FCNTL_DATA_VERSION ){ 3724ea99a31cSdrh *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager); 3725ea99a31cSdrh rc = SQLITE_OK; 37260b52b7d0Sdrh }else{ 3727afb39a4cSdrh rc = sqlite3OsFileControl(fd, op, pArg); 3728cc6bb3eaSdrh } 3729cc6bb3eaSdrh sqlite3BtreeLeave(pBtree); 3730cc6bb3eaSdrh } 3731cc6bb3eaSdrh sqlite3_mutex_leave(db->mutex); 3732cc6bb3eaSdrh return rc; 3733cc6bb3eaSdrh } 3734ed13d98cSdrh 3735ed13d98cSdrh /* 3736ed13d98cSdrh ** Interface to the testing logic. 3737ed13d98cSdrh */ 3738ed13d98cSdrh int sqlite3_test_control(int op, ...){ 3739ed13d98cSdrh int rc = 0; 3740d12602a9Sdrh #ifdef SQLITE_UNTESTABLE 3741f5ed7ad6Sdrh UNUSED_PARAMETER(op); 3742f5ed7ad6Sdrh #else 37432fa1868fSdrh va_list ap; 3744ed13d98cSdrh va_start(ap, op); 3745ed13d98cSdrh switch( op ){ 3746d09414cdSdanielk1977 3747d09414cdSdanielk1977 /* 3748984bfaa4Sdrh ** Save the current state of the PRNG. 3749984bfaa4Sdrh */ 37502fa1868fSdrh case SQLITE_TESTCTRL_PRNG_SAVE: { 37512fa1868fSdrh sqlite3PrngSaveState(); 37522fa1868fSdrh break; 37532fa1868fSdrh } 3754984bfaa4Sdrh 3755984bfaa4Sdrh /* 3756984bfaa4Sdrh ** Restore the state of the PRNG to the last state saved using 3757984bfaa4Sdrh ** PRNG_SAVE. If PRNG_SAVE has never before been called, then 3758984bfaa4Sdrh ** this verb acts like PRNG_RESET. 3759984bfaa4Sdrh */ 37602fa1868fSdrh case SQLITE_TESTCTRL_PRNG_RESTORE: { 37612fa1868fSdrh sqlite3PrngRestoreState(); 37622fa1868fSdrh break; 37632fa1868fSdrh } 3764984bfaa4Sdrh 3765984bfaa4Sdrh /* 3766984bfaa4Sdrh ** Reset the PRNG back to its uninitialized state. The next call 3767984bfaa4Sdrh ** to sqlite3_randomness() will reseed the PRNG using a single call 3768984bfaa4Sdrh ** to the xRandomness method of the default VFS. 3769984bfaa4Sdrh */ 37702fa1868fSdrh case SQLITE_TESTCTRL_PRNG_RESET: { 3771fe98081eSdrh sqlite3_randomness(0,0); 37722fa1868fSdrh break; 37732fa1868fSdrh } 37743088d59eSdrh 37753088d59eSdrh /* 37763088d59eSdrh ** sqlite3_test_control(BITVEC_TEST, size, program) 37773088d59eSdrh ** 37783088d59eSdrh ** Run a test against a Bitvec object of size. The program argument 37793088d59eSdrh ** is an array of integers that defines the test. Return -1 on a 37803088d59eSdrh ** memory allocation error, 0 on success, or non-zero for an error. 37813088d59eSdrh ** See the sqlite3BitvecBuiltinTest() for additional information. 37823088d59eSdrh */ 37833088d59eSdrh case SQLITE_TESTCTRL_BITVEC_TEST: { 37843088d59eSdrh int sz = va_arg(ap, int); 37853088d59eSdrh int *aProg = va_arg(ap, int*); 37863088d59eSdrh rc = sqlite3BitvecBuiltinTest(sz, aProg); 37873088d59eSdrh break; 37883088d59eSdrh } 37892d1d86fbSdanielk1977 37902d1d86fbSdanielk1977 /* 3791c007f61bSdrh ** sqlite3_test_control(FAULT_INSTALL, xCallback) 3792c007f61bSdrh ** 3793c007f61bSdrh ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called, 3794c007f61bSdrh ** if xCallback is not NULL. 3795c007f61bSdrh ** 3796c007f61bSdrh ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0) 3797c007f61bSdrh ** is called immediately after installing the new callback and the return 3798c007f61bSdrh ** value from sqlite3FaultSim(0) becomes the return from 3799c007f61bSdrh ** sqlite3_test_control(). 3800c007f61bSdrh */ 3801c007f61bSdrh case SQLITE_TESTCTRL_FAULT_INSTALL: { 380277a90ce8Smistachkin /* MSVC is picky about pulling func ptrs from va lists. 380377a90ce8Smistachkin ** http://support.microsoft.com/kb/47961 3804685ffb13Sdan ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int)); 380577a90ce8Smistachkin */ 380677a90ce8Smistachkin typedef int(*TESTCALLBACKFUNC_t)(int); 3807685ffb13Sdan sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t); 3808c007f61bSdrh rc = sqlite3FaultSim(0); 3809c007f61bSdrh break; 3810c007f61bSdrh } 3811c007f61bSdrh 3812c007f61bSdrh /* 38132d1d86fbSdanielk1977 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) 38142d1d86fbSdanielk1977 ** 38152d1d86fbSdanielk1977 ** Register hooks to call to indicate which malloc() failures 38162d1d86fbSdanielk1977 ** are benign. 38172d1d86fbSdanielk1977 */ 38182d1d86fbSdanielk1977 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { 3819ca026796Sdanielk1977 typedef void (*void_function)(void); 3820ca026796Sdanielk1977 void_function xBenignBegin; 3821ca026796Sdanielk1977 void_function xBenignEnd; 3822ca026796Sdanielk1977 xBenignBegin = va_arg(ap, void_function); 3823ca026796Sdanielk1977 xBenignEnd = va_arg(ap, void_function); 38242d1d86fbSdanielk1977 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); 38252d1d86fbSdanielk1977 break; 38262d1d86fbSdanielk1977 } 3827c7a3bb94Sdrh 3828c7a3bb94Sdrh /* 3829f3af63f9Sdrh ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) 3830c7a3bb94Sdrh ** 3831c7a3bb94Sdrh ** Set the PENDING byte to the value in the argument, if X>0. 3832c7a3bb94Sdrh ** Make no changes if X==0. Return the value of the pending byte 3833c7a3bb94Sdrh ** as it existing before this routine was called. 3834c7a3bb94Sdrh ** 3835c7a3bb94Sdrh ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in 3836c7a3bb94Sdrh ** an incompatible database file format. Changing the PENDING byte 3837c7a3bb94Sdrh ** while any database connection is open results in undefined and 383860ec914cSpeter.d.reid ** deleterious behavior. 3839c7a3bb94Sdrh */ 3840c7a3bb94Sdrh case SQLITE_TESTCTRL_PENDING_BYTE: { 3841f83dc1efSdrh rc = PENDING_BYTE; 3842f83dc1efSdrh #ifndef SQLITE_OMIT_WSD 3843f83dc1efSdrh { 3844c7a3bb94Sdrh unsigned int newVal = va_arg(ap, unsigned int); 3845c7a3bb94Sdrh if( newVal ) sqlite3PendingByte = newVal; 3846f83dc1efSdrh } 3847f83dc1efSdrh #endif 3848c7a3bb94Sdrh break; 3849c7a3bb94Sdrh } 3850f3af63f9Sdrh 3851f3af63f9Sdrh /* 3852f3af63f9Sdrh ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X) 3853f3af63f9Sdrh ** 3854f3af63f9Sdrh ** This action provides a run-time test to see whether or not 3855f3af63f9Sdrh ** assert() was enabled at compile-time. If X is true and assert() 3856f3af63f9Sdrh ** is enabled, then the return value is true. If X is true and 3857f3af63f9Sdrh ** assert() is disabled, then the return value is zero. If X is 3858f3af63f9Sdrh ** false and assert() is enabled, then the assertion fires and the 3859f3af63f9Sdrh ** process aborts. If X is false and assert() is disabled, then the 3860f3af63f9Sdrh ** return value is zero. 3861f3af63f9Sdrh */ 3862f3af63f9Sdrh case SQLITE_TESTCTRL_ASSERT: { 3863f3af63f9Sdrh volatile int x = 0; 3864cc5f8a46Sdrh assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 ); 3865f3af63f9Sdrh rc = x; 3866f3af63f9Sdrh break; 3867f3af63f9Sdrh } 3868f3af63f9Sdrh 3869f3af63f9Sdrh 3870f3af63f9Sdrh /* 3871f3af63f9Sdrh ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) 3872f3af63f9Sdrh ** 3873f3af63f9Sdrh ** This action provides a run-time test to see how the ALWAYS and 3874f3af63f9Sdrh ** NEVER macros were defined at compile-time. 3875f3af63f9Sdrh ** 3876ce2c482eSdrh ** The return value is ALWAYS(X) if X is true, or 0 if X is false. 3877f3af63f9Sdrh ** 3878f3af63f9Sdrh ** The recommended test is X==2. If the return value is 2, that means 3879f3af63f9Sdrh ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the 3880f3af63f9Sdrh ** default setting. If the return value is 1, then ALWAYS() is either 3881f3af63f9Sdrh ** hard-coded to true or else it asserts if its argument is false. 3882f3af63f9Sdrh ** The first behavior (hard-coded to true) is the case if 3883f3af63f9Sdrh ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second 3884f3af63f9Sdrh ** behavior (assert if the argument to ALWAYS() is false) is the case if 3885f3af63f9Sdrh ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled. 3886f3af63f9Sdrh ** 3887f3af63f9Sdrh ** The run-time test procedure might look something like this: 3888f3af63f9Sdrh ** 3889f3af63f9Sdrh ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){ 3890f3af63f9Sdrh ** // ALWAYS() and NEVER() are no-op pass-through macros 3891f3af63f9Sdrh ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){ 3892f3af63f9Sdrh ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false. 3893f3af63f9Sdrh ** }else{ 3894f3af63f9Sdrh ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. 3895f3af63f9Sdrh ** } 3896f3af63f9Sdrh */ 3897f3af63f9Sdrh case SQLITE_TESTCTRL_ALWAYS: { 3898f3af63f9Sdrh int x = va_arg(ap,int); 3899ce2c482eSdrh rc = x ? ALWAYS(x) : 0; 3900f3af63f9Sdrh break; 3901f3af63f9Sdrh } 3902c046e3edSdrh 39032cf4acbdSdrh /* 39042cf4acbdSdrh ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER); 39052cf4acbdSdrh ** 39062cf4acbdSdrh ** The integer returned reveals the byte-order of the computer on which 39072cf4acbdSdrh ** SQLite is running: 39082cf4acbdSdrh ** 39092cf4acbdSdrh ** 1 big-endian, determined at run-time 39102cf4acbdSdrh ** 10 little-endian, determined at run-time 39112cf4acbdSdrh ** 432101 big-endian, determined at compile-time 39122cf4acbdSdrh ** 123410 little-endian, determined at compile-time 39132cf4acbdSdrh */ 39142cf4acbdSdrh case SQLITE_TESTCTRL_BYTEORDER: { 39152cf4acbdSdrh rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN; 39162cf4acbdSdrh break; 39172cf4acbdSdrh } 39182cf4acbdSdrh 3919c046e3edSdrh /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) 3920c046e3edSdrh ** 3921c046e3edSdrh ** Set the nReserve size to N for the main database on the database 3922c046e3edSdrh ** connection db. 3923c046e3edSdrh */ 3924c046e3edSdrh case SQLITE_TESTCTRL_RESERVE: { 3925c046e3edSdrh sqlite3 *db = va_arg(ap, sqlite3*); 3926c046e3edSdrh int x = va_arg(ap,int); 3927c046e3edSdrh sqlite3_mutex_enter(db->mutex); 3928c046e3edSdrh sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); 3929c046e3edSdrh sqlite3_mutex_leave(db->mutex); 3930c046e3edSdrh break; 3931c046e3edSdrh } 3932c046e3edSdrh 393307096f68Sdrh /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) 393407096f68Sdrh ** 393507096f68Sdrh ** Enable or disable various optimizations for testing purposes. The 393607096f68Sdrh ** argument N is a bitmask of optimizations to be disabled. For normal 393707096f68Sdrh ** operation N should be 0. The idea is that a test program (like the 393807096f68Sdrh ** SQL Logic Test or SLT test module) can run the same SQL multiple times 393907096f68Sdrh ** with various optimizations disabled to verify that the same answer 394007096f68Sdrh ** is obtained in every case. 394107096f68Sdrh */ 394207096f68Sdrh case SQLITE_TESTCTRL_OPTIMIZATIONS: { 394307096f68Sdrh sqlite3 *db = va_arg(ap, sqlite3*); 3944a5759677Sdrh db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff); 394507096f68Sdrh break; 394607096f68Sdrh } 394707096f68Sdrh 3948c17d696cSdan /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); 3949c17d696cSdan ** 3950c17d696cSdan ** If parameter onoff is non-zero, configure the wrappers so that all 3951c17d696cSdan ** subsequent calls to localtime() and variants fail. If onoff is zero, 3952c17d696cSdan ** undo this setting. 3953c17d696cSdan */ 3954c17d696cSdan case SQLITE_TESTCTRL_LOCALTIME_FAULT: { 3955c17d696cSdan sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); 3956c17d696cSdan break; 3957c17d696cSdan } 3958c17d696cSdan 395909fe6143Sdrh /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); 396009fe6143Sdrh ** 396109fe6143Sdrh ** Set or clear a flag that indicates that the database file is always well- 396209fe6143Sdrh ** formed and never corrupt. This flag is clear by default, indicating that 396309fe6143Sdrh ** database files might have arbitrary corruption. Setting the flag during 396409fe6143Sdrh ** testing causes certain assert() statements in the code to be activated 396509fe6143Sdrh ** that demonstrat invariants on well-formed database files. 396609fe6143Sdrh */ 396709fe6143Sdrh case SQLITE_TESTCTRL_NEVER_CORRUPT: { 39689c2552f2Sdan sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int); 396909fe6143Sdrh break; 397009fe6143Sdrh } 397109fe6143Sdrh 39729e5eb9c8Sdrh /* Set the threshold at which OP_Once counters reset back to zero. 3973e0736da1Smistachkin ** By default this is 0x7ffffffe (over 2 billion), but that value is 39749e5eb9c8Sdrh ** too big to test in a reasonable amount of time, so this control is 39759e5eb9c8Sdrh ** provided to set a small and easily reachable reset value. 39769e5eb9c8Sdrh */ 39779e5eb9c8Sdrh case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: { 39789e5eb9c8Sdrh sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int); 39799e5eb9c8Sdrh break; 39809e5eb9c8Sdrh } 3981688852abSdrh 3982688852abSdrh /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr); 3983688852abSdrh ** 3984688852abSdrh ** Set the VDBE coverage callback function to xCallback with context 3985688852abSdrh ** pointer ptr. 3986688852abSdrh */ 3987688852abSdrh case SQLITE_TESTCTRL_VDBE_COVERAGE: { 3988688852abSdrh #ifdef SQLITE_VDBE_COVERAGE 39897083a487Sdrh typedef void (*branch_callback)(void*,unsigned int, 39907083a487Sdrh unsigned char,unsigned char); 3991688852abSdrh sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback); 3992688852abSdrh sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*); 3993688852abSdrh #endif 3994688852abSdrh break; 3995688852abSdrh } 3996688852abSdrh 39978930c2abSdan /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */ 39988930c2abSdan case SQLITE_TESTCTRL_SORTER_MMAP: { 39998930c2abSdan sqlite3 *db = va_arg(ap, sqlite3*); 40008930c2abSdan db->nMaxSorterMmap = va_arg(ap, int); 40018930c2abSdan break; 40028930c2abSdan } 40038930c2abSdan 400443cfc230Sdrh /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT); 400543cfc230Sdrh ** 400643cfc230Sdrh ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if 400743cfc230Sdrh ** not. 400843cfc230Sdrh */ 400943cfc230Sdrh case SQLITE_TESTCTRL_ISINIT: { 401043cfc230Sdrh if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR; 401143cfc230Sdrh break; 401243cfc230Sdrh } 40138964b345Sdrh 40141ffede8cSdrh /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum); 40158964b345Sdrh ** 40161ffede8cSdrh ** This test control is used to create imposter tables. "db" is a pointer 40171ffede8cSdrh ** to the database connection. dbName is the database name (ex: "main" or 40181ffede8cSdrh ** "temp") which will receive the imposter. "onOff" turns imposter mode on 40191ffede8cSdrh ** or off. "tnum" is the root page of the b-tree to which the imposter 40201ffede8cSdrh ** table should connect. 40211ffede8cSdrh ** 40221ffede8cSdrh ** Enable imposter mode only when the schema has already been parsed. Then 40233ba689d8Sdrh ** run a single CREATE TABLE statement to construct the imposter table in 40243ba689d8Sdrh ** the parsed schema. Then turn imposter mode back off again. 40251ffede8cSdrh ** 40261ffede8cSdrh ** If onOff==0 and tnum>0 then reset the schema for all databases, causing 40271ffede8cSdrh ** the schema to be reparsed the next time it is needed. This has the 40281ffede8cSdrh ** effect of erasing all imposter tables. 40298964b345Sdrh */ 40301ffede8cSdrh case SQLITE_TESTCTRL_IMPOSTER: { 40318964b345Sdrh sqlite3 *db = va_arg(ap, sqlite3*); 40328fb15e3bSdrh sqlite3_mutex_enter(db->mutex); 40331ffede8cSdrh db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*)); 40341ffede8cSdrh db->init.busy = db->init.imposterTable = va_arg(ap,int); 40358964b345Sdrh db->init.newTnum = va_arg(ap,int); 40361ffede8cSdrh if( db->init.busy==0 && db->init.newTnum>0 ){ 40371ffede8cSdrh sqlite3ResetAllSchemasOfConnection(db); 40381ffede8cSdrh } 40398fb15e3bSdrh sqlite3_mutex_leave(db->mutex); 40408964b345Sdrh break; 40418964b345Sdrh } 40420d9de99cSdrh 40430d9de99cSdrh #if defined(YYCOVERAGE) 40440d9de99cSdrh /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out) 40450d9de99cSdrh ** 40460d9de99cSdrh ** This test control (only available when SQLite is compiled with 40470d9de99cSdrh ** -DYYCOVERAGE) writes a report onto "out" that shows all 40480d9de99cSdrh ** state/lookahead combinations in the parser state machine 40490d9de99cSdrh ** which are never exercised. If any state is missed, make the 40500d9de99cSdrh ** return code SQLITE_ERROR. 40510d9de99cSdrh */ 40520d9de99cSdrh case SQLITE_TESTCTRL_PARSER_COVERAGE: { 40530d9de99cSdrh FILE *out = va_arg(ap, FILE*); 40540d9de99cSdrh if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR; 40550d9de99cSdrh break; 40560d9de99cSdrh } 40570d9de99cSdrh #endif /* defined(YYCOVERAGE) */ 4058ed13d98cSdrh } 4059ed13d98cSdrh va_end(ap); 4060d12602a9Sdrh #endif /* SQLITE_UNTESTABLE */ 40617c36d077Sdanielk1977 return rc; 4062ed13d98cSdrh } 4063cc487d13Sdrh 4064cc487d13Sdrh /* 4065cc487d13Sdrh ** This is a utility routine, useful to VFS implementations, that checks 4066cc487d13Sdrh ** to see if a database file was a URI that contained a specific query 4067cc487d13Sdrh ** parameter, and if so obtains the value of the query parameter. 4068cc487d13Sdrh ** 4069cc487d13Sdrh ** The zFilename argument is the filename pointer passed into the xOpen() 4070cc487d13Sdrh ** method of a VFS implementation. The zParam argument is the name of the 4071cc487d13Sdrh ** query parameter we seek. This routine returns the value of the zParam 4072cc487d13Sdrh ** parameter if it exists. If the parameter does not exist, this routine 4073cc487d13Sdrh ** returns a NULL pointer. 4074cc487d13Sdrh */ 4075cc487d13Sdrh const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ 40769ca95730Sdrh if( zFilename==0 || zParam==0 ) return 0; 4077bd69559bSdrh zFilename += sqlite3Strlen30(zFilename) + 1; 4078cc487d13Sdrh while( zFilename[0] ){ 4079cc487d13Sdrh int x = strcmp(zFilename, zParam); 4080bd69559bSdrh zFilename += sqlite3Strlen30(zFilename) + 1; 4081cc487d13Sdrh if( x==0 ) return zFilename; 4082bd69559bSdrh zFilename += sqlite3Strlen30(zFilename) + 1; 4083cc487d13Sdrh } 4084cc487d13Sdrh return 0; 4085cc487d13Sdrh } 4086283829cbSdrh 4087283829cbSdrh /* 408892913720Sdrh ** Return a boolean value for a query parameter. 408992913720Sdrh */ 409092913720Sdrh int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){ 409192913720Sdrh const char *z = sqlite3_uri_parameter(zFilename, zParam); 409238d9c612Sdrh bDflt = bDflt!=0; 409338d9c612Sdrh return z ? sqlite3GetBoolean(z, bDflt) : bDflt; 409492913720Sdrh } 409592913720Sdrh 409692913720Sdrh /* 409792913720Sdrh ** Return a 64-bit integer value for a query parameter. 409892913720Sdrh */ 409992913720Sdrh sqlite3_int64 sqlite3_uri_int64( 410092913720Sdrh const char *zFilename, /* Filename as passed to xOpen */ 410192913720Sdrh const char *zParam, /* URI parameter sought */ 410292913720Sdrh sqlite3_int64 bDflt /* return if parameter is missing */ 410392913720Sdrh ){ 410492913720Sdrh const char *z = sqlite3_uri_parameter(zFilename, zParam); 410592913720Sdrh sqlite3_int64 v; 410684d4f1a3Sdrh if( z && sqlite3DecOrHexToI64(z, &v)==0 ){ 410792913720Sdrh bDflt = v; 410892913720Sdrh } 410992913720Sdrh return bDflt; 411092913720Sdrh } 411192913720Sdrh 411292913720Sdrh /* 4113421377e6Sdrh ** Return the Btree pointer identified by zDbName. Return NULL if not found. 4114421377e6Sdrh */ 4115421377e6Sdrh Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){ 41162951809eSdrh int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0; 41172951809eSdrh return iDb<0 ? 0 : db->aDb[iDb].pBt; 4118421377e6Sdrh } 4119421377e6Sdrh 4120421377e6Sdrh /* 4121283829cbSdrh ** Return the filename of the database associated with a database 4122283829cbSdrh ** connection. 4123283829cbSdrh */ 4124283829cbSdrh const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ 4125cd54bab6Smistachkin Btree *pBt; 41269ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 41279ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 41289ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 41299ca95730Sdrh return 0; 41309ca95730Sdrh } 41319ca95730Sdrh #endif 4132cd54bab6Smistachkin pBt = sqlite3DbNameToBtree(db, zDbName); 4133421377e6Sdrh return pBt ? sqlite3BtreeGetFilename(pBt) : 0; 4134283829cbSdrh } 4135421377e6Sdrh 4136421377e6Sdrh /* 4137421377e6Sdrh ** Return 1 if database is read-only or 0 if read/write. Return -1 if 4138421377e6Sdrh ** no such database exists. 4139421377e6Sdrh */ 4140421377e6Sdrh int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ 4141cd54bab6Smistachkin Btree *pBt; 41429ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 41439ca95730Sdrh if( !sqlite3SafetyCheckOk(db) ){ 41449ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 41459ca95730Sdrh return -1; 41469ca95730Sdrh } 41479ca95730Sdrh #endif 4148cd54bab6Smistachkin pBt = sqlite3DbNameToBtree(db, zDbName); 4149781597feSdrh return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; 4150283829cbSdrh } 4151fc1acf33Sdan 4152fc1acf33Sdan #ifdef SQLITE_ENABLE_SNAPSHOT 4153fc1acf33Sdan /* 4154fc1acf33Sdan ** Obtain a snapshot handle for the snapshot of database zDb currently 4155fc1acf33Sdan ** being read by handle db. 4156fc1acf33Sdan */ 4157fc1acf33Sdan int sqlite3_snapshot_get( 4158fc1acf33Sdan sqlite3 *db, 4159fc1acf33Sdan const char *zDb, 4160fc1acf33Sdan sqlite3_snapshot **ppSnapshot 4161fc1acf33Sdan ){ 4162fc1acf33Sdan int rc = SQLITE_ERROR; 4163fc1acf33Sdan #ifndef SQLITE_OMIT_WAL 4164fc1acf33Sdan 4165fc1acf33Sdan #ifdef SQLITE_ENABLE_API_ARMOR 4166fc1acf33Sdan if( !sqlite3SafetyCheckOk(db) ){ 4167fc1acf33Sdan return SQLITE_MISUSE_BKPT; 4168fc1acf33Sdan } 4169fc1acf33Sdan #endif 4170fc1acf33Sdan sqlite3_mutex_enter(db->mutex); 4171fc1acf33Sdan 4172edace5d4Sdan if( db->autoCommit==0 ){ 4173edace5d4Sdan int iDb = sqlite3FindDbName(db, zDb); 4174fc1acf33Sdan if( iDb==0 || iDb>1 ){ 4175fc1acf33Sdan Btree *pBt = db->aDb[iDb].pBt; 41767116dc60Sdan if( 0==sqlite3BtreeIsInTrans(pBt) ){ 4177685a50adSdrh rc = sqlite3BtreeBeginTrans(pBt, 0, 0); 41787116dc60Sdan if( rc==SQLITE_OK ){ 4179fc1acf33Sdan rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot); 4180fc1acf33Sdan } 4181fc1acf33Sdan } 41827116dc60Sdan } 4183edace5d4Sdan } 4184fc1acf33Sdan 4185fc1acf33Sdan sqlite3_mutex_leave(db->mutex); 4186fc1acf33Sdan #endif /* SQLITE_OMIT_WAL */ 4187fc1acf33Sdan return rc; 4188fc1acf33Sdan } 4189fc1acf33Sdan 4190fc1acf33Sdan /* 4191fc1acf33Sdan ** Open a read-transaction on the snapshot idendified by pSnapshot. 4192fc1acf33Sdan */ 4193fc1acf33Sdan int sqlite3_snapshot_open( 4194fc1acf33Sdan sqlite3 *db, 4195fc1acf33Sdan const char *zDb, 4196fc1acf33Sdan sqlite3_snapshot *pSnapshot 4197fc1acf33Sdan ){ 4198fc1acf33Sdan int rc = SQLITE_ERROR; 4199fc1acf33Sdan #ifndef SQLITE_OMIT_WAL 4200fc1acf33Sdan 4201fc1acf33Sdan #ifdef SQLITE_ENABLE_API_ARMOR 4202fc1acf33Sdan if( !sqlite3SafetyCheckOk(db) ){ 4203fc1acf33Sdan return SQLITE_MISUSE_BKPT; 4204fc1acf33Sdan } 4205fc1acf33Sdan #endif 4206fc1acf33Sdan sqlite3_mutex_enter(db->mutex); 4207fc1acf33Sdan if( db->autoCommit==0 ){ 4208fc1acf33Sdan int iDb; 4209fc1acf33Sdan iDb = sqlite3FindDbName(db, zDb); 4210fc1acf33Sdan if( iDb==0 || iDb>1 ){ 4211fc1acf33Sdan Btree *pBt = db->aDb[iDb].pBt; 4212*fa3d4c19Sdan if( sqlite3BtreeIsInTrans(pBt)==0 ){ 4213*fa3d4c19Sdan Pager *pPager = sqlite3BtreePager(pBt); 4214*fa3d4c19Sdan int bUnlock = 0; 4215*fa3d4c19Sdan if( sqlite3BtreeIsInReadTrans(pBt) ){ 4216*fa3d4c19Sdan if( db->nVdbeActive==0 ){ 4217*fa3d4c19Sdan rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot); 4218*fa3d4c19Sdan if( rc==SQLITE_OK ){ 4219*fa3d4c19Sdan bUnlock = 1; 4220*fa3d4c19Sdan rc = sqlite3BtreeCommit(pBt); 4221*fa3d4c19Sdan } 4222*fa3d4c19Sdan } 4223*fa3d4c19Sdan }else{ 4224*fa3d4c19Sdan rc = SQLITE_OK; 4225*fa3d4c19Sdan } 4226*fa3d4c19Sdan if( rc==SQLITE_OK ){ 4227*fa3d4c19Sdan rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot); 4228*fa3d4c19Sdan } 4229fc1acf33Sdan if( rc==SQLITE_OK ){ 4230685a50adSdrh rc = sqlite3BtreeBeginTrans(pBt, 0, 0); 4231*fa3d4c19Sdan sqlite3PagerSnapshotOpen(pPager, 0); 4232*fa3d4c19Sdan } 4233*fa3d4c19Sdan if( bUnlock ){ 4234*fa3d4c19Sdan sqlite3PagerSnapshotUnlock(pPager); 4235fc1acf33Sdan } 4236fc1acf33Sdan } 4237fc1acf33Sdan } 4238fc1acf33Sdan } 4239fc1acf33Sdan 4240fc1acf33Sdan sqlite3_mutex_leave(db->mutex); 4241fc1acf33Sdan #endif /* SQLITE_OMIT_WAL */ 4242fc1acf33Sdan return rc; 4243fc1acf33Sdan } 4244fc1acf33Sdan 4245fc1acf33Sdan /* 42461158498dSdan ** Recover as many snapshots as possible from the wal file associated with 42471158498dSdan ** schema zDb of database db. 42481158498dSdan */ 42491158498dSdan int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){ 42501158498dSdan int rc = SQLITE_ERROR; 42511158498dSdan int iDb; 42521158498dSdan #ifndef SQLITE_OMIT_WAL 42531158498dSdan 42541158498dSdan #ifdef SQLITE_ENABLE_API_ARMOR 42551158498dSdan if( !sqlite3SafetyCheckOk(db) ){ 42561158498dSdan return SQLITE_MISUSE_BKPT; 42571158498dSdan } 42581158498dSdan #endif 42591158498dSdan 42601158498dSdan sqlite3_mutex_enter(db->mutex); 42611158498dSdan iDb = sqlite3FindDbName(db, zDb); 42621158498dSdan if( iDb==0 || iDb>1 ){ 42631158498dSdan Btree *pBt = db->aDb[iDb].pBt; 42641158498dSdan if( 0==sqlite3BtreeIsInReadTrans(pBt) ){ 4265685a50adSdrh rc = sqlite3BtreeBeginTrans(pBt, 0, 0); 426693f51132Sdan if( rc==SQLITE_OK ){ 42671158498dSdan rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt)); 426893f51132Sdan sqlite3BtreeCommit(pBt); 426993f51132Sdan } 42701158498dSdan } 42711158498dSdan } 42721158498dSdan sqlite3_mutex_leave(db->mutex); 42731158498dSdan #endif /* SQLITE_OMIT_WAL */ 42741158498dSdan return rc; 42751158498dSdan } 42761158498dSdan 42771158498dSdan /* 4278fc1acf33Sdan ** Free a snapshot handle obtained from sqlite3_snapshot_get(). 4279fc1acf33Sdan */ 4280fc1acf33Sdan void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){ 4281fc1acf33Sdan sqlite3_free(pSnapshot); 4282fc1acf33Sdan } 4283fc1acf33Sdan #endif /* SQLITE_ENABLE_SNAPSHOT */ 4284da1f49b8Sdan 4285da1f49b8Sdan #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 4286da1f49b8Sdan /* 4287da1f49b8Sdan ** Given the name of a compile-time option, return true if that option 4288da1f49b8Sdan ** was used and false if not. 4289da1f49b8Sdan ** 4290da1f49b8Sdan ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix 4291da1f49b8Sdan ** is not required for a match. 4292da1f49b8Sdan */ 4293da1f49b8Sdan int sqlite3_compileoption_used(const char *zOptName){ 4294da1f49b8Sdan int i, n; 4295da1f49b8Sdan int nOpt; 4296da1f49b8Sdan const char **azCompileOpt; 4297da1f49b8Sdan 4298da1f49b8Sdan #if SQLITE_ENABLE_API_ARMOR 4299da1f49b8Sdan if( zOptName==0 ){ 4300da1f49b8Sdan (void)SQLITE_MISUSE_BKPT; 4301da1f49b8Sdan return 0; 4302da1f49b8Sdan } 4303da1f49b8Sdan #endif 4304da1f49b8Sdan 4305da1f49b8Sdan azCompileOpt = sqlite3CompileOptions(&nOpt); 4306da1f49b8Sdan 4307da1f49b8Sdan if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7; 4308da1f49b8Sdan n = sqlite3Strlen30(zOptName); 4309da1f49b8Sdan 4310da1f49b8Sdan /* Since nOpt is normally in single digits, a linear search is 4311da1f49b8Sdan ** adequate. No need for a binary search. */ 4312da1f49b8Sdan for(i=0; i<nOpt; i++){ 4313da1f49b8Sdan if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0 4314da1f49b8Sdan && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0 4315da1f49b8Sdan ){ 4316da1f49b8Sdan return 1; 4317da1f49b8Sdan } 4318da1f49b8Sdan } 4319da1f49b8Sdan return 0; 4320da1f49b8Sdan } 4321da1f49b8Sdan 4322da1f49b8Sdan /* 4323da1f49b8Sdan ** Return the N-th compile-time option string. If N is out of range, 4324da1f49b8Sdan ** return a NULL pointer. 4325da1f49b8Sdan */ 4326da1f49b8Sdan const char *sqlite3_compileoption_get(int N){ 4327da1f49b8Sdan int nOpt; 4328da1f49b8Sdan const char **azCompileOpt; 4329da1f49b8Sdan azCompileOpt = sqlite3CompileOptions(&nOpt); 4330da1f49b8Sdan if( N>=0 && N<nOpt ){ 4331da1f49b8Sdan return azCompileOpt[N]; 4332da1f49b8Sdan } 4333da1f49b8Sdan return 0; 4334da1f49b8Sdan } 4335da1f49b8Sdan #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ 4336