⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 最新的sqlite3.6.2源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
*/int sqlite3_close(sqlite3 *db){  HashElem *i;  int j;  if( !db ){    return SQLITE_OK;  }  if( !sqlite3SafetyCheckSickOrOk(db) ){    return SQLITE_MISUSE;  }  sqlite3_mutex_enter(db->mutex);#ifdef SQLITE_SSE  {    extern void sqlite3SseCleanup(sqlite3*);    sqlite3SseCleanup(db);  }#endif   sqlite3ResetInternalSchema(db, 0);  /* If a transaction is open, the ResetInternalSchema() call above  ** will not have called the xDisconnect() method on any virtual  ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()  ** call will do so. We need to do this before the check for active  ** SQL statements below, as the v-table implementation may be storing  ** some prepared statements internally.  */  sqlite3VtabRollback(db);  /* If there are any outstanding VMs, return SQLITE_BUSY. */  if( db->pVdbe ){    sqlite3Error(db, SQLITE_BUSY,         "Unable to close due to unfinalised statements");    sqlite3_mutex_leave(db->mutex);    return SQLITE_BUSY;  }  assert( sqlite3SafetyCheckSickOrOk(db) );  for(j=0; j<db->nDb; j++){    struct Db *pDb = &db->aDb[j];    if( pDb->pBt ){      sqlite3BtreeClose(pDb->pBt);      pDb->pBt = 0;      if( j!=1 ){        pDb->pSchema = 0;      }    }  }  sqlite3ResetInternalSchema(db, 0);  assert( db->nDb<=2 );  assert( db->aDb==db->aDbStatic );  for(j=0; j<ArraySize(db->aFunc.a); j++){    FuncDef *pNext, *pHash, *p;    for(p=db->aFunc.a[j]; p; p=pHash){      pHash = p->pHash;      while( p ){        pNext = p->pNext;        sqlite3DbFree(db, p);        p = pNext;      }    }  }  for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){    CollSeq *pColl = (CollSeq *)sqliteHashData(i);    /* Invoke any destructors registered for collation sequence user data. */    for(j=0; j<3; j++){      if( pColl[j].xDel ){        pColl[j].xDel(pColl[j].pUser);      }    }    sqlite3DbFree(db, pColl);  }  sqlite3HashClear(&db->aCollSeq);#ifndef SQLITE_OMIT_VIRTUALTABLE  for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){    Module *pMod = (Module *)sqliteHashData(i);    if( pMod->xDestroy ){      pMod->xDestroy(pMod->pAux);    }    sqlite3DbFree(db, pMod);  }  sqlite3HashClear(&db->aModule);#endif  sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */  if( db->pErr ){    sqlite3ValueFree(db->pErr);  }  sqlite3CloseExtensions(db);  db->magic = SQLITE_MAGIC_ERROR;  /* The temp-database schema is allocated differently from the other schema  ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).  ** So it needs to be freed here. Todo: Why not roll the temp schema into  ** the same sqliteMalloc() as the one that allocates the database   ** structure?  */  sqlite3DbFree(db, db->aDb[1].pSchema);  sqlite3_mutex_leave(db->mutex);  db->magic = SQLITE_MAGIC_CLOSED;  sqlite3_mutex_free(db->mutex);  if( db->lookaside.bMalloced ){    sqlite3_free(db->lookaside.pStart);  }  sqlite3_free(db);  return SQLITE_OK;}/*** Rollback all database files.*/void sqlite3RollbackAll(sqlite3 *db){  int i;  int inTrans = 0;  assert( sqlite3_mutex_held(db->mutex) );  sqlite3BeginBenignMalloc();  for(i=0; i<db->nDb; i++){    if( db->aDb[i].pBt ){      if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){        inTrans = 1;      }      sqlite3BtreeRollback(db->aDb[i].pBt);      db->aDb[i].inTrans = 0;    }  }  sqlite3VtabRollback(db);  sqlite3EndBenignMalloc();  if( db->flags&SQLITE_InternChanges ){    sqlite3ExpirePreparedStatements(db);    sqlite3ResetInternalSchema(db, 0);  }  /* If one has been configured, invoke the rollback-hook callback */  if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){    db->xRollbackCallback(db->pRollbackArg);  }}/*** Return a static string that describes the kind of error specified in the** argument.*/const char *sqlite3ErrStr(int rc){  const char *z;  switch( rc & 0xff ){    case SQLITE_ROW:    case SQLITE_DONE:    case SQLITE_OK:         z = "not an error";                          break;    case SQLITE_ERROR:      z = "SQL logic error or missing database";   break;    case SQLITE_PERM:       z = "access permission denied";              break;    case SQLITE_ABORT:      z = "callback requested query abort";        break;    case SQLITE_BUSY:       z = "database is locked";                    break;    case SQLITE_LOCKED:     z = "database table is locked";              break;    case SQLITE_NOMEM:      z = "out of memory";                         break;    case SQLITE_READONLY:   z = "attempt to write a readonly database";  break;    case SQLITE_INTERRUPT:  z = "interrupted";                           break;    case SQLITE_IOERR:      z = "disk I/O error";                        break;    case SQLITE_CORRUPT:    z = "database disk image is malformed";      break;    case SQLITE_FULL:       z = "database or disk is full";              break;    case SQLITE_CANTOPEN:   z = "unable to open database file";          break;    case SQLITE_EMPTY:      z = "table contains no data";                break;    case SQLITE_SCHEMA:     z = "database schema has changed";           break;    case SQLITE_TOOBIG:     z = "String or BLOB exceeded size limit";    break;    case SQLITE_CONSTRAINT: z = "constraint failed";                     break;    case SQLITE_MISMATCH:   z = "datatype mismatch";                     break;    case SQLITE_MISUSE:     z = "library routine called out of sequence";break;    case SQLITE_NOLFS:      z = "large file support is disabled";        break;    case SQLITE_AUTH:       z = "authorization denied";                  break;    case SQLITE_FORMAT:     z = "auxiliary database format error";       break;    case SQLITE_RANGE:      z = "bind or column index out of range";     break;    case SQLITE_NOTADB:     z = "file is encrypted or is not a database";break;    default:                z = "unknown error";                         break;  }  return z;}/*** This routine implements a busy callback that sleeps and tries** again until a timeout value is reached.  The timeout value is** an integer number of milliseconds passed in as the first** argument.*/static int sqliteDefaultBusyCallback( void *ptr,               /* Database connection */ int count                /* Number of times table has been busy */){#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)  static const u8 delays[] =     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };  static const u8 totals[] =     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };# define NDELAY (sizeof(delays)/sizeof(delays[0]))  sqlite3 *db = (sqlite3 *)ptr;  int timeout = db->busyTimeout;  int delay, prior;  assert( count>=0 );  if( count < NDELAY ){    delay = delays[count];    prior = totals[count];  }else{    delay = delays[NDELAY-1];    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));  }  if( prior + delay > timeout ){    delay = timeout - prior;    if( delay<=0 ) return 0;  }  sqlite3OsSleep(db->pVfs, delay*1000);  return 1;#else  sqlite3 *db = (sqlite3 *)ptr;  int timeout = ((sqlite3 *)ptr)->busyTimeout;  if( (count+1)*1000 > timeout ){    return 0;  }  sqlite3OsSleep(db->pVfs, 1000000);  return 1;#endif}/*** Invoke the given busy handler.**** This routine is called when an operation failed with a lock.** If this routine returns non-zero, the lock is retried.  If it** returns 0, the operation aborts with an SQLITE_BUSY error.*/int sqlite3InvokeBusyHandler(BusyHandler *p){  int rc;  if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;  rc = p->xFunc(p->pArg, p->nBusy);  if( rc==0 ){    p->nBusy = -1;  }else{    p->nBusy++;  }  return rc; }/*** This routine sets the busy callback for an Sqlite database to the** given callback function with the given argument.*/int sqlite3_busy_handler(  sqlite3 *db,  int (*xBusy)(void*,int),  void *pArg){  sqlite3_mutex_enter(db->mutex);  db->busyHandler.xFunc = xBusy;  db->busyHandler.pArg = pArg;  db->busyHandler.nBusy = 0;  sqlite3_mutex_leave(db->mutex);  return SQLITE_OK;}#ifndef SQLITE_OMIT_PROGRESS_CALLBACK/*** This routine sets the progress callback for an Sqlite database to the** given callback function with the given argument. The progress callback will** be invoked every nOps opcodes.*/void sqlite3_progress_handler(  sqlite3 *db,   int nOps,  int (*xProgress)(void*),   void *pArg){  sqlite3_mutex_enter(db->mutex);  if( nOps>0 ){    db->xProgress = xProgress;    db->nProgressOps = nOps;    db->pProgressArg = pArg;  }else{    db->xProgress = 0;    db->nProgressOps = 0;    db->pProgressArg = 0;  }  sqlite3_mutex_leave(db->mutex);}#endif/*** This routine installs a default busy handler that waits for the** specified number of milliseconds before returning 0.*/int sqlite3_busy_timeout(sqlite3 *db, int ms){  if( ms>0 ){    db->busyTimeout = ms;    sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);  }else{    sqlite3_busy_handler(db, 0, 0);  }  return SQLITE_OK;}/*** Cause any pending operation to stop at its earliest opportunity.*/void sqlite3_interrupt(sqlite3 *db){  db->u1.isInterrupted = 1;}/*** This function is exactly the same as sqlite3_create_function(), except** that it is designed to be called by internal code. The difference is** that if a malloc() fails in sqlite3_create_function(), an error code** is returned and the mallocFailed flag cleared. */int sqlite3CreateFunc(  sqlite3 *db,  const char *zFunctionName,  int nArg,  int enc,  void *pUserData,  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),  void (*xStep)(sqlite3_context*,int,sqlite3_value **),  void (*xFinal)(sqlite3_context*)){  FuncDef *p;  int nName;  assert( sqlite3_mutex_held(db->mutex) );  if( zFunctionName==0 ||      (xFunc && (xFinal || xStep)) ||       (!xFunc && (xFinal && !xStep)) ||      (!xFunc && (!xFinal && xStep)) ||      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||      (255<(nName = sqlite3Strlen(db, zFunctionName))) ){    sqlite3Error(db, SQLITE_ERROR, "bad parameters");    return SQLITE_ERROR;  }  #ifndef SQLITE_OMIT_UTF16  /* If SQLITE_UTF16 is specified as the encoding type, transform this  ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the  ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.  **  ** If SQLITE_ANY is specified, add three versions of the function  ** to the hash table.  */  if( enc==SQLITE_UTF16 ){    enc = SQLITE_UTF16NATIVE;  }else if( enc==SQLITE_ANY ){    int rc;    rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8,         pUserData, xFunc, xStep, xFinal);    if( rc==SQLITE_OK ){      rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE,          pUserData, xFunc, xStep, xFinal);    }    if( rc!=SQLITE_OK ){      return rc;    }    enc = SQLITE_UTF16BE;  }#else  enc = SQLITE_UTF8;#endif    /* Check if an existing function is being overridden or deleted. If so,  ** and there are active VMs, then return SQLITE_BUSY. If a function  ** is being overridden/deleted but there are no active VMs, allow the  ** operation to continue but invalidate all precompiled statements.  */  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 0);  if( p && p->iPrefEnc==enc && p->nArg==nArg ){    if( db->activeVdbeCnt ){      sqlite3Error(db, SQLITE_BUSY,         "Unable to delete/modify user-function due to active statements");      assert( !db->mallocFailed );      return SQLITE_BUSY;    }else{      sqlite3ExpirePreparedStatements(db);    }  }  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);  assert(p || db->mallocFailed);  if( !p ){    return SQLITE_NOMEM;  }  p->flags = 0;  p->xFunc = xFunc;  p->xStep = xStep;  p->xFinalize = xFinal;  p->pUserData = pUserData;  p->nArg = nArg;  return SQLITE_OK;}/*** Create new user functions.*/int sqlite3_create_function(  sqlite3 *db,  const char *zFunctionName,  int nArg,  int enc,  void *p,  void (*xFunc)(sqlite3_context*,int,sqlite3_value **),  void (*xStep)(sqlite3_context*,int,sqlite3_value **),  void (*xFinal)(sqlite3_context*)){  int rc;  sqlite3_mutex_enter(db->mutex);  rc = sqlite3CreateFunc(db, zFunctionName, nArg, enc, p, xFunc, xStep, xFinal);  rc = sqlite3ApiExit(db, rc);  sqlite3_mutex_leave(db->mutex);  return rc;}#ifndef SQLITE_OMIT_UTF16int sqlite3_create_function16(  sqlite3 *db,  const void *zFunctionName,  int nArg,  int eTextRep,  void *p,  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),  void (*xStep)(sqlite3_context*,int,sqlite3_value**),  void (*xFinal)(sqlite3_context*)){  int rc;  char *zFunc8;  sqlite3_mutex_enter(db->mutex);  assert( !db->mallocFailed );  zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1);  rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal);  sqlite3DbFree(db, zFunc8);  rc = sqlite3ApiExit(db, rc);  sqlite3_mutex_leave(db->mutex);  return rc;}#endif/*** Declare that a function has been overloaded by a virtual table.**** If the function already exists as a regular global function, then** this routine is a no-op.  If the function does not exist, then create** a new one that always throws a run-time error.  **** When virtual tables intend to provide an overloaded function, they** should call this routine to make sure the global function exists.** A global function must exist in order for name resolution to work** properly.*/int sqlite3_overload_function(  sqlite3 *db,  const char *zName,  int nArg){  int nName = sqlite3Strlen(db, zName);  int rc;  sqlite3_mutex_enter(db->mutex);  if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){    sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,                      0, sqlite3InvalidFunction, 0, 0);  }  rc = sqlite3ApiExit(db, SQLITE_OK);  sqlite3_mutex_leave(db->mutex);  return rc;}#ifndef SQLITE_OMIT_TRACE/*** Register a trace function.  The pArg from the previously registered trace** is returned.  **** A NULL trace function means that no tracing is executes.  A non-NULL** trace is a pointer to a function that is invoked at the start of each** SQL statement.*/void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){  void *pOld;  sqlite3_mutex_enter(db->mutex);  pOld = db->pTraceArg;  db->xTrace = xTrace;  db->pTraceArg = pArg;  sqlite3_mutex_leave(db->mutex);  return pOld;}/*** Register a profile function.  The pArg from the previously registered ** profile function is returned.  **** A NULL profile function means that no profiling is executes.  A non-NULL** profile is a pointer to a function that is invoked at the conclusion of** each SQL statement that is run.*/void *sqlite3_profile(  sqlite3 *db,  void (*xProfile)(void*,const char*,sqlite_uint64),  void *pArg){  void *pOld;  sqlite3_mutex_enter(db->mutex);  pOld = db->pProfileArg;  db->xProfile = xProfile;  db->pProfileArg = pArg;  sqlite3_mutex_leave(db->mutex);  return pOld;}#endif /* SQLITE_OMIT_TRACE *//*** EXPERIMENTAL ******* Register a function to be invoked when a transaction comments.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -