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