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