ds@1952: /* ds@1952: ** 2005 May 25 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains the implementation of the sqlite3_prepare() ds@1952: ** interface, and routines that contribute to loading the database schema ds@1952: ** from disk. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: #include ds@1952: ds@1952: /* ds@1952: ** Fill the InitData structure with an error message that indicates ds@1952: ** that the database is corrupt. ds@1952: */ ds@1952: static void corruptSchema(InitData *pData, const char *zExtra){ ds@1952: if( !pData->db->mallocFailed ){ ds@1952: sqlite3SetString(pData->pzErrMsg, "malformed database schema", ds@1952: zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); ds@1952: } ds@1952: pData->rc = SQLITE_CORRUPT; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This is the callback routine for the code that initializes the ds@1952: ** database. See sqlite3Init() below for additional information. ds@1952: ** This routine is also called from the OP_ParseSchema opcode of the VDBE. ds@1952: ** ds@1952: ** Each callback contains the following information: ds@1952: ** ds@1952: ** argv[0] = name of thing being created ds@1952: ** argv[1] = root page number for table or index. 0 for trigger or view. ds@1952: ** argv[2] = SQL text for the CREATE statement. ds@1952: ** ds@1952: */ ds@1952: int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ ds@1952: InitData *pData = (InitData*)pInit; ds@1952: sqlite3 *db = pData->db; ds@1952: int iDb = pData->iDb; ds@1952: ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: pData->rc = SQLITE_OK; ds@1952: DbClearProperty(db, iDb, DB_Empty); ds@1952: if( db->mallocFailed ){ ds@1952: corruptSchema(pData, 0); ds@1952: return SQLITE_NOMEM; ds@1952: } ds@1952: ds@1952: assert( argc==3 ); ds@1952: if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ ds@1952: if( argv[1]==0 ){ ds@1952: corruptSchema(pData, 0); ds@1952: return 1; ds@1952: } ds@1952: assert( iDb>=0 && iDbnDb ); ds@1952: if( argv[2] && argv[2][0] ){ ds@1952: /* Call the parser to process a CREATE TABLE, INDEX or VIEW. ds@1952: ** But because db->init.busy is set to 1, no VDBE code is generated ds@1952: ** or executed. All the parser does is build the internal data ds@1952: ** structures that describe the table, index, or view. ds@1952: */ ds@1952: char *zErr; ds@1952: int rc; ds@1952: assert( db->init.busy ); ds@1952: db->init.iDb = iDb; ds@1952: db->init.newTnum = atoi(argv[1]); ds@1952: rc = sqlite3_exec(db, argv[2], 0, 0, &zErr); ds@1952: db->init.iDb = 0; ds@1952: assert( rc!=SQLITE_OK || zErr==0 ); ds@1952: if( SQLITE_OK!=rc ){ ds@1952: pData->rc = rc; ds@1952: if( rc==SQLITE_NOMEM ){ ds@1952: db->mallocFailed = 1; ds@1952: }else if( rc!=SQLITE_INTERRUPT ){ ds@1952: corruptSchema(pData, zErr); ds@1952: } ds@1952: sqlite3_free(zErr); ds@1952: return 1; ds@1952: } ds@1952: }else{ ds@1952: /* If the SQL column is blank it means this is an index that ds@1952: ** was created to be the PRIMARY KEY or to fulfill a UNIQUE ds@1952: ** constraint for a CREATE TABLE. The index should have already ds@1952: ** been created when we processed the CREATE TABLE. All we have ds@1952: ** to do here is record the root page number for that index. ds@1952: */ ds@1952: Index *pIndex; ds@1952: pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); ds@1952: if( pIndex==0 || pIndex->tnum!=0 ){ ds@1952: /* This can occur if there exists an index on a TEMP table which ds@1952: ** has the same name as another index on a permanent index. Since ds@1952: ** the permanent table is hidden by the TEMP table, we can also ds@1952: ** safely ignore the index on the permanent table. ds@1952: */ ds@1952: /* Do Nothing */; ds@1952: }else{ ds@1952: pIndex->tnum = atoi(argv[1]); ds@1952: } ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Attempt to read the database schema and initialize internal ds@1952: ** data structures for a single database file. The index of the ds@1952: ** database file is given by iDb. iDb==0 is used for the main ds@1952: ** database. iDb==1 should never be used. iDb>=2 is used for ds@1952: ** auxiliary databases. Return one of the SQLITE_ error codes to ds@1952: ** indicate success or failure. ds@1952: */ ds@1952: static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ ds@1952: int rc; ds@1952: BtCursor *curMain; ds@1952: int size; ds@1952: Table *pTab; ds@1952: Db *pDb; ds@1952: char const *azArg[4]; ds@1952: int meta[10]; ds@1952: InitData initData; ds@1952: char const *zMasterSchema; ds@1952: char const *zMasterName = SCHEMA_TABLE(iDb); ds@1952: ds@1952: /* ds@1952: ** The master database table has a structure like this ds@1952: */ ds@1952: static const char master_schema[] = ds@1952: "CREATE TABLE sqlite_master(\n" ds@1952: " type text,\n" ds@1952: " name text,\n" ds@1952: " tbl_name text,\n" ds@1952: " rootpage integer,\n" ds@1952: " sql text\n" ds@1952: ")" ds@1952: ; ds@1952: #ifndef SQLITE_OMIT_TEMPDB ds@1952: static const char temp_master_schema[] = ds@1952: "CREATE TEMP TABLE sqlite_temp_master(\n" ds@1952: " type text,\n" ds@1952: " name text,\n" ds@1952: " tbl_name text,\n" ds@1952: " rootpage integer,\n" ds@1952: " sql text\n" ds@1952: ")" ds@1952: ; ds@1952: #else ds@1952: #define temp_master_schema 0 ds@1952: #endif ds@1952: ds@1952: assert( iDb>=0 && iDbnDb ); ds@1952: assert( db->aDb[iDb].pSchema ); ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: ds@1952: /* zMasterSchema and zInitScript are set to point at the master schema ds@1952: ** and initialisation script appropriate for the database being ds@1952: ** initialised. zMasterName is the name of the master table. ds@1952: */ ds@1952: if( !OMIT_TEMPDB && iDb==1 ){ ds@1952: zMasterSchema = temp_master_schema; ds@1952: }else{ ds@1952: zMasterSchema = master_schema; ds@1952: } ds@1952: zMasterName = SCHEMA_TABLE(iDb); ds@1952: ds@1952: /* Construct the schema tables. */ ds@1952: sqlite3SafetyOff(db); ds@1952: azArg[0] = zMasterName; ds@1952: azArg[1] = "1"; ds@1952: azArg[2] = zMasterSchema; ds@1952: azArg[3] = 0; ds@1952: initData.db = db; ds@1952: initData.iDb = iDb; ds@1952: initData.pzErrMsg = pzErrMsg; ds@1952: rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0); ds@1952: if( rc ){ ds@1952: sqlite3SafetyOn(db); ds@1952: rc = initData.rc; ds@1952: goto error_out; ds@1952: } ds@1952: pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); ds@1952: if( pTab ){ ds@1952: pTab->readOnly = 1; ds@1952: } ds@1952: sqlite3SafetyOn(db); ds@1952: ds@1952: /* Create a cursor to hold the database open ds@1952: */ ds@1952: pDb = &db->aDb[iDb]; ds@1952: if( pDb->pBt==0 ){ ds@1952: if( !OMIT_TEMPDB && iDb==1 ){ ds@1952: DbSetProperty(db, 1, DB_SchemaLoaded); ds@1952: } ds@1952: return SQLITE_OK; ds@1952: } ds@1952: sqlite3BtreeEnter(pDb->pBt); ds@1952: rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain); ds@1952: if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ ds@1952: sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); ds@1952: sqlite3BtreeLeave(pDb->pBt); ds@1952: goto error_out; ds@1952: } ds@1952: ds@1952: /* Get the database meta information. ds@1952: ** ds@1952: ** Meta values are as follows: ds@1952: ** meta[0] Schema cookie. Changes with each schema change. ds@1952: ** meta[1] File format of schema layer. ds@1952: ** meta[2] Size of the page cache. ds@1952: ** meta[3] Use freelist if 0. Autovacuum if greater than zero. ds@1952: ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE ds@1952: ** meta[5] The user cookie. Used by the application. ds@1952: ** meta[6] Incremental-vacuum flag. ds@1952: ** meta[7] ds@1952: ** meta[8] ds@1952: ** meta[9] ds@1952: ** ds@1952: ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to ds@1952: ** the possible values of meta[4]. ds@1952: */ ds@1952: if( rc==SQLITE_OK ){ ds@1952: int i; ds@1952: for(i=0; rc==SQLITE_OK && ipBt, i+1, (u32 *)&meta[i]); ds@1952: } ds@1952: if( rc ){ ds@1952: sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); ds@1952: sqlite3BtreeCloseCursor(curMain); ds@1952: sqlite3BtreeLeave(pDb->pBt); ds@1952: goto error_out; ds@1952: } ds@1952: }else{ ds@1952: memset(meta, 0, sizeof(meta)); ds@1952: } ds@1952: pDb->pSchema->schema_cookie = meta[0]; ds@1952: ds@1952: /* If opening a non-empty database, check the text encoding. For the ds@1952: ** main database, set sqlite3.enc to the encoding of the main database. ds@1952: ** For an attached db, it is an error if the encoding is not the same ds@1952: ** as sqlite3.enc. ds@1952: */ ds@1952: if( meta[4] ){ /* text encoding */ ds@1952: if( iDb==0 ){ ds@1952: /* If opening the main database, set ENC(db). */ ds@1952: ENC(db) = (u8)meta[4]; ds@1952: db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0); ds@1952: }else{ ds@1952: /* If opening an attached database, the encoding much match ENC(db) */ ds@1952: if( meta[4]!=ENC(db) ){ ds@1952: sqlite3BtreeCloseCursor(curMain); ds@1952: sqlite3SetString(pzErrMsg, "attached databases must use the same" ds@1952: " text encoding as main database", (char*)0); ds@1952: sqlite3BtreeLeave(pDb->pBt); ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: } ds@1952: }else{ ds@1952: DbSetProperty(db, iDb, DB_Empty); ds@1952: } ds@1952: pDb->pSchema->enc = ENC(db); ds@1952: ds@1952: size = meta[2]; ds@1952: if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } ds@1952: pDb->pSchema->cache_size = size; ds@1952: sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); ds@1952: ds@1952: /* ds@1952: ** file_format==1 Version 3.0.0. ds@1952: ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN ds@1952: ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults ds@1952: ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants ds@1952: */ ds@1952: pDb->pSchema->file_format = meta[1]; ds@1952: if( pDb->pSchema->file_format==0 ){ ds@1952: pDb->pSchema->file_format = 1; ds@1952: } ds@1952: if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ ds@1952: sqlite3BtreeCloseCursor(curMain); ds@1952: sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0); ds@1952: sqlite3BtreeLeave(pDb->pBt); ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: ds@1952: ds@1952: /* Read the schema information out of the schema tables ds@1952: */ ds@1952: assert( db->init.busy ); ds@1952: if( rc==SQLITE_EMPTY ){ ds@1952: /* For an empty database, there is nothing to read */ ds@1952: rc = SQLITE_OK; ds@1952: }else{ ds@1952: char *zSql; ds@1952: zSql = sqlite3MPrintf(db, ds@1952: "SELECT name, rootpage, sql FROM '%q'.%s", ds@1952: db->aDb[iDb].zName, zMasterName); ds@1952: sqlite3SafetyOff(db); ds@1952: rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); ds@1952: if( rc==SQLITE_ABORT ) rc = initData.rc; ds@1952: sqlite3SafetyOn(db); ds@1952: sqlite3_free(zSql); ds@1952: #ifndef SQLITE_OMIT_ANALYZE ds@1952: if( rc==SQLITE_OK ){ ds@1952: sqlite3AnalysisLoad(db, iDb); ds@1952: } ds@1952: #endif ds@1952: sqlite3BtreeCloseCursor(curMain); ds@1952: } ds@1952: if( db->mallocFailed ){ ds@1952: /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */ ds@1952: rc = SQLITE_NOMEM; ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: } ds@1952: if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ ds@1952: /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider ds@1952: ** the schema loaded, even if errors occured. In this situation the ds@1952: ** current sqlite3_prepare() operation will fail, but the following one ds@1952: ** will attempt to compile the supplied statement against whatever subset ds@1952: ** of the schema was loaded before the error occured. The primary ds@1952: ** purpose of this is to allow access to the sqlite_master table ds@1952: ** even when it's contents have been corrupted. ds@1952: */ ds@1952: DbSetProperty(db, iDb, DB_SchemaLoaded); ds@1952: rc = SQLITE_OK; ds@1952: } ds@1952: sqlite3BtreeLeave(pDb->pBt); ds@1952: ds@1952: error_out: ds@1952: if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Initialize all database files - the main database file, the file ds@1952: ** used to store temporary tables, and any additional database files ds@1952: ** created using ATTACH statements. Return a success code. If an ds@1952: ** error occurs, write an error message into *pzErrMsg. ds@1952: ** ds@1952: ** After a database is initialized, the DB_SchemaLoaded bit is set ds@1952: ** bit is set in the flags field of the Db structure. If the database ds@1952: ** file was of zero-length, then the DB_Empty flag is also set. ds@1952: */ ds@1952: int sqlite3Init(sqlite3 *db, char **pzErrMsg){ ds@1952: int i, rc; ds@1952: int commit_internal = !(db->flags&SQLITE_InternChanges); ds@1952: ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: if( db->init.busy ) return SQLITE_OK; ds@1952: rc = SQLITE_OK; ds@1952: db->init.busy = 1; ds@1952: for(i=0; rc==SQLITE_OK && inDb; i++){ ds@1952: if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; ds@1952: rc = sqlite3InitOne(db, i, pzErrMsg); ds@1952: if( rc ){ ds@1952: sqlite3ResetInternalSchema(db, i); ds@1952: } ds@1952: } ds@1952: ds@1952: /* Once all the other databases have been initialised, load the schema ds@1952: ** for the TEMP database. This is loaded last, as the TEMP database ds@1952: ** schema may contain references to objects in other databases. ds@1952: */ ds@1952: #ifndef SQLITE_OMIT_TEMPDB ds@1952: if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ ds@1952: rc = sqlite3InitOne(db, 1, pzErrMsg); ds@1952: if( rc ){ ds@1952: sqlite3ResetInternalSchema(db, 1); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: db->init.busy = 0; ds@1952: if( rc==SQLITE_OK && commit_internal ){ ds@1952: sqlite3CommitInternalChanges(db); ds@1952: } ds@1952: ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This routine is a no-op if the database schema is already initialised. ds@1952: ** Otherwise, the schema is loaded. An error code is returned. ds@1952: */ ds@1952: int sqlite3ReadSchema(Parse *pParse){ ds@1952: int rc = SQLITE_OK; ds@1952: sqlite3 *db = pParse->db; ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: if( !db->init.busy ){ ds@1952: rc = sqlite3Init(db, &pParse->zErrMsg); ds@1952: } ds@1952: if( rc!=SQLITE_OK ){ ds@1952: pParse->rc = rc; ds@1952: pParse->nErr++; ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Check schema cookies in all databases. If any cookie is out ds@1952: ** of date, return 0. If all schema cookies are current, return 1. ds@1952: */ ds@1952: static int schemaIsValid(sqlite3 *db){ ds@1952: int iDb; ds@1952: int rc; ds@1952: BtCursor *curTemp; ds@1952: int cookie; ds@1952: int allOk = 1; ds@1952: ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: for(iDb=0; allOk && iDbnDb; iDb++){ ds@1952: Btree *pBt; ds@1952: pBt = db->aDb[iDb].pBt; ds@1952: if( pBt==0 ) continue; ds@1952: rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp); ds@1952: if( rc==SQLITE_OK ){ ds@1952: rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie); ds@1952: if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){ ds@1952: allOk = 0; ds@1952: } ds@1952: sqlite3BtreeCloseCursor(curTemp); ds@1952: } ds@1952: if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ ds@1952: db->mallocFailed = 1; ds@1952: } ds@1952: } ds@1952: return allOk; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Convert a schema pointer into the iDb index that indicates ds@1952: ** which database file in db->aDb[] the schema refers to. ds@1952: ** ds@1952: ** If the same database is attached more than once, the first ds@1952: ** attached database is returned. ds@1952: */ ds@1952: int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ ds@1952: int i = -1000000; ds@1952: ds@1952: /* If pSchema is NULL, then return -1000000. This happens when code in ds@1952: ** expr.c is trying to resolve a reference to a transient table (i.e. one ds@1952: ** created by a sub-select). In this case the return value of this ds@1952: ** function should never be used. ds@1952: ** ds@1952: ** We return -1000000 instead of the more usual -1 simply because using ds@1952: ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much ds@1952: ** more likely to cause a segfault than -1 (of course there are assert() ds@1952: ** statements too, but it never hurts to play the odds). ds@1952: */ ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: if( pSchema ){ ds@1952: for(i=0; inDb; i++){ ds@1952: if( db->aDb[i].pSchema==pSchema ){ ds@1952: break; ds@1952: } ds@1952: } ds@1952: assert( i>=0 &&i>=0 && inDb ); ds@1952: } ds@1952: return i; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. ds@1952: */ ds@1952: int sqlite3Prepare( ds@1952: sqlite3 *db, /* Database handle. */ ds@1952: const char *zSql, /* UTF-8 encoded SQL statement. */ ds@1952: int nBytes, /* Length of zSql in bytes. */ ds@1952: int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ ds@1952: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ ds@1952: const char **pzTail /* OUT: End of parsed string */ ds@1952: ){ ds@1952: Parse sParse; ds@1952: char *zErrMsg = 0; ds@1952: int rc = SQLITE_OK; ds@1952: int i; ds@1952: ds@1952: assert( ppStmt ); ds@1952: *ppStmt = 0; ds@1952: if( sqlite3SafetyOn(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: assert( !db->mallocFailed ); ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: ds@1952: /* If any attached database schemas are locked, do not proceed with ds@1952: ** compilation. Instead return SQLITE_LOCKED immediately. ds@1952: */ ds@1952: for(i=0; inDb; i++) { ds@1952: Btree *pBt = db->aDb[i].pBt; ds@1952: if( pBt ){ ds@1952: int rc; ds@1952: rc = sqlite3BtreeSchemaLocked(pBt); ds@1952: if( rc ){ ds@1952: const char *zDb = db->aDb[i].zName; ds@1952: sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb); ds@1952: sqlite3SafetyOff(db); ds@1952: return SQLITE_LOCKED; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: memset(&sParse, 0, sizeof(sParse)); ds@1952: sParse.db = db; ds@1952: if( nBytes>=0 && zSql[nBytes]!=0 ){ ds@1952: char *zSqlCopy; ds@1952: if( nBytes>SQLITE_MAX_SQL_LENGTH ){ ds@1952: return SQLITE_TOOBIG; ds@1952: } ds@1952: zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); ds@1952: if( zSqlCopy ){ ds@1952: sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg); ds@1952: sqlite3_free(zSqlCopy); ds@1952: } ds@1952: sParse.zTail = &zSql[nBytes]; ds@1952: }else{ ds@1952: sqlite3RunParser(&sParse, zSql, &zErrMsg); ds@1952: } ds@1952: ds@1952: if( db->mallocFailed ){ ds@1952: sParse.rc = SQLITE_NOMEM; ds@1952: } ds@1952: if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; ds@1952: if( sParse.checkSchema && !schemaIsValid(db) ){ ds@1952: sParse.rc = SQLITE_SCHEMA; ds@1952: } ds@1952: if( sParse.rc==SQLITE_SCHEMA ){ ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: } ds@1952: if( db->mallocFailed ){ ds@1952: sParse.rc = SQLITE_NOMEM; ds@1952: } ds@1952: if( pzTail ){ ds@1952: *pzTail = sParse.zTail; ds@1952: } ds@1952: rc = sParse.rc; ds@1952: ds@1952: #ifndef SQLITE_OMIT_EXPLAIN ds@1952: if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ ds@1952: if( sParse.explain==2 ){ ds@1952: sqlite3VdbeSetNumCols(sParse.pVdbe, 3); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC); ds@1952: }else{ ds@1952: sqlite3VdbeSetNumCols(sParse.pVdbe, 5); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC); ds@1952: sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC); ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: if( sqlite3SafetyOff(db) ){ ds@1952: rc = SQLITE_MISUSE; ds@1952: } ds@1952: ds@1952: if( saveSqlFlag ){ ds@1952: sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql); ds@1952: } ds@1952: if( rc!=SQLITE_OK || db->mallocFailed ){ ds@1952: sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); ds@1952: assert(!(*ppStmt)); ds@1952: }else{ ds@1952: *ppStmt = (sqlite3_stmt*)sParse.pVdbe; ds@1952: } ds@1952: ds@1952: if( zErrMsg ){ ds@1952: sqlite3Error(db, rc, "%s", zErrMsg); ds@1952: sqlite3_free(zErrMsg); ds@1952: }else{ ds@1952: sqlite3Error(db, rc, 0); ds@1952: } ds@1952: ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: /* sqlite3ReleaseThreadData(); */ ds@1952: assert( (rc&db->errMask)==rc ); ds@1952: return rc; ds@1952: } ds@1952: static int sqlite3LockAndPrepare( ds@1952: sqlite3 *db, /* Database handle. */ ds@1952: const char *zSql, /* UTF-8 encoded SQL statement. */ ds@1952: int nBytes, /* Length of zSql in bytes. */ ds@1952: int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ ds@1952: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ ds@1952: const char **pzTail /* OUT: End of parsed string */ ds@1952: ){ ds@1952: int rc; ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: sqlite3BtreeEnterAll(db); ds@1952: rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail); ds@1952: sqlite3BtreeLeaveAll(db); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Rerun the compilation of a statement after a schema change. ds@1952: ** Return true if the statement was recompiled successfully. ds@1952: ** Return false if there is an error of some kind. ds@1952: */ ds@1952: int sqlite3Reprepare(Vdbe *p){ ds@1952: int rc; ds@1952: sqlite3_stmt *pNew; ds@1952: const char *zSql; ds@1952: sqlite3 *db; ds@1952: ds@1952: assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); ds@1952: zSql = sqlite3VdbeGetSql(p); ds@1952: if( zSql==0 ){ ds@1952: return 0; ds@1952: } ds@1952: db = sqlite3VdbeDb(p); ds@1952: assert( sqlite3_mutex_held(db->mutex) ); ds@1952: rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0); ds@1952: if( rc ){ ds@1952: assert( pNew==0 ); ds@1952: return 0; ds@1952: }else{ ds@1952: assert( pNew!=0 ); ds@1952: } ds@1952: sqlite3VdbeSwap((Vdbe*)pNew, p); ds@1952: sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p); ds@1952: sqlite3VdbeResetStepResult((Vdbe*)pNew); ds@1952: sqlite3VdbeFinalize((Vdbe*)pNew); ds@1952: return 1; ds@1952: } ds@1952: ds@1952: ds@1952: /* ds@1952: ** Two versions of the official API. Legacy and new use. In the legacy ds@1952: ** version, the original SQL text is not saved in the prepared statement ds@1952: ** and so if a schema change occurs, SQLITE_SCHEMA is returned by ds@1952: ** sqlite3_step(). In the new version, the original SQL text is retained ds@1952: ** and the statement is automatically recompiled if an schema change ds@1952: ** occurs. ds@1952: */ ds@1952: int sqlite3_prepare( ds@1952: sqlite3 *db, /* Database handle. */ ds@1952: const char *zSql, /* UTF-8 encoded SQL statement. */ ds@1952: int nBytes, /* Length of zSql in bytes. */ ds@1952: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ ds@1952: const char **pzTail /* OUT: End of parsed string */ ds@1952: ){ ds@1952: return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail); ds@1952: } ds@1952: int sqlite3_prepare_v2( ds@1952: sqlite3 *db, /* Database handle. */ ds@1952: const char *zSql, /* UTF-8 encoded SQL statement. */ ds@1952: int nBytes, /* Length of zSql in bytes. */ ds@1952: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ ds@1952: const char **pzTail /* OUT: End of parsed string */ ds@1952: ){ ds@1952: return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail); ds@1952: } ds@1952: ds@1952: ds@1952: #ifndef SQLITE_OMIT_UTF16 ds@1952: /* ds@1952: ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. ds@1952: */ ds@1952: static int sqlite3Prepare16( ds@1952: sqlite3 *db, /* Database handle. */ ds@1952: const void *zSql, /* UTF-8 encoded SQL statement. */ ds@1952: int nBytes, /* Length of zSql in bytes. */ ds@1952: int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ ds@1952: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ ds@1952: const void **pzTail /* OUT: End of parsed string */ ds@1952: ){ ds@1952: /* This function currently works by first transforming the UTF-16 ds@1952: ** encoded string to UTF-8, then invoking sqlite3_prepare(). The ds@1952: ** tricky bit is figuring out the pointer to return in *pzTail. ds@1952: */ ds@1952: char *zSql8; ds@1952: const char *zTail8 = 0; ds@1952: int rc = SQLITE_OK; ds@1952: ds@1952: if( sqlite3SafetyCheck(db) ){ ds@1952: return SQLITE_MISUSE; ds@1952: } ds@1952: sqlite3_mutex_enter(db->mutex); ds@1952: zSql8 = sqlite3Utf16to8(db, zSql, nBytes); ds@1952: if( zSql8 ){ ds@1952: rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8); ds@1952: } ds@1952: ds@1952: if( zTail8 && pzTail ){ ds@1952: /* If sqlite3_prepare returns a tail pointer, we calculate the ds@1952: ** equivalent pointer into the UTF-16 string by counting the unicode ds@1952: ** characters between zSql8 and zTail8, and then returning a pointer ds@1952: ** the same number of characters into the UTF-16 string. ds@1952: */ ds@1952: int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8); ds@1952: *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); ds@1952: } ds@1952: sqlite3_free(zSql8); ds@1952: rc = sqlite3ApiExit(db, rc); ds@1952: sqlite3_mutex_leave(db->mutex); ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Two versions of the official API. Legacy and new use. In the legacy ds@1952: ** version, the original SQL text is not saved in the prepared statement ds@1952: ** and so if a schema change occurs, SQLITE_SCHEMA is returned by ds@1952: ** sqlite3_step(). In the new version, the original SQL text is retained ds@1952: ** and the statement is automatically recompiled if an schema change ds@1952: ** occurs. ds@1952: */ ds@1952: int sqlite3_prepare16( ds@1952: sqlite3 *db, /* Database handle. */ ds@1952: const void *zSql, /* UTF-8 encoded SQL statement. */ ds@1952: int nBytes, /* Length of zSql in bytes. */ ds@1952: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ ds@1952: const void **pzTail /* OUT: End of parsed string */ ds@1952: ){ ds@1952: return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); ds@1952: } ds@1952: int sqlite3_prepare16_v2( ds@1952: sqlite3 *db, /* Database handle. */ ds@1952: const void *zSql, /* UTF-8 encoded SQL statement. */ ds@1952: int nBytes, /* Length of zSql in bytes. */ ds@1952: sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ ds@1952: const void **pzTail /* OUT: End of parsed string */ ds@1952: ){ ds@1952: return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); ds@1952: } ds@1952: ds@1952: #endif /* SQLITE_OMIT_UTF16 */