xref: /sqlite-3.40.0/src/attach.c (revision c0391398)
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*c0391398Sdanielk1977 ** $Id: attach.c,v 1.14 2004/06/09 12:30:05 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 
4481e293b4Sdrh   zFile = 0;
454adee20fSdanielk1977   sqlite3SetNString(&zFile, pFilename->z, pFilename->n, 0);
4681e293b4Sdrh   if( zFile==0 ) return;
474adee20fSdanielk1977   sqlite3Dequote(zFile);
4881e293b4Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION
494adee20fSdanielk1977   if( sqlite3AuthCheck(pParse, SQLITE_ATTACH, zFile, 0, 0)!=SQLITE_OK ){
5081e293b4Sdrh     sqliteFree(zFile);
5181e293b4Sdrh     return;
5281e293b4Sdrh   }
5381e293b4Sdrh #endif /* SQLITE_OMIT_AUTHORIZATION */
5481e293b4Sdrh 
5581e293b4Sdrh   zName = 0;
564adee20fSdanielk1977   sqlite3SetNString(&zName, pDbname->z, pDbname->n, 0);
5781e293b4Sdrh   if( zName==0 ) return;
584adee20fSdanielk1977   sqlite3Dequote(zName);
5981e293b4Sdrh   for(i=0; i<db->nDb; i++){
604adee20fSdanielk1977     if( db->aDb[i].zName && sqlite3StrICmp(db->aDb[i].zName, zName)==0 ){
614adee20fSdanielk1977       sqlite3ErrorMsg(pParse, "database %z is already in use", zName);
6281e293b4Sdrh       pParse->rc = SQLITE_ERROR;
6381e293b4Sdrh       sqliteFree(zFile);
6481e293b4Sdrh       return;
6581e293b4Sdrh     }
6681e293b4Sdrh   }
6781e293b4Sdrh 
68c11d4f93Sdrh   if( db->aDb==db->aDbStatic ){
69c11d4f93Sdrh     aNew = sqliteMalloc( sizeof(db->aDb[0])*3 );
70c11d4f93Sdrh     if( aNew==0 ) return;
71c11d4f93Sdrh     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
72c11d4f93Sdrh   }else{
73c11d4f93Sdrh     aNew = sqliteRealloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
74c11d4f93Sdrh     if( aNew==0 ) return;
75c11d4f93Sdrh   }
76c11d4f93Sdrh   db->aDb = aNew;
77c11d4f93Sdrh   aNew = &db->aDb[db->nDb++];
78c11d4f93Sdrh   memset(aNew, 0, sizeof(*aNew));
794adee20fSdanielk1977   sqlite3HashInit(&aNew->tblHash, SQLITE_HASH_STRING, 0);
804adee20fSdanielk1977   sqlite3HashInit(&aNew->idxHash, SQLITE_HASH_STRING, 0);
814adee20fSdanielk1977   sqlite3HashInit(&aNew->trigHash, SQLITE_HASH_STRING, 0);
824adee20fSdanielk1977   sqlite3HashInit(&aNew->aFKey, SQLITE_HASH_STRING, 1);
83c11d4f93Sdrh   aNew->zName = zName;
844adee20fSdanielk1977   rc = sqlite3BtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt);
85c11d4f93Sdrh   if( rc ){
864adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "unable to open database: %s", zFile);
87c11d4f93Sdrh   }
884d189ca4Sdrh #if SQLITE_HAS_CODEC
894d189ca4Sdrh   {
904d189ca4Sdrh     extern int sqliteCodecAttach(sqlite*, int, void*, int);
914d189ca4Sdrh     char *zKey = 0;
924d189ca4Sdrh     int nKey;
934d189ca4Sdrh     if( pKey && pKey->z && pKey->n ){
944adee20fSdanielk1977       sqlite3SetNString(&zKey, pKey->z, pKey->n, 0);
954adee20fSdanielk1977       sqlite3Dequote(zKey);
964d189ca4Sdrh       nKey = strlen(zKey);
974d189ca4Sdrh     }else{
984d189ca4Sdrh       zKey = 0;
994d189ca4Sdrh       nKey = 0;
1004d189ca4Sdrh     }
1014d189ca4Sdrh     sqliteCodecAttach(db, db->nDb-1, zKey, nKey);
1024d189ca4Sdrh   }
1034d189ca4Sdrh #endif
104c11d4f93Sdrh   sqliteFree(zFile);
105c11d4f93Sdrh   db->flags &= ~SQLITE_Initialized;
106c11d4f93Sdrh   if( pParse->nErr ) return;
107447623d9Sdrh   if( rc==SQLITE_OK ){
108*c0391398Sdanielk1977     rc = sqlite3ReadSchema(pParse->db, &pParse->zErrMsg);
109447623d9Sdrh   }
110c11d4f93Sdrh   if( rc ){
111447623d9Sdrh     int i = db->nDb - 1;
112447623d9Sdrh     assert( i>=2 );
113447623d9Sdrh     if( db->aDb[i].pBt ){
1144adee20fSdanielk1977       sqlite3BtreeClose(db->aDb[i].pBt);
115447623d9Sdrh       db->aDb[i].pBt = 0;
116447623d9Sdrh     }
1174adee20fSdanielk1977     sqlite3ResetInternalSchema(db, 0);
118c11d4f93Sdrh     pParse->nErr++;
119c11d4f93Sdrh     pParse->rc = SQLITE_ERROR;
120c11d4f93Sdrh   }
121c11d4f93Sdrh }
122c11d4f93Sdrh 
123c11d4f93Sdrh /*
124c11d4f93Sdrh ** This routine is called by the parser to process a DETACH statement:
125c11d4f93Sdrh **
126c11d4f93Sdrh **    DETACH DATABASE dbname
127c11d4f93Sdrh **
128c11d4f93Sdrh ** The pDbname argument is the name of the database in the DETACH statement.
129c11d4f93Sdrh */
1304adee20fSdanielk1977 void sqlite3Detach(Parse *pParse, Token *pDbname){
131c11d4f93Sdrh   int i;
132c11d4f93Sdrh   sqlite *db;
1330bca3530Sdrh   Vdbe *v;
134c11d4f93Sdrh 
1354adee20fSdanielk1977   v = sqlite3GetVdbe(pParse);
1364adee20fSdanielk1977   sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
137c11d4f93Sdrh   if( pParse->explain ) return;
138c11d4f93Sdrh   db = pParse->db;
139c11d4f93Sdrh   for(i=0; i<db->nDb; i++){
140c11d4f93Sdrh     if( db->aDb[i].pBt==0 || db->aDb[i].zName==0 ) continue;
141c11d4f93Sdrh     if( strlen(db->aDb[i].zName)!=pDbname->n ) continue;
1424adee20fSdanielk1977     if( sqlite3StrNICmp(db->aDb[i].zName, pDbname->z, pDbname->n)==0 ) break;
143c11d4f93Sdrh   }
144c11d4f93Sdrh   if( i>=db->nDb ){
1454adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "no such database: %T", pDbname);
146c11d4f93Sdrh     return;
147c11d4f93Sdrh   }
148c11d4f93Sdrh   if( i<2 ){
1494adee20fSdanielk1977     sqlite3ErrorMsg(pParse, "cannot detach database %T", pDbname);
150c11d4f93Sdrh     return;
151c11d4f93Sdrh   }
15281e293b4Sdrh #ifndef SQLITE_OMIT_AUTHORIZATION
1534adee20fSdanielk1977   if( sqlite3AuthCheck(pParse,SQLITE_DETACH,db->aDb[i].zName,0,0)!=SQLITE_OK ){
15481e293b4Sdrh     return;
15581e293b4Sdrh   }
15681e293b4Sdrh #endif /* SQLITE_OMIT_AUTHORIZATION */
1574adee20fSdanielk1977   sqlite3BtreeClose(db->aDb[i].pBt);
158c11d4f93Sdrh   db->aDb[i].pBt = 0;
15970ce3f0cSdrh   sqliteFree(db->aDb[i].zName);
1604adee20fSdanielk1977   sqlite3ResetInternalSchema(db, i);
161c11d4f93Sdrh   db->nDb--;
162c11d4f93Sdrh   if( i<db->nDb ){
163c11d4f93Sdrh     db->aDb[i] = db->aDb[db->nDb];
164c11d4f93Sdrh     memset(&db->aDb[db->nDb], 0, sizeof(db->aDb[0]));
1654adee20fSdanielk1977     sqlite3ResetInternalSchema(db, i);
166c11d4f93Sdrh   }
167c11d4f93Sdrh }
168f26e09c8Sdrh 
169f26e09c8Sdrh /*
170f26e09c8Sdrh ** Initialize a DbFixer structure.  This routine must be called prior
171f26e09c8Sdrh ** to passing the structure to one of the sqliteFixAAAA() routines below.
172f26e09c8Sdrh **
173f26e09c8Sdrh ** The return value indicates whether or not fixation is required.  TRUE
174f26e09c8Sdrh ** means we do need to fix the database references, FALSE means we do not.
175f26e09c8Sdrh */
1764adee20fSdanielk1977 int sqlite3FixInit(
177f26e09c8Sdrh   DbFixer *pFix,      /* The fixer to be initialized */
178f26e09c8Sdrh   Parse *pParse,      /* Error messages will be written here */
179f26e09c8Sdrh   int iDb,            /* This is the database that must must be used */
180f26e09c8Sdrh   const char *zType,  /* "view", "trigger", or "index" */
181f26e09c8Sdrh   const Token *pName  /* Name of the view, trigger, or index */
182f26e09c8Sdrh ){
183f26e09c8Sdrh   sqlite *db;
184f26e09c8Sdrh 
185f26e09c8Sdrh   if( iDb<0 || iDb==1 ) return 0;
186f26e09c8Sdrh   db = pParse->db;
187f26e09c8Sdrh   assert( db->nDb>iDb );
1884312db55Sdrh   pFix->pParse = pParse;
189f26e09c8Sdrh   pFix->zDb = db->aDb[iDb].zName;
190f26e09c8Sdrh   pFix->zType = zType;
191f26e09c8Sdrh   pFix->pName = pName;
192f26e09c8Sdrh   return 1;
193f26e09c8Sdrh }
194f26e09c8Sdrh 
195f26e09c8Sdrh /*
196f26e09c8Sdrh ** The following set of routines walk through the parse tree and assign
197f26e09c8Sdrh ** a specific database to all table references where the database name
198f26e09c8Sdrh ** was left unspecified in the original SQL statement.  The pFix structure
1994adee20fSdanielk1977 ** must have been initialized by a prior call to sqlite3FixInit().
200f26e09c8Sdrh **
201f26e09c8Sdrh ** These routines are used to make sure that an index, trigger, or
202f26e09c8Sdrh ** view in one database does not refer to objects in a different database.
203f26e09c8Sdrh ** (Exception: indices, triggers, and views in the TEMP database are
204f26e09c8Sdrh ** allowed to refer to anything.)  If a reference is explicitly made
205f26e09c8Sdrh ** to an object in a different database, an error message is added to
206f26e09c8Sdrh ** pParse->zErrMsg and these routines return non-zero.  If everything
207f26e09c8Sdrh ** checks out, these routines return 0.
208f26e09c8Sdrh */
2094adee20fSdanielk1977 int sqlite3FixSrcList(
210f26e09c8Sdrh   DbFixer *pFix,       /* Context of the fixation */
211f26e09c8Sdrh   SrcList *pList       /* The Source list to check and modify */
212f26e09c8Sdrh ){
213f26e09c8Sdrh   int i;
214f26e09c8Sdrh   const char *zDb;
215f26e09c8Sdrh 
216f26e09c8Sdrh   if( pList==0 ) return 0;
217f26e09c8Sdrh   zDb = pFix->zDb;
218f26e09c8Sdrh   for(i=0; i<pList->nSrc; i++){
219f26e09c8Sdrh     if( pList->a[i].zDatabase==0 ){
220f26e09c8Sdrh       pList->a[i].zDatabase = sqliteStrDup(zDb);
2214adee20fSdanielk1977     }else if( sqlite3StrICmp(pList->a[i].zDatabase,zDb)!=0 ){
2224adee20fSdanielk1977       sqlite3ErrorMsg(pFix->pParse,
2234312db55Sdrh          "%s %z cannot reference objects in database %s",
2244312db55Sdrh          pFix->zType, sqliteStrNDup(pFix->pName->z, pFix->pName->n),
2254312db55Sdrh          pList->a[i].zDatabase);
226f26e09c8Sdrh       return 1;
227f26e09c8Sdrh     }
2284adee20fSdanielk1977     if( sqlite3FixSelect(pFix, pList->a[i].pSelect) ) return 1;
2294adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pList->a[i].pOn) ) return 1;
230f26e09c8Sdrh   }
231f26e09c8Sdrh   return 0;
232f26e09c8Sdrh }
2334adee20fSdanielk1977 int sqlite3FixSelect(
234f26e09c8Sdrh   DbFixer *pFix,       /* Context of the fixation */
235f26e09c8Sdrh   Select *pSelect      /* The SELECT statement to be fixed to one database */
236f26e09c8Sdrh ){
237f26e09c8Sdrh   while( pSelect ){
2384adee20fSdanielk1977     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
239f26e09c8Sdrh       return 1;
240f26e09c8Sdrh     }
2414adee20fSdanielk1977     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
242f26e09c8Sdrh       return 1;
243f26e09c8Sdrh     }
2444adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
245f26e09c8Sdrh       return 1;
246f26e09c8Sdrh     }
2474adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
248f26e09c8Sdrh       return 1;
249f26e09c8Sdrh     }
250f26e09c8Sdrh     pSelect = pSelect->pPrior;
251f26e09c8Sdrh   }
252f26e09c8Sdrh   return 0;
253f26e09c8Sdrh }
2544adee20fSdanielk1977 int sqlite3FixExpr(
255f26e09c8Sdrh   DbFixer *pFix,     /* Context of the fixation */
256f26e09c8Sdrh   Expr *pExpr        /* The expression to be fixed to one database */
257f26e09c8Sdrh ){
258f26e09c8Sdrh   while( pExpr ){
2594adee20fSdanielk1977     if( sqlite3FixSelect(pFix, pExpr->pSelect) ){
260f26e09c8Sdrh       return 1;
261f26e09c8Sdrh     }
2624adee20fSdanielk1977     if( sqlite3FixExprList(pFix, pExpr->pList) ){
263f26e09c8Sdrh       return 1;
264f26e09c8Sdrh     }
2654adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
266f26e09c8Sdrh       return 1;
267f26e09c8Sdrh     }
268f26e09c8Sdrh     pExpr = pExpr->pLeft;
269f26e09c8Sdrh   }
270f26e09c8Sdrh   return 0;
271f26e09c8Sdrh }
2724adee20fSdanielk1977 int sqlite3FixExprList(
273f26e09c8Sdrh   DbFixer *pFix,     /* Context of the fixation */
274f26e09c8Sdrh   ExprList *pList    /* The expression to be fixed to one database */
275f26e09c8Sdrh ){
276f26e09c8Sdrh   int i;
277f26e09c8Sdrh   if( pList==0 ) return 0;
278f26e09c8Sdrh   for(i=0; i<pList->nExpr; i++){
2794adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pList->a[i].pExpr) ){
280f26e09c8Sdrh       return 1;
281f26e09c8Sdrh     }
282f26e09c8Sdrh   }
283f26e09c8Sdrh   return 0;
284f26e09c8Sdrh }
2854adee20fSdanielk1977 int sqlite3FixTriggerStep(
286f26e09c8Sdrh   DbFixer *pFix,     /* Context of the fixation */
287f26e09c8Sdrh   TriggerStep *pStep /* The trigger step be fixed to one database */
288f26e09c8Sdrh ){
289f26e09c8Sdrh   while( pStep ){
2904adee20fSdanielk1977     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
291f26e09c8Sdrh       return 1;
292f26e09c8Sdrh     }
2934adee20fSdanielk1977     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
294f26e09c8Sdrh       return 1;
295f26e09c8Sdrh     }
2964adee20fSdanielk1977     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
297f26e09c8Sdrh       return 1;
298f26e09c8Sdrh     }
299f26e09c8Sdrh     pStep = pStep->pNext;
300f26e09c8Sdrh   }
301f26e09c8Sdrh   return 0;
302f26e09c8Sdrh }
3034adee20fSdanielk1977 
3044adee20fSdanielk1977 
3054adee20fSdanielk1977 
306