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