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  /*
000061  ** This is the callback routine for the code that initializes the
000062  ** database.  See sqlite3Init() below for additional information.
000063  ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
000064  **
000065  ** Each callback contains the following information:
000066  **
000067  **     argv[0] = name of thing being created
000068  **     argv[1] = root page number for table or index. 0 for trigger or view.
000069  **     argv[2] = SQL text for the CREATE statement.
000070  **
000071  */
000072  int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
000073    InitData *pData = (InitData*)pInit;
000074    sqlite3 *db = pData->db;
000075    int iDb = pData->iDb;
000076  
000077    assert( argc==3 );
000078    UNUSED_PARAMETER2(NotUsed, argc);
000079    assert( sqlite3_mutex_held(db->mutex) );
000080    DbClearProperty(db, iDb, DB_Empty);
000081    pData->nInitRow++;
000082    if( db->mallocFailed ){
000083      corruptSchema(pData, argv[0], 0);
000084      return 1;
000085    }
000086  
000087    assert( iDb>=0 && iDb<db->nDb );
000088    if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
000089    if( argv[1]==0 ){
000090      corruptSchema(pData, argv[0], 0);
000091    }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
000092      /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
000093      ** But because db->init.busy is set to 1, no VDBE code is generated
000094      ** or executed.  All the parser does is build the internal data
000095      ** structures that describe the table, index, or view.
000096      */
000097      int rc;
000098      u8 saved_iDb = db->init.iDb;
000099      sqlite3_stmt *pStmt;
000100      TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
000101  
000102      assert( db->init.busy );
000103      db->init.iDb = iDb;
000104      db->init.newTnum = sqlite3Atoi(argv[1]);
000105      db->init.orphanTrigger = 0;
000106      TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
000107      rc = db->errCode;
000108      assert( (rc&0xFF)==(rcp&0xFF) );
000109      db->init.iDb = saved_iDb;
000110      /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
000111      if( SQLITE_OK!=rc ){
000112        if( db->init.orphanTrigger ){
000113          assert( iDb==1 );
000114        }else{
000115          pData->rc = rc;
000116          if( rc==SQLITE_NOMEM ){
000117            sqlite3OomFault(db);
000118          }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
000119            corruptSchema(pData, argv[0], sqlite3_errmsg(db));
000120          }
000121        }
000122      }
000123      sqlite3_finalize(pStmt);
000124    }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
000125      corruptSchema(pData, argv[0], 0);
000126    }else{
000127      /* If the SQL column is blank it means this is an index that
000128      ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
000129      ** constraint for a CREATE TABLE.  The index should have already
000130      ** been created when we processed the CREATE TABLE.  All we have
000131      ** to do here is record the root page number for that index.
000132      */
000133      Index *pIndex;
000134      pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
000135      if( pIndex==0
000136       || sqlite3GetInt32(argv[1],&pIndex->tnum)==0
000137       || pIndex->tnum<2
000138       || sqlite3IndexHasDuplicateRootPage(pIndex)
000139      ){
000140        corruptSchema(pData, argv[0], pIndex?"invalid rootpage":"orphan index");
000141      }
000142    }
000143    return 0;
000144  }
000145  
000146  /*
000147  ** Attempt to read the database schema and initialize internal
000148  ** data structures for a single database file.  The index of the
000149  ** database file is given by iDb.  iDb==0 is used for the main
000150  ** database.  iDb==1 should never be used.  iDb>=2 is used for
000151  ** auxiliary databases.  Return one of the SQLITE_ error codes to
000152  ** indicate success or failure.
000153  */
000154  int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
000155    int rc;
000156    int i;
000157  #ifndef SQLITE_OMIT_DEPRECATED
000158    int size;
000159  #endif
000160    Db *pDb;
000161    char const *azArg[4];
000162    int meta[5];
000163    InitData initData;
000164    const char *zMasterName;
000165    int openedTransaction = 0;
000166  
000167    assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
000168    assert( iDb>=0 && iDb<db->nDb );
000169    assert( db->aDb[iDb].pSchema );
000170    assert( sqlite3_mutex_held(db->mutex) );
000171    assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
000172  
000173    db->init.busy = 1;
000174  
000175    /* Construct the in-memory representation schema tables (sqlite_master or
000176    ** sqlite_temp_master) by invoking the parser directly.  The appropriate
000177    ** table name will be inserted automatically by the parser so we can just
000178    ** use the abbreviation "x" here.  The parser will also automatically tag
000179    ** the schema table as read-only. */
000180    azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
000181    azArg[1] = "1";
000182    azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
000183                              "rootpage int,sql text)";
000184    azArg[3] = 0;
000185    initData.db = db;
000186    initData.iDb = iDb;
000187    initData.rc = SQLITE_OK;
000188    initData.pzErrMsg = pzErrMsg;
000189    initData.mInitFlags = mFlags;
000190    initData.nInitRow = 0;
000191    sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
000192    if( initData.rc ){
000193      rc = initData.rc;
000194      goto error_out;
000195    }
000196  
000197    /* Create a cursor to hold the database open
000198    */
000199    pDb = &db->aDb[iDb];
000200    if( pDb->pBt==0 ){
000201      assert( iDb==1 );
000202      DbSetProperty(db, 1, DB_SchemaLoaded);
000203      rc = SQLITE_OK;
000204      goto error_out;
000205    }
000206  
000207    /* If there is not already a read-only (or read-write) transaction opened
000208    ** on the b-tree database, open one now. If a transaction is opened, it 
000209    ** will be closed before this function returns.  */
000210    sqlite3BtreeEnter(pDb->pBt);
000211    if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
000212      rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
000213      if( rc!=SQLITE_OK ){
000214        sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
000215        goto initone_error_out;
000216      }
000217      openedTransaction = 1;
000218    }
000219  
000220    /* Get the database meta information.
000221    **
000222    ** Meta values are as follows:
000223    **    meta[0]   Schema cookie.  Changes with each schema change.
000224    **    meta[1]   File format of schema layer.
000225    **    meta[2]   Size of the page cache.
000226    **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
000227    **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
000228    **    meta[5]   User version
000229    **    meta[6]   Incremental vacuum mode
000230    **    meta[7]   unused
000231    **    meta[8]   unused
000232    **    meta[9]   unused
000233    **
000234    ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
000235    ** the possible values of meta[4].
000236    */
000237    for(i=0; i<ArraySize(meta); i++){
000238      sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
000239    }
000240    if( (db->flags & SQLITE_ResetDatabase)!=0 ){
000241      memset(meta, 0, sizeof(meta));
000242    }
000243    pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
000244  
000245    /* If opening a non-empty database, check the text encoding. For the
000246    ** main database, set sqlite3.enc to the encoding of the main database.
000247    ** For an attached db, it is an error if the encoding is not the same
000248    ** as sqlite3.enc.
000249    */
000250    if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
000251      if( iDb==0 ){
000252  #ifndef SQLITE_OMIT_UTF16
000253        u8 encoding;
000254        /* If opening the main database, set ENC(db). */
000255        encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
000256        if( encoding==0 ) encoding = SQLITE_UTF8;
000257        ENC(db) = encoding;
000258  #else
000259        ENC(db) = SQLITE_UTF8;
000260  #endif
000261      }else{
000262        /* If opening an attached database, the encoding much match ENC(db) */
000263        if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
000264          sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
000265              " text encoding as main database");
000266          rc = SQLITE_ERROR;
000267          goto initone_error_out;
000268        }
000269      }
000270    }else{
000271      DbSetProperty(db, iDb, DB_Empty);
000272    }
000273    pDb->pSchema->enc = ENC(db);
000274  
000275    if( pDb->pSchema->cache_size==0 ){
000276  #ifndef SQLITE_OMIT_DEPRECATED
000277      size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
000278      if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
000279      pDb->pSchema->cache_size = size;
000280  #else
000281      pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
000282  #endif
000283      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
000284    }
000285  
000286    /*
000287    ** file_format==1    Version 3.0.0.
000288    ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
000289    ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
000290    ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
000291    */
000292    pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
000293    if( pDb->pSchema->file_format==0 ){
000294      pDb->pSchema->file_format = 1;
000295    }
000296    if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
000297      sqlite3SetString(pzErrMsg, db, "unsupported file format");
000298      rc = SQLITE_ERROR;
000299      goto initone_error_out;
000300    }
000301  
000302    /* Ticket #2804:  When we open a database in the newer file format,
000303    ** clear the legacy_file_format pragma flag so that a VACUUM will
000304    ** not downgrade the database and thus invalidate any descending
000305    ** indices that the user might have created.
000306    */
000307    if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
000308      db->flags &= ~(u64)SQLITE_LegacyFileFmt;
000309    }
000310  
000311    /* Read the schema information out of the schema tables
000312    */
000313    assert( db->init.busy );
000314    {
000315      char *zSql;
000316      zSql = sqlite3MPrintf(db, 
000317          "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
000318          db->aDb[iDb].zDbSName, zMasterName);
000319  #ifndef SQLITE_OMIT_AUTHORIZATION
000320      {
000321        sqlite3_xauth xAuth;
000322        xAuth = db->xAuth;
000323        db->xAuth = 0;
000324  #endif
000325        rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
000326  #ifndef SQLITE_OMIT_AUTHORIZATION
000327        db->xAuth = xAuth;
000328      }
000329  #endif
000330      if( rc==SQLITE_OK ) rc = initData.rc;
000331      sqlite3DbFree(db, zSql);
000332  #ifndef SQLITE_OMIT_ANALYZE
000333      if( rc==SQLITE_OK ){
000334        sqlite3AnalysisLoad(db, iDb);
000335      }
000336  #endif
000337    }
000338    if( db->mallocFailed ){
000339      rc = SQLITE_NOMEM_BKPT;
000340      sqlite3ResetAllSchemasOfConnection(db);
000341    }
000342    if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
000343      /* Black magic: If the SQLITE_NoSchemaError flag is set, then consider
000344      ** the schema loaded, even if errors occurred. In this situation the 
000345      ** current sqlite3_prepare() operation will fail, but the following one
000346      ** will attempt to compile the supplied statement against whatever subset
000347      ** of the schema was loaded before the error occurred. The primary
000348      ** purpose of this is to allow access to the sqlite_master table
000349      ** even when its contents have been corrupted.
000350      */
000351      DbSetProperty(db, iDb, DB_SchemaLoaded);
000352      rc = SQLITE_OK;
000353    }
000354  
000355    /* Jump here for an error that occurs after successfully allocating
000356    ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
000357    ** before that point, jump to error_out.
000358    */
000359  initone_error_out:
000360    if( openedTransaction ){
000361      sqlite3BtreeCommit(pDb->pBt);
000362    }
000363    sqlite3BtreeLeave(pDb->pBt);
000364  
000365  error_out:
000366    if( rc ){
000367      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
000368        sqlite3OomFault(db);
000369      }
000370      sqlite3ResetOneSchema(db, iDb);
000371    }
000372    db->init.busy = 0;
000373    return rc;
000374  }
000375  
000376  /*
000377  ** Initialize all database files - the main database file, the file
000378  ** used to store temporary tables, and any additional database files
000379  ** created using ATTACH statements.  Return a success code.  If an
000380  ** error occurs, write an error message into *pzErrMsg.
000381  **
000382  ** After a database is initialized, the DB_SchemaLoaded bit is set
000383  ** bit is set in the flags field of the Db structure. If the database
000384  ** file was of zero-length, then the DB_Empty flag is also set.
000385  */
000386  int sqlite3Init(sqlite3 *db, char **pzErrMsg){
000387    int i, rc;
000388    int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
000389    
000390    assert( sqlite3_mutex_held(db->mutex) );
000391    assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
000392    assert( db->init.busy==0 );
000393    ENC(db) = SCHEMA_ENC(db);
000394    assert( db->nDb>0 );
000395    /* Do the main schema first */
000396    if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
000397      rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
000398      if( rc ) return rc;
000399    }
000400    /* All other schemas after the main schema. The "temp" schema must be last */
000401    for(i=db->nDb-1; i>0; i--){
000402      assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
000403      if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
000404        rc = sqlite3InitOne(db, i, pzErrMsg, 0);
000405        if( rc ) return rc;
000406      }
000407    }
000408    if( commit_internal ){
000409      sqlite3CommitInternalChanges(db);
000410    }
000411    return SQLITE_OK;
000412  }
000413  
000414  /*
000415  ** This routine is a no-op if the database schema is already initialized.
000416  ** Otherwise, the schema is loaded. An error code is returned.
000417  */
000418  int sqlite3ReadSchema(Parse *pParse){
000419    int rc = SQLITE_OK;
000420    sqlite3 *db = pParse->db;
000421    assert( sqlite3_mutex_held(db->mutex) );
000422    if( !db->init.busy ){
000423      rc = sqlite3Init(db, &pParse->zErrMsg);
000424      if( rc!=SQLITE_OK ){
000425        pParse->rc = rc;
000426        pParse->nErr++;
000427      }else if( db->noSharedCache ){
000428        db->mDbFlags |= DBFLAG_SchemaKnownOk;
000429      }
000430    }
000431    return rc;
000432  }
000433  
000434  
000435  /*
000436  ** Check schema cookies in all databases.  If any cookie is out
000437  ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
000438  ** make no changes to pParse->rc.
000439  */
000440  static void schemaIsValid(Parse *pParse){
000441    sqlite3 *db = pParse->db;
000442    int iDb;
000443    int rc;
000444    int cookie;
000445  
000446    assert( pParse->checkSchema );
000447    assert( sqlite3_mutex_held(db->mutex) );
000448    for(iDb=0; iDb<db->nDb; iDb++){
000449      int openedTransaction = 0;         /* True if a transaction is opened */
000450      Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
000451      if( pBt==0 ) continue;
000452  
000453      /* If there is not already a read-only (or read-write) transaction opened
000454      ** on the b-tree database, open one now. If a transaction is opened, it 
000455      ** will be closed immediately after reading the meta-value. */
000456      if( !sqlite3BtreeIsInReadTrans(pBt) ){
000457        rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
000458        if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
000459          sqlite3OomFault(db);
000460        }
000461        if( rc!=SQLITE_OK ) return;
000462        openedTransaction = 1;
000463      }
000464  
000465      /* Read the schema cookie from the database. If it does not match the 
000466      ** value stored as part of the in-memory schema representation,
000467      ** set Parse.rc to SQLITE_SCHEMA. */
000468      sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
000469      assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
000470      if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
000471        sqlite3ResetOneSchema(db, iDb);
000472        pParse->rc = SQLITE_SCHEMA;
000473      }
000474  
000475      /* Close the transaction, if one was opened. */
000476      if( openedTransaction ){
000477        sqlite3BtreeCommit(pBt);
000478      }
000479    }
000480  }
000481  
000482  /*
000483  ** Convert a schema pointer into the iDb index that indicates
000484  ** which database file in db->aDb[] the schema refers to.
000485  **
000486  ** If the same database is attached more than once, the first
000487  ** attached database is returned.
000488  */
000489  int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
000490    int i = -1000000;
000491  
000492    /* If pSchema is NULL, then return -1000000. This happens when code in 
000493    ** expr.c is trying to resolve a reference to a transient table (i.e. one
000494    ** created by a sub-select). In this case the return value of this 
000495    ** function should never be used.
000496    **
000497    ** We return -1000000 instead of the more usual -1 simply because using
000498    ** -1000000 as the incorrect index into db->aDb[] is much 
000499    ** more likely to cause a segfault than -1 (of course there are assert()
000500    ** statements too, but it never hurts to play the odds).
000501    */
000502    assert( sqlite3_mutex_held(db->mutex) );
000503    if( pSchema ){
000504      for(i=0; 1; i++){
000505        assert( i<db->nDb );
000506        if( db->aDb[i].pSchema==pSchema ){
000507          break;
000508        }
000509      }
000510      assert( i>=0 && i<db->nDb );
000511    }
000512    return i;
000513  }
000514  
000515  /*
000516  ** Free all memory allocations in the pParse object
000517  */
000518  void sqlite3ParserReset(Parse *pParse){
000519    sqlite3 *db = pParse->db;
000520    sqlite3DbFree(db, pParse->aLabel);
000521    sqlite3ExprListDelete(db, pParse->pConstExpr);
000522    if( db ){
000523      assert( db->lookaside.bDisable >= pParse->disableLookaside );
000524      db->lookaside.bDisable -= pParse->disableLookaside;
000525    }
000526    pParse->disableLookaside = 0;
000527  }
000528  
000529  /*
000530  ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
000531  */
000532  static int sqlite3Prepare(
000533    sqlite3 *db,              /* Database handle. */
000534    const char *zSql,         /* UTF-8 encoded SQL statement. */
000535    int nBytes,               /* Length of zSql in bytes. */
000536    u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */
000537    Vdbe *pReprepare,         /* VM being reprepared */
000538    sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
000539    const char **pzTail       /* OUT: End of parsed string */
000540  ){
000541    char *zErrMsg = 0;        /* Error message */
000542    int rc = SQLITE_OK;       /* Result code */
000543    int i;                    /* Loop counter */
000544    Parse sParse;             /* Parsing context */
000545  
000546    memset(&sParse, 0, PARSE_HDR_SZ);
000547    memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
000548    sParse.pReprepare = pReprepare;
000549    assert( ppStmt && *ppStmt==0 );
000550    /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
000551    assert( sqlite3_mutex_held(db->mutex) );
000552  
000553    /* For a long-term use prepared statement avoid the use of
000554    ** lookaside memory.
000555    */
000556    if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
000557      sParse.disableLookaside++;
000558      db->lookaside.bDisable++;
000559    }
000560    sParse.disableVtab = (prepFlags & SQLITE_PREPARE_NO_VTAB)!=0;
000561  
000562    /* Check to verify that it is possible to get a read lock on all
000563    ** database schemas.  The inability to get a read lock indicates that
000564    ** some other database connection is holding a write-lock, which in
000565    ** turn means that the other connection has made uncommitted changes
000566    ** to the schema.
000567    **
000568    ** Were we to proceed and prepare the statement against the uncommitted
000569    ** schema changes and if those schema changes are subsequently rolled
000570    ** back and different changes are made in their place, then when this
000571    ** prepared statement goes to run the schema cookie would fail to detect
000572    ** the schema change.  Disaster would follow.
000573    **
000574    ** This thread is currently holding mutexes on all Btrees (because
000575    ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
000576    ** is not possible for another thread to start a new schema change
000577    ** while this routine is running.  Hence, we do not need to hold 
000578    ** locks on the schema, we just need to make sure nobody else is 
000579    ** holding them.
000580    **
000581    ** Note that setting READ_UNCOMMITTED overrides most lock detection,
000582    ** but it does *not* override schema lock detection, so this all still
000583    ** works even if READ_UNCOMMITTED is set.
000584    */
000585    for(i=0; i<db->nDb; i++) {
000586      Btree *pBt = db->aDb[i].pBt;
000587      if( pBt ){
000588        assert( sqlite3BtreeHoldsMutex(pBt) );
000589        rc = sqlite3BtreeSchemaLocked(pBt);
000590        if( rc ){
000591          const char *zDb = db->aDb[i].zDbSName;
000592          sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
000593          testcase( db->flags & SQLITE_ReadUncommit );
000594          goto end_prepare;
000595        }
000596      }
000597    }
000598  
000599    sqlite3VtabUnlockList(db);
000600  
000601    sParse.db = db;
000602    if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
000603      char *zSqlCopy;
000604      int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
000605      testcase( nBytes==mxLen );
000606      testcase( nBytes==mxLen+1 );
000607      if( nBytes>mxLen ){
000608        sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
000609        rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
000610        goto end_prepare;
000611      }
000612      zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
000613      if( zSqlCopy ){
000614        sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
000615        sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
000616        sqlite3DbFree(db, zSqlCopy);
000617      }else{
000618        sParse.zTail = &zSql[nBytes];
000619      }
000620    }else{
000621      sqlite3RunParser(&sParse, zSql, &zErrMsg);
000622    }
000623    assert( 0==sParse.nQueryLoop );
000624  
000625    if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
000626    if( sParse.checkSchema ){
000627      schemaIsValid(&sParse);
000628    }
000629    if( db->mallocFailed ){
000630      sParse.rc = SQLITE_NOMEM_BKPT;
000631    }
000632    if( pzTail ){
000633      *pzTail = sParse.zTail;
000634    }
000635    rc = sParse.rc;
000636  
000637  #ifndef SQLITE_OMIT_EXPLAIN
000638    if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
000639      static const char * const azColName[] = {
000640         "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
000641         "id", "parent", "notused", "detail"
000642      };
000643      int iFirst, mx;
000644      if( sParse.explain==2 ){
000645        sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
000646        iFirst = 8;
000647        mx = 12;
000648      }else{
000649        sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
000650        iFirst = 0;
000651        mx = 8;
000652      }
000653      for(i=iFirst; i<mx; i++){
000654        sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
000655                              azColName[i], SQLITE_STATIC);
000656      }
000657    }
000658  #endif
000659  
000660    if( db->init.busy==0 ){
000661      sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
000662    }
000663    if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
000664      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 */