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 | |
| 29 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 30 | ** The version of the library |
| 31 | */ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 32 | #ifndef SQLITE_AMALGAMATION |
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 | 4aec8b6 | 2004-08-28 16:19:00 +0000 | [diff] [blame] | 35 | const char *sqlite3_libversion(void){ return sqlite3_version; } |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 36 | const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
danielk1977 | 99ba19e | 2005-02-05 07:33:34 +0000 | [diff] [blame] | 37 | int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
drh | b67e8bf | 2007-08-30 20:09:48 +0000 | [diff] [blame] | 38 | int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
shaneh | ca07b9d | 2010-02-23 05:17:51 +0000 | [diff] [blame] | 39 | const char *sqlite3_compileopts(void){ |
shaneh | bdea6d1 | 2010-02-23 04:19:54 +0000 | [diff] [blame] | 40 | static char zOpts[32] = ""; |
| 41 | sqlite_int64 iEnable = 0; /* bitmask of all the SQLITE_ENABLE* defines */ |
| 42 | sqlite_int64 iOmit = 0; /* bitmask of all the SQLITE_OMIT* defines */ |
| 43 | sqlite_int64 iOther = 0; /* bitmask of all the SQLITE_* defines except */ |
| 44 | /* SQLITE_MAX* and SQLITE_DEF* */ |
| 45 | |
shaneh | ca07b9d | 2010-02-23 05:17:51 +0000 | [diff] [blame] | 46 | #ifdef SQLITE_32BIT_ROWID
|
| 47 | iOther |= ((sqlite_int64)1<<0);
|
| 48 | #endif
|
| 49 | #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
|
| 50 | iOther |= ((sqlite_int64)1<<1);
|
| 51 | #endif
|
| 52 | #ifdef SQLITE_API
|
| 53 | iOther |= ((sqlite_int64)1<<2);
|
| 54 | #endif
|
| 55 | #ifdef SQLITE_CASE_SENSITIVE_LIKE
|
| 56 | iOther |= ((sqlite_int64)1<<3);
|
| 57 | #endif
|
| 58 | #ifdef SQLITE_CHECK_PAGES
|
| 59 | iOther |= ((sqlite_int64)1<<4);
|
| 60 | #endif
|
| 61 | #ifdef SQLITE_COVERAGE_TEST
|
| 62 | iOther |= ((sqlite_int64)1<<5);
|
| 63 | #endif
|
| 64 | #ifdef SQLITE_DEBUG
|
| 65 | iOther |= ((sqlite_int64)1<<6);
|
| 66 | #endif
|
| 67 | #ifdef SQLITE_DISABLE_DIRSYNC
|
| 68 | iOther |= ((sqlite_int64)1<<7);
|
| 69 | #endif
|
| 70 | #ifdef SQLITE_DISABLE_LFS
|
| 71 | iOther |= ((sqlite_int64)1<<8);
|
| 72 | #endif
|
| 73 | #ifdef SQLITE_ENABLE_ATOMIC_WRITE
|
| 74 | iEnable |= ((sqlite_int64)1<<0);
|
| 75 | #endif
|
| 76 | #ifdef SQLITE_ENABLE_CEROD
|
| 77 | iEnable |= ((sqlite_int64)1<<1);
|
| 78 | #endif
|
| 79 | #ifdef SQLITE_ENABLE_COLUMN_METADATA
|
| 80 | iEnable |= ((sqlite_int64)1<<2);
|
| 81 | #endif
|
| 82 | #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
|
| 83 | iEnable |= ((sqlite_int64)1<<3);
|
| 84 | #endif
|
| 85 | #ifdef SQLITE_ENABLE_FTS1
|
| 86 | iEnable |= ((sqlite_int64)1<<4);
|
| 87 | #endif
|
| 88 | #ifdef SQLITE_ENABLE_FTS2
|
| 89 | iEnable |= ((sqlite_int64)1<<5);
|
| 90 | #endif
|
| 91 | #ifdef SQLITE_ENABLE_FTS3
|
| 92 | iEnable |= ((sqlite_int64)1<<6);
|
| 93 | #endif
|
| 94 | #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
|
| 95 | iEnable |= ((sqlite_int64)1<<7);
|
| 96 | #endif
|
| 97 | #ifdef SQLITE_ENABLE_FTS4
|
| 98 | iEnable |= ((sqlite_int64)1<<8);
|
| 99 | #endif
|
| 100 | #ifdef SQLITE_ENABLE_ICU
|
| 101 | iEnable |= ((sqlite_int64)1<<9);
|
| 102 | #endif
|
| 103 | #ifdef SQLITE_ENABLE_IOTRACE
|
| 104 | iEnable |= ((sqlite_int64)1<<10);
|
| 105 | #endif
|
| 106 | #ifdef SQLITE_ENABLE_LOAD_EXTENSION
|
| 107 | iEnable |= ((sqlite_int64)1<<11);
|
| 108 | #endif
|
| 109 | #ifdef SQLITE_ENABLE_LOCKING_STYLE
|
| 110 | iEnable |= ((sqlite_int64)1<<12);
|
| 111 | #endif
|
| 112 | #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
|
| 113 | iEnable |= ((sqlite_int64)1<<13);
|
| 114 | #endif
|
| 115 | #ifdef SQLITE_ENABLE_MEMSYS3
|
| 116 | iEnable |= ((sqlite_int64)1<<14);
|
| 117 | #endif
|
| 118 | #ifdef SQLITE_ENABLE_MEMSYS5
|
| 119 | iEnable |= ((sqlite_int64)1<<15);
|
| 120 | #endif
|
| 121 | #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
|
| 122 | iEnable |= ((sqlite_int64)1<<16);
|
| 123 | #endif
|
| 124 | #ifdef SQLITE_ENABLE_RTREE
|
| 125 | iEnable |= ((sqlite_int64)1<<17);
|
| 126 | #endif
|
| 127 | #ifdef SQLITE_ENABLE_STAT2
|
| 128 | iEnable |= ((sqlite_int64)1<<18);
|
| 129 | #endif
|
| 130 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
|
| 131 | iEnable |= ((sqlite_int64)1<<19);
|
| 132 | #endif
|
| 133 | #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
|
| 134 | iEnable |= ((sqlite_int64)1<<20);
|
| 135 | #endif
|
| 136 | #ifdef SQLITE_HAS_CODEC
|
| 137 | iOther |= ((sqlite_int64)1<<9);
|
| 138 | #endif
|
| 139 | #ifdef SQLITE_HAVE_ISNAN
|
| 140 | iOther |= ((sqlite_int64)1<<10);
|
| 141 | #endif
|
| 142 | #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
|
| 143 | iOther |= ((sqlite_int64)1<<11);
|
| 144 | #endif
|
| 145 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
|
| 146 | iOther |= ((sqlite_int64)1<<12);
|
| 147 | #endif
|
| 148 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
|
| 149 | iOther |= ((sqlite_int64)1<<13);
|
| 150 | #endif
|
| 151 | #ifdef SQLITE_INT64_TYPE
|
| 152 | iOther |= ((sqlite_int64)1<<14);
|
| 153 | #endif
|
| 154 | #ifdef SQLITE_LOCK_TRACE
|
| 155 | iOther |= ((sqlite_int64)1<<15);
|
| 156 | #endif
|
| 157 | #ifdef SQLITE_MEMDEBUG
|
| 158 | iOther |= ((sqlite_int64)1<<16);
|
| 159 | #endif
|
| 160 | #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
|
| 161 | iOther |= ((sqlite_int64)1<<17);
|
| 162 | #endif
|
| 163 | #ifdef SQLITE_MUTEX_NOOP
|
| 164 | iOther |= ((sqlite_int64)1<<18);
|
| 165 | #endif
|
| 166 | #ifdef SQLITE_MUTEX_OMIT
|
| 167 | iOther |= ((sqlite_int64)1<<19);
|
| 168 | #endif
|
| 169 | #ifdef SQLITE_MUTEX_OS2
|
| 170 | iOther |= ((sqlite_int64)1<<20);
|
| 171 | #endif
|
| 172 | #ifdef SQLITE_MUTEX_PTHREADS
|
| 173 | iOther |= ((sqlite_int64)1<<21);
|
| 174 | #endif
|
| 175 | #ifdef SQLITE_MUTEX_W32
|
| 176 | iOther |= ((sqlite_int64)1<<22);
|
| 177 | #endif
|
| 178 | #ifdef SQLITE_NO_SYNC
|
| 179 | iOther |= ((sqlite_int64)1<<23);
|
| 180 | #endif
|
| 181 | #ifdef SQLITE_OMIT_ALTERTABLE
|
| 182 | iOmit |= ((sqlite_int64)1<<0);
|
| 183 | #endif
|
| 184 | #ifdef SQLITE_OMIT_ANALYZE
|
| 185 | iOmit |= ((sqlite_int64)1<<1);
|
| 186 | #endif
|
| 187 | #ifdef SQLITE_OMIT_ATTACH
|
| 188 | iOmit |= ((sqlite_int64)1<<2);
|
| 189 | #endif
|
| 190 | #ifdef SQLITE_OMIT_AUTHORIZATION
|
| 191 | iOmit |= ((sqlite_int64)1<<3);
|
| 192 | #endif
|
| 193 | #ifdef SQLITE_OMIT_AUTOINCREMENT
|
| 194 | iOmit |= ((sqlite_int64)1<<4);
|
| 195 | #endif
|
| 196 | #ifdef SQLITE_OMIT_AUTOINIT
|
| 197 | iOmit |= ((sqlite_int64)1<<5);
|
| 198 | #endif
|
| 199 | #ifdef SQLITE_OMIT_AUTOVACUUM
|
| 200 | iOmit |= ((sqlite_int64)1<<6);
|
| 201 | #endif
|
| 202 | #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
|
| 203 | iOmit |= ((sqlite_int64)1<<7);
|
| 204 | #endif
|
| 205 | #ifdef SQLITE_OMIT_BLOB_LITERAL
|
| 206 | iOmit |= ((sqlite_int64)1<<8);
|
| 207 | #endif
|
| 208 | #ifdef SQLITE_OMIT_BTREECOUNT
|
| 209 | iOmit |= ((sqlite_int64)1<<9);
|
| 210 | #endif
|
| 211 | #ifdef SQLITE_OMIT_BUILTIN_TEST
|
| 212 | iOmit |= ((sqlite_int64)1<<10);
|
| 213 | #endif
|
| 214 | #ifdef SQLITE_OMIT_CAST
|
| 215 | iOmit |= ((sqlite_int64)1<<11);
|
| 216 | #endif
|
| 217 | #ifdef SQLITE_OMIT_CHECK
|
| 218 | iOmit |= ((sqlite_int64)1<<12);
|
| 219 | #endif
|
| 220 | #ifdef SQLITE_OMIT_COMPLETE
|
| 221 | iOmit |= ((sqlite_int64)1<<13);
|
| 222 | #endif
|
| 223 | #ifdef SQLITE_OMIT_COMPOUND_SELECT
|
| 224 | iOmit |= ((sqlite_int64)1<<14);
|
| 225 | #endif
|
| 226 | #ifdef SQLITE_OMIT_DATETIME_FUNCS
|
| 227 | iOmit |= ((sqlite_int64)1<<15);
|
| 228 | #endif
|
| 229 | #ifdef SQLITE_OMIT_DECLTYPE
|
| 230 | iOmit |= ((sqlite_int64)1<<16);
|
| 231 | #endif
|
| 232 | #ifdef SQLITE_OMIT_DEPRECATED
|
| 233 | iOmit |= ((sqlite_int64)1<<17);
|
| 234 | #endif
|
| 235 | #ifdef SQLITE_OMIT_DISKIO
|
| 236 | iOmit |= ((sqlite_int64)1<<18);
|
| 237 | #endif
|
| 238 | #ifdef SQLITE_OMIT_EXPLAIN
|
| 239 | iOmit |= ((sqlite_int64)1<<19);
|
| 240 | #endif
|
| 241 | #ifdef SQLITE_OMIT_FLAG_PRAGMAS
|
| 242 | iOmit |= ((sqlite_int64)1<<20);
|
| 243 | #endif
|
| 244 | #ifdef SQLITE_OMIT_FLOATING_POINT
|
| 245 | iOmit |= ((sqlite_int64)1<<21);
|
| 246 | #endif
|
| 247 | #ifdef SQLITE_OMIT_FOREIGN_KEY
|
| 248 | iOmit |= ((sqlite_int64)1<<22);
|
| 249 | #endif
|
| 250 | #ifdef SQLITE_OMIT_GET_TABLE
|
| 251 | iOmit |= ((sqlite_int64)1<<23);
|
| 252 | #endif
|
| 253 | #ifdef SQLITE_OMIT_GLOBALRECOVER
|
| 254 | iOmit |= ((sqlite_int64)1<<24);
|
| 255 | #endif
|
| 256 | #ifdef SQLITE_OMIT_INCRBLOB
|
| 257 | iOmit |= ((sqlite_int64)1<<25);
|
| 258 | #endif
|
| 259 | #ifdef SQLITE_OMIT_INTEGRITY_CHECK
|
| 260 | iOmit |= ((sqlite_int64)1<<26);
|
| 261 | #endif
|
| 262 | #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
|
| 263 | iOmit |= ((sqlite_int64)1<<27);
|
| 264 | #endif
|
| 265 | #ifdef SQLITE_OMIT_LOAD_EXTENSION
|
| 266 | iOmit |= ((sqlite_int64)1<<28);
|
| 267 | #endif
|
| 268 | #ifdef SQLITE_OMIT_LOCALTIME
|
| 269 | iOmit |= ((sqlite_int64)1<<29);
|
| 270 | #endif
|
| 271 | #ifdef SQLITE_OMIT_LOOKASIDE
|
| 272 | iOmit |= ((sqlite_int64)1<<30);
|
| 273 | #endif
|
| 274 | #ifdef SQLITE_OMIT_MEMORYDB
|
| 275 | iOmit |= ((sqlite_int64)1<<31);
|
| 276 | #endif
|
| 277 | #ifdef SQLITE_OMIT_OR_OPTIMIZATION
|
| 278 | iOmit |= ((sqlite_int64)1<<32);
|
| 279 | #endif
|
| 280 | #ifdef SQLITE_OMIT_PAGER_PRAGMAS
|
| 281 | iOmit |= ((sqlite_int64)1<<33);
|
| 282 | #endif
|
| 283 | #ifdef SQLITE_OMIT_PRAGMA
|
| 284 | iOmit |= ((sqlite_int64)1<<34);
|
| 285 | #endif
|
| 286 | #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
|
| 287 | iOmit |= ((sqlite_int64)1<<35);
|
| 288 | #endif
|
| 289 | #ifdef SQLITE_OMIT_QUICKBALANCE
|
| 290 | iOmit |= ((sqlite_int64)1<<36);
|
| 291 | #endif
|
| 292 | #ifdef SQLITE_OMIT_REINDEX
|
| 293 | iOmit |= ((sqlite_int64)1<<37);
|
| 294 | #endif
|
| 295 | #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
|
| 296 | iOmit |= ((sqlite_int64)1<<38);
|
| 297 | #endif
|
| 298 | #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
|
| 299 | iOmit |= ((sqlite_int64)1<<39);
|
| 300 | #endif
|
| 301 | #ifdef SQLITE_OMIT_SHARED_CACHE
|
| 302 | iOmit |= ((sqlite_int64)1<<40);
|
| 303 | #endif
|
| 304 | #ifdef SQLITE_OMIT_SUBQUERY
|
| 305 | iOmit |= ((sqlite_int64)1<<41);
|
| 306 | #endif
|
| 307 | #ifdef SQLITE_OMIT_TCL_VARIABLE
|
| 308 | iOmit |= ((sqlite_int64)1<<42);
|
| 309 | #endif
|
| 310 | #ifdef SQLITE_OMIT_TEMPDB
|
| 311 | iOmit |= ((sqlite_int64)1<<43);
|
| 312 | #endif
|
| 313 | #ifdef SQLITE_OMIT_TRACE
|
| 314 | iOmit |= ((sqlite_int64)1<<44);
|
| 315 | #endif
|
| 316 | #ifdef SQLITE_OMIT_TRIGGER
|
| 317 | iOmit |= ((sqlite_int64)1<<45);
|
| 318 | #endif
|
| 319 | #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
|
| 320 | iOmit |= ((sqlite_int64)1<<46);
|
| 321 | #endif
|
| 322 | #ifdef SQLITE_OMIT_UTF16
|
| 323 | iOmit |= ((sqlite_int64)1<<47);
|
| 324 | #endif
|
| 325 | #ifdef SQLITE_OMIT_VACUUM
|
| 326 | iOmit |= ((sqlite_int64)1<<48);
|
| 327 | #endif
|
| 328 | #ifdef SQLITE_OMIT_VIEW
|
| 329 | iOmit |= ((sqlite_int64)1<<49);
|
| 330 | #endif
|
| 331 | #ifdef SQLITE_OMIT_VIRTUALTABLE
|
| 332 | iOmit |= ((sqlite_int64)1<<50);
|
| 333 | #endif
|
| 334 | #ifdef SQLITE_OMIT_WSD
|
| 335 | iOmit |= ((sqlite_int64)1<<51);
|
| 336 | #endif
|
| 337 | #ifdef SQLITE_OMIT_XFER_OPT
|
| 338 | iOmit |= ((sqlite_int64)1<<52);
|
| 339 | #endif
|
| 340 | #ifdef SQLITE_PERFORMANCE_TRACE
|
| 341 | iOther |= ((sqlite_int64)1<<24);
|
| 342 | #endif
|
| 343 | #ifdef SQLITE_PROXY_DEBUG
|
| 344 | iOther |= ((sqlite_int64)1<<25);
|
| 345 | #endif
|
| 346 | #ifdef SQLITE_SECURE_DELETE
|
| 347 | iOther |= ((sqlite_int64)1<<26);
|
| 348 | #endif
|
| 349 | #ifdef SQLITE_SMALL_STACK
|
| 350 | iOther |= ((sqlite_int64)1<<27);
|
| 351 | #endif
|
| 352 | #ifdef SQLITE_SOUNDEX
|
| 353 | iOther |= ((sqlite_int64)1<<28);
|
| 354 | #endif
|
| 355 | #ifdef SQLITE_TCL
|
| 356 | iOther |= ((sqlite_int64)1<<29);
|
| 357 | #endif
|
| 358 | #ifdef SQLITE_TEST
|
| 359 | iOther |= ((sqlite_int64)1<<30);
|
| 360 | #endif
|
| 361 | #ifdef SQLITE_USE_ALLOCA
|
| 362 | iOther |= ((sqlite_int64)1<<31);
|
| 363 | #endif
|
| 364 | #ifdef SQLITE_ZERO_MALLOC
|
| 365 | iOther |= ((sqlite_int64)1<<32);
|
| 366 | #endif
|
shaneh | bdea6d1 | 2010-02-23 04:19:54 +0000 | [diff] [blame] | 367 | |
| 368 | sqlite3_snprintf(sizeof(zOpts)-1, zOpts, |
| 369 | "%016llx%016llx%016llx", iEnable, iOmit, iOther); |
| 370 | return zOpts; |
| 371 | } |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 372 | |
drh | e265b08 | 2008-05-01 17:03:49 +0000 | [diff] [blame] | 373 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 374 | /* |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 375 | ** If the following function pointer is not NULL and if |
| 376 | ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing |
| 377 | ** I/O active are written using this function. These messages |
| 378 | ** are intended for debugging activity only. |
| 379 | */ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 380 | void (*sqlite3IoTrace)(const char*, ...) = 0; |
drh | e265b08 | 2008-05-01 17:03:49 +0000 | [diff] [blame] | 381 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 382 | |
| 383 | /* |
drh | a16313e | 2007-03-30 11:29:32 +0000 | [diff] [blame] | 384 | ** If the following global variable points to a string which is the |
| 385 | ** name of a directory, then that directory will be used to store |
| 386 | ** temporary files. |
| 387 | ** |
| 388 | ** See also the "PRAGMA temp_store_directory" SQL command. |
| 389 | */ |
| 390 | char *sqlite3_temp_directory = 0; |
| 391 | |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 392 | /* |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 393 | ** Initialize SQLite. |
| 394 | ** |
| 395 | ** This routine must be called to initialize the memory allocation, |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 396 | ** VFS, and mutex subsystems prior to doing any serious work with |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 397 | ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT |
| 398 | ** this routine will be called automatically by key routines such as |
| 399 | ** sqlite3_open(). |
| 400 | ** |
| 401 | ** This routine is a no-op except on its very first call for the process, |
| 402 | ** or for the first call after a call to sqlite3_shutdown. |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 403 | ** |
| 404 | ** The first thread to call this routine runs the initialization to |
| 405 | ** completion. If subsequent threads call this routine before the first |
| 406 | ** thread has finished the initialization process, then the subsequent |
| 407 | ** threads must block until the first thread finishes with the initialization. |
| 408 | ** |
| 409 | ** The first thread might call this routine recursively. Recursive |
| 410 | ** calls to this routine should not block, of course. Otherwise the |
| 411 | ** initialization process would never complete. |
| 412 | ** |
| 413 | ** Let X be the first thread to enter this routine. Let Y be some other |
| 414 | ** thread. Then while the initial invocation of this routine by X is |
| 415 | ** incomplete, it is required that: |
| 416 | ** |
| 417 | ** * Calls to this routine from Y must block until the outer-most |
| 418 | ** call by X completes. |
| 419 | ** |
| 420 | ** * Recursive calls to this routine from thread X return immediately |
| 421 | ** without blocking. |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 422 | */ |
| 423 | int sqlite3_initialize(void){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 424 | sqlite3_mutex *pMaster; /* The main static mutex */ |
| 425 | int rc; /* Result code */ |
| 426 | |
| 427 | #ifdef SQLITE_OMIT_WSD |
danielk1977 | a8f83bf | 2008-09-02 16:22:28 +0000 | [diff] [blame] | 428 | rc = sqlite3_wsd_init(4096, 24); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 429 | if( rc!=SQLITE_OK ){ |
| 430 | return rc; |
| 431 | } |
| 432 | #endif |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 433 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 434 | /* If SQLite is already completely initialized, then this call |
| 435 | ** to sqlite3_initialize() should be a no-op. But the initialization |
| 436 | ** must be complete. So isInit must not be set until the very end |
| 437 | ** of this routine. |
| 438 | */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 439 | if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 440 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 441 | /* Make sure the mutex subsystem is initialized. If unable to |
| 442 | ** initialize the mutex subsystem, return early with the error. |
| 443 | ** If the system is so sick that we are unable to allocate a mutex, |
| 444 | ** there is not much SQLite is going to be able to do. |
| 445 | ** |
| 446 | ** The mutex subsystem must take care of serializing its own |
| 447 | ** initialization. |
| 448 | */ |
danielk1977 | d025174 | 2008-06-18 18:57:42 +0000 | [diff] [blame] | 449 | rc = sqlite3MutexInit(); |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 450 | if( rc ) return rc; |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 451 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 452 | /* Initialize the malloc() system and the recursive pInitMutex mutex. |
| 453 | ** This operation is protected by the STATIC_MASTER mutex. Note that |
| 454 | ** MutexAlloc() is called for a static mutex prior to initializing the |
| 455 | ** malloc subsystem - this implies that the allocation of a static |
| 456 | ** mutex must not require support from the malloc subsystem. |
| 457 | */ |
| 458 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 459 | sqlite3_mutex_enter(pMaster); |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 460 | sqlite3GlobalConfig.isMutexInit = 1; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 461 | if( !sqlite3GlobalConfig.isMallocInit ){ |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 462 | rc = sqlite3MallocInit(); |
| 463 | } |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 464 | if( rc==SQLITE_OK ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 465 | sqlite3GlobalConfig.isMallocInit = 1; |
| 466 | if( !sqlite3GlobalConfig.pInitMutex ){ |
drh | 9ac0650 | 2009-08-17 13:42:29 +0000 | [diff] [blame] | 467 | sqlite3GlobalConfig.pInitMutex = |
| 468 | sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 469 | if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 470 | rc = SQLITE_NOMEM; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
danielk1977 | 77eb5bb | 2008-09-22 17:22:19 +0000 | [diff] [blame] | 473 | } |
| 474 | if( rc==SQLITE_OK ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 475 | sqlite3GlobalConfig.nRefInitMutex++; |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 476 | } |
| 477 | sqlite3_mutex_leave(pMaster); |
danielk1977 | 71bc31c | 2008-06-26 08:29:34 +0000 | [diff] [blame] | 478 | |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 479 | /* If rc is not SQLITE_OK at this point, then either the malloc |
| 480 | ** subsystem could not be initialized or the system failed to allocate |
| 481 | ** the pInitMutex mutex. Return an error in either case. */ |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 482 | if( rc!=SQLITE_OK ){ |
| 483 | return rc; |
| 484 | } |
| 485 | |
| 486 | /* Do the rest of the initialization under the recursive mutex so |
| 487 | ** that we will be able to handle recursive calls into |
| 488 | ** sqlite3_initialize(). The recursive calls normally come through |
| 489 | ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other |
| 490 | ** recursive calls might also be possible. |
| 491 | */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 492 | sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 493 | if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 494 | FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 495 | sqlite3GlobalConfig.inProgress = 1; |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 496 | memset(pHash, 0, sizeof(sqlite3GlobalFunctions)); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 497 | sqlite3RegisterGlobalFunctions(); |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 498 | if( sqlite3GlobalConfig.isPCacheInit==0 ){ |
| 499 | rc = sqlite3PcacheInitialize(); |
| 500 | } |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 501 | if( rc==SQLITE_OK ){ |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 502 | sqlite3GlobalConfig.isPCacheInit = 1; |
dan | 3d6e060 | 2009-08-17 15:52:25 +0000 | [diff] [blame] | 503 | rc = sqlite3OsInit(); |
drh | 0bf9f7b | 2009-04-17 16:54:22 +0000 | [diff] [blame] | 504 | } |
| 505 | if( rc==SQLITE_OK ){ |
danielk1977 | 5b77529 | 2008-09-02 09:38:06 +0000 | [diff] [blame] | 506 | sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, |
| 507 | sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); |
drh | 0bf9f7b | 2009-04-17 16:54:22 +0000 | [diff] [blame] | 508 | sqlite3GlobalConfig.isInit = 1; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 509 | } |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 510 | sqlite3GlobalConfig.inProgress = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 511 | } |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 512 | sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 513 | |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 514 | /* Go back under the static mutex and clean up the recursive |
| 515 | ** mutex to prevent a resource leak. |
| 516 | */ |
| 517 | sqlite3_mutex_enter(pMaster); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 518 | sqlite3GlobalConfig.nRefInitMutex--; |
| 519 | if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ |
| 520 | assert( sqlite3GlobalConfig.nRefInitMutex==0 ); |
| 521 | sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); |
| 522 | sqlite3GlobalConfig.pInitMutex = 0; |
drh | 93ed56d | 2008-08-12 15:21:11 +0000 | [diff] [blame] | 523 | } |
| 524 | sqlite3_mutex_leave(pMaster); |
| 525 | |
| 526 | /* The following is just a sanity check to make sure SQLite has |
| 527 | ** been compiled correctly. It is important to run this code, but |
| 528 | ** we don't want to run it too often and soak up CPU cycles for no |
| 529 | ** reason. So we run it once during initialization. |
| 530 | */ |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 531 | #ifndef NDEBUG |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 532 | #ifndef SQLITE_OMIT_FLOATING_POINT |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 533 | /* This section of code's only "output" is via assert() statements. */ |
| 534 | if ( rc==SQLITE_OK ){ |
| 535 | u64 x = (((u64)1)<<63)-1; |
| 536 | double y; |
| 537 | assert(sizeof(x)==8); |
| 538 | assert(sizeof(x)==sizeof(y)); |
| 539 | memcpy(&y, &x, 8); |
| 540 | assert( sqlite3IsNaN(y) ); |
| 541 | } |
| 542 | #endif |
shane | fbd60f8 | 2009-02-04 03:59:25 +0000 | [diff] [blame] | 543 | #endif |
shane | dfb7b37 | 2008-07-22 05:13:30 +0000 | [diff] [blame] | 544 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 545 | return rc; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | ** Undo the effects of sqlite3_initialize(). Must not be called while |
| 550 | ** there are outstanding database connections or memory allocations or |
| 551 | ** while any part of SQLite is otherwise in use in any thread. This |
drh | d1a2440 | 2009-04-19 12:23:58 +0000 | [diff] [blame] | 552 | ** routine is not threadsafe. But it is safe to invoke this routine |
| 553 | ** on when SQLite is already shut down. If SQLite is already shut down |
| 554 | ** when this routine is invoked, then this routine is a harmless no-op. |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 555 | */ |
| 556 | int sqlite3_shutdown(void){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 557 | if( sqlite3GlobalConfig.isInit ){ |
drh | a764092 | 2009-04-21 12:02:56 +0000 | [diff] [blame] | 558 | sqlite3_os_end(); |
drh | d1a2440 | 2009-04-19 12:23:58 +0000 | [diff] [blame] | 559 | sqlite3_reset_auto_extension(); |
drh | d1a2440 | 2009-04-19 12:23:58 +0000 | [diff] [blame] | 560 | sqlite3GlobalConfig.isInit = 0; |
danielk1977 | fb43727 | 2008-07-08 12:02:35 +0000 | [diff] [blame] | 561 | } |
dan | e1ab219 | 2009-08-17 15:16:19 +0000 | [diff] [blame] | 562 | if( sqlite3GlobalConfig.isPCacheInit ){ |
| 563 | sqlite3PcacheShutdown(); |
| 564 | sqlite3GlobalConfig.isPCacheInit = 0; |
| 565 | } |
| 566 | if( sqlite3GlobalConfig.isMallocInit ){ |
| 567 | sqlite3MallocEnd(); |
| 568 | sqlite3GlobalConfig.isMallocInit = 0; |
| 569 | } |
| 570 | if( sqlite3GlobalConfig.isMutexInit ){ |
| 571 | sqlite3MutexEnd(); |
| 572 | sqlite3GlobalConfig.isMutexInit = 0; |
| 573 | } |
| 574 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 575 | return SQLITE_OK; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | ** This API allows applications to modify the global configuration of |
| 580 | ** the SQLite library at run-time. |
| 581 | ** |
| 582 | ** This routine should only be called when there are no outstanding |
| 583 | ** database connections or memory allocations. This routine is not |
| 584 | ** threadsafe. Failure to heed these warnings can lead to unpredictable |
| 585 | ** behavior. |
| 586 | */ |
| 587 | int sqlite3_config(int op, ...){ |
| 588 | va_list ap; |
| 589 | int rc = SQLITE_OK; |
| 590 | |
| 591 | /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while |
| 592 | ** the SQLite library is in use. */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 593 | if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 594 | |
| 595 | va_start(ap, op); |
| 596 | switch( op ){ |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 597 | |
| 598 | /* Mutex configuration options are only available in a threadsafe |
| 599 | ** compile. |
| 600 | */ |
drh | d68eee0 | 2009-12-11 03:44:18 +0000 | [diff] [blame] | 601 | #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 602 | case SQLITE_CONFIG_SINGLETHREAD: { |
| 603 | /* Disable all mutexing */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 604 | sqlite3GlobalConfig.bCoreMutex = 0; |
| 605 | sqlite3GlobalConfig.bFullMutex = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 606 | break; |
| 607 | } |
| 608 | case SQLITE_CONFIG_MULTITHREAD: { |
| 609 | /* Disable mutexing of database connections */ |
| 610 | /* Enable mutexing of core data structures */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 611 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 612 | sqlite3GlobalConfig.bFullMutex = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | case SQLITE_CONFIG_SERIALIZED: { |
| 616 | /* Enable all mutexing */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 617 | sqlite3GlobalConfig.bCoreMutex = 1; |
| 618 | sqlite3GlobalConfig.bFullMutex = 1; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 619 | break; |
| 620 | } |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 621 | case SQLITE_CONFIG_MUTEX: { |
| 622 | /* Specify an alternative mutex implementation */ |
| 623 | sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 624 | break; |
| 625 | } |
| 626 | case SQLITE_CONFIG_GETMUTEX: { |
| 627 | /* Retrieve the current mutex implementation */ |
| 628 | *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 629 | break; |
| 630 | } |
| 631 | #endif |
| 632 | |
| 633 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 634 | case SQLITE_CONFIG_MALLOC: { |
| 635 | /* Specify an alternative malloc implementation */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 636 | sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 637 | break; |
| 638 | } |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 639 | case SQLITE_CONFIG_GETMALLOC: { |
danielk1977 | 57e5ea9 | 2008-06-24 19:02:55 +0000 | [diff] [blame] | 640 | /* Retrieve the current malloc() implementation */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 641 | if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 642 | *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 643 | break; |
| 644 | } |
drh | fec00ea | 2008-06-14 16:56:21 +0000 | [diff] [blame] | 645 | case SQLITE_CONFIG_MEMSTATUS: { |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 646 | /* Enable or disable the malloc status collection */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 647 | sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 648 | break; |
| 649 | } |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 650 | case SQLITE_CONFIG_SCRATCH: { |
| 651 | /* Designate a buffer for scratch memory space */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 652 | sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 653 | sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 654 | sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | case SQLITE_CONFIG_PAGECACHE: { |
drh | b093719 | 2009-05-22 10:53:29 +0000 | [diff] [blame] | 658 | /* Designate a buffer for page cache memory space */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 659 | sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 660 | sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 661 | sqlite3GlobalConfig.nPage = va_arg(ap, int); |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 664 | |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 665 | case SQLITE_CONFIG_PCACHE: { |
drh | b093719 | 2009-05-22 10:53:29 +0000 | [diff] [blame] | 666 | /* Specify an alternative page cache implementation */ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 667 | sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*); |
| 668 | break; |
| 669 | } |
| 670 | |
drh | b232c23 | 2008-11-19 01:20:26 +0000 | [diff] [blame] | 671 | case SQLITE_CONFIG_GETPCACHE: { |
| 672 | if( sqlite3GlobalConfig.pcache.xInit==0 ){ |
| 673 | sqlite3PCacheSetDefault(); |
| 674 | } |
| 675 | *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache; |
| 676 | break; |
| 677 | } |
| 678 | |
drh | 5d41483 | 2008-07-15 14:47:18 +0000 | [diff] [blame] | 679 | #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
drh | 3358979 | 2008-06-18 13:27:46 +0000 | [diff] [blame] | 680 | case SQLITE_CONFIG_HEAP: { |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 681 | /* Designate a buffer for heap memory space */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 682 | sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 683 | sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 684 | sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 685 | |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 686 | if( sqlite3GlobalConfig.pHeap==0 ){ |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 687 | /* If the heap pointer is NULL, then restore the malloc implementation |
| 688 | ** back to NULL pointers too. This will cause the malloc to go |
| 689 | ** back to its default implementation when sqlite3_initialize() is |
| 690 | ** run. |
| 691 | */ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 692 | memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 693 | }else{ |
| 694 | /* The heap pointer is not NULL, then install one of the |
| 695 | ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor |
| 696 | ** ENABLE_MEMSYS5 is defined, return an error. |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 697 | */ |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 698 | #ifdef SQLITE_ENABLE_MEMSYS3 |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 699 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 700 | #endif |
| 701 | #ifdef SQLITE_ENABLE_MEMSYS5 |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 702 | sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 703 | #endif |
drh | 8a42cbd | 2008-07-10 18:13:42 +0000 | [diff] [blame] | 704 | } |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 705 | break; |
| 706 | } |
drh | 5d41483 | 2008-07-15 14:47:18 +0000 | [diff] [blame] | 707 | #endif |
danielk1977 | 5099be5 | 2008-06-27 13:27:03 +0000 | [diff] [blame] | 708 | |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 709 | case SQLITE_CONFIG_LOOKASIDE: { |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 710 | sqlite3GlobalConfig.szLookaside = va_arg(ap, int); |
| 711 | sqlite3GlobalConfig.nLookaside = va_arg(ap, int); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 712 | break; |
| 713 | } |
drh | 3f28070 | 2010-02-18 18:45:09 +0000 | [diff] [blame] | 714 | |
| 715 | /* Record a pointer to the logger funcction and its first argument. |
| 716 | ** The default is NULL. Logging is disabled if the function pointer is |
| 717 | ** NULL. |
| 718 | */ |
| 719 | case SQLITE_CONFIG_LOG: { |
| 720 | sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); |
| 721 | sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); |
| 722 | break; |
| 723 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 724 | |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 725 | default: { |
| 726 | rc = SQLITE_ERROR; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | va_end(ap); |
| 731 | return rc; |
| 732 | } |
| 733 | |
| 734 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 735 | ** Set up the lookaside buffers for a database connection. |
| 736 | ** Return SQLITE_OK on success. |
| 737 | ** If lookaside is already active, return SQLITE_BUSY. |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 738 | ** |
| 739 | ** The sz parameter is the number of bytes in each lookaside slot. |
| 740 | ** The cnt parameter is the number of slots. If pStart is NULL the |
| 741 | ** space for the lookaside memory is obtained from sqlite3_malloc(). |
| 742 | ** If pStart is not NULL then it is sz*cnt bytes of memory to use for |
| 743 | ** the lookaside memory. |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 744 | */ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 745 | static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 746 | void *pStart; |
| 747 | if( db->lookaside.nOut ){ |
| 748 | return SQLITE_BUSY; |
| 749 | } |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 750 | /* Free any existing lookaside buffer for this handle before |
| 751 | ** allocating a new one so we don't have to have space for |
| 752 | ** both at the same time. |
| 753 | */ |
| 754 | if( db->lookaside.bMalloced ){ |
| 755 | sqlite3_free(db->lookaside.pStart); |
| 756 | } |
| 757 | /* The size of a lookaside slot needs to be larger than a pointer |
| 758 | ** to be useful. |
| 759 | */ |
drh | 1d34fde | 2009-02-03 15:50:33 +0000 | [diff] [blame] | 760 | if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 761 | if( cnt<0 ) cnt = 0; |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 762 | if( sz==0 || cnt==0 ){ |
| 763 | sz = 0; |
| 764 | pStart = 0; |
| 765 | }else if( pBuf==0 ){ |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 766 | sz = ROUND8(sz); |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 767 | sqlite3BeginBenignMalloc(); |
| 768 | pStart = sqlite3Malloc( sz*cnt ); |
| 769 | sqlite3EndBenignMalloc(); |
| 770 | }else{ |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 771 | sz = ROUNDDOWN8(sz); |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 772 | pStart = pBuf; |
| 773 | } |
drh | 4cfb22f | 2008-08-01 18:47:01 +0000 | [diff] [blame] | 774 | db->lookaside.pStart = pStart; |
| 775 | db->lookaside.pFree = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 776 | db->lookaside.sz = (u16)sz; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 777 | if( pStart ){ |
| 778 | int i; |
| 779 | LookasideSlot *p; |
drh | de46798 | 2009-04-02 17:22:41 +0000 | [diff] [blame] | 780 | assert( sz > (int)sizeof(LookasideSlot*) ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 781 | p = (LookasideSlot*)pStart; |
| 782 | for(i=cnt-1; i>=0; i--){ |
| 783 | p->pNext = db->lookaside.pFree; |
| 784 | db->lookaside.pFree = p; |
| 785 | p = (LookasideSlot*)&((u8*)p)[sz]; |
| 786 | } |
| 787 | db->lookaside.pEnd = p; |
| 788 | db->lookaside.bEnabled = 1; |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 789 | db->lookaside.bMalloced = pBuf==0 ?1:0; |
drh | 4cfb22f | 2008-08-01 18:47:01 +0000 | [diff] [blame] | 790 | }else{ |
| 791 | db->lookaside.pEnd = 0; |
| 792 | db->lookaside.bEnabled = 0; |
shane | f71f89e | 2009-01-30 06:11:54 +0000 | [diff] [blame] | 793 | db->lookaside.bMalloced = 0; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 794 | } |
| 795 | return SQLITE_OK; |
| 796 | } |
| 797 | |
| 798 | /* |
drh | 4413d0e | 2008-11-04 13:46:27 +0000 | [diff] [blame] | 799 | ** Return the mutex associated with a database connection. |
| 800 | */ |
| 801 | sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 802 | return db->mutex; |
| 803 | } |
| 804 | |
| 805 | /* |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 806 | ** Configuration settings for an individual database connection |
| 807 | */ |
| 808 | int sqlite3_db_config(sqlite3 *db, int op, ...){ |
| 809 | va_list ap; |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 810 | int rc; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 811 | va_start(ap, op); |
| 812 | switch( op ){ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 813 | case SQLITE_DBCONFIG_LOOKASIDE: { |
| 814 | void *pBuf = va_arg(ap, void*); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 815 | int sz = va_arg(ap, int); |
| 816 | int cnt = va_arg(ap, int); |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 817 | rc = setupLookaside(db, pBuf, sz, cnt); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 818 | break; |
| 819 | } |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 820 | default: { |
| 821 | rc = SQLITE_ERROR; |
| 822 | break; |
| 823 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 824 | } |
| 825 | va_end(ap); |
| 826 | return rc; |
| 827 | } |
| 828 | |
drh | a16313e | 2007-03-30 11:29:32 +0000 | [diff] [blame] | 829 | |
| 830 | /* |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 831 | ** Return true if the buffer z[0..n-1] contains all spaces. |
| 832 | */ |
| 833 | static int allSpaces(const char *z, int n){ |
drh | 5f3a367 | 2008-04-15 04:02:40 +0000 | [diff] [blame] | 834 | while( n>0 && z[n-1]==' ' ){ n--; } |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 835 | return n==0; |
| 836 | } |
| 837 | |
| 838 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 839 | ** This is the default collating function named "BINARY" which is always |
| 840 | ** available. |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 841 | ** |
| 842 | ** If the padFlag argument is not NULL then space padding at the end |
| 843 | ** of strings is ignored. This implements the RTRIM collation. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 844 | */ |
danielk1977 | 2c33654 | 2005-01-13 02:14:23 +0000 | [diff] [blame] | 845 | static int binCollFunc( |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 846 | void *padFlag, |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 847 | int nKey1, const void *pKey1, |
| 848 | int nKey2, const void *pKey2 |
| 849 | ){ |
| 850 | int rc, n; |
| 851 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 852 | rc = memcmp(pKey1, pKey2, n); |
| 853 | if( rc==0 ){ |
drh | 9b5adfa | 2008-01-20 23:19:56 +0000 | [diff] [blame] | 854 | if( padFlag |
| 855 | && allSpaces(((char*)pKey1)+n, nKey1-n) |
| 856 | && allSpaces(((char*)pKey2)+n, nKey2-n) |
| 857 | ){ |
| 858 | /* Leave rc unchanged at 0 */ |
| 859 | }else{ |
| 860 | rc = nKey1 - nKey2; |
| 861 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 862 | } |
| 863 | return rc; |
| 864 | } |
| 865 | |
| 866 | /* |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 867 | ** Another built-in collating sequence: NOCASE. |
| 868 | ** |
| 869 | ** This collating sequence is intended to be used for "case independant |
| 870 | ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 871 | ** extends only to the 26 characters used in the English language. |
| 872 | ** |
| 873 | ** At the moment there is only a UTF-8 implementation. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 874 | */ |
| 875 | static int nocaseCollatingFunc( |
| 876 | void *NotUsed, |
| 877 | int nKey1, const void *pKey1, |
| 878 | int nKey2, const void *pKey2 |
| 879 | ){ |
| 880 | int r = sqlite3StrNICmp( |
drh | 208f80a | 2004-08-29 20:08:58 +0000 | [diff] [blame] | 881 | (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 882 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 883 | if( 0==r ){ |
| 884 | r = nKey1-nKey2; |
| 885 | } |
| 886 | return r; |
| 887 | } |
| 888 | |
| 889 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 890 | ** Return the ROWID of the most recent insert |
| 891 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 892 | sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 893 | return db->lastRowid; |
| 894 | } |
| 895 | |
| 896 | /* |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 897 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 898 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 899 | int sqlite3_changes(sqlite3 *db){ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 900 | return db->nChange; |
| 901 | } |
| 902 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 903 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 904 | ** Return the number of changes since the database handle was opened. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 905 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 906 | int sqlite3_total_changes(sqlite3 *db){ |
| 907 | return db->nTotalChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 908 | } |
| 909 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 910 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 911 | ** Close all open savepoints. This function only manipulates fields of the |
| 912 | ** database handle object, it does not close any savepoints that may be open |
| 913 | ** at the b-tree/pager level. |
| 914 | */ |
| 915 | void sqlite3CloseSavepoints(sqlite3 *db){ |
| 916 | while( db->pSavepoint ){ |
| 917 | Savepoint *pTmp = db->pSavepoint; |
| 918 | db->pSavepoint = pTmp->pNext; |
| 919 | sqlite3DbFree(db, pTmp); |
| 920 | } |
| 921 | db->nSavepoint = 0; |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 922 | db->nStatement = 0; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 923 | db->isTransactionSavepoint = 0; |
| 924 | } |
| 925 | |
| 926 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 927 | ** Close an existing SQLite database |
| 928 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 929 | int sqlite3_close(sqlite3 *db){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 930 | HashElem *i; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 931 | int j; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 932 | |
| 933 | if( !db ){ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 934 | return SQLITE_OK; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 935 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 936 | if( !sqlite3SafetyCheckSickOrOk(db) ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 937 | return SQLITE_MISUSE; |
| 938 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 939 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 940 | |
drh | ffc13f6 | 2006-07-30 20:50:44 +0000 | [diff] [blame] | 941 | sqlite3ResetInternalSchema(db, 0); |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 942 | |
danielk1977 | 0125683 | 2007-04-18 14:24:32 +0000 | [diff] [blame] | 943 | /* If a transaction is open, the ResetInternalSchema() call above |
| 944 | ** will not have called the xDisconnect() method on any virtual |
| 945 | ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() |
| 946 | ** call will do so. We need to do this before the check for active |
| 947 | ** SQL statements below, as the v-table implementation may be storing |
| 948 | ** some prepared statements internally. |
| 949 | */ |
| 950 | sqlite3VtabRollback(db); |
| 951 | |
danielk1977 | a04a34f | 2007-04-16 15:06:25 +0000 | [diff] [blame] | 952 | /* If there are any outstanding VMs, return SQLITE_BUSY. */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 953 | if( db->pVdbe ){ |
| 954 | sqlite3Error(db, SQLITE_BUSY, |
drh | b309bec | 2009-02-04 17:40:57 +0000 | [diff] [blame] | 955 | "unable to close due to unfinalised statements"); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 956 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 957 | return SQLITE_BUSY; |
| 958 | } |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 959 | assert( sqlite3SafetyCheckSickOrOk(db) ); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 960 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 961 | for(j=0; j<db->nDb; j++){ |
| 962 | Btree *pBt = db->aDb[j].pBt; |
| 963 | if( pBt && sqlite3BtreeIsInBackup(pBt) ){ |
| 964 | sqlite3Error(db, SQLITE_BUSY, |
drh | b309bec | 2009-02-04 17:40:57 +0000 | [diff] [blame] | 965 | "unable to close due to unfinished backup operation"); |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 966 | sqlite3_mutex_leave(db->mutex); |
| 967 | return SQLITE_BUSY; |
| 968 | } |
| 969 | } |
| 970 | |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 971 | /* Free any outstanding Savepoint structures. */ |
| 972 | sqlite3CloseSavepoints(db); |
| 973 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 974 | for(j=0; j<db->nDb; j++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 975 | struct Db *pDb = &db->aDb[j]; |
| 976 | if( pDb->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 977 | sqlite3BtreeClose(pDb->pBt); |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 978 | pDb->pBt = 0; |
danielk1977 | 311019b | 2006-01-10 07:14:23 +0000 | [diff] [blame] | 979 | if( j!=1 ){ |
| 980 | pDb->pSchema = 0; |
| 981 | } |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 982 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 983 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 984 | sqlite3ResetInternalSchema(db, 0); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 985 | |
| 986 | /* Tell the code in notify.c that the connection no longer holds any |
| 987 | ** locks and does not require any further unlock-notify callbacks. |
| 988 | */ |
| 989 | sqlite3ConnectionClosed(db); |
| 990 | |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 991 | assert( db->nDb<=2 ); |
| 992 | assert( db->aDb==db->aDbStatic ); |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 993 | for(j=0; j<ArraySize(db->aFunc.a); j++){ |
| 994 | FuncDef *pNext, *pHash, *p; |
| 995 | for(p=db->aFunc.a[j]; p; p=pHash){ |
| 996 | pHash = p->pHash; |
| 997 | while( p ){ |
| 998 | pNext = p->pNext; |
| 999 | sqlite3DbFree(db, p); |
| 1000 | p = pNext; |
| 1001 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1004 | for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1005 | CollSeq *pColl = (CollSeq *)sqliteHashData(i); |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1006 | /* Invoke any destructors registered for collation sequence user data. */ |
| 1007 | for(j=0; j<3; j++){ |
| 1008 | if( pColl[j].xDel ){ |
| 1009 | pColl[j].xDel(pColl[j].pUser); |
| 1010 | } |
| 1011 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1012 | sqlite3DbFree(db, pColl); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1013 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1014 | sqlite3HashClear(&db->aCollSeq); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1015 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 1016 | for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ |
| 1017 | Module *pMod = (Module *)sqliteHashData(i); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 1018 | if( pMod->xDestroy ){ |
| 1019 | pMod->xDestroy(pMod->pAux); |
| 1020 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1021 | sqlite3DbFree(db, pMod); |
danielk1977 | d1ab1ba | 2006-06-15 04:28:13 +0000 | [diff] [blame] | 1022 | } |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1023 | sqlite3HashClear(&db->aModule); |
| 1024 | #endif |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1025 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1026 | sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1027 | if( db->pErr ){ |
| 1028 | sqlite3ValueFree(db->pErr); |
| 1029 | } |
drh | f1952c5 | 2006-06-08 15:48:00 +0000 | [diff] [blame] | 1030 | sqlite3CloseExtensions(db); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 1031 | |
| 1032 | db->magic = SQLITE_MAGIC_ERROR; |
danielk1977 | 311019b | 2006-01-10 07:14:23 +0000 | [diff] [blame] | 1033 | |
| 1034 | /* The temp-database schema is allocated differently from the other schema |
| 1035 | ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). |
| 1036 | ** So it needs to be freed here. Todo: Why not roll the temp schema into |
| 1037 | ** the same sqliteMalloc() as the one that allocates the database |
| 1038 | ** structure? |
| 1039 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1040 | sqlite3DbFree(db, db->aDb[1].pSchema); |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1041 | sqlite3_mutex_leave(db->mutex); |
drh | 7e8b848 | 2008-01-23 03:03:05 +0000 | [diff] [blame] | 1042 | db->magic = SQLITE_MAGIC_CLOSED; |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1043 | sqlite3_mutex_free(db->mutex); |
drh | 4150ebf | 2008-10-11 15:38:29 +0000 | [diff] [blame] | 1044 | assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 1045 | if( db->lookaside.bMalloced ){ |
| 1046 | sqlite3_free(db->lookaside.pStart); |
| 1047 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1048 | sqlite3_free(db); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 1049 | return SQLITE_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1053 | ** Rollback all database files. |
| 1054 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1055 | void sqlite3RollbackAll(sqlite3 *db){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1056 | int i; |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 1057 | int inTrans = 0; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1058 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1059 | sqlite3BeginBenignMalloc(); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1060 | for(i=0; i<db->nDb; i++){ |
| 1061 | if( db->aDb[i].pBt ){ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 1062 | if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){ |
| 1063 | inTrans = 1; |
| 1064 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1065 | sqlite3BtreeRollback(db->aDb[i].pBt); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1066 | db->aDb[i].inTrans = 0; |
| 1067 | } |
| 1068 | } |
danielk1977 | a298e90 | 2006-06-22 09:53:48 +0000 | [diff] [blame] | 1069 | sqlite3VtabRollback(db); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 1070 | sqlite3EndBenignMalloc(); |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 1071 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1072 | if( db->flags&SQLITE_InternChanges ){ |
danielk1977 | ea4d9e2 | 2007-08-13 15:28:33 +0000 | [diff] [blame] | 1073 | sqlite3ExpirePreparedStatements(db); |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1074 | sqlite3ResetInternalSchema(db, 0); |
| 1075 | } |
| 1076 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 1077 | /* Any deferred constraint violations have now been resolved. */ |
| 1078 | db->nDeferredCons = 0; |
| 1079 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1080 | /* If one has been configured, invoke the rollback-hook callback */ |
danielk1977 | f3f06bb | 2005-12-16 15:24:28 +0000 | [diff] [blame] | 1081 | if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1082 | db->xRollbackCallback(db->pRollbackArg); |
| 1083 | } |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 1087 | ** Return a static string that describes the kind of error specified in the |
| 1088 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 1089 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 1090 | const char *sqlite3ErrStr(int rc){ |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1091 | static const char* const aMsg[] = { |
| 1092 | /* SQLITE_OK */ "not an error", |
| 1093 | /* SQLITE_ERROR */ "SQL logic error or missing database", |
| 1094 | /* SQLITE_INTERNAL */ 0, |
| 1095 | /* SQLITE_PERM */ "access permission denied", |
| 1096 | /* SQLITE_ABORT */ "callback requested query abort", |
| 1097 | /* SQLITE_BUSY */ "database is locked", |
| 1098 | /* SQLITE_LOCKED */ "database table is locked", |
| 1099 | /* SQLITE_NOMEM */ "out of memory", |
| 1100 | /* SQLITE_READONLY */ "attempt to write a readonly database", |
| 1101 | /* SQLITE_INTERRUPT */ "interrupted", |
| 1102 | /* SQLITE_IOERR */ "disk I/O error", |
| 1103 | /* SQLITE_CORRUPT */ "database disk image is malformed", |
| 1104 | /* SQLITE_NOTFOUND */ 0, |
| 1105 | /* SQLITE_FULL */ "database or disk is full", |
| 1106 | /* SQLITE_CANTOPEN */ "unable to open database file", |
| 1107 | /* SQLITE_PROTOCOL */ 0, |
| 1108 | /* SQLITE_EMPTY */ "table contains no data", |
| 1109 | /* SQLITE_SCHEMA */ "database schema has changed", |
drh | 4c8555f | 2009-06-25 01:47:11 +0000 | [diff] [blame] | 1110 | /* SQLITE_TOOBIG */ "string or blob too big", |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1111 | /* SQLITE_CONSTRAINT */ "constraint failed", |
| 1112 | /* SQLITE_MISMATCH */ "datatype mismatch", |
| 1113 | /* SQLITE_MISUSE */ "library routine called out of sequence", |
| 1114 | /* SQLITE_NOLFS */ "large file support is disabled", |
| 1115 | /* SQLITE_AUTH */ "authorization denied", |
| 1116 | /* SQLITE_FORMAT */ "auxiliary database format error", |
| 1117 | /* SQLITE_RANGE */ "bind or column index out of range", |
| 1118 | /* SQLITE_NOTADB */ "file is encrypted or is not a database", |
| 1119 | }; |
| 1120 | rc &= 0xff; |
drh | b2f9efc | 2009-05-06 19:03:13 +0000 | [diff] [blame] | 1121 | if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){ |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1122 | return aMsg[rc]; |
| 1123 | }else{ |
| 1124 | return "unknown error"; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 1125 | } |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1129 | ** This routine implements a busy callback that sleeps and tries |
| 1130 | ** again until a timeout value is reached. The timeout value is |
| 1131 | ** an integer number of milliseconds passed in as the first |
| 1132 | ** argument. |
| 1133 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1134 | static int sqliteDefaultBusyCallback( |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 1135 | void *ptr, /* Database connection */ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1136 | int count /* Number of times table has been busy */ |
| 1137 | ){ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 1138 | #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP) |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 1139 | static const u8 delays[] = |
| 1140 | { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; |
| 1141 | static const u8 totals[] = |
| 1142 | { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 1143 | # define NDELAY (sizeof(delays)/sizeof(delays[0])) |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1144 | sqlite3 *db = (sqlite3 *)ptr; |
| 1145 | int timeout = db->busyTimeout; |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 1146 | int delay, prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1147 | |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 1148 | assert( count>=0 ); |
| 1149 | if( count < NDELAY ){ |
| 1150 | delay = delays[count]; |
| 1151 | prior = totals[count]; |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 1152 | }else{ |
| 1153 | delay = delays[NDELAY-1]; |
drh | 68cb619 | 2005-05-06 22:05:56 +0000 | [diff] [blame] | 1154 | prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1155 | } |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 1156 | if( prior + delay > timeout ){ |
| 1157 | delay = timeout - prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1158 | if( delay<=0 ) return 0; |
| 1159 | } |
danielk1977 | 2a6bdf6 | 2007-08-20 16:07:00 +0000 | [diff] [blame] | 1160 | sqlite3OsSleep(db->pVfs, delay*1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1161 | return 1; |
| 1162 | #else |
drh | 482ea18 | 2007-08-20 11:12:40 +0000 | [diff] [blame] | 1163 | sqlite3 *db = (sqlite3 *)ptr; |
drh | 3f73708 | 2005-06-14 02:24:31 +0000 | [diff] [blame] | 1164 | int timeout = ((sqlite3 *)ptr)->busyTimeout; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1165 | if( (count+1)*1000 > timeout ){ |
| 1166 | return 0; |
| 1167 | } |
drh | 482ea18 | 2007-08-20 11:12:40 +0000 | [diff] [blame] | 1168 | sqlite3OsSleep(db->pVfs, 1000000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1169 | return 1; |
| 1170 | #endif |
| 1171 | } |
| 1172 | |
| 1173 | /* |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1174 | ** Invoke the given busy handler. |
| 1175 | ** |
| 1176 | ** This routine is called when an operation failed with a lock. |
| 1177 | ** If this routine returns non-zero, the lock is retried. If it |
| 1178 | ** returns 0, the operation aborts with an SQLITE_BUSY error. |
| 1179 | */ |
| 1180 | int sqlite3InvokeBusyHandler(BusyHandler *p){ |
| 1181 | int rc; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1182 | if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1183 | rc = p->xFunc(p->pArg, p->nBusy); |
| 1184 | if( rc==0 ){ |
| 1185 | p->nBusy = -1; |
| 1186 | }else{ |
| 1187 | p->nBusy++; |
| 1188 | } |
| 1189 | return rc; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1193 | ** This routine sets the busy callback for an Sqlite database to the |
| 1194 | ** given callback function with the given argument. |
| 1195 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1196 | int sqlite3_busy_handler( |
| 1197 | sqlite3 *db, |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 1198 | int (*xBusy)(void*,int), |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1199 | void *pArg |
| 1200 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1201 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 1202 | db->busyHandler.xFunc = xBusy; |
| 1203 | db->busyHandler.pArg = pArg; |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1204 | db->busyHandler.nBusy = 0; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1205 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1206 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1209 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1210 | /* |
| 1211 | ** This routine sets the progress callback for an Sqlite database to the |
| 1212 | ** given callback function with the given argument. The progress callback will |
| 1213 | ** be invoked every nOps opcodes. |
| 1214 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1215 | void sqlite3_progress_handler( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1216 | sqlite3 *db, |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1217 | int nOps, |
| 1218 | int (*xProgress)(void*), |
| 1219 | void *pArg |
| 1220 | ){ |
drh | bd0b1b5 | 2008-07-07 19:52:09 +0000 | [diff] [blame] | 1221 | sqlite3_mutex_enter(db->mutex); |
| 1222 | if( nOps>0 ){ |
| 1223 | db->xProgress = xProgress; |
| 1224 | db->nProgressOps = nOps; |
| 1225 | db->pProgressArg = pArg; |
| 1226 | }else{ |
| 1227 | db->xProgress = 0; |
| 1228 | db->nProgressOps = 0; |
| 1229 | db->pProgressArg = 0; |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1230 | } |
drh | bd0b1b5 | 2008-07-07 19:52:09 +0000 | [diff] [blame] | 1231 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 1232 | } |
| 1233 | #endif |
| 1234 | |
| 1235 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1236 | /* |
| 1237 | ** This routine installs a default busy handler that waits for the |
| 1238 | ** specified number of milliseconds before returning 0. |
| 1239 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1240 | int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1241 | if( ms>0 ){ |
drh | 97903fe | 2005-05-24 20:19:57 +0000 | [diff] [blame] | 1242 | db->busyTimeout = ms; |
| 1243 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1244 | }else{ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1245 | sqlite3_busy_handler(db, 0, 0); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1246 | } |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1247 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1248 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1249 | |
| 1250 | /* |
| 1251 | ** Cause any pending operation to stop at its earliest opportunity. |
| 1252 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1253 | void sqlite3_interrupt(sqlite3 *db){ |
drh | bd0b1b5 | 2008-07-07 19:52:09 +0000 | [diff] [blame] | 1254 | db->u1.isInterrupted = 1; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1255 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1256 | |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1259 | ** This function is exactly the same as sqlite3_create_function(), except |
| 1260 | ** that it is designed to be called by internal code. The difference is |
| 1261 | ** that if a malloc() fails in sqlite3_create_function(), an error code |
| 1262 | ** is returned and the mallocFailed flag cleared. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 1263 | */ |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1264 | int sqlite3CreateFunc( |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1265 | sqlite3 *db, |
| 1266 | const char *zFunctionName, |
| 1267 | int nArg, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1268 | int enc, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1269 | void *pUserData, |
| 1270 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 1271 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 1272 | void (*xFinal)(sqlite3_context*) |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1273 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1274 | FuncDef *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1275 | int nName; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1276 | |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1277 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1278 | if( zFunctionName==0 || |
danielk1977 | 398eae7 | 2004-05-26 06:58:43 +0000 | [diff] [blame] | 1279 | (xFunc && (xFinal || xStep)) || |
| 1280 | (!xFunc && (xFinal && !xStep)) || |
| 1281 | (!xFunc && (!xFinal && xStep)) || |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 1282 | (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || |
drh | dee0e40 | 2009-05-03 20:23:53 +0000 | [diff] [blame] | 1283 | (255<(nName = sqlite3Strlen30( zFunctionName))) ){ |
drh | dff6c17 | 2009-05-07 13:43:49 +0000 | [diff] [blame] | 1284 | return SQLITE_MISUSE; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1285 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1286 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1287 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1288 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 1289 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 1290 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 1291 | ** |
| 1292 | ** If SQLITE_ANY is specified, add three versions of the function |
| 1293 | ** to the hash table. |
| 1294 | */ |
| 1295 | if( enc==SQLITE_UTF16 ){ |
| 1296 | enc = SQLITE_UTF16NATIVE; |
| 1297 | }else if( enc==SQLITE_ANY ){ |
| 1298 | int rc; |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1299 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 1300 | pUserData, xFunc, xStep, xFinal); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1301 | if( rc==SQLITE_OK ){ |
| 1302 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, |
| 1303 | pUserData, xFunc, xStep, xFinal); |
| 1304 | } |
| 1305 | if( rc!=SQLITE_OK ){ |
| 1306 | return rc; |
| 1307 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1308 | enc = SQLITE_UTF16BE; |
| 1309 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1310 | #else |
| 1311 | enc = SQLITE_UTF8; |
| 1312 | #endif |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1313 | |
| 1314 | /* Check if an existing function is being overridden or deleted. If so, |
| 1315 | ** and there are active VMs, then return SQLITE_BUSY. If a function |
| 1316 | ** is being overridden/deleted but there are no active VMs, allow the |
| 1317 | ** operation to continue but invalidate all precompiled statements. |
| 1318 | */ |
drh | 3abbd39 | 2008-12-10 23:04:13 +0000 | [diff] [blame] | 1319 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1320 | if( p && p->iPrefEnc==enc && p->nArg==nArg ){ |
| 1321 | if( db->activeVdbeCnt ){ |
| 1322 | sqlite3Error(db, SQLITE_BUSY, |
drh | b309bec | 2009-02-04 17:40:57 +0000 | [diff] [blame] | 1323 | "unable to delete/modify user-function due to active statements"); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1324 | assert( !db->mallocFailed ); |
danielk1977 | 9636c4e | 2005-01-25 04:27:54 +0000 | [diff] [blame] | 1325 | return SQLITE_BUSY; |
| 1326 | }else{ |
| 1327 | sqlite3ExpirePreparedStatements(db); |
| 1328 | } |
| 1329 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1330 | |
drh | 3abbd39 | 2008-12-10 23:04:13 +0000 | [diff] [blame] | 1331 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1332 | assert(p || db->mallocFailed); |
| 1333 | if( !p ){ |
| 1334 | return SQLITE_NOMEM; |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1335 | } |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 1336 | p->flags = 0; |
| 1337 | p->xFunc = xFunc; |
| 1338 | p->xStep = xStep; |
| 1339 | p->xFinalize = xFinal; |
| 1340 | p->pUserData = pUserData; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1341 | p->nArg = (u16)nArg; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1342 | return SQLITE_OK; |
| 1343 | } |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* |
| 1346 | ** Create new user functions. |
| 1347 | */ |
| 1348 | int sqlite3_create_function( |
| 1349 | sqlite3 *db, |
| 1350 | const char *zFunctionName, |
| 1351 | int nArg, |
| 1352 | int enc, |
| 1353 | void *p, |
| 1354 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 1355 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 1356 | void (*xFinal)(sqlite3_context*) |
| 1357 | ){ |
| 1358 | int rc; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1359 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1360 | rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1361 | rc = sqlite3ApiExit(db, rc); |
| 1362 | sqlite3_mutex_leave(db->mutex); |
| 1363 | return rc; |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1366 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1367 | int sqlite3_create_function16( |
| 1368 | sqlite3 *db, |
| 1369 | const void *zFunctionName, |
| 1370 | int nArg, |
| 1371 | int eTextRep, |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1372 | void *p, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 1373 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 1374 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 1375 | void (*xFinal)(sqlite3_context*) |
| 1376 | ){ |
| 1377 | int rc; |
drh | af9a7c2 | 2005-12-15 03:04:10 +0000 | [diff] [blame] | 1378 | char *zFunc8; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1379 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1380 | assert( !db->mallocFailed ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 1381 | zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1); |
danielk1977 | 771151b | 2006-01-17 13:21:40 +0000 | [diff] [blame] | 1382 | rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1383 | sqlite3DbFree(db, zFunc8); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1384 | rc = sqlite3ApiExit(db, rc); |
| 1385 | sqlite3_mutex_leave(db->mutex); |
| 1386 | return rc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1387 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1388 | #endif |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1389 | |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* |
| 1392 | ** Declare that a function has been overloaded by a virtual table. |
| 1393 | ** |
| 1394 | ** If the function already exists as a regular global function, then |
| 1395 | ** this routine is a no-op. If the function does not exist, then create |
| 1396 | ** a new one that always throws a run-time error. |
| 1397 | ** |
| 1398 | ** When virtual tables intend to provide an overloaded function, they |
| 1399 | ** should call this routine to make sure the global function exists. |
| 1400 | ** A global function must exist in order for name resolution to work |
| 1401 | ** properly. |
| 1402 | */ |
| 1403 | int sqlite3_overload_function( |
| 1404 | sqlite3 *db, |
| 1405 | const char *zName, |
| 1406 | int nArg |
| 1407 | ){ |
drh | dee0e40 | 2009-05-03 20:23:53 +0000 | [diff] [blame] | 1408 | int nName = sqlite3Strlen30(zName); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1409 | int rc; |
| 1410 | sqlite3_mutex_enter(db->mutex); |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 1411 | if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 1412 | sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 1413 | 0, sqlite3InvalidFunction, 0, 0); |
| 1414 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1415 | rc = sqlite3ApiExit(db, SQLITE_OK); |
| 1416 | sqlite3_mutex_leave(db->mutex); |
| 1417 | return rc; |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1420 | #ifndef SQLITE_OMIT_TRACE |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1421 | /* |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1422 | ** Register a trace function. The pArg from the previously registered trace |
| 1423 | ** is returned. |
| 1424 | ** |
| 1425 | ** A NULL trace function means that no tracing is executes. A non-NULL |
| 1426 | ** 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] | 1427 | ** SQL statement. |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1428 | */ |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1429 | void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1430 | void *pOld; |
| 1431 | sqlite3_mutex_enter(db->mutex); |
| 1432 | pOld = db->pTraceArg; |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1433 | db->xTrace = xTrace; |
| 1434 | db->pTraceArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1435 | sqlite3_mutex_leave(db->mutex); |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 1436 | return pOld; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 1437 | } |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1438 | /* |
| 1439 | ** Register a profile function. The pArg from the previously registered |
| 1440 | ** profile function is returned. |
| 1441 | ** |
| 1442 | ** A NULL profile function means that no profiling is executes. A non-NULL |
| 1443 | ** profile is a pointer to a function that is invoked at the conclusion of |
| 1444 | ** each SQL statement that is run. |
| 1445 | */ |
| 1446 | void *sqlite3_profile( |
| 1447 | sqlite3 *db, |
| 1448 | void (*xProfile)(void*,const char*,sqlite_uint64), |
| 1449 | void *pArg |
| 1450 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1451 | void *pOld; |
| 1452 | sqlite3_mutex_enter(db->mutex); |
| 1453 | pOld = db->pProfileArg; |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1454 | db->xProfile = xProfile; |
| 1455 | db->pProfileArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1456 | sqlite3_mutex_leave(db->mutex); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 1457 | return pOld; |
| 1458 | } |
| 1459 | #endif /* SQLITE_OMIT_TRACE */ |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1460 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1461 | /*** EXPERIMENTAL *** |
| 1462 | ** |
| 1463 | ** Register a function to be invoked when a transaction comments. |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1464 | ** If the invoked function returns non-zero, then the commit becomes a |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1465 | ** rollback. |
| 1466 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 1467 | void *sqlite3_commit_hook( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 1468 | sqlite3 *db, /* Attach the hook to this database */ |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1469 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 1470 | void *pArg /* Argument to the function */ |
| 1471 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1472 | void *pOld; |
| 1473 | sqlite3_mutex_enter(db->mutex); |
| 1474 | pOld = db->pCommitArg; |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1475 | db->xCommitCallback = xCallback; |
| 1476 | db->pCommitArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1477 | sqlite3_mutex_leave(db->mutex); |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1478 | return pOld; |
| 1479 | } |
| 1480 | |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1481 | /* |
| 1482 | ** Register a callback to be invoked each time a row is updated, |
| 1483 | ** inserted or deleted using this database connection. |
| 1484 | */ |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1485 | void *sqlite3_update_hook( |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1486 | sqlite3 *db, /* Attach the hook to this database */ |
| 1487 | void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 1488 | void *pArg /* Argument to the function */ |
| 1489 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1490 | void *pRet; |
| 1491 | sqlite3_mutex_enter(db->mutex); |
| 1492 | pRet = db->pUpdateArg; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1493 | db->xUpdateCallback = xCallback; |
| 1494 | db->pUpdateArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1495 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1496 | return pRet; |
danielk1977 | 94eb6a1 | 2005-12-15 15:22:08 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1499 | /* |
| 1500 | ** Register a callback to be invoked each time a transaction is rolled |
| 1501 | ** back by this database connection. |
| 1502 | */ |
| 1503 | void *sqlite3_rollback_hook( |
| 1504 | sqlite3 *db, /* Attach the hook to this database */ |
| 1505 | void (*xCallback)(void*), /* Callback function */ |
| 1506 | void *pArg /* Argument to the function */ |
| 1507 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1508 | void *pRet; |
| 1509 | sqlite3_mutex_enter(db->mutex); |
| 1510 | pRet = db->pRollbackArg; |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1511 | db->xRollbackCallback = xCallback; |
| 1512 | db->pRollbackArg = pArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1513 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 71fd80b | 2005-12-16 06:54:01 +0000 | [diff] [blame] | 1514 | return pRet; |
| 1515 | } |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 1516 | |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1517 | /* |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1518 | ** This function returns true if main-memory should be used instead of |
| 1519 | ** a temporary file for transient pager files and statement journals. |
| 1520 | ** The value returned depends on the value of db->temp_store (runtime |
| 1521 | ** parameter) and the compile time value of SQLITE_TEMP_STORE. The |
| 1522 | ** following table describes the relationship between these two values |
| 1523 | ** and this functions return value. |
| 1524 | ** |
| 1525 | ** SQLITE_TEMP_STORE db->temp_store Location of temporary database |
| 1526 | ** ----------------- -------------- ------------------------------ |
| 1527 | ** 0 any file (return 0) |
| 1528 | ** 1 1 file (return 0) |
| 1529 | ** 1 2 memory (return 1) |
| 1530 | ** 1 0 file (return 0) |
| 1531 | ** 2 1 file (return 0) |
| 1532 | ** 2 2 memory (return 1) |
| 1533 | ** 2 0 memory (return 1) |
| 1534 | ** 3 any memory (return 1) |
| 1535 | */ |
drh | 1c51414 | 2009-04-30 12:25:10 +0000 | [diff] [blame] | 1536 | int sqlite3TempInMemory(const sqlite3 *db){ |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1537 | #if SQLITE_TEMP_STORE==1 |
| 1538 | return ( db->temp_store==2 ); |
| 1539 | #endif |
| 1540 | #if SQLITE_TEMP_STORE==2 |
| 1541 | return ( db->temp_store!=1 ); |
| 1542 | #endif |
| 1543 | #if SQLITE_TEMP_STORE==3 |
| 1544 | return 1; |
shane | cf69739 | 2009-06-01 16:53:09 +0000 | [diff] [blame] | 1545 | #endif |
| 1546 | #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1547 | return 0; |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 1548 | #endif |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | /* |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 1552 | ** This routine is called to create a connection to a database BTree |
| 1553 | ** driver. If zFilename is the name of a file, then that file is |
| 1554 | ** opened and used. If zFilename is the magic name ":memory:" then |
| 1555 | ** the database is stored in memory (and is thus forgotten as soon as |
| 1556 | ** the connection is closed.) If zFilename is NULL then the database |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 1557 | ** is a "virtual" database for transient use only and is deleted as |
| 1558 | ** soon as the connection is closed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1559 | ** |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 1560 | ** A virtual database can be either a disk file (that is automatically |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1561 | ** deleted when the file is closed) or it an be held entirely in memory. |
| 1562 | ** The sqlite3TempInMemory() function is used to determine which. |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1563 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1564 | int sqlite3BtreeFactory( |
drh | 1860e3f | 2009-11-04 13:30:01 +0000 | [diff] [blame] | 1565 | sqlite3 *db, /* Main database when opening aux otherwise 0 */ |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1566 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 1567 | int omitJournal, /* if TRUE then do not journal this file */ |
| 1568 | int nCache, /* How many pages in the page cache */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1569 | int vfsFlags, /* Flags passed through to vfsOpen */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1570 | Btree **ppBtree /* Pointer to new Btree object written here */ |
| 1571 | ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1572 | int btFlags = 0; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1573 | int rc; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1574 | |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1575 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | eec983e | 2004-05-08 10:11:36 +0000 | [diff] [blame] | 1576 | assert( ppBtree != 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1577 | if( omitJournal ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1578 | btFlags |= BTREE_OMIT_JOURNAL; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1579 | } |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 1580 | if( db->flags & SQLITE_NoReadlock ){ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1581 | btFlags |= BTREE_NO_READLOCK; |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 1582 | } |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1583 | #ifndef SQLITE_OMIT_MEMORYDB |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1584 | if( zFilename==0 && sqlite3TempInMemory(db) ){ |
drh | 22ac46d | 2004-08-14 18:34:54 +0000 | [diff] [blame] | 1585 | zFilename = ":memory:"; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1586 | } |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 1587 | #endif |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1588 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1589 | if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){ |
| 1590 | vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; |
| 1591 | } |
| 1592 | rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 1593 | |
| 1594 | /* If the B-Tree was successfully opened, set the pager-cache size to the |
| 1595 | ** default value. Except, if the call to BtreeOpen() returned a handle |
| 1596 | ** open on an existing shared pager-cache, do not change the pager-cache |
| 1597 | ** size. |
| 1598 | */ |
| 1599 | if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1600 | sqlite3BtreeSetCacheSize(*ppBtree, nCache); |
| 1601 | } |
| 1602 | return rc; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 1603 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1604 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1605 | /* |
| 1606 | ** Return UTF-8 encoded English language explanation of the most recent |
| 1607 | ** error. |
| 1608 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1609 | const char *sqlite3_errmsg(sqlite3 *db){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1610 | const char *z; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1611 | if( !db ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 1612 | return sqlite3ErrStr(SQLITE_NOMEM); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1613 | } |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1614 | if( !sqlite3SafetyCheckSickOrOk(db) ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 1615 | return sqlite3ErrStr(SQLITE_MISUSE); |
| 1616 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1617 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 1618 | if( db->mallocFailed ){ |
| 1619 | z = sqlite3ErrStr(SQLITE_NOMEM); |
| 1620 | }else{ |
| 1621 | z = (char*)sqlite3_value_text(db->pErr); |
| 1622 | assert( !db->mallocFailed ); |
| 1623 | if( z==0 ){ |
| 1624 | z = sqlite3ErrStr(db->errCode); |
| 1625 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1626 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1627 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1628 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1631 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1632 | /* |
| 1633 | ** Return UTF-16 encoded English language explanation of the most recent |
| 1634 | ** error. |
| 1635 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1636 | const void *sqlite3_errmsg16(sqlite3 *db){ |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1637 | static const u16 outOfMem[] = { |
| 1638 | 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0 |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1639 | }; |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1640 | static const u16 misuse[] = { |
| 1641 | 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', |
| 1642 | 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', |
| 1643 | 'c', 'a', 'l', 'l', 'e', 'd', ' ', |
| 1644 | 'o', 'u', 't', ' ', |
| 1645 | 'o', 'f', ' ', |
| 1646 | 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0 |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 1647 | }; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1648 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1649 | const void *z; |
danielk1977 | ae7fc49 | 2007-03-29 15:00:52 +0000 | [diff] [blame] | 1650 | if( !db ){ |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1651 | return (void *)outOfMem; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1652 | } |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1653 | if( !sqlite3SafetyCheckSickOrOk(db) ){ |
danielk1977 | 7c5c3ca | 2009-02-23 14:42:53 +0000 | [diff] [blame] | 1654 | return (void *)misuse; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1655 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1656 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 1657 | if( db->mallocFailed ){ |
| 1658 | z = (void *)outOfMem; |
| 1659 | }else{ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1660 | z = sqlite3_value_text16(db->pErr); |
danielk1977 | 238746a | 2009-03-19 18:51:06 +0000 | [diff] [blame] | 1661 | if( z==0 ){ |
| 1662 | sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), |
| 1663 | SQLITE_UTF8, SQLITE_STATIC); |
| 1664 | z = sqlite3_value_text16(db->pErr); |
| 1665 | } |
| 1666 | /* A malloc() may have failed within the call to sqlite3_value_text16() |
| 1667 | ** above. If this is the case, then the db->mallocFailed flag needs to |
| 1668 | ** be cleared before returning. Do this directly, instead of via |
| 1669 | ** sqlite3ApiExit(), to avoid setting the database handle error message. |
| 1670 | */ |
| 1671 | db->mallocFailed = 0; |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1672 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1673 | sqlite3_mutex_leave(db->mutex); |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1674 | return z; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1675 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 1676 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1677 | |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1678 | /* |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 1679 | ** Return the most recent error code generated by an SQLite routine. If NULL is |
| 1680 | ** passed to this function, we assume a malloc() failed during sqlite3_open(). |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1681 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1682 | int sqlite3_errcode(sqlite3 *db){ |
danielk1977 | 7c36d07 | 2008-01-31 15:31:01 +0000 | [diff] [blame] | 1683 | if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
drh | c60d044 | 2004-09-30 13:43:13 +0000 | [diff] [blame] | 1684 | return SQLITE_MISUSE; |
| 1685 | } |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 1686 | if( !db || db->mallocFailed ){ |
| 1687 | return SQLITE_NOMEM; |
| 1688 | } |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1689 | return db->errCode & db->errMask; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1690 | } |
drh | 99dfe5e | 2008-10-30 15:03:15 +0000 | [diff] [blame] | 1691 | int sqlite3_extended_errcode(sqlite3 *db){ |
| 1692 | if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
| 1693 | return SQLITE_MISUSE; |
| 1694 | } |
| 1695 | if( !db || db->mallocFailed ){ |
| 1696 | return SQLITE_NOMEM; |
| 1697 | } |
| 1698 | return db->errCode; |
| 1699 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1700 | |
drh | 0e819f9 | 2006-02-01 13:50:41 +0000 | [diff] [blame] | 1701 | /* |
| 1702 | ** Create a new collating function for database "db". The name is zName |
| 1703 | ** and the encoding is enc. |
| 1704 | */ |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1705 | static int createCollation( |
drh | f8d4e8b | 2009-08-20 02:49:30 +0000 | [diff] [blame] | 1706 | sqlite3* db, |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1707 | const char *zName, |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 1708 | u8 enc, |
| 1709 | u8 collType, |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1710 | void* pCtx, |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1711 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 1712 | void(*xDel)(void*) |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1713 | ){ |
| 1714 | CollSeq *pColl; |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 1715 | int enc2; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1716 | int nName = sqlite3Strlen30(zName); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1717 | |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 1718 | assert( sqlite3_mutex_held(db->mutex) ); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1719 | |
| 1720 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 1721 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 1722 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 1723 | */ |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1724 | enc2 = enc; |
danielk1977 | ebb3293 | 2009-04-28 15:35:38 +0000 | [diff] [blame] | 1725 | testcase( enc2==SQLITE_UTF16 ); |
| 1726 | testcase( enc2==SQLITE_UTF16_ALIGNED ); |
| 1727 | if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ |
drh | 7d9bd4e | 2006-02-16 18:16:36 +0000 | [diff] [blame] | 1728 | enc2 = SQLITE_UTF16NATIVE; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1729 | } |
drh | 51c7d86 | 2009-04-27 18:46:06 +0000 | [diff] [blame] | 1730 | if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){ |
drh | d55d57e | 2008-07-07 17:53:07 +0000 | [diff] [blame] | 1731 | return SQLITE_MISUSE; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* Check if this call is removing or replacing an existing collation |
| 1735 | ** sequence. If so, and there are active VMs, return busy. If there |
| 1736 | ** are no active VMs, invalidate any pre-compiled statements. |
| 1737 | */ |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1738 | pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1739 | if( pColl && pColl->xCmp ){ |
| 1740 | if( db->activeVdbeCnt ){ |
| 1741 | sqlite3Error(db, SQLITE_BUSY, |
drh | b309bec | 2009-02-04 17:40:57 +0000 | [diff] [blame] | 1742 | "unable to delete/modify collation sequence due to active statements"); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1743 | return SQLITE_BUSY; |
| 1744 | } |
| 1745 | sqlite3ExpirePreparedStatements(db); |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1746 | |
| 1747 | /* If collation sequence pColl was created directly by a call to |
| 1748 | ** sqlite3_create_collation, and not generated by synthCollSeq(), |
| 1749 | ** then any copies made by synthCollSeq() need to be invalidated. |
| 1750 | ** Also, collation destructor - CollSeq.xDel() - function may need |
| 1751 | ** to be called. |
| 1752 | */ |
| 1753 | if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ |
drh | 0a687d1 | 2008-07-08 14:52:07 +0000 | [diff] [blame] | 1754 | CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName); |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1755 | int j; |
| 1756 | for(j=0; j<3; j++){ |
| 1757 | CollSeq *p = &aColl[j]; |
| 1758 | if( p->enc==pColl->enc ){ |
| 1759 | if( p->xDel ){ |
| 1760 | p->xDel(p->pUser); |
| 1761 | } |
| 1762 | p->xCmp = 0; |
| 1763 | } |
| 1764 | } |
| 1765 | } |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1768 | pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1769 | if( pColl ){ |
| 1770 | pColl->xCmp = xCompare; |
| 1771 | pColl->pUser = pCtx; |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 1772 | pColl->xDel = xDel; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1773 | pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); |
drh | f8d4e8b | 2009-08-20 02:49:30 +0000 | [diff] [blame] | 1774 | pColl->type = collType; |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 1775 | } |
| 1776 | sqlite3Error(db, SQLITE_OK, 0); |
| 1777 | return SQLITE_OK; |
| 1778 | } |
| 1779 | |
| 1780 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1781 | /* |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1782 | ** This array defines hard upper bounds on limit values. The |
| 1783 | ** initializer must be kept in sync with the SQLITE_LIMIT_* |
| 1784 | ** #defines in sqlite3.h. |
| 1785 | */ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 1786 | static const int aHardLimit[] = { |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1787 | SQLITE_MAX_LENGTH, |
| 1788 | SQLITE_MAX_SQL_LENGTH, |
| 1789 | SQLITE_MAX_COLUMN, |
| 1790 | SQLITE_MAX_EXPR_DEPTH, |
| 1791 | SQLITE_MAX_COMPOUND_SELECT, |
| 1792 | SQLITE_MAX_VDBE_OP, |
| 1793 | SQLITE_MAX_FUNCTION_ARG, |
| 1794 | SQLITE_MAX_ATTACHED, |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1795 | SQLITE_MAX_LIKE_PATTERN_LENGTH, |
| 1796 | SQLITE_MAX_VARIABLE_NUMBER, |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 1797 | SQLITE_MAX_TRIGGER_DEPTH, |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1798 | }; |
| 1799 | |
| 1800 | /* |
| 1801 | ** Make sure the hard limits are set to reasonable values |
| 1802 | */ |
| 1803 | #if SQLITE_MAX_LENGTH<100 |
| 1804 | # error SQLITE_MAX_LENGTH must be at least 100 |
| 1805 | #endif |
| 1806 | #if SQLITE_MAX_SQL_LENGTH<100 |
| 1807 | # error SQLITE_MAX_SQL_LENGTH must be at least 100 |
| 1808 | #endif |
| 1809 | #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH |
| 1810 | # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH |
| 1811 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1812 | #if SQLITE_MAX_COMPOUND_SELECT<2 |
| 1813 | # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 |
| 1814 | #endif |
| 1815 | #if SQLITE_MAX_VDBE_OP<40 |
| 1816 | # error SQLITE_MAX_VDBE_OP must be at least 40 |
| 1817 | #endif |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 1818 | #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000 |
| 1819 | # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000 |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1820 | #endif |
danielk1977 | 7b7e23b | 2008-08-20 16:21:12 +0000 | [diff] [blame] | 1821 | #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>30 |
| 1822 | # error SQLITE_MAX_ATTACHED must be between 0 and 30 |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1823 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1824 | #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 |
| 1825 | # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 |
| 1826 | #endif |
drh | 70a8ca3 | 2008-08-21 18:49:27 +0000 | [diff] [blame] | 1827 | #if SQLITE_MAX_COLUMN>32767 |
| 1828 | # error SQLITE_MAX_COLUMN must not exceed 32767 |
| 1829 | #endif |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 1830 | #if SQLITE_MAX_TRIGGER_DEPTH<1 |
| 1831 | # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 |
| 1832 | #endif |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1833 | |
| 1834 | |
| 1835 | /* |
| 1836 | ** Change the value of a limit. Report the old value. |
| 1837 | ** If an invalid limit index is supplied, report -1. |
| 1838 | ** Make no changes but still report the old value if the |
| 1839 | ** new limit is negative. |
| 1840 | ** |
| 1841 | ** A new lower limit does not shrink existing constructs. |
| 1842 | ** It merely prevents new constructs that exceed the limit |
| 1843 | ** from forming. |
| 1844 | */ |
| 1845 | int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 1846 | int oldLimit; |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 1847 | if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1848 | return -1; |
| 1849 | } |
| 1850 | oldLimit = db->aLimit[limitId]; |
| 1851 | if( newLimit>=0 ){ |
drh | f47ce56 | 2008-03-20 18:00:49 +0000 | [diff] [blame] | 1852 | if( newLimit>aHardLimit[limitId] ){ |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1853 | newLimit = aHardLimit[limitId]; |
| 1854 | } |
| 1855 | db->aLimit[limitId] = newLimit; |
| 1856 | } |
| 1857 | return oldLimit; |
| 1858 | } |
| 1859 | |
| 1860 | /* |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1861 | ** This routine does the work of opening a database on behalf of |
| 1862 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
drh | ee570fa | 2005-04-28 12:06:05 +0000 | [diff] [blame] | 1863 | ** is UTF-8 encoded. |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1864 | */ |
| 1865 | static int openDatabase( |
| 1866 | const char *zFilename, /* Database filename UTF-8 encoded */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1867 | sqlite3 **ppDb, /* OUT: Returned database handle */ |
| 1868 | unsigned flags, /* Operational flags */ |
| 1869 | const char *zVfs /* Name of the VFS to use */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1870 | ){ |
| 1871 | sqlite3 *db; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1872 | int rc; |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 1873 | int isThreadsafe; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1874 | |
drh | e0d0f8e | 2009-04-28 04:47:31 +0000 | [diff] [blame] | 1875 | *ppDb = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 1876 | #ifndef SQLITE_OMIT_AUTOINIT |
| 1877 | rc = sqlite3_initialize(); |
| 1878 | if( rc ) return rc; |
| 1879 | #endif |
| 1880 | |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 1881 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 1882 | isThreadsafe = 0; |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 1883 | }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 1884 | isThreadsafe = 0; |
| 1885 | }else if( flags & SQLITE_OPEN_FULLMUTEX ){ |
| 1886 | isThreadsafe = 1; |
| 1887 | }else{ |
| 1888 | isThreadsafe = sqlite3GlobalConfig.bFullMutex; |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 1889 | } |
drh | f1f1268 | 2009-09-09 14:17:52 +0000 | [diff] [blame] | 1890 | if( flags & SQLITE_OPEN_PRIVATECACHE ){ |
drh | f4cfac9 | 2009-09-09 15:29:41 +0000 | [diff] [blame] | 1891 | flags &= ~SQLITE_OPEN_SHAREDCACHE; |
drh | f1f1268 | 2009-09-09 14:17:52 +0000 | [diff] [blame] | 1892 | }else if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
| 1893 | flags |= SQLITE_OPEN_SHAREDCACHE; |
| 1894 | } |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 1895 | |
drh | b25a9f4 | 2009-05-12 13:35:11 +0000 | [diff] [blame] | 1896 | /* Remove harmful bits from the flags parameter |
| 1897 | ** |
| 1898 | ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were |
| 1899 | ** dealt with in the previous code block. Besides these, the only |
| 1900 | ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, |
| 1901 | ** SQLITE_OPEN_READWRITE, and SQLITE_OPEN_CREATE. Silently mask |
| 1902 | ** off all other flags. |
| 1903 | */ |
drh | a4267dc | 2008-02-19 15:20:44 +0000 | [diff] [blame] | 1904 | flags &= ~( SQLITE_OPEN_DELETEONCLOSE | |
drh | b25a9f4 | 2009-05-12 13:35:11 +0000 | [diff] [blame] | 1905 | SQLITE_OPEN_EXCLUSIVE | |
drh | a4267dc | 2008-02-19 15:20:44 +0000 | [diff] [blame] | 1906 | SQLITE_OPEN_MAIN_DB | |
| 1907 | SQLITE_OPEN_TEMP_DB | |
| 1908 | SQLITE_OPEN_TRANSIENT_DB | |
| 1909 | SQLITE_OPEN_MAIN_JOURNAL | |
| 1910 | SQLITE_OPEN_TEMP_JOURNAL | |
| 1911 | SQLITE_OPEN_SUBJOURNAL | |
danielk1977 | 9a6284c | 2008-07-10 17:52:49 +0000 | [diff] [blame] | 1912 | SQLITE_OPEN_MASTER_JOURNAL | |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 1913 | SQLITE_OPEN_NOMUTEX | |
| 1914 | SQLITE_OPEN_FULLMUTEX |
drh | a4267dc | 2008-02-19 15:20:44 +0000 | [diff] [blame] | 1915 | ); |
| 1916 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1917 | /* Allocate the sqlite data structure */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1918 | db = sqlite3MallocZero( sizeof(sqlite3) ); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1919 | if( db==0 ) goto opendb_out; |
drh | 039963a | 2008-09-03 00:43:15 +0000 | [diff] [blame] | 1920 | if( isThreadsafe ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1921 | db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
| 1922 | if( db->mutex==0 ){ |
| 1923 | sqlite3_free(db); |
| 1924 | db = 0; |
| 1925 | goto opendb_out; |
| 1926 | } |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 1927 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1928 | sqlite3_mutex_enter(db->mutex); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1929 | db->errMask = 0xff; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1930 | db->nDb = 2; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1931 | db->magic = SQLITE_MAGIC_BUSY; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1932 | db->aDb = db->aDbStatic; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1933 | |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 1934 | assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); |
| 1935 | memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1936 | db->autoCommit = 1; |
drh | ddac25c | 2007-12-05 01:38:23 +0000 | [diff] [blame] | 1937 | db->nextAutovac = -1; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1938 | db->nextPagesize = 0; |
drh | 76fe803 | 2006-07-11 14:17:51 +0000 | [diff] [blame] | 1939 | db->flags |= SQLITE_ShortColNames |
| 1940 | #if SQLITE_DEFAULT_FILE_FORMAT<4 |
| 1941 | | SQLITE_LegacyFileFmt |
| 1942 | #endif |
drh | 56424db | 2007-03-27 22:24:11 +0000 | [diff] [blame] | 1943 | #ifdef SQLITE_ENABLE_LOAD_EXTENSION |
| 1944 | | SQLITE_LoadExtension |
| 1945 | #endif |
dan | 5bde73c | 2009-09-01 17:11:07 +0000 | [diff] [blame] | 1946 | #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS |
dan | 5e1a278 | 2009-09-01 17:28:29 +0000 | [diff] [blame] | 1947 | | SQLITE_RecTriggers |
dan | 76d462e | 2009-08-30 11:42:51 +0000 | [diff] [blame] | 1948 | #endif |
drh | 76fe803 | 2006-07-11 14:17:51 +0000 | [diff] [blame] | 1949 | ; |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 1950 | sqlite3HashInit(&db->aCollSeq); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1951 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | e61922a | 2009-05-02 13:29:37 +0000 | [diff] [blame] | 1952 | sqlite3HashInit(&db->aModule); |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1953 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1954 | |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 1955 | db->pVfs = sqlite3_vfs_find(zVfs); |
| 1956 | if( !db->pVfs ){ |
| 1957 | rc = SQLITE_ERROR; |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 1958 | sqlite3Error(db, rc, "no such vfs: %s", zVfs); |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 1959 | goto opendb_out; |
| 1960 | } |
| 1961 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1962 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 1963 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 1964 | ** conversions. The only error that can occur here is a malloc() failure. |
| 1965 | */ |
drh | f8d4e8b | 2009-08-20 02:49:30 +0000 | [diff] [blame] | 1966 | createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0, |
| 1967 | binCollFunc, 0); |
| 1968 | createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0, |
| 1969 | binCollFunc, 0); |
| 1970 | createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0, |
| 1971 | binCollFunc, 0); |
| 1972 | createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1, |
| 1973 | binCollFunc, 0); |
drh | 77db4c0 | 2008-03-18 13:01:38 +0000 | [diff] [blame] | 1974 | if( db->mallocFailed ){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1975 | goto opendb_out; |
| 1976 | } |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 1977 | db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); |
drh | 77db4c0 | 2008-03-18 13:01:38 +0000 | [diff] [blame] | 1978 | assert( db->pDfltColl!=0 ); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1979 | |
| 1980 | /* Also add a UTF-8 case-insensitive collation sequence. */ |
drh | f8d4e8b | 2009-08-20 02:49:30 +0000 | [diff] [blame] | 1981 | createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0, |
| 1982 | nocaseCollatingFunc, 0); |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 1983 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1984 | /* Open the backend database driver */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1985 | db->openFlags = flags; |
| 1986 | rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, |
| 1987 | flags | SQLITE_OPEN_MAIN_DB, |
drh | c797d4d | 2007-05-08 01:08:49 +0000 | [diff] [blame] | 1988 | &db->aDb[0].pBt); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1989 | if( rc!=SQLITE_OK ){ |
danielk1977 | 45e60aa | 2008-09-23 17:39:26 +0000 | [diff] [blame] | 1990 | if( rc==SQLITE_IOERR_NOMEM ){ |
| 1991 | rc = SQLITE_NOMEM; |
| 1992 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1993 | sqlite3Error(db, rc, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1994 | goto opendb_out; |
| 1995 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1996 | db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); |
| 1997 | db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 1998 | |
drh | 03b808a | 2006-03-13 15:06:05 +0000 | [diff] [blame] | 1999 | |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2000 | /* The default safety_level for the main database is 'full'; for the temp |
| 2001 | ** database it is 'NONE'. This matches the pager layer defaults. |
| 2002 | */ |
| 2003 | db->aDb[0].zName = "main"; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 2004 | db->aDb[0].safety_level = 3; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2005 | db->aDb[1].zName = "temp"; |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 2006 | db->aDb[1].safety_level = 1; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 2007 | |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2008 | db->magic = SQLITE_MAGIC_OPEN; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2009 | if( db->mallocFailed ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2010 | goto opendb_out; |
| 2011 | } |
| 2012 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 2013 | /* Register all built-in functions, but do not attempt to read the |
| 2014 | ** database schema yet. This is delayed until the first time the database |
| 2015 | ** is accessed. |
| 2016 | */ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2017 | sqlite3Error(db, SQLITE_OK, 0); |
| 2018 | sqlite3RegisterBuiltinFunctions(db); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2019 | |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2020 | /* Load automatic extensions - extensions that have been registered |
| 2021 | ** using the sqlite3_automatic_extension() API. |
| 2022 | */ |
drh | 7aaa878 | 2009-05-20 02:40:45 +0000 | [diff] [blame] | 2023 | sqlite3AutoLoadExtensions(db); |
| 2024 | rc = sqlite3_errcode(db); |
| 2025 | if( rc!=SQLITE_OK ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2026 | goto opendb_out; |
| 2027 | } |
drh | 1409be6 | 2006-08-23 20:07:20 +0000 | [diff] [blame] | 2028 | |
drh | aa29c13 | 2006-09-02 13:58:06 +0000 | [diff] [blame] | 2029 | #ifdef SQLITE_ENABLE_FTS1 |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2030 | if( !db->mallocFailed ){ |
drh | aa29c13 | 2006-09-02 13:58:06 +0000 | [diff] [blame] | 2031 | extern int sqlite3Fts1Init(sqlite3*); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2032 | rc = sqlite3Fts1Init(db); |
drh | aa29c13 | 2006-09-02 13:58:06 +0000 | [diff] [blame] | 2033 | } |
| 2034 | #endif |
| 2035 | |
shess | a26cf57 | 2006-10-19 20:27:58 +0000 | [diff] [blame] | 2036 | #ifdef SQLITE_ENABLE_FTS2 |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2037 | if( !db->mallocFailed && rc==SQLITE_OK ){ |
shess | a26cf57 | 2006-10-19 20:27:58 +0000 | [diff] [blame] | 2038 | extern int sqlite3Fts2Init(sqlite3*); |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2039 | rc = sqlite3Fts2Init(db); |
shess | a26cf57 | 2006-10-19 20:27:58 +0000 | [diff] [blame] | 2040 | } |
| 2041 | #endif |
| 2042 | |
shess | 69c4ae2 | 2007-08-20 17:37:47 +0000 | [diff] [blame] | 2043 | #ifdef SQLITE_ENABLE_FTS3 |
| 2044 | if( !db->mallocFailed && rc==SQLITE_OK ){ |
shess | 69c4ae2 | 2007-08-20 17:37:47 +0000 | [diff] [blame] | 2045 | rc = sqlite3Fts3Init(db); |
| 2046 | } |
| 2047 | #endif |
| 2048 | |
danielk1977 | 83852ac | 2007-05-06 16:04:11 +0000 | [diff] [blame] | 2049 | #ifdef SQLITE_ENABLE_ICU |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2050 | if( !db->mallocFailed && rc==SQLITE_OK ){ |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2051 | rc = sqlite3IcuInit(db); |
danielk1977 | 83852ac | 2007-05-06 16:04:11 +0000 | [diff] [blame] | 2052 | } |
| 2053 | #endif |
danielk1977 | ebaecc1 | 2008-05-26 18:41:54 +0000 | [diff] [blame] | 2054 | |
| 2055 | #ifdef SQLITE_ENABLE_RTREE |
| 2056 | if( !db->mallocFailed && rc==SQLITE_OK){ |
danielk1977 | ebaecc1 | 2008-05-26 18:41:54 +0000 | [diff] [blame] | 2057 | rc = sqlite3RtreeInit(db); |
| 2058 | } |
| 2059 | #endif |
| 2060 | |
danielk1977 | 832a58a | 2007-06-22 15:21:15 +0000 | [diff] [blame] | 2061 | sqlite3Error(db, rc, 0); |
danielk1977 | 83852ac | 2007-05-06 16:04:11 +0000 | [diff] [blame] | 2062 | |
drh | 8fd5bd3 | 2007-04-02 16:40:37 +0000 | [diff] [blame] | 2063 | /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking |
| 2064 | ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking |
| 2065 | ** mode. Doing nothing at all also makes NORMAL the default. |
| 2066 | */ |
| 2067 | #ifdef SQLITE_DEFAULT_LOCKING_MODE |
| 2068 | db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; |
| 2069 | sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), |
| 2070 | SQLITE_DEFAULT_LOCKING_MODE); |
| 2071 | #endif |
| 2072 | |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2073 | /* Enable the lookaside-malloc subsystem */ |
drh | 1b67f3c | 2008-10-10 17:41:28 +0000 | [diff] [blame] | 2074 | setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, |
| 2075 | sqlite3GlobalConfig.nLookaside); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2076 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2077 | opendb_out: |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 2078 | if( db ){ |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 2079 | assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 2080 | sqlite3_mutex_leave(db->mutex); |
| 2081 | } |
danielk1977 | 59633ae | 2008-08-27 19:01:57 +0000 | [diff] [blame] | 2082 | rc = sqlite3_errcode(db); |
| 2083 | if( rc==SQLITE_NOMEM ){ |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2084 | sqlite3_close(db); |
| 2085 | db = 0; |
danielk1977 | 59633ae | 2008-08-27 19:01:57 +0000 | [diff] [blame] | 2086 | }else if( rc!=SQLITE_OK ){ |
| 2087 | db->magic = SQLITE_MAGIC_SICK; |
danielk1977 | 1307393 | 2004-06-30 11:54:06 +0000 | [diff] [blame] | 2088 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2089 | *ppDb = db; |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 2090 | return sqlite3ApiExit(0, rc); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | ** Open a new database handle. |
| 2095 | */ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2096 | int sqlite3_open( |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2097 | const char *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 2098 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2099 | ){ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2100 | return openDatabase(zFilename, ppDb, |
| 2101 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
| 2102 | } |
| 2103 | int sqlite3_open_v2( |
drh | 428e282 | 2007-08-30 16:23:19 +0000 | [diff] [blame] | 2104 | const char *filename, /* Database filename (UTF-8) */ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2105 | sqlite3 **ppDb, /* OUT: SQLite db handle */ |
| 2106 | int flags, /* Flags */ |
| 2107 | const char *zVfs /* Name of VFS module to use */ |
| 2108 | ){ |
| 2109 | return openDatabase(filename, ppDb, flags, zVfs); |
danielk1977 | 83ab5a8 | 2004-05-21 11:39:05 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2112 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2113 | /* |
| 2114 | ** Open a new database handle. |
| 2115 | */ |
| 2116 | int sqlite3_open16( |
| 2117 | const void *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 2118 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2119 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2120 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2121 | sqlite3_value *pVal; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2122 | int rc; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2123 | |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2124 | assert( zFilename ); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2125 | assert( ppDb ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2126 | *ppDb = 0; |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2127 | #ifndef SQLITE_OMIT_AUTOINIT |
| 2128 | rc = sqlite3_initialize(); |
| 2129 | if( rc ) return rc; |
| 2130 | #endif |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2131 | pVal = sqlite3ValueNew(0); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 2132 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 2133 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2134 | if( zFilename8 ){ |
drh | 605264d | 2007-08-21 15:13:19 +0000 | [diff] [blame] | 2135 | rc = openDatabase(zFilename8, ppDb, |
| 2136 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
drh | afc9104 | 2008-02-21 02:09:45 +0000 | [diff] [blame] | 2137 | assert( *ppDb || rc==SQLITE_NOMEM ); |
danielk1977 | f51bf48 | 2008-04-28 16:19:35 +0000 | [diff] [blame] | 2138 | if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ |
danielk1977 | fb103a8 | 2008-04-03 16:28:24 +0000 | [diff] [blame] | 2139 | ENC(*ppDb) = SQLITE_UTF16NATIVE; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 2140 | } |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2141 | }else{ |
| 2142 | rc = SQLITE_NOMEM; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2143 | } |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2144 | sqlite3ValueFree(pVal); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 2145 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 2146 | return sqlite3ApiExit(0, rc); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2147 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2148 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 2149 | |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 2150 | /* |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2151 | ** Register a new collation sequence with the database handle db. |
| 2152 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2153 | int sqlite3_create_collation( |
| 2154 | sqlite3* db, |
| 2155 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 2156 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2157 | void* pCtx, |
| 2158 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 2159 | ){ |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2160 | int rc; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2161 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2162 | assert( !db->mallocFailed ); |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 2163 | rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2164 | rc = sqlite3ApiExit(db, rc); |
| 2165 | sqlite3_mutex_leave(db->mutex); |
| 2166 | return rc; |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | ** Register a new collation sequence with the database handle db. |
| 2171 | */ |
danielk1977 | a393c03 | 2007-05-07 14:58:53 +0000 | [diff] [blame] | 2172 | int sqlite3_create_collation_v2( |
danielk1977 | a9808b3 | 2007-05-07 09:32:45 +0000 | [diff] [blame] | 2173 | sqlite3* db, |
| 2174 | const char *zName, |
| 2175 | int enc, |
| 2176 | void* pCtx, |
| 2177 | int(*xCompare)(void*,int,const void*,int,const void*), |
| 2178 | void(*xDel)(void*) |
| 2179 | ){ |
| 2180 | int rc; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2181 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2182 | assert( !db->mallocFailed ); |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 2183 | rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2184 | rc = sqlite3ApiExit(db, rc); |
| 2185 | sqlite3_mutex_leave(db->mutex); |
| 2186 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2189 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2190 | /* |
| 2191 | ** Register a new collation sequence with the database handle db. |
| 2192 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2193 | int sqlite3_create_collation16( |
| 2194 | sqlite3* db, |
mihailim | bda2e62 | 2008-06-23 11:23:14 +0000 | [diff] [blame] | 2195 | const void *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 2196 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2197 | void* pCtx, |
| 2198 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 2199 | ){ |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2200 | int rc = SQLITE_OK; |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 2201 | char *zName8; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2202 | sqlite3_mutex_enter(db->mutex); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2203 | assert( !db->mallocFailed ); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2204 | zName8 = sqlite3Utf16to8(db, zName, -1); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2205 | if( zName8 ){ |
shane | cea72b2 | 2009-09-07 04:38:36 +0000 | [diff] [blame] | 2206 | rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2207 | sqlite3DbFree(db, zName8); |
danielk1977 | 9a30cf6 | 2006-01-18 04:26:07 +0000 | [diff] [blame] | 2208 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2209 | rc = sqlite3ApiExit(db, rc); |
| 2210 | sqlite3_mutex_leave(db->mutex); |
| 2211 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2212 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2213 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2214 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2215 | /* |
| 2216 | ** Register a collation sequence factory callback with the database handle |
| 2217 | ** db. Replace any previously installed collation sequence factory. |
| 2218 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2219 | int sqlite3_collation_needed( |
| 2220 | sqlite3 *db, |
| 2221 | void *pCollNeededArg, |
| 2222 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 2223 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2224 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2225 | db->xCollNeeded = xCollNeeded; |
| 2226 | db->xCollNeeded16 = 0; |
| 2227 | db->pCollNeededArg = pCollNeededArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2228 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2229 | return SQLITE_OK; |
| 2230 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2231 | |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2232 | #ifndef SQLITE_OMIT_UTF16 |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 2233 | /* |
| 2234 | ** Register a collation sequence factory callback with the database handle |
| 2235 | ** db. Replace any previously installed collation sequence factory. |
| 2236 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2237 | int sqlite3_collation_needed16( |
| 2238 | sqlite3 *db, |
| 2239 | void *pCollNeededArg, |
| 2240 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 2241 | ){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2242 | sqlite3_mutex_enter(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2243 | db->xCollNeeded = 0; |
| 2244 | db->xCollNeeded16 = xCollNeeded16; |
| 2245 | db->pCollNeededArg = pCollNeededArg; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2246 | sqlite3_mutex_leave(db->mutex); |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2247 | return SQLITE_OK; |
| 2248 | } |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 2249 | #endif /* SQLITE_OMIT_UTF16 */ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2250 | |
| 2251 | #ifndef SQLITE_OMIT_GLOBALRECOVER |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2252 | #ifndef SQLITE_OMIT_DEPRECATED |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2253 | /* |
danielk1977 | 7ddad96 | 2005-12-12 06:53:03 +0000 | [diff] [blame] | 2254 | ** This function is now an anachronism. It used to be used to recover from a |
| 2255 | ** malloc() failure, but SQLite now does this automatically. |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2256 | */ |
drh | 7d97efb | 2007-10-12 19:35:48 +0000 | [diff] [blame] | 2257 | int sqlite3_global_recover(void){ |
danielk1977 | 261919c | 2005-12-06 12:52:59 +0000 | [diff] [blame] | 2258 | return SQLITE_OK; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 2259 | } |
| 2260 | #endif |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2261 | #endif |
drh | 3e1d8e6 | 2005-05-26 16:23:34 +0000 | [diff] [blame] | 2262 | |
| 2263 | /* |
| 2264 | ** Test to see whether or not the database connection is in autocommit |
| 2265 | ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 2266 | ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 2267 | ** by the next COMMIT or ROLLBACK. |
| 2268 | ** |
| 2269 | ******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** |
| 2270 | */ |
| 2271 | int sqlite3_get_autocommit(sqlite3 *db){ |
| 2272 | return db->autoCommit; |
| 2273 | } |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2274 | |
| 2275 | #ifdef SQLITE_DEBUG |
| 2276 | /* |
| 2277 | ** The following routine is subtituted for constant SQLITE_CORRUPT in |
| 2278 | ** debugging builds. This provides a way to set a breakpoint for when |
| 2279 | ** corruption is first detected. |
| 2280 | */ |
| 2281 | int sqlite3Corrupt(void){ |
| 2282 | return SQLITE_CORRUPT; |
| 2283 | } |
| 2284 | #endif |
drh | 7c1817e | 2006-01-10 13:58:48 +0000 | [diff] [blame] | 2285 | |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2286 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2287 | /* |
| 2288 | ** This is a convenience routine that makes sure that all thread-specific |
| 2289 | ** data for this thread has been deallocated. |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 2290 | ** |
| 2291 | ** SQLite no longer uses thread-specific data so this routine is now a |
| 2292 | ** no-op. It is retained for historical compatibility. |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2293 | */ |
| 2294 | void sqlite3_thread_cleanup(void){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 2295 | } |
shane | eec556d | 2008-10-12 00:27:53 +0000 | [diff] [blame] | 2296 | #endif |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2297 | |
| 2298 | /* |
| 2299 | ** Return meta information about a specific column of a database table. |
| 2300 | ** See comment in sqlite3.h (sqlite.h.in) for details. |
| 2301 | */ |
| 2302 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 2303 | int sqlite3_table_column_metadata( |
| 2304 | sqlite3 *db, /* Connection handle */ |
| 2305 | const char *zDbName, /* Database name or NULL */ |
| 2306 | const char *zTableName, /* Table name */ |
| 2307 | const char *zColumnName, /* Column name */ |
| 2308 | char const **pzDataType, /* OUTPUT: Declared data type */ |
| 2309 | char const **pzCollSeq, /* OUTPUT: Collation sequence name */ |
| 2310 | int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ |
| 2311 | int *pPrimaryKey, /* OUTPUT: True if column part of PK */ |
drh | 9be0716 | 2008-06-16 20:51:15 +0000 | [diff] [blame] | 2312 | int *pAutoinc /* OUTPUT: True if column is auto-increment */ |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2313 | ){ |
| 2314 | int rc; |
| 2315 | char *zErrMsg = 0; |
| 2316 | Table *pTab = 0; |
| 2317 | Column *pCol = 0; |
| 2318 | int iCol; |
| 2319 | |
| 2320 | char const *zDataType = 0; |
| 2321 | char const *zCollSeq = 0; |
| 2322 | int notnull = 0; |
| 2323 | int primarykey = 0; |
| 2324 | int autoinc = 0; |
| 2325 | |
| 2326 | /* Ensure the database schema has been loaded */ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2327 | sqlite3_mutex_enter(db->mutex); |
drh | a0af99f | 2008-04-16 00:49:12 +0000 | [diff] [blame] | 2328 | (void)sqlite3SafetyOn(db); |
drh | 3cb3edc | 2008-03-07 21:37:19 +0000 | [diff] [blame] | 2329 | sqlite3BtreeEnterAll(db); |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2330 | rc = sqlite3Init(db, &zErrMsg); |
| 2331 | if( SQLITE_OK!=rc ){ |
| 2332 | goto error_out; |
| 2333 | } |
| 2334 | |
| 2335 | /* Locate the table in question */ |
| 2336 | pTab = sqlite3FindTable(db, zTableName, zDbName); |
| 2337 | if( !pTab || pTab->pSelect ){ |
| 2338 | pTab = 0; |
| 2339 | goto error_out; |
| 2340 | } |
| 2341 | |
| 2342 | /* Find the column for which info is requested */ |
| 2343 | if( sqlite3IsRowid(zColumnName) ){ |
| 2344 | iCol = pTab->iPKey; |
| 2345 | if( iCol>=0 ){ |
| 2346 | pCol = &pTab->aCol[iCol]; |
| 2347 | } |
| 2348 | }else{ |
| 2349 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 2350 | pCol = &pTab->aCol[iCol]; |
| 2351 | if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ |
| 2352 | break; |
| 2353 | } |
| 2354 | } |
| 2355 | if( iCol==pTab->nCol ){ |
| 2356 | pTab = 0; |
| 2357 | goto error_out; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | /* The following block stores the meta information that will be returned |
| 2362 | ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey |
| 2363 | ** and autoinc. At this point there are two possibilities: |
| 2364 | ** |
| 2365 | ** 1. The specified column name was rowid", "oid" or "_rowid_" |
| 2366 | ** and there is no explicitly declared IPK column. |
| 2367 | ** |
| 2368 | ** 2. The table is not a view and the column name identified an |
| 2369 | ** explicitly declared column. Copy meta information from *pCol. |
| 2370 | */ |
| 2371 | if( pCol ){ |
| 2372 | zDataType = pCol->zType; |
| 2373 | zCollSeq = pCol->zColl; |
drh | 9be0716 | 2008-06-16 20:51:15 +0000 | [diff] [blame] | 2374 | notnull = pCol->notNull!=0; |
| 2375 | primarykey = pCol->isPrimKey!=0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2376 | autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2377 | }else{ |
| 2378 | zDataType = "INTEGER"; |
| 2379 | primarykey = 1; |
| 2380 | } |
| 2381 | if( !zCollSeq ){ |
| 2382 | zCollSeq = "BINARY"; |
| 2383 | } |
| 2384 | |
| 2385 | error_out: |
danielk1977 | 02b4e3b | 2009-02-26 07:15:59 +0000 | [diff] [blame] | 2386 | sqlite3BtreeLeaveAll(db); |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 2387 | (void)sqlite3SafetyOff(db); |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2388 | |
| 2389 | /* Whether the function call succeeded or failed, set the output parameters |
| 2390 | ** to whatever their local counterparts contain. If an error did occur, |
| 2391 | ** this has the effect of zeroing all output parameters. |
| 2392 | */ |
| 2393 | if( pzDataType ) *pzDataType = zDataType; |
| 2394 | if( pzCollSeq ) *pzCollSeq = zCollSeq; |
| 2395 | if( pNotNull ) *pNotNull = notnull; |
| 2396 | if( pPrimaryKey ) *pPrimaryKey = primarykey; |
| 2397 | if( pAutoinc ) *pAutoinc = autoinc; |
| 2398 | |
| 2399 | if( SQLITE_OK==rc && !pTab ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2400 | sqlite3DbFree(db, zErrMsg); |
drh | 118640b | 2008-07-16 14:02:32 +0000 | [diff] [blame] | 2401 | zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 2402 | zColumnName); |
danielk1977 | deb802c | 2006-02-09 13:43:28 +0000 | [diff] [blame] | 2403 | rc = SQLITE_ERROR; |
| 2404 | } |
| 2405 | sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2406 | sqlite3DbFree(db, zErrMsg); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2407 | rc = sqlite3ApiExit(db, rc); |
| 2408 | sqlite3_mutex_leave(db->mutex); |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 2409 | return rc; |
| 2410 | } |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2411 | #endif |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 2412 | |
| 2413 | /* |
| 2414 | ** Sleep for a little while. Return the amount of time slept. |
| 2415 | */ |
| 2416 | int sqlite3_sleep(int ms){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 2417 | sqlite3_vfs *pVfs; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2418 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2419 | pVfs = sqlite3_vfs_find(0); |
drh | 40257ff | 2008-06-13 18:24:27 +0000 | [diff] [blame] | 2420 | if( pVfs==0 ) return 0; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 2421 | |
| 2422 | /* This function works in milliseconds, but the underlying OsSleep() |
| 2423 | ** API uses microseconds. Hence the 1000's. |
| 2424 | */ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2425 | rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2426 | return rc; |
drh | f9cb7f5 | 2006-06-27 20:06:44 +0000 | [diff] [blame] | 2427 | } |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2428 | |
| 2429 | /* |
| 2430 | ** Enable or disable the extended result codes. |
| 2431 | */ |
| 2432 | int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2433 | sqlite3_mutex_enter(db->mutex); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2434 | db->errMask = onoff ? 0xffffffff : 0xff; |
drh | e30f442 | 2007-08-21 16:15:55 +0000 | [diff] [blame] | 2435 | sqlite3_mutex_leave(db->mutex); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 2436 | return SQLITE_OK; |
| 2437 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2438 | |
| 2439 | /* |
| 2440 | ** Invoke the xFileControl method on a particular database. |
| 2441 | */ |
| 2442 | int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ |
| 2443 | int rc = SQLITE_ERROR; |
| 2444 | int iDb; |
| 2445 | sqlite3_mutex_enter(db->mutex); |
| 2446 | if( zDbName==0 ){ |
| 2447 | iDb = 0; |
| 2448 | }else{ |
| 2449 | for(iDb=0; iDb<db->nDb; iDb++){ |
| 2450 | if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break; |
| 2451 | } |
| 2452 | } |
| 2453 | if( iDb<db->nDb ){ |
| 2454 | Btree *pBtree = db->aDb[iDb].pBt; |
| 2455 | if( pBtree ){ |
| 2456 | Pager *pPager; |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 2457 | sqlite3_file *fd; |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2458 | sqlite3BtreeEnter(pBtree); |
| 2459 | pPager = sqlite3BtreePager(pBtree); |
drh | 5517625 | 2008-01-22 14:50:16 +0000 | [diff] [blame] | 2460 | assert( pPager!=0 ); |
| 2461 | fd = sqlite3PagerFile(pPager); |
| 2462 | assert( fd!=0 ); |
| 2463 | if( fd->pMethods ){ |
| 2464 | rc = sqlite3OsFileControl(fd, op, pArg); |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 2465 | } |
| 2466 | sqlite3BtreeLeave(pBtree); |
| 2467 | } |
| 2468 | } |
| 2469 | sqlite3_mutex_leave(db->mutex); |
| 2470 | return rc; |
| 2471 | } |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2472 | |
| 2473 | /* |
| 2474 | ** Interface to the testing logic. |
| 2475 | */ |
| 2476 | int sqlite3_test_control(int op, ...){ |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2477 | int rc = 0; |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 2478 | #ifndef SQLITE_OMIT_BUILTIN_TEST |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2479 | va_list ap; |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2480 | va_start(ap, op); |
| 2481 | switch( op ){ |
danielk1977 | d09414c | 2008-06-19 18:17:49 +0000 | [diff] [blame] | 2482 | |
| 2483 | /* |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2484 | ** Save the current state of the PRNG. |
| 2485 | */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2486 | case SQLITE_TESTCTRL_PRNG_SAVE: { |
| 2487 | sqlite3PrngSaveState(); |
| 2488 | break; |
| 2489 | } |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2490 | |
| 2491 | /* |
| 2492 | ** Restore the state of the PRNG to the last state saved using |
| 2493 | ** PRNG_SAVE. If PRNG_SAVE has never before been called, then |
| 2494 | ** this verb acts like PRNG_RESET. |
| 2495 | */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2496 | case SQLITE_TESTCTRL_PRNG_RESTORE: { |
| 2497 | sqlite3PrngRestoreState(); |
| 2498 | break; |
| 2499 | } |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 2500 | |
| 2501 | /* |
| 2502 | ** Reset the PRNG back to its uninitialized state. The next call |
| 2503 | ** to sqlite3_randomness() will reseed the PRNG using a single call |
| 2504 | ** to the xRandomness method of the default VFS. |
| 2505 | */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 2506 | case SQLITE_TESTCTRL_PRNG_RESET: { |
| 2507 | sqlite3PrngResetState(); |
| 2508 | break; |
| 2509 | } |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 2510 | |
| 2511 | /* |
| 2512 | ** sqlite3_test_control(BITVEC_TEST, size, program) |
| 2513 | ** |
| 2514 | ** Run a test against a Bitvec object of size. The program argument |
| 2515 | ** is an array of integers that defines the test. Return -1 on a |
| 2516 | ** memory allocation error, 0 on success, or non-zero for an error. |
| 2517 | ** See the sqlite3BitvecBuiltinTest() for additional information. |
| 2518 | */ |
| 2519 | case SQLITE_TESTCTRL_BITVEC_TEST: { |
| 2520 | int sz = va_arg(ap, int); |
| 2521 | int *aProg = va_arg(ap, int*); |
| 2522 | rc = sqlite3BitvecBuiltinTest(sz, aProg); |
| 2523 | break; |
| 2524 | } |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2525 | |
| 2526 | /* |
| 2527 | ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) |
| 2528 | ** |
| 2529 | ** Register hooks to call to indicate which malloc() failures |
| 2530 | ** are benign. |
| 2531 | */ |
| 2532 | case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { |
danielk1977 | ca02679 | 2008-06-23 14:15:52 +0000 | [diff] [blame] | 2533 | typedef void (*void_function)(void); |
| 2534 | void_function xBenignBegin; |
| 2535 | void_function xBenignEnd; |
| 2536 | xBenignBegin = va_arg(ap, void_function); |
| 2537 | xBenignEnd = va_arg(ap, void_function); |
danielk1977 | 2d1d86f | 2008-06-20 14:59:51 +0000 | [diff] [blame] | 2538 | sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); |
| 2539 | break; |
| 2540 | } |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 2541 | |
| 2542 | /* |
drh | f3af63f | 2009-05-09 18:59:42 +0000 | [diff] [blame] | 2543 | ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 2544 | ** |
| 2545 | ** Set the PENDING byte to the value in the argument, if X>0. |
| 2546 | ** Make no changes if X==0. Return the value of the pending byte |
| 2547 | ** as it existing before this routine was called. |
| 2548 | ** |
| 2549 | ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in |
| 2550 | ** an incompatible database file format. Changing the PENDING byte |
| 2551 | ** while any database connection is open results in undefined and |
| 2552 | ** dileterious behavior. |
| 2553 | */ |
| 2554 | case SQLITE_TESTCTRL_PENDING_BYTE: { |
| 2555 | unsigned int newVal = va_arg(ap, unsigned int); |
| 2556 | rc = sqlite3PendingByte; |
| 2557 | if( newVal ) sqlite3PendingByte = newVal; |
| 2558 | break; |
| 2559 | } |
drh | f3af63f | 2009-05-09 18:59:42 +0000 | [diff] [blame] | 2560 | |
| 2561 | /* |
| 2562 | ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X) |
| 2563 | ** |
| 2564 | ** This action provides a run-time test to see whether or not |
| 2565 | ** assert() was enabled at compile-time. If X is true and assert() |
| 2566 | ** is enabled, then the return value is true. If X is true and |
| 2567 | ** assert() is disabled, then the return value is zero. If X is |
| 2568 | ** false and assert() is enabled, then the assertion fires and the |
| 2569 | ** process aborts. If X is false and assert() is disabled, then the |
| 2570 | ** return value is zero. |
| 2571 | */ |
| 2572 | case SQLITE_TESTCTRL_ASSERT: { |
| 2573 | volatile int x = 0; |
| 2574 | assert( (x = va_arg(ap,int))!=0 ); |
| 2575 | rc = x; |
| 2576 | break; |
| 2577 | } |
| 2578 | |
| 2579 | |
| 2580 | /* |
| 2581 | ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) |
| 2582 | ** |
| 2583 | ** This action provides a run-time test to see how the ALWAYS and |
| 2584 | ** NEVER macros were defined at compile-time. |
| 2585 | ** |
| 2586 | ** The return value is ALWAYS(X). |
| 2587 | ** |
| 2588 | ** The recommended test is X==2. If the return value is 2, that means |
| 2589 | ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the |
| 2590 | ** default setting. If the return value is 1, then ALWAYS() is either |
| 2591 | ** hard-coded to true or else it asserts if its argument is false. |
| 2592 | ** The first behavior (hard-coded to true) is the case if |
| 2593 | ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second |
| 2594 | ** behavior (assert if the argument to ALWAYS() is false) is the case if |
| 2595 | ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled. |
| 2596 | ** |
| 2597 | ** The run-time test procedure might look something like this: |
| 2598 | ** |
| 2599 | ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){ |
| 2600 | ** // ALWAYS() and NEVER() are no-op pass-through macros |
| 2601 | ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){ |
| 2602 | ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false. |
| 2603 | ** }else{ |
| 2604 | ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. |
| 2605 | ** } |
| 2606 | */ |
| 2607 | case SQLITE_TESTCTRL_ALWAYS: { |
| 2608 | int x = va_arg(ap,int); |
| 2609 | rc = ALWAYS(x); |
| 2610 | break; |
| 2611 | } |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 2612 | |
| 2613 | /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) |
| 2614 | ** |
| 2615 | ** Set the nReserve size to N for the main database on the database |
| 2616 | ** connection db. |
| 2617 | */ |
| 2618 | case SQLITE_TESTCTRL_RESERVE: { |
| 2619 | sqlite3 *db = va_arg(ap, sqlite3*); |
| 2620 | int x = va_arg(ap,int); |
| 2621 | sqlite3_mutex_enter(db->mutex); |
| 2622 | sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); |
| 2623 | sqlite3_mutex_leave(db->mutex); |
| 2624 | break; |
| 2625 | } |
| 2626 | |
drh | 07096f6 | 2009-12-22 23:52:32 +0000 | [diff] [blame] | 2627 | /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) |
| 2628 | ** |
| 2629 | ** Enable or disable various optimizations for testing purposes. The |
| 2630 | ** argument N is a bitmask of optimizations to be disabled. For normal |
| 2631 | ** operation N should be 0. The idea is that a test program (like the |
| 2632 | ** SQL Logic Test or SLT test module) can run the same SQL multiple times |
| 2633 | ** with various optimizations disabled to verify that the same answer |
| 2634 | ** is obtained in every case. |
| 2635 | */ |
| 2636 | case SQLITE_TESTCTRL_OPTIMIZATIONS: { |
| 2637 | sqlite3 *db = va_arg(ap, sqlite3*); |
| 2638 | int x = va_arg(ap,int); |
| 2639 | db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask); |
| 2640 | break; |
| 2641 | } |
| 2642 | |
drh | 0e85773 | 2010-01-02 03:21:35 +0000 | [diff] [blame] | 2643 | #ifdef SQLITE_N_KEYWORD |
| 2644 | /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) |
| 2645 | ** |
| 2646 | ** If zWord is a keyword recognized by the parser, then return the |
| 2647 | ** number of keywords. Or if zWord is not a keyword, return 0. |
| 2648 | ** |
| 2649 | ** This test feature is only available in the amalgamation since |
| 2650 | ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite |
| 2651 | ** is built using separate source files. |
| 2652 | */ |
| 2653 | case SQLITE_TESTCTRL_ISKEYWORD: { |
| 2654 | const char *zWord = va_arg(ap, const char*); |
| 2655 | int n = sqlite3Strlen30(zWord); |
drh | 855787a | 2010-01-02 03:46:43 +0000 | [diff] [blame] | 2656 | rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; |
drh | 0e85773 | 2010-01-02 03:21:35 +0000 | [diff] [blame] | 2657 | break; |
| 2658 | } |
| 2659 | #endif |
| 2660 | |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2661 | } |
| 2662 | va_end(ap); |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 2663 | #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
danielk1977 | 7c36d07 | 2008-01-31 15:31:01 +0000 | [diff] [blame] | 2664 | return rc; |
drh | ed13d98 | 2008-01-31 14:43:24 +0000 | [diff] [blame] | 2665 | } |