00001 /* 00002 ** 2003 April 6 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** This file contains code used to implement the ATTACH and DETACH commands. 00013 ** 00014 ** $Id: attach.c,v 1.79 2008/10/28 17:52:39 danielk1977 Exp $ 00015 */ 00016 #include "sqliteInt.h" 00017 00018 #ifndef SQLITE_OMIT_ATTACH 00019 /* 00020 ** Resolve an expression that was part of an ATTACH or DETACH statement. This 00021 ** is slightly different from resolving a normal SQL expression, because simple 00022 ** identifiers are treated as strings, not possible column names or aliases. 00023 ** 00024 ** i.e. if the parser sees: 00025 ** 00026 ** ATTACH DATABASE abc AS def 00027 ** 00028 ** it treats the two expressions as literal strings 'abc' and 'def' instead of 00029 ** looking for columns of the same name. 00030 ** 00031 ** This only applies to the root node of pExpr, so the statement: 00032 ** 00033 ** ATTACH DATABASE abc||def AS 'db2' 00034 ** 00035 ** will fail because neither abc or def can be resolved. 00036 */ 00037 static int resolveAttachExpr(NameContext *pName, Expr *pExpr) 00038 { 00039 int rc = SQLITE_OK; 00040 if( pExpr ){ 00041 if( pExpr->op!=TK_ID ){ 00042 rc = sqlite3ResolveExprNames(pName, pExpr); 00043 if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){ 00044 sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span); 00045 return SQLITE_ERROR; 00046 } 00047 }else{ 00048 pExpr->op = TK_STRING; 00049 } 00050 } 00051 return rc; 00052 } 00053 00054 /* 00055 ** An SQL user-function registered to do the work of an ATTACH statement. The 00056 ** three arguments to the function come directly from an attach statement: 00057 ** 00058 ** ATTACH DATABASE x AS y KEY z 00059 ** 00060 ** SELECT sqlite_attach(x, y, z) 00061 ** 00062 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the 00063 ** third argument. 00064 */ 00065 static void attachFunc( 00066 sqlite3_context *context, 00067 int argc, 00068 sqlite3_value **argv 00069 ){ 00070 int i; 00071 int rc = 0; 00072 sqlite3 *db = sqlite3_context_db_handle(context); 00073 const char *zName; 00074 const char *zFile; 00075 Db *aNew; 00076 char *zErrDyn = 0; 00077 char zErr[128]; 00078 00079 zFile = (const char *)sqlite3_value_text(argv[0]); 00080 zName = (const char *)sqlite3_value_text(argv[1]); 00081 if( zFile==0 ) zFile = ""; 00082 if( zName==0 ) zName = ""; 00083 00084 /* Check for the following errors: 00085 ** 00086 ** * Too many attached databases, 00087 ** * Transaction currently open 00088 ** * Specified database name already being used. 00089 */ 00090 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){ 00091 sqlite3_snprintf( 00092 sizeof(zErr), zErr, "too many attached databases - max %d", 00093 db->aLimit[SQLITE_LIMIT_ATTACHED] 00094 ); 00095 goto attach_error; 00096 } 00097 if( !db->autoCommit ){ 00098 sqlite3_snprintf(sizeof(zErr), zErr, 00099 "cannot ATTACH database within transaction"); 00100 goto attach_error; 00101 } 00102 for(i=0; i<db->nDb; i++){ 00103 char *z = db->aDb[i].zName; 00104 if( z && zName && sqlite3StrICmp(z, zName)==0 ){ 00105 sqlite3_snprintf(sizeof(zErr), zErr, 00106 "database %s is already in use", zName); 00107 goto attach_error; 00108 } 00109 } 00110 00111 /* Allocate the new entry in the db->aDb[] array and initialise the schema 00112 ** hash tables. 00113 */ 00114 if( db->aDb==db->aDbStatic ){ 00115 aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 ); 00116 if( aNew==0 ) return; 00117 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); 00118 }else{ 00119 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); 00120 if( aNew==0 ) return; 00121 } 00122 db->aDb = aNew; 00123 aNew = &db->aDb[db->nDb++]; 00124 memset(aNew, 0, sizeof(*aNew)); 00125 00126 /* Open the database file. If the btree is successfully opened, use 00127 ** it to obtain the database schema. At this point the schema may 00128 ** or may not be initialised. 00129 */ 00130 rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE, 00131 db->openFlags | SQLITE_OPEN_MAIN_DB, 00132 &aNew->pBt); 00133 if( rc==SQLITE_OK ){ 00134 Pager *pPager; 00135 aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt); 00136 if( !aNew->pSchema ){ 00137 rc = SQLITE_NOMEM; 00138 }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ 00139 sqlite3_snprintf(sizeof(zErr), zErr, 00140 "attached databases must use the same text encoding as main database"); 00141 goto attach_error; 00142 } 00143 pPager = sqlite3BtreePager(aNew->pBt); 00144 sqlite3PagerLockingMode(pPager, db->dfltLockMode); 00145 sqlite3PagerJournalMode(pPager, db->dfltJournalMode); 00146 } 00147 aNew->zName = sqlite3DbStrDup(db, zName); 00148 aNew->safety_level = 3; 00149 00150 #if SQLITE_HAS_CODEC 00151 { 00152 extern int sqlite3CodecAttach(sqlite3*, int, const void*, int); 00153 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); 00154 int nKey; 00155 char *zKey; 00156 int t = sqlite3_value_type(argv[2]); 00157 switch( t ){ 00158 case SQLITE_INTEGER: 00159 case SQLITE_FLOAT: 00160 zErrDyn = sqlite3DbStrDup(db, "Invalid key value"); 00161 rc = SQLITE_ERROR; 00162 break; 00163 00164 case SQLITE_TEXT: 00165 case SQLITE_BLOB: 00166 nKey = sqlite3_value_bytes(argv[2]); 00167 zKey = (char *)sqlite3_value_blob(argv[2]); 00168 sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); 00169 break; 00170 00171 case SQLITE_NULL: 00172 /* No key specified. Use the key from the main database */ 00173 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); 00174 sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); 00175 break; 00176 } 00177 } 00178 #endif 00179 00180 /* If the file was opened successfully, read the schema for the new database. 00181 ** If this fails, or if opening the file failed, then close the file and 00182 ** remove the entry from the db->aDb[] array. i.e. put everything back the way 00183 ** we found it. 00184 */ 00185 if( rc==SQLITE_OK ){ 00186 (void)sqlite3SafetyOn(db); 00187 sqlite3BtreeEnterAll(db); 00188 rc = sqlite3Init(db, &zErrDyn); 00189 sqlite3BtreeLeaveAll(db); 00190 (void)sqlite3SafetyOff(db); 00191 } 00192 if( rc ){ 00193 int iDb = db->nDb - 1; 00194 assert( iDb>=2 ); 00195 if( db->aDb[iDb].pBt ){ 00196 sqlite3BtreeClose(db->aDb[iDb].pBt); 00197 db->aDb[iDb].pBt = 0; 00198 db->aDb[iDb].pSchema = 0; 00199 } 00200 sqlite3ResetInternalSchema(db, 0); 00201 db->nDb = iDb; 00202 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ 00203 db->mallocFailed = 1; 00204 sqlite3_snprintf(sizeof(zErr),zErr, "out of memory"); 00205 }else{ 00206 sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile); 00207 } 00208 goto attach_error; 00209 } 00210 00211 return; 00212 00213 attach_error: 00214 /* Return an error if we get here */ 00215 if( zErrDyn ){ 00216 sqlite3_result_error(context, zErrDyn, -1); 00217 sqlite3DbFree(db, zErrDyn); 00218 }else{ 00219 zErr[sizeof(zErr)-1] = 0; 00220 sqlite3_result_error(context, zErr, -1); 00221 } 00222 if( rc ) sqlite3_result_error_code(context, rc); 00223 } 00224 00225 /* 00226 ** An SQL user-function registered to do the work of an DETACH statement. The 00227 ** three arguments to the function come directly from a detach statement: 00228 ** 00229 ** DETACH DATABASE x 00230 ** 00231 ** SELECT sqlite_detach(x) 00232 */ 00233 static void detachFunc( 00234 sqlite3_context *context, 00235 int argc, 00236 sqlite3_value **argv 00237 ){ 00238 const char *zName = (const char *)sqlite3_value_text(argv[0]); 00239 sqlite3 *db = sqlite3_context_db_handle(context); 00240 int i; 00241 Db *pDb = 0; 00242 char zErr[128]; 00243 00244 if( zName==0 ) zName = ""; 00245 for(i=0; i<db->nDb; i++){ 00246 pDb = &db->aDb[i]; 00247 if( pDb->pBt==0 ) continue; 00248 if( sqlite3StrICmp(pDb->zName, zName)==0 ) break; 00249 } 00250 00251 if( i>=db->nDb ){ 00252 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName); 00253 goto detach_error; 00254 } 00255 if( i<2 ){ 00256 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); 00257 goto detach_error; 00258 } 00259 if( !db->autoCommit ){ 00260 sqlite3_snprintf(sizeof(zErr), zErr, 00261 "cannot DETACH database within transaction"); 00262 goto detach_error; 00263 } 00264 if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){ 00265 sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); 00266 goto detach_error; 00267 } 00268 00269 sqlite3BtreeClose(pDb->pBt); 00270 pDb->pBt = 0; 00271 pDb->pSchema = 0; 00272 sqlite3ResetInternalSchema(db, 0); 00273 return; 00274 00275 detach_error: 00276 sqlite3_result_error(context, zErr, -1); 00277 } 00278 00279 /* 00280 ** This procedure generates VDBE code for a single invocation of either the 00281 ** sqlite_detach() or sqlite_attach() SQL user functions. 00282 */ 00283 static void codeAttach( 00284 Parse *pParse, /* The parser context */ 00285 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */ 00286 FuncDef *pFunc, /* FuncDef wrapper for detachFunc() or attachFunc() */ 00287 Expr *pAuthArg, /* Expression to pass to authorization callback */ 00288 Expr *pFilename, /* Name of database file */ 00289 Expr *pDbname, /* Name of the database to use internally */ 00290 Expr *pKey /* Database key for encryption extension */ 00291 ){ 00292 int rc; 00293 NameContext sName; 00294 Vdbe *v; 00295 sqlite3* db = pParse->db; 00296 int regArgs; 00297 00298 #ifndef SQLITE_OMIT_AUTHORIZATION 00299 assert( db->mallocFailed || pAuthArg ); 00300 if( pAuthArg ){ 00301 char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span); 00302 if( !zAuthArg ){ 00303 goto attach_end; 00304 } 00305 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); 00306 sqlite3DbFree(db, zAuthArg); 00307 if(rc!=SQLITE_OK ){ 00308 goto attach_end; 00309 } 00310 } 00311 #endif /* SQLITE_OMIT_AUTHORIZATION */ 00312 00313 memset(&sName, 0, sizeof(NameContext)); 00314 sName.pParse = pParse; 00315 00316 if( 00317 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || 00318 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || 00319 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) 00320 ){ 00321 pParse->nErr++; 00322 goto attach_end; 00323 } 00324 00325 v = sqlite3GetVdbe(pParse); 00326 regArgs = sqlite3GetTempRange(pParse, 4); 00327 sqlite3ExprCode(pParse, pFilename, regArgs); 00328 sqlite3ExprCode(pParse, pDbname, regArgs+1); 00329 sqlite3ExprCode(pParse, pKey, regArgs+2); 00330 00331 assert( v || db->mallocFailed ); 00332 if( v ){ 00333 sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3); 00334 sqlite3VdbeChangeP5(v, pFunc->nArg); 00335 sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF); 00336 00337 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this 00338 ** statement only). For DETACH, set it to false (expire all existing 00339 ** statements). 00340 */ 00341 sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH)); 00342 } 00343 00344 attach_end: 00345 sqlite3ExprDelete(db, pFilename); 00346 sqlite3ExprDelete(db, pDbname); 00347 sqlite3ExprDelete(db, pKey); 00348 } 00349 00350 /* 00351 ** Called by the parser to compile a DETACH statement. 00352 ** 00353 ** DETACH pDbname 00354 */ 00355 void sqlite3Detach(Parse *pParse, Expr *pDbname){ 00356 static FuncDef detach_func = { 00357 1, /* nArg */ 00358 SQLITE_UTF8, /* iPrefEnc */ 00359 0, /* flags */ 00360 0, /* pUserData */ 00361 0, /* pNext */ 00362 detachFunc, /* xFunc */ 00363 0, /* xStep */ 00364 0, /* xFinalize */ 00365 "sqlite_detach", /* zName */ 00366 0 /* pHash */ 00367 }; 00368 codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); 00369 } 00370 00371 /* 00372 ** Called by the parser to compile an ATTACH statement. 00373 ** 00374 ** ATTACH p AS pDbname KEY pKey 00375 */ 00376 void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ 00377 static FuncDef attach_func = { 00378 3, /* nArg */ 00379 SQLITE_UTF8, /* iPrefEnc */ 00380 0, /* flags */ 00381 0, /* pUserData */ 00382 0, /* pNext */ 00383 attachFunc, /* xFunc */ 00384 0, /* xStep */ 00385 0, /* xFinalize */ 00386 "sqlite_attach", /* zName */ 00387 0 /* pHash */ 00388 }; 00389 codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); 00390 } 00391 #endif /* SQLITE_OMIT_ATTACH */ 00392 00393 /* 00394 ** Initialize a DbFixer structure. This routine must be called prior 00395 ** to passing the structure to one of the sqliteFixAAAA() routines below. 00396 ** 00397 ** The return value indicates whether or not fixation is required. TRUE 00398 ** means we do need to fix the database references, FALSE means we do not. 00399 */ 00400 int sqlite3FixInit( 00401 DbFixer *pFix, /* The fixer to be initialized */ 00402 Parse *pParse, /* Error messages will be written here */ 00403 int iDb, /* This is the database that must be used */ 00404 const char *zType, /* "view", "trigger", or "index" */ 00405 const Token *pName /* Name of the view, trigger, or index */ 00406 ){ 00407 sqlite3 *db; 00408 00409 if( iDb<0 || iDb==1 ) return 0; 00410 db = pParse->db; 00411 assert( db->nDb>iDb ); 00412 pFix->pParse = pParse; 00413 pFix->zDb = db->aDb[iDb].zName; 00414 pFix->zType = zType; 00415 pFix->pName = pName; 00416 return 1; 00417 } 00418 00419 /* 00420 ** The following set of routines walk through the parse tree and assign 00421 ** a specific database to all table references where the database name 00422 ** was left unspecified in the original SQL statement. The pFix structure 00423 ** must have been initialized by a prior call to sqlite3FixInit(). 00424 ** 00425 ** These routines are used to make sure that an index, trigger, or 00426 ** view in one database does not refer to objects in a different database. 00427 ** (Exception: indices, triggers, and views in the TEMP database are 00428 ** allowed to refer to anything.) If a reference is explicitly made 00429 ** to an object in a different database, an error message is added to 00430 ** pParse->zErrMsg and these routines return non-zero. If everything 00431 ** checks out, these routines return 0. 00432 */ 00433 int sqlite3FixSrcList( 00434 DbFixer *pFix, /* Context of the fixation */ 00435 SrcList *pList /* The Source list to check and modify */ 00436 ){ 00437 int i; 00438 const char *zDb; 00439 struct SrcList_item *pItem; 00440 00441 if( pList==0 ) return 0; 00442 zDb = pFix->zDb; 00443 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ 00444 if( pItem->zDatabase==0 ){ 00445 pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb); 00446 }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){ 00447 sqlite3ErrorMsg(pFix->pParse, 00448 "%s %T cannot reference objects in database %s", 00449 pFix->zType, pFix->pName, pItem->zDatabase); 00450 return 1; 00451 } 00452 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) 00453 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; 00454 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; 00455 #endif 00456 } 00457 return 0; 00458 } 00459 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) 00460 int sqlite3FixSelect( 00461 DbFixer *pFix, /* Context of the fixation */ 00462 Select *pSelect /* The SELECT statement to be fixed to one database */ 00463 ){ 00464 while( pSelect ){ 00465 if( sqlite3FixExprList(pFix, pSelect->pEList) ){ 00466 return 1; 00467 } 00468 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ 00469 return 1; 00470 } 00471 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ 00472 return 1; 00473 } 00474 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ 00475 return 1; 00476 } 00477 pSelect = pSelect->pPrior; 00478 } 00479 return 0; 00480 } 00481 int sqlite3FixExpr( 00482 DbFixer *pFix, /* Context of the fixation */ 00483 Expr *pExpr /* The expression to be fixed to one database */ 00484 ){ 00485 while( pExpr ){ 00486 if( sqlite3FixSelect(pFix, pExpr->pSelect) ){ 00487 return 1; 00488 } 00489 if( sqlite3FixExprList(pFix, pExpr->pList) ){ 00490 return 1; 00491 } 00492 if( sqlite3FixExpr(pFix, pExpr->pRight) ){ 00493 return 1; 00494 } 00495 pExpr = pExpr->pLeft; 00496 } 00497 return 0; 00498 } 00499 int sqlite3FixExprList( 00500 DbFixer *pFix, /* Context of the fixation */ 00501 ExprList *pList /* The expression to be fixed to one database */ 00502 ){ 00503 int i; 00504 struct ExprList_item *pItem; 00505 if( pList==0 ) return 0; 00506 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){ 00507 if( sqlite3FixExpr(pFix, pItem->pExpr) ){ 00508 return 1; 00509 } 00510 } 00511 return 0; 00512 } 00513 #endif 00514 00515 #ifndef SQLITE_OMIT_TRIGGER 00516 int sqlite3FixTriggerStep( 00517 DbFixer *pFix, /* Context of the fixation */ 00518 TriggerStep *pStep /* The trigger step be fixed to one database */ 00519 ){ 00520 while( pStep ){ 00521 if( sqlite3FixSelect(pFix, pStep->pSelect) ){ 00522 return 1; 00523 } 00524 if( sqlite3FixExpr(pFix, pStep->pWhere) ){ 00525 return 1; 00526 } 00527 if( sqlite3FixExprList(pFix, pStep->pExprList) ){ 00528 return 1; 00529 } 00530 pStep = pStep->pNext; 00531 } 00532 return 0; 00533 } 00534 #endif
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:51 2011 by Doxygen 1.6.1