extensions/sqlite/sqlite-source/prepare.c
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/extensions/sqlite/sqlite-source/prepare.c	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,742 +0,0 @@
     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 <ctype.h>
    1.23 -
    1.24 -/*
    1.25 -** Fill the InitData structure with an error message that indicates
    1.26 -** that the database is corrupt.
    1.27 -*/
    1.28 -static void corruptSchema(InitData *pData, const char *zExtra){
    1.29 -  if( !pData->db->mallocFailed ){
    1.30 -    sqlite3SetString(pData->pzErrMsg, "malformed database schema",
    1.31 -       zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
    1.32 -  }
    1.33 -  pData->rc = SQLITE_CORRUPT;
    1.34 -}
    1.35 -
    1.36 -/*
    1.37 -** This is the callback routine for the code that initializes the
    1.38 -** database.  See sqlite3Init() below for additional information.
    1.39 -** This routine is also called from the OP_ParseSchema opcode of the VDBE.
    1.40 -**
    1.41 -** Each callback contains the following information:
    1.42 -**
    1.43 -**     argv[0] = name of thing being created
    1.44 -**     argv[1] = root page number for table or index. 0 for trigger or view.
    1.45 -**     argv[2] = SQL text for the CREATE statement.
    1.46 -**
    1.47 -*/
    1.48 -int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
    1.49 -  InitData *pData = (InitData*)pInit;
    1.50 -  sqlite3 *db = pData->db;
    1.51 -  int iDb = pData->iDb;
    1.52 -
    1.53 -  assert( sqlite3_mutex_held(db->mutex) );
    1.54 -  pData->rc = SQLITE_OK;
    1.55 -  DbClearProperty(db, iDb, DB_Empty);
    1.56 -  if( db->mallocFailed ){
    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 -        db->mallocFailed = 1;
    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 -  assert( sqlite3_mutex_held(db->mutex) );
   1.164 -
   1.165 -  /* zMasterSchema and zInitScript are set to point at the master schema
   1.166 -  ** and initialisation script appropriate for the database being
   1.167 -  ** initialised. zMasterName is the name of the master table.
   1.168 -  */
   1.169 -  if( !OMIT_TEMPDB && iDb==1 ){
   1.170 -    zMasterSchema = temp_master_schema;
   1.171 -  }else{
   1.172 -    zMasterSchema = master_schema;
   1.173 -  }
   1.174 -  zMasterName = SCHEMA_TABLE(iDb);
   1.175 -
   1.176 -  /* Construct the schema tables.  */
   1.177 -  sqlite3SafetyOff(db);
   1.178 -  azArg[0] = zMasterName;
   1.179 -  azArg[1] = "1";
   1.180 -  azArg[2] = zMasterSchema;
   1.181 -  azArg[3] = 0;
   1.182 -  initData.db = db;
   1.183 -  initData.iDb = iDb;
   1.184 -  initData.pzErrMsg = pzErrMsg;
   1.185 -  rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
   1.186 -  if( rc ){
   1.187 -    sqlite3SafetyOn(db);
   1.188 -    rc = initData.rc;
   1.189 -    goto error_out;
   1.190 -  }
   1.191 -  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
   1.192 -  if( pTab ){
   1.193 -    pTab->readOnly = 1;
   1.194 -  }
   1.195 -  sqlite3SafetyOn(db);
   1.196 -
   1.197 -  /* Create a cursor to hold the database open
   1.198 -  */
   1.199 -  pDb = &db->aDb[iDb];
   1.200 -  if( pDb->pBt==0 ){
   1.201 -    if( !OMIT_TEMPDB && iDb==1 ){
   1.202 -      DbSetProperty(db, 1, DB_SchemaLoaded);
   1.203 -    }
   1.204 -    return SQLITE_OK;
   1.205 -  }
   1.206 -  sqlite3BtreeEnter(pDb->pBt);
   1.207 -  rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
   1.208 -  if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
   1.209 -    sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
   1.210 -    sqlite3BtreeLeave(pDb->pBt);
   1.211 -    goto error_out;
   1.212 -  }
   1.213 -
   1.214 -  /* Get the database meta information.
   1.215 -  **
   1.216 -  ** Meta values are as follows:
   1.217 -  **    meta[0]   Schema cookie.  Changes with each schema change.
   1.218 -  **    meta[1]   File format of schema layer.
   1.219 -  **    meta[2]   Size of the page cache.
   1.220 -  **    meta[3]   Use freelist if 0.  Autovacuum if greater than zero.
   1.221 -  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
   1.222 -  **    meta[5]   The user cookie. Used by the application.
   1.223 -  **    meta[6]   Incremental-vacuum flag.
   1.224 -  **    meta[7]
   1.225 -  **    meta[8]
   1.226 -  **    meta[9]
   1.227 -  **
   1.228 -  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
   1.229 -  ** the possible values of meta[4].
   1.230 -  */
   1.231 -  if( rc==SQLITE_OK ){
   1.232 -    int i;
   1.233 -    for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
   1.234 -      rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
   1.235 -    }
   1.236 -    if( rc ){
   1.237 -      sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
   1.238 -      sqlite3BtreeCloseCursor(curMain);
   1.239 -      sqlite3BtreeLeave(pDb->pBt);
   1.240 -      goto error_out;
   1.241 -    }
   1.242 -  }else{
   1.243 -    memset(meta, 0, sizeof(meta));
   1.244 -  }
   1.245 -  pDb->pSchema->schema_cookie = meta[0];
   1.246 -
   1.247 -  /* If opening a non-empty database, check the text encoding. For the
   1.248 -  ** main database, set sqlite3.enc to the encoding of the main database.
   1.249 -  ** For an attached db, it is an error if the encoding is not the same
   1.250 -  ** as sqlite3.enc.
   1.251 -  */
   1.252 -  if( meta[4] ){  /* text encoding */
   1.253 -    if( iDb==0 ){
   1.254 -      /* If opening the main database, set ENC(db). */
   1.255 -      ENC(db) = (u8)meta[4];
   1.256 -      db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
   1.257 -    }else{
   1.258 -      /* If opening an attached database, the encoding much match ENC(db) */
   1.259 -      if( meta[4]!=ENC(db) ){
   1.260 -        sqlite3BtreeCloseCursor(curMain);
   1.261 -        sqlite3SetString(pzErrMsg, "attached databases must use the same"
   1.262 -            " text encoding as main database", (char*)0);
   1.263 -        sqlite3BtreeLeave(pDb->pBt);
   1.264 -        return SQLITE_ERROR;
   1.265 -      }
   1.266 -    }
   1.267 -  }else{
   1.268 -    DbSetProperty(db, iDb, DB_Empty);
   1.269 -  }
   1.270 -  pDb->pSchema->enc = ENC(db);
   1.271 -
   1.272 -  size = meta[2];
   1.273 -  if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
   1.274 -  pDb->pSchema->cache_size = size;
   1.275 -  sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
   1.276 -
   1.277 -  /*
   1.278 -  ** file_format==1    Version 3.0.0.
   1.279 -  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
   1.280 -  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
   1.281 -  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
   1.282 -  */
   1.283 -  pDb->pSchema->file_format = meta[1];
   1.284 -  if( pDb->pSchema->file_format==0 ){
   1.285 -    pDb->pSchema->file_format = 1;
   1.286 -  }
   1.287 -  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
   1.288 -    sqlite3BtreeCloseCursor(curMain);
   1.289 -    sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
   1.290 -    sqlite3BtreeLeave(pDb->pBt);
   1.291 -    return SQLITE_ERROR;
   1.292 -  }
   1.293 -
   1.294 -
   1.295 -  /* Read the schema information out of the schema tables
   1.296 -  */
   1.297 -  assert( db->init.busy );
   1.298 -  if( rc==SQLITE_EMPTY ){
   1.299 -    /* For an empty database, there is nothing to read */
   1.300 -    rc = SQLITE_OK;
   1.301 -  }else{
   1.302 -    char *zSql;
   1.303 -    zSql = sqlite3MPrintf(db, 
   1.304 -        "SELECT name, rootpage, sql FROM '%q'.%s",
   1.305 -        db->aDb[iDb].zName, zMasterName);
   1.306 -    sqlite3SafetyOff(db);
   1.307 -    rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
   1.308 -    if( rc==SQLITE_ABORT ) rc = initData.rc;
   1.309 -    sqlite3SafetyOn(db);
   1.310 -    sqlite3_free(zSql);
   1.311 -#ifndef SQLITE_OMIT_ANALYZE
   1.312 -    if( rc==SQLITE_OK ){
   1.313 -      sqlite3AnalysisLoad(db, iDb);
   1.314 -    }
   1.315 -#endif
   1.316 -    sqlite3BtreeCloseCursor(curMain);
   1.317 -  }
   1.318 -  if( db->mallocFailed ){
   1.319 -    /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
   1.320 -    rc = SQLITE_NOMEM;
   1.321 -    sqlite3ResetInternalSchema(db, 0);
   1.322 -  }
   1.323 -  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
   1.324 -    /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
   1.325 -    ** the schema loaded, even if errors occured. In this situation the 
   1.326 -    ** current sqlite3_prepare() operation will fail, but the following one
   1.327 -    ** will attempt to compile the supplied statement against whatever subset
   1.328 -    ** of the schema was loaded before the error occured. The primary
   1.329 -    ** purpose of this is to allow access to the sqlite_master table
   1.330 -    ** even when it's contents have been corrupted.
   1.331 -    */
   1.332 -    DbSetProperty(db, iDb, DB_SchemaLoaded);
   1.333 -    rc = SQLITE_OK;
   1.334 -  }
   1.335 -  sqlite3BtreeLeave(pDb->pBt);
   1.336 -
   1.337 -error_out:
   1.338 -  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
   1.339 -    db->mallocFailed = 1;
   1.340 -  }
   1.341 -  return rc;
   1.342 -}
   1.343 -
   1.344 -/*
   1.345 -** Initialize all database files - the main database file, the file
   1.346 -** used to store temporary tables, and any additional database files
   1.347 -** created using ATTACH statements.  Return a success code.  If an
   1.348 -** error occurs, write an error message into *pzErrMsg.
   1.349 -**
   1.350 -** After a database is initialized, the DB_SchemaLoaded bit is set
   1.351 -** bit is set in the flags field of the Db structure. If the database
   1.352 -** file was of zero-length, then the DB_Empty flag is also set.
   1.353 -*/
   1.354 -int sqlite3Init(sqlite3 *db, char **pzErrMsg){
   1.355 -  int i, rc;
   1.356 -  int commit_internal = !(db->flags&SQLITE_InternChanges);
   1.357 -  
   1.358 -  assert( sqlite3_mutex_held(db->mutex) );
   1.359 -  if( db->init.busy ) return SQLITE_OK;
   1.360 -  rc = SQLITE_OK;
   1.361 -  db->init.busy = 1;
   1.362 -  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
   1.363 -    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
   1.364 -    rc = sqlite3InitOne(db, i, pzErrMsg);
   1.365 -    if( rc ){
   1.366 -      sqlite3ResetInternalSchema(db, i);
   1.367 -    }
   1.368 -  }
   1.369 -
   1.370 -  /* Once all the other databases have been initialised, load the schema
   1.371 -  ** for the TEMP database. This is loaded last, as the TEMP database
   1.372 -  ** schema may contain references to objects in other databases.
   1.373 -  */
   1.374 -#ifndef SQLITE_OMIT_TEMPDB
   1.375 -  if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
   1.376 -    rc = sqlite3InitOne(db, 1, pzErrMsg);
   1.377 -    if( rc ){
   1.378 -      sqlite3ResetInternalSchema(db, 1);
   1.379 -    }
   1.380 -  }
   1.381 -#endif
   1.382 -
   1.383 -  db->init.busy = 0;
   1.384 -  if( rc==SQLITE_OK && commit_internal ){
   1.385 -    sqlite3CommitInternalChanges(db);
   1.386 -  }
   1.387 -
   1.388 -  return rc; 
   1.389 -}
   1.390 -
   1.391 -/*
   1.392 -** This routine is a no-op if the database schema is already initialised.
   1.393 -** Otherwise, the schema is loaded. An error code is returned.
   1.394 -*/
   1.395 -int sqlite3ReadSchema(Parse *pParse){
   1.396 -  int rc = SQLITE_OK;
   1.397 -  sqlite3 *db = pParse->db;
   1.398 -  assert( sqlite3_mutex_held(db->mutex) );
   1.399 -  if( !db->init.busy ){
   1.400 -    rc = sqlite3Init(db, &pParse->zErrMsg);
   1.401 -  }
   1.402 -  if( rc!=SQLITE_OK ){
   1.403 -    pParse->rc = rc;
   1.404 -    pParse->nErr++;
   1.405 -  }
   1.406 -  return rc;
   1.407 -}
   1.408 -
   1.409 -
   1.410 -/*
   1.411 -** Check schema cookies in all databases.  If any cookie is out
   1.412 -** of date, return 0.  If all schema cookies are current, return 1.
   1.413 -*/
   1.414 -static int schemaIsValid(sqlite3 *db){
   1.415 -  int iDb;
   1.416 -  int rc;
   1.417 -  BtCursor *curTemp;
   1.418 -  int cookie;
   1.419 -  int allOk = 1;
   1.420 -
   1.421 -  assert( sqlite3_mutex_held(db->mutex) );
   1.422 -  for(iDb=0; allOk && iDb<db->nDb; iDb++){
   1.423 -    Btree *pBt;
   1.424 -    pBt = db->aDb[iDb].pBt;
   1.425 -    if( pBt==0 ) continue;
   1.426 -    rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
   1.427 -    if( rc==SQLITE_OK ){
   1.428 -      rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
   1.429 -      if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
   1.430 -        allOk = 0;
   1.431 -      }
   1.432 -      sqlite3BtreeCloseCursor(curTemp);
   1.433 -    }
   1.434 -    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
   1.435 -      db->mallocFailed = 1;
   1.436 -    }
   1.437 -  }
   1.438 -  return allOk;
   1.439 -}
   1.440 -
   1.441 -/*
   1.442 -** Convert a schema pointer into the iDb index that indicates
   1.443 -** which database file in db->aDb[] the schema refers to.
   1.444 -**
   1.445 -** If the same database is attached more than once, the first
   1.446 -** attached database is returned.
   1.447 -*/
   1.448 -int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
   1.449 -  int i = -1000000;
   1.450 -
   1.451 -  /* If pSchema is NULL, then return -1000000. This happens when code in 
   1.452 -  ** expr.c is trying to resolve a reference to a transient table (i.e. one
   1.453 -  ** created by a sub-select). In this case the return value of this 
   1.454 -  ** function should never be used.
   1.455 -  **
   1.456 -  ** We return -1000000 instead of the more usual -1 simply because using
   1.457 -  ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much 
   1.458 -  ** more likely to cause a segfault than -1 (of course there are assert()
   1.459 -  ** statements too, but it never hurts to play the odds).
   1.460 -  */
   1.461 -  assert( sqlite3_mutex_held(db->mutex) );
   1.462 -  if( pSchema ){
   1.463 -    for(i=0; i<db->nDb; i++){
   1.464 -      if( db->aDb[i].pSchema==pSchema ){
   1.465 -        break;
   1.466 -      }
   1.467 -    }
   1.468 -    assert( i>=0 &&i>=0 &&  i<db->nDb );
   1.469 -  }
   1.470 -  return i;
   1.471 -}
   1.472 -
   1.473 -/*
   1.474 -** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
   1.475 -*/
   1.476 -int sqlite3Prepare(
   1.477 -  sqlite3 *db,              /* Database handle. */
   1.478 -  const char *zSql,         /* UTF-8 encoded SQL statement. */
   1.479 -  int nBytes,               /* Length of zSql in bytes. */
   1.480 -  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
   1.481 -  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   1.482 -  const char **pzTail       /* OUT: End of parsed string */
   1.483 -){
   1.484 -  Parse sParse;
   1.485 -  char *zErrMsg = 0;
   1.486 -  int rc = SQLITE_OK;
   1.487 -  int i;
   1.488 -
   1.489 -  assert( ppStmt );
   1.490 -  *ppStmt = 0;
   1.491 -  if( sqlite3SafetyOn(db) ){
   1.492 -    return SQLITE_MISUSE;
   1.493 -  }
   1.494 -  assert( !db->mallocFailed );
   1.495 -  assert( sqlite3_mutex_held(db->mutex) );
   1.496 -
   1.497 -  /* If any attached database schemas are locked, do not proceed with
   1.498 -  ** compilation. Instead return SQLITE_LOCKED immediately.
   1.499 -  */
   1.500 -  for(i=0; i<db->nDb; i++) {
   1.501 -    Btree *pBt = db->aDb[i].pBt;
   1.502 -    if( pBt ){
   1.503 -      int rc;
   1.504 -      rc = sqlite3BtreeSchemaLocked(pBt);
   1.505 -      if( rc ){
   1.506 -        const char *zDb = db->aDb[i].zName;
   1.507 -        sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
   1.508 -        sqlite3SafetyOff(db);
   1.509 -        return SQLITE_LOCKED;
   1.510 -      }
   1.511 -    }
   1.512 -  }
   1.513 -  
   1.514 -  memset(&sParse, 0, sizeof(sParse));
   1.515 -  sParse.db = db;
   1.516 -  if( nBytes>=0 && zSql[nBytes]!=0 ){
   1.517 -    char *zSqlCopy;
   1.518 -    if( nBytes>SQLITE_MAX_SQL_LENGTH ){
   1.519 -      return SQLITE_TOOBIG;
   1.520 -    }
   1.521 -    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
   1.522 -    if( zSqlCopy ){
   1.523 -      sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
   1.524 -      sqlite3_free(zSqlCopy);
   1.525 -    }
   1.526 -    sParse.zTail = &zSql[nBytes];
   1.527 -  }else{
   1.528 -    sqlite3RunParser(&sParse, zSql, &zErrMsg);
   1.529 -  }
   1.530 -
   1.531 -  if( db->mallocFailed ){
   1.532 -    sParse.rc = SQLITE_NOMEM;
   1.533 -  }
   1.534 -  if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
   1.535 -  if( sParse.checkSchema && !schemaIsValid(db) ){
   1.536 -    sParse.rc = SQLITE_SCHEMA;
   1.537 -  }
   1.538 -  if( sParse.rc==SQLITE_SCHEMA ){
   1.539 -    sqlite3ResetInternalSchema(db, 0);
   1.540 -  }
   1.541 -  if( db->mallocFailed ){
   1.542 -    sParse.rc = SQLITE_NOMEM;
   1.543 -  }
   1.544 -  if( pzTail ){
   1.545 -    *pzTail = sParse.zTail;
   1.546 -  }
   1.547 -  rc = sParse.rc;
   1.548 -
   1.549 -#ifndef SQLITE_OMIT_EXPLAIN
   1.550 -  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
   1.551 -    if( sParse.explain==2 ){
   1.552 -      sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
   1.553 -      sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
   1.554 -      sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
   1.555 -      sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
   1.556 -    }else{
   1.557 -      sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
   1.558 -      sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
   1.559 -      sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
   1.560 -      sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
   1.561 -      sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
   1.562 -      sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
   1.563 -    }
   1.564 -  }
   1.565 -#endif
   1.566 -
   1.567 -  if( sqlite3SafetyOff(db) ){
   1.568 -    rc = SQLITE_MISUSE;
   1.569 -  }
   1.570 -
   1.571 -  if( saveSqlFlag ){
   1.572 -    sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
   1.573 -  }
   1.574 -  if( rc!=SQLITE_OK || db->mallocFailed ){
   1.575 -    sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
   1.576 -    assert(!(*ppStmt));
   1.577 -  }else{
   1.578 -    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
   1.579 -  }
   1.580 -
   1.581 -  if( zErrMsg ){
   1.582 -    sqlite3Error(db, rc, "%s", zErrMsg);
   1.583 -    sqlite3_free(zErrMsg);
   1.584 -  }else{
   1.585 -    sqlite3Error(db, rc, 0);
   1.586 -  }
   1.587 -
   1.588 -  rc = sqlite3ApiExit(db, rc);
   1.589 -  /* sqlite3ReleaseThreadData(); */
   1.590 -  assert( (rc&db->errMask)==rc );
   1.591 -  return rc;
   1.592 -}
   1.593 -static int sqlite3LockAndPrepare(
   1.594 -  sqlite3 *db,              /* Database handle. */
   1.595 -  const char *zSql,         /* UTF-8 encoded SQL statement. */
   1.596 -  int nBytes,               /* Length of zSql in bytes. */
   1.597 -  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
   1.598 -  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   1.599 -  const char **pzTail       /* OUT: End of parsed string */
   1.600 -){
   1.601 -  int rc;
   1.602 -  if( sqlite3SafetyCheck(db) ){
   1.603 -    return SQLITE_MISUSE;
   1.604 -  }
   1.605 -  sqlite3_mutex_enter(db->mutex);
   1.606 -  sqlite3BtreeEnterAll(db);
   1.607 -  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
   1.608 -  sqlite3BtreeLeaveAll(db);
   1.609 -  sqlite3_mutex_leave(db->mutex);
   1.610 -  return rc;
   1.611 -}
   1.612 -
   1.613 -/*
   1.614 -** Rerun the compilation of a statement after a schema change.
   1.615 -** Return true if the statement was recompiled successfully.
   1.616 -** Return false if there is an error of some kind.
   1.617 -*/
   1.618 -int sqlite3Reprepare(Vdbe *p){
   1.619 -  int rc;
   1.620 -  sqlite3_stmt *pNew;
   1.621 -  const char *zSql;
   1.622 -  sqlite3 *db;
   1.623 -
   1.624 -  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
   1.625 -  zSql = sqlite3VdbeGetSql(p);
   1.626 -  if( zSql==0 ){
   1.627 -    return 0;
   1.628 -  }
   1.629 -  db = sqlite3VdbeDb(p);
   1.630 -  assert( sqlite3_mutex_held(db->mutex) );
   1.631 -  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0);
   1.632 -  if( rc ){
   1.633 -    assert( pNew==0 );
   1.634 -    return 0;
   1.635 -  }else{
   1.636 -    assert( pNew!=0 );
   1.637 -  }
   1.638 -  sqlite3VdbeSwap((Vdbe*)pNew, p);
   1.639 -  sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p);
   1.640 -  sqlite3VdbeResetStepResult((Vdbe*)pNew);
   1.641 -  sqlite3VdbeFinalize((Vdbe*)pNew);
   1.642 -  return 1;
   1.643 -}
   1.644 -
   1.645 -
   1.646 -/*
   1.647 -** Two versions of the official API.  Legacy and new use.  In the legacy
   1.648 -** version, the original SQL text is not saved in the prepared statement
   1.649 -** and so if a schema change occurs, SQLITE_SCHEMA is returned by
   1.650 -** sqlite3_step().  In the new version, the original SQL text is retained
   1.651 -** and the statement is automatically recompiled if an schema change
   1.652 -** occurs.
   1.653 -*/
   1.654 -int sqlite3_prepare(
   1.655 -  sqlite3 *db,              /* Database handle. */
   1.656 -  const char *zSql,         /* UTF-8 encoded SQL statement. */
   1.657 -  int nBytes,               /* Length of zSql in bytes. */
   1.658 -  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   1.659 -  const char **pzTail       /* OUT: End of parsed string */
   1.660 -){
   1.661 -  return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail);
   1.662 -}
   1.663 -int sqlite3_prepare_v2(
   1.664 -  sqlite3 *db,              /* Database handle. */
   1.665 -  const char *zSql,         /* UTF-8 encoded SQL statement. */
   1.666 -  int nBytes,               /* Length of zSql in bytes. */
   1.667 -  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   1.668 -  const char **pzTail       /* OUT: End of parsed string */
   1.669 -){
   1.670 -  return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail);
   1.671 -}
   1.672 -
   1.673 -
   1.674 -#ifndef SQLITE_OMIT_UTF16
   1.675 -/*
   1.676 -** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
   1.677 -*/
   1.678 -static int sqlite3Prepare16(
   1.679 -  sqlite3 *db,              /* Database handle. */ 
   1.680 -  const void *zSql,         /* UTF-8 encoded SQL statement. */
   1.681 -  int nBytes,               /* Length of zSql in bytes. */
   1.682 -  int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
   1.683 -  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   1.684 -  const void **pzTail       /* OUT: End of parsed string */
   1.685 -){
   1.686 -  /* This function currently works by first transforming the UTF-16
   1.687 -  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
   1.688 -  ** tricky bit is figuring out the pointer to return in *pzTail.
   1.689 -  */
   1.690 -  char *zSql8;
   1.691 -  const char *zTail8 = 0;
   1.692 -  int rc = SQLITE_OK;
   1.693 -
   1.694 -  if( sqlite3SafetyCheck(db) ){
   1.695 -    return SQLITE_MISUSE;
   1.696 -  }
   1.697 -  sqlite3_mutex_enter(db->mutex);
   1.698 -  zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
   1.699 -  if( zSql8 ){
   1.700 -    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
   1.701 -  }
   1.702 -
   1.703 -  if( zTail8 && pzTail ){
   1.704 -    /* If sqlite3_prepare returns a tail pointer, we calculate the
   1.705 -    ** equivalent pointer into the UTF-16 string by counting the unicode
   1.706 -    ** characters between zSql8 and zTail8, and then returning a pointer
   1.707 -    ** the same number of characters into the UTF-16 string.
   1.708 -    */
   1.709 -    int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8);
   1.710 -    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
   1.711 -  }
   1.712 -  sqlite3_free(zSql8); 
   1.713 -  rc = sqlite3ApiExit(db, rc);
   1.714 -  sqlite3_mutex_leave(db->mutex);
   1.715 -  return rc;
   1.716 -}
   1.717 -
   1.718 -/*
   1.719 -** Two versions of the official API.  Legacy and new use.  In the legacy
   1.720 -** version, the original SQL text is not saved in the prepared statement
   1.721 -** and so if a schema change occurs, SQLITE_SCHEMA is returned by
   1.722 -** sqlite3_step().  In the new version, the original SQL text is retained
   1.723 -** and the statement is automatically recompiled if an schema change
   1.724 -** occurs.
   1.725 -*/
   1.726 -int sqlite3_prepare16(
   1.727 -  sqlite3 *db,              /* Database handle. */ 
   1.728 -  const void *zSql,         /* UTF-8 encoded SQL statement. */
   1.729 -  int nBytes,               /* Length of zSql in bytes. */
   1.730 -  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   1.731 -  const void **pzTail       /* OUT: End of parsed string */
   1.732 -){
   1.733 -  return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
   1.734 -}
   1.735 -int sqlite3_prepare16_v2(
   1.736 -  sqlite3 *db,              /* Database handle. */ 
   1.737 -  const void *zSql,         /* UTF-8 encoded SQL statement. */
   1.738 -  int nBytes,               /* Length of zSql in bytes. */
   1.739 -  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
   1.740 -  const void **pzTail       /* OUT: End of parsed string */
   1.741 -){
   1.742 -  return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
   1.743 -}
   1.744 -
   1.745 -#endif /* SQLITE_OMIT_UTF16 */