000001 /*
000002 ** 2005 May 25
000003 **
000004 ** The author disclaims copyright to this source code. In place of
000005 ** a legal notice, here is a blessing:
000006 **
000007 ** May you do good and not evil.
000008 ** May you find forgiveness for yourself and forgive others.
000009 ** May you share freely, never taking more than you give.
000010 **
000011 *************************************************************************
000012 ** This file contains the implementation of the sqlite3_prepare()
000013 ** interface, and routines that contribute to loading the database schema
000014 ** from disk.
000015 */
000016 #include "sqliteInt.h"
000017
000018 /*
000019 ** Fill the InitData structure with an error message that indicates
000020 ** that the database is corrupt.
000021 */
000022 static void corruptSchema(
000023 InitData *pData, /* Initialization context */
000024 char **azObj, /* Type and name of object being parsed */
000025 const char *zExtra /* Error information */
000026 ){
000027 sqlite3 *db = pData->db;
000028 if( db->mallocFailed ){
000029 pData->rc = SQLITE_NOMEM_BKPT;
000030 }else if( pData->pzErrMsg[0]!=0 ){
000031 /* A error message has already been generated. Do not overwrite it */
000032 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
000033 static const char *azAlterType[] = {
000034 "rename",
000035 "drop column",
000036 "add column"
000037 };
000038 *pData->pzErrMsg = sqlite3MPrintf(db,
000039 "error in %s %s after %s: %s", azObj[0], azObj[1],
000040 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
000041 zExtra
000042 );
000043 pData->rc = SQLITE_ERROR;
000044 }else if( db->flags & SQLITE_WriteSchema ){
000045 pData->rc = SQLITE_CORRUPT_BKPT;
000046 }else{
000047 char *z;
000048 const char *zObj = azObj[1] ? azObj[1] : "?";
000049 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
000050 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
000051 *pData->pzErrMsg = z;
000052 pData->rc = SQLITE_CORRUPT_BKPT;
000053 }
000054 }
000055
000056 /*
000057 ** Check to see if any sibling index (another index on the same table)
000058 ** of pIndex has the same root page number, and if it does, return true.
000059 ** This would indicate a corrupt schema.
000060 */
000061 int sqlite3IndexHasDuplicateRootPage(Index *pIndex){
000062 Index *p;
000063 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
000064 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
000065 }
000066 return 0;
000067 }
000068
000069 /* forward declaration */
000070 static int sqlite3Prepare(
000071 sqlite3 *db, /* Database handle. */
000072 const char *zSql, /* UTF-8 encoded SQL statement. */
000073 int nBytes, /* Length of zSql in bytes. */
000074 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
000075 Vdbe *pReprepare, /* VM being reprepared */
000076 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
000077 const char **pzTail /* OUT: End of parsed string */
000078 );
000079
000080
000081 /*
000082 ** This is the callback routine for the code that initializes the
000083 ** database. See sqlite3Init() below for additional information.
000084 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
000085 **
000086 ** Each callback contains the following information:
000087 **
000088 ** argv[0] = type of object: "table", "index", "trigger", or "view".
000089 ** argv[1] = name of thing being created
000090 ** argv[2] = associated table if an index or trigger
000091 ** argv[3] = root page number for table or index. 0 for trigger or view.
000092 ** argv[4] = SQL text for the CREATE statement.
000093 **
000094 */
000095 int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
000096 InitData *pData = (InitData*)pInit;
000097 sqlite3 *db = pData->db;
000098 int iDb = pData->iDb;
000099
000100 assert( argc==5 );
000101 UNUSED_PARAMETER2(NotUsed, argc);
000102 assert( sqlite3_mutex_held(db->mutex) );
000103 db->mDbFlags |= DBFLAG_EncodingFixed;
000104 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
000105 pData->nInitRow++;
000106 if( db->mallocFailed ){
000107 corruptSchema(pData, argv, 0);
000108 return 1;
000109 }
000110
000111 assert( iDb>=0 && iDb<db->nDb );
000112 if( argv[3]==0 ){
000113 corruptSchema(pData, argv, 0);
000114 }else if( argv[4]
000115 && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]
000116 && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){
000117 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
000118 ** But because db->init.busy is set to 1, no VDBE code is generated
000119 ** or executed. All the parser does is build the internal data
000120 ** structures that describe the table, index, or view.
000121 **
000122 ** No other valid SQL statement, other than the variable CREATE statements,
000123 ** can begin with the letters "C" and "R". Thus, it is not possible run
000124 ** any other kind of statement while parsing the schema, even a corrupt
000125 ** schema.
000126 */
000127 int rc;
000128 u8 saved_iDb = db->init.iDb;
000129 sqlite3_stmt *pStmt;
000130 TESTONLY(int rcp); /* Return code from sqlite3_prepare() */
000131
000132 assert( db->init.busy );
000133 db->init.iDb = iDb;
000134 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
000135 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
000136 ){
000137 if( sqlite3Config.bExtraSchemaChecks ){
000138 corruptSchema(pData, argv, "invalid rootpage");
000139 }
000140 }
000141 db->init.orphanTrigger = 0;
000142 db->init.azInit = (const char**)argv;
000143 pStmt = 0;
000144 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
000145 rc = db->errCode;
000146 assert( (rc&0xFF)==(rcp&0xFF) );
000147 db->init.iDb = saved_iDb;
000148 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
000149 if( SQLITE_OK!=rc ){
000150 if( db->init.orphanTrigger ){
000151 assert( iDb==1 );
000152 }else{
000153 if( rc > pData->rc ) pData->rc = rc;
000154 if( rc==SQLITE_NOMEM ){
000155 sqlite3OomFault(db);
000156 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
000157 corruptSchema(pData, argv, sqlite3_errmsg(db));
000158 }
000159 }
000160 }
000161 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
000162 sqlite3_finalize(pStmt);
000163 }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
000164 corruptSchema(pData, argv, 0);
000165 }else{
000166 /* If the SQL column is blank it means this is an index that
000167 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
000168 ** constraint for a CREATE TABLE. The index should have already
000169 ** been created when we processed the CREATE TABLE. All we have
000170 ** to do here is record the root page number for that index.
000171 */
000172 Index *pIndex;
000173 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
000174 if( pIndex==0 ){
000175 corruptSchema(pData, argv, "orphan index");
000176 }else
000177 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
000178 || pIndex->tnum<2
000179 || pIndex->tnum>pData->mxPage
000180 || sqlite3IndexHasDuplicateRootPage(pIndex)
000181 ){
000182 if( sqlite3Config.bExtraSchemaChecks ){
000183 corruptSchema(pData, argv, "invalid rootpage");
000184 }
000185 }
000186 }
000187 return 0;
000188 }
000189
000190 /*
000191 ** Attempt to read the database schema and initialize internal
000192 ** data structures for a single database file. The index of the
000193 ** database file is given by iDb. iDb==0 is used for the main
000194 ** database. iDb==1 should never be used. iDb>=2 is used for
000195 ** auxiliary databases. Return one of the SQLITE_ error codes to
000196 ** indicate success or failure.
000197 */
000198 int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
000199 int rc;
000200 int i;
000201 #ifndef SQLITE_OMIT_DEPRECATED
000202 int size;
000203 #endif
000204 Db *pDb;
000205 char const *azArg[6];
000206 int meta[5];
000207 InitData initData;
000208 const char *zSchemaTabName;
000209 int openedTransaction = 0;
000210 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
000211
000212 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
000213 assert( iDb>=0 && iDb<db->nDb );
000214 assert( db->aDb[iDb].pSchema );
000215 assert( sqlite3_mutex_held(db->mutex) );
000216 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
000217
000218 db->init.busy = 1;
000219
000220 /* Construct the in-memory representation schema tables (sqlite_schema or
000221 ** sqlite_temp_schema) by invoking the parser directly. The appropriate
000222 ** table name will be inserted automatically by the parser so we can just
000223 ** use the abbreviation "x" here. The parser will also automatically tag
000224 ** the schema table as read-only. */
000225 azArg[0] = "table";
000226 azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb);
000227 azArg[2] = azArg[1];
000228 azArg[3] = "1";
000229 azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
000230 "rootpage int,sql text)";
000231 azArg[5] = 0;
000232 initData.db = db;
000233 initData.iDb = iDb;
000234 initData.rc = SQLITE_OK;
000235 initData.pzErrMsg = pzErrMsg;
000236 initData.mInitFlags = mFlags;
000237 initData.nInitRow = 0;
000238 initData.mxPage = 0;
000239 sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
000240 db->mDbFlags &= mask;
000241 if( initData.rc ){
000242 rc = initData.rc;
000243 goto error_out;
000244 }
000245
000246 /* Create a cursor to hold the database open
000247 */
000248 pDb = &db->aDb[iDb];
000249 if( pDb->pBt==0 ){
000250 assert( iDb==1 );
000251 DbSetProperty(db, 1, DB_SchemaLoaded);
000252 rc = SQLITE_OK;
000253 goto error_out;
000254 }
000255
000256 /* If there is not already a read-only (or read-write) transaction opened
000257 ** on the b-tree database, open one now. If a transaction is opened, it
000258 ** will be closed before this function returns. */
000259 sqlite3BtreeEnter(pDb->pBt);
000260 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
000261 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
000262 if( rc!=SQLITE_OK ){
000263 sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));
000264 goto initone_error_out;
000265 }
000266 openedTransaction = 1;
000267 }
000268
000269 /* Get the database meta information.
000270 **
000271 ** Meta values are as follows:
000272 ** meta[0] Schema cookie. Changes with each schema change.
000273 ** meta[1] File format of schema layer.
000274 ** meta[2] Size of the page cache.
000275 ** meta[3] Largest rootpage (auto/incr_vacuum mode)
000276 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
000277 ** meta[5] User version
000278 ** meta[6] Incremental vacuum mode
000279 ** meta[7] unused
000280 ** meta[8] unused
000281 ** meta[9] unused
000282 **
000283 ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
000284 ** the possible values of meta[4].
000285 */
000286 for(i=0; i<ArraySize(meta); i++){
000287 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
000288 }
000289 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
000290 memset(meta, 0, sizeof(meta));
000291 }
000292 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
000293
000294 /* If opening a non-empty database, check the text encoding. For the
000295 ** main database, set sqlite3.enc to the encoding of the main database.
000296 ** For an attached db, it is an error if the encoding is not the same
000297 ** as sqlite3.enc.
000298 */
000299 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
000300 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
000301 u8 encoding;
000302 #ifndef SQLITE_OMIT_UTF16
000303 /* If opening the main database, set ENC(db). */
000304 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
000305 if( encoding==0 ) encoding = SQLITE_UTF8;
000306 #else
000307 encoding = SQLITE_UTF8;
000308 #endif
000309 sqlite3SetTextEncoding(db, encoding);
000310 }else{
000311 /* If opening an attached database, the encoding much match ENC(db) */
000312 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
000313 sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
000314 " text encoding as main database");
000315 rc = SQLITE_ERROR;
000316 goto initone_error_out;
000317 }
000318 }
000319 }
000320 pDb->pSchema->enc = ENC(db);
000321
000322 if( pDb->pSchema->cache_size==0 ){
000323 #ifndef SQLITE_OMIT_DEPRECATED
000324 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
000325 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
000326 pDb->pSchema->cache_size = size;
000327 #else
000328 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
000329 #endif
000330 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
000331 }
000332
000333 /*
000334 ** file_format==1 Version 3.0.0.
000335 ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
000336 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
000337 ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
000338 */
000339 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
000340 if( pDb->pSchema->file_format==0 ){
000341 pDb->pSchema->file_format = 1;
000342 }
000343 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
000344 sqlite3SetString(pzErrMsg, db, "unsupported file format");
000345 rc = SQLITE_ERROR;
000346 goto initone_error_out;
000347 }
000348
000349 /* Ticket #2804: When we open a database in the newer file format,
000350 ** clear the legacy_file_format pragma flag so that a VACUUM will
000351 ** not downgrade the database and thus invalidate any descending
000352 ** indices that the user might have created.
000353 */
000354 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
000355 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
000356 }
000357
000358 /* Read the schema information out of the schema tables
000359 */
000360 assert( db->init.busy );
000361 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
000362 {
000363 char *zSql;
000364 zSql = sqlite3MPrintf(db,
000365 "SELECT*FROM\"%w\".%s ORDER BY rowid",
000366 db->aDb[iDb].zDbSName, zSchemaTabName);
000367 #ifndef SQLITE_OMIT_AUTHORIZATION
000368 {
000369 sqlite3_xauth xAuth;
000370 xAuth = db->xAuth;
000371 db->xAuth = 0;
000372 #endif
000373 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
000374 #ifndef SQLITE_OMIT_AUTHORIZATION
000375 db->xAuth = xAuth;
000376 }
000377 #endif
000378 if( rc==SQLITE_OK ) rc = initData.rc;
000379 sqlite3DbFree(db, zSql);
000380 #ifndef SQLITE_OMIT_ANALYZE
000381 if( rc==SQLITE_OK ){
000382 sqlite3AnalysisLoad(db, iDb);
000383 }
000384 #endif
000385 }
000386 assert( pDb == &(db->aDb[iDb]) );
000387 if( db->mallocFailed ){
000388 rc = SQLITE_NOMEM_BKPT;
000389 sqlite3ResetAllSchemasOfConnection(db);
000390 pDb = &db->aDb[iDb];
000391 }else
000392 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
000393 /* Hack: If the SQLITE_NoSchemaError flag is set, then consider
000394 ** the schema loaded, even if errors (other than OOM) occurred. In
000395 ** this situation the current sqlite3_prepare() operation will fail,
000396 ** but the following one will attempt to compile the supplied statement
000397 ** against whatever subset of the schema was loaded before the error
000398 ** occurred.
000399 **
000400 ** The primary purpose of this is to allow access to the sqlite_schema
000401 ** table even when its contents have been corrupted.
000402 */
000403 DbSetProperty(db, iDb, DB_SchemaLoaded);
000404 rc = SQLITE_OK;
000405 }
000406
000407 /* Jump here for an error that occurs after successfully allocating
000408 ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
000409 ** before that point, jump to error_out.
000410 */
000411 initone_error_out:
000412 if( openedTransaction ){
000413 sqlite3BtreeCommit(pDb->pBt);
000414 }
000415 sqlite3BtreeLeave(pDb->pBt);
000416
000417 error_out:
000418 if( rc ){
000419 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
000420 sqlite3OomFault(db);
000421 }
000422 sqlite3ResetOneSchema(db, iDb);
000423 }
000424 db->init.busy = 0;
000425 return rc;
000426 }
000427
000428 /*
000429 ** Initialize all database files - the main database file, the file
000430 ** used to store temporary tables, and any additional database files
000431 ** created using ATTACH statements. Return a success code. If an
000432 ** error occurs, write an error message into *pzErrMsg.
000433 **
000434 ** After a database is initialized, the DB_SchemaLoaded bit is set
000435 ** bit is set in the flags field of the Db structure.
000436 */
000437 int sqlite3Init(sqlite3 *db, char **pzErrMsg){
000438 int i, rc;
000439 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
000440
000441 assert( sqlite3_mutex_held(db->mutex) );
000442 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
000443 assert( db->init.busy==0 );
000444 ENC(db) = SCHEMA_ENC(db);
000445 assert( db->nDb>0 );
000446 /* Do the main schema first */
000447 if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){
000448 rc = sqlite3InitOne(db, 0, pzErrMsg, 0);
000449 if( rc ) return rc;
000450 }
000451 /* All other schemas after the main schema. The "temp" schema must be last */
000452 for(i=db->nDb-1; i>0; i--){
000453 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
000454 if( !DbHasProperty(db, i, DB_SchemaLoaded) ){
000455 rc = sqlite3InitOne(db, i, pzErrMsg, 0);
000456 if( rc ) return rc;
000457 }
000458 }
000459 if( commit_internal ){
000460 sqlite3CommitInternalChanges(db);
000461 }
000462 return SQLITE_OK;
000463 }
000464
000465 /*
000466 ** This routine is a no-op if the database schema is already initialized.
000467 ** Otherwise, the schema is loaded. An error code is returned.
000468 */
000469 int sqlite3ReadSchema(Parse *pParse){
000470 int rc = SQLITE_OK;
000471 sqlite3 *db = pParse->db;
000472 assert( sqlite3_mutex_held(db->mutex) );
000473 if( !db->init.busy ){
000474 rc = sqlite3Init(db, &pParse->zErrMsg);
000475 if( rc!=SQLITE_OK ){
000476 pParse->rc = rc;
000477 pParse->nErr++;
000478 }else if( db->noSharedCache ){
000479 db->mDbFlags |= DBFLAG_SchemaKnownOk;
000480 }
000481 }
000482 return rc;
000483 }
000484
000485
000486 /*
000487 ** Check schema cookies in all databases. If any cookie is out
000488 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
000489 ** make no changes to pParse->rc.
000490 */
000491 static void schemaIsValid(Parse *pParse){
000492 sqlite3 *db = pParse->db;
000493 int iDb;
000494 int rc;
000495 int cookie;
000496
000497 assert( pParse->checkSchema );
000498 assert( sqlite3_mutex_held(db->mutex) );
000499 for(iDb=0; iDb<db->nDb; iDb++){
000500 int openedTransaction = 0; /* True if a transaction is opened */
000501 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
000502 if( pBt==0 ) continue;
000503
000504 /* If there is not already a read-only (or read-write) transaction opened
000505 ** on the b-tree database, open one now. If a transaction is opened, it
000506 ** will be closed immediately after reading the meta-value. */
000507 if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE ){
000508 rc = sqlite3BtreeBeginTrans(pBt, 0, 0);
000509 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
000510 sqlite3OomFault(db);
000511 pParse->rc = SQLITE_NOMEM;
000512 }
000513 if( rc!=SQLITE_OK ) return;
000514 openedTransaction = 1;
000515 }
000516
000517 /* Read the schema cookie from the database. If it does not match the
000518 ** value stored as part of the in-memory schema representation,
000519 ** set Parse.rc to SQLITE_SCHEMA. */
000520 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
000521 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
000522 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
000523 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
000524 sqlite3ResetOneSchema(db, iDb);
000525 }
000526
000527 /* Close the transaction, if one was opened. */
000528 if( openedTransaction ){
000529 sqlite3BtreeCommit(pBt);
000530 }
000531 }
000532 }
000533
000534 /*
000535 ** Convert a schema pointer into the iDb index that indicates
000536 ** which database file in db->aDb[] the schema refers to.
000537 **
000538 ** If the same database is attached more than once, the first
000539 ** attached database is returned.
000540 */
000541 int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
000542 int i = -32768;
000543
000544 /* If pSchema is NULL, then return -32768. This happens when code in
000545 ** expr.c is trying to resolve a reference to a transient table (i.e. one
000546 ** created by a sub-select). In this case the return value of this
000547 ** function should never be used.
000548 **
000549 ** We return -32768 instead of the more usual -1 simply because using
000550 ** -32768 as the incorrect index into db->aDb[] is much
000551 ** more likely to cause a segfault than -1 (of course there are assert()
000552 ** statements too, but it never hurts to play the odds) and
000553 ** -32768 will still fit into a 16-bit signed integer.
000554 */
000555 assert( sqlite3_mutex_held(db->mutex) );
000556 if( pSchema ){
000557 for(i=0; 1; i++){
000558 assert( i<db->nDb );
000559 if( db->aDb[i].pSchema==pSchema ){
000560 break;
000561 }
000562 }
000563 assert( i>=0 && i<db->nDb );
000564 }
000565 return i;
000566 }
000567
000568 /*
000569 ** Free all memory allocations in the pParse object
000570 */
000571 void sqlite3ParseObjectReset(Parse *pParse){
000572 sqlite3 *db = pParse->db;
000573 assert( db!=0 );
000574 assert( db->pParse==pParse );
000575 assert( pParse->nested==0 );
000576 #ifndef SQLITE_OMIT_SHARED_CACHE
000577 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
000578 #endif
000579 while( pParse->pCleanup ){
000580 ParseCleanup *pCleanup = pParse->pCleanup;
000581 pParse->pCleanup = pCleanup->pNext;
000582 pCleanup->xCleanup(db, pCleanup->pPtr);
000583 sqlite3DbNNFreeNN(db, pCleanup);
000584 }
000585 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
000586 if( pParse->pConstExpr ){
000587 sqlite3ExprListDelete(db, pParse->pConstExpr);
000588 }
000589 assert( db->lookaside.bDisable >= pParse->disableLookaside );
000590 db->lookaside.bDisable -= pParse->disableLookaside;
000591 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
000592 assert( pParse->db->pParse==pParse );
000593 db->pParse = pParse->pOuterParse;
000594 }
000595
000596 /*
000597 ** Add a new cleanup operation to a Parser. The cleanup should happen when
000598 ** the parser object is destroyed. But, beware: the cleanup might happen
000599 ** immediately.
000600 **
000601 ** Use this mechanism for uncommon cleanups. There is a higher setup
000602 ** cost for this mechanism (an extra malloc), so it should not be used
000603 ** for common cleanups that happen on most calls. But for less
000604 ** common cleanups, we save a single NULL-pointer comparison in
000605 ** sqlite3ParseObjectReset(), which reduces the total CPU cycle count.
000606 **
000607 ** If a memory allocation error occurs, then the cleanup happens immediately.
000608 ** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the
000609 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
000610 ** that test cases exist for which this happens, to guard against possible
000611 ** use-after-free errors following an OOM. The preferred way to do this is
000612 ** to immediately follow the call to this routine with:
000613 **
000614 ** testcase( pParse->earlyCleanup );
000615 **
000616 ** This routine returns a copy of its pPtr input (the third parameter)
000617 ** except if an early cleanup occurs, in which case it returns NULL. So
000618 ** another way to check for early cleanup is to check the return value.
000619 ** Or, stop using the pPtr parameter with this call and use only its
000620 ** return value thereafter. Something like this:
000621 **
000622 ** pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);
000623 */
000624 void *sqlite3ParserAddCleanup(
000625 Parse *pParse, /* Destroy when this Parser finishes */
000626 void (*xCleanup)(sqlite3*,void*), /* The cleanup routine */
000627 void *pPtr /* Pointer to object to be cleaned up */
000628 ){
000629 ParseCleanup *pCleanup;
000630 if( sqlite3FaultSim(300) ){
000631 pCleanup = 0;
000632 sqlite3OomFault(pParse->db);
000633 }else{
000634 pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
000635 }
000636 if( pCleanup ){
000637 pCleanup->pNext = pParse->pCleanup;
000638 pParse->pCleanup = pCleanup;
000639 pCleanup->pPtr = pPtr;
000640 pCleanup->xCleanup = xCleanup;
000641 }else{
000642 xCleanup(pParse->db, pPtr);
000643 pPtr = 0;
000644 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
000645 pParse->earlyCleanup = 1;
000646 #endif
000647 }
000648 return pPtr;
000649 }
000650
000651 /*
000652 ** Turn bulk memory into a valid Parse object and link that Parse object
000653 ** into database connection db.
000654 **
000655 ** Call sqlite3ParseObjectReset() to undo this operation.
000656 **
000657 ** Caution: Do not confuse this routine with sqlite3ParseObjectInit() which
000658 ** is generated by Lemon.
000659 */
000660 void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){
000661 memset(PARSE_HDR(pParse), 0, PARSE_HDR_SZ);
000662 memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
000663 assert( db->pParse!=pParse );
000664 pParse->pOuterParse = db->pParse;
000665 db->pParse = pParse;
000666 pParse->db = db;
000667 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
000668 }
000669
000670 /*
000671 ** Maximum number of times that we will try again to prepare a statement
000672 ** that returns SQLITE_ERROR_RETRY.
000673 */
000674 #ifndef SQLITE_MAX_PREPARE_RETRY
000675 # define SQLITE_MAX_PREPARE_RETRY 25
000676 #endif
000677
000678 /*
000679 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
000680 */
000681 static int sqlite3Prepare(
000682 sqlite3 *db, /* Database handle. */
000683 const char *zSql, /* UTF-8 encoded SQL statement. */
000684 int nBytes, /* Length of zSql in bytes. */
000685 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
000686 Vdbe *pReprepare, /* VM being reprepared */
000687 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
000688 const char **pzTail /* OUT: End of parsed string */
000689 ){
000690 int rc = SQLITE_OK; /* Result code */
000691 int i; /* Loop counter */
000692 Parse sParse; /* Parsing context */
000693
000694 /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */
000695 memset(PARSE_HDR(&sParse), 0, PARSE_HDR_SZ);
000696 memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
000697 sParse.pOuterParse = db->pParse;
000698 db->pParse = &sParse;
000699 sParse.db = db;
000700 if( pReprepare ){
000701 sParse.pReprepare = pReprepare;
000702 sParse.explain = sqlite3_stmt_isexplain((sqlite3_stmt*)pReprepare);
000703 }else{
000704 assert( sParse.pReprepare==0 );
000705 }
000706 assert( ppStmt && *ppStmt==0 );
000707 if( db->mallocFailed ){
000708 sqlite3ErrorMsg(&sParse, "out of memory");
000709 db->errCode = rc = SQLITE_NOMEM;
000710 goto end_prepare;
000711 }
000712 assert( sqlite3_mutex_held(db->mutex) );
000713
000714 /* For a long-term use prepared statement avoid the use of
000715 ** lookaside memory.
000716 */
000717 if( prepFlags & SQLITE_PREPARE_PERSISTENT ){
000718 sParse.disableLookaside++;
000719 DisableLookaside;
000720 }
000721 sParse.prepFlags = prepFlags & 0xff;
000722
000723 /* Check to verify that it is possible to get a read lock on all
000724 ** database schemas. The inability to get a read lock indicates that
000725 ** some other database connection is holding a write-lock, which in
000726 ** turn means that the other connection has made uncommitted changes
000727 ** to the schema.
000728 **
000729 ** Were we to proceed and prepare the statement against the uncommitted
000730 ** schema changes and if those schema changes are subsequently rolled
000731 ** back and different changes are made in their place, then when this
000732 ** prepared statement goes to run the schema cookie would fail to detect
000733 ** the schema change. Disaster would follow.
000734 **
000735 ** This thread is currently holding mutexes on all Btrees (because
000736 ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
000737 ** is not possible for another thread to start a new schema change
000738 ** while this routine is running. Hence, we do not need to hold
000739 ** locks on the schema, we just need to make sure nobody else is
000740 ** holding them.
000741 **
000742 ** Note that setting READ_UNCOMMITTED overrides most lock detection,
000743 ** but it does *not* override schema lock detection, so this all still
000744 ** works even if READ_UNCOMMITTED is set.
000745 */
000746 if( !db->noSharedCache ){
000747 for(i=0; i<db->nDb; i++) {
000748 Btree *pBt = db->aDb[i].pBt;
000749 if( pBt ){
000750 assert( sqlite3BtreeHoldsMutex(pBt) );
000751 rc = sqlite3BtreeSchemaLocked(pBt);
000752 if( rc ){
000753 const char *zDb = db->aDb[i].zDbSName;
000754 sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
000755 testcase( db->flags & SQLITE_ReadUncommit );
000756 goto end_prepare;
000757 }
000758 }
000759 }
000760 }
000761
000762 #ifndef SQLITE_OMIT_VIRTUALTABLE
000763 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
000764 #endif
000765
000766 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
000767 char *zSqlCopy;
000768 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
000769 testcase( nBytes==mxLen );
000770 testcase( nBytes==mxLen+1 );
000771 if( nBytes>mxLen ){
000772 sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
000773 rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
000774 goto end_prepare;
000775 }
000776 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
000777 if( zSqlCopy ){
000778 sqlite3RunParser(&sParse, zSqlCopy);
000779 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
000780 sqlite3DbFree(db, zSqlCopy);
000781 }else{
000782 sParse.zTail = &zSql[nBytes];
000783 }
000784 }else{
000785 sqlite3RunParser(&sParse, zSql);
000786 }
000787 assert( 0==sParse.nQueryLoop );
000788
000789 if( pzTail ){
000790 *pzTail = sParse.zTail;
000791 }
000792
000793 if( db->init.busy==0 ){
000794 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
000795 }
000796 if( db->mallocFailed ){
000797 sParse.rc = SQLITE_NOMEM_BKPT;
000798 sParse.checkSchema = 0;
000799 }
000800 if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){
000801 if( sParse.checkSchema && db->init.busy==0 ){
000802 schemaIsValid(&sParse);
000803 }
000804 if( sParse.pVdbe ){
000805 sqlite3VdbeFinalize(sParse.pVdbe);
000806 }
000807 assert( 0==(*ppStmt) );
000808 rc = sParse.rc;
000809 if( sParse.zErrMsg ){
000810 sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);
000811 sqlite3DbFree(db, sParse.zErrMsg);
000812 }else{
000813 sqlite3Error(db, rc);
000814 }
000815 }else{
000816 assert( sParse.zErrMsg==0 );
000817 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
000818 rc = SQLITE_OK;
000819 sqlite3ErrorClear(db);
000820 }
000821
000822
000823 /* Delete any TriggerPrg structures allocated while parsing this statement. */
000824 while( sParse.pTriggerPrg ){
000825 TriggerPrg *pT = sParse.pTriggerPrg;
000826 sParse.pTriggerPrg = pT->pNext;
000827 sqlite3DbFree(db, pT);
000828 }
000829
000830 end_prepare:
000831
000832 sqlite3ParseObjectReset(&sParse);
000833 return rc;
000834 }
000835 static int sqlite3LockAndPrepare(
000836 sqlite3 *db, /* Database handle. */
000837 const char *zSql, /* UTF-8 encoded SQL statement. */
000838 int nBytes, /* Length of zSql in bytes. */
000839 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
000840 Vdbe *pOld, /* VM being reprepared */
000841 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
000842 const char **pzTail /* OUT: End of parsed string */
000843 ){
000844 int rc;
000845 int cnt = 0;
000846
000847 #ifdef SQLITE_ENABLE_API_ARMOR
000848 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
000849 #endif
000850 *ppStmt = 0;
000851 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
000852 return SQLITE_MISUSE_BKPT;
000853 }
000854 sqlite3_mutex_enter(db->mutex);
000855 sqlite3BtreeEnterAll(db);
000856 do{
000857 /* Make multiple attempts to compile the SQL, until it either succeeds
000858 ** or encounters a permanent error. A schema problem after one schema
000859 ** reset is considered a permanent error. */
000860 rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);
000861 assert( rc==SQLITE_OK || *ppStmt==0 );
000862 if( rc==SQLITE_OK || db->mallocFailed ) break;
000863 }while( (rc==SQLITE_ERROR_RETRY && (cnt++)<SQLITE_MAX_PREPARE_RETRY)
000864 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
000865 sqlite3BtreeLeaveAll(db);
000866 rc = sqlite3ApiExit(db, rc);
000867 assert( (rc&db->errMask)==rc );
000868 db->busyHandler.nBusy = 0;
000869 sqlite3_mutex_leave(db->mutex);
000870 assert( rc==SQLITE_OK || (*ppStmt)==0 );
000871 return rc;
000872 }
000873
000874
000875 /*
000876 ** Rerun the compilation of a statement after a schema change.
000877 **
000878 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
000879 ** if the statement cannot be recompiled because another connection has
000880 ** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error
000881 ** occurs, return SQLITE_SCHEMA.
000882 */
000883 int sqlite3Reprepare(Vdbe *p){
000884 int rc;
000885 sqlite3_stmt *pNew;
000886 const char *zSql;
000887 sqlite3 *db;
000888 u8 prepFlags;
000889
000890 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
000891 zSql = sqlite3_sql((sqlite3_stmt *)p);
000892 assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */
000893 db = sqlite3VdbeDb(p);
000894 assert( sqlite3_mutex_held(db->mutex) );
000895 prepFlags = sqlite3VdbePrepareFlags(p);
000896 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
000897 if( rc ){
000898 if( rc==SQLITE_NOMEM ){
000899 sqlite3OomFault(db);
000900 }
000901 assert( pNew==0 );
000902 return rc;
000903 }else{
000904 assert( pNew!=0 );
000905 }
000906 sqlite3VdbeSwap((Vdbe*)pNew, p);
000907 sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
000908 sqlite3VdbeResetStepResult((Vdbe*)pNew);
000909 sqlite3VdbeFinalize((Vdbe*)pNew);
000910 return SQLITE_OK;
000911 }
000912
000913
000914 /*
000915 ** Two versions of the official API. Legacy and new use. In the legacy
000916 ** version, the original SQL text is not saved in the prepared statement
000917 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
000918 ** sqlite3_step(). In the new version, the original SQL text is retained
000919 ** and the statement is automatically recompiled if an schema change
000920 ** occurs.
000921 */
000922 int sqlite3_prepare(
000923 sqlite3 *db, /* Database handle. */
000924 const char *zSql, /* UTF-8 encoded SQL statement. */
000925 int nBytes, /* Length of zSql in bytes. */
000926 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
000927 const char **pzTail /* OUT: End of parsed string */
000928 ){
000929 int rc;
000930 rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
000931 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
000932 return rc;
000933 }
000934 int sqlite3_prepare_v2(
000935 sqlite3 *db, /* Database handle. */
000936 const char *zSql, /* UTF-8 encoded SQL statement. */
000937 int nBytes, /* Length of zSql in bytes. */
000938 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
000939 const char **pzTail /* OUT: End of parsed string */
000940 ){
000941 int rc;
000942 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
000943 ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags
000944 ** parameter.
000945 **
000946 ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */
000947 rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,
000948 ppStmt,pzTail);
000949 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
000950 return rc;
000951 }
000952 int sqlite3_prepare_v3(
000953 sqlite3 *db, /* Database handle. */
000954 const char *zSql, /* UTF-8 encoded SQL statement. */
000955 int nBytes, /* Length of zSql in bytes. */
000956 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
000957 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
000958 const char **pzTail /* OUT: End of parsed string */
000959 ){
000960 int rc;
000961 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
000962 ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,
000963 ** which is a bit array consisting of zero or more of the
000964 ** SQLITE_PREPARE_* flags.
000965 **
000966 ** Proof by comparison to the implementation of sqlite3_prepare_v2()
000967 ** directly above. */
000968 rc = sqlite3LockAndPrepare(db,zSql,nBytes,
000969 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
000970 0,ppStmt,pzTail);
000971 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );
000972 return rc;
000973 }
000974
000975
000976 #ifndef SQLITE_OMIT_UTF16
000977 /*
000978 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
000979 */
000980 static int sqlite3Prepare16(
000981 sqlite3 *db, /* Database handle. */
000982 const void *zSql, /* UTF-16 encoded SQL statement. */
000983 int nBytes, /* Length of zSql in bytes. */
000984 u32 prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
000985 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
000986 const void **pzTail /* OUT: End of parsed string */
000987 ){
000988 /* This function currently works by first transforming the UTF-16
000989 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
000990 ** tricky bit is figuring out the pointer to return in *pzTail.
000991 */
000992 char *zSql8;
000993 const char *zTail8 = 0;
000994 int rc = SQLITE_OK;
000995
000996 #ifdef SQLITE_ENABLE_API_ARMOR
000997 if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;
000998 #endif
000999 *ppStmt = 0;
001000 if( !sqlite3SafetyCheckOk(db)||zSql==0 ){
001001 return SQLITE_MISUSE_BKPT;
001002 }
001003
001004 /* Make sure nBytes is non-negative and correct. It should be the
001005 ** number of bytes until the end of the input buffer or until the first
001006 ** U+0000 character. If the input nBytes is odd, convert it into
001007 ** an even number. If the input nBytes is negative, then the input
001008 ** must be terminated by at least one U+0000 character */
001009 if( nBytes>=0 ){
001010 int sz;
001011 const char *z = (const char*)zSql;
001012 for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
001013 nBytes = sz;
001014 }else{
001015 int sz;
001016 const char *z = (const char*)zSql;
001017 for(sz=0; z[sz]!=0 || z[sz+1]!=0; sz += 2){}
001018 nBytes = sz;
001019 }
001020
001021 sqlite3_mutex_enter(db->mutex);
001022 zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
001023 if( zSql8 ){
001024 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
001025 }
001026
001027 if( zTail8 && pzTail ){
001028 /* If sqlite3_prepare returns a tail pointer, we calculate the
001029 ** equivalent pointer into the UTF-16 string by counting the unicode
001030 ** characters between zSql8 and zTail8, and then returning a pointer
001031 ** the same number of characters into the UTF-16 string.
001032 */
001033 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
001034 *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, nBytes, chars_parsed);
001035 }
001036 sqlite3DbFree(db, zSql8);
001037 rc = sqlite3ApiExit(db, rc);
001038 sqlite3_mutex_leave(db->mutex);
001039 return rc;
001040 }
001041
001042 /*
001043 ** Two versions of the official API. Legacy and new use. In the legacy
001044 ** version, the original SQL text is not saved in the prepared statement
001045 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
001046 ** sqlite3_step(). In the new version, the original SQL text is retained
001047 ** and the statement is automatically recompiled if an schema change
001048 ** occurs.
001049 */
001050 int sqlite3_prepare16(
001051 sqlite3 *db, /* Database handle. */
001052 const void *zSql, /* UTF-16 encoded SQL statement. */
001053 int nBytes, /* Length of zSql in bytes. */
001054 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
001055 const void **pzTail /* OUT: End of parsed string */
001056 ){
001057 int rc;
001058 rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
001059 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
001060 return rc;
001061 }
001062 int sqlite3_prepare16_v2(
001063 sqlite3 *db, /* Database handle. */
001064 const void *zSql, /* UTF-16 encoded SQL statement. */
001065 int nBytes, /* Length of zSql in bytes. */
001066 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
001067 const void **pzTail /* OUT: End of parsed string */
001068 ){
001069 int rc;
001070 rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);
001071 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
001072 return rc;
001073 }
001074 int sqlite3_prepare16_v3(
001075 sqlite3 *db, /* Database handle. */
001076 const void *zSql, /* UTF-16 encoded SQL statement. */
001077 int nBytes, /* Length of zSql in bytes. */
001078 unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_* flags */
001079 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
001080 const void **pzTail /* OUT: End of parsed string */
001081 ){
001082 int rc;
001083 rc = sqlite3Prepare16(db,zSql,nBytes,
001084 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),
001085 ppStmt,pzTail);
001086 assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */
001087 return rc;
001088 }
001089
001090 #endif /* SQLITE_OMIT_UTF16 */