drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Main file for the SQLite library. The routines in this file |
| 13 | ** implement the programmer interface to the library. Routines in |
| 14 | ** other files are for internal use by SQLite and should not be |
| 15 | ** accessed by users of the library. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
drh | 58f1c8b | 2008-05-26 20:19:25 +0000 | [diff] [blame] | 18 | |
drh | 820a906 | 2008-01-31 13:35:48 +0000 | [diff] [blame] | 19 | #ifdef SQLITE_ENABLE_FTS3 |
| 20 | # include "fts3.h" |
| 21 | #endif |
drh | 58f1c8b | 2008-05-26 20:19:25 +0000 | [diff] [blame] | 22 | #ifdef SQLITE_ENABLE_RTREE |
| 23 | # include "rtree.h" |
| 24 | #endif |
danielk1977 | 1c82665 | 2008-09-08 08:08:09 +0000 | [diff] [blame] | 25 | #ifdef SQLITE_ENABLE_ICU |
| 26 | # include "sqliteicu.h" |
| 27 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 28 | |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 29 | #ifndef SQLITE_AMALGAMATION |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 30 | /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant |
| 31 | ** contains the text of SQLITE_VERSION macro. |
| 32 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 33 | const char sqlite3_version[] = SQLITE_VERSION; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 34 | #endif |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 35 | |
| 36 | /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns |
| 37 | ** a pointer to the to the sqlite3_version[] string constant. |
| 38 | */ |
drh | 4aec8b6 | 2004-08-28 16:19:00 +0000 | [diff] [blame] | 39 | const char *sqlite3_libversion(void){ return sqlite3_version; } |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 40 | |
| 41 | /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a |
| 42 | ** pointer to a string constant whose value is the same as the |
| 43 | ** SQLITE_SOURCE_ID C preprocessor macro. |
| 44 | */ |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 45 | const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 46 | |
| 47 | /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function |
| 48 | ** returns an integer equal to SQLITE_VERSION_NUMBER. |
| 49 | */ |
danielk1977 | 99ba19e | 2005-02-05 07:33:34 +0000 | [diff] [blame] | 50 | int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 51 | |
drh | 5dc2bcd | 2012-01-02 15:45:12 +0000 | [diff] [blame] | 52 | /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 53 | ** zero if and only if SQLite was compiled with mutexing code omitted due to |
drh | 9f129f4 | 2010-08-31 15:27:32 +0000 | [diff] [blame] | 54 | ** the SQLITE_THREADSAFE compile-time option being set to 0. |
| 55 | */ |
drh | b67e8bf | 2007-08-30 20:09:48 +0000 | [diff] [blame] | 56 | int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 57 | |
drh | e265b08 | 2008-05-01 17:03:49 +0000 | [diff] [blame] | 58 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 59 | /* |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 60 | ** If the following function pointer is not NULL and if |
| 61 | ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing |
| 62 | ** I/O active are written using this function. These messages |
| 63 | ** are intended for debugging activity only. |
| 64 | */ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 65 | void (*sqlite3IoTrace)(const char*, ...) = 0; |
drh | e265b08 | 2008-05-01 17:03:49 +0000 | [diff] [blame] | 66 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
drh | a16313e | 2007-03-30 11:29:32 +0000 | [diff] [blame] | 69 | ** If the following global variable points to a string which is the |
| 70 | ** name of a directory, then that directory will be used to store |
| 71 | ** temporary files. |
| 72 | ** |
| 73 | ** See also the "PRAGMA temp_store_directory" SQL command. |
| 74 | */ |
| 75 | char *sqlite3_temp_directory = 0; |
| 76 | |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 77 | /* |
mistachkin | a112d14 | 2012-03-14 00:44:01 +0000 | [diff] [blame] | 78 | ** If the following global variable points to a string which is the |
| 79 | ** name of a directory, then that directory will be used to store |
| 80 | ** all database files specified with a relative pathname. |
| 81 | ** |
| 82 | ** See also the "PRAGMA data_store_directory" SQL command. |
| 83 | */ |
| 84 | char *sqlite3_data_directory = 0; |
| 85 | |
| 86 | /* |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 87 | ** Initialize SQLite. |
| 88 | ** |
| 89 | ** This routine must be called to initialize the memory allocation, |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 90 | ** VFS, and mutex subsystems prior to doing any serious work with |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 91 | ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT |
| 92 | ** this routine will be called automatically by key routines such as |
| 93 | ** sqlite3_open(). |
| 94 | ** |
| 95 | ** This routine is a no-op except on its very first call for the process, |
| 96 | ** or for the first call after a call to sqlite3_shutdown. |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 97 | ** |
| 98 | ** The first thread to call this routine runs the initialization to |
| 99 | ** completion. If subsequent threads call this routine before the first |
| 100 | ** thread has finished the initialization process, then the subsequent |
| 101 | ** threads must block until the first thread finishes with the initialization. |
| 102 | ** |
| 103 | ** The first thread might call this routine recursively. Recursive |
| 104 | ** calls to this routine should not block, of course. Otherwise the |
| 105 | ** initialization process would never complete. |
| 106 | ** |
| 107 | ** Let X be the first thread to enter this routine. Let Y be some other |
| 108 | ** thread. Then while the initial invocation of this routine by X is |
| 109 | ** incomplete, it is required that: |
| 110 | ** |
| 111 | ** * Calls to this routine from Y must block until the outer-most |
| 112 | ** call by X completes. |
| 113 | ** |
| 114 | ** * Recursive calls to this routine from thread X return immediately |
| 115 | ** without blocking. |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 116 | */ |
| 117 | int sqlite3_initialize(void){ |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 118 | MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 119 | int rc; /* Result code */ |
| 120 | |
| 121 | #ifdef SQLITE_OMIT_WSD |
danielk1977 | a8f83bf | 2008-09-02 16:22:28 +0000 | [diff] [blame] | 122 | rc = sqlite3_wsd_init(4096, 24); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 123 | if( rc!=SQLITE_OK ){ |
| 124 | return rc; |
| 125 | } |
| 126 | #endif |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 127 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 128 | /* If SQLite is already completely initialized, then this call |
| 129 | ** to sqlite3_initialize() should be a no-op. But the initialization |
| 130 | ** must be complete. So isInit must not be set until the very end |
| 131 | ** of this routine. |
| 132 | */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 133 | if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 134 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 135 | /* Make sure the mutex subsystem is initialized. If unable to |
| 136 | ** initialize the mutex subsystem, return early with the error. |
| 137 | ** If the system is so sick that we are unable to allocate a mutex, |
| 138 | ** there is not much SQLite is going to be able to do. |
| 139 | ** |
| 140 | ** The mutex subsystem must take care of serializing its own |
| 141 | ** initialization. |
| 142 | */ |
danielk1977 | d025174 | 2008-06-18 18:57:42 +0000 | [diff] [blame] | 143 | rc = sqlite3MutexInit(); |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 144 | if( rc ) return rc; |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 145 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 146 | /* Initialize the malloc() system and the recursive pInitMutex mutex. |
| 147 | ** This operation is protected by the STATIC_MASTER mutex. Note that |
| 148 | ** MutexAlloc() is called for a static mutex prior to initializing the |
| 149 | ** malloc subsystem - this implies that the allocation of a static |
| 150 | ** mutex must not require support from the malloc subsystem. |
| 151 | */ |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 152 | MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 153 | sqlite3_mutex_enter(pMaster); |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 154 | sqlite3GlobalConfig.isMutexInit = 1; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 155 | if( !sqlite3GlobalConfig.isMallocInit ){ |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 156 | rc = sqlite3MallocInit(); |
| 157 | } |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 158 | if( rc==SQLITE_OK ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 159 | sqlite3GlobalConfig.isMallocInit = 1; |
| 160 | if( !sqlite3GlobalConfig.pInitMutex ){ |
drh | 9ac0650 | 2009-08-17 13:42:29 +0000 | [diff] [blame] | 161 | sqlite3GlobalConfig.pInitMutex = |
| 162 | sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 163 | if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 164 | rc = SQLITE_NOMEM; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
danielk1977 | 77eb5bb | 2008-09-22 17:22:19 +0000 | [diff] [blame] | 167 | } |
| 168 | if( rc==SQLITE_OK ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 169 | sqlite3GlobalConfig.nRefInitMutex++; |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 170 | } |
| 171 | sqlite3_mutex_leave(pMaster); |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 172 | |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 173 | /* If rc is not SQLITE_OK at this point, then either the malloc |
| 174 | ** subsystem could not be initialized or the system failed to allocate |
| 175 | ** the pInitMutex mutex. Return an error in either case. */ |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 176 | if( rc!=SQLITE_OK ){ |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | /* Do the rest of the initialization under the recursive mutex so |
| 181 | ** that we will be able to handle recursive calls into |
| 182 | ** sqlite3_initialize(). The recursive calls normally come through |
| 183 | ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other |
| 184 | ** recursive calls might also be possible. |
drh | f759bb8 | 2010-09-09 18:25:34 +0000 | [diff] [blame] | 185 | ** |
| 186 | ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls |
| 187 | ** to the xInit method, so the xInit method need not be threadsafe. |
| 188 | ** |
| 189 | ** The following mutex is what serializes access to the appdef pcache xInit |
| 190 | ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the |
| 191 | ** call to sqlite3PcacheInitialize(). |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 192 | */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 193 | sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 194 | if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 195 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 196 | sqlite3GlobalConfig.inProgress = 1; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 197 | memset(pHash, 0, sizeof(sqlite3GlobalFunctions)); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 198 | sqlite3RegisterGlobalFunctions(); |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 199 | if( sqlite3GlobalConfig.isPCacheInit==0 ){ |
| 200 | rc = sqlite3PcacheInitialize(); |
| 201 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 202 | if( rc==SQLITE_OK ){ |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 203 | sqlite3GlobalConfig.isPCacheInit = 1; |
dan | 3d6e060 | 2009-08-17 15:52:25 +0000 | [diff] [blame] | 204 | rc = sqlite3OsInit(); |
drh | 0bf9f7b | 2009-04-17 16:54:22 +0000 | [diff] [blame] | 205 | } |
| 206 | if( rc==SQLITE_OK ){ |
danielk1977 | 5b77529 | 2008-09-02 09:38:06 +0000 | [diff] [blame] | 207 | sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, |
| 208 | sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); |
drh | 0bf9f7b | 2009-04-17 16:54:22 +0000 | [diff] [blame] | 209 | sqlite3GlobalConfig.isInit = 1; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 210 | } |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 211 | sqlite3GlobalConfig.inProgress = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 212 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 213 | sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 214 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 215 | /* Go back under the static mutex and clean up the recursive |
| 216 | ** mutex to prevent a resource leak. |
| 217 | */ |
| 218 | sqlite3_mutex_enter(pMaster); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 219 | sqlite3GlobalConfig.nRefInitMutex--; |
| 220 | if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ |
| 221 | assert( sqlite3GlobalConfig.nRefInitMutex==0 ); |
| 222 | sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); |
| 223 | sqlite3GlobalConfig.pInitMutex = 0; |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 224 | } |
| 225 | sqlite3_mutex_leave(pMaster); |
| 226 | |
| 227 | /* The following is just a sanity check to make sure SQLite has |
| 228 | ** been compiled correctly. It is important to run this code, but |
| 229 | ** we don't want to run it too often and soak up CPU cycles for no |
| 230 | ** reason. So we run it once during initialization. |
| 231 | */ |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 232 | #ifndef NDEBUG |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 233 | #ifndef SQLITE_OMIT_FLOATING_POINT |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 234 | /* This section of code's only "output" is via assert() statements. */ |
| 235 | if ( rc==SQLITE_OK ){ |
| 236 | u64 x = (((u64)1)<<63)-1; |
| 237 | double y; |
| 238 | assert(sizeof(x)==8); |
| 239 | assert(sizeof(x)==sizeof(y)); |
| 240 | memcpy(&y, &x, 8); |
| 241 | assert( sqlite3IsNaN(y) ); |
| 242 | } |
| 243 | #endif |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 244 | #endif |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 245 | |
drh | e4cf0b3 | 2011-08-25 00:14:41 +0000 | [diff] [blame] | 246 | /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT |
| 247 | ** compile-time option. |
| 248 | */ |
| 249 | #ifdef SQLITE_EXTRA_INIT |
| 250 | if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){ |
drh | c7f9462 | 2011-12-13 18:22:38 +0000 | [diff] [blame] | 251 | int SQLITE_EXTRA_INIT(const char*); |
| 252 | rc = SQLITE_EXTRA_INIT(0); |
drh | e4cf0b3 | 2011-08-25 00:14:41 +0000 | [diff] [blame] | 253 | } |
| 254 | #endif |
| 255 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | ** Undo the effects of sqlite3_initialize(). Must not be called while |
| 261 | ** there are outstanding database connections or memory allocations or |
| 262 | ** while any part of SQLite is otherwise in use in any thread. This |
drh | d1a2440 | 2009-04-19 12:23:58 +0000 | [diff] [blame] | 263 | ** routine is not threadsafe. But it is safe to invoke this routine |
| 264 | ** on when SQLite is already shut down. If SQLite is already shut down |
| 265 | ** when this routine is invoked, then this routine is a harmless no-op. |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 266 | */ |
| 267 | int sqlite3_shutdown(void){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 268 | if( sqlite3GlobalConfig.isInit ){ |
drh | 9797706 | 2011-12-13 01:34:21 +0000 | [diff] [blame] | 269 | #ifdef SQLITE_EXTRA_SHUTDOWN |
| 270 | void SQLITE_EXTRA_SHUTDOWN(void); |
| 271 | SQLITE_EXTRA_SHUTDOWN(); |
| 272 | #endif |
drh | a764092 | 2009-04-21 12:02:56 +0000 | [diff] [blame] | 273 | sqlite3_os_end(); |
drh | d1a2440 | 2009-04-19 12:23:58 +0000 | [diff] [blame] | 274 | sqlite3_reset_auto_extension(); |
drh | d1a2440 | 2009-04-19 12:23:58 +0000 | [diff] [blame] | 275 | sqlite3GlobalConfig.isInit = 0; |
danielk1977 | fb43727 | 2008-07-08 12:02:35 +0000 | [diff] [blame] | 276 | } |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 277 | if( sqlite3GlobalConfig.isPCacheInit ){ |
| 278 | sqlite3PcacheShutdown(); |
| 279 | sqlite3GlobalConfig.isPCacheInit = 0; |
| 280 | } |
| 281 | if( sqlite3GlobalConfig.isMallocInit ){ |
| 282 | sqlite3MallocEnd(); |
| 283 | sqlite3GlobalConfig.isMallocInit = 0; |
mistachkin | 86f8987 | 2012-03-18 01:32:44 +0000 | [diff] [blame] | 284 | |
| 285 | #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES |
| 286 | /* The heap subsystem has now been shutdown and these values are supposed |
| 287 | ** to be NULL or point to memory that was obtained from sqlite3_malloc(), |
| 288 | ** which would rely on that heap subsystem; therefore, make sure these |
| 289 | ** values cannot refer to heap memory that was just invalidated when the |
| 290 | ** heap subsystem was shutdown. This is only done if the current call to |
| 291 | ** this function resulted in the heap subsystem actually being shutdown. |
| 292 | */ |
| 293 | sqlite3_data_directory = 0; |
| 294 | sqlite3_temp_directory = 0; |
| 295 | #endif |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 296 | } |
| 297 | if( sqlite3GlobalConfig.isMutexInit ){ |
| 298 | sqlite3MutexEnd(); |
| 299 | sqlite3GlobalConfig.isMutexInit = 0; |
| 300 | } |
| 301 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 302 | return SQLITE_OK; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | ** This API allows applications to modify the global configuration of |
| 307 | ** the SQLite library at run-time. |
| 308 | ** |
| 309 | ** This routine should only be called when there are no outstanding |
| 310 | ** database connections or memory allocations. This routine is not |
| 311 | ** threadsafe. Failure to heed these warnings can lead to unpredictable |
| 312 | ** behavior. |
| 313 | */ |
| 314 | int sqlite3_config(int op, ...){ |
| 315 | va_list ap; |
| 316 | int rc = SQLITE_OK; |
| 317 | |
| 318 | /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while |
| 319 | ** the SQLite library is in use. */ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 320 | if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 321 | |
| 322 | va_start(ap, op); |
| 323 | switch( op ){ |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 324 | |
| 325 | /* Mutex configuration options are only available in a threadsafe |
| 326 | ** compile. |
| 327 | */ |
drh | d68eee0 | 2009-12-11 03:44:18 +0000 | [diff] [blame] | 328 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 329 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 330 | /* Disable all mutexing */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 331 | sqlite3GlobalConfig.bCoreMutex = 0; |
| 332 | sqlite3GlobalConfig.bFullMutex = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | case SQLITE_CONFIG_MULTITHREAD: { |
| 336 | /* Disable mutexing of database connections */ |
| 337 | /* Enable mutexing of core data structures */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 338 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 339 | sqlite3GlobalConfig.bFullMutex = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | case SQLITE_CONFIG_SERIALIZED: { |
| 343 | /* Enable all mutexing */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 344 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 345 | sqlite3GlobalConfig.bFullMutex = 1; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 346 | break; |
| 347 | } |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 348 | case SQLITE_CONFIG_MUTEX: { |
| 349 | /* Specify an alternative mutex implementation */ |
| 350 | sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 351 | break; |
| 352 | } |
| 353 | case SQLITE_CONFIG_GETMUTEX: { |
| 354 | /* Retrieve the current mutex implementation */ |
| 355 | *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 356 | break; |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 361 | case SQLITE_CONFIG_MALLOC: { |
| 362 | /* Specify an alternative malloc implementation */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 363 | sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 366 | case SQLITE_CONFIG_GETMALLOC: { |
danielk1977 | 57e5ea9 | 2008-06-24 19:02:55 +0000 | [diff] [blame] | 367 | /* Retrieve the current malloc() implementation */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 368 | if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 369 | *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 370 | break; |
| 371 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 372 | case SQLITE_CONFIG_MEMSTATUS: { |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 373 | /* Enable or disable the malloc status collection */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 374 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 377 | case SQLITE_CONFIG_SCRATCH: { |
| 378 | /* Designate a buffer for scratch memory space */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 379 | sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 380 | sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 381 | sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 382 | break; |
| 383 | } |
| 384 | case SQLITE_CONFIG_PAGECACHE: { |
drh | b093719 | 2009-05-22 10:53:29 +0000 | [diff] [blame] | 385 | /* Designate a buffer for page cache memory space */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 386 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 387 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 388 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 389 | break; |
| 390 | } |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 391 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 392 | case SQLITE_CONFIG_PCACHE: { |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 393 | /* no-op */ |
| 394 | break; |
| 395 | } |
| 396 | case SQLITE_CONFIG_GETPCACHE: { |
| 397 | /* now an error */ |
| 398 | rc = SQLITE_ERROR; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 399 | break; |
| 400 | } |
| 401 | |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 402 | case SQLITE_CONFIG_PCACHE2: { |
| 403 | /* Specify an alternative page cache implementation */ |
| 404 | sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 405 | break; |
| 406 | } |
| 407 | case SQLITE_CONFIG_GETPCACHE2: { |
| 408 | if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
drh | b232c23 | 2008-11-19 01:20:26 +0000 | [diff] [blame] | 409 | sqlite3PCacheSetDefault(); |
| 410 | } |
dan | 22e21ff | 2011-11-08 20:08:44 +0000 | [diff] [blame] | 411 | *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
drh | b232c23 | 2008-11-19 01:20:26 +0000 | [diff] [blame] | 412 | break; |
| 413 | } |
| 414 | |
drh | 5d41483 | 2008-07-15 14:47:18 +0000 | [diff] [blame] | 415 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 416 | case SQLITE_CONFIG_HEAP: { |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 417 | /* Designate a buffer for heap memory space */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 418 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 419 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 420 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 421 | |
shaneh | a6ec892 | 2011-03-09 21:36:17 +0000 | [diff] [blame] | 422 | if( sqlite3GlobalConfig.mnReq<1 ){ |
| 423 | sqlite3GlobalConfig.mnReq = 1; |
| 424 | }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ |
| 425 | /* cap min request size at 2^12 */ |
| 426 | sqlite3GlobalConfig.mnReq = (1<<12); |
| 427 | } |
| 428 | |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 429 | if( sqlite3GlobalConfig.pHeap==0 ){ |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 430 | /* If the heap pointer is NULL, then restore the malloc implementation |
| 431 | ** back to NULL pointers too. This will cause the malloc to go |
| 432 | ** back to its default implementation when sqlite3_initialize() is |
| 433 | ** run. |
| 434 | */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 435 | memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 436 | }else{ |
| 437 | /* The heap pointer is not NULL, then install one of the |
| 438 | ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor |
| 439 | ** ENABLE_MEMSYS5 is defined, return an error. |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 440 | */ |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 441 | #ifdef SQLITE_ENABLE_MEMSYS3 |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 442 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 443 | #endif |
| 444 | #ifdef SQLITE_ENABLE_MEMSYS5 |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 445 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 446 | #endif |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 447 | } |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 448 | break; |
| 449 | } |
drh | 5d41483 | 2008-07-15 14:47:18 +0000 | [diff] [blame] | 450 | #endif |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 451 | |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 452 | case SQLITE_CONFIG_LOOKASIDE: { |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 453 | sqlite3GlobalConfig.szLookaside = va_arg(ap, int); |
| 454 | sqlite3GlobalConfig.nLookaside = va_arg(ap, int); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 457 | |
| 458 | /* Record a pointer to the logger funcction and its first argument. |
| 459 | ** The default is NULL. Logging is disabled if the function pointer is |
| 460 | ** NULL. |
| 461 | */ |
| 462 | case SQLITE_CONFIG_LOG: { |
shaneh | dc97a8c | 2010-02-23 20:08:35 +0000 | [diff] [blame] | 463 | /* MSVC is picky about pulling func ptrs from va lists. |
| 464 | ** http://support.microsoft.com/kb/47961 |
| 465 | ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); |
| 466 | */ |
| 467 | typedef void(*LOGFUNC_t)(void*,int,const char*); |
| 468 | sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 469 | sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); |
| 470 | break; |
| 471 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 472 | |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 473 | case SQLITE_CONFIG_URI: { |
| 474 | sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 475 | break; |
| 476 | } |
| 477 | |
drh | de9a7b8 | 2012-09-17 20:44:46 +0000 | [diff] [blame] | 478 | case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 479 | sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 480 | break; |
| 481 | } |
| 482 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 483 | default: { |
| 484 | rc = SQLITE_ERROR; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | va_end(ap); |
| 489 | return rc; |
| 490 | } |
| 491 | |
| 492 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 493 | ** Set up the lookaside buffers for a database connection. |
| 494 | ** Return SQLITE_OK on success. |
| 495 | ** If lookaside is already active, return SQLITE_BUSY. |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 496 | ** |
| 497 | ** The sz parameter is the number of bytes in each lookaside slot. |
| 498 | ** The cnt parameter is the number of slots. If pStart is NULL the |
| 499 | ** space for the lookaside memory is obtained from sqlite3_malloc(). |
| 500 | ** If pStart is not NULL then it is sz*cnt bytes of memory to use for |
| 501 | ** the lookaside memory. |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 502 | */ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 503 | static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 504 | void *pStart; |
| 505 | if( db->lookaside.nOut ){ |
| 506 | return SQLITE_BUSY; |
| 507 | } |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 508 | /* Free any existing lookaside buffer for this handle before |
| 509 | ** allocating a new one so we don't have to have space for |
| 510 | ** both at the same time. |
| 511 | */ |
| 512 | if( db->lookaside.bMalloced ){ |
| 513 | sqlite3_free(db->lookaside.pStart); |
| 514 | } |
drh | 61a4bd5 | 2011-11-10 02:39:28 +0000 | [diff] [blame] | 515 | /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger |
| 516 | ** than a pointer to be useful. |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 517 | */ |
drh | 61a4bd5 | 2011-11-10 02:39:28 +0000 | [diff] [blame] | 518 | sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 519 | if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 520 | if( cnt<0 ) cnt = 0; |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 521 | if( sz==0 || cnt==0 ){ |
| 522 | sz = 0; |
| 523 | pStart = 0; |
| 524 | }else if( pBuf==0 ){ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 525 | sqlite3BeginBenignMalloc(); |
drh | 9dd55f5 | 2010-09-03 03:32:46 +0000 | [diff] [blame] | 526 | pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 527 | sqlite3EndBenignMalloc(); |
drh | 8225d66 | 2011-11-10 02:24:11 +0000 | [diff] [blame] | 528 | if( pStart ) cnt = sqlite3MallocSize(pStart)/sz; |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 529 | }else{ |
| 530 | pStart = pBuf; |
| 531 | } |
drh | 4cfb22f | 2008-08-01 18:47:01 +0000 | [diff] [blame] | 532 | db->lookaside.pStart = pStart; |
| 533 | db->lookaside.pFree = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 534 | db->lookaside.sz = (u16)sz; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 535 | if( pStart ){ |
| 536 | int i; |
| 537 | LookasideSlot *p; |
drh | de46798 | 2009-04-02 17:22:41 +0000 | [diff] [blame] | 538 | assert( sz > (int)sizeof(LookasideSlot*) ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 539 | p = (LookasideSlot*)pStart; |
| 540 | for(i=cnt-1; i>=0; i--){ |
| 541 | p->pNext = db->lookaside.pFree; |
| 542 | db->lookaside.pFree = p; |
| 543 | p = (LookasideSlot*)&((u8*)p)[sz]; |
| 544 | } |
| 545 | db->lookaside.pEnd = p; |
| 546 | db->lookaside.bEnabled = 1; |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 547 | db->lookaside.bMalloced = pBuf==0 ?1:0; |
drh | 4cfb22f | 2008-08-01 18:47:01 +0000 | [diff] [blame] | 548 | }else{ |
| 549 | db->lookaside.pEnd = 0; |
| 550 | db->lookaside.bEnabled = 0; |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 551 | db->lookaside.bMalloced = 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 552 | } |
| 553 | return SQLITE_OK; |
| 554 | } |
| 555 | |
| 556 | /* |
drh | 4413d0e | 2008-11-04 13:46:27 +0000 | [diff] [blame] | 557 | ** Return the mutex associated with a database connection. |
| 558 | */ |
| 559 | sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 560 | return db->mutex; |
| 561 | } |
| 562 | |
| 563 | /* |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 564 | ** Free up as much memory as we can from the given database |
| 565 | ** connection. |
| 566 | */ |
| 567 | int sqlite3_db_release_memory(sqlite3 *db){ |
| 568 | int i; |
dan | d9bb3a9 | 2011-12-30 11:43:59 +0000 | [diff] [blame] | 569 | sqlite3_mutex_enter(db->mutex); |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 570 | sqlite3BtreeEnterAll(db); |
| 571 | for(i=0; i<db->nDb; i++){ |
| 572 | Btree *pBt = db->aDb[i].pBt; |
| 573 | if( pBt ){ |
| 574 | Pager *pPager = sqlite3BtreePager(pBt); |
| 575 | sqlite3PagerShrink(pPager); |
| 576 | } |
| 577 | } |
| 578 | sqlite3BtreeLeaveAll(db); |
dan | d9bb3a9 | 2011-12-30 11:43:59 +0000 | [diff] [blame] | 579 | sqlite3_mutex_leave(db->mutex); |
drh | 09419b4 | 2011-11-16 19:29:17 +0000 | [diff] [blame] | 580 | return SQLITE_OK; |
| 581 | } |
| 582 | |
| 583 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 584 | ** Configuration settings for an individual database connection |
| 585 | */ |
| 586 | int sqlite3_db_config(sqlite3 *db, int op, ...){ |
| 587 | va_list ap; |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 588 | int rc; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 589 | va_start(ap, op); |
| 590 | switch( op ){ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 591 | case SQLITE_DBCONFIG_LOOKASIDE: { |
drh | 8b2b2e6 | 2011-04-07 01:14:12 +0000 | [diff] [blame] | 592 | void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ |
drh | 9dd55f5 | 2010-09-03 03:32:46 +0000 | [diff] [blame] | 593 | int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ |
| 594 | int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 595 | rc = setupLookaside(db, pBuf, sz, cnt); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 596 | break; |
| 597 | } |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 598 | default: { |
drh | e83cafd | 2011-03-21 17:15:58 +0000 | [diff] [blame] | 599 | static const struct { |
| 600 | int op; /* The opcode */ |
| 601 | u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */ |
| 602 | } aFlagOp[] = { |
| 603 | { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys }, |
| 604 | { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger }, |
| 605 | }; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 606 | unsigned int i; |
drh | 9dd55f5 | 2010-09-03 03:32:46 +0000 | [diff] [blame] | 607 | rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ |
drh | e83cafd | 2011-03-21 17:15:58 +0000 | [diff] [blame] | 608 | for(i=0; i<ArraySize(aFlagOp); i++){ |
| 609 | if( aFlagOp[i].op==op ){ |
| 610 | int onoff = va_arg(ap, int); |
| 611 | int *pRes = va_arg(ap, int*); |
drh | d7b302b | 2011-03-23 22:54:59 +0000 | [diff] [blame] | 612 | int oldFlags = db->flags; |
drh | e83cafd | 2011-03-21 17:15:58 +0000 | [diff] [blame] | 613 | if( onoff>0 ){ |
| 614 | db->flags |= aFlagOp[i].mask; |
| 615 | }else if( onoff==0 ){ |
| 616 | db->flags &= ~aFlagOp[i].mask; |
| 617 | } |
| 618 | if( oldFlags!=db->flags ){ |
| 619 | sqlite3ExpirePreparedStatements(db); |
| 620 | } |
| 621 | if( pRes ){ |
| 622 | *pRes = (db->flags & aFlagOp[i].mask)!=0; |
| 623 | } |
| 624 | rc = SQLITE_OK; |
| 625 | break; |
| 626 | } |
| 627 | } |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 628 | break; |
| 629 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 630 | } |
| 631 | va_end(ap); |
| 632 | return rc; |
| 633 | } |
| 634 | |
drh | a16313e | 2007-03-30 11:29:32 +0000 | [diff] [blame] | 635 | |
| 636 | /* |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 637 | ** Return true if the buffer z[0..n-1] contains all spaces. |
| 638 | */ |
| 639 | static int allSpaces(const char *z, int n){ |
drh | 5f3a367 | 2008-04-15 04:02:40 +0000 | [diff] [blame] | 640 | while( n>0 && z[n-1]==' ' ){ n--; } |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 641 | return n==0; |
| 642 | } |
| 643 | |
| 644 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 645 | ** This is the default collating function named "BINARY" which is always |
| 646 | ** available. |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 647 | ** |
| 648 | ** If the padFlag argument is not NULL then space padding at the end |
| 649 | ** of strings is ignored. This implements the RTRIM collation. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 650 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 651 | static int binCollFunc( |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 652 | void *padFlag, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 653 | int nKey1, const void *pKey1, |
| 654 | int nKey2, const void *pKey2 |
| 655 | ){ |
| 656 | int rc, n; |
| 657 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 658 | rc = memcmp(pKey1, pKey2, n); |
| 659 | if( rc==0 ){ |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 660 | if( padFlag |
| 661 | && allSpaces(((char*)pKey1)+n, nKey1-n) |
| 662 | && allSpaces(((char*)pKey2)+n, nKey2-n) |
| 663 | ){ |
| 664 | /* Leave rc unchanged at 0 */ |
| 665 | }else{ |
| 666 | rc = nKey1 - nKey2; |
| 667 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 668 | } |
| 669 | return rc; |
| 670 | } |
| 671 | |
| 672 | /* |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 673 | ** Another built-in collating sequence: NOCASE. |
| 674 | ** |
| 675 | ** This collating sequence is intended to be used for "case independant |
| 676 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 677 | ** extends only to the 26 characters used in the English language. |
| 678 | ** |
| 679 | ** At the moment there is only a UTF-8 implementation. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 680 | */ |
| 681 | static int nocaseCollatingFunc( |
| 682 | void *NotUsed, |
| 683 | int nKey1, const void *pKey1, |
| 684 | int nKey2, const void *pKey2 |
| 685 | ){ |
| 686 | int r = sqlite3StrNICmp( |
drh | 208f80a | 2004-08-29 20:08:58 +0000 | [diff] [blame] | 687 | (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 688 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 689 | if( 0==r ){ |
| 690 | r = nKey1-nKey2; |
| 691 | } |
| 692 | return r; |
| 693 | } |
| 694 | |
| 695 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 696 | ** Return the ROWID of the most recent insert |
| 697 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 698 | sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 699 | return db->lastRowid; |
| 700 | } |
| 701 | |
| 702 | /* |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 703 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 704 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 705 | int sqlite3_changes(sqlite3 *db){ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 706 | return db->nChange; |
| 707 | } |
| 708 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 709 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 710 | ** Return the number of changes since the database handle was opened. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 711 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 712 | int sqlite3_total_changes(sqlite3 *db){ |
| 713 | return db->nTotalChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 714 | } |
| 715 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 716 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 717 | ** Close all open savepoints. This function only manipulates fields of the |
| 718 | ** database handle object, it does not close any savepoints that may be open |
| 719 | ** at the b-tree/pager level. |
| 720 | */ |
| 721 | void sqlite3CloseSavepoints(sqlite3 *db){ |
| 722 | while( db->pSavepoint ){ |
| 723 | Savepoint *pTmp = db->pSavepoint; |
| 724 | db->pSavepoint = pTmp->pNext; |
| 725 | sqlite3DbFree(db, pTmp); |
| 726 | } |
| 727 | db->nSavepoint = 0; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 728 | db->nStatement = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 729 | db->isTransactionSavepoint = 0; |
| 730 | } |
| 731 | |
| 732 | /* |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 733 | ** Invoke the destructor function associated with FuncDef p, if any. Except, |
| 734 | ** if this is not the last copy of the function, do not invoke it. Multiple |
| 735 | ** copies of a single function are created when create_function() is called |
| 736 | ** with SQLITE_ANY as the encoding. |
| 737 | */ |
| 738 | static void functionDestroy(sqlite3 *db, FuncDef *p){ |
| 739 | FuncDestructor *pDestructor = p->pDestructor; |
| 740 | if( pDestructor ){ |
| 741 | pDestructor->nRef--; |
| 742 | if( pDestructor->nRef==0 ){ |
| 743 | pDestructor->xDestroy(pDestructor->pUserData); |
| 744 | sqlite3DbFree(db, pDestructor); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /* |
dan | bba02a9 | 2012-05-15 17:15:34 +0000 | [diff] [blame] | 750 | ** Disconnect all sqlite3_vtab objects that belong to database connection |
| 751 | ** db. This is called when db is being closed. |
| 752 | */ |
| 753 | static void disconnectAllVtab(sqlite3 *db){ |
| 754 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 755 | int i; |
| 756 | sqlite3BtreeEnterAll(db); |
| 757 | for(i=0; i<db->nDb; i++){ |
| 758 | Schema *pSchema = db->aDb[i].pSchema; |
| 759 | if( db->aDb[i].pSchema ){ |
| 760 | HashElem *p; |
| 761 | for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ |
| 762 | Table *pTab = (Table *)sqliteHashData(p); |
| 763 | if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | sqlite3BtreeLeaveAll(db); |
| 768 | #else |
| 769 | UNUSED_PARAMETER(db); |
| 770 | #endif |
| 771 | } |
| 772 | |
| 773 | /* |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 774 | ** Return TRUE if database connection db has unfinalized prepared |
| 775 | ** statements or unfinished sqlite3_backup objects. |
| 776 | */ |
| 777 | static int connectionIsBusy(sqlite3 *db){ |
| 778 | int j; |
| 779 | assert( sqlite3_mutex_held(db->mutex) ); |
| 780 | if( db->pVdbe ) return 1; |
| 781 | for(j=0; j<db->nDb; j++){ |
| 782 | Btree *pBt = db->aDb[j].pBt; |
| 783 | if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1; |
| 784 | } |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 789 | ** Close an existing SQLite database |
| 790 | */ |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 791 | static int sqlite3Close(sqlite3 *db, int forceZombie){ |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 792 | if( !db ){ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 793 | return SQLITE_OK; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 794 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 795 | if( !sqlite3SafetyCheckSickOrOk(db) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 796 | return SQLITE_MISUSE_BKPT; |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 797 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 798 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 799 | |
dan | bba02a9 | 2012-05-15 17:15:34 +0000 | [diff] [blame] | 800 | /* Force xDisconnect calls on all virtual tables */ |
| 801 | disconnectAllVtab(db); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 802 | |
dan | bba02a9 | 2012-05-15 17:15:34 +0000 | [diff] [blame] | 803 | /* If a transaction is open, the disconnectAllVtab() call above |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 804 | ** will not have called the xDisconnect() method on any virtual |
| 805 | ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() |
| 806 | ** call will do so. We need to do this before the check for active |
| 807 | ** SQL statements below, as the v-table implementation may be storing |
| 808 | ** some prepared statements internally. |
| 809 | */ |
| 810 | sqlite3VtabRollback(db); |
| 811 | |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 812 | /* Legacy behavior (sqlite3_close() behavior) is to return |
| 813 | ** SQLITE_BUSY if the connection can not be closed immediately. |
| 814 | */ |
| 815 | if( !forceZombie && connectionIsBusy(db) ){ |
| 816 | sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized " |
| 817 | "statements or unfinished backups"); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 818 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 819 | return SQLITE_BUSY; |
| 820 | } |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 821 | |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 822 | /* Convert the connection into a zombie and then close it. |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 823 | */ |
| 824 | db->magic = SQLITE_MAGIC_ZOMBIE; |
| 825 | sqlite3LeaveMutexAndCloseZombie(db); |
| 826 | return SQLITE_OK; |
| 827 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 828 | |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 829 | /* |
| 830 | ** Two variations on the public interface for closing a database |
| 831 | ** connection. The sqlite3_close() version returns SQLITE_BUSY and |
| 832 | ** leaves the connection option if there are unfinalized prepared |
| 833 | ** statements or unfinished sqlite3_backups. The sqlite3_close_v2() |
| 834 | ** version forces the connection to become a zombie if there are |
| 835 | ** unclosed resources, and arranges for deallocation when the last |
| 836 | ** prepare statement or sqlite3_backup closes. |
| 837 | */ |
| 838 | int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); } |
| 839 | int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); } |
| 840 | |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 841 | |
| 842 | /* |
| 843 | ** Close the mutex on database connection db. |
| 844 | ** |
| 845 | ** Furthermore, if database connection db is a zombie (meaning that there |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 846 | ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and |
| 847 | ** every sqlite3_stmt has now been finalized and every sqlite3_backup has |
| 848 | ** finished, then free all resources. |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 849 | */ |
| 850 | void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ |
| 851 | HashElem *i; /* Hash table iterator */ |
| 852 | int j; |
| 853 | |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 854 | /* If there are outstanding sqlite3_stmt or sqlite3_backup objects |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 855 | ** or if the connection has not yet been closed by sqlite3_close_v2(), |
| 856 | ** then just leave the mutex and return. |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 857 | */ |
drh | 167cd6a | 2012-06-02 17:09:46 +0000 | [diff] [blame] | 858 | if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){ |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 859 | sqlite3_mutex_leave(db->mutex); |
| 860 | return; |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 861 | } |
| 862 | |
drh | 4245c40 | 2012-06-02 14:32:21 +0000 | [diff] [blame] | 863 | /* If we reach this point, it means that the database connection has |
| 864 | ** closed all sqlite3_stmt and sqlite3_backup objects and has been |
| 865 | ** pased to sqlite3_close (meaning that it is a zombie). Therefore, |
| 866 | ** go ahead and free all resources. |
| 867 | */ |
| 868 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 869 | /* Free any outstanding Savepoint structures. */ |
| 870 | sqlite3CloseSavepoints(db); |
| 871 | |
drh | 5efb314 | 2012-05-16 01:24:34 +0000 | [diff] [blame] | 872 | /* Close all database connections */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 873 | for(j=0; j<db->nDb; j++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 874 | struct Db *pDb = &db->aDb[j]; |
| 875 | if( pDb->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 876 | sqlite3BtreeClose(pDb->pBt); |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 877 | pDb->pBt = 0; |
danielk1977 | 311019b | 2006-01-10 07:14:23 +0000 | [diff] [blame] | 878 | if( j!=1 ){ |
| 879 | pDb->pSchema = 0; |
| 880 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 881 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 882 | } |
drh | 5efb314 | 2012-05-16 01:24:34 +0000 | [diff] [blame] | 883 | /* Clear the TEMP schema separately and last */ |
| 884 | if( db->aDb[1].pSchema ){ |
| 885 | sqlite3SchemaClear(db->aDb[1].pSchema); |
| 886 | } |
| 887 | sqlite3VtabUnlockList(db); |
dan | bba02a9 | 2012-05-15 17:15:34 +0000 | [diff] [blame] | 888 | |
drh | 5efb314 | 2012-05-16 01:24:34 +0000 | [diff] [blame] | 889 | /* Free up the array of auxiliary databases */ |
| 890 | sqlite3CollapseDatabaseArray(db); |
dan | bba02a9 | 2012-05-15 17:15:34 +0000 | [diff] [blame] | 891 | assert( db->nDb<=2 ); |
| 892 | assert( db->aDb==db->aDbStatic ); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 893 | |
| 894 | /* Tell the code in notify.c that the connection no longer holds any |
| 895 | ** locks and does not require any further unlock-notify callbacks. |
| 896 | */ |
| 897 | sqlite3ConnectionClosed(db); |
| 898 | |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 899 | for(j=0; j<ArraySize(db->aFunc.a); j++){ |
| 900 | FuncDef *pNext, *pHash, *p; |
| 901 | for(p=db->aFunc.a[j]; p; p=pHash){ |
| 902 | pHash = p->pHash; |
| 903 | while( p ){ |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 904 | functionDestroy(db, p); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 905 | pNext = p->pNext; |
| 906 | sqlite3DbFree(db, p); |
| 907 | p = pNext; |
| 908 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 911 | for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 912 | CollSeq *pColl = (CollSeq *)sqliteHashData(i); |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 913 | /* Invoke any destructors registered for collation sequence user data. */ |
| 914 | for(j=0; j<3; j++){ |
| 915 | if( pColl[j].xDel ){ |
| 916 | pColl[j].xDel(pColl[j].pUser); |
| 917 | } |
| 918 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 919 | sqlite3DbFree(db, pColl); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 920 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 921 | sqlite3HashClear(&db->aCollSeq); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 922 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 923 | for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ |
| 924 | Module *pMod = (Module *)sqliteHashData(i); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 925 | if( pMod->xDestroy ){ |
| 926 | pMod->xDestroy(pMod->pAux); |
| 927 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 928 | sqlite3DbFree(db, pMod); |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 929 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 930 | sqlite3HashClear(&db->aModule); |
| 931 | #endif |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 932 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 933 | sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 934 | if( db->pErr ){ |
| 935 | sqlite3ValueFree(db->pErr); |
| 936 | } |
drh | f1952c5 | 2006-06-08 15:48:00 +0000 | [diff] [blame] | 937 | sqlite3CloseExtensions(db); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 938 | |
| 939 | db->magic = SQLITE_MAGIC_ERROR; |
danielk1977 | 311019b | 2006-01-10 07:14:23 +0000 | [diff] [blame] | 940 | |
| 941 | /* The temp-database schema is allocated differently from the other schema |
| 942 | ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). |
| 943 | ** So it needs to be freed here. Todo: Why not roll the temp schema into |
| 944 | ** the same sqliteMalloc() as the one that allocates the database |
| 945 | ** structure? |
| 946 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 947 | sqlite3DbFree(db, db->aDb[1].pSchema); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 948 | sqlite3_mutex_leave(db->mutex); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 949 | db->magic = SQLITE_MAGIC_CLOSED; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 950 | sqlite3_mutex_free(db->mutex); |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 951 | assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 952 | if( db->lookaside.bMalloced ){ |
| 953 | sqlite3_free(db->lookaside.pStart); |
| 954 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 955 | sqlite3_free(db); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /* |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 959 | ** Rollback all database files. If tripCode is not SQLITE_OK, then |
| 960 | ** any open cursors are invalidated ("tripped" - as in "tripping a circuit |
| 961 | ** breaker") and made to return tripCode if there are any further |
| 962 | ** attempts to use that cursor. |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 963 | */ |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 964 | void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 965 | int i; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 966 | int inTrans = 0; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 967 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 968 | sqlite3BeginBenignMalloc(); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 969 | for(i=0; i<db->nDb; i++){ |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 970 | Btree *p = db->aDb[i].pBt; |
| 971 | if( p ){ |
| 972 | if( sqlite3BtreeIsInTrans(p) ){ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 973 | inTrans = 1; |
| 974 | } |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 975 | sqlite3BtreeRollback(p, tripCode); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 976 | db->aDb[i].inTrans = 0; |
| 977 | } |
| 978 | } |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 979 | sqlite3VtabRollback(db); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 980 | sqlite3EndBenignMalloc(); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 981 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 982 | if( db->flags&SQLITE_InternChanges ){ |
danielk1977 | ea4d9e2 | 2007-08-13 15:28:33 +0000 | [diff] [blame] | 983 | sqlite3ExpirePreparedStatements(db); |
drh | 81028a4 | 2012-05-15 18:28:27 +0000 | [diff] [blame] | 984 | sqlite3ResetAllSchemasOfConnection(db); |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 985 | } |
| 986 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 987 | /* Any deferred constraint violations have now been resolved. */ |
| 988 | db->nDeferredCons = 0; |
| 989 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 990 | /* If one has been configured, invoke the rollback-hook callback */ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 991 | if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 992 | db->xRollbackCallback(db->pRollbackArg); |
| 993 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 997 | ** Return a static string that describes the kind of error specified in the |
| 998 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 999 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 1000 | const char *sqlite3ErrStr(int rc){ |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1001 | static const char* const aMsg[] = { |
| 1002 | /* SQLITE_OK */ "not an error", |
| 1003 | /* SQLITE_ERROR */ "SQL logic error or missing database", |
| 1004 | /* SQLITE_INTERNAL */ 0, |
| 1005 | /* SQLITE_PERM */ "access permission denied", |
| 1006 | /* SQLITE_ABORT */ "callback requested query abort", |
| 1007 | /* SQLITE_BUSY */ "database is locked", |
| 1008 | /* SQLITE_LOCKED */ "database table is locked", |
| 1009 | /* SQLITE_NOMEM */ "out of memory", |
| 1010 | /* SQLITE_READONLY */ "attempt to write a readonly database", |
| 1011 | /* SQLITE_INTERRUPT */ "interrupted", |
| 1012 | /* SQLITE_IOERR */ "disk I/O error", |
| 1013 | /* SQLITE_CORRUPT */ "database disk image is malformed", |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 1014 | /* SQLITE_NOTFOUND */ "unknown operation", |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1015 | /* SQLITE_FULL */ "database or disk is full", |
| 1016 | /* SQLITE_CANTOPEN */ "unable to open database file", |
dan | 0dc7b74 | 2010-06-04 15:59:58 +0000 | [diff] [blame] | 1017 | /* SQLITE_PROTOCOL */ "locking protocol", |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1018 | /* SQLITE_EMPTY */ "table contains no data", |
| 1019 | /* SQLITE_SCHEMA */ "database schema has changed", |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 1020 | /* SQLITE_TOOBIG */ "string or blob too big", |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1021 | /* SQLITE_CONSTRAINT */ "constraint failed", |
| 1022 | /* SQLITE_MISMATCH */ "datatype mismatch", |
| 1023 | /* SQLITE_MISUSE */ "library routine called out of sequence", |
| 1024 | /* SQLITE_NOLFS */ "large file support is disabled", |
| 1025 | /* SQLITE_AUTH */ "authorization denied", |
| 1026 | /* SQLITE_FORMAT */ "auxiliary database format error", |
| 1027 | /* SQLITE_RANGE */ "bind or column index out of range", |
| 1028 | /* SQLITE_NOTADB */ "file is encrypted or is not a database", |
| 1029 | }; |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 1030 | const char *zErr = "unknown error"; |
| 1031 | switch( rc ){ |
| 1032 | case SQLITE_ABORT_ROLLBACK: { |
| 1033 | zErr = "abort due to ROLLBACK"; |
| 1034 | break; |
| 1035 | } |
| 1036 | default: { |
| 1037 | rc &= 0xff; |
| 1038 | if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){ |
| 1039 | zErr = aMsg[rc]; |
| 1040 | } |
| 1041 | break; |
| 1042 | } |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 1043 | } |
drh | 21021a5 | 2012-02-13 17:01:51 +0000 | [diff] [blame] | 1044 | return zErr; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1048 | ** This routine implements a busy callback that sleeps and tries |
| 1049 | ** again until a timeout value is reached. The timeout value is |
| 1050 | ** an integer number of milliseconds passed in as the first |
| 1051 | ** argument. |
| 1052 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1053 | static int sqliteDefaultBusyCallback( |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 1054 | void *ptr, /* Database connection */ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1055 | int count /* Number of times table has been busy */ |
| 1056 | ){ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1057 | #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP) |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 1058 | static const u8 delays[] = |
| 1059 | { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; |
| 1060 | static const u8 totals[] = |
| 1061 | { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 1062 | # define NDELAY ArraySize(delays) |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1063 | sqlite3 *db = (sqlite3 *)ptr; |
| 1064 | int timeout = db->busyTimeout; |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 1065 | int delay, prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1066 | |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 1067 | assert( count>=0 ); |
| 1068 | if( count < NDELAY ){ |
| 1069 | delay = delays[count]; |
| 1070 | prior = totals[count]; |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 1071 | }else{ |
| 1072 | delay = delays[NDELAY-1]; |
drh | 68cb619 | 2005-05-06 22:05:56 +0000 | [diff] [blame] | 1073 | prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1074 | } |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 1075 | if( prior + delay > timeout ){ |
| 1076 | delay = timeout - prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1077 | if( delay<=0 ) return 0; |
| 1078 | } |
danielk1977 | 2a6bdf6 | 2007-08-20 16:07:00 +0000 | [diff] [blame] | 1079 | sqlite3OsSleep(db->pVfs, delay*1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1080 | return 1; |
| 1081 | #else |
drh | 482ea18 | 2007-08-20 11:12:40 +0000 | [diff] [blame] | 1082 | sqlite3 *db = (sqlite3 *)ptr; |
drh | 3f73708 | 2005-06-14 02:24:31 +0000 | [diff] [blame] | 1083 | int timeout = ((sqlite3 *)ptr)->busyTimeout; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1084 | if( (count+1)*1000 > timeout ){ |
| 1085 | return 0; |
| 1086 | } |
drh | 482ea18 | 2007-08-20 11:12:40 +0000 | [diff] [blame] | 1087 | sqlite3OsSleep(db->pVfs, 1000000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1088 | return 1; |
| 1089 | #endif |
| 1090 | } |
| 1091 | |
| 1092 | /* |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1093 | ** Invoke the given busy handler. |
| 1094 | ** |
| 1095 | ** This routine is called when an operation failed with a lock. |
| 1096 | ** If this routine returns non-zero, the lock is retried. If it |
| 1097 | ** returns 0, the operation aborts with an SQLITE_BUSY error. |
| 1098 | */ |
| 1099 | int sqlite3InvokeBusyHandler(BusyHandler *p){ |
| 1100 | int rc; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1101 | if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1102 | rc = p->xFunc(p->pArg, p->nBusy); |
| 1103 | if( rc==0 ){ |
| 1104 | p->nBusy = -1; |
| 1105 | }else{ |
| 1106 | p->nBusy++; |
| 1107 | } |
| 1108 | return rc; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1112 | ** This routine sets the busy callback for an Sqlite database to the |
| 1113 | ** given callback function with the given argument. |
| 1114 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1115 | int sqlite3_busy_handler( |
| 1116 | sqlite3 *db, |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 1117 | int (*xBusy)(void*,int), |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1118 | void *pArg |
| 1119 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1120 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 1121 | db->busyHandler.xFunc = xBusy; |
| 1122 | db->busyHandler.pArg = pArg; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1123 | db->busyHandler.nBusy = 0; |
drh | c0c7b5e | 2012-09-07 18:49:57 +0000 | [diff] [blame] | 1124 | db->busyTimeout = 0; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1125 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1126 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1129 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1130 | /* |
| 1131 | ** This routine sets the progress callback for an Sqlite database to the |
| 1132 | ** given callback function with the given argument. The progress callback will |
| 1133 | ** be invoked every nOps opcodes. |
| 1134 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1135 | void sqlite3_progress_handler( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1136 | sqlite3 *db, |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1137 | int nOps, |
| 1138 | int (*xProgress)(void*), |
| 1139 | void *pArg |
| 1140 | ){ |
drh | bd0b1b5 | 2008-07-07 19:52:09 +0000 | [diff] [blame] | 1141 | sqlite3_mutex_enter(db->mutex); |
| 1142 | if( nOps>0 ){ |
| 1143 | db->xProgress = xProgress; |
| 1144 | db->nProgressOps = nOps; |
| 1145 | db->pProgressArg = pArg; |
| 1146 | }else{ |
| 1147 | db->xProgress = 0; |
| 1148 | db->nProgressOps = 0; |
| 1149 | db->pProgressArg = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1150 | } |
drh | bd0b1b5 | 2008-07-07 19:52:09 +0000 | [diff] [blame] | 1151 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1152 | } |
| 1153 | #endif |
| 1154 | |
| 1155 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1156 | /* |
| 1157 | ** This routine installs a default busy handler that waits for the |
| 1158 | ** specified number of milliseconds before returning 0. |
| 1159 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1160 | int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1161 | if( ms>0 ){ |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 1162 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
drh | c0c7b5e | 2012-09-07 18:49:57 +0000 | [diff] [blame] | 1163 | db->busyTimeout = ms; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1164 | }else{ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1165 | sqlite3_busy_handler(db, 0, 0); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1166 | } |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1167 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1168 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1169 | |
| 1170 | /* |
| 1171 | ** Cause any pending operation to stop at its earliest opportunity. |
| 1172 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1173 | void sqlite3_interrupt(sqlite3 *db){ |
drh | bd0b1b5 | 2008-07-07 19:52:09 +0000 | [diff] [blame] | 1174 | db->u1.isInterrupted = 1; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1175 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1176 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1177 | |
| 1178 | /* |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1179 | ** This function is exactly the same as sqlite3_create_function(), except |
| 1180 | ** that it is designed to be called by internal code. The difference is |
| 1181 | ** that if a malloc() fails in sqlite3_create_function(), an error code |
| 1182 | ** is returned and the mallocFailed flag cleared. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1183 | */ |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1184 | int sqlite3CreateFunc( |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1185 | sqlite3 *db, |
| 1186 | const char *zFunctionName, |
| 1187 | int nArg, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1188 | int enc, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1189 | void *pUserData, |
| 1190 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 1191 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1192 | void (*xFinal)(sqlite3_context*), |
| 1193 | FuncDestructor *pDestructor |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1194 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1195 | FuncDef *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1196 | int nName; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1197 | |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1198 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1199 | if( zFunctionName==0 || |
danielk1977 | 398eae7 | 2004-05-26 06:58:43 +0000 | [diff] [blame] | 1200 | (xFunc && (xFinal || xStep)) || |
| 1201 | (!xFunc && (xFinal && !xStep)) || |
| 1202 | (!xFunc && (!xFinal && xStep)) || |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 1203 | (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || |
drh | dee0e40 | 2009-05-03 20:23:53 +0000 | [diff] [blame] | 1204 | (255<(nName = sqlite3Strlen30( zFunctionName))) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1205 | return SQLITE_MISUSE_BKPT; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1206 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1207 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1208 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1209 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 1210 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 1211 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 1212 | ** |
| 1213 | ** If SQLITE_ANY is specified, add three versions of the function |
| 1214 | ** to the hash table. |
| 1215 | */ |
| 1216 | if( enc==SQLITE_UTF16 ){ |
| 1217 | enc = SQLITE_UTF16NATIVE; |
| 1218 | }else if( enc==SQLITE_ANY ){ |
| 1219 | int rc; |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1220 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1221 | pUserData, xFunc, xStep, xFinal, pDestructor); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1222 | if( rc==SQLITE_OK ){ |
| 1223 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1224 | pUserData, xFunc, xStep, xFinal, pDestructor); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1225 | } |
| 1226 | if( rc!=SQLITE_OK ){ |
| 1227 | return rc; |
| 1228 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1229 | enc = SQLITE_UTF16BE; |
| 1230 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1231 | #else |
| 1232 | enc = SQLITE_UTF8; |
| 1233 | #endif |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1234 | |
| 1235 | /* Check if an existing function is being overridden or deleted. If so, |
| 1236 | ** and there are active VMs, then return SQLITE_BUSY. If a function |
| 1237 | ** is being overridden/deleted but there are no active VMs, allow the |
| 1238 | ** operation to continue but invalidate all precompiled statements. |
| 1239 | */ |
drh | 3abbd39 | 2008-12-10 23:04:13 +0000 | [diff] [blame] | 1240 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1241 | if( p && p->iPrefEnc==enc && p->nArg==nArg ){ |
| 1242 | if( db->activeVdbeCnt ){ |
| 1243 | sqlite3Error(db, SQLITE_BUSY, |
drh | b309bec | 2009-02-04 17:40:57 +0000 | [diff] [blame] | 1244 | "unable to delete/modify user-function due to active statements"); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1245 | assert( !db->mallocFailed ); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1246 | return SQLITE_BUSY; |
| 1247 | }else{ |
| 1248 | sqlite3ExpirePreparedStatements(db); |
| 1249 | } |
| 1250 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1251 | |
drh | 3abbd39 | 2008-12-10 23:04:13 +0000 | [diff] [blame] | 1252 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1253 | assert(p || db->mallocFailed); |
| 1254 | if( !p ){ |
| 1255 | return SQLITE_NOMEM; |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1256 | } |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* If an older version of the function with a configured destructor is |
| 1259 | ** being replaced invoke the destructor function here. */ |
| 1260 | functionDestroy(db, p); |
| 1261 | |
| 1262 | if( pDestructor ){ |
| 1263 | pDestructor->nRef++; |
| 1264 | } |
| 1265 | p->pDestructor = pDestructor; |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1266 | p->flags = 0; |
| 1267 | p->xFunc = xFunc; |
| 1268 | p->xStep = xStep; |
| 1269 | p->xFinalize = xFinal; |
| 1270 | p->pUserData = pUserData; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1271 | p->nArg = (u16)nArg; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1272 | return SQLITE_OK; |
| 1273 | } |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1274 | |
| 1275 | /* |
| 1276 | ** Create new user functions. |
| 1277 | */ |
| 1278 | int sqlite3_create_function( |
| 1279 | sqlite3 *db, |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1280 | const char *zFunc, |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1281 | int nArg, |
| 1282 | int enc, |
| 1283 | void *p, |
| 1284 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 1285 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 1286 | void (*xFinal)(sqlite3_context*) |
| 1287 | ){ |
drh | 7b19fac | 2010-08-27 18:44:54 +0000 | [diff] [blame] | 1288 | return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep, |
| 1289 | xFinal, 0); |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | int sqlite3_create_function_v2( |
| 1293 | sqlite3 *db, |
| 1294 | const char *zFunc, |
| 1295 | int nArg, |
| 1296 | int enc, |
| 1297 | void *p, |
| 1298 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 1299 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 1300 | void (*xFinal)(sqlite3_context*), |
| 1301 | void (*xDestroy)(void *) |
| 1302 | ){ |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 +0000 | [diff] [blame] | 1303 | int rc = SQLITE_ERROR; |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1304 | FuncDestructor *pArg = 0; |
| 1305 | sqlite3_mutex_enter(db->mutex); |
| 1306 | if( xDestroy ){ |
| 1307 | pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
dan | 9508daa | 2010-08-28 18:58:00 +0000 | [diff] [blame] | 1308 | if( !pArg ){ |
| 1309 | xDestroy(p); |
| 1310 | goto out; |
| 1311 | } |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1312 | pArg->xDestroy = xDestroy; |
| 1313 | pArg->pUserData = p; |
| 1314 | } |
| 1315 | rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg); |
| 1316 | if( pArg && pArg->nRef==0 ){ |
| 1317 | assert( rc!=SQLITE_OK ); |
dan | 9508daa | 2010-08-28 18:58:00 +0000 | [diff] [blame] | 1318 | xDestroy(p); |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1319 | sqlite3DbFree(db, pArg); |
| 1320 | } |
| 1321 | |
| 1322 | out: |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1323 | rc = sqlite3ApiExit(db, rc); |
| 1324 | sqlite3_mutex_leave(db->mutex); |
| 1325 | return rc; |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1328 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1329 | int sqlite3_create_function16( |
| 1330 | sqlite3 *db, |
| 1331 | const void *zFunctionName, |
| 1332 | int nArg, |
| 1333 | int eTextRep, |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1334 | void *p, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1335 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 1336 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 1337 | void (*xFinal)(sqlite3_context*) |
| 1338 | ){ |
| 1339 | int rc; |
drh | af9a7c2 | 2005-12-15 03:04:10 +0000 | [diff] [blame] | 1340 | char *zFunc8; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1341 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1342 | assert( !db->mallocFailed ); |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 1343 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
dan | d2199f0 | 2010-08-27 17:48:52 +0000 | [diff] [blame] | 1344 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1345 | sqlite3DbFree(db, zFunc8); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1346 | rc = sqlite3ApiExit(db, rc); |
| 1347 | sqlite3_mutex_leave(db->mutex); |
| 1348 | return rc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1349 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1350 | #endif |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1351 | |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 1352 | |
| 1353 | /* |
| 1354 | ** Declare that a function has been overloaded by a virtual table. |
| 1355 | ** |
| 1356 | ** If the function already exists as a regular global function, then |
| 1357 | ** this routine is a no-op. If the function does not exist, then create |
| 1358 | ** a new one that always throws a run-time error. |
| 1359 | ** |
| 1360 | ** When virtual tables intend to provide an overloaded function, they |
| 1361 | ** should call this routine to make sure the global function exists. |
| 1362 | ** A global function must exist in order for name resolution to work |
| 1363 | ** properly. |
| 1364 | */ |
| 1365 | int sqlite3_overload_function( |
| 1366 | sqlite3 *db, |
| 1367 | const char *zName, |
| 1368 | int nArg |
| 1369 | ){ |
drh | dee0e40 | 2009-05-03 20:23:53 +0000 | [diff] [blame] | 1370 | int nName = sqlite3Strlen30(zName); |
drh | cb5e4db | 2011-10-12 23:49:49 +0000 | [diff] [blame] | 1371 | int rc = SQLITE_OK; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1372 | sqlite3_mutex_enter(db->mutex); |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 1373 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
drh | cb5e4db | 2011-10-12 23:49:49 +0000 | [diff] [blame] | 1374 | rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 1375 | 0, sqlite3InvalidFunction, 0, 0, 0); |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 1376 | } |
drh | cb5e4db | 2011-10-12 23:49:49 +0000 | [diff] [blame] | 1377 | rc = sqlite3ApiExit(db, rc); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1378 | sqlite3_mutex_leave(db->mutex); |
| 1379 | return rc; |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1382 | #ifndef SQLITE_OMIT_TRACE |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1383 | /* |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1384 | ** Register a trace function. The pArg from the previously registered trace |
| 1385 | ** is returned. |
| 1386 | ** |
| 1387 | ** A NULL trace function means that no tracing is executes. A non-NULL |
| 1388 | ** trace is a pointer to a function that is invoked at the start of each |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1389 | ** SQL statement. |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1390 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1391 | void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1392 | void *pOld; |
| 1393 | sqlite3_mutex_enter(db->mutex); |
| 1394 | pOld = db->pTraceArg; |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1395 | db->xTrace = xTrace; |
| 1396 | db->pTraceArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1397 | sqlite3_mutex_leave(db->mutex); |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1398 | return pOld; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 1399 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1400 | /* |
| 1401 | ** Register a profile function. The pArg from the previously registered |
| 1402 | ** profile function is returned. |
| 1403 | ** |
| 1404 | ** A NULL profile function means that no profiling is executes. A non-NULL |
| 1405 | ** profile is a pointer to a function that is invoked at the conclusion of |
| 1406 | ** each SQL statement that is run. |
| 1407 | */ |
| 1408 | void *sqlite3_profile( |
| 1409 | sqlite3 *db, |
| 1410 | void (*xProfile)(void*,const char*,sqlite_uint64), |
| 1411 | void *pArg |
| 1412 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1413 | void *pOld; |
| 1414 | sqlite3_mutex_enter(db->mutex); |
| 1415 | pOld = db->pProfileArg; |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1416 | db->xProfile = xProfile; |
| 1417 | db->pProfileArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1418 | sqlite3_mutex_leave(db->mutex); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1419 | return pOld; |
| 1420 | } |
| 1421 | #endif /* SQLITE_OMIT_TRACE */ |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1422 | |
drh | cd565fd | 2012-03-01 21:30:44 +0000 | [diff] [blame] | 1423 | /* |
| 1424 | ** Register a function to be invoked when a transaction commits. |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1425 | ** If the invoked function returns non-zero, then the commit becomes a |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1426 | ** rollback. |
| 1427 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1428 | void *sqlite3_commit_hook( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1429 | sqlite3 *db, /* Attach the hook to this database */ |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1430 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 1431 | void *pArg /* Argument to the function */ |
| 1432 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1433 | void *pOld; |
| 1434 | sqlite3_mutex_enter(db->mutex); |
| 1435 | pOld = db->pCommitArg; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1436 | db->xCommitCallback = xCallback; |
| 1437 | db->pCommitArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1438 | sqlite3_mutex_leave(db->mutex); |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1439 | return pOld; |
| 1440 | } |
| 1441 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1442 | /* |
| 1443 | ** Register a callback to be invoked each time a row is updated, |
| 1444 | ** inserted or deleted using this database connection. |
| 1445 | */ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1446 | void *sqlite3_update_hook( |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1447 | sqlite3 *db, /* Attach the hook to this database */ |
| 1448 | void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 1449 | void *pArg /* Argument to the function */ |
| 1450 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1451 | void *pRet; |
| 1452 | sqlite3_mutex_enter(db->mutex); |
| 1453 | pRet = db->pUpdateArg; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1454 | db->xUpdateCallback = xCallback; |
| 1455 | db->pUpdateArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1456 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1457 | return pRet; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1460 | /* |
| 1461 | ** Register a callback to be invoked each time a transaction is rolled |
| 1462 | ** back by this database connection. |
| 1463 | */ |
| 1464 | void *sqlite3_rollback_hook( |
| 1465 | sqlite3 *db, /* Attach the hook to this database */ |
| 1466 | void (*xCallback)(void*), /* Callback function */ |
| 1467 | void *pArg /* Argument to the function */ |
| 1468 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1469 | void *pRet; |
| 1470 | sqlite3_mutex_enter(db->mutex); |
| 1471 | pRet = db->pRollbackArg; |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1472 | db->xRollbackCallback = xCallback; |
| 1473 | db->pRollbackArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1474 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1475 | return pRet; |
| 1476 | } |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1477 | |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1478 | #ifndef SQLITE_OMIT_WAL |
| 1479 | /* |
| 1480 | ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 1481 | ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file |
| 1482 | ** is greater than sqlite3.pWalArg cast to an integer (the value configured by |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1483 | ** wal_autocheckpoint()). |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1484 | */ |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1485 | int sqlite3WalDefaultHook( |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 1486 | void *pClientData, /* Argument */ |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1487 | sqlite3 *db, /* Connection */ |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 1488 | const char *zDb, /* Database */ |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1489 | int nFrame /* Size of WAL */ |
| 1490 | ){ |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 1491 | if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){ |
drh | f533035 | 2010-05-24 20:27:44 +0000 | [diff] [blame] | 1492 | sqlite3BeginBenignMalloc(); |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 1493 | sqlite3_wal_checkpoint(db, zDb); |
drh | f533035 | 2010-05-24 20:27:44 +0000 | [diff] [blame] | 1494 | sqlite3EndBenignMalloc(); |
drh | 5def084 | 2010-05-05 20:00:25 +0000 | [diff] [blame] | 1495 | } |
| 1496 | return SQLITE_OK; |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1497 | } |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1498 | #endif /* SQLITE_OMIT_WAL */ |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1499 | |
| 1500 | /* |
| 1501 | ** Configure an sqlite3_wal_hook() callback to automatically checkpoint |
| 1502 | ** a database after committing a transaction if there are nFrame or |
| 1503 | ** more frames in the log file. Passing zero or a negative value as the |
| 1504 | ** nFrame parameter disables automatic checkpoints entirely. |
| 1505 | ** |
| 1506 | ** The callback registered by this function replaces any existing callback |
| 1507 | ** registered using sqlite3_wal_hook(). Likewise, registering a callback |
| 1508 | ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism |
| 1509 | ** configured by this function. |
| 1510 | */ |
| 1511 | int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 +0000 | [diff] [blame] | 1512 | #ifdef SQLITE_OMIT_WAL |
drh | 9dd55f5 | 2010-09-03 03:32:46 +0000 | [diff] [blame] | 1513 | UNUSED_PARAMETER(db); |
| 1514 | UNUSED_PARAMETER(nFrame); |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 +0000 | [diff] [blame] | 1515 | #else |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1516 | if( nFrame>0 ){ |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1517 | sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1518 | }else{ |
| 1519 | sqlite3_wal_hook(db, 0, 0); |
| 1520 | } |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1521 | #endif |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1522 | return SQLITE_OK; |
| 1523 | } |
| 1524 | |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1525 | /* |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1526 | ** Register a callback to be invoked each time a transaction is written |
| 1527 | ** into the write-ahead-log by this database connection. |
| 1528 | */ |
drh | 833bf96 | 2010-04-28 14:42:19 +0000 | [diff] [blame] | 1529 | void *sqlite3_wal_hook( |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1530 | sqlite3 *db, /* Attach the hook to this db handle */ |
| 1531 | int(*xCallback)(void *, sqlite3*, const char*, int), |
| 1532 | void *pArg /* First argument passed to xCallback() */ |
| 1533 | ){ |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1534 | #ifndef SQLITE_OMIT_WAL |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1535 | void *pRet; |
| 1536 | sqlite3_mutex_enter(db->mutex); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 1537 | pRet = db->pWalArg; |
| 1538 | db->xWalCallback = xCallback; |
| 1539 | db->pWalArg = pArg; |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1540 | sqlite3_mutex_leave(db->mutex); |
| 1541 | return pRet; |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1542 | #else |
| 1543 | return 0; |
| 1544 | #endif |
dan | 8d22a17 | 2010-04-19 18:03:51 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /* |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1548 | ** Checkpoint database zDb. |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1549 | */ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1550 | int sqlite3_wal_checkpoint_v2( |
| 1551 | sqlite3 *db, /* Database handle */ |
| 1552 | const char *zDb, /* Name of attached database (or NULL) */ |
| 1553 | int eMode, /* SQLITE_CHECKPOINT_* value */ |
| 1554 | int *pnLog, /* OUT: Size of WAL log in frames */ |
| 1555 | int *pnCkpt /* OUT: Total number of frames checkpointed */ |
| 1556 | ){ |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1557 | #ifdef SQLITE_OMIT_WAL |
| 1558 | return SQLITE_OK; |
| 1559 | #else |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1560 | int rc; /* Return code */ |
dan | 6fcff07 | 2010-05-03 14:05:43 +0000 | [diff] [blame] | 1561 | int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1562 | |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 1563 | /* Initialize the output variables to -1 in case an error occurs. */ |
| 1564 | if( pnLog ) *pnLog = -1; |
| 1565 | if( pnCkpt ) *pnCkpt = -1; |
| 1566 | |
drh | 383d79e | 2011-02-09 03:03:16 +0000 | [diff] [blame] | 1567 | assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE ); |
| 1568 | assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART ); |
| 1569 | assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART ); |
| 1570 | if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1571 | return SQLITE_MISUSE; |
| 1572 | } |
| 1573 | |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1574 | sqlite3_mutex_enter(db->mutex); |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1575 | if( zDb && zDb[0] ){ |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1576 | iDb = sqlite3FindDbName(db, zDb); |
| 1577 | } |
| 1578 | if( iDb<0 ){ |
| 1579 | rc = SQLITE_ERROR; |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 1580 | sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb); |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1581 | }else{ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1582 | rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 1583 | sqlite3Error(db, rc, 0); |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1584 | } |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1585 | rc = sqlite3ApiExit(db, rc); |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1586 | sqlite3_mutex_leave(db->mutex); |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1587 | return rc; |
| 1588 | #endif |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1591 | |
| 1592 | /* |
| 1593 | ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points |
| 1594 | ** to contains a zero-length string, all attached databases are |
| 1595 | ** checkpointed. |
| 1596 | */ |
| 1597 | int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ |
| 1598 | return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0); |
| 1599 | } |
| 1600 | |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1601 | #ifndef SQLITE_OMIT_WAL |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1602 | /* |
| 1603 | ** Run a checkpoint on database iDb. This is a no-op if database iDb is |
| 1604 | ** not currently open in WAL mode. |
| 1605 | ** |
dan | 6fcff07 | 2010-05-03 14:05:43 +0000 | [diff] [blame] | 1606 | ** If a transaction is open on the database being checkpointed, this |
| 1607 | ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If |
| 1608 | ** an error occurs while running the checkpoint, an SQLite error code is |
| 1609 | ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK. |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1610 | ** |
| 1611 | ** The mutex on database handle db should be held by the caller. The mutex |
| 1612 | ** associated with the specific b-tree being checkpointed is taken by |
| 1613 | ** this function while the checkpoint is running. |
dan | 6fcff07 | 2010-05-03 14:05:43 +0000 | [diff] [blame] | 1614 | ** |
| 1615 | ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are |
| 1616 | ** checkpointed. If an error is encountered it is returned immediately - |
| 1617 | ** no attempt is made to checkpoint any remaining databases. |
dan | a58f26f | 2010-11-16 18:56:51 +0000 | [diff] [blame] | 1618 | ** |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1619 | ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1620 | */ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1621 | int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){ |
dan | 6fcff07 | 2010-05-03 14:05:43 +0000 | [diff] [blame] | 1622 | int rc = SQLITE_OK; /* Return code */ |
| 1623 | int i; /* Used to iterate through attached dbs */ |
dan | f2b8dd5 | 2010-11-18 19:28:01 +0000 | [diff] [blame] | 1624 | int bBusy = 0; /* True if SQLITE_BUSY has been encountered */ |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1625 | |
| 1626 | assert( sqlite3_mutex_held(db->mutex) ); |
dan | 9c5e368 | 2011-02-07 15:12:12 +0000 | [diff] [blame] | 1627 | assert( !pnLog || *pnLog==-1 ); |
| 1628 | assert( !pnCkpt || *pnCkpt==-1 ); |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1629 | |
dan | 6fcff07 | 2010-05-03 14:05:43 +0000 | [diff] [blame] | 1630 | for(i=0; i<db->nDb && rc==SQLITE_OK; i++){ |
| 1631 | if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 1632 | rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt); |
dan | f2b8dd5 | 2010-11-18 19:28:01 +0000 | [diff] [blame] | 1633 | pnLog = 0; |
| 1634 | pnCkpt = 0; |
| 1635 | if( rc==SQLITE_BUSY ){ |
| 1636 | bBusy = 1; |
| 1637 | rc = SQLITE_OK; |
| 1638 | } |
dan | 6fcff07 | 2010-05-03 14:05:43 +0000 | [diff] [blame] | 1639 | } |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
dan | f2b8dd5 | 2010-11-18 19:28:01 +0000 | [diff] [blame] | 1642 | return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc; |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1643 | } |
drh | b033d8b | 2010-05-03 13:37:30 +0000 | [diff] [blame] | 1644 | #endif /* SQLITE_OMIT_WAL */ |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 1645 | |
| 1646 | /* |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1647 | ** This function returns true if main-memory should be used instead of |
| 1648 | ** a temporary file for transient pager files and statement journals. |
| 1649 | ** The value returned depends on the value of db->temp_store (runtime |
| 1650 | ** parameter) and the compile time value of SQLITE_TEMP_STORE. The |
| 1651 | ** following table describes the relationship between these two values |
| 1652 | ** and this functions return value. |
| 1653 | ** |
| 1654 | ** SQLITE_TEMP_STORE db->temp_store Location of temporary database |
| 1655 | ** ----------------- -------------- ------------------------------ |
| 1656 | ** 0 any file (return 0) |
| 1657 | ** 1 1 file (return 0) |
| 1658 | ** 1 2 memory (return 1) |
| 1659 | ** 1 0 file (return 0) |
| 1660 | ** 2 1 file (return 0) |
| 1661 | ** 2 2 memory (return 1) |
| 1662 | ** 2 0 memory (return 1) |
| 1663 | ** 3 any memory (return 1) |
| 1664 | */ |
drh | 1c51414 | 2009-04-30 12:25:10 +0000 | [diff] [blame] | 1665 | int sqlite3TempInMemory(const sqlite3 *db){ |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1666 | #if SQLITE_TEMP_STORE==1 |
| 1667 | return ( db->temp_store==2 ); |
| 1668 | #endif |
| 1669 | #if SQLITE_TEMP_STORE==2 |
| 1670 | return ( db->temp_store!=1 ); |
| 1671 | #endif |
| 1672 | #if SQLITE_TEMP_STORE==3 |
| 1673 | return 1; |
shane | cf69739 | 2009-06-01 16:53:09 +0000 | [diff] [blame] | 1674 | #endif |
| 1675 | #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1676 | return 0; |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 1677 | #endif |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1681 | ** Return UTF-8 encoded English language explanation of the most recent |
| 1682 | ** error. |
| 1683 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1684 | const char *sqlite3_errmsg(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1685 | const char *z; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1686 | if( !db ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 1687 | return sqlite3ErrStr(SQLITE_NOMEM); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1688 | } |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1689 | if( !sqlite3SafetyCheckSickOrOk(db) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1690 | return sqlite3ErrStr(SQLITE_MISUSE_BKPT); |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 1691 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1692 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 1693 | if( db->mallocFailed ){ |
| 1694 | z = sqlite3ErrStr(SQLITE_NOMEM); |
| 1695 | }else{ |
| 1696 | z = (char*)sqlite3_value_text(db->pErr); |
| 1697 | assert( !db->mallocFailed ); |
| 1698 | if( z==0 ){ |
| 1699 | z = sqlite3ErrStr(db->errCode); |
| 1700 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1701 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1702 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1703 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1706 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1707 | /* |
| 1708 | ** Return UTF-16 encoded English language explanation of the most recent |
| 1709 | ** error. |
| 1710 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1711 | const void *sqlite3_errmsg16(sqlite3 *db){ |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1712 | static const u16 outOfMem[] = { |
| 1713 | 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0 |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1714 | }; |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1715 | static const u16 misuse[] = { |
| 1716 | 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', |
| 1717 | 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', |
| 1718 | 'c', 'a', 'l', 'l', 'e', 'd', ' ', |
| 1719 | 'o', 'u', 't', ' ', |
| 1720 | 'o', 'f', ' ', |
| 1721 | 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0 |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 1722 | }; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1723 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1724 | const void *z; |
danielk1977 | ae7fc49 | 2007-03-29 15:00:52 +0000 | [diff] [blame] | 1725 | if( !db ){ |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1726 | return (void *)outOfMem; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1727 | } |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1728 | if( !sqlite3SafetyCheckSickOrOk(db) ){ |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1729 | return (void *)misuse; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1730 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1731 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 1732 | if( db->mallocFailed ){ |
| 1733 | z = (void *)outOfMem; |
| 1734 | }else{ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1735 | z = sqlite3_value_text16(db->pErr); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 1736 | if( z==0 ){ |
| 1737 | sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), |
| 1738 | SQLITE_UTF8, SQLITE_STATIC); |
| 1739 | z = sqlite3_value_text16(db->pErr); |
| 1740 | } |
| 1741 | /* A malloc() may have failed within the call to sqlite3_value_text16() |
| 1742 | ** above. If this is the case, then the db->mallocFailed flag needs to |
| 1743 | ** be cleared before returning. Do this directly, instead of via |
| 1744 | ** sqlite3ApiExit(), to avoid setting the database handle error message. |
| 1745 | */ |
| 1746 | db->mallocFailed = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1747 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1748 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1749 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1750 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1751 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1752 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1753 | /* |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 1754 | ** Return the most recent error code generated by an SQLite routine. If NULL is |
| 1755 | ** passed to this function, we assume a malloc() failed during sqlite3_open(). |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1756 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1757 | int sqlite3_errcode(sqlite3 *db){ |
danielk1977 | 7c36d07 | 2008-01-31 15:31:01 +0000 | [diff] [blame] | 1758 | if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1759 | return SQLITE_MISUSE_BKPT; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1760 | } |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 1761 | if( !db || db->mallocFailed ){ |
| 1762 | return SQLITE_NOMEM; |
| 1763 | } |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1764 | return db->errCode & db->errMask; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1765 | } |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 1766 | int sqlite3_extended_errcode(sqlite3 *db){ |
| 1767 | if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1768 | return SQLITE_MISUSE_BKPT; |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 1769 | } |
| 1770 | if( !db || db->mallocFailed ){ |
| 1771 | return SQLITE_NOMEM; |
| 1772 | } |
| 1773 | return db->errCode; |
| 1774 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1775 | |
drh | 0e819f9 | 2006-02-01 13:50:41 +0000 | [diff] [blame] | 1776 | /* |
mistachkin | 5dac843 | 2012-09-11 02:00:25 +0000 | [diff] [blame] | 1777 | ** Return a string that describes the kind of error specified in the |
| 1778 | ** argument. For now, this simply calls the internal sqlite3ErrStr() |
| 1779 | ** function. |
| 1780 | */ |
| 1781 | const char *sqlite3_errstr(int rc){ |
| 1782 | return sqlite3ErrStr(rc); |
| 1783 | } |
| 1784 | |
| 1785 | /* |
drh | 0e819f9 | 2006-02-01 13:50:41 +0000 | [diff] [blame] | 1786 | ** Create a new collating function for database "db". The name is zName |
| 1787 | ** and the encoding is enc. |
| 1788 | */ |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1789 | static int createCollation( |
drh | f8d4e8b | 2009-08-20 02:49:30 +0000 | [diff] [blame] | 1790 | sqlite3* db, |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1791 | const char *zName, |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 1792 | u8 enc, |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1793 | void* pCtx, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1794 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 1795 | void(*xDel)(void*) |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1796 | ){ |
| 1797 | CollSeq *pColl; |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 1798 | int enc2; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1799 | int nName = sqlite3Strlen30(zName); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1800 | |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1801 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1802 | |
| 1803 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 1804 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 1805 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 1806 | */ |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1807 | enc2 = enc; |
danielk1977 | ebb3293 | 2009-04-28 15:35:38 +0000 | [diff] [blame] | 1808 | testcase( enc2==SQLITE_UTF16 ); |
| 1809 | testcase( enc2==SQLITE_UTF16_ALIGNED ); |
| 1810 | if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 1811 | enc2 = SQLITE_UTF16NATIVE; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1812 | } |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1813 | if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1814 | return SQLITE_MISUSE_BKPT; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | /* Check if this call is removing or replacing an existing collation |
| 1818 | ** sequence. If so, and there are active VMs, return busy. If there |
| 1819 | ** are no active VMs, invalidate any pre-compiled statements. |
| 1820 | */ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1821 | pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1822 | if( pColl && pColl->xCmp ){ |
| 1823 | if( db->activeVdbeCnt ){ |
| 1824 | sqlite3Error(db, SQLITE_BUSY, |
drh | b309bec | 2009-02-04 17:40:57 +0000 | [diff] [blame] | 1825 | "unable to delete/modify collation sequence due to active statements"); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1826 | return SQLITE_BUSY; |
| 1827 | } |
| 1828 | sqlite3ExpirePreparedStatements(db); |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1829 | |
| 1830 | /* If collation sequence pColl was created directly by a call to |
| 1831 | ** sqlite3_create_collation, and not generated by synthCollSeq(), |
| 1832 | ** then any copies made by synthCollSeq() need to be invalidated. |
| 1833 | ** Also, collation destructor - CollSeq.xDel() - function may need |
| 1834 | ** to be called. |
| 1835 | */ |
| 1836 | if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame] | 1837 | CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName); |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1838 | int j; |
| 1839 | for(j=0; j<3; j++){ |
| 1840 | CollSeq *p = &aColl[j]; |
| 1841 | if( p->enc==pColl->enc ){ |
| 1842 | if( p->xDel ){ |
| 1843 | p->xDel(p->pUser); |
| 1844 | } |
| 1845 | p->xCmp = 0; |
| 1846 | } |
| 1847 | } |
| 1848 | } |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1851 | pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); |
drh | 88404c2 | 2010-10-11 17:58:21 +0000 | [diff] [blame] | 1852 | if( pColl==0 ) return SQLITE_NOMEM; |
| 1853 | pColl->xCmp = xCompare; |
| 1854 | pColl->pUser = pCtx; |
| 1855 | pColl->xDel = xDel; |
| 1856 | pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1857 | sqlite3Error(db, SQLITE_OK, 0); |
| 1858 | return SQLITE_OK; |
| 1859 | } |
| 1860 | |
| 1861 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1862 | /* |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1863 | ** This array defines hard upper bounds on limit values. The |
| 1864 | ** initializer must be kept in sync with the SQLITE_LIMIT_* |
| 1865 | ** #defines in sqlite3.h. |
| 1866 | */ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 1867 | static const int aHardLimit[] = { |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1868 | SQLITE_MAX_LENGTH, |
| 1869 | SQLITE_MAX_SQL_LENGTH, |
| 1870 | SQLITE_MAX_COLUMN, |
| 1871 | SQLITE_MAX_EXPR_DEPTH, |
| 1872 | SQLITE_MAX_COMPOUND_SELECT, |
| 1873 | SQLITE_MAX_VDBE_OP, |
| 1874 | SQLITE_MAX_FUNCTION_ARG, |
| 1875 | SQLITE_MAX_ATTACHED, |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1876 | SQLITE_MAX_LIKE_PATTERN_LENGTH, |
| 1877 | SQLITE_MAX_VARIABLE_NUMBER, |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 1878 | SQLITE_MAX_TRIGGER_DEPTH, |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1879 | }; |
| 1880 | |
| 1881 | /* |
| 1882 | ** Make sure the hard limits are set to reasonable values |
| 1883 | */ |
| 1884 | #if SQLITE_MAX_LENGTH<100 |
| 1885 | # error SQLITE_MAX_LENGTH must be at least 100 |
| 1886 | #endif |
| 1887 | #if SQLITE_MAX_SQL_LENGTH<100 |
| 1888 | # error SQLITE_MAX_SQL_LENGTH must be at least 100 |
| 1889 | #endif |
| 1890 | #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH |
| 1891 | # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH |
| 1892 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1893 | #if SQLITE_MAX_COMPOUND_SELECT<2 |
| 1894 | # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 |
| 1895 | #endif |
| 1896 | #if SQLITE_MAX_VDBE_OP<40 |
| 1897 | # error SQLITE_MAX_VDBE_OP must be at least 40 |
| 1898 | #endif |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 1899 | #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000 |
| 1900 | # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000 |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1901 | #endif |
drh | 01c7dc8 | 2011-03-23 18:22:34 +0000 | [diff] [blame] | 1902 | #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62 |
| 1903 | # error SQLITE_MAX_ATTACHED must be between 0 and 62 |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1904 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1905 | #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 |
| 1906 | # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 |
| 1907 | #endif |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1908 | #if SQLITE_MAX_COLUMN>32767 |
| 1909 | # error SQLITE_MAX_COLUMN must not exceed 32767 |
| 1910 | #endif |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 1911 | #if SQLITE_MAX_TRIGGER_DEPTH<1 |
| 1912 | # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 |
| 1913 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1914 | |
| 1915 | |
| 1916 | /* |
| 1917 | ** Change the value of a limit. Report the old value. |
| 1918 | ** If an invalid limit index is supplied, report -1. |
| 1919 | ** Make no changes but still report the old value if the |
| 1920 | ** new limit is negative. |
| 1921 | ** |
| 1922 | ** A new lower limit does not shrink existing constructs. |
| 1923 | ** It merely prevents new constructs that exceed the limit |
| 1924 | ** from forming. |
| 1925 | */ |
| 1926 | int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 1927 | int oldLimit; |
drh | 4e93f5b | 2010-09-07 14:59:15 +0000 | [diff] [blame] | 1928 | |
| 1929 | |
| 1930 | /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 1931 | ** there is a hard upper bound set at compile-time by a C preprocessor |
| 1932 | ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 1933 | ** "_MAX_".) |
| 1934 | */ |
| 1935 | assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); |
| 1936 | assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); |
| 1937 | assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); |
| 1938 | assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); |
| 1939 | assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); |
| 1940 | assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); |
| 1941 | assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); |
| 1942 | assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); |
| 1943 | assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== |
| 1944 | SQLITE_MAX_LIKE_PATTERN_LENGTH ); |
| 1945 | assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); |
| 1946 | assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); |
| 1947 | assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) ); |
| 1948 | |
| 1949 | |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 1950 | if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1951 | return -1; |
| 1952 | } |
| 1953 | oldLimit = db->aLimit[limitId]; |
drh | 4e93f5b | 2010-09-07 14:59:15 +0000 | [diff] [blame] | 1954 | if( newLimit>=0 ){ /* IMP: R-52476-28732 */ |
drh | f47ce56 | 2008-03-20 18:00:49 +0000 | [diff] [blame] | 1955 | if( newLimit>aHardLimit[limitId] ){ |
drh | 4e93f5b | 2010-09-07 14:59:15 +0000 | [diff] [blame] | 1956 | newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1957 | } |
| 1958 | db->aLimit[limitId] = newLimit; |
| 1959 | } |
drh | 4e93f5b | 2010-09-07 14:59:15 +0000 | [diff] [blame] | 1960 | return oldLimit; /* IMP: R-53341-35419 */ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /* |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 1964 | ** This function is used to parse both URIs and non-URI filenames passed by the |
| 1965 | ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database |
| 1966 | ** URIs specified as part of ATTACH statements. |
| 1967 | ** |
| 1968 | ** The first argument to this function is the name of the VFS to use (or |
| 1969 | ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx" |
| 1970 | ** query parameter. The second argument contains the URI (or non-URI filename) |
| 1971 | ** itself. When this function is called the *pFlags variable should contain |
| 1972 | ** the default flags to open the database handle with. The value stored in |
| 1973 | ** *pFlags may be updated before returning if the URI filename contains |
| 1974 | ** "cache=xxx" or "mode=xxx" query parameters. |
| 1975 | ** |
| 1976 | ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to |
| 1977 | ** the VFS that should be used to open the database file. *pzFile is set to |
| 1978 | ** point to a buffer containing the name of the file to open. It is the |
| 1979 | ** responsibility of the caller to eventually call sqlite3_free() to release |
| 1980 | ** this buffer. |
| 1981 | ** |
| 1982 | ** If an error occurs, then an SQLite error code is returned and *pzErrMsg |
| 1983 | ** may be set to point to a buffer containing an English language error |
| 1984 | ** message. It is the responsibility of the caller to eventually release |
| 1985 | ** this buffer by calling sqlite3_free(). |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 1986 | */ |
| 1987 | int sqlite3ParseUri( |
| 1988 | const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */ |
| 1989 | const char *zUri, /* Nul-terminated URI to parse */ |
drh | 522c26f | 2011-05-07 14:40:29 +0000 | [diff] [blame] | 1990 | unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */ |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 1991 | sqlite3_vfs **ppVfs, /* OUT: VFS to use */ |
| 1992 | char **pzFile, /* OUT: Filename component of URI */ |
| 1993 | char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */ |
| 1994 | ){ |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 1995 | int rc = SQLITE_OK; |
drh | 522c26f | 2011-05-07 14:40:29 +0000 | [diff] [blame] | 1996 | unsigned int flags = *pFlags; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 1997 | const char *zVfs = zDefaultVfs; |
| 1998 | char *zFile; |
drh | de941c3 | 2011-05-09 19:20:17 +0000 | [diff] [blame] | 1999 | char c; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2000 | int nUri = sqlite3Strlen30(zUri); |
| 2001 | |
| 2002 | assert( *pzErrMsg==0 ); |
| 2003 | |
| 2004 | if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) |
| 2005 | && nUri>=5 && memcmp(zUri, "file:", 5)==0 |
| 2006 | ){ |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2007 | char *zOpt; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2008 | int eState; /* Parser state when parsing URI */ |
| 2009 | int iIn; /* Input character index */ |
| 2010 | int iOut = 0; /* Output character index */ |
| 2011 | int nByte = nUri+2; /* Bytes of space to allocate */ |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2012 | |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 2013 | /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen |
| 2014 | ** method that there may be extra parameters following the file-name. */ |
| 2015 | flags |= SQLITE_OPEN_URI; |
| 2016 | |
| 2017 | for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&'); |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2018 | zFile = sqlite3_malloc(nByte); |
| 2019 | if( !zFile ) return SQLITE_NOMEM; |
| 2020 | |
| 2021 | /* Discard the scheme and authority segments of the URI. */ |
| 2022 | if( zUri[5]=='/' && zUri[6]=='/' ){ |
| 2023 | iIn = 7; |
| 2024 | while( zUri[iIn] && zUri[iIn]!='/' ) iIn++; |
dan | 3b18a9a | 2011-05-03 11:53:20 +0000 | [diff] [blame] | 2025 | |
| 2026 | if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){ |
| 2027 | *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", |
| 2028 | iIn-7, &zUri[7]); |
| 2029 | rc = SQLITE_ERROR; |
| 2030 | goto parse_uri_out; |
| 2031 | } |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2032 | }else{ |
| 2033 | iIn = 5; |
| 2034 | } |
| 2035 | |
| 2036 | /* Copy the filename and any query parameters into the zFile buffer. |
| 2037 | ** Decode %HH escape codes along the way. |
| 2038 | ** |
| 2039 | ** Within this loop, variable eState may be set to 0, 1 or 2, depending |
| 2040 | ** on the parsing context. As follows: |
| 2041 | ** |
| 2042 | ** 0: Parsing file-name. |
| 2043 | ** 1: Parsing name section of a name=value query parameter. |
| 2044 | ** 2: Parsing value section of a name=value query parameter. |
| 2045 | */ |
| 2046 | eState = 0; |
drh | de941c3 | 2011-05-09 19:20:17 +0000 | [diff] [blame] | 2047 | while( (c = zUri[iIn])!=0 && c!='#' ){ |
| 2048 | iIn++; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2049 | if( c=='%' |
| 2050 | && sqlite3Isxdigit(zUri[iIn]) |
| 2051 | && sqlite3Isxdigit(zUri[iIn+1]) |
| 2052 | ){ |
dan | 6d49e25 | 2011-05-03 15:09:05 +0000 | [diff] [blame] | 2053 | int octet = (sqlite3HexToInt(zUri[iIn++]) << 4); |
| 2054 | octet += sqlite3HexToInt(zUri[iIn++]); |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2055 | |
dan | 6d49e25 | 2011-05-03 15:09:05 +0000 | [diff] [blame] | 2056 | assert( octet>=0 && octet<256 ); |
| 2057 | if( octet==0 ){ |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2058 | /* This branch is taken when "%00" appears within the URI. In this |
| 2059 | ** case we ignore all text in the remainder of the path, name or |
| 2060 | ** value currently being parsed. So ignore the current character |
| 2061 | ** and skip to the next "?", "=" or "&", as appropriate. */ |
drh | de941c3 | 2011-05-09 19:20:17 +0000 | [diff] [blame] | 2062 | while( (c = zUri[iIn])!=0 && c!='#' |
| 2063 | && (eState!=0 || c!='?') |
| 2064 | && (eState!=1 || (c!='=' && c!='&')) |
| 2065 | && (eState!=2 || c!='&') |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2066 | ){ |
| 2067 | iIn++; |
| 2068 | } |
| 2069 | continue; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2070 | } |
dan | 6d49e25 | 2011-05-03 15:09:05 +0000 | [diff] [blame] | 2071 | c = octet; |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2072 | }else if( eState==1 && (c=='&' || c=='=') ){ |
| 2073 | if( zFile[iOut-1]==0 ){ |
| 2074 | /* An empty option name. Ignore this option altogether. */ |
| 2075 | while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++; |
| 2076 | continue; |
| 2077 | } |
| 2078 | if( c=='&' ){ |
| 2079 | zFile[iOut++] = '\0'; |
| 2080 | }else{ |
| 2081 | eState = 2; |
| 2082 | } |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2083 | c = 0; |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2084 | }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){ |
| 2085 | c = 0; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2086 | eState = 1; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2087 | } |
| 2088 | zFile[iOut++] = c; |
| 2089 | } |
| 2090 | if( eState==1 ) zFile[iOut++] = '\0'; |
| 2091 | zFile[iOut++] = '\0'; |
| 2092 | zFile[iOut++] = '\0'; |
| 2093 | |
| 2094 | /* Check if there were any options specified that should be interpreted |
| 2095 | ** here. Options that are interpreted here include "vfs" and those that |
| 2096 | ** correspond to flags that may be passed to the sqlite3_open_v2() |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2097 | ** method. */ |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2098 | zOpt = &zFile[sqlite3Strlen30(zFile)+1]; |
| 2099 | while( zOpt[0] ){ |
| 2100 | int nOpt = sqlite3Strlen30(zOpt); |
| 2101 | char *zVal = &zOpt[nOpt+1]; |
| 2102 | int nVal = sqlite3Strlen30(zVal); |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2103 | |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 2104 | if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){ |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2105 | zVfs = zVal; |
| 2106 | }else{ |
| 2107 | struct OpenMode { |
| 2108 | const char *z; |
| 2109 | int mode; |
| 2110 | } *aMode = 0; |
drh | 45caede | 2011-06-03 14:19:10 +0000 | [diff] [blame] | 2111 | char *zModeType = 0; |
| 2112 | int mask = 0; |
| 2113 | int limit = 0; |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2114 | |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 2115 | if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){ |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2116 | static struct OpenMode aCacheMode[] = { |
| 2117 | { "shared", SQLITE_OPEN_SHAREDCACHE }, |
| 2118 | { "private", SQLITE_OPEN_PRIVATECACHE }, |
| 2119 | { 0, 0 } |
| 2120 | }; |
| 2121 | |
| 2122 | mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE; |
| 2123 | aMode = aCacheMode; |
| 2124 | limit = mask; |
| 2125 | zModeType = "cache"; |
| 2126 | } |
dan | 286ab7c | 2011-05-06 18:34:54 +0000 | [diff] [blame] | 2127 | if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){ |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2128 | static struct OpenMode aOpenMode[] = { |
| 2129 | { "ro", SQLITE_OPEN_READONLY }, |
| 2130 | { "rw", SQLITE_OPEN_READWRITE }, |
| 2131 | { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE }, |
dan | 0b8dcfa | 2012-06-07 17:16:04 +0000 | [diff] [blame] | 2132 | { "memory", SQLITE_OPEN_MEMORY }, |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2133 | { 0, 0 } |
| 2134 | }; |
| 2135 | |
drh | 9c67b2a | 2012-05-28 13:58:00 +0000 | [diff] [blame] | 2136 | mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE |
| 2137 | | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY; |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2138 | aMode = aOpenMode; |
| 2139 | limit = mask & flags; |
| 2140 | zModeType = "access"; |
| 2141 | } |
| 2142 | |
| 2143 | if( aMode ){ |
| 2144 | int i; |
| 2145 | int mode = 0; |
| 2146 | for(i=0; aMode[i].z; i++){ |
| 2147 | const char *z = aMode[i].z; |
drh | 522c26f | 2011-05-07 14:40:29 +0000 | [diff] [blame] | 2148 | if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){ |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2149 | mode = aMode[i].mode; |
| 2150 | break; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2151 | } |
| 2152 | } |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2153 | if( mode==0 ){ |
| 2154 | *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal); |
| 2155 | rc = SQLITE_ERROR; |
| 2156 | goto parse_uri_out; |
| 2157 | } |
drh | 9c67b2a | 2012-05-28 13:58:00 +0000 | [diff] [blame] | 2158 | if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){ |
drh | de941c3 | 2011-05-09 19:20:17 +0000 | [diff] [blame] | 2159 | *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s", |
| 2160 | zModeType, zVal); |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2161 | rc = SQLITE_PERM; |
| 2162 | goto parse_uri_out; |
| 2163 | } |
| 2164 | flags = (flags & ~mask) | mode; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2165 | } |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2166 | } |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2167 | |
| 2168 | zOpt = &zVal[nVal+1]; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | }else{ |
| 2172 | zFile = sqlite3_malloc(nUri+2); |
| 2173 | if( !zFile ) return SQLITE_NOMEM; |
| 2174 | memcpy(zFile, zUri, nUri); |
| 2175 | zFile[nUri] = '\0'; |
| 2176 | zFile[nUri+1] = '\0'; |
drh | 4ab9d25 | 2012-05-26 20:08:49 +0000 | [diff] [blame] | 2177 | flags &= ~SQLITE_OPEN_URI; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | *ppVfs = sqlite3_vfs_find(zVfs); |
| 2181 | if( *ppVfs==0 ){ |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2182 | *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs); |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2183 | rc = SQLITE_ERROR; |
| 2184 | } |
| 2185 | parse_uri_out: |
| 2186 | if( rc!=SQLITE_OK ){ |
dan | 5de1537 | 2011-04-23 10:12:30 +0000 | [diff] [blame] | 2187 | sqlite3_free(zFile); |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2188 | zFile = 0; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2189 | } |
| 2190 | *pFlags = flags; |
| 2191 | *pzFile = zFile; |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2192 | return rc; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | |
| 2196 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2197 | ** This routine does the work of opening a database on behalf of |
| 2198 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 2199 | ** is UTF-8 encoded. |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2200 | */ |
| 2201 | static int openDatabase( |
| 2202 | const char *zFilename, /* Database filename UTF-8 encoded */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2203 | sqlite3 **ppDb, /* OUT: Returned database handle */ |
drh | 522c26f | 2011-05-07 14:40:29 +0000 | [diff] [blame] | 2204 | unsigned int flags, /* Operational flags */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2205 | const char *zVfs /* Name of the VFS to use */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2206 | ){ |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2207 | sqlite3 *db; /* Store allocated handle here */ |
| 2208 | int rc; /* Return code */ |
| 2209 | int isThreadsafe; /* True for threadsafe connections */ |
| 2210 | char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 2211 | char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2212 | |
drh | e0d0f8e | 2009-04-28 04:47:31 +0000 | [diff] [blame] | 2213 | *ppDb = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2214 | #ifndef SQLITE_OMIT_AUTOINIT |
| 2215 | rc = sqlite3_initialize(); |
| 2216 | if( rc ) return rc; |
| 2217 | #endif |
| 2218 | |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2219 | /* Only allow sensible combinations of bits in the flags argument. |
| 2220 | ** Throw an error if any non-sense combination is used. If we |
| 2221 | ** do not block illegal combinations here, it could trigger |
| 2222 | ** assert() statements in deeper layers. Sensible combinations |
| 2223 | ** are: |
| 2224 | ** |
| 2225 | ** 1: SQLITE_OPEN_READONLY |
| 2226 | ** 2: SQLITE_OPEN_READWRITE |
| 2227 | ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
| 2228 | */ |
| 2229 | assert( SQLITE_OPEN_READONLY == 0x01 ); |
| 2230 | assert( SQLITE_OPEN_READWRITE == 0x02 ); |
| 2231 | assert( SQLITE_OPEN_CREATE == 0x04 ); |
| 2232 | testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
| 2233 | testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
| 2234 | testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
drh | 5dd72ad | 2011-05-07 18:18:33 +0000 | [diff] [blame] | 2235 | if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT; |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2236 | |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2237 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2238 | isThreadsafe = 0; |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2239 | }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 2240 | isThreadsafe = 0; |
| 2241 | }else if( flags & SQLITE_OPEN_FULLMUTEX ){ |
| 2242 | isThreadsafe = 1; |
| 2243 | }else{ |
| 2244 | isThreadsafe = sqlite3GlobalConfig.bFullMutex; |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2245 | } |
drh | f1f1268 | 2009-09-09 14:17:52 +0000 | [diff] [blame] | 2246 | if( flags & SQLITE_OPEN_PRIVATECACHE ){ |
drh | f4cfac9 | 2009-09-09 15:29:41 +0000 | [diff] [blame] | 2247 | flags &= ~SQLITE_OPEN_SHAREDCACHE; |
drh | f1f1268 | 2009-09-09 14:17:52 +0000 | [diff] [blame] | 2248 | }else if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
| 2249 | flags |= SQLITE_OPEN_SHAREDCACHE; |
| 2250 | } |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2251 | |
drh | b25a9f4 | 2009-05-12 13:35:11 +0000 | [diff] [blame] | 2252 | /* Remove harmful bits from the flags parameter |
| 2253 | ** |
| 2254 | ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were |
| 2255 | ** dealt with in the previous code block. Besides these, the only |
| 2256 | ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, |
drh | 03e1b40 | 2011-02-23 22:39:23 +0000 | [diff] [blame] | 2257 | ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE, |
| 2258 | ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask |
drh | b25a9f4 | 2009-05-12 13:35:11 +0000 | [diff] [blame] | 2259 | ** off all other flags. |
| 2260 | */ |
drh | a4267dc | 2008-02-19 15:20:44 +0000 | [diff] [blame] | 2261 | flags &= ~( SQLITE_OPEN_DELETEONCLOSE | |
drh | b25a9f4 | 2009-05-12 13:35:11 +0000 | [diff] [blame] | 2262 | SQLITE_OPEN_EXCLUSIVE | |
drh | a4267dc | 2008-02-19 15:20:44 +0000 | [diff] [blame] | 2263 | SQLITE_OPEN_MAIN_DB | |
| 2264 | SQLITE_OPEN_TEMP_DB | |
| 2265 | SQLITE_OPEN_TRANSIENT_DB | |
| 2266 | SQLITE_OPEN_MAIN_JOURNAL | |
| 2267 | SQLITE_OPEN_TEMP_JOURNAL | |
| 2268 | SQLITE_OPEN_SUBJOURNAL | |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 2269 | SQLITE_OPEN_MASTER_JOURNAL | |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2270 | SQLITE_OPEN_NOMUTEX | |
drh | 357b5f9 | 2010-08-24 18:07:57 +0000 | [diff] [blame] | 2271 | SQLITE_OPEN_FULLMUTEX | |
| 2272 | SQLITE_OPEN_WAL |
drh | a4267dc | 2008-02-19 15:20:44 +0000 | [diff] [blame] | 2273 | ); |
| 2274 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2275 | /* Allocate the sqlite data structure */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2276 | db = sqlite3MallocZero( sizeof(sqlite3) ); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2277 | if( db==0 ) goto opendb_out; |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 2278 | if( isThreadsafe ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 2279 | db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
| 2280 | if( db->mutex==0 ){ |
| 2281 | sqlite3_free(db); |
| 2282 | db = 0; |
| 2283 | goto opendb_out; |
| 2284 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2285 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 2286 | sqlite3_mutex_enter(db->mutex); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2287 | db->errMask = 0xff; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2288 | db->nDb = 2; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 2289 | db->magic = SQLITE_MAGIC_BUSY; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2290 | db->aDb = db->aDbStatic; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2291 | |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 2292 | assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); |
| 2293 | memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 2294 | db->autoCommit = 1; |
drh | ddac25c | 2007-12-05 01:38:23 +0000 | [diff] [blame] | 2295 | db->nextAutovac = -1; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2296 | db->nextPagesize = 0; |
drh | e83cafd | 2011-03-21 17:15:58 +0000 | [diff] [blame] | 2297 | db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger |
drh | 76fe803 | 2006-07-11 14:17:51 +0000 | [diff] [blame] | 2298 | #if SQLITE_DEFAULT_FILE_FORMAT<4 |
| 2299 | | SQLITE_LegacyFileFmt |
| 2300 | #endif |
drh | 56424db | 2007-03-27 22:24:11 +0000 | [diff] [blame] | 2301 | #ifdef SQLITE_ENABLE_LOAD_EXTENSION |
| 2302 | | SQLITE_LoadExtension |
| 2303 | #endif |
dan | 5bde73c | 2009-09-01 17:11:07 +0000 | [diff] [blame] | 2304 | #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS |
dan | 5e1a278 | 2009-09-01 17:28:29 +0000 | [diff] [blame] | 2305 | | SQLITE_RecTriggers |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 2306 | #endif |
drh | a4bfd7f | 2010-12-09 19:15:17 +0000 | [diff] [blame] | 2307 | #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS |
| 2308 | | SQLITE_ForeignKeys |
| 2309 | #endif |
drh | 76fe803 | 2006-07-11 14:17:51 +0000 | [diff] [blame] | 2310 | ; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 2311 | sqlite3HashInit(&db->aCollSeq); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2312 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 2313 | sqlite3HashInit(&db->aModule); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 2314 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2315 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2316 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 2317 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 2318 | ** conversions. The only error that can occur here is a malloc() failure. |
| 2319 | */ |
drh | 4dd65e0 | 2011-11-29 14:46:56 +0000 | [diff] [blame] | 2320 | createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); |
| 2321 | createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); |
| 2322 | createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); |
| 2323 | createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
drh | 77db4c0 | 2008-03-18 13:01:38 +0000 | [diff] [blame] | 2324 | if( db->mallocFailed ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2325 | goto opendb_out; |
| 2326 | } |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 2327 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
drh | 77db4c0 | 2008-03-18 13:01:38 +0000 | [diff] [blame] | 2328 | assert( db->pDfltColl!=0 ); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2329 | |
| 2330 | /* Also add a UTF-8 case-insensitive collation sequence. */ |
drh | 4dd65e0 | 2011-11-29 14:46:56 +0000 | [diff] [blame] | 2331 | createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 2332 | |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2333 | /* Parse the filename/URI argument. */ |
dan | 1943299 | 2011-05-10 18:39:10 +0000 | [diff] [blame] | 2334 | db->openFlags = flags; |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2335 | rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 2336 | if( rc!=SQLITE_OK ){ |
drh | ffd9668 | 2011-05-07 18:40:36 +0000 | [diff] [blame] | 2337 | if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
dan | 78e9dd2 | 2011-05-03 10:22:32 +0000 | [diff] [blame] | 2338 | sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg); |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2339 | sqlite3_free(zErrMsg); |
| 2340 | goto opendb_out; |
| 2341 | } |
| 2342 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2343 | /* Open the backend database driver */ |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 2344 | rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0, |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 2345 | flags | SQLITE_OPEN_MAIN_DB); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2346 | if( rc!=SQLITE_OK ){ |
danielk1977 | 45e60aa | 2008-09-23 17:39:26 +0000 | [diff] [blame] | 2347 | if( rc==SQLITE_IOERR_NOMEM ){ |
| 2348 | rc = SQLITE_NOMEM; |
| 2349 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2350 | sqlite3Error(db, rc, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2351 | goto opendb_out; |
| 2352 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2353 | db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); |
| 2354 | db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2355 | |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 2356 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2357 | /* The default safety_level for the main database is 'full'; for the temp |
| 2358 | ** database it is 'NONE'. This matches the pager layer defaults. |
| 2359 | */ |
| 2360 | db->aDb[0].zName = "main"; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 2361 | db->aDb[0].safety_level = 3; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2362 | db->aDb[1].zName = "temp"; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 2363 | db->aDb[1].safety_level = 1; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2364 | |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2365 | db->magic = SQLITE_MAGIC_OPEN; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2366 | if( db->mallocFailed ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2367 | goto opendb_out; |
| 2368 | } |
| 2369 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 2370 | /* Register all built-in functions, but do not attempt to read the |
| 2371 | ** database schema yet. This is delayed until the first time the database |
| 2372 | ** is accessed. |
| 2373 | */ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2374 | sqlite3Error(db, SQLITE_OK, 0); |
| 2375 | sqlite3RegisterBuiltinFunctions(db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2376 | |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2377 | /* Load automatic extensions - extensions that have been registered |
| 2378 | ** using the sqlite3_automatic_extension() API. |
| 2379 | */ |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 2380 | rc = sqlite3_errcode(db); |
drh | e5077c1 | 2011-12-13 04:08:36 +0000 | [diff] [blame] | 2381 | if( rc==SQLITE_OK ){ |
| 2382 | sqlite3AutoLoadExtensions(db); |
| 2383 | rc = sqlite3_errcode(db); |
| 2384 | if( rc!=SQLITE_OK ){ |
| 2385 | goto opendb_out; |
| 2386 | } |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2387 | } |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2388 | |
drh | aa29c13 | 2006-09-02 13:58:06 +0000 | [diff] [blame] | 2389 | #ifdef SQLITE_ENABLE_FTS1 |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2390 | if( !db->mallocFailed ){ |
drh | aa29c13 | 2006-09-02 13:58:06 +0000 | [diff] [blame] | 2391 | extern int sqlite3Fts1Init(sqlite3*); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2392 | rc = sqlite3Fts1Init(db); |
drh | aa29c13 | 2006-09-02 13:58:06 +0000 | [diff] [blame] | 2393 | } |
| 2394 | #endif |
| 2395 | |
shess | a26cf57 | 2006-10-19 20:27:58 +0000 | [diff] [blame] | 2396 | #ifdef SQLITE_ENABLE_FTS2 |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2397 | if( !db->mallocFailed && rc==SQLITE_OK ){ |
shess | a26cf57 | 2006-10-19 20:27:58 +0000 | [diff] [blame] | 2398 | extern int sqlite3Fts2Init(sqlite3*); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2399 | rc = sqlite3Fts2Init(db); |
shess | a26cf57 | 2006-10-19 20:27:58 +0000 | [diff] [blame] | 2400 | } |
| 2401 | #endif |
| 2402 | |
shess | 69c4ae2 | 2007-08-20 17:37:47 +0000 | [diff] [blame] | 2403 | #ifdef SQLITE_ENABLE_FTS3 |
| 2404 | if( !db->mallocFailed && rc==SQLITE_OK ){ |
shess | 69c4ae2 | 2007-08-20 17:37:47 +0000 | [diff] [blame] | 2405 | rc = sqlite3Fts3Init(db); |
| 2406 | } |
| 2407 | #endif |
| 2408 | |
danielk1977 | 83852ac | 2007-05-06 16:04:11 +0000 | [diff] [blame] | 2409 | #ifdef SQLITE_ENABLE_ICU |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2410 | if( !db->mallocFailed && rc==SQLITE_OK ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2411 | rc = sqlite3IcuInit(db); |
danielk1977 | 83852ac | 2007-05-06 16:04:11 +0000 | [diff] [blame] | 2412 | } |
| 2413 | #endif |
danielk1977 | ebaecc1 | 2008-05-26 18:41:54 +0000 | [diff] [blame] | 2414 | |
| 2415 | #ifdef SQLITE_ENABLE_RTREE |
| 2416 | if( !db->mallocFailed && rc==SQLITE_OK){ |
danielk1977 | ebaecc1 | 2008-05-26 18:41:54 +0000 | [diff] [blame] | 2417 | rc = sqlite3RtreeInit(db); |
| 2418 | } |
| 2419 | #endif |
| 2420 | |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2421 | sqlite3Error(db, rc, 0); |
danielk1977 | 83852ac | 2007-05-06 16:04:11 +0000 | [diff] [blame] | 2422 | |
drh | 8fd5bd3 | 2007-04-02 16:40:37 +0000 | [diff] [blame] | 2423 | /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking |
| 2424 | ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking |
| 2425 | ** mode. Doing nothing at all also makes NORMAL the default. |
| 2426 | */ |
| 2427 | #ifdef SQLITE_DEFAULT_LOCKING_MODE |
| 2428 | db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; |
| 2429 | sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), |
| 2430 | SQLITE_DEFAULT_LOCKING_MODE); |
| 2431 | #endif |
| 2432 | |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2433 | /* Enable the lookaside-malloc subsystem */ |
drh | 1b67f3c | 2008-10-10 17:41:28 +0000 | [diff] [blame] | 2434 | setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, |
| 2435 | sqlite3GlobalConfig.nLookaside); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2436 | |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 2437 | sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); |
dan | 586b9c8 | 2010-05-03 08:04:49 +0000 | [diff] [blame] | 2438 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2439 | opendb_out: |
dan | cd74b61 | 2011-04-22 19:37:32 +0000 | [diff] [blame] | 2440 | sqlite3_free(zOpen); |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 2441 | if( db ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 2442 | assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2443 | sqlite3_mutex_leave(db->mutex); |
| 2444 | } |
danielk1977 | 59633ae | 2008-08-27 19:01:57 +0000 | [diff] [blame] | 2445 | rc = sqlite3_errcode(db); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2446 | assert( db!=0 || rc==SQLITE_NOMEM ); |
danielk1977 | 59633ae | 2008-08-27 19:01:57 +0000 | [diff] [blame] | 2447 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2448 | sqlite3_close(db); |
| 2449 | db = 0; |
danielk1977 | 59633ae | 2008-08-27 19:01:57 +0000 | [diff] [blame] | 2450 | }else if( rc!=SQLITE_OK ){ |
| 2451 | db->magic = SQLITE_MAGIC_SICK; |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 2452 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2453 | *ppDb = db; |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 2454 | return sqlite3ApiExit(0, rc); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | /* |
| 2458 | ** Open a new database handle. |
| 2459 | */ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2460 | int sqlite3_open( |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2461 | const char *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 2462 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2463 | ){ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2464 | return openDatabase(zFilename, ppDb, |
| 2465 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
| 2466 | } |
| 2467 | int sqlite3_open_v2( |
drh | 428e282 | 2007-08-30 16:23:19 +0000 | [diff] [blame] | 2468 | const char *filename, /* Database filename (UTF-8) */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2469 | sqlite3 **ppDb, /* OUT: SQLite db handle */ |
| 2470 | int flags, /* Flags */ |
| 2471 | const char *zVfs /* Name of VFS module to use */ |
| 2472 | ){ |
drh | 522c26f | 2011-05-07 14:40:29 +0000 | [diff] [blame] | 2473 | return openDatabase(filename, ppDb, (unsigned int)flags, zVfs); |
danielk1977 | 83ab5a8 | 2004-05-21 11:39:05 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2476 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2477 | /* |
| 2478 | ** Open a new database handle. |
| 2479 | */ |
| 2480 | int sqlite3_open16( |
| 2481 | const void *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 2482 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2483 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2484 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2485 | sqlite3_value *pVal; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2486 | int rc; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2487 | |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2488 | assert( zFilename ); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2489 | assert( ppDb ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2490 | *ppDb = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2491 | #ifndef SQLITE_OMIT_AUTOINIT |
| 2492 | rc = sqlite3_initialize(); |
| 2493 | if( rc ) return rc; |
| 2494 | #endif |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2495 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2496 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 2497 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2498 | if( zFilename8 ){ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2499 | rc = openDatabase(zFilename8, ppDb, |
| 2500 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 2501 | assert( *ppDb || rc==SQLITE_NOMEM ); |
danielk1977 | f51bf48 | 2008-04-28 16:19:35 +0000 | [diff] [blame] | 2502 | if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ |
danielk1977 | fb103a8 | 2008-04-03 16:28:24 +0000 | [diff] [blame] | 2503 | ENC(*ppDb) = SQLITE_UTF16NATIVE; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2504 | } |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2505 | }else{ |
| 2506 | rc = SQLITE_NOMEM; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2507 | } |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2508 | sqlite3ValueFree(pVal); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 2509 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 2510 | return sqlite3ApiExit(0, rc); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2511 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2512 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2513 | |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2514 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2515 | ** Register a new collation sequence with the database handle db. |
| 2516 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2517 | int sqlite3_create_collation( |
| 2518 | sqlite3* db, |
| 2519 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 2520 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2521 | void* pCtx, |
| 2522 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 2523 | ){ |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2524 | int rc; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2525 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2526 | assert( !db->mallocFailed ); |
drh | 4dd65e0 | 2011-11-29 14:46:56 +0000 | [diff] [blame] | 2527 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2528 | rc = sqlite3ApiExit(db, rc); |
| 2529 | sqlite3_mutex_leave(db->mutex); |
| 2530 | return rc; |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | /* |
| 2534 | ** Register a new collation sequence with the database handle db. |
| 2535 | */ |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 2536 | int sqlite3_create_collation_v2( |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 2537 | sqlite3* db, |
| 2538 | const char *zName, |
| 2539 | int enc, |
| 2540 | void* pCtx, |
| 2541 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 2542 | void(*xDel)(void*) |
| 2543 | ){ |
| 2544 | int rc; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2545 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2546 | assert( !db->mallocFailed ); |
drh | 4dd65e0 | 2011-11-29 14:46:56 +0000 | [diff] [blame] | 2547 | rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2548 | rc = sqlite3ApiExit(db, rc); |
| 2549 | sqlite3_mutex_leave(db->mutex); |
| 2550 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2553 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2554 | /* |
| 2555 | ** Register a new collation sequence with the database handle db. |
| 2556 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2557 | int sqlite3_create_collation16( |
| 2558 | sqlite3* db, |
mihailim | bda2e62 | 2008-06-23 11:23:14 +0000 | [diff] [blame] | 2559 | const void *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 2560 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2561 | void* pCtx, |
| 2562 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 2563 | ){ |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2564 | int rc = SQLITE_OK; |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 2565 | char *zName8; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2566 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2567 | assert( !db->mallocFailed ); |
dan | b7dca7d | 2010-03-05 16:32:12 +0000 | [diff] [blame] | 2568 | zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2569 | if( zName8 ){ |
drh | 4dd65e0 | 2011-11-29 14:46:56 +0000 | [diff] [blame] | 2570 | rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2571 | sqlite3DbFree(db, zName8); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2572 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2573 | rc = sqlite3ApiExit(db, rc); |
| 2574 | sqlite3_mutex_leave(db->mutex); |
| 2575 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2576 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2577 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2578 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2579 | /* |
| 2580 | ** Register a collation sequence factory callback with the database handle |
| 2581 | ** db. Replace any previously installed collation sequence factory. |
| 2582 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2583 | int sqlite3_collation_needed( |
| 2584 | sqlite3 *db, |
| 2585 | void *pCollNeededArg, |
| 2586 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 2587 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2588 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2589 | db->xCollNeeded = xCollNeeded; |
| 2590 | db->xCollNeeded16 = 0; |
| 2591 | db->pCollNeededArg = pCollNeededArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2592 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2593 | return SQLITE_OK; |
| 2594 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2595 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2596 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2597 | /* |
| 2598 | ** Register a collation sequence factory callback with the database handle |
| 2599 | ** db. Replace any previously installed collation sequence factory. |
| 2600 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2601 | int sqlite3_collation_needed16( |
| 2602 | sqlite3 *db, |
| 2603 | void *pCollNeededArg, |
| 2604 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 2605 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2606 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2607 | db->xCollNeeded = 0; |
| 2608 | db->xCollNeeded16 = xCollNeeded16; |
| 2609 | db->pCollNeededArg = pCollNeededArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2610 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2611 | return SQLITE_OK; |
| 2612 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2613 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2614 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2615 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2616 | /* |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2617 | ** This function is now an anachronism. It used to be used to recover from a |
| 2618 | ** malloc() failure, but SQLite now does this automatically. |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2619 | */ |
drh | 7d97efb | 2007-10-12 19:35:48 +0000 | [diff] [blame] | 2620 | int sqlite3_global_recover(void){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2621 | return SQLITE_OK; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2622 | } |
| 2623 | #endif |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 2624 | |
| 2625 | /* |
| 2626 | ** Test to see whether or not the database connection is in autocommit |
| 2627 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 2628 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 2629 | ** by the next COMMIT or ROLLBACK. |
| 2630 | ** |
| 2631 | ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** |
| 2632 | */ |
| 2633 | int sqlite3_get_autocommit(sqlite3 *db){ |
| 2634 | return db->autoCommit; |
| 2635 | } |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2636 | |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2637 | /* |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 2638 | ** The following routines are subtitutes for constants SQLITE_CORRUPT, |
| 2639 | ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error |
| 2640 | ** constants. They server two purposes: |
| 2641 | ** |
| 2642 | ** 1. Serve as a convenient place to set a breakpoint in a debugger |
| 2643 | ** to detect when version error conditions occurs. |
| 2644 | ** |
| 2645 | ** 2. Invoke sqlite3_log() to provide the source code location where |
| 2646 | ** a low-level error is first detected. |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2647 | */ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 2648 | int sqlite3CorruptError(int lineno){ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 2649 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 2650 | sqlite3_log(SQLITE_CORRUPT, |
drh | 75e876b | 2010-06-23 17:59:51 +0000 | [diff] [blame] | 2651 | "database corruption at line %d of [%.10s]", |
| 2652 | lineno, 20+sqlite3_sourceid()); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2653 | return SQLITE_CORRUPT; |
| 2654 | } |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 2655 | int sqlite3MisuseError(int lineno){ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 2656 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 75e876b | 2010-06-23 17:59:51 +0000 | [diff] [blame] | 2657 | sqlite3_log(SQLITE_MISUSE, |
| 2658 | "misuse at line %d of [%.10s]", |
| 2659 | lineno, 20+sqlite3_sourceid()); |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 2660 | return SQLITE_MISUSE; |
| 2661 | } |
| 2662 | int sqlite3CantopenError(int lineno){ |
drh | af46dc1 | 2010-02-24 21:44:07 +0000 | [diff] [blame] | 2663 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
drh | 75e876b | 2010-06-23 17:59:51 +0000 | [diff] [blame] | 2664 | sqlite3_log(SQLITE_CANTOPEN, |
| 2665 | "cannot open file at line %d of [%.10s]", |
| 2666 | lineno, 20+sqlite3_sourceid()); |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 2667 | return SQLITE_CANTOPEN; |
| 2668 | } |
| 2669 | |
drh | 7c1817e | 2006-01-10 13:58:48 +0000 | [diff] [blame] | 2670 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2671 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2672 | /* |
| 2673 | ** This is a convenience routine that makes sure that all thread-specific |
| 2674 | ** data for this thread has been deallocated. |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 2675 | ** |
| 2676 | ** SQLite no longer uses thread-specific data so this routine is now a |
| 2677 | ** no-op. It is retained for historical compatibility. |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2678 | */ |
| 2679 | void sqlite3_thread_cleanup(void){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2680 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2681 | #endif |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2682 | |
| 2683 | /* |
| 2684 | ** Return meta information about a specific column of a database table. |
| 2685 | ** See comment in sqlite3.h (sqlite.h.in) for details. |
| 2686 | */ |
| 2687 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 2688 | int sqlite3_table_column_metadata( |
| 2689 | sqlite3 *db, /* Connection handle */ |
| 2690 | const char *zDbName, /* Database name or NULL */ |
| 2691 | const char *zTableName, /* Table name */ |
| 2692 | const char *zColumnName, /* Column name */ |
| 2693 | char const **pzDataType, /* OUTPUT: Declared data type */ |
| 2694 | char const **pzCollSeq, /* OUTPUT: Collation sequence name */ |
| 2695 | int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ |
| 2696 | int *pPrimaryKey, /* OUTPUT: True if column part of PK */ |
drh | 9be0716 | 2008-06-16 20:51:15 +0000 | [diff] [blame] | 2697 | int *pAutoinc /* OUTPUT: True if column is auto-increment */ |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2698 | ){ |
| 2699 | int rc; |
| 2700 | char *zErrMsg = 0; |
| 2701 | Table *pTab = 0; |
| 2702 | Column *pCol = 0; |
| 2703 | int iCol; |
| 2704 | |
| 2705 | char const *zDataType = 0; |
| 2706 | char const *zCollSeq = 0; |
| 2707 | int notnull = 0; |
| 2708 | int primarykey = 0; |
| 2709 | int autoinc = 0; |
| 2710 | |
| 2711 | /* Ensure the database schema has been loaded */ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2712 | sqlite3_mutex_enter(db->mutex); |
drh | 3cb3edc | 2008-03-07 21:37:19 +0000 | [diff] [blame] | 2713 | sqlite3BtreeEnterAll(db); |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2714 | rc = sqlite3Init(db, &zErrMsg); |
| 2715 | if( SQLITE_OK!=rc ){ |
| 2716 | goto error_out; |
| 2717 | } |
| 2718 | |
| 2719 | /* Locate the table in question */ |
| 2720 | pTab = sqlite3FindTable(db, zTableName, zDbName); |
| 2721 | if( !pTab || pTab->pSelect ){ |
| 2722 | pTab = 0; |
| 2723 | goto error_out; |
| 2724 | } |
| 2725 | |
| 2726 | /* Find the column for which info is requested */ |
| 2727 | if( sqlite3IsRowid(zColumnName) ){ |
| 2728 | iCol = pTab->iPKey; |
| 2729 | if( iCol>=0 ){ |
| 2730 | pCol = &pTab->aCol[iCol]; |
| 2731 | } |
| 2732 | }else{ |
| 2733 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 2734 | pCol = &pTab->aCol[iCol]; |
| 2735 | if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ |
| 2736 | break; |
| 2737 | } |
| 2738 | } |
| 2739 | if( iCol==pTab->nCol ){ |
| 2740 | pTab = 0; |
| 2741 | goto error_out; |
| 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | /* The following block stores the meta information that will be returned |
| 2746 | ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey |
| 2747 | ** and autoinc. At this point there are two possibilities: |
| 2748 | ** |
| 2749 | ** 1. The specified column name was rowid", "oid" or "_rowid_" |
| 2750 | ** and there is no explicitly declared IPK column. |
| 2751 | ** |
| 2752 | ** 2. The table is not a view and the column name identified an |
| 2753 | ** explicitly declared column. Copy meta information from *pCol. |
| 2754 | */ |
| 2755 | if( pCol ){ |
| 2756 | zDataType = pCol->zType; |
| 2757 | zCollSeq = pCol->zColl; |
drh | 9be0716 | 2008-06-16 20:51:15 +0000 | [diff] [blame] | 2758 | notnull = pCol->notNull!=0; |
drh | a371ace | 2012-09-13 14:22:47 +0000 | [diff] [blame] | 2759 | primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2760 | autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2761 | }else{ |
| 2762 | zDataType = "INTEGER"; |
| 2763 | primarykey = 1; |
| 2764 | } |
| 2765 | if( !zCollSeq ){ |
| 2766 | zCollSeq = "BINARY"; |
| 2767 | } |
| 2768 | |
| 2769 | error_out: |
danielk1977 | 02b4e3b | 2009-02-26 07:15:59 +0000 | [diff] [blame] | 2770 | sqlite3BtreeLeaveAll(db); |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2771 | |
| 2772 | /* Whether the function call succeeded or failed, set the output parameters |
| 2773 | ** to whatever their local counterparts contain. If an error did occur, |
| 2774 | ** this has the effect of zeroing all output parameters. |
| 2775 | */ |
| 2776 | if( pzDataType ) *pzDataType = zDataType; |
| 2777 | if( pzCollSeq ) *pzCollSeq = zCollSeq; |
| 2778 | if( pNotNull ) *pNotNull = notnull; |
| 2779 | if( pPrimaryKey ) *pPrimaryKey = primarykey; |
| 2780 | if( pAutoinc ) *pAutoinc = autoinc; |
| 2781 | |
| 2782 | if( SQLITE_OK==rc && !pTab ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2783 | sqlite3DbFree(db, zErrMsg); |
drh | 118640b | 2008-07-16 14:02:32 +0000 | [diff] [blame] | 2784 | zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2785 | zColumnName); |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2786 | rc = SQLITE_ERROR; |
| 2787 | } |
| 2788 | sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2789 | sqlite3DbFree(db, zErrMsg); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2790 | rc = sqlite3ApiExit(db, rc); |
| 2791 | sqlite3_mutex_leave(db->mutex); |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 2792 | return rc; |
| 2793 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2794 | #endif |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 2795 | |
| 2796 | /* |
| 2797 | ** Sleep for a little while. Return the amount of time slept. |
| 2798 | */ |
| 2799 | int sqlite3_sleep(int ms){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2800 | sqlite3_vfs *pVfs; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2801 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2802 | pVfs = sqlite3_vfs_find(0); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2803 | if( pVfs==0 ) return 0; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2804 | |
| 2805 | /* This function works in milliseconds, but the underlying OsSleep() |
| 2806 | ** API uses microseconds. Hence the 1000's. |
| 2807 | */ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2808 | rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2809 | return rc; |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 2810 | } |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2811 | |
| 2812 | /* |
| 2813 | ** Enable or disable the extended result codes. |
| 2814 | */ |
| 2815 | int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2816 | sqlite3_mutex_enter(db->mutex); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2817 | db->errMask = onoff ? 0xffffffff : 0xff; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2818 | sqlite3_mutex_leave(db->mutex); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2819 | return SQLITE_OK; |
| 2820 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2821 | |
| 2822 | /* |
| 2823 | ** Invoke the xFileControl method on a particular database. |
| 2824 | */ |
| 2825 | int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
| 2826 | int rc = SQLITE_ERROR; |
drh | 421377e | 2012-03-15 21:28:54 +0000 | [diff] [blame] | 2827 | Btree *pBtree; |
| 2828 | |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2829 | sqlite3_mutex_enter(db->mutex); |
drh | 421377e | 2012-03-15 21:28:54 +0000 | [diff] [blame] | 2830 | pBtree = sqlite3DbNameToBtree(db, zDbName); |
| 2831 | if( pBtree ){ |
| 2832 | Pager *pPager; |
| 2833 | sqlite3_file *fd; |
| 2834 | sqlite3BtreeEnter(pBtree); |
| 2835 | pPager = sqlite3BtreePager(pBtree); |
| 2836 | assert( pPager!=0 ); |
| 2837 | fd = sqlite3PagerFile(pPager); |
| 2838 | assert( fd!=0 ); |
| 2839 | if( op==SQLITE_FCNTL_FILE_POINTER ){ |
| 2840 | *(sqlite3_file**)pArg = fd; |
| 2841 | rc = SQLITE_OK; |
| 2842 | }else if( fd->pMethods ){ |
| 2843 | rc = sqlite3OsFileControl(fd, op, pArg); |
| 2844 | }else{ |
| 2845 | rc = SQLITE_NOTFOUND; |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2846 | } |
drh | 421377e | 2012-03-15 21:28:54 +0000 | [diff] [blame] | 2847 | sqlite3BtreeLeave(pBtree); |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2848 | } |
| 2849 | sqlite3_mutex_leave(db->mutex); |
| 2850 | return rc; |
| 2851 | } |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2852 | |
| 2853 | /* |
| 2854 | ** Interface to the testing logic. |
| 2855 | */ |
| 2856 | int sqlite3_test_control(int op, ...){ |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2857 | int rc = 0; |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 2858 | #ifndef SQLITE_OMIT_BUILTIN_TEST |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2859 | va_list ap; |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2860 | va_start(ap, op); |
| 2861 | switch( op ){ |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 2862 | |
| 2863 | /* |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2864 | ** Save the current state of the PRNG. |
| 2865 | */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2866 | case SQLITE_TESTCTRL_PRNG_SAVE: { |
| 2867 | sqlite3PrngSaveState(); |
| 2868 | break; |
| 2869 | } |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2870 | |
| 2871 | /* |
| 2872 | ** Restore the state of the PRNG to the last state saved using |
| 2873 | ** PRNG_SAVE. If PRNG_SAVE has never before been called, then |
| 2874 | ** this verb acts like PRNG_RESET. |
| 2875 | */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2876 | case SQLITE_TESTCTRL_PRNG_RESTORE: { |
| 2877 | sqlite3PrngRestoreState(); |
| 2878 | break; |
| 2879 | } |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2880 | |
| 2881 | /* |
| 2882 | ** Reset the PRNG back to its uninitialized state. The next call |
| 2883 | ** to sqlite3_randomness() will reseed the PRNG using a single call |
| 2884 | ** to the xRandomness method of the default VFS. |
| 2885 | */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2886 | case SQLITE_TESTCTRL_PRNG_RESET: { |
| 2887 | sqlite3PrngResetState(); |
| 2888 | break; |
| 2889 | } |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 2890 | |
| 2891 | /* |
| 2892 | ** sqlite3_test_control(BITVEC_TEST, size, program) |
| 2893 | ** |
| 2894 | ** Run a test against a Bitvec object of size. The program argument |
| 2895 | ** is an array of integers that defines the test. Return -1 on a |
| 2896 | ** memory allocation error, 0 on success, or non-zero for an error. |
| 2897 | ** See the sqlite3BitvecBuiltinTest() for additional information. |
| 2898 | */ |
| 2899 | case SQLITE_TESTCTRL_BITVEC_TEST: { |
| 2900 | int sz = va_arg(ap, int); |
| 2901 | int *aProg = va_arg(ap, int*); |
| 2902 | rc = sqlite3BitvecBuiltinTest(sz, aProg); |
| 2903 | break; |
| 2904 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2905 | |
| 2906 | /* |
| 2907 | ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) |
| 2908 | ** |
| 2909 | ** Register hooks to call to indicate which malloc() failures |
| 2910 | ** are benign. |
| 2911 | */ |
| 2912 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { |
danielk1977 | ca02679 | 2008-06-23 14:15:52 +0000 | [diff] [blame] | 2913 | typedef void (*void_function)(void); |
| 2914 | void_function xBenignBegin; |
| 2915 | void_function xBenignEnd; |
| 2916 | xBenignBegin = va_arg(ap, void_function); |
| 2917 | xBenignEnd = va_arg(ap, void_function); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2918 | sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); |
| 2919 | break; |
| 2920 | } |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 2921 | |
| 2922 | /* |
drh | f3af63f | 2009-05-09 18:59:42 +0000 | [diff] [blame] | 2923 | ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 2924 | ** |
| 2925 | ** Set the PENDING byte to the value in the argument, if X>0. |
| 2926 | ** Make no changes if X==0. Return the value of the pending byte |
| 2927 | ** as it existing before this routine was called. |
| 2928 | ** |
| 2929 | ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in |
| 2930 | ** an incompatible database file format. Changing the PENDING byte |
| 2931 | ** while any database connection is open results in undefined and |
| 2932 | ** dileterious behavior. |
| 2933 | */ |
| 2934 | case SQLITE_TESTCTRL_PENDING_BYTE: { |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 2935 | rc = PENDING_BYTE; |
| 2936 | #ifndef SQLITE_OMIT_WSD |
| 2937 | { |
| 2938 | unsigned int newVal = va_arg(ap, unsigned int); |
| 2939 | if( newVal ) sqlite3PendingByte = newVal; |
| 2940 | } |
| 2941 | #endif |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 2942 | break; |
| 2943 | } |
drh | f3af63f | 2009-05-09 18:59:42 +0000 | [diff] [blame] | 2944 | |
| 2945 | /* |
| 2946 | ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X) |
| 2947 | ** |
| 2948 | ** This action provides a run-time test to see whether or not |
| 2949 | ** assert() was enabled at compile-time. If X is true and assert() |
| 2950 | ** is enabled, then the return value is true. If X is true and |
| 2951 | ** assert() is disabled, then the return value is zero. If X is |
| 2952 | ** false and assert() is enabled, then the assertion fires and the |
| 2953 | ** process aborts. If X is false and assert() is disabled, then the |
| 2954 | ** return value is zero. |
| 2955 | */ |
| 2956 | case SQLITE_TESTCTRL_ASSERT: { |
| 2957 | volatile int x = 0; |
| 2958 | assert( (x = va_arg(ap,int))!=0 ); |
| 2959 | rc = x; |
| 2960 | break; |
| 2961 | } |
| 2962 | |
| 2963 | |
| 2964 | /* |
| 2965 | ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) |
| 2966 | ** |
| 2967 | ** This action provides a run-time test to see how the ALWAYS and |
| 2968 | ** NEVER macros were defined at compile-time. |
| 2969 | ** |
| 2970 | ** The return value is ALWAYS(X). |
| 2971 | ** |
| 2972 | ** The recommended test is X==2. If the return value is 2, that means |
| 2973 | ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the |
| 2974 | ** default setting. If the return value is 1, then ALWAYS() is either |
| 2975 | ** hard-coded to true or else it asserts if its argument is false. |
| 2976 | ** The first behavior (hard-coded to true) is the case if |
| 2977 | ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second |
| 2978 | ** behavior (assert if the argument to ALWAYS() is false) is the case if |
| 2979 | ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled. |
| 2980 | ** |
| 2981 | ** The run-time test procedure might look something like this: |
| 2982 | ** |
| 2983 | ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){ |
| 2984 | ** // ALWAYS() and NEVER() are no-op pass-through macros |
| 2985 | ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){ |
| 2986 | ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false. |
| 2987 | ** }else{ |
| 2988 | ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. |
| 2989 | ** } |
| 2990 | */ |
| 2991 | case SQLITE_TESTCTRL_ALWAYS: { |
| 2992 | int x = va_arg(ap,int); |
| 2993 | rc = ALWAYS(x); |
| 2994 | break; |
| 2995 | } |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 2996 | |
| 2997 | /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) |
| 2998 | ** |
| 2999 | ** Set the nReserve size to N for the main database on the database |
| 3000 | ** connection db. |
| 3001 | */ |
| 3002 | case SQLITE_TESTCTRL_RESERVE: { |
| 3003 | sqlite3 *db = va_arg(ap, sqlite3*); |
| 3004 | int x = va_arg(ap,int); |
| 3005 | sqlite3_mutex_enter(db->mutex); |
| 3006 | sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); |
| 3007 | sqlite3_mutex_leave(db->mutex); |
| 3008 | break; |
| 3009 | } |
| 3010 | |
drh | 07096f6 | 2009-12-22 23:52:32 +0000 | [diff] [blame] | 3011 | /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) |
| 3012 | ** |
| 3013 | ** Enable or disable various optimizations for testing purposes. The |
| 3014 | ** argument N is a bitmask of optimizations to be disabled. For normal |
| 3015 | ** operation N should be 0. The idea is that a test program (like the |
| 3016 | ** SQL Logic Test or SLT test module) can run the same SQL multiple times |
| 3017 | ** with various optimizations disabled to verify that the same answer |
| 3018 | ** is obtained in every case. |
| 3019 | */ |
| 3020 | case SQLITE_TESTCTRL_OPTIMIZATIONS: { |
| 3021 | sqlite3 *db = va_arg(ap, sqlite3*); |
drh | a575967 | 2012-10-30 14:39:12 +0000 | [diff] [blame^] | 3022 | db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff); |
drh | 07096f6 | 2009-12-22 23:52:32 +0000 | [diff] [blame] | 3023 | break; |
| 3024 | } |
| 3025 | |
drh | 0e85773 | 2010-01-02 03:21:35 +0000 | [diff] [blame] | 3026 | #ifdef SQLITE_N_KEYWORD |
| 3027 | /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) |
| 3028 | ** |
| 3029 | ** If zWord is a keyword recognized by the parser, then return the |
| 3030 | ** number of keywords. Or if zWord is not a keyword, return 0. |
| 3031 | ** |
| 3032 | ** This test feature is only available in the amalgamation since |
| 3033 | ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite |
| 3034 | ** is built using separate source files. |
| 3035 | */ |
| 3036 | case SQLITE_TESTCTRL_ISKEYWORD: { |
| 3037 | const char *zWord = va_arg(ap, const char*); |
| 3038 | int n = sqlite3Strlen30(zWord); |
drh | 855787a | 2010-01-02 03:46:43 +0000 | [diff] [blame] | 3039 | rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; |
drh | 0e85773 | 2010-01-02 03:21:35 +0000 | [diff] [blame] | 3040 | break; |
| 3041 | } |
| 3042 | #endif |
| 3043 | |
drh | badc980 | 2010-08-27 17:16:44 +0000 | [diff] [blame] | 3044 | /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); |
| 3045 | ** |
| 3046 | ** Pass pFree into sqlite3ScratchFree(). |
| 3047 | ** If sz>0 then allocate a scratch buffer into pNew. |
| 3048 | */ |
| 3049 | case SQLITE_TESTCTRL_SCRATCHMALLOC: { |
| 3050 | void *pFree, **ppNew; |
| 3051 | int sz; |
| 3052 | sz = va_arg(ap, int); |
| 3053 | ppNew = va_arg(ap, void**); |
| 3054 | pFree = va_arg(ap, void*); |
| 3055 | if( sz ) *ppNew = sqlite3ScratchMalloc(sz); |
| 3056 | sqlite3ScratchFree(pFree); |
| 3057 | break; |
| 3058 | } |
| 3059 | |
dan | c17d696 | 2011-06-21 12:47:30 +0000 | [diff] [blame] | 3060 | /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); |
| 3061 | ** |
| 3062 | ** If parameter onoff is non-zero, configure the wrappers so that all |
| 3063 | ** subsequent calls to localtime() and variants fail. If onoff is zero, |
| 3064 | ** undo this setting. |
| 3065 | */ |
| 3066 | case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 3067 | sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); |
| 3068 | break; |
| 3069 | } |
| 3070 | |
drh | 678a9aa | 2011-12-10 15:55:01 +0000 | [diff] [blame] | 3071 | #if defined(SQLITE_ENABLE_TREE_EXPLAIN) |
drh | 7e02e5e | 2011-12-06 19:44:51 +0000 | [diff] [blame] | 3072 | /* sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, |
| 3073 | ** sqlite3_stmt*,const char**); |
| 3074 | ** |
drh | 678a9aa | 2011-12-10 15:55:01 +0000 | [diff] [blame] | 3075 | ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds |
| 3076 | ** a string that describes the optimized parse tree. This test-control |
| 3077 | ** returns a pointer to that string. |
drh | 7e02e5e | 2011-12-06 19:44:51 +0000 | [diff] [blame] | 3078 | */ |
| 3079 | case SQLITE_TESTCTRL_EXPLAIN_STMT: { |
| 3080 | sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*); |
| 3081 | const char **pzRet = va_arg(ap, const char**); |
| 3082 | *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt); |
| 3083 | break; |
| 3084 | } |
drh | 678a9aa | 2011-12-10 15:55:01 +0000 | [diff] [blame] | 3085 | #endif |
drh | 7e02e5e | 2011-12-06 19:44:51 +0000 | [diff] [blame] | 3086 | |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 3087 | } |
| 3088 | va_end(ap); |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 3089 | #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
danielk1977 | 7c36d07 | 2008-01-31 15:31:01 +0000 | [diff] [blame] | 3090 | return rc; |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 3091 | } |
drh | cc487d1 | 2011-05-17 18:53:08 +0000 | [diff] [blame] | 3092 | |
| 3093 | /* |
| 3094 | ** This is a utility routine, useful to VFS implementations, that checks |
| 3095 | ** to see if a database file was a URI that contained a specific query |
| 3096 | ** parameter, and if so obtains the value of the query parameter. |
| 3097 | ** |
| 3098 | ** The zFilename argument is the filename pointer passed into the xOpen() |
| 3099 | ** method of a VFS implementation. The zParam argument is the name of the |
| 3100 | ** query parameter we seek. This routine returns the value of the zParam |
| 3101 | ** parameter if it exists. If the parameter does not exist, this routine |
| 3102 | ** returns a NULL pointer. |
| 3103 | */ |
| 3104 | const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){ |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 3105 | if( zFilename==0 ) return 0; |
drh | bd69559 | 2011-05-17 19:43:38 +0000 | [diff] [blame] | 3106 | zFilename += sqlite3Strlen30(zFilename) + 1; |
drh | cc487d1 | 2011-05-17 18:53:08 +0000 | [diff] [blame] | 3107 | while( zFilename[0] ){ |
| 3108 | int x = strcmp(zFilename, zParam); |
drh | bd69559 | 2011-05-17 19:43:38 +0000 | [diff] [blame] | 3109 | zFilename += sqlite3Strlen30(zFilename) + 1; |
drh | cc487d1 | 2011-05-17 18:53:08 +0000 | [diff] [blame] | 3110 | if( x==0 ) return zFilename; |
drh | bd69559 | 2011-05-17 19:43:38 +0000 | [diff] [blame] | 3111 | zFilename += sqlite3Strlen30(zFilename) + 1; |
drh | cc487d1 | 2011-05-17 18:53:08 +0000 | [diff] [blame] | 3112 | } |
| 3113 | return 0; |
| 3114 | } |
drh | 283829c | 2011-11-17 00:56:20 +0000 | [diff] [blame] | 3115 | |
| 3116 | /* |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 3117 | ** Return a boolean value for a query parameter. |
| 3118 | */ |
| 3119 | int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){ |
| 3120 | const char *z = sqlite3_uri_parameter(zFilename, zParam); |
drh | 38d9c61 | 2012-01-31 14:24:47 +0000 | [diff] [blame] | 3121 | bDflt = bDflt!=0; |
| 3122 | return z ? sqlite3GetBoolean(z, bDflt) : bDflt; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | /* |
| 3126 | ** Return a 64-bit integer value for a query parameter. |
| 3127 | */ |
| 3128 | sqlite3_int64 sqlite3_uri_int64( |
| 3129 | const char *zFilename, /* Filename as passed to xOpen */ |
| 3130 | const char *zParam, /* URI parameter sought */ |
| 3131 | sqlite3_int64 bDflt /* return if parameter is missing */ |
| 3132 | ){ |
| 3133 | const char *z = sqlite3_uri_parameter(zFilename, zParam); |
| 3134 | sqlite3_int64 v; |
| 3135 | if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){ |
| 3136 | bDflt = v; |
| 3137 | } |
| 3138 | return bDflt; |
| 3139 | } |
| 3140 | |
| 3141 | /* |
drh | 421377e | 2012-03-15 21:28:54 +0000 | [diff] [blame] | 3142 | ** Return the Btree pointer identified by zDbName. Return NULL if not found. |
| 3143 | */ |
| 3144 | Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){ |
| 3145 | int i; |
| 3146 | for(i=0; i<db->nDb; i++){ |
| 3147 | if( db->aDb[i].pBt |
| 3148 | && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0) |
| 3149 | ){ |
| 3150 | return db->aDb[i].pBt; |
| 3151 | } |
| 3152 | } |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
drh | 283829c | 2011-11-17 00:56:20 +0000 | [diff] [blame] | 3157 | ** Return the filename of the database associated with a database |
| 3158 | ** connection. |
| 3159 | */ |
| 3160 | const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
drh | 421377e | 2012-03-15 21:28:54 +0000 | [diff] [blame] | 3161 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 3162 | return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 3163 | } |
| 3164 | |
| 3165 | /* |
| 3166 | ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 3167 | ** no such database exists. |
| 3168 | */ |
| 3169 | int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 3170 | Btree *pBt = sqlite3DbNameToBtree(db, zDbName); |
| 3171 | return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1; |
drh | 283829c | 2011-11-17 00:56:20 +0000 | [diff] [blame] | 3172 | } |