blob: ef2fa66ec3a18a16b2bdf6e047ebdaa908a1dc66 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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:00 +000010**
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:00 +000016*/
17#include "sqliteInt.h"
drh58f1c8b2008-05-26 20:19:25 +000018
drh820a9062008-01-31 13:35:48 +000019#ifdef SQLITE_ENABLE_FTS3
20# include "fts3.h"
21#endif
drh58f1c8b2008-05-26 20:19:25 +000022#ifdef SQLITE_ENABLE_RTREE
23# include "rtree.h"
24#endif
danielk19771c826652008-09-08 08:08:09 +000025#ifdef SQLITE_ENABLE_ICU
26# include "sqliteicu.h"
27#endif
drhc306e082015-10-08 23:37:00 +000028#ifdef SQLITE_ENABLE_JSON1
29int sqlite3Json1Init(sqlite3*);
30#endif
31#ifdef SQLITE_ENABLE_FTS5
32int sqlite3Fts5Init(sqlite3*);
33#endif
drh75897232000-05-29 14:26:00 +000034
drhb3190c12008-12-08 21:37:14 +000035#ifndef SQLITE_AMALGAMATION
drh9f129f42010-08-31 15:27:32 +000036/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
37** contains the text of SQLITE_VERSION macro.
38*/
danielk197724b03fd2004-05-10 10:34:34 +000039const char sqlite3_version[] = SQLITE_VERSION;
drhb3190c12008-12-08 21:37:14 +000040#endif
drh9f129f42010-08-31 15:27:32 +000041
42/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
43** a pointer to the to the sqlite3_version[] string constant.
44*/
drh4aec8b62004-08-28 16:19:00 +000045const char *sqlite3_libversion(void){ return sqlite3_version; }
drh9f129f42010-08-31 15:27:32 +000046
47/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
48** pointer to a string constant whose value is the same as the
49** SQLITE_SOURCE_ID C preprocessor macro.
50*/
drh47baebc2009-08-14 16:01:24 +000051const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
drh9f129f42010-08-31 15:27:32 +000052
53/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
54** returns an integer equal to SQLITE_VERSION_NUMBER.
55*/
danielk197799ba19e2005-02-05 07:33:34 +000056int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drh9f129f42010-08-31 15:27:32 +000057
drh5dc2bcd2012-01-02 15:45:12 +000058/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
drhb8a45bb2011-12-31 21:51:55 +000059** zero if and only if SQLite was compiled with mutexing code omitted due to
drh9f129f42010-08-31 15:27:32 +000060** the SQLITE_THREADSAFE compile-time option being set to 0.
61*/
drhb67e8bf2007-08-30 20:09:48 +000062int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
drhb217a572000-08-22 13:40:18 +000063
mistachkin34cf2582015-04-02 17:46:52 +000064/*
65** When compiling the test fixture or with debugging enabled (on Win32),
66** this variable being set to non-zero will cause OSTRACE macros to emit
67** extra diagnostic information.
68*/
mistachkinfb383e92015-04-16 03:24:38 +000069#ifdef SQLITE_HAVE_OS_TRACE
mistachkin34cf2582015-04-02 17:46:52 +000070# ifndef SQLITE_DEBUG_OS_TRACE
71# define SQLITE_DEBUG_OS_TRACE 0
72# endif
73 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
74#endif
75
drhe265b082008-05-01 17:03:49 +000076#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
drhb217a572000-08-22 13:40:18 +000077/*
drhb0603412007-02-28 04:47:26 +000078** If the following function pointer is not NULL and if
79** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
80** I/O active are written using this function. These messages
81** are intended for debugging activity only.
82*/
mistachkin9871a932015-03-27 00:21:52 +000083SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0;
drhe265b082008-05-01 17:03:49 +000084#endif
drhb0603412007-02-28 04:47:26 +000085
86/*
drha16313e2007-03-30 11:29:32 +000087** If the following global variable points to a string which is the
88** name of a directory, then that directory will be used to store
89** temporary files.
90**
91** See also the "PRAGMA temp_store_directory" SQL command.
92*/
93char *sqlite3_temp_directory = 0;
94
drhc5499be2008-04-01 15:06:33 +000095/*
mistachkina112d142012-03-14 00:44:01 +000096** If the following global variable points to a string which is the
97** name of a directory, then that directory will be used to store
98** all database files specified with a relative pathname.
99**
100** See also the "PRAGMA data_store_directory" SQL command.
101*/
102char *sqlite3_data_directory = 0;
103
104/*
drh40257ff2008-06-13 18:24:27 +0000105** Initialize SQLite.
106**
107** This routine must be called to initialize the memory allocation,
drh93ed56d2008-08-12 15:21:11 +0000108** VFS, and mutex subsystems prior to doing any serious work with
drh40257ff2008-06-13 18:24:27 +0000109** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
110** this routine will be called automatically by key routines such as
111** sqlite3_open().
112**
113** This routine is a no-op except on its very first call for the process,
114** or for the first call after a call to sqlite3_shutdown.
drh93ed56d2008-08-12 15:21:11 +0000115**
116** The first thread to call this routine runs the initialization to
117** completion. If subsequent threads call this routine before the first
118** thread has finished the initialization process, then the subsequent
119** threads must block until the first thread finishes with the initialization.
120**
121** The first thread might call this routine recursively. Recursive
122** calls to this routine should not block, of course. Otherwise the
123** initialization process would never complete.
124**
125** Let X be the first thread to enter this routine. Let Y be some other
126** thread. Then while the initial invocation of this routine by X is
127** incomplete, it is required that:
128**
129** * Calls to this routine from Y must block until the outer-most
130** call by X completes.
131**
132** * Recursive calls to this routine from thread X return immediately
133** without blocking.
drh40257ff2008-06-13 18:24:27 +0000134*/
135int sqlite3_initialize(void){
drh30ddce62011-10-15 00:16:30 +0000136 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
danielk1977075c23a2008-09-01 18:34:20 +0000137 int rc; /* Result code */
drhab80be92013-08-08 14:38:45 +0000138#ifdef SQLITE_EXTRA_INIT
139 int bRunExtraInit = 0; /* Extra initialization needed */
140#endif
danielk1977075c23a2008-09-01 18:34:20 +0000141
142#ifdef SQLITE_OMIT_WSD
danielk1977a8f83bf2008-09-02 16:22:28 +0000143 rc = sqlite3_wsd_init(4096, 24);
danielk1977075c23a2008-09-01 18:34:20 +0000144 if( rc!=SQLITE_OK ){
145 return rc;
146 }
147#endif
danielk197771bc31c2008-06-26 08:29:34 +0000148
drh2b4905c2015-03-23 18:52:56 +0000149 /* If the following assert() fails on some obscure processor/compiler
150 ** combination, the work-around is to set the correct pointer
151 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
152 assert( SQLITE_PTRSIZE==sizeof(char*) );
153
drh93ed56d2008-08-12 15:21:11 +0000154 /* If SQLite is already completely initialized, then this call
155 ** to sqlite3_initialize() should be a no-op. But the initialization
156 ** must be complete. So isInit must not be set until the very end
157 ** of this routine.
158 */
danielk1977075c23a2008-09-01 18:34:20 +0000159 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
danielk197771bc31c2008-06-26 08:29:34 +0000160
drh93ed56d2008-08-12 15:21:11 +0000161 /* Make sure the mutex subsystem is initialized. If unable to
162 ** initialize the mutex subsystem, return early with the error.
163 ** If the system is so sick that we are unable to allocate a mutex,
164 ** there is not much SQLite is going to be able to do.
165 **
166 ** The mutex subsystem must take care of serializing its own
167 ** initialization.
168 */
danielk1977d0251742008-06-18 18:57:42 +0000169 rc = sqlite3MutexInit();
drh93ed56d2008-08-12 15:21:11 +0000170 if( rc ) return rc;
danielk197771bc31c2008-06-26 08:29:34 +0000171
drh93ed56d2008-08-12 15:21:11 +0000172 /* Initialize the malloc() system and the recursive pInitMutex mutex.
173 ** This operation is protected by the STATIC_MASTER mutex. Note that
174 ** MutexAlloc() is called for a static mutex prior to initializing the
175 ** malloc subsystem - this implies that the allocation of a static
176 ** mutex must not require support from the malloc subsystem.
177 */
drh30ddce62011-10-15 00:16:30 +0000178 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
drh93ed56d2008-08-12 15:21:11 +0000179 sqlite3_mutex_enter(pMaster);
dane1ab2192009-08-17 15:16:19 +0000180 sqlite3GlobalConfig.isMutexInit = 1;
danielk1977075c23a2008-09-01 18:34:20 +0000181 if( !sqlite3GlobalConfig.isMallocInit ){
drh93ed56d2008-08-12 15:21:11 +0000182 rc = sqlite3MallocInit();
183 }
drh40257ff2008-06-13 18:24:27 +0000184 if( rc==SQLITE_OK ){
danielk1977075c23a2008-09-01 18:34:20 +0000185 sqlite3GlobalConfig.isMallocInit = 1;
186 if( !sqlite3GlobalConfig.pInitMutex ){
drh9ac06502009-08-17 13:42:29 +0000187 sqlite3GlobalConfig.pInitMutex =
188 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
danielk1977075c23a2008-09-01 18:34:20 +0000189 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
drh93ed56d2008-08-12 15:21:11 +0000190 rc = SQLITE_NOMEM;
drh40257ff2008-06-13 18:24:27 +0000191 }
192 }
danielk197777eb5bb2008-09-22 17:22:19 +0000193 }
194 if( rc==SQLITE_OK ){
danielk1977075c23a2008-09-01 18:34:20 +0000195 sqlite3GlobalConfig.nRefInitMutex++;
drh93ed56d2008-08-12 15:21:11 +0000196 }
197 sqlite3_mutex_leave(pMaster);
danielk197771bc31c2008-06-26 08:29:34 +0000198
dane1ab2192009-08-17 15:16:19 +0000199 /* If rc is not SQLITE_OK at this point, then either the malloc
200 ** subsystem could not be initialized or the system failed to allocate
201 ** the pInitMutex mutex. Return an error in either case. */
drh93ed56d2008-08-12 15:21:11 +0000202 if( rc!=SQLITE_OK ){
203 return rc;
204 }
205
206 /* Do the rest of the initialization under the recursive mutex so
207 ** that we will be able to handle recursive calls into
208 ** sqlite3_initialize(). The recursive calls normally come through
209 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
210 ** recursive calls might also be possible.
drhf759bb82010-09-09 18:25:34 +0000211 **
212 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
213 ** to the xInit method, so the xInit method need not be threadsafe.
214 **
215 ** The following mutex is what serializes access to the appdef pcache xInit
216 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the
217 ** call to sqlite3PcacheInitialize().
drh93ed56d2008-08-12 15:21:11 +0000218 */
danielk1977075c23a2008-09-01 18:34:20 +0000219 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
danielk1977502b4e02008-09-02 14:07:24 +0000220 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000221 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
danielk1977502b4e02008-09-02 14:07:24 +0000222 sqlite3GlobalConfig.inProgress = 1;
danielk1977075c23a2008-09-01 18:34:20 +0000223 memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
drh70a8ca32008-08-21 18:49:27 +0000224 sqlite3RegisterGlobalFunctions();
dane1ab2192009-08-17 15:16:19 +0000225 if( sqlite3GlobalConfig.isPCacheInit==0 ){
226 rc = sqlite3PcacheInitialize();
227 }
danielk19778c0a7912008-08-20 14:49:23 +0000228 if( rc==SQLITE_OK ){
dane1ab2192009-08-17 15:16:19 +0000229 sqlite3GlobalConfig.isPCacheInit = 1;
dan3d6e0602009-08-17 15:52:25 +0000230 rc = sqlite3OsInit();
drh0bf9f7b2009-04-17 16:54:22 +0000231 }
232 if( rc==SQLITE_OK ){
danielk19775b775292008-09-02 09:38:06 +0000233 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
234 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
drh0bf9f7b2009-04-17 16:54:22 +0000235 sqlite3GlobalConfig.isInit = 1;
drhab80be92013-08-08 14:38:45 +0000236#ifdef SQLITE_EXTRA_INIT
237 bRunExtraInit = 1;
238#endif
danielk19778c0a7912008-08-20 14:49:23 +0000239 }
danielk1977502b4e02008-09-02 14:07:24 +0000240 sqlite3GlobalConfig.inProgress = 0;
drh40257ff2008-06-13 18:24:27 +0000241 }
danielk1977075c23a2008-09-01 18:34:20 +0000242 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
shanedfb7b372008-07-22 05:13:30 +0000243
drh93ed56d2008-08-12 15:21:11 +0000244 /* Go back under the static mutex and clean up the recursive
245 ** mutex to prevent a resource leak.
246 */
247 sqlite3_mutex_enter(pMaster);
danielk1977075c23a2008-09-01 18:34:20 +0000248 sqlite3GlobalConfig.nRefInitMutex--;
249 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
250 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
251 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
252 sqlite3GlobalConfig.pInitMutex = 0;
drh93ed56d2008-08-12 15:21:11 +0000253 }
254 sqlite3_mutex_leave(pMaster);
255
256 /* The following is just a sanity check to make sure SQLite has
257 ** been compiled correctly. It is important to run this code, but
258 ** we don't want to run it too often and soak up CPU cycles for no
259 ** reason. So we run it once during initialization.
260 */
shanedfb7b372008-07-22 05:13:30 +0000261#ifndef NDEBUG
shanefbd60f82009-02-04 03:59:25 +0000262#ifndef SQLITE_OMIT_FLOATING_POINT
shanedfb7b372008-07-22 05:13:30 +0000263 /* This section of code's only "output" is via assert() statements. */
264 if ( rc==SQLITE_OK ){
265 u64 x = (((u64)1)<<63)-1;
266 double y;
267 assert(sizeof(x)==8);
268 assert(sizeof(x)==sizeof(y));
269 memcpy(&y, &x, 8);
270 assert( sqlite3IsNaN(y) );
271 }
272#endif
shanefbd60f82009-02-04 03:59:25 +0000273#endif
shanedfb7b372008-07-22 05:13:30 +0000274
drhe4cf0b32011-08-25 00:14:41 +0000275 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
276 ** compile-time option.
277 */
278#ifdef SQLITE_EXTRA_INIT
drhab80be92013-08-08 14:38:45 +0000279 if( bRunExtraInit ){
drhc7f94622011-12-13 18:22:38 +0000280 int SQLITE_EXTRA_INIT(const char*);
281 rc = SQLITE_EXTRA_INIT(0);
drhe4cf0b32011-08-25 00:14:41 +0000282 }
283#endif
284
drh40257ff2008-06-13 18:24:27 +0000285 return rc;
286}
287
288/*
289** Undo the effects of sqlite3_initialize(). Must not be called while
290** there are outstanding database connections or memory allocations or
291** while any part of SQLite is otherwise in use in any thread. This
drhd1a24402009-04-19 12:23:58 +0000292** routine is not threadsafe. But it is safe to invoke this routine
293** on when SQLite is already shut down. If SQLite is already shut down
294** when this routine is invoked, then this routine is a harmless no-op.
drh40257ff2008-06-13 18:24:27 +0000295*/
296int sqlite3_shutdown(void){
mistachkin054450f2014-12-23 20:42:48 +0000297#ifdef SQLITE_OMIT_WSD
298 int rc = sqlite3_wsd_init(4096, 24);
299 if( rc!=SQLITE_OK ){
300 return rc;
301 }
302#endif
303
danielk1977075c23a2008-09-01 18:34:20 +0000304 if( sqlite3GlobalConfig.isInit ){
drh97977062011-12-13 01:34:21 +0000305#ifdef SQLITE_EXTRA_SHUTDOWN
306 void SQLITE_EXTRA_SHUTDOWN(void);
307 SQLITE_EXTRA_SHUTDOWN();
308#endif
drha7640922009-04-21 12:02:56 +0000309 sqlite3_os_end();
drhd1a24402009-04-19 12:23:58 +0000310 sqlite3_reset_auto_extension();
drhd1a24402009-04-19 12:23:58 +0000311 sqlite3GlobalConfig.isInit = 0;
danielk1977fb437272008-07-08 12:02:35 +0000312 }
dane1ab2192009-08-17 15:16:19 +0000313 if( sqlite3GlobalConfig.isPCacheInit ){
314 sqlite3PcacheShutdown();
315 sqlite3GlobalConfig.isPCacheInit = 0;
316 }
317 if( sqlite3GlobalConfig.isMallocInit ){
318 sqlite3MallocEnd();
319 sqlite3GlobalConfig.isMallocInit = 0;
mistachkin86f89872012-03-18 01:32:44 +0000320
321#ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
322 /* The heap subsystem has now been shutdown and these values are supposed
323 ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
324 ** which would rely on that heap subsystem; therefore, make sure these
325 ** values cannot refer to heap memory that was just invalidated when the
326 ** heap subsystem was shutdown. This is only done if the current call to
327 ** this function resulted in the heap subsystem actually being shutdown.
328 */
329 sqlite3_data_directory = 0;
330 sqlite3_temp_directory = 0;
331#endif
dane1ab2192009-08-17 15:16:19 +0000332 }
333 if( sqlite3GlobalConfig.isMutexInit ){
334 sqlite3MutexEnd();
335 sqlite3GlobalConfig.isMutexInit = 0;
336 }
337
drh40257ff2008-06-13 18:24:27 +0000338 return SQLITE_OK;
339}
340
341/*
342** This API allows applications to modify the global configuration of
343** the SQLite library at run-time.
344**
345** This routine should only be called when there are no outstanding
346** database connections or memory allocations. This routine is not
347** threadsafe. Failure to heed these warnings can lead to unpredictable
348** behavior.
349*/
350int sqlite3_config(int op, ...){
351 va_list ap;
352 int rc = SQLITE_OK;
353
354 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
355 ** the SQLite library is in use. */
drh413c3d32010-02-23 20:11:56 +0000356 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
drh40257ff2008-06-13 18:24:27 +0000357
358 va_start(ap, op);
359 switch( op ){
drh18472fa2008-10-07 15:25:48 +0000360
361 /* Mutex configuration options are only available in a threadsafe
drhd1dcb232014-11-01 18:32:18 +0000362 ** compile.
drh18472fa2008-10-07 15:25:48 +0000363 */
drhd1dcb232014-11-01 18:32:18 +0000364#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
drh40257ff2008-06-13 18:24:27 +0000365 case SQLITE_CONFIG_SINGLETHREAD: {
drh682a6ef2015-03-04 23:14:14 +0000366 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
367 ** Single-thread. */
368 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */
369 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
drh40257ff2008-06-13 18:24:27 +0000370 break;
371 }
drhd1dcb232014-11-01 18:32:18 +0000372#endif
373#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
drh40257ff2008-06-13 18:24:27 +0000374 case SQLITE_CONFIG_MULTITHREAD: {
drh682a6ef2015-03-04 23:14:14 +0000375 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
376 ** Multi-thread. */
377 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
378 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */
drh40257ff2008-06-13 18:24:27 +0000379 break;
380 }
drhd1dcb232014-11-01 18:32:18 +0000381#endif
382#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
drh40257ff2008-06-13 18:24:27 +0000383 case SQLITE_CONFIG_SERIALIZED: {
drh682a6ef2015-03-04 23:14:14 +0000384 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
385 ** Serialized. */
386 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */
387 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */
drh40257ff2008-06-13 18:24:27 +0000388 break;
389 }
drhd1dcb232014-11-01 18:32:18 +0000390#endif
391#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
drh18472fa2008-10-07 15:25:48 +0000392 case SQLITE_CONFIG_MUTEX: {
393 /* Specify an alternative mutex implementation */
394 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
395 break;
396 }
drhd1dcb232014-11-01 18:32:18 +0000397#endif
398#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
drh18472fa2008-10-07 15:25:48 +0000399 case SQLITE_CONFIG_GETMUTEX: {
400 /* Retrieve the current mutex implementation */
401 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
402 break;
403 }
404#endif
405
drh40257ff2008-06-13 18:24:27 +0000406 case SQLITE_CONFIG_MALLOC: {
drh5279d342014-11-04 13:41:32 +0000407 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
408 ** single argument which is a pointer to an instance of the
409 ** sqlite3_mem_methods structure. The argument specifies alternative
410 ** low-level memory allocation routines to be used in place of the memory
411 ** allocation routines built into SQLite. */
danielk1977075c23a2008-09-01 18:34:20 +0000412 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
drh40257ff2008-06-13 18:24:27 +0000413 break;
414 }
drh33589792008-06-18 13:27:46 +0000415 case SQLITE_CONFIG_GETMALLOC: {
drh5279d342014-11-04 13:41:32 +0000416 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
417 ** single argument which is a pointer to an instance of the
418 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is
419 ** filled with the currently defined memory allocation routines. */
danielk1977075c23a2008-09-01 18:34:20 +0000420 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
421 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
drh33589792008-06-18 13:27:46 +0000422 break;
423 }
drhfec00ea2008-06-14 16:56:21 +0000424 case SQLITE_CONFIG_MEMSTATUS: {
drh5279d342014-11-04 13:41:32 +0000425 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
426 ** single argument of type int, interpreted as a boolean, which enables
427 ** or disables the collection of memory allocation statistics. */
428 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
drh40257ff2008-06-13 18:24:27 +0000429 break;
430 }
drh33589792008-06-18 13:27:46 +0000431 case SQLITE_CONFIG_SCRATCH: {
drh5279d342014-11-04 13:41:32 +0000432 /* EVIDENCE-OF: R-08404-60887 There are three arguments to
433 ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from
434 ** which the scratch allocations will be drawn, the size of each scratch
435 ** allocation (sz), and the maximum number of scratch allocations (N). */
danielk1977075c23a2008-09-01 18:34:20 +0000436 sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
437 sqlite3GlobalConfig.szScratch = va_arg(ap, int);
438 sqlite3GlobalConfig.nScratch = va_arg(ap, int);
drh33589792008-06-18 13:27:46 +0000439 break;
440 }
441 case SQLITE_CONFIG_PAGECACHE: {
drh5279d342014-11-04 13:41:32 +0000442 /* EVIDENCE-OF: R-31408-40510 There are three arguments to
443 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory, the size
444 ** of each page buffer (sz), and the number of pages (N). */
danielk1977075c23a2008-09-01 18:34:20 +0000445 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
446 sqlite3GlobalConfig.szPage = va_arg(ap, int);
447 sqlite3GlobalConfig.nPage = va_arg(ap, int);
drh33589792008-06-18 13:27:46 +0000448 break;
449 }
drhdef68892014-11-04 12:11:23 +0000450 case SQLITE_CONFIG_PCACHE_HDRSZ: {
drh5279d342014-11-04 13:41:32 +0000451 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
452 ** a single parameter which is a pointer to an integer and writes into
453 ** that integer the number of extra bytes per page required for each page
454 ** in SQLITE_CONFIG_PAGECACHE. */
drhdef68892014-11-04 12:11:23 +0000455 *va_arg(ap, int*) =
456 sqlite3HeaderSizeBtree() +
457 sqlite3HeaderSizePcache() +
458 sqlite3HeaderSizePcache1();
459 break;
460 }
danielk19775099be52008-06-27 13:27:03 +0000461
danielk1977bc2ca9e2008-11-13 14:28:28 +0000462 case SQLITE_CONFIG_PCACHE: {
dan22e21ff2011-11-08 20:08:44 +0000463 /* no-op */
464 break;
465 }
466 case SQLITE_CONFIG_GETPCACHE: {
467 /* now an error */
468 rc = SQLITE_ERROR;
danielk1977bc2ca9e2008-11-13 14:28:28 +0000469 break;
470 }
471
dan22e21ff2011-11-08 20:08:44 +0000472 case SQLITE_CONFIG_PCACHE2: {
drh5279d342014-11-04 13:41:32 +0000473 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
474 ** single argument which is a pointer to an sqlite3_pcache_methods2
475 ** object. This object specifies the interface to a custom page cache
476 ** implementation. */
dan22e21ff2011-11-08 20:08:44 +0000477 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
478 break;
479 }
480 case SQLITE_CONFIG_GETPCACHE2: {
drh5279d342014-11-04 13:41:32 +0000481 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
482 ** single argument which is a pointer to an sqlite3_pcache_methods2
483 ** object. SQLite copies of the current page cache implementation into
484 ** that object. */
dan22e21ff2011-11-08 20:08:44 +0000485 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
drhb232c232008-11-19 01:20:26 +0000486 sqlite3PCacheSetDefault();
487 }
dan22e21ff2011-11-08 20:08:44 +0000488 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
drhb232c232008-11-19 01:20:26 +0000489 break;
490 }
491
drh8790b6e2014-11-07 01:43:56 +0000492/* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
493** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or
494** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */
drh5d414832008-07-15 14:47:18 +0000495#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
drh33589792008-06-18 13:27:46 +0000496 case SQLITE_CONFIG_HEAP: {
drh5279d342014-11-04 13:41:32 +0000497 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
498 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
drh3ba689d2015-03-03 14:00:11 +0000499 ** number of bytes in the memory buffer, and the minimum allocation size.
500 */
danielk1977075c23a2008-09-01 18:34:20 +0000501 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
502 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
503 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
danielk19775099be52008-06-27 13:27:03 +0000504
shaneha6ec8922011-03-09 21:36:17 +0000505 if( sqlite3GlobalConfig.mnReq<1 ){
506 sqlite3GlobalConfig.mnReq = 1;
507 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
508 /* cap min request size at 2^12 */
509 sqlite3GlobalConfig.mnReq = (1<<12);
510 }
511
danielk1977075c23a2008-09-01 18:34:20 +0000512 if( sqlite3GlobalConfig.pHeap==0 ){
drh8790b6e2014-11-07 01:43:56 +0000513 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
514 ** is NULL, then SQLite reverts to using its default memory allocator
515 ** (the system malloc() implementation), undoing any prior invocation of
516 ** SQLITE_CONFIG_MALLOC.
517 **
518 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to
519 ** revert to its default implementation when sqlite3_initialize() is run
drh8a42cbd2008-07-10 18:13:42 +0000520 */
danielk1977075c23a2008-09-01 18:34:20 +0000521 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
drh8a42cbd2008-07-10 18:13:42 +0000522 }else{
drh8790b6e2014-11-07 01:43:56 +0000523 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
524 ** alternative memory allocator is engaged to handle all of SQLites
525 ** memory allocation needs. */
danielk19775099be52008-06-27 13:27:03 +0000526#ifdef SQLITE_ENABLE_MEMSYS3
danielk1977075c23a2008-09-01 18:34:20 +0000527 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
drh8a42cbd2008-07-10 18:13:42 +0000528#endif
529#ifdef SQLITE_ENABLE_MEMSYS5
danielk1977075c23a2008-09-01 18:34:20 +0000530 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
drh8a42cbd2008-07-10 18:13:42 +0000531#endif
drh8a42cbd2008-07-10 18:13:42 +0000532 }
danielk19775099be52008-06-27 13:27:03 +0000533 break;
534 }
drh5d414832008-07-15 14:47:18 +0000535#endif
danielk19775099be52008-06-27 13:27:03 +0000536
drh633e6d52008-07-28 19:34:53 +0000537 case SQLITE_CONFIG_LOOKASIDE: {
danielk1977075c23a2008-09-01 18:34:20 +0000538 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
539 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
drh633e6d52008-07-28 19:34:53 +0000540 break;
541 }
drh3f280702010-02-18 18:45:09 +0000542
mistachkin037933b2013-08-13 22:33:41 +0000543 /* Record a pointer to the logger function and its first argument.
drh3f280702010-02-18 18:45:09 +0000544 ** The default is NULL. Logging is disabled if the function pointer is
545 ** NULL.
546 */
547 case SQLITE_CONFIG_LOG: {
shanehdc97a8c2010-02-23 20:08:35 +0000548 /* MSVC is picky about pulling func ptrs from va lists.
549 ** http://support.microsoft.com/kb/47961
550 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
551 */
552 typedef void(*LOGFUNC_t)(void*,int,const char*);
553 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
drh3f280702010-02-18 18:45:09 +0000554 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
555 break;
556 }
drh633e6d52008-07-28 19:34:53 +0000557
drh00729cb2014-10-04 11:59:33 +0000558 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
559 ** can be changed at start-time using the
560 ** sqlite3_config(SQLITE_CONFIG_URI,1) or
561 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
562 */
dancd74b612011-04-22 19:37:32 +0000563 case SQLITE_CONFIG_URI: {
drh4d9f1882014-11-04 17:23:24 +0000564 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
565 ** argument of type int. If non-zero, then URI handling is globally
566 ** enabled. If the parameter is zero, then URI handling is globally
567 ** disabled. */
dancd74b612011-04-22 19:37:32 +0000568 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
569 break;
570 }
571
drhde9a7b82012-09-17 20:44:46 +0000572 case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
drh4d9f1882014-11-04 17:23:24 +0000573 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
574 ** option takes a single integer argument which is interpreted as a
575 ** boolean in order to enable or disable the use of covering indices for
576 ** full table scans in the query optimizer. */
drhde9a7b82012-09-17 20:44:46 +0000577 sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
578 break;
579 }
580
danac455932012-11-26 19:50:41 +0000581#ifdef SQLITE_ENABLE_SQLLOG
582 case SQLITE_CONFIG_SQLLOG: {
583 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
584 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
585 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
586 break;
587 }
588#endif
589
drh9b4c59f2013-04-15 17:03:42 +0000590 case SQLITE_CONFIG_MMAP_SIZE: {
drh4d9f1882014-11-04 17:23:24 +0000591 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
592 ** integer (sqlite3_int64) values that are the default mmap size limit
593 ** (the default setting for PRAGMA mmap_size) and the maximum allowed
594 ** mmap size limit. */
drh9b4c59f2013-04-15 17:03:42 +0000595 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
drha1f42c72013-04-01 22:38:06 +0000596 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
drh4d9f1882014-11-04 17:23:24 +0000597 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
drh8790b6e2014-11-07 01:43:56 +0000598 ** negative, then that argument is changed to its compile-time default.
599 **
600 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
601 ** silently truncated if necessary so that it does not exceed the
602 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
603 ** compile-time option.
604 */
drh3ba689d2015-03-03 14:00:11 +0000605 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
606 mxMmap = SQLITE_MAX_MMAP_SIZE;
607 }
drh9b4c59f2013-04-15 17:03:42 +0000608 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
609 if( szMmap>mxMmap) szMmap = mxMmap;
drh4d9f1882014-11-04 17:23:24 +0000610 sqlite3GlobalConfig.mxMmap = mxMmap;
drh9b4c59f2013-04-15 17:03:42 +0000611 sqlite3GlobalConfig.szMmap = szMmap;
drha1f42c72013-04-01 22:38:06 +0000612 break;
613 }
614
drh4d9f1882014-11-04 17:23:24 +0000615#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
mistachkinaf8641b2013-11-25 21:49:04 +0000616 case SQLITE_CONFIG_WIN32_HEAPSIZE: {
drh4d9f1882014-11-04 17:23:24 +0000617 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
618 ** unsigned integer value that specifies the maximum size of the created
619 ** heap. */
mistachkinac1f1042013-11-23 00:27:29 +0000620 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
621 break;
622 }
623#endif
624
drh3bd17912015-01-02 15:55:29 +0000625 case SQLITE_CONFIG_PMASZ: {
626 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int);
627 break;
628 }
629
drh40257ff2008-06-13 18:24:27 +0000630 default: {
631 rc = SQLITE_ERROR;
632 break;
633 }
634 }
635 va_end(ap);
636 return rc;
637}
638
639/*
drh633e6d52008-07-28 19:34:53 +0000640** Set up the lookaside buffers for a database connection.
641** Return SQLITE_OK on success.
642** If lookaside is already active, return SQLITE_BUSY.
drhe9d1c722008-08-04 20:13:26 +0000643**
644** The sz parameter is the number of bytes in each lookaside slot.
645** The cnt parameter is the number of slots. If pStart is NULL the
646** space for the lookaside memory is obtained from sqlite3_malloc().
647** If pStart is not NULL then it is sz*cnt bytes of memory to use for
648** the lookaside memory.
drh633e6d52008-07-28 19:34:53 +0000649*/
drhe9d1c722008-08-04 20:13:26 +0000650static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
drh7877d9a2015-07-23 17:16:27 +0000651#ifndef SQLITE_OMIT_LOOKASIDE
drh633e6d52008-07-28 19:34:53 +0000652 void *pStart;
653 if( db->lookaside.nOut ){
654 return SQLITE_BUSY;
655 }
shanef71f89e2009-01-30 06:11:54 +0000656 /* Free any existing lookaside buffer for this handle before
657 ** allocating a new one so we don't have to have space for
658 ** both at the same time.
659 */
660 if( db->lookaside.bMalloced ){
661 sqlite3_free(db->lookaside.pStart);
662 }
drh61a4bd52011-11-10 02:39:28 +0000663 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
664 ** than a pointer to be useful.
shanef71f89e2009-01-30 06:11:54 +0000665 */
drh61a4bd52011-11-10 02:39:28 +0000666 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
drh1d34fde2009-02-03 15:50:33 +0000667 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
drh633e6d52008-07-28 19:34:53 +0000668 if( cnt<0 ) cnt = 0;
shanef71f89e2009-01-30 06:11:54 +0000669 if( sz==0 || cnt==0 ){
670 sz = 0;
671 pStart = 0;
672 }else if( pBuf==0 ){
drhe9d1c722008-08-04 20:13:26 +0000673 sqlite3BeginBenignMalloc();
drh9dd55f52010-09-03 03:32:46 +0000674 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
drhe9d1c722008-08-04 20:13:26 +0000675 sqlite3EndBenignMalloc();
drh8225d662011-11-10 02:24:11 +0000676 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
drhe9d1c722008-08-04 20:13:26 +0000677 }else{
678 pStart = pBuf;
679 }
drh4cfb22f2008-08-01 18:47:01 +0000680 db->lookaside.pStart = pStart;
681 db->lookaside.pFree = 0;
drh1bd10f82008-12-10 21:19:56 +0000682 db->lookaside.sz = (u16)sz;
drh633e6d52008-07-28 19:34:53 +0000683 if( pStart ){
684 int i;
685 LookasideSlot *p;
drhde467982009-04-02 17:22:41 +0000686 assert( sz > (int)sizeof(LookasideSlot*) );
drh633e6d52008-07-28 19:34:53 +0000687 p = (LookasideSlot*)pStart;
688 for(i=cnt-1; i>=0; i--){
689 p->pNext = db->lookaside.pFree;
690 db->lookaside.pFree = p;
691 p = (LookasideSlot*)&((u8*)p)[sz];
692 }
693 db->lookaside.pEnd = p;
694 db->lookaside.bEnabled = 1;
shanef71f89e2009-01-30 06:11:54 +0000695 db->lookaside.bMalloced = pBuf==0 ?1:0;
drh4cfb22f2008-08-01 18:47:01 +0000696 }else{
drhb0e77042013-12-10 19:49:00 +0000697 db->lookaside.pStart = db;
698 db->lookaside.pEnd = db;
drh4cfb22f2008-08-01 18:47:01 +0000699 db->lookaside.bEnabled = 0;
shanef71f89e2009-01-30 06:11:54 +0000700 db->lookaside.bMalloced = 0;
drh633e6d52008-07-28 19:34:53 +0000701 }
drh7877d9a2015-07-23 17:16:27 +0000702#endif /* SQLITE_OMIT_LOOKASIDE */
drh633e6d52008-07-28 19:34:53 +0000703 return SQLITE_OK;
704}
705
706/*
drh4413d0e2008-11-04 13:46:27 +0000707** Return the mutex associated with a database connection.
708*/
709sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
drh9ca95732014-10-24 00:35:58 +0000710#ifdef SQLITE_ENABLE_API_ARMOR
711 if( !sqlite3SafetyCheckOk(db) ){
712 (void)SQLITE_MISUSE_BKPT;
713 return 0;
714 }
715#endif
drh4413d0e2008-11-04 13:46:27 +0000716 return db->mutex;
717}
718
719/*
drh09419b42011-11-16 19:29:17 +0000720** Free up as much memory as we can from the given database
721** connection.
722*/
723int sqlite3_db_release_memory(sqlite3 *db){
724 int i;
drh9ca95732014-10-24 00:35:58 +0000725
726#ifdef SQLITE_ENABLE_API_ARMOR
727 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
728#endif
dand9bb3a92011-12-30 11:43:59 +0000729 sqlite3_mutex_enter(db->mutex);
drh09419b42011-11-16 19:29:17 +0000730 sqlite3BtreeEnterAll(db);
731 for(i=0; i<db->nDb; i++){
732 Btree *pBt = db->aDb[i].pBt;
733 if( pBt ){
734 Pager *pPager = sqlite3BtreePager(pBt);
735 sqlite3PagerShrink(pPager);
736 }
737 }
738 sqlite3BtreeLeaveAll(db);
dand9bb3a92011-12-30 11:43:59 +0000739 sqlite3_mutex_leave(db->mutex);
drh09419b42011-11-16 19:29:17 +0000740 return SQLITE_OK;
741}
742
743/*
drh633e6d52008-07-28 19:34:53 +0000744** Configuration settings for an individual database connection
745*/
746int sqlite3_db_config(sqlite3 *db, int op, ...){
747 va_list ap;
drh6480aad2008-08-01 16:31:14 +0000748 int rc;
drh633e6d52008-07-28 19:34:53 +0000749 va_start(ap, op);
750 switch( op ){
drhe9d1c722008-08-04 20:13:26 +0000751 case SQLITE_DBCONFIG_LOOKASIDE: {
drh8b2b2e62011-04-07 01:14:12 +0000752 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
drh9dd55f52010-09-03 03:32:46 +0000753 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
754 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
drhe9d1c722008-08-04 20:13:26 +0000755 rc = setupLookaside(db, pBuf, sz, cnt);
drh633e6d52008-07-28 19:34:53 +0000756 break;
757 }
drh6480aad2008-08-01 16:31:14 +0000758 default: {
drhe83cafd2011-03-21 17:15:58 +0000759 static const struct {
760 int op; /* The opcode */
761 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */
762 } aFlagOp[] = {
763 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys },
764 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger },
765 };
drh58ad5802011-03-23 22:02:23 +0000766 unsigned int i;
drh9dd55f52010-09-03 03:32:46 +0000767 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
drhe83cafd2011-03-21 17:15:58 +0000768 for(i=0; i<ArraySize(aFlagOp); i++){
769 if( aFlagOp[i].op==op ){
770 int onoff = va_arg(ap, int);
771 int *pRes = va_arg(ap, int*);
drhd7b302b2011-03-23 22:54:59 +0000772 int oldFlags = db->flags;
drhe83cafd2011-03-21 17:15:58 +0000773 if( onoff>0 ){
774 db->flags |= aFlagOp[i].mask;
775 }else if( onoff==0 ){
776 db->flags &= ~aFlagOp[i].mask;
777 }
778 if( oldFlags!=db->flags ){
779 sqlite3ExpirePreparedStatements(db);
780 }
781 if( pRes ){
782 *pRes = (db->flags & aFlagOp[i].mask)!=0;
783 }
784 rc = SQLITE_OK;
785 break;
786 }
787 }
drh6480aad2008-08-01 16:31:14 +0000788 break;
789 }
drh633e6d52008-07-28 19:34:53 +0000790 }
791 va_end(ap);
792 return rc;
793}
794
drha16313e2007-03-30 11:29:32 +0000795
796/*
drh9b5adfa2008-01-20 23:19:56 +0000797** Return true if the buffer z[0..n-1] contains all spaces.
798*/
799static int allSpaces(const char *z, int n){
drh5f3a3672008-04-15 04:02:40 +0000800 while( n>0 && z[n-1]==' ' ){ n--; }
drh9b5adfa2008-01-20 23:19:56 +0000801 return n==0;
802}
803
804/*
drhd3d39e92004-05-20 22:16:29 +0000805** This is the default collating function named "BINARY" which is always
806** available.
drh9b5adfa2008-01-20 23:19:56 +0000807**
808** If the padFlag argument is not NULL then space padding at the end
809** of strings is ignored. This implements the RTRIM collation.
drhd3d39e92004-05-20 22:16:29 +0000810*/
danielk19772c336542005-01-13 02:14:23 +0000811static int binCollFunc(
drh9b5adfa2008-01-20 23:19:56 +0000812 void *padFlag,
drhd3d39e92004-05-20 22:16:29 +0000813 int nKey1, const void *pKey1,
814 int nKey2, const void *pKey2
815){
816 int rc, n;
817 n = nKey1<nKey2 ? nKey1 : nKey2;
drh5e3b49b2014-11-20 19:22:26 +0000818 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
819 ** strings byte by byte using the memcmp() function from the standard C
820 ** library. */
drhd3d39e92004-05-20 22:16:29 +0000821 rc = memcmp(pKey1, pKey2, n);
822 if( rc==0 ){
drh9b5adfa2008-01-20 23:19:56 +0000823 if( padFlag
824 && allSpaces(((char*)pKey1)+n, nKey1-n)
825 && allSpaces(((char*)pKey2)+n, nKey2-n)
826 ){
drh5e3b49b2014-11-20 19:22:26 +0000827 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra
828 ** spaces at the end of either string do not change the result. In other
829 ** words, strings will compare equal to one another as long as they
830 ** differ only in the number of spaces at the end.
831 */
drh9b5adfa2008-01-20 23:19:56 +0000832 }else{
833 rc = nKey1 - nKey2;
834 }
drhd3d39e92004-05-20 22:16:29 +0000835 }
836 return rc;
837}
838
839/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000840** Another built-in collating sequence: NOCASE.
841**
drhf7b54962013-05-28 12:11:54 +0000842** This collating sequence is intended to be used for "case independent
danielk1977dc1bdc42004-06-11 10:51:27 +0000843** comparison". SQLite's knowledge of upper and lower case equivalents
844** extends only to the 26 characters used in the English language.
845**
846** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000847*/
848static int nocaseCollatingFunc(
849 void *NotUsed,
850 int nKey1, const void *pKey1,
851 int nKey2, const void *pKey2
852){
853 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +0000854 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk197762c14b32008-11-19 09:05:26 +0000855 UNUSED_PARAMETER(NotUsed);
danielk19770202b292004-06-09 09:55:16 +0000856 if( 0==r ){
857 r = nKey1-nKey2;
858 }
859 return r;
860}
861
862/*
drhaf9ff332002-01-16 21:00:27 +0000863** Return the ROWID of the most recent insert
864*/
drh9bb575f2004-09-06 17:24:11 +0000865sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drh9ca95732014-10-24 00:35:58 +0000866#ifdef SQLITE_ENABLE_API_ARMOR
867 if( !sqlite3SafetyCheckOk(db) ){
868 (void)SQLITE_MISUSE_BKPT;
869 return 0;
870 }
871#endif
drhaf9ff332002-01-16 21:00:27 +0000872 return db->lastRowid;
873}
874
875/*
danielk197724b03fd2004-05-10 10:34:34 +0000876** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000877*/
drh9bb575f2004-09-06 17:24:11 +0000878int sqlite3_changes(sqlite3 *db){
drh9ca95732014-10-24 00:35:58 +0000879#ifdef SQLITE_ENABLE_API_ARMOR
880 if( !sqlite3SafetyCheckOk(db) ){
881 (void)SQLITE_MISUSE_BKPT;
882 return 0;
883 }
884#endif
drhc8d30ac2002-04-12 10:08:59 +0000885 return db->nChange;
886}
887
rdcf146a772004-02-25 22:51:06 +0000888/*
danielk1977b28af712004-06-21 06:50:26 +0000889** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000890*/
danielk1977b28af712004-06-21 06:50:26 +0000891int sqlite3_total_changes(sqlite3 *db){
drh9ca95732014-10-24 00:35:58 +0000892#ifdef SQLITE_ENABLE_API_ARMOR
893 if( !sqlite3SafetyCheckOk(db) ){
894 (void)SQLITE_MISUSE_BKPT;
895 return 0;
896 }
897#endif
danielk1977b28af712004-06-21 06:50:26 +0000898 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000899}
900
drhc8d30ac2002-04-12 10:08:59 +0000901/*
danielk1977fd7f0452008-12-17 17:30:26 +0000902** Close all open savepoints. This function only manipulates fields of the
903** database handle object, it does not close any savepoints that may be open
904** at the b-tree/pager level.
905*/
906void sqlite3CloseSavepoints(sqlite3 *db){
907 while( db->pSavepoint ){
908 Savepoint *pTmp = db->pSavepoint;
909 db->pSavepoint = pTmp->pNext;
910 sqlite3DbFree(db, pTmp);
911 }
912 db->nSavepoint = 0;
danielk1977bd434552009-03-18 10:33:00 +0000913 db->nStatement = 0;
danielk1977fd7f0452008-12-17 17:30:26 +0000914 db->isTransactionSavepoint = 0;
915}
916
917/*
dand2199f02010-08-27 17:48:52 +0000918** Invoke the destructor function associated with FuncDef p, if any. Except,
919** if this is not the last copy of the function, do not invoke it. Multiple
920** copies of a single function are created when create_function() is called
921** with SQLITE_ANY as the encoding.
922*/
923static void functionDestroy(sqlite3 *db, FuncDef *p){
924 FuncDestructor *pDestructor = p->pDestructor;
925 if( pDestructor ){
926 pDestructor->nRef--;
927 if( pDestructor->nRef==0 ){
928 pDestructor->xDestroy(pDestructor->pUserData);
929 sqlite3DbFree(db, pDestructor);
930 }
931 }
932}
933
934/*
danbba02a92012-05-15 17:15:34 +0000935** Disconnect all sqlite3_vtab objects that belong to database connection
936** db. This is called when db is being closed.
937*/
938static void disconnectAllVtab(sqlite3 *db){
939#ifndef SQLITE_OMIT_VIRTUALTABLE
940 int i;
drh51be3872015-08-19 02:32:25 +0000941 HashElem *p;
danbba02a92012-05-15 17:15:34 +0000942 sqlite3BtreeEnterAll(db);
943 for(i=0; i<db->nDb; i++){
944 Schema *pSchema = db->aDb[i].pSchema;
945 if( db->aDb[i].pSchema ){
danbba02a92012-05-15 17:15:34 +0000946 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
947 Table *pTab = (Table *)sqliteHashData(p);
948 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
949 }
950 }
951 }
drh51be3872015-08-19 02:32:25 +0000952 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
953 Module *pMod = (Module *)sqliteHashData(p);
954 if( pMod->pEpoTab ){
955 sqlite3VtabDisconnect(db, pMod->pEpoTab);
956 }
957 }
dand88e5212014-03-12 19:38:38 +0000958 sqlite3VtabUnlockList(db);
danbba02a92012-05-15 17:15:34 +0000959 sqlite3BtreeLeaveAll(db);
960#else
961 UNUSED_PARAMETER(db);
962#endif
963}
964
965/*
drh167cd6a2012-06-02 17:09:46 +0000966** Return TRUE if database connection db has unfinalized prepared
967** statements or unfinished sqlite3_backup objects.
968*/
969static int connectionIsBusy(sqlite3 *db){
970 int j;
971 assert( sqlite3_mutex_held(db->mutex) );
972 if( db->pVdbe ) return 1;
973 for(j=0; j<db->nDb; j++){
974 Btree *pBt = db->aDb[j].pBt;
975 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
976 }
977 return 0;
978}
979
980/*
drh50e5dad2001-09-15 00:57:28 +0000981** Close an existing SQLite database
982*/
drh167cd6a2012-06-02 17:09:46 +0000983static int sqlite3Close(sqlite3 *db, int forceZombie){
danielk19775c4c7782004-06-16 10:39:23 +0000984 if( !db ){
drhddb17ca2014-08-11 15:54:11 +0000985 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
986 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
danielk197796d81f92004-06-19 03:33:57 +0000987 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000988 }
drh7e8b8482008-01-23 03:03:05 +0000989 if( !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:56 +0000990 return SQLITE_MISUSE_BKPT;
danielk1977e35ee192004-06-26 09:50:11 +0000991 }
drh605264d2007-08-21 15:13:19 +0000992 sqlite3_mutex_enter(db->mutex);
danielk1977e35ee192004-06-26 09:50:11 +0000993
danbba02a92012-05-15 17:15:34 +0000994 /* Force xDisconnect calls on all virtual tables */
995 disconnectAllVtab(db);
danielk1977a04a34f2007-04-16 15:06:25 +0000996
danbba02a92012-05-15 17:15:34 +0000997 /* If a transaction is open, the disconnectAllVtab() call above
danielk197701256832007-04-18 14:24:32 +0000998 ** will not have called the xDisconnect() method on any virtual
999 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
1000 ** call will do so. We need to do this before the check for active
1001 ** SQL statements below, as the v-table implementation may be storing
1002 ** some prepared statements internally.
1003 */
dan67430812013-05-15 10:21:50 +00001004 sqlite3VtabRollback(db);
danielk197701256832007-04-18 14:24:32 +00001005
drh167cd6a2012-06-02 17:09:46 +00001006 /* Legacy behavior (sqlite3_close() behavior) is to return
1007 ** SQLITE_BUSY if the connection can not be closed immediately.
1008 */
1009 if( !forceZombie && connectionIsBusy(db) ){
drh13f40da2014-08-22 18:00:11 +00001010 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized "
drh167cd6a2012-06-02 17:09:46 +00001011 "statements or unfinished backups");
drh27641702007-08-22 02:56:42 +00001012 sqlite3_mutex_leave(db->mutex);
danielk197796d81f92004-06-19 03:33:57 +00001013 return SQLITE_BUSY;
1014 }
danielk1977e0048402004-06-15 16:51:01 +00001015
danac455932012-11-26 19:50:41 +00001016#ifdef SQLITE_ENABLE_SQLLOG
1017 if( sqlite3GlobalConfig.xSqllog ){
dan71ba10d2012-11-27 10:56:39 +00001018 /* Closing the handle. Fourth parameter is passed the value 2. */
1019 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
danac455932012-11-26 19:50:41 +00001020 }
1021#endif
1022
drh167cd6a2012-06-02 17:09:46 +00001023 /* Convert the connection into a zombie and then close it.
drh4245c402012-06-02 14:32:21 +00001024 */
1025 db->magic = SQLITE_MAGIC_ZOMBIE;
1026 sqlite3LeaveMutexAndCloseZombie(db);
1027 return SQLITE_OK;
1028}
drh94e92032003-02-16 22:21:32 +00001029
drh167cd6a2012-06-02 17:09:46 +00001030/*
1031** Two variations on the public interface for closing a database
1032** connection. The sqlite3_close() version returns SQLITE_BUSY and
1033** leaves the connection option if there are unfinalized prepared
1034** statements or unfinished sqlite3_backups. The sqlite3_close_v2()
1035** version forces the connection to become a zombie if there are
1036** unclosed resources, and arranges for deallocation when the last
1037** prepare statement or sqlite3_backup closes.
1038*/
1039int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
1040int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
1041
drh4245c402012-06-02 14:32:21 +00001042
1043/*
1044** Close the mutex on database connection db.
1045**
1046** Furthermore, if database connection db is a zombie (meaning that there
drh167cd6a2012-06-02 17:09:46 +00001047** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
1048** every sqlite3_stmt has now been finalized and every sqlite3_backup has
1049** finished, then free all resources.
drh4245c402012-06-02 14:32:21 +00001050*/
1051void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
1052 HashElem *i; /* Hash table iterator */
1053 int j;
1054
drh4245c402012-06-02 14:32:21 +00001055 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
drh167cd6a2012-06-02 17:09:46 +00001056 ** or if the connection has not yet been closed by sqlite3_close_v2(),
1057 ** then just leave the mutex and return.
drh4245c402012-06-02 14:32:21 +00001058 */
drh167cd6a2012-06-02 17:09:46 +00001059 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
drh4245c402012-06-02 14:32:21 +00001060 sqlite3_mutex_leave(db->mutex);
1061 return;
danielk197704103022009-02-03 16:51:24 +00001062 }
1063
drh4245c402012-06-02 14:32:21 +00001064 /* If we reach this point, it means that the database connection has
1065 ** closed all sqlite3_stmt and sqlite3_backup objects and has been
mistachkin48864df2013-03-21 21:20:32 +00001066 ** passed to sqlite3_close (meaning that it is a zombie). Therefore,
drh4245c402012-06-02 14:32:21 +00001067 ** go ahead and free all resources.
1068 */
1069
dan617dc862013-05-16 11:57:28 +00001070 /* If a transaction is open, roll it back. This also ensures that if
1071 ** any database schemas have been modified by an uncommitted transaction
1072 ** they are reset. And that the required b-tree mutex is held to make
1073 ** the pager rollback and schema reset an atomic operation. */
1074 sqlite3RollbackAll(db, SQLITE_OK);
1075
danielk1977fd7f0452008-12-17 17:30:26 +00001076 /* Free any outstanding Savepoint structures. */
1077 sqlite3CloseSavepoints(db);
1078
drh5efb3142012-05-16 01:24:34 +00001079 /* Close all database connections */
drh001bbcb2003-03-19 03:14:00 +00001080 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +00001081 struct Db *pDb = &db->aDb[j];
1082 if( pDb->pBt ){
drhebdb81d2014-12-05 15:31:33 +00001083 sqlite3BtreeClose(pDb->pBt);
1084 pDb->pBt = 0;
1085 if( j!=1 ){
danielk1977311019b2006-01-10 07:14:23 +00001086 pDb->pSchema = 0;
1087 }
drh113088e2003-03-20 01:16:58 +00001088 }
drhf57b3392001-10-08 13:22:32 +00001089 }
drh5efb3142012-05-16 01:24:34 +00001090 /* Clear the TEMP schema separately and last */
1091 if( db->aDb[1].pSchema ){
1092 sqlite3SchemaClear(db->aDb[1].pSchema);
1093 }
1094 sqlite3VtabUnlockList(db);
danbba02a92012-05-15 17:15:34 +00001095
drh5efb3142012-05-16 01:24:34 +00001096 /* Free up the array of auxiliary databases */
1097 sqlite3CollapseDatabaseArray(db);
danbba02a92012-05-15 17:15:34 +00001098 assert( db->nDb<=2 );
1099 assert( db->aDb==db->aDbStatic );
danielk1977404ca072009-03-16 13:19:36 +00001100
1101 /* Tell the code in notify.c that the connection no longer holds any
1102 ** locks and does not require any further unlock-notify callbacks.
1103 */
1104 sqlite3ConnectionClosed(db);
1105
drh70a8ca32008-08-21 18:49:27 +00001106 for(j=0; j<ArraySize(db->aFunc.a); j++){
1107 FuncDef *pNext, *pHash, *p;
1108 for(p=db->aFunc.a[j]; p; p=pHash){
1109 pHash = p->pHash;
1110 while( p ){
dand2199f02010-08-27 17:48:52 +00001111 functionDestroy(db, p);
drh70a8ca32008-08-21 18:49:27 +00001112 pNext = p->pNext;
1113 sqlite3DbFree(db, p);
1114 p = pNext;
1115 }
drh8e0a2f92002-02-23 23:45:45 +00001116 }
1117 }
danielk1977d8123362004-06-12 09:25:12 +00001118 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +00001119 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977a9808b32007-05-07 09:32:45 +00001120 /* Invoke any destructors registered for collation sequence user data. */
1121 for(j=0; j<3; j++){
1122 if( pColl[j].xDel ){
1123 pColl[j].xDel(pColl[j].pUser);
1124 }
1125 }
drh633e6d52008-07-28 19:34:53 +00001126 sqlite3DbFree(db, pColl);
danielk1977466be562004-06-10 02:16:01 +00001127 }
danielk1977d8123362004-06-12 09:25:12 +00001128 sqlite3HashClear(&db->aCollSeq);
drhb9bb7c12006-06-11 23:41:55 +00001129#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +00001130 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
1131 Module *pMod = (Module *)sqliteHashData(i);
danielk1977832a58a2007-06-22 15:21:15 +00001132 if( pMod->xDestroy ){
1133 pMod->xDestroy(pMod->pAux);
1134 }
drh51be3872015-08-19 02:32:25 +00001135 sqlite3VtabEponymousTableClear(db, pMod);
drh633e6d52008-07-28 19:34:53 +00001136 sqlite3DbFree(db, pMod);
danielk1977d1ab1ba2006-06-15 04:28:13 +00001137 }
drhb9bb7c12006-06-11 23:41:55 +00001138 sqlite3HashClear(&db->aModule);
1139#endif
danielk1977466be562004-06-10 02:16:01 +00001140
drh13f40da2014-08-22 18:00:11 +00001141 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
drha3cc0072013-12-13 16:23:55 +00001142 sqlite3ValueFree(db->pErr);
drhf1952c52006-06-08 15:48:00 +00001143 sqlite3CloseExtensions(db);
drhd4530972014-09-09 14:47:53 +00001144#if SQLITE_USER_AUTHENTICATION
1145 sqlite3_free(db->auth.zAuthUser);
1146 sqlite3_free(db->auth.zAuthPW);
1147#endif
danielk197796d81f92004-06-19 03:33:57 +00001148
1149 db->magic = SQLITE_MAGIC_ERROR;
danielk1977311019b2006-01-10 07:14:23 +00001150
1151 /* The temp-database schema is allocated differently from the other schema
1152 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
1153 ** So it needs to be freed here. Todo: Why not roll the temp schema into
1154 ** the same sqliteMalloc() as the one that allocates the database
1155 ** structure?
1156 */
drh633e6d52008-07-28 19:34:53 +00001157 sqlite3DbFree(db, db->aDb[1].pSchema);
drh605264d2007-08-21 15:13:19 +00001158 sqlite3_mutex_leave(db->mutex);
drh7e8b8482008-01-23 03:03:05 +00001159 db->magic = SQLITE_MAGIC_CLOSED;
drh605264d2007-08-21 15:13:19 +00001160 sqlite3_mutex_free(db->mutex);
drh4150ebf2008-10-11 15:38:29 +00001161 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
drhe9d1c722008-08-04 20:13:26 +00001162 if( db->lookaside.bMalloced ){
1163 sqlite3_free(db->lookaside.pStart);
1164 }
drh17435752007-08-16 04:30:38 +00001165 sqlite3_free(db);
drh75897232000-05-29 14:26:00 +00001166}
1167
1168/*
drh0f198a72012-02-13 16:43:16 +00001169** Rollback all database files. If tripCode is not SQLITE_OK, then
drh47b7fc72014-11-11 01:33:57 +00001170** any write cursors are invalidated ("tripped" - as in "tripping a circuit
drh0f198a72012-02-13 16:43:16 +00001171** breaker") and made to return tripCode if there are any further
drh47b7fc72014-11-11 01:33:57 +00001172** attempts to use that cursor. Read cursors remain open and valid
1173** but are "saved" in case the table pages are moved around.
drh001bbcb2003-03-19 03:14:00 +00001174*/
drh0f198a72012-02-13 16:43:16 +00001175void sqlite3RollbackAll(sqlite3 *db, int tripCode){
drh001bbcb2003-03-19 03:14:00 +00001176 int i;
danielk1977f3f06bb2005-12-16 15:24:28 +00001177 int inTrans = 0;
drh47b7fc72014-11-11 01:33:57 +00001178 int schemaChange;
drhe30f4422007-08-21 16:15:55 +00001179 assert( sqlite3_mutex_held(db->mutex) );
danielk19772d1d86f2008-06-20 14:59:51 +00001180 sqlite3BeginBenignMalloc();
dan617dc862013-05-16 11:57:28 +00001181
1182 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
1183 ** This is important in case the transaction being rolled back has
1184 ** modified the database schema. If the b-tree mutexes are not taken
1185 ** here, then another shared-cache connection might sneak in between
1186 ** the database rollback and schema reset, which can cause false
1187 ** corruption reports in some cases. */
dancd7b91a2013-05-13 18:23:15 +00001188 sqlite3BtreeEnterAll(db);
drh47b7fc72014-11-11 01:33:57 +00001189 schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0;
dan617dc862013-05-16 11:57:28 +00001190
drh001bbcb2003-03-19 03:14:00 +00001191 for(i=0; i<db->nDb; i++){
drh0f198a72012-02-13 16:43:16 +00001192 Btree *p = db->aDb[i].pBt;
1193 if( p ){
1194 if( sqlite3BtreeIsInTrans(p) ){
danielk1977f3f06bb2005-12-16 15:24:28 +00001195 inTrans = 1;
1196 }
drh47b7fc72014-11-11 01:33:57 +00001197 sqlite3BtreeRollback(p, tripCode, !schemaChange);
drh001bbcb2003-03-19 03:14:00 +00001198 }
1199 }
danielk1977a298e902006-06-22 09:53:48 +00001200 sqlite3VtabRollback(db);
danielk19772d1d86f2008-06-20 14:59:51 +00001201 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001202
drh5c5760a2013-02-19 18:34:45 +00001203 if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
danielk1977ea4d9e22007-08-13 15:28:33 +00001204 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00001205 sqlite3ResetAllSchemasOfConnection(db);
danielk197771fd80b2005-12-16 06:54:01 +00001206 }
dancd7b91a2013-05-13 18:23:15 +00001207 sqlite3BtreeLeaveAll(db);
danielk197771fd80b2005-12-16 06:54:01 +00001208
dan1da40a32009-09-19 17:00:31 +00001209 /* Any deferred constraint violations have now been resolved. */
1210 db->nDeferredCons = 0;
drh648e2642013-07-11 15:03:32 +00001211 db->nDeferredImmCons = 0;
1212 db->flags &= ~SQLITE_DeferFKs;
dan1da40a32009-09-19 17:00:31 +00001213
danielk197771fd80b2005-12-16 06:54:01 +00001214 /* If one has been configured, invoke the rollback-hook callback */
danielk1977f3f06bb2005-12-16 15:24:28 +00001215 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
danielk197771fd80b2005-12-16 06:54:01 +00001216 db->xRollbackCallback(db->pRollbackArg);
1217 }
drh001bbcb2003-03-19 03:14:00 +00001218}
1219
1220/*
mistachkinf2c1c992013-04-28 01:44:43 +00001221** Return a static string containing the name corresponding to the error code
1222** specified in the argument.
1223*/
mistachkin5824d442015-04-28 23:34:10 +00001224#if defined(SQLITE_NEED_ERR_NAME)
mistachkinf2c1c992013-04-28 01:44:43 +00001225const char *sqlite3ErrName(int rc){
1226 const char *zName = 0;
mistachkin10269dc2013-04-30 07:54:42 +00001227 int i, origRc = rc;
mistachkinf2c1c992013-04-28 01:44:43 +00001228 for(i=0; i<2 && zName==0; i++, rc &= 0xff){
1229 switch( rc ){
1230 case SQLITE_OK: zName = "SQLITE_OK"; break;
1231 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break;
1232 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break;
1233 case SQLITE_PERM: zName = "SQLITE_PERM"; break;
1234 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001235 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001236 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001237 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break;
danf73819a2013-06-27 11:46:27 +00001238 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001239 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break;
1240 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
1241 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break;
1242 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001243 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break;
1244 case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break;
1245 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break;
mistachkin091a81b2013-12-06 19:58:32 +00001246 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001247 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break;
1248 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001249 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break;
1250 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break;
1251 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break;
1252 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break;
1253 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break;
1254 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break;
1255 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break;
1256 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break;
1257 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break;
1258 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001259 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break;
1260 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break;
1261 case SQLITE_IOERR_CHECKRESERVEDLOCK:
1262 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
1263 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break;
1264 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break;
1265 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break;
1266 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break;
1267 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break;
1268 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break;
1269 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
1270 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
1271 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
1272 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
mistachkin16a2e7a2013-07-31 22:27:16 +00001273 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
mistachkind95a3d32013-08-30 21:52:38 +00001274 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001275 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001276 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001277 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
1278 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
1279 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001280 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
1281 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
1282 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
mistachkind95a3d32013-08-30 21:52:38 +00001283 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001284 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
1285 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
1286 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
1287 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
1288 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
1289 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
1290 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
1291 case SQLITE_CONSTRAINT_FOREIGNKEY:
1292 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break;
1293 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break;
1294 case SQLITE_CONSTRAINT_PRIMARYKEY:
1295 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break;
1296 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
1297 case SQLITE_CONSTRAINT_COMMITHOOK:
1298 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break;
1299 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break;
1300 case SQLITE_CONSTRAINT_FUNCTION:
1301 zName = "SQLITE_CONSTRAINT_FUNCTION"; break;
drhf9c8ce32013-11-05 13:33:55 +00001302 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001303 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break;
1304 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break;
1305 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break;
1306 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break;
1307 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break;
1308 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break;
1309 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
1310 case SQLITE_ROW: zName = "SQLITE_ROW"; break;
1311 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break;
mistachkine84d8d32013-04-29 03:09:10 +00001312 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
1313 case SQLITE_NOTICE_RECOVER_ROLLBACK:
1314 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001315 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break;
drh8d56e202013-06-28 23:55:45 +00001316 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001317 case SQLITE_DONE: zName = "SQLITE_DONE"; break;
mistachkinf2c1c992013-04-28 01:44:43 +00001318 }
1319 }
mistachkin10269dc2013-04-30 07:54:42 +00001320 if( zName==0 ){
1321 static char zBuf[50];
1322 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
1323 zName = zBuf;
1324 }
mistachkinf2c1c992013-04-28 01:44:43 +00001325 return zName;
1326}
mistachkin10269dc2013-04-30 07:54:42 +00001327#endif
mistachkinf2c1c992013-04-28 01:44:43 +00001328
1329/*
drhc22bd472002-05-10 13:14:07 +00001330** Return a static string that describes the kind of error specified in the
1331** argument.
drh247be432002-05-10 05:44:55 +00001332*/
danielk1977f20b21c2004-05-31 23:56:42 +00001333const char *sqlite3ErrStr(int rc){
drh51c7d862009-04-27 18:46:06 +00001334 static const char* const aMsg[] = {
1335 /* SQLITE_OK */ "not an error",
1336 /* SQLITE_ERROR */ "SQL logic error or missing database",
1337 /* SQLITE_INTERNAL */ 0,
1338 /* SQLITE_PERM */ "access permission denied",
1339 /* SQLITE_ABORT */ "callback requested query abort",
1340 /* SQLITE_BUSY */ "database is locked",
1341 /* SQLITE_LOCKED */ "database table is locked",
1342 /* SQLITE_NOMEM */ "out of memory",
1343 /* SQLITE_READONLY */ "attempt to write a readonly database",
1344 /* SQLITE_INTERRUPT */ "interrupted",
1345 /* SQLITE_IOERR */ "disk I/O error",
1346 /* SQLITE_CORRUPT */ "database disk image is malformed",
drh0b52b7d2011-01-26 19:46:22 +00001347 /* SQLITE_NOTFOUND */ "unknown operation",
drh51c7d862009-04-27 18:46:06 +00001348 /* SQLITE_FULL */ "database or disk is full",
1349 /* SQLITE_CANTOPEN */ "unable to open database file",
dan0dc7b742010-06-04 15:59:58 +00001350 /* SQLITE_PROTOCOL */ "locking protocol",
drh51c7d862009-04-27 18:46:06 +00001351 /* SQLITE_EMPTY */ "table contains no data",
1352 /* SQLITE_SCHEMA */ "database schema has changed",
drh4c8555f2009-06-25 01:47:11 +00001353 /* SQLITE_TOOBIG */ "string or blob too big",
drh51c7d862009-04-27 18:46:06 +00001354 /* SQLITE_CONSTRAINT */ "constraint failed",
1355 /* SQLITE_MISMATCH */ "datatype mismatch",
1356 /* SQLITE_MISUSE */ "library routine called out of sequence",
1357 /* SQLITE_NOLFS */ "large file support is disabled",
1358 /* SQLITE_AUTH */ "authorization denied",
1359 /* SQLITE_FORMAT */ "auxiliary database format error",
1360 /* SQLITE_RANGE */ "bind or column index out of range",
1361 /* SQLITE_NOTADB */ "file is encrypted or is not a database",
1362 };
drh21021a52012-02-13 17:01:51 +00001363 const char *zErr = "unknown error";
1364 switch( rc ){
1365 case SQLITE_ABORT_ROLLBACK: {
1366 zErr = "abort due to ROLLBACK";
1367 break;
1368 }
1369 default: {
1370 rc &= 0xff;
1371 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
1372 zErr = aMsg[rc];
1373 }
1374 break;
1375 }
drh247be432002-05-10 05:44:55 +00001376 }
drh21021a52012-02-13 17:01:51 +00001377 return zErr;
drh247be432002-05-10 05:44:55 +00001378}
1379
1380/*
drh2dfbbca2000-07-28 14:32:48 +00001381** This routine implements a busy callback that sleeps and tries
1382** again until a timeout value is reached. The timeout value is
1383** an integer number of milliseconds passed in as the first
1384** argument.
1385*/
drhdaffd0e2001-04-11 14:28:42 +00001386static int sqliteDefaultBusyCallback(
drh97903fe2005-05-24 20:19:57 +00001387 void *ptr, /* Database connection */
drh2dfbbca2000-07-28 14:32:48 +00001388 int count /* Number of times table has been busy */
1389){
drh0ede9eb2015-01-10 16:49:23 +00001390#if SQLITE_OS_WIN || HAVE_USLEEP
drhee570fa2005-04-28 12:06:05 +00001391 static const u8 delays[] =
1392 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
1393 static const u8 totals[] =
1394 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhfcd71b62011-04-05 22:08:24 +00001395# define NDELAY ArraySize(delays)
danielk1977b4b47412007-08-17 15:53:36 +00001396 sqlite3 *db = (sqlite3 *)ptr;
1397 int timeout = db->busyTimeout;
drh97903fe2005-05-24 20:19:57 +00001398 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +00001399
drhee570fa2005-04-28 12:06:05 +00001400 assert( count>=0 );
1401 if( count < NDELAY ){
1402 delay = delays[count];
1403 prior = totals[count];
drhd1bec472004-01-15 13:29:31 +00001404 }else{
1405 delay = delays[NDELAY-1];
drh68cb6192005-05-06 22:05:56 +00001406 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
drh2dfbbca2000-07-28 14:32:48 +00001407 }
drhd1bec472004-01-15 13:29:31 +00001408 if( prior + delay > timeout ){
1409 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +00001410 if( delay<=0 ) return 0;
1411 }
danielk19772a6bdf62007-08-20 16:07:00 +00001412 sqlite3OsSleep(db->pVfs, delay*1000);
drh2dfbbca2000-07-28 14:32:48 +00001413 return 1;
1414#else
drh482ea182007-08-20 11:12:40 +00001415 sqlite3 *db = (sqlite3 *)ptr;
drh3f737082005-06-14 02:24:31 +00001416 int timeout = ((sqlite3 *)ptr)->busyTimeout;
drh2dfbbca2000-07-28 14:32:48 +00001417 if( (count+1)*1000 > timeout ){
1418 return 0;
1419 }
drh482ea182007-08-20 11:12:40 +00001420 sqlite3OsSleep(db->pVfs, 1000000);
drh2dfbbca2000-07-28 14:32:48 +00001421 return 1;
1422#endif
1423}
1424
1425/*
drha4afb652005-07-09 02:16:02 +00001426** Invoke the given busy handler.
1427**
1428** This routine is called when an operation failed with a lock.
1429** If this routine returns non-zero, the lock is retried. If it
1430** returns 0, the operation aborts with an SQLITE_BUSY error.
1431*/
1432int sqlite3InvokeBusyHandler(BusyHandler *p){
1433 int rc;
drh34004ce2008-07-11 16:15:17 +00001434 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
drha4afb652005-07-09 02:16:02 +00001435 rc = p->xFunc(p->pArg, p->nBusy);
1436 if( rc==0 ){
1437 p->nBusy = -1;
1438 }else{
1439 p->nBusy++;
1440 }
1441 return rc;
1442}
1443
1444/*
drh2dfbbca2000-07-28 14:32:48 +00001445** This routine sets the busy callback for an Sqlite database to the
1446** given callback function with the given argument.
1447*/
danielk1977f9d64d22004-06-19 08:18:07 +00001448int sqlite3_busy_handler(
1449 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +00001450 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +00001451 void *pArg
1452){
drh9ca95732014-10-24 00:35:58 +00001453#ifdef SQLITE_ENABLE_API_ARMOR
drh96c707a2015-02-13 16:36:14 +00001454 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
drh9ca95732014-10-24 00:35:58 +00001455#endif
drhe30f4422007-08-21 16:15:55 +00001456 sqlite3_mutex_enter(db->mutex);
danielk197724162fe2004-06-04 06:22:00 +00001457 db->busyHandler.xFunc = xBusy;
1458 db->busyHandler.pArg = pArg;
drha4afb652005-07-09 02:16:02 +00001459 db->busyHandler.nBusy = 0;
drhc0c7b5e2012-09-07 18:49:57 +00001460 db->busyTimeout = 0;
drhe30f4422007-08-21 16:15:55 +00001461 sqlite3_mutex_leave(db->mutex);
danielk1977f9d64d22004-06-19 08:18:07 +00001462 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +00001463}
1464
danielk1977348bb5d2003-10-18 09:37:26 +00001465#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1466/*
1467** This routine sets the progress callback for an Sqlite database to the
1468** given callback function with the given argument. The progress callback will
1469** be invoked every nOps opcodes.
1470*/
danielk197724b03fd2004-05-10 10:34:34 +00001471void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +00001472 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +00001473 int nOps,
1474 int (*xProgress)(void*),
1475 void *pArg
1476){
drh9ca95732014-10-24 00:35:58 +00001477#ifdef SQLITE_ENABLE_API_ARMOR
1478 if( !sqlite3SafetyCheckOk(db) ){
1479 (void)SQLITE_MISUSE_BKPT;
1480 return;
1481 }
1482#endif
drhbd0b1b52008-07-07 19:52:09 +00001483 sqlite3_mutex_enter(db->mutex);
1484 if( nOps>0 ){
1485 db->xProgress = xProgress;
drh8f8c65f2013-07-10 18:14:29 +00001486 db->nProgressOps = (unsigned)nOps;
drhbd0b1b52008-07-07 19:52:09 +00001487 db->pProgressArg = pArg;
1488 }else{
1489 db->xProgress = 0;
1490 db->nProgressOps = 0;
1491 db->pProgressArg = 0;
danielk1977348bb5d2003-10-18 09:37:26 +00001492 }
drhbd0b1b52008-07-07 19:52:09 +00001493 sqlite3_mutex_leave(db->mutex);
danielk1977348bb5d2003-10-18 09:37:26 +00001494}
1495#endif
1496
1497
drh2dfbbca2000-07-28 14:32:48 +00001498/*
1499** This routine installs a default busy handler that waits for the
1500** specified number of milliseconds before returning 0.
1501*/
danielk1977f9d64d22004-06-19 08:18:07 +00001502int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh9ca95732014-10-24 00:35:58 +00001503#ifdef SQLITE_ENABLE_API_ARMOR
1504 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1505#endif
drh2dfbbca2000-07-28 14:32:48 +00001506 if( ms>0 ){
drh97903fe2005-05-24 20:19:57 +00001507 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
drhc0c7b5e2012-09-07 18:49:57 +00001508 db->busyTimeout = ms;
drh2dfbbca2000-07-28 14:32:48 +00001509 }else{
danielk197724b03fd2004-05-10 10:34:34 +00001510 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +00001511 }
danielk1977f9d64d22004-06-19 08:18:07 +00001512 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +00001513}
drh4c504392000-10-16 22:06:40 +00001514
1515/*
1516** Cause any pending operation to stop at its earliest opportunity.
1517*/
drh9bb575f2004-09-06 17:24:11 +00001518void sqlite3_interrupt(sqlite3 *db){
drh9ca95732014-10-24 00:35:58 +00001519#ifdef SQLITE_ENABLE_API_ARMOR
1520 if( !sqlite3SafetyCheckOk(db) ){
1521 (void)SQLITE_MISUSE_BKPT;
1522 return;
1523 }
1524#endif
drhbd0b1b52008-07-07 19:52:09 +00001525 db->u1.isInterrupted = 1;
drh4c504392000-10-16 22:06:40 +00001526}
drhfa86c412002-02-02 15:01:15 +00001527
drhfa86c412002-02-02 15:01:15 +00001528
1529/*
danielk1977771151b2006-01-17 13:21:40 +00001530** This function is exactly the same as sqlite3_create_function(), except
1531** that it is designed to be called by internal code. The difference is
1532** that if a malloc() fails in sqlite3_create_function(), an error code
1533** is returned and the mallocFailed flag cleared.
drhfa86c412002-02-02 15:01:15 +00001534*/
danielk1977771151b2006-01-17 13:21:40 +00001535int sqlite3CreateFunc(
danielk197765904932004-05-26 06:18:37 +00001536 sqlite3 *db,
1537 const char *zFunctionName,
1538 int nArg,
danielk1977d8123362004-06-12 09:25:12 +00001539 int enc,
danielk197765904932004-05-26 06:18:37 +00001540 void *pUserData,
1541 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1542 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
dand2199f02010-08-27 17:48:52 +00001543 void (*xFinal)(sqlite3_context*),
1544 FuncDestructor *pDestructor
drh8e0a2f92002-02-23 23:45:45 +00001545){
drh0bce8352002-02-28 00:41:10 +00001546 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +00001547 int nName;
drh4a8ee3d2013-12-14 13:44:22 +00001548 int extraFlags;
danielk197765904932004-05-26 06:18:37 +00001549
drhe30f4422007-08-21 16:15:55 +00001550 assert( sqlite3_mutex_held(db->mutex) );
drhc60d0442004-09-30 13:43:13 +00001551 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +00001552 (xFunc && (xFinal || xStep)) ||
1553 (!xFunc && (xFinal && !xStep)) ||
1554 (!xFunc && (!xFinal && xStep)) ||
drh24b58dd2008-07-07 14:50:14 +00001555 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
drhdee0e402009-05-03 20:23:53 +00001556 (255<(nName = sqlite3Strlen30( zFunctionName))) ){
drh413c3d32010-02-23 20:11:56 +00001557 return SQLITE_MISUSE_BKPT;
danielk197765904932004-05-26 06:18:37 +00001558 }
drh4a8ee3d2013-12-14 13:44:22 +00001559
1560 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
1561 extraFlags = enc & SQLITE_DETERMINISTIC;
1562 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
danielk1977d8123362004-06-12 09:25:12 +00001563
danielk197793758c82005-01-21 08:13:14 +00001564#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001565 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1566 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1567 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1568 **
1569 ** If SQLITE_ANY is specified, add three versions of the function
1570 ** to the hash table.
1571 */
1572 if( enc==SQLITE_UTF16 ){
1573 enc = SQLITE_UTF16NATIVE;
1574 }else if( enc==SQLITE_ANY ){
1575 int rc;
drh4a8ee3d2013-12-14 13:44:22 +00001576 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
dand2199f02010-08-27 17:48:52 +00001577 pUserData, xFunc, xStep, xFinal, pDestructor);
drhe30f4422007-08-21 16:15:55 +00001578 if( rc==SQLITE_OK ){
drh4a8ee3d2013-12-14 13:44:22 +00001579 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
dand2199f02010-08-27 17:48:52 +00001580 pUserData, xFunc, xStep, xFinal, pDestructor);
drhe30f4422007-08-21 16:15:55 +00001581 }
1582 if( rc!=SQLITE_OK ){
1583 return rc;
1584 }
danielk1977d8123362004-06-12 09:25:12 +00001585 enc = SQLITE_UTF16BE;
1586 }
danielk197793758c82005-01-21 08:13:14 +00001587#else
1588 enc = SQLITE_UTF8;
1589#endif
danielk19779636c4e2005-01-25 04:27:54 +00001590
1591 /* Check if an existing function is being overridden or deleted. If so,
1592 ** and there are active VMs, then return SQLITE_BUSY. If a function
1593 ** is being overridden/deleted but there are no active VMs, allow the
1594 ** operation to continue but invalidate all precompiled statements.
1595 */
drh3abbd392008-12-10 23:04:13 +00001596 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
drhd36e1042013-09-06 13:10:12 +00001597 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
drh4f7d3a52013-06-27 23:54:02 +00001598 if( db->nVdbeActive ){
drh13f40da2014-08-22 18:00:11 +00001599 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:57 +00001600 "unable to delete/modify user-function due to active statements");
drh17435752007-08-16 04:30:38 +00001601 assert( !db->mallocFailed );
danielk19779636c4e2005-01-25 04:27:54 +00001602 return SQLITE_BUSY;
1603 }else{
1604 sqlite3ExpirePreparedStatements(db);
1605 }
1606 }
danielk197765904932004-05-26 06:18:37 +00001607
drh3abbd392008-12-10 23:04:13 +00001608 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
danielk1977fa18bec2007-09-03 11:04:22 +00001609 assert(p || db->mallocFailed);
1610 if( !p ){
1611 return SQLITE_NOMEM;
danielk1977771151b2006-01-17 13:21:40 +00001612 }
dand2199f02010-08-27 17:48:52 +00001613
1614 /* If an older version of the function with a configured destructor is
1615 ** being replaced invoke the destructor function here. */
1616 functionDestroy(db, p);
1617
1618 if( pDestructor ){
1619 pDestructor->nRef++;
1620 }
1621 p->pDestructor = pDestructor;
drh4a8ee3d2013-12-14 13:44:22 +00001622 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
1623 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
danielk1977fa18bec2007-09-03 11:04:22 +00001624 p->xFunc = xFunc;
1625 p->xStep = xStep;
1626 p->xFinalize = xFinal;
1627 p->pUserData = pUserData;
drh1bd10f82008-12-10 21:19:56 +00001628 p->nArg = (u16)nArg;
danielk197765904932004-05-26 06:18:37 +00001629 return SQLITE_OK;
1630}
danielk1977771151b2006-01-17 13:21:40 +00001631
1632/*
1633** Create new user functions.
1634*/
1635int sqlite3_create_function(
1636 sqlite3 *db,
dand2199f02010-08-27 17:48:52 +00001637 const char *zFunc,
danielk1977771151b2006-01-17 13:21:40 +00001638 int nArg,
1639 int enc,
1640 void *p,
1641 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1642 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1643 void (*xFinal)(sqlite3_context*)
1644){
drh7b19fac2010-08-27 18:44:54 +00001645 return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
1646 xFinal, 0);
dand2199f02010-08-27 17:48:52 +00001647}
1648
1649int sqlite3_create_function_v2(
1650 sqlite3 *db,
1651 const char *zFunc,
1652 int nArg,
1653 int enc,
1654 void *p,
1655 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1656 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1657 void (*xFinal)(sqlite3_context*),
1658 void (*xDestroy)(void *)
1659){
shanehbd2aaf92010-09-01 02:38:21 +00001660 int rc = SQLITE_ERROR;
dand2199f02010-08-27 17:48:52 +00001661 FuncDestructor *pArg = 0;
drh9ca95732014-10-24 00:35:58 +00001662
1663#ifdef SQLITE_ENABLE_API_ARMOR
1664 if( !sqlite3SafetyCheckOk(db) ){
1665 return SQLITE_MISUSE_BKPT;
1666 }
1667#endif
dand2199f02010-08-27 17:48:52 +00001668 sqlite3_mutex_enter(db->mutex);
1669 if( xDestroy ){
1670 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
dan9508daa2010-08-28 18:58:00 +00001671 if( !pArg ){
1672 xDestroy(p);
1673 goto out;
1674 }
dand2199f02010-08-27 17:48:52 +00001675 pArg->xDestroy = xDestroy;
1676 pArg->pUserData = p;
1677 }
1678 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
1679 if( pArg && pArg->nRef==0 ){
1680 assert( rc!=SQLITE_OK );
dan9508daa2010-08-28 18:58:00 +00001681 xDestroy(p);
dand2199f02010-08-27 17:48:52 +00001682 sqlite3DbFree(db, pArg);
1683 }
1684
1685 out:
drhe30f4422007-08-21 16:15:55 +00001686 rc = sqlite3ApiExit(db, rc);
1687 sqlite3_mutex_leave(db->mutex);
1688 return rc;
danielk1977771151b2006-01-17 13:21:40 +00001689}
1690
danielk197793758c82005-01-21 08:13:14 +00001691#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +00001692int sqlite3_create_function16(
1693 sqlite3 *db,
1694 const void *zFunctionName,
1695 int nArg,
1696 int eTextRep,
danielk1977771151b2006-01-17 13:21:40 +00001697 void *p,
danielk197765904932004-05-26 06:18:37 +00001698 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
1699 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
1700 void (*xFinal)(sqlite3_context*)
1701){
1702 int rc;
drhaf9a7c22005-12-15 03:04:10 +00001703 char *zFunc8;
drh9ca95732014-10-24 00:35:58 +00001704
1705#ifdef SQLITE_ENABLE_API_ARMOR
1706 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT;
1707#endif
drhe30f4422007-08-21 16:15:55 +00001708 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:38 +00001709 assert( !db->mallocFailed );
danb7dca7d2010-03-05 16:32:12 +00001710 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
dand2199f02010-08-27 17:48:52 +00001711 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
drh633e6d52008-07-28 19:34:53 +00001712 sqlite3DbFree(db, zFunc8);
drhe30f4422007-08-21 16:15:55 +00001713 rc = sqlite3ApiExit(db, rc);
1714 sqlite3_mutex_leave(db->mutex);
1715 return rc;
drh8e0a2f92002-02-23 23:45:45 +00001716}
danielk197793758c82005-01-21 08:13:14 +00001717#endif
drhc9b84a12002-06-20 11:36:48 +00001718
drhb7481e72006-09-16 21:45:14 +00001719
1720/*
1721** Declare that a function has been overloaded by a virtual table.
1722**
1723** If the function already exists as a regular global function, then
1724** this routine is a no-op. If the function does not exist, then create
1725** a new one that always throws a run-time error.
1726**
1727** When virtual tables intend to provide an overloaded function, they
1728** should call this routine to make sure the global function exists.
1729** A global function must exist in order for name resolution to work
1730** properly.
1731*/
1732int sqlite3_overload_function(
1733 sqlite3 *db,
1734 const char *zName,
1735 int nArg
1736){
drhdee0e402009-05-03 20:23:53 +00001737 int nName = sqlite3Strlen30(zName);
drhcb5e4db2011-10-12 23:49:49 +00001738 int rc = SQLITE_OK;
drh9ca95732014-10-24 00:35:58 +00001739
1740#ifdef SQLITE_ENABLE_API_ARMOR
1741 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
1742 return SQLITE_MISUSE_BKPT;
1743 }
1744#endif
drhe30f4422007-08-21 16:15:55 +00001745 sqlite3_mutex_enter(db->mutex);
drhb7481e72006-09-16 21:45:14 +00001746 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
drhcb5e4db2011-10-12 23:49:49 +00001747 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
1748 0, sqlite3InvalidFunction, 0, 0, 0);
drhb7481e72006-09-16 21:45:14 +00001749 }
drhcb5e4db2011-10-12 23:49:49 +00001750 rc = sqlite3ApiExit(db, rc);
drhe30f4422007-08-21 16:15:55 +00001751 sqlite3_mutex_leave(db->mutex);
1752 return rc;
drhb7481e72006-09-16 21:45:14 +00001753}
1754
drh19e2d372005-08-29 23:00:03 +00001755#ifndef SQLITE_OMIT_TRACE
drhc9b84a12002-06-20 11:36:48 +00001756/*
drh18de4822003-01-16 16:28:53 +00001757** Register a trace function. The pArg from the previously registered trace
1758** is returned.
1759**
1760** A NULL trace function means that no tracing is executes. A non-NULL
1761** trace is a pointer to a function that is invoked at the start of each
drh19e2d372005-08-29 23:00:03 +00001762** SQL statement.
drh18de4822003-01-16 16:28:53 +00001763*/
drh9bb575f2004-09-06 17:24:11 +00001764void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drhe30f4422007-08-21 16:15:55 +00001765 void *pOld;
drh9ca95732014-10-24 00:35:58 +00001766
1767#ifdef SQLITE_ENABLE_API_ARMOR
1768 if( !sqlite3SafetyCheckOk(db) ){
1769 (void)SQLITE_MISUSE_BKPT;
1770 return 0;
1771 }
1772#endif
drhe30f4422007-08-21 16:15:55 +00001773 sqlite3_mutex_enter(db->mutex);
1774 pOld = db->pTraceArg;
drh18de4822003-01-16 16:28:53 +00001775 db->xTrace = xTrace;
1776 db->pTraceArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001777 sqlite3_mutex_leave(db->mutex);
drh18de4822003-01-16 16:28:53 +00001778 return pOld;
drh0d1a6432003-04-03 15:46:04 +00001779}
drh19e2d372005-08-29 23:00:03 +00001780/*
1781** Register a profile function. The pArg from the previously registered
1782** profile function is returned.
1783**
1784** A NULL profile function means that no profiling is executes. A non-NULL
1785** profile is a pointer to a function that is invoked at the conclusion of
1786** each SQL statement that is run.
1787*/
1788void *sqlite3_profile(
1789 sqlite3 *db,
1790 void (*xProfile)(void*,const char*,sqlite_uint64),
1791 void *pArg
1792){
drhe30f4422007-08-21 16:15:55 +00001793 void *pOld;
drh9ca95732014-10-24 00:35:58 +00001794
1795#ifdef SQLITE_ENABLE_API_ARMOR
1796 if( !sqlite3SafetyCheckOk(db) ){
1797 (void)SQLITE_MISUSE_BKPT;
1798 return 0;
1799 }
1800#endif
drhe30f4422007-08-21 16:15:55 +00001801 sqlite3_mutex_enter(db->mutex);
1802 pOld = db->pProfileArg;
drh19e2d372005-08-29 23:00:03 +00001803 db->xProfile = xProfile;
1804 db->pProfileArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001805 sqlite3_mutex_leave(db->mutex);
drh19e2d372005-08-29 23:00:03 +00001806 return pOld;
1807}
1808#endif /* SQLITE_OMIT_TRACE */
paulb0208cc2003-04-13 18:26:49 +00001809
drhcd565fd2012-03-01 21:30:44 +00001810/*
1811** Register a function to be invoked when a transaction commits.
danielk197771fd80b2005-12-16 06:54:01 +00001812** If the invoked function returns non-zero, then the commit becomes a
drhaa940ea2004-01-15 02:44:03 +00001813** rollback.
1814*/
danielk197724b03fd2004-05-10 10:34:34 +00001815void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +00001816 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +00001817 int (*xCallback)(void*), /* Function to invoke on each commit */
1818 void *pArg /* Argument to the function */
1819){
drhe30f4422007-08-21 16:15:55 +00001820 void *pOld;
drh9ca95732014-10-24 00:35:58 +00001821
1822#ifdef SQLITE_ENABLE_API_ARMOR
1823 if( !sqlite3SafetyCheckOk(db) ){
1824 (void)SQLITE_MISUSE_BKPT;
1825 return 0;
1826 }
1827#endif
drhe30f4422007-08-21 16:15:55 +00001828 sqlite3_mutex_enter(db->mutex);
1829 pOld = db->pCommitArg;
drhaa940ea2004-01-15 02:44:03 +00001830 db->xCommitCallback = xCallback;
1831 db->pCommitArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001832 sqlite3_mutex_leave(db->mutex);
drhaa940ea2004-01-15 02:44:03 +00001833 return pOld;
1834}
1835
danielk197794eb6a12005-12-15 15:22:08 +00001836/*
1837** Register a callback to be invoked each time a row is updated,
1838** inserted or deleted using this database connection.
1839*/
danielk197771fd80b2005-12-16 06:54:01 +00001840void *sqlite3_update_hook(
danielk197794eb6a12005-12-15 15:22:08 +00001841 sqlite3 *db, /* Attach the hook to this database */
1842 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
1843 void *pArg /* Argument to the function */
1844){
drhe30f4422007-08-21 16:15:55 +00001845 void *pRet;
drh9ca95732014-10-24 00:35:58 +00001846
1847#ifdef SQLITE_ENABLE_API_ARMOR
1848 if( !sqlite3SafetyCheckOk(db) ){
1849 (void)SQLITE_MISUSE_BKPT;
1850 return 0;
1851 }
1852#endif
drhe30f4422007-08-21 16:15:55 +00001853 sqlite3_mutex_enter(db->mutex);
1854 pRet = db->pUpdateArg;
danielk197794eb6a12005-12-15 15:22:08 +00001855 db->xUpdateCallback = xCallback;
1856 db->pUpdateArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001857 sqlite3_mutex_leave(db->mutex);
danielk197771fd80b2005-12-16 06:54:01 +00001858 return pRet;
danielk197794eb6a12005-12-15 15:22:08 +00001859}
1860
danielk197771fd80b2005-12-16 06:54:01 +00001861/*
1862** Register a callback to be invoked each time a transaction is rolled
1863** back by this database connection.
1864*/
1865void *sqlite3_rollback_hook(
1866 sqlite3 *db, /* Attach the hook to this database */
1867 void (*xCallback)(void*), /* Callback function */
1868 void *pArg /* Argument to the function */
1869){
drhe30f4422007-08-21 16:15:55 +00001870 void *pRet;
drh9ca95732014-10-24 00:35:58 +00001871
1872#ifdef SQLITE_ENABLE_API_ARMOR
1873 if( !sqlite3SafetyCheckOk(db) ){
1874 (void)SQLITE_MISUSE_BKPT;
1875 return 0;
1876 }
1877#endif
drhe30f4422007-08-21 16:15:55 +00001878 sqlite3_mutex_enter(db->mutex);
1879 pRet = db->pRollbackArg;
danielk197771fd80b2005-12-16 06:54:01 +00001880 db->xRollbackCallback = xCallback;
1881 db->pRollbackArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001882 sqlite3_mutex_leave(db->mutex);
danielk197771fd80b2005-12-16 06:54:01 +00001883 return pRet;
1884}
drhaa940ea2004-01-15 02:44:03 +00001885
dan586b9c82010-05-03 08:04:49 +00001886#ifndef SQLITE_OMIT_WAL
1887/*
1888** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
drh5def0842010-05-05 20:00:25 +00001889** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
1890** is greater than sqlite3.pWalArg cast to an integer (the value configured by
drhb033d8b2010-05-03 13:37:30 +00001891** wal_autocheckpoint()).
dan586b9c82010-05-03 08:04:49 +00001892*/
drhb033d8b2010-05-03 13:37:30 +00001893int sqlite3WalDefaultHook(
drh5def0842010-05-05 20:00:25 +00001894 void *pClientData, /* Argument */
drhb033d8b2010-05-03 13:37:30 +00001895 sqlite3 *db, /* Connection */
drh5def0842010-05-05 20:00:25 +00001896 const char *zDb, /* Database */
drhb033d8b2010-05-03 13:37:30 +00001897 int nFrame /* Size of WAL */
1898){
drh5def0842010-05-05 20:00:25 +00001899 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
drhf5330352010-05-24 20:27:44 +00001900 sqlite3BeginBenignMalloc();
drh5def0842010-05-05 20:00:25 +00001901 sqlite3_wal_checkpoint(db, zDb);
drhf5330352010-05-24 20:27:44 +00001902 sqlite3EndBenignMalloc();
drh5def0842010-05-05 20:00:25 +00001903 }
1904 return SQLITE_OK;
dan586b9c82010-05-03 08:04:49 +00001905}
drhb033d8b2010-05-03 13:37:30 +00001906#endif /* SQLITE_OMIT_WAL */
dan586b9c82010-05-03 08:04:49 +00001907
1908/*
1909** Configure an sqlite3_wal_hook() callback to automatically checkpoint
1910** a database after committing a transaction if there are nFrame or
1911** more frames in the log file. Passing zero or a negative value as the
1912** nFrame parameter disables automatic checkpoints entirely.
1913**
1914** The callback registered by this function replaces any existing callback
1915** registered using sqlite3_wal_hook(). Likewise, registering a callback
1916** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
1917** configured by this function.
1918*/
1919int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
shanehbd2aaf92010-09-01 02:38:21 +00001920#ifdef SQLITE_OMIT_WAL
drh9dd55f52010-09-03 03:32:46 +00001921 UNUSED_PARAMETER(db);
1922 UNUSED_PARAMETER(nFrame);
shanehbd2aaf92010-09-01 02:38:21 +00001923#else
drh9ca95732014-10-24 00:35:58 +00001924#ifdef SQLITE_ENABLE_API_ARMOR
1925 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1926#endif
dan586b9c82010-05-03 08:04:49 +00001927 if( nFrame>0 ){
drhb033d8b2010-05-03 13:37:30 +00001928 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
dan586b9c82010-05-03 08:04:49 +00001929 }else{
1930 sqlite3_wal_hook(db, 0, 0);
1931 }
drhb033d8b2010-05-03 13:37:30 +00001932#endif
dan586b9c82010-05-03 08:04:49 +00001933 return SQLITE_OK;
1934}
1935
paulb0208cc2003-04-13 18:26:49 +00001936/*
dan8d22a172010-04-19 18:03:51 +00001937** Register a callback to be invoked each time a transaction is written
1938** into the write-ahead-log by this database connection.
1939*/
drh833bf962010-04-28 14:42:19 +00001940void *sqlite3_wal_hook(
dan8d22a172010-04-19 18:03:51 +00001941 sqlite3 *db, /* Attach the hook to this db handle */
1942 int(*xCallback)(void *, sqlite3*, const char*, int),
1943 void *pArg /* First argument passed to xCallback() */
1944){
drhb033d8b2010-05-03 13:37:30 +00001945#ifndef SQLITE_OMIT_WAL
dan8d22a172010-04-19 18:03:51 +00001946 void *pRet;
drh9ca95732014-10-24 00:35:58 +00001947#ifdef SQLITE_ENABLE_API_ARMOR
1948 if( !sqlite3SafetyCheckOk(db) ){
1949 (void)SQLITE_MISUSE_BKPT;
1950 return 0;
1951 }
1952#endif
dan8d22a172010-04-19 18:03:51 +00001953 sqlite3_mutex_enter(db->mutex);
drh7ed91f22010-04-29 22:34:07 +00001954 pRet = db->pWalArg;
1955 db->xWalCallback = xCallback;
1956 db->pWalArg = pArg;
dan8d22a172010-04-19 18:03:51 +00001957 sqlite3_mutex_leave(db->mutex);
1958 return pRet;
drhb033d8b2010-05-03 13:37:30 +00001959#else
1960 return 0;
1961#endif
dan8d22a172010-04-19 18:03:51 +00001962}
1963
1964/*
dancdc1f042010-11-18 12:11:05 +00001965** Checkpoint database zDb.
dan586b9c82010-05-03 08:04:49 +00001966*/
dancdc1f042010-11-18 12:11:05 +00001967int sqlite3_wal_checkpoint_v2(
1968 sqlite3 *db, /* Database handle */
1969 const char *zDb, /* Name of attached database (or NULL) */
1970 int eMode, /* SQLITE_CHECKPOINT_* value */
1971 int *pnLog, /* OUT: Size of WAL log in frames */
1972 int *pnCkpt /* OUT: Total number of frames checkpointed */
1973){
drhb033d8b2010-05-03 13:37:30 +00001974#ifdef SQLITE_OMIT_WAL
1975 return SQLITE_OK;
1976#else
dan586b9c82010-05-03 08:04:49 +00001977 int rc; /* Return code */
dan6fcff072010-05-03 14:05:43 +00001978 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */
dan586b9c82010-05-03 08:04:49 +00001979
drh9ca95732014-10-24 00:35:58 +00001980#ifdef SQLITE_ENABLE_API_ARMOR
1981 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
1982#endif
1983
dan9c5e3682011-02-07 15:12:12 +00001984 /* Initialize the output variables to -1 in case an error occurs. */
1985 if( pnLog ) *pnLog = -1;
1986 if( pnCkpt ) *pnCkpt = -1;
1987
danf26a1542014-12-02 19:04:54 +00001988 assert( SQLITE_CHECKPOINT_PASSIVE==0 );
1989 assert( SQLITE_CHECKPOINT_FULL==1 );
1990 assert( SQLITE_CHECKPOINT_RESTART==2 );
1991 assert( SQLITE_CHECKPOINT_TRUNCATE==3 );
1992 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){
drhbb9a3782014-12-03 18:32:47 +00001993 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
1994 ** mode: */
dancdc1f042010-11-18 12:11:05 +00001995 return SQLITE_MISUSE;
1996 }
1997
dan586b9c82010-05-03 08:04:49 +00001998 sqlite3_mutex_enter(db->mutex);
danaf0cfd32010-05-03 15:58:50 +00001999 if( zDb && zDb[0] ){
dan586b9c82010-05-03 08:04:49 +00002000 iDb = sqlite3FindDbName(db, zDb);
2001 }
2002 if( iDb<0 ){
2003 rc = SQLITE_ERROR;
drh13f40da2014-08-22 18:00:11 +00002004 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb);
dan586b9c82010-05-03 08:04:49 +00002005 }else{
dan976b0032015-01-29 19:12:12 +00002006 db->busyHandler.nBusy = 0;
dancdc1f042010-11-18 12:11:05 +00002007 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
drh13f40da2014-08-22 18:00:11 +00002008 sqlite3Error(db, rc);
dan586b9c82010-05-03 08:04:49 +00002009 }
drhb033d8b2010-05-03 13:37:30 +00002010 rc = sqlite3ApiExit(db, rc);
dan586b9c82010-05-03 08:04:49 +00002011 sqlite3_mutex_leave(db->mutex);
drhb033d8b2010-05-03 13:37:30 +00002012 return rc;
2013#endif
dan586b9c82010-05-03 08:04:49 +00002014}
2015
dancdc1f042010-11-18 12:11:05 +00002016
2017/*
2018** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
2019** to contains a zero-length string, all attached databases are
2020** checkpointed.
2021*/
2022int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
drhbb9a3782014-12-03 18:32:47 +00002023 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
2024 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */
2025 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0);
dancdc1f042010-11-18 12:11:05 +00002026}
2027
drhb033d8b2010-05-03 13:37:30 +00002028#ifndef SQLITE_OMIT_WAL
dan586b9c82010-05-03 08:04:49 +00002029/*
2030** Run a checkpoint on database iDb. This is a no-op if database iDb is
2031** not currently open in WAL mode.
2032**
dan6fcff072010-05-03 14:05:43 +00002033** If a transaction is open on the database being checkpointed, this
2034** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
2035** an error occurs while running the checkpoint, an SQLite error code is
2036** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
dan586b9c82010-05-03 08:04:49 +00002037**
2038** The mutex on database handle db should be held by the caller. The mutex
2039** associated with the specific b-tree being checkpointed is taken by
2040** this function while the checkpoint is running.
dan6fcff072010-05-03 14:05:43 +00002041**
2042** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
2043** checkpointed. If an error is encountered it is returned immediately -
2044** no attempt is made to checkpoint any remaining databases.
dana58f26f2010-11-16 18:56:51 +00002045**
dancdc1f042010-11-18 12:11:05 +00002046** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
dan586b9c82010-05-03 08:04:49 +00002047*/
dancdc1f042010-11-18 12:11:05 +00002048int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
dan6fcff072010-05-03 14:05:43 +00002049 int rc = SQLITE_OK; /* Return code */
2050 int i; /* Used to iterate through attached dbs */
danf2b8dd52010-11-18 19:28:01 +00002051 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */
dan586b9c82010-05-03 08:04:49 +00002052
2053 assert( sqlite3_mutex_held(db->mutex) );
dan9c5e3682011-02-07 15:12:12 +00002054 assert( !pnLog || *pnLog==-1 );
2055 assert( !pnCkpt || *pnCkpt==-1 );
dan586b9c82010-05-03 08:04:49 +00002056
dan6fcff072010-05-03 14:05:43 +00002057 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
2058 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
dancdc1f042010-11-18 12:11:05 +00002059 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
danf2b8dd52010-11-18 19:28:01 +00002060 pnLog = 0;
2061 pnCkpt = 0;
2062 if( rc==SQLITE_BUSY ){
2063 bBusy = 1;
2064 rc = SQLITE_OK;
2065 }
dan6fcff072010-05-03 14:05:43 +00002066 }
dan586b9c82010-05-03 08:04:49 +00002067 }
2068
danf2b8dd52010-11-18 19:28:01 +00002069 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
dan586b9c82010-05-03 08:04:49 +00002070}
drhb033d8b2010-05-03 13:37:30 +00002071#endif /* SQLITE_OMIT_WAL */
dan586b9c82010-05-03 08:04:49 +00002072
2073/*
danielk1977d8293352009-04-30 09:10:37 +00002074** This function returns true if main-memory should be used instead of
2075** a temporary file for transient pager files and statement journals.
2076** The value returned depends on the value of db->temp_store (runtime
2077** parameter) and the compile time value of SQLITE_TEMP_STORE. The
2078** following table describes the relationship between these two values
2079** and this functions return value.
2080**
2081** SQLITE_TEMP_STORE db->temp_store Location of temporary database
2082** ----------------- -------------- ------------------------------
2083** 0 any file (return 0)
2084** 1 1 file (return 0)
2085** 1 2 memory (return 1)
2086** 1 0 file (return 0)
2087** 2 1 file (return 0)
2088** 2 2 memory (return 1)
2089** 2 0 memory (return 1)
2090** 3 any memory (return 1)
2091*/
drh1c514142009-04-30 12:25:10 +00002092int sqlite3TempInMemory(const sqlite3 *db){
danielk1977d8293352009-04-30 09:10:37 +00002093#if SQLITE_TEMP_STORE==1
2094 return ( db->temp_store==2 );
2095#endif
2096#if SQLITE_TEMP_STORE==2
2097 return ( db->temp_store!=1 );
2098#endif
2099#if SQLITE_TEMP_STORE==3
drhf5ed7ad2015-06-15 14:43:25 +00002100 UNUSED_PARAMETER(db);
danielk1977d8293352009-04-30 09:10:37 +00002101 return 1;
shanecf697392009-06-01 16:53:09 +00002102#endif
2103#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
drhf5ed7ad2015-06-15 14:43:25 +00002104 UNUSED_PARAMETER(db);
danielk1977d8293352009-04-30 09:10:37 +00002105 return 0;
shane60a4b532009-05-06 18:57:09 +00002106#endif
danielk1977d8293352009-04-30 09:10:37 +00002107}
2108
2109/*
danielk19774ad17132004-05-21 01:47:26 +00002110** Return UTF-8 encoded English language explanation of the most recent
2111** error.
2112*/
danielk19776622cce2004-05-20 11:00:52 +00002113const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +00002114 const char *z;
drhf49661a2008-12-10 16:45:50 +00002115 if( !db ){
danielk1977f20b21c2004-05-31 23:56:42 +00002116 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +00002117 }
drhd55d57e2008-07-07 17:53:07 +00002118 if( !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:56 +00002119 return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
danielk1977e35ee192004-06-26 09:50:11 +00002120 }
drh27641702007-08-22 02:56:42 +00002121 sqlite3_mutex_enter(db->mutex);
danielk1977238746a2009-03-19 18:51:06 +00002122 if( db->mallocFailed ){
2123 z = sqlite3ErrStr(SQLITE_NOMEM);
2124 }else{
drha3cc0072013-12-13 16:23:55 +00002125 testcase( db->pErr==0 );
danielk1977238746a2009-03-19 18:51:06 +00002126 z = (char*)sqlite3_value_text(db->pErr);
2127 assert( !db->mallocFailed );
2128 if( z==0 ){
2129 z = sqlite3ErrStr(db->errCode);
2130 }
danielk19776622cce2004-05-20 11:00:52 +00002131 }
drhe30f4422007-08-21 16:15:55 +00002132 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00002133 return z;
danielk19776622cce2004-05-20 11:00:52 +00002134}
2135
drh6c626082004-11-14 21:56:29 +00002136#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00002137/*
2138** Return UTF-16 encoded English language explanation of the most recent
2139** error.
2140*/
danielk19776622cce2004-05-20 11:00:52 +00002141const void *sqlite3_errmsg16(sqlite3 *db){
danielk19777c5c3ca2009-02-23 14:42:53 +00002142 static const u16 outOfMem[] = {
2143 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
danielk1977bfd6cce2004-06-18 04:24:54 +00002144 };
danielk19777c5c3ca2009-02-23 14:42:53 +00002145 static const u16 misuse[] = {
2146 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
2147 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
2148 'c', 'a', 'l', 'l', 'e', 'd', ' ',
2149 'o', 'u', 't', ' ',
2150 'o', 'f', ' ',
2151 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
danielk1977e35ee192004-06-26 09:50:11 +00002152 };
danielk19774ad17132004-05-21 01:47:26 +00002153
drhc60d0442004-09-30 13:43:13 +00002154 const void *z;
danielk1977ae7fc492007-03-29 15:00:52 +00002155 if( !db ){
danielk19777c5c3ca2009-02-23 14:42:53 +00002156 return (void *)outOfMem;
drhc60d0442004-09-30 13:43:13 +00002157 }
drhd55d57e2008-07-07 17:53:07 +00002158 if( !sqlite3SafetyCheckSickOrOk(db) ){
danielk19777c5c3ca2009-02-23 14:42:53 +00002159 return (void *)misuse;
drhc60d0442004-09-30 13:43:13 +00002160 }
drhe30f4422007-08-21 16:15:55 +00002161 sqlite3_mutex_enter(db->mutex);
danielk1977238746a2009-03-19 18:51:06 +00002162 if( db->mallocFailed ){
2163 z = (void *)outOfMem;
2164 }else{
drhc60d0442004-09-30 13:43:13 +00002165 z = sqlite3_value_text16(db->pErr);
danielk1977238746a2009-03-19 18:51:06 +00002166 if( z==0 ){
drh13f40da2014-08-22 18:00:11 +00002167 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
danielk1977238746a2009-03-19 18:51:06 +00002168 z = sqlite3_value_text16(db->pErr);
2169 }
2170 /* A malloc() may have failed within the call to sqlite3_value_text16()
2171 ** above. If this is the case, then the db->mallocFailed flag needs to
2172 ** be cleared before returning. Do this directly, instead of via
2173 ** sqlite3ApiExit(), to avoid setting the database handle error message.
2174 */
2175 db->mallocFailed = 0;
drhc60d0442004-09-30 13:43:13 +00002176 }
drhe30f4422007-08-21 16:15:55 +00002177 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00002178 return z;
danielk19776622cce2004-05-20 11:00:52 +00002179}
drh6c626082004-11-14 21:56:29 +00002180#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00002181
drhc60d0442004-09-30 13:43:13 +00002182/*
danielk19777ddad962005-12-12 06:53:03 +00002183** Return the most recent error code generated by an SQLite routine. If NULL is
2184** passed to this function, we assume a malloc() failed during sqlite3_open().
drhc60d0442004-09-30 13:43:13 +00002185*/
danielk19776622cce2004-05-20 11:00:52 +00002186int sqlite3_errcode(sqlite3 *db){
danielk19777c36d072008-01-31 15:31:01 +00002187 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:56 +00002188 return SQLITE_MISUSE_BKPT;
drhc60d0442004-09-30 13:43:13 +00002189 }
drh01495b92008-01-23 12:52:40 +00002190 if( !db || db->mallocFailed ){
2191 return SQLITE_NOMEM;
2192 }
drh4ac285a2006-09-15 07:28:50 +00002193 return db->errCode & db->errMask;
danielk19776622cce2004-05-20 11:00:52 +00002194}
drh99dfe5e2008-10-30 15:03:15 +00002195int sqlite3_extended_errcode(sqlite3 *db){
2196 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
drh413c3d32010-02-23 20:11:56 +00002197 return SQLITE_MISUSE_BKPT;
drh99dfe5e2008-10-30 15:03:15 +00002198 }
2199 if( !db || db->mallocFailed ){
2200 return SQLITE_NOMEM;
2201 }
2202 return db->errCode;
2203}
danielk19776622cce2004-05-20 11:00:52 +00002204
drh0e819f92006-02-01 13:50:41 +00002205/*
mistachkin5dac8432012-09-11 02:00:25 +00002206** Return a string that describes the kind of error specified in the
2207** argument. For now, this simply calls the internal sqlite3ErrStr()
2208** function.
2209*/
2210const char *sqlite3_errstr(int rc){
2211 return sqlite3ErrStr(rc);
2212}
2213
2214/*
drh0e819f92006-02-01 13:50:41 +00002215** Create a new collating function for database "db". The name is zName
2216** and the encoding is enc.
2217*/
danielk19779a30cf62006-01-18 04:26:07 +00002218static int createCollation(
drhf8d4e8b2009-08-20 02:49:30 +00002219 sqlite3* db,
danielk19779a30cf62006-01-18 04:26:07 +00002220 const char *zName,
shanecea72b22009-09-07 04:38:36 +00002221 u8 enc,
danielk19779a30cf62006-01-18 04:26:07 +00002222 void* pCtx,
danielk1977a9808b32007-05-07 09:32:45 +00002223 int(*xCompare)(void*,int,const void*,int,const void*),
2224 void(*xDel)(void*)
danielk19779a30cf62006-01-18 04:26:07 +00002225){
2226 CollSeq *pColl;
drh7d9bd4e2006-02-16 18:16:36 +00002227 int enc2;
danielk19779a30cf62006-01-18 04:26:07 +00002228
drhe30f4422007-08-21 16:15:55 +00002229 assert( sqlite3_mutex_held(db->mutex) );
danielk19779a30cf62006-01-18 04:26:07 +00002230
2231 /* If SQLITE_UTF16 is specified as the encoding type, transform this
2232 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
2233 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
2234 */
drh51c7d862009-04-27 18:46:06 +00002235 enc2 = enc;
danielk1977ebb32932009-04-28 15:35:38 +00002236 testcase( enc2==SQLITE_UTF16 );
2237 testcase( enc2==SQLITE_UTF16_ALIGNED );
2238 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
drh7d9bd4e2006-02-16 18:16:36 +00002239 enc2 = SQLITE_UTF16NATIVE;
danielk19779a30cf62006-01-18 04:26:07 +00002240 }
drh51c7d862009-04-27 18:46:06 +00002241 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
drh413c3d32010-02-23 20:11:56 +00002242 return SQLITE_MISUSE_BKPT;
danielk19779a30cf62006-01-18 04:26:07 +00002243 }
2244
2245 /* Check if this call is removing or replacing an existing collation
2246 ** sequence. If so, and there are active VMs, return busy. If there
2247 ** are no active VMs, invalidate any pre-compiled statements.
2248 */
drhc4a64fa2009-05-11 20:53:28 +00002249 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
danielk19779a30cf62006-01-18 04:26:07 +00002250 if( pColl && pColl->xCmp ){
drh4f7d3a52013-06-27 23:54:02 +00002251 if( db->nVdbeActive ){
drh13f40da2014-08-22 18:00:11 +00002252 sqlite3ErrorWithMsg(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:57 +00002253 "unable to delete/modify collation sequence due to active statements");
danielk19779a30cf62006-01-18 04:26:07 +00002254 return SQLITE_BUSY;
2255 }
2256 sqlite3ExpirePreparedStatements(db);
danielk1977a9808b32007-05-07 09:32:45 +00002257
2258 /* If collation sequence pColl was created directly by a call to
2259 ** sqlite3_create_collation, and not generated by synthCollSeq(),
2260 ** then any copies made by synthCollSeq() need to be invalidated.
2261 ** Also, collation destructor - CollSeq.xDel() - function may need
2262 ** to be called.
2263 */
2264 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
drhacbcb7e2014-08-21 20:26:37 +00002265 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
danielk1977a9808b32007-05-07 09:32:45 +00002266 int j;
2267 for(j=0; j<3; j++){
2268 CollSeq *p = &aColl[j];
2269 if( p->enc==pColl->enc ){
2270 if( p->xDel ){
2271 p->xDel(p->pUser);
2272 }
2273 p->xCmp = 0;
2274 }
2275 }
2276 }
danielk19779a30cf62006-01-18 04:26:07 +00002277 }
2278
drhc4a64fa2009-05-11 20:53:28 +00002279 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
drh88404c22010-10-11 17:58:21 +00002280 if( pColl==0 ) return SQLITE_NOMEM;
2281 pColl->xCmp = xCompare;
2282 pColl->pUser = pCtx;
2283 pColl->xDel = xDel;
2284 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
drh13f40da2014-08-22 18:00:11 +00002285 sqlite3Error(db, SQLITE_OK);
danielk19779a30cf62006-01-18 04:26:07 +00002286 return SQLITE_OK;
2287}
2288
2289
danielk19776622cce2004-05-20 11:00:52 +00002290/*
drhbb4957f2008-03-20 14:03:29 +00002291** This array defines hard upper bounds on limit values. The
2292** initializer must be kept in sync with the SQLITE_LIMIT_*
2293** #defines in sqlite3.h.
2294*/
drhb1a6c3c2008-03-20 16:30:17 +00002295static const int aHardLimit[] = {
drhbb4957f2008-03-20 14:03:29 +00002296 SQLITE_MAX_LENGTH,
2297 SQLITE_MAX_SQL_LENGTH,
2298 SQLITE_MAX_COLUMN,
2299 SQLITE_MAX_EXPR_DEPTH,
2300 SQLITE_MAX_COMPOUND_SELECT,
2301 SQLITE_MAX_VDBE_OP,
2302 SQLITE_MAX_FUNCTION_ARG,
2303 SQLITE_MAX_ATTACHED,
drhbb4957f2008-03-20 14:03:29 +00002304 SQLITE_MAX_LIKE_PATTERN_LENGTH,
drh9f959b02014-08-11 17:37:27 +00002305 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
drh417168a2009-09-07 18:14:02 +00002306 SQLITE_MAX_TRIGGER_DEPTH,
drh111544c2014-08-29 16:20:47 +00002307 SQLITE_MAX_WORKER_THREADS,
drhbb4957f2008-03-20 14:03:29 +00002308};
2309
2310/*
2311** Make sure the hard limits are set to reasonable values
2312*/
2313#if SQLITE_MAX_LENGTH<100
2314# error SQLITE_MAX_LENGTH must be at least 100
2315#endif
2316#if SQLITE_MAX_SQL_LENGTH<100
2317# error SQLITE_MAX_SQL_LENGTH must be at least 100
2318#endif
2319#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
2320# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
2321#endif
drhbb4957f2008-03-20 14:03:29 +00002322#if SQLITE_MAX_COMPOUND_SELECT<2
2323# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
2324#endif
2325#if SQLITE_MAX_VDBE_OP<40
2326# error SQLITE_MAX_VDBE_OP must be at least 40
2327#endif
drhe82f5d02008-10-07 19:53:14 +00002328#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
2329# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
drhbb4957f2008-03-20 14:03:29 +00002330#endif
drhd08b2792014-07-22 15:33:31 +00002331#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125
2332# error SQLITE_MAX_ATTACHED must be between 0 and 125
drhbb4957f2008-03-20 14:03:29 +00002333#endif
drhbb4957f2008-03-20 14:03:29 +00002334#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
2335# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
2336#endif
drh70a8ca32008-08-21 18:49:27 +00002337#if SQLITE_MAX_COLUMN>32767
2338# error SQLITE_MAX_COLUMN must not exceed 32767
2339#endif
drh417168a2009-09-07 18:14:02 +00002340#if SQLITE_MAX_TRIGGER_DEPTH<1
2341# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
2342#endif
drh111544c2014-08-29 16:20:47 +00002343#if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50
2344# error SQLITE_MAX_WORKER_THREADS must be between 0 and 50
2345#endif
drhbb4957f2008-03-20 14:03:29 +00002346
2347
2348/*
2349** Change the value of a limit. Report the old value.
2350** If an invalid limit index is supplied, report -1.
2351** Make no changes but still report the old value if the
2352** new limit is negative.
2353**
2354** A new lower limit does not shrink existing constructs.
2355** It merely prevents new constructs that exceed the limit
2356** from forming.
2357*/
2358int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
2359 int oldLimit;
drh4e93f5b2010-09-07 14:59:15 +00002360
drh9ca95732014-10-24 00:35:58 +00002361#ifdef SQLITE_ENABLE_API_ARMOR
2362 if( !sqlite3SafetyCheckOk(db) ){
2363 (void)SQLITE_MISUSE_BKPT;
2364 return -1;
2365 }
2366#endif
drh4e93f5b2010-09-07 14:59:15 +00002367
2368 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
2369 ** there is a hard upper bound set at compile-time by a C preprocessor
2370 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
2371 ** "_MAX_".)
2372 */
2373 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
2374 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
2375 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
2376 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
2377 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
2378 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
2379 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
2380 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
2381 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
2382 SQLITE_MAX_LIKE_PATTERN_LENGTH );
2383 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
2384 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
drh111544c2014-08-29 16:20:47 +00002385 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS );
2386 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
drh4e93f5b2010-09-07 14:59:15 +00002387
2388
drh521cc842008-04-15 02:36:33 +00002389 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
drhbb4957f2008-03-20 14:03:29 +00002390 return -1;
2391 }
2392 oldLimit = db->aLimit[limitId];
drh4e93f5b2010-09-07 14:59:15 +00002393 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
drhf47ce562008-03-20 18:00:49 +00002394 if( newLimit>aHardLimit[limitId] ){
drh4e93f5b2010-09-07 14:59:15 +00002395 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
drhbb4957f2008-03-20 14:03:29 +00002396 }
2397 db->aLimit[limitId] = newLimit;
2398 }
drh4e93f5b2010-09-07 14:59:15 +00002399 return oldLimit; /* IMP: R-53341-35419 */
drhbb4957f2008-03-20 14:03:29 +00002400}
2401
2402/*
dan286ab7c2011-05-06 18:34:54 +00002403** This function is used to parse both URIs and non-URI filenames passed by the
2404** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
2405** URIs specified as part of ATTACH statements.
2406**
2407** The first argument to this function is the name of the VFS to use (or
2408** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
2409** query parameter. The second argument contains the URI (or non-URI filename)
2410** itself. When this function is called the *pFlags variable should contain
2411** the default flags to open the database handle with. The value stored in
2412** *pFlags may be updated before returning if the URI filename contains
2413** "cache=xxx" or "mode=xxx" query parameters.
2414**
2415** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
2416** the VFS that should be used to open the database file. *pzFile is set to
2417** point to a buffer containing the name of the file to open. It is the
2418** responsibility of the caller to eventually call sqlite3_free() to release
2419** this buffer.
2420**
2421** If an error occurs, then an SQLite error code is returned and *pzErrMsg
2422** may be set to point to a buffer containing an English language error
2423** message. It is the responsibility of the caller to eventually release
2424** this buffer by calling sqlite3_free().
dancd74b612011-04-22 19:37:32 +00002425*/
2426int sqlite3ParseUri(
2427 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */
2428 const char *zUri, /* Nul-terminated URI to parse */
drh522c26f2011-05-07 14:40:29 +00002429 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */
dancd74b612011-04-22 19:37:32 +00002430 sqlite3_vfs **ppVfs, /* OUT: VFS to use */
2431 char **pzFile, /* OUT: Filename component of URI */
2432 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
2433){
dan78e9dd22011-05-03 10:22:32 +00002434 int rc = SQLITE_OK;
drh522c26f2011-05-07 14:40:29 +00002435 unsigned int flags = *pFlags;
dancd74b612011-04-22 19:37:32 +00002436 const char *zVfs = zDefaultVfs;
2437 char *zFile;
drhde941c32011-05-09 19:20:17 +00002438 char c;
dancd74b612011-04-22 19:37:32 +00002439 int nUri = sqlite3Strlen30(zUri);
2440
2441 assert( *pzErrMsg==0 );
2442
drh8790b6e2014-11-07 01:43:56 +00002443 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
2444 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
drh00729cb2014-10-04 11:59:33 +00002445 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
dancd74b612011-04-22 19:37:32 +00002446 ){
dan5de15372011-04-23 10:12:30 +00002447 char *zOpt;
dancd74b612011-04-22 19:37:32 +00002448 int eState; /* Parser state when parsing URI */
2449 int iIn; /* Input character index */
2450 int iOut = 0; /* Output character index */
drhf3cdcdc2015-04-29 16:50:28 +00002451 u64 nByte = nUri+2; /* Bytes of space to allocate */
dancd74b612011-04-22 19:37:32 +00002452
dan286ab7c2011-05-06 18:34:54 +00002453 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
2454 ** method that there may be extra parameters following the file-name. */
2455 flags |= SQLITE_OPEN_URI;
2456
2457 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
drhf3cdcdc2015-04-29 16:50:28 +00002458 zFile = sqlite3_malloc64(nByte);
dancd74b612011-04-22 19:37:32 +00002459 if( !zFile ) return SQLITE_NOMEM;
2460
drh869c0402013-08-07 23:15:52 +00002461 iIn = 5;
drh4a4b1382015-03-03 16:58:56 +00002462#ifdef SQLITE_ALLOW_URI_AUTHORITY
2463 if( strncmp(zUri+5, "///", 3)==0 ){
2464 iIn = 7;
2465 /* The following condition causes URIs with five leading / characters
2466 ** like file://///host/path to be converted into UNCs like //host/path.
2467 ** The correct URI for that UNC has only two or four leading / characters
2468 ** file://host/path or file:////host/path. But 5 leading slashes is a
2469 ** common error, we are told, so we handle it as a special case. */
2470 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; }
2471 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){
2472 iIn = 16;
2473 }
2474#else
dancd74b612011-04-22 19:37:32 +00002475 /* Discard the scheme and authority segments of the URI. */
2476 if( zUri[5]=='/' && zUri[6]=='/' ){
2477 iIn = 7;
2478 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
dan3b18a9a2011-05-03 11:53:20 +00002479 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
2480 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
2481 iIn-7, &zUri[7]);
2482 rc = SQLITE_ERROR;
2483 goto parse_uri_out;
2484 }
dancd74b612011-04-22 19:37:32 +00002485 }
drh869c0402013-08-07 23:15:52 +00002486#endif
dancd74b612011-04-22 19:37:32 +00002487
2488 /* Copy the filename and any query parameters into the zFile buffer.
2489 ** Decode %HH escape codes along the way.
2490 **
2491 ** Within this loop, variable eState may be set to 0, 1 or 2, depending
2492 ** on the parsing context. As follows:
2493 **
2494 ** 0: Parsing file-name.
2495 ** 1: Parsing name section of a name=value query parameter.
2496 ** 2: Parsing value section of a name=value query parameter.
2497 */
2498 eState = 0;
drhde941c32011-05-09 19:20:17 +00002499 while( (c = zUri[iIn])!=0 && c!='#' ){
2500 iIn++;
dancd74b612011-04-22 19:37:32 +00002501 if( c=='%'
2502 && sqlite3Isxdigit(zUri[iIn])
2503 && sqlite3Isxdigit(zUri[iIn+1])
2504 ){
dan6d49e252011-05-03 15:09:05 +00002505 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
2506 octet += sqlite3HexToInt(zUri[iIn++]);
dancd74b612011-04-22 19:37:32 +00002507
dan6d49e252011-05-03 15:09:05 +00002508 assert( octet>=0 && octet<256 );
2509 if( octet==0 ){
dan5de15372011-04-23 10:12:30 +00002510 /* This branch is taken when "%00" appears within the URI. In this
2511 ** case we ignore all text in the remainder of the path, name or
2512 ** value currently being parsed. So ignore the current character
2513 ** and skip to the next "?", "=" or "&", as appropriate. */
drhde941c32011-05-09 19:20:17 +00002514 while( (c = zUri[iIn])!=0 && c!='#'
2515 && (eState!=0 || c!='?')
2516 && (eState!=1 || (c!='=' && c!='&'))
2517 && (eState!=2 || c!='&')
dan5de15372011-04-23 10:12:30 +00002518 ){
2519 iIn++;
2520 }
2521 continue;
dancd74b612011-04-22 19:37:32 +00002522 }
dan6d49e252011-05-03 15:09:05 +00002523 c = octet;
dan5de15372011-04-23 10:12:30 +00002524 }else if( eState==1 && (c=='&' || c=='=') ){
2525 if( zFile[iOut-1]==0 ){
2526 /* An empty option name. Ignore this option altogether. */
2527 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
2528 continue;
2529 }
2530 if( c=='&' ){
2531 zFile[iOut++] = '\0';
2532 }else{
2533 eState = 2;
2534 }
dancd74b612011-04-22 19:37:32 +00002535 c = 0;
dan5de15372011-04-23 10:12:30 +00002536 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
2537 c = 0;
dancd74b612011-04-22 19:37:32 +00002538 eState = 1;
dancd74b612011-04-22 19:37:32 +00002539 }
2540 zFile[iOut++] = c;
2541 }
2542 if( eState==1 ) zFile[iOut++] = '\0';
2543 zFile[iOut++] = '\0';
2544 zFile[iOut++] = '\0';
2545
2546 /* Check if there were any options specified that should be interpreted
2547 ** here. Options that are interpreted here include "vfs" and those that
2548 ** correspond to flags that may be passed to the sqlite3_open_v2()
dan78e9dd22011-05-03 10:22:32 +00002549 ** method. */
dan5de15372011-04-23 10:12:30 +00002550 zOpt = &zFile[sqlite3Strlen30(zFile)+1];
2551 while( zOpt[0] ){
2552 int nOpt = sqlite3Strlen30(zOpt);
2553 char *zVal = &zOpt[nOpt+1];
2554 int nVal = sqlite3Strlen30(zVal);
dancd74b612011-04-22 19:37:32 +00002555
dan286ab7c2011-05-06 18:34:54 +00002556 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
dan78e9dd22011-05-03 10:22:32 +00002557 zVfs = zVal;
2558 }else{
2559 struct OpenMode {
2560 const char *z;
2561 int mode;
2562 } *aMode = 0;
drh45caede2011-06-03 14:19:10 +00002563 char *zModeType = 0;
2564 int mask = 0;
2565 int limit = 0;
dan78e9dd22011-05-03 10:22:32 +00002566
dan286ab7c2011-05-06 18:34:54 +00002567 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
dan78e9dd22011-05-03 10:22:32 +00002568 static struct OpenMode aCacheMode[] = {
2569 { "shared", SQLITE_OPEN_SHAREDCACHE },
2570 { "private", SQLITE_OPEN_PRIVATECACHE },
2571 { 0, 0 }
2572 };
2573
2574 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
2575 aMode = aCacheMode;
2576 limit = mask;
2577 zModeType = "cache";
2578 }
dan286ab7c2011-05-06 18:34:54 +00002579 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
dan78e9dd22011-05-03 10:22:32 +00002580 static struct OpenMode aOpenMode[] = {
2581 { "ro", SQLITE_OPEN_READONLY },
2582 { "rw", SQLITE_OPEN_READWRITE },
2583 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
dan0b8dcfa2012-06-07 17:16:04 +00002584 { "memory", SQLITE_OPEN_MEMORY },
dan78e9dd22011-05-03 10:22:32 +00002585 { 0, 0 }
2586 };
2587
drh9c67b2a2012-05-28 13:58:00 +00002588 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
2589 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
dan78e9dd22011-05-03 10:22:32 +00002590 aMode = aOpenMode;
2591 limit = mask & flags;
2592 zModeType = "access";
2593 }
2594
2595 if( aMode ){
2596 int i;
2597 int mode = 0;
2598 for(i=0; aMode[i].z; i++){
2599 const char *z = aMode[i].z;
drh522c26f2011-05-07 14:40:29 +00002600 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
dan78e9dd22011-05-03 10:22:32 +00002601 mode = aMode[i].mode;
2602 break;
dancd74b612011-04-22 19:37:32 +00002603 }
2604 }
dan78e9dd22011-05-03 10:22:32 +00002605 if( mode==0 ){
2606 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
2607 rc = SQLITE_ERROR;
2608 goto parse_uri_out;
2609 }
drh9c67b2a2012-05-28 13:58:00 +00002610 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
drhde941c32011-05-09 19:20:17 +00002611 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
2612 zModeType, zVal);
dan78e9dd22011-05-03 10:22:32 +00002613 rc = SQLITE_PERM;
2614 goto parse_uri_out;
2615 }
2616 flags = (flags & ~mask) | mode;
dancd74b612011-04-22 19:37:32 +00002617 }
dancd74b612011-04-22 19:37:32 +00002618 }
dan5de15372011-04-23 10:12:30 +00002619
2620 zOpt = &zVal[nVal+1];
dancd74b612011-04-22 19:37:32 +00002621 }
2622
2623 }else{
drhf3cdcdc2015-04-29 16:50:28 +00002624 zFile = sqlite3_malloc64(nUri+2);
dancd74b612011-04-22 19:37:32 +00002625 if( !zFile ) return SQLITE_NOMEM;
2626 memcpy(zFile, zUri, nUri);
2627 zFile[nUri] = '\0';
2628 zFile[nUri+1] = '\0';
drh4ab9d252012-05-26 20:08:49 +00002629 flags &= ~SQLITE_OPEN_URI;
dancd74b612011-04-22 19:37:32 +00002630 }
2631
2632 *ppVfs = sqlite3_vfs_find(zVfs);
2633 if( *ppVfs==0 ){
dancd74b612011-04-22 19:37:32 +00002634 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
dan78e9dd22011-05-03 10:22:32 +00002635 rc = SQLITE_ERROR;
2636 }
2637 parse_uri_out:
2638 if( rc!=SQLITE_OK ){
dan5de15372011-04-23 10:12:30 +00002639 sqlite3_free(zFile);
dan78e9dd22011-05-03 10:22:32 +00002640 zFile = 0;
dancd74b612011-04-22 19:37:32 +00002641 }
2642 *pFlags = flags;
2643 *pzFile = zFile;
dan78e9dd22011-05-03 10:22:32 +00002644 return rc;
dancd74b612011-04-22 19:37:32 +00002645}
2646
2647
2648/*
danielk19774ad17132004-05-21 01:47:26 +00002649** This routine does the work of opening a database on behalf of
2650** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +00002651** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +00002652*/
2653static int openDatabase(
2654 const char *zFilename, /* Database filename UTF-8 encoded */
drh605264d2007-08-21 15:13:19 +00002655 sqlite3 **ppDb, /* OUT: Returned database handle */
drh522c26f2011-05-07 14:40:29 +00002656 unsigned int flags, /* Operational flags */
drh605264d2007-08-21 15:13:19 +00002657 const char *zVfs /* Name of the VFS to use */
danielk19774ad17132004-05-21 01:47:26 +00002658){
dancd74b612011-04-22 19:37:32 +00002659 sqlite3 *db; /* Store allocated handle here */
2660 int rc; /* Return code */
2661 int isThreadsafe; /* True for threadsafe connections */
2662 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */
2663 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
danielk19774ad17132004-05-21 01:47:26 +00002664
drh9ca95732014-10-24 00:35:58 +00002665#ifdef SQLITE_ENABLE_API_ARMOR
2666 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
2667#endif
drhe0d0f8e2009-04-28 04:47:31 +00002668 *ppDb = 0;
drh40257ff2008-06-13 18:24:27 +00002669#ifndef SQLITE_OMIT_AUTOINIT
2670 rc = sqlite3_initialize();
2671 if( rc ) return rc;
2672#endif
2673
dan78e9dd22011-05-03 10:22:32 +00002674 /* Only allow sensible combinations of bits in the flags argument.
2675 ** Throw an error if any non-sense combination is used. If we
2676 ** do not block illegal combinations here, it could trigger
2677 ** assert() statements in deeper layers. Sensible combinations
2678 ** are:
2679 **
2680 ** 1: SQLITE_OPEN_READONLY
2681 ** 2: SQLITE_OPEN_READWRITE
2682 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
2683 */
2684 assert( SQLITE_OPEN_READONLY == 0x01 );
2685 assert( SQLITE_OPEN_READWRITE == 0x02 );
2686 assert( SQLITE_OPEN_CREATE == 0x04 );
2687 testcase( (1<<(flags&7))==0x02 ); /* READONLY */
2688 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
2689 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
drh00729cb2014-10-04 11:59:33 +00002690 if( ((1<<(flags&7)) & 0x46)==0 ){
2691 return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */
2692 }
dan78e9dd22011-05-03 10:22:32 +00002693
drh039963a2008-09-03 00:43:15 +00002694 if( sqlite3GlobalConfig.bCoreMutex==0 ){
danielk19779a6284c2008-07-10 17:52:49 +00002695 isThreadsafe = 0;
drh039963a2008-09-03 00:43:15 +00002696 }else if( flags & SQLITE_OPEN_NOMUTEX ){
2697 isThreadsafe = 0;
2698 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
2699 isThreadsafe = 1;
2700 }else{
2701 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
danielk19779a6284c2008-07-10 17:52:49 +00002702 }
drhf1f12682009-09-09 14:17:52 +00002703 if( flags & SQLITE_OPEN_PRIVATECACHE ){
drhf4cfac92009-09-09 15:29:41 +00002704 flags &= ~SQLITE_OPEN_SHAREDCACHE;
drhf1f12682009-09-09 14:17:52 +00002705 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
2706 flags |= SQLITE_OPEN_SHAREDCACHE;
2707 }
danielk19779a6284c2008-07-10 17:52:49 +00002708
drhb25a9f42009-05-12 13:35:11 +00002709 /* Remove harmful bits from the flags parameter
2710 **
2711 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
2712 ** dealt with in the previous code block. Besides these, the only
2713 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
drh03e1b402011-02-23 22:39:23 +00002714 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
2715 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask
drhb25a9f42009-05-12 13:35:11 +00002716 ** off all other flags.
2717 */
drha4267dc2008-02-19 15:20:44 +00002718 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
drhb25a9f42009-05-12 13:35:11 +00002719 SQLITE_OPEN_EXCLUSIVE |
drha4267dc2008-02-19 15:20:44 +00002720 SQLITE_OPEN_MAIN_DB |
2721 SQLITE_OPEN_TEMP_DB |
2722 SQLITE_OPEN_TRANSIENT_DB |
2723 SQLITE_OPEN_MAIN_JOURNAL |
2724 SQLITE_OPEN_TEMP_JOURNAL |
2725 SQLITE_OPEN_SUBJOURNAL |
danielk19779a6284c2008-07-10 17:52:49 +00002726 SQLITE_OPEN_MASTER_JOURNAL |
drh039963a2008-09-03 00:43:15 +00002727 SQLITE_OPEN_NOMUTEX |
drh357b5f92010-08-24 18:07:57 +00002728 SQLITE_OPEN_FULLMUTEX |
2729 SQLITE_OPEN_WAL
drha4267dc2008-02-19 15:20:44 +00002730 );
2731
danielk19774ad17132004-05-21 01:47:26 +00002732 /* Allocate the sqlite data structure */
drh17435752007-08-16 04:30:38 +00002733 db = sqlite3MallocZero( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +00002734 if( db==0 ) goto opendb_out;
drh039963a2008-09-03 00:43:15 +00002735 if( isThreadsafe ){
danielk197759f8c082008-06-18 17:09:10 +00002736 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
2737 if( db->mutex==0 ){
2738 sqlite3_free(db);
2739 db = 0;
2740 goto opendb_out;
2741 }
drh605264d2007-08-21 15:13:19 +00002742 }
drh27641702007-08-22 02:56:42 +00002743 sqlite3_mutex_enter(db->mutex);
drh4ac285a2006-09-15 07:28:50 +00002744 db->errMask = 0xff;
danielk19774ad17132004-05-21 01:47:26 +00002745 db->nDb = 2;
drh153c62c2007-08-24 03:51:33 +00002746 db->magic = SQLITE_MAGIC_BUSY;
danielk19774ad17132004-05-21 01:47:26 +00002747 db->aDb = db->aDbStatic;
drh633e6d52008-07-28 19:34:53 +00002748
drhbb4957f2008-03-20 14:03:29 +00002749 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
2750 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
drh6b2129a2014-08-29 19:06:07 +00002751 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
danielk19771d850a72004-05-31 08:26:49 +00002752 db->autoCommit = 1;
drhddac25c2007-12-05 01:38:23 +00002753 db->nextAutovac = -1;
drh9b4c59f2013-04-15 17:03:42 +00002754 db->szMmap = sqlite3GlobalConfig.szMmap;
danielk1977f653d782008-03-20 11:04:21 +00002755 db->nextPagesize = 0;
dan8930c2a2014-04-03 16:25:29 +00002756 db->nMaxSorterMmap = 0x7FFFFFFF;
drh40c39412013-08-16 20:42:20 +00002757 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
dan1d1f07d2013-07-25 16:41:39 +00002758#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
drh986b3872013-06-28 21:12:20 +00002759 | SQLITE_AutoIndex
2760#endif
drh883ad042015-02-19 00:29:11 +00002761#if SQLITE_DEFAULT_CKPTFULLFSYNC
2762 | SQLITE_CkptFullFSync
2763#endif
drh76fe8032006-07-11 14:17:51 +00002764#if SQLITE_DEFAULT_FILE_FORMAT<4
2765 | SQLITE_LegacyFileFmt
2766#endif
drh56424db2007-03-27 22:24:11 +00002767#ifdef SQLITE_ENABLE_LOAD_EXTENSION
2768 | SQLITE_LoadExtension
2769#endif
dan5bde73c2009-09-01 17:11:07 +00002770#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
dan5e1a2782009-09-01 17:28:29 +00002771 | SQLITE_RecTriggers
dan76d462e2009-08-30 11:42:51 +00002772#endif
drha4bfd7f2010-12-09 19:15:17 +00002773#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
2774 | SQLITE_ForeignKeys
2775#endif
drhf5471922014-12-09 15:12:11 +00002776#if defined(SQLITE_REVERSE_UNORDERED_SELECTS)
2777 | SQLITE_ReverseOrder
2778#endif
drh1421d982015-05-27 03:46:18 +00002779#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
2780 | SQLITE_CellSizeCk
2781#endif
drh76fe8032006-07-11 14:17:51 +00002782 ;
drhe61922a2009-05-02 13:29:37 +00002783 sqlite3HashInit(&db->aCollSeq);
drhb9bb7c12006-06-11 23:41:55 +00002784#ifndef SQLITE_OMIT_VIRTUALTABLE
drhe61922a2009-05-02 13:29:37 +00002785 sqlite3HashInit(&db->aModule);
drhb9bb7c12006-06-11 23:41:55 +00002786#endif
danielk1977da184232006-01-05 11:34:32 +00002787
danielk19770202b292004-06-09 09:55:16 +00002788 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
2789 ** and UTF-16, so add a version for each to avoid any unnecessary
2790 ** conversions. The only error that can occur here is a malloc() failure.
drh5e3b49b2014-11-20 19:22:26 +00002791 **
2792 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
2793 ** functions:
danielk19770202b292004-06-09 09:55:16 +00002794 */
drh4dd65e02011-11-29 14:46:56 +00002795 createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
2796 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
2797 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
drh5e3b49b2014-11-20 19:22:26 +00002798 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
drh4dd65e02011-11-29 14:46:56 +00002799 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
drh77db4c02008-03-18 13:01:38 +00002800 if( db->mallocFailed ){
danielk19770202b292004-06-09 09:55:16 +00002801 goto opendb_out;
2802 }
drh5e3b49b2014-11-20 19:22:26 +00002803 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
2804 ** strings is BINARY.
2805 */
drhc4a64fa2009-05-11 20:53:28 +00002806 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
drh77db4c02008-03-18 13:01:38 +00002807 assert( db->pDfltColl!=0 );
danielk19770202b292004-06-09 09:55:16 +00002808
dancd74b612011-04-22 19:37:32 +00002809 /* Parse the filename/URI argument. */
dan19432992011-05-10 18:39:10 +00002810 db->openFlags = flags;
dancd74b612011-04-22 19:37:32 +00002811 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
2812 if( rc!=SQLITE_OK ){
drhffd96682011-05-07 18:40:36 +00002813 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
drh13f40da2014-08-22 18:00:11 +00002814 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
dancd74b612011-04-22 19:37:32 +00002815 sqlite3_free(zErrMsg);
2816 goto opendb_out;
2817 }
2818
danielk19774ad17132004-05-21 01:47:26 +00002819 /* Open the backend database driver */
dan3a6d8ae2011-04-23 15:54:54 +00002820 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
drh75c014c2010-08-30 15:02:28 +00002821 flags | SQLITE_OPEN_MAIN_DB);
danielk19774ad17132004-05-21 01:47:26 +00002822 if( rc!=SQLITE_OK ){
danielk197745e60aa2008-09-23 17:39:26 +00002823 if( rc==SQLITE_IOERR_NOMEM ){
2824 rc = SQLITE_NOMEM;
2825 }
drh13f40da2014-08-22 18:00:11 +00002826 sqlite3Error(db, rc);
danielk19774ad17132004-05-21 01:47:26 +00002827 goto opendb_out;
2828 }
dan0235a032014-12-08 20:20:16 +00002829 sqlite3BtreeEnter(db->aDb[0].pBt);
drh17435752007-08-16 04:30:38 +00002830 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
drh9bd3cc42014-12-12 23:17:54 +00002831 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
dan0235a032014-12-08 20:20:16 +00002832 sqlite3BtreeLeave(db->aDb[0].pBt);
drh17435752007-08-16 04:30:38 +00002833 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
danielk197714db2662006-01-09 16:12:04 +00002834
danielk197753c0f742005-03-29 03:10:59 +00002835 /* The default safety_level for the main database is 'full'; for the temp
2836 ** database it is 'NONE'. This matches the pager layer defaults.
2837 */
2838 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +00002839 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +00002840 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +00002841 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +00002842
danielk1977832a58a2007-06-22 15:21:15 +00002843 db->magic = SQLITE_MAGIC_OPEN;
drh17435752007-08-16 04:30:38 +00002844 if( db->mallocFailed ){
danielk1977832a58a2007-06-22 15:21:15 +00002845 goto opendb_out;
2846 }
2847
danielk19778e227872004-06-07 07:52:17 +00002848 /* Register all built-in functions, but do not attempt to read the
2849 ** database schema yet. This is delayed until the first time the database
2850 ** is accessed.
2851 */
drh13f40da2014-08-22 18:00:11 +00002852 sqlite3Error(db, SQLITE_OK);
danielk1977832a58a2007-06-22 15:21:15 +00002853 sqlite3RegisterBuiltinFunctions(db);
danielk19774ad17132004-05-21 01:47:26 +00002854
drh1409be62006-08-23 20:07:20 +00002855 /* Load automatic extensions - extensions that have been registered
2856 ** using the sqlite3_automatic_extension() API.
2857 */
drh7aaa8782009-05-20 02:40:45 +00002858 rc = sqlite3_errcode(db);
drhe5077c12011-12-13 04:08:36 +00002859 if( rc==SQLITE_OK ){
2860 sqlite3AutoLoadExtensions(db);
2861 rc = sqlite3_errcode(db);
2862 if( rc!=SQLITE_OK ){
2863 goto opendb_out;
2864 }
danielk1977832a58a2007-06-22 15:21:15 +00002865 }
drh1409be62006-08-23 20:07:20 +00002866
drhaa29c132006-09-02 13:58:06 +00002867#ifdef SQLITE_ENABLE_FTS1
drh17435752007-08-16 04:30:38 +00002868 if( !db->mallocFailed ){
drhaa29c132006-09-02 13:58:06 +00002869 extern int sqlite3Fts1Init(sqlite3*);
danielk1977832a58a2007-06-22 15:21:15 +00002870 rc = sqlite3Fts1Init(db);
drhaa29c132006-09-02 13:58:06 +00002871 }
2872#endif
2873
shessa26cf572006-10-19 20:27:58 +00002874#ifdef SQLITE_ENABLE_FTS2
drh17435752007-08-16 04:30:38 +00002875 if( !db->mallocFailed && rc==SQLITE_OK ){
shessa26cf572006-10-19 20:27:58 +00002876 extern int sqlite3Fts2Init(sqlite3*);
danielk1977832a58a2007-06-22 15:21:15 +00002877 rc = sqlite3Fts2Init(db);
shessa26cf572006-10-19 20:27:58 +00002878 }
2879#endif
2880
drh50065652015-10-08 19:29:18 +00002881#ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */
shess69c4ae22007-08-20 17:37:47 +00002882 if( !db->mallocFailed && rc==SQLITE_OK ){
shess69c4ae22007-08-20 17:37:47 +00002883 rc = sqlite3Fts3Init(db);
2884 }
2885#endif
2886
drh50065652015-10-08 19:29:18 +00002887#ifdef SQLITE_ENABLE_FTS5
2888 if( !db->mallocFailed && rc==SQLITE_OK ){
drh50065652015-10-08 19:29:18 +00002889 rc = sqlite3Fts5Init(db);
2890 }
2891#endif
2892
danielk197783852ac2007-05-06 16:04:11 +00002893#ifdef SQLITE_ENABLE_ICU
drh17435752007-08-16 04:30:38 +00002894 if( !db->mallocFailed && rc==SQLITE_OK ){
danielk1977832a58a2007-06-22 15:21:15 +00002895 rc = sqlite3IcuInit(db);
danielk197783852ac2007-05-06 16:04:11 +00002896 }
2897#endif
danielk1977ebaecc12008-05-26 18:41:54 +00002898
2899#ifdef SQLITE_ENABLE_RTREE
2900 if( !db->mallocFailed && rc==SQLITE_OK){
danielk1977ebaecc12008-05-26 18:41:54 +00002901 rc = sqlite3RtreeInit(db);
2902 }
2903#endif
2904
drha3ab9d02015-05-04 20:25:05 +00002905#ifdef SQLITE_ENABLE_DBSTAT_VTAB
2906 if( !db->mallocFailed && rc==SQLITE_OK){
drh3e0327d2015-05-11 11:59:15 +00002907 rc = sqlite3DbstatRegister(db);
drha3ab9d02015-05-04 20:25:05 +00002908 }
2909#endif
2910
drh2f20e132015-09-26 17:44:59 +00002911#ifdef SQLITE_ENABLE_JSON1
2912 if( !db->mallocFailed && rc==SQLITE_OK){
drh2f20e132015-09-26 17:44:59 +00002913 rc = sqlite3Json1Init(db);
2914 }
2915#endif
2916
drh8fd5bd32007-04-02 16:40:37 +00002917 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
2918 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
2919 ** mode. Doing nothing at all also makes NORMAL the default.
2920 */
2921#ifdef SQLITE_DEFAULT_LOCKING_MODE
2922 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
2923 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
2924 SQLITE_DEFAULT_LOCKING_MODE);
2925#endif
2926
drh13f40da2014-08-22 18:00:11 +00002927 if( rc ) sqlite3Error(db, rc);
drhf6b1a8e2013-12-19 16:26:05 +00002928
drh633e6d52008-07-28 19:34:53 +00002929 /* Enable the lookaside-malloc subsystem */
drh1b67f3c2008-10-10 17:41:28 +00002930 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
2931 sqlite3GlobalConfig.nLookaside);
drh633e6d52008-07-28 19:34:53 +00002932
dan5a299f92010-05-03 11:05:08 +00002933 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
dan586b9c82010-05-03 08:04:49 +00002934
danielk19774ad17132004-05-21 01:47:26 +00002935opendb_out:
dancd74b612011-04-22 19:37:32 +00002936 sqlite3_free(zOpen);
drhafc91042008-02-21 02:09:45 +00002937 if( db ){
drh3ba689d2015-03-03 14:00:11 +00002938 assert( db->mutex!=0 || isThreadsafe==0
2939 || sqlite3GlobalConfig.bFullMutex==0 );
drhf3a65f72007-08-22 20:18:21 +00002940 sqlite3_mutex_leave(db->mutex);
2941 }
danielk197759633ae2008-08-27 19:01:57 +00002942 rc = sqlite3_errcode(db);
drhb07028f2011-10-14 21:49:18 +00002943 assert( db!=0 || rc==SQLITE_NOMEM );
danielk197759633ae2008-08-27 19:01:57 +00002944 if( rc==SQLITE_NOMEM ){
danielk19777ddad962005-12-12 06:53:03 +00002945 sqlite3_close(db);
2946 db = 0;
danielk197759633ae2008-08-27 19:01:57 +00002947 }else if( rc!=SQLITE_OK ){
2948 db->magic = SQLITE_MAGIC_SICK;
danielk197713073932004-06-30 11:54:06 +00002949 }
danielk19774ad17132004-05-21 01:47:26 +00002950 *ppDb = db;
danac455932012-11-26 19:50:41 +00002951#ifdef SQLITE_ENABLE_SQLLOG
2952 if( sqlite3GlobalConfig.xSqllog ){
dan71ba10d2012-11-27 10:56:39 +00002953 /* Opening a db handle. Fourth parameter is passed 0. */
2954 void *pArg = sqlite3GlobalConfig.pSqllogArg;
2955 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
danac455932012-11-26 19:50:41 +00002956 }
2957#endif
drh597d2b62015-06-30 03:13:47 +00002958 return rc & 0xff;
danielk19774ad17132004-05-21 01:47:26 +00002959}
2960
2961/*
2962** Open a new database handle.
2963*/
danielk197780290862004-05-22 09:21:21 +00002964int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00002965 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00002966 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00002967){
drh605264d2007-08-21 15:13:19 +00002968 return openDatabase(zFilename, ppDb,
2969 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
2970}
2971int sqlite3_open_v2(
drh428e2822007-08-30 16:23:19 +00002972 const char *filename, /* Database filename (UTF-8) */
drh605264d2007-08-21 15:13:19 +00002973 sqlite3 **ppDb, /* OUT: SQLite db handle */
2974 int flags, /* Flags */
2975 const char *zVfs /* Name of VFS module to use */
2976){
drh522c26f2011-05-07 14:40:29 +00002977 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
danielk197783ab5a82004-05-21 11:39:05 +00002978}
2979
drh6c626082004-11-14 21:56:29 +00002980#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00002981/*
2982** Open a new database handle.
2983*/
2984int sqlite3_open16(
2985 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00002986 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00002987){
danielk1977bfd6cce2004-06-18 04:24:54 +00002988 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
danielk1977bfd6cce2004-06-18 04:24:54 +00002989 sqlite3_value *pVal;
drh40257ff2008-06-13 18:24:27 +00002990 int rc;
danielk19774ad17132004-05-21 01:47:26 +00002991
drh9ca95732014-10-24 00:35:58 +00002992#ifdef SQLITE_ENABLE_API_ARMOR
2993 if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
2994#endif
danielk1977bfd6cce2004-06-18 04:24:54 +00002995 *ppDb = 0;
drh40257ff2008-06-13 18:24:27 +00002996#ifndef SQLITE_OMIT_AUTOINIT
2997 rc = sqlite3_initialize();
2998 if( rc ) return rc;
2999#endif
drh9ca95732014-10-24 00:35:58 +00003000 if( zFilename==0 ) zFilename = "\000\000";
danielk19771e536952007-08-16 10:09:01 +00003001 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00003002 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
3003 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
danielk1977bfd6cce2004-06-18 04:24:54 +00003004 if( zFilename8 ){
drh605264d2007-08-21 15:13:19 +00003005 rc = openDatabase(zFilename8, ppDb,
3006 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
drhafc91042008-02-21 02:09:45 +00003007 assert( *ppDb || rc==SQLITE_NOMEM );
danielk1977f51bf482008-04-28 16:19:35 +00003008 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
drh9bd3cc42014-12-12 23:17:54 +00003009 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE;
danielk1977bfd6cce2004-06-18 04:24:54 +00003010 }
drh40257ff2008-06-13 18:24:27 +00003011 }else{
3012 rc = SQLITE_NOMEM;
danielk19774ad17132004-05-21 01:47:26 +00003013 }
danielk19777ddad962005-12-12 06:53:03 +00003014 sqlite3ValueFree(pVal);
danielk19778e227872004-06-07 07:52:17 +00003015
drh597d2b62015-06-30 03:13:47 +00003016 return rc & 0xff;
danielk19774ad17132004-05-21 01:47:26 +00003017}
drh6c626082004-11-14 21:56:29 +00003018#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00003019
danielk1977106bb232004-05-21 10:08:53 +00003020/*
danielk1977d8123362004-06-12 09:25:12 +00003021** Register a new collation sequence with the database handle db.
3022*/
danielk19770202b292004-06-09 09:55:16 +00003023int sqlite3_create_collation(
3024 sqlite3* db,
3025 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00003026 int enc,
danielk19770202b292004-06-09 09:55:16 +00003027 void* pCtx,
3028 int(*xCompare)(void*,int,const void*,int,const void*)
3029){
drh4f81bbb2014-10-23 01:01:26 +00003030 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0);
danielk1977a9808b32007-05-07 09:32:45 +00003031}
3032
3033/*
3034** Register a new collation sequence with the database handle db.
3035*/
danielk1977a393c032007-05-07 14:58:53 +00003036int sqlite3_create_collation_v2(
danielk1977a9808b32007-05-07 09:32:45 +00003037 sqlite3* db,
3038 const char *zName,
3039 int enc,
3040 void* pCtx,
3041 int(*xCompare)(void*,int,const void*,int,const void*),
3042 void(*xDel)(void*)
3043){
3044 int rc;
drh9ca95732014-10-24 00:35:58 +00003045
3046#ifdef SQLITE_ENABLE_API_ARMOR
3047 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
3048#endif
drhe30f4422007-08-21 16:15:55 +00003049 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:38 +00003050 assert( !db->mallocFailed );
drh4dd65e02011-11-29 14:46:56 +00003051 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
drhe30f4422007-08-21 16:15:55 +00003052 rc = sqlite3ApiExit(db, rc);
3053 sqlite3_mutex_leave(db->mutex);
3054 return rc;
danielk19770202b292004-06-09 09:55:16 +00003055}
3056
drh6c626082004-11-14 21:56:29 +00003057#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00003058/*
3059** Register a new collation sequence with the database handle db.
3060*/
danielk19770202b292004-06-09 09:55:16 +00003061int sqlite3_create_collation16(
3062 sqlite3* db,
mihailimbda2e622008-06-23 11:23:14 +00003063 const void *zName,
danielk1977466be562004-06-10 02:16:01 +00003064 int enc,
danielk19770202b292004-06-09 09:55:16 +00003065 void* pCtx,
3066 int(*xCompare)(void*,int,const void*,int,const void*)
3067){
danielk19779a30cf62006-01-18 04:26:07 +00003068 int rc = SQLITE_OK;
drh01495b92008-01-23 12:52:40 +00003069 char *zName8;
drh9ca95732014-10-24 00:35:58 +00003070
3071#ifdef SQLITE_ENABLE_API_ARMOR
3072 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT;
3073#endif
drhe30f4422007-08-21 16:15:55 +00003074 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:38 +00003075 assert( !db->mallocFailed );
danb7dca7d2010-03-05 16:32:12 +00003076 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
danielk19779a30cf62006-01-18 04:26:07 +00003077 if( zName8 ){
drh4dd65e02011-11-29 14:46:56 +00003078 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
drh633e6d52008-07-28 19:34:53 +00003079 sqlite3DbFree(db, zName8);
danielk19779a30cf62006-01-18 04:26:07 +00003080 }
drhe30f4422007-08-21 16:15:55 +00003081 rc = sqlite3ApiExit(db, rc);
3082 sqlite3_mutex_leave(db->mutex);
3083 return rc;
danielk19770202b292004-06-09 09:55:16 +00003084}
drh6c626082004-11-14 21:56:29 +00003085#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00003086
danielk1977d8123362004-06-12 09:25:12 +00003087/*
3088** Register a collation sequence factory callback with the database handle
3089** db. Replace any previously installed collation sequence factory.
3090*/
danielk19777cedc8d2004-06-10 10:50:08 +00003091int sqlite3_collation_needed(
3092 sqlite3 *db,
3093 void *pCollNeededArg,
3094 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
3095){
drh9ca95732014-10-24 00:35:58 +00003096#ifdef SQLITE_ENABLE_API_ARMOR
3097 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3098#endif
drhe30f4422007-08-21 16:15:55 +00003099 sqlite3_mutex_enter(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00003100 db->xCollNeeded = xCollNeeded;
3101 db->xCollNeeded16 = 0;
3102 db->pCollNeededArg = pCollNeededArg;
drhe30f4422007-08-21 16:15:55 +00003103 sqlite3_mutex_leave(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00003104 return SQLITE_OK;
3105}
danielk1977d8123362004-06-12 09:25:12 +00003106
drh6c626082004-11-14 21:56:29 +00003107#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00003108/*
3109** Register a collation sequence factory callback with the database handle
3110** db. Replace any previously installed collation sequence factory.
3111*/
danielk19777cedc8d2004-06-10 10:50:08 +00003112int sqlite3_collation_needed16(
3113 sqlite3 *db,
3114 void *pCollNeededArg,
3115 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
3116){
drh9ca95732014-10-24 00:35:58 +00003117#ifdef SQLITE_ENABLE_API_ARMOR
3118 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3119#endif
drhe30f4422007-08-21 16:15:55 +00003120 sqlite3_mutex_enter(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00003121 db->xCollNeeded = 0;
3122 db->xCollNeeded16 = xCollNeeded16;
3123 db->pCollNeededArg = pCollNeededArg;
drhe30f4422007-08-21 16:15:55 +00003124 sqlite3_mutex_leave(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00003125 return SQLITE_OK;
3126}
drh6c626082004-11-14 21:56:29 +00003127#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00003128
shaneeec556d2008-10-12 00:27:53 +00003129#ifndef SQLITE_OMIT_DEPRECATED
danielk19776b456a22005-03-21 04:04:02 +00003130/*
danielk19777ddad962005-12-12 06:53:03 +00003131** This function is now an anachronism. It used to be used to recover from a
3132** malloc() failure, but SQLite now does this automatically.
danielk19776b456a22005-03-21 04:04:02 +00003133*/
drh7d97efb2007-10-12 19:35:48 +00003134int sqlite3_global_recover(void){
danielk1977261919c2005-12-06 12:52:59 +00003135 return SQLITE_OK;
danielk19776b456a22005-03-21 04:04:02 +00003136}
3137#endif
drh3e1d8e62005-05-26 16:23:34 +00003138
3139/*
3140** Test to see whether or not the database connection is in autocommit
3141** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
3142** by default. Autocommit is disabled by a BEGIN statement and reenabled
3143** by the next COMMIT or ROLLBACK.
drh3e1d8e62005-05-26 16:23:34 +00003144*/
3145int sqlite3_get_autocommit(sqlite3 *db){
drh9ca95732014-10-24 00:35:58 +00003146#ifdef SQLITE_ENABLE_API_ARMOR
3147 if( !sqlite3SafetyCheckOk(db) ){
3148 (void)SQLITE_MISUSE_BKPT;
3149 return 0;
3150 }
3151#endif
drh3e1d8e62005-05-26 16:23:34 +00003152 return db->autoCommit;
3153}
drh49285702005-09-17 15:20:26 +00003154
drh49285702005-09-17 15:20:26 +00003155/*
peter.d.reid60ec9142014-09-06 16:39:46 +00003156** The following routines are substitutes for constants SQLITE_CORRUPT,
drh9978c972010-02-23 17:36:32 +00003157** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
peter.d.reid60ec9142014-09-06 16:39:46 +00003158** constants. They serve two purposes:
drh9978c972010-02-23 17:36:32 +00003159**
3160** 1. Serve as a convenient place to set a breakpoint in a debugger
3161** to detect when version error conditions occurs.
3162**
3163** 2. Invoke sqlite3_log() to provide the source code location where
3164** a low-level error is first detected.
drh49285702005-09-17 15:20:26 +00003165*/
drh9978c972010-02-23 17:36:32 +00003166int sqlite3CorruptError(int lineno){
drhaf46dc12010-02-24 21:44:07 +00003167 testcase( sqlite3GlobalConfig.xLog!=0 );
drh9978c972010-02-23 17:36:32 +00003168 sqlite3_log(SQLITE_CORRUPT,
drh75e876b2010-06-23 17:59:51 +00003169 "database corruption at line %d of [%.10s]",
3170 lineno, 20+sqlite3_sourceid());
drh49285702005-09-17 15:20:26 +00003171 return SQLITE_CORRUPT;
3172}
drh9978c972010-02-23 17:36:32 +00003173int sqlite3MisuseError(int lineno){
drhaf46dc12010-02-24 21:44:07 +00003174 testcase( sqlite3GlobalConfig.xLog!=0 );
drh75e876b2010-06-23 17:59:51 +00003175 sqlite3_log(SQLITE_MISUSE,
3176 "misuse at line %d of [%.10s]",
3177 lineno, 20+sqlite3_sourceid());
drh9978c972010-02-23 17:36:32 +00003178 return SQLITE_MISUSE;
3179}
3180int sqlite3CantopenError(int lineno){
drhaf46dc12010-02-24 21:44:07 +00003181 testcase( sqlite3GlobalConfig.xLog!=0 );
drh75e876b2010-06-23 17:59:51 +00003182 sqlite3_log(SQLITE_CANTOPEN,
3183 "cannot open file at line %d of [%.10s]",
3184 lineno, 20+sqlite3_sourceid());
drh9978c972010-02-23 17:36:32 +00003185 return SQLITE_CANTOPEN;
3186}
3187
drh7c1817e2006-01-10 13:58:48 +00003188
shaneeec556d2008-10-12 00:27:53 +00003189#ifndef SQLITE_OMIT_DEPRECATED
drh6f7adc82006-01-11 21:41:20 +00003190/*
3191** This is a convenience routine that makes sure that all thread-specific
3192** data for this thread has been deallocated.
drhdce8bdb2007-08-16 13:01:44 +00003193**
3194** SQLite no longer uses thread-specific data so this routine is now a
3195** no-op. It is retained for historical compatibility.
drh6f7adc82006-01-11 21:41:20 +00003196*/
3197void sqlite3_thread_cleanup(void){
drh6f7adc82006-01-11 21:41:20 +00003198}
shaneeec556d2008-10-12 00:27:53 +00003199#endif
danielk1977deb802c2006-02-09 13:43:28 +00003200
3201/*
3202** Return meta information about a specific column of a database table.
3203** See comment in sqlite3.h (sqlite.h.in) for details.
3204*/
danielk1977deb802c2006-02-09 13:43:28 +00003205int sqlite3_table_column_metadata(
3206 sqlite3 *db, /* Connection handle */
3207 const char *zDbName, /* Database name or NULL */
3208 const char *zTableName, /* Table name */
3209 const char *zColumnName, /* Column name */
3210 char const **pzDataType, /* OUTPUT: Declared data type */
3211 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
3212 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
3213 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
drh9be07162008-06-16 20:51:15 +00003214 int *pAutoinc /* OUTPUT: True if column is auto-increment */
danielk1977deb802c2006-02-09 13:43:28 +00003215){
3216 int rc;
3217 char *zErrMsg = 0;
3218 Table *pTab = 0;
3219 Column *pCol = 0;
mistachkin1a51ce72015-01-12 18:38:02 +00003220 int iCol = 0;
danielk1977deb802c2006-02-09 13:43:28 +00003221 char const *zDataType = 0;
3222 char const *zCollSeq = 0;
3223 int notnull = 0;
3224 int primarykey = 0;
3225 int autoinc = 0;
3226
drh96c707a2015-02-13 16:36:14 +00003227
3228#ifdef SQLITE_ENABLE_API_ARMOR
3229 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){
3230 return SQLITE_MISUSE_BKPT;
3231 }
3232#endif
3233
danielk1977deb802c2006-02-09 13:43:28 +00003234 /* Ensure the database schema has been loaded */
drhe30f4422007-08-21 16:15:55 +00003235 sqlite3_mutex_enter(db->mutex);
drh3cb3edc2008-03-07 21:37:19 +00003236 sqlite3BtreeEnterAll(db);
danielk1977deb802c2006-02-09 13:43:28 +00003237 rc = sqlite3Init(db, &zErrMsg);
3238 if( SQLITE_OK!=rc ){
3239 goto error_out;
3240 }
3241
3242 /* Locate the table in question */
3243 pTab = sqlite3FindTable(db, zTableName, zDbName);
3244 if( !pTab || pTab->pSelect ){
3245 pTab = 0;
3246 goto error_out;
3247 }
3248
3249 /* Find the column for which info is requested */
drh45d1b202014-12-09 22:24:42 +00003250 if( zColumnName==0 ){
3251 /* Query for existance of table only */
danielk1977deb802c2006-02-09 13:43:28 +00003252 }else{
3253 for(iCol=0; iCol<pTab->nCol; iCol++){
3254 pCol = &pTab->aCol[iCol];
3255 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
3256 break;
3257 }
3258 }
3259 if( iCol==pTab->nCol ){
drh45d1b202014-12-09 22:24:42 +00003260 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){
3261 iCol = pTab->iPKey;
3262 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
3263 }else{
3264 pTab = 0;
3265 goto error_out;
3266 }
danielk1977deb802c2006-02-09 13:43:28 +00003267 }
3268 }
3269
3270 /* The following block stores the meta information that will be returned
3271 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
3272 ** and autoinc. At this point there are two possibilities:
3273 **
3274 ** 1. The specified column name was rowid", "oid" or "_rowid_"
3275 ** and there is no explicitly declared IPK column.
3276 **
3277 ** 2. The table is not a view and the column name identified an
3278 ** explicitly declared column. Copy meta information from *pCol.
3279 */
3280 if( pCol ){
3281 zDataType = pCol->zType;
3282 zCollSeq = pCol->zColl;
drh9be07162008-06-16 20:51:15 +00003283 notnull = pCol->notNull!=0;
drha371ace2012-09-13 14:22:47 +00003284 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
drh7d10d5a2008-08-20 16:35:10 +00003285 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
danielk1977deb802c2006-02-09 13:43:28 +00003286 }else{
3287 zDataType = "INTEGER";
3288 primarykey = 1;
3289 }
3290 if( !zCollSeq ){
3291 zCollSeq = "BINARY";
3292 }
3293
3294error_out:
danielk197702b4e3b2009-02-26 07:15:59 +00003295 sqlite3BtreeLeaveAll(db);
danielk1977deb802c2006-02-09 13:43:28 +00003296
3297 /* Whether the function call succeeded or failed, set the output parameters
3298 ** to whatever their local counterparts contain. If an error did occur,
3299 ** this has the effect of zeroing all output parameters.
3300 */
3301 if( pzDataType ) *pzDataType = zDataType;
3302 if( pzCollSeq ) *pzCollSeq = zCollSeq;
3303 if( pNotNull ) *pNotNull = notnull;
3304 if( pPrimaryKey ) *pPrimaryKey = primarykey;
3305 if( pAutoinc ) *pAutoinc = autoinc;
3306
3307 if( SQLITE_OK==rc && !pTab ){
drh633e6d52008-07-28 19:34:53 +00003308 sqlite3DbFree(db, zErrMsg);
drh118640b2008-07-16 14:02:32 +00003309 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
drhf089aa42008-07-08 19:34:06 +00003310 zColumnName);
danielk1977deb802c2006-02-09 13:43:28 +00003311 rc = SQLITE_ERROR;
3312 }
drh13f40da2014-08-22 18:00:11 +00003313 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg);
drh633e6d52008-07-28 19:34:53 +00003314 sqlite3DbFree(db, zErrMsg);
drhe30f4422007-08-21 16:15:55 +00003315 rc = sqlite3ApiExit(db, rc);
3316 sqlite3_mutex_leave(db->mutex);
drhf9cb7f52006-06-27 20:06:44 +00003317 return rc;
3318}
3319
3320/*
3321** Sleep for a little while. Return the amount of time slept.
3322*/
3323int sqlite3_sleep(int ms){
danielk1977b4b47412007-08-17 15:53:36 +00003324 sqlite3_vfs *pVfs;
drhe30f4422007-08-21 16:15:55 +00003325 int rc;
drhd677b3d2007-08-20 22:48:41 +00003326 pVfs = sqlite3_vfs_find(0);
drh40257ff2008-06-13 18:24:27 +00003327 if( pVfs==0 ) return 0;
danielk1977fee2d252007-08-18 10:59:19 +00003328
3329 /* This function works in milliseconds, but the underlying OsSleep()
3330 ** API uses microseconds. Hence the 1000's.
3331 */
drhe30f4422007-08-21 16:15:55 +00003332 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
drhe30f4422007-08-21 16:15:55 +00003333 return rc;
drhf9cb7f52006-06-27 20:06:44 +00003334}
drh4ac285a2006-09-15 07:28:50 +00003335
3336/*
3337** Enable or disable the extended result codes.
3338*/
3339int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
drh9ca95732014-10-24 00:35:58 +00003340#ifdef SQLITE_ENABLE_API_ARMOR
3341 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3342#endif
drhe30f4422007-08-21 16:15:55 +00003343 sqlite3_mutex_enter(db->mutex);
drh4ac285a2006-09-15 07:28:50 +00003344 db->errMask = onoff ? 0xffffffff : 0xff;
drhe30f4422007-08-21 16:15:55 +00003345 sqlite3_mutex_leave(db->mutex);
drh4ac285a2006-09-15 07:28:50 +00003346 return SQLITE_OK;
3347}
drhcc6bb3e2007-08-31 16:11:35 +00003348
3349/*
3350** Invoke the xFileControl method on a particular database.
3351*/
3352int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
3353 int rc = SQLITE_ERROR;
drh421377e2012-03-15 21:28:54 +00003354 Btree *pBtree;
3355
drh9ca95732014-10-24 00:35:58 +00003356#ifdef SQLITE_ENABLE_API_ARMOR
3357 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
3358#endif
drhcc6bb3e2007-08-31 16:11:35 +00003359 sqlite3_mutex_enter(db->mutex);
drh421377e2012-03-15 21:28:54 +00003360 pBtree = sqlite3DbNameToBtree(db, zDbName);
3361 if( pBtree ){
3362 Pager *pPager;
3363 sqlite3_file *fd;
3364 sqlite3BtreeEnter(pBtree);
3365 pPager = sqlite3BtreePager(pBtree);
3366 assert( pPager!=0 );
3367 fd = sqlite3PagerFile(pPager);
3368 assert( fd!=0 );
3369 if( op==SQLITE_FCNTL_FILE_POINTER ){
3370 *(sqlite3_file**)pArg = fd;
3371 rc = SQLITE_OK;
3372 }else if( fd->pMethods ){
3373 rc = sqlite3OsFileControl(fd, op, pArg);
3374 }else{
3375 rc = SQLITE_NOTFOUND;
drhcc6bb3e2007-08-31 16:11:35 +00003376 }
drh421377e2012-03-15 21:28:54 +00003377 sqlite3BtreeLeave(pBtree);
drhcc6bb3e2007-08-31 16:11:35 +00003378 }
3379 sqlite3_mutex_leave(db->mutex);
drh883ad042015-02-19 00:29:11 +00003380 return rc;
drhcc6bb3e2007-08-31 16:11:35 +00003381}
drhed13d982008-01-31 14:43:24 +00003382
3383/*
3384** Interface to the testing logic.
3385*/
3386int sqlite3_test_control(int op, ...){
drhed13d982008-01-31 14:43:24 +00003387 int rc = 0;
drhf5ed7ad2015-06-15 14:43:25 +00003388#ifdef SQLITE_OMIT_BUILTIN_TEST
3389 UNUSED_PARAMETER(op);
3390#else
drh2fa18682008-03-19 14:15:34 +00003391 va_list ap;
drhed13d982008-01-31 14:43:24 +00003392 va_start(ap, op);
3393 switch( op ){
danielk1977d09414c2008-06-19 18:17:49 +00003394
3395 /*
drh984bfaa2008-03-19 16:08:53 +00003396 ** Save the current state of the PRNG.
3397 */
drh2fa18682008-03-19 14:15:34 +00003398 case SQLITE_TESTCTRL_PRNG_SAVE: {
3399 sqlite3PrngSaveState();
3400 break;
3401 }
drh984bfaa2008-03-19 16:08:53 +00003402
3403 /*
3404 ** Restore the state of the PRNG to the last state saved using
3405 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
3406 ** this verb acts like PRNG_RESET.
3407 */
drh2fa18682008-03-19 14:15:34 +00003408 case SQLITE_TESTCTRL_PRNG_RESTORE: {
3409 sqlite3PrngRestoreState();
3410 break;
3411 }
drh984bfaa2008-03-19 16:08:53 +00003412
3413 /*
3414 ** Reset the PRNG back to its uninitialized state. The next call
3415 ** to sqlite3_randomness() will reseed the PRNG using a single call
3416 ** to the xRandomness method of the default VFS.
3417 */
drh2fa18682008-03-19 14:15:34 +00003418 case SQLITE_TESTCTRL_PRNG_RESET: {
drhfe980812014-01-01 14:00:13 +00003419 sqlite3_randomness(0,0);
drh2fa18682008-03-19 14:15:34 +00003420 break;
3421 }
drh3088d592008-03-21 16:45:47 +00003422
3423 /*
3424 ** sqlite3_test_control(BITVEC_TEST, size, program)
3425 **
3426 ** Run a test against a Bitvec object of size. The program argument
3427 ** is an array of integers that defines the test. Return -1 on a
3428 ** memory allocation error, 0 on success, or non-zero for an error.
3429 ** See the sqlite3BitvecBuiltinTest() for additional information.
3430 */
3431 case SQLITE_TESTCTRL_BITVEC_TEST: {
3432 int sz = va_arg(ap, int);
3433 int *aProg = va_arg(ap, int*);
3434 rc = sqlite3BitvecBuiltinTest(sz, aProg);
3435 break;
3436 }
danielk19772d1d86f2008-06-20 14:59:51 +00003437
3438 /*
drhc007f612014-05-16 14:17:01 +00003439 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
3440 **
3441 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
3442 ** if xCallback is not NULL.
3443 **
3444 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
3445 ** is called immediately after installing the new callback and the return
3446 ** value from sqlite3FaultSim(0) becomes the return from
3447 ** sqlite3_test_control().
3448 */
3449 case SQLITE_TESTCTRL_FAULT_INSTALL: {
mistachkin77a90ce2014-05-16 23:15:50 +00003450 /* MSVC is picky about pulling func ptrs from va lists.
3451 ** http://support.microsoft.com/kb/47961
dan685ffb12014-06-23 10:18:50 +00003452 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int));
mistachkin77a90ce2014-05-16 23:15:50 +00003453 */
3454 typedef int(*TESTCALLBACKFUNC_t)(int);
dan685ffb12014-06-23 10:18:50 +00003455 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
drhc007f612014-05-16 14:17:01 +00003456 rc = sqlite3FaultSim(0);
3457 break;
3458 }
3459
3460 /*
danielk19772d1d86f2008-06-20 14:59:51 +00003461 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
3462 **
3463 ** Register hooks to call to indicate which malloc() failures
3464 ** are benign.
3465 */
3466 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
danielk1977ca026792008-06-23 14:15:52 +00003467 typedef void (*void_function)(void);
3468 void_function xBenignBegin;
3469 void_function xBenignEnd;
3470 xBenignBegin = va_arg(ap, void_function);
3471 xBenignEnd = va_arg(ap, void_function);
danielk19772d1d86f2008-06-20 14:59:51 +00003472 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
3473 break;
3474 }
drhc7a3bb92009-02-05 16:31:45 +00003475
3476 /*
drhf3af63f2009-05-09 18:59:42 +00003477 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
drhc7a3bb92009-02-05 16:31:45 +00003478 **
3479 ** Set the PENDING byte to the value in the argument, if X>0.
3480 ** Make no changes if X==0. Return the value of the pending byte
3481 ** as it existing before this routine was called.
3482 **
3483 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
3484 ** an incompatible database file format. Changing the PENDING byte
3485 ** while any database connection is open results in undefined and
peter.d.reid60ec9142014-09-06 16:39:46 +00003486 ** deleterious behavior.
drhc7a3bb92009-02-05 16:31:45 +00003487 */
3488 case SQLITE_TESTCTRL_PENDING_BYTE: {
drhf83dc1e2010-06-03 12:09:52 +00003489 rc = PENDING_BYTE;
3490#ifndef SQLITE_OMIT_WSD
3491 {
3492 unsigned int newVal = va_arg(ap, unsigned int);
3493 if( newVal ) sqlite3PendingByte = newVal;
3494 }
3495#endif
drhc7a3bb92009-02-05 16:31:45 +00003496 break;
3497 }
drhf3af63f2009-05-09 18:59:42 +00003498
3499 /*
3500 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
3501 **
3502 ** This action provides a run-time test to see whether or not
3503 ** assert() was enabled at compile-time. If X is true and assert()
3504 ** is enabled, then the return value is true. If X is true and
3505 ** assert() is disabled, then the return value is zero. If X is
3506 ** false and assert() is enabled, then the assertion fires and the
3507 ** process aborts. If X is false and assert() is disabled, then the
3508 ** return value is zero.
3509 */
3510 case SQLITE_TESTCTRL_ASSERT: {
3511 volatile int x = 0;
3512 assert( (x = va_arg(ap,int))!=0 );
3513 rc = x;
3514 break;
3515 }
3516
3517
3518 /*
3519 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
3520 **
3521 ** This action provides a run-time test to see how the ALWAYS and
3522 ** NEVER macros were defined at compile-time.
3523 **
3524 ** The return value is ALWAYS(X).
3525 **
3526 ** The recommended test is X==2. If the return value is 2, that means
3527 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
3528 ** default setting. If the return value is 1, then ALWAYS() is either
3529 ** hard-coded to true or else it asserts if its argument is false.
3530 ** The first behavior (hard-coded to true) is the case if
3531 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
3532 ** behavior (assert if the argument to ALWAYS() is false) is the case if
3533 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
3534 **
3535 ** The run-time test procedure might look something like this:
3536 **
3537 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
3538 ** // ALWAYS() and NEVER() are no-op pass-through macros
3539 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
3540 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
3541 ** }else{
3542 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
3543 ** }
3544 */
3545 case SQLITE_TESTCTRL_ALWAYS: {
3546 int x = va_arg(ap,int);
3547 rc = ALWAYS(x);
3548 break;
3549 }
drhc046e3e2009-07-15 11:26:44 +00003550
drh2cf4acb2014-04-18 00:06:02 +00003551 /*
3552 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER);
3553 **
3554 ** The integer returned reveals the byte-order of the computer on which
3555 ** SQLite is running:
3556 **
3557 ** 1 big-endian, determined at run-time
3558 ** 10 little-endian, determined at run-time
3559 ** 432101 big-endian, determined at compile-time
3560 ** 123410 little-endian, determined at compile-time
3561 */
3562 case SQLITE_TESTCTRL_BYTEORDER: {
3563 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN;
3564 break;
3565 }
3566
drhc046e3e2009-07-15 11:26:44 +00003567 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
3568 **
3569 ** Set the nReserve size to N for the main database on the database
3570 ** connection db.
3571 */
3572 case SQLITE_TESTCTRL_RESERVE: {
3573 sqlite3 *db = va_arg(ap, sqlite3*);
3574 int x = va_arg(ap,int);
3575 sqlite3_mutex_enter(db->mutex);
3576 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
3577 sqlite3_mutex_leave(db->mutex);
3578 break;
3579 }
3580
drh07096f62009-12-22 23:52:32 +00003581 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
3582 **
3583 ** Enable or disable various optimizations for testing purposes. The
3584 ** argument N is a bitmask of optimizations to be disabled. For normal
3585 ** operation N should be 0. The idea is that a test program (like the
3586 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
3587 ** with various optimizations disabled to verify that the same answer
3588 ** is obtained in every case.
3589 */
3590 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
3591 sqlite3 *db = va_arg(ap, sqlite3*);
drha5759672012-10-30 14:39:12 +00003592 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
drh07096f62009-12-22 23:52:32 +00003593 break;
3594 }
3595
drh0e857732010-01-02 03:21:35 +00003596#ifdef SQLITE_N_KEYWORD
3597 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
3598 **
3599 ** If zWord is a keyword recognized by the parser, then return the
3600 ** number of keywords. Or if zWord is not a keyword, return 0.
3601 **
3602 ** This test feature is only available in the amalgamation since
3603 ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
3604 ** is built using separate source files.
3605 */
3606 case SQLITE_TESTCTRL_ISKEYWORD: {
3607 const char *zWord = va_arg(ap, const char*);
3608 int n = sqlite3Strlen30(zWord);
drh855787a2010-01-02 03:46:43 +00003609 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
drh0e857732010-01-02 03:21:35 +00003610 break;
3611 }
3612#endif
3613
drhbadc9802010-08-27 17:16:44 +00003614 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
3615 **
3616 ** Pass pFree into sqlite3ScratchFree().
3617 ** If sz>0 then allocate a scratch buffer into pNew.
3618 */
3619 case SQLITE_TESTCTRL_SCRATCHMALLOC: {
3620 void *pFree, **ppNew;
3621 int sz;
3622 sz = va_arg(ap, int);
3623 ppNew = va_arg(ap, void**);
3624 pFree = va_arg(ap, void*);
3625 if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
3626 sqlite3ScratchFree(pFree);
3627 break;
3628 }
3629
danc17d6962011-06-21 12:47:30 +00003630 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
3631 **
3632 ** If parameter onoff is non-zero, configure the wrappers so that all
3633 ** subsequent calls to localtime() and variants fail. If onoff is zero,
3634 ** undo this setting.
3635 */
3636 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
3637 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
3638 break;
3639 }
3640
drh09fe6142013-11-29 15:06:27 +00003641 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
3642 **
3643 ** Set or clear a flag that indicates that the database file is always well-
3644 ** formed and never corrupt. This flag is clear by default, indicating that
3645 ** database files might have arbitrary corruption. Setting the flag during
3646 ** testing causes certain assert() statements in the code to be activated
3647 ** that demonstrat invariants on well-formed database files.
3648 */
3649 case SQLITE_TESTCTRL_NEVER_CORRUPT: {
dan9c2552f2014-01-28 17:49:13 +00003650 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
drh09fe6142013-11-29 15:06:27 +00003651 break;
3652 }
3653
drh688852a2014-02-17 22:40:43 +00003654
3655 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
3656 **
3657 ** Set the VDBE coverage callback function to xCallback with context
3658 ** pointer ptr.
3659 */
3660 case SQLITE_TESTCTRL_VDBE_COVERAGE: {
3661#ifdef SQLITE_VDBE_COVERAGE
3662 typedef void (*branch_callback)(void*,int,u8,u8);
3663 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
3664 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
3665#endif
3666 break;
3667 }
3668
dan8930c2a2014-04-03 16:25:29 +00003669 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */
3670 case SQLITE_TESTCTRL_SORTER_MMAP: {
3671 sqlite3 *db = va_arg(ap, sqlite3*);
3672 db->nMaxSorterMmap = va_arg(ap, int);
3673 break;
3674 }
3675
drh43cfc232014-07-29 14:09:21 +00003676 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT);
3677 **
3678 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if
3679 ** not.
3680 */
3681 case SQLITE_TESTCTRL_ISINIT: {
3682 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR;
3683 break;
3684 }
drh8964b342015-01-29 17:54:52 +00003685
drh3ba689d2015-03-03 14:00:11 +00003686 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum);
drh8964b342015-01-29 17:54:52 +00003687 **
drh1ffede82015-01-30 20:59:27 +00003688 ** This test control is used to create imposter tables. "db" is a pointer
3689 ** to the database connection. dbName is the database name (ex: "main" or
3690 ** "temp") which will receive the imposter. "onOff" turns imposter mode on
3691 ** or off. "tnum" is the root page of the b-tree to which the imposter
3692 ** table should connect.
3693 **
3694 ** Enable imposter mode only when the schema has already been parsed. Then
drh3ba689d2015-03-03 14:00:11 +00003695 ** run a single CREATE TABLE statement to construct the imposter table in
3696 ** the parsed schema. Then turn imposter mode back off again.
drh1ffede82015-01-30 20:59:27 +00003697 **
3698 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing
3699 ** the schema to be reparsed the next time it is needed. This has the
3700 ** effect of erasing all imposter tables.
drh8964b342015-01-29 17:54:52 +00003701 */
drh1ffede82015-01-30 20:59:27 +00003702 case SQLITE_TESTCTRL_IMPOSTER: {
drh8964b342015-01-29 17:54:52 +00003703 sqlite3 *db = va_arg(ap, sqlite3*);
drh8fb15e32015-02-04 20:56:49 +00003704 sqlite3_mutex_enter(db->mutex);
drh1ffede82015-01-30 20:59:27 +00003705 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
3706 db->init.busy = db->init.imposterTable = va_arg(ap,int);
drh8964b342015-01-29 17:54:52 +00003707 db->init.newTnum = va_arg(ap,int);
drh1ffede82015-01-30 20:59:27 +00003708 if( db->init.busy==0 && db->init.newTnum>0 ){
3709 sqlite3ResetAllSchemasOfConnection(db);
3710 }
drh8fb15e32015-02-04 20:56:49 +00003711 sqlite3_mutex_leave(db->mutex);
drh8964b342015-01-29 17:54:52 +00003712 break;
3713 }
drhed13d982008-01-31 14:43:24 +00003714 }
3715 va_end(ap);
drh3088d592008-03-21 16:45:47 +00003716#endif /* SQLITE_OMIT_BUILTIN_TEST */
danielk19777c36d072008-01-31 15:31:01 +00003717 return rc;
drhed13d982008-01-31 14:43:24 +00003718}
drhcc487d12011-05-17 18:53:08 +00003719
3720/*
3721** This is a utility routine, useful to VFS implementations, that checks
3722** to see if a database file was a URI that contained a specific query
3723** parameter, and if so obtains the value of the query parameter.
3724**
3725** The zFilename argument is the filename pointer passed into the xOpen()
3726** method of a VFS implementation. The zParam argument is the name of the
3727** query parameter we seek. This routine returns the value of the zParam
3728** parameter if it exists. If the parameter does not exist, this routine
3729** returns a NULL pointer.
3730*/
3731const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
drh9ca95732014-10-24 00:35:58 +00003732 if( zFilename==0 || zParam==0 ) return 0;
drhbd695592011-05-17 19:43:38 +00003733 zFilename += sqlite3Strlen30(zFilename) + 1;
drhcc487d12011-05-17 18:53:08 +00003734 while( zFilename[0] ){
3735 int x = strcmp(zFilename, zParam);
drhbd695592011-05-17 19:43:38 +00003736 zFilename += sqlite3Strlen30(zFilename) + 1;
drhcc487d12011-05-17 18:53:08 +00003737 if( x==0 ) return zFilename;
drhbd695592011-05-17 19:43:38 +00003738 zFilename += sqlite3Strlen30(zFilename) + 1;
drhcc487d12011-05-17 18:53:08 +00003739 }
3740 return 0;
3741}
drh283829c2011-11-17 00:56:20 +00003742
3743/*
drh92913722011-12-23 00:07:33 +00003744** Return a boolean value for a query parameter.
3745*/
3746int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
3747 const char *z = sqlite3_uri_parameter(zFilename, zParam);
drh38d9c612012-01-31 14:24:47 +00003748 bDflt = bDflt!=0;
3749 return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
drh92913722011-12-23 00:07:33 +00003750}
3751
3752/*
3753** Return a 64-bit integer value for a query parameter.
3754*/
3755sqlite3_int64 sqlite3_uri_int64(
3756 const char *zFilename, /* Filename as passed to xOpen */
3757 const char *zParam, /* URI parameter sought */
3758 sqlite3_int64 bDflt /* return if parameter is missing */
3759){
3760 const char *z = sqlite3_uri_parameter(zFilename, zParam);
3761 sqlite3_int64 v;
drh9296c182014-07-23 13:40:49 +00003762 if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){
drh92913722011-12-23 00:07:33 +00003763 bDflt = v;
3764 }
3765 return bDflt;
3766}
3767
3768/*
drh421377e2012-03-15 21:28:54 +00003769** Return the Btree pointer identified by zDbName. Return NULL if not found.
3770*/
3771Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
3772 int i;
3773 for(i=0; i<db->nDb; i++){
3774 if( db->aDb[i].pBt
3775 && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
3776 ){
3777 return db->aDb[i].pBt;
3778 }
3779 }
3780 return 0;
3781}
3782
3783/*
drh283829c2011-11-17 00:56:20 +00003784** Return the filename of the database associated with a database
3785** connection.
3786*/
3787const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
mistachkincd54bab2014-12-20 21:14:14 +00003788 Btree *pBt;
drh9ca95732014-10-24 00:35:58 +00003789#ifdef SQLITE_ENABLE_API_ARMOR
3790 if( !sqlite3SafetyCheckOk(db) ){
3791 (void)SQLITE_MISUSE_BKPT;
3792 return 0;
3793 }
3794#endif
mistachkincd54bab2014-12-20 21:14:14 +00003795 pBt = sqlite3DbNameToBtree(db, zDbName);
drh421377e2012-03-15 21:28:54 +00003796 return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
3797}
3798
3799/*
3800** Return 1 if database is read-only or 0 if read/write. Return -1 if
3801** no such database exists.
3802*/
3803int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
mistachkincd54bab2014-12-20 21:14:14 +00003804 Btree *pBt;
drh9ca95732014-10-24 00:35:58 +00003805#ifdef SQLITE_ENABLE_API_ARMOR
3806 if( !sqlite3SafetyCheckOk(db) ){
3807 (void)SQLITE_MISUSE_BKPT;
3808 return -1;
3809 }
3810#endif
mistachkincd54bab2014-12-20 21:14:14 +00003811 pBt = sqlite3DbNameToBtree(db, zDbName);
drh781597f2014-05-21 08:21:07 +00003812 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
drh283829c2011-11-17 00:56:20 +00003813}