ext/pdo_sqlite/sqlite/src/prepare.c
/*
** 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.
*/
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.
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;
  Db *pDb;
  char const *azArg[4];
  int meta[10];
  InitData initData;
  char const *zMasterSchema;
  char const *zMasterName = SCHEMA_TABLE(iDb);
  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"
     ")"
  ;
#ifndef SQLITE_OMIT_TEMPDB
  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"
     ")"
  ;
#else
  #define temp_master_schema 0
#endif
  assert( iDb>=0 && iDb<db->nDb );
  assert( db->aDb[iDb].pSchema );
  if( !OMIT_TEMPDB && iDb==1 ){
    zMasterSchema = temp_master_schema;
  }else{
    zMasterSchema = master_schema;
  }
  zMasterName = SCHEMA_TABLE(iDb);
  sqlite3SafetyOff(db);
  azArg[0] = zMasterName;
  azArg[1] = "1";
  azArg[2] = zMasterSchema;
  azArg[3] = 0;
  initData.db = db;
  initData.iDb = iDb;
  initData.pzErrMsg = pzErrMsg;
  rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
  if( rc ){
    sqlite3SafetyOn(db);
    return initData.rc;
  }
  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
  if( pTab ){
    pTab->readOnly = 1;
  }
  sqlite3SafetyOn(db);
  pDb = &db->aDb[iDb];
  if( pDb->pBt==0 ){
    if( !OMIT_TEMPDB && iDb==1 ){
      DbSetProperty(db, 1, DB_SchemaLoaded);
    }
    return SQLITE_OK;
  }
  rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
  if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
    sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
    return rc;
  }
  if( rc==SQLITE_OK ){
    int i;
    for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
      rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
    }
    if( rc ){
      sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
      sqlite3BtreeCloseCursor(curMain);
      return rc;
    }
  }else{
    memset(meta, 0, sizeof(meta));
  }
  pDb->pSchema->schema_cookie = meta[0];
  if( meta[4] ){  
    if( iDb==0 ){
      ENC(db) = (u8)meta[4];
      db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
    }else{
      if( meta[4]!=ENC(db) ){
        sqlite3BtreeCloseCursor(curMain);
        sqlite3SetString(pzErrMsg, "attached databases must use the same"
            " text encoding as main database", (char*)0);
        return SQLITE_ERROR;
      }
    }
  }else{
    DbSetProperty(db, iDb, DB_Empty);
  }
  pDb->pSchema->enc = ENC(db);
  size = meta[2];
  if( size==0 ){ size = MAX_PAGES; }
  pDb->pSchema->cache_size = size;
  sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
  pDb->pSchema->file_format = meta[1];
  if( pDb->pSchema->file_format==0 ){
    pDb->pSchema->file_format = 1;
  }
  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
    sqlite3BtreeCloseCursor(curMain);
    sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
    return SQLITE_ERROR;
  }
  assert( db->init.busy );
  if( rc==SQLITE_EMPTY ){
    rc = SQLITE_OK;
  }else{
    char *zSql;
    zSql = sqlite3MPrintf(
        "SELECT name, rootpage, sql FROM '%q'.%s",
        db->aDb[iDb].zName, zMasterName);
    sqlite3SafetyOff(db);
    rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
    if( rc==SQLITE_ABORT ) rc = initData.rc;
    sqlite3SafetyOn(db);
    sqliteFree(zSql);
#ifndef SQLITE_OMIT_ANALYZE
    if( rc==SQLITE_OK ){
      sqlite3AnalysisLoad(db, iDb);
    }
#endif
    sqlite3BtreeCloseCursor(curMain);
  }
  if( sqlite3MallocFailed() ){
    rc = SQLITE_NOMEM;
    sqlite3ResetInternalSchema(db, 0);
  }
  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
    DbSetProperty(db, iDb, DB_SchemaLoaded);
    rc = SQLITE_OK;
  }
  return rc;
}
static int sqlite3InitOne ( sqlite3 * db , int iDb , char * * pzErrMsg ) { int rc ; BtCursor * curMain ; int size ; Table * pTab ; Db * pDb ; char const * azArg [ 4 ] ; int meta [ 10 ] ; InitData initData ; char const * zMasterSchema ; char const * zMasterName = SCHEMA_TABLE ( iDb ) ; static const char master_schema [ ] = " \n " " \n " " \n " " \n " " \n " " \n " " " ; #ifndef SQLITE_OMIT_TEMPDB static const char temp_master_schema [ ] = " \n " " \n " " \n " " \n " " \n " " \n " " " ; #else #define temp_master_schema  0 #endif assert ( iDb >= 0 && iDb < db -> nDb ) ; assert ( db -> aDb [ iDb ] . pSchema ) ; if ( ! OMIT_TEMPDB && iDb == 1 ) { zMasterSchema = temp_master_schema ; } else { zMasterSchema = master_schema ; } zMasterName = SCHEMA_TABLE ( iDb ) ; sqlite3SafetyOff ( db ) ; azArg [ 0 ] = zMasterName ; azArg [ 1 ] = " " ; azArg [ 2 ] = zMasterSchema ; azArg [ 3 ] = 0 ; initData . db = db ; initData . iDb = iDb ; initData . pzErrMsg = pzErrMsg ; rc = sqlite3InitCallback ( & initData , 3 , ( char * * ) azArg , 0 ) ; if ( rc ) { sqlite3SafetyOn ( db ) ; return initData . rc ; } pTab = sqlite3FindTable ( db , zMasterName , db -> aDb [ iDb ] . zName ) ; if ( pTab ) { pTab -> readOnly = 1 ; } sqlite3SafetyOn ( db ) ; pDb = & db -> aDb [ iDb ] ; if ( pDb -> pBt == 0 ) { if ( ! OMIT_TEMPDB && iDb == 1 ) { DbSetProperty ( db , 1 , DB_SchemaLoaded ) ; } return SQLITE_OK ; } rc = sqlite3BtreeCursor ( pDb -> pBt , MASTER_ROOT , 0 , 0 , 0 , & curMain ) ; if ( rc != SQLITE_OK && rc != SQLITE_EMPTY ) { sqlite3SetString ( pzErrMsg , sqlite3ErrStr ( rc ) , ( char * ) 0 ) ; return rc ; } if ( rc == SQLITE_OK ) { int i ; for ( i = 0 ; rc == SQLITE_OK && i < sizeof ( meta ) / sizeof ( meta [ 0 ] ) ; i ++ ) { rc = sqlite3BtreeGetMeta ( pDb -> pBt , i + 1 , ( u32 * ) & meta [ i ] ) ; } if ( rc ) { sqlite3SetString ( pzErrMsg , sqlite3ErrStr ( rc ) , ( char * ) 0 ) ; sqlite3BtreeCloseCursor ( curMain ) ; return rc ; } } else { memset ( meta , 0 , sizeof ( meta ) ) ; } pDb -> pSchema -> schema_cookie = meta [ 0 ] ; if ( meta [ 4 ] ) { if ( iDb == 0 ) { ENC ( db ) = ( u8 ) meta [ 4 ] ; db -> pDfltColl = sqlite3FindCollSeq ( db , SQLITE_UTF8 , " " , 6 , 0 ) ; } else { if ( meta [ 4 ] != ENC ( db ) ) { sqlite3BtreeCloseCursor ( curMain ) ; sqlite3SetString ( pzErrMsg , " " " " , ( char * ) 0 ) ; return SQLITE_ERROR ; } } } else { DbSetProperty ( db , iDb , DB_Empty ) ; } pDb -> pSchema -> enc = ENC ( db ) ; size = meta [ 2 ] ; if ( size == 0 ) { size = MAX_PAGES ; } pDb -> pSchema -> cache_size = size ; sqlite3BtreeSetCacheSize ( pDb -> pBt , pDb -> pSchema -> cache_size ) ; pDb -> pSchema -> file_format = meta [ 1 ] ; if ( pDb -> pSchema -> file_format == 0 ) { pDb -> pSchema -> file_format = 1 ; } if ( pDb -> pSchema -> file_format > SQLITE_MAX_FILE_FORMAT ) { sqlite3BtreeCloseCursor ( curMain ) ; sqlite3SetString ( pzErrMsg , " " , ( char * ) 0 ) ; return SQLITE_ERROR ; } assert ( db -> init . busy ) ; if ( rc == SQLITE_EMPTY ) { rc = SQLITE_OK ; } else { char * zSql ; zSql = sqlite3MPrintf ( " " , db -> aDb [ iDb ] . zName , zMasterName ) ; sqlite3SafetyOff ( db ) ; rc = sqlite3_exec ( db , zSql , sqlite3InitCallback , & initData , 0 ) ; if ( rc == SQLITE_ABORT ) rc = initData . rc ; sqlite3SafetyOn ( db ) ; sqliteFree ( zSql ) ; #ifndef SQLITE_OMIT_ANALYZE if ( rc == SQLITE_OK ) { sqlite3AnalysisLoad ( db , iDb ) ; } #endif sqlite3BtreeCloseCursor ( curMain ) ; } if ( sqlite3MallocFailed ( ) ) { rc = SQLITE_NOMEM ; sqlite3ResetInternalSchema ( db , 0 ) ; } if ( rc == SQLITE_OK || ( db -> flags & SQLITE_RecoveryMode ) ) { DbSetProperty ( db , iDb , DB_SchemaLoaded ) ; rc = SQLITE_OK ; } return rc ; }
Attempt to read the database schema and initialize internal
data structures for a single database file.
Attempt to read the database schema and initialize internal data structures for a single database file .
/*
  ** The master database table has a structure like this
  */ /* 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.
  */ /* Construct the schema tables.  */ /* Create a cursor to hold the database open
  */ /* 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 2:UTF-16LE 3:UTF-16BE
  **    meta[5]   The user cookie. Used by the application.
  **    meta[6]   
  **    meta[7]
  **    meta[8]
  **    meta[9]
  **
  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
  ** the possible values of meta[4].
  */ /* 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.
  */ /* text encoding */ /* If opening the main database, set ENC(db). */ /* If opening an attached database, the encoding much match ENC(db) */ /*
  ** file_format==1    Version 3.0.0.
  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
  */ /* Read the schema information out of the schema tables
  */ /* For an empty database, there is nothing to read */ /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */ /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
    ** the schema loaded, even if errors occured. In this situation the 
    ** current sqlite3_prepare() operation will fail, but the following one
    ** will attempt to compile the supplied statement against whatever subset
    ** of the schema was loaded before the error occured. The primary
    ** purpose of this is to allow access to the sqlite_master table
    ** even when it's contents have been corrupted.
    */