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

📄 main.c

📁 一个小型的嵌入式数据库
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** 2001 September 15**** The author disclaims copyright to this source code.  In place of** a legal notice, here is a blessing:****    May you do good and not evil.**    May you find forgiveness for yourself and forgive others.**    May you share freely, never taking more than you give.***************************************************************************** Main file for the SQLite library.  The routines in this file** implement the programmer interface to the library.  Routines in** other files are for internal use by SQLite and should not be** accessed by users of the library.**** $Id: main.c,v 1.281 2005/02/19 08:18:06 danielk1977 Exp $*/#include "sqliteInt.h"#include "os.h"#include <ctype.h>/*** The following constant value is used by the SQLITE_BIGENDIAN and** SQLITE_LITTLEENDIAN macros.*/const int sqlite3one = 1;/*** Fill the InitData structure with an error message that indicates** that the database is corrupt.*/static void corruptSchema(InitData *pData, const char *zExtra){  if( !sqlite3_malloc_failed ){    sqlite3SetString(pData->pzErrMsg, "malformed database schema",       zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);  }}/*** This is the callback routine for the code that initializes the** database.  See sqlite3Init() below for additional information.** This routine is also called from the OP_ParseSchema opcode of the VDBE.**** Each callback contains the following information:****     argv[0] = name of thing being created**     argv[1] = root page number for table or index.  NULL for trigger or view.**     argv[2] = SQL text for the CREATE statement.**     argv[3] = "1" for temporary files, "0" for main database, "2" or more**               for auxiliary database files.***/int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){  InitData *pData = (InitData*)pInit;  sqlite3 *db = pData->db;  int iDb;  assert( argc==4 );  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */  if( argv[1]==0 || argv[3]==0 ){    corruptSchema(pData, 0);    return 1;  }  iDb = atoi(argv[3]);  assert( iDb>=0 && iDb<db->nDb );  if( argv[2] && argv[2][0] ){    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.    ** But because db->init.busy is set to 1, no VDBE code is generated    ** or executed.  All the parser does is build the internal data    ** structures that describe the table, index, or view.    */    char *zErr;    int rc;    assert( db->init.busy );    db->init.iDb = iDb;    db->init.newTnum = atoi(argv[1]);    rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);    db->init.iDb = 0;    if( SQLITE_OK!=rc ){      corruptSchema(pData, zErr);      sqlite3_free(zErr);      return rc;    }  }else{    /* If the SQL column is blank it means this is an index that    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE    ** constraint for a CREATE TABLE.  The index should have already    ** been created when we processed the CREATE TABLE.  All we have    ** to do here is record the root page number for that index.    */    Index *pIndex;    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);    if( pIndex==0 || pIndex->tnum!=0 ){      /* This can occur if there exists an index on a TEMP table which      ** has the same name as another index on a permanent index.  Since      ** the permanent table is hidden by the TEMP table, we can also      ** safely ignore the index on the permanent table.      */      /* Do Nothing */;    }else{      pIndex->tnum = atoi(argv[1]);    }  }  return 0;}/*** Attempt to read the database schema and initialize internal** data structures for a single database file.  The index of the** database file is given by iDb.  iDb==0 is used for the main** database.  iDb==1 should never be used.  iDb>=2 is used for** auxiliary databases.  Return one of the SQLITE_ error codes to** indicate success or failure.*/static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){  int rc;  BtCursor *curMain;  int size;  Table *pTab;  char const *azArg[5];  char zDbNum[30];  int meta[10];  InitData initData;  char const *zMasterSchema;  char const *zMasterName;  /*  ** The master database table has a structure like this  */  static const char master_schema[] =      "CREATE TABLE sqlite_master(\n"     "  type text,\n"     "  name text,\n"     "  tbl_name text,\n"     "  rootpage integer,\n"     "  sql text\n"     ")"  ;  static const char temp_master_schema[] =      "CREATE TEMP TABLE sqlite_temp_master(\n"     "  type text,\n"     "  name text,\n"     "  tbl_name text,\n"     "  rootpage integer,\n"     "  sql text\n"     ")"  ;  assert( iDb>=0 && iDb<db->nDb );  /* zMasterSchema and zInitScript are set to point at the master schema  ** and initialisation script appropriate for the database being  ** initialised. zMasterName is the name of the master table.  */  if( iDb==1 ){    zMasterSchema = temp_master_schema;    zMasterName = TEMP_MASTER_NAME;  }else{    zMasterSchema = master_schema;    zMasterName = MASTER_NAME;  }  /* Construct the schema tables.  */  sqlite3SafetyOff(db);  azArg[0] = zMasterName;  azArg[1] = "1";  azArg[2] = zMasterSchema;  sprintf(zDbNum, "%d", iDb);  azArg[3] = zDbNum;  azArg[4] = 0;  initData.db = db;  initData.pzErrMsg = pzErrMsg;  rc = sqlite3InitCallback(&initData, 4, (char **)azArg, 0);  if( rc!=SQLITE_OK ){    sqlite3SafetyOn(db);    return rc;  }  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);  if( pTab ){    pTab->readOnly = 1;  }  sqlite3SafetyOn(db);  /* Create a cursor to hold the database open  */  if( db->aDb[iDb].pBt==0 ){    if( iDb==1 ) DbSetProperty(db, 1, DB_SchemaLoaded);    return SQLITE_OK;  }  rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);  if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){    sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);    return rc;  }  /* Get the database meta information.  **  ** Meta values are as follows:  **    meta[0]   Schema cookie.  Changes with each schema change.  **    meta[1]   File format of schema layer.  **    meta[2]   Size of the page cache.  **    meta[3]   Use freelist if 0.  Autovacuum if greater than zero.  **    meta[4]   Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE  **    meta[5]   The user cookie. Used by the application.  **    meta[6]     **    meta[7]  **    meta[8]  **    meta[9]  **  ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to  ** the possible values of meta[4].  */  if( rc==SQLITE_OK ){    int i;    for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){      rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);    }    if( rc ){      sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);      sqlite3BtreeCloseCursor(curMain);      return rc;    }  }else{    memset(meta, 0, sizeof(meta));  }  db->aDb[iDb].schema_cookie = meta[0];  /* If opening a non-empty database, check the text encoding. For the  ** main database, set sqlite3.enc to the encoding of the main database.  ** For an attached db, it is an error if the encoding is not the same  ** as sqlite3.enc.  */  if( meta[4] ){  /* text encoding */    if( iDb==0 ){      /* If opening the main database, set db->enc. */      db->enc = (u8)meta[4];      db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);    }else{      /* If opening an attached database, the encoding much match db->enc */      if( meta[4]!=db->enc ){        sqlite3BtreeCloseCursor(curMain);        sqlite3SetString(pzErrMsg, "attached databases must use the same"            " text encoding as main database", (char*)0);        return SQLITE_ERROR;      }    }  }  size = meta[2];  if( size==0 ){ size = MAX_PAGES; }  db->aDb[iDb].cache_size = size;  if( iDb==0 ){    db->file_format = meta[1];    if( db->file_format==0 ){      /* This happens if the database was initially empty */      db->file_format = 1;    }    if( db->file_format==2 ){      /* File format 2 is treated exactly as file format 1. New       ** databases are created with file format 1.      */       db->file_format = 1;    }  }  /*  ** file_format==1    Version 3.0.0.  ** file_format==2    Version 3.1.3.  **  ** Version 3.0 can only use files with file_format==1. Version 3.1.3  ** can read and write files with file_format==1 or file_format==2.  */  if( meta[1]>2 ){    sqlite3BtreeCloseCursor(curMain);    sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);    return SQLITE_ERROR;  }  sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);  /* Read the schema information out of the schema tables  */  assert( db->init.busy );  if( rc==SQLITE_EMPTY ){    /* For an empty database, there is nothing to read */    rc = SQLITE_OK;  }else{    char *zSql;    zSql = sqlite3MPrintf(        "SELECT name, rootpage, sql, '%s' FROM '%q'.%s",        zDbNum, db->aDb[iDb].zName, zMasterName);    sqlite3SafetyOff(db);    rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);    sqlite3SafetyOn(db);    sqliteFree(zSql);    sqlite3BtreeCloseCursor(curMain);  }  if( sqlite3_malloc_failed ){    sqlite3SetString(pzErrMsg, "out of memory", (char*)0);    rc = SQLITE_NOMEM;    sqlite3ResetInternalSchema(db, 0);  }  if( rc==SQLITE_OK ){    DbSetProperty(db, iDb, DB_SchemaLoaded);  }else{    sqlite3ResetInternalSchema(db, iDb);  }  return rc;}/*** Initialize all database files - the main database file, the file** used to store temporary tables, and any additional database files** created using ATTACH statements.  Return a success code.  If an** error occurs, write an error message into *pzErrMsg.**** After the database is initialized, the SQLITE_Initialized** bit is set in the flags field of the sqlite structure. */int sqlite3Init(sqlite3 *db, char **pzErrMsg){  int i, rc;    if( db->init.busy ) return SQLITE_OK;  assert( (db->flags & SQLITE_Initialized)==0 );  rc = SQLITE_OK;  db->init.busy = 1;  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;    rc = sqlite3InitOne(db, i, pzErrMsg);    if( rc ){      sqlite3ResetInternalSchema(db, i);    }  }  /* Once all the other databases have been initialised, load the schema  ** for the TEMP database. This is loaded last, as the TEMP database  ** schema may contain references to objects in other databases.  */  if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){    rc = sqlite3InitOne(db, 1, pzErrMsg);    if( rc ){      sqlite3ResetInternalSchema(db, 1);    }  }  db->init.busy = 0;  if( rc==SQLITE_OK ){    db->flags |= SQLITE_Initialized;    sqlite3CommitInternalChanges(db);  }  if( rc!=SQLITE_OK ){    db->flags &= ~SQLITE_Initialized;  }  return rc;}/*** This routine is a no-op if the database schema is already initialised.** Otherwise, the schema is loaded. An error code is returned.*/int sqlite3ReadSchema(Parse *pParse){  int rc = SQLITE_OK;  sqlite3 *db = pParse->db;  if( !db->init.busy ){    if( (db->flags & SQLITE_Initialized)==0 ){      rc = sqlite3Init(db, &pParse->zErrMsg);    }  }  assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy );  if( rc!=SQLITE_OK ){    pParse->rc = rc;    pParse->nErr++;  }  return rc;}/*** The version of the library*/const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";const char sqlite3_version[] = SQLITE_VERSION;const char *sqlite3_libversion(void){ return sqlite3_version; }int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }/*** This is the default collating function named "BINARY" which is always** available.*/static int binCollFunc(  void *NotUsed,  int nKey1, const void *pKey1,  int nKey2, const void *pKey2){  int rc, n;  n = nKey1<nKey2 ? nKey1 : nKey2;  rc = memcmp(pKey1, pKey2, n);  if( rc==0 ){    rc = nKey1 - nKey2;  }  return rc;}/*** Another built-in collating sequence: NOCASE. **** This collating sequence is intended to be used for "case independant** comparison". SQLite's knowledge of upper and lower case equivalents** extends only to the 26 characters used in the English language.**** At the moment there is only a UTF-8 implementation.*/static int nocaseCollatingFunc(  void *NotUsed,  int nKey1, const void *pKey1,  int nKey2, const void *pKey2){  int r = sqlite3StrNICmp(      (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);  if( 0==r ){    r = nKey1-nKey2;  }  return r;}/*** Return the ROWID of the most recent insert*/sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){  return db->lastRowid;}/*** Return the number of changes in the most recent call to sqlite3_exec().*/int sqlite3_changes(sqlite3 *db){  return db->nChange;}/*** Return the number of changes since the database handle was opened.*/int sqlite3_total_changes(sqlite3 *db){  return db->nTotalChange;}/*** Close an existing SQLite database*/int sqlite3_close(sqlite3 *db){  HashElem *i;  int j;  if( !db ){    return SQLITE_OK;  }  if( sqlite3SafetyCheck(db) ){    return SQLITE_MISUSE;  }  /* If there are any outstanding VMs, return SQLITE_BUSY. */  if( db->pVdbe ){    sqlite3Error(db, SQLITE_BUSY,         "Unable to close due to unfinalised statements");

⌨️ 快捷键说明

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