blob: d367e402134f317499c4b541c5b0a66f8180a3c7 [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**
drh701a0ae2004-02-22 20:05:00 +000026** $Id: build.c,v 1.174 2004/02/22 20:05:01 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;
drh8bf8dc92003-05-17 17:35:10 +000039 int i;
drhe0bc4042002-06-25 01:09:11 +000040 pParse->explain = explainFlag;
drh1d85d932004-02-14 23:05:52 +000041 if((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){
drhe0bc4042002-06-25 01:09:11 +000042 int rc = sqliteInit(db, &pParse->zErrMsg);
43 if( rc!=SQLITE_OK ){
44 pParse->rc = rc;
45 pParse->nErr++;
46 }
47 }
drh8bf8dc92003-05-17 17:35:10 +000048 for(i=0; i<db->nDb; i++){
49 DbClearProperty(db, i, DB_Locked);
50 if( !db->aDb[i].inTrans ){
51 DbClearProperty(db, i, DB_Cookie);
52 }
53 }
drh7c972de2003-09-06 22:18:07 +000054 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000055}
56
57/*
drh75897232000-05-29 14:26:00 +000058** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000059** parsed and we want to execute the VDBE code to implement
60** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000061** constructed VDBE code to do the work of the SQL statement.
62** This routine just has to execute the VDBE code.
63**
64** Note that if an error occurred, it might be the case that
65** no VDBE code was generated.
66*/
67void sqliteExec(Parse *pParse){
drhbe0072d2001-09-13 14:46:09 +000068 sqlite *db = pParse->db;
drhb86ccfb2003-01-28 23:13:10 +000069 Vdbe *v = pParse->pVdbe;
drhb86ccfb2003-01-28 23:13:10 +000070
drh826fb5a2004-02-14 23:59:57 +000071 if( v==0 && (v = sqliteGetVdbe(pParse))!=0 ){
72 sqliteVdbeAddOp(v, OP_Halt, 0, 0);
drh50350a12004-02-13 16:22:22 +000073 }
drh826fb5a2004-02-14 23:59:57 +000074 if( sqlite_malloc_failed ) return;
drhb86ccfb2003-01-28 23:13:10 +000075 if( v && pParse->nErr==0 ){
76 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
77 sqliteVdbeTrace(v, trace);
drh826fb5a2004-02-14 23:59:57 +000078 sqliteVdbeMakeReady(v, pParse->nVar, pParse->explain);
79 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +000080 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +000081 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +000082 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +000083 }
drha226d052002-09-25 19:04:07 +000084 pParse->nTab = 0;
85 pParse->nMem = 0;
86 pParse->nSet = 0;
87 pParse->nAgg = 0;
drh7c972de2003-09-06 22:18:07 +000088 pParse->nVar = 0;
drh75897232000-05-29 14:26:00 +000089}
90
91/*
drhf57b3392001-10-08 13:22:32 +000092** Locate the in-memory structure that describes
93** a particular database table given the name
drha69d9162003-04-17 22:57:53 +000094** of that table and (optionally) the name of the database
95** containing the table. Return NULL if not found.
96**
drhf26e09c2003-05-31 16:21:12 +000097** If zDatabase is 0, all databases are searched for the
98** table and the first matching table is returned. (No checking
99** for duplicate table names is done.) The search order is
100** TEMP first, then MAIN, then any auxiliary databases added
101** using the ATTACH command.
102**
drha69d9162003-04-17 22:57:53 +0000103** See also sqliteLocateTable().
drh75897232000-05-29 14:26:00 +0000104*/
drhd24cc422003-03-27 12:51:24 +0000105Table *sqliteFindTable(sqlite *db, const char *zName, const char *zDatabase){
106 Table *p = 0;
107 int i;
108 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000109 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
110 if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) ) continue;
111 p = sqliteHashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000112 if( p ) break;
113 }
drh74e24cd2002-01-09 03:19:59 +0000114 return p;
drh75897232000-05-29 14:26:00 +0000115}
116
117/*
drhf57b3392001-10-08 13:22:32 +0000118** Locate the in-memory structure that describes
drha69d9162003-04-17 22:57:53 +0000119** a particular database table given the name
120** of that table and (optionally) the name of the database
121** containing the table. Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000122** Also leave an error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000123**
drhf26e09c2003-05-31 16:21:12 +0000124** The difference between this routine and sqliteFindTable()
125** is that this routine leaves an error message in pParse->zErrMsg
126** where sqliteFindTable() does not.
drha69d9162003-04-17 22:57:53 +0000127*/
128Table *sqliteLocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000129 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000130
131 p = sqliteFindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000132 if( p==0 ){
133 if( zDbase ){
134 sqliteErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
drhf26e09c2003-05-31 16:21:12 +0000135 }else if( sqliteFindTable(pParse->db, zName, 0)!=0 ){
drhf0f258b2003-04-21 18:48:45 +0000136 sqliteErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
drhf26e09c2003-05-31 16:21:12 +0000137 zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000138 }else{
139 sqliteErrorMsg(pParse, "no such table: %s", zName);
140 }
141 }
142 return p;
143}
144
145/*
146** Locate the in-memory structure that describes
147** a particular index given the name of that index
148** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000149** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000150**
151** If zDatabase is 0, all databases are searched for the
152** table and the first matching index is returned. (No checking
153** for duplicate index names is done.) The search order is
154** TEMP first, then MAIN, then any auxiliary databases added
155** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000156*/
drhd24cc422003-03-27 12:51:24 +0000157Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){
158 Index *p = 0;
159 int i;
160 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000161 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
162 if( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) ) continue;
163 p = sqliteHashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000164 if( p ) break;
165 }
drh74e24cd2002-01-09 03:19:59 +0000166 return p;
drh75897232000-05-29 14:26:00 +0000167}
168
169/*
170** Remove the given index from the index hash table, and free
171** its memory structures.
172**
drhd229ca92002-01-09 13:30:41 +0000173** The index is removed from the database hash tables but
174** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000175** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000176*/
drh74e24cd2002-01-09 03:19:59 +0000177static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000178 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000179
drhd229ca92002-01-09 13:30:41 +0000180 assert( db!=0 && p->zName!=0 );
drhd24cc422003-03-27 12:51:24 +0000181 pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
182 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000183 if( pOld!=0 && pOld!=p ){
drhd24cc422003-03-27 12:51:24 +0000184 sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
185 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000186 }
drh74e24cd2002-01-09 03:19:59 +0000187 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000188}
189
190/*
drhbeae3192001-09-22 18:12:08 +0000191** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000192** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000193** structures.
194*/
drh6d4abfb2001-10-22 02:58:08 +0000195void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000196 if( pIndex->pTable->pIndex==pIndex ){
197 pIndex->pTable->pIndex = pIndex->pNext;
198 }else{
199 Index *p;
200 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
201 if( p && p->pNext==pIndex ){
202 p->pNext = pIndex->pNext;
203 }
204 }
205 sqliteDeleteIndex(db, pIndex);
206}
207
208/*
drhe0bc4042002-06-25 01:09:11 +0000209** Erase all schema information from the in-memory hash tables of
210** database connection. This routine is called to reclaim memory
211** before the connection closes. It is also called during a rollback
212** if there were schema changes during the transaction.
drh1c2d8412003-03-31 00:30:47 +0000213**
214** If iDb<=0 then reset the internal schema tables for all database
215** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000216** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000217*/
drh1c2d8412003-03-31 00:30:47 +0000218void sqliteResetInternalSchema(sqlite *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000219 HashElem *pElem;
220 Hash temp1;
221 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000222 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000223
drh1c2d8412003-03-31 00:30:47 +0000224 assert( iDb>=0 && iDb<db->nDb );
225 db->flags &= ~SQLITE_Initialized;
226 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000227 Db *pDb = &db->aDb[i];
228 temp1 = pDb->tblHash;
229 temp2 = pDb->trigHash;
230 sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
231 sqliteHashClear(&pDb->aFKey);
232 sqliteHashClear(&pDb->idxHash);
233 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
234 Trigger *pTrigger = sqliteHashData(pElem);
235 sqliteDeleteTrigger(pTrigger);
236 }
237 sqliteHashClear(&temp2);
238 sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
239 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
240 Table *pTab = sqliteHashData(pElem);
241 sqliteDeleteTable(db, pTab);
242 }
243 sqliteHashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000244 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000245 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000246 }
drh1c2d8412003-03-31 00:30:47 +0000247 assert( iDb==0 );
248 db->flags &= ~SQLITE_InternChanges;
249
250 /* If one or more of the auxiliary database files has been closed,
251 ** then remove then from the auxiliary database list. We take the
252 ** opportunity to do this here since we have just deleted all of the
253 ** schema hash tables and therefore do not have to make any changes
254 ** to any of those tables.
255 */
drh4d189ca2004-02-12 18:46:38 +0000256 for(i=0; i<db->nDb; i++){
257 struct Db *pDb = &db->aDb[i];
258 if( pDb->pBt==0 ){
259 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
260 pDb->pAux = 0;
261 }
262 }
drh1c2d8412003-03-31 00:30:47 +0000263 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000264 struct Db *pDb = &db->aDb[i];
265 if( pDb->pBt==0 ){
266 sqliteFree(pDb->zName);
267 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000268 continue;
269 }
270 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000271 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000272 }
drh8bf8dc92003-05-17 17:35:10 +0000273 j++;
drh1c2d8412003-03-31 00:30:47 +0000274 }
275 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
276 db->nDb = j;
277 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
278 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
279 sqliteFree(db->aDb);
280 db->aDb = db->aDbStatic;
281 }
drhe0bc4042002-06-25 01:09:11 +0000282}
283
284/*
285** This routine is called whenever a rollback occurs. If there were
286** schema changes during the transaction, then we have to reset the
287** internal hash tables and reload them from disk.
288*/
289void sqliteRollbackInternalChanges(sqlite *db){
290 if( db->flags & SQLITE_InternChanges ){
drh1c2d8412003-03-31 00:30:47 +0000291 sqliteResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000292 }
293}
294
295/*
296** This routine is called when a commit occurs.
297*/
298void sqliteCommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000299 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000300 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000301}
302
303/*
drh75897232000-05-29 14:26:00 +0000304** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000305** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000306**
307** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000308** the table data structure from the hash table. Nor does it remove
309** foreign keys from the sqlite.aFKey hash table. But it does destroy
310** memory structures of the indices and foreign keys associated with
311** the table.
drhdaffd0e2001-04-11 14:28:42 +0000312**
313** Indices associated with the table are unlinked from the "db"
314** data structure if db!=NULL. If db==NULL, indices attached to
315** the table are deleted, but it is assumed they have already been
316** unlinked.
drh75897232000-05-29 14:26:00 +0000317*/
318void sqliteDeleteTable(sqlite *db, Table *pTable){
319 int i;
320 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000321 FKey *pFKey, *pNextFKey;
322
drh75897232000-05-29 14:26:00 +0000323 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000324
325 /* Delete all indices associated with this table
326 */
327 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
328 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000329 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000330 sqliteDeleteIndex(db, pIndex);
331 }
332
333 /* Delete all foreign keys associated with this table. The keys
334 ** should have already been unlinked from the db->aFKey hash table
335 */
336 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
337 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000338 assert( pTable->iDb<db->nDb );
339 assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
340 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000341 sqliteFree(pFKey);
342 }
343
344 /* Delete the Table structure itself.
345 */
drh75897232000-05-29 14:26:00 +0000346 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000347 sqliteFree(pTable->aCol[i].zName);
348 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000349 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000350 }
drh6e142f52000-06-08 13:36:40 +0000351 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000352 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000353 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000354 sqliteFree(pTable);
355}
356
357/*
drh5edc3122001-09-13 21:53:09 +0000358** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000359** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000360*/
drh74e24cd2002-01-09 03:19:59 +0000361static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000362 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000363 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000364 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000365 assert( db!=0 );
drhd24cc422003-03-27 12:51:24 +0000366 pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000367 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000368 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
369 int nTo = strlen(pF1->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000370 pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000371 if( pF2==pF1 ){
drhd24cc422003-03-27 12:51:24 +0000372 sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000373 }else{
374 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
375 if( pF2 ){
376 pF2->pNextTo = pF1->pNextTo;
377 }
378 }
379 }
drh74e24cd2002-01-09 03:19:59 +0000380 sqliteDeleteTable(db, p);
381}
382
383/*
drh1ccde152000-06-17 13:12:39 +0000384** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000385**
386** Space to hold the name is obtained from sqliteMalloc() and must
387** be freed by the calling function.
388*/
drhcce7d172000-05-31 15:34:51 +0000389char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000390 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000391 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000392 return zName;
393}
394
395/*
drhe0bc4042002-06-25 01:09:11 +0000396** Generate code to open the appropriate master table. The table
397** opened will be SQLITE_MASTER for persistent tables and
398** SQLITE_TEMP_MASTER for temporary tables. The table is opened
399** on cursor 0.
400*/
401void sqliteOpenMasterTable(Vdbe *v, int isTemp){
drh001bbcb2003-03-19 03:14:00 +0000402 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
403 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
drhe0bc4042002-06-25 01:09:11 +0000404}
405
406/*
drh75897232000-05-29 14:26:00 +0000407** Begin constructing a new table representation in memory. This is
408** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000409** to a CREATE TABLE statement. In particular, this routine is called
410** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000411** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000412** flag is true if the table should be stored in the auxiliary database
413** file instead of in the main database file. This is normally the case
414** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000415** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000416**
drhf57b3392001-10-08 13:22:32 +0000417** The new table record is initialized and put in pParse->pNewTable.
418** As more of the CREATE TABLE statement is parsed, additional action
419** routines will be called to add more information to this record.
420** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
421** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000422*/
drhe5f9c642003-01-13 23:27:31 +0000423void sqliteStartTable(
424 Parse *pParse, /* Parser context */
425 Token *pStart, /* The "CREATE" token */
426 Token *pName, /* Name of table or view to create */
427 int isTemp, /* True if this is a TEMP table */
428 int isView /* True if this is a VIEW */
429){
drh75897232000-05-29 14:26:00 +0000430 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000431 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000432 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000433 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000434 Vdbe *v;
drh1c2d8412003-03-31 00:30:47 +0000435 int iDb;
drh75897232000-05-29 14:26:00 +0000436
437 pParse->sFirstToken = *pStart;
438 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000439 if( zName==0 ) return;
drh1d85d932004-02-14 23:05:52 +0000440 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000441#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000442 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000443 {
444 int code;
drhe22a3342003-04-22 20:30:37 +0000445 char *zDb = isTemp ? "temp" : "main";
446 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
447 sqliteFree(zName);
448 return;
449 }
drhe5f9c642003-01-13 23:27:31 +0000450 if( isView ){
451 if( isTemp ){
452 code = SQLITE_CREATE_TEMP_VIEW;
453 }else{
454 code = SQLITE_CREATE_VIEW;
455 }
456 }else{
457 if( isTemp ){
458 code = SQLITE_CREATE_TEMP_TABLE;
459 }else{
460 code = SQLITE_CREATE_TABLE;
461 }
462 }
drhe22a3342003-04-22 20:30:37 +0000463 if( sqliteAuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000464 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000465 return;
466 }
467 }
468#endif
469
drhf57b3392001-10-08 13:22:32 +0000470
471 /* Before trying to create a temporary table, make sure the Btree for
472 ** holding temporary tables is open.
473 */
drhd24cc422003-03-27 12:51:24 +0000474 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
drh13bff812003-04-15 01:19:47 +0000475 int rc = sqliteBtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000476 if( rc!=SQLITE_OK ){
drhf7a9e1a2004-02-22 18:40:56 +0000477 sqliteErrorMsg(pParse, "unable to open a temporary database "
478 "file for storing temporary tables");
drhf57b3392001-10-08 13:22:32 +0000479 pParse->nErr++;
480 return;
481 }
482 if( db->flags & SQLITE_InTrans ){
drh001bbcb2003-03-19 03:14:00 +0000483 rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000484 if( rc!=SQLITE_OK ){
drhf7a9e1a2004-02-22 18:40:56 +0000485 sqliteErrorMsg(pParse, "unable to get a write lock on "
486 "the temporary database file");
drhf57b3392001-10-08 13:22:32 +0000487 pParse->nErr++;
488 return;
489 }
490 }
491 }
492
493 /* Make sure the new table name does not collide with an existing
494 ** index or table name. Issue an error message if it does.
495 **
496 ** If we are re-reading the sqlite_master table because of a schema
497 ** change and a new permanent table is found whose name collides with
drhd24cc422003-03-27 12:51:24 +0000498 ** an existing temporary table, that is not an error.
drhf57b3392001-10-08 13:22:32 +0000499 */
drhd24cc422003-03-27 12:51:24 +0000500 pTable = sqliteFindTable(db, zName, 0);
drh1d85d932004-02-14 23:05:52 +0000501 iDb = isTemp ? 1 : db->init.iDb;
502 if( pTable!=0 && (pTable->iDb==iDb || !db->init.busy) ){
drhf7a9e1a2004-02-22 18:40:56 +0000503 sqliteErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000504 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000505 return;
drh75897232000-05-29 14:26:00 +0000506 }
drhd24cc422003-03-27 12:51:24 +0000507 if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
drh1d85d932004-02-14 23:05:52 +0000508 (pIdx->iDb==0 || !db->init.busy) ){
drhf7a9e1a2004-02-22 18:40:56 +0000509 sqliteErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000510 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000511 return;
512 }
513 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000514 if( pTable==0 ){
515 sqliteFree(zName);
516 return;
517 }
drh75897232000-05-29 14:26:00 +0000518 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000519 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000520 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000521 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000522 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000523 pTable->iDb = iDb;
drhbe0072d2001-09-13 14:46:09 +0000524 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000525 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000526
527 /* Begin generating the code that will insert the table record into
528 ** the SQLITE_MASTER table. Note in particular that we must go ahead
529 ** and allocate the record number for the table entry now. Before any
530 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
531 ** indices to be created and the table record must come before the
532 ** indices. Hence, the record number for the table must be allocated
533 ** now.
534 */
drh1d85d932004-02-14 23:05:52 +0000535 if( !db->init.busy && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000536 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000537 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000538 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
539 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000540 }
drhe0bc4042002-06-25 01:09:11 +0000541 sqliteOpenMasterTable(v, isTemp);
542 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
543 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
544 sqliteVdbeAddOp(v, OP_String, 0, 0);
545 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000546 }
drh75897232000-05-29 14:26:00 +0000547}
548
549/*
550** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000551**
552** The parser calls this routine once for each column declaration
553** in a CREATE TABLE statement. sqliteStartTable() gets called
554** first to get things going. Then this routine is called for each
555** column.
drh75897232000-05-29 14:26:00 +0000556*/
557void sqliteAddColumn(Parse *pParse, Token *pName){
558 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000559 int i;
560 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000561 Column *pCol;
drh75897232000-05-29 14:26:00 +0000562 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000563 sqliteSetNString(&z, pName->z, pName->n, 0);
564 if( z==0 ) return;
565 sqliteDequote(z);
566 for(i=0; i<p->nCol; i++){
567 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
drhf7a9e1a2004-02-22 18:40:56 +0000568 sqliteErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000569 sqliteFree(z);
570 return;
571 }
572 }
drh75897232000-05-29 14:26:00 +0000573 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000574 Column *aNew;
575 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
576 if( aNew==0 ) return;
577 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000578 }
drhc9b84a12002-06-20 11:36:48 +0000579 pCol = &p->aCol[p->nCol];
580 memset(pCol, 0, sizeof(p->aCol[0]));
581 pCol->zName = z;
582 pCol->sortOrder = SQLITE_SO_NUM;
583 p->nCol++;
drh75897232000-05-29 14:26:00 +0000584}
585
586/*
drh382c0242001-10-06 16:33:02 +0000587** This routine is called by the parser while in the middle of
588** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
589** been seen on a column. This routine sets the notNull flag on
590** the column currently under construction.
591*/
drh9cfcf5d2002-01-29 18:41:24 +0000592void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000593 Table *p;
594 int i;
595 if( (p = pParse->pNewTable)==0 ) return;
596 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000597 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000598}
599
600/*
601** This routine is called by the parser while in the middle of
602** parsing a CREATE TABLE statement. The pFirst token is the first
603** token in the sequence of tokens that describe the type of the
604** column currently under construction. pLast is the last token
605** in the sequence. Use this information to construct a string
606** that contains the typename of the column and store that string
607** in zType.
608*/
609void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
610 Table *p;
611 int i, j;
612 int n;
613 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000614 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000615 if( (p = pParse->pNewTable)==0 ) return;
616 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000617 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000618 pCol = &p->aCol[i];
619 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000620 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000621 sqliteSetNString(pz, pFirst->z, n, 0);
622 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000623 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000624 for(i=j=0; z[i]; i++){
625 int c = z[i];
626 if( isspace(c) ) continue;
627 z[j++] = c;
628 }
629 z[j] = 0;
drh3d037a92002-08-15 01:26:09 +0000630 if( pParse->db->file_format>=4 ){
drhfcb78a42003-01-18 20:11:05 +0000631 pCol->sortOrder = sqliteCollateType(z, n);
632 }else{
633 pCol->sortOrder = SQLITE_SO_NUM;
drhc9b84a12002-06-20 11:36:48 +0000634 }
drh382c0242001-10-06 16:33:02 +0000635}
636
637/*
drh7020f652000-06-03 18:06:52 +0000638** The given token is the default value for the last column added to
639** the table currently under construction. If "minusFlag" is true, it
640** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000641**
642** This routine is called by the parser while in the middle of
643** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000644*/
645void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
646 Table *p;
647 int i;
648 char **pz;
649 if( (p = pParse->pNewTable)==0 ) return;
650 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000651 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000652 pz = &p->aCol[i].zDflt;
653 if( minusFlag ){
654 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
655 }else{
656 sqliteSetNString(pz, pVal->z, pVal->n, 0);
657 }
658 sqliteDequote(*pz);
659}
660
661/*
drh4a324312001-12-21 14:30:42 +0000662** Designate the PRIMARY KEY for the table. pList is a list of names
663** of columns that form the primary key. If pList is NULL, then the
664** most recently added column of the table is the primary key.
665**
666** A table can have at most one primary key. If the table already has
667** a primary key (and this is the second primary key) then create an
668** error.
669**
670** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
671** then we will try to use that column as the row id. (Exception:
672** For backwards compatibility with older databases, do not do this
673** if the file format version number is less than 1.) Set the Table.iPKey
674** field of the table under construction to be the index of the
675** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
676** no INTEGER PRIMARY KEY.
677**
678** If the key is not an INTEGER PRIMARY KEY, then create a unique
679** index for the key. No index is created for INTEGER PRIMARY KEYs.
680*/
drh9cfcf5d2002-01-29 18:41:24 +0000681void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000682 Table *pTab = pParse->pNewTable;
683 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000684 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000685 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000686 if( pTab->hasPrimKey ){
drhf7a9e1a2004-02-22 18:40:56 +0000687 sqliteErrorMsg(pParse,
688 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000689 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000690 }
691 pTab->hasPrimKey = 1;
692 if( pList==0 ){
693 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000694 pTab->aCol[iCol].isPrimKey = 1;
695 }else{
696 for(i=0; i<pList->nId; i++){
697 for(iCol=0; iCol<pTab->nCol; iCol++){
drh3ca65152003-08-24 16:38:17 +0000698 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ) break;
drh78100cc2003-08-23 22:40:53 +0000699 }
700 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000701 }
drh78100cc2003-08-23 22:40:53 +0000702 if( pList->nId>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000703 }
704 if( iCol>=0 && iCol<pTab->nCol ){
705 zType = pTab->aCol[iCol].zType;
706 }
707 if( pParse->db->file_format>=1 &&
708 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
709 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000710 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000711 }else{
drh4925ca02003-11-27 00:48:57 +0000712 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000713 pList = 0;
drh4a324312001-12-21 14:30:42 +0000714 }
drhe0194f22003-02-26 13:52:51 +0000715
716primary_key_exit:
717 sqliteIdListDelete(pList);
718 return;
drh4a324312001-12-21 14:30:42 +0000719}
720
721/*
drhfcb78a42003-01-18 20:11:05 +0000722** Return the appropriate collating type given a type name.
723**
724** The collation type is text (SQLITE_SO_TEXT) if the type
725** name contains the character stream "text" or "blob" or
726** "clob". Any other type name is collated as numeric
727** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000728*/
drhfcb78a42003-01-18 20:11:05 +0000729int sqliteCollateType(const char *zType, int nType){
730 int i;
drhd3834012004-02-22 18:56:49 +0000731 for(i=0; i<nType-3; i++){
732 int c = *(zType++) | 0x60;
733 if( (c=='b' || c=='c') && sqliteStrNICmp(zType, "lob", 3)==0 ){
734 return SQLITE_SO_TEXT;
735 }
736 if( c=='c' && sqliteStrNICmp(zType, "har", 3)==0 ){
737 return SQLITE_SO_TEXT;
738 }
739 if( c=='t' && sqliteStrNICmp(zType, "ext", 3)==0 ){
740 return SQLITE_SO_TEXT;
drhfcb78a42003-01-18 20:11:05 +0000741 }
drh8e2ca022002-06-17 17:07:19 +0000742 }
drhfcb78a42003-01-18 20:11:05 +0000743 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000744}
745
746/*
747** This routine is called by the parser while in the middle of
748** parsing a CREATE TABLE statement. A "COLLATE" clause has
749** been seen on a column. This routine sets the Column.sortOrder on
750** the column currently under construction.
751*/
752void sqliteAddCollateType(Parse *pParse, int collType){
753 Table *p;
754 int i;
755 if( (p = pParse->pNewTable)==0 ) return;
756 i = p->nCol-1;
757 if( i>=0 ) p->aCol[i].sortOrder = collType;
758}
759
760/*
drh50e5dad2001-09-15 00:57:28 +0000761** Come up with a new random value for the schema cookie. Make sure
762** the new value is different from the old.
763**
764** The schema cookie is used to determine when the schema for the
765** database changes. After each schema change, the cookie value
766** changes. When a process first reads the schema it records the
767** cookie. Thereafter, whenever it goes to access the database,
768** it checks the cookie to make sure the schema has not changed
769** since it was last read.
770**
771** This plan is not completely bullet-proof. It is possible for
772** the schema to change multiple times and for the cookie to be
773** set back to prior value. But schema changes are infrequent
774** and the probability of hitting the same cookie value is only
775** 1 chance in 2^32. So we're safe enough.
776*/
drhe0bc4042002-06-25 01:09:11 +0000777void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh001bbcb2003-03-19 03:14:00 +0000778 if( db->next_cookie==db->aDb[0].schema_cookie ){
drhbbd82df2004-02-11 09:46:30 +0000779 unsigned char r;
780 sqliteRandomness(1, &r);
781 db->next_cookie = db->aDb[0].schema_cookie + r + 1;
drh50e5dad2001-09-15 00:57:28 +0000782 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000783 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
784 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000785 }
786}
787
788/*
drh969fa7c2002-02-18 18:30:32 +0000789** Measure the number of characters needed to output the given
790** identifier. The number returned includes any quotes used
791** but does not include the null terminator.
792*/
793static int identLength(const char *z){
794 int n;
drh17f71932002-02-21 12:01:27 +0000795 int needQuote = 0;
796 for(n=0; *z; n++, z++){
797 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000798 }
drh17f71932002-02-21 12:01:27 +0000799 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000800}
801
802/*
803** Write an identifier onto the end of the given string. Add
804** quote characters as needed.
805*/
806static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000807 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000808 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000809 for(j=0; zIdent[j]; j++){
810 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
811 }
812 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
813 || sqliteKeywordCode(zIdent, j)!=TK_ID;
814 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000815 for(j=0; zIdent[j]; j++){
816 z[i++] = zIdent[j];
817 if( zIdent[j]=='\'' ) z[i++] = '\'';
818 }
drh17f71932002-02-21 12:01:27 +0000819 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000820 z[i] = 0;
821 *pIdx = i;
822}
823
824/*
825** Generate a CREATE TABLE statement appropriate for the given
826** table. Memory to hold the text of the statement is obtained
827** from sqliteMalloc() and must be freed by the calling function.
828*/
829static char *createTableStmt(Table *p){
830 int i, k, n;
831 char *zStmt;
832 char *zSep, *zSep2, *zEnd;
833 n = 0;
834 for(i=0; i<p->nCol; i++){
835 n += identLength(p->aCol[i].zName);
836 }
837 n += identLength(p->zName);
838 if( n<40 ){
839 zSep = "";
840 zSep2 = ",";
841 zEnd = ")";
842 }else{
843 zSep = "\n ";
844 zSep2 = ",\n ";
845 zEnd = "\n)";
846 }
drhe0bc4042002-06-25 01:09:11 +0000847 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000848 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000849 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +0000850 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000851 k = strlen(zStmt);
852 identPut(zStmt, &k, p->zName);
853 zStmt[k++] = '(';
854 for(i=0; i<p->nCol; i++){
855 strcpy(&zStmt[k], zSep);
856 k += strlen(&zStmt[k]);
857 zSep = zSep2;
858 identPut(zStmt, &k, p->aCol[i].zName);
859 }
860 strcpy(&zStmt[k], zEnd);
861 return zStmt;
862}
863
864/*
drh75897232000-05-29 14:26:00 +0000865** This routine is called to report the final ")" that terminates
866** a CREATE TABLE statement.
867**
drhf57b3392001-10-08 13:22:32 +0000868** The table structure that other action routines have been building
869** is added to the internal hash tables, assuming no errors have
870** occurred.
drh75897232000-05-29 14:26:00 +0000871**
drh1d85d932004-02-14 23:05:52 +0000872** An entry for the table is made in the master table on disk, unless
873** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +0000874** it means we are reading the sqlite_master table because we just
875** connected to the database or because the sqlite_master table has
876** recently changes, so the entry for this table already exists in
877** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000878**
879** If the pSelect argument is not NULL, it means that this routine
880** was called to create a table generated from a
881** "CREATE TABLE ... AS SELECT ..." statement. The column names of
882** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000883*/
drh969fa7c2002-02-18 18:30:32 +0000884void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000885 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000886 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000887
drh969fa7c2002-02-18 18:30:32 +0000888 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000889 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000890 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000891
drh969fa7c2002-02-18 18:30:32 +0000892 /* If the table is generated from a SELECT, then construct the
893 ** list of columns and the text of the table.
894 */
895 if( pSelect ){
896 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000897 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000898 assert( p->aCol==0 );
899 p->nCol = pSelTab->nCol;
900 p->aCol = pSelTab->aCol;
901 pSelTab->nCol = 0;
902 pSelTab->aCol = 0;
903 sqliteDeleteTable(0, pSelTab);
904 }
905
drh1d85d932004-02-14 23:05:52 +0000906 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000907 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
908 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +0000909 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +0000910 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000911 */
drh1d85d932004-02-14 23:05:52 +0000912 if( db->init.busy ){
913 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +0000914 }
915
drhe3c41372001-09-17 20:25:58 +0000916 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000917 ** in the SQLITE_MASTER table of the database. The record number
918 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000919 **
drhe0bc4042002-06-25 01:09:11 +0000920 ** If this is a TEMPORARY table, write the entry into the auxiliary
921 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000922 */
drh1d85d932004-02-14 23:05:52 +0000923 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +0000924 int n;
drhd8bc7082000-06-07 23:51:50 +0000925 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000926
drhd8bc7082000-06-07 23:51:50 +0000927 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000928 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000929 if( p->pSelect==0 ){
930 /* A regular table */
drh701a0ae2004-02-22 20:05:00 +0000931 sqliteVdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
drh4ff6dfa2002-03-03 23:06:00 +0000932 }else{
933 /* A view */
934 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
935 }
drh969fa7c2002-02-18 18:30:32 +0000936 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000937 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
drh701a0ae2004-02-22 20:05:00 +0000938 sqliteVdbeOp3(v, OP_String, 0, 0, p->pSelect==0?"table":"view", P3_STATIC);
939 sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
940 sqliteVdbeOp3(v, OP_String, 0, 0, p->zName, 0);
drhe0bc4042002-06-25 01:09:11 +0000941 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
942 sqliteVdbeAddOp(v, OP_String, 0, 0);
943 if( pSelect ){
944 char *z = createTableStmt(p);
945 n = z ? strlen(z) : 0;
946 sqliteVdbeChangeP3(v, -1, z, n);
947 sqliteFree(z);
948 }else{
949 assert( pEnd!=0 );
950 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
951 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
952 }
953 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
954 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhd24cc422003-03-27 12:51:24 +0000955 if( !p->iDb ){
drhe0bc4042002-06-25 01:09:11 +0000956 sqliteChangeCookie(db, v);
957 }
958 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000959 if( pSelect ){
drhd24cc422003-03-27 12:51:24 +0000960 sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000961 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +0000962 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000963 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000964 }
drh1c928532002-01-31 15:54:21 +0000965 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000966 }
drh17e9e292003-02-01 13:53:28 +0000967
968 /* Add the table to the in-memory representation of the database.
969 */
drhd24cc422003-03-27 12:51:24 +0000970 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +0000971 Table *pOld;
972 FKey *pFKey;
drhd24cc422003-03-27 12:51:24 +0000973 pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
974 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +0000975 if( pOld ){
976 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
977 return;
978 }
979 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
980 int nTo = strlen(pFKey->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000981 pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
982 sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +0000983 }
984 pParse->pNewTable = 0;
985 db->nTable++;
986 db->flags |= SQLITE_InternChanges;
987 }
drh75897232000-05-29 14:26:00 +0000988}
989
990/*
drha76b5df2002-02-23 02:32:10 +0000991** The parser calls this routine in order to create a new VIEW
992*/
993void sqliteCreateView(
994 Parse *pParse, /* The parsing context */
995 Token *pBegin, /* The CREATE token that begins the statement */
996 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000997 Select *pSelect, /* A SELECT statement that will become the new view */
998 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000999){
drha76b5df2002-02-23 02:32:10 +00001000 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001001 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001002 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001003 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001004 DbFixer sFix;
drha76b5df2002-02-23 02:32:10 +00001005
drhe5f9c642003-01-13 23:27:31 +00001006 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001007 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001008 if( p==0 || pParse->nErr ){
drh417be792002-03-03 18:59:40 +00001009 sqliteSelectDelete(pSelect);
1010 return;
1011 }
drhf26e09c2003-05-31 16:21:12 +00001012 if( sqliteFixInit(&sFix, pParse, p->iDb, "view", pName)
1013 && sqliteFixSelect(&sFix, pSelect)
1014 ){
1015 sqliteSelectDelete(pSelect);
1016 return;
1017 }
drh174b6192002-12-03 02:22:52 +00001018
drh4b59ab52002-08-24 18:24:51 +00001019 /* Make a copy of the entire SELECT statement that defines the view.
1020 ** This will force all the Expr.token.z values to be dynamically
1021 ** allocated rather than point to the input string - which means that
1022 ** they will persist after the current sqlite_exec() call returns.
1023 */
1024 p->pSelect = sqliteSelectDup(pSelect);
1025 sqliteSelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001026 if( !pParse->db->init.busy ){
drh4b59ab52002-08-24 18:24:51 +00001027 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001028 }
drh4b59ab52002-08-24 18:24:51 +00001029
1030 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1031 ** the end.
1032 */
drha76b5df2002-02-23 02:32:10 +00001033 sEnd = pParse->sLastToken;
1034 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1035 sEnd.z += sEnd.n;
1036 }
1037 sEnd.n = 0;
1038 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001039 z = pBegin->z;
1040 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1041 sEnd.z = &z[n-1];
1042 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001043
1044 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
1045 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001046 return;
drh417be792002-03-03 18:59:40 +00001047}
drha76b5df2002-02-23 02:32:10 +00001048
drh417be792002-03-03 18:59:40 +00001049/*
1050** The Table structure pTable is really a VIEW. Fill in the names of
1051** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001052** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001053*/
1054int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
1055 ExprList *pEList;
1056 Select *pSel;
1057 Table *pSelTab;
1058 int nErr = 0;
1059
1060 assert( pTable );
1061
1062 /* A positive nCol means the columns names for this view are
1063 ** already known.
1064 */
1065 if( pTable->nCol>0 ) return 0;
1066
1067 /* A negative nCol is a special marker meaning that we are currently
1068 ** trying to compute the column names. If we enter this routine with
1069 ** a negative nCol, it means two or more views form a loop, like this:
1070 **
1071 ** CREATE VIEW one AS SELECT * FROM two;
1072 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001073 **
1074 ** Actually, this error is caught previously and so the following test
1075 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001076 */
1077 if( pTable->nCol<0 ){
drhf7a9e1a2004-02-22 18:40:56 +00001078 sqliteErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001079 return 1;
1080 }
1081
1082 /* If we get this far, it means we need to compute the table names.
1083 */
1084 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1085 pSel = pTable->pSelect;
1086
1087 /* Note that the call to sqliteResultSetOfSelect() will expand any
1088 ** "*" elements in this list. But we will need to restore the list
1089 ** back to its original configuration afterwards, so we save a copy of
1090 ** the original in pEList.
1091 */
1092 pEList = pSel->pEList;
1093 pSel->pEList = sqliteExprListDup(pEList);
1094 if( pSel->pEList==0 ){
1095 pSel->pEList = pEList;
1096 return 1; /* Malloc failed */
1097 }
1098 pTable->nCol = -1;
1099 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1100 if( pSelTab ){
1101 assert( pTable->aCol==0 );
1102 pTable->nCol = pSelTab->nCol;
1103 pTable->aCol = pSelTab->aCol;
1104 pSelTab->nCol = 0;
1105 pSelTab->aCol = 0;
1106 sqliteDeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001107 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001108 }else{
1109 pTable->nCol = 0;
1110 nErr++;
1111 }
1112 sqliteSelectUnbind(pSel);
1113 sqliteExprListDelete(pSel->pEList);
1114 pSel->pEList = pEList;
1115 return nErr;
1116}
1117
1118/*
1119** Clear the column names from the VIEW pTable.
1120**
1121** This routine is called whenever any other table or view is modified.
1122** The view passed into this routine might depend directly or indirectly
1123** on the modified or deleted table so we need to clear the old column
1124** names so that they will be recomputed.
1125*/
1126static void sqliteViewResetColumnNames(Table *pTable){
1127 int i;
drh701a0ae2004-02-22 20:05:00 +00001128 Column *pCol;
1129 assert( pTable!=0 && pTable->pSelect!=0 );
1130 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1131 sqliteFree(pCol->zName);
1132 sqliteFree(pCol->zDflt);
1133 sqliteFree(pCol->zType);
drh417be792002-03-03 18:59:40 +00001134 }
1135 sqliteFree(pTable->aCol);
1136 pTable->aCol = 0;
1137 pTable->nCol = 0;
1138}
1139
1140/*
drh8bf8dc92003-05-17 17:35:10 +00001141** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001142*/
drhd24cc422003-03-27 12:51:24 +00001143static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001144 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001145 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001146 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001147 Table *pTab = sqliteHashData(i);
1148 if( pTab->pSelect ){
1149 sqliteViewResetColumnNames(pTab);
1150 }
1151 }
drh8bf8dc92003-05-17 17:35:10 +00001152 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001153}
1154
1155/*
drh75897232000-05-29 14:26:00 +00001156** Given a token, look up a table with that name. If not found, leave
1157** an error for the parser to find and return NULL.
1158*/
drhcce7d172000-05-31 15:34:51 +00001159Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001160 char *zName;
1161 Table *pTab;
1162 zName = sqliteTableNameFromToken(pTok);
1163 if( zName==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001164 pTab = sqliteFindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001165 sqliteFree(zName);
1166 if( pTab==0 ){
drhf7a9e1a2004-02-22 18:40:56 +00001167 sqliteErrorMsg(pParse, "no such table: %T", pTok);
drh75897232000-05-29 14:26:00 +00001168 }
1169 return pTab;
1170}
1171
1172/*
1173** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001174** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001175*/
drh4ff6dfa2002-03-03 23:06:00 +00001176void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001177 Table *pTable;
drh75897232000-05-29 14:26:00 +00001178 Vdbe *v;
1179 int base;
drh5edc3122001-09-13 21:53:09 +00001180 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001181 int iDb;
drh75897232000-05-29 14:26:00 +00001182
drhdaffd0e2001-04-11 14:28:42 +00001183 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001184 pTable = sqliteTableFromToken(pParse, pName);
1185 if( pTable==0 ) return;
drhd24cc422003-03-27 12:51:24 +00001186 iDb = pTable->iDb;
drhe22a3342003-04-22 20:30:37 +00001187 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001188#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001189 {
1190 int code;
drhe22a3342003-04-22 20:30:37 +00001191 const char *zTab = SCHEMA_TABLE(pTable->iDb);
1192 const char *zDb = db->aDb[pTable->iDb].zName;
1193 if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
1194 return;
1195 }
drhe5f9c642003-01-13 23:27:31 +00001196 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001197 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001198 code = SQLITE_DROP_TEMP_VIEW;
1199 }else{
1200 code = SQLITE_DROP_VIEW;
1201 }
1202 }else{
drhd24cc422003-03-27 12:51:24 +00001203 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001204 code = SQLITE_DROP_TEMP_TABLE;
1205 }else{
1206 code = SQLITE_DROP_TABLE;
1207 }
1208 }
drhe22a3342003-04-22 20:30:37 +00001209 if( sqliteAuthCheck(pParse, code, pTable->zName, 0, zDb) ){
drhe5f9c642003-01-13 23:27:31 +00001210 return;
1211 }
drhe22a3342003-04-22 20:30:37 +00001212 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +00001213 return;
1214 }
drhe5f9c642003-01-13 23:27:31 +00001215 }
1216#endif
drh75897232000-05-29 14:26:00 +00001217 if( pTable->readOnly ){
drhf7a9e1a2004-02-22 18:40:56 +00001218 sqliteErrorMsg(pParse, "table %s may not be dropped", pTable->zName);
drh75897232000-05-29 14:26:00 +00001219 pParse->nErr++;
1220 return;
1221 }
drh4ff6dfa2002-03-03 23:06:00 +00001222 if( isView && pTable->pSelect==0 ){
drhf7a9e1a2004-02-22 18:40:56 +00001223 sqliteErrorMsg(pParse, "use DROP TABLE to delete table %s", pTable->zName);
drh4ff6dfa2002-03-03 23:06:00 +00001224 return;
1225 }
1226 if( !isView && pTable->pSelect ){
drhf7a9e1a2004-02-22 18:40:56 +00001227 sqliteErrorMsg(pParse, "use DROP VIEW to delete view %s", pTable->zName);
drh4ff6dfa2002-03-03 23:06:00 +00001228 return;
1229 }
drh75897232000-05-29 14:26:00 +00001230
drh1ccde152000-06-17 13:12:39 +00001231 /* Generate code to remove the table from the master table
1232 ** on disk.
1233 */
drhd8bc7082000-06-07 23:51:50 +00001234 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001235 if( v ){
drh905793e2004-02-21 13:31:09 +00001236 static VdbeOpList dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001237 { OP_Rewind, 0, ADDR(8), 0},
1238 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001239 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001240 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001241 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001242 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001243 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001244 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001245 };
1246 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001247 Trigger *pTrigger;
drhd24cc422003-03-27 12:51:24 +00001248 sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001249
danielk1977c3f9bad2002-05-15 08:30:12 +00001250 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001251 pTrigger = pTable->pTrigger;
1252 while( pTrigger ){
drh8bf8dc92003-05-17 17:35:10 +00001253 assert( pTrigger->iDb==pTable->iDb || pTrigger->iDb==1 );
drh79a519c2003-05-17 19:04:03 +00001254 sqliteDropTriggerPtr(pParse, pTrigger, 1);
drhe0bc4042002-06-25 01:09:11 +00001255 if( pParse->explain ){
1256 pTrigger = pTrigger->pNext;
1257 }else{
1258 pTrigger = pTable->pTrigger;
1259 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001260 }
drh8bf8dc92003-05-17 17:35:10 +00001261
1262 /* Drop all SQLITE_MASTER entries that refer to the table */
1263 sqliteOpenMasterTable(v, pTable->iDb);
drhe0bc4042002-06-25 01:09:11 +00001264 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1265 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drh8bf8dc92003-05-17 17:35:10 +00001266
1267 /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */
1268 if( pTable->iDb!=1 ){
1269 sqliteOpenMasterTable(v, 1);
1270 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1271 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
1272 }
1273
1274 if( pTable->iDb==0 ){
drhe0bc4042002-06-25 01:09:11 +00001275 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001276 }
drhe0bc4042002-06-25 01:09:11 +00001277 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001278 if( !isView ){
drhd24cc422003-03-27 12:51:24 +00001279 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001280 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drh8bf8dc92003-05-17 17:35:10 +00001281 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001282 }
drh5e00f6c2001-09-13 13:46:56 +00001283 }
drh1c928532002-01-31 15:54:21 +00001284 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001285 }
1286
drhe0bc4042002-06-25 01:09:11 +00001287 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001288 **
1289 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001290 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001291 */
1292 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001293 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001294 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001295 }
drhd24cc422003-03-27 12:51:24 +00001296 sqliteViewResetAll(db, iDb);
drh75897232000-05-29 14:26:00 +00001297}
1298
1299/*
drh38640e12002-07-05 21:42:36 +00001300** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1301** opcode and adds that P3 string to the most recently inserted instruction
1302** in the virtual machine. The P3 string consists of a single character
1303** for each column in the index pIdx of table pTab. If the column uses
1304** a numeric sort order, then the P3 string character corresponding to
1305** that column is 'n'. If the column uses a text sort order, then the
1306** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1307** additional information. See also the sqliteAddKeyType() routine.
1308*/
1309void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1310 char *zType;
1311 Table *pTab;
1312 int i, n;
1313 assert( pIdx!=0 && pIdx->pTable!=0 );
1314 pTab = pIdx->pTable;
1315 n = pIdx->nColumn;
drh8c1238a2003-01-02 14:43:55 +00001316 zType = sqliteMallocRaw( n+1 );
drh38640e12002-07-05 21:42:36 +00001317 if( zType==0 ) return;
1318 for(i=0; i<n; i++){
1319 int iCol = pIdx->aiColumn[i];
1320 assert( iCol>=0 && iCol<pTab->nCol );
1321 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1322 zType[i] = 't';
1323 }else{
1324 zType[i] = 'n';
1325 }
1326 }
1327 zType[n] = 0;
1328 sqliteVdbeChangeP3(v, -1, zType, n);
1329 sqliteFree(zType);
1330}
1331
1332/*
drhc2eef3b2002-08-31 18:53:06 +00001333** This routine is called to create a new foreign key on the table
1334** currently under construction. pFromCol determines which columns
1335** in the current table point to the foreign key. If pFromCol==0 then
1336** connect the key to the last column inserted. pTo is the name of
1337** the table referred to. pToCol is a list of tables in the other
1338** pTo table that the foreign key points to. flags contains all
1339** information about the conflict resolution algorithms specified
1340** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1341**
1342** An FKey structure is created and added to the table currently
1343** under construction in the pParse->pNewTable field. The new FKey
1344** is not linked into db->aFKey at this point - that does not happen
1345** until sqliteEndTable().
1346**
1347** The foreign key is set for IMMEDIATE processing. A subsequent call
1348** to sqliteDeferForeignKey() might change this to DEFERRED.
1349*/
1350void sqliteCreateForeignKey(
1351 Parse *pParse, /* Parsing context */
1352 IdList *pFromCol, /* Columns in this table that point to other table */
1353 Token *pTo, /* Name of the other table */
1354 IdList *pToCol, /* Columns in the other table */
1355 int flags /* Conflict resolution algorithms. */
1356){
1357 Table *p = pParse->pNewTable;
1358 int nByte;
1359 int i;
1360 int nCol;
1361 char *z;
1362 FKey *pFKey = 0;
1363
1364 assert( pTo!=0 );
1365 if( p==0 || pParse->nErr ) goto fk_end;
1366 if( pFromCol==0 ){
1367 int iCol = p->nCol-1;
1368 if( iCol<0 ) goto fk_end;
1369 if( pToCol && pToCol->nId!=1 ){
drhf7a9e1a2004-02-22 18:40:56 +00001370 sqliteErrorMsg(pParse, "foreign key on %s"
1371 " should reference only one column of table %T",
1372 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001373 goto fk_end;
1374 }
1375 nCol = 1;
1376 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
drhf7a9e1a2004-02-22 18:40:56 +00001377 sqliteErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001378 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001379 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001380 goto fk_end;
1381 }else{
1382 nCol = pFromCol->nId;
1383 }
1384 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1385 if( pToCol ){
1386 for(i=0; i<pToCol->nId; i++){
1387 nByte += strlen(pToCol->a[i].zName) + 1;
1388 }
1389 }
1390 pFKey = sqliteMalloc( nByte );
1391 if( pFKey==0 ) goto fk_end;
1392 pFKey->pFrom = p;
1393 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001394 z = (char*)&pFKey[1];
1395 pFKey->aCol = (struct sColMap*)z;
1396 z += sizeof(struct sColMap)*nCol;
1397 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001398 memcpy(z, pTo->z, pTo->n);
1399 z[pTo->n] = 0;
1400 z += pTo->n+1;
1401 pFKey->pNextTo = 0;
1402 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001403 if( pFromCol==0 ){
1404 pFKey->aCol[0].iFrom = p->nCol-1;
1405 }else{
1406 for(i=0; i<nCol; i++){
1407 int j;
1408 for(j=0; j<p->nCol; j++){
1409 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1410 pFKey->aCol[i].iFrom = j;
1411 break;
1412 }
1413 }
1414 if( j>=p->nCol ){
drhf7a9e1a2004-02-22 18:40:56 +00001415 sqliteErrorMsg(pParse,
1416 "unknown column \"%s\" in foreign key definition",
1417 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001418 goto fk_end;
1419 }
1420 }
1421 }
1422 if( pToCol ){
1423 for(i=0; i<nCol; i++){
1424 int n = strlen(pToCol->a[i].zName);
1425 pFKey->aCol[i].zCol = z;
1426 memcpy(z, pToCol->a[i].zName, n);
1427 z[n] = 0;
1428 z += n+1;
1429 }
1430 }
1431 pFKey->isDeferred = 0;
1432 pFKey->deleteConf = flags & 0xff;
1433 pFKey->updateConf = (flags >> 8 ) & 0xff;
1434 pFKey->insertConf = (flags >> 16 ) & 0xff;
1435
1436 /* Link the foreign key to the table as the last step.
1437 */
1438 p->pFKey = pFKey;
1439 pFKey = 0;
1440
1441fk_end:
1442 sqliteFree(pFKey);
1443 sqliteIdListDelete(pFromCol);
1444 sqliteIdListDelete(pToCol);
1445}
1446
1447/*
1448** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1449** clause is seen as part of a foreign key definition. The isDeferred
1450** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1451** The behavior of the most recently created foreign key is adjusted
1452** accordingly.
1453*/
1454void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1455 Table *pTab;
1456 FKey *pFKey;
1457 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1458 pFKey->isDeferred = isDeferred;
1459}
1460
1461/*
drh75897232000-05-29 14:26:00 +00001462** Create a new index for an SQL table. pIndex is the name of the index
1463** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001464** be NULL for a primary key or an index that is created to satisfy a
1465** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001466** as the table to be indexed. pParse->pNewTable is a table that is
1467** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001468**
drh382c0242001-10-06 16:33:02 +00001469** pList is a list of columns to be indexed. pList will be NULL if this
1470** is a primary key or unique-constraint on the most recent column added
1471** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001472*/
1473void sqliteCreateIndex(
1474 Parse *pParse, /* All information about this parse */
1475 Token *pName, /* Name of the index. May be NULL */
drhd24cc422003-03-27 12:51:24 +00001476 SrcList *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001477 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001478 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001479 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1480 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1481){
1482 Table *pTab; /* Table to be indexed */
1483 Index *pIndex; /* The index to be created */
1484 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001485 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001486 Token nullId; /* Fake token for an empty ID list */
1487 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001488 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001489 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001490
drhdaffd0e2001-04-11 14:28:42 +00001491 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
drh1d85d932004-02-14 23:05:52 +00001492 if( db->init.busy
1493 && sqliteFixInit(&sFix, pParse, db->init.iDb, "index", pName)
drhf26e09c2003-05-31 16:21:12 +00001494 && sqliteFixSrcList(&sFix, pTable)
1495 ){
1496 goto exit_create_index;
1497 }
drhdaffd0e2001-04-11 14:28:42 +00001498
drh75897232000-05-29 14:26:00 +00001499 /*
1500 ** Find the table that is to be indexed. Return early if not found.
1501 */
1502 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001503 assert( pName!=0 );
drhd24cc422003-03-27 12:51:24 +00001504 assert( pTable->nSrc==1 );
drh812d7a22003-03-27 13:50:00 +00001505 pTab = sqliteSrcListLookup(pParse, pTable);
drh75897232000-05-29 14:26:00 +00001506 }else{
drhe3c41372001-09-17 20:25:58 +00001507 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001508 pTab = pParse->pNewTable;
1509 }
1510 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001511 if( pTab->readOnly ){
drhf7a9e1a2004-02-22 18:40:56 +00001512 sqliteErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001513 goto exit_create_index;
1514 }
drh1d85d932004-02-14 23:05:52 +00001515 if( pTab->iDb>=2 && db->init.busy==0 ){
drhf7a9e1a2004-02-22 18:40:56 +00001516 sqliteErrorMsg(pParse, "table %s may not have indices added", pTab->zName);
drh75897232000-05-29 14:26:00 +00001517 goto exit_create_index;
1518 }
drha76b5df2002-02-23 02:32:10 +00001519 if( pTab->pSelect ){
drhf7a9e1a2004-02-22 18:40:56 +00001520 sqliteErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001521 goto exit_create_index;
1522 }
drh4925ca02003-11-27 00:48:57 +00001523 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001524
1525 /*
1526 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001527 ** index or table with the same name.
1528 **
1529 ** Exception: If we are reading the names of permanent indices from the
1530 ** sqlite_master table (because some other process changed the schema) and
1531 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001532 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001533 **
1534 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001535 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1536 ** own name.
drh75897232000-05-29 14:26:00 +00001537 */
drh1d85d932004-02-14 23:05:52 +00001538 if( pName && !db->init.busy ){
drhf57b3392001-10-08 13:22:32 +00001539 Index *pISameName; /* Another index with the same name */
1540 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001541 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001542 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001543 if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
drhf7a9e1a2004-02-22 18:40:56 +00001544 sqliteErrorMsg(pParse, "index %s already exists", zName);
drhd24cc422003-03-27 12:51:24 +00001545 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001546 }
drhd24cc422003-03-27 12:51:24 +00001547 if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
drhf7a9e1a2004-02-22 18:40:56 +00001548 sqliteErrorMsg(pParse, "there is already a table named %s", zName);
drhd24cc422003-03-27 12:51:24 +00001549 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001550 }
drhd24cc422003-03-27 12:51:24 +00001551 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001552 char zBuf[30];
1553 int n;
1554 Index *pLoop;
1555 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1556 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001557 zName = 0;
drh41743982003-12-06 21:43:55 +00001558 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001559 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001560 }else{
1561 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001562 }
1563
drhe5f9c642003-01-13 23:27:31 +00001564 /* Check for authorization to create an index.
1565 */
1566#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001567 {
1568 const char *zDb = db->aDb[pTab->iDb].zName;
1569
drh1d85d932004-02-14 23:05:52 +00001570 assert( pTab->iDb==db->init.iDb || isTemp );
drhe22a3342003-04-22 20:30:37 +00001571 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
1572 goto exit_create_index;
1573 }
1574 i = SQLITE_CREATE_INDEX;
1575 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
1576 if( sqliteAuthCheck(pParse, i, zName, pTab->zName, zDb) ){
1577 goto exit_create_index;
1578 }
drhe5f9c642003-01-13 23:27:31 +00001579 }
1580#endif
1581
drh75897232000-05-29 14:26:00 +00001582 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001583 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001584 ** So create a fake list to simulate this.
1585 */
1586 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001587 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001588 nullId.n = strlen(nullId.z);
1589 pList = sqliteIdListAppend(0, &nullId);
1590 if( pList==0 ) goto exit_create_index;
1591 }
1592
1593 /*
1594 ** Allocate the index structure.
1595 */
drhdcc581c2000-05-30 13:44:19 +00001596 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001597 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001598 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001599 pIndex->aiColumn = (int*)&pIndex[1];
1600 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001601 strcpy(pIndex->zName, zName);
1602 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001603 pIndex->nColumn = pList->nId;
drhea1ba172003-04-20 00:00:23 +00001604 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001605 pIndex->autoIndex = pName==0;
drh1d85d932004-02-14 23:05:52 +00001606 pIndex->iDb = isTemp ? 1 : db->init.iDb;
drh75897232000-05-29 14:26:00 +00001607
drh1ccde152000-06-17 13:12:39 +00001608 /* Scan the names of the columns of the table to be indexed and
1609 ** load the column indices into the Index structure. Report an error
1610 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001611 */
1612 for(i=0; i<pList->nId; i++){
1613 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001614 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001615 }
1616 if( j>=pTab->nCol ){
drhf7a9e1a2004-02-22 18:40:56 +00001617 sqliteErrorMsg(pParse, "table %s has no column named %s",
1618 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001619 sqliteFree(pIndex);
1620 goto exit_create_index;
1621 }
drh967e8b72000-06-21 13:59:10 +00001622 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001623 }
1624
1625 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001626 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001627 */
drhd24cc422003-03-27 12:51:24 +00001628 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001629 Index *p;
drh3c8bf552003-07-01 18:13:14 +00001630 p = sqliteHashInsert(&db->aDb[pIndex->iDb].idxHash,
1631 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001632 if( p ){
1633 assert( p==pIndex ); /* Malloc must have failed */
1634 sqliteFree(pIndex);
1635 goto exit_create_index;
1636 }
drh5e00f6c2001-09-13 13:46:56 +00001637 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001638 }
drh9cfcf5d2002-01-29 18:41:24 +00001639
1640 /* When adding an index to the list of indices for a table, make
1641 ** sure all indices labeled OE_Replace come after all those labeled
1642 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1643 ** and INSERT.
1644 */
1645 if( onError!=OE_Replace || pTab->pIndex==0
1646 || pTab->pIndex->onError==OE_Replace){
1647 pIndex->pNext = pTab->pIndex;
1648 pTab->pIndex = pIndex;
1649 }else{
1650 Index *pOther = pTab->pIndex;
1651 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1652 pOther = pOther->pNext;
1653 }
1654 pIndex->pNext = pOther->pNext;
1655 pOther->pNext = pIndex;
1656 }
drh75897232000-05-29 14:26:00 +00001657
drh1d85d932004-02-14 23:05:52 +00001658 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhd78eeee2001-09-13 16:18:53 +00001659 ** "sqlite_master" table on the disk. So do not write to the disk
drh1d85d932004-02-14 23:05:52 +00001660 ** again. Extract the table number from the db->init.newTnum field.
drhd78eeee2001-09-13 16:18:53 +00001661 */
drh1d85d932004-02-14 23:05:52 +00001662 if( db->init.busy && pTable!=0 ){
1663 pIndex->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001664 }
1665
drh1d85d932004-02-14 23:05:52 +00001666 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00001667 ** involves writing the index into the master table and filling in the
1668 ** index with the current table contents.
1669 **
drh1d85d932004-02-14 23:05:52 +00001670 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
1671 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00001672 ** CREATE INDEX statements are read out of the master table. In
1673 ** the latter case the index already exists on disk, which is why
1674 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001675 **
1676 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001677 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1678 ** has just been created, it contains no data and the index initialization
1679 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001680 */
drh1d85d932004-02-14 23:05:52 +00001681 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00001682 int n;
drhadbca9c2001-09-27 15:11:53 +00001683 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001684 int lbl1, lbl2;
1685 int i;
drhadbca9c2001-09-27 15:11:53 +00001686 int addr;
drh75897232000-05-29 14:26:00 +00001687
drhd8bc7082000-06-07 23:51:50 +00001688 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001689 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001690 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001691 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001692 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001693 }
drhe0bc4042002-06-25 01:09:11 +00001694 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
drh701a0ae2004-02-22 20:05:00 +00001695 sqliteVdbeOp3(v, OP_String, 0, 0, "index", P3_STATIC);
1696 sqliteVdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0);
1697 sqliteVdbeOp3(v, OP_String, 0, 0, pTab->zName, 0);
1698 sqliteVdbeOp3(v, OP_CreateIndex, 0, isTemp,(char*)&pIndex->tnum,P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001699 pIndex->tnum = 0;
1700 if( pTable ){
drh701a0ae2004-02-22 20:05:00 +00001701 sqliteVdbeCode(v,
1702 OP_Dup, 0, 0,
1703 OP_Integer, isTemp, 0,
1704 OP_OpenWrite, 1, 0,
1705 0);
drh5e00f6c2001-09-13 13:46:56 +00001706 }
drhe0bc4042002-06-25 01:09:11 +00001707 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1708 if( pStart && pEnd ){
1709 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1710 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001711 }
drhe0bc4042002-06-25 01:09:11 +00001712 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1713 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001714 if( pTable ){
drhd24cc422003-03-27 12:51:24 +00001715 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh701a0ae2004-02-22 20:05:00 +00001716 sqliteVdbeOp3(v, OP_OpenRead, 2, pTab->tnum, pTab->zName, 0);
drhadbca9c2001-09-27 15:11:53 +00001717 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001718 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1719 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001720 for(i=0; i<pIndex->nColumn; i++){
drh56e452c2003-05-01 16:56:03 +00001721 int iCol = pIndex->aiColumn[i];
1722 if( pTab->iPKey==iCol ){
1723 sqliteVdbeAddOp(v, OP_Dup, i, 0);
1724 }else{
drh6a3ea0e2003-05-02 14:32:12 +00001725 sqliteVdbeAddOp(v, OP_Column, 2, iCol);
drh56e452c2003-05-01 16:56:03 +00001726 }
drhadbca9c2001-09-27 15:11:53 +00001727 }
drh99fcd712001-10-13 01:06:47 +00001728 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001729 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh701a0ae2004-02-22 20:05:00 +00001730 sqliteVdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
1731 "indexed columns are not unique", P3_STATIC);
drh6b563442001-11-07 16:48:26 +00001732 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001733 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001734 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1735 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001736 }
drhadbca9c2001-09-27 15:11:53 +00001737 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001738 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001739 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001740 }
drhe0bc4042002-06-25 01:09:11 +00001741 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001742 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001743 }
drh75897232000-05-29 14:26:00 +00001744 }
1745
drh75897232000-05-29 14:26:00 +00001746 /* Clean up before exiting */
1747exit_create_index:
1748 sqliteIdListDelete(pList);
drhd24cc422003-03-27 12:51:24 +00001749 sqliteSrcListDelete(pTable);
drh75897232000-05-29 14:26:00 +00001750 sqliteFree(zName);
1751 return;
1752}
1753
1754/*
drh74e24cd2002-01-09 03:19:59 +00001755** This routine will drop an existing named index. This routine
1756** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001757*/
drhd24cc422003-03-27 12:51:24 +00001758void sqliteDropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001759 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001760 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001761 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001762
drhdaffd0e2001-04-11 14:28:42 +00001763 if( pParse->nErr || sqlite_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001764 assert( pName->nSrc==1 );
1765 pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001766 if( pIndex==0 ){
drhda93d232003-03-31 02:12:46 +00001767 sqliteErrorMsg(pParse, "no such index: %S", pName, 0);
drhd24cc422003-03-27 12:51:24 +00001768 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001769 }
drh485b39b2002-07-13 03:11:52 +00001770 if( pIndex->autoIndex ){
drhda93d232003-03-31 02:12:46 +00001771 sqliteErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00001772 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00001773 goto exit_drop_index;
1774 }
1775 if( pIndex->iDb>1 ){
drhda93d232003-03-31 02:12:46 +00001776 sqliteErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00001777 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00001778 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001779 }
drhe5f9c642003-01-13 23:27:31 +00001780#ifndef SQLITE_OMIT_AUTHORIZATION
1781 {
1782 int code = SQLITE_DROP_INDEX;
1783 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00001784 const char *zDb = db->aDb[pIndex->iDb].zName;
1785 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
1786 if( sqliteAuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001787 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001788 }
drhd24cc422003-03-27 12:51:24 +00001789 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
drhe22a3342003-04-22 20:30:37 +00001790 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001791 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001792 }
drhed6c8672003-01-12 18:02:16 +00001793 }
drhe5f9c642003-01-13 23:27:31 +00001794#endif
drh75897232000-05-29 14:26:00 +00001795
1796 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001797 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001798 if( v ){
drh905793e2004-02-21 13:31:09 +00001799 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001800 { OP_Rewind, 0, ADDR(9), 0},
1801 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001802 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001803 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001804 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001805 { OP_Eq, 0, ADDR(8), 0},
1806 { OP_Next, 0, ADDR(3), 0},
1807 { OP_Goto, 0, ADDR(9), 0},
1808 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001809 };
1810 int base;
1811
drhd24cc422003-03-27 12:51:24 +00001812 sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
1813 sqliteOpenMasterTable(v, pIndex->iDb);
drhe0bc4042002-06-25 01:09:11 +00001814 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1815 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001816 if( pIndex->iDb==0 ){
drhe0bc4042002-06-25 01:09:11 +00001817 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001818 }
drhe0bc4042002-06-25 01:09:11 +00001819 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhd24cc422003-03-27 12:51:24 +00001820 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh1c928532002-01-31 15:54:21 +00001821 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001822 }
1823
drhe0bc4042002-06-25 01:09:11 +00001824 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001825 */
1826 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001827 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001828 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001829 }
drhd24cc422003-03-27 12:51:24 +00001830
1831exit_drop_index:
1832 sqliteSrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001833}
1834
1835/*
drh75897232000-05-29 14:26:00 +00001836** Append a new element to the given IdList. Create a new IdList if
1837** need be.
drhdaffd0e2001-04-11 14:28:42 +00001838**
1839** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001840*/
1841IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1842 if( pList==0 ){
1843 pList = sqliteMalloc( sizeof(IdList) );
1844 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001845 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00001846 }
drh4305d102003-07-30 12:34:12 +00001847 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00001848 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00001849 pList->nAlloc = pList->nAlloc*2 + 5;
1850 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00001851 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001852 sqliteIdListDelete(pList);
1853 return 0;
drh75897232000-05-29 14:26:00 +00001854 }
drh6d4abfb2001-10-22 02:58:08 +00001855 pList->a = a;
drh75897232000-05-29 14:26:00 +00001856 }
1857 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1858 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001859 char **pz = &pList->a[pList->nId].zName;
1860 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1861 if( *pz==0 ){
1862 sqliteIdListDelete(pList);
1863 return 0;
1864 }else{
1865 sqliteDequote(*pz);
1866 }
drh75897232000-05-29 14:26:00 +00001867 }
1868 pList->nId++;
1869 return pList;
1870}
1871
1872/*
drhad3cab52002-05-24 02:04:32 +00001873** Append a new table name to the given SrcList. Create a new SrcList if
1874** need be. A new entry is created in the SrcList even if pToken is NULL.
1875**
1876** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00001877**
1878** If pDatabase is not null, it means that the table has an optional
1879** database name prefix. Like this: "database.table". The pDatabase
1880** points to the table name and the pTable points to the database name.
1881** The SrcList.a[].zName field is filled with the table name which might
1882** come from pTable (if pDatabase is NULL) or from pDatabase.
1883** SrcList.a[].zDatabase is filled with the database name from pTable,
1884** or with NULL if no database is specified.
1885**
1886** In other words, if call like this:
1887**
1888** sqliteSrcListAppend(A,B,0);
1889**
1890** Then B is a table name and the database name is unspecified. If called
1891** like this:
1892**
1893** sqliteSrcListAppend(A,B,C);
1894**
1895** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00001896*/
drh113088e2003-03-20 01:16:58 +00001897SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00001898 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00001899 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00001900 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001901 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00001902 }
drh4305d102003-07-30 12:34:12 +00001903 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00001904 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00001905 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00001906 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00001907 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00001908 if( pNew==0 ){
drhad3cab52002-05-24 02:04:32 +00001909 sqliteSrcListDelete(pList);
1910 return 0;
1911 }
drh113088e2003-03-20 01:16:58 +00001912 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00001913 }
1914 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00001915 if( pDatabase && pDatabase->z==0 ){
1916 pDatabase = 0;
1917 }
1918 if( pDatabase && pTable ){
1919 Token *pTemp = pDatabase;
1920 pDatabase = pTable;
1921 pTable = pTemp;
1922 }
1923 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00001924 char **pz = &pList->a[pList->nSrc].zName;
drh113088e2003-03-20 01:16:58 +00001925 sqliteSetNString(pz, pTable->z, pTable->n, 0);
1926 if( *pz==0 ){
1927 sqliteSrcListDelete(pList);
1928 return 0;
1929 }else{
1930 sqliteDequote(*pz);
1931 }
1932 }
1933 if( pDatabase ){
1934 char **pz = &pList->a[pList->nSrc].zDatabase;
1935 sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00001936 if( *pz==0 ){
1937 sqliteSrcListDelete(pList);
1938 return 0;
1939 }else{
1940 sqliteDequote(*pz);
1941 }
1942 }
drh63eb5f22003-04-29 16:20:44 +00001943 pList->a[pList->nSrc].iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00001944 pList->nSrc++;
1945 return pList;
1946}
1947
1948/*
drh63eb5f22003-04-29 16:20:44 +00001949** Assign cursors to all tables in a SrcList
1950*/
1951void sqliteSrcListAssignCursors(Parse *pParse, SrcList *pList){
1952 int i;
1953 for(i=0; i<pList->nSrc; i++){
1954 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00001955 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00001956 }
1957 }
1958}
1959
1960/*
drh75897232000-05-29 14:26:00 +00001961** Add an alias to the last identifier on the given identifier list.
1962*/
drhad3cab52002-05-24 02:04:32 +00001963void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1964 if( pList && pList->nSrc>0 ){
1965 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001966 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001967 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001968 }
1969}
1970
1971/*
drhad3cab52002-05-24 02:04:32 +00001972** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001973*/
1974void sqliteIdListDelete(IdList *pList){
1975 int i;
1976 if( pList==0 ) return;
1977 for(i=0; i<pList->nId; i++){
1978 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001979 }
1980 sqliteFree(pList->a);
1981 sqliteFree(pList);
1982}
1983
1984/*
drhad2d8302002-05-24 20:31:36 +00001985** Return the index in pList of the identifier named zId. Return -1
1986** if not found.
1987*/
1988int sqliteIdListIndex(IdList *pList, const char *zName){
1989 int i;
1990 if( pList==0 ) return -1;
1991 for(i=0; i<pList->nId; i++){
1992 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1993 }
1994 return -1;
1995}
1996
1997/*
drhad3cab52002-05-24 02:04:32 +00001998** Delete an entire SrcList including all its substructure.
1999*/
2000void sqliteSrcListDelete(SrcList *pList){
2001 int i;
2002 if( pList==0 ) return;
2003 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002004 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002005 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002006 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002007 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00002008 sqliteDeleteTable(0, pList->a[i].pTab);
2009 }
drhff78bd22002-02-27 01:47:11 +00002010 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00002011 sqliteExprDelete(pList->a[i].pOn);
2012 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002013 }
drh75897232000-05-29 14:26:00 +00002014 sqliteFree(pList);
2015}
2016
drh982cef72000-05-30 16:27:03 +00002017/*
drhc4a3c772001-04-04 11:48:57 +00002018** Begin a transaction
2019*/
drh1c928532002-01-31 15:54:21 +00002020void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002021 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002022
drh001bbcb2003-03-19 03:14:00 +00002023 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002024 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe22a3342003-04-22 20:30:37 +00002025 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002026 if( db->flags & SQLITE_InTrans ){
drhda93d232003-03-31 02:12:46 +00002027 sqliteErrorMsg(pParse, "cannot start a transaction within a transaction");
drh6b8b8742002-08-18 20:28:06 +00002028 return;
2029 }
drhcabb0812002-09-14 13:47:32 +00002030 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002031 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002032 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00002033}
2034
2035/*
2036** Commit a transaction
2037*/
2038void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002039 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002040
drh001bbcb2003-03-19 03:14:00 +00002041 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002042 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe22a3342003-04-22 20:30:37 +00002043 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002044 if( (db->flags & SQLITE_InTrans)==0 ){
drhda93d232003-03-31 02:12:46 +00002045 sqliteErrorMsg(pParse, "cannot commit - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002046 return;
2047 }
drh5e00f6c2001-09-13 13:46:56 +00002048 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002049 sqliteEndWriteOperation(pParse);
2050 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002051}
2052
2053/*
2054** Rollback a transaction
2055*/
2056void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002057 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002058 Vdbe *v;
2059
drh001bbcb2003-03-19 03:14:00 +00002060 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002061 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe22a3342003-04-22 20:30:37 +00002062 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002063 if( (db->flags & SQLITE_InTrans)==0 ){
drhda93d232003-03-31 02:12:46 +00002064 sqliteErrorMsg(pParse, "cannot rollback - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002065 return;
2066 }
drh5e00f6c2001-09-13 13:46:56 +00002067 v = sqliteGetVdbe(pParse);
2068 if( v ){
drh99fcd712001-10-13 01:06:47 +00002069 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002070 }
drh5e00f6c2001-09-13 13:46:56 +00002071 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002072 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002073}
drhf57b14a2001-09-14 18:54:08 +00002074
2075/*
drh001bbcb2003-03-19 03:14:00 +00002076** Generate VDBE code that will verify the schema cookie for all
2077** named database files.
2078*/
drh8bf8dc92003-05-17 17:35:10 +00002079void sqliteCodeVerifySchema(Parse *pParse, int iDb){
drh001bbcb2003-03-19 03:14:00 +00002080 sqlite *db = pParse->db;
2081 Vdbe *v = sqliteGetVdbe(pParse);
drh8bf8dc92003-05-17 17:35:10 +00002082 assert( iDb>=0 && iDb<db->nDb );
2083 assert( db->aDb[iDb].pBt!=0 );
2084 if( iDb!=1 && !DbHasProperty(db, iDb, DB_Cookie) ){
2085 sqliteVdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
2086 DbSetProperty(db, iDb, DB_Cookie);
drh001bbcb2003-03-19 03:14:00 +00002087 }
drh001bbcb2003-03-19 03:14:00 +00002088}
2089
2090/*
drh1c928532002-01-31 15:54:21 +00002091** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002092** might change the database.
2093**
2094** This routine starts a new transaction if we are not already within
2095** a transaction. If we are already within a transaction, then a checkpoint
2096** is set if the setCheckpoint parameter is true. A checkpoint should
2097** be set for operations that might fail (due to a constraint) part of
2098** the way through and which will need to undo some writes without having to
2099** rollback the whole transaction. For operations where all constraints
2100** can be checked before any changes are made to the database, it is never
2101** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002102**
drh8bf8dc92003-05-17 17:35:10 +00002103** Only database iDb and the temp database are made writable by this call.
2104** If iDb==0, then the main and temp databases are made writable. If
2105** iDb==1 then only the temp database is made writable. If iDb>1 then the
2106** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002107*/
drh8bf8dc92003-05-17 17:35:10 +00002108void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int iDb){
drh663fc632002-02-02 18:49:19 +00002109 Vdbe *v;
drh8bf8dc92003-05-17 17:35:10 +00002110 sqlite *db = pParse->db;
2111 if( DbHasProperty(db, iDb, DB_Locked) ) return;
drh663fc632002-02-02 18:49:19 +00002112 v = sqliteGetVdbe(pParse);
2113 if( v==0 ) return;
drh8bf8dc92003-05-17 17:35:10 +00002114 if( !db->aDb[iDb].inTrans ){
2115 sqliteVdbeAddOp(v, OP_Transaction, iDb, 0);
2116 DbSetProperty(db, iDb, DB_Locked);
2117 sqliteCodeVerifySchema(pParse, iDb);
2118 if( iDb!=1 ){
2119 sqliteBeginWriteOperation(pParse, setCheckpoint, 1);
drhcabb0812002-09-14 13:47:32 +00002120 }
drhc977f7f2002-05-21 11:38:11 +00002121 }else if( setCheckpoint ){
drh8bf8dc92003-05-17 17:35:10 +00002122 sqliteVdbeAddOp(v, OP_Checkpoint, iDb, 0);
2123 DbSetProperty(db, iDb, DB_Locked);
drh663fc632002-02-02 18:49:19 +00002124 }
2125}
2126
2127/*
drh1c928532002-01-31 15:54:21 +00002128** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002129** the database. If a statement transaction was started, then emit
2130** an OP_Commit that will cause the changes to be committed to disk.
2131**
2132** Note that checkpoints are automatically committed at the end of
2133** a statement. Note also that there can be multiple calls to
2134** sqliteBeginWriteOperation() but there should only be a single
2135** call to sqliteEndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002136*/
2137void sqliteEndWriteOperation(Parse *pParse){
2138 Vdbe *v;
drh8bf8dc92003-05-17 17:35:10 +00002139 sqlite *db = pParse->db;
danielk1977f29ce552002-05-19 23:43:12 +00002140 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002141 v = sqliteGetVdbe(pParse);
2142 if( v==0 ) return;
drh8bf8dc92003-05-17 17:35:10 +00002143 if( db->flags & SQLITE_InTrans ){
2144 /* A BEGIN has executed. Do not commit until we see an explicit
2145 ** COMMIT statement. */
drh1c928532002-01-31 15:54:21 +00002146 }else{
2147 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2148 }
2149}