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