1c11d4f93Sdrh /* 2c11d4f93Sdrh ** 2003 April 6 3c11d4f93Sdrh ** 4c11d4f93Sdrh ** The author disclaims copyright to this source code. In place of 5c11d4f93Sdrh ** a legal notice, here is a blessing: 6c11d4f93Sdrh ** 7c11d4f93Sdrh ** May you do good and not evil. 8c11d4f93Sdrh ** May you find forgiveness for yourself and forgive others. 9c11d4f93Sdrh ** May you share freely, never taking more than you give. 10c11d4f93Sdrh ** 11c11d4f93Sdrh ************************************************************************* 12c11d4f93Sdrh ** This file contains code used to implement the ATTACH and DETACH commands. 13c11d4f93Sdrh ** 14*fdbcdee5Sdrh ** $Id: attach.c,v 1.56 2007/03/27 14:44:51 drh Exp $ 15c11d4f93Sdrh */ 16c11d4f93Sdrh #include "sqliteInt.h" 17c11d4f93Sdrh 18*fdbcdee5Sdrh #ifndef SQLITE_OMIT_ATTACH 19c11d4f93Sdrh /* 20f744bb56Sdanielk1977 ** Resolve an expression that was part of an ATTACH or DETACH statement. This 21f744bb56Sdanielk1977 ** is slightly different from resolving a normal SQL expression, because simple 22f744bb56Sdanielk1977 ** identifiers are treated as strings, not possible column names or aliases. 23c11d4f93Sdrh ** 24f744bb56Sdanielk1977 ** i.e. if the parser sees: 25c11d4f93Sdrh ** 26f744bb56Sdanielk1977 ** ATTACH DATABASE abc AS def 27f744bb56Sdanielk1977 ** 28f744bb56Sdanielk1977 ** it treats the two expressions as literal strings 'abc' and 'def' instead of 29f744bb56Sdanielk1977 ** looking for columns of the same name. 30f744bb56Sdanielk1977 ** 31f744bb56Sdanielk1977 ** This only applies to the root node of pExpr, so the statement: 32f744bb56Sdanielk1977 ** 33f744bb56Sdanielk1977 ** ATTACH DATABASE abc||def AS 'db2' 34f744bb56Sdanielk1977 ** 35f744bb56Sdanielk1977 ** will fail because neither abc or def can be resolved. 36c11d4f93Sdrh */ 37f0113000Sdanielk1977 static int resolveAttachExpr(NameContext *pName, Expr *pExpr) 38f744bb56Sdanielk1977 { 39f744bb56Sdanielk1977 int rc = SQLITE_OK; 40f744bb56Sdanielk1977 if( pExpr ){ 41f744bb56Sdanielk1977 if( pExpr->op!=TK_ID ){ 42f744bb56Sdanielk1977 rc = sqlite3ExprResolveNames(pName, pExpr); 43f744bb56Sdanielk1977 }else{ 44f744bb56Sdanielk1977 pExpr->op = TK_STRING; 45f744bb56Sdanielk1977 } 46f744bb56Sdanielk1977 } 47f744bb56Sdanielk1977 return rc; 48f744bb56Sdanielk1977 } 49f744bb56Sdanielk1977 50f744bb56Sdanielk1977 /* 51f744bb56Sdanielk1977 ** An SQL user-function registered to do the work of an ATTACH statement. The 52f744bb56Sdanielk1977 ** three arguments to the function come directly from an attach statement: 53f744bb56Sdanielk1977 ** 54f744bb56Sdanielk1977 ** ATTACH DATABASE x AS y KEY z 55f744bb56Sdanielk1977 ** 56f744bb56Sdanielk1977 ** SELECT sqlite_attach(x, y, z) 57f744bb56Sdanielk1977 ** 58f744bb56Sdanielk1977 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the 59f744bb56Sdanielk1977 ** third argument. 60f744bb56Sdanielk1977 */ 61f744bb56Sdanielk1977 static void attachFunc( 62f744bb56Sdanielk1977 sqlite3_context *context, 63f744bb56Sdanielk1977 int argc, 64f744bb56Sdanielk1977 sqlite3_value **argv 6525d6543dSdrh ){ 66f744bb56Sdanielk1977 int i; 67f744bb56Sdanielk1977 int rc = 0; 68f744bb56Sdanielk1977 sqlite3 *db = sqlite3_user_data(context); 69f744bb56Sdanielk1977 const char *zName; 70f744bb56Sdanielk1977 const char *zFile; 71c11d4f93Sdrh Db *aNew; 72f744bb56Sdanielk1977 char zErr[128]; 73f744bb56Sdanielk1977 char *zErrDyn = 0; 74c11d4f93Sdrh 75f744bb56Sdanielk1977 zFile = (const char *)sqlite3_value_text(argv[0]); 76f744bb56Sdanielk1977 zName = (const char *)sqlite3_value_text(argv[1]); 77ea063f5bSdrh if( zFile==0 ) zFile = ""; 78ea063f5bSdrh if( zName==0 ) zName = ""; 79f744bb56Sdanielk1977 80f744bb56Sdanielk1977 /* Check for the following errors: 81f744bb56Sdanielk1977 ** 82f744bb56Sdanielk1977 ** * Too many attached databases, 83f744bb56Sdanielk1977 ** * Transaction currently open 84f744bb56Sdanielk1977 ** * Specified database name already being used. 85f744bb56Sdanielk1977 */ 86c11d4f93Sdrh if( db->nDb>=MAX_ATTACHED+2 ){ 87f744bb56Sdanielk1977 sqlite3_snprintf( 88ea063f5bSdrh sizeof(zErr), zErr, "too many attached databases - max %d", MAX_ATTACHED 89f744bb56Sdanielk1977 ); 90f744bb56Sdanielk1977 goto attach_error; 91c11d4f93Sdrh } 9292f9a1bbSdanielk1977 if( !db->autoCommit ){ 93f744bb56Sdanielk1977 strcpy(zErr, "cannot ATTACH database within transaction"); 94f744bb56Sdanielk1977 goto attach_error; 95d5d56523Sdanielk1977 } 9681e293b4Sdrh for(i=0; i<db->nDb; i++){ 97124b27e6Sdrh char *z = db->aDb[i].zName; 98ea063f5bSdrh if( z && zName && sqlite3StrICmp(z, zName)==0 ){ 99ea063f5bSdrh sqlite3_snprintf(sizeof(zErr), zErr, "database %s is already in use", zName); 100f744bb56Sdanielk1977 goto attach_error; 10181e293b4Sdrh } 10281e293b4Sdrh } 10381e293b4Sdrh 104f744bb56Sdanielk1977 /* Allocate the new entry in the db->aDb[] array and initialise the schema 105f744bb56Sdanielk1977 ** hash tables. 106f744bb56Sdanielk1977 */ 107c11d4f93Sdrh if( db->aDb==db->aDbStatic ){ 108c11d4f93Sdrh aNew = sqliteMalloc( sizeof(db->aDb[0])*3 ); 109d5d56523Sdanielk1977 if( aNew==0 ){ 110f744bb56Sdanielk1977 return; 111d5d56523Sdanielk1977 } 112c11d4f93Sdrh memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); 113c11d4f93Sdrh }else{ 114c11d4f93Sdrh aNew = sqliteRealloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); 115d5d56523Sdanielk1977 if( aNew==0 ){ 116f744bb56Sdanielk1977 return; 117d5d56523Sdanielk1977 } 118c11d4f93Sdrh } 119c11d4f93Sdrh db->aDb = aNew; 120c11d4f93Sdrh aNew = &db->aDb[db->nDb++]; 121c11d4f93Sdrh memset(aNew, 0, sizeof(*aNew)); 122da184236Sdanielk1977 123da184236Sdanielk1977 /* Open the database file. If the btree is successfully opened, use 124da184236Sdanielk1977 ** it to obtain the database schema. At this point the schema may 125da184236Sdanielk1977 ** or may not be initialised. 126da184236Sdanielk1977 */ 127da184236Sdanielk1977 rc = sqlite3BtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt); 128da184236Sdanielk1977 if( rc==SQLITE_OK ){ 129da184236Sdanielk1977 aNew->pSchema = sqlite3SchemaGet(aNew->pBt); 130da184236Sdanielk1977 if( !aNew->pSchema ){ 131da184236Sdanielk1977 rc = SQLITE_NOMEM; 13229c636bcSdrh }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ 13314db2665Sdanielk1977 strcpy(zErr, 13414db2665Sdanielk1977 "attached databases must use the same text encoding as main database"); 13514db2665Sdanielk1977 goto attach_error; 13614db2665Sdanielk1977 } 13741483468Sdanielk1977 sqlite3PagerLockingMode(sqlite3BtreePager(aNew->pBt), db->dfltLockMode); 138da184236Sdanielk1977 } 139f744bb56Sdanielk1977 aNew->zName = sqliteStrDup(zName); 14091cf71b0Sdanielk1977 aNew->safety_level = 3; 141f744bb56Sdanielk1977 1424d189ca4Sdrh #if SQLITE_HAS_CODEC 14325d6543dSdrh { 14425d6543dSdrh extern int sqlite3CodecAttach(sqlite3*, int, void*, int); 14525d6543dSdrh extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); 14672e5c6dbSdrh int nKey; 14772e5c6dbSdrh char *zKey; 148f744bb56Sdanielk1977 int t = sqlite3_value_type(argv[2]); 149f744bb56Sdanielk1977 switch( t ){ 150f744bb56Sdanielk1977 case SQLITE_INTEGER: 151f744bb56Sdanielk1977 case SQLITE_FLOAT: 152f744bb56Sdanielk1977 zErrDyn = sqliteStrDup("Invalid key value"); 153f744bb56Sdanielk1977 rc = SQLITE_ERROR; 154f744bb56Sdanielk1977 break; 155f744bb56Sdanielk1977 156f744bb56Sdanielk1977 case SQLITE_TEXT: 157f744bb56Sdanielk1977 case SQLITE_BLOB: 158f744bb56Sdanielk1977 nKey = sqlite3_value_bytes(argv[2]); 159f744bb56Sdanielk1977 zKey = (char *)sqlite3_value_blob(argv[2]); 16025d6543dSdrh sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); 161f744bb56Sdanielk1977 break; 162f744bb56Sdanielk1977 163f744bb56Sdanielk1977 case SQLITE_NULL: 164f744bb56Sdanielk1977 /* No key specified. Use the key from the main database */ 165f744bb56Sdanielk1977 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); 166f744bb56Sdanielk1977 sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); 167f744bb56Sdanielk1977 break; 16825d6543dSdrh } 1694d189ca4Sdrh } 1704d189ca4Sdrh #endif 171f744bb56Sdanielk1977 172f744bb56Sdanielk1977 /* If the file was opened successfully, read the schema for the new database. 173f744bb56Sdanielk1977 ** If this fails, or if opening the file failed, then close the file and 174f744bb56Sdanielk1977 ** remove the entry from the db->aDb[] array. i.e. put everything back the way 175f744bb56Sdanielk1977 ** we found it. 176f744bb56Sdanielk1977 */ 177f744bb56Sdanielk1977 if( rc==SQLITE_OK ){ 178f744bb56Sdanielk1977 sqlite3SafetyOn(db); 179f744bb56Sdanielk1977 rc = sqlite3Init(db, &zErrDyn); 180f744bb56Sdanielk1977 sqlite3SafetyOff(db); 181447623d9Sdrh } 182c11d4f93Sdrh if( rc ){ 183f0113000Sdanielk1977 int iDb = db->nDb - 1; 184f0113000Sdanielk1977 assert( iDb>=2 ); 185f0113000Sdanielk1977 if( db->aDb[iDb].pBt ){ 186f0113000Sdanielk1977 sqlite3BtreeClose(db->aDb[iDb].pBt); 187f0113000Sdanielk1977 db->aDb[iDb].pBt = 0; 188f0113000Sdanielk1977 db->aDb[iDb].pSchema = 0; 189447623d9Sdrh } 1904adee20fSdanielk1977 sqlite3ResetInternalSchema(db, 0); 191f0113000Sdanielk1977 db->nDb = iDb; 1926103fe97Sdrh if( rc==SQLITE_NOMEM ){ 193fe39319eSdanielk1977 sqlite3FailedMalloc(); 194ea063f5bSdrh sqlite3_snprintf(sizeof(zErr),zErr, "out of memory"); 1956103fe97Sdrh }else{ 196ea063f5bSdrh sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile); 1976103fe97Sdrh } 198f744bb56Sdanielk1977 goto attach_error; 199c11d4f93Sdrh } 200d5d56523Sdanielk1977 201f744bb56Sdanielk1977 return; 202f744bb56Sdanielk1977 203f744bb56Sdanielk1977 attach_error: 204f744bb56Sdanielk1977 /* Return an error if we get here */ 205f744bb56Sdanielk1977 if( zErrDyn ){ 206f744bb56Sdanielk1977 sqlite3_result_error(context, zErrDyn, -1); 207f744bb56Sdanielk1977 sqliteFree(zErrDyn); 208f744bb56Sdanielk1977 }else{ 209f744bb56Sdanielk1977 zErr[sizeof(zErr)-1] = 0; 210f744bb56Sdanielk1977 sqlite3_result_error(context, zErr, -1); 211f744bb56Sdanielk1977 } 2128a41449eSdanielk1977 } 213c11d4f93Sdrh 214c11d4f93Sdrh /* 215f744bb56Sdanielk1977 ** An SQL user-function registered to do the work of an DETACH statement. The 216f744bb56Sdanielk1977 ** three arguments to the function come directly from a detach statement: 217c11d4f93Sdrh ** 218f744bb56Sdanielk1977 ** DETACH DATABASE x 219c11d4f93Sdrh ** 220f744bb56Sdanielk1977 ** SELECT sqlite_detach(x) 221c11d4f93Sdrh */ 222f744bb56Sdanielk1977 static void detachFunc( 223f744bb56Sdanielk1977 sqlite3_context *context, 224f744bb56Sdanielk1977 int argc, 225f744bb56Sdanielk1977 sqlite3_value **argv 226f744bb56Sdanielk1977 ){ 227f744bb56Sdanielk1977 const char *zName = (const char *)sqlite3_value_text(argv[0]); 228f744bb56Sdanielk1977 sqlite3 *db = sqlite3_user_data(context); 229c11d4f93Sdrh int i; 230e0d4b060Sdanielk1977 Db *pDb = 0; 231f744bb56Sdanielk1977 char zErr[128]; 232c11d4f93Sdrh 233ea063f5bSdrh if( zName==0 ) zName = ""; 234c11d4f93Sdrh for(i=0; i<db->nDb; i++){ 235124b27e6Sdrh pDb = &db->aDb[i]; 236ae8b3615Sdrh if( pDb->pBt==0 ) continue; 237ae8b3615Sdrh if( sqlite3StrICmp(pDb->zName, zName)==0 ) break; 238c11d4f93Sdrh } 239f744bb56Sdanielk1977 240c11d4f93Sdrh if( i>=db->nDb ){ 241f744bb56Sdanielk1977 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName); 242f744bb56Sdanielk1977 goto detach_error; 243c11d4f93Sdrh } 244c11d4f93Sdrh if( i<2 ){ 245f744bb56Sdanielk1977 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); 246f744bb56Sdanielk1977 goto detach_error; 247c11d4f93Sdrh } 24892f9a1bbSdanielk1977 if( !db->autoCommit ){ 249f744bb56Sdanielk1977 strcpy(zErr, "cannot DETACH database within transaction"); 250f744bb56Sdanielk1977 goto detach_error; 25192f9a1bbSdanielk1977 } 2522372c2b1Sdanielk1977 if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){ 2532372c2b1Sdanielk1977 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); 2542372c2b1Sdanielk1977 goto detach_error; 2552372c2b1Sdanielk1977 } 256f744bb56Sdanielk1977 257124b27e6Sdrh sqlite3BtreeClose(pDb->pBt); 258124b27e6Sdrh pDb->pBt = 0; 259311019beSdanielk1977 pDb->pSchema = 0; 260db2d9a0bSdrh sqlite3ResetInternalSchema(db, 0); 261f744bb56Sdanielk1977 return; 262f744bb56Sdanielk1977 263f744bb56Sdanielk1977 detach_error: 264f744bb56Sdanielk1977 sqlite3_result_error(context, zErr, -1); 265f744bb56Sdanielk1977 } 266f744bb56Sdanielk1977 267f744bb56Sdanielk1977 /* 268f744bb56Sdanielk1977 ** This procedure generates VDBE code for a single invocation of either the 269f744bb56Sdanielk1977 ** sqlite_detach() or sqlite_attach() SQL user functions. 270f744bb56Sdanielk1977 */ 271f744bb56Sdanielk1977 static void codeAttach( 272f744bb56Sdanielk1977 Parse *pParse, /* The parser context */ 273f744bb56Sdanielk1977 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */ 274f744bb56Sdanielk1977 const char *zFunc, /* Either "sqlite_attach" or "sqlite_detach */ 275f744bb56Sdanielk1977 int nFunc, /* Number of args to pass to zFunc */ 276f744bb56Sdanielk1977 Expr *pAuthArg, /* Expression to pass to authorization callback */ 277f744bb56Sdanielk1977 Expr *pFilename, /* Name of database file */ 278f744bb56Sdanielk1977 Expr *pDbname, /* Name of the database to use internally */ 279f744bb56Sdanielk1977 Expr *pKey /* Database key for encryption extension */ 280f744bb56Sdanielk1977 ){ 281f744bb56Sdanielk1977 int rc; 282f744bb56Sdanielk1977 NameContext sName; 283f744bb56Sdanielk1977 Vdbe *v; 284f744bb56Sdanielk1977 FuncDef *pFunc; 285f744bb56Sdanielk1977 sqlite3* db = pParse->db; 286f744bb56Sdanielk1977 287f744bb56Sdanielk1977 #ifndef SQLITE_OMIT_AUTHORIZATION 2889e12800dSdanielk1977 assert( sqlite3MallocFailed() || pAuthArg ); 2892e588c75Sdanielk1977 if( pAuthArg ){ 290f744bb56Sdanielk1977 char *zAuthArg = sqlite3NameFromToken(&pAuthArg->span); 291f744bb56Sdanielk1977 if( !zAuthArg ){ 292f744bb56Sdanielk1977 goto attach_end; 293f744bb56Sdanielk1977 } 2942e588c75Sdanielk1977 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); 2952e588c75Sdanielk1977 sqliteFree(zAuthArg); 2962e588c75Sdanielk1977 if(rc!=SQLITE_OK ){ 297f744bb56Sdanielk1977 goto attach_end; 298f744bb56Sdanielk1977 } 2992e588c75Sdanielk1977 } 300f744bb56Sdanielk1977 #endif /* SQLITE_OMIT_AUTHORIZATION */ 301f744bb56Sdanielk1977 302f744bb56Sdanielk1977 memset(&sName, 0, sizeof(NameContext)); 303f744bb56Sdanielk1977 sName.pParse = pParse; 304f744bb56Sdanielk1977 305f744bb56Sdanielk1977 if( 306f744bb56Sdanielk1977 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || 307f744bb56Sdanielk1977 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || 308f744bb56Sdanielk1977 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) 309f744bb56Sdanielk1977 ){ 310f744bb56Sdanielk1977 pParse->nErr++; 311f744bb56Sdanielk1977 goto attach_end; 312f744bb56Sdanielk1977 } 313f744bb56Sdanielk1977 314f744bb56Sdanielk1977 v = sqlite3GetVdbe(pParse); 315f744bb56Sdanielk1977 sqlite3ExprCode(pParse, pFilename); 316f744bb56Sdanielk1977 sqlite3ExprCode(pParse, pDbname); 317f744bb56Sdanielk1977 sqlite3ExprCode(pParse, pKey); 318f744bb56Sdanielk1977 3199e12800dSdanielk1977 assert( v || sqlite3MallocFailed() ); 320f744bb56Sdanielk1977 if( v ){ 321f744bb56Sdanielk1977 sqlite3VdbeAddOp(v, OP_Function, 0, nFunc); 322f744bb56Sdanielk1977 pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0); 323f744bb56Sdanielk1977 sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF); 324f744bb56Sdanielk1977 325f744bb56Sdanielk1977 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this 326f744bb56Sdanielk1977 ** statement only). For DETACH, set it to false (expire all existing 327f744bb56Sdanielk1977 ** statements). 328f744bb56Sdanielk1977 */ 329f744bb56Sdanielk1977 sqlite3VdbeAddOp(v, OP_Expire, (type==SQLITE_ATTACH), 0); 330f744bb56Sdanielk1977 } 331f744bb56Sdanielk1977 332f744bb56Sdanielk1977 attach_end: 333f744bb56Sdanielk1977 sqlite3ExprDelete(pFilename); 334f744bb56Sdanielk1977 sqlite3ExprDelete(pDbname); 335f744bb56Sdanielk1977 sqlite3ExprDelete(pKey); 336f744bb56Sdanielk1977 } 337f744bb56Sdanielk1977 338f744bb56Sdanielk1977 /* 339f744bb56Sdanielk1977 ** Called by the parser to compile a DETACH statement. 340f744bb56Sdanielk1977 ** 341f744bb56Sdanielk1977 ** DETACH pDbname 342f744bb56Sdanielk1977 */ 343f744bb56Sdanielk1977 void sqlite3Detach(Parse *pParse, Expr *pDbname){ 344f744bb56Sdanielk1977 codeAttach(pParse, SQLITE_DETACH, "sqlite_detach", 1, pDbname, 0, 0, pDbname); 345f744bb56Sdanielk1977 } 346f744bb56Sdanielk1977 347f744bb56Sdanielk1977 /* 348f744bb56Sdanielk1977 ** Called by the parser to compile an ATTACH statement. 349f744bb56Sdanielk1977 ** 350f744bb56Sdanielk1977 ** ATTACH p AS pDbname KEY pKey 351f744bb56Sdanielk1977 */ 352f744bb56Sdanielk1977 void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ 353f744bb56Sdanielk1977 codeAttach(pParse, SQLITE_ATTACH, "sqlite_attach", 3, p, p, pDbname, pKey); 354f744bb56Sdanielk1977 } 355*fdbcdee5Sdrh #endif /* SQLITE_OMIT_ATTACH */ 356f744bb56Sdanielk1977 357198bf391Sdrh /* 358198bf391Sdrh ** Register the functions sqlite_attach and sqlite_detach. 359198bf391Sdrh */ 360198bf391Sdrh void sqlite3AttachFunctions(sqlite3 *db){ 361*fdbcdee5Sdrh #ifndef SQLITE_OMIT_ATTACH 362f744bb56Sdanielk1977 static const int enc = SQLITE_UTF8; 363771151b6Sdanielk1977 sqlite3CreateFunc(db, "sqlite_attach", 3, enc, db, attachFunc, 0, 0); 364771151b6Sdanielk1977 sqlite3CreateFunc(db, "sqlite_detach", 1, enc, db, detachFunc, 0, 0); 365*fdbcdee5Sdrh #endif 366c11d4f93Sdrh } 367f26e09c8Sdrh 368f26e09c8Sdrh /* 369f26e09c8Sdrh ** Initialize a DbFixer structure. This routine must be called prior 370f26e09c8Sdrh ** to passing the structure to one of the sqliteFixAAAA() routines below. 371f26e09c8Sdrh ** 372f26e09c8Sdrh ** The return value indicates whether or not fixation is required. TRUE 373f26e09c8Sdrh ** means we do need to fix the database references, FALSE means we do not. 374f26e09c8Sdrh */ 3754adee20fSdanielk1977 int sqlite3FixInit( 376f26e09c8Sdrh DbFixer *pFix, /* The fixer to be initialized */ 377f26e09c8Sdrh Parse *pParse, /* Error messages will be written here */ 378873cdcb2Sdrh int iDb, /* This is the database that must be used */ 379f26e09c8Sdrh const char *zType, /* "view", "trigger", or "index" */ 380f26e09c8Sdrh const Token *pName /* Name of the view, trigger, or index */ 381f26e09c8Sdrh ){ 3829bb575fdSdrh sqlite3 *db; 383f26e09c8Sdrh 384f26e09c8Sdrh if( iDb<0 || iDb==1 ) return 0; 385f26e09c8Sdrh db = pParse->db; 386f26e09c8Sdrh assert( db->nDb>iDb ); 3874312db55Sdrh pFix->pParse = pParse; 388f26e09c8Sdrh pFix->zDb = db->aDb[iDb].zName; 389f26e09c8Sdrh pFix->zType = zType; 390f26e09c8Sdrh pFix->pName = pName; 391f26e09c8Sdrh return 1; 392f26e09c8Sdrh } 393f26e09c8Sdrh 394f26e09c8Sdrh /* 395f26e09c8Sdrh ** The following set of routines walk through the parse tree and assign 396f26e09c8Sdrh ** a specific database to all table references where the database name 397f26e09c8Sdrh ** was left unspecified in the original SQL statement. The pFix structure 3984adee20fSdanielk1977 ** must have been initialized by a prior call to sqlite3FixInit(). 399f26e09c8Sdrh ** 400f26e09c8Sdrh ** These routines are used to make sure that an index, trigger, or 401f26e09c8Sdrh ** view in one database does not refer to objects in a different database. 402f26e09c8Sdrh ** (Exception: indices, triggers, and views in the TEMP database are 403f26e09c8Sdrh ** allowed to refer to anything.) If a reference is explicitly made 404f26e09c8Sdrh ** to an object in a different database, an error message is added to 405f26e09c8Sdrh ** pParse->zErrMsg and these routines return non-zero. If everything 406f26e09c8Sdrh ** checks out, these routines return 0. 407f26e09c8Sdrh */ 4084adee20fSdanielk1977 int sqlite3FixSrcList( 409f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 410f26e09c8Sdrh SrcList *pList /* The Source list to check and modify */ 411f26e09c8Sdrh ){ 412f26e09c8Sdrh int i; 413f26e09c8Sdrh const char *zDb; 414873cdcb2Sdrh struct SrcList_item *pItem; 415f26e09c8Sdrh 416f26e09c8Sdrh if( pList==0 ) return 0; 417f26e09c8Sdrh zDb = pFix->zDb; 418873cdcb2Sdrh for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ 419873cdcb2Sdrh if( pItem->zDatabase==0 ){ 420873cdcb2Sdrh pItem->zDatabase = sqliteStrDup(zDb); 421873cdcb2Sdrh }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){ 4224adee20fSdanielk1977 sqlite3ErrorMsg(pFix->pParse, 423873cdcb2Sdrh "%s %T cannot reference objects in database %s", 424873cdcb2Sdrh pFix->zType, pFix->pName, pItem->zDatabase); 425f26e09c8Sdrh return 1; 426f26e09c8Sdrh } 427b84f96f8Sdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) 428873cdcb2Sdrh if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; 429873cdcb2Sdrh if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; 430b84f96f8Sdanielk1977 #endif 431f26e09c8Sdrh } 432f26e09c8Sdrh return 0; 433f26e09c8Sdrh } 434b84f96f8Sdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) 4354adee20fSdanielk1977 int sqlite3FixSelect( 436f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 437f26e09c8Sdrh Select *pSelect /* The SELECT statement to be fixed to one database */ 438f26e09c8Sdrh ){ 439f26e09c8Sdrh while( pSelect ){ 4404adee20fSdanielk1977 if( sqlite3FixExprList(pFix, pSelect->pEList) ){ 441f26e09c8Sdrh return 1; 442f26e09c8Sdrh } 4434adee20fSdanielk1977 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ 444f26e09c8Sdrh return 1; 445f26e09c8Sdrh } 4464adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ 447f26e09c8Sdrh return 1; 448f26e09c8Sdrh } 4494adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ 450f26e09c8Sdrh return 1; 451f26e09c8Sdrh } 452f26e09c8Sdrh pSelect = pSelect->pPrior; 453f26e09c8Sdrh } 454f26e09c8Sdrh return 0; 455f26e09c8Sdrh } 4564adee20fSdanielk1977 int sqlite3FixExpr( 457f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 458f26e09c8Sdrh Expr *pExpr /* The expression to be fixed to one database */ 459f26e09c8Sdrh ){ 460f26e09c8Sdrh while( pExpr ){ 4614adee20fSdanielk1977 if( sqlite3FixSelect(pFix, pExpr->pSelect) ){ 462f26e09c8Sdrh return 1; 463f26e09c8Sdrh } 4644adee20fSdanielk1977 if( sqlite3FixExprList(pFix, pExpr->pList) ){ 465f26e09c8Sdrh return 1; 466f26e09c8Sdrh } 4674adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pExpr->pRight) ){ 468f26e09c8Sdrh return 1; 469f26e09c8Sdrh } 470f26e09c8Sdrh pExpr = pExpr->pLeft; 471f26e09c8Sdrh } 472f26e09c8Sdrh return 0; 473f26e09c8Sdrh } 4744adee20fSdanielk1977 int sqlite3FixExprList( 475f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 476f26e09c8Sdrh ExprList *pList /* The expression to be fixed to one database */ 477f26e09c8Sdrh ){ 478f26e09c8Sdrh int i; 479873cdcb2Sdrh struct ExprList_item *pItem; 480f26e09c8Sdrh if( pList==0 ) return 0; 481873cdcb2Sdrh for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){ 482873cdcb2Sdrh if( sqlite3FixExpr(pFix, pItem->pExpr) ){ 483f26e09c8Sdrh return 1; 484f26e09c8Sdrh } 485f26e09c8Sdrh } 486f26e09c8Sdrh return 0; 487f26e09c8Sdrh } 488b84f96f8Sdanielk1977 #endif 489b84f96f8Sdanielk1977 490b84f96f8Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 4914adee20fSdanielk1977 int sqlite3FixTriggerStep( 492f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 493f26e09c8Sdrh TriggerStep *pStep /* The trigger step be fixed to one database */ 494f26e09c8Sdrh ){ 495f26e09c8Sdrh while( pStep ){ 4964adee20fSdanielk1977 if( sqlite3FixSelect(pFix, pStep->pSelect) ){ 497f26e09c8Sdrh return 1; 498f26e09c8Sdrh } 4994adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pStep->pWhere) ){ 500f26e09c8Sdrh return 1; 501f26e09c8Sdrh } 5024adee20fSdanielk1977 if( sqlite3FixExprList(pFix, pStep->pExprList) ){ 503f26e09c8Sdrh return 1; 504f26e09c8Sdrh } 505f26e09c8Sdrh pStep = pStep->pNext; 506f26e09c8Sdrh } 507f26e09c8Sdrh return 0; 508f26e09c8Sdrh } 509b84f96f8Sdanielk1977 #endif 510