xref: /sqlite-3.40.0/src/build.c (revision 8749c183)
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 /*
28 ** This routine is called when a new SQL statement is beginning to
29 ** be parsed.  Initialize the pParse structure as needed.
30 */
31 void sqlite3BeginParse(Parse *pParse, int explainFlag){
32   pParse->explain = (u8)explainFlag;
33   pParse->nVar = 0;
34 }
35 
36 #ifndef SQLITE_OMIT_SHARED_CACHE
37 /*
38 ** The TableLock structure is only used by the sqlite3TableLock() and
39 ** codeTableLocks() functions.
40 */
41 struct TableLock {
42   int iDb;             /* The database containing the table to be locked */
43   int iTab;            /* The root page of the table to be locked */
44   u8 isWriteLock;      /* True for write lock.  False for a read lock */
45   const char *zName;   /* Name of the table */
46 };
47 
48 /*
49 ** Record the fact that we want to lock a table at run-time.
50 **
51 ** The table to be locked has root page iTab and is found in database iDb.
52 ** A read or a write lock can be taken depending on isWritelock.
53 **
54 ** This routine just records the fact that the lock is desired.  The
55 ** code to make the lock occur is generated by a later call to
56 ** codeTableLocks() which occurs during sqlite3FinishCoding().
57 */
58 void sqlite3TableLock(
59   Parse *pParse,     /* Parsing context */
60   int iDb,           /* Index of the database containing the table to lock */
61   int iTab,          /* Root page number of the table to be locked */
62   u8 isWriteLock,    /* True for a write lock */
63   const char *zName  /* Name of the table to be locked */
64 ){
65   Parse *pToplevel = sqlite3ParseToplevel(pParse);
66   int i;
67   int nBytes;
68   TableLock *p;
69   assert( iDb>=0 );
70 
71   for(i=0; i<pToplevel->nTableLock; i++){
72     p = &pToplevel->aTableLock[i];
73     if( p->iDb==iDb && p->iTab==iTab ){
74       p->isWriteLock = (p->isWriteLock || isWriteLock);
75       return;
76     }
77   }
78 
79   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
80   pToplevel->aTableLock =
81       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
82   if( pToplevel->aTableLock ){
83     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
84     p->iDb = iDb;
85     p->iTab = iTab;
86     p->isWriteLock = isWriteLock;
87     p->zName = zName;
88   }else{
89     pToplevel->nTableLock = 0;
90     pToplevel->db->mallocFailed = 1;
91   }
92 }
93 
94 /*
95 ** Code an OP_TableLock instruction for each table locked by the
96 ** statement (configured by calls to sqlite3TableLock()).
97 */
98 static void codeTableLocks(Parse *pParse){
99   int i;
100   Vdbe *pVdbe;
101 
102   pVdbe = sqlite3GetVdbe(pParse);
103   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
104 
105   for(i=0; i<pParse->nTableLock; i++){
106     TableLock *p = &pParse->aTableLock[i];
107     int p1 = p->iDb;
108     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
109                       p->zName, P4_STATIC);
110   }
111 }
112 #else
113   #define codeTableLocks(x)
114 #endif
115 
116 /*
117 ** This routine is called after a single SQL statement has been
118 ** parsed and a VDBE program to execute that statement has been
119 ** prepared.  This routine puts the finishing touches on the
120 ** VDBE program and resets the pParse structure for the next
121 ** parse.
122 **
123 ** Note that if an error occurred, it might be the case that
124 ** no VDBE code was generated.
125 */
126 void sqlite3FinishCoding(Parse *pParse){
127   sqlite3 *db;
128   Vdbe *v;
129 
130   db = pParse->db;
131   if( db->mallocFailed ) return;
132   if( pParse->nested ) return;
133   if( pParse->nErr ) return;
134 
135   /* Begin by generating some termination code at the end of the
136   ** vdbe program
137   */
138   v = sqlite3GetVdbe(pParse);
139   assert( !pParse->isMultiWrite
140        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
141   if( v ){
142     sqlite3VdbeAddOp0(v, OP_Halt);
143 
144     /* The cookie mask contains one bit for each database file open.
145     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
146     ** set for each database that is used.  Generate code to start a
147     ** transaction on each used database and to verify the schema cookie
148     ** on each used database.
149     */
150     if( pParse->cookieGoto>0 ){
151       yDbMask mask;
152       int iDb;
153       sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
154       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
155         if( (mask & pParse->cookieMask)==0 ) continue;
156         sqlite3VdbeUsesBtree(v, iDb);
157         sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
158         if( db->init.busy==0 ){
159           assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
160           sqlite3VdbeAddOp3(v, OP_VerifyCookie,
161                             iDb, pParse->cookieValue[iDb],
162                             db->aDb[iDb].pSchema->iGeneration);
163         }
164       }
165 #ifndef SQLITE_OMIT_VIRTUALTABLE
166       {
167         int i;
168         for(i=0; i<pParse->nVtabLock; i++){
169           char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
170           sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
171         }
172         pParse->nVtabLock = 0;
173       }
174 #endif
175 
176       /* Once all the cookies have been verified and transactions opened,
177       ** obtain the required table-locks. This is a no-op unless the
178       ** shared-cache feature is enabled.
179       */
180       codeTableLocks(pParse);
181 
182       /* Initialize any AUTOINCREMENT data structures required.
183       */
184       sqlite3AutoincrementBegin(pParse);
185 
186       /* Finally, jump back to the beginning of the executable code. */
187       sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
188     }
189   }
190 
191 
192   /* Get the VDBE program ready for execution
193   */
194   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
195 #ifdef SQLITE_DEBUG
196     FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
197     sqlite3VdbeTrace(v, trace);
198 #endif
199     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
200     /* A minimum of one cursor is required if autoincrement is used
201     *  See ticket [a696379c1f08866] */
202     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
203     sqlite3VdbeMakeReady(v, pParse);
204     pParse->rc = SQLITE_DONE;
205     pParse->colNamesSet = 0;
206   }else{
207     pParse->rc = SQLITE_ERROR;
208   }
209   pParse->nTab = 0;
210   pParse->nMem = 0;
211   pParse->nSet = 0;
212   pParse->nVar = 0;
213   pParse->cookieMask = 0;
214   pParse->cookieGoto = 0;
215 }
216 
217 /*
218 ** Run the parser and code generator recursively in order to generate
219 ** code for the SQL statement given onto the end of the pParse context
220 ** currently under construction.  When the parser is run recursively
221 ** this way, the final OP_Halt is not appended and other initialization
222 ** and finalization steps are omitted because those are handling by the
223 ** outermost parser.
224 **
225 ** Not everything is nestable.  This facility is designed to permit
226 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
227 ** care if you decide to try to use this routine for some other purposes.
228 */
229 void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
230   va_list ap;
231   char *zSql;
232   char *zErrMsg = 0;
233   sqlite3 *db = pParse->db;
234 # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
235   char saveBuf[SAVE_SZ];
236 
237   if( pParse->nErr ) return;
238   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
239   va_start(ap, zFormat);
240   zSql = sqlite3VMPrintf(db, zFormat, ap);
241   va_end(ap);
242   if( zSql==0 ){
243     return;   /* A malloc must have failed */
244   }
245   pParse->nested++;
246   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
247   memset(&pParse->nVar, 0, SAVE_SZ);
248   sqlite3RunParser(pParse, zSql, &zErrMsg);
249   sqlite3DbFree(db, zErrMsg);
250   sqlite3DbFree(db, zSql);
251   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
252   pParse->nested--;
253 }
254 
255 /*
256 ** Locate the in-memory structure that describes a particular database
257 ** table given the name of that table and (optionally) the name of the
258 ** database containing the table.  Return NULL if not found.
259 **
260 ** If zDatabase is 0, all databases are searched for the table and the
261 ** first matching table is returned.  (No checking for duplicate table
262 ** names is done.)  The search order is TEMP first, then MAIN, then any
263 ** auxiliary databases added using the ATTACH command.
264 **
265 ** See also sqlite3LocateTable().
266 */
267 Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
268   Table *p = 0;
269   int i;
270   int nName;
271   assert( zName!=0 );
272   nName = sqlite3Strlen30(zName);
273   /* All mutexes are required for schema access.  Make sure we hold them. */
274   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
275   for(i=OMIT_TEMPDB; i<db->nDb; i++){
276     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
277     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
278     assert( sqlite3SchemaMutexHeld(db, j, 0) );
279     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
280     if( p ) break;
281   }
282   return p;
283 }
284 
285 /*
286 ** Locate the in-memory structure that describes a particular database
287 ** table given the name of that table and (optionally) the name of the
288 ** database containing the table.  Return NULL if not found.  Also leave an
289 ** error message in pParse->zErrMsg.
290 **
291 ** The difference between this routine and sqlite3FindTable() is that this
292 ** routine leaves an error message in pParse->zErrMsg where
293 ** sqlite3FindTable() does not.
294 */
295 Table *sqlite3LocateTable(
296   Parse *pParse,         /* context in which to report errors */
297   int isView,            /* True if looking for a VIEW rather than a TABLE */
298   const char *zName,     /* Name of the table we are looking for */
299   const char *zDbase     /* Name of the database.  Might be NULL */
300 ){
301   Table *p;
302 
303   /* Read the database schema. If an error occurs, leave an error message
304   ** and code in pParse and return NULL. */
305   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
306     return 0;
307   }
308 
309   p = sqlite3FindTable(pParse->db, zName, zDbase);
310   if( p==0 ){
311     const char *zMsg = isView ? "no such view" : "no such table";
312     if( zDbase ){
313       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
314     }else{
315       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
316     }
317     pParse->checkSchema = 1;
318   }
319   return p;
320 }
321 
322 /*
323 ** Locate the table identified by *p.
324 **
325 ** This is a wrapper around sqlite3LocateTable(). The difference between
326 ** sqlite3LocateTable() and this function is that this function restricts
327 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
328 ** non-NULL if it is part of a view or trigger program definition. See
329 ** sqlite3FixSrcList() for details.
330 */
331 Table *sqlite3LocateTableItem(
332   Parse *pParse,
333   int isView,
334   struct SrcList_item *p
335 ){
336   const char *zDb;
337   assert( p->pSchema==0 || p->zDatabase==0 );
338   if( p->pSchema ){
339     int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
340     zDb = pParse->db->aDb[iDb].zName;
341   }else{
342     zDb = p->zDatabase;
343   }
344   return sqlite3LocateTable(pParse, isView, p->zName, zDb);
345 }
346 
347 /*
348 ** Locate the in-memory structure that describes
349 ** a particular index given the name of that index
350 ** and the name of the database that contains the index.
351 ** Return NULL if not found.
352 **
353 ** If zDatabase is 0, all databases are searched for the
354 ** table and the first matching index is returned.  (No checking
355 ** for duplicate index names is done.)  The search order is
356 ** TEMP first, then MAIN, then any auxiliary databases added
357 ** using the ATTACH command.
358 */
359 Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
360   Index *p = 0;
361   int i;
362   int nName = sqlite3Strlen30(zName);
363   /* All mutexes are required for schema access.  Make sure we hold them. */
364   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
365   for(i=OMIT_TEMPDB; i<db->nDb; i++){
366     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
367     Schema *pSchema = db->aDb[j].pSchema;
368     assert( pSchema );
369     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
370     assert( sqlite3SchemaMutexHeld(db, j, 0) );
371     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
372     if( p ) break;
373   }
374   return p;
375 }
376 
377 /*
378 ** Reclaim the memory used by an index
379 */
380 static void freeIndex(sqlite3 *db, Index *p){
381 #ifndef SQLITE_OMIT_ANALYZE
382   sqlite3DeleteIndexSamples(db, p);
383 #endif
384   sqlite3DbFree(db, p->zColAff);
385   sqlite3DbFree(db, p);
386 }
387 
388 /*
389 ** For the index called zIdxName which is found in the database iDb,
390 ** unlike that index from its Table then remove the index from
391 ** the index hash table and free all memory structures associated
392 ** with the index.
393 */
394 void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
395   Index *pIndex;
396   int len;
397   Hash *pHash;
398 
399   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
400   pHash = &db->aDb[iDb].pSchema->idxHash;
401   len = sqlite3Strlen30(zIdxName);
402   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
403   if( ALWAYS(pIndex) ){
404     if( pIndex->pTable->pIndex==pIndex ){
405       pIndex->pTable->pIndex = pIndex->pNext;
406     }else{
407       Index *p;
408       /* Justification of ALWAYS();  The index must be on the list of
409       ** indices. */
410       p = pIndex->pTable->pIndex;
411       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
412       if( ALWAYS(p && p->pNext==pIndex) ){
413         p->pNext = pIndex->pNext;
414       }
415     }
416     freeIndex(db, pIndex);
417   }
418   db->flags |= SQLITE_InternChanges;
419 }
420 
421 /*
422 ** Look through the list of open database files in db->aDb[] and if
423 ** any have been closed, remove them from the list.  Reallocate the
424 ** db->aDb[] structure to a smaller size, if possible.
425 **
426 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
427 ** are never candidates for being collapsed.
428 */
429 void sqlite3CollapseDatabaseArray(sqlite3 *db){
430   int i, j;
431   for(i=j=2; i<db->nDb; i++){
432     struct Db *pDb = &db->aDb[i];
433     if( pDb->pBt==0 ){
434       sqlite3DbFree(db, pDb->zName);
435       pDb->zName = 0;
436       continue;
437     }
438     if( j<i ){
439       db->aDb[j] = db->aDb[i];
440     }
441     j++;
442   }
443   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
444   db->nDb = j;
445   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
446     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
447     sqlite3DbFree(db, db->aDb);
448     db->aDb = db->aDbStatic;
449   }
450 }
451 
452 /*
453 ** Reset the schema for the database at index iDb.  Also reset the
454 ** TEMP schema.
455 */
456 void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
457   Db *pDb;
458   assert( iDb<db->nDb );
459 
460   /* Case 1:  Reset the single schema identified by iDb */
461   pDb = &db->aDb[iDb];
462   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
463   assert( pDb->pSchema!=0 );
464   sqlite3SchemaClear(pDb->pSchema);
465 
466   /* If any database other than TEMP is reset, then also reset TEMP
467   ** since TEMP might be holding triggers that reference tables in the
468   ** other database.
469   */
470   if( iDb!=1 ){
471     pDb = &db->aDb[1];
472     assert( pDb->pSchema!=0 );
473     sqlite3SchemaClear(pDb->pSchema);
474   }
475   return;
476 }
477 
478 /*
479 ** Erase all schema information from all attached databases (including
480 ** "main" and "temp") for a single database connection.
481 */
482 void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
483   int i;
484   sqlite3BtreeEnterAll(db);
485   for(i=0; i<db->nDb; i++){
486     Db *pDb = &db->aDb[i];
487     if( pDb->pSchema ){
488       sqlite3SchemaClear(pDb->pSchema);
489     }
490   }
491   db->flags &= ~SQLITE_InternChanges;
492   sqlite3VtabUnlockList(db);
493   sqlite3BtreeLeaveAll(db);
494   sqlite3CollapseDatabaseArray(db);
495 }
496 
497 /*
498 ** This routine is called when a commit occurs.
499 */
500 void sqlite3CommitInternalChanges(sqlite3 *db){
501   db->flags &= ~SQLITE_InternChanges;
502 }
503 
504 /*
505 ** Delete memory allocated for the column names of a table or view (the
506 ** Table.aCol[] array).
507 */
508 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
509   int i;
510   Column *pCol;
511   assert( pTable!=0 );
512   if( (pCol = pTable->aCol)!=0 ){
513     for(i=0; i<pTable->nCol; i++, pCol++){
514       sqlite3DbFree(db, pCol->zName);
515       sqlite3ExprDelete(db, pCol->pDflt);
516       sqlite3DbFree(db, pCol->zDflt);
517       sqlite3DbFree(db, pCol->zType);
518       sqlite3DbFree(db, pCol->zColl);
519     }
520     sqlite3DbFree(db, pTable->aCol);
521   }
522 }
523 
524 /*
525 ** Remove the memory data structures associated with the given
526 ** Table.  No changes are made to disk by this routine.
527 **
528 ** This routine just deletes the data structure.  It does not unlink
529 ** the table data structure from the hash table.  But it does destroy
530 ** memory structures of the indices and foreign keys associated with
531 ** the table.
532 **
533 ** The db parameter is optional.  It is needed if the Table object
534 ** contains lookaside memory.  (Table objects in the schema do not use
535 ** lookaside memory, but some ephemeral Table objects do.)  Or the
536 ** db parameter can be used with db->pnBytesFreed to measure the memory
537 ** used by the Table object.
538 */
539 void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
540   Index *pIndex, *pNext;
541   TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
542 
543   assert( !pTable || pTable->nRef>0 );
544 
545   /* Do not delete the table until the reference count reaches zero. */
546   if( !pTable ) return;
547   if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
548 
549   /* Record the number of outstanding lookaside allocations in schema Tables
550   ** prior to doing any free() operations.  Since schema Tables do not use
551   ** lookaside, this number should not change. */
552   TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
553                          db->lookaside.nOut : 0 );
554 
555   /* Delete all indices associated with this table. */
556   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
557     pNext = pIndex->pNext;
558     assert( pIndex->pSchema==pTable->pSchema );
559     if( !db || db->pnBytesFreed==0 ){
560       char *zName = pIndex->zName;
561       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
562          &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
563       );
564       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
565       assert( pOld==pIndex || pOld==0 );
566     }
567     freeIndex(db, pIndex);
568   }
569 
570   /* Delete any foreign keys attached to this table. */
571   sqlite3FkDelete(db, pTable);
572 
573   /* Delete the Table structure itself.
574   */
575   sqliteDeleteColumnNames(db, pTable);
576   sqlite3DbFree(db, pTable->zName);
577   sqlite3DbFree(db, pTable->zColAff);
578   sqlite3SelectDelete(db, pTable->pSelect);
579 #ifndef SQLITE_OMIT_CHECK
580   sqlite3ExprListDelete(db, pTable->pCheck);
581 #endif
582 #ifndef SQLITE_OMIT_VIRTUALTABLE
583   sqlite3VtabClear(db, pTable);
584 #endif
585   sqlite3DbFree(db, pTable);
586 
587   /* Verify that no lookaside memory was used by schema tables */
588   assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
589 }
590 
591 /*
592 ** Unlink the given table from the hash tables and the delete the
593 ** table structure with all its indices and foreign keys.
594 */
595 void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
596   Table *p;
597   Db *pDb;
598 
599   assert( db!=0 );
600   assert( iDb>=0 && iDb<db->nDb );
601   assert( zTabName );
602   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
603   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
604   pDb = &db->aDb[iDb];
605   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
606                         sqlite3Strlen30(zTabName),0);
607   sqlite3DeleteTable(db, p);
608   db->flags |= SQLITE_InternChanges;
609 }
610 
611 /*
612 ** Given a token, return a string that consists of the text of that
613 ** token.  Space to hold the returned string
614 ** is obtained from sqliteMalloc() and must be freed by the calling
615 ** function.
616 **
617 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
618 ** surround the body of the token are removed.
619 **
620 ** Tokens are often just pointers into the original SQL text and so
621 ** are not \000 terminated and are not persistent.  The returned string
622 ** is \000 terminated and is persistent.
623 */
624 char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
625   char *zName;
626   if( pName ){
627     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
628     sqlite3Dequote(zName);
629   }else{
630     zName = 0;
631   }
632   return zName;
633 }
634 
635 /*
636 ** Open the sqlite_master table stored in database number iDb for
637 ** writing. The table is opened using cursor 0.
638 */
639 void sqlite3OpenMasterTable(Parse *p, int iDb){
640   Vdbe *v = sqlite3GetVdbe(p);
641   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
642   sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb);
643   sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32);  /* 5 column table */
644   if( p->nTab==0 ){
645     p->nTab = 1;
646   }
647 }
648 
649 /*
650 ** Parameter zName points to a nul-terminated buffer containing the name
651 ** of a database ("main", "temp" or the name of an attached db). This
652 ** function returns the index of the named database in db->aDb[], or
653 ** -1 if the named db cannot be found.
654 */
655 int sqlite3FindDbName(sqlite3 *db, const char *zName){
656   int i = -1;         /* Database number */
657   if( zName ){
658     Db *pDb;
659     int n = sqlite3Strlen30(zName);
660     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
661       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
662           0==sqlite3StrICmp(pDb->zName, zName) ){
663         break;
664       }
665     }
666   }
667   return i;
668 }
669 
670 /*
671 ** The token *pName contains the name of a database (either "main" or
672 ** "temp" or the name of an attached db). This routine returns the
673 ** index of the named database in db->aDb[], or -1 if the named db
674 ** does not exist.
675 */
676 int sqlite3FindDb(sqlite3 *db, Token *pName){
677   int i;                               /* Database number */
678   char *zName;                         /* Name we are searching for */
679   zName = sqlite3NameFromToken(db, pName);
680   i = sqlite3FindDbName(db, zName);
681   sqlite3DbFree(db, zName);
682   return i;
683 }
684 
685 /* The table or view or trigger name is passed to this routine via tokens
686 ** pName1 and pName2. If the table name was fully qualified, for example:
687 **
688 ** CREATE TABLE xxx.yyy (...);
689 **
690 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
691 ** the table name is not fully qualified, i.e.:
692 **
693 ** CREATE TABLE yyy(...);
694 **
695 ** Then pName1 is set to "yyy" and pName2 is "".
696 **
697 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
698 ** pName2) that stores the unqualified table name.  The index of the
699 ** database "xxx" is returned.
700 */
701 int sqlite3TwoPartName(
702   Parse *pParse,      /* Parsing and code generating context */
703   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
704   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
705   Token **pUnqual     /* Write the unqualified object name here */
706 ){
707   int iDb;                    /* Database holding the object */
708   sqlite3 *db = pParse->db;
709 
710   if( ALWAYS(pName2!=0) && pName2->n>0 ){
711     if( db->init.busy ) {
712       sqlite3ErrorMsg(pParse, "corrupt database");
713       pParse->nErr++;
714       return -1;
715     }
716     *pUnqual = pName2;
717     iDb = sqlite3FindDb(db, pName1);
718     if( iDb<0 ){
719       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
720       pParse->nErr++;
721       return -1;
722     }
723   }else{
724     assert( db->init.iDb==0 || db->init.busy );
725     iDb = db->init.iDb;
726     *pUnqual = pName1;
727   }
728   return iDb;
729 }
730 
731 /*
732 ** This routine is used to check if the UTF-8 string zName is a legal
733 ** unqualified name for a new schema object (table, index, view or
734 ** trigger). All names are legal except those that begin with the string
735 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
736 ** is reserved for internal use.
737 */
738 int sqlite3CheckObjectName(Parse *pParse, const char *zName){
739   if( !pParse->db->init.busy && pParse->nested==0
740           && (pParse->db->flags & SQLITE_WriteSchema)==0
741           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
742     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
743     return SQLITE_ERROR;
744   }
745   return SQLITE_OK;
746 }
747 
748 /*
749 ** Begin constructing a new table representation in memory.  This is
750 ** the first of several action routines that get called in response
751 ** to a CREATE TABLE statement.  In particular, this routine is called
752 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
753 ** flag is true if the table should be stored in the auxiliary database
754 ** file instead of in the main database file.  This is normally the case
755 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
756 ** CREATE and TABLE.
757 **
758 ** The new table record is initialized and put in pParse->pNewTable.
759 ** As more of the CREATE TABLE statement is parsed, additional action
760 ** routines will be called to add more information to this record.
761 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
762 ** is called to complete the construction of the new table record.
763 */
764 void sqlite3StartTable(
765   Parse *pParse,   /* Parser context */
766   Token *pName1,   /* First part of the name of the table or view */
767   Token *pName2,   /* Second part of the name of the table or view */
768   int isTemp,      /* True if this is a TEMP table */
769   int isView,      /* True if this is a VIEW */
770   int isVirtual,   /* True if this is a VIRTUAL table */
771   int noErr        /* Do nothing if table already exists */
772 ){
773   Table *pTable;
774   char *zName = 0; /* The name of the new table */
775   sqlite3 *db = pParse->db;
776   Vdbe *v;
777   int iDb;         /* Database number to create the table in */
778   Token *pName;    /* Unqualified name of the table to create */
779 
780   /* The table or view name to create is passed to this routine via tokens
781   ** pName1 and pName2. If the table name was fully qualified, for example:
782   **
783   ** CREATE TABLE xxx.yyy (...);
784   **
785   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
786   ** the table name is not fully qualified, i.e.:
787   **
788   ** CREATE TABLE yyy(...);
789   **
790   ** Then pName1 is set to "yyy" and pName2 is "".
791   **
792   ** The call below sets the pName pointer to point at the token (pName1 or
793   ** pName2) that stores the unqualified table name. The variable iDb is
794   ** set to the index of the database that the table or view is to be
795   ** created in.
796   */
797   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
798   if( iDb<0 ) return;
799   if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
800     /* If creating a temp table, the name may not be qualified. Unless
801     ** the database name is "temp" anyway.  */
802     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
803     return;
804   }
805   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
806 
807   pParse->sNameToken = *pName;
808   zName = sqlite3NameFromToken(db, pName);
809   if( zName==0 ) return;
810   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
811     goto begin_table_error;
812   }
813   if( db->init.iDb==1 ) isTemp = 1;
814 #ifndef SQLITE_OMIT_AUTHORIZATION
815   assert( (isTemp & 1)==isTemp );
816   {
817     int code;
818     char *zDb = db->aDb[iDb].zName;
819     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
820       goto begin_table_error;
821     }
822     if( isView ){
823       if( !OMIT_TEMPDB && isTemp ){
824         code = SQLITE_CREATE_TEMP_VIEW;
825       }else{
826         code = SQLITE_CREATE_VIEW;
827       }
828     }else{
829       if( !OMIT_TEMPDB && isTemp ){
830         code = SQLITE_CREATE_TEMP_TABLE;
831       }else{
832         code = SQLITE_CREATE_TABLE;
833       }
834     }
835     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
836       goto begin_table_error;
837     }
838   }
839 #endif
840 
841   /* Make sure the new table name does not collide with an existing
842   ** index or table name in the same database.  Issue an error message if
843   ** it does. The exception is if the statement being parsed was passed
844   ** to an sqlite3_declare_vtab() call. In that case only the column names
845   ** and types will be used, so there is no need to test for namespace
846   ** collisions.
847   */
848   if( !IN_DECLARE_VTAB ){
849     char *zDb = db->aDb[iDb].zName;
850     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
851       goto begin_table_error;
852     }
853     pTable = sqlite3FindTable(db, zName, zDb);
854     if( pTable ){
855       if( !noErr ){
856         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
857       }else{
858         assert( !db->init.busy );
859         sqlite3CodeVerifySchema(pParse, iDb);
860       }
861       goto begin_table_error;
862     }
863     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
864       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
865       goto begin_table_error;
866     }
867   }
868 
869   pTable = sqlite3DbMallocZero(db, sizeof(Table));
870   if( pTable==0 ){
871     db->mallocFailed = 1;
872     pParse->rc = SQLITE_NOMEM;
873     pParse->nErr++;
874     goto begin_table_error;
875   }
876   pTable->zName = zName;
877   pTable->iPKey = -1;
878   pTable->pSchema = db->aDb[iDb].pSchema;
879   pTable->nRef = 1;
880   pTable->nRowEst = 1000000;
881   assert( pParse->pNewTable==0 );
882   pParse->pNewTable = pTable;
883 
884   /* If this is the magic sqlite_sequence table used by autoincrement,
885   ** then record a pointer to this table in the main database structure
886   ** so that INSERT can find the table easily.
887   */
888 #ifndef SQLITE_OMIT_AUTOINCREMENT
889   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
890     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
891     pTable->pSchema->pSeqTab = pTable;
892   }
893 #endif
894 
895   /* Begin generating the code that will insert the table record into
896   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
897   ** and allocate the record number for the table entry now.  Before any
898   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
899   ** indices to be created and the table record must come before the
900   ** indices.  Hence, the record number for the table must be allocated
901   ** now.
902   */
903   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
904     int j1;
905     int fileFormat;
906     int reg1, reg2, reg3;
907     sqlite3BeginWriteOperation(pParse, 0, iDb);
908 
909 #ifndef SQLITE_OMIT_VIRTUALTABLE
910     if( isVirtual ){
911       sqlite3VdbeAddOp0(v, OP_VBegin);
912     }
913 #endif
914 
915     /* If the file format and encoding in the database have not been set,
916     ** set them now.
917     */
918     reg1 = pParse->regRowid = ++pParse->nMem;
919     reg2 = pParse->regRoot = ++pParse->nMem;
920     reg3 = ++pParse->nMem;
921     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
922     sqlite3VdbeUsesBtree(v, iDb);
923     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
924     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
925                   1 : SQLITE_MAX_FILE_FORMAT;
926     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
927     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
928     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
929     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
930     sqlite3VdbeJumpHere(v, j1);
931 
932     /* This just creates a place-holder record in the sqlite_master table.
933     ** The record created does not contain anything yet.  It will be replaced
934     ** by the real entry in code generated at sqlite3EndTable().
935     **
936     ** The rowid for the new entry is left in register pParse->regRowid.
937     ** The root page number of the new table is left in reg pParse->regRoot.
938     ** The rowid and root page number values are needed by the code that
939     ** sqlite3EndTable will generate.
940     */
941 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
942     if( isView || isVirtual ){
943       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
944     }else
945 #endif
946     {
947       sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
948     }
949     sqlite3OpenMasterTable(pParse, iDb);
950     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
951     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
952     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
953     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
954     sqlite3VdbeAddOp0(v, OP_Close);
955   }
956 
957   /* Normal (non-error) return. */
958   return;
959 
960   /* If an error occurs, we jump here */
961 begin_table_error:
962   sqlite3DbFree(db, zName);
963   return;
964 }
965 
966 /*
967 ** This macro is used to compare two strings in a case-insensitive manner.
968 ** It is slightly faster than calling sqlite3StrICmp() directly, but
969 ** produces larger code.
970 **
971 ** WARNING: This macro is not compatible with the strcmp() family. It
972 ** returns true if the two strings are equal, otherwise false.
973 */
974 #define STRICMP(x, y) (\
975 sqlite3UpperToLower[*(unsigned char *)(x)]==   \
976 sqlite3UpperToLower[*(unsigned char *)(y)]     \
977 && sqlite3StrICmp((x)+1,(y)+1)==0 )
978 
979 /*
980 ** Add a new column to the table currently being constructed.
981 **
982 ** The parser calls this routine once for each column declaration
983 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
984 ** first to get things going.  Then this routine is called for each
985 ** column.
986 */
987 void sqlite3AddColumn(Parse *pParse, Token *pName){
988   Table *p;
989   int i;
990   char *z;
991   Column *pCol;
992   sqlite3 *db = pParse->db;
993   if( (p = pParse->pNewTable)==0 ) return;
994 #if SQLITE_MAX_COLUMN
995   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
996     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
997     return;
998   }
999 #endif
1000   z = sqlite3NameFromToken(db, pName);
1001   if( z==0 ) return;
1002   for(i=0; i<p->nCol; i++){
1003     if( STRICMP(z, p->aCol[i].zName) ){
1004       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
1005       sqlite3DbFree(db, z);
1006       return;
1007     }
1008   }
1009   if( (p->nCol & 0x7)==0 ){
1010     Column *aNew;
1011     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
1012     if( aNew==0 ){
1013       sqlite3DbFree(db, z);
1014       return;
1015     }
1016     p->aCol = aNew;
1017   }
1018   pCol = &p->aCol[p->nCol];
1019   memset(pCol, 0, sizeof(p->aCol[0]));
1020   pCol->zName = z;
1021 
1022   /* If there is no type specified, columns have the default affinity
1023   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
1024   ** be called next to set pCol->affinity correctly.
1025   */
1026   pCol->affinity = SQLITE_AFF_NONE;
1027   p->nCol++;
1028 }
1029 
1030 /*
1031 ** This routine is called by the parser while in the middle of
1032 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
1033 ** been seen on a column.  This routine sets the notNull flag on
1034 ** the column currently under construction.
1035 */
1036 void sqlite3AddNotNull(Parse *pParse, int onError){
1037   Table *p;
1038   p = pParse->pNewTable;
1039   if( p==0 || NEVER(p->nCol<1) ) return;
1040   p->aCol[p->nCol-1].notNull = (u8)onError;
1041 }
1042 
1043 /*
1044 ** Scan the column type name zType (length nType) and return the
1045 ** associated affinity type.
1046 **
1047 ** This routine does a case-independent search of zType for the
1048 ** substrings in the following table. If one of the substrings is
1049 ** found, the corresponding affinity is returned. If zType contains
1050 ** more than one of the substrings, entries toward the top of
1051 ** the table take priority. For example, if zType is 'BLOBINT',
1052 ** SQLITE_AFF_INTEGER is returned.
1053 **
1054 ** Substring     | Affinity
1055 ** --------------------------------
1056 ** 'INT'         | SQLITE_AFF_INTEGER
1057 ** 'CHAR'        | SQLITE_AFF_TEXT
1058 ** 'CLOB'        | SQLITE_AFF_TEXT
1059 ** 'TEXT'        | SQLITE_AFF_TEXT
1060 ** 'BLOB'        | SQLITE_AFF_NONE
1061 ** 'REAL'        | SQLITE_AFF_REAL
1062 ** 'FLOA'        | SQLITE_AFF_REAL
1063 ** 'DOUB'        | SQLITE_AFF_REAL
1064 **
1065 ** If none of the substrings in the above table are found,
1066 ** SQLITE_AFF_NUMERIC is returned.
1067 */
1068 char sqlite3AffinityType(const char *zIn){
1069   u32 h = 0;
1070   char aff = SQLITE_AFF_NUMERIC;
1071 
1072   if( zIn ) while( zIn[0] ){
1073     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
1074     zIn++;
1075     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
1076       aff = SQLITE_AFF_TEXT;
1077     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
1078       aff = SQLITE_AFF_TEXT;
1079     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
1080       aff = SQLITE_AFF_TEXT;
1081     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
1082         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
1083       aff = SQLITE_AFF_NONE;
1084 #ifndef SQLITE_OMIT_FLOATING_POINT
1085     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
1086         && aff==SQLITE_AFF_NUMERIC ){
1087       aff = SQLITE_AFF_REAL;
1088     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
1089         && aff==SQLITE_AFF_NUMERIC ){
1090       aff = SQLITE_AFF_REAL;
1091     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
1092         && aff==SQLITE_AFF_NUMERIC ){
1093       aff = SQLITE_AFF_REAL;
1094 #endif
1095     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
1096       aff = SQLITE_AFF_INTEGER;
1097       break;
1098     }
1099   }
1100 
1101   return aff;
1102 }
1103 
1104 /*
1105 ** This routine is called by the parser while in the middle of
1106 ** parsing a CREATE TABLE statement.  The pFirst token is the first
1107 ** token in the sequence of tokens that describe the type of the
1108 ** column currently under construction.   pLast is the last token
1109 ** in the sequence.  Use this information to construct a string
1110 ** that contains the typename of the column and store that string
1111 ** in zType.
1112 */
1113 void sqlite3AddColumnType(Parse *pParse, Token *pType){
1114   Table *p;
1115   Column *pCol;
1116 
1117   p = pParse->pNewTable;
1118   if( p==0 || NEVER(p->nCol<1) ) return;
1119   pCol = &p->aCol[p->nCol-1];
1120   assert( pCol->zType==0 );
1121   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
1122   pCol->affinity = sqlite3AffinityType(pCol->zType);
1123 }
1124 
1125 /*
1126 ** The expression is the default value for the most recently added column
1127 ** of the table currently under construction.
1128 **
1129 ** Default value expressions must be constant.  Raise an exception if this
1130 ** is not the case.
1131 **
1132 ** This routine is called by the parser while in the middle of
1133 ** parsing a CREATE TABLE statement.
1134 */
1135 void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
1136   Table *p;
1137   Column *pCol;
1138   sqlite3 *db = pParse->db;
1139   p = pParse->pNewTable;
1140   if( p!=0 ){
1141     pCol = &(p->aCol[p->nCol-1]);
1142     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
1143       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
1144           pCol->zName);
1145     }else{
1146       /* A copy of pExpr is used instead of the original, as pExpr contains
1147       ** tokens that point to volatile memory. The 'span' of the expression
1148       ** is required by pragma table_info.
1149       */
1150       sqlite3ExprDelete(db, pCol->pDflt);
1151       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
1152       sqlite3DbFree(db, pCol->zDflt);
1153       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
1154                                      (int)(pSpan->zEnd - pSpan->zStart));
1155     }
1156   }
1157   sqlite3ExprDelete(db, pSpan->pExpr);
1158 }
1159 
1160 /*
1161 ** Designate the PRIMARY KEY for the table.  pList is a list of names
1162 ** of columns that form the primary key.  If pList is NULL, then the
1163 ** most recently added column of the table is the primary key.
1164 **
1165 ** A table can have at most one primary key.  If the table already has
1166 ** a primary key (and this is the second primary key) then create an
1167 ** error.
1168 **
1169 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
1170 ** then we will try to use that column as the rowid.  Set the Table.iPKey
1171 ** field of the table under construction to be the index of the
1172 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
1173 ** no INTEGER PRIMARY KEY.
1174 **
1175 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
1176 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
1177 */
1178 void sqlite3AddPrimaryKey(
1179   Parse *pParse,    /* Parsing context */
1180   ExprList *pList,  /* List of field names to be indexed */
1181   int onError,      /* What to do with a uniqueness conflict */
1182   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
1183   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
1184 ){
1185   Table *pTab = pParse->pNewTable;
1186   char *zType = 0;
1187   int iCol = -1, i;
1188   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
1189   if( pTab->tabFlags & TF_HasPrimaryKey ){
1190     sqlite3ErrorMsg(pParse,
1191       "table \"%s\" has more than one primary key", pTab->zName);
1192     goto primary_key_exit;
1193   }
1194   pTab->tabFlags |= TF_HasPrimaryKey;
1195   if( pList==0 ){
1196     iCol = pTab->nCol - 1;
1197     pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
1198   }else{
1199     for(i=0; i<pList->nExpr; i++){
1200       for(iCol=0; iCol<pTab->nCol; iCol++){
1201         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
1202           break;
1203         }
1204       }
1205       if( iCol<pTab->nCol ){
1206         pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
1207       }
1208     }
1209     if( pList->nExpr>1 ) iCol = -1;
1210   }
1211   if( iCol>=0 && iCol<pTab->nCol ){
1212     zType = pTab->aCol[iCol].zType;
1213   }
1214   if( zType && sqlite3StrICmp(zType, "INTEGER")==0
1215         && sortOrder==SQLITE_SO_ASC ){
1216     pTab->iPKey = iCol;
1217     pTab->keyConf = (u8)onError;
1218     assert( autoInc==0 || autoInc==1 );
1219     pTab->tabFlags |= autoInc*TF_Autoincrement;
1220   }else if( autoInc ){
1221 #ifndef SQLITE_OMIT_AUTOINCREMENT
1222     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
1223        "INTEGER PRIMARY KEY");
1224 #endif
1225   }else{
1226     Index *p;
1227     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
1228     if( p ){
1229       p->autoIndex = 2;
1230     }
1231     pList = 0;
1232   }
1233 
1234 primary_key_exit:
1235   sqlite3ExprListDelete(pParse->db, pList);
1236   return;
1237 }
1238 
1239 /*
1240 ** Add a new CHECK constraint to the table currently under construction.
1241 */
1242 void sqlite3AddCheckConstraint(
1243   Parse *pParse,    /* Parsing context */
1244   Expr *pCheckExpr  /* The check expression */
1245 ){
1246 #ifndef SQLITE_OMIT_CHECK
1247   Table *pTab = pParse->pNewTable;
1248   if( pTab && !IN_DECLARE_VTAB ){
1249     pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
1250     if( pParse->constraintName.n ){
1251       sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
1252     }
1253   }else
1254 #endif
1255   {
1256     sqlite3ExprDelete(pParse->db, pCheckExpr);
1257   }
1258 }
1259 
1260 /*
1261 ** Set the collation function of the most recently parsed table column
1262 ** to the CollSeq given.
1263 */
1264 void sqlite3AddCollateType(Parse *pParse, Token *pToken){
1265   Table *p;
1266   int i;
1267   char *zColl;              /* Dequoted name of collation sequence */
1268   sqlite3 *db;
1269 
1270   if( (p = pParse->pNewTable)==0 ) return;
1271   i = p->nCol-1;
1272   db = pParse->db;
1273   zColl = sqlite3NameFromToken(db, pToken);
1274   if( !zColl ) return;
1275 
1276   if( sqlite3LocateCollSeq(pParse, zColl) ){
1277     Index *pIdx;
1278     p->aCol[i].zColl = zColl;
1279 
1280     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
1281     ** then an index may have been created on this column before the
1282     ** collation type was added. Correct this if it is the case.
1283     */
1284     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
1285       assert( pIdx->nColumn==1 );
1286       if( pIdx->aiColumn[0]==i ){
1287         pIdx->azColl[0] = p->aCol[i].zColl;
1288       }
1289     }
1290   }else{
1291     sqlite3DbFree(db, zColl);
1292   }
1293 }
1294 
1295 /*
1296 ** This function returns the collation sequence for database native text
1297 ** encoding identified by the string zName, length nName.
1298 **
1299 ** If the requested collation sequence is not available, or not available
1300 ** in the database native encoding, the collation factory is invoked to
1301 ** request it. If the collation factory does not supply such a sequence,
1302 ** and the sequence is available in another text encoding, then that is
1303 ** returned instead.
1304 **
1305 ** If no versions of the requested collations sequence are available, or
1306 ** another error occurs, NULL is returned and an error message written into
1307 ** pParse.
1308 **
1309 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
1310 ** invokes the collation factory if the named collation cannot be found
1311 ** and generates an error message.
1312 **
1313 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
1314 */
1315 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
1316   sqlite3 *db = pParse->db;
1317   u8 enc = ENC(db);
1318   u8 initbusy = db->init.busy;
1319   CollSeq *pColl;
1320 
1321   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
1322   if( !initbusy && (!pColl || !pColl->xCmp) ){
1323     pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
1324   }
1325 
1326   return pColl;
1327 }
1328 
1329 
1330 /*
1331 ** Generate code that will increment the schema cookie.
1332 **
1333 ** The schema cookie is used to determine when the schema for the
1334 ** database changes.  After each schema change, the cookie value
1335 ** changes.  When a process first reads the schema it records the
1336 ** cookie.  Thereafter, whenever it goes to access the database,
1337 ** it checks the cookie to make sure the schema has not changed
1338 ** since it was last read.
1339 **
1340 ** This plan is not completely bullet-proof.  It is possible for
1341 ** the schema to change multiple times and for the cookie to be
1342 ** set back to prior value.  But schema changes are infrequent
1343 ** and the probability of hitting the same cookie value is only
1344 ** 1 chance in 2^32.  So we're safe enough.
1345 */
1346 void sqlite3ChangeCookie(Parse *pParse, int iDb){
1347   int r1 = sqlite3GetTempReg(pParse);
1348   sqlite3 *db = pParse->db;
1349   Vdbe *v = pParse->pVdbe;
1350   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1351   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
1352   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
1353   sqlite3ReleaseTempReg(pParse, r1);
1354 }
1355 
1356 /*
1357 ** Measure the number of characters needed to output the given
1358 ** identifier.  The number returned includes any quotes used
1359 ** but does not include the null terminator.
1360 **
1361 ** The estimate is conservative.  It might be larger that what is
1362 ** really needed.
1363 */
1364 static int identLength(const char *z){
1365   int n;
1366   for(n=0; *z; n++, z++){
1367     if( *z=='"' ){ n++; }
1368   }
1369   return n + 2;
1370 }
1371 
1372 /*
1373 ** The first parameter is a pointer to an output buffer. The second
1374 ** parameter is a pointer to an integer that contains the offset at
1375 ** which to write into the output buffer. This function copies the
1376 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
1377 ** to the specified offset in the buffer and updates *pIdx to refer
1378 ** to the first byte after the last byte written before returning.
1379 **
1380 ** If the string zSignedIdent consists entirely of alpha-numeric
1381 ** characters, does not begin with a digit and is not an SQL keyword,
1382 ** then it is copied to the output buffer exactly as it is. Otherwise,
1383 ** it is quoted using double-quotes.
1384 */
1385 static void identPut(char *z, int *pIdx, char *zSignedIdent){
1386   unsigned char *zIdent = (unsigned char*)zSignedIdent;
1387   int i, j, needQuote;
1388   i = *pIdx;
1389 
1390   for(j=0; zIdent[j]; j++){
1391     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1392   }
1393   needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID;
1394   if( !needQuote ){
1395     needQuote = zIdent[j];
1396   }
1397 
1398   if( needQuote ) z[i++] = '"';
1399   for(j=0; zIdent[j]; j++){
1400     z[i++] = zIdent[j];
1401     if( zIdent[j]=='"' ) z[i++] = '"';
1402   }
1403   if( needQuote ) z[i++] = '"';
1404   z[i] = 0;
1405   *pIdx = i;
1406 }
1407 
1408 /*
1409 ** Generate a CREATE TABLE statement appropriate for the given
1410 ** table.  Memory to hold the text of the statement is obtained
1411 ** from sqliteMalloc() and must be freed by the calling function.
1412 */
1413 static char *createTableStmt(sqlite3 *db, Table *p){
1414   int i, k, n;
1415   char *zStmt;
1416   char *zSep, *zSep2, *zEnd;
1417   Column *pCol;
1418   n = 0;
1419   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1420     n += identLength(pCol->zName) + 5;
1421   }
1422   n += identLength(p->zName);
1423   if( n<50 ){
1424     zSep = "";
1425     zSep2 = ",";
1426     zEnd = ")";
1427   }else{
1428     zSep = "\n  ";
1429     zSep2 = ",\n  ";
1430     zEnd = "\n)";
1431   }
1432   n += 35 + 6*p->nCol;
1433   zStmt = sqlite3DbMallocRaw(0, n);
1434   if( zStmt==0 ){
1435     db->mallocFailed = 1;
1436     return 0;
1437   }
1438   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
1439   k = sqlite3Strlen30(zStmt);
1440   identPut(zStmt, &k, p->zName);
1441   zStmt[k++] = '(';
1442   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
1443     static const char * const azType[] = {
1444         /* SQLITE_AFF_TEXT    */ " TEXT",
1445         /* SQLITE_AFF_NONE    */ "",
1446         /* SQLITE_AFF_NUMERIC */ " NUM",
1447         /* SQLITE_AFF_INTEGER */ " INT",
1448         /* SQLITE_AFF_REAL    */ " REAL"
1449     };
1450     int len;
1451     const char *zType;
1452 
1453     sqlite3_snprintf(n-k, &zStmt[k], zSep);
1454     k += sqlite3Strlen30(&zStmt[k]);
1455     zSep = zSep2;
1456     identPut(zStmt, &k, pCol->zName);
1457     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
1458     assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
1459     testcase( pCol->affinity==SQLITE_AFF_TEXT );
1460     testcase( pCol->affinity==SQLITE_AFF_NONE );
1461     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
1462     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
1463     testcase( pCol->affinity==SQLITE_AFF_REAL );
1464 
1465     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
1466     len = sqlite3Strlen30(zType);
1467     assert( pCol->affinity==SQLITE_AFF_NONE
1468             || pCol->affinity==sqlite3AffinityType(zType) );
1469     memcpy(&zStmt[k], zType, len);
1470     k += len;
1471     assert( k<=n );
1472   }
1473   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
1474   return zStmt;
1475 }
1476 
1477 /*
1478 ** This routine is called to report the final ")" that terminates
1479 ** a CREATE TABLE statement.
1480 **
1481 ** The table structure that other action routines have been building
1482 ** is added to the internal hash tables, assuming no errors have
1483 ** occurred.
1484 **
1485 ** An entry for the table is made in the master table on disk, unless
1486 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
1487 ** it means we are reading the sqlite_master table because we just
1488 ** connected to the database or because the sqlite_master table has
1489 ** recently changed, so the entry for this table already exists in
1490 ** the sqlite_master table.  We do not want to create it again.
1491 **
1492 ** If the pSelect argument is not NULL, it means that this routine
1493 ** was called to create a table generated from a
1494 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
1495 ** the new table will match the result set of the SELECT.
1496 */
1497 void sqlite3EndTable(
1498   Parse *pParse,          /* Parse context */
1499   Token *pCons,           /* The ',' token after the last column defn. */
1500   Token *pEnd,            /* The final ')' token in the CREATE TABLE */
1501   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
1502 ){
1503   Table *p;
1504   sqlite3 *db = pParse->db;
1505   int iDb;
1506 
1507   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
1508     return;
1509   }
1510   p = pParse->pNewTable;
1511   if( p==0 ) return;
1512 
1513   assert( !db->init.busy || !pSelect );
1514 
1515   iDb = sqlite3SchemaToIndex(db, p->pSchema);
1516 
1517 #ifndef SQLITE_OMIT_CHECK
1518   /* Resolve names in all CHECK constraint expressions.
1519   */
1520   if( p->pCheck ){
1521     SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
1522     NameContext sNC;                /* Name context for pParse->pNewTable */
1523     ExprList *pList;                /* List of all CHECK constraints */
1524     int i;                          /* Loop counter */
1525 
1526     memset(&sNC, 0, sizeof(sNC));
1527     memset(&sSrc, 0, sizeof(sSrc));
1528     sSrc.nSrc = 1;
1529     sSrc.a[0].zName = p->zName;
1530     sSrc.a[0].pTab = p;
1531     sSrc.a[0].iCursor = -1;
1532     sNC.pParse = pParse;
1533     sNC.pSrcList = &sSrc;
1534     sNC.ncFlags = NC_IsCheck;
1535     pList = p->pCheck;
1536     for(i=0; i<pList->nExpr; i++){
1537       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
1538         return;
1539       }
1540     }
1541   }
1542 #endif /* !defined(SQLITE_OMIT_CHECK) */
1543 
1544   /* If the db->init.busy is 1 it means we are reading the SQL off the
1545   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1546   ** So do not write to the disk again.  Extract the root page number
1547   ** for the table from the db->init.newTnum field.  (The page number
1548   ** should have been put there by the sqliteOpenCb routine.)
1549   */
1550   if( db->init.busy ){
1551     p->tnum = db->init.newTnum;
1552   }
1553 
1554   /* If not initializing, then create a record for the new table
1555   ** in the SQLITE_MASTER table of the database.
1556   **
1557   ** If this is a TEMPORARY table, write the entry into the auxiliary
1558   ** file instead of into the main database file.
1559   */
1560   if( !db->init.busy ){
1561     int n;
1562     Vdbe *v;
1563     char *zType;    /* "view" or "table" */
1564     char *zType2;   /* "VIEW" or "TABLE" */
1565     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
1566 
1567     v = sqlite3GetVdbe(pParse);
1568     if( NEVER(v==0) ) return;
1569 
1570     sqlite3VdbeAddOp1(v, OP_Close, 0);
1571 
1572     /*
1573     ** Initialize zType for the new view or table.
1574     */
1575     if( p->pSelect==0 ){
1576       /* A regular table */
1577       zType = "table";
1578       zType2 = "TABLE";
1579 #ifndef SQLITE_OMIT_VIEW
1580     }else{
1581       /* A view */
1582       zType = "view";
1583       zType2 = "VIEW";
1584 #endif
1585     }
1586 
1587     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1588     ** statement to populate the new table. The root-page number for the
1589     ** new table is in register pParse->regRoot.
1590     **
1591     ** Once the SELECT has been coded by sqlite3Select(), it is in a
1592     ** suitable state to query for the column names and types to be used
1593     ** by the new table.
1594     **
1595     ** A shared-cache write-lock is not required to write to the new table,
1596     ** as a schema-lock must have already been obtained to create it. Since
1597     ** a schema-lock excludes all other database users, the write-lock would
1598     ** be redundant.
1599     */
1600     if( pSelect ){
1601       SelectDest dest;
1602       Table *pSelTab;
1603 
1604       assert(pParse->nTab==1);
1605       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
1606       sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
1607       pParse->nTab = 2;
1608       sqlite3SelectDestInit(&dest, SRT_Table, 1);
1609       sqlite3Select(pParse, pSelect, &dest);
1610       sqlite3VdbeAddOp1(v, OP_Close, 1);
1611       if( pParse->nErr==0 ){
1612         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
1613         if( pSelTab==0 ) return;
1614         assert( p->aCol==0 );
1615         p->nCol = pSelTab->nCol;
1616         p->aCol = pSelTab->aCol;
1617         pSelTab->nCol = 0;
1618         pSelTab->aCol = 0;
1619         sqlite3DeleteTable(db, pSelTab);
1620       }
1621     }
1622 
1623     /* Compute the complete text of the CREATE statement */
1624     if( pSelect ){
1625       zStmt = createTableStmt(db, p);
1626     }else{
1627       n = (int)(pEnd->z - pParse->sNameToken.z) + 1;
1628       zStmt = sqlite3MPrintf(db,
1629           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
1630       );
1631     }
1632 
1633     /* A slot for the record has already been allocated in the
1634     ** SQLITE_MASTER table.  We just need to update that slot with all
1635     ** the information we've collected.
1636     */
1637     sqlite3NestedParse(pParse,
1638       "UPDATE %Q.%s "
1639          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
1640        "WHERE rowid=#%d",
1641       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
1642       zType,
1643       p->zName,
1644       p->zName,
1645       pParse->regRoot,
1646       zStmt,
1647       pParse->regRowid
1648     );
1649     sqlite3DbFree(db, zStmt);
1650     sqlite3ChangeCookie(pParse, iDb);
1651 
1652 #ifndef SQLITE_OMIT_AUTOINCREMENT
1653     /* Check to see if we need to create an sqlite_sequence table for
1654     ** keeping track of autoincrement keys.
1655     */
1656     if( p->tabFlags & TF_Autoincrement ){
1657       Db *pDb = &db->aDb[iDb];
1658       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1659       if( pDb->pSchema->pSeqTab==0 ){
1660         sqlite3NestedParse(pParse,
1661           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
1662           pDb->zName
1663         );
1664       }
1665     }
1666 #endif
1667 
1668     /* Reparse everything to update our internal data structures */
1669     sqlite3VdbeAddParseSchemaOp(v, iDb,
1670                sqlite3MPrintf(db, "tbl_name='%q'", p->zName));
1671   }
1672 
1673 
1674   /* Add the table to the in-memory representation of the database.
1675   */
1676   if( db->init.busy ){
1677     Table *pOld;
1678     Schema *pSchema = p->pSchema;
1679     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1680     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
1681                              sqlite3Strlen30(p->zName),p);
1682     if( pOld ){
1683       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
1684       db->mallocFailed = 1;
1685       return;
1686     }
1687     pParse->pNewTable = 0;
1688     db->flags |= SQLITE_InternChanges;
1689 
1690 #ifndef SQLITE_OMIT_ALTERTABLE
1691     if( !p->pSelect ){
1692       const char *zName = (const char *)pParse->sNameToken.z;
1693       int nName;
1694       assert( !pSelect && pCons && pEnd );
1695       if( pCons->z==0 ){
1696         pCons = pEnd;
1697       }
1698       nName = (int)((const char *)pCons->z - zName);
1699       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
1700     }
1701 #endif
1702   }
1703 }
1704 
1705 #ifndef SQLITE_OMIT_VIEW
1706 /*
1707 ** The parser calls this routine in order to create a new VIEW
1708 */
1709 void sqlite3CreateView(
1710   Parse *pParse,     /* The parsing context */
1711   Token *pBegin,     /* The CREATE token that begins the statement */
1712   Token *pName1,     /* The token that holds the name of the view */
1713   Token *pName2,     /* The token that holds the name of the view */
1714   Select *pSelect,   /* A SELECT statement that will become the new view */
1715   int isTemp,        /* TRUE for a TEMPORARY view */
1716   int noErr          /* Suppress error messages if VIEW already exists */
1717 ){
1718   Table *p;
1719   int n;
1720   const char *z;
1721   Token sEnd;
1722   DbFixer sFix;
1723   Token *pName = 0;
1724   int iDb;
1725   sqlite3 *db = pParse->db;
1726 
1727   if( pParse->nVar>0 ){
1728     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
1729     sqlite3SelectDelete(db, pSelect);
1730     return;
1731   }
1732   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
1733   p = pParse->pNewTable;
1734   if( p==0 || pParse->nErr ){
1735     sqlite3SelectDelete(db, pSelect);
1736     return;
1737   }
1738   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
1739   iDb = sqlite3SchemaToIndex(db, p->pSchema);
1740   if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName)
1741     && sqlite3FixSelect(&sFix, pSelect)
1742   ){
1743     sqlite3SelectDelete(db, pSelect);
1744     return;
1745   }
1746 
1747   /* Make a copy of the entire SELECT statement that defines the view.
1748   ** This will force all the Expr.token.z values to be dynamically
1749   ** allocated rather than point to the input string - which means that
1750   ** they will persist after the current sqlite3_exec() call returns.
1751   */
1752   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
1753   sqlite3SelectDelete(db, pSelect);
1754   if( db->mallocFailed ){
1755     return;
1756   }
1757   if( !db->init.busy ){
1758     sqlite3ViewGetColumnNames(pParse, p);
1759   }
1760 
1761   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
1762   ** the end.
1763   */
1764   sEnd = pParse->sLastToken;
1765   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
1766     sEnd.z += sEnd.n;
1767   }
1768   sEnd.n = 0;
1769   n = (int)(sEnd.z - pBegin->z);
1770   z = pBegin->z;
1771   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
1772   sEnd.z = &z[n-1];
1773   sEnd.n = 1;
1774 
1775   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1776   sqlite3EndTable(pParse, 0, &sEnd, 0);
1777   return;
1778 }
1779 #endif /* SQLITE_OMIT_VIEW */
1780 
1781 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
1782 /*
1783 ** The Table structure pTable is really a VIEW.  Fill in the names of
1784 ** the columns of the view in the pTable structure.  Return the number
1785 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
1786 */
1787 int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
1788   Table *pSelTab;   /* A fake table from which we get the result set */
1789   Select *pSel;     /* Copy of the SELECT that implements the view */
1790   int nErr = 0;     /* Number of errors encountered */
1791   int n;            /* Temporarily holds the number of cursors assigned */
1792   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
1793   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
1794 
1795   assert( pTable );
1796 
1797 #ifndef SQLITE_OMIT_VIRTUALTABLE
1798   if( sqlite3VtabCallConnect(pParse, pTable) ){
1799     return SQLITE_ERROR;
1800   }
1801   if( IsVirtual(pTable) ) return 0;
1802 #endif
1803 
1804 #ifndef SQLITE_OMIT_VIEW
1805   /* A positive nCol means the columns names for this view are
1806   ** already known.
1807   */
1808   if( pTable->nCol>0 ) return 0;
1809 
1810   /* A negative nCol is a special marker meaning that we are currently
1811   ** trying to compute the column names.  If we enter this routine with
1812   ** a negative nCol, it means two or more views form a loop, like this:
1813   **
1814   **     CREATE VIEW one AS SELECT * FROM two;
1815   **     CREATE VIEW two AS SELECT * FROM one;
1816   **
1817   ** Actually, the error above is now caught prior to reaching this point.
1818   ** But the following test is still important as it does come up
1819   ** in the following:
1820   **
1821   **     CREATE TABLE main.ex1(a);
1822   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
1823   **     SELECT * FROM temp.ex1;
1824   */
1825   if( pTable->nCol<0 ){
1826     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
1827     return 1;
1828   }
1829   assert( pTable->nCol>=0 );
1830 
1831   /* If we get this far, it means we need to compute the table names.
1832   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
1833   ** "*" elements in the results set of the view and will assign cursors
1834   ** to the elements of the FROM clause.  But we do not want these changes
1835   ** to be permanent.  So the computation is done on a copy of the SELECT
1836   ** statement that defines the view.
1837   */
1838   assert( pTable->pSelect );
1839   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
1840   if( pSel ){
1841     u8 enableLookaside = db->lookaside.bEnabled;
1842     n = pParse->nTab;
1843     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
1844     pTable->nCol = -1;
1845     db->lookaside.bEnabled = 0;
1846 #ifndef SQLITE_OMIT_AUTHORIZATION
1847     xAuth = db->xAuth;
1848     db->xAuth = 0;
1849     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
1850     db->xAuth = xAuth;
1851 #else
1852     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
1853 #endif
1854     db->lookaside.bEnabled = enableLookaside;
1855     pParse->nTab = n;
1856     if( pSelTab ){
1857       assert( pTable->aCol==0 );
1858       pTable->nCol = pSelTab->nCol;
1859       pTable->aCol = pSelTab->aCol;
1860       pSelTab->nCol = 0;
1861       pSelTab->aCol = 0;
1862       sqlite3DeleteTable(db, pSelTab);
1863       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
1864       pTable->pSchema->flags |= DB_UnresetViews;
1865     }else{
1866       pTable->nCol = 0;
1867       nErr++;
1868     }
1869     sqlite3SelectDelete(db, pSel);
1870   } else {
1871     nErr++;
1872   }
1873 #endif /* SQLITE_OMIT_VIEW */
1874   return nErr;
1875 }
1876 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
1877 
1878 #ifndef SQLITE_OMIT_VIEW
1879 /*
1880 ** Clear the column names from every VIEW in database idx.
1881 */
1882 static void sqliteViewResetAll(sqlite3 *db, int idx){
1883   HashElem *i;
1884   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
1885   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
1886   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
1887     Table *pTab = sqliteHashData(i);
1888     if( pTab->pSelect ){
1889       sqliteDeleteColumnNames(db, pTab);
1890       pTab->aCol = 0;
1891       pTab->nCol = 0;
1892     }
1893   }
1894   DbClearProperty(db, idx, DB_UnresetViews);
1895 }
1896 #else
1897 # define sqliteViewResetAll(A,B)
1898 #endif /* SQLITE_OMIT_VIEW */
1899 
1900 /*
1901 ** This function is called by the VDBE to adjust the internal schema
1902 ** used by SQLite when the btree layer moves a table root page. The
1903 ** root-page of a table or index in database iDb has changed from iFrom
1904 ** to iTo.
1905 **
1906 ** Ticket #1728:  The symbol table might still contain information
1907 ** on tables and/or indices that are the process of being deleted.
1908 ** If you are unlucky, one of those deleted indices or tables might
1909 ** have the same rootpage number as the real table or index that is
1910 ** being moved.  So we cannot stop searching after the first match
1911 ** because the first match might be for one of the deleted indices
1912 ** or tables and not the table/index that is actually being moved.
1913 ** We must continue looping until all tables and indices with
1914 ** rootpage==iFrom have been converted to have a rootpage of iTo
1915 ** in order to be certain that we got the right one.
1916 */
1917 #ifndef SQLITE_OMIT_AUTOVACUUM
1918 void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
1919   HashElem *pElem;
1920   Hash *pHash;
1921   Db *pDb;
1922 
1923   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
1924   pDb = &db->aDb[iDb];
1925   pHash = &pDb->pSchema->tblHash;
1926   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
1927     Table *pTab = sqliteHashData(pElem);
1928     if( pTab->tnum==iFrom ){
1929       pTab->tnum = iTo;
1930     }
1931   }
1932   pHash = &pDb->pSchema->idxHash;
1933   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
1934     Index *pIdx = sqliteHashData(pElem);
1935     if( pIdx->tnum==iFrom ){
1936       pIdx->tnum = iTo;
1937     }
1938   }
1939 }
1940 #endif
1941 
1942 /*
1943 ** Write code to erase the table with root-page iTable from database iDb.
1944 ** Also write code to modify the sqlite_master table and internal schema
1945 ** if a root-page of another table is moved by the btree-layer whilst
1946 ** erasing iTable (this can happen with an auto-vacuum database).
1947 */
1948 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
1949   Vdbe *v = sqlite3GetVdbe(pParse);
1950   int r1 = sqlite3GetTempReg(pParse);
1951   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
1952   sqlite3MayAbort(pParse);
1953 #ifndef SQLITE_OMIT_AUTOVACUUM
1954   /* OP_Destroy stores an in integer r1. If this integer
1955   ** is non-zero, then it is the root page number of a table moved to
1956   ** location iTable. The following code modifies the sqlite_master table to
1957   ** reflect this.
1958   **
1959   ** The "#NNN" in the SQL is a special constant that means whatever value
1960   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
1961   ** token for additional information.
1962   */
1963   sqlite3NestedParse(pParse,
1964      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
1965      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
1966 #endif
1967   sqlite3ReleaseTempReg(pParse, r1);
1968 }
1969 
1970 /*
1971 ** Write VDBE code to erase table pTab and all associated indices on disk.
1972 ** Code to update the sqlite_master tables and internal schema definitions
1973 ** in case a root-page belonging to another table is moved by the btree layer
1974 ** is also added (this can happen with an auto-vacuum database).
1975 */
1976 static void destroyTable(Parse *pParse, Table *pTab){
1977 #ifdef SQLITE_OMIT_AUTOVACUUM
1978   Index *pIdx;
1979   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
1980   destroyRootPage(pParse, pTab->tnum, iDb);
1981   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1982     destroyRootPage(pParse, pIdx->tnum, iDb);
1983   }
1984 #else
1985   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1986   ** is not defined), then it is important to call OP_Destroy on the
1987   ** table and index root-pages in order, starting with the numerically
1988   ** largest root-page number. This guarantees that none of the root-pages
1989   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1990   ** following were coded:
1991   **
1992   ** OP_Destroy 4 0
1993   ** ...
1994   ** OP_Destroy 5 0
1995   **
1996   ** and root page 5 happened to be the largest root-page number in the
1997   ** database, then root page 5 would be moved to page 4 by the
1998   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1999   ** a free-list page.
2000   */
2001   int iTab = pTab->tnum;
2002   int iDestroyed = 0;
2003 
2004   while( 1 ){
2005     Index *pIdx;
2006     int iLargest = 0;
2007 
2008     if( iDestroyed==0 || iTab<iDestroyed ){
2009       iLargest = iTab;
2010     }
2011     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2012       int iIdx = pIdx->tnum;
2013       assert( pIdx->pSchema==pTab->pSchema );
2014       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
2015         iLargest = iIdx;
2016       }
2017     }
2018     if( iLargest==0 ){
2019       return;
2020     }else{
2021       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
2022       assert( iDb>=0 && iDb<pParse->db->nDb );
2023       destroyRootPage(pParse, iLargest, iDb);
2024       iDestroyed = iLargest;
2025     }
2026   }
2027 #endif
2028 }
2029 
2030 /*
2031 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
2032 ** after a DROP INDEX or DROP TABLE command.
2033 */
2034 static void sqlite3ClearStatTables(
2035   Parse *pParse,         /* The parsing context */
2036   int iDb,               /* The database number */
2037   const char *zType,     /* "idx" or "tbl" */
2038   const char *zName      /* Name of index or table */
2039 ){
2040   int i;
2041   const char *zDbName = pParse->db->aDb[iDb].zName;
2042   for(i=1; i<=3; i++){
2043     char zTab[24];
2044     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
2045     if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
2046       sqlite3NestedParse(pParse,
2047         "DELETE FROM %Q.%s WHERE %s=%Q",
2048         zDbName, zTab, zType, zName
2049       );
2050     }
2051   }
2052 }
2053 
2054 /*
2055 ** Generate code to drop a table.
2056 */
2057 void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
2058   Vdbe *v;
2059   sqlite3 *db = pParse->db;
2060   Trigger *pTrigger;
2061   Db *pDb = &db->aDb[iDb];
2062 
2063   v = sqlite3GetVdbe(pParse);
2064   assert( v!=0 );
2065   sqlite3BeginWriteOperation(pParse, 1, iDb);
2066 
2067 #ifndef SQLITE_OMIT_VIRTUALTABLE
2068   if( IsVirtual(pTab) ){
2069     sqlite3VdbeAddOp0(v, OP_VBegin);
2070   }
2071 #endif
2072 
2073   /* Drop all triggers associated with the table being dropped. Code
2074   ** is generated to remove entries from sqlite_master and/or
2075   ** sqlite_temp_master if required.
2076   */
2077   pTrigger = sqlite3TriggerList(pParse, pTab);
2078   while( pTrigger ){
2079     assert( pTrigger->pSchema==pTab->pSchema ||
2080         pTrigger->pSchema==db->aDb[1].pSchema );
2081     sqlite3DropTriggerPtr(pParse, pTrigger);
2082     pTrigger = pTrigger->pNext;
2083   }
2084 
2085 #ifndef SQLITE_OMIT_AUTOINCREMENT
2086   /* Remove any entries of the sqlite_sequence table associated with
2087   ** the table being dropped. This is done before the table is dropped
2088   ** at the btree level, in case the sqlite_sequence table needs to
2089   ** move as a result of the drop (can happen in auto-vacuum mode).
2090   */
2091   if( pTab->tabFlags & TF_Autoincrement ){
2092     sqlite3NestedParse(pParse,
2093       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
2094       pDb->zName, pTab->zName
2095     );
2096   }
2097 #endif
2098 
2099   /* Drop all SQLITE_MASTER table and index entries that refer to the
2100   ** table. The program name loops through the master table and deletes
2101   ** every row that refers to a table of the same name as the one being
2102   ** dropped. Triggers are handled seperately because a trigger can be
2103   ** created in the temp database that refers to a table in another
2104   ** database.
2105   */
2106   sqlite3NestedParse(pParse,
2107       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
2108       pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
2109   if( !isView && !IsVirtual(pTab) ){
2110     destroyTable(pParse, pTab);
2111   }
2112 
2113   /* Remove the table entry from SQLite's internal schema and modify
2114   ** the schema cookie.
2115   */
2116   if( IsVirtual(pTab) ){
2117     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
2118   }
2119   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
2120   sqlite3ChangeCookie(pParse, iDb);
2121   sqliteViewResetAll(db, iDb);
2122 }
2123 
2124 /*
2125 ** This routine is called to do the work of a DROP TABLE statement.
2126 ** pName is the name of the table to be dropped.
2127 */
2128 void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
2129   Table *pTab;
2130   Vdbe *v;
2131   sqlite3 *db = pParse->db;
2132   int iDb;
2133 
2134   if( db->mallocFailed ){
2135     goto exit_drop_table;
2136   }
2137   assert( pParse->nErr==0 );
2138   assert( pName->nSrc==1 );
2139   if( noErr ) db->suppressErr++;
2140   pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
2141   if( noErr ) db->suppressErr--;
2142 
2143   if( pTab==0 ){
2144     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
2145     goto exit_drop_table;
2146   }
2147   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
2148   assert( iDb>=0 && iDb<db->nDb );
2149 
2150   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
2151   ** it is initialized.
2152   */
2153   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
2154     goto exit_drop_table;
2155   }
2156 #ifndef SQLITE_OMIT_AUTHORIZATION
2157   {
2158     int code;
2159     const char *zTab = SCHEMA_TABLE(iDb);
2160     const char *zDb = db->aDb[iDb].zName;
2161     const char *zArg2 = 0;
2162     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
2163       goto exit_drop_table;
2164     }
2165     if( isView ){
2166       if( !OMIT_TEMPDB && iDb==1 ){
2167         code = SQLITE_DROP_TEMP_VIEW;
2168       }else{
2169         code = SQLITE_DROP_VIEW;
2170       }
2171 #ifndef SQLITE_OMIT_VIRTUALTABLE
2172     }else if( IsVirtual(pTab) ){
2173       code = SQLITE_DROP_VTABLE;
2174       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
2175 #endif
2176     }else{
2177       if( !OMIT_TEMPDB && iDb==1 ){
2178         code = SQLITE_DROP_TEMP_TABLE;
2179       }else{
2180         code = SQLITE_DROP_TABLE;
2181       }
2182     }
2183     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
2184       goto exit_drop_table;
2185     }
2186     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
2187       goto exit_drop_table;
2188     }
2189   }
2190 #endif
2191   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
2192     && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
2193     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
2194     goto exit_drop_table;
2195   }
2196 
2197 #ifndef SQLITE_OMIT_VIEW
2198   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
2199   ** on a table.
2200   */
2201   if( isView && pTab->pSelect==0 ){
2202     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
2203     goto exit_drop_table;
2204   }
2205   if( !isView && pTab->pSelect ){
2206     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
2207     goto exit_drop_table;
2208   }
2209 #endif
2210 
2211   /* Generate code to remove the table from the master table
2212   ** on disk.
2213   */
2214   v = sqlite3GetVdbe(pParse);
2215   if( v ){
2216     sqlite3BeginWriteOperation(pParse, 1, iDb);
2217     sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
2218     sqlite3FkDropTable(pParse, pName, pTab);
2219     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
2220   }
2221 
2222 exit_drop_table:
2223   sqlite3SrcListDelete(db, pName);
2224 }
2225 
2226 /*
2227 ** This routine is called to create a new foreign key on the table
2228 ** currently under construction.  pFromCol determines which columns
2229 ** in the current table point to the foreign key.  If pFromCol==0 then
2230 ** connect the key to the last column inserted.  pTo is the name of
2231 ** the table referred to.  pToCol is a list of tables in the other
2232 ** pTo table that the foreign key points to.  flags contains all
2233 ** information about the conflict resolution algorithms specified
2234 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
2235 **
2236 ** An FKey structure is created and added to the table currently
2237 ** under construction in the pParse->pNewTable field.
2238 **
2239 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
2240 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
2241 */
2242 void sqlite3CreateForeignKey(
2243   Parse *pParse,       /* Parsing context */
2244   ExprList *pFromCol,  /* Columns in this table that point to other table */
2245   Token *pTo,          /* Name of the other table */
2246   ExprList *pToCol,    /* Columns in the other table */
2247   int flags            /* Conflict resolution algorithms. */
2248 ){
2249   sqlite3 *db = pParse->db;
2250 #ifndef SQLITE_OMIT_FOREIGN_KEY
2251   FKey *pFKey = 0;
2252   FKey *pNextTo;
2253   Table *p = pParse->pNewTable;
2254   int nByte;
2255   int i;
2256   int nCol;
2257   char *z;
2258 
2259   assert( pTo!=0 );
2260   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
2261   if( pFromCol==0 ){
2262     int iCol = p->nCol-1;
2263     if( NEVER(iCol<0) ) goto fk_end;
2264     if( pToCol && pToCol->nExpr!=1 ){
2265       sqlite3ErrorMsg(pParse, "foreign key on %s"
2266          " should reference only one column of table %T",
2267          p->aCol[iCol].zName, pTo);
2268       goto fk_end;
2269     }
2270     nCol = 1;
2271   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
2272     sqlite3ErrorMsg(pParse,
2273         "number of columns in foreign key does not match the number of "
2274         "columns in the referenced table");
2275     goto fk_end;
2276   }else{
2277     nCol = pFromCol->nExpr;
2278   }
2279   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
2280   if( pToCol ){
2281     for(i=0; i<pToCol->nExpr; i++){
2282       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
2283     }
2284   }
2285   pFKey = sqlite3DbMallocZero(db, nByte );
2286   if( pFKey==0 ){
2287     goto fk_end;
2288   }
2289   pFKey->pFrom = p;
2290   pFKey->pNextFrom = p->pFKey;
2291   z = (char*)&pFKey->aCol[nCol];
2292   pFKey->zTo = z;
2293   memcpy(z, pTo->z, pTo->n);
2294   z[pTo->n] = 0;
2295   sqlite3Dequote(z);
2296   z += pTo->n+1;
2297   pFKey->nCol = nCol;
2298   if( pFromCol==0 ){
2299     pFKey->aCol[0].iFrom = p->nCol-1;
2300   }else{
2301     for(i=0; i<nCol; i++){
2302       int j;
2303       for(j=0; j<p->nCol; j++){
2304         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
2305           pFKey->aCol[i].iFrom = j;
2306           break;
2307         }
2308       }
2309       if( j>=p->nCol ){
2310         sqlite3ErrorMsg(pParse,
2311           "unknown column \"%s\" in foreign key definition",
2312           pFromCol->a[i].zName);
2313         goto fk_end;
2314       }
2315     }
2316   }
2317   if( pToCol ){
2318     for(i=0; i<nCol; i++){
2319       int n = sqlite3Strlen30(pToCol->a[i].zName);
2320       pFKey->aCol[i].zCol = z;
2321       memcpy(z, pToCol->a[i].zName, n);
2322       z[n] = 0;
2323       z += n+1;
2324     }
2325   }
2326   pFKey->isDeferred = 0;
2327   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
2328   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
2329 
2330   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
2331   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
2332       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
2333   );
2334   if( pNextTo==pFKey ){
2335     db->mallocFailed = 1;
2336     goto fk_end;
2337   }
2338   if( pNextTo ){
2339     assert( pNextTo->pPrevTo==0 );
2340     pFKey->pNextTo = pNextTo;
2341     pNextTo->pPrevTo = pFKey;
2342   }
2343 
2344   /* Link the foreign key to the table as the last step.
2345   */
2346   p->pFKey = pFKey;
2347   pFKey = 0;
2348 
2349 fk_end:
2350   sqlite3DbFree(db, pFKey);
2351 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
2352   sqlite3ExprListDelete(db, pFromCol);
2353   sqlite3ExprListDelete(db, pToCol);
2354 }
2355 
2356 /*
2357 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
2358 ** clause is seen as part of a foreign key definition.  The isDeferred
2359 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
2360 ** The behavior of the most recently created foreign key is adjusted
2361 ** accordingly.
2362 */
2363 void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
2364 #ifndef SQLITE_OMIT_FOREIGN_KEY
2365   Table *pTab;
2366   FKey *pFKey;
2367   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
2368   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
2369   pFKey->isDeferred = (u8)isDeferred;
2370 #endif
2371 }
2372 
2373 /*
2374 ** Generate code that will erase and refill index *pIdx.  This is
2375 ** used to initialize a newly created index or to recompute the
2376 ** content of an index in response to a REINDEX command.
2377 **
2378 ** if memRootPage is not negative, it means that the index is newly
2379 ** created.  The register specified by memRootPage contains the
2380 ** root page number of the index.  If memRootPage is negative, then
2381 ** the index already exists and must be cleared before being refilled and
2382 ** the root page number of the index is taken from pIndex->tnum.
2383 */
2384 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
2385   Table *pTab = pIndex->pTable;  /* The table that is indexed */
2386   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
2387   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
2388   int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
2389   int addr1;                     /* Address of top of loop */
2390   int addr2;                     /* Address to jump to for next iteration */
2391   int tnum;                      /* Root page of index */
2392   Vdbe *v;                       /* Generate code into this virtual machine */
2393   KeyInfo *pKey;                 /* KeyInfo for index */
2394 #ifdef SQLITE_OMIT_MERGE_SORT
2395   int regIdxKey;                 /* Registers containing the index key */
2396 #endif
2397   int regRecord;                 /* Register holding assemblied index record */
2398   sqlite3 *db = pParse->db;      /* The database connection */
2399   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
2400 
2401 #ifndef SQLITE_OMIT_AUTHORIZATION
2402   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
2403       db->aDb[iDb].zName ) ){
2404     return;
2405   }
2406 #endif
2407 
2408   /* Require a write-lock on the table to perform this operation */
2409   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
2410 
2411   v = sqlite3GetVdbe(pParse);
2412   if( v==0 ) return;
2413   if( memRootPage>=0 ){
2414     tnum = memRootPage;
2415   }else{
2416     tnum = pIndex->tnum;
2417     sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
2418   }
2419   pKey = sqlite3IndexKeyinfo(pParse, pIndex);
2420   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
2421                     (char *)pKey, P4_KEYINFO_HANDOFF);
2422   sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
2423 
2424 #ifndef SQLITE_OMIT_MERGE_SORT
2425   /* Open the sorter cursor if we are to use one. */
2426   iSorter = pParse->nTab++;
2427   sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
2428 #else
2429   iSorter = iTab;
2430 #endif
2431 
2432   /* Open the table. Loop through all rows of the table, inserting index
2433   ** records into the sorter. */
2434   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
2435   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
2436   regRecord = sqlite3GetTempReg(pParse);
2437 
2438 #ifndef SQLITE_OMIT_MERGE_SORT
2439   sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
2440   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
2441   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
2442   sqlite3VdbeJumpHere(v, addr1);
2443   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
2444   if( pIndex->onError!=OE_None ){
2445     int j2 = sqlite3VdbeCurrentAddr(v) + 3;
2446     sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
2447     addr2 = sqlite3VdbeCurrentAddr(v);
2448     sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
2449     sqlite3HaltConstraint(
2450         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
2451     );
2452   }else{
2453     addr2 = sqlite3VdbeCurrentAddr(v);
2454   }
2455   sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
2456   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
2457   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
2458 #else
2459   regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
2460   addr2 = addr1 + 1;
2461   if( pIndex->onError!=OE_None ){
2462     const int regRowid = regIdxKey + pIndex->nColumn;
2463     const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
2464     void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
2465 
2466     /* The registers accessed by the OP_IsUnique opcode were allocated
2467     ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
2468     ** call above. Just before that function was freed they were released
2469     ** (made available to the compiler for reuse) using
2470     ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique
2471     ** opcode use the values stored within seems dangerous. However, since
2472     ** we can be sure that no other temp registers have been allocated
2473     ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
2474     */
2475     sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
2476     sqlite3HaltConstraint(
2477         pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
2478   }
2479   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
2480   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
2481 #endif
2482   sqlite3ReleaseTempReg(pParse, regRecord);
2483   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
2484   sqlite3VdbeJumpHere(v, addr1);
2485 
2486   sqlite3VdbeAddOp1(v, OP_Close, iTab);
2487   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
2488   sqlite3VdbeAddOp1(v, OP_Close, iSorter);
2489 }
2490 
2491 /*
2492 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index
2493 ** and pTblList is the name of the table that is to be indexed.  Both will
2494 ** be NULL for a primary key or an index that is created to satisfy a
2495 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
2496 ** as the table to be indexed.  pParse->pNewTable is a table that is
2497 ** currently being constructed by a CREATE TABLE statement.
2498 **
2499 ** pList is a list of columns to be indexed.  pList will be NULL if this
2500 ** is a primary key or unique-constraint on the most recent column added
2501 ** to the table currently under construction.
2502 **
2503 ** If the index is created successfully, return a pointer to the new Index
2504 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
2505 ** as the tables primary key (Index.autoIndex==2).
2506 */
2507 Index *sqlite3CreateIndex(
2508   Parse *pParse,     /* All information about this parse */
2509   Token *pName1,     /* First part of index name. May be NULL */
2510   Token *pName2,     /* Second part of index name. May be NULL */
2511   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
2512   ExprList *pList,   /* A list of columns to be indexed */
2513   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
2514   Token *pStart,     /* The CREATE token that begins this statement */
2515   Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
2516   int sortOrder,     /* Sort order of primary key when pList==NULL */
2517   int ifNotExist     /* Omit error if index already exists */
2518 ){
2519   Index *pRet = 0;     /* Pointer to return */
2520   Table *pTab = 0;     /* Table to be indexed */
2521   Index *pIndex = 0;   /* The index to be created */
2522   char *zName = 0;     /* Name of the index */
2523   int nName;           /* Number of characters in zName */
2524   int i, j;
2525   Token nullId;        /* Fake token for an empty ID list */
2526   DbFixer sFix;        /* For assigning database names to pTable */
2527   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
2528   sqlite3 *db = pParse->db;
2529   Db *pDb;             /* The specific table containing the indexed database */
2530   int iDb;             /* Index of the database that is being written */
2531   Token *pName = 0;    /* Unqualified name of the index to create */
2532   struct ExprList_item *pListItem; /* For looping over pList */
2533   int nCol;
2534   int nExtra = 0;
2535   char *zExtra;
2536 
2537   assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
2538   assert( pParse->nErr==0 );      /* Never called with prior errors */
2539   if( db->mallocFailed || IN_DECLARE_VTAB ){
2540     goto exit_create_index;
2541   }
2542   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
2543     goto exit_create_index;
2544   }
2545 
2546   /*
2547   ** Find the table that is to be indexed.  Return early if not found.
2548   */
2549   if( pTblName!=0 ){
2550 
2551     /* Use the two-part index name to determine the database
2552     ** to search for the table. 'Fix' the table name to this db
2553     ** before looking up the table.
2554     */
2555     assert( pName1 && pName2 );
2556     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
2557     if( iDb<0 ) goto exit_create_index;
2558     assert( pName && pName->z );
2559 
2560 #ifndef SQLITE_OMIT_TEMPDB
2561     /* If the index name was unqualified, check if the table
2562     ** is a temp table. If so, set the database to 1. Do not do this
2563     ** if initialising a database schema.
2564     */
2565     if( !db->init.busy ){
2566       pTab = sqlite3SrcListLookup(pParse, pTblName);
2567       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
2568         iDb = 1;
2569       }
2570     }
2571 #endif
2572 
2573     if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
2574         sqlite3FixSrcList(&sFix, pTblName)
2575     ){
2576       /* Because the parser constructs pTblName from a single identifier,
2577       ** sqlite3FixSrcList can never fail. */
2578       assert(0);
2579     }
2580     pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
2581     assert( db->mallocFailed==0 || pTab==0 );
2582     if( pTab==0 ) goto exit_create_index;
2583     assert( db->aDb[iDb].pSchema==pTab->pSchema );
2584   }else{
2585     assert( pName==0 );
2586     assert( pStart==0 );
2587     pTab = pParse->pNewTable;
2588     if( !pTab ) goto exit_create_index;
2589     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
2590   }
2591   pDb = &db->aDb[iDb];
2592 
2593   assert( pTab!=0 );
2594   assert( pParse->nErr==0 );
2595   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
2596        && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){
2597     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
2598     goto exit_create_index;
2599   }
2600 #ifndef SQLITE_OMIT_VIEW
2601   if( pTab->pSelect ){
2602     sqlite3ErrorMsg(pParse, "views may not be indexed");
2603     goto exit_create_index;
2604   }
2605 #endif
2606 #ifndef SQLITE_OMIT_VIRTUALTABLE
2607   if( IsVirtual(pTab) ){
2608     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
2609     goto exit_create_index;
2610   }
2611 #endif
2612 
2613   /*
2614   ** Find the name of the index.  Make sure there is not already another
2615   ** index or table with the same name.
2616   **
2617   ** Exception:  If we are reading the names of permanent indices from the
2618   ** sqlite_master table (because some other process changed the schema) and
2619   ** one of the index names collides with the name of a temporary table or
2620   ** index, then we will continue to process this index.
2621   **
2622   ** If pName==0 it means that we are
2623   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
2624   ** own name.
2625   */
2626   if( pName ){
2627     zName = sqlite3NameFromToken(db, pName);
2628     if( zName==0 ) goto exit_create_index;
2629     assert( pName->z!=0 );
2630     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
2631       goto exit_create_index;
2632     }
2633     if( !db->init.busy ){
2634       if( sqlite3FindTable(db, zName, 0)!=0 ){
2635         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
2636         goto exit_create_index;
2637       }
2638     }
2639     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
2640       if( !ifNotExist ){
2641         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
2642       }else{
2643         assert( !db->init.busy );
2644         sqlite3CodeVerifySchema(pParse, iDb);
2645       }
2646       goto exit_create_index;
2647     }
2648   }else{
2649     int n;
2650     Index *pLoop;
2651     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
2652     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
2653     if( zName==0 ){
2654       goto exit_create_index;
2655     }
2656   }
2657 
2658   /* Check for authorization to create an index.
2659   */
2660 #ifndef SQLITE_OMIT_AUTHORIZATION
2661   {
2662     const char *zDb = pDb->zName;
2663     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
2664       goto exit_create_index;
2665     }
2666     i = SQLITE_CREATE_INDEX;
2667     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
2668     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
2669       goto exit_create_index;
2670     }
2671   }
2672 #endif
2673 
2674   /* If pList==0, it means this routine was called to make a primary
2675   ** key out of the last column added to the table under construction.
2676   ** So create a fake list to simulate this.
2677   */
2678   if( pList==0 ){
2679     nullId.z = pTab->aCol[pTab->nCol-1].zName;
2680     nullId.n = sqlite3Strlen30((char*)nullId.z);
2681     pList = sqlite3ExprListAppend(pParse, 0, 0);
2682     if( pList==0 ) goto exit_create_index;
2683     sqlite3ExprListSetName(pParse, pList, &nullId, 0);
2684     pList->a[0].sortOrder = (u8)sortOrder;
2685   }
2686 
2687   /* Figure out how many bytes of space are required to store explicitly
2688   ** specified collation sequence names.
2689   */
2690   for(i=0; i<pList->nExpr; i++){
2691     Expr *pExpr = pList->a[i].pExpr;
2692     if( pExpr ){
2693       CollSeq *pColl = pExpr->pColl;
2694       /* Either pColl!=0 or there was an OOM failure.  But if an OOM
2695       ** failure we have quit before reaching this point. */
2696       if( ALWAYS(pColl) ){
2697         nExtra += (1 + sqlite3Strlen30(pColl->zName));
2698       }
2699     }
2700   }
2701 
2702   /*
2703   ** Allocate the index structure.
2704   */
2705   nName = sqlite3Strlen30(zName);
2706   nCol = pList->nExpr;
2707   pIndex = sqlite3DbMallocZero(db,
2708       ROUND8(sizeof(Index)) +              /* Index structure  */
2709       ROUND8(sizeof(tRowcnt)*(nCol+1)) +   /* Index.aiRowEst   */
2710       sizeof(char *)*nCol +                /* Index.azColl     */
2711       sizeof(int)*nCol +                   /* Index.aiColumn   */
2712       sizeof(u8)*nCol +                    /* Index.aSortOrder */
2713       nName + 1 +                          /* Index.zName      */
2714       nExtra                               /* Collation sequence names */
2715   );
2716   if( db->mallocFailed ){
2717     goto exit_create_index;
2718   }
2719   zExtra = (char*)pIndex;
2720   pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
2721   pIndex->azColl = (char**)
2722      ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
2723   assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
2724   assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
2725   pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
2726   pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
2727   pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
2728   zExtra = (char *)(&pIndex->zName[nName+1]);
2729   memcpy(pIndex->zName, zName, nName+1);
2730   pIndex->pTable = pTab;
2731   pIndex->nColumn = pList->nExpr;
2732   pIndex->onError = (u8)onError;
2733   pIndex->autoIndex = (u8)(pName==0);
2734   pIndex->pSchema = db->aDb[iDb].pSchema;
2735   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
2736 
2737   /* Check to see if we should honor DESC requests on index columns
2738   */
2739   if( pDb->pSchema->file_format>=4 ){
2740     sortOrderMask = -1;   /* Honor DESC */
2741   }else{
2742     sortOrderMask = 0;    /* Ignore DESC */
2743   }
2744 
2745   /* Scan the names of the columns of the table to be indexed and
2746   ** load the column indices into the Index structure.  Report an error
2747   ** if any column is not found.
2748   **
2749   ** TODO:  Add a test to make sure that the same column is not named
2750   ** more than once within the same index.  Only the first instance of
2751   ** the column will ever be used by the optimizer.  Note that using the
2752   ** same column more than once cannot be an error because that would
2753   ** break backwards compatibility - it needs to be a warning.
2754   */
2755   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
2756     const char *zColName = pListItem->zName;
2757     Column *pTabCol;
2758     int requestedSortOrder;
2759     char *zColl;                   /* Collation sequence name */
2760 
2761     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
2762       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
2763     }
2764     if( j>=pTab->nCol ){
2765       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
2766         pTab->zName, zColName);
2767       pParse->checkSchema = 1;
2768       goto exit_create_index;
2769     }
2770     pIndex->aiColumn[i] = j;
2771     /* Justification of the ALWAYS(pListItem->pExpr->pColl):  Because of
2772     ** the way the "idxlist" non-terminal is constructed by the parser,
2773     ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl
2774     ** must exist or else there must have been an OOM error.  But if there
2775     ** was an OOM error, we would never reach this point. */
2776     if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){
2777       int nColl;
2778       zColl = pListItem->pExpr->pColl->zName;
2779       nColl = sqlite3Strlen30(zColl) + 1;
2780       assert( nExtra>=nColl );
2781       memcpy(zExtra, zColl, nColl);
2782       zColl = zExtra;
2783       zExtra += nColl;
2784       nExtra -= nColl;
2785     }else{
2786       zColl = pTab->aCol[j].zColl;
2787       if( !zColl ){
2788         zColl = "BINARY";
2789       }
2790     }
2791     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
2792       goto exit_create_index;
2793     }
2794     pIndex->azColl[i] = zColl;
2795     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
2796     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
2797   }
2798   sqlite3DefaultRowEst(pIndex);
2799 
2800   if( pTab==pParse->pNewTable ){
2801     /* This routine has been called to create an automatic index as a
2802     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2803     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2804     ** i.e. one of:
2805     **
2806     ** CREATE TABLE t(x PRIMARY KEY, y);
2807     ** CREATE TABLE t(x, y, UNIQUE(x, y));
2808     **
2809     ** Either way, check to see if the table already has such an index. If
2810     ** so, don't bother creating this one. This only applies to
2811     ** automatically created indices. Users can do as they wish with
2812     ** explicit indices.
2813     **
2814     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
2815     ** (and thus suppressing the second one) even if they have different
2816     ** sort orders.
2817     **
2818     ** If there are different collating sequences or if the columns of
2819     ** the constraint occur in different orders, then the constraints are
2820     ** considered distinct and both result in separate indices.
2821     */
2822     Index *pIdx;
2823     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2824       int k;
2825       assert( pIdx->onError!=OE_None );
2826       assert( pIdx->autoIndex );
2827       assert( pIndex->onError!=OE_None );
2828 
2829       if( pIdx->nColumn!=pIndex->nColumn ) continue;
2830       for(k=0; k<pIdx->nColumn; k++){
2831         const char *z1;
2832         const char *z2;
2833         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2834         z1 = pIdx->azColl[k];
2835         z2 = pIndex->azColl[k];
2836         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
2837       }
2838       if( k==pIdx->nColumn ){
2839         if( pIdx->onError!=pIndex->onError ){
2840           /* This constraint creates the same index as a previous
2841           ** constraint specified somewhere in the CREATE TABLE statement.
2842           ** However the ON CONFLICT clauses are different. If both this
2843           ** constraint and the previous equivalent constraint have explicit
2844           ** ON CONFLICT clauses this is an error. Otherwise, use the
2845           ** explicitly specified behaviour for the index.
2846           */
2847           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2848             sqlite3ErrorMsg(pParse,
2849                 "conflicting ON CONFLICT clauses specified", 0);
2850           }
2851           if( pIdx->onError==OE_Default ){
2852             pIdx->onError = pIndex->onError;
2853           }
2854         }
2855         goto exit_create_index;
2856       }
2857     }
2858   }
2859 
2860   /* Link the new Index structure to its table and to the other
2861   ** in-memory database structures.
2862   */
2863   if( db->init.busy ){
2864     Index *p;
2865     assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
2866     p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
2867                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
2868                           pIndex);
2869     if( p ){
2870       assert( p==pIndex );  /* Malloc must have failed */
2871       db->mallocFailed = 1;
2872       goto exit_create_index;
2873     }
2874     db->flags |= SQLITE_InternChanges;
2875     if( pTblName!=0 ){
2876       pIndex->tnum = db->init.newTnum;
2877     }
2878   }
2879 
2880   /* If the db->init.busy is 0 then create the index on disk.  This
2881   ** involves writing the index into the master table and filling in the
2882   ** index with the current table contents.
2883   **
2884   ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2885   ** command.  db->init.busy is 1 when a database is opened and
2886   ** CREATE INDEX statements are read out of the master table.  In
2887   ** the latter case the index already exists on disk, which is why
2888   ** we don't want to recreate it.
2889   **
2890   ** If pTblName==0 it means this index is generated as a primary key
2891   ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
2892   ** has just been created, it contains no data and the index initialization
2893   ** step can be skipped.
2894   */
2895   else{ /* if( db->init.busy==0 ) */
2896     Vdbe *v;
2897     char *zStmt;
2898     int iMem = ++pParse->nMem;
2899 
2900     v = sqlite3GetVdbe(pParse);
2901     if( v==0 ) goto exit_create_index;
2902 
2903 
2904     /* Create the rootpage for the index
2905     */
2906     sqlite3BeginWriteOperation(pParse, 1, iDb);
2907     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
2908 
2909     /* Gather the complete text of the CREATE INDEX statement into
2910     ** the zStmt variable
2911     */
2912     if( pStart ){
2913       assert( pEnd!=0 );
2914       /* A named index with an explicit CREATE INDEX statement */
2915       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
2916         onError==OE_None ? "" : " UNIQUE",
2917         (int)(pEnd->z - pName->z) + 1,
2918         pName->z);
2919     }else{
2920       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
2921       /* zStmt = sqlite3MPrintf(""); */
2922       zStmt = 0;
2923     }
2924 
2925     /* Add an entry in sqlite_master for this index
2926     */
2927     sqlite3NestedParse(pParse,
2928         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
2929         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2930         pIndex->zName,
2931         pTab->zName,
2932         iMem,
2933         zStmt
2934     );
2935     sqlite3DbFree(db, zStmt);
2936 
2937     /* Fill the index with data and reparse the schema. Code an OP_Expire
2938     ** to invalidate all pre-compiled statements.
2939     */
2940     if( pTblName ){
2941       sqlite3RefillIndex(pParse, pIndex, iMem);
2942       sqlite3ChangeCookie(pParse, iDb);
2943       sqlite3VdbeAddParseSchemaOp(v, iDb,
2944          sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
2945       sqlite3VdbeAddOp1(v, OP_Expire, 0);
2946     }
2947   }
2948 
2949   /* When adding an index to the list of indices for a table, make
2950   ** sure all indices labeled OE_Replace come after all those labeled
2951   ** OE_Ignore.  This is necessary for the correct constraint check
2952   ** processing (in sqlite3GenerateConstraintChecks()) as part of
2953   ** UPDATE and INSERT statements.
2954   */
2955   if( db->init.busy || pTblName==0 ){
2956     if( onError!=OE_Replace || pTab->pIndex==0
2957          || pTab->pIndex->onError==OE_Replace){
2958       pIndex->pNext = pTab->pIndex;
2959       pTab->pIndex = pIndex;
2960     }else{
2961       Index *pOther = pTab->pIndex;
2962       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2963         pOther = pOther->pNext;
2964       }
2965       pIndex->pNext = pOther->pNext;
2966       pOther->pNext = pIndex;
2967     }
2968     pRet = pIndex;
2969     pIndex = 0;
2970   }
2971 
2972   /* Clean up before exiting */
2973 exit_create_index:
2974   if( pIndex ){
2975     sqlite3DbFree(db, pIndex->zColAff);
2976     sqlite3DbFree(db, pIndex);
2977   }
2978   sqlite3ExprListDelete(db, pList);
2979   sqlite3SrcListDelete(db, pTblName);
2980   sqlite3DbFree(db, zName);
2981   return pRet;
2982 }
2983 
2984 /*
2985 ** Fill the Index.aiRowEst[] array with default information - information
2986 ** to be used when we have not run the ANALYZE command.
2987 **
2988 ** aiRowEst[0] is suppose to contain the number of elements in the index.
2989 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
2990 ** number of rows in the table that match any particular value of the
2991 ** first column of the index.  aiRowEst[2] is an estimate of the number
2992 ** of rows that match any particular combiniation of the first 2 columns
2993 ** of the index.  And so forth.  It must always be the case that
2994 *
2995 **           aiRowEst[N]<=aiRowEst[N-1]
2996 **           aiRowEst[N]>=1
2997 **
2998 ** Apart from that, we have little to go on besides intuition as to
2999 ** how aiRowEst[] should be initialized.  The numbers generated here
3000 ** are based on typical values found in actual indices.
3001 */
3002 void sqlite3DefaultRowEst(Index *pIdx){
3003   tRowcnt *a = pIdx->aiRowEst;
3004   int i;
3005   tRowcnt n;
3006   assert( a!=0 );
3007   a[0] = pIdx->pTable->nRowEst;
3008   if( a[0]<10 ) a[0] = 10;
3009   n = 10;
3010   for(i=1; i<=pIdx->nColumn; i++){
3011     a[i] = n;
3012     if( n>5 ) n--;
3013   }
3014   if( pIdx->onError!=OE_None ){
3015     a[pIdx->nColumn] = 1;
3016   }
3017 }
3018 
3019 /*
3020 ** This routine will drop an existing named index.  This routine
3021 ** implements the DROP INDEX statement.
3022 */
3023 void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
3024   Index *pIndex;
3025   Vdbe *v;
3026   sqlite3 *db = pParse->db;
3027   int iDb;
3028 
3029   assert( pParse->nErr==0 );   /* Never called with prior errors */
3030   if( db->mallocFailed ){
3031     goto exit_drop_index;
3032   }
3033   assert( pName->nSrc==1 );
3034   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
3035     goto exit_drop_index;
3036   }
3037   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
3038   if( pIndex==0 ){
3039     if( !ifExists ){
3040       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
3041     }else{
3042       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
3043     }
3044     pParse->checkSchema = 1;
3045     goto exit_drop_index;
3046   }
3047   if( pIndex->autoIndex ){
3048     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
3049       "or PRIMARY KEY constraint cannot be dropped", 0);
3050     goto exit_drop_index;
3051   }
3052   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
3053 #ifndef SQLITE_OMIT_AUTHORIZATION
3054   {
3055     int code = SQLITE_DROP_INDEX;
3056     Table *pTab = pIndex->pTable;
3057     const char *zDb = db->aDb[iDb].zName;
3058     const char *zTab = SCHEMA_TABLE(iDb);
3059     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
3060       goto exit_drop_index;
3061     }
3062     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
3063     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
3064       goto exit_drop_index;
3065     }
3066   }
3067 #endif
3068 
3069   /* Generate code to remove the index and from the master table */
3070   v = sqlite3GetVdbe(pParse);
3071   if( v ){
3072     sqlite3BeginWriteOperation(pParse, 1, iDb);
3073     sqlite3NestedParse(pParse,
3074        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
3075        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
3076     );
3077     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
3078     sqlite3ChangeCookie(pParse, iDb);
3079     destroyRootPage(pParse, pIndex->tnum, iDb);
3080     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
3081   }
3082 
3083 exit_drop_index:
3084   sqlite3SrcListDelete(db, pName);
3085 }
3086 
3087 /*
3088 ** pArray is a pointer to an array of objects. Each object in the
3089 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
3090 ** to extend the array so that there is space for a new object at the end.
3091 **
3092 ** When this function is called, *pnEntry contains the current size of
3093 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
3094 ** in total).
3095 **
3096 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
3097 ** space allocated for the new object is zeroed, *pnEntry updated to
3098 ** reflect the new size of the array and a pointer to the new allocation
3099 ** returned. *pIdx is set to the index of the new array entry in this case.
3100 **
3101 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
3102 ** unchanged and a copy of pArray returned.
3103 */
3104 void *sqlite3ArrayAllocate(
3105   sqlite3 *db,      /* Connection to notify of malloc failures */
3106   void *pArray,     /* Array of objects.  Might be reallocated */
3107   int szEntry,      /* Size of each object in the array */
3108   int *pnEntry,     /* Number of objects currently in use */
3109   int *pIdx         /* Write the index of a new slot here */
3110 ){
3111   char *z;
3112   int n = *pnEntry;
3113   if( (n & (n-1))==0 ){
3114     int sz = (n==0) ? 1 : 2*n;
3115     void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
3116     if( pNew==0 ){
3117       *pIdx = -1;
3118       return pArray;
3119     }
3120     pArray = pNew;
3121   }
3122   z = (char*)pArray;
3123   memset(&z[n * szEntry], 0, szEntry);
3124   *pIdx = n;
3125   ++*pnEntry;
3126   return pArray;
3127 }
3128 
3129 /*
3130 ** Append a new element to the given IdList.  Create a new IdList if
3131 ** need be.
3132 **
3133 ** A new IdList is returned, or NULL if malloc() fails.
3134 */
3135 IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
3136   int i;
3137   if( pList==0 ){
3138     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
3139     if( pList==0 ) return 0;
3140   }
3141   pList->a = sqlite3ArrayAllocate(
3142       db,
3143       pList->a,
3144       sizeof(pList->a[0]),
3145       &pList->nId,
3146       &i
3147   );
3148   if( i<0 ){
3149     sqlite3IdListDelete(db, pList);
3150     return 0;
3151   }
3152   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
3153   return pList;
3154 }
3155 
3156 /*
3157 ** Delete an IdList.
3158 */
3159 void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
3160   int i;
3161   if( pList==0 ) return;
3162   for(i=0; i<pList->nId; i++){
3163     sqlite3DbFree(db, pList->a[i].zName);
3164   }
3165   sqlite3DbFree(db, pList->a);
3166   sqlite3DbFree(db, pList);
3167 }
3168 
3169 /*
3170 ** Return the index in pList of the identifier named zId.  Return -1
3171 ** if not found.
3172 */
3173 int sqlite3IdListIndex(IdList *pList, const char *zName){
3174   int i;
3175   if( pList==0 ) return -1;
3176   for(i=0; i<pList->nId; i++){
3177     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
3178   }
3179   return -1;
3180 }
3181 
3182 /*
3183 ** Expand the space allocated for the given SrcList object by
3184 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
3185 ** New slots are zeroed.
3186 **
3187 ** For example, suppose a SrcList initially contains two entries: A,B.
3188 ** To append 3 new entries onto the end, do this:
3189 **
3190 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
3191 **
3192 ** After the call above it would contain:  A, B, nil, nil, nil.
3193 ** If the iStart argument had been 1 instead of 2, then the result
3194 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
3195 ** the iStart value would be 0.  The result then would
3196 ** be: nil, nil, nil, A, B.
3197 **
3198 ** If a memory allocation fails the SrcList is unchanged.  The
3199 ** db->mallocFailed flag will be set to true.
3200 */
3201 SrcList *sqlite3SrcListEnlarge(
3202   sqlite3 *db,       /* Database connection to notify of OOM errors */
3203   SrcList *pSrc,     /* The SrcList to be enlarged */
3204   int nExtra,        /* Number of new slots to add to pSrc->a[] */
3205   int iStart         /* Index in pSrc->a[] of first new slot */
3206 ){
3207   int i;
3208 
3209   /* Sanity checking on calling parameters */
3210   assert( iStart>=0 );
3211   assert( nExtra>=1 );
3212   assert( pSrc!=0 );
3213   assert( iStart<=pSrc->nSrc );
3214 
3215   /* Allocate additional space if needed */
3216   if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
3217     SrcList *pNew;
3218     int nAlloc = pSrc->nSrc+nExtra;
3219     int nGot;
3220     pNew = sqlite3DbRealloc(db, pSrc,
3221                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
3222     if( pNew==0 ){
3223       assert( db->mallocFailed );
3224       return pSrc;
3225     }
3226     pSrc = pNew;
3227     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
3228     pSrc->nAlloc = (u16)nGot;
3229   }
3230 
3231   /* Move existing slots that come after the newly inserted slots
3232   ** out of the way */
3233   for(i=pSrc->nSrc-1; i>=iStart; i--){
3234     pSrc->a[i+nExtra] = pSrc->a[i];
3235   }
3236   pSrc->nSrc += (i16)nExtra;
3237 
3238   /* Zero the newly allocated slots */
3239   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
3240   for(i=iStart; i<iStart+nExtra; i++){
3241     pSrc->a[i].iCursor = -1;
3242   }
3243 
3244   /* Return a pointer to the enlarged SrcList */
3245   return pSrc;
3246 }
3247 
3248 
3249 /*
3250 ** Append a new table name to the given SrcList.  Create a new SrcList if
3251 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
3252 **
3253 ** A SrcList is returned, or NULL if there is an OOM error.  The returned
3254 ** SrcList might be the same as the SrcList that was input or it might be
3255 ** a new one.  If an OOM error does occurs, then the prior value of pList
3256 ** that is input to this routine is automatically freed.
3257 **
3258 ** If pDatabase is not null, it means that the table has an optional
3259 ** database name prefix.  Like this:  "database.table".  The pDatabase
3260 ** points to the table name and the pTable points to the database name.
3261 ** The SrcList.a[].zName field is filled with the table name which might
3262 ** come from pTable (if pDatabase is NULL) or from pDatabase.
3263 ** SrcList.a[].zDatabase is filled with the database name from pTable,
3264 ** or with NULL if no database is specified.
3265 **
3266 ** In other words, if call like this:
3267 **
3268 **         sqlite3SrcListAppend(D,A,B,0);
3269 **
3270 ** Then B is a table name and the database name is unspecified.  If called
3271 ** like this:
3272 **
3273 **         sqlite3SrcListAppend(D,A,B,C);
3274 **
3275 ** Then C is the table name and B is the database name.  If C is defined
3276 ** then so is B.  In other words, we never have a case where:
3277 **
3278 **         sqlite3SrcListAppend(D,A,0,C);
3279 **
3280 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
3281 ** before being added to the SrcList.
3282 */
3283 SrcList *sqlite3SrcListAppend(
3284   sqlite3 *db,        /* Connection to notify of malloc failures */
3285   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
3286   Token *pTable,      /* Table to append */
3287   Token *pDatabase    /* Database of the table */
3288 ){
3289   struct SrcList_item *pItem;
3290   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
3291   if( pList==0 ){
3292     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
3293     if( pList==0 ) return 0;
3294     pList->nAlloc = 1;
3295   }
3296   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
3297   if( db->mallocFailed ){
3298     sqlite3SrcListDelete(db, pList);
3299     return 0;
3300   }
3301   pItem = &pList->a[pList->nSrc-1];
3302   if( pDatabase && pDatabase->z==0 ){
3303     pDatabase = 0;
3304   }
3305   if( pDatabase ){
3306     Token *pTemp = pDatabase;
3307     pDatabase = pTable;
3308     pTable = pTemp;
3309   }
3310   pItem->zName = sqlite3NameFromToken(db, pTable);
3311   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
3312   return pList;
3313 }
3314 
3315 /*
3316 ** Assign VdbeCursor index numbers to all tables in a SrcList
3317 */
3318 void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
3319   int i;
3320   struct SrcList_item *pItem;
3321   assert(pList || pParse->db->mallocFailed );
3322   if( pList ){
3323     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
3324       if( pItem->iCursor>=0 ) break;
3325       pItem->iCursor = pParse->nTab++;
3326       if( pItem->pSelect ){
3327         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
3328       }
3329     }
3330   }
3331 }
3332 
3333 /*
3334 ** Delete an entire SrcList including all its substructure.
3335 */
3336 void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
3337   int i;
3338   struct SrcList_item *pItem;
3339   if( pList==0 ) return;
3340   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
3341     sqlite3DbFree(db, pItem->zDatabase);
3342     sqlite3DbFree(db, pItem->zName);
3343     sqlite3DbFree(db, pItem->zAlias);
3344     sqlite3DbFree(db, pItem->zIndex);
3345     sqlite3DeleteTable(db, pItem->pTab);
3346     sqlite3SelectDelete(db, pItem->pSelect);
3347     sqlite3ExprDelete(db, pItem->pOn);
3348     sqlite3IdListDelete(db, pItem->pUsing);
3349   }
3350   sqlite3DbFree(db, pList);
3351 }
3352 
3353 /*
3354 ** This routine is called by the parser to add a new term to the
3355 ** end of a growing FROM clause.  The "p" parameter is the part of
3356 ** the FROM clause that has already been constructed.  "p" is NULL
3357 ** if this is the first term of the FROM clause.  pTable and pDatabase
3358 ** are the name of the table and database named in the FROM clause term.
3359 ** pDatabase is NULL if the database name qualifier is missing - the
3360 ** usual case.  If the term has a alias, then pAlias points to the
3361 ** alias token.  If the term is a subquery, then pSubquery is the
3362 ** SELECT statement that the subquery encodes.  The pTable and
3363 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
3364 ** parameters are the content of the ON and USING clauses.
3365 **
3366 ** Return a new SrcList which encodes is the FROM with the new
3367 ** term added.
3368 */
3369 SrcList *sqlite3SrcListAppendFromTerm(
3370   Parse *pParse,          /* Parsing context */
3371   SrcList *p,             /* The left part of the FROM clause already seen */
3372   Token *pTable,          /* Name of the table to add to the FROM clause */
3373   Token *pDatabase,       /* Name of the database containing pTable */
3374   Token *pAlias,          /* The right-hand side of the AS subexpression */
3375   Select *pSubquery,      /* A subquery used in place of a table name */
3376   Expr *pOn,              /* The ON clause of a join */
3377   IdList *pUsing          /* The USING clause of a join */
3378 ){
3379   struct SrcList_item *pItem;
3380   sqlite3 *db = pParse->db;
3381   if( !p && (pOn || pUsing) ){
3382     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
3383       (pOn ? "ON" : "USING")
3384     );
3385     goto append_from_error;
3386   }
3387   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
3388   if( p==0 || NEVER(p->nSrc==0) ){
3389     goto append_from_error;
3390   }
3391   pItem = &p->a[p->nSrc-1];
3392   assert( pAlias!=0 );
3393   if( pAlias->n ){
3394     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
3395   }
3396   pItem->pSelect = pSubquery;
3397   pItem->pOn = pOn;
3398   pItem->pUsing = pUsing;
3399   return p;
3400 
3401  append_from_error:
3402   assert( p==0 );
3403   sqlite3ExprDelete(db, pOn);
3404   sqlite3IdListDelete(db, pUsing);
3405   sqlite3SelectDelete(db, pSubquery);
3406   return 0;
3407 }
3408 
3409 /*
3410 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
3411 ** element of the source-list passed as the second argument.
3412 */
3413 void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
3414   assert( pIndexedBy!=0 );
3415   if( p && ALWAYS(p->nSrc>0) ){
3416     struct SrcList_item *pItem = &p->a[p->nSrc-1];
3417     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
3418     if( pIndexedBy->n==1 && !pIndexedBy->z ){
3419       /* A "NOT INDEXED" clause was supplied. See parse.y
3420       ** construct "indexed_opt" for details. */
3421       pItem->notIndexed = 1;
3422     }else{
3423       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
3424     }
3425   }
3426 }
3427 
3428 /*
3429 ** When building up a FROM clause in the parser, the join operator
3430 ** is initially attached to the left operand.  But the code generator
3431 ** expects the join operator to be on the right operand.  This routine
3432 ** Shifts all join operators from left to right for an entire FROM
3433 ** clause.
3434 **
3435 ** Example: Suppose the join is like this:
3436 **
3437 **           A natural cross join B
3438 **
3439 ** The operator is "natural cross join".  The A and B operands are stored
3440 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
3441 ** operator with A.  This routine shifts that operator over to B.
3442 */
3443 void sqlite3SrcListShiftJoinType(SrcList *p){
3444   if( p ){
3445     int i;
3446     assert( p->a || p->nSrc==0 );
3447     for(i=p->nSrc-1; i>0; i--){
3448       p->a[i].jointype = p->a[i-1].jointype;
3449     }
3450     p->a[0].jointype = 0;
3451   }
3452 }
3453 
3454 /*
3455 ** Begin a transaction
3456 */
3457 void sqlite3BeginTransaction(Parse *pParse, int type){
3458   sqlite3 *db;
3459   Vdbe *v;
3460   int i;
3461 
3462   assert( pParse!=0 );
3463   db = pParse->db;
3464   assert( db!=0 );
3465 /*  if( db->aDb[0].pBt==0 ) return; */
3466   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
3467     return;
3468   }
3469   v = sqlite3GetVdbe(pParse);
3470   if( !v ) return;
3471   if( type!=TK_DEFERRED ){
3472     for(i=0; i<db->nDb; i++){
3473       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
3474       sqlite3VdbeUsesBtree(v, i);
3475     }
3476   }
3477   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
3478 }
3479 
3480 /*
3481 ** Commit a transaction
3482 */
3483 void sqlite3CommitTransaction(Parse *pParse){
3484   Vdbe *v;
3485 
3486   assert( pParse!=0 );
3487   assert( pParse->db!=0 );
3488   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
3489     return;
3490   }
3491   v = sqlite3GetVdbe(pParse);
3492   if( v ){
3493     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
3494   }
3495 }
3496 
3497 /*
3498 ** Rollback a transaction
3499 */
3500 void sqlite3RollbackTransaction(Parse *pParse){
3501   Vdbe *v;
3502 
3503   assert( pParse!=0 );
3504   assert( pParse->db!=0 );
3505   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
3506     return;
3507   }
3508   v = sqlite3GetVdbe(pParse);
3509   if( v ){
3510     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
3511   }
3512 }
3513 
3514 /*
3515 ** This function is called by the parser when it parses a command to create,
3516 ** release or rollback an SQL savepoint.
3517 */
3518 void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
3519   char *zName = sqlite3NameFromToken(pParse->db, pName);
3520   if( zName ){
3521     Vdbe *v = sqlite3GetVdbe(pParse);
3522 #ifndef SQLITE_OMIT_AUTHORIZATION
3523     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
3524     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
3525 #endif
3526     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
3527       sqlite3DbFree(pParse->db, zName);
3528       return;
3529     }
3530     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
3531   }
3532 }
3533 
3534 /*
3535 ** Make sure the TEMP database is open and available for use.  Return
3536 ** the number of errors.  Leave any error messages in the pParse structure.
3537 */
3538 int sqlite3OpenTempDatabase(Parse *pParse){
3539   sqlite3 *db = pParse->db;
3540   if( db->aDb[1].pBt==0 && !pParse->explain ){
3541     int rc;
3542     Btree *pBt;
3543     static const int flags =
3544           SQLITE_OPEN_READWRITE |
3545           SQLITE_OPEN_CREATE |
3546           SQLITE_OPEN_EXCLUSIVE |
3547           SQLITE_OPEN_DELETEONCLOSE |
3548           SQLITE_OPEN_TEMP_DB;
3549 
3550     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
3551     if( rc!=SQLITE_OK ){
3552       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
3553         "file for storing temporary tables");
3554       pParse->rc = rc;
3555       return 1;
3556     }
3557     db->aDb[1].pBt = pBt;
3558     assert( db->aDb[1].pSchema );
3559     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
3560       db->mallocFailed = 1;
3561       return 1;
3562     }
3563   }
3564   return 0;
3565 }
3566 
3567 /*
3568 ** Generate VDBE code that will verify the schema cookie and start
3569 ** a read-transaction for all named database files.
3570 **
3571 ** It is important that all schema cookies be verified and all
3572 ** read transactions be started before anything else happens in
3573 ** the VDBE program.  But this routine can be called after much other
3574 ** code has been generated.  So here is what we do:
3575 **
3576 ** The first time this routine is called, we code an OP_Goto that
3577 ** will jump to a subroutine at the end of the program.  Then we
3578 ** record every database that needs its schema verified in the
3579 ** pParse->cookieMask field.  Later, after all other code has been
3580 ** generated, the subroutine that does the cookie verifications and
3581 ** starts the transactions will be coded and the OP_Goto P2 value
3582 ** will be made to point to that subroutine.  The generation of the
3583 ** cookie verification subroutine code happens in sqlite3FinishCoding().
3584 **
3585 ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
3586 ** schema on any databases.  This can be used to position the OP_Goto
3587 ** early in the code, before we know if any database tables will be used.
3588 */
3589 void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
3590   Parse *pToplevel = sqlite3ParseToplevel(pParse);
3591 
3592   if( pToplevel->cookieGoto==0 ){
3593     Vdbe *v = sqlite3GetVdbe(pToplevel);
3594     if( v==0 ) return;  /* This only happens if there was a prior error */
3595     pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
3596   }
3597   if( iDb>=0 ){
3598     sqlite3 *db = pToplevel->db;
3599     yDbMask mask;
3600 
3601     assert( iDb<db->nDb );
3602     assert( db->aDb[iDb].pBt!=0 || iDb==1 );
3603     assert( iDb<SQLITE_MAX_ATTACHED+2 );
3604     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
3605     mask = ((yDbMask)1)<<iDb;
3606     if( (pToplevel->cookieMask & mask)==0 ){
3607       pToplevel->cookieMask |= mask;
3608       pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
3609       if( !OMIT_TEMPDB && iDb==1 ){
3610         sqlite3OpenTempDatabase(pToplevel);
3611       }
3612     }
3613   }
3614 }
3615 
3616 /*
3617 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
3618 ** attached database. Otherwise, invoke it for the database named zDb only.
3619 */
3620 void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
3621   sqlite3 *db = pParse->db;
3622   int i;
3623   for(i=0; i<db->nDb; i++){
3624     Db *pDb = &db->aDb[i];
3625     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
3626       sqlite3CodeVerifySchema(pParse, i);
3627     }
3628   }
3629 }
3630 
3631 /*
3632 ** Generate VDBE code that prepares for doing an operation that
3633 ** might change the database.
3634 **
3635 ** This routine starts a new transaction if we are not already within
3636 ** a transaction.  If we are already within a transaction, then a checkpoint
3637 ** is set if the setStatement parameter is true.  A checkpoint should
3638 ** be set for operations that might fail (due to a constraint) part of
3639 ** the way through and which will need to undo some writes without having to
3640 ** rollback the whole transaction.  For operations where all constraints
3641 ** can be checked before any changes are made to the database, it is never
3642 ** necessary to undo a write and the checkpoint should not be set.
3643 */
3644 void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
3645   Parse *pToplevel = sqlite3ParseToplevel(pParse);
3646   sqlite3CodeVerifySchema(pParse, iDb);
3647   pToplevel->writeMask |= ((yDbMask)1)<<iDb;
3648   pToplevel->isMultiWrite |= setStatement;
3649 }
3650 
3651 /*
3652 ** Indicate that the statement currently under construction might write
3653 ** more than one entry (example: deleting one row then inserting another,
3654 ** inserting multiple rows in a table, or inserting a row and index entries.)
3655 ** If an abort occurs after some of these writes have completed, then it will
3656 ** be necessary to undo the completed writes.
3657 */
3658 void sqlite3MultiWrite(Parse *pParse){
3659   Parse *pToplevel = sqlite3ParseToplevel(pParse);
3660   pToplevel->isMultiWrite = 1;
3661 }
3662 
3663 /*
3664 ** The code generator calls this routine if is discovers that it is
3665 ** possible to abort a statement prior to completion.  In order to
3666 ** perform this abort without corrupting the database, we need to make
3667 ** sure that the statement is protected by a statement transaction.
3668 **
3669 ** Technically, we only need to set the mayAbort flag if the
3670 ** isMultiWrite flag was previously set.  There is a time dependency
3671 ** such that the abort must occur after the multiwrite.  This makes
3672 ** some statements involving the REPLACE conflict resolution algorithm
3673 ** go a little faster.  But taking advantage of this time dependency
3674 ** makes it more difficult to prove that the code is correct (in
3675 ** particular, it prevents us from writing an effective
3676 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
3677 ** to take the safe route and skip the optimization.
3678 */
3679 void sqlite3MayAbort(Parse *pParse){
3680   Parse *pToplevel = sqlite3ParseToplevel(pParse);
3681   pToplevel->mayAbort = 1;
3682 }
3683 
3684 /*
3685 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
3686 ** error. The onError parameter determines which (if any) of the statement
3687 ** and/or current transaction is rolled back.
3688 */
3689 void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){
3690   Vdbe *v = sqlite3GetVdbe(pParse);
3691   if( onError==OE_Abort ){
3692     sqlite3MayAbort(pParse);
3693   }
3694   sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type);
3695 }
3696 
3697 /*
3698 ** Check to see if pIndex uses the collating sequence pColl.  Return
3699 ** true if it does and false if it does not.
3700 */
3701 #ifndef SQLITE_OMIT_REINDEX
3702 static int collationMatch(const char *zColl, Index *pIndex){
3703   int i;
3704   assert( zColl!=0 );
3705   for(i=0; i<pIndex->nColumn; i++){
3706     const char *z = pIndex->azColl[i];
3707     assert( z!=0 );
3708     if( 0==sqlite3StrICmp(z, zColl) ){
3709       return 1;
3710     }
3711   }
3712   return 0;
3713 }
3714 #endif
3715 
3716 /*
3717 ** Recompute all indices of pTab that use the collating sequence pColl.
3718 ** If pColl==0 then recompute all indices of pTab.
3719 */
3720 #ifndef SQLITE_OMIT_REINDEX
3721 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
3722   Index *pIndex;              /* An index associated with pTab */
3723 
3724   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
3725     if( zColl==0 || collationMatch(zColl, pIndex) ){
3726       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
3727       sqlite3BeginWriteOperation(pParse, 0, iDb);
3728       sqlite3RefillIndex(pParse, pIndex, -1);
3729     }
3730   }
3731 }
3732 #endif
3733 
3734 /*
3735 ** Recompute all indices of all tables in all databases where the
3736 ** indices use the collating sequence pColl.  If pColl==0 then recompute
3737 ** all indices everywhere.
3738 */
3739 #ifndef SQLITE_OMIT_REINDEX
3740 static void reindexDatabases(Parse *pParse, char const *zColl){
3741   Db *pDb;                    /* A single database */
3742   int iDb;                    /* The database index number */
3743   sqlite3 *db = pParse->db;   /* The database connection */
3744   HashElem *k;                /* For looping over tables in pDb */
3745   Table *pTab;                /* A table in the database */
3746 
3747   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
3748   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
3749     assert( pDb!=0 );
3750     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
3751       pTab = (Table*)sqliteHashData(k);
3752       reindexTable(pParse, pTab, zColl);
3753     }
3754   }
3755 }
3756 #endif
3757 
3758 /*
3759 ** Generate code for the REINDEX command.
3760 **
3761 **        REINDEX                            -- 1
3762 **        REINDEX  <collation>               -- 2
3763 **        REINDEX  ?<database>.?<tablename>  -- 3
3764 **        REINDEX  ?<database>.?<indexname>  -- 4
3765 **
3766 ** Form 1 causes all indices in all attached databases to be rebuilt.
3767 ** Form 2 rebuilds all indices in all databases that use the named
3768 ** collating function.  Forms 3 and 4 rebuild the named index or all
3769 ** indices associated with the named table.
3770 */
3771 #ifndef SQLITE_OMIT_REINDEX
3772 void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
3773   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
3774   char *z;                    /* Name of a table or index */
3775   const char *zDb;            /* Name of the database */
3776   Table *pTab;                /* A table in the database */
3777   Index *pIndex;              /* An index associated with pTab */
3778   int iDb;                    /* The database index number */
3779   sqlite3 *db = pParse->db;   /* The database connection */
3780   Token *pObjName;            /* Name of the table or index to be reindexed */
3781 
3782   /* Read the database schema. If an error occurs, leave an error message
3783   ** and code in pParse and return NULL. */
3784   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
3785     return;
3786   }
3787 
3788   if( pName1==0 ){
3789     reindexDatabases(pParse, 0);
3790     return;
3791   }else if( NEVER(pName2==0) || pName2->z==0 ){
3792     char *zColl;
3793     assert( pName1->z );
3794     zColl = sqlite3NameFromToken(pParse->db, pName1);
3795     if( !zColl ) return;
3796     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
3797     if( pColl ){
3798       reindexDatabases(pParse, zColl);
3799       sqlite3DbFree(db, zColl);
3800       return;
3801     }
3802     sqlite3DbFree(db, zColl);
3803   }
3804   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
3805   if( iDb<0 ) return;
3806   z = sqlite3NameFromToken(db, pObjName);
3807   if( z==0 ) return;
3808   zDb = db->aDb[iDb].zName;
3809   pTab = sqlite3FindTable(db, z, zDb);
3810   if( pTab ){
3811     reindexTable(pParse, pTab, 0);
3812     sqlite3DbFree(db, z);
3813     return;
3814   }
3815   pIndex = sqlite3FindIndex(db, z, zDb);
3816   sqlite3DbFree(db, z);
3817   if( pIndex ){
3818     sqlite3BeginWriteOperation(pParse, 0, iDb);
3819     sqlite3RefillIndex(pParse, pIndex, -1);
3820     return;
3821   }
3822   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
3823 }
3824 #endif
3825 
3826 /*
3827 ** Return a dynamicly allocated KeyInfo structure that can be used
3828 ** with OP_OpenRead or OP_OpenWrite to access database index pIdx.
3829 **
3830 ** If successful, a pointer to the new structure is returned. In this case
3831 ** the caller is responsible for calling sqlite3DbFree(db, ) on the returned
3832 ** pointer. If an error occurs (out of memory or missing collation
3833 ** sequence), NULL is returned and the state of pParse updated to reflect
3834 ** the error.
3835 */
3836 KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){
3837   int i;
3838   int nCol = pIdx->nColumn;
3839   int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol;
3840   sqlite3 *db = pParse->db;
3841   KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes);
3842 
3843   if( pKey ){
3844     pKey->db = pParse->db;
3845     pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]);
3846     assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) );
3847     for(i=0; i<nCol; i++){
3848       char *zColl = pIdx->azColl[i];
3849       assert( zColl );
3850       pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl);
3851       pKey->aSortOrder[i] = pIdx->aSortOrder[i];
3852     }
3853     pKey->nField = (u16)nCol;
3854   }
3855 
3856   if( pParse->nErr ){
3857     sqlite3DbFree(db, pKey);
3858     pKey = 0;
3859   }
3860   return pKey;
3861 }
3862