1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/prepare.c Sun Jul 29 01:15:31 2007 +0000
1.3 @@ -0,0 +1,697 @@
1.4 +/*
1.5 +** 2005 May 25
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +** This file contains the implementation of the sqlite3_prepare()
1.16 +** interface, and routines that contribute to loading the database schema
1.17 +** from disk.
1.18 +**
1.19 +** $Id$
1.20 +*/
1.21 +#include "sqliteInt.h"
1.22 +#include "os.h"
1.23 +#include <ctype.h>
1.24 +
1.25 +/*
1.26 +** Fill the InitData structure with an error message that indicates
1.27 +** that the database is corrupt.
1.28 +*/
1.29 +static void corruptSchema(InitData *pData, const char *zExtra){
1.30 + if( !sqlite3MallocFailed() ){
1.31 + sqlite3SetString(pData->pzErrMsg, "malformed database schema",
1.32 + zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
1.33 + }
1.34 + pData->rc = SQLITE_CORRUPT;
1.35 +}
1.36 +
1.37 +/*
1.38 +** This is the callback routine for the code that initializes the
1.39 +** database. See sqlite3Init() below for additional information.
1.40 +** This routine is also called from the OP_ParseSchema opcode of the VDBE.
1.41 +**
1.42 +** Each callback contains the following information:
1.43 +**
1.44 +** argv[0] = name of thing being created
1.45 +** argv[1] = root page number for table or index. 0 for trigger or view.
1.46 +** argv[2] = SQL text for the CREATE statement.
1.47 +**
1.48 +*/
1.49 +int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
1.50 + InitData *pData = (InitData*)pInit;
1.51 + sqlite3 *db = pData->db;
1.52 + int iDb = pData->iDb;
1.53 +
1.54 + pData->rc = SQLITE_OK;
1.55 + DbClearProperty(db, iDb, DB_Empty);
1.56 + if( sqlite3MallocFailed() ){
1.57 + corruptSchema(pData, 0);
1.58 + return SQLITE_NOMEM;
1.59 + }
1.60 +
1.61 + assert( argc==3 );
1.62 + if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
1.63 + if( argv[1]==0 ){
1.64 + corruptSchema(pData, 0);
1.65 + return 1;
1.66 + }
1.67 + assert( iDb>=0 && iDb<db->nDb );
1.68 + if( argv[2] && argv[2][0] ){
1.69 + /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
1.70 + ** But because db->init.busy is set to 1, no VDBE code is generated
1.71 + ** or executed. All the parser does is build the internal data
1.72 + ** structures that describe the table, index, or view.
1.73 + */
1.74 + char *zErr;
1.75 + int rc;
1.76 + assert( db->init.busy );
1.77 + db->init.iDb = iDb;
1.78 + db->init.newTnum = atoi(argv[1]);
1.79 + rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
1.80 + db->init.iDb = 0;
1.81 + assert( rc!=SQLITE_OK || zErr==0 );
1.82 + if( SQLITE_OK!=rc ){
1.83 + pData->rc = rc;
1.84 + if( rc==SQLITE_NOMEM ){
1.85 + sqlite3FailedMalloc();
1.86 + }else if( rc!=SQLITE_INTERRUPT ){
1.87 + corruptSchema(pData, zErr);
1.88 + }
1.89 + sqlite3_free(zErr);
1.90 + return 1;
1.91 + }
1.92 + }else{
1.93 + /* If the SQL column is blank it means this is an index that
1.94 + ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
1.95 + ** constraint for a CREATE TABLE. The index should have already
1.96 + ** been created when we processed the CREATE TABLE. All we have
1.97 + ** to do here is record the root page number for that index.
1.98 + */
1.99 + Index *pIndex;
1.100 + pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
1.101 + if( pIndex==0 || pIndex->tnum!=0 ){
1.102 + /* This can occur if there exists an index on a TEMP table which
1.103 + ** has the same name as another index on a permanent index. Since
1.104 + ** the permanent table is hidden by the TEMP table, we can also
1.105 + ** safely ignore the index on the permanent table.
1.106 + */
1.107 + /* Do Nothing */;
1.108 + }else{
1.109 + pIndex->tnum = atoi(argv[1]);
1.110 + }
1.111 + }
1.112 + return 0;
1.113 +}
1.114 +
1.115 +/*
1.116 +** Attempt to read the database schema and initialize internal
1.117 +** data structures for a single database file. The index of the
1.118 +** database file is given by iDb. iDb==0 is used for the main
1.119 +** database. iDb==1 should never be used. iDb>=2 is used for
1.120 +** auxiliary databases. Return one of the SQLITE_ error codes to
1.121 +** indicate success or failure.
1.122 +*/
1.123 +static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
1.124 + int rc;
1.125 + BtCursor *curMain;
1.126 + int size;
1.127 + Table *pTab;
1.128 + Db *pDb;
1.129 + char const *azArg[4];
1.130 + int meta[10];
1.131 + InitData initData;
1.132 + char const *zMasterSchema;
1.133 + char const *zMasterName = SCHEMA_TABLE(iDb);
1.134 +
1.135 + /*
1.136 + ** The master database table has a structure like this
1.137 + */
1.138 + static const char master_schema[] =
1.139 + "CREATE TABLE sqlite_master(\n"
1.140 + " type text,\n"
1.141 + " name text,\n"
1.142 + " tbl_name text,\n"
1.143 + " rootpage integer,\n"
1.144 + " sql text\n"
1.145 + ")"
1.146 + ;
1.147 +#ifndef SQLITE_OMIT_TEMPDB
1.148 + static const char temp_master_schema[] =
1.149 + "CREATE TEMP TABLE sqlite_temp_master(\n"
1.150 + " type text,\n"
1.151 + " name text,\n"
1.152 + " tbl_name text,\n"
1.153 + " rootpage integer,\n"
1.154 + " sql text\n"
1.155 + ")"
1.156 + ;
1.157 +#else
1.158 + #define temp_master_schema 0
1.159 +#endif
1.160 +
1.161 + assert( iDb>=0 && iDb<db->nDb );
1.162 + assert( db->aDb[iDb].pSchema );
1.163 +
1.164 + /* zMasterSchema and zInitScript are set to point at the master schema
1.165 + ** and initialisation script appropriate for the database being
1.166 + ** initialised. zMasterName is the name of the master table.
1.167 + */
1.168 + if( !OMIT_TEMPDB && iDb==1 ){
1.169 + zMasterSchema = temp_master_schema;
1.170 + }else{
1.171 + zMasterSchema = master_schema;
1.172 + }
1.173 + zMasterName = SCHEMA_TABLE(iDb);
1.174 +
1.175 + /* Construct the schema tables. */
1.176 + sqlite3SafetyOff(db);
1.177 + azArg[0] = zMasterName;
1.178 + azArg[1] = "1";
1.179 + azArg[2] = zMasterSchema;
1.180 + azArg[3] = 0;
1.181 + initData.db = db;
1.182 + initData.iDb = iDb;
1.183 + initData.pzErrMsg = pzErrMsg;
1.184 + rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
1.185 + if( rc ){
1.186 + sqlite3SafetyOn(db);
1.187 + return initData.rc;
1.188 + }
1.189 + pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
1.190 + if( pTab ){
1.191 + pTab->readOnly = 1;
1.192 + }
1.193 + sqlite3SafetyOn(db);
1.194 +
1.195 + /* Create a cursor to hold the database open
1.196 + */
1.197 + pDb = &db->aDb[iDb];
1.198 + if( pDb->pBt==0 ){
1.199 + if( !OMIT_TEMPDB && iDb==1 ){
1.200 + DbSetProperty(db, 1, DB_SchemaLoaded);
1.201 + }
1.202 + return SQLITE_OK;
1.203 + }
1.204 + rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
1.205 + if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
1.206 + sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
1.207 + return rc;
1.208 + }
1.209 +
1.210 + /* Get the database meta information.
1.211 + **
1.212 + ** Meta values are as follows:
1.213 + ** meta[0] Schema cookie. Changes with each schema change.
1.214 + ** meta[1] File format of schema layer.
1.215 + ** meta[2] Size of the page cache.
1.216 + ** meta[3] Use freelist if 0. Autovacuum if greater than zero.
1.217 + ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
1.218 + ** meta[5] The user cookie. Used by the application.
1.219 + ** meta[6] Incremental-vacuum flag.
1.220 + ** meta[7]
1.221 + ** meta[8]
1.222 + ** meta[9]
1.223 + **
1.224 + ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
1.225 + ** the possible values of meta[4].
1.226 + */
1.227 + if( rc==SQLITE_OK ){
1.228 + int i;
1.229 + for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
1.230 + rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
1.231 + }
1.232 + if( rc ){
1.233 + sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
1.234 + sqlite3BtreeCloseCursor(curMain);
1.235 + return rc;
1.236 + }
1.237 + }else{
1.238 + memset(meta, 0, sizeof(meta));
1.239 + }
1.240 + pDb->pSchema->schema_cookie = meta[0];
1.241 +
1.242 + /* If opening a non-empty database, check the text encoding. For the
1.243 + ** main database, set sqlite3.enc to the encoding of the main database.
1.244 + ** For an attached db, it is an error if the encoding is not the same
1.245 + ** as sqlite3.enc.
1.246 + */
1.247 + if( meta[4] ){ /* text encoding */
1.248 + if( iDb==0 ){
1.249 + /* If opening the main database, set ENC(db). */
1.250 + ENC(db) = (u8)meta[4];
1.251 + db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
1.252 + }else{
1.253 + /* If opening an attached database, the encoding much match ENC(db) */
1.254 + if( meta[4]!=ENC(db) ){
1.255 + sqlite3BtreeCloseCursor(curMain);
1.256 + sqlite3SetString(pzErrMsg, "attached databases must use the same"
1.257 + " text encoding as main database", (char*)0);
1.258 + return SQLITE_ERROR;
1.259 + }
1.260 + }
1.261 + }else{
1.262 + DbSetProperty(db, iDb, DB_Empty);
1.263 + }
1.264 + pDb->pSchema->enc = ENC(db);
1.265 +
1.266 + size = meta[2];
1.267 + if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
1.268 + pDb->pSchema->cache_size = size;
1.269 + sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
1.270 +
1.271 + /*
1.272 + ** file_format==1 Version 3.0.0.
1.273 + ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN
1.274 + ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
1.275 + ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants
1.276 + */
1.277 + pDb->pSchema->file_format = meta[1];
1.278 + if( pDb->pSchema->file_format==0 ){
1.279 + pDb->pSchema->file_format = 1;
1.280 + }
1.281 + if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
1.282 + sqlite3BtreeCloseCursor(curMain);
1.283 + sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
1.284 + return SQLITE_ERROR;
1.285 + }
1.286 +
1.287 +
1.288 + /* Read the schema information out of the schema tables
1.289 + */
1.290 + assert( db->init.busy );
1.291 + if( rc==SQLITE_EMPTY ){
1.292 + /* For an empty database, there is nothing to read */
1.293 + rc = SQLITE_OK;
1.294 + }else{
1.295 + char *zSql;
1.296 + zSql = sqlite3MPrintf(
1.297 + "SELECT name, rootpage, sql FROM '%q'.%s",
1.298 + db->aDb[iDb].zName, zMasterName);
1.299 + sqlite3SafetyOff(db);
1.300 + rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
1.301 + if( rc==SQLITE_ABORT ) rc = initData.rc;
1.302 + sqlite3SafetyOn(db);
1.303 + sqliteFree(zSql);
1.304 +#ifndef SQLITE_OMIT_ANALYZE
1.305 + if( rc==SQLITE_OK ){
1.306 + sqlite3AnalysisLoad(db, iDb);
1.307 + }
1.308 +#endif
1.309 + sqlite3BtreeCloseCursor(curMain);
1.310 + }
1.311 + if( sqlite3MallocFailed() ){
1.312 + /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
1.313 + rc = SQLITE_NOMEM;
1.314 + sqlite3ResetInternalSchema(db, 0);
1.315 + }
1.316 + if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
1.317 + /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
1.318 + ** the schema loaded, even if errors occured. In this situation the
1.319 + ** current sqlite3_prepare() operation will fail, but the following one
1.320 + ** will attempt to compile the supplied statement against whatever subset
1.321 + ** of the schema was loaded before the error occured. The primary
1.322 + ** purpose of this is to allow access to the sqlite_master table
1.323 + ** even when it's contents have been corrupted.
1.324 + */
1.325 + DbSetProperty(db, iDb, DB_SchemaLoaded);
1.326 + rc = SQLITE_OK;
1.327 + }
1.328 + return rc;
1.329 +}
1.330 +
1.331 +/*
1.332 +** Initialize all database files - the main database file, the file
1.333 +** used to store temporary tables, and any additional database files
1.334 +** created using ATTACH statements. Return a success code. If an
1.335 +** error occurs, write an error message into *pzErrMsg.
1.336 +**
1.337 +** After a database is initialized, the DB_SchemaLoaded bit is set
1.338 +** bit is set in the flags field of the Db structure. If the database
1.339 +** file was of zero-length, then the DB_Empty flag is also set.
1.340 +*/
1.341 +int sqlite3Init(sqlite3 *db, char **pzErrMsg){
1.342 + int i, rc;
1.343 + int called_initone = 0;
1.344 +
1.345 + if( db->init.busy ) return SQLITE_OK;
1.346 + rc = SQLITE_OK;
1.347 + db->init.busy = 1;
1.348 + for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1.349 + if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
1.350 + rc = sqlite3InitOne(db, i, pzErrMsg);
1.351 + if( rc ){
1.352 + sqlite3ResetInternalSchema(db, i);
1.353 + }
1.354 + called_initone = 1;
1.355 + }
1.356 +
1.357 + /* Once all the other databases have been initialised, load the schema
1.358 + ** for the TEMP database. This is loaded last, as the TEMP database
1.359 + ** schema may contain references to objects in other databases.
1.360 + */
1.361 +#ifndef SQLITE_OMIT_TEMPDB
1.362 + if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
1.363 + rc = sqlite3InitOne(db, 1, pzErrMsg);
1.364 + if( rc ){
1.365 + sqlite3ResetInternalSchema(db, 1);
1.366 + }
1.367 + called_initone = 1;
1.368 + }
1.369 +#endif
1.370 +
1.371 + db->init.busy = 0;
1.372 + if( rc==SQLITE_OK && called_initone ){
1.373 + sqlite3CommitInternalChanges(db);
1.374 + }
1.375 +
1.376 + return rc;
1.377 +}
1.378 +
1.379 +/*
1.380 +** This routine is a no-op if the database schema is already initialised.
1.381 +** Otherwise, the schema is loaded. An error code is returned.
1.382 +*/
1.383 +int sqlite3ReadSchema(Parse *pParse){
1.384 + int rc = SQLITE_OK;
1.385 + sqlite3 *db = pParse->db;
1.386 + if( !db->init.busy ){
1.387 + rc = sqlite3Init(db, &pParse->zErrMsg);
1.388 + }
1.389 + if( rc!=SQLITE_OK ){
1.390 + pParse->rc = rc;
1.391 + pParse->nErr++;
1.392 + }
1.393 + return rc;
1.394 +}
1.395 +
1.396 +
1.397 +/*
1.398 +** Check schema cookies in all databases. If any cookie is out
1.399 +** of date, return 0. If all schema cookies are current, return 1.
1.400 +*/
1.401 +static int schemaIsValid(sqlite3 *db){
1.402 + int iDb;
1.403 + int rc;
1.404 + BtCursor *curTemp;
1.405 + int cookie;
1.406 + int allOk = 1;
1.407 +
1.408 + for(iDb=0; allOk && iDb<db->nDb; iDb++){
1.409 + Btree *pBt;
1.410 + pBt = db->aDb[iDb].pBt;
1.411 + if( pBt==0 ) continue;
1.412 + rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
1.413 + if( rc==SQLITE_OK ){
1.414 + rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
1.415 + if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
1.416 + allOk = 0;
1.417 + }
1.418 + sqlite3BtreeCloseCursor(curTemp);
1.419 + }
1.420 + }
1.421 + return allOk;
1.422 +}
1.423 +
1.424 +/*
1.425 +** Convert a schema pointer into the iDb index that indicates
1.426 +** which database file in db->aDb[] the schema refers to.
1.427 +**
1.428 +** If the same database is attached more than once, the first
1.429 +** attached database is returned.
1.430 +*/
1.431 +int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
1.432 + int i = -1000000;
1.433 +
1.434 + /* If pSchema is NULL, then return -1000000. This happens when code in
1.435 + ** expr.c is trying to resolve a reference to a transient table (i.e. one
1.436 + ** created by a sub-select). In this case the return value of this
1.437 + ** function should never be used.
1.438 + **
1.439 + ** We return -1000000 instead of the more usual -1 simply because using
1.440 + ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much
1.441 + ** more likely to cause a segfault than -1 (of course there are assert()
1.442 + ** statements too, but it never hurts to play the odds).
1.443 + */
1.444 + if( pSchema ){
1.445 + for(i=0; i<db->nDb; i++){
1.446 + if( db->aDb[i].pSchema==pSchema ){
1.447 + break;
1.448 + }
1.449 + }
1.450 + assert( i>=0 &&i>=0 && i<db->nDb );
1.451 + }
1.452 + return i;
1.453 +}
1.454 +
1.455 +/*
1.456 +** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
1.457 +*/
1.458 +int sqlite3Prepare(
1.459 + sqlite3 *db, /* Database handle. */
1.460 + const char *zSql, /* UTF-8 encoded SQL statement. */
1.461 + int nBytes, /* Length of zSql in bytes. */
1.462 + int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
1.463 + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1.464 + const char **pzTail /* OUT: End of parsed string */
1.465 +){
1.466 + Parse sParse;
1.467 + char *zErrMsg = 0;
1.468 + int rc = SQLITE_OK;
1.469 + int i;
1.470 +
1.471 + /* Assert that malloc() has not failed */
1.472 + assert( !sqlite3MallocFailed() );
1.473 +
1.474 + assert( ppStmt );
1.475 + *ppStmt = 0;
1.476 + if( sqlite3SafetyOn(db) ){
1.477 + return SQLITE_MISUSE;
1.478 + }
1.479 +
1.480 + /* If any attached database schemas are locked, do not proceed with
1.481 + ** compilation. Instead return SQLITE_LOCKED immediately.
1.482 + */
1.483 + for(i=0; i<db->nDb; i++) {
1.484 + Btree *pBt = db->aDb[i].pBt;
1.485 + if( pBt && sqlite3BtreeSchemaLocked(pBt) ){
1.486 + const char *zDb = db->aDb[i].zName;
1.487 + sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
1.488 + sqlite3SafetyOff(db);
1.489 + return SQLITE_LOCKED;
1.490 + }
1.491 + }
1.492 +
1.493 + memset(&sParse, 0, sizeof(sParse));
1.494 + sParse.db = db;
1.495 + if( nBytes>=0 && zSql[nBytes]!=0 ){
1.496 + char *zSqlCopy;
1.497 + if( nBytes>SQLITE_MAX_SQL_LENGTH ){
1.498 + return SQLITE_TOOBIG;
1.499 + }
1.500 + zSqlCopy = sqlite3StrNDup(zSql, nBytes);
1.501 + if( zSqlCopy ){
1.502 + sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
1.503 + sqliteFree(zSqlCopy);
1.504 + }
1.505 + sParse.zTail = &zSql[nBytes];
1.506 + }else{
1.507 + sqlite3RunParser(&sParse, zSql, &zErrMsg);
1.508 + }
1.509 +
1.510 + if( sqlite3MallocFailed() ){
1.511 + sParse.rc = SQLITE_NOMEM;
1.512 + }
1.513 + if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
1.514 + if( sParse.checkSchema && !schemaIsValid(db) ){
1.515 + sParse.rc = SQLITE_SCHEMA;
1.516 + }
1.517 + if( sParse.rc==SQLITE_SCHEMA ){
1.518 + sqlite3ResetInternalSchema(db, 0);
1.519 + }
1.520 + if( sqlite3MallocFailed() ){
1.521 + sParse.rc = SQLITE_NOMEM;
1.522 + }
1.523 + if( pzTail ){
1.524 + *pzTail = sParse.zTail;
1.525 + }
1.526 + rc = sParse.rc;
1.527 +
1.528 +#ifndef SQLITE_OMIT_EXPLAIN
1.529 + if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1.530 + if( sParse.explain==2 ){
1.531 + sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
1.532 + sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
1.533 + sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
1.534 + sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
1.535 + }else{
1.536 + sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
1.537 + sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
1.538 + sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
1.539 + sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
1.540 + sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
1.541 + sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
1.542 + }
1.543 + }
1.544 +#endif
1.545 +
1.546 + if( sqlite3SafetyOff(db) ){
1.547 + rc = SQLITE_MISUSE;
1.548 + }
1.549 +
1.550 + if( saveSqlFlag ){
1.551 + sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
1.552 + }
1.553 + if( rc!=SQLITE_OK || sqlite3MallocFailed() ){
1.554 + sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
1.555 + assert(!(*ppStmt));
1.556 + }else{
1.557 + *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1.558 + }
1.559 +
1.560 + if( zErrMsg ){
1.561 + sqlite3Error(db, rc, "%s", zErrMsg);
1.562 + sqliteFree(zErrMsg);
1.563 + }else{
1.564 + sqlite3Error(db, rc, 0);
1.565 + }
1.566 +
1.567 + rc = sqlite3ApiExit(db, rc);
1.568 + sqlite3ReleaseThreadData();
1.569 + assert( (rc&db->errMask)==rc );
1.570 + return rc;
1.571 +}
1.572 +
1.573 +/*
1.574 +** Rerun the compilation of a statement after a schema change.
1.575 +** Return true if the statement was recompiled successfully.
1.576 +** Return false if there is an error of some kind.
1.577 +*/
1.578 +int sqlite3Reprepare(Vdbe *p){
1.579 + int rc;
1.580 + sqlite3_stmt *pNew;
1.581 + const char *zSql;
1.582 + sqlite3 *db;
1.583 +
1.584 + zSql = sqlite3VdbeGetSql(p);
1.585 + if( zSql==0 ){
1.586 + return 0;
1.587 + }
1.588 + db = sqlite3VdbeDb(p);
1.589 + rc = sqlite3Prepare(db, zSql, -1, 0, &pNew, 0);
1.590 + if( rc ){
1.591 + assert( pNew==0 );
1.592 + return 0;
1.593 + }else{
1.594 + assert( pNew!=0 );
1.595 + }
1.596 + sqlite3VdbeSwap((Vdbe*)pNew, p);
1.597 + sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p);
1.598 + sqlite3VdbeResetStepResult((Vdbe*)pNew);
1.599 + sqlite3VdbeFinalize((Vdbe*)pNew);
1.600 + return 1;
1.601 +}
1.602 +
1.603 +
1.604 +/*
1.605 +** Two versions of the official API. Legacy and new use. In the legacy
1.606 +** version, the original SQL text is not saved in the prepared statement
1.607 +** and so if a schema change occurs, SQLITE_SCHEMA is returned by
1.608 +** sqlite3_step(). In the new version, the original SQL text is retained
1.609 +** and the statement is automatically recompiled if an schema change
1.610 +** occurs.
1.611 +*/
1.612 +int sqlite3_prepare(
1.613 + sqlite3 *db, /* Database handle. */
1.614 + const char *zSql, /* UTF-8 encoded SQL statement. */
1.615 + int nBytes, /* Length of zSql in bytes. */
1.616 + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1.617 + const char **pzTail /* OUT: End of parsed string */
1.618 +){
1.619 + return sqlite3Prepare(db,zSql,nBytes,0,ppStmt,pzTail);
1.620 +}
1.621 +int sqlite3_prepare_v2(
1.622 + sqlite3 *db, /* Database handle. */
1.623 + const char *zSql, /* UTF-8 encoded SQL statement. */
1.624 + int nBytes, /* Length of zSql in bytes. */
1.625 + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1.626 + const char **pzTail /* OUT: End of parsed string */
1.627 +){
1.628 + return sqlite3Prepare(db,zSql,nBytes,1,ppStmt,pzTail);
1.629 +}
1.630 +
1.631 +
1.632 +#ifndef SQLITE_OMIT_UTF16
1.633 +/*
1.634 +** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1.635 +*/
1.636 +static int sqlite3Prepare16(
1.637 + sqlite3 *db, /* Database handle. */
1.638 + const void *zSql, /* UTF-8 encoded SQL statement. */
1.639 + int nBytes, /* Length of zSql in bytes. */
1.640 + int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */
1.641 + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1.642 + const void **pzTail /* OUT: End of parsed string */
1.643 +){
1.644 + /* This function currently works by first transforming the UTF-16
1.645 + ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1.646 + ** tricky bit is figuring out the pointer to return in *pzTail.
1.647 + */
1.648 + char *zSql8;
1.649 + const char *zTail8 = 0;
1.650 + int rc = SQLITE_OK;
1.651 +
1.652 + if( sqlite3SafetyCheck(db) ){
1.653 + return SQLITE_MISUSE;
1.654 + }
1.655 + zSql8 = sqlite3Utf16to8(zSql, nBytes);
1.656 + if( zSql8 ){
1.657 + rc = sqlite3Prepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
1.658 + }
1.659 +
1.660 + if( zTail8 && pzTail ){
1.661 + /* If sqlite3_prepare returns a tail pointer, we calculate the
1.662 + ** equivalent pointer into the UTF-16 string by counting the unicode
1.663 + ** characters between zSql8 and zTail8, and then returning a pointer
1.664 + ** the same number of characters into the UTF-16 string.
1.665 + */
1.666 + int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8);
1.667 + *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
1.668 + }
1.669 + sqliteFree(zSql8);
1.670 + return sqlite3ApiExit(db, rc);
1.671 +}
1.672 +
1.673 +/*
1.674 +** Two versions of the official API. Legacy and new use. In the legacy
1.675 +** version, the original SQL text is not saved in the prepared statement
1.676 +** and so if a schema change occurs, SQLITE_SCHEMA is returned by
1.677 +** sqlite3_step(). In the new version, the original SQL text is retained
1.678 +** and the statement is automatically recompiled if an schema change
1.679 +** occurs.
1.680 +*/
1.681 +int sqlite3_prepare16(
1.682 + sqlite3 *db, /* Database handle. */
1.683 + const void *zSql, /* UTF-8 encoded SQL statement. */
1.684 + int nBytes, /* Length of zSql in bytes. */
1.685 + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1.686 + const void **pzTail /* OUT: End of parsed string */
1.687 +){
1.688 + return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
1.689 +}
1.690 +int sqlite3_prepare16_v2(
1.691 + sqlite3 *db, /* Database handle. */
1.692 + const void *zSql, /* UTF-8 encoded SQL statement. */
1.693 + int nBytes, /* Length of zSql in bytes. */
1.694 + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1.695 + const void **pzTail /* OUT: End of parsed string */
1.696 +){
1.697 + return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
1.698 +}
1.699 +
1.700 +#endif /* SQLITE_OMIT_UTF16 */