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