xref: /illumos-gate/usr/src/lib/libsqlite/src/build.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 **     PRAGMA
25 **
26 ** $Id: build.c,v 1.176.2.2 2004/07/20 00:50:30 drh Exp $
27 */
28 #include "sqliteInt.h"
29 #include <ctype.h>
30 
31 /*
32 ** This routine is called when a new SQL statement is beginning to
33 ** be parsed.  Check to see if the schema for the database needs
34 ** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
35 ** If it does, then read it.
36 */
sqliteBeginParse(Parse * pParse,int explainFlag)37 void sqliteBeginParse(Parse *pParse, int explainFlag){
38   sqlite *db = pParse->db;
39   int i;
40   pParse->explain = explainFlag;
41   if((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){
42     int rc = sqliteInit(db, &pParse->zErrMsg);
43     if( rc!=SQLITE_OK ){
44       pParse->rc = rc;
45       pParse->nErr++;
46     }
47   }
48   for(i=0; i<db->nDb; i++){
49     DbClearProperty(db, i, DB_Locked);
50     if( !db->aDb[i].inTrans ){
51       DbClearProperty(db, i, DB_Cookie);
52     }
53   }
54   pParse->nVar = 0;
55 }
56 
57 /*
58 ** This routine is called after a single SQL statement has been
59 ** parsed and we want to execute the VDBE code to implement
60 ** that statement.  Prior action routines should have already
61 ** constructed VDBE code to do the work of the SQL statement.
62 ** This routine just has to execute the VDBE code.
63 **
64 ** Note that if an error occurred, it might be the case that
65 ** no VDBE code was generated.
66 */
sqliteExec(Parse * pParse)67 void sqliteExec(Parse *pParse){
68   sqlite *db = pParse->db;
69   Vdbe *v = pParse->pVdbe;
70 
71   if( v==0 && (v = sqliteGetVdbe(pParse))!=0 ){
72     sqliteVdbeAddOp(v, OP_Halt, 0, 0);
73   }
74   if( sqlite_malloc_failed ) return;
75   if( v && pParse->nErr==0 ){
76     FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
77     sqliteVdbeTrace(v, trace);
78     sqliteVdbeMakeReady(v, pParse->nVar, pParse->explain);
79     pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
80     pParse->colNamesSet = 0;
81   }else if( pParse->rc==SQLITE_OK ){
82     pParse->rc = SQLITE_ERROR;
83   }
84   pParse->nTab = 0;
85   pParse->nMem = 0;
86   pParse->nSet = 0;
87   pParse->nAgg = 0;
88   pParse->nVar = 0;
89 }
90 
91 /*
92 ** Locate the in-memory structure that describes
93 ** a particular database table given the name
94 ** of that table and (optionally) the name of the database
95 ** containing the table.  Return NULL if not found.
96 **
97 ** If zDatabase is 0, all databases are searched for the
98 ** table and the first matching table is returned.  (No checking
99 ** for duplicate table names is done.)  The search order is
100 ** TEMP first, then MAIN, then any auxiliary databases added
101 ** using the ATTACH command.
102 **
103 ** See also sqliteLocateTable().
104 */
sqliteFindTable(sqlite * db,const char * zName,const char * zDatabase)105 Table *sqliteFindTable(sqlite *db, const char *zName, const char *zDatabase){
106   Table *p = 0;
107   int i;
108   for(i=0; i<db->nDb; i++){
109     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
110     if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) ) continue;
111     p = sqliteHashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
112     if( p ) break;
113   }
114   return p;
115 }
116 
117 /*
118 ** Locate the in-memory structure that describes
119 ** a particular database table given the name
120 ** of that table and (optionally) the name of the database
121 ** containing the table.  Return NULL if not found.
122 ** Also leave an error message in pParse->zErrMsg.
123 **
124 ** The difference between this routine and sqliteFindTable()
125 ** is that this routine leaves an error message in pParse->zErrMsg
126 ** where sqliteFindTable() does not.
127 */
sqliteLocateTable(Parse * pParse,const char * zName,const char * zDbase)128 Table *sqliteLocateTable(Parse *pParse, const char *zName, const char *zDbase){
129   Table *p;
130 
131   p = sqliteFindTable(pParse->db, zName, zDbase);
132   if( p==0 ){
133     if( zDbase ){
134       sqliteErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
135     }else if( sqliteFindTable(pParse->db, zName, 0)!=0 ){
136       sqliteErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
137          zName, zDbase);
138     }else{
139       sqliteErrorMsg(pParse, "no such table: %s", zName);
140     }
141   }
142   return p;
143 }
144 
145 /*
146 ** Locate the in-memory structure that describes
147 ** a particular index given the name of that index
148 ** and the name of the database that contains the index.
149 ** Return NULL if not found.
150 **
151 ** If zDatabase is 0, all databases are searched for the
152 ** table and the first matching index is returned.  (No checking
153 ** for duplicate index names is done.)  The search order is
154 ** TEMP first, then MAIN, then any auxiliary databases added
155 ** using the ATTACH command.
156 */
sqliteFindIndex(sqlite * db,const char * zName,const char * zDb)157 Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){
158   Index *p = 0;
159   int i;
160   for(i=0; i<db->nDb; i++){
161     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
162     if( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) ) continue;
163     p = sqliteHashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
164     if( p ) break;
165   }
166   return p;
167 }
168 
169 /*
170 ** Remove the given index from the index hash table, and free
171 ** its memory structures.
172 **
173 ** The index is removed from the database hash tables but
174 ** it is not unlinked from the Table that it indexes.
175 ** Unlinking from the Table must be done by the calling function.
176 */
sqliteDeleteIndex(sqlite * db,Index * p)177 static void sqliteDeleteIndex(sqlite *db, Index *p){
178   Index *pOld;
179 
180   assert( db!=0 && p->zName!=0 );
181   pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
182                           strlen(p->zName)+1, 0);
183   if( pOld!=0 && pOld!=p ){
184     sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
185                      strlen(pOld->zName)+1, pOld);
186   }
187   sqliteFree(p);
188 }
189 
190 /*
191 ** Unlink the given index from its table, then remove
192 ** the index from the index hash table and free its memory
193 ** structures.
194 */
sqliteUnlinkAndDeleteIndex(sqlite * db,Index * pIndex)195 void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
196   if( pIndex->pTable->pIndex==pIndex ){
197     pIndex->pTable->pIndex = pIndex->pNext;
198   }else{
199     Index *p;
200     for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
201     if( p && p->pNext==pIndex ){
202       p->pNext = pIndex->pNext;
203     }
204   }
205   sqliteDeleteIndex(db, pIndex);
206 }
207 
208 /*
209 ** Erase all schema information from the in-memory hash tables of
210 ** database connection.  This routine is called to reclaim memory
211 ** before the connection closes.  It is also called during a rollback
212 ** if there were schema changes during the transaction.
213 **
214 ** If iDb<=0 then reset the internal schema tables for all database
215 ** files.  If iDb>=2 then reset the internal schema for only the
216 ** single file indicated.
217 */
sqliteResetInternalSchema(sqlite * db,int iDb)218 void sqliteResetInternalSchema(sqlite *db, int iDb){
219   HashElem *pElem;
220   Hash temp1;
221   Hash temp2;
222   int i, j;
223 
224   assert( iDb>=0 && iDb<db->nDb );
225   db->flags &= ~SQLITE_Initialized;
226   for(i=iDb; i<db->nDb; i++){
227     Db *pDb = &db->aDb[i];
228     temp1 = pDb->tblHash;
229     temp2 = pDb->trigHash;
230     sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
231     sqliteHashClear(&pDb->aFKey);
232     sqliteHashClear(&pDb->idxHash);
233     for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
234       Trigger *pTrigger = sqliteHashData(pElem);
235       sqliteDeleteTrigger(pTrigger);
236     }
237     sqliteHashClear(&temp2);
238     sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
239     for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
240       Table *pTab = sqliteHashData(pElem);
241       sqliteDeleteTable(db, pTab);
242     }
243     sqliteHashClear(&temp1);
244     DbClearProperty(db, i, DB_SchemaLoaded);
245     if( iDb>0 ) return;
246   }
247   assert( iDb==0 );
248   db->flags &= ~SQLITE_InternChanges;
249 
250   /* If one or more of the auxiliary database files has been closed,
251   ** then remove then from the auxiliary database list.  We take the
252   ** opportunity to do this here since we have just deleted all of the
253   ** schema hash tables and therefore do not have to make any changes
254   ** to any of those tables.
255   */
256   for(i=0; i<db->nDb; i++){
257     struct Db *pDb = &db->aDb[i];
258     if( pDb->pBt==0 ){
259       if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
260       pDb->pAux = 0;
261     }
262   }
263   for(i=j=2; i<db->nDb; i++){
264     struct Db *pDb = &db->aDb[i];
265     if( pDb->pBt==0 ){
266       sqliteFree(pDb->zName);
267       pDb->zName = 0;
268       continue;
269     }
270     if( j<i ){
271       db->aDb[j] = db->aDb[i];
272     }
273     j++;
274   }
275   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
276   db->nDb = j;
277   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
278     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
279     sqliteFree(db->aDb);
280     db->aDb = db->aDbStatic;
281   }
282 }
283 
284 /*
285 ** This routine is called whenever a rollback occurs.  If there were
286 ** schema changes during the transaction, then we have to reset the
287 ** internal hash tables and reload them from disk.
288 */
sqliteRollbackInternalChanges(sqlite * db)289 void sqliteRollbackInternalChanges(sqlite *db){
290   if( db->flags & SQLITE_InternChanges ){
291     sqliteResetInternalSchema(db, 0);
292   }
293 }
294 
295 /*
296 ** This routine is called when a commit occurs.
297 */
sqliteCommitInternalChanges(sqlite * db)298 void sqliteCommitInternalChanges(sqlite *db){
299   db->aDb[0].schema_cookie = db->next_cookie;
300   db->flags &= ~SQLITE_InternChanges;
301 }
302 
303 /*
304 ** Remove the memory data structures associated with the given
305 ** Table.  No changes are made to disk by this routine.
306 **
307 ** This routine just deletes the data structure.  It does not unlink
308 ** the table data structure from the hash table.  Nor does it remove
309 ** foreign keys from the sqlite.aFKey hash table.  But it does destroy
310 ** memory structures of the indices and foreign keys associated with
311 ** the table.
312 **
313 ** Indices associated with the table are unlinked from the "db"
314 ** data structure if db!=NULL.  If db==NULL, indices attached to
315 ** the table are deleted, but it is assumed they have already been
316 ** unlinked.
317 */
sqliteDeleteTable(sqlite * db,Table * pTable)318 void sqliteDeleteTable(sqlite *db, Table *pTable){
319   int i;
320   Index *pIndex, *pNext;
321   FKey *pFKey, *pNextFKey;
322 
323   if( pTable==0 ) return;
324 
325   /* Delete all indices associated with this table
326   */
327   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
328     pNext = pIndex->pNext;
329     assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
330     sqliteDeleteIndex(db, pIndex);
331   }
332 
333   /* Delete all foreign keys associated with this table.  The keys
334   ** should have already been unlinked from the db->aFKey hash table
335   */
336   for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
337     pNextFKey = pFKey->pNextFrom;
338     assert( pTable->iDb<db->nDb );
339     assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
340                            pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
341     sqliteFree(pFKey);
342   }
343 
344   /* Delete the Table structure itself.
345   */
346   for(i=0; i<pTable->nCol; i++){
347     sqliteFree(pTable->aCol[i].zName);
348     sqliteFree(pTable->aCol[i].zDflt);
349     sqliteFree(pTable->aCol[i].zType);
350   }
351   sqliteFree(pTable->zName);
352   sqliteFree(pTable->aCol);
353   sqliteSelectDelete(pTable->pSelect);
354   sqliteFree(pTable);
355 }
356 
357 /*
358 ** Unlink the given table from the hash tables and the delete the
359 ** table structure with all its indices and foreign keys.
360 */
sqliteUnlinkAndDeleteTable(sqlite * db,Table * p)361 static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
362   Table *pOld;
363   FKey *pF1, *pF2;
364   int i = p->iDb;
365   assert( db!=0 );
366   pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
367   assert( pOld==0 || pOld==p );
368   for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
369     int nTo = strlen(pF1->zTo) + 1;
370     pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
371     if( pF2==pF1 ){
372       sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
373     }else{
374       while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
375       if( pF2 ){
376         pF2->pNextTo = pF1->pNextTo;
377       }
378     }
379   }
380   sqliteDeleteTable(db, p);
381 }
382 
383 /*
384 ** Construct the name of a user table or index from a token.
385 **
386 ** Space to hold the name is obtained from sqliteMalloc() and must
387 ** be freed by the calling function.
388 */
sqliteTableNameFromToken(Token * pName)389 char *sqliteTableNameFromToken(Token *pName){
390   char *zName = sqliteStrNDup(pName->z, pName->n);
391   sqliteDequote(zName);
392   return zName;
393 }
394 
395 /*
396 ** Generate code to open the appropriate master table.  The table
397 ** opened will be SQLITE_MASTER for persistent tables and
398 ** SQLITE_TEMP_MASTER for temporary tables.  The table is opened
399 ** on cursor 0.
400 */
sqliteOpenMasterTable(Vdbe * v,int isTemp)401 void sqliteOpenMasterTable(Vdbe *v, int isTemp){
402   sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
403   sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
404 }
405 
406 /*
407 ** Begin constructing a new table representation in memory.  This is
408 ** the first of several action routines that get called in response
409 ** to a CREATE TABLE statement.  In particular, this routine is called
410 ** after seeing tokens "CREATE" and "TABLE" and the table name.  The
411 ** pStart token is the CREATE and pName is the table name.  The isTemp
412 ** flag is true if the table should be stored in the auxiliary database
413 ** file instead of in the main database file.  This is normally the case
414 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
415 ** CREATE and TABLE.
416 **
417 ** The new table record is initialized and put in pParse->pNewTable.
418 ** As more of the CREATE TABLE statement is parsed, additional action
419 ** routines will be called to add more information to this record.
420 ** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
421 ** is called to complete the construction of the new table record.
422 */
sqliteStartTable(Parse * pParse,Token * pStart,Token * pName,int isTemp,int isView)423 void sqliteStartTable(
424   Parse *pParse,   /* Parser context */
425   Token *pStart,   /* The "CREATE" token */
426   Token *pName,    /* Name of table or view to create */
427   int isTemp,      /* True if this is a TEMP table */
428   int isView       /* True if this is a VIEW */
429 ){
430   Table *pTable;
431   Index *pIdx;
432   char *zName;
433   sqlite *db = pParse->db;
434   Vdbe *v;
435   int iDb;
436 
437   pParse->sFirstToken = *pStart;
438   zName = sqliteTableNameFromToken(pName);
439   if( zName==0 ) return;
440   if( db->init.iDb==1 ) isTemp = 1;
441 #ifndef SQLITE_OMIT_AUTHORIZATION
442   assert( (isTemp & 1)==isTemp );
443   {
444     int code;
445     char *zDb = isTemp ? "temp" : "main";
446     if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
447       sqliteFree(zName);
448       return;
449     }
450     if( isView ){
451       if( isTemp ){
452         code = SQLITE_CREATE_TEMP_VIEW;
453       }else{
454         code = SQLITE_CREATE_VIEW;
455       }
456     }else{
457       if( isTemp ){
458         code = SQLITE_CREATE_TEMP_TABLE;
459       }else{
460         code = SQLITE_CREATE_TABLE;
461       }
462     }
463     if( sqliteAuthCheck(pParse, code, zName, 0, zDb) ){
464       sqliteFree(zName);
465       return;
466     }
467   }
468 #endif
469 
470 
471   /* Before trying to create a temporary table, make sure the Btree for
472   ** holding temporary tables is open.
473   */
474   if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
475     int rc = sqliteBtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
476     if( rc!=SQLITE_OK ){
477       sqliteErrorMsg(pParse, "unable to open a temporary database "
478         "file for storing temporary tables");
479       pParse->nErr++;
480       return;
481     }
482     if( db->flags & SQLITE_InTrans ){
483       rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
484       if( rc!=SQLITE_OK ){
485         sqliteErrorMsg(pParse, "unable to get a write lock on "
486           "the temporary database file");
487         return;
488       }
489     }
490   }
491 
492   /* Make sure the new table name does not collide with an existing
493   ** index or table name.  Issue an error message if it does.
494   **
495   ** If we are re-reading the sqlite_master table because of a schema
496   ** change and a new permanent table is found whose name collides with
497   ** an existing temporary table, that is not an error.
498   */
499   pTable = sqliteFindTable(db, zName, 0);
500   iDb = isTemp ? 1 : db->init.iDb;
501   if( pTable!=0 && (pTable->iDb==iDb || !db->init.busy) ){
502     sqliteErrorMsg(pParse, "table %T already exists", pName);
503     sqliteFree(zName);
504     return;
505   }
506   if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
507           (pIdx->iDb==0 || !db->init.busy) ){
508     sqliteErrorMsg(pParse, "there is already an index named %s", zName);
509     sqliteFree(zName);
510     return;
511   }
512   pTable = sqliteMalloc( sizeof(Table) );
513   if( pTable==0 ){
514     sqliteFree(zName);
515     return;
516   }
517   pTable->zName = zName;
518   pTable->nCol = 0;
519   pTable->aCol = 0;
520   pTable->iPKey = -1;
521   pTable->pIndex = 0;
522   pTable->iDb = iDb;
523   if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
524   pParse->pNewTable = pTable;
525 
526   /* Begin generating the code that will insert the table record into
527   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
528   ** and allocate the record number for the table entry now.  Before any
529   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
530   ** indices to be created and the table record must come before the
531   ** indices.  Hence, the record number for the table must be allocated
532   ** now.
533   */
534   if( !db->init.busy && (v = sqliteGetVdbe(pParse))!=0 ){
535     sqliteBeginWriteOperation(pParse, 0, isTemp);
536     if( !isTemp ){
537       sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
538       sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
539     }
540     sqliteOpenMasterTable(v, isTemp);
541     sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
542     sqliteVdbeAddOp(v, OP_Dup, 0, 0);
543     sqliteVdbeAddOp(v, OP_String, 0, 0);
544     sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
545   }
546 }
547 
548 /*
549 ** Add a new column to the table currently being constructed.
550 **
551 ** The parser calls this routine once for each column declaration
552 ** in a CREATE TABLE statement.  sqliteStartTable() gets called
553 ** first to get things going.  Then this routine is called for each
554 ** column.
555 */
sqliteAddColumn(Parse * pParse,Token * pName)556 void sqliteAddColumn(Parse *pParse, Token *pName){
557   Table *p;
558   int i;
559   char *z = 0;
560   Column *pCol;
561   if( (p = pParse->pNewTable)==0 ) return;
562   sqliteSetNString(&z, pName->z, pName->n, 0);
563   if( z==0 ) return;
564   sqliteDequote(z);
565   for(i=0; i<p->nCol; i++){
566     if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
567       sqliteErrorMsg(pParse, "duplicate column name: %s", z);
568       sqliteFree(z);
569       return;
570     }
571   }
572   if( (p->nCol & 0x7)==0 ){
573     Column *aNew;
574     aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
575     if( aNew==0 ) return;
576     p->aCol = aNew;
577   }
578   pCol = &p->aCol[p->nCol];
579   memset(pCol, 0, sizeof(p->aCol[0]));
580   pCol->zName = z;
581   pCol->sortOrder = SQLITE_SO_NUM;
582   p->nCol++;
583 }
584 
585 /*
586 ** This routine is called by the parser while in the middle of
587 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
588 ** been seen on a column.  This routine sets the notNull flag on
589 ** the column currently under construction.
590 */
sqliteAddNotNull(Parse * pParse,int onError)591 void sqliteAddNotNull(Parse *pParse, int onError){
592   Table *p;
593   int i;
594   if( (p = pParse->pNewTable)==0 ) return;
595   i = p->nCol-1;
596   if( i>=0 ) p->aCol[i].notNull = onError;
597 }
598 
599 /*
600 ** This routine is called by the parser while in the middle of
601 ** parsing a CREATE TABLE statement.  The pFirst token is the first
602 ** token in the sequence of tokens that describe the type of the
603 ** column currently under construction.   pLast is the last token
604 ** in the sequence.  Use this information to construct a string
605 ** that contains the typename of the column and store that string
606 ** in zType.
607 */
sqliteAddColumnType(Parse * pParse,Token * pFirst,Token * pLast)608 void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
609   Table *p;
610   int i, j;
611   int n;
612   char *z, **pz;
613   Column *pCol;
614   if( (p = pParse->pNewTable)==0 ) return;
615   i = p->nCol-1;
616   if( i<0 ) return;
617   pCol = &p->aCol[i];
618   pz = &pCol->zType;
619   n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
620   sqliteSetNString(pz, pFirst->z, n, 0);
621   z = *pz;
622   if( z==0 ) return;
623   for(i=j=0; z[i]; i++){
624     int c = z[i];
625     if( isspace(c) ) continue;
626     z[j++] = c;
627   }
628   z[j] = 0;
629   if( pParse->db->file_format>=4 ){
630     pCol->sortOrder = sqliteCollateType(z, n);
631   }else{
632     pCol->sortOrder = SQLITE_SO_NUM;
633   }
634 }
635 
636 /*
637 ** The given token is the default value for the last column added to
638 ** the table currently under construction.  If "minusFlag" is true, it
639 ** means the value token was preceded by a minus sign.
640 **
641 ** This routine is called by the parser while in the middle of
642 ** parsing a CREATE TABLE statement.
643 */
sqliteAddDefaultValue(Parse * pParse,Token * pVal,int minusFlag)644 void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
645   Table *p;
646   int i;
647   char **pz;
648   if( (p = pParse->pNewTable)==0 ) return;
649   i = p->nCol-1;
650   if( i<0 ) return;
651   pz = &p->aCol[i].zDflt;
652   if( minusFlag ){
653     sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
654   }else{
655     sqliteSetNString(pz, pVal->z, pVal->n, 0);
656   }
657   sqliteDequote(*pz);
658 }
659 
660 /*
661 ** Designate the PRIMARY KEY for the table.  pList is a list of names
662 ** of columns that form the primary key.  If pList is NULL, then the
663 ** most recently added column of the table is the primary key.
664 **
665 ** A table can have at most one primary key.  If the table already has
666 ** a primary key (and this is the second primary key) then create an
667 ** error.
668 **
669 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
670 ** then we will try to use that column as the row id.  (Exception:
671 ** For backwards compatibility with older databases, do not do this
672 ** if the file format version number is less than 1.)  Set the Table.iPKey
673 ** field of the table under construction to be the index of the
674 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
675 ** no INTEGER PRIMARY KEY.
676 **
677 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
678 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
679 */
sqliteAddPrimaryKey(Parse * pParse,IdList * pList,int onError)680 void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
681   Table *pTab = pParse->pNewTable;
682   char *zType = 0;
683   int iCol = -1, i;
684   if( pTab==0 ) goto primary_key_exit;
685   if( pTab->hasPrimKey ){
686     sqliteErrorMsg(pParse,
687       "table \"%s\" has more than one primary key", pTab->zName);
688     goto primary_key_exit;
689   }
690   pTab->hasPrimKey = 1;
691   if( pList==0 ){
692     iCol = pTab->nCol - 1;
693     pTab->aCol[iCol].isPrimKey = 1;
694   }else{
695     for(i=0; i<pList->nId; i++){
696       for(iCol=0; iCol<pTab->nCol; iCol++){
697         if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ) break;
698       }
699       if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
700     }
701     if( pList->nId>1 ) iCol = -1;
702   }
703   if( iCol>=0 && iCol<pTab->nCol ){
704     zType = pTab->aCol[iCol].zType;
705   }
706   if( pParse->db->file_format>=1 &&
707            zType && sqliteStrICmp(zType, "INTEGER")==0 ){
708     pTab->iPKey = iCol;
709     pTab->keyConf = onError;
710   }else{
711     sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
712     pList = 0;
713   }
714 
715 primary_key_exit:
716   sqliteIdListDelete(pList);
717   return;
718 }
719 
720 /*
721 ** Return the appropriate collating type given a type name.
722 **
723 ** The collation type is text (SQLITE_SO_TEXT) if the type
724 ** name contains the character stream "text" or "blob" or
725 ** "clob".  Any other type name is collated as numeric
726 ** (SQLITE_SO_NUM).
727 */
sqliteCollateType(const char * zType,int nType)728 int sqliteCollateType(const char *zType, int nType){
729   int i;
730   for(i=0; i<nType-3; i++){
731     int c = *(zType++) | 0x60;
732     if( (c=='b' || c=='c') && sqliteStrNICmp(zType, "lob", 3)==0 ){
733       return SQLITE_SO_TEXT;
734     }
735     if( c=='c' && sqliteStrNICmp(zType, "har", 3)==0 ){
736       return SQLITE_SO_TEXT;
737     }
738     if( c=='t' && sqliteStrNICmp(zType, "ext", 3)==0 ){
739       return SQLITE_SO_TEXT;
740     }
741   }
742   return SQLITE_SO_NUM;
743 }
744 
745 /*
746 ** This routine is called by the parser while in the middle of
747 ** parsing a CREATE TABLE statement.  A "COLLATE" clause has
748 ** been seen on a column.  This routine sets the Column.sortOrder on
749 ** the column currently under construction.
750 */
sqliteAddCollateType(Parse * pParse,int collType)751 void sqliteAddCollateType(Parse *pParse, int collType){
752   Table *p;
753   int i;
754   if( (p = pParse->pNewTable)==0 ) return;
755   i = p->nCol-1;
756   if( i>=0 ) p->aCol[i].sortOrder = collType;
757 }
758 
759 /*
760 ** Come up with a new random value for the schema cookie.  Make sure
761 ** the new value is different from the old.
762 **
763 ** The schema cookie is used to determine when the schema for the
764 ** database changes.  After each schema change, the cookie value
765 ** changes.  When a process first reads the schema it records the
766 ** cookie.  Thereafter, whenever it goes to access the database,
767 ** it checks the cookie to make sure the schema has not changed
768 ** since it was last read.
769 **
770 ** This plan is not completely bullet-proof.  It is possible for
771 ** the schema to change multiple times and for the cookie to be
772 ** set back to prior value.  But schema changes are infrequent
773 ** and the probability of hitting the same cookie value is only
774 ** 1 chance in 2^32.  So we're safe enough.
775 */
sqliteChangeCookie(sqlite * db,Vdbe * v)776 void sqliteChangeCookie(sqlite *db, Vdbe *v){
777   if( db->next_cookie==db->aDb[0].schema_cookie ){
778     unsigned char r;
779     sqliteRandomness(1, &r);
780     db->next_cookie = db->aDb[0].schema_cookie + r + 1;
781     db->flags |= SQLITE_InternChanges;
782     sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
783     sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
784   }
785 }
786 
787 /*
788 ** Measure the number of characters needed to output the given
789 ** identifier.  The number returned includes any quotes used
790 ** but does not include the null terminator.
791 */
identLength(const char * z)792 static int identLength(const char *z){
793   int n;
794   int needQuote = 0;
795   for(n=0; *z; n++, z++){
796     if( *z=='\'' ){ n++; needQuote=1; }
797   }
798   return n + needQuote*2;
799 }
800 
801 /*
802 ** Write an identifier onto the end of the given string.  Add
803 ** quote characters as needed.
804 */
identPut(char * z,int * pIdx,char * zIdent)805 static void identPut(char *z, int *pIdx, char *zIdent){
806   int i, j, needQuote;
807   i = *pIdx;
808   for(j=0; zIdent[j]; j++){
809     if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
810   }
811   needQuote =  zIdent[j]!=0 || isdigit(zIdent[0])
812                   || sqliteKeywordCode(zIdent, j)!=TK_ID;
813   if( needQuote ) z[i++] = '\'';
814   for(j=0; zIdent[j]; j++){
815     z[i++] = zIdent[j];
816     if( zIdent[j]=='\'' ) z[i++] = '\'';
817   }
818   if( needQuote ) z[i++] = '\'';
819   z[i] = 0;
820   *pIdx = i;
821 }
822 
823 /*
824 ** Generate a CREATE TABLE statement appropriate for the given
825 ** table.  Memory to hold the text of the statement is obtained
826 ** from sqliteMalloc() and must be freed by the calling function.
827 */
createTableStmt(Table * p)828 static char *createTableStmt(Table *p){
829   int i, k, n;
830   char *zStmt;
831   char *zSep, *zSep2, *zEnd;
832   n = 0;
833   for(i=0; i<p->nCol; i++){
834     n += identLength(p->aCol[i].zName);
835   }
836   n += identLength(p->zName);
837   if( n<40 ){
838     zSep = "";
839     zSep2 = ",";
840     zEnd = ")";
841   }else{
842     zSep = "\n  ";
843     zSep2 = ",\n  ";
844     zEnd = "\n)";
845   }
846   n += 35 + 6*p->nCol;
847   zStmt = sqliteMallocRaw( n );
848   if( zStmt==0 ) return 0;
849   strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
850   k = strlen(zStmt);
851   identPut(zStmt, &k, p->zName);
852   zStmt[k++] = '(';
853   for(i=0; i<p->nCol; i++){
854     strcpy(&zStmt[k], zSep);
855     k += strlen(&zStmt[k]);
856     zSep = zSep2;
857     identPut(zStmt, &k, p->aCol[i].zName);
858   }
859   strcpy(&zStmt[k], zEnd);
860   return zStmt;
861 }
862 
863 /*
864 ** This routine is called to report the final ")" that terminates
865 ** a CREATE TABLE statement.
866 **
867 ** The table structure that other action routines have been building
868 ** is added to the internal hash tables, assuming no errors have
869 ** occurred.
870 **
871 ** An entry for the table is made in the master table on disk, unless
872 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
873 ** it means we are reading the sqlite_master table because we just
874 ** connected to the database or because the sqlite_master table has
875 ** recently changes, so the entry for this table already exists in
876 ** the sqlite_master table.  We do not want to create it again.
877 **
878 ** If the pSelect argument is not NULL, it means that this routine
879 ** was called to create a table generated from a
880 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
881 ** the new table will match the result set of the SELECT.
882 */
sqliteEndTable(Parse * pParse,Token * pEnd,Select * pSelect)883 void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
884   Table *p;
885   sqlite *db = pParse->db;
886 
887   if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
888   p = pParse->pNewTable;
889   if( p==0 ) return;
890 
891   /* If the table is generated from a SELECT, then construct the
892   ** list of columns and the text of the table.
893   */
894   if( pSelect ){
895     Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
896     if( pSelTab==0 ) return;
897     assert( p->aCol==0 );
898     p->nCol = pSelTab->nCol;
899     p->aCol = pSelTab->aCol;
900     pSelTab->nCol = 0;
901     pSelTab->aCol = 0;
902     sqliteDeleteTable(0, pSelTab);
903   }
904 
905   /* If the db->init.busy is 1 it means we are reading the SQL off the
906   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
907   ** So do not write to the disk again.  Extract the root page number
908   ** for the table from the db->init.newTnum field.  (The page number
909   ** should have been put there by the sqliteOpenCb routine.)
910   */
911   if( db->init.busy ){
912     p->tnum = db->init.newTnum;
913   }
914 
915   /* If not initializing, then create a record for the new table
916   ** in the SQLITE_MASTER table of the database.  The record number
917   ** for the new table entry should already be on the stack.
918   **
919   ** If this is a TEMPORARY table, write the entry into the auxiliary
920   ** file instead of into the main database file.
921   */
922   if( !db->init.busy ){
923     int n;
924     Vdbe *v;
925 
926     v = sqliteGetVdbe(pParse);
927     if( v==0 ) return;
928     if( p->pSelect==0 ){
929       /* A regular table */
930       sqliteVdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
931     }else{
932       /* A view */
933       sqliteVdbeAddOp(v, OP_Integer, 0, 0);
934     }
935     p->tnum = 0;
936     sqliteVdbeAddOp(v, OP_Pull, 1, 0);
937     sqliteVdbeOp3(v, OP_String, 0, 0, p->pSelect==0?"table":"view", P3_STATIC);
938     sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
939     sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
940     sqliteVdbeAddOp(v, OP_Dup, 4, 0);
941     sqliteVdbeAddOp(v, OP_String, 0, 0);
942     if( pSelect ){
943       char *z = createTableStmt(p);
944       n = z ? strlen(z) : 0;
945       sqliteVdbeChangeP3(v, -1, z, n);
946       sqliteFree(z);
947     }else{
948       assert( pEnd!=0 );
949       n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
950       sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
951     }
952     sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
953     sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
954     if( !p->iDb ){
955       sqliteChangeCookie(db, v);
956     }
957     sqliteVdbeAddOp(v, OP_Close, 0, 0);
958     if( pSelect ){
959       sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
960       sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
961       pParse->nTab = 2;
962       sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
963     }
964     sqliteEndWriteOperation(pParse);
965   }
966 
967   /* Add the table to the in-memory representation of the database.
968   */
969   if( pParse->explain==0 && pParse->nErr==0 ){
970     Table *pOld;
971     FKey *pFKey;
972     pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
973                             p->zName, strlen(p->zName)+1, p);
974     if( pOld ){
975       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
976       return;
977     }
978     for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
979       int nTo = strlen(pFKey->zTo) + 1;
980       pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
981       sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
982     }
983     pParse->pNewTable = 0;
984     db->nTable++;
985     db->flags |= SQLITE_InternChanges;
986   }
987 }
988 
989 /*
990 ** The parser calls this routine in order to create a new VIEW
991 */
sqliteCreateView(Parse * pParse,Token * pBegin,Token * pName,Select * pSelect,int isTemp)992 void sqliteCreateView(
993   Parse *pParse,     /* The parsing context */
994   Token *pBegin,     /* The CREATE token that begins the statement */
995   Token *pName,      /* The token that holds the name of the view */
996   Select *pSelect,   /* A SELECT statement that will become the new view */
997   int isTemp         /* TRUE for a TEMPORARY view */
998 ){
999   Table *p;
1000   int n;
1001   const char *z;
1002   Token sEnd;
1003   DbFixer sFix;
1004 
1005   sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
1006   p = pParse->pNewTable;
1007   if( p==0 || pParse->nErr ){
1008     sqliteSelectDelete(pSelect);
1009     return;
1010   }
1011   if( sqliteFixInit(&sFix, pParse, p->iDb, "view", pName)
1012     && sqliteFixSelect(&sFix, pSelect)
1013   ){
1014     sqliteSelectDelete(pSelect);
1015     return;
1016   }
1017 
1018   /* Make a copy of the entire SELECT statement that defines the view.
1019   ** This will force all the Expr.token.z values to be dynamically
1020   ** allocated rather than point to the input string - which means that
1021   ** they will persist after the current sqlite_exec() call returns.
1022   */
1023   p->pSelect = sqliteSelectDup(pSelect);
1024   sqliteSelectDelete(pSelect);
1025   if( !pParse->db->init.busy ){
1026     sqliteViewGetColumnNames(pParse, p);
1027   }
1028 
1029   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
1030   ** the end.
1031   */
1032   sEnd = pParse->sLastToken;
1033   if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1034     sEnd.z += sEnd.n;
1035   }
1036   sEnd.n = 0;
1037   n = sEnd.z - pBegin->z;
1038   z = pBegin->z;
1039   while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1040   sEnd.z = &z[n-1];
1041   sEnd.n = 1;
1042 
1043   /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
1044   sqliteEndTable(pParse, &sEnd, 0);
1045   return;
1046 }
1047 
1048 /*
1049 ** The Table structure pTable is really a VIEW.  Fill in the names of
1050 ** the columns of the view in the pTable structure.  Return the number
1051 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
1052 */
sqliteViewGetColumnNames(Parse * pParse,Table * pTable)1053 int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
1054   ExprList *pEList;
1055   Select *pSel;
1056   Table *pSelTab;
1057   int nErr = 0;
1058 
1059   assert( pTable );
1060 
1061   /* A positive nCol means the columns names for this view are
1062   ** already known.
1063   */
1064   if( pTable->nCol>0 ) return 0;
1065 
1066   /* A negative nCol is a special marker meaning that we are currently
1067   ** trying to compute the column names.  If we enter this routine with
1068   ** a negative nCol, it means two or more views form a loop, like this:
1069   **
1070   **     CREATE VIEW one AS SELECT * FROM two;
1071   **     CREATE VIEW two AS SELECT * FROM one;
1072   **
1073   ** Actually, this error is caught previously and so the following test
1074   ** should always fail.  But we will leave it in place just to be safe.
1075   */
1076   if( pTable->nCol<0 ){
1077     sqliteErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
1078     return 1;
1079   }
1080 
1081   /* If we get this far, it means we need to compute the table names.
1082   */
1083   assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1084   pSel = pTable->pSelect;
1085 
1086   /* Note that the call to sqliteResultSetOfSelect() will expand any
1087   ** "*" elements in this list.  But we will need to restore the list
1088   ** back to its original configuration afterwards, so we save a copy of
1089   ** the original in pEList.
1090   */
1091   pEList = pSel->pEList;
1092   pSel->pEList = sqliteExprListDup(pEList);
1093   if( pSel->pEList==0 ){
1094     pSel->pEList = pEList;
1095     return 1;  /* Malloc failed */
1096   }
1097   pTable->nCol = -1;
1098   pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1099   if( pSelTab ){
1100     assert( pTable->aCol==0 );
1101     pTable->nCol = pSelTab->nCol;
1102     pTable->aCol = pSelTab->aCol;
1103     pSelTab->nCol = 0;
1104     pSelTab->aCol = 0;
1105     sqliteDeleteTable(0, pSelTab);
1106     DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
1107   }else{
1108     pTable->nCol = 0;
1109     nErr++;
1110   }
1111   sqliteSelectUnbind(pSel);
1112   sqliteExprListDelete(pSel->pEList);
1113   pSel->pEList = pEList;
1114   return nErr;
1115 }
1116 
1117 /*
1118 ** Clear the column names from the VIEW pTable.
1119 **
1120 ** This routine is called whenever any other table or view is modified.
1121 ** The view passed into this routine might depend directly or indirectly
1122 ** on the modified or deleted table so we need to clear the old column
1123 ** names so that they will be recomputed.
1124 */
sqliteViewResetColumnNames(Table * pTable)1125 static void sqliteViewResetColumnNames(Table *pTable){
1126   int i;
1127   Column *pCol;
1128   assert( pTable!=0 && pTable->pSelect!=0 );
1129   for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1130     sqliteFree(pCol->zName);
1131     sqliteFree(pCol->zDflt);
1132     sqliteFree(pCol->zType);
1133   }
1134   sqliteFree(pTable->aCol);
1135   pTable->aCol = 0;
1136   pTable->nCol = 0;
1137 }
1138 
1139 /*
1140 ** Clear the column names from every VIEW in database idx.
1141 */
sqliteViewResetAll(sqlite * db,int idx)1142 static void sqliteViewResetAll(sqlite *db, int idx){
1143   HashElem *i;
1144   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
1145   for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
1146     Table *pTab = sqliteHashData(i);
1147     if( pTab->pSelect ){
1148       sqliteViewResetColumnNames(pTab);
1149     }
1150   }
1151   DbClearProperty(db, idx, DB_UnresetViews);
1152 }
1153 
1154 /*
1155 ** Given a token, look up a table with that name.  If not found, leave
1156 ** an error for the parser to find and return NULL.
1157 */
sqliteTableFromToken(Parse * pParse,Token * pTok)1158 Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
1159   char *zName;
1160   Table *pTab;
1161   zName = sqliteTableNameFromToken(pTok);
1162   if( zName==0 ) return 0;
1163   pTab = sqliteFindTable(pParse->db, zName, 0);
1164   sqliteFree(zName);
1165   if( pTab==0 ){
1166     sqliteErrorMsg(pParse, "no such table: %T", pTok);
1167   }
1168   return pTab;
1169 }
1170 
1171 /*
1172 ** This routine is called to do the work of a DROP TABLE statement.
1173 ** pName is the name of the table to be dropped.
1174 */
sqliteDropTable(Parse * pParse,Token * pName,int isView)1175 void sqliteDropTable(Parse *pParse, Token *pName, int isView){
1176   Table *pTable;
1177   Vdbe *v;
1178   int base;
1179   sqlite *db = pParse->db;
1180   int iDb;
1181 
1182   if( pParse->nErr || sqlite_malloc_failed ) return;
1183   pTable = sqliteTableFromToken(pParse, pName);
1184   if( pTable==0 ) return;
1185   iDb = pTable->iDb;
1186   assert( iDb>=0 && iDb<db->nDb );
1187 #ifndef SQLITE_OMIT_AUTHORIZATION
1188   {
1189     int code;
1190     const char *zTab = SCHEMA_TABLE(pTable->iDb);
1191     const char *zDb = db->aDb[pTable->iDb].zName;
1192     if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
1193       return;
1194     }
1195     if( isView ){
1196       if( iDb==1 ){
1197         code = SQLITE_DROP_TEMP_VIEW;
1198       }else{
1199         code = SQLITE_DROP_VIEW;
1200       }
1201     }else{
1202       if( iDb==1 ){
1203         code = SQLITE_DROP_TEMP_TABLE;
1204       }else{
1205         code = SQLITE_DROP_TABLE;
1206       }
1207     }
1208     if( sqliteAuthCheck(pParse, code, pTable->zName, 0, zDb) ){
1209       return;
1210     }
1211     if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0, zDb) ){
1212       return;
1213     }
1214   }
1215 #endif
1216   if( pTable->readOnly ){
1217     sqliteErrorMsg(pParse, "table %s may not be dropped", pTable->zName);
1218     pParse->nErr++;
1219     return;
1220   }
1221   if( isView && pTable->pSelect==0 ){
1222     sqliteErrorMsg(pParse, "use DROP TABLE to delete table %s", pTable->zName);
1223     return;
1224   }
1225   if( !isView && pTable->pSelect ){
1226     sqliteErrorMsg(pParse, "use DROP VIEW to delete view %s", pTable->zName);
1227     return;
1228   }
1229 
1230   /* Generate code to remove the table from the master table
1231   ** on disk.
1232   */
1233   v = sqliteGetVdbe(pParse);
1234   if( v ){
1235     static VdbeOpList dropTable[] = {
1236       { OP_Rewind,     0, ADDR(8),  0},
1237       { OP_String,     0, 0,        0}, /* 1 */
1238       { OP_MemStore,   1, 1,        0},
1239       { OP_MemLoad,    1, 0,        0}, /* 3 */
1240       { OP_Column,     0, 2,        0},
1241       { OP_Ne,         0, ADDR(7),  0},
1242       { OP_Delete,     0, 0,        0},
1243       { OP_Next,       0, ADDR(3),  0}, /* 7 */
1244     };
1245     Index *pIdx;
1246     Trigger *pTrigger;
1247     sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
1248 
1249     /* Drop all triggers associated with the table being dropped */
1250     pTrigger = pTable->pTrigger;
1251     while( pTrigger ){
1252       assert( pTrigger->iDb==pTable->iDb || pTrigger->iDb==1 );
1253       sqliteDropTriggerPtr(pParse, pTrigger, 1);
1254       if( pParse->explain ){
1255         pTrigger = pTrigger->pNext;
1256       }else{
1257         pTrigger = pTable->pTrigger;
1258       }
1259     }
1260 
1261     /* Drop all SQLITE_MASTER entries that refer to the table */
1262     sqliteOpenMasterTable(v, pTable->iDb);
1263     base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1264     sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
1265 
1266     /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */
1267     if( pTable->iDb!=1 ){
1268       sqliteOpenMasterTable(v, 1);
1269       base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1270       sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
1271     }
1272 
1273     if( pTable->iDb==0 ){
1274       sqliteChangeCookie(db, v);
1275     }
1276     sqliteVdbeAddOp(v, OP_Close, 0, 0);
1277     if( !isView ){
1278       sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
1279       for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1280         sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
1281       }
1282     }
1283     sqliteEndWriteOperation(pParse);
1284   }
1285 
1286   /* Delete the in-memory description of the table.
1287   **
1288   ** Exception: if the SQL statement began with the EXPLAIN keyword,
1289   ** then no changes should be made.
1290   */
1291   if( !pParse->explain ){
1292     sqliteUnlinkAndDeleteTable(db, pTable);
1293     db->flags |= SQLITE_InternChanges;
1294   }
1295   sqliteViewResetAll(db, iDb);
1296 }
1297 
1298 /*
1299 ** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1300 ** opcode and adds that P3 string to the most recently inserted instruction
1301 ** in the virtual machine.  The P3 string consists of a single character
1302 ** for each column in the index pIdx of table pTab.  If the column uses
1303 ** a numeric sort order, then the P3 string character corresponding to
1304 ** that column is 'n'.  If the column uses a text sort order, then the
1305 ** P3 string is 't'.  See the OP_MakeIdxKey opcode documentation for
1306 ** additional information.  See also the sqliteAddKeyType() routine.
1307 */
sqliteAddIdxKeyType(Vdbe * v,Index * pIdx)1308 void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1309   char *zType;
1310   Table *pTab;
1311   int i, n;
1312   assert( pIdx!=0 && pIdx->pTable!=0 );
1313   pTab = pIdx->pTable;
1314   n = pIdx->nColumn;
1315   zType = sqliteMallocRaw( n+1 );
1316   if( zType==0 ) return;
1317   for(i=0; i<n; i++){
1318     int iCol = pIdx->aiColumn[i];
1319     assert( iCol>=0 && iCol<pTab->nCol );
1320     if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1321       zType[i] = 't';
1322     }else{
1323       zType[i] = 'n';
1324     }
1325   }
1326   zType[n] = 0;
1327   sqliteVdbeChangeP3(v, -1, zType, n);
1328   sqliteFree(zType);
1329 }
1330 
1331 /*
1332 ** This routine is called to create a new foreign key on the table
1333 ** currently under construction.  pFromCol determines which columns
1334 ** in the current table point to the foreign key.  If pFromCol==0 then
1335 ** connect the key to the last column inserted.  pTo is the name of
1336 ** the table referred to.  pToCol is a list of tables in the other
1337 ** pTo table that the foreign key points to.  flags contains all
1338 ** information about the conflict resolution algorithms specified
1339 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1340 **
1341 ** An FKey structure is created and added to the table currently
1342 ** under construction in the pParse->pNewTable field.  The new FKey
1343 ** is not linked into db->aFKey at this point - that does not happen
1344 ** until sqliteEndTable().
1345 **
1346 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
1347 ** to sqliteDeferForeignKey() might change this to DEFERRED.
1348 */
sqliteCreateForeignKey(Parse * pParse,IdList * pFromCol,Token * pTo,IdList * pToCol,int flags)1349 void sqliteCreateForeignKey(
1350   Parse *pParse,       /* Parsing context */
1351   IdList *pFromCol,    /* Columns in this table that point to other table */
1352   Token *pTo,          /* Name of the other table */
1353   IdList *pToCol,      /* Columns in the other table */
1354   int flags            /* Conflict resolution algorithms. */
1355 ){
1356   Table *p = pParse->pNewTable;
1357   int nByte;
1358   int i;
1359   int nCol;
1360   char *z;
1361   FKey *pFKey = 0;
1362 
1363   assert( pTo!=0 );
1364   if( p==0 || pParse->nErr ) goto fk_end;
1365   if( pFromCol==0 ){
1366     int iCol = p->nCol-1;
1367     if( iCol<0 ) goto fk_end;
1368     if( pToCol && pToCol->nId!=1 ){
1369       sqliteErrorMsg(pParse, "foreign key on %s"
1370          " should reference only one column of table %T",
1371          p->aCol[iCol].zName, pTo);
1372       goto fk_end;
1373     }
1374     nCol = 1;
1375   }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1376     sqliteErrorMsg(pParse,
1377         "number of columns in foreign key does not match the number of "
1378         "columns in the referenced table");
1379     goto fk_end;
1380   }else{
1381     nCol = pFromCol->nId;
1382   }
1383   nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1384   if( pToCol ){
1385     for(i=0; i<pToCol->nId; i++){
1386       nByte += strlen(pToCol->a[i].zName) + 1;
1387     }
1388   }
1389   pFKey = sqliteMalloc( nByte );
1390   if( pFKey==0 ) goto fk_end;
1391   pFKey->pFrom = p;
1392   pFKey->pNextFrom = p->pFKey;
1393   z = (char*)&pFKey[1];
1394   pFKey->aCol = (struct sColMap*)z;
1395   z += sizeof(struct sColMap)*nCol;
1396   pFKey->zTo = z;
1397   memcpy(z, pTo->z, pTo->n);
1398   z[pTo->n] = 0;
1399   z += pTo->n+1;
1400   pFKey->pNextTo = 0;
1401   pFKey->nCol = nCol;
1402   if( pFromCol==0 ){
1403     pFKey->aCol[0].iFrom = p->nCol-1;
1404   }else{
1405     for(i=0; i<nCol; i++){
1406       int j;
1407       for(j=0; j<p->nCol; j++){
1408         if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1409           pFKey->aCol[i].iFrom = j;
1410           break;
1411         }
1412       }
1413       if( j>=p->nCol ){
1414         sqliteErrorMsg(pParse,
1415           "unknown column \"%s\" in foreign key definition",
1416           pFromCol->a[i].zName);
1417         goto fk_end;
1418       }
1419     }
1420   }
1421   if( pToCol ){
1422     for(i=0; i<nCol; i++){
1423       int n = strlen(pToCol->a[i].zName);
1424       pFKey->aCol[i].zCol = z;
1425       memcpy(z, pToCol->a[i].zName, n);
1426       z[n] = 0;
1427       z += n+1;
1428     }
1429   }
1430   pFKey->isDeferred = 0;
1431   pFKey->deleteConf = flags & 0xff;
1432   pFKey->updateConf = (flags >> 8 ) & 0xff;
1433   pFKey->insertConf = (flags >> 16 ) & 0xff;
1434 
1435   /* Link the foreign key to the table as the last step.
1436   */
1437   p->pFKey = pFKey;
1438   pFKey = 0;
1439 
1440 fk_end:
1441   sqliteFree(pFKey);
1442   sqliteIdListDelete(pFromCol);
1443   sqliteIdListDelete(pToCol);
1444 }
1445 
1446 /*
1447 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1448 ** clause is seen as part of a foreign key definition.  The isDeferred
1449 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1450 ** The behavior of the most recently created foreign key is adjusted
1451 ** accordingly.
1452 */
sqliteDeferForeignKey(Parse * pParse,int isDeferred)1453 void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1454   Table *pTab;
1455   FKey *pFKey;
1456   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1457   pFKey->isDeferred = isDeferred;
1458 }
1459 
1460 /*
1461 ** Create a new index for an SQL table.  pIndex is the name of the index
1462 ** and pTable is the name of the table that is to be indexed.  Both will
1463 ** be NULL for a primary key or an index that is created to satisfy a
1464 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
1465 ** as the table to be indexed.  pParse->pNewTable is a table that is
1466 ** currently being constructed by a CREATE TABLE statement.
1467 **
1468 ** pList is a list of columns to be indexed.  pList will be NULL if this
1469 ** is a primary key or unique-constraint on the most recent column added
1470 ** to the table currently under construction.
1471 */
sqliteCreateIndex(Parse * pParse,Token * pName,SrcList * pTable,IdList * pList,int onError,Token * pStart,Token * pEnd)1472 void sqliteCreateIndex(
1473   Parse *pParse,   /* All information about this parse */
1474   Token *pName,    /* Name of the index.  May be NULL */
1475   SrcList *pTable, /* Name of the table to index.  Use pParse->pNewTable if 0 */
1476   IdList *pList,   /* A list of columns to be indexed */
1477   int onError,     /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1478   Token *pStart,   /* The CREATE token that begins a CREATE TABLE statement */
1479   Token *pEnd      /* The ")" that closes the CREATE INDEX statement */
1480 ){
1481   Table *pTab;     /* Table to be indexed */
1482   Index *pIndex;   /* The index to be created */
1483   char *zName = 0;
1484   int i, j;
1485   Token nullId;    /* Fake token for an empty ID list */
1486   DbFixer sFix;    /* For assigning database names to pTable */
1487   int isTemp;      /* True for a temporary index */
1488   sqlite *db = pParse->db;
1489 
1490   if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1491   if( db->init.busy
1492      && sqliteFixInit(&sFix, pParse, db->init.iDb, "index", pName)
1493      && sqliteFixSrcList(&sFix, pTable)
1494   ){
1495     goto exit_create_index;
1496   }
1497 
1498   /*
1499   ** Find the table that is to be indexed.  Return early if not found.
1500   */
1501   if( pTable!=0 ){
1502     assert( pName!=0 );
1503     assert( pTable->nSrc==1 );
1504     pTab =  sqliteSrcListLookup(pParse, pTable);
1505   }else{
1506     assert( pName==0 );
1507     pTab =  pParse->pNewTable;
1508   }
1509   if( pTab==0 || pParse->nErr ) goto exit_create_index;
1510   if( pTab->readOnly ){
1511     sqliteErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
1512     goto exit_create_index;
1513   }
1514   if( pTab->iDb>=2 && db->init.busy==0 ){
1515     sqliteErrorMsg(pParse, "table %s may not have indices added", pTab->zName);
1516     goto exit_create_index;
1517   }
1518   if( pTab->pSelect ){
1519     sqliteErrorMsg(pParse, "views may not be indexed");
1520     goto exit_create_index;
1521   }
1522   isTemp = pTab->iDb==1;
1523 
1524   /*
1525   ** Find the name of the index.  Make sure there is not already another
1526   ** index or table with the same name.
1527   **
1528   ** Exception:  If we are reading the names of permanent indices from the
1529   ** sqlite_master table (because some other process changed the schema) and
1530   ** one of the index names collides with the name of a temporary table or
1531   ** index, then we will continue to process this index.
1532   **
1533   ** If pName==0 it means that we are
1534   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
1535   ** own name.
1536   */
1537   if( pName && !db->init.busy ){
1538     Index *pISameName;    /* Another index with the same name */
1539     Table *pTSameName;    /* A table with same name as the index */
1540     zName = sqliteTableNameFromToken(pName);
1541     if( zName==0 ) goto exit_create_index;
1542     if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
1543       sqliteErrorMsg(pParse, "index %s already exists", zName);
1544       goto exit_create_index;
1545     }
1546     if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
1547       sqliteErrorMsg(pParse, "there is already a table named %s", zName);
1548       goto exit_create_index;
1549     }
1550   }else if( pName==0 ){
1551     char zBuf[30];
1552     int n;
1553     Index *pLoop;
1554     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1555     sprintf(zBuf,"%d)",n);
1556     zName = 0;
1557     sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);
1558     if( zName==0 ) goto exit_create_index;
1559   }else{
1560     zName = sqliteStrNDup(pName->z, pName->n);
1561   }
1562 
1563   /* Check for authorization to create an index.
1564   */
1565 #ifndef SQLITE_OMIT_AUTHORIZATION
1566   {
1567     const char *zDb = db->aDb[pTab->iDb].zName;
1568 
1569     assert( pTab->iDb==db->init.iDb || isTemp );
1570     if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
1571       goto exit_create_index;
1572     }
1573     i = SQLITE_CREATE_INDEX;
1574     if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
1575     if( sqliteAuthCheck(pParse, i, zName, pTab->zName, zDb) ){
1576       goto exit_create_index;
1577     }
1578   }
1579 #endif
1580 
1581   /* If pList==0, it means this routine was called to make a primary
1582   ** key out of the last column added to the table under construction.
1583   ** So create a fake list to simulate this.
1584   */
1585   if( pList==0 ){
1586     nullId.z = pTab->aCol[pTab->nCol-1].zName;
1587     nullId.n = strlen(nullId.z);
1588     pList = sqliteIdListAppend(0, &nullId);
1589     if( pList==0 ) goto exit_create_index;
1590   }
1591 
1592   /*
1593   ** Allocate the index structure.
1594   */
1595   pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
1596                         sizeof(int)*pList->nId );
1597   if( pIndex==0 ) goto exit_create_index;
1598   pIndex->aiColumn = (int*)&pIndex[1];
1599   pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
1600   strcpy(pIndex->zName, zName);
1601   pIndex->pTable = pTab;
1602   pIndex->nColumn = pList->nId;
1603   pIndex->onError = onError;
1604   pIndex->autoIndex = pName==0;
1605   pIndex->iDb = isTemp ? 1 : db->init.iDb;
1606 
1607   /* Scan the names of the columns of the table to be indexed and
1608   ** load the column indices into the Index structure.  Report an error
1609   ** if any column is not found.
1610   */
1611   for(i=0; i<pList->nId; i++){
1612     for(j=0; j<pTab->nCol; j++){
1613       if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
1614     }
1615     if( j>=pTab->nCol ){
1616       sqliteErrorMsg(pParse, "table %s has no column named %s",
1617         pTab->zName, pList->a[i].zName);
1618       sqliteFree(pIndex);
1619       goto exit_create_index;
1620     }
1621     pIndex->aiColumn[i] = j;
1622   }
1623 
1624   /* Link the new Index structure to its table and to the other
1625   ** in-memory database structures.
1626   */
1627   if( !pParse->explain ){
1628     Index *p;
1629     p = sqliteHashInsert(&db->aDb[pIndex->iDb].idxHash,
1630                          pIndex->zName, strlen(pIndex->zName)+1, pIndex);
1631     if( p ){
1632       assert( p==pIndex );  /* Malloc must have failed */
1633       sqliteFree(pIndex);
1634       goto exit_create_index;
1635     }
1636     db->flags |= SQLITE_InternChanges;
1637   }
1638 
1639   /* When adding an index to the list of indices for a table, make
1640   ** sure all indices labeled OE_Replace come after all those labeled
1641   ** OE_Ignore.  This is necessary for the correct operation of UPDATE
1642   ** and INSERT.
1643   */
1644   if( onError!=OE_Replace || pTab->pIndex==0
1645        || pTab->pIndex->onError==OE_Replace){
1646     pIndex->pNext = pTab->pIndex;
1647     pTab->pIndex = pIndex;
1648   }else{
1649     Index *pOther = pTab->pIndex;
1650     while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1651       pOther = pOther->pNext;
1652     }
1653     pIndex->pNext = pOther->pNext;
1654     pOther->pNext = pIndex;
1655   }
1656 
1657   /* If the db->init.busy is 1 it means we are reading the SQL off the
1658   ** "sqlite_master" table on the disk.  So do not write to the disk
1659   ** again.  Extract the table number from the db->init.newTnum field.
1660   */
1661   if( db->init.busy && pTable!=0 ){
1662     pIndex->tnum = db->init.newTnum;
1663   }
1664 
1665   /* If the db->init.busy is 0 then create the index on disk.  This
1666   ** involves writing the index into the master table and filling in the
1667   ** index with the current table contents.
1668   **
1669   ** The db->init.busy is 0 when the user first enters a CREATE INDEX
1670   ** command.  db->init.busy is 1 when a database is opened and
1671   ** CREATE INDEX statements are read out of the master table.  In
1672   ** the latter case the index already exists on disk, which is why
1673   ** we don't want to recreate it.
1674   **
1675   ** If pTable==0 it means this index is generated as a primary key
1676   ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
1677   ** has just been created, it contains no data and the index initialization
1678   ** step can be skipped.
1679   */
1680   else if( db->init.busy==0 ){
1681     int n;
1682     Vdbe *v;
1683     int lbl1, lbl2;
1684     int i;
1685     int addr;
1686 
1687     v = sqliteGetVdbe(pParse);
1688     if( v==0 ) goto exit_create_index;
1689     if( pTable!=0 ){
1690       sqliteBeginWriteOperation(pParse, 0, isTemp);
1691       sqliteOpenMasterTable(v, isTemp);
1692     }
1693     sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1694     sqliteVdbeOp3(v, OP_String, 0, 0, "index", P3_STATIC);
1695     sqliteVdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0);
1696     sqliteVdbeOp3(v, OP_String, 0, 0, pTab->zName, 0);
1697     sqliteVdbeOp3(v, OP_CreateIndex, 0, isTemp,(char*)&pIndex->tnum,P3_POINTER);
1698     pIndex->tnum = 0;
1699     if( pTable ){
1700       sqliteVdbeCode(v,
1701           OP_Dup,       0,      0,
1702           OP_Integer,   isTemp, 0,
1703           OP_OpenWrite, 1,      0,
1704       0);
1705     }
1706     addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1707     if( pStart && pEnd ){
1708       n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1709       sqliteVdbeChangeP3(v, addr, pStart->z, n);
1710     }
1711     sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1712     sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
1713     if( pTable ){
1714       sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
1715       sqliteVdbeOp3(v, OP_OpenRead, 2, pTab->tnum, pTab->zName, 0);
1716       lbl2 = sqliteVdbeMakeLabel(v);
1717       sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1718       lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
1719       for(i=0; i<pIndex->nColumn; i++){
1720         int iCol = pIndex->aiColumn[i];
1721         if( pTab->iPKey==iCol ){
1722           sqliteVdbeAddOp(v, OP_Dup, i, 0);
1723         }else{
1724           sqliteVdbeAddOp(v, OP_Column, 2, iCol);
1725         }
1726       }
1727       sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
1728       if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
1729       sqliteVdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
1730                       "indexed columns are not unique", P3_STATIC);
1731       sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
1732       sqliteVdbeResolveLabel(v, lbl2);
1733       sqliteVdbeAddOp(v, OP_Close, 2, 0);
1734       sqliteVdbeAddOp(v, OP_Close, 1, 0);
1735     }
1736     if( pTable!=0 ){
1737       if( !isTemp ){
1738         sqliteChangeCookie(db, v);
1739       }
1740       sqliteVdbeAddOp(v, OP_Close, 0, 0);
1741       sqliteEndWriteOperation(pParse);
1742     }
1743   }
1744 
1745   /* Clean up before exiting */
1746 exit_create_index:
1747   sqliteIdListDelete(pList);
1748   sqliteSrcListDelete(pTable);
1749   sqliteFree(zName);
1750   return;
1751 }
1752 
1753 /*
1754 ** This routine will drop an existing named index.  This routine
1755 ** implements the DROP INDEX statement.
1756 */
sqliteDropIndex(Parse * pParse,SrcList * pName)1757 void sqliteDropIndex(Parse *pParse, SrcList *pName){
1758   Index *pIndex;
1759   Vdbe *v;
1760   sqlite *db = pParse->db;
1761 
1762   if( pParse->nErr || sqlite_malloc_failed ) return;
1763   assert( pName->nSrc==1 );
1764   pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
1765   if( pIndex==0 ){
1766     sqliteErrorMsg(pParse, "no such index: %S", pName, 0);
1767     goto exit_drop_index;
1768   }
1769   if( pIndex->autoIndex ){
1770     sqliteErrorMsg(pParse, "index associated with UNIQUE "
1771       "or PRIMARY KEY constraint cannot be dropped", 0);
1772     goto exit_drop_index;
1773   }
1774   if( pIndex->iDb>1 ){
1775     sqliteErrorMsg(pParse, "cannot alter schema of attached "
1776        "databases", 0);
1777     goto exit_drop_index;
1778   }
1779 #ifndef SQLITE_OMIT_AUTHORIZATION
1780   {
1781     int code = SQLITE_DROP_INDEX;
1782     Table *pTab = pIndex->pTable;
1783     const char *zDb = db->aDb[pIndex->iDb].zName;
1784     const char *zTab = SCHEMA_TABLE(pIndex->iDb);
1785     if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
1786       goto exit_drop_index;
1787     }
1788     if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
1789     if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
1790       goto exit_drop_index;
1791     }
1792   }
1793 #endif
1794 
1795   /* Generate code to remove the index and from the master table */
1796   v = sqliteGetVdbe(pParse);
1797   if( v ){
1798     static VdbeOpList dropIndex[] = {
1799       { OP_Rewind,     0, ADDR(9), 0},
1800       { OP_String,     0, 0,       0}, /* 1 */
1801       { OP_MemStore,   1, 1,       0},
1802       { OP_MemLoad,    1, 0,       0}, /* 3 */
1803       { OP_Column,     0, 1,       0},
1804       { OP_Eq,         0, ADDR(8), 0},
1805       { OP_Next,       0, ADDR(3), 0},
1806       { OP_Goto,       0, ADDR(9), 0},
1807       { OP_Delete,     0, 0,       0}, /* 8 */
1808     };
1809     int base;
1810 
1811     sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
1812     sqliteOpenMasterTable(v, pIndex->iDb);
1813     base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1814     sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
1815     if( pIndex->iDb==0 ){
1816       sqliteChangeCookie(db, v);
1817     }
1818     sqliteVdbeAddOp(v, OP_Close, 0, 0);
1819     sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
1820     sqliteEndWriteOperation(pParse);
1821   }
1822 
1823   /* Delete the in-memory description of this index.
1824   */
1825   if( !pParse->explain ){
1826     sqliteUnlinkAndDeleteIndex(db, pIndex);
1827     db->flags |= SQLITE_InternChanges;
1828   }
1829 
1830 exit_drop_index:
1831   sqliteSrcListDelete(pName);
1832 }
1833 
1834 /*
1835 ** Append a new element to the given IdList.  Create a new IdList if
1836 ** need be.
1837 **
1838 ** A new IdList is returned, or NULL if malloc() fails.
1839 */
sqliteIdListAppend(IdList * pList,Token * pToken)1840 IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1841   if( pList==0 ){
1842     pList = sqliteMalloc( sizeof(IdList) );
1843     if( pList==0 ) return 0;
1844     pList->nAlloc = 0;
1845   }
1846   if( pList->nId>=pList->nAlloc ){
1847     struct IdList_item *a;
1848     pList->nAlloc = pList->nAlloc*2 + 5;
1849     a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
1850     if( a==0 ){
1851       sqliteIdListDelete(pList);
1852       return 0;
1853     }
1854     pList->a = a;
1855   }
1856   memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1857   if( pToken ){
1858     char **pz = &pList->a[pList->nId].zName;
1859     sqliteSetNString(pz, pToken->z, pToken->n, 0);
1860     if( *pz==0 ){
1861       sqliteIdListDelete(pList);
1862       return 0;
1863     }else{
1864       sqliteDequote(*pz);
1865     }
1866   }
1867   pList->nId++;
1868   return pList;
1869 }
1870 
1871 /*
1872 ** Append a new table name to the given SrcList.  Create a new SrcList if
1873 ** need be.  A new entry is created in the SrcList even if pToken is NULL.
1874 **
1875 ** A new SrcList is returned, or NULL if malloc() fails.
1876 **
1877 ** If pDatabase is not null, it means that the table has an optional
1878 ** database name prefix.  Like this:  "database.table".  The pDatabase
1879 ** points to the table name and the pTable points to the database name.
1880 ** The SrcList.a[].zName field is filled with the table name which might
1881 ** come from pTable (if pDatabase is NULL) or from pDatabase.
1882 ** SrcList.a[].zDatabase is filled with the database name from pTable,
1883 ** or with NULL if no database is specified.
1884 **
1885 ** In other words, if call like this:
1886 **
1887 **         sqliteSrcListAppend(A,B,0);
1888 **
1889 ** Then B is a table name and the database name is unspecified.  If called
1890 ** like this:
1891 **
1892 **         sqliteSrcListAppend(A,B,C);
1893 **
1894 ** Then C is the table name and B is the database name.
1895 */
sqliteSrcListAppend(SrcList * pList,Token * pTable,Token * pDatabase)1896 SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
1897   if( pList==0 ){
1898     pList = sqliteMalloc( sizeof(SrcList) );
1899     if( pList==0 ) return 0;
1900     pList->nAlloc = 1;
1901   }
1902   if( pList->nSrc>=pList->nAlloc ){
1903     SrcList *pNew;
1904     pList->nAlloc *= 2;
1905     pNew = sqliteRealloc(pList,
1906                sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
1907     if( pNew==0 ){
1908       sqliteSrcListDelete(pList);
1909       return 0;
1910     }
1911     pList = pNew;
1912   }
1913   memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
1914   if( pDatabase && pDatabase->z==0 ){
1915     pDatabase = 0;
1916   }
1917   if( pDatabase && pTable ){
1918     Token *pTemp = pDatabase;
1919     pDatabase = pTable;
1920     pTable = pTemp;
1921   }
1922   if( pTable ){
1923     char **pz = &pList->a[pList->nSrc].zName;
1924     sqliteSetNString(pz, pTable->z, pTable->n, 0);
1925     if( *pz==0 ){
1926       sqliteSrcListDelete(pList);
1927       return 0;
1928     }else{
1929       sqliteDequote(*pz);
1930     }
1931   }
1932   if( pDatabase ){
1933     char **pz = &pList->a[pList->nSrc].zDatabase;
1934     sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
1935     if( *pz==0 ){
1936       sqliteSrcListDelete(pList);
1937       return 0;
1938     }else{
1939       sqliteDequote(*pz);
1940     }
1941   }
1942   pList->a[pList->nSrc].iCursor = -1;
1943   pList->nSrc++;
1944   return pList;
1945 }
1946 
1947 /*
1948 ** Assign cursors to all tables in a SrcList
1949 */
sqliteSrcListAssignCursors(Parse * pParse,SrcList * pList)1950 void sqliteSrcListAssignCursors(Parse *pParse, SrcList *pList){
1951   int i;
1952   for(i=0; i<pList->nSrc; i++){
1953     if( pList->a[i].iCursor<0 ){
1954       pList->a[i].iCursor = pParse->nTab++;
1955     }
1956   }
1957 }
1958 
1959 /*
1960 ** Add an alias to the last identifier on the given identifier list.
1961 */
sqliteSrcListAddAlias(SrcList * pList,Token * pToken)1962 void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1963   if( pList && pList->nSrc>0 ){
1964     int i = pList->nSrc - 1;
1965     sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
1966     sqliteDequote(pList->a[i].zAlias);
1967   }
1968 }
1969 
1970 /*
1971 ** Delete an IdList.
1972 */
sqliteIdListDelete(IdList * pList)1973 void sqliteIdListDelete(IdList *pList){
1974   int i;
1975   if( pList==0 ) return;
1976   for(i=0; i<pList->nId; i++){
1977     sqliteFree(pList->a[i].zName);
1978   }
1979   sqliteFree(pList->a);
1980   sqliteFree(pList);
1981 }
1982 
1983 /*
1984 ** Return the index in pList of the identifier named zId.  Return -1
1985 ** if not found.
1986 */
sqliteIdListIndex(IdList * pList,const char * zName)1987 int sqliteIdListIndex(IdList *pList, const char *zName){
1988   int i;
1989   if( pList==0 ) return -1;
1990   for(i=0; i<pList->nId; i++){
1991     if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1992   }
1993   return -1;
1994 }
1995 
1996 /*
1997 ** Delete an entire SrcList including all its substructure.
1998 */
sqliteSrcListDelete(SrcList * pList)1999 void sqliteSrcListDelete(SrcList *pList){
2000   int i;
2001   if( pList==0 ) return;
2002   for(i=0; i<pList->nSrc; i++){
2003     sqliteFree(pList->a[i].zDatabase);
2004     sqliteFree(pList->a[i].zName);
2005     sqliteFree(pList->a[i].zAlias);
2006     if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
2007       sqliteDeleteTable(0, pList->a[i].pTab);
2008     }
2009     sqliteSelectDelete(pList->a[i].pSelect);
2010     sqliteExprDelete(pList->a[i].pOn);
2011     sqliteIdListDelete(pList->a[i].pUsing);
2012   }
2013   sqliteFree(pList);
2014 }
2015 
2016 /*
2017 ** Begin a transaction
2018 */
sqliteBeginTransaction(Parse * pParse,int onError)2019 void sqliteBeginTransaction(Parse *pParse, int onError){
2020   sqlite *db;
2021 
2022   if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
2023   if( pParse->nErr || sqlite_malloc_failed ) return;
2024   if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
2025   if( db->flags & SQLITE_InTrans ){
2026     sqliteErrorMsg(pParse, "cannot start a transaction within a transaction");
2027     return;
2028   }
2029   sqliteBeginWriteOperation(pParse, 0, 0);
2030   if( !pParse->explain ){
2031     db->flags |= SQLITE_InTrans;
2032     db->onError = onError;
2033   }
2034 }
2035 
2036 /*
2037 ** Commit a transaction
2038 */
sqliteCommitTransaction(Parse * pParse)2039 void sqliteCommitTransaction(Parse *pParse){
2040   sqlite *db;
2041 
2042   if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
2043   if( pParse->nErr || sqlite_malloc_failed ) return;
2044   if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
2045   if( (db->flags & SQLITE_InTrans)==0 ){
2046     sqliteErrorMsg(pParse, "cannot commit - no transaction is active");
2047     return;
2048   }
2049   if( !pParse->explain ){
2050     db->flags &= ~SQLITE_InTrans;
2051   }
2052   sqliteEndWriteOperation(pParse);
2053   if( !pParse->explain ){
2054     db->onError = OE_Default;
2055   }
2056 }
2057 
2058 /*
2059 ** Rollback a transaction
2060 */
sqliteRollbackTransaction(Parse * pParse)2061 void sqliteRollbackTransaction(Parse *pParse){
2062   sqlite *db;
2063   Vdbe *v;
2064 
2065   if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
2066   if( pParse->nErr || sqlite_malloc_failed ) return;
2067   if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
2068   if( (db->flags & SQLITE_InTrans)==0 ){
2069     sqliteErrorMsg(pParse, "cannot rollback - no transaction is active");
2070     return;
2071   }
2072   v = sqliteGetVdbe(pParse);
2073   if( v ){
2074     sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
2075   }
2076   if( !pParse->explain ){
2077     db->flags &= ~SQLITE_InTrans;
2078     db->onError = OE_Default;
2079   }
2080 }
2081 
2082 /*
2083 ** Generate VDBE code that will verify the schema cookie for all
2084 ** named database files.
2085 */
sqliteCodeVerifySchema(Parse * pParse,int iDb)2086 void sqliteCodeVerifySchema(Parse *pParse, int iDb){
2087   sqlite *db = pParse->db;
2088   Vdbe *v = sqliteGetVdbe(pParse);
2089   assert( iDb>=0 && iDb<db->nDb );
2090   assert( db->aDb[iDb].pBt!=0 );
2091   if( iDb!=1 && !DbHasProperty(db, iDb, DB_Cookie) ){
2092     sqliteVdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
2093     DbSetProperty(db, iDb, DB_Cookie);
2094   }
2095 }
2096 
2097 /*
2098 ** Generate VDBE code that prepares for doing an operation that
2099 ** might change the database.
2100 **
2101 ** This routine starts a new transaction if we are not already within
2102 ** a transaction.  If we are already within a transaction, then a checkpoint
2103 ** is set if the setCheckpoint parameter is true.  A checkpoint should
2104 ** be set for operations that might fail (due to a constraint) part of
2105 ** the way through and which will need to undo some writes without having to
2106 ** rollback the whole transaction.  For operations where all constraints
2107 ** can be checked before any changes are made to the database, it is never
2108 ** necessary to undo a write and the checkpoint should not be set.
2109 **
2110 ** Only database iDb and the temp database are made writable by this call.
2111 ** If iDb==0, then the main and temp databases are made writable.   If
2112 ** iDb==1 then only the temp database is made writable.  If iDb>1 then the
2113 ** specified auxiliary database and the temp database are made writable.
2114 */
sqliteBeginWriteOperation(Parse * pParse,int setCheckpoint,int iDb)2115 void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int iDb){
2116   Vdbe *v;
2117   sqlite *db = pParse->db;
2118   if( DbHasProperty(db, iDb, DB_Locked) ) return;
2119   v = sqliteGetVdbe(pParse);
2120   if( v==0 ) return;
2121   if( !db->aDb[iDb].inTrans ){
2122     sqliteVdbeAddOp(v, OP_Transaction, iDb, 0);
2123     DbSetProperty(db, iDb, DB_Locked);
2124     sqliteCodeVerifySchema(pParse, iDb);
2125     if( iDb!=1 ){
2126       sqliteBeginWriteOperation(pParse, setCheckpoint, 1);
2127     }
2128   }else if( setCheckpoint ){
2129     sqliteVdbeAddOp(v, OP_Checkpoint, iDb, 0);
2130     DbSetProperty(db, iDb, DB_Locked);
2131   }
2132 }
2133 
2134 /*
2135 ** Generate code that concludes an operation that may have changed
2136 ** the database.  If a statement transaction was started, then emit
2137 ** an OP_Commit that will cause the changes to be committed to disk.
2138 **
2139 ** Note that checkpoints are automatically committed at the end of
2140 ** a statement.  Note also that there can be multiple calls to
2141 ** sqliteBeginWriteOperation() but there should only be a single
2142 ** call to sqliteEndWriteOperation() at the conclusion of the statement.
2143 */
sqliteEndWriteOperation(Parse * pParse)2144 void sqliteEndWriteOperation(Parse *pParse){
2145   Vdbe *v;
2146   sqlite *db = pParse->db;
2147   if( pParse->trigStack ) return; /* if this is in a trigger */
2148   v = sqliteGetVdbe(pParse);
2149   if( v==0 ) return;
2150   if( db->flags & SQLITE_InTrans ){
2151     /* A BEGIN has executed.  Do not commit until we see an explicit
2152     ** COMMIT statement. */
2153   }else{
2154     sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2155   }
2156 }
2157