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

📄 main.c

📁 SQLite is a software library that implements a self-contained, serverless, zero-configuration, trans
💻 C
📖 第 1 页 / 共 4 页
字号:
}#endif /* SQLITE_OMIT_UTF16 *//*** Return the most recent error code generated by an SQLite routine. If NULL is** passed to this function, we assume a malloc() failed during sqlite3_open().*/int sqlite3_errcode(sqlite3 *db){  if( db && !sqlite3SafetyCheckSickOrOk(db) ){    return SQLITE_MISUSE;  }  if( !db || db->mallocFailed ){    return SQLITE_NOMEM;  }  return db->errCode & db->errMask;}/*** Create a new collating function for database "db".  The name is zName** and the encoding is enc.*/static int createCollation(  sqlite3* db,   const char *zName,   int enc,   void* pCtx,  int(*xCompare)(void*,int,const void*,int,const void*),  void(*xDel)(void*)){  CollSeq *pColl;  int enc2;    assert( sqlite3_mutex_held(db->mutex) );  /* 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.  */  enc2 = enc & ~SQLITE_UTF16_ALIGNED;  if( enc2==SQLITE_UTF16 ){    enc2 = SQLITE_UTF16NATIVE;  }  if( (enc2&~3)!=0 ){    sqlite3Error(db, SQLITE_ERROR, "unknown encoding");    return SQLITE_ERROR;  }  /* Check if this call is removing or replacing an existing collation   ** sequence. If so, and there are active VMs, return busy. If there  ** are no active VMs, invalidate any pre-compiled statements.  */  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 0);  if( pColl && pColl->xCmp ){    if( db->activeVdbeCnt ){      sqlite3Error(db, SQLITE_BUSY,         "Unable to delete/modify collation sequence due to active statements");      return SQLITE_BUSY;    }    sqlite3ExpirePreparedStatements(db);    /* If collation sequence pColl was created directly by a call to    ** sqlite3_create_collation, and not generated by synthCollSeq(),    ** then any copies made by synthCollSeq() need to be invalidated.    ** Also, collation destructor - CollSeq.xDel() - function may need    ** to be called.    */     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){      CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, strlen(zName));      int j;      for(j=0; j<3; j++){        CollSeq *p = &aColl[j];        if( p->enc==pColl->enc ){          if( p->xDel ){            p->xDel(p->pUser);          }          p->xCmp = 0;        }      }    }  }  pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, strlen(zName), 1);  if( pColl ){    pColl->xCmp = xCompare;    pColl->pUser = pCtx;    pColl->xDel = xDel;    pColl->enc = enc2 | (enc & SQLITE_UTF16_ALIGNED);  }  sqlite3Error(db, SQLITE_OK, 0);  return SQLITE_OK;}/*** This array defines hard upper bounds on limit values.  The** initializer must be kept in sync with the SQLITE_LIMIT_*** #defines in sqlite3.h.*/static const int aHardLimit[] = {  SQLITE_MAX_LENGTH,  SQLITE_MAX_SQL_LENGTH,  SQLITE_MAX_COLUMN,  SQLITE_MAX_EXPR_DEPTH,  SQLITE_MAX_COMPOUND_SELECT,  SQLITE_MAX_VDBE_OP,  SQLITE_MAX_FUNCTION_ARG,  SQLITE_MAX_ATTACHED,  SQLITE_MAX_LIKE_PATTERN_LENGTH,  SQLITE_MAX_VARIABLE_NUMBER,};/*** Make sure the hard limits are set to reasonable values*/#if SQLITE_MAX_LENGTH<100# error SQLITE_MAX_LENGTH must be at least 100#endif#if SQLITE_MAX_SQL_LENGTH<100# error SQLITE_MAX_SQL_LENGTH must be at least 100#endif#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH#endif#if SQLITE_MAX_COLUMN<1# error SQLITE_MAX_COLUMN must be at least 1#endif#if SQLITE_MAX_EXPR_DEPTH<1# error SQLITE_MAX_EXPR_DEPTH must be at least 1#endif#if SQLITE_MAX_COMPOUND_SELECT<2# error SQLITE_MAX_COMPOUND_SELECT must be at least 2#endif#if SQLITE_MAX_VDBE_OP<40# error SQLITE_MAX_VDBE_OP must be at least 40#endif#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>255# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 255#endif#if SQLITE_MAX_ATTACH<0 || SQLITE_MAX_ATTACH>30# error SQLITE_MAX_ATTACH must be between 0 and 30#endif#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1#endif#if SQLITE_MAX_VARIABLE_NUMBER<1# error SQLITE_MAX_VARIABLE_NUMBER must be at least 1#endif/*** Change the value of a limit.  Report the old value.** If an invalid limit index is supplied, report -1.** Make no changes but still report the old value if the** new limit is negative.**** A new lower limit does not shrink existing constructs.** It merely prevents new constructs that exceed the limit** from forming.*/int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){  int oldLimit;  if( limitId<0 || limitId>=SQLITE_N_LIMIT ){    return -1;  }  oldLimit = db->aLimit[limitId];  if( newLimit>=0 ){    if( newLimit>aHardLimit[limitId] ){      newLimit = aHardLimit[limitId];    }    db->aLimit[limitId] = newLimit;  }  return oldLimit;}/*** This routine does the work of opening a database on behalf of** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"  ** is UTF-8 encoded.*/static int openDatabase(  const char *zFilename, /* Database filename UTF-8 encoded */  sqlite3 **ppDb,        /* OUT: Returned database handle */  unsigned flags,        /* Operational flags */  const char *zVfs       /* Name of the VFS to use */){  sqlite3 *db;  int rc;  CollSeq *pColl;  /* Remove harmful bits from the flags parameter */  flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |               SQLITE_OPEN_MAIN_DB |               SQLITE_OPEN_TEMP_DB |                SQLITE_OPEN_TRANSIENT_DB |                SQLITE_OPEN_MAIN_JOURNAL |                SQLITE_OPEN_TEMP_JOURNAL |                SQLITE_OPEN_SUBJOURNAL |                SQLITE_OPEN_MASTER_JOURNAL             );  /* Allocate the sqlite data structure */  db = sqlite3MallocZero( sizeof(sqlite3) );  if( db==0 ) goto opendb_out;  db->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);  if( db->mutex==0 ){    sqlite3_free(db);    db = 0;    goto opendb_out;  }  sqlite3_mutex_enter(db->mutex);  db->errMask = 0xff;  db->priorNewRowid = 0;  db->nDb = 2;  db->magic = SQLITE_MAGIC_BUSY;  db->aDb = db->aDbStatic;  assert( sizeof(db->aLimit)==sizeof(aHardLimit) );  memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));  db->autoCommit = 1;  db->nextAutovac = -1;  db->nextPagesize = 0;  db->flags |= SQLITE_ShortColNames#if SQLITE_DEFAULT_FILE_FORMAT<4                 | SQLITE_LegacyFileFmt#endif#ifdef SQLITE_ENABLE_LOAD_EXTENSION                 | SQLITE_LoadExtension#endif      ;  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);  sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);#ifndef SQLITE_OMIT_VIRTUALTABLE  sqlite3HashInit(&db->aModule, SQLITE_HASH_STRING, 0);#endif  db->pVfs = sqlite3_vfs_find(zVfs);  if( !db->pVfs ){    rc = SQLITE_ERROR;    db->magic = SQLITE_MAGIC_SICK;    sqlite3Error(db, rc, "no such vfs: %s", zVfs);    goto opendb_out;  }  /* Add the default collation sequence BINARY. BINARY works for both UTF-8  ** and UTF-16, so add a version for each to avoid any unnecessary  ** conversions. The only error that can occur here is a malloc() failure.  */  createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);  createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);  createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);  createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);  if( db->mallocFailed ){    db->magic = SQLITE_MAGIC_SICK;    goto opendb_out;  }  db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);  assert( db->pDfltColl!=0 );  /* Also add a UTF-8 case-insensitive collation sequence. */  createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);  /* Set flags on the built-in collating sequences */  db->pDfltColl->type = SQLITE_COLL_BINARY;  pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "NOCASE", 6, 0);  if( pColl ){    pColl->type = SQLITE_COLL_NOCASE;  }  /* Open the backend database driver */  db->openFlags = flags;  rc = sqlite3BtreeFactory(db, zFilename, 0, SQLITE_DEFAULT_CACHE_SIZE,                            flags | SQLITE_OPEN_MAIN_DB,                           &db->aDb[0].pBt);  if( rc!=SQLITE_OK ){    sqlite3Error(db, rc, 0);    db->magic = SQLITE_MAGIC_SICK;    goto opendb_out;  }  db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);  db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);  /* The default safety_level for the main database is 'full'; for the temp  ** database it is 'NONE'. This matches the pager layer defaults.    */  db->aDb[0].zName = "main";  db->aDb[0].safety_level = 3;#ifndef SQLITE_OMIT_TEMPDB  db->aDb[1].zName = "temp";  db->aDb[1].safety_level = 1;#endif  db->magic = SQLITE_MAGIC_OPEN;  if( db->mallocFailed ){    goto opendb_out;  }  /* Register all built-in functions, but do not attempt to read the  ** database schema yet. This is delayed until the first time the database  ** is accessed.  */  sqlite3Error(db, SQLITE_OK, 0);  sqlite3RegisterBuiltinFunctions(db);  /* Load automatic extensions - extensions that have been registered  ** using the sqlite3_automatic_extension() API.  */  (void)sqlite3AutoLoadExtensions(db);  if( sqlite3_errcode(db)!=SQLITE_OK ){    goto opendb_out;  }#ifdef SQLITE_ENABLE_FTS1  if( !db->mallocFailed ){    extern int sqlite3Fts1Init(sqlite3*);    rc = sqlite3Fts1Init(db);  }#endif#ifdef SQLITE_ENABLE_FTS2  if( !db->mallocFailed && rc==SQLITE_OK ){    extern int sqlite3Fts2Init(sqlite3*);    rc = sqlite3Fts2Init(db);  }#endif#ifdef SQLITE_ENABLE_FTS3  if( !db->mallocFailed && rc==SQLITE_OK ){    rc = sqlite3Fts3Init(db);  }#endif#ifdef SQLITE_ENABLE_ICU  if( !db->mallocFailed && rc==SQLITE_OK ){    extern int sqlite3IcuInit(sqlite3*);    rc = sqlite3IcuInit(db);  }#endif  sqlite3Error(db, rc, 0);  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking  ** mode.  Doing nothing at all also makes NORMAL the default.  */#ifdef SQLITE_DEFAULT_LOCKING_MODE  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;  sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),                          SQLITE_DEFAULT_LOCKING_MODE);#endifopendb_out:  if( db ){    assert( db->mutex!=0 );    sqlite3_mutex_leave(db->mutex);  }  if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){    sqlite3_close(db);    db = 0;  }  *ppDb = db;  return sqlite3ApiExit(0, rc);}/*** Open a new database handle.*/int sqlite3_open(  const char *zFilename,   sqlite3 **ppDb ){  return openDatabase(zFilename, ppDb,                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);}int sqlite3_open_v2(  const char *filename,   /* Database filename (UTF-8) */  sqlite3 **ppDb,         /* OUT: SQLite db handle */  int flags,              /* Flags */  const char *zVfs        /* Name of VFS module to use */){  return openDatabase(filename, ppDb, flags, zVfs);}#ifndef SQLITE_OMIT_UTF16/*** Open a new database handle.*/int sqlite3_open16(  const void *zFilename,   sqlite3 **ppDb){  char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */  sqlite3_value *pVal;  int rc = SQLITE_NOMEM;  assert( zFilename );  assert( ppDb );  *ppDb = 0;  pVal = sqlite3ValueNew(0);  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);  if( zFilename8 ){    rc = openDatabase(zFilename8, ppDb,                      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);    assert( *ppDb || rc==SQLITE_NOMEM );    if( rc==SQLITE_OK ){      ENC(*ppDb) = SQLITE_UTF16NATIVE;      if( rc!=SQLITE_OK ){        sqlite3_close(*ppDb);        *ppDb = 0;      }    }  }  sqlite3ValueFree(pVal);  return sqlite3ApiExit(0, rc);}#endif /* SQLITE_OMIT_UTF16 *//*** Register a new collation sequence with the database handle db.*/int sqlite3_create_collation(  sqlite3* db, 

⌨️ 快捷键说明

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