comparison sqlite/attach.c @ 1434:b6b61becdf4e trunk

[svn] - add sqlite/ directory
author nenolod
date Thu, 27 Jul 2006 22:41:31 -0700
parents
children
comparison
equal deleted inserted replaced
1433:3cbe3d14ea68 1434:b6b61becdf4e
1 /*
2 ** 2003 April 6
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file contains code used to implement the ATTACH and DETACH commands.
13 **
14 ** $Id: attach.c,v 1.52 2006/05/25 11:52:38 drh Exp $
15 */
16 #include "sqliteInt.h"
17
18 /*
19 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
20 ** is slightly different from resolving a normal SQL expression, because simple
21 ** identifiers are treated as strings, not possible column names or aliases.
22 **
23 ** i.e. if the parser sees:
24 **
25 ** ATTACH DATABASE abc AS def
26 **
27 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
28 ** looking for columns of the same name.
29 **
30 ** This only applies to the root node of pExpr, so the statement:
31 **
32 ** ATTACH DATABASE abc||def AS 'db2'
33 **
34 ** will fail because neither abc or def can be resolved.
35 */
36 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
37 {
38 int rc = SQLITE_OK;
39 if( pExpr ){
40 if( pExpr->op!=TK_ID ){
41 rc = sqlite3ExprResolveNames(pName, pExpr);
42 }else{
43 pExpr->op = TK_STRING;
44 }
45 }
46 return rc;
47 }
48
49 /*
50 ** An SQL user-function registered to do the work of an ATTACH statement. The
51 ** three arguments to the function come directly from an attach statement:
52 **
53 ** ATTACH DATABASE x AS y KEY z
54 **
55 ** SELECT sqlite_attach(x, y, z)
56 **
57 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
58 ** third argument.
59 */
60 static void attachFunc(
61 sqlite3_context *context,
62 int argc,
63 sqlite3_value **argv
64 ){
65 int i;
66 int rc = 0;
67 sqlite3 *db = sqlite3_user_data(context);
68 const char *zName;
69 const char *zFile;
70 Db *aNew;
71 char zErr[128];
72 char *zErrDyn = 0;
73
74 zFile = (const char *)sqlite3_value_text(argv[0]);
75 zName = (const char *)sqlite3_value_text(argv[1]);
76 if( zFile==0 ) zFile = "";
77 if( zName==0 ) zName = "";
78
79 /* Check for the following errors:
80 **
81 ** * Too many attached databases,
82 ** * Transaction currently open
83 ** * Specified database name already being used.
84 */
85 if( db->nDb>=MAX_ATTACHED+2 ){
86 sqlite3_snprintf(
87 sizeof(zErr), zErr, "too many attached databases - max %d", MAX_ATTACHED
88 );
89 goto attach_error;
90 }
91 if( !db->autoCommit ){
92 strcpy(zErr, "cannot ATTACH database within transaction");
93 goto attach_error;
94 }
95 for(i=0; i<db->nDb; i++){
96 char *z = db->aDb[i].zName;
97 if( z && zName && sqlite3StrICmp(z, zName)==0 ){
98 sqlite3_snprintf(sizeof(zErr), zErr, "database %s is already in use", zName);
99 goto attach_error;
100 }
101 }
102
103 /* Allocate the new entry in the db->aDb[] array and initialise the schema
104 ** hash tables.
105 */
106 if( db->aDb==db->aDbStatic ){
107 aNew = sqliteMalloc( sizeof(db->aDb[0])*3 );
108 if( aNew==0 ){
109 return;
110 }
111 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
112 }else{
113 aNew = sqliteRealloc(db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
114 if( aNew==0 ){
115 return;
116 }
117 }
118 db->aDb = aNew;
119 aNew = &db->aDb[db->nDb++];
120 memset(aNew, 0, sizeof(*aNew));
121
122 /* Open the database file. If the btree is successfully opened, use
123 ** it to obtain the database schema. At this point the schema may
124 ** or may not be initialised.
125 */
126 rc = sqlite3BtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt);
127 if( rc==SQLITE_OK ){
128 aNew->pSchema = sqlite3SchemaGet(aNew->pBt);
129 if( !aNew->pSchema ){
130 rc = SQLITE_NOMEM;
131 }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
132 strcpy(zErr,
133 "attached databases must use the same text encoding as main database");
134 goto attach_error;
135 }
136 }
137 aNew->zName = sqliteStrDup(zName);
138 aNew->safety_level = 3;
139
140 #if SQLITE_HAS_CODEC
141 {
142 extern int sqlite3CodecAttach(sqlite3*, int, void*, int);
143 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
144 int nKey;
145 char *zKey;
146 int t = sqlite3_value_type(argv[2]);
147 switch( t ){
148 case SQLITE_INTEGER:
149 case SQLITE_FLOAT:
150 zErrDyn = sqliteStrDup("Invalid key value");
151 rc = SQLITE_ERROR;
152 break;
153
154 case SQLITE_TEXT:
155 case SQLITE_BLOB:
156 nKey = sqlite3_value_bytes(argv[2]);
157 zKey = (char *)sqlite3_value_blob(argv[2]);
158 sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
159 break;
160
161 case SQLITE_NULL:
162 /* No key specified. Use the key from the main database */
163 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
164 sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
165 break;
166 }
167 }
168 #endif
169
170 /* If the file was opened successfully, read the schema for the new database.
171 ** If this fails, or if opening the file failed, then close the file and
172 ** remove the entry from the db->aDb[] array. i.e. put everything back the way
173 ** we found it.
174 */
175 if( rc==SQLITE_OK ){
176 sqlite3SafetyOn(db);
177 rc = sqlite3Init(db, &zErrDyn);
178 sqlite3SafetyOff(db);
179 }
180 if( rc ){
181 int iDb = db->nDb - 1;
182 assert( iDb>=2 );
183 if( db->aDb[iDb].pBt ){
184 sqlite3BtreeClose(db->aDb[iDb].pBt);
185 db->aDb[iDb].pBt = 0;
186 db->aDb[iDb].pSchema = 0;
187 }
188 sqlite3ResetInternalSchema(db, 0);
189 db->nDb = iDb;
190 if( rc==SQLITE_NOMEM ){
191 if( !sqlite3MallocFailed() ) sqlite3FailedMalloc();
192 sqlite3_snprintf(sizeof(zErr),zErr, "out of memory");
193 }else{
194 sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile);
195 }
196 goto attach_error;
197 }
198
199 return;
200
201 attach_error:
202 /* Return an error if we get here */
203 if( zErrDyn ){
204 sqlite3_result_error(context, zErrDyn, -1);
205 sqliteFree(zErrDyn);
206 }else{
207 zErr[sizeof(zErr)-1] = 0;
208 sqlite3_result_error(context, zErr, -1);
209 }
210 }
211
212 /*
213 ** An SQL user-function registered to do the work of an DETACH statement. The
214 ** three arguments to the function come directly from a detach statement:
215 **
216 ** DETACH DATABASE x
217 **
218 ** SELECT sqlite_detach(x)
219 */
220 static void detachFunc(
221 sqlite3_context *context,
222 int argc,
223 sqlite3_value **argv
224 ){
225 const char *zName = (const char *)sqlite3_value_text(argv[0]);
226 sqlite3 *db = sqlite3_user_data(context);
227 int i;
228 Db *pDb = 0;
229 char zErr[128];
230
231 if( zName==0 ) zName = "";
232 for(i=0; i<db->nDb; i++){
233 pDb = &db->aDb[i];
234 if( pDb->pBt==0 ) continue;
235 if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
236 }
237
238 if( i>=db->nDb ){
239 sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
240 goto detach_error;
241 }
242 if( i<2 ){
243 sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
244 goto detach_error;
245 }
246 if( !db->autoCommit ){
247 strcpy(zErr, "cannot DETACH database within transaction");
248 goto detach_error;
249 }
250
251 sqlite3BtreeClose(pDb->pBt);
252 pDb->pBt = 0;
253 pDb->pSchema = 0;
254 sqlite3ResetInternalSchema(db, 0);
255 return;
256
257 detach_error:
258 sqlite3_result_error(context, zErr, -1);
259 }
260
261 /*
262 ** This procedure generates VDBE code for a single invocation of either the
263 ** sqlite_detach() or sqlite_attach() SQL user functions.
264 */
265 static void codeAttach(
266 Parse *pParse, /* The parser context */
267 int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */
268 const char *zFunc, /* Either "sqlite_attach" or "sqlite_detach */
269 int nFunc, /* Number of args to pass to zFunc */
270 Expr *pAuthArg, /* Expression to pass to authorization callback */
271 Expr *pFilename, /* Name of database file */
272 Expr *pDbname, /* Name of the database to use internally */
273 Expr *pKey /* Database key for encryption extension */
274 ){
275 int rc;
276 NameContext sName;
277 Vdbe *v;
278 FuncDef *pFunc;
279 sqlite3* db = pParse->db;
280
281 #ifndef SQLITE_OMIT_AUTHORIZATION
282 assert( sqlite3MallocFailed() || pAuthArg );
283 if( pAuthArg ){
284 char *zAuthArg = sqlite3NameFromToken(&pAuthArg->span);
285 if( !zAuthArg ){
286 goto attach_end;
287 }
288 rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
289 sqliteFree(zAuthArg);
290 if(rc!=SQLITE_OK ){
291 goto attach_end;
292 }
293 }
294 #endif /* SQLITE_OMIT_AUTHORIZATION */
295
296 memset(&sName, 0, sizeof(NameContext));
297 sName.pParse = pParse;
298
299 if(
300 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
301 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
302 SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
303 ){
304 pParse->nErr++;
305 goto attach_end;
306 }
307
308 v = sqlite3GetVdbe(pParse);
309 sqlite3ExprCode(pParse, pFilename);
310 sqlite3ExprCode(pParse, pDbname);
311 sqlite3ExprCode(pParse, pKey);
312
313 assert( v || sqlite3MallocFailed() );
314 if( v ){
315 sqlite3VdbeAddOp(v, OP_Function, 0, nFunc);
316 pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
317 sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF);
318
319 /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
320 ** statement only). For DETACH, set it to false (expire all existing
321 ** statements).
322 */
323 sqlite3VdbeAddOp(v, OP_Expire, (type==SQLITE_ATTACH), 0);
324 }
325
326 attach_end:
327 sqlite3ExprDelete(pFilename);
328 sqlite3ExprDelete(pDbname);
329 sqlite3ExprDelete(pKey);
330 }
331
332 /*
333 ** Called by the parser to compile a DETACH statement.
334 **
335 ** DETACH pDbname
336 */
337 void sqlite3Detach(Parse *pParse, Expr *pDbname){
338 codeAttach(pParse, SQLITE_DETACH, "sqlite_detach", 1, pDbname, 0, 0, pDbname);
339 }
340
341 /*
342 ** Called by the parser to compile an ATTACH statement.
343 **
344 ** ATTACH p AS pDbname KEY pKey
345 */
346 void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
347 codeAttach(pParse, SQLITE_ATTACH, "sqlite_attach", 3, p, p, pDbname, pKey);
348 }
349
350 /*
351 ** Register the functions sqlite_attach and sqlite_detach.
352 */
353 void sqlite3AttachFunctions(sqlite3 *db){
354 static const int enc = SQLITE_UTF8;
355 sqlite3CreateFunc(db, "sqlite_attach", 3, enc, db, attachFunc, 0, 0);
356 sqlite3CreateFunc(db, "sqlite_detach", 1, enc, db, detachFunc, 0, 0);
357 }
358
359 /*
360 ** Initialize a DbFixer structure. This routine must be called prior
361 ** to passing the structure to one of the sqliteFixAAAA() routines below.
362 **
363 ** The return value indicates whether or not fixation is required. TRUE
364 ** means we do need to fix the database references, FALSE means we do not.
365 */
366 int sqlite3FixInit(
367 DbFixer *pFix, /* The fixer to be initialized */
368 Parse *pParse, /* Error messages will be written here */
369 int iDb, /* This is the database that must be used */
370 const char *zType, /* "view", "trigger", or "index" */
371 const Token *pName /* Name of the view, trigger, or index */
372 ){
373 sqlite3 *db;
374
375 if( iDb<0 || iDb==1 ) return 0;
376 db = pParse->db;
377 assert( db->nDb>iDb );
378 pFix->pParse = pParse;
379 pFix->zDb = db->aDb[iDb].zName;
380 pFix->zType = zType;
381 pFix->pName = pName;
382 return 1;
383 }
384
385 /*
386 ** The following set of routines walk through the parse tree and assign
387 ** a specific database to all table references where the database name
388 ** was left unspecified in the original SQL statement. The pFix structure
389 ** must have been initialized by a prior call to sqlite3FixInit().
390 **
391 ** These routines are used to make sure that an index, trigger, or
392 ** view in one database does not refer to objects in a different database.
393 ** (Exception: indices, triggers, and views in the TEMP database are
394 ** allowed to refer to anything.) If a reference is explicitly made
395 ** to an object in a different database, an error message is added to
396 ** pParse->zErrMsg and these routines return non-zero. If everything
397 ** checks out, these routines return 0.
398 */
399 int sqlite3FixSrcList(
400 DbFixer *pFix, /* Context of the fixation */
401 SrcList *pList /* The Source list to check and modify */
402 ){
403 int i;
404 const char *zDb;
405 struct SrcList_item *pItem;
406
407 if( pList==0 ) return 0;
408 zDb = pFix->zDb;
409 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
410 if( pItem->zDatabase==0 ){
411 pItem->zDatabase = sqliteStrDup(zDb);
412 }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
413 sqlite3ErrorMsg(pFix->pParse,
414 "%s %T cannot reference objects in database %s",
415 pFix->zType, pFix->pName, pItem->zDatabase);
416 return 1;
417 }
418 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
419 if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
420 if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
421 #endif
422 }
423 return 0;
424 }
425 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
426 int sqlite3FixSelect(
427 DbFixer *pFix, /* Context of the fixation */
428 Select *pSelect /* The SELECT statement to be fixed to one database */
429 ){
430 while( pSelect ){
431 if( sqlite3FixExprList(pFix, pSelect->pEList) ){
432 return 1;
433 }
434 if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
435 return 1;
436 }
437 if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
438 return 1;
439 }
440 if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
441 return 1;
442 }
443 pSelect = pSelect->pPrior;
444 }
445 return 0;
446 }
447 int sqlite3FixExpr(
448 DbFixer *pFix, /* Context of the fixation */
449 Expr *pExpr /* The expression to be fixed to one database */
450 ){
451 while( pExpr ){
452 if( sqlite3FixSelect(pFix, pExpr->pSelect) ){
453 return 1;
454 }
455 if( sqlite3FixExprList(pFix, pExpr->pList) ){
456 return 1;
457 }
458 if( sqlite3FixExpr(pFix, pExpr->pRight) ){
459 return 1;
460 }
461 pExpr = pExpr->pLeft;
462 }
463 return 0;
464 }
465 int sqlite3FixExprList(
466 DbFixer *pFix, /* Context of the fixation */
467 ExprList *pList /* The expression to be fixed to one database */
468 ){
469 int i;
470 struct ExprList_item *pItem;
471 if( pList==0 ) return 0;
472 for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
473 if( sqlite3FixExpr(pFix, pItem->pExpr) ){
474 return 1;
475 }
476 }
477 return 0;
478 }
479 #endif
480
481 #ifndef SQLITE_OMIT_TRIGGER
482 int sqlite3FixTriggerStep(
483 DbFixer *pFix, /* Context of the fixation */
484 TriggerStep *pStep /* The trigger step be fixed to one database */
485 ){
486 while( pStep ){
487 if( sqlite3FixSelect(pFix, pStep->pSelect) ){
488 return 1;
489 }
490 if( sqlite3FixExpr(pFix, pStep->pWhere) ){
491 return 1;
492 }
493 if( sqlite3FixExprList(pFix, pStep->pExprList) ){
494 return 1;
495 }
496 pStep = pStep->pNext;
497 }
498 return 0;
499 }
500 #endif