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