blob: 6db409278d0ff09d994c3e98a9d10544d0d532fb [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This file contains C code routines that are called by the SQLite parser
13** when syntax rules are reduced. The routines in this file handle the
14** following kinds of SQL syntax:
drh75897232000-05-29 14:26:00 +000015**
drhbed86902000-06-02 13:27:59 +000016** CREATE TABLE
17** DROP TABLE
18** CREATE INDEX
19** DROP INDEX
drh832508b2002-03-02 17:04:07 +000020** creating ID lists
drhb19a2bc2001-09-16 00:13:26 +000021** BEGIN TRANSACTION
22** COMMIT
23** ROLLBACK
24** PRAGMA
drhbed86902000-06-02 13:27:59 +000025**
drh13bff812003-04-15 01:19:47 +000026** $Id: build.c,v 1.145 2003/04/15 01:19:48 drh Exp $
drh75897232000-05-29 14:26:00 +000027*/
28#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000029#include <ctype.h>
drh75897232000-05-29 14:26:00 +000030
31/*
drhe0bc4042002-06-25 01:09:11 +000032** This routine is called when a new SQL statement is beginning to
33** be parsed. Check to see if the schema for the database needs
34** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
35** If it does, then read it.
36*/
37void sqliteBeginParse(Parse *pParse, int explainFlag){
38 sqlite *db = pParse->db;
39 pParse->explain = explainFlag;
40 if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){
41 int rc = sqliteInit(db, &pParse->zErrMsg);
42 if( rc!=SQLITE_OK ){
43 pParse->rc = rc;
44 pParse->nErr++;
45 }
46 }
47}
48
49/*
drhb86ccfb2003-01-28 23:13:10 +000050** This is a fake callback procedure used when sqlite_exec() is
51** invoked with a NULL callback pointer. If we pass a NULL callback
52** pointer into sqliteVdbeExec() it will return at every OP_Callback,
53** which we do not want it to do. So we substitute a pointer to this
54** procedure in place of the NULL.
55*/
56static int fakeCallback(void *NotUsed, int n, char **az1, char **az2){
57 return 0;
58}
59
60/*
drh75897232000-05-29 14:26:00 +000061** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000062** parsed and we want to execute the VDBE code to implement
63** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000064** constructed VDBE code to do the work of the SQL statement.
65** This routine just has to execute the VDBE code.
66**
67** Note that if an error occurred, it might be the case that
68** no VDBE code was generated.
69*/
70void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000071 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000072 sqlite *db = pParse->db;
drhb86ccfb2003-01-28 23:13:10 +000073 Vdbe *v = pParse->pVdbe;
74 int (*xCallback)(void*,int,char**,char**);
75
drhdaffd0e2001-04-11 14:28:42 +000076 if( sqlite_malloc_failed ) return;
drhb86ccfb2003-01-28 23:13:10 +000077 xCallback = pParse->xCallback;
78 if( xCallback==0 && pParse->useCallback ) xCallback = fakeCallback;
79 if( v && pParse->nErr==0 ){
80 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
81 sqliteVdbeTrace(v, trace);
82 sqliteVdbeMakeReady(v, xCallback, pParse->pArg, pParse->explain);
83 if( pParse->useCallback ){
84 if( pParse->explain ){
85 rc = sqliteVdbeList(v);
drh001bbcb2003-03-19 03:14:00 +000086 db->next_cookie = db->aDb[0].schema_cookie;
drhb86ccfb2003-01-28 23:13:10 +000087 }else{
88 sqliteVdbeExec(v);
89 }
90 rc = sqliteVdbeFinalize(v, &pParse->zErrMsg);
drhecdc7532001-09-23 02:35:53 +000091 if( rc ) pParse->nErr++;
drhb86ccfb2003-01-28 23:13:10 +000092 pParse->pVdbe = 0;
93 pParse->rc = rc;
94 if( rc ) pParse->nErr++;
95 }else{
96 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drh75897232000-05-29 14:26:00 +000097 }
drhd8bc7082000-06-07 23:51:50 +000098 pParse->colNamesSet = 0;
drh50e5dad2001-09-15 00:57:28 +000099 pParse->schemaVerified = 0;
drh483750b2003-01-29 18:46:51 +0000100 }else if( pParse->useCallback==0 ){
101 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000102 }
drha226d052002-09-25 19:04:07 +0000103 pParse->nTab = 0;
104 pParse->nMem = 0;
105 pParse->nSet = 0;
106 pParse->nAgg = 0;
drh75897232000-05-29 14:26:00 +0000107}
108
109/*
drhf57b3392001-10-08 13:22:32 +0000110** Locate the in-memory structure that describes
111** a particular database table given the name
drh75897232000-05-29 14:26:00 +0000112** of that table. Return NULL if not found.
113*/
drhd24cc422003-03-27 12:51:24 +0000114Table *sqliteFindTable(sqlite *db, const char *zName, const char *zDatabase){
115 Table *p = 0;
116 int i;
117 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000118 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
119 if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) ) continue;
120 p = sqliteHashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000121 if( p ) break;
122 }
drh74e24cd2002-01-09 03:19:59 +0000123 return p;
drh75897232000-05-29 14:26:00 +0000124}
125
126/*
drhf57b3392001-10-08 13:22:32 +0000127** Locate the in-memory structure that describes
128** a particular index given the name of that index.
129** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000130*/
drhd24cc422003-03-27 12:51:24 +0000131Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){
132 Index *p = 0;
133 int i;
134 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000135 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
136 if( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) ) continue;
137 p = sqliteHashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000138 if( p ) break;
139 }
drh74e24cd2002-01-09 03:19:59 +0000140 return p;
drh75897232000-05-29 14:26:00 +0000141}
142
143/*
144** Remove the given index from the index hash table, and free
145** its memory structures.
146**
drhd229ca92002-01-09 13:30:41 +0000147** The index is removed from the database hash tables but
148** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000149** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000150*/
drh74e24cd2002-01-09 03:19:59 +0000151static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000152 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000153
drhd229ca92002-01-09 13:30:41 +0000154 assert( db!=0 && p->zName!=0 );
drhd24cc422003-03-27 12:51:24 +0000155 pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
156 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000157 if( pOld!=0 && pOld!=p ){
drhd24cc422003-03-27 12:51:24 +0000158 sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
159 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000160 }
drh74e24cd2002-01-09 03:19:59 +0000161 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000162}
163
164/*
drhbeae3192001-09-22 18:12:08 +0000165** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000166** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000167** structures.
168*/
drh6d4abfb2001-10-22 02:58:08 +0000169void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000170 if( pIndex->pTable->pIndex==pIndex ){
171 pIndex->pTable->pIndex = pIndex->pNext;
172 }else{
173 Index *p;
174 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
175 if( p && p->pNext==pIndex ){
176 p->pNext = pIndex->pNext;
177 }
178 }
179 sqliteDeleteIndex(db, pIndex);
180}
181
182/*
drhe0bc4042002-06-25 01:09:11 +0000183** Erase all schema information from the in-memory hash tables of
184** database connection. This routine is called to reclaim memory
185** before the connection closes. It is also called during a rollback
186** if there were schema changes during the transaction.
drh1c2d8412003-03-31 00:30:47 +0000187**
188** If iDb<=0 then reset the internal schema tables for all database
189** files. If iDb>=2 then reset the internal schema for only the
190** single file indicates.
drh74e24cd2002-01-09 03:19:59 +0000191*/
drh1c2d8412003-03-31 00:30:47 +0000192void sqliteResetInternalSchema(sqlite *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000193 HashElem *pElem;
194 Hash temp1;
195 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000196 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000197
drh1c2d8412003-03-31 00:30:47 +0000198 assert( iDb>=0 && iDb<db->nDb );
199 db->flags &= ~SQLITE_Initialized;
200 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000201 Db *pDb = &db->aDb[i];
202 temp1 = pDb->tblHash;
203 temp2 = pDb->trigHash;
204 sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
205 sqliteHashClear(&pDb->aFKey);
206 sqliteHashClear(&pDb->idxHash);
207 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
208 Trigger *pTrigger = sqliteHashData(pElem);
209 sqliteDeleteTrigger(pTrigger);
210 }
211 sqliteHashClear(&temp2);
212 sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
213 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
214 Table *pTab = sqliteHashData(pElem);
215 sqliteDeleteTable(db, pTab);
216 }
217 sqliteHashClear(&temp1);
drh1c2d8412003-03-31 00:30:47 +0000218 db->aDb[i].flags &= ~SQLITE_Initialized;
219 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000220 }
drh1c2d8412003-03-31 00:30:47 +0000221 assert( iDb==0 );
222 db->flags &= ~SQLITE_InternChanges;
223
224 /* If one or more of the auxiliary database files has been closed,
225 ** then remove then from the auxiliary database list. We take the
226 ** opportunity to do this here since we have just deleted all of the
227 ** schema hash tables and therefore do not have to make any changes
228 ** to any of those tables.
229 */
230 for(i=j=2; i<db->nDb; i++){
231 if( db->aDb[i].pBt==0 ){
232 sqliteFree(db->aDb[i].zName);
233 db->aDb[i].zName = 0;
234 continue;
235 }
236 if( j<i ){
237 db->aDb[j++] = db->aDb[i];
238 }
239 }
240 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
241 db->nDb = j;
242 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
243 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
244 sqliteFree(db->aDb);
245 db->aDb = db->aDbStatic;
246 }
drhe0bc4042002-06-25 01:09:11 +0000247}
248
249/*
250** This routine is called whenever a rollback occurs. If there were
251** schema changes during the transaction, then we have to reset the
252** internal hash tables and reload them from disk.
253*/
254void sqliteRollbackInternalChanges(sqlite *db){
255 if( db->flags & SQLITE_InternChanges ){
drh1c2d8412003-03-31 00:30:47 +0000256 sqliteResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000257 }
258}
259
260/*
261** This routine is called when a commit occurs.
262*/
263void sqliteCommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000264 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000265 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000266}
267
268/*
drh75897232000-05-29 14:26:00 +0000269** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000270** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000271**
272** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000273** the table data structure from the hash table. Nor does it remove
274** foreign keys from the sqlite.aFKey hash table. But it does destroy
275** memory structures of the indices and foreign keys associated with
276** the table.
drhdaffd0e2001-04-11 14:28:42 +0000277**
278** Indices associated with the table are unlinked from the "db"
279** data structure if db!=NULL. If db==NULL, indices attached to
280** the table are deleted, but it is assumed they have already been
281** unlinked.
drh75897232000-05-29 14:26:00 +0000282*/
283void sqliteDeleteTable(sqlite *db, Table *pTable){
284 int i;
285 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000286 FKey *pFKey, *pNextFKey;
287
drh75897232000-05-29 14:26:00 +0000288 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000289
290 /* Delete all indices associated with this table
291 */
292 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
293 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000294 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000295 sqliteDeleteIndex(db, pIndex);
296 }
297
298 /* Delete all foreign keys associated with this table. The keys
299 ** should have already been unlinked from the db->aFKey hash table
300 */
301 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
302 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000303 assert( pTable->iDb<db->nDb );
304 assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
305 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000306 sqliteFree(pFKey);
307 }
308
309 /* Delete the Table structure itself.
310 */
drh75897232000-05-29 14:26:00 +0000311 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000312 sqliteFree(pTable->aCol[i].zName);
313 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000314 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000315 }
drh6e142f52000-06-08 13:36:40 +0000316 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000317 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000318 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000319 sqliteFree(pTable);
320}
321
322/*
drh5edc3122001-09-13 21:53:09 +0000323** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000324** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000325*/
drh74e24cd2002-01-09 03:19:59 +0000326static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000327 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000328 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000329 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000330 assert( db!=0 );
drhd24cc422003-03-27 12:51:24 +0000331 pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000332 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000333 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
334 int nTo = strlen(pF1->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000335 pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000336 if( pF2==pF1 ){
drhd24cc422003-03-27 12:51:24 +0000337 sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000338 }else{
339 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
340 if( pF2 ){
341 pF2->pNextTo = pF1->pNextTo;
342 }
343 }
344 }
drh74e24cd2002-01-09 03:19:59 +0000345 sqliteDeleteTable(db, p);
346}
347
348/*
drh1ccde152000-06-17 13:12:39 +0000349** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000350**
351** Space to hold the name is obtained from sqliteMalloc() and must
352** be freed by the calling function.
353*/
drhcce7d172000-05-31 15:34:51 +0000354char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000355 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000356 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000357 return zName;
358}
359
360/*
drhe0bc4042002-06-25 01:09:11 +0000361** Generate code to open the appropriate master table. The table
362** opened will be SQLITE_MASTER for persistent tables and
363** SQLITE_TEMP_MASTER for temporary tables. The table is opened
364** on cursor 0.
365*/
366void sqliteOpenMasterTable(Vdbe *v, int isTemp){
drh001bbcb2003-03-19 03:14:00 +0000367 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
368 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
drhe0bc4042002-06-25 01:09:11 +0000369}
370
371/*
drh75897232000-05-29 14:26:00 +0000372** Begin constructing a new table representation in memory. This is
373** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000374** to a CREATE TABLE statement. In particular, this routine is called
375** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000376** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000377** flag is true if the table should be stored in the auxiliary database
378** file instead of in the main database file. This is normally the case
379** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000380** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000381**
drhf57b3392001-10-08 13:22:32 +0000382** The new table record is initialized and put in pParse->pNewTable.
383** As more of the CREATE TABLE statement is parsed, additional action
384** routines will be called to add more information to this record.
385** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
386** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000387*/
drhe5f9c642003-01-13 23:27:31 +0000388void sqliteStartTable(
389 Parse *pParse, /* Parser context */
390 Token *pStart, /* The "CREATE" token */
391 Token *pName, /* Name of table or view to create */
392 int isTemp, /* True if this is a TEMP table */
393 int isView /* True if this is a VIEW */
394){
drh75897232000-05-29 14:26:00 +0000395 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000396 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000397 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000398 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000399 Vdbe *v;
drh1c2d8412003-03-31 00:30:47 +0000400 int iDb;
drh75897232000-05-29 14:26:00 +0000401
402 pParse->sFirstToken = *pStart;
403 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000404 if( zName==0 ) return;
drhd24cc422003-03-27 12:51:24 +0000405 if( pParse->iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000406#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000407 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000408 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drh77ad4e42003-01-14 02:49:27 +0000409 sqliteFree(zName);
drhed6c8672003-01-12 18:02:16 +0000410 return;
411 }
drhe5f9c642003-01-13 23:27:31 +0000412 {
413 int code;
414 if( isView ){
415 if( isTemp ){
416 code = SQLITE_CREATE_TEMP_VIEW;
417 }else{
418 code = SQLITE_CREATE_VIEW;
419 }
420 }else{
421 if( isTemp ){
422 code = SQLITE_CREATE_TEMP_TABLE;
423 }else{
424 code = SQLITE_CREATE_TABLE;
425 }
426 }
427 if( sqliteAuthCheck(pParse, code, zName, 0) ){
drh77ad4e42003-01-14 02:49:27 +0000428 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000429 return;
430 }
431 }
432#endif
433
drhf57b3392001-10-08 13:22:32 +0000434
435 /* Before trying to create a temporary table, make sure the Btree for
436 ** holding temporary tables is open.
437 */
drhd24cc422003-03-27 12:51:24 +0000438 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
drh13bff812003-04-15 01:19:47 +0000439 int rc = sqliteBtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000440 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000441 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000442 "file for storing temporary tables", 0);
443 pParse->nErr++;
444 return;
445 }
446 if( db->flags & SQLITE_InTrans ){
drh001bbcb2003-03-19 03:14:00 +0000447 rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000448 if( rc!=SQLITE_OK ){
449 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000450 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000451 pParse->nErr++;
452 return;
453 }
454 }
455 }
456
457 /* Make sure the new table name does not collide with an existing
458 ** index or table name. Issue an error message if it does.
459 **
460 ** If we are re-reading the sqlite_master table because of a schema
461 ** change and a new permanent table is found whose name collides with
drhd24cc422003-03-27 12:51:24 +0000462 ** an existing temporary table, that is not an error.
drhf57b3392001-10-08 13:22:32 +0000463 */
drhd24cc422003-03-27 12:51:24 +0000464 pTable = sqliteFindTable(db, zName, 0);
drh1c2d8412003-03-31 00:30:47 +0000465 iDb = isTemp ? 1 : pParse->iDb;
466 if( pTable!=0 && (pTable->iDb==iDb || !pParse->initFlag) ){
drhd24cc422003-03-27 12:51:24 +0000467 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
468 " already exists", 0, 0);
469 sqliteFree(zName);
470 pParse->nErr++;
471 return;
drh75897232000-05-29 14:26:00 +0000472 }
drhd24cc422003-03-27 12:51:24 +0000473 if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
474 (pIdx->iDb==0 || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000475 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
476 zName, 0);
drh75897232000-05-29 14:26:00 +0000477 sqliteFree(zName);
478 pParse->nErr++;
479 return;
480 }
481 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000482 if( pTable==0 ){
483 sqliteFree(zName);
484 return;
485 }
drh75897232000-05-29 14:26:00 +0000486 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000487 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000488 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000489 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000490 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000491 pTable->iDb = iDb;
drhbe0072d2001-09-13 14:46:09 +0000492 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000493 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000494
495 /* Begin generating the code that will insert the table record into
496 ** the SQLITE_MASTER table. Note in particular that we must go ahead
497 ** and allocate the record number for the table entry now. Before any
498 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
499 ** indices to be created and the table record must come before the
500 ** indices. Hence, the record number for the table must be allocated
501 ** now.
502 */
drhadbca9c2001-09-27 15:11:53 +0000503 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000504 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000505 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000506 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
507 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000508 }
drhe0bc4042002-06-25 01:09:11 +0000509 sqliteOpenMasterTable(v, isTemp);
510 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
511 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
512 sqliteVdbeAddOp(v, OP_String, 0, 0);
513 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000514 }
drh75897232000-05-29 14:26:00 +0000515}
516
517/*
518** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000519**
520** The parser calls this routine once for each column declaration
521** in a CREATE TABLE statement. sqliteStartTable() gets called
522** first to get things going. Then this routine is called for each
523** column.
drh75897232000-05-29 14:26:00 +0000524*/
525void sqliteAddColumn(Parse *pParse, Token *pName){
526 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000527 int i;
528 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000529 Column *pCol;
drh75897232000-05-29 14:26:00 +0000530 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000531 sqliteSetNString(&z, pName->z, pName->n, 0);
532 if( z==0 ) return;
533 sqliteDequote(z);
534 for(i=0; i<p->nCol; i++){
535 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
536 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
537 pParse->nErr++;
538 sqliteFree(z);
539 return;
540 }
541 }
drh75897232000-05-29 14:26:00 +0000542 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000543 Column *aNew;
544 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
545 if( aNew==0 ) return;
546 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000547 }
drhc9b84a12002-06-20 11:36:48 +0000548 pCol = &p->aCol[p->nCol];
549 memset(pCol, 0, sizeof(p->aCol[0]));
550 pCol->zName = z;
551 pCol->sortOrder = SQLITE_SO_NUM;
552 p->nCol++;
drh75897232000-05-29 14:26:00 +0000553}
554
555/*
drh382c0242001-10-06 16:33:02 +0000556** This routine is called by the parser while in the middle of
557** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
558** been seen on a column. This routine sets the notNull flag on
559** the column currently under construction.
560*/
drh9cfcf5d2002-01-29 18:41:24 +0000561void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000562 Table *p;
563 int i;
564 if( (p = pParse->pNewTable)==0 ) return;
565 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000566 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000567}
568
569/*
570** This routine is called by the parser while in the middle of
571** parsing a CREATE TABLE statement. The pFirst token is the first
572** token in the sequence of tokens that describe the type of the
573** column currently under construction. pLast is the last token
574** in the sequence. Use this information to construct a string
575** that contains the typename of the column and store that string
576** in zType.
577*/
578void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
579 Table *p;
580 int i, j;
581 int n;
582 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000583 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000584 if( (p = pParse->pNewTable)==0 ) return;
585 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000586 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000587 pCol = &p->aCol[i];
588 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000589 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000590 sqliteSetNString(pz, pFirst->z, n, 0);
591 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000592 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000593 for(i=j=0; z[i]; i++){
594 int c = z[i];
595 if( isspace(c) ) continue;
596 z[j++] = c;
597 }
598 z[j] = 0;
drh3d037a92002-08-15 01:26:09 +0000599 if( pParse->db->file_format>=4 ){
drhfcb78a42003-01-18 20:11:05 +0000600 pCol->sortOrder = sqliteCollateType(z, n);
601 }else{
602 pCol->sortOrder = SQLITE_SO_NUM;
drhc9b84a12002-06-20 11:36:48 +0000603 }
drh382c0242001-10-06 16:33:02 +0000604}
605
606/*
drh7020f652000-06-03 18:06:52 +0000607** The given token is the default value for the last column added to
608** the table currently under construction. If "minusFlag" is true, it
609** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000610**
611** This routine is called by the parser while in the middle of
612** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000613*/
614void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
615 Table *p;
616 int i;
617 char **pz;
618 if( (p = pParse->pNewTable)==0 ) return;
619 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000620 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000621 pz = &p->aCol[i].zDflt;
622 if( minusFlag ){
623 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
624 }else{
625 sqliteSetNString(pz, pVal->z, pVal->n, 0);
626 }
627 sqliteDequote(*pz);
628}
629
630/*
drh4a324312001-12-21 14:30:42 +0000631** Designate the PRIMARY KEY for the table. pList is a list of names
632** of columns that form the primary key. If pList is NULL, then the
633** most recently added column of the table is the primary key.
634**
635** A table can have at most one primary key. If the table already has
636** a primary key (and this is the second primary key) then create an
637** error.
638**
639** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
640** then we will try to use that column as the row id. (Exception:
641** For backwards compatibility with older databases, do not do this
642** if the file format version number is less than 1.) Set the Table.iPKey
643** field of the table under construction to be the index of the
644** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
645** no INTEGER PRIMARY KEY.
646**
647** If the key is not an INTEGER PRIMARY KEY, then create a unique
648** index for the key. No index is created for INTEGER PRIMARY KEYs.
649*/
drh9cfcf5d2002-01-29 18:41:24 +0000650void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000651 Table *pTab = pParse->pNewTable;
652 char *zType = 0;
653 int iCol = -1;
drhe0194f22003-02-26 13:52:51 +0000654 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000655 if( pTab->hasPrimKey ){
656 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
657 "\" has more than one primary key", 0);
658 pParse->nErr++;
drhe0194f22003-02-26 13:52:51 +0000659 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000660 }
661 pTab->hasPrimKey = 1;
662 if( pList==0 ){
663 iCol = pTab->nCol - 1;
664 }else if( pList->nId==1 ){
665 for(iCol=0; iCol<pTab->nCol; iCol++){
666 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
667 }
668 }
669 if( iCol>=0 && iCol<pTab->nCol ){
670 zType = pTab->aCol[iCol].zType;
671 }
672 if( pParse->db->file_format>=1 &&
673 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
674 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000675 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000676 }else{
drhd24cc422003-03-27 12:51:24 +0000677 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000678 pList = 0;
drh4a324312001-12-21 14:30:42 +0000679 }
drhe0194f22003-02-26 13:52:51 +0000680
681primary_key_exit:
682 sqliteIdListDelete(pList);
683 return;
drh4a324312001-12-21 14:30:42 +0000684}
685
686/*
drhfcb78a42003-01-18 20:11:05 +0000687** Return the appropriate collating type given a type name.
688**
689** The collation type is text (SQLITE_SO_TEXT) if the type
690** name contains the character stream "text" or "blob" or
691** "clob". Any other type name is collated as numeric
692** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000693*/
drhfcb78a42003-01-18 20:11:05 +0000694int sqliteCollateType(const char *zType, int nType){
695 int i;
drhfcb78a42003-01-18 20:11:05 +0000696 for(i=0; i<nType-1; i++){
697 switch( zType[i] ){
698 case 'b':
699 case 'B': {
700 if( i<nType-3 && sqliteStrNICmp(&zType[i],"blob",4)==0 ){
701 return SQLITE_SO_TEXT;
702 }
703 break;
704 }
705 case 'c':
706 case 'C': {
707 if( i<nType-3 && (sqliteStrNICmp(&zType[i],"char",4)==0 ||
708 sqliteStrNICmp(&zType[i],"clob",4)==0)
709 ){
710 return SQLITE_SO_TEXT;
711 }
712 break;
713 }
714 case 'x':
715 case 'X': {
716 if( i>=2 && sqliteStrNICmp(&zType[i-2],"text",4)==0 ){
717 return SQLITE_SO_TEXT;
718 }
719 break;
720 }
721 default: {
722 break;
723 }
724 }
drh8e2ca022002-06-17 17:07:19 +0000725 }
drhfcb78a42003-01-18 20:11:05 +0000726 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000727}
728
729/*
730** This routine is called by the parser while in the middle of
731** parsing a CREATE TABLE statement. A "COLLATE" clause has
732** been seen on a column. This routine sets the Column.sortOrder on
733** the column currently under construction.
734*/
735void sqliteAddCollateType(Parse *pParse, int collType){
736 Table *p;
737 int i;
738 if( (p = pParse->pNewTable)==0 ) return;
739 i = p->nCol-1;
740 if( i>=0 ) p->aCol[i].sortOrder = collType;
741}
742
743/*
drh50e5dad2001-09-15 00:57:28 +0000744** Come up with a new random value for the schema cookie. Make sure
745** the new value is different from the old.
746**
747** The schema cookie is used to determine when the schema for the
748** database changes. After each schema change, the cookie value
749** changes. When a process first reads the schema it records the
750** cookie. Thereafter, whenever it goes to access the database,
751** it checks the cookie to make sure the schema has not changed
752** since it was last read.
753**
754** This plan is not completely bullet-proof. It is possible for
755** the schema to change multiple times and for the cookie to be
756** set back to prior value. But schema changes are infrequent
757** and the probability of hitting the same cookie value is only
758** 1 chance in 2^32. So we're safe enough.
759*/
drhe0bc4042002-06-25 01:09:11 +0000760void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh001bbcb2003-03-19 03:14:00 +0000761 if( db->next_cookie==db->aDb[0].schema_cookie ){
762 db->next_cookie = db->aDb[0].schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000763 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000764 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
765 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000766 }
767}
768
769/*
drh969fa7c2002-02-18 18:30:32 +0000770** Measure the number of characters needed to output the given
771** identifier. The number returned includes any quotes used
772** but does not include the null terminator.
773*/
774static int identLength(const char *z){
775 int n;
drh17f71932002-02-21 12:01:27 +0000776 int needQuote = 0;
777 for(n=0; *z; n++, z++){
778 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000779 }
drh17f71932002-02-21 12:01:27 +0000780 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000781}
782
783/*
784** Write an identifier onto the end of the given string. Add
785** quote characters as needed.
786*/
787static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000788 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000789 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000790 for(j=0; zIdent[j]; j++){
791 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
792 }
793 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
794 || sqliteKeywordCode(zIdent, j)!=TK_ID;
795 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000796 for(j=0; zIdent[j]; j++){
797 z[i++] = zIdent[j];
798 if( zIdent[j]=='\'' ) z[i++] = '\'';
799 }
drh17f71932002-02-21 12:01:27 +0000800 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000801 z[i] = 0;
802 *pIdx = i;
803}
804
805/*
806** Generate a CREATE TABLE statement appropriate for the given
807** table. Memory to hold the text of the statement is obtained
808** from sqliteMalloc() and must be freed by the calling function.
809*/
810static char *createTableStmt(Table *p){
811 int i, k, n;
812 char *zStmt;
813 char *zSep, *zSep2, *zEnd;
814 n = 0;
815 for(i=0; i<p->nCol; i++){
816 n += identLength(p->aCol[i].zName);
817 }
818 n += identLength(p->zName);
819 if( n<40 ){
820 zSep = "";
821 zSep2 = ",";
822 zEnd = ")";
823 }else{
824 zSep = "\n ";
825 zSep2 = ",\n ";
826 zEnd = "\n)";
827 }
drhe0bc4042002-06-25 01:09:11 +0000828 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000829 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000830 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +0000831 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000832 k = strlen(zStmt);
833 identPut(zStmt, &k, p->zName);
834 zStmt[k++] = '(';
835 for(i=0; i<p->nCol; i++){
836 strcpy(&zStmt[k], zSep);
837 k += strlen(&zStmt[k]);
838 zSep = zSep2;
839 identPut(zStmt, &k, p->aCol[i].zName);
840 }
841 strcpy(&zStmt[k], zEnd);
842 return zStmt;
843}
844
845/*
drh75897232000-05-29 14:26:00 +0000846** This routine is called to report the final ")" that terminates
847** a CREATE TABLE statement.
848**
drhf57b3392001-10-08 13:22:32 +0000849** The table structure that other action routines have been building
850** is added to the internal hash tables, assuming no errors have
851** occurred.
drh75897232000-05-29 14:26:00 +0000852**
drh1ccde152000-06-17 13:12:39 +0000853** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000854** unless this is a temporary table or initFlag==1. When initFlag==1,
855** it means we are reading the sqlite_master table because we just
856** connected to the database or because the sqlite_master table has
857** recently changes, so the entry for this table already exists in
858** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000859**
860** If the pSelect argument is not NULL, it means that this routine
861** was called to create a table generated from a
862** "CREATE TABLE ... AS SELECT ..." statement. The column names of
863** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000864*/
drh969fa7c2002-02-18 18:30:32 +0000865void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000866 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000867 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000868
drh969fa7c2002-02-18 18:30:32 +0000869 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000870 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000871 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000872
drh969fa7c2002-02-18 18:30:32 +0000873 /* If the table is generated from a SELECT, then construct the
874 ** list of columns and the text of the table.
875 */
876 if( pSelect ){
877 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000878 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000879 assert( p->aCol==0 );
880 p->nCol = pSelTab->nCol;
881 p->aCol = pSelTab->aCol;
882 pSelTab->nCol = 0;
883 pSelTab->aCol = 0;
884 sqliteDeleteTable(0, pSelTab);
885 }
886
drhd78eeee2001-09-13 16:18:53 +0000887 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000888 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
889 ** So do not write to the disk again. Extract the root page number
890 ** for the table from the pParse->newTnum field. (The page number
891 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000892 */
893 if( pParse->initFlag ){
894 p->tnum = pParse->newTnum;
895 }
896
drhe3c41372001-09-17 20:25:58 +0000897 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000898 ** in the SQLITE_MASTER table of the database. The record number
899 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000900 **
drhe0bc4042002-06-25 01:09:11 +0000901 ** If this is a TEMPORARY table, write the entry into the auxiliary
902 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000903 */
904 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000905 int n;
drhd8bc7082000-06-07 23:51:50 +0000906 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000907
drhd8bc7082000-06-07 23:51:50 +0000908 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000909 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000910 if( p->pSelect==0 ){
911 /* A regular table */
drhd24cc422003-03-27 12:51:24 +0000912 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->iDb);
drh4ff6dfa2002-03-03 23:06:00 +0000913 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
914 }else{
915 /* A view */
916 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
917 }
drh969fa7c2002-02-18 18:30:32 +0000918 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000919 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
920 sqliteVdbeAddOp(v, OP_String, 0, 0);
921 if( p->pSelect==0 ){
922 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
923 }else{
924 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000925 }
drhe0bc4042002-06-25 01:09:11 +0000926 sqliteVdbeAddOp(v, OP_String, 0, 0);
927 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
928 sqliteVdbeAddOp(v, OP_String, 0, 0);
929 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
930 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
931 sqliteVdbeAddOp(v, OP_String, 0, 0);
932 if( pSelect ){
933 char *z = createTableStmt(p);
934 n = z ? strlen(z) : 0;
935 sqliteVdbeChangeP3(v, -1, z, n);
936 sqliteFree(z);
937 }else{
938 assert( pEnd!=0 );
939 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
940 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
941 }
942 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
943 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhd24cc422003-03-27 12:51:24 +0000944 if( !p->iDb ){
drhe0bc4042002-06-25 01:09:11 +0000945 sqliteChangeCookie(db, v);
946 }
947 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000948 if( pSelect ){
drhd24cc422003-03-27 12:51:24 +0000949 sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000950 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +0000951 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000952 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000953 }
drh1c928532002-01-31 15:54:21 +0000954 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000955 }
drh17e9e292003-02-01 13:53:28 +0000956
957 /* Add the table to the in-memory representation of the database.
958 */
drhd24cc422003-03-27 12:51:24 +0000959 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +0000960 Table *pOld;
961 FKey *pFKey;
drhd24cc422003-03-27 12:51:24 +0000962 pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
963 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +0000964 if( pOld ){
965 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
966 return;
967 }
968 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
969 int nTo = strlen(pFKey->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000970 pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
971 sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +0000972 }
973 pParse->pNewTable = 0;
974 db->nTable++;
975 db->flags |= SQLITE_InternChanges;
976 }
drh75897232000-05-29 14:26:00 +0000977}
978
979/*
drha76b5df2002-02-23 02:32:10 +0000980** The parser calls this routine in order to create a new VIEW
981*/
982void sqliteCreateView(
983 Parse *pParse, /* The parsing context */
984 Token *pBegin, /* The CREATE token that begins the statement */
985 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000986 Select *pSelect, /* A SELECT statement that will become the new view */
987 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000988){
drha76b5df2002-02-23 02:32:10 +0000989 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000990 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000991 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000992 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000993
drhe5f9c642003-01-13 23:27:31 +0000994 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +0000995 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +0000996 if( p==0 || pParse->nErr ){
drh417be792002-03-03 18:59:40 +0000997 sqliteSelectDelete(pSelect);
998 return;
999 }
drh174b6192002-12-03 02:22:52 +00001000
drh4b59ab52002-08-24 18:24:51 +00001001 /* Make a copy of the entire SELECT statement that defines the view.
1002 ** This will force all the Expr.token.z values to be dynamically
1003 ** allocated rather than point to the input string - which means that
1004 ** they will persist after the current sqlite_exec() call returns.
1005 */
1006 p->pSelect = sqliteSelectDup(pSelect);
1007 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001008 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +00001009 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001010 }
drh4b59ab52002-08-24 18:24:51 +00001011
1012 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1013 ** the end.
1014 */
drha76b5df2002-02-23 02:32:10 +00001015 sEnd = pParse->sLastToken;
1016 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1017 sEnd.z += sEnd.n;
1018 }
1019 sEnd.n = 0;
1020 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001021 z = pBegin->z;
1022 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1023 sEnd.z = &z[n-1];
1024 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001025
1026 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
1027 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001028 return;
drh417be792002-03-03 18:59:40 +00001029}
drha76b5df2002-02-23 02:32:10 +00001030
drh417be792002-03-03 18:59:40 +00001031/*
1032** The Table structure pTable is really a VIEW. Fill in the names of
1033** the columns of the view in the pTable structure. Return the number
1034** of errors. If an error is seen leave an error message in pPare->zErrMsg.
1035*/
1036int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
1037 ExprList *pEList;
1038 Select *pSel;
1039 Table *pSelTab;
1040 int nErr = 0;
1041
1042 assert( pTable );
1043
1044 /* A positive nCol means the columns names for this view are
1045 ** already known.
1046 */
1047 if( pTable->nCol>0 ) return 0;
1048
1049 /* A negative nCol is a special marker meaning that we are currently
1050 ** trying to compute the column names. If we enter this routine with
1051 ** a negative nCol, it means two or more views form a loop, like this:
1052 **
1053 ** CREATE VIEW one AS SELECT * FROM two;
1054 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001055 **
1056 ** Actually, this error is caught previously and so the following test
1057 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001058 */
1059 if( pTable->nCol<0 ){
1060 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
1061 " is circularly defined", 0);
1062 pParse->nErr++;
1063 return 1;
1064 }
1065
1066 /* If we get this far, it means we need to compute the table names.
1067 */
1068 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1069 pSel = pTable->pSelect;
1070
1071 /* Note that the call to sqliteResultSetOfSelect() will expand any
1072 ** "*" elements in this list. But we will need to restore the list
1073 ** back to its original configuration afterwards, so we save a copy of
1074 ** the original in pEList.
1075 */
1076 pEList = pSel->pEList;
1077 pSel->pEList = sqliteExprListDup(pEList);
1078 if( pSel->pEList==0 ){
1079 pSel->pEList = pEList;
1080 return 1; /* Malloc failed */
1081 }
1082 pTable->nCol = -1;
1083 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1084 if( pSelTab ){
1085 assert( pTable->aCol==0 );
1086 pTable->nCol = pSelTab->nCol;
1087 pTable->aCol = pSelTab->aCol;
1088 pSelTab->nCol = 0;
1089 pSelTab->aCol = 0;
1090 sqliteDeleteTable(0, pSelTab);
drhd24cc422003-03-27 12:51:24 +00001091 pParse->db->aDb[pTable->iDb].flags |= SQLITE_UnresetViews;
drh417be792002-03-03 18:59:40 +00001092 }else{
1093 pTable->nCol = 0;
1094 nErr++;
1095 }
1096 sqliteSelectUnbind(pSel);
1097 sqliteExprListDelete(pSel->pEList);
1098 pSel->pEList = pEList;
1099 return nErr;
1100}
1101
1102/*
1103** Clear the column names from the VIEW pTable.
1104**
1105** This routine is called whenever any other table or view is modified.
1106** The view passed into this routine might depend directly or indirectly
1107** on the modified or deleted table so we need to clear the old column
1108** names so that they will be recomputed.
1109*/
1110static void sqliteViewResetColumnNames(Table *pTable){
1111 int i;
1112 if( pTable==0 || pTable->pSelect==0 ) return;
1113 if( pTable->nCol==0 ) return;
1114 for(i=0; i<pTable->nCol; i++){
1115 sqliteFree(pTable->aCol[i].zName);
1116 sqliteFree(pTable->aCol[i].zDflt);
1117 sqliteFree(pTable->aCol[i].zType);
1118 }
1119 sqliteFree(pTable->aCol);
1120 pTable->aCol = 0;
1121 pTable->nCol = 0;
1122}
1123
1124/*
1125** Clear the column names from every VIEW.
1126*/
drhd24cc422003-03-27 12:51:24 +00001127static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001128 HashElem *i;
drhd24cc422003-03-27 12:51:24 +00001129 if( (db->aDb[idx].flags & SQLITE_UnresetViews)==0 ) return;
1130 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001131 Table *pTab = sqliteHashData(i);
1132 if( pTab->pSelect ){
1133 sqliteViewResetColumnNames(pTab);
1134 }
1135 }
drhd24cc422003-03-27 12:51:24 +00001136 db->aDb[idx].flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001137}
1138
1139/*
drh75897232000-05-29 14:26:00 +00001140** Given a token, look up a table with that name. If not found, leave
1141** an error for the parser to find and return NULL.
1142*/
drhcce7d172000-05-31 15:34:51 +00001143Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001144 char *zName;
1145 Table *pTab;
1146 zName = sqliteTableNameFromToken(pTok);
1147 if( zName==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001148 pTab = sqliteFindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001149 sqliteFree(zName);
1150 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001151 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1152 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001153 pParse->nErr++;
1154 }
1155 return pTab;
1156}
1157
1158/*
1159** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001160** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001161*/
drh4ff6dfa2002-03-03 23:06:00 +00001162void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001163 Table *pTable;
drh75897232000-05-29 14:26:00 +00001164 Vdbe *v;
1165 int base;
drh5edc3122001-09-13 21:53:09 +00001166 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001167 int iDb;
drh75897232000-05-29 14:26:00 +00001168
drhdaffd0e2001-04-11 14:28:42 +00001169 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001170 pTable = sqliteTableFromToken(pParse, pName);
1171 if( pTable==0 ) return;
drhd24cc422003-03-27 12:51:24 +00001172 iDb = pTable->iDb;
drhe5f9c642003-01-13 23:27:31 +00001173#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001174 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTable->iDb),0)){
drhed6c8672003-01-12 18:02:16 +00001175 return;
1176 }
drhe5f9c642003-01-13 23:27:31 +00001177 {
1178 int code;
1179 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001180 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001181 code = SQLITE_DROP_TEMP_VIEW;
1182 }else{
1183 code = SQLITE_DROP_VIEW;
1184 }
1185 }else{
drhd24cc422003-03-27 12:51:24 +00001186 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001187 code = SQLITE_DROP_TEMP_TABLE;
1188 }else{
1189 code = SQLITE_DROP_TABLE;
1190 }
1191 }
1192 if( sqliteAuthCheck(pParse, code, pTable->zName, 0) ){
1193 return;
1194 }
drh77ad4e42003-01-14 02:49:27 +00001195 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0) ){
1196 return;
1197 }
drhe5f9c642003-01-13 23:27:31 +00001198 }
1199#endif
drh75897232000-05-29 14:26:00 +00001200 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001201 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1202 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001203 pParse->nErr++;
1204 return;
1205 }
drh4ff6dfa2002-03-03 23:06:00 +00001206 if( isView && pTable->pSelect==0 ){
1207 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1208 pTable->zName, 0);
1209 pParse->nErr++;
1210 return;
1211 }
1212 if( !isView && pTable->pSelect ){
1213 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1214 pTable->zName, 0);
1215 pParse->nErr++;
1216 return;
1217 }
drh75897232000-05-29 14:26:00 +00001218
drh1ccde152000-06-17 13:12:39 +00001219 /* Generate code to remove the table from the master table
1220 ** on disk.
1221 */
drhd8bc7082000-06-07 23:51:50 +00001222 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001223 if( v ){
1224 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001225 { OP_Rewind, 0, ADDR(8), 0},
1226 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001227 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001228 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001229 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001230 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001231 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001232 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001233 };
1234 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001235 Trigger *pTrigger;
drhd24cc422003-03-27 12:51:24 +00001236 sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
1237 sqliteOpenMasterTable(v, pTable->iDb);
danielk1977c3f9bad2002-05-15 08:30:12 +00001238 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001239 pTrigger = pTable->pTrigger;
1240 while( pTrigger ){
drhd24cc422003-03-27 12:51:24 +00001241 SrcList *pNm;
1242 assert( pTrigger->iDb==pTable->iDb );
1243 pNm = sqliteSrcListAppend(0, 0, 0);
1244 pNm->a[0].zName = sqliteStrDup(pTrigger->name);
1245 pNm->a[0].zDatabase = sqliteStrDup(db->aDb[pTable->iDb].zName);
1246 sqliteDropTrigger(pParse, pNm, 1);
drhe0bc4042002-06-25 01:09:11 +00001247 if( pParse->explain ){
1248 pTrigger = pTrigger->pNext;
1249 }else{
1250 pTrigger = pTable->pTrigger;
1251 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001252 }
drhe0bc4042002-06-25 01:09:11 +00001253 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1254 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001255 if( !pTable->iDb ){
drhe0bc4042002-06-25 01:09:11 +00001256 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001257 }
drhe0bc4042002-06-25 01:09:11 +00001258 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001259 if( !isView ){
drhd24cc422003-03-27 12:51:24 +00001260 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001261 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drhd24cc422003-03-27 12:51:24 +00001262 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001263 }
drh5e00f6c2001-09-13 13:46:56 +00001264 }
drh1c928532002-01-31 15:54:21 +00001265 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001266 }
1267
drhe0bc4042002-06-25 01:09:11 +00001268 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001269 **
1270 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001271 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001272 */
1273 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001274 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001275 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001276 }
drhd24cc422003-03-27 12:51:24 +00001277 sqliteViewResetAll(db, iDb);
drh75897232000-05-29 14:26:00 +00001278}
1279
1280/*
drh38640e12002-07-05 21:42:36 +00001281** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1282** opcode and adds that P3 string to the most recently inserted instruction
1283** in the virtual machine. The P3 string consists of a single character
1284** for each column in the index pIdx of table pTab. If the column uses
1285** a numeric sort order, then the P3 string character corresponding to
1286** that column is 'n'. If the column uses a text sort order, then the
1287** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1288** additional information. See also the sqliteAddKeyType() routine.
1289*/
1290void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1291 char *zType;
1292 Table *pTab;
1293 int i, n;
1294 assert( pIdx!=0 && pIdx->pTable!=0 );
1295 pTab = pIdx->pTable;
1296 n = pIdx->nColumn;
drh8c1238a2003-01-02 14:43:55 +00001297 zType = sqliteMallocRaw( n+1 );
drh38640e12002-07-05 21:42:36 +00001298 if( zType==0 ) return;
1299 for(i=0; i<n; i++){
1300 int iCol = pIdx->aiColumn[i];
1301 assert( iCol>=0 && iCol<pTab->nCol );
1302 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1303 zType[i] = 't';
1304 }else{
1305 zType[i] = 'n';
1306 }
1307 }
1308 zType[n] = 0;
1309 sqliteVdbeChangeP3(v, -1, zType, n);
1310 sqliteFree(zType);
1311}
1312
1313/*
drhc2eef3b2002-08-31 18:53:06 +00001314** This routine is called to create a new foreign key on the table
1315** currently under construction. pFromCol determines which columns
1316** in the current table point to the foreign key. If pFromCol==0 then
1317** connect the key to the last column inserted. pTo is the name of
1318** the table referred to. pToCol is a list of tables in the other
1319** pTo table that the foreign key points to. flags contains all
1320** information about the conflict resolution algorithms specified
1321** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1322**
1323** An FKey structure is created and added to the table currently
1324** under construction in the pParse->pNewTable field. The new FKey
1325** is not linked into db->aFKey at this point - that does not happen
1326** until sqliteEndTable().
1327**
1328** The foreign key is set for IMMEDIATE processing. A subsequent call
1329** to sqliteDeferForeignKey() might change this to DEFERRED.
1330*/
1331void sqliteCreateForeignKey(
1332 Parse *pParse, /* Parsing context */
1333 IdList *pFromCol, /* Columns in this table that point to other table */
1334 Token *pTo, /* Name of the other table */
1335 IdList *pToCol, /* Columns in the other table */
1336 int flags /* Conflict resolution algorithms. */
1337){
1338 Table *p = pParse->pNewTable;
1339 int nByte;
1340 int i;
1341 int nCol;
1342 char *z;
1343 FKey *pFKey = 0;
1344
1345 assert( pTo!=0 );
1346 if( p==0 || pParse->nErr ) goto fk_end;
1347 if( pFromCol==0 ){
1348 int iCol = p->nCol-1;
1349 if( iCol<0 ) goto fk_end;
1350 if( pToCol && pToCol->nId!=1 ){
1351 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1352 p->aCol[iCol].zName, -1,
1353 " should reference only one column of table ", -1,
1354 pTo->z, pTo->n, 0);
1355 pParse->nErr++;
1356 goto fk_end;
1357 }
1358 nCol = 1;
1359 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1360 sqliteSetString(&pParse->zErrMsg,
1361 "number of columns in foreign key does not match the number of "
1362 "columns in the referenced table", 0);
1363 pParse->nErr++;
1364 goto fk_end;
1365 }else{
1366 nCol = pFromCol->nId;
1367 }
1368 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1369 if( pToCol ){
1370 for(i=0; i<pToCol->nId; i++){
1371 nByte += strlen(pToCol->a[i].zName) + 1;
1372 }
1373 }
1374 pFKey = sqliteMalloc( nByte );
1375 if( pFKey==0 ) goto fk_end;
1376 pFKey->pFrom = p;
1377 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001378 z = (char*)&pFKey[1];
1379 pFKey->aCol = (struct sColMap*)z;
1380 z += sizeof(struct sColMap)*nCol;
1381 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001382 memcpy(z, pTo->z, pTo->n);
1383 z[pTo->n] = 0;
1384 z += pTo->n+1;
1385 pFKey->pNextTo = 0;
1386 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001387 if( pFromCol==0 ){
1388 pFKey->aCol[0].iFrom = p->nCol-1;
1389 }else{
1390 for(i=0; i<nCol; i++){
1391 int j;
1392 for(j=0; j<p->nCol; j++){
1393 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1394 pFKey->aCol[i].iFrom = j;
1395 break;
1396 }
1397 }
1398 if( j>=p->nCol ){
1399 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1400 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1401 pParse->nErr++;
1402 goto fk_end;
1403 }
1404 }
1405 }
1406 if( pToCol ){
1407 for(i=0; i<nCol; i++){
1408 int n = strlen(pToCol->a[i].zName);
1409 pFKey->aCol[i].zCol = z;
1410 memcpy(z, pToCol->a[i].zName, n);
1411 z[n] = 0;
1412 z += n+1;
1413 }
1414 }
1415 pFKey->isDeferred = 0;
1416 pFKey->deleteConf = flags & 0xff;
1417 pFKey->updateConf = (flags >> 8 ) & 0xff;
1418 pFKey->insertConf = (flags >> 16 ) & 0xff;
1419
1420 /* Link the foreign key to the table as the last step.
1421 */
1422 p->pFKey = pFKey;
1423 pFKey = 0;
1424
1425fk_end:
1426 sqliteFree(pFKey);
1427 sqliteIdListDelete(pFromCol);
1428 sqliteIdListDelete(pToCol);
1429}
1430
1431/*
1432** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1433** clause is seen as part of a foreign key definition. The isDeferred
1434** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1435** The behavior of the most recently created foreign key is adjusted
1436** accordingly.
1437*/
1438void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1439 Table *pTab;
1440 FKey *pFKey;
1441 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1442 pFKey->isDeferred = isDeferred;
1443}
1444
1445/*
drh75897232000-05-29 14:26:00 +00001446** Create a new index for an SQL table. pIndex is the name of the index
1447** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001448** be NULL for a primary key or an index that is created to satisfy a
1449** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001450** as the table to be indexed. pParse->pNewTable is a table that is
1451** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001452**
drh382c0242001-10-06 16:33:02 +00001453** pList is a list of columns to be indexed. pList will be NULL if this
1454** is a primary key or unique-constraint on the most recent column added
1455** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001456*/
1457void sqliteCreateIndex(
1458 Parse *pParse, /* All information about this parse */
1459 Token *pName, /* Name of the index. May be NULL */
drhd24cc422003-03-27 12:51:24 +00001460 SrcList *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001461 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001462 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drhd24cc422003-03-27 12:51:24 +00001463 int isTemp, /* True if this is a temporary index */
drh75897232000-05-29 14:26:00 +00001464 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1465 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1466){
1467 Table *pTab; /* Table to be indexed */
1468 Index *pIndex; /* The index to be created */
1469 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001470 int i, j;
drhf57b3392001-10-08 13:22:32 +00001471 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001472 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001473
drhdaffd0e2001-04-11 14:28:42 +00001474 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1475
drh75897232000-05-29 14:26:00 +00001476 /*
1477 ** Find the table that is to be indexed. Return early if not found.
1478 */
1479 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001480 assert( pName!=0 );
drhd24cc422003-03-27 12:51:24 +00001481 assert( pTable->nSrc==1 );
drh812d7a22003-03-27 13:50:00 +00001482 pTab = sqliteSrcListLookup(pParse, pTable);
drh75897232000-05-29 14:26:00 +00001483 }else{
drhe3c41372001-09-17 20:25:58 +00001484 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001485 pTab = pParse->pNewTable;
1486 }
1487 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001488 if( pTab->readOnly ){
1489 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1490 " may not be indexed", 0);
1491 pParse->nErr++;
1492 goto exit_create_index;
1493 }
drh1c2d8412003-03-31 00:30:47 +00001494 if( !isTemp && pTab->iDb>=2 && pParse->initFlag==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001495 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drhd24cc422003-03-27 12:51:24 +00001496 " may not have non-temporary indices added", 0);
drh75897232000-05-29 14:26:00 +00001497 pParse->nErr++;
1498 goto exit_create_index;
1499 }
drha76b5df2002-02-23 02:32:10 +00001500 if( pTab->pSelect ){
1501 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1502 pParse->nErr++;
1503 goto exit_create_index;
1504 }
drhd24cc422003-03-27 12:51:24 +00001505 if( pTab->iDb==1 ){
1506 isTemp = 1;
1507 }
drh75897232000-05-29 14:26:00 +00001508
1509 /*
1510 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001511 ** index or table with the same name.
1512 **
1513 ** Exception: If we are reading the names of permanent indices from the
1514 ** sqlite_master table (because some other process changed the schema) and
1515 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001516 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001517 **
1518 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001519 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1520 ** own name.
drh75897232000-05-29 14:26:00 +00001521 */
drhd24cc422003-03-27 12:51:24 +00001522 if( pName && !pParse->initFlag ){
drhf57b3392001-10-08 13:22:32 +00001523 Index *pISameName; /* Another index with the same name */
1524 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001525 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001526 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001527 if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
1528 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1529 " already exists", 0);
1530 pParse->nErr++;
1531 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001532 }
drhd24cc422003-03-27 12:51:24 +00001533 if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
1534 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1535 zName, 0);
1536 pParse->nErr++;
1537 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001538 }
drhd24cc422003-03-27 12:51:24 +00001539 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001540 char zBuf[30];
1541 int n;
1542 Index *pLoop;
1543 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1544 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001545 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001546 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001547 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001548 }else{
1549 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001550 }
1551
drhe5f9c642003-01-13 23:27:31 +00001552 /* Check for authorization to create an index.
1553 */
1554#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001555 assert( isTemp==0 || isTemp==1 );
1556 assert( pTab->iDb==pParse->iDb || isTemp==1 );
1557 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drhe5f9c642003-01-13 23:27:31 +00001558 goto exit_create_index;
1559 }
1560 i = SQLITE_CREATE_INDEX;
drhd24cc422003-03-27 12:51:24 +00001561 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
drhe5f9c642003-01-13 23:27:31 +00001562 if( sqliteAuthCheck(pParse, i, zName, pTab->zName) ){
1563 goto exit_create_index;
1564 }
1565#endif
1566
drh75897232000-05-29 14:26:00 +00001567 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001568 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001569 ** So create a fake list to simulate this.
1570 */
1571 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001572 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001573 nullId.n = strlen(nullId.z);
1574 pList = sqliteIdListAppend(0, &nullId);
1575 if( pList==0 ) goto exit_create_index;
1576 }
1577
1578 /*
1579 ** Allocate the index structure.
1580 */
drhdcc581c2000-05-30 13:44:19 +00001581 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001582 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001583 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001584 pIndex->aiColumn = (int*)&pIndex[1];
1585 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001586 strcpy(pIndex->zName, zName);
1587 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001588 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001589 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001590 pIndex->autoIndex = pName==0;
drhd24cc422003-03-27 12:51:24 +00001591 pIndex->iDb = isTemp ? 1 : pParse->iDb;
drh75897232000-05-29 14:26:00 +00001592
drh1ccde152000-06-17 13:12:39 +00001593 /* Scan the names of the columns of the table to be indexed and
1594 ** load the column indices into the Index structure. Report an error
1595 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001596 */
1597 for(i=0; i<pList->nId; i++){
1598 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001599 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001600 }
1601 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001602 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001603 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001604 pParse->nErr++;
1605 sqliteFree(pIndex);
1606 goto exit_create_index;
1607 }
drh967e8b72000-06-21 13:59:10 +00001608 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001609 }
1610
1611 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001612 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001613 */
drhd24cc422003-03-27 12:51:24 +00001614 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001615 Index *p;
drhd24cc422003-03-27 12:51:24 +00001616 p = sqliteHashInsert(&db->aDb[isTemp].idxHash,
1617 pIndex->zName, strlen(zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001618 if( p ){
1619 assert( p==pIndex ); /* Malloc must have failed */
1620 sqliteFree(pIndex);
1621 goto exit_create_index;
1622 }
drh5e00f6c2001-09-13 13:46:56 +00001623 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001624 }
drh9cfcf5d2002-01-29 18:41:24 +00001625
1626 /* When adding an index to the list of indices for a table, make
1627 ** sure all indices labeled OE_Replace come after all those labeled
1628 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1629 ** and INSERT.
1630 */
1631 if( onError!=OE_Replace || pTab->pIndex==0
1632 || pTab->pIndex->onError==OE_Replace){
1633 pIndex->pNext = pTab->pIndex;
1634 pTab->pIndex = pIndex;
1635 }else{
1636 Index *pOther = pTab->pIndex;
1637 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1638 pOther = pOther->pNext;
1639 }
1640 pIndex->pNext = pOther->pNext;
1641 pOther->pNext = pIndex;
1642 }
drh75897232000-05-29 14:26:00 +00001643
drhd78eeee2001-09-13 16:18:53 +00001644 /* If the initFlag is 1 it means we are reading the SQL off the
1645 ** "sqlite_master" table on the disk. So do not write to the disk
1646 ** again. Extract the table number from the pParse->newTnum field.
1647 */
drhadbca9c2001-09-27 15:11:53 +00001648 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001649 pIndex->tnum = pParse->newTnum;
1650 }
1651
drh75897232000-05-29 14:26:00 +00001652 /* If the initFlag is 0 then create the index on disk. This
1653 ** involves writing the index into the master table and filling in the
1654 ** index with the current table contents.
1655 **
1656 ** The initFlag is 0 when the user first enters a CREATE INDEX
1657 ** command. The initFlag is 1 when a database is opened and
1658 ** CREATE INDEX statements are read out of the master table. In
1659 ** the latter case the index already exists on disk, which is why
1660 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001661 **
1662 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001663 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1664 ** has just been created, it contains no data and the index initialization
1665 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001666 */
drhadbca9c2001-09-27 15:11:53 +00001667 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001668 int n;
drhadbca9c2001-09-27 15:11:53 +00001669 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001670 int lbl1, lbl2;
1671 int i;
drhadbca9c2001-09-27 15:11:53 +00001672 int addr;
drh75897232000-05-29 14:26:00 +00001673
drhd8bc7082000-06-07 23:51:50 +00001674 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001675 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001676 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001677 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001678 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001679 }
drhe0bc4042002-06-25 01:09:11 +00001680 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1681 sqliteVdbeAddOp(v, OP_String, 0, 0);
1682 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1683 sqliteVdbeAddOp(v, OP_String, 0, 0);
1684 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1685 sqliteVdbeAddOp(v, OP_String, 0, 0);
1686 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001687 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1688 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001689 pIndex->tnum = 0;
1690 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001691 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00001692 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
1693 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00001694 }
drhe0bc4042002-06-25 01:09:11 +00001695 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1696 if( pStart && pEnd ){
1697 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1698 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001699 }
drhe0bc4042002-06-25 01:09:11 +00001700 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1701 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001702 if( pTable ){
drhd24cc422003-03-27 12:51:24 +00001703 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00001704 sqliteVdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00001705 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001706 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001707 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1708 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001709 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001710 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001711 }
drh99fcd712001-10-13 01:06:47 +00001712 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001713 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001714 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh483750b2003-01-29 18:46:51 +00001715 sqliteVdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
drh6b563442001-11-07 16:48:26 +00001716 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001717 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001718 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1719 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001720 }
drhadbca9c2001-09-27 15:11:53 +00001721 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001722 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001723 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001724 }
drhe0bc4042002-06-25 01:09:11 +00001725 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001726 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001727 }
drh75897232000-05-29 14:26:00 +00001728 }
1729
drh75897232000-05-29 14:26:00 +00001730 /* Clean up before exiting */
1731exit_create_index:
1732 sqliteIdListDelete(pList);
drhd24cc422003-03-27 12:51:24 +00001733 sqliteSrcListDelete(pTable);
drh75897232000-05-29 14:26:00 +00001734 sqliteFree(zName);
1735 return;
1736}
1737
1738/*
drh74e24cd2002-01-09 03:19:59 +00001739** This routine will drop an existing named index. This routine
1740** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001741*/
drhd24cc422003-03-27 12:51:24 +00001742void sqliteDropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001743 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001744 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001745 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001746
drhdaffd0e2001-04-11 14:28:42 +00001747 if( pParse->nErr || sqlite_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001748 assert( pName->nSrc==1 );
1749 pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001750 if( pIndex==0 ){
drhda93d232003-03-31 02:12:46 +00001751 sqliteErrorMsg(pParse, "no such index: %S", pName, 0);
drhd24cc422003-03-27 12:51:24 +00001752 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001753 }
drh485b39b2002-07-13 03:11:52 +00001754 if( pIndex->autoIndex ){
drhda93d232003-03-31 02:12:46 +00001755 sqliteErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00001756 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00001757 goto exit_drop_index;
1758 }
1759 if( pIndex->iDb>1 ){
drhda93d232003-03-31 02:12:46 +00001760 sqliteErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00001761 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00001762 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001763 }
drhe5f9c642003-01-13 23:27:31 +00001764#ifndef SQLITE_OMIT_AUTHORIZATION
1765 {
1766 int code = SQLITE_DROP_INDEX;
1767 Table *pTab = pIndex->pTable;
drhd24cc422003-03-27 12:51:24 +00001768 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pIndex->iDb), 0) ){
1769 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001770 }
drhd24cc422003-03-27 12:51:24 +00001771 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
drh77ad4e42003-01-14 02:49:27 +00001772 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName) ){
drhd24cc422003-03-27 12:51:24 +00001773 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001774 }
drhed6c8672003-01-12 18:02:16 +00001775 }
drhe5f9c642003-01-13 23:27:31 +00001776#endif
drh75897232000-05-29 14:26:00 +00001777
1778 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001779 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001780 if( v ){
1781 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001782 { OP_Rewind, 0, ADDR(9), 0},
1783 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001784 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001785 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001786 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001787 { OP_Eq, 0, ADDR(8), 0},
1788 { OP_Next, 0, ADDR(3), 0},
1789 { OP_Goto, 0, ADDR(9), 0},
1790 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001791 };
1792 int base;
1793
drhd24cc422003-03-27 12:51:24 +00001794 sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
1795 sqliteOpenMasterTable(v, pIndex->iDb);
drhe0bc4042002-06-25 01:09:11 +00001796 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1797 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001798 if( pIndex->iDb==0 ){
drhe0bc4042002-06-25 01:09:11 +00001799 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001800 }
drhe0bc4042002-06-25 01:09:11 +00001801 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhd24cc422003-03-27 12:51:24 +00001802 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh1c928532002-01-31 15:54:21 +00001803 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001804 }
1805
drhe0bc4042002-06-25 01:09:11 +00001806 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001807 */
1808 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001809 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001810 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001811 }
drhd24cc422003-03-27 12:51:24 +00001812
1813exit_drop_index:
1814 sqliteSrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001815}
1816
1817/*
drh75897232000-05-29 14:26:00 +00001818** Append a new element to the given IdList. Create a new IdList if
1819** need be.
drhdaffd0e2001-04-11 14:28:42 +00001820**
1821** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001822*/
1823IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1824 if( pList==0 ){
1825 pList = sqliteMalloc( sizeof(IdList) );
1826 if( pList==0 ) return 0;
1827 }
1828 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001829 struct IdList_item *a;
1830 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1831 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001832 sqliteIdListDelete(pList);
1833 return 0;
drh75897232000-05-29 14:26:00 +00001834 }
drh6d4abfb2001-10-22 02:58:08 +00001835 pList->a = a;
drh75897232000-05-29 14:26:00 +00001836 }
1837 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1838 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001839 char **pz = &pList->a[pList->nId].zName;
1840 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1841 if( *pz==0 ){
1842 sqliteIdListDelete(pList);
1843 return 0;
1844 }else{
1845 sqliteDequote(*pz);
1846 }
drh75897232000-05-29 14:26:00 +00001847 }
1848 pList->nId++;
1849 return pList;
1850}
1851
1852/*
drhad3cab52002-05-24 02:04:32 +00001853** Append a new table name to the given SrcList. Create a new SrcList if
1854** need be. A new entry is created in the SrcList even if pToken is NULL.
1855**
1856** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00001857**
1858** If pDatabase is not null, it means that the table has an optional
1859** database name prefix. Like this: "database.table". The pDatabase
1860** points to the table name and the pTable points to the database name.
1861** The SrcList.a[].zName field is filled with the table name which might
1862** come from pTable (if pDatabase is NULL) or from pDatabase.
1863** SrcList.a[].zDatabase is filled with the database name from pTable,
1864** or with NULL if no database is specified.
1865**
1866** In other words, if call like this:
1867**
1868** sqliteSrcListAppend(A,B,0);
1869**
1870** Then B is a table name and the database name is unspecified. If called
1871** like this:
1872**
1873** sqliteSrcListAppend(A,B,C);
1874**
1875** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00001876*/
drh113088e2003-03-20 01:16:58 +00001877SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00001878 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00001879 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00001880 if( pList==0 ) return 0;
1881 }
drh113088e2003-03-20 01:16:58 +00001882 if( (pList->nSrc & 7)==1 ){
1883 SrcList *pNew;
1884 pNew = sqliteRealloc(pList,
1885 sizeof(*pList) + (pList->nSrc+8)*sizeof(pList->a[0]) );
1886 if( pNew==0 ){
drhad3cab52002-05-24 02:04:32 +00001887 sqliteSrcListDelete(pList);
1888 return 0;
1889 }
drh113088e2003-03-20 01:16:58 +00001890 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00001891 }
1892 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00001893 if( pDatabase && pDatabase->z==0 ){
1894 pDatabase = 0;
1895 }
1896 if( pDatabase && pTable ){
1897 Token *pTemp = pDatabase;
1898 pDatabase = pTable;
1899 pTable = pTemp;
1900 }
1901 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00001902 char **pz = &pList->a[pList->nSrc].zName;
drh113088e2003-03-20 01:16:58 +00001903 sqliteSetNString(pz, pTable->z, pTable->n, 0);
1904 if( *pz==0 ){
1905 sqliteSrcListDelete(pList);
1906 return 0;
1907 }else{
1908 sqliteDequote(*pz);
1909 }
1910 }
1911 if( pDatabase ){
1912 char **pz = &pList->a[pList->nSrc].zDatabase;
1913 sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00001914 if( *pz==0 ){
1915 sqliteSrcListDelete(pList);
1916 return 0;
1917 }else{
1918 sqliteDequote(*pz);
1919 }
1920 }
1921 pList->nSrc++;
1922 return pList;
1923}
1924
1925/*
drh75897232000-05-29 14:26:00 +00001926** Add an alias to the last identifier on the given identifier list.
1927*/
drhad3cab52002-05-24 02:04:32 +00001928void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1929 if( pList && pList->nSrc>0 ){
1930 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001931 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001932 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001933 }
1934}
1935
1936/*
drhad3cab52002-05-24 02:04:32 +00001937** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001938*/
1939void sqliteIdListDelete(IdList *pList){
1940 int i;
1941 if( pList==0 ) return;
1942 for(i=0; i<pList->nId; i++){
1943 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001944 }
1945 sqliteFree(pList->a);
1946 sqliteFree(pList);
1947}
1948
1949/*
drhad2d8302002-05-24 20:31:36 +00001950** Return the index in pList of the identifier named zId. Return -1
1951** if not found.
1952*/
1953int sqliteIdListIndex(IdList *pList, const char *zName){
1954 int i;
1955 if( pList==0 ) return -1;
1956 for(i=0; i<pList->nId; i++){
1957 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1958 }
1959 return -1;
1960}
1961
1962/*
drhad3cab52002-05-24 02:04:32 +00001963** Delete an entire SrcList including all its substructure.
1964*/
1965void sqliteSrcListDelete(SrcList *pList){
1966 int i;
1967 if( pList==0 ) return;
1968 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00001969 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00001970 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001971 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001972 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001973 sqliteDeleteTable(0, pList->a[i].pTab);
1974 }
drhff78bd22002-02-27 01:47:11 +00001975 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001976 sqliteExprDelete(pList->a[i].pOn);
1977 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001978 }
drh75897232000-05-29 14:26:00 +00001979 sqliteFree(pList);
1980}
1981
drh982cef72000-05-30 16:27:03 +00001982/*
drhc4a3c772001-04-04 11:48:57 +00001983** Begin a transaction
1984*/
drh1c928532002-01-31 15:54:21 +00001985void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001986 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001987
drh001bbcb2003-03-19 03:14:00 +00001988 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001989 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00001990 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00001991 if( db->flags & SQLITE_InTrans ){
drhda93d232003-03-31 02:12:46 +00001992 sqliteErrorMsg(pParse, "cannot start a transaction within a transaction");
drh6b8b8742002-08-18 20:28:06 +00001993 return;
1994 }
drhcabb0812002-09-14 13:47:32 +00001995 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00001996 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001997 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001998}
1999
2000/*
2001** Commit a transaction
2002*/
2003void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002004 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002005
drh001bbcb2003-03-19 03:14:00 +00002006 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002007 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002008 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002009 if( (db->flags & SQLITE_InTrans)==0 ){
drhda93d232003-03-31 02:12:46 +00002010 sqliteErrorMsg(pParse, "cannot commit - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002011 return;
2012 }
drh5e00f6c2001-09-13 13:46:56 +00002013 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002014 sqliteEndWriteOperation(pParse);
2015 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002016}
2017
2018/*
2019** Rollback a transaction
2020*/
2021void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002022 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002023 Vdbe *v;
2024
drh001bbcb2003-03-19 03:14:00 +00002025 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002026 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002027 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002028 if( (db->flags & SQLITE_InTrans)==0 ){
drhda93d232003-03-31 02:12:46 +00002029 sqliteErrorMsg(pParse, "cannot rollback - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002030 return;
2031 }
drh5e00f6c2001-09-13 13:46:56 +00002032 v = sqliteGetVdbe(pParse);
2033 if( v ){
drh99fcd712001-10-13 01:06:47 +00002034 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002035 }
drh5e00f6c2001-09-13 13:46:56 +00002036 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002037 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002038}
drhf57b14a2001-09-14 18:54:08 +00002039
2040/*
drh001bbcb2003-03-19 03:14:00 +00002041** Generate VDBE code that will verify the schema cookie for all
2042** named database files.
2043*/
2044void sqliteCodeVerifySchema(Parse *pParse){
2045 int i;
2046 sqlite *db = pParse->db;
2047 Vdbe *v = sqliteGetVdbe(pParse);
2048 for(i=0; i<db->nDb; i++){
drh113088e2003-03-20 01:16:58 +00002049 if( i==1 || db->aDb[i].pBt==0 ) continue;
drh1c2d8412003-03-31 00:30:47 +00002050 sqliteVdbeAddOp(v, OP_VerifyCookie, i, db->aDb[i].schema_cookie);
drh001bbcb2003-03-19 03:14:00 +00002051 }
2052 pParse->schemaVerified = 1;
2053}
2054
2055/*
drh1c928532002-01-31 15:54:21 +00002056** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002057** might change the database.
2058**
2059** This routine starts a new transaction if we are not already within
2060** a transaction. If we are already within a transaction, then a checkpoint
2061** is set if the setCheckpoint parameter is true. A checkpoint should
2062** be set for operations that might fail (due to a constraint) part of
2063** the way through and which will need to undo some writes without having to
2064** rollback the whole transaction. For operations where all constraints
2065** can be checked before any changes are made to the database, it is never
2066** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002067**
2068** The tempOnly flag indicates that only temporary tables will be changed
2069** during this write operation. The primary database table is not
2070** write-locked. Only the temporary database file gets a write lock.
2071** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00002072*/
drhcabb0812002-09-14 13:47:32 +00002073void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00002074 Vdbe *v;
2075 v = sqliteGetVdbe(pParse);
2076 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00002077 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00002078 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh001bbcb2003-03-19 03:14:00 +00002079 sqliteVdbeAddOp(v, OP_Transaction, 1, 0);
drhcabb0812002-09-14 13:47:32 +00002080 if( !tempOnly ){
drh001bbcb2003-03-19 03:14:00 +00002081 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
2082 sqliteCodeVerifySchema(pParse);
drhcabb0812002-09-14 13:47:32 +00002083 }
drhc977f7f2002-05-21 11:38:11 +00002084 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00002085 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00002086 sqliteVdbeAddOp(v, OP_Checkpoint, 1, 0);
drh663fc632002-02-02 18:49:19 +00002087 }
2088}
2089
2090/*
drh1c928532002-01-31 15:54:21 +00002091** Generate code that concludes an operation that may have changed
2092** the database. This is a companion function to BeginWriteOperation().
2093** If a transaction was started, then commit it. If a checkpoint was
2094** started then commit that.
2095*/
2096void sqliteEndWriteOperation(Parse *pParse){
2097 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002098 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002099 v = sqliteGetVdbe(pParse);
2100 if( v==0 ) return;
2101 if( pParse->db->flags & SQLITE_InTrans ){
2102 /* Do Nothing */
2103 }else{
2104 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2105 }
2106}