ds@1952: /* ds@1952: ** 2003 April 6 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ************************************************************************* ds@1952: ** This file contains code used to implement the ATTACH and DETACH commands. ds@1952: ** ds@1952: ** $Id$ ds@1952: */ ds@1952: #include "sqliteInt.h" ds@1952: ds@1952: #ifndef SQLITE_OMIT_ATTACH ds@1952: /* ds@1952: ** Resolve an expression that was part of an ATTACH or DETACH statement. This ds@1952: ** is slightly different from resolving a normal SQL expression, because simple ds@1952: ** identifiers are treated as strings, not possible column names or aliases. ds@1952: ** ds@1952: ** i.e. if the parser sees: ds@1952: ** ds@1952: ** ATTACH DATABASE abc AS def ds@1952: ** ds@1952: ** it treats the two expressions as literal strings 'abc' and 'def' instead of ds@1952: ** looking for columns of the same name. ds@1952: ** ds@1952: ** This only applies to the root node of pExpr, so the statement: ds@1952: ** ds@1952: ** ATTACH DATABASE abc||def AS 'db2' ds@1952: ** ds@1952: ** will fail because neither abc or def can be resolved. ds@1952: */ ds@1952: static int resolveAttachExpr(NameContext *pName, Expr *pExpr) ds@1952: { ds@1952: int rc = SQLITE_OK; ds@1952: if( pExpr ){ ds@1952: if( pExpr->op!=TK_ID ){ ds@1952: rc = sqlite3ExprResolveNames(pName, pExpr); ds@1952: if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){ ds@1952: sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span); ds@1952: return SQLITE_ERROR; ds@1952: } ds@1952: }else{ ds@1952: pExpr->op = TK_STRING; ds@1952: } ds@1952: } ds@1952: return rc; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** An SQL user-function registered to do the work of an ATTACH statement. The ds@1952: ** three arguments to the function come directly from an attach statement: ds@1952: ** ds@1952: ** ATTACH DATABASE x AS y KEY z ds@1952: ** ds@1952: ** SELECT sqlite_attach(x, y, z) ds@1952: ** ds@1952: ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the ds@1952: ** third argument. ds@1952: */ ds@1952: static void attachFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: int i; ds@1952: int rc = 0; ds@1952: sqlite3 *db = sqlite3_user_data(context); ds@1952: const char *zName; ds@1952: const char *zFile; ds@1952: Db *aNew; ds@1952: char *zErrDyn = 0; ds@1952: char zErr[128]; ds@1952: ds@1952: zFile = (const char *)sqlite3_value_text(argv[0]); ds@1952: zName = (const char *)sqlite3_value_text(argv[1]); ds@1952: if( zFile==0 ) zFile = ""; ds@1952: if( zName==0 ) zName = ""; ds@1952: ds@1952: /* Check for the following errors: ds@1952: ** ds@1952: ** * Too many attached databases, ds@1952: ** * Transaction currently open ds@1952: ** * Specified database name already being used. ds@1952: */ ds@1952: if( db->nDb>=SQLITE_MAX_ATTACHED+2 ){ ds@1952: sqlite3_snprintf( ds@1952: sizeof(zErr), zErr, "too many attached databases - max %d", ds@1952: SQLITE_MAX_ATTACHED ds@1952: ); ds@1952: goto attach_error; ds@1952: } ds@1952: if( !db->autoCommit ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, ds@1952: "cannot ATTACH database within transaction"); ds@1952: goto attach_error; ds@1952: } ds@1952: for(i=0; inDb; i++){ ds@1952: char *z = db->aDb[i].zName; ds@1952: if( z && zName && sqlite3StrICmp(z, zName)==0 ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, ds@1952: "database %s is already in use", zName); ds@1952: goto attach_error; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Allocate the new entry in the db->aDb[] array and initialise the schema ds@1952: ** hash tables. ds@1952: */ ds@1952: if( db->aDb==db->aDbStatic ){ ds@1952: aNew = sqlite3_malloc( sizeof(db->aDb[0])*3 ); ds@1952: if( aNew==0 ){ ds@1952: db->mallocFailed = 1; ds@1952: return; ds@1952: } ds@1952: memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); ds@1952: }else{ ds@1952: aNew = sqlite3_realloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); ds@1952: if( aNew==0 ){ ds@1952: db->mallocFailed = 1; ds@1952: return; ds@1952: } ds@1952: } ds@1952: db->aDb = aNew; ds@1952: aNew = &db->aDb[db->nDb++]; ds@1952: memset(aNew, 0, sizeof(*aNew)); ds@1952: ds@1952: /* Open the database file. If the btree is successfully opened, use ds@1952: ** it to obtain the database schema. At this point the schema may ds@1952: ** or may not be initialised. ds@1952: */ ds@1952: rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE, ds@1952: db->openFlags | SQLITE_OPEN_MAIN_DB, ds@1952: &aNew->pBt); ds@1952: if( rc==SQLITE_OK ){ ds@1952: aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt); ds@1952: if( !aNew->pSchema ){ ds@1952: rc = SQLITE_NOMEM; ds@1952: }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, ds@1952: "attached databases must use the same text encoding as main database"); ds@1952: goto attach_error; ds@1952: } ds@1952: sqlite3PagerLockingMode(sqlite3BtreePager(aNew->pBt), db->dfltLockMode); ds@1952: } ds@1952: aNew->zName = sqlite3DbStrDup(db, zName); ds@1952: aNew->safety_level = 3; ds@1952: ds@1952: #if SQLITE_HAS_CODEC ds@1952: { ds@1952: extern int sqlite3CodecAttach(sqlite3*, int, const void*, int); ds@1952: extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); ds@1952: int nKey; ds@1952: char *zKey; ds@1952: int t = sqlite3_value_type(argv[2]); ds@1952: switch( t ){ ds@1952: case SQLITE_INTEGER: ds@1952: case SQLITE_FLOAT: ds@1952: zErrDyn = sqlite3DbStrDup(db, "Invalid key value"); ds@1952: rc = SQLITE_ERROR; ds@1952: break; ds@1952: ds@1952: case SQLITE_TEXT: ds@1952: case SQLITE_BLOB: ds@1952: nKey = sqlite3_value_bytes(argv[2]); ds@1952: zKey = (char *)sqlite3_value_blob(argv[2]); ds@1952: sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); ds@1952: break; ds@1952: ds@1952: case SQLITE_NULL: ds@1952: /* No key specified. Use the key from the main database */ ds@1952: sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); ds@1952: sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); ds@1952: break; ds@1952: } ds@1952: } ds@1952: #endif ds@1952: ds@1952: /* If the file was opened successfully, read the schema for the new database. ds@1952: ** If this fails, or if opening the file failed, then close the file and ds@1952: ** remove the entry from the db->aDb[] array. i.e. put everything back the way ds@1952: ** we found it. ds@1952: */ ds@1952: if( rc==SQLITE_OK ){ ds@1952: sqlite3SafetyOn(db); ds@1952: rc = sqlite3Init(db, &zErrDyn); ds@1952: sqlite3SafetyOff(db); ds@1952: } ds@1952: if( rc ){ ds@1952: int iDb = db->nDb - 1; ds@1952: assert( iDb>=2 ); ds@1952: if( db->aDb[iDb].pBt ){ ds@1952: sqlite3BtreeClose(db->aDb[iDb].pBt); ds@1952: db->aDb[iDb].pBt = 0; ds@1952: db->aDb[iDb].pSchema = 0; ds@1952: } ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: db->nDb = iDb; ds@1952: if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ ds@1952: db->mallocFailed = 1; ds@1952: sqlite3_snprintf(sizeof(zErr),zErr, "out of memory"); ds@1952: }else{ ds@1952: sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile); ds@1952: } ds@1952: goto attach_error; ds@1952: } ds@1952: ds@1952: return; ds@1952: ds@1952: attach_error: ds@1952: /* Return an error if we get here */ ds@1952: if( zErrDyn ){ ds@1952: sqlite3_result_error(context, zErrDyn, -1); ds@1952: sqlite3_free(zErrDyn); ds@1952: }else{ ds@1952: zErr[sizeof(zErr)-1] = 0; ds@1952: sqlite3_result_error(context, zErr, -1); ds@1952: } ds@1952: } ds@1952: ds@1952: /* ds@1952: ** An SQL user-function registered to do the work of an DETACH statement. The ds@1952: ** three arguments to the function come directly from a detach statement: ds@1952: ** ds@1952: ** DETACH DATABASE x ds@1952: ** ds@1952: ** SELECT sqlite_detach(x) ds@1952: */ ds@1952: static void detachFunc( ds@1952: sqlite3_context *context, ds@1952: int argc, ds@1952: sqlite3_value **argv ds@1952: ){ ds@1952: const char *zName = (const char *)sqlite3_value_text(argv[0]); ds@1952: sqlite3 *db = sqlite3_user_data(context); ds@1952: int i; ds@1952: Db *pDb = 0; ds@1952: char zErr[128]; ds@1952: ds@1952: if( zName==0 ) zName = ""; ds@1952: for(i=0; inDb; i++){ ds@1952: pDb = &db->aDb[i]; ds@1952: if( pDb->pBt==0 ) continue; ds@1952: if( sqlite3StrICmp(pDb->zName, zName)==0 ) break; ds@1952: } ds@1952: ds@1952: if( i>=db->nDb ){ ds@1952: sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName); ds@1952: goto detach_error; ds@1952: } ds@1952: if( i<2 ){ ds@1952: sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); ds@1952: goto detach_error; ds@1952: } ds@1952: if( !db->autoCommit ){ ds@1952: sqlite3_snprintf(sizeof(zErr), zErr, ds@1952: "cannot DETACH database within transaction"); ds@1952: goto detach_error; ds@1952: } ds@1952: if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){ ds@1952: sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); ds@1952: goto detach_error; ds@1952: } ds@1952: ds@1952: sqlite3BtreeClose(pDb->pBt); ds@1952: pDb->pBt = 0; ds@1952: pDb->pSchema = 0; ds@1952: sqlite3ResetInternalSchema(db, 0); ds@1952: return; ds@1952: ds@1952: detach_error: ds@1952: sqlite3_result_error(context, zErr, -1); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** This procedure generates VDBE code for a single invocation of either the ds@1952: ** sqlite_detach() or sqlite_attach() SQL user functions. ds@1952: */ ds@1952: static void codeAttach( ds@1952: Parse *pParse, /* The parser context */ ds@1952: int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */ ds@1952: const char *zFunc, /* Either "sqlite_attach" or "sqlite_detach */ ds@1952: int nFunc, /* Number of args to pass to zFunc */ ds@1952: Expr *pAuthArg, /* Expression to pass to authorization callback */ ds@1952: Expr *pFilename, /* Name of database file */ ds@1952: Expr *pDbname, /* Name of the database to use internally */ ds@1952: Expr *pKey /* Database key for encryption extension */ ds@1952: ){ ds@1952: int rc; ds@1952: NameContext sName; ds@1952: Vdbe *v; ds@1952: FuncDef *pFunc; ds@1952: sqlite3* db = pParse->db; ds@1952: ds@1952: #ifndef SQLITE_OMIT_AUTHORIZATION ds@1952: assert( db->mallocFailed || pAuthArg ); ds@1952: if( pAuthArg ){ ds@1952: char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span); ds@1952: if( !zAuthArg ){ ds@1952: goto attach_end; ds@1952: } ds@1952: rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); ds@1952: sqlite3_free(zAuthArg); ds@1952: if(rc!=SQLITE_OK ){ ds@1952: goto attach_end; ds@1952: } ds@1952: } ds@1952: #endif /* SQLITE_OMIT_AUTHORIZATION */ ds@1952: ds@1952: memset(&sName, 0, sizeof(NameContext)); ds@1952: sName.pParse = pParse; ds@1952: ds@1952: if( ds@1952: SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || ds@1952: SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || ds@1952: SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) ds@1952: ){ ds@1952: pParse->nErr++; ds@1952: goto attach_end; ds@1952: } ds@1952: ds@1952: v = sqlite3GetVdbe(pParse); ds@1952: sqlite3ExprCode(pParse, pFilename); ds@1952: sqlite3ExprCode(pParse, pDbname); ds@1952: sqlite3ExprCode(pParse, pKey); ds@1952: ds@1952: assert( v || db->mallocFailed ); ds@1952: if( v ){ ds@1952: sqlite3VdbeAddOp(v, OP_Function, 0, nFunc); ds@1952: pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0); ds@1952: sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF); ds@1952: ds@1952: /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this ds@1952: ** statement only). For DETACH, set it to false (expire all existing ds@1952: ** statements). ds@1952: */ ds@1952: sqlite3VdbeAddOp(v, OP_Expire, (type==SQLITE_ATTACH), 0); ds@1952: } ds@1952: ds@1952: attach_end: ds@1952: sqlite3ExprDelete(pFilename); ds@1952: sqlite3ExprDelete(pDbname); ds@1952: sqlite3ExprDelete(pKey); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Called by the parser to compile a DETACH statement. ds@1952: ** ds@1952: ** DETACH pDbname ds@1952: */ ds@1952: void sqlite3Detach(Parse *pParse, Expr *pDbname){ ds@1952: codeAttach(pParse, SQLITE_DETACH, "sqlite_detach", 1, pDbname, 0, 0, pDbname); ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Called by the parser to compile an ATTACH statement. ds@1952: ** ds@1952: ** ATTACH p AS pDbname KEY pKey ds@1952: */ ds@1952: void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ ds@1952: codeAttach(pParse, SQLITE_ATTACH, "sqlite_attach", 3, p, p, pDbname, pKey); ds@1952: } ds@1952: #endif /* SQLITE_OMIT_ATTACH */ ds@1952: ds@1952: /* ds@1952: ** Register the functions sqlite_attach and sqlite_detach. ds@1952: */ ds@1952: void sqlite3AttachFunctions(sqlite3 *db){ ds@1952: #ifndef SQLITE_OMIT_ATTACH ds@1952: static const int enc = SQLITE_UTF8; ds@1952: sqlite3CreateFunc(db, "sqlite_attach", 3, enc, db, attachFunc, 0, 0); ds@1952: sqlite3CreateFunc(db, "sqlite_detach", 1, enc, db, detachFunc, 0, 0); ds@1952: #endif ds@1952: } ds@1952: ds@1952: /* ds@1952: ** Initialize a DbFixer structure. This routine must be called prior ds@1952: ** to passing the structure to one of the sqliteFixAAAA() routines below. ds@1952: ** ds@1952: ** The return value indicates whether or not fixation is required. TRUE ds@1952: ** means we do need to fix the database references, FALSE means we do not. ds@1952: */ ds@1952: int sqlite3FixInit( ds@1952: DbFixer *pFix, /* The fixer to be initialized */ ds@1952: Parse *pParse, /* Error messages will be written here */ ds@1952: int iDb, /* This is the database that must be used */ ds@1952: const char *zType, /* "view", "trigger", or "index" */ ds@1952: const Token *pName /* Name of the view, trigger, or index */ ds@1952: ){ ds@1952: sqlite3 *db; ds@1952: ds@1952: if( iDb<0 || iDb==1 ) return 0; ds@1952: db = pParse->db; ds@1952: assert( db->nDb>iDb ); ds@1952: pFix->pParse = pParse; ds@1952: pFix->zDb = db->aDb[iDb].zName; ds@1952: pFix->zType = zType; ds@1952: pFix->pName = pName; ds@1952: return 1; ds@1952: } ds@1952: ds@1952: /* ds@1952: ** The following set of routines walk through the parse tree and assign ds@1952: ** a specific database to all table references where the database name ds@1952: ** was left unspecified in the original SQL statement. The pFix structure ds@1952: ** must have been initialized by a prior call to sqlite3FixInit(). ds@1952: ** ds@1952: ** These routines are used to make sure that an index, trigger, or ds@1952: ** view in one database does not refer to objects in a different database. ds@1952: ** (Exception: indices, triggers, and views in the TEMP database are ds@1952: ** allowed to refer to anything.) If a reference is explicitly made ds@1952: ** to an object in a different database, an error message is added to ds@1952: ** pParse->zErrMsg and these routines return non-zero. If everything ds@1952: ** checks out, these routines return 0. ds@1952: */ ds@1952: int sqlite3FixSrcList( ds@1952: DbFixer *pFix, /* Context of the fixation */ ds@1952: SrcList *pList /* The Source list to check and modify */ ds@1952: ){ ds@1952: int i; ds@1952: const char *zDb; ds@1952: struct SrcList_item *pItem; ds@1952: ds@1952: if( pList==0 ) return 0; ds@1952: zDb = pFix->zDb; ds@1952: for(i=0, pItem=pList->a; inSrc; i++, pItem++){ ds@1952: if( pItem->zDatabase==0 ){ ds@1952: pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb); ds@1952: }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){ ds@1952: sqlite3ErrorMsg(pFix->pParse, ds@1952: "%s %T cannot reference objects in database %s", ds@1952: pFix->zType, pFix->pName, pItem->zDatabase); ds@1952: return 1; ds@1952: } ds@1952: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) ds@1952: if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; ds@1952: if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; ds@1952: #endif ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) ds@1952: int sqlite3FixSelect( ds@1952: DbFixer *pFix, /* Context of the fixation */ ds@1952: Select *pSelect /* The SELECT statement to be fixed to one database */ ds@1952: ){ ds@1952: while( pSelect ){ ds@1952: if( sqlite3FixExprList(pFix, pSelect->pEList) ){ ds@1952: return 1; ds@1952: } ds@1952: if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ ds@1952: return 1; ds@1952: } ds@1952: if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ ds@1952: return 1; ds@1952: } ds@1952: if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ ds@1952: return 1; ds@1952: } ds@1952: pSelect = pSelect->pPrior; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: int sqlite3FixExpr( ds@1952: DbFixer *pFix, /* Context of the fixation */ ds@1952: Expr *pExpr /* The expression to be fixed to one database */ ds@1952: ){ ds@1952: while( pExpr ){ ds@1952: if( sqlite3FixSelect(pFix, pExpr->pSelect) ){ ds@1952: return 1; ds@1952: } ds@1952: if( sqlite3FixExprList(pFix, pExpr->pList) ){ ds@1952: return 1; ds@1952: } ds@1952: if( sqlite3FixExpr(pFix, pExpr->pRight) ){ ds@1952: return 1; ds@1952: } ds@1952: pExpr = pExpr->pLeft; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: int sqlite3FixExprList( ds@1952: DbFixer *pFix, /* Context of the fixation */ ds@1952: ExprList *pList /* The expression to be fixed to one database */ ds@1952: ){ ds@1952: int i; ds@1952: struct ExprList_item *pItem; ds@1952: if( pList==0 ) return 0; ds@1952: for(i=0, pItem=pList->a; inExpr; i++, pItem++){ ds@1952: if( sqlite3FixExpr(pFix, pItem->pExpr) ){ ds@1952: return 1; ds@1952: } ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: #endif ds@1952: ds@1952: #ifndef SQLITE_OMIT_TRIGGER ds@1952: int sqlite3FixTriggerStep( ds@1952: DbFixer *pFix, /* Context of the fixation */ ds@1952: TriggerStep *pStep /* The trigger step be fixed to one database */ ds@1952: ){ ds@1952: while( pStep ){ ds@1952: if( sqlite3FixSelect(pFix, pStep->pSelect) ){ ds@1952: return 1; ds@1952: } ds@1952: if( sqlite3FixExpr(pFix, pStep->pWhere) ){ ds@1952: return 1; ds@1952: } ds@1952: if( sqlite3FixExprList(pFix, pStep->pExprList) ){ ds@1952: return 1; ds@1952: } ds@1952: pStep = pStep->pNext; ds@1952: } ds@1952: return 0; ds@1952: } ds@1952: #endif