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