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