os/persistentdata/persistentstorage/sql/SQLite364/main.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/SQLite364/main.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,2091 @@
     1.4 +/*
     1.5 +** 2001 September 15
     1.6 +**
     1.7 +** The author disclaims copyright to this source code.  In place of
     1.8 +** a legal notice, here is a blessing:
     1.9 +**
    1.10 +**    May you do good and not evil.
    1.11 +**    May you find forgiveness for yourself and forgive others.
    1.12 +**    May you share freely, never taking more than you give.
    1.13 +**
    1.14 +*************************************************************************
    1.15 +** Main file for the SQLite library.  The routines in this file
    1.16 +** implement the programmer interface to the library.  Routines in
    1.17 +** other files are for internal use by SQLite and should not be
    1.18 +** accessed by users of the library.
    1.19 +**
    1.20 +** $Id: main.c,v 1.508 2008/10/12 00:27:53 shane Exp $
    1.21 +*/
    1.22 +#include "sqliteInt.h"
    1.23 +#include <ctype.h>
    1.24 +
    1.25 +#ifdef SQLITE_ENABLE_FTS3
    1.26 +# include "fts3.h"
    1.27 +#endif
    1.28 +#ifdef SQLITE_ENABLE_RTREE
    1.29 +# include "rtree.h"
    1.30 +#endif
    1.31 +#ifdef SQLITE_ENABLE_ICU
    1.32 +# include "sqliteicu.h"
    1.33 +#endif
    1.34 +
    1.35 +/*
    1.36 +** The version of the library
    1.37 +*/
    1.38 +const char sqlite3_version[] = SQLITE_VERSION;
    1.39 +const char *sqlite3_libversion(void){ return sqlite3_version; }
    1.40 +int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
    1.41 +int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
    1.42 +
    1.43 +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
    1.44 +/*
    1.45 +** If the following function pointer is not NULL and if
    1.46 +** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
    1.47 +** I/O active are written using this function.  These messages
    1.48 +** are intended for debugging activity only.
    1.49 +*/
    1.50 +void (*sqlite3IoTrace)(const char*, ...) = 0;
    1.51 +#endif
    1.52 +
    1.53 +/*
    1.54 +** If the following global variable points to a string which is the
    1.55 +** name of a directory, then that directory will be used to store
    1.56 +** temporary files.
    1.57 +**
    1.58 +** See also the "PRAGMA temp_store_directory" SQL command.
    1.59 +*/
    1.60 +char *sqlite3_temp_directory = 0;
    1.61 +
    1.62 +/*
    1.63 +** Initialize SQLite.  
    1.64 +**
    1.65 +** This routine must be called to initialize the memory allocation,
    1.66 +** VFS, and mutex subsystems prior to doing any serious work with
    1.67 +** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
    1.68 +** this routine will be called automatically by key routines such as
    1.69 +** sqlite3_open().  
    1.70 +**
    1.71 +** This routine is a no-op except on its very first call for the process,
    1.72 +** or for the first call after a call to sqlite3_shutdown.
    1.73 +**
    1.74 +** The first thread to call this routine runs the initialization to
    1.75 +** completion.  If subsequent threads call this routine before the first
    1.76 +** thread has finished the initialization process, then the subsequent
    1.77 +** threads must block until the first thread finishes with the initialization.
    1.78 +**
    1.79 +** The first thread might call this routine recursively.  Recursive
    1.80 +** calls to this routine should not block, of course.  Otherwise the
    1.81 +** initialization process would never complete.
    1.82 +**
    1.83 +** Let X be the first thread to enter this routine.  Let Y be some other
    1.84 +** thread.  Then while the initial invocation of this routine by X is
    1.85 +** incomplete, it is required that:
    1.86 +**
    1.87 +**    *  Calls to this routine from Y must block until the outer-most
    1.88 +**       call by X completes.
    1.89 +**
    1.90 +**    *  Recursive calls to this routine from thread X return immediately
    1.91 +**       without blocking.
    1.92 +*/
    1.93 +int sqlite3_initialize(void){
    1.94 +  sqlite3_mutex *pMaster;                      /* The main static mutex */
    1.95 +  int rc;                                      /* Result code */
    1.96 +
    1.97 +#ifdef SQLITE_OMIT_WSD
    1.98 +  rc = sqlite3_wsd_init(4096, 24);
    1.99 +  if( rc!=SQLITE_OK ){
   1.100 +    return rc;
   1.101 +  }
   1.102 +#endif
   1.103 +
   1.104 +  /* If SQLite is already completely initialized, then this call
   1.105 +  ** to sqlite3_initialize() should be a no-op.  But the initialization
   1.106 +  ** must be complete.  So isInit must not be set until the very end
   1.107 +  ** of this routine.
   1.108 +  */
   1.109 +  if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
   1.110 +
   1.111 +  /* Make sure the mutex subsystem is initialized.  If unable to 
   1.112 +  ** initialize the mutex subsystem, return early with the error.
   1.113 +  ** If the system is so sick that we are unable to allocate a mutex,
   1.114 +  ** there is not much SQLite is going to be able to do.
   1.115 +  **
   1.116 +  ** The mutex subsystem must take care of serializing its own
   1.117 +  ** initialization.
   1.118 +  */
   1.119 +  rc = sqlite3MutexInit();
   1.120 +  if( rc ) return rc;
   1.121 +
   1.122 +  /* Initialize the malloc() system and the recursive pInitMutex mutex.
   1.123 +  ** This operation is protected by the STATIC_MASTER mutex.  Note that
   1.124 +  ** MutexAlloc() is called for a static mutex prior to initializing the
   1.125 +  ** malloc subsystem - this implies that the allocation of a static
   1.126 +  ** mutex must not require support from the malloc subsystem.
   1.127 +  */
   1.128 +  pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   1.129 +  sqlite3_mutex_enter(pMaster);
   1.130 +  if( !sqlite3GlobalConfig.isMallocInit ){
   1.131 +    rc = sqlite3MallocInit();
   1.132 +  }
   1.133 +  if( rc==SQLITE_OK ){
   1.134 +    sqlite3GlobalConfig.isMallocInit = 1;
   1.135 +    if( !sqlite3GlobalConfig.pInitMutex ){
   1.136 +      sqlite3GlobalConfig.pInitMutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
   1.137 +      if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
   1.138 +        rc = SQLITE_NOMEM;
   1.139 +      }
   1.140 +    }
   1.141 +  }
   1.142 +  if( rc==SQLITE_OK ){
   1.143 +    sqlite3GlobalConfig.nRefInitMutex++;
   1.144 +  }
   1.145 +  sqlite3_mutex_leave(pMaster);
   1.146 +
   1.147 +  /* If unable to initialize the malloc subsystem, then return early.
   1.148 +  ** There is little hope of getting SQLite to run if the malloc
   1.149 +  ** subsystem cannot be initialized.
   1.150 +  */
   1.151 +  if( rc!=SQLITE_OK ){
   1.152 +    return rc;
   1.153 +  }
   1.154 +
   1.155 +  /* Do the rest of the initialization under the recursive mutex so
   1.156 +  ** that we will be able to handle recursive calls into
   1.157 +  ** sqlite3_initialize().  The recursive calls normally come through
   1.158 +  ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
   1.159 +  ** recursive calls might also be possible.
   1.160 +  */
   1.161 +  sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
   1.162 +  if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
   1.163 +    FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
   1.164 +    sqlite3GlobalConfig.inProgress = 1;
   1.165 +    memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
   1.166 +    sqlite3RegisterGlobalFunctions();
   1.167 +    rc = sqlite3_os_init();
   1.168 +    if( rc==SQLITE_OK ){
   1.169 +      rc = sqlite3PcacheInitialize();
   1.170 +      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
   1.171 +          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
   1.172 +    }
   1.173 +    sqlite3GlobalConfig.inProgress = 0;
   1.174 +    sqlite3GlobalConfig.isInit = (rc==SQLITE_OK ? 1 : 0);
   1.175 +  }
   1.176 +  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
   1.177 +
   1.178 +  /* Go back under the static mutex and clean up the recursive
   1.179 +  ** mutex to prevent a resource leak.
   1.180 +  */
   1.181 +  sqlite3_mutex_enter(pMaster);
   1.182 +  sqlite3GlobalConfig.nRefInitMutex--;
   1.183 +  if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
   1.184 +    assert( sqlite3GlobalConfig.nRefInitMutex==0 );
   1.185 +    sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
   1.186 +    sqlite3GlobalConfig.pInitMutex = 0;
   1.187 +  }
   1.188 +  sqlite3_mutex_leave(pMaster);
   1.189 +
   1.190 +  /* The following is just a sanity check to make sure SQLite has
   1.191 +  ** been compiled correctly.  It is important to run this code, but
   1.192 +  ** we don't want to run it too often and soak up CPU cycles for no
   1.193 +  ** reason.  So we run it once during initialization.
   1.194 +  */
   1.195 +#ifndef NDEBUG
   1.196 +  /* This section of code's only "output" is via assert() statements. */
   1.197 +  if ( rc==SQLITE_OK ){
   1.198 +    u64 x = (((u64)1)<<63)-1;
   1.199 +    double y;
   1.200 +    assert(sizeof(x)==8);
   1.201 +    assert(sizeof(x)==sizeof(y));
   1.202 +    memcpy(&y, &x, 8);
   1.203 +    assert( sqlite3IsNaN(y) );
   1.204 +  }
   1.205 +#endif
   1.206 +
   1.207 +  return rc;
   1.208 +}
   1.209 +
   1.210 +/*
   1.211 +** Undo the effects of sqlite3_initialize().  Must not be called while
   1.212 +** there are outstanding database connections or memory allocations or
   1.213 +** while any part of SQLite is otherwise in use in any thread.  This
   1.214 +** routine is not threadsafe.  Not by a long shot.
   1.215 +*/
   1.216 +int sqlite3_shutdown(void){
   1.217 +  sqlite3GlobalConfig.isMallocInit = 0;
   1.218 +  sqlite3PcacheShutdown();
   1.219 +  if( sqlite3GlobalConfig.isInit ){
   1.220 +    sqlite3_os_end();
   1.221 +  }
   1.222 +  sqlite3MallocEnd();
   1.223 +  sqlite3MutexEnd();
   1.224 +  sqlite3GlobalConfig.isInit = 0;
   1.225 +  return SQLITE_OK;
   1.226 +}
   1.227 +
   1.228 +/*
   1.229 +** This API allows applications to modify the global configuration of
   1.230 +** the SQLite library at run-time.
   1.231 +**
   1.232 +** This routine should only be called when there are no outstanding
   1.233 +** database connections or memory allocations.  This routine is not
   1.234 +** threadsafe.  Failure to heed these warnings can lead to unpredictable
   1.235 +** behavior.
   1.236 +*/
   1.237 +int sqlite3_config(int op, ...){
   1.238 +  va_list ap;
   1.239 +  int rc = SQLITE_OK;
   1.240 +
   1.241 +  /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
   1.242 +  ** the SQLite library is in use. */
   1.243 +  if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE;
   1.244 +
   1.245 +  va_start(ap, op);
   1.246 +  switch( op ){
   1.247 +
   1.248 +    /* Mutex configuration options are only available in a threadsafe
   1.249 +    ** compile. 
   1.250 +    */
   1.251 +#if SQLITE_THREADSAFE
   1.252 +    case SQLITE_CONFIG_SINGLETHREAD: {
   1.253 +      /* Disable all mutexing */
   1.254 +      sqlite3GlobalConfig.bCoreMutex = 0;
   1.255 +      sqlite3GlobalConfig.bFullMutex = 0;
   1.256 +      break;
   1.257 +    }
   1.258 +    case SQLITE_CONFIG_MULTITHREAD: {
   1.259 +      /* Disable mutexing of database connections */
   1.260 +      /* Enable mutexing of core data structures */
   1.261 +      sqlite3GlobalConfig.bCoreMutex = 1;
   1.262 +      sqlite3GlobalConfig.bFullMutex = 0;
   1.263 +      break;
   1.264 +    }
   1.265 +    case SQLITE_CONFIG_SERIALIZED: {
   1.266 +      /* Enable all mutexing */
   1.267 +      sqlite3GlobalConfig.bCoreMutex = 1;
   1.268 +      sqlite3GlobalConfig.bFullMutex = 1;
   1.269 +      break;
   1.270 +    }
   1.271 +    case SQLITE_CONFIG_MUTEX: {
   1.272 +      /* Specify an alternative mutex implementation */
   1.273 +      sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
   1.274 +      break;
   1.275 +    }
   1.276 +    case SQLITE_CONFIG_GETMUTEX: {
   1.277 +      /* Retrieve the current mutex implementation */
   1.278 +      *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
   1.279 +      break;
   1.280 +    }
   1.281 +#endif
   1.282 +
   1.283 +
   1.284 +    case SQLITE_CONFIG_MALLOC: {
   1.285 +      /* Specify an alternative malloc implementation */
   1.286 +      sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
   1.287 +      break;
   1.288 +    }
   1.289 +    case SQLITE_CONFIG_GETMALLOC: {
   1.290 +      /* Retrieve the current malloc() implementation */
   1.291 +      if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
   1.292 +      *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
   1.293 +      break;
   1.294 +    }
   1.295 +    case SQLITE_CONFIG_MEMSTATUS: {
   1.296 +      /* Enable or disable the malloc status collection */
   1.297 +      sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
   1.298 +      break;
   1.299 +    }
   1.300 +    case SQLITE_CONFIG_SCRATCH: {
   1.301 +      /* Designate a buffer for scratch memory space */
   1.302 +      sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
   1.303 +      sqlite3GlobalConfig.szScratch = va_arg(ap, int);
   1.304 +      sqlite3GlobalConfig.nScratch = va_arg(ap, int);
   1.305 +      break;
   1.306 +    }
   1.307 +    case SQLITE_CONFIG_PAGECACHE: {
   1.308 +      /* Designate a buffer for scratch memory space */
   1.309 +      sqlite3GlobalConfig.pPage = va_arg(ap, void*);
   1.310 +      sqlite3GlobalConfig.szPage = va_arg(ap, int);
   1.311 +      sqlite3GlobalConfig.nPage = va_arg(ap, int);
   1.312 +      break;
   1.313 +    }
   1.314 +
   1.315 +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
   1.316 +    case SQLITE_CONFIG_HEAP: {
   1.317 +      /* Designate a buffer for heap memory space */
   1.318 +      sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
   1.319 +      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
   1.320 +      sqlite3GlobalConfig.mnReq = va_arg(ap, int);
   1.321 +
   1.322 +      if( sqlite3GlobalConfig.pHeap==0 ){
   1.323 +        /* If the heap pointer is NULL, then restore the malloc implementation
   1.324 +        ** back to NULL pointers too.  This will cause the malloc to go
   1.325 +        ** back to its default implementation when sqlite3_initialize() is
   1.326 +        ** run.
   1.327 +        */
   1.328 +        memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
   1.329 +      }else{
   1.330 +        /* The heap pointer is not NULL, then install one of the
   1.331 +        ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor
   1.332 +        ** ENABLE_MEMSYS5 is defined, return an error.
   1.333 +        ** the default case and return an error.
   1.334 +        */
   1.335 +#ifdef SQLITE_ENABLE_MEMSYS3
   1.336 +        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
   1.337 +#endif
   1.338 +#ifdef SQLITE_ENABLE_MEMSYS5
   1.339 +        sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
   1.340 +#endif
   1.341 +      }
   1.342 +      break;
   1.343 +    }
   1.344 +#endif
   1.345 +
   1.346 +#if defined(SQLITE_ENABLE_MEMSYS6)
   1.347 +    case SQLITE_CONFIG_CHUNKALLOC: {
   1.348 +      sqlite3GlobalConfig.nSmall = va_arg(ap, int);
   1.349 +      sqlite3GlobalConfig.m = *sqlite3MemGetMemsys6();
   1.350 +      break;
   1.351 +    }
   1.352 +#endif
   1.353 +
   1.354 +    case SQLITE_CONFIG_LOOKASIDE: {
   1.355 +      sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
   1.356 +      sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
   1.357 +      break;
   1.358 +    }
   1.359 +
   1.360 +    default: {
   1.361 +      rc = SQLITE_ERROR;
   1.362 +      break;
   1.363 +    }
   1.364 +  }
   1.365 +  va_end(ap);
   1.366 +  return rc;
   1.367 +}
   1.368 +
   1.369 +/*
   1.370 +** Set up the lookaside buffers for a database connection.
   1.371 +** Return SQLITE_OK on success.  
   1.372 +** If lookaside is already active, return SQLITE_BUSY.
   1.373 +**
   1.374 +** The sz parameter is the number of bytes in each lookaside slot.
   1.375 +** The cnt parameter is the number of slots.  If pStart is NULL the
   1.376 +** space for the lookaside memory is obtained from sqlite3_malloc().
   1.377 +** If pStart is not NULL then it is sz*cnt bytes of memory to use for
   1.378 +** the lookaside memory.
   1.379 +*/
   1.380 +static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
   1.381 +  void *pStart;
   1.382 +  if( db->lookaside.nOut ){
   1.383 +    return SQLITE_BUSY;
   1.384 +  }
   1.385 +  if( sz<0 ) sz = 0;
   1.386 +  if( cnt<0 ) cnt = 0;
   1.387 +  if( pBuf==0 ){
   1.388 +    sz = (sz + 7)&~7;
   1.389 +    sqlite3BeginBenignMalloc();
   1.390 +    pStart = sqlite3Malloc( sz*cnt );
   1.391 +    sqlite3EndBenignMalloc();
   1.392 +  }else{
   1.393 +    sz = sz&~7;
   1.394 +    pStart = pBuf;
   1.395 +  }
   1.396 +  if( db->lookaside.bMalloced ){
   1.397 +    sqlite3_free(db->lookaside.pStart);
   1.398 +  }
   1.399 +  db->lookaside.pStart = pStart;
   1.400 +  db->lookaside.pFree = 0;
   1.401 +  db->lookaside.sz = sz;
   1.402 +  db->lookaside.bMalloced = pBuf==0;
   1.403 +  if( pStart ){
   1.404 +    int i;
   1.405 +    LookasideSlot *p;
   1.406 +    p = (LookasideSlot*)pStart;
   1.407 +    for(i=cnt-1; i>=0; i--){
   1.408 +      p->pNext = db->lookaside.pFree;
   1.409 +      db->lookaside.pFree = p;
   1.410 +      p = (LookasideSlot*)&((u8*)p)[sz];
   1.411 +    }
   1.412 +    db->lookaside.pEnd = p;
   1.413 +    db->lookaside.bEnabled = 1;
   1.414 +  }else{
   1.415 +    db->lookaside.pEnd = 0;
   1.416 +    db->lookaside.bEnabled = 0;
   1.417 +  }
   1.418 +  return SQLITE_OK;
   1.419 +}
   1.420 +
   1.421 +/*
   1.422 +** Configuration settings for an individual database connection
   1.423 +*/
   1.424 +int sqlite3_db_config(sqlite3 *db, int op, ...){
   1.425 +  va_list ap;
   1.426 +  int rc;
   1.427 +  va_start(ap, op);
   1.428 +  switch( op ){
   1.429 +    case SQLITE_DBCONFIG_LOOKASIDE: {
   1.430 +      void *pBuf = va_arg(ap, void*);
   1.431 +      int sz = va_arg(ap, int);
   1.432 +      int cnt = va_arg(ap, int);
   1.433 +      rc = setupLookaside(db, pBuf, sz, cnt);
   1.434 +      break;
   1.435 +    }
   1.436 +    default: {
   1.437 +      rc = SQLITE_ERROR;
   1.438 +      break;
   1.439 +    }
   1.440 +  }
   1.441 +  va_end(ap);
   1.442 +  return rc;
   1.443 +}
   1.444 +
   1.445 +/*
   1.446 +** Routine needed to support the testcase() macro.
   1.447 +*/
   1.448 +#ifdef SQLITE_COVERAGE_TEST
   1.449 +void sqlite3Coverage(int x){
   1.450 +  static int dummy = 0;
   1.451 +  dummy += x;
   1.452 +}
   1.453 +#endif
   1.454 +
   1.455 +
   1.456 +/*
   1.457 +** Return true if the buffer z[0..n-1] contains all spaces.
   1.458 +*/
   1.459 +static int allSpaces(const char *z, int n){
   1.460 +  while( n>0 && z[n-1]==' ' ){ n--; }
   1.461 +  return n==0;
   1.462 +}
   1.463 +
   1.464 +/*
   1.465 +** This is the default collating function named "BINARY" which is always
   1.466 +** available.
   1.467 +**
   1.468 +** If the padFlag argument is not NULL then space padding at the end
   1.469 +** of strings is ignored.  This implements the RTRIM collation.
   1.470 +*/
   1.471 +static int binCollFunc(
   1.472 +  void *padFlag,
   1.473 +  int nKey1, const void *pKey1,
   1.474 +  int nKey2, const void *pKey2
   1.475 +){
   1.476 +  int rc, n;
   1.477 +  n = nKey1<nKey2 ? nKey1 : nKey2;
   1.478 +  rc = memcmp(pKey1, pKey2, n);
   1.479 +  if( rc==0 ){
   1.480 +    if( padFlag
   1.481 +     && allSpaces(((char*)pKey1)+n, nKey1-n)
   1.482 +     && allSpaces(((char*)pKey2)+n, nKey2-n)
   1.483 +    ){
   1.484 +      /* Leave rc unchanged at 0 */
   1.485 +    }else{
   1.486 +      rc = nKey1 - nKey2;
   1.487 +    }
   1.488 +  }
   1.489 +  return rc;
   1.490 +}
   1.491 +
   1.492 +/*
   1.493 +** Another built-in collating sequence: NOCASE. 
   1.494 +**
   1.495 +** This collating sequence is intended to be used for "case independant
   1.496 +** comparison". SQLite's knowledge of upper and lower case equivalents
   1.497 +** extends only to the 26 characters used in the English language.
   1.498 +**
   1.499 +** At the moment there is only a UTF-8 implementation.
   1.500 +*/
   1.501 +static int nocaseCollatingFunc(
   1.502 +  void *NotUsed,
   1.503 +  int nKey1, const void *pKey1,
   1.504 +  int nKey2, const void *pKey2
   1.505 +){
   1.506 +  int r = sqlite3StrNICmp(
   1.507 +      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
   1.508 +  if( 0==r ){
   1.509 +    r = nKey1-nKey2;
   1.510 +  }
   1.511 +  return r;
   1.512 +}
   1.513 +
   1.514 +/*
   1.515 +** Return the ROWID of the most recent insert
   1.516 +*/
   1.517 +sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
   1.518 +  return db->lastRowid;
   1.519 +}
   1.520 +
   1.521 +/*
   1.522 +** Return the number of changes in the most recent call to sqlite3_exec().
   1.523 +*/
   1.524 +int sqlite3_changes(sqlite3 *db){
   1.525 +  return db->nChange;
   1.526 +}
   1.527 +
   1.528 +/*
   1.529 +** Return the number of changes since the database handle was opened.
   1.530 +*/
   1.531 +int sqlite3_total_changes(sqlite3 *db){
   1.532 +  return db->nTotalChange;
   1.533 +}
   1.534 +
   1.535 +/*
   1.536 +** Close an existing SQLite database
   1.537 +*/
   1.538 +int sqlite3_close(sqlite3 *db){
   1.539 +  HashElem *i;
   1.540 +  int j;
   1.541 +
   1.542 +  if( !db ){
   1.543 +    return SQLITE_OK;
   1.544 +  }
   1.545 +  if( !sqlite3SafetyCheckSickOrOk(db) ){
   1.546 +    return SQLITE_MISUSE;
   1.547 +  }
   1.548 +  sqlite3_mutex_enter(db->mutex);
   1.549 +
   1.550 +#ifdef SQLITE_SSE
   1.551 +  {
   1.552 +    extern void sqlite3SseCleanup(sqlite3*);
   1.553 +    sqlite3SseCleanup(db);
   1.554 +  }
   1.555 +#endif 
   1.556 +
   1.557 +  sqlite3ResetInternalSchema(db, 0);
   1.558 +
   1.559 +  /* If a transaction is open, the ResetInternalSchema() call above
   1.560 +  ** will not have called the xDisconnect() method on any virtual
   1.561 +  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
   1.562 +  ** call will do so. We need to do this before the check for active
   1.563 +  ** SQL statements below, as the v-table implementation may be storing
   1.564 +  ** some prepared statements internally.
   1.565 +  */
   1.566 +  sqlite3VtabRollback(db);
   1.567 +
   1.568 +  /* If there are any outstanding VMs, return SQLITE_BUSY. */
   1.569 +  if( db->pVdbe ){
   1.570 +    sqlite3Error(db, SQLITE_BUSY, 
   1.571 +        "Unable to close due to unfinalised statements");
   1.572 +    sqlite3_mutex_leave(db->mutex);
   1.573 +    return SQLITE_BUSY;
   1.574 +  }
   1.575 +  assert( sqlite3SafetyCheckSickOrOk(db) );
   1.576 +
   1.577 +  for(j=0; j<db->nDb; j++){
   1.578 +    struct Db *pDb = &db->aDb[j];
   1.579 +    if( pDb->pBt ){
   1.580 +      sqlite3BtreeClose(pDb->pBt);
   1.581 +      pDb->pBt = 0;
   1.582 +      if( j!=1 ){
   1.583 +        pDb->pSchema = 0;
   1.584 +      }
   1.585 +    }
   1.586 +  }
   1.587 +  sqlite3ResetInternalSchema(db, 0);
   1.588 +  assert( db->nDb<=2 );
   1.589 +  assert( db->aDb==db->aDbStatic );
   1.590 +  for(j=0; j<ArraySize(db->aFunc.a); j++){
   1.591 +    FuncDef *pNext, *pHash, *p;
   1.592 +    for(p=db->aFunc.a[j]; p; p=pHash){
   1.593 +      pHash = p->pHash;
   1.594 +      while( p ){
   1.595 +        pNext = p->pNext;
   1.596 +        sqlite3DbFree(db, p);
   1.597 +        p = pNext;
   1.598 +      }
   1.599 +    }
   1.600 +  }
   1.601 +  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
   1.602 +    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
   1.603 +    /* Invoke any destructors registered for collation sequence user data. */
   1.604 +    for(j=0; j<3; j++){
   1.605 +      if( pColl[j].xDel ){
   1.606 +        pColl[j].xDel(pColl[j].pUser);
   1.607 +      }
   1.608 +    }
   1.609 +    sqlite3DbFree(db, pColl);
   1.610 +  }
   1.611 +  sqlite3HashClear(&db->aCollSeq);
   1.612 +#ifndef SQLITE_OMIT_VIRTUALTABLE
   1.613 +  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
   1.614 +    Module *pMod = (Module *)sqliteHashData(i);
   1.615 +    if( pMod->xDestroy ){
   1.616 +      pMod->xDestroy(pMod->pAux);
   1.617 +    }
   1.618 +    sqlite3DbFree(db, pMod);
   1.619 +  }
   1.620 +  sqlite3HashClear(&db->aModule);
   1.621 +#endif
   1.622 +
   1.623 +  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
   1.624 +  if( db->pErr ){
   1.625 +    sqlite3ValueFree(db->pErr);
   1.626 +  }
   1.627 +  sqlite3CloseExtensions(db);
   1.628 +
   1.629 +  db->magic = SQLITE_MAGIC_ERROR;
   1.630 +
   1.631 +  /* The temp-database schema is allocated differently from the other schema
   1.632 +  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
   1.633 +  ** So it needs to be freed here. Todo: Why not roll the temp schema into
   1.634 +  ** the same sqliteMalloc() as the one that allocates the database 
   1.635 +  ** structure?
   1.636 +  */
   1.637 +  sqlite3DbFree(db, db->aDb[1].pSchema);
   1.638 +  sqlite3_mutex_leave(db->mutex);
   1.639 +  db->magic = SQLITE_MAGIC_CLOSED;
   1.640 +  sqlite3_mutex_free(db->mutex);
   1.641 +  assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
   1.642 +  if( db->lookaside.bMalloced ){
   1.643 +    sqlite3_free(db->lookaside.pStart);
   1.644 +  }
   1.645 +  sqlite3_free(db);
   1.646 +  return SQLITE_OK;
   1.647 +}
   1.648 +
   1.649 +/*
   1.650 +** Rollback all database files.
   1.651 +*/
   1.652 +void sqlite3RollbackAll(sqlite3 *db){
   1.653 +  int i;
   1.654 +  int inTrans = 0;
   1.655 +  assert( sqlite3_mutex_held(db->mutex) );
   1.656 +  sqlite3BeginBenignMalloc();
   1.657 +  for(i=0; i<db->nDb; i++){
   1.658 +    if( db->aDb[i].pBt ){
   1.659 +      if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
   1.660 +        inTrans = 1;
   1.661 +      }
   1.662 +      sqlite3BtreeRollback(db->aDb[i].pBt);
   1.663 +      db->aDb[i].inTrans = 0;
   1.664 +    }
   1.665 +  }
   1.666 +  sqlite3VtabRollback(db);
   1.667 +  sqlite3EndBenignMalloc();
   1.668 +
   1.669 +  if( db->flags&SQLITE_InternChanges ){
   1.670 +    sqlite3ExpirePreparedStatements(db);
   1.671 +    sqlite3ResetInternalSchema(db, 0);
   1.672 +  }
   1.673 +
   1.674 +  /* If one has been configured, invoke the rollback-hook callback */
   1.675 +  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
   1.676 +    db->xRollbackCallback(db->pRollbackArg);
   1.677 +  }
   1.678 +}
   1.679 +
   1.680 +/*
   1.681 +** Return a static string that describes the kind of error specified in the
   1.682 +** argument.
   1.683 +*/
   1.684 +const char *sqlite3ErrStr(int rc){
   1.685 +  const char *z;
   1.686 +  switch( rc & 0xff ){
   1.687 +    case SQLITE_ROW:
   1.688 +    case SQLITE_DONE:
   1.689 +    case SQLITE_OK:         z = "not an error";                          break;
   1.690 +    case SQLITE_ERROR:      z = "SQL logic error or missing database";   break;
   1.691 +    case SQLITE_PERM:       z = "access permission denied";              break;
   1.692 +    case SQLITE_ABORT:      z = "callback requested query abort";        break;
   1.693 +    case SQLITE_BUSY:       z = "database is locked";                    break;
   1.694 +    case SQLITE_LOCKED:     z = "database table is locked";              break;
   1.695 +    case SQLITE_NOMEM:      z = "out of memory";                         break;
   1.696 +    case SQLITE_READONLY:   z = "attempt to write a readonly database";  break;
   1.697 +    case SQLITE_INTERRUPT:  z = "interrupted";                           break;
   1.698 +    case SQLITE_IOERR:      z = "disk I/O error";                        break;
   1.699 +    case SQLITE_CORRUPT:    z = "database disk image is malformed";      break;
   1.700 +    case SQLITE_FULL:       z = "database or disk is full";              break;
   1.701 +    case SQLITE_CANTOPEN:   z = "unable to open database file";          break;
   1.702 +    case SQLITE_EMPTY:      z = "table contains no data";                break;
   1.703 +    case SQLITE_SCHEMA:     z = "database schema has changed";           break;
   1.704 +    case SQLITE_TOOBIG:     z = "String or BLOB exceeded size limit";    break;
   1.705 +    case SQLITE_CONSTRAINT: z = "constraint failed";                     break;
   1.706 +    case SQLITE_MISMATCH:   z = "datatype mismatch";                     break;
   1.707 +    case SQLITE_MISUSE:     z = "library routine called out of sequence";break;
   1.708 +    case SQLITE_NOLFS:      z = "large file support is disabled";        break;
   1.709 +    case SQLITE_AUTH:       z = "authorization denied";                  break;
   1.710 +    case SQLITE_FORMAT:     z = "auxiliary database format error";       break;
   1.711 +    case SQLITE_RANGE:      z = "bind or column index out of range";     break;
   1.712 +    case SQLITE_NOTADB:     z = "file is encrypted or is not a database";break;
   1.713 +    default:                z = "unknown error";                         break;
   1.714 +  }
   1.715 +  return z;
   1.716 +}
   1.717 +
   1.718 +/*
   1.719 +** This routine implements a busy callback that sleeps and tries
   1.720 +** again until a timeout value is reached.  The timeout value is
   1.721 +** an integer number of milliseconds passed in as the first
   1.722 +** argument.
   1.723 +*/
   1.724 +static int sqliteDefaultBusyCallback(
   1.725 + void *ptr,               /* Database connection */
   1.726 + int count                /* Number of times table has been busy */
   1.727 +){
   1.728 +#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
   1.729 +  static const u8 delays[] =
   1.730 +     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
   1.731 +  static const u8 totals[] =
   1.732 +     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
   1.733 +# define NDELAY (sizeof(delays)/sizeof(delays[0]))
   1.734 +  sqlite3 *db = (sqlite3 *)ptr;
   1.735 +  int timeout = db->busyTimeout;
   1.736 +  int delay, prior;
   1.737 +
   1.738 +  assert( count>=0 );
   1.739 +  if( count < NDELAY ){
   1.740 +    delay = delays[count];
   1.741 +    prior = totals[count];
   1.742 +  }else{
   1.743 +    delay = delays[NDELAY-1];
   1.744 +    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
   1.745 +  }
   1.746 +  if( prior + delay > timeout ){
   1.747 +    delay = timeout - prior;
   1.748 +    if( delay<=0 ) return 0;
   1.749 +  }
   1.750 +  sqlite3OsSleep(db->pVfs, delay*1000);
   1.751 +  return 1;
   1.752 +#else
   1.753 +  sqlite3 *db = (sqlite3 *)ptr;
   1.754 +  int timeout = ((sqlite3 *)ptr)->busyTimeout;
   1.755 +  if( (count+1)*1000 > timeout ){
   1.756 +    return 0;
   1.757 +  }
   1.758 +  sqlite3OsSleep(db->pVfs, 1000000);
   1.759 +  return 1;
   1.760 +#endif
   1.761 +}
   1.762 +
   1.763 +/*
   1.764 +** Invoke the given busy handler.
   1.765 +**
   1.766 +** This routine is called when an operation failed with a lock.
   1.767 +** If this routine returns non-zero, the lock is retried.  If it
   1.768 +** returns 0, the operation aborts with an SQLITE_BUSY error.
   1.769 +*/
   1.770 +int sqlite3InvokeBusyHandler(BusyHandler *p){
   1.771 +  int rc;
   1.772 +  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
   1.773 +  rc = p->xFunc(p->pArg, p->nBusy);
   1.774 +  if( rc==0 ){
   1.775 +    p->nBusy = -1;
   1.776 +  }else{
   1.777 +    p->nBusy++;
   1.778 +  }
   1.779 +  return rc; 
   1.780 +}
   1.781 +
   1.782 +/*
   1.783 +** This routine sets the busy callback for an Sqlite database to the
   1.784 +** given callback function with the given argument.
   1.785 +*/
   1.786 +int sqlite3_busy_handler(
   1.787 +  sqlite3 *db,
   1.788 +  int (*xBusy)(void*,int),
   1.789 +  void *pArg
   1.790 +){
   1.791 +  sqlite3_mutex_enter(db->mutex);
   1.792 +  db->busyHandler.xFunc = xBusy;
   1.793 +  db->busyHandler.pArg = pArg;
   1.794 +  db->busyHandler.nBusy = 0;
   1.795 +  sqlite3_mutex_leave(db->mutex);
   1.796 +  return SQLITE_OK;
   1.797 +}
   1.798 +
   1.799 +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   1.800 +/*
   1.801 +** This routine sets the progress callback for an Sqlite database to the
   1.802 +** given callback function with the given argument. The progress callback will
   1.803 +** be invoked every nOps opcodes.
   1.804 +*/
   1.805 +void sqlite3_progress_handler(
   1.806 +  sqlite3 *db, 
   1.807 +  int nOps,
   1.808 +  int (*xProgress)(void*), 
   1.809 +  void *pArg
   1.810 +){
   1.811 +  sqlite3_mutex_enter(db->mutex);
   1.812 +  if( nOps>0 ){
   1.813 +    db->xProgress = xProgress;
   1.814 +    db->nProgressOps = nOps;
   1.815 +    db->pProgressArg = pArg;
   1.816 +  }else{
   1.817 +    db->xProgress = 0;
   1.818 +    db->nProgressOps = 0;
   1.819 +    db->pProgressArg = 0;
   1.820 +  }
   1.821 +  sqlite3_mutex_leave(db->mutex);
   1.822 +}
   1.823 +#endif
   1.824 +
   1.825 +
   1.826 +/*
   1.827 +** This routine installs a default busy handler that waits for the
   1.828 +** specified number of milliseconds before returning 0.
   1.829 +*/
   1.830 +int sqlite3_busy_timeout(sqlite3 *db, int ms){
   1.831 +  if( ms>0 ){
   1.832 +    db->busyTimeout = ms;
   1.833 +    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
   1.834 +  }else{
   1.835 +    sqlite3_busy_handler(db, 0, 0);
   1.836 +  }
   1.837 +  return SQLITE_OK;
   1.838 +}
   1.839 +
   1.840 +/*
   1.841 +** Cause any pending operation to stop at its earliest opportunity.
   1.842 +*/
   1.843 +void sqlite3_interrupt(sqlite3 *db){
   1.844 +  db->u1.isInterrupted = 1;
   1.845 +}
   1.846 +
   1.847 +
   1.848 +/*
   1.849 +** This function is exactly the same as sqlite3_create_function(), except
   1.850 +** that it is designed to be called by internal code. The difference is
   1.851 +** that if a malloc() fails in sqlite3_create_function(), an error code
   1.852 +** is returned and the mallocFailed flag cleared. 
   1.853 +*/
   1.854 +int sqlite3CreateFunc(
   1.855 +  sqlite3 *db,
   1.856 +  const char *zFunctionName,
   1.857 +  int nArg,
   1.858 +  int enc,
   1.859 +  void *pUserData,
   1.860 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
   1.861 +  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
   1.862 +  void (*xFinal)(sqlite3_context*)
   1.863 +){
   1.864 +  FuncDef *p;
   1.865 +  int nName;
   1.866 +
   1.867 +  assert( sqlite3_mutex_held(db->mutex) );
   1.868 +  if( zFunctionName==0 ||
   1.869 +      (xFunc && (xFinal || xStep)) || 
   1.870 +      (!xFunc && (xFinal && !xStep)) ||
   1.871 +      (!xFunc && (!xFinal && xStep)) ||
   1.872 +      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
   1.873 +      (255<(nName = sqlite3Strlen(db, zFunctionName))) ){
   1.874 +    sqlite3Error(db, SQLITE_ERROR, "bad parameters");
   1.875 +    return SQLITE_ERROR;
   1.876 +  }
   1.877 +  
   1.878 +#ifndef SQLITE_OMIT_UTF16
   1.879 +  /* If SQLITE_UTF16 is specified as the encoding type, transform this
   1.880 +  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   1.881 +  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
   1.882 +  **
   1.883 +  ** If SQLITE_ANY is specified, add three versions of the function
   1.884 +  ** to the hash table.
   1.885 +  */
   1.886 +  if( enc==SQLITE_UTF16 ){
   1.887 +    enc = SQLITE_UTF16NATIVE;
   1.888 +  }else if( enc==SQLITE_ANY ){
   1.889 +    int rc;
   1.890 +    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
   1.891 +         pUserData, xFunc, xStep, xFinal);
   1.892 +    if( rc==SQLITE_OK ){
   1.893 +      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
   1.894 +          pUserData, xFunc, xStep, xFinal);
   1.895 +    }
   1.896 +    if( rc!=SQLITE_OK ){
   1.897 +      return rc;
   1.898 +    }
   1.899 +    enc = SQLITE_UTF16BE;
   1.900 +  }
   1.901 +#else
   1.902 +  enc = SQLITE_UTF8;
   1.903 +#endif
   1.904 +  
   1.905 +  /* Check if an existing function is being overridden or deleted. If so,
   1.906 +  ** and there are active VMs, then return SQLITE_BUSY. If a function
   1.907 +  ** is being overridden/deleted but there are no active VMs, allow the
   1.908 +  ** operation to continue but invalidate all precompiled statements.
   1.909 +  */
   1.910 +  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
   1.911 +  if( p && p->iPrefEnc==enc && p->nArg==nArg ){
   1.912 +    if( db->activeVdbeCnt ){
   1.913 +      sqlite3Error(db, SQLITE_BUSY, 
   1.914 +        "Unable to delete/modify user-function due to active statements");
   1.915 +      assert( !db->mallocFailed );
   1.916 +      return SQLITE_BUSY;
   1.917 +    }else{
   1.918 +      sqlite3ExpirePreparedStatements(db);
   1.919 +    }
   1.920 +  }
   1.921 +
   1.922 +  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
   1.923 +  assert(p || db->mallocFailed);
   1.924 +  if( !p ){
   1.925 +    return SQLITE_NOMEM;
   1.926 +  }
   1.927 +  p->flags = 0;
   1.928 +  p->xFunc = xFunc;
   1.929 +  p->xStep = xStep;
   1.930 +  p->xFinalize = xFinal;
   1.931 +  p->pUserData = pUserData;
   1.932 +  p->nArg = nArg;
   1.933 +  return SQLITE_OK;
   1.934 +}
   1.935 +
   1.936 +/*
   1.937 +** Create new user functions.
   1.938 +*/
   1.939 +int sqlite3_create_function(
   1.940 +  sqlite3 *db,
   1.941 +  const char *zFunctionName,
   1.942 +  int nArg,
   1.943 +  int enc,
   1.944 +  void *p,
   1.945 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
   1.946 +  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
   1.947 +  void (*xFinal)(sqlite3_context*)
   1.948 +){
   1.949 +  int rc;
   1.950 +  sqlite3_mutex_enter(db->mutex);
   1.951 +  rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
   1.952 +  rc = sqlite3ApiExit(db, rc);
   1.953 +  sqlite3_mutex_leave(db->mutex);
   1.954 +  return rc;
   1.955 +}
   1.956 +
   1.957 +#ifndef SQLITE_OMIT_UTF16
   1.958 +int sqlite3_create_function16(
   1.959 +  sqlite3 *db,
   1.960 +  const void *zFunctionName,
   1.961 +  int nArg,
   1.962 +  int eTextRep,
   1.963 +  void *p,
   1.964 +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   1.965 +  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   1.966 +  void (*xFinal)(sqlite3_context*)
   1.967 +){
   1.968 +  int rc;
   1.969 +  char *zFunc8;
   1.970 +  sqlite3_mutex_enter(db->mutex);
   1.971 +  assert( !db->mallocFailed );
   1.972 +  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
   1.973 +  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
   1.974 +  sqlite3DbFree(db, zFunc8);
   1.975 +  rc = sqlite3ApiExit(db, rc);
   1.976 +  sqlite3_mutex_leave(db->mutex);
   1.977 +  return rc;
   1.978 +}
   1.979 +#endif
   1.980 +
   1.981 +
   1.982 +/*
   1.983 +** Declare that a function has been overloaded by a virtual table.
   1.984 +**
   1.985 +** If the function already exists as a regular global function, then
   1.986 +** this routine is a no-op.  If the function does not exist, then create
   1.987 +** a new one that always throws a run-time error.  
   1.988 +**
   1.989 +** When virtual tables intend to provide an overloaded function, they
   1.990 +** should call this routine to make sure the global function exists.
   1.991 +** A global function must exist in order for name resolution to work
   1.992 +** properly.
   1.993 +*/
   1.994 +int sqlite3_overload_function(
   1.995 +  sqlite3 *db,
   1.996 +  const char *zName,
   1.997 +  int nArg
   1.998 +){
   1.999 +  int nName = sqlite3Strlen(db, zName);
  1.1000 +  int rc;
  1.1001 +  sqlite3_mutex_enter(db->mutex);
  1.1002 +  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
  1.1003 +    sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
  1.1004 +                      0, sqlite3InvalidFunction, 0, 0);
  1.1005 +  }
  1.1006 +  rc = sqlite3ApiExit(db, SQLITE_OK);
  1.1007 +  sqlite3_mutex_leave(db->mutex);
  1.1008 +  return rc;
  1.1009 +}
  1.1010 +
  1.1011 +#ifndef SQLITE_OMIT_TRACE
  1.1012 +/*
  1.1013 +** Register a trace function.  The pArg from the previously registered trace
  1.1014 +** is returned.  
  1.1015 +**
  1.1016 +** A NULL trace function means that no tracing is executes.  A non-NULL
  1.1017 +** trace is a pointer to a function that is invoked at the start of each
  1.1018 +** SQL statement.
  1.1019 +*/
  1.1020 +void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
  1.1021 +  void *pOld;
  1.1022 +  sqlite3_mutex_enter(db->mutex);
  1.1023 +  pOld = db->pTraceArg;
  1.1024 +  db->xTrace = xTrace;
  1.1025 +  db->pTraceArg = pArg;
  1.1026 +  sqlite3_mutex_leave(db->mutex);
  1.1027 +  return pOld;
  1.1028 +}
  1.1029 +/*
  1.1030 +** Register a profile function.  The pArg from the previously registered 
  1.1031 +** profile function is returned.  
  1.1032 +**
  1.1033 +** A NULL profile function means that no profiling is executes.  A non-NULL
  1.1034 +** profile is a pointer to a function that is invoked at the conclusion of
  1.1035 +** each SQL statement that is run.
  1.1036 +*/
  1.1037 +void *sqlite3_profile(
  1.1038 +  sqlite3 *db,
  1.1039 +  void (*xProfile)(void*,const char*,sqlite_uint64),
  1.1040 +  void *pArg
  1.1041 +){
  1.1042 +  void *pOld;
  1.1043 +  sqlite3_mutex_enter(db->mutex);
  1.1044 +  pOld = db->pProfileArg;
  1.1045 +  db->xProfile = xProfile;
  1.1046 +  db->pProfileArg = pArg;
  1.1047 +  sqlite3_mutex_leave(db->mutex);
  1.1048 +  return pOld;
  1.1049 +}
  1.1050 +#endif /* SQLITE_OMIT_TRACE */
  1.1051 +
  1.1052 +/*** EXPERIMENTAL ***
  1.1053 +**
  1.1054 +** Register a function to be invoked when a transaction comments.
  1.1055 +** If the invoked function returns non-zero, then the commit becomes a
  1.1056 +** rollback.
  1.1057 +*/
  1.1058 +void *sqlite3_commit_hook(
  1.1059 +  sqlite3 *db,              /* Attach the hook to this database */
  1.1060 +  int (*xCallback)(void*),  /* Function to invoke on each commit */
  1.1061 +  void *pArg                /* Argument to the function */
  1.1062 +){
  1.1063 +  void *pOld;
  1.1064 +  sqlite3_mutex_enter(db->mutex);
  1.1065 +  pOld = db->pCommitArg;
  1.1066 +  db->xCommitCallback = xCallback;
  1.1067 +  db->pCommitArg = pArg;
  1.1068 +  sqlite3_mutex_leave(db->mutex);
  1.1069 +  return pOld;
  1.1070 +}
  1.1071 +
  1.1072 +/*
  1.1073 +** Register a callback to be invoked each time a row is updated,
  1.1074 +** inserted or deleted using this database connection.
  1.1075 +*/
  1.1076 +void *sqlite3_update_hook(
  1.1077 +  sqlite3 *db,              /* Attach the hook to this database */
  1.1078 +  void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
  1.1079 +  void *pArg                /* Argument to the function */
  1.1080 +){
  1.1081 +  void *pRet;
  1.1082 +  sqlite3_mutex_enter(db->mutex);
  1.1083 +  pRet = db->pUpdateArg;
  1.1084 +  db->xUpdateCallback = xCallback;
  1.1085 +  db->pUpdateArg = pArg;
  1.1086 +  sqlite3_mutex_leave(db->mutex);
  1.1087 +  return pRet;
  1.1088 +}
  1.1089 +
  1.1090 +/*
  1.1091 +** Register a callback to be invoked each time a transaction is rolled
  1.1092 +** back by this database connection.
  1.1093 +*/
  1.1094 +void *sqlite3_rollback_hook(
  1.1095 +  sqlite3 *db,              /* Attach the hook to this database */
  1.1096 +  void (*xCallback)(void*), /* Callback function */
  1.1097 +  void *pArg                /* Argument to the function */
  1.1098 +){
  1.1099 +  void *pRet;
  1.1100 +  sqlite3_mutex_enter(db->mutex);
  1.1101 +  pRet = db->pRollbackArg;
  1.1102 +  db->xRollbackCallback = xCallback;
  1.1103 +  db->pRollbackArg = pArg;
  1.1104 +  sqlite3_mutex_leave(db->mutex);
  1.1105 +  return pRet;
  1.1106 +}
  1.1107 +
  1.1108 +/*
  1.1109 +** This routine is called to create a connection to a database BTree
  1.1110 +** driver.  If zFilename is the name of a file, then that file is
  1.1111 +** opened and used.  If zFilename is the magic name ":memory:" then
  1.1112 +** the database is stored in memory (and is thus forgotten as soon as
  1.1113 +** the connection is closed.)  If zFilename is NULL then the database
  1.1114 +** is a "virtual" database for transient use only and is deleted as
  1.1115 +** soon as the connection is closed.
  1.1116 +**
  1.1117 +** A virtual database can be either a disk file (that is automatically
  1.1118 +** deleted when the file is closed) or it an be held entirely in memory,
  1.1119 +** depending on the values of the SQLITE_TEMP_STORE compile-time macro and the
  1.1120 +** db->temp_store variable, according to the following chart:
  1.1121 +**
  1.1122 +**   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
  1.1123 +**   -----------------     --------------     ------------------------------
  1.1124 +**   0                     any                file
  1.1125 +**   1                     1                  file
  1.1126 +**   1                     2                  memory
  1.1127 +**   1                     0                  file
  1.1128 +**   2                     1                  file
  1.1129 +**   2                     2                  memory
  1.1130 +**   2                     0                  memory
  1.1131 +**   3                     any                memory
  1.1132 +*/
  1.1133 +int sqlite3BtreeFactory(
  1.1134 +  const sqlite3 *db,        /* Main database when opening aux otherwise 0 */
  1.1135 +  const char *zFilename,    /* Name of the file containing the BTree database */
  1.1136 +  int omitJournal,          /* if TRUE then do not journal this file */
  1.1137 +  int nCache,               /* How many pages in the page cache */
  1.1138 +  int vfsFlags,             /* Flags passed through to vfsOpen */
  1.1139 +  Btree **ppBtree           /* Pointer to new Btree object written here */
  1.1140 +){
  1.1141 +  int btFlags = 0;
  1.1142 +  int rc;
  1.1143 +  
  1.1144 +  assert( sqlite3_mutex_held(db->mutex) );
  1.1145 +  assert( ppBtree != 0);
  1.1146 +  if( omitJournal ){
  1.1147 +    btFlags |= BTREE_OMIT_JOURNAL;
  1.1148 +  }
  1.1149 +  if( db->flags & SQLITE_NoReadlock ){
  1.1150 +    btFlags |= BTREE_NO_READLOCK;
  1.1151 +  }
  1.1152 +  if( zFilename==0 ){
  1.1153 +#if SQLITE_TEMP_STORE==0
  1.1154 +    /* Do nothing */
  1.1155 +#endif
  1.1156 +#ifndef SQLITE_OMIT_MEMORYDB
  1.1157 +#if SQLITE_TEMP_STORE==1
  1.1158 +    if( db->temp_store==2 ) zFilename = ":memory:";
  1.1159 +#endif
  1.1160 +#if SQLITE_TEMP_STORE==2
  1.1161 +    if( db->temp_store!=1 ) zFilename = ":memory:";
  1.1162 +#endif
  1.1163 +#if SQLITE_TEMP_STORE==3
  1.1164 +    zFilename = ":memory:";
  1.1165 +#endif
  1.1166 +#endif /* SQLITE_OMIT_MEMORYDB */
  1.1167 +  }
  1.1168 +
  1.1169 +  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
  1.1170 +    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
  1.1171 +  }
  1.1172 +  rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
  1.1173 +
  1.1174 +  /* If the B-Tree was successfully opened, set the pager-cache size to the
  1.1175 +  ** default value. Except, if the call to BtreeOpen() returned a handle
  1.1176 +  ** open on an existing shared pager-cache, do not change the pager-cache 
  1.1177 +  ** size.
  1.1178 +  */
  1.1179 +  if( rc==SQLITE_OK && 0==sqlite3BtreeSchema(*ppBtree, 0, 0) ){
  1.1180 +    sqlite3BtreeSetCacheSize(*ppBtree, nCache);
  1.1181 +  }
  1.1182 +  return rc;
  1.1183 +}
  1.1184 +
  1.1185 +/*
  1.1186 +** Return UTF-8 encoded English language explanation of the most recent
  1.1187 +** error.
  1.1188 +*/
  1.1189 +const char *sqlite3_errmsg(sqlite3 *db){
  1.1190 +  const char *z;
  1.1191 +  if( !db ){
  1.1192 +    return sqlite3ErrStr(SQLITE_NOMEM);
  1.1193 +  }
  1.1194 +  if( !sqlite3SafetyCheckSickOrOk(db) ){
  1.1195 +    return sqlite3ErrStr(SQLITE_MISUSE);
  1.1196 +  }
  1.1197 +  sqlite3_mutex_enter(db->mutex);
  1.1198 +  assert( !db->mallocFailed );
  1.1199 +  z = (char*)sqlite3_value_text(db->pErr);
  1.1200 +  assert( !db->mallocFailed );
  1.1201 +  if( z==0 ){
  1.1202 +    z = sqlite3ErrStr(db->errCode);
  1.1203 +  }
  1.1204 +  sqlite3_mutex_leave(db->mutex);
  1.1205 +  return z;
  1.1206 +}
  1.1207 +
  1.1208 +#ifndef SQLITE_OMIT_UTF16
  1.1209 +/*
  1.1210 +** Return UTF-16 encoded English language explanation of the most recent
  1.1211 +** error.
  1.1212 +*/
  1.1213 +const void *sqlite3_errmsg16(sqlite3 *db){
  1.1214 +  static const u16 outOfMem[] = {
  1.1215 +    'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
  1.1216 +  };
  1.1217 +  static const u16 misuse[] = {
  1.1218 +    'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
  1.1219 +    'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
  1.1220 +    'c', 'a', 'l', 'l', 'e', 'd', ' ',
  1.1221 +    'o', 'u', 't', ' ',
  1.1222 +    'o', 'f', ' ',
  1.1223 +    's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
  1.1224 +  };
  1.1225 +
  1.1226 +  const void *z;
  1.1227 +  if( !db ){
  1.1228 +    return (void *)outOfMem;
  1.1229 +  }
  1.1230 +  if( !sqlite3SafetyCheckSickOrOk(db) ){
  1.1231 +    return (void *)misuse;
  1.1232 +  }
  1.1233 +  sqlite3_mutex_enter(db->mutex);
  1.1234 +  assert( !db->mallocFailed );
  1.1235 +  z = sqlite3_value_text16(db->pErr);
  1.1236 +  if( z==0 ){
  1.1237 +    sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
  1.1238 +         SQLITE_UTF8, SQLITE_STATIC);
  1.1239 +    z = sqlite3_value_text16(db->pErr);
  1.1240 +  }
  1.1241 +  /* A malloc() may have failed within the call to sqlite3_value_text16()
  1.1242 +  ** above. If this is the case, then the db->mallocFailed flag needs to
  1.1243 +  ** be cleared before returning. Do this directly, instead of via
  1.1244 +  ** sqlite3ApiExit(), to avoid setting the database handle error message.
  1.1245 +  */
  1.1246 +  db->mallocFailed = 0;
  1.1247 +  sqlite3_mutex_leave(db->mutex);
  1.1248 +  return z;
  1.1249 +}
  1.1250 +#endif /* SQLITE_OMIT_UTF16 */
  1.1251 +
  1.1252 +/*
  1.1253 +** Return the most recent error code generated by an SQLite routine. If NULL is
  1.1254 +** passed to this function, we assume a malloc() failed during sqlite3_open().
  1.1255 +*/
  1.1256 +int sqlite3_errcode(sqlite3 *db){
  1.1257 +  if( db && !sqlite3SafetyCheckSickOrOk(db) ){
  1.1258 +    return SQLITE_MISUSE;
  1.1259 +  }
  1.1260 +  if( !db || db->mallocFailed ){
  1.1261 +    return SQLITE_NOMEM;
  1.1262 +  }
  1.1263 +  return db->errCode & db->errMask;
  1.1264 +}
  1.1265 +
  1.1266 +/*
  1.1267 +** Create a new collating function for database "db".  The name is zName
  1.1268 +** and the encoding is enc.
  1.1269 +*/
  1.1270 +static int createCollation(
  1.1271 +  sqlite3* db, 
  1.1272 +  const char *zName, 
  1.1273 +  int enc, 
  1.1274 +  void* pCtx,
  1.1275 +  int(*xCompare)(void*,int,const void*,int,const void*),
  1.1276 +  void(*xDel)(void*)
  1.1277 +){
  1.1278 +  CollSeq *pColl;
  1.1279 +  int enc2;
  1.1280 +  int nName;
  1.1281 +  
  1.1282 +  assert( sqlite3_mutex_held(db->mutex) );
  1.1283 +
  1.1284 +  /* If SQLITE_UTF16 is specified as the encoding type, transform this
  1.1285 +  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
  1.1286 +  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
  1.1287 +  */
  1.1288 +  enc2 = enc & ~SQLITE_UTF16_ALIGNED;
  1.1289 +  if( enc2==SQLITE_UTF16 ){
  1.1290 +    enc2 = SQLITE_UTF16NATIVE;
  1.1291 +  }
  1.1292 +  if( (enc2&~3)!=0 ){
  1.1293 +    return SQLITE_MISUSE;
  1.1294 +  }
  1.1295 +
  1.1296 +  /* Check if this call is removing or replacing an existing collation 
  1.1297 +  ** sequence. If so, and there are active VMs, return busy. If there
  1.1298 +  ** are no active VMs, invalidate any pre-compiled statements.
  1.1299 +  */
  1.1300 +  nName = sqlite3Strlen(db, zName);
  1.1301 +  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, nName, 0);
  1.1302 +  if( pColl && pColl->xCmp ){
  1.1303 +    if( db->activeVdbeCnt ){
  1.1304 +      sqlite3Error(db, SQLITE_BUSY, 
  1.1305 +        "Unable to delete/modify collation sequence due to active statements");
  1.1306 +      return SQLITE_BUSY;
  1.1307 +    }
  1.1308 +    sqlite3ExpirePreparedStatements(db);
  1.1309 +
  1.1310 +    /* If collation sequence pColl was created directly by a call to
  1.1311 +    ** sqlite3_create_collation, and not generated by synthCollSeq(),
  1.1312 +    ** then any copies made by synthCollSeq() need to be invalidated.
  1.1313 +    ** Also, collation destructor - CollSeq.xDel() - function may need
  1.1314 +    ** to be called.
  1.1315 +    */ 
  1.1316 +    if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
  1.1317 +      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
  1.1318 +      int j;
  1.1319 +      for(j=0; j<3; j++){
  1.1320 +        CollSeq *p = &aColl[j];
  1.1321 +        if( p->enc==pColl->enc ){
  1.1322 +          if( p->xDel ){
  1.1323 +            p->xDel(p->pUser);
  1.1324 +          }
  1.1325 +          p->xCmp = 0;
  1.1326 +        }
  1.1327 +      }
  1.1328 +    }
  1.1329 +  }
  1.1330 +
  1.1331 +  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, nName, 1);
  1.1332 +  if( pColl ){
  1.1333 +    pColl->xCmp = xCompare;
  1.1334 +    pColl->pUser = pCtx;
  1.1335 +    pColl->xDel = xDel;
  1.1336 +    pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED);
  1.1337 +  }
  1.1338 +  sqlite3Error(db, SQLITE_OK, 0);
  1.1339 +  return SQLITE_OK;
  1.1340 +}
  1.1341 +
  1.1342 +
  1.1343 +/*
  1.1344 +** This array defines hard upper bounds on limit values.  The
  1.1345 +** initializer must be kept in sync with the SQLITE_LIMIT_*
  1.1346 +** #defines in sqlite3.h.
  1.1347 +*/
  1.1348 +static const int aHardLimit[] = {
  1.1349 +  SQLITE_MAX_LENGTH,
  1.1350 +  SQLITE_MAX_SQL_LENGTH,
  1.1351 +  SQLITE_MAX_COLUMN,
  1.1352 +  SQLITE_MAX_EXPR_DEPTH,
  1.1353 +  SQLITE_MAX_COMPOUND_SELECT,
  1.1354 +  SQLITE_MAX_VDBE_OP,
  1.1355 +  SQLITE_MAX_FUNCTION_ARG,
  1.1356 +  SQLITE_MAX_ATTACHED,
  1.1357 +  SQLITE_MAX_LIKE_PATTERN_LENGTH,
  1.1358 +  SQLITE_MAX_VARIABLE_NUMBER,
  1.1359 +};
  1.1360 +
  1.1361 +/*
  1.1362 +** Make sure the hard limits are set to reasonable values
  1.1363 +*/
  1.1364 +#if SQLITE_MAX_LENGTH<100
  1.1365 +# error SQLITE_MAX_LENGTH must be at least 100
  1.1366 +#endif
  1.1367 +#if SQLITE_MAX_SQL_LENGTH<100
  1.1368 +# error SQLITE_MAX_SQL_LENGTH must be at least 100
  1.1369 +#endif
  1.1370 +#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
  1.1371 +# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
  1.1372 +#endif
  1.1373 +#if SQLITE_MAX_COMPOUND_SELECT<2
  1.1374 +# error SQLITE_MAX_COMPOUND_SELECT must be at least 2
  1.1375 +#endif
  1.1376 +#if SQLITE_MAX_VDBE_OP<40
  1.1377 +# error SQLITE_MAX_VDBE_OP must be at least 40
  1.1378 +#endif
  1.1379 +#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
  1.1380 +# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
  1.1381 +#endif
  1.1382 +#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>30
  1.1383 +# error SQLITE_MAX_ATTACHED must be between 0 and 30
  1.1384 +#endif
  1.1385 +#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
  1.1386 +# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
  1.1387 +#endif
  1.1388 +#if SQLITE_MAX_VARIABLE_NUMBER<1
  1.1389 +# error SQLITE_MAX_VARIABLE_NUMBER must be at least 1
  1.1390 +#endif
  1.1391 +#if SQLITE_MAX_COLUMN>32767
  1.1392 +# error SQLITE_MAX_COLUMN must not exceed 32767
  1.1393 +#endif
  1.1394 +
  1.1395 +
  1.1396 +/*
  1.1397 +** Change the value of a limit.  Report the old value.
  1.1398 +** If an invalid limit index is supplied, report -1.
  1.1399 +** Make no changes but still report the old value if the
  1.1400 +** new limit is negative.
  1.1401 +**
  1.1402 +** A new lower limit does not shrink existing constructs.
  1.1403 +** It merely prevents new constructs that exceed the limit
  1.1404 +** from forming.
  1.1405 +*/
  1.1406 +int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
  1.1407 +  int oldLimit;
  1.1408 +  if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
  1.1409 +    return -1;
  1.1410 +  }
  1.1411 +  oldLimit = db->aLimit[limitId];
  1.1412 +  if( newLimit>=0 ){
  1.1413 +    if( newLimit>aHardLimit[limitId] ){
  1.1414 +      newLimit = aHardLimit[limitId];
  1.1415 +    }
  1.1416 +    db->aLimit[limitId] = newLimit;
  1.1417 +  }
  1.1418 +  return oldLimit;
  1.1419 +}
  1.1420 +
  1.1421 +/*
  1.1422 +** This routine does the work of opening a database on behalf of
  1.1423 +** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
  1.1424 +** is UTF-8 encoded.
  1.1425 +*/
  1.1426 +static int openDatabase(
  1.1427 +  const char *zFilename, /* Database filename UTF-8 encoded */
  1.1428 +  sqlite3 **ppDb,        /* OUT: Returned database handle */
  1.1429 +  unsigned flags,        /* Operational flags */
  1.1430 +  const char *zVfs       /* Name of the VFS to use */
  1.1431 +){
  1.1432 +  sqlite3 *db;
  1.1433 +  int rc;
  1.1434 +  CollSeq *pColl;
  1.1435 +  int isThreadsafe;
  1.1436 +
  1.1437 +#ifndef SQLITE_OMIT_AUTOINIT
  1.1438 +  rc = sqlite3_initialize();
  1.1439 +  if( rc ) return rc;
  1.1440 +#endif
  1.1441 +
  1.1442 +  if( sqlite3GlobalConfig.bCoreMutex==0 ){
  1.1443 +    isThreadsafe = 0;
  1.1444 +  }else if( flags & SQLITE_OPEN_NOMUTEX ){
  1.1445 +    isThreadsafe = 0;
  1.1446 +  }else if( flags & SQLITE_OPEN_FULLMUTEX ){
  1.1447 +    isThreadsafe = 1;
  1.1448 +  }else{
  1.1449 +    isThreadsafe = sqlite3GlobalConfig.bFullMutex;
  1.1450 +  }
  1.1451 +
  1.1452 +  /* Remove harmful bits from the flags parameter */
  1.1453 +  flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
  1.1454 +               SQLITE_OPEN_MAIN_DB |
  1.1455 +               SQLITE_OPEN_TEMP_DB | 
  1.1456 +               SQLITE_OPEN_TRANSIENT_DB | 
  1.1457 +               SQLITE_OPEN_MAIN_JOURNAL | 
  1.1458 +               SQLITE_OPEN_TEMP_JOURNAL | 
  1.1459 +               SQLITE_OPEN_SUBJOURNAL | 
  1.1460 +               SQLITE_OPEN_MASTER_JOURNAL |
  1.1461 +               SQLITE_OPEN_NOMUTEX |
  1.1462 +               SQLITE_OPEN_FULLMUTEX
  1.1463 +             );
  1.1464 +
  1.1465 +  /* Allocate the sqlite data structure */
  1.1466 +  db = sqlite3MallocZero( sizeof(sqlite3) );
  1.1467 +  if( db==0 ) goto opendb_out;
  1.1468 +  if( isThreadsafe ){
  1.1469 +    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
  1.1470 +    if( db->mutex==0 ){
  1.1471 +      sqlite3_free(db);
  1.1472 +      db = 0;
  1.1473 +      goto opendb_out;
  1.1474 +    }
  1.1475 +  }
  1.1476 +  sqlite3_mutex_enter(db->mutex);
  1.1477 +  db->errMask = 0xff;
  1.1478 +  db->priorNewRowid = 0;
  1.1479 +  db->nDb = 2;
  1.1480 +  db->magic = SQLITE_MAGIC_BUSY;
  1.1481 +  db->aDb = db->aDbStatic;
  1.1482 +
  1.1483 +  assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
  1.1484 +  memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
  1.1485 +  db->autoCommit = 1;
  1.1486 +  db->nextAutovac = -1;
  1.1487 +  db->nextPagesize = 0;
  1.1488 +  db->flags |= SQLITE_ShortColNames
  1.1489 +#if SQLITE_DEFAULT_FILE_FORMAT<4
  1.1490 +                 | SQLITE_LegacyFileFmt
  1.1491 +#endif
  1.1492 +#ifdef SQLITE_ENABLE_LOAD_EXTENSION
  1.1493 +                 | SQLITE_LoadExtension
  1.1494 +#endif
  1.1495 +      ;
  1.1496 +  sqlite3HashInit(&db->aCollSeq, 0);
  1.1497 +#ifndef SQLITE_OMIT_VIRTUALTABLE
  1.1498 +  sqlite3HashInit(&db->aModule, 0);
  1.1499 +#endif
  1.1500 +
  1.1501 +  db->pVfs = sqlite3_vfs_find(zVfs);
  1.1502 +  if( !db->pVfs ){
  1.1503 +    rc = SQLITE_ERROR;
  1.1504 +    sqlite3Error(db, rc, "no such vfs: %s", zVfs);
  1.1505 +    goto opendb_out;
  1.1506 +  }
  1.1507 +
  1.1508 +  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
  1.1509 +  ** and UTF-16, so add a version for each to avoid any unnecessary
  1.1510 +  ** conversions. The only error that can occur here is a malloc() failure.
  1.1511 +  */
  1.1512 +  createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
  1.1513 +  createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
  1.1514 +  createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
  1.1515 +  createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
  1.1516 +  if( db->mallocFailed ){
  1.1517 +    goto opendb_out;
  1.1518 +  }
  1.1519 +  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
  1.1520 +  assert( db->pDfltColl!=0 );
  1.1521 +
  1.1522 +  /* Also add a UTF-8 case-insensitive collation sequence. */
  1.1523 +  createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
  1.1524 +
  1.1525 +  /* Set flags on the built-in collating sequences */
  1.1526 +  db->pDfltColl->type = SQLITE_COLL_BINARY;
  1.1527 +  pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);
  1.1528 +  if( pColl ){
  1.1529 +    pColl->type = SQLITE_COLL_NOCASE;
  1.1530 +  }
  1.1531 +
  1.1532 +  /* Open the backend database driver */
  1.1533 +  db->openFlags = flags;
  1.1534 +  rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, 
  1.1535 +                           flags | SQLITE_OPEN_MAIN_DB,
  1.1536 +                           &db->aDb[0].pBt);
  1.1537 +  if( rc!=SQLITE_OK ){
  1.1538 +    if( rc==SQLITE_IOERR_NOMEM ){
  1.1539 +      rc = SQLITE_NOMEM;
  1.1540 +    }
  1.1541 +    sqlite3Error(db, rc, 0);
  1.1542 +    goto opendb_out;
  1.1543 +  }
  1.1544 +  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
  1.1545 +  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
  1.1546 +
  1.1547 +
  1.1548 +  /* The default safety_level for the main database is 'full'; for the temp
  1.1549 +  ** database it is 'NONE'. This matches the pager layer defaults.  
  1.1550 +  */
  1.1551 +  db->aDb[0].zName = "main";
  1.1552 +  db->aDb[0].safety_level = 3;
  1.1553 +#ifndef SQLITE_OMIT_TEMPDB
  1.1554 +  db->aDb[1].zName = "temp";
  1.1555 +  db->aDb[1].safety_level = 1;
  1.1556 +#endif
  1.1557 +
  1.1558 +  db->magic = SQLITE_MAGIC_OPEN;
  1.1559 +  if( db->mallocFailed ){
  1.1560 +    goto opendb_out;
  1.1561 +  }
  1.1562 +
  1.1563 +  /* Register all built-in functions, but do not attempt to read the
  1.1564 +  ** database schema yet. This is delayed until the first time the database
  1.1565 +  ** is accessed.
  1.1566 +  */
  1.1567 +  sqlite3Error(db, SQLITE_OK, 0);
  1.1568 +  sqlite3RegisterBuiltinFunctions(db);
  1.1569 +
  1.1570 +  /* Load automatic extensions - extensions that have been registered
  1.1571 +  ** using the sqlite3_automatic_extension() API.
  1.1572 +  */
  1.1573 +  (void)sqlite3AutoLoadExtensions(db);
  1.1574 +  if( sqlite3_errcode(db)!=SQLITE_OK ){
  1.1575 +    goto opendb_out;
  1.1576 +  }
  1.1577 +
  1.1578 +#ifdef SQLITE_ENABLE_FTS1
  1.1579 +  if( !db->mallocFailed ){
  1.1580 +    extern int sqlite3Fts1Init(sqlite3*);
  1.1581 +    rc = sqlite3Fts1Init(db);
  1.1582 +  }
  1.1583 +#endif
  1.1584 +
  1.1585 +#ifdef SQLITE_ENABLE_FTS2
  1.1586 +  if( !db->mallocFailed && rc==SQLITE_OK ){
  1.1587 +    extern int sqlite3Fts2Init(sqlite3*);
  1.1588 +    rc = sqlite3Fts2Init(db);
  1.1589 +  }
  1.1590 +#endif
  1.1591 +
  1.1592 +#ifdef SQLITE_ENABLE_FTS3
  1.1593 +  if( !db->mallocFailed && rc==SQLITE_OK ){
  1.1594 +    rc = sqlite3Fts3Init(db);
  1.1595 +  }
  1.1596 +#endif
  1.1597 +
  1.1598 +#ifdef SQLITE_ENABLE_ICU
  1.1599 +  if( !db->mallocFailed && rc==SQLITE_OK ){
  1.1600 +    rc = sqlite3IcuInit(db);
  1.1601 +  }
  1.1602 +#endif
  1.1603 +
  1.1604 +#ifdef SQLITE_ENABLE_RTREE
  1.1605 +  if( !db->mallocFailed && rc==SQLITE_OK){
  1.1606 +    rc = sqlite3RtreeInit(db);
  1.1607 +  }
  1.1608 +#endif
  1.1609 +
  1.1610 +  sqlite3Error(db, rc, 0);
  1.1611 +
  1.1612 +  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  1.1613 +  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  1.1614 +  ** mode.  Doing nothing at all also makes NORMAL the default.
  1.1615 +  */
  1.1616 +#ifdef SQLITE_DEFAULT_LOCKING_MODE
  1.1617 +  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
  1.1618 +  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
  1.1619 +                          SQLITE_DEFAULT_LOCKING_MODE);
  1.1620 +#endif
  1.1621 +
  1.1622 +  /* Enable the lookaside-malloc subsystem */
  1.1623 +  setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
  1.1624 +                        sqlite3GlobalConfig.nLookaside);
  1.1625 +
  1.1626 +opendb_out:
  1.1627 +  if( db ){
  1.1628 +    assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
  1.1629 +    sqlite3_mutex_leave(db->mutex);
  1.1630 +  }
  1.1631 +  rc = sqlite3_errcode(db);
  1.1632 +  if( rc==SQLITE_NOMEM ){
  1.1633 +    sqlite3_close(db);
  1.1634 +    db = 0;
  1.1635 +  }else if( rc!=SQLITE_OK ){
  1.1636 +    db->magic = SQLITE_MAGIC_SICK;
  1.1637 +  }
  1.1638 +  *ppDb = db;
  1.1639 +  return sqlite3ApiExit(0, rc);
  1.1640 +}
  1.1641 +
  1.1642 +/*
  1.1643 +** Open a new database handle.
  1.1644 +*/
  1.1645 +int sqlite3_open(
  1.1646 +  const char *zFilename, 
  1.1647 +  sqlite3 **ppDb 
  1.1648 +){
  1.1649 +  return openDatabase(zFilename, ppDb,
  1.1650 +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
  1.1651 +}
  1.1652 +int sqlite3_open_v2(
  1.1653 +  const char *filename,   /* Database filename (UTF-8) */
  1.1654 +  sqlite3 **ppDb,         /* OUT: SQLite db handle */
  1.1655 +  int flags,              /* Flags */
  1.1656 +  const char *zVfs        /* Name of VFS module to use */
  1.1657 +){
  1.1658 +  return openDatabase(filename, ppDb, flags, zVfs);
  1.1659 +}
  1.1660 +
  1.1661 +#ifndef SQLITE_OMIT_UTF16
  1.1662 +/*
  1.1663 +** Open a new database handle.
  1.1664 +*/
  1.1665 +int sqlite3_open16(
  1.1666 +  const void *zFilename, 
  1.1667 +  sqlite3 **ppDb
  1.1668 +){
  1.1669 +  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
  1.1670 +  sqlite3_value *pVal;
  1.1671 +  int rc;
  1.1672 +
  1.1673 +  assert( zFilename );
  1.1674 +  assert( ppDb );
  1.1675 +  *ppDb = 0;
  1.1676 +#ifndef SQLITE_OMIT_AUTOINIT
  1.1677 +  rc = sqlite3_initialize();
  1.1678 +  if( rc ) return rc;
  1.1679 +#endif
  1.1680 +  pVal = sqlite3ValueNew(0);
  1.1681 +  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
  1.1682 +  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
  1.1683 +  if( zFilename8 ){
  1.1684 +    rc = openDatabase(zFilename8, ppDb,
  1.1685 +                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
  1.1686 +    assert( *ppDb || rc==SQLITE_NOMEM );
  1.1687 +    if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
  1.1688 +      ENC(*ppDb) = SQLITE_UTF16NATIVE;
  1.1689 +    }
  1.1690 +  }else{
  1.1691 +    rc = SQLITE_NOMEM;
  1.1692 +  }
  1.1693 +  sqlite3ValueFree(pVal);
  1.1694 +
  1.1695 +  return sqlite3ApiExit(0, rc);
  1.1696 +}
  1.1697 +#endif /* SQLITE_OMIT_UTF16 */
  1.1698 +
  1.1699 +/*
  1.1700 +** Register a new collation sequence with the database handle db.
  1.1701 +*/
  1.1702 +int sqlite3_create_collation(
  1.1703 +  sqlite3* db, 
  1.1704 +  const char *zName, 
  1.1705 +  int enc, 
  1.1706 +  void* pCtx,
  1.1707 +  int(*xCompare)(void*,int,const void*,int,const void*)
  1.1708 +){
  1.1709 +  int rc;
  1.1710 +  sqlite3_mutex_enter(db->mutex);
  1.1711 +  assert( !db->mallocFailed );
  1.1712 +  rc = createCollation(db, zName, enc, pCtx, xCompare, 0);
  1.1713 +  rc = sqlite3ApiExit(db, rc);
  1.1714 +  sqlite3_mutex_leave(db->mutex);
  1.1715 +  return rc;
  1.1716 +}
  1.1717 +
  1.1718 +/*
  1.1719 +** Register a new collation sequence with the database handle db.
  1.1720 +*/
  1.1721 +int sqlite3_create_collation_v2(
  1.1722 +  sqlite3* db, 
  1.1723 +  const char *zName, 
  1.1724 +  int enc, 
  1.1725 +  void* pCtx,
  1.1726 +  int(*xCompare)(void*,int,const void*,int,const void*),
  1.1727 +  void(*xDel)(void*)
  1.1728 +){
  1.1729 +  int rc;
  1.1730 +  sqlite3_mutex_enter(db->mutex);
  1.1731 +  assert( !db->mallocFailed );
  1.1732 +  rc = createCollation(db, zName, enc, pCtx, xCompare, xDel);
  1.1733 +  rc = sqlite3ApiExit(db, rc);
  1.1734 +  sqlite3_mutex_leave(db->mutex);
  1.1735 +  return rc;
  1.1736 +}
  1.1737 +
  1.1738 +#ifndef SQLITE_OMIT_UTF16
  1.1739 +/*
  1.1740 +** Register a new collation sequence with the database handle db.
  1.1741 +*/
  1.1742 +int sqlite3_create_collation16(
  1.1743 +  sqlite3* db, 
  1.1744 +  const void *zName,
  1.1745 +  int enc, 
  1.1746 +  void* pCtx,
  1.1747 +  int(*xCompare)(void*,int,const void*,int,const void*)
  1.1748 +){
  1.1749 +  int rc = SQLITE_OK;
  1.1750 +  char *zName8;
  1.1751 +  sqlite3_mutex_enter(db->mutex);
  1.1752 +  assert( !db->mallocFailed );
  1.1753 +  zName8 = sqlite3Utf16to8(db, zName, -1);
  1.1754 +  if( zName8 ){
  1.1755 +    rc = createCollation(db, zName8, enc, pCtx, xCompare, 0);
  1.1756 +    sqlite3DbFree(db, zName8);
  1.1757 +  }
  1.1758 +  rc = sqlite3ApiExit(db, rc);
  1.1759 +  sqlite3_mutex_leave(db->mutex);
  1.1760 +  return rc;
  1.1761 +}
  1.1762 +#endif /* SQLITE_OMIT_UTF16 */
  1.1763 +
  1.1764 +/*
  1.1765 +** Register a collation sequence factory callback with the database handle
  1.1766 +** db. Replace any previously installed collation sequence factory.
  1.1767 +*/
  1.1768 +int sqlite3_collation_needed(
  1.1769 +  sqlite3 *db, 
  1.1770 +  void *pCollNeededArg, 
  1.1771 +  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
  1.1772 +){
  1.1773 +  sqlite3_mutex_enter(db->mutex);
  1.1774 +  db->xCollNeeded = xCollNeeded;
  1.1775 +  db->xCollNeeded16 = 0;
  1.1776 +  db->pCollNeededArg = pCollNeededArg;
  1.1777 +  sqlite3_mutex_leave(db->mutex);
  1.1778 +  return SQLITE_OK;
  1.1779 +}
  1.1780 +
  1.1781 +#ifndef SQLITE_OMIT_UTF16
  1.1782 +/*
  1.1783 +** Register a collation sequence factory callback with the database handle
  1.1784 +** db. Replace any previously installed collation sequence factory.
  1.1785 +*/
  1.1786 +int sqlite3_collation_needed16(
  1.1787 +  sqlite3 *db, 
  1.1788 +  void *pCollNeededArg, 
  1.1789 +  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
  1.1790 +){
  1.1791 +  sqlite3_mutex_enter(db->mutex);
  1.1792 +  db->xCollNeeded = 0;
  1.1793 +  db->xCollNeeded16 = xCollNeeded16;
  1.1794 +  db->pCollNeededArg = pCollNeededArg;
  1.1795 +  sqlite3_mutex_leave(db->mutex);
  1.1796 +  return SQLITE_OK;
  1.1797 +}
  1.1798 +#endif /* SQLITE_OMIT_UTF16 */
  1.1799 +
  1.1800 +#ifndef SQLITE_OMIT_GLOBALRECOVER
  1.1801 +#ifndef SQLITE_OMIT_DEPRECATED
  1.1802 +/*
  1.1803 +** This function is now an anachronism. It used to be used to recover from a
  1.1804 +** malloc() failure, but SQLite now does this automatically.
  1.1805 +*/
  1.1806 +int sqlite3_global_recover(void){
  1.1807 +  return SQLITE_OK;
  1.1808 +}
  1.1809 +#endif
  1.1810 +#endif
  1.1811 +
  1.1812 +/*
  1.1813 +** Test to see whether or not the database connection is in autocommit
  1.1814 +** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
  1.1815 +** by default.  Autocommit is disabled by a BEGIN statement and reenabled
  1.1816 +** by the next COMMIT or ROLLBACK.
  1.1817 +**
  1.1818 +******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
  1.1819 +*/
  1.1820 +int sqlite3_get_autocommit(sqlite3 *db){
  1.1821 +  return db->autoCommit;
  1.1822 +}
  1.1823 +
  1.1824 +#ifdef SQLITE_DEBUG
  1.1825 +/*
  1.1826 +** The following routine is subtituted for constant SQLITE_CORRUPT in
  1.1827 +** debugging builds.  This provides a way to set a breakpoint for when
  1.1828 +** corruption is first detected.
  1.1829 +*/
  1.1830 +int sqlite3Corrupt(void){
  1.1831 +  return SQLITE_CORRUPT;
  1.1832 +}
  1.1833 +#endif
  1.1834 +
  1.1835 +#ifndef SQLITE_OMIT_DEPRECATED
  1.1836 +/*
  1.1837 +** This is a convenience routine that makes sure that all thread-specific
  1.1838 +** data for this thread has been deallocated.
  1.1839 +**
  1.1840 +** SQLite no longer uses thread-specific data so this routine is now a
  1.1841 +** no-op.  It is retained for historical compatibility.
  1.1842 +*/
  1.1843 +void sqlite3_thread_cleanup(void){
  1.1844 +}
  1.1845 +#endif
  1.1846 +
  1.1847 +/*
  1.1848 +** Return meta information about a specific column of a database table.
  1.1849 +** See comment in sqlite3.h (sqlite.h.in) for details.
  1.1850 +*/
  1.1851 +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1.1852 +int sqlite3_table_column_metadata(
  1.1853 +  sqlite3 *db,                /* Connection handle */
  1.1854 +  const char *zDbName,        /* Database name or NULL */
  1.1855 +  const char *zTableName,     /* Table name */
  1.1856 +  const char *zColumnName,    /* Column name */
  1.1857 +  char const **pzDataType,    /* OUTPUT: Declared data type */
  1.1858 +  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
  1.1859 +  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  1.1860 +  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  1.1861 +  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
  1.1862 +){
  1.1863 +  int rc;
  1.1864 +  char *zErrMsg = 0;
  1.1865 +  Table *pTab = 0;
  1.1866 +  Column *pCol = 0;
  1.1867 +  int iCol;
  1.1868 +
  1.1869 +  char const *zDataType = 0;
  1.1870 +  char const *zCollSeq = 0;
  1.1871 +  int notnull = 0;
  1.1872 +  int primarykey = 0;
  1.1873 +  int autoinc = 0;
  1.1874 +
  1.1875 +  /* Ensure the database schema has been loaded */
  1.1876 +  sqlite3_mutex_enter(db->mutex);
  1.1877 +  (void)sqlite3SafetyOn(db);
  1.1878 +  sqlite3BtreeEnterAll(db);
  1.1879 +  rc = sqlite3Init(db, &zErrMsg);
  1.1880 +  sqlite3BtreeLeaveAll(db);
  1.1881 +  if( SQLITE_OK!=rc ){
  1.1882 +    goto error_out;
  1.1883 +  }
  1.1884 +
  1.1885 +  /* Locate the table in question */
  1.1886 +  pTab = sqlite3FindTable(db, zTableName, zDbName);
  1.1887 +  if( !pTab || pTab->pSelect ){
  1.1888 +    pTab = 0;
  1.1889 +    goto error_out;
  1.1890 +  }
  1.1891 +
  1.1892 +  /* Find the column for which info is requested */
  1.1893 +  if( sqlite3IsRowid(zColumnName) ){
  1.1894 +    iCol = pTab->iPKey;
  1.1895 +    if( iCol>=0 ){
  1.1896 +      pCol = &pTab->aCol[iCol];
  1.1897 +    }
  1.1898 +  }else{
  1.1899 +    for(iCol=0; iCol<pTab->nCol; iCol++){
  1.1900 +      pCol = &pTab->aCol[iCol];
  1.1901 +      if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
  1.1902 +        break;
  1.1903 +      }
  1.1904 +    }
  1.1905 +    if( iCol==pTab->nCol ){
  1.1906 +      pTab = 0;
  1.1907 +      goto error_out;
  1.1908 +    }
  1.1909 +  }
  1.1910 +
  1.1911 +  /* The following block stores the meta information that will be returned
  1.1912 +  ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
  1.1913 +  ** and autoinc. At this point there are two possibilities:
  1.1914 +  ** 
  1.1915 +  **     1. The specified column name was rowid", "oid" or "_rowid_" 
  1.1916 +  **        and there is no explicitly declared IPK column. 
  1.1917 +  **
  1.1918 +  **     2. The table is not a view and the column name identified an 
  1.1919 +  **        explicitly declared column. Copy meta information from *pCol.
  1.1920 +  */ 
  1.1921 +  if( pCol ){
  1.1922 +    zDataType = pCol->zType;
  1.1923 +    zCollSeq = pCol->zColl;
  1.1924 +    notnull = pCol->notNull!=0;
  1.1925 +    primarykey  = pCol->isPrimKey!=0;
  1.1926 +    autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
  1.1927 +  }else{
  1.1928 +    zDataType = "INTEGER";
  1.1929 +    primarykey = 1;
  1.1930 +  }
  1.1931 +  if( !zCollSeq ){
  1.1932 +    zCollSeq = "BINARY";
  1.1933 +  }
  1.1934 +
  1.1935 +error_out:
  1.1936 +  (void)sqlite3SafetyOff(db);
  1.1937 +
  1.1938 +  /* Whether the function call succeeded or failed, set the output parameters
  1.1939 +  ** to whatever their local counterparts contain. If an error did occur,
  1.1940 +  ** this has the effect of zeroing all output parameters.
  1.1941 +  */
  1.1942 +  if( pzDataType ) *pzDataType = zDataType;
  1.1943 +  if( pzCollSeq ) *pzCollSeq = zCollSeq;
  1.1944 +  if( pNotNull ) *pNotNull = notnull;
  1.1945 +  if( pPrimaryKey ) *pPrimaryKey = primarykey;
  1.1946 +  if( pAutoinc ) *pAutoinc = autoinc;
  1.1947 +
  1.1948 +  if( SQLITE_OK==rc && !pTab ){
  1.1949 +    sqlite3DbFree(db, zErrMsg);
  1.1950 +    zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
  1.1951 +        zColumnName);
  1.1952 +    rc = SQLITE_ERROR;
  1.1953 +  }
  1.1954 +  sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
  1.1955 +  sqlite3DbFree(db, zErrMsg);
  1.1956 +  rc = sqlite3ApiExit(db, rc);
  1.1957 +  sqlite3_mutex_leave(db->mutex);
  1.1958 +  return rc;
  1.1959 +}
  1.1960 +#endif
  1.1961 +
  1.1962 +/*
  1.1963 +** Sleep for a little while.  Return the amount of time slept.
  1.1964 +*/
  1.1965 +int sqlite3_sleep(int ms){
  1.1966 +  sqlite3_vfs *pVfs;
  1.1967 +  int rc;
  1.1968 +  pVfs = sqlite3_vfs_find(0);
  1.1969 +  if( pVfs==0 ) return 0;
  1.1970 +
  1.1971 +  /* This function works in milliseconds, but the underlying OsSleep() 
  1.1972 +  ** API uses microseconds. Hence the 1000's.
  1.1973 +  */
  1.1974 +  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
  1.1975 +  return rc;
  1.1976 +}
  1.1977 +
  1.1978 +/*
  1.1979 +** Enable or disable the extended result codes.
  1.1980 +*/
  1.1981 +int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
  1.1982 +  sqlite3_mutex_enter(db->mutex);
  1.1983 +  db->errMask = onoff ? 0xffffffff : 0xff;
  1.1984 +  sqlite3_mutex_leave(db->mutex);
  1.1985 +  return SQLITE_OK;
  1.1986 +}
  1.1987 +
  1.1988 +/*
  1.1989 +** Invoke the xFileControl method on a particular database.
  1.1990 +*/
  1.1991 +int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
  1.1992 +  int rc = SQLITE_ERROR;
  1.1993 +  int iDb;
  1.1994 +  sqlite3_mutex_enter(db->mutex);
  1.1995 +  if( zDbName==0 ){
  1.1996 +    iDb = 0;
  1.1997 +  }else{
  1.1998 +    for(iDb=0; iDb<db->nDb; iDb++){
  1.1999 +      if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
  1.2000 +    }
  1.2001 +  }
  1.2002 +  if( iDb<db->nDb ){
  1.2003 +    Btree *pBtree = db->aDb[iDb].pBt;
  1.2004 +    if( pBtree ){
  1.2005 +      Pager *pPager;
  1.2006 +      sqlite3_file *fd;
  1.2007 +      sqlite3BtreeEnter(pBtree);
  1.2008 +      pPager = sqlite3BtreePager(pBtree);
  1.2009 +      assert( pPager!=0 );
  1.2010 +      fd = sqlite3PagerFile(pPager);
  1.2011 +      assert( fd!=0 );
  1.2012 +      if( fd->pMethods ){
  1.2013 +        rc = sqlite3OsFileControl(fd, op, pArg);
  1.2014 +      }
  1.2015 +      sqlite3BtreeLeave(pBtree);
  1.2016 +    }
  1.2017 +  }
  1.2018 +  sqlite3_mutex_leave(db->mutex);
  1.2019 +  return rc;   
  1.2020 +}
  1.2021 +
  1.2022 +/*
  1.2023 +** Interface to the testing logic.
  1.2024 +*/
  1.2025 +int sqlite3_test_control(int op, ...){
  1.2026 +  int rc = 0;
  1.2027 +#ifndef SQLITE_OMIT_BUILTIN_TEST
  1.2028 +  va_list ap;
  1.2029 +  va_start(ap, op);
  1.2030 +  switch( op ){
  1.2031 +
  1.2032 +    /*
  1.2033 +    ** Save the current state of the PRNG.
  1.2034 +    */
  1.2035 +    case SQLITE_TESTCTRL_PRNG_SAVE: {
  1.2036 +      sqlite3PrngSaveState();
  1.2037 +      break;
  1.2038 +    }
  1.2039 +
  1.2040 +    /*
  1.2041 +    ** Restore the state of the PRNG to the last state saved using
  1.2042 +    ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
  1.2043 +    ** this verb acts like PRNG_RESET.
  1.2044 +    */
  1.2045 +    case SQLITE_TESTCTRL_PRNG_RESTORE: {
  1.2046 +      sqlite3PrngRestoreState();
  1.2047 +      break;
  1.2048 +    }
  1.2049 +
  1.2050 +    /*
  1.2051 +    ** Reset the PRNG back to its uninitialized state.  The next call
  1.2052 +    ** to sqlite3_randomness() will reseed the PRNG using a single call
  1.2053 +    ** to the xRandomness method of the default VFS.
  1.2054 +    */
  1.2055 +    case SQLITE_TESTCTRL_PRNG_RESET: {
  1.2056 +      sqlite3PrngResetState();
  1.2057 +      break;
  1.2058 +    }
  1.2059 +
  1.2060 +    /*
  1.2061 +    **  sqlite3_test_control(BITVEC_TEST, size, program)
  1.2062 +    **
  1.2063 +    ** Run a test against a Bitvec object of size.  The program argument
  1.2064 +    ** is an array of integers that defines the test.  Return -1 on a
  1.2065 +    ** memory allocation error, 0 on success, or non-zero for an error.
  1.2066 +    ** See the sqlite3BitvecBuiltinTest() for additional information.
  1.2067 +    */
  1.2068 +    case SQLITE_TESTCTRL_BITVEC_TEST: {
  1.2069 +      int sz = va_arg(ap, int);
  1.2070 +      int *aProg = va_arg(ap, int*);
  1.2071 +      rc = sqlite3BitvecBuiltinTest(sz, aProg);
  1.2072 +      break;
  1.2073 +    }
  1.2074 +
  1.2075 +    /*
  1.2076 +    **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
  1.2077 +    **
  1.2078 +    ** Register hooks to call to indicate which malloc() failures 
  1.2079 +    ** are benign.
  1.2080 +    */
  1.2081 +    case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
  1.2082 +      typedef void (*void_function)(void);
  1.2083 +      void_function xBenignBegin;
  1.2084 +      void_function xBenignEnd;
  1.2085 +      xBenignBegin = va_arg(ap, void_function);
  1.2086 +      xBenignEnd = va_arg(ap, void_function);
  1.2087 +      sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
  1.2088 +      break;
  1.2089 +    }
  1.2090 +  }
  1.2091 +  va_end(ap);
  1.2092 +#endif /* SQLITE_OMIT_BUILTIN_TEST */
  1.2093 +  return rc;
  1.2094 +}