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