annotate sqlite/attach.c @ 1581:68aec931b0b6 trunk

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