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