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*91cf71b0Sdanielk1977 ** $Id: attach.c,v 1.18 2004/06/26 06:37:07 danielk1977 Exp $ 15c11d4f93Sdrh */ 16c11d4f93Sdrh #include "sqliteInt.h" 17c11d4f93Sdrh 18c11d4f93Sdrh /* 19c11d4f93Sdrh ** This routine is called by the parser to process an ATTACH statement: 20c11d4f93Sdrh ** 21c11d4f93Sdrh ** ATTACH DATABASE filename AS dbname 22c11d4f93Sdrh ** 23c11d4f93Sdrh ** The pFilename and pDbname arguments are the tokens that define the 24c11d4f93Sdrh ** filename and dbname in the ATTACH statement. 25c11d4f93Sdrh */ 264adee20fSdanielk1977 void sqlite3Attach(Parse *pParse, Token *pFilename, Token *pDbname, Token *pKey){ 27c11d4f93Sdrh Db *aNew; 28c11d4f93Sdrh int rc, i; 29c11d4f93Sdrh char *zFile, *zName; 30c11d4f93Sdrh sqlite *db; 310bca3530Sdrh Vdbe *v; 32c11d4f93Sdrh 334adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 344adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Halt, 0, 0); 35c11d4f93Sdrh if( pParse->explain ) return; 36c11d4f93Sdrh db = pParse->db; 37c11d4f93Sdrh if( db->nDb>=MAX_ATTACHED+2 ){ 384adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "too many attached databases - max %d", 39c11d4f93Sdrh MAX_ATTACHED); 40c11d4f93Sdrh pParse->rc = SQLITE_ERROR; 41c11d4f93Sdrh return; 42c11d4f93Sdrh } 4381e293b4Sdrh 4492f9a1bbSdanielk1977 if( !db->autoCommit ){ 4592f9a1bbSdanielk1977 sqlite3ErrorMsg(pParse, "cannot ATTACH database within transaction"); 4692f9a1bbSdanielk1977 pParse->rc = SQLITE_ERROR; 4792f9a1bbSdanielk1977 return; 4892f9a1bbSdanielk1977 } 4992f9a1bbSdanielk1977 50a99db3b6Sdrh zFile = sqlite3NameFromToken(pFilename);; 5181e293b4Sdrh if( zFile==0 ) return; 5281e293b4Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION 534adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_ATTACH, zFile, 0, 0)!=SQLITE_OK ){ 5481e293b4Sdrh sqliteFree(zFile); 5581e293b4Sdrh return; 5681e293b4Sdrh } 5781e293b4Sdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 5881e293b4Sdrh 59a99db3b6Sdrh zName = sqlite3NameFromToken(pDbname); 6081e293b4Sdrh if( zName==0 ) return; 6181e293b4Sdrh for(i=0; i<db->nDb; i++){ 62124b27e6Sdrh char *z = db->aDb[i].zName; 63124b27e6Sdrh if( z && sqlite3StrICmp(z, zName)==0 ){ 644adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "database %z is already in use", zName); 6581e293b4Sdrh pParse->rc = SQLITE_ERROR; 6681e293b4Sdrh sqliteFree(zFile); 6781e293b4Sdrh return; 6881e293b4Sdrh } 6981e293b4Sdrh } 7081e293b4Sdrh 71c11d4f93Sdrh if( db->aDb==db->aDbStatic ){ 72c11d4f93Sdrh aNew = sqliteMalloc( sizeof(db->aDb[0])*3 ); 73c11d4f93Sdrh if( aNew==0 ) return; 74c11d4f93Sdrh memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); 75c11d4f93Sdrh }else{ 76c11d4f93Sdrh aNew = sqliteRealloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); 77c11d4f93Sdrh if( aNew==0 ) return; 78c11d4f93Sdrh } 79c11d4f93Sdrh db->aDb = aNew; 80c11d4f93Sdrh aNew = &db->aDb[db->nDb++]; 81c11d4f93Sdrh memset(aNew, 0, sizeof(*aNew)); 824adee20fSdanielk1977 sqlite3HashInit(&aNew->tblHash, SQLITE_HASH_STRING, 0); 834adee20fSdanielk1977 sqlite3HashInit(&aNew->idxHash, SQLITE_HASH_STRING, 0); 844adee20fSdanielk1977 sqlite3HashInit(&aNew->trigHash, SQLITE_HASH_STRING, 0); 854adee20fSdanielk1977 sqlite3HashInit(&aNew->aFKey, SQLITE_HASH_STRING, 1); 86c11d4f93Sdrh aNew->zName = zName; 87*91cf71b0Sdanielk1977 aNew->safety_level = 3; 884adee20fSdanielk1977 rc = sqlite3BtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt); 89c11d4f93Sdrh if( rc ){ 904adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "unable to open database: %s", zFile); 91c11d4f93Sdrh } 924d189ca4Sdrh #if SQLITE_HAS_CODEC 934d189ca4Sdrh { 944d189ca4Sdrh extern int sqliteCodecAttach(sqlite*, int, void*, int); 954d189ca4Sdrh char *zKey = 0; 964d189ca4Sdrh int nKey; 974d189ca4Sdrh if( pKey && pKey->z && pKey->n ){ 984adee20fSdanielk1977 sqlite3SetNString(&zKey, pKey->z, pKey->n, 0); 994adee20fSdanielk1977 sqlite3Dequote(zKey); 1004d189ca4Sdrh nKey = strlen(zKey); 1014d189ca4Sdrh }else{ 1024d189ca4Sdrh zKey = 0; 1034d189ca4Sdrh nKey = 0; 1044d189ca4Sdrh } 1054d189ca4Sdrh sqliteCodecAttach(db, db->nDb-1, zKey, nKey); 1064d189ca4Sdrh } 1074d189ca4Sdrh #endif 108c11d4f93Sdrh sqliteFree(zFile); 109c11d4f93Sdrh db->flags &= ~SQLITE_Initialized; 110c11d4f93Sdrh if( pParse->nErr ) return; 111447623d9Sdrh if( rc==SQLITE_OK ){ 112c0391398Sdanielk1977 rc = sqlite3ReadSchema(pParse->db, &pParse->zErrMsg); 113447623d9Sdrh } 114c11d4f93Sdrh if( rc ){ 115447623d9Sdrh int i = db->nDb - 1; 116447623d9Sdrh assert( i>=2 ); 117447623d9Sdrh if( db->aDb[i].pBt ){ 1184adee20fSdanielk1977 sqlite3BtreeClose(db->aDb[i].pBt); 119447623d9Sdrh db->aDb[i].pBt = 0; 120447623d9Sdrh } 1214adee20fSdanielk1977 sqlite3ResetInternalSchema(db, 0); 122c11d4f93Sdrh pParse->nErr++; 123c11d4f93Sdrh pParse->rc = SQLITE_ERROR; 124c11d4f93Sdrh } 125c11d4f93Sdrh } 126c11d4f93Sdrh 127c11d4f93Sdrh /* 128c11d4f93Sdrh ** This routine is called by the parser to process a DETACH statement: 129c11d4f93Sdrh ** 130c11d4f93Sdrh ** DETACH DATABASE dbname 131c11d4f93Sdrh ** 132c11d4f93Sdrh ** The pDbname argument is the name of the database in the DETACH statement. 133c11d4f93Sdrh */ 1344adee20fSdanielk1977 void sqlite3Detach(Parse *pParse, Token *pDbname){ 135c11d4f93Sdrh int i; 136c11d4f93Sdrh sqlite *db; 1370bca3530Sdrh Vdbe *v; 138124b27e6Sdrh Db *pDb; 139c11d4f93Sdrh 1404adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 1414adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Halt, 0, 0); 142c11d4f93Sdrh if( pParse->explain ) return; 143c11d4f93Sdrh db = pParse->db; 144c11d4f93Sdrh for(i=0; i<db->nDb; i++){ 145124b27e6Sdrh pDb = &db->aDb[i]; 146124b27e6Sdrh if( pDb->pBt==0 || pDb->zName==0 ) continue; 147124b27e6Sdrh if( strlen(pDb->zName)!=pDbname->n ) continue; 148124b27e6Sdrh if( sqlite3StrNICmp(pDb->zName, pDbname->z, pDbname->n)==0 ) break; 149c11d4f93Sdrh } 150c11d4f93Sdrh if( i>=db->nDb ){ 1514adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no such database: %T", pDbname); 152c11d4f93Sdrh return; 153c11d4f93Sdrh } 154c11d4f93Sdrh if( i<2 ){ 1554adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "cannot detach database %T", pDbname); 156c11d4f93Sdrh return; 157c11d4f93Sdrh } 15892f9a1bbSdanielk1977 if( !db->autoCommit ){ 15992f9a1bbSdanielk1977 sqlite3ErrorMsg(pParse, "cannot DETACH database within transaction"); 16092f9a1bbSdanielk1977 pParse->rc = SQLITE_ERROR; 16192f9a1bbSdanielk1977 return; 16292f9a1bbSdanielk1977 } 16381e293b4Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION 1644adee20fSdanielk1977 if( sqlite3AuthCheck(pParse,SQLITE_DETACH,db->aDb[i].zName,0,0)!=SQLITE_OK ){ 16581e293b4Sdrh return; 16681e293b4Sdrh } 16781e293b4Sdrh #endif /* SQLITE_OMIT_AUTHORIZATION */ 168124b27e6Sdrh sqlite3BtreeClose(pDb->pBt); 169124b27e6Sdrh pDb->pBt = 0; 170124b27e6Sdrh sqliteFree(pDb->zName); 1714adee20fSdanielk1977 sqlite3ResetInternalSchema(db, i); 172c11d4f93Sdrh db->nDb--; 173c11d4f93Sdrh if( i<db->nDb ){ 174c11d4f93Sdrh db->aDb[i] = db->aDb[db->nDb]; 175c11d4f93Sdrh memset(&db->aDb[db->nDb], 0, sizeof(db->aDb[0])); 1764adee20fSdanielk1977 sqlite3ResetInternalSchema(db, i); 177c11d4f93Sdrh } 178c11d4f93Sdrh } 179f26e09c8Sdrh 180f26e09c8Sdrh /* 181f26e09c8Sdrh ** Initialize a DbFixer structure. This routine must be called prior 182f26e09c8Sdrh ** to passing the structure to one of the sqliteFixAAAA() routines below. 183f26e09c8Sdrh ** 184f26e09c8Sdrh ** The return value indicates whether or not fixation is required. TRUE 185f26e09c8Sdrh ** means we do need to fix the database references, FALSE means we do not. 186f26e09c8Sdrh */ 1874adee20fSdanielk1977 int sqlite3FixInit( 188f26e09c8Sdrh DbFixer *pFix, /* The fixer to be initialized */ 189f26e09c8Sdrh Parse *pParse, /* Error messages will be written here */ 190f26e09c8Sdrh int iDb, /* This is the database that must must be used */ 191f26e09c8Sdrh const char *zType, /* "view", "trigger", or "index" */ 192f26e09c8Sdrh const Token *pName /* Name of the view, trigger, or index */ 193f26e09c8Sdrh ){ 194f26e09c8Sdrh sqlite *db; 195f26e09c8Sdrh 196f26e09c8Sdrh if( iDb<0 || iDb==1 ) return 0; 197f26e09c8Sdrh db = pParse->db; 198f26e09c8Sdrh assert( db->nDb>iDb ); 1994312db55Sdrh pFix->pParse = pParse; 200f26e09c8Sdrh pFix->zDb = db->aDb[iDb].zName; 201f26e09c8Sdrh pFix->zType = zType; 202f26e09c8Sdrh pFix->pName = pName; 203f26e09c8Sdrh return 1; 204f26e09c8Sdrh } 205f26e09c8Sdrh 206f26e09c8Sdrh /* 207f26e09c8Sdrh ** The following set of routines walk through the parse tree and assign 208f26e09c8Sdrh ** a specific database to all table references where the database name 209f26e09c8Sdrh ** was left unspecified in the original SQL statement. The pFix structure 2104adee20fSdanielk1977 ** must have been initialized by a prior call to sqlite3FixInit(). 211f26e09c8Sdrh ** 212f26e09c8Sdrh ** These routines are used to make sure that an index, trigger, or 213f26e09c8Sdrh ** view in one database does not refer to objects in a different database. 214f26e09c8Sdrh ** (Exception: indices, triggers, and views in the TEMP database are 215f26e09c8Sdrh ** allowed to refer to anything.) If a reference is explicitly made 216f26e09c8Sdrh ** to an object in a different database, an error message is added to 217f26e09c8Sdrh ** pParse->zErrMsg and these routines return non-zero. If everything 218f26e09c8Sdrh ** checks out, these routines return 0. 219f26e09c8Sdrh */ 2204adee20fSdanielk1977 int sqlite3FixSrcList( 221f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 222f26e09c8Sdrh SrcList *pList /* The Source list to check and modify */ 223f26e09c8Sdrh ){ 224f26e09c8Sdrh int i; 225f26e09c8Sdrh const char *zDb; 226f26e09c8Sdrh 227f26e09c8Sdrh if( pList==0 ) return 0; 228f26e09c8Sdrh zDb = pFix->zDb; 229f26e09c8Sdrh for(i=0; i<pList->nSrc; i++){ 230f26e09c8Sdrh if( pList->a[i].zDatabase==0 ){ 231f26e09c8Sdrh pList->a[i].zDatabase = sqliteStrDup(zDb); 2324adee20fSdanielk1977 }else if( sqlite3StrICmp(pList->a[i].zDatabase,zDb)!=0 ){ 2334adee20fSdanielk1977 sqlite3ErrorMsg(pFix->pParse, 2344312db55Sdrh "%s %z cannot reference objects in database %s", 2354312db55Sdrh pFix->zType, sqliteStrNDup(pFix->pName->z, pFix->pName->n), 2364312db55Sdrh pList->a[i].zDatabase); 237f26e09c8Sdrh return 1; 238f26e09c8Sdrh } 2394adee20fSdanielk1977 if( sqlite3FixSelect(pFix, pList->a[i].pSelect) ) return 1; 2404adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pList->a[i].pOn) ) return 1; 241f26e09c8Sdrh } 242f26e09c8Sdrh return 0; 243f26e09c8Sdrh } 2444adee20fSdanielk1977 int sqlite3FixSelect( 245f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 246f26e09c8Sdrh Select *pSelect /* The SELECT statement to be fixed to one database */ 247f26e09c8Sdrh ){ 248f26e09c8Sdrh while( pSelect ){ 2494adee20fSdanielk1977 if( sqlite3FixExprList(pFix, pSelect->pEList) ){ 250f26e09c8Sdrh return 1; 251f26e09c8Sdrh } 2524adee20fSdanielk1977 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ 253f26e09c8Sdrh return 1; 254f26e09c8Sdrh } 2554adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ 256f26e09c8Sdrh return 1; 257f26e09c8Sdrh } 2584adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ 259f26e09c8Sdrh return 1; 260f26e09c8Sdrh } 261f26e09c8Sdrh pSelect = pSelect->pPrior; 262f26e09c8Sdrh } 263f26e09c8Sdrh return 0; 264f26e09c8Sdrh } 2654adee20fSdanielk1977 int sqlite3FixExpr( 266f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 267f26e09c8Sdrh Expr *pExpr /* The expression to be fixed to one database */ 268f26e09c8Sdrh ){ 269f26e09c8Sdrh while( pExpr ){ 2704adee20fSdanielk1977 if( sqlite3FixSelect(pFix, pExpr->pSelect) ){ 271f26e09c8Sdrh return 1; 272f26e09c8Sdrh } 2734adee20fSdanielk1977 if( sqlite3FixExprList(pFix, pExpr->pList) ){ 274f26e09c8Sdrh return 1; 275f26e09c8Sdrh } 2764adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pExpr->pRight) ){ 277f26e09c8Sdrh return 1; 278f26e09c8Sdrh } 279f26e09c8Sdrh pExpr = pExpr->pLeft; 280f26e09c8Sdrh } 281f26e09c8Sdrh return 0; 282f26e09c8Sdrh } 2834adee20fSdanielk1977 int sqlite3FixExprList( 284f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 285f26e09c8Sdrh ExprList *pList /* The expression to be fixed to one database */ 286f26e09c8Sdrh ){ 287f26e09c8Sdrh int i; 288f26e09c8Sdrh if( pList==0 ) return 0; 289f26e09c8Sdrh for(i=0; i<pList->nExpr; i++){ 2904adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pList->a[i].pExpr) ){ 291f26e09c8Sdrh return 1; 292f26e09c8Sdrh } 293f26e09c8Sdrh } 294f26e09c8Sdrh return 0; 295f26e09c8Sdrh } 2964adee20fSdanielk1977 int sqlite3FixTriggerStep( 297f26e09c8Sdrh DbFixer *pFix, /* Context of the fixation */ 298f26e09c8Sdrh TriggerStep *pStep /* The trigger step be fixed to one database */ 299f26e09c8Sdrh ){ 300f26e09c8Sdrh while( pStep ){ 3014adee20fSdanielk1977 if( sqlite3FixSelect(pFix, pStep->pSelect) ){ 302f26e09c8Sdrh return 1; 303f26e09c8Sdrh } 3044adee20fSdanielk1977 if( sqlite3FixExpr(pFix, pStep->pWhere) ){ 305f26e09c8Sdrh return 1; 306f26e09c8Sdrh } 3074adee20fSdanielk1977 if( sqlite3FixExprList(pFix, pStep->pExprList) ){ 308f26e09c8Sdrh return 1; 309f26e09c8Sdrh } 310f26e09c8Sdrh pStep = pStep->pNext; 311f26e09c8Sdrh } 312f26e09c8Sdrh return 0; 313f26e09c8Sdrh } 314