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