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