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