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