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