4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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
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 const char *zObj
, /* Object being parsed at the point of error */
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_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
;
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
);
43 pData
->rc
= SQLITE_CORRUPT_BKPT
;
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.
52 int sqlite3IndexHasDuplicateRootPage(Index
*pIndex
){
54 for(p
=pIndex
->pTable
->pIndex
; p
; p
=p
->pNext
){
55 if( p
->tnum
==pIndex
->tnum
&& p
!=pIndex
) return 1;
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.
65 ** Each callback contains the following information:
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.
72 int sqlite3InitCallback(void *pInit
, int argc
, char **argv
, char **NotUsed
){
73 InitData
*pData
= (InitData
*)pInit
;
74 sqlite3
*db
= pData
->db
;
78 UNUSED_PARAMETER2(NotUsed
, argc
);
79 assert( sqlite3_mutex_held(db
->mutex
) );
80 DbClearProperty(db
, iDb
, DB_Empty
);
82 if( db
->mallocFailed
){
83 corruptSchema(pData
, argv
[0], 0);
87 assert( iDb
>=0 && iDb
<db
->nDb
);
88 if( argv
==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
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.
98 u8 saved_iDb
= db
->init
.iDb
;
100 TESTONLY(int rcp
); /* Return code from sqlite3_prepare() */
102 assert( db
->init
.busy
);
104 db
->init
.newTnum
= sqlite3Atoi(argv
[1]);
105 db
->init
.orphanTrigger
= 0;
106 TESTONLY(rcp
= ) sqlite3_prepare(db
, argv
[2], -1, &pStmt
, 0);
108 assert( (rc
&0xFF)==(rcp
&0xFF) );
109 db
->init
.iDb
= saved_iDb
;
110 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
112 if( db
->init
.orphanTrigger
){
116 if( rc
==SQLITE_NOMEM
){
118 }else if( rc
!=SQLITE_INTERRUPT
&& (rc
&0xFF)!=SQLITE_LOCKED
){
119 corruptSchema(pData
, argv
[0], sqlite3_errmsg(db
));
123 sqlite3_finalize(pStmt
);
124 }else if( argv
[0]==0 || (argv
[2]!=0 && argv
[2][0]!=0) ){
125 corruptSchema(pData
, argv
[0], 0);
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.
134 pIndex
= sqlite3FindIndex(db
, argv
[0], db
->aDb
[iDb
].zDbSName
);
136 || sqlite3GetInt32(argv
[1],&pIndex
->tnum
)==0
138 || sqlite3IndexHasDuplicateRootPage(pIndex
)
140 corruptSchema(pData
, argv
[0], pIndex
?"invalid rootpage":"orphan index");
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.
154 int sqlite3InitOne(sqlite3
*db
, int iDb
, char **pzErrMsg
, u32 mFlags
){
157 #ifndef SQLITE_OMIT_DEPRECATED
161 char const *azArg
[4];
164 const char *zMasterName
;
165 int openedTransaction
= 0;
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
) );
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
);
182 azArg
[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
183 "rootpage int,sql text)";
187 initData
.rc
= SQLITE_OK
;
188 initData
.pzErrMsg
= pzErrMsg
;
189 initData
.mInitFlags
= mFlags
;
190 initData
.nInitRow
= 0;
191 sqlite3InitCallback(&initData
, 3, (char **)azArg
, 0);
197 /* Create a cursor to hold the database open
202 DbSetProperty(db
, 1, DB_SchemaLoaded
);
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);
214 sqlite3SetString(pzErrMsg
, db
, sqlite3ErrStr(rc
));
215 goto initone_error_out
;
217 openedTransaction
= 1;
220 /* Get the database meta information.
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
234 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
235 ** the possible values of meta[4].
237 for(i
=0; i
<ArraySize(meta
); i
++){
238 sqlite3BtreeGetMeta(pDb
->pBt
, i
+1, (u32
*)&meta
[i
]);
240 if( (db
->flags
& SQLITE_ResetDatabase
)!=0 ){
241 memset(meta
, 0, sizeof(meta
));
243 pDb
->pSchema
->schema_cookie
= meta
[BTREE_SCHEMA_VERSION
-1];
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
250 if( meta
[BTREE_TEXT_ENCODING
-1] ){ /* text encoding */
252 #ifndef SQLITE_OMIT_UTF16
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
;
259 ENC(db
) = SQLITE_UTF8
;
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");
267 goto initone_error_out
;
271 DbSetProperty(db
, iDb
, DB_Empty
);
273 pDb
->pSchema
->enc
= ENC(db
);
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
;
281 pDb
->pSchema
->cache_size
= SQLITE_DEFAULT_CACHE_SIZE
;
283 sqlite3BtreeSetCacheSize(pDb
->pBt
, pDb
->pSchema
->cache_size
);
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
292 pDb
->pSchema
->file_format
= (u8
)meta
[BTREE_FILE_FORMAT
-1];
293 if( pDb
->pSchema
->file_format
==0 ){
294 pDb
->pSchema
->file_format
= 1;
296 if( pDb
->pSchema
->file_format
>SQLITE_MAX_FILE_FORMAT
){
297 sqlite3SetString(pzErrMsg
, db
, "unsupported file format");
299 goto initone_error_out
;
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.
307 if( iDb
==0 && meta
[BTREE_FILE_FORMAT
-1]>=4 ){
308 db
->flags
&= ~(u64
)SQLITE_LegacyFileFmt
;
311 /* Read the schema information out of the schema tables
313 assert( db
->init
.busy
);
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
325 rc
= sqlite3_exec(db
, zSql
, sqlite3InitCallback
, &initData
, 0);
326 #ifndef SQLITE_OMIT_AUTHORIZATION
330 if( rc
==SQLITE_OK
) rc
= initData
.rc
;
331 sqlite3DbFree(db
, zSql
);
332 #ifndef SQLITE_OMIT_ANALYZE
334 sqlite3AnalysisLoad(db
, iDb
);
338 if( db
->mallocFailed
){
339 rc
= SQLITE_NOMEM_BKPT
;
340 sqlite3ResetAllSchemasOfConnection(db
);
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.
351 DbSetProperty(db
, iDb
, DB_SchemaLoaded
);
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.
360 if( openedTransaction
){
361 sqlite3BtreeCommit(pDb
->pBt
);
363 sqlite3BtreeLeave(pDb
->pBt
);
367 if( rc
==SQLITE_NOMEM
|| rc
==SQLITE_IOERR_NOMEM
){
370 sqlite3ResetOneSchema(db
, iDb
);
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.
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.
386 int sqlite3Init(sqlite3
*db
, char **pzErrMsg
){
388 int commit_internal
= !(db
->mDbFlags
&DBFLAG_SchemaChange
);
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
);
395 /* Do the main schema first */
396 if( !DbHasProperty(db
, 0, DB_SchemaLoaded
) ){
397 rc
= sqlite3InitOne(db
, 0, pzErrMsg
, 0);
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);
408 if( commit_internal
){
409 sqlite3CommitInternalChanges(db
);
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.
418 int sqlite3ReadSchema(Parse
*pParse
){
420 sqlite3
*db
= pParse
->db
;
421 assert( sqlite3_mutex_held(db
->mutex
) );
422 if( !db
->init
.busy
){
423 rc
= sqlite3Init(db
, &pParse
->zErrMsg
);
427 }else if( db
->noSharedCache
){
428 db
->mDbFlags
|= DBFLAG_SchemaKnownOk
;
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.
440 static void schemaIsValid(Parse
*pParse
){
441 sqlite3
*db
= pParse
->db
;
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;
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
){
461 if( rc
!=SQLITE_OK
) return;
462 openedTransaction
= 1;
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
;
475 /* Close the transaction, if one was opened. */
476 if( openedTransaction
){
477 sqlite3BtreeCommit(pBt
);
483 ** Convert a schema pointer into the iDb index that indicates
484 ** which database file in db->aDb[] the schema refers to.
486 ** If the same database is attached more than once, the first
487 ** attached database is returned.
489 int sqlite3SchemaToIndex(sqlite3
*db
, Schema
*pSchema
){
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.
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).
502 assert( sqlite3_mutex_held(db
->mutex
) );
506 if( db
->aDb
[i
].pSchema
==pSchema
){
510 assert( i
>=0 && i
<db
->nDb
);
516 ** Free all memory allocations in the pParse object
518 void sqlite3ParserReset(Parse
*pParse
){
519 sqlite3
*db
= pParse
->db
;
520 sqlite3DbFree(db
, pParse
->aLabel
);
521 sqlite3ExprListDelete(db
, pParse
->pConstExpr
);
523 assert( db
->lookaside
.bDisable
>= pParse
->disableLookaside
);
524 db
->lookaside
.bDisable
-= pParse
->disableLookaside
;
526 pParse
->disableLookaside
= 0;
530 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
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 */
541 char *zErrMsg
= 0; /* Error message */
542 int rc
= SQLITE_OK
; /* Result code */
543 int i
; /* Loop counter */
544 Parse sParse
; /* Parsing context */
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
) );
553 /* For a long-term use prepared statement avoid the use of
556 if( prepFlags
& SQLITE_PREPARE_PERSISTENT
){
557 sParse
.disableLookaside
++;
558 db
->lookaside
.bDisable
++;
560 sParse
.disableVtab
= (prepFlags
& SQLITE_PREPARE_NO_VTAB
)!=0;
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
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.
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
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.
585 for(i
=0; i
<db
->nDb
; i
++) {
586 Btree
*pBt
= db
->aDb
[i
].pBt
;
588 assert( sqlite3BtreeHoldsMutex(pBt
) );
589 rc
= sqlite3BtreeSchemaLocked(pBt
);
591 const char *zDb
= db
->aDb
[i
].zDbSName
;
592 sqlite3ErrorWithMsg(db
, rc
, "database schema is locked: %s", zDb
);
593 testcase( db
->flags
& SQLITE_ReadUncommit
);
599 sqlite3VtabUnlockList(db
);
602 if( nBytes
>=0 && (nBytes
==0 || zSql
[nBytes
-1]!=0) ){
604 int mxLen
= db
->aLimit
[SQLITE_LIMIT_SQL_LENGTH
];
605 testcase( nBytes
==mxLen
);
606 testcase( nBytes
==mxLen
+1 );
608 sqlite3ErrorWithMsg(db
, SQLITE_TOOBIG
, "statement too long");
609 rc
= sqlite3ApiExit(db
, SQLITE_TOOBIG
);
612 zSqlCopy
= sqlite3DbStrNDup(db
, zSql
, nBytes
);
614 sqlite3RunParser(&sParse
, zSqlCopy
, &zErrMsg
);
615 sParse
.zTail
= &zSql
[sParse
.zTail
-zSqlCopy
];
616 sqlite3DbFree(db
, zSqlCopy
);
618 sParse
.zTail
= &zSql
[nBytes
];
621 sqlite3RunParser(&sParse
, zSql
, &zErrMsg
);
623 assert( 0==sParse
.nQueryLoop
);
625 if( sParse
.rc
==SQLITE_DONE
) sParse
.rc
= SQLITE_OK
;
626 if( sParse
.checkSchema
){
627 schemaIsValid(&sParse
);
629 if( db
->mallocFailed
){
630 sParse
.rc
= SQLITE_NOMEM_BKPT
;
633 *pzTail
= sParse
.zTail
;
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"
644 if( sParse
.explain
==2 ){
645 sqlite3VdbeSetNumCols(sParse
.pVdbe
, 4);
649 sqlite3VdbeSetNumCols(sParse
.pVdbe
, 8);
653 for(i
=iFirst
; i
<mx
; i
++){
654 sqlite3VdbeSetColName(sParse
.pVdbe
, i
-iFirst
, COLNAME_NAME
,
655 azColName
[i
], SQLITE_STATIC
);
660 if( db
->init
.busy
==0 ){
661 sqlite3VdbeSetSql(sParse
.pVdbe
, zSql
, (int)(sParse
.zTail
-zSql
), prepFlags
);
663 if( sParse
.pVdbe
&& (rc
!=SQLITE_OK
|| db
->mallocFailed
) ){
664 sqlite3VdbeFinalize(sParse
.pVdbe
);
667 *ppStmt
= (sqlite3_stmt
*)sParse
.pVdbe
;
671 sqlite3ErrorWithMsg(db
, rc
, "%s", zErrMsg
);
672 sqlite3DbFree(db
, zErrMsg
);
674 sqlite3Error(db
, rc
);
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
);
686 sqlite3ParserReset(&sParse
);
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 */
701 #ifdef SQLITE_ENABLE_API_ARMOR
702 if( ppStmt
==0 ) return SQLITE_MISUSE_BKPT
;
705 if( !sqlite3SafetyCheckOk(db
)||zSql
==0 ){
706 return SQLITE_MISUSE_BKPT
;
708 sqlite3_mutex_enter(db
->mutex
);
709 sqlite3BtreeEnterAll(db
);
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
);
727 ** Rerun the compilation of a statement after a schema change.
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.
734 int sqlite3Reprepare(Vdbe
*p
){
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);
749 if( rc
==SQLITE_NOMEM
){
757 sqlite3VdbeSwap((Vdbe
*)pNew
, p
);
758 sqlite3TransferBindings(pNew
, (sqlite3_stmt
*)p
);
759 sqlite3VdbeResetStepResult((Vdbe
*)pNew
);
760 sqlite3VdbeFinalize((Vdbe
*)pNew
);
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
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 */
781 rc
= sqlite3LockAndPrepare(db
,zSql
,nBytes
,0,0,ppStmt
,pzTail
);
782 assert( rc
==SQLITE_OK
|| ppStmt
==0 || *ppStmt
==0 ); /* VERIFY: F13021 */
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 */
793 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
794 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
797 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
798 rc
= sqlite3LockAndPrepare(db
,zSql
,nBytes
,SQLITE_PREPARE_SAVESQL
,0,
800 assert( rc
==SQLITE_OK
|| ppStmt
==0 || *ppStmt
==0 );
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 */
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.
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
),
822 assert( rc
==SQLITE_OK
|| ppStmt
==0 || *ppStmt
==0 );
827 #ifndef SQLITE_OMIT_UTF16
829 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
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 */
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.
844 const char *zTail8
= 0;
847 #ifdef SQLITE_ENABLE_API_ARMOR
848 if( ppStmt
==0 ) return SQLITE_MISUSE_BKPT
;
851 if( !sqlite3SafetyCheckOk(db
)||zSql
==0 ){
852 return SQLITE_MISUSE_BKPT
;
856 const char *z
= (const char*)zSql
;
857 for(sz
=0; sz
<nBytes
&& (z
[sz
]!=0 || z
[sz
+1]!=0); sz
+= 2){}
860 sqlite3_mutex_enter(db
->mutex
);
861 zSql8
= sqlite3Utf16to8(db
, zSql
, nBytes
, SQLITE_UTF16NATIVE
);
863 rc
= sqlite3LockAndPrepare(db
, zSql8
, -1, prepFlags
, 0, ppStmt
, &zTail8
);
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.
872 int chars_parsed
= sqlite3Utf8CharLen(zSql8
, (int)(zTail8
-zSql8
));
873 *pzTail
= (u8
*)zSql
+ sqlite3Utf16ByteLen(zSql
, chars_parsed
);
875 sqlite3DbFree(db
, zSql8
);
876 rc
= sqlite3ApiExit(db
, rc
);
877 sqlite3_mutex_leave(db
->mutex
);
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
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 */
897 rc
= sqlite3Prepare16(db
,zSql
,nBytes
,0,ppStmt
,pzTail
);
898 assert( rc
==SQLITE_OK
|| ppStmt
==0 || *ppStmt
==0 ); /* VERIFY: F13021 */
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 */
909 rc
= sqlite3Prepare16(db
,zSql
,nBytes
,SQLITE_PREPARE_SAVESQL
,ppStmt
,pzTail
);
910 assert( rc
==SQLITE_OK
|| ppStmt
==0 || *ppStmt
==0 ); /* VERIFY: F13021 */
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 */
922 rc
= sqlite3Prepare16(db
,zSql
,nBytes
,
923 SQLITE_PREPARE_SAVESQL
|(prepFlags
&SQLITE_PREPARE_MASK
),
925 assert( rc
==SQLITE_OK
|| ppStmt
==0 || *ppStmt
==0 ); /* VERIFY: F13021 */
929 #endif /* SQLITE_OMIT_UTF16 */