2000-05-29 18:26:00 +04:00
|
|
|
/*
|
2001-09-16 04:13:26 +04:00
|
|
|
** 2001 September 15
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
2001-09-16 04:13:26 +04:00
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
2000-05-29 18:26:00 +04:00
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
** Main file for the SQLite library. The routines in this file
|
|
|
|
** implement the programmer interface to the library. Routines in
|
|
|
|
** other files are for internal use by SQLite and should not be
|
|
|
|
** accessed by users of the library.
|
|
|
|
*/
|
|
|
|
#include "sqliteInt.h"
|
2008-05-27 00:19:25 +04:00
|
|
|
|
2008-01-31 16:35:48 +03:00
|
|
|
#ifdef SQLITE_ENABLE_FTS3
|
|
|
|
# include "fts3.h"
|
|
|
|
#endif
|
2008-05-27 00:19:25 +04:00
|
|
|
#ifdef SQLITE_ENABLE_RTREE
|
|
|
|
# include "rtree.h"
|
|
|
|
#endif
|
2008-09-08 12:08:09 +04:00
|
|
|
#ifdef SQLITE_ENABLE_ICU
|
|
|
|
# include "sqliteicu.h"
|
|
|
|
#endif
|
2000-05-29 18:26:00 +04:00
|
|
|
|
2000-08-22 17:40:18 +04:00
|
|
|
/*
|
|
|
|
** The version of the library
|
|
|
|
*/
|
2008-12-09 00:37:14 +03:00
|
|
|
#ifndef SQLITE_AMALGAMATION
|
2004-05-10 14:34:34 +04:00
|
|
|
const char sqlite3_version[] = SQLITE_VERSION;
|
2008-12-09 00:37:14 +03:00
|
|
|
#endif
|
2004-08-28 20:19:00 +04:00
|
|
|
const char *sqlite3_libversion(void){ return sqlite3_version; }
|
2009-08-14 20:01:24 +04:00
|
|
|
const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
|
2005-02-05 10:33:34 +03:00
|
|
|
int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
|
2007-08-31 00:09:48 +04:00
|
|
|
int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
|
2000-08-22 17:40:18 +04:00
|
|
|
|
2008-05-01 21:03:49 +04:00
|
|
|
#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
|
2007-02-28 07:47:26 +03:00
|
|
|
/*
|
|
|
|
** If the following function pointer is not NULL and if
|
|
|
|
** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
|
|
|
|
** I/O active are written using this function. These messages
|
|
|
|
** are intended for debugging activity only.
|
|
|
|
*/
|
2008-03-04 20:45:01 +03:00
|
|
|
void (*sqlite3IoTrace)(const char*, ...) = 0;
|
2008-05-01 21:03:49 +04:00
|
|
|
#endif
|
2007-02-28 07:47:26 +03:00
|
|
|
|
2007-03-30 15:29:32 +04:00
|
|
|
/*
|
|
|
|
** If the following global variable points to a string which is the
|
|
|
|
** name of a directory, then that directory will be used to store
|
|
|
|
** temporary files.
|
|
|
|
**
|
|
|
|
** See also the "PRAGMA temp_store_directory" SQL command.
|
|
|
|
*/
|
|
|
|
char *sqlite3_temp_directory = 0;
|
|
|
|
|
2008-06-13 22:24:27 +04:00
|
|
|
/*
|
|
|
|
** Initialize SQLite.
|
|
|
|
**
|
|
|
|
** This routine must be called to initialize the memory allocation,
|
2008-08-12 19:21:11 +04:00
|
|
|
** VFS, and mutex subsystems prior to doing any serious work with
|
2008-06-13 22:24:27 +04:00
|
|
|
** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT
|
|
|
|
** this routine will be called automatically by key routines such as
|
|
|
|
** sqlite3_open().
|
|
|
|
**
|
|
|
|
** This routine is a no-op except on its very first call for the process,
|
|
|
|
** or for the first call after a call to sqlite3_shutdown.
|
2008-08-12 19:21:11 +04:00
|
|
|
**
|
|
|
|
** The first thread to call this routine runs the initialization to
|
|
|
|
** completion. If subsequent threads call this routine before the first
|
|
|
|
** thread has finished the initialization process, then the subsequent
|
|
|
|
** threads must block until the first thread finishes with the initialization.
|
|
|
|
**
|
|
|
|
** The first thread might call this routine recursively. Recursive
|
|
|
|
** calls to this routine should not block, of course. Otherwise the
|
|
|
|
** initialization process would never complete.
|
|
|
|
**
|
|
|
|
** Let X be the first thread to enter this routine. Let Y be some other
|
|
|
|
** thread. Then while the initial invocation of this routine by X is
|
|
|
|
** incomplete, it is required that:
|
|
|
|
**
|
|
|
|
** * Calls to this routine from Y must block until the outer-most
|
|
|
|
** call by X completes.
|
|
|
|
**
|
|
|
|
** * Recursive calls to this routine from thread X return immediately
|
|
|
|
** without blocking.
|
2008-06-13 22:24:27 +04:00
|
|
|
*/
|
|
|
|
int sqlite3_initialize(void){
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3_mutex *pMaster; /* The main static mutex */
|
|
|
|
int rc; /* Result code */
|
|
|
|
|
|
|
|
#ifdef SQLITE_OMIT_WSD
|
2008-09-02 20:22:28 +04:00
|
|
|
rc = sqlite3_wsd_init(4096, 24);
|
2008-09-01 22:34:20 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
2008-06-26 12:29:34 +04:00
|
|
|
|
2008-08-12 19:21:11 +04:00
|
|
|
/* If SQLite is already completely initialized, then this call
|
|
|
|
** to sqlite3_initialize() should be a no-op. But the initialization
|
|
|
|
** must be complete. So isInit must not be set until the very end
|
|
|
|
** of this routine.
|
|
|
|
*/
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
|
2008-06-26 12:29:34 +04:00
|
|
|
|
2008-08-12 19:21:11 +04:00
|
|
|
/* Make sure the mutex subsystem is initialized. If unable to
|
|
|
|
** initialize the mutex subsystem, return early with the error.
|
|
|
|
** If the system is so sick that we are unable to allocate a mutex,
|
|
|
|
** there is not much SQLite is going to be able to do.
|
|
|
|
**
|
|
|
|
** The mutex subsystem must take care of serializing its own
|
|
|
|
** initialization.
|
|
|
|
*/
|
2008-06-18 22:57:42 +04:00
|
|
|
rc = sqlite3MutexInit();
|
2008-08-12 19:21:11 +04:00
|
|
|
if( rc ) return rc;
|
2008-06-26 12:29:34 +04:00
|
|
|
|
2008-08-12 19:21:11 +04:00
|
|
|
/* Initialize the malloc() system and the recursive pInitMutex mutex.
|
|
|
|
** This operation is protected by the STATIC_MASTER mutex. Note that
|
|
|
|
** MutexAlloc() is called for a static mutex prior to initializing the
|
|
|
|
** malloc subsystem - this implies that the allocation of a static
|
|
|
|
** mutex must not require support from the malloc subsystem.
|
|
|
|
*/
|
|
|
|
pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
|
|
|
|
sqlite3_mutex_enter(pMaster);
|
2009-08-17 19:16:19 +04:00
|
|
|
sqlite3GlobalConfig.isMutexInit = 1;
|
2008-09-01 22:34:20 +04:00
|
|
|
if( !sqlite3GlobalConfig.isMallocInit ){
|
2008-08-12 19:21:11 +04:00
|
|
|
rc = sqlite3MallocInit();
|
|
|
|
}
|
2008-06-13 22:24:27 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.isMallocInit = 1;
|
|
|
|
if( !sqlite3GlobalConfig.pInitMutex ){
|
2009-08-17 17:42:29 +04:00
|
|
|
sqlite3GlobalConfig.pInitMutex =
|
|
|
|
sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
|
2008-08-12 19:21:11 +04:00
|
|
|
rc = SQLITE_NOMEM;
|
2008-06-13 22:24:27 +04:00
|
|
|
}
|
|
|
|
}
|
2008-09-22 21:22:19 +04:00
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.nRefInitMutex++;
|
2008-08-12 19:21:11 +04:00
|
|
|
}
|
|
|
|
sqlite3_mutex_leave(pMaster);
|
2008-06-26 12:29:34 +04:00
|
|
|
|
2009-08-17 19:16:19 +04:00
|
|
|
/* If rc is not SQLITE_OK at this point, then either the malloc
|
|
|
|
** subsystem could not be initialized or the system failed to allocate
|
|
|
|
** the pInitMutex mutex. Return an error in either case. */
|
2008-08-12 19:21:11 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the rest of the initialization under the recursive mutex so
|
|
|
|
** that we will be able to handle recursive calls into
|
|
|
|
** sqlite3_initialize(). The recursive calls normally come through
|
|
|
|
** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
|
|
|
|
** recursive calls might also be possible.
|
|
|
|
*/
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
|
2008-09-02 18:07:24 +04:00
|
|
|
if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
|
2008-09-01 22:34:20 +04:00
|
|
|
FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
|
2008-09-02 18:07:24 +04:00
|
|
|
sqlite3GlobalConfig.inProgress = 1;
|
2008-09-01 22:34:20 +04:00
|
|
|
memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
|
2008-08-21 22:49:27 +04:00
|
|
|
sqlite3RegisterGlobalFunctions();
|
2009-08-17 19:16:19 +04:00
|
|
|
if( sqlite3GlobalConfig.isPCacheInit==0 ){
|
|
|
|
rc = sqlite3PcacheInitialize();
|
|
|
|
}
|
2009-04-17 20:54:22 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2009-08-17 19:16:19 +04:00
|
|
|
sqlite3GlobalConfig.isPCacheInit = 1;
|
2009-08-17 19:52:25 +04:00
|
|
|
rc = sqlite3OsInit();
|
2009-04-17 20:54:22 +04:00
|
|
|
}
|
2008-08-20 18:49:23 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2008-09-02 13:38:06 +04:00
|
|
|
sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
|
|
|
|
sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
|
2009-04-17 20:54:22 +04:00
|
|
|
sqlite3GlobalConfig.isInit = 1;
|
2008-08-20 18:49:23 +04:00
|
|
|
}
|
2008-09-02 18:07:24 +04:00
|
|
|
sqlite3GlobalConfig.inProgress = 0;
|
2008-06-13 22:24:27 +04:00
|
|
|
}
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
|
2008-08-12 19:21:11 +04:00
|
|
|
|
|
|
|
/* Go back under the static mutex and clean up the recursive
|
|
|
|
** mutex to prevent a resource leak.
|
|
|
|
*/
|
|
|
|
sqlite3_mutex_enter(pMaster);
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.nRefInitMutex--;
|
|
|
|
if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
|
|
|
|
assert( sqlite3GlobalConfig.nRefInitMutex==0 );
|
|
|
|
sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
|
|
|
|
sqlite3GlobalConfig.pInitMutex = 0;
|
2008-08-12 19:21:11 +04:00
|
|
|
}
|
|
|
|
sqlite3_mutex_leave(pMaster);
|
2008-07-22 09:13:30 +04:00
|
|
|
|
2008-08-12 19:21:11 +04:00
|
|
|
/* The following is just a sanity check to make sure SQLite has
|
|
|
|
** been compiled correctly. It is important to run this code, but
|
|
|
|
** we don't want to run it too often and soak up CPU cycles for no
|
|
|
|
** reason. So we run it once during initialization.
|
|
|
|
*/
|
2008-07-22 09:13:30 +04:00
|
|
|
#ifndef NDEBUG
|
2009-02-04 06:59:25 +03:00
|
|
|
#ifndef SQLITE_OMIT_FLOATING_POINT
|
2008-07-22 09:13:30 +04:00
|
|
|
/* This section of code's only "output" is via assert() statements. */
|
|
|
|
if ( rc==SQLITE_OK ){
|
|
|
|
u64 x = (((u64)1)<<63)-1;
|
|
|
|
double y;
|
|
|
|
assert(sizeof(x)==8);
|
|
|
|
assert(sizeof(x)==sizeof(y));
|
|
|
|
memcpy(&y, &x, 8);
|
|
|
|
assert( sqlite3IsNaN(y) );
|
|
|
|
}
|
2009-02-04 06:59:25 +03:00
|
|
|
#endif
|
2008-07-22 09:13:30 +04:00
|
|
|
#endif
|
|
|
|
|
2008-06-13 22:24:27 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Undo the effects of sqlite3_initialize(). Must not be called while
|
|
|
|
** there are outstanding database connections or memory allocations or
|
|
|
|
** while any part of SQLite is otherwise in use in any thread. This
|
2009-04-19 16:23:58 +04:00
|
|
|
** routine is not threadsafe. But it is safe to invoke this routine
|
|
|
|
** on when SQLite is already shut down. If SQLite is already shut down
|
|
|
|
** when this routine is invoked, then this routine is a harmless no-op.
|
2008-06-13 22:24:27 +04:00
|
|
|
*/
|
|
|
|
int sqlite3_shutdown(void){
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.isInit ){
|
2009-04-21 16:02:56 +04:00
|
|
|
sqlite3_os_end();
|
2009-04-19 16:23:58 +04:00
|
|
|
sqlite3_reset_auto_extension();
|
2009-08-17 19:16:19 +04:00
|
|
|
sqlite3GlobalConfig.isInit = 0;
|
|
|
|
}
|
|
|
|
if( sqlite3GlobalConfig.isPCacheInit ){
|
|
|
|
sqlite3PcacheShutdown();
|
|
|
|
sqlite3GlobalConfig.isPCacheInit = 0;
|
|
|
|
}
|
|
|
|
if( sqlite3GlobalConfig.isMallocInit ){
|
2009-04-19 16:23:58 +04:00
|
|
|
sqlite3MallocEnd();
|
2009-08-17 19:16:19 +04:00
|
|
|
sqlite3GlobalConfig.isMallocInit = 0;
|
|
|
|
}
|
|
|
|
if( sqlite3GlobalConfig.isMutexInit ){
|
2009-04-19 16:23:58 +04:00
|
|
|
sqlite3MutexEnd();
|
2009-08-17 19:16:19 +04:00
|
|
|
sqlite3GlobalConfig.isMutexInit = 0;
|
2008-07-08 16:02:35 +04:00
|
|
|
}
|
2009-08-17 19:16:19 +04:00
|
|
|
|
2008-06-13 22:24:27 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This API allows applications to modify the global configuration of
|
|
|
|
** the SQLite library at run-time.
|
|
|
|
**
|
|
|
|
** This routine should only be called when there are no outstanding
|
|
|
|
** database connections or memory allocations. This routine is not
|
|
|
|
** threadsafe. Failure to heed these warnings can lead to unpredictable
|
|
|
|
** behavior.
|
|
|
|
*/
|
|
|
|
int sqlite3_config(int op, ...){
|
|
|
|
va_list ap;
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
|
|
|
/* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
|
|
|
|
** the SQLite library is in use. */
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE;
|
2008-06-13 22:24:27 +04:00
|
|
|
|
|
|
|
va_start(ap, op);
|
|
|
|
switch( op ){
|
2008-10-07 19:25:48 +04:00
|
|
|
|
|
|
|
/* Mutex configuration options are only available in a threadsafe
|
|
|
|
** compile.
|
|
|
|
*/
|
2009-12-11 06:44:18 +03:00
|
|
|
#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
|
2008-06-13 22:24:27 +04:00
|
|
|
case SQLITE_CONFIG_SINGLETHREAD: {
|
|
|
|
/* Disable all mutexing */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.bCoreMutex = 0;
|
|
|
|
sqlite3GlobalConfig.bFullMutex = 0;
|
2008-06-13 22:24:27 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_CONFIG_MULTITHREAD: {
|
|
|
|
/* Disable mutexing of database connections */
|
|
|
|
/* Enable mutexing of core data structures */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.bCoreMutex = 1;
|
|
|
|
sqlite3GlobalConfig.bFullMutex = 0;
|
2008-06-13 22:24:27 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_CONFIG_SERIALIZED: {
|
|
|
|
/* Enable all mutexing */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.bCoreMutex = 1;
|
|
|
|
sqlite3GlobalConfig.bFullMutex = 1;
|
2008-06-13 22:24:27 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-10-07 19:25:48 +04:00
|
|
|
case SQLITE_CONFIG_MUTEX: {
|
|
|
|
/* Specify an alternative mutex implementation */
|
|
|
|
sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_CONFIG_GETMUTEX: {
|
|
|
|
/* Retrieve the current mutex implementation */
|
|
|
|
*va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-06-13 22:24:27 +04:00
|
|
|
case SQLITE_CONFIG_MALLOC: {
|
|
|
|
/* Specify an alternative malloc implementation */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
|
2008-06-13 22:24:27 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-06-18 17:27:46 +04:00
|
|
|
case SQLITE_CONFIG_GETMALLOC: {
|
2008-06-24 23:02:55 +04:00
|
|
|
/* Retrieve the current malloc() implementation */
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
|
|
|
|
*va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
|
2008-06-18 17:27:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-06-14 20:56:21 +04:00
|
|
|
case SQLITE_CONFIG_MEMSTATUS: {
|
2008-06-13 22:24:27 +04:00
|
|
|
/* Enable or disable the malloc status collection */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
|
2008-06-13 22:24:27 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-06-18 17:27:46 +04:00
|
|
|
case SQLITE_CONFIG_SCRATCH: {
|
|
|
|
/* Designate a buffer for scratch memory space */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
|
|
|
|
sqlite3GlobalConfig.szScratch = va_arg(ap, int);
|
|
|
|
sqlite3GlobalConfig.nScratch = va_arg(ap, int);
|
2008-06-18 17:27:46 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_CONFIG_PAGECACHE: {
|
2009-05-22 14:53:29 +04:00
|
|
|
/* Designate a buffer for page cache memory space */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.pPage = va_arg(ap, void*);
|
|
|
|
sqlite3GlobalConfig.szPage = va_arg(ap, int);
|
|
|
|
sqlite3GlobalConfig.nPage = va_arg(ap, int);
|
2008-06-18 17:27:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-06-27 17:27:03 +04:00
|
|
|
|
2008-11-13 17:28:28 +03:00
|
|
|
case SQLITE_CONFIG_PCACHE: {
|
2009-05-22 14:53:29 +04:00
|
|
|
/* Specify an alternative page cache implementation */
|
2008-11-13 17:28:28 +03:00
|
|
|
sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-19 04:20:26 +03:00
|
|
|
case SQLITE_CONFIG_GETPCACHE: {
|
|
|
|
if( sqlite3GlobalConfig.pcache.xInit==0 ){
|
|
|
|
sqlite3PCacheSetDefault();
|
|
|
|
}
|
|
|
|
*va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-15 18:47:18 +04:00
|
|
|
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
|
2008-06-18 17:27:46 +04:00
|
|
|
case SQLITE_CONFIG_HEAP: {
|
2008-06-27 17:27:03 +04:00
|
|
|
/* Designate a buffer for heap memory space */
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
|
|
|
|
sqlite3GlobalConfig.nHeap = va_arg(ap, int);
|
|
|
|
sqlite3GlobalConfig.mnReq = va_arg(ap, int);
|
2008-06-27 17:27:03 +04:00
|
|
|
|
2008-09-01 22:34:20 +04:00
|
|
|
if( sqlite3GlobalConfig.pHeap==0 ){
|
2008-07-10 22:13:42 +04:00
|
|
|
/* If the heap pointer is NULL, then restore the malloc implementation
|
|
|
|
** back to NULL pointers too. This will cause the malloc to go
|
|
|
|
** back to its default implementation when sqlite3_initialize() is
|
|
|
|
** run.
|
|
|
|
*/
|
2008-09-01 22:34:20 +04:00
|
|
|
memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
|
2008-07-10 22:13:42 +04:00
|
|
|
}else{
|
|
|
|
/* The heap pointer is not NULL, then install one of the
|
|
|
|
** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
|
|
|
|
** ENABLE_MEMSYS5 is defined, return an error.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_ENABLE_MEMSYS3
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
|
2008-07-10 22:13:42 +04:00
|
|
|
#endif
|
2008-06-27 17:27:03 +04:00
|
|
|
#ifdef SQLITE_ENABLE_MEMSYS5
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
|
2008-07-10 22:13:42 +04:00
|
|
|
#endif
|
|
|
|
}
|
2008-06-18 17:27:46 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-07-15 18:47:18 +04:00
|
|
|
#endif
|
2008-06-27 17:27:03 +04:00
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
case SQLITE_CONFIG_LOOKASIDE: {
|
2008-09-01 22:34:20 +04:00
|
|
|
sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
|
|
|
|
sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
|
2008-07-28 23:34:53 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-13 22:24:27 +04:00
|
|
|
default: {
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
/*
|
|
|
|
** Set up the lookaside buffers for a database connection.
|
|
|
|
** Return SQLITE_OK on success.
|
|
|
|
** If lookaside is already active, return SQLITE_BUSY.
|
2008-08-05 00:13:26 +04:00
|
|
|
**
|
|
|
|
** The sz parameter is the number of bytes in each lookaside slot.
|
|
|
|
** The cnt parameter is the number of slots. If pStart is NULL the
|
|
|
|
** space for the lookaside memory is obtained from sqlite3_malloc().
|
|
|
|
** If pStart is not NULL then it is sz*cnt bytes of memory to use for
|
|
|
|
** the lookaside memory.
|
2008-07-28 23:34:53 +04:00
|
|
|
*/
|
2008-08-05 00:13:26 +04:00
|
|
|
static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
|
2008-07-28 23:34:53 +04:00
|
|
|
void *pStart;
|
|
|
|
if( db->lookaside.nOut ){
|
|
|
|
return SQLITE_BUSY;
|
|
|
|
}
|
2009-01-30 09:11:54 +03:00
|
|
|
/* Free any existing lookaside buffer for this handle before
|
|
|
|
** allocating a new one so we don't have to have space for
|
|
|
|
** both at the same time.
|
|
|
|
*/
|
|
|
|
if( db->lookaside.bMalloced ){
|
|
|
|
sqlite3_free(db->lookaside.pStart);
|
|
|
|
}
|
|
|
|
/* The size of a lookaside slot needs to be larger than a pointer
|
|
|
|
** to be useful.
|
|
|
|
*/
|
2009-02-03 18:50:33 +03:00
|
|
|
if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
|
2008-07-28 23:34:53 +04:00
|
|
|
if( cnt<0 ) cnt = 0;
|
2009-01-30 09:11:54 +03:00
|
|
|
if( sz==0 || cnt==0 ){
|
|
|
|
sz = 0;
|
|
|
|
pStart = 0;
|
|
|
|
}else if( pBuf==0 ){
|
2009-03-23 07:33:32 +03:00
|
|
|
sz = ROUND8(sz);
|
2008-08-05 00:13:26 +04:00
|
|
|
sqlite3BeginBenignMalloc();
|
|
|
|
pStart = sqlite3Malloc( sz*cnt );
|
|
|
|
sqlite3EndBenignMalloc();
|
|
|
|
}else{
|
2009-03-23 07:33:32 +03:00
|
|
|
sz = ROUNDDOWN8(sz);
|
2008-08-05 00:13:26 +04:00
|
|
|
pStart = pBuf;
|
|
|
|
}
|
2008-08-01 22:47:01 +04:00
|
|
|
db->lookaside.pStart = pStart;
|
|
|
|
db->lookaside.pFree = 0;
|
2008-12-11 00:19:56 +03:00
|
|
|
db->lookaside.sz = (u16)sz;
|
2008-07-28 23:34:53 +04:00
|
|
|
if( pStart ){
|
|
|
|
int i;
|
|
|
|
LookasideSlot *p;
|
2009-04-02 21:22:41 +04:00
|
|
|
assert( sz > (int)sizeof(LookasideSlot*) );
|
2008-07-28 23:34:53 +04:00
|
|
|
p = (LookasideSlot*)pStart;
|
|
|
|
for(i=cnt-1; i>=0; i--){
|
|
|
|
p->pNext = db->lookaside.pFree;
|
|
|
|
db->lookaside.pFree = p;
|
|
|
|
p = (LookasideSlot*)&((u8*)p)[sz];
|
|
|
|
}
|
|
|
|
db->lookaside.pEnd = p;
|
|
|
|
db->lookaside.bEnabled = 1;
|
2009-01-30 09:11:54 +03:00
|
|
|
db->lookaside.bMalloced = pBuf==0 ?1:0;
|
2008-08-01 22:47:01 +04:00
|
|
|
}else{
|
|
|
|
db->lookaside.pEnd = 0;
|
|
|
|
db->lookaside.bEnabled = 0;
|
2009-01-30 09:11:54 +03:00
|
|
|
db->lookaside.bMalloced = 0;
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2008-11-04 16:46:27 +03:00
|
|
|
/*
|
|
|
|
** Return the mutex associated with a database connection.
|
|
|
|
*/
|
|
|
|
sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
|
|
|
|
return db->mutex;
|
|
|
|
}
|
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
/*
|
|
|
|
** Configuration settings for an individual database connection
|
|
|
|
*/
|
|
|
|
int sqlite3_db_config(sqlite3 *db, int op, ...){
|
|
|
|
va_list ap;
|
2008-08-01 20:31:14 +04:00
|
|
|
int rc;
|
2008-07-28 23:34:53 +04:00
|
|
|
va_start(ap, op);
|
|
|
|
switch( op ){
|
2008-08-05 00:13:26 +04:00
|
|
|
case SQLITE_DBCONFIG_LOOKASIDE: {
|
|
|
|
void *pBuf = va_arg(ap, void*);
|
2008-07-28 23:34:53 +04:00
|
|
|
int sz = va_arg(ap, int);
|
|
|
|
int cnt = va_arg(ap, int);
|
2008-08-05 00:13:26 +04:00
|
|
|
rc = setupLookaside(db, pBuf, sz, cnt);
|
2008-07-28 23:34:53 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-08-01 20:31:14 +04:00
|
|
|
default: {
|
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-03-30 15:29:32 +04:00
|
|
|
|
2008-01-21 02:19:56 +03:00
|
|
|
/*
|
|
|
|
** Return true if the buffer z[0..n-1] contains all spaces.
|
|
|
|
*/
|
|
|
|
static int allSpaces(const char *z, int n){
|
2008-04-15 08:02:40 +04:00
|
|
|
while( n>0 && z[n-1]==' ' ){ n--; }
|
2008-01-21 02:19:56 +03:00
|
|
|
return n==0;
|
|
|
|
}
|
|
|
|
|
2004-05-21 02:16:29 +04:00
|
|
|
/*
|
|
|
|
** This is the default collating function named "BINARY" which is always
|
|
|
|
** available.
|
2008-01-21 02:19:56 +03:00
|
|
|
**
|
|
|
|
** If the padFlag argument is not NULL then space padding at the end
|
|
|
|
** of strings is ignored. This implements the RTRIM collation.
|
2004-05-21 02:16:29 +04:00
|
|
|
*/
|
2005-01-13 05:14:23 +03:00
|
|
|
static int binCollFunc(
|
2008-01-21 02:19:56 +03:00
|
|
|
void *padFlag,
|
2004-05-21 02:16:29 +04:00
|
|
|
int nKey1, const void *pKey1,
|
|
|
|
int nKey2, const void *pKey2
|
|
|
|
){
|
|
|
|
int rc, n;
|
|
|
|
n = nKey1<nKey2 ? nKey1 : nKey2;
|
|
|
|
rc = memcmp(pKey1, pKey2, n);
|
|
|
|
if( rc==0 ){
|
2008-01-21 02:19:56 +03:00
|
|
|
if( padFlag
|
|
|
|
&& allSpaces(((char*)pKey1)+n, nKey1-n)
|
|
|
|
&& allSpaces(((char*)pKey2)+n, nKey2-n)
|
|
|
|
){
|
|
|
|
/* Leave rc unchanged at 0 */
|
|
|
|
}else{
|
|
|
|
rc = nKey1 - nKey2;
|
|
|
|
}
|
2004-05-21 02:16:29 +04:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2004-06-09 13:55:16 +04:00
|
|
|
/*
|
2004-06-11 14:51:27 +04:00
|
|
|
** Another built-in collating sequence: NOCASE.
|
|
|
|
**
|
|
|
|
** This collating sequence is intended to be used for "case independant
|
|
|
|
** comparison". SQLite's knowledge of upper and lower case equivalents
|
|
|
|
** extends only to the 26 characters used in the English language.
|
|
|
|
**
|
|
|
|
** At the moment there is only a UTF-8 implementation.
|
2004-06-09 13:55:16 +04:00
|
|
|
*/
|
|
|
|
static int nocaseCollatingFunc(
|
|
|
|
void *NotUsed,
|
|
|
|
int nKey1, const void *pKey1,
|
|
|
|
int nKey2, const void *pKey2
|
|
|
|
){
|
|
|
|
int r = sqlite3StrNICmp(
|
2004-08-30 00:08:58 +04:00
|
|
|
(const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
|
2008-11-19 12:05:26 +03:00
|
|
|
UNUSED_PARAMETER(NotUsed);
|
2004-06-09 13:55:16 +04:00
|
|
|
if( 0==r ){
|
|
|
|
r = nKey1-nKey2;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2002-01-17 00:00:27 +03:00
|
|
|
/*
|
|
|
|
** Return the ROWID of the most recent insert
|
|
|
|
*/
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
|
2002-01-17 00:00:27 +03:00
|
|
|
return db->lastRowid;
|
|
|
|
}
|
|
|
|
|
2002-04-12 14:08:59 +04:00
|
|
|
/*
|
2004-05-10 14:34:34 +04:00
|
|
|
** Return the number of changes in the most recent call to sqlite3_exec().
|
2002-04-12 14:08:59 +04:00
|
|
|
*/
|
2004-09-06 21:24:11 +04:00
|
|
|
int sqlite3_changes(sqlite3 *db){
|
2002-04-12 14:08:59 +04:00
|
|
|
return db->nChange;
|
|
|
|
}
|
|
|
|
|
2004-02-26 01:51:06 +03:00
|
|
|
/*
|
2004-06-21 10:50:26 +04:00
|
|
|
** Return the number of changes since the database handle was opened.
|
2004-02-26 01:51:06 +03:00
|
|
|
*/
|
2004-06-21 10:50:26 +04:00
|
|
|
int sqlite3_total_changes(sqlite3 *db){
|
|
|
|
return db->nTotalChange;
|
2004-02-21 01:53:38 +03:00
|
|
|
}
|
|
|
|
|
2008-12-17 20:30:26 +03:00
|
|
|
/*
|
|
|
|
** Close all open savepoints. This function only manipulates fields of the
|
|
|
|
** database handle object, it does not close any savepoints that may be open
|
|
|
|
** at the b-tree/pager level.
|
|
|
|
*/
|
|
|
|
void sqlite3CloseSavepoints(sqlite3 *db){
|
|
|
|
while( db->pSavepoint ){
|
|
|
|
Savepoint *pTmp = db->pSavepoint;
|
|
|
|
db->pSavepoint = pTmp->pNext;
|
|
|
|
sqlite3DbFree(db, pTmp);
|
|
|
|
}
|
|
|
|
db->nSavepoint = 0;
|
2009-03-18 13:33:00 +03:00
|
|
|
db->nStatement = 0;
|
2008-12-17 20:30:26 +03:00
|
|
|
db->isTransactionSavepoint = 0;
|
|
|
|
}
|
|
|
|
|
2001-09-15 04:57:28 +04:00
|
|
|
/*
|
|
|
|
** Close an existing SQLite database
|
|
|
|
*/
|
2004-09-06 21:24:11 +04:00
|
|
|
int sqlite3_close(sqlite3 *db){
|
2002-02-24 02:45:45 +03:00
|
|
|
HashElem *i;
|
2003-03-19 06:14:00 +03:00
|
|
|
int j;
|
2004-06-16 14:39:23 +04:00
|
|
|
|
|
|
|
if( !db ){
|
2004-06-19 07:33:57 +04:00
|
|
|
return SQLITE_OK;
|
2004-06-16 14:39:23 +04:00
|
|
|
}
|
2008-01-23 06:03:05 +03:00
|
|
|
if( !sqlite3SafetyCheckSickOrOk(db) ){
|
2004-06-26 13:50:11 +04:00
|
|
|
return SQLITE_MISUSE;
|
|
|
|
}
|
2007-08-21 19:13:19 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2004-06-26 13:50:11 +04:00
|
|
|
|
2006-07-31 00:50:44 +04:00
|
|
|
sqlite3ResetInternalSchema(db, 0);
|
2007-04-16 19:06:25 +04:00
|
|
|
|
2007-04-18 18:24:32 +04:00
|
|
|
/* If a transaction is open, the ResetInternalSchema() call above
|
|
|
|
** will not have called the xDisconnect() method on any virtual
|
|
|
|
** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
|
|
|
|
** call will do so. We need to do this before the check for active
|
|
|
|
** SQL statements below, as the v-table implementation may be storing
|
|
|
|
** some prepared statements internally.
|
|
|
|
*/
|
|
|
|
sqlite3VtabRollback(db);
|
|
|
|
|
2007-04-16 19:06:25 +04:00
|
|
|
/* If there are any outstanding VMs, return SQLITE_BUSY. */
|
2004-06-19 07:33:57 +04:00
|
|
|
if( db->pVdbe ){
|
|
|
|
sqlite3Error(db, SQLITE_BUSY,
|
2009-02-04 20:40:57 +03:00
|
|
|
"unable to close due to unfinalised statements");
|
2007-08-22 06:56:42 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2004-06-19 07:33:57 +04:00
|
|
|
return SQLITE_BUSY;
|
|
|
|
}
|
2008-01-23 06:03:05 +03:00
|
|
|
assert( sqlite3SafetyCheckSickOrOk(db) );
|
2004-06-15 20:51:01 +04:00
|
|
|
|
2009-02-03 19:51:24 +03:00
|
|
|
for(j=0; j<db->nDb; j++){
|
|
|
|
Btree *pBt = db->aDb[j].pBt;
|
|
|
|
if( pBt && sqlite3BtreeIsInBackup(pBt) ){
|
|
|
|
sqlite3Error(db, SQLITE_BUSY,
|
2009-02-04 20:40:57 +03:00
|
|
|
"unable to close due to unfinished backup operation");
|
2009-02-03 19:51:24 +03:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return SQLITE_BUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 20:30:26 +03:00
|
|
|
/* Free any outstanding Savepoint structures. */
|
|
|
|
sqlite3CloseSavepoints(db);
|
|
|
|
|
2003-03-19 06:14:00 +03:00
|
|
|
for(j=0; j<db->nDb; j++){
|
2004-02-12 21:46:38 +03:00
|
|
|
struct Db *pDb = &db->aDb[j];
|
|
|
|
if( pDb->pBt ){
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3BtreeClose(pDb->pBt);
|
2004-02-12 21:46:38 +03:00
|
|
|
pDb->pBt = 0;
|
2006-01-10 10:14:23 +03:00
|
|
|
if( j!=1 ){
|
|
|
|
pDb->pSchema = 0;
|
|
|
|
}
|
2003-03-20 04:16:58 +03:00
|
|
|
}
|
2001-10-08 17:22:32 +04:00
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3ResetInternalSchema(db, 0);
|
2009-03-16 16:19:36 +03:00
|
|
|
|
|
|
|
/* Tell the code in notify.c that the connection no longer holds any
|
|
|
|
** locks and does not require any further unlock-notify callbacks.
|
|
|
|
*/
|
|
|
|
sqlite3ConnectionClosed(db);
|
|
|
|
|
2003-03-31 04:30:47 +04:00
|
|
|
assert( db->nDb<=2 );
|
|
|
|
assert( db->aDb==db->aDbStatic );
|
2008-08-21 22:49:27 +04:00
|
|
|
for(j=0; j<ArraySize(db->aFunc.a); j++){
|
|
|
|
FuncDef *pNext, *pHash, *p;
|
|
|
|
for(p=db->aFunc.a[j]; p; p=pHash){
|
|
|
|
pHash = p->pHash;
|
|
|
|
while( p ){
|
|
|
|
pNext = p->pNext;
|
|
|
|
sqlite3DbFree(db, p);
|
|
|
|
p = pNext;
|
|
|
|
}
|
2002-02-24 02:45:45 +03:00
|
|
|
}
|
|
|
|
}
|
2004-06-12 13:25:12 +04:00
|
|
|
for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
|
2004-06-10 06:16:01 +04:00
|
|
|
CollSeq *pColl = (CollSeq *)sqliteHashData(i);
|
2007-05-07 13:32:45 +04:00
|
|
|
/* Invoke any destructors registered for collation sequence user data. */
|
|
|
|
for(j=0; j<3; j++){
|
|
|
|
if( pColl[j].xDel ){
|
|
|
|
pColl[j].xDel(pColl[j].pUser);
|
|
|
|
}
|
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, pColl);
|
2004-06-10 06:16:01 +04:00
|
|
|
}
|
2004-06-12 13:25:12 +04:00
|
|
|
sqlite3HashClear(&db->aCollSeq);
|
2006-06-12 03:41:55 +04:00
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2006-06-15 08:28:13 +04:00
|
|
|
for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
|
|
|
|
Module *pMod = (Module *)sqliteHashData(i);
|
2007-06-22 19:21:15 +04:00
|
|
|
if( pMod->xDestroy ){
|
|
|
|
pMod->xDestroy(pMod->pAux);
|
|
|
|
}
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, pMod);
|
2006-06-15 08:28:13 +04:00
|
|
|
}
|
2006-06-12 03:41:55 +04:00
|
|
|
sqlite3HashClear(&db->aModule);
|
|
|
|
#endif
|
2004-06-10 06:16:01 +04:00
|
|
|
|
2004-05-20 15:00:52 +04:00
|
|
|
sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
|
2004-06-18 08:24:54 +04:00
|
|
|
if( db->pErr ){
|
|
|
|
sqlite3ValueFree(db->pErr);
|
|
|
|
}
|
2006-06-08 19:48:00 +04:00
|
|
|
sqlite3CloseExtensions(db);
|
2004-06-19 07:33:57 +04:00
|
|
|
|
|
|
|
db->magic = SQLITE_MAGIC_ERROR;
|
2006-01-10 10:14:23 +03:00
|
|
|
|
|
|
|
/* The temp-database schema is allocated differently from the other schema
|
|
|
|
** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
|
|
|
|
** So it needs to be freed here. Todo: Why not roll the temp schema into
|
|
|
|
** the same sqliteMalloc() as the one that allocates the database
|
|
|
|
** structure?
|
|
|
|
*/
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, db->aDb[1].pSchema);
|
2007-08-21 19:13:19 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2008-01-23 06:03:05 +03:00
|
|
|
db->magic = SQLITE_MAGIC_CLOSED;
|
2007-08-21 19:13:19 +04:00
|
|
|
sqlite3_mutex_free(db->mutex);
|
2008-10-11 19:38:29 +04:00
|
|
|
assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */
|
2008-08-05 00:13:26 +04:00
|
|
|
if( db->lookaside.bMalloced ){
|
|
|
|
sqlite3_free(db->lookaside.pStart);
|
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
sqlite3_free(db);
|
2004-06-19 07:33:57 +04:00
|
|
|
return SQLITE_OK;
|
2000-05-29 18:26:00 +04:00
|
|
|
}
|
|
|
|
|
2003-03-19 06:14:00 +03:00
|
|
|
/*
|
|
|
|
** Rollback all database files.
|
|
|
|
*/
|
2004-09-06 21:24:11 +04:00
|
|
|
void sqlite3RollbackAll(sqlite3 *db){
|
2003-03-19 06:14:00 +03:00
|
|
|
int i;
|
2005-12-16 18:24:28 +03:00
|
|
|
int inTrans = 0;
|
2007-08-21 20:15:55 +04:00
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
2008-06-20 18:59:51 +04:00
|
|
|
sqlite3BeginBenignMalloc();
|
2003-03-19 06:14:00 +03:00
|
|
|
for(i=0; i<db->nDb; i++){
|
|
|
|
if( db->aDb[i].pBt ){
|
2005-12-16 18:24:28 +03:00
|
|
|
if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
|
|
|
|
inTrans = 1;
|
|
|
|
}
|
2004-05-08 12:23:19 +04:00
|
|
|
sqlite3BtreeRollback(db->aDb[i].pBt);
|
2003-03-19 06:14:00 +03:00
|
|
|
db->aDb[i].inTrans = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-06-22 13:53:48 +04:00
|
|
|
sqlite3VtabRollback(db);
|
2008-06-20 18:59:51 +04:00
|
|
|
sqlite3EndBenignMalloc();
|
2007-10-03 12:46:44 +04:00
|
|
|
|
2005-12-16 09:54:01 +03:00
|
|
|
if( db->flags&SQLITE_InternChanges ){
|
2007-08-13 19:28:33 +04:00
|
|
|
sqlite3ExpirePreparedStatements(db);
|
2005-12-16 09:54:01 +03:00
|
|
|
sqlite3ResetInternalSchema(db, 0);
|
|
|
|
}
|
|
|
|
|
2009-09-19 21:00:31 +04:00
|
|
|
/* Any deferred constraint violations have now been resolved. */
|
|
|
|
db->nDeferredCons = 0;
|
|
|
|
|
2005-12-16 09:54:01 +03:00
|
|
|
/* If one has been configured, invoke the rollback-hook callback */
|
2005-12-16 18:24:28 +03:00
|
|
|
if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
|
2005-12-16 09:54:01 +03:00
|
|
|
db->xRollbackCallback(db->pRollbackArg);
|
|
|
|
}
|
2003-03-19 06:14:00 +03:00
|
|
|
}
|
|
|
|
|
2002-05-10 09:44:55 +04:00
|
|
|
/*
|
2002-05-10 17:14:07 +04:00
|
|
|
** Return a static string that describes the kind of error specified in the
|
|
|
|
** argument.
|
2002-05-10 09:44:55 +04:00
|
|
|
*/
|
2004-06-01 03:56:42 +04:00
|
|
|
const char *sqlite3ErrStr(int rc){
|
2009-04-27 22:46:06 +04:00
|
|
|
static const char* const aMsg[] = {
|
|
|
|
/* SQLITE_OK */ "not an error",
|
|
|
|
/* SQLITE_ERROR */ "SQL logic error or missing database",
|
|
|
|
/* SQLITE_INTERNAL */ 0,
|
|
|
|
/* SQLITE_PERM */ "access permission denied",
|
|
|
|
/* SQLITE_ABORT */ "callback requested query abort",
|
|
|
|
/* SQLITE_BUSY */ "database is locked",
|
|
|
|
/* SQLITE_LOCKED */ "database table is locked",
|
|
|
|
/* SQLITE_NOMEM */ "out of memory",
|
|
|
|
/* SQLITE_READONLY */ "attempt to write a readonly database",
|
|
|
|
/* SQLITE_INTERRUPT */ "interrupted",
|
|
|
|
/* SQLITE_IOERR */ "disk I/O error",
|
|
|
|
/* SQLITE_CORRUPT */ "database disk image is malformed",
|
|
|
|
/* SQLITE_NOTFOUND */ 0,
|
|
|
|
/* SQLITE_FULL */ "database or disk is full",
|
|
|
|
/* SQLITE_CANTOPEN */ "unable to open database file",
|
|
|
|
/* SQLITE_PROTOCOL */ 0,
|
|
|
|
/* SQLITE_EMPTY */ "table contains no data",
|
|
|
|
/* SQLITE_SCHEMA */ "database schema has changed",
|
2009-06-25 05:47:11 +04:00
|
|
|
/* SQLITE_TOOBIG */ "string or blob too big",
|
2009-04-27 22:46:06 +04:00
|
|
|
/* SQLITE_CONSTRAINT */ "constraint failed",
|
|
|
|
/* SQLITE_MISMATCH */ "datatype mismatch",
|
|
|
|
/* SQLITE_MISUSE */ "library routine called out of sequence",
|
|
|
|
/* SQLITE_NOLFS */ "large file support is disabled",
|
|
|
|
/* SQLITE_AUTH */ "authorization denied",
|
|
|
|
/* SQLITE_FORMAT */ "auxiliary database format error",
|
|
|
|
/* SQLITE_RANGE */ "bind or column index out of range",
|
|
|
|
/* SQLITE_NOTADB */ "file is encrypted or is not a database",
|
|
|
|
};
|
|
|
|
rc &= 0xff;
|
2009-05-06 23:03:13 +04:00
|
|
|
if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){
|
2009-04-27 22:46:06 +04:00
|
|
|
return aMsg[rc];
|
|
|
|
}else{
|
|
|
|
return "unknown error";
|
2002-05-10 09:44:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-28 18:32:48 +04:00
|
|
|
/*
|
|
|
|
** This routine implements a busy callback that sleeps and tries
|
|
|
|
** again until a timeout value is reached. The timeout value is
|
|
|
|
** an integer number of milliseconds passed in as the first
|
|
|
|
** argument.
|
|
|
|
*/
|
2001-04-11 18:28:42 +04:00
|
|
|
static int sqliteDefaultBusyCallback(
|
2005-05-25 00:19:57 +04:00
|
|
|
void *ptr, /* Database connection */
|
2000-07-28 18:32:48 +04:00
|
|
|
int count /* Number of times table has been busy */
|
|
|
|
){
|
2008-06-26 14:41:19 +04:00
|
|
|
#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
|
2005-04-28 16:06:05 +04:00
|
|
|
static const u8 delays[] =
|
|
|
|
{ 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 };
|
|
|
|
static const u8 totals[] =
|
|
|
|
{ 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 };
|
2004-01-15 16:29:31 +03:00
|
|
|
# define NDELAY (sizeof(delays)/sizeof(delays[0]))
|
2007-08-17 19:53:36 +04:00
|
|
|
sqlite3 *db = (sqlite3 *)ptr;
|
|
|
|
int timeout = db->busyTimeout;
|
2005-05-25 00:19:57 +04:00
|
|
|
int delay, prior;
|
2000-07-28 18:32:48 +04:00
|
|
|
|
2005-04-28 16:06:05 +04:00
|
|
|
assert( count>=0 );
|
|
|
|
if( count < NDELAY ){
|
|
|
|
delay = delays[count];
|
|
|
|
prior = totals[count];
|
2004-01-15 16:29:31 +03:00
|
|
|
}else{
|
|
|
|
delay = delays[NDELAY-1];
|
2005-05-07 02:05:56 +04:00
|
|
|
prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
|
2000-07-28 18:32:48 +04:00
|
|
|
}
|
2004-01-15 16:29:31 +03:00
|
|
|
if( prior + delay > timeout ){
|
|
|
|
delay = timeout - prior;
|
2000-07-28 18:32:48 +04:00
|
|
|
if( delay<=0 ) return 0;
|
|
|
|
}
|
2007-08-20 20:07:00 +04:00
|
|
|
sqlite3OsSleep(db->pVfs, delay*1000);
|
2000-07-28 18:32:48 +04:00
|
|
|
return 1;
|
|
|
|
#else
|
2007-08-20 15:12:40 +04:00
|
|
|
sqlite3 *db = (sqlite3 *)ptr;
|
2005-06-14 06:24:31 +04:00
|
|
|
int timeout = ((sqlite3 *)ptr)->busyTimeout;
|
2000-07-28 18:32:48 +04:00
|
|
|
if( (count+1)*1000 > timeout ){
|
|
|
|
return 0;
|
|
|
|
}
|
2007-08-20 15:12:40 +04:00
|
|
|
sqlite3OsSleep(db->pVfs, 1000000);
|
2000-07-28 18:32:48 +04:00
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-07-09 06:16:02 +04:00
|
|
|
/*
|
|
|
|
** Invoke the given busy handler.
|
|
|
|
**
|
|
|
|
** This routine is called when an operation failed with a lock.
|
|
|
|
** If this routine returns non-zero, the lock is retried. If it
|
|
|
|
** returns 0, the operation aborts with an SQLITE_BUSY error.
|
|
|
|
*/
|
|
|
|
int sqlite3InvokeBusyHandler(BusyHandler *p){
|
|
|
|
int rc;
|
2008-07-11 20:15:17 +04:00
|
|
|
if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
|
2005-07-09 06:16:02 +04:00
|
|
|
rc = p->xFunc(p->pArg, p->nBusy);
|
|
|
|
if( rc==0 ){
|
|
|
|
p->nBusy = -1;
|
|
|
|
}else{
|
|
|
|
p->nBusy++;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2000-07-28 18:32:48 +04:00
|
|
|
/*
|
|
|
|
** This routine sets the busy callback for an Sqlite database to the
|
|
|
|
** given callback function with the given argument.
|
|
|
|
*/
|
2004-06-19 12:18:07 +04:00
|
|
|
int sqlite3_busy_handler(
|
|
|
|
sqlite3 *db,
|
2004-06-12 05:43:26 +04:00
|
|
|
int (*xBusy)(void*,int),
|
2000-07-28 18:32:48 +04:00
|
|
|
void *pArg
|
|
|
|
){
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2004-06-04 10:22:00 +04:00
|
|
|
db->busyHandler.xFunc = xBusy;
|
|
|
|
db->busyHandler.pArg = pArg;
|
2005-07-09 06:16:02 +04:00
|
|
|
db->busyHandler.nBusy = 0;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2004-06-19 12:18:07 +04:00
|
|
|
return SQLITE_OK;
|
2000-07-28 18:32:48 +04:00
|
|
|
}
|
|
|
|
|
2003-10-18 13:37:26 +04:00
|
|
|
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
|
|
|
|
/*
|
|
|
|
** This routine sets the progress callback for an Sqlite database to the
|
|
|
|
** given callback function with the given argument. The progress callback will
|
|
|
|
** be invoked every nOps opcodes.
|
|
|
|
*/
|
2004-05-10 14:34:34 +04:00
|
|
|
void sqlite3_progress_handler(
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite3 *db,
|
2003-10-18 13:37:26 +04:00
|
|
|
int nOps,
|
|
|
|
int (*xProgress)(void*),
|
|
|
|
void *pArg
|
|
|
|
){
|
2008-07-07 23:52:09 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
if( nOps>0 ){
|
|
|
|
db->xProgress = xProgress;
|
|
|
|
db->nProgressOps = nOps;
|
|
|
|
db->pProgressArg = pArg;
|
|
|
|
}else{
|
|
|
|
db->xProgress = 0;
|
|
|
|
db->nProgressOps = 0;
|
|
|
|
db->pProgressArg = 0;
|
2003-10-18 13:37:26 +04:00
|
|
|
}
|
2008-07-07 23:52:09 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2003-10-18 13:37:26 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-07-28 18:32:48 +04:00
|
|
|
/*
|
|
|
|
** This routine installs a default busy handler that waits for the
|
|
|
|
** specified number of milliseconds before returning 0.
|
|
|
|
*/
|
2004-06-19 12:18:07 +04:00
|
|
|
int sqlite3_busy_timeout(sqlite3 *db, int ms){
|
2000-07-28 18:32:48 +04:00
|
|
|
if( ms>0 ){
|
2005-05-25 00:19:57 +04:00
|
|
|
db->busyTimeout = ms;
|
|
|
|
sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
|
2000-07-28 18:32:48 +04:00
|
|
|
}else{
|
2004-05-10 14:34:34 +04:00
|
|
|
sqlite3_busy_handler(db, 0, 0);
|
2000-07-28 18:32:48 +04:00
|
|
|
}
|
2004-06-19 12:18:07 +04:00
|
|
|
return SQLITE_OK;
|
2000-07-28 18:32:48 +04:00
|
|
|
}
|
2000-10-17 02:06:40 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Cause any pending operation to stop at its earliest opportunity.
|
|
|
|
*/
|
2004-09-06 21:24:11 +04:00
|
|
|
void sqlite3_interrupt(sqlite3 *db){
|
2008-07-07 23:52:09 +04:00
|
|
|
db->u1.isInterrupted = 1;
|
2000-10-17 02:06:40 +04:00
|
|
|
}
|
2002-02-02 18:01:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-01-17 16:21:40 +03:00
|
|
|
** This function is exactly the same as sqlite3_create_function(), except
|
|
|
|
** that it is designed to be called by internal code. The difference is
|
|
|
|
** that if a malloc() fails in sqlite3_create_function(), an error code
|
|
|
|
** is returned and the mallocFailed flag cleared.
|
2002-02-02 18:01:15 +03:00
|
|
|
*/
|
2006-01-17 16:21:40 +03:00
|
|
|
int sqlite3CreateFunc(
|
2004-05-26 10:18:37 +04:00
|
|
|
sqlite3 *db,
|
|
|
|
const char *zFunctionName,
|
|
|
|
int nArg,
|
2004-06-12 13:25:12 +04:00
|
|
|
int enc,
|
2004-05-26 10:18:37 +04:00
|
|
|
void *pUserData,
|
|
|
|
void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
|
|
|
|
void (*xStep)(sqlite3_context*,int,sqlite3_value **),
|
|
|
|
void (*xFinal)(sqlite3_context*)
|
2002-02-24 02:45:45 +03:00
|
|
|
){
|
2002-02-28 03:41:10 +03:00
|
|
|
FuncDef *p;
|
2002-08-24 22:24:51 +04:00
|
|
|
int nName;
|
2004-05-26 10:18:37 +04:00
|
|
|
|
2007-08-21 20:15:55 +04:00
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
2004-09-30 17:43:13 +04:00
|
|
|
if( zFunctionName==0 ||
|
2004-05-26 10:58:43 +04:00
|
|
|
(xFunc && (xFinal || xStep)) ||
|
|
|
|
(!xFunc && (xFinal && !xStep)) ||
|
|
|
|
(!xFunc && (!xFinal && xStep)) ||
|
2008-07-07 18:50:14 +04:00
|
|
|
(nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
|
2009-05-04 00:23:53 +04:00
|
|
|
(255<(nName = sqlite3Strlen30( zFunctionName))) ){
|
2009-05-07 17:43:49 +04:00
|
|
|
return SQLITE_MISUSE;
|
2004-05-26 10:18:37 +04:00
|
|
|
}
|
2004-06-12 13:25:12 +04:00
|
|
|
|
2005-01-21 11:13:14 +03:00
|
|
|
#ifndef SQLITE_OMIT_UTF16
|
2004-06-12 13:25:12 +04:00
|
|
|
/* If SQLITE_UTF16 is specified as the encoding type, transform this
|
|
|
|
** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
|
|
|
|
** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
|
|
|
|
**
|
|
|
|
** If SQLITE_ANY is specified, add three versions of the function
|
|
|
|
** to the hash table.
|
|
|
|
*/
|
|
|
|
if( enc==SQLITE_UTF16 ){
|
|
|
|
enc = SQLITE_UTF16NATIVE;
|
|
|
|
}else if( enc==SQLITE_ANY ){
|
|
|
|
int rc;
|
2006-01-17 16:21:40 +03:00
|
|
|
rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
|
2004-06-19 12:18:07 +04:00
|
|
|
pUserData, xFunc, xStep, xFinal);
|
2007-08-21 20:15:55 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
|
|
|
|
pUserData, xFunc, xStep, xFinal);
|
|
|
|
}
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
return rc;
|
|
|
|
}
|
2004-06-12 13:25:12 +04:00
|
|
|
enc = SQLITE_UTF16BE;
|
|
|
|
}
|
2005-01-21 11:13:14 +03:00
|
|
|
#else
|
|
|
|
enc = SQLITE_UTF8;
|
|
|
|
#endif
|
2005-01-25 07:27:54 +03:00
|
|
|
|
|
|
|
/* Check if an existing function is being overridden or deleted. If so,
|
|
|
|
** and there are active VMs, then return SQLITE_BUSY. If a function
|
|
|
|
** is being overridden/deleted but there are no active VMs, allow the
|
|
|
|
** operation to continue but invalidate all precompiled statements.
|
|
|
|
*/
|
2008-12-11 02:04:13 +03:00
|
|
|
p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
|
2005-01-25 07:27:54 +03:00
|
|
|
if( p && p->iPrefEnc==enc && p->nArg==nArg ){
|
|
|
|
if( db->activeVdbeCnt ){
|
|
|
|
sqlite3Error(db, SQLITE_BUSY,
|
2009-02-04 20:40:57 +03:00
|
|
|
"unable to delete/modify user-function due to active statements");
|
2007-08-16 08:30:38 +04:00
|
|
|
assert( !db->mallocFailed );
|
2005-01-25 07:27:54 +03:00
|
|
|
return SQLITE_BUSY;
|
|
|
|
}else{
|
|
|
|
sqlite3ExpirePreparedStatements(db);
|
|
|
|
}
|
|
|
|
}
|
2004-05-26 10:18:37 +04:00
|
|
|
|
2008-12-11 02:04:13 +03:00
|
|
|
p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
|
2007-09-03 15:04:22 +04:00
|
|
|
assert(p || db->mallocFailed);
|
|
|
|
if( !p ){
|
|
|
|
return SQLITE_NOMEM;
|
2006-01-17 16:21:40 +03:00
|
|
|
}
|
2007-09-03 15:04:22 +04:00
|
|
|
p->flags = 0;
|
|
|
|
p->xFunc = xFunc;
|
|
|
|
p->xStep = xStep;
|
|
|
|
p->xFinalize = xFinal;
|
|
|
|
p->pUserData = pUserData;
|
2008-12-11 00:19:56 +03:00
|
|
|
p->nArg = (u16)nArg;
|
2004-05-26 10:18:37 +04:00
|
|
|
return SQLITE_OK;
|
2002-02-24 02:45:45 +03:00
|
|
|
}
|
2006-01-17 16:21:40 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Create new user functions.
|
|
|
|
*/
|
|
|
|
int sqlite3_create_function(
|
|
|
|
sqlite3 *db,
|
|
|
|
const char *zFunctionName,
|
|
|
|
int nArg,
|
|
|
|
int enc,
|
|
|
|
void *p,
|
|
|
|
void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
|
|
|
|
void (*xStep)(sqlite3_context*,int,sqlite3_value **),
|
|
|
|
void (*xFinal)(sqlite3_context*)
|
|
|
|
){
|
|
|
|
int rc;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2006-01-17 16:21:40 +03:00
|
|
|
rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = sqlite3ApiExit(db, rc);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return rc;
|
2006-01-17 16:21:40 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 11:13:14 +03:00
|
|
|
#ifndef SQLITE_OMIT_UTF16
|
2004-05-26 10:18:37 +04:00
|
|
|
int sqlite3_create_function16(
|
|
|
|
sqlite3 *db,
|
|
|
|
const void *zFunctionName,
|
|
|
|
int nArg,
|
|
|
|
int eTextRep,
|
2006-01-17 16:21:40 +03:00
|
|
|
void *p,
|
2004-05-26 10:18:37 +04:00
|
|
|
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
|
|
|
|
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
|
|
|
|
void (*xFinal)(sqlite3_context*)
|
2002-02-24 02:45:45 +03:00
|
|
|
){
|
2004-05-26 10:18:37 +04:00
|
|
|
int rc;
|
2005-12-15 06:04:10 +03:00
|
|
|
char *zFunc8;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2007-08-16 08:30:38 +04:00
|
|
|
assert( !db->mallocFailed );
|
2007-08-16 14:09:01 +04:00
|
|
|
zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
|
2006-01-17 16:21:40 +03:00
|
|
|
rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, zFunc8);
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = sqlite3ApiExit(db, rc);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return rc;
|
2002-02-24 02:45:45 +03:00
|
|
|
}
|
2005-01-21 11:13:14 +03:00
|
|
|
#endif
|
2002-06-20 15:36:48 +04:00
|
|
|
|
2006-09-17 01:45:14 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Declare that a function has been overloaded by a virtual table.
|
|
|
|
**
|
|
|
|
** If the function already exists as a regular global function, then
|
|
|
|
** this routine is a no-op. If the function does not exist, then create
|
|
|
|
** a new one that always throws a run-time error.
|
|
|
|
**
|
|
|
|
** When virtual tables intend to provide an overloaded function, they
|
|
|
|
** should call this routine to make sure the global function exists.
|
|
|
|
** A global function must exist in order for name resolution to work
|
|
|
|
** properly.
|
|
|
|
*/
|
|
|
|
int sqlite3_overload_function(
|
|
|
|
sqlite3 *db,
|
|
|
|
const char *zName,
|
|
|
|
int nArg
|
|
|
|
){
|
2009-05-04 00:23:53 +04:00
|
|
|
int nName = sqlite3Strlen30(zName);
|
2007-08-21 20:15:55 +04:00
|
|
|
int rc;
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2006-09-17 01:45:14 +04:00
|
|
|
if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
|
|
|
|
sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
|
|
|
|
0, sqlite3InvalidFunction, 0, 0);
|
|
|
|
}
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = sqlite3ApiExit(db, SQLITE_OK);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return rc;
|
2006-09-17 01:45:14 +04:00
|
|
|
}
|
|
|
|
|
2005-08-30 03:00:03 +04:00
|
|
|
#ifndef SQLITE_OMIT_TRACE
|
2003-01-16 19:28:53 +03:00
|
|
|
/*
|
|
|
|
** Register a trace function. The pArg from the previously registered trace
|
|
|
|
** is returned.
|
|
|
|
**
|
|
|
|
** A NULL trace function means that no tracing is executes. A non-NULL
|
|
|
|
** trace is a pointer to a function that is invoked at the start of each
|
2005-08-30 03:00:03 +04:00
|
|
|
** SQL statement.
|
2003-01-16 19:28:53 +03:00
|
|
|
*/
|
2004-09-06 21:24:11 +04:00
|
|
|
void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
|
2007-08-21 20:15:55 +04:00
|
|
|
void *pOld;
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
pOld = db->pTraceArg;
|
2003-01-16 19:28:53 +03:00
|
|
|
db->xTrace = xTrace;
|
|
|
|
db->pTraceArg = pArg;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2003-01-16 19:28:53 +03:00
|
|
|
return pOld;
|
2003-04-03 19:46:04 +04:00
|
|
|
}
|
2005-08-30 03:00:03 +04:00
|
|
|
/*
|
|
|
|
** Register a profile function. The pArg from the previously registered
|
|
|
|
** profile function is returned.
|
|
|
|
**
|
|
|
|
** A NULL profile function means that no profiling is executes. A non-NULL
|
|
|
|
** profile is a pointer to a function that is invoked at the conclusion of
|
|
|
|
** each SQL statement that is run.
|
|
|
|
*/
|
|
|
|
void *sqlite3_profile(
|
|
|
|
sqlite3 *db,
|
|
|
|
void (*xProfile)(void*,const char*,sqlite_uint64),
|
|
|
|
void *pArg
|
|
|
|
){
|
2007-08-21 20:15:55 +04:00
|
|
|
void *pOld;
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
pOld = db->pProfileArg;
|
2005-08-30 03:00:03 +04:00
|
|
|
db->xProfile = xProfile;
|
|
|
|
db->pProfileArg = pArg;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2005-08-30 03:00:03 +04:00
|
|
|
return pOld;
|
|
|
|
}
|
|
|
|
#endif /* SQLITE_OMIT_TRACE */
|
2003-04-13 22:26:49 +04:00
|
|
|
|
2004-01-15 05:44:03 +03:00
|
|
|
/*** EXPERIMENTAL ***
|
|
|
|
**
|
|
|
|
** Register a function to be invoked when a transaction comments.
|
2005-12-16 09:54:01 +03:00
|
|
|
** If the invoked function returns non-zero, then the commit becomes a
|
2004-01-15 05:44:03 +03:00
|
|
|
** rollback.
|
|
|
|
*/
|
2004-05-10 14:34:34 +04:00
|
|
|
void *sqlite3_commit_hook(
|
2004-09-06 21:24:11 +04:00
|
|
|
sqlite3 *db, /* Attach the hook to this database */
|
2004-01-15 05:44:03 +03:00
|
|
|
int (*xCallback)(void*), /* Function to invoke on each commit */
|
|
|
|
void *pArg /* Argument to the function */
|
|
|
|
){
|
2007-08-21 20:15:55 +04:00
|
|
|
void *pOld;
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
pOld = db->pCommitArg;
|
2004-01-15 05:44:03 +03:00
|
|
|
db->xCommitCallback = xCallback;
|
|
|
|
db->pCommitArg = pArg;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2004-01-15 05:44:03 +03:00
|
|
|
return pOld;
|
|
|
|
}
|
|
|
|
|
2005-12-15 18:22:08 +03:00
|
|
|
/*
|
|
|
|
** Register a callback to be invoked each time a row is updated,
|
|
|
|
** inserted or deleted using this database connection.
|
|
|
|
*/
|
2005-12-16 09:54:01 +03:00
|
|
|
void *sqlite3_update_hook(
|
2005-12-15 18:22:08 +03:00
|
|
|
sqlite3 *db, /* Attach the hook to this database */
|
|
|
|
void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
|
|
|
|
void *pArg /* Argument to the function */
|
|
|
|
){
|
2007-08-21 20:15:55 +04:00
|
|
|
void *pRet;
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
pRet = db->pUpdateArg;
|
2005-12-15 18:22:08 +03:00
|
|
|
db->xUpdateCallback = xCallback;
|
|
|
|
db->pUpdateArg = pArg;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2005-12-16 09:54:01 +03:00
|
|
|
return pRet;
|
2005-12-15 18:22:08 +03:00
|
|
|
}
|
|
|
|
|
2005-12-16 09:54:01 +03:00
|
|
|
/*
|
|
|
|
** Register a callback to be invoked each time a transaction is rolled
|
|
|
|
** back by this database connection.
|
|
|
|
*/
|
|
|
|
void *sqlite3_rollback_hook(
|
|
|
|
sqlite3 *db, /* Attach the hook to this database */
|
|
|
|
void (*xCallback)(void*), /* Callback function */
|
|
|
|
void *pArg /* Argument to the function */
|
|
|
|
){
|
2007-08-21 20:15:55 +04:00
|
|
|
void *pRet;
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
pRet = db->pRollbackArg;
|
2005-12-16 09:54:01 +03:00
|
|
|
db->xRollbackCallback = xCallback;
|
|
|
|
db->pRollbackArg = pArg;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2005-12-16 09:54:01 +03:00
|
|
|
return pRet;
|
|
|
|
}
|
2004-01-15 05:44:03 +03:00
|
|
|
|
2009-04-30 13:10:37 +04:00
|
|
|
/*
|
|
|
|
** This function returns true if main-memory should be used instead of
|
|
|
|
** a temporary file for transient pager files and statement journals.
|
|
|
|
** The value returned depends on the value of db->temp_store (runtime
|
|
|
|
** parameter) and the compile time value of SQLITE_TEMP_STORE. The
|
|
|
|
** following table describes the relationship between these two values
|
|
|
|
** and this functions return value.
|
|
|
|
**
|
|
|
|
** SQLITE_TEMP_STORE db->temp_store Location of temporary database
|
|
|
|
** ----------------- -------------- ------------------------------
|
|
|
|
** 0 any file (return 0)
|
|
|
|
** 1 1 file (return 0)
|
|
|
|
** 1 2 memory (return 1)
|
|
|
|
** 1 0 file (return 0)
|
|
|
|
** 2 1 file (return 0)
|
|
|
|
** 2 2 memory (return 1)
|
|
|
|
** 2 0 memory (return 1)
|
|
|
|
** 3 any memory (return 1)
|
|
|
|
*/
|
2009-04-30 16:25:10 +04:00
|
|
|
int sqlite3TempInMemory(const sqlite3 *db){
|
2009-04-30 13:10:37 +04:00
|
|
|
#if SQLITE_TEMP_STORE==1
|
|
|
|
return ( db->temp_store==2 );
|
|
|
|
#endif
|
|
|
|
#if SQLITE_TEMP_STORE==2
|
|
|
|
return ( db->temp_store!=1 );
|
|
|
|
#endif
|
|
|
|
#if SQLITE_TEMP_STORE==3
|
|
|
|
return 1;
|
2009-06-01 20:53:09 +04:00
|
|
|
#endif
|
|
|
|
#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
|
2009-04-30 13:10:37 +04:00
|
|
|
return 0;
|
2009-05-06 22:57:09 +04:00
|
|
|
#endif
|
2009-04-30 13:10:37 +04:00
|
|
|
}
|
|
|
|
|
2003-04-13 22:26:49 +04:00
|
|
|
/*
|
2003-04-15 05:19:47 +04:00
|
|
|
** This routine is called to create a connection to a database BTree
|
|
|
|
** driver. If zFilename is the name of a file, then that file is
|
|
|
|
** opened and used. If zFilename is the magic name ":memory:" then
|
|
|
|
** the database is stored in memory (and is thus forgotten as soon as
|
|
|
|
** the connection is closed.) If zFilename is NULL then the database
|
2005-07-15 17:05:21 +04:00
|
|
|
** is a "virtual" database for transient use only and is deleted as
|
|
|
|
** soon as the connection is closed.
|
2004-07-22 05:19:35 +04:00
|
|
|
**
|
2005-07-15 17:05:21 +04:00
|
|
|
** A virtual database can be either a disk file (that is automatically
|
2009-04-30 13:10:37 +04:00
|
|
|
** deleted when the file is closed) or it an be held entirely in memory.
|
|
|
|
** The sqlite3TempInMemory() function is used to determine which.
|
2003-04-13 22:26:49 +04:00
|
|
|
*/
|
2004-05-08 12:23:19 +04:00
|
|
|
int sqlite3BtreeFactory(
|
2009-11-04 16:30:01 +03:00
|
|
|
sqlite3 *db, /* Main database when opening aux otherwise 0 */
|
2003-04-13 22:26:49 +04:00
|
|
|
const char *zFilename, /* Name of the file containing the BTree database */
|
|
|
|
int omitJournal, /* if TRUE then do not journal this file */
|
|
|
|
int nCache, /* How many pages in the page cache */
|
2007-09-03 19:19:34 +04:00
|
|
|
int vfsFlags, /* Flags passed through to vfsOpen */
|
2004-05-08 12:23:19 +04:00
|
|
|
Btree **ppBtree /* Pointer to new Btree object written here */
|
|
|
|
){
|
2007-09-03 19:19:34 +04:00
|
|
|
int btFlags = 0;
|
2004-07-22 05:19:35 +04:00
|
|
|
int rc;
|
2004-05-08 12:23:19 +04:00
|
|
|
|
2007-08-21 20:15:55 +04:00
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
2004-05-08 14:11:36 +04:00
|
|
|
assert( ppBtree != 0);
|
2004-05-08 12:23:19 +04:00
|
|
|
if( omitJournal ){
|
2007-09-03 19:19:34 +04:00
|
|
|
btFlags |= BTREE_OMIT_JOURNAL;
|
2004-05-08 12:23:19 +04:00
|
|
|
}
|
2005-02-06 05:45:41 +03:00
|
|
|
if( db->flags & SQLITE_NoReadlock ){
|
2007-09-03 19:19:34 +04:00
|
|
|
btFlags |= BTREE_NO_READLOCK;
|
2005-02-06 05:45:41 +03:00
|
|
|
}
|
2004-11-22 08:26:27 +03:00
|
|
|
#ifndef SQLITE_OMIT_MEMORYDB
|
2009-04-30 13:10:37 +04:00
|
|
|
if( zFilename==0 && sqlite3TempInMemory(db) ){
|
2004-08-14 22:34:54 +04:00
|
|
|
zFilename = ":memory:";
|
2004-07-22 05:19:35 +04:00
|
|
|
}
|
2009-04-30 13:10:37 +04:00
|
|
|
#endif
|
2003-04-13 22:26:49 +04:00
|
|
|
|
2007-09-03 19:19:34 +04:00
|
|
|
if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
|
|
|
|
vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
|
|
|
|
}
|
|
|
|
rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
|
2008-06-23 13:50:50 +04:00
|
|
|
|
|
|
|
/* If the B-Tree was successfully opened, set the pager-cache size to the
|
|
|
|
** default value. Except, if the call to BtreeOpen() returned a handle
|
|
|
|
** open on an existing shared pager-cache, do not change the pager-cache
|
|
|
|
** size.
|
|
|
|
*/
|
|
|
|
if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){
|
2004-07-22 05:19:35 +04:00
|
|
|
sqlite3BtreeSetCacheSize(*ppBtree, nCache);
|
|
|
|
}
|
|
|
|
return rc;
|
2004-05-08 12:23:19 +04:00
|
|
|
}
|
|
|
|
|
2004-05-21 05:47:26 +04:00
|
|
|
/*
|
|
|
|
** Return UTF-8 encoded English language explanation of the most recent
|
|
|
|
** error.
|
|
|
|
*/
|
2004-05-20 15:00:52 +04:00
|
|
|
const char *sqlite3_errmsg(sqlite3 *db){
|
2004-09-30 17:43:13 +04:00
|
|
|
const char *z;
|
2008-12-10 19:45:50 +03:00
|
|
|
if( !db ){
|
2004-06-01 03:56:42 +04:00
|
|
|
return sqlite3ErrStr(SQLITE_NOMEM);
|
2004-05-21 05:47:26 +04:00
|
|
|
}
|
2008-07-07 21:53:07 +04:00
|
|
|
if( !sqlite3SafetyCheckSickOrOk(db) ){
|
2004-06-26 13:50:11 +04:00
|
|
|
return sqlite3ErrStr(SQLITE_MISUSE);
|
|
|
|
}
|
2007-08-22 06:56:42 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2009-03-19 21:51:06 +03:00
|
|
|
if( db->mallocFailed ){
|
|
|
|
z = sqlite3ErrStr(SQLITE_NOMEM);
|
|
|
|
}else{
|
|
|
|
z = (char*)sqlite3_value_text(db->pErr);
|
|
|
|
assert( !db->mallocFailed );
|
|
|
|
if( z==0 ){
|
|
|
|
z = sqlite3ErrStr(db->errCode);
|
|
|
|
}
|
2004-05-20 15:00:52 +04:00
|
|
|
}
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2004-09-30 17:43:13 +04:00
|
|
|
return z;
|
2004-05-20 15:00:52 +04:00
|
|
|
}
|
|
|
|
|
2004-11-15 00:56:29 +03:00
|
|
|
#ifndef SQLITE_OMIT_UTF16
|
2004-05-21 05:47:26 +04:00
|
|
|
/*
|
|
|
|
** Return UTF-16 encoded English language explanation of the most recent
|
|
|
|
** error.
|
|
|
|
*/
|
2004-05-20 15:00:52 +04:00
|
|
|
const void *sqlite3_errmsg16(sqlite3 *db){
|
2009-02-23 17:42:53 +03:00
|
|
|
static const u16 outOfMem[] = {
|
|
|
|
'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
|
2004-06-18 08:24:54 +04:00
|
|
|
};
|
2009-02-23 17:42:53 +03:00
|
|
|
static const u16 misuse[] = {
|
|
|
|
'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
|
|
|
|
'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
|
|
|
|
'c', 'a', 'l', 'l', 'e', 'd', ' ',
|
|
|
|
'o', 'u', 't', ' ',
|
|
|
|
'o', 'f', ' ',
|
|
|
|
's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
|
2004-06-26 13:50:11 +04:00
|
|
|
};
|
2004-06-18 08:24:54 +04:00
|
|
|
|
2004-09-30 17:43:13 +04:00
|
|
|
const void *z;
|
2007-03-29 19:00:52 +04:00
|
|
|
if( !db ){
|
2009-02-23 17:42:53 +03:00
|
|
|
return (void *)outOfMem;
|
2004-09-30 17:43:13 +04:00
|
|
|
}
|
2008-07-07 21:53:07 +04:00
|
|
|
if( !sqlite3SafetyCheckSickOrOk(db) ){
|
2009-02-23 17:42:53 +03:00
|
|
|
return (void *)misuse;
|
2004-09-30 17:43:13 +04:00
|
|
|
}
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2009-03-19 21:51:06 +03:00
|
|
|
if( db->mallocFailed ){
|
|
|
|
z = (void *)outOfMem;
|
|
|
|
}else{
|
2004-09-30 17:43:13 +04:00
|
|
|
z = sqlite3_value_text16(db->pErr);
|
2009-03-19 21:51:06 +03:00
|
|
|
if( z==0 ){
|
|
|
|
sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
|
|
|
|
SQLITE_UTF8, SQLITE_STATIC);
|
|
|
|
z = sqlite3_value_text16(db->pErr);
|
|
|
|
}
|
|
|
|
/* A malloc() may have failed within the call to sqlite3_value_text16()
|
|
|
|
** above. If this is the case, then the db->mallocFailed flag needs to
|
|
|
|
** be cleared before returning. Do this directly, instead of via
|
|
|
|
** sqlite3ApiExit(), to avoid setting the database handle error message.
|
|
|
|
*/
|
|
|
|
db->mallocFailed = 0;
|
2004-09-30 17:43:13 +04:00
|
|
|
}
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2004-09-30 17:43:13 +04:00
|
|
|
return z;
|
2004-05-20 15:00:52 +04:00
|
|
|
}
|
2004-11-15 00:56:29 +03:00
|
|
|
#endif /* SQLITE_OMIT_UTF16 */
|
2004-05-20 15:00:52 +04:00
|
|
|
|
2004-09-30 17:43:13 +04:00
|
|
|
/*
|
2005-12-12 09:53:03 +03:00
|
|
|
** Return the most recent error code generated by an SQLite routine. If NULL is
|
|
|
|
** passed to this function, we assume a malloc() failed during sqlite3_open().
|
2004-09-30 17:43:13 +04:00
|
|
|
*/
|
2004-05-20 15:00:52 +04:00
|
|
|
int sqlite3_errcode(sqlite3 *db){
|
2008-01-31 18:31:01 +03:00
|
|
|
if( db && !sqlite3SafetyCheckSickOrOk(db) ){
|
2004-09-30 17:43:13 +04:00
|
|
|
return SQLITE_MISUSE;
|
|
|
|
}
|
2008-01-23 15:52:40 +03:00
|
|
|
if( !db || db->mallocFailed ){
|
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
2006-09-15 11:28:50 +04:00
|
|
|
return db->errCode & db->errMask;
|
2004-05-20 15:00:52 +04:00
|
|
|
}
|
2008-10-30 18:03:15 +03:00
|
|
|
int sqlite3_extended_errcode(sqlite3 *db){
|
|
|
|
if( db && !sqlite3SafetyCheckSickOrOk(db) ){
|
|
|
|
return SQLITE_MISUSE;
|
|
|
|
}
|
|
|
|
if( !db || db->mallocFailed ){
|
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
return db->errCode;
|
|
|
|
}
|
2004-05-20 15:00:52 +04:00
|
|
|
|
2006-02-01 16:50:41 +03:00
|
|
|
/*
|
|
|
|
** Create a new collating function for database "db". The name is zName
|
|
|
|
** and the encoding is enc.
|
|
|
|
*/
|
2006-01-18 07:26:07 +03:00
|
|
|
static int createCollation(
|
2009-08-20 06:49:30 +04:00
|
|
|
sqlite3* db,
|
2006-01-18 07:26:07 +03:00
|
|
|
const char *zName,
|
2009-09-07 08:38:36 +04:00
|
|
|
u8 enc,
|
|
|
|
u8 collType,
|
2006-01-18 07:26:07 +03:00
|
|
|
void* pCtx,
|
2007-05-07 13:32:45 +04:00
|
|
|
int(*xCompare)(void*,int,const void*,int,const void*),
|
|
|
|
void(*xDel)(void*)
|
2006-01-18 07:26:07 +03:00
|
|
|
){
|
|
|
|
CollSeq *pColl;
|
2006-02-16 21:16:36 +03:00
|
|
|
int enc2;
|
2009-05-12 00:53:28 +04:00
|
|
|
int nName = sqlite3Strlen30(zName);
|
2006-01-18 07:26:07 +03:00
|
|
|
|
2007-08-21 20:15:55 +04:00
|
|
|
assert( sqlite3_mutex_held(db->mutex) );
|
2006-01-18 07:26:07 +03:00
|
|
|
|
|
|
|
/* If SQLITE_UTF16 is specified as the encoding type, transform this
|
|
|
|
** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
|
|
|
|
** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
|
|
|
|
*/
|
2009-04-27 22:46:06 +04:00
|
|
|
enc2 = enc;
|
2009-04-28 19:35:38 +04:00
|
|
|
testcase( enc2==SQLITE_UTF16 );
|
|
|
|
testcase( enc2==SQLITE_UTF16_ALIGNED );
|
|
|
|
if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
|
2006-02-16 21:16:36 +03:00
|
|
|
enc2 = SQLITE_UTF16NATIVE;
|
2006-01-18 07:26:07 +03:00
|
|
|
}
|
2009-04-27 22:46:06 +04:00
|
|
|
if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
|
2008-07-07 21:53:07 +04:00
|
|
|
return SQLITE_MISUSE;
|
2006-01-18 07:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this call is removing or replacing an existing collation
|
|
|
|
** sequence. If so, and there are active VMs, return busy. If there
|
|
|
|
** are no active VMs, invalidate any pre-compiled statements.
|
|
|
|
*/
|
2009-05-12 00:53:28 +04:00
|
|
|
pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
|
2006-01-18 07:26:07 +03:00
|
|
|
if( pColl && pColl->xCmp ){
|
|
|
|
if( db->activeVdbeCnt ){
|
|
|
|
sqlite3Error(db, SQLITE_BUSY,
|
2009-02-04 20:40:57 +03:00
|
|
|
"unable to delete/modify collation sequence due to active statements");
|
2006-01-18 07:26:07 +03:00
|
|
|
return SQLITE_BUSY;
|
|
|
|
}
|
|
|
|
sqlite3ExpirePreparedStatements(db);
|
2007-05-07 13:32:45 +04:00
|
|
|
|
|
|
|
/* If collation sequence pColl was created directly by a call to
|
|
|
|
** sqlite3_create_collation, and not generated by synthCollSeq(),
|
|
|
|
** then any copies made by synthCollSeq() need to be invalidated.
|
|
|
|
** Also, collation destructor - CollSeq.xDel() - function may need
|
|
|
|
** to be called.
|
|
|
|
*/
|
|
|
|
if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
|
2008-07-08 18:52:07 +04:00
|
|
|
CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
|
2007-05-07 13:32:45 +04:00
|
|
|
int j;
|
|
|
|
for(j=0; j<3; j++){
|
|
|
|
CollSeq *p = &aColl[j];
|
|
|
|
if( p->enc==pColl->enc ){
|
|
|
|
if( p->xDel ){
|
|
|
|
p->xDel(p->pUser);
|
|
|
|
}
|
|
|
|
p->xCmp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-18 07:26:07 +03:00
|
|
|
}
|
|
|
|
|
2009-05-12 00:53:28 +04:00
|
|
|
pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
|
2006-01-18 07:26:07 +03:00
|
|
|
if( pColl ){
|
|
|
|
pColl->xCmp = xCompare;
|
|
|
|
pColl->pUser = pCtx;
|
2007-05-07 13:32:45 +04:00
|
|
|
pColl->xDel = xDel;
|
2008-12-11 00:19:56 +03:00
|
|
|
pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
|
2009-08-20 06:49:30 +04:00
|
|
|
pColl->type = collType;
|
2006-01-18 07:26:07 +03:00
|
|
|
}
|
|
|
|
sqlite3Error(db, SQLITE_OK, 0);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-20 17:03:29 +03:00
|
|
|
/*
|
|
|
|
** This array defines hard upper bounds on limit values. The
|
|
|
|
** initializer must be kept in sync with the SQLITE_LIMIT_*
|
|
|
|
** #defines in sqlite3.h.
|
|
|
|
*/
|
2008-03-20 19:30:17 +03:00
|
|
|
static const int aHardLimit[] = {
|
2008-03-20 17:03:29 +03:00
|
|
|
SQLITE_MAX_LENGTH,
|
|
|
|
SQLITE_MAX_SQL_LENGTH,
|
|
|
|
SQLITE_MAX_COLUMN,
|
|
|
|
SQLITE_MAX_EXPR_DEPTH,
|
|
|
|
SQLITE_MAX_COMPOUND_SELECT,
|
|
|
|
SQLITE_MAX_VDBE_OP,
|
|
|
|
SQLITE_MAX_FUNCTION_ARG,
|
|
|
|
SQLITE_MAX_ATTACHED,
|
|
|
|
SQLITE_MAX_LIKE_PATTERN_LENGTH,
|
|
|
|
SQLITE_MAX_VARIABLE_NUMBER,
|
2009-09-07 22:14:02 +04:00
|
|
|
SQLITE_MAX_TRIGGER_DEPTH,
|
2008-03-20 17:03:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Make sure the hard limits are set to reasonable values
|
|
|
|
*/
|
|
|
|
#if SQLITE_MAX_LENGTH<100
|
|
|
|
# error SQLITE_MAX_LENGTH must be at least 100
|
|
|
|
#endif
|
|
|
|
#if SQLITE_MAX_SQL_LENGTH<100
|
|
|
|
# error SQLITE_MAX_SQL_LENGTH must be at least 100
|
|
|
|
#endif
|
|
|
|
#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
|
|
|
|
# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
|
|
|
|
#endif
|
|
|
|
#if SQLITE_MAX_COMPOUND_SELECT<2
|
|
|
|
# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
|
|
|
|
#endif
|
|
|
|
#if SQLITE_MAX_VDBE_OP<40
|
|
|
|
# error SQLITE_MAX_VDBE_OP must be at least 40
|
|
|
|
#endif
|
2008-10-07 23:53:14 +04:00
|
|
|
#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
|
|
|
|
# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
|
2008-03-20 17:03:29 +03:00
|
|
|
#endif
|
2008-08-20 20:21:12 +04:00
|
|
|
#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>30
|
|
|
|
# error SQLITE_MAX_ATTACHED must be between 0 and 30
|
2008-03-20 17:03:29 +03:00
|
|
|
#endif
|
|
|
|
#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
|
|
|
|
# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
|
|
|
|
#endif
|
2008-08-21 22:49:27 +04:00
|
|
|
#if SQLITE_MAX_COLUMN>32767
|
|
|
|
# error SQLITE_MAX_COLUMN must not exceed 32767
|
|
|
|
#endif
|
2009-09-07 22:14:02 +04:00
|
|
|
#if SQLITE_MAX_TRIGGER_DEPTH<1
|
|
|
|
# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
|
|
|
|
#endif
|
2008-03-20 17:03:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Change the value of a limit. Report the old value.
|
|
|
|
** If an invalid limit index is supplied, report -1.
|
|
|
|
** Make no changes but still report the old value if the
|
|
|
|
** new limit is negative.
|
|
|
|
**
|
|
|
|
** A new lower limit does not shrink existing constructs.
|
|
|
|
** It merely prevents new constructs that exceed the limit
|
|
|
|
** from forming.
|
|
|
|
*/
|
|
|
|
int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
|
|
|
|
int oldLimit;
|
2008-04-15 06:36:33 +04:00
|
|
|
if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
|
2008-03-20 17:03:29 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
oldLimit = db->aLimit[limitId];
|
|
|
|
if( newLimit>=0 ){
|
2008-03-20 21:00:49 +03:00
|
|
|
if( newLimit>aHardLimit[limitId] ){
|
2008-03-20 17:03:29 +03:00
|
|
|
newLimit = aHardLimit[limitId];
|
|
|
|
}
|
|
|
|
db->aLimit[limitId] = newLimit;
|
|
|
|
}
|
|
|
|
return oldLimit;
|
|
|
|
}
|
|
|
|
|
2004-05-21 05:47:26 +04:00
|
|
|
/*
|
|
|
|
** This routine does the work of opening a database on behalf of
|
|
|
|
** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
|
2005-04-28 16:06:05 +04:00
|
|
|
** is UTF-8 encoded.
|
2004-05-21 05:47:26 +04:00
|
|
|
*/
|
|
|
|
static int openDatabase(
|
|
|
|
const char *zFilename, /* Database filename UTF-8 encoded */
|
2007-08-21 19:13:19 +04:00
|
|
|
sqlite3 **ppDb, /* OUT: Returned database handle */
|
|
|
|
unsigned flags, /* Operational flags */
|
|
|
|
const char *zVfs /* Name of the VFS to use */
|
2004-05-21 05:47:26 +04:00
|
|
|
){
|
|
|
|
sqlite3 *db;
|
2006-01-05 14:34:32 +03:00
|
|
|
int rc;
|
2008-09-03 04:43:15 +04:00
|
|
|
int isThreadsafe;
|
2004-05-21 05:47:26 +04:00
|
|
|
|
2009-04-28 08:47:31 +04:00
|
|
|
*ppDb = 0;
|
2008-06-13 22:24:27 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
rc = sqlite3_initialize();
|
|
|
|
if( rc ) return rc;
|
|
|
|
#endif
|
|
|
|
|
2008-09-03 04:43:15 +04:00
|
|
|
if( sqlite3GlobalConfig.bCoreMutex==0 ){
|
2008-07-10 21:52:49 +04:00
|
|
|
isThreadsafe = 0;
|
2008-09-03 04:43:15 +04:00
|
|
|
}else if( flags & SQLITE_OPEN_NOMUTEX ){
|
|
|
|
isThreadsafe = 0;
|
|
|
|
}else if( flags & SQLITE_OPEN_FULLMUTEX ){
|
|
|
|
isThreadsafe = 1;
|
|
|
|
}else{
|
|
|
|
isThreadsafe = sqlite3GlobalConfig.bFullMutex;
|
2008-07-10 21:52:49 +04:00
|
|
|
}
|
2009-09-09 18:17:52 +04:00
|
|
|
if( flags & SQLITE_OPEN_PRIVATECACHE ){
|
2009-09-09 19:29:41 +04:00
|
|
|
flags &= ~SQLITE_OPEN_SHAREDCACHE;
|
2009-09-09 18:17:52 +04:00
|
|
|
}else if( sqlite3GlobalConfig.sharedCacheEnabled ){
|
|
|
|
flags |= SQLITE_OPEN_SHAREDCACHE;
|
|
|
|
}
|
2008-07-10 21:52:49 +04:00
|
|
|
|
2009-05-12 17:35:11 +04:00
|
|
|
/* Remove harmful bits from the flags parameter
|
|
|
|
**
|
|
|
|
** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
|
|
|
|
** dealt with in the previous code block. Besides these, the only
|
|
|
|
** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
|
|
|
|
** SQLITE_OPEN_READWRITE, and SQLITE_OPEN_CREATE. Silently mask
|
|
|
|
** off all other flags.
|
|
|
|
*/
|
2008-02-19 18:20:44 +03:00
|
|
|
flags &= ~( SQLITE_OPEN_DELETEONCLOSE |
|
2009-05-12 17:35:11 +04:00
|
|
|
SQLITE_OPEN_EXCLUSIVE |
|
2008-02-19 18:20:44 +03:00
|
|
|
SQLITE_OPEN_MAIN_DB |
|
|
|
|
SQLITE_OPEN_TEMP_DB |
|
|
|
|
SQLITE_OPEN_TRANSIENT_DB |
|
|
|
|
SQLITE_OPEN_MAIN_JOURNAL |
|
|
|
|
SQLITE_OPEN_TEMP_JOURNAL |
|
|
|
|
SQLITE_OPEN_SUBJOURNAL |
|
2008-07-10 21:52:49 +04:00
|
|
|
SQLITE_OPEN_MASTER_JOURNAL |
|
2008-09-03 04:43:15 +04:00
|
|
|
SQLITE_OPEN_NOMUTEX |
|
|
|
|
SQLITE_OPEN_FULLMUTEX
|
2008-02-19 18:20:44 +03:00
|
|
|
);
|
|
|
|
|
2004-05-21 05:47:26 +04:00
|
|
|
/* Allocate the sqlite data structure */
|
2007-08-16 08:30:38 +04:00
|
|
|
db = sqlite3MallocZero( sizeof(sqlite3) );
|
2004-05-21 05:47:26 +04:00
|
|
|
if( db==0 ) goto opendb_out;
|
2008-09-03 04:43:15 +04:00
|
|
|
if( isThreadsafe ){
|
2008-06-18 21:09:10 +04:00
|
|
|
db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
|
|
|
|
if( db->mutex==0 ){
|
|
|
|
sqlite3_free(db);
|
|
|
|
db = 0;
|
|
|
|
goto opendb_out;
|
|
|
|
}
|
2007-08-21 19:13:19 +04:00
|
|
|
}
|
2007-08-22 06:56:42 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2006-09-15 11:28:50 +04:00
|
|
|
db->errMask = 0xff;
|
2004-05-21 05:47:26 +04:00
|
|
|
db->nDb = 2;
|
2007-08-24 07:51:33 +04:00
|
|
|
db->magic = SQLITE_MAGIC_BUSY;
|
2004-05-21 05:47:26 +04:00
|
|
|
db->aDb = db->aDbStatic;
|
2008-07-28 23:34:53 +04:00
|
|
|
|
2008-03-20 17:03:29 +03:00
|
|
|
assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
|
|
|
|
memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
|
2004-05-31 12:26:49 +04:00
|
|
|
db->autoCommit = 1;
|
2007-12-05 04:38:23 +03:00
|
|
|
db->nextAutovac = -1;
|
2008-03-20 14:04:21 +03:00
|
|
|
db->nextPagesize = 0;
|
2006-07-11 18:17:51 +04:00
|
|
|
db->flags |= SQLITE_ShortColNames
|
|
|
|
#if SQLITE_DEFAULT_FILE_FORMAT<4
|
|
|
|
| SQLITE_LegacyFileFmt
|
2007-03-28 02:24:11 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_ENABLE_LOAD_EXTENSION
|
|
|
|
| SQLITE_LoadExtension
|
2009-08-30 15:42:51 +04:00
|
|
|
#endif
|
2009-09-01 21:11:07 +04:00
|
|
|
#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
|
2009-09-01 21:28:29 +04:00
|
|
|
| SQLITE_RecTriggers
|
2006-07-11 18:17:51 +04:00
|
|
|
#endif
|
|
|
|
;
|
2009-05-02 17:29:37 +04:00
|
|
|
sqlite3HashInit(&db->aCollSeq);
|
2006-06-12 03:41:55 +04:00
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2009-05-02 17:29:37 +04:00
|
|
|
sqlite3HashInit(&db->aModule);
|
2006-06-12 03:41:55 +04:00
|
|
|
#endif
|
2006-01-05 14:34:32 +03:00
|
|
|
|
2007-09-01 10:51:27 +04:00
|
|
|
db->pVfs = sqlite3_vfs_find(zVfs);
|
|
|
|
if( !db->pVfs ){
|
|
|
|
rc = SQLITE_ERROR;
|
2008-02-21 05:09:45 +03:00
|
|
|
sqlite3Error(db, rc, "no such vfs: %s", zVfs);
|
2007-09-01 10:51:27 +04:00
|
|
|
goto opendb_out;
|
|
|
|
}
|
|
|
|
|
2004-06-09 13:55:16 +04:00
|
|
|
/* Add the default collation sequence BINARY. BINARY works for both UTF-8
|
|
|
|
** and UTF-16, so add a version for each to avoid any unnecessary
|
|
|
|
** conversions. The only error that can occur here is a malloc() failure.
|
|
|
|
*/
|
2009-08-20 06:49:30 +04:00
|
|
|
createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
|
|
|
|
binCollFunc, 0);
|
|
|
|
createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
|
|
|
|
binCollFunc, 0);
|
|
|
|
createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
|
|
|
|
binCollFunc, 0);
|
|
|
|
createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
|
|
|
|
binCollFunc, 0);
|
2008-03-18 16:01:38 +03:00
|
|
|
if( db->mallocFailed ){
|
2004-06-09 13:55:16 +04:00
|
|
|
goto opendb_out;
|
|
|
|
}
|
2009-05-12 00:53:28 +04:00
|
|
|
db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
|
2008-03-18 16:01:38 +03:00
|
|
|
assert( db->pDfltColl!=0 );
|
2004-06-09 13:55:16 +04:00
|
|
|
|
|
|
|
/* Also add a UTF-8 case-insensitive collation sequence. */
|
2009-08-20 06:49:30 +04:00
|
|
|
createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
|
|
|
|
nocaseCollatingFunc, 0);
|
2005-08-28 21:00:23 +04:00
|
|
|
|
2004-05-21 05:47:26 +04:00
|
|
|
/* Open the backend database driver */
|
2007-09-03 19:19:34 +04:00
|
|
|
db->openFlags = flags;
|
|
|
|
rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE,
|
|
|
|
flags | SQLITE_OPEN_MAIN_DB,
|
2007-05-08 05:08:49 +04:00
|
|
|
&db->aDb[0].pBt);
|
2004-05-21 05:47:26 +04:00
|
|
|
if( rc!=SQLITE_OK ){
|
2008-09-23 21:39:26 +04:00
|
|
|
if( rc==SQLITE_IOERR_NOMEM ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}
|
2004-05-21 05:47:26 +04:00
|
|
|
sqlite3Error(db, rc, 0);
|
|
|
|
goto opendb_out;
|
|
|
|
}
|
2007-08-16 08:30:38 +04:00
|
|
|
db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
|
|
|
|
db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
|
2006-01-09 19:12:04 +03:00
|
|
|
|
2006-03-13 18:06:05 +03:00
|
|
|
|
2005-03-29 07:10:59 +04:00
|
|
|
/* The default safety_level for the main database is 'full'; for the temp
|
|
|
|
** database it is 'NONE'. This matches the pager layer defaults.
|
|
|
|
*/
|
|
|
|
db->aDb[0].zName = "main";
|
2004-06-26 10:37:06 +04:00
|
|
|
db->aDb[0].safety_level = 3;
|
2005-03-29 07:10:59 +04:00
|
|
|
db->aDb[1].zName = "temp";
|
2004-06-26 10:37:06 +04:00
|
|
|
db->aDb[1].safety_level = 1;
|
2005-03-29 07:10:59 +04:00
|
|
|
|
2007-06-22 19:21:15 +04:00
|
|
|
db->magic = SQLITE_MAGIC_OPEN;
|
2007-08-16 08:30:38 +04:00
|
|
|
if( db->mallocFailed ){
|
2007-06-22 19:21:15 +04:00
|
|
|
goto opendb_out;
|
|
|
|
}
|
|
|
|
|
2004-06-07 11:52:17 +04:00
|
|
|
/* Register all built-in functions, but do not attempt to read the
|
|
|
|
** database schema yet. This is delayed until the first time the database
|
|
|
|
** is accessed.
|
|
|
|
*/
|
2007-06-22 19:21:15 +04:00
|
|
|
sqlite3Error(db, SQLITE_OK, 0);
|
|
|
|
sqlite3RegisterBuiltinFunctions(db);
|
2004-05-21 05:47:26 +04:00
|
|
|
|
2006-08-24 00:07:20 +04:00
|
|
|
/* Load automatic extensions - extensions that have been registered
|
|
|
|
** using the sqlite3_automatic_extension() API.
|
|
|
|
*/
|
2009-05-20 06:40:45 +04:00
|
|
|
sqlite3AutoLoadExtensions(db);
|
|
|
|
rc = sqlite3_errcode(db);
|
|
|
|
if( rc!=SQLITE_OK ){
|
2007-06-22 19:21:15 +04:00
|
|
|
goto opendb_out;
|
|
|
|
}
|
2006-08-24 00:07:20 +04:00
|
|
|
|
2006-09-02 17:58:06 +04:00
|
|
|
#ifdef SQLITE_ENABLE_FTS1
|
2007-08-16 08:30:38 +04:00
|
|
|
if( !db->mallocFailed ){
|
2006-09-02 17:58:06 +04:00
|
|
|
extern int sqlite3Fts1Init(sqlite3*);
|
2007-06-22 19:21:15 +04:00
|
|
|
rc = sqlite3Fts1Init(db);
|
2006-09-02 17:58:06 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-20 00:27:58 +04:00
|
|
|
#ifdef SQLITE_ENABLE_FTS2
|
2007-08-16 08:30:38 +04:00
|
|
|
if( !db->mallocFailed && rc==SQLITE_OK ){
|
2006-10-20 00:27:58 +04:00
|
|
|
extern int sqlite3Fts2Init(sqlite3*);
|
2007-06-22 19:21:15 +04:00
|
|
|
rc = sqlite3Fts2Init(db);
|
2006-10-20 00:27:58 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-20 21:37:47 +04:00
|
|
|
#ifdef SQLITE_ENABLE_FTS3
|
|
|
|
if( !db->mallocFailed && rc==SQLITE_OK ){
|
|
|
|
rc = sqlite3Fts3Init(db);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-06 20:04:11 +04:00
|
|
|
#ifdef SQLITE_ENABLE_ICU
|
2007-08-16 08:30:38 +04:00
|
|
|
if( !db->mallocFailed && rc==SQLITE_OK ){
|
2007-06-22 19:21:15 +04:00
|
|
|
rc = sqlite3IcuInit(db);
|
2007-05-06 20:04:11 +04:00
|
|
|
}
|
|
|
|
#endif
|
2008-05-26 22:41:54 +04:00
|
|
|
|
|
|
|
#ifdef SQLITE_ENABLE_RTREE
|
|
|
|
if( !db->mallocFailed && rc==SQLITE_OK){
|
|
|
|
rc = sqlite3RtreeInit(db);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-22 19:21:15 +04:00
|
|
|
sqlite3Error(db, rc, 0);
|
2007-05-06 20:04:11 +04:00
|
|
|
|
2007-04-02 20:40:37 +04:00
|
|
|
/* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
|
|
|
|
** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
|
|
|
|
** mode. Doing nothing at all also makes NORMAL the default.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_DEFAULT_LOCKING_MODE
|
|
|
|
db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
|
|
|
|
sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
|
|
|
|
SQLITE_DEFAULT_LOCKING_MODE);
|
|
|
|
#endif
|
|
|
|
|
2008-07-28 23:34:53 +04:00
|
|
|
/* Enable the lookaside-malloc subsystem */
|
2008-10-10 21:41:28 +04:00
|
|
|
setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
|
|
|
|
sqlite3GlobalConfig.nLookaside);
|
2008-07-28 23:34:53 +04:00
|
|
|
|
2004-05-21 05:47:26 +04:00
|
|
|
opendb_out:
|
2008-02-21 05:09:45 +03:00
|
|
|
if( db ){
|
2008-09-01 22:34:20 +04:00
|
|
|
assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
|
2007-08-23 00:18:21 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
}
|
2008-08-27 23:01:57 +04:00
|
|
|
rc = sqlite3_errcode(db);
|
|
|
|
if( rc==SQLITE_NOMEM ){
|
2005-12-12 09:53:03 +03:00
|
|
|
sqlite3_close(db);
|
|
|
|
db = 0;
|
2008-08-27 23:01:57 +04:00
|
|
|
}else if( rc!=SQLITE_OK ){
|
|
|
|
db->magic = SQLITE_MAGIC_SICK;
|
2004-06-30 15:54:06 +04:00
|
|
|
}
|
2004-05-21 05:47:26 +04:00
|
|
|
*ppDb = db;
|
2006-01-18 18:25:17 +03:00
|
|
|
return sqlite3ApiExit(0, rc);
|
2004-05-21 05:47:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Open a new database handle.
|
|
|
|
*/
|
2004-05-22 13:21:21 +04:00
|
|
|
int sqlite3_open(
|
2004-05-21 05:47:26 +04:00
|
|
|
const char *zFilename,
|
2004-06-08 04:02:33 +04:00
|
|
|
sqlite3 **ppDb
|
2004-05-21 05:47:26 +04:00
|
|
|
){
|
2007-08-21 19:13:19 +04:00
|
|
|
return openDatabase(zFilename, ppDb,
|
|
|
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
|
|
|
|
}
|
|
|
|
int sqlite3_open_v2(
|
2007-08-30 20:23:19 +04:00
|
|
|
const char *filename, /* Database filename (UTF-8) */
|
2007-08-21 19:13:19 +04:00
|
|
|
sqlite3 **ppDb, /* OUT: SQLite db handle */
|
|
|
|
int flags, /* Flags */
|
|
|
|
const char *zVfs /* Name of VFS module to use */
|
|
|
|
){
|
|
|
|
return openDatabase(filename, ppDb, flags, zVfs);
|
2004-05-21 15:39:05 +04:00
|
|
|
}
|
|
|
|
|
2004-11-15 00:56:29 +03:00
|
|
|
#ifndef SQLITE_OMIT_UTF16
|
2004-05-21 05:47:26 +04:00
|
|
|
/*
|
|
|
|
** Open a new database handle.
|
|
|
|
*/
|
|
|
|
int sqlite3_open16(
|
|
|
|
const void *zFilename,
|
2004-06-08 04:02:33 +04:00
|
|
|
sqlite3 **ppDb
|
2004-05-21 05:47:26 +04:00
|
|
|
){
|
2004-06-18 08:24:54 +04:00
|
|
|
char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
|
|
|
|
sqlite3_value *pVal;
|
2008-06-13 22:24:27 +04:00
|
|
|
int rc;
|
2004-05-21 05:47:26 +04:00
|
|
|
|
2005-12-12 09:53:03 +03:00
|
|
|
assert( zFilename );
|
2004-05-21 05:47:26 +04:00
|
|
|
assert( ppDb );
|
2004-06-18 08:24:54 +04:00
|
|
|
*ppDb = 0;
|
2008-06-13 22:24:27 +04:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
rc = sqlite3_initialize();
|
|
|
|
if( rc ) return rc;
|
|
|
|
#endif
|
2007-08-16 14:09:01 +04:00
|
|
|
pVal = sqlite3ValueNew(0);
|
2007-08-21 23:33:56 +04:00
|
|
|
sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
|
|
|
|
zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
|
2004-06-18 08:24:54 +04:00
|
|
|
if( zFilename8 ){
|
2007-08-21 19:13:19 +04:00
|
|
|
rc = openDatabase(zFilename8, ppDb,
|
|
|
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
|
2008-02-21 05:09:45 +03:00
|
|
|
assert( *ppDb || rc==SQLITE_NOMEM );
|
2008-04-28 20:19:35 +04:00
|
|
|
if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
|
2008-04-03 20:28:24 +04:00
|
|
|
ENC(*ppDb) = SQLITE_UTF16NATIVE;
|
2004-06-18 08:24:54 +04:00
|
|
|
}
|
2008-06-13 22:24:27 +04:00
|
|
|
}else{
|
|
|
|
rc = SQLITE_NOMEM;
|
2004-05-21 05:47:26 +04:00
|
|
|
}
|
2005-12-12 09:53:03 +03:00
|
|
|
sqlite3ValueFree(pVal);
|
2004-06-07 11:52:17 +04:00
|
|
|
|
2006-01-18 18:25:17 +03:00
|
|
|
return sqlite3ApiExit(0, rc);
|
2004-05-21 05:47:26 +04:00
|
|
|
}
|
2004-11-15 00:56:29 +03:00
|
|
|
#endif /* SQLITE_OMIT_UTF16 */
|
2004-05-21 05:47:26 +04:00
|
|
|
|
2004-06-12 13:25:12 +04:00
|
|
|
/*
|
|
|
|
** Register a new collation sequence with the database handle db.
|
|
|
|
*/
|
2004-06-09 13:55:16 +04:00
|
|
|
int sqlite3_create_collation(
|
|
|
|
sqlite3* db,
|
|
|
|
const char *zName,
|
2004-06-10 06:16:01 +04:00
|
|
|
int enc,
|
2004-06-09 13:55:16 +04:00
|
|
|
void* pCtx,
|
|
|
|
int(*xCompare)(void*,int,const void*,int,const void*)
|
|
|
|
){
|
2006-01-18 07:26:07 +03:00
|
|
|
int rc;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2007-08-16 08:30:38 +04:00
|
|
|
assert( !db->mallocFailed );
|
2009-09-07 08:38:36 +04:00
|
|
|
rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = sqlite3ApiExit(db, rc);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return rc;
|
2007-05-07 13:32:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Register a new collation sequence with the database handle db.
|
|
|
|
*/
|
2007-05-07 18:58:53 +04:00
|
|
|
int sqlite3_create_collation_v2(
|
2007-05-07 13:32:45 +04:00
|
|
|
sqlite3* db,
|
|
|
|
const char *zName,
|
|
|
|
int enc,
|
|
|
|
void* pCtx,
|
|
|
|
int(*xCompare)(void*,int,const void*,int,const void*),
|
|
|
|
void(*xDel)(void*)
|
|
|
|
){
|
|
|
|
int rc;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2007-08-16 08:30:38 +04:00
|
|
|
assert( !db->mallocFailed );
|
2009-09-07 08:38:36 +04:00
|
|
|
rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = sqlite3ApiExit(db, rc);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return rc;
|
2004-06-09 13:55:16 +04:00
|
|
|
}
|
|
|
|
|
2004-11-15 00:56:29 +03:00
|
|
|
#ifndef SQLITE_OMIT_UTF16
|
2004-06-12 13:25:12 +04:00
|
|
|
/*
|
|
|
|
** Register a new collation sequence with the database handle db.
|
|
|
|
*/
|
2004-06-09 13:55:16 +04:00
|
|
|
int sqlite3_create_collation16(
|
|
|
|
sqlite3* db,
|
2008-06-23 15:23:14 +04:00
|
|
|
const void *zName,
|
2004-06-10 06:16:01 +04:00
|
|
|
int enc,
|
2004-06-09 13:55:16 +04:00
|
|
|
void* pCtx,
|
|
|
|
int(*xCompare)(void*,int,const void*,int,const void*)
|
|
|
|
){
|
2006-01-18 07:26:07 +03:00
|
|
|
int rc = SQLITE_OK;
|
2008-01-23 15:52:40 +03:00
|
|
|
char *zName8;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2007-08-16 08:30:38 +04:00
|
|
|
assert( !db->mallocFailed );
|
2007-08-16 14:09:01 +04:00
|
|
|
zName8 = sqlite3Utf16to8(db, zName, -1);
|
2006-01-18 07:26:07 +03:00
|
|
|
if( zName8 ){
|
2009-09-07 08:38:36 +04:00
|
|
|
rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, zName8);
|
2006-01-18 07:26:07 +03:00
|
|
|
}
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = sqlite3ApiExit(db, rc);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return rc;
|
2004-06-09 13:55:16 +04:00
|
|
|
}
|
2004-11-15 00:56:29 +03:00
|
|
|
#endif /* SQLITE_OMIT_UTF16 */
|
2004-06-10 14:50:08 +04:00
|
|
|
|
2004-06-12 13:25:12 +04:00
|
|
|
/*
|
|
|
|
** Register a collation sequence factory callback with the database handle
|
|
|
|
** db. Replace any previously installed collation sequence factory.
|
|
|
|
*/
|
2004-06-10 14:50:08 +04:00
|
|
|
int sqlite3_collation_needed(
|
|
|
|
sqlite3 *db,
|
|
|
|
void *pCollNeededArg,
|
|
|
|
void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
|
|
|
|
){
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2004-06-10 14:50:08 +04:00
|
|
|
db->xCollNeeded = xCollNeeded;
|
|
|
|
db->xCollNeeded16 = 0;
|
|
|
|
db->pCollNeededArg = pCollNeededArg;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2004-06-10 14:50:08 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2004-06-12 13:25:12 +04:00
|
|
|
|
2004-11-15 00:56:29 +03:00
|
|
|
#ifndef SQLITE_OMIT_UTF16
|
2004-06-12 13:25:12 +04:00
|
|
|
/*
|
|
|
|
** Register a collation sequence factory callback with the database handle
|
|
|
|
** db. Replace any previously installed collation sequence factory.
|
|
|
|
*/
|
2004-06-10 14:50:08 +04:00
|
|
|
int sqlite3_collation_needed16(
|
|
|
|
sqlite3 *db,
|
|
|
|
void *pCollNeededArg,
|
|
|
|
void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
|
|
|
|
){
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2004-06-10 14:50:08 +04:00
|
|
|
db->xCollNeeded = 0;
|
|
|
|
db->xCollNeeded16 = xCollNeeded16;
|
|
|
|
db->pCollNeededArg = pCollNeededArg;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2004-06-10 14:50:08 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2004-11-15 00:56:29 +03:00
|
|
|
#endif /* SQLITE_OMIT_UTF16 */
|
2005-03-21 07:04:02 +03:00
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_GLOBALRECOVER
|
2008-10-12 04:27:53 +04:00
|
|
|
#ifndef SQLITE_OMIT_DEPRECATED
|
2005-03-21 07:04:02 +03:00
|
|
|
/*
|
2005-12-12 09:53:03 +03:00
|
|
|
** This function is now an anachronism. It used to be used to recover from a
|
|
|
|
** malloc() failure, but SQLite now does this automatically.
|
2005-03-21 07:04:02 +03:00
|
|
|
*/
|
2007-10-12 23:35:48 +04:00
|
|
|
int sqlite3_global_recover(void){
|
2005-12-06 15:52:59 +03:00
|
|
|
return SQLITE_OK;
|
2005-03-21 07:04:02 +03:00
|
|
|
}
|
|
|
|
#endif
|
2008-10-12 04:27:53 +04:00
|
|
|
#endif
|
2005-05-26 20:23:34 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Test to see whether or not the database connection is in autocommit
|
|
|
|
** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on
|
|
|
|
** by default. Autocommit is disabled by a BEGIN statement and reenabled
|
|
|
|
** by the next COMMIT or ROLLBACK.
|
|
|
|
**
|
|
|
|
******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
|
|
|
|
*/
|
|
|
|
int sqlite3_get_autocommit(sqlite3 *db){
|
|
|
|
return db->autoCommit;
|
|
|
|
}
|
2005-09-17 19:20:26 +04:00
|
|
|
|
|
|
|
#ifdef SQLITE_DEBUG
|
|
|
|
/*
|
|
|
|
** The following routine is subtituted for constant SQLITE_CORRUPT in
|
|
|
|
** debugging builds. This provides a way to set a breakpoint for when
|
|
|
|
** corruption is first detected.
|
|
|
|
*/
|
|
|
|
int sqlite3Corrupt(void){
|
|
|
|
return SQLITE_CORRUPT;
|
|
|
|
}
|
|
|
|
#endif
|
2006-01-10 16:58:48 +03:00
|
|
|
|
2008-10-12 04:27:53 +04:00
|
|
|
#ifndef SQLITE_OMIT_DEPRECATED
|
2006-01-12 00:41:20 +03:00
|
|
|
/*
|
|
|
|
** This is a convenience routine that makes sure that all thread-specific
|
|
|
|
** data for this thread has been deallocated.
|
2007-08-16 17:01:44 +04:00
|
|
|
**
|
|
|
|
** SQLite no longer uses thread-specific data so this routine is now a
|
|
|
|
** no-op. It is retained for historical compatibility.
|
2006-01-12 00:41:20 +03:00
|
|
|
*/
|
|
|
|
void sqlite3_thread_cleanup(void){
|
|
|
|
}
|
2008-10-12 04:27:53 +04:00
|
|
|
#endif
|
2006-02-09 16:43:28 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Return meta information about a specific column of a database table.
|
|
|
|
** See comment in sqlite3.h (sqlite.h.in) for details.
|
|
|
|
*/
|
|
|
|
#ifdef SQLITE_ENABLE_COLUMN_METADATA
|
|
|
|
int sqlite3_table_column_metadata(
|
|
|
|
sqlite3 *db, /* Connection handle */
|
|
|
|
const char *zDbName, /* Database name or NULL */
|
|
|
|
const char *zTableName, /* Table name */
|
|
|
|
const char *zColumnName, /* Column name */
|
|
|
|
char const **pzDataType, /* OUTPUT: Declared data type */
|
|
|
|
char const **pzCollSeq, /* OUTPUT: Collation sequence name */
|
|
|
|
int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
|
|
|
|
int *pPrimaryKey, /* OUTPUT: True if column part of PK */
|
2008-06-17 00:51:15 +04:00
|
|
|
int *pAutoinc /* OUTPUT: True if column is auto-increment */
|
2006-02-09 16:43:28 +03:00
|
|
|
){
|
|
|
|
int rc;
|
|
|
|
char *zErrMsg = 0;
|
|
|
|
Table *pTab = 0;
|
|
|
|
Column *pCol = 0;
|
|
|
|
int iCol;
|
|
|
|
|
|
|
|
char const *zDataType = 0;
|
|
|
|
char const *zCollSeq = 0;
|
|
|
|
int notnull = 0;
|
|
|
|
int primarykey = 0;
|
|
|
|
int autoinc = 0;
|
|
|
|
|
|
|
|
/* Ensure the database schema has been loaded */
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2008-04-16 04:49:12 +04:00
|
|
|
(void)sqlite3SafetyOn(db);
|
2008-03-08 00:37:19 +03:00
|
|
|
sqlite3BtreeEnterAll(db);
|
2006-02-09 16:43:28 +03:00
|
|
|
rc = sqlite3Init(db, &zErrMsg);
|
|
|
|
if( SQLITE_OK!=rc ){
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate the table in question */
|
|
|
|
pTab = sqlite3FindTable(db, zTableName, zDbName);
|
|
|
|
if( !pTab || pTab->pSelect ){
|
|
|
|
pTab = 0;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the column for which info is requested */
|
|
|
|
if( sqlite3IsRowid(zColumnName) ){
|
|
|
|
iCol = pTab->iPKey;
|
|
|
|
if( iCol>=0 ){
|
|
|
|
pCol = &pTab->aCol[iCol];
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for(iCol=0; iCol<pTab->nCol; iCol++){
|
|
|
|
pCol = &pTab->aCol[iCol];
|
|
|
|
if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( iCol==pTab->nCol ){
|
|
|
|
pTab = 0;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The following block stores the meta information that will be returned
|
|
|
|
** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
|
|
|
|
** and autoinc. At this point there are two possibilities:
|
|
|
|
**
|
|
|
|
** 1. The specified column name was rowid", "oid" or "_rowid_"
|
|
|
|
** and there is no explicitly declared IPK column.
|
|
|
|
**
|
|
|
|
** 2. The table is not a view and the column name identified an
|
|
|
|
** explicitly declared column. Copy meta information from *pCol.
|
|
|
|
*/
|
|
|
|
if( pCol ){
|
|
|
|
zDataType = pCol->zType;
|
|
|
|
zCollSeq = pCol->zColl;
|
2008-06-17 00:51:15 +04:00
|
|
|
notnull = pCol->notNull!=0;
|
|
|
|
primarykey = pCol->isPrimKey!=0;
|
2008-08-20 20:35:10 +04:00
|
|
|
autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
|
2006-02-09 16:43:28 +03:00
|
|
|
}else{
|
|
|
|
zDataType = "INTEGER";
|
|
|
|
primarykey = 1;
|
|
|
|
}
|
|
|
|
if( !zCollSeq ){
|
|
|
|
zCollSeq = "BINARY";
|
|
|
|
}
|
|
|
|
|
|
|
|
error_out:
|
2009-02-26 10:15:59 +03:00
|
|
|
sqlite3BtreeLeaveAll(db);
|
2008-01-23 15:52:40 +03:00
|
|
|
(void)sqlite3SafetyOff(db);
|
2006-02-09 16:43:28 +03:00
|
|
|
|
|
|
|
/* Whether the function call succeeded or failed, set the output parameters
|
|
|
|
** to whatever their local counterparts contain. If an error did occur,
|
|
|
|
** this has the effect of zeroing all output parameters.
|
|
|
|
*/
|
|
|
|
if( pzDataType ) *pzDataType = zDataType;
|
|
|
|
if( pzCollSeq ) *pzCollSeq = zCollSeq;
|
|
|
|
if( pNotNull ) *pNotNull = notnull;
|
|
|
|
if( pPrimaryKey ) *pPrimaryKey = primarykey;
|
|
|
|
if( pAutoinc ) *pAutoinc = autoinc;
|
|
|
|
|
|
|
|
if( SQLITE_OK==rc && !pTab ){
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, zErrMsg);
|
2008-07-16 18:02:32 +04:00
|
|
|
zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
|
2008-07-08 23:34:06 +04:00
|
|
|
zColumnName);
|
2006-02-09 16:43:28 +03:00
|
|
|
rc = SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3DbFree(db, zErrMsg);
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = sqlite3ApiExit(db, rc);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2006-06-28 00:06:44 +04:00
|
|
|
return rc;
|
|
|
|
}
|
2007-08-21 20:15:55 +04:00
|
|
|
#endif
|
2006-06-28 00:06:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Sleep for a little while. Return the amount of time slept.
|
|
|
|
*/
|
|
|
|
int sqlite3_sleep(int ms){
|
2007-08-17 19:53:36 +04:00
|
|
|
sqlite3_vfs *pVfs;
|
2007-08-21 20:15:55 +04:00
|
|
|
int rc;
|
2007-08-21 02:48:41 +04:00
|
|
|
pVfs = sqlite3_vfs_find(0);
|
2008-06-13 22:24:27 +04:00
|
|
|
if( pVfs==0 ) return 0;
|
2007-08-18 14:59:19 +04:00
|
|
|
|
|
|
|
/* This function works in milliseconds, but the underlying OsSleep()
|
|
|
|
** API uses microseconds. Hence the 1000's.
|
|
|
|
*/
|
2007-08-21 20:15:55 +04:00
|
|
|
rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
|
|
|
|
return rc;
|
2006-06-28 00:06:44 +04:00
|
|
|
}
|
2006-09-15 11:28:50 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Enable or disable the extended result codes.
|
|
|
|
*/
|
|
|
|
int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_enter(db->mutex);
|
2006-09-15 11:28:50 +04:00
|
|
|
db->errMask = onoff ? 0xffffffff : 0xff;
|
2007-08-21 20:15:55 +04:00
|
|
|
sqlite3_mutex_leave(db->mutex);
|
2006-09-15 11:28:50 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
2007-08-31 20:11:35 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Invoke the xFileControl method on a particular database.
|
|
|
|
*/
|
|
|
|
int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
|
|
|
|
int rc = SQLITE_ERROR;
|
|
|
|
int iDb;
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
if( zDbName==0 ){
|
|
|
|
iDb = 0;
|
|
|
|
}else{
|
|
|
|
for(iDb=0; iDb<db->nDb; iDb++){
|
|
|
|
if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( iDb<db->nDb ){
|
|
|
|
Btree *pBtree = db->aDb[iDb].pBt;
|
|
|
|
if( pBtree ){
|
|
|
|
Pager *pPager;
|
2008-01-22 17:50:16 +03:00
|
|
|
sqlite3_file *fd;
|
2007-08-31 20:11:35 +04:00
|
|
|
sqlite3BtreeEnter(pBtree);
|
|
|
|
pPager = sqlite3BtreePager(pBtree);
|
2008-01-22 17:50:16 +03:00
|
|
|
assert( pPager!=0 );
|
|
|
|
fd = sqlite3PagerFile(pPager);
|
|
|
|
assert( fd!=0 );
|
|
|
|
if( fd->pMethods ){
|
|
|
|
rc = sqlite3OsFileControl(fd, op, pArg);
|
2007-08-31 20:11:35 +04:00
|
|
|
}
|
|
|
|
sqlite3BtreeLeave(pBtree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
return rc;
|
|
|
|
}
|
2008-01-31 17:43:24 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Interface to the testing logic.
|
|
|
|
*/
|
|
|
|
int sqlite3_test_control(int op, ...){
|
|
|
|
int rc = 0;
|
2008-03-21 19:45:47 +03:00
|
|
|
#ifndef SQLITE_OMIT_BUILTIN_TEST
|
2008-03-19 17:15:34 +03:00
|
|
|
va_list ap;
|
2008-01-31 17:43:24 +03:00
|
|
|
va_start(ap, op);
|
|
|
|
switch( op ){
|
2008-06-19 22:17:49 +04:00
|
|
|
|
2008-03-19 19:08:53 +03:00
|
|
|
/*
|
|
|
|
** Save the current state of the PRNG.
|
|
|
|
*/
|
2008-03-19 17:15:34 +03:00
|
|
|
case SQLITE_TESTCTRL_PRNG_SAVE: {
|
|
|
|
sqlite3PrngSaveState();
|
|
|
|
break;
|
|
|
|
}
|
2008-03-19 19:08:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Restore the state of the PRNG to the last state saved using
|
|
|
|
** PRNG_SAVE. If PRNG_SAVE has never before been called, then
|
|
|
|
** this verb acts like PRNG_RESET.
|
|
|
|
*/
|
2008-03-19 17:15:34 +03:00
|
|
|
case SQLITE_TESTCTRL_PRNG_RESTORE: {
|
|
|
|
sqlite3PrngRestoreState();
|
|
|
|
break;
|
|
|
|
}
|
2008-03-19 19:08:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Reset the PRNG back to its uninitialized state. The next call
|
|
|
|
** to sqlite3_randomness() will reseed the PRNG using a single call
|
|
|
|
** to the xRandomness method of the default VFS.
|
|
|
|
*/
|
2008-03-19 17:15:34 +03:00
|
|
|
case SQLITE_TESTCTRL_PRNG_RESET: {
|
|
|
|
sqlite3PrngResetState();
|
|
|
|
break;
|
|
|
|
}
|
2008-03-21 19:45:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** sqlite3_test_control(BITVEC_TEST, size, program)
|
|
|
|
**
|
|
|
|
** Run a test against a Bitvec object of size. The program argument
|
|
|
|
** is an array of integers that defines the test. Return -1 on a
|
|
|
|
** memory allocation error, 0 on success, or non-zero for an error.
|
|
|
|
** See the sqlite3BitvecBuiltinTest() for additional information.
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_BITVEC_TEST: {
|
|
|
|
int sz = va_arg(ap, int);
|
|
|
|
int *aProg = va_arg(ap, int*);
|
|
|
|
rc = sqlite3BitvecBuiltinTest(sz, aProg);
|
|
|
|
break;
|
|
|
|
}
|
2008-06-20 18:59:51 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
|
|
|
|
**
|
|
|
|
** Register hooks to call to indicate which malloc() failures
|
|
|
|
** are benign.
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
|
2008-06-23 18:15:52 +04:00
|
|
|
typedef void (*void_function)(void);
|
|
|
|
void_function xBenignBegin;
|
|
|
|
void_function xBenignEnd;
|
|
|
|
xBenignBegin = va_arg(ap, void_function);
|
|
|
|
xBenignEnd = va_arg(ap, void_function);
|
2008-06-20 18:59:51 +04:00
|
|
|
sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
|
|
|
|
break;
|
|
|
|
}
|
2009-02-05 19:31:45 +03:00
|
|
|
|
|
|
|
/*
|
2009-05-09 22:59:42 +04:00
|
|
|
** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
|
2009-02-05 19:31:45 +03:00
|
|
|
**
|
|
|
|
** Set the PENDING byte to the value in the argument, if X>0.
|
|
|
|
** Make no changes if X==0. Return the value of the pending byte
|
|
|
|
** as it existing before this routine was called.
|
|
|
|
**
|
|
|
|
** IMPORTANT: Changing the PENDING byte from 0x40000000 results in
|
|
|
|
** an incompatible database file format. Changing the PENDING byte
|
|
|
|
** while any database connection is open results in undefined and
|
|
|
|
** dileterious behavior.
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_PENDING_BYTE: {
|
|
|
|
unsigned int newVal = va_arg(ap, unsigned int);
|
|
|
|
rc = sqlite3PendingByte;
|
|
|
|
if( newVal ) sqlite3PendingByte = newVal;
|
|
|
|
break;
|
|
|
|
}
|
2009-05-09 22:59:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
|
|
|
|
**
|
|
|
|
** This action provides a run-time test to see whether or not
|
|
|
|
** assert() was enabled at compile-time. If X is true and assert()
|
|
|
|
** is enabled, then the return value is true. If X is true and
|
|
|
|
** assert() is disabled, then the return value is zero. If X is
|
|
|
|
** false and assert() is enabled, then the assertion fires and the
|
|
|
|
** process aborts. If X is false and assert() is disabled, then the
|
|
|
|
** return value is zero.
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_ASSERT: {
|
|
|
|
volatile int x = 0;
|
|
|
|
assert( (x = va_arg(ap,int))!=0 );
|
|
|
|
rc = x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
|
|
|
|
**
|
|
|
|
** This action provides a run-time test to see how the ALWAYS and
|
|
|
|
** NEVER macros were defined at compile-time.
|
|
|
|
**
|
|
|
|
** The return value is ALWAYS(X).
|
|
|
|
**
|
|
|
|
** The recommended test is X==2. If the return value is 2, that means
|
|
|
|
** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
|
|
|
|
** default setting. If the return value is 1, then ALWAYS() is either
|
|
|
|
** hard-coded to true or else it asserts if its argument is false.
|
|
|
|
** The first behavior (hard-coded to true) is the case if
|
|
|
|
** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
|
|
|
|
** behavior (assert if the argument to ALWAYS() is false) is the case if
|
|
|
|
** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
|
|
|
|
**
|
|
|
|
** The run-time test procedure might look something like this:
|
|
|
|
**
|
|
|
|
** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
|
|
|
|
** // ALWAYS() and NEVER() are no-op pass-through macros
|
|
|
|
** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
|
|
|
|
** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
|
|
|
|
** }else{
|
|
|
|
** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0.
|
|
|
|
** }
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_ALWAYS: {
|
|
|
|
int x = va_arg(ap,int);
|
|
|
|
rc = ALWAYS(x);
|
|
|
|
break;
|
|
|
|
}
|
2009-07-15 15:26:44 +04:00
|
|
|
|
|
|
|
/* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
|
|
|
|
**
|
|
|
|
** Set the nReserve size to N for the main database on the database
|
|
|
|
** connection db.
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_RESERVE: {
|
|
|
|
sqlite3 *db = va_arg(ap, sqlite3*);
|
|
|
|
int x = va_arg(ap,int);
|
|
|
|
sqlite3_mutex_enter(db->mutex);
|
|
|
|
sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
|
|
|
|
sqlite3_mutex_leave(db->mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-23 02:52:32 +03:00
|
|
|
/* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
|
|
|
|
**
|
|
|
|
** Enable or disable various optimizations for testing purposes. The
|
|
|
|
** argument N is a bitmask of optimizations to be disabled. For normal
|
|
|
|
** operation N should be 0. The idea is that a test program (like the
|
|
|
|
** SQL Logic Test or SLT test module) can run the same SQL multiple times
|
|
|
|
** with various optimizations disabled to verify that the same answer
|
|
|
|
** is obtained in every case.
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_OPTIMIZATIONS: {
|
|
|
|
sqlite3 *db = va_arg(ap, sqlite3*);
|
|
|
|
int x = va_arg(ap,int);
|
|
|
|
db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-02 06:21:35 +03:00
|
|
|
#ifdef SQLITE_N_KEYWORD
|
|
|
|
/* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
|
|
|
|
**
|
|
|
|
** If zWord is a keyword recognized by the parser, then return the
|
|
|
|
** number of keywords. Or if zWord is not a keyword, return 0.
|
|
|
|
**
|
|
|
|
** This test feature is only available in the amalgamation since
|
|
|
|
** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
|
|
|
|
** is built using separate source files.
|
|
|
|
*/
|
|
|
|
case SQLITE_TESTCTRL_ISKEYWORD: {
|
|
|
|
const char *zWord = va_arg(ap, const char*);
|
|
|
|
int n = sqlite3Strlen30(zWord);
|
|
|
|
rc = sqlite3KeywordCode((u8*)zWord, n) ? SQLITE_N_KEYWORD : 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 17:43:24 +03:00
|
|
|
}
|
|
|
|
va_end(ap);
|
2008-03-21 19:45:47 +03:00
|
|
|
#endif /* SQLITE_OMIT_BUILTIN_TEST */
|
2008-01-31 18:31:01 +03:00
|
|
|
return rc;
|
2008-01-31 17:43:24 +03:00
|
|
|
}
|