Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
prepare.c1094 linesDownload Raw Back to src
1/*2** 2005 May 253**4** The author disclaims copyright to this source code.  In place of5** a legal notice, here is a blessing:6**7**    May you do good and not evil.8**    May you find forgiveness for yourself and forgive others.9**    May you share freely, never taking more than you give.10**11*************************************************************************12** This file contains the implementation of the sqlite3_prepare()13** interface, and routines that contribute to loading the database schema14** from disk.15*/16#include "sqliteInt.h"17 18/*19** Fill the InitData structure with an error message that indicates20** that the database is corrupt.21*/22static void corruptSchema(23  InitData *pData,     /* Initialization context */24  char **azObj,        /* Type and name of object being parsed */25  const char *zExtra   /* Error information */26){27  sqlite3 *db = pData->db;28  if( db->mallocFailed ){29    pData->rc = SQLITE_NOMEM_BKPT;30  }else if( pData->pzErrMsg[0]!=0 ){31    /* A error message has already been generated.  Do not overwrite it */32  }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){33    static const char *azAlterType[] = {34       "rename",35       "drop column",36       "add column",37       "drop constraint"38    };39    *pData->pzErrMsg = sqlite3MPrintf(db, 40        "error in %s %s after %s: %s", azObj[0], azObj[1], 41        azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1], 42        zExtra43    );44    pData->rc = SQLITE_ERROR;45  }else if( db->flags & SQLITE_WriteSchema ){46    pData->rc = SQLITE_CORRUPT_BKPT;47  }else{48    char *z;49    const char *zObj = azObj[1] ? azObj[1] : "?";50    z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);51    if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);52    *pData->pzErrMsg = z;53    pData->rc = SQLITE_CORRUPT_BKPT;54  }55}56 57/*58** Check to see if any sibling index (another index on the same table)59** of pIndex has the same root page number, and if it does, return true.60** This would indicate a corrupt schema.61*/62int sqlite3IndexHasDuplicateRootPage(Index *pIndex){63  Index *p;64  for(p=pIndex->pTable->pIndex; p; p=p->pNext){65    if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;66  }67  return 0;68}69 70/* forward declaration */71static int sqlite3Prepare(72  sqlite3 *db,              /* Database handle. */73  const char *zSql,         /* UTF-8 encoded SQL statement. */74  int nBytes,               /* Length of zSql in bytes. */75  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */76  Vdbe *pReprepare,         /* VM being reprepared */77  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */78  const char **pzTail       /* OUT: End of parsed string */79);80 81 82/*83** This is the callback routine for the code that initializes the84** database.  See sqlite3Init() below for additional information.85** This routine is also called from the OP_ParseSchema opcode of the VDBE.86**87** Each callback contains the following information:88**89**     argv[0] = type of object: "table", "index", "trigger", or "view".90**     argv[1] = name of thing being created91**     argv[2] = associated table if an index or trigger92**     argv[3] = root page number for table or index. 0 for trigger or view.93**     argv[4] = SQL text for the CREATE statement.94**95*/96int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){97  InitData *pData = (InitData*)pInit;98  sqlite3 *db = pData->db;99  int iDb = pData->iDb;100 101  assert( argc==5 );102  UNUSED_PARAMETER2(NotUsed, argc);103  assert( sqlite3_mutex_held(db->mutex) );104  db->mDbFlags |= DBFLAG_EncodingFixed;105  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */106  pData->nInitRow++;107  if( db->mallocFailed ){108    corruptSchema(pData, argv, 0);109    return 1;110  }111 112  assert( iDb>=0 && iDb<db->nDb );113  if( argv[3]==0 ){114    corruptSchema(pData, argv, 0);115  }else if( argv[4]116         && 'c'==sqlite3UpperToLower[(unsigned char)argv[4][0]]117         && 'r'==sqlite3UpperToLower[(unsigned char)argv[4][1]] ){118    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.119    ** But because db->init.busy is set to 1, no VDBE code is generated120    ** or executed.  All the parser does is build the internal data121    ** structures that describe the table, index, or view.122    **123    ** No other valid SQL statement, other than the variable CREATE statements,124    ** can begin with the letters "C" and "R".  Thus, it is not possible run125    ** any other kind of statement while parsing the schema, even a corrupt126    ** schema.127    */128    int rc;129    u8 saved_iDb = db->init.iDb;130    sqlite3_stmt *pStmt;131    TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */132 133    assert( db->init.busy );134    db->init.iDb = iDb;135    if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0136     || (db->init.newTnum>pData->mxPage && pData->mxPage>0)137    ){138      if( sqlite3Config.bExtraSchemaChecks ){139        corruptSchema(pData, argv, "invalid rootpage");140      }141    }142    db->init.orphanTrigger = 0;143    db->init.azInit = (const char**)argv;144    pStmt = 0;145    TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);146    rc = db->errCode;147    assert( (rc&0xFF)==(rcp&0xFF) );148    db->init.iDb = saved_iDb;149    /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */150    if( SQLITE_OK!=rc ){151      if( db->init.orphanTrigger ){152        assert( iDb==1 );153      }else{154        if( rc > pData->rc ) pData->rc = rc;155        if( rc==SQLITE_NOMEM ){156          sqlite3OomFault(db);157        }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){158          corruptSchema(pData, argv, sqlite3_errmsg(db));159        }160      }161    }162    db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */163    sqlite3_finalize(pStmt);164  }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){165    corruptSchema(pData, argv, 0);166  }else{167    /* If the SQL column is blank it means this is an index that168    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE169    ** constraint for a CREATE TABLE.  The index should have already170    ** been created when we processed the CREATE TABLE.  All we have171    ** to do here is record the root page number for that index.172    */173    Index *pIndex;174    pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);175    if( pIndex==0 ){176      corruptSchema(pData, argv, "orphan index");177    }else178    if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0179     || pIndex->tnum<2180     || pIndex->tnum>pData->mxPage181     || sqlite3IndexHasDuplicateRootPage(pIndex)182    ){183      if( sqlite3Config.bExtraSchemaChecks ){184        corruptSchema(pData, argv, "invalid rootpage");185      }186    }187  }188  return 0;189}190 191/*192** Attempt to read the database schema and initialize internal193** data structures for a single database file.  The index of the194** database file is given by iDb.  iDb==0 is used for the main195** database.  iDb==1 should never be used.  iDb>=2 is used for196** auxiliary databases.  Return one of the SQLITE_ error codes to197** indicate success or failure.198*/199int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){200  int rc;201  int i;202#ifndef SQLITE_OMIT_DEPRECATED203  int size;204#endif205  Db *pDb;206  char const *azArg[6];207  int meta[5];208  InitData initData;209  const char *zSchemaTabName;210  int openedTransaction = 0;211  int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);212 213  assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );214  assert( iDb>=0 && iDb<db->nDb );215  assert( db->aDb[iDb].pSchema );216  assert( sqlite3_mutex_held(db->mutex) );217  assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );218 219  db->init.busy = 1;220 221  /* Construct the in-memory representation schema tables (sqlite_schema or222  ** sqlite_temp_schema) by invoking the parser directly.  The appropriate223  ** table name will be inserted automatically by the parser so we can just224  ** use the abbreviation "x" here.  The parser will also automatically tag225  ** the schema table as read-only. */226  azArg[0] = "table";227  azArg[1] = zSchemaTabName = SCHEMA_TABLE(iDb);228  azArg[2] = azArg[1];229  azArg[3] = "1";230  azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"231                            "rootpage int,sql text)";232  azArg[5] = 0;233  initData.db = db;234  initData.iDb = iDb;235  initData.rc = SQLITE_OK;236  initData.pzErrMsg = pzErrMsg;237  initData.mInitFlags = mFlags;238  initData.nInitRow = 0;239  initData.mxPage = 0;240  sqlite3InitCallback(&initData, 5, (char **)azArg, 0);241  db->mDbFlags &= mask;242  if( initData.rc ){243    rc = initData.rc;244    goto error_out;245  }246 247  /* Create a cursor to hold the database open248  */249  pDb = &db->aDb[iDb];250  if( pDb->pBt==0 ){251    assert( iDb==1 );252    DbSetProperty(db, 1, DB_SchemaLoaded);253    rc = SQLITE_OK;254    goto error_out;255  }256 257  /* If there is not already a read-only (or read-write) transaction opened258  ** on the b-tree database, open one now. If a transaction is opened, it 259  ** will be closed before this function returns.  */260  sqlite3BtreeEnter(pDb->pBt);261  if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){262    rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);263    if( rc!=SQLITE_OK ){264      sqlite3SetString(pzErrMsg, db, sqlite3ErrStr(rc));265      goto initone_error_out;266    }267    openedTransaction = 1;268  }269 270  /* Get the database meta information.271  **272  ** Meta values are as follows:273  **    meta[0]   Schema cookie.  Changes with each schema change.274  **    meta[1]   File format of schema layer.275  **    meta[2]   Size of the page cache.276  **    meta[3]   Largest rootpage (auto/incr_vacuum mode)277  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE278  **    meta[5]   User version279  **    meta[6]   Incremental vacuum mode280  **    meta[7]   unused281  **    meta[8]   unused282  **    meta[9]   unused283  **284  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to285  ** the possible values of meta[4].286  */287  for(i=0; i<ArraySize(meta); i++){288    sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);289  }290  if( (db->flags & SQLITE_ResetDatabase)!=0 ){291    memset(meta, 0, sizeof(meta));292  }293  pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];294 295  /* If opening a non-empty database, check the text encoding. For the296  ** main database, set sqlite3.enc to the encoding of the main database.297  ** For an attached db, it is an error if the encoding is not the same298  ** as sqlite3.enc.299  */300  if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */301    if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){302      u8 encoding;303#ifndef SQLITE_OMIT_UTF16304      /* If opening the main database, set ENC(db). */305      encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;306      if( encoding==0 ) encoding = SQLITE_UTF8;307#else308      encoding = SQLITE_UTF8;309#endif310      sqlite3SetTextEncoding(db, encoding);311    }else{312      /* If opening an attached database, the encoding much match ENC(db) */313      if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){314        sqlite3SetString(pzErrMsg, db, "attached databases must use the same"315            " text encoding as main database");316        rc = SQLITE_ERROR;317        goto initone_error_out;318      }319    }320  }321  pDb->pSchema->enc = ENC(db);322 323  if( pDb->pSchema->cache_size==0 ){324#ifndef SQLITE_OMIT_DEPRECATED325    size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);326    if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }327    pDb->pSchema->cache_size = size;328#else329    pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;330#endif331    sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);332  }333 334  /*335  ** file_format==1    Version 3.0.0.336  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN337  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults338  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants339  */340  pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];341  if( pDb->pSchema->file_format==0 ){342    pDb->pSchema->file_format = 1;343  }344  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){345    sqlite3SetString(pzErrMsg, db, "unsupported file format");346    rc = SQLITE_ERROR;347    goto initone_error_out;348  }349 350  /* Ticket #2804:  When we open a database in the newer file format,351  ** clear the legacy_file_format pragma flag so that a VACUUM will352  ** not downgrade the database and thus invalidate any descending353  ** indices that the user might have created.354  */355  if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){356    db->flags &= ~(u64)SQLITE_LegacyFileFmt;357  }358 359  /* Read the schema information out of the schema tables360  */361  assert( db->init.busy );362  initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);363  {364    char *zSql;365    zSql = sqlite3MPrintf(db, 366        "SELECT*FROM\"%w\".%s ORDER BY rowid",367        db->aDb[iDb].zDbSName, zSchemaTabName);368#ifndef SQLITE_OMIT_AUTHORIZATION369    {370      sqlite3_xauth xAuth;371      xAuth = db->xAuth;372      db->xAuth = 0;373#endif374      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);375#ifndef SQLITE_OMIT_AUTHORIZATION376      db->xAuth = xAuth;377    }378#endif379    if( rc==SQLITE_OK ) rc = initData.rc;380    sqlite3DbFree(db, zSql);381#ifndef SQLITE_OMIT_ANALYZE382    if( rc==SQLITE_OK ){383      sqlite3AnalysisLoad(db, iDb);384    }385#endif386  }387  assert( pDb == &(db->aDb[iDb]) );388  if( db->mallocFailed ){389    rc = SQLITE_NOMEM_BKPT;390    sqlite3ResetAllSchemasOfConnection(db);391    pDb = &db->aDb[iDb];392  }else393  if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){394    /* Hack: If the SQLITE_NoSchemaError flag is set, then consider395    ** the schema loaded, even if errors (other than OOM) occurred. In396    ** this situation the current sqlite3_prepare() operation will fail,397    ** but the following one will attempt to compile the supplied statement398    ** against whatever subset of the schema was loaded before the error399    ** occurred.400    **401    ** The primary purpose of this is to allow access to the sqlite_schema402    ** table even when its contents have been corrupted.403    */404    DbSetProperty(db, iDb, DB_SchemaLoaded);405    rc = SQLITE_OK;406  }407 408  /* Jump here for an error that occurs after successfully allocating409  ** curMain and calling sqlite3BtreeEnter(). For an error that occurs410  ** before that point, jump to error_out.411  */412initone_error_out:413  if( openedTransaction ){414    sqlite3BtreeCommit(pDb->pBt);415  }416  sqlite3BtreeLeave(pDb->pBt);417 418error_out:419  if( rc ){420    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){421      sqlite3OomFault(db);422    }423    sqlite3ResetOneSchema(db, iDb);424  }425  db->init.busy = 0;426  return rc;427}428 429/*430** Initialize all database files - the main database file, the file431** used to store temporary tables, and any additional database files432** created using ATTACH statements.  Return a success code.  If an433** error occurs, write an error message into *pzErrMsg.434**435** After a database is initialized, the DB_SchemaLoaded bit is set436** bit is set in the flags field of the Db structure. 437*/438int sqlite3Init(sqlite3 *db, char **pzErrMsg){439  int i, rc;440  int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);441  442  assert( sqlite3_mutex_held(db->mutex) );443  assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );444  assert( db->init.busy==0 );445  ENC(db) = SCHEMA_ENC(db);446  assert( db->nDb>0 );447  /* Do the main schema first */448  if( !DbHasProperty(db, 0, DB_SchemaLoaded) ){449    rc = sqlite3InitOne(db, 0, pzErrMsg, 0);450    if( rc ) return rc;451  }452  /* All other schemas after the main schema. The "temp" schema must be last */453  for(i=db->nDb-1; i>0; i--){454    assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );455    if( !DbHasProperty(db, i, DB_SchemaLoaded) ){456      rc = sqlite3InitOne(db, i, pzErrMsg, 0);457      if( rc ) return rc;458    }459  }460  if( commit_internal ){461    sqlite3CommitInternalChanges(db);462  }463  return SQLITE_OK;464}465 466/*467** This routine is a no-op if the database schema is already initialized.468** Otherwise, the schema is loaded. An error code is returned.469*/470int sqlite3ReadSchema(Parse *pParse){471  int rc = SQLITE_OK;472  sqlite3 *db = pParse->db;473  assert( sqlite3_mutex_held(db->mutex) );474  if( !db->init.busy ){475    rc = sqlite3Init(db, &pParse->zErrMsg);476    if( rc!=SQLITE_OK ){477      pParse->rc = rc;478      pParse->nErr++;479    }else if( db->noSharedCache ){480      db->mDbFlags |= DBFLAG_SchemaKnownOk;481    }482  }483  return rc;484}485 486 487/*488** Check schema cookies in all databases.  If any cookie is out489** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies490** make no changes to pParse->rc.491*/492static void schemaIsValid(Parse *pParse){493  sqlite3 *db = pParse->db;494  int iDb;495  int rc;496  int cookie;497 498  assert( pParse->checkSchema );499  assert( sqlite3_mutex_held(db->mutex) );500  for(iDb=0; iDb<db->nDb; iDb++){501    int openedTransaction = 0;         /* True if a transaction is opened */502    Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */503    if( pBt==0 ) continue;504 505    /* If there is not already a read-only (or read-write) transaction opened506    ** on the b-tree database, open one now. If a transaction is opened, it 507    ** will be closed immediately after reading the meta-value. */508    if( sqlite3BtreeTxnState(pBt)==SQLITE_TXN_NONE ){509      rc = sqlite3BtreeBeginTrans(pBt, 0, 0);510      if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){511        sqlite3OomFault(db);512        pParse->rc = SQLITE_NOMEM;513      }514      if( rc!=SQLITE_OK ) return;515      openedTransaction = 1;516    }517 518    /* Read the schema cookie from the database. If it does not match the 519    ** value stored as part of the in-memory schema representation,520    ** set Parse.rc to SQLITE_SCHEMA. */521    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);522    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );523    if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){524      if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;525      sqlite3ResetOneSchema(db, iDb);526    }527 528    /* Close the transaction, if one was opened. */529    if( openedTransaction ){530      sqlite3BtreeCommit(pBt);531    }532  }533}534 535/*536** Convert a schema pointer into the iDb index that indicates537** which database file in db->aDb[] the schema refers to.538**539** If the same database is attached more than once, the first540** attached database is returned.541*/542int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){543  int i = -32768;544 545  /* If pSchema is NULL, then return -32768. This happens when code in 546  ** expr.c is trying to resolve a reference to a transient table (i.e. one547  ** created by a sub-select). In this case the return value of this 548  ** function should never be used.549  **550  ** We return -32768 instead of the more usual -1 simply because using551  ** -32768 as the incorrect index into db->aDb[] is much 552  ** more likely to cause a segfault than -1 (of course there are assert()553  ** statements too, but it never hurts to play the odds) and554  ** -32768 will still fit into a 16-bit signed integer.555  */556  assert( sqlite3_mutex_held(db->mutex) );557  if( pSchema ){558    for(i=0; 1; i++){559      assert( i<db->nDb );560      if( db->aDb[i].pSchema==pSchema ){561        break;562      }563    }564    assert( i>=0 && i<db->nDb );565  }566  return i;567}568 569/*570** Free all memory allocations in the pParse object571*/572void sqlite3ParseObjectReset(Parse *pParse){573  sqlite3 *db = pParse->db;574  assert( db!=0 );575  assert( db->pParse==pParse );576  assert( pParse->nested==0 );577#ifndef SQLITE_OMIT_SHARED_CACHE578  if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);579#endif580  while( pParse->pCleanup ){581    ParseCleanup *pCleanup = pParse->pCleanup;582    pParse->pCleanup = pCleanup->pNext;583    pCleanup->xCleanup(db, pCleanup->pPtr);584    sqlite3DbNNFreeNN(db, pCleanup);585  }586  if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);587  if( pParse->pConstExpr ){588    sqlite3ExprListDelete(db, pParse->pConstExpr);589  }590  assert( db->lookaside.bDisable >= pParse->disableLookaside );591  db->lookaside.bDisable -= pParse->disableLookaside;592  db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;593  assert( pParse->db->pParse==pParse );594  db->pParse = pParse->pOuterParse;595}596 597/*598** Add a new cleanup operation to a Parser.  The cleanup should happen when599** the parser object is destroyed.  But, beware: the cleanup might happen600** immediately.601**602** Use this mechanism for uncommon cleanups.  There is a higher setup603** cost for this mechanism (an extra malloc), so it should not be used604** for common cleanups that happen on most calls.  But for less605** common cleanups, we save a single NULL-pointer comparison in606** sqlite3ParseObjectReset(), which reduces the total CPU cycle count.607**608** If a memory allocation error occurs, then the cleanup happens immediately.609** When either SQLITE_DEBUG or SQLITE_COVERAGE_TEST are defined, the610** pParse->earlyCleanup flag is set in that case.  Calling code show verify611** that test cases exist for which this happens, to guard against possible612** use-after-free errors following an OOM.  The preferred way to do this is613** to immediately follow the call to this routine with:614**615**       testcase( pParse->earlyCleanup );616**617** This routine returns a copy of its pPtr input (the third parameter)618** except if an early cleanup occurs, in which case it returns NULL.  So619** another way to check for early cleanup is to check the return value.620** Or, stop using the pPtr parameter with this call and use only its621** return value thereafter.  Something like this:622**623**       pObj = sqlite3ParserAddCleanup(pParse, destructor, pObj);624*/625void *sqlite3ParserAddCleanup(626  Parse *pParse,                      /* Destroy when this Parser finishes */627  void (*xCleanup)(sqlite3*,void*),   /* The cleanup routine */628  void *pPtr                          /* Pointer to object to be cleaned up */629){630  ParseCleanup *pCleanup;631  if( sqlite3FaultSim(300) ){632    pCleanup = 0;633    sqlite3OomFault(pParse->db);634  }else{635    pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));636  }637  if( pCleanup ){638    pCleanup->pNext = pParse->pCleanup;639    pParse->pCleanup = pCleanup;640    pCleanup->pPtr = pPtr;641    pCleanup->xCleanup = xCleanup;642  }else{643    xCleanup(pParse->db, pPtr);644    pPtr = 0;645#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)646    pParse->earlyCleanup = 1;647#endif648  }649  return pPtr;650}651 652/*653** Turn bulk memory into a valid Parse object and link that Parse object654** into database connection db.655**656** Call sqlite3ParseObjectReset() to undo this operation.657**658** Caution:  Do not confuse this routine with sqlite3ParseObjectInit() which659** is generated by Lemon.660*/661void sqlite3ParseObjectInit(Parse *pParse, sqlite3 *db){662  memset(PARSE_HDR(pParse), 0, PARSE_HDR_SZ);663  memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);664  assert( db->pParse!=pParse );665  pParse->pOuterParse = db->pParse;666  db->pParse = pParse;667  pParse->db = db;668  if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");669}670 671/*672** Maximum number of times that we will try again to prepare a statement673** that returns SQLITE_ERROR_RETRY.674*/675#ifndef SQLITE_MAX_PREPARE_RETRY676# define SQLITE_MAX_PREPARE_RETRY 25677#endif678 679/*680** Compile the UTF-8 encoded SQL statement zSql into a statement handle.681*/682static int sqlite3Prepare(683  sqlite3 *db,              /* Database handle. */684  const char *zSql,         /* UTF-8 encoded SQL statement. */685  int nBytes,               /* Length of zSql in bytes. */686  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */687  Vdbe *pReprepare,         /* VM being reprepared */688  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */689  const char **pzTail       /* OUT: End of parsed string */690){691  int rc = SQLITE_OK;       /* Result code */692  int i;                    /* Loop counter */693  Parse sParse;             /* Parsing context */694 695  /* sqlite3ParseObjectInit(&sParse, db); // inlined for performance */696  memset(PARSE_HDR(&sParse), 0, PARSE_HDR_SZ);697  memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);698  sParse.pOuterParse = db->pParse;699  db->pParse = &sParse;700  sParse.db = db;701  if( pReprepare ){702    sParse.pReprepare = pReprepare;703    sParse.explain = sqlite3_stmt_isexplain((sqlite3_stmt*)pReprepare);704  }else{705    assert( sParse.pReprepare==0 );706  }707  assert( ppStmt && *ppStmt==0 );708  if( db->mallocFailed ){709    sqlite3ErrorMsg(&sParse, "out of memory");710    db->errCode = rc = SQLITE_NOMEM;711    goto end_prepare;712  }713  assert( sqlite3_mutex_held(db->mutex) );714 715  /* For a long-term use prepared statement avoid the use of716  ** lookaside memory.717  */718  if( prepFlags & SQLITE_PREPARE_PERSISTENT ){719    sParse.disableLookaside++;720    DisableLookaside;721  }722  sParse.prepFlags = prepFlags & 0xff;723 724  /* Check to verify that it is possible to get a read lock on all725  ** database schemas.  The inability to get a read lock indicates that726  ** some other database connection is holding a write-lock, which in727  ** turn means that the other connection has made uncommitted changes728  ** to the schema.729  **730  ** Were we to proceed and prepare the statement against the uncommitted731  ** schema changes and if those schema changes are subsequently rolled732  ** back and different changes are made in their place, then when this733  ** prepared statement goes to run the schema cookie would fail to detect734  ** the schema change.  Disaster would follow.735  **736  ** This thread is currently holding mutexes on all Btrees (because737  ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it738  ** is not possible for another thread to start a new schema change739  ** while this routine is running.  Hence, we do not need to hold 740  ** locks on the schema, we just need to make sure nobody else is 741  ** holding them.742  **743  ** Note that setting READ_UNCOMMITTED overrides most lock detection,744  ** but it does *not* override schema lock detection, so this all still745  ** works even if READ_UNCOMMITTED is set.746  */747  if( !db->noSharedCache ){748    for(i=0; i<db->nDb; i++) {749      Btree *pBt = db->aDb[i].pBt;750      if( pBt ){751        assert( sqlite3BtreeHoldsMutex(pBt) );752        rc = sqlite3BtreeSchemaLocked(pBt);753        if( rc ){754          const char *zDb = db->aDb[i].zDbSName;755          sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);756          testcase( db->flags & SQLITE_ReadUncommit );757          goto end_prepare;758        }759      }760    }761  }762 763#ifndef SQLITE_OMIT_VIRTUALTABLE764  if( db->pDisconnect ) sqlite3VtabUnlockList(db);765#endif766 767  if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){768    char *zSqlCopy;769    int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];770    testcase( nBytes==mxLen );771    testcase( nBytes==mxLen+1 );772    if( nBytes>mxLen ){773      sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");774      rc = sqlite3ApiExit(db, SQLITE_TOOBIG);775      goto end_prepare;776    }777    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);778    if( zSqlCopy ){779      sqlite3RunParser(&sParse, zSqlCopy);780      sParse.zTail = &zSql[sParse.zTail-zSqlCopy];781      sqlite3DbFree(db, zSqlCopy);782    }else{783      sParse.zTail = &zSql[nBytes];784    }785  }else{786    sqlite3RunParser(&sParse, zSql);787  }788  assert( 0==sParse.nQueryLoop );789 790  if( pzTail ){791    *pzTail = sParse.zTail;792  }793 794  if( db->init.busy==0 ){795    sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);796  }797  if( db->mallocFailed ){798    sParse.rc = SQLITE_NOMEM_BKPT;799    sParse.checkSchema = 0;800  }801  if( sParse.rc!=SQLITE_OK && sParse.rc!=SQLITE_DONE ){802    if( sParse.checkSchema && db->init.busy==0 ){803      schemaIsValid(&sParse);804    }805    if( sParse.pVdbe ){806      sqlite3VdbeFinalize(sParse.pVdbe);807    }808    assert( 0==(*ppStmt) );809    rc = sParse.rc;810    if( sParse.zErrMsg ){811      sqlite3ErrorWithMsg(db, rc, "%s", sParse.zErrMsg);812      sqlite3DbFree(db, sParse.zErrMsg);813    }else{814      sqlite3Error(db, rc);815    }816  }else{817    assert( sParse.zErrMsg==0 );818    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;819    rc = SQLITE_OK;820    sqlite3ErrorClear(db);821  }822 823 824  /* Delete any TriggerPrg structures allocated while parsing this statement. */825  while( sParse.pTriggerPrg ){826    TriggerPrg *pT = sParse.pTriggerPrg;827    sParse.pTriggerPrg = pT->pNext;828    sqlite3DbFree(db, pT);829  }830 831end_prepare:832 833  sqlite3ParseObjectReset(&sParse);834  return rc;835}836static int sqlite3LockAndPrepare(837  sqlite3 *db,              /* Database handle. */838  const char *zSql,         /* UTF-8 encoded SQL statement. */839  int nBytes,               /* Length of zSql in bytes. */840  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */841  Vdbe *pOld,               /* VM being reprepared */842  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */843  const char **pzTail       /* OUT: End of parsed string */844){845  int rc;846  int cnt = 0;847 848#ifdef SQLITE_ENABLE_API_ARMOR849  if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;850#endif851  *ppStmt = 0;852  if( !sqlite3SafetyCheckOk(db)||zSql==0 ){853    return SQLITE_MISUSE_BKPT;854  }855  sqlite3_mutex_enter(db->mutex);856  sqlite3BtreeEnterAll(db);857  do{858    /* Make multiple attempts to compile the SQL, until it either succeeds859    ** or encounters a permanent error.  A schema problem after one schema860    ** reset is considered a permanent error. */861    rc = sqlite3Prepare(db, zSql, nBytes, prepFlags, pOld, ppStmt, pzTail);862    assert( rc==SQLITE_OK || *ppStmt==0 );863    if( rc==SQLITE_OK || db->mallocFailed ) break;864    cnt++;865  }while( (rc==SQLITE_ERROR_RETRY && ALWAYS(cnt<=SQLITE_MAX_PREPARE_RETRY))866       || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt)==1) );867  sqlite3BtreeLeaveAll(db);868  assert( rc!=SQLITE_ERROR_RETRY );869  rc = sqlite3ApiExit(db, rc);870  assert( (rc&db->errMask)==rc );871  db->busyHandler.nBusy = 0;872  sqlite3_mutex_leave(db->mutex);873  assert( rc==SQLITE_OK || (*ppStmt)==0 );874  return rc;875}876 877 878/*879** Rerun the compilation of a statement after a schema change.880**881** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,882** if the statement cannot be recompiled because another connection has883** locked the sqlite3_schema table, return SQLITE_LOCKED. If any other error884** occurs, return SQLITE_SCHEMA.885*/886int sqlite3Reprepare(Vdbe *p){887  int rc;888  sqlite3_stmt *pNew;889  const char *zSql;890  sqlite3 *db;891  u8 prepFlags;892 893  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );894  zSql = sqlite3_sql((sqlite3_stmt *)p);895  assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */896  db = sqlite3VdbeDb(p);897  assert( sqlite3_mutex_held(db->mutex) );898  prepFlags = sqlite3VdbePrepareFlags(p);899  rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);900  if( rc ){901    if( rc==SQLITE_NOMEM ){902      sqlite3OomFault(db);903    }904    assert( pNew==0 );905    return rc;906  }else{907    assert( pNew!=0 );908  }909  sqlite3VdbeSwap((Vdbe*)pNew, p);910  sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);911  sqlite3VdbeResetStepResult((Vdbe*)pNew);912  sqlite3VdbeFinalize((Vdbe*)pNew);913  return SQLITE_OK;914}915 916 917/*918** Two versions of the official API.  Legacy and new use.  In the legacy919** version, the original SQL text is not saved in the prepared statement920** and so if a schema change occurs, SQLITE_SCHEMA is returned by921** sqlite3_step().  In the new version, the original SQL text is retained922** and the statement is automatically recompiled if an schema change923** occurs.924*/925int sqlite3_prepare(926  sqlite3 *db,              /* Database handle. */927  const char *zSql,         /* UTF-8 encoded SQL statement. */928  int nBytes,               /* Length of zSql in bytes. */929  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */930  const char **pzTail       /* OUT: End of parsed string */931){932  int rc;933  rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);934  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */935  return rc;936}937int sqlite3_prepare_v2(938  sqlite3 *db,              /* Database handle. */939  const char *zSql,         /* UTF-8 encoded SQL statement. */940  int nBytes,               /* Length of zSql in bytes. */941  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */942  const char **pzTail       /* OUT: End of parsed string */943){944  int rc;945  /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works946  ** exactly the same as sqlite3_prepare_v3() with a zero prepFlags947  ** parameter.948  **949  ** Proof in that the 5th parameter to sqlite3LockAndPrepare is 0 */950  rc = sqlite3LockAndPrepare(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,0,951                             ppStmt,pzTail);952  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );953  return rc;954}955int sqlite3_prepare_v3(956  sqlite3 *db,              /* Database handle. */957  const char *zSql,         /* UTF-8 encoded SQL statement. */958  int nBytes,               /* Length of zSql in bytes. */959  unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */960  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */961  const char **pzTail       /* OUT: End of parsed string */962){963  int rc;964  /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from965  ** sqlite3_prepare_v2() only in having the extra prepFlags parameter,966  ** which is a bit array consisting of zero or more of the967  ** SQLITE_PREPARE_* flags.968  **969  ** Proof by comparison to the implementation of sqlite3_prepare_v2()970  ** directly above. */971  rc = sqlite3LockAndPrepare(db,zSql,nBytes,972                 SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),973                 0,ppStmt,pzTail);974  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );975  return rc;976}977 978 979#ifndef SQLITE_OMIT_UTF16980/*981** Compile the UTF-16 encoded SQL statement zSql into a statement handle.982*/983static int sqlite3Prepare16(984  sqlite3 *db,              /* Database handle. */ 985  const void *zSql,         /* UTF-16 encoded SQL statement. */986  int nBytes,               /* Length of zSql in bytes. */987  u32 prepFlags,            /* Zero or more SQLITE_PREPARE_* flags */988  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */989  const void **pzTail       /* OUT: End of parsed string */990){991  /* This function currently works by first transforming the UTF-16992  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The993  ** tricky bit is figuring out the pointer to return in *pzTail.994  */995  char *zSql8;996  const char *zTail8 = 0;997  int rc = SQLITE_OK;998 999#ifdef SQLITE_ENABLE_API_ARMOR1000  if( ppStmt==0 ) return SQLITE_MISUSE_BKPT;1001#endif1002  *ppStmt = 0;1003  if( !sqlite3SafetyCheckOk(db)||zSql==0 ){1004    return SQLITE_MISUSE_BKPT;1005  }1006 1007  /* Make sure nBytes is non-negative and correct.  It should be the1008  ** number of bytes until the end of the input buffer or until the first1009  ** U+0000 character.  If the input nBytes is odd, convert it into1010  ** an even number.  If the input nBytes is negative, then the input1011  ** must be terminated by at least one U+0000 character */1012  if( nBytes>=0 ){1013    int sz;1014    const char *z = (const char*)zSql;1015    for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}1016    nBytes = sz;1017  }else{1018    int sz;1019    const char *z = (const char*)zSql;1020    for(sz=0; z[sz]!=0 || z[sz+1]!=0; sz += 2){}1021    nBytes = sz;1022  }1023 1024  sqlite3_mutex_enter(db->mutex);1025  zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);1026  if( zSql8 ){1027    rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);1028  }1029 1030  if( zTail8 && pzTail ){1031    /* If sqlite3_prepare returns a tail pointer, we calculate the1032    ** equivalent pointer into the UTF-16 string by counting the unicode1033    ** characters between zSql8 and zTail8, and then returning a pointer1034    ** the same number of characters into the UTF-16 string.1035    */1036    int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));1037    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, nBytes, chars_parsed);1038  }1039  sqlite3DbFree(db, zSql8); 1040  rc = sqlite3ApiExit(db, rc);1041  sqlite3_mutex_leave(db->mutex);1042  return rc;1043}1044 1045/*1046** Two versions of the official API.  Legacy and new use.  In the legacy1047** version, the original SQL text is not saved in the prepared statement1048** and so if a schema change occurs, SQLITE_SCHEMA is returned by1049** sqlite3_step().  In the new version, the original SQL text is retained1050** and the statement is automatically recompiled if an schema change1051** occurs.1052*/1053int sqlite3_prepare16(1054  sqlite3 *db,              /* Database handle. */ 1055  const void *zSql,         /* UTF-16 encoded SQL statement. */1056  int nBytes,               /* Length of zSql in bytes. */1057  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */1058  const void **pzTail       /* OUT: End of parsed string */1059){1060  int rc;1061  rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);1062  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */1063  return rc;1064}1065int sqlite3_prepare16_v2(1066  sqlite3 *db,              /* Database handle. */ 1067  const void *zSql,         /* UTF-16 encoded SQL statement. */1068  int nBytes,               /* Length of zSql in bytes. */1069  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */1070  const void **pzTail       /* OUT: End of parsed string */1071){1072  int rc;1073  rc = sqlite3Prepare16(db,zSql,nBytes,SQLITE_PREPARE_SAVESQL,ppStmt,pzTail);1074  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */1075  return rc;1076}1077int sqlite3_prepare16_v3(1078  sqlite3 *db,              /* Database handle. */ 1079  const void *zSql,         /* UTF-16 encoded SQL statement. */1080  int nBytes,               /* Length of zSql in bytes. */1081  unsigned int prepFlags,   /* Zero or more SQLITE_PREPARE_* flags */1082  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */1083  const void **pzTail       /* OUT: End of parsed string */1084){1085  int rc;1086  rc = sqlite3Prepare16(db,zSql,nBytes,1087         SQLITE_PREPARE_SAVESQL|(prepFlags&SQLITE_PREPARE_MASK),1088         ppStmt,pzTail);1089  assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */1090  return rc;1091}1092 1093#endif /* SQLITE_OMIT_UTF16 */1094