Home | History | Annotate | Download | only in src
      1 /*
      2 ** 2005 May 25
      3 **
      4 ** The author disclaims copyright to this source code.  In place of
      5 ** a legal notice, here is a blessing:
      6 **
      7 **    May you do good and not evil.
      8 **    May you find forgiveness for yourself and forgive others.
      9 **    May you share freely, never taking more than you give.
     10 **
     11 *************************************************************************
     12 ** This file contains the implementation of the sqlite3_prepare()
     13 ** interface, and routines that contribute to loading the database schema
     14 ** from disk.
     15 */
     16 #include "sqliteInt.h"
     17 
     18 /*
     19 ** Fill the InitData structure with an error message that indicates
     20 ** that the database is corrupt.
     21 */
     22 static void corruptSchema(
     23   InitData *pData,     /* Initialization context */
     24   const char *zObj,    /* Object being parsed at the point of error */
     25   const char *zExtra   /* Error information */
     26 ){
     27   sqlite3 *db = pData->db;
     28   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
     29     if( zObj==0 ) zObj = "?";
     30     sqlite3SetString(pData->pzErrMsg, db,
     31       "malformed database schema (%s)", zObj);
     32     if( zExtra ){
     33       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
     34                                  "%s - %s", *pData->pzErrMsg, zExtra);
     35     }
     36   }
     37   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
     38 }
     39 
     40 /*
     41 ** This is the callback routine for the code that initializes the
     42 ** database.  See sqlite3Init() below for additional information.
     43 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
     44 **
     45 ** Each callback contains the following information:
     46 **
     47 **     argv[0] = name of thing being created
     48 **     argv[1] = root page number for table or index. 0 for trigger or view.
     49 **     argv[2] = SQL text for the CREATE statement.
     50 **
     51 */
     52 int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
     53   InitData *pData = (InitData*)pInit;
     54   sqlite3 *db = pData->db;
     55   int iDb = pData->iDb;
     56 
     57   assert( argc==3 );
     58   UNUSED_PARAMETER2(NotUsed, argc);
     59   assert( sqlite3_mutex_held(db->mutex) );
     60   DbClearProperty(db, iDb, DB_Empty);
     61   if( db->mallocFailed ){
     62     corruptSchema(pData, argv[0], 0);
     63     return 1;
     64   }
     65 
     66   assert( iDb>=0 && iDb<db->nDb );
     67   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
     68   if( argv[1]==0 ){
     69     corruptSchema(pData, argv[0], 0);
     70   }else if( argv[2] && argv[2][0] ){
     71     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
     72     ** But because db->init.busy is set to 1, no VDBE code is generated
     73     ** or executed.  All the parser does is build the internal data
     74     ** structures that describe the table, index, or view.
     75     */
     76     int rc;
     77     sqlite3_stmt *pStmt;
     78     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
     79 
     80     assert( db->init.busy );
     81     db->init.iDb = iDb;
     82     db->init.newTnum = sqlite3Atoi(argv[1]);
     83     db->init.orphanTrigger = 0;
     84     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
     85     rc = db->errCode;
     86     assert( (rc&0xFF)==(rcp&0xFF) );
     87     db->init.iDb = 0;
     88     if( SQLITE_OK!=rc ){
     89       if( db->init.orphanTrigger ){
     90         assert( iDb==1 );
     91       }else{
     92         pData->rc = rc;
     93         if( rc==SQLITE_NOMEM ){
     94           db->mallocFailed = 1;
     95         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
     96           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
     97         }
     98       }
     99     }
    100     sqlite3_finalize(pStmt);
    101   }else if( argv[0]==0 ){
    102     corruptSchema(pData, 0, 0);
    103   }else{
    104     /* If the SQL column is blank it means this is an index that
    105     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
    106     ** constraint for a CREATE TABLE.  The index should have already
    107     ** been created when we processed the CREATE TABLE.  All we have
    108     ** to do here is record the root page number for that index.
    109     */
    110     Index *pIndex;
    111     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
    112     if( pIndex==0 ){
    113       /* This can occur if there exists an index on a TEMP table which
    114       ** has the same name as another index on a permanent index.  Since
    115       ** the permanent table is hidden by the TEMP table, we can also
    116       ** safely ignore the index on the permanent table.
    117       */
    118       /* Do Nothing */;
    119     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
    120       corruptSchema(pData, argv[0], "invalid rootpage");
    121     }
    122   }
    123   return 0;
    124 }
    125 
    126 /*
    127 ** Attempt to read the database schema and initialize internal
    128 ** data structures for a single database file.  The index of the
    129 ** database file is given by iDb.  iDb==0 is used for the main
    130 ** database.  iDb==1 should never be used.  iDb>=2 is used for
    131 ** auxiliary databases.  Return one of the SQLITE_ error codes to
    132 ** indicate success or failure.
    133 */
    134 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
    135   int rc;
    136   int i;
    137   int size;
    138   Table *pTab;
    139   Db *pDb;
    140   char const *azArg[4];
    141   int meta[5];
    142   InitData initData;
    143   char const *zMasterSchema;
    144   char const *zMasterName;
    145   int openedTransaction = 0;
    146 
    147   /*
    148   ** The master database table has a structure like this
    149   */
    150   static const char master_schema[] =
    151      "CREATE TABLE sqlite_master(\n"
    152      "  type text,\n"
    153      "  name text,\n"
    154      "  tbl_name text,\n"
    155      "  rootpage integer,\n"
    156      "  sql text\n"
    157      ")"
    158   ;
    159 #ifndef SQLITE_OMIT_TEMPDB
    160   static const char temp_master_schema[] =
    161      "CREATE TEMP TABLE sqlite_temp_master(\n"
    162      "  type text,\n"
    163      "  name text,\n"
    164      "  tbl_name text,\n"
    165      "  rootpage integer,\n"
    166      "  sql text\n"
    167      ")"
    168   ;
    169 #else
    170   #define temp_master_schema 0
    171 #endif
    172 
    173   assert( iDb>=0 && iDb<db->nDb );
    174   assert( db->aDb[iDb].pSchema );
    175   assert( sqlite3_mutex_held(db->mutex) );
    176   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
    177 
    178   /* zMasterSchema and zInitScript are set to point at the master schema
    179   ** and initialisation script appropriate for the database being
    180   ** initialised. zMasterName is the name of the master table.
    181   */
    182   if( !OMIT_TEMPDB && iDb==1 ){
    183     zMasterSchema = temp_master_schema;
    184   }else{
    185     zMasterSchema = master_schema;
    186   }
    187   zMasterName = SCHEMA_TABLE(iDb);
    188 
    189   /* Construct the schema tables.  */
    190   azArg[0] = zMasterName;
    191   azArg[1] = "1";
    192   azArg[2] = zMasterSchema;
    193   azArg[3] = 0;
    194   initData.db = db;
    195   initData.iDb = iDb;
    196   initData.rc = SQLITE_OK;
    197   initData.pzErrMsg = pzErrMsg;
    198   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
    199   if( initData.rc ){
    200     rc = initData.rc;
    201     goto error_out;
    202   }
    203   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
    204   if( ALWAYS(pTab) ){
    205     pTab->tabFlags |= TF_Readonly;
    206   }
    207 
    208   /* Create a cursor to hold the database open
    209   */
    210   pDb = &db->aDb[iDb];
    211   if( pDb->pBt==0 ){
    212     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
    213       DbSetProperty(db, 1, DB_SchemaLoaded);
    214     }
    215     return SQLITE_OK;
    216   }
    217 
    218   /* If there is not already a read-only (or read-write) transaction opened
    219   ** on the b-tree database, open one now. If a transaction is opened, it
    220   ** will be closed before this function returns.  */
    221   sqlite3BtreeEnter(pDb->pBt);
    222   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
    223     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
    224     if( rc!=SQLITE_OK ){
    225       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
    226       goto initone_error_out;
    227     }
    228     openedTransaction = 1;
    229   }
    230 
    231   /* Get the database meta information.
    232   **
    233   ** Meta values are as follows:
    234   **    meta[0]   Schema cookie.  Changes with each schema change.
    235   **    meta[1]   File format of schema layer.
    236   **    meta[2]   Size of the page cache.
    237   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
    238   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
    239   **    meta[5]   User version
    240   **    meta[6]   Incremental vacuum mode
    241   **    meta[7]   unused
    242   **    meta[8]   unused
    243   **    meta[9]   unused
    244   **
    245   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
    246   ** the possible values of meta[4].
    247   */
    248   for(i=0; i<ArraySize(meta); i++){
    249     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
    250   }
    251   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
    252 
    253   /* If opening a non-empty database, check the text encoding. For the
    254   ** main database, set sqlite3.enc to the encoding of the main database.
    255   ** For an attached db, it is an error if the encoding is not the same
    256   ** as sqlite3.enc.
    257   */
    258   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
    259     if( iDb==0 ){
    260       u8 encoding;
    261       /* If opening the main database, set ENC(db). */
    262       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
    263       if( encoding==0 ) encoding = SQLITE_UTF8;
    264       ENC(db) = encoding;
    265       db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
    266     }else{
    267       /* If opening an attached database, the encoding much match ENC(db) */
    268       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
    269         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
    270             " text encoding as main database");
    271         rc = SQLITE_ERROR;
    272         goto initone_error_out;
    273       }
    274     }
    275   }else{
    276     DbSetProperty(db, iDb, DB_Empty);
    277   }
    278   pDb->pSchema->enc = ENC(db);
    279 
    280   if( pDb->pSchema->cache_size==0 ){
    281     size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
    282     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
    283     pDb->pSchema->cache_size = size;
    284     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
    285   }
    286 
    287   /*
    288   ** file_format==1    Version 3.0.0.
    289   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
    290   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
    291   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
    292   */
    293   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
    294   if( pDb->pSchema->file_format==0 ){
    295     pDb->pSchema->file_format = 1;
    296   }
    297   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
    298     sqlite3SetString(pzErrMsg, db, "unsupported file format");
    299     rc = SQLITE_ERROR;
    300     goto initone_error_out;
    301   }
    302 
    303   /* Ticket #2804:  When we open a database in the newer file format,
    304   ** clear the legacy_file_format pragma flag so that a VACUUM will
    305   ** not downgrade the database and thus invalidate any descending
    306   ** indices that the user might have created.
    307   */
    308   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
    309     db->flags &= ~SQLITE_LegacyFileFmt;
    310   }
    311 
    312   /* Read the schema information out of the schema tables
    313   */
    314   assert( db->init.busy );
    315   {
    316     char *zSql;
    317     zSql = sqlite3MPrintf(db,
    318         "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
    319         db->aDb[iDb].zName, zMasterName);
    320 #ifndef SQLITE_OMIT_AUTHORIZATION
    321     {
    322       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
    323       xAuth = db->xAuth;
    324       db->xAuth = 0;
    325 #endif
    326       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
    327 #ifndef SQLITE_OMIT_AUTHORIZATION
    328       db->xAuth = xAuth;
    329     }
    330 #endif
    331     if( rc==SQLITE_OK ) rc = initData.rc;
    332     sqlite3DbFree(db, zSql);
    333 #ifndef SQLITE_OMIT_ANALYZE
    334     if( rc==SQLITE_OK ){
    335       sqlite3AnalysisLoad(db, iDb);
    336     }
    337 #endif
    338   }
    339   if( db->mallocFailed ){
    340     rc = SQLITE_NOMEM;
    341     sqlite3ResetInternalSchema(db, -1);
    342   }
    343   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
    344     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
    345     ** the schema loaded, even if errors occurred. In this situation the
    346     ** current sqlite3_prepare() operation will fail, but the following one
    347     ** will attempt to compile the supplied statement against whatever subset
    348     ** of the schema was loaded before the error occurred. The primary
    349     ** purpose of this is to allow access to the sqlite_master table
    350     ** even when its contents have been corrupted.
    351     */
    352     DbSetProperty(db, iDb, DB_SchemaLoaded);
    353     rc = SQLITE_OK;
    354   }
    355 
    356   /* Jump here for an error that occurs after successfully allocating
    357   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
    358   ** before that point, jump to error_out.
    359   */
    360 initone_error_out:
    361   if( openedTransaction ){
    362     sqlite3BtreeCommit(pDb->pBt);
    363   }
    364   sqlite3BtreeLeave(pDb->pBt);
    365 
    366 error_out:
    367   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
    368     db->mallocFailed = 1;
    369   }
    370   return rc;
    371 }
    372 
    373 /*
    374 ** Initialize all database files - the main database file, the file
    375 ** used to store temporary tables, and any additional database files
    376 ** created using ATTACH statements.  Return a success code.  If an
    377 ** error occurs, write an error message into *pzErrMsg.
    378 **
    379 ** After a database is initialized, the DB_SchemaLoaded bit is set
    380 ** bit is set in the flags field of the Db structure. If the database
    381 ** file was of zero-length, then the DB_Empty flag is also set.
    382 */
    383 int sqlite3Init(sqlite3 *db, char **pzErrMsg){
    384   int i, rc;
    385   int commit_internal = !(db->flags&SQLITE_InternChanges);
    386 
    387   assert( sqlite3_mutex_held(db->mutex) );
    388   rc = SQLITE_OK;
    389   db->init.busy = 1;
    390   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
    391     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
    392     rc = sqlite3InitOne(db, i, pzErrMsg);
    393     if( rc ){
    394       sqlite3ResetInternalSchema(db, i);
    395     }
    396   }
    397 
    398   /* Once all the other databases have been initialised, load the schema
    399   ** for the TEMP database. This is loaded last, as the TEMP database
    400   ** schema may contain references to objects in other databases.
    401   */
    402 #ifndef SQLITE_OMIT_TEMPDB
    403   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
    404                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
    405     rc = sqlite3InitOne(db, 1, pzErrMsg);
    406     if( rc ){
    407       sqlite3ResetInternalSchema(db, 1);
    408     }
    409   }
    410 #endif
    411 
    412   db->init.busy = 0;
    413   if( rc==SQLITE_OK && commit_internal ){
    414     sqlite3CommitInternalChanges(db);
    415   }
    416 
    417   return rc;
    418 }
    419 
    420 /*
    421 ** This routine is a no-op if the database schema is already initialised.
    422 ** Otherwise, the schema is loaded. An error code is returned.
    423 */
    424 int sqlite3ReadSchema(Parse *pParse){
    425   int rc = SQLITE_OK;
    426   sqlite3 *db = pParse->db;
    427   assert( sqlite3_mutex_held(db->mutex) );
    428   if( !db->init.busy ){
    429     rc = sqlite3Init(db, &pParse->zErrMsg);
    430   }
    431   if( rc!=SQLITE_OK ){
    432     pParse->rc = rc;
    433     pParse->nErr++;
    434   }
    435   return rc;
    436 }
    437 
    438 
    439 /*
    440 ** Check schema cookies in all databases.  If any cookie is out
    441 ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
    442 ** make no changes to pParse->rc.
    443 */
    444 static void schemaIsValid(Parse *pParse){
    445   sqlite3 *db = pParse->db;
    446   int iDb;
    447   int rc;
    448   int cookie;
    449 
    450   assert( pParse->checkSchema );
    451   assert( sqlite3_mutex_held(db->mutex) );
    452   for(iDb=0; iDb<db->nDb; iDb++){
    453     int openedTransaction = 0;         /* True if a transaction is opened */
    454     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
    455     if( pBt==0 ) continue;
    456 
    457     /* If there is not already a read-only (or read-write) transaction opened
    458     ** on the b-tree database, open one now. If a transaction is opened, it
    459     ** will be closed immediately after reading the meta-value. */
    460     if( !sqlite3BtreeIsInReadTrans(pBt) ){
    461       rc = sqlite3BtreeBeginTrans(pBt, 0);
    462       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
    463         db->mallocFailed = 1;
    464       }
    465       if( rc!=SQLITE_OK ) return;
    466       openedTransaction = 1;
    467     }
    468 
    469     /* Read the schema cookie from the database. If it does not match the
    470     ** value stored as part of the in-memory schema representation,
    471     ** set Parse.rc to SQLITE_SCHEMA. */
    472     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
    473     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
    474     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
    475       sqlite3ResetInternalSchema(db, iDb);
    476       pParse->rc = SQLITE_SCHEMA;
    477     }
    478 
    479     /* Close the transaction, if one was opened. */
    480     if( openedTransaction ){
    481       sqlite3BtreeCommit(pBt);
    482     }
    483   }
    484 }
    485 
    486 /*
    487 ** Convert a schema pointer into the iDb index that indicates
    488 ** which database file in db->aDb[] the schema refers to.
    489 **
    490 ** If the same database is attached more than once, the first
    491 ** attached database is returned.
    492 */
    493 int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
    494   int i = -1000000;
    495 
    496   /* If pSchema is NULL, then return -1000000. This happens when code in
    497   ** expr.c is trying to resolve a reference to a transient table (i.e. one
    498   ** created by a sub-select). In this case the return value of this
    499   ** function should never be used.
    500   **
    501   ** We return -1000000 instead of the more usual -1 simply because using
    502   ** -1000000 as the incorrect index into db->aDb[] is much
    503   ** more likely to cause a segfault than -1 (of course there are assert()
    504   ** statements too, but it never hurts to play the odds).
    505   */
    506   assert( sqlite3_mutex_held(db->mutex) );
    507   if( pSchema ){
    508     for(i=0; ALWAYS(i<db->nDb); i++){
    509       if( db->aDb[i].pSchema==pSchema ){
    510         break;
    511       }
    512     }
    513     assert( i>=0 && i<db->nDb );
    514   }
    515   return i;
    516 }
    517 
    518 /*
    519 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
    520 */
    521 static int sqlite3Prepare(
    522   sqlite3 *db,              /* Database handle. */
    523   const char *zSql,         /* UTF-8 encoded SQL statement. */
    524   int nBytes,               /* Length of zSql in bytes. */
    525   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
    526   Vdbe *pReprepare,         /* VM being reprepared */
    527   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    528   const char **pzTail       /* OUT: End of parsed string */
    529 ){
    530   Parse *pParse;            /* Parsing context */
    531   char *zErrMsg = 0;        /* Error message */
    532   int rc = SQLITE_OK;       /* Result code */
    533   int i;                    /* Loop counter */
    534 
    535   /* Allocate the parsing context */
    536   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
    537   if( pParse==0 ){
    538     rc = SQLITE_NOMEM;
    539     goto end_prepare;
    540   }
    541   pParse->pReprepare = pReprepare;
    542   assert( ppStmt && *ppStmt==0 );
    543   assert( !db->mallocFailed );
    544   assert( sqlite3_mutex_held(db->mutex) );
    545 
    546   /* Check to verify that it is possible to get a read lock on all
    547   ** database schemas.  The inability to get a read lock indicates that
    548   ** some other database connection is holding a write-lock, which in
    549   ** turn means that the other connection has made uncommitted changes
    550   ** to the schema.
    551   **
    552   ** Were we to proceed and prepare the statement against the uncommitted
    553   ** schema changes and if those schema changes are subsequently rolled
    554   ** back and different changes are made in their place, then when this
    555   ** prepared statement goes to run the schema cookie would fail to detect
    556   ** the schema change.  Disaster would follow.
    557   **
    558   ** This thread is currently holding mutexes on all Btrees (because
    559   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
    560   ** is not possible for another thread to start a new schema change
    561   ** while this routine is running.  Hence, we do not need to hold
    562   ** locks on the schema, we just need to make sure nobody else is
    563   ** holding them.
    564   **
    565   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
    566   ** but it does *not* override schema lock detection, so this all still
    567   ** works even if READ_UNCOMMITTED is set.
    568   */
    569   for(i=0; i<db->nDb; i++) {
    570     Btree *pBt = db->aDb[i].pBt;
    571     if( pBt ){
    572       assert( sqlite3BtreeHoldsMutex(pBt) );
    573       rc = sqlite3BtreeSchemaLocked(pBt);
    574       if( rc ){
    575         const char *zDb = db->aDb[i].zName;
    576         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
    577         testcase( db->flags & SQLITE_ReadUncommitted );
    578         goto end_prepare;
    579       }
    580     }
    581   }
    582 
    583   sqlite3VtabUnlockList(db);
    584 
    585   pParse->db = db;
    586   pParse->nQueryLoop = (double)1;
    587   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
    588     char *zSqlCopy;
    589     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
    590     testcase( nBytes==mxLen );
    591     testcase( nBytes==mxLen+1 );
    592     if( nBytes>mxLen ){
    593       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
    594       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
    595       goto end_prepare;
    596     }
    597     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
    598     if( zSqlCopy ){
    599       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
    600       sqlite3DbFree(db, zSqlCopy);
    601       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
    602     }else{
    603       pParse->zTail = &zSql[nBytes];
    604     }
    605   }else{
    606     sqlite3RunParser(pParse, zSql, &zErrMsg);
    607   }
    608   assert( 1==(int)pParse->nQueryLoop );
    609 
    610   if( db->mallocFailed ){
    611     pParse->rc = SQLITE_NOMEM;
    612   }
    613   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
    614   if( pParse->checkSchema ){
    615     schemaIsValid(pParse);
    616   }
    617   if( db->mallocFailed ){
    618     pParse->rc = SQLITE_NOMEM;
    619   }
    620   if( pzTail ){
    621     *pzTail = pParse->zTail;
    622   }
    623   rc = pParse->rc;
    624 
    625 #ifndef SQLITE_OMIT_EXPLAIN
    626   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
    627     static const char * const azColName[] = {
    628        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
    629        "selectid", "order", "from", "detail"
    630     };
    631     int iFirst, mx;
    632     if( pParse->explain==2 ){
    633       sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
    634       iFirst = 8;
    635       mx = 12;
    636     }else{
    637       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
    638       iFirst = 0;
    639       mx = 8;
    640     }
    641     for(i=iFirst; i<mx; i++){
    642       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
    643                             azColName[i], SQLITE_STATIC);
    644     }
    645   }
    646 #endif
    647 
    648   assert( db->init.busy==0 || saveSqlFlag==0 );
    649   if( db->init.busy==0 ){
    650     Vdbe *pVdbe = pParse->pVdbe;
    651     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
    652   }
    653   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
    654     sqlite3VdbeFinalize(pParse->pVdbe);
    655     assert(!(*ppStmt));
    656   }else{
    657     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
    658   }
    659 
    660   if( zErrMsg ){
    661     sqlite3Error(db, rc, "%s", zErrMsg);
    662     sqlite3DbFree(db, zErrMsg);
    663   }else{
    664     sqlite3Error(db, rc, 0);
    665   }
    666 
    667   /* Delete any TriggerPrg structures allocated while parsing this statement. */
    668   while( pParse->pTriggerPrg ){
    669     TriggerPrg *pT = pParse->pTriggerPrg;
    670     pParse->pTriggerPrg = pT->pNext;
    671     sqlite3DbFree(db, pT);
    672   }
    673 
    674 end_prepare:
    675 
    676   sqlite3StackFree(db, pParse);
    677   rc = sqlite3ApiExit(db, rc);
    678   assert( (rc&db->errMask)==rc );
    679   return rc;
    680 }
    681 static int sqlite3LockAndPrepare(
    682   sqlite3 *db,              /* Database handle. */
    683   const char *zSql,         /* UTF-8 encoded SQL statement. */
    684   int nBytes,               /* Length of zSql in bytes. */
    685   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
    686   Vdbe *pOld,               /* VM being reprepared */
    687   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    688   const char **pzTail       /* OUT: End of parsed string */
    689 ){
    690   int rc;
    691   assert( ppStmt!=0 );
    692   *ppStmt = 0;
    693   if( !sqlite3SafetyCheckOk(db) ){
    694     return SQLITE_MISUSE_BKPT;
    695   }
    696   sqlite3_mutex_enter(db->mutex);
    697   sqlite3BtreeEnterAll(db);
    698   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
    699   if( rc==SQLITE_SCHEMA ){
    700     sqlite3_finalize(*ppStmt);
    701     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
    702   }
    703   sqlite3BtreeLeaveAll(db);
    704   sqlite3_mutex_leave(db->mutex);
    705   return rc;
    706 }
    707 
    708 /*
    709 ** Rerun the compilation of a statement after a schema change.
    710 **
    711 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
    712 ** if the statement cannot be recompiled because another connection has
    713 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
    714 ** occurs, return SQLITE_SCHEMA.
    715 */
    716 int sqlite3Reprepare(Vdbe *p){
    717   int rc;
    718   sqlite3_stmt *pNew;
    719   const char *zSql;
    720   sqlite3 *db;
    721 
    722   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
    723   zSql = sqlite3_sql((sqlite3_stmt *)p);
    724   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
    725   db = sqlite3VdbeDb(p);
    726   assert( sqlite3_mutex_held(db->mutex) );
    727   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
    728   if( rc ){
    729     if( rc==SQLITE_NOMEM ){
    730       db->mallocFailed = 1;
    731     }
    732     assert( pNew==0 );
    733     return rc;
    734   }else{
    735     assert( pNew!=0 );
    736   }
    737   sqlite3VdbeSwap((Vdbe*)pNew, p);
    738   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
    739   sqlite3VdbeResetStepResult((Vdbe*)pNew);
    740   sqlite3VdbeFinalize((Vdbe*)pNew);
    741   return SQLITE_OK;
    742 }
    743 
    744 
    745 /*
    746 ** Two versions of the official API.  Legacy and new use.  In the legacy
    747 ** version, the original SQL text is not saved in the prepared statement
    748 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
    749 ** sqlite3_step().  In the new version, the original SQL text is retained
    750 ** and the statement is automatically recompiled if an schema change
    751 ** occurs.
    752 */
    753 int sqlite3_prepare(
    754   sqlite3 *db,              /* Database handle. */
    755   const char *zSql,         /* UTF-8 encoded SQL statement. */
    756   int nBytes,               /* Length of zSql in bytes. */
    757   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    758   const char **pzTail       /* OUT: End of parsed string */
    759 ){
    760   int rc;
    761   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
    762   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
    763   return rc;
    764 }
    765 int sqlite3_prepare_v2(
    766   sqlite3 *db,              /* Database handle. */
    767   const char *zSql,         /* UTF-8 encoded SQL statement. */
    768   int nBytes,               /* Length of zSql in bytes. */
    769   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    770   const char **pzTail       /* OUT: End of parsed string */
    771 ){
    772   int rc;
    773   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
    774   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
    775   return rc;
    776 }
    777 
    778 
    779 #ifndef SQLITE_OMIT_UTF16
    780 /*
    781 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
    782 */
    783 static int sqlite3Prepare16(
    784   sqlite3 *db,              /* Database handle. */
    785   const void *zSql,         /* UTF-16 encoded SQL statement. */
    786   int nBytes,               /* Length of zSql in bytes. */
    787   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
    788   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    789   const void **pzTail       /* OUT: End of parsed string */
    790 ){
    791   /* This function currently works by first transforming the UTF-16
    792   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
    793   ** tricky bit is figuring out the pointer to return in *pzTail.
    794   */
    795   char *zSql8;
    796   const char *zTail8 = 0;
    797   int rc = SQLITE_OK;
    798 
    799   assert( ppStmt );
    800   *ppStmt = 0;
    801   if( !sqlite3SafetyCheckOk(db) ){
    802     return SQLITE_MISUSE_BKPT;
    803   }
    804   sqlite3_mutex_enter(db->mutex);
    805   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
    806   if( zSql8 ){
    807     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
    808   }
    809 
    810   if( zTail8 && pzTail ){
    811     /* If sqlite3_prepare returns a tail pointer, we calculate the
    812     ** equivalent pointer into the UTF-16 string by counting the unicode
    813     ** characters between zSql8 and zTail8, and then returning a pointer
    814     ** the same number of characters into the UTF-16 string.
    815     */
    816     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
    817     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
    818   }
    819   sqlite3DbFree(db, zSql8);
    820   rc = sqlite3ApiExit(db, rc);
    821   sqlite3_mutex_leave(db->mutex);
    822   return rc;
    823 }
    824 
    825 /*
    826 ** Two versions of the official API.  Legacy and new use.  In the legacy
    827 ** version, the original SQL text is not saved in the prepared statement
    828 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
    829 ** sqlite3_step().  In the new version, the original SQL text is retained
    830 ** and the statement is automatically recompiled if an schema change
    831 ** occurs.
    832 */
    833 int sqlite3_prepare16(
    834   sqlite3 *db,              /* Database handle. */
    835   const void *zSql,         /* UTF-16 encoded SQL statement. */
    836   int nBytes,               /* Length of zSql in bytes. */
    837   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    838   const void **pzTail       /* OUT: End of parsed string */
    839 ){
    840   int rc;
    841   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
    842   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
    843   return rc;
    844 }
    845 int sqlite3_prepare16_v2(
    846   sqlite3 *db,              /* Database handle. */
    847   const void *zSql,         /* UTF-16 encoded SQL statement. */
    848   int nBytes,               /* Length of zSql in bytes. */
    849   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
    850   const void **pzTail       /* OUT: End of parsed string */
    851 ){
    852   int rc;
    853   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
    854   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
    855   return rc;
    856 }
    857 
    858 #endif /* SQLITE_OMIT_UTF16 */
    859