xref: /sqlite-3.40.0/src/build.c (revision 065f3bf4)
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 #include "sqliteInt.h"
26 
27 #ifndef SQLITE_OMIT_SHARED_CACHE
28 /*
29 ** The TableLock structure is only used by the sqlite3TableLock() and
30 ** codeTableLocks() functions.
31 */
32 struct TableLock {
33   int iDb;               /* The database containing the table to be locked */
34   int iTab;              /* The root page of the table to be locked */
35   u8 isWriteLock;        /* True for write lock.  False for a read lock */
36   const char *zLockName; /* Name of the table */
37 };
38 
39 /*
40 ** Record the fact that we want to lock a table at run-time.
41 **
42 ** The table to be locked has root page iTab and is found in database iDb.
43 ** A read or a write lock can be taken depending on isWritelock.
44 **
45 ** This routine just records the fact that the lock is desired.  The
46 ** code to make the lock occur is generated by a later call to
47 ** codeTableLocks() which occurs during sqlite3FinishCoding().
48 */
49 void sqlite3TableLock(
50   Parse *pParse,     /* Parsing context */
51   int iDb,           /* Index of the database containing the table to lock */
52   int iTab,          /* Root page number of the table to be locked */
53   u8 isWriteLock,    /* True for a write lock */
54   const char *zName  /* Name of the table to be locked */
55 ){
56   Parse *pToplevel = sqlite3ParseToplevel(pParse);
57   int i;
58   int nBytes;
59   TableLock *p;
60   assert( iDb>=0 );
61 
62   if( iDb==1 ) return;
63   if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
64   for(i=0; i<pToplevel->nTableLock; i++){
65     p = &pToplevel->aTableLock[i];
66     if( p->iDb==iDb && p->iTab==iTab ){
67       p->isWriteLock = (p->isWriteLock || isWriteLock);
68       return;
69     }
70   }
71 
72   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
73   pToplevel->aTableLock =
74       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
75   if( pToplevel->aTableLock ){
76     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
77     p->iDb = iDb;
78     p->iTab = iTab;
79     p->isWriteLock = isWriteLock;
80     p->zLockName = zName;
81   }else{
82     pToplevel->nTableLock = 0;
83     sqlite3OomFault(pToplevel->db);
84   }
85 }
86 
87 /*
88 ** Code an OP_TableLock instruction for each table locked by the
89 ** statement (configured by calls to sqlite3TableLock()).
90 */
91 static void codeTableLocks(Parse *pParse){
92   int i;
93   Vdbe *pVdbe;
94 
95   pVdbe = sqlite3GetVdbe(pParse);
96   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
97 
98   for(i=0; i<pParse->nTableLock; i++){
99     TableLock *p = &pParse->aTableLock[i];
100     int p1 = p->iDb;
101     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
102                       p->zLockName, P4_STATIC);
103   }
104 }
105 #else
106   #define codeTableLocks(x)
107 #endif
108 
109 /*
110 ** Return TRUE if the given yDbMask object is empty - if it contains no
111 ** 1 bits.  This routine is used by the DbMaskAllZero() and DbMaskNotZero()
112 ** macros when SQLITE_MAX_ATTACHED is greater than 30.
113 */
114 #if SQLITE_MAX_ATTACHED>30
115 int sqlite3DbMaskAllZero(yDbMask m){
116   int i;
117   for(i=0; i<sizeof(yDbMask); i++) if( m[i] ) return 0;
118   return 1;
119 }
120 #endif
121 
122 /*
123 ** This routine is called after a single SQL statement has been
124 ** parsed and a VDBE program to execute that statement has been
125 ** prepared.  This routine puts the finishing touches on the
126 ** VDBE program and resets the pParse structure for the next
127 ** parse.
128 **
129 ** Note that if an error occurred, it might be the case that
130 ** no VDBE code was generated.
131 */
132 void sqlite3FinishCoding(Parse *pParse){
133   sqlite3 *db;
134   Vdbe *v;
135 
136   assert( pParse->pToplevel==0 );
137   db = pParse->db;
138   if( pParse->nested ) return;
139   if( db->mallocFailed || pParse->nErr ){
140     if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
141     return;
142   }
143 
144   /* Begin by generating some termination code at the end of the
145   ** vdbe program
146   */
147   v = sqlite3GetVdbe(pParse);
148   assert( !pParse->isMultiWrite
149        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
150   if( v ){
151     sqlite3VdbeAddOp0(v, OP_Halt);
152 
153 #if SQLITE_USER_AUTHENTICATION
154     if( pParse->nTableLock>0 && db->init.busy==0 ){
155       sqlite3UserAuthInit(db);
156       if( db->auth.authLevel<UAUTH_User ){
157         sqlite3ErrorMsg(pParse, "user not authenticated");
158         pParse->rc = SQLITE_AUTH_USER;
159         return;
160       }
161     }
162 #endif
163 
164     /* The cookie mask contains one bit for each database file open.
165     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
166     ** set for each database that is used.  Generate code to start a
167     ** transaction on each used database and to verify the schema cookie
168     ** on each used database.
169     */
170     if( db->mallocFailed==0
171      && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
172     ){
173       int iDb, i;
174       assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
175       sqlite3VdbeJumpHere(v, 0);
176       for(iDb=0; iDb<db->nDb; iDb++){
177         Schema *pSchema;
178         if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
179         sqlite3VdbeUsesBtree(v, iDb);
180         pSchema = db->aDb[iDb].pSchema;
181         sqlite3VdbeAddOp4Int(v,
182           OP_Transaction,                    /* Opcode */
183           iDb,                               /* P1 */
184           DbMaskTest(pParse->writeMask,iDb), /* P2 */
185           pSchema->schema_cookie,            /* P3 */
186           pSchema->iGeneration               /* P4 */
187         );
188         if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
189         VdbeComment((v,
190               "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
191       }
192 #ifndef SQLITE_OMIT_VIRTUALTABLE
193       for(i=0; i<pParse->nVtabLock; i++){
194         char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
195         sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
196       }
197       pParse->nVtabLock = 0;
198 #endif
199 
200       /* Once all the cookies have been verified and transactions opened,
201       ** obtain the required table-locks. This is a no-op unless the
202       ** shared-cache feature is enabled.
203       */
204       codeTableLocks(pParse);
205 
206       /* Initialize any AUTOINCREMENT data structures required.
207       */
208       sqlite3AutoincrementBegin(pParse);
209 
210       /* Code constant expressions that where factored out of inner loops */
211       if( pParse->pConstExpr ){
212         ExprList *pEL = pParse->pConstExpr;
213         pParse->okConstFactor = 0;
214         for(i=0; i<pEL->nExpr; i++){
215           sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
216         }
217       }
218 
219       /* Finally, jump back to the beginning of the executable code. */
220       sqlite3VdbeGoto(v, 1);
221     }
222   }
223 
224 
225   /* Get the VDBE program ready for execution
226   */
227   if( v && pParse->nErr==0 && !db->mallocFailed ){
228     /* A minimum of one cursor is required if autoincrement is used
229     *  See ticket [a696379c1f08866] */
230     assert( pParse->pAinc==0 || pParse->nTab>0 );
231     sqlite3VdbeMakeReady(v, pParse);
232     pParse->rc = SQLITE_DONE;
233   }else{
234     pParse->rc = SQLITE_ERROR;
235   }
236 }
237 
238 /*
239 ** Run the parser and code generator recursively in order to generate
240 ** code for the SQL statement given onto the end of the pParse context
241 ** currently under construction.  When the parser is run recursively
242 ** this way, the final OP_Halt is not appended and other initialization
243 ** and finalization steps are omitted because those are handling by the
244 ** outermost parser.
245 **
246 ** Not everything is nestable.  This facility is designed to permit
247 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
248 ** care if you decide to try to use this routine for some other purposes.
249 */
250 void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
251   va_list ap;
252   char *zSql;
253   char *zErrMsg = 0;
254   sqlite3 *db = pParse->db;
255   char saveBuf[PARSE_TAIL_SZ];
256 
257   if( pParse->nErr ) return;
258   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
259   va_start(ap, zFormat);
260   zSql = sqlite3VMPrintf(db, zFormat, ap);
261   va_end(ap);
262   if( zSql==0 ){
263     /* This can result either from an OOM or because the formatted string
264     ** exceeds SQLITE_LIMIT_LENGTH.  In the latter case, we need to set
265     ** an error */
266     if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
267     return;
268   }
269   pParse->nested++;
270   memcpy(saveBuf, PARSE_TAIL(pParse), PARSE_TAIL_SZ);
271   memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
272   sqlite3RunParser(pParse, zSql, &zErrMsg);
273   sqlite3DbFree(db, zErrMsg);
274   sqlite3DbFree(db, zSql);
275   memcpy(PARSE_TAIL(pParse), saveBuf, PARSE_TAIL_SZ);
276   pParse->nested--;
277 }
278 
279 #if SQLITE_USER_AUTHENTICATION
280 /*
281 ** Return TRUE if zTable is the name of the system table that stores the
282 ** list of users and their access credentials.
283 */
284 int sqlite3UserAuthTable(const char *zTable){
285   return sqlite3_stricmp(zTable, "sqlite_user")==0;
286 }
287 #endif
288 
289 /*
290 ** Locate the in-memory structure that describes a particular database
291 ** table given the name of that table and (optionally) the name of the
292 ** database containing the table.  Return NULL if not found.
293 **
294 ** If zDatabase is 0, all databases are searched for the table and the
295 ** first matching table is returned.  (No checking for duplicate table
296 ** names is done.)  The search order is TEMP first, then MAIN, then any
297 ** auxiliary databases added using the ATTACH command.
298 **
299 ** See also sqlite3LocateTable().
300 */
301 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
302   Table *p = 0;
303   int i;
304 
305   /* All mutexes are required for schema access.  Make sure we hold them. */
306   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
307 #if SQLITE_USER_AUTHENTICATION
308   /* Only the admin user is allowed to know that the sqlite_user table
309   ** exists */
310   if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
311     return 0;
312   }
313 #endif
314   while(1){
315     for(i=OMIT_TEMPDB; i<db->nDb; i++){
316       int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
317       if( zDatabase==0 || sqlite3StrICmp(zDatabase, db->aDb[j].zDbSName)==0 ){
318         assert( sqlite3SchemaMutexHeld(db, j, 0) );
319         p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName);
320         if( p ) return p;
321       }
322     }
323     /* Not found.  If the name we were looking for was temp.sqlite_master
324     ** then change the name to sqlite_temp_master and try again. */
325     if( sqlite3StrICmp(zName, MASTER_NAME)!=0 ) break;
326     if( sqlite3_stricmp(zDatabase, db->aDb[1].zDbSName)!=0 ) break;
327     zName = TEMP_MASTER_NAME;
328   }
329   return 0;
330 }
331 
332 /*
333 ** Locate the in-memory structure that describes a particular database
334 ** table given the name of that table and (optionally) the name of the
335 ** database containing the table.  Return NULL if not found.  Also leave an
336 ** error message in pParse->zErrMsg.
337 **
338 ** The difference between this routine and sqlite3FindTable() is that this
339 ** routine leaves an error message in pParse->zErrMsg where
340 ** sqlite3FindTable() does not.
341 */
342 Table *sqlite3LocateTable(
343   Parse *pParse,         /* context in which to report errors */
344   u32 flags,             /* LOCATE_VIEW or LOCATE_NOERR */
345   const char *zName,     /* Name of the table we are looking for */
346   const char *zDbase     /* Name of the database.  Might be NULL */
347 ){
348   Table *p;
349   sqlite3 *db = pParse->db;
350 
351   /* Read the database schema. If an error occurs, leave an error message
352   ** and code in pParse and return NULL. */
353   if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
354    && SQLITE_OK!=sqlite3ReadSchema(pParse)
355   ){
356     return 0;
357   }
358 
359   p = sqlite3FindTable(db, zName, zDbase);
360   if( p==0 ){
361 #ifndef SQLITE_OMIT_VIRTUALTABLE
362     /* If zName is the not the name of a table in the schema created using
363     ** CREATE, then check to see if it is the name of an virtual table that
364     ** can be an eponymous virtual table. */
365     if( pParse->disableVtab==0 ){
366       Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
367       if( pMod==0 && sqlite3_strnicmp(zName, "pragma_", 7)==0 ){
368         pMod = sqlite3PragmaVtabRegister(db, zName);
369       }
370       if( pMod && sqlite3VtabEponymousTableInit(pParse, pMod) ){
371         return pMod->pEpoTab;
372       }
373     }
374 #endif
375     if( flags & LOCATE_NOERR ) return 0;
376     pParse->checkSchema = 1;
377   }else if( IsVirtual(p) && pParse->disableVtab ){
378     p = 0;
379   }
380 
381   if( p==0 ){
382     const char *zMsg = flags & LOCATE_VIEW ? "no such view" : "no such table";
383     if( zDbase ){
384       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
385     }else{
386       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
387     }
388   }
389 
390   return p;
391 }
392 
393 /*
394 ** Locate the table identified by *p.
395 **
396 ** This is a wrapper around sqlite3LocateTable(). The difference between
397 ** sqlite3LocateTable() and this function is that this function restricts
398 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
399 ** non-NULL if it is part of a view or trigger program definition. See
400 ** sqlite3FixSrcList() for details.
401 */
402 Table *sqlite3LocateTableItem(
403   Parse *pParse,
404   u32 flags,
405   struct SrcList_item *p
406 ){
407   const char *zDb;
408   assert( p->pSchema==0 || p->zDatabase==0 );
409   if( p->pSchema ){
410     int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
411     zDb = pParse->db->aDb[iDb].zDbSName;
412   }else{
413     zDb = p->zDatabase;
414   }
415   return sqlite3LocateTable(pParse, flags, p->zName, zDb);
416 }
417 
418 /*
419 ** Locate the in-memory structure that describes
420 ** a particular index given the name of that index
421 ** and the name of the database that contains the index.
422 ** Return NULL if not found.
423 **
424 ** If zDatabase is 0, all databases are searched for the
425 ** table and the first matching index is returned.  (No checking
426 ** for duplicate index names is done.)  The search order is
427 ** TEMP first, then MAIN, then any auxiliary databases added
428 ** using the ATTACH command.
429 */
430 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
431   Index *p = 0;
432   int i;
433   /* All mutexes are required for schema access.  Make sure we hold them. */
434   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
435   for(i=OMIT_TEMPDB; i<db->nDb; i++){
436     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
437     Schema *pSchema = db->aDb[j].pSchema;
438     assert( pSchema );
439     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zDbSName) ) continue;
440     assert( sqlite3SchemaMutexHeld(db, j, 0) );
441     p = sqlite3HashFind(&pSchema->idxHash, zName);
442     if( p ) break;
443   }
444   return p;
445 }
446 
447 /*
448 ** Reclaim the memory used by an index
449 */
450 void sqlite3FreeIndex(sqlite3 *db, Index *p){
451 #ifndef SQLITE_OMIT_ANALYZE
452   sqlite3DeleteIndexSamples(db, p);
453 #endif
454   sqlite3ExprDelete(db, p->pPartIdxWhere);
455   sqlite3ExprListDelete(db, p->aColExpr);
456   sqlite3DbFree(db, p->zColAff);
457   if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
458 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
459   sqlite3_free(p->aiRowEst);
460 #endif
461   sqlite3DbFree(db, p);
462 }
463 
464 /*
465 ** For the index called zIdxName which is found in the database iDb,
466 ** unlike that index from its Table then remove the index from
467 ** the index hash table and free all memory structures associated
468 ** with the index.
469 */
470 void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
471   Index *pIndex;
472   Hash *pHash;
473 
474   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
475   pHash = &db->aDb[iDb].pSchema->idxHash;
476   pIndex = sqlite3HashInsert(pHash, zIdxName, 0);
477   if( ALWAYS(pIndex) ){
478     if( pIndex->pTable->pIndex==pIndex ){
479       pIndex->pTable->pIndex = pIndex->pNext;
480     }else{
481       Index *p;
482       /* Justification of ALWAYS();  The index must be on the list of
483       ** indices. */
484       p = pIndex->pTable->pIndex;
485       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
486       if( ALWAYS(p && p->pNext==pIndex) ){
487         p->pNext = pIndex->pNext;
488       }
489     }
490     sqlite3FreeIndex(db, pIndex);
491   }
492   db->mDbFlags |= DBFLAG_SchemaChange;
493 }
494 
495 /*
496 ** Look through the list of open database files in db->aDb[] and if
497 ** any have been closed, remove them from the list.  Reallocate the
498 ** db->aDb[] structure to a smaller size, if possible.
499 **
500 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
501 ** are never candidates for being collapsed.
502 */
503 void sqlite3CollapseDatabaseArray(sqlite3 *db){
504   int i, j;
505   for(i=j=2; i<db->nDb; i++){
506     struct Db *pDb = &db->aDb[i];
507     if( pDb->pBt==0 ){
508       sqlite3DbFree(db, pDb->zDbSName);
509       pDb->zDbSName = 0;
510       continue;
511     }
512     if( j<i ){
513       db->aDb[j] = db->aDb[i];
514     }
515     j++;
516   }
517   db->nDb = j;
518   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
519     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
520     sqlite3DbFree(db, db->aDb);
521     db->aDb = db->aDbStatic;
522   }
523 }
524 
525 /*
526 ** Reset the schema for the database at index iDb.  Also reset the
527 ** TEMP schema.  The reset is deferred if db->nSchemaLock is not zero.
528 ** Deferred resets may be run by calling with iDb<0.
529 */
530 void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
531   int i;
532   assert( iDb<db->nDb );
533 
534   if( iDb>=0 ){
535     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
536     DbSetProperty(db, iDb, DB_ResetWanted);
537     DbSetProperty(db, 1, DB_ResetWanted);
538     db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
539   }
540 
541   if( db->nSchemaLock==0 ){
542     for(i=0; i<db->nDb; i++){
543       if( DbHasProperty(db, i, DB_ResetWanted) ){
544         sqlite3SchemaClear(db->aDb[i].pSchema);
545       }
546     }
547   }
548 }
549 
550 /*
551 ** Erase all schema information from all attached databases (including
552 ** "main" and "temp") for a single database connection.
553 */
554 void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
555   int i;
556   sqlite3BtreeEnterAll(db);
557   for(i=0; i<db->nDb; i++){
558     Db *pDb = &db->aDb[i];
559     if( pDb->pSchema ){
560       if( db->nSchemaLock==0 ){
561         sqlite3SchemaClear(pDb->pSchema);
562       }else{
563         DbSetProperty(db, i, DB_ResetWanted);
564       }
565     }
566   }
567   db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
568   sqlite3VtabUnlockList(db);
569   sqlite3BtreeLeaveAll(db);
570   if( db->nSchemaLock==0 ){
571     sqlite3CollapseDatabaseArray(db);
572   }
573 }
574 
575 /*
576 ** This routine is called when a commit occurs.
577 */
578 void sqlite3CommitInternalChanges(sqlite3 *db){
579   db->mDbFlags &= ~DBFLAG_SchemaChange;
580 }
581 
582 /*
583 ** Delete memory allocated for the column names of a table or view (the
584 ** Table.aCol[] array).
585 */
586 void sqlite3DeleteColumnNames(sqlite3 *db, Table *pTable){
587   int i;
588   Column *pCol;
589   assert( pTable!=0 );
590   if( (pCol = pTable->aCol)!=0 ){
591     for(i=0; i<pTable->nCol; i++, pCol++){
592       sqlite3DbFree(db, pCol->zName);
593       sqlite3ExprDelete(db, pCol->pDflt);
594       sqlite3DbFree(db, pCol->zColl);
595     }
596     sqlite3DbFree(db, pTable->aCol);
597   }
598 }
599 
600 /*
601 ** Remove the memory data structures associated with the given
602 ** Table.  No changes are made to disk by this routine.
603 **
604 ** This routine just deletes the data structure.  It does not unlink
605 ** the table data structure from the hash table.  But it does destroy
606 ** memory structures of the indices and foreign keys associated with
607 ** the table.
608 **
609 ** The db parameter is optional.  It is needed if the Table object
610 ** contains lookaside memory.  (Table objects in the schema do not use
611 ** lookaside memory, but some ephemeral Table objects do.)  Or the
612 ** db parameter can be used with db->pnBytesFreed to measure the memory
613 ** used by the Table object.
614 */
615 static void SQLITE_NOINLINE deleteTable(sqlite3 *db, Table *pTable){
616   Index *pIndex, *pNext;
617 
618 #ifdef SQLITE_DEBUG
619   /* Record the number of outstanding lookaside allocations in schema Tables
620   ** prior to doing any free() operations.  Since schema Tables do not use
621   ** lookaside, this number should not change. */
622   int nLookaside = 0;
623   if( db && (pTable->tabFlags & TF_Ephemeral)==0 ){
624     nLookaside = sqlite3LookasideUsed(db, 0);
625   }
626 #endif
627 
628   /* Delete all indices associated with this table. */
629   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
630     pNext = pIndex->pNext;
631     assert( pIndex->pSchema==pTable->pSchema
632          || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
633     if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
634       char *zName = pIndex->zName;
635       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
636          &pIndex->pSchema->idxHash, zName, 0
637       );
638       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
639       assert( pOld==pIndex || pOld==0 );
640     }
641     sqlite3FreeIndex(db, pIndex);
642   }
643 
644   /* Delete any foreign keys attached to this table. */
645   sqlite3FkDelete(db, pTable);
646 
647   /* Delete the Table structure itself.
648   */
649   sqlite3DeleteColumnNames(db, pTable);
650   sqlite3DbFree(db, pTable->zName);
651   sqlite3DbFree(db, pTable->zColAff);
652   sqlite3SelectDelete(db, pTable->pSelect);
653   sqlite3ExprListDelete(db, pTable->pCheck);
654 #ifndef SQLITE_OMIT_VIRTUALTABLE
655   sqlite3VtabClear(db, pTable);
656 #endif
657   sqlite3DbFree(db, pTable);
658 
659   /* Verify that no lookaside memory was used by schema tables */
660   assert( nLookaside==0 || nLookaside==sqlite3LookasideUsed(db,0) );
661 }
662 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
663   /* Do not delete the table until the reference count reaches zero. */
664   if( !pTable ) return;
665   if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
666   deleteTable(db, pTable);
667 }
668 
669 
670 /*
671 ** Unlink the given table from the hash tables and the delete the
672 ** table structure with all its indices and foreign keys.
673 */
674 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
675   Table *p;
676   Db *pDb;
677 
678   assert( db!=0 );
679   assert( iDb>=0 && iDb<db->nDb );
680   assert( zTabName );
681   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
682   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
683   pDb = &db->aDb[iDb];
684   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
685   sqlite3DeleteTable(db, p);
686   db->mDbFlags |= DBFLAG_SchemaChange;
687 }
688 
689 /*
690 ** Given a token, return a string that consists of the text of that
691 ** token.  Space to hold the returned string
692 ** is obtained from sqliteMalloc() and must be freed by the calling
693 ** function.
694 **
695 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
696 ** surround the body of the token are removed.
697 **
698 ** Tokens are often just pointers into the original SQL text and so
699 ** are not \000 terminated and are not persistent.  The returned string
700 ** is \000 terminated and is persistent.
701 */
702 char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
703   char *zName;
704   if( pName ){
705     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
706     sqlite3Dequote(zName);
707   }else{
708     zName = 0;
709   }
710   return zName;
711 }
712 
713 /*
714 ** Open the sqlite_master table stored in database number iDb for
715 ** writing. The table is opened using cursor 0.
716 */
717 void sqlite3OpenMasterTable(Parse *p, int iDb){
718   Vdbe *v = sqlite3GetVdbe(p);
719   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, MASTER_NAME);
720   sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
721   if( p->nTab==0 ){
722     p->nTab = 1;
723   }
724 }
725 
726 /*
727 ** Parameter zName points to a nul-terminated buffer containing the name
728 ** of a database ("main", "temp" or the name of an attached db). This
729 ** function returns the index of the named database in db->aDb[], or
730 ** -1 if the named db cannot be found.
731 */
732 int sqlite3FindDbName(sqlite3 *db, const char *zName){
733   int i = -1;         /* Database number */
734   if( zName ){
735     Db *pDb;
736     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
737       if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
738       /* "main" is always an acceptable alias for the primary database
739       ** even if it has been renamed using SQLITE_DBCONFIG_MAINDBNAME. */
740       if( i==0 && 0==sqlite3_stricmp("main", zName) ) break;
741     }
742   }
743   return i;
744 }
745 
746 /*
747 ** The token *pName contains the name of a database (either "main" or
748 ** "temp" or the name of an attached db). This routine returns the
749 ** index of the named database in db->aDb[], or -1 if the named db
750 ** does not exist.
751 */
752 int sqlite3FindDb(sqlite3 *db, Token *pName){
753   int i;                               /* Database number */
754   char *zName;                         /* Name we are searching for */
755   zName = sqlite3NameFromToken(db, pName);
756   i = sqlite3FindDbName(db, zName);
757   sqlite3DbFree(db, zName);
758   return i;
759 }
760 
761 /* The table or view or trigger name is passed to this routine via tokens
762 ** pName1 and pName2. If the table name was fully qualified, for example:
763 **
764 ** CREATE TABLE xxx.yyy (...);
765 **
766 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
767 ** the table name is not fully qualified, i.e.:
768 **
769 ** CREATE TABLE yyy(...);
770 **
771 ** Then pName1 is set to "yyy" and pName2 is "".
772 **
773 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
774 ** pName2) that stores the unqualified table name.  The index of the
775 ** database "xxx" is returned.
776 */
777 int sqlite3TwoPartName(
778   Parse *pParse,      /* Parsing and code generating context */
779   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
780   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
781   Token **pUnqual     /* Write the unqualified object name here */
782 ){
783   int iDb;                    /* Database holding the object */
784   sqlite3 *db = pParse->db;
785 
786   assert( pName2!=0 );
787   if( pName2->n>0 ){
788     if( db->init.busy ) {
789       sqlite3ErrorMsg(pParse, "corrupt database");
790       return -1;
791     }
792     *pUnqual = pName2;
793     iDb = sqlite3FindDb(db, pName1);
794     if( iDb<0 ){
795       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
796       return -1;
797     }
798   }else{
799     assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
800              || (db->mDbFlags & DBFLAG_Vacuum)!=0);
801     iDb = db->init.iDb;
802     *pUnqual = pName1;
803   }
804   return iDb;
805 }
806 
807 /*
808 ** True if PRAGMA writable_schema is ON
809 */
810 int sqlite3WritableSchema(sqlite3 *db){
811   testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
812   testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
813                SQLITE_WriteSchema );
814   testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
815                SQLITE_Defensive );
816   testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
817                (SQLITE_WriteSchema|SQLITE_Defensive) );
818   return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
819 }
820 
821 /*
822 ** This routine is used to check if the UTF-8 string zName is a legal
823 ** unqualified name for a new schema object (table, index, view or
824 ** trigger). All names are legal except those that begin with the string
825 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
826 ** is reserved for internal use.
827 */
828 int sqlite3CheckObjectName(Parse *pParse, const char *zName){
829   if( !pParse->db->init.busy && pParse->nested==0
830           && sqlite3WritableSchema(pParse->db)==0
831           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
832     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
833     return SQLITE_ERROR;
834   }
835   return SQLITE_OK;
836 }
837 
838 /*
839 ** Return the PRIMARY KEY index of a table
840 */
841 Index *sqlite3PrimaryKeyIndex(Table *pTab){
842   Index *p;
843   for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
844   return p;
845 }
846 
847 /*
848 ** Return the column of index pIdx that corresponds to table
849 ** column iCol.  Return -1 if not found.
850 */
851 i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
852   int i;
853   for(i=0; i<pIdx->nColumn; i++){
854     if( iCol==pIdx->aiColumn[i] ) return i;
855   }
856   return -1;
857 }
858 
859 /*
860 ** Begin constructing a new table representation in memory.  This is
861 ** the first of several action routines that get called in response
862 ** to a CREATE TABLE statement.  In particular, this routine is called
863 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
864 ** flag is true if the table should be stored in the auxiliary database
865 ** file instead of in the main database file.  This is normally the case
866 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
867 ** CREATE and TABLE.
868 **
869 ** The new table record is initialized and put in pParse->pNewTable.
870 ** As more of the CREATE TABLE statement is parsed, additional action
871 ** routines will be called to add more information to this record.
872 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
873 ** is called to complete the construction of the new table record.
874 */
875 void sqlite3StartTable(
876   Parse *pParse,   /* Parser context */
877   Token *pName1,   /* First part of the name of the table or view */
878   Token *pName2,   /* Second part of the name of the table or view */
879   int isTemp,      /* True if this is a TEMP table */
880   int isView,      /* True if this is a VIEW */
881   int isVirtual,   /* True if this is a VIRTUAL table */
882   int noErr        /* Do nothing if table already exists */
883 ){
884   Table *pTable;
885   char *zName = 0; /* The name of the new table */
886   sqlite3 *db = pParse->db;
887   Vdbe *v;
888   int iDb;         /* Database number to create the table in */
889   Token *pName;    /* Unqualified name of the table to create */
890 
891   if( db->init.busy && db->init.newTnum==1 ){
892     /* Special case:  Parsing the sqlite_master or sqlite_temp_master schema */
893     iDb = db->init.iDb;
894     zName = sqlite3DbStrDup(db, SCHEMA_TABLE(iDb));
895     pName = pName1;
896   }else{
897     /* The common case */
898     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
899     if( iDb<0 ) return;
900     if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
901       /* If creating a temp table, the name may not be qualified. Unless
902       ** the database name is "temp" anyway.  */
903       sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
904       return;
905     }
906     if( !OMIT_TEMPDB && isTemp ) iDb = 1;
907     zName = sqlite3NameFromToken(db, pName);
908     if( IN_RENAME_OBJECT ){
909       sqlite3RenameTokenMap(pParse, (void*)zName, pName);
910     }
911   }
912   pParse->sNameToken = *pName;
913   if( zName==0 ) return;
914   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
915     goto begin_table_error;
916   }
917   if( db->init.iDb==1 ) isTemp = 1;
918 #ifndef SQLITE_OMIT_AUTHORIZATION
919   assert( isTemp==0 || isTemp==1 );
920   assert( isView==0 || isView==1 );
921   {
922     static const u8 aCode[] = {
923        SQLITE_CREATE_TABLE,
924        SQLITE_CREATE_TEMP_TABLE,
925        SQLITE_CREATE_VIEW,
926        SQLITE_CREATE_TEMP_VIEW
927     };
928     char *zDb = db->aDb[iDb].zDbSName;
929     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
930       goto begin_table_error;
931     }
932     if( !isVirtual && sqlite3AuthCheck(pParse, (int)aCode[isTemp+2*isView],
933                                        zName, 0, zDb) ){
934       goto begin_table_error;
935     }
936   }
937 #endif
938 
939   /* Make sure the new table name does not collide with an existing
940   ** index or table name in the same database.  Issue an error message if
941   ** it does. The exception is if the statement being parsed was passed
942   ** to an sqlite3_declare_vtab() call. In that case only the column names
943   ** and types will be used, so there is no need to test for namespace
944   ** collisions.
945   */
946   if( !IN_SPECIAL_PARSE ){
947     char *zDb = db->aDb[iDb].zDbSName;
948     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
949       goto begin_table_error;
950     }
951     pTable = sqlite3FindTable(db, zName, zDb);
952     if( pTable ){
953       if( !noErr ){
954         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
955       }else{
956         assert( !db->init.busy || CORRUPT_DB );
957         sqlite3CodeVerifySchema(pParse, iDb);
958       }
959       goto begin_table_error;
960     }
961     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
962       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
963       goto begin_table_error;
964     }
965   }
966 
967   pTable = sqlite3DbMallocZero(db, sizeof(Table));
968   if( pTable==0 ){
969     assert( db->mallocFailed );
970     pParse->rc = SQLITE_NOMEM_BKPT;
971     pParse->nErr++;
972     goto begin_table_error;
973   }
974   pTable->zName = zName;
975   pTable->iPKey = -1;
976   pTable->pSchema = db->aDb[iDb].pSchema;
977   pTable->nTabRef = 1;
978 #ifdef SQLITE_DEFAULT_ROWEST
979   pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
980 #else
981   pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
982 #endif
983   assert( pParse->pNewTable==0 );
984   pParse->pNewTable = pTable;
985 
986   /* If this is the magic sqlite_sequence table used by autoincrement,
987   ** then record a pointer to this table in the main database structure
988   ** so that INSERT can find the table easily.
989   */
990 #ifndef SQLITE_OMIT_AUTOINCREMENT
991   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
992     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
993     pTable->pSchema->pSeqTab = pTable;
994   }
995 #endif
996 
997   /* Begin generating the code that will insert the table record into
998   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
999   ** and allocate the record number for the table entry now.  Before any
1000   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
1001   ** indices to be created and the table record must come before the
1002   ** indices.  Hence, the record number for the table must be allocated
1003   ** now.
1004   */
1005   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
1006     int addr1;
1007     int fileFormat;
1008     int reg1, reg2, reg3;
1009     /* nullRow[] is an OP_Record encoding of a row containing 5 NULLs */
1010     static const char nullRow[] = { 6, 0, 0, 0, 0, 0 };
1011     sqlite3BeginWriteOperation(pParse, 1, iDb);
1012 
1013 #ifndef SQLITE_OMIT_VIRTUALTABLE
1014     if( isVirtual ){
1015       sqlite3VdbeAddOp0(v, OP_VBegin);
1016     }
1017 #endif
1018 
1019     /* If the file format and encoding in the database have not been set,
1020     ** set them now.
1021     */
1022     reg1 = pParse->regRowid = ++pParse->nMem;
1023     reg2 = pParse->regRoot = ++pParse->nMem;
1024     reg3 = ++pParse->nMem;
1025     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
1026     sqlite3VdbeUsesBtree(v, iDb);
1027     addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
1028     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
1029                   1 : SQLITE_MAX_FILE_FORMAT;
1030     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
1031     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
1032     sqlite3VdbeJumpHere(v, addr1);
1033 
1034     /* This just creates a place-holder record in the sqlite_master table.
1035     ** The record created does not contain anything yet.  It will be replaced
1036     ** by the real entry in code generated at sqlite3EndTable().
1037     **
1038     ** The rowid for the new entry is left in register pParse->regRowid.
1039     ** The root page number of the new table is left in reg pParse->regRoot.
1040     ** The rowid and root page number values are needed by the code that
1041     ** sqlite3EndTable will generate.
1042     */
1043 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
1044     if( isView || isVirtual ){
1045       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
1046     }else
1047 #endif
1048     {
1049       pParse->addrCrTab =
1050          sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, reg2, BTREE_INTKEY);
1051     }
1052     sqlite3OpenMasterTable(pParse, iDb);
1053     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
1054     sqlite3VdbeAddOp4(v, OP_Blob, 6, reg3, 0, nullRow, P4_STATIC);
1055     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
1056     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
1057     sqlite3VdbeAddOp0(v, OP_Close);
1058   }
1059 
1060   /* Normal (non-error) return. */
1061   return;
1062 
1063   /* If an error occurs, we jump here */
1064 begin_table_error:
1065   sqlite3DbFree(db, zName);
1066   return;
1067 }
1068 
1069 /* Set properties of a table column based on the (magical)
1070 ** name of the column.
1071 */
1072 #if SQLITE_ENABLE_HIDDEN_COLUMNS
1073 void sqlite3ColumnPropertiesFromName(Table *pTab, Column *pCol){
1074   if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
1075     pCol->colFlags |= COLFLAG_HIDDEN;
1076   }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
1077     pTab->tabFlags |= TF_OOOHidden;
1078   }
1079 }
1080 #endif
1081 
1082 
1083 /*
1084 ** Add a new column to the table currently being constructed.
1085 **
1086 ** The parser calls this routine once for each column declaration
1087 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
1088 ** first to get things going.  Then this routine is called for each
1089 ** column.
1090 */
1091 void sqlite3AddColumn(Parse *pParse, Token *pName, Token *pType){
1092   Table *p;
1093   int i;
1094   char *z;
1095   char *zType;
1096   Column *pCol;
1097   sqlite3 *db = pParse->db;
1098   if( (p = pParse->pNewTable)==0 ) return;
1099   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
1100     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
1101     return;
1102   }
1103   z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
1104   if( z==0 ) return;
1105   if( IN_RENAME_OBJECT ) sqlite3RenameTokenMap(pParse, (void*)z, pName);
1106   memcpy(z, pName->z, pName->n);
1107   z[pName->n] = 0;
1108   sqlite3Dequote(z);
1109   for(i=0; i<p->nCol; i++){
1110     if( sqlite3_stricmp(z, p->aCol[i].zName)==0 ){
1111       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
1112       sqlite3DbFree(db, z);
1113       return;
1114     }
1115   }
1116   if( (p->nCol & 0x7)==0 ){
1117     Column *aNew;
1118     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
1119     if( aNew==0 ){
1120       sqlite3DbFree(db, z);
1121       return;
1122     }
1123     p->aCol = aNew;
1124   }
1125   pCol = &p->aCol[p->nCol];
1126   memset(pCol, 0, sizeof(p->aCol[0]));
1127   pCol->zName = z;
1128   sqlite3ColumnPropertiesFromName(p, pCol);
1129 
1130   if( pType->n==0 ){
1131     /* If there is no type specified, columns have the default affinity
1132     ** 'BLOB' with a default size of 4 bytes. */
1133     pCol->affinity = SQLITE_AFF_BLOB;
1134     pCol->szEst = 1;
1135 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1136     if( 4>=sqlite3GlobalConfig.szSorterRef ){
1137       pCol->colFlags |= COLFLAG_SORTERREF;
1138     }
1139 #endif
1140   }else{
1141     zType = z + sqlite3Strlen30(z) + 1;
1142     memcpy(zType, pType->z, pType->n);
1143     zType[pType->n] = 0;
1144     sqlite3Dequote(zType);
1145     pCol->affinity = sqlite3AffinityType(zType, pCol);
1146     pCol->colFlags |= COLFLAG_HASTYPE;
1147   }
1148   p->nCol++;
1149   pParse->constraintName.n = 0;
1150 }
1151 
1152 /*
1153 ** This routine is called by the parser while in the middle of
1154 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
1155 ** been seen on a column.  This routine sets the notNull flag on
1156 ** the column currently under construction.
1157 */
1158 void sqlite3AddNotNull(Parse *pParse, int onError){
1159   Table *p;
1160   Column *pCol;
1161   p = pParse->pNewTable;
1162   if( p==0 || NEVER(p->nCol<1) ) return;
1163   pCol = &p->aCol[p->nCol-1];
1164   pCol->notNull = (u8)onError;
1165   p->tabFlags |= TF_HasNotNull;
1166 
1167   /* Set the uniqNotNull flag on any UNIQUE or PK indexes already created
1168   ** on this column.  */
1169   if( pCol->colFlags & COLFLAG_UNIQUE ){
1170     Index *pIdx;
1171     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
1172       assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
1173       if( pIdx->aiColumn[0]==p->nCol-1 ){
1174         pIdx->uniqNotNull = 1;
1175       }
1176     }
1177   }
1178 }
1179 
1180 /*
1181 ** Scan the column type name zType (length nType) and return the
1182 ** associated affinity type.
1183 **
1184 ** This routine does a case-independent search of zType for the
1185 ** substrings in the following table. If one of the substrings is
1186 ** found, the corresponding affinity is returned. If zType contains
1187 ** more than one of the substrings, entries toward the top of
1188 ** the table take priority. For example, if zType is 'BLOBINT',
1189 ** SQLITE_AFF_INTEGER is returned.
1190 **
1191 ** Substring     | Affinity
1192 ** --------------------------------
1193 ** 'INT'         | SQLITE_AFF_INTEGER
1194 ** 'CHAR'        | SQLITE_AFF_TEXT
1195 ** 'CLOB'        | SQLITE_AFF_TEXT
1196 ** 'TEXT'        | SQLITE_AFF_TEXT
1197 ** 'BLOB'        | SQLITE_AFF_BLOB
1198 ** 'REAL'        | SQLITE_AFF_REAL
1199 ** 'FLOA'        | SQLITE_AFF_REAL
1200 ** 'DOUB'        | SQLITE_AFF_REAL
1201 **
1202 ** If none of the substrings in the above table are found,
1203 ** SQLITE_AFF_NUMERIC is returned.
1204 */
1205 char sqlite3AffinityType(const char *zIn, Column *pCol){
1206   u32 h = 0;
1207   char aff = SQLITE_AFF_NUMERIC;
1208   const char *zChar = 0;
1209 
1210   assert( zIn!=0 );
1211   while( zIn[0] ){
1212     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
1213     zIn++;
1214     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
1215       aff = SQLITE_AFF_TEXT;
1216       zChar = zIn;
1217     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
1218       aff = SQLITE_AFF_TEXT;
1219     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
1220       aff = SQLITE_AFF_TEXT;
1221     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
1222         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
1223       aff = SQLITE_AFF_BLOB;
1224       if( zIn[0]=='(' ) zChar = zIn;
1225 #ifndef SQLITE_OMIT_FLOATING_POINT
1226     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
1227         && aff==SQLITE_AFF_NUMERIC ){
1228       aff = SQLITE_AFF_REAL;
1229     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
1230         && aff==SQLITE_AFF_NUMERIC ){
1231       aff = SQLITE_AFF_REAL;
1232     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
1233         && aff==SQLITE_AFF_NUMERIC ){
1234       aff = SQLITE_AFF_REAL;
1235 #endif
1236     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
1237       aff = SQLITE_AFF_INTEGER;
1238       break;
1239     }
1240   }
1241 
1242   /* If pCol is not NULL, store an estimate of the field size.  The
1243   ** estimate is scaled so that the size of an integer is 1.  */
1244   if( pCol ){
1245     int v = 0;   /* default size is approx 4 bytes */
1246     if( aff<SQLITE_AFF_NUMERIC ){
1247       if( zChar ){
1248         while( zChar[0] ){
1249           if( sqlite3Isdigit(zChar[0]) ){
1250             /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
1251             sqlite3GetInt32(zChar, &v);
1252             break;
1253           }
1254           zChar++;
1255         }
1256       }else{
1257         v = 16;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
1258       }
1259     }
1260 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
1261     if( v>=sqlite3GlobalConfig.szSorterRef ){
1262       pCol->colFlags |= COLFLAG_SORTERREF;
1263     }
1264 #endif
1265     v = v/4 + 1;
1266     if( v>255 ) v = 255;
1267     pCol->szEst = v;
1268   }
1269   return aff;
1270 }
1271 
1272 /*
1273 ** The expression is the default value for the most recently added column
1274 ** of the table currently under construction.
1275 **
1276 ** Default value expressions must be constant.  Raise an exception if this
1277 ** is not the case.
1278 **
1279 ** This routine is called by the parser while in the middle of
1280 ** parsing a CREATE TABLE statement.
1281 */
1282 void sqlite3AddDefaultValue(
1283   Parse *pParse,           /* Parsing context */
1284   Expr *pExpr,             /* The parsed expression of the default value */
1285   const char *zStart,      /* Start of the default value text */
1286   const char *zEnd         /* First character past end of defaut value text */
1287 ){
1288   Table *p;
1289   Column *pCol;
1290   sqlite3 *db = pParse->db;
1291   p = pParse->pNewTable;
1292   if( p!=0 ){
1293     pCol = &(p->aCol[p->nCol-1]);
1294     if( !sqlite3ExprIsConstantOrFunction(pExpr, db->init.busy) ){
1295       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
1296           pCol->zName);
1297     }else{
1298       /* A copy of pExpr is used instead of the original, as pExpr contains
1299       ** tokens that point to volatile memory.
1300       */
1301       Expr x;
1302       sqlite3ExprDelete(db, pCol->pDflt);
1303       memset(&x, 0, sizeof(x));
1304       x.op = TK_SPAN;
1305       x.u.zToken = sqlite3DbSpanDup(db, zStart, zEnd);
1306       x.pLeft = pExpr;
1307       x.flags = EP_Skip;
1308       pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
1309       sqlite3DbFree(db, x.u.zToken);
1310     }
1311   }
1312   if( IN_RENAME_OBJECT ){
1313     sqlite3RenameExprUnmap(pParse, pExpr);
1314   }
1315   sqlite3ExprDelete(db, pExpr);
1316 }
1317 
1318 /*
1319 ** Backwards Compatibility Hack:
1320 **
1321 ** Historical versions of SQLite accepted strings as column names in
1322 ** indexes and PRIMARY KEY constraints and in UNIQUE constraints.  Example:
1323 **
1324 **     CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
1325 **     CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
1326 **
1327 ** This is goofy.  But to preserve backwards compatibility we continue to
1328 ** accept it.  This routine does the necessary conversion.  It converts
1329 ** the expression given in its argument from a TK_STRING into a TK_ID
1330 ** if the expression is just a TK_STRING with an optional COLLATE clause.
1331 ** If the epxression is anything other than TK_STRING, the expression is
1332 ** unchanged.
1333 */
1334 static void sqlite3StringToId(Expr *p){
1335   if( p->op==TK_STRING ){
1336     p->op = TK_ID;
1337   }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
1338     p->pLeft->op = TK_ID;
1339   }
1340 }
1341 
1342 /*
1343 ** Designate the PRIMARY KEY for the table.  pList is a list of names
1344 ** of columns that form the primary key.  If pList is NULL, then the
1345 ** most recently added column of the table is the primary key.
1346 **
1347 ** A table can have at most one primary key.  If the table already has
1348 ** a primary key (and this is the second primary key) then create an
1349 ** error.
1350 **
1351 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
1352 ** then we will try to use that column as the rowid.  Set the Table.iPKey
1353 ** field of the table under construction to be the index of the
1354 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
1355 ** no INTEGER PRIMARY KEY.
1356 **
1357 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
1358 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
1359 */
1360 void sqlite3AddPrimaryKey(
1361   Parse *pParse,    /* Parsing context */
1362   ExprList *pList,  /* List of field names to be indexed */
1363   int onError,      /* What to do with a uniqueness conflict */
1364   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
1365   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
1366 ){
1367   Table *pTab = pParse->pNewTable;
1368   Column *pCol = 0;
1369   int iCol = -1, i;
1370   int nTerm;
1371   if( pTab==0 ) goto primary_key_exit;
1372   if( pTab->tabFlags & TF_HasPrimaryKey ){
1373     sqlite3ErrorMsg(pParse,
1374       "table \"%s\" has more than one primary key", pTab->zName);
1375     goto primary_key_exit;
1376   }
1377   pTab->tabFlags |= TF_HasPrimaryKey;
1378   if( pList==0 ){
1379     iCol = pTab->nCol - 1;
1380     pCol = &pTab->aCol[iCol];
1381     pCol->colFlags |= COLFLAG_PRIMKEY;
1382     nTerm = 1;
1383   }else{
1384     nTerm = pList->nExpr;
1385     for(i=0; i<nTerm; i++){
1386       Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
1387       assert( pCExpr!=0 );
1388       sqlite3StringToId(pCExpr);
1389       if( pCExpr->op==TK_ID ){
1390         const char *zCName = pCExpr->u.zToken;
1391         for(iCol=0; iCol<pTab->nCol; iCol++){
1392           if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
1393             pCol = &pTab->aCol[iCol];
1394             pCol->colFlags |= COLFLAG_PRIMKEY;
1395             break;
1396           }
1397         }
1398       }
1399     }
1400   }
1401   if( nTerm==1
1402    && pCol
1403    && sqlite3StrICmp(sqlite3ColumnType(pCol,""), "INTEGER")==0
1404    && sortOrder!=SQLITE_SO_DESC
1405   ){
1406     if( IN_RENAME_OBJECT && pList ){
1407       sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pList->a[0].pExpr);
1408     }
1409     pTab->iPKey = iCol;
1410     pTab->keyConf = (u8)onError;
1411     assert( autoInc==0 || autoInc==1 );
1412     pTab->tabFlags |= autoInc*TF_Autoincrement;
1413     if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
1414   }else if( autoInc ){
1415 #ifndef SQLITE_OMIT_AUTOINCREMENT
1416     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
1417        "INTEGER PRIMARY KEY");
1418 #endif
1419   }else{
1420     sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
1421                            0, sortOrder, 0, SQLITE_IDXTYPE_PRIMARYKEY);
1422     pList = 0;
1423   }
1424 
1425 primary_key_exit:
1426   sqlite3ExprListDelete(pParse->db, pList);
1427   return;
1428 }
1429 
1430 /*
1431 ** Add a new CHECK constraint to the table currently under construction.
1432 */
1433 void sqlite3AddCheckConstraint(
1434   Parse *pParse,    /* Parsing context */
1435   Expr *pCheckExpr  /* The check expression */
1436 ){
1437 #ifndef SQLITE_OMIT_CHECK
1438   Table *pTab = pParse->pNewTable;
1439   sqlite3 *db = pParse->db;
1440   if( pTab && !IN_DECLARE_VTAB
1441    && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
1442   ){
1443     pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
1444     if( pParse->constraintName.n ){
1445       sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
1446     }
1447   }else
1448 #endif
1449   {
1450     sqlite3ExprDelete(pParse->db, pCheckExpr);
1451   }
1452 }
1453 
1454 /*
1455 ** Set the collation function of the most recently parsed table column
1456 ** to the CollSeq given.
1457 */
1458 void sqlite3AddCollateType(Parse *pParse, Token *pToken){
1459   Table *p;
1460   int i;
1461   char *zColl;              /* Dequoted name of collation sequence */
1462   sqlite3 *db;
1463 
1464   if( (p = pParse->pNewTable)==0 ) return;
1465   i = p->nCol-1;
1466   db = pParse->db;
1467   zColl = sqlite3NameFromToken(db, pToken);
1468   if( !zColl ) return;
1469 
1470   if( sqlite3LocateCollSeq(pParse, zColl) ){
1471     Index *pIdx;
1472     sqlite3DbFree(db, p->aCol[i].zColl);
1473     p->aCol[i].zColl = zColl;
1474 
1475     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
1476     ** then an index may have been created on this column before the
1477     ** collation type was added. Correct this if it is the case.
1478     */
1479     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
1480       assert( pIdx->nKeyCol==1 );
1481       if( pIdx->aiColumn[0]==i ){
1482         pIdx->azColl[0] = p->aCol[i].zColl;
1483       }
1484     }
1485   }else{
1486     sqlite3DbFree(db, zColl);
1487   }
1488 }
1489 
1490 /*
1491 ** This function returns the collation sequence for database native text
1492 ** encoding identified by the string zName, length nName.
1493 **
1494 ** If the requested collation sequence is not available, or not available
1495 ** in the database native encoding, the collation factory is invoked to
1496 ** request it. If the collation factory does not supply such a sequence,
1497 ** and the sequence is available in another text encoding, then that is
1498 ** returned instead.
1499 **
1500 ** If no versions of the requested collations sequence are available, or
1501 ** another error occurs, NULL is returned and an error message written into
1502 ** pParse.
1503 **
1504 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
1505 ** invokes the collation factory if the named collation cannot be found
1506 ** and generates an error message.
1507 **
1508 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
1509 */
1510 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
1511   sqlite3 *db = pParse->db;
1512   u8 enc = ENC(db);
1513   u8 initbusy = db->init.busy;
1514   CollSeq *pColl;
1515 
1516   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
1517   if( !initbusy && (!pColl || !pColl->xCmp) ){
1518     pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
1519   }
1520 
1521   return pColl;
1522 }
1523 
1524 
1525 /*
1526 ** Generate code that will increment the schema cookie.
1527 **
1528 ** The schema cookie is used to determine when the schema for the
1529 ** database changes.  After each schema change, the cookie value
1530 ** changes.  When a process first reads the schema it records the
1531 ** cookie.  Thereafter, whenever it goes to access the database,
1532 ** it checks the cookie to make sure the schema has not changed
1533 ** since it was last read.
1534 **
1535 ** This plan is not completely bullet-proof.  It is possible for
1536 ** the schema to change multiple times and for the cookie to be
1537 ** set back to prior value.  But schema changes are infrequent
1538 ** and the probability of hitting the same cookie value is only
1539 ** 1 chance in 2^32.  So we're safe enough.
1540 **
1541 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
1542 ** the schema-version whenever the schema changes.
1543 */
1544 void sqlite3ChangeCookie(Parse *pParse, int iDb){
1545   sqlite3 *db = pParse->db;
1546   Vdbe *v = pParse->pVdbe;
1547   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1548   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
1549                    (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
1550 }
1551 
1552 /*
1553 ** Measure the number of characters needed to output the given
1554 ** identifier.  The number returned includes any quotes used
1555 ** but does not include the null terminator.
1556 **
1557 ** The estimate is conservative.  It might be larger that what is
1558 ** really needed.
1559 */
1560 static int identLength(const char *z){
1561   int n;
1562   for(n=0; *z; n++, z++){
1563     if( *z=='"' ){ n++; }
1564   }
1565   return n + 2;
1566 }
1567 
1568 /*
1569 ** The first parameter is a pointer to an output buffer. The second
1570 ** parameter is a pointer to an integer that contains the offset at
1571 ** which to write into the output buffer. This function copies the
1572 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
1573 ** to the specified offset in the buffer and updates *pIdx to refer
1574 ** to the first byte after the last byte written before returning.
1575 **
1576 ** If the string zSignedIdent consists entirely of alpha-numeric
1577 ** characters, does not begin with a digit and is not an SQL keyword,
1578 ** then it is copied to the output buffer exactly as it is. Otherwise,
1579 ** it is quoted using double-quotes.
1580 */
1581 static void identPut(char *z, int *pIdx, char *zSignedIdent){
1582   unsigned char *zIdent = (unsigned char*)zSignedIdent;
1583   int i, j, needQuote;
1584   i = *pIdx;
1585 
1586   for(j=0; zIdent[j]; j++){
1587     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1588   }
1589   needQuote = sqlite3Isdigit(zIdent[0])
1590             || sqlite3KeywordCode(zIdent, j)!=TK_ID
1591             || zIdent[j]!=0
1592             || j==0;
1593 
1594   if( needQuote ) z[i++] = '"';
1595   for(j=0; zIdent[j]; j++){
1596     z[i++] = zIdent[j];
1597     if( zIdent[j]=='"' ) z[i++] = '"';
1598   }
1599   if( needQuote ) z[i++] = '"';
1600   z[i] = 0;
1601   *pIdx = i;
1602 }
1603 
1604 /*
1605 ** Generate a CREATE TABLE statement appropriate for the given
1606 ** table.  Memory to hold the text of the statement is obtained
1607 ** from sqliteMalloc() and must be freed by the calling function.
1608 */
1609 static char *createTableStmt(sqlite3 *db, Table *p){
1610   int i, k, n;
1611   char *zStmt;
1612   char *zSep, *zSep2, *zEnd;
1613   Column *pCol;
1614   n = 0;
1615   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1616     n += identLength(pCol->zName) + 5;
1617   }
1618   n += identLength(p->zName);
1619   if( n<50 ){
1620     zSep = "";
1621     zSep2 = ",";
1622     zEnd = ")";
1623   }else{
1624     zSep = "\n  ";
1625     zSep2 = ",\n  ";
1626     zEnd = "\n)";
1627   }
1628   n += 35 + 6*p->nCol;
1629   zStmt = sqlite3DbMallocRaw(0, n);
1630   if( zStmt==0 ){
1631     sqlite3OomFault(db);
1632     return 0;
1633   }
1634   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
1635   k = sqlite3Strlen30(zStmt);
1636   identPut(zStmt, &k, p->zName);
1637   zStmt[k++] = '(';
1638   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
1639     static const char * const azType[] = {
1640         /* SQLITE_AFF_BLOB    */ "",
1641         /* SQLITE_AFF_TEXT    */ " TEXT",
1642         /* SQLITE_AFF_NUMERIC */ " NUM",
1643         /* SQLITE_AFF_INTEGER */ " INT",
1644         /* SQLITE_AFF_REAL    */ " REAL"
1645     };
1646     int len;
1647     const char *zType;
1648 
1649     sqlite3_snprintf(n-k, &zStmt[k], zSep);
1650     k += sqlite3Strlen30(&zStmt[k]);
1651     zSep = zSep2;
1652     identPut(zStmt, &k, pCol->zName);
1653     assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
1654     assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
1655     testcase( pCol->affinity==SQLITE_AFF_BLOB );
1656     testcase( pCol->affinity==SQLITE_AFF_TEXT );
1657     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
1658     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
1659     testcase( pCol->affinity==SQLITE_AFF_REAL );
1660 
1661     zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
1662     len = sqlite3Strlen30(zType);
1663     assert( pCol->affinity==SQLITE_AFF_BLOB
1664             || pCol->affinity==sqlite3AffinityType(zType, 0) );
1665     memcpy(&zStmt[k], zType, len);
1666     k += len;
1667     assert( k<=n );
1668   }
1669   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
1670   return zStmt;
1671 }
1672 
1673 /*
1674 ** Resize an Index object to hold N columns total.  Return SQLITE_OK
1675 ** on success and SQLITE_NOMEM on an OOM error.
1676 */
1677 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
1678   char *zExtra;
1679   int nByte;
1680   if( pIdx->nColumn>=N ) return SQLITE_OK;
1681   assert( pIdx->isResized==0 );
1682   nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
1683   zExtra = sqlite3DbMallocZero(db, nByte);
1684   if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
1685   memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
1686   pIdx->azColl = (const char**)zExtra;
1687   zExtra += sizeof(char*)*N;
1688   memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
1689   pIdx->aiColumn = (i16*)zExtra;
1690   zExtra += sizeof(i16)*N;
1691   memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
1692   pIdx->aSortOrder = (u8*)zExtra;
1693   pIdx->nColumn = N;
1694   pIdx->isResized = 1;
1695   return SQLITE_OK;
1696 }
1697 
1698 /*
1699 ** Estimate the total row width for a table.
1700 */
1701 static void estimateTableWidth(Table *pTab){
1702   unsigned wTable = 0;
1703   const Column *pTabCol;
1704   int i;
1705   for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
1706     wTable += pTabCol->szEst;
1707   }
1708   if( pTab->iPKey<0 ) wTable++;
1709   pTab->szTabRow = sqlite3LogEst(wTable*4);
1710 }
1711 
1712 /*
1713 ** Estimate the average size of a row for an index.
1714 */
1715 static void estimateIndexWidth(Index *pIdx){
1716   unsigned wIndex = 0;
1717   int i;
1718   const Column *aCol = pIdx->pTable->aCol;
1719   for(i=0; i<pIdx->nColumn; i++){
1720     i16 x = pIdx->aiColumn[i];
1721     assert( x<pIdx->pTable->nCol );
1722     wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
1723   }
1724   pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
1725 }
1726 
1727 /* Return true if value x is found any of the first nCol entries of aiCol[]
1728 */
1729 static int hasColumn(const i16 *aiCol, int nCol, int x){
1730   while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
1731   return 0;
1732 }
1733 
1734 /* Recompute the colNotIdxed field of the Index.
1735 **
1736 ** colNotIdxed is a bitmask that has a 0 bit representing each indexed
1737 ** columns that are within the first 63 columns of the table.  The
1738 ** high-order bit of colNotIdxed is always 1.  All unindexed columns
1739 ** of the table have a 1.
1740 **
1741 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
1742 ** to determine if the index is covering index.
1743 */
1744 static void recomputeColumnsNotIndexed(Index *pIdx){
1745   Bitmask m = 0;
1746   int j;
1747   for(j=pIdx->nColumn-1; j>=0; j--){
1748     int x = pIdx->aiColumn[j];
1749     if( x>=0 ){
1750       testcase( x==BMS-1 );
1751       testcase( x==BMS-2 );
1752       if( x<BMS-1 ) m |= MASKBIT(x);
1753     }
1754   }
1755   pIdx->colNotIdxed = ~m;
1756   assert( (pIdx->colNotIdxed>>63)==1 );
1757 }
1758 
1759 /*
1760 ** This routine runs at the end of parsing a CREATE TABLE statement that
1761 ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
1762 ** internal schema data structures and the generated VDBE code so that they
1763 ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
1764 ** Changes include:
1765 **
1766 **     (1)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
1767 **     (2)  Convert P3 parameter of the OP_CreateBtree from BTREE_INTKEY
1768 **          into BTREE_BLOBKEY.
1769 **     (3)  Bypass the creation of the sqlite_master table entry
1770 **          for the PRIMARY KEY as the primary key index is now
1771 **          identified by the sqlite_master table entry of the table itself.
1772 **     (4)  Set the Index.tnum of the PRIMARY KEY Index object in the
1773 **          schema to the rootpage from the main table.
1774 **     (5)  Add all table columns to the PRIMARY KEY Index object
1775 **          so that the PRIMARY KEY is a covering index.  The surplus
1776 **          columns are part of KeyInfo.nAllField and are not used for
1777 **          sorting or lookup or uniqueness checks.
1778 **     (6)  Replace the rowid tail on all automatically generated UNIQUE
1779 **          indices with the PRIMARY KEY columns.
1780 **
1781 ** For virtual tables, only (1) is performed.
1782 */
1783 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
1784   Index *pIdx;
1785   Index *pPk;
1786   int nPk;
1787   int i, j;
1788   sqlite3 *db = pParse->db;
1789   Vdbe *v = pParse->pVdbe;
1790 
1791   /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
1792   */
1793   if( !db->init.imposterTable ){
1794     for(i=0; i<pTab->nCol; i++){
1795       if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
1796         pTab->aCol[i].notNull = OE_Abort;
1797       }
1798     }
1799   }
1800 
1801   /* Convert the P3 operand of the OP_CreateBtree opcode from BTREE_INTKEY
1802   ** into BTREE_BLOBKEY.
1803   */
1804   if( pParse->addrCrTab ){
1805     assert( v );
1806     sqlite3VdbeChangeP3(v, pParse->addrCrTab, BTREE_BLOBKEY);
1807   }
1808 
1809   /* Locate the PRIMARY KEY index.  Or, if this table was originally
1810   ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
1811   */
1812   if( pTab->iPKey>=0 ){
1813     ExprList *pList;
1814     Token ipkToken;
1815     sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
1816     pList = sqlite3ExprListAppend(pParse, 0,
1817                   sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
1818     if( pList==0 ) return;
1819     pList->a[0].sortOrder = pParse->iPkSortOrder;
1820     assert( pParse->pNewTable==pTab );
1821     sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
1822                        SQLITE_IDXTYPE_PRIMARYKEY);
1823     if( db->mallocFailed || pParse->nErr ) return;
1824     pPk = sqlite3PrimaryKeyIndex(pTab);
1825     pTab->iPKey = -1;
1826   }else{
1827     pPk = sqlite3PrimaryKeyIndex(pTab);
1828     assert( pPk!=0 );
1829 
1830     /*
1831     ** Remove all redundant columns from the PRIMARY KEY.  For example, change
1832     ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
1833     ** code assumes the PRIMARY KEY contains no repeated columns.
1834     */
1835     for(i=j=1; i<pPk->nKeyCol; i++){
1836       if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
1837         pPk->nColumn--;
1838       }else{
1839         pPk->aiColumn[j++] = pPk->aiColumn[i];
1840       }
1841     }
1842     pPk->nKeyCol = j;
1843   }
1844   assert( pPk!=0 );
1845   pPk->isCovering = 1;
1846   if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
1847   nPk = pPk->nKeyCol;
1848 
1849   /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
1850   ** table entry. This is only required if currently generating VDBE
1851   ** code for a CREATE TABLE (not when parsing one as part of reading
1852   ** a database schema).  */
1853   if( v && pPk->tnum>0 ){
1854     assert( db->init.busy==0 );
1855     sqlite3VdbeChangeOpcode(v, pPk->tnum, OP_Goto);
1856   }
1857 
1858   /* The root page of the PRIMARY KEY is the table root page */
1859   pPk->tnum = pTab->tnum;
1860 
1861   /* Update the in-memory representation of all UNIQUE indices by converting
1862   ** the final rowid column into one or more columns of the PRIMARY KEY.
1863   */
1864   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1865     int n;
1866     if( IsPrimaryKeyIndex(pIdx) ) continue;
1867     for(i=n=0; i<nPk; i++){
1868       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
1869     }
1870     if( n==0 ){
1871       /* This index is a superset of the primary key */
1872       pIdx->nColumn = pIdx->nKeyCol;
1873       continue;
1874     }
1875     if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
1876     for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
1877       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
1878         pIdx->aiColumn[j] = pPk->aiColumn[i];
1879         pIdx->azColl[j] = pPk->azColl[i];
1880         j++;
1881       }
1882     }
1883     assert( pIdx->nColumn>=pIdx->nKeyCol+n );
1884     assert( pIdx->nColumn>=j );
1885   }
1886 
1887   /* Add all table columns to the PRIMARY KEY index
1888   */
1889   if( nPk<pTab->nCol ){
1890     if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
1891     for(i=0, j=nPk; i<pTab->nCol; i++){
1892       if( !hasColumn(pPk->aiColumn, j, i) ){
1893         assert( j<pPk->nColumn );
1894         pPk->aiColumn[j] = i;
1895         pPk->azColl[j] = sqlite3StrBINARY;
1896         j++;
1897       }
1898     }
1899     assert( pPk->nColumn==j );
1900     assert( pTab->nCol==j );
1901   }else{
1902     pPk->nColumn = pTab->nCol;
1903   }
1904   recomputeColumnsNotIndexed(pPk);
1905 }
1906 
1907 #ifndef SQLITE_OMIT_VIRTUALTABLE
1908 /*
1909 ** Return true if zName is a shadow table name in the current database
1910 ** connection.
1911 **
1912 ** zName is temporarily modified while this routine is running, but is
1913 ** restored to its original value prior to this routine returning.
1914 */
1915 static int isShadowTableName(sqlite3 *db, char *zName){
1916   char *zTail;                  /* Pointer to the last "_" in zName */
1917   Table *pTab;                  /* Table that zName is a shadow of */
1918   Module *pMod;                 /* Module for the virtual table */
1919 
1920   zTail = strrchr(zName, '_');
1921   if( zTail==0 ) return 0;
1922   *zTail = 0;
1923   pTab = sqlite3FindTable(db, zName, 0);
1924   *zTail = '_';
1925   if( pTab==0 ) return 0;
1926   if( !IsVirtual(pTab) ) return 0;
1927   pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
1928   if( pMod==0 ) return 0;
1929   if( pMod->pModule->iVersion<3 ) return 0;
1930   if( pMod->pModule->xShadowName==0 ) return 0;
1931   return pMod->pModule->xShadowName(zTail+1);
1932 }
1933 #else
1934 # define isShadowTableName(x,y) 0
1935 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
1936 
1937 /*
1938 ** This routine is called to report the final ")" that terminates
1939 ** a CREATE TABLE statement.
1940 **
1941 ** The table structure that other action routines have been building
1942 ** is added to the internal hash tables, assuming no errors have
1943 ** occurred.
1944 **
1945 ** An entry for the table is made in the master table on disk, unless
1946 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
1947 ** it means we are reading the sqlite_master table because we just
1948 ** connected to the database or because the sqlite_master table has
1949 ** recently changed, so the entry for this table already exists in
1950 ** the sqlite_master table.  We do not want to create it again.
1951 **
1952 ** If the pSelect argument is not NULL, it means that this routine
1953 ** was called to create a table generated from a
1954 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
1955 ** the new table will match the result set of the SELECT.
1956 */
1957 void sqlite3EndTable(
1958   Parse *pParse,          /* Parse context */
1959   Token *pCons,           /* The ',' token after the last column defn. */
1960   Token *pEnd,            /* The ')' before options in the CREATE TABLE */
1961   u8 tabOpts,             /* Extra table options. Usually 0. */
1962   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
1963 ){
1964   Table *p;                 /* The new table */
1965   sqlite3 *db = pParse->db; /* The database connection */
1966   int iDb;                  /* Database in which the table lives */
1967   Index *pIdx;              /* An implied index of the table */
1968 
1969   if( pEnd==0 && pSelect==0 ){
1970     return;
1971   }
1972   assert( !db->mallocFailed );
1973   p = pParse->pNewTable;
1974   if( p==0 ) return;
1975 
1976   if( pSelect==0 && isShadowTableName(db, p->zName) ){
1977     p->tabFlags |= TF_Shadow;
1978   }
1979 
1980   /* If the db->init.busy is 1 it means we are reading the SQL off the
1981   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1982   ** So do not write to the disk again.  Extract the root page number
1983   ** for the table from the db->init.newTnum field.  (The page number
1984   ** should have been put there by the sqliteOpenCb routine.)
1985   **
1986   ** If the root page number is 1, that means this is the sqlite_master
1987   ** table itself.  So mark it read-only.
1988   */
1989   if( db->init.busy ){
1990     if( pSelect ){
1991       sqlite3ErrorMsg(pParse, "");
1992       return;
1993     }
1994     p->tnum = db->init.newTnum;
1995     if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
1996   }
1997 
1998   assert( (p->tabFlags & TF_HasPrimaryKey)==0
1999        || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
2000   assert( (p->tabFlags & TF_HasPrimaryKey)!=0
2001        || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
2002 
2003   /* Special processing for WITHOUT ROWID Tables */
2004   if( tabOpts & TF_WithoutRowid ){
2005     if( (p->tabFlags & TF_Autoincrement) ){
2006       sqlite3ErrorMsg(pParse,
2007           "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
2008       return;
2009     }
2010     if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
2011       sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
2012     }else{
2013       p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
2014       convertToWithoutRowidTable(pParse, p);
2015     }
2016   }
2017 
2018   iDb = sqlite3SchemaToIndex(db, p->pSchema);
2019 
2020 #ifndef SQLITE_OMIT_CHECK
2021   /* Resolve names in all CHECK constraint expressions.
2022   */
2023   if( p->pCheck ){
2024     sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
2025   }
2026 #endif /* !defined(SQLITE_OMIT_CHECK) */
2027 
2028   /* Estimate the average row size for the table and for all implied indices */
2029   estimateTableWidth(p);
2030   for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
2031     estimateIndexWidth(pIdx);
2032   }
2033 
2034   /* If not initializing, then create a record for the new table
2035   ** in the SQLITE_MASTER table of the database.
2036   **
2037   ** If this is a TEMPORARY table, write the entry into the auxiliary
2038   ** file instead of into the main database file.
2039   */
2040   if( !db->init.busy ){
2041     int n;
2042     Vdbe *v;
2043     char *zType;    /* "view" or "table" */
2044     char *zType2;   /* "VIEW" or "TABLE" */
2045     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
2046 
2047     v = sqlite3GetVdbe(pParse);
2048     if( NEVER(v==0) ) return;
2049 
2050     sqlite3VdbeAddOp1(v, OP_Close, 0);
2051 
2052     /*
2053     ** Initialize zType for the new view or table.
2054     */
2055     if( p->pSelect==0 ){
2056       /* A regular table */
2057       zType = "table";
2058       zType2 = "TABLE";
2059 #ifndef SQLITE_OMIT_VIEW
2060     }else{
2061       /* A view */
2062       zType = "view";
2063       zType2 = "VIEW";
2064 #endif
2065     }
2066 
2067     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
2068     ** statement to populate the new table. The root-page number for the
2069     ** new table is in register pParse->regRoot.
2070     **
2071     ** Once the SELECT has been coded by sqlite3Select(), it is in a
2072     ** suitable state to query for the column names and types to be used
2073     ** by the new table.
2074     **
2075     ** A shared-cache write-lock is not required to write to the new table,
2076     ** as a schema-lock must have already been obtained to create it. Since
2077     ** a schema-lock excludes all other database users, the write-lock would
2078     ** be redundant.
2079     */
2080     if( pSelect ){
2081       SelectDest dest;    /* Where the SELECT should store results */
2082       int regYield;       /* Register holding co-routine entry-point */
2083       int addrTop;        /* Top of the co-routine */
2084       int regRec;         /* A record to be insert into the new table */
2085       int regRowid;       /* Rowid of the next row to insert */
2086       int addrInsLoop;    /* Top of the loop for inserting rows */
2087       Table *pSelTab;     /* A table that describes the SELECT results */
2088 
2089       regYield = ++pParse->nMem;
2090       regRec = ++pParse->nMem;
2091       regRowid = ++pParse->nMem;
2092       assert(pParse->nTab==1);
2093       sqlite3MayAbort(pParse);
2094       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
2095       sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
2096       pParse->nTab = 2;
2097       addrTop = sqlite3VdbeCurrentAddr(v) + 1;
2098       sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
2099       if( pParse->nErr ) return;
2100       pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
2101       if( pSelTab==0 ) return;
2102       assert( p->aCol==0 );
2103       p->nCol = pSelTab->nCol;
2104       p->aCol = pSelTab->aCol;
2105       pSelTab->nCol = 0;
2106       pSelTab->aCol = 0;
2107       sqlite3DeleteTable(db, pSelTab);
2108       sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
2109       sqlite3Select(pParse, pSelect, &dest);
2110       if( pParse->nErr ) return;
2111       sqlite3VdbeEndCoroutine(v, regYield);
2112       sqlite3VdbeJumpHere(v, addrTop - 1);
2113       addrInsLoop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
2114       VdbeCoverage(v);
2115       sqlite3VdbeAddOp3(v, OP_MakeRecord, dest.iSdst, dest.nSdst, regRec);
2116       sqlite3TableAffinity(v, p, 0);
2117       sqlite3VdbeAddOp2(v, OP_NewRowid, 1, regRowid);
2118       sqlite3VdbeAddOp3(v, OP_Insert, 1, regRec, regRowid);
2119       sqlite3VdbeGoto(v, addrInsLoop);
2120       sqlite3VdbeJumpHere(v, addrInsLoop);
2121       sqlite3VdbeAddOp1(v, OP_Close, 1);
2122     }
2123 
2124     /* Compute the complete text of the CREATE statement */
2125     if( pSelect ){
2126       zStmt = createTableStmt(db, p);
2127     }else{
2128       Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
2129       n = (int)(pEnd2->z - pParse->sNameToken.z);
2130       if( pEnd2->z[0]!=';' ) n += pEnd2->n;
2131       zStmt = sqlite3MPrintf(db,
2132           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
2133       );
2134     }
2135 
2136     /* A slot for the record has already been allocated in the
2137     ** SQLITE_MASTER table.  We just need to update that slot with all
2138     ** the information we've collected.
2139     */
2140     sqlite3NestedParse(pParse,
2141       "UPDATE %Q.%s "
2142          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
2143        "WHERE rowid=#%d",
2144       db->aDb[iDb].zDbSName, MASTER_NAME,
2145       zType,
2146       p->zName,
2147       p->zName,
2148       pParse->regRoot,
2149       zStmt,
2150       pParse->regRowid
2151     );
2152     sqlite3DbFree(db, zStmt);
2153     sqlite3ChangeCookie(pParse, iDb);
2154 
2155 #ifndef SQLITE_OMIT_AUTOINCREMENT
2156     /* Check to see if we need to create an sqlite_sequence table for
2157     ** keeping track of autoincrement keys.
2158     */
2159     if( (p->tabFlags & TF_Autoincrement)!=0 ){
2160       Db *pDb = &db->aDb[iDb];
2161       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
2162       if( pDb->pSchema->pSeqTab==0 ){
2163         sqlite3NestedParse(pParse,
2164           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
2165           pDb->zDbSName
2166         );
2167       }
2168     }
2169 #endif
2170 
2171     /* Reparse everything to update our internal data structures */
2172     sqlite3VdbeAddParseSchemaOp(v, iDb,
2173            sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
2174   }
2175 
2176 
2177   /* Add the table to the in-memory representation of the database.
2178   */
2179   if( db->init.busy ){
2180     Table *pOld;
2181     Schema *pSchema = p->pSchema;
2182     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
2183     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
2184     if( pOld ){
2185       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
2186       sqlite3OomFault(db);
2187       return;
2188     }
2189     pParse->pNewTable = 0;
2190     db->mDbFlags |= DBFLAG_SchemaChange;
2191 
2192 #ifndef SQLITE_OMIT_ALTERTABLE
2193     if( !p->pSelect ){
2194       const char *zName = (const char *)pParse->sNameToken.z;
2195       int nName;
2196       assert( !pSelect && pCons && pEnd );
2197       if( pCons->z==0 ){
2198         pCons = pEnd;
2199       }
2200       nName = (int)((const char *)pCons->z - zName);
2201       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
2202     }
2203 #endif
2204   }
2205 }
2206 
2207 #ifndef SQLITE_OMIT_VIEW
2208 /*
2209 ** The parser calls this routine in order to create a new VIEW
2210 */
2211 void sqlite3CreateView(
2212   Parse *pParse,     /* The parsing context */
2213   Token *pBegin,     /* The CREATE token that begins the statement */
2214   Token *pName1,     /* The token that holds the name of the view */
2215   Token *pName2,     /* The token that holds the name of the view */
2216   ExprList *pCNames, /* Optional list of view column names */
2217   Select *pSelect,   /* A SELECT statement that will become the new view */
2218   int isTemp,        /* TRUE for a TEMPORARY view */
2219   int noErr          /* Suppress error messages if VIEW already exists */
2220 ){
2221   Table *p;
2222   int n;
2223   const char *z;
2224   Token sEnd;
2225   DbFixer sFix;
2226   Token *pName = 0;
2227   int iDb;
2228   sqlite3 *db = pParse->db;
2229 
2230   if( pParse->nVar>0 ){
2231     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
2232     goto create_view_fail;
2233   }
2234   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
2235   p = pParse->pNewTable;
2236   if( p==0 || pParse->nErr ) goto create_view_fail;
2237   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
2238   iDb = sqlite3SchemaToIndex(db, p->pSchema);
2239   sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
2240   if( sqlite3FixSelect(&sFix, pSelect) ) goto create_view_fail;
2241 
2242   /* Make a copy of the entire SELECT statement that defines the view.
2243   ** This will force all the Expr.token.z values to be dynamically
2244   ** allocated rather than point to the input string - which means that
2245   ** they will persist after the current sqlite3_exec() call returns.
2246   */
2247   if( IN_RENAME_OBJECT ){
2248     p->pSelect = pSelect;
2249     pSelect = 0;
2250   }else{
2251     p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
2252   }
2253   p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
2254   if( db->mallocFailed ) goto create_view_fail;
2255 
2256   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
2257   ** the end.
2258   */
2259   sEnd = pParse->sLastToken;
2260   assert( sEnd.z[0]!=0 || sEnd.n==0 );
2261   if( sEnd.z[0]!=';' ){
2262     sEnd.z += sEnd.n;
2263   }
2264   sEnd.n = 0;
2265   n = (int)(sEnd.z - pBegin->z);
2266   assert( n>0 );
2267   z = pBegin->z;
2268   while( sqlite3Isspace(z[n-1]) ){ n--; }
2269   sEnd.z = &z[n-1];
2270   sEnd.n = 1;
2271 
2272   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
2273   sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
2274 
2275 create_view_fail:
2276   sqlite3SelectDelete(db, pSelect);
2277   if( IN_RENAME_OBJECT ){
2278     sqlite3RenameExprlistUnmap(pParse, pCNames);
2279   }
2280   sqlite3ExprListDelete(db, pCNames);
2281   return;
2282 }
2283 #endif /* SQLITE_OMIT_VIEW */
2284 
2285 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
2286 /*
2287 ** The Table structure pTable is really a VIEW.  Fill in the names of
2288 ** the columns of the view in the pTable structure.  Return the number
2289 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
2290 */
2291 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
2292   Table *pSelTab;   /* A fake table from which we get the result set */
2293   Select *pSel;     /* Copy of the SELECT that implements the view */
2294   int nErr = 0;     /* Number of errors encountered */
2295   int n;            /* Temporarily holds the number of cursors assigned */
2296   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
2297 #ifndef SQLITE_OMIT_VIRTUALTABLE
2298   int rc;
2299 #endif
2300 #ifndef SQLITE_OMIT_AUTHORIZATION
2301   sqlite3_xauth xAuth;       /* Saved xAuth pointer */
2302 #endif
2303 
2304   assert( pTable );
2305 
2306 #ifndef SQLITE_OMIT_VIRTUALTABLE
2307   db->nSchemaLock++;
2308   rc = sqlite3VtabCallConnect(pParse, pTable);
2309   db->nSchemaLock--;
2310   if( rc ){
2311     return 1;
2312   }
2313   if( IsVirtual(pTable) ) return 0;
2314 #endif
2315 
2316 #ifndef SQLITE_OMIT_VIEW
2317   /* A positive nCol means the columns names for this view are
2318   ** already known.
2319   */
2320   if( pTable->nCol>0 ) return 0;
2321 
2322   /* A negative nCol is a special marker meaning that we are currently
2323   ** trying to compute the column names.  If we enter this routine with
2324   ** a negative nCol, it means two or more views form a loop, like this:
2325   **
2326   **     CREATE VIEW one AS SELECT * FROM two;
2327   **     CREATE VIEW two AS SELECT * FROM one;
2328   **
2329   ** Actually, the error above is now caught prior to reaching this point.
2330   ** But the following test is still important as it does come up
2331   ** in the following:
2332   **
2333   **     CREATE TABLE main.ex1(a);
2334   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
2335   **     SELECT * FROM temp.ex1;
2336   */
2337   if( pTable->nCol<0 ){
2338     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
2339     return 1;
2340   }
2341   assert( pTable->nCol>=0 );
2342 
2343   /* If we get this far, it means we need to compute the table names.
2344   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
2345   ** "*" elements in the results set of the view and will assign cursors
2346   ** to the elements of the FROM clause.  But we do not want these changes
2347   ** to be permanent.  So the computation is done on a copy of the SELECT
2348   ** statement that defines the view.
2349   */
2350   assert( pTable->pSelect );
2351   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
2352   if( pSel ){
2353 #ifndef SQLITE_OMIT_ALTERTABLE
2354     u8 eParseMode = pParse->eParseMode;
2355     pParse->eParseMode = PARSE_MODE_NORMAL;
2356 #endif
2357     n = pParse->nTab;
2358     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
2359     pTable->nCol = -1;
2360     db->lookaside.bDisable++;
2361 #ifndef SQLITE_OMIT_AUTHORIZATION
2362     xAuth = db->xAuth;
2363     db->xAuth = 0;
2364     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
2365     db->xAuth = xAuth;
2366 #else
2367     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
2368 #endif
2369     pParse->nTab = n;
2370     if( pTable->pCheck ){
2371       /* CREATE VIEW name(arglist) AS ...
2372       ** The names of the columns in the table are taken from
2373       ** arglist which is stored in pTable->pCheck.  The pCheck field
2374       ** normally holds CHECK constraints on an ordinary table, but for
2375       ** a VIEW it holds the list of column names.
2376       */
2377       sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
2378                                  &pTable->nCol, &pTable->aCol);
2379       if( db->mallocFailed==0
2380        && pParse->nErr==0
2381        && pTable->nCol==pSel->pEList->nExpr
2382       ){
2383         sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
2384       }
2385     }else if( pSelTab ){
2386       /* CREATE VIEW name AS...  without an argument list.  Construct
2387       ** the column names from the SELECT statement that defines the view.
2388       */
2389       assert( pTable->aCol==0 );
2390       pTable->nCol = pSelTab->nCol;
2391       pTable->aCol = pSelTab->aCol;
2392       pSelTab->nCol = 0;
2393       pSelTab->aCol = 0;
2394       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
2395     }else{
2396       pTable->nCol = 0;
2397       nErr++;
2398     }
2399     sqlite3DeleteTable(db, pSelTab);
2400     sqlite3SelectDelete(db, pSel);
2401     db->lookaside.bDisable--;
2402 #ifndef SQLITE_OMIT_ALTERTABLE
2403     pParse->eParseMode = eParseMode;
2404 #endif
2405   } else {
2406     nErr++;
2407   }
2408   pTable->pSchema->schemaFlags |= DB_UnresetViews;
2409   if( db->mallocFailed ){
2410     sqlite3DeleteColumnNames(db, pTable);
2411     pTable->aCol = 0;
2412     pTable->nCol = 0;
2413   }
2414 #endif /* SQLITE_OMIT_VIEW */
2415   return nErr;
2416 }
2417 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
2418 
2419 #ifndef SQLITE_OMIT_VIEW
2420 /*
2421 ** Clear the column names from every VIEW in database idx.
2422 */
2423 static void sqliteViewResetAll(sqlite3 *db, int idx){
2424   HashElem *i;
2425   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
2426   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
2427   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
2428     Table *pTab = sqliteHashData(i);
2429     if( pTab->pSelect ){
2430       sqlite3DeleteColumnNames(db, pTab);
2431       pTab->aCol = 0;
2432       pTab->nCol = 0;
2433     }
2434   }
2435   DbClearProperty(db, idx, DB_UnresetViews);
2436 }
2437 #else
2438 # define sqliteViewResetAll(A,B)
2439 #endif /* SQLITE_OMIT_VIEW */
2440 
2441 /*
2442 ** This function is called by the VDBE to adjust the internal schema
2443 ** used by SQLite when the btree layer moves a table root page. The
2444 ** root-page of a table or index in database iDb has changed from iFrom
2445 ** to iTo.
2446 **
2447 ** Ticket #1728:  The symbol table might still contain information
2448 ** on tables and/or indices that are the process of being deleted.
2449 ** If you are unlucky, one of those deleted indices or tables might
2450 ** have the same rootpage number as the real table or index that is
2451 ** being moved.  So we cannot stop searching after the first match
2452 ** because the first match might be for one of the deleted indices
2453 ** or tables and not the table/index that is actually being moved.
2454 ** We must continue looping until all tables and indices with
2455 ** rootpage==iFrom have been converted to have a rootpage of iTo
2456 ** in order to be certain that we got the right one.
2457 */
2458 #ifndef SQLITE_OMIT_AUTOVACUUM
2459 void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
2460   HashElem *pElem;
2461   Hash *pHash;
2462   Db *pDb;
2463 
2464   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
2465   pDb = &db->aDb[iDb];
2466   pHash = &pDb->pSchema->tblHash;
2467   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
2468     Table *pTab = sqliteHashData(pElem);
2469     if( pTab->tnum==iFrom ){
2470       pTab->tnum = iTo;
2471     }
2472   }
2473   pHash = &pDb->pSchema->idxHash;
2474   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
2475     Index *pIdx = sqliteHashData(pElem);
2476     if( pIdx->tnum==iFrom ){
2477       pIdx->tnum = iTo;
2478     }
2479   }
2480 }
2481 #endif
2482 
2483 /*
2484 ** Write code to erase the table with root-page iTable from database iDb.
2485 ** Also write code to modify the sqlite_master table and internal schema
2486 ** if a root-page of another table is moved by the btree-layer whilst
2487 ** erasing iTable (this can happen with an auto-vacuum database).
2488 */
2489 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
2490   Vdbe *v = sqlite3GetVdbe(pParse);
2491   int r1 = sqlite3GetTempReg(pParse);
2492   if( iTable<2 ) sqlite3ErrorMsg(pParse, "corrupt schema");
2493   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
2494   sqlite3MayAbort(pParse);
2495 #ifndef SQLITE_OMIT_AUTOVACUUM
2496   /* OP_Destroy stores an in integer r1. If this integer
2497   ** is non-zero, then it is the root page number of a table moved to
2498   ** location iTable. The following code modifies the sqlite_master table to
2499   ** reflect this.
2500   **
2501   ** The "#NNN" in the SQL is a special constant that means whatever value
2502   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
2503   ** token for additional information.
2504   */
2505   sqlite3NestedParse(pParse,
2506      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
2507      pParse->db->aDb[iDb].zDbSName, MASTER_NAME, iTable, r1, r1);
2508 #endif
2509   sqlite3ReleaseTempReg(pParse, r1);
2510 }
2511 
2512 /*
2513 ** Write VDBE code to erase table pTab and all associated indices on disk.
2514 ** Code to update the sqlite_master tables and internal schema definitions
2515 ** in case a root-page belonging to another table is moved by the btree layer
2516 ** is also added (this can happen with an auto-vacuum database).
2517 */
2518 static void destroyTable(Parse *pParse, Table *pTab){
2519   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
2520   ** is not defined), then it is important to call OP_Destroy on the
2521   ** table and index root-pages in order, starting with the numerically
2522   ** largest root-page number. This guarantees that none of the root-pages
2523   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
2524   ** following were coded:
2525   **
2526   ** OP_Destroy 4 0
2527   ** ...
2528   ** OP_Destroy 5 0
2529   **
2530   ** and root page 5 happened to be the largest root-page number in the
2531   ** database, then root page 5 would be moved to page 4 by the
2532   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
2533   ** a free-list page.
2534   */
2535   int iTab = pTab->tnum;
2536   int iDestroyed = 0;
2537 
2538   while( 1 ){
2539     Index *pIdx;
2540     int iLargest = 0;
2541 
2542     if( iDestroyed==0 || iTab<iDestroyed ){
2543       iLargest = iTab;
2544     }
2545     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2546       int iIdx = pIdx->tnum;
2547       assert( pIdx->pSchema==pTab->pSchema );
2548       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
2549         iLargest = iIdx;
2550       }
2551     }
2552     if( iLargest==0 ){
2553       return;
2554     }else{
2555       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
2556       assert( iDb>=0 && iDb<pParse->db->nDb );
2557       destroyRootPage(pParse, iLargest, iDb);
2558       iDestroyed = iLargest;
2559     }
2560   }
2561 }
2562 
2563 /*
2564 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
2565 ** after a DROP INDEX or DROP TABLE command.
2566 */
2567 static void sqlite3ClearStatTables(
2568   Parse *pParse,         /* The parsing context */
2569   int iDb,               /* The database number */
2570   const char *zType,     /* "idx" or "tbl" */
2571   const char *zName      /* Name of index or table */
2572 ){
2573   int i;
2574   const char *zDbName = pParse->db->aDb[iDb].zDbSName;
2575   for(i=1; i<=4; i++){
2576     char zTab[24];
2577     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
2578     if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
2579       sqlite3NestedParse(pParse,
2580         "DELETE FROM %Q.%s WHERE %s=%Q",
2581         zDbName, zTab, zType, zName
2582       );
2583     }
2584   }
2585 }
2586 
2587 /*
2588 ** Generate code to drop a table.
2589 */
2590 void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
2591   Vdbe *v;
2592   sqlite3 *db = pParse->db;
2593   Trigger *pTrigger;
2594   Db *pDb = &db->aDb[iDb];
2595 
2596   v = sqlite3GetVdbe(pParse);
2597   assert( v!=0 );
2598   sqlite3BeginWriteOperation(pParse, 1, iDb);
2599 
2600 #ifndef SQLITE_OMIT_VIRTUALTABLE
2601   if( IsVirtual(pTab) ){
2602     sqlite3VdbeAddOp0(v, OP_VBegin);
2603   }
2604 #endif
2605 
2606   /* Drop all triggers associated with the table being dropped. Code
2607   ** is generated to remove entries from sqlite_master and/or
2608   ** sqlite_temp_master if required.
2609   */
2610   pTrigger = sqlite3TriggerList(pParse, pTab);
2611   while( pTrigger ){
2612     assert( pTrigger->pSchema==pTab->pSchema ||
2613         pTrigger->pSchema==db->aDb[1].pSchema );
2614     sqlite3DropTriggerPtr(pParse, pTrigger);
2615     pTrigger = pTrigger->pNext;
2616   }
2617 
2618 #ifndef SQLITE_OMIT_AUTOINCREMENT
2619   /* Remove any entries of the sqlite_sequence table associated with
2620   ** the table being dropped. This is done before the table is dropped
2621   ** at the btree level, in case the sqlite_sequence table needs to
2622   ** move as a result of the drop (can happen in auto-vacuum mode).
2623   */
2624   if( pTab->tabFlags & TF_Autoincrement ){
2625     sqlite3NestedParse(pParse,
2626       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
2627       pDb->zDbSName, pTab->zName
2628     );
2629   }
2630 #endif
2631 
2632   /* Drop all SQLITE_MASTER table and index entries that refer to the
2633   ** table. The program name loops through the master table and deletes
2634   ** every row that refers to a table of the same name as the one being
2635   ** dropped. Triggers are handled separately because a trigger can be
2636   ** created in the temp database that refers to a table in another
2637   ** database.
2638   */
2639   sqlite3NestedParse(pParse,
2640       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
2641       pDb->zDbSName, MASTER_NAME, pTab->zName);
2642   if( !isView && !IsVirtual(pTab) ){
2643     destroyTable(pParse, pTab);
2644   }
2645 
2646   /* Remove the table entry from SQLite's internal schema and modify
2647   ** the schema cookie.
2648   */
2649   if( IsVirtual(pTab) ){
2650     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
2651     sqlite3MayAbort(pParse);
2652   }
2653   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
2654   sqlite3ChangeCookie(pParse, iDb);
2655   sqliteViewResetAll(db, iDb);
2656 }
2657 
2658 /*
2659 ** This routine is called to do the work of a DROP TABLE statement.
2660 ** pName is the name of the table to be dropped.
2661 */
2662 void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
2663   Table *pTab;
2664   Vdbe *v;
2665   sqlite3 *db = pParse->db;
2666   int iDb;
2667 
2668   if( db->mallocFailed ){
2669     goto exit_drop_table;
2670   }
2671   assert( pParse->nErr==0 );
2672   assert( pName->nSrc==1 );
2673   if( sqlite3ReadSchema(pParse) ) goto exit_drop_table;
2674   if( noErr ) db->suppressErr++;
2675   assert( isView==0 || isView==LOCATE_VIEW );
2676   pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
2677   if( noErr ) db->suppressErr--;
2678 
2679   if( pTab==0 ){
2680     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
2681     goto exit_drop_table;
2682   }
2683   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
2684   assert( iDb>=0 && iDb<db->nDb );
2685 
2686   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
2687   ** it is initialized.
2688   */
2689   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
2690     goto exit_drop_table;
2691   }
2692 #ifndef SQLITE_OMIT_AUTHORIZATION
2693   {
2694     int code;
2695     const char *zTab = SCHEMA_TABLE(iDb);
2696     const char *zDb = db->aDb[iDb].zDbSName;
2697     const char *zArg2 = 0;
2698     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
2699       goto exit_drop_table;
2700     }
2701     if( isView ){
2702       if( !OMIT_TEMPDB && iDb==1 ){
2703         code = SQLITE_DROP_TEMP_VIEW;
2704       }else{
2705         code = SQLITE_DROP_VIEW;
2706       }
2707 #ifndef SQLITE_OMIT_VIRTUALTABLE
2708     }else if( IsVirtual(pTab) ){
2709       code = SQLITE_DROP_VTABLE;
2710       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
2711 #endif
2712     }else{
2713       if( !OMIT_TEMPDB && iDb==1 ){
2714         code = SQLITE_DROP_TEMP_TABLE;
2715       }else{
2716         code = SQLITE_DROP_TABLE;
2717       }
2718     }
2719     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
2720       goto exit_drop_table;
2721     }
2722     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
2723       goto exit_drop_table;
2724     }
2725   }
2726 #endif
2727   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
2728     && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
2729     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
2730     goto exit_drop_table;
2731   }
2732 
2733 #ifndef SQLITE_OMIT_VIEW
2734   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
2735   ** on a table.
2736   */
2737   if( isView && pTab->pSelect==0 ){
2738     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
2739     goto exit_drop_table;
2740   }
2741   if( !isView && pTab->pSelect ){
2742     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
2743     goto exit_drop_table;
2744   }
2745 #endif
2746 
2747   /* Generate code to remove the table from the master table
2748   ** on disk.
2749   */
2750   v = sqlite3GetVdbe(pParse);
2751   if( v ){
2752     sqlite3BeginWriteOperation(pParse, 1, iDb);
2753     if( !isView ){
2754       sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
2755       sqlite3FkDropTable(pParse, pName, pTab);
2756     }
2757     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
2758   }
2759 
2760 exit_drop_table:
2761   sqlite3SrcListDelete(db, pName);
2762 }
2763 
2764 /*
2765 ** This routine is called to create a new foreign key on the table
2766 ** currently under construction.  pFromCol determines which columns
2767 ** in the current table point to the foreign key.  If pFromCol==0 then
2768 ** connect the key to the last column inserted.  pTo is the name of
2769 ** the table referred to (a.k.a the "parent" table).  pToCol is a list
2770 ** of tables in the parent pTo table.  flags contains all
2771 ** information about the conflict resolution algorithms specified
2772 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
2773 **
2774 ** An FKey structure is created and added to the table currently
2775 ** under construction in the pParse->pNewTable field.
2776 **
2777 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
2778 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
2779 */
2780 void sqlite3CreateForeignKey(
2781   Parse *pParse,       /* Parsing context */
2782   ExprList *pFromCol,  /* Columns in this table that point to other table */
2783   Token *pTo,          /* Name of the other table */
2784   ExprList *pToCol,    /* Columns in the other table */
2785   int flags            /* Conflict resolution algorithms. */
2786 ){
2787   sqlite3 *db = pParse->db;
2788 #ifndef SQLITE_OMIT_FOREIGN_KEY
2789   FKey *pFKey = 0;
2790   FKey *pNextTo;
2791   Table *p = pParse->pNewTable;
2792   int nByte;
2793   int i;
2794   int nCol;
2795   char *z;
2796 
2797   assert( pTo!=0 );
2798   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
2799   if( pFromCol==0 ){
2800     int iCol = p->nCol-1;
2801     if( NEVER(iCol<0) ) goto fk_end;
2802     if( pToCol && pToCol->nExpr!=1 ){
2803       sqlite3ErrorMsg(pParse, "foreign key on %s"
2804          " should reference only one column of table %T",
2805          p->aCol[iCol].zName, pTo);
2806       goto fk_end;
2807     }
2808     nCol = 1;
2809   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
2810     sqlite3ErrorMsg(pParse,
2811         "number of columns in foreign key does not match the number of "
2812         "columns in the referenced table");
2813     goto fk_end;
2814   }else{
2815     nCol = pFromCol->nExpr;
2816   }
2817   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
2818   if( pToCol ){
2819     for(i=0; i<pToCol->nExpr; i++){
2820       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
2821     }
2822   }
2823   pFKey = sqlite3DbMallocZero(db, nByte );
2824   if( pFKey==0 ){
2825     goto fk_end;
2826   }
2827   pFKey->pFrom = p;
2828   pFKey->pNextFrom = p->pFKey;
2829   z = (char*)&pFKey->aCol[nCol];
2830   pFKey->zTo = z;
2831   if( IN_RENAME_OBJECT ){
2832     sqlite3RenameTokenMap(pParse, (void*)z, pTo);
2833   }
2834   memcpy(z, pTo->z, pTo->n);
2835   z[pTo->n] = 0;
2836   sqlite3Dequote(z);
2837   z += pTo->n+1;
2838   pFKey->nCol = nCol;
2839   if( pFromCol==0 ){
2840     pFKey->aCol[0].iFrom = p->nCol-1;
2841   }else{
2842     for(i=0; i<nCol; i++){
2843       int j;
2844       for(j=0; j<p->nCol; j++){
2845         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
2846           pFKey->aCol[i].iFrom = j;
2847           break;
2848         }
2849       }
2850       if( j>=p->nCol ){
2851         sqlite3ErrorMsg(pParse,
2852           "unknown column \"%s\" in foreign key definition",
2853           pFromCol->a[i].zName);
2854         goto fk_end;
2855       }
2856       if( IN_RENAME_OBJECT ){
2857         sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zName);
2858       }
2859     }
2860   }
2861   if( pToCol ){
2862     for(i=0; i<nCol; i++){
2863       int n = sqlite3Strlen30(pToCol->a[i].zName);
2864       pFKey->aCol[i].zCol = z;
2865       if( IN_RENAME_OBJECT ){
2866         sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zName);
2867       }
2868       memcpy(z, pToCol->a[i].zName, n);
2869       z[n] = 0;
2870       z += n+1;
2871     }
2872   }
2873   pFKey->isDeferred = 0;
2874   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
2875   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
2876 
2877   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
2878   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
2879       pFKey->zTo, (void *)pFKey
2880   );
2881   if( pNextTo==pFKey ){
2882     sqlite3OomFault(db);
2883     goto fk_end;
2884   }
2885   if( pNextTo ){
2886     assert( pNextTo->pPrevTo==0 );
2887     pFKey->pNextTo = pNextTo;
2888     pNextTo->pPrevTo = pFKey;
2889   }
2890 
2891   /* Link the foreign key to the table as the last step.
2892   */
2893   p->pFKey = pFKey;
2894   pFKey = 0;
2895 
2896 fk_end:
2897   sqlite3DbFree(db, pFKey);
2898 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
2899   sqlite3ExprListDelete(db, pFromCol);
2900   sqlite3ExprListDelete(db, pToCol);
2901 }
2902 
2903 /*
2904 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
2905 ** clause is seen as part of a foreign key definition.  The isDeferred
2906 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
2907 ** The behavior of the most recently created foreign key is adjusted
2908 ** accordingly.
2909 */
2910 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
2911 #ifndef SQLITE_OMIT_FOREIGN_KEY
2912   Table *pTab;
2913   FKey *pFKey;
2914   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
2915   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
2916   pFKey->isDeferred = (u8)isDeferred;
2917 #endif
2918 }
2919 
2920 /*
2921 ** Generate code that will erase and refill index *pIdx.  This is
2922 ** used to initialize a newly created index or to recompute the
2923 ** content of an index in response to a REINDEX command.
2924 **
2925 ** if memRootPage is not negative, it means that the index is newly
2926 ** created.  The register specified by memRootPage contains the
2927 ** root page number of the index.  If memRootPage is negative, then
2928 ** the index already exists and must be cleared before being refilled and
2929 ** the root page number of the index is taken from pIndex->tnum.
2930 */
2931 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
2932   Table *pTab = pIndex->pTable;  /* The table that is indexed */
2933   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
2934   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
2935   int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
2936   int addr1;                     /* Address of top of loop */
2937   int addr2;                     /* Address to jump to for next iteration */
2938   int tnum;                      /* Root page of index */
2939   int iPartIdxLabel;             /* Jump to this label to skip a row */
2940   Vdbe *v;                       /* Generate code into this virtual machine */
2941   KeyInfo *pKey;                 /* KeyInfo for index */
2942   int regRecord;                 /* Register holding assembled index record */
2943   sqlite3 *db = pParse->db;      /* The database connection */
2944   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
2945 
2946 #ifndef SQLITE_OMIT_AUTHORIZATION
2947   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
2948       db->aDb[iDb].zDbSName ) ){
2949     return;
2950   }
2951 #endif
2952 
2953   /* Require a write-lock on the table to perform this operation */
2954   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
2955 
2956   v = sqlite3GetVdbe(pParse);
2957   if( v==0 ) return;
2958   if( memRootPage>=0 ){
2959     tnum = memRootPage;
2960   }else{
2961     tnum = pIndex->tnum;
2962   }
2963   pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
2964   assert( pKey!=0 || db->mallocFailed || pParse->nErr );
2965 
2966   /* Open the sorter cursor if we are to use one. */
2967   iSorter = pParse->nTab++;
2968   sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
2969                     sqlite3KeyInfoRef(pKey), P4_KEYINFO);
2970 
2971   /* Open the table. Loop through all rows of the table, inserting index
2972   ** records into the sorter. */
2973   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
2974   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
2975   regRecord = sqlite3GetTempReg(pParse);
2976   sqlite3MultiWrite(pParse);
2977 
2978   sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
2979   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
2980   sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
2981   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
2982   sqlite3VdbeJumpHere(v, addr1);
2983   if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
2984   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
2985                     (char *)pKey, P4_KEYINFO);
2986   sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
2987 
2988   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
2989   if( IsUniqueIndex(pIndex) ){
2990     int j2 = sqlite3VdbeGoto(v, 1);
2991     addr2 = sqlite3VdbeCurrentAddr(v);
2992     sqlite3VdbeVerifyAbortable(v, OE_Abort);
2993     sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
2994                          pIndex->nKeyCol); VdbeCoverage(v);
2995     sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
2996     sqlite3VdbeJumpHere(v, j2);
2997   }else{
2998     addr2 = sqlite3VdbeCurrentAddr(v);
2999   }
3000   sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
3001   sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
3002   sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
3003   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
3004   sqlite3ReleaseTempReg(pParse, regRecord);
3005   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
3006   sqlite3VdbeJumpHere(v, addr1);
3007 
3008   sqlite3VdbeAddOp1(v, OP_Close, iTab);
3009   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
3010   sqlite3VdbeAddOp1(v, OP_Close, iSorter);
3011 }
3012 
3013 /*
3014 ** Allocate heap space to hold an Index object with nCol columns.
3015 **
3016 ** Increase the allocation size to provide an extra nExtra bytes
3017 ** of 8-byte aligned space after the Index object and return a
3018 ** pointer to this extra space in *ppExtra.
3019 */
3020 Index *sqlite3AllocateIndexObject(
3021   sqlite3 *db,         /* Database connection */
3022   i16 nCol,            /* Total number of columns in the index */
3023   int nExtra,          /* Number of bytes of extra space to alloc */
3024   char **ppExtra       /* Pointer to the "extra" space */
3025 ){
3026   Index *p;            /* Allocated index object */
3027   int nByte;           /* Bytes of space for Index object + arrays */
3028 
3029   nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
3030           ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
3031           ROUND8(sizeof(LogEst)*(nCol+1) +     /* Index.aiRowLogEst   */
3032                  sizeof(i16)*nCol +            /* Index.aiColumn   */
3033                  sizeof(u8)*nCol);             /* Index.aSortOrder */
3034   p = sqlite3DbMallocZero(db, nByte + nExtra);
3035   if( p ){
3036     char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
3037     p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
3038     p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
3039     p->aiColumn = (i16*)pExtra;       pExtra += sizeof(i16)*nCol;
3040     p->aSortOrder = (u8*)pExtra;
3041     p->nColumn = nCol;
3042     p->nKeyCol = nCol - 1;
3043     *ppExtra = ((char*)p) + nByte;
3044   }
3045   return p;
3046 }
3047 
3048 /*
3049 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index
3050 ** and pTblList is the name of the table that is to be indexed.  Both will
3051 ** be NULL for a primary key or an index that is created to satisfy a
3052 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
3053 ** as the table to be indexed.  pParse->pNewTable is a table that is
3054 ** currently being constructed by a CREATE TABLE statement.
3055 **
3056 ** pList is a list of columns to be indexed.  pList will be NULL if this
3057 ** is a primary key or unique-constraint on the most recent column added
3058 ** to the table currently under construction.
3059 */
3060 void sqlite3CreateIndex(
3061   Parse *pParse,     /* All information about this parse */
3062   Token *pName1,     /* First part of index name. May be NULL */
3063   Token *pName2,     /* Second part of index name. May be NULL */
3064   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
3065   ExprList *pList,   /* A list of columns to be indexed */
3066   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
3067   Token *pStart,     /* The CREATE token that begins this statement */
3068   Expr *pPIWhere,    /* WHERE clause for partial indices */
3069   int sortOrder,     /* Sort order of primary key when pList==NULL */
3070   int ifNotExist,    /* Omit error if index already exists */
3071   u8 idxType         /* The index type */
3072 ){
3073   Table *pTab = 0;     /* Table to be indexed */
3074   Index *pIndex = 0;   /* The index to be created */
3075   char *zName = 0;     /* Name of the index */
3076   int nName;           /* Number of characters in zName */
3077   int i, j;
3078   DbFixer sFix;        /* For assigning database names to pTable */
3079   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
3080   sqlite3 *db = pParse->db;
3081   Db *pDb;             /* The specific table containing the indexed database */
3082   int iDb;             /* Index of the database that is being written */
3083   Token *pName = 0;    /* Unqualified name of the index to create */
3084   struct ExprList_item *pListItem; /* For looping over pList */
3085   int nExtra = 0;                  /* Space allocated for zExtra[] */
3086   int nExtraCol;                   /* Number of extra columns needed */
3087   char *zExtra = 0;                /* Extra space after the Index object */
3088   Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
3089 
3090   if( db->mallocFailed || pParse->nErr>0 ){
3091     goto exit_create_index;
3092   }
3093   if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
3094     goto exit_create_index;
3095   }
3096   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
3097     goto exit_create_index;
3098   }
3099 
3100   /*
3101   ** Find the table that is to be indexed.  Return early if not found.
3102   */
3103   if( pTblName!=0 ){
3104 
3105     /* Use the two-part index name to determine the database
3106     ** to search for the table. 'Fix' the table name to this db
3107     ** before looking up the table.
3108     */
3109     assert( pName1 && pName2 );
3110     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
3111     if( iDb<0 ) goto exit_create_index;
3112     assert( pName && pName->z );
3113 
3114 #ifndef SQLITE_OMIT_TEMPDB
3115     /* If the index name was unqualified, check if the table
3116     ** is a temp table. If so, set the database to 1. Do not do this
3117     ** if initialising a database schema.
3118     */
3119     if( !db->init.busy ){
3120       pTab = sqlite3SrcListLookup(pParse, pTblName);
3121       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
3122         iDb = 1;
3123       }
3124     }
3125 #endif
3126 
3127     sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
3128     if( sqlite3FixSrcList(&sFix, pTblName) ){
3129       /* Because the parser constructs pTblName from a single identifier,
3130       ** sqlite3FixSrcList can never fail. */
3131       assert(0);
3132     }
3133     pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
3134     assert( db->mallocFailed==0 || pTab==0 );
3135     if( pTab==0 ) goto exit_create_index;
3136     if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
3137       sqlite3ErrorMsg(pParse,
3138            "cannot create a TEMP index on non-TEMP table \"%s\"",
3139            pTab->zName);
3140       goto exit_create_index;
3141     }
3142     if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
3143   }else{
3144     assert( pName==0 );
3145     assert( pStart==0 );
3146     pTab = pParse->pNewTable;
3147     if( !pTab ) goto exit_create_index;
3148     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
3149   }
3150   pDb = &db->aDb[iDb];
3151 
3152   assert( pTab!=0 );
3153   assert( pParse->nErr==0 );
3154   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
3155        && db->init.busy==0
3156 #if SQLITE_USER_AUTHENTICATION
3157        && sqlite3UserAuthTable(pTab->zName)==0
3158 #endif
3159 #ifdef SQLITE_ALLOW_SQLITE_MASTER_INDEX
3160        && sqlite3StrICmp(&pTab->zName[7],"master")!=0
3161 #endif
3162        && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0
3163  ){
3164     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
3165     goto exit_create_index;
3166   }
3167 #ifndef SQLITE_OMIT_VIEW
3168   if( pTab->pSelect ){
3169     sqlite3ErrorMsg(pParse, "views may not be indexed");
3170     goto exit_create_index;
3171   }
3172 #endif
3173 #ifndef SQLITE_OMIT_VIRTUALTABLE
3174   if( IsVirtual(pTab) ){
3175     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
3176     goto exit_create_index;
3177   }
3178 #endif
3179 
3180   /*
3181   ** Find the name of the index.  Make sure there is not already another
3182   ** index or table with the same name.
3183   **
3184   ** Exception:  If we are reading the names of permanent indices from the
3185   ** sqlite_master table (because some other process changed the schema) and
3186   ** one of the index names collides with the name of a temporary table or
3187   ** index, then we will continue to process this index.
3188   **
3189   ** If pName==0 it means that we are
3190   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
3191   ** own name.
3192   */
3193   if( pName ){
3194     zName = sqlite3NameFromToken(db, pName);
3195     if( zName==0 ) goto exit_create_index;
3196     assert( pName->z!=0 );
3197     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
3198       goto exit_create_index;
3199     }
3200     if( !IN_RENAME_OBJECT ){
3201       if( !db->init.busy ){
3202         if( sqlite3FindTable(db, zName, 0)!=0 ){
3203           sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
3204           goto exit_create_index;
3205         }
3206       }
3207       if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
3208         if( !ifNotExist ){
3209           sqlite3ErrorMsg(pParse, "index %s already exists", zName);
3210         }else{
3211           assert( !db->init.busy );
3212           sqlite3CodeVerifySchema(pParse, iDb);
3213         }
3214         goto exit_create_index;
3215       }
3216     }
3217   }else{
3218     int n;
3219     Index *pLoop;
3220     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
3221     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
3222     if( zName==0 ){
3223       goto exit_create_index;
3224     }
3225 
3226     /* Automatic index names generated from within sqlite3_declare_vtab()
3227     ** must have names that are distinct from normal automatic index names.
3228     ** The following statement converts "sqlite3_autoindex..." into
3229     ** "sqlite3_butoindex..." in order to make the names distinct.
3230     ** The "vtab_err.test" test demonstrates the need of this statement. */
3231     if( IN_SPECIAL_PARSE ) zName[7]++;
3232   }
3233 
3234   /* Check for authorization to create an index.
3235   */
3236 #ifndef SQLITE_OMIT_AUTHORIZATION
3237   if( !IN_RENAME_OBJECT ){
3238     const char *zDb = pDb->zDbSName;
3239     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
3240       goto exit_create_index;
3241     }
3242     i = SQLITE_CREATE_INDEX;
3243     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
3244     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
3245       goto exit_create_index;
3246     }
3247   }
3248 #endif
3249 
3250   /* If pList==0, it means this routine was called to make a primary
3251   ** key out of the last column added to the table under construction.
3252   ** So create a fake list to simulate this.
3253   */
3254   if( pList==0 ){
3255     Token prevCol;
3256     Column *pCol = &pTab->aCol[pTab->nCol-1];
3257     pCol->colFlags |= COLFLAG_UNIQUE;
3258     sqlite3TokenInit(&prevCol, pCol->zName);
3259     pList = sqlite3ExprListAppend(pParse, 0,
3260               sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
3261     if( pList==0 ) goto exit_create_index;
3262     assert( pList->nExpr==1 );
3263     sqlite3ExprListSetSortOrder(pList, sortOrder);
3264   }else{
3265     sqlite3ExprListCheckLength(pParse, pList, "index");
3266   }
3267 
3268   /* Figure out how many bytes of space are required to store explicitly
3269   ** specified collation sequence names.
3270   */
3271   for(i=0; i<pList->nExpr; i++){
3272     Expr *pExpr = pList->a[i].pExpr;
3273     assert( pExpr!=0 );
3274     if( pExpr->op==TK_COLLATE ){
3275       nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
3276     }
3277   }
3278 
3279   /*
3280   ** Allocate the index structure.
3281   */
3282   nName = sqlite3Strlen30(zName);
3283   nExtraCol = pPk ? pPk->nKeyCol : 1;
3284   pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
3285                                       nName + nExtra + 1, &zExtra);
3286   if( db->mallocFailed ){
3287     goto exit_create_index;
3288   }
3289   assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
3290   assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
3291   pIndex->zName = zExtra;
3292   zExtra += nName + 1;
3293   memcpy(pIndex->zName, zName, nName+1);
3294   pIndex->pTable = pTab;
3295   pIndex->onError = (u8)onError;
3296   pIndex->uniqNotNull = onError!=OE_None;
3297   pIndex->idxType = idxType;
3298   pIndex->pSchema = db->aDb[iDb].pSchema;
3299   pIndex->nKeyCol = pList->nExpr;
3300   if( pPIWhere ){
3301     sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
3302     pIndex->pPartIdxWhere = pPIWhere;
3303     pPIWhere = 0;
3304   }
3305   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3306 
3307   /* Check to see if we should honor DESC requests on index columns
3308   */
3309   if( pDb->pSchema->file_format>=4 ){
3310     sortOrderMask = -1;   /* Honor DESC */
3311   }else{
3312     sortOrderMask = 0;    /* Ignore DESC */
3313   }
3314 
3315   /* Analyze the list of expressions that form the terms of the index and
3316   ** report any errors.  In the common case where the expression is exactly
3317   ** a table column, store that column in aiColumn[].  For general expressions,
3318   ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
3319   **
3320   ** TODO: Issue a warning if two or more columns of the index are identical.
3321   ** TODO: Issue a warning if the table primary key is used as part of the
3322   ** index key.
3323   */
3324   pListItem = pList->a;
3325   if( IN_RENAME_OBJECT ){
3326     pIndex->aColExpr = pList;
3327     pList = 0;
3328   }
3329   for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
3330     Expr *pCExpr;                  /* The i-th index expression */
3331     int requestedSortOrder;        /* ASC or DESC on the i-th expression */
3332     const char *zColl;             /* Collation sequence name */
3333 
3334     sqlite3StringToId(pListItem->pExpr);
3335     sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
3336     if( pParse->nErr ) goto exit_create_index;
3337     pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
3338     if( pCExpr->op!=TK_COLUMN ){
3339       if( pTab==pParse->pNewTable ){
3340         sqlite3ErrorMsg(pParse, "expressions prohibited in PRIMARY KEY and "
3341                                 "UNIQUE constraints");
3342         goto exit_create_index;
3343       }
3344       if( pIndex->aColExpr==0 ){
3345         pIndex->aColExpr = pList;
3346         pList = 0;
3347       }
3348       j = XN_EXPR;
3349       pIndex->aiColumn[i] = XN_EXPR;
3350       pIndex->uniqNotNull = 0;
3351     }else{
3352       j = pCExpr->iColumn;
3353       assert( j<=0x7fff );
3354       if( j<0 ){
3355         j = pTab->iPKey;
3356       }else if( pTab->aCol[j].notNull==0 ){
3357         pIndex->uniqNotNull = 0;
3358       }
3359       pIndex->aiColumn[i] = (i16)j;
3360     }
3361     zColl = 0;
3362     if( pListItem->pExpr->op==TK_COLLATE ){
3363       int nColl;
3364       zColl = pListItem->pExpr->u.zToken;
3365       nColl = sqlite3Strlen30(zColl) + 1;
3366       assert( nExtra>=nColl );
3367       memcpy(zExtra, zColl, nColl);
3368       zColl = zExtra;
3369       zExtra += nColl;
3370       nExtra -= nColl;
3371     }else if( j>=0 ){
3372       zColl = pTab->aCol[j].zColl;
3373     }
3374     if( !zColl ) zColl = sqlite3StrBINARY;
3375     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
3376       goto exit_create_index;
3377     }
3378     pIndex->azColl[i] = zColl;
3379     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
3380     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
3381   }
3382 
3383   /* Append the table key to the end of the index.  For WITHOUT ROWID
3384   ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
3385   ** normal tables (when pPk==0) this will be the rowid.
3386   */
3387   if( pPk ){
3388     for(j=0; j<pPk->nKeyCol; j++){
3389       int x = pPk->aiColumn[j];
3390       assert( x>=0 );
3391       if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
3392         pIndex->nColumn--;
3393       }else{
3394         pIndex->aiColumn[i] = x;
3395         pIndex->azColl[i] = pPk->azColl[j];
3396         pIndex->aSortOrder[i] = pPk->aSortOrder[j];
3397         i++;
3398       }
3399     }
3400     assert( i==pIndex->nColumn );
3401   }else{
3402     pIndex->aiColumn[i] = XN_ROWID;
3403     pIndex->azColl[i] = sqlite3StrBINARY;
3404   }
3405   sqlite3DefaultRowEst(pIndex);
3406   if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
3407 
3408   /* If this index contains every column of its table, then mark
3409   ** it as a covering index */
3410   assert( HasRowid(pTab)
3411       || pTab->iPKey<0 || sqlite3ColumnOfIndex(pIndex, pTab->iPKey)>=0 );
3412   recomputeColumnsNotIndexed(pIndex);
3413   if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
3414     pIndex->isCovering = 1;
3415     for(j=0; j<pTab->nCol; j++){
3416       if( j==pTab->iPKey ) continue;
3417       if( sqlite3ColumnOfIndex(pIndex,j)>=0 ) continue;
3418       pIndex->isCovering = 0;
3419       break;
3420     }
3421   }
3422 
3423   if( pTab==pParse->pNewTable ){
3424     /* This routine has been called to create an automatic index as a
3425     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
3426     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
3427     ** i.e. one of:
3428     **
3429     ** CREATE TABLE t(x PRIMARY KEY, y);
3430     ** CREATE TABLE t(x, y, UNIQUE(x, y));
3431     **
3432     ** Either way, check to see if the table already has such an index. If
3433     ** so, don't bother creating this one. This only applies to
3434     ** automatically created indices. Users can do as they wish with
3435     ** explicit indices.
3436     **
3437     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
3438     ** (and thus suppressing the second one) even if they have different
3439     ** sort orders.
3440     **
3441     ** If there are different collating sequences or if the columns of
3442     ** the constraint occur in different orders, then the constraints are
3443     ** considered distinct and both result in separate indices.
3444     */
3445     Index *pIdx;
3446     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
3447       int k;
3448       assert( IsUniqueIndex(pIdx) );
3449       assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
3450       assert( IsUniqueIndex(pIndex) );
3451 
3452       if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
3453       for(k=0; k<pIdx->nKeyCol; k++){
3454         const char *z1;
3455         const char *z2;
3456         assert( pIdx->aiColumn[k]>=0 );
3457         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
3458         z1 = pIdx->azColl[k];
3459         z2 = pIndex->azColl[k];
3460         if( sqlite3StrICmp(z1, z2) ) break;
3461       }
3462       if( k==pIdx->nKeyCol ){
3463         if( pIdx->onError!=pIndex->onError ){
3464           /* This constraint creates the same index as a previous
3465           ** constraint specified somewhere in the CREATE TABLE statement.
3466           ** However the ON CONFLICT clauses are different. If both this
3467           ** constraint and the previous equivalent constraint have explicit
3468           ** ON CONFLICT clauses this is an error. Otherwise, use the
3469           ** explicitly specified behavior for the index.
3470           */
3471           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
3472             sqlite3ErrorMsg(pParse,
3473                 "conflicting ON CONFLICT clauses specified", 0);
3474           }
3475           if( pIdx->onError==OE_Default ){
3476             pIdx->onError = pIndex->onError;
3477           }
3478         }
3479         if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
3480         if( IN_RENAME_OBJECT ){
3481           pIndex->pNext = pParse->pNewIndex;
3482           pParse->pNewIndex = pIndex;
3483           pIndex = 0;
3484         }
3485         goto exit_create_index;
3486       }
3487     }
3488   }
3489 
3490   if( !IN_RENAME_OBJECT ){
3491 
3492     /* Link the new Index structure to its table and to the other
3493     ** in-memory database structures.
3494     */
3495     assert( pParse->nErr==0 );
3496     if( db->init.busy ){
3497       Index *p;
3498       assert( !IN_SPECIAL_PARSE );
3499       assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
3500       if( pTblName!=0 ){
3501         pIndex->tnum = db->init.newTnum;
3502         if( sqlite3IndexHasDuplicateRootPage(pIndex) ){
3503           sqlite3ErrorMsg(pParse, "invalid rootpage");
3504           pParse->rc = SQLITE_CORRUPT_BKPT;
3505           goto exit_create_index;
3506         }
3507       }
3508       p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
3509           pIndex->zName, pIndex);
3510       if( p ){
3511         assert( p==pIndex );  /* Malloc must have failed */
3512         sqlite3OomFault(db);
3513         goto exit_create_index;
3514       }
3515       db->mDbFlags |= DBFLAG_SchemaChange;
3516     }
3517 
3518     /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
3519     ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
3520     ** emit code to allocate the index rootpage on disk and make an entry for
3521     ** the index in the sqlite_master table and populate the index with
3522     ** content.  But, do not do this if we are simply reading the sqlite_master
3523     ** table to parse the schema, or if this index is the PRIMARY KEY index
3524     ** of a WITHOUT ROWID table.
3525     **
3526     ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
3527     ** or UNIQUE index in a CREATE TABLE statement.  Since the table
3528     ** has just been created, it contains no data and the index initialization
3529     ** step can be skipped.
3530     */
3531     else if( HasRowid(pTab) || pTblName!=0 ){
3532       Vdbe *v;
3533       char *zStmt;
3534       int iMem = ++pParse->nMem;
3535 
3536       v = sqlite3GetVdbe(pParse);
3537       if( v==0 ) goto exit_create_index;
3538 
3539       sqlite3BeginWriteOperation(pParse, 1, iDb);
3540 
3541       /* Create the rootpage for the index using CreateIndex. But before
3542       ** doing so, code a Noop instruction and store its address in
3543       ** Index.tnum. This is required in case this index is actually a
3544       ** PRIMARY KEY and the table is actually a WITHOUT ROWID table. In
3545       ** that case the convertToWithoutRowidTable() routine will replace
3546       ** the Noop with a Goto to jump over the VDBE code generated below. */
3547       pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
3548       sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
3549 
3550       /* Gather the complete text of the CREATE INDEX statement into
3551       ** the zStmt variable
3552       */
3553       if( pStart ){
3554         int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
3555         if( pName->z[n-1]==';' ) n--;
3556         /* A named index with an explicit CREATE INDEX statement */
3557         zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
3558             onError==OE_None ? "" : " UNIQUE", n, pName->z);
3559       }else{
3560         /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
3561         /* zStmt = sqlite3MPrintf(""); */
3562         zStmt = 0;
3563       }
3564 
3565       /* Add an entry in sqlite_master for this index
3566       */
3567       sqlite3NestedParse(pParse,
3568           "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
3569           db->aDb[iDb].zDbSName, MASTER_NAME,
3570           pIndex->zName,
3571           pTab->zName,
3572           iMem,
3573           zStmt
3574           );
3575       sqlite3DbFree(db, zStmt);
3576 
3577       /* Fill the index with data and reparse the schema. Code an OP_Expire
3578       ** to invalidate all pre-compiled statements.
3579       */
3580       if( pTblName ){
3581         sqlite3RefillIndex(pParse, pIndex, iMem);
3582         sqlite3ChangeCookie(pParse, iDb);
3583         sqlite3VdbeAddParseSchemaOp(v, iDb,
3584             sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
3585         sqlite3VdbeAddOp2(v, OP_Expire, 0, 1);
3586       }
3587 
3588       sqlite3VdbeJumpHere(v, pIndex->tnum);
3589     }
3590   }
3591 
3592   /* When adding an index to the list of indices for a table, make
3593   ** sure all indices labeled OE_Replace come after all those labeled
3594   ** OE_Ignore.  This is necessary for the correct constraint check
3595   ** processing (in sqlite3GenerateConstraintChecks()) as part of
3596   ** UPDATE and INSERT statements.
3597   */
3598   if( db->init.busy || pTblName==0 ){
3599     if( onError!=OE_Replace || pTab->pIndex==0
3600          || pTab->pIndex->onError==OE_Replace){
3601       pIndex->pNext = pTab->pIndex;
3602       pTab->pIndex = pIndex;
3603     }else{
3604       Index *pOther = pTab->pIndex;
3605       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
3606         pOther = pOther->pNext;
3607       }
3608       pIndex->pNext = pOther->pNext;
3609       pOther->pNext = pIndex;
3610     }
3611     pIndex = 0;
3612   }
3613   else if( IN_RENAME_OBJECT ){
3614     assert( pParse->pNewIndex==0 );
3615     pParse->pNewIndex = pIndex;
3616     pIndex = 0;
3617   }
3618 
3619   /* Clean up before exiting */
3620 exit_create_index:
3621   if( pIndex ) sqlite3FreeIndex(db, pIndex);
3622   sqlite3ExprDelete(db, pPIWhere);
3623   sqlite3ExprListDelete(db, pList);
3624   sqlite3SrcListDelete(db, pTblName);
3625   sqlite3DbFree(db, zName);
3626 }
3627 
3628 /*
3629 ** Fill the Index.aiRowEst[] array with default information - information
3630 ** to be used when we have not run the ANALYZE command.
3631 **
3632 ** aiRowEst[0] is supposed to contain the number of elements in the index.
3633 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
3634 ** number of rows in the table that match any particular value of the
3635 ** first column of the index.  aiRowEst[2] is an estimate of the number
3636 ** of rows that match any particular combination of the first 2 columns
3637 ** of the index.  And so forth.  It must always be the case that
3638 *
3639 **           aiRowEst[N]<=aiRowEst[N-1]
3640 **           aiRowEst[N]>=1
3641 **
3642 ** Apart from that, we have little to go on besides intuition as to
3643 ** how aiRowEst[] should be initialized.  The numbers generated here
3644 ** are based on typical values found in actual indices.
3645 */
3646 void sqlite3DefaultRowEst(Index *pIdx){
3647   /*                10,  9,  8,  7,  6 */
3648   LogEst aVal[] = { 33, 32, 30, 28, 26 };
3649   LogEst *a = pIdx->aiRowLogEst;
3650   int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
3651   int i;
3652 
3653   /* Indexes with default row estimates should not have stat1 data */
3654   assert( !pIdx->hasStat1 );
3655 
3656   /* Set the first entry (number of rows in the index) to the estimated
3657   ** number of rows in the table, or half the number of rows in the table
3658   ** for a partial index.   But do not let the estimate drop below 10. */
3659   a[0] = pIdx->pTable->nRowLogEst;
3660   if( pIdx->pPartIdxWhere!=0 ) a[0] -= 10;  assert( 10==sqlite3LogEst(2) );
3661   if( a[0]<33 ) a[0] = 33;                  assert( 33==sqlite3LogEst(10) );
3662 
3663   /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
3664   ** 6 and each subsequent value (if any) is 5.  */
3665   memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
3666   for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
3667     a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
3668   }
3669 
3670   assert( 0==sqlite3LogEst(1) );
3671   if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
3672 }
3673 
3674 /*
3675 ** This routine will drop an existing named index.  This routine
3676 ** implements the DROP INDEX statement.
3677 */
3678 void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
3679   Index *pIndex;
3680   Vdbe *v;
3681   sqlite3 *db = pParse->db;
3682   int iDb;
3683 
3684   assert( pParse->nErr==0 );   /* Never called with prior errors */
3685   if( db->mallocFailed ){
3686     goto exit_drop_index;
3687   }
3688   assert( pName->nSrc==1 );
3689   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
3690     goto exit_drop_index;
3691   }
3692   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
3693   if( pIndex==0 ){
3694     if( !ifExists ){
3695       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
3696     }else{
3697       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
3698     }
3699     pParse->checkSchema = 1;
3700     goto exit_drop_index;
3701   }
3702   if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
3703     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
3704       "or PRIMARY KEY constraint cannot be dropped", 0);
3705     goto exit_drop_index;
3706   }
3707   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
3708 #ifndef SQLITE_OMIT_AUTHORIZATION
3709   {
3710     int code = SQLITE_DROP_INDEX;
3711     Table *pTab = pIndex->pTable;
3712     const char *zDb = db->aDb[iDb].zDbSName;
3713     const char *zTab = SCHEMA_TABLE(iDb);
3714     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
3715       goto exit_drop_index;
3716     }
3717     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
3718     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
3719       goto exit_drop_index;
3720     }
3721   }
3722 #endif
3723 
3724   /* Generate code to remove the index and from the master table */
3725   v = sqlite3GetVdbe(pParse);
3726   if( v ){
3727     sqlite3BeginWriteOperation(pParse, 1, iDb);
3728     sqlite3NestedParse(pParse,
3729        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
3730        db->aDb[iDb].zDbSName, MASTER_NAME, pIndex->zName
3731     );
3732     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
3733     sqlite3ChangeCookie(pParse, iDb);
3734     destroyRootPage(pParse, pIndex->tnum, iDb);
3735     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
3736   }
3737 
3738 exit_drop_index:
3739   sqlite3SrcListDelete(db, pName);
3740 }
3741 
3742 /*
3743 ** pArray is a pointer to an array of objects. Each object in the
3744 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
3745 ** to extend the array so that there is space for a new object at the end.
3746 **
3747 ** When this function is called, *pnEntry contains the current size of
3748 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
3749 ** in total).
3750 **
3751 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
3752 ** space allocated for the new object is zeroed, *pnEntry updated to
3753 ** reflect the new size of the array and a pointer to the new allocation
3754 ** returned. *pIdx is set to the index of the new array entry in this case.
3755 **
3756 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
3757 ** unchanged and a copy of pArray returned.
3758 */
3759 void *sqlite3ArrayAllocate(
3760   sqlite3 *db,      /* Connection to notify of malloc failures */
3761   void *pArray,     /* Array of objects.  Might be reallocated */
3762   int szEntry,      /* Size of each object in the array */
3763   int *pnEntry,     /* Number of objects currently in use */
3764   int *pIdx         /* Write the index of a new slot here */
3765 ){
3766   char *z;
3767   int n = *pnEntry;
3768   if( (n & (n-1))==0 ){
3769     int sz = (n==0) ? 1 : 2*n;
3770     void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
3771     if( pNew==0 ){
3772       *pIdx = -1;
3773       return pArray;
3774     }
3775     pArray = pNew;
3776   }
3777   z = (char*)pArray;
3778   memset(&z[n * szEntry], 0, szEntry);
3779   *pIdx = n;
3780   ++*pnEntry;
3781   return pArray;
3782 }
3783 
3784 /*
3785 ** Append a new element to the given IdList.  Create a new IdList if
3786 ** need be.
3787 **
3788 ** A new IdList is returned, or NULL if malloc() fails.
3789 */
3790 IdList *sqlite3IdListAppend(Parse *pParse, IdList *pList, Token *pToken){
3791   sqlite3 *db = pParse->db;
3792   int i;
3793   if( pList==0 ){
3794     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
3795     if( pList==0 ) return 0;
3796   }
3797   pList->a = sqlite3ArrayAllocate(
3798       db,
3799       pList->a,
3800       sizeof(pList->a[0]),
3801       &pList->nId,
3802       &i
3803   );
3804   if( i<0 ){
3805     sqlite3IdListDelete(db, pList);
3806     return 0;
3807   }
3808   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
3809   if( IN_RENAME_OBJECT && pList->a[i].zName ){
3810     sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
3811   }
3812   return pList;
3813 }
3814 
3815 /*
3816 ** Delete an IdList.
3817 */
3818 void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
3819   int i;
3820   if( pList==0 ) return;
3821   for(i=0; i<pList->nId; i++){
3822     sqlite3DbFree(db, pList->a[i].zName);
3823   }
3824   sqlite3DbFree(db, pList->a);
3825   sqlite3DbFreeNN(db, pList);
3826 }
3827 
3828 /*
3829 ** Return the index in pList of the identifier named zId.  Return -1
3830 ** if not found.
3831 */
3832 int sqlite3IdListIndex(IdList *pList, const char *zName){
3833   int i;
3834   if( pList==0 ) return -1;
3835   for(i=0; i<pList->nId; i++){
3836     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
3837   }
3838   return -1;
3839 }
3840 
3841 /*
3842 ** Maximum size of a SrcList object.
3843 ** The SrcList object is used to represent the FROM clause of a
3844 ** SELECT statement, and the query planner cannot deal with more
3845 ** than 64 tables in a join.  So any value larger than 64 here
3846 ** is sufficient for most uses.  Smaller values, like say 10, are
3847 ** appropriate for small and memory-limited applications.
3848 */
3849 #ifndef SQLITE_MAX_SRCLIST
3850 # define SQLITE_MAX_SRCLIST 200
3851 #endif
3852 
3853 /*
3854 ** Expand the space allocated for the given SrcList object by
3855 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
3856 ** New slots are zeroed.
3857 **
3858 ** For example, suppose a SrcList initially contains two entries: A,B.
3859 ** To append 3 new entries onto the end, do this:
3860 **
3861 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
3862 **
3863 ** After the call above it would contain:  A, B, nil, nil, nil.
3864 ** If the iStart argument had been 1 instead of 2, then the result
3865 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
3866 ** the iStart value would be 0.  The result then would
3867 ** be: nil, nil, nil, A, B.
3868 **
3869 ** If a memory allocation fails or the SrcList becomes too large, leave
3870 ** the original SrcList unchanged, return NULL, and leave an error message
3871 ** in pParse.
3872 */
3873 SrcList *sqlite3SrcListEnlarge(
3874   Parse *pParse,     /* Parsing context into which errors are reported */
3875   SrcList *pSrc,     /* The SrcList to be enlarged */
3876   int nExtra,        /* Number of new slots to add to pSrc->a[] */
3877   int iStart         /* Index in pSrc->a[] of first new slot */
3878 ){
3879   int i;
3880 
3881   /* Sanity checking on calling parameters */
3882   assert( iStart>=0 );
3883   assert( nExtra>=1 );
3884   assert( pSrc!=0 );
3885   assert( iStart<=pSrc->nSrc );
3886 
3887   /* Allocate additional space if needed */
3888   if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
3889     SrcList *pNew;
3890     int nAlloc = pSrc->nSrc*2+nExtra;
3891     sqlite3 *db = pParse->db;
3892 
3893     if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
3894       sqlite3ErrorMsg(pParse, "too many FROM clause terms, max: %d",
3895                       SQLITE_MAX_SRCLIST);
3896       return 0;
3897     }
3898     if( nAlloc>SQLITE_MAX_SRCLIST ) nAlloc = SQLITE_MAX_SRCLIST;
3899     pNew = sqlite3DbRealloc(db, pSrc,
3900                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
3901     if( pNew==0 ){
3902       assert( db->mallocFailed );
3903       return 0;
3904     }
3905     pSrc = pNew;
3906     pSrc->nAlloc = nAlloc;
3907   }
3908 
3909   /* Move existing slots that come after the newly inserted slots
3910   ** out of the way */
3911   for(i=pSrc->nSrc-1; i>=iStart; i--){
3912     pSrc->a[i+nExtra] = pSrc->a[i];
3913   }
3914   pSrc->nSrc += nExtra;
3915 
3916   /* Zero the newly allocated slots */
3917   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
3918   for(i=iStart; i<iStart+nExtra; i++){
3919     pSrc->a[i].iCursor = -1;
3920   }
3921 
3922   /* Return a pointer to the enlarged SrcList */
3923   return pSrc;
3924 }
3925 
3926 
3927 /*
3928 ** Append a new table name to the given SrcList.  Create a new SrcList if
3929 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
3930 **
3931 ** A SrcList is returned, or NULL if there is an OOM error or if the
3932 ** SrcList grows to large.  The returned
3933 ** SrcList might be the same as the SrcList that was input or it might be
3934 ** a new one.  If an OOM error does occurs, then the prior value of pList
3935 ** that is input to this routine is automatically freed.
3936 **
3937 ** If pDatabase is not null, it means that the table has an optional
3938 ** database name prefix.  Like this:  "database.table".  The pDatabase
3939 ** points to the table name and the pTable points to the database name.
3940 ** The SrcList.a[].zName field is filled with the table name which might
3941 ** come from pTable (if pDatabase is NULL) or from pDatabase.
3942 ** SrcList.a[].zDatabase is filled with the database name from pTable,
3943 ** or with NULL if no database is specified.
3944 **
3945 ** In other words, if call like this:
3946 **
3947 **         sqlite3SrcListAppend(D,A,B,0);
3948 **
3949 ** Then B is a table name and the database name is unspecified.  If called
3950 ** like this:
3951 **
3952 **         sqlite3SrcListAppend(D,A,B,C);
3953 **
3954 ** Then C is the table name and B is the database name.  If C is defined
3955 ** then so is B.  In other words, we never have a case where:
3956 **
3957 **         sqlite3SrcListAppend(D,A,0,C);
3958 **
3959 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
3960 ** before being added to the SrcList.
3961 */
3962 SrcList *sqlite3SrcListAppend(
3963   Parse *pParse,      /* Parsing context, in which errors are reported */
3964   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
3965   Token *pTable,      /* Table to append */
3966   Token *pDatabase    /* Database of the table */
3967 ){
3968   struct SrcList_item *pItem;
3969   sqlite3 *db;
3970   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
3971   assert( pParse!=0 );
3972   assert( pParse->db!=0 );
3973   db = pParse->db;
3974   if( pList==0 ){
3975     pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
3976     if( pList==0 ) return 0;
3977     pList->nAlloc = 1;
3978     pList->nSrc = 1;
3979     memset(&pList->a[0], 0, sizeof(pList->a[0]));
3980     pList->a[0].iCursor = -1;
3981   }else{
3982     SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
3983     if( pNew==0 ){
3984       sqlite3SrcListDelete(db, pList);
3985       return 0;
3986     }else{
3987       pList = pNew;
3988     }
3989   }
3990   pItem = &pList->a[pList->nSrc-1];
3991   if( pDatabase && pDatabase->z==0 ){
3992     pDatabase = 0;
3993   }
3994   if( pDatabase ){
3995     pItem->zName = sqlite3NameFromToken(db, pDatabase);
3996     pItem->zDatabase = sqlite3NameFromToken(db, pTable);
3997   }else{
3998     pItem->zName = sqlite3NameFromToken(db, pTable);
3999     pItem->zDatabase = 0;
4000   }
4001   return pList;
4002 }
4003 
4004 /*
4005 ** Assign VdbeCursor index numbers to all tables in a SrcList
4006 */
4007 void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
4008   int i;
4009   struct SrcList_item *pItem;
4010   assert(pList || pParse->db->mallocFailed );
4011   if( pList ){
4012     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
4013       if( pItem->iCursor>=0 ) break;
4014       pItem->iCursor = pParse->nTab++;
4015       if( pItem->pSelect ){
4016         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
4017       }
4018     }
4019   }
4020 }
4021 
4022 /*
4023 ** Delete an entire SrcList including all its substructure.
4024 */
4025 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
4026   int i;
4027   struct SrcList_item *pItem;
4028   if( pList==0 ) return;
4029   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
4030     sqlite3DbFree(db, pItem->zDatabase);
4031     sqlite3DbFree(db, pItem->zName);
4032     sqlite3DbFree(db, pItem->zAlias);
4033     if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
4034     if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
4035     sqlite3DeleteTable(db, pItem->pTab);
4036     sqlite3SelectDelete(db, pItem->pSelect);
4037     sqlite3ExprDelete(db, pItem->pOn);
4038     sqlite3IdListDelete(db, pItem->pUsing);
4039   }
4040   sqlite3DbFreeNN(db, pList);
4041 }
4042 
4043 /*
4044 ** This routine is called by the parser to add a new term to the
4045 ** end of a growing FROM clause.  The "p" parameter is the part of
4046 ** the FROM clause that has already been constructed.  "p" is NULL
4047 ** if this is the first term of the FROM clause.  pTable and pDatabase
4048 ** are the name of the table and database named in the FROM clause term.
4049 ** pDatabase is NULL if the database name qualifier is missing - the
4050 ** usual case.  If the term has an alias, then pAlias points to the
4051 ** alias token.  If the term is a subquery, then pSubquery is the
4052 ** SELECT statement that the subquery encodes.  The pTable and
4053 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
4054 ** parameters are the content of the ON and USING clauses.
4055 **
4056 ** Return a new SrcList which encodes is the FROM with the new
4057 ** term added.
4058 */
4059 SrcList *sqlite3SrcListAppendFromTerm(
4060   Parse *pParse,          /* Parsing context */
4061   SrcList *p,             /* The left part of the FROM clause already seen */
4062   Token *pTable,          /* Name of the table to add to the FROM clause */
4063   Token *pDatabase,       /* Name of the database containing pTable */
4064   Token *pAlias,          /* The right-hand side of the AS subexpression */
4065   Select *pSubquery,      /* A subquery used in place of a table name */
4066   Expr *pOn,              /* The ON clause of a join */
4067   IdList *pUsing          /* The USING clause of a join */
4068 ){
4069   struct SrcList_item *pItem;
4070   sqlite3 *db = pParse->db;
4071   if( !p && (pOn || pUsing) ){
4072     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
4073       (pOn ? "ON" : "USING")
4074     );
4075     goto append_from_error;
4076   }
4077   p = sqlite3SrcListAppend(pParse, p, pTable, pDatabase);
4078   if( p==0 ){
4079     goto append_from_error;
4080   }
4081   assert( p->nSrc>0 );
4082   pItem = &p->a[p->nSrc-1];
4083   assert( (pTable==0)==(pDatabase==0) );
4084   assert( pItem->zName==0 || pDatabase!=0 );
4085   if( IN_RENAME_OBJECT && pItem->zName ){
4086     Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
4087     sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
4088   }
4089   assert( pAlias!=0 );
4090   if( pAlias->n ){
4091     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
4092   }
4093   pItem->pSelect = pSubquery;
4094   pItem->pOn = pOn;
4095   pItem->pUsing = pUsing;
4096   return p;
4097 
4098  append_from_error:
4099   assert( p==0 );
4100   sqlite3ExprDelete(db, pOn);
4101   sqlite3IdListDelete(db, pUsing);
4102   sqlite3SelectDelete(db, pSubquery);
4103   return 0;
4104 }
4105 
4106 /*
4107 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
4108 ** element of the source-list passed as the second argument.
4109 */
4110 void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
4111   assert( pIndexedBy!=0 );
4112   if( p && pIndexedBy->n>0 ){
4113     struct SrcList_item *pItem;
4114     assert( p->nSrc>0 );
4115     pItem = &p->a[p->nSrc-1];
4116     assert( pItem->fg.notIndexed==0 );
4117     assert( pItem->fg.isIndexedBy==0 );
4118     assert( pItem->fg.isTabFunc==0 );
4119     if( pIndexedBy->n==1 && !pIndexedBy->z ){
4120       /* A "NOT INDEXED" clause was supplied. See parse.y
4121       ** construct "indexed_opt" for details. */
4122       pItem->fg.notIndexed = 1;
4123     }else{
4124       pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
4125       pItem->fg.isIndexedBy = 1;
4126     }
4127   }
4128 }
4129 
4130 /*
4131 ** Add the list of function arguments to the SrcList entry for a
4132 ** table-valued-function.
4133 */
4134 void sqlite3SrcListFuncArgs(Parse *pParse, SrcList *p, ExprList *pList){
4135   if( p ){
4136     struct SrcList_item *pItem = &p->a[p->nSrc-1];
4137     assert( pItem->fg.notIndexed==0 );
4138     assert( pItem->fg.isIndexedBy==0 );
4139     assert( pItem->fg.isTabFunc==0 );
4140     pItem->u1.pFuncArg = pList;
4141     pItem->fg.isTabFunc = 1;
4142   }else{
4143     sqlite3ExprListDelete(pParse->db, pList);
4144   }
4145 }
4146 
4147 /*
4148 ** When building up a FROM clause in the parser, the join operator
4149 ** is initially attached to the left operand.  But the code generator
4150 ** expects the join operator to be on the right operand.  This routine
4151 ** Shifts all join operators from left to right for an entire FROM
4152 ** clause.
4153 **
4154 ** Example: Suppose the join is like this:
4155 **
4156 **           A natural cross join B
4157 **
4158 ** The operator is "natural cross join".  The A and B operands are stored
4159 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
4160 ** operator with A.  This routine shifts that operator over to B.
4161 */
4162 void sqlite3SrcListShiftJoinType(SrcList *p){
4163   if( p ){
4164     int i;
4165     for(i=p->nSrc-1; i>0; i--){
4166       p->a[i].fg.jointype = p->a[i-1].fg.jointype;
4167     }
4168     p->a[0].fg.jointype = 0;
4169   }
4170 }
4171 
4172 /*
4173 ** Generate VDBE code for a BEGIN statement.
4174 */
4175 void sqlite3BeginTransaction(Parse *pParse, int type){
4176   sqlite3 *db;
4177   Vdbe *v;
4178   int i;
4179 
4180   assert( pParse!=0 );
4181   db = pParse->db;
4182   assert( db!=0 );
4183   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
4184     return;
4185   }
4186   v = sqlite3GetVdbe(pParse);
4187   if( !v ) return;
4188   if( type!=TK_DEFERRED ){
4189     for(i=0; i<db->nDb; i++){
4190       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
4191       sqlite3VdbeUsesBtree(v, i);
4192     }
4193   }
4194   sqlite3VdbeAddOp0(v, OP_AutoCommit);
4195 }
4196 
4197 /*
4198 ** Generate VDBE code for a COMMIT or ROLLBACK statement.
4199 ** Code for ROLLBACK is generated if eType==TK_ROLLBACK.  Otherwise
4200 ** code is generated for a COMMIT.
4201 */
4202 void sqlite3EndTransaction(Parse *pParse, int eType){
4203   Vdbe *v;
4204   int isRollback;
4205 
4206   assert( pParse!=0 );
4207   assert( pParse->db!=0 );
4208   assert( eType==TK_COMMIT || eType==TK_END || eType==TK_ROLLBACK );
4209   isRollback = eType==TK_ROLLBACK;
4210   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION,
4211        isRollback ? "ROLLBACK" : "COMMIT", 0, 0) ){
4212     return;
4213   }
4214   v = sqlite3GetVdbe(pParse);
4215   if( v ){
4216     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, isRollback);
4217   }
4218 }
4219 
4220 /*
4221 ** This function is called by the parser when it parses a command to create,
4222 ** release or rollback an SQL savepoint.
4223 */
4224 void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
4225   char *zName = sqlite3NameFromToken(pParse->db, pName);
4226   if( zName ){
4227     Vdbe *v = sqlite3GetVdbe(pParse);
4228 #ifndef SQLITE_OMIT_AUTHORIZATION
4229     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
4230     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
4231 #endif
4232     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
4233       sqlite3DbFree(pParse->db, zName);
4234       return;
4235     }
4236     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
4237   }
4238 }
4239 
4240 /*
4241 ** Make sure the TEMP database is open and available for use.  Return
4242 ** the number of errors.  Leave any error messages in the pParse structure.
4243 */
4244 int sqlite3OpenTempDatabase(Parse *pParse){
4245   sqlite3 *db = pParse->db;
4246   if( db->aDb[1].pBt==0 && !pParse->explain ){
4247     int rc;
4248     Btree *pBt;
4249     static const int flags =
4250           SQLITE_OPEN_READWRITE |
4251           SQLITE_OPEN_CREATE |
4252           SQLITE_OPEN_EXCLUSIVE |
4253           SQLITE_OPEN_DELETEONCLOSE |
4254           SQLITE_OPEN_TEMP_DB;
4255 
4256     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
4257     if( rc!=SQLITE_OK ){
4258       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
4259         "file for storing temporary tables");
4260       pParse->rc = rc;
4261       return 1;
4262     }
4263     db->aDb[1].pBt = pBt;
4264     assert( db->aDb[1].pSchema );
4265     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
4266       sqlite3OomFault(db);
4267       return 1;
4268     }
4269   }
4270   return 0;
4271 }
4272 
4273 /*
4274 ** Record the fact that the schema cookie will need to be verified
4275 ** for database iDb.  The code to actually verify the schema cookie
4276 ** will occur at the end of the top-level VDBE and will be generated
4277 ** later, by sqlite3FinishCoding().
4278 */
4279 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
4280   Parse *pToplevel = sqlite3ParseToplevel(pParse);
4281 
4282   assert( iDb>=0 && iDb<pParse->db->nDb );
4283   assert( pParse->db->aDb[iDb].pBt!=0 || iDb==1 );
4284   assert( iDb<SQLITE_MAX_ATTACHED+2 );
4285   assert( sqlite3SchemaMutexHeld(pParse->db, iDb, 0) );
4286   if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
4287     DbMaskSet(pToplevel->cookieMask, iDb);
4288     if( !OMIT_TEMPDB && iDb==1 ){
4289       sqlite3OpenTempDatabase(pToplevel);
4290     }
4291   }
4292 }
4293 
4294 /*
4295 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
4296 ** attached database. Otherwise, invoke it for the database named zDb only.
4297 */
4298 void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
4299   sqlite3 *db = pParse->db;
4300   int i;
4301   for(i=0; i<db->nDb; i++){
4302     Db *pDb = &db->aDb[i];
4303     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
4304       sqlite3CodeVerifySchema(pParse, i);
4305     }
4306   }
4307 }
4308 
4309 /*
4310 ** Generate VDBE code that prepares for doing an operation that
4311 ** might change the database.
4312 **
4313 ** This routine starts a new transaction if we are not already within
4314 ** a transaction.  If we are already within a transaction, then a checkpoint
4315 ** is set if the setStatement parameter is true.  A checkpoint should
4316 ** be set for operations that might fail (due to a constraint) part of
4317 ** the way through and which will need to undo some writes without having to
4318 ** rollback the whole transaction.  For operations where all constraints
4319 ** can be checked before any changes are made to the database, it is never
4320 ** necessary to undo a write and the checkpoint should not be set.
4321 */
4322 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
4323   Parse *pToplevel = sqlite3ParseToplevel(pParse);
4324   sqlite3CodeVerifySchema(pParse, iDb);
4325   DbMaskSet(pToplevel->writeMask, iDb);
4326   pToplevel->isMultiWrite |= setStatement;
4327 }
4328 
4329 /*
4330 ** Indicate that the statement currently under construction might write
4331 ** more than one entry (example: deleting one row then inserting another,
4332 ** inserting multiple rows in a table, or inserting a row and index entries.)
4333 ** If an abort occurs after some of these writes have completed, then it will
4334 ** be necessary to undo the completed writes.
4335 */
4336 void sqlite3MultiWrite(Parse *pParse){
4337   Parse *pToplevel = sqlite3ParseToplevel(pParse);
4338   pToplevel->isMultiWrite = 1;
4339 }
4340 
4341 /*
4342 ** The code generator calls this routine if is discovers that it is
4343 ** possible to abort a statement prior to completion.  In order to
4344 ** perform this abort without corrupting the database, we need to make
4345 ** sure that the statement is protected by a statement transaction.
4346 **
4347 ** Technically, we only need to set the mayAbort flag if the
4348 ** isMultiWrite flag was previously set.  There is a time dependency
4349 ** such that the abort must occur after the multiwrite.  This makes
4350 ** some statements involving the REPLACE conflict resolution algorithm
4351 ** go a little faster.  But taking advantage of this time dependency
4352 ** makes it more difficult to prove that the code is correct (in
4353 ** particular, it prevents us from writing an effective
4354 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
4355 ** to take the safe route and skip the optimization.
4356 */
4357 void sqlite3MayAbort(Parse *pParse){
4358   Parse *pToplevel = sqlite3ParseToplevel(pParse);
4359   pToplevel->mayAbort = 1;
4360 }
4361 
4362 /*
4363 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
4364 ** error. The onError parameter determines which (if any) of the statement
4365 ** and/or current transaction is rolled back.
4366 */
4367 void sqlite3HaltConstraint(
4368   Parse *pParse,    /* Parsing context */
4369   int errCode,      /* extended error code */
4370   int onError,      /* Constraint type */
4371   char *p4,         /* Error message */
4372   i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
4373   u8 p5Errmsg       /* P5_ErrMsg type */
4374 ){
4375   Vdbe *v = sqlite3GetVdbe(pParse);
4376   assert( (errCode&0xff)==SQLITE_CONSTRAINT );
4377   if( onError==OE_Abort ){
4378     sqlite3MayAbort(pParse);
4379   }
4380   sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
4381   sqlite3VdbeChangeP5(v, p5Errmsg);
4382 }
4383 
4384 /*
4385 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
4386 */
4387 void sqlite3UniqueConstraint(
4388   Parse *pParse,    /* Parsing context */
4389   int onError,      /* Constraint type */
4390   Index *pIdx       /* The index that triggers the constraint */
4391 ){
4392   char *zErr;
4393   int j;
4394   StrAccum errMsg;
4395   Table *pTab = pIdx->pTable;
4396 
4397   sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
4398   if( pIdx->aColExpr ){
4399     sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
4400   }else{
4401     for(j=0; j<pIdx->nKeyCol; j++){
4402       char *zCol;
4403       assert( pIdx->aiColumn[j]>=0 );
4404       zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
4405       if( j ) sqlite3_str_append(&errMsg, ", ", 2);
4406       sqlite3_str_appendall(&errMsg, pTab->zName);
4407       sqlite3_str_append(&errMsg, ".", 1);
4408       sqlite3_str_appendall(&errMsg, zCol);
4409     }
4410   }
4411   zErr = sqlite3StrAccumFinish(&errMsg);
4412   sqlite3HaltConstraint(pParse,
4413     IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
4414                             : SQLITE_CONSTRAINT_UNIQUE,
4415     onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
4416 }
4417 
4418 
4419 /*
4420 ** Code an OP_Halt due to non-unique rowid.
4421 */
4422 void sqlite3RowidConstraint(
4423   Parse *pParse,    /* Parsing context */
4424   int onError,      /* Conflict resolution algorithm */
4425   Table *pTab       /* The table with the non-unique rowid */
4426 ){
4427   char *zMsg;
4428   int rc;
4429   if( pTab->iPKey>=0 ){
4430     zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
4431                           pTab->aCol[pTab->iPKey].zName);
4432     rc = SQLITE_CONSTRAINT_PRIMARYKEY;
4433   }else{
4434     zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
4435     rc = SQLITE_CONSTRAINT_ROWID;
4436   }
4437   sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
4438                         P5_ConstraintUnique);
4439 }
4440 
4441 /*
4442 ** Check to see if pIndex uses the collating sequence pColl.  Return
4443 ** true if it does and false if it does not.
4444 */
4445 #ifndef SQLITE_OMIT_REINDEX
4446 static int collationMatch(const char *zColl, Index *pIndex){
4447   int i;
4448   assert( zColl!=0 );
4449   for(i=0; i<pIndex->nColumn; i++){
4450     const char *z = pIndex->azColl[i];
4451     assert( z!=0 || pIndex->aiColumn[i]<0 );
4452     if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
4453       return 1;
4454     }
4455   }
4456   return 0;
4457 }
4458 #endif
4459 
4460 /*
4461 ** Recompute all indices of pTab that use the collating sequence pColl.
4462 ** If pColl==0 then recompute all indices of pTab.
4463 */
4464 #ifndef SQLITE_OMIT_REINDEX
4465 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
4466   if( !IsVirtual(pTab) ){
4467     Index *pIndex;              /* An index associated with pTab */
4468 
4469     for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
4470       if( zColl==0 || collationMatch(zColl, pIndex) ){
4471         int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
4472         sqlite3BeginWriteOperation(pParse, 0, iDb);
4473         sqlite3RefillIndex(pParse, pIndex, -1);
4474       }
4475     }
4476   }
4477 }
4478 #endif
4479 
4480 /*
4481 ** Recompute all indices of all tables in all databases where the
4482 ** indices use the collating sequence pColl.  If pColl==0 then recompute
4483 ** all indices everywhere.
4484 */
4485 #ifndef SQLITE_OMIT_REINDEX
4486 static void reindexDatabases(Parse *pParse, char const *zColl){
4487   Db *pDb;                    /* A single database */
4488   int iDb;                    /* The database index number */
4489   sqlite3 *db = pParse->db;   /* The database connection */
4490   HashElem *k;                /* For looping over tables in pDb */
4491   Table *pTab;                /* A table in the database */
4492 
4493   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
4494   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
4495     assert( pDb!=0 );
4496     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
4497       pTab = (Table*)sqliteHashData(k);
4498       reindexTable(pParse, pTab, zColl);
4499     }
4500   }
4501 }
4502 #endif
4503 
4504 /*
4505 ** Generate code for the REINDEX command.
4506 **
4507 **        REINDEX                            -- 1
4508 **        REINDEX  <collation>               -- 2
4509 **        REINDEX  ?<database>.?<tablename>  -- 3
4510 **        REINDEX  ?<database>.?<indexname>  -- 4
4511 **
4512 ** Form 1 causes all indices in all attached databases to be rebuilt.
4513 ** Form 2 rebuilds all indices in all databases that use the named
4514 ** collating function.  Forms 3 and 4 rebuild the named index or all
4515 ** indices associated with the named table.
4516 */
4517 #ifndef SQLITE_OMIT_REINDEX
4518 void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
4519   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
4520   char *z;                    /* Name of a table or index */
4521   const char *zDb;            /* Name of the database */
4522   Table *pTab;                /* A table in the database */
4523   Index *pIndex;              /* An index associated with pTab */
4524   int iDb;                    /* The database index number */
4525   sqlite3 *db = pParse->db;   /* The database connection */
4526   Token *pObjName;            /* Name of the table or index to be reindexed */
4527 
4528   /* Read the database schema. If an error occurs, leave an error message
4529   ** and code in pParse and return NULL. */
4530   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
4531     return;
4532   }
4533 
4534   if( pName1==0 ){
4535     reindexDatabases(pParse, 0);
4536     return;
4537   }else if( NEVER(pName2==0) || pName2->z==0 ){
4538     char *zColl;
4539     assert( pName1->z );
4540     zColl = sqlite3NameFromToken(pParse->db, pName1);
4541     if( !zColl ) return;
4542     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
4543     if( pColl ){
4544       reindexDatabases(pParse, zColl);
4545       sqlite3DbFree(db, zColl);
4546       return;
4547     }
4548     sqlite3DbFree(db, zColl);
4549   }
4550   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
4551   if( iDb<0 ) return;
4552   z = sqlite3NameFromToken(db, pObjName);
4553   if( z==0 ) return;
4554   zDb = db->aDb[iDb].zDbSName;
4555   pTab = sqlite3FindTable(db, z, zDb);
4556   if( pTab ){
4557     reindexTable(pParse, pTab, 0);
4558     sqlite3DbFree(db, z);
4559     return;
4560   }
4561   pIndex = sqlite3FindIndex(db, z, zDb);
4562   sqlite3DbFree(db, z);
4563   if( pIndex ){
4564     sqlite3BeginWriteOperation(pParse, 0, iDb);
4565     sqlite3RefillIndex(pParse, pIndex, -1);
4566     return;
4567   }
4568   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
4569 }
4570 #endif
4571 
4572 /*
4573 ** Return a KeyInfo structure that is appropriate for the given Index.
4574 **
4575 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
4576 ** when it has finished using it.
4577 */
4578 KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
4579   int i;
4580   int nCol = pIdx->nColumn;
4581   int nKey = pIdx->nKeyCol;
4582   KeyInfo *pKey;
4583   if( pParse->nErr ) return 0;
4584   if( pIdx->uniqNotNull ){
4585     pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
4586   }else{
4587     pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
4588   }
4589   if( pKey ){
4590     assert( sqlite3KeyInfoIsWriteable(pKey) );
4591     for(i=0; i<nCol; i++){
4592       const char *zColl = pIdx->azColl[i];
4593       pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
4594                         sqlite3LocateCollSeq(pParse, zColl);
4595       pKey->aSortOrder[i] = pIdx->aSortOrder[i];
4596     }
4597     if( pParse->nErr ){
4598       assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
4599       if( pIdx->bNoQuery==0 ){
4600         /* Deactivate the index because it contains an unknown collating
4601         ** sequence.  The only way to reactive the index is to reload the
4602         ** schema.  Adding the missing collating sequence later does not
4603         ** reactive the index.  The application had the chance to register
4604         ** the missing index using the collation-needed callback.  For
4605         ** simplicity, SQLite will not give the application a second chance.
4606         */
4607         pIdx->bNoQuery = 1;
4608         pParse->rc = SQLITE_ERROR_RETRY;
4609       }
4610       sqlite3KeyInfoUnref(pKey);
4611       pKey = 0;
4612     }
4613   }
4614   return pKey;
4615 }
4616 
4617 #ifndef SQLITE_OMIT_CTE
4618 /*
4619 ** This routine is invoked once per CTE by the parser while parsing a
4620 ** WITH clause.
4621 */
4622 With *sqlite3WithAdd(
4623   Parse *pParse,          /* Parsing context */
4624   With *pWith,            /* Existing WITH clause, or NULL */
4625   Token *pName,           /* Name of the common-table */
4626   ExprList *pArglist,     /* Optional column name list for the table */
4627   Select *pQuery          /* Query used to initialize the table */
4628 ){
4629   sqlite3 *db = pParse->db;
4630   With *pNew;
4631   char *zName;
4632 
4633   /* Check that the CTE name is unique within this WITH clause. If
4634   ** not, store an error in the Parse structure. */
4635   zName = sqlite3NameFromToken(pParse->db, pName);
4636   if( zName && pWith ){
4637     int i;
4638     for(i=0; i<pWith->nCte; i++){
4639       if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
4640         sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
4641       }
4642     }
4643   }
4644 
4645   if( pWith ){
4646     int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
4647     pNew = sqlite3DbRealloc(db, pWith, nByte);
4648   }else{
4649     pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
4650   }
4651   assert( (pNew!=0 && zName!=0) || db->mallocFailed );
4652 
4653   if( db->mallocFailed ){
4654     sqlite3ExprListDelete(db, pArglist);
4655     sqlite3SelectDelete(db, pQuery);
4656     sqlite3DbFree(db, zName);
4657     pNew = pWith;
4658   }else{
4659     pNew->a[pNew->nCte].pSelect = pQuery;
4660     pNew->a[pNew->nCte].pCols = pArglist;
4661     pNew->a[pNew->nCte].zName = zName;
4662     pNew->a[pNew->nCte].zCteErr = 0;
4663     pNew->nCte++;
4664   }
4665 
4666   return pNew;
4667 }
4668 
4669 /*
4670 ** Free the contents of the With object passed as the second argument.
4671 */
4672 void sqlite3WithDelete(sqlite3 *db, With *pWith){
4673   if( pWith ){
4674     int i;
4675     for(i=0; i<pWith->nCte; i++){
4676       struct Cte *pCte = &pWith->a[i];
4677       sqlite3ExprListDelete(db, pCte->pCols);
4678       sqlite3SelectDelete(db, pCte->pSelect);
4679       sqlite3DbFree(db, pCte->zName);
4680     }
4681     sqlite3DbFree(db, pWith);
4682   }
4683 }
4684 #endif /* !defined(SQLITE_OMIT_CTE) */
4685