extensions/sqlite/sqlite-source/main.c
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/extensions/sqlite/sqlite-source/main.c	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,1485 +0,0 @@
     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$
    1.21 -*/
    1.22 -#include "sqliteInt.h"
    1.23 -#include <ctype.h>
    1.24 -
    1.25 -/*
    1.26 -** The version of the library
    1.27 -*/
    1.28 -const char sqlite3_version[] = SQLITE_VERSION;
    1.29 -const char *sqlite3_libversion(void){ return sqlite3_version; }
    1.30 -int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
    1.31 -int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
    1.32 -
    1.33 -/*
    1.34 -** If the following function pointer is not NULL and if
    1.35 -** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
    1.36 -** I/O active are written using this function.  These messages
    1.37 -** are intended for debugging activity only.
    1.38 -*/
    1.39 -void (*sqlite3_io_trace)(const char*, ...) = 0;
    1.40 -
    1.41 -/*
    1.42 -** If the following global variable points to a string which is the
    1.43 -** name of a directory, then that directory will be used to store
    1.44 -** temporary files.
    1.45 -**
    1.46 -** See also the "PRAGMA temp_store_directory" SQL command.
    1.47 -*/
    1.48 -char *sqlite3_temp_directory = 0;
    1.49 -
    1.50 -
    1.51 -/*
    1.52 -** This is the default collating function named "BINARY" which is always
    1.53 -** available.
    1.54 -*/
    1.55 -static int binCollFunc(
    1.56 -  void *NotUsed,
    1.57 -  int nKey1, const void *pKey1,
    1.58 -  int nKey2, const void *pKey2
    1.59 -){
    1.60 -  int rc, n;
    1.61 -  n = nKey1<nKey2 ? nKey1 : nKey2;
    1.62 -  rc = memcmp(pKey1, pKey2, n);
    1.63 -  if( rc==0 ){
    1.64 -    rc = nKey1 - nKey2;
    1.65 -  }
    1.66 -  return rc;
    1.67 -}
    1.68 -
    1.69 -/*
    1.70 -** Another built-in collating sequence: NOCASE. 
    1.71 -**
    1.72 -** This collating sequence is intended to be used for "case independant
    1.73 -** comparison". SQLite's knowledge of upper and lower case equivalents
    1.74 -** extends only to the 26 characters used in the English language.
    1.75 -**
    1.76 -** At the moment there is only a UTF-8 implementation.
    1.77 -*/
    1.78 -static int nocaseCollatingFunc(
    1.79 -  void *NotUsed,
    1.80 -  int nKey1, const void *pKey1,
    1.81 -  int nKey2, const void *pKey2
    1.82 -){
    1.83 -  int r = sqlite3StrNICmp(
    1.84 -      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
    1.85 -  if( 0==r ){
    1.86 -    r = nKey1-nKey2;
    1.87 -  }
    1.88 -  return r;
    1.89 -}
    1.90 -
    1.91 -/*
    1.92 -** Return the ROWID of the most recent insert
    1.93 -*/
    1.94 -sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
    1.95 -  return db->lastRowid;
    1.96 -}
    1.97 -
    1.98 -/*
    1.99 -** Return the number of changes in the most recent call to sqlite3_exec().
   1.100 -*/
   1.101 -int sqlite3_changes(sqlite3 *db){
   1.102 -  return db->nChange;
   1.103 -}
   1.104 -
   1.105 -/*
   1.106 -** Return the number of changes since the database handle was opened.
   1.107 -*/
   1.108 -int sqlite3_total_changes(sqlite3 *db){
   1.109 -  return db->nTotalChange;
   1.110 -}
   1.111 -
   1.112 -/*
   1.113 -** Close an existing SQLite database
   1.114 -*/
   1.115 -int sqlite3_close(sqlite3 *db){
   1.116 -  HashElem *i;
   1.117 -  int j;
   1.118 -
   1.119 -  if( !db ){
   1.120 -    return SQLITE_OK;
   1.121 -  }
   1.122 -  if( sqlite3SafetyCheck(db) ){
   1.123 -    return SQLITE_MISUSE;
   1.124 -  }
   1.125 -  sqlite3_mutex_enter(db->mutex);
   1.126 -
   1.127 -#ifdef SQLITE_SSE
   1.128 -  {
   1.129 -    extern void sqlite3SseCleanup(sqlite3*);
   1.130 -    sqlite3SseCleanup(db);
   1.131 -  }
   1.132 -#endif 
   1.133 -
   1.134 -  sqlite3ResetInternalSchema(db, 0);
   1.135 -
   1.136 -  /* If a transaction is open, the ResetInternalSchema() call above
   1.137 -  ** will not have called the xDisconnect() method on any virtual
   1.138 -  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
   1.139 -  ** call will do so. We need to do this before the check for active
   1.140 -  ** SQL statements below, as the v-table implementation may be storing
   1.141 -  ** some prepared statements internally.
   1.142 -  */
   1.143 -  sqlite3VtabRollback(db);
   1.144 -
   1.145 -  /* If there are any outstanding VMs, return SQLITE_BUSY. */
   1.146 -  if( db->pVdbe ){
   1.147 -    sqlite3Error(db, SQLITE_BUSY, 
   1.148 -        "Unable to close due to unfinalised statements");
   1.149 -    sqlite3_mutex_leave(db->mutex);
   1.150 -    return SQLITE_BUSY;
   1.151 -  }
   1.152 -  assert( !sqlite3SafetyCheck(db) );
   1.153 -
   1.154 -  /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
   1.155 -  ** cannot be opened for some reason. So this routine needs to run in
   1.156 -  ** that case. But maybe there should be an extra magic value for the
   1.157 -  ** "failed to open" state.
   1.158 -  **
   1.159 -  ** TODO: Coverage tests do not test the case where this condition is
   1.160 -  ** true. It's hard to see how to cause it without messing with threads.
   1.161 -  */
   1.162 -  if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
   1.163 -    /* printf("DID NOT CLOSE\n"); fflush(stdout); */
   1.164 -    sqlite3_mutex_leave(db->mutex);
   1.165 -    return SQLITE_ERROR;
   1.166 -  }
   1.167 -
   1.168 -  for(j=0; j<db->nDb; j++){
   1.169 -    struct Db *pDb = &db->aDb[j];
   1.170 -    if( pDb->pBt ){
   1.171 -      sqlite3BtreeClose(pDb->pBt);
   1.172 -      pDb->pBt = 0;
   1.173 -      if( j!=1 ){
   1.174 -        pDb->pSchema = 0;
   1.175 -      }
   1.176 -    }
   1.177 -  }
   1.178 -  sqlite3ResetInternalSchema(db, 0);
   1.179 -  assert( db->nDb<=2 );
   1.180 -  assert( db->aDb==db->aDbStatic );
   1.181 -  for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
   1.182 -    FuncDef *pFunc, *pNext;
   1.183 -    for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
   1.184 -      pNext = pFunc->pNext;
   1.185 -      sqlite3_free(pFunc);
   1.186 -    }
   1.187 -  }
   1.188 -
   1.189 -  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
   1.190 -    CollSeq *pColl = (CollSeq *)sqliteHashData(i);
   1.191 -    /* Invoke any destructors registered for collation sequence user data. */
   1.192 -    for(j=0; j<3; j++){
   1.193 -      if( pColl[j].xDel ){
   1.194 -        pColl[j].xDel(pColl[j].pUser);
   1.195 -      }
   1.196 -    }
   1.197 -    sqlite3_free(pColl);
   1.198 -  }
   1.199 -  sqlite3HashClear(&db->aCollSeq);
   1.200 -#ifndef SQLITE_OMIT_VIRTUALTABLE
   1.201 -  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
   1.202 -    Module *pMod = (Module *)sqliteHashData(i);
   1.203 -    if( pMod->xDestroy ){
   1.204 -      pMod->xDestroy(pMod->pAux);
   1.205 -    }
   1.206 -    sqlite3_free(pMod);
   1.207 -  }
   1.208 -  sqlite3HashClear(&db->aModule);
   1.209 -#endif
   1.210 -
   1.211 -  sqlite3HashClear(&db->aFunc);
   1.212 -  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
   1.213 -  if( db->pErr ){
   1.214 -    sqlite3ValueFree(db->pErr);
   1.215 -  }
   1.216 -  sqlite3CloseExtensions(db);
   1.217 -
   1.218 -  db->magic = SQLITE_MAGIC_ERROR;
   1.219 -
   1.220 -  /* The temp-database schema is allocated differently from the other schema
   1.221 -  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
   1.222 -  ** So it needs to be freed here. Todo: Why not roll the temp schema into
   1.223 -  ** the same sqliteMalloc() as the one that allocates the database 
   1.224 -  ** structure?
   1.225 -  */
   1.226 -  sqlite3_free(db->aDb[1].pSchema);
   1.227 -  sqlite3_mutex_leave(db->mutex);
   1.228 -  sqlite3_mutex_free(db->mutex);
   1.229 -  sqlite3_free(db);
   1.230 -  return SQLITE_OK;
   1.231 -}
   1.232 -
   1.233 -/*
   1.234 -** Rollback all database files.
   1.235 -*/
   1.236 -void sqlite3RollbackAll(sqlite3 *db){
   1.237 -  int i;
   1.238 -  int inTrans = 0;
   1.239 -  assert( sqlite3_mutex_held(db->mutex) );
   1.240 -  sqlite3MallocEnterBenignBlock(1);                 /* Enter benign region */
   1.241 -  for(i=0; i<db->nDb; i++){
   1.242 -    if( db->aDb[i].pBt ){
   1.243 -      if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){
   1.244 -        inTrans = 1;
   1.245 -      }
   1.246 -      sqlite3BtreeRollback(db->aDb[i].pBt);
   1.247 -      db->aDb[i].inTrans = 0;
   1.248 -    }
   1.249 -  }
   1.250 -  sqlite3VtabRollback(db);
   1.251 -  sqlite3MallocLeaveBenignBlock();                 /* Leave benign region */
   1.252 -
   1.253 -  if( db->flags&SQLITE_InternChanges ){
   1.254 -    sqlite3ExpirePreparedStatements(db);
   1.255 -    sqlite3ResetInternalSchema(db, 0);
   1.256 -  }
   1.257 -
   1.258 -  /* If one has been configured, invoke the rollback-hook callback */
   1.259 -  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
   1.260 -    db->xRollbackCallback(db->pRollbackArg);
   1.261 -  }
   1.262 -}
   1.263 -
   1.264 -/*
   1.265 -** Return a static string that describes the kind of error specified in the
   1.266 -** argument.
   1.267 -*/
   1.268 -const char *sqlite3ErrStr(int rc){
   1.269 -  const char *z;
   1.270 -  switch( rc & 0xff ){
   1.271 -    case SQLITE_ROW:
   1.272 -    case SQLITE_DONE:
   1.273 -    case SQLITE_OK:         z = "not an error";                          break;
   1.274 -    case SQLITE_ERROR:      z = "SQL logic error or missing database";   break;
   1.275 -    case SQLITE_PERM:       z = "access permission denied";              break;
   1.276 -    case SQLITE_ABORT:      z = "callback requested query abort";        break;
   1.277 -    case SQLITE_BUSY:       z = "database is locked";                    break;
   1.278 -    case SQLITE_LOCKED:     z = "database table is locked";              break;
   1.279 -    case SQLITE_NOMEM:      z = "out of memory";                         break;
   1.280 -    case SQLITE_READONLY:   z = "attempt to write a readonly database";  break;
   1.281 -    case SQLITE_INTERRUPT:  z = "interrupted";                           break;
   1.282 -    case SQLITE_IOERR:      z = "disk I/O error";                        break;
   1.283 -    case SQLITE_CORRUPT:    z = "database disk image is malformed";      break;
   1.284 -    case SQLITE_FULL:       z = "database or disk is full";              break;
   1.285 -    case SQLITE_CANTOPEN:   z = "unable to open database file";          break;
   1.286 -    case SQLITE_EMPTY:      z = "table contains no data";                break;
   1.287 -    case SQLITE_SCHEMA:     z = "database schema has changed";           break;
   1.288 -    case SQLITE_TOOBIG:     z = "String or BLOB exceeded size limit";    break;
   1.289 -    case SQLITE_CONSTRAINT: z = "constraint failed";                     break;
   1.290 -    case SQLITE_MISMATCH:   z = "datatype mismatch";                     break;
   1.291 -    case SQLITE_MISUSE:     z = "library routine called out of sequence";break;
   1.292 -    case SQLITE_NOLFS:      z = "kernel lacks large file support";       break;
   1.293 -    case SQLITE_AUTH:       z = "authorization denied";                  break;
   1.294 -    case SQLITE_FORMAT:     z = "auxiliary database format error";       break;
   1.295 -    case SQLITE_RANGE:      z = "bind or column index out of range";     break;
   1.296 -    case SQLITE_NOTADB:     z = "file is encrypted or is not a database";break;
   1.297 -    default:                z = "unknown error";                         break;
   1.298 -  }
   1.299 -  return z;
   1.300 -}
   1.301 -
   1.302 -/*
   1.303 -** This routine implements a busy callback that sleeps and tries
   1.304 -** again until a timeout value is reached.  The timeout value is
   1.305 -** an integer number of milliseconds passed in as the first
   1.306 -** argument.
   1.307 -*/
   1.308 -static int sqliteDefaultBusyCallback(
   1.309 - void *ptr,               /* Database connection */
   1.310 - int count                /* Number of times table has been busy */
   1.311 -){
   1.312 -#if OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
   1.313 -  static const u8 delays[] =
   1.314 -     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
   1.315 -  static const u8 totals[] =
   1.316 -     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
   1.317 -# define NDELAY (sizeof(delays)/sizeof(delays[0]))
   1.318 -  sqlite3 *db = (sqlite3 *)ptr;
   1.319 -  int timeout = db->busyTimeout;
   1.320 -  int delay, prior;
   1.321 -
   1.322 -  assert( count>=0 );
   1.323 -  if( count < NDELAY ){
   1.324 -    delay = delays[count];
   1.325 -    prior = totals[count];
   1.326 -  }else{
   1.327 -    delay = delays[NDELAY-1];
   1.328 -    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
   1.329 -  }
   1.330 -  if( prior + delay > timeout ){
   1.331 -    delay = timeout - prior;
   1.332 -    if( delay<=0 ) return 0;
   1.333 -  }
   1.334 -  sqlite3OsSleep(db->pVfs, delay*1000);
   1.335 -  return 1;
   1.336 -#else
   1.337 -  sqlite3 *db = (sqlite3 *)ptr;
   1.338 -  int timeout = ((sqlite3 *)ptr)->busyTimeout;
   1.339 -  if( (count+1)*1000 > timeout ){
   1.340 -    return 0;
   1.341 -  }
   1.342 -  sqlite3OsSleep(db->pVfs, 1000000);
   1.343 -  return 1;
   1.344 -#endif
   1.345 -}
   1.346 -
   1.347 -/*
   1.348 -** Invoke the given busy handler.
   1.349 -**
   1.350 -** This routine is called when an operation failed with a lock.
   1.351 -** If this routine returns non-zero, the lock is retried.  If it
   1.352 -** returns 0, the operation aborts with an SQLITE_BUSY error.
   1.353 -*/
   1.354 -int sqlite3InvokeBusyHandler(BusyHandler *p){
   1.355 -  int rc;
   1.356 -  if( p==0 || p->xFunc==0 || p->nBusy<0 ) return 0;
   1.357 -  rc = p->xFunc(p->pArg, p->nBusy);
   1.358 -  if( rc==0 ){
   1.359 -    p->nBusy = -1;
   1.360 -  }else{
   1.361 -    p->nBusy++;
   1.362 -  }
   1.363 -  return rc; 
   1.364 -}
   1.365 -
   1.366 -/*
   1.367 -** This routine sets the busy callback for an Sqlite database to the
   1.368 -** given callback function with the given argument.
   1.369 -*/
   1.370 -int sqlite3_busy_handler(
   1.371 -  sqlite3 *db,
   1.372 -  int (*xBusy)(void*,int),
   1.373 -  void *pArg
   1.374 -){
   1.375 -  if( sqlite3SafetyCheck(db) ){
   1.376 -    return SQLITE_MISUSE;
   1.377 -  }
   1.378 -  sqlite3_mutex_enter(db->mutex);
   1.379 -  db->busyHandler.xFunc = xBusy;
   1.380 -  db->busyHandler.pArg = pArg;
   1.381 -  db->busyHandler.nBusy = 0;
   1.382 -  sqlite3_mutex_leave(db->mutex);
   1.383 -  return SQLITE_OK;
   1.384 -}
   1.385 -
   1.386 -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   1.387 -/*
   1.388 -** This routine sets the progress callback for an Sqlite database to the
   1.389 -** given callback function with the given argument. The progress callback will
   1.390 -** be invoked every nOps opcodes.
   1.391 -*/
   1.392 -void sqlite3_progress_handler(
   1.393 -  sqlite3 *db, 
   1.394 -  int nOps,
   1.395 -  int (*xProgress)(void*), 
   1.396 -  void *pArg
   1.397 -){
   1.398 -  if( !sqlite3SafetyCheck(db) ){
   1.399 -    sqlite3_mutex_enter(db->mutex);
   1.400 -    if( nOps>0 ){
   1.401 -      db->xProgress = xProgress;
   1.402 -      db->nProgressOps = nOps;
   1.403 -      db->pProgressArg = pArg;
   1.404 -    }else{
   1.405 -      db->xProgress = 0;
   1.406 -      db->nProgressOps = 0;
   1.407 -      db->pProgressArg = 0;
   1.408 -    }
   1.409 -    sqlite3_mutex_leave(db->mutex);
   1.410 -  }
   1.411 -}
   1.412 -#endif
   1.413 -
   1.414 -
   1.415 -/*
   1.416 -** This routine installs a default busy handler that waits for the
   1.417 -** specified number of milliseconds before returning 0.
   1.418 -*/
   1.419 -int sqlite3_busy_timeout(sqlite3 *db, int ms){
   1.420 -  if( sqlite3SafetyCheck(db) ){
   1.421 -    return SQLITE_MISUSE;
   1.422 -  }
   1.423 -  if( ms>0 ){
   1.424 -    db->busyTimeout = ms;
   1.425 -    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
   1.426 -  }else{
   1.427 -    sqlite3_busy_handler(db, 0, 0);
   1.428 -  }
   1.429 -  return SQLITE_OK;
   1.430 -}
   1.431 -
   1.432 -/*
   1.433 -** Cause any pending operation to stop at its earliest opportunity.
   1.434 -*/
   1.435 -void sqlite3_interrupt(sqlite3 *db){
   1.436 -  if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){
   1.437 -    db->u1.isInterrupted = 1;
   1.438 -  }
   1.439 -}
   1.440 -
   1.441 -
   1.442 -/*
   1.443 -** This function is exactly the same as sqlite3_create_function(), except
   1.444 -** that it is designed to be called by internal code. The difference is
   1.445 -** that if a malloc() fails in sqlite3_create_function(), an error code
   1.446 -** is returned and the mallocFailed flag cleared. 
   1.447 -*/
   1.448 -int sqlite3CreateFunc(
   1.449 -  sqlite3 *db,
   1.450 -  const char *zFunctionName,
   1.451 -  int nArg,
   1.452 -  int enc,
   1.453 -  void *pUserData,
   1.454 -  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
   1.455 -  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
   1.456 -  void (*xFinal)(sqlite3_context*)
   1.457 -){
   1.458 -  FuncDef *p;
   1.459 -  int nName;
   1.460 -
   1.461 -  assert( sqlite3_mutex_held(db->mutex) );
   1.462 -  if( sqlite3SafetyCheck(db) ){
   1.463 -    return SQLITE_MISUSE;
   1.464 -  }
   1.465 -  if( zFunctionName==0 ||
   1.466 -      (xFunc && (xFinal || xStep)) || 
   1.467 -      (!xFunc && (xFinal && !xStep)) ||
   1.468 -      (!xFunc && (!xFinal && xStep)) ||
   1.469 -      (nArg<-1 || nArg>127) ||
   1.470 -      (255<(nName = strlen(zFunctionName))) ){
   1.471 -    sqlite3Error(db, SQLITE_ERROR, "bad parameters");
   1.472 -    return SQLITE_ERROR;
   1.473 -  }
   1.474 -  
   1.475 -#ifndef SQLITE_OMIT_UTF16
   1.476 -  /* If SQLITE_UTF16 is specified as the encoding type, transform this
   1.477 -  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   1.478 -  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
   1.479 -  **
   1.480 -  ** If SQLITE_ANY is specified, add three versions of the function
   1.481 -  ** to the hash table.
   1.482 -  */
   1.483 -  if( enc==SQLITE_UTF16 ){
   1.484 -    enc = SQLITE_UTF16NATIVE;
   1.485 -  }else if( enc==SQLITE_ANY ){
   1.486 -    int rc;
   1.487 -    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,
   1.488 -         pUserData, xFunc, xStep, xFinal);
   1.489 -    if( rc==SQLITE_OK ){
   1.490 -      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,
   1.491 -          pUserData, xFunc, xStep, xFinal);
   1.492 -    }
   1.493 -    if( rc!=SQLITE_OK ){
   1.494 -      return rc;
   1.495 -    }
   1.496 -    enc = SQLITE_UTF16BE;
   1.497 -  }
   1.498 -#else
   1.499 -  enc = SQLITE_UTF8;
   1.500 -#endif
   1.501 -  
   1.502 -  /* Check if an existing function is being overridden or deleted. If so,
   1.503 -  ** and there are active VMs, then return SQLITE_BUSY. If a function
   1.504 -  ** is being overridden/deleted but there are no active VMs, allow the
   1.505 -  ** operation to continue but invalidate all precompiled statements.
   1.506 -  */
   1.507 -  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);
   1.508 -  if( p && p->iPrefEnc==enc && p->nArg==nArg ){
   1.509 -    if( db->activeVdbeCnt ){
   1.510 -      sqlite3Error(db, SQLITE_BUSY, 
   1.511 -        "Unable to delete/modify user-function due to active statements");
   1.512 -      assert( !db->mallocFailed );
   1.513 -      return SQLITE_BUSY;
   1.514 -    }else{
   1.515 -      sqlite3ExpirePreparedStatements(db);
   1.516 -    }
   1.517 -  }
   1.518 -
   1.519 -  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
   1.520 -  assert(p || db->mallocFailed);
   1.521 -  if( !p ){
   1.522 -    return SQLITE_NOMEM;
   1.523 -  }
   1.524 -  p->flags = 0;
   1.525 -  p->xFunc = xFunc;
   1.526 -  p->xStep = xStep;
   1.527 -  p->xFinalize = xFinal;
   1.528 -  p->pUserData = pUserData;
   1.529 -  p->nArg = nArg;
   1.530 -  return SQLITE_OK;
   1.531 -}
   1.532 -
   1.533 -/*
   1.534 -** Create new user functions.
   1.535 -*/
   1.536 -int sqlite3_create_function(
   1.537 -  sqlite3 *db,
   1.538 -  const char *zFunctionName,
   1.539 -  int nArg,
   1.540 -  int enc,
   1.541 -  void *p,
   1.542 -  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
   1.543 -  void (*xStep)(sqlite3_context*,int,sqlite3_value **),
   1.544 -  void (*xFinal)(sqlite3_context*)
   1.545 -){
   1.546 -  int rc;
   1.547 -  sqlite3_mutex_enter(db->mutex);
   1.548 -  assert( !db->mallocFailed );
   1.549 -  rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);
   1.550 -  rc = sqlite3ApiExit(db, rc);
   1.551 -  sqlite3_mutex_leave(db->mutex);
   1.552 -  return rc;
   1.553 -}
   1.554 -
   1.555 -#ifndef SQLITE_OMIT_UTF16
   1.556 -int sqlite3_create_function16(
   1.557 -  sqlite3 *db,
   1.558 -  const void *zFunctionName,
   1.559 -  int nArg,
   1.560 -  int eTextRep,
   1.561 -  void *p,
   1.562 -  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
   1.563 -  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
   1.564 -  void (*xFinal)(sqlite3_context*)
   1.565 -){
   1.566 -  int rc;
   1.567 -  char *zFunc8;
   1.568 -  sqlite3_mutex_enter(db->mutex);
   1.569 -  assert( !db->mallocFailed );
   1.570 -  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);
   1.571 -  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);
   1.572 -  sqlite3_free(zFunc8);
   1.573 -  rc = sqlite3ApiExit(db, rc);
   1.574 -  sqlite3_mutex_leave(db->mutex);
   1.575 -  return rc;
   1.576 -}
   1.577 -#endif
   1.578 -
   1.579 -
   1.580 -/*
   1.581 -** Declare that a function has been overloaded by a virtual table.
   1.582 -**
   1.583 -** If the function already exists as a regular global function, then
   1.584 -** this routine is a no-op.  If the function does not exist, then create
   1.585 -** a new one that always throws a run-time error.  
   1.586 -**
   1.587 -** When virtual tables intend to provide an overloaded function, they
   1.588 -** should call this routine to make sure the global function exists.
   1.589 -** A global function must exist in order for name resolution to work
   1.590 -** properly.
   1.591 -*/
   1.592 -int sqlite3_overload_function(
   1.593 -  sqlite3 *db,
   1.594 -  const char *zName,
   1.595 -  int nArg
   1.596 -){
   1.597 -  int nName = strlen(zName);
   1.598 -  int rc;
   1.599 -  sqlite3_mutex_enter(db->mutex);
   1.600 -  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
   1.601 -    sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
   1.602 -                      0, sqlite3InvalidFunction, 0, 0);
   1.603 -  }
   1.604 -  rc = sqlite3ApiExit(db, SQLITE_OK);
   1.605 -  sqlite3_mutex_leave(db->mutex);
   1.606 -  return rc;
   1.607 -}
   1.608 -
   1.609 -#ifndef SQLITE_OMIT_TRACE
   1.610 -/*
   1.611 -** Register a trace function.  The pArg from the previously registered trace
   1.612 -** is returned.  
   1.613 -**
   1.614 -** A NULL trace function means that no tracing is executes.  A non-NULL
   1.615 -** trace is a pointer to a function that is invoked at the start of each
   1.616 -** SQL statement.
   1.617 -*/
   1.618 -void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
   1.619 -  void *pOld;
   1.620 -  sqlite3_mutex_enter(db->mutex);
   1.621 -  pOld = db->pTraceArg;
   1.622 -  db->xTrace = xTrace;
   1.623 -  db->pTraceArg = pArg;
   1.624 -  sqlite3_mutex_leave(db->mutex);
   1.625 -  return pOld;
   1.626 -}
   1.627 -/*
   1.628 -** Register a profile function.  The pArg from the previously registered 
   1.629 -** profile function is returned.  
   1.630 -**
   1.631 -** A NULL profile function means that no profiling is executes.  A non-NULL
   1.632 -** profile is a pointer to a function that is invoked at the conclusion of
   1.633 -** each SQL statement that is run.
   1.634 -*/
   1.635 -void *sqlite3_profile(
   1.636 -  sqlite3 *db,
   1.637 -  void (*xProfile)(void*,const char*,sqlite_uint64),
   1.638 -  void *pArg
   1.639 -){
   1.640 -  void *pOld;
   1.641 -  sqlite3_mutex_enter(db->mutex);
   1.642 -  pOld = db->pProfileArg;
   1.643 -  db->xProfile = xProfile;
   1.644 -  db->pProfileArg = pArg;
   1.645 -  sqlite3_mutex_leave(db->mutex);
   1.646 -  return pOld;
   1.647 -}
   1.648 -#endif /* SQLITE_OMIT_TRACE */
   1.649 -
   1.650 -/*** EXPERIMENTAL ***
   1.651 -**
   1.652 -** Register a function to be invoked when a transaction comments.
   1.653 -** If the invoked function returns non-zero, then the commit becomes a
   1.654 -** rollback.
   1.655 -*/
   1.656 -void *sqlite3_commit_hook(
   1.657 -  sqlite3 *db,              /* Attach the hook to this database */
   1.658 -  int (*xCallback)(void*),  /* Function to invoke on each commit */
   1.659 -  void *pArg                /* Argument to the function */
   1.660 -){
   1.661 -  void *pOld;
   1.662 -  sqlite3_mutex_enter(db->mutex);
   1.663 -  pOld = db->pCommitArg;
   1.664 -  db->xCommitCallback = xCallback;
   1.665 -  db->pCommitArg = pArg;
   1.666 -  sqlite3_mutex_leave(db->mutex);
   1.667 -  return pOld;
   1.668 -}
   1.669 -
   1.670 -/*
   1.671 -** Register a callback to be invoked each time a row is updated,
   1.672 -** inserted or deleted using this database connection.
   1.673 -*/
   1.674 -void *sqlite3_update_hook(
   1.675 -  sqlite3 *db,              /* Attach the hook to this database */
   1.676 -  void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
   1.677 -  void *pArg                /* Argument to the function */
   1.678 -){
   1.679 -  void *pRet;
   1.680 -  sqlite3_mutex_enter(db->mutex);
   1.681 -  pRet = db->pUpdateArg;
   1.682 -  db->xUpdateCallback = xCallback;
   1.683 -  db->pUpdateArg = pArg;
   1.684 -  sqlite3_mutex_leave(db->mutex);
   1.685 -  return pRet;
   1.686 -}
   1.687 -
   1.688 -/*
   1.689 -** Register a callback to be invoked each time a transaction is rolled
   1.690 -** back by this database connection.
   1.691 -*/
   1.692 -void *sqlite3_rollback_hook(
   1.693 -  sqlite3 *db,              /* Attach the hook to this database */
   1.694 -  void (*xCallback)(void*), /* Callback function */
   1.695 -  void *pArg                /* Argument to the function */
   1.696 -){
   1.697 -  void *pRet;
   1.698 -  sqlite3_mutex_enter(db->mutex);
   1.699 -  pRet = db->pRollbackArg;
   1.700 -  db->xRollbackCallback = xCallback;
   1.701 -  db->pRollbackArg = pArg;
   1.702 -  sqlite3_mutex_leave(db->mutex);
   1.703 -  return pRet;
   1.704 -}
   1.705 -
   1.706 -/*
   1.707 -** This routine is called to create a connection to a database BTree
   1.708 -** driver.  If zFilename is the name of a file, then that file is
   1.709 -** opened and used.  If zFilename is the magic name ":memory:" then
   1.710 -** the database is stored in memory (and is thus forgotten as soon as
   1.711 -** the connection is closed.)  If zFilename is NULL then the database
   1.712 -** is a "virtual" database for transient use only and is deleted as
   1.713 -** soon as the connection is closed.
   1.714 -**
   1.715 -** A virtual database can be either a disk file (that is automatically
   1.716 -** deleted when the file is closed) or it an be held entirely in memory,
   1.717 -** depending on the values of the TEMP_STORE compile-time macro and the
   1.718 -** db->temp_store variable, according to the following chart:
   1.719 -**
   1.720 -**       TEMP_STORE     db->temp_store     Location of temporary database
   1.721 -**       ----------     --------------     ------------------------------
   1.722 -**           0               any             file
   1.723 -**           1                1              file
   1.724 -**           1                2              memory
   1.725 -**           1                0              file
   1.726 -**           2                1              file
   1.727 -**           2                2              memory
   1.728 -**           2                0              memory
   1.729 -**           3               any             memory
   1.730 -*/
   1.731 -int sqlite3BtreeFactory(
   1.732 -  const sqlite3 *db,        /* Main database when opening aux otherwise 0 */
   1.733 -  const char *zFilename,    /* Name of the file containing the BTree database */
   1.734 -  int omitJournal,          /* if TRUE then do not journal this file */
   1.735 -  int nCache,               /* How many pages in the page cache */
   1.736 -  int vfsFlags,             /* Flags passed through to vfsOpen */
   1.737 -  Btree **ppBtree           /* Pointer to new Btree object written here */
   1.738 -){
   1.739 -  int btFlags = 0;
   1.740 -  int rc;
   1.741 -  
   1.742 -  assert( sqlite3_mutex_held(db->mutex) );
   1.743 -  assert( ppBtree != 0);
   1.744 -  if( omitJournal ){
   1.745 -    btFlags |= BTREE_OMIT_JOURNAL;
   1.746 -  }
   1.747 -  if( db->flags & SQLITE_NoReadlock ){
   1.748 -    btFlags |= BTREE_NO_READLOCK;
   1.749 -  }
   1.750 -  if( zFilename==0 ){
   1.751 -#if TEMP_STORE==0
   1.752 -    /* Do nothing */
   1.753 -#endif
   1.754 -#ifndef SQLITE_OMIT_MEMORYDB
   1.755 -#if TEMP_STORE==1
   1.756 -    if( db->temp_store==2 ) zFilename = ":memory:";
   1.757 -#endif
   1.758 -#if TEMP_STORE==2
   1.759 -    if( db->temp_store!=1 ) zFilename = ":memory:";
   1.760 -#endif
   1.761 -#if TEMP_STORE==3
   1.762 -    zFilename = ":memory:";
   1.763 -#endif
   1.764 -#endif /* SQLITE_OMIT_MEMORYDB */
   1.765 -  }
   1.766 -
   1.767 -  if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (zFilename==0 || *zFilename==0) ){
   1.768 -    vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
   1.769 -  }
   1.770 -  rc = sqlite3BtreeOpen(zFilename, (sqlite3 *)db, ppBtree, btFlags, vfsFlags);
   1.771 -  if( rc==SQLITE_OK ){
   1.772 -    sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);
   1.773 -    sqlite3BtreeSetCacheSize(*ppBtree, nCache);
   1.774 -  }
   1.775 -  return rc;
   1.776 -}
   1.777 -
   1.778 -/*
   1.779 -** Return UTF-8 encoded English language explanation of the most recent
   1.780 -** error.
   1.781 -*/
   1.782 -const char *sqlite3_errmsg(sqlite3 *db){
   1.783 -  const char *z;
   1.784 -  if( !db ){
   1.785 -    return sqlite3ErrStr(SQLITE_NOMEM);
   1.786 -  }
   1.787 -  if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   1.788 -    return sqlite3ErrStr(SQLITE_MISUSE);
   1.789 -  }
   1.790 -  sqlite3_mutex_enter(db->mutex);
   1.791 -  assert( !db->mallocFailed );
   1.792 -  z = (char*)sqlite3_value_text(db->pErr);
   1.793 -  if( z==0 ){
   1.794 -    z = sqlite3ErrStr(db->errCode);
   1.795 -  }
   1.796 -  sqlite3_mutex_leave(db->mutex);
   1.797 -  return z;
   1.798 -}
   1.799 -
   1.800 -#ifndef SQLITE_OMIT_UTF16
   1.801 -/*
   1.802 -** Return UTF-16 encoded English language explanation of the most recent
   1.803 -** error.
   1.804 -*/
   1.805 -const void *sqlite3_errmsg16(sqlite3 *db){
   1.806 -  /* Because all the characters in the string are in the unicode
   1.807 -  ** range 0x00-0xFF, if we pad the big-endian string with a 
   1.808 -  ** zero byte, we can obtain the little-endian string with
   1.809 -  ** &big_endian[1].
   1.810 -  */
   1.811 -  static const char outOfMemBe[] = {
   1.812 -    0, 'o', 0, 'u', 0, 't', 0, ' ', 
   1.813 -    0, 'o', 0, 'f', 0, ' ', 
   1.814 -    0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
   1.815 -  };
   1.816 -  static const char misuseBe [] = {
   1.817 -    0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ', 
   1.818 -    0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 
   1.819 -    0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ', 
   1.820 -    0, 'o', 0, 'u', 0, 't', 0, ' ', 
   1.821 -    0, 'o', 0, 'f', 0, ' ', 
   1.822 -    0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
   1.823 -  };
   1.824 -
   1.825 -  const void *z;
   1.826 -  if( !db ){
   1.827 -    return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   1.828 -  }
   1.829 -  if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){
   1.830 -    return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
   1.831 -  }
   1.832 -  sqlite3_mutex_enter(db->mutex);
   1.833 -  assert( !db->mallocFailed );
   1.834 -  z = sqlite3_value_text16(db->pErr);
   1.835 -  if( z==0 ){
   1.836 -    sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
   1.837 -         SQLITE_UTF8, SQLITE_STATIC);
   1.838 -    z = sqlite3_value_text16(db->pErr);
   1.839 -  }
   1.840 -  sqlite3ApiExit(0, 0);
   1.841 -  sqlite3_mutex_leave(db->mutex);
   1.842 -  return z;
   1.843 -}
   1.844 -#endif /* SQLITE_OMIT_UTF16 */
   1.845 -
   1.846 -/*
   1.847 -** Return the most recent error code generated by an SQLite routine. If NULL is
   1.848 -** passed to this function, we assume a malloc() failed during sqlite3_open().
   1.849 -*/
   1.850 -int sqlite3_errcode(sqlite3 *db){
   1.851 -  if( !db || db->mallocFailed ){
   1.852 -    return SQLITE_NOMEM;
   1.853 -  }
   1.854 -  if( sqlite3SafetyCheck(db) ){
   1.855 -    return SQLITE_MISUSE;
   1.856 -  }
   1.857 -  return db->errCode & db->errMask;
   1.858 -}
   1.859 -
   1.860 -/*
   1.861 -** Create a new collating function for database "db".  The name is zName
   1.862 -** and the encoding is enc.
   1.863 -*/
   1.864 -static int createCollation(
   1.865 -  sqlite3* db, 
   1.866 -  const char *zName, 
   1.867 -  int enc, 
   1.868 -  void* pCtx,
   1.869 -  int(*xCompare)(void*,int,const void*,int,const void*),
   1.870 -  void(*xDel)(void*)
   1.871 -){
   1.872 -  CollSeq *pColl;
   1.873 -  int enc2;
   1.874 -  
   1.875 -  if( sqlite3SafetyCheck(db) ){
   1.876 -    return SQLITE_MISUSE;
   1.877 -  }
   1.878 -  assert( sqlite3_mutex_held(db->mutex) );
   1.879 -
   1.880 -  /* If SQLITE_UTF16 is specified as the encoding type, transform this
   1.881 -  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
   1.882 -  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
   1.883 -  */
   1.884 -  enc2 = enc & ~SQLITE_UTF16_ALIGNED;
   1.885 -  if( enc2==SQLITE_UTF16 ){
   1.886 -    enc2 = SQLITE_UTF16NATIVE;
   1.887 -  }
   1.888 -
   1.889 -  if( (enc2&~3)!=0 ){
   1.890 -    sqlite3Error(db, SQLITE_ERROR, "unknown encoding");
   1.891 -    return SQLITE_ERROR;
   1.892 -  }
   1.893 -
   1.894 -  /* Check if this call is removing or replacing an existing collation 
   1.895 -  ** sequence. If so, and there are active VMs, return busy. If there
   1.896 -  ** are no active VMs, invalidate any pre-compiled statements.
   1.897 -  */
   1.898 -  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 0);
   1.899 -  if( pColl && pColl->xCmp ){
   1.900 -    if( db->activeVdbeCnt ){
   1.901 -      sqlite3Error(db, SQLITE_BUSY, 
   1.902 -        "Unable to delete/modify collation sequence due to active statements");
   1.903 -      return SQLITE_BUSY;
   1.904 -    }
   1.905 -    sqlite3ExpirePreparedStatements(db);
   1.906 -
   1.907 -    /* If collation sequence pColl was created directly by a call to
   1.908 -    ** sqlite3_create_collation, and not generated by synthCollSeq(),
   1.909 -    ** then any copies made by synthCollSeq() need to be invalidated.
   1.910 -    ** Also, collation destructor - CollSeq.xDel() - function may need
   1.911 -    ** to be called.
   1.912 -    */ 
   1.913 -    if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
   1.914 -      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, strlen(zName));
   1.915 -      int j;
   1.916 -      for(j=0; j<3; j++){
   1.917 -        CollSeq *p = &aColl[j];
   1.918 -        if( p->enc==pColl->enc ){
   1.919 -          if( p->xDel ){
   1.920 -            p->xDel(p->pUser);
   1.921 -          }
   1.922 -          p->xCmp = 0;
   1.923 -        }
   1.924 -      }
   1.925 -    }
   1.926 -  }
   1.927 -
   1.928 -  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1);
   1.929 -  if( pColl ){
   1.930 -    pColl->xCmp = xCompare;
   1.931 -    pColl->pUser = pCtx;
   1.932 -    pColl->xDel = xDel;
   1.933 -    pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED);
   1.934 -  }
   1.935 -  sqlite3Error(db, SQLITE_OK, 0);
   1.936 -  return SQLITE_OK;
   1.937 -}
   1.938 -
   1.939 -
   1.940 -/*
   1.941 -** This routine does the work of opening a database on behalf of
   1.942 -** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  
   1.943 -** is UTF-8 encoded.
   1.944 -*/
   1.945 -static int openDatabase(
   1.946 -  const char *zFilename, /* Database filename UTF-8 encoded */
   1.947 -  sqlite3 **ppDb,        /* OUT: Returned database handle */
   1.948 -  unsigned flags,        /* Operational flags */
   1.949 -  const char *zVfs       /* Name of the VFS to use */
   1.950 -){
   1.951 -  sqlite3 *db;
   1.952 -  int rc;
   1.953 -  CollSeq *pColl;
   1.954 -
   1.955 -  /* Allocate the sqlite data structure */
   1.956 -  db = sqlite3MallocZero( sizeof(sqlite3) );
   1.957 -  if( db==0 ) goto opendb_out;
   1.958 -  db->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
   1.959 -  if( db->mutex==0 ){
   1.960 -    sqlite3_free(db);
   1.961 -    db = 0;
   1.962 -    goto opendb_out;
   1.963 -  }
   1.964 -  sqlite3_mutex_enter(db->mutex);
   1.965 -  db->errMask = 0xff;
   1.966 -  db->priorNewRowid = 0;
   1.967 -  db->nDb = 2;
   1.968 -  db->magic = SQLITE_MAGIC_BUSY;
   1.969 -  db->aDb = db->aDbStatic;
   1.970 -  db->autoCommit = 1;
   1.971 -  db->flags |= SQLITE_ShortColNames
   1.972 -#if SQLITE_DEFAULT_FILE_FORMAT<4
   1.973 -                 | SQLITE_LegacyFileFmt
   1.974 -#endif
   1.975 -#ifdef SQLITE_ENABLE_LOAD_EXTENSION
   1.976 -                 | SQLITE_LoadExtension
   1.977 -#endif
   1.978 -      ;
   1.979 -  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
   1.980 -  sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
   1.981 -#ifndef SQLITE_OMIT_VIRTUALTABLE
   1.982 -  sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0);
   1.983 -#endif
   1.984 -
   1.985 -  db->pVfs = sqlite3_vfs_find(zVfs);
   1.986 -  if( !db->pVfs ){
   1.987 -    rc = SQLITE_ERROR;
   1.988 -    db->magic = SQLITE_MAGIC_CLOSED;
   1.989 -    sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)"));
   1.990 -    goto opendb_out;
   1.991 -  }
   1.992 -
   1.993 -  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
   1.994 -  ** and UTF-16, so add a version for each to avoid any unnecessary
   1.995 -  ** conversions. The only error that can occur here is a malloc() failure.
   1.996 -  */
   1.997 -  if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) ||
   1.998 -      createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) ||
   1.999 -      createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) ||
  1.1000 -      (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 
  1.1001 -  ){
  1.1002 -    assert( db->mallocFailed );
  1.1003 -    db->magic = SQLITE_MAGIC_CLOSED;
  1.1004 -    goto opendb_out;
  1.1005 -  }
  1.1006 -
  1.1007 -  /* Also add a UTF-8 case-insensitive collation sequence. */
  1.1008 -  createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
  1.1009 -
  1.1010 -  /* Set flags on the built-in collating sequences */
  1.1011 -  db->pDfltColl->type = SQLITE_COLL_BINARY;
  1.1012 -  pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);
  1.1013 -  if( pColl ){
  1.1014 -    pColl->type = SQLITE_COLL_NOCASE;
  1.1015 -  }
  1.1016 -
  1.1017 -  /* Open the backend database driver */
  1.1018 -  db->openFlags = flags;
  1.1019 -  rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE, 
  1.1020 -                           flags | SQLITE_OPEN_MAIN_DB,
  1.1021 -                           &db->aDb[0].pBt);
  1.1022 -  if( rc!=SQLITE_OK ){
  1.1023 -    sqlite3Error(db, rc, 0);
  1.1024 -    db->magic = SQLITE_MAGIC_CLOSED;
  1.1025 -    goto opendb_out;
  1.1026 -  }
  1.1027 -  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
  1.1028 -  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
  1.1029 -
  1.1030 -
  1.1031 -  /* The default safety_level for the main database is 'full'; for the temp
  1.1032 -  ** database it is 'NONE'. This matches the pager layer defaults.  
  1.1033 -  */
  1.1034 -  db->aDb[0].zName = "main";
  1.1035 -  db->aDb[0].safety_level = 3;
  1.1036 -#ifndef SQLITE_OMIT_TEMPDB
  1.1037 -  db->aDb[1].zName = "temp";
  1.1038 -  db->aDb[1].safety_level = 1;
  1.1039 -#endif
  1.1040 -
  1.1041 -  db->magic = SQLITE_MAGIC_OPEN;
  1.1042 -  if( db->mallocFailed ){
  1.1043 -    goto opendb_out;
  1.1044 -  }
  1.1045 -
  1.1046 -  /* Register all built-in functions, but do not attempt to read the
  1.1047 -  ** database schema yet. This is delayed until the first time the database
  1.1048 -  ** is accessed.
  1.1049 -  */
  1.1050 -  sqlite3Error(db, SQLITE_OK, 0);
  1.1051 -  sqlite3RegisterBuiltinFunctions(db);
  1.1052 -
  1.1053 -  /* Load automatic extensions - extensions that have been registered
  1.1054 -  ** using the sqlite3_automatic_extension() API.
  1.1055 -  */
  1.1056 -  (void)sqlite3AutoLoadExtensions(db);
  1.1057 -  if( sqlite3_errcode(db)!=SQLITE_OK ){
  1.1058 -    goto opendb_out;
  1.1059 -  }
  1.1060 -
  1.1061 -#ifdef SQLITE_ENABLE_FTS1
  1.1062 -  if( !db->mallocFailed ){
  1.1063 -    extern int sqlite3Fts1Init(sqlite3*);
  1.1064 -    rc = sqlite3Fts1Init(db);
  1.1065 -  }
  1.1066 -#endif
  1.1067 -
  1.1068 -#ifdef SQLITE_ENABLE_FTS2
  1.1069 -  if( !db->mallocFailed && rc==SQLITE_OK ){
  1.1070 -    extern int sqlite3Fts2Init(sqlite3*);
  1.1071 -    rc = sqlite3Fts2Init(db);
  1.1072 -  }
  1.1073 -#endif
  1.1074 -
  1.1075 -#ifdef SQLITE_ENABLE_FTS3
  1.1076 -  if( !db->mallocFailed && rc==SQLITE_OK ){
  1.1077 -    extern int sqlite3Fts3Init(sqlite3*);
  1.1078 -    rc = sqlite3Fts3Init(db);
  1.1079 -  }
  1.1080 -#endif
  1.1081 -
  1.1082 -#ifdef SQLITE_ENABLE_ICU
  1.1083 -  if( !db->mallocFailed && rc==SQLITE_OK ){
  1.1084 -    extern int sqlite3IcuInit(sqlite3*);
  1.1085 -    rc = sqlite3IcuInit(db);
  1.1086 -  }
  1.1087 -#endif
  1.1088 -  sqlite3Error(db, rc, 0);
  1.1089 -
  1.1090 -  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  1.1091 -  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  1.1092 -  ** mode.  Doing nothing at all also makes NORMAL the default.
  1.1093 -  */
  1.1094 -#ifdef SQLITE_DEFAULT_LOCKING_MODE
  1.1095 -  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
  1.1096 -  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
  1.1097 -                          SQLITE_DEFAULT_LOCKING_MODE);
  1.1098 -#endif
  1.1099 -
  1.1100 -opendb_out:
  1.1101 -  if( db && db->mutex ){
  1.1102 -    sqlite3_mutex_leave(db->mutex);
  1.1103 -  }
  1.1104 -  if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){
  1.1105 -    sqlite3_close(db);
  1.1106 -    db = 0;
  1.1107 -  }
  1.1108 -  *ppDb = db;
  1.1109 -  return sqlite3ApiExit(0, rc);
  1.1110 -}
  1.1111 -
  1.1112 -/*
  1.1113 -** Open a new database handle.
  1.1114 -*/
  1.1115 -int sqlite3_open(
  1.1116 -  const char *zFilename, 
  1.1117 -  sqlite3 **ppDb 
  1.1118 -){
  1.1119 -  return openDatabase(zFilename, ppDb,
  1.1120 -                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
  1.1121 -}
  1.1122 -int sqlite3_open_v2(
  1.1123 -  const char *filename,   /* Database filename (UTF-8) */
  1.1124 -  sqlite3 **ppDb,         /* OUT: SQLite db handle */
  1.1125 -  int flags,              /* Flags */
  1.1126 -  const char *zVfs        /* Name of VFS module to use */
  1.1127 -){
  1.1128 -  return openDatabase(filename, ppDb, flags, zVfs);
  1.1129 -}
  1.1130 -
  1.1131 -#ifndef SQLITE_OMIT_UTF16
  1.1132 -/*
  1.1133 -** Open a new database handle.
  1.1134 -*/
  1.1135 -int sqlite3_open16(
  1.1136 -  const void *zFilename, 
  1.1137 -  sqlite3 **ppDb
  1.1138 -){
  1.1139 -  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
  1.1140 -  sqlite3_value *pVal;
  1.1141 -  int rc = SQLITE_NOMEM;
  1.1142 -
  1.1143 -  assert( zFilename );
  1.1144 -  assert( ppDb );
  1.1145 -  *ppDb = 0;
  1.1146 -  pVal = sqlite3ValueNew(0);
  1.1147 -  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
  1.1148 -  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
  1.1149 -  if( zFilename8 ){
  1.1150 -    rc = openDatabase(zFilename8, ppDb,
  1.1151 -                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
  1.1152 -    if( rc==SQLITE_OK && *ppDb ){
  1.1153 -      rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
  1.1154 -      if( rc!=SQLITE_OK ){
  1.1155 -        sqlite3_close(*ppDb);
  1.1156 -        *ppDb = 0;
  1.1157 -      }
  1.1158 -    }
  1.1159 -  }
  1.1160 -  sqlite3ValueFree(pVal);
  1.1161 -
  1.1162 -  return sqlite3ApiExit(0, rc);
  1.1163 -}
  1.1164 -#endif /* SQLITE_OMIT_UTF16 */
  1.1165 -
  1.1166 -/*
  1.1167 -** Register a new collation sequence with the database handle db.
  1.1168 -*/
  1.1169 -int sqlite3_create_collation(
  1.1170 -  sqlite3* db, 
  1.1171 -  const char *zName, 
  1.1172 -  int enc, 
  1.1173 -  void* pCtx,
  1.1174 -  int(*xCompare)(void*,int,const void*,int,const void*)
  1.1175 -){
  1.1176 -  int rc;
  1.1177 -  sqlite3_mutex_enter(db->mutex);
  1.1178 -  assert( !db->mallocFailed );
  1.1179 -  rc = createCollation(db, zName, enc, pCtx, xCompare, 0);
  1.1180 -  rc = sqlite3ApiExit(db, rc);
  1.1181 -  sqlite3_mutex_leave(db->mutex);
  1.1182 -  return rc;
  1.1183 -}
  1.1184 -
  1.1185 -/*
  1.1186 -** Register a new collation sequence with the database handle db.
  1.1187 -*/
  1.1188 -int sqlite3_create_collation_v2(
  1.1189 -  sqlite3* db, 
  1.1190 -  const char *zName, 
  1.1191 -  int enc, 
  1.1192 -  void* pCtx,
  1.1193 -  int(*xCompare)(void*,int,const void*,int,const void*),
  1.1194 -  void(*xDel)(void*)
  1.1195 -){
  1.1196 -  int rc;
  1.1197 -  sqlite3_mutex_enter(db->mutex);
  1.1198 -  assert( !db->mallocFailed );
  1.1199 -  rc = createCollation(db, zName, enc, pCtx, xCompare, xDel);
  1.1200 -  rc = sqlite3ApiExit(db, rc);
  1.1201 -  sqlite3_mutex_leave(db->mutex);
  1.1202 -  return rc;
  1.1203 -}
  1.1204 -
  1.1205 -#ifndef SQLITE_OMIT_UTF16
  1.1206 -/*
  1.1207 -** Register a new collation sequence with the database handle db.
  1.1208 -*/
  1.1209 -int sqlite3_create_collation16(
  1.1210 -  sqlite3* db, 
  1.1211 -  const char *zName, 
  1.1212 -  int enc, 
  1.1213 -  void* pCtx,
  1.1214 -  int(*xCompare)(void*,int,const void*,int,const void*)
  1.1215 -){
  1.1216 -  int rc = SQLITE_OK;
  1.1217 -  char *zName8; 
  1.1218 -  sqlite3_mutex_enter(db->mutex);
  1.1219 -  assert( !db->mallocFailed );
  1.1220 -  zName8 = sqlite3Utf16to8(db, zName, -1);
  1.1221 -  if( zName8 ){
  1.1222 -    rc = createCollation(db, zName8, enc, pCtx, xCompare, 0);
  1.1223 -    sqlite3_free(zName8);
  1.1224 -  }
  1.1225 -  rc = sqlite3ApiExit(db, rc);
  1.1226 -  sqlite3_mutex_leave(db->mutex);
  1.1227 -  return rc;
  1.1228 -}
  1.1229 -#endif /* SQLITE_OMIT_UTF16 */
  1.1230 -
  1.1231 -/*
  1.1232 -** Register a collation sequence factory callback with the database handle
  1.1233 -** db. Replace any previously installed collation sequence factory.
  1.1234 -*/
  1.1235 -int sqlite3_collation_needed(
  1.1236 -  sqlite3 *db, 
  1.1237 -  void *pCollNeededArg, 
  1.1238 -  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
  1.1239 -){
  1.1240 -  if( sqlite3SafetyCheck(db) ){
  1.1241 -    return SQLITE_MISUSE;
  1.1242 -  }
  1.1243 -  sqlite3_mutex_enter(db->mutex);
  1.1244 -  db->xCollNeeded = xCollNeeded;
  1.1245 -  db->xCollNeeded16 = 0;
  1.1246 -  db->pCollNeededArg = pCollNeededArg;
  1.1247 -  sqlite3_mutex_leave(db->mutex);
  1.1248 -  return SQLITE_OK;
  1.1249 -}
  1.1250 -
  1.1251 -#ifndef SQLITE_OMIT_UTF16
  1.1252 -/*
  1.1253 -** Register a collation sequence factory callback with the database handle
  1.1254 -** db. Replace any previously installed collation sequence factory.
  1.1255 -*/
  1.1256 -int sqlite3_collation_needed16(
  1.1257 -  sqlite3 *db, 
  1.1258 -  void *pCollNeededArg, 
  1.1259 -  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
  1.1260 -){
  1.1261 -  if( sqlite3SafetyCheck(db) ){
  1.1262 -    return SQLITE_MISUSE;
  1.1263 -  }
  1.1264 -  sqlite3_mutex_enter(db->mutex);
  1.1265 -  db->xCollNeeded = 0;
  1.1266 -  db->xCollNeeded16 = xCollNeeded16;
  1.1267 -  db->pCollNeededArg = pCollNeededArg;
  1.1268 -  sqlite3_mutex_leave(db->mutex);
  1.1269 -  return SQLITE_OK;
  1.1270 -}
  1.1271 -#endif /* SQLITE_OMIT_UTF16 */
  1.1272 -
  1.1273 -#ifndef SQLITE_OMIT_GLOBALRECOVER
  1.1274 -/*
  1.1275 -** This function is now an anachronism. It used to be used to recover from a
  1.1276 -** malloc() failure, but SQLite now does this automatically.
  1.1277 -*/
  1.1278 -int sqlite3_global_recover(){
  1.1279 -  return SQLITE_OK;
  1.1280 -}
  1.1281 -#endif
  1.1282 -
  1.1283 -/*
  1.1284 -** Test to see whether or not the database connection is in autocommit
  1.1285 -** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
  1.1286 -** by default.  Autocommit is disabled by a BEGIN statement and reenabled
  1.1287 -** by the next COMMIT or ROLLBACK.
  1.1288 -**
  1.1289 -******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
  1.1290 -*/
  1.1291 -int sqlite3_get_autocommit(sqlite3 *db){
  1.1292 -  return db->autoCommit;
  1.1293 -}
  1.1294 -
  1.1295 -#ifdef SQLITE_DEBUG
  1.1296 -/*
  1.1297 -** The following routine is subtituted for constant SQLITE_CORRUPT in
  1.1298 -** debugging builds.  This provides a way to set a breakpoint for when
  1.1299 -** corruption is first detected.
  1.1300 -*/
  1.1301 -int sqlite3Corrupt(void){
  1.1302 -  return SQLITE_CORRUPT;
  1.1303 -}
  1.1304 -#endif
  1.1305 -
  1.1306 -/*
  1.1307 -** This is a convenience routine that makes sure that all thread-specific
  1.1308 -** data for this thread has been deallocated.
  1.1309 -**
  1.1310 -** SQLite no longer uses thread-specific data so this routine is now a
  1.1311 -** no-op.  It is retained for historical compatibility.
  1.1312 -*/
  1.1313 -void sqlite3_thread_cleanup(void){
  1.1314 -}
  1.1315 -
  1.1316 -/*
  1.1317 -** Return meta information about a specific column of a database table.
  1.1318 -** See comment in sqlite3.h (sqlite.h.in) for details.
  1.1319 -*/
  1.1320 -#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1.1321 -int sqlite3_table_column_metadata(
  1.1322 -  sqlite3 *db,                /* Connection handle */
  1.1323 -  const char *zDbName,        /* Database name or NULL */
  1.1324 -  const char *zTableName,     /* Table name */
  1.1325 -  const char *zColumnName,    /* Column name */
  1.1326 -  char const **pzDataType,    /* OUTPUT: Declared data type */
  1.1327 -  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
  1.1328 -  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
  1.1329 -  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
  1.1330 -  int *pAutoinc               /* OUTPUT: True if colums is auto-increment */
  1.1331 -){
  1.1332 -  int rc;
  1.1333 -  char *zErrMsg = 0;
  1.1334 -  Table *pTab = 0;
  1.1335 -  Column *pCol = 0;
  1.1336 -  int iCol;
  1.1337 -
  1.1338 -  char const *zDataType = 0;
  1.1339 -  char const *zCollSeq = 0;
  1.1340 -  int notnull = 0;
  1.1341 -  int primarykey = 0;
  1.1342 -  int autoinc = 0;
  1.1343 -
  1.1344 -  /* Ensure the database schema has been loaded */
  1.1345 -  if( sqlite3SafetyOn(db) ){
  1.1346 -    return SQLITE_MISUSE;
  1.1347 -  }
  1.1348 -  sqlite3_mutex_enter(db->mutex);
  1.1349 -  rc = sqlite3Init(db, &zErrMsg);
  1.1350 -  if( SQLITE_OK!=rc ){
  1.1351 -    goto error_out;
  1.1352 -  }
  1.1353 -
  1.1354 -  /* Locate the table in question */
  1.1355 -  pTab = sqlite3FindTable(db, zTableName, zDbName);
  1.1356 -  if( !pTab || pTab->pSelect ){
  1.1357 -    pTab = 0;
  1.1358 -    goto error_out;
  1.1359 -  }
  1.1360 -
  1.1361 -  /* Find the column for which info is requested */
  1.1362 -  if( sqlite3IsRowid(zColumnName) ){
  1.1363 -    iCol = pTab->iPKey;
  1.1364 -    if( iCol>=0 ){
  1.1365 -      pCol = &pTab->aCol[iCol];
  1.1366 -    }
  1.1367 -  }else{
  1.1368 -    for(iCol=0; iCol<pTab->nCol; iCol++){
  1.1369 -      pCol = &pTab->aCol[iCol];
  1.1370 -      if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
  1.1371 -        break;
  1.1372 -      }
  1.1373 -    }
  1.1374 -    if( iCol==pTab->nCol ){
  1.1375 -      pTab = 0;
  1.1376 -      goto error_out;
  1.1377 -    }
  1.1378 -  }
  1.1379 -
  1.1380 -  /* The following block stores the meta information that will be returned
  1.1381 -  ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
  1.1382 -  ** and autoinc. At this point there are two possibilities:
  1.1383 -  ** 
  1.1384 -  **     1. The specified column name was rowid", "oid" or "_rowid_" 
  1.1385 -  **        and there is no explicitly declared IPK column. 
  1.1386 -  **
  1.1387 -  **     2. The table is not a view and the column name identified an 
  1.1388 -  **        explicitly declared column. Copy meta information from *pCol.
  1.1389 -  */ 
  1.1390 -  if( pCol ){
  1.1391 -    zDataType = pCol->zType;
  1.1392 -    zCollSeq = pCol->zColl;
  1.1393 -    notnull = (pCol->notNull?1:0);
  1.1394 -    primarykey  = (pCol->isPrimKey?1:0);
  1.1395 -    autoinc = ((pTab->iPKey==iCol && pTab->autoInc)?1:0);
  1.1396 -  }else{
  1.1397 -    zDataType = "INTEGER";
  1.1398 -    primarykey = 1;
  1.1399 -  }
  1.1400 -  if( !zCollSeq ){
  1.1401 -    zCollSeq = "BINARY";
  1.1402 -  }
  1.1403 -
  1.1404 -error_out:
  1.1405 -  if( sqlite3SafetyOff(db) ){
  1.1406 -    rc = SQLITE_MISUSE;
  1.1407 -  }
  1.1408 -
  1.1409 -  /* Whether the function call succeeded or failed, set the output parameters
  1.1410 -  ** to whatever their local counterparts contain. If an error did occur,
  1.1411 -  ** this has the effect of zeroing all output parameters.
  1.1412 -  */
  1.1413 -  if( pzDataType ) *pzDataType = zDataType;
  1.1414 -  if( pzCollSeq ) *pzCollSeq = zCollSeq;
  1.1415 -  if( pNotNull ) *pNotNull = notnull;
  1.1416 -  if( pPrimaryKey ) *pPrimaryKey = primarykey;
  1.1417 -  if( pAutoinc ) *pAutoinc = autoinc;
  1.1418 -
  1.1419 -  if( SQLITE_OK==rc && !pTab ){
  1.1420 -    sqlite3SetString(&zErrMsg, "no such table column: ", zTableName, ".", 
  1.1421 -        zColumnName, 0);
  1.1422 -    rc = SQLITE_ERROR;
  1.1423 -  }
  1.1424 -  sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
  1.1425 -  sqlite3_free(zErrMsg);
  1.1426 -  rc = sqlite3ApiExit(db, rc);
  1.1427 -  sqlite3_mutex_leave(db->mutex);
  1.1428 -  return rc;
  1.1429 -}
  1.1430 -#endif
  1.1431 -
  1.1432 -/*
  1.1433 -** Sleep for a little while.  Return the amount of time slept.
  1.1434 -*/
  1.1435 -int sqlite3_sleep(int ms){
  1.1436 -  sqlite3_vfs *pVfs;
  1.1437 -  int rc;
  1.1438 -  pVfs = sqlite3_vfs_find(0);
  1.1439 -
  1.1440 -  /* This function works in milliseconds, but the underlying OsSleep() 
  1.1441 -  ** API uses microseconds. Hence the 1000's.
  1.1442 -  */
  1.1443 -  rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
  1.1444 -  return rc;
  1.1445 -}
  1.1446 -
  1.1447 -/*
  1.1448 -** Enable or disable the extended result codes.
  1.1449 -*/
  1.1450 -int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
  1.1451 -  sqlite3_mutex_enter(db->mutex);
  1.1452 -  db->errMask = onoff ? 0xffffffff : 0xff;
  1.1453 -  sqlite3_mutex_leave(db->mutex);
  1.1454 -  return SQLITE_OK;
  1.1455 -}
  1.1456 -
  1.1457 -/*
  1.1458 -** Invoke the xFileControl method on a particular database.
  1.1459 -*/
  1.1460 -int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
  1.1461 -  int rc = SQLITE_ERROR;
  1.1462 -  int iDb;
  1.1463 -  sqlite3_mutex_enter(db->mutex);
  1.1464 -  if( zDbName==0 ){
  1.1465 -    iDb = 0;
  1.1466 -  }else{
  1.1467 -    for(iDb=0; iDb<db->nDb; iDb++){
  1.1468 -      if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break;
  1.1469 -    }
  1.1470 -  }
  1.1471 -  if( iDb<db->nDb ){
  1.1472 -    Btree *pBtree = db->aDb[iDb].pBt;
  1.1473 -    if( pBtree ){
  1.1474 -      Pager *pPager;
  1.1475 -      sqlite3BtreeEnter(pBtree);
  1.1476 -      pPager = sqlite3BtreePager(pBtree);
  1.1477 -      if( pPager ){
  1.1478 -        sqlite3_file *fd = sqlite3PagerFile(pPager);
  1.1479 -        if( fd ){
  1.1480 -          rc = sqlite3OsFileControl(fd, op, pArg);
  1.1481 -        }
  1.1482 -      }
  1.1483 -      sqlite3BtreeLeave(pBtree);
  1.1484 -    }
  1.1485 -  }
  1.1486 -  sqlite3_mutex_leave(db->mutex);
  1.1487 -  return rc;   
  1.1488 -}