Add tests to bestindexC.test. No changes to code.
[sqlite.git] / src / prepare.c
blobdf9c98f7438eb08c1c631202c7f34b5bac94d25d
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.
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.
16 #include "sqliteInt.h"
19 ** Fill the InitData structure with an error message that indicates
20 ** that the database is corrupt.
22 static void corruptSchema(
23 InitData *pData, /* Initialization context */
24 char **azObj, /* Type and name of object being parsed */
25 const char *zExtra /* Error information */
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_AlterMask) ){
33 static const char *azAlterType[] = {
34 "rename",
35 "drop column",
36 "add column"
38 *pData->pzErrMsg = sqlite3MPrintf(db,
39 "error in %s %s after %s: %s", azObj[0], azObj[1],
40 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
41 zExtra
43 pData->rc = SQLITE_ERROR;
44 }else if( db->flags & SQLITE_WriteSchema ){
45 pData->rc = SQLITE_CORRUPT_BKPT;
46 }else{
47 char *z;
48 const char *zObj = azObj[1] ? azObj[1] : "?";
49 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
50 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
51 *pData->pzErrMsg = z;
52 pData->rc = SQLITE_CORRUPT_BKPT;
57 ** Check to see if any sibling index (another index on the same table)
58 ** of pIndex has the same root page number, and if it does, return true.
59 ** This would indicate a corrupt schema.
61 int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
62 Index *p;
63 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
64 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
66 return 0;
69 /* forward declaration */
70 static int sqlite3Prepare(
71 sqlite3 *db, /* Database handle. */
72 const char *zSql, /* UTF-8 encoded SQL statement. */
73 int nBytes, /* Length of zSql in bytes. */
74 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
75 Vdbe *pReprepare, /* VM being reprepared */
76 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
77 const char **pzTail /* OUT: End of parsed string */
82 ** This is the callback routine for the code that initializes the
83 ** database. See sqlite3Init() below for additional information.
84 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
86 ** Each callback contains the following information:
88 ** argv[0] = type of object: "table", "index", "trigger", or "view".
89 ** argv[1] = name of thing being created
90 ** argv[2] = associated table if an index or trigger
91 ** argv[3] = root page number for table or index. 0 for trigger or view.
92 ** argv[4] = SQL text for the CREATE statement.
95 int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
96 InitData *pData = (InitData*)pInit;
97 sqlite3 *db = pData->db;
98 int iDb = pData->iDb;
100 assert( argc==5 );
101 UNUSED_PARAMETER2(NotUsed, argc);
102 assert( sqlite3_mutex_held(db->mutex) );
103 db->mDbFlags |= DBFLAG_EncodingFixed;
104 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
105 pData->nInitRow++;
106 if( db->mallocFailed ){
107 corruptSchema(pData, argv, 0);
108 return 1;
111 assert( iDb>=0 && iDb<db->nDb );
112 if( argv[3]==0 ){
113 corruptSchema(pData, argv, 0);
114 }else if( argv[4]
115 && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]
116 && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){
117 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
118 ** But because db->init.busy is set to 1, no VDBE code is generated
119 ** or executed. All the parser does is build the internal data
120 ** structures that describe the table, index, or view.
122 ** No other valid SQL statement, other than the variable CREATE statements,
123 ** can begin with the letters "C" and "R". Thus, it is not possible run
124 ** any other kind of statement while parsing the schema, even a corrupt
125 ** schema.
127 int rc;
128 u8 saved_iDb = db->init.iDb;
129 sqlite3_stmt *pStmt;
130 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
132 assert( db->init.busy );
133 db->init.iDb = iDb;
134 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
135 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
137 if( sqlite3Config.bExtraSchemaChecks ){
138 corruptSchema(pData, argv, "invalid rootpage");
141 db->init.orphanTrigger = 0;
142 db->init.azInit = (const char**)argv;
143 pStmt = 0;
144 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
145 rc = db->errCode;
146 assert( (rc&0xFF)==(rcp&0xFF) );
147 db->init.iDb = saved_iDb;
148 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
149 if( SQLITE_OK!=rc ){
150 if( db->init.orphanTrigger ){
151 assert( iDb==1 );
152 }else{
153 if( rc > pData->rc ) pData->rc = rc;
154 if( rc==SQLITE_NOMEM ){
155 sqlite3OomFault(db);
156 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
157 corruptSchema(pData, argv, sqlite3_errmsg(db));
161 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
162 sqlite3_finalize(pStmt);
163 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
164 corruptSchema(pData, argv, 0);
165 }else{
166 /* If the SQL column is blank it means this is an index that
167 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
168 ** constraint for a CREATE TABLE. The index should have already
169 ** been created when we processed the CREATE TABLE. All we have
170 ** to do here is record the root page number for that index.
172 Index *pIndex;
173 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
174 if( pIndex==0 ){
175 corruptSchema(pData, argv, "orphan index");
176 }else
177 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
178 || pIndex->tnum<2
179 || pIndex->tnum>pData->mxPage
180 || sqlite3IndexHasDuplicateRootPage(pIndex)
182 if( sqlite3Config.bExtraSchemaChecks ){
183 corruptSchema(pData, argv, "invalid rootpage");
187 return 0;
191 ** Attempt to read the database schema and initialize internal
192 ** data structures for a single database file. The index of the
193 ** database file is given by iDb. iDb==0 is used for the main
194 ** database. iDb==1 should never be used. iDb>=2 is used for
195 ** auxiliary databases. Return one of the SQLITE_ error codes to
196 ** indicate success or failure.
198 int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
199 int rc;
200 int i;
201 #ifndef SQLITE_OMIT_DEPRECATED
202 int size;
203 #endif
204 Db *pDb;
205 char const *azArg[6];
206 int meta[5];
207 InitData initData;
208 const char *zSchemaTabName;
209 int openedTransaction = 0;
210 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
212 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
213 assert( iDb>=0 && iDb<db->nDb );
214 assert( db->aDb[iDb].pSchema );
215 assert( sqlite3_mutex_held(db->mutex) );
216 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
218 db->init.busy = 1;
220 /* Construct the in-memory representation schema tables (sqlite_schema or
221 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
222 ** table name will be inserted automatically by the parser so we can just
223 ** use the abbreviation "x" here. The parser will also automatically tag
224 ** the schema table as read-only. */
225 azArg[0] = "table";
226 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb);
227 azArg[2] = azArg[1];
228 azArg[3] = "1";
229 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
230 "rootpage int,sql text)";
231 azArg[5] = 0;
232 initData.db = db;
233 initData.iDb = iDb;
234 initData.rc = SQLITE_OK;
235 initData.pzErrMsg = pzErrMsg;
236 initData.mInitFlags = mFlags;
237 initData.nInitRow = 0;
238 initData.mxPage = 0;
239 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
240 db->mDbFlags &= mask;
241 if( initData.rc ){
242 rc = initData.rc;
243 goto error_out;
246 /* Create a cursor to hold the database open
248 pDb = &db->aDb[iDb];
249 if( pDb->pBt==0 ){
250 assert( iDb==1 );
251 DbSetProperty(db, 1, DB_SchemaLoaded);
252 rc = SQLITE_OK;
253 goto error_out;
256 /* If there is not already a read-only (or read-write) transaction opened
257 ** on the b-tree database, open one now. If a transaction is opened, it
258 ** will be closed before this function returns. */
259 sqlite3BtreeEnter(pDb->pBt);
260 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
261 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
262 if( rc!=SQLITE_OK ){
263 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
264 goto initone_error_out;
266 openedTransaction = 1;
269 /* Get the database meta information.
271 ** Meta values are as follows:
272 ** meta[0] Schema cookie. Changes with each schema change.
273 ** meta[1] File format of schema layer.
274 ** meta[2] Size of the page cache.
275 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
276 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
277 ** meta[5] User version
278 ** meta[6] Incremental vacuum mode
279 ** meta[7] unused
280 ** meta[8] unused
281 ** meta[9] unused
283 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
284 ** the possible values of meta[4].
286 for(i=0; i<ArraySize(meta); i++){
287 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
289 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
290 memset(meta, 0, sizeof(meta));
292 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
294 /* If opening a non-empty database, check the text encoding. For the
295 ** main database, set sqlite3.enc to the encoding of the main database.
296 ** For an attached db, it is an error if the encoding is not the same
297 ** as sqlite3.enc.
299 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
300 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
301 u8 encoding;
302 #ifndef SQLITE_OMIT_UTF16
303 /* If opening the main database, set ENC(db). */
304 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
305 if( encoding==0 ) encoding = SQLITE_UTF8;
306 #else
307 encoding = SQLITE_UTF8;
308 #endif
309 if( db->nVdbeActive>0 && encoding!=ENC(db)
310 && (db->mDbFlags & DBFLAG_Vacuum)==0
312 rc = SQLITE_LOCKED;
313 goto initone_error_out;
314 }else{
315 sqlite3SetTextEncoding(db, encoding);
317 }else{
318 /* If opening an attached database, the encoding much match ENC(db) */
319 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
320 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
321 " text encoding as main database");
322 rc = SQLITE_ERROR;
323 goto initone_error_out;
327 pDb->pSchema->enc = ENC(db);
329 if( pDb->pSchema->cache_size==0 ){
330 #ifndef SQLITE_OMIT_DEPRECATED
331 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
332 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
333 pDb->pSchema->cache_size = size;
334 #else
335 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
336 #endif
337 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
341 ** file_format==1 Version 3.0.0.
342 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
343 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
344 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
346 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
347 if( pDb->pSchema->file_format==0 ){
348 pDb->pSchema->file_format = 1;
350 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
351 sqlite3SetString(pzErrMsg, db, "unsupported file format");
352 rc = SQLITE_ERROR;
353 goto initone_error_out;
356 /* Ticket #2804: When we open a database in the newer file format,
357 ** clear the legacy_file_format pragma flag so that a VACUUM will
358 ** not downgrade the database and thus invalidate any descending
359 ** indices that the user might have created.
361 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
362 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
365 /* Read the schema information out of the schema tables
367 assert( db->init.busy );
368 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
370 char *zSql;
371 zSql = sqlite3MPrintf(db,
372 "SELECT*FROM\"%w\".%s ORDER BY rowid",
373 db->aDb[iDb].zDbSName, zSchemaTabName);
374 #ifndef SQLITE_OMIT_AUTHORIZATION
376 sqlite3_xauth xAuth;
377 xAuth = db->xAuth;
378 db->xAuth = 0;
379 #endif
380 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
381 #ifndef SQLITE_OMIT_AUTHORIZATION
382 db->xAuth = xAuth;
384 #endif
385 if( rc==SQLITE_OK ) rc = initData.rc;
386 sqlite3DbFree(db, zSql);
387 #ifndef SQLITE_OMIT_ANALYZE
388 if( rc==SQLITE_OK ){
389 sqlite3AnalysisLoad(db, iDb);
391 #endif
393 assert( pDb == &(db->aDb[iDb]) );
394 if( db->mallocFailed ){
395 rc = SQLITE_NOMEM_BKPT;
396 sqlite3ResetAllSchemasOfConnection(db);
397 pDb = &db->aDb[iDb];
398 }else
399 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
400 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
401 ** the schema loaded, even if errors (other than OOM) occurred. In
402 ** this situation the current sqlite3_prepare() operation will fail,
403 ** but the following one will attempt to compile the supplied statement
404 ** against whatever subset of the schema was loaded before the error
405 ** occurred.
407 ** The primary purpose of this is to allow access to the sqlite_schema
408 ** table even when its contents have been corrupted.
410 DbSetProperty(db, iDb, DB_SchemaLoaded);
411 rc = SQLITE_OK;
414 /* Jump here for an error that occurs after successfully allocating
415 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
416 ** before that point, jump to error_out.
418 initone_error_out:
419 if( openedTransaction ){
420 sqlite3BtreeCommit(pDb->pBt);
422 sqlite3BtreeLeave(pDb->pBt);
424 error_out:
425 if( rc ){
426 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
427 sqlite3OomFault(db);
429 sqlite3ResetOneSchema(db, iDb);
431 db->init.busy = 0;
432 return rc;
436 ** Initialize all database files - the main database file, the file
437 ** used to store temporary tables, and any additional database files
438 ** created using ATTACH statements. Return a success code. If an
439 ** error occurs, write an error message into *pzErrMsg.
441 ** After a database is initialized, the DB_SchemaLoaded bit is set
442 ** bit is set in the flags field of the Db structure.
444 int sqlite3Init(sqlite3 *db, char **pzErrMsg){
445 int i, rc;
446 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
448 assert( sqlite3_mutex_held(db->mutex) );
449 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
450 assert( db->init.busy==0 );
451 ENC(db) = SCHEMA_ENC(db);
452 assert( db->nDb>0 );
453 /* Do the main schema first */
454 if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
455 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
456 if( rc ) return rc;
458 /* All other schemas after the main schema. The "temp" schema must be last */
459 for(i=db->nDb-1; i>0; i--){
460 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
461 if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
462 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
463 if( rc ) return rc;
466 if( commit_internal ){
467 sqlite3CommitInternalChanges(db);
469 return SQLITE_OK;
473 ** This routine is a no-op if the database schema is already initialized.
474 ** Otherwise, the schema is loaded. An error code is returned.
476 int sqlite3ReadSchema(Parse *pParse){
477 int rc = SQLITE_OK;
478 sqlite3 *db = pParse->db;
479 assert( sqlite3_mutex_held(db->mutex) );
480 if( !db->init.busy ){
481 rc = sqlite3Init(db, &pParse->zErrMsg);
482 if( rc!=SQLITE_OK ){
483 pParse->rc = rc;
484 pParse->nErr++;
485 }else if( db->noSharedCache ){
486 db->mDbFlags |= DBFLAG_SchemaKnownOk;
489 return rc;
494 ** Check schema cookies in all databases. If any cookie is out
495 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
496 ** make no changes to pParse->rc.
498 static void schemaIsValid(Parse *pParse){
499 sqlite3 *db = pParse->db;
500 int iDb;
501 int rc;
502 int cookie;
504 assert( pParse->checkSchema );
505 assert( sqlite3_mutex_held(db->mutex) );
506 for(iDb=0; iDb<db->nDb; iDb++){
507 int openedTransaction = 0; /* True if a transaction is opened */
508 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
509 if( pBt==0 ) continue;
511 /* If there is not already a read-only (or read-write) transaction opened
512 ** on the b-tree database, open one now. If a transaction is opened, it
513 ** will be closed immediately after reading the meta-value. */
514 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE ){
515 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
516 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
517 sqlite3OomFault(db);
518 pParse->rc = SQLITE_NOMEM;
520 if( rc!=SQLITE_OK ) return;
521 openedTransaction = 1;
524 /* Read the schema cookie from the database. If it does not match the
525 ** value stored as part of the in-memory schema representation,
526 ** set Parse.rc to SQLITE_SCHEMA. */
527 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
528 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
529 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
530 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
531 sqlite3ResetOneSchema(db, iDb);
534 /* Close the transaction, if one was opened. */
535 if( openedTransaction ){
536 sqlite3BtreeCommit(pBt);
542 ** Convert a schema pointer into the iDb index that indicates
543 ** which database file in db->aDb[] the schema refers to.
545 ** If the same database is attached more than once, the first
546 ** attached database is returned.
548 int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
549 int i = -32768;
551 /* If pSchema is NULL, then return -32768. This happens when code in
552 ** expr.c is trying to resolve a reference to a transient table (i.e. one
553 ** created by a sub-select). In this case the return value of this
554 ** function should never be used.
556 ** We return -32768 instead of the more usual -1 simply because using
557 ** -32768 as the incorrect index into db->aDb[] is much
558 ** more likely to cause a segfault than -1 (of course there are assert()
559 ** statements too, but it never hurts to play the odds) and
560 ** -32768 will still fit into a 16-bit signed integer.
562 assert( sqlite3_mutex_held(db->mutex) );
563 if( pSchema ){
564 for(i=0; 1; i++){
565 assert( i<db->nDb );
566 if( db->aDb[i].pSchema==pSchema ){
567 break;
570 assert( i>=0 && i<db->nDb );
572 return i;
576 ** Free all memory allocations in the pParse object
578 void sqlite3ParseObjectReset(Parse *pParse){
579 sqlite3 *db = pParse->db;
580 assert( db!=0 );
581 assert( db->pParse==pParse );
582 assert( pParse->nested==0 );
583 #ifndef SQLITE_OMIT_SHARED_CACHE
584 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
585 #endif
586 while( pParse->pCleanup ){
587 ParseCleanup *pCleanup = pParse->pCleanup;
588 pParse->pCleanup = pCleanup->pNext;
589 pCleanup->xCleanup(db, pCleanup->pPtr);
590 sqlite3DbNNFreeNN(db, pCleanup);
592 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
593 if( pParse->pConstExpr ){
594 sqlite3ExprListDelete(db, pParse->pConstExpr);
596 assert( db->lookaside.bDisable >= pParse->disableLookaside );
597 db->lookaside.bDisable -= pParse->disableLookaside;
598 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
599 assert( pParse->db->pParse==pParse );
600 db->pParse = pParse->pOuterParse;
604 ** Add a new cleanup operation to a Parser. The cleanup should happen when
605 ** the parser object is destroyed. But, beware: the cleanup might happen
606 ** immediately.
608 ** Use this mechanism for uncommon cleanups. There is a higher setup
609 ** cost for this mechanism (an extra malloc), so it should not be used
610 ** for common cleanups that happen on most calls. But for less
611 ** common cleanups, we save a single NULL-pointer comparison in
612 ** sqlite3ParseObjectReset(), which reduces the total CPU cycle count.
614 ** If a memory allocation error occurs, then the cleanup happens immediately.
615 ** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
616 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
617 ** that test cases exist for which this happens, to guard against possible
618 ** use-after-free errors following an OOM. The preferred way to do this is
619 ** to immediately follow the call to this routine with:
621 ** testcase( pParse->earlyCleanup );
623 ** This routine returns a copy of its pPtr input (the third parameter)
624 ** except if an early cleanup occurs, in which case it returns NULL. So
625 ** another way to check for early cleanup is to check the return value.
626 ** Or, stop using the pPtr parameter with this call and use only its
627 ** return value thereafter. Something like this:
629 ** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);
631 void *sqlite3ParserAddCleanup(
632 Parse *pParse, /* Destroy when this Parser finishes */
633 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
634 void *pPtr /* Pointer to object to be cleaned up */
636 ParseCleanup *pCleanup;
637 if( sqlite3FaultSim(300) ){
638 pCleanup = 0;
639 sqlite3OomFault(pParse->db);
640 }else{
641 pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
643 if( pCleanup ){
644 pCleanup->pNext = pParse->pCleanup;
645 pParse->pCleanup = pCleanup;
646 pCleanup->pPtr = pPtr;
647 pCleanup->xCleanup = xCleanup;
648 }else{
649 xCleanup(pParse->db, pPtr);
650 pPtr = 0;
651 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
652 pParse->earlyCleanup = 1;
653 #endif
655 return pPtr;
659 ** Turn bulk memory into a valid Parse object and link that Parse object
660 ** into database connection db.
662 ** Call sqlite3ParseObjectReset() to undo this operation.
664 ** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which
665 ** is generated by Lemon.
667 void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){
668 memset(PARSE_HDR(pParse), 0, PARSE_HDR_SZ);
669 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
670 assert( db->pParse!=pParse );
671 pParse->pOuterParse = db->pParse;
672 db->pParse = pParse;
673 pParse->db = db;
674 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
678 ** Maximum number of times that we will try again to prepare a statement
679 ** that returns SQLITE_ERROR_RETRY.
681 #ifndef SQLITE_MAX_PREPARE_RETRY
682 # define SQLITE_MAX_PREPARE_RETRY 25
683 #endif
686 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
688 static int sqlite3Prepare(
689 sqlite3 *db, /* Database handle. */
690 const char *zSql, /* UTF-8 encoded SQL statement. */
691 int nBytes, /* Length of zSql in bytes. */
692 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
693 Vdbe *pReprepare, /* VM being reprepared */
694 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
695 const char **pzTail /* OUT: End of parsed string */
697 int rc = SQLITE_OK; /* Result code */
698 int i; /* Loop counter */
699 Parse sParse; /* Parsing context */
701 /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */
702 memset(PARSE_HDR(&sParse), 0, PARSE_HDR_SZ);
703 memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
704 sParse.pOuterParse = db->pParse;
705 db->pParse = &sParse;
706 sParse.db = db;
707 if( pReprepare ){
708 sParse.pReprepare = pReprepare;
709 sParse.explain = sqlite3_stmt_isexplain((sqlite3_stmt*)pReprepare);
710 }else{
711 assert( sParse.pReprepare==0 );
713 assert( ppStmt && *ppStmt==0 );
714 if( db->mallocFailed ){
715 sqlite3ErrorMsg(&sParse, "out of memory");
716 db->errCode = rc = SQLITE_NOMEM;
717 goto end_prepare;
719 assert( sqlite3_mutex_held(db->mutex) );
721 /* For a long-term use prepared statement avoid the use of
722 ** lookaside memory.
724 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
725 sParse.disableLookaside++;
726 DisableLookaside;
728 sParse.prepFlags = prepFlags & 0xff;
730 /* Check to verify that it is possible to get a read lock on all
731 ** database schemas. The inability to get a read lock indicates that
732 ** some other database connection is holding a write-lock, which in
733 ** turn means that the other connection has made uncommitted changes
734 ** to the schema.
736 ** Were we to proceed and prepare the statement against the uncommitted
737 ** schema changes and if those schema changes are subsequently rolled
738 ** back and different changes are made in their place, then when this
739 ** prepared statement goes to run the schema cookie would fail to detect
740 ** the schema change. Disaster would follow.
742 ** This thread is currently holding mutexes on all Btrees (because
743 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
744 ** is not possible for another thread to start a new schema change
745 ** while this routine is running. Hence, we do not need to hold
746 ** locks on the schema, we just need to make sure nobody else is
747 ** holding them.
749 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
750 ** but it does *not* override schema lock detection, so this all still
751 ** works even if READ_UNCOMMITTED is set.
753 if( !db->noSharedCache ){
754 for(i=0; i<db->nDb; i++) {
755 Btree *pBt = db->aDb[i].pBt;
756 if( pBt ){
757 assert( sqlite3BtreeHoldsMutex(pBt) );
758 rc = sqlite3BtreeSchemaLocked(pBt);
759 if( rc ){
760 const char *zDb = db->aDb[i].zDbSName;
761 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
762 testcase( db->flags & SQLITE_ReadUncommit );
763 goto end_prepare;
769 #ifndef SQLITE_OMIT_VIRTUALTABLE
770 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
771 #endif
773 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
774 char *zSqlCopy;
775 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
776 testcase( nBytes==mxLen );
777 testcase( nBytes==mxLen+1 );
778 if( nBytes>mxLen ){
779 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
780 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
781 goto end_prepare;
783 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
784 if( zSqlCopy ){
785 sqlite3RunParser(&sParse, zSqlCopy);
786 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
787 sqlite3DbFree(db, zSqlCopy);
788 }else{
789 sParse.zTail = &zSql[nBytes];
791 }else{
792 sqlite3RunParser(&sParse, zSql);
794 assert( 0==sParse.nQueryLoop );
796 if( pzTail ){
797 *pzTail = sParse.zTail;
800 if( db->init.busy==0 ){
801 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
803 if( db->mallocFailed ){
804 sParse.rc = SQLITE_NOMEM_BKPT;
805 sParse.checkSchema = 0;
807 if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){
808 if( sParse.checkSchema && db->init.busy==0 ){
809 schemaIsValid(&sParse);
811 if( sParse.pVdbe ){
812 sqlite3VdbeFinalize(sParse.pVdbe);
814 assert( 0==(*ppStmt) );
815 rc = sParse.rc;
816 if( sParse.zErrMsg ){
817 sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);
818 sqlite3DbFree(db, sParse.zErrMsg);
819 }else{
820 sqlite3Error(db, rc);
822 }else{
823 assert( sParse.zErrMsg==0 );
824 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
825 rc = SQLITE_OK;
826 sqlite3ErrorClear(db);
830 /* Delete any TriggerPrg structures allocated while parsing this statement. */
831 while( sParse.pTriggerPrg ){
832 TriggerPrg *pT = sParse.pTriggerPrg;
833 sParse.pTriggerPrg = pT->pNext;
834 sqlite3DbFree(db, pT);
837 end_prepare:
839 sqlite3ParseObjectReset(&sParse);
840 return rc;
842 static int sqlite3LockAndPrepare(
843 sqlite3 *db, /* Database handle. */
844 const char *zSql, /* UTF-8 encoded SQL statement. */
845 int nBytes, /* Length of zSql in bytes. */
846 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
847 Vdbe *pOld, /* VM being reprepared */
848 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
849 const char **pzTail /* OUT: End of parsed string */
851 int rc;
852 int cnt = 0;
854 #ifdef SQLITE_ENABLE_API_ARMOR
855 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
856 #endif
857 *ppStmt = 0;
858 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
859 return SQLITE_MISUSE_BKPT;
861 sqlite3_mutex_enter(db->mutex);
862 sqlite3BtreeEnterAll(db);
864 /* Make multiple attempts to compile the SQL, until it either succeeds
865 ** or encounters a permanent error. A schema problem after one schema
866 ** reset is considered a permanent error. */
867 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
868 assert( rc==SQLITE_OK || *ppStmt==0 );
869 if( rc==SQLITE_OK || db->mallocFailed ) break;
870 }while( (rc==SQLITE_ERROR_RETRY && (cnt++)<SQLITE_MAX_PREPARE_RETRY)
871 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
872 sqlite3BtreeLeaveAll(db);
873 rc = sqlite3ApiExit(db, rc);
874 assert( (rc&db->errMask)==rc );
875 db->busyHandler.nBusy = 0;
876 sqlite3_mutex_leave(db->mutex);
877 assert( rc==SQLITE_OK || (*ppStmt)==0 );
878 return rc;
883 ** Rerun the compilation of a statement after a schema change.
885 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
886 ** if the statement cannot be recompiled because another connection has
887 ** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
888 ** occurs, return SQLITE_SCHEMA.
890 int sqlite3Reprepare(Vdbe *p){
891 int rc;
892 sqlite3_stmt *pNew;
893 const char *zSql;
894 sqlite3 *db;
895 u8 prepFlags;
897 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
898 zSql = sqlite3_sql((sqlite3_stmt *)p);
899 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
900 db = sqlite3VdbeDb(p);
901 assert( sqlite3_mutex_held(db->mutex) );
902 prepFlags = sqlite3VdbePrepareFlags(p);
903 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
904 if( rc ){
905 if( rc==SQLITE_NOMEM ){
906 sqlite3OomFault(db);
908 assert( pNew==0 );
909 return rc;
910 }else{
911 assert( pNew!=0 );
913 sqlite3VdbeSwap((Vdbe*)pNew, p);
914 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
915 sqlite3VdbeResetStepResult((Vdbe*)pNew);
916 sqlite3VdbeFinalize((Vdbe*)pNew);
917 return SQLITE_OK;
922 ** Two versions of the official API. Legacy and new use. In the legacy
923 ** version, the original SQL text is not saved in the prepared statement
924 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
925 ** sqlite3_step(). In the new version, the original SQL text is retained
926 ** and the statement is automatically recompiled if an schema change
927 ** occurs.
929 int sqlite3_prepare(
930 sqlite3 *db, /* Database handle. */
931 const char *zSql, /* UTF-8 encoded SQL statement. */
932 int nBytes, /* Length of zSql in bytes. */
933 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
934 const char **pzTail /* OUT: End of parsed string */
936 int rc;
937 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
938 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
939 return rc;
941 int sqlite3_prepare_v2(
942 sqlite3 *db, /* Database handle. */
943 const char *zSql, /* UTF-8 encoded SQL statement. */
944 int nBytes, /* Length of zSql in bytes. */
945 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
946 const char **pzTail /* OUT: End of parsed string */
948 int rc;
949 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
950 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
951 ** parameter.
953 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
954 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
955 ppStmt,pzTail);
956 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
957 return rc;
959 int sqlite3_prepare_v3(
960 sqlite3 *db, /* Database handle. */
961 const char *zSql, /* UTF-8 encoded SQL statement. */
962 int nBytes, /* Length of zSql in bytes. */
963 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
964 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
965 const char **pzTail /* OUT: End of parsed string */
967 int rc;
968 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
969 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
970 ** which is a bit array consisting of zero or more of the
971 ** SQLITE_PREPARE_* flags.
973 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
974 ** directly above. */
975 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
976 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
977 0,ppStmt,pzTail);
978 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
979 return rc;
983 #ifndef SQLITE_OMIT_UTF16
985 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
987 static int sqlite3Prepare16(
988 sqlite3 *db, /* Database handle. */
989 const void *zSql, /* UTF-16 encoded SQL statement. */
990 int nBytes, /* Length of zSql in bytes. */
991 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
992 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
993 const void **pzTail /* OUT: End of parsed string */
995 /* This function currently works by first transforming the UTF-16
996 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
997 ** tricky bit is figuring out the pointer to return in *pzTail.
999 char *zSql8;
1000 const char *zTail8 = 0;
1001 int rc = SQLITE_OK;
1003 #ifdef SQLITE_ENABLE_API_ARMOR
1004 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
1005 #endif
1006 *ppStmt = 0;
1007 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
1008 return SQLITE_MISUSE_BKPT;
1010 if( nBytes>=0 ){
1011 int sz;
1012 const char *z = (const char*)zSql;
1013 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
1014 nBytes = sz;
1016 sqlite3_mutex_enter(db->mutex);
1017 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
1018 if( zSql8 ){
1019 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
1022 if( zTail8 && pzTail ){
1023 /* If sqlite3_prepare returns a tail pointer, we calculate the
1024 ** equivalent pointer into the UTF-16 string by counting the unicode
1025 ** characters between zSql8 and zTail8, and then returning a pointer
1026 ** the same number of characters into the UTF-16 string.
1028 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
1029 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
1031 sqlite3DbFree(db, zSql8);
1032 rc = sqlite3ApiExit(db, rc);
1033 sqlite3_mutex_leave(db->mutex);
1034 return rc;
1038 ** Two versions of the official API. Legacy and new use. In the legacy
1039 ** version, the original SQL text is not saved in the prepared statement
1040 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
1041 ** sqlite3_step(). In the new version, the original SQL text is retained
1042 ** and the statement is automatically recompiled if an schema change
1043 ** occurs.
1045 int sqlite3_prepare16(
1046 sqlite3 *db, /* Database handle. */
1047 const void *zSql, /* UTF-16 encoded SQL statement. */
1048 int nBytes, /* Length of zSql in bytes. */
1049 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1050 const void **pzTail /* OUT: End of parsed string */
1052 int rc;
1053 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
1054 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
1055 return rc;
1057 int sqlite3_prepare16_v2(
1058 sqlite3 *db, /* Database handle. */
1059 const void *zSql, /* UTF-16 encoded SQL statement. */
1060 int nBytes, /* Length of zSql in bytes. */
1061 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1062 const void **pzTail /* OUT: End of parsed string */
1064 int rc;
1065 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
1066 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
1067 return rc;
1069 int sqlite3_prepare16_v3(
1070 sqlite3 *db, /* Database handle. */
1071 const void *zSql, /* UTF-16 encoded SQL statement. */
1072 int nBytes, /* Length of zSql in bytes. */
1073 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
1074 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1075 const void **pzTail /* OUT: End of parsed string */
1077 int rc;
1078 rc = sqlite3Prepare16(db,zSql,nBytes,
1079 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
1080 ppStmt,pzTail);
1081 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
1082 return rc;
1085 #endif /* SQLITE_OMIT_UTF16 */