Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 0dd9926fec46b85f5c61b0fb90f8469370465a1f [file] [log] [blame]
drh75897232000-05-29 14:26:001/*
drhb19a2bc2001-09-16 00:13:262** 2001 September 15
drh75897232000-05-29 14:26:003**
drhb19a2bc2001-09-16 00:13:264** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:006**
drhb19a2bc2001-09-16 00:13:267** 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.
drh75897232000-05-29 14:26:0010**
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.
drh75897232000-05-29 14:26:0016*/
17#include "sqliteInt.h"
drh58f1c8b2008-05-26 20:19:2518
drh820a9062008-01-31 13:35:4819#ifdef SQLITE_ENABLE_FTS3
20# include "fts3.h"
21#endif
drh58f1c8b2008-05-26 20:19:2522#ifdef SQLITE_ENABLE_RTREE
23# include "rtree.h"
24#endif
dan21540ae2017-12-08 16:23:3825#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
danielk19771c826652008-09-08 08:08:0926# include "sqliteicu.h"
27#endif
drh20e34f92020-05-04 17:15:2128
29/*
30** This is an extension initializer that is a no-op and always
31** succeeds, except that it fails if the fault-simulation is set
32** to 500.
33*/
34static int sqlite3TestExtInit(sqlite3 *db){
35 (void)db;
36 return sqlite3FaultSim(500);
37}
38
39
40/*
41** Forward declarations of external module initializer functions
42** for modules that need them.
43*/
drh20e34f92020-05-04 17:15:2144#ifdef SQLITE_ENABLE_FTS5
45int sqlite3Fts5Init(sqlite3*);
46#endif
drhc6603af2017-06-29 14:33:5147#ifdef SQLITE_ENABLE_STMTVTAB
48int sqlite3StmtVtabInit(sqlite3*);
drhf00f5302017-06-28 15:47:2949#endif
stephan95484722023-08-17 09:49:5350#ifdef SQLITE_EXTRA_AUTOEXT
51int SQLITE_EXTRA_AUTOEXT(sqlite3*);
52#endif
drh20e34f92020-05-04 17:15:2153/*
54** An array of pointers to extension initializer functions for
55** built-in extensions.
56*/
57static int (*const sqlite3BuiltinExtensions[])(sqlite3*) = {
drh20e34f92020-05-04 17:15:2158#ifdef SQLITE_ENABLE_FTS3
59 sqlite3Fts3Init,
60#endif
61#ifdef SQLITE_ENABLE_FTS5
62 sqlite3Fts5Init,
63#endif
64#if defined(SQLITE_ENABLE_ICU) || defined(SQLITE_ENABLE_ICU_COLLATIONS)
65 sqlite3IcuInit,
66#endif
67#ifdef SQLITE_ENABLE_RTREE
68 sqlite3RtreeInit,
69#endif
70#ifdef SQLITE_ENABLE_DBPAGE_VTAB
71 sqlite3DbpageRegister,
72#endif
73#ifdef SQLITE_ENABLE_DBSTAT_VTAB
74 sqlite3DbstatRegister,
75#endif
76 sqlite3TestExtInit,
drh9dbf96b2022-01-06 01:40:0977#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
78 sqlite3JsonTableFunctions,
drh20e34f92020-05-04 17:15:2179#endif
80#ifdef SQLITE_ENABLE_STMTVTAB
81 sqlite3StmtVtabInit,
82#endif
83#ifdef SQLITE_ENABLE_BYTECODE_VTAB
84 sqlite3VdbeBytecodeVtabInit,
85#endif
stephan95484722023-08-17 09:49:5386#ifdef SQLITE_EXTRA_AUTOEXT
87 SQLITE_EXTRA_AUTOEXT,
88#endif
drh20e34f92020-05-04 17:15:2189};
drh75897232000-05-29 14:26:0090
drhb3190c12008-12-08 21:37:1491#ifndef SQLITE_AMALGAMATION
drh9f129f42010-08-31 15:27:3292/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
drh8adc8f12020-07-24 11:01:2993** contains the text of SQLITE_VERSION macro.
drh9f129f42010-08-31 15:27:3294*/
danielk197724b03fd2004-05-10 10:34:3495const char sqlite3_version[] = SQLITE_VERSION;
drhb3190c12008-12-08 21:37:1496#endif
drh9f129f42010-08-31 15:27:3297
98/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
larrybrbc917382023-06-07 08:40:3199** a pointer to the to the sqlite3_version[] string constant.
drh9f129f42010-08-31 15:27:32100*/
drh4aec8b62004-08-28 16:19:00101const char *sqlite3_libversion(void){ return sqlite3_version; }
drh9f129f42010-08-31 15:27:32102
drhc6aa3812017-10-04 18:26:44103/* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
drh9f129f42010-08-31 15:27:32104** pointer to a string constant whose value is the same as the
drhc6aa3812017-10-04 18:26:44105** SQLITE_SOURCE_ID C preprocessor macro. Except if SQLite is built using
106** an edited copy of the amalgamation, then the last four characters of
107** the hash might be different from SQLITE_SOURCE_ID.
drh9f129f42010-08-31 15:27:32108*/
drh47baebc2009-08-14 16:01:24109const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
drh9f129f42010-08-31 15:27:32110
111/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
112** returns an integer equal to SQLITE_VERSION_NUMBER.
113*/
danielk197799ba19e2005-02-05 07:33:34114int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drh9f129f42010-08-31 15:27:32115
drh5dc2bcd2012-01-02 15:45:12116/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
drhb8a45bb2011-12-31 21:51:55117** zero if and only if SQLite was compiled with mutexing code omitted due to
drh9f129f42010-08-31 15:27:32118** the SQLITE_THREADSAFE compile-time option being set to 0.
119*/
drhb67e8bf2007-08-30 20:09:48120int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
drhb217a572000-08-22 13:40:18121
mistachkin34cf2582015-04-02 17:46:52122/*
123** When compiling the test fixture or with debugging enabled (on Win32),
124** this variable being set to non-zero will cause OSTRACE macros to emit
125** extra diagnostic information.
126*/
mistachkinfb383e92015-04-16 03:24:38127#ifdef SQLITE_HAVE_OS_TRACE
mistachkin34cf2582015-04-02 17:46:52128# ifndef SQLITE_DEBUG_OS_TRACE
129# define SQLITE_DEBUG_OS_TRACE 0
130# endif
131 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
132#endif
133
drhe265b082008-05-01 17:03:49134#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
drhb217a572000-08-22 13:40:18135/*
drhb0603412007-02-28 04:47:26136** If the following function pointer is not NULL and if
137** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
138** I/O active are written using this function. These messages
139** are intended for debugging activity only.
140*/
mistachkin9871a932015-03-27 00:21:52141SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
drhe265b082008-05-01 17:03:49142#endif
drhb0603412007-02-28 04:47:26143
144/*
drha16313e2007-03-30 11:29:32145** If the following global variable points to a string which is the
146** name of a directory, then that directory will be used to store
147** temporary files.
148**
149** See also the "PRAGMA temp_store_directory" SQL command.
150*/
151char *sqlite3_temp_directory = 0;
152
drhc5499be2008-04-01 15:06:33153/*
mistachkina112d142012-03-14 00:44:01154** If the following global variable points to a string which is the
155** name of a directory, then that directory will be used to store
156** all database files specified with a relative pathname.
157**
158** See also the "PRAGMA data_store_directory" SQL command.
159*/
160char *sqlite3_data_directory = 0;
161
drh5b5d4492023-09-13 20:06:46162/*
larrybrbc917382023-06-07 08:40:31163** Initialize SQLite.
drh40257ff2008-06-13 18:24:27164**
165** This routine must be called to initialize the memory allocation,
drh93ed56d2008-08-12 15:21:11166** VFS, and mutex subsystems prior to doing any serious work with
drh40257ff2008-06-13 18:24:27167** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
168** this routine will be called automatically by key routines such as
larrybrbc917382023-06-07 08:40:31169** sqlite3_open().
drh40257ff2008-06-13 18:24:27170**
171** This routine is a no-op except on its very first call for the process,
172** or for the first call after a call to sqlite3_shutdown.
drh93ed56d2008-08-12 15:21:11173**
174** The first thread to call this routine runs the initialization to
175** completion. If subsequent threads call this routine before the first
176** thread has finished the initialization process, then the subsequent
177** threads must block until the first thread finishes with the initialization.
178**
179** The first thread might call this routine recursively. Recursive
180** calls to this routine should not block, of course. Otherwise the
181** initialization process would never complete.
182**
183** Let X be the first thread to enter this routine. Let Y be some other
184** thread. Then while the initial invocation of this routine by X is
185** incomplete, it is required that:
186**
187** * Calls to this routine from Y must block until the outer-most
188** call by X completes.
189**
190** * Recursive calls to this routine from thread X return immediately
191** without blocking.
drh40257ff2008-06-13 18:24:27192*/
193int sqlite3_initialize(void){
drh067b92b2020-06-19 15:24:12194 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) /* The main static mutex */
danielk1977075c23a2008-09-01 18:34:20195 int rc; /* Result code */
drhab80be92013-08-08 14:38:45196#ifdef SQLITE_EXTRA_INIT
197 int bRunExtraInit = 0; /* Extra initialization needed */
198#endif
danielk1977075c23a2008-09-01 18:34:20199
200#ifdef SQLITE_OMIT_WSD
danielk1977a8f83bf2008-09-02 16:22:28201 rc = sqlite3_wsd_init(4096, 24);
danielk1977075c23a2008-09-01 18:34:20202 if( rc!=SQLITE_OK ){
203 return rc;
204 }
205#endif
danielk197771bc31c2008-06-26 08:29:34206
drh2b4905c2015-03-23 18:52:56207 /* If the following assert() fails on some obscure processor/compiler
208 ** combination, the work-around is to set the correct pointer
209 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
210 assert( SQLITE_PTRSIZE==sizeof(char*) );
211
drh93ed56d2008-08-12 15:21:11212 /* If SQLite is already completely initialized, then this call
213 ** to sqlite3_initialize() should be a no-op. But the initialization
214 ** must be complete. So isInit must not be set until the very end
215 ** of this routine.
216 */
drhffd3fd02020-01-29 13:10:50217 if( sqlite3GlobalConfig.isInit ){
218 sqlite3MemoryBarrier();
219 return SQLITE_OK;
220 }
danielk197771bc31c2008-06-26 08:29:34221
larrybrbc917382023-06-07 08:40:31222 /* Make sure the mutex subsystem is initialized. If unable to
drh93ed56d2008-08-12 15:21:11223 ** initialize the mutex subsystem, return early with the error.
224 ** If the system is so sick that we are unable to allocate a mutex,
225 ** there is not much SQLite is going to be able to do.
226 **
227 ** The mutex subsystem must take care of serializing its own
228 ** initialization.
229 */
danielk1977d0251742008-06-18 18:57:42230 rc = sqlite3MutexInit();
drh93ed56d2008-08-12 15:21:11231 if( rc ) return rc;
danielk197771bc31c2008-06-26 08:29:34232
drh93ed56d2008-08-12 15:21:11233 /* Initialize the malloc() system and the recursive pInitMutex mutex.
drhccb21132020-06-19 11:34:57234 ** This operation is protected by the STATIC_MAIN mutex. Note that
drh93ed56d2008-08-12 15:21:11235 ** MutexAlloc() is called for a static mutex prior to initializing the
236 ** malloc subsystem - this implies that the allocation of a static
237 ** mutex must not require support from the malloc subsystem.
238 */
drh067b92b2020-06-19 15:24:12239 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
240 sqlite3_mutex_enter(pMainMtx);
dane1ab2192009-08-17 15:16:19241 sqlite3GlobalConfig.isMutexInit = 1;
danielk1977075c23a2008-09-01 18:34:20242 if( !sqlite3GlobalConfig.isMallocInit ){
drh93ed56d2008-08-12 15:21:11243 rc = sqlite3MallocInit();
244 }
drh40257ff2008-06-13 18:24:27245 if( rc==SQLITE_OK ){
danielk1977075c23a2008-09-01 18:34:20246 sqlite3GlobalConfig.isMallocInit = 1;
247 if( !sqlite3GlobalConfig.pInitMutex ){
drh9ac06502009-08-17 13:42:29248 sqlite3GlobalConfig.pInitMutex =
249 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
danielk1977075c23a2008-09-01 18:34:20250 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
mistachkinfad30392016-02-13 23:43:46251 rc = SQLITE_NOMEM_BKPT;
drh40257ff2008-06-13 18:24:27252 }
253 }
danielk197777eb5bb2008-09-22 17:22:19254 }
255 if( rc==SQLITE_OK ){
danielk1977075c23a2008-09-01 18:34:20256 sqlite3GlobalConfig.nRefInitMutex++;
drh93ed56d2008-08-12 15:21:11257 }
drh067b92b2020-06-19 15:24:12258 sqlite3_mutex_leave(pMainMtx);
danielk197771bc31c2008-06-26 08:29:34259
dane1ab2192009-08-17 15:16:19260 /* If rc is not SQLITE_OK at this point, then either the malloc
261 ** subsystem could not be initialized or the system failed to allocate
262 ** the pInitMutex mutex. Return an error in either case. */
drh93ed56d2008-08-12 15:21:11263 if( rc!=SQLITE_OK ){
264 return rc;
265 }
266
267 /* Do the rest of the initialization under the recursive mutex so
268 ** that we will be able to handle recursive calls into
269 ** sqlite3_initialize(). The recursive calls normally come through
270 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
271 ** recursive calls might also be possible.
drhf759bb82010-09-09 18:25:34272 **
273 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
274 ** to the xInit method, so the xInit method need not be threadsafe.
275 **
276 ** The following mutex is what serializes access to the appdef pcache xInit
277 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
278 ** call to sqlite3PcacheInitialize().
drh93ed56d2008-08-12 15:21:11279 */
danielk1977075c23a2008-09-01 18:34:20280 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
danielk1977502b4e02008-09-02 14:07:24281 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
danielk1977502b4e02008-09-02 14:07:24282 sqlite3GlobalConfig.inProgress = 1;
drh5e3cefe2015-11-12 23:48:08283#ifdef SQLITE_ENABLE_SQLLOG
dand83f7ca2015-11-12 20:12:51284 {
drh5e3cefe2015-11-12 23:48:08285 extern void sqlite3_init_sqllog(void);
286 sqlite3_init_sqllog();
dand83f7ca2015-11-12 20:12:51287 }
288#endif
drh80738d92016-02-15 00:34:16289 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions));
290 sqlite3RegisterBuiltinFunctions();
dane1ab2192009-08-17 15:16:19291 if( sqlite3GlobalConfig.isPCacheInit==0 ){
292 rc = sqlite3PcacheInitialize();
293 }
danielk19778c0a7912008-08-20 14:49:23294 if( rc==SQLITE_OK ){
dane1ab2192009-08-17 15:16:19295 sqlite3GlobalConfig.isPCacheInit = 1;
dan3d6e0602009-08-17 15:52:25296 rc = sqlite3OsInit();
drh0bf9f7b2009-04-17 16:54:22297 }
drh8d889af2021-05-08 17:18:23298#ifndef SQLITE_OMIT_DESERIALIZE
drhac442f42018-01-03 01:28:46299 if( rc==SQLITE_OK ){
300 rc = sqlite3MemdbInit();
301 }
302#endif
drh0bf9f7b2009-04-17 16:54:22303 if( rc==SQLITE_OK ){
larrybrbc917382023-06-07 08:40:31304 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
danielk19775b775292008-09-02 09:38:06305 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
danfd3938f2025-02-27 11:03:54306#ifdef SQLITE_EXTRA_INIT_MUTEXED
307 {
308 int SQLITE_EXTRA_INIT_MUTEXED(const char*);
309 rc = SQLITE_EXTRA_INIT_MUTEXED(0);
310 }
311#endif
312 }
313 if( rc==SQLITE_OK ){
drhc4842882020-05-16 16:23:48314 sqlite3MemoryBarrier();
drh0bf9f7b2009-04-17 16:54:22315 sqlite3GlobalConfig.isInit = 1;
drhab80be92013-08-08 14:38:45316#ifdef SQLITE_EXTRA_INIT
317 bRunExtraInit = 1;
318#endif
danielk19778c0a7912008-08-20 14:49:23319 }
danielk1977502b4e02008-09-02 14:07:24320 sqlite3GlobalConfig.inProgress = 0;
drh40257ff2008-06-13 18:24:27321 }
danielk1977075c23a2008-09-01 18:34:20322 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
shanedfb7b372008-07-22 05:13:30323
drh93ed56d2008-08-12 15:21:11324 /* Go back under the static mutex and clean up the recursive
325 ** mutex to prevent a resource leak.
326 */
drh067b92b2020-06-19 15:24:12327 sqlite3_mutex_enter(pMainMtx);
danielk1977075c23a2008-09-01 18:34:20328 sqlite3GlobalConfig.nRefInitMutex--;
329 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
330 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
331 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
332 sqlite3GlobalConfig.pInitMutex = 0;
drh93ed56d2008-08-12 15:21:11333 }
drh067b92b2020-06-19 15:24:12334 sqlite3_mutex_leave(pMainMtx);
drh93ed56d2008-08-12 15:21:11335
336 /* The following is just a sanity check to make sure SQLite has
337 ** been compiled correctly. It is important to run this code, but
338 ** we don't want to run it too often and soak up CPU cycles for no
339 ** reason. So we run it once during initialization.
340 */
shanedfb7b372008-07-22 05:13:30341#ifndef NDEBUG
shanefbd60f82009-02-04 03:59:25342#ifndef SQLITE_OMIT_FLOATING_POINT
shanedfb7b372008-07-22 05:13:30343 /* This section of code's only "output" is via assert() statements. */
drhac442f42018-01-03 01:28:46344 if( rc==SQLITE_OK ){
shanedfb7b372008-07-22 05:13:30345 u64 x = (((u64)1)<<63)-1;
346 double y;
347 assert(sizeof(x)==8);
348 assert(sizeof(x)==sizeof(y));
349 memcpy(&y, &x, 8);
350 assert( sqlite3IsNaN(y) );
351 }
352#endif
shanefbd60f82009-02-04 03:59:25353#endif
shanedfb7b372008-07-22 05:13:30354
drhe4cf0b32011-08-25 00:14:41355 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
356 ** compile-time option.
357 */
358#ifdef SQLITE_EXTRA_INIT
drhab80be92013-08-08 14:38:45359 if( bRunExtraInit ){
drhc7f94622011-12-13 18:22:38360 int SQLITE_EXTRA_INIT(const char*);
361 rc = SQLITE_EXTRA_INIT(0);
drhe4cf0b32011-08-25 00:14:41362 }
363#endif
drh40257ff2008-06-13 18:24:27364 return rc;
365}
366
367/*
368** Undo the effects of sqlite3_initialize(). Must not be called while
369** there are outstanding database connections or memory allocations or
370** while any part of SQLite is otherwise in use in any thread. This
drhd1a24402009-04-19 12:23:58371** routine is not threadsafe. But it is safe to invoke this routine
372** on when SQLite is already shut down. If SQLite is already shut down
373** when this routine is invoked, then this routine is a harmless no-op.
drh40257ff2008-06-13 18:24:27374*/
375int sqlite3_shutdown(void){
mistachkin054450f2014-12-23 20:42:48376#ifdef SQLITE_OMIT_WSD
377 int rc = sqlite3_wsd_init(4096, 24);
378 if( rc!=SQLITE_OK ){
379 return rc;
380 }
381#endif
382
danielk1977075c23a2008-09-01 18:34:20383 if( sqlite3GlobalConfig.isInit ){
drh97977062011-12-13 01:34:21384#ifdef SQLITE_EXTRA_SHUTDOWN
385 void SQLITE_EXTRA_SHUTDOWN(void);
386 SQLITE_EXTRA_SHUTDOWN();
387#endif
drha7640922009-04-21 12:02:56388 sqlite3_os_end();
drhd1a24402009-04-19 12:23:58389 sqlite3_reset_auto_extension();
drhd1a24402009-04-19 12:23:58390 sqlite3GlobalConfig.isInit = 0;
danielk1977fb437272008-07-08 12:02:35391 }
dane1ab2192009-08-17 15:16:19392 if( sqlite3GlobalConfig.isPCacheInit ){
393 sqlite3PcacheShutdown();
394 sqlite3GlobalConfig.isPCacheInit = 0;
395 }
396 if( sqlite3GlobalConfig.isMallocInit ){
397 sqlite3MallocEnd();
398 sqlite3GlobalConfig.isMallocInit = 0;
mistachkin86f89872012-03-18 01:32:44399
400#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
401 /* The heap subsystem has now been shutdown and these values are supposed
402 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
403 ** which would rely on that heap subsystem; therefore, make sure these
404 ** values cannot refer to heap memory that was just invalidated when the
405 ** heap subsystem was shutdown. This is only done if the current call to
406 ** this function resulted in the heap subsystem actually being shutdown.
407 */
408 sqlite3_data_directory = 0;
409 sqlite3_temp_directory = 0;
410#endif
dane1ab2192009-08-17 15:16:19411 }
412 if( sqlite3GlobalConfig.isMutexInit ){
413 sqlite3MutexEnd();
414 sqlite3GlobalConfig.isMutexInit = 0;
415 }
416
drh40257ff2008-06-13 18:24:27417 return SQLITE_OK;
418}
419
420/*
421** This API allows applications to modify the global configuration of
422** the SQLite library at run-time.
423**
424** This routine should only be called when there are no outstanding
425** database connections or memory allocations. This routine is not
426** threadsafe. Failure to heed these warnings can lead to unpredictable
427** behavior.
428*/
429int sqlite3_config(int op, ...){
430 va_list ap;
431 int rc = SQLITE_OK;
432
drhad96db82023-02-23 14:22:29433 /* sqlite3_config() normally returns SQLITE_MISUSE if it is invoked while
434 ** the SQLite library is in use. Except, a few selected opcodes
435 ** are allowed.
436 */
437 if( sqlite3GlobalConfig.isInit ){
438 static const u64 mAnytimeConfigOption = 0
439 | MASKBIT64( SQLITE_CONFIG_LOG )
drhad96db82023-02-23 14:22:29440 | MASKBIT64( SQLITE_CONFIG_PCACHE_HDRSZ )
441 ;
442 if( op<0 || op>63 || (MASKBIT64(op) & mAnytimeConfigOption)==0 ){
443 return SQLITE_MISUSE_BKPT;
444 }
445 testcase( op==SQLITE_CONFIG_LOG );
drhad96db82023-02-23 14:22:29446 testcase( op==SQLITE_CONFIG_PCACHE_HDRSZ );
447 }
drh40257ff2008-06-13 18:24:27448
449 va_start(ap, op);
450 switch( op ){
drh18472fa2008-10-07 15:25:48451
452 /* Mutex configuration options are only available in a threadsafe
drhd1dcb232014-11-01 18:32:18453 ** compile.
drh18472fa2008-10-07 15:25:48454 */
drhd1dcb232014-11-01 18:32:18455#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
drh40257ff2008-06-13 18:24:27456 case SQLITE_CONFIG_SINGLETHREAD: {
drh682a6ef2015-03-04 23:14:14457 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
458 ** Single-thread. */
459 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
460 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
drh40257ff2008-06-13 18:24:27461 break;
462 }
drhd1dcb232014-11-01 18:32:18463#endif
464#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
drh40257ff2008-06-13 18:24:27465 case SQLITE_CONFIG_MULTITHREAD: {
drh682a6ef2015-03-04 23:14:14466 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
467 ** Multi-thread. */
468 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
469 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
drh40257ff2008-06-13 18:24:27470 break;
471 }
drhd1dcb232014-11-01 18:32:18472#endif
473#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
drh40257ff2008-06-13 18:24:27474 case SQLITE_CONFIG_SERIALIZED: {
drh682a6ef2015-03-04 23:14:14475 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
476 ** Serialized. */
477 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
478 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
drh40257ff2008-06-13 18:24:27479 break;
480 }
drhd1dcb232014-11-01 18:32:18481#endif
482#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
drh18472fa2008-10-07 15:25:48483 case SQLITE_CONFIG_MUTEX: {
484 /* Specify an alternative mutex implementation */
485 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
486 break;
487 }
drhd1dcb232014-11-01 18:32:18488#endif
489#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
drh18472fa2008-10-07 15:25:48490 case SQLITE_CONFIG_GETMUTEX: {
491 /* Retrieve the current mutex implementation */
492 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
493 break;
494 }
495#endif
496
drh40257ff2008-06-13 18:24:27497 case SQLITE_CONFIG_MALLOC: {
drh5279d342014-11-04 13:41:32498 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
499 ** single argument which is a pointer to an instance of the
500 ** sqlite3_mem_methods structure. The argument specifies alternative
501 ** low-level memory allocation routines to be used in place of the memory
502 ** allocation routines built into SQLite. */
danielk1977075c23a2008-09-01 18:34:20503 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
drh40257ff2008-06-13 18:24:27504 break;
505 }
drh33589792008-06-18 13:27:46506 case SQLITE_CONFIG_GETMALLOC: {
drh5279d342014-11-04 13:41:32507 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
508 ** single argument which is a pointer to an instance of the
509 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
510 ** filled with the currently defined memory allocation routines. */
danielk1977075c23a2008-09-01 18:34:20511 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
512 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
drh33589792008-06-18 13:27:46513 break;
514 }
drhfec00ea2008-06-14 16:56:21515 case SQLITE_CONFIG_MEMSTATUS: {
drhad96db82023-02-23 14:22:29516 assert( !sqlite3GlobalConfig.isInit ); /* Cannot change at runtime */
drh5279d342014-11-04 13:41:32517 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
518 ** single argument of type int, interpreted as a boolean, which enables
519 ** or disables the collection of memory allocation statistics. */
520 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
drh40257ff2008-06-13 18:24:27521 break;
522 }
drhb2a0f752017-08-28 15:51:35523 case SQLITE_CONFIG_SMALL_MALLOC: {
524 sqlite3GlobalConfig.bSmallMalloc = va_arg(ap, int);
drh33589792008-06-18 13:27:46525 break;
526 }
527 case SQLITE_CONFIG_PAGECACHE: {
drh15427272015-12-03 22:33:55528 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
529 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
530 ** the size of each page cache line (sz), and the number of cache lines
531 ** (N). */
danielk1977075c23a2008-09-01 18:34:20532 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
533 sqlite3GlobalConfig.szPage = va_arg(ap, int);
534 sqlite3GlobalConfig.nPage = va_arg(ap, int);
drh33589792008-06-18 13:27:46535 break;
536 }
drhdef68892014-11-04 12:11:23537 case SQLITE_CONFIG_PCACHE_HDRSZ: {
drh5279d342014-11-04 13:41:32538 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
539 ** a single parameter which is a pointer to an integer and writes into
540 ** that integer the number of extra bytes per page required for each page
541 ** in SQLITE_CONFIG_PAGECACHE. */
larrybrbc917382023-06-07 08:40:31542 *va_arg(ap, int*) =
drhdef68892014-11-04 12:11:23543 sqlite3HeaderSizeBtree() +
544 sqlite3HeaderSizePcache() +
545 sqlite3HeaderSizePcache1();
546 break;
547 }
danielk19775099be52008-06-27 13:27:03548
danielk1977bc2ca9e2008-11-13 14:28:28549 case SQLITE_CONFIG_PCACHE: {
dan22e21ff2011-11-08 20:08:44550 /* no-op */
551 break;
552 }
553 case SQLITE_CONFIG_GETPCACHE: {
554 /* now an error */
555 rc = SQLITE_ERROR;
danielk1977bc2ca9e2008-11-13 14:28:28556 break;
557 }
558
dan22e21ff2011-11-08 20:08:44559 case SQLITE_CONFIG_PCACHE2: {
drh5279d342014-11-04 13:41:32560 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
561 ** single argument which is a pointer to an sqlite3_pcache_methods2
562 ** object. This object specifies the interface to a custom page cache
563 ** implementation. */
dan22e21ff2011-11-08 20:08:44564 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
565 break;
566 }
567 case SQLITE_CONFIG_GETPCACHE2: {
drh5279d342014-11-04 13:41:32568 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
569 ** single argument which is a pointer to an sqlite3_pcache_methods2
570 ** object. SQLite copies of the current page cache implementation into
571 ** that object. */
dan22e21ff2011-11-08 20:08:44572 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
drhb232c232008-11-19 01:20:26573 sqlite3PCacheSetDefault();
574 }
dan22e21ff2011-11-08 20:08:44575 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
drhb232c232008-11-19 01:20:26576 break;
577 }
578
drh8790b6e2014-11-07 01:43:56579/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
580** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
581** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
drh5d414832008-07-15 14:47:18582#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
drh33589792008-06-18 13:27:46583 case SQLITE_CONFIG_HEAP: {
drh5279d342014-11-04 13:41:32584 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
585 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
drh3ba689d2015-03-03 14:00:11586 ** number of bytes in the memory buffer, and the minimum allocation size.
587 */
danielk1977075c23a2008-09-01 18:34:20588 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
589 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
590 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
danielk19775099be52008-06-27 13:27:03591
shaneha6ec8922011-03-09 21:36:17592 if( sqlite3GlobalConfig.mnReq<1 ){
593 sqlite3GlobalConfig.mnReq = 1;
594 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
595 /* cap min request size at 2^12 */
596 sqlite3GlobalConfig.mnReq = (1<<12);
597 }
598
danielk1977075c23a2008-09-01 18:34:20599 if( sqlite3GlobalConfig.pHeap==0 ){
drh8790b6e2014-11-07 01:43:56600 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
601 ** is NULL, then SQLite reverts to using its default memory allocator
602 ** (the system malloc() implementation), undoing any prior invocation of
603 ** SQLITE_CONFIG_MALLOC.
604 **
605 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
606 ** revert to its default implementation when sqlite3_initialize() is run
drh8a42cbd2008-07-10 18:13:42607 */
danielk1977075c23a2008-09-01 18:34:20608 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
drh8a42cbd2008-07-10 18:13:42609 }else{
drh8790b6e2014-11-07 01:43:56610 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
611 ** alternative memory allocator is engaged to handle all of SQLites
612 ** memory allocation needs. */
danielk19775099be52008-06-27 13:27:03613#ifdef SQLITE_ENABLE_MEMSYS3
danielk1977075c23a2008-09-01 18:34:20614 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
drh8a42cbd2008-07-10 18:13:42615#endif
616#ifdef SQLITE_ENABLE_MEMSYS5
danielk1977075c23a2008-09-01 18:34:20617 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
drh8a42cbd2008-07-10 18:13:42618#endif
drh8a42cbd2008-07-10 18:13:42619 }
danielk19775099be52008-06-27 13:27:03620 break;
621 }
drh5d414832008-07-15 14:47:18622#endif
danielk19775099be52008-06-27 13:27:03623
drh633e6d52008-07-28 19:34:53624 case SQLITE_CONFIG_LOOKASIDE: {
danielk1977075c23a2008-09-01 18:34:20625 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
626 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
drh633e6d52008-07-28 19:34:53627 break;
628 }
larrybrbc917382023-06-07 08:40:31629
mistachkin037933b2013-08-13 22:33:41630 /* Record a pointer to the logger function and its first argument.
drh3f280702010-02-18 18:45:09631 ** The default is NULL. Logging is disabled if the function pointer is
632 ** NULL.
633 */
634 case SQLITE_CONFIG_LOG: {
shanehdc97a8c2010-02-23 20:08:35635 /* MSVC is picky about pulling func ptrs from va lists.
636 ** http://support.microsoft.com/kb/47961
637 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
638 */
639 typedef void(*LOGFUNC_t)(void*,int,const char*);
drhad96db82023-02-23 14:22:29640 LOGFUNC_t xLog = va_arg(ap, LOGFUNC_t);
641 void *pLogArg = va_arg(ap, void*);
642 AtomicStore(&sqlite3GlobalConfig.xLog, xLog);
643 AtomicStore(&sqlite3GlobalConfig.pLogArg, pLogArg);
drh3f280702010-02-18 18:45:09644 break;
645 }
drh633e6d52008-07-28 19:34:53646
drh00729cb2014-10-04 11:59:33647 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
648 ** can be changed at start-time using the
649 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
650 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
651 */
dancd74b612011-04-22 19:37:32652 case SQLITE_CONFIG_URI: {
drh4d9f1882014-11-04 17:23:24653 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
654 ** argument of type int. If non-zero, then URI handling is globally
655 ** enabled. If the parameter is zero, then URI handling is globally
656 ** disabled. */
drhad96db82023-02-23 14:22:29657 int bOpenUri = va_arg(ap, int);
658 AtomicStore(&sqlite3GlobalConfig.bOpenUri, bOpenUri);
dancd74b612011-04-22 19:37:32659 break;
660 }
661
drhde9a7b82012-09-17 20:44:46662 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
drh4d9f1882014-11-04 17:23:24663 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
664 ** option takes a single integer argument which is interpreted as a
665 ** boolean in order to enable or disable the use of covering indices for
666 ** full table scans in the query optimizer. */
drhde9a7b82012-09-17 20:44:46667 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
668 break;
669 }
670
danac455932012-11-26 19:50:41671#ifdef SQLITE_ENABLE_SQLLOG
672 case SQLITE_CONFIG_SQLLOG: {
673 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
674 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
675 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
676 break;
677 }
678#endif
679
drh9b4c59f2013-04-15 17:03:42680 case SQLITE_CONFIG_MMAP_SIZE: {
drh4d9f1882014-11-04 17:23:24681 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
682 ** integer (sqlite3_int64) values that are the default mmap size limit
683 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
684 ** mmap size limit. */
drh9b4c59f2013-04-15 17:03:42685 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
drha1f42c72013-04-01 22:38:06686 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
drh4d9f1882014-11-04 17:23:24687 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
drh8790b6e2014-11-07 01:43:56688 ** negative, then that argument is changed to its compile-time default.
689 **
690 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
691 ** silently truncated if necessary so that it does not exceed the
692 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
693 ** compile-time option.
694 */
drh3ba689d2015-03-03 14:00:11695 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
696 mxMmap = SQLITE_MAX_MMAP_SIZE;
697 }
drh9b4c59f2013-04-15 17:03:42698 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
699 if( szMmap>mxMmap) szMmap = mxMmap;
drh4d9f1882014-11-04 17:23:24700 sqlite3GlobalConfig.mxMmap = mxMmap;
drh9b4c59f2013-04-15 17:03:42701 sqlite3GlobalConfig.szMmap = szMmap;
drha1f42c72013-04-01 22:38:06702 break;
703 }
704
drh4d9f1882014-11-04 17:23:24705#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
mistachkinaf8641b2013-11-25 21:49:04706 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
drh4d9f1882014-11-04 17:23:24707 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
708 ** unsigned integer value that specifies the maximum size of the created
709 ** heap. */
mistachkinac1f1042013-11-23 00:27:29710 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
711 break;
712 }
713#endif
714
drh3bd17912015-01-02 15:55:29715 case SQLITE_CONFIG_PMASZ: {
716 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
717 break;
718 }
719
drh8c71a982016-03-07 17:37:37720 case SQLITE_CONFIG_STMTJRNL_SPILL: {
721 sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int);
722 break;
723 }
724
dan2e3a5a82018-04-16 21:12:42725#ifdef SQLITE_ENABLE_SORTER_REFERENCES
drhbbade8d2018-04-18 14:48:08726 case SQLITE_CONFIG_SORTERREF_SIZE: {
dan2e3a5a82018-04-16 21:12:42727 int iVal = va_arg(ap, int);
728 if( iVal<0 ){
729 iVal = SQLITE_DEFAULT_SORTERREF_SIZE;
730 }
731 sqlite3GlobalConfig.szSorterRef = (u32)iVal;
dan2e3a5a82018-04-16 21:12:42732 break;
733 }
drhbbade8d2018-04-18 14:48:08734#endif /* SQLITE_ENABLE_SORTER_REFERENCES */
dan2e3a5a82018-04-16 21:12:42735
drh8d889af2021-05-08 17:18:23736#ifndef SQLITE_OMIT_DESERIALIZE
drh23a88592019-01-31 15:38:53737 case SQLITE_CONFIG_MEMDB_MAXSIZE: {
738 sqlite3GlobalConfig.mxMemdbSize = va_arg(ap, sqlite3_int64);
739 break;
740 }
drh8d889af2021-05-08 17:18:23741#endif /* SQLITE_OMIT_DESERIALIZE */
drh23a88592019-01-31 15:38:53742
drh254729e2024-03-19 23:01:56743 case SQLITE_CONFIG_ROWID_IN_VIEW: {
744 int *pVal = va_arg(ap,int*);
drh261c6df2024-03-19 13:55:10745#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
drh254729e2024-03-19 23:01:56746 if( 0==*pVal ) sqlite3GlobalConfig.mNoVisibleRowid = TF_NoVisibleRowid;
747 if( 1==*pVal ) sqlite3GlobalConfig.mNoVisibleRowid = 0;
748 *pVal = (sqlite3GlobalConfig.mNoVisibleRowid==0);
749#else
750 *pVal = 0;
drh261c6df2024-03-19 13:55:10751#endif
752 break;
753 }
754
drh40257ff2008-06-13 18:24:27755 default: {
756 rc = SQLITE_ERROR;
757 break;
758 }
759 }
760 va_end(ap);
761 return rc;
762}
763
764/*
drh633e6d52008-07-28 19:34:53765** Set up the lookaside buffers for a database connection.
larrybrbc917382023-06-07 08:40:31766** Return SQLITE_OK on success.
drh633e6d52008-07-28 19:34:53767** If lookaside is already active, return SQLITE_BUSY.
drhe9d1c722008-08-04 20:13:26768**
769** The sz parameter is the number of bytes in each lookaside slot.
drh56d2fd02025-02-17 14:16:49770** The cnt parameter is the number of slots. If pBuf is NULL the
771** space for the lookaside memory is obtained from sqlite3_malloc()
772** or similar. If pBuf is not NULL then it is sz*cnt bytes of memory
773** to use for the lookaside memory.
drh633e6d52008-07-28 19:34:53774*/
drh56d2fd02025-02-17 14:16:49775static int setupLookaside(
776 sqlite3 *db, /* Database connection being configured */
777 void *pBuf, /* Memory to use for lookaside. May be NULL */
778 int sz, /* Desired size of each lookaside memory slot */
779 int cnt /* Number of slots to allocate */
780){
drh7877d9a2015-07-23 17:16:27781#ifndef SQLITE_OMIT_LOOKASIDE
drh56d2fd02025-02-17 14:16:49782 void *pStart; /* Start of the lookaside buffer */
783 sqlite3_int64 szAlloc; /* Total space set aside for lookaside memory */
784 int nBig; /* Number of full-size slots */
785 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
larrybrbc917382023-06-07 08:40:31786
drh52fb8e12017-08-29 20:21:12787 if( sqlite3LookasideUsed(db,0)>0 ){
drh633e6d52008-07-28 19:34:53788 return SQLITE_BUSY;
789 }
shanef71f89e2009-01-30 06:11:54790 /* Free any existing lookaside buffer for this handle before
larrybrbc917382023-06-07 08:40:31791 ** allocating a new one so we don't have to have space for
shanef71f89e2009-01-30 06:11:54792 ** both at the same time.
793 */
794 if( db->lookaside.bMalloced ){
795 sqlite3_free(db->lookaside.pStart);
796 }
drh61a4bd52011-11-10 02:39:28797 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
drh56d2fd02025-02-17 14:16:49798 ** than a pointer and small enough to fit in a u16.
shanef71f89e2009-01-30 06:11:54799 */
drh56d2fd02025-02-17 14:16:49800 sz = ROUNDDOWN8(sz);
drh1d34fde2009-02-03 15:50:33801 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
drh56d2fd02025-02-17 14:16:49802 if( sz>65528 ) sz = 65528;
803 /* Count must be at least 1 to be useful, but not so large as to use
804 ** more than 0x7fff0000 total bytes for lookaside. */
805 if( cnt<1 ) cnt = 0;
806 if( sz>0 && cnt>(0x7fff0000/sz) ) cnt = 0x7fff0000/sz;
807 szAlloc = (i64)sz*(i64)cnt;
808 if( szAlloc==0 ){
shanef71f89e2009-01-30 06:11:54809 sz = 0;
810 pStart = 0;
811 }else if( pBuf==0 ){
drhe9d1c722008-08-04 20:13:26812 sqlite3BeginBenignMalloc();
drh56d2fd02025-02-17 14:16:49813 pStart = sqlite3Malloc( szAlloc );
drhe9d1c722008-08-04 20:13:26814 sqlite3EndBenignMalloc();
drhe6068022019-12-13 15:48:21815 if( pStart ) szAlloc = sqlite3MallocSize(pStart);
drhe9d1c722008-08-04 20:13:26816 }else{
817 pStart = pBuf;
818 }
drhcf014f62019-12-31 15:12:34819#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
820 if( sz>=LOOKASIDE_SMALL*3 ){
821 nBig = szAlloc/(3*LOOKASIDE_SMALL+sz);
drh56d2fd02025-02-17 14:16:49822 nSm = (szAlloc - (i64)sz*(i64)nBig)/LOOKASIDE_SMALL;
drhcf014f62019-12-31 15:12:34823 }else if( sz>=LOOKASIDE_SMALL*2 ){
824 nBig = szAlloc/(LOOKASIDE_SMALL+sz);
drh56d2fd02025-02-17 14:16:49825 nSm = (szAlloc - (i64)sz*(i64)nBig)/LOOKASIDE_SMALL;
drhe6068022019-12-13 15:48:21826 }else
drhcf014f62019-12-31 15:12:34827#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
drhe6068022019-12-13 15:48:21828 if( sz>0 ){
829 nBig = szAlloc/sz;
830 nSm = 0;
831 }else{
832 nBig = nSm = 0;
833 }
drh4cfb22f2008-08-01 18:47:01834 db->lookaside.pStart = pStart;
drh52fb8e12017-08-29 20:21:12835 db->lookaside.pInit = 0;
drh4cfb22f2008-08-01 18:47:01836 db->lookaside.pFree = 0;
drh1bd10f82008-12-10 21:19:56837 db->lookaside.sz = (u16)sz;
drh31f69622019-10-05 14:39:36838 db->lookaside.szTrue = (u16)sz;
drh633e6d52008-07-28 19:34:53839 if( pStart ){
840 int i;
841 LookasideSlot *p;
drhde467982009-04-02 17:22:41842 assert( sz > (int)sizeof(LookasideSlot*) );
drh633e6d52008-07-28 19:34:53843 p = (LookasideSlot*)pStart;
drhe6068022019-12-13 15:48:21844 for(i=0; i<nBig; i++){
drh52fb8e12017-08-29 20:21:12845 p->pNext = db->lookaside.pInit;
846 db->lookaside.pInit = p;
drh633e6d52008-07-28 19:34:53847 p = (LookasideSlot*)&((u8*)p)[sz];
848 }
drhcf014f62019-12-31 15:12:34849#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
850 db->lookaside.pSmallInit = 0;
851 db->lookaside.pSmallFree = 0;
numist115d6632019-12-12 02:50:07852 db->lookaside.pMiddle = p;
drhe6068022019-12-13 15:48:21853 for(i=0; i<nSm; i++){
drhcf014f62019-12-31 15:12:34854 p->pNext = db->lookaside.pSmallInit;
855 db->lookaside.pSmallInit = p;
856 p = (LookasideSlot*)&((u8*)p)[LOOKASIDE_SMALL];
numist115d6632019-12-12 02:50:07857 }
drhcf014f62019-12-31 15:12:34858#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
drhe6068022019-12-13 15:48:21859 assert( ((uptr)p)<=szAlloc + (uptr)pStart );
drh633e6d52008-07-28 19:34:53860 db->lookaside.pEnd = p;
drh4a642b62016-02-05 01:55:27861 db->lookaside.bDisable = 0;
shanef71f89e2009-01-30 06:11:54862 db->lookaside.bMalloced = pBuf==0 ?1:0;
drhe6068022019-12-13 15:48:21863 db->lookaside.nSlot = nBig+nSm;
drh4cfb22f2008-08-01 18:47:01864 }else{
drh376860b2022-08-22 15:18:37865 db->lookaside.pStart = 0;
drhcf014f62019-12-31 15:12:34866#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
867 db->lookaside.pSmallInit = 0;
868 db->lookaside.pSmallFree = 0;
drh376860b2022-08-22 15:18:37869 db->lookaside.pMiddle = 0;
drhcf014f62019-12-31 15:12:34870#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
drh376860b2022-08-22 15:18:37871 db->lookaside.pEnd = 0;
drh4a642b62016-02-05 01:55:27872 db->lookaside.bDisable = 1;
drh31f69622019-10-05 14:39:36873 db->lookaside.sz = 0;
shanef71f89e2009-01-30 06:11:54874 db->lookaside.bMalloced = 0;
drh52fb8e12017-08-29 20:21:12875 db->lookaside.nSlot = 0;
drh633e6d52008-07-28 19:34:53876 }
drh376860b2022-08-22 15:18:37877 db->lookaside.pTrueEnd = db->lookaside.pEnd;
drhe6068022019-12-13 15:48:21878 assert( sqlite3LookasideUsed(db,0)==0 );
drh7877d9a2015-07-23 17:16:27879#endif /* SQLITE_OMIT_LOOKASIDE */
drh633e6d52008-07-28 19:34:53880 return SQLITE_OK;
881}
882
883/*
drh4413d0e2008-11-04 13:46:27884** Return the mutex associated with a database connection.
885*/
886sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
drh9ca95732014-10-24 00:35:58887#ifdef SQLITE_ENABLE_API_ARMOR
888 if( !sqlite3SafetyCheckOk(db) ){
889 (void)SQLITE_MISUSE_BKPT;
890 return 0;
891 }
892#endif
drh4413d0e2008-11-04 13:46:27893 return db->mutex;
894}
895
896/*
drh09419b42011-11-16 19:29:17897** Free up as much memory as we can from the given database
898** connection.
899*/
900int sqlite3_db_release_memory(sqlite3 *db){
901 int i;
drh9ca95732014-10-24 00:35:58902
903#ifdef SQLITE_ENABLE_API_ARMOR
904 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
905#endif
dand9bb3a92011-12-30 11:43:59906 sqlite3_mutex_enter(db->mutex);
drh09419b42011-11-16 19:29:17907 sqlite3BtreeEnterAll(db);
908 for(i=0; i<db->nDb; i++){
909 Btree *pBt = db->aDb[i].pBt;
910 if( pBt ){
911 Pager *pPager = sqlite3BtreePager(pBt);
912 sqlite3PagerShrink(pPager);
913 }
914 }
915 sqlite3BtreeLeaveAll(db);
dand9bb3a92011-12-30 11:43:59916 sqlite3_mutex_leave(db->mutex);
drh09419b42011-11-16 19:29:17917 return SQLITE_OK;
918}
919
920/*
dan6fa255f2015-10-28 19:46:57921** Flush any dirty pages in the pager-cache for any attached database
922** to disk.
923*/
924int sqlite3_db_cacheflush(sqlite3 *db){
925 int i;
926 int rc = SQLITE_OK;
927 int bSeenBusy = 0;
928
929#ifdef SQLITE_ENABLE_API_ARMOR
930 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
931#endif
932 sqlite3_mutex_enter(db->mutex);
933 sqlite3BtreeEnterAll(db);
934 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
935 Btree *pBt = db->aDb[i].pBt;
drh99744fa2020-08-25 19:09:07936 if( pBt && sqlite3BtreeTxnState(pBt)==SQLITE_TXN_WRITE ){
dan6fa255f2015-10-28 19:46:57937 Pager *pPager = sqlite3BtreePager(pBt);
938 rc = sqlite3PagerFlush(pPager);
939 if( rc==SQLITE_BUSY ){
940 bSeenBusy = 1;
941 rc = SQLITE_OK;
942 }
943 }
944 }
dan6fa255f2015-10-28 19:46:57945 sqlite3BtreeLeaveAll(db);
946 sqlite3_mutex_leave(db->mutex);
947 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc);
948}
949
950/*
drh633e6d52008-07-28 19:34:53951** Configuration settings for an individual database connection
952*/
953int sqlite3_db_config(sqlite3 *db, int op, ...){
954 va_list ap;
drh6480aad2008-08-01 16:31:14955 int rc;
stephan7dc0cc42023-10-13 12:48:35956
957#ifdef SQLITE_ENABLE_API_ARMOR
958 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
959#endif
drh7383f5a2022-08-24 17:59:00960 sqlite3_mutex_enter(db->mutex);
drh633e6d52008-07-28 19:34:53961 va_start(ap, op);
962 switch( op ){
drhda84dca2016-08-18 22:44:22963 case SQLITE_DBCONFIG_MAINDBNAME: {
drh749e4a92017-07-14 19:47:32964 /* IMP: R-06824-28531 */
965 /* IMP: R-36257-52125 */
drhda84dca2016-08-18 22:44:22966 db->aDb[0].zDbSName = va_arg(ap,char*);
967 rc = SQLITE_OK;
968 break;
969 }
drhe9d1c722008-08-04 20:13:26970 case SQLITE_DBCONFIG_LOOKASIDE: {
drh8b2b2e62011-04-07 01:14:12971 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
drh9dd55f52010-09-03 03:32:46972 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
973 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
drhe9d1c722008-08-04 20:13:26974 rc = setupLookaside(db, pBuf, sz, cnt);
drh633e6d52008-07-28 19:34:53975 break;
976 }
drh6480aad2008-08-01 16:31:14977 default: {
drhe83cafd2011-03-21 17:15:58978 static const struct {
979 int op; /* The opcode */
drhc850c2b2025-01-22 19:37:47980 u64 mask; /* Mask of the bit in sqlite3.flags to set/clear */
drhe83cafd2011-03-21 17:15:58981 } aFlagOp[] = {
drhd42908f2016-02-26 15:38:24982 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
983 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
drh11d88e62019-08-15 21:27:20984 { SQLITE_DBCONFIG_ENABLE_VIEW, SQLITE_EnableView },
drhd42908f2016-02-26 15:38:24985 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer },
drh191dd062016-04-21 01:30:09986 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension },
dan298af022016-10-31 16:16:49987 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose },
drh169dd922017-06-26 13:57:49988 { SQLITE_DBCONFIG_ENABLE_QPSG, SQLITE_EnableQPSG },
drh36e31c62017-12-21 18:23:26989 { SQLITE_DBCONFIG_TRIGGER_EQP, SQLITE_TriggerEQP },
drh7df01192018-04-28 12:43:16990 { SQLITE_DBCONFIG_RESET_DATABASE, SQLITE_ResetDatabase },
drha296cda2018-11-03 16:09:59991 { SQLITE_DBCONFIG_DEFENSIVE, SQLITE_Defensive },
drh346f4e22019-03-25 21:35:41992 { SQLITE_DBCONFIG_WRITABLE_SCHEMA, SQLITE_WriteSchema|
993 SQLITE_NoSchemaError },
drh0a6873b2019-06-14 21:25:25994 { SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, SQLITE_LegacyAlter },
drhd0ff6012019-06-17 13:56:11995 { SQLITE_DBCONFIG_DQS_DDL, SQLITE_DqsDDL },
996 { SQLITE_DBCONFIG_DQS_DML, SQLITE_DqsDML },
drh66c48902019-10-29 16:18:45997 { SQLITE_DBCONFIG_LEGACY_FILE_FORMAT, SQLITE_LegacyFileFmt },
drhb77da372020-01-07 16:09:11998 { SQLITE_DBCONFIG_TRUSTED_SCHEMA, SQLITE_TrustedSchema },
dan06382de2023-02-28 20:04:01999 { SQLITE_DBCONFIG_STMT_SCANSTATUS, SQLITE_StmtScanStatus },
drhf30da222023-03-06 19:04:391000 { SQLITE_DBCONFIG_REVERSE_SCANORDER, SQLITE_ReverseOrder },
drhc850c2b2025-01-22 19:37:471001 { SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE, SQLITE_AttachCreate },
1002 { SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE, SQLITE_AttachWrite },
drhe16b3452025-01-31 01:34:191003 { SQLITE_DBCONFIG_ENABLE_COMMENTS, SQLITE_Comments },
drhe83cafd2011-03-21 17:15:581004 };
drh58ad5802011-03-23 22:02:231005 unsigned int i;
drh9dd55f52010-09-03 03:32:461006 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
drhe83cafd2011-03-21 17:15:581007 for(i=0; i<ArraySize(aFlagOp); i++){
1008 if( aFlagOp[i].op==op ){
1009 int onoff = va_arg(ap, int);
1010 int *pRes = va_arg(ap, int*);
drh70d5dfb2018-12-06 16:50:551011 u64 oldFlags = db->flags;
drhe83cafd2011-03-21 17:15:581012 if( onoff>0 ){
1013 db->flags |= aFlagOp[i].mask;
1014 }else if( onoff==0 ){
drhd5b44d62018-12-06 17:06:021015 db->flags &= ~(u64)aFlagOp[i].mask;
drhe83cafd2011-03-21 17:15:581016 }
1017 if( oldFlags!=db->flags ){
drhba968db2018-07-24 22:02:121018 sqlite3ExpirePreparedStatements(db, 0);
drhe83cafd2011-03-21 17:15:581019 }
1020 if( pRes ){
1021 *pRes = (db->flags & aFlagOp[i].mask)!=0;
1022 }
1023 rc = SQLITE_OK;
1024 break;
1025 }
1026 }
drh6480aad2008-08-01 16:31:141027 break;
1028 }
drh633e6d52008-07-28 19:34:531029 }
1030 va_end(ap);
drh7383f5a2022-08-24 17:59:001031 sqlite3_mutex_leave(db->mutex);
drh633e6d52008-07-28 19:34:531032 return rc;
1033}
1034
drh9b5adfa2008-01-20 23:19:561035/*
drhd3d39e92004-05-20 22:16:291036** This is the default collating function named "BINARY" which is always
1037** available.
drhd3d39e92004-05-20 22:16:291038*/
danielk19772c336542005-01-13 02:14:231039static int binCollFunc(
drh821afa42019-06-14 13:24:461040 void *NotUsed,
drhd3d39e92004-05-20 22:16:291041 int nKey1, const void *pKey1,
1042 int nKey2, const void *pKey2
1043){
1044 int rc, n;
drh821afa42019-06-14 13:24:461045 UNUSED_PARAMETER(NotUsed);
drhd3d39e92004-05-20 22:16:291046 n = nKey1<nKey2 ? nKey1 : nKey2;
drh5e3b49b2014-11-20 19:22:261047 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
1048 ** strings byte by byte using the memcmp() function from the standard C
1049 ** library. */
dan21766c02017-05-22 08:04:091050 assert( pKey1 && pKey2 );
drhd3d39e92004-05-20 22:16:291051 rc = memcmp(pKey1, pKey2, n);
1052 if( rc==0 ){
drh821afa42019-06-14 13:24:461053 rc = nKey1 - nKey2;
drhd3d39e92004-05-20 22:16:291054 }
1055 return rc;
1056}
1057
1058/*
drh821afa42019-06-14 13:24:461059** This is the collating function named "RTRIM" which is always
1060** available. Ignore trailing spaces.
1061*/
1062static int rtrimCollFunc(
1063 void *pUser,
1064 int nKey1, const void *pKey1,
1065 int nKey2, const void *pKey2
1066){
1067 const u8 *pK1 = (const u8*)pKey1;
1068 const u8 *pK2 = (const u8*)pKey2;
1069 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
1070 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
1071 return binCollFunc(pUser, nKey1, pKey1, nKey2, pKey2);
1072}
1073
1074/*
drhbcd15932018-07-27 18:12:401075** Return true if CollSeq is the default built-in BINARY.
1076*/
1077int sqlite3IsBinary(const CollSeq *p){
drh821afa42019-06-14 13:24:461078 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
1079 return p==0 || p->xCmp==binCollFunc;
drhbcd15932018-07-27 18:12:401080}
1081
1082/*
larrybrbc917382023-06-07 08:40:311083** Another built-in collating sequence: NOCASE.
danielk1977dc1bdc42004-06-11 10:51:271084**
drhf7b54962013-05-28 12:11:541085** This collating sequence is intended to be used for "case independent
danielk1977dc1bdc42004-06-11 10:51:271086** comparison". SQLite's knowledge of upper and lower case equivalents
1087** extends only to the 26 characters used in the English language.
1088**
1089** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:161090*/
1091static int nocaseCollatingFunc(
1092 void *NotUsed,
1093 int nKey1, const void *pKey1,
1094 int nKey2, const void *pKey2
1095){
1096 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:581097 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk197762c14b32008-11-19 09:05:261098 UNUSED_PARAMETER(NotUsed);
danielk19770202b292004-06-09 09:55:161099 if( 0==r ){
1100 r = nKey1-nKey2;
1101 }
1102 return r;
1103}
1104
1105/*
drhaf9ff332002-01-16 21:00:271106** Return the ROWID of the most recent insert
1107*/
drh9bb575f2004-09-06 17:24:111108sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drh9ca95732014-10-24 00:35:581109#ifdef SQLITE_ENABLE_API_ARMOR
1110 if( !sqlite3SafetyCheckOk(db) ){
1111 (void)SQLITE_MISUSE_BKPT;
1112 return 0;
1113 }
1114#endif
drhaf9ff332002-01-16 21:00:271115 return db->lastRowid;
1116}
1117
1118/*
dan9c58b632017-02-27 14:52:481119** Set the value returned by the sqlite3_last_insert_rowid() API function.
1120*/
1121void sqlite3_set_last_insert_rowid(sqlite3 *db, sqlite3_int64 iRowid){
1122#ifdef SQLITE_ENABLE_API_ARMOR
1123 if( !sqlite3SafetyCheckOk(db) ){
1124 (void)SQLITE_MISUSE_BKPT;
1125 return;
1126 }
1127#endif
1128 sqlite3_mutex_enter(db->mutex);
1129 db->lastRowid = iRowid;
1130 sqlite3_mutex_leave(db->mutex);
1131}
1132
1133/*
danielk197724b03fd2004-05-10 10:34:341134** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:591135*/
dan2c718872021-06-22 18:32:051136sqlite3_int64 sqlite3_changes64(sqlite3 *db){
drh9ca95732014-10-24 00:35:581137#ifdef SQLITE_ENABLE_API_ARMOR
1138 if( !sqlite3SafetyCheckOk(db) ){
1139 (void)SQLITE_MISUSE_BKPT;
1140 return 0;
1141 }
1142#endif
drhc8d30ac2002-04-12 10:08:591143 return db->nChange;
1144}
dan2c718872021-06-22 18:32:051145int sqlite3_changes(sqlite3 *db){
1146 return (int)sqlite3_changes64(db);
1147}
drhc8d30ac2002-04-12 10:08:591148
rdcf146a772004-02-25 22:51:061149/*
danielk1977b28af712004-06-21 06:50:261150** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:061151*/
dan2c718872021-06-22 18:32:051152sqlite3_int64 sqlite3_total_changes64(sqlite3 *db){
drh9ca95732014-10-24 00:35:581153#ifdef SQLITE_ENABLE_API_ARMOR
1154 if( !sqlite3SafetyCheckOk(db) ){
1155 (void)SQLITE_MISUSE_BKPT;
1156 return 0;
1157 }
1158#endif
danielk1977b28af712004-06-21 06:50:261159 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:381160}
dan2c718872021-06-22 18:32:051161int sqlite3_total_changes(sqlite3 *db){
1162 return (int)sqlite3_total_changes64(db);
1163}
rdcb0c374f2004-02-20 22:53:381164
drhc8d30ac2002-04-12 10:08:591165/*
danielk1977fd7f0452008-12-17 17:30:261166** Close all open savepoints. This function only manipulates fields of the
1167** database handle object, it does not close any savepoints that may be open
1168** at the b-tree/pager level.
1169*/
1170void sqlite3CloseSavepoints(sqlite3 *db){
1171 while( db->pSavepoint ){
1172 Savepoint *pTmp = db->pSavepoint;
1173 db->pSavepoint = pTmp->pNext;
1174 sqlite3DbFree(db, pTmp);
1175 }
1176 db->nSavepoint = 0;
danielk1977bd434552009-03-18 10:33:001177 db->nStatement = 0;
danielk1977fd7f0452008-12-17 17:30:261178 db->isTransactionSavepoint = 0;
1179}
1180
1181/*
dand2199f02010-08-27 17:48:521182** Invoke the destructor function associated with FuncDef p, if any. Except,
1183** if this is not the last copy of the function, do not invoke it. Multiple
1184** copies of a single function are created when create_function() is called
1185** with SQLITE_ANY as the encoding.
1186*/
1187static void functionDestroy(sqlite3 *db, FuncDef *p){
drhf9751072021-10-07 13:40:291188 FuncDestructor *pDestructor;
1189 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
1190 pDestructor = p->u.pDestructor;
dand2199f02010-08-27 17:48:521191 if( pDestructor ){
1192 pDestructor->nRef--;
1193 if( pDestructor->nRef==0 ){
1194 pDestructor->xDestroy(pDestructor->pUserData);
1195 sqlite3DbFree(db, pDestructor);
1196 }
1197 }
1198}
1199
1200/*
danbba02a92012-05-15 17:15:341201** Disconnect all sqlite3_vtab objects that belong to database connection
1202** db. This is called when db is being closed.
1203*/
1204static void disconnectAllVtab(sqlite3 *db){
1205#ifndef SQLITE_OMIT_VIRTUALTABLE
1206 int i;
drh51be3872015-08-19 02:32:251207 HashElem *p;
danbba02a92012-05-15 17:15:341208 sqlite3BtreeEnterAll(db);
1209 for(i=0; i<db->nDb; i++){
1210 Schema *pSchema = db->aDb[i].pSchema;
mistachkin6389a7b2018-08-08 20:46:351211 if( pSchema ){
danbba02a92012-05-15 17:15:341212 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
1213 Table *pTab = (Table *)sqliteHashData(p);
1214 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
1215 }
1216 }
1217 }
drh51be3872015-08-19 02:32:251218 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
1219 Module *pMod = (Module *)sqliteHashData(p);
1220 if( pMod->pEpoTab ){
1221 sqlite3VtabDisconnect(db, pMod->pEpoTab);
1222 }
1223 }
dand88e5212014-03-12 19:38:381224 sqlite3VtabUnlockList(db);
danbba02a92012-05-15 17:15:341225 sqlite3BtreeLeaveAll(db);
1226#else
1227 UNUSED_PARAMETER(db);
1228#endif
1229}
1230
1231/*
drh167cd6a2012-06-02 17:09:461232** Return TRUE if database connection db has unfinalized prepared
larrybrbc917382023-06-07 08:40:311233** statements or unfinished sqlite3_backup objects.
drh167cd6a2012-06-02 17:09:461234*/
1235static int connectionIsBusy(sqlite3 *db){
1236 int j;
1237 assert( sqlite3_mutex_held(db->mutex) );
1238 if( db->pVdbe ) return 1;
1239 for(j=0; j<db->nDb; j++){
1240 Btree *pBt = db->aDb[j].pBt;
1241 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
1242 }
1243 return 0;
1244}
1245
1246/*
drh50e5dad2001-09-15 00:57:281247** Close an existing SQLite database
1248*/
drh167cd6a2012-06-02 17:09:461249static int sqlite3Close(sqlite3 *db, int forceZombie){
danielk19775c4c7782004-06-16 10:39:231250 if( !db ){
drhddb17ca2014-08-11 15:54:111251 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
1252 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
danielk197796d81f92004-06-19 03:33:571253 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:231254 }
drh7e8b8482008-01-23 03:03:051255 if( !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:561256 return SQLITE_MISUSE_BKPT;
danielk1977e35ee192004-06-26 09:50:111257 }
drh605264d2007-08-21 15:13:191258 sqlite3_mutex_enter(db->mutex);
drh3d2a5292016-07-13 22:55:011259 if( db->mTrace & SQLITE_TRACE_CLOSE ){
drh08b92082020-08-10 14:18:001260 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
drh3d2a5292016-07-13 22:55:011261 }
danielk1977e35ee192004-06-26 09:50:111262
danbba02a92012-05-15 17:15:341263 /* Force xDisconnect calls on all virtual tables */
1264 disconnectAllVtab(db);
danielk1977a04a34f2007-04-16 15:06:251265
danbba02a92012-05-15 17:15:341266 /* If a transaction is open, the disconnectAllVtab() call above
danielk197701256832007-04-18 14:24:321267 ** will not have called the xDisconnect() method on any virtual
1268 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
1269 ** call will do so. We need to do this before the check for active
1270 ** SQL statements below, as the v-table implementation may be storing
1271 ** some prepared statements internally.
1272 */
dan67430812013-05-15 10:21:501273 sqlite3VtabRollback(db);
danielk197701256832007-04-18 14:24:321274
drh167cd6a2012-06-02 17:09:461275 /* Legacy behavior (sqlite3_close() behavior) is to return
1276 ** SQLITE_BUSY if the connection can not be closed immediately.
1277 */
1278 if( !forceZombie && connectionIsBusy(db) ){
drh13f40da2014-08-22 18:00:111279 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
drh167cd6a2012-06-02 17:09:461280 "statements or unfinished backups");
drh27641702007-08-22 02:56:421281 sqlite3_mutex_leave(db->mutex);
danielk197796d81f92004-06-19 03:33:571282 return SQLITE_BUSY;
1283 }
danielk1977e0048402004-06-15 16:51:011284
danac455932012-11-26 19:50:411285#ifdef SQLITE_ENABLE_SQLLOG
1286 if( sqlite3GlobalConfig.xSqllog ){
dan71ba10d2012-11-27 10:56:391287 /* Closing the handle. Fourth parameter is passed the value 2. */
1288 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
danac455932012-11-26 19:50:411289 }
1290#endif
1291
drh10deb352023-08-30 15:20:151292 while( db->pDbData ){
1293 DbClientData *p = db->pDbData;
1294 db->pDbData = p->pNext;
1295 assert( p->pData!=0 );
1296 if( p->xDestructor ) p->xDestructor(p->pData);
1297 sqlite3_free(p);
1298 }
1299
drh167cd6a2012-06-02 17:09:461300 /* Convert the connection into a zombie and then close it.
drh4245c402012-06-02 14:32:211301 */
drh5f9de6e2021-08-07 23:16:521302 db->eOpenState = SQLITE_STATE_ZOMBIE;
drh4245c402012-06-02 14:32:211303 sqlite3LeaveMutexAndCloseZombie(db);
1304 return SQLITE_OK;
1305}
drh94e92032003-02-16 22:21:321306
drh167cd6a2012-06-02 17:09:461307/*
drh99744fa2020-08-25 19:09:071308** Return the transaction state for a single databse, or the maximum
1309** transaction state over all attached databases if zSchema is null.
1310*/
1311int sqlite3_txn_state(sqlite3 *db, const char *zSchema){
1312 int iDb, nDb;
1313 int iTxn = -1;
1314#ifdef SQLITE_ENABLE_API_ARMOR
1315 if( !sqlite3SafetyCheckOk(db) ){
1316 (void)SQLITE_MISUSE_BKPT;
1317 return -1;
1318 }
1319#endif
1320 sqlite3_mutex_enter(db->mutex);
1321 if( zSchema ){
1322 nDb = iDb = sqlite3FindDbName(db, zSchema);
1323 if( iDb<0 ) nDb--;
1324 }else{
1325 iDb = 0;
1326 nDb = db->nDb-1;
1327 }
1328 for(; iDb<=nDb; iDb++){
1329 Btree *pBt = db->aDb[iDb].pBt;
1330 int x = pBt!=0 ? sqlite3BtreeTxnState(pBt) : SQLITE_TXN_NONE;
1331 if( x>iTxn ) iTxn = x;
1332 }
1333 sqlite3_mutex_leave(db->mutex);
1334 return iTxn;
1335}
1336
1337/*
drh167cd6a2012-06-02 17:09:461338** Two variations on the public interface for closing a database
1339** connection. The sqlite3_close() version returns SQLITE_BUSY and
drh1d6d7372021-04-12 11:51:321340** leaves the connection open if there are unfinalized prepared
drh167cd6a2012-06-02 17:09:461341** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
1342** version forces the connection to become a zombie if there are
1343** unclosed resources, and arranges for deallocation when the last
1344** prepare statement or sqlite3_backup closes.
1345*/
1346int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
1347int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
1348
drh4245c402012-06-02 14:32:211349
1350/*
1351** Close the mutex on database connection db.
1352**
1353** Furthermore, if database connection db is a zombie (meaning that there
drh167cd6a2012-06-02 17:09:461354** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
1355** every sqlite3_stmt has now been finalized and every sqlite3_backup has
1356** finished, then free all resources.
drh4245c402012-06-02 14:32:211357*/
1358void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
1359 HashElem *i; /* Hash table iterator */
1360 int j;
1361
drh4245c402012-06-02 14:32:211362 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
drh167cd6a2012-06-02 17:09:461363 ** or if the connection has not yet been closed by sqlite3_close_v2(),
1364 ** then just leave the mutex and return.
drh4245c402012-06-02 14:32:211365 */
drh5f9de6e2021-08-07 23:16:521366 if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){
drh4245c402012-06-02 14:32:211367 sqlite3_mutex_leave(db->mutex);
1368 return;
danielk197704103022009-02-03 16:51:241369 }
1370
drh4245c402012-06-02 14:32:211371 /* If we reach this point, it means that the database connection has
1372 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
mistachkin48864df2013-03-21 21:20:321373 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
drh4245c402012-06-02 14:32:211374 ** go ahead and free all resources.
1375 */
1376
dan617dc862013-05-16 11:57:281377 /* If a transaction is open, roll it back. This also ensures that if
1378 ** any database schemas have been modified by an uncommitted transaction
1379 ** they are reset. And that the required b-tree mutex is held to make
1380 ** the pager rollback and schema reset an atomic operation. */
1381 sqlite3RollbackAll(db, SQLITE_OK);
1382
danielk1977fd7f0452008-12-17 17:30:261383 /* Free any outstanding Savepoint structures. */
1384 sqlite3CloseSavepoints(db);
1385
drh5efb3142012-05-16 01:24:341386 /* Close all database connections */
drh001bbcb2003-03-19 03:14:001387 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:381388 struct Db *pDb = &db->aDb[j];
1389 if( pDb->pBt ){
drhebdb81d2014-12-05 15:31:331390 sqlite3BtreeClose(pDb->pBt);
1391 pDb->pBt = 0;
1392 if( j!=1 ){
danielk1977311019b2006-01-10 07:14:231393 pDb->pSchema = 0;
1394 }
drh113088e2003-03-20 01:16:581395 }
drhf57b3392001-10-08 13:22:321396 }
drh5efb3142012-05-16 01:24:341397 /* Clear the TEMP schema separately and last */
1398 if( db->aDb[1].pSchema ){
1399 sqlite3SchemaClear(db->aDb[1].pSchema);
1400 }
1401 sqlite3VtabUnlockList(db);
danbba02a92012-05-15 17:15:341402
drh5efb3142012-05-16 01:24:341403 /* Free up the array of auxiliary databases */
1404 sqlite3CollapseDatabaseArray(db);
danbba02a92012-05-15 17:15:341405 assert( db->nDb<=2 );
1406 assert( db->aDb==db->aDbStatic );
danielk1977404ca072009-03-16 13:19:361407
1408 /* Tell the code in notify.c that the connection no longer holds any
1409 ** locks and does not require any further unlock-notify callbacks.
1410 */
1411 sqlite3ConnectionClosed(db);
1412
drh80738d92016-02-15 00:34:161413 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
1414 FuncDef *pNext, *p;
1415 p = sqliteHashData(i);
1416 do{
1417 functionDestroy(db, p);
1418 pNext = p->pNext;
1419 sqlite3DbFree(db, p);
1420 p = pNext;
1421 }while( p );
drh8e0a2f92002-02-23 23:45:451422 }
drh80738d92016-02-15 00:34:161423 sqlite3HashClear(&db->aFunc);
danielk1977d8123362004-06-12 09:25:121424 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:011425 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977a9808b32007-05-07 09:32:451426 /* Invoke any destructors registered for collation sequence user data. */
1427 for(j=0; j<3; j++){
1428 if( pColl[j].xDel ){
1429 pColl[j].xDel(pColl[j].pUser);
1430 }
1431 }
drh633e6d52008-07-28 19:34:531432 sqlite3DbFree(db, pColl);
danielk1977466be562004-06-10 02:16:011433 }
danielk1977d8123362004-06-12 09:25:121434 sqlite3HashClear(&db->aCollSeq);
drhb9bb7c12006-06-11 23:41:551435#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:131436 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
1437 Module *pMod = (Module *)sqliteHashData(i);
drh51be3872015-08-19 02:32:251438 sqlite3VtabEponymousTableClear(db, pMod);
drhcc5979d2019-08-16 22:58:291439 sqlite3VtabModuleUnref(db, pMod);
danielk1977d1ab1ba2006-06-15 04:28:131440 }
drhb9bb7c12006-06-11 23:41:551441 sqlite3HashClear(&db->aModule);
1442#endif
danielk1977466be562004-06-10 02:16:011443
drh13f40da2014-08-22 18:00:111444 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
drha3cc0072013-12-13 16:23:551445 sqlite3ValueFree(db->pErr);
drhf1952c52006-06-08 15:48:001446 sqlite3CloseExtensions(db);
danielk197796d81f92004-06-19 03:33:571447
drh5f9de6e2021-08-07 23:16:521448 db->eOpenState = SQLITE_STATE_ERROR;
danielk1977311019b2006-01-10 07:14:231449
1450 /* The temp-database schema is allocated differently from the other schema
1451 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
1452 ** So it needs to be freed here. Todo: Why not roll the temp schema into
larrybrbc917382023-06-07 08:40:311453 ** the same sqliteMalloc() as the one that allocates the database
danielk1977311019b2006-01-10 07:14:231454 ** structure?
1455 */
drh633e6d52008-07-28 19:34:531456 sqlite3DbFree(db, db->aDb[1].pSchema);
drh1bbfc672021-10-15 23:02:271457 if( db->xAutovacDestr ){
1458 db->xAutovacDestr(db->pAutovacPagesArg);
1459 }
drh605264d2007-08-21 15:13:191460 sqlite3_mutex_leave(db->mutex);
drh5f9de6e2021-08-07 23:16:521461 db->eOpenState = SQLITE_STATE_CLOSED;
drh605264d2007-08-21 15:13:191462 sqlite3_mutex_free(db->mutex);
drh52fb8e12017-08-29 20:21:121463 assert( sqlite3LookasideUsed(db,0)==0 );
drhe9d1c722008-08-04 20:13:261464 if( db->lookaside.bMalloced ){
1465 sqlite3_free(db->lookaside.pStart);
1466 }
drh17435752007-08-16 04:30:381467 sqlite3_free(db);
drh75897232000-05-29 14:26:001468}
1469
1470/*
drh0f198a72012-02-13 16:43:161471** Rollback all database files. If tripCode is not SQLITE_OK, then
drh47b7fc72014-11-11 01:33:571472** any write cursors are invalidated ("tripped" - as in "tripping a circuit
drh0f198a72012-02-13 16:43:161473** breaker") and made to return tripCode if there are any further
drh47b7fc72014-11-11 01:33:571474** attempts to use that cursor. Read cursors remain open and valid
1475** but are "saved" in case the table pages are moved around.
drh001bbcb2003-03-19 03:14:001476*/
drh0f198a72012-02-13 16:43:161477void sqlite3RollbackAll(sqlite3 *db, int tripCode){
drh001bbcb2003-03-19 03:14:001478 int i;
danielk1977f3f06bb2005-12-16 15:24:281479 int inTrans = 0;
drh47b7fc72014-11-11 01:33:571480 int schemaChange;
drhe30f4422007-08-21 16:15:551481 assert( sqlite3_mutex_held(db->mutex) );
danielk19772d1d86f2008-06-20 14:59:511482 sqlite3BeginBenignMalloc();
dan617dc862013-05-16 11:57:281483
larrybrbc917382023-06-07 08:40:311484 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
dan617dc862013-05-16 11:57:281485 ** This is important in case the transaction being rolled back has
1486 ** modified the database schema. If the b-tree mutexes are not taken
1487 ** here, then another shared-cache connection might sneak in between
1488 ** the database rollback and schema reset, which can cause false
1489 ** corruption reports in some cases. */
dancd7b91a2013-05-13 18:23:151490 sqlite3BtreeEnterAll(db);
drh8257aa82017-07-26 19:59:131491 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
dan617dc862013-05-16 11:57:281492
drh001bbcb2003-03-19 03:14:001493 for(i=0; i<db->nDb; i++){
drh0f198a72012-02-13 16:43:161494 Btree *p = db->aDb[i].pBt;
1495 if( p ){
drh99744fa2020-08-25 19:09:071496 if( sqlite3BtreeTxnState(p)==SQLITE_TXN_WRITE ){
danielk1977f3f06bb2005-12-16 15:24:281497 inTrans = 1;
1498 }
drh47b7fc72014-11-11 01:33:571499 sqlite3BtreeRollback(p, tripCode, !schemaChange);
drh001bbcb2003-03-19 03:14:001500 }
1501 }
danielk1977a298e902006-06-22 09:53:481502 sqlite3VtabRollback(db);
danielk19772d1d86f2008-06-20 14:59:511503 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:441504
drh7f32dc92018-07-16 11:32:591505 if( schemaChange ){
drhba968db2018-07-24 22:02:121506 sqlite3ExpirePreparedStatements(db, 0);
drh81028a42012-05-15 18:28:271507 sqlite3ResetAllSchemasOfConnection(db);
danielk197771fd80b2005-12-16 06:54:011508 }
dancd7b91a2013-05-13 18:23:151509 sqlite3BtreeLeaveAll(db);
danielk197771fd80b2005-12-16 06:54:011510
dan1da40a32009-09-19 17:00:311511 /* Any deferred constraint violations have now been resolved. */
1512 db->nDeferredCons = 0;
drh648e2642013-07-11 15:03:321513 db->nDeferredImmCons = 0;
drh46c425b2021-11-10 10:59:101514 db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly);
dan1da40a32009-09-19 17:00:311515
danielk197771fd80b2005-12-16 06:54:011516 /* If one has been configured, invoke the rollback-hook callback */
danielk1977f3f06bb2005-12-16 15:24:281517 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
danielk197771fd80b2005-12-16 06:54:011518 db->xRollbackCallback(db->pRollbackArg);
1519 }
drh001bbcb2003-03-19 03:14:001520}
1521
1522/*
mistachkinf2c1c992013-04-28 01:44:431523** Return a static string containing the name corresponding to the error code
1524** specified in the argument.
1525*/
mistachkin5824d442015-04-28 23:34:101526#if defined(SQLITE_NEED_ERR_NAME)
mistachkinf2c1c992013-04-28 01:44:431527const char *sqlite3ErrName(int rc){
1528 const char *zName = 0;
mistachkin10269dc2013-04-30 07:54:421529 int i, origRc = rc;
mistachkinf2c1c992013-04-28 01:44:431530 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
1531 switch( rc ){
1532 case SQLITE_OK: zName = "SQLITE_OK"; break;
1533 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
dan8d4b7a32018-08-31 19:00:161534 case SQLITE_ERROR_SNAPSHOT: zName = "SQLITE_ERROR_SNAPSHOT"; break;
mistachkinf2c1c992013-04-28 01:44:431535 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
1536 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
1537 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
mistachkine84d8d32013-04-29 03:09:101538 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
mistachkinf2c1c992013-04-28 01:44:431539 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
mistachkine84d8d32013-04-29 03:09:101540 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
danf73819a2013-06-27 11:46:271541 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
mistachkinf2c1c992013-04-28 01:44:431542 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
1543 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
1544 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
1545 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
mistachkine84d8d32013-04-29 03:09:101546 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
drh7e45e3a2017-11-08 17:32:121547 case SQLITE_READONLY_CANTINIT: zName = "SQLITE_READONLY_CANTINIT"; break;
mistachkine84d8d32013-04-29 03:09:101548 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
mistachkin091a81b2013-12-06 19:58:321549 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
dana688ca52018-01-10 11:56:031550 case SQLITE_READONLY_DIRECTORY: zName = "SQLITE_READONLY_DIRECTORY";break;
mistachkinf2c1c992013-04-28 01:44:431551 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
1552 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
mistachkine84d8d32013-04-29 03:09:101553 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
1554 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
1555 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
1556 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
1557 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
1558 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
1559 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
1560 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
1561 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
1562 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
mistachkine84d8d32013-04-29 03:09:101563 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
1564 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
1565 case SQLITE_IOERR_CHECKRESERVEDLOCK:
1566 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
1567 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
1568 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
1569 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
1570 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
1571 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
1572 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
1573 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
1574 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
1575 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
1576 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
mistachkin16a2e7a2013-07-31 22:27:161577 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
mistachkind95a3d32013-08-30 21:52:381578 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
mistachkinf2c1c992013-04-28 01:44:431579 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
mistachkine84d8d32013-04-29 03:09:101580 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
mistachkinf2c1c992013-04-28 01:44:431581 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
1582 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
1583 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
mistachkine84d8d32013-04-29 03:09:101584 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
1585 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
1586 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
mistachkind95a3d32013-08-30 21:52:381587 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
mistachkin3c161e02019-11-18 23:48:031588 case SQLITE_CANTOPEN_SYMLINK: zName = "SQLITE_CANTOPEN_SYMLINK"; break;
mistachkinf2c1c992013-04-28 01:44:431589 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
1590 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
1591 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
1592 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
1593 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
1594 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
1595 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
1596 case SQLITE_CONSTRAINT_FOREIGNKEY:
1597 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
1598 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
1599 case SQLITE_CONSTRAINT_PRIMARYKEY:
1600 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
1601 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
1602 case SQLITE_CONSTRAINT_COMMITHOOK:
1603 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
1604 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
1605 case SQLITE_CONSTRAINT_FUNCTION:
1606 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
drhf9c8ce32013-11-05 13:33:551607 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
mistachkinf2c1c992013-04-28 01:44:431608 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
1609 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
1610 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
1611 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
1612 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
1613 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
1614 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
1615 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
1616 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
mistachkine84d8d32013-04-29 03:09:101617 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
1618 case SQLITE_NOTICE_RECOVER_ROLLBACK:
1619 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
dand8e07c72022-12-12 17:33:361620 case SQLITE_NOTICE_RBU: zName = "SQLITE_NOTICE_RBU"; break;
mistachkinf2c1c992013-04-28 01:44:431621 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
drh8d56e202013-06-28 23:55:451622 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
mistachkinf2c1c992013-04-28 01:44:431623 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
mistachkinf2c1c992013-04-28 01:44:431624 }
1625 }
mistachkin10269dc2013-04-30 07:54:421626 if( zName==0 ){
1627 static char zBuf[50];
1628 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
1629 zName = zBuf;
1630 }
mistachkinf2c1c992013-04-28 01:44:431631 return zName;
1632}
mistachkin10269dc2013-04-30 07:54:421633#endif
mistachkinf2c1c992013-04-28 01:44:431634
1635/*
drhc22bd472002-05-10 13:14:071636** Return a static string that describes the kind of error specified in the
1637** argument.
drh247be432002-05-10 05:44:551638*/
danielk1977f20b21c2004-05-31 23:56:421639const char *sqlite3ErrStr(int rc){
drh51c7d862009-04-27 18:46:061640 static const char* const aMsg[] = {
1641 /* SQLITE_OK */ "not an error",
drha690ff32017-07-07 19:43:231642 /* SQLITE_ERROR */ "SQL logic error",
drh51c7d862009-04-27 18:46:061643 /* SQLITE_INTERNAL */ 0,
1644 /* SQLITE_PERM */ "access permission denied",
drhff4fa772017-07-10 12:07:531645 /* SQLITE_ABORT */ "query aborted",
drh51c7d862009-04-27 18:46:061646 /* SQLITE_BUSY */ "database is locked",
1647 /* SQLITE_LOCKED */ "database table is locked",
1648 /* SQLITE_NOMEM */ "out of memory",
1649 /* SQLITE_READONLY */ "attempt to write a readonly database",
1650 /* SQLITE_INTERRUPT */ "interrupted",
1651 /* SQLITE_IOERR */ "disk I/O error",
1652 /* SQLITE_CORRUPT */ "database disk image is malformed",
drh0b52b7d2011-01-26 19:46:221653 /* SQLITE_NOTFOUND */ "unknown operation",
drh51c7d862009-04-27 18:46:061654 /* SQLITE_FULL */ "database or disk is full",
1655 /* SQLITE_CANTOPEN */ "unable to open database file",
dan0dc7b742010-06-04 15:59:581656 /* SQLITE_PROTOCOL */ "locking protocol",
drhe75be1a2017-07-10 11:17:511657 /* SQLITE_EMPTY */ 0,
drh51c7d862009-04-27 18:46:061658 /* SQLITE_SCHEMA */ "database schema has changed",
drh4c8555f2009-06-25 01:47:111659 /* SQLITE_TOOBIG */ "string or blob too big",
drh51c7d862009-04-27 18:46:061660 /* SQLITE_CONSTRAINT */ "constraint failed",
1661 /* SQLITE_MISMATCH */ "datatype mismatch",
drhff4fa772017-07-10 12:07:531662 /* SQLITE_MISUSE */ "bad parameter or other API misuse",
drhe75be1a2017-07-10 11:17:511663#ifdef SQLITE_DISABLE_LFS
drh51c7d862009-04-27 18:46:061664 /* SQLITE_NOLFS */ "large file support is disabled",
drhe75be1a2017-07-10 11:17:511665#else
1666 /* SQLITE_NOLFS */ 0,
1667#endif
drh51c7d862009-04-27 18:46:061668 /* SQLITE_AUTH */ "authorization denied",
drhe75be1a2017-07-10 11:17:511669 /* SQLITE_FORMAT */ 0,
drhff4fa772017-07-10 12:07:531670 /* SQLITE_RANGE */ "column index out of range",
1671 /* SQLITE_NOTADB */ "file is not a database",
mistachkin2a861102018-02-08 01:00:111672 /* SQLITE_NOTICE */ "notification message",
1673 /* SQLITE_WARNING */ "warning message",
drh51c7d862009-04-27 18:46:061674 };
drh21021a52012-02-13 17:01:511675 const char *zErr = "unknown error";
1676 switch( rc ){
1677 case SQLITE_ABORT_ROLLBACK: {
1678 zErr = "abort due to ROLLBACK";
1679 break;
1680 }
mistachkin2a861102018-02-08 01:00:111681 case SQLITE_ROW: {
1682 zErr = "another row available";
1683 break;
1684 }
1685 case SQLITE_DONE: {
1686 zErr = "no more rows available";
1687 break;
1688 }
drh21021a52012-02-13 17:01:511689 default: {
1690 rc &= 0xff;
1691 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
1692 zErr = aMsg[rc];
1693 }
1694 break;
1695 }
drh247be432002-05-10 05:44:551696 }
drh21021a52012-02-13 17:01:511697 return zErr;
drh247be432002-05-10 05:44:551698}
1699
1700/*
drh2dfbbca2000-07-28 14:32:481701** This routine implements a busy callback that sleeps and tries
1702** again until a timeout value is reached. The timeout value is
1703** an integer number of milliseconds passed in as the first
1704** argument.
drhf0119b22018-03-26 17:40:531705**
1706** Return non-zero to retry the lock. Return zero to stop trying
1707** and cause SQLite to return SQLITE_BUSY.
drh2dfbbca2000-07-28 14:32:481708*/
drhdaffd0e2001-04-11 14:28:421709static int sqliteDefaultBusyCallback(
drhf0119b22018-03-26 17:40:531710 void *ptr, /* Database connection */
dan8714de92020-05-04 19:42:351711 int count /* Number of times table has been busy */
drh2dfbbca2000-07-28 14:32:481712){
drh9b1334b2023-08-03 12:41:301713#if SQLITE_OS_WIN || !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP
drhf0119b22018-03-26 17:40:531714 /* This case is for systems that have support for sleeping for fractions of
drh9b1334b2023-08-03 12:41:301715 ** a second. Examples: All windows systems, unix systems with nanosleep() */
drhee570fa2005-04-28 12:06:051716 static const u8 delays[] =
1717 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
1718 static const u8 totals[] =
1719 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhfcd71b62011-04-05 22:08:241720# define NDELAY ArraySize(delays)
danielk1977b4b47412007-08-17 15:53:361721 sqlite3 *db = (sqlite3 *)ptr;
drhf0119b22018-03-26 17:40:531722 int tmout = db->busyTimeout;
drh97903fe2005-05-24 20:19:571723 int delay, prior;
drh2dfbbca2000-07-28 14:32:481724
drhee570fa2005-04-28 12:06:051725 assert( count>=0 );
1726 if( count < NDELAY ){
1727 delay = delays[count];
1728 prior = totals[count];
drhd1bec472004-01-15 13:29:311729 }else{
1730 delay = delays[NDELAY-1];
drh68cb6192005-05-06 22:05:561731 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
drh2dfbbca2000-07-28 14:32:481732 }
drhf0119b22018-03-26 17:40:531733 if( prior + delay > tmout ){
1734 delay = tmout - prior;
drh2dfbbca2000-07-28 14:32:481735 if( delay<=0 ) return 0;
1736 }
danielk19772a6bdf62007-08-20 16:07:001737 sqlite3OsSleep(db->pVfs, delay*1000);
drh2dfbbca2000-07-28 14:32:481738 return 1;
1739#else
drhf0119b22018-03-26 17:40:531740 /* This case for unix systems that lack usleep() support. Sleeping
1741 ** must be done in increments of whole seconds */
drh482ea182007-08-20 11:12:401742 sqlite3 *db = (sqlite3 *)ptr;
drhf0119b22018-03-26 17:40:531743 int tmout = ((sqlite3 *)ptr)->busyTimeout;
1744 if( (count+1)*1000 > tmout ){
drh2dfbbca2000-07-28 14:32:481745 return 0;
1746 }
drh482ea182007-08-20 11:12:401747 sqlite3OsSleep(db->pVfs, 1000000);
drh2dfbbca2000-07-28 14:32:481748 return 1;
1749#endif
1750}
1751
1752/*
drha4afb652005-07-09 02:16:021753** Invoke the given busy handler.
1754**
drhf0119b22018-03-26 17:40:531755** This routine is called when an operation failed to acquire a
1756** lock on VFS file pFile.
1757**
drha4afb652005-07-09 02:16:021758** If this routine returns non-zero, the lock is retried. If it
1759** returns 0, the operation aborts with an SQLITE_BUSY error.
1760*/
drh783e1592020-05-06 20:55:381761int sqlite3InvokeBusyHandler(BusyHandler *p){
drha4afb652005-07-09 02:16:021762 int rc;
drh80262892018-03-26 16:37:531763 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
dan8714de92020-05-04 19:42:351764 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
drha4afb652005-07-09 02:16:021765 if( rc==0 ){
1766 p->nBusy = -1;
1767 }else{
1768 p->nBusy++;
1769 }
larrybrbc917382023-06-07 08:40:311770 return rc;
drha4afb652005-07-09 02:16:021771}
1772
1773/*
drh2dfbbca2000-07-28 14:32:481774** This routine sets the busy callback for an Sqlite database to the
1775** given callback function with the given argument.
1776*/
danielk1977f9d64d22004-06-19 08:18:071777int sqlite3_busy_handler(
1778 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:261779 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:481780 void *pArg
1781){
drh9ca95732014-10-24 00:35:581782#ifdef SQLITE_ENABLE_API_ARMOR
drh96c707a2015-02-13 16:36:141783 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
drh9ca95732014-10-24 00:35:581784#endif
drhe30f4422007-08-21 16:15:551785 sqlite3_mutex_enter(db->mutex);
drh80262892018-03-26 16:37:531786 db->busyHandler.xBusyHandler = xBusy;
1787 db->busyHandler.pBusyArg = pArg;
drha4afb652005-07-09 02:16:021788 db->busyHandler.nBusy = 0;
drhc0c7b5e2012-09-07 18:49:571789 db->busyTimeout = 0;
dan43aad252025-01-27 11:50:031790#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
1791 db->setlkTimeout = 0;
1792#endif
drhe30f4422007-08-21 16:15:551793 sqlite3_mutex_leave(db->mutex);
danielk1977f9d64d22004-06-19 08:18:071794 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:481795}
1796
danielk1977348bb5d2003-10-18 09:37:261797#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1798/*
1799** This routine sets the progress callback for an Sqlite database to the
1800** given callback function with the given argument. The progress callback will
1801** be invoked every nOps opcodes.
1802*/
danielk197724b03fd2004-05-10 10:34:341803void sqlite3_progress_handler(
larrybrbc917382023-06-07 08:40:311804 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:261805 int nOps,
larrybrbc917382023-06-07 08:40:311806 int (*xProgress)(void*),
danielk1977348bb5d2003-10-18 09:37:261807 void *pArg
1808){
drh9ca95732014-10-24 00:35:581809#ifdef SQLITE_ENABLE_API_ARMOR
1810 if( !sqlite3SafetyCheckOk(db) ){
1811 (void)SQLITE_MISUSE_BKPT;
1812 return;
1813 }
1814#endif
drhbd0b1b52008-07-07 19:52:091815 sqlite3_mutex_enter(db->mutex);
1816 if( nOps>0 ){
1817 db->xProgress = xProgress;
drh8f8c65f2013-07-10 18:14:291818 db->nProgressOps = (unsigned)nOps;
drhbd0b1b52008-07-07 19:52:091819 db->pProgressArg = pArg;
1820 }else{
1821 db->xProgress = 0;
1822 db->nProgressOps = 0;
1823 db->pProgressArg = 0;
danielk1977348bb5d2003-10-18 09:37:261824 }
drhbd0b1b52008-07-07 19:52:091825 sqlite3_mutex_leave(db->mutex);
danielk1977348bb5d2003-10-18 09:37:261826}
1827#endif
1828
1829
drh2dfbbca2000-07-28 14:32:481830/*
1831** This routine installs a default busy handler that waits for the
1832** specified number of milliseconds before returning 0.
1833*/
danielk1977f9d64d22004-06-19 08:18:071834int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh9ca95732014-10-24 00:35:581835#ifdef SQLITE_ENABLE_API_ARMOR
1836 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1837#endif
drh2dfbbca2000-07-28 14:32:481838 if( ms>0 ){
drhf0119b22018-03-26 17:40:531839 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
1840 (void*)db);
drhc0c7b5e2012-09-07 18:49:571841 db->busyTimeout = ms;
dan43aad252025-01-27 11:50:031842#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
1843 db->setlkTimeout = ms;
1844#endif
drh2dfbbca2000-07-28 14:32:481845 }else{
danielk197724b03fd2004-05-10 10:34:341846 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:481847 }
danielk1977f9d64d22004-06-19 08:18:071848 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:481849}
drh4c504392000-10-16 22:06:401850
dan46288882025-01-30 15:26:161851/*
1852** Set the setlk timeout value.
1853*/
dan2d878942025-02-10 20:46:141854int sqlite3_setlk_timeout(sqlite3 *db, int ms, int flags){
dan138951d2025-02-11 17:10:461855#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
dan2d878942025-02-10 20:46:141856 int iDb;
1857 int bBOC = ((flags & SQLITE_SETLK_BLOCK_ON_CONNECT) ? 1 : 0);
dan138951d2025-02-11 17:10:461858#endif
dan43aad252025-01-27 11:50:031859#ifdef SQLITE_ENABLE_API_ARMOR
1860 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1861#endif
dan46288882025-01-30 15:26:161862 if( ms<-1 ) return SQLITE_RANGE;
dan43aad252025-01-27 11:50:031863#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
dan792d1d12025-06-12 07:35:381864 sqlite3_mutex_enter(db->mutex);
dan46288882025-01-30 15:26:161865 db->setlkTimeout = ms;
dan2d878942025-02-10 20:46:141866 db->setlkFlags = flags;
1867 sqlite3BtreeEnterAll(db);
1868 for(iDb=0; iDb<db->nDb; iDb++){
1869 Btree *pBt = db->aDb[iDb].pBt;
1870 if( pBt ){
1871 sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pBt));
1872 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, (void*)&bBOC);
1873 }
1874 }
1875 sqlite3BtreeLeaveAll(db);
dan792d1d12025-06-12 07:35:381876 sqlite3_mutex_leave(db->mutex);
dan43aad252025-01-27 11:50:031877#endif
drh1c03a342025-02-25 20:02:581878#if !defined(SQLITE_ENABLE_API_ARMOR) && !defined(SQLITE_ENABLE_SETLK_TIMEOUT)
1879 UNUSED_PARAMETER(db);
1880 UNUSED_PARAMETER(flags);
1881#endif
dan43aad252025-01-27 11:50:031882 return SQLITE_OK;
1883}
1884
drh4c504392000-10-16 22:06:401885/*
1886** Cause any pending operation to stop at its earliest opportunity.
1887*/
drh9bb575f2004-09-06 17:24:111888void sqlite3_interrupt(sqlite3 *db){
drh9ca95732014-10-24 00:35:581889#ifdef SQLITE_ENABLE_API_ARMOR
drh3b7a19b2023-01-11 00:27:061890 if( !sqlite3SafetyCheckOk(db)
1891 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE)
1892 ){
drh9ca95732014-10-24 00:35:581893 (void)SQLITE_MISUSE_BKPT;
1894 return;
1895 }
1896#endif
dan892edb62020-03-30 13:35:051897 AtomicStore(&db->u1.isInterrupted, 1);
drh4c504392000-10-16 22:06:401898}
drhfa86c412002-02-02 15:01:151899
drh3b7a19b2023-01-11 00:27:061900/*
1901** Return true or false depending on whether or not an interrupt is
1902** pending on connection db.
1903*/
1904int sqlite3_is_interrupted(sqlite3 *db){
1905#ifdef SQLITE_ENABLE_API_ARMOR
1906 if( !sqlite3SafetyCheckOk(db)
1907 && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE)
1908 ){
1909 (void)SQLITE_MISUSE_BKPT;
1910 return 0;
1911 }
1912#endif
1913 return AtomicLoad(&db->u1.isInterrupted)!=0;
1914}
drhfa86c412002-02-02 15:01:151915
1916/*
danielk1977771151b2006-01-17 13:21:401917** This function is exactly the same as sqlite3_create_function(), except
1918** that it is designed to be called by internal code. The difference is
1919** that if a malloc() fails in sqlite3_create_function(), an error code
larrybrbc917382023-06-07 08:40:311920** is returned and the mallocFailed flag cleared.
drhfa86c412002-02-02 15:01:151921*/
danielk1977771151b2006-01-17 13:21:401922int sqlite3CreateFunc(
danielk197765904932004-05-26 06:18:371923 sqlite3 *db,
1924 const char *zFunctionName,
1925 int nArg,
danielk1977d8123362004-06-12 09:25:121926 int enc,
danielk197765904932004-05-26 06:18:371927 void *pUserData,
drh2d801512016-01-14 22:19:581928 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
danielk197765904932004-05-26 06:18:371929 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
dand2199f02010-08-27 17:48:521930 void (*xFinal)(sqlite3_context*),
dan660af932018-06-18 16:55:221931 void (*xValue)(sqlite3_context*),
1932 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
dand2199f02010-08-27 17:48:521933 FuncDestructor *pDestructor
drh8e0a2f92002-02-23 23:45:451934){
drh0bce8352002-02-28 00:41:101935 FuncDef *p;
drh4a8ee3d2013-12-14 13:44:221936 int extraFlags;
danielk197765904932004-05-26 06:18:371937
drhe30f4422007-08-21 16:15:551938 assert( sqlite3_mutex_held(db->mutex) );
drh1e80ace2018-07-09 02:37:121939 assert( xValue==0 || xSFunc==0 );
1940 if( zFunctionName==0 /* Must have a valid name */
1941 || (xSFunc!=0 && xFinal!=0) /* Not both xSFunc and xFinal */
1942 || ((xFinal==0)!=(xStep==0)) /* Both or neither of xFinal and xStep */
1943 || ((xValue==0)!=(xInverse==0)) /* Both or neither of xValue, xInverse */
1944 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
drh7d4c94b2021-10-04 22:34:381945 || (255<sqlite3Strlen30(zFunctionName))
drh1e80ace2018-07-09 02:37:121946 ){
drh413c3d32010-02-23 20:11:561947 return SQLITE_MISUSE_BKPT;
danielk197765904932004-05-26 06:18:371948 }
drh4a8ee3d2013-12-14 13:44:221949
1950 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
drh42d2fce2019-08-15 20:04:091951 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
drhc4ad8492020-01-03 20:57:381952 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
drhdf2d14b2024-08-31 22:22:241953 SQLITE_SUBTYPE|SQLITE_INNOCUOUS|
1954 SQLITE_RESULT_SUBTYPE|SQLITE_SELFORDER1);
drh4a8ee3d2013-12-14 13:44:221955 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
drh4be621e2020-01-03 21:57:531956
1957 /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
1958 ** the meaning is inverted. So flip the bit. */
1959 assert( SQLITE_FUNC_UNSAFE==SQLITE_INNOCUOUS );
drh67918912023-01-09 12:01:301960 extraFlags ^= SQLITE_FUNC_UNSAFE; /* tag-20230109-1 */
drh4be621e2020-01-03 21:57:531961
larrybrbc917382023-06-07 08:40:311962
danielk197793758c82005-01-21 08:13:141963#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:121964 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1965 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1966 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1967 **
1968 ** If SQLITE_ANY is specified, add three versions of the function
1969 ** to the hash table.
1970 */
drh62f560f2021-07-09 13:52:011971 switch( enc ){
1972 case SQLITE_UTF16:
1973 enc = SQLITE_UTF16NATIVE;
1974 break;
1975 case SQLITE_ANY: {
1976 int rc;
drh79d5bc82020-01-04 01:43:021977 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
drh67918912023-01-09 12:01:301978 (SQLITE_UTF8|extraFlags)^SQLITE_FUNC_UNSAFE, /* tag-20230109-1 */
drh79d5bc82020-01-04 01:43:021979 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
drh62f560f2021-07-09 13:52:011980 if( rc==SQLITE_OK ){
1981 rc = sqlite3CreateFunc(db, zFunctionName, nArg,
drh67918912023-01-09 12:01:301982 (SQLITE_UTF16LE|extraFlags)^SQLITE_FUNC_UNSAFE, /* tag-20230109-1*/
drh62f560f2021-07-09 13:52:011983 pUserData, xSFunc, xStep, xFinal, xValue, xInverse, pDestructor);
1984 }
1985 if( rc!=SQLITE_OK ){
1986 return rc;
1987 }
1988 enc = SQLITE_UTF16BE;
1989 break;
drhe30f4422007-08-21 16:15:551990 }
drh62f560f2021-07-09 13:52:011991 case SQLITE_UTF8:
1992 case SQLITE_UTF16LE:
1993 case SQLITE_UTF16BE:
1994 break;
1995 default:
1996 enc = SQLITE_UTF8;
1997 break;
danielk1977d8123362004-06-12 09:25:121998 }
danielk197793758c82005-01-21 08:13:141999#else
2000 enc = SQLITE_UTF8;
2001#endif
larrybrbc917382023-06-07 08:40:312002
danielk19779636c4e2005-01-25 04:27:542003 /* Check if an existing function is being overridden or deleted. If so,
2004 ** and there are active VMs, then return SQLITE_BUSY. If a function
2005 ** is being overridden/deleted but there are no active VMs, allow the
2006 ** operation to continue but invalidate all precompiled statements.
2007 */
drh80738d92016-02-15 00:34:162008 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0);
drhc7bf5712018-07-09 22:49:012009 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
drh4f7d3a52013-06-27 23:54:022010 if( db->nVdbeActive ){
larrybrbc917382023-06-07 08:40:312011 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:572012 "unable to delete/modify user-function due to active statements");
drh17435752007-08-16 04:30:382013 assert( !db->mallocFailed );
danielk19779636c4e2005-01-25 04:27:542014 return SQLITE_BUSY;
2015 }else{
drhba968db2018-07-24 22:02:122016 sqlite3ExpirePreparedStatements(db, 0);
danielk19779636c4e2005-01-25 04:27:542017 }
drhba39ca42021-05-17 13:11:242018 }else if( xSFunc==0 && xFinal==0 ){
2019 /* Trying to delete a function that does not exist. This is a no-op.
2020 ** https://sqlite.org/forum/forumpost/726219164b */
2021 return SQLITE_OK;
danielk19779636c4e2005-01-25 04:27:542022 }
danielk197765904932004-05-26 06:18:372023
drh80738d92016-02-15 00:34:162024 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1);
danielk1977fa18bec2007-09-03 11:04:222025 assert(p || db->mallocFailed);
2026 if( !p ){
mistachkinfad30392016-02-13 23:43:462027 return SQLITE_NOMEM_BKPT;
danielk1977771151b2006-01-17 13:21:402028 }
dand2199f02010-08-27 17:48:522029
2030 /* If an older version of the function with a configured destructor is
2031 ** being replaced invoke the destructor function here. */
2032 functionDestroy(db, p);
2033
2034 if( pDestructor ){
2035 pDestructor->nRef++;
2036 }
drh80738d92016-02-15 00:34:162037 p->u.pDestructor = pDestructor;
drh4a8ee3d2013-12-14 13:44:222038 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
2039 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
drh42d2fce2019-08-15 20:04:092040 testcase( p->funcFlags & SQLITE_DIRECTONLY );
drh2d801512016-01-14 22:19:582041 p->xSFunc = xSFunc ? xSFunc : xStep;
danielk1977fa18bec2007-09-03 11:04:222042 p->xFinalize = xFinal;
dan660af932018-06-18 16:55:222043 p->xValue = xValue;
2044 p->xInverse = xInverse;
danielk1977fa18bec2007-09-03 11:04:222045 p->pUserData = pUserData;
drh1bd10f82008-12-10 21:19:562046 p->nArg = (u16)nArg;
danielk197765904932004-05-26 06:18:372047 return SQLITE_OK;
2048}
danielk1977771151b2006-01-17 13:21:402049
2050/*
dan660af932018-06-18 16:55:222051** Worker function used by utf-8 APIs that create new functions:
2052**
2053** sqlite3_create_function()
2054** sqlite3_create_function_v2()
2055** sqlite3_create_window_function()
danielk1977771151b2006-01-17 13:21:402056*/
dan660af932018-06-18 16:55:222057static int createFunctionApi(
danielk1977771151b2006-01-17 13:21:402058 sqlite3 *db,
dand2199f02010-08-27 17:48:522059 const char *zFunc,
danielk1977771151b2006-01-17 13:21:402060 int nArg,
2061 int enc,
2062 void *p,
dan660af932018-06-18 16:55:222063 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
2064 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
dand2199f02010-08-27 17:48:522065 void (*xFinal)(sqlite3_context*),
dan660af932018-06-18 16:55:222066 void (*xValue)(sqlite3_context*),
2067 void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
2068 void(*xDestroy)(void*)
dand2199f02010-08-27 17:48:522069){
shanehbd2aaf92010-09-01 02:38:212070 int rc = SQLITE_ERROR;
dand2199f02010-08-27 17:48:522071 FuncDestructor *pArg = 0;
drh9ca95732014-10-24 00:35:582072
2073#ifdef SQLITE_ENABLE_API_ARMOR
2074 if( !sqlite3SafetyCheckOk(db) ){
2075 return SQLITE_MISUSE_BKPT;
2076 }
2077#endif
dand2199f02010-08-27 17:48:522078 sqlite3_mutex_enter(db->mutex);
2079 if( xDestroy ){
drh92011842018-05-26 16:00:262080 pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
dan9508daa2010-08-28 18:58:002081 if( !pArg ){
drh92011842018-05-26 16:00:262082 sqlite3OomFault(db);
dan9508daa2010-08-28 18:58:002083 xDestroy(p);
2084 goto out;
2085 }
drh92011842018-05-26 16:00:262086 pArg->nRef = 0;
dand2199f02010-08-27 17:48:522087 pArg->xDestroy = xDestroy;
2088 pArg->pUserData = p;
2089 }
larrybrbc917382023-06-07 08:40:312090 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p,
dan660af932018-06-18 16:55:222091 xSFunc, xStep, xFinal, xValue, xInverse, pArg
2092 );
dand2199f02010-08-27 17:48:522093 if( pArg && pArg->nRef==0 ){
dan8eed5842021-07-09 14:59:122094 assert( rc!=SQLITE_OK || (xStep==0 && xFinal==0) );
dan9508daa2010-08-28 18:58:002095 xDestroy(p);
drh92011842018-05-26 16:00:262096 sqlite3_free(pArg);
dand2199f02010-08-27 17:48:522097 }
2098
2099 out:
drhe30f4422007-08-21 16:15:552100 rc = sqlite3ApiExit(db, rc);
2101 sqlite3_mutex_leave(db->mutex);
2102 return rc;
danielk1977771151b2006-01-17 13:21:402103}
2104
dan660af932018-06-18 16:55:222105/*
2106** Create new user functions.
2107*/
2108int sqlite3_create_function(
2109 sqlite3 *db,
2110 const char *zFunc,
2111 int nArg,
2112 int enc,
2113 void *p,
2114 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
2115 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
2116 void (*xFinal)(sqlite3_context*)
2117){
2118 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
2119 xFinal, 0, 0, 0);
2120}
2121int sqlite3_create_function_v2(
2122 sqlite3 *db,
2123 const char *zFunc,
2124 int nArg,
2125 int enc,
2126 void *p,
2127 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **),
2128 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
2129 void (*xFinal)(sqlite3_context*),
2130 void (*xDestroy)(void *)
2131){
2132 return createFunctionApi(db, zFunc, nArg, enc, p, xSFunc, xStep,
2133 xFinal, 0, 0, xDestroy);
2134}
2135int sqlite3_create_window_function(
2136 sqlite3 *db,
2137 const char *zFunc,
2138 int nArg,
2139 int enc,
2140 void *p,
2141 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
2142 void (*xFinal)(sqlite3_context*),
2143 void (*xValue)(sqlite3_context*),
2144 void (*xInverse)(sqlite3_context*,int,sqlite3_value **),
2145 void (*xDestroy)(void *)
2146){
2147 return createFunctionApi(db, zFunc, nArg, enc, p, 0, xStep,
2148 xFinal, xValue, xInverse, xDestroy);
2149}
2150
danielk197793758c82005-01-21 08:13:142151#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:372152int sqlite3_create_function16(
2153 sqlite3 *db,
2154 const void *zFunctionName,
2155 int nArg,
2156 int eTextRep,
danielk1977771151b2006-01-17 13:21:402157 void *p,
drh2d801512016-01-14 22:19:582158 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**),
danielk197765904932004-05-26 06:18:372159 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
2160 void (*xFinal)(sqlite3_context*)
2161){
2162 int rc;
drhaf9a7c22005-12-15 03:04:102163 char *zFunc8;
drh9ca95732014-10-24 00:35:582164
2165#ifdef SQLITE_ENABLE_API_ARMOR
2166 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
2167#endif
drhe30f4422007-08-21 16:15:552168 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:382169 assert( !db->mallocFailed );
danb7dca7d2010-03-05 16:32:122170 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
dan660af932018-06-18 16:55:222171 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0,0,0);
drh633e6d52008-07-28 19:34:532172 sqlite3DbFree(db, zFunc8);
drhe30f4422007-08-21 16:15:552173 rc = sqlite3ApiExit(db, rc);
2174 sqlite3_mutex_leave(db->mutex);
2175 return rc;
drh8e0a2f92002-02-23 23:45:452176}
danielk197793758c82005-01-21 08:13:142177#endif
drhc9b84a12002-06-20 11:36:482178
drhb7481e72006-09-16 21:45:142179
2180/*
drh92011842018-05-26 16:00:262181** The following is the implementation of an SQL function that always
2182** fails with an error message stating that the function is used in the
2183** wrong context. The sqlite3_overload_function() API might construct
2184** SQL function that use this routine so that the functions will exist
2185** for name resolution but are actually overloaded by the xFindFunction
2186** method of virtual tables.
2187*/
2188static void sqlite3InvalidFunction(
2189 sqlite3_context *context, /* The function calling context */
2190 int NotUsed, /* Number of arguments to the function */
2191 sqlite3_value **NotUsed2 /* Value of each argument */
2192){
2193 const char *zName = (const char*)sqlite3_user_data(context);
2194 char *zErr;
2195 UNUSED_PARAMETER2(NotUsed, NotUsed2);
2196 zErr = sqlite3_mprintf(
2197 "unable to use function %s in the requested context", zName);
2198 sqlite3_result_error(context, zErr, -1);
2199 sqlite3_free(zErr);
2200}
2201
2202/*
drhb7481e72006-09-16 21:45:142203** Declare that a function has been overloaded by a virtual table.
2204**
2205** If the function already exists as a regular global function, then
2206** this routine is a no-op. If the function does not exist, then create
larrybrbc917382023-06-07 08:40:312207** a new one that always throws a run-time error.
drhb7481e72006-09-16 21:45:142208**
2209** When virtual tables intend to provide an overloaded function, they
2210** should call this routine to make sure the global function exists.
2211** A global function must exist in order for name resolution to work
2212** properly.
2213*/
2214int sqlite3_overload_function(
2215 sqlite3 *db,
2216 const char *zName,
2217 int nArg
2218){
drh92011842018-05-26 16:00:262219 int rc;
2220 char *zCopy;
drh9ca95732014-10-24 00:35:582221
2222#ifdef SQLITE_ENABLE_API_ARMOR
2223 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
2224 return SQLITE_MISUSE_BKPT;
2225 }
2226#endif
drhe30f4422007-08-21 16:15:552227 sqlite3_mutex_enter(db->mutex);
drh92011842018-05-26 16:00:262228 rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
drhe30f4422007-08-21 16:15:552229 sqlite3_mutex_leave(db->mutex);
drh92011842018-05-26 16:00:262230 if( rc ) return SQLITE_OK;
dan7acee132022-12-15 11:31:112231 zCopy = sqlite3_mprintf("%s", zName);
drh92011842018-05-26 16:00:262232 if( zCopy==0 ) return SQLITE_NOMEM;
2233 return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
2234 zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
drhb7481e72006-09-16 21:45:142235}
2236
drh19e2d372005-08-29 23:00:032237#ifndef SQLITE_OMIT_TRACE
drhc9b84a12002-06-20 11:36:482238/*
drh18de4822003-01-16 16:28:532239** Register a trace function. The pArg from the previously registered trace
larrybrbc917382023-06-07 08:40:312240** is returned.
drh18de4822003-01-16 16:28:532241**
2242** A NULL trace function means that no tracing is executes. A non-NULL
2243** trace is a pointer to a function that is invoked at the start of each
drh19e2d372005-08-29 23:00:032244** SQL statement.
drh18de4822003-01-16 16:28:532245*/
drh3d2a5292016-07-13 22:55:012246#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:082247void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), void *pArg){
drhe30f4422007-08-21 16:15:552248 void *pOld;
drh9ca95732014-10-24 00:35:582249
2250#ifdef SQLITE_ENABLE_API_ARMOR
2251 if( !sqlite3SafetyCheckOk(db) ){
2252 (void)SQLITE_MISUSE_BKPT;
2253 return 0;
2254 }
2255#endif
drhe30f4422007-08-21 16:15:552256 sqlite3_mutex_enter(db->mutex);
2257 pOld = db->pTraceArg;
drh1637a512016-07-13 23:18:272258 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
drh08b92082020-08-10 14:18:002259 db->trace.xLegacy = xTrace;
drh18de4822003-01-16 16:28:532260 db->pTraceArg = pArg;
drhe30f4422007-08-21 16:15:552261 sqlite3_mutex_leave(db->mutex);
drh18de4822003-01-16 16:28:532262 return pOld;
drh0d1a6432003-04-03 15:46:042263}
drh3d2a5292016-07-13 22:55:012264#endif /* SQLITE_OMIT_DEPRECATED */
2265
2266/* Register a trace callback using the version-2 interface.
2267*/
2268int sqlite3_trace_v2(
drhfca760c2016-07-14 01:09:082269 sqlite3 *db, /* Trace this connection */
2270 unsigned mTrace, /* Mask of events to be traced */
2271 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */
2272 void *pArg /* Context */
drh3d2a5292016-07-13 22:55:012273){
2274#ifdef SQLITE_ENABLE_API_ARMOR
2275 if( !sqlite3SafetyCheckOk(db) ){
mistachkin283a85c2016-07-15 00:39:472276 return SQLITE_MISUSE_BKPT;
drh3d2a5292016-07-13 22:55:012277 }
2278#endif
2279 sqlite3_mutex_enter(db->mutex);
drhfb828202016-08-25 22:06:372280 if( mTrace==0 ) xTrace = 0;
2281 if( xTrace==0 ) mTrace = 0;
drh3d2a5292016-07-13 22:55:012282 db->mTrace = mTrace;
drh08b92082020-08-10 14:18:002283 db->trace.xV2 = xTrace;
drh3d2a5292016-07-13 22:55:012284 db->pTraceArg = pArg;
2285 sqlite3_mutex_leave(db->mutex);
2286 return SQLITE_OK;
2287}
2288
drh087ec072016-07-25 00:05:562289#ifndef SQLITE_OMIT_DEPRECATED
drh19e2d372005-08-29 23:00:032290/*
larrybrbc917382023-06-07 08:40:312291** Register a profile function. The pArg from the previously registered
2292** profile function is returned.
drh19e2d372005-08-29 23:00:032293**
2294** A NULL profile function means that no profiling is executes. A non-NULL
2295** profile is a pointer to a function that is invoked at the conclusion of
2296** each SQL statement that is run.
2297*/
2298void *sqlite3_profile(
2299 sqlite3 *db,
2300 void (*xProfile)(void*,const char*,sqlite_uint64),
2301 void *pArg
2302){
drhe30f4422007-08-21 16:15:552303 void *pOld;
drh9ca95732014-10-24 00:35:582304
2305#ifdef SQLITE_ENABLE_API_ARMOR
2306 if( !sqlite3SafetyCheckOk(db) ){
2307 (void)SQLITE_MISUSE_BKPT;
2308 return 0;
2309 }
2310#endif
drhe30f4422007-08-21 16:15:552311 sqlite3_mutex_enter(db->mutex);
2312 pOld = db->pProfileArg;
drh19e2d372005-08-29 23:00:032313 db->xProfile = xProfile;
2314 db->pProfileArg = pArg;
drh04c67472018-12-04 14:33:022315 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
2316 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
drhe30f4422007-08-21 16:15:552317 sqlite3_mutex_leave(db->mutex);
drh19e2d372005-08-29 23:00:032318 return pOld;
2319}
drh087ec072016-07-25 00:05:562320#endif /* SQLITE_OMIT_DEPRECATED */
drh19e2d372005-08-29 23:00:032321#endif /* SQLITE_OMIT_TRACE */
paulb0208cc2003-04-13 18:26:492322
drhcd565fd2012-03-01 21:30:442323/*
2324** Register a function to be invoked when a transaction commits.
danielk197771fd80b2005-12-16 06:54:012325** If the invoked function returns non-zero, then the commit becomes a
drhaa940ea2004-01-15 02:44:032326** rollback.
2327*/
danielk197724b03fd2004-05-10 10:34:342328void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:112329 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:032330 int (*xCallback)(void*), /* Function to invoke on each commit */
2331 void *pArg /* Argument to the function */
2332){
drhe30f4422007-08-21 16:15:552333 void *pOld;
drh9ca95732014-10-24 00:35:582334
2335#ifdef SQLITE_ENABLE_API_ARMOR
stephan067a13c2023-10-14 12:45:112336 if( !sqlite3SafetyCheckOk(db) ){
drh9ca95732014-10-24 00:35:582337 (void)SQLITE_MISUSE_BKPT;
2338 return 0;
2339 }
2340#endif
drhe30f4422007-08-21 16:15:552341 sqlite3_mutex_enter(db->mutex);
2342 pOld = db->pCommitArg;
drhaa940ea2004-01-15 02:44:032343 db->xCommitCallback = xCallback;
2344 db->pCommitArg = pArg;
drhe30f4422007-08-21 16:15:552345 sqlite3_mutex_leave(db->mutex);
drhaa940ea2004-01-15 02:44:032346 return pOld;
2347}
2348
danielk197794eb6a12005-12-15 15:22:082349/*
2350** Register a callback to be invoked each time a row is updated,
2351** inserted or deleted using this database connection.
2352*/
danielk197771fd80b2005-12-16 06:54:012353void *sqlite3_update_hook(
danielk197794eb6a12005-12-15 15:22:082354 sqlite3 *db, /* Attach the hook to this database */
2355 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
2356 void *pArg /* Argument to the function */
2357){
drhe30f4422007-08-21 16:15:552358 void *pRet;
drh9ca95732014-10-24 00:35:582359
2360#ifdef SQLITE_ENABLE_API_ARMOR
2361 if( !sqlite3SafetyCheckOk(db) ){
2362 (void)SQLITE_MISUSE_BKPT;
2363 return 0;
2364 }
2365#endif
drhe30f4422007-08-21 16:15:552366 sqlite3_mutex_enter(db->mutex);
2367 pRet = db->pUpdateArg;
danielk197794eb6a12005-12-15 15:22:082368 db->xUpdateCallback = xCallback;
2369 db->pUpdateArg = pArg;
drhe30f4422007-08-21 16:15:552370 sqlite3_mutex_leave(db->mutex);
danielk197771fd80b2005-12-16 06:54:012371 return pRet;
danielk197794eb6a12005-12-15 15:22:082372}
2373
danielk197771fd80b2005-12-16 06:54:012374/*
2375** Register a callback to be invoked each time a transaction is rolled
2376** back by this database connection.
2377*/
2378void *sqlite3_rollback_hook(
2379 sqlite3 *db, /* Attach the hook to this database */
2380 void (*xCallback)(void*), /* Callback function */
2381 void *pArg /* Argument to the function */
2382){
drhe30f4422007-08-21 16:15:552383 void *pRet;
drh9ca95732014-10-24 00:35:582384
2385#ifdef SQLITE_ENABLE_API_ARMOR
stephan067a13c2023-10-14 12:45:112386 if( !sqlite3SafetyCheckOk(db) ){
drh9ca95732014-10-24 00:35:582387 (void)SQLITE_MISUSE_BKPT;
2388 return 0;
2389 }
2390#endif
drhe30f4422007-08-21 16:15:552391 sqlite3_mutex_enter(db->mutex);
2392 pRet = db->pRollbackArg;
danielk197771fd80b2005-12-16 06:54:012393 db->xRollbackCallback = xCallback;
2394 db->pRollbackArg = pArg;
drhe30f4422007-08-21 16:15:552395 sqlite3_mutex_leave(db->mutex);
danielk197771fd80b2005-12-16 06:54:012396 return pRet;
2397}
drhaa940ea2004-01-15 02:44:032398
drh9b1c62d2011-03-30 21:04:432399#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:072400/*
2401** Register a callback to be invoked each time a row is updated,
2402** inserted or deleted using this database connection.
2403*/
2404void *sqlite3_preupdate_hook(
2405 sqlite3 *db, /* Attach the hook to this database */
2406 void(*xCallback)( /* Callback function */
2407 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64),
2408 void *pArg /* First callback argument */
2409){
2410 void *pRet;
stephan7dc0cc42023-10-13 12:48:352411
2412#ifdef SQLITE_ENABLE_API_ARMOR
dan2db02ec2023-10-23 15:24:442413 if( db==0 ){
stephan29aeb622023-10-15 13:25:392414 return 0;
stephan7dc0cc42023-10-13 12:48:352415 }
2416#endif
dan46c47d42011-03-01 18:42:072417 sqlite3_mutex_enter(db->mutex);
2418 pRet = db->pPreUpdateArg;
2419 db->xPreUpdateCallback = xCallback;
2420 db->pPreUpdateArg = pArg;
2421 sqlite3_mutex_leave(db->mutex);
2422 return pRet;
2423}
drh9b1c62d2011-03-30 21:04:432424#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
dan46c47d42011-03-01 18:42:072425
drh1bbfc672021-10-15 23:02:272426/*
2427** Register a function to be invoked prior to each autovacuum that
2428** determines the number of pages to vacuum.
2429*/
2430int sqlite3_autovacuum_pages(
2431 sqlite3 *db, /* Attach the hook to this database */
larrybrbc917382023-06-07 08:40:312432 unsigned int (*xCallback)(void*,const char*,u32,u32,u32),
drh1bbfc672021-10-15 23:02:272433 void *pArg, /* Argument to the function */
2434 void (*xDestructor)(void*) /* Destructor for pArg */
2435){
2436#ifdef SQLITE_ENABLE_API_ARMOR
2437 if( !sqlite3SafetyCheckOk(db) ){
2438 if( xDestructor ) xDestructor(pArg);
2439 return SQLITE_MISUSE_BKPT;
2440 }
2441#endif
2442 sqlite3_mutex_enter(db->mutex);
2443 if( db->xAutovacDestr ){
2444 db->xAutovacDestr(db->pAutovacPagesArg);
2445 }
2446 db->xAutovacPages = xCallback;
2447 db->pAutovacPagesArg = pArg;
2448 db->xAutovacDestr = xDestructor;
2449 sqlite3_mutex_leave(db->mutex);
2450 return SQLITE_OK;
2451}
2452
2453
dan586b9c82010-05-03 08:04:492454#ifndef SQLITE_OMIT_WAL
2455/*
2456** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
drh5def0842010-05-05 20:00:252457** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
2458** is greater than sqlite3.pWalArg cast to an integer (the value configured by
drhb033d8b2010-05-03 13:37:302459** wal_autocheckpoint()).
larrybrbc917382023-06-07 08:40:312460*/
drhb033d8b2010-05-03 13:37:302461int sqlite3WalDefaultHook(
drh5def0842010-05-05 20:00:252462 void *pClientData, /* Argument */
drhb033d8b2010-05-03 13:37:302463 sqlite3 *db, /* Connection */
drh5def0842010-05-05 20:00:252464 const char *zDb, /* Database */
drhb033d8b2010-05-03 13:37:302465 int nFrame /* Size of WAL */
2466){
drh5def0842010-05-05 20:00:252467 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
drhf5330352010-05-24 20:27:442468 sqlite3BeginBenignMalloc();
drh5def0842010-05-05 20:00:252469 sqlite3_wal_checkpoint(db, zDb);
drhf5330352010-05-24 20:27:442470 sqlite3EndBenignMalloc();
drh5def0842010-05-05 20:00:252471 }
2472 return SQLITE_OK;
dan586b9c82010-05-03 08:04:492473}
drhb033d8b2010-05-03 13:37:302474#endif /* SQLITE_OMIT_WAL */
dan586b9c82010-05-03 08:04:492475
2476/*
2477** Configure an sqlite3_wal_hook() callback to automatically checkpoint
2478** a database after committing a transaction if there are nFrame or
2479** more frames in the log file. Passing zero or a negative value as the
2480** nFrame parameter disables automatic checkpoints entirely.
2481**
2482** The callback registered by this function replaces any existing callback
2483** registered using sqlite3_wal_hook(). Likewise, registering a callback
2484** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
2485** configured by this function.
2486*/
2487int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
shanehbd2aaf92010-09-01 02:38:212488#ifdef SQLITE_OMIT_WAL
drh9dd55f52010-09-03 03:32:462489 UNUSED_PARAMETER(db);
2490 UNUSED_PARAMETER(nFrame);
shanehbd2aaf92010-09-01 02:38:212491#else
drh9ca95732014-10-24 00:35:582492#ifdef SQLITE_ENABLE_API_ARMOR
2493 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
2494#endif
dan586b9c82010-05-03 08:04:492495 if( nFrame>0 ){
drhb033d8b2010-05-03 13:37:302496 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
dan586b9c82010-05-03 08:04:492497 }else{
2498 sqlite3_wal_hook(db, 0, 0);
2499 }
drhb033d8b2010-05-03 13:37:302500#endif
dan586b9c82010-05-03 08:04:492501 return SQLITE_OK;
2502}
2503
paulb0208cc2003-04-13 18:26:492504/*
dan8d22a172010-04-19 18:03:512505** Register a callback to be invoked each time a transaction is written
2506** into the write-ahead-log by this database connection.
2507*/
drh833bf962010-04-28 14:42:192508void *sqlite3_wal_hook(
dan8d22a172010-04-19 18:03:512509 sqlite3 *db, /* Attach the hook to this db handle */
2510 int(*xCallback)(void *, sqlite3*, const char*, int),
2511 void *pArg /* First argument passed to xCallback() */
2512){
drhb033d8b2010-05-03 13:37:302513#ifndef SQLITE_OMIT_WAL
dan8d22a172010-04-19 18:03:512514 void *pRet;
drh9ca95732014-10-24 00:35:582515#ifdef SQLITE_ENABLE_API_ARMOR
2516 if( !sqlite3SafetyCheckOk(db) ){
2517 (void)SQLITE_MISUSE_BKPT;
2518 return 0;
2519 }
2520#endif
dan8d22a172010-04-19 18:03:512521 sqlite3_mutex_enter(db->mutex);
drh7ed91f22010-04-29 22:34:072522 pRet = db->pWalArg;
2523 db->xWalCallback = xCallback;
2524 db->pWalArg = pArg;
dan8d22a172010-04-19 18:03:512525 sqlite3_mutex_leave(db->mutex);
2526 return pRet;
drhb033d8b2010-05-03 13:37:302527#else
2528 return 0;
2529#endif
dan8d22a172010-04-19 18:03:512530}
2531
2532/*
dancdc1f042010-11-18 12:11:052533** Checkpoint database zDb.
dan586b9c82010-05-03 08:04:492534*/
dancdc1f042010-11-18 12:11:052535int sqlite3_wal_checkpoint_v2(
2536 sqlite3 *db, /* Database handle */
2537 const char *zDb, /* Name of attached database (or NULL) */
2538 int eMode, /* SQLITE_CHECKPOINT_* value */
2539 int *pnLog, /* OUT: Size of WAL log in frames */
2540 int *pnCkpt /* OUT: Total number of frames checkpointed */
2541){
drhb033d8b2010-05-03 13:37:302542#ifdef SQLITE_OMIT_WAL
2543 return SQLITE_OK;
2544#else
dan586b9c82010-05-03 08:04:492545 int rc; /* Return code */
drh099b3852021-03-10 16:35:372546 int iDb; /* Schema to checkpoint */
dan586b9c82010-05-03 08:04:492547
drh9ca95732014-10-24 00:35:582548#ifdef SQLITE_ENABLE_API_ARMOR
2549 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
2550#endif
2551
dan9c5e3682011-02-07 15:12:122552 /* Initialize the output variables to -1 in case an error occurs. */
2553 if( pnLog ) *pnLog = -1;
2554 if( pnCkpt ) *pnCkpt = -1;
2555
danf26a1542014-12-02 19:04:542556 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
2557 assert( SQLITE_CHECKPOINT_FULL==1 );
2558 assert( SQLITE_CHECKPOINT_RESTART==2 );
2559 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
2560 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
drhbb9a3782014-12-03 18:32:472561 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
2562 ** mode: */
drh5a05a682023-09-05 15:03:232563 return SQLITE_MISUSE_BKPT;
dancdc1f042010-11-18 12:11:052564 }
2565
dan586b9c82010-05-03 08:04:492566 sqlite3_mutex_enter(db->mutex);
danaf0cfd32010-05-03 15:58:502567 if( zDb && zDb[0] ){
dan586b9c82010-05-03 08:04:492568 iDb = sqlite3FindDbName(db, zDb);
drh099b3852021-03-10 16:35:372569 }else{
2570 iDb = SQLITE_MAX_DB; /* This means process all schemas */
dan586b9c82010-05-03 08:04:492571 }
2572 if( iDb<0 ){
2573 rc = SQLITE_ERROR;
drh13f40da2014-08-22 18:00:112574 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
dan586b9c82010-05-03 08:04:492575 }else{
dan976b0032015-01-29 19:12:122576 db->busyHandler.nBusy = 0;
dancdc1f042010-11-18 12:11:052577 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
drh13f40da2014-08-22 18:00:112578 sqlite3Error(db, rc);
dan586b9c82010-05-03 08:04:492579 }
drhb033d8b2010-05-03 13:37:302580 rc = sqlite3ApiExit(db, rc);
dan7fb89902016-08-12 16:21:152581
2582 /* If there are no active statements, clear the interrupt flag at this
2583 ** point. */
2584 if( db->nVdbeActive==0 ){
dan892edb62020-03-30 13:35:052585 AtomicStore(&db->u1.isInterrupted, 0);
dan7fb89902016-08-12 16:21:152586 }
2587
dan586b9c82010-05-03 08:04:492588 sqlite3_mutex_leave(db->mutex);
drhb033d8b2010-05-03 13:37:302589 return rc;
2590#endif
dan586b9c82010-05-03 08:04:492591}
2592
dancdc1f042010-11-18 12:11:052593
2594/*
2595** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
larrybrbc917382023-06-07 08:40:312596** to contains a zero-length string, all attached databases are
dancdc1f042010-11-18 12:11:052597** checkpointed.
2598*/
2599int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
drhbb9a3782014-12-03 18:32:472600 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
2601 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
2602 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
dancdc1f042010-11-18 12:11:052603}
2604
drhb033d8b2010-05-03 13:37:302605#ifndef SQLITE_OMIT_WAL
dan586b9c82010-05-03 08:04:492606/*
2607** Run a checkpoint on database iDb. This is a no-op if database iDb is
2608** not currently open in WAL mode.
2609**
larrybrbc917382023-06-07 08:40:312610** If a transaction is open on the database being checkpointed, this
2611** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
2612** an error occurs while running the checkpoint, an SQLite error code is
dan6fcff072010-05-03 14:05:432613** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
dan586b9c82010-05-03 08:04:492614**
2615** The mutex on database handle db should be held by the caller. The mutex
2616** associated with the specific b-tree being checkpointed is taken by
2617** this function while the checkpoint is running.
dan6fcff072010-05-03 14:05:432618**
drh099b3852021-03-10 16:35:372619** If iDb is passed SQLITE_MAX_DB then all attached databases are
dan6fcff072010-05-03 14:05:432620** checkpointed. If an error is encountered it is returned immediately -
2621** no attempt is made to checkpoint any remaining databases.
dana58f26f2010-11-16 18:56:512622**
dan78345512017-09-08 17:48:002623** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL, RESTART
2624** or TRUNCATE.
dan586b9c82010-05-03 08:04:492625*/
dancdc1f042010-11-18 12:11:052626int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
dan6fcff072010-05-03 14:05:432627 int rc = SQLITE_OK; /* Return code */
2628 int i; /* Used to iterate through attached dbs */
danf2b8dd52010-11-18 19:28:012629 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
dan586b9c82010-05-03 08:04:492630
2631 assert( sqlite3_mutex_held(db->mutex) );
dan9c5e3682011-02-07 15:12:122632 assert( !pnLog || *pnLog==-1 );
2633 assert( !pnCkpt || *pnCkpt==-1 );
drh662a1422021-03-10 18:21:182634 testcase( iDb==SQLITE_MAX_ATTACHED ); /* See forum post a006d86f72 */
2635 testcase( iDb==SQLITE_MAX_DB );
dan586b9c82010-05-03 08:04:492636
dan6fcff072010-05-03 14:05:432637 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
drh099b3852021-03-10 16:35:372638 if( i==iDb || iDb==SQLITE_MAX_DB ){
dancdc1f042010-11-18 12:11:052639 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
danf2b8dd52010-11-18 19:28:012640 pnLog = 0;
2641 pnCkpt = 0;
2642 if( rc==SQLITE_BUSY ){
2643 bBusy = 1;
2644 rc = SQLITE_OK;
2645 }
dan6fcff072010-05-03 14:05:432646 }
dan586b9c82010-05-03 08:04:492647 }
2648
danf2b8dd52010-11-18 19:28:012649 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
dan586b9c82010-05-03 08:04:492650}
drhb033d8b2010-05-03 13:37:302651#endif /* SQLITE_OMIT_WAL */
dan586b9c82010-05-03 08:04:492652
2653/*
danielk1977d8293352009-04-30 09:10:372654** This function returns true if main-memory should be used instead of
2655** a temporary file for transient pager files and statement journals.
2656** The value returned depends on the value of db->temp_store (runtime
2657** parameter) and the compile time value of SQLITE_TEMP_STORE. The
2658** following table describes the relationship between these two values
2659** and this functions return value.
2660**
2661** SQLITE_TEMP_STORE db->temp_store Location of temporary database
2662** ----------------- -------------- ------------------------------
2663** 0 any file (return 0)
2664** 1 1 file (return 0)
2665** 1 2 memory (return 1)
2666** 1 0 file (return 0)
2667** 2 1 file (return 0)
2668** 2 2 memory (return 1)
2669** 2 0 memory (return 1)
2670** 3 any memory (return 1)
2671*/
drh1c514142009-04-30 12:25:102672int sqlite3TempInMemory(const sqlite3 *db){
danielk1977d8293352009-04-30 09:10:372673#if SQLITE_TEMP_STORE==1
2674 return ( db->temp_store==2 );
2675#endif
2676#if SQLITE_TEMP_STORE==2
2677 return ( db->temp_store!=1 );
2678#endif
2679#if SQLITE_TEMP_STORE==3
drhf5ed7ad2015-06-15 14:43:252680 UNUSED_PARAMETER(db);
danielk1977d8293352009-04-30 09:10:372681 return 1;
shanecf697392009-06-01 16:53:092682#endif
2683#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
drhf5ed7ad2015-06-15 14:43:252684 UNUSED_PARAMETER(db);
danielk1977d8293352009-04-30 09:10:372685 return 0;
shane60a4b532009-05-06 18:57:092686#endif
danielk1977d8293352009-04-30 09:10:372687}
2688
2689/*
danielk19774ad17132004-05-21 01:47:262690** Return UTF-8 encoded English language explanation of the most recent
2691** error.
2692*/
danielk19776622cce2004-05-20 11:00:522693const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:132694 const char *z;
drhf49661a2008-12-10 16:45:502695 if( !db ){
mistachkinfad30392016-02-13 23:43:462696 return sqlite3ErrStr(SQLITE_NOMEM_BKPT);
danielk19774ad17132004-05-21 01:47:262697 }
drhd55d57e2008-07-07 17:53:072698 if( !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:562699 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
danielk1977e35ee192004-06-26 09:50:112700 }
drh27641702007-08-22 02:56:422701 sqlite3_mutex_enter(db->mutex);
danielk1977238746a2009-03-19 18:51:062702 if( db->mallocFailed ){
mistachkinfad30392016-02-13 23:43:462703 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT);
danielk1977238746a2009-03-19 18:51:062704 }else{
drha3cc0072013-12-13 16:23:552705 testcase( db->pErr==0 );
drh368bfe82018-12-11 12:20:412706 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
danielk1977238746a2009-03-19 18:51:062707 assert( !db->mallocFailed );
2708 if( z==0 ){
2709 z = sqlite3ErrStr(db->errCode);
2710 }
danielk19776622cce2004-05-20 11:00:522711 }
drhe30f4422007-08-21 16:15:552712 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:132713 return z;
danielk19776622cce2004-05-20 11:00:522714}
2715
drhf62641e2021-12-24 20:22:132716/*
2717** Return the byte offset of the most recent error
2718*/
2719int sqlite3_error_offset(sqlite3 *db){
2720 int iOffset = -1;
drh5cec7e12021-12-25 00:19:462721 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
drhf62641e2021-12-24 20:22:132722 sqlite3_mutex_enter(db->mutex);
2723 iOffset = db->errByteOffset;
2724 sqlite3_mutex_leave(db->mutex);
2725 }
2726 return iOffset;
2727}
2728
drh6c626082004-11-14 21:56:292729#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:262730/*
2731** Return UTF-16 encoded English language explanation of the most recent
2732** error.
2733*/
danielk19776622cce2004-05-20 11:00:522734const void *sqlite3_errmsg16(sqlite3 *db){
danielk19777c5c3ca2009-02-23 14:42:532735 static const u16 outOfMem[] = {
2736 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
danielk1977bfd6cce2004-06-18 04:24:542737 };
danielk19777c5c3ca2009-02-23 14:42:532738 static const u16 misuse[] = {
drhff4fa772017-07-10 12:07:532739 'b', 'a', 'd', ' ', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', ' ',
2740 'o', 'r', ' ', 'o', 't', 'h', 'e', 'r', ' ', 'A', 'P', 'I', ' ',
2741 'm', 'i', 's', 'u', 's', 'e', 0
danielk1977e35ee192004-06-26 09:50:112742 };
danielk19774ad17132004-05-21 01:47:262743
drhc60d0442004-09-30 13:43:132744 const void *z;
danielk1977ae7fc492007-03-29 15:00:522745 if( !db ){
danielk19777c5c3ca2009-02-23 14:42:532746 return (void *)outOfMem;
drhc60d0442004-09-30 13:43:132747 }
drhd55d57e2008-07-07 17:53:072748 if( !sqlite3SafetyCheckSickOrOk(db) ){
danielk19777c5c3ca2009-02-23 14:42:532749 return (void *)misuse;
drhc60d0442004-09-30 13:43:132750 }
drhe30f4422007-08-21 16:15:552751 sqlite3_mutex_enter(db->mutex);
danielk1977238746a2009-03-19 18:51:062752 if( db->mallocFailed ){
2753 z = (void *)outOfMem;
2754 }else{
drhc60d0442004-09-30 13:43:132755 z = sqlite3_value_text16(db->pErr);
danielk1977238746a2009-03-19 18:51:062756 if( z==0 ){
drh13f40da2014-08-22 18:00:112757 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
danielk1977238746a2009-03-19 18:51:062758 z = sqlite3_value_text16(db->pErr);
2759 }
2760 /* A malloc() may have failed within the call to sqlite3_value_text16()
2761 ** above. If this is the case, then the db->mallocFailed flag needs to
2762 ** be cleared before returning. Do this directly, instead of via
2763 ** sqlite3ApiExit(), to avoid setting the database handle error message.
2764 */
drh4a642b62016-02-05 01:55:272765 sqlite3OomClear(db);
drhc60d0442004-09-30 13:43:132766 }
drhe30f4422007-08-21 16:15:552767 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:132768 return z;
danielk19776622cce2004-05-20 11:00:522769}
drh6c626082004-11-14 21:56:292770#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:522771
drhc60d0442004-09-30 13:43:132772/*
danielk19777ddad962005-12-12 06:53:032773** Return the most recent error code generated by an SQLite routine. If NULL is
2774** passed to this function, we assume a malloc() failed during sqlite3_open().
drhc60d0442004-09-30 13:43:132775*/
danielk19776622cce2004-05-20 11:00:522776int sqlite3_errcode(sqlite3 *db){
danielk19777c36d072008-01-31 15:31:012777 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:562778 return SQLITE_MISUSE_BKPT;
drhc60d0442004-09-30 13:43:132779 }
drh01495b92008-01-23 12:52:402780 if( !db || db->mallocFailed ){
mistachkinfad30392016-02-13 23:43:462781 return SQLITE_NOMEM_BKPT;
drh01495b92008-01-23 12:52:402782 }
drh4ac285a2006-09-15 07:28:502783 return db->errCode & db->errMask;
danielk19776622cce2004-05-20 11:00:522784}
drh99dfe5e2008-10-30 15:03:152785int sqlite3_extended_errcode(sqlite3 *db){
2786 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:562787 return SQLITE_MISUSE_BKPT;
drh99dfe5e2008-10-30 15:03:152788 }
2789 if( !db || db->mallocFailed ){
mistachkinfad30392016-02-13 23:43:462790 return SQLITE_NOMEM_BKPT;
drh99dfe5e2008-10-30 15:03:152791 }
2792 return db->errCode;
2793}
drh1b9f2142016-03-17 16:01:232794int sqlite3_system_errno(sqlite3 *db){
2795 return db ? db->iSysErrno : 0;
larrybrbc917382023-06-07 08:40:312796}
danielk19776622cce2004-05-20 11:00:522797
drh0e819f92006-02-01 13:50:412798/*
mistachkin5dac8432012-09-11 02:00:252799** Return a string that describes the kind of error specified in the
2800** argument. For now, this simply calls the internal sqlite3ErrStr()
2801** function.
2802*/
2803const char *sqlite3_errstr(int rc){
2804 return sqlite3ErrStr(rc);
2805}
2806
2807/*
drh0e819f92006-02-01 13:50:412808** Create a new collating function for database "db". The name is zName
2809** and the encoding is enc.
2810*/
danielk19779a30cf62006-01-18 04:26:072811static int createCollation(
drhf8d4e8b2009-08-20 02:49:302812 sqlite3* db,
larrybrbc917382023-06-07 08:40:312813 const char *zName,
shanecea72b22009-09-07 04:38:362814 u8 enc,
danielk19779a30cf62006-01-18 04:26:072815 void* pCtx,
danielk1977a9808b32007-05-07 09:32:452816 int(*xCompare)(void*,int,const void*,int,const void*),
2817 void(*xDel)(void*)
danielk19779a30cf62006-01-18 04:26:072818){
2819 CollSeq *pColl;
drh7d9bd4e2006-02-16 18:16:362820 int enc2;
larrybrbc917382023-06-07 08:40:312821
drhe30f4422007-08-21 16:15:552822 assert( sqlite3_mutex_held(db->mutex) );
danielk19779a30cf62006-01-18 04:26:072823
2824 /* If SQLITE_UTF16 is specified as the encoding type, transform this
2825 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
2826 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
2827 */
drh51c7d862009-04-27 18:46:062828 enc2 = enc;
danielk1977ebb32932009-04-28 15:35:382829 testcase( enc2==SQLITE_UTF16 );
2830 testcase( enc2==SQLITE_UTF16_ALIGNED );
2831 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
drh7d9bd4e2006-02-16 18:16:362832 enc2 = SQLITE_UTF16NATIVE;
danielk19779a30cf62006-01-18 04:26:072833 }
drh51c7d862009-04-27 18:46:062834 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
drh413c3d32010-02-23 20:11:562835 return SQLITE_MISUSE_BKPT;
danielk19779a30cf62006-01-18 04:26:072836 }
2837
larrybrbc917382023-06-07 08:40:312838 /* Check if this call is removing or replacing an existing collation
danielk19779a30cf62006-01-18 04:26:072839 ** sequence. If so, and there are active VMs, return busy. If there
2840 ** are no active VMs, invalidate any pre-compiled statements.
2841 */
drhc4a64fa2009-05-11 20:53:282842 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
danielk19779a30cf62006-01-18 04:26:072843 if( pColl && pColl->xCmp ){
drh4f7d3a52013-06-27 23:54:022844 if( db->nVdbeActive ){
larrybrbc917382023-06-07 08:40:312845 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:572846 "unable to delete/modify collation sequence due to active statements");
danielk19779a30cf62006-01-18 04:26:072847 return SQLITE_BUSY;
2848 }
drhba968db2018-07-24 22:02:122849 sqlite3ExpirePreparedStatements(db, 0);
danielk1977a9808b32007-05-07 09:32:452850
2851 /* If collation sequence pColl was created directly by a call to
2852 ** sqlite3_create_collation, and not generated by synthCollSeq(),
2853 ** then any copies made by synthCollSeq() need to be invalidated.
2854 ** Also, collation destructor - CollSeq.xDel() - function may need
2855 ** to be called.
larrybrbc917382023-06-07 08:40:312856 */
danielk1977a9808b32007-05-07 09:32:452857 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
drhacbcb7e2014-08-21 20:26:372858 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
danielk1977a9808b32007-05-07 09:32:452859 int j;
2860 for(j=0; j<3; j++){
2861 CollSeq *p = &aColl[j];
2862 if( p->enc==pColl->enc ){
2863 if( p->xDel ){
2864 p->xDel(p->pUser);
2865 }
2866 p->xCmp = 0;
2867 }
2868 }
2869 }
danielk19779a30cf62006-01-18 04:26:072870 }
2871
drhc4a64fa2009-05-11 20:53:282872 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
mistachkinfad30392016-02-13 23:43:462873 if( pColl==0 ) return SQLITE_NOMEM_BKPT;
drh88404c22010-10-11 17:58:212874 pColl->xCmp = xCompare;
2875 pColl->pUser = pCtx;
2876 pColl->xDel = xDel;
2877 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
drh13f40da2014-08-22 18:00:112878 sqlite3Error(db, SQLITE_OK);
danielk19779a30cf62006-01-18 04:26:072879 return SQLITE_OK;
2880}
2881
2882
danielk19776622cce2004-05-20 11:00:522883/*
drhbb4957f2008-03-20 14:03:292884** This array defines hard upper bounds on limit values. The
2885** initializer must be kept in sync with the SQLITE_LIMIT_*
2886** #defines in sqlite3.h.
2887*/
drhb1a6c3c2008-03-20 16:30:172888static const int aHardLimit[] = {
drhbb4957f2008-03-20 14:03:292889 SQLITE_MAX_LENGTH,
2890 SQLITE_MAX_SQL_LENGTH,
2891 SQLITE_MAX_COLUMN,
2892 SQLITE_MAX_EXPR_DEPTH,
2893 SQLITE_MAX_COMPOUND_SELECT,
2894 SQLITE_MAX_VDBE_OP,
2895 SQLITE_MAX_FUNCTION_ARG,
2896 SQLITE_MAX_ATTACHED,
drhbb4957f2008-03-20 14:03:292897 SQLITE_MAX_LIKE_PATTERN_LENGTH,
drh9f959b02014-08-11 17:37:272898 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
drh417168a2009-09-07 18:14:022899 SQLITE_MAX_TRIGGER_DEPTH,
drh111544c2014-08-29 16:20:472900 SQLITE_MAX_WORKER_THREADS,
drhbb4957f2008-03-20 14:03:292901};
2902
2903/*
2904** Make sure the hard limits are set to reasonable values
2905*/
2906#if SQLITE_MAX_LENGTH<100
2907# error SQLITE_MAX_LENGTH must be at least 100
2908#endif
2909#if SQLITE_MAX_SQL_LENGTH<100
2910# error SQLITE_MAX_SQL_LENGTH must be at least 100
2911#endif
2912#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
2913# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
2914#endif
drhbb4957f2008-03-20 14:03:292915#if SQLITE_MAX_COMPOUND_SELECT<2
2916# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
2917#endif
2918#if SQLITE_MAX_VDBE_OP<40
2919# error SQLITE_MAX_VDBE_OP must be at least 40
2920#endif
drh35d302c2024-12-12 15:11:272921#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>32767
2922# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 32767
drhbb4957f2008-03-20 14:03:292923#endif
drhd08b2792014-07-22 15:33:312924#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
2925# error SQLITE_MAX_ATTACHED must be between 0 and 125
drhbb4957f2008-03-20 14:03:292926#endif
drhbb4957f2008-03-20 14:03:292927#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
2928# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
2929#endif
drh70a8ca32008-08-21 18:49:272930#if SQLITE_MAX_COLUMN>32767
2931# error SQLITE_MAX_COLUMN must not exceed 32767
2932#endif
drh417168a2009-09-07 18:14:022933#if SQLITE_MAX_TRIGGER_DEPTH<1
2934# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
2935#endif
drh111544c2014-08-29 16:20:472936#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
2937# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
2938#endif
drhbb4957f2008-03-20 14:03:292939
2940
2941/*
2942** Change the value of a limit. Report the old value.
2943** If an invalid limit index is supplied, report -1.
2944** Make no changes but still report the old value if the
2945** new limit is negative.
2946**
2947** A new lower limit does not shrink existing constructs.
2948** It merely prevents new constructs that exceed the limit
2949** from forming.
2950*/
2951int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
2952 int oldLimit;
drh4e93f5b2010-09-07 14:59:152953
drh9ca95732014-10-24 00:35:582954#ifdef SQLITE_ENABLE_API_ARMOR
2955 if( !sqlite3SafetyCheckOk(db) ){
2956 (void)SQLITE_MISUSE_BKPT;
2957 return -1;
2958 }
2959#endif
drh4e93f5b2010-09-07 14:59:152960
2961 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
2962 ** there is a hard upper bound set at compile-time by a C preprocessor
2963 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
2964 ** "_MAX_".)
2965 */
2966 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
2967 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
2968 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
2969 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
2970 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
2971 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
2972 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
2973 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
2974 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
2975 SQLITE_MAX_LIKE_PATTERN_LENGTH );
2976 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
2977 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
drh111544c2014-08-29 16:20:472978 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
2979 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
drh4e93f5b2010-09-07 14:59:152980
2981
drh521cc842008-04-15 02:36:332982 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
drhbb4957f2008-03-20 14:03:292983 return -1;
2984 }
2985 oldLimit = db->aLimit[limitId];
drh4e93f5b2010-09-07 14:59:152986 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
drhf47ce562008-03-20 18:00:492987 if( newLimit>aHardLimit[limitId] ){
drh4e93f5b2010-09-07 14:59:152988 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
drh4ddeccf2024-11-08 20:57:452989 }else if( newLimit<SQLITE_MIN_LENGTH && limitId==SQLITE_LIMIT_LENGTH ){
2990 newLimit = SQLITE_MIN_LENGTH;
drhbb4957f2008-03-20 14:03:292991 }
2992 db->aLimit[limitId] = newLimit;
2993 }
drh4e93f5b2010-09-07 14:59:152994 return oldLimit; /* IMP: R-53341-35419 */
drhbb4957f2008-03-20 14:03:292995}
2996
2997/*
dan286ab7c2011-05-06 18:34:542998** This function is used to parse both URIs and non-URI filenames passed by the
2999** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
3000** URIs specified as part of ATTACH statements.
3001**
3002** The first argument to this function is the name of the VFS to use (or
3003** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
3004** query parameter. The second argument contains the URI (or non-URI filename)
3005** itself. When this function is called the *pFlags variable should contain
3006** the default flags to open the database handle with. The value stored in
larrybrbc917382023-06-07 08:40:313007** *pFlags may be updated before returning if the URI filename contains
dan286ab7c2011-05-06 18:34:543008** "cache=xxx" or "mode=xxx" query parameters.
3009**
3010** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
3011** the VFS that should be used to open the database file. *pzFile is set to
drhdf97d432020-02-27 11:32:143012** point to a buffer containing the name of the file to open. The value
3013** stored in *pzFile is a database name acceptable to sqlite3_uri_parameter()
3014** and is in the same format as names created using sqlite3_create_filename().
3015** The caller must invoke sqlite3_free_filename() (not sqlite3_free()!) on
3016** the value returned in *pzFile to avoid a memory leak.
dan286ab7c2011-05-06 18:34:543017**
3018** If an error occurs, then an SQLite error code is returned and *pzErrMsg
larrybrbc917382023-06-07 08:40:313019** may be set to point to a buffer containing an English language error
dan286ab7c2011-05-06 18:34:543020** message. It is the responsibility of the caller to eventually release
3021** this buffer by calling sqlite3_free().
dancd74b612011-04-22 19:37:323022*/
3023int sqlite3ParseUri(
3024 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
3025 const char *zUri, /* Nul-terminated URI to parse */
drh522c26f2011-05-07 14:40:293026 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
larrybrbc917382023-06-07 08:40:313027 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
dancd74b612011-04-22 19:37:323028 char **pzFile, /* OUT: Filename component of URI */
3029 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
3030){
dan78e9dd22011-05-03 10:22:323031 int rc = SQLITE_OK;
drh522c26f2011-05-07 14:40:293032 unsigned int flags = *pFlags;
dancd74b612011-04-22 19:37:323033 const char *zVfs = zDefaultVfs;
3034 char *zFile;
drhde941c32011-05-09 19:20:173035 char c;
dancd74b612011-04-22 19:37:323036 int nUri = sqlite3Strlen30(zUri);
3037
3038 assert( *pzErrMsg==0 );
3039
drhad96db82023-02-23 14:22:293040 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
3041 || AtomicLoad(&sqlite3GlobalConfig.bOpenUri)) /* IMP: R-51689-46548 */
3042 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
dancd74b612011-04-22 19:37:323043 ){
dan5de15372011-04-23 10:12:303044 char *zOpt;
dancd74b612011-04-22 19:37:323045 int eState; /* Parser state when parsing URI */
3046 int iIn; /* Input character index */
3047 int iOut = 0; /* Output character index */
drhdf97d432020-02-27 11:32:143048 u64 nByte = nUri+8; /* Bytes of space to allocate */
dancd74b612011-04-22 19:37:323049
larrybrbc917382023-06-07 08:40:313050 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
dan286ab7c2011-05-06 18:34:543051 ** method that there may be extra parameters following the file-name. */
3052 flags |= SQLITE_OPEN_URI;
3053
3054 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
drhf3cdcdc2015-04-29 16:50:283055 zFile = sqlite3_malloc64(nByte);
mistachkinfad30392016-02-13 23:43:463056 if( !zFile ) return SQLITE_NOMEM_BKPT;
dancd74b612011-04-22 19:37:323057
drhdf97d432020-02-27 11:32:143058 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
3059 zFile += 4;
3060
drh869c0402013-08-07 23:15:523061 iIn = 5;
drh4a4b1382015-03-03 16:58:563062#ifdef SQLITE_ALLOW_URI_AUTHORITY
3063 if( strncmp(zUri+5, "///", 3)==0 ){
3064 iIn = 7;
3065 /* The following condition causes URIs with five leading / characters
3066 ** like file://///host/path to be converted into UNCs like //host/path.
3067 ** The correct URI for that UNC has only two or four leading / characters
larrybrbc917382023-06-07 08:40:313068 ** file://host/path or file:////host/path. But 5 leading slashes is a
drh4a4b1382015-03-03 16:58:563069 ** common error, we are told, so we handle it as a special case. */
3070 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
3071 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
3072 iIn = 16;
3073 }
3074#else
dancd74b612011-04-22 19:37:323075 /* Discard the scheme and authority segments of the URI. */
3076 if( zUri[5]=='/' && zUri[6]=='/' ){
3077 iIn = 7;
3078 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
dan3b18a9a2011-05-03 11:53:203079 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
larrybrbc917382023-06-07 08:40:313080 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
dan3b18a9a2011-05-03 11:53:203081 iIn-7, &zUri[7]);
3082 rc = SQLITE_ERROR;
3083 goto parse_uri_out;
3084 }
dancd74b612011-04-22 19:37:323085 }
drh869c0402013-08-07 23:15:523086#endif
dancd74b612011-04-22 19:37:323087
larrybrbc917382023-06-07 08:40:313088 /* Copy the filename and any query parameters into the zFile buffer.
3089 ** Decode %HH escape codes along the way.
dancd74b612011-04-22 19:37:323090 **
3091 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
3092 ** on the parsing context. As follows:
3093 **
3094 ** 0: Parsing file-name.
3095 ** 1: Parsing name section of a name=value query parameter.
3096 ** 2: Parsing value section of a name=value query parameter.
3097 */
3098 eState = 0;
drhde941c32011-05-09 19:20:173099 while( (c = zUri[iIn])!=0 && c!='#' ){
3100 iIn++;
larrybrbc917382023-06-07 08:40:313101 if( c=='%'
3102 && sqlite3Isxdigit(zUri[iIn])
3103 && sqlite3Isxdigit(zUri[iIn+1])
dancd74b612011-04-22 19:37:323104 ){
dan6d49e252011-05-03 15:09:053105 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
3106 octet += sqlite3HexToInt(zUri[iIn++]);
dancd74b612011-04-22 19:37:323107
dan6d49e252011-05-03 15:09:053108 assert( octet>=0 && octet<256 );
3109 if( octet==0 ){
dan5c35e902016-10-26 12:15:413110#ifndef SQLITE_ENABLE_URI_00_ERROR
dan5de15372011-04-23 10:12:303111 /* This branch is taken when "%00" appears within the URI. In this
3112 ** case we ignore all text in the remainder of the path, name or
3113 ** value currently being parsed. So ignore the current character
3114 ** and skip to the next "?", "=" or "&", as appropriate. */
larrybrbc917382023-06-07 08:40:313115 while( (c = zUri[iIn])!=0 && c!='#'
drhde941c32011-05-09 19:20:173116 && (eState!=0 || c!='?')
3117 && (eState!=1 || (c!='=' && c!='&'))
3118 && (eState!=2 || c!='&')
dan5de15372011-04-23 10:12:303119 ){
3120 iIn++;
3121 }
3122 continue;
dan5c35e902016-10-26 12:15:413123#else
3124 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */
3125 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri");
3126 rc = SQLITE_ERROR;
3127 goto parse_uri_out;
3128#endif
dancd74b612011-04-22 19:37:323129 }
dan6d49e252011-05-03 15:09:053130 c = octet;
dan5de15372011-04-23 10:12:303131 }else if( eState==1 && (c=='&' || c=='=') ){
3132 if( zFile[iOut-1]==0 ){
3133 /* An empty option name. Ignore this option altogether. */
3134 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
3135 continue;
3136 }
3137 if( c=='&' ){
3138 zFile[iOut++] = '\0';
3139 }else{
3140 eState = 2;
3141 }
dancd74b612011-04-22 19:37:323142 c = 0;
dan5de15372011-04-23 10:12:303143 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
3144 c = 0;
dancd74b612011-04-22 19:37:323145 eState = 1;
dancd74b612011-04-22 19:37:323146 }
3147 zFile[iOut++] = c;
3148 }
3149 if( eState==1 ) zFile[iOut++] = '\0';
drhdf97d432020-02-27 11:32:143150 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
dancd74b612011-04-22 19:37:323151
larrybrbc917382023-06-07 08:40:313152 /* Check if there were any options specified that should be interpreted
dancd74b612011-04-22 19:37:323153 ** here. Options that are interpreted here include "vfs" and those that
3154 ** correspond to flags that may be passed to the sqlite3_open_v2()
dan78e9dd22011-05-03 10:22:323155 ** method. */
dan5de15372011-04-23 10:12:303156 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
3157 while( zOpt[0] ){
3158 int nOpt = sqlite3Strlen30(zOpt);
3159 char *zVal = &zOpt[nOpt+1];
3160 int nVal = sqlite3Strlen30(zVal);
dancd74b612011-04-22 19:37:323161
dan286ab7c2011-05-06 18:34:543162 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
dan78e9dd22011-05-03 10:22:323163 zVfs = zVal;
3164 }else{
3165 struct OpenMode {
3166 const char *z;
3167 int mode;
3168 } *aMode = 0;
drh45caede2011-06-03 14:19:103169 char *zModeType = 0;
3170 int mask = 0;
3171 int limit = 0;
dan78e9dd22011-05-03 10:22:323172
dan286ab7c2011-05-06 18:34:543173 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
dan78e9dd22011-05-03 10:22:323174 static struct OpenMode aCacheMode[] = {
3175 { "shared", SQLITE_OPEN_SHAREDCACHE },
3176 { "private", SQLITE_OPEN_PRIVATECACHE },
3177 { 0, 0 }
3178 };
3179
3180 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
3181 aMode = aCacheMode;
3182 limit = mask;
3183 zModeType = "cache";
3184 }
dan286ab7c2011-05-06 18:34:543185 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
dan78e9dd22011-05-03 10:22:323186 static struct OpenMode aOpenMode[] = {
3187 { "ro", SQLITE_OPEN_READONLY },
larrybrbc917382023-06-07 08:40:313188 { "rw", SQLITE_OPEN_READWRITE },
dan78e9dd22011-05-03 10:22:323189 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
dan0b8dcfa2012-06-07 17:16:043190 { "memory", SQLITE_OPEN_MEMORY },
dan78e9dd22011-05-03 10:22:323191 { 0, 0 }
3192 };
3193
drh9c67b2a2012-05-28 13:58:003194 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
3195 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
dan78e9dd22011-05-03 10:22:323196 aMode = aOpenMode;
3197 limit = mask & flags;
3198 zModeType = "access";
3199 }
3200
3201 if( aMode ){
3202 int i;
3203 int mode = 0;
3204 for(i=0; aMode[i].z; i++){
3205 const char *z = aMode[i].z;
drh522c26f2011-05-07 14:40:293206 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
dan78e9dd22011-05-03 10:22:323207 mode = aMode[i].mode;
3208 break;
dancd74b612011-04-22 19:37:323209 }
3210 }
dan78e9dd22011-05-03 10:22:323211 if( mode==0 ){
3212 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
3213 rc = SQLITE_ERROR;
3214 goto parse_uri_out;
3215 }
drh9c67b2a2012-05-28 13:58:003216 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
drhde941c32011-05-09 19:20:173217 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
3218 zModeType, zVal);
dan78e9dd22011-05-03 10:22:323219 rc = SQLITE_PERM;
3220 goto parse_uri_out;
3221 }
3222 flags = (flags & ~mask) | mode;
dancd74b612011-04-22 19:37:323223 }
dancd74b612011-04-22 19:37:323224 }
dan5de15372011-04-23 10:12:303225
3226 zOpt = &zVal[nVal+1];
dancd74b612011-04-22 19:37:323227 }
3228
3229 }else{
drhdf97d432020-02-27 11:32:143230 zFile = sqlite3_malloc64(nUri+8);
mistachkinfad30392016-02-13 23:43:463231 if( !zFile ) return SQLITE_NOMEM_BKPT;
drhdf97d432020-02-27 11:32:143232 memset(zFile, 0, 4);
3233 zFile += 4;
dan895decf2016-12-30 14:15:563234 if( nUri ){
3235 memcpy(zFile, zUri, nUri);
3236 }
drhdf97d432020-02-27 11:32:143237 memset(zFile+nUri, 0, 4);
drh4ab9d252012-05-26 20:08:493238 flags &= ~SQLITE_OPEN_URI;
dancd74b612011-04-22 19:37:323239 }
3240
3241 *ppVfs = sqlite3_vfs_find(zVfs);
3242 if( *ppVfs==0 ){
dancd74b612011-04-22 19:37:323243 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
dan78e9dd22011-05-03 10:22:323244 rc = SQLITE_ERROR;
3245 }
3246 parse_uri_out:
3247 if( rc!=SQLITE_OK ){
drhdf97d432020-02-27 11:32:143248 sqlite3_free_filename(zFile);
dan78e9dd22011-05-03 10:22:323249 zFile = 0;
dancd74b612011-04-22 19:37:323250 }
3251 *pFlags = flags;
3252 *pzFile = zFile;
dan78e9dd22011-05-03 10:22:323253 return rc;
dancd74b612011-04-22 19:37:323254}
3255
drh1eac9662020-02-01 13:30:393256/*
3257** This routine does the core work of extracting URI parameters from a
3258** database filename for the sqlite3_uri_parameter() interface.
3259*/
3260static const char *uriParameter(const char *zFilename, const char *zParam){
3261 zFilename += sqlite3Strlen30(zFilename) + 1;
drhc59ffa82021-10-04 15:08:493262 while( ALWAYS(zFilename!=0) && zFilename[0] ){
drh1eac9662020-02-01 13:30:393263 int x = strcmp(zFilename, zParam);
3264 zFilename += sqlite3Strlen30(zFilename) + 1;
3265 if( x==0 ) return zFilename;
3266 zFilename += sqlite3Strlen30(zFilename) + 1;
3267 }
3268 return 0;
3269}
3270
drhe5989722019-02-02 15:59:493271
dancd74b612011-04-22 19:37:323272
3273/*
danielk19774ad17132004-05-21 01:47:263274** This routine does the work of opening a database on behalf of
larrybrbc917382023-06-07 08:40:313275** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:053276** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:263277*/
3278static int openDatabase(
3279 const char *zFilename, /* Database filename UTF-8 encoded */
drh605264d2007-08-21 15:13:193280 sqlite3 **ppDb, /* OUT: Returned database handle */
drh522c26f2011-05-07 14:40:293281 unsigned int flags, /* Operational flags */
drh605264d2007-08-21 15:13:193282 const char *zVfs /* Name of the VFS to use */
danielk19774ad17132004-05-21 01:47:263283){
dancd74b612011-04-22 19:37:323284 sqlite3 *db; /* Store allocated handle here */
3285 int rc; /* Return code */
3286 int isThreadsafe; /* True for threadsafe connections */
3287 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
3288 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
drh20e34f92020-05-04 17:15:213289 int i; /* Loop counter */
danielk19774ad17132004-05-21 01:47:263290
drh9ca95732014-10-24 00:35:583291#ifdef SQLITE_ENABLE_API_ARMOR
3292 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
3293#endif
drhe0d0f8e2009-04-28 04:47:313294 *ppDb = 0;
drh40257ff2008-06-13 18:24:273295#ifndef SQLITE_OMIT_AUTOINIT
3296 rc = sqlite3_initialize();
3297 if( rc ) return rc;
3298#endif
3299
drh039963a2008-09-03 00:43:153300 if( sqlite3GlobalConfig.bCoreMutex==0 ){
danielk19779a6284c2008-07-10 17:52:493301 isThreadsafe = 0;
drh039963a2008-09-03 00:43:153302 }else if( flags & SQLITE_OPEN_NOMUTEX ){
3303 isThreadsafe = 0;
3304 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
3305 isThreadsafe = 1;
3306 }else{
3307 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
danielk19779a6284c2008-07-10 17:52:493308 }
dan8385bec2017-11-25 17:51:013309
drhf1f12682009-09-09 14:17:523310 if( flags & SQLITE_OPEN_PRIVATECACHE ){
drhf4cfac92009-09-09 15:29:413311 flags &= ~SQLITE_OPEN_SHAREDCACHE;
drhf1f12682009-09-09 14:17:523312 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
3313 flags |= SQLITE_OPEN_SHAREDCACHE;
3314 }
danielk19779a6284c2008-07-10 17:52:493315
drhb25a9f42009-05-12 13:35:113316 /* Remove harmful bits from the flags parameter
3317 **
3318 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
3319 ** dealt with in the previous code block. Besides these, the only
3320 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
drh03e1b402011-02-23 22:39:233321 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
drh91acf7d2021-11-05 19:36:263322 ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_EXRESCODE, and some reserved
3323 ** bits. Silently mask off all other flags.
drhb25a9f42009-05-12 13:35:113324 */
drha4267dc2008-02-19 15:20:443325 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
drhf6d07c82021-10-13 14:45:353326 SQLITE_OPEN_EXCLUSIVE |
drha4267dc2008-02-19 15:20:443327 SQLITE_OPEN_MAIN_DB |
larrybrbc917382023-06-07 08:40:313328 SQLITE_OPEN_TEMP_DB |
3329 SQLITE_OPEN_TRANSIENT_DB |
3330 SQLITE_OPEN_MAIN_JOURNAL |
3331 SQLITE_OPEN_TEMP_JOURNAL |
3332 SQLITE_OPEN_SUBJOURNAL |
drhccb21132020-06-19 11:34:573333 SQLITE_OPEN_SUPER_JOURNAL |
drh039963a2008-09-03 00:43:153334 SQLITE_OPEN_NOMUTEX |
drh357b5f92010-08-24 18:07:573335 SQLITE_OPEN_FULLMUTEX |
3336 SQLITE_OPEN_WAL
drha4267dc2008-02-19 15:20:443337 );
3338
danielk19774ad17132004-05-21 01:47:263339 /* Allocate the sqlite data structure */
drh17435752007-08-16 04:30:383340 db = sqlite3MallocZero( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:263341 if( db==0 ) goto opendb_out;
larrybrbc917382023-06-07 08:40:313342 if( isThreadsafe
dan8385bec2017-11-25 17:51:013343#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
3344 || sqlite3GlobalConfig.bCoreMutex
3345#endif
3346 ){
danielk197759f8c082008-06-18 17:09:103347 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
3348 if( db->mutex==0 ){
3349 sqlite3_free(db);
3350 db = 0;
3351 goto opendb_out;
3352 }
dan8385bec2017-11-25 17:51:013353 if( isThreadsafe==0 ){
3354 sqlite3MutexWarnOnContention(db->mutex);
3355 }
drh605264d2007-08-21 15:13:193356 }
drh27641702007-08-22 02:56:423357 sqlite3_mutex_enter(db->mutex);
drh91acf7d2021-11-05 19:36:263358 db->errMask = (flags & SQLITE_OPEN_EXRESCODE)!=0 ? 0xffffffff : 0xff;
danielk19774ad17132004-05-21 01:47:263359 db->nDb = 2;
drh5f9de6e2021-08-07 23:16:523360 db->eOpenState = SQLITE_STATE_BUSY;
danielk19774ad17132004-05-21 01:47:263361 db->aDb = db->aDbStatic;
danc4b1e5e2018-09-18 17:50:343362 db->lookaside.bDisable = 1;
drh31f69622019-10-05 14:39:363363 db->lookaside.sz = 0;
drh633e6d52008-07-28 19:34:533364
drhbb4957f2008-03-20 14:03:293365 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
3366 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
drh6b2129a2014-08-29 19:06:073367 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
danielk19771d850a72004-05-31 08:26:493368 db->autoCommit = 1;
drhddac25c2007-12-05 01:38:233369 db->nextAutovac = -1;
drh9b4c59f2013-04-15 17:03:423370 db->szMmap = sqlite3GlobalConfig.szMmap;
danielk1977f653d782008-03-20 11:04:213371 db->nextPagesize = 0;
drh2a6a72a2021-09-24 02:14:353372 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
dan05460272021-08-12 14:22:303373#ifdef SQLITE_ENABLE_SORTER_MMAP
larrybrbc917382023-06-07 08:40:313374 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
dan05460272021-08-12 14:22:303375 ** the temporary files used to do external sorts (see code in vdbesort.c)
3376 ** is disabled. It can still be used either by defining
3377 ** SQLITE_ENABLE_SORTER_MMAP at compile time or by using the
3378 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
dan8930c2a2014-04-03 16:25:293379 db->nMaxSorterMmap = 0x7FFFFFFF;
dan05460272021-08-12 14:22:303380#endif
drh4b50da92019-07-02 12:23:093381 db->flags |= SQLITE_ShortColNames
3382 | SQLITE_EnableTrigger
drh11d88e62019-08-15 21:27:203383 | SQLITE_EnableView
drh4b50da92019-07-02 12:23:093384 | SQLITE_CacheSpill
drhc850c2b2025-01-22 19:37:473385 | SQLITE_AttachCreate
3386 | SQLITE_AttachWrite
drhe16b3452025-01-31 01:34:193387 | SQLITE_Comments
drhb77da372020-01-07 16:09:113388#if !defined(SQLITE_TRUSTED_SCHEMA) || SQLITE_TRUSTED_SCHEMA+0!=0
3389 | SQLITE_TrustedSchema
3390#endif
drh4b50da92019-07-02 12:23:093391/* The SQLITE_DQS compile-time option determines the default settings
3392** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
3393**
3394** SQLITE_DQS SQLITE_DBCONFIG_DQS_DDL SQLITE_DBCONFIG_DQS_DML
3395** ---------- ----------------------- -----------------------
larrybrbc917382023-06-07 08:40:313396** undefined on on
drh4b50da92019-07-02 12:23:093397** 3 on on
3398** 2 on off
3399** 1 off on
3400** 0 off off
3401**
3402** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
larrybrbc917382023-06-07 08:40:313403** and so that is the default. But developers are encouraged to use
drh4b50da92019-07-02 12:23:093404** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
3405*/
3406#if !defined(SQLITE_DQS)
3407# define SQLITE_DQS 3
3408#endif
3409#if (SQLITE_DQS&1)==1
drhd0ff6012019-06-17 13:56:113410 | SQLITE_DqsDML
drh4b50da92019-07-02 12:23:093411#endif
3412#if (SQLITE_DQS&2)==2
3413 | SQLITE_DqsDDL
3414#endif
3415
dan1d1f07d2013-07-25 16:41:393416#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
drh986b3872013-06-28 21:12:203417 | SQLITE_AutoIndex
3418#endif
drh883ad042015-02-19 00:29:113419#if SQLITE_DEFAULT_CKPTFULLFSYNC
3420 | SQLITE_CkptFullFSync
3421#endif
drh76fe8032006-07-11 14:17:513422#if SQLITE_DEFAULT_FILE_FORMAT<4
3423 | SQLITE_LegacyFileFmt
3424#endif
drh56424db2007-03-27 22:24:113425#ifdef SQLITE_ENABLE_LOAD_EXTENSION
3426 | SQLITE_LoadExtension
3427#endif
dan5bde73c2009-09-01 17:11:073428#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
dan5e1a2782009-09-01 17:28:293429 | SQLITE_RecTriggers
dan76d462e2009-08-30 11:42:513430#endif
drha4bfd7f2010-12-09 19:15:173431#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
3432 | SQLITE_ForeignKeys
3433#endif
drhf5471922014-12-09 15:12:113434#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
3435 | SQLITE_ReverseOrder
3436#endif
drh1421d982015-05-27 03:46:183437#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
3438 | SQLITE_CellSizeCk
3439#endif
drhd42908f2016-02-26 15:38:243440#if defined(SQLITE_ENABLE_FTS3_TOKENIZER)
3441 | SQLITE_Fts3Tokenizer
3442#endif
drh169dd922017-06-26 13:57:493443#if defined(SQLITE_ENABLE_QPSG)
3444 | SQLITE_EnableQPSG
3445#endif
drh6ab91a72018-11-07 02:17:013446#if defined(SQLITE_DEFAULT_DEFENSIVE)
3447 | SQLITE_Defensive
3448#endif
drhe0a8b712020-05-06 18:43:573449#if defined(SQLITE_DEFAULT_LEGACY_ALTER_TABLE)
3450 | SQLITE_LegacyAlter
3451#endif
dan3410e552023-03-03 21:17:123452#if defined(SQLITE_ENABLE_STMT_SCANSTATUS)
3453 | SQLITE_StmtScanStatus
3454#endif
drh76fe8032006-07-11 14:17:513455 ;
drhe61922a2009-05-02 13:29:373456 sqlite3HashInit(&db->aCollSeq);
drhb9bb7c12006-06-11 23:41:553457#ifndef SQLITE_OMIT_VIRTUALTABLE
drhe61922a2009-05-02 13:29:373458 sqlite3HashInit(&db->aModule);
drhb9bb7c12006-06-11 23:41:553459#endif
danielk1977da184232006-01-05 11:34:323460
danielk19770202b292004-06-09 09:55:163461 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
3462 ** and UTF-16, so add a version for each to avoid any unnecessary
3463 ** conversions. The only error that can occur here is a malloc() failure.
drh5e3b49b2014-11-20 19:22:263464 **
3465 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
3466 ** functions:
danielk19770202b292004-06-09 09:55:163467 */
drhf19aa5f2015-12-30 16:51:203468 createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0);
3469 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0);
3470 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0);
drh5e3b49b2014-11-20 19:22:263471 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
drh821afa42019-06-14 13:24:463472 createCollation(db, "RTRIM", SQLITE_UTF8, 0, rtrimCollFunc, 0);
drh77db4c02008-03-18 13:01:383473 if( db->mallocFailed ){
danielk19770202b292004-06-09 09:55:163474 goto opendb_out;
3475 }
danielk19770202b292004-06-09 09:55:163476
drhc3b6fda2022-09-20 14:36:533477#if SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL)
3478 /* Process magic filenames ":localStorage:" and ":sessionStorage:" */
3479 if( zFilename && zFilename[0]==':' ){
3480 if( strcmp(zFilename, ":localStorage:")==0 ){
3481 zFilename = "file:local?vfs=kvvfs";
3482 flags |= SQLITE_OPEN_URI;
3483 }else if( strcmp(zFilename, ":sessionStorage:")==0 ){
3484 zFilename = "file:session?vfs=kvvfs";
3485 flags |= SQLITE_OPEN_URI;
3486 }
3487 }
3488#endif /* SQLITE_OS_UNIX && defined(SQLITE_OS_KV_OPTIONAL) */
3489
drhff4fa772017-07-10 12:07:533490 /* Parse the filename/URI argument
3491 **
larrybrbc917382023-06-07 08:40:313492 ** Only allow sensible combinations of bits in the flags argument.
drhff4fa772017-07-10 12:07:533493 ** Throw an error if any non-sense combination is used. If we
3494 ** do not block illegal combinations here, it could trigger
3495 ** assert() statements in deeper layers. Sensible combinations
3496 ** are:
3497 **
3498 ** 1: SQLITE_OPEN_READONLY
3499 ** 2: SQLITE_OPEN_READWRITE
3500 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
3501 */
dan19432992011-05-10 18:39:103502 db->openFlags = flags;
drhff4fa772017-07-10 12:07:533503 assert( SQLITE_OPEN_READONLY == 0x01 );
3504 assert( SQLITE_OPEN_READWRITE == 0x02 );
3505 assert( SQLITE_OPEN_CREATE == 0x04 );
3506 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
3507 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
3508 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
3509 if( ((1<<(flags&7)) & 0x46)==0 ){
drhbbf6d432020-05-15 15:03:513510 rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
drhff4fa772017-07-10 12:07:533511 }else{
drh38f2c5f2024-09-21 15:57:063512 if( zFilename==0 ) zFilename = ":memory:";
drhff4fa772017-07-10 12:07:533513 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
3514 }
dancd74b612011-04-22 19:37:323515 if( rc!=SQLITE_OK ){
drh4a642b62016-02-05 01:55:273516 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
drh13f40da2014-08-22 18:00:113517 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
dancd74b612011-04-22 19:37:323518 sqlite3_free(zErrMsg);
3519 goto opendb_out;
3520 }
drhfd608062022-10-27 14:28:153521 assert( db->pVfs!=0 );
drhdddec5c2022-11-01 13:12:203522#if SQLITE_OS_KV || defined(SQLITE_OS_KV_OPTIONAL)
drhfd608062022-10-27 14:28:153523 if( sqlite3_stricmp(db->pVfs->zName, "kvvfs")==0 ){
3524 db->temp_store = 2;
3525 }
3526#endif
dancd74b612011-04-22 19:37:323527
danielk19774ad17132004-05-21 01:47:263528 /* Open the backend database driver */
dan3a6d8ae2011-04-23 15:54:543529 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
drh75c014c2010-08-30 15:02:283530 flags | SQLITE_OPEN_MAIN_DB);
danielk19774ad17132004-05-21 01:47:263531 if( rc!=SQLITE_OK ){
danielk197745e60aa2008-09-23 17:39:263532 if( rc==SQLITE_IOERR_NOMEM ){
mistachkinfad30392016-02-13 23:43:463533 rc = SQLITE_NOMEM_BKPT;
danielk197745e60aa2008-09-23 17:39:263534 }
drh13f40da2014-08-22 18:00:113535 sqlite3Error(db, rc);
danielk19774ad17132004-05-21 01:47:263536 goto opendb_out;
3537 }
dan0235a032014-12-08 20:20:163538 sqlite3BtreeEnter(db->aDb[0].pBt);
drh17435752007-08-16 04:30:383539 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
drh42a630b2020-03-05 16:13:243540 if( !db->mallocFailed ){
3541 sqlite3SetTextEncoding(db, SCHEMA_ENC(db));
3542 }
dan0235a032014-12-08 20:20:163543 sqlite3BtreeLeave(db->aDb[0].pBt);
drh17435752007-08-16 04:30:383544 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
danielk197714db2662006-01-09 16:12:043545
drhe872d512016-02-22 13:23:163546 /* The default safety_level for the main database is FULL; for the temp
larrybrbc917382023-06-07 08:40:313547 ** database it is OFF. This matches the pager layer defaults.
danielk197753c0f742005-03-29 03:10:593548 */
drh69c33822016-08-18 14:33:113549 db->aDb[0].zDbSName = "main";
drhc2ae2072016-03-08 15:30:013550 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
drh69c33822016-08-18 14:33:113551 db->aDb[1].zDbSName = "temp";
drhe872d512016-02-22 13:23:163552 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
danielk197753c0f742005-03-29 03:10:593553
drh5f9de6e2021-08-07 23:16:523554 db->eOpenState = SQLITE_STATE_OPEN;
drh17435752007-08-16 04:30:383555 if( db->mallocFailed ){
danielk1977832a58a2007-06-22 15:21:153556 goto opendb_out;
3557 }
3558
danielk19778e227872004-06-07 07:52:173559 /* Register all built-in functions, but do not attempt to read the
3560 ** database schema yet. This is delayed until the first time the database
3561 ** is accessed.
3562 */
drh13f40da2014-08-22 18:00:113563 sqlite3Error(db, SQLITE_OK);
drh80738d92016-02-15 00:34:163564 sqlite3RegisterPerConnectionBuiltinFunctions(db);
danc9e75fb2016-08-19 18:37:353565 rc = sqlite3_errcode(db);
3566
drh20e34f92020-05-04 17:15:213567
3568 /* Load compiled-in extensions */
3569 for(i=0; rc==SQLITE_OK && i<ArraySize(sqlite3BuiltinExtensions); i++){
3570 rc = sqlite3BuiltinExtensions[i](db);
danc9e75fb2016-08-19 18:37:353571 }
danielk19774ad17132004-05-21 01:47:263572
drh1409be62006-08-23 20:07:203573 /* Load automatic extensions - extensions that have been registered
3574 ** using the sqlite3_automatic_extension() API.
3575 */
drhe5077c12011-12-13 04:08:363576 if( rc==SQLITE_OK ){
3577 sqlite3AutoLoadExtensions(db);
3578 rc = sqlite3_errcode(db);
3579 if( rc!=SQLITE_OK ){
3580 goto opendb_out;
3581 }
danielk1977832a58a2007-06-22 15:21:153582 }
drh1409be62006-08-23 20:07:203583
drh2a83c102020-01-01 23:02:353584#ifdef SQLITE_ENABLE_INTERNAL_FUNCTIONS
3585 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
larrybrbc917382023-06-07 08:40:313586 ** option gives access to internal functions by default.
drh2a83c102020-01-01 23:02:353587 ** Testing use only!!! */
3588 db->mDbFlags |= DBFLAG_InternalFunc;
3589#endif
3590
drh8fd5bd32007-04-02 16:40:373591 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
3592 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
3593 ** mode. Doing nothing at all also makes NORMAL the default.
3594 */
3595#ifdef SQLITE_DEFAULT_LOCKING_MODE
3596 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
3597 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
3598 SQLITE_DEFAULT_LOCKING_MODE);
3599#endif
3600
drh13f40da2014-08-22 18:00:113601 if( rc ) sqlite3Error(db, rc);
drhf6b1a8e2013-12-19 16:26:053602
drh633e6d52008-07-28 19:34:533603 /* Enable the lookaside-malloc subsystem */
drh1b67f3c2008-10-10 17:41:283604 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
3605 sqlite3GlobalConfig.nLookaside);
drh633e6d52008-07-28 19:34:533606
dan5a299f92010-05-03 11:05:083607 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
dan586b9c82010-05-03 08:04:493608
danielk19774ad17132004-05-21 01:47:263609opendb_out:
drhafc91042008-02-21 02:09:453610 if( db ){
drh3ba689d2015-03-03 14:00:113611 assert( db->mutex!=0 || isThreadsafe==0
3612 || sqlite3GlobalConfig.bFullMutex==0 );
drhf3a65f72007-08-22 20:18:213613 sqlite3_mutex_leave(db->mutex);
3614 }
danielk197759633ae2008-08-27 19:01:573615 rc = sqlite3_errcode(db);
drhc18e0222021-11-05 19:52:273616 assert( db!=0 || (rc&0xff)==SQLITE_NOMEM );
drh91acf7d2021-11-05 19:36:263617 if( (rc&0xff)==SQLITE_NOMEM ){
danielk19777ddad962005-12-12 06:53:033618 sqlite3_close(db);
3619 db = 0;
danielk197759633ae2008-08-27 19:01:573620 }else if( rc!=SQLITE_OK ){
drh5f9de6e2021-08-07 23:16:523621 db->eOpenState = SQLITE_STATE_SICK;
danielk197713073932004-06-30 11:54:063622 }
danielk19774ad17132004-05-21 01:47:263623 *ppDb = db;
danac455932012-11-26 19:50:413624#ifdef SQLITE_ENABLE_SQLLOG
3625 if( sqlite3GlobalConfig.xSqllog ){
dan71ba10d2012-11-27 10:56:393626 /* Opening a db handle. Fourth parameter is passed 0. */
3627 void *pArg = sqlite3GlobalConfig.pSqllogArg;
3628 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
danac455932012-11-26 19:50:413629 }
3630#endif
drhdf97d432020-02-27 11:32:143631 sqlite3_free_filename(zOpen);
drh91acf7d2021-11-05 19:36:263632 return rc;
danielk19774ad17132004-05-21 01:47:263633}
3634
drhe5989722019-02-02 15:59:493635
danielk19774ad17132004-05-21 01:47:263636/*
3637** Open a new database handle.
3638*/
danielk197780290862004-05-22 09:21:213639int sqlite3_open(
larrybrbc917382023-06-07 08:40:313640 const char *zFilename,
3641 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:263642){
drh605264d2007-08-21 15:13:193643 return openDatabase(zFilename, ppDb,
3644 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
3645}
3646int sqlite3_open_v2(
drh428e2822007-08-30 16:23:193647 const char *filename, /* Database filename (UTF-8) */
drh605264d2007-08-21 15:13:193648 sqlite3 **ppDb, /* OUT: SQLite db handle */
3649 int flags, /* Flags */
3650 const char *zVfs /* Name of VFS module to use */
3651){
drh522c26f2011-05-07 14:40:293652 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
danielk197783ab5a82004-05-21 11:39:053653}
3654
drh6c626082004-11-14 21:56:293655#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:263656/*
3657** Open a new database handle.
3658*/
3659int sqlite3_open16(
larrybrbc917382023-06-07 08:40:313660 const void *zFilename,
danielk19774f057f92004-06-08 00:02:333661 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:263662){
danielk1977bfd6cce2004-06-18 04:24:543663 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
danielk1977bfd6cce2004-06-18 04:24:543664 sqlite3_value *pVal;
drh40257ff2008-06-13 18:24:273665 int rc;
danielk19774ad17132004-05-21 01:47:263666
drh9ca95732014-10-24 00:35:583667#ifdef SQLITE_ENABLE_API_ARMOR
3668 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
3669#endif
danielk1977bfd6cce2004-06-18 04:24:543670 *ppDb = 0;
drh40257ff2008-06-13 18:24:273671#ifndef SQLITE_OMIT_AUTOINIT
3672 rc = sqlite3_initialize();
3673 if( rc ) return rc;
3674#endif
drh9ca95732014-10-24 00:35:583675 if( zFilename==0 ) zFilename = "\000\000";
danielk19771e536952007-08-16 10:09:013676 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:563677 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
3678 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
danielk1977bfd6cce2004-06-18 04:24:543679 if( zFilename8 ){
drh605264d2007-08-21 15:13:193680 rc = openDatabase(zFilename8, ppDb,
3681 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
drhafc91042008-02-21 02:09:453682 assert( *ppDb || rc==SQLITE_NOMEM );
danielk1977f51bf482008-04-28 16:19:353683 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
drh9bd3cc42014-12-12 23:17:543684 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
danielk1977bfd6cce2004-06-18 04:24:543685 }
drh40257ff2008-06-13 18:24:273686 }else{
mistachkinfad30392016-02-13 23:43:463687 rc = SQLITE_NOMEM_BKPT;
danielk19774ad17132004-05-21 01:47:263688 }
danielk19777ddad962005-12-12 06:53:033689 sqlite3ValueFree(pVal);
danielk19778e227872004-06-07 07:52:173690
drh597d2b62015-06-30 03:13:473691 return rc & 0xff;
danielk19774ad17132004-05-21 01:47:263692}
drh6c626082004-11-14 21:56:293693#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:263694
danielk1977106bb232004-05-21 10:08:533695/*
danielk1977d8123362004-06-12 09:25:123696** Register a new collation sequence with the database handle db.
3697*/
danielk19770202b292004-06-09 09:55:163698int sqlite3_create_collation(
larrybrbc917382023-06-07 08:40:313699 sqlite3* db,
3700 const char *zName,
3701 int enc,
danielk19770202b292004-06-09 09:55:163702 void* pCtx,
3703 int(*xCompare)(void*,int,const void*,int,const void*)
3704){
drh4f81bbb2014-10-23 01:01:263705 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
danielk1977a9808b32007-05-07 09:32:453706}
3707
3708/*
3709** Register a new collation sequence with the database handle db.
3710*/
danielk1977a393c032007-05-07 14:58:533711int sqlite3_create_collation_v2(
larrybrbc917382023-06-07 08:40:313712 sqlite3* db,
3713 const char *zName,
3714 int enc,
danielk1977a9808b32007-05-07 09:32:453715 void* pCtx,
3716 int(*xCompare)(void*,int,const void*,int,const void*),
3717 void(*xDel)(void*)
3718){
3719 int rc;
drh9ca95732014-10-24 00:35:583720
3721#ifdef SQLITE_ENABLE_API_ARMOR
3722 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
3723#endif
drhe30f4422007-08-21 16:15:553724 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:383725 assert( !db->mallocFailed );
drh4dd65e02011-11-29 14:46:563726 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
drhe30f4422007-08-21 16:15:553727 rc = sqlite3ApiExit(db, rc);
3728 sqlite3_mutex_leave(db->mutex);
3729 return rc;
danielk19770202b292004-06-09 09:55:163730}
3731
drh6c626082004-11-14 21:56:293732#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:123733/*
3734** Register a new collation sequence with the database handle db.
3735*/
danielk19770202b292004-06-09 09:55:163736int sqlite3_create_collation16(
larrybrbc917382023-06-07 08:40:313737 sqlite3* db,
mihailimbda2e622008-06-23 11:23:143738 const void *zName,
larrybrbc917382023-06-07 08:40:313739 int enc,
danielk19770202b292004-06-09 09:55:163740 void* pCtx,
3741 int(*xCompare)(void*,int,const void*,int,const void*)
3742){
danielk19779a30cf62006-01-18 04:26:073743 int rc = SQLITE_OK;
drh01495b92008-01-23 12:52:403744 char *zName8;
drh9ca95732014-10-24 00:35:583745
3746#ifdef SQLITE_ENABLE_API_ARMOR
3747 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
3748#endif
drhe30f4422007-08-21 16:15:553749 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:383750 assert( !db->mallocFailed );
danb7dca7d2010-03-05 16:32:123751 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
danielk19779a30cf62006-01-18 04:26:073752 if( zName8 ){
drh4dd65e02011-11-29 14:46:563753 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
drh633e6d52008-07-28 19:34:533754 sqlite3DbFree(db, zName8);
danielk19779a30cf62006-01-18 04:26:073755 }
drhe30f4422007-08-21 16:15:553756 rc = sqlite3ApiExit(db, rc);
3757 sqlite3_mutex_leave(db->mutex);
3758 return rc;
danielk19770202b292004-06-09 09:55:163759}
drh6c626082004-11-14 21:56:293760#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:083761
danielk1977d8123362004-06-12 09:25:123762/*
3763** Register a collation sequence factory callback with the database handle
3764** db. Replace any previously installed collation sequence factory.
3765*/
danielk19777cedc8d2004-06-10 10:50:083766int sqlite3_collation_needed(
larrybrbc917382023-06-07 08:40:313767 sqlite3 *db,
3768 void *pCollNeededArg,
danielk19777cedc8d2004-06-10 10:50:083769 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
3770){
drh9ca95732014-10-24 00:35:583771#ifdef SQLITE_ENABLE_API_ARMOR
3772 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3773#endif
drhe30f4422007-08-21 16:15:553774 sqlite3_mutex_enter(db->mutex);
danielk19777cedc8d2004-06-10 10:50:083775 db->xCollNeeded = xCollNeeded;
3776 db->xCollNeeded16 = 0;
3777 db->pCollNeededArg = pCollNeededArg;
drhe30f4422007-08-21 16:15:553778 sqlite3_mutex_leave(db->mutex);
danielk19777cedc8d2004-06-10 10:50:083779 return SQLITE_OK;
3780}
danielk1977d8123362004-06-12 09:25:123781
drh6c626082004-11-14 21:56:293782#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:123783/*
3784** Register a collation sequence factory callback with the database handle
3785** db. Replace any previously installed collation sequence factory.
3786*/
danielk19777cedc8d2004-06-10 10:50:083787int sqlite3_collation_needed16(
larrybrbc917382023-06-07 08:40:313788 sqlite3 *db,
3789 void *pCollNeededArg,
danielk19777cedc8d2004-06-10 10:50:083790 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
3791){
drh9ca95732014-10-24 00:35:583792#ifdef SQLITE_ENABLE_API_ARMOR
3793 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3794#endif
drhe30f4422007-08-21 16:15:553795 sqlite3_mutex_enter(db->mutex);
danielk19777cedc8d2004-06-10 10:50:083796 db->xCollNeeded = 0;
3797 db->xCollNeeded16 = xCollNeeded16;
3798 db->pCollNeededArg = pCollNeededArg;
drhe30f4422007-08-21 16:15:553799 sqlite3_mutex_leave(db->mutex);
danielk19777cedc8d2004-06-10 10:50:083800 return SQLITE_OK;
3801}
drh6c626082004-11-14 21:56:293802#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:023803
drh10deb352023-08-30 15:20:153804/*
3805** Find existing client data.
3806*/
3807void *sqlite3_get_clientdata(sqlite3 *db, const char *zName){
3808 DbClientData *p;
drh38232082023-08-30 18:51:263809 sqlite3_mutex_enter(db->mutex);
3810 for(p=db->pDbData; p; p=p->pNext){
stephan7fa8d652023-09-10 10:56:283811 if( strcmp(p->zName, zName)==0 ){
drh38232082023-08-30 18:51:263812 void *pResult = p->pData;
3813 sqlite3_mutex_leave(db->mutex);
3814 return pResult;
3815 }
3816 }
3817 sqlite3_mutex_leave(db->mutex);
3818 return 0;
drh10deb352023-08-30 15:20:153819}
3820
3821/*
3822** Add new client data to a database connection.
3823*/
3824int sqlite3_set_clientdata(
3825 sqlite3 *db, /* Attach client data to this connection */
3826 const char *zName, /* Name of the client data */
3827 void *pData, /* The client data itself */
3828 void (*xDestructor)(void*) /* Destructor */
3829){
3830 DbClientData *p, **pp;
drh38232082023-08-30 18:51:263831 sqlite3_mutex_enter(db->mutex);
drh10deb352023-08-30 15:20:153832 pp = &db->pDbData;
stephan7fa8d652023-09-10 10:56:283833 for(p=db->pDbData; p && strcmp(p->zName,zName); p=p->pNext){
drh10deb352023-08-30 15:20:153834 pp = &p->pNext;
3835 }
3836 if( p ){
drhd25d9382023-08-30 17:41:553837 assert( p->pData!=0 );
3838 if( p->xDestructor ) p->xDestructor(p->pData);
drh10deb352023-08-30 15:20:153839 if( pData==0 ){
3840 *pp = p->pNext;
3841 sqlite3_free(p);
drh38232082023-08-30 18:51:263842 sqlite3_mutex_leave(db->mutex);
drh10deb352023-08-30 15:20:153843 return SQLITE_OK;
3844 }
3845 }else if( pData==0 ){
drh38232082023-08-30 18:51:263846 sqlite3_mutex_leave(db->mutex);
drh10deb352023-08-30 15:20:153847 return SQLITE_OK;
3848 }else{
3849 size_t n = strlen(zName);
drhcebf06c2025-03-14 18:10:023850 p = sqlite3_malloc64( SZ_DBCLIENTDATA(n+1) );
drh10deb352023-08-30 15:20:153851 if( p==0 ){
drha5af4a62023-08-30 17:14:123852 if( xDestructor ) xDestructor(pData);
drh38232082023-08-30 18:51:263853 sqlite3_mutex_leave(db->mutex);
drh10deb352023-08-30 15:20:153854 return SQLITE_NOMEM;
3855 }
3856 memcpy(p->zName, zName, n+1);
3857 p->pNext = db->pDbData;
3858 db->pDbData = p;
3859 }
3860 p->pData = pData;
3861 p->xDestructor = xDestructor;
drh38232082023-08-30 18:51:263862 sqlite3_mutex_leave(db->mutex);
drh10deb352023-08-30 15:20:153863 return SQLITE_OK;
3864}
3865
3866
shaneeec556d2008-10-12 00:27:533867#ifndef SQLITE_OMIT_DEPRECATED
danielk19776b456a22005-03-21 04:04:023868/*
danielk19777ddad962005-12-12 06:53:033869** This function is now an anachronism. It used to be used to recover from a
3870** malloc() failure, but SQLite now does this automatically.
danielk19776b456a22005-03-21 04:04:023871*/
drh7d97efb2007-10-12 19:35:483872int sqlite3_global_recover(void){
danielk1977261919c2005-12-06 12:52:593873 return SQLITE_OK;
danielk19776b456a22005-03-21 04:04:023874}
3875#endif
drh3e1d8e62005-05-26 16:23:343876
3877/*
3878** Test to see whether or not the database connection is in autocommit
3879** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
3880** by default. Autocommit is disabled by a BEGIN statement and reenabled
3881** by the next COMMIT or ROLLBACK.
drh3e1d8e62005-05-26 16:23:343882*/
3883int sqlite3_get_autocommit(sqlite3 *db){
drh9ca95732014-10-24 00:35:583884#ifdef SQLITE_ENABLE_API_ARMOR
3885 if( !sqlite3SafetyCheckOk(db) ){
3886 (void)SQLITE_MISUSE_BKPT;
3887 return 0;
3888 }
3889#endif
drh3e1d8e62005-05-26 16:23:343890 return db->autoCommit;
3891}
drh49285702005-09-17 15:20:263892
drh49285702005-09-17 15:20:263893/*
peter.d.reid60ec9142014-09-06 16:39:463894** The following routines are substitutes for constants SQLITE_CORRUPT,
mistachkin8fd84132016-02-14 00:14:353895** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error
peter.d.reid60ec9142014-09-06 16:39:463896** constants. They serve two purposes:
drh9978c972010-02-23 17:36:323897**
3898** 1. Serve as a convenient place to set a breakpoint in a debugger
3899** to detect when version error conditions occurs.
3900**
3901** 2. Invoke sqlite3_log() to provide the source code location where
3902** a low-level error is first detected.
drh49285702005-09-17 15:20:263903*/
daneebf2f52017-11-18 17:30:083904int sqlite3ReportError(int iErr, int lineno, const char *zType){
drh32c49902016-02-15 18:15:153905 sqlite3_log(iErr, "%s at line %d of [%.10s]",
3906 zType, lineno, 20+sqlite3_sourceid());
3907 return iErr;
3908}
drh9978c972010-02-23 17:36:323909int sqlite3CorruptError(int lineno){
drhaf46dc12010-02-24 21:44:073910 testcase( sqlite3GlobalConfig.xLog!=0 );
daneebf2f52017-11-18 17:30:083911 return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption");
drh49285702005-09-17 15:20:263912}
drh9978c972010-02-23 17:36:323913int sqlite3MisuseError(int lineno){
drhaf46dc12010-02-24 21:44:073914 testcase( sqlite3GlobalConfig.xLog!=0 );
daneebf2f52017-11-18 17:30:083915 return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse");
drh9978c972010-02-23 17:36:323916}
3917int sqlite3CantopenError(int lineno){
drhaf46dc12010-02-24 21:44:073918 testcase( sqlite3GlobalConfig.xLog!=0 );
daneebf2f52017-11-18 17:30:083919 return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file");
drh9978c972010-02-23 17:36:323920}
dan3cdc8202020-02-04 20:01:443921#if defined(SQLITE_DEBUG) || defined(SQLITE_ENABLE_CORRUPT_PGNO)
drhcc97ca42017-06-07 22:32:593922int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
3923 char zMsg[100];
3924 sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
3925 testcase( sqlite3GlobalConfig.xLog!=0 );
daneebf2f52017-11-18 17:30:083926 return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
drhcc97ca42017-06-07 22:32:593927}
dan3cdc8202020-02-04 20:01:443928#endif
3929#ifdef SQLITE_DEBUG
mistachkinfad30392016-02-13 23:43:463930int sqlite3NomemError(int lineno){
3931 testcase( sqlite3GlobalConfig.xLog!=0 );
daneebf2f52017-11-18 17:30:083932 return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
mistachkinfad30392016-02-13 23:43:463933}
3934int sqlite3IoerrnomemError(int lineno){
3935 testcase( sqlite3GlobalConfig.xLog!=0 );
daneebf2f52017-11-18 17:30:083936 return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
mistachkinfad30392016-02-13 23:43:463937}
drh32c49902016-02-15 18:15:153938#endif
drh7c1817e2006-01-10 13:58:483939
shaneeec556d2008-10-12 00:27:533940#ifndef SQLITE_OMIT_DEPRECATED
drh6f7adc82006-01-11 21:41:203941/*
3942** This is a convenience routine that makes sure that all thread-specific
3943** data for this thread has been deallocated.
drhdce8bdb2007-08-16 13:01:443944**
3945** SQLite no longer uses thread-specific data so this routine is now a
3946** no-op. It is retained for historical compatibility.
drh6f7adc82006-01-11 21:41:203947*/
3948void sqlite3_thread_cleanup(void){
drh6f7adc82006-01-11 21:41:203949}
shaneeec556d2008-10-12 00:27:533950#endif
danielk1977deb802c2006-02-09 13:43:283951
3952/*
3953** Return meta information about a specific column of a database table.
3954** See comment in sqlite3.h (sqlite.h.in) for details.
3955*/
danielk1977deb802c2006-02-09 13:43:283956int sqlite3_table_column_metadata(
3957 sqlite3 *db, /* Connection handle */
3958 const char *zDbName, /* Database name or NULL */
3959 const char *zTableName, /* Table name */
3960 const char *zColumnName, /* Column name */
3961 char const **pzDataType, /* OUTPUT: Declared data type */
3962 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
3963 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
3964 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
drh9be07162008-06-16 20:51:153965 int *pAutoinc /* OUTPUT: True if column is auto-increment */
danielk1977deb802c2006-02-09 13:43:283966){
3967 int rc;
3968 char *zErrMsg = 0;
3969 Table *pTab = 0;
3970 Column *pCol = 0;
mistachkin1a51ce72015-01-12 18:38:023971 int iCol = 0;
danielk1977deb802c2006-02-09 13:43:283972 char const *zDataType = 0;
3973 char const *zCollSeq = 0;
3974 int notnull = 0;
3975 int primarykey = 0;
3976 int autoinc = 0;
3977
drh96c707a2015-02-13 16:36:143978
3979#ifdef SQLITE_ENABLE_API_ARMOR
3980 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
3981 return SQLITE_MISUSE_BKPT;
3982 }
3983#endif
3984
danielk1977deb802c2006-02-09 13:43:283985 /* Ensure the database schema has been loaded */
drhe30f4422007-08-21 16:15:553986 sqlite3_mutex_enter(db->mutex);
drh3cb3edc2008-03-07 21:37:193987 sqlite3BtreeEnterAll(db);
danielk1977deb802c2006-02-09 13:43:283988 rc = sqlite3Init(db, &zErrMsg);
3989 if( SQLITE_OK!=rc ){
3990 goto error_out;
3991 }
3992
3993 /* Locate the table in question */
3994 pTab = sqlite3FindTable(db, zTableName, zDbName);
drhf38524d2021-08-02 16:41:573995 if( !pTab || IsView(pTab) ){
danielk1977deb802c2006-02-09 13:43:283996 pTab = 0;
3997 goto error_out;
3998 }
3999
4000 /* Find the column for which info is requested */
drh45d1b202014-12-09 22:24:424001 if( zColumnName==0 ){
larrybrbc917382023-06-07 08:40:314002 /* Query for existence of table only */
danielk1977deb802c2006-02-09 13:43:284003 }else{
drh9d90a3a2025-02-08 14:15:424004 iCol = sqlite3ColumnIndex(pTab, zColumnName);
4005 if( iCol>=0 ){
danielk1977deb802c2006-02-09 13:43:284006 pCol = &pTab->aCol[iCol];
drh9d90a3a2025-02-08 14:15:424007 }else{
drh45d1b202014-12-09 22:24:424008 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
4009 iCol = pTab->iPKey;
4010 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
4011 }else{
4012 pTab = 0;
4013 goto error_out;
4014 }
danielk1977deb802c2006-02-09 13:43:284015 }
4016 }
4017
4018 /* The following block stores the meta information that will be returned
4019 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
4020 ** and autoinc. At this point there are two possibilities:
danielk1977deb802c2006-02-09 13:43:284021 **
larrybrbc917382023-06-07 08:40:314022 ** 1. The specified column name was rowid", "oid" or "_rowid_"
4023 ** and there is no explicitly declared IPK column.
4024 **
4025 ** 2. The table is not a view and the column name identified an
danielk1977deb802c2006-02-09 13:43:284026 ** explicitly declared column. Copy meta information from *pCol.
larrybrbc917382023-06-07 08:40:314027 */
danielk1977deb802c2006-02-09 13:43:284028 if( pCol ){
drhd7564862016-03-22 20:05:094029 zDataType = sqlite3ColumnType(pCol,0);
drh65b40092021-08-05 15:27:194030 zCollSeq = sqlite3ColumnColl(pCol);
drh9be07162008-06-16 20:51:154031 notnull = pCol->notNull!=0;
drha371ace2012-09-13 14:22:474032 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
drh7d10d5a2008-08-20 16:35:104033 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
danielk1977deb802c2006-02-09 13:43:284034 }else{
4035 zDataType = "INTEGER";
4036 primarykey = 1;
4037 }
4038 if( !zCollSeq ){
drhf19aa5f2015-12-30 16:51:204039 zCollSeq = sqlite3StrBINARY;
danielk1977deb802c2006-02-09 13:43:284040 }
4041
4042error_out:
danielk197702b4e3b2009-02-26 07:15:594043 sqlite3BtreeLeaveAll(db);
danielk1977deb802c2006-02-09 13:43:284044
4045 /* Whether the function call succeeded or failed, set the output parameters
4046 ** to whatever their local counterparts contain. If an error did occur,
4047 ** this has the effect of zeroing all output parameters.
4048 */
4049 if( pzDataType ) *pzDataType = zDataType;
4050 if( pzCollSeq ) *pzCollSeq = zCollSeq;
4051 if( pNotNull ) *pNotNull = notnull;
4052 if( pPrimaryKey ) *pPrimaryKey = primarykey;
4053 if( pAutoinc ) *pAutoinc = autoinc;
4054
4055 if( SQLITE_OK==rc && !pTab ){
drh633e6d52008-07-28 19:34:534056 sqlite3DbFree(db, zErrMsg);
drh118640b2008-07-16 14:02:324057 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
drhf089aa42008-07-08 19:34:064058 zColumnName);
danielk1977deb802c2006-02-09 13:43:284059 rc = SQLITE_ERROR;
4060 }
drh13f40da2014-08-22 18:00:114061 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
drh633e6d52008-07-28 19:34:534062 sqlite3DbFree(db, zErrMsg);
drhe30f4422007-08-21 16:15:554063 rc = sqlite3ApiExit(db, rc);
4064 sqlite3_mutex_leave(db->mutex);
drhf9cb7f52006-06-27 20:06:444065 return rc;
4066}
drhf9cb7f52006-06-27 20:06:444067
4068/*
4069** Sleep for a little while. Return the amount of time slept.
4070*/
4071int sqlite3_sleep(int ms){
danielk1977b4b47412007-08-17 15:53:364072 sqlite3_vfs *pVfs;
drhe30f4422007-08-21 16:15:554073 int rc;
drhd677b3d2007-08-20 22:48:414074 pVfs = sqlite3_vfs_find(0);
drh40257ff2008-06-13 18:24:274075 if( pVfs==0 ) return 0;
danielk1977fee2d252007-08-18 10:59:194076
larrybrbc917382023-06-07 08:40:314077 /* This function works in milliseconds, but the underlying OsSleep()
danielk1977fee2d252007-08-18 10:59:194078 ** API uses microseconds. Hence the 1000's.
4079 */
drhd6b3a022023-05-02 16:34:154080 rc = (sqlite3OsSleep(pVfs, ms<0 ? 0 : 1000*ms)/1000);
drhe30f4422007-08-21 16:15:554081 return rc;
drhf9cb7f52006-06-27 20:06:444082}
drh4ac285a2006-09-15 07:28:504083
4084/*
4085** Enable or disable the extended result codes.
4086*/
4087int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
drh9ca95732014-10-24 00:35:584088#ifdef SQLITE_ENABLE_API_ARMOR
4089 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
4090#endif
drhe30f4422007-08-21 16:15:554091 sqlite3_mutex_enter(db->mutex);
drh4ac285a2006-09-15 07:28:504092 db->errMask = onoff ? 0xffffffff : 0xff;
drhe30f4422007-08-21 16:15:554093 sqlite3_mutex_leave(db->mutex);
drh4ac285a2006-09-15 07:28:504094 return SQLITE_OK;
4095}
drhcc6bb3e2007-08-31 16:11:354096
4097/*
4098** Invoke the xFileControl method on a particular database.
4099*/
4100int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
4101 int rc = SQLITE_ERROR;
drh421377e2012-03-15 21:28:544102 Btree *pBtree;
4103
drh9ca95732014-10-24 00:35:584104#ifdef SQLITE_ENABLE_API_ARMOR
4105 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
4106#endif
drhcc6bb3e2007-08-31 16:11:354107 sqlite3_mutex_enter(db->mutex);
drh421377e2012-03-15 21:28:544108 pBtree = sqlite3DbNameToBtree(db, zDbName);
4109 if( pBtree ){
4110 Pager *pPager;
4111 sqlite3_file *fd;
4112 sqlite3BtreeEnter(pBtree);
4113 pPager = sqlite3BtreePager(pBtree);
4114 assert( pPager!=0 );
4115 fd = sqlite3PagerFile(pPager);
4116 assert( fd!=0 );
4117 if( op==SQLITE_FCNTL_FILE_POINTER ){
4118 *(sqlite3_file**)pArg = fd;
4119 rc = SQLITE_OK;
drh790f2872015-11-28 18:06:364120 }else if( op==SQLITE_FCNTL_VFS_POINTER ){
4121 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager);
4122 rc = SQLITE_OK;
drh21d61852016-01-08 02:27:014123 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){
4124 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager);
4125 rc = SQLITE_OK;
drhea99a312018-07-18 19:09:074126 }else if( op==SQLITE_FCNTL_DATA_VERSION ){
4127 *(unsigned int*)pArg = sqlite3PagerDataVersion(pPager);
4128 rc = SQLITE_OK;
drh45248de2020-04-20 15:18:434129 }else if( op==SQLITE_FCNTL_RESERVE_BYTES ){
4130 int iNew = *(int*)pArg;
4131 *(int*)pArg = sqlite3BtreeGetRequestedReserve(pBtree);
drhe937df82020-05-07 01:56:574132 if( iNew>=0 && iNew<=255 ){
drh45248de2020-04-20 15:18:434133 sqlite3BtreeSetPageSize(pBtree, 0, iNew, 0);
4134 }
4135 rc = SQLITE_OK;
dan1b3d13e2022-11-28 18:41:414136 }else if( op==SQLITE_FCNTL_RESET_CACHE ){
4137 sqlite3BtreeClearCache(pBtree);
4138 rc = SQLITE_OK;
drh421377e2012-03-15 21:28:544139 }else{
dan2b06b072020-09-04 17:30:594140 int nSave = db->busyHandler.nBusy;
drhafb39a42018-03-29 13:47:014141 rc = sqlite3OsFileControl(fd, op, pArg);
dan2b06b072020-09-04 17:30:594142 db->busyHandler.nBusy = nSave;
drhcc6bb3e2007-08-31 16:11:354143 }
drh421377e2012-03-15 21:28:544144 sqlite3BtreeLeave(pBtree);
drhcc6bb3e2007-08-31 16:11:354145 }
4146 sqlite3_mutex_leave(db->mutex);
drh883ad042015-02-19 00:29:114147 return rc;
drhcc6bb3e2007-08-31 16:11:354148}
drhed13d982008-01-31 14:43:244149
4150/*
4151** Interface to the testing logic.
4152*/
4153int sqlite3_test_control(int op, ...){
drhed13d982008-01-31 14:43:244154 int rc = 0;
drhd12602a2016-12-07 15:49:024155#ifdef SQLITE_UNTESTABLE
drhf5ed7ad2015-06-15 14:43:254156 UNUSED_PARAMETER(op);
4157#else
drh2fa18682008-03-19 14:15:344158 va_list ap;
drhed13d982008-01-31 14:43:244159 va_start(ap, op);
4160 switch( op ){
danielk1977d09414c2008-06-19 18:17:494161
4162 /*
drh984bfaa2008-03-19 16:08:534163 ** Save the current state of the PRNG.
4164 */
drh2fa18682008-03-19 14:15:344165 case SQLITE_TESTCTRL_PRNG_SAVE: {
4166 sqlite3PrngSaveState();
4167 break;
4168 }
drh984bfaa2008-03-19 16:08:534169
4170 /*
4171 ** Restore the state of the PRNG to the last state saved using
4172 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
4173 ** this verb acts like PRNG_RESET.
4174 */
drh2fa18682008-03-19 14:15:344175 case SQLITE_TESTCTRL_PRNG_RESTORE: {
4176 sqlite3PrngRestoreState();
4177 break;
4178 }
drh984bfaa2008-03-19 16:08:534179
drh2e6d83b2019-08-03 01:39:204180 /* sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
drhade54d62019-08-02 20:45:044181 **
drh2e6d83b2019-08-03 01:39:204182 ** Control the seed for the pseudo-random number generator (PRNG) that
4183 ** is built into SQLite. Cases:
4184 **
4185 ** x!=0 && db!=0 Seed the PRNG to the current value of the
4186 ** schema cookie in the main database for db, or
4187 ** x if the schema cookie is zero. This case
4188 ** is convenient to use with database fuzzers
4189 ** as it allows the fuzzer some control over the
4190 ** the PRNG seed.
4191 **
4192 ** x!=0 && db==0 Seed the PRNG to the value of x.
4193 **
4194 ** x==0 && db==0 Revert to default behavior of using the
4195 ** xRandomness method on the primary VFS.
4196 **
4197 ** This test-control also resets the PRNG so that the new seed will
4198 ** be used for the next call to sqlite3_randomness().
drh984bfaa2008-03-19 16:08:534199 */
drhef9f7192020-01-17 19:14:084200#ifndef SQLITE_OMIT_WSD
drhade54d62019-08-02 20:45:044201 case SQLITE_TESTCTRL_PRNG_SEED: {
drh2e6d83b2019-08-03 01:39:204202 int x = va_arg(ap, int);
4203 int y;
4204 sqlite3 *db = va_arg(ap, sqlite3*);
4205 assert( db==0 || db->aDb[0].pSchema!=0 );
4206 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
4207 sqlite3Config.iPrngSeed = x;
4208 sqlite3_randomness(0,0);
drh2fa18682008-03-19 14:15:344209 break;
4210 }
drhef9f7192020-01-17 19:14:084211#endif
drh3088d592008-03-21 16:45:474212
drh51b358e2023-10-21 12:54:374213 /* sqlite3_test_control(SQLITE_TESTCTRL_FK_NO_ACTION, sqlite3 *db, int b);
4214 **
4215 ** If b is true, then activate the SQLITE_FkNoAction setting. If b is
stephan5d60f472025-02-25 20:55:144216 ** false then clear that setting. If the SQLITE_FkNoAction setting is
4217 ** enabled, all foreign key ON DELETE and ON UPDATE actions behave as if
drh51b358e2023-10-21 12:54:374218 ** they were NO ACTION, regardless of how they are defined.
drh5c8cfe92023-10-21 16:25:444219 **
4220 ** NB: One must usually run "PRAGMA writable_schema=RESET" after
4221 ** using this test-control, before it will take full effect. failing
4222 ** to reset the schema can result in some unexpected behavior.
drh51b358e2023-10-21 12:54:374223 */
4224 case SQLITE_TESTCTRL_FK_NO_ACTION: {
4225 sqlite3 *db = va_arg(ap, sqlite3*);
4226 int b = va_arg(ap, int);
4227 if( b ){
4228 db->flags |= SQLITE_FkNoAction;
4229 }else{
4230 db->flags &= ~SQLITE_FkNoAction;
4231 }
4232 break;
4233 }
4234
drh3088d592008-03-21 16:45:474235 /*
4236 ** sqlite3_test_control(BITVEC_TEST, size, program)
4237 **
4238 ** Run a test against a Bitvec object of size. The program argument
4239 ** is an array of integers that defines the test. Return -1 on a
4240 ** memory allocation error, 0 on success, or non-zero for an error.
4241 ** See the sqlite3BitvecBuiltinTest() for additional information.
4242 */
4243 case SQLITE_TESTCTRL_BITVEC_TEST: {
4244 int sz = va_arg(ap, int);
4245 int *aProg = va_arg(ap, int*);
4246 rc = sqlite3BitvecBuiltinTest(sz, aProg);
4247 break;
4248 }
danielk19772d1d86f2008-06-20 14:59:514249
4250 /*
drhc007f612014-05-16 14:17:014251 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
4252 **
4253 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
4254 ** if xCallback is not NULL.
4255 **
4256 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
4257 ** is called immediately after installing the new callback and the return
4258 ** value from sqlite3FaultSim(0) becomes the return from
4259 ** sqlite3_test_control().
4260 */
4261 case SQLITE_TESTCTRL_FAULT_INSTALL: {
drh0d58ae02022-02-11 11:37:124262 /* A bug in MSVC prevents it from understanding pointers to functions
4263 ** types in the second argument to va_arg(). Work around the problem
4264 ** using a typedef.
4265 ** http://support.microsoft.com/kb/47961 <-- dead hyperlink
drh11fc3c02022-02-11 17:47:254266 ** Search at http://web.archive.org/ to find the 2015-03-16 archive
4267 ** of the link above to see the original text.
dan685ffb12014-06-23 10:18:504268 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
mistachkin77a90ce2014-05-16 23:15:504269 */
drh0d58ae02022-02-11 11:37:124270 typedef int(*sqlite3FaultFuncType)(int);
4271 sqlite3GlobalConfig.xTestCallback = va_arg(ap, sqlite3FaultFuncType);
drhc007f612014-05-16 14:17:014272 rc = sqlite3FaultSim(0);
4273 break;
4274 }
4275
4276 /*
danielk19772d1d86f2008-06-20 14:59:514277 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
4278 **
larrybrbc917382023-06-07 08:40:314279 ** Register hooks to call to indicate which malloc() failures
danielk19772d1d86f2008-06-20 14:59:514280 ** are benign.
4281 */
4282 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
danielk1977ca026792008-06-23 14:15:524283 typedef void (*void_function)(void);
4284 void_function xBenignBegin;
4285 void_function xBenignEnd;
4286 xBenignBegin = va_arg(ap, void_function);
4287 xBenignEnd = va_arg(ap, void_function);
danielk19772d1d86f2008-06-20 14:59:514288 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
4289 break;
4290 }
drhc7a3bb92009-02-05 16:31:454291
4292 /*
drhf3af63f2009-05-09 18:59:424293 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
drhc7a3bb92009-02-05 16:31:454294 **
4295 ** Set the PENDING byte to the value in the argument, if X>0.
4296 ** Make no changes if X==0. Return the value of the pending byte
4297 ** as it existing before this routine was called.
4298 **
4299 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
4300 ** an incompatible database file format. Changing the PENDING byte
4301 ** while any database connection is open results in undefined and
peter.d.reid60ec9142014-09-06 16:39:464302 ** deleterious behavior.
drhc7a3bb92009-02-05 16:31:454303 */
4304 case SQLITE_TESTCTRL_PENDING_BYTE: {
drhf83dc1e2010-06-03 12:09:524305 rc = PENDING_BYTE;
4306#ifndef SQLITE_OMIT_WSD
4307 {
4308 unsigned int newVal = va_arg(ap, unsigned int);
4309 if( newVal ) sqlite3PendingByte = newVal;
4310 }
4311#endif
drhc7a3bb92009-02-05 16:31:454312 break;
4313 }
drhf3af63f2009-05-09 18:59:424314
4315 /*
4316 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
4317 **
4318 ** This action provides a run-time test to see whether or not
4319 ** assert() was enabled at compile-time. If X is true and assert()
4320 ** is enabled, then the return value is true. If X is true and
4321 ** assert() is disabled, then the return value is zero. If X is
4322 ** false and assert() is enabled, then the assertion fires and the
4323 ** process aborts. If X is false and assert() is disabled, then the
4324 ** return value is zero.
4325 */
4326 case SQLITE_TESTCTRL_ASSERT: {
4327 volatile int x = 0;
drhcc5f8a42016-02-06 22:32:064328 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
drhf3af63f2009-05-09 18:59:424329 rc = x;
drh4cd82962022-04-07 13:48:344330#if defined(SQLITE_DEBUG)
4331 /* Invoke these debugging routines so that the compiler does not
4332 ** issue "defined but not used" warnings. */
4333 if( x==9999 ){
4334 sqlite3ShowExpr(0);
drh4cd82962022-04-07 13:48:344335 sqlite3ShowExprList(0);
4336 sqlite3ShowIdList(0);
4337 sqlite3ShowSrcList(0);
4338 sqlite3ShowWith(0);
4339 sqlite3ShowUpsert(0);
drh86d77fd2023-08-03 13:30:004340#ifndef SQLITE_OMIT_TRIGGER
drh4cd82962022-04-07 13:48:344341 sqlite3ShowTriggerStep(0);
4342 sqlite3ShowTriggerStepList(0);
4343 sqlite3ShowTrigger(0);
4344 sqlite3ShowTriggerList(0);
drh86d77fd2023-08-03 13:30:004345#endif
danf53487a2022-07-05 19:53:594346#ifndef SQLITE_OMIT_WINDOWFUNC
drh4cd82962022-04-07 13:48:344347 sqlite3ShowWindow(0);
4348 sqlite3ShowWinFunc(0);
danf53487a2022-07-05 19:53:594349#endif
drhf1ab6422022-07-11 18:26:144350 sqlite3ShowSelect(0);
drh4cd82962022-04-07 13:48:344351 }
4352#endif
drhf3af63f2009-05-09 18:59:424353 break;
4354 }
4355
4356
4357 /*
4358 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
4359 **
4360 ** This action provides a run-time test to see how the ALWAYS and
4361 ** NEVER macros were defined at compile-time.
4362 **
drhce2c4822017-10-03 17:17:344363 ** The return value is ALWAYS(X) if X is true, or 0 if X is false.
drhf3af63f2009-05-09 18:59:424364 **
4365 ** The recommended test is X==2. If the return value is 2, that means
4366 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
4367 ** default setting. If the return value is 1, then ALWAYS() is either
4368 ** hard-coded to true or else it asserts if its argument is false.
4369 ** The first behavior (hard-coded to true) is the case if
4370 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
4371 ** behavior (assert if the argument to ALWAYS() is false) is the case if
4372 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
4373 **
4374 ** The run-time test procedure might look something like this:
4375 **
4376 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
4377 ** // ALWAYS() and NEVER() are no-op pass-through macros
4378 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
4379 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
4380 ** }else{
4381 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
4382 ** }
4383 */
4384 case SQLITE_TESTCTRL_ALWAYS: {
4385 int x = va_arg(ap,int);
drhce2c4822017-10-03 17:17:344386 rc = x ? ALWAYS(x) : 0;
drhf3af63f2009-05-09 18:59:424387 break;
4388 }
drhc046e3e2009-07-15 11:26:444389
drh2cf4acb2014-04-18 00:06:024390 /*
4391 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
4392 **
4393 ** The integer returned reveals the byte-order of the computer on which
4394 ** SQLite is running:
4395 **
4396 ** 1 big-endian, determined at run-time
4397 ** 10 little-endian, determined at run-time
4398 ** 432101 big-endian, determined at compile-time
4399 ** 123410 little-endian, determined at compile-time
larrybrbc917382023-06-07 08:40:314400 */
drh2cf4acb2014-04-18 00:06:024401 case SQLITE_TESTCTRL_BYTEORDER: {
4402 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
4403 break;
4404 }
4405
drh07096f62009-12-22 23:52:324406 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
4407 **
larrybrbc917382023-06-07 08:40:314408 ** Enable or disable various optimizations for testing purposes. The
drh07096f62009-12-22 23:52:324409 ** argument N is a bitmask of optimizations to be disabled. For normal
4410 ** operation N should be 0. The idea is that a test program (like the
4411 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
4412 ** with various optimizations disabled to verify that the same answer
4413 ** is obtained in every case.
4414 */
4415 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
4416 sqlite3 *db = va_arg(ap, sqlite3*);
drhaf7b7652021-01-13 19:28:174417 db->dbOptFlags = va_arg(ap, u32);
drh07096f62009-12-22 23:52:324418 break;
4419 }
4420
drhbe1fdbd2024-08-21 18:57:554421 /* sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, sqlite3 *db, int *N)
4422 **
4423 ** Write the current optimization settings into *N. A zero bit means that
4424 ** the optimization is on, and a 1 bit means that the optimization is off.
4425 */
4426 case SQLITE_TESTCTRL_GETOPT: {
4427 sqlite3 *db = va_arg(ap, sqlite3*);
4428 int *pN = va_arg(ap, int*);
4429 *pN = db->dbOptFlags;
4430 break;
4431 }
4432
drhd7e185c2022-02-10 21:26:534433 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, onoff, xAlt);
danc17d6962011-06-21 12:47:304434 **
drhd7e185c2022-02-10 21:26:534435 ** If parameter onoff is 1, subsequent calls to localtime() fail.
4436 ** If 2, then invoke xAlt() instead of localtime(). If 0, normal
4437 ** processing.
4438 **
4439 ** xAlt arguments are void pointers, but they really want to be:
4440 **
4441 ** int xAlt(const time_t*, struct tm*);
4442 **
4443 ** xAlt should write results in to struct tm object of its 2nd argument
4444 ** and return zero on success, or return non-zero on failure.
danc17d6962011-06-21 12:47:304445 */
4446 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
4447 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
drhd7e185c2022-02-10 21:26:534448 if( sqlite3GlobalConfig.bLocaltimeFault==2 ){
drh0d58ae02022-02-11 11:37:124449 typedef int(*sqlite3LocaltimeType)(const void*,void*);
4450 sqlite3GlobalConfig.xAltLocaltime = va_arg(ap, sqlite3LocaltimeType);
drhd7e185c2022-02-10 21:26:534451 }else{
4452 sqlite3GlobalConfig.xAltLocaltime = 0;
4453 }
danc17d6962011-06-21 12:47:304454 break;
4455 }
4456
drh171c50e2020-01-01 15:43:304457 /* sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, sqlite3*);
drheea8eb62018-11-26 18:09:154458 **
drh171c50e2020-01-01 15:43:304459 ** Toggle the ability to use internal functions on or off for
4460 ** the database connection given in the argument.
drheea8eb62018-11-26 18:09:154461 */
4462 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: {
drh171c50e2020-01-01 15:43:304463 sqlite3 *db = va_arg(ap, sqlite3*);
4464 db->mDbFlags ^= DBFLAG_InternalFunc;
drheea8eb62018-11-26 18:09:154465 break;
4466 }
4467
drh09fe6142013-11-29 15:06:274468 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
4469 **
4470 ** Set or clear a flag that indicates that the database file is always well-
4471 ** formed and never corrupt. This flag is clear by default, indicating that
4472 ** database files might have arbitrary corruption. Setting the flag during
4473 ** testing causes certain assert() statements in the code to be activated
larrybrbc917382023-06-07 08:40:314474 ** that demonstrate invariants on well-formed database files.
drh09fe6142013-11-29 15:06:274475 */
4476 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
dan9c2552f2014-01-28 17:49:134477 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
drh09fe6142013-11-29 15:06:274478 break;
4479 }
4480
drh30842992019-08-12 14:17:434481 /* sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
4482 **
4483 ** Set or clear a flag that causes SQLite to verify that type, name,
drh1e32bed2020-06-19 13:33:534484 ** and tbl_name fields of the sqlite_schema table. This is normally
drh30842992019-08-12 14:17:434485 ** on, but it is sometimes useful to turn it off for testing.
drhca439a42020-07-22 21:05:234486 **
4487 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
4488 ** verification of rootpage numbers when parsing the schema. This
4489 ** is useful to make it easier to reach strange internal error states
drh8c1fbe82020-08-11 17:20:024490 ** during testing. The EXTRA_SCHEMA_CHECKS setting is always enabled
drhca439a42020-07-22 21:05:234491 ** in production.
drh30842992019-08-12 14:17:434492 */
4493 case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: {
4494 sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int);
4495 break;
4496 }
4497
drh9e5eb9c2016-09-18 16:08:104498 /* Set the threshold at which OP_Once counters reset back to zero.
mistachkine0736da2016-09-20 17:38:274499 ** By default this is 0x7ffffffe (over 2 billion), but that value is
drh9e5eb9c2016-09-18 16:08:104500 ** too big to test in a reasonable amount of time, so this control is
4501 ** provided to set a small and easily reachable reset value.
4502 */
4503 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {
4504 sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int);
4505 break;
4506 }
drh688852a2014-02-17 22:40:434507
4508 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
4509 **
larrybrbc917382023-06-07 08:40:314510 ** Set the VDBE coverage callback function to xCallback with context
drh688852a2014-02-17 22:40:434511 ** pointer ptr.
4512 */
4513 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
4514#ifdef SQLITE_VDBE_COVERAGE
drh7083a482018-07-10 16:04:044515 typedef void (*branch_callback)(void*,unsigned int,
4516 unsigned char,unsigned char);
drh688852a2014-02-17 22:40:434517 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
4518 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
4519#endif
4520 break;
4521 }
4522
dan8930c2a2014-04-03 16:25:294523 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
4524 case SQLITE_TESTCTRL_SORTER_MMAP: {
4525 sqlite3 *db = va_arg(ap, sqlite3*);
4526 db->nMaxSorterMmap = va_arg(ap, int);
4527 break;
4528 }
4529
drh43cfc232014-07-29 14:09:214530 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
4531 **
4532 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
4533 ** not.
4534 */
4535 case SQLITE_TESTCTRL_ISINIT: {
4536 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
4537 break;
4538 }
drh8964b342015-01-29 17:54:524539
drh3ba689d2015-03-03 14:00:114540 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
drh8964b342015-01-29 17:54:524541 **
drh1ffede82015-01-30 20:59:274542 ** This test control is used to create imposter tables. "db" is a pointer
4543 ** to the database connection. dbName is the database name (ex: "main" or
4544 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
4545 ** or off. "tnum" is the root page of the b-tree to which the imposter
4546 ** table should connect.
4547 **
4548 ** Enable imposter mode only when the schema has already been parsed. Then
drh3ba689d2015-03-03 14:00:114549 ** run a single CREATE TABLE statement to construct the imposter table in
4550 ** the parsed schema. Then turn imposter mode back off again.
drh1ffede82015-01-30 20:59:274551 **
4552 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
4553 ** the schema to be reparsed the next time it is needed. This has the
4554 ** effect of erasing all imposter tables.
drh8964b342015-01-29 17:54:524555 */
drh1ffede82015-01-30 20:59:274556 case SQLITE_TESTCTRL_IMPOSTER: {
drh8964b342015-01-29 17:54:524557 sqlite3 *db = va_arg(ap, sqlite3*);
drh39addd02021-12-06 15:08:304558 int iDb;
drh8fb15e32015-02-04 20:56:494559 sqlite3_mutex_enter(db->mutex);
drh39addd02021-12-06 15:08:304560 iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
4561 if( iDb>=0 ){
4562 db->init.iDb = iDb;
4563 db->init.busy = db->init.imposterTable = va_arg(ap,int);
4564 db->init.newTnum = va_arg(ap,int);
4565 if( db->init.busy==0 && db->init.newTnum>0 ){
4566 sqlite3ResetAllSchemasOfConnection(db);
4567 }
drh1ffede82015-01-30 20:59:274568 }
drh8fb15e32015-02-04 20:56:494569 sqlite3_mutex_leave(db->mutex);
drh8964b342015-01-29 17:54:524570 break;
4571 }
drh0d9de992017-12-26 18:04:234572
4573#if defined(YYCOVERAGE)
4574 /* sqlite3_test_control(SQLITE_TESTCTRL_PARSER_COVERAGE, FILE *out)
4575 **
4576 ** This test control (only available when SQLite is compiled with
4577 ** -DYYCOVERAGE) writes a report onto "out" that shows all
4578 ** state/lookahead combinations in the parser state machine
4579 ** which are never exercised. If any state is missed, make the
4580 ** return code SQLITE_ERROR.
4581 */
4582 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
4583 FILE *out = va_arg(ap, FILE*);
4584 if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
4585 break;
4586 }
4587#endif /* defined(YYCOVERAGE) */
drh0c8f4032019-05-03 21:17:284588
4589 /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
4590 **
4591 ** This test-control causes the most recent sqlite3_result_int64() value
4592 ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally,
4593 ** MEM_IntReal values only arise during an INSERT operation of integer
4594 ** values into a REAL column, so they can be challenging to test. This
4595 ** test-control enables us to write an intreal() SQL function that can
4596 ** inject an intreal() value at arbitrary places in an SQL statement,
4597 ** for testing purposes.
4598 */
4599 case SQLITE_TESTCTRL_RESULT_INTREAL: {
4600 sqlite3_context *pCtx = va_arg(ap, sqlite3_context*);
4601 sqlite3ResultIntReal(pCtx);
4602 break;
4603 }
drh37ccfcf2020-08-31 18:49:044604
4605 /* sqlite3_test_control(SQLITE_TESTCTRL_SEEK_COUNT,
4606 ** sqlite3 *db, // Database connection
4607 ** u64 *pnSeek // Write seek count here
4608 ** );
4609 **
4610 ** This test-control queries the seek-counter on the "main" database
4611 ** file. The seek-counter is written into *pnSeek and is then reset.
4612 ** The seek-count is only available if compiled with SQLITE_DEBUG.
4613 */
4614 case SQLITE_TESTCTRL_SEEK_COUNT: {
4615 sqlite3 *db = va_arg(ap, sqlite3*);
4616 u64 *pn = va_arg(ap, sqlite3_uint64*);
4617 *pn = sqlite3BtreeSeekCount(db->aDb->pBt);
drhd321b6f2020-09-01 00:26:214618 (void)db; /* Silence harmless unused variable warning */
drh37ccfcf2020-08-31 18:49:044619 break;
4620 }
4621
drhc0622a42020-12-04 01:17:574622 /* sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, op, ptr)
4623 **
larrybrbc917382023-06-07 08:40:314624 ** "ptr" is a pointer to a u32.
drhc0622a42020-12-04 01:17:574625 **
drh5e431be2022-04-06 11:08:384626 ** op==0 Store the current sqlite3TreeTrace in *ptr
4627 ** op==1 Set sqlite3TreeTrace to the value *ptr
drh179c32c2023-06-01 20:38:224628 ** op==2 Store the current sqlite3WhereTrace in *ptr
drhc0622a42020-12-04 01:17:574629 ** op==3 Set sqlite3WhereTrace to the value *ptr
4630 */
4631 case SQLITE_TESTCTRL_TRACEFLAGS: {
drhfda8e492020-12-04 16:04:454632 int opTrace = va_arg(ap, int);
drhc0622a42020-12-04 01:17:574633 u32 *ptr = va_arg(ap, u32*);
drhfda8e492020-12-04 16:04:454634 switch( opTrace ){
drh5e431be2022-04-06 11:08:384635 case 0: *ptr = sqlite3TreeTrace; break;
4636 case 1: sqlite3TreeTrace = *ptr; break;
4637 case 2: *ptr = sqlite3WhereTrace; break;
4638 case 3: sqlite3WhereTrace = *ptr; break;
drhc0622a42020-12-04 01:17:574639 }
4640 break;
drhc0622a42020-12-04 01:17:574641 }
drhf3c12562021-06-04 13:16:464642
drh7e910f62021-12-09 01:28:154643 /* sqlite3_test_control(SQLITE_TESTCTRL_LOGEST,
4644 ** double fIn, // Input value
4645 ** int *pLogEst, // sqlite3LogEstFromDouble(fIn)
4646 ** u64 *pInt, // sqlite3LogEstToInt(*pLogEst)
4647 ** int *pLogEst2 // sqlite3LogEst(*pInt)
4648 ** );
4649 **
4650 ** Test access for the LogEst conversion routines.
4651 */
4652 case SQLITE_TESTCTRL_LOGEST: {
4653 double rIn = va_arg(ap, double);
4654 LogEst rLogEst = sqlite3LogEstFromDouble(rIn);
drhb528a5a2022-03-09 13:22:534655 int *pI1 = va_arg(ap,int*);
4656 u64 *pU64 = va_arg(ap,u64*);
4657 int *pI2 = va_arg(ap,int*);
4658 *pI1 = rLogEst;
4659 *pU64 = sqlite3LogEstToInt(rLogEst);
4660 *pI2 = sqlite3LogEst(*pU64);
drh7e910f62021-12-09 01:28:154661 break;
4662 }
larrybrbc917382023-06-07 08:40:314663
drh9d82a2c2021-06-15 20:07:164664#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
drh2d26cfc2021-06-04 13:40:264665 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
drhf3c12562021-06-04 13:16:464666 **
drh2d26cfc2021-06-04 13:40:264667 ** If "id" is an integer between 1 and SQLITE_NTUNE then set the value
4668 ** of the id-th tuning parameter to *piValue. If "id" is between -1
4669 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
4670 ** tuning parameter into *piValue.
drhf3c12562021-06-04 13:16:464671 **
4672 ** Tuning parameters are for use during transient development builds,
4673 ** to help find the best values for constants in the query planner.
4674 ** Access tuning parameters using the Tuning(ID) macro. Set the
4675 ** parameters in the CLI using ".testctrl tune ID VALUE".
4676 **
4677 ** Transient use only. Tuning parameters should not be used in
4678 ** checked-in code.
4679 */
4680 case SQLITE_TESTCTRL_TUNE: {
4681 int id = va_arg(ap, int);
drh2d26cfc2021-06-04 13:40:264682 int *piValue = va_arg(ap, int*);
4683 if( id>0 && id<=SQLITE_NTUNE ){
4684 Tuning(id) = *piValue;
4685 }else if( id<0 && id>=-SQLITE_NTUNE ){
4686 *piValue = Tuning(-id);
4687 }else{
4688 rc = SQLITE_NOTFOUND;
4689 }
drhf3c12562021-06-04 13:16:464690 break;
4691 }
4692#endif
drh7d2eaae2023-12-11 17:03:124693
drhba550562023-12-11 19:00:444694 /* sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &onOff);
drh7d2eaae2023-12-11 17:03:124695 **
4696 ** Activate or deactivate validation of JSONB that is generated from
4697 ** text. Off by default, as the validation is slow. Validation is
4698 ** only available if compiled using SQLITE_DEBUG.
drhba550562023-12-11 19:00:444699 **
4700 ** If onOff is initially 1, then turn it on. If onOff is initially
4701 ** off, turn it off. If onOff is initially -1, then change onOff
4702 ** to be the current setting.
drh7d2eaae2023-12-11 17:03:124703 */
drhba550562023-12-11 19:00:444704 case SQLITE_TESTCTRL_JSON_SELFCHECK: {
drhaa08f882024-01-03 20:40:174705#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
drhba550562023-12-11 19:00:444706 int *pOnOff = va_arg(ap, int*);
4707 if( *pOnOff<0 ){
4708 *pOnOff = sqlite3Config.bJsonSelfcheck;
4709 }else{
4710 sqlite3Config.bJsonSelfcheck = (u8)((*pOnOff)&0xff);
4711 }
drh7d2eaae2023-12-11 17:03:124712#endif
4713 break;
4714 }
drhed13d982008-01-31 14:43:244715 }
4716 va_end(ap);
drhd12602a2016-12-07 15:49:024717#endif /* SQLITE_UNTESTABLE */
danielk19777c36d072008-01-31 15:31:014718 return rc;
drhed13d982008-01-31 14:43:244719}
drhcc487d12011-05-17 18:53:084720
4721/*
drh532b0d22020-01-27 14:40:444722** The Pager stores the Database filename, Journal filename, and WAL filename
4723** consecutively in memory, in that order. The database filename is prefixed
4724** by four zero bytes. Locate the start of the database filename by searching
4725** backwards for the first byte following four consecutive zero bytes.
4726**
4727** This only works if the filename passed in was obtained from the Pager.
4728*/
4729static const char *databaseName(const char *zName){
4730 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
4731 zName--;
4732 }
4733 return zName;
4734}
4735
4736/*
drh4defddd2020-02-18 19:49:484737** Append text z[] to the end of p[]. Return a pointer to the first
4738** character after then zero terminator on the new text in p[].
4739*/
4740static char *appendText(char *p, const char *z){
4741 size_t n = strlen(z);
4742 memcpy(p, z, n+1);
4743 return p+n+1;
4744}
4745
4746/*
4747** Allocate memory to hold names for a database, journal file, WAL file,
4748** and query parameters. The pointer returned is valid for use by
4749** sqlite3_filename_database() and sqlite3_uri_parameter() and related
4750** functions.
4751**
4752** Memory layout must be compatible with that generated by the pager
4753** and expected by sqlite3_uri_parameter() and databaseName().
4754*/
dan52d5d472022-11-01 17:43:194755const char *sqlite3_create_filename(
drh4defddd2020-02-18 19:49:484756 const char *zDatabase,
4757 const char *zJournal,
4758 const char *zWal,
4759 int nParam,
4760 const char **azParam
4761){
4762 sqlite3_int64 nByte;
4763 int i;
4764 char *pResult, *p;
4765 nByte = strlen(zDatabase) + strlen(zJournal) + strlen(zWal) + 10;
4766 for(i=0; i<nParam*2; i++){
4767 nByte += strlen(azParam[i])+1;
4768 }
4769 pResult = p = sqlite3_malloc64( nByte );
4770 if( p==0 ) return 0;
4771 memset(p, 0, 4);
4772 p += 4;
4773 p = appendText(p, zDatabase);
4774 for(i=0; i<nParam*2; i++){
4775 p = appendText(p, azParam[i]);
4776 }
4777 *(p++) = 0;
4778 p = appendText(p, zJournal);
4779 p = appendText(p, zWal);
4780 *(p++) = 0;
4781 *(p++) = 0;
4782 assert( (sqlite3_int64)(p - pResult)==nByte );
4783 return pResult + 4;
4784}
4785
4786/*
4787** Free memory obtained from sqlite3_create_filename(). It is a severe
4788** error to call this routine with any parameter other than a pointer
4789** previously obtained from sqlite3_create_filename() or a NULL pointer.
4790*/
dan52d5d472022-11-01 17:43:194791void sqlite3_free_filename(const char *p){
drh4defddd2020-02-18 19:49:484792 if( p==0 ) return;
dan52d5d472022-11-01 17:43:194793 p = databaseName(p);
4794 sqlite3_free((char*)p - 4);
drh4defddd2020-02-18 19:49:484795}
4796
4797
4798/*
drhcc487d12011-05-17 18:53:084799** This is a utility routine, useful to VFS implementations, that checks
larrybrbc917382023-06-07 08:40:314800** to see if a database file was a URI that contained a specific query
drhcc487d12011-05-17 18:53:084801** parameter, and if so obtains the value of the query parameter.
4802**
4803** The zFilename argument is the filename pointer passed into the xOpen()
4804** method of a VFS implementation. The zParam argument is the name of the
4805** query parameter we seek. This routine returns the value of the zParam
4806** parameter if it exists. If the parameter does not exist, this routine
4807** returns a NULL pointer.
4808*/
4809const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
drh9ca95732014-10-24 00:35:584810 if( zFilename==0 || zParam==0 ) return 0;
drh532b0d22020-01-27 14:40:444811 zFilename = databaseName(zFilename);
drh1eac9662020-02-01 13:30:394812 return uriParameter(zFilename, zParam);
drhcc487d12011-05-17 18:53:084813}
drh283829c2011-11-17 00:56:204814
4815/*
drh80804032020-01-11 16:08:314816** Return a pointer to the name of Nth query parameter of the filename.
4817*/
4818const char *sqlite3_uri_key(const char *zFilename, int N){
4819 if( zFilename==0 || N<0 ) return 0;
drh532b0d22020-01-27 14:40:444820 zFilename = databaseName(zFilename);
drh80804032020-01-11 16:08:314821 zFilename += sqlite3Strlen30(zFilename) + 1;
drhc59ffa82021-10-04 15:08:494822 while( ALWAYS(zFilename) && zFilename[0] && (N--)>0 ){
drh80804032020-01-11 16:08:314823 zFilename += sqlite3Strlen30(zFilename) + 1;
4824 zFilename += sqlite3Strlen30(zFilename) + 1;
4825 }
4826 return zFilename[0] ? zFilename : 0;
4827}
4828
4829/*
drh92913722011-12-23 00:07:334830** Return a boolean value for a query parameter.
4831*/
4832int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
4833 const char *z = sqlite3_uri_parameter(zFilename, zParam);
drh38d9c612012-01-31 14:24:474834 bDflt = bDflt!=0;
4835 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
drh92913722011-12-23 00:07:334836}
4837
4838/*
4839** Return a 64-bit integer value for a query parameter.
4840*/
4841sqlite3_int64 sqlite3_uri_int64(
4842 const char *zFilename, /* Filename as passed to xOpen */
4843 const char *zParam, /* URI parameter sought */
4844 sqlite3_int64 bDflt /* return if parameter is missing */
4845){
4846 const char *z = sqlite3_uri_parameter(zFilename, zParam);
4847 sqlite3_int64 v;
drh84d4f1a2017-09-20 10:47:104848 if( z && sqlite3DecOrHexToI64(z, &v)==0 ){
drh92913722011-12-23 00:07:334849 bDflt = v;
4850 }
4851 return bDflt;
4852}
4853
4854/*
drh8875b9e2020-01-10 18:05:554855** Translate a filename that was handed to a VFS routine into the corresponding
4856** database, journal, or WAL file.
4857**
4858** It is an error to pass this routine a filename string that was not
4859** passed into the VFS from the SQLite core. Doing so is similar to
4860** passing free() a pointer that was not obtained from malloc() - it is
4861** an error that we cannot easily detect but that will likely cause memory
4862** corruption.
4863*/
4864const char *sqlite3_filename_database(const char *zFilename){
drhcc9309c2021-10-02 17:12:584865 if( zFilename==0 ) return 0;
drh532b0d22020-01-27 14:40:444866 return databaseName(zFilename);
drh8875b9e2020-01-10 18:05:554867}
4868const char *sqlite3_filename_journal(const char *zFilename){
drhcc9309c2021-10-02 17:12:584869 if( zFilename==0 ) return 0;
drh532b0d22020-01-27 14:40:444870 zFilename = databaseName(zFilename);
4871 zFilename += sqlite3Strlen30(zFilename) + 1;
drhc59ffa82021-10-04 15:08:494872 while( ALWAYS(zFilename) && zFilename[0] ){
drh532b0d22020-01-27 14:40:444873 zFilename += sqlite3Strlen30(zFilename) + 1;
4874 zFilename += sqlite3Strlen30(zFilename) + 1;
4875 }
4876 return zFilename + 1;
drh8875b9e2020-01-10 18:05:554877}
4878const char *sqlite3_filename_wal(const char *zFilename){
drh532b0d22020-01-27 14:40:444879#ifdef SQLITE_OMIT_WAL
4880 return 0;
4881#else
4882 zFilename = sqlite3_filename_journal(zFilename);
drhcc9309c2021-10-02 17:12:584883 if( zFilename ) zFilename += sqlite3Strlen30(zFilename) + 1;
drh532b0d22020-01-27 14:40:444884 return zFilename;
4885#endif
drh8875b9e2020-01-10 18:05:554886}
4887
4888/*
drh421377e2012-03-15 21:28:544889** Return the Btree pointer identified by zDbName. Return NULL if not found.
4890*/
4891Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
drh29518092016-12-24 19:37:164892 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
4893 return iDb<0 ? 0 : db->aDb[iDb].pBt;
drh421377e2012-03-15 21:28:544894}
4895
4896/*
drhff162672022-05-17 14:59:054897** Return the name of the N-th database schema. Return NULL if N is out
4898** of range.
4899*/
4900const char *sqlite3_db_name(sqlite3 *db, int N){
4901#ifdef SQLITE_ENABLE_API_ARMOR
4902 if( !sqlite3SafetyCheckOk(db) ){
4903 (void)SQLITE_MISUSE_BKPT;
4904 return 0;
4905 }
4906#endif
4907 if( N<0 || N>=db->nDb ){
4908 return 0;
4909 }else{
4910 return db->aDb[N].zDbSName;
4911 }
4912}
4913
4914/*
drh283829c2011-11-17 00:56:204915** Return the filename of the database associated with a database
4916** connection.
4917*/
4918const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
mistachkincd54bab2014-12-20 21:14:144919 Btree *pBt;
drh9ca95732014-10-24 00:35:584920#ifdef SQLITE_ENABLE_API_ARMOR
4921 if( !sqlite3SafetyCheckOk(db) ){
4922 (void)SQLITE_MISUSE_BKPT;
4923 return 0;
4924 }
4925#endif
mistachkincd54bab2014-12-20 21:14:144926 pBt = sqlite3DbNameToBtree(db, zDbName);
drh421377e2012-03-15 21:28:544927 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
4928}
4929
4930/*
4931** Return 1 if database is read-only or 0 if read/write. Return -1 if
4932** no such database exists.
4933*/
4934int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
mistachkincd54bab2014-12-20 21:14:144935 Btree *pBt;
drh9ca95732014-10-24 00:35:584936#ifdef SQLITE_ENABLE_API_ARMOR
4937 if( !sqlite3SafetyCheckOk(db) ){
4938 (void)SQLITE_MISUSE_BKPT;
4939 return -1;
4940 }
4941#endif
mistachkincd54bab2014-12-20 21:14:144942 pBt = sqlite3DbNameToBtree(db, zDbName);
drh781597f2014-05-21 08:21:074943 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
drh283829c2011-11-17 00:56:204944}
danfc1acf32015-12-05 20:51:544945
4946#ifdef SQLITE_ENABLE_SNAPSHOT
4947/*
larrybrbc917382023-06-07 08:40:314948** Obtain a snapshot handle for the snapshot of database zDb currently
danfc1acf32015-12-05 20:51:544949** being read by handle db.
4950*/
4951int sqlite3_snapshot_get(
larrybrbc917382023-06-07 08:40:314952 sqlite3 *db,
danfc1acf32015-12-05 20:51:544953 const char *zDb,
4954 sqlite3_snapshot **ppSnapshot
4955){
4956 int rc = SQLITE_ERROR;
4957#ifndef SQLITE_OMIT_WAL
danfc1acf32015-12-05 20:51:544958
4959#ifdef SQLITE_ENABLE_API_ARMOR
4960 if( !sqlite3SafetyCheckOk(db) ){
4961 return SQLITE_MISUSE_BKPT;
4962 }
4963#endif
4964 sqlite3_mutex_enter(db->mutex);
4965
danedace5d2016-11-18 18:43:394966 if( db->autoCommit==0 ){
4967 int iDb = sqlite3FindDbName(db, zDb);
4968 if( iDb==0 || iDb>1 ){
4969 Btree *pBt = db->aDb[iDb].pBt;
drh99744fa2020-08-25 19:09:074970 if( SQLITE_TXN_WRITE!=sqlite3BtreeTxnState(pBt) ){
dand0720ee2024-09-26 18:02:174971 Pager *pPager = sqlite3BtreePager(pBt);
4972 i64 dummy = 0;
4973 sqlite3PagerSnapshotOpen(pPager, (sqlite3_snapshot*)&dummy);
drh685a50a2018-06-06 18:50:504974 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
dand0720ee2024-09-26 18:02:174975 sqlite3PagerSnapshotOpen(pPager, 0);
danedace5d2016-11-18 18:43:394976 if( rc==SQLITE_OK ){
4977 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot);
4978 }
dan7116dc62015-12-10 20:03:084979 }
danfc1acf32015-12-05 20:51:544980 }
4981 }
4982
4983 sqlite3_mutex_leave(db->mutex);
4984#endif /* SQLITE_OMIT_WAL */
4985 return rc;
4986}
4987
4988/*
larrybrbc917382023-06-07 08:40:314989** Open a read-transaction on the snapshot identified by pSnapshot.
danfc1acf32015-12-05 20:51:544990*/
4991int sqlite3_snapshot_open(
larrybrbc917382023-06-07 08:40:314992 sqlite3 *db,
4993 const char *zDb,
danfc1acf32015-12-05 20:51:544994 sqlite3_snapshot *pSnapshot
4995){
4996 int rc = SQLITE_ERROR;
4997#ifndef SQLITE_OMIT_WAL
4998
4999#ifdef SQLITE_ENABLE_API_ARMOR
5000 if( !sqlite3SafetyCheckOk(db) ){
5001 return SQLITE_MISUSE_BKPT;
5002 }
5003#endif
5004 sqlite3_mutex_enter(db->mutex);
5005 if( db->autoCommit==0 ){
5006 int iDb;
5007 iDb = sqlite3FindDbName(db, zDb);
5008 if( iDb==0 || iDb>1 ){
5009 Btree *pBt = db->aDb[iDb].pBt;
drh99744fa2020-08-25 19:09:075010 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ){
danfa3d4c12018-08-06 17:12:365011 Pager *pPager = sqlite3BtreePager(pBt);
5012 int bUnlock = 0;
drh99744fa2020-08-25 19:09:075013 if( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_NONE ){
danfa3d4c12018-08-06 17:12:365014 if( db->nVdbeActive==0 ){
5015 rc = sqlite3PagerSnapshotCheck(pPager, pSnapshot);
5016 if( rc==SQLITE_OK ){
5017 bUnlock = 1;
5018 rc = sqlite3BtreeCommit(pBt);
5019 }
5020 }
5021 }else{
5022 rc = SQLITE_OK;
5023 }
5024 if( rc==SQLITE_OK ){
dan861fb1e2020-05-06 19:14:415025 rc = sqlite3PagerSnapshotOpen(pPager, pSnapshot);
danfa3d4c12018-08-06 17:12:365026 }
danfc1acf32015-12-05 20:51:545027 if( rc==SQLITE_OK ){
drh685a50a2018-06-06 18:50:505028 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
dan861fb1e2020-05-06 19:14:415029 sqlite3PagerSnapshotOpen(pPager, 0);
danfa3d4c12018-08-06 17:12:365030 }
5031 if( bUnlock ){
5032 sqlite3PagerSnapshotUnlock(pPager);
danfc1acf32015-12-05 20:51:545033 }
5034 }
5035 }
5036 }
5037
5038 sqlite3_mutex_leave(db->mutex);
5039#endif /* SQLITE_OMIT_WAL */
5040 return rc;
5041}
5042
5043/*
dan11584982016-11-18 20:49:435044** Recover as many snapshots as possible from the wal file associated with
5045** schema zDb of database db.
5046*/
5047int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){
5048 int rc = SQLITE_ERROR;
dan11584982016-11-18 20:49:435049#ifndef SQLITE_OMIT_WAL
drh7585f492022-09-10 18:20:595050 int iDb;
dan11584982016-11-18 20:49:435051
5052#ifdef SQLITE_ENABLE_API_ARMOR
5053 if( !sqlite3SafetyCheckOk(db) ){
5054 return SQLITE_MISUSE_BKPT;
5055 }
5056#endif
5057
5058 sqlite3_mutex_enter(db->mutex);
5059 iDb = sqlite3FindDbName(db, zDb);
5060 if( iDb==0 || iDb>1 ){
5061 Btree *pBt = db->aDb[iDb].pBt;
drh99744fa2020-08-25 19:09:075062 if( SQLITE_TXN_NONE==sqlite3BtreeTxnState(pBt) ){
drh685a50a2018-06-06 18:50:505063 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
dan93f51132016-11-19 18:31:375064 if( rc==SQLITE_OK ){
5065 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt));
5066 sqlite3BtreeCommit(pBt);
5067 }
dan11584982016-11-18 20:49:435068 }
5069 }
5070 sqlite3_mutex_leave(db->mutex);
5071#endif /* SQLITE_OMIT_WAL */
5072 return rc;
5073}
5074
5075/*
danfc1acf32015-12-05 20:51:545076** Free a snapshot handle obtained from sqlite3_snapshot_get().
5077*/
5078void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){
5079 sqlite3_free(pSnapshot);
5080}
5081#endif /* SQLITE_ENABLE_SNAPSHOT */
danda1f49b2017-06-16 19:51:475082
5083#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
5084/*
5085** Given the name of a compile-time option, return true if that option
5086** was used and false if not.
5087**
5088** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
5089** is not required for a match.
5090*/
5091int sqlite3_compileoption_used(const char *zOptName){
5092 int i, n;
5093 int nOpt;
5094 const char **azCompileOpt;
larrybrbc917382023-06-07 08:40:315095
stephan24f6bac2023-10-15 13:36:215096#ifdef SQLITE_ENABLE_API_ARMOR
danda1f49b2017-06-16 19:51:475097 if( zOptName==0 ){
5098 (void)SQLITE_MISUSE_BKPT;
5099 return 0;
5100 }
5101#endif
5102
5103 azCompileOpt = sqlite3CompileOptions(&nOpt);
5104
5105 if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
5106 n = sqlite3Strlen30(zOptName);
5107
larrybrbc917382023-06-07 08:40:315108 /* Since nOpt is normally in single digits, a linear search is
danda1f49b2017-06-16 19:51:475109 ** adequate. No need for a binary search. */
5110 for(i=0; i<nOpt; i++){
5111 if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
5112 && sqlite3IsIdChar((unsigned char)azCompileOpt[i][n])==0
5113 ){
5114 return 1;
5115 }
5116 }
5117 return 0;
5118}
5119
5120/*
5121** Return the N-th compile-time option string. If N is out of range,
5122** return a NULL pointer.
5123*/
5124const char *sqlite3_compileoption_get(int N){
5125 int nOpt;
5126 const char **azCompileOpt;
5127 azCompileOpt = sqlite3CompileOptions(&nOpt);
5128 if( N>=0 && N<nOpt ){
5129 return azCompileOpt[N];
5130 }
5131 return 0;
5132}
5133#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */