xref: /sqlite-3.40.0/src/attach.c (revision 8a41449e)
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*8a41449eSdanielk1977 ** $Id: attach.c,v 1.20 2004/06/29 08:59:35 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;
8791cf71b0Sdanielk1977   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 ){
112*8a41449eSdanielk1977     rc = sqlite3ReadSchema(pParse);
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);
122*8a41449eSdanielk1977     if( 0==pParse->nErr ){
123c11d4f93Sdrh       pParse->nErr++;
124c11d4f93Sdrh       pParse->rc = SQLITE_ERROR;
125c11d4f93Sdrh     }
126c11d4f93Sdrh   }
127*8a41449eSdanielk1977 }
128c11d4f93Sdrh 
129c11d4f93Sdrh /*
130c11d4f93Sdrh ** This routine is called by the parser to process a DETACH statement:
131c11d4f93Sdrh **
132c11d4f93Sdrh **    DETACH DATABASE dbname
133c11d4f93Sdrh **
134c11d4f93Sdrh ** The pDbname argument is the name of the database in the DETACH statement.
135c11d4f93Sdrh */
1364adee20fSdanielk1977 void sqlite3Detach(Parse *pParse, Token *pDbname){
137c11d4f93Sdrh   int i;
138c11d4f93Sdrh   sqlite *db;
1390bca3530Sdrh   Vdbe *v;
140e0d4b060Sdanielk1977   Db *pDb = 0;
141c11d4f93Sdrh 
1424adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
1434adee20fSdanielk1977   sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
144c11d4f93Sdrh   if( pParse->explain ) return;
145c11d4f93Sdrh   db = pParse->db;
146c11d4f93Sdrh   for(i=0; i<db->nDb; i++){
147124b27e6Sdrh     pDb = &db->aDb[i];
148124b27e6Sdrh     if( pDb->pBt==0 || pDb->zName==0 ) continue;
149124b27e6Sdrh     if( strlen(pDb->zName)!=pDbname->n ) continue;
150124b27e6Sdrh     if( sqlite3StrNICmp(pDb->zName, pDbname->z, pDbname->n)==0 ) break;
151c11d4f93Sdrh   }
152c11d4f93Sdrh   if( i>=db->nDb ){
1534adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "no such database: %T", pDbname);
154c11d4f93Sdrh     return;
155c11d4f93Sdrh   }
156c11d4f93Sdrh   if( i<2 ){
1574adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "cannot detach database %T", pDbname);
158c11d4f93Sdrh     return;
159c11d4f93Sdrh   }
16092f9a1bbSdanielk1977   if( !db->autoCommit ){
16192f9a1bbSdanielk1977     sqlite3ErrorMsg(pParse, "cannot DETACH database within transaction");
16292f9a1bbSdanielk1977     pParse->rc = SQLITE_ERROR;
16392f9a1bbSdanielk1977     return;
16492f9a1bbSdanielk1977   }
16581e293b4Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION
1664adee20fSdanielk1977   if( sqlite3AuthCheck(pParse,SQLITE_DETACH,db->aDb[i].zName,0,0)!=SQLITE_OK ){
16781e293b4Sdrh     return;
16881e293b4Sdrh   }
16981e293b4Sdrh #endif /* SQLITE_OMIT_AUTHORIZATION */
170124b27e6Sdrh   sqlite3BtreeClose(pDb->pBt);
171124b27e6Sdrh   pDb->pBt = 0;
172124b27e6Sdrh   sqliteFree(pDb->zName);
1734adee20fSdanielk1977   sqlite3ResetInternalSchema(db, i);
174c11d4f93Sdrh   db->nDb--;
175c11d4f93Sdrh   if( i<db->nDb ){
176c11d4f93Sdrh     db->aDb[i] = db->aDb[db->nDb];
177c11d4f93Sdrh     memset(&db->aDb[db->nDb], 0, sizeof(db->aDb[0]));
1784adee20fSdanielk1977     sqlite3ResetInternalSchema(db, i);
179c11d4f93Sdrh   }
180c11d4f93Sdrh }
181f26e09c8Sdrh 
182f26e09c8Sdrh /*
183f26e09c8Sdrh ** Initialize a DbFixer structure.  This routine must be called prior
184f26e09c8Sdrh ** to passing the structure to one of the sqliteFixAAAA() routines below.
185f26e09c8Sdrh **
186f26e09c8Sdrh ** The return value indicates whether or not fixation is required.  TRUE
187f26e09c8Sdrh ** means we do need to fix the database references, FALSE means we do not.
188f26e09c8Sdrh */
1894adee20fSdanielk1977 int sqlite3FixInit(
190f26e09c8Sdrh   DbFixer *pFix,      /* The fixer to be initialized */
191f26e09c8Sdrh   Parse *pParse,      /* Error messages will be written here */
192f26e09c8Sdrh   int iDb,            /* This is the database that must must be used */
193f26e09c8Sdrh   const char *zType,  /* "view", "trigger", or "index" */
194f26e09c8Sdrh   const Token *pName  /* Name of the view, trigger, or index */
195f26e09c8Sdrh ){
196f26e09c8Sdrh   sqlite *db;
197f26e09c8Sdrh 
198f26e09c8Sdrh   if( iDb<0 || iDb==1 ) return 0;
199f26e09c8Sdrh   db = pParse->db;
200f26e09c8Sdrh   assert( db->nDb>iDb );
2014312db55Sdrh   pFix->pParse = pParse;
202f26e09c8Sdrh   pFix->zDb = db->aDb[iDb].zName;
203f26e09c8Sdrh   pFix->zType = zType;
204f26e09c8Sdrh   pFix->pName = pName;
205f26e09c8Sdrh   return 1;
206f26e09c8Sdrh }
207f26e09c8Sdrh 
208f26e09c8Sdrh /*
209f26e09c8Sdrh ** The following set of routines walk through the parse tree and assign
210f26e09c8Sdrh ** a specific database to all table references where the database name
211f26e09c8Sdrh ** was left unspecified in the original SQL statement.  The pFix structure
2124adee20fSdanielk1977 ** must have been initialized by a prior call to sqlite3FixInit().
213f26e09c8Sdrh **
214f26e09c8Sdrh ** These routines are used to make sure that an index, trigger, or
215f26e09c8Sdrh ** view in one database does not refer to objects in a different database.
216f26e09c8Sdrh ** (Exception: indices, triggers, and views in the TEMP database are
217f26e09c8Sdrh ** allowed to refer to anything.)  If a reference is explicitly made
218f26e09c8Sdrh ** to an object in a different database, an error message is added to
219f26e09c8Sdrh ** pParse->zErrMsg and these routines return non-zero.  If everything
220f26e09c8Sdrh ** checks out, these routines return 0.
221f26e09c8Sdrh */
2224adee20fSdanielk1977 int sqlite3FixSrcList(
223f26e09c8Sdrh   DbFixer *pFix,       /* Context of the fixation */
224f26e09c8Sdrh   SrcList *pList       /* The Source list to check and modify */
225f26e09c8Sdrh ){
226f26e09c8Sdrh   int i;
227f26e09c8Sdrh   const char *zDb;
228f26e09c8Sdrh 
229f26e09c8Sdrh   if( pList==0 ) return 0;
230f26e09c8Sdrh   zDb = pFix->zDb;
231f26e09c8Sdrh   for(i=0; i<pList->nSrc; i++){
232f26e09c8Sdrh     if( pList->a[i].zDatabase==0 ){
233f26e09c8Sdrh       pList->a[i].zDatabase = sqliteStrDup(zDb);
2344adee20fSdanielk1977     }else if( sqlite3StrICmp(pList->a[i].zDatabase,zDb)!=0 ){
2354adee20fSdanielk1977       sqlite3ErrorMsg(pFix->pParse,
2364312db55Sdrh          "%s %z cannot reference objects in database %s",
2374312db55Sdrh          pFix->zType, sqliteStrNDup(pFix->pName->z, pFix->pName->n),
2384312db55Sdrh          pList->a[i].zDatabase);
239f26e09c8Sdrh       return 1;
240f26e09c8Sdrh     }
2414adee20fSdanielk1977     if( sqlite3FixSelect(pFix, pList->a[i].pSelect) ) return 1;
2424adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pList->a[i].pOn) ) return 1;
243f26e09c8Sdrh   }
244f26e09c8Sdrh   return 0;
245f26e09c8Sdrh }
2464adee20fSdanielk1977 int sqlite3FixSelect(
247f26e09c8Sdrh   DbFixer *pFix,       /* Context of the fixation */
248f26e09c8Sdrh   Select *pSelect      /* The SELECT statement to be fixed to one database */
249f26e09c8Sdrh ){
250f26e09c8Sdrh   while( pSelect ){
2514adee20fSdanielk1977     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
252f26e09c8Sdrh       return 1;
253f26e09c8Sdrh     }
2544adee20fSdanielk1977     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
255f26e09c8Sdrh       return 1;
256f26e09c8Sdrh     }
2574adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
258f26e09c8Sdrh       return 1;
259f26e09c8Sdrh     }
2604adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
261f26e09c8Sdrh       return 1;
262f26e09c8Sdrh     }
263f26e09c8Sdrh     pSelect = pSelect->pPrior;
264f26e09c8Sdrh   }
265f26e09c8Sdrh   return 0;
266f26e09c8Sdrh }
2674adee20fSdanielk1977 int sqlite3FixExpr(
268f26e09c8Sdrh   DbFixer *pFix,     /* Context of the fixation */
269f26e09c8Sdrh   Expr *pExpr        /* The expression to be fixed to one database */
270f26e09c8Sdrh ){
271f26e09c8Sdrh   while( pExpr ){
2724adee20fSdanielk1977     if( sqlite3FixSelect(pFix, pExpr->pSelect) ){
273f26e09c8Sdrh       return 1;
274f26e09c8Sdrh     }
2754adee20fSdanielk1977     if( sqlite3FixExprList(pFix, pExpr->pList) ){
276f26e09c8Sdrh       return 1;
277f26e09c8Sdrh     }
2784adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
279f26e09c8Sdrh       return 1;
280f26e09c8Sdrh     }
281f26e09c8Sdrh     pExpr = pExpr->pLeft;
282f26e09c8Sdrh   }
283f26e09c8Sdrh   return 0;
284f26e09c8Sdrh }
2854adee20fSdanielk1977 int sqlite3FixExprList(
286f26e09c8Sdrh   DbFixer *pFix,     /* Context of the fixation */
287f26e09c8Sdrh   ExprList *pList    /* The expression to be fixed to one database */
288f26e09c8Sdrh ){
289f26e09c8Sdrh   int i;
290f26e09c8Sdrh   if( pList==0 ) return 0;
291f26e09c8Sdrh   for(i=0; i<pList->nExpr; i++){
2924adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pList->a[i].pExpr) ){
293f26e09c8Sdrh       return 1;
294f26e09c8Sdrh     }
295f26e09c8Sdrh   }
296f26e09c8Sdrh   return 0;
297f26e09c8Sdrh }
2984adee20fSdanielk1977 int sqlite3FixTriggerStep(
299f26e09c8Sdrh   DbFixer *pFix,     /* Context of the fixation */
300f26e09c8Sdrh   TriggerStep *pStep /* The trigger step be fixed to one database */
301f26e09c8Sdrh ){
302f26e09c8Sdrh   while( pStep ){
3034adee20fSdanielk1977     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
304f26e09c8Sdrh       return 1;
305f26e09c8Sdrh     }
3064adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
307f26e09c8Sdrh       return 1;
308f26e09c8Sdrh     }
3094adee20fSdanielk1977     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
310f26e09c8Sdrh       return 1;
311f26e09c8Sdrh     }
312f26e09c8Sdrh     pStep = pStep->pNext;
313f26e09c8Sdrh   }
314f26e09c8Sdrh   return 0;
315f26e09c8Sdrh }
316