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

📄 main.c

📁 sqlite数据库管理系统开放源码
💻 C
📖 第 1 页 / 共 3 页
字号:
  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 = sqliteInitOne(db, i, pzErrMsg);    if( rc ){      sqliteResetInternalSchema(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 = sqliteInitOne(db, 1, pzErrMsg);    if( rc ){      sqliteResetInternalSchema(db, 1);    }  }  db->init.busy = 0;  if( rc==SQLITE_OK ){    db->flags |= SQLITE_Initialized;    sqliteCommitInternalChanges(db);  }  /* If the database is in formats 1 or 2, then upgrade it to  ** version 3.  This will reconstruct all indices.  If the  ** upgrade fails for any reason (ex: out of disk space, database  ** is read only, interrupt received, etc.) then fail the init.  */  if( rc==SQLITE_OK && db->file_format<3 ){    char *zErr = 0;    InitData initData;    int meta[SQLITE_N_BTREE_META];    db->magic = SQLITE_MAGIC_OPEN;    initData.db = db;    initData.pzErrMsg = &zErr;    db->file_format = 3;    rc = sqlite_exec(db,      "BEGIN; SELECT name FROM sqlite_master WHERE type='table';",      upgrade_3_callback,      &initData,      &zErr);    if( rc==SQLITE_OK ){      sqliteBtreeGetMeta(db->aDb[0].pBt, meta);      meta[2] = 4;      sqliteBtreeUpdateMeta(db->aDb[0].pBt, meta);      sqlite_exec(db, "COMMIT", 0, 0, 0);    }    if( rc!=SQLITE_OK ){      sqliteSetString(pzErrMsg,         "unable to upgrade database to the version 2.6 format",        zErr ? ": " : 0, zErr, (char*)0);    }    sqlite_freemem(zErr);  }  if( rc!=SQLITE_OK ){    db->flags &= ~SQLITE_Initialized;  }  return rc;}/*** The version of the library*/const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";const char sqlite_version[] = SQLITE_VERSION;/*** Does the library expect data to be encoded as UTF-8 or iso8859?  The** following global constant always lets us know.*/#ifdef SQLITE_UTF8const char sqlite_encoding[] = "UTF-8";#elseconst char sqlite_encoding[] = "iso8859";#endif/*** Open a new SQLite database.  Construct an "sqlite" structure to define** the state of this database and return a pointer to that structure.**** An attempt is made to initialize the in-memory data structures that** hold the database schema.  But if this fails (because the schema file** is locked) then that step is deferred until the first call to** sqlite_exec().*/sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){  sqlite *db;  int rc, i;  /* Allocate the sqlite data structure */  db = sqliteMalloc( sizeof(sqlite) );  if( pzErrMsg ) *pzErrMsg = 0;  if( db==0 ) goto no_mem_on_open;  db->onError = OE_Default;  db->priorNewRowid = 0;  db->magic = SQLITE_MAGIC_BUSY;  db->nDb = 2;  db->aDb = db->aDbStatic;  /* db->flags |= SQLITE_ShortColNames; */  sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);  for(i=0; i<db->nDb; i++){    sqliteHashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);    sqliteHashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);    sqliteHashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);    sqliteHashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);  }    /* Open the backend database driver */  if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){    db->temp_store = 2;  }  rc = sqliteBtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);  if( rc!=SQLITE_OK ){    switch( rc ){      default: {        sqliteSetString(pzErrMsg, "unable to open database: ",           zFilename, (char*)0);      }    }    sqliteFree(db);    sqliteStrRealloc(pzErrMsg);    return 0;  }  db->aDb[0].zName = "main";  db->aDb[1].zName = "temp";  /* Attempt to read the schema */  sqliteRegisterBuiltinFunctions(db);  rc = sqliteInit(db, pzErrMsg);  db->magic = SQLITE_MAGIC_OPEN;  if( sqlite_malloc_failed ){    sqlite_close(db);    goto no_mem_on_open;  }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){    sqlite_close(db);    sqliteStrRealloc(pzErrMsg);    return 0;  }else if( pzErrMsg ){    sqliteFree(*pzErrMsg);    *pzErrMsg = 0;  }  /* Return a pointer to the newly opened database structure */  return db;no_mem_on_open:  sqliteSetString(pzErrMsg, "out of memory", (char*)0);  sqliteStrRealloc(pzErrMsg);  return 0;}/*** Return the ROWID of the most recent insert*/int sqlite_last_insert_rowid(sqlite *db){  return db->lastRowid;}/*** Return the number of changes in the most recent call to sqlite_exec().*/int sqlite_changes(sqlite *db){  return db->nChange;}/*** Return the number of changes produced by the last INSERT, UPDATE, or** DELETE statement to complete execution. The count does not include** changes due to SQL statements executed in trigger programs that were** triggered by that statement*/int sqlite_last_statement_changes(sqlite *db){  return db->lsChange;}/*** Close an existing SQLite database*/void sqlite_close(sqlite *db){  HashElem *i;  int j;  db->want_to_close = 1;  if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){    /* printf("DID NOT CLOSE\n"); fflush(stdout); */    return;  }  db->magic = SQLITE_MAGIC_CLOSED;  for(j=0; j<db->nDb; j++){    struct Db *pDb = &db->aDb[j];    if( pDb->pBt ){      sqliteBtreeClose(pDb->pBt);      pDb->pBt = 0;    }  }  sqliteResetInternalSchema(db, 0);  assert( db->nDb<=2 );  assert( db->aDb==db->aDbStatic );  for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){    FuncDef *pFunc, *pNext;    for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){      pNext = pFunc->pNext;      sqliteFree(pFunc);    }  }  sqliteHashClear(&db->aFunc);  sqliteFree(db);}/*** Rollback all database files.*/void sqliteRollbackAll(sqlite *db){  int i;  for(i=0; i<db->nDb; i++){    if( db->aDb[i].pBt ){      sqliteBtreeRollback(db->aDb[i].pBt);      db->aDb[i].inTrans = 0;    }  }  sqliteResetInternalSchema(db, 0);  /* sqliteRollbackInternalChanges(db); */}/*** Execute SQL code.  Return one of the SQLITE_ success/failure** codes.  Also write an error message into memory obtained from** malloc() and make *pzErrMsg point to that message.**** If the SQL is a query, then for each row in the query result** the xCallback() function is called.  pArg becomes the first** argument to xCallback().  If xCallback=NULL then no callback** is invoked, even for queries.*/int sqlite_exec(  sqlite *db,                 /* The database on which the SQL executes */  const char *zSql,           /* The SQL to be executed */  sqlite_callback xCallback,  /* Invoke this callback routine */  void *pArg,                 /* First argument to xCallback() */  char **pzErrMsg             /* Write error messages here */){  int rc = SQLITE_OK;  const char *zLeftover;  sqlite_vm *pVm;  int nRetry = 0;  int nChange = 0;  int nCallback;  if( zSql==0 ) return SQLITE_OK;  while( rc==SQLITE_OK && zSql[0] ){    pVm = 0;    rc = sqlite_compile(db, zSql, &zLeftover, &pVm, pzErrMsg);    if( rc!=SQLITE_OK ){      assert( pVm==0 || sqlite_malloc_failed );      return rc;    }    if( pVm==0 ){      /* This happens if the zSql input contained only whitespace */      break;    }    db->nChange += nChange;    nCallback = 0;    while(1){      int nArg;      char **azArg, **azCol;      rc = sqlite_step(pVm, &nArg, (const char***)&azArg,(const char***)&azCol);      if( rc==SQLITE_ROW ){        if( xCallback!=0 && xCallback(pArg, nArg, azArg, azCol) ){          sqlite_finalize(pVm, 0);          return SQLITE_ABORT;        }        nCallback++;      }else{        if( rc==SQLITE_DONE && nCallback==0          && (db->flags & SQLITE_NullCallback)!=0 && xCallback!=0 ){          xCallback(pArg, nArg, azArg, azCol);        }        rc = sqlite_finalize(pVm, pzErrMsg);        if( rc==SQLITE_SCHEMA && nRetry<2 ){          nRetry++;          rc = SQLITE_OK;          break;        }        if( db->pVdbe==0 ){          nChange = db->nChange;        }        nRetry = 0;        zSql = zLeftover;        while( isspace(zSql[0]) ) zSql++;        break;      }    }  }  return rc;}/*** Compile a single statement of SQL into a virtual machine.  Return one** of the SQLITE_ success/failure codes.  Also write an error message into** memory obtained from malloc() and make *pzErrMsg point to that message.*/int sqlite_compile(  sqlite *db,                 /* The database on which the SQL executes */  const char *zSql,           /* The SQL to be executed */  const char **pzTail,        /* OUT: Next statement after the first */  sqlite_vm **ppVm,           /* OUT: The virtual machine */  char **pzErrMsg             /* OUT: Write error messages here */){  Parse sParse;  if( pzErrMsg ) *pzErrMsg = 0;  if( sqliteSafetyOn(db) ) goto exec_misuse;  if( !db->init.busy ){    if( (db->flags & SQLITE_Initialized)==0 ){      int rc, cnt = 1;      while( (rc = sqliteInit(db, pzErrMsg))==SQLITE_BUSY         && db->xBusyCallback         && db->xBusyCallback(db->pBusyArg, "", cnt++)!=0 ){}      if( rc!=SQLITE_OK ){        sqliteStrRealloc(pzErrMsg);        sqliteSafetyOff(db);        return rc;      }      if( pzErrMsg ){        sqliteFree(*pzErrMsg);        *pzErrMsg = 0;      }    }    if( db->file_format<3 ){      sqliteSafetyOff(db);      sqliteSetString(pzErrMsg, "obsolete database file format", (char*)0);      return SQLITE_ERROR;    }  }  assert( (db->flags & SQLITE_Initialized)!=0 || db->init.busy );  if( db->pVdbe==0 ){ db->nChange = 0; }  memset(&sParse, 0, sizeof(sParse));  sParse.db = db;  sqliteRunParser(&sParse, zSql, pzErrMsg);  if( db->xTrace && !db->init.busy ){    /* Trace only the statment that was compiled.    ** Make a copy of that part of the SQL string since zSQL is const    ** and we must pass a zero terminated string to the trace function    ** The copy is unnecessary if the tail pointer is pointing at the    ** beginnig or end of the SQL string.    */    if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){      char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql);      if( tmpSql ){        db->xTrace(db->pTraceArg, tmpSql);        free(tmpSql);      }else{        /* If a memory error occurred during the copy,        ** trace entire SQL string and fall through to the        ** sqlite_malloc_failed test to report the error.        */        db->xTrace(db->pTraceArg, zSql);       }    }else{      db->xTrace(db->pTraceArg, zSql);     }  }  if( sqlite_malloc_failed ){    sqliteSetString(pzErrMsg, "out of memory", (char*)0);    sParse.rc = SQLITE_NOMEM;    sqliteRollbackAll(db);    sqliteResetInternalSchema(db, 0);    db->flags &= ~SQLITE_InTrans;  }  if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;  if( sParse.rc!=SQLITE_OK && pzErrMsg && *pzErrMsg==0 ){    sqliteSetString(pzErrMsg, sqlite_error_string(sParse.rc), (char*)0);  }  sqliteStrRealloc(pzErrMsg);  if( sParse.rc==SQLITE_SCHEMA ){

⌨️ 快捷键说明

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