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