blob: a1cd608b5c8f6f20c09a2fa30931d0764064f8b0 [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**
danielk197784ac9d02004-05-18 09:58:06 +000026** $Id: build.c,v 1.187 2004/05/18 09:58:07 danielk1977 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*/
danielk19774adee202004-05-08 08:23:19 +000037void sqlite3BeginParse(Parse *pParse, int explainFlag){
drhe0bc4042002-06-25 01:09:11 +000038 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 ){
danielk19774adee202004-05-08 08:23:19 +000042 int rc = sqlite3Init(db, &pParse->zErrMsg);
drhe0bc4042002-06-25 01:09:11 +000043 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*/
danielk19774adee202004-05-08 08:23:19 +000067void sqlite3Exec(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
danielk19774adee202004-05-08 08:23:19 +000071 if( v==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
72 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
drh50350a12004-02-13 16:22:22 +000073 }
danielk197724b03fd2004-05-10 10:34:34 +000074 if( sqlite3_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;
danielk19774adee202004-05-08 08:23:19 +000077 sqlite3VdbeTrace(v, trace);
78 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +000079 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**
danielk19774adee202004-05-08 08:23:19 +0000103** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000104*/
danielk19774adee202004-05-08 08:23:19 +0000105Table *sqlite3FindTable(sqlite *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000106 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 */
danielk19774adee202004-05-08 08:23:19 +0000110 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
111 p = sqlite3HashFind(&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**
danielk19774adee202004-05-08 08:23:19 +0000124** The difference between this routine and sqlite3FindTable()
drhf26e09c2003-05-31 16:21:12 +0000125** is that this routine leaves an error message in pParse->zErrMsg
danielk19774adee202004-05-08 08:23:19 +0000126** where sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000127*/
danielk19774adee202004-05-08 08:23:19 +0000128Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000129 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000130
danielk19774adee202004-05-08 08:23:19 +0000131 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000132 if( p==0 ){
133 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000134 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
135 }else if( sqlite3FindTable(pParse->db, zName, 0)!=0 ){
136 sqlite3ErrorMsg(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{
danielk19774adee202004-05-08 08:23:19 +0000139 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000140 }
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*/
danielk19774adee202004-05-08 08:23:19 +0000157Index *sqlite3FindIndex(sqlite *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000158 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 */
danielk19774adee202004-05-08 08:23:19 +0000162 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
163 p = sqlite3HashFind(&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 );
danielk19774adee202004-05-08 08:23:19 +0000181 pOld = sqlite3HashInsert(&db->aDb[p->iDb].idxHash, p->zName,
drhd24cc422003-03-27 12:51:24 +0000182 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000183 if( pOld!=0 && pOld!=p ){
danielk19774adee202004-05-08 08:23:19 +0000184 sqlite3HashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
drhd24cc422003-03-27 12:51:24 +0000185 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000186 }
danielk1977a37cdde2004-05-16 11:15:36 +0000187 if( p->zColAff ){
188 sqliteFree(p->zColAff);
189 }
drh74e24cd2002-01-09 03:19:59 +0000190 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000191}
192
193/*
drhbeae3192001-09-22 18:12:08 +0000194** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000195** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000196** structures.
197*/
danielk19774adee202004-05-08 08:23:19 +0000198void sqlite3UnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000199 if( pIndex->pTable->pIndex==pIndex ){
200 pIndex->pTable->pIndex = pIndex->pNext;
201 }else{
202 Index *p;
203 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
204 if( p && p->pNext==pIndex ){
205 p->pNext = pIndex->pNext;
206 }
207 }
208 sqliteDeleteIndex(db, pIndex);
209}
210
211/*
drhe0bc4042002-06-25 01:09:11 +0000212** Erase all schema information from the in-memory hash tables of
213** database connection. This routine is called to reclaim memory
214** before the connection closes. It is also called during a rollback
215** if there were schema changes during the transaction.
drh1c2d8412003-03-31 00:30:47 +0000216**
217** If iDb<=0 then reset the internal schema tables for all database
218** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000219** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000220*/
danielk19774adee202004-05-08 08:23:19 +0000221void sqlite3ResetInternalSchema(sqlite *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000222 HashElem *pElem;
223 Hash temp1;
224 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000225 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000226
drh1c2d8412003-03-31 00:30:47 +0000227 assert( iDb>=0 && iDb<db->nDb );
228 db->flags &= ~SQLITE_Initialized;
229 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000230 Db *pDb = &db->aDb[i];
231 temp1 = pDb->tblHash;
232 temp2 = pDb->trigHash;
danielk19774adee202004-05-08 08:23:19 +0000233 sqlite3HashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
234 sqlite3HashClear(&pDb->aFKey);
235 sqlite3HashClear(&pDb->idxHash);
drhd24cc422003-03-27 12:51:24 +0000236 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
237 Trigger *pTrigger = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000238 sqlite3DeleteTrigger(pTrigger);
drhd24cc422003-03-27 12:51:24 +0000239 }
danielk19774adee202004-05-08 08:23:19 +0000240 sqlite3HashClear(&temp2);
241 sqlite3HashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
drhd24cc422003-03-27 12:51:24 +0000242 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
243 Table *pTab = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000244 sqlite3DeleteTable(db, pTab);
drhd24cc422003-03-27 12:51:24 +0000245 }
danielk19774adee202004-05-08 08:23:19 +0000246 sqlite3HashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000247 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000248 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000249 }
drh1c2d8412003-03-31 00:30:47 +0000250 assert( iDb==0 );
251 db->flags &= ~SQLITE_InternChanges;
252
253 /* If one or more of the auxiliary database files has been closed,
254 ** then remove then from the auxiliary database list. We take the
255 ** opportunity to do this here since we have just deleted all of the
256 ** schema hash tables and therefore do not have to make any changes
257 ** to any of those tables.
258 */
drh4d189ca2004-02-12 18:46:38 +0000259 for(i=0; i<db->nDb; i++){
260 struct Db *pDb = &db->aDb[i];
261 if( pDb->pBt==0 ){
262 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
263 pDb->pAux = 0;
264 }
265 }
drh1c2d8412003-03-31 00:30:47 +0000266 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000267 struct Db *pDb = &db->aDb[i];
268 if( pDb->pBt==0 ){
269 sqliteFree(pDb->zName);
270 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000271 continue;
272 }
273 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000274 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000275 }
drh8bf8dc92003-05-17 17:35:10 +0000276 j++;
drh1c2d8412003-03-31 00:30:47 +0000277 }
278 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
279 db->nDb = j;
280 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
281 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
282 sqliteFree(db->aDb);
283 db->aDb = db->aDbStatic;
284 }
drhe0bc4042002-06-25 01:09:11 +0000285}
286
287/*
288** This routine is called whenever a rollback occurs. If there were
289** schema changes during the transaction, then we have to reset the
290** internal hash tables and reload them from disk.
291*/
danielk19774adee202004-05-08 08:23:19 +0000292void sqlite3RollbackInternalChanges(sqlite *db){
drhe0bc4042002-06-25 01:09:11 +0000293 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000294 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000295 }
296}
297
298/*
299** This routine is called when a commit occurs.
300*/
danielk19774adee202004-05-08 08:23:19 +0000301void sqlite3CommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000302 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000303 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000304}
305
306/*
drh75897232000-05-29 14:26:00 +0000307** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000308** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000309**
310** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000311** the table data structure from the hash table. Nor does it remove
312** foreign keys from the sqlite.aFKey hash table. But it does destroy
313** memory structures of the indices and foreign keys associated with
314** the table.
drhdaffd0e2001-04-11 14:28:42 +0000315**
316** Indices associated with the table are unlinked from the "db"
317** data structure if db!=NULL. If db==NULL, indices attached to
318** the table are deleted, but it is assumed they have already been
319** unlinked.
drh75897232000-05-29 14:26:00 +0000320*/
danielk19774adee202004-05-08 08:23:19 +0000321void sqlite3DeleteTable(sqlite *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000322 int i;
323 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000324 FKey *pFKey, *pNextFKey;
325
drh75897232000-05-29 14:26:00 +0000326 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000327
328 /* Delete all indices associated with this table
329 */
330 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
331 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000332 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000333 sqliteDeleteIndex(db, pIndex);
334 }
335
336 /* Delete all foreign keys associated with this table. The keys
337 ** should have already been unlinked from the db->aFKey hash table
338 */
339 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
340 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000341 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000342 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000343 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000344 sqliteFree(pFKey);
345 }
346
347 /* Delete the Table structure itself.
348 */
drh75897232000-05-29 14:26:00 +0000349 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000350 sqliteFree(pTable->aCol[i].zName);
351 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000352 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000353 }
drh6e142f52000-06-08 13:36:40 +0000354 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000355 sqliteFree(pTable->aCol);
danielk19773d1bfea2004-05-14 11:00:53 +0000356 if( pTable->zColAff ){
357 sqliteFree(pTable->zColAff);
358 }
danielk19774adee202004-05-08 08:23:19 +0000359 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000360 sqliteFree(pTable);
361}
362
363/*
drh5edc3122001-09-13 21:53:09 +0000364** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000365** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000366*/
drh74e24cd2002-01-09 03:19:59 +0000367static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000368 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000369 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000370 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000371 assert( db!=0 );
danielk19774adee202004-05-08 08:23:19 +0000372 pOld = sqlite3HashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000373 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000374 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
375 int nTo = strlen(pF1->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +0000376 pF2 = sqlite3HashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000377 if( pF2==pF1 ){
danielk19774adee202004-05-08 08:23:19 +0000378 sqlite3HashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000379 }else{
380 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
381 if( pF2 ){
382 pF2->pNextTo = pF1->pNextTo;
383 }
384 }
385 }
danielk19774adee202004-05-08 08:23:19 +0000386 sqlite3DeleteTable(db, p);
drh74e24cd2002-01-09 03:19:59 +0000387}
388
389/*
drh1ccde152000-06-17 13:12:39 +0000390** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000391**
392** Space to hold the name is obtained from sqliteMalloc() and must
393** be freed by the calling function.
394*/
danielk19774adee202004-05-08 08:23:19 +0000395char *sqlite3TableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000396 char *zName = sqliteStrNDup(pName->z, pName->n);
danielk19774adee202004-05-08 08:23:19 +0000397 sqlite3Dequote(zName);
drh75897232000-05-29 14:26:00 +0000398 return zName;
399}
400
401/*
drhe0bc4042002-06-25 01:09:11 +0000402** Generate code to open the appropriate master table. The table
403** opened will be SQLITE_MASTER for persistent tables and
404** SQLITE_TEMP_MASTER for temporary tables. The table is opened
405** on cursor 0.
406*/
danielk19774adee202004-05-08 08:23:19 +0000407void sqlite3OpenMasterTable(Vdbe *v, int isTemp){
408 sqlite3VdbeAddOp(v, OP_Integer, isTemp, 0);
danielk19778e150812004-05-10 01:17:37 +0000409 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000410 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000411}
412
413/*
drh75897232000-05-29 14:26:00 +0000414** Begin constructing a new table representation in memory. This is
415** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000416** to a CREATE TABLE statement. In particular, this routine is called
417** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000418** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000419** flag is true if the table should be stored in the auxiliary database
420** file instead of in the main database file. This is normally the case
421** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000422** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000423**
drhf57b3392001-10-08 13:22:32 +0000424** The new table record is initialized and put in pParse->pNewTable.
425** As more of the CREATE TABLE statement is parsed, additional action
426** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000427** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000428** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000429*/
danielk19774adee202004-05-08 08:23:19 +0000430void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000431 Parse *pParse, /* Parser context */
432 Token *pStart, /* The "CREATE" token */
433 Token *pName, /* Name of table or view to create */
434 int isTemp, /* True if this is a TEMP table */
435 int isView /* True if this is a VIEW */
436){
drh75897232000-05-29 14:26:00 +0000437 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000438 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000439 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000440 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000441 Vdbe *v;
drh1c2d8412003-03-31 00:30:47 +0000442 int iDb;
drh75897232000-05-29 14:26:00 +0000443
444 pParse->sFirstToken = *pStart;
danielk19774adee202004-05-08 08:23:19 +0000445 zName = sqlite3TableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000446 if( zName==0 ) return;
drh1d85d932004-02-14 23:05:52 +0000447 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000448#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000449 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000450 {
451 int code;
drhe22a3342003-04-22 20:30:37 +0000452 char *zDb = isTemp ? "temp" : "main";
danielk19774adee202004-05-08 08:23:19 +0000453 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000454 sqliteFree(zName);
455 return;
456 }
drhe5f9c642003-01-13 23:27:31 +0000457 if( isView ){
458 if( isTemp ){
459 code = SQLITE_CREATE_TEMP_VIEW;
460 }else{
461 code = SQLITE_CREATE_VIEW;
462 }
463 }else{
464 if( isTemp ){
465 code = SQLITE_CREATE_TEMP_TABLE;
466 }else{
467 code = SQLITE_CREATE_TABLE;
468 }
469 }
danielk19774adee202004-05-08 08:23:19 +0000470 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000471 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000472 return;
473 }
474 }
475#endif
476
drhf57b3392001-10-08 13:22:32 +0000477
478 /* Before trying to create a temporary table, make sure the Btree for
479 ** holding temporary tables is open.
480 */
drhd24cc422003-03-27 12:51:24 +0000481 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +0000482 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000483 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000484 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
drhf7a9e1a2004-02-22 18:40:56 +0000485 "file for storing temporary tables");
drhf57b3392001-10-08 13:22:32 +0000486 pParse->nErr++;
487 return;
488 }
489 if( db->flags & SQLITE_InTrans ){
danielk19774adee202004-05-08 08:23:19 +0000490 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000491 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000492 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
drhf7a9e1a2004-02-22 18:40:56 +0000493 "the temporary database file");
drhf57b3392001-10-08 13:22:32 +0000494 return;
495 }
496 }
497 }
498
499 /* Make sure the new table name does not collide with an existing
500 ** index or table name. Issue an error message if it does.
501 **
502 ** If we are re-reading the sqlite_master table because of a schema
503 ** change and a new permanent table is found whose name collides with
drhd24cc422003-03-27 12:51:24 +0000504 ** an existing temporary table, that is not an error.
drhf57b3392001-10-08 13:22:32 +0000505 */
danielk19774adee202004-05-08 08:23:19 +0000506 pTable = sqlite3FindTable(db, zName, 0);
drh1d85d932004-02-14 23:05:52 +0000507 iDb = isTemp ? 1 : db->init.iDb;
508 if( pTable!=0 && (pTable->iDb==iDb || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000509 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000510 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000511 return;
drh75897232000-05-29 14:26:00 +0000512 }
danielk19774adee202004-05-08 08:23:19 +0000513 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
drh1d85d932004-02-14 23:05:52 +0000514 (pIdx->iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000515 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000516 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000517 return;
518 }
519 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000520 if( pTable==0 ){
521 sqliteFree(zName);
522 return;
523 }
drh75897232000-05-29 14:26:00 +0000524 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000525 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000526 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000527 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000528 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000529 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000530 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000531 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000532
533 /* Begin generating the code that will insert the table record into
534 ** the SQLITE_MASTER table. Note in particular that we must go ahead
535 ** and allocate the record number for the table entry now. Before any
536 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
537 ** indices to be created and the table record must come before the
538 ** indices. Hence, the record number for the table must be allocated
539 ** now.
540 */
danielk19774adee202004-05-08 08:23:19 +0000541 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
542 sqlite3BeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000543 if( !isTemp ){
danielk19774adee202004-05-08 08:23:19 +0000544 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
545 sqlite3VdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000546 }
danielk19774adee202004-05-08 08:23:19 +0000547 sqlite3OpenMasterTable(v, isTemp);
548 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
549 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
550 sqlite3VdbeAddOp(v, OP_String, 0, 0);
551 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000552 }
drh75897232000-05-29 14:26:00 +0000553}
554
555/*
556** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000557**
558** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000559** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000560** first to get things going. Then this routine is called for each
561** column.
drh75897232000-05-29 14:26:00 +0000562*/
danielk19774adee202004-05-08 08:23:19 +0000563void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000564 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000565 int i;
566 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000567 Column *pCol;
drh75897232000-05-29 14:26:00 +0000568 if( (p = pParse->pNewTable)==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000569 sqlite3SetNString(&z, pName->z, pName->n, 0);
drh97fc3d02002-05-22 21:27:03 +0000570 if( z==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000571 sqlite3Dequote(z);
drh97fc3d02002-05-22 21:27:03 +0000572 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000573 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
574 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000575 sqliteFree(z);
576 return;
577 }
578 }
drh75897232000-05-29 14:26:00 +0000579 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000580 Column *aNew;
581 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
582 if( aNew==0 ) return;
583 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000584 }
drhc9b84a12002-06-20 11:36:48 +0000585 pCol = &p->aCol[p->nCol];
586 memset(pCol, 0, sizeof(p->aCol[0]));
587 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000588
589 /* If there is no type specified, columns have the default affinity
590 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType()
591 ** will be called next to set pCol->affinity correctly.
592 */
593 pCol->affinity = SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +0000594 p->nCol++;
drh75897232000-05-29 14:26:00 +0000595}
596
597/*
drh382c0242001-10-06 16:33:02 +0000598** This routine is called by the parser while in the middle of
599** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
600** been seen on a column. This routine sets the notNull flag on
601** the column currently under construction.
602*/
danielk19774adee202004-05-08 08:23:19 +0000603void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000604 Table *p;
605 int i;
606 if( (p = pParse->pNewTable)==0 ) return;
607 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000608 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000609}
610
611/*
612** This routine is called by the parser while in the middle of
613** parsing a CREATE TABLE statement. The pFirst token is the first
614** token in the sequence of tokens that describe the type of the
615** column currently under construction. pLast is the last token
616** in the sequence. Use this information to construct a string
617** that contains the typename of the column and store that string
618** in zType.
619*/
danielk19774adee202004-05-08 08:23:19 +0000620void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000621 Table *p;
622 int i, j;
623 int n;
624 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000625 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000626 if( (p = pParse->pNewTable)==0 ) return;
627 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000628 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000629 pCol = &p->aCol[i];
630 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000631 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000632 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000633 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000634 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000635 for(i=j=0; z[i]; i++){
636 int c = z[i];
637 if( isspace(c) ) continue;
638 z[j++] = c;
639 }
640 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000641// pCol->sortOrder = sqlite3CollateType(z, n);
642 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000643}
644
645/*
drh7020f652000-06-03 18:06:52 +0000646** The given token is the default value for the last column added to
647** the table currently under construction. If "minusFlag" is true, it
648** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000649**
650** This routine is called by the parser while in the middle of
651** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000652*/
danielk19774adee202004-05-08 08:23:19 +0000653void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000654 Table *p;
655 int i;
656 char **pz;
657 if( (p = pParse->pNewTable)==0 ) return;
658 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000659 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000660 pz = &p->aCol[i].zDflt;
661 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000662 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000663 }else{
danielk19774adee202004-05-08 08:23:19 +0000664 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000665 }
danielk19774adee202004-05-08 08:23:19 +0000666 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000667}
668
669/*
drh4a324312001-12-21 14:30:42 +0000670** Designate the PRIMARY KEY for the table. pList is a list of names
671** of columns that form the primary key. If pList is NULL, then the
672** most recently added column of the table is the primary key.
673**
674** A table can have at most one primary key. If the table already has
675** a primary key (and this is the second primary key) then create an
676** error.
677**
678** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
679** then we will try to use that column as the row id. (Exception:
680** For backwards compatibility with older databases, do not do this
681** if the file format version number is less than 1.) Set the Table.iPKey
682** field of the table under construction to be the index of the
683** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
684** no INTEGER PRIMARY KEY.
685**
686** If the key is not an INTEGER PRIMARY KEY, then create a unique
687** index for the key. No index is created for INTEGER PRIMARY KEYs.
688*/
danielk19774adee202004-05-08 08:23:19 +0000689void sqlite3AddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000690 Table *pTab = pParse->pNewTable;
691 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000692 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000693 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000694 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000695 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000696 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000697 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000698 }
699 pTab->hasPrimKey = 1;
700 if( pList==0 ){
701 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000702 pTab->aCol[iCol].isPrimKey = 1;
703 }else{
704 for(i=0; i<pList->nId; i++){
705 for(iCol=0; iCol<pTab->nCol; iCol++){
danielk19774adee202004-05-08 08:23:19 +0000706 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ) break;
drh78100cc2003-08-23 22:40:53 +0000707 }
708 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000709 }
drh78100cc2003-08-23 22:40:53 +0000710 if( pList->nId>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000711 }
712 if( iCol>=0 && iCol<pTab->nCol ){
713 zType = pTab->aCol[iCol].zType;
714 }
danielk19773d68f032004-05-11 07:11:51 +0000715 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000716 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000717 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000718 }else{
danielk19774adee202004-05-08 08:23:19 +0000719 sqlite3CreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000720 pList = 0;
drh4a324312001-12-21 14:30:42 +0000721 }
drhe0194f22003-02-26 13:52:51 +0000722
723primary_key_exit:
danielk19774adee202004-05-08 08:23:19 +0000724 sqlite3IdListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000725 return;
drh4a324312001-12-21 14:30:42 +0000726}
727
728/*
drhfcb78a42003-01-18 20:11:05 +0000729** Return the appropriate collating type given a type name.
730**
731** The collation type is text (SQLITE_SO_TEXT) if the type
732** name contains the character stream "text" or "blob" or
733** "clob". Any other type name is collated as numeric
734** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000735*/
danielk19774adee202004-05-08 08:23:19 +0000736int sqlite3CollateType(const char *zType, int nType){
drhfcb78a42003-01-18 20:11:05 +0000737 int i;
drhd3834012004-02-22 18:56:49 +0000738 for(i=0; i<nType-3; i++){
739 int c = *(zType++) | 0x60;
danielk19774adee202004-05-08 08:23:19 +0000740 if( (c=='b' || c=='c') && sqlite3StrNICmp(zType, "lob", 3)==0 ){
drhd3834012004-02-22 18:56:49 +0000741 return SQLITE_SO_TEXT;
742 }
danielk19774adee202004-05-08 08:23:19 +0000743 if( c=='c' && sqlite3StrNICmp(zType, "har", 3)==0 ){
drhd3834012004-02-22 18:56:49 +0000744 return SQLITE_SO_TEXT;
745 }
danielk19774adee202004-05-08 08:23:19 +0000746 if( c=='t' && sqlite3StrNICmp(zType, "ext", 3)==0 ){
drhd3834012004-02-22 18:56:49 +0000747 return SQLITE_SO_TEXT;
drhfcb78a42003-01-18 20:11:05 +0000748 }
drh8e2ca022002-06-17 17:07:19 +0000749 }
drhfcb78a42003-01-18 20:11:05 +0000750 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000751}
752
753/*
754** This routine is called by the parser while in the middle of
755** parsing a CREATE TABLE statement. A "COLLATE" clause has
756** been seen on a column. This routine sets the Column.sortOrder on
757** the column currently under construction.
758*/
danielk19774adee202004-05-08 08:23:19 +0000759void sqlite3AddCollateType(Parse *pParse, int collType){
drh8e2ca022002-06-17 17:07:19 +0000760 Table *p;
761 int i;
762 if( (p = pParse->pNewTable)==0 ) return;
763 i = p->nCol-1;
danielk1977a37cdde2004-05-16 11:15:36 +0000764
765 /* FIX ME */
766 /* if( i>=0 ) p->aCol[i].sortOrder = collType; */
767}
768
769/*
770** Parse the column type name zType (length nType) and return the
771** associated affinity type.
772*/
773char sqlite3AffinityType(const char *zType, int nType){
774 /* FIX ME: This could be done more efficiently */
775 int n, i;
776 struct {
777 const char *zSub;
778 int nSub;
779 char affinity;
780 } substrings[] = {
781 {"INT", 3, SQLITE_AFF_INTEGER},
782 {"REAL", 4, SQLITE_AFF_NUMERIC},
783 {"FLOAT", 5, SQLITE_AFF_NUMERIC},
784 {"DOUBLE", 6, SQLITE_AFF_NUMERIC},
785 {"NUM", 3, SQLITE_AFF_NUMERIC},
786 {"CHAR", 4, SQLITE_AFF_TEXT},
787 {"CLOB", 4, SQLITE_AFF_TEXT},
788 {"TEXT", 4, SQLITE_AFF_TEXT}
789 };
790
danielk1977f9dd2c22004-05-16 11:57:28 +0000791 for(n=0; n<(nType-2); n++){
danielk1977a37cdde2004-05-16 11:15:36 +0000792 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
danielk197784ac9d02004-05-18 09:58:06 +0000793 if( 0==sqlite3StrNICmp(&zType[n], substrings[i].zSub, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +0000794 return substrings[i].affinity;
795 }
796 }
797 }
798
799 return SQLITE_AFF_NONE;
drh8e2ca022002-06-17 17:07:19 +0000800}
801
802/*
drh50e5dad2001-09-15 00:57:28 +0000803** Come up with a new random value for the schema cookie. Make sure
804** the new value is different from the old.
805**
806** The schema cookie is used to determine when the schema for the
807** database changes. After each schema change, the cookie value
808** changes. When a process first reads the schema it records the
809** cookie. Thereafter, whenever it goes to access the database,
810** it checks the cookie to make sure the schema has not changed
811** since it was last read.
812**
813** This plan is not completely bullet-proof. It is possible for
814** the schema to change multiple times and for the cookie to be
815** set back to prior value. But schema changes are infrequent
816** and the probability of hitting the same cookie value is only
817** 1 chance in 2^32. So we're safe enough.
818*/
danielk19774adee202004-05-08 08:23:19 +0000819void sqlite3ChangeCookie(sqlite *db, Vdbe *v){
drh001bbcb2003-03-19 03:14:00 +0000820 if( db->next_cookie==db->aDb[0].schema_cookie ){
drhbbd82df2004-02-11 09:46:30 +0000821 unsigned char r;
danielk19774adee202004-05-08 08:23:19 +0000822 sqlite3Randomness(1, &r);
drhbbd82df2004-02-11 09:46:30 +0000823 db->next_cookie = db->aDb[0].schema_cookie + r + 1;
drh50e5dad2001-09-15 00:57:28 +0000824 db->flags |= SQLITE_InternChanges;
danielk19774adee202004-05-08 08:23:19 +0000825 sqlite3VdbeAddOp(v, OP_Integer, db->next_cookie, 0);
826 sqlite3VdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000827 }
828}
829
830/*
drh969fa7c2002-02-18 18:30:32 +0000831** Measure the number of characters needed to output the given
832** identifier. The number returned includes any quotes used
833** but does not include the null terminator.
834*/
835static int identLength(const char *z){
836 int n;
drh17f71932002-02-21 12:01:27 +0000837 int needQuote = 0;
838 for(n=0; *z; n++, z++){
839 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000840 }
drh17f71932002-02-21 12:01:27 +0000841 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000842}
843
844/*
845** Write an identifier onto the end of the given string. Add
846** quote characters as needed.
847*/
848static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000849 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000850 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000851 for(j=0; zIdent[j]; j++){
852 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
853 }
854 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +0000855 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh17f71932002-02-21 12:01:27 +0000856 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000857 for(j=0; zIdent[j]; j++){
858 z[i++] = zIdent[j];
859 if( zIdent[j]=='\'' ) z[i++] = '\'';
860 }
drh17f71932002-02-21 12:01:27 +0000861 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000862 z[i] = 0;
863 *pIdx = i;
864}
865
866/*
867** Generate a CREATE TABLE statement appropriate for the given
868** table. Memory to hold the text of the statement is obtained
869** from sqliteMalloc() and must be freed by the calling function.
870*/
871static char *createTableStmt(Table *p){
872 int i, k, n;
873 char *zStmt;
874 char *zSep, *zSep2, *zEnd;
875 n = 0;
876 for(i=0; i<p->nCol; i++){
877 n += identLength(p->aCol[i].zName);
878 }
879 n += identLength(p->zName);
880 if( n<40 ){
881 zSep = "";
882 zSep2 = ",";
883 zEnd = ")";
884 }else{
885 zSep = "\n ";
886 zSep2 = ",\n ";
887 zEnd = "\n)";
888 }
drhe0bc4042002-06-25 01:09:11 +0000889 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000890 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000891 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +0000892 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000893 k = strlen(zStmt);
894 identPut(zStmt, &k, p->zName);
895 zStmt[k++] = '(';
896 for(i=0; i<p->nCol; i++){
897 strcpy(&zStmt[k], zSep);
898 k += strlen(&zStmt[k]);
899 zSep = zSep2;
900 identPut(zStmt, &k, p->aCol[i].zName);
901 }
902 strcpy(&zStmt[k], zEnd);
903 return zStmt;
904}
905
906/*
drh75897232000-05-29 14:26:00 +0000907** This routine is called to report the final ")" that terminates
908** a CREATE TABLE statement.
909**
drhf57b3392001-10-08 13:22:32 +0000910** The table structure that other action routines have been building
911** is added to the internal hash tables, assuming no errors have
912** occurred.
drh75897232000-05-29 14:26:00 +0000913**
drh1d85d932004-02-14 23:05:52 +0000914** An entry for the table is made in the master table on disk, unless
915** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +0000916** it means we are reading the sqlite_master table because we just
917** connected to the database or because the sqlite_master table has
918** recently changes, so the entry for this table already exists in
919** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000920**
921** If the pSelect argument is not NULL, it means that this routine
922** was called to create a table generated from a
923** "CREATE TABLE ... AS SELECT ..." statement. The column names of
924** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000925*/
danielk19774adee202004-05-08 08:23:19 +0000926void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000927 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000928 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000929
danielk197724b03fd2004-05-10 10:34:34 +0000930 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000931 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000932 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000933
drh969fa7c2002-02-18 18:30:32 +0000934 /* If the table is generated from a SELECT, then construct the
935 ** list of columns and the text of the table.
936 */
937 if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +0000938 Table *pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000939 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000940 assert( p->aCol==0 );
941 p->nCol = pSelTab->nCol;
942 p->aCol = pSelTab->aCol;
943 pSelTab->nCol = 0;
944 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +0000945 sqlite3DeleteTable(0, pSelTab);
drh969fa7c2002-02-18 18:30:32 +0000946 }
947
drh1d85d932004-02-14 23:05:52 +0000948 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000949 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
950 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +0000951 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +0000952 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000953 */
drh1d85d932004-02-14 23:05:52 +0000954 if( db->init.busy ){
955 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +0000956 }
957
drhe3c41372001-09-17 20:25:58 +0000958 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000959 ** in the SQLITE_MASTER table of the database. The record number
960 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000961 **
drhe0bc4042002-06-25 01:09:11 +0000962 ** If this is a TEMPORARY table, write the entry into the auxiliary
963 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000964 */
drh1d85d932004-02-14 23:05:52 +0000965 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +0000966 int n;
drhd8bc7082000-06-07 23:51:50 +0000967 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000968
danielk19774adee202004-05-08 08:23:19 +0000969 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000970 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000971 if( p->pSelect==0 ){
972 /* A regular table */
danielk19774adee202004-05-08 08:23:19 +0000973 sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
drh4ff6dfa2002-03-03 23:06:00 +0000974 }else{
975 /* A view */
danielk19774adee202004-05-08 08:23:19 +0000976 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +0000977 }
drh969fa7c2002-02-18 18:30:32 +0000978 p->tnum = 0;
danielk19774adee202004-05-08 08:23:19 +0000979 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
980 sqlite3VdbeOp3(v, OP_String, 0, 0, p->pSelect==0?"table":"view", P3_STATIC);
981 sqlite3VdbeOp3(v, OP_String, 0, 0, p->zName, 0);
982 sqlite3VdbeOp3(v, OP_String, 0, 0, p->zName, 0);
983 sqlite3VdbeAddOp(v, OP_Dup, 4, 0);
984 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhe0bc4042002-06-25 01:09:11 +0000985 if( pSelect ){
986 char *z = createTableStmt(p);
987 n = z ? strlen(z) : 0;
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +0000989 sqliteFree(z);
990 }else{
991 assert( pEnd!=0 );
992 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
danielk19774adee202004-05-08 08:23:19 +0000993 sqlite3VdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
drhe0bc4042002-06-25 01:09:11 +0000994 }
danielk197784ac9d02004-05-18 09:58:06 +0000995 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000996 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drhd24cc422003-03-27 12:51:24 +0000997 if( !p->iDb ){
danielk19774adee202004-05-08 08:23:19 +0000998 sqlite3ChangeCookie(db, v);
drhe0bc4042002-06-25 01:09:11 +0000999 }
danielk19774adee202004-05-08 08:23:19 +00001000 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +00001001 if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001002 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1003 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +00001004 pParse->nTab = 2;
danielk197784ac9d02004-05-18 09:58:06 +00001005 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +00001006 }
danielk19774adee202004-05-08 08:23:19 +00001007 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001008 }
drh17e9e292003-02-01 13:53:28 +00001009
1010 /* Add the table to the in-memory representation of the database.
1011 */
drhd24cc422003-03-27 12:51:24 +00001012 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001013 Table *pOld;
1014 FKey *pFKey;
danielk19774adee202004-05-08 08:23:19 +00001015 pOld = sqlite3HashInsert(&db->aDb[p->iDb].tblHash,
drhd24cc422003-03-27 12:51:24 +00001016 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001017 if( pOld ){
1018 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1019 return;
1020 }
1021 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1022 int nTo = strlen(pFKey->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +00001023 pFKey->pNextTo = sqlite3HashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
1024 sqlite3HashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001025 }
1026 pParse->pNewTable = 0;
1027 db->nTable++;
1028 db->flags |= SQLITE_InternChanges;
1029 }
drh75897232000-05-29 14:26:00 +00001030}
1031
1032/*
drha76b5df2002-02-23 02:32:10 +00001033** The parser calls this routine in order to create a new VIEW
1034*/
danielk19774adee202004-05-08 08:23:19 +00001035void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001036 Parse *pParse, /* The parsing context */
1037 Token *pBegin, /* The CREATE token that begins the statement */
1038 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001039 Select *pSelect, /* A SELECT statement that will become the new view */
1040 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001041){
drha76b5df2002-02-23 02:32:10 +00001042 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001043 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001044 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001045 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001046 DbFixer sFix;
drha76b5df2002-02-23 02:32:10 +00001047
danielk19774adee202004-05-08 08:23:19 +00001048 sqlite3StartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001049 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001050 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001051 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001052 return;
1053 }
danielk19774adee202004-05-08 08:23:19 +00001054 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1055 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001056 ){
danielk19774adee202004-05-08 08:23:19 +00001057 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001058 return;
1059 }
drh174b6192002-12-03 02:22:52 +00001060
drh4b59ab52002-08-24 18:24:51 +00001061 /* Make a copy of the entire SELECT statement that defines the view.
1062 ** This will force all the Expr.token.z values to be dynamically
1063 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001064 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001065 */
danielk19774adee202004-05-08 08:23:19 +00001066 p->pSelect = sqlite3SelectDup(pSelect);
1067 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001068 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001069 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001070 }
drh4b59ab52002-08-24 18:24:51 +00001071
1072 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1073 ** the end.
1074 */
drha76b5df2002-02-23 02:32:10 +00001075 sEnd = pParse->sLastToken;
1076 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1077 sEnd.z += sEnd.n;
1078 }
1079 sEnd.n = 0;
1080 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001081 z = pBegin->z;
1082 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1083 sEnd.z = &z[n-1];
1084 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001085
danielk19774adee202004-05-08 08:23:19 +00001086 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1087 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001088 return;
drh417be792002-03-03 18:59:40 +00001089}
drha76b5df2002-02-23 02:32:10 +00001090
drh417be792002-03-03 18:59:40 +00001091/*
1092** The Table structure pTable is really a VIEW. Fill in the names of
1093** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001094** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001095*/
danielk19774adee202004-05-08 08:23:19 +00001096int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001097 ExprList *pEList;
1098 Select *pSel;
1099 Table *pSelTab;
1100 int nErr = 0;
1101
1102 assert( pTable );
1103
1104 /* A positive nCol means the columns names for this view are
1105 ** already known.
1106 */
1107 if( pTable->nCol>0 ) return 0;
1108
1109 /* A negative nCol is a special marker meaning that we are currently
1110 ** trying to compute the column names. If we enter this routine with
1111 ** a negative nCol, it means two or more views form a loop, like this:
1112 **
1113 ** CREATE VIEW one AS SELECT * FROM two;
1114 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001115 **
1116 ** Actually, this error is caught previously and so the following test
1117 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001118 */
1119 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001120 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001121 return 1;
1122 }
1123
1124 /* If we get this far, it means we need to compute the table names.
1125 */
1126 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1127 pSel = pTable->pSelect;
1128
danielk19774adee202004-05-08 08:23:19 +00001129 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001130 ** "*" elements in this list. But we will need to restore the list
1131 ** back to its original configuration afterwards, so we save a copy of
1132 ** the original in pEList.
1133 */
1134 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001135 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001136 if( pSel->pEList==0 ){
1137 pSel->pEList = pEList;
1138 return 1; /* Malloc failed */
1139 }
1140 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001141 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001142 if( pSelTab ){
1143 assert( pTable->aCol==0 );
1144 pTable->nCol = pSelTab->nCol;
1145 pTable->aCol = pSelTab->aCol;
1146 pSelTab->nCol = 0;
1147 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001148 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001149 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001150 }else{
1151 pTable->nCol = 0;
1152 nErr++;
1153 }
danielk19774adee202004-05-08 08:23:19 +00001154 sqlite3SelectUnbind(pSel);
1155 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001156 pSel->pEList = pEList;
1157 return nErr;
1158}
1159
1160/*
1161** Clear the column names from the VIEW pTable.
1162**
1163** This routine is called whenever any other table or view is modified.
1164** The view passed into this routine might depend directly or indirectly
1165** on the modified or deleted table so we need to clear the old column
1166** names so that they will be recomputed.
1167*/
1168static void sqliteViewResetColumnNames(Table *pTable){
1169 int i;
drh701a0ae2004-02-22 20:05:00 +00001170 Column *pCol;
1171 assert( pTable!=0 && pTable->pSelect!=0 );
1172 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1173 sqliteFree(pCol->zName);
1174 sqliteFree(pCol->zDflt);
1175 sqliteFree(pCol->zType);
drh417be792002-03-03 18:59:40 +00001176 }
1177 sqliteFree(pTable->aCol);
1178 pTable->aCol = 0;
1179 pTable->nCol = 0;
1180}
1181
1182/*
drh8bf8dc92003-05-17 17:35:10 +00001183** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001184*/
drhd24cc422003-03-27 12:51:24 +00001185static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001186 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001187 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001188 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001189 Table *pTab = sqliteHashData(i);
1190 if( pTab->pSelect ){
1191 sqliteViewResetColumnNames(pTab);
1192 }
1193 }
drh8bf8dc92003-05-17 17:35:10 +00001194 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001195}
1196
1197/*
drh75897232000-05-29 14:26:00 +00001198** Given a token, look up a table with that name. If not found, leave
1199** an error for the parser to find and return NULL.
1200*/
danielk19774adee202004-05-08 08:23:19 +00001201Table *sqlite3TableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001202 char *zName;
1203 Table *pTab;
danielk19774adee202004-05-08 08:23:19 +00001204 zName = sqlite3TableNameFromToken(pTok);
drhdaffd0e2001-04-11 14:28:42 +00001205 if( zName==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001206 pTab = sqlite3FindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001207 sqliteFree(zName);
1208 if( pTab==0 ){
danielk19774adee202004-05-08 08:23:19 +00001209 sqlite3ErrorMsg(pParse, "no such table: %T", pTok);
drh75897232000-05-29 14:26:00 +00001210 }
1211 return pTab;
1212}
1213
1214/*
1215** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001216** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001217*/
danielk19774adee202004-05-08 08:23:19 +00001218void sqlite3DropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001219 Table *pTable;
drh75897232000-05-29 14:26:00 +00001220 Vdbe *v;
1221 int base;
drh5edc3122001-09-13 21:53:09 +00001222 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001223 int iDb;
drh75897232000-05-29 14:26:00 +00001224
danielk197724b03fd2004-05-10 10:34:34 +00001225 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00001226 pTable = sqlite3TableFromToken(pParse, pName);
drh75897232000-05-29 14:26:00 +00001227 if( pTable==0 ) return;
drhd24cc422003-03-27 12:51:24 +00001228 iDb = pTable->iDb;
drhe22a3342003-04-22 20:30:37 +00001229 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001230#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001231 {
1232 int code;
drhe22a3342003-04-22 20:30:37 +00001233 const char *zTab = SCHEMA_TABLE(pTable->iDb);
1234 const char *zDb = db->aDb[pTable->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001235 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
drhe22a3342003-04-22 20:30:37 +00001236 return;
1237 }
drhe5f9c642003-01-13 23:27:31 +00001238 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001239 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001240 code = SQLITE_DROP_TEMP_VIEW;
1241 }else{
1242 code = SQLITE_DROP_VIEW;
1243 }
1244 }else{
drhd24cc422003-03-27 12:51:24 +00001245 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001246 code = SQLITE_DROP_TEMP_TABLE;
1247 }else{
1248 code = SQLITE_DROP_TABLE;
1249 }
1250 }
danielk19774adee202004-05-08 08:23:19 +00001251 if( sqlite3AuthCheck(pParse, code, pTable->zName, 0, zDb) ){
drhe5f9c642003-01-13 23:27:31 +00001252 return;
1253 }
danielk19774adee202004-05-08 08:23:19 +00001254 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +00001255 return;
1256 }
drhe5f9c642003-01-13 23:27:31 +00001257 }
1258#endif
drh75897232000-05-29 14:26:00 +00001259 if( pTable->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001260 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTable->zName);
drh75897232000-05-29 14:26:00 +00001261 pParse->nErr++;
1262 return;
1263 }
drh4ff6dfa2002-03-03 23:06:00 +00001264 if( isView && pTable->pSelect==0 ){
danielk19774adee202004-05-08 08:23:19 +00001265 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTable->zName);
drh4ff6dfa2002-03-03 23:06:00 +00001266 return;
1267 }
1268 if( !isView && pTable->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001269 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTable->zName);
drh4ff6dfa2002-03-03 23:06:00 +00001270 return;
1271 }
drh75897232000-05-29 14:26:00 +00001272
drh1ccde152000-06-17 13:12:39 +00001273 /* Generate code to remove the table from the master table
1274 ** on disk.
1275 */
danielk19774adee202004-05-08 08:23:19 +00001276 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001277 if( v ){
drh905793e2004-02-21 13:31:09 +00001278 static VdbeOpList dropTable[] = {
danielk19778d059842004-05-12 11:24:02 +00001279 { OP_Rewind, 0, ADDR(10), 0},
drhe0bc4042002-06-25 01:09:11 +00001280 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001281 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001282 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001283 { OP_Column, 0, 2, 0},
danielk19778d059842004-05-12 11:24:02 +00001284 { OP_Ne, 0, ADDR(9), 0},
drh75897232000-05-29 14:26:00 +00001285 { OP_Delete, 0, 0, 0},
danielk19778d059842004-05-12 11:24:02 +00001286 { OP_Rewind, 0, ADDR(10), 0},
1287 { OP_Goto, 0, ADDR(3), 0},
1288 { OP_Next, 0, ADDR(3), 0}, /* 9 */
drh75897232000-05-29 14:26:00 +00001289 };
1290 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001291 Trigger *pTrigger;
danielk19774adee202004-05-08 08:23:19 +00001292 sqlite3BeginWriteOperation(pParse, 0, pTable->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001293
danielk1977c3f9bad2002-05-15 08:30:12 +00001294 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001295 pTrigger = pTable->pTrigger;
1296 while( pTrigger ){
drh8bf8dc92003-05-17 17:35:10 +00001297 assert( pTrigger->iDb==pTable->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001298 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drhe0bc4042002-06-25 01:09:11 +00001299 if( pParse->explain ){
1300 pTrigger = pTrigger->pNext;
1301 }else{
1302 pTrigger = pTable->pTrigger;
1303 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001304 }
drh8bf8dc92003-05-17 17:35:10 +00001305
1306 /* Drop all SQLITE_MASTER entries that refer to the table */
danielk19774adee202004-05-08 08:23:19 +00001307 sqlite3OpenMasterTable(v, pTable->iDb);
1308 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
1309 sqlite3VdbeChangeP3(v, base+1, pTable->zName, 0);
drh8bf8dc92003-05-17 17:35:10 +00001310
1311 /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */
1312 if( pTable->iDb!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001313 sqlite3OpenMasterTable(v, 1);
1314 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
1315 sqlite3VdbeChangeP3(v, base+1, pTable->zName, 0);
drh8bf8dc92003-05-17 17:35:10 +00001316 }
1317
1318 if( pTable->iDb==0 ){
danielk19774adee202004-05-08 08:23:19 +00001319 sqlite3ChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001320 }
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001322 if( !isView ){
danielk19774adee202004-05-08 08:23:19 +00001323 sqlite3VdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001324 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001325 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001326 }
drh5e00f6c2001-09-13 13:46:56 +00001327 }
danielk19774adee202004-05-08 08:23:19 +00001328 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001329 }
1330
drhe0bc4042002-06-25 01:09:11 +00001331 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001332 **
1333 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001334 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001335 */
1336 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001337 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001338 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001339 }
drhd24cc422003-03-27 12:51:24 +00001340 sqliteViewResetAll(db, iDb);
drh75897232000-05-29 14:26:00 +00001341}
1342
1343/*
drhc2eef3b2002-08-31 18:53:06 +00001344** This routine is called to create a new foreign key on the table
1345** currently under construction. pFromCol determines which columns
1346** in the current table point to the foreign key. If pFromCol==0 then
1347** connect the key to the last column inserted. pTo is the name of
1348** the table referred to. pToCol is a list of tables in the other
1349** pTo table that the foreign key points to. flags contains all
1350** information about the conflict resolution algorithms specified
1351** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1352**
1353** An FKey structure is created and added to the table currently
1354** under construction in the pParse->pNewTable field. The new FKey
1355** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001356** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001357**
1358** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001359** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001360*/
danielk19774adee202004-05-08 08:23:19 +00001361void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001362 Parse *pParse, /* Parsing context */
1363 IdList *pFromCol, /* Columns in this table that point to other table */
1364 Token *pTo, /* Name of the other table */
1365 IdList *pToCol, /* Columns in the other table */
1366 int flags /* Conflict resolution algorithms. */
1367){
1368 Table *p = pParse->pNewTable;
1369 int nByte;
1370 int i;
1371 int nCol;
1372 char *z;
1373 FKey *pFKey = 0;
1374
1375 assert( pTo!=0 );
1376 if( p==0 || pParse->nErr ) goto fk_end;
1377 if( pFromCol==0 ){
1378 int iCol = p->nCol-1;
1379 if( iCol<0 ) goto fk_end;
1380 if( pToCol && pToCol->nId!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001381 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001382 " should reference only one column of table %T",
1383 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001384 goto fk_end;
1385 }
1386 nCol = 1;
1387 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001389 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001390 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001391 goto fk_end;
1392 }else{
1393 nCol = pFromCol->nId;
1394 }
1395 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1396 if( pToCol ){
1397 for(i=0; i<pToCol->nId; i++){
1398 nByte += strlen(pToCol->a[i].zName) + 1;
1399 }
1400 }
1401 pFKey = sqliteMalloc( nByte );
1402 if( pFKey==0 ) goto fk_end;
1403 pFKey->pFrom = p;
1404 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001405 z = (char*)&pFKey[1];
1406 pFKey->aCol = (struct sColMap*)z;
1407 z += sizeof(struct sColMap)*nCol;
1408 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001409 memcpy(z, pTo->z, pTo->n);
1410 z[pTo->n] = 0;
1411 z += pTo->n+1;
1412 pFKey->pNextTo = 0;
1413 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001414 if( pFromCol==0 ){
1415 pFKey->aCol[0].iFrom = p->nCol-1;
1416 }else{
1417 for(i=0; i<nCol; i++){
1418 int j;
1419 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001420 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001421 pFKey->aCol[i].iFrom = j;
1422 break;
1423 }
1424 }
1425 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001426 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001427 "unknown column \"%s\" in foreign key definition",
1428 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001429 goto fk_end;
1430 }
1431 }
1432 }
1433 if( pToCol ){
1434 for(i=0; i<nCol; i++){
1435 int n = strlen(pToCol->a[i].zName);
1436 pFKey->aCol[i].zCol = z;
1437 memcpy(z, pToCol->a[i].zName, n);
1438 z[n] = 0;
1439 z += n+1;
1440 }
1441 }
1442 pFKey->isDeferred = 0;
1443 pFKey->deleteConf = flags & 0xff;
1444 pFKey->updateConf = (flags >> 8 ) & 0xff;
1445 pFKey->insertConf = (flags >> 16 ) & 0xff;
1446
1447 /* Link the foreign key to the table as the last step.
1448 */
1449 p->pFKey = pFKey;
1450 pFKey = 0;
1451
1452fk_end:
1453 sqliteFree(pFKey);
danielk19774adee202004-05-08 08:23:19 +00001454 sqlite3IdListDelete(pFromCol);
1455 sqlite3IdListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001456}
1457
1458/*
1459** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1460** clause is seen as part of a foreign key definition. The isDeferred
1461** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1462** The behavior of the most recently created foreign key is adjusted
1463** accordingly.
1464*/
danielk19774adee202004-05-08 08:23:19 +00001465void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001466 Table *pTab;
1467 FKey *pFKey;
1468 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1469 pFKey->isDeferred = isDeferred;
1470}
1471
1472/*
drh75897232000-05-29 14:26:00 +00001473** Create a new index for an SQL table. pIndex is the name of the index
1474** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001475** be NULL for a primary key or an index that is created to satisfy a
1476** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001477** as the table to be indexed. pParse->pNewTable is a table that is
1478** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001479**
drh382c0242001-10-06 16:33:02 +00001480** pList is a list of columns to be indexed. pList will be NULL if this
1481** is a primary key or unique-constraint on the most recent column added
1482** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001483*/
danielk19774adee202004-05-08 08:23:19 +00001484void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001485 Parse *pParse, /* All information about this parse */
1486 Token *pName, /* Name of the index. May be NULL */
drhd24cc422003-03-27 12:51:24 +00001487 SrcList *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001488 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001489 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001490 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1491 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1492){
1493 Table *pTab; /* Table to be indexed */
1494 Index *pIndex; /* The index to be created */
1495 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001496 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001497 Token nullId; /* Fake token for an empty ID list */
1498 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001499 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001500 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001501
danielk197724b03fd2004-05-10 10:34:34 +00001502 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drh1d85d932004-02-14 23:05:52 +00001503 if( db->init.busy
danielk19774adee202004-05-08 08:23:19 +00001504 && sqlite3FixInit(&sFix, pParse, db->init.iDb, "index", pName)
1505 && sqlite3FixSrcList(&sFix, pTable)
drhf26e09c2003-05-31 16:21:12 +00001506 ){
1507 goto exit_create_index;
1508 }
drhdaffd0e2001-04-11 14:28:42 +00001509
drh75897232000-05-29 14:26:00 +00001510 /*
1511 ** Find the table that is to be indexed. Return early if not found.
1512 */
1513 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001514 assert( pName!=0 );
drhd24cc422003-03-27 12:51:24 +00001515 assert( pTable->nSrc==1 );
danielk19774adee202004-05-08 08:23:19 +00001516 pTab = sqlite3SrcListLookup(pParse, pTable);
drh75897232000-05-29 14:26:00 +00001517 }else{
drhe3c41372001-09-17 20:25:58 +00001518 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001519 pTab = pParse->pNewTable;
1520 }
1521 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001522 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001523 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001524 goto exit_create_index;
1525 }
drh1d85d932004-02-14 23:05:52 +00001526 if( pTab->iDb>=2 && db->init.busy==0 ){
danielk19774adee202004-05-08 08:23:19 +00001527 sqlite3ErrorMsg(pParse, "table %s may not have indices added", pTab->zName);
drh75897232000-05-29 14:26:00 +00001528 goto exit_create_index;
1529 }
drha76b5df2002-02-23 02:32:10 +00001530 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001531 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001532 goto exit_create_index;
1533 }
drh4925ca02003-11-27 00:48:57 +00001534 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001535
1536 /*
1537 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001538 ** index or table with the same name.
1539 **
1540 ** Exception: If we are reading the names of permanent indices from the
1541 ** sqlite_master table (because some other process changed the schema) and
1542 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001543 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001544 **
1545 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001546 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1547 ** own name.
drh75897232000-05-29 14:26:00 +00001548 */
drh1d85d932004-02-14 23:05:52 +00001549 if( pName && !db->init.busy ){
drhf57b3392001-10-08 13:22:32 +00001550 Index *pISameName; /* Another index with the same name */
1551 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001552 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001553 if( zName==0 ) goto exit_create_index;
danielk19774adee202004-05-08 08:23:19 +00001554 if( (pISameName = sqlite3FindIndex(db, zName, 0))!=0 ){
1555 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
drhd24cc422003-03-27 12:51:24 +00001556 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001557 }
danielk19774adee202004-05-08 08:23:19 +00001558 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1559 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
drhd24cc422003-03-27 12:51:24 +00001560 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001561 }
drhd24cc422003-03-27 12:51:24 +00001562 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001563 char zBuf[30];
1564 int n;
1565 Index *pLoop;
1566 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1567 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001568 zName = 0;
danielk19774adee202004-05-08 08:23:19 +00001569 sqlite3SetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001570 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001571 }else{
1572 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001573 }
1574
drhe5f9c642003-01-13 23:27:31 +00001575 /* Check for authorization to create an index.
1576 */
1577#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001578 {
1579 const char *zDb = db->aDb[pTab->iDb].zName;
1580
drh1d85d932004-02-14 23:05:52 +00001581 assert( pTab->iDb==db->init.iDb || isTemp );
danielk19774adee202004-05-08 08:23:19 +00001582 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001583 goto exit_create_index;
1584 }
1585 i = SQLITE_CREATE_INDEX;
1586 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001587 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001588 goto exit_create_index;
1589 }
drhe5f9c642003-01-13 23:27:31 +00001590 }
1591#endif
1592
drh75897232000-05-29 14:26:00 +00001593 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001594 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001595 ** So create a fake list to simulate this.
1596 */
1597 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001598 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001599 nullId.n = strlen(nullId.z);
danielk19774adee202004-05-08 08:23:19 +00001600 pList = sqlite3IdListAppend(0, &nullId);
drh75897232000-05-29 14:26:00 +00001601 if( pList==0 ) goto exit_create_index;
1602 }
1603
1604 /*
1605 ** Allocate the index structure.
1606 */
drhdcc581c2000-05-30 13:44:19 +00001607 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001608 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001609 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001610 pIndex->aiColumn = (int*)&pIndex[1];
1611 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001612 strcpy(pIndex->zName, zName);
1613 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001614 pIndex->nColumn = pList->nId;
drhea1ba172003-04-20 00:00:23 +00001615 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001616 pIndex->autoIndex = pName==0;
drh1d85d932004-02-14 23:05:52 +00001617 pIndex->iDb = isTemp ? 1 : db->init.iDb;
drh75897232000-05-29 14:26:00 +00001618
drh1ccde152000-06-17 13:12:39 +00001619 /* Scan the names of the columns of the table to be indexed and
1620 ** load the column indices into the Index structure. Report an error
1621 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001622 */
1623 for(i=0; i<pList->nId; i++){
1624 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001625 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001626 }
1627 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001628 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00001629 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001630 sqliteFree(pIndex);
1631 goto exit_create_index;
1632 }
drh967e8b72000-06-21 13:59:10 +00001633 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001634 }
1635
1636 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001637 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001638 */
drhd24cc422003-03-27 12:51:24 +00001639 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001640 Index *p;
danielk19774adee202004-05-08 08:23:19 +00001641 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00001642 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001643 if( p ){
1644 assert( p==pIndex ); /* Malloc must have failed */
1645 sqliteFree(pIndex);
1646 goto exit_create_index;
1647 }
drh5e00f6c2001-09-13 13:46:56 +00001648 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001649 }
drh9cfcf5d2002-01-29 18:41:24 +00001650
1651 /* When adding an index to the list of indices for a table, make
1652 ** sure all indices labeled OE_Replace come after all those labeled
1653 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1654 ** and INSERT.
1655 */
1656 if( onError!=OE_Replace || pTab->pIndex==0
1657 || pTab->pIndex->onError==OE_Replace){
1658 pIndex->pNext = pTab->pIndex;
1659 pTab->pIndex = pIndex;
1660 }else{
1661 Index *pOther = pTab->pIndex;
1662 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1663 pOther = pOther->pNext;
1664 }
1665 pIndex->pNext = pOther->pNext;
1666 pOther->pNext = pIndex;
1667 }
drh75897232000-05-29 14:26:00 +00001668
drh1d85d932004-02-14 23:05:52 +00001669 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhd78eeee2001-09-13 16:18:53 +00001670 ** "sqlite_master" table on the disk. So do not write to the disk
drh1d85d932004-02-14 23:05:52 +00001671 ** again. Extract the table number from the db->init.newTnum field.
drhd78eeee2001-09-13 16:18:53 +00001672 */
drh1d85d932004-02-14 23:05:52 +00001673 if( db->init.busy && pTable!=0 ){
1674 pIndex->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001675 }
1676
drh1d85d932004-02-14 23:05:52 +00001677 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00001678 ** involves writing the index into the master table and filling in the
1679 ** index with the current table contents.
1680 **
drh1d85d932004-02-14 23:05:52 +00001681 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
1682 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00001683 ** CREATE INDEX statements are read out of the master table. In
1684 ** the latter case the index already exists on disk, which is why
1685 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001686 **
1687 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001688 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1689 ** has just been created, it contains no data and the index initialization
1690 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001691 */
drh1d85d932004-02-14 23:05:52 +00001692 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00001693 int n;
drhadbca9c2001-09-27 15:11:53 +00001694 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001695 int lbl1, lbl2;
1696 int i;
drhadbca9c2001-09-27 15:11:53 +00001697 int addr;
drh75897232000-05-29 14:26:00 +00001698
danielk19774adee202004-05-08 08:23:19 +00001699 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001700 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001701 if( pTable!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001702 sqlite3BeginWriteOperation(pParse, 0, isTemp);
1703 sqlite3OpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001704 }
danielk19774adee202004-05-08 08:23:19 +00001705 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
1706 sqlite3VdbeOp3(v, OP_String, 0, 0, "index", P3_STATIC);
1707 sqlite3VdbeOp3(v, OP_String, 0, 0, pIndex->zName, 0);
1708 sqlite3VdbeOp3(v, OP_String, 0, 0, pTab->zName, 0);
1709 sqlite3VdbeOp3(v, OP_CreateIndex, 0, isTemp,(char*)&pIndex->tnum,P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001710 pIndex->tnum = 0;
1711 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +00001712 sqlite3VdbeCode(v,
drh701a0ae2004-02-22 20:05:00 +00001713 OP_Dup, 0, 0,
1714 OP_Integer, isTemp, 0,
1715 OP_OpenWrite, 1, 0,
1716 0);
drh5e00f6c2001-09-13 13:46:56 +00001717 }
danielk19774adee202004-05-08 08:23:19 +00001718 addr = sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00001719 if( pStart && pEnd ){
1720 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
danielk19774adee202004-05-08 08:23:19 +00001721 sqlite3VdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001722 }
danielk197784ac9d02004-05-18 09:58:06 +00001723 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001724 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001725 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +00001726 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
1727 sqlite3VdbeOp3(v, OP_OpenRead, 2, pTab->tnum, pTab->zName, 0);
danielk1977b4964b72004-05-18 01:23:38 +00001728 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00001729 lbl2 = sqlite3VdbeMakeLabel(v);
1730 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
1731 lbl1 = sqlite3VdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001732 for(i=0; i<pIndex->nColumn; i++){
drh56e452c2003-05-01 16:56:03 +00001733 int iCol = pIndex->aiColumn[i];
1734 if( pTab->iPKey==iCol ){
danielk19774adee202004-05-08 08:23:19 +00001735 sqlite3VdbeAddOp(v, OP_Dup, i, 0);
drh56e452c2003-05-01 16:56:03 +00001736 }else{
danielk19774adee202004-05-08 08:23:19 +00001737 sqlite3VdbeAddOp(v, OP_Column, 2, iCol);
drh56e452c2003-05-01 16:56:03 +00001738 }
drhadbca9c2001-09-27 15:11:53 +00001739 }
danielk19774adee202004-05-08 08:23:19 +00001740 sqlite3VdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001741 sqlite3IndexAffinityStr(v, pIndex);
danielk19774adee202004-05-08 08:23:19 +00001742 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00001743 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001744 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
1745 sqlite3VdbeResolveLabel(v, lbl2);
1746 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
1747 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001748 }
drhadbca9c2001-09-27 15:11:53 +00001749 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001750 if( !isTemp ){
danielk19774adee202004-05-08 08:23:19 +00001751 sqlite3ChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001752 }
danielk19774adee202004-05-08 08:23:19 +00001753 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1754 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001755 }
drh75897232000-05-29 14:26:00 +00001756 }
1757
drh75897232000-05-29 14:26:00 +00001758 /* Clean up before exiting */
1759exit_create_index:
danielk19774adee202004-05-08 08:23:19 +00001760 sqlite3IdListDelete(pList);
1761 sqlite3SrcListDelete(pTable);
drh75897232000-05-29 14:26:00 +00001762 sqliteFree(zName);
1763 return;
1764}
1765
1766/*
drh74e24cd2002-01-09 03:19:59 +00001767** This routine will drop an existing named index. This routine
1768** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001769*/
danielk19774adee202004-05-08 08:23:19 +00001770void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001771 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001772 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001773 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001774
danielk197724b03fd2004-05-10 10:34:34 +00001775 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001776 assert( pName->nSrc==1 );
danielk19774adee202004-05-08 08:23:19 +00001777 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001778 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drhd24cc422003-03-27 12:51:24 +00001780 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001781 }
drh485b39b2002-07-13 03:11:52 +00001782 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00001783 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00001784 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00001785 goto exit_drop_index;
1786 }
1787 if( pIndex->iDb>1 ){
danielk19774adee202004-05-08 08:23:19 +00001788 sqlite3ErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00001789 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00001790 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001791 }
drhe5f9c642003-01-13 23:27:31 +00001792#ifndef SQLITE_OMIT_AUTHORIZATION
1793 {
1794 int code = SQLITE_DROP_INDEX;
1795 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00001796 const char *zDb = db->aDb[pIndex->iDb].zName;
1797 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00001798 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001799 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001800 }
drhd24cc422003-03-27 12:51:24 +00001801 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001802 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001803 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001804 }
drhed6c8672003-01-12 18:02:16 +00001805 }
drhe5f9c642003-01-13 23:27:31 +00001806#endif
drh75897232000-05-29 14:26:00 +00001807
1808 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00001809 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001810 if( v ){
drh905793e2004-02-21 13:31:09 +00001811 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001812 { OP_Rewind, 0, ADDR(9), 0},
1813 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001814 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001815 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001816 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001817 { OP_Eq, 0, ADDR(8), 0},
1818 { OP_Next, 0, ADDR(3), 0},
1819 { OP_Goto, 0, ADDR(9), 0},
1820 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001821 };
1822 int base;
1823
danielk19774adee202004-05-08 08:23:19 +00001824 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
1825 sqlite3OpenMasterTable(v, pIndex->iDb);
1826 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1827 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001828 if( pIndex->iDb==0 ){
danielk19774adee202004-05-08 08:23:19 +00001829 sqlite3ChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001830 }
danielk19774adee202004-05-08 08:23:19 +00001831 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1832 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
1833 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001834 }
1835
drhe0bc4042002-06-25 01:09:11 +00001836 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001837 */
1838 if( !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +00001839 sqlite3UnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001840 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001841 }
drhd24cc422003-03-27 12:51:24 +00001842
1843exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00001844 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001845}
1846
1847/*
drh75897232000-05-29 14:26:00 +00001848** Append a new element to the given IdList. Create a new IdList if
1849** need be.
drhdaffd0e2001-04-11 14:28:42 +00001850**
1851** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001852*/
danielk19774adee202004-05-08 08:23:19 +00001853IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00001854 if( pList==0 ){
1855 pList = sqliteMalloc( sizeof(IdList) );
1856 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001857 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00001858 }
drh4305d102003-07-30 12:34:12 +00001859 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00001860 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00001861 pList->nAlloc = pList->nAlloc*2 + 5;
1862 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00001863 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00001864 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00001865 return 0;
drh75897232000-05-29 14:26:00 +00001866 }
drh6d4abfb2001-10-22 02:58:08 +00001867 pList->a = a;
drh75897232000-05-29 14:26:00 +00001868 }
1869 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1870 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001871 char **pz = &pList->a[pList->nId].zName;
danielk19774adee202004-05-08 08:23:19 +00001872 sqlite3SetNString(pz, pToken->z, pToken->n, 0);
drhdaffd0e2001-04-11 14:28:42 +00001873 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00001874 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00001875 return 0;
1876 }else{
danielk19774adee202004-05-08 08:23:19 +00001877 sqlite3Dequote(*pz);
drhdaffd0e2001-04-11 14:28:42 +00001878 }
drh75897232000-05-29 14:26:00 +00001879 }
1880 pList->nId++;
1881 return pList;
1882}
1883
1884/*
drhad3cab52002-05-24 02:04:32 +00001885** Append a new table name to the given SrcList. Create a new SrcList if
1886** need be. A new entry is created in the SrcList even if pToken is NULL.
1887**
1888** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00001889**
1890** If pDatabase is not null, it means that the table has an optional
1891** database name prefix. Like this: "database.table". The pDatabase
1892** points to the table name and the pTable points to the database name.
1893** The SrcList.a[].zName field is filled with the table name which might
1894** come from pTable (if pDatabase is NULL) or from pDatabase.
1895** SrcList.a[].zDatabase is filled with the database name from pTable,
1896** or with NULL if no database is specified.
1897**
1898** In other words, if call like this:
1899**
danielk19774adee202004-05-08 08:23:19 +00001900** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00001901**
1902** Then B is a table name and the database name is unspecified. If called
1903** like this:
1904**
danielk19774adee202004-05-08 08:23:19 +00001905** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00001906**
1907** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00001908*/
danielk19774adee202004-05-08 08:23:19 +00001909SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00001910 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00001911 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00001912 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001913 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00001914 }
drh4305d102003-07-30 12:34:12 +00001915 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00001916 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00001917 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00001918 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00001919 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00001920 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00001921 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00001922 return 0;
1923 }
drh113088e2003-03-20 01:16:58 +00001924 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00001925 }
1926 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00001927 if( pDatabase && pDatabase->z==0 ){
1928 pDatabase = 0;
1929 }
1930 if( pDatabase && pTable ){
1931 Token *pTemp = pDatabase;
1932 pDatabase = pTable;
1933 pTable = pTemp;
1934 }
1935 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00001936 char **pz = &pList->a[pList->nSrc].zName;
danielk19774adee202004-05-08 08:23:19 +00001937 sqlite3SetNString(pz, pTable->z, pTable->n, 0);
drh113088e2003-03-20 01:16:58 +00001938 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00001939 sqlite3SrcListDelete(pList);
drh113088e2003-03-20 01:16:58 +00001940 return 0;
1941 }else{
danielk19774adee202004-05-08 08:23:19 +00001942 sqlite3Dequote(*pz);
drh113088e2003-03-20 01:16:58 +00001943 }
1944 }
1945 if( pDatabase ){
1946 char **pz = &pList->a[pList->nSrc].zDatabase;
danielk19774adee202004-05-08 08:23:19 +00001947 sqlite3SetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00001948 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00001949 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00001950 return 0;
1951 }else{
danielk19774adee202004-05-08 08:23:19 +00001952 sqlite3Dequote(*pz);
drhad3cab52002-05-24 02:04:32 +00001953 }
1954 }
drh63eb5f22003-04-29 16:20:44 +00001955 pList->a[pList->nSrc].iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00001956 pList->nSrc++;
1957 return pList;
1958}
1959
1960/*
drh63eb5f22003-04-29 16:20:44 +00001961** Assign cursors to all tables in a SrcList
1962*/
danielk19774adee202004-05-08 08:23:19 +00001963void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00001964 int i;
1965 for(i=0; i<pList->nSrc; i++){
1966 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00001967 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00001968 }
1969 }
1970}
1971
1972/*
drh75897232000-05-29 14:26:00 +00001973** Add an alias to the last identifier on the given identifier list.
1974*/
danielk19774adee202004-05-08 08:23:19 +00001975void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00001976 if( pList && pList->nSrc>0 ){
1977 int i = pList->nSrc - 1;
danielk19774adee202004-05-08 08:23:19 +00001978 sqlite3SetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
1979 sqlite3Dequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001980 }
1981}
1982
1983/*
drhad3cab52002-05-24 02:04:32 +00001984** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001985*/
danielk19774adee202004-05-08 08:23:19 +00001986void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00001987 int i;
1988 if( pList==0 ) return;
1989 for(i=0; i<pList->nId; i++){
1990 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001991 }
1992 sqliteFree(pList->a);
1993 sqliteFree(pList);
1994}
1995
1996/*
drhad2d8302002-05-24 20:31:36 +00001997** Return the index in pList of the identifier named zId. Return -1
1998** if not found.
1999*/
danielk19774adee202004-05-08 08:23:19 +00002000int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002001 int i;
2002 if( pList==0 ) return -1;
2003 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002004 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002005 }
2006 return -1;
2007}
2008
2009/*
drhad3cab52002-05-24 02:04:32 +00002010** Delete an entire SrcList including all its substructure.
2011*/
danielk19774adee202004-05-08 08:23:19 +00002012void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002013 int i;
2014 if( pList==0 ) return;
2015 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002016 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002017 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002018 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002019 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
danielk19774adee202004-05-08 08:23:19 +00002020 sqlite3DeleteTable(0, pList->a[i].pTab);
drhdaffd0e2001-04-11 14:28:42 +00002021 }
danielk19774adee202004-05-08 08:23:19 +00002022 sqlite3SelectDelete(pList->a[i].pSelect);
2023 sqlite3ExprDelete(pList->a[i].pOn);
2024 sqlite3IdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002025 }
drh75897232000-05-29 14:26:00 +00002026 sqliteFree(pList);
2027}
2028
drh982cef72000-05-30 16:27:03 +00002029/*
drhc4a3c772001-04-04 11:48:57 +00002030** Begin a transaction
2031*/
danielk19774adee202004-05-08 08:23:19 +00002032void sqlite3BeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002033 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002034
drh001bbcb2003-03-19 03:14:00 +00002035 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002036 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002037 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002038 if( db->flags & SQLITE_InTrans ){
danielk19774adee202004-05-08 08:23:19 +00002039 sqlite3ErrorMsg(pParse, "cannot start a transaction within a transaction");
drh6b8b8742002-08-18 20:28:06 +00002040 return;
2041 }
danielk19774adee202004-05-08 08:23:19 +00002042 sqlite3BeginWriteOperation(pParse, 0, 0);
drh02f75f12004-02-24 01:04:11 +00002043 if( !pParse->explain ){
2044 db->flags |= SQLITE_InTrans;
2045 db->onError = onError;
2046 }
drhc4a3c772001-04-04 11:48:57 +00002047}
2048
2049/*
2050** Commit a transaction
2051*/
danielk19774adee202004-05-08 08:23:19 +00002052void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002053 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002054
drh001bbcb2003-03-19 03:14:00 +00002055 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002056 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002057 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002058 if( (db->flags & SQLITE_InTrans)==0 ){
danielk19774adee202004-05-08 08:23:19 +00002059 sqlite3ErrorMsg(pParse, "cannot commit - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002060 return;
2061 }
drh02f75f12004-02-24 01:04:11 +00002062 if( !pParse->explain ){
2063 db->flags &= ~SQLITE_InTrans;
2064 }
danielk19774adee202004-05-08 08:23:19 +00002065 sqlite3EndWriteOperation(pParse);
drh02f75f12004-02-24 01:04:11 +00002066 if( !pParse->explain ){
2067 db->onError = OE_Default;
2068 }
drhc4a3c772001-04-04 11:48:57 +00002069}
2070
2071/*
2072** Rollback a transaction
2073*/
danielk19774adee202004-05-08 08:23:19 +00002074void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002075 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002076 Vdbe *v;
2077
drh001bbcb2003-03-19 03:14:00 +00002078 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002079 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002080 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002081 if( (db->flags & SQLITE_InTrans)==0 ){
danielk19774adee202004-05-08 08:23:19 +00002082 sqlite3ErrorMsg(pParse, "cannot rollback - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002083 return;
2084 }
danielk19774adee202004-05-08 08:23:19 +00002085 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002086 if( v ){
danielk19774adee202004-05-08 08:23:19 +00002087 sqlite3VdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002088 }
drh02f75f12004-02-24 01:04:11 +00002089 if( !pParse->explain ){
2090 db->flags &= ~SQLITE_InTrans;
2091 db->onError = OE_Default;
2092 }
drhc4a3c772001-04-04 11:48:57 +00002093}
drhf57b14a2001-09-14 18:54:08 +00002094
2095/*
drh001bbcb2003-03-19 03:14:00 +00002096** Generate VDBE code that will verify the schema cookie for all
2097** named database files.
2098*/
danielk19774adee202004-05-08 08:23:19 +00002099void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh001bbcb2003-03-19 03:14:00 +00002100 sqlite *db = pParse->db;
danielk19774adee202004-05-08 08:23:19 +00002101 Vdbe *v = sqlite3GetVdbe(pParse);
drh8bf8dc92003-05-17 17:35:10 +00002102 assert( iDb>=0 && iDb<db->nDb );
2103 assert( db->aDb[iDb].pBt!=0 );
2104 if( iDb!=1 && !DbHasProperty(db, iDb, DB_Cookie) ){
danielk19774adee202004-05-08 08:23:19 +00002105 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
drh8bf8dc92003-05-17 17:35:10 +00002106 DbSetProperty(db, iDb, DB_Cookie);
drh001bbcb2003-03-19 03:14:00 +00002107 }
drh001bbcb2003-03-19 03:14:00 +00002108}
2109
2110/*
drh1c928532002-01-31 15:54:21 +00002111** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002112** might change the database.
2113**
2114** This routine starts a new transaction if we are not already within
2115** a transaction. If we are already within a transaction, then a checkpoint
2116** is set if the setCheckpoint parameter is true. A checkpoint should
2117** be set for operations that might fail (due to a constraint) part of
2118** the way through and which will need to undo some writes without having to
2119** rollback the whole transaction. For operations where all constraints
2120** can be checked before any changes are made to the database, it is never
2121** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002122**
drh8bf8dc92003-05-17 17:35:10 +00002123** Only database iDb and the temp database are made writable by this call.
2124** If iDb==0, then the main and temp databases are made writable. If
2125** iDb==1 then only the temp database is made writable. If iDb>1 then the
2126** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002127*/
danielk19774adee202004-05-08 08:23:19 +00002128void sqlite3BeginWriteOperation(Parse *pParse, int setCheckpoint, int iDb){
drh663fc632002-02-02 18:49:19 +00002129 Vdbe *v;
drh8bf8dc92003-05-17 17:35:10 +00002130 sqlite *db = pParse->db;
2131 if( DbHasProperty(db, iDb, DB_Locked) ) return;
danielk19774adee202004-05-08 08:23:19 +00002132 v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002133 if( v==0 ) return;
drh8bf8dc92003-05-17 17:35:10 +00002134 if( !db->aDb[iDb].inTrans ){
danielk19774adee202004-05-08 08:23:19 +00002135 sqlite3VdbeAddOp(v, OP_Transaction, iDb, 0);
drh8bf8dc92003-05-17 17:35:10 +00002136 DbSetProperty(db, iDb, DB_Locked);
danielk19774adee202004-05-08 08:23:19 +00002137 sqlite3CodeVerifySchema(pParse, iDb);
drh8bf8dc92003-05-17 17:35:10 +00002138 if( iDb!=1 ){
danielk19774adee202004-05-08 08:23:19 +00002139 sqlite3BeginWriteOperation(pParse, setCheckpoint, 1);
drhcabb0812002-09-14 13:47:32 +00002140 }
drhc977f7f2002-05-21 11:38:11 +00002141 }else if( setCheckpoint ){
danielk19774adee202004-05-08 08:23:19 +00002142 sqlite3VdbeAddOp(v, OP_Checkpoint, iDb, 0);
drh8bf8dc92003-05-17 17:35:10 +00002143 DbSetProperty(db, iDb, DB_Locked);
drh663fc632002-02-02 18:49:19 +00002144 }
2145}
2146
2147/*
drh1c928532002-01-31 15:54:21 +00002148** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002149** the database. If a statement transaction was started, then emit
2150** an OP_Commit that will cause the changes to be committed to disk.
2151**
2152** Note that checkpoints are automatically committed at the end of
2153** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002154** sqlite3BeginWriteOperation() but there should only be a single
2155** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002156*/
danielk19774adee202004-05-08 08:23:19 +00002157void sqlite3EndWriteOperation(Parse *pParse){
drh1c928532002-01-31 15:54:21 +00002158 Vdbe *v;
drh8bf8dc92003-05-17 17:35:10 +00002159 sqlite *db = pParse->db;
danielk1977f29ce552002-05-19 23:43:12 +00002160 if( pParse->trigStack ) return; /* if this is in a trigger */
danielk19774adee202004-05-08 08:23:19 +00002161 v = sqlite3GetVdbe(pParse);
drh1c928532002-01-31 15:54:21 +00002162 if( v==0 ) return;
drh8bf8dc92003-05-17 17:35:10 +00002163 if( db->flags & SQLITE_InTrans ){
2164 /* A BEGIN has executed. Do not commit until we see an explicit
2165 ** COMMIT statement. */
drh1c928532002-01-31 15:54:21 +00002166 }else{
danielk19774adee202004-05-08 08:23:19 +00002167 sqlite3VdbeAddOp(v, OP_Commit, 0, 0);
drh1c928532002-01-31 15:54:21 +00002168 }
2169}
danielk19774adee202004-05-08 08:23:19 +00002170
2171
2172