blob: 2bd43fbe17a0ef7da7ced749738d7de562d9f315 [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
drh75897232000-05-29 14:26:00 +000028
29/*
drhb217a572000-08-22 13:40:18 +000030** The version of the library
31*/
drhb3190c12008-12-08 21:37:14 +000032#ifndef SQLITE_AMALGAMATION
danielk197724b03fd2004-05-10 10:34:34 +000033const char sqlite3_version[] = SQLITE_VERSION;
drhb3190c12008-12-08 21:37:14 +000034#endif
drh4aec8b62004-08-28 16:19:00 +000035const char *sqlite3_libversion(void){ return sqlite3_version; }
drh47baebc2009-08-14 16:01:24 +000036const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
danielk197799ba19e2005-02-05 07:33:34 +000037int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
drhb67e8bf2007-08-30 20:09:48 +000038int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
shanehca07b9d2010-02-23 05:17:51 +000039const char *sqlite3_compileopts(void){
shanehbdea6d12010-02-23 04:19:54 +000040 static char zOpts[32] = "";
41 sqlite_int64 iEnable = 0; /* bitmask of all the SQLITE_ENABLE* defines */
42 sqlite_int64 iOmit = 0; /* bitmask of all the SQLITE_OMIT* defines */
43 sqlite_int64 iOther = 0; /* bitmask of all the SQLITE_* defines except */
44 /* SQLITE_MAX* and SQLITE_DEF* */
45
shanehca07b9d2010-02-23 05:17:51 +000046#ifdef SQLITE_32BIT_ROWID
47 iOther |= ((sqlite_int64)1<<0);
48#endif
49#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
50 iOther |= ((sqlite_int64)1<<1);
51#endif
52#ifdef SQLITE_API
53 iOther |= ((sqlite_int64)1<<2);
54#endif
55#ifdef SQLITE_CASE_SENSITIVE_LIKE
56 iOther |= ((sqlite_int64)1<<3);
57#endif
58#ifdef SQLITE_CHECK_PAGES
59 iOther |= ((sqlite_int64)1<<4);
60#endif
61#ifdef SQLITE_COVERAGE_TEST
62 iOther |= ((sqlite_int64)1<<5);
63#endif
64#ifdef SQLITE_DEBUG
65 iOther |= ((sqlite_int64)1<<6);
66#endif
67#ifdef SQLITE_DISABLE_DIRSYNC
68 iOther |= ((sqlite_int64)1<<7);
69#endif
70#ifdef SQLITE_DISABLE_LFS
71 iOther |= ((sqlite_int64)1<<8);
72#endif
73#ifdef SQLITE_ENABLE_ATOMIC_WRITE
74 iEnable |= ((sqlite_int64)1<<0);
75#endif
76#ifdef SQLITE_ENABLE_CEROD
77 iEnable |= ((sqlite_int64)1<<1);
78#endif
79#ifdef SQLITE_ENABLE_COLUMN_METADATA
80 iEnable |= ((sqlite_int64)1<<2);
81#endif
82#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
83 iEnable |= ((sqlite_int64)1<<3);
84#endif
85#ifdef SQLITE_ENABLE_FTS1
86 iEnable |= ((sqlite_int64)1<<4);
87#endif
88#ifdef SQLITE_ENABLE_FTS2
89 iEnable |= ((sqlite_int64)1<<5);
90#endif
91#ifdef SQLITE_ENABLE_FTS3
92 iEnable |= ((sqlite_int64)1<<6);
93#endif
94#ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
95 iEnable |= ((sqlite_int64)1<<7);
96#endif
97#ifdef SQLITE_ENABLE_FTS4
98 iEnable |= ((sqlite_int64)1<<8);
99#endif
100#ifdef SQLITE_ENABLE_ICU
101 iEnable |= ((sqlite_int64)1<<9);
102#endif
103#ifdef SQLITE_ENABLE_IOTRACE
104 iEnable |= ((sqlite_int64)1<<10);
105#endif
106#ifdef SQLITE_ENABLE_LOAD_EXTENSION
107 iEnable |= ((sqlite_int64)1<<11);
108#endif
109#ifdef SQLITE_ENABLE_LOCKING_STYLE
110 iEnable |= ((sqlite_int64)1<<12);
111#endif
112#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
113 iEnable |= ((sqlite_int64)1<<13);
114#endif
115#ifdef SQLITE_ENABLE_MEMSYS3
116 iEnable |= ((sqlite_int64)1<<14);
117#endif
118#ifdef SQLITE_ENABLE_MEMSYS5
119 iEnable |= ((sqlite_int64)1<<15);
120#endif
121#ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
122 iEnable |= ((sqlite_int64)1<<16);
123#endif
124#ifdef SQLITE_ENABLE_RTREE
125 iEnable |= ((sqlite_int64)1<<17);
126#endif
127#ifdef SQLITE_ENABLE_STAT2
128 iEnable |= ((sqlite_int64)1<<18);
129#endif
130#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
131 iEnable |= ((sqlite_int64)1<<19);
132#endif
133#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
134 iEnable |= ((sqlite_int64)1<<20);
135#endif
136#ifdef SQLITE_HAS_CODEC
137 iOther |= ((sqlite_int64)1<<9);
138#endif
139#ifdef SQLITE_HAVE_ISNAN
140 iOther |= ((sqlite_int64)1<<10);
141#endif
142#ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
143 iOther |= ((sqlite_int64)1<<11);
144#endif
145#ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
146 iOther |= ((sqlite_int64)1<<12);
147#endif
148#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
149 iOther |= ((sqlite_int64)1<<13);
150#endif
151#ifdef SQLITE_INT64_TYPE
152 iOther |= ((sqlite_int64)1<<14);
153#endif
154#ifdef SQLITE_LOCK_TRACE
155 iOther |= ((sqlite_int64)1<<15);
156#endif
157#ifdef SQLITE_MEMDEBUG
158 iOther |= ((sqlite_int64)1<<16);
159#endif
160#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
161 iOther |= ((sqlite_int64)1<<17);
162#endif
163#ifdef SQLITE_MUTEX_NOOP
164 iOther |= ((sqlite_int64)1<<18);
165#endif
166#ifdef SQLITE_MUTEX_OMIT
167 iOther |= ((sqlite_int64)1<<19);
168#endif
169#ifdef SQLITE_MUTEX_OS2
170 iOther |= ((sqlite_int64)1<<20);
171#endif
172#ifdef SQLITE_MUTEX_PTHREADS
173 iOther |= ((sqlite_int64)1<<21);
174#endif
175#ifdef SQLITE_MUTEX_W32
176 iOther |= ((sqlite_int64)1<<22);
177#endif
178#ifdef SQLITE_NO_SYNC
179 iOther |= ((sqlite_int64)1<<23);
180#endif
181#ifdef SQLITE_OMIT_ALTERTABLE
182 iOmit |= ((sqlite_int64)1<<0);
183#endif
184#ifdef SQLITE_OMIT_ANALYZE
185 iOmit |= ((sqlite_int64)1<<1);
186#endif
187#ifdef SQLITE_OMIT_ATTACH
188 iOmit |= ((sqlite_int64)1<<2);
189#endif
190#ifdef SQLITE_OMIT_AUTHORIZATION
191 iOmit |= ((sqlite_int64)1<<3);
192#endif
193#ifdef SQLITE_OMIT_AUTOINCREMENT
194 iOmit |= ((sqlite_int64)1<<4);
195#endif
196#ifdef SQLITE_OMIT_AUTOINIT
197 iOmit |= ((sqlite_int64)1<<5);
198#endif
199#ifdef SQLITE_OMIT_AUTOVACUUM
200 iOmit |= ((sqlite_int64)1<<6);
201#endif
202#ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
203 iOmit |= ((sqlite_int64)1<<7);
204#endif
205#ifdef SQLITE_OMIT_BLOB_LITERAL
206 iOmit |= ((sqlite_int64)1<<8);
207#endif
208#ifdef SQLITE_OMIT_BTREECOUNT
209 iOmit |= ((sqlite_int64)1<<9);
210#endif
211#ifdef SQLITE_OMIT_BUILTIN_TEST
212 iOmit |= ((sqlite_int64)1<<10);
213#endif
214#ifdef SQLITE_OMIT_CAST
215 iOmit |= ((sqlite_int64)1<<11);
216#endif
217#ifdef SQLITE_OMIT_CHECK
218 iOmit |= ((sqlite_int64)1<<12);
219#endif
220#ifdef SQLITE_OMIT_COMPLETE
221 iOmit |= ((sqlite_int64)1<<13);
222#endif
223#ifdef SQLITE_OMIT_COMPOUND_SELECT
224 iOmit |= ((sqlite_int64)1<<14);
225#endif
226#ifdef SQLITE_OMIT_DATETIME_FUNCS
227 iOmit |= ((sqlite_int64)1<<15);
228#endif
229#ifdef SQLITE_OMIT_DECLTYPE
230 iOmit |= ((sqlite_int64)1<<16);
231#endif
232#ifdef SQLITE_OMIT_DEPRECATED
233 iOmit |= ((sqlite_int64)1<<17);
234#endif
235#ifdef SQLITE_OMIT_DISKIO
236 iOmit |= ((sqlite_int64)1<<18);
237#endif
238#ifdef SQLITE_OMIT_EXPLAIN
239 iOmit |= ((sqlite_int64)1<<19);
240#endif
241#ifdef SQLITE_OMIT_FLAG_PRAGMAS
242 iOmit |= ((sqlite_int64)1<<20);
243#endif
244#ifdef SQLITE_OMIT_FLOATING_POINT
245 iOmit |= ((sqlite_int64)1<<21);
246#endif
247#ifdef SQLITE_OMIT_FOREIGN_KEY
248 iOmit |= ((sqlite_int64)1<<22);
249#endif
250#ifdef SQLITE_OMIT_GET_TABLE
251 iOmit |= ((sqlite_int64)1<<23);
252#endif
253#ifdef SQLITE_OMIT_GLOBALRECOVER
254 iOmit |= ((sqlite_int64)1<<24);
255#endif
256#ifdef SQLITE_OMIT_INCRBLOB
257 iOmit |= ((sqlite_int64)1<<25);
258#endif
259#ifdef SQLITE_OMIT_INTEGRITY_CHECK
260 iOmit |= ((sqlite_int64)1<<26);
261#endif
262#ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
263 iOmit |= ((sqlite_int64)1<<27);
264#endif
265#ifdef SQLITE_OMIT_LOAD_EXTENSION
266 iOmit |= ((sqlite_int64)1<<28);
267#endif
268#ifdef SQLITE_OMIT_LOCALTIME
269 iOmit |= ((sqlite_int64)1<<29);
270#endif
271#ifdef SQLITE_OMIT_LOOKASIDE
272 iOmit |= ((sqlite_int64)1<<30);
273#endif
274#ifdef SQLITE_OMIT_MEMORYDB
275 iOmit |= ((sqlite_int64)1<<31);
276#endif
277#ifdef SQLITE_OMIT_OR_OPTIMIZATION
278 iOmit |= ((sqlite_int64)1<<32);
279#endif
280#ifdef SQLITE_OMIT_PAGER_PRAGMAS
281 iOmit |= ((sqlite_int64)1<<33);
282#endif
283#ifdef SQLITE_OMIT_PRAGMA
284 iOmit |= ((sqlite_int64)1<<34);
285#endif
286#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
287 iOmit |= ((sqlite_int64)1<<35);
288#endif
289#ifdef SQLITE_OMIT_QUICKBALANCE
290 iOmit |= ((sqlite_int64)1<<36);
291#endif
292#ifdef SQLITE_OMIT_REINDEX
293 iOmit |= ((sqlite_int64)1<<37);
294#endif
295#ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
296 iOmit |= ((sqlite_int64)1<<38);
297#endif
298#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
299 iOmit |= ((sqlite_int64)1<<39);
300#endif
301#ifdef SQLITE_OMIT_SHARED_CACHE
302 iOmit |= ((sqlite_int64)1<<40);
303#endif
304#ifdef SQLITE_OMIT_SUBQUERY
305 iOmit |= ((sqlite_int64)1<<41);
306#endif
307#ifdef SQLITE_OMIT_TCL_VARIABLE
308 iOmit |= ((sqlite_int64)1<<42);
309#endif
310#ifdef SQLITE_OMIT_TEMPDB
311 iOmit |= ((sqlite_int64)1<<43);
312#endif
313#ifdef SQLITE_OMIT_TRACE
314 iOmit |= ((sqlite_int64)1<<44);
315#endif
316#ifdef SQLITE_OMIT_TRIGGER
317 iOmit |= ((sqlite_int64)1<<45);
318#endif
319#ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
320 iOmit |= ((sqlite_int64)1<<46);
321#endif
322#ifdef SQLITE_OMIT_UTF16
323 iOmit |= ((sqlite_int64)1<<47);
324#endif
325#ifdef SQLITE_OMIT_VACUUM
326 iOmit |= ((sqlite_int64)1<<48);
327#endif
328#ifdef SQLITE_OMIT_VIEW
329 iOmit |= ((sqlite_int64)1<<49);
330#endif
331#ifdef SQLITE_OMIT_VIRTUALTABLE
332 iOmit |= ((sqlite_int64)1<<50);
333#endif
334#ifdef SQLITE_OMIT_WSD
335 iOmit |= ((sqlite_int64)1<<51);
336#endif
337#ifdef SQLITE_OMIT_XFER_OPT
338 iOmit |= ((sqlite_int64)1<<52);
339#endif
340#ifdef SQLITE_PERFORMANCE_TRACE
341 iOther |= ((sqlite_int64)1<<24);
342#endif
343#ifdef SQLITE_PROXY_DEBUG
344 iOther |= ((sqlite_int64)1<<25);
345#endif
346#ifdef SQLITE_SECURE_DELETE
347 iOther |= ((sqlite_int64)1<<26);
348#endif
349#ifdef SQLITE_SMALL_STACK
350 iOther |= ((sqlite_int64)1<<27);
351#endif
352#ifdef SQLITE_SOUNDEX
353 iOther |= ((sqlite_int64)1<<28);
354#endif
355#ifdef SQLITE_TCL
356 iOther |= ((sqlite_int64)1<<29);
357#endif
358#ifdef SQLITE_TEST
359 iOther |= ((sqlite_int64)1<<30);
360#endif
361#ifdef SQLITE_USE_ALLOCA
362 iOther |= ((sqlite_int64)1<<31);
363#endif
364#ifdef SQLITE_ZERO_MALLOC
365 iOther |= ((sqlite_int64)1<<32);
366#endif
shanehbdea6d12010-02-23 04:19:54 +0000367
368 sqlite3_snprintf(sizeof(zOpts)-1, zOpts,
369 "%016llx%016llx%016llx", iEnable, iOmit, iOther);
370 return zOpts;
371}
drhb217a572000-08-22 13:40:18 +0000372
drhe265b082008-05-01 17:03:49 +0000373#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
drhb217a572000-08-22 13:40:18 +0000374/*
drhb0603412007-02-28 04:47:26 +0000375** If the following function pointer is not NULL and if
376** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
377** I/O active are written using this function. These messages
378** are intended for debugging activity only.
379*/
mlcreech3a00f902008-03-04 17:45:01 +0000380void (*sqlite3IoTrace)(const char*, ...) = 0;
drhe265b082008-05-01 17:03:49 +0000381#endif
drhb0603412007-02-28 04:47:26 +0000382
383/*
drha16313e2007-03-30 11:29:32 +0000384** If the following global variable points to a string which is the
385** name of a directory, then that directory will be used to store
386** temporary files.
387**
388** See also the "PRAGMA temp_store_directory" SQL command.
389*/
390char *sqlite3_temp_directory = 0;
391
drhc5499be2008-04-01 15:06:33 +0000392/*
drh40257ff2008-06-13 18:24:27 +0000393** Initialize SQLite.
394**
395** This routine must be called to initialize the memory allocation,
drh93ed56d2008-08-12 15:21:11 +0000396** VFS, and mutex subsystems prior to doing any serious work with
drh40257ff2008-06-13 18:24:27 +0000397** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
398** this routine will be called automatically by key routines such as
399** sqlite3_open().
400**
401** This routine is a no-op except on its very first call for the process,
402** or for the first call after a call to sqlite3_shutdown.
drh93ed56d2008-08-12 15:21:11 +0000403**
404** The first thread to call this routine runs the initialization to
405** completion. If subsequent threads call this routine before the first
406** thread has finished the initialization process, then the subsequent
407** threads must block until the first thread finishes with the initialization.
408**
409** The first thread might call this routine recursively. Recursive
410** calls to this routine should not block, of course. Otherwise the
411** initialization process would never complete.
412**
413** Let X be the first thread to enter this routine. Let Y be some other
414** thread. Then while the initial invocation of this routine by X is
415** incomplete, it is required that:
416**
417** * Calls to this routine from Y must block until the outer-most
418** call by X completes.
419**
420** * Recursive calls to this routine from thread X return immediately
421** without blocking.
drh40257ff2008-06-13 18:24:27 +0000422*/
423int sqlite3_initialize(void){
danielk1977075c23a2008-09-01 18:34:20 +0000424 sqlite3_mutex *pMaster; /* The main static mutex */
425 int rc; /* Result code */
426
427#ifdef SQLITE_OMIT_WSD
danielk1977a8f83bf2008-09-02 16:22:28 +0000428 rc = sqlite3_wsd_init(4096, 24);
danielk1977075c23a2008-09-01 18:34:20 +0000429 if( rc!=SQLITE_OK ){
430 return rc;
431 }
432#endif
danielk197771bc31c2008-06-26 08:29:34 +0000433
drh93ed56d2008-08-12 15:21:11 +0000434 /* If SQLite is already completely initialized, then this call
435 ** to sqlite3_initialize() should be a no-op. But the initialization
436 ** must be complete. So isInit must not be set until the very end
437 ** of this routine.
438 */
danielk1977075c23a2008-09-01 18:34:20 +0000439 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
danielk197771bc31c2008-06-26 08:29:34 +0000440
drh93ed56d2008-08-12 15:21:11 +0000441 /* Make sure the mutex subsystem is initialized. If unable to
442 ** initialize the mutex subsystem, return early with the error.
443 ** If the system is so sick that we are unable to allocate a mutex,
444 ** there is not much SQLite is going to be able to do.
445 **
446 ** The mutex subsystem must take care of serializing its own
447 ** initialization.
448 */
danielk1977d0251742008-06-18 18:57:42 +0000449 rc = sqlite3MutexInit();
drh93ed56d2008-08-12 15:21:11 +0000450 if( rc ) return rc;
danielk197771bc31c2008-06-26 08:29:34 +0000451
drh93ed56d2008-08-12 15:21:11 +0000452 /* Initialize the malloc() system and the recursive pInitMutex mutex.
453 ** This operation is protected by the STATIC_MASTER mutex. Note that
454 ** MutexAlloc() is called for a static mutex prior to initializing the
455 ** malloc subsystem - this implies that the allocation of a static
456 ** mutex must not require support from the malloc subsystem.
457 */
458 pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
459 sqlite3_mutex_enter(pMaster);
dane1ab2192009-08-17 15:16:19 +0000460 sqlite3GlobalConfig.isMutexInit = 1;
danielk1977075c23a2008-09-01 18:34:20 +0000461 if( !sqlite3GlobalConfig.isMallocInit ){
drh93ed56d2008-08-12 15:21:11 +0000462 rc = sqlite3MallocInit();
463 }
drh40257ff2008-06-13 18:24:27 +0000464 if( rc==SQLITE_OK ){
danielk1977075c23a2008-09-01 18:34:20 +0000465 sqlite3GlobalConfig.isMallocInit = 1;
466 if( !sqlite3GlobalConfig.pInitMutex ){
drh9ac06502009-08-17 13:42:29 +0000467 sqlite3GlobalConfig.pInitMutex =
468 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
danielk1977075c23a2008-09-01 18:34:20 +0000469 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
drh93ed56d2008-08-12 15:21:11 +0000470 rc = SQLITE_NOMEM;
drh40257ff2008-06-13 18:24:27 +0000471 }
472 }
danielk197777eb5bb2008-09-22 17:22:19 +0000473 }
474 if( rc==SQLITE_OK ){
danielk1977075c23a2008-09-01 18:34:20 +0000475 sqlite3GlobalConfig.nRefInitMutex++;
drh93ed56d2008-08-12 15:21:11 +0000476 }
477 sqlite3_mutex_leave(pMaster);
danielk197771bc31c2008-06-26 08:29:34 +0000478
dane1ab2192009-08-17 15:16:19 +0000479 /* If rc is not SQLITE_OK at this point, then either the malloc
480 ** subsystem could not be initialized or the system failed to allocate
481 ** the pInitMutex mutex. Return an error in either case. */
drh93ed56d2008-08-12 15:21:11 +0000482 if( rc!=SQLITE_OK ){
483 return rc;
484 }
485
486 /* Do the rest of the initialization under the recursive mutex so
487 ** that we will be able to handle recursive calls into
488 ** sqlite3_initialize(). The recursive calls normally come through
489 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
490 ** recursive calls might also be possible.
491 */
danielk1977075c23a2008-09-01 18:34:20 +0000492 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
danielk1977502b4e02008-09-02 14:07:24 +0000493 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
danielk1977075c23a2008-09-01 18:34:20 +0000494 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
danielk1977502b4e02008-09-02 14:07:24 +0000495 sqlite3GlobalConfig.inProgress = 1;
danielk1977075c23a2008-09-01 18:34:20 +0000496 memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
drh70a8ca32008-08-21 18:49:27 +0000497 sqlite3RegisterGlobalFunctions();
dane1ab2192009-08-17 15:16:19 +0000498 if( sqlite3GlobalConfig.isPCacheInit==0 ){
499 rc = sqlite3PcacheInitialize();
500 }
danielk19778c0a7912008-08-20 14:49:23 +0000501 if( rc==SQLITE_OK ){
dane1ab2192009-08-17 15:16:19 +0000502 sqlite3GlobalConfig.isPCacheInit = 1;
dan3d6e0602009-08-17 15:52:25 +0000503 rc = sqlite3OsInit();
drh0bf9f7b2009-04-17 16:54:22 +0000504 }
505 if( rc==SQLITE_OK ){
danielk19775b775292008-09-02 09:38:06 +0000506 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
507 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
drh0bf9f7b2009-04-17 16:54:22 +0000508 sqlite3GlobalConfig.isInit = 1;
danielk19778c0a7912008-08-20 14:49:23 +0000509 }
danielk1977502b4e02008-09-02 14:07:24 +0000510 sqlite3GlobalConfig.inProgress = 0;
drh40257ff2008-06-13 18:24:27 +0000511 }
danielk1977075c23a2008-09-01 18:34:20 +0000512 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
shanedfb7b372008-07-22 05:13:30 +0000513
drh93ed56d2008-08-12 15:21:11 +0000514 /* Go back under the static mutex and clean up the recursive
515 ** mutex to prevent a resource leak.
516 */
517 sqlite3_mutex_enter(pMaster);
danielk1977075c23a2008-09-01 18:34:20 +0000518 sqlite3GlobalConfig.nRefInitMutex--;
519 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
520 assert( sqlite3GlobalConfig.nRefInitMutex==0 );
521 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
522 sqlite3GlobalConfig.pInitMutex = 0;
drh93ed56d2008-08-12 15:21:11 +0000523 }
524 sqlite3_mutex_leave(pMaster);
525
526 /* The following is just a sanity check to make sure SQLite has
527 ** been compiled correctly. It is important to run this code, but
528 ** we don't want to run it too often and soak up CPU cycles for no
529 ** reason. So we run it once during initialization.
530 */
shanedfb7b372008-07-22 05:13:30 +0000531#ifndef NDEBUG
shanefbd60f82009-02-04 03:59:25 +0000532#ifndef SQLITE_OMIT_FLOATING_POINT
shanedfb7b372008-07-22 05:13:30 +0000533 /* This section of code's only "output" is via assert() statements. */
534 if ( rc==SQLITE_OK ){
535 u64 x = (((u64)1)<<63)-1;
536 double y;
537 assert(sizeof(x)==8);
538 assert(sizeof(x)==sizeof(y));
539 memcpy(&y, &x, 8);
540 assert( sqlite3IsNaN(y) );
541 }
542#endif
shanefbd60f82009-02-04 03:59:25 +0000543#endif
shanedfb7b372008-07-22 05:13:30 +0000544
drh40257ff2008-06-13 18:24:27 +0000545 return rc;
546}
547
548/*
549** Undo the effects of sqlite3_initialize(). Must not be called while
550** there are outstanding database connections or memory allocations or
551** while any part of SQLite is otherwise in use in any thread. This
drhd1a24402009-04-19 12:23:58 +0000552** routine is not threadsafe. But it is safe to invoke this routine
553** on when SQLite is already shut down. If SQLite is already shut down
554** when this routine is invoked, then this routine is a harmless no-op.
drh40257ff2008-06-13 18:24:27 +0000555*/
556int sqlite3_shutdown(void){
danielk1977075c23a2008-09-01 18:34:20 +0000557 if( sqlite3GlobalConfig.isInit ){
drha7640922009-04-21 12:02:56 +0000558 sqlite3_os_end();
drhd1a24402009-04-19 12:23:58 +0000559 sqlite3_reset_auto_extension();
drhd1a24402009-04-19 12:23:58 +0000560 sqlite3GlobalConfig.isInit = 0;
danielk1977fb437272008-07-08 12:02:35 +0000561 }
dane1ab2192009-08-17 15:16:19 +0000562 if( sqlite3GlobalConfig.isPCacheInit ){
563 sqlite3PcacheShutdown();
564 sqlite3GlobalConfig.isPCacheInit = 0;
565 }
566 if( sqlite3GlobalConfig.isMallocInit ){
567 sqlite3MallocEnd();
568 sqlite3GlobalConfig.isMallocInit = 0;
569 }
570 if( sqlite3GlobalConfig.isMutexInit ){
571 sqlite3MutexEnd();
572 sqlite3GlobalConfig.isMutexInit = 0;
573 }
574
drh40257ff2008-06-13 18:24:27 +0000575 return SQLITE_OK;
576}
577
578/*
579** This API allows applications to modify the global configuration of
580** the SQLite library at run-time.
581**
582** This routine should only be called when there are no outstanding
583** database connections or memory allocations. This routine is not
584** threadsafe. Failure to heed these warnings can lead to unpredictable
585** behavior.
586*/
587int sqlite3_config(int op, ...){
588 va_list ap;
589 int rc = SQLITE_OK;
590
591 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
592 ** the SQLite library is in use. */
danielk1977075c23a2008-09-01 18:34:20 +0000593 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE;
drh40257ff2008-06-13 18:24:27 +0000594
595 va_start(ap, op);
596 switch( op ){
drh18472fa2008-10-07 15:25:48 +0000597
598 /* Mutex configuration options are only available in a threadsafe
599 ** compile.
600 */
drhd68eee02009-12-11 03:44:18 +0000601#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
drh40257ff2008-06-13 18:24:27 +0000602 case SQLITE_CONFIG_SINGLETHREAD: {
603 /* Disable all mutexing */
danielk1977075c23a2008-09-01 18:34:20 +0000604 sqlite3GlobalConfig.bCoreMutex = 0;
605 sqlite3GlobalConfig.bFullMutex = 0;
drh40257ff2008-06-13 18:24:27 +0000606 break;
607 }
608 case SQLITE_CONFIG_MULTITHREAD: {
609 /* Disable mutexing of database connections */
610 /* Enable mutexing of core data structures */
danielk1977075c23a2008-09-01 18:34:20 +0000611 sqlite3GlobalConfig.bCoreMutex = 1;
612 sqlite3GlobalConfig.bFullMutex = 0;
drh40257ff2008-06-13 18:24:27 +0000613 break;
614 }
615 case SQLITE_CONFIG_SERIALIZED: {
616 /* Enable all mutexing */
danielk1977075c23a2008-09-01 18:34:20 +0000617 sqlite3GlobalConfig.bCoreMutex = 1;
618 sqlite3GlobalConfig.bFullMutex = 1;
drh40257ff2008-06-13 18:24:27 +0000619 break;
620 }
drh18472fa2008-10-07 15:25:48 +0000621 case SQLITE_CONFIG_MUTEX: {
622 /* Specify an alternative mutex implementation */
623 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
624 break;
625 }
626 case SQLITE_CONFIG_GETMUTEX: {
627 /* Retrieve the current mutex implementation */
628 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
629 break;
630 }
631#endif
632
633
drh40257ff2008-06-13 18:24:27 +0000634 case SQLITE_CONFIG_MALLOC: {
635 /* Specify an alternative malloc implementation */
danielk1977075c23a2008-09-01 18:34:20 +0000636 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
drh40257ff2008-06-13 18:24:27 +0000637 break;
638 }
drh33589792008-06-18 13:27:46 +0000639 case SQLITE_CONFIG_GETMALLOC: {
danielk197757e5ea92008-06-24 19:02:55 +0000640 /* Retrieve the current malloc() implementation */
danielk1977075c23a2008-09-01 18:34:20 +0000641 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
642 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
drh33589792008-06-18 13:27:46 +0000643 break;
644 }
drhfec00ea2008-06-14 16:56:21 +0000645 case SQLITE_CONFIG_MEMSTATUS: {
drh40257ff2008-06-13 18:24:27 +0000646 /* Enable or disable the malloc status collection */
danielk1977075c23a2008-09-01 18:34:20 +0000647 sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
drh40257ff2008-06-13 18:24:27 +0000648 break;
649 }
drh33589792008-06-18 13:27:46 +0000650 case SQLITE_CONFIG_SCRATCH: {
651 /* Designate a buffer for scratch memory space */
danielk1977075c23a2008-09-01 18:34:20 +0000652 sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
653 sqlite3GlobalConfig.szScratch = va_arg(ap, int);
654 sqlite3GlobalConfig.nScratch = va_arg(ap, int);
drh33589792008-06-18 13:27:46 +0000655 break;
656 }
657 case SQLITE_CONFIG_PAGECACHE: {
drhb0937192009-05-22 10:53:29 +0000658 /* Designate a buffer for page cache memory space */
danielk1977075c23a2008-09-01 18:34:20 +0000659 sqlite3GlobalConfig.pPage = va_arg(ap, void*);
660 sqlite3GlobalConfig.szPage = va_arg(ap, int);
661 sqlite3GlobalConfig.nPage = va_arg(ap, int);
drh33589792008-06-18 13:27:46 +0000662 break;
663 }
danielk19775099be52008-06-27 13:27:03 +0000664
danielk1977bc2ca9e2008-11-13 14:28:28 +0000665 case SQLITE_CONFIG_PCACHE: {
drhb0937192009-05-22 10:53:29 +0000666 /* Specify an alternative page cache implementation */
danielk1977bc2ca9e2008-11-13 14:28:28 +0000667 sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
668 break;
669 }
670
drhb232c232008-11-19 01:20:26 +0000671 case SQLITE_CONFIG_GETPCACHE: {
672 if( sqlite3GlobalConfig.pcache.xInit==0 ){
673 sqlite3PCacheSetDefault();
674 }
675 *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
676 break;
677 }
678
drh5d414832008-07-15 14:47:18 +0000679#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
drh33589792008-06-18 13:27:46 +0000680 case SQLITE_CONFIG_HEAP: {
danielk19775099be52008-06-27 13:27:03 +0000681 /* Designate a buffer for heap memory space */
danielk1977075c23a2008-09-01 18:34:20 +0000682 sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
683 sqlite3GlobalConfig.nHeap = va_arg(ap, int);
684 sqlite3GlobalConfig.mnReq = va_arg(ap, int);
danielk19775099be52008-06-27 13:27:03 +0000685
danielk1977075c23a2008-09-01 18:34:20 +0000686 if( sqlite3GlobalConfig.pHeap==0 ){
drh8a42cbd2008-07-10 18:13:42 +0000687 /* If the heap pointer is NULL, then restore the malloc implementation
688 ** back to NULL pointers too. This will cause the malloc to go
689 ** back to its default implementation when sqlite3_initialize() is
690 ** run.
691 */
danielk1977075c23a2008-09-01 18:34:20 +0000692 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
drh8a42cbd2008-07-10 18:13:42 +0000693 }else{
694 /* The heap pointer is not NULL, then install one of the
695 ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
696 ** ENABLE_MEMSYS5 is defined, return an error.
drh8a42cbd2008-07-10 18:13:42 +0000697 */
danielk19775099be52008-06-27 13:27:03 +0000698#ifdef SQLITE_ENABLE_MEMSYS3
danielk1977075c23a2008-09-01 18:34:20 +0000699 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
drh8a42cbd2008-07-10 18:13:42 +0000700#endif
701#ifdef SQLITE_ENABLE_MEMSYS5
danielk1977075c23a2008-09-01 18:34:20 +0000702 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
drh8a42cbd2008-07-10 18:13:42 +0000703#endif
drh8a42cbd2008-07-10 18:13:42 +0000704 }
danielk19775099be52008-06-27 13:27:03 +0000705 break;
706 }
drh5d414832008-07-15 14:47:18 +0000707#endif
danielk19775099be52008-06-27 13:27:03 +0000708
drh633e6d52008-07-28 19:34:53 +0000709 case SQLITE_CONFIG_LOOKASIDE: {
danielk1977075c23a2008-09-01 18:34:20 +0000710 sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
711 sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
drh633e6d52008-07-28 19:34:53 +0000712 break;
713 }
drh3f280702010-02-18 18:45:09 +0000714
715 /* Record a pointer to the logger funcction and its first argument.
716 ** The default is NULL. Logging is disabled if the function pointer is
717 ** NULL.
718 */
719 case SQLITE_CONFIG_LOG: {
720 sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
721 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
722 break;
723 }
drh633e6d52008-07-28 19:34:53 +0000724
drh40257ff2008-06-13 18:24:27 +0000725 default: {
726 rc = SQLITE_ERROR;
727 break;
728 }
729 }
730 va_end(ap);
731 return rc;
732}
733
734/*
drh633e6d52008-07-28 19:34:53 +0000735** Set up the lookaside buffers for a database connection.
736** Return SQLITE_OK on success.
737** If lookaside is already active, return SQLITE_BUSY.
drhe9d1c722008-08-04 20:13:26 +0000738**
739** The sz parameter is the number of bytes in each lookaside slot.
740** The cnt parameter is the number of slots. If pStart is NULL the
741** space for the lookaside memory is obtained from sqlite3_malloc().
742** If pStart is not NULL then it is sz*cnt bytes of memory to use for
743** the lookaside memory.
drh633e6d52008-07-28 19:34:53 +0000744*/
drhe9d1c722008-08-04 20:13:26 +0000745static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
drh633e6d52008-07-28 19:34:53 +0000746 void *pStart;
747 if( db->lookaside.nOut ){
748 return SQLITE_BUSY;
749 }
shanef71f89e2009-01-30 06:11:54 +0000750 /* Free any existing lookaside buffer for this handle before
751 ** allocating a new one so we don't have to have space for
752 ** both at the same time.
753 */
754 if( db->lookaside.bMalloced ){
755 sqlite3_free(db->lookaside.pStart);
756 }
757 /* The size of a lookaside slot needs to be larger than a pointer
758 ** to be useful.
759 */
drh1d34fde2009-02-03 15:50:33 +0000760 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
drh633e6d52008-07-28 19:34:53 +0000761 if( cnt<0 ) cnt = 0;
shanef71f89e2009-01-30 06:11:54 +0000762 if( sz==0 || cnt==0 ){
763 sz = 0;
764 pStart = 0;
765 }else if( pBuf==0 ){
danielk1977bc739712009-03-23 04:33:32 +0000766 sz = ROUND8(sz);
drhe9d1c722008-08-04 20:13:26 +0000767 sqlite3BeginBenignMalloc();
768 pStart = sqlite3Malloc( sz*cnt );
769 sqlite3EndBenignMalloc();
770 }else{
danielk1977bc739712009-03-23 04:33:32 +0000771 sz = ROUNDDOWN8(sz);
drhe9d1c722008-08-04 20:13:26 +0000772 pStart = pBuf;
773 }
drh4cfb22f2008-08-01 18:47:01 +0000774 db->lookaside.pStart = pStart;
775 db->lookaside.pFree = 0;
drh1bd10f82008-12-10 21:19:56 +0000776 db->lookaside.sz = (u16)sz;
drh633e6d52008-07-28 19:34:53 +0000777 if( pStart ){
778 int i;
779 LookasideSlot *p;
drhde467982009-04-02 17:22:41 +0000780 assert( sz > (int)sizeof(LookasideSlot*) );
drh633e6d52008-07-28 19:34:53 +0000781 p = (LookasideSlot*)pStart;
782 for(i=cnt-1; i>=0; i--){
783 p->pNext = db->lookaside.pFree;
784 db->lookaside.pFree = p;
785 p = (LookasideSlot*)&((u8*)p)[sz];
786 }
787 db->lookaside.pEnd = p;
788 db->lookaside.bEnabled = 1;
shanef71f89e2009-01-30 06:11:54 +0000789 db->lookaside.bMalloced = pBuf==0 ?1:0;
drh4cfb22f2008-08-01 18:47:01 +0000790 }else{
791 db->lookaside.pEnd = 0;
792 db->lookaside.bEnabled = 0;
shanef71f89e2009-01-30 06:11:54 +0000793 db->lookaside.bMalloced = 0;
drh633e6d52008-07-28 19:34:53 +0000794 }
795 return SQLITE_OK;
796}
797
798/*
drh4413d0e2008-11-04 13:46:27 +0000799** Return the mutex associated with a database connection.
800*/
801sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
802 return db->mutex;
803}
804
805/*
drh633e6d52008-07-28 19:34:53 +0000806** Configuration settings for an individual database connection
807*/
808int sqlite3_db_config(sqlite3 *db, int op, ...){
809 va_list ap;
drh6480aad2008-08-01 16:31:14 +0000810 int rc;
drh633e6d52008-07-28 19:34:53 +0000811 va_start(ap, op);
812 switch( op ){
drhe9d1c722008-08-04 20:13:26 +0000813 case SQLITE_DBCONFIG_LOOKASIDE: {
814 void *pBuf = va_arg(ap, void*);
drh633e6d52008-07-28 19:34:53 +0000815 int sz = va_arg(ap, int);
816 int cnt = va_arg(ap, int);
drhe9d1c722008-08-04 20:13:26 +0000817 rc = setupLookaside(db, pBuf, sz, cnt);
drh633e6d52008-07-28 19:34:53 +0000818 break;
819 }
drh6480aad2008-08-01 16:31:14 +0000820 default: {
821 rc = SQLITE_ERROR;
822 break;
823 }
drh633e6d52008-07-28 19:34:53 +0000824 }
825 va_end(ap);
826 return rc;
827}
828
drha16313e2007-03-30 11:29:32 +0000829
830/*
drh9b5adfa2008-01-20 23:19:56 +0000831** Return true if the buffer z[0..n-1] contains all spaces.
832*/
833static int allSpaces(const char *z, int n){
drh5f3a3672008-04-15 04:02:40 +0000834 while( n>0 && z[n-1]==' ' ){ n--; }
drh9b5adfa2008-01-20 23:19:56 +0000835 return n==0;
836}
837
838/*
drhd3d39e92004-05-20 22:16:29 +0000839** This is the default collating function named "BINARY" which is always
840** available.
drh9b5adfa2008-01-20 23:19:56 +0000841**
842** If the padFlag argument is not NULL then space padding at the end
843** of strings is ignored. This implements the RTRIM collation.
drhd3d39e92004-05-20 22:16:29 +0000844*/
danielk19772c336542005-01-13 02:14:23 +0000845static int binCollFunc(
drh9b5adfa2008-01-20 23:19:56 +0000846 void *padFlag,
drhd3d39e92004-05-20 22:16:29 +0000847 int nKey1, const void *pKey1,
848 int nKey2, const void *pKey2
849){
850 int rc, n;
851 n = nKey1<nKey2 ? nKey1 : nKey2;
852 rc = memcmp(pKey1, pKey2, n);
853 if( rc==0 ){
drh9b5adfa2008-01-20 23:19:56 +0000854 if( padFlag
855 && allSpaces(((char*)pKey1)+n, nKey1-n)
856 && allSpaces(((char*)pKey2)+n, nKey2-n)
857 ){
858 /* Leave rc unchanged at 0 */
859 }else{
860 rc = nKey1 - nKey2;
861 }
drhd3d39e92004-05-20 22:16:29 +0000862 }
863 return rc;
864}
865
866/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000867** Another built-in collating sequence: NOCASE.
868**
869** This collating sequence is intended to be used for "case independant
870** comparison". SQLite's knowledge of upper and lower case equivalents
871** extends only to the 26 characters used in the English language.
872**
873** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000874*/
875static int nocaseCollatingFunc(
876 void *NotUsed,
877 int nKey1, const void *pKey1,
878 int nKey2, const void *pKey2
879){
880 int r = sqlite3StrNICmp(
drh208f80a2004-08-29 20:08:58 +0000881 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
danielk197762c14b32008-11-19 09:05:26 +0000882 UNUSED_PARAMETER(NotUsed);
danielk19770202b292004-06-09 09:55:16 +0000883 if( 0==r ){
884 r = nKey1-nKey2;
885 }
886 return r;
887}
888
889/*
drhaf9ff332002-01-16 21:00:27 +0000890** Return the ROWID of the most recent insert
891*/
drh9bb575f2004-09-06 17:24:11 +0000892sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
drhaf9ff332002-01-16 21:00:27 +0000893 return db->lastRowid;
894}
895
896/*
danielk197724b03fd2004-05-10 10:34:34 +0000897** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000898*/
drh9bb575f2004-09-06 17:24:11 +0000899int sqlite3_changes(sqlite3 *db){
drhc8d30ac2002-04-12 10:08:59 +0000900 return db->nChange;
901}
902
rdcf146a772004-02-25 22:51:06 +0000903/*
danielk1977b28af712004-06-21 06:50:26 +0000904** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000905*/
danielk1977b28af712004-06-21 06:50:26 +0000906int sqlite3_total_changes(sqlite3 *db){
907 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000908}
909
drhc8d30ac2002-04-12 10:08:59 +0000910/*
danielk1977fd7f0452008-12-17 17:30:26 +0000911** Close all open savepoints. This function only manipulates fields of the
912** database handle object, it does not close any savepoints that may be open
913** at the b-tree/pager level.
914*/
915void sqlite3CloseSavepoints(sqlite3 *db){
916 while( db->pSavepoint ){
917 Savepoint *pTmp = db->pSavepoint;
918 db->pSavepoint = pTmp->pNext;
919 sqlite3DbFree(db, pTmp);
920 }
921 db->nSavepoint = 0;
danielk1977bd434552009-03-18 10:33:00 +0000922 db->nStatement = 0;
danielk1977fd7f0452008-12-17 17:30:26 +0000923 db->isTransactionSavepoint = 0;
924}
925
926/*
drh50e5dad2001-09-15 00:57:28 +0000927** Close an existing SQLite database
928*/
drh9bb575f2004-09-06 17:24:11 +0000929int sqlite3_close(sqlite3 *db){
drh8e0a2f92002-02-23 23:45:45 +0000930 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000931 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000932
933 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000934 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000935 }
drh7e8b8482008-01-23 03:03:05 +0000936 if( !sqlite3SafetyCheckSickOrOk(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000937 return SQLITE_MISUSE;
938 }
drh605264d2007-08-21 15:13:19 +0000939 sqlite3_mutex_enter(db->mutex);
danielk1977e35ee192004-06-26 09:50:11 +0000940
drhffc13f62006-07-30 20:50:44 +0000941 sqlite3ResetInternalSchema(db, 0);
danielk1977a04a34f2007-04-16 15:06:25 +0000942
danielk197701256832007-04-18 14:24:32 +0000943 /* If a transaction is open, the ResetInternalSchema() call above
944 ** will not have called the xDisconnect() method on any virtual
945 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
946 ** call will do so. We need to do this before the check for active
947 ** SQL statements below, as the v-table implementation may be storing
948 ** some prepared statements internally.
949 */
950 sqlite3VtabRollback(db);
951
danielk1977a04a34f2007-04-16 15:06:25 +0000952 /* If there are any outstanding VMs, return SQLITE_BUSY. */
danielk197796d81f92004-06-19 03:33:57 +0000953 if( db->pVdbe ){
954 sqlite3Error(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:57 +0000955 "unable to close due to unfinalised statements");
drh27641702007-08-22 02:56:42 +0000956 sqlite3_mutex_leave(db->mutex);
danielk197796d81f92004-06-19 03:33:57 +0000957 return SQLITE_BUSY;
958 }
drh7e8b8482008-01-23 03:03:05 +0000959 assert( sqlite3SafetyCheckSickOrOk(db) );
danielk1977e0048402004-06-15 16:51:01 +0000960
danielk197704103022009-02-03 16:51:24 +0000961 for(j=0; j<db->nDb; j++){
962 Btree *pBt = db->aDb[j].pBt;
963 if( pBt && sqlite3BtreeIsInBackup(pBt) ){
964 sqlite3Error(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:57 +0000965 "unable to close due to unfinished backup operation");
danielk197704103022009-02-03 16:51:24 +0000966 sqlite3_mutex_leave(db->mutex);
967 return SQLITE_BUSY;
968 }
969 }
970
danielk1977fd7f0452008-12-17 17:30:26 +0000971 /* Free any outstanding Savepoint structures. */
972 sqlite3CloseSavepoints(db);
973
drh001bbcb2003-03-19 03:14:00 +0000974 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000975 struct Db *pDb = &db->aDb[j];
976 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000977 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000978 pDb->pBt = 0;
danielk1977311019b2006-01-10 07:14:23 +0000979 if( j!=1 ){
980 pDb->pSchema = 0;
981 }
drh113088e2003-03-20 01:16:58 +0000982 }
drhf57b3392001-10-08 13:22:32 +0000983 }
danielk19774adee202004-05-08 08:23:19 +0000984 sqlite3ResetInternalSchema(db, 0);
danielk1977404ca072009-03-16 13:19:36 +0000985
986 /* Tell the code in notify.c that the connection no longer holds any
987 ** locks and does not require any further unlock-notify callbacks.
988 */
989 sqlite3ConnectionClosed(db);
990
drh1c2d8412003-03-31 00:30:47 +0000991 assert( db->nDb<=2 );
992 assert( db->aDb==db->aDbStatic );
drh70a8ca32008-08-21 18:49:27 +0000993 for(j=0; j<ArraySize(db->aFunc.a); j++){
994 FuncDef *pNext, *pHash, *p;
995 for(p=db->aFunc.a[j]; p; p=pHash){
996 pHash = p->pHash;
997 while( p ){
998 pNext = p->pNext;
999 sqlite3DbFree(db, p);
1000 p = pNext;
1001 }
drh8e0a2f92002-02-23 23:45:45 +00001002 }
1003 }
danielk1977d8123362004-06-12 09:25:12 +00001004 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +00001005 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977a9808b32007-05-07 09:32:45 +00001006 /* Invoke any destructors registered for collation sequence user data. */
1007 for(j=0; j<3; j++){
1008 if( pColl[j].xDel ){
1009 pColl[j].xDel(pColl[j].pUser);
1010 }
1011 }
drh633e6d52008-07-28 19:34:53 +00001012 sqlite3DbFree(db, pColl);
danielk1977466be562004-06-10 02:16:01 +00001013 }
danielk1977d8123362004-06-12 09:25:12 +00001014 sqlite3HashClear(&db->aCollSeq);
drhb9bb7c12006-06-11 23:41:55 +00001015#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977d1ab1ba2006-06-15 04:28:13 +00001016 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
1017 Module *pMod = (Module *)sqliteHashData(i);
danielk1977832a58a2007-06-22 15:21:15 +00001018 if( pMod->xDestroy ){
1019 pMod->xDestroy(pMod->pAux);
1020 }
drh633e6d52008-07-28 19:34:53 +00001021 sqlite3DbFree(db, pMod);
danielk1977d1ab1ba2006-06-15 04:28:13 +00001022 }
drhb9bb7c12006-06-11 23:41:55 +00001023 sqlite3HashClear(&db->aModule);
1024#endif
danielk1977466be562004-06-10 02:16:01 +00001025
danielk19776622cce2004-05-20 11:00:52 +00001026 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +00001027 if( db->pErr ){
1028 sqlite3ValueFree(db->pErr);
1029 }
drhf1952c52006-06-08 15:48:00 +00001030 sqlite3CloseExtensions(db);
danielk197796d81f92004-06-19 03:33:57 +00001031
1032 db->magic = SQLITE_MAGIC_ERROR;
danielk1977311019b2006-01-10 07:14:23 +00001033
1034 /* The temp-database schema is allocated differently from the other schema
1035 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
1036 ** So it needs to be freed here. Todo: Why not roll the temp schema into
1037 ** the same sqliteMalloc() as the one that allocates the database
1038 ** structure?
1039 */
drh633e6d52008-07-28 19:34:53 +00001040 sqlite3DbFree(db, db->aDb[1].pSchema);
drh605264d2007-08-21 15:13:19 +00001041 sqlite3_mutex_leave(db->mutex);
drh7e8b8482008-01-23 03:03:05 +00001042 db->magic = SQLITE_MAGIC_CLOSED;
drh605264d2007-08-21 15:13:19 +00001043 sqlite3_mutex_free(db->mutex);
drh4150ebf2008-10-11 15:38:29 +00001044 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
drhe9d1c722008-08-04 20:13:26 +00001045 if( db->lookaside.bMalloced ){
1046 sqlite3_free(db->lookaside.pStart);
1047 }
drh17435752007-08-16 04:30:38 +00001048 sqlite3_free(db);
danielk197796d81f92004-06-19 03:33:57 +00001049 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +00001050}
1051
1052/*
drh001bbcb2003-03-19 03:14:00 +00001053** Rollback all database files.
1054*/
drh9bb575f2004-09-06 17:24:11 +00001055void sqlite3RollbackAll(sqlite3 *db){
drh001bbcb2003-03-19 03:14:00 +00001056 int i;
danielk1977f3f06bb2005-12-16 15:24:28 +00001057 int inTrans = 0;
drhe30f4422007-08-21 16:15:55 +00001058 assert( sqlite3_mutex_held(db->mutex) );
danielk19772d1d86f2008-06-20 14:59:51 +00001059 sqlite3BeginBenignMalloc();
drh001bbcb2003-03-19 03:14:00 +00001060 for(i=0; i<db->nDb; i++){
1061 if( db->aDb[i].pBt ){
danielk1977f3f06bb2005-12-16 15:24:28 +00001062 if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
1063 inTrans = 1;
1064 }
danielk19774adee202004-05-08 08:23:19 +00001065 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +00001066 db->aDb[i].inTrans = 0;
1067 }
1068 }
danielk1977a298e902006-06-22 09:53:48 +00001069 sqlite3VtabRollback(db);
danielk19772d1d86f2008-06-20 14:59:51 +00001070 sqlite3EndBenignMalloc();
danielk1977ae72d982007-10-03 08:46:44 +00001071
danielk197771fd80b2005-12-16 06:54:01 +00001072 if( db->flags&SQLITE_InternChanges ){
danielk1977ea4d9e22007-08-13 15:28:33 +00001073 sqlite3ExpirePreparedStatements(db);
danielk197771fd80b2005-12-16 06:54:01 +00001074 sqlite3ResetInternalSchema(db, 0);
1075 }
1076
dan1da40a32009-09-19 17:00:31 +00001077 /* Any deferred constraint violations have now been resolved. */
1078 db->nDeferredCons = 0;
1079
danielk197771fd80b2005-12-16 06:54:01 +00001080 /* If one has been configured, invoke the rollback-hook callback */
danielk1977f3f06bb2005-12-16 15:24:28 +00001081 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
danielk197771fd80b2005-12-16 06:54:01 +00001082 db->xRollbackCallback(db->pRollbackArg);
1083 }
drh001bbcb2003-03-19 03:14:00 +00001084}
1085
1086/*
drhc22bd472002-05-10 13:14:07 +00001087** Return a static string that describes the kind of error specified in the
1088** argument.
drh247be432002-05-10 05:44:55 +00001089*/
danielk1977f20b21c2004-05-31 23:56:42 +00001090const char *sqlite3ErrStr(int rc){
drh51c7d862009-04-27 18:46:06 +00001091 static const char* const aMsg[] = {
1092 /* SQLITE_OK */ "not an error",
1093 /* SQLITE_ERROR */ "SQL logic error or missing database",
1094 /* SQLITE_INTERNAL */ 0,
1095 /* SQLITE_PERM */ "access permission denied",
1096 /* SQLITE_ABORT */ "callback requested query abort",
1097 /* SQLITE_BUSY */ "database is locked",
1098 /* SQLITE_LOCKED */ "database table is locked",
1099 /* SQLITE_NOMEM */ "out of memory",
1100 /* SQLITE_READONLY */ "attempt to write a readonly database",
1101 /* SQLITE_INTERRUPT */ "interrupted",
1102 /* SQLITE_IOERR */ "disk I/O error",
1103 /* SQLITE_CORRUPT */ "database disk image is malformed",
1104 /* SQLITE_NOTFOUND */ 0,
1105 /* SQLITE_FULL */ "database or disk is full",
1106 /* SQLITE_CANTOPEN */ "unable to open database file",
1107 /* SQLITE_PROTOCOL */ 0,
1108 /* SQLITE_EMPTY */ "table contains no data",
1109 /* SQLITE_SCHEMA */ "database schema has changed",
drh4c8555f2009-06-25 01:47:11 +00001110 /* SQLITE_TOOBIG */ "string or blob too big",
drh51c7d862009-04-27 18:46:06 +00001111 /* SQLITE_CONSTRAINT */ "constraint failed",
1112 /* SQLITE_MISMATCH */ "datatype mismatch",
1113 /* SQLITE_MISUSE */ "library routine called out of sequence",
1114 /* SQLITE_NOLFS */ "large file support is disabled",
1115 /* SQLITE_AUTH */ "authorization denied",
1116 /* SQLITE_FORMAT */ "auxiliary database format error",
1117 /* SQLITE_RANGE */ "bind or column index out of range",
1118 /* SQLITE_NOTADB */ "file is encrypted or is not a database",
1119 };
1120 rc &= 0xff;
drhb2f9efc2009-05-06 19:03:13 +00001121 if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
drh51c7d862009-04-27 18:46:06 +00001122 return aMsg[rc];
1123 }else{
1124 return "unknown error";
drh247be432002-05-10 05:44:55 +00001125 }
drh247be432002-05-10 05:44:55 +00001126}
1127
1128/*
drh2dfbbca2000-07-28 14:32:48 +00001129** This routine implements a busy callback that sleeps and tries
1130** again until a timeout value is reached. The timeout value is
1131** an integer number of milliseconds passed in as the first
1132** argument.
1133*/
drhdaffd0e2001-04-11 14:28:42 +00001134static int sqliteDefaultBusyCallback(
drh97903fe2005-05-24 20:19:57 +00001135 void *ptr, /* Database connection */
drh2dfbbca2000-07-28 14:32:48 +00001136 int count /* Number of times table has been busy */
1137){
danielk197729bafea2008-06-26 10:41:19 +00001138#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
drhee570fa2005-04-28 12:06:05 +00001139 static const u8 delays[] =
1140 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
1141 static const u8 totals[] =
1142 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
drhd1bec472004-01-15 13:29:31 +00001143# define NDELAY (sizeof(delays)/sizeof(delays[0]))
danielk1977b4b47412007-08-17 15:53:36 +00001144 sqlite3 *db = (sqlite3 *)ptr;
1145 int timeout = db->busyTimeout;
drh97903fe2005-05-24 20:19:57 +00001146 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +00001147
drhee570fa2005-04-28 12:06:05 +00001148 assert( count>=0 );
1149 if( count < NDELAY ){
1150 delay = delays[count];
1151 prior = totals[count];
drhd1bec472004-01-15 13:29:31 +00001152 }else{
1153 delay = delays[NDELAY-1];
drh68cb6192005-05-06 22:05:56 +00001154 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
drh2dfbbca2000-07-28 14:32:48 +00001155 }
drhd1bec472004-01-15 13:29:31 +00001156 if( prior + delay > timeout ){
1157 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +00001158 if( delay<=0 ) return 0;
1159 }
danielk19772a6bdf62007-08-20 16:07:00 +00001160 sqlite3OsSleep(db->pVfs, delay*1000);
drh2dfbbca2000-07-28 14:32:48 +00001161 return 1;
1162#else
drh482ea182007-08-20 11:12:40 +00001163 sqlite3 *db = (sqlite3 *)ptr;
drh3f737082005-06-14 02:24:31 +00001164 int timeout = ((sqlite3 *)ptr)->busyTimeout;
drh2dfbbca2000-07-28 14:32:48 +00001165 if( (count+1)*1000 > timeout ){
1166 return 0;
1167 }
drh482ea182007-08-20 11:12:40 +00001168 sqlite3OsSleep(db->pVfs, 1000000);
drh2dfbbca2000-07-28 14:32:48 +00001169 return 1;
1170#endif
1171}
1172
1173/*
drha4afb652005-07-09 02:16:02 +00001174** Invoke the given busy handler.
1175**
1176** This routine is called when an operation failed with a lock.
1177** If this routine returns non-zero, the lock is retried. If it
1178** returns 0, the operation aborts with an SQLITE_BUSY error.
1179*/
1180int sqlite3InvokeBusyHandler(BusyHandler *p){
1181 int rc;
drh34004ce2008-07-11 16:15:17 +00001182 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
drha4afb652005-07-09 02:16:02 +00001183 rc = p->xFunc(p->pArg, p->nBusy);
1184 if( rc==0 ){
1185 p->nBusy = -1;
1186 }else{
1187 p->nBusy++;
1188 }
1189 return rc;
1190}
1191
1192/*
drh2dfbbca2000-07-28 14:32:48 +00001193** This routine sets the busy callback for an Sqlite database to the
1194** given callback function with the given argument.
1195*/
danielk1977f9d64d22004-06-19 08:18:07 +00001196int sqlite3_busy_handler(
1197 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +00001198 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +00001199 void *pArg
1200){
drhe30f4422007-08-21 16:15:55 +00001201 sqlite3_mutex_enter(db->mutex);
danielk197724162fe2004-06-04 06:22:00 +00001202 db->busyHandler.xFunc = xBusy;
1203 db->busyHandler.pArg = pArg;
drha4afb652005-07-09 02:16:02 +00001204 db->busyHandler.nBusy = 0;
drhe30f4422007-08-21 16:15:55 +00001205 sqlite3_mutex_leave(db->mutex);
danielk1977f9d64d22004-06-19 08:18:07 +00001206 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +00001207}
1208
danielk1977348bb5d2003-10-18 09:37:26 +00001209#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1210/*
1211** This routine sets the progress callback for an Sqlite database to the
1212** given callback function with the given argument. The progress callback will
1213** be invoked every nOps opcodes.
1214*/
danielk197724b03fd2004-05-10 10:34:34 +00001215void sqlite3_progress_handler(
drh9bb575f2004-09-06 17:24:11 +00001216 sqlite3 *db,
danielk1977348bb5d2003-10-18 09:37:26 +00001217 int nOps,
1218 int (*xProgress)(void*),
1219 void *pArg
1220){
drhbd0b1b52008-07-07 19:52:09 +00001221 sqlite3_mutex_enter(db->mutex);
1222 if( nOps>0 ){
1223 db->xProgress = xProgress;
1224 db->nProgressOps = nOps;
1225 db->pProgressArg = pArg;
1226 }else{
1227 db->xProgress = 0;
1228 db->nProgressOps = 0;
1229 db->pProgressArg = 0;
danielk1977348bb5d2003-10-18 09:37:26 +00001230 }
drhbd0b1b52008-07-07 19:52:09 +00001231 sqlite3_mutex_leave(db->mutex);
danielk1977348bb5d2003-10-18 09:37:26 +00001232}
1233#endif
1234
1235
drh2dfbbca2000-07-28 14:32:48 +00001236/*
1237** This routine installs a default busy handler that waits for the
1238** specified number of milliseconds before returning 0.
1239*/
danielk1977f9d64d22004-06-19 08:18:07 +00001240int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +00001241 if( ms>0 ){
drh97903fe2005-05-24 20:19:57 +00001242 db->busyTimeout = ms;
1243 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
drh2dfbbca2000-07-28 14:32:48 +00001244 }else{
danielk197724b03fd2004-05-10 10:34:34 +00001245 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +00001246 }
danielk1977f9d64d22004-06-19 08:18:07 +00001247 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +00001248}
drh4c504392000-10-16 22:06:40 +00001249
1250/*
1251** Cause any pending operation to stop at its earliest opportunity.
1252*/
drh9bb575f2004-09-06 17:24:11 +00001253void sqlite3_interrupt(sqlite3 *db){
drhbd0b1b52008-07-07 19:52:09 +00001254 db->u1.isInterrupted = 1;
drh4c504392000-10-16 22:06:40 +00001255}
drhfa86c412002-02-02 15:01:15 +00001256
drhfa86c412002-02-02 15:01:15 +00001257
1258/*
danielk1977771151b2006-01-17 13:21:40 +00001259** This function is exactly the same as sqlite3_create_function(), except
1260** that it is designed to be called by internal code. The difference is
1261** that if a malloc() fails in sqlite3_create_function(), an error code
1262** is returned and the mallocFailed flag cleared.
drhfa86c412002-02-02 15:01:15 +00001263*/
danielk1977771151b2006-01-17 13:21:40 +00001264int sqlite3CreateFunc(
danielk197765904932004-05-26 06:18:37 +00001265 sqlite3 *db,
1266 const char *zFunctionName,
1267 int nArg,
danielk1977d8123362004-06-12 09:25:12 +00001268 int enc,
danielk197765904932004-05-26 06:18:37 +00001269 void *pUserData,
1270 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1271 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1272 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +00001273){
drh0bce8352002-02-28 00:41:10 +00001274 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +00001275 int nName;
danielk197765904932004-05-26 06:18:37 +00001276
drhe30f4422007-08-21 16:15:55 +00001277 assert( sqlite3_mutex_held(db->mutex) );
drhc60d0442004-09-30 13:43:13 +00001278 if( zFunctionName==0 ||
danielk1977398eae72004-05-26 06:58:43 +00001279 (xFunc && (xFinal || xStep)) ||
1280 (!xFunc && (xFinal && !xStep)) ||
1281 (!xFunc && (!xFinal && xStep)) ||
drh24b58dd2008-07-07 14:50:14 +00001282 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
drhdee0e402009-05-03 20:23:53 +00001283 (255<(nName = sqlite3Strlen30( zFunctionName))) ){
drhdff6c172009-05-07 13:43:49 +00001284 return SQLITE_MISUSE;
danielk197765904932004-05-26 06:18:37 +00001285 }
danielk1977d8123362004-06-12 09:25:12 +00001286
danielk197793758c82005-01-21 08:13:14 +00001287#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00001288 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1289 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1290 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1291 **
1292 ** If SQLITE_ANY is specified, add three versions of the function
1293 ** to the hash table.
1294 */
1295 if( enc==SQLITE_UTF16 ){
1296 enc = SQLITE_UTF16NATIVE;
1297 }else if( enc==SQLITE_ANY ){
1298 int rc;
danielk1977771151b2006-01-17 13:21:40 +00001299 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +00001300 pUserData, xFunc, xStep, xFinal);
drhe30f4422007-08-21 16:15:55 +00001301 if( rc==SQLITE_OK ){
1302 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
1303 pUserData, xFunc, xStep, xFinal);
1304 }
1305 if( rc!=SQLITE_OK ){
1306 return rc;
1307 }
danielk1977d8123362004-06-12 09:25:12 +00001308 enc = SQLITE_UTF16BE;
1309 }
danielk197793758c82005-01-21 08:13:14 +00001310#else
1311 enc = SQLITE_UTF8;
1312#endif
danielk19779636c4e2005-01-25 04:27:54 +00001313
1314 /* Check if an existing function is being overridden or deleted. If so,
1315 ** and there are active VMs, then return SQLITE_BUSY. If a function
1316 ** is being overridden/deleted but there are no active VMs, allow the
1317 ** operation to continue but invalidate all precompiled statements.
1318 */
drh3abbd392008-12-10 23:04:13 +00001319 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
danielk19779636c4e2005-01-25 04:27:54 +00001320 if( p && p->iPrefEnc==enc && p->nArg==nArg ){
1321 if( db->activeVdbeCnt ){
1322 sqlite3Error(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:57 +00001323 "unable to delete/modify user-function due to active statements");
drh17435752007-08-16 04:30:38 +00001324 assert( !db->mallocFailed );
danielk19779636c4e2005-01-25 04:27:54 +00001325 return SQLITE_BUSY;
1326 }else{
1327 sqlite3ExpirePreparedStatements(db);
1328 }
1329 }
danielk197765904932004-05-26 06:18:37 +00001330
drh3abbd392008-12-10 23:04:13 +00001331 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
danielk1977fa18bec2007-09-03 11:04:22 +00001332 assert(p || db->mallocFailed);
1333 if( !p ){
1334 return SQLITE_NOMEM;
danielk1977771151b2006-01-17 13:21:40 +00001335 }
danielk1977fa18bec2007-09-03 11:04:22 +00001336 p->flags = 0;
1337 p->xFunc = xFunc;
1338 p->xStep = xStep;
1339 p->xFinalize = xFinal;
1340 p->pUserData = pUserData;
drh1bd10f82008-12-10 21:19:56 +00001341 p->nArg = (u16)nArg;
danielk197765904932004-05-26 06:18:37 +00001342 return SQLITE_OK;
1343}
danielk1977771151b2006-01-17 13:21:40 +00001344
1345/*
1346** Create new user functions.
1347*/
1348int sqlite3_create_function(
1349 sqlite3 *db,
1350 const char *zFunctionName,
1351 int nArg,
1352 int enc,
1353 void *p,
1354 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
1355 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
1356 void (*xFinal)(sqlite3_context*)
1357){
1358 int rc;
drhe30f4422007-08-21 16:15:55 +00001359 sqlite3_mutex_enter(db->mutex);
danielk1977771151b2006-01-17 13:21:40 +00001360 rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
drhe30f4422007-08-21 16:15:55 +00001361 rc = sqlite3ApiExit(db, rc);
1362 sqlite3_mutex_leave(db->mutex);
1363 return rc;
danielk1977771151b2006-01-17 13:21:40 +00001364}
1365
danielk197793758c82005-01-21 08:13:14 +00001366#ifndef SQLITE_OMIT_UTF16
danielk197765904932004-05-26 06:18:37 +00001367int sqlite3_create_function16(
1368 sqlite3 *db,
1369 const void *zFunctionName,
1370 int nArg,
1371 int eTextRep,
danielk1977771151b2006-01-17 13:21:40 +00001372 void *p,
danielk197765904932004-05-26 06:18:37 +00001373 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
1374 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
1375 void (*xFinal)(sqlite3_context*)
1376){
1377 int rc;
drhaf9a7c22005-12-15 03:04:10 +00001378 char *zFunc8;
drhe30f4422007-08-21 16:15:55 +00001379 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:38 +00001380 assert( !db->mallocFailed );
danielk19771e536952007-08-16 10:09:01 +00001381 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
danielk1977771151b2006-01-17 13:21:40 +00001382 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
drh633e6d52008-07-28 19:34:53 +00001383 sqlite3DbFree(db, zFunc8);
drhe30f4422007-08-21 16:15:55 +00001384 rc = sqlite3ApiExit(db, rc);
1385 sqlite3_mutex_leave(db->mutex);
1386 return rc;
drh8e0a2f92002-02-23 23:45:45 +00001387}
danielk197793758c82005-01-21 08:13:14 +00001388#endif
drhc9b84a12002-06-20 11:36:48 +00001389
drhb7481e72006-09-16 21:45:14 +00001390
1391/*
1392** Declare that a function has been overloaded by a virtual table.
1393**
1394** If the function already exists as a regular global function, then
1395** this routine is a no-op. If the function does not exist, then create
1396** a new one that always throws a run-time error.
1397**
1398** When virtual tables intend to provide an overloaded function, they
1399** should call this routine to make sure the global function exists.
1400** A global function must exist in order for name resolution to work
1401** properly.
1402*/
1403int sqlite3_overload_function(
1404 sqlite3 *db,
1405 const char *zName,
1406 int nArg
1407){
drhdee0e402009-05-03 20:23:53 +00001408 int nName = sqlite3Strlen30(zName);
drhe30f4422007-08-21 16:15:55 +00001409 int rc;
1410 sqlite3_mutex_enter(db->mutex);
drhb7481e72006-09-16 21:45:14 +00001411 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
1412 sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
1413 0, sqlite3InvalidFunction, 0, 0);
1414 }
drhe30f4422007-08-21 16:15:55 +00001415 rc = sqlite3ApiExit(db, SQLITE_OK);
1416 sqlite3_mutex_leave(db->mutex);
1417 return rc;
drhb7481e72006-09-16 21:45:14 +00001418}
1419
drh19e2d372005-08-29 23:00:03 +00001420#ifndef SQLITE_OMIT_TRACE
drhc9b84a12002-06-20 11:36:48 +00001421/*
drh18de4822003-01-16 16:28:53 +00001422** Register a trace function. The pArg from the previously registered trace
1423** is returned.
1424**
1425** A NULL trace function means that no tracing is executes. A non-NULL
1426** trace is a pointer to a function that is invoked at the start of each
drh19e2d372005-08-29 23:00:03 +00001427** SQL statement.
drh18de4822003-01-16 16:28:53 +00001428*/
drh9bb575f2004-09-06 17:24:11 +00001429void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
drhe30f4422007-08-21 16:15:55 +00001430 void *pOld;
1431 sqlite3_mutex_enter(db->mutex);
1432 pOld = db->pTraceArg;
drh18de4822003-01-16 16:28:53 +00001433 db->xTrace = xTrace;
1434 db->pTraceArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001435 sqlite3_mutex_leave(db->mutex);
drh18de4822003-01-16 16:28:53 +00001436 return pOld;
drh0d1a6432003-04-03 15:46:04 +00001437}
drh19e2d372005-08-29 23:00:03 +00001438/*
1439** Register a profile function. The pArg from the previously registered
1440** profile function is returned.
1441**
1442** A NULL profile function means that no profiling is executes. A non-NULL
1443** profile is a pointer to a function that is invoked at the conclusion of
1444** each SQL statement that is run.
1445*/
1446void *sqlite3_profile(
1447 sqlite3 *db,
1448 void (*xProfile)(void*,const char*,sqlite_uint64),
1449 void *pArg
1450){
drhe30f4422007-08-21 16:15:55 +00001451 void *pOld;
1452 sqlite3_mutex_enter(db->mutex);
1453 pOld = db->pProfileArg;
drh19e2d372005-08-29 23:00:03 +00001454 db->xProfile = xProfile;
1455 db->pProfileArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001456 sqlite3_mutex_leave(db->mutex);
drh19e2d372005-08-29 23:00:03 +00001457 return pOld;
1458}
1459#endif /* SQLITE_OMIT_TRACE */
paulb0208cc2003-04-13 18:26:49 +00001460
drhaa940ea2004-01-15 02:44:03 +00001461/*** EXPERIMENTAL ***
1462**
1463** Register a function to be invoked when a transaction comments.
danielk197771fd80b2005-12-16 06:54:01 +00001464** If the invoked function returns non-zero, then the commit becomes a
drhaa940ea2004-01-15 02:44:03 +00001465** rollback.
1466*/
danielk197724b03fd2004-05-10 10:34:34 +00001467void *sqlite3_commit_hook(
drh9bb575f2004-09-06 17:24:11 +00001468 sqlite3 *db, /* Attach the hook to this database */
drhaa940ea2004-01-15 02:44:03 +00001469 int (*xCallback)(void*), /* Function to invoke on each commit */
1470 void *pArg /* Argument to the function */
1471){
drhe30f4422007-08-21 16:15:55 +00001472 void *pOld;
1473 sqlite3_mutex_enter(db->mutex);
1474 pOld = db->pCommitArg;
drhaa940ea2004-01-15 02:44:03 +00001475 db->xCommitCallback = xCallback;
1476 db->pCommitArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001477 sqlite3_mutex_leave(db->mutex);
drhaa940ea2004-01-15 02:44:03 +00001478 return pOld;
1479}
1480
danielk197794eb6a12005-12-15 15:22:08 +00001481/*
1482** Register a callback to be invoked each time a row is updated,
1483** inserted or deleted using this database connection.
1484*/
danielk197771fd80b2005-12-16 06:54:01 +00001485void *sqlite3_update_hook(
danielk197794eb6a12005-12-15 15:22:08 +00001486 sqlite3 *db, /* Attach the hook to this database */
1487 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
1488 void *pArg /* Argument to the function */
1489){
drhe30f4422007-08-21 16:15:55 +00001490 void *pRet;
1491 sqlite3_mutex_enter(db->mutex);
1492 pRet = db->pUpdateArg;
danielk197794eb6a12005-12-15 15:22:08 +00001493 db->xUpdateCallback = xCallback;
1494 db->pUpdateArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001495 sqlite3_mutex_leave(db->mutex);
danielk197771fd80b2005-12-16 06:54:01 +00001496 return pRet;
danielk197794eb6a12005-12-15 15:22:08 +00001497}
1498
danielk197771fd80b2005-12-16 06:54:01 +00001499/*
1500** Register a callback to be invoked each time a transaction is rolled
1501** back by this database connection.
1502*/
1503void *sqlite3_rollback_hook(
1504 sqlite3 *db, /* Attach the hook to this database */
1505 void (*xCallback)(void*), /* Callback function */
1506 void *pArg /* Argument to the function */
1507){
drhe30f4422007-08-21 16:15:55 +00001508 void *pRet;
1509 sqlite3_mutex_enter(db->mutex);
1510 pRet = db->pRollbackArg;
danielk197771fd80b2005-12-16 06:54:01 +00001511 db->xRollbackCallback = xCallback;
1512 db->pRollbackArg = pArg;
drhe30f4422007-08-21 16:15:55 +00001513 sqlite3_mutex_leave(db->mutex);
danielk197771fd80b2005-12-16 06:54:01 +00001514 return pRet;
1515}
drhaa940ea2004-01-15 02:44:03 +00001516
paulb0208cc2003-04-13 18:26:49 +00001517/*
danielk1977d8293352009-04-30 09:10:37 +00001518** This function returns true if main-memory should be used instead of
1519** a temporary file for transient pager files and statement journals.
1520** The value returned depends on the value of db->temp_store (runtime
1521** parameter) and the compile time value of SQLITE_TEMP_STORE. The
1522** following table describes the relationship between these two values
1523** and this functions return value.
1524**
1525** SQLITE_TEMP_STORE db->temp_store Location of temporary database
1526** ----------------- -------------- ------------------------------
1527** 0 any file (return 0)
1528** 1 1 file (return 0)
1529** 1 2 memory (return 1)
1530** 1 0 file (return 0)
1531** 2 1 file (return 0)
1532** 2 2 memory (return 1)
1533** 2 0 memory (return 1)
1534** 3 any memory (return 1)
1535*/
drh1c514142009-04-30 12:25:10 +00001536int sqlite3TempInMemory(const sqlite3 *db){
danielk1977d8293352009-04-30 09:10:37 +00001537#if SQLITE_TEMP_STORE==1
1538 return ( db->temp_store==2 );
1539#endif
1540#if SQLITE_TEMP_STORE==2
1541 return ( db->temp_store!=1 );
1542#endif
1543#if SQLITE_TEMP_STORE==3
1544 return 1;
shanecf697392009-06-01 16:53:09 +00001545#endif
1546#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
danielk1977d8293352009-04-30 09:10:37 +00001547 return 0;
shane60a4b532009-05-06 18:57:09 +00001548#endif
danielk1977d8293352009-04-30 09:10:37 +00001549}
1550
1551/*
drh13bff812003-04-15 01:19:47 +00001552** This routine is called to create a connection to a database BTree
1553** driver. If zFilename is the name of a file, then that file is
1554** opened and used. If zFilename is the magic name ":memory:" then
1555** the database is stored in memory (and is thus forgotten as soon as
1556** the connection is closed.) If zFilename is NULL then the database
drh84bfda42005-07-15 13:05:21 +00001557** is a "virtual" database for transient use only and is deleted as
1558** soon as the connection is closed.
drh90f5ecb2004-07-22 01:19:35 +00001559**
drh84bfda42005-07-15 13:05:21 +00001560** A virtual database can be either a disk file (that is automatically
danielk1977d8293352009-04-30 09:10:37 +00001561** deleted when the file is closed) or it an be held entirely in memory.
1562** The sqlite3TempInMemory() function is used to determine which.
paulb0208cc2003-04-13 18:26:49 +00001563*/
danielk19774adee202004-05-08 08:23:19 +00001564int sqlite3BtreeFactory(
drh1860e3f2009-11-04 13:30:01 +00001565 sqlite3 *db, /* Main database when opening aux otherwise 0 */
paulb0208cc2003-04-13 18:26:49 +00001566 const char *zFilename, /* Name of the file containing the BTree database */
1567 int omitJournal, /* if TRUE then do not journal this file */
1568 int nCache, /* How many pages in the page cache */
drh33f4e022007-09-03 15:19:34 +00001569 int vfsFlags, /* Flags passed through to vfsOpen */
danielk19774adee202004-05-08 08:23:19 +00001570 Btree **ppBtree /* Pointer to new Btree object written here */
1571){
drh33f4e022007-09-03 15:19:34 +00001572 int btFlags = 0;
drh90f5ecb2004-07-22 01:19:35 +00001573 int rc;
danielk19774adee202004-05-08 08:23:19 +00001574
drhe30f4422007-08-21 16:15:55 +00001575 assert( sqlite3_mutex_held(db->mutex) );
drheec983e2004-05-08 10:11:36 +00001576 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +00001577 if( omitJournal ){
drh33f4e022007-09-03 15:19:34 +00001578 btFlags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +00001579 }
drh7bec5052005-02-06 02:45:41 +00001580 if( db->flags & SQLITE_NoReadlock ){
drh33f4e022007-09-03 15:19:34 +00001581 btFlags |= BTREE_NO_READLOCK;
drh7bec5052005-02-06 02:45:41 +00001582 }
danielk197703aded42004-11-22 05:26:27 +00001583#ifndef SQLITE_OMIT_MEMORYDB
danielk1977d8293352009-04-30 09:10:37 +00001584 if( zFilename==0 && sqlite3TempInMemory(db) ){
drh22ac46d2004-08-14 18:34:54 +00001585 zFilename = ":memory:";
drh90f5ecb2004-07-22 01:19:35 +00001586 }
danielk1977d8293352009-04-30 09:10:37 +00001587#endif
danielk19774adee202004-05-08 08:23:19 +00001588
drh33f4e022007-09-03 15:19:34 +00001589 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
1590 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
1591 }
1592 rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
danielk1977171bfed2008-06-23 09:50:50 +00001593
1594 /* If the B-Tree was successfully opened, set the pager-cache size to the
1595 ** default value. Except, if the call to BtreeOpen() returned a handle
1596 ** open on an existing shared pager-cache, do not change the pager-cache
1597 ** size.
1598 */
1599 if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){
drh90f5ecb2004-07-22 01:19:35 +00001600 sqlite3BtreeSetCacheSize(*ppBtree, nCache);
1601 }
1602 return rc;
paulb0208cc2003-04-13 18:26:49 +00001603}
danielk19774adee202004-05-08 08:23:19 +00001604
danielk19774ad17132004-05-21 01:47:26 +00001605/*
1606** Return UTF-8 encoded English language explanation of the most recent
1607** error.
1608*/
danielk19776622cce2004-05-20 11:00:52 +00001609const char *sqlite3_errmsg(sqlite3 *db){
drhc60d0442004-09-30 13:43:13 +00001610 const char *z;
drhf49661a2008-12-10 16:45:50 +00001611 if( !db ){
danielk1977f20b21c2004-05-31 23:56:42 +00001612 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +00001613 }
drhd55d57e2008-07-07 17:53:07 +00001614 if( !sqlite3SafetyCheckSickOrOk(db) ){
danielk1977e35ee192004-06-26 09:50:11 +00001615 return sqlite3ErrStr(SQLITE_MISUSE);
1616 }
drh27641702007-08-22 02:56:42 +00001617 sqlite3_mutex_enter(db->mutex);
danielk1977238746a2009-03-19 18:51:06 +00001618 if( db->mallocFailed ){
1619 z = sqlite3ErrStr(SQLITE_NOMEM);
1620 }else{
1621 z = (char*)sqlite3_value_text(db->pErr);
1622 assert( !db->mallocFailed );
1623 if( z==0 ){
1624 z = sqlite3ErrStr(db->errCode);
1625 }
danielk19776622cce2004-05-20 11:00:52 +00001626 }
drhe30f4422007-08-21 16:15:55 +00001627 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00001628 return z;
danielk19776622cce2004-05-20 11:00:52 +00001629}
1630
drh6c626082004-11-14 21:56:29 +00001631#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00001632/*
1633** Return UTF-16 encoded English language explanation of the most recent
1634** error.
1635*/
danielk19776622cce2004-05-20 11:00:52 +00001636const void *sqlite3_errmsg16(sqlite3 *db){
danielk19777c5c3ca2009-02-23 14:42:53 +00001637 static const u16 outOfMem[] = {
1638 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
danielk1977bfd6cce2004-06-18 04:24:54 +00001639 };
danielk19777c5c3ca2009-02-23 14:42:53 +00001640 static const u16 misuse[] = {
1641 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
1642 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
1643 'c', 'a', 'l', 'l', 'e', 'd', ' ',
1644 'o', 'u', 't', ' ',
1645 'o', 'f', ' ',
1646 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
danielk1977e35ee192004-06-26 09:50:11 +00001647 };
danielk19774ad17132004-05-21 01:47:26 +00001648
drhc60d0442004-09-30 13:43:13 +00001649 const void *z;
danielk1977ae7fc492007-03-29 15:00:52 +00001650 if( !db ){
danielk19777c5c3ca2009-02-23 14:42:53 +00001651 return (void *)outOfMem;
drhc60d0442004-09-30 13:43:13 +00001652 }
drhd55d57e2008-07-07 17:53:07 +00001653 if( !sqlite3SafetyCheckSickOrOk(db) ){
danielk19777c5c3ca2009-02-23 14:42:53 +00001654 return (void *)misuse;
drhc60d0442004-09-30 13:43:13 +00001655 }
drhe30f4422007-08-21 16:15:55 +00001656 sqlite3_mutex_enter(db->mutex);
danielk1977238746a2009-03-19 18:51:06 +00001657 if( db->mallocFailed ){
1658 z = (void *)outOfMem;
1659 }else{
drhc60d0442004-09-30 13:43:13 +00001660 z = sqlite3_value_text16(db->pErr);
danielk1977238746a2009-03-19 18:51:06 +00001661 if( z==0 ){
1662 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
1663 SQLITE_UTF8, SQLITE_STATIC);
1664 z = sqlite3_value_text16(db->pErr);
1665 }
1666 /* A malloc() may have failed within the call to sqlite3_value_text16()
1667 ** above. If this is the case, then the db->mallocFailed flag needs to
1668 ** be cleared before returning. Do this directly, instead of via
1669 ** sqlite3ApiExit(), to avoid setting the database handle error message.
1670 */
1671 db->mallocFailed = 0;
drhc60d0442004-09-30 13:43:13 +00001672 }
drhe30f4422007-08-21 16:15:55 +00001673 sqlite3_mutex_leave(db->mutex);
drhc60d0442004-09-30 13:43:13 +00001674 return z;
danielk19776622cce2004-05-20 11:00:52 +00001675}
drh6c626082004-11-14 21:56:29 +00001676#endif /* SQLITE_OMIT_UTF16 */
danielk19776622cce2004-05-20 11:00:52 +00001677
drhc60d0442004-09-30 13:43:13 +00001678/*
danielk19777ddad962005-12-12 06:53:03 +00001679** Return the most recent error code generated by an SQLite routine. If NULL is
1680** passed to this function, we assume a malloc() failed during sqlite3_open().
drhc60d0442004-09-30 13:43:13 +00001681*/
danielk19776622cce2004-05-20 11:00:52 +00001682int sqlite3_errcode(sqlite3 *db){
danielk19777c36d072008-01-31 15:31:01 +00001683 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
drhc60d0442004-09-30 13:43:13 +00001684 return SQLITE_MISUSE;
1685 }
drh01495b92008-01-23 12:52:40 +00001686 if( !db || db->mallocFailed ){
1687 return SQLITE_NOMEM;
1688 }
drh4ac285a2006-09-15 07:28:50 +00001689 return db->errCode & db->errMask;
danielk19776622cce2004-05-20 11:00:52 +00001690}
drh99dfe5e2008-10-30 15:03:15 +00001691int sqlite3_extended_errcode(sqlite3 *db){
1692 if( db && !sqlite3SafetyCheckSickOrOk(db) ){
1693 return SQLITE_MISUSE;
1694 }
1695 if( !db || db->mallocFailed ){
1696 return SQLITE_NOMEM;
1697 }
1698 return db->errCode;
1699}
danielk19776622cce2004-05-20 11:00:52 +00001700
drh0e819f92006-02-01 13:50:41 +00001701/*
1702** Create a new collating function for database "db". The name is zName
1703** and the encoding is enc.
1704*/
danielk19779a30cf62006-01-18 04:26:07 +00001705static int createCollation(
drhf8d4e8b2009-08-20 02:49:30 +00001706 sqlite3* db,
danielk19779a30cf62006-01-18 04:26:07 +00001707 const char *zName,
shanecea72b22009-09-07 04:38:36 +00001708 u8 enc,
1709 u8 collType,
danielk19779a30cf62006-01-18 04:26:07 +00001710 void* pCtx,
danielk1977a9808b32007-05-07 09:32:45 +00001711 int(*xCompare)(void*,int,const void*,int,const void*),
1712 void(*xDel)(void*)
danielk19779a30cf62006-01-18 04:26:07 +00001713){
1714 CollSeq *pColl;
drh7d9bd4e2006-02-16 18:16:36 +00001715 int enc2;
drhc4a64fa2009-05-11 20:53:28 +00001716 int nName = sqlite3Strlen30(zName);
danielk19779a30cf62006-01-18 04:26:07 +00001717
drhe30f4422007-08-21 16:15:55 +00001718 assert( sqlite3_mutex_held(db->mutex) );
danielk19779a30cf62006-01-18 04:26:07 +00001719
1720 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1721 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1722 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1723 */
drh51c7d862009-04-27 18:46:06 +00001724 enc2 = enc;
danielk1977ebb32932009-04-28 15:35:38 +00001725 testcase( enc2==SQLITE_UTF16 );
1726 testcase( enc2==SQLITE_UTF16_ALIGNED );
1727 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
drh7d9bd4e2006-02-16 18:16:36 +00001728 enc2 = SQLITE_UTF16NATIVE;
danielk19779a30cf62006-01-18 04:26:07 +00001729 }
drh51c7d862009-04-27 18:46:06 +00001730 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
drhd55d57e2008-07-07 17:53:07 +00001731 return SQLITE_MISUSE;
danielk19779a30cf62006-01-18 04:26:07 +00001732 }
1733
1734 /* Check if this call is removing or replacing an existing collation
1735 ** sequence. If so, and there are active VMs, return busy. If there
1736 ** are no active VMs, invalidate any pre-compiled statements.
1737 */
drhc4a64fa2009-05-11 20:53:28 +00001738 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
danielk19779a30cf62006-01-18 04:26:07 +00001739 if( pColl && pColl->xCmp ){
1740 if( db->activeVdbeCnt ){
1741 sqlite3Error(db, SQLITE_BUSY,
drhb309bec2009-02-04 17:40:57 +00001742 "unable to delete/modify collation sequence due to active statements");
danielk19779a30cf62006-01-18 04:26:07 +00001743 return SQLITE_BUSY;
1744 }
1745 sqlite3ExpirePreparedStatements(db);
danielk1977a9808b32007-05-07 09:32:45 +00001746
1747 /* If collation sequence pColl was created directly by a call to
1748 ** sqlite3_create_collation, and not generated by synthCollSeq(),
1749 ** then any copies made by synthCollSeq() need to be invalidated.
1750 ** Also, collation destructor - CollSeq.xDel() - function may need
1751 ** to be called.
1752 */
1753 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
drh0a687d12008-07-08 14:52:07 +00001754 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977a9808b32007-05-07 09:32:45 +00001755 int j;
1756 for(j=0; j<3; j++){
1757 CollSeq *p = &aColl[j];
1758 if( p->enc==pColl->enc ){
1759 if( p->xDel ){
1760 p->xDel(p->pUser);
1761 }
1762 p->xCmp = 0;
1763 }
1764 }
1765 }
danielk19779a30cf62006-01-18 04:26:07 +00001766 }
1767
drhc4a64fa2009-05-11 20:53:28 +00001768 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
danielk19779a30cf62006-01-18 04:26:07 +00001769 if( pColl ){
1770 pColl->xCmp = xCompare;
1771 pColl->pUser = pCtx;
danielk1977a9808b32007-05-07 09:32:45 +00001772 pColl->xDel = xDel;
drh1bd10f82008-12-10 21:19:56 +00001773 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
drhf8d4e8b2009-08-20 02:49:30 +00001774 pColl->type = collType;
danielk19779a30cf62006-01-18 04:26:07 +00001775 }
1776 sqlite3Error(db, SQLITE_OK, 0);
1777 return SQLITE_OK;
1778}
1779
1780
danielk19776622cce2004-05-20 11:00:52 +00001781/*
drhbb4957f2008-03-20 14:03:29 +00001782** This array defines hard upper bounds on limit values. The
1783** initializer must be kept in sync with the SQLITE_LIMIT_*
1784** #defines in sqlite3.h.
1785*/
drhb1a6c3c2008-03-20 16:30:17 +00001786static const int aHardLimit[] = {
drhbb4957f2008-03-20 14:03:29 +00001787 SQLITE_MAX_LENGTH,
1788 SQLITE_MAX_SQL_LENGTH,
1789 SQLITE_MAX_COLUMN,
1790 SQLITE_MAX_EXPR_DEPTH,
1791 SQLITE_MAX_COMPOUND_SELECT,
1792 SQLITE_MAX_VDBE_OP,
1793 SQLITE_MAX_FUNCTION_ARG,
1794 SQLITE_MAX_ATTACHED,
drhbb4957f2008-03-20 14:03:29 +00001795 SQLITE_MAX_LIKE_PATTERN_LENGTH,
1796 SQLITE_MAX_VARIABLE_NUMBER,
drh417168a2009-09-07 18:14:02 +00001797 SQLITE_MAX_TRIGGER_DEPTH,
drhbb4957f2008-03-20 14:03:29 +00001798};
1799
1800/*
1801** Make sure the hard limits are set to reasonable values
1802*/
1803#if SQLITE_MAX_LENGTH<100
1804# error SQLITE_MAX_LENGTH must be at least 100
1805#endif
1806#if SQLITE_MAX_SQL_LENGTH<100
1807# error SQLITE_MAX_SQL_LENGTH must be at least 100
1808#endif
1809#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
1810# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
1811#endif
drhbb4957f2008-03-20 14:03:29 +00001812#if SQLITE_MAX_COMPOUND_SELECT<2
1813# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
1814#endif
1815#if SQLITE_MAX_VDBE_OP<40
1816# error SQLITE_MAX_VDBE_OP must be at least 40
1817#endif
drhe82f5d02008-10-07 19:53:14 +00001818#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
1819# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
drhbb4957f2008-03-20 14:03:29 +00001820#endif
danielk19777b7e23b2008-08-20 16:21:12 +00001821#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>30
1822# error SQLITE_MAX_ATTACHED must be between 0 and 30
drhbb4957f2008-03-20 14:03:29 +00001823#endif
drhbb4957f2008-03-20 14:03:29 +00001824#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
1825# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
1826#endif
drh70a8ca32008-08-21 18:49:27 +00001827#if SQLITE_MAX_COLUMN>32767
1828# error SQLITE_MAX_COLUMN must not exceed 32767
1829#endif
drh417168a2009-09-07 18:14:02 +00001830#if SQLITE_MAX_TRIGGER_DEPTH<1
1831# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
1832#endif
drhbb4957f2008-03-20 14:03:29 +00001833
1834
1835/*
1836** Change the value of a limit. Report the old value.
1837** If an invalid limit index is supplied, report -1.
1838** Make no changes but still report the old value if the
1839** new limit is negative.
1840**
1841** A new lower limit does not shrink existing constructs.
1842** It merely prevents new constructs that exceed the limit
1843** from forming.
1844*/
1845int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
1846 int oldLimit;
drh521cc842008-04-15 02:36:33 +00001847 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
drhbb4957f2008-03-20 14:03:29 +00001848 return -1;
1849 }
1850 oldLimit = db->aLimit[limitId];
1851 if( newLimit>=0 ){
drhf47ce562008-03-20 18:00:49 +00001852 if( newLimit>aHardLimit[limitId] ){
drhbb4957f2008-03-20 14:03:29 +00001853 newLimit = aHardLimit[limitId];
1854 }
1855 db->aLimit[limitId] = newLimit;
1856 }
1857 return oldLimit;
1858}
1859
1860/*
danielk19774ad17132004-05-21 01:47:26 +00001861** This routine does the work of opening a database on behalf of
1862** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
drhee570fa2005-04-28 12:06:05 +00001863** is UTF-8 encoded.
danielk19774ad17132004-05-21 01:47:26 +00001864*/
1865static int openDatabase(
1866 const char *zFilename, /* Database filename UTF-8 encoded */
drh605264d2007-08-21 15:13:19 +00001867 sqlite3 **ppDb, /* OUT: Returned database handle */
1868 unsigned flags, /* Operational flags */
1869 const char *zVfs /* Name of the VFS to use */
danielk19774ad17132004-05-21 01:47:26 +00001870){
1871 sqlite3 *db;
danielk1977da184232006-01-05 11:34:32 +00001872 int rc;
drh039963a2008-09-03 00:43:15 +00001873 int isThreadsafe;
danielk19774ad17132004-05-21 01:47:26 +00001874
drhe0d0f8e2009-04-28 04:47:31 +00001875 *ppDb = 0;
drh40257ff2008-06-13 18:24:27 +00001876#ifndef SQLITE_OMIT_AUTOINIT
1877 rc = sqlite3_initialize();
1878 if( rc ) return rc;
1879#endif
1880
drh039963a2008-09-03 00:43:15 +00001881 if( sqlite3GlobalConfig.bCoreMutex==0 ){
danielk19779a6284c2008-07-10 17:52:49 +00001882 isThreadsafe = 0;
drh039963a2008-09-03 00:43:15 +00001883 }else if( flags & SQLITE_OPEN_NOMUTEX ){
1884 isThreadsafe = 0;
1885 }else if( flags & SQLITE_OPEN_FULLMUTEX ){
1886 isThreadsafe = 1;
1887 }else{
1888 isThreadsafe = sqlite3GlobalConfig.bFullMutex;
danielk19779a6284c2008-07-10 17:52:49 +00001889 }
drhf1f12682009-09-09 14:17:52 +00001890 if( flags & SQLITE_OPEN_PRIVATECACHE ){
drhf4cfac92009-09-09 15:29:41 +00001891 flags &= ~SQLITE_OPEN_SHAREDCACHE;
drhf1f12682009-09-09 14:17:52 +00001892 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
1893 flags |= SQLITE_OPEN_SHAREDCACHE;
1894 }
danielk19779a6284c2008-07-10 17:52:49 +00001895
drhb25a9f42009-05-12 13:35:11 +00001896 /* Remove harmful bits from the flags parameter
1897 **
1898 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
1899 ** dealt with in the previous code block. Besides these, the only
1900 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
1901 ** SQLITE_OPEN_READWRITE, and SQLITE_OPEN_CREATE. Silently mask
1902 ** off all other flags.
1903 */
drha4267dc2008-02-19 15:20:44 +00001904 flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
drhb25a9f42009-05-12 13:35:11 +00001905 SQLITE_OPEN_EXCLUSIVE |
drha4267dc2008-02-19 15:20:44 +00001906 SQLITE_OPEN_MAIN_DB |
1907 SQLITE_OPEN_TEMP_DB |
1908 SQLITE_OPEN_TRANSIENT_DB |
1909 SQLITE_OPEN_MAIN_JOURNAL |
1910 SQLITE_OPEN_TEMP_JOURNAL |
1911 SQLITE_OPEN_SUBJOURNAL |
danielk19779a6284c2008-07-10 17:52:49 +00001912 SQLITE_OPEN_MASTER_JOURNAL |
drh039963a2008-09-03 00:43:15 +00001913 SQLITE_OPEN_NOMUTEX |
1914 SQLITE_OPEN_FULLMUTEX
drha4267dc2008-02-19 15:20:44 +00001915 );
1916
danielk19774ad17132004-05-21 01:47:26 +00001917 /* Allocate the sqlite data structure */
drh17435752007-08-16 04:30:38 +00001918 db = sqlite3MallocZero( sizeof(sqlite3) );
danielk19774ad17132004-05-21 01:47:26 +00001919 if( db==0 ) goto opendb_out;
drh039963a2008-09-03 00:43:15 +00001920 if( isThreadsafe ){
danielk197759f8c082008-06-18 17:09:10 +00001921 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
1922 if( db->mutex==0 ){
1923 sqlite3_free(db);
1924 db = 0;
1925 goto opendb_out;
1926 }
drh605264d2007-08-21 15:13:19 +00001927 }
drh27641702007-08-22 02:56:42 +00001928 sqlite3_mutex_enter(db->mutex);
drh4ac285a2006-09-15 07:28:50 +00001929 db->errMask = 0xff;
danielk19774ad17132004-05-21 01:47:26 +00001930 db->nDb = 2;
drh153c62c2007-08-24 03:51:33 +00001931 db->magic = SQLITE_MAGIC_BUSY;
danielk19774ad17132004-05-21 01:47:26 +00001932 db->aDb = db->aDbStatic;
drh633e6d52008-07-28 19:34:53 +00001933
drhbb4957f2008-03-20 14:03:29 +00001934 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
1935 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
danielk19771d850a72004-05-31 08:26:49 +00001936 db->autoCommit = 1;
drhddac25c2007-12-05 01:38:23 +00001937 db->nextAutovac = -1;
danielk1977f653d782008-03-20 11:04:21 +00001938 db->nextPagesize = 0;
drh76fe8032006-07-11 14:17:51 +00001939 db->flags |= SQLITE_ShortColNames
1940#if SQLITE_DEFAULT_FILE_FORMAT<4
1941 | SQLITE_LegacyFileFmt
1942#endif
drh56424db2007-03-27 22:24:11 +00001943#ifdef SQLITE_ENABLE_LOAD_EXTENSION
1944 | SQLITE_LoadExtension
1945#endif
dan5bde73c2009-09-01 17:11:07 +00001946#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
dan5e1a2782009-09-01 17:28:29 +00001947 | SQLITE_RecTriggers
dan76d462e2009-08-30 11:42:51 +00001948#endif
drh76fe8032006-07-11 14:17:51 +00001949 ;
drhe61922a2009-05-02 13:29:37 +00001950 sqlite3HashInit(&db->aCollSeq);
drhb9bb7c12006-06-11 23:41:55 +00001951#ifndef SQLITE_OMIT_VIRTUALTABLE
drhe61922a2009-05-02 13:29:37 +00001952 sqlite3HashInit(&db->aModule);
drhb9bb7c12006-06-11 23:41:55 +00001953#endif
danielk1977da184232006-01-05 11:34:32 +00001954
danielk197795c8a542007-09-01 06:51:27 +00001955 db->pVfs = sqlite3_vfs_find(zVfs);
1956 if( !db->pVfs ){
1957 rc = SQLITE_ERROR;
drhafc91042008-02-21 02:09:45 +00001958 sqlite3Error(db, rc, "no such vfs: %s", zVfs);
danielk197795c8a542007-09-01 06:51:27 +00001959 goto opendb_out;
1960 }
1961
danielk19770202b292004-06-09 09:55:16 +00001962 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1963 ** and UTF-16, so add a version for each to avoid any unnecessary
1964 ** conversions. The only error that can occur here is a malloc() failure.
1965 */
drhf8d4e8b2009-08-20 02:49:30 +00001966 createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
1967 binCollFunc, 0);
1968 createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
1969 binCollFunc, 0);
1970 createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
1971 binCollFunc, 0);
1972 createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
1973 binCollFunc, 0);
drh77db4c02008-03-18 13:01:38 +00001974 if( db->mallocFailed ){
danielk19770202b292004-06-09 09:55:16 +00001975 goto opendb_out;
1976 }
drhc4a64fa2009-05-11 20:53:28 +00001977 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
drh77db4c02008-03-18 13:01:38 +00001978 assert( db->pDfltColl!=0 );
danielk19770202b292004-06-09 09:55:16 +00001979
1980 /* Also add a UTF-8 case-insensitive collation sequence. */
drhf8d4e8b2009-08-20 02:49:30 +00001981 createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
1982 nocaseCollatingFunc, 0);
drhd64fe2f2005-08-28 17:00:23 +00001983
danielk19774ad17132004-05-21 01:47:26 +00001984 /* Open the backend database driver */
drh33f4e022007-09-03 15:19:34 +00001985 db->openFlags = flags;
1986 rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE,
1987 flags | SQLITE_OPEN_MAIN_DB,
drhc797d4d2007-05-08 01:08:49 +00001988 &db->aDb[0].pBt);
danielk19774ad17132004-05-21 01:47:26 +00001989 if( rc!=SQLITE_OK ){
danielk197745e60aa2008-09-23 17:39:26 +00001990 if( rc==SQLITE_IOERR_NOMEM ){
1991 rc = SQLITE_NOMEM;
1992 }
danielk19774ad17132004-05-21 01:47:26 +00001993 sqlite3Error(db, rc, 0);
danielk19774ad17132004-05-21 01:47:26 +00001994 goto opendb_out;
1995 }
drh17435752007-08-16 04:30:38 +00001996 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
1997 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
danielk197714db2662006-01-09 16:12:04 +00001998
drh03b808a2006-03-13 15:06:05 +00001999
danielk197753c0f742005-03-29 03:10:59 +00002000 /* The default safety_level for the main database is 'full'; for the temp
2001 ** database it is 'NONE'. This matches the pager layer defaults.
2002 */
2003 db->aDb[0].zName = "main";
danielk197791cf71b2004-06-26 06:37:06 +00002004 db->aDb[0].safety_level = 3;
danielk197753c0f742005-03-29 03:10:59 +00002005 db->aDb[1].zName = "temp";
danielk197791cf71b2004-06-26 06:37:06 +00002006 db->aDb[1].safety_level = 1;
danielk197753c0f742005-03-29 03:10:59 +00002007
danielk1977832a58a2007-06-22 15:21:15 +00002008 db->magic = SQLITE_MAGIC_OPEN;
drh17435752007-08-16 04:30:38 +00002009 if( db->mallocFailed ){
danielk1977832a58a2007-06-22 15:21:15 +00002010 goto opendb_out;
2011 }
2012
danielk19778e227872004-06-07 07:52:17 +00002013 /* Register all built-in functions, but do not attempt to read the
2014 ** database schema yet. This is delayed until the first time the database
2015 ** is accessed.
2016 */
danielk1977832a58a2007-06-22 15:21:15 +00002017 sqlite3Error(db, SQLITE_OK, 0);
2018 sqlite3RegisterBuiltinFunctions(db);
danielk19774ad17132004-05-21 01:47:26 +00002019
drh1409be62006-08-23 20:07:20 +00002020 /* Load automatic extensions - extensions that have been registered
2021 ** using the sqlite3_automatic_extension() API.
2022 */
drh7aaa8782009-05-20 02:40:45 +00002023 sqlite3AutoLoadExtensions(db);
2024 rc = sqlite3_errcode(db);
2025 if( rc!=SQLITE_OK ){
danielk1977832a58a2007-06-22 15:21:15 +00002026 goto opendb_out;
2027 }
drh1409be62006-08-23 20:07:20 +00002028
drhaa29c132006-09-02 13:58:06 +00002029#ifdef SQLITE_ENABLE_FTS1
drh17435752007-08-16 04:30:38 +00002030 if( !db->mallocFailed ){
drhaa29c132006-09-02 13:58:06 +00002031 extern int sqlite3Fts1Init(sqlite3*);
danielk1977832a58a2007-06-22 15:21:15 +00002032 rc = sqlite3Fts1Init(db);
drhaa29c132006-09-02 13:58:06 +00002033 }
2034#endif
2035
shessa26cf572006-10-19 20:27:58 +00002036#ifdef SQLITE_ENABLE_FTS2
drh17435752007-08-16 04:30:38 +00002037 if( !db->mallocFailed && rc==SQLITE_OK ){
shessa26cf572006-10-19 20:27:58 +00002038 extern int sqlite3Fts2Init(sqlite3*);
danielk1977832a58a2007-06-22 15:21:15 +00002039 rc = sqlite3Fts2Init(db);
shessa26cf572006-10-19 20:27:58 +00002040 }
2041#endif
2042
shess69c4ae22007-08-20 17:37:47 +00002043#ifdef SQLITE_ENABLE_FTS3
2044 if( !db->mallocFailed && rc==SQLITE_OK ){
shess69c4ae22007-08-20 17:37:47 +00002045 rc = sqlite3Fts3Init(db);
2046 }
2047#endif
2048
danielk197783852ac2007-05-06 16:04:11 +00002049#ifdef SQLITE_ENABLE_ICU
drh17435752007-08-16 04:30:38 +00002050 if( !db->mallocFailed && rc==SQLITE_OK ){
danielk1977832a58a2007-06-22 15:21:15 +00002051 rc = sqlite3IcuInit(db);
danielk197783852ac2007-05-06 16:04:11 +00002052 }
2053#endif
danielk1977ebaecc12008-05-26 18:41:54 +00002054
2055#ifdef SQLITE_ENABLE_RTREE
2056 if( !db->mallocFailed && rc==SQLITE_OK){
danielk1977ebaecc12008-05-26 18:41:54 +00002057 rc = sqlite3RtreeInit(db);
2058 }
2059#endif
2060
danielk1977832a58a2007-06-22 15:21:15 +00002061 sqlite3Error(db, rc, 0);
danielk197783852ac2007-05-06 16:04:11 +00002062
drh8fd5bd32007-04-02 16:40:37 +00002063 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
2064 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
2065 ** mode. Doing nothing at all also makes NORMAL the default.
2066 */
2067#ifdef SQLITE_DEFAULT_LOCKING_MODE
2068 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
2069 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
2070 SQLITE_DEFAULT_LOCKING_MODE);
2071#endif
2072
drh633e6d52008-07-28 19:34:53 +00002073 /* Enable the lookaside-malloc subsystem */
drh1b67f3c2008-10-10 17:41:28 +00002074 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
2075 sqlite3GlobalConfig.nLookaside);
drh633e6d52008-07-28 19:34:53 +00002076
danielk19774ad17132004-05-21 01:47:26 +00002077opendb_out:
drhafc91042008-02-21 02:09:45 +00002078 if( db ){
danielk1977075c23a2008-09-01 18:34:20 +00002079 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
drhf3a65f72007-08-22 20:18:21 +00002080 sqlite3_mutex_leave(db->mutex);
2081 }
danielk197759633ae2008-08-27 19:01:57 +00002082 rc = sqlite3_errcode(db);
2083 if( rc==SQLITE_NOMEM ){
danielk19777ddad962005-12-12 06:53:03 +00002084 sqlite3_close(db);
2085 db = 0;
danielk197759633ae2008-08-27 19:01:57 +00002086 }else if( rc!=SQLITE_OK ){
2087 db->magic = SQLITE_MAGIC_SICK;
danielk197713073932004-06-30 11:54:06 +00002088 }
danielk19774ad17132004-05-21 01:47:26 +00002089 *ppDb = db;
danielk197754f01982006-01-18 15:25:17 +00002090 return sqlite3ApiExit(0, rc);
danielk19774ad17132004-05-21 01:47:26 +00002091}
2092
2093/*
2094** Open a new database handle.
2095*/
danielk197780290862004-05-22 09:21:21 +00002096int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00002097 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00002098 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00002099){
drh605264d2007-08-21 15:13:19 +00002100 return openDatabase(zFilename, ppDb,
2101 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
2102}
2103int sqlite3_open_v2(
drh428e2822007-08-30 16:23:19 +00002104 const char *filename, /* Database filename (UTF-8) */
drh605264d2007-08-21 15:13:19 +00002105 sqlite3 **ppDb, /* OUT: SQLite db handle */
2106 int flags, /* Flags */
2107 const char *zVfs /* Name of VFS module to use */
2108){
2109 return openDatabase(filename, ppDb, flags, zVfs);
danielk197783ab5a82004-05-21 11:39:05 +00002110}
2111
drh6c626082004-11-14 21:56:29 +00002112#ifndef SQLITE_OMIT_UTF16
danielk19774ad17132004-05-21 01:47:26 +00002113/*
2114** Open a new database handle.
2115*/
2116int sqlite3_open16(
2117 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00002118 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00002119){
danielk1977bfd6cce2004-06-18 04:24:54 +00002120 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
danielk1977bfd6cce2004-06-18 04:24:54 +00002121 sqlite3_value *pVal;
drh40257ff2008-06-13 18:24:27 +00002122 int rc;
danielk19774ad17132004-05-21 01:47:26 +00002123
danielk19777ddad962005-12-12 06:53:03 +00002124 assert( zFilename );
danielk19774ad17132004-05-21 01:47:26 +00002125 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00002126 *ppDb = 0;
drh40257ff2008-06-13 18:24:27 +00002127#ifndef SQLITE_OMIT_AUTOINIT
2128 rc = sqlite3_initialize();
2129 if( rc ) return rc;
2130#endif
danielk19771e536952007-08-16 10:09:01 +00002131 pVal = sqlite3ValueNew(0);
drhb21c8cd2007-08-21 19:33:56 +00002132 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
2133 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
danielk1977bfd6cce2004-06-18 04:24:54 +00002134 if( zFilename8 ){
drh605264d2007-08-21 15:13:19 +00002135 rc = openDatabase(zFilename8, ppDb,
2136 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
drhafc91042008-02-21 02:09:45 +00002137 assert( *ppDb || rc==SQLITE_NOMEM );
danielk1977f51bf482008-04-28 16:19:35 +00002138 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
danielk1977fb103a82008-04-03 16:28:24 +00002139 ENC(*ppDb) = SQLITE_UTF16NATIVE;
danielk1977bfd6cce2004-06-18 04:24:54 +00002140 }
drh40257ff2008-06-13 18:24:27 +00002141 }else{
2142 rc = SQLITE_NOMEM;
danielk19774ad17132004-05-21 01:47:26 +00002143 }
danielk19777ddad962005-12-12 06:53:03 +00002144 sqlite3ValueFree(pVal);
danielk19778e227872004-06-07 07:52:17 +00002145
danielk197754f01982006-01-18 15:25:17 +00002146 return sqlite3ApiExit(0, rc);
danielk19774ad17132004-05-21 01:47:26 +00002147}
drh6c626082004-11-14 21:56:29 +00002148#endif /* SQLITE_OMIT_UTF16 */
danielk19774ad17132004-05-21 01:47:26 +00002149
danielk1977106bb232004-05-21 10:08:53 +00002150/*
danielk1977d8123362004-06-12 09:25:12 +00002151** Register a new collation sequence with the database handle db.
2152*/
danielk19770202b292004-06-09 09:55:16 +00002153int sqlite3_create_collation(
2154 sqlite3* db,
2155 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00002156 int enc,
danielk19770202b292004-06-09 09:55:16 +00002157 void* pCtx,
2158 int(*xCompare)(void*,int,const void*,int,const void*)
2159){
danielk19779a30cf62006-01-18 04:26:07 +00002160 int rc;
drhe30f4422007-08-21 16:15:55 +00002161 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:38 +00002162 assert( !db->mallocFailed );
shanecea72b22009-09-07 04:38:36 +00002163 rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
drhe30f4422007-08-21 16:15:55 +00002164 rc = sqlite3ApiExit(db, rc);
2165 sqlite3_mutex_leave(db->mutex);
2166 return rc;
danielk1977a9808b32007-05-07 09:32:45 +00002167}
2168
2169/*
2170** Register a new collation sequence with the database handle db.
2171*/
danielk1977a393c032007-05-07 14:58:53 +00002172int sqlite3_create_collation_v2(
danielk1977a9808b32007-05-07 09:32:45 +00002173 sqlite3* db,
2174 const char *zName,
2175 int enc,
2176 void* pCtx,
2177 int(*xCompare)(void*,int,const void*,int,const void*),
2178 void(*xDel)(void*)
2179){
2180 int rc;
drhe30f4422007-08-21 16:15:55 +00002181 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:38 +00002182 assert( !db->mallocFailed );
shanecea72b22009-09-07 04:38:36 +00002183 rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
drhe30f4422007-08-21 16:15:55 +00002184 rc = sqlite3ApiExit(db, rc);
2185 sqlite3_mutex_leave(db->mutex);
2186 return rc;
danielk19770202b292004-06-09 09:55:16 +00002187}
2188
drh6c626082004-11-14 21:56:29 +00002189#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00002190/*
2191** Register a new collation sequence with the database handle db.
2192*/
danielk19770202b292004-06-09 09:55:16 +00002193int sqlite3_create_collation16(
2194 sqlite3* db,
mihailimbda2e622008-06-23 11:23:14 +00002195 const void *zName,
danielk1977466be562004-06-10 02:16:01 +00002196 int enc,
danielk19770202b292004-06-09 09:55:16 +00002197 void* pCtx,
2198 int(*xCompare)(void*,int,const void*,int,const void*)
2199){
danielk19779a30cf62006-01-18 04:26:07 +00002200 int rc = SQLITE_OK;
drh01495b92008-01-23 12:52:40 +00002201 char *zName8;
drhe30f4422007-08-21 16:15:55 +00002202 sqlite3_mutex_enter(db->mutex);
drh17435752007-08-16 04:30:38 +00002203 assert( !db->mallocFailed );
danielk19771e536952007-08-16 10:09:01 +00002204 zName8 = sqlite3Utf16to8(db, zName, -1);
danielk19779a30cf62006-01-18 04:26:07 +00002205 if( zName8 ){
shanecea72b22009-09-07 04:38:36 +00002206 rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
drh633e6d52008-07-28 19:34:53 +00002207 sqlite3DbFree(db, zName8);
danielk19779a30cf62006-01-18 04:26:07 +00002208 }
drhe30f4422007-08-21 16:15:55 +00002209 rc = sqlite3ApiExit(db, rc);
2210 sqlite3_mutex_leave(db->mutex);
2211 return rc;
danielk19770202b292004-06-09 09:55:16 +00002212}
drh6c626082004-11-14 21:56:29 +00002213#endif /* SQLITE_OMIT_UTF16 */
danielk19777cedc8d2004-06-10 10:50:08 +00002214
danielk1977d8123362004-06-12 09:25:12 +00002215/*
2216** Register a collation sequence factory callback with the database handle
2217** db. Replace any previously installed collation sequence factory.
2218*/
danielk19777cedc8d2004-06-10 10:50:08 +00002219int sqlite3_collation_needed(
2220 sqlite3 *db,
2221 void *pCollNeededArg,
2222 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
2223){
drhe30f4422007-08-21 16:15:55 +00002224 sqlite3_mutex_enter(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00002225 db->xCollNeeded = xCollNeeded;
2226 db->xCollNeeded16 = 0;
2227 db->pCollNeededArg = pCollNeededArg;
drhe30f4422007-08-21 16:15:55 +00002228 sqlite3_mutex_leave(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00002229 return SQLITE_OK;
2230}
danielk1977d8123362004-06-12 09:25:12 +00002231
drh6c626082004-11-14 21:56:29 +00002232#ifndef SQLITE_OMIT_UTF16
danielk1977d8123362004-06-12 09:25:12 +00002233/*
2234** Register a collation sequence factory callback with the database handle
2235** db. Replace any previously installed collation sequence factory.
2236*/
danielk19777cedc8d2004-06-10 10:50:08 +00002237int sqlite3_collation_needed16(
2238 sqlite3 *db,
2239 void *pCollNeededArg,
2240 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
2241){
drhe30f4422007-08-21 16:15:55 +00002242 sqlite3_mutex_enter(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00002243 db->xCollNeeded = 0;
2244 db->xCollNeeded16 = xCollNeeded16;
2245 db->pCollNeededArg = pCollNeededArg;
drhe30f4422007-08-21 16:15:55 +00002246 sqlite3_mutex_leave(db->mutex);
danielk19777cedc8d2004-06-10 10:50:08 +00002247 return SQLITE_OK;
2248}
drh6c626082004-11-14 21:56:29 +00002249#endif /* SQLITE_OMIT_UTF16 */
danielk19776b456a22005-03-21 04:04:02 +00002250
2251#ifndef SQLITE_OMIT_GLOBALRECOVER
shaneeec556d2008-10-12 00:27:53 +00002252#ifndef SQLITE_OMIT_DEPRECATED
danielk19776b456a22005-03-21 04:04:02 +00002253/*
danielk19777ddad962005-12-12 06:53:03 +00002254** This function is now an anachronism. It used to be used to recover from a
2255** malloc() failure, but SQLite now does this automatically.
danielk19776b456a22005-03-21 04:04:02 +00002256*/
drh7d97efb2007-10-12 19:35:48 +00002257int sqlite3_global_recover(void){
danielk1977261919c2005-12-06 12:52:59 +00002258 return SQLITE_OK;
danielk19776b456a22005-03-21 04:04:02 +00002259}
2260#endif
shaneeec556d2008-10-12 00:27:53 +00002261#endif
drh3e1d8e62005-05-26 16:23:34 +00002262
2263/*
2264** Test to see whether or not the database connection is in autocommit
2265** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
2266** by default. Autocommit is disabled by a BEGIN statement and reenabled
2267** by the next COMMIT or ROLLBACK.
2268**
2269******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
2270*/
2271int sqlite3_get_autocommit(sqlite3 *db){
2272 return db->autoCommit;
2273}
drh49285702005-09-17 15:20:26 +00002274
2275#ifdef SQLITE_DEBUG
2276/*
2277** The following routine is subtituted for constant SQLITE_CORRUPT in
2278** debugging builds. This provides a way to set a breakpoint for when
2279** corruption is first detected.
2280*/
2281int sqlite3Corrupt(void){
2282 return SQLITE_CORRUPT;
2283}
2284#endif
drh7c1817e2006-01-10 13:58:48 +00002285
shaneeec556d2008-10-12 00:27:53 +00002286#ifndef SQLITE_OMIT_DEPRECATED
drh6f7adc82006-01-11 21:41:20 +00002287/*
2288** This is a convenience routine that makes sure that all thread-specific
2289** data for this thread has been deallocated.
drhdce8bdb2007-08-16 13:01:44 +00002290**
2291** SQLite no longer uses thread-specific data so this routine is now a
2292** no-op. It is retained for historical compatibility.
drh6f7adc82006-01-11 21:41:20 +00002293*/
2294void sqlite3_thread_cleanup(void){
drh6f7adc82006-01-11 21:41:20 +00002295}
shaneeec556d2008-10-12 00:27:53 +00002296#endif
danielk1977deb802c2006-02-09 13:43:28 +00002297
2298/*
2299** Return meta information about a specific column of a database table.
2300** See comment in sqlite3.h (sqlite.h.in) for details.
2301*/
2302#ifdef SQLITE_ENABLE_COLUMN_METADATA
2303int sqlite3_table_column_metadata(
2304 sqlite3 *db, /* Connection handle */
2305 const char *zDbName, /* Database name or NULL */
2306 const char *zTableName, /* Table name */
2307 const char *zColumnName, /* Column name */
2308 char const **pzDataType, /* OUTPUT: Declared data type */
2309 char const **pzCollSeq, /* OUTPUT: Collation sequence name */
2310 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
2311 int *pPrimaryKey, /* OUTPUT: True if column part of PK */
drh9be07162008-06-16 20:51:15 +00002312 int *pAutoinc /* OUTPUT: True if column is auto-increment */
danielk1977deb802c2006-02-09 13:43:28 +00002313){
2314 int rc;
2315 char *zErrMsg = 0;
2316 Table *pTab = 0;
2317 Column *pCol = 0;
2318 int iCol;
2319
2320 char const *zDataType = 0;
2321 char const *zCollSeq = 0;
2322 int notnull = 0;
2323 int primarykey = 0;
2324 int autoinc = 0;
2325
2326 /* Ensure the database schema has been loaded */
drhe30f4422007-08-21 16:15:55 +00002327 sqlite3_mutex_enter(db->mutex);
drha0af99f2008-04-16 00:49:12 +00002328 (void)sqlite3SafetyOn(db);
drh3cb3edc2008-03-07 21:37:19 +00002329 sqlite3BtreeEnterAll(db);
danielk1977deb802c2006-02-09 13:43:28 +00002330 rc = sqlite3Init(db, &zErrMsg);
2331 if( SQLITE_OK!=rc ){
2332 goto error_out;
2333 }
2334
2335 /* Locate the table in question */
2336 pTab = sqlite3FindTable(db, zTableName, zDbName);
2337 if( !pTab || pTab->pSelect ){
2338 pTab = 0;
2339 goto error_out;
2340 }
2341
2342 /* Find the column for which info is requested */
2343 if( sqlite3IsRowid(zColumnName) ){
2344 iCol = pTab->iPKey;
2345 if( iCol>=0 ){
2346 pCol = &pTab->aCol[iCol];
2347 }
2348 }else{
2349 for(iCol=0; iCol<pTab->nCol; iCol++){
2350 pCol = &pTab->aCol[iCol];
2351 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
2352 break;
2353 }
2354 }
2355 if( iCol==pTab->nCol ){
2356 pTab = 0;
2357 goto error_out;
2358 }
2359 }
2360
2361 /* The following block stores the meta information that will be returned
2362 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
2363 ** and autoinc. At this point there are two possibilities:
2364 **
2365 ** 1. The specified column name was rowid", "oid" or "_rowid_"
2366 ** and there is no explicitly declared IPK column.
2367 **
2368 ** 2. The table is not a view and the column name identified an
2369 ** explicitly declared column. Copy meta information from *pCol.
2370 */
2371 if( pCol ){
2372 zDataType = pCol->zType;
2373 zCollSeq = pCol->zColl;
drh9be07162008-06-16 20:51:15 +00002374 notnull = pCol->notNull!=0;
2375 primarykey = pCol->isPrimKey!=0;
drh7d10d5a2008-08-20 16:35:10 +00002376 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
danielk1977deb802c2006-02-09 13:43:28 +00002377 }else{
2378 zDataType = "INTEGER";
2379 primarykey = 1;
2380 }
2381 if( !zCollSeq ){
2382 zCollSeq = "BINARY";
2383 }
2384
2385error_out:
danielk197702b4e3b2009-02-26 07:15:59 +00002386 sqlite3BtreeLeaveAll(db);
drh01495b92008-01-23 12:52:40 +00002387 (void)sqlite3SafetyOff(db);
danielk1977deb802c2006-02-09 13:43:28 +00002388
2389 /* Whether the function call succeeded or failed, set the output parameters
2390 ** to whatever their local counterparts contain. If an error did occur,
2391 ** this has the effect of zeroing all output parameters.
2392 */
2393 if( pzDataType ) *pzDataType = zDataType;
2394 if( pzCollSeq ) *pzCollSeq = zCollSeq;
2395 if( pNotNull ) *pNotNull = notnull;
2396 if( pPrimaryKey ) *pPrimaryKey = primarykey;
2397 if( pAutoinc ) *pAutoinc = autoinc;
2398
2399 if( SQLITE_OK==rc && !pTab ){
drh633e6d52008-07-28 19:34:53 +00002400 sqlite3DbFree(db, zErrMsg);
drh118640b2008-07-16 14:02:32 +00002401 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
drhf089aa42008-07-08 19:34:06 +00002402 zColumnName);
danielk1977deb802c2006-02-09 13:43:28 +00002403 rc = SQLITE_ERROR;
2404 }
2405 sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
drh633e6d52008-07-28 19:34:53 +00002406 sqlite3DbFree(db, zErrMsg);
drhe30f4422007-08-21 16:15:55 +00002407 rc = sqlite3ApiExit(db, rc);
2408 sqlite3_mutex_leave(db->mutex);
drhf9cb7f52006-06-27 20:06:44 +00002409 return rc;
2410}
drhe30f4422007-08-21 16:15:55 +00002411#endif
drhf9cb7f52006-06-27 20:06:44 +00002412
2413/*
2414** Sleep for a little while. Return the amount of time slept.
2415*/
2416int sqlite3_sleep(int ms){
danielk1977b4b47412007-08-17 15:53:36 +00002417 sqlite3_vfs *pVfs;
drhe30f4422007-08-21 16:15:55 +00002418 int rc;
drhd677b3d2007-08-20 22:48:41 +00002419 pVfs = sqlite3_vfs_find(0);
drh40257ff2008-06-13 18:24:27 +00002420 if( pVfs==0 ) return 0;
danielk1977fee2d252007-08-18 10:59:19 +00002421
2422 /* This function works in milliseconds, but the underlying OsSleep()
2423 ** API uses microseconds. Hence the 1000's.
2424 */
drhe30f4422007-08-21 16:15:55 +00002425 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
drhe30f4422007-08-21 16:15:55 +00002426 return rc;
drhf9cb7f52006-06-27 20:06:44 +00002427}
drh4ac285a2006-09-15 07:28:50 +00002428
2429/*
2430** Enable or disable the extended result codes.
2431*/
2432int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
drhe30f4422007-08-21 16:15:55 +00002433 sqlite3_mutex_enter(db->mutex);
drh4ac285a2006-09-15 07:28:50 +00002434 db->errMask = onoff ? 0xffffffff : 0xff;
drhe30f4422007-08-21 16:15:55 +00002435 sqlite3_mutex_leave(db->mutex);
drh4ac285a2006-09-15 07:28:50 +00002436 return SQLITE_OK;
2437}
drhcc6bb3e2007-08-31 16:11:35 +00002438
2439/*
2440** Invoke the xFileControl method on a particular database.
2441*/
2442int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
2443 int rc = SQLITE_ERROR;
2444 int iDb;
2445 sqlite3_mutex_enter(db->mutex);
2446 if( zDbName==0 ){
2447 iDb = 0;
2448 }else{
2449 for(iDb=0; iDb<db->nDb; iDb++){
2450 if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
2451 }
2452 }
2453 if( iDb<db->nDb ){
2454 Btree *pBtree = db->aDb[iDb].pBt;
2455 if( pBtree ){
2456 Pager *pPager;
drh55176252008-01-22 14:50:16 +00002457 sqlite3_file *fd;
drhcc6bb3e2007-08-31 16:11:35 +00002458 sqlite3BtreeEnter(pBtree);
2459 pPager = sqlite3BtreePager(pBtree);
drh55176252008-01-22 14:50:16 +00002460 assert( pPager!=0 );
2461 fd = sqlite3PagerFile(pPager);
2462 assert( fd!=0 );
2463 if( fd->pMethods ){
2464 rc = sqlite3OsFileControl(fd, op, pArg);
drhcc6bb3e2007-08-31 16:11:35 +00002465 }
2466 sqlite3BtreeLeave(pBtree);
2467 }
2468 }
2469 sqlite3_mutex_leave(db->mutex);
2470 return rc;
2471}
drhed13d982008-01-31 14:43:24 +00002472
2473/*
2474** Interface to the testing logic.
2475*/
2476int sqlite3_test_control(int op, ...){
drhed13d982008-01-31 14:43:24 +00002477 int rc = 0;
drh3088d592008-03-21 16:45:47 +00002478#ifndef SQLITE_OMIT_BUILTIN_TEST
drh2fa18682008-03-19 14:15:34 +00002479 va_list ap;
drhed13d982008-01-31 14:43:24 +00002480 va_start(ap, op);
2481 switch( op ){
danielk1977d09414c2008-06-19 18:17:49 +00002482
2483 /*
drh984bfaa2008-03-19 16:08:53 +00002484 ** Save the current state of the PRNG.
2485 */
drh2fa18682008-03-19 14:15:34 +00002486 case SQLITE_TESTCTRL_PRNG_SAVE: {
2487 sqlite3PrngSaveState();
2488 break;
2489 }
drh984bfaa2008-03-19 16:08:53 +00002490
2491 /*
2492 ** Restore the state of the PRNG to the last state saved using
2493 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then
2494 ** this verb acts like PRNG_RESET.
2495 */
drh2fa18682008-03-19 14:15:34 +00002496 case SQLITE_TESTCTRL_PRNG_RESTORE: {
2497 sqlite3PrngRestoreState();
2498 break;
2499 }
drh984bfaa2008-03-19 16:08:53 +00002500
2501 /*
2502 ** Reset the PRNG back to its uninitialized state. The next call
2503 ** to sqlite3_randomness() will reseed the PRNG using a single call
2504 ** to the xRandomness method of the default VFS.
2505 */
drh2fa18682008-03-19 14:15:34 +00002506 case SQLITE_TESTCTRL_PRNG_RESET: {
2507 sqlite3PrngResetState();
2508 break;
2509 }
drh3088d592008-03-21 16:45:47 +00002510
2511 /*
2512 ** sqlite3_test_control(BITVEC_TEST, size, program)
2513 **
2514 ** Run a test against a Bitvec object of size. The program argument
2515 ** is an array of integers that defines the test. Return -1 on a
2516 ** memory allocation error, 0 on success, or non-zero for an error.
2517 ** See the sqlite3BitvecBuiltinTest() for additional information.
2518 */
2519 case SQLITE_TESTCTRL_BITVEC_TEST: {
2520 int sz = va_arg(ap, int);
2521 int *aProg = va_arg(ap, int*);
2522 rc = sqlite3BitvecBuiltinTest(sz, aProg);
2523 break;
2524 }
danielk19772d1d86f2008-06-20 14:59:51 +00002525
2526 /*
2527 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
2528 **
2529 ** Register hooks to call to indicate which malloc() failures
2530 ** are benign.
2531 */
2532 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
danielk1977ca026792008-06-23 14:15:52 +00002533 typedef void (*void_function)(void);
2534 void_function xBenignBegin;
2535 void_function xBenignEnd;
2536 xBenignBegin = va_arg(ap, void_function);
2537 xBenignEnd = va_arg(ap, void_function);
danielk19772d1d86f2008-06-20 14:59:51 +00002538 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
2539 break;
2540 }
drhc7a3bb92009-02-05 16:31:45 +00002541
2542 /*
drhf3af63f2009-05-09 18:59:42 +00002543 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
drhc7a3bb92009-02-05 16:31:45 +00002544 **
2545 ** Set the PENDING byte to the value in the argument, if X>0.
2546 ** Make no changes if X==0. Return the value of the pending byte
2547 ** as it existing before this routine was called.
2548 **
2549 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
2550 ** an incompatible database file format. Changing the PENDING byte
2551 ** while any database connection is open results in undefined and
2552 ** dileterious behavior.
2553 */
2554 case SQLITE_TESTCTRL_PENDING_BYTE: {
2555 unsigned int newVal = va_arg(ap, unsigned int);
2556 rc = sqlite3PendingByte;
2557 if( newVal ) sqlite3PendingByte = newVal;
2558 break;
2559 }
drhf3af63f2009-05-09 18:59:42 +00002560
2561 /*
2562 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
2563 **
2564 ** This action provides a run-time test to see whether or not
2565 ** assert() was enabled at compile-time. If X is true and assert()
2566 ** is enabled, then the return value is true. If X is true and
2567 ** assert() is disabled, then the return value is zero. If X is
2568 ** false and assert() is enabled, then the assertion fires and the
2569 ** process aborts. If X is false and assert() is disabled, then the
2570 ** return value is zero.
2571 */
2572 case SQLITE_TESTCTRL_ASSERT: {
2573 volatile int x = 0;
2574 assert( (x = va_arg(ap,int))!=0 );
2575 rc = x;
2576 break;
2577 }
2578
2579
2580 /*
2581 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
2582 **
2583 ** This action provides a run-time test to see how the ALWAYS and
2584 ** NEVER macros were defined at compile-time.
2585 **
2586 ** The return value is ALWAYS(X).
2587 **
2588 ** The recommended test is X==2. If the return value is 2, that means
2589 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
2590 ** default setting. If the return value is 1, then ALWAYS() is either
2591 ** hard-coded to true or else it asserts if its argument is false.
2592 ** The first behavior (hard-coded to true) is the case if
2593 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
2594 ** behavior (assert if the argument to ALWAYS() is false) is the case if
2595 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
2596 **
2597 ** The run-time test procedure might look something like this:
2598 **
2599 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
2600 ** // ALWAYS() and NEVER() are no-op pass-through macros
2601 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
2602 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
2603 ** }else{
2604 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
2605 ** }
2606 */
2607 case SQLITE_TESTCTRL_ALWAYS: {
2608 int x = va_arg(ap,int);
2609 rc = ALWAYS(x);
2610 break;
2611 }
drhc046e3e2009-07-15 11:26:44 +00002612
2613 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
2614 **
2615 ** Set the nReserve size to N for the main database on the database
2616 ** connection db.
2617 */
2618 case SQLITE_TESTCTRL_RESERVE: {
2619 sqlite3 *db = va_arg(ap, sqlite3*);
2620 int x = va_arg(ap,int);
2621 sqlite3_mutex_enter(db->mutex);
2622 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
2623 sqlite3_mutex_leave(db->mutex);
2624 break;
2625 }
2626
drh07096f62009-12-22 23:52:32 +00002627 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
2628 **
2629 ** Enable or disable various optimizations for testing purposes. The
2630 ** argument N is a bitmask of optimizations to be disabled. For normal
2631 ** operation N should be 0. The idea is that a test program (like the
2632 ** SQL Logic Test or SLT test module) can run the same SQL multiple times
2633 ** with various optimizations disabled to verify that the same answer
2634 ** is obtained in every case.
2635 */
2636 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
2637 sqlite3 *db = va_arg(ap, sqlite3*);
2638 int x = va_arg(ap,int);
2639 db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
2640 break;
2641 }
2642
drh0e857732010-01-02 03:21:35 +00002643#ifdef SQLITE_N_KEYWORD
2644 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
2645 **
2646 ** If zWord is a keyword recognized by the parser, then return the
2647 ** number of keywords. Or if zWord is not a keyword, return 0.
2648 **
2649 ** This test feature is only available in the amalgamation since
2650 ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
2651 ** is built using separate source files.
2652 */
2653 case SQLITE_TESTCTRL_ISKEYWORD: {
2654 const char *zWord = va_arg(ap, const char*);
2655 int n = sqlite3Strlen30(zWord);
drh855787a2010-01-02 03:46:43 +00002656 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
drh0e857732010-01-02 03:21:35 +00002657 break;
2658 }
2659#endif
2660
drhed13d982008-01-31 14:43:24 +00002661 }
2662 va_end(ap);
drh3088d592008-03-21 16:45:47 +00002663#endif /* SQLITE_OMIT_BUILTIN_TEST */
danielk19777c36d072008-01-31 15:31:01 +00002664 return rc;
drhed13d982008-01-31 14:43:24 +00002665}