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