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

📄 main.c

📁 一个小型的嵌入式数据库
💻 C
📖 第 1 页 / 共 3 页
字号:
  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;}/*** Check schema cookies in all databases.  If any cookie is out** of date, return 0.  If all schema cookies are current, return 1.*/static int schemaIsValid(sqlite3 *db){  int iDb;  int rc;  BtCursor *curTemp;  int cookie;  int allOk = 1;  for(iDb=0; allOk && iDb<db->nDb; iDb++){    Btree *pBt;    pBt = db->aDb[iDb].pBt;    if( pBt==0 ) continue;    rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);    if( rc==SQLITE_OK ){      rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);      if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){        allOk = 0;      }      sqlite3BtreeCloseCursor(curTemp);    }  }  return allOk;}/*** Compile the UTF-8 encoded SQL statement zSql into a statement handle.*/int sqlite3_prepare(  sqlite3 *db,              /* Database handle. */  const char *zSql,         /* UTF-8 encoded SQL statement. */  int nBytes,               /* Length of zSql in bytes. */  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */  const char** pzTail       /* OUT: End of parsed string */){  Parse sParse;  char *zErrMsg = 0;  int rc = SQLITE_OK;  if( sqlite3_malloc_failed ){    return SQLITE_NOMEM;  }  assert( ppStmt );  *ppStmt = 0;  if( sqlite3SafetyOn(db) ){    return SQLITE_MISUSE;  }  memset(&sParse, 0, sizeof(sParse));  sParse.db = db;  sqlite3RunParser(&sParse, zSql, &zErrMsg);  if( sqlite3_malloc_failed ){    rc = SQLITE_NOMEM;    sqlite3RollbackAll(db);    sqlite3ResetInternalSchema(db, 0);    db->flags &= ~SQLITE_InTrans;    goto prepare_out;  }  if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;  if( sParse.rc!=SQLITE_OK && sParse.checkSchema && !schemaIsValid(db) ){    sParse.rc = SQLITE_SCHEMA;  }  if( sParse.rc==SQLITE_SCHEMA ){    sqlite3ResetInternalSchema(db, 0);  }  if( pzTail ) *pzTail = sParse.zTail;  rc = sParse.rc;#ifndef SQLITE_OMIT_EXPLAIN  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){    sqlite3VdbeSetNumCols(sParse.pVdbe, 5);    sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);    sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);    sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);    sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);    sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);  } #endifprepare_out:  if( sqlite3SafetyOff(db) ){    rc = SQLITE_MISUSE;  }  if( rc==SQLITE_OK ){    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;  }else if( sParse.pVdbe ){    sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);  }  if( zErrMsg ){    sqlite3Error(db, rc, "%s", zErrMsg);    sqliteFree(zErrMsg);  }else{    sqlite3Error(db, rc, 0);  }  return rc;}#ifndef SQLITE_OMIT_UTF16/*** Compile the UTF-16 encoded SQL statement zSql into a statement handle.*/int sqlite3_prepare16(  sqlite3 *db,              /* Database handle. */   const void *zSql,         /* UTF-8 encoded SQL statement. */  int nBytes,               /* Length of zSql in bytes. */  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */  const void **pzTail       /* OUT: End of parsed string */){  /* This function currently works by first transforming the UTF-16  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The  ** tricky bit is figuring out the pointer to return in *pzTail.  */  char const *zSql8 = 0;  char const *zTail8 = 0;  int rc;  sqlite3_value *pTmp;  if( sqlite3SafetyCheck(db) ){    return SQLITE_MISUSE;  }  pTmp = sqlite3GetTransientValue(db);  sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);  zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);  if( !zSql8 ){    sqlite3Error(db, SQLITE_NOMEM, 0);    return SQLITE_NOMEM;  }  rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);  if( zTail8 && pzTail ){    /* If sqlite3_prepare returns a tail pointer, we calculate the    ** equivalent pointer into the UTF-16 string by counting the unicode    ** characters between zSql8 and zTail8, and then returning a pointer    ** the same number of characters into the UTF-16 string.    */    int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);    *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);  }   return rc;}#endif /* SQLITE_OMIT_UTF16 *//*** 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. The fourth argument, "def_enc" is one of the TEXT_*** macros from sqliteInt.h. If we end up creating a new database file** (not opening an existing one), the text encoding of the database** will be set to this value.*/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;  }  db->aDb[0].zName = "main";  db->aDb[1].zName = "temp";  /* 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].safety_level = 3;  db->aDb[1].safety_level = 1;  /* 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;  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 */

⌨️ 快捷键说明

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