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