extensions/sqlite/sqlite-source/prepare.c
author David Anderson <dvander@alliedmods.net>
Sun Aug 30 01:54:45 2009 -0700 (2009-08-30)
changeset 2790 6dd851ba15fd
parent 1952 b7aa2b626807
permissions -rw-r--r--
Fixed extension version reporting.
ds@1952
     1
/*
ds@1952
     2
** 2005 May 25
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This file contains the implementation of the sqlite3_prepare()
ds@1952
    13
** interface, and routines that contribute to loading the database schema
ds@1952
    14
** from disk.
ds@1952
    15
**
ds@1952
    16
** $Id$
ds@1952
    17
*/
ds@1952
    18
#include "sqliteInt.h"
ds@1952
    19
#include <ctype.h>
ds@1952
    20
ds@1952
    21
/*
ds@1952
    22
** Fill the InitData structure with an error message that indicates
ds@1952
    23
** that the database is corrupt.
ds@1952
    24
*/
ds@1952
    25
static void corruptSchema(InitData *pData, const char *zExtra){
ds@1952
    26
  if( !pData->db->mallocFailed ){
ds@1952
    27
    sqlite3SetString(pData->pzErrMsg, "malformed database schema",
ds@1952
    28
       zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
ds@1952
    29
  }
ds@1952
    30
  pData->rc = SQLITE_CORRUPT;
ds@1952
    31
}
ds@1952
    32
ds@1952
    33
/*
ds@1952
    34
** This is the callback routine for the code that initializes the
ds@1952
    35
** database.  See sqlite3Init() below for additional information.
ds@1952
    36
** This routine is also called from the OP_ParseSchema opcode of the VDBE.
ds@1952
    37
**
ds@1952
    38
** Each callback contains the following information:
ds@1952
    39
**
ds@1952
    40
**     argv[0] = name of thing being created
ds@1952
    41
**     argv[1] = root page number for table or index. 0 for trigger or view.
ds@1952
    42
**     argv[2] = SQL text for the CREATE statement.
ds@1952
    43
**
ds@1952
    44
*/
ds@1952
    45
int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
ds@1952
    46
  InitData *pData = (InitData*)pInit;
ds@1952
    47
  sqlite3 *db = pData->db;
ds@1952
    48
  int iDb = pData->iDb;
ds@1952
    49
ds@1952
    50
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
    51
  pData->rc = SQLITE_OK;
ds@1952
    52
  DbClearProperty(db, iDb, DB_Empty);
ds@1952
    53
  if( db->mallocFailed ){
ds@1952
    54
    corruptSchema(pData, 0);
ds@1952
    55
    return SQLITE_NOMEM;
ds@1952
    56
  }
ds@1952
    57
ds@1952
    58
  assert( argc==3 );
ds@1952
    59
  if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
ds@1952
    60
  if( argv[1]==0 ){
ds@1952
    61
    corruptSchema(pData, 0);
ds@1952
    62
    return 1;
ds@1952
    63
  }
ds@1952
    64
  assert( iDb>=0 && iDb<db->nDb );
ds@1952
    65
  if( argv[2] && argv[2][0] ){
ds@1952
    66
    /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
ds@1952
    67
    ** But because db->init.busy is set to 1, no VDBE code is generated
ds@1952
    68
    ** or executed.  All the parser does is build the internal data
ds@1952
    69
    ** structures that describe the table, index, or view.
ds@1952
    70
    */
ds@1952
    71
    char *zErr;
ds@1952
    72
    int rc;
ds@1952
    73
    assert( db->init.busy );
ds@1952
    74
    db->init.iDb = iDb;
ds@1952
    75
    db->init.newTnum = atoi(argv[1]);
ds@1952
    76
    rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
ds@1952
    77
    db->init.iDb = 0;
ds@1952
    78
    assert( rc!=SQLITE_OK || zErr==0 );
ds@1952
    79
    if( SQLITE_OK!=rc ){
ds@1952
    80
      pData->rc = rc;
ds@1952
    81
      if( rc==SQLITE_NOMEM ){
ds@1952
    82
        db->mallocFailed = 1;
ds@1952
    83
      }else if( rc!=SQLITE_INTERRUPT ){
ds@1952
    84
        corruptSchema(pData, zErr);
ds@1952
    85
      }
ds@1952
    86
      sqlite3_free(zErr);
ds@1952
    87
      return 1;
ds@1952
    88
    }
ds@1952
    89
  }else{
ds@1952
    90
    /* If the SQL column is blank it means this is an index that
ds@1952
    91
    ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
ds@1952
    92
    ** constraint for a CREATE TABLE.  The index should have already
ds@1952
    93
    ** been created when we processed the CREATE TABLE.  All we have
ds@1952
    94
    ** to do here is record the root page number for that index.
ds@1952
    95
    */
ds@1952
    96
    Index *pIndex;
ds@1952
    97
    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
ds@1952
    98
    if( pIndex==0 || pIndex->tnum!=0 ){
ds@1952
    99
      /* This can occur if there exists an index on a TEMP table which
ds@1952
   100
      ** has the same name as another index on a permanent index.  Since
ds@1952
   101
      ** the permanent table is hidden by the TEMP table, we can also
ds@1952
   102
      ** safely ignore the index on the permanent table.
ds@1952
   103
      */
ds@1952
   104
      /* Do Nothing */;
ds@1952
   105
    }else{
ds@1952
   106
      pIndex->tnum = atoi(argv[1]);
ds@1952
   107
    }
ds@1952
   108
  }
ds@1952
   109
  return 0;
ds@1952
   110
}
ds@1952
   111
ds@1952
   112
/*
ds@1952
   113
** Attempt to read the database schema and initialize internal
ds@1952
   114
** data structures for a single database file.  The index of the
ds@1952
   115
** database file is given by iDb.  iDb==0 is used for the main
ds@1952
   116
** database.  iDb==1 should never be used.  iDb>=2 is used for
ds@1952
   117
** auxiliary databases.  Return one of the SQLITE_ error codes to
ds@1952
   118
** indicate success or failure.
ds@1952
   119
*/
ds@1952
   120
static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
ds@1952
   121
  int rc;
ds@1952
   122
  BtCursor *curMain;
ds@1952
   123
  int size;
ds@1952
   124
  Table *pTab;
ds@1952
   125
  Db *pDb;
ds@1952
   126
  char const *azArg[4];
ds@1952
   127
  int meta[10];
ds@1952
   128
  InitData initData;
ds@1952
   129
  char const *zMasterSchema;
ds@1952
   130
  char const *zMasterName = SCHEMA_TABLE(iDb);
ds@1952
   131
ds@1952
   132
  /*
ds@1952
   133
  ** The master database table has a structure like this
ds@1952
   134
  */
ds@1952
   135
  static const char master_schema[] = 
ds@1952
   136
     "CREATE TABLE sqlite_master(\n"
ds@1952
   137
     "  type text,\n"
ds@1952
   138
     "  name text,\n"
ds@1952
   139
     "  tbl_name text,\n"
ds@1952
   140
     "  rootpage integer,\n"
ds@1952
   141
     "  sql text\n"
ds@1952
   142
     ")"
ds@1952
   143
  ;
ds@1952
   144
#ifndef SQLITE_OMIT_TEMPDB
ds@1952
   145
  static const char temp_master_schema[] = 
ds@1952
   146
     "CREATE TEMP TABLE sqlite_temp_master(\n"
ds@1952
   147
     "  type text,\n"
ds@1952
   148
     "  name text,\n"
ds@1952
   149
     "  tbl_name text,\n"
ds@1952
   150
     "  rootpage integer,\n"
ds@1952
   151
     "  sql text\n"
ds@1952
   152
     ")"
ds@1952
   153
  ;
ds@1952
   154
#else
ds@1952
   155
  #define temp_master_schema 0
ds@1952
   156
#endif
ds@1952
   157
ds@1952
   158
  assert( iDb>=0 && iDb<db->nDb );
ds@1952
   159
  assert( db->aDb[iDb].pSchema );
ds@1952
   160
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
   161
ds@1952
   162
  /* zMasterSchema and zInitScript are set to point at the master schema
ds@1952
   163
  ** and initialisation script appropriate for the database being
ds@1952
   164
  ** initialised. zMasterName is the name of the master table.
ds@1952
   165
  */
ds@1952
   166
  if( !OMIT_TEMPDB && iDb==1 ){
ds@1952
   167
    zMasterSchema = temp_master_schema;
ds@1952
   168
  }else{
ds@1952
   169
    zMasterSchema = master_schema;
ds@1952
   170
  }
ds@1952
   171
  zMasterName = SCHEMA_TABLE(iDb);
ds@1952
   172
ds@1952
   173
  /* Construct the schema tables.  */
ds@1952
   174
  sqlite3SafetyOff(db);
ds@1952
   175
  azArg[0] = zMasterName;
ds@1952
   176
  azArg[1] = "1";
ds@1952
   177
  azArg[2] = zMasterSchema;
ds@1952
   178
  azArg[3] = 0;
ds@1952
   179
  initData.db = db;
ds@1952
   180
  initData.iDb = iDb;
ds@1952
   181
  initData.pzErrMsg = pzErrMsg;
ds@1952
   182
  rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
ds@1952
   183
  if( rc ){
ds@1952
   184
    sqlite3SafetyOn(db);
ds@1952
   185
    rc = initData.rc;
ds@1952
   186
    goto error_out;
ds@1952
   187
  }
ds@1952
   188
  pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
ds@1952
   189
  if( pTab ){
ds@1952
   190
    pTab->readOnly = 1;
ds@1952
   191
  }
ds@1952
   192
  sqlite3SafetyOn(db);
ds@1952
   193
ds@1952
   194
  /* Create a cursor to hold the database open
ds@1952
   195
  */
ds@1952
   196
  pDb = &db->aDb[iDb];
ds@1952
   197
  if( pDb->pBt==0 ){
ds@1952
   198
    if( !OMIT_TEMPDB && iDb==1 ){
ds@1952
   199
      DbSetProperty(db, 1, DB_SchemaLoaded);
ds@1952
   200
    }
ds@1952
   201
    return SQLITE_OK;
ds@1952
   202
  }
ds@1952
   203
  sqlite3BtreeEnter(pDb->pBt);
ds@1952
   204
  rc = sqlite3BtreeCursor(pDb->pBt, MASTER_ROOT, 0, 0, 0, &curMain);
ds@1952
   205
  if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
ds@1952
   206
    sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
ds@1952
   207
    sqlite3BtreeLeave(pDb->pBt);
ds@1952
   208
    goto error_out;
ds@1952
   209
  }
ds@1952
   210
ds@1952
   211
  /* Get the database meta information.
ds@1952
   212
  **
ds@1952
   213
  ** Meta values are as follows:
ds@1952
   214
  **    meta[0]   Schema cookie.  Changes with each schema change.
ds@1952
   215
  **    meta[1]   File format of schema layer.
ds@1952
   216
  **    meta[2]   Size of the page cache.
ds@1952
   217
  **    meta[3]   Use freelist if 0.  Autovacuum if greater than zero.
ds@1952
   218
  **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
ds@1952
   219
  **    meta[5]   The user cookie. Used by the application.
ds@1952
   220
  **    meta[6]   Incremental-vacuum flag.
ds@1952
   221
  **    meta[7]
ds@1952
   222
  **    meta[8]
ds@1952
   223
  **    meta[9]
ds@1952
   224
  **
ds@1952
   225
  ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
ds@1952
   226
  ** the possible values of meta[4].
ds@1952
   227
  */
ds@1952
   228
  if( rc==SQLITE_OK ){
ds@1952
   229
    int i;
ds@1952
   230
    for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
ds@1952
   231
      rc = sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
ds@1952
   232
    }
ds@1952
   233
    if( rc ){
ds@1952
   234
      sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
ds@1952
   235
      sqlite3BtreeCloseCursor(curMain);
ds@1952
   236
      sqlite3BtreeLeave(pDb->pBt);
ds@1952
   237
      goto error_out;
ds@1952
   238
    }
ds@1952
   239
  }else{
ds@1952
   240
    memset(meta, 0, sizeof(meta));
ds@1952
   241
  }
ds@1952
   242
  pDb->pSchema->schema_cookie = meta[0];
ds@1952
   243
ds@1952
   244
  /* If opening a non-empty database, check the text encoding. For the
ds@1952
   245
  ** main database, set sqlite3.enc to the encoding of the main database.
ds@1952
   246
  ** For an attached db, it is an error if the encoding is not the same
ds@1952
   247
  ** as sqlite3.enc.
ds@1952
   248
  */
ds@1952
   249
  if( meta[4] ){  /* text encoding */
ds@1952
   250
    if( iDb==0 ){
ds@1952
   251
      /* If opening the main database, set ENC(db). */
ds@1952
   252
      ENC(db) = (u8)meta[4];
ds@1952
   253
      db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0);
ds@1952
   254
    }else{
ds@1952
   255
      /* If opening an attached database, the encoding much match ENC(db) */
ds@1952
   256
      if( meta[4]!=ENC(db) ){
ds@1952
   257
        sqlite3BtreeCloseCursor(curMain);
ds@1952
   258
        sqlite3SetString(pzErrMsg, "attached databases must use the same"
ds@1952
   259
            " text encoding as main database", (char*)0);
ds@1952
   260
        sqlite3BtreeLeave(pDb->pBt);
ds@1952
   261
        return SQLITE_ERROR;
ds@1952
   262
      }
ds@1952
   263
    }
ds@1952
   264
  }else{
ds@1952
   265
    DbSetProperty(db, iDb, DB_Empty);
ds@1952
   266
  }
ds@1952
   267
  pDb->pSchema->enc = ENC(db);
ds@1952
   268
ds@1952
   269
  size = meta[2];
ds@1952
   270
  if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
ds@1952
   271
  pDb->pSchema->cache_size = size;
ds@1952
   272
  sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
ds@1952
   273
ds@1952
   274
  /*
ds@1952
   275
  ** file_format==1    Version 3.0.0.
ds@1952
   276
  ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
ds@1952
   277
  ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
ds@1952
   278
  ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
ds@1952
   279
  */
ds@1952
   280
  pDb->pSchema->file_format = meta[1];
ds@1952
   281
  if( pDb->pSchema->file_format==0 ){
ds@1952
   282
    pDb->pSchema->file_format = 1;
ds@1952
   283
  }
ds@1952
   284
  if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
ds@1952
   285
    sqlite3BtreeCloseCursor(curMain);
ds@1952
   286
    sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
ds@1952
   287
    sqlite3BtreeLeave(pDb->pBt);
ds@1952
   288
    return SQLITE_ERROR;
ds@1952
   289
  }
ds@1952
   290
ds@1952
   291
ds@1952
   292
  /* Read the schema information out of the schema tables
ds@1952
   293
  */
ds@1952
   294
  assert( db->init.busy );
ds@1952
   295
  if( rc==SQLITE_EMPTY ){
ds@1952
   296
    /* For an empty database, there is nothing to read */
ds@1952
   297
    rc = SQLITE_OK;
ds@1952
   298
  }else{
ds@1952
   299
    char *zSql;
ds@1952
   300
    zSql = sqlite3MPrintf(db, 
ds@1952
   301
        "SELECT name, rootpage, sql FROM '%q'.%s",
ds@1952
   302
        db->aDb[iDb].zName, zMasterName);
ds@1952
   303
    sqlite3SafetyOff(db);
ds@1952
   304
    rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
ds@1952
   305
    if( rc==SQLITE_ABORT ) rc = initData.rc;
ds@1952
   306
    sqlite3SafetyOn(db);
ds@1952
   307
    sqlite3_free(zSql);
ds@1952
   308
#ifndef SQLITE_OMIT_ANALYZE
ds@1952
   309
    if( rc==SQLITE_OK ){
ds@1952
   310
      sqlite3AnalysisLoad(db, iDb);
ds@1952
   311
    }
ds@1952
   312
#endif
ds@1952
   313
    sqlite3BtreeCloseCursor(curMain);
ds@1952
   314
  }
ds@1952
   315
  if( db->mallocFailed ){
ds@1952
   316
    /* sqlite3SetString(pzErrMsg, "out of memory", (char*)0); */
ds@1952
   317
    rc = SQLITE_NOMEM;
ds@1952
   318
    sqlite3ResetInternalSchema(db, 0);
ds@1952
   319
  }
ds@1952
   320
  if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
ds@1952
   321
    /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
ds@1952
   322
    ** the schema loaded, even if errors occured. In this situation the 
ds@1952
   323
    ** current sqlite3_prepare() operation will fail, but the following one
ds@1952
   324
    ** will attempt to compile the supplied statement against whatever subset
ds@1952
   325
    ** of the schema was loaded before the error occured. The primary
ds@1952
   326
    ** purpose of this is to allow access to the sqlite_master table
ds@1952
   327
    ** even when it's contents have been corrupted.
ds@1952
   328
    */
ds@1952
   329
    DbSetProperty(db, iDb, DB_SchemaLoaded);
ds@1952
   330
    rc = SQLITE_OK;
ds@1952
   331
  }
ds@1952
   332
  sqlite3BtreeLeave(pDb->pBt);
ds@1952
   333
ds@1952
   334
error_out:
ds@1952
   335
  if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
ds@1952
   336
    db->mallocFailed = 1;
ds@1952
   337
  }
ds@1952
   338
  return rc;
ds@1952
   339
}
ds@1952
   340
ds@1952
   341
/*
ds@1952
   342
** Initialize all database files - the main database file, the file
ds@1952
   343
** used to store temporary tables, and any additional database files
ds@1952
   344
** created using ATTACH statements.  Return a success code.  If an
ds@1952
   345
** error occurs, write an error message into *pzErrMsg.
ds@1952
   346
**
ds@1952
   347
** After a database is initialized, the DB_SchemaLoaded bit is set
ds@1952
   348
** bit is set in the flags field of the Db structure. If the database
ds@1952
   349
** file was of zero-length, then the DB_Empty flag is also set.
ds@1952
   350
*/
ds@1952
   351
int sqlite3Init(sqlite3 *db, char **pzErrMsg){
ds@1952
   352
  int i, rc;
ds@1952
   353
  int commit_internal = !(db->flags&SQLITE_InternChanges);
ds@1952
   354
  
ds@1952
   355
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
   356
  if( db->init.busy ) return SQLITE_OK;
ds@1952
   357
  rc = SQLITE_OK;
ds@1952
   358
  db->init.busy = 1;
ds@1952
   359
  for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
ds@1952
   360
    if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
ds@1952
   361
    rc = sqlite3InitOne(db, i, pzErrMsg);
ds@1952
   362
    if( rc ){
ds@1952
   363
      sqlite3ResetInternalSchema(db, i);
ds@1952
   364
    }
ds@1952
   365
  }
ds@1952
   366
ds@1952
   367
  /* Once all the other databases have been initialised, load the schema
ds@1952
   368
  ** for the TEMP database. This is loaded last, as the TEMP database
ds@1952
   369
  ** schema may contain references to objects in other databases.
ds@1952
   370
  */
ds@1952
   371
#ifndef SQLITE_OMIT_TEMPDB
ds@1952
   372
  if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
ds@1952
   373
    rc = sqlite3InitOne(db, 1, pzErrMsg);
ds@1952
   374
    if( rc ){
ds@1952
   375
      sqlite3ResetInternalSchema(db, 1);
ds@1952
   376
    }
ds@1952
   377
  }
ds@1952
   378
#endif
ds@1952
   379
ds@1952
   380
  db->init.busy = 0;
ds@1952
   381
  if( rc==SQLITE_OK && commit_internal ){
ds@1952
   382
    sqlite3CommitInternalChanges(db);
ds@1952
   383
  }
ds@1952
   384
ds@1952
   385
  return rc; 
ds@1952
   386
}
ds@1952
   387
ds@1952
   388
/*
ds@1952
   389
** This routine is a no-op if the database schema is already initialised.
ds@1952
   390
** Otherwise, the schema is loaded. An error code is returned.
ds@1952
   391
*/
ds@1952
   392
int sqlite3ReadSchema(Parse *pParse){
ds@1952
   393
  int rc = SQLITE_OK;
ds@1952
   394
  sqlite3 *db = pParse->db;
ds@1952
   395
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
   396
  if( !db->init.busy ){
ds@1952
   397
    rc = sqlite3Init(db, &pParse->zErrMsg);
ds@1952
   398
  }
ds@1952
   399
  if( rc!=SQLITE_OK ){
ds@1952
   400
    pParse->rc = rc;
ds@1952
   401
    pParse->nErr++;
ds@1952
   402
  }
ds@1952
   403
  return rc;
ds@1952
   404
}
ds@1952
   405
ds@1952
   406
ds@1952
   407
/*
ds@1952
   408
** Check schema cookies in all databases.  If any cookie is out
ds@1952
   409
** of date, return 0.  If all schema cookies are current, return 1.
ds@1952
   410
*/
ds@1952
   411
static int schemaIsValid(sqlite3 *db){
ds@1952
   412
  int iDb;
ds@1952
   413
  int rc;
ds@1952
   414
  BtCursor *curTemp;
ds@1952
   415
  int cookie;
ds@1952
   416
  int allOk = 1;
ds@1952
   417
ds@1952
   418
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
   419
  for(iDb=0; allOk && iDb<db->nDb; iDb++){
ds@1952
   420
    Btree *pBt;
ds@1952
   421
    pBt = db->aDb[iDb].pBt;
ds@1952
   422
    if( pBt==0 ) continue;
ds@1952
   423
    rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
ds@1952
   424
    if( rc==SQLITE_OK ){
ds@1952
   425
      rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
ds@1952
   426
      if( rc==SQLITE_OK && cookie!=db->aDb[iDb].pSchema->schema_cookie ){
ds@1952
   427
        allOk = 0;
ds@1952
   428
      }
ds@1952
   429
      sqlite3BtreeCloseCursor(curTemp);
ds@1952
   430
    }
ds@1952
   431
    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
ds@1952
   432
      db->mallocFailed = 1;
ds@1952
   433
    }
ds@1952
   434
  }
ds@1952
   435
  return allOk;
ds@1952
   436
}
ds@1952
   437
ds@1952
   438
/*
ds@1952
   439
** Convert a schema pointer into the iDb index that indicates
ds@1952
   440
** which database file in db->aDb[] the schema refers to.
ds@1952
   441
**
ds@1952
   442
** If the same database is attached more than once, the first
ds@1952
   443
** attached database is returned.
ds@1952
   444
*/
ds@1952
   445
int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
ds@1952
   446
  int i = -1000000;
ds@1952
   447
ds@1952
   448
  /* If pSchema is NULL, then return -1000000. This happens when code in 
ds@1952
   449
  ** expr.c is trying to resolve a reference to a transient table (i.e. one
ds@1952
   450
  ** created by a sub-select). In this case the return value of this 
ds@1952
   451
  ** function should never be used.
ds@1952
   452
  **
ds@1952
   453
  ** We return -1000000 instead of the more usual -1 simply because using
ds@1952
   454
  ** -1000000 as incorrectly using -1000000 index into db->aDb[] is much 
ds@1952
   455
  ** more likely to cause a segfault than -1 (of course there are assert()
ds@1952
   456
  ** statements too, but it never hurts to play the odds).
ds@1952
   457
  */
ds@1952
   458
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
   459
  if( pSchema ){
ds@1952
   460
    for(i=0; i<db->nDb; i++){
ds@1952
   461
      if( db->aDb[i].pSchema==pSchema ){
ds@1952
   462
        break;
ds@1952
   463
      }
ds@1952
   464
    }
ds@1952
   465
    assert( i>=0 &&i>=0 &&  i<db->nDb );
ds@1952
   466
  }
ds@1952
   467
  return i;
ds@1952
   468
}
ds@1952
   469
ds@1952
   470
/*
ds@1952
   471
** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
ds@1952
   472
*/
ds@1952
   473
int sqlite3Prepare(
ds@1952
   474
  sqlite3 *db,              /* Database handle. */
ds@1952
   475
  const char *zSql,         /* UTF-8 encoded SQL statement. */
ds@1952
   476
  int nBytes,               /* Length of zSql in bytes. */
ds@1952
   477
  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
ds@1952
   478
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
ds@1952
   479
  const char **pzTail       /* OUT: End of parsed string */
ds@1952
   480
){
ds@1952
   481
  Parse sParse;
ds@1952
   482
  char *zErrMsg = 0;
ds@1952
   483
  int rc = SQLITE_OK;
ds@1952
   484
  int i;
ds@1952
   485
ds@1952
   486
  assert( ppStmt );
ds@1952
   487
  *ppStmt = 0;
ds@1952
   488
  if( sqlite3SafetyOn(db) ){
ds@1952
   489
    return SQLITE_MISUSE;
ds@1952
   490
  }
ds@1952
   491
  assert( !db->mallocFailed );
ds@1952
   492
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
   493
ds@1952
   494
  /* If any attached database schemas are locked, do not proceed with
ds@1952
   495
  ** compilation. Instead return SQLITE_LOCKED immediately.
ds@1952
   496
  */
ds@1952
   497
  for(i=0; i<db->nDb; i++) {
ds@1952
   498
    Btree *pBt = db->aDb[i].pBt;
ds@1952
   499
    if( pBt ){
ds@1952
   500
      int rc;
ds@1952
   501
      rc = sqlite3BtreeSchemaLocked(pBt);
ds@1952
   502
      if( rc ){
ds@1952
   503
        const char *zDb = db->aDb[i].zName;
ds@1952
   504
        sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb);
ds@1952
   505
        sqlite3SafetyOff(db);
ds@1952
   506
        return SQLITE_LOCKED;
ds@1952
   507
      }
ds@1952
   508
    }
ds@1952
   509
  }
ds@1952
   510
  
ds@1952
   511
  memset(&sParse, 0, sizeof(sParse));
ds@1952
   512
  sParse.db = db;
ds@1952
   513
  if( nBytes>=0 && zSql[nBytes]!=0 ){
ds@1952
   514
    char *zSqlCopy;
ds@1952
   515
    if( nBytes>SQLITE_MAX_SQL_LENGTH ){
ds@1952
   516
      return SQLITE_TOOBIG;
ds@1952
   517
    }
ds@1952
   518
    zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
ds@1952
   519
    if( zSqlCopy ){
ds@1952
   520
      sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
ds@1952
   521
      sqlite3_free(zSqlCopy);
ds@1952
   522
    }
ds@1952
   523
    sParse.zTail = &zSql[nBytes];
ds@1952
   524
  }else{
ds@1952
   525
    sqlite3RunParser(&sParse, zSql, &zErrMsg);
ds@1952
   526
  }
ds@1952
   527
ds@1952
   528
  if( db->mallocFailed ){
ds@1952
   529
    sParse.rc = SQLITE_NOMEM;
ds@1952
   530
  }
ds@1952
   531
  if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
ds@1952
   532
  if( sParse.checkSchema && !schemaIsValid(db) ){
ds@1952
   533
    sParse.rc = SQLITE_SCHEMA;
ds@1952
   534
  }
ds@1952
   535
  if( sParse.rc==SQLITE_SCHEMA ){
ds@1952
   536
    sqlite3ResetInternalSchema(db, 0);
ds@1952
   537
  }
ds@1952
   538
  if( db->mallocFailed ){
ds@1952
   539
    sParse.rc = SQLITE_NOMEM;
ds@1952
   540
  }
ds@1952
   541
  if( pzTail ){
ds@1952
   542
    *pzTail = sParse.zTail;
ds@1952
   543
  }
ds@1952
   544
  rc = sParse.rc;
ds@1952
   545
ds@1952
   546
#ifndef SQLITE_OMIT_EXPLAIN
ds@1952
   547
  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
ds@1952
   548
    if( sParse.explain==2 ){
ds@1952
   549
      sqlite3VdbeSetNumCols(sParse.pVdbe, 3);
ds@1952
   550
      sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC);
ds@1952
   551
      sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC);
ds@1952
   552
      sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC);
ds@1952
   553
    }else{
ds@1952
   554
      sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
ds@1952
   555
      sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC);
ds@1952
   556
      sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC);
ds@1952
   557
      sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC);
ds@1952
   558
      sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC);
ds@1952
   559
      sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC);
ds@1952
   560
    }
ds@1952
   561
  }
ds@1952
   562
#endif
ds@1952
   563
ds@1952
   564
  if( sqlite3SafetyOff(db) ){
ds@1952
   565
    rc = SQLITE_MISUSE;
ds@1952
   566
  }
ds@1952
   567
ds@1952
   568
  if( saveSqlFlag ){
ds@1952
   569
    sqlite3VdbeSetSql(sParse.pVdbe, zSql, sParse.zTail - zSql);
ds@1952
   570
  }
ds@1952
   571
  if( rc!=SQLITE_OK || db->mallocFailed ){
ds@1952
   572
    sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
ds@1952
   573
    assert(!(*ppStmt));
ds@1952
   574
  }else{
ds@1952
   575
    *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
ds@1952
   576
  }
ds@1952
   577
ds@1952
   578
  if( zErrMsg ){
ds@1952
   579
    sqlite3Error(db, rc, "%s", zErrMsg);
ds@1952
   580
    sqlite3_free(zErrMsg);
ds@1952
   581
  }else{
ds@1952
   582
    sqlite3Error(db, rc, 0);
ds@1952
   583
  }
ds@1952
   584
ds@1952
   585
  rc = sqlite3ApiExit(db, rc);
ds@1952
   586
  /* sqlite3ReleaseThreadData(); */
ds@1952
   587
  assert( (rc&db->errMask)==rc );
ds@1952
   588
  return rc;
ds@1952
   589
}
ds@1952
   590
static int sqlite3LockAndPrepare(
ds@1952
   591
  sqlite3 *db,              /* Database handle. */
ds@1952
   592
  const char *zSql,         /* UTF-8 encoded SQL statement. */
ds@1952
   593
  int nBytes,               /* Length of zSql in bytes. */
ds@1952
   594
  int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
ds@1952
   595
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
ds@1952
   596
  const char **pzTail       /* OUT: End of parsed string */
ds@1952
   597
){
ds@1952
   598
  int rc;
ds@1952
   599
  if( sqlite3SafetyCheck(db) ){
ds@1952
   600
    return SQLITE_MISUSE;
ds@1952
   601
  }
ds@1952
   602
  sqlite3_mutex_enter(db->mutex);
ds@1952
   603
  sqlite3BtreeEnterAll(db);
ds@1952
   604
  rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, ppStmt, pzTail);
ds@1952
   605
  sqlite3BtreeLeaveAll(db);
ds@1952
   606
  sqlite3_mutex_leave(db->mutex);
ds@1952
   607
  return rc;
ds@1952
   608
}
ds@1952
   609
ds@1952
   610
/*
ds@1952
   611
** Rerun the compilation of a statement after a schema change.
ds@1952
   612
** Return true if the statement was recompiled successfully.
ds@1952
   613
** Return false if there is an error of some kind.
ds@1952
   614
*/
ds@1952
   615
int sqlite3Reprepare(Vdbe *p){
ds@1952
   616
  int rc;
ds@1952
   617
  sqlite3_stmt *pNew;
ds@1952
   618
  const char *zSql;
ds@1952
   619
  sqlite3 *db;
ds@1952
   620
ds@1952
   621
  assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
ds@1952
   622
  zSql = sqlite3VdbeGetSql(p);
ds@1952
   623
  if( zSql==0 ){
ds@1952
   624
    return 0;
ds@1952
   625
  }
ds@1952
   626
  db = sqlite3VdbeDb(p);
ds@1952
   627
  assert( sqlite3_mutex_held(db->mutex) );
ds@1952
   628
  rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0);
ds@1952
   629
  if( rc ){
ds@1952
   630
    assert( pNew==0 );
ds@1952
   631
    return 0;
ds@1952
   632
  }else{
ds@1952
   633
    assert( pNew!=0 );
ds@1952
   634
  }
ds@1952
   635
  sqlite3VdbeSwap((Vdbe*)pNew, p);
ds@1952
   636
  sqlite3_transfer_bindings(pNew, (sqlite3_stmt*)p);
ds@1952
   637
  sqlite3VdbeResetStepResult((Vdbe*)pNew);
ds@1952
   638
  sqlite3VdbeFinalize((Vdbe*)pNew);
ds@1952
   639
  return 1;
ds@1952
   640
}
ds@1952
   641
ds@1952
   642
ds@1952
   643
/*
ds@1952
   644
** Two versions of the official API.  Legacy and new use.  In the legacy
ds@1952
   645
** version, the original SQL text is not saved in the prepared statement
ds@1952
   646
** and so if a schema change occurs, SQLITE_SCHEMA is returned by
ds@1952
   647
** sqlite3_step().  In the new version, the original SQL text is retained
ds@1952
   648
** and the statement is automatically recompiled if an schema change
ds@1952
   649
** occurs.
ds@1952
   650
*/
ds@1952
   651
int sqlite3_prepare(
ds@1952
   652
  sqlite3 *db,              /* Database handle. */
ds@1952
   653
  const char *zSql,         /* UTF-8 encoded SQL statement. */
ds@1952
   654
  int nBytes,               /* Length of zSql in bytes. */
ds@1952
   655
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
ds@1952
   656
  const char **pzTail       /* OUT: End of parsed string */
ds@1952
   657
){
ds@1952
   658
  return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail);
ds@1952
   659
}
ds@1952
   660
int sqlite3_prepare_v2(
ds@1952
   661
  sqlite3 *db,              /* Database handle. */
ds@1952
   662
  const char *zSql,         /* UTF-8 encoded SQL statement. */
ds@1952
   663
  int nBytes,               /* Length of zSql in bytes. */
ds@1952
   664
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
ds@1952
   665
  const char **pzTail       /* OUT: End of parsed string */
ds@1952
   666
){
ds@1952
   667
  return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail);
ds@1952
   668
}
ds@1952
   669
ds@1952
   670
ds@1952
   671
#ifndef SQLITE_OMIT_UTF16
ds@1952
   672
/*
ds@1952
   673
** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
ds@1952
   674
*/
ds@1952
   675
static int sqlite3Prepare16(
ds@1952
   676
  sqlite3 *db,              /* Database handle. */ 
ds@1952
   677
  const void *zSql,         /* UTF-8 encoded SQL statement. */
ds@1952
   678
  int nBytes,               /* Length of zSql in bytes. */
ds@1952
   679
  int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
ds@1952
   680
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
ds@1952
   681
  const void **pzTail       /* OUT: End of parsed string */
ds@1952
   682
){
ds@1952
   683
  /* This function currently works by first transforming the UTF-16
ds@1952
   684
  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
ds@1952
   685
  ** tricky bit is figuring out the pointer to return in *pzTail.
ds@1952
   686
  */
ds@1952
   687
  char *zSql8;
ds@1952
   688
  const char *zTail8 = 0;
ds@1952
   689
  int rc = SQLITE_OK;
ds@1952
   690
ds@1952
   691
  if( sqlite3SafetyCheck(db) ){
ds@1952
   692
    return SQLITE_MISUSE;
ds@1952
   693
  }
ds@1952
   694
  sqlite3_mutex_enter(db->mutex);
ds@1952
   695
  zSql8 = sqlite3Utf16to8(db, zSql, nBytes);
ds@1952
   696
  if( zSql8 ){
ds@1952
   697
    rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, ppStmt, &zTail8);
ds@1952
   698
  }
ds@1952
   699
ds@1952
   700
  if( zTail8 && pzTail ){
ds@1952
   701
    /* If sqlite3_prepare returns a tail pointer, we calculate the
ds@1952
   702
    ** equivalent pointer into the UTF-16 string by counting the unicode
ds@1952
   703
    ** characters between zSql8 and zTail8, and then returning a pointer
ds@1952
   704
    ** the same number of characters into the UTF-16 string.
ds@1952
   705
    */
ds@1952
   706
    int chars_parsed = sqlite3Utf8CharLen(zSql8, zTail8-zSql8);
ds@1952
   707
    *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
ds@1952
   708
  }
ds@1952
   709
  sqlite3_free(zSql8); 
ds@1952
   710
  rc = sqlite3ApiExit(db, rc);
ds@1952
   711
  sqlite3_mutex_leave(db->mutex);
ds@1952
   712
  return rc;
ds@1952
   713
}
ds@1952
   714
ds@1952
   715
/*
ds@1952
   716
** Two versions of the official API.  Legacy and new use.  In the legacy
ds@1952
   717
** version, the original SQL text is not saved in the prepared statement
ds@1952
   718
** and so if a schema change occurs, SQLITE_SCHEMA is returned by
ds@1952
   719
** sqlite3_step().  In the new version, the original SQL text is retained
ds@1952
   720
** and the statement is automatically recompiled if an schema change
ds@1952
   721
** occurs.
ds@1952
   722
*/
ds@1952
   723
int sqlite3_prepare16(
ds@1952
   724
  sqlite3 *db,              /* Database handle. */ 
ds@1952
   725
  const void *zSql,         /* UTF-8 encoded SQL statement. */
ds@1952
   726
  int nBytes,               /* Length of zSql in bytes. */
ds@1952
   727
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
ds@1952
   728
  const void **pzTail       /* OUT: End of parsed string */
ds@1952
   729
){
ds@1952
   730
  return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
ds@1952
   731
}
ds@1952
   732
int sqlite3_prepare16_v2(
ds@1952
   733
  sqlite3 *db,              /* Database handle. */ 
ds@1952
   734
  const void *zSql,         /* UTF-8 encoded SQL statement. */
ds@1952
   735
  int nBytes,               /* Length of zSql in bytes. */
ds@1952
   736
  sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
ds@1952
   737
  const void **pzTail       /* OUT: End of parsed string */
ds@1952
   738
){
ds@1952
   739
  return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
ds@1952
   740
}
ds@1952
   741
ds@1952
   742
#endif /* SQLITE_OMIT_UTF16 */