extensions/sqlite/sqlite-source/attach.c
author Asher Baker <asherkin@gmail.com>
Thu Apr 26 16:58:18 2012 +0100 (2012-04-26)
changeset 3488 bee90c8ef6c3
parent 1952 b7aa2b626807
permissions -rw-r--r--
Fixed client serials not being unique on Windows (bug 5285, r=psychonic).
ds@1952
     1
/*
ds@1952
     2
** 2003 April 6
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 code used to implement the ATTACH and DETACH commands.
ds@1952
    13
**
ds@1952
    14
** $Id$
ds@1952
    15
*/
ds@1952
    16
#include "sqliteInt.h"
ds@1952
    17
ds@1952
    18
#ifndef SQLITE_OMIT_ATTACH
ds@1952
    19
/*
ds@1952
    20
** Resolve an expression that was part of an ATTACH or DETACH statement. This
ds@1952
    21
** is slightly different from resolving a normal SQL expression, because simple
ds@1952
    22
** identifiers are treated as strings, not possible column names or aliases.
ds@1952
    23
**
ds@1952
    24
** i.e. if the parser sees:
ds@1952
    25
**
ds@1952
    26
**     ATTACH DATABASE abc AS def
ds@1952
    27
**
ds@1952
    28
** it treats the two expressions as literal strings 'abc' and 'def' instead of
ds@1952
    29
** looking for columns of the same name.
ds@1952
    30
**
ds@1952
    31
** This only applies to the root node of pExpr, so the statement:
ds@1952
    32
**
ds@1952
    33
**     ATTACH DATABASE abc||def AS 'db2'
ds@1952
    34
**
ds@1952
    35
** will fail because neither abc or def can be resolved.
ds@1952
    36
*/
ds@1952
    37
static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
ds@1952
    38
{
ds@1952
    39
  int rc = SQLITE_OK;
ds@1952
    40
  if( pExpr ){
ds@1952
    41
    if( pExpr->op!=TK_ID ){
ds@1952
    42
      rc = sqlite3ExprResolveNames(pName, pExpr);
ds@1952
    43
      if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
ds@1952
    44
        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span);
ds@1952
    45
        return SQLITE_ERROR;
ds@1952
    46
      }
ds@1952
    47
    }else{
ds@1952
    48
      pExpr->op = TK_STRING;
ds@1952
    49
    }
ds@1952
    50
  }
ds@1952
    51
  return rc;
ds@1952
    52
}
ds@1952
    53
ds@1952
    54
/*
ds@1952
    55
** An SQL user-function registered to do the work of an ATTACH statement. The
ds@1952
    56
** three arguments to the function come directly from an attach statement:
ds@1952
    57
**
ds@1952
    58
**     ATTACH DATABASE x AS y KEY z
ds@1952
    59
**
ds@1952
    60
**     SELECT sqlite_attach(x, y, z)
ds@1952
    61
**
ds@1952
    62
** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
ds@1952
    63
** third argument.
ds@1952
    64
*/
ds@1952
    65
static void attachFunc(
ds@1952
    66
  sqlite3_context *context,
ds@1952
    67
  int argc,
ds@1952
    68
  sqlite3_value **argv
ds@1952
    69
){
ds@1952
    70
  int i;
ds@1952
    71
  int rc = 0;
ds@1952
    72
  sqlite3 *db = sqlite3_user_data(context);
ds@1952
    73
  const char *zName;
ds@1952
    74
  const char *zFile;
ds@1952
    75
  Db *aNew;
ds@1952
    76
  char *zErrDyn = 0;
ds@1952
    77
  char zErr[128];
ds@1952
    78
ds@1952
    79
  zFile = (const char *)sqlite3_value_text(argv[0]);
ds@1952
    80
  zName = (const char *)sqlite3_value_text(argv[1]);
ds@1952
    81
  if( zFile==0 ) zFile = "";
ds@1952
    82
  if( zName==0 ) zName = "";
ds@1952
    83
ds@1952
    84
  /* Check for the following errors:
ds@1952
    85
  **
ds@1952
    86
  **     * Too many attached databases,
ds@1952
    87
  **     * Transaction currently open
ds@1952
    88
  **     * Specified database name already being used.
ds@1952
    89
  */
ds@1952
    90
  if( db->nDb>=SQLITE_MAX_ATTACHED+2 ){
ds@1952
    91
    sqlite3_snprintf(
ds@1952
    92
      sizeof(zErr), zErr, "too many attached databases - max %d", 
ds@1952
    93
      SQLITE_MAX_ATTACHED
ds@1952
    94
    );
ds@1952
    95
    goto attach_error;
ds@1952
    96
  }
ds@1952
    97
  if( !db->autoCommit ){
ds@1952
    98
    sqlite3_snprintf(sizeof(zErr), zErr,
ds@1952
    99
                     "cannot ATTACH database within transaction");
ds@1952
   100
    goto attach_error;
ds@1952
   101
  }
ds@1952
   102
  for(i=0; i<db->nDb; i++){
ds@1952
   103
    char *z = db->aDb[i].zName;
ds@1952
   104
    if( z && zName && sqlite3StrICmp(z, zName)==0 ){
ds@1952
   105
      sqlite3_snprintf(sizeof(zErr), zErr, 
ds@1952
   106
                       "database %s is already in use", zName);
ds@1952
   107
      goto attach_error;
ds@1952
   108
    }
ds@1952
   109
  }
ds@1952
   110
ds@1952
   111
  /* Allocate the new entry in the db->aDb[] array and initialise the schema
ds@1952
   112
  ** hash tables.
ds@1952
   113
  */
ds@1952
   114
  if( db->aDb==db->aDbStatic ){
ds@1952
   115
    aNew = sqlite3_malloc( sizeof(db->aDb[0])*3 );
ds@1952
   116
    if( aNew==0 ){
ds@1952
   117
      db->mallocFailed = 1;
ds@1952
   118
      return;
ds@1952
   119
    }
ds@1952
   120
    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
ds@1952
   121
  }else{
ds@1952
   122
    aNew = sqlite3_realloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
ds@1952
   123
    if( aNew==0 ){
ds@1952
   124
      db->mallocFailed = 1;
ds@1952
   125
      return;
ds@1952
   126
    } 
ds@1952
   127
  }
ds@1952
   128
  db->aDb = aNew;
ds@1952
   129
  aNew = &db->aDb[db->nDb++];
ds@1952
   130
  memset(aNew, 0, sizeof(*aNew));
ds@1952
   131
ds@1952
   132
  /* Open the database file. If the btree is successfully opened, use
ds@1952
   133
  ** it to obtain the database schema. At this point the schema may
ds@1952
   134
  ** or may not be initialised.
ds@1952
   135
  */
ds@1952
   136
  rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE,
ds@1952
   137
                           db->openFlags | SQLITE_OPEN_MAIN_DB,
ds@1952
   138
                           &aNew->pBt);
ds@1952
   139
  if( rc==SQLITE_OK ){
ds@1952
   140
    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
ds@1952
   141
    if( !aNew->pSchema ){
ds@1952
   142
      rc = SQLITE_NOMEM;
ds@1952
   143
    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
ds@1952
   144
      sqlite3_snprintf(sizeof(zErr), zErr, 
ds@1952
   145
        "attached databases must use the same text encoding as main database");
ds@1952
   146
      goto attach_error;
ds@1952
   147
    }
ds@1952
   148
    sqlite3PagerLockingMode(sqlite3BtreePager(aNew->pBt), db->dfltLockMode);
ds@1952
   149
  }
ds@1952
   150
  aNew->zName = sqlite3DbStrDup(db, zName);
ds@1952
   151
  aNew->safety_level = 3;
ds@1952
   152
ds@1952
   153
#if SQLITE_HAS_CODEC
ds@1952
   154
  {
ds@1952
   155
    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
ds@1952
   156
    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
ds@1952
   157
    int nKey;
ds@1952
   158
    char *zKey;
ds@1952
   159
    int t = sqlite3_value_type(argv[2]);
ds@1952
   160
    switch( t ){
ds@1952
   161
      case SQLITE_INTEGER:
ds@1952
   162
      case SQLITE_FLOAT:
ds@1952
   163
        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
ds@1952
   164
        rc = SQLITE_ERROR;
ds@1952
   165
        break;
ds@1952
   166
        
ds@1952
   167
      case SQLITE_TEXT:
ds@1952
   168
      case SQLITE_BLOB:
ds@1952
   169
        nKey = sqlite3_value_bytes(argv[2]);
ds@1952
   170
        zKey = (char *)sqlite3_value_blob(argv[2]);
ds@1952
   171
        sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
ds@1952
   172
        break;
ds@1952
   173
ds@1952
   174
      case SQLITE_NULL:
ds@1952
   175
        /* No key specified.  Use the key from the main database */
ds@1952
   176
        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
ds@1952
   177
        sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
ds@1952
   178
        break;
ds@1952
   179
    }
ds@1952
   180
  }
ds@1952
   181
#endif
ds@1952
   182
ds@1952
   183
  /* If the file was opened successfully, read the schema for the new database.
ds@1952
   184
  ** If this fails, or if opening the file failed, then close the file and 
ds@1952
   185
  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
ds@1952
   186
  ** we found it.
ds@1952
   187
  */
ds@1952
   188
  if( rc==SQLITE_OK ){
ds@1952
   189
    sqlite3SafetyOn(db);
ds@1952
   190
    rc = sqlite3Init(db, &zErrDyn);
ds@1952
   191
    sqlite3SafetyOff(db);
ds@1952
   192
  }
ds@1952
   193
  if( rc ){
ds@1952
   194
    int iDb = db->nDb - 1;
ds@1952
   195
    assert( iDb>=2 );
ds@1952
   196
    if( db->aDb[iDb].pBt ){
ds@1952
   197
      sqlite3BtreeClose(db->aDb[iDb].pBt);
ds@1952
   198
      db->aDb[iDb].pBt = 0;
ds@1952
   199
      db->aDb[iDb].pSchema = 0;
ds@1952
   200
    }
ds@1952
   201
    sqlite3ResetInternalSchema(db, 0);
ds@1952
   202
    db->nDb = iDb;
ds@1952
   203
    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
ds@1952
   204
      db->mallocFailed = 1;
ds@1952
   205
      sqlite3_snprintf(sizeof(zErr),zErr, "out of memory");
ds@1952
   206
    }else{
ds@1952
   207
      sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile);
ds@1952
   208
    }
ds@1952
   209
    goto attach_error;
ds@1952
   210
  }
ds@1952
   211
  
ds@1952
   212
  return;
ds@1952
   213
ds@1952
   214
attach_error:
ds@1952
   215
  /* Return an error if we get here */
ds@1952
   216
  if( zErrDyn ){
ds@1952
   217
    sqlite3_result_error(context, zErrDyn, -1);
ds@1952
   218
    sqlite3_free(zErrDyn);
ds@1952
   219
  }else{
ds@1952
   220
    zErr[sizeof(zErr)-1] = 0;
ds@1952
   221
    sqlite3_result_error(context, zErr, -1);
ds@1952
   222
  }
ds@1952
   223
}
ds@1952
   224
ds@1952
   225
/*
ds@1952
   226
** An SQL user-function registered to do the work of an DETACH statement. The
ds@1952
   227
** three arguments to the function come directly from a detach statement:
ds@1952
   228
**
ds@1952
   229
**     DETACH DATABASE x
ds@1952
   230
**
ds@1952
   231
**     SELECT sqlite_detach(x)
ds@1952
   232
*/
ds@1952
   233
static void detachFunc(
ds@1952
   234
  sqlite3_context *context,
ds@1952
   235
  int argc,
ds@1952
   236
  sqlite3_value **argv
ds@1952
   237
){
ds@1952
   238
  const char *zName = (const char *)sqlite3_value_text(argv[0]);
ds@1952
   239
  sqlite3 *db = sqlite3_user_data(context);
ds@1952
   240
  int i;
ds@1952
   241
  Db *pDb = 0;
ds@1952
   242
  char zErr[128];
ds@1952
   243
ds@1952
   244
  if( zName==0 ) zName = "";
ds@1952
   245
  for(i=0; i<db->nDb; i++){
ds@1952
   246
    pDb = &db->aDb[i];
ds@1952
   247
    if( pDb->pBt==0 ) continue;
ds@1952
   248
    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
ds@1952
   249
  }
ds@1952
   250
ds@1952
   251
  if( i>=db->nDb ){
ds@1952
   252
    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
ds@1952
   253
    goto detach_error;
ds@1952
   254
  }
ds@1952
   255
  if( i<2 ){
ds@1952
   256
    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
ds@1952
   257
    goto detach_error;
ds@1952
   258
  }
ds@1952
   259
  if( !db->autoCommit ){
ds@1952
   260
    sqlite3_snprintf(sizeof(zErr), zErr,
ds@1952
   261
                     "cannot DETACH database within transaction");
ds@1952
   262
    goto detach_error;
ds@1952
   263
  }
ds@1952
   264
  if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){
ds@1952
   265
    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
ds@1952
   266
    goto detach_error;
ds@1952
   267
  }
ds@1952
   268
ds@1952
   269
  sqlite3BtreeClose(pDb->pBt);
ds@1952
   270
  pDb->pBt = 0;
ds@1952
   271
  pDb->pSchema = 0;
ds@1952
   272
  sqlite3ResetInternalSchema(db, 0);
ds@1952
   273
  return;
ds@1952
   274
ds@1952
   275
detach_error:
ds@1952
   276
  sqlite3_result_error(context, zErr, -1);
ds@1952
   277
}
ds@1952
   278
ds@1952
   279
/*
ds@1952
   280
** This procedure generates VDBE code for a single invocation of either the
ds@1952
   281
** sqlite_detach() or sqlite_attach() SQL user functions.
ds@1952
   282
*/
ds@1952
   283
static void codeAttach(
ds@1952
   284
  Parse *pParse,       /* The parser context */
ds@1952
   285
  int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
ds@1952
   286
  const char *zFunc,   /* Either "sqlite_attach" or "sqlite_detach */
ds@1952
   287
  int nFunc,           /* Number of args to pass to zFunc */
ds@1952
   288
  Expr *pAuthArg,      /* Expression to pass to authorization callback */
ds@1952
   289
  Expr *pFilename,     /* Name of database file */
ds@1952
   290
  Expr *pDbname,       /* Name of the database to use internally */
ds@1952
   291
  Expr *pKey           /* Database key for encryption extension */
ds@1952
   292
){
ds@1952
   293
  int rc;
ds@1952
   294
  NameContext sName;
ds@1952
   295
  Vdbe *v;
ds@1952
   296
  FuncDef *pFunc;
ds@1952
   297
  sqlite3* db = pParse->db;
ds@1952
   298
ds@1952
   299
#ifndef SQLITE_OMIT_AUTHORIZATION
ds@1952
   300
  assert( db->mallocFailed || pAuthArg );
ds@1952
   301
  if( pAuthArg ){
ds@1952
   302
    char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span);
ds@1952
   303
    if( !zAuthArg ){
ds@1952
   304
      goto attach_end;
ds@1952
   305
    }
ds@1952
   306
    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
ds@1952
   307
    sqlite3_free(zAuthArg);
ds@1952
   308
    if(rc!=SQLITE_OK ){
ds@1952
   309
      goto attach_end;
ds@1952
   310
    }
ds@1952
   311
  }
ds@1952
   312
#endif /* SQLITE_OMIT_AUTHORIZATION */
ds@1952
   313
ds@1952
   314
  memset(&sName, 0, sizeof(NameContext));
ds@1952
   315
  sName.pParse = pParse;
ds@1952
   316
ds@1952
   317
  if( 
ds@1952
   318
      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
ds@1952
   319
      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
ds@1952
   320
      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
ds@1952
   321
  ){
ds@1952
   322
    pParse->nErr++;
ds@1952
   323
    goto attach_end;
ds@1952
   324
  }
ds@1952
   325
ds@1952
   326
  v = sqlite3GetVdbe(pParse);
ds@1952
   327
  sqlite3ExprCode(pParse, pFilename);
ds@1952
   328
  sqlite3ExprCode(pParse, pDbname);
ds@1952
   329
  sqlite3ExprCode(pParse, pKey);
ds@1952
   330
ds@1952
   331
  assert( v || db->mallocFailed );
ds@1952
   332
  if( v ){
ds@1952
   333
    sqlite3VdbeAddOp(v, OP_Function, 0, nFunc);
ds@1952
   334
    pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
ds@1952
   335
    sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF);
ds@1952
   336
ds@1952
   337
    /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
ds@1952
   338
    ** statement only). For DETACH, set it to false (expire all existing
ds@1952
   339
    ** statements).
ds@1952
   340
    */
ds@1952
   341
    sqlite3VdbeAddOp(v, OP_Expire, (type==SQLITE_ATTACH), 0);
ds@1952
   342
  }
ds@1952
   343
  
ds@1952
   344
attach_end:
ds@1952
   345
  sqlite3ExprDelete(pFilename);
ds@1952
   346
  sqlite3ExprDelete(pDbname);
ds@1952
   347
  sqlite3ExprDelete(pKey);
ds@1952
   348
}
ds@1952
   349
ds@1952
   350
/*
ds@1952
   351
** Called by the parser to compile a DETACH statement.
ds@1952
   352
**
ds@1952
   353
**     DETACH pDbname
ds@1952
   354
*/
ds@1952
   355
void sqlite3Detach(Parse *pParse, Expr *pDbname){
ds@1952
   356
  codeAttach(pParse, SQLITE_DETACH, "sqlite_detach", 1, pDbname, 0, 0, pDbname);
ds@1952
   357
}
ds@1952
   358
ds@1952
   359
/*
ds@1952
   360
** Called by the parser to compile an ATTACH statement.
ds@1952
   361
**
ds@1952
   362
**     ATTACH p AS pDbname KEY pKey
ds@1952
   363
*/
ds@1952
   364
void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
ds@1952
   365
  codeAttach(pParse, SQLITE_ATTACH, "sqlite_attach", 3, p, p, pDbname, pKey);
ds@1952
   366
}
ds@1952
   367
#endif /* SQLITE_OMIT_ATTACH */
ds@1952
   368
ds@1952
   369
/*
ds@1952
   370
** Register the functions sqlite_attach and sqlite_detach.
ds@1952
   371
*/
ds@1952
   372
void sqlite3AttachFunctions(sqlite3 *db){
ds@1952
   373
#ifndef SQLITE_OMIT_ATTACH
ds@1952
   374
  static const int enc = SQLITE_UTF8;
ds@1952
   375
  sqlite3CreateFunc(db, "sqlite_attach", 3, enc, db, attachFunc, 0, 0);
ds@1952
   376
  sqlite3CreateFunc(db, "sqlite_detach", 1, enc, db, detachFunc, 0, 0);
ds@1952
   377
#endif
ds@1952
   378
}
ds@1952
   379
ds@1952
   380
/*
ds@1952
   381
** Initialize a DbFixer structure.  This routine must be called prior
ds@1952
   382
** to passing the structure to one of the sqliteFixAAAA() routines below.
ds@1952
   383
**
ds@1952
   384
** The return value indicates whether or not fixation is required.  TRUE
ds@1952
   385
** means we do need to fix the database references, FALSE means we do not.
ds@1952
   386
*/
ds@1952
   387
int sqlite3FixInit(
ds@1952
   388
  DbFixer *pFix,      /* The fixer to be initialized */
ds@1952
   389
  Parse *pParse,      /* Error messages will be written here */
ds@1952
   390
  int iDb,            /* This is the database that must be used */
ds@1952
   391
  const char *zType,  /* "view", "trigger", or "index" */
ds@1952
   392
  const Token *pName  /* Name of the view, trigger, or index */
ds@1952
   393
){
ds@1952
   394
  sqlite3 *db;
ds@1952
   395
ds@1952
   396
  if( iDb<0 || iDb==1 ) return 0;
ds@1952
   397
  db = pParse->db;
ds@1952
   398
  assert( db->nDb>iDb );
ds@1952
   399
  pFix->pParse = pParse;
ds@1952
   400
  pFix->zDb = db->aDb[iDb].zName;
ds@1952
   401
  pFix->zType = zType;
ds@1952
   402
  pFix->pName = pName;
ds@1952
   403
  return 1;
ds@1952
   404
}
ds@1952
   405
ds@1952
   406
/*
ds@1952
   407
** The following set of routines walk through the parse tree and assign
ds@1952
   408
** a specific database to all table references where the database name
ds@1952
   409
** was left unspecified in the original SQL statement.  The pFix structure
ds@1952
   410
** must have been initialized by a prior call to sqlite3FixInit().
ds@1952
   411
**
ds@1952
   412
** These routines are used to make sure that an index, trigger, or
ds@1952
   413
** view in one database does not refer to objects in a different database.
ds@1952
   414
** (Exception: indices, triggers, and views in the TEMP database are
ds@1952
   415
** allowed to refer to anything.)  If a reference is explicitly made
ds@1952
   416
** to an object in a different database, an error message is added to
ds@1952
   417
** pParse->zErrMsg and these routines return non-zero.  If everything
ds@1952
   418
** checks out, these routines return 0.
ds@1952
   419
*/
ds@1952
   420
int sqlite3FixSrcList(
ds@1952
   421
  DbFixer *pFix,       /* Context of the fixation */
ds@1952
   422
  SrcList *pList       /* The Source list to check and modify */
ds@1952
   423
){
ds@1952
   424
  int i;
ds@1952
   425
  const char *zDb;
ds@1952
   426
  struct SrcList_item *pItem;
ds@1952
   427
ds@1952
   428
  if( pList==0 ) return 0;
ds@1952
   429
  zDb = pFix->zDb;
ds@1952
   430
  for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
ds@1952
   431
    if( pItem->zDatabase==0 ){
ds@1952
   432
      pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
ds@1952
   433
    }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
ds@1952
   434
      sqlite3ErrorMsg(pFix->pParse,
ds@1952
   435
         "%s %T cannot reference objects in database %s",
ds@1952
   436
         pFix->zType, pFix->pName, pItem->zDatabase);
ds@1952
   437
      return 1;
ds@1952
   438
    }
ds@1952
   439
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
ds@1952
   440
    if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
ds@1952
   441
    if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
ds@1952
   442
#endif
ds@1952
   443
  }
ds@1952
   444
  return 0;
ds@1952
   445
}
ds@1952
   446
#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
ds@1952
   447
int sqlite3FixSelect(
ds@1952
   448
  DbFixer *pFix,       /* Context of the fixation */
ds@1952
   449
  Select *pSelect      /* The SELECT statement to be fixed to one database */
ds@1952
   450
){
ds@1952
   451
  while( pSelect ){
ds@1952
   452
    if( sqlite3FixExprList(pFix, pSelect->pEList) ){
ds@1952
   453
      return 1;
ds@1952
   454
    }
ds@1952
   455
    if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
ds@1952
   456
      return 1;
ds@1952
   457
    }
ds@1952
   458
    if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
ds@1952
   459
      return 1;
ds@1952
   460
    }
ds@1952
   461
    if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
ds@1952
   462
      return 1;
ds@1952
   463
    }
ds@1952
   464
    pSelect = pSelect->pPrior;
ds@1952
   465
  }
ds@1952
   466
  return 0;
ds@1952
   467
}
ds@1952
   468
int sqlite3FixExpr(
ds@1952
   469
  DbFixer *pFix,     /* Context of the fixation */
ds@1952
   470
  Expr *pExpr        /* The expression to be fixed to one database */
ds@1952
   471
){
ds@1952
   472
  while( pExpr ){
ds@1952
   473
    if( sqlite3FixSelect(pFix, pExpr->pSelect) ){
ds@1952
   474
      return 1;
ds@1952
   475
    }
ds@1952
   476
    if( sqlite3FixExprList(pFix, pExpr->pList) ){
ds@1952
   477
      return 1;
ds@1952
   478
    }
ds@1952
   479
    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
ds@1952
   480
      return 1;
ds@1952
   481
    }
ds@1952
   482
    pExpr = pExpr->pLeft;
ds@1952
   483
  }
ds@1952
   484
  return 0;
ds@1952
   485
}
ds@1952
   486
int sqlite3FixExprList(
ds@1952
   487
  DbFixer *pFix,     /* Context of the fixation */
ds@1952
   488
  ExprList *pList    /* The expression to be fixed to one database */
ds@1952
   489
){
ds@1952
   490
  int i;
ds@1952
   491
  struct ExprList_item *pItem;
ds@1952
   492
  if( pList==0 ) return 0;
ds@1952
   493
  for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
ds@1952
   494
    if( sqlite3FixExpr(pFix, pItem->pExpr) ){
ds@1952
   495
      return 1;
ds@1952
   496
    }
ds@1952
   497
  }
ds@1952
   498
  return 0;
ds@1952
   499
}
ds@1952
   500
#endif
ds@1952
   501
ds@1952
   502
#ifndef SQLITE_OMIT_TRIGGER
ds@1952
   503
int sqlite3FixTriggerStep(
ds@1952
   504
  DbFixer *pFix,     /* Context of the fixation */
ds@1952
   505
  TriggerStep *pStep /* The trigger step be fixed to one database */
ds@1952
   506
){
ds@1952
   507
  while( pStep ){
ds@1952
   508
    if( sqlite3FixSelect(pFix, pStep->pSelect) ){
ds@1952
   509
      return 1;
ds@1952
   510
    }
ds@1952
   511
    if( sqlite3FixExpr(pFix, pStep->pWhere) ){
ds@1952
   512
      return 1;
ds@1952
   513
    }
ds@1952
   514
    if( sqlite3FixExprList(pFix, pStep->pExprList) ){
ds@1952
   515
      return 1;
ds@1952
   516
    }
ds@1952
   517
    pStep = pStep->pNext;
ds@1952
   518
  }
ds@1952
   519
  return 0;
ds@1952
   520
}
ds@1952
   521
#endif