|
1 /* |
|
2 ** 2001 September 15 |
|
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 C code routines that are called by the SQLite parser |
|
13 ** when syntax rules are reduced. The routines in this file handle the |
|
14 ** following kinds of SQL syntax: |
|
15 ** |
|
16 ** CREATE TABLE |
|
17 ** DROP TABLE |
|
18 ** CREATE INDEX |
|
19 ** DROP INDEX |
|
20 ** creating ID lists |
|
21 ** BEGIN TRANSACTION |
|
22 ** COMMIT |
|
23 ** ROLLBACK |
|
24 ** |
|
25 ** $Id: build.c,v 1.410 2006/08/14 14:23:42 drh Exp $ |
|
26 */ |
|
27 #include "sqliteInt.h" |
|
28 #include <ctype.h> |
|
29 |
|
30 /* |
|
31 ** This routine is called when a new SQL statement is beginning to |
|
32 ** be parsed. Initialize the pParse structure as needed. |
|
33 */ |
|
34 void sqlite3BeginParse(Parse *pParse, int explainFlag){ |
|
35 pParse->explain = explainFlag; |
|
36 pParse->nVar = 0; |
|
37 } |
|
38 |
|
39 #ifndef SQLITE_OMIT_SHARED_CACHE |
|
40 /* |
|
41 ** The TableLock structure is only used by the sqlite3TableLock() and |
|
42 ** codeTableLocks() functions. |
|
43 */ |
|
44 struct TableLock { |
|
45 int iDb; /* The database containing the table to be locked */ |
|
46 int iTab; /* The root page of the table to be locked */ |
|
47 u8 isWriteLock; /* True for write lock. False for a read lock */ |
|
48 const char *zName; /* Name of the table */ |
|
49 }; |
|
50 |
|
51 /* |
|
52 ** Record the fact that we want to lock a table at run-time. |
|
53 ** |
|
54 ** The table to be locked has root page iTab and is found in database iDb. |
|
55 ** A read or a write lock can be taken depending on isWritelock. |
|
56 ** |
|
57 ** This routine just records the fact that the lock is desired. The |
|
58 ** code to make the lock occur is generated by a later call to |
|
59 ** codeTableLocks() which occurs during sqlite3FinishCoding(). |
|
60 */ |
|
61 void sqlite3TableLock( |
|
62 Parse *pParse, /* Parsing context */ |
|
63 int iDb, /* Index of the database containing the table to lock */ |
|
64 int iTab, /* Root page number of the table to be locked */ |
|
65 u8 isWriteLock, /* True for a write lock */ |
|
66 const char *zName /* Name of the table to be locked */ |
|
67 ){ |
|
68 int i; |
|
69 int nBytes; |
|
70 TableLock *p; |
|
71 |
|
72 if( 0==sqlite3ThreadDataReadOnly()->useSharedData || iDb<0 ){ |
|
73 return; |
|
74 } |
|
75 |
|
76 for(i=0; i<pParse->nTableLock; i++){ |
|
77 p = &pParse->aTableLock[i]; |
|
78 if( p->iDb==iDb && p->iTab==iTab ){ |
|
79 p->isWriteLock = (p->isWriteLock || isWriteLock); |
|
80 return; |
|
81 } |
|
82 } |
|
83 |
|
84 nBytes = sizeof(TableLock) * (pParse->nTableLock+1); |
|
85 sqliteReallocOrFree((void **)&pParse->aTableLock, nBytes); |
|
86 if( pParse->aTableLock ){ |
|
87 p = &pParse->aTableLock[pParse->nTableLock++]; |
|
88 p->iDb = iDb; |
|
89 p->iTab = iTab; |
|
90 p->isWriteLock = isWriteLock; |
|
91 p->zName = zName; |
|
92 } |
|
93 } |
|
94 |
|
95 /* |
|
96 ** Code an OP_TableLock instruction for each table locked by the |
|
97 ** statement (configured by calls to sqlite3TableLock()). |
|
98 */ |
|
99 static void codeTableLocks(Parse *pParse){ |
|
100 int i; |
|
101 Vdbe *pVdbe; |
|
102 assert( sqlite3ThreadDataReadOnly()->useSharedData || pParse->nTableLock==0 ); |
|
103 |
|
104 if( 0==(pVdbe = sqlite3GetVdbe(pParse)) ){ |
|
105 return; |
|
106 } |
|
107 |
|
108 for(i=0; i<pParse->nTableLock; i++){ |
|
109 TableLock *p = &pParse->aTableLock[i]; |
|
110 int p1 = p->iDb; |
|
111 if( p->isWriteLock ){ |
|
112 p1 = -1*(p1+1); |
|
113 } |
|
114 sqlite3VdbeOp3(pVdbe, OP_TableLock, p1, p->iTab, p->zName, P3_STATIC); |
|
115 } |
|
116 } |
|
117 #else |
|
118 #define codeTableLocks(x) |
|
119 #endif |
|
120 |
|
121 /* |
|
122 ** This routine is called after a single SQL statement has been |
|
123 ** parsed and a VDBE program to execute that statement has been |
|
124 ** prepared. This routine puts the finishing touches on the |
|
125 ** VDBE program and resets the pParse structure for the next |
|
126 ** parse. |
|
127 ** |
|
128 ** Note that if an error occurred, it might be the case that |
|
129 ** no VDBE code was generated. |
|
130 */ |
|
131 void sqlite3FinishCoding(Parse *pParse){ |
|
132 sqlite3 *db; |
|
133 Vdbe *v; |
|
134 |
|
135 if( sqlite3MallocFailed() ) return; |
|
136 if( pParse->nested ) return; |
|
137 if( !pParse->pVdbe ){ |
|
138 if( pParse->rc==SQLITE_OK && pParse->nErr ){ |
|
139 pParse->rc = SQLITE_ERROR; |
|
140 return; |
|
141 } |
|
142 } |
|
143 |
|
144 /* Begin by generating some termination code at the end of the |
|
145 ** vdbe program |
|
146 */ |
|
147 db = pParse->db; |
|
148 v = sqlite3GetVdbe(pParse); |
|
149 if( v ){ |
|
150 sqlite3VdbeAddOp(v, OP_Halt, 0, 0); |
|
151 |
|
152 /* The cookie mask contains one bit for each database file open. |
|
153 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are |
|
154 ** set for each database that is used. Generate code to start a |
|
155 ** transaction on each used database and to verify the schema cookie |
|
156 ** on each used database. |
|
157 */ |
|
158 if( pParse->cookieGoto>0 ){ |
|
159 u32 mask; |
|
160 int iDb; |
|
161 sqlite3VdbeJumpHere(v, pParse->cookieGoto-1); |
|
162 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){ |
|
163 if( (mask & pParse->cookieMask)==0 ) continue; |
|
164 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0); |
|
165 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]); |
|
166 } |
|
167 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
168 if( pParse->pVirtualLock ){ |
|
169 char *vtab = (char *)pParse->pVirtualLock->pVtab; |
|
170 sqlite3VdbeOp3(v, OP_VBegin, 0, 0, vtab, P3_VTAB); |
|
171 } |
|
172 #endif |
|
173 |
|
174 /* Once all the cookies have been verified and transactions opened, |
|
175 ** obtain the required table-locks. This is a no-op unless the |
|
176 ** shared-cache feature is enabled. |
|
177 */ |
|
178 codeTableLocks(pParse); |
|
179 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto); |
|
180 } |
|
181 |
|
182 #ifndef SQLITE_OMIT_TRACE |
|
183 /* Add a No-op that contains the complete text of the compiled SQL |
|
184 ** statement as its P3 argument. This does not change the functionality |
|
185 ** of the program. |
|
186 ** |
|
187 ** This is used to implement sqlite3_trace(). |
|
188 */ |
|
189 sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql); |
|
190 #endif /* SQLITE_OMIT_TRACE */ |
|
191 } |
|
192 |
|
193 |
|
194 /* Get the VDBE program ready for execution |
|
195 */ |
|
196 if( v && pParse->nErr==0 && !sqlite3MallocFailed() ){ |
|
197 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; |
|
198 sqlite3VdbeTrace(v, trace); |
|
199 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3, |
|
200 pParse->nTab+3, pParse->explain); |
|
201 pParse->rc = SQLITE_DONE; |
|
202 pParse->colNamesSet = 0; |
|
203 }else if( pParse->rc==SQLITE_OK ){ |
|
204 pParse->rc = SQLITE_ERROR; |
|
205 } |
|
206 pParse->nTab = 0; |
|
207 pParse->nMem = 0; |
|
208 pParse->nSet = 0; |
|
209 pParse->nVar = 0; |
|
210 pParse->cookieMask = 0; |
|
211 pParse->cookieGoto = 0; |
|
212 } |
|
213 |
|
214 /* |
|
215 ** Run the parser and code generator recursively in order to generate |
|
216 ** code for the SQL statement given onto the end of the pParse context |
|
217 ** currently under construction. When the parser is run recursively |
|
218 ** this way, the final OP_Halt is not appended and other initialization |
|
219 ** and finalization steps are omitted because those are handling by the |
|
220 ** outermost parser. |
|
221 ** |
|
222 ** Not everything is nestable. This facility is designed to permit |
|
223 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use |
|
224 ** care if you decide to try to use this routine for some other purposes. |
|
225 */ |
|
226 void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ |
|
227 va_list ap; |
|
228 char *zSql; |
|
229 # define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar)) |
|
230 char saveBuf[SAVE_SZ]; |
|
231 |
|
232 if( pParse->nErr ) return; |
|
233 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ |
|
234 va_start(ap, zFormat); |
|
235 zSql = sqlite3VMPrintf(zFormat, ap); |
|
236 va_end(ap); |
|
237 if( zSql==0 ){ |
|
238 return; /* A malloc must have failed */ |
|
239 } |
|
240 pParse->nested++; |
|
241 memcpy(saveBuf, &pParse->nVar, SAVE_SZ); |
|
242 memset(&pParse->nVar, 0, SAVE_SZ); |
|
243 sqlite3RunParser(pParse, zSql, 0); |
|
244 sqliteFree(zSql); |
|
245 memcpy(&pParse->nVar, saveBuf, SAVE_SZ); |
|
246 pParse->nested--; |
|
247 } |
|
248 |
|
249 /* |
|
250 ** Locate the in-memory structure that describes a particular database |
|
251 ** table given the name of that table and (optionally) the name of the |
|
252 ** database containing the table. Return NULL if not found. |
|
253 ** |
|
254 ** If zDatabase is 0, all databases are searched for the table and the |
|
255 ** first matching table is returned. (No checking for duplicate table |
|
256 ** names is done.) The search order is TEMP first, then MAIN, then any |
|
257 ** auxiliary databases added using the ATTACH command. |
|
258 ** |
|
259 ** See also sqlite3LocateTable(). |
|
260 */ |
|
261 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ |
|
262 Table *p = 0; |
|
263 int i; |
|
264 assert( zName!=0 ); |
|
265 for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
|
266 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
|
267 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue; |
|
268 p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, strlen(zName)+1); |
|
269 if( p ) break; |
|
270 } |
|
271 return p; |
|
272 } |
|
273 |
|
274 /* |
|
275 ** Locate the in-memory structure that describes a particular database |
|
276 ** table given the name of that table and (optionally) the name of the |
|
277 ** database containing the table. Return NULL if not found. Also leave an |
|
278 ** error message in pParse->zErrMsg. |
|
279 ** |
|
280 ** The difference between this routine and sqlite3FindTable() is that this |
|
281 ** routine leaves an error message in pParse->zErrMsg where |
|
282 ** sqlite3FindTable() does not. |
|
283 */ |
|
284 Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){ |
|
285 Table *p; |
|
286 |
|
287 /* Read the database schema. If an error occurs, leave an error message |
|
288 ** and code in pParse and return NULL. */ |
|
289 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
|
290 return 0; |
|
291 } |
|
292 |
|
293 p = sqlite3FindTable(pParse->db, zName, zDbase); |
|
294 if( p==0 ){ |
|
295 if( zDbase ){ |
|
296 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName); |
|
297 }else{ |
|
298 sqlite3ErrorMsg(pParse, "no such table: %s", zName); |
|
299 } |
|
300 pParse->checkSchema = 1; |
|
301 } |
|
302 return p; |
|
303 } |
|
304 |
|
305 /* |
|
306 ** Locate the in-memory structure that describes |
|
307 ** a particular index given the name of that index |
|
308 ** and the name of the database that contains the index. |
|
309 ** Return NULL if not found. |
|
310 ** |
|
311 ** If zDatabase is 0, all databases are searched for the |
|
312 ** table and the first matching index is returned. (No checking |
|
313 ** for duplicate index names is done.) The search order is |
|
314 ** TEMP first, then MAIN, then any auxiliary databases added |
|
315 ** using the ATTACH command. |
|
316 */ |
|
317 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ |
|
318 Index *p = 0; |
|
319 int i; |
|
320 for(i=OMIT_TEMPDB; i<db->nDb; i++){ |
|
321 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ |
|
322 Schema *pSchema = db->aDb[j].pSchema; |
|
323 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; |
|
324 assert( pSchema || (j==1 && !db->aDb[1].pBt) ); |
|
325 if( pSchema ){ |
|
326 p = sqlite3HashFind(&pSchema->idxHash, zName, strlen(zName)+1); |
|
327 } |
|
328 if( p ) break; |
|
329 } |
|
330 return p; |
|
331 } |
|
332 |
|
333 /* |
|
334 ** Reclaim the memory used by an index |
|
335 */ |
|
336 static void freeIndex(Index *p){ |
|
337 sqliteFree(p->zColAff); |
|
338 sqliteFree(p); |
|
339 } |
|
340 |
|
341 /* |
|
342 ** Remove the given index from the index hash table, and free |
|
343 ** its memory structures. |
|
344 ** |
|
345 ** The index is removed from the database hash tables but |
|
346 ** it is not unlinked from the Table that it indexes. |
|
347 ** Unlinking from the Table must be done by the calling function. |
|
348 */ |
|
349 static void sqliteDeleteIndex(Index *p){ |
|
350 Index *pOld; |
|
351 const char *zName = p->zName; |
|
352 |
|
353 pOld = sqlite3HashInsert(&p->pSchema->idxHash, zName, strlen( zName)+1, 0); |
|
354 assert( pOld==0 || pOld==p ); |
|
355 freeIndex(p); |
|
356 } |
|
357 |
|
358 /* |
|
359 ** For the index called zIdxName which is found in the database iDb, |
|
360 ** unlike that index from its Table then remove the index from |
|
361 ** the index hash table and free all memory structures associated |
|
362 ** with the index. |
|
363 */ |
|
364 void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ |
|
365 Index *pIndex; |
|
366 int len; |
|
367 Hash *pHash = &db->aDb[iDb].pSchema->idxHash; |
|
368 |
|
369 len = strlen(zIdxName); |
|
370 pIndex = sqlite3HashInsert(pHash, zIdxName, len+1, 0); |
|
371 if( pIndex ){ |
|
372 if( pIndex->pTable->pIndex==pIndex ){ |
|
373 pIndex->pTable->pIndex = pIndex->pNext; |
|
374 }else{ |
|
375 Index *p; |
|
376 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){} |
|
377 if( p && p->pNext==pIndex ){ |
|
378 p->pNext = pIndex->pNext; |
|
379 } |
|
380 } |
|
381 freeIndex(pIndex); |
|
382 } |
|
383 db->flags |= SQLITE_InternChanges; |
|
384 } |
|
385 |
|
386 /* |
|
387 ** Erase all schema information from the in-memory hash tables of |
|
388 ** a single database. This routine is called to reclaim memory |
|
389 ** before the database closes. It is also called during a rollback |
|
390 ** if there were schema changes during the transaction or if a |
|
391 ** schema-cookie mismatch occurs. |
|
392 ** |
|
393 ** If iDb<=0 then reset the internal schema tables for all database |
|
394 ** files. If iDb>=2 then reset the internal schema for only the |
|
395 ** single file indicated. |
|
396 */ |
|
397 void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){ |
|
398 int i, j; |
|
399 |
|
400 assert( iDb>=0 && iDb<db->nDb ); |
|
401 for(i=iDb; i<db->nDb; i++){ |
|
402 Db *pDb = &db->aDb[i]; |
|
403 if( pDb->pSchema ){ |
|
404 sqlite3SchemaFree(pDb->pSchema); |
|
405 } |
|
406 if( iDb>0 ) return; |
|
407 } |
|
408 assert( iDb==0 ); |
|
409 db->flags &= ~SQLITE_InternChanges; |
|
410 |
|
411 /* If one or more of the auxiliary database files has been closed, |
|
412 ** then remove them from the auxiliary database list. We take the |
|
413 ** opportunity to do this here since we have just deleted all of the |
|
414 ** schema hash tables and therefore do not have to make any changes |
|
415 ** to any of those tables. |
|
416 */ |
|
417 for(i=0; i<db->nDb; i++){ |
|
418 struct Db *pDb = &db->aDb[i]; |
|
419 if( pDb->pBt==0 ){ |
|
420 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux); |
|
421 pDb->pAux = 0; |
|
422 } |
|
423 } |
|
424 for(i=j=2; i<db->nDb; i++){ |
|
425 struct Db *pDb = &db->aDb[i]; |
|
426 if( pDb->pBt==0 ){ |
|
427 sqliteFree(pDb->zName); |
|
428 pDb->zName = 0; |
|
429 continue; |
|
430 } |
|
431 if( j<i ){ |
|
432 db->aDb[j] = db->aDb[i]; |
|
433 } |
|
434 j++; |
|
435 } |
|
436 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j])); |
|
437 db->nDb = j; |
|
438 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ |
|
439 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); |
|
440 sqliteFree(db->aDb); |
|
441 db->aDb = db->aDbStatic; |
|
442 } |
|
443 } |
|
444 |
|
445 /* |
|
446 ** This routine is called whenever a rollback occurs. If there were |
|
447 ** schema changes during the transaction, then we have to reset the |
|
448 ** internal hash tables and reload them from disk. |
|
449 */ |
|
450 void sqlite3RollbackInternalChanges(sqlite3 *db){ |
|
451 if( db->flags & SQLITE_InternChanges ){ |
|
452 sqlite3ResetInternalSchema(db, 0); |
|
453 } |
|
454 } |
|
455 |
|
456 /* |
|
457 ** This routine is called when a commit occurs. |
|
458 */ |
|
459 void sqlite3CommitInternalChanges(sqlite3 *db){ |
|
460 db->flags &= ~SQLITE_InternChanges; |
|
461 } |
|
462 |
|
463 /* |
|
464 ** Clear the column names from a table or view. |
|
465 */ |
|
466 static void sqliteResetColumnNames(Table *pTable){ |
|
467 int i; |
|
468 Column *pCol; |
|
469 assert( pTable!=0 ); |
|
470 if( (pCol = pTable->aCol)!=0 ){ |
|
471 for(i=0; i<pTable->nCol; i++, pCol++){ |
|
472 sqliteFree(pCol->zName); |
|
473 sqlite3ExprDelete(pCol->pDflt); |
|
474 sqliteFree(pCol->zType); |
|
475 sqliteFree(pCol->zColl); |
|
476 } |
|
477 sqliteFree(pTable->aCol); |
|
478 } |
|
479 pTable->aCol = 0; |
|
480 pTable->nCol = 0; |
|
481 } |
|
482 |
|
483 /* |
|
484 ** Remove the memory data structures associated with the given |
|
485 ** Table. No changes are made to disk by this routine. |
|
486 ** |
|
487 ** This routine just deletes the data structure. It does not unlink |
|
488 ** the table data structure from the hash table. Nor does it remove |
|
489 ** foreign keys from the sqlite.aFKey hash table. But it does destroy |
|
490 ** memory structures of the indices and foreign keys associated with |
|
491 ** the table. |
|
492 ** |
|
493 ** Indices associated with the table are unlinked from the "db" |
|
494 ** data structure if db!=NULL. If db==NULL, indices attached to |
|
495 ** the table are deleted, but it is assumed they have already been |
|
496 ** unlinked. |
|
497 */ |
|
498 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ |
|
499 Index *pIndex, *pNext; |
|
500 FKey *pFKey, *pNextFKey; |
|
501 |
|
502 db = 0; |
|
503 |
|
504 if( pTable==0 ) return; |
|
505 |
|
506 /* Do not delete the table until the reference count reaches zero. */ |
|
507 pTable->nRef--; |
|
508 if( pTable->nRef>0 ){ |
|
509 return; |
|
510 } |
|
511 assert( pTable->nRef==0 ); |
|
512 |
|
513 /* Delete all indices associated with this table |
|
514 */ |
|
515 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ |
|
516 pNext = pIndex->pNext; |
|
517 assert( pIndex->pSchema==pTable->pSchema ); |
|
518 sqliteDeleteIndex(pIndex); |
|
519 } |
|
520 |
|
521 #ifndef SQLITE_OMIT_FOREIGN_KEY |
|
522 /* Delete all foreign keys associated with this table. The keys |
|
523 ** should have already been unlinked from the db->aFKey hash table |
|
524 */ |
|
525 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){ |
|
526 pNextFKey = pFKey->pNextFrom; |
|
527 assert( sqlite3HashFind(&pTable->pSchema->aFKey, |
|
528 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey ); |
|
529 sqliteFree(pFKey); |
|
530 } |
|
531 #endif |
|
532 |
|
533 /* Delete the Table structure itself. |
|
534 */ |
|
535 sqliteResetColumnNames(pTable); |
|
536 sqliteFree(pTable->zName); |
|
537 sqliteFree(pTable->zColAff); |
|
538 sqlite3SelectDelete(pTable->pSelect); |
|
539 #ifndef SQLITE_OMIT_CHECK |
|
540 sqlite3ExprDelete(pTable->pCheck); |
|
541 #endif |
|
542 sqlite3VtabClear(pTable); |
|
543 sqliteFree(pTable); |
|
544 } |
|
545 |
|
546 /* |
|
547 ** Unlink the given table from the hash tables and the delete the |
|
548 ** table structure with all its indices and foreign keys. |
|
549 */ |
|
550 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ |
|
551 Table *p; |
|
552 FKey *pF1, *pF2; |
|
553 Db *pDb; |
|
554 |
|
555 assert( db!=0 ); |
|
556 assert( iDb>=0 && iDb<db->nDb ); |
|
557 assert( zTabName && zTabName[0] ); |
|
558 pDb = &db->aDb[iDb]; |
|
559 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, strlen(zTabName)+1,0); |
|
560 if( p ){ |
|
561 #ifndef SQLITE_OMIT_FOREIGN_KEY |
|
562 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){ |
|
563 int nTo = strlen(pF1->zTo) + 1; |
|
564 pF2 = sqlite3HashFind(&pDb->pSchema->aFKey, pF1->zTo, nTo); |
|
565 if( pF2==pF1 ){ |
|
566 sqlite3HashInsert(&pDb->pSchema->aFKey, pF1->zTo, nTo, pF1->pNextTo); |
|
567 }else{ |
|
568 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; } |
|
569 if( pF2 ){ |
|
570 pF2->pNextTo = pF1->pNextTo; |
|
571 } |
|
572 } |
|
573 } |
|
574 #endif |
|
575 sqlite3DeleteTable(db, p); |
|
576 } |
|
577 db->flags |= SQLITE_InternChanges; |
|
578 } |
|
579 |
|
580 /* |
|
581 ** Given a token, return a string that consists of the text of that |
|
582 ** token with any quotations removed. Space to hold the returned string |
|
583 ** is obtained from sqliteMalloc() and must be freed by the calling |
|
584 ** function. |
|
585 ** |
|
586 ** Tokens are often just pointers into the original SQL text and so |
|
587 ** are not \000 terminated and are not persistent. The returned string |
|
588 ** is \000 terminated and is persistent. |
|
589 */ |
|
590 char *sqlite3NameFromToken(Token *pName){ |
|
591 char *zName; |
|
592 if( pName ){ |
|
593 zName = sqliteStrNDup((char*)pName->z, pName->n); |
|
594 sqlite3Dequote(zName); |
|
595 }else{ |
|
596 zName = 0; |
|
597 } |
|
598 return zName; |
|
599 } |
|
600 |
|
601 /* |
|
602 ** Open the sqlite_master table stored in database number iDb for |
|
603 ** writing. The table is opened using cursor 0. |
|
604 */ |
|
605 void sqlite3OpenMasterTable(Parse *p, int iDb){ |
|
606 Vdbe *v = sqlite3GetVdbe(p); |
|
607 sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb)); |
|
608 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
|
609 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT); |
|
610 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */ |
|
611 } |
|
612 |
|
613 /* |
|
614 ** The token *pName contains the name of a database (either "main" or |
|
615 ** "temp" or the name of an attached db). This routine returns the |
|
616 ** index of the named database in db->aDb[], or -1 if the named db |
|
617 ** does not exist. |
|
618 */ |
|
619 int sqlite3FindDb(sqlite3 *db, Token *pName){ |
|
620 int i = -1; /* Database number */ |
|
621 int n; /* Number of characters in the name */ |
|
622 Db *pDb; /* A database whose name space is being searched */ |
|
623 char *zName; /* Name we are searching for */ |
|
624 |
|
625 zName = sqlite3NameFromToken(pName); |
|
626 if( zName ){ |
|
627 n = strlen(zName); |
|
628 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ |
|
629 if( (!OMIT_TEMPDB || i!=1 ) && n==strlen(pDb->zName) && |
|
630 0==sqlite3StrICmp(pDb->zName, zName) ){ |
|
631 break; |
|
632 } |
|
633 } |
|
634 sqliteFree(zName); |
|
635 } |
|
636 return i; |
|
637 } |
|
638 |
|
639 /* The table or view or trigger name is passed to this routine via tokens |
|
640 ** pName1 and pName2. If the table name was fully qualified, for example: |
|
641 ** |
|
642 ** CREATE TABLE xxx.yyy (...); |
|
643 ** |
|
644 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
|
645 ** the table name is not fully qualified, i.e.: |
|
646 ** |
|
647 ** CREATE TABLE yyy(...); |
|
648 ** |
|
649 ** Then pName1 is set to "yyy" and pName2 is "". |
|
650 ** |
|
651 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or |
|
652 ** pName2) that stores the unqualified table name. The index of the |
|
653 ** database "xxx" is returned. |
|
654 */ |
|
655 int sqlite3TwoPartName( |
|
656 Parse *pParse, /* Parsing and code generating context */ |
|
657 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ |
|
658 Token *pName2, /* The "yyy" in the name "xxx.yyy" */ |
|
659 Token **pUnqual /* Write the unqualified object name here */ |
|
660 ){ |
|
661 int iDb; /* Database holding the object */ |
|
662 sqlite3 *db = pParse->db; |
|
663 |
|
664 if( pName2 && pName2->n>0 ){ |
|
665 assert( !db->init.busy ); |
|
666 *pUnqual = pName2; |
|
667 iDb = sqlite3FindDb(db, pName1); |
|
668 if( iDb<0 ){ |
|
669 sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
|
670 pParse->nErr++; |
|
671 return -1; |
|
672 } |
|
673 }else{ |
|
674 assert( db->init.iDb==0 || db->init.busy ); |
|
675 iDb = db->init.iDb; |
|
676 *pUnqual = pName1; |
|
677 } |
|
678 return iDb; |
|
679 } |
|
680 |
|
681 /* |
|
682 ** This routine is used to check if the UTF-8 string zName is a legal |
|
683 ** unqualified name for a new schema object (table, index, view or |
|
684 ** trigger). All names are legal except those that begin with the string |
|
685 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace |
|
686 ** is reserved for internal use. |
|
687 */ |
|
688 int sqlite3CheckObjectName(Parse *pParse, const char *zName){ |
|
689 if( !pParse->db->init.busy && pParse->nested==0 |
|
690 && (pParse->db->flags & SQLITE_WriteSchema)==0 |
|
691 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ |
|
692 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); |
|
693 return SQLITE_ERROR; |
|
694 } |
|
695 return SQLITE_OK; |
|
696 } |
|
697 |
|
698 /* |
|
699 ** Begin constructing a new table representation in memory. This is |
|
700 ** the first of several action routines that get called in response |
|
701 ** to a CREATE TABLE statement. In particular, this routine is called |
|
702 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp |
|
703 ** flag is true if the table should be stored in the auxiliary database |
|
704 ** file instead of in the main database file. This is normally the case |
|
705 ** when the "TEMP" or "TEMPORARY" keyword occurs in between |
|
706 ** CREATE and TABLE. |
|
707 ** |
|
708 ** The new table record is initialized and put in pParse->pNewTable. |
|
709 ** As more of the CREATE TABLE statement is parsed, additional action |
|
710 ** routines will be called to add more information to this record. |
|
711 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine |
|
712 ** is called to complete the construction of the new table record. |
|
713 */ |
|
714 void sqlite3StartTable( |
|
715 Parse *pParse, /* Parser context */ |
|
716 Token *pName1, /* First part of the name of the table or view */ |
|
717 Token *pName2, /* Second part of the name of the table or view */ |
|
718 int isTemp, /* True if this is a TEMP table */ |
|
719 int isView, /* True if this is a VIEW */ |
|
720 int isVirtual, /* True if this is a VIRTUAL table */ |
|
721 int noErr /* Do nothing if table already exists */ |
|
722 ){ |
|
723 Table *pTable; |
|
724 char *zName = 0; /* The name of the new table */ |
|
725 sqlite3 *db = pParse->db; |
|
726 Vdbe *v; |
|
727 int iDb; /* Database number to create the table in */ |
|
728 Token *pName; /* Unqualified name of the table to create */ |
|
729 |
|
730 /* The table or view name to create is passed to this routine via tokens |
|
731 ** pName1 and pName2. If the table name was fully qualified, for example: |
|
732 ** |
|
733 ** CREATE TABLE xxx.yyy (...); |
|
734 ** |
|
735 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if |
|
736 ** the table name is not fully qualified, i.e.: |
|
737 ** |
|
738 ** CREATE TABLE yyy(...); |
|
739 ** |
|
740 ** Then pName1 is set to "yyy" and pName2 is "". |
|
741 ** |
|
742 ** The call below sets the pName pointer to point at the token (pName1 or |
|
743 ** pName2) that stores the unqualified table name. The variable iDb is |
|
744 ** set to the index of the database that the table or view is to be |
|
745 ** created in. |
|
746 */ |
|
747 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
|
748 if( iDb<0 ) return; |
|
749 if( !OMIT_TEMPDB && isTemp && iDb>1 ){ |
|
750 /* If creating a temp table, the name may not be qualified */ |
|
751 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); |
|
752 return; |
|
753 } |
|
754 if( !OMIT_TEMPDB && isTemp ) iDb = 1; |
|
755 |
|
756 pParse->sNameToken = *pName; |
|
757 zName = sqlite3NameFromToken(pName); |
|
758 if( zName==0 ) return; |
|
759 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
|
760 goto begin_table_error; |
|
761 } |
|
762 if( db->init.iDb==1 ) isTemp = 1; |
|
763 #ifndef SQLITE_OMIT_AUTHORIZATION |
|
764 assert( (isTemp & 1)==isTemp ); |
|
765 { |
|
766 int code; |
|
767 char *zDb = db->aDb[iDb].zName; |
|
768 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ |
|
769 goto begin_table_error; |
|
770 } |
|
771 if( isView ){ |
|
772 if( !OMIT_TEMPDB && isTemp ){ |
|
773 code = SQLITE_CREATE_TEMP_VIEW; |
|
774 }else{ |
|
775 code = SQLITE_CREATE_VIEW; |
|
776 } |
|
777 }else{ |
|
778 if( !OMIT_TEMPDB && isTemp ){ |
|
779 code = SQLITE_CREATE_TEMP_TABLE; |
|
780 }else{ |
|
781 code = SQLITE_CREATE_TABLE; |
|
782 } |
|
783 } |
|
784 if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){ |
|
785 goto begin_table_error; |
|
786 } |
|
787 } |
|
788 #endif |
|
789 |
|
790 /* Make sure the new table name does not collide with an existing |
|
791 ** index or table name in the same database. Issue an error message if |
|
792 ** it does. The exception is if the statement being parsed was passed |
|
793 ** to an sqlite3_declare_vtab() call. In that case only the column names |
|
794 ** and types will be used, so there is no need to test for namespace |
|
795 ** collisions. |
|
796 */ |
|
797 if( !IN_DECLARE_VTAB ){ |
|
798 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
|
799 goto begin_table_error; |
|
800 } |
|
801 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName); |
|
802 if( pTable ){ |
|
803 if( !noErr ){ |
|
804 sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
|
805 } |
|
806 goto begin_table_error; |
|
807 } |
|
808 if( sqlite3FindIndex(db, zName, 0)!=0 && (iDb==0 || !db->init.busy) ){ |
|
809 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); |
|
810 goto begin_table_error; |
|
811 } |
|
812 } |
|
813 |
|
814 pTable = sqliteMalloc( sizeof(Table) ); |
|
815 if( pTable==0 ){ |
|
816 pParse->rc = SQLITE_NOMEM; |
|
817 pParse->nErr++; |
|
818 goto begin_table_error; |
|
819 } |
|
820 pTable->zName = zName; |
|
821 pTable->iPKey = -1; |
|
822 pTable->pSchema = db->aDb[iDb].pSchema; |
|
823 pTable->nRef = 1; |
|
824 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable); |
|
825 pParse->pNewTable = pTable; |
|
826 |
|
827 /* If this is the magic sqlite_sequence table used by autoincrement, |
|
828 ** then record a pointer to this table in the main database structure |
|
829 ** so that INSERT can find the table easily. |
|
830 */ |
|
831 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
832 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ |
|
833 pTable->pSchema->pSeqTab = pTable; |
|
834 } |
|
835 #endif |
|
836 |
|
837 /* Begin generating the code that will insert the table record into |
|
838 ** the SQLITE_MASTER table. Note in particular that we must go ahead |
|
839 ** and allocate the record number for the table entry now. Before any |
|
840 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause |
|
841 ** indices to be created and the table record must come before the |
|
842 ** indices. Hence, the record number for the table must be allocated |
|
843 ** now. |
|
844 */ |
|
845 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ |
|
846 int lbl; |
|
847 int fileFormat; |
|
848 sqlite3BeginWriteOperation(pParse, 0, iDb); |
|
849 |
|
850 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
851 if( isVirtual ){ |
|
852 sqlite3VdbeAddOp(v, OP_VBegin, 0, 0); |
|
853 } |
|
854 #endif |
|
855 |
|
856 /* If the file format and encoding in the database have not been set, |
|
857 ** set them now. |
|
858 */ |
|
859 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); /* file_format */ |
|
860 lbl = sqlite3VdbeMakeLabel(v); |
|
861 sqlite3VdbeAddOp(v, OP_If, 0, lbl); |
|
862 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? |
|
863 1 : SQLITE_MAX_FILE_FORMAT; |
|
864 sqlite3VdbeAddOp(v, OP_Integer, fileFormat, 0); |
|
865 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1); |
|
866 sqlite3VdbeAddOp(v, OP_Integer, ENC(db), 0); |
|
867 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4); |
|
868 sqlite3VdbeResolveLabel(v, lbl); |
|
869 |
|
870 /* This just creates a place-holder record in the sqlite_master table. |
|
871 ** The record created does not contain anything yet. It will be replaced |
|
872 ** by the real entry in code generated at sqlite3EndTable(). |
|
873 ** |
|
874 ** The rowid for the new entry is left on the top of the stack. |
|
875 ** The rowid value is needed by the code that sqlite3EndTable will |
|
876 ** generate. |
|
877 */ |
|
878 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
|
879 if( isView || isVirtual ){ |
|
880 sqlite3VdbeAddOp(v, OP_Integer, 0, 0); |
|
881 }else |
|
882 #endif |
|
883 { |
|
884 sqlite3VdbeAddOp(v, OP_CreateTable, iDb, 0); |
|
885 } |
|
886 sqlite3OpenMasterTable(pParse, iDb); |
|
887 sqlite3VdbeAddOp(v, OP_NewRowid, 0, 0); |
|
888 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
|
889 sqlite3VdbeAddOp(v, OP_Null, 0, 0); |
|
890 sqlite3VdbeAddOp(v, OP_Insert, 0, 0); |
|
891 sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
|
892 sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
|
893 } |
|
894 |
|
895 /* Normal (non-error) return. */ |
|
896 return; |
|
897 |
|
898 /* If an error occurs, we jump here */ |
|
899 begin_table_error: |
|
900 sqliteFree(zName); |
|
901 return; |
|
902 } |
|
903 |
|
904 /* |
|
905 ** This macro is used to compare two strings in a case-insensitive manner. |
|
906 ** It is slightly faster than calling sqlite3StrICmp() directly, but |
|
907 ** produces larger code. |
|
908 ** |
|
909 ** WARNING: This macro is not compatible with the strcmp() family. It |
|
910 ** returns true if the two strings are equal, otherwise false. |
|
911 */ |
|
912 #define STRICMP(x, y) (\ |
|
913 sqlite3UpperToLower[*(unsigned char *)(x)]== \ |
|
914 sqlite3UpperToLower[*(unsigned char *)(y)] \ |
|
915 && sqlite3StrICmp((x)+1,(y)+1)==0 ) |
|
916 |
|
917 /* |
|
918 ** Add a new column to the table currently being constructed. |
|
919 ** |
|
920 ** The parser calls this routine once for each column declaration |
|
921 ** in a CREATE TABLE statement. sqlite3StartTable() gets called |
|
922 ** first to get things going. Then this routine is called for each |
|
923 ** column. |
|
924 */ |
|
925 void sqlite3AddColumn(Parse *pParse, Token *pName){ |
|
926 Table *p; |
|
927 int i; |
|
928 char *z; |
|
929 Column *pCol; |
|
930 if( (p = pParse->pNewTable)==0 ) return; |
|
931 z = sqlite3NameFromToken(pName); |
|
932 if( z==0 ) return; |
|
933 for(i=0; i<p->nCol; i++){ |
|
934 if( STRICMP(z, p->aCol[i].zName) ){ |
|
935 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); |
|
936 sqliteFree(z); |
|
937 return; |
|
938 } |
|
939 } |
|
940 if( (p->nCol & 0x7)==0 ){ |
|
941 Column *aNew; |
|
942 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0])); |
|
943 if( aNew==0 ){ |
|
944 sqliteFree(z); |
|
945 return; |
|
946 } |
|
947 p->aCol = aNew; |
|
948 } |
|
949 pCol = &p->aCol[p->nCol]; |
|
950 memset(pCol, 0, sizeof(p->aCol[0])); |
|
951 pCol->zName = z; |
|
952 |
|
953 /* If there is no type specified, columns have the default affinity |
|
954 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will |
|
955 ** be called next to set pCol->affinity correctly. |
|
956 */ |
|
957 pCol->affinity = SQLITE_AFF_NONE; |
|
958 p->nCol++; |
|
959 } |
|
960 |
|
961 /* |
|
962 ** This routine is called by the parser while in the middle of |
|
963 ** parsing a CREATE TABLE statement. A "NOT NULL" constraint has |
|
964 ** been seen on a column. This routine sets the notNull flag on |
|
965 ** the column currently under construction. |
|
966 */ |
|
967 void sqlite3AddNotNull(Parse *pParse, int onError){ |
|
968 Table *p; |
|
969 int i; |
|
970 if( (p = pParse->pNewTable)==0 ) return; |
|
971 i = p->nCol-1; |
|
972 if( i>=0 ) p->aCol[i].notNull = onError; |
|
973 } |
|
974 |
|
975 /* |
|
976 ** Scan the column type name zType (length nType) and return the |
|
977 ** associated affinity type. |
|
978 ** |
|
979 ** This routine does a case-independent search of zType for the |
|
980 ** substrings in the following table. If one of the substrings is |
|
981 ** found, the corresponding affinity is returned. If zType contains |
|
982 ** more than one of the substrings, entries toward the top of |
|
983 ** the table take priority. For example, if zType is 'BLOBINT', |
|
984 ** SQLITE_AFF_INTEGER is returned. |
|
985 ** |
|
986 ** Substring | Affinity |
|
987 ** -------------------------------- |
|
988 ** 'INT' | SQLITE_AFF_INTEGER |
|
989 ** 'CHAR' | SQLITE_AFF_TEXT |
|
990 ** 'CLOB' | SQLITE_AFF_TEXT |
|
991 ** 'TEXT' | SQLITE_AFF_TEXT |
|
992 ** 'BLOB' | SQLITE_AFF_NONE |
|
993 ** 'REAL' | SQLITE_AFF_REAL |
|
994 ** 'FLOA' | SQLITE_AFF_REAL |
|
995 ** 'DOUB' | SQLITE_AFF_REAL |
|
996 ** |
|
997 ** If none of the substrings in the above table are found, |
|
998 ** SQLITE_AFF_NUMERIC is returned. |
|
999 */ |
|
1000 char sqlite3AffinityType(const Token *pType){ |
|
1001 u32 h = 0; |
|
1002 char aff = SQLITE_AFF_NUMERIC; |
|
1003 const unsigned char *zIn = pType->z; |
|
1004 const unsigned char *zEnd = &pType->z[pType->n]; |
|
1005 |
|
1006 while( zIn!=zEnd ){ |
|
1007 h = (h<<8) + sqlite3UpperToLower[*zIn]; |
|
1008 zIn++; |
|
1009 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ |
|
1010 aff = SQLITE_AFF_TEXT; |
|
1011 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ |
|
1012 aff = SQLITE_AFF_TEXT; |
|
1013 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ |
|
1014 aff = SQLITE_AFF_TEXT; |
|
1015 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ |
|
1016 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ |
|
1017 aff = SQLITE_AFF_NONE; |
|
1018 #ifndef SQLITE_OMIT_FLOATING_POINT |
|
1019 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ |
|
1020 && aff==SQLITE_AFF_NUMERIC ){ |
|
1021 aff = SQLITE_AFF_REAL; |
|
1022 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ |
|
1023 && aff==SQLITE_AFF_NUMERIC ){ |
|
1024 aff = SQLITE_AFF_REAL; |
|
1025 }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ |
|
1026 && aff==SQLITE_AFF_NUMERIC ){ |
|
1027 aff = SQLITE_AFF_REAL; |
|
1028 #endif |
|
1029 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ |
|
1030 aff = SQLITE_AFF_INTEGER; |
|
1031 break; |
|
1032 } |
|
1033 } |
|
1034 |
|
1035 return aff; |
|
1036 } |
|
1037 |
|
1038 /* |
|
1039 ** This routine is called by the parser while in the middle of |
|
1040 ** parsing a CREATE TABLE statement. The pFirst token is the first |
|
1041 ** token in the sequence of tokens that describe the type of the |
|
1042 ** column currently under construction. pLast is the last token |
|
1043 ** in the sequence. Use this information to construct a string |
|
1044 ** that contains the typename of the column and store that string |
|
1045 ** in zType. |
|
1046 */ |
|
1047 void sqlite3AddColumnType(Parse *pParse, Token *pType){ |
|
1048 Table *p; |
|
1049 int i; |
|
1050 Column *pCol; |
|
1051 |
|
1052 if( (p = pParse->pNewTable)==0 ) return; |
|
1053 i = p->nCol-1; |
|
1054 if( i<0 ) return; |
|
1055 pCol = &p->aCol[i]; |
|
1056 sqliteFree(pCol->zType); |
|
1057 pCol->zType = sqlite3NameFromToken(pType); |
|
1058 pCol->affinity = sqlite3AffinityType(pType); |
|
1059 } |
|
1060 |
|
1061 /* |
|
1062 ** The expression is the default value for the most recently added column |
|
1063 ** of the table currently under construction. |
|
1064 ** |
|
1065 ** Default value expressions must be constant. Raise an exception if this |
|
1066 ** is not the case. |
|
1067 ** |
|
1068 ** This routine is called by the parser while in the middle of |
|
1069 ** parsing a CREATE TABLE statement. |
|
1070 */ |
|
1071 void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){ |
|
1072 Table *p; |
|
1073 Column *pCol; |
|
1074 if( (p = pParse->pNewTable)!=0 ){ |
|
1075 pCol = &(p->aCol[p->nCol-1]); |
|
1076 if( !sqlite3ExprIsConstantOrFunction(pExpr) ){ |
|
1077 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", |
|
1078 pCol->zName); |
|
1079 }else{ |
|
1080 Expr *pCopy; |
|
1081 sqlite3ExprDelete(pCol->pDflt); |
|
1082 pCol->pDflt = pCopy = sqlite3ExprDup(pExpr); |
|
1083 if( pCopy ){ |
|
1084 sqlite3TokenCopy(&pCopy->span, &pExpr->span); |
|
1085 } |
|
1086 } |
|
1087 } |
|
1088 sqlite3ExprDelete(pExpr); |
|
1089 } |
|
1090 |
|
1091 /* |
|
1092 ** Designate the PRIMARY KEY for the table. pList is a list of names |
|
1093 ** of columns that form the primary key. If pList is NULL, then the |
|
1094 ** most recently added column of the table is the primary key. |
|
1095 ** |
|
1096 ** A table can have at most one primary key. If the table already has |
|
1097 ** a primary key (and this is the second primary key) then create an |
|
1098 ** error. |
|
1099 ** |
|
1100 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER, |
|
1101 ** then we will try to use that column as the rowid. Set the Table.iPKey |
|
1102 ** field of the table under construction to be the index of the |
|
1103 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is |
|
1104 ** no INTEGER PRIMARY KEY. |
|
1105 ** |
|
1106 ** If the key is not an INTEGER PRIMARY KEY, then create a unique |
|
1107 ** index for the key. No index is created for INTEGER PRIMARY KEYs. |
|
1108 */ |
|
1109 void sqlite3AddPrimaryKey( |
|
1110 Parse *pParse, /* Parsing context */ |
|
1111 ExprList *pList, /* List of field names to be indexed */ |
|
1112 int onError, /* What to do with a uniqueness conflict */ |
|
1113 int autoInc, /* True if the AUTOINCREMENT keyword is present */ |
|
1114 int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ |
|
1115 ){ |
|
1116 Table *pTab = pParse->pNewTable; |
|
1117 char *zType = 0; |
|
1118 int iCol = -1, i; |
|
1119 if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit; |
|
1120 if( pTab->hasPrimKey ){ |
|
1121 sqlite3ErrorMsg(pParse, |
|
1122 "table \"%s\" has more than one primary key", pTab->zName); |
|
1123 goto primary_key_exit; |
|
1124 } |
|
1125 pTab->hasPrimKey = 1; |
|
1126 if( pList==0 ){ |
|
1127 iCol = pTab->nCol - 1; |
|
1128 pTab->aCol[iCol].isPrimKey = 1; |
|
1129 }else{ |
|
1130 for(i=0; i<pList->nExpr; i++){ |
|
1131 for(iCol=0; iCol<pTab->nCol; iCol++){ |
|
1132 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){ |
|
1133 break; |
|
1134 } |
|
1135 } |
|
1136 if( iCol<pTab->nCol ){ |
|
1137 pTab->aCol[iCol].isPrimKey = 1; |
|
1138 } |
|
1139 } |
|
1140 if( pList->nExpr>1 ) iCol = -1; |
|
1141 } |
|
1142 if( iCol>=0 && iCol<pTab->nCol ){ |
|
1143 zType = pTab->aCol[iCol].zType; |
|
1144 } |
|
1145 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 |
|
1146 && sortOrder==SQLITE_SO_ASC ){ |
|
1147 pTab->iPKey = iCol; |
|
1148 pTab->keyConf = onError; |
|
1149 pTab->autoInc = autoInc; |
|
1150 }else if( autoInc ){ |
|
1151 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
1152 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " |
|
1153 "INTEGER PRIMARY KEY"); |
|
1154 #endif |
|
1155 }else{ |
|
1156 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0); |
|
1157 pList = 0; |
|
1158 } |
|
1159 |
|
1160 primary_key_exit: |
|
1161 sqlite3ExprListDelete(pList); |
|
1162 return; |
|
1163 } |
|
1164 |
|
1165 /* |
|
1166 ** Add a new CHECK constraint to the table currently under construction. |
|
1167 */ |
|
1168 void sqlite3AddCheckConstraint( |
|
1169 Parse *pParse, /* Parsing context */ |
|
1170 Expr *pCheckExpr /* The check expression */ |
|
1171 ){ |
|
1172 #ifndef SQLITE_OMIT_CHECK |
|
1173 Table *pTab = pParse->pNewTable; |
|
1174 if( pTab && !IN_DECLARE_VTAB ){ |
|
1175 /* The CHECK expression must be duplicated so that tokens refer |
|
1176 ** to malloced space and not the (ephemeral) text of the CREATE TABLE |
|
1177 ** statement */ |
|
1178 pTab->pCheck = sqlite3ExprAnd(pTab->pCheck, sqlite3ExprDup(pCheckExpr)); |
|
1179 } |
|
1180 #endif |
|
1181 sqlite3ExprDelete(pCheckExpr); |
|
1182 } |
|
1183 |
|
1184 /* |
|
1185 ** Set the collation function of the most recently parsed table column |
|
1186 ** to the CollSeq given. |
|
1187 */ |
|
1188 void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){ |
|
1189 Table *p; |
|
1190 int i; |
|
1191 |
|
1192 if( (p = pParse->pNewTable)==0 ) return; |
|
1193 i = p->nCol-1; |
|
1194 |
|
1195 if( sqlite3LocateCollSeq(pParse, zType, nType) ){ |
|
1196 Index *pIdx; |
|
1197 p->aCol[i].zColl = sqliteStrNDup(zType, nType); |
|
1198 |
|
1199 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>", |
|
1200 ** then an index may have been created on this column before the |
|
1201 ** collation type was added. Correct this if it is the case. |
|
1202 */ |
|
1203 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ |
|
1204 assert( pIdx->nColumn==1 ); |
|
1205 if( pIdx->aiColumn[0]==i ){ |
|
1206 pIdx->azColl[0] = p->aCol[i].zColl; |
|
1207 } |
|
1208 } |
|
1209 } |
|
1210 } |
|
1211 |
|
1212 /* |
|
1213 ** This function returns the collation sequence for database native text |
|
1214 ** encoding identified by the string zName, length nName. |
|
1215 ** |
|
1216 ** If the requested collation sequence is not available, or not available |
|
1217 ** in the database native encoding, the collation factory is invoked to |
|
1218 ** request it. If the collation factory does not supply such a sequence, |
|
1219 ** and the sequence is available in another text encoding, then that is |
|
1220 ** returned instead. |
|
1221 ** |
|
1222 ** If no versions of the requested collations sequence are available, or |
|
1223 ** another error occurs, NULL is returned and an error message written into |
|
1224 ** pParse. |
|
1225 */ |
|
1226 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){ |
|
1227 sqlite3 *db = pParse->db; |
|
1228 u8 enc = ENC(db); |
|
1229 u8 initbusy = db->init.busy; |
|
1230 CollSeq *pColl; |
|
1231 |
|
1232 pColl = sqlite3FindCollSeq(db, enc, zName, nName, initbusy); |
|
1233 if( !initbusy && (!pColl || !pColl->xCmp) ){ |
|
1234 pColl = sqlite3GetCollSeq(db, pColl, zName, nName); |
|
1235 if( !pColl ){ |
|
1236 if( nName<0 ){ |
|
1237 nName = strlen(zName); |
|
1238 } |
|
1239 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName); |
|
1240 pColl = 0; |
|
1241 } |
|
1242 } |
|
1243 |
|
1244 return pColl; |
|
1245 } |
|
1246 |
|
1247 |
|
1248 /* |
|
1249 ** Generate code that will increment the schema cookie. |
|
1250 ** |
|
1251 ** The schema cookie is used to determine when the schema for the |
|
1252 ** database changes. After each schema change, the cookie value |
|
1253 ** changes. When a process first reads the schema it records the |
|
1254 ** cookie. Thereafter, whenever it goes to access the database, |
|
1255 ** it checks the cookie to make sure the schema has not changed |
|
1256 ** since it was last read. |
|
1257 ** |
|
1258 ** This plan is not completely bullet-proof. It is possible for |
|
1259 ** the schema to change multiple times and for the cookie to be |
|
1260 ** set back to prior value. But schema changes are infrequent |
|
1261 ** and the probability of hitting the same cookie value is only |
|
1262 ** 1 chance in 2^32. So we're safe enough. |
|
1263 */ |
|
1264 void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){ |
|
1265 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, 0); |
|
1266 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0); |
|
1267 } |
|
1268 |
|
1269 /* |
|
1270 ** Measure the number of characters needed to output the given |
|
1271 ** identifier. The number returned includes any quotes used |
|
1272 ** but does not include the null terminator. |
|
1273 ** |
|
1274 ** The estimate is conservative. It might be larger that what is |
|
1275 ** really needed. |
|
1276 */ |
|
1277 static int identLength(const char *z){ |
|
1278 int n; |
|
1279 for(n=0; *z; n++, z++){ |
|
1280 if( *z=='"' ){ n++; } |
|
1281 } |
|
1282 return n + 2; |
|
1283 } |
|
1284 |
|
1285 /* |
|
1286 ** Write an identifier onto the end of the given string. Add |
|
1287 ** quote characters as needed. |
|
1288 */ |
|
1289 static void identPut(char *z, int *pIdx, char *zSignedIdent){ |
|
1290 unsigned char *zIdent = (unsigned char*)zSignedIdent; |
|
1291 int i, j, needQuote; |
|
1292 i = *pIdx; |
|
1293 for(j=0; zIdent[j]; j++){ |
|
1294 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break; |
|
1295 } |
|
1296 needQuote = zIdent[j]!=0 || isdigit(zIdent[0]) |
|
1297 || sqlite3KeywordCode(zIdent, j)!=TK_ID; |
|
1298 if( needQuote ) z[i++] = '"'; |
|
1299 for(j=0; zIdent[j]; j++){ |
|
1300 z[i++] = zIdent[j]; |
|
1301 if( zIdent[j]=='"' ) z[i++] = '"'; |
|
1302 } |
|
1303 if( needQuote ) z[i++] = '"'; |
|
1304 z[i] = 0; |
|
1305 *pIdx = i; |
|
1306 } |
|
1307 |
|
1308 /* |
|
1309 ** Generate a CREATE TABLE statement appropriate for the given |
|
1310 ** table. Memory to hold the text of the statement is obtained |
|
1311 ** from sqliteMalloc() and must be freed by the calling function. |
|
1312 */ |
|
1313 static char *createTableStmt(Table *p, int isTemp){ |
|
1314 int i, k, n; |
|
1315 char *zStmt; |
|
1316 char *zSep, *zSep2, *zEnd, *z; |
|
1317 Column *pCol; |
|
1318 n = 0; |
|
1319 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){ |
|
1320 n += identLength(pCol->zName); |
|
1321 z = pCol->zType; |
|
1322 if( z ){ |
|
1323 n += (strlen(z) + 1); |
|
1324 } |
|
1325 } |
|
1326 n += identLength(p->zName); |
|
1327 if( n<50 ){ |
|
1328 zSep = ""; |
|
1329 zSep2 = ","; |
|
1330 zEnd = ")"; |
|
1331 }else{ |
|
1332 zSep = "\n "; |
|
1333 zSep2 = ",\n "; |
|
1334 zEnd = "\n)"; |
|
1335 } |
|
1336 n += 35 + 6*p->nCol; |
|
1337 zStmt = sqliteMallocRaw( n ); |
|
1338 if( zStmt==0 ) return 0; |
|
1339 strcpy(zStmt, !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE "); |
|
1340 k = strlen(zStmt); |
|
1341 identPut(zStmt, &k, p->zName); |
|
1342 zStmt[k++] = '('; |
|
1343 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){ |
|
1344 strcpy(&zStmt[k], zSep); |
|
1345 k += strlen(&zStmt[k]); |
|
1346 zSep = zSep2; |
|
1347 identPut(zStmt, &k, pCol->zName); |
|
1348 if( (z = pCol->zType)!=0 ){ |
|
1349 zStmt[k++] = ' '; |
|
1350 strcpy(&zStmt[k], z); |
|
1351 k += strlen(z); |
|
1352 } |
|
1353 } |
|
1354 strcpy(&zStmt[k], zEnd); |
|
1355 return zStmt; |
|
1356 } |
|
1357 |
|
1358 /* |
|
1359 ** This routine is called to report the final ")" that terminates |
|
1360 ** a CREATE TABLE statement. |
|
1361 ** |
|
1362 ** The table structure that other action routines have been building |
|
1363 ** is added to the internal hash tables, assuming no errors have |
|
1364 ** occurred. |
|
1365 ** |
|
1366 ** An entry for the table is made in the master table on disk, unless |
|
1367 ** this is a temporary table or db->init.busy==1. When db->init.busy==1 |
|
1368 ** it means we are reading the sqlite_master table because we just |
|
1369 ** connected to the database or because the sqlite_master table has |
|
1370 ** recently changed, so the entry for this table already exists in |
|
1371 ** the sqlite_master table. We do not want to create it again. |
|
1372 ** |
|
1373 ** If the pSelect argument is not NULL, it means that this routine |
|
1374 ** was called to create a table generated from a |
|
1375 ** "CREATE TABLE ... AS SELECT ..." statement. The column names of |
|
1376 ** the new table will match the result set of the SELECT. |
|
1377 */ |
|
1378 void sqlite3EndTable( |
|
1379 Parse *pParse, /* Parse context */ |
|
1380 Token *pCons, /* The ',' token after the last column defn. */ |
|
1381 Token *pEnd, /* The final ')' token in the CREATE TABLE */ |
|
1382 Select *pSelect /* Select from a "CREATE ... AS SELECT" */ |
|
1383 ){ |
|
1384 Table *p; |
|
1385 sqlite3 *db = pParse->db; |
|
1386 int iDb; |
|
1387 |
|
1388 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3MallocFailed() ) { |
|
1389 return; |
|
1390 } |
|
1391 p = pParse->pNewTable; |
|
1392 if( p==0 ) return; |
|
1393 |
|
1394 assert( !db->init.busy || !pSelect ); |
|
1395 |
|
1396 iDb = sqlite3SchemaToIndex(db, p->pSchema); |
|
1397 |
|
1398 #ifndef SQLITE_OMIT_CHECK |
|
1399 /* Resolve names in all CHECK constraint expressions. |
|
1400 */ |
|
1401 if( p->pCheck ){ |
|
1402 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ |
|
1403 NameContext sNC; /* Name context for pParse->pNewTable */ |
|
1404 |
|
1405 memset(&sNC, 0, sizeof(sNC)); |
|
1406 memset(&sSrc, 0, sizeof(sSrc)); |
|
1407 sSrc.nSrc = 1; |
|
1408 sSrc.a[0].zName = p->zName; |
|
1409 sSrc.a[0].pTab = p; |
|
1410 sSrc.a[0].iCursor = -1; |
|
1411 sNC.pParse = pParse; |
|
1412 sNC.pSrcList = &sSrc; |
|
1413 sNC.isCheck = 1; |
|
1414 if( sqlite3ExprResolveNames(&sNC, p->pCheck) ){ |
|
1415 return; |
|
1416 } |
|
1417 } |
|
1418 #endif /* !defined(SQLITE_OMIT_CHECK) */ |
|
1419 |
|
1420 /* If the db->init.busy is 1 it means we are reading the SQL off the |
|
1421 ** "sqlite_master" or "sqlite_temp_master" table on the disk. |
|
1422 ** So do not write to the disk again. Extract the root page number |
|
1423 ** for the table from the db->init.newTnum field. (The page number |
|
1424 ** should have been put there by the sqliteOpenCb routine.) |
|
1425 */ |
|
1426 if( db->init.busy ){ |
|
1427 p->tnum = db->init.newTnum; |
|
1428 } |
|
1429 |
|
1430 /* If not initializing, then create a record for the new table |
|
1431 ** in the SQLITE_MASTER table of the database. The record number |
|
1432 ** for the new table entry should already be on the stack. |
|
1433 ** |
|
1434 ** If this is a TEMPORARY table, write the entry into the auxiliary |
|
1435 ** file instead of into the main database file. |
|
1436 */ |
|
1437 if( !db->init.busy ){ |
|
1438 int n; |
|
1439 Vdbe *v; |
|
1440 char *zType; /* "view" or "table" */ |
|
1441 char *zType2; /* "VIEW" or "TABLE" */ |
|
1442 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ |
|
1443 |
|
1444 v = sqlite3GetVdbe(pParse); |
|
1445 if( v==0 ) return; |
|
1446 |
|
1447 sqlite3VdbeAddOp(v, OP_Close, 0, 0); |
|
1448 |
|
1449 /* Create the rootpage for the new table and push it onto the stack. |
|
1450 ** A view has no rootpage, so just push a zero onto the stack for |
|
1451 ** views. Initialize zType at the same time. |
|
1452 */ |
|
1453 if( p->pSelect==0 ){ |
|
1454 /* A regular table */ |
|
1455 zType = "table"; |
|
1456 zType2 = "TABLE"; |
|
1457 #ifndef SQLITE_OMIT_VIEW |
|
1458 }else{ |
|
1459 /* A view */ |
|
1460 zType = "view"; |
|
1461 zType2 = "VIEW"; |
|
1462 #endif |
|
1463 } |
|
1464 |
|
1465 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT |
|
1466 ** statement to populate the new table. The root-page number for the |
|
1467 ** new table is on the top of the vdbe stack. |
|
1468 ** |
|
1469 ** Once the SELECT has been coded by sqlite3Select(), it is in a |
|
1470 ** suitable state to query for the column names and types to be used |
|
1471 ** by the new table. |
|
1472 ** |
|
1473 ** A shared-cache write-lock is not required to write to the new table, |
|
1474 ** as a schema-lock must have already been obtained to create it. Since |
|
1475 ** a schema-lock excludes all other database users, the write-lock would |
|
1476 ** be redundant. |
|
1477 */ |
|
1478 if( pSelect ){ |
|
1479 Table *pSelTab; |
|
1480 sqlite3VdbeAddOp(v, OP_Dup, 0, 0); |
|
1481 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
|
1482 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0); |
|
1483 pParse->nTab = 2; |
|
1484 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0); |
|
1485 sqlite3VdbeAddOp(v, OP_Close, 1, 0); |
|
1486 if( pParse->nErr==0 ){ |
|
1487 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect); |
|
1488 if( pSelTab==0 ) return; |
|
1489 assert( p->aCol==0 ); |
|
1490 p->nCol = pSelTab->nCol; |
|
1491 p->aCol = pSelTab->aCol; |
|
1492 pSelTab->nCol = 0; |
|
1493 pSelTab->aCol = 0; |
|
1494 sqlite3DeleteTable(0, pSelTab); |
|
1495 } |
|
1496 } |
|
1497 |
|
1498 /* Compute the complete text of the CREATE statement */ |
|
1499 if( pSelect ){ |
|
1500 zStmt = createTableStmt(p, p->pSchema==pParse->db->aDb[1].pSchema); |
|
1501 }else{ |
|
1502 n = pEnd->z - pParse->sNameToken.z + 1; |
|
1503 zStmt = sqlite3MPrintf("CREATE %s %.*s", zType2, n, pParse->sNameToken.z); |
|
1504 } |
|
1505 |
|
1506 /* A slot for the record has already been allocated in the |
|
1507 ** SQLITE_MASTER table. We just need to update that slot with all |
|
1508 ** the information we've collected. The rowid for the preallocated |
|
1509 ** slot is the 2nd item on the stack. The top of the stack is the |
|
1510 ** root page for the new table (or a 0 if this is a view). |
|
1511 */ |
|
1512 sqlite3NestedParse(pParse, |
|
1513 "UPDATE %Q.%s " |
|
1514 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#0, sql=%Q " |
|
1515 "WHERE rowid=#1", |
|
1516 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
|
1517 zType, |
|
1518 p->zName, |
|
1519 p->zName, |
|
1520 zStmt |
|
1521 ); |
|
1522 sqliteFree(zStmt); |
|
1523 sqlite3ChangeCookie(db, v, iDb); |
|
1524 |
|
1525 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
1526 /* Check to see if we need to create an sqlite_sequence table for |
|
1527 ** keeping track of autoincrement keys. |
|
1528 */ |
|
1529 if( p->autoInc ){ |
|
1530 Db *pDb = &db->aDb[iDb]; |
|
1531 if( pDb->pSchema->pSeqTab==0 ){ |
|
1532 sqlite3NestedParse(pParse, |
|
1533 "CREATE TABLE %Q.sqlite_sequence(name,seq)", |
|
1534 pDb->zName |
|
1535 ); |
|
1536 } |
|
1537 } |
|
1538 #endif |
|
1539 |
|
1540 /* Reparse everything to update our internal data structures */ |
|
1541 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, |
|
1542 sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC); |
|
1543 } |
|
1544 |
|
1545 |
|
1546 /* Add the table to the in-memory representation of the database. |
|
1547 */ |
|
1548 if( db->init.busy && pParse->nErr==0 ){ |
|
1549 Table *pOld; |
|
1550 FKey *pFKey; |
|
1551 Schema *pSchema = p->pSchema; |
|
1552 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, strlen(p->zName)+1,p); |
|
1553 if( pOld ){ |
|
1554 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ |
|
1555 return; |
|
1556 } |
|
1557 #ifndef SQLITE_OMIT_FOREIGN_KEY |
|
1558 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){ |
|
1559 int nTo = strlen(pFKey->zTo) + 1; |
|
1560 pFKey->pNextTo = sqlite3HashFind(&pSchema->aFKey, pFKey->zTo, nTo); |
|
1561 sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey); |
|
1562 } |
|
1563 #endif |
|
1564 pParse->pNewTable = 0; |
|
1565 db->nTable++; |
|
1566 db->flags |= SQLITE_InternChanges; |
|
1567 |
|
1568 #ifndef SQLITE_OMIT_ALTERTABLE |
|
1569 if( !p->pSelect ){ |
|
1570 const char *zName = (const char *)pParse->sNameToken.z; |
|
1571 int nName; |
|
1572 assert( !pSelect && pCons && pEnd ); |
|
1573 if( pCons->z==0 ){ |
|
1574 pCons = pEnd; |
|
1575 } |
|
1576 nName = (const char *)pCons->z - zName; |
|
1577 p->addColOffset = 13 + sqlite3utf8CharLen(zName, nName); |
|
1578 } |
|
1579 #endif |
|
1580 } |
|
1581 } |
|
1582 |
|
1583 #ifndef SQLITE_OMIT_VIEW |
|
1584 /* |
|
1585 ** The parser calls this routine in order to create a new VIEW |
|
1586 */ |
|
1587 void sqlite3CreateView( |
|
1588 Parse *pParse, /* The parsing context */ |
|
1589 Token *pBegin, /* The CREATE token that begins the statement */ |
|
1590 Token *pName1, /* The token that holds the name of the view */ |
|
1591 Token *pName2, /* The token that holds the name of the view */ |
|
1592 Select *pSelect, /* A SELECT statement that will become the new view */ |
|
1593 int isTemp /* TRUE for a TEMPORARY view */ |
|
1594 ){ |
|
1595 Table *p; |
|
1596 int n; |
|
1597 const unsigned char *z; |
|
1598 Token sEnd; |
|
1599 DbFixer sFix; |
|
1600 Token *pName; |
|
1601 int iDb; |
|
1602 |
|
1603 if( pParse->nVar>0 ){ |
|
1604 sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); |
|
1605 sqlite3SelectDelete(pSelect); |
|
1606 return; |
|
1607 } |
|
1608 sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, 0); |
|
1609 p = pParse->pNewTable; |
|
1610 if( p==0 || pParse->nErr ){ |
|
1611 sqlite3SelectDelete(pSelect); |
|
1612 return; |
|
1613 } |
|
1614 sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
|
1615 iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema); |
|
1616 if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) |
|
1617 && sqlite3FixSelect(&sFix, pSelect) |
|
1618 ){ |
|
1619 sqlite3SelectDelete(pSelect); |
|
1620 return; |
|
1621 } |
|
1622 |
|
1623 /* Make a copy of the entire SELECT statement that defines the view. |
|
1624 ** This will force all the Expr.token.z values to be dynamically |
|
1625 ** allocated rather than point to the input string - which means that |
|
1626 ** they will persist after the current sqlite3_exec() call returns. |
|
1627 */ |
|
1628 p->pSelect = sqlite3SelectDup(pSelect); |
|
1629 sqlite3SelectDelete(pSelect); |
|
1630 if( sqlite3MallocFailed() ){ |
|
1631 return; |
|
1632 } |
|
1633 if( !pParse->db->init.busy ){ |
|
1634 sqlite3ViewGetColumnNames(pParse, p); |
|
1635 } |
|
1636 |
|
1637 /* Locate the end of the CREATE VIEW statement. Make sEnd point to |
|
1638 ** the end. |
|
1639 */ |
|
1640 sEnd = pParse->sLastToken; |
|
1641 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ |
|
1642 sEnd.z += sEnd.n; |
|
1643 } |
|
1644 sEnd.n = 0; |
|
1645 n = sEnd.z - pBegin->z; |
|
1646 z = (const unsigned char*)pBegin->z; |
|
1647 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; } |
|
1648 sEnd.z = &z[n-1]; |
|
1649 sEnd.n = 1; |
|
1650 |
|
1651 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ |
|
1652 sqlite3EndTable(pParse, 0, &sEnd, 0); |
|
1653 return; |
|
1654 } |
|
1655 #endif /* SQLITE_OMIT_VIEW */ |
|
1656 |
|
1657 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
|
1658 /* |
|
1659 ** The Table structure pTable is really a VIEW. Fill in the names of |
|
1660 ** the columns of the view in the pTable structure. Return the number |
|
1661 ** of errors. If an error is seen leave an error message in pParse->zErrMsg. |
|
1662 */ |
|
1663 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ |
|
1664 Table *pSelTab; /* A fake table from which we get the result set */ |
|
1665 Select *pSel; /* Copy of the SELECT that implements the view */ |
|
1666 int nErr = 0; /* Number of errors encountered */ |
|
1667 int n; /* Temporarily holds the number of cursors assigned */ |
|
1668 |
|
1669 assert( pTable ); |
|
1670 |
|
1671 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
1672 if( sqlite3VtabCallConnect(pParse, pTable) ){ |
|
1673 return SQLITE_ERROR; |
|
1674 } |
|
1675 if( IsVirtual(pTable) ) return 0; |
|
1676 #endif |
|
1677 |
|
1678 #ifndef SQLITE_OMIT_VIEW |
|
1679 /* A positive nCol means the columns names for this view are |
|
1680 ** already known. |
|
1681 */ |
|
1682 if( pTable->nCol>0 ) return 0; |
|
1683 |
|
1684 /* A negative nCol is a special marker meaning that we are currently |
|
1685 ** trying to compute the column names. If we enter this routine with |
|
1686 ** a negative nCol, it means two or more views form a loop, like this: |
|
1687 ** |
|
1688 ** CREATE VIEW one AS SELECT * FROM two; |
|
1689 ** CREATE VIEW two AS SELECT * FROM one; |
|
1690 ** |
|
1691 ** Actually, this error is caught previously and so the following test |
|
1692 ** should always fail. But we will leave it in place just to be safe. |
|
1693 */ |
|
1694 if( pTable->nCol<0 ){ |
|
1695 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); |
|
1696 return 1; |
|
1697 } |
|
1698 assert( pTable->nCol>=0 ); |
|
1699 |
|
1700 /* If we get this far, it means we need to compute the table names. |
|
1701 ** Note that the call to sqlite3ResultSetOfSelect() will expand any |
|
1702 ** "*" elements in the results set of the view and will assign cursors |
|
1703 ** to the elements of the FROM clause. But we do not want these changes |
|
1704 ** to be permanent. So the computation is done on a copy of the SELECT |
|
1705 ** statement that defines the view. |
|
1706 */ |
|
1707 assert( pTable->pSelect ); |
|
1708 pSel = sqlite3SelectDup(pTable->pSelect); |
|
1709 if( pSel ){ |
|
1710 n = pParse->nTab; |
|
1711 sqlite3SrcListAssignCursors(pParse, pSel->pSrc); |
|
1712 pTable->nCol = -1; |
|
1713 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel); |
|
1714 pParse->nTab = n; |
|
1715 if( pSelTab ){ |
|
1716 assert( pTable->aCol==0 ); |
|
1717 pTable->nCol = pSelTab->nCol; |
|
1718 pTable->aCol = pSelTab->aCol; |
|
1719 pSelTab->nCol = 0; |
|
1720 pSelTab->aCol = 0; |
|
1721 sqlite3DeleteTable(0, pSelTab); |
|
1722 pTable->pSchema->flags |= DB_UnresetViews; |
|
1723 }else{ |
|
1724 pTable->nCol = 0; |
|
1725 nErr++; |
|
1726 } |
|
1727 sqlite3SelectDelete(pSel); |
|
1728 } else { |
|
1729 nErr++; |
|
1730 } |
|
1731 #endif /* SQLITE_OMIT_VIEW */ |
|
1732 return nErr; |
|
1733 } |
|
1734 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
|
1735 |
|
1736 #ifndef SQLITE_OMIT_VIEW |
|
1737 /* |
|
1738 ** Clear the column names from every VIEW in database idx. |
|
1739 */ |
|
1740 static void sqliteViewResetAll(sqlite3 *db, int idx){ |
|
1741 HashElem *i; |
|
1742 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; |
|
1743 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ |
|
1744 Table *pTab = sqliteHashData(i); |
|
1745 if( pTab->pSelect ){ |
|
1746 sqliteResetColumnNames(pTab); |
|
1747 } |
|
1748 } |
|
1749 DbClearProperty(db, idx, DB_UnresetViews); |
|
1750 } |
|
1751 #else |
|
1752 # define sqliteViewResetAll(A,B) |
|
1753 #endif /* SQLITE_OMIT_VIEW */ |
|
1754 |
|
1755 /* |
|
1756 ** This function is called by the VDBE to adjust the internal schema |
|
1757 ** used by SQLite when the btree layer moves a table root page. The |
|
1758 ** root-page of a table or index in database iDb has changed from iFrom |
|
1759 ** to iTo. |
|
1760 ** |
|
1761 ** Ticket #1728: The symbol table might still contain information |
|
1762 ** on tables and/or indices that are the process of being deleted. |
|
1763 ** If you are unlucky, one of those deleted indices or tables might |
|
1764 ** have the same rootpage number as the real table or index that is |
|
1765 ** being moved. So we cannot stop searching after the first match |
|
1766 ** because the first match might be for one of the deleted indices |
|
1767 ** or tables and not the table/index that is actually being moved. |
|
1768 ** We must continue looping until all tables and indices with |
|
1769 ** rootpage==iFrom have been converted to have a rootpage of iTo |
|
1770 ** in order to be certain that we got the right one. |
|
1771 */ |
|
1772 #ifndef SQLITE_OMIT_AUTOVACUUM |
|
1773 void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){ |
|
1774 HashElem *pElem; |
|
1775 Hash *pHash; |
|
1776 |
|
1777 pHash = &pDb->pSchema->tblHash; |
|
1778 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
|
1779 Table *pTab = sqliteHashData(pElem); |
|
1780 if( pTab->tnum==iFrom ){ |
|
1781 pTab->tnum = iTo; |
|
1782 } |
|
1783 } |
|
1784 pHash = &pDb->pSchema->idxHash; |
|
1785 for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ |
|
1786 Index *pIdx = sqliteHashData(pElem); |
|
1787 if( pIdx->tnum==iFrom ){ |
|
1788 pIdx->tnum = iTo; |
|
1789 } |
|
1790 } |
|
1791 } |
|
1792 #endif |
|
1793 |
|
1794 /* |
|
1795 ** Write code to erase the table with root-page iTable from database iDb. |
|
1796 ** Also write code to modify the sqlite_master table and internal schema |
|
1797 ** if a root-page of another table is moved by the btree-layer whilst |
|
1798 ** erasing iTable (this can happen with an auto-vacuum database). |
|
1799 */ |
|
1800 static void destroyRootPage(Parse *pParse, int iTable, int iDb){ |
|
1801 Vdbe *v = sqlite3GetVdbe(pParse); |
|
1802 sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb); |
|
1803 #ifndef SQLITE_OMIT_AUTOVACUUM |
|
1804 /* OP_Destroy pushes an integer onto the stack. If this integer |
|
1805 ** is non-zero, then it is the root page number of a table moved to |
|
1806 ** location iTable. The following code modifies the sqlite_master table to |
|
1807 ** reflect this. |
|
1808 ** |
|
1809 ** The "#0" in the SQL is a special constant that means whatever value |
|
1810 ** is on the top of the stack. See sqlite3RegisterExpr(). |
|
1811 */ |
|
1812 sqlite3NestedParse(pParse, |
|
1813 "UPDATE %Q.%s SET rootpage=%d WHERE #0 AND rootpage=#0", |
|
1814 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable); |
|
1815 #endif |
|
1816 } |
|
1817 |
|
1818 /* |
|
1819 ** Write VDBE code to erase table pTab and all associated indices on disk. |
|
1820 ** Code to update the sqlite_master tables and internal schema definitions |
|
1821 ** in case a root-page belonging to another table is moved by the btree layer |
|
1822 ** is also added (this can happen with an auto-vacuum database). |
|
1823 */ |
|
1824 static void destroyTable(Parse *pParse, Table *pTab){ |
|
1825 #ifdef SQLITE_OMIT_AUTOVACUUM |
|
1826 Index *pIdx; |
|
1827 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
|
1828 destroyRootPage(pParse, pTab->tnum, iDb); |
|
1829 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
|
1830 destroyRootPage(pParse, pIdx->tnum, iDb); |
|
1831 } |
|
1832 #else |
|
1833 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM |
|
1834 ** is not defined), then it is important to call OP_Destroy on the |
|
1835 ** table and index root-pages in order, starting with the numerically |
|
1836 ** largest root-page number. This guarantees that none of the root-pages |
|
1837 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the |
|
1838 ** following were coded: |
|
1839 ** |
|
1840 ** OP_Destroy 4 0 |
|
1841 ** ... |
|
1842 ** OP_Destroy 5 0 |
|
1843 ** |
|
1844 ** and root page 5 happened to be the largest root-page number in the |
|
1845 ** database, then root page 5 would be moved to page 4 by the |
|
1846 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit |
|
1847 ** a free-list page. |
|
1848 */ |
|
1849 int iTab = pTab->tnum; |
|
1850 int iDestroyed = 0; |
|
1851 |
|
1852 while( 1 ){ |
|
1853 Index *pIdx; |
|
1854 int iLargest = 0; |
|
1855 |
|
1856 if( iDestroyed==0 || iTab<iDestroyed ){ |
|
1857 iLargest = iTab; |
|
1858 } |
|
1859 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
|
1860 int iIdx = pIdx->tnum; |
|
1861 assert( pIdx->pSchema==pTab->pSchema ); |
|
1862 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){ |
|
1863 iLargest = iIdx; |
|
1864 } |
|
1865 } |
|
1866 if( iLargest==0 ){ |
|
1867 return; |
|
1868 }else{ |
|
1869 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
|
1870 destroyRootPage(pParse, iLargest, iDb); |
|
1871 iDestroyed = iLargest; |
|
1872 } |
|
1873 } |
|
1874 #endif |
|
1875 } |
|
1876 |
|
1877 /* |
|
1878 ** This routine is called to do the work of a DROP TABLE statement. |
|
1879 ** pName is the name of the table to be dropped. |
|
1880 */ |
|
1881 void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ |
|
1882 Table *pTab; |
|
1883 Vdbe *v; |
|
1884 sqlite3 *db = pParse->db; |
|
1885 int iDb; |
|
1886 |
|
1887 if( pParse->nErr || sqlite3MallocFailed() ){ |
|
1888 goto exit_drop_table; |
|
1889 } |
|
1890 assert( pName->nSrc==1 ); |
|
1891 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase); |
|
1892 |
|
1893 if( pTab==0 ){ |
|
1894 if( noErr ){ |
|
1895 sqlite3ErrorClear(pParse); |
|
1896 } |
|
1897 goto exit_drop_table; |
|
1898 } |
|
1899 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
|
1900 assert( iDb>=0 && iDb<db->nDb ); |
|
1901 #ifndef SQLITE_OMIT_AUTHORIZATION |
|
1902 { |
|
1903 int code; |
|
1904 const char *zTab = SCHEMA_TABLE(iDb); |
|
1905 const char *zDb = db->aDb[iDb].zName; |
|
1906 const char *zArg2 = 0; |
|
1907 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ |
|
1908 goto exit_drop_table; |
|
1909 } |
|
1910 if( isView ){ |
|
1911 if( !OMIT_TEMPDB && iDb==1 ){ |
|
1912 code = SQLITE_DROP_TEMP_VIEW; |
|
1913 }else{ |
|
1914 code = SQLITE_DROP_VIEW; |
|
1915 } |
|
1916 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
1917 }else if( IsVirtual(pTab) ){ |
|
1918 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
|
1919 goto exit_drop_table; |
|
1920 } |
|
1921 code = SQLITE_DROP_VTABLE; |
|
1922 zArg2 = pTab->pMod->zName; |
|
1923 #endif |
|
1924 }else{ |
|
1925 if( !OMIT_TEMPDB && iDb==1 ){ |
|
1926 code = SQLITE_DROP_TEMP_TABLE; |
|
1927 }else{ |
|
1928 code = SQLITE_DROP_TABLE; |
|
1929 } |
|
1930 } |
|
1931 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ |
|
1932 goto exit_drop_table; |
|
1933 } |
|
1934 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ |
|
1935 goto exit_drop_table; |
|
1936 } |
|
1937 } |
|
1938 #endif |
|
1939 if( pTab->readOnly || pTab==db->aDb[iDb].pSchema->pSeqTab ){ |
|
1940 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); |
|
1941 goto exit_drop_table; |
|
1942 } |
|
1943 |
|
1944 #ifndef SQLITE_OMIT_VIEW |
|
1945 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used |
|
1946 ** on a table. |
|
1947 */ |
|
1948 if( isView && pTab->pSelect==0 ){ |
|
1949 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); |
|
1950 goto exit_drop_table; |
|
1951 } |
|
1952 if( !isView && pTab->pSelect ){ |
|
1953 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); |
|
1954 goto exit_drop_table; |
|
1955 } |
|
1956 #endif |
|
1957 |
|
1958 /* Generate code to remove the table from the master table |
|
1959 ** on disk. |
|
1960 */ |
|
1961 v = sqlite3GetVdbe(pParse); |
|
1962 if( v ){ |
|
1963 Trigger *pTrigger; |
|
1964 Db *pDb = &db->aDb[iDb]; |
|
1965 sqlite3BeginWriteOperation(pParse, 0, iDb); |
|
1966 |
|
1967 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
1968 if( IsVirtual(pTab) ){ |
|
1969 Vdbe *v = sqlite3GetVdbe(pParse); |
|
1970 if( v ){ |
|
1971 sqlite3VdbeAddOp(v, OP_VBegin, 0, 0); |
|
1972 } |
|
1973 } |
|
1974 #endif |
|
1975 |
|
1976 /* Drop all triggers associated with the table being dropped. Code |
|
1977 ** is generated to remove entries from sqlite_master and/or |
|
1978 ** sqlite_temp_master if required. |
|
1979 */ |
|
1980 pTrigger = pTab->pTrigger; |
|
1981 while( pTrigger ){ |
|
1982 assert( pTrigger->pSchema==pTab->pSchema || |
|
1983 pTrigger->pSchema==db->aDb[1].pSchema ); |
|
1984 sqlite3DropTriggerPtr(pParse, pTrigger); |
|
1985 pTrigger = pTrigger->pNext; |
|
1986 } |
|
1987 |
|
1988 #ifndef SQLITE_OMIT_AUTOINCREMENT |
|
1989 /* Remove any entries of the sqlite_sequence table associated with |
|
1990 ** the table being dropped. This is done before the table is dropped |
|
1991 ** at the btree level, in case the sqlite_sequence table needs to |
|
1992 ** move as a result of the drop (can happen in auto-vacuum mode). |
|
1993 */ |
|
1994 if( pTab->autoInc ){ |
|
1995 sqlite3NestedParse(pParse, |
|
1996 "DELETE FROM %s.sqlite_sequence WHERE name=%Q", |
|
1997 pDb->zName, pTab->zName |
|
1998 ); |
|
1999 } |
|
2000 #endif |
|
2001 |
|
2002 /* Drop all SQLITE_MASTER table and index entries that refer to the |
|
2003 ** table. The program name loops through the master table and deletes |
|
2004 ** every row that refers to a table of the same name as the one being |
|
2005 ** dropped. Triggers are handled seperately because a trigger can be |
|
2006 ** created in the temp database that refers to a table in another |
|
2007 ** database. |
|
2008 */ |
|
2009 sqlite3NestedParse(pParse, |
|
2010 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", |
|
2011 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); |
|
2012 if( !isView && !IsVirtual(pTab) ){ |
|
2013 destroyTable(pParse, pTab); |
|
2014 } |
|
2015 |
|
2016 /* Remove the table entry from SQLite's internal schema and modify |
|
2017 ** the schema cookie. |
|
2018 */ |
|
2019 if( IsVirtual(pTab) ){ |
|
2020 sqlite3VdbeOp3(v, OP_VDestroy, iDb, 0, pTab->zName, 0); |
|
2021 } |
|
2022 sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0); |
|
2023 sqlite3ChangeCookie(db, v, iDb); |
|
2024 } |
|
2025 sqliteViewResetAll(db, iDb); |
|
2026 |
|
2027 exit_drop_table: |
|
2028 sqlite3SrcListDelete(pName); |
|
2029 } |
|
2030 |
|
2031 /* |
|
2032 ** This routine is called to create a new foreign key on the table |
|
2033 ** currently under construction. pFromCol determines which columns |
|
2034 ** in the current table point to the foreign key. If pFromCol==0 then |
|
2035 ** connect the key to the last column inserted. pTo is the name of |
|
2036 ** the table referred to. pToCol is a list of tables in the other |
|
2037 ** pTo table that the foreign key points to. flags contains all |
|
2038 ** information about the conflict resolution algorithms specified |
|
2039 ** in the ON DELETE, ON UPDATE and ON INSERT clauses. |
|
2040 ** |
|
2041 ** An FKey structure is created and added to the table currently |
|
2042 ** under construction in the pParse->pNewTable field. The new FKey |
|
2043 ** is not linked into db->aFKey at this point - that does not happen |
|
2044 ** until sqlite3EndTable(). |
|
2045 ** |
|
2046 ** The foreign key is set for IMMEDIATE processing. A subsequent call |
|
2047 ** to sqlite3DeferForeignKey() might change this to DEFERRED. |
|
2048 */ |
|
2049 void sqlite3CreateForeignKey( |
|
2050 Parse *pParse, /* Parsing context */ |
|
2051 ExprList *pFromCol, /* Columns in this table that point to other table */ |
|
2052 Token *pTo, /* Name of the other table */ |
|
2053 ExprList *pToCol, /* Columns in the other table */ |
|
2054 int flags /* Conflict resolution algorithms. */ |
|
2055 ){ |
|
2056 #ifndef SQLITE_OMIT_FOREIGN_KEY |
|
2057 FKey *pFKey = 0; |
|
2058 Table *p = pParse->pNewTable; |
|
2059 int nByte; |
|
2060 int i; |
|
2061 int nCol; |
|
2062 char *z; |
|
2063 |
|
2064 assert( pTo!=0 ); |
|
2065 if( p==0 || pParse->nErr || IN_DECLARE_VTAB ) goto fk_end; |
|
2066 if( pFromCol==0 ){ |
|
2067 int iCol = p->nCol-1; |
|
2068 if( iCol<0 ) goto fk_end; |
|
2069 if( pToCol && pToCol->nExpr!=1 ){ |
|
2070 sqlite3ErrorMsg(pParse, "foreign key on %s" |
|
2071 " should reference only one column of table %T", |
|
2072 p->aCol[iCol].zName, pTo); |
|
2073 goto fk_end; |
|
2074 } |
|
2075 nCol = 1; |
|
2076 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ |
|
2077 sqlite3ErrorMsg(pParse, |
|
2078 "number of columns in foreign key does not match the number of " |
|
2079 "columns in the referenced table"); |
|
2080 goto fk_end; |
|
2081 }else{ |
|
2082 nCol = pFromCol->nExpr; |
|
2083 } |
|
2084 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1; |
|
2085 if( pToCol ){ |
|
2086 for(i=0; i<pToCol->nExpr; i++){ |
|
2087 nByte += strlen(pToCol->a[i].zName) + 1; |
|
2088 } |
|
2089 } |
|
2090 pFKey = sqliteMalloc( nByte ); |
|
2091 if( pFKey==0 ) goto fk_end; |
|
2092 pFKey->pFrom = p; |
|
2093 pFKey->pNextFrom = p->pFKey; |
|
2094 z = (char*)&pFKey[1]; |
|
2095 pFKey->aCol = (struct sColMap*)z; |
|
2096 z += sizeof(struct sColMap)*nCol; |
|
2097 pFKey->zTo = z; |
|
2098 memcpy(z, pTo->z, pTo->n); |
|
2099 z[pTo->n] = 0; |
|
2100 z += pTo->n+1; |
|
2101 pFKey->pNextTo = 0; |
|
2102 pFKey->nCol = nCol; |
|
2103 if( pFromCol==0 ){ |
|
2104 pFKey->aCol[0].iFrom = p->nCol-1; |
|
2105 }else{ |
|
2106 for(i=0; i<nCol; i++){ |
|
2107 int j; |
|
2108 for(j=0; j<p->nCol; j++){ |
|
2109 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ |
|
2110 pFKey->aCol[i].iFrom = j; |
|
2111 break; |
|
2112 } |
|
2113 } |
|
2114 if( j>=p->nCol ){ |
|
2115 sqlite3ErrorMsg(pParse, |
|
2116 "unknown column \"%s\" in foreign key definition", |
|
2117 pFromCol->a[i].zName); |
|
2118 goto fk_end; |
|
2119 } |
|
2120 } |
|
2121 } |
|
2122 if( pToCol ){ |
|
2123 for(i=0; i<nCol; i++){ |
|
2124 int n = strlen(pToCol->a[i].zName); |
|
2125 pFKey->aCol[i].zCol = z; |
|
2126 memcpy(z, pToCol->a[i].zName, n); |
|
2127 z[n] = 0; |
|
2128 z += n+1; |
|
2129 } |
|
2130 } |
|
2131 pFKey->isDeferred = 0; |
|
2132 pFKey->deleteConf = flags & 0xff; |
|
2133 pFKey->updateConf = (flags >> 8 ) & 0xff; |
|
2134 pFKey->insertConf = (flags >> 16 ) & 0xff; |
|
2135 |
|
2136 /* Link the foreign key to the table as the last step. |
|
2137 */ |
|
2138 p->pFKey = pFKey; |
|
2139 pFKey = 0; |
|
2140 |
|
2141 fk_end: |
|
2142 sqliteFree(pFKey); |
|
2143 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
|
2144 sqlite3ExprListDelete(pFromCol); |
|
2145 sqlite3ExprListDelete(pToCol); |
|
2146 } |
|
2147 |
|
2148 /* |
|
2149 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED |
|
2150 ** clause is seen as part of a foreign key definition. The isDeferred |
|
2151 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. |
|
2152 ** The behavior of the most recently created foreign key is adjusted |
|
2153 ** accordingly. |
|
2154 */ |
|
2155 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ |
|
2156 #ifndef SQLITE_OMIT_FOREIGN_KEY |
|
2157 Table *pTab; |
|
2158 FKey *pFKey; |
|
2159 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; |
|
2160 pFKey->isDeferred = isDeferred; |
|
2161 #endif |
|
2162 } |
|
2163 |
|
2164 /* |
|
2165 ** Generate code that will erase and refill index *pIdx. This is |
|
2166 ** used to initialize a newly created index or to recompute the |
|
2167 ** content of an index in response to a REINDEX command. |
|
2168 ** |
|
2169 ** if memRootPage is not negative, it means that the index is newly |
|
2170 ** created. The memory cell specified by memRootPage contains the |
|
2171 ** root page number of the index. If memRootPage is negative, then |
|
2172 ** the index already exists and must be cleared before being refilled and |
|
2173 ** the root page number of the index is taken from pIndex->tnum. |
|
2174 */ |
|
2175 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ |
|
2176 Table *pTab = pIndex->pTable; /* The table that is indexed */ |
|
2177 int iTab = pParse->nTab; /* Btree cursor used for pTab */ |
|
2178 int iIdx = pParse->nTab+1; /* Btree cursor used for pIndex */ |
|
2179 int addr1; /* Address of top of loop */ |
|
2180 int tnum; /* Root page of index */ |
|
2181 Vdbe *v; /* Generate code into this virtual machine */ |
|
2182 KeyInfo *pKey; /* KeyInfo for index */ |
|
2183 int iDb = sqlite3SchemaToIndex(pParse->db, pIndex->pSchema); |
|
2184 |
|
2185 #ifndef SQLITE_OMIT_AUTHORIZATION |
|
2186 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, |
|
2187 pParse->db->aDb[iDb].zName ) ){ |
|
2188 return; |
|
2189 } |
|
2190 #endif |
|
2191 |
|
2192 /* Require a write-lock on the table to perform this operation */ |
|
2193 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); |
|
2194 |
|
2195 v = sqlite3GetVdbe(pParse); |
|
2196 if( v==0 ) return; |
|
2197 if( memRootPage>=0 ){ |
|
2198 sqlite3VdbeAddOp(v, OP_MemLoad, memRootPage, 0); |
|
2199 tnum = 0; |
|
2200 }else{ |
|
2201 tnum = pIndex->tnum; |
|
2202 sqlite3VdbeAddOp(v, OP_Clear, tnum, iDb); |
|
2203 } |
|
2204 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); |
|
2205 pKey = sqlite3IndexKeyinfo(pParse, pIndex); |
|
2206 sqlite3VdbeOp3(v, OP_OpenWrite, iIdx, tnum, (char *)pKey, P3_KEYINFO_HANDOFF); |
|
2207 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
|
2208 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0); |
|
2209 sqlite3GenerateIndexKey(v, pIndex, iTab); |
|
2210 if( pIndex->onError!=OE_None ){ |
|
2211 int curaddr = sqlite3VdbeCurrentAddr(v); |
|
2212 int addr2 = curaddr+4; |
|
2213 sqlite3VdbeChangeP2(v, curaddr-1, addr2); |
|
2214 sqlite3VdbeAddOp(v, OP_Rowid, iTab, 0); |
|
2215 sqlite3VdbeAddOp(v, OP_AddImm, 1, 0); |
|
2216 sqlite3VdbeAddOp(v, OP_IsUnique, iIdx, addr2); |
|
2217 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort, |
|
2218 "indexed columns are not unique", P3_STATIC); |
|
2219 assert( addr2==sqlite3VdbeCurrentAddr(v) ); |
|
2220 } |
|
2221 sqlite3VdbeAddOp(v, OP_IdxInsert, iIdx, 0); |
|
2222 sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1); |
|
2223 sqlite3VdbeJumpHere(v, addr1); |
|
2224 sqlite3VdbeAddOp(v, OP_Close, iTab, 0); |
|
2225 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); |
|
2226 } |
|
2227 |
|
2228 /* |
|
2229 ** Create a new index for an SQL table. pName1.pName2 is the name of the index |
|
2230 ** and pTblList is the name of the table that is to be indexed. Both will |
|
2231 ** be NULL for a primary key or an index that is created to satisfy a |
|
2232 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable |
|
2233 ** as the table to be indexed. pParse->pNewTable is a table that is |
|
2234 ** currently being constructed by a CREATE TABLE statement. |
|
2235 ** |
|
2236 ** pList is a list of columns to be indexed. pList will be NULL if this |
|
2237 ** is a primary key or unique-constraint on the most recent column added |
|
2238 ** to the table currently under construction. |
|
2239 */ |
|
2240 void sqlite3CreateIndex( |
|
2241 Parse *pParse, /* All information about this parse */ |
|
2242 Token *pName1, /* First part of index name. May be NULL */ |
|
2243 Token *pName2, /* Second part of index name. May be NULL */ |
|
2244 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ |
|
2245 ExprList *pList, /* A list of columns to be indexed */ |
|
2246 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
|
2247 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */ |
|
2248 Token *pEnd, /* The ")" that closes the CREATE INDEX statement */ |
|
2249 int sortOrder, /* Sort order of primary key when pList==NULL */ |
|
2250 int ifNotExist /* Omit error if index already exists */ |
|
2251 ){ |
|
2252 Table *pTab = 0; /* Table to be indexed */ |
|
2253 Index *pIndex = 0; /* The index to be created */ |
|
2254 char *zName = 0; /* Name of the index */ |
|
2255 int nName; /* Number of characters in zName */ |
|
2256 int i, j; |
|
2257 Token nullId; /* Fake token for an empty ID list */ |
|
2258 DbFixer sFix; /* For assigning database names to pTable */ |
|
2259 int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ |
|
2260 sqlite3 *db = pParse->db; |
|
2261 Db *pDb; /* The specific table containing the indexed database */ |
|
2262 int iDb; /* Index of the database that is being written */ |
|
2263 Token *pName = 0; /* Unqualified name of the index to create */ |
|
2264 struct ExprList_item *pListItem; /* For looping over pList */ |
|
2265 int nCol; |
|
2266 int nExtra = 0; |
|
2267 char *zExtra; |
|
2268 |
|
2269 if( pParse->nErr || sqlite3MallocFailed() || IN_DECLARE_VTAB ){ |
|
2270 goto exit_create_index; |
|
2271 } |
|
2272 |
|
2273 /* |
|
2274 ** Find the table that is to be indexed. Return early if not found. |
|
2275 */ |
|
2276 if( pTblName!=0 ){ |
|
2277 |
|
2278 /* Use the two-part index name to determine the database |
|
2279 ** to search for the table. 'Fix' the table name to this db |
|
2280 ** before looking up the table. |
|
2281 */ |
|
2282 assert( pName1 && pName2 ); |
|
2283 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); |
|
2284 if( iDb<0 ) goto exit_create_index; |
|
2285 |
|
2286 #ifndef SQLITE_OMIT_TEMPDB |
|
2287 /* If the index name was unqualified, check if the the table |
|
2288 ** is a temp table. If so, set the database to 1. |
|
2289 */ |
|
2290 pTab = sqlite3SrcListLookup(pParse, pTblName); |
|
2291 if( pName2 && pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ |
|
2292 iDb = 1; |
|
2293 } |
|
2294 #endif |
|
2295 |
|
2296 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) && |
|
2297 sqlite3FixSrcList(&sFix, pTblName) |
|
2298 ){ |
|
2299 /* Because the parser constructs pTblName from a single identifier, |
|
2300 ** sqlite3FixSrcList can never fail. */ |
|
2301 assert(0); |
|
2302 } |
|
2303 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName, |
|
2304 pTblName->a[0].zDatabase); |
|
2305 if( !pTab ) goto exit_create_index; |
|
2306 assert( db->aDb[iDb].pSchema==pTab->pSchema ); |
|
2307 }else{ |
|
2308 assert( pName==0 ); |
|
2309 pTab = pParse->pNewTable; |
|
2310 if( !pTab ) goto exit_create_index; |
|
2311 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
|
2312 } |
|
2313 pDb = &db->aDb[iDb]; |
|
2314 |
|
2315 if( pTab==0 || pParse->nErr ) goto exit_create_index; |
|
2316 if( pTab->readOnly ){ |
|
2317 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); |
|
2318 goto exit_create_index; |
|
2319 } |
|
2320 #ifndef SQLITE_OMIT_VIEW |
|
2321 if( pTab->pSelect ){ |
|
2322 sqlite3ErrorMsg(pParse, "views may not be indexed"); |
|
2323 goto exit_create_index; |
|
2324 } |
|
2325 #endif |
|
2326 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
2327 if( IsVirtual(pTab) ){ |
|
2328 sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); |
|
2329 goto exit_create_index; |
|
2330 } |
|
2331 #endif |
|
2332 |
|
2333 /* |
|
2334 ** Find the name of the index. Make sure there is not already another |
|
2335 ** index or table with the same name. |
|
2336 ** |
|
2337 ** Exception: If we are reading the names of permanent indices from the |
|
2338 ** sqlite_master table (because some other process changed the schema) and |
|
2339 ** one of the index names collides with the name of a temporary table or |
|
2340 ** index, then we will continue to process this index. |
|
2341 ** |
|
2342 ** If pName==0 it means that we are |
|
2343 ** dealing with a primary key or UNIQUE constraint. We have to invent our |
|
2344 ** own name. |
|
2345 */ |
|
2346 if( pName ){ |
|
2347 zName = sqlite3NameFromToken(pName); |
|
2348 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index; |
|
2349 if( zName==0 ) goto exit_create_index; |
|
2350 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ |
|
2351 goto exit_create_index; |
|
2352 } |
|
2353 if( !db->init.busy ){ |
|
2354 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index; |
|
2355 if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){ |
|
2356 if( !ifNotExist ){ |
|
2357 sqlite3ErrorMsg(pParse, "index %s already exists", zName); |
|
2358 } |
|
2359 goto exit_create_index; |
|
2360 } |
|
2361 if( sqlite3FindTable(db, zName, 0)!=0 ){ |
|
2362 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); |
|
2363 goto exit_create_index; |
|
2364 } |
|
2365 } |
|
2366 }else{ |
|
2367 char zBuf[30]; |
|
2368 int n; |
|
2369 Index *pLoop; |
|
2370 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} |
|
2371 sprintf(zBuf,"_%d",n); |
|
2372 zName = 0; |
|
2373 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0); |
|
2374 if( zName==0 ) goto exit_create_index; |
|
2375 } |
|
2376 |
|
2377 /* Check for authorization to create an index. |
|
2378 */ |
|
2379 #ifndef SQLITE_OMIT_AUTHORIZATION |
|
2380 { |
|
2381 const char *zDb = pDb->zName; |
|
2382 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ |
|
2383 goto exit_create_index; |
|
2384 } |
|
2385 i = SQLITE_CREATE_INDEX; |
|
2386 if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; |
|
2387 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ |
|
2388 goto exit_create_index; |
|
2389 } |
|
2390 } |
|
2391 #endif |
|
2392 |
|
2393 /* If pList==0, it means this routine was called to make a primary |
|
2394 ** key out of the last column added to the table under construction. |
|
2395 ** So create a fake list to simulate this. |
|
2396 */ |
|
2397 if( pList==0 ){ |
|
2398 nullId.z = (u8*)pTab->aCol[pTab->nCol-1].zName; |
|
2399 nullId.n = strlen((char*)nullId.z); |
|
2400 pList = sqlite3ExprListAppend(0, 0, &nullId); |
|
2401 if( pList==0 ) goto exit_create_index; |
|
2402 pList->a[0].sortOrder = sortOrder; |
|
2403 } |
|
2404 |
|
2405 /* Figure out how many bytes of space are required to store explicitly |
|
2406 ** specified collation sequence names. |
|
2407 */ |
|
2408 for(i=0; i<pList->nExpr; i++){ |
|
2409 Expr *pExpr = pList->a[i].pExpr; |
|
2410 if( pExpr ){ |
|
2411 nExtra += (1 + strlen(pExpr->pColl->zName)); |
|
2412 } |
|
2413 } |
|
2414 |
|
2415 /* |
|
2416 ** Allocate the index structure. |
|
2417 */ |
|
2418 nName = strlen(zName); |
|
2419 nCol = pList->nExpr; |
|
2420 pIndex = sqliteMalloc( |
|
2421 sizeof(Index) + /* Index structure */ |
|
2422 sizeof(int)*nCol + /* Index.aiColumn */ |
|
2423 sizeof(int)*(nCol+1) + /* Index.aiRowEst */ |
|
2424 sizeof(char *)*nCol + /* Index.azColl */ |
|
2425 sizeof(u8)*nCol + /* Index.aSortOrder */ |
|
2426 nName + 1 + /* Index.zName */ |
|
2427 nExtra /* Collation sequence names */ |
|
2428 ); |
|
2429 if( sqlite3MallocFailed() ) goto exit_create_index; |
|
2430 pIndex->azColl = (char**)(&pIndex[1]); |
|
2431 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]); |
|
2432 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]); |
|
2433 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]); |
|
2434 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); |
|
2435 zExtra = (char *)(&pIndex->zName[nName+1]); |
|
2436 strcpy(pIndex->zName, zName); |
|
2437 pIndex->pTable = pTab; |
|
2438 pIndex->nColumn = pList->nExpr; |
|
2439 pIndex->onError = onError; |
|
2440 pIndex->autoIndex = pName==0; |
|
2441 pIndex->pSchema = db->aDb[iDb].pSchema; |
|
2442 |
|
2443 /* Check to see if we should honor DESC requests on index columns |
|
2444 */ |
|
2445 if( pDb->pSchema->file_format>=4 ){ |
|
2446 sortOrderMask = -1; /* Honor DESC */ |
|
2447 }else{ |
|
2448 sortOrderMask = 0; /* Ignore DESC */ |
|
2449 } |
|
2450 |
|
2451 /* Scan the names of the columns of the table to be indexed and |
|
2452 ** load the column indices into the Index structure. Report an error |
|
2453 ** if any column is not found. |
|
2454 */ |
|
2455 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){ |
|
2456 const char *zColName = pListItem->zName; |
|
2457 Column *pTabCol; |
|
2458 int requestedSortOrder; |
|
2459 char *zColl; /* Collation sequence */ |
|
2460 |
|
2461 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){ |
|
2462 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break; |
|
2463 } |
|
2464 if( j>=pTab->nCol ){ |
|
2465 sqlite3ErrorMsg(pParse, "table %s has no column named %s", |
|
2466 pTab->zName, zColName); |
|
2467 goto exit_create_index; |
|
2468 } |
|
2469 pIndex->aiColumn[i] = j; |
|
2470 if( pListItem->pExpr ){ |
|
2471 assert( pListItem->pExpr->pColl ); |
|
2472 zColl = zExtra; |
|
2473 strcpy(zExtra, pListItem->pExpr->pColl->zName); |
|
2474 zExtra += (strlen(zColl) + 1); |
|
2475 }else{ |
|
2476 zColl = pTab->aCol[j].zColl; |
|
2477 if( !zColl ){ |
|
2478 zColl = db->pDfltColl->zName; |
|
2479 } |
|
2480 } |
|
2481 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl, -1) ){ |
|
2482 goto exit_create_index; |
|
2483 } |
|
2484 pIndex->azColl[i] = zColl; |
|
2485 requestedSortOrder = pListItem->sortOrder & sortOrderMask; |
|
2486 pIndex->aSortOrder[i] = requestedSortOrder; |
|
2487 } |
|
2488 sqlite3DefaultRowEst(pIndex); |
|
2489 |
|
2490 if( pTab==pParse->pNewTable ){ |
|
2491 /* This routine has been called to create an automatic index as a |
|
2492 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or |
|
2493 ** a PRIMARY KEY or UNIQUE clause following the column definitions. |
|
2494 ** i.e. one of: |
|
2495 ** |
|
2496 ** CREATE TABLE t(x PRIMARY KEY, y); |
|
2497 ** CREATE TABLE t(x, y, UNIQUE(x, y)); |
|
2498 ** |
|
2499 ** Either way, check to see if the table already has such an index. If |
|
2500 ** so, don't bother creating this one. This only applies to |
|
2501 ** automatically created indices. Users can do as they wish with |
|
2502 ** explicit indices. |
|
2503 */ |
|
2504 Index *pIdx; |
|
2505 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
|
2506 int k; |
|
2507 assert( pIdx->onError!=OE_None ); |
|
2508 assert( pIdx->autoIndex ); |
|
2509 assert( pIndex->onError!=OE_None ); |
|
2510 |
|
2511 if( pIdx->nColumn!=pIndex->nColumn ) continue; |
|
2512 for(k=0; k<pIdx->nColumn; k++){ |
|
2513 const char *z1 = pIdx->azColl[k]; |
|
2514 const char *z2 = pIndex->azColl[k]; |
|
2515 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; |
|
2516 if( pIdx->aSortOrder[k]!=pIndex->aSortOrder[k] ) break; |
|
2517 if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break; |
|
2518 } |
|
2519 if( k==pIdx->nColumn ){ |
|
2520 if( pIdx->onError!=pIndex->onError ){ |
|
2521 /* This constraint creates the same index as a previous |
|
2522 ** constraint specified somewhere in the CREATE TABLE statement. |
|
2523 ** However the ON CONFLICT clauses are different. If both this |
|
2524 ** constraint and the previous equivalent constraint have explicit |
|
2525 ** ON CONFLICT clauses this is an error. Otherwise, use the |
|
2526 ** explicitly specified behaviour for the index. |
|
2527 */ |
|
2528 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ |
|
2529 sqlite3ErrorMsg(pParse, |
|
2530 "conflicting ON CONFLICT clauses specified", 0); |
|
2531 } |
|
2532 if( pIdx->onError==OE_Default ){ |
|
2533 pIdx->onError = pIndex->onError; |
|
2534 } |
|
2535 } |
|
2536 goto exit_create_index; |
|
2537 } |
|
2538 } |
|
2539 } |
|
2540 |
|
2541 /* Link the new Index structure to its table and to the other |
|
2542 ** in-memory database structures. |
|
2543 */ |
|
2544 if( db->init.busy ){ |
|
2545 Index *p; |
|
2546 p = sqlite3HashInsert(&pIndex->pSchema->idxHash, |
|
2547 pIndex->zName, strlen(pIndex->zName)+1, pIndex); |
|
2548 if( p ){ |
|
2549 assert( p==pIndex ); /* Malloc must have failed */ |
|
2550 goto exit_create_index; |
|
2551 } |
|
2552 db->flags |= SQLITE_InternChanges; |
|
2553 if( pTblName!=0 ){ |
|
2554 pIndex->tnum = db->init.newTnum; |
|
2555 } |
|
2556 } |
|
2557 |
|
2558 /* If the db->init.busy is 0 then create the index on disk. This |
|
2559 ** involves writing the index into the master table and filling in the |
|
2560 ** index with the current table contents. |
|
2561 ** |
|
2562 ** The db->init.busy is 0 when the user first enters a CREATE INDEX |
|
2563 ** command. db->init.busy is 1 when a database is opened and |
|
2564 ** CREATE INDEX statements are read out of the master table. In |
|
2565 ** the latter case the index already exists on disk, which is why |
|
2566 ** we don't want to recreate it. |
|
2567 ** |
|
2568 ** If pTblName==0 it means this index is generated as a primary key |
|
2569 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table |
|
2570 ** has just been created, it contains no data and the index initialization |
|
2571 ** step can be skipped. |
|
2572 */ |
|
2573 else if( db->init.busy==0 ){ |
|
2574 Vdbe *v; |
|
2575 char *zStmt; |
|
2576 int iMem = pParse->nMem++; |
|
2577 |
|
2578 v = sqlite3GetVdbe(pParse); |
|
2579 if( v==0 ) goto exit_create_index; |
|
2580 |
|
2581 |
|
2582 /* Create the rootpage for the index |
|
2583 */ |
|
2584 sqlite3BeginWriteOperation(pParse, 1, iDb); |
|
2585 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0); |
|
2586 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); |
|
2587 |
|
2588 /* Gather the complete text of the CREATE INDEX statement into |
|
2589 ** the zStmt variable |
|
2590 */ |
|
2591 if( pStart && pEnd ){ |
|
2592 /* A named index with an explicit CREATE INDEX statement */ |
|
2593 zStmt = sqlite3MPrintf("CREATE%s INDEX %.*s", |
|
2594 onError==OE_None ? "" : " UNIQUE", |
|
2595 pEnd->z - pName->z + 1, |
|
2596 pName->z); |
|
2597 }else{ |
|
2598 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ |
|
2599 /* zStmt = sqlite3MPrintf(""); */ |
|
2600 zStmt = 0; |
|
2601 } |
|
2602 |
|
2603 /* Add an entry in sqlite_master for this index |
|
2604 */ |
|
2605 sqlite3NestedParse(pParse, |
|
2606 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#0,%Q);", |
|
2607 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
|
2608 pIndex->zName, |
|
2609 pTab->zName, |
|
2610 zStmt |
|
2611 ); |
|
2612 sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
|
2613 sqliteFree(zStmt); |
|
2614 |
|
2615 /* Fill the index with data and reparse the schema. Code an OP_Expire |
|
2616 ** to invalidate all pre-compiled statements. |
|
2617 */ |
|
2618 if( pTblName ){ |
|
2619 sqlite3RefillIndex(pParse, pIndex, iMem); |
|
2620 sqlite3ChangeCookie(db, v, iDb); |
|
2621 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, |
|
2622 sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC); |
|
2623 sqlite3VdbeAddOp(v, OP_Expire, 0, 0); |
|
2624 } |
|
2625 } |
|
2626 |
|
2627 /* When adding an index to the list of indices for a table, make |
|
2628 ** sure all indices labeled OE_Replace come after all those labeled |
|
2629 ** OE_Ignore. This is necessary for the correct operation of UPDATE |
|
2630 ** and INSERT. |
|
2631 */ |
|
2632 if( db->init.busy || pTblName==0 ){ |
|
2633 if( onError!=OE_Replace || pTab->pIndex==0 |
|
2634 || pTab->pIndex->onError==OE_Replace){ |
|
2635 pIndex->pNext = pTab->pIndex; |
|
2636 pTab->pIndex = pIndex; |
|
2637 }else{ |
|
2638 Index *pOther = pTab->pIndex; |
|
2639 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ |
|
2640 pOther = pOther->pNext; |
|
2641 } |
|
2642 pIndex->pNext = pOther->pNext; |
|
2643 pOther->pNext = pIndex; |
|
2644 } |
|
2645 pIndex = 0; |
|
2646 } |
|
2647 |
|
2648 /* Clean up before exiting */ |
|
2649 exit_create_index: |
|
2650 if( pIndex ){ |
|
2651 freeIndex(pIndex); |
|
2652 } |
|
2653 sqlite3ExprListDelete(pList); |
|
2654 sqlite3SrcListDelete(pTblName); |
|
2655 sqliteFree(zName); |
|
2656 return; |
|
2657 } |
|
2658 |
|
2659 /* |
|
2660 ** Generate code to make sure the file format number is at least minFormat. |
|
2661 ** The generated code will increase the file format number if necessary. |
|
2662 */ |
|
2663 void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){ |
|
2664 Vdbe *v; |
|
2665 v = sqlite3GetVdbe(pParse); |
|
2666 if( v ){ |
|
2667 sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); |
|
2668 sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0); |
|
2669 sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3); |
|
2670 sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0); |
|
2671 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1); |
|
2672 } |
|
2673 } |
|
2674 |
|
2675 /* |
|
2676 ** Fill the Index.aiRowEst[] array with default information - information |
|
2677 ** to be used when we have not run the ANALYZE command. |
|
2678 ** |
|
2679 ** aiRowEst[0] is suppose to contain the number of elements in the index. |
|
2680 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the |
|
2681 ** number of rows in the table that match any particular value of the |
|
2682 ** first column of the index. aiRowEst[2] is an estimate of the number |
|
2683 ** of rows that match any particular combiniation of the first 2 columns |
|
2684 ** of the index. And so forth. It must always be the case that |
|
2685 * |
|
2686 ** aiRowEst[N]<=aiRowEst[N-1] |
|
2687 ** aiRowEst[N]>=1 |
|
2688 ** |
|
2689 ** Apart from that, we have little to go on besides intuition as to |
|
2690 ** how aiRowEst[] should be initialized. The numbers generated here |
|
2691 ** are based on typical values found in actual indices. |
|
2692 */ |
|
2693 void sqlite3DefaultRowEst(Index *pIdx){ |
|
2694 unsigned *a = pIdx->aiRowEst; |
|
2695 int i; |
|
2696 assert( a!=0 ); |
|
2697 a[0] = 1000000; |
|
2698 for(i=pIdx->nColumn; i>=5; i--){ |
|
2699 a[i] = 5; |
|
2700 } |
|
2701 while( i>=1 ){ |
|
2702 a[i] = 11 - i; |
|
2703 i--; |
|
2704 } |
|
2705 if( pIdx->onError!=OE_None ){ |
|
2706 a[pIdx->nColumn] = 1; |
|
2707 } |
|
2708 } |
|
2709 |
|
2710 /* |
|
2711 ** This routine will drop an existing named index. This routine |
|
2712 ** implements the DROP INDEX statement. |
|
2713 */ |
|
2714 void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ |
|
2715 Index *pIndex; |
|
2716 Vdbe *v; |
|
2717 sqlite3 *db = pParse->db; |
|
2718 int iDb; |
|
2719 |
|
2720 if( pParse->nErr || sqlite3MallocFailed() ){ |
|
2721 goto exit_drop_index; |
|
2722 } |
|
2723 assert( pName->nSrc==1 ); |
|
2724 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
|
2725 goto exit_drop_index; |
|
2726 } |
|
2727 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); |
|
2728 if( pIndex==0 ){ |
|
2729 if( !ifExists ){ |
|
2730 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); |
|
2731 } |
|
2732 pParse->checkSchema = 1; |
|
2733 goto exit_drop_index; |
|
2734 } |
|
2735 if( pIndex->autoIndex ){ |
|
2736 sqlite3ErrorMsg(pParse, "index associated with UNIQUE " |
|
2737 "or PRIMARY KEY constraint cannot be dropped", 0); |
|
2738 goto exit_drop_index; |
|
2739 } |
|
2740 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); |
|
2741 #ifndef SQLITE_OMIT_AUTHORIZATION |
|
2742 { |
|
2743 int code = SQLITE_DROP_INDEX; |
|
2744 Table *pTab = pIndex->pTable; |
|
2745 const char *zDb = db->aDb[iDb].zName; |
|
2746 const char *zTab = SCHEMA_TABLE(iDb); |
|
2747 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ |
|
2748 goto exit_drop_index; |
|
2749 } |
|
2750 if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; |
|
2751 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ |
|
2752 goto exit_drop_index; |
|
2753 } |
|
2754 } |
|
2755 #endif |
|
2756 |
|
2757 /* Generate code to remove the index and from the master table */ |
|
2758 v = sqlite3GetVdbe(pParse); |
|
2759 if( v ){ |
|
2760 sqlite3NestedParse(pParse, |
|
2761 "DELETE FROM %Q.%s WHERE name=%Q", |
|
2762 db->aDb[iDb].zName, SCHEMA_TABLE(iDb), |
|
2763 pIndex->zName |
|
2764 ); |
|
2765 sqlite3ChangeCookie(db, v, iDb); |
|
2766 destroyRootPage(pParse, pIndex->tnum, iDb); |
|
2767 sqlite3VdbeOp3(v, OP_DropIndex, iDb, 0, pIndex->zName, 0); |
|
2768 } |
|
2769 |
|
2770 exit_drop_index: |
|
2771 sqlite3SrcListDelete(pName); |
|
2772 } |
|
2773 |
|
2774 /* |
|
2775 ** ppArray points into a structure where there is an array pointer |
|
2776 ** followed by two integers. The first integer is the |
|
2777 ** number of elements in the structure array. The second integer |
|
2778 ** is the number of allocated slots in the array. |
|
2779 ** |
|
2780 ** In other words, the structure looks something like this: |
|
2781 ** |
|
2782 ** struct Example1 { |
|
2783 ** struct subElem *aEntry; |
|
2784 ** int nEntry; |
|
2785 ** int nAlloc; |
|
2786 ** } |
|
2787 ** |
|
2788 ** The pnEntry parameter points to the equivalent of Example1.nEntry. |
|
2789 ** |
|
2790 ** This routine allocates a new slot in the array, zeros it out, |
|
2791 ** and returns its index. If malloc fails a negative number is returned. |
|
2792 ** |
|
2793 ** szEntry is the sizeof of a single array entry. initSize is the |
|
2794 ** number of array entries allocated on the initial allocation. |
|
2795 */ |
|
2796 int sqlite3ArrayAllocate(void **ppArray, int szEntry, int initSize){ |
|
2797 char *p; |
|
2798 int *an = (int*)&ppArray[1]; |
|
2799 if( an[0]>=an[1] ){ |
|
2800 void *pNew; |
|
2801 int newSize; |
|
2802 newSize = an[1]*2 + initSize; |
|
2803 pNew = sqliteRealloc(*ppArray, newSize*szEntry); |
|
2804 if( pNew==0 ){ |
|
2805 return -1; |
|
2806 } |
|
2807 an[1] = newSize; |
|
2808 *ppArray = pNew; |
|
2809 } |
|
2810 p = *ppArray; |
|
2811 memset(&p[an[0]*szEntry], 0, szEntry); |
|
2812 return an[0]++; |
|
2813 } |
|
2814 |
|
2815 /* |
|
2816 ** Append a new element to the given IdList. Create a new IdList if |
|
2817 ** need be. |
|
2818 ** |
|
2819 ** A new IdList is returned, or NULL if malloc() fails. |
|
2820 */ |
|
2821 IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){ |
|
2822 int i; |
|
2823 if( pList==0 ){ |
|
2824 pList = sqliteMalloc( sizeof(IdList) ); |
|
2825 if( pList==0 ) return 0; |
|
2826 pList->nAlloc = 0; |
|
2827 } |
|
2828 i = sqlite3ArrayAllocate((void**)&pList->a, sizeof(pList->a[0]), 5); |
|
2829 if( i<0 ){ |
|
2830 sqlite3IdListDelete(pList); |
|
2831 return 0; |
|
2832 } |
|
2833 pList->a[i].zName = sqlite3NameFromToken(pToken); |
|
2834 return pList; |
|
2835 } |
|
2836 |
|
2837 /* |
|
2838 ** Delete an IdList. |
|
2839 */ |
|
2840 void sqlite3IdListDelete(IdList *pList){ |
|
2841 int i; |
|
2842 if( pList==0 ) return; |
|
2843 for(i=0; i<pList->nId; i++){ |
|
2844 sqliteFree(pList->a[i].zName); |
|
2845 } |
|
2846 sqliteFree(pList->a); |
|
2847 sqliteFree(pList); |
|
2848 } |
|
2849 |
|
2850 /* |
|
2851 ** Return the index in pList of the identifier named zId. Return -1 |
|
2852 ** if not found. |
|
2853 */ |
|
2854 int sqlite3IdListIndex(IdList *pList, const char *zName){ |
|
2855 int i; |
|
2856 if( pList==0 ) return -1; |
|
2857 for(i=0; i<pList->nId; i++){ |
|
2858 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; |
|
2859 } |
|
2860 return -1; |
|
2861 } |
|
2862 |
|
2863 /* |
|
2864 ** Append a new table name to the given SrcList. Create a new SrcList if |
|
2865 ** need be. A new entry is created in the SrcList even if pToken is NULL. |
|
2866 ** |
|
2867 ** A new SrcList is returned, or NULL if malloc() fails. |
|
2868 ** |
|
2869 ** If pDatabase is not null, it means that the table has an optional |
|
2870 ** database name prefix. Like this: "database.table". The pDatabase |
|
2871 ** points to the table name and the pTable points to the database name. |
|
2872 ** The SrcList.a[].zName field is filled with the table name which might |
|
2873 ** come from pTable (if pDatabase is NULL) or from pDatabase. |
|
2874 ** SrcList.a[].zDatabase is filled with the database name from pTable, |
|
2875 ** or with NULL if no database is specified. |
|
2876 ** |
|
2877 ** In other words, if call like this: |
|
2878 ** |
|
2879 ** sqlite3SrcListAppend(A,B,0); |
|
2880 ** |
|
2881 ** Then B is a table name and the database name is unspecified. If called |
|
2882 ** like this: |
|
2883 ** |
|
2884 ** sqlite3SrcListAppend(A,B,C); |
|
2885 ** |
|
2886 ** Then C is the table name and B is the database name. |
|
2887 */ |
|
2888 SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){ |
|
2889 struct SrcList_item *pItem; |
|
2890 if( pList==0 ){ |
|
2891 pList = sqliteMalloc( sizeof(SrcList) ); |
|
2892 if( pList==0 ) return 0; |
|
2893 pList->nAlloc = 1; |
|
2894 } |
|
2895 if( pList->nSrc>=pList->nAlloc ){ |
|
2896 SrcList *pNew; |
|
2897 pList->nAlloc *= 2; |
|
2898 pNew = sqliteRealloc(pList, |
|
2899 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) ); |
|
2900 if( pNew==0 ){ |
|
2901 sqlite3SrcListDelete(pList); |
|
2902 return 0; |
|
2903 } |
|
2904 pList = pNew; |
|
2905 } |
|
2906 pItem = &pList->a[pList->nSrc]; |
|
2907 memset(pItem, 0, sizeof(pList->a[0])); |
|
2908 if( pDatabase && pDatabase->z==0 ){ |
|
2909 pDatabase = 0; |
|
2910 } |
|
2911 if( pDatabase && pTable ){ |
|
2912 Token *pTemp = pDatabase; |
|
2913 pDatabase = pTable; |
|
2914 pTable = pTemp; |
|
2915 } |
|
2916 pItem->zName = sqlite3NameFromToken(pTable); |
|
2917 pItem->zDatabase = sqlite3NameFromToken(pDatabase); |
|
2918 pItem->iCursor = -1; |
|
2919 pItem->isPopulated = 0; |
|
2920 pList->nSrc++; |
|
2921 return pList; |
|
2922 } |
|
2923 |
|
2924 /* |
|
2925 ** Assign cursors to all tables in a SrcList |
|
2926 */ |
|
2927 void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ |
|
2928 int i; |
|
2929 struct SrcList_item *pItem; |
|
2930 assert(pList || sqlite3MallocFailed() ); |
|
2931 if( pList ){ |
|
2932 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){ |
|
2933 if( pItem->iCursor>=0 ) break; |
|
2934 pItem->iCursor = pParse->nTab++; |
|
2935 if( pItem->pSelect ){ |
|
2936 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); |
|
2937 } |
|
2938 } |
|
2939 } |
|
2940 } |
|
2941 |
|
2942 /* |
|
2943 ** Add an alias to the last identifier on the given identifier list. |
|
2944 */ |
|
2945 void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){ |
|
2946 if( pList && pList->nSrc>0 ){ |
|
2947 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken); |
|
2948 } |
|
2949 } |
|
2950 |
|
2951 /* |
|
2952 ** Delete an entire SrcList including all its substructure. |
|
2953 */ |
|
2954 void sqlite3SrcListDelete(SrcList *pList){ |
|
2955 int i; |
|
2956 struct SrcList_item *pItem; |
|
2957 if( pList==0 ) return; |
|
2958 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){ |
|
2959 sqliteFree(pItem->zDatabase); |
|
2960 sqliteFree(pItem->zName); |
|
2961 sqliteFree(pItem->zAlias); |
|
2962 sqlite3DeleteTable(0, pItem->pTab); |
|
2963 sqlite3SelectDelete(pItem->pSelect); |
|
2964 sqlite3ExprDelete(pItem->pOn); |
|
2965 sqlite3IdListDelete(pItem->pUsing); |
|
2966 } |
|
2967 sqliteFree(pList); |
|
2968 } |
|
2969 |
|
2970 /* |
|
2971 ** Begin a transaction |
|
2972 */ |
|
2973 void sqlite3BeginTransaction(Parse *pParse, int type){ |
|
2974 sqlite3 *db; |
|
2975 Vdbe *v; |
|
2976 int i; |
|
2977 |
|
2978 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
|
2979 if( pParse->nErr || sqlite3MallocFailed() ) return; |
|
2980 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return; |
|
2981 |
|
2982 v = sqlite3GetVdbe(pParse); |
|
2983 if( !v ) return; |
|
2984 if( type!=TK_DEFERRED ){ |
|
2985 for(i=0; i<db->nDb; i++){ |
|
2986 sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); |
|
2987 } |
|
2988 } |
|
2989 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0); |
|
2990 } |
|
2991 |
|
2992 /* |
|
2993 ** Commit a transaction |
|
2994 */ |
|
2995 void sqlite3CommitTransaction(Parse *pParse){ |
|
2996 sqlite3 *db; |
|
2997 Vdbe *v; |
|
2998 |
|
2999 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
|
3000 if( pParse->nErr || sqlite3MallocFailed() ) return; |
|
3001 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return; |
|
3002 |
|
3003 v = sqlite3GetVdbe(pParse); |
|
3004 if( v ){ |
|
3005 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0); |
|
3006 } |
|
3007 } |
|
3008 |
|
3009 /* |
|
3010 ** Rollback a transaction |
|
3011 */ |
|
3012 void sqlite3RollbackTransaction(Parse *pParse){ |
|
3013 sqlite3 *db; |
|
3014 Vdbe *v; |
|
3015 |
|
3016 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return; |
|
3017 if( pParse->nErr || sqlite3MallocFailed() ) return; |
|
3018 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return; |
|
3019 |
|
3020 v = sqlite3GetVdbe(pParse); |
|
3021 if( v ){ |
|
3022 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1); |
|
3023 } |
|
3024 } |
|
3025 |
|
3026 /* |
|
3027 ** Make sure the TEMP database is open and available for use. Return |
|
3028 ** the number of errors. Leave any error messages in the pParse structure. |
|
3029 */ |
|
3030 int sqlite3OpenTempDatabase(Parse *pParse){ |
|
3031 sqlite3 *db = pParse->db; |
|
3032 if( db->aDb[1].pBt==0 && !pParse->explain ){ |
|
3033 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt); |
|
3034 if( rc!=SQLITE_OK ){ |
|
3035 sqlite3ErrorMsg(pParse, "unable to open a temporary database " |
|
3036 "file for storing temporary tables"); |
|
3037 pParse->rc = rc; |
|
3038 return 1; |
|
3039 } |
|
3040 if( db->flags & !db->autoCommit ){ |
|
3041 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1); |
|
3042 if( rc!=SQLITE_OK ){ |
|
3043 sqlite3ErrorMsg(pParse, "unable to get a write lock on " |
|
3044 "the temporary database file"); |
|
3045 pParse->rc = rc; |
|
3046 return 1; |
|
3047 } |
|
3048 } |
|
3049 assert( db->aDb[1].pSchema ); |
|
3050 } |
|
3051 return 0; |
|
3052 } |
|
3053 |
|
3054 /* |
|
3055 ** Generate VDBE code that will verify the schema cookie and start |
|
3056 ** a read-transaction for all named database files. |
|
3057 ** |
|
3058 ** It is important that all schema cookies be verified and all |
|
3059 ** read transactions be started before anything else happens in |
|
3060 ** the VDBE program. But this routine can be called after much other |
|
3061 ** code has been generated. So here is what we do: |
|
3062 ** |
|
3063 ** The first time this routine is called, we code an OP_Goto that |
|
3064 ** will jump to a subroutine at the end of the program. Then we |
|
3065 ** record every database that needs its schema verified in the |
|
3066 ** pParse->cookieMask field. Later, after all other code has been |
|
3067 ** generated, the subroutine that does the cookie verifications and |
|
3068 ** starts the transactions will be coded and the OP_Goto P2 value |
|
3069 ** will be made to point to that subroutine. The generation of the |
|
3070 ** cookie verification subroutine code happens in sqlite3FinishCoding(). |
|
3071 ** |
|
3072 ** If iDb<0 then code the OP_Goto only - don't set flag to verify the |
|
3073 ** schema on any databases. This can be used to position the OP_Goto |
|
3074 ** early in the code, before we know if any database tables will be used. |
|
3075 */ |
|
3076 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ |
|
3077 sqlite3 *db; |
|
3078 Vdbe *v; |
|
3079 int mask; |
|
3080 |
|
3081 v = sqlite3GetVdbe(pParse); |
|
3082 if( v==0 ) return; /* This only happens if there was a prior error */ |
|
3083 db = pParse->db; |
|
3084 if( pParse->cookieGoto==0 ){ |
|
3085 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1; |
|
3086 } |
|
3087 if( iDb>=0 ){ |
|
3088 assert( iDb<db->nDb ); |
|
3089 assert( db->aDb[iDb].pBt!=0 || iDb==1 ); |
|
3090 assert( iDb<MAX_ATTACHED+2 ); |
|
3091 mask = 1<<iDb; |
|
3092 if( (pParse->cookieMask & mask)==0 ){ |
|
3093 pParse->cookieMask |= mask; |
|
3094 pParse->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie; |
|
3095 if( !OMIT_TEMPDB && iDb==1 ){ |
|
3096 sqlite3OpenTempDatabase(pParse); |
|
3097 } |
|
3098 } |
|
3099 } |
|
3100 } |
|
3101 |
|
3102 /* |
|
3103 ** Generate VDBE code that prepares for doing an operation that |
|
3104 ** might change the database. |
|
3105 ** |
|
3106 ** This routine starts a new transaction if we are not already within |
|
3107 ** a transaction. If we are already within a transaction, then a checkpoint |
|
3108 ** is set if the setStatement parameter is true. A checkpoint should |
|
3109 ** be set for operations that might fail (due to a constraint) part of |
|
3110 ** the way through and which will need to undo some writes without having to |
|
3111 ** rollback the whole transaction. For operations where all constraints |
|
3112 ** can be checked before any changes are made to the database, it is never |
|
3113 ** necessary to undo a write and the checkpoint should not be set. |
|
3114 ** |
|
3115 ** Only database iDb and the temp database are made writable by this call. |
|
3116 ** If iDb==0, then the main and temp databases are made writable. If |
|
3117 ** iDb==1 then only the temp database is made writable. If iDb>1 then the |
|
3118 ** specified auxiliary database and the temp database are made writable. |
|
3119 */ |
|
3120 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ |
|
3121 Vdbe *v = sqlite3GetVdbe(pParse); |
|
3122 if( v==0 ) return; |
|
3123 sqlite3CodeVerifySchema(pParse, iDb); |
|
3124 pParse->writeMask |= 1<<iDb; |
|
3125 if( setStatement && pParse->nested==0 ){ |
|
3126 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0); |
|
3127 } |
|
3128 if( (OMIT_TEMPDB || iDb!=1) && pParse->db->aDb[1].pBt!=0 ){ |
|
3129 sqlite3BeginWriteOperation(pParse, setStatement, 1); |
|
3130 } |
|
3131 } |
|
3132 |
|
3133 /* |
|
3134 ** Check to see if pIndex uses the collating sequence pColl. Return |
|
3135 ** true if it does and false if it does not. |
|
3136 */ |
|
3137 #ifndef SQLITE_OMIT_REINDEX |
|
3138 static int collationMatch(const char *zColl, Index *pIndex){ |
|
3139 int i; |
|
3140 for(i=0; i<pIndex->nColumn; i++){ |
|
3141 const char *z = pIndex->azColl[i]; |
|
3142 if( z==zColl || (z && zColl && 0==sqlite3StrICmp(z, zColl)) ){ |
|
3143 return 1; |
|
3144 } |
|
3145 } |
|
3146 return 0; |
|
3147 } |
|
3148 #endif |
|
3149 |
|
3150 /* |
|
3151 ** Recompute all indices of pTab that use the collating sequence pColl. |
|
3152 ** If pColl==0 then recompute all indices of pTab. |
|
3153 */ |
|
3154 #ifndef SQLITE_OMIT_REINDEX |
|
3155 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ |
|
3156 Index *pIndex; /* An index associated with pTab */ |
|
3157 |
|
3158 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ |
|
3159 if( zColl==0 || collationMatch(zColl, pIndex) ){ |
|
3160 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
|
3161 sqlite3BeginWriteOperation(pParse, 0, iDb); |
|
3162 sqlite3RefillIndex(pParse, pIndex, -1); |
|
3163 } |
|
3164 } |
|
3165 } |
|
3166 #endif |
|
3167 |
|
3168 /* |
|
3169 ** Recompute all indices of all tables in all databases where the |
|
3170 ** indices use the collating sequence pColl. If pColl==0 then recompute |
|
3171 ** all indices everywhere. |
|
3172 */ |
|
3173 #ifndef SQLITE_OMIT_REINDEX |
|
3174 static void reindexDatabases(Parse *pParse, char const *zColl){ |
|
3175 Db *pDb; /* A single database */ |
|
3176 int iDb; /* The database index number */ |
|
3177 sqlite3 *db = pParse->db; /* The database connection */ |
|
3178 HashElem *k; /* For looping over tables in pDb */ |
|
3179 Table *pTab; /* A table in the database */ |
|
3180 |
|
3181 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){ |
|
3182 assert( pDb!=0 ); |
|
3183 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ |
|
3184 pTab = (Table*)sqliteHashData(k); |
|
3185 reindexTable(pParse, pTab, zColl); |
|
3186 } |
|
3187 } |
|
3188 } |
|
3189 #endif |
|
3190 |
|
3191 /* |
|
3192 ** Generate code for the REINDEX command. |
|
3193 ** |
|
3194 ** REINDEX -- 1 |
|
3195 ** REINDEX <collation> -- 2 |
|
3196 ** REINDEX ?<database>.?<tablename> -- 3 |
|
3197 ** REINDEX ?<database>.?<indexname> -- 4 |
|
3198 ** |
|
3199 ** Form 1 causes all indices in all attached databases to be rebuilt. |
|
3200 ** Form 2 rebuilds all indices in all databases that use the named |
|
3201 ** collating function. Forms 3 and 4 rebuild the named index or all |
|
3202 ** indices associated with the named table. |
|
3203 */ |
|
3204 #ifndef SQLITE_OMIT_REINDEX |
|
3205 void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ |
|
3206 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ |
|
3207 char *z; /* Name of a table or index */ |
|
3208 const char *zDb; /* Name of the database */ |
|
3209 Table *pTab; /* A table in the database */ |
|
3210 Index *pIndex; /* An index associated with pTab */ |
|
3211 int iDb; /* The database index number */ |
|
3212 sqlite3 *db = pParse->db; /* The database connection */ |
|
3213 Token *pObjName; /* Name of the table or index to be reindexed */ |
|
3214 |
|
3215 /* Read the database schema. If an error occurs, leave an error message |
|
3216 ** and code in pParse and return NULL. */ |
|
3217 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
|
3218 return; |
|
3219 } |
|
3220 |
|
3221 if( pName1==0 || pName1->z==0 ){ |
|
3222 reindexDatabases(pParse, 0); |
|
3223 return; |
|
3224 }else if( pName2==0 || pName2->z==0 ){ |
|
3225 assert( pName1->z ); |
|
3226 pColl = sqlite3FindCollSeq(db, ENC(db), (char*)pName1->z, pName1->n, 0); |
|
3227 if( pColl ){ |
|
3228 char *zColl = sqliteStrNDup((const char *)pName1->z, pName1->n); |
|
3229 if( zColl ){ |
|
3230 reindexDatabases(pParse, zColl); |
|
3231 sqliteFree(zColl); |
|
3232 } |
|
3233 return; |
|
3234 } |
|
3235 } |
|
3236 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); |
|
3237 if( iDb<0 ) return; |
|
3238 z = sqlite3NameFromToken(pObjName); |
|
3239 zDb = db->aDb[iDb].zName; |
|
3240 pTab = sqlite3FindTable(db, z, zDb); |
|
3241 if( pTab ){ |
|
3242 reindexTable(pParse, pTab, 0); |
|
3243 sqliteFree(z); |
|
3244 return; |
|
3245 } |
|
3246 pIndex = sqlite3FindIndex(db, z, zDb); |
|
3247 sqliteFree(z); |
|
3248 if( pIndex ){ |
|
3249 sqlite3BeginWriteOperation(pParse, 0, iDb); |
|
3250 sqlite3RefillIndex(pParse, pIndex, -1); |
|
3251 return; |
|
3252 } |
|
3253 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); |
|
3254 } |
|
3255 #endif |
|
3256 |
|
3257 /* |
|
3258 ** Return a dynamicly allocated KeyInfo structure that can be used |
|
3259 ** with OP_OpenRead or OP_OpenWrite to access database index pIdx. |
|
3260 ** |
|
3261 ** If successful, a pointer to the new structure is returned. In this case |
|
3262 ** the caller is responsible for calling sqliteFree() on the returned |
|
3263 ** pointer. If an error occurs (out of memory or missing collation |
|
3264 ** sequence), NULL is returned and the state of pParse updated to reflect |
|
3265 ** the error. |
|
3266 */ |
|
3267 KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){ |
|
3268 int i; |
|
3269 int nCol = pIdx->nColumn; |
|
3270 int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol; |
|
3271 KeyInfo *pKey = (KeyInfo *)sqliteMalloc(nBytes); |
|
3272 |
|
3273 if( pKey ){ |
|
3274 pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]); |
|
3275 assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) ); |
|
3276 for(i=0; i<nCol; i++){ |
|
3277 char *zColl = pIdx->azColl[i]; |
|
3278 assert( zColl ); |
|
3279 pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl, -1); |
|
3280 pKey->aSortOrder[i] = pIdx->aSortOrder[i]; |
|
3281 } |
|
3282 pKey->nField = nCol; |
|
3283 } |
|
3284 |
|
3285 if( pParse->nErr ){ |
|
3286 sqliteFree(pKey); |
|
3287 pKey = 0; |
|
3288 } |
|
3289 return pKey; |
|
3290 } |