000001  /*
000002  ** 2005 May 25
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** This file contains the implementation of the sqlite3_prepare()
000013  ** interface, and routines that contribute to loading the database schema
000014  ** from disk.
000015  */
000016  #include "sqliteInt.h"
000017  
000018  /*
000019  ** Fill the InitData structure with an error message that indicates
000020  ** that the database is corrupt.
000021  */
000022  static void corruptSchema(
000023    InitData *pData,     /* Initialization context */
000024    const char *zObj,    /* Object being parsed at the point of error */
000025    const char *zExtra   /* Error information */
000026  ){
000027    sqlite3 *db = pData->db;
000028    if( db->mallocFailed ){
000029      pData->rc = SQLITE_NOMEM_BKPT;
000030    }else if( pData->pzErrMsg[0]!=0 ){
000031      /* A error message has already been generated.  Do not overwrite it */
000032    }else if( pData->mInitFlags & INITFLAG_AlterTable ){
000033      *pData->pzErrMsg = sqlite3DbStrDup(db, zExtra);
000034      pData->rc = SQLITE_ERROR;
000035    }else if( db->flags & SQLITE_WriteSchema ){
000036      pData->rc = SQLITE_CORRUPT_BKPT;
000037    }else{
000038      char *z;
000039      if( zObj==0 ) zObj = "?";
000040      z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
000041      if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
000042      *pData->pzErrMsg = z;
000043      pData->rc = SQLITE_CORRUPT_BKPT;
000044    }
000045  }
000046  
000047  /*
000048  ** Check to see if any sibling index (another index on the same table)
000049  ** of pIndex has the same root page number, and if it does, return true.
000050  ** This would indicate a corrupt schema.
000051  */
000052  int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
000053    Index *p;
000054    for(p=pIndex->pTable->pIndex; p; p=p->pNext){
000055      if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
000056    }
000057    return 0;
000058  }
000059  
000060  /* forward declaration */
000061  static int sqlite3Prepare(
000062    sqlite3 *db,              /* Database handle. */
000063    const char *zSql,         /* UTF-8 encoded SQL statement. */
000064    int nBytes,               /* Length of zSql in bytes. */
000065    u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
000066    Vdbe *pReprepare,         /* VM being reprepared */
000067    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000068    const char **pzTail       /* OUT: End of parsed string */
000069  );
000070  
000071  
000072  /*
000073  ** This is the callback routine for the code that initializes the
000074  ** database.  See sqlite3Init() below for additional information.
000075  ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
000076  **
000077  ** Each callback contains the following information:
000078  **
000079  **     argv[0] = type of object: "table", "index", "trigger", or "view".
000080  **     argv[1] = name of thing being created
000081  **     argv[2] = associated table if an index or trigger
000082  **     argv[3] = root page number for table or index. 0 for trigger or view.
000083  **     argv[4] = SQL text for the CREATE statement.
000084  **
000085  */
000086  int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
000087    InitData *pData = (InitData*)pInit;
000088    sqlite3 *db = pData->db;
000089    int iDb = pData->iDb;
000090  
000091    assert( argc==5 );
000092    UNUSED_PARAMETER2(NotUsed, argc);
000093    assert( sqlite3_mutex_held(db->mutex) );
000094    DbClearProperty(db, iDb, DB_Empty);
000095    pData->nInitRow++;
000096    if( db->mallocFailed ){
000097      corruptSchema(pData, argv[1], 0);
000098      return 1;
000099    }
000100  
000101    assert( iDb>=0 && iDb<db->nDb );
000102    if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
000103    if( argv[3]==0 ){
000104      corruptSchema(pData, argv[1], 0);
000105    }else if( sqlite3_strnicmp(argv[4],"create ",7)==0 ){
000106      /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
000107      ** But because db->init.busy is set to 1, no VDBE code is generated
000108      ** or executed.  All the parser does is build the internal data
000109      ** structures that describe the table, index, or view.
000110      */
000111      int rc;
000112      u8 saved_iDb = db->init.iDb;
000113      sqlite3_stmt *pStmt;
000114      TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
000115  
000116      assert( db->init.busy );
000117      db->init.iDb = iDb;
000118      db->init.newTnum = sqlite3Atoi(argv[3]);
000119      db->init.orphanTrigger = 0;
000120      db->init.azInit = argv;
000121      pStmt = 0;
000122      TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
000123      rc = db->errCode;
000124      assert( (rc&0xFF)==(rcp&0xFF) );
000125      db->init.iDb = saved_iDb;
000126      /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
000127      if( SQLITE_OK!=rc ){
000128        if( db->init.orphanTrigger ){
000129          assert( iDb==1 );
000130        }else{
000131          if( rc > pData->rc ) pData->rc = rc;
000132          if( rc==SQLITE_NOMEM ){
000133            sqlite3OomFault(db);
000134          }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
000135            corruptSchema(pData, argv[1], sqlite3_errmsg(db));
000136          }
000137        }
000138      }
000139      sqlite3_finalize(pStmt);
000140    }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
000141      corruptSchema(pData, argv[1], 0);
000142    }else{
000143      /* If the SQL column is blank it means this is an index that
000144      ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
000145      ** constraint for a CREATE TABLE.  The index should have already
000146      ** been created when we processed the CREATE TABLE.  All we have
000147      ** to do here is record the root page number for that index.
000148      */
000149      Index *pIndex;
000150      pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
000151      if( pIndex==0
000152       || sqlite3GetInt32(argv[3],&pIndex->tnum)==0
000153       || pIndex->tnum<2
000154       || sqlite3IndexHasDuplicateRootPage(pIndex)
000155      ){
000156        corruptSchema(pData, argv[1], pIndex?"invalid rootpage":"orphan index");
000157      }
000158    }
000159    return 0;
000160  }
000161  
000162  /*
000163  ** Attempt to read the database schema and initialize internal
000164  ** data structures for a single database file.  The index of the
000165  ** database file is given by iDb.  iDb==0 is used for the main
000166  ** database.  iDb==1 should never be used.  iDb>=2 is used for
000167  ** auxiliary databases.  Return one of the SQLITE_ error codes to
000168  ** indicate success or failure.
000169  */
000170  int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
000171    int rc;
000172    int i;
000173  #ifndef SQLITE_OMIT_DEPRECATED
000174    int size;
000175  #endif
000176    Db *pDb;
000177    char const *azArg[6];
000178    int meta[5];
000179    InitData initData;
000180    const char *zMasterName;
000181    int openedTransaction = 0;
000182  
000183    assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
000184    assert( iDb>=0 && iDb<db->nDb );
000185    assert( db->aDb[iDb].pSchema );
000186    assert( sqlite3_mutex_held(db->mutex) );
000187    assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
000188  
000189    db->init.busy = 1;
000190  
000191    /* Construct the in-memory representation schema tables (sqlite_master or
000192    ** sqlite_temp_master) by invoking the parser directly.  The appropriate
000193    ** table name will be inserted automatically by the parser so we can just
000194    ** use the abbreviation "x" here.  The parser will also automatically tag
000195    ** the schema table as read-only. */
000196    azArg[0] = "table";
000197    azArg[1] = zMasterName = SCHEMA_TABLE(iDb);
000198    azArg[2] = azArg[1];
000199    azArg[3] = "1";
000200    azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
000201                              "rootpage int,sql text)";
000202    azArg[5] = 0;
000203    initData.db = db;
000204    initData.iDb = iDb;
000205    initData.rc = SQLITE_OK;
000206    initData.pzErrMsg = pzErrMsg;
000207    initData.mInitFlags = mFlags;
000208    initData.nInitRow = 0;
000209    sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
000210    if( initData.rc ){
000211      rc = initData.rc;
000212      goto error_out;
000213    }
000214  
000215    /* Create a cursor to hold the database open
000216    */
000217    pDb = &db->aDb[iDb];
000218    if( pDb->pBt==0 ){
000219      assert( iDb==1 );
000220      DbSetProperty(db, 1, DB_SchemaLoaded);
000221      rc = SQLITE_OK;
000222      goto error_out;
000223    }
000224  
000225    /* If there is not already a read-only (or read-write) transaction opened
000226    ** on the b-tree database, open one now. If a transaction is opened, it 
000227    ** will be closed before this function returns.  */
000228    sqlite3BtreeEnter(pDb->pBt);
000229    if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
000230      rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
000231      if( rc!=SQLITE_OK ){
000232        sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
000233        goto initone_error_out;
000234      }
000235      openedTransaction = 1;
000236    }
000237  
000238    /* Get the database meta information.
000239    **
000240    ** Meta values are as follows:
000241    **    meta[0]   Schema cookie.  Changes with each schema change.
000242    **    meta[1]   File format of schema layer.
000243    **    meta[2]   Size of the page cache.
000244    **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
000245    **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
000246    **    meta[5]   User version
000247    **    meta[6]   Incremental vacuum mode
000248    **    meta[7]   unused
000249    **    meta[8]   unused
000250    **    meta[9]   unused
000251    **
000252    ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
000253    ** the possible values of meta[4].
000254    */
000255    for(i=0; i<ArraySize(meta); i++){
000256      sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
000257    }
000258    if( (db->flags & SQLITE_ResetDatabase)!=0 ){
000259      memset(meta, 0, sizeof(meta));
000260    }
000261    pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
000262  
000263    /* If opening a non-empty database, check the text encoding. For the
000264    ** main database, set sqlite3.enc to the encoding of the main database.
000265    ** For an attached db, it is an error if the encoding is not the same
000266    ** as sqlite3.enc.
000267    */
000268    if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
000269      if( iDb==0 ){
000270  #ifndef SQLITE_OMIT_UTF16
000271        u8 encoding;
000272        /* If opening the main database, set ENC(db). */
000273        encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
000274        if( encoding==0 ) encoding = SQLITE_UTF8;
000275        ENC(db) = encoding;
000276  #else
000277        ENC(db) = SQLITE_UTF8;
000278  #endif
000279      }else{
000280        /* If opening an attached database, the encoding much match ENC(db) */
000281        if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
000282          sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
000283              " text encoding as main database");
000284          rc = SQLITE_ERROR;
000285          goto initone_error_out;
000286        }
000287      }
000288    }else{
000289      DbSetProperty(db, iDb, DB_Empty);
000290    }
000291    pDb->pSchema->enc = ENC(db);
000292  
000293    if( pDb->pSchema->cache_size==0 ){
000294  #ifndef SQLITE_OMIT_DEPRECATED
000295      size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
000296      if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
000297      pDb->pSchema->cache_size = size;
000298  #else
000299      pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
000300  #endif
000301      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
000302    }
000303  
000304    /*
000305    ** file_format==1    Version 3.0.0.
000306    ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
000307    ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
000308    ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
000309    */
000310    pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
000311    if( pDb->pSchema->file_format==0 ){
000312      pDb->pSchema->file_format = 1;
000313    }
000314    if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
000315      sqlite3SetString(pzErrMsg, db, "unsupported file format");
000316      rc = SQLITE_ERROR;
000317      goto initone_error_out;
000318    }
000319  
000320    /* Ticket #2804:  When we open a database in the newer file format,
000321    ** clear the legacy_file_format pragma flag so that a VACUUM will
000322    ** not downgrade the database and thus invalidate any descending
000323    ** indices that the user might have created.
000324    */
000325    if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
000326      db->flags &= ~(u64)SQLITE_LegacyFileFmt;
000327    }
000328  
000329    /* Read the schema information out of the schema tables
000330    */
000331    assert( db->init.busy );
000332    {
000333      char *zSql;
000334      zSql = sqlite3MPrintf(db, 
000335          "SELECT*FROM\"%w\".%s ORDER BY rowid",
000336          db->aDb[iDb].zDbSName, zMasterName);
000337  #ifndef SQLITE_OMIT_AUTHORIZATION
000338      {
000339        sqlite3_xauth xAuth;
000340        xAuth = db->xAuth;
000341        db->xAuth = 0;
000342  #endif
000343        rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
000344  #ifndef SQLITE_OMIT_AUTHORIZATION
000345        db->xAuth = xAuth;
000346      }
000347  #endif
000348      if( rc==SQLITE_OK ) rc = initData.rc;
000349      sqlite3DbFree(db, zSql);
000350  #ifndef SQLITE_OMIT_ANALYZE
000351      if( rc==SQLITE_OK ){
000352        sqlite3AnalysisLoad(db, iDb);
000353      }
000354  #endif
000355    }
000356    if( db->mallocFailed ){
000357      rc = SQLITE_NOMEM_BKPT;
000358      sqlite3ResetAllSchemasOfConnection(db);
000359    }
000360    if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
000361      /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
000362      ** the schema loaded, even if errors occurred. In this situation the 
000363      ** current sqlite3_prepare() operation will fail, but the following one
000364      ** will attempt to compile the supplied statement against whatever subset
000365      ** of the schema was loaded before the error occurred. The primary
000366      ** purpose of this is to allow access to the sqlite_master table
000367      ** even when its contents have been corrupted.
000368      */
000369      DbSetProperty(db, iDb, DB_SchemaLoaded);
000370      rc = SQLITE_OK;
000371    }
000372  
000373    /* Jump here for an error that occurs after successfully allocating
000374    ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
000375    ** before that point, jump to error_out.
000376    */
000377  initone_error_out:
000378    if( openedTransaction ){
000379      sqlite3BtreeCommit(pDb->pBt);
000380    }
000381    sqlite3BtreeLeave(pDb->pBt);
000382  
000383  error_out:
000384    if( rc ){
000385      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
000386        sqlite3OomFault(db);
000387      }
000388      sqlite3ResetOneSchema(db, iDb);
000389    }
000390    db->init.busy = 0;
000391    return rc;
000392  }
000393  
000394  /*
000395  ** Initialize all database files - the main database file, the file
000396  ** used to store temporary tables, and any additional database files
000397  ** created using ATTACH statements.  Return a success code.  If an
000398  ** error occurs, write an error message into *pzErrMsg.
000399  **
000400  ** After a database is initialized, the DB_SchemaLoaded bit is set
000401  ** bit is set in the flags field of the Db structure. If the database
000402  ** file was of zero-length, then the DB_Empty flag is also set.
000403  */
000404  int sqlite3Init(sqlite3 *db, char **pzErrMsg){
000405    int i, rc;
000406    int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
000407    
000408    assert( sqlite3_mutex_held(db->mutex) );
000409    assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
000410    assert( db->init.busy==0 );
000411    ENC(db) = SCHEMA_ENC(db);
000412    assert( db->nDb>0 );
000413    /* Do the main schema first */
000414    if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
000415      rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
000416      if( rc ) return rc;
000417    }
000418    /* All other schemas after the main schema. The "temp" schema must be last */
000419    for(i=db->nDb-1; i>0; i--){
000420      assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
000421      if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
000422        rc = sqlite3InitOne(db, i, pzErrMsg, 0);
000423        if( rc ) return rc;
000424      }
000425    }
000426    if( commit_internal ){
000427      sqlite3CommitInternalChanges(db);
000428    }
000429    return SQLITE_OK;
000430  }
000431  
000432  /*
000433  ** This routine is a no-op if the database schema is already initialized.
000434  ** Otherwise, the schema is loaded. An error code is returned.
000435  */
000436  int sqlite3ReadSchema(Parse *pParse){
000437    int rc = SQLITE_OK;
000438    sqlite3 *db = pParse->db;
000439    assert( sqlite3_mutex_held(db->mutex) );
000440    if( !db->init.busy ){
000441      rc = sqlite3Init(db, &pParse->zErrMsg);
000442      if( rc!=SQLITE_OK ){
000443        pParse->rc = rc;
000444        pParse->nErr++;
000445      }else if( db->noSharedCache ){
000446        db->mDbFlags |= DBFLAG_SchemaKnownOk;
000447      }
000448    }
000449    return rc;
000450  }
000451  
000452  
000453  /*
000454  ** Check schema cookies in all databases.  If any cookie is out
000455  ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
000456  ** make no changes to pParse->rc.
000457  */
000458  static void schemaIsValid(Parse *pParse){
000459    sqlite3 *db = pParse->db;
000460    int iDb;
000461    int rc;
000462    int cookie;
000463  
000464    assert( pParse->checkSchema );
000465    assert( sqlite3_mutex_held(db->mutex) );
000466    for(iDb=0; iDb<db->nDb; iDb++){
000467      int openedTransaction = 0;         /* True if a transaction is opened */
000468      Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
000469      if( pBt==0 ) continue;
000470  
000471      /* If there is not already a read-only (or read-write) transaction opened
000472      ** on the b-tree database, open one now. If a transaction is opened, it 
000473      ** will be closed immediately after reading the meta-value. */
000474      if( !sqlite3BtreeIsInReadTrans(pBt) ){
000475        rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
000476        if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
000477          sqlite3OomFault(db);
000478        }
000479        if( rc!=SQLITE_OK ) return;
000480        openedTransaction = 1;
000481      }
000482  
000483      /* Read the schema cookie from the database. If it does not match the 
000484      ** value stored as part of the in-memory schema representation,
000485      ** set Parse.rc to SQLITE_SCHEMA. */
000486      sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
000487      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
000488      if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
000489        sqlite3ResetOneSchema(db, iDb);
000490        pParse->rc = SQLITE_SCHEMA;
000491      }
000492  
000493      /* Close the transaction, if one was opened. */
000494      if( openedTransaction ){
000495        sqlite3BtreeCommit(pBt);
000496      }
000497    }
000498  }
000499  
000500  /*
000501  ** Convert a schema pointer into the iDb index that indicates
000502  ** which database file in db->aDb[] the schema refers to.
000503  **
000504  ** If the same database is attached more than once, the first
000505  ** attached database is returned.
000506  */
000507  int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
000508    int i = -1000000;
000509  
000510    /* If pSchema is NULL, then return -1000000. This happens when code in 
000511    ** expr.c is trying to resolve a reference to a transient table (i.e. one
000512    ** created by a sub-select). In this case the return value of this 
000513    ** function should never be used.
000514    **
000515    ** We return -1000000 instead of the more usual -1 simply because using
000516    ** -1000000 as the incorrect index into db->aDb[] is much 
000517    ** more likely to cause a segfault than -1 (of course there are assert()
000518    ** statements too, but it never hurts to play the odds).
000519    */
000520    assert( sqlite3_mutex_held(db->mutex) );
000521    if( pSchema ){
000522      for(i=0; 1; i++){
000523        assert( i<db->nDb );
000524        if( db->aDb[i].pSchema==pSchema ){
000525          break;
000526        }
000527      }
000528      assert( i>=0 && i<db->nDb );
000529    }
000530    return i;
000531  }
000532  
000533  /*
000534  ** Free all memory allocations in the pParse object
000535  */
000536  void sqlite3ParserReset(Parse *pParse){
000537    sqlite3 *db = pParse->db;
000538    sqlite3DbFree(db, pParse->aLabel);
000539    sqlite3ExprListDelete(db, pParse->pConstExpr);
000540    if( db ){
000541      assert( db->lookaside.bDisable >= pParse->disableLookaside );
000542      db->lookaside.bDisable -= pParse->disableLookaside;
000543      db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
000544    }
000545    pParse->disableLookaside = 0;
000546  }
000547  
000548  /*
000549  ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
000550  */
000551  static int sqlite3Prepare(
000552    sqlite3 *db,              /* Database handle. */
000553    const char *zSql,         /* UTF-8 encoded SQL statement. */
000554    int nBytes,               /* Length of zSql in bytes. */
000555    u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
000556    Vdbe *pReprepare,         /* VM being reprepared */
000557    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000558    const char **pzTail       /* OUT: End of parsed string */
000559  ){
000560    char *zErrMsg = 0;        /* Error message */
000561    int rc = SQLITE_OK;       /* Result code */
000562    int i;                    /* Loop counter */
000563    Parse sParse;             /* Parsing context */
000564  
000565    memset(&sParse, 0, PARSE_HDR_SZ);
000566    memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
000567    sParse.pReprepare = pReprepare;
000568    assert( ppStmt && *ppStmt==0 );
000569    /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
000570    assert( sqlite3_mutex_held(db->mutex) );
000571  
000572    /* For a long-term use prepared statement avoid the use of
000573    ** lookaside memory.
000574    */
000575    if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
000576      sParse.disableLookaside++;
000577      DisableLookaside;
000578    }
000579    sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
000580  
000581    /* Check to verify that it is possible to get a read lock on all
000582    ** database schemas.  The inability to get a read lock indicates that
000583    ** some other database connection is holding a write-lock, which in
000584    ** turn means that the other connection has made uncommitted changes
000585    ** to the schema.
000586    **
000587    ** Were we to proceed and prepare the statement against the uncommitted
000588    ** schema changes and if those schema changes are subsequently rolled
000589    ** back and different changes are made in their place, then when this
000590    ** prepared statement goes to run the schema cookie would fail to detect
000591    ** the schema change.  Disaster would follow.
000592    **
000593    ** This thread is currently holding mutexes on all Btrees (because
000594    ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
000595    ** is not possible for another thread to start a new schema change
000596    ** while this routine is running.  Hence, we do not need to hold 
000597    ** locks on the schema, we just need to make sure nobody else is 
000598    ** holding them.
000599    **
000600    ** Note that setting READ_UNCOMMITTED overrides most lock detection,
000601    ** but it does *not* override schema lock detection, so this all still
000602    ** works even if READ_UNCOMMITTED is set.
000603    */
000604    if( !db->noSharedCache ){
000605      for(i=0; i<db->nDb; i++) {
000606        Btree *pBt = db->aDb[i].pBt;
000607        if( pBt ){
000608          assert( sqlite3BtreeHoldsMutex(pBt) );
000609          rc = sqlite3BtreeSchemaLocked(pBt);
000610          if( rc ){
000611            const char *zDb = db->aDb[i].zDbSName;
000612            sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
000613            testcase( db->flags & SQLITE_ReadUncommit );
000614            goto end_prepare;
000615          }
000616        }
000617      }
000618    }
000619  
000620    sqlite3VtabUnlockList(db);
000621  
000622    sParse.db = db;
000623    if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
000624      char *zSqlCopy;
000625      int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
000626      testcase( nBytes==mxLen );
000627      testcase( nBytes==mxLen+1 );
000628      if( nBytes>mxLen ){
000629        sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
000630        rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
000631        goto end_prepare;
000632      }
000633      zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
000634      if( zSqlCopy ){
000635        sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
000636        sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
000637        sqlite3DbFree(db, zSqlCopy);
000638      }else{
000639        sParse.zTail = &zSql[nBytes];
000640      }
000641    }else{
000642      sqlite3RunParser(&sParse, zSql, &zErrMsg);
000643    }
000644    assert( 0==sParse.nQueryLoop );
000645  
000646    if( sParse.rc==SQLITE_DONE ){
000647      sParse.rc = SQLITE_OK;
000648    }
000649    if( sParse.checkSchema ){
000650      schemaIsValid(&sParse);
000651    }
000652    if( pzTail ){
000653      *pzTail = sParse.zTail;
000654    }
000655  
000656    if( db->init.busy==0 ){
000657      sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
000658    }
000659    if( db->mallocFailed ){
000660      sParse.rc = SQLITE_NOMEM_BKPT;
000661    }
000662    rc = sParse.rc;
000663    if( rc!=SQLITE_OK ){
000664      if( sParse.pVdbe ) sqlite3VdbeFinalize(sParse.pVdbe);
000665      assert(!(*ppStmt));
000666    }else{
000667      *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
000668    }
000669  
000670    if( zErrMsg ){
000671      sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
000672      sqlite3DbFree(db, zErrMsg);
000673    }else{
000674      sqlite3Error(db, rc);
000675    }
000676  
000677    /* Delete any TriggerPrg structures allocated while parsing this statement. */
000678    while( sParse.pTriggerPrg ){
000679      TriggerPrg *pT = sParse.pTriggerPrg;
000680      sParse.pTriggerPrg = pT->pNext;
000681      sqlite3DbFree(db, pT);
000682    }
000683  
000684  end_prepare:
000685  
000686    sqlite3ParserReset(&sParse);
000687    return rc;
000688  }
000689  static int sqlite3LockAndPrepare(
000690    sqlite3 *db,              /* Database handle. */
000691    const char *zSql,         /* UTF-8 encoded SQL statement. */
000692    int nBytes,               /* Length of zSql in bytes. */
000693    u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
000694    Vdbe *pOld,               /* VM being reprepared */
000695    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000696    const char **pzTail       /* OUT: End of parsed string */
000697  ){
000698    int rc;
000699    int cnt = 0;
000700  
000701  #ifdef SQLITE_ENABLE_API_ARMOR
000702    if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
000703  #endif
000704    *ppStmt = 0;
000705    if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
000706      return SQLITE_MISUSE_BKPT;
000707    }
000708    sqlite3_mutex_enter(db->mutex);
000709    sqlite3BtreeEnterAll(db);
000710    do{
000711      /* Make multiple attempts to compile the SQL, until it either succeeds
000712      ** or encounters a permanent error.  A schema problem after one schema
000713      ** reset is considered a permanent error. */
000714      rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
000715      assert( rc==SQLITE_OK || *ppStmt==0 );
000716    }while( rc==SQLITE_ERROR_RETRY
000717         || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
000718    sqlite3BtreeLeaveAll(db);
000719    rc = sqlite3ApiExit(db, rc);
000720    assert( (rc&db->errMask)==rc );
000721    sqlite3_mutex_leave(db->mutex);
000722    return rc;
000723  }
000724  
000725  
000726  /*
000727  ** Rerun the compilation of a statement after a schema change.
000728  **
000729  ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
000730  ** if the statement cannot be recompiled because another connection has
000731  ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
000732  ** occurs, return SQLITE_SCHEMA.
000733  */
000734  int sqlite3Reprepare(Vdbe *p){
000735    int rc;
000736    sqlite3_stmt *pNew;
000737    const char *zSql;
000738    sqlite3 *db;
000739    u8 prepFlags;
000740  
000741    assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
000742    zSql = sqlite3_sql((sqlite3_stmt *)p);
000743    assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
000744    db = sqlite3VdbeDb(p);
000745    assert( sqlite3_mutex_held(db->mutex) );
000746    prepFlags = sqlite3VdbePrepareFlags(p);
000747    rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
000748    if( rc ){
000749      if( rc==SQLITE_NOMEM ){
000750        sqlite3OomFault(db);
000751      }
000752      assert( pNew==0 );
000753      return rc;
000754    }else{
000755      assert( pNew!=0 );
000756    }
000757    sqlite3VdbeSwap((Vdbe*)pNew, p);
000758    sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
000759    sqlite3VdbeResetStepResult((Vdbe*)pNew);
000760    sqlite3VdbeFinalize((Vdbe*)pNew);
000761    return SQLITE_OK;
000762  }
000763  
000764  
000765  /*
000766  ** Two versions of the official API.  Legacy and new use.  In the legacy
000767  ** version, the original SQL text is not saved in the prepared statement
000768  ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
000769  ** sqlite3_step().  In the new version, the original SQL text is retained
000770  ** and the statement is automatically recompiled if an schema change
000771  ** occurs.
000772  */
000773  int sqlite3_prepare(
000774    sqlite3 *db,              /* Database handle. */
000775    const char *zSql,         /* UTF-8 encoded SQL statement. */
000776    int nBytes,               /* Length of zSql in bytes. */
000777    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000778    const char **pzTail       /* OUT: End of parsed string */
000779  ){
000780    int rc;
000781    rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
000782    assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
000783    return rc;
000784  }
000785  int sqlite3_prepare_v2(
000786    sqlite3 *db,              /* Database handle. */
000787    const char *zSql,         /* UTF-8 encoded SQL statement. */
000788    int nBytes,               /* Length of zSql in bytes. */
000789    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000790    const char **pzTail       /* OUT: End of parsed string */
000791  ){
000792    int rc;
000793    /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
000794    ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
000795    ** parameter.
000796    **
000797    ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
000798    rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
000799                               ppStmt,pzTail);
000800    assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
000801    return rc;
000802  }
000803  int sqlite3_prepare_v3(
000804    sqlite3 *db,              /* Database handle. */
000805    const char *zSql,         /* UTF-8 encoded SQL statement. */
000806    int nBytes,               /* Length of zSql in bytes. */
000807    unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */
000808    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000809    const char **pzTail       /* OUT: End of parsed string */
000810  ){
000811    int rc;
000812    /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
000813    ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
000814    ** which is a bit array consisting of zero or more of the
000815    ** SQLITE_PREPARE_* flags.
000816    **
000817    ** Proof by comparison to the implementation of sqlite3_prepare_v2()
000818    ** directly above. */
000819    rc = sqlite3LockAndPrepare(db,zSql,nBytes,
000820                   SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
000821                   0,ppStmt,pzTail);
000822    assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
000823    return rc;
000824  }
000825  
000826  
000827  #ifndef SQLITE_OMIT_UTF16
000828  /*
000829  ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
000830  */
000831  static int sqlite3Prepare16(
000832    sqlite3 *db,              /* Database handle. */ 
000833    const void *zSql,         /* UTF-16 encoded SQL statement. */
000834    int nBytes,               /* Length of zSql in bytes. */
000835    u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
000836    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000837    const void **pzTail       /* OUT: End of parsed string */
000838  ){
000839    /* This function currently works by first transforming the UTF-16
000840    ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
000841    ** tricky bit is figuring out the pointer to return in *pzTail.
000842    */
000843    char *zSql8;
000844    const char *zTail8 = 0;
000845    int rc = SQLITE_OK;
000846  
000847  #ifdef SQLITE_ENABLE_API_ARMOR
000848    if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
000849  #endif
000850    *ppStmt = 0;
000851    if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
000852      return SQLITE_MISUSE_BKPT;
000853    }
000854    if( nBytes>=0 ){
000855      int sz;
000856      const char *z = (const char*)zSql;
000857      for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
000858      nBytes = sz;
000859    }
000860    sqlite3_mutex_enter(db->mutex);
000861    zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
000862    if( zSql8 ){
000863      rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
000864    }
000865  
000866    if( zTail8 && pzTail ){
000867      /* If sqlite3_prepare returns a tail pointer, we calculate the
000868      ** equivalent pointer into the UTF-16 string by counting the unicode
000869      ** characters between zSql8 and zTail8, and then returning a pointer
000870      ** the same number of characters into the UTF-16 string.
000871      */
000872      int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
000873      *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
000874    }
000875    sqlite3DbFree(db, zSql8); 
000876    rc = sqlite3ApiExit(db, rc);
000877    sqlite3_mutex_leave(db->mutex);
000878    return rc;
000879  }
000880  
000881  /*
000882  ** Two versions of the official API.  Legacy and new use.  In the legacy
000883  ** version, the original SQL text is not saved in the prepared statement
000884  ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
000885  ** sqlite3_step().  In the new version, the original SQL text is retained
000886  ** and the statement is automatically recompiled if an schema change
000887  ** occurs.
000888  */
000889  int sqlite3_prepare16(
000890    sqlite3 *db,              /* Database handle. */ 
000891    const void *zSql,         /* UTF-16 encoded SQL statement. */
000892    int nBytes,               /* Length of zSql in bytes. */
000893    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000894    const void **pzTail       /* OUT: End of parsed string */
000895  ){
000896    int rc;
000897    rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
000898    assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
000899    return rc;
000900  }
000901  int sqlite3_prepare16_v2(
000902    sqlite3 *db,              /* Database handle. */ 
000903    const void *zSql,         /* UTF-16 encoded SQL statement. */
000904    int nBytes,               /* Length of zSql in bytes. */
000905    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000906    const void **pzTail       /* OUT: End of parsed string */
000907  ){
000908    int rc;
000909    rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
000910    assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
000911    return rc;
000912  }
000913  int sqlite3_prepare16_v3(
000914    sqlite3 *db,              /* Database handle. */ 
000915    const void *zSql,         /* UTF-16 encoded SQL statement. */
000916    int nBytes,               /* Length of zSql in bytes. */
000917    unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */
000918    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000919    const void **pzTail       /* OUT: End of parsed string */
000920  ){
000921    int rc;
000922    rc = sqlite3Prepare16(db,zSql,nBytes,
000923           SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
000924           ppStmt,pzTail);
000925    assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
000926    return rc;
000927  }
000928  
000929  #endif /* SQLITE_OMIT_UTF16 */