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

📄 main.c

📁 sqlite 嵌入式数据库的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  void *pOld = db->pTraceArg;  db->xTrace = xTrace;  db->pTraceArg = pArg;  return pOld;}/*** EXPERIMENTAL ******* Register a function to be invoked when a transaction comments.** If either function returns non-zero, then the commit becomes a** rollback.*/void *sqlite3_commit_hook(  sqlite3 *db,              /* Attach the hook to this database */  int (*xCallback)(void*),  /* Function to invoke on each commit */  void *pArg                /* Argument to the function */){  void *pOld = db->pCommitArg;  db->xCommitCallback = xCallback;  db->pCommitArg = pArg;  return pOld;}/*** This routine is called to create a connection to a database BTree** driver.  If zFilename is the name of a file, then that file is** opened and used.  If zFilename is the magic name ":memory:" then** the database is stored in memory (and is thus forgotten as soon as** the connection is closed.)  If zFilename is NULL then the database** is for temporary use only and is deleted as soon as the connection** is closed.**** A temporary database can be either a disk file (that is automatically** deleted when the file is closed) or a set of red-black trees held in memory,** depending on the values of the TEMP_STORE compile-time macro and the** db->temp_store variable, according to the following chart:****       TEMP_STORE     db->temp_store     Location of temporary database**       ----------     --------------     ------------------------------**           0               any             file**           1                1              file**           1                2              memory**           1                0              file**           2                1              file**           2                2              memory**           2                0              memory**           3               any             memory*/int sqlite3BtreeFactory(  const sqlite3 *db,        /* Main database when opening aux otherwise 0 */  const char *zFilename,    /* Name of the file containing the BTree database */  int omitJournal,          /* if TRUE then do not journal this file */  int nCache,               /* How many pages in the page cache */  Btree **ppBtree           /* Pointer to new Btree object written here */){  int btree_flags = 0;  int rc;    assert( ppBtree != 0);  if( omitJournal ){    btree_flags |= BTREE_OMIT_JOURNAL;  }  if( db->flags & SQLITE_NoReadlock ){    btree_flags |= BTREE_NO_READLOCK;  }  if( zFilename==0 ){#if TEMP_STORE==0    /* Do nothing */#endif#ifndef SQLITE_OMIT_MEMORYDB#if TEMP_STORE==1    if( db->temp_store==2 ) zFilename = ":memory:";#endif#if TEMP_STORE==2    if( db->temp_store!=1 ) zFilename = ":memory:";#endif#if TEMP_STORE==3    zFilename = ":memory:";#endif#endif /* SQLITE_OMIT_MEMORYDB */  }  rc = sqlite3BtreeOpen(zFilename, ppBtree, btree_flags);  if( rc==SQLITE_OK ){    sqlite3BtreeSetBusyHandler(*ppBtree, (void*)&db->busyHandler);    sqlite3BtreeSetCacheSize(*ppBtree, nCache);  }  return rc;}/*** Return UTF-8 encoded English language explanation of the most recent** error.*/const char *sqlite3_errmsg(sqlite3 *db){  const char *z;  if( sqlite3_malloc_failed ){    return sqlite3ErrStr(SQLITE_NOMEM);  }  if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){    return sqlite3ErrStr(SQLITE_MISUSE);  }  z = sqlite3_value_text(db->pErr);  if( z==0 ){    z = sqlite3ErrStr(db->errCode);  }  return z;}#ifndef SQLITE_OMIT_UTF16/*** Return UTF-16 encoded English language explanation of the most recent** error.*/const void *sqlite3_errmsg16(sqlite3 *db){  /* Because all the characters in the string are in the unicode  ** range 0x00-0xFF, if we pad the big-endian string with a   ** zero byte, we can obtain the little-endian string with  ** &big_endian[1].  */  static const char outOfMemBe[] = {    0, 'o', 0, 'u', 0, 't', 0, ' ',     0, 'o', 0, 'f', 0, ' ',     0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0  };  static const char misuseBe [] = {    0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',     0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',     0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',     0, 'o', 0, 'u', 0, 't', 0, ' ',     0, 'o', 0, 'f', 0, ' ',     0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0  };  const void *z;  if( sqlite3_malloc_failed ){    return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);  }  if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){    return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);  }  z = sqlite3_value_text16(db->pErr);  if( z==0 ){    sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),         SQLITE_UTF8, SQLITE_STATIC);    z = sqlite3_value_text16(db->pErr);  }  return z;}#endif /* SQLITE_OMIT_UTF16 *//*** Return the most recent error code generated by an SQLite routine.*/int sqlite3_errcode(sqlite3 *db){  if( sqlite3_malloc_failed ){    return SQLITE_NOMEM;  }  if( sqlite3SafetyCheck(db) ){    return SQLITE_MISUSE;  }  return db->errCode;}/*** 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 */){  sqlite3 *db;  int rc, i;  /* Allocate the sqlite data structure */  db = sqliteMalloc( sizeof(sqlite3) );  if( db==0 ) goto opendb_out;  db->priorNewRowid = 0;  db->magic = SQLITE_MAGIC_BUSY;  db->nDb = 2;  db->aDb = db->aDbStatic;  db->enc = SQLITE_UTF8;  db->autoCommit = 1;  db->flags |= SQLITE_ShortColNames;  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);  sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);  for(i=0; i<db->nDb; i++){    sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);    sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);    sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);    sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);  }    /* 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.  */  if( sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binCollFunc) ||      sqlite3_create_collation(db, "BINARY", SQLITE_UTF16, 0,binCollFunc) ||      !(db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0)) ){    rc = db->errCode;    assert( rc!=SQLITE_OK );    db->magic = SQLITE_MAGIC_CLOSED;    goto opendb_out;  }  /* Also add a UTF-8 case-insensitive collation sequence. */  sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);  /* Open the backend database driver */  rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);  if( rc!=SQLITE_OK ){    sqlite3Error(db, rc, 0);    db->magic = SQLITE_MAGIC_CLOSED;    goto opendb_out;  }  /* 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  /* 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.  */  sqlite3RegisterBuiltinFunctions(db);  sqlite3Error(db, SQLITE_OK, 0);  db->magic = SQLITE_MAGIC_OPEN;opendb_out:  if( sqlite3_errcode(db)==SQLITE_OK && sqlite3_malloc_failed ){    sqlite3Error(db, SQLITE_NOMEM, 0);  }  *ppDb = db;#ifndef SQLITE_OMIT_GLOBALRECOVER  if( db ){    sqlite3OsEnterMutex();    db->pNext = pDbList;    pDbList = db;    sqlite3OsLeaveMutex();  }#endif  return sqlite3_errcode(db);}/*** Open a new database handle.*/int sqlite3_open(  const char *zFilename,   sqlite3 **ppDb ){  return openDatabase(zFilename, ppDb);}#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 */  int rc = SQLITE_NOMEM;  sqlite3_value *pVal;  assert( ppDb );  *ppDb = 0;  pVal = sqlite3ValueNew();  sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);  zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);  if( zFilename8 ){    rc = openDatabase(zFilename8, ppDb);    if( rc==SQLITE_OK && *ppDb ){      sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);    }  }  if( pVal ){    sqlite3ValueFree(pVal);  }  return rc;}#endif /* SQLITE_OMIT_UTF16 *//*** The following routine destroys a virtual machine that is created by** the sqlite3_compile() routine. The integer returned is an SQLITE_** success/failure code that describes the result of executing the virtual** machine.**** This routine sets the error code and string returned by** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().*/int sqlite3_finalize(sqlite3_stmt *pStmt){  int rc;  if( pStmt==0 ){    rc = SQLITE_OK;  }else{    rc = sqlite3VdbeFinalize((Vdbe*)pStmt);  }  return rc;}/*** Terminate the current execution of an SQL statement and reset it** back to its starting state so that it can be reused. A success code from** the prior execution is returned.**** This routine sets the error code and string returned by** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().*/int sqlite3_reset(sqlite3_stmt *pStmt){  int rc;  if( pStmt==0 ){    rc = SQLITE_OK;  }else{    rc = sqlite3VdbeReset((Vdbe*)pStmt);    sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0, 0, 0, 0);  }  return rc;}/*** Register a new collation sequence with the database handle db.*/int sqlite3_create_collation(  sqlite3* db,   const char *zName,   int enc,   void* pCtx,  int(*xCompare)(void*,int,const void*,int,const void*)){  CollSeq *pColl;  int rc = SQLITE_OK;    if( sqlite3SafetyCheck(db) ){    return SQLITE_MISUSE;  }  /* 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( enc==SQLITE_UTF16 ){    enc = SQLITE_UTF16NATIVE;  }  if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){    sqlite3Error(db, SQLITE_ERROR,         "Param 3 to sqlite3_create_collation() must be one of "        "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"    );    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)enc, 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);  }  pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);  if( 0==pColl ){   rc = SQLITE_NOMEM;  }else{    pColl->xCmp = xCompare;    pColl->pUser = pCtx;    pColl->enc = enc;  }  sqlite3Error(db, rc, 0);  return rc;}#ifndef SQLITE_OMIT_UTF16/*** Register a new collation sequence with the database handle db.*/int sqlite3_create_collation16(  sqlite3* db,   const char *zName,   int enc,   void* pCtx,  int(*xCompare)(void*,int,const void*,int,const void*)){  char const *zName8;  sqlite3_value *pTmp;  if( sqlite3SafetyCheck(db) ){    return SQLITE_MISUSE;  }  pTmp = sqlite3GetTransientValue(db);  sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);  zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);  return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);}#endif /* SQLITE_OMIT_UTF16 *//*** Register a collation sequence factory callback with the database handle** db. Replace any previously installed collation sequence factory.*/int sqlite3_collation_needed(  sqlite3 *db,   void *pCollNeededArg,   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)){  if( sqlite3SafetyCheck(db) ){    return SQLITE_MISUSE;  }  db->xCollNeeded = xCollNeeded;  db->xCollNeeded16 = 0;  db->pCollNeededArg = pCollNeededArg;  return SQLITE_OK;}#ifndef SQLITE_OMIT_UTF16/*** Register a collation sequence factory callback with the database handle** db. Replace any previously installed collation sequence factory.*/int sqlite3_collation_needed16(  sqlite3 *db,   void *pCollNeededArg,   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)){  if( sqlite3SafetyCheck(db) ){    return SQLITE_MISUSE;  }  db->xCollNeeded = 0;  db->xCollNeeded16 = xCollNeeded16;  db->pCollNeededArg = pCollNeededArg;  return SQLITE_OK;}#endif /* SQLITE_OMIT_UTF16 */#ifndef SQLITE_OMIT_GLOBALRECOVER/*** This function is called to recover from a malloc failure that occured** within SQLite. **** This function is *not* threadsafe. Calling this from within a threaded** application when threads other than the caller have used SQLite is ** dangerous and will almost certainly result in malfunctions.*/int sqlite3_global_recover(){  int rc = SQLITE_OK;  if( sqlite3_malloc_failed ){    sqlite3 *db;    int i;    sqlite3_malloc_failed = 0;    for(db=pDbList; db; db=db->pNext ){      sqlite3ExpirePreparedStatements(db);      for(i=0; i<db->nDb; i++){        Btree *pBt = db->aDb[i].pBt;        if( pBt && (rc=sqlite3BtreeReset(pBt)) ){          goto recover_out;        }      }       db->autoCommit = 1;    }  }recover_out:  if( rc!=SQLITE_OK ){    sqlite3_malloc_failed = 1;  }  return rc;}#endif/*** Test to see whether or not the database connection is in autocommit** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on** by default.  Autocommit is disabled by a BEGIN statement and reenabled** by the next COMMIT or ROLLBACK.********* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE *******/int sqlite3_get_autocommit(sqlite3 *db){  return db->autoCommit;}

⌨️ 快捷键说明

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