blob: 6aeb8cbd829a6e9652d9d4b49b1ea8467a5c3243 [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**
danielk19771d850a72004-05-31 08:26:49 +000026** $Id: build.c,v 1.201 2004/05/31 08:26:49 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/*
danielk1977cbb18d22004-05-28 11:37:27 +0000402** Open the sqlite_master table stored in database number iDb for
403** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000404*/
danielk1977cbb18d22004-05-28 11:37:27 +0000405void sqlite3OpenMasterTable(Vdbe *v, int iDb){
406 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000407 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000408 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000409}
410
411/*
danielk1977cbb18d22004-05-28 11:37:27 +0000412** The token *pName contains the name of a database (either "main" or
413** "temp" or the name of an attached db). This routine returns the
414** index of the named database in db->aDb[], or -1 if the named db
415** does not exist.
416*/
417int findDb(sqlite3 *db, Token *pName){
418 int i;
419 for(i=0; i<db->nDb; i++){
420 if( pName->n==strlen(db->aDb[i].zName) &&
421 0==sqlite3StrNICmp(db->aDb[i].zName, pName->z, pName->n) ){
422 return i;
423 }
424 }
425 return -1;
426}
427
danielk1977ef2cb632004-05-29 02:37:19 +0000428int sqlite3TwoPartName(
danielk1977cbb18d22004-05-28 11:37:27 +0000429 Parse *pParse,
430 Token *pName1,
431 Token *pName2,
432 Token **pUnqual
433){
434 int iDb;
435 sqlite3 *db = pParse->db;
436
437 if( pName2 && pName2->n>0 ){
438 assert( !db->init.busy );
439 *pUnqual = pName2;
440 iDb = findDb(db, pName1);
441 if( iDb<0 ){
442 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
443 pParse->nErr++;
444 return -1;
445 }
446 }else{
447 assert( db->init.iDb==0 || db->init.busy );
448 iDb = db->init.iDb;
449 *pUnqual = pName1;
450 }
451 return iDb;
452}
453
454/*
drh75897232000-05-29 14:26:00 +0000455** Begin constructing a new table representation in memory. This is
456** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000457** to a CREATE TABLE statement. In particular, this routine is called
458** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000459** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000460** flag is true if the table should be stored in the auxiliary database
461** file instead of in the main database file. This is normally the case
462** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000463** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000464**
drhf57b3392001-10-08 13:22:32 +0000465** The new table record is initialized and put in pParse->pNewTable.
466** As more of the CREATE TABLE statement is parsed, additional action
467** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000468** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000469** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000470*/
danielk19774adee202004-05-08 08:23:19 +0000471void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000472 Parse *pParse, /* Parser context */
473 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000474 Token *pName1, /* First part of the name of the table or view */
475 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000476 int isTemp, /* True if this is a TEMP table */
477 int isView /* True if this is a VIEW */
478){
drh75897232000-05-29 14:26:00 +0000479 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000480 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000481 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000482 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000483 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000484 int iDb; /* Database number to create the table in */
485 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000486
danielk1977cbb18d22004-05-28 11:37:27 +0000487 /* The table or view name to create is passed to this routine via tokens
488 ** pName1 and pName2. If the table name was fully qualified, for example:
489 **
490 ** CREATE TABLE xxx.yyy (...);
491 **
492 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
493 ** the table name is not fully qualified, i.e.:
494 **
495 ** CREATE TABLE yyy(...);
496 **
497 ** Then pName1 is set to "yyy" and pName2 is "".
498 **
499 ** The call below sets the pName pointer to point at the token (pName1 or
500 ** pName2) that stores the unqualified table name. The variable iDb is
501 ** set to the index of the database that the table or view is to be
502 ** created in.
503 */
danielk1977ef2cb632004-05-29 02:37:19 +0000504 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000505 if( iDb<0 ) return;
506 if( isTemp && iDb>1 ){
507 /* If creating a temp table, the name may not be qualified */
508 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
509 pParse->nErr++;
510 return;
511 }
512 if( isTemp ) iDb = 1;
513
514 pParse->sNameToken = *pName;
danielk19774adee202004-05-08 08:23:19 +0000515 zName = sqlite3TableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000516 if( zName==0 ) return;
drh1d85d932004-02-14 23:05:52 +0000517 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000518#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000519 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000520 {
521 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000522 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000523 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000524 sqliteFree(zName);
525 return;
526 }
drhe5f9c642003-01-13 23:27:31 +0000527 if( isView ){
528 if( isTemp ){
529 code = SQLITE_CREATE_TEMP_VIEW;
530 }else{
531 code = SQLITE_CREATE_VIEW;
532 }
533 }else{
534 if( isTemp ){
535 code = SQLITE_CREATE_TEMP_TABLE;
536 }else{
537 code = SQLITE_CREATE_TABLE;
538 }
539 }
danielk19774adee202004-05-08 08:23:19 +0000540 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000541 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000542 return;
543 }
544 }
545#endif
drhf57b3392001-10-08 13:22:32 +0000546
547 /* Before trying to create a temporary table, make sure the Btree for
548 ** holding temporary tables is open.
549 */
drhd24cc422003-03-27 12:51:24 +0000550 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +0000551 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000552 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000553 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
drhf7a9e1a2004-02-22 18:40:56 +0000554 "file for storing temporary tables");
drhf57b3392001-10-08 13:22:32 +0000555 pParse->nErr++;
556 return;
557 }
558 if( db->flags & SQLITE_InTrans ){
danielk19774adee202004-05-08 08:23:19 +0000559 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000560 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000561 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
drhf7a9e1a2004-02-22 18:40:56 +0000562 "the temporary database file");
drhf57b3392001-10-08 13:22:32 +0000563 return;
564 }
565 }
566 }
567
568 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000569 ** index or table name in the same database. Issue an error message if
570 ** it does.
drhf57b3392001-10-08 13:22:32 +0000571 */
danielk19773df6b252004-05-29 10:23:19 +0000572 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
573 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000574 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000575 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000576 return;
drh75897232000-05-29 14:26:00 +0000577 }
danielk19774adee202004-05-08 08:23:19 +0000578 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
drh1d85d932004-02-14 23:05:52 +0000579 (pIdx->iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000580 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000581 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000582 return;
583 }
584 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000585 if( pTable==0 ){
586 sqliteFree(zName);
587 return;
588 }
drh75897232000-05-29 14:26:00 +0000589 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000590 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000591 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000592 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000593 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000594 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000595 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000596 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000597
598 /* Begin generating the code that will insert the table record into
599 ** the SQLITE_MASTER table. Note in particular that we must go ahead
600 ** and allocate the record number for the table entry now. Before any
601 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
602 ** indices to be created and the table record must come before the
603 ** indices. Hence, the record number for the table must be allocated
604 ** now.
605 */
danielk19774adee202004-05-08 08:23:19 +0000606 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000607 sqlite3BeginWriteOperation(pParse, 0, iDb);
drhf57b3392001-10-08 13:22:32 +0000608 if( !isTemp ){
danielk1977cbb18d22004-05-28 11:37:27 +0000609 /* Every time a new table is created the file-format
610 ** and encoding meta-values are set in the database, in
611 ** case this is the first table created.
612 */
danielk19774adee202004-05-08 08:23:19 +0000613 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000614 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
danielk1977172bc392004-05-22 08:09:11 +0000615 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000616 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
drhf57b3392001-10-08 13:22:32 +0000617 }
danielk1977cbb18d22004-05-28 11:37:27 +0000618 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000619 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
620 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000621 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000622 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000623 }
drh75897232000-05-29 14:26:00 +0000624}
625
626/*
627** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000628**
629** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000630** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000631** first to get things going. Then this routine is called for each
632** column.
drh75897232000-05-29 14:26:00 +0000633*/
danielk19774adee202004-05-08 08:23:19 +0000634void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000635 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000636 int i;
637 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000638 Column *pCol;
drh75897232000-05-29 14:26:00 +0000639 if( (p = pParse->pNewTable)==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000640 sqlite3SetNString(&z, pName->z, pName->n, 0);
drh97fc3d02002-05-22 21:27:03 +0000641 if( z==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000642 sqlite3Dequote(z);
drh97fc3d02002-05-22 21:27:03 +0000643 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000644 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
645 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000646 sqliteFree(z);
647 return;
648 }
649 }
drh75897232000-05-29 14:26:00 +0000650 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000651 Column *aNew;
652 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
653 if( aNew==0 ) return;
654 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000655 }
drhc9b84a12002-06-20 11:36:48 +0000656 pCol = &p->aCol[p->nCol];
657 memset(pCol, 0, sizeof(p->aCol[0]));
658 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000659
660 /* If there is no type specified, columns have the default affinity
drhe2ea40d2004-05-20 12:41:19 +0000661 ** 'NUMERIC'. If there is a type specified, then sqlite3AddColumnType()
danielk1977a37cdde2004-05-16 11:15:36 +0000662 ** will be called next to set pCol->affinity correctly.
663 */
drhe2ea40d2004-05-20 12:41:19 +0000664 pCol->affinity = SQLITE_AFF_NUMERIC;
drhd3d39e92004-05-20 22:16:29 +0000665 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000666 p->nCol++;
drh75897232000-05-29 14:26:00 +0000667}
668
669/*
drh382c0242001-10-06 16:33:02 +0000670** This routine is called by the parser while in the middle of
671** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
672** been seen on a column. This routine sets the notNull flag on
673** the column currently under construction.
674*/
danielk19774adee202004-05-08 08:23:19 +0000675void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000676 Table *p;
677 int i;
678 if( (p = pParse->pNewTable)==0 ) return;
679 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000680 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000681}
682
683/*
684** This routine is called by the parser while in the middle of
685** parsing a CREATE TABLE statement. The pFirst token is the first
686** token in the sequence of tokens that describe the type of the
687** column currently under construction. pLast is the last token
688** in the sequence. Use this information to construct a string
689** that contains the typename of the column and store that string
690** in zType.
691*/
danielk19774adee202004-05-08 08:23:19 +0000692void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000693 Table *p;
694 int i, j;
695 int n;
696 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000697 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000698 if( (p = pParse->pNewTable)==0 ) return;
699 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000700 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000701 pCol = &p->aCol[i];
702 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000703 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000704 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000705 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000706 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000707 for(i=j=0; z[i]; i++){
708 int c = z[i];
709 if( isspace(c) ) continue;
710 z[j++] = c;
711 }
712 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000713// pCol->sortOrder = sqlite3CollateType(z, n);
714 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000715}
716
717/*
drh7020f652000-06-03 18:06:52 +0000718** The given token is the default value for the last column added to
719** the table currently under construction. If "minusFlag" is true, it
720** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000721**
722** This routine is called by the parser while in the middle of
723** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000724*/
danielk19774adee202004-05-08 08:23:19 +0000725void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000726 Table *p;
727 int i;
728 char **pz;
729 if( (p = pParse->pNewTable)==0 ) return;
730 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000731 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000732 pz = &p->aCol[i].zDflt;
733 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000734 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000735 }else{
danielk19774adee202004-05-08 08:23:19 +0000736 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000737 }
danielk19774adee202004-05-08 08:23:19 +0000738 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000739}
740
741/*
drh4a324312001-12-21 14:30:42 +0000742** Designate the PRIMARY KEY for the table. pList is a list of names
743** of columns that form the primary key. If pList is NULL, then the
744** most recently added column of the table is the primary key.
745**
746** A table can have at most one primary key. If the table already has
747** a primary key (and this is the second primary key) then create an
748** error.
749**
750** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
751** then we will try to use that column as the row id. (Exception:
752** For backwards compatibility with older databases, do not do this
753** if the file format version number is less than 1.) Set the Table.iPKey
754** field of the table under construction to be the index of the
755** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
756** no INTEGER PRIMARY KEY.
757**
758** If the key is not an INTEGER PRIMARY KEY, then create a unique
759** index for the key. No index is created for INTEGER PRIMARY KEYs.
760*/
danielk19774adee202004-05-08 08:23:19 +0000761void sqlite3AddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000762 Table *pTab = pParse->pNewTable;
763 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000764 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000765 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000766 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000767 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000768 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000769 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000770 }
771 pTab->hasPrimKey = 1;
772 if( pList==0 ){
773 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000774 pTab->aCol[iCol].isPrimKey = 1;
775 }else{
776 for(i=0; i<pList->nId; i++){
777 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000778 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
779 break;
780 }
drh78100cc2003-08-23 22:40:53 +0000781 }
782 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000783 }
drh78100cc2003-08-23 22:40:53 +0000784 if( pList->nId>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000785 }
786 if( iCol>=0 && iCol<pTab->nCol ){
787 zType = pTab->aCol[iCol].zType;
788 }
danielk19773d68f032004-05-11 07:11:51 +0000789 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000790 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000791 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000792 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000793 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000794 pList = 0;
drh4a324312001-12-21 14:30:42 +0000795 }
drhe0194f22003-02-26 13:52:51 +0000796
797primary_key_exit:
danielk19774adee202004-05-08 08:23:19 +0000798 sqlite3IdListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000799 return;
drh4a324312001-12-21 14:30:42 +0000800}
801
802/*
drhd3d39e92004-05-20 22:16:29 +0000803** Return a pointer to CollSeq given the name of a collating sequence.
804** If the collating sequence did not previously exist, create it but
805** assign it an NULL comparison function.
drh8e2ca022002-06-17 17:07:19 +0000806*/
drhd3d39e92004-05-20 22:16:29 +0000807CollSeq *sqlite3CollateType(Parse *pParse, const char *zType, int nType){
808 CollSeq *pColl;
809 sqlite *db = pParse->db;
810
811 pColl = sqlite3HashFind(&db->aCollSeq, zType, nType);
812 if( pColl==0 ){
813 sqlite3ChangeCollatingFunction(db, zType, nType, 0, 0);
814 pColl = sqlite3HashFind(&db->aCollSeq, zType, nType);
drh8e2ca022002-06-17 17:07:19 +0000815 }
drhd3d39e92004-05-20 22:16:29 +0000816 return pColl;
drh8e2ca022002-06-17 17:07:19 +0000817}
818
819/*
drhd3d39e92004-05-20 22:16:29 +0000820** Set the collation function of the most recently parsed table column
821** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000822*/
drhd3d39e92004-05-20 22:16:29 +0000823void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000824 Table *p;
drhd3d39e92004-05-20 22:16:29 +0000825 CollSeq *pColl;
826 sqlite *db = pParse->db;
danielk1977a37cdde2004-05-16 11:15:36 +0000827
drhd3d39e92004-05-20 22:16:29 +0000828 if( (p = pParse->pNewTable)==0 ) return;
829 pColl = sqlite3HashFind(&db->aCollSeq, zType, nType);
830 if( pColl==0 ){
831 pColl = sqlite3ChangeCollatingFunction(db, zType, nType, 0, 0);
832 }
833 if( pColl ){
834 p->aCol[p->nCol-1].pColl = pColl;
835 }
836}
837
838/*
839** Create or modify a collating sequence entry in the sqlite.aCollSeq
840** table.
841**
842** Once an entry is added to the sqlite.aCollSeq table, it can never
843** be removed, though is comparison function or user data can be changed.
844**
845** Return a pointer to the collating function that was created or modified.
846*/
847CollSeq *sqlite3ChangeCollatingFunction(
848 sqlite *db, /* Database into which to insert the collation */
849 const char *zName, /* Name of the collation */
850 int nName, /* Number of characters in zName */
851 void *pUser, /* First argument to xCmp */
852 int (*xCmp)(void*,int,const void*,int,const void*) /* Comparison function */
853){
854 CollSeq *pColl;
855
856 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
857 if( pColl==0 ){
858 pColl = sqliteMallocRaw( sizeof(*pColl) + nName + 1 );
859 if( pColl==0 ){
860 return 0;
861 }
862 pColl->zName = (char*)&pColl[1];
drhd3d39e92004-05-20 22:16:29 +0000863 memcpy(pColl->zName, zName, nName+1);
864 sqlite3HashInsert(&db->aCollSeq, pColl->zName, nName, pColl);
865 }
866 pColl->pUser = pUser;
867 pColl->xCmp = xCmp;
868 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000869}
870
871/*
drh1ad3b9e2004-05-20 12:10:20 +0000872** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +0000873** associated affinity type.
874*/
875char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +0000876 int n, i;
877 struct {
drh1ad3b9e2004-05-20 12:10:20 +0000878 const char *zSub; /* Keywords substring to search for */
879 int nSub; /* length of zSub */
880 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +0000881 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +0000882 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +0000883 {"CHAR", 4, SQLITE_AFF_TEXT},
884 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +0000885 {"TEXT", 4, SQLITE_AFF_TEXT},
886 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +0000887 };
888
drh1ad3b9e2004-05-20 12:10:20 +0000889 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
890 int c1 = substrings[i].zSub[0];
891 int c2 = tolower(c1);
892 int limit = nType - substrings[i].nSub;
893 const char *z = substrings[i].zSub;
894 for(n=0; n<=limit; n++){
895 int c = zType[n];
896 if( (c==c1 || c==c2)
897 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +0000898 return substrings[i].affinity;
899 }
900 }
901 }
drh1ad3b9e2004-05-20 12:10:20 +0000902 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +0000903}
904
905/*
drh50e5dad2001-09-15 00:57:28 +0000906** Come up with a new random value for the schema cookie. Make sure
907** the new value is different from the old.
908**
909** The schema cookie is used to determine when the schema for the
910** database changes. After each schema change, the cookie value
911** changes. When a process first reads the schema it records the
912** cookie. Thereafter, whenever it goes to access the database,
913** it checks the cookie to make sure the schema has not changed
914** since it was last read.
915**
916** This plan is not completely bullet-proof. It is possible for
917** the schema to change multiple times and for the cookie to be
918** set back to prior value. But schema changes are infrequent
919** and the probability of hitting the same cookie value is only
920** 1 chance in 2^32. So we're safe enough.
921*/
danielk1977cbb18d22004-05-28 11:37:27 +0000922void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){
danielk19771d850a72004-05-31 08:26:49 +0000923 unsigned char r;
924 int *pSchemaCookie = &(db->aDb[iDb].schema_cookie);
925
926 sqlite3Randomness(1, &r);
927 *pSchemaCookie = *pSchemaCookie + r + 1;
928 db->flags |= SQLITE_InternChanges;
929 sqlite3VdbeAddOp(v, OP_Integer, *pSchemaCookie, 0);
930 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
931/*
drh001bbcb2003-03-19 03:14:00 +0000932 if( db->next_cookie==db->aDb[0].schema_cookie ){
drhbbd82df2004-02-11 09:46:30 +0000933 unsigned char r;
danielk19774adee202004-05-08 08:23:19 +0000934 sqlite3Randomness(1, &r);
drhbbd82df2004-02-11 09:46:30 +0000935 db->next_cookie = db->aDb[0].schema_cookie + r + 1;
drh50e5dad2001-09-15 00:57:28 +0000936 db->flags |= SQLITE_InternChanges;
danielk19774adee202004-05-08 08:23:19 +0000937 sqlite3VdbeAddOp(v, OP_Integer, db->next_cookie, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000938 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +0000939 }
danielk19771d850a72004-05-31 08:26:49 +0000940*/
drh50e5dad2001-09-15 00:57:28 +0000941}
942
943/*
drh969fa7c2002-02-18 18:30:32 +0000944** Measure the number of characters needed to output the given
945** identifier. The number returned includes any quotes used
946** but does not include the null terminator.
947*/
948static int identLength(const char *z){
949 int n;
drh17f71932002-02-21 12:01:27 +0000950 int needQuote = 0;
951 for(n=0; *z; n++, z++){
952 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000953 }
drh17f71932002-02-21 12:01:27 +0000954 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000955}
956
957/*
958** Write an identifier onto the end of the given string. Add
959** quote characters as needed.
960*/
961static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000962 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000963 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000964 for(j=0; zIdent[j]; j++){
965 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
966 }
967 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +0000968 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh17f71932002-02-21 12:01:27 +0000969 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000970 for(j=0; zIdent[j]; j++){
971 z[i++] = zIdent[j];
972 if( zIdent[j]=='\'' ) z[i++] = '\'';
973 }
drh17f71932002-02-21 12:01:27 +0000974 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000975 z[i] = 0;
976 *pIdx = i;
977}
978
979/*
980** Generate a CREATE TABLE statement appropriate for the given
981** table. Memory to hold the text of the statement is obtained
982** from sqliteMalloc() and must be freed by the calling function.
983*/
984static char *createTableStmt(Table *p){
985 int i, k, n;
986 char *zStmt;
987 char *zSep, *zSep2, *zEnd;
988 n = 0;
989 for(i=0; i<p->nCol; i++){
990 n += identLength(p->aCol[i].zName);
991 }
992 n += identLength(p->zName);
993 if( n<40 ){
994 zSep = "";
995 zSep2 = ",";
996 zEnd = ")";
997 }else{
998 zSep = "\n ";
999 zSep2 = ",\n ";
1000 zEnd = "\n)";
1001 }
drhe0bc4042002-06-25 01:09:11 +00001002 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001003 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001004 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001005 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001006 k = strlen(zStmt);
1007 identPut(zStmt, &k, p->zName);
1008 zStmt[k++] = '(';
1009 for(i=0; i<p->nCol; i++){
1010 strcpy(&zStmt[k], zSep);
1011 k += strlen(&zStmt[k]);
1012 zSep = zSep2;
1013 identPut(zStmt, &k, p->aCol[i].zName);
1014 }
1015 strcpy(&zStmt[k], zEnd);
1016 return zStmt;
1017}
1018
1019/*
drh75897232000-05-29 14:26:00 +00001020** This routine is called to report the final ")" that terminates
1021** a CREATE TABLE statement.
1022**
drhf57b3392001-10-08 13:22:32 +00001023** The table structure that other action routines have been building
1024** is added to the internal hash tables, assuming no errors have
1025** occurred.
drh75897232000-05-29 14:26:00 +00001026**
drh1d85d932004-02-14 23:05:52 +00001027** An entry for the table is made in the master table on disk, unless
1028** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001029** it means we are reading the sqlite_master table because we just
1030** connected to the database or because the sqlite_master table has
1031** recently changes, so the entry for this table already exists in
1032** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001033**
1034** If the pSelect argument is not NULL, it means that this routine
1035** was called to create a table generated from a
1036** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1037** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001038*/
danielk19774adee202004-05-08 08:23:19 +00001039void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001040 Table *p;
drhbe0072d2001-09-13 14:46:09 +00001041 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001042
danielk197724b03fd2004-05-10 10:34:34 +00001043 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001044 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001045 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001046
drh969fa7c2002-02-18 18:30:32 +00001047 /* If the table is generated from a SELECT, then construct the
1048 ** list of columns and the text of the table.
1049 */
1050 if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001051 Table *pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +00001052 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +00001053 assert( p->aCol==0 );
1054 p->nCol = pSelTab->nCol;
1055 p->aCol = pSelTab->aCol;
1056 pSelTab->nCol = 0;
1057 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001058 sqlite3DeleteTable(0, pSelTab);
drh969fa7c2002-02-18 18:30:32 +00001059 }
1060
drh1d85d932004-02-14 23:05:52 +00001061 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001062 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1063 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001064 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001065 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001066 */
drh1d85d932004-02-14 23:05:52 +00001067 if( db->init.busy ){
1068 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001069 }
1070
drhe3c41372001-09-17 20:25:58 +00001071 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001072 ** in the SQLITE_MASTER table of the database. The record number
1073 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001074 **
drhe0bc4042002-06-25 01:09:11 +00001075 ** If this is a TEMPORARY table, write the entry into the auxiliary
1076 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001077 */
drh1d85d932004-02-14 23:05:52 +00001078 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001079 int n;
drhd8bc7082000-06-07 23:51:50 +00001080 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001081
danielk19774adee202004-05-08 08:23:19 +00001082 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001083 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +00001084 if( p->pSelect==0 ){
1085 /* A regular table */
danielk19774adee202004-05-08 08:23:19 +00001086 sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
drh4ff6dfa2002-03-03 23:06:00 +00001087 }else{
1088 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001089 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001090 }
drh969fa7c2002-02-18 18:30:32 +00001091 p->tnum = 0;
danielk19774adee202004-05-08 08:23:19 +00001092 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001093 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view", P3_STATIC);
1094 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1095 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00001096 sqlite3VdbeAddOp(v, OP_Dup, 4, 0);
drhe0bc4042002-06-25 01:09:11 +00001097 if( pSelect ){
1098 char *z = createTableStmt(p);
1099 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001100 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001101 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001102 sqliteFree(z);
1103 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001104 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001105 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001106 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001107 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001108 }
drhe0bc4042002-06-25 01:09:11 +00001109 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001110 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001111 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001112 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
1113 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drhe0bc4042002-06-25 01:09:11 +00001114 }
danielk197784ac9d02004-05-18 09:58:06 +00001115 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001116 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk19771d850a72004-05-31 08:26:49 +00001117 if( p->iDb!=1 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001118 sqlite3ChangeCookie(db, v, p->iDb);
drhe0bc4042002-06-25 01:09:11 +00001119 }
danielk19774adee202004-05-08 08:23:19 +00001120 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +00001121 if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001122 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1123 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +00001124 pParse->nTab = 2;
danielk197784ac9d02004-05-18 09:58:06 +00001125 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +00001126 }
danielk19774adee202004-05-08 08:23:19 +00001127 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001128 }
drh17e9e292003-02-01 13:53:28 +00001129
1130 /* Add the table to the in-memory representation of the database.
1131 */
drhd24cc422003-03-27 12:51:24 +00001132 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001133 Table *pOld;
1134 FKey *pFKey;
danielk19774adee202004-05-08 08:23:19 +00001135 pOld = sqlite3HashInsert(&db->aDb[p->iDb].tblHash,
drhd24cc422003-03-27 12:51:24 +00001136 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001137 if( pOld ){
1138 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1139 return;
1140 }
1141 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1142 int nTo = strlen(pFKey->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +00001143 pFKey->pNextTo = sqlite3HashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
1144 sqlite3HashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001145 }
1146 pParse->pNewTable = 0;
1147 db->nTable++;
1148 db->flags |= SQLITE_InternChanges;
1149 }
drh75897232000-05-29 14:26:00 +00001150}
1151
1152/*
drha76b5df2002-02-23 02:32:10 +00001153** The parser calls this routine in order to create a new VIEW
1154*/
danielk19774adee202004-05-08 08:23:19 +00001155void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001156 Parse *pParse, /* The parsing context */
1157 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001158 Token *pName1, /* The token that holds the name of the view */
1159 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001160 Select *pSelect, /* A SELECT statement that will become the new view */
1161 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001162){
drha76b5df2002-02-23 02:32:10 +00001163 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001164 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001165 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001166 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001167 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001168 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001169
danielk197748dec7e2004-05-28 12:33:30 +00001170 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001171 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001172 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001173 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001174 return;
1175 }
danielk1977ef2cb632004-05-29 02:37:19 +00001176 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001177 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1178 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001179 ){
danielk19774adee202004-05-08 08:23:19 +00001180 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001181 return;
1182 }
drh174b6192002-12-03 02:22:52 +00001183
drh4b59ab52002-08-24 18:24:51 +00001184 /* Make a copy of the entire SELECT statement that defines the view.
1185 ** This will force all the Expr.token.z values to be dynamically
1186 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001187 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001188 */
danielk19774adee202004-05-08 08:23:19 +00001189 p->pSelect = sqlite3SelectDup(pSelect);
1190 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001191 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001193 }
drh4b59ab52002-08-24 18:24:51 +00001194
1195 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1196 ** the end.
1197 */
drha76b5df2002-02-23 02:32:10 +00001198 sEnd = pParse->sLastToken;
1199 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1200 sEnd.z += sEnd.n;
1201 }
1202 sEnd.n = 0;
1203 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001204 z = pBegin->z;
1205 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1206 sEnd.z = &z[n-1];
1207 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001208
danielk19774adee202004-05-08 08:23:19 +00001209 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1210 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001211 return;
drh417be792002-03-03 18:59:40 +00001212}
drha76b5df2002-02-23 02:32:10 +00001213
drh417be792002-03-03 18:59:40 +00001214/*
1215** The Table structure pTable is really a VIEW. Fill in the names of
1216** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001217** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001218*/
danielk19774adee202004-05-08 08:23:19 +00001219int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001220 ExprList *pEList;
1221 Select *pSel;
1222 Table *pSelTab;
1223 int nErr = 0;
1224
1225 assert( pTable );
1226
1227 /* A positive nCol means the columns names for this view are
1228 ** already known.
1229 */
1230 if( pTable->nCol>0 ) return 0;
1231
1232 /* A negative nCol is a special marker meaning that we are currently
1233 ** trying to compute the column names. If we enter this routine with
1234 ** a negative nCol, it means two or more views form a loop, like this:
1235 **
1236 ** CREATE VIEW one AS SELECT * FROM two;
1237 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001238 **
1239 ** Actually, this error is caught previously and so the following test
1240 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001241 */
1242 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001243 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001244 return 1;
1245 }
1246
1247 /* If we get this far, it means we need to compute the table names.
1248 */
1249 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1250 pSel = pTable->pSelect;
1251
danielk19774adee202004-05-08 08:23:19 +00001252 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001253 ** "*" elements in this list. But we will need to restore the list
1254 ** back to its original configuration afterwards, so we save a copy of
1255 ** the original in pEList.
1256 */
1257 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001258 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001259 if( pSel->pEList==0 ){
1260 pSel->pEList = pEList;
1261 return 1; /* Malloc failed */
1262 }
1263 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001264 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001265 if( pSelTab ){
1266 assert( pTable->aCol==0 );
1267 pTable->nCol = pSelTab->nCol;
1268 pTable->aCol = pSelTab->aCol;
1269 pSelTab->nCol = 0;
1270 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001271 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001272 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001273 }else{
1274 pTable->nCol = 0;
1275 nErr++;
1276 }
danielk19774adee202004-05-08 08:23:19 +00001277 sqlite3SelectUnbind(pSel);
1278 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001279 pSel->pEList = pEList;
1280 return nErr;
1281}
1282
1283/*
1284** Clear the column names from the VIEW pTable.
1285**
1286** This routine is called whenever any other table or view is modified.
1287** The view passed into this routine might depend directly or indirectly
1288** on the modified or deleted table so we need to clear the old column
1289** names so that they will be recomputed.
1290*/
1291static void sqliteViewResetColumnNames(Table *pTable){
1292 int i;
drh701a0ae2004-02-22 20:05:00 +00001293 Column *pCol;
1294 assert( pTable!=0 && pTable->pSelect!=0 );
1295 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1296 sqliteFree(pCol->zName);
1297 sqliteFree(pCol->zDflt);
1298 sqliteFree(pCol->zType);
drh417be792002-03-03 18:59:40 +00001299 }
1300 sqliteFree(pTable->aCol);
1301 pTable->aCol = 0;
1302 pTable->nCol = 0;
1303}
1304
1305/*
drh8bf8dc92003-05-17 17:35:10 +00001306** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001307*/
drhd24cc422003-03-27 12:51:24 +00001308static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001309 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001310 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001311 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001312 Table *pTab = sqliteHashData(i);
1313 if( pTab->pSelect ){
1314 sqliteViewResetColumnNames(pTab);
1315 }
1316 }
drh8bf8dc92003-05-17 17:35:10 +00001317 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001318}
1319
1320/*
drh75897232000-05-29 14:26:00 +00001321** Given a token, look up a table with that name. If not found, leave
1322** an error for the parser to find and return NULL.
1323*/
danielk19774adee202004-05-08 08:23:19 +00001324Table *sqlite3TableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001325 char *zName;
1326 Table *pTab;
danielk19774adee202004-05-08 08:23:19 +00001327 zName = sqlite3TableNameFromToken(pTok);
drhdaffd0e2001-04-11 14:28:42 +00001328 if( zName==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001329 pTab = sqlite3FindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001330 sqliteFree(zName);
1331 if( pTab==0 ){
danielk19774adee202004-05-08 08:23:19 +00001332 sqlite3ErrorMsg(pParse, "no such table: %T", pTok);
drh75897232000-05-29 14:26:00 +00001333 }
1334 return pTab;
1335}
1336
1337/*
1338** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001339** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001340*/
danielk1977a8858102004-05-28 12:11:21 +00001341void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1342 Table *pTab;
drh75897232000-05-29 14:26:00 +00001343 Vdbe *v;
1344 int base;
drh5edc3122001-09-13 21:53:09 +00001345 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001346 int iDb;
drh75897232000-05-29 14:26:00 +00001347
danielk1977a8858102004-05-28 12:11:21 +00001348 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1349 assert( pName->nSrc==1 );
1350 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1351
1352 if( pTab==0 ) goto exit_drop_table;
1353 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001354 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001355#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001356 {
1357 int code;
danielk1977a8858102004-05-28 12:11:21 +00001358 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1359 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001360 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001361 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001362 }
drhe5f9c642003-01-13 23:27:31 +00001363 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001364 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001365 code = SQLITE_DROP_TEMP_VIEW;
1366 }else{
1367 code = SQLITE_DROP_VIEW;
1368 }
1369 }else{
drhd24cc422003-03-27 12:51:24 +00001370 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001371 code = SQLITE_DROP_TEMP_TABLE;
1372 }else{
1373 code = SQLITE_DROP_TABLE;
1374 }
1375 }
danielk1977a8858102004-05-28 12:11:21 +00001376 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1377 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001378 }
danielk1977a8858102004-05-28 12:11:21 +00001379 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1380 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001381 }
drhe5f9c642003-01-13 23:27:31 +00001382 }
1383#endif
danielk1977a8858102004-05-28 12:11:21 +00001384 if( pTab->readOnly ){
1385 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001386 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001387 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001388 }
danielk1977a8858102004-05-28 12:11:21 +00001389 if( isView && pTab->pSelect==0 ){
1390 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1391 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001392 }
danielk1977a8858102004-05-28 12:11:21 +00001393 if( !isView && pTab->pSelect ){
1394 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1395 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001396 }
drh75897232000-05-29 14:26:00 +00001397
drh1ccde152000-06-17 13:12:39 +00001398 /* Generate code to remove the table from the master table
1399 ** on disk.
1400 */
danielk19774adee202004-05-08 08:23:19 +00001401 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001402 if( v ){
drh905793e2004-02-21 13:31:09 +00001403 static VdbeOpList dropTable[] = {
danielk19778d059842004-05-12 11:24:02 +00001404 { OP_Rewind, 0, ADDR(10), 0},
danielk19770f69c1e2004-05-29 11:24:50 +00001405 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001406 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001407 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001408 { OP_Column, 0, 2, 0},
danielk19778d059842004-05-12 11:24:02 +00001409 { OP_Ne, 0, ADDR(9), 0},
drh75897232000-05-29 14:26:00 +00001410 { OP_Delete, 0, 0, 0},
danielk19778d059842004-05-12 11:24:02 +00001411 { OP_Rewind, 0, ADDR(10), 0},
1412 { OP_Goto, 0, ADDR(3), 0},
1413 { OP_Next, 0, ADDR(3), 0}, /* 9 */
drh75897232000-05-29 14:26:00 +00001414 };
1415 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001416 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001417 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001418
danielk1977c3f9bad2002-05-15 08:30:12 +00001419 /* Drop all triggers associated with the table being dropped */
danielk1977a8858102004-05-28 12:11:21 +00001420 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001421 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001422 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001423 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drhe0bc4042002-06-25 01:09:11 +00001424 if( pParse->explain ){
1425 pTrigger = pTrigger->pNext;
1426 }else{
danielk1977a8858102004-05-28 12:11:21 +00001427 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001428 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001429 }
drh8bf8dc92003-05-17 17:35:10 +00001430
1431 /* Drop all SQLITE_MASTER entries that refer to the table */
danielk1977a8858102004-05-28 12:11:21 +00001432 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001433 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001434 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
drh8bf8dc92003-05-17 17:35:10 +00001435
1436 /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */
danielk1977a8858102004-05-28 12:11:21 +00001437 if( pTab->iDb!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001438 sqlite3OpenMasterTable(v, 1);
1439 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001440 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
drh8bf8dc92003-05-17 17:35:10 +00001441 }
1442
danielk1977a8858102004-05-28 12:11:21 +00001443 if( pTab->iDb!=1 ){ /* Temp database has no schema cookie */
1444 sqlite3ChangeCookie(db, v, pTab->iDb);
drhf57b3392001-10-08 13:22:32 +00001445 }
danielk19774adee202004-05-08 08:23:19 +00001446 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001447 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001448 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1449 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001450 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001451 }
drh5e00f6c2001-09-13 13:46:56 +00001452 }
danielk19774adee202004-05-08 08:23:19 +00001453 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001454 }
1455
drhe0bc4042002-06-25 01:09:11 +00001456 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001457 **
1458 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001459 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001460 */
1461 if( !pParse->explain ){
danielk1977a8858102004-05-28 12:11:21 +00001462 sqliteUnlinkAndDeleteTable(db, pTab);
drh5edc3122001-09-13 21:53:09 +00001463 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001464 }
drhd24cc422003-03-27 12:51:24 +00001465 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001466
1467exit_drop_table:
1468 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001469}
1470
1471/*
drhc2eef3b2002-08-31 18:53:06 +00001472** This routine is called to create a new foreign key on the table
1473** currently under construction. pFromCol determines which columns
1474** in the current table point to the foreign key. If pFromCol==0 then
1475** connect the key to the last column inserted. pTo is the name of
1476** the table referred to. pToCol is a list of tables in the other
1477** pTo table that the foreign key points to. flags contains all
1478** information about the conflict resolution algorithms specified
1479** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1480**
1481** An FKey structure is created and added to the table currently
1482** under construction in the pParse->pNewTable field. The new FKey
1483** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001484** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001485**
1486** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001487** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001488*/
danielk19774adee202004-05-08 08:23:19 +00001489void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001490 Parse *pParse, /* Parsing context */
1491 IdList *pFromCol, /* Columns in this table that point to other table */
1492 Token *pTo, /* Name of the other table */
1493 IdList *pToCol, /* Columns in the other table */
1494 int flags /* Conflict resolution algorithms. */
1495){
1496 Table *p = pParse->pNewTable;
1497 int nByte;
1498 int i;
1499 int nCol;
1500 char *z;
1501 FKey *pFKey = 0;
1502
1503 assert( pTo!=0 );
1504 if( p==0 || pParse->nErr ) goto fk_end;
1505 if( pFromCol==0 ){
1506 int iCol = p->nCol-1;
1507 if( iCol<0 ) goto fk_end;
1508 if( pToCol && pToCol->nId!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001509 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001510 " should reference only one column of table %T",
1511 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001512 goto fk_end;
1513 }
1514 nCol = 1;
1515 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
danielk19774adee202004-05-08 08:23:19 +00001516 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001517 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001518 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001519 goto fk_end;
1520 }else{
1521 nCol = pFromCol->nId;
1522 }
1523 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1524 if( pToCol ){
1525 for(i=0; i<pToCol->nId; i++){
1526 nByte += strlen(pToCol->a[i].zName) + 1;
1527 }
1528 }
1529 pFKey = sqliteMalloc( nByte );
1530 if( pFKey==0 ) goto fk_end;
1531 pFKey->pFrom = p;
1532 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001533 z = (char*)&pFKey[1];
1534 pFKey->aCol = (struct sColMap*)z;
1535 z += sizeof(struct sColMap)*nCol;
1536 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001537 memcpy(z, pTo->z, pTo->n);
1538 z[pTo->n] = 0;
1539 z += pTo->n+1;
1540 pFKey->pNextTo = 0;
1541 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001542 if( pFromCol==0 ){
1543 pFKey->aCol[0].iFrom = p->nCol-1;
1544 }else{
1545 for(i=0; i<nCol; i++){
1546 int j;
1547 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001548 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001549 pFKey->aCol[i].iFrom = j;
1550 break;
1551 }
1552 }
1553 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001554 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001555 "unknown column \"%s\" in foreign key definition",
1556 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001557 goto fk_end;
1558 }
1559 }
1560 }
1561 if( pToCol ){
1562 for(i=0; i<nCol; i++){
1563 int n = strlen(pToCol->a[i].zName);
1564 pFKey->aCol[i].zCol = z;
1565 memcpy(z, pToCol->a[i].zName, n);
1566 z[n] = 0;
1567 z += n+1;
1568 }
1569 }
1570 pFKey->isDeferred = 0;
1571 pFKey->deleteConf = flags & 0xff;
1572 pFKey->updateConf = (flags >> 8 ) & 0xff;
1573 pFKey->insertConf = (flags >> 16 ) & 0xff;
1574
1575 /* Link the foreign key to the table as the last step.
1576 */
1577 p->pFKey = pFKey;
1578 pFKey = 0;
1579
1580fk_end:
1581 sqliteFree(pFKey);
danielk19774adee202004-05-08 08:23:19 +00001582 sqlite3IdListDelete(pFromCol);
1583 sqlite3IdListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001584}
1585
1586/*
1587** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1588** clause is seen as part of a foreign key definition. The isDeferred
1589** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1590** The behavior of the most recently created foreign key is adjusted
1591** accordingly.
1592*/
danielk19774adee202004-05-08 08:23:19 +00001593void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001594 Table *pTab;
1595 FKey *pFKey;
1596 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1597 pFKey->isDeferred = isDeferred;
1598}
1599
1600/*
drh75897232000-05-29 14:26:00 +00001601** Create a new index for an SQL table. pIndex is the name of the index
1602** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001603** be NULL for a primary key or an index that is created to satisfy a
1604** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001605** as the table to be indexed. pParse->pNewTable is a table that is
1606** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001607**
drh382c0242001-10-06 16:33:02 +00001608** pList is a list of columns to be indexed. pList will be NULL if this
1609** is a primary key or unique-constraint on the most recent column added
1610** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001611*/
danielk19774adee202004-05-08 08:23:19 +00001612void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001613 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001614 Token *pName1, /* First part of index name. May be NULL */
1615 Token *pName2, /* Second part of index name. May be NULL */
danielk1977ef2cb632004-05-29 02:37:19 +00001616 SrcList *pTblName, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001617 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001618 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001619 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1620 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1621){
danielk1977cbb18d22004-05-28 11:37:27 +00001622 Table *pTab = 0; /* Table to be indexed */
drh75897232000-05-29 14:26:00 +00001623 Index *pIndex; /* The index to be created */
1624 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001625 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001626 Token nullId; /* Fake token for an empty ID list */
1627 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001628 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001629 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001630
danielk1977cbb18d22004-05-28 11:37:27 +00001631 int iDb; /* Index of the database that is being written */
1632 Token *pName = 0; /* Unqualified name of the index to create */
1633
danielk197724b03fd2004-05-10 10:34:34 +00001634 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001635
drh75897232000-05-29 14:26:00 +00001636 /*
1637 ** Find the table that is to be indexed. Return early if not found.
1638 */
danielk1977cbb18d22004-05-28 11:37:27 +00001639 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001640
1641 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001642 ** to search for the table. 'Fix' the table name to this db
1643 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001644 */
1645 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001646 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001647 if( iDb<0 ) goto exit_create_index;
1648
danielk1977ef2cb632004-05-29 02:37:19 +00001649 /* If the index name was unqualified, check if the the table
1650 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001651 */
danielk1977ef2cb632004-05-29 02:37:19 +00001652 pTab = sqlite3SrcListLookup(pParse, pTblName);
1653 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1654 iDb = 1;
1655 }
1656
1657 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1658 sqlite3FixSrcList(&sFix, pTblName)
1659 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001660 goto exit_create_index;
1661 }
danielk1977ef2cb632004-05-29 02:37:19 +00001662 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1663 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001664 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001665 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001666 }else{
drhe3c41372001-09-17 20:25:58 +00001667 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001668 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001669 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001670 }
danielk1977cbb18d22004-05-28 11:37:27 +00001671
drh75897232000-05-29 14:26:00 +00001672 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001673 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001674 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001675 goto exit_create_index;
1676 }
drha76b5df2002-02-23 02:32:10 +00001677 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001678 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001679 goto exit_create_index;
1680 }
drh4925ca02003-11-27 00:48:57 +00001681 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001682
1683 /*
1684 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001685 ** index or table with the same name.
1686 **
1687 ** Exception: If we are reading the names of permanent indices from the
1688 ** sqlite_master table (because some other process changed the schema) and
1689 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001690 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001691 **
1692 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001693 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1694 ** own name.
drh75897232000-05-29 14:26:00 +00001695 */
drh1d85d932004-02-14 23:05:52 +00001696 if( pName && !db->init.busy ){
drhf57b3392001-10-08 13:22:32 +00001697 Index *pISameName; /* Another index with the same name */
1698 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001699 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001700 if( zName==0 ) goto exit_create_index;
danielk19773df6b252004-05-29 10:23:19 +00001701 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001702 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
drhd24cc422003-03-27 12:51:24 +00001703 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001704 }
danielk19774adee202004-05-08 08:23:19 +00001705 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1706 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
drhd24cc422003-03-27 12:51:24 +00001707 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001708 }
drhd24cc422003-03-27 12:51:24 +00001709 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001710 char zBuf[30];
1711 int n;
1712 Index *pLoop;
1713 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1714 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001715 zName = 0;
danielk19774adee202004-05-08 08:23:19 +00001716 sqlite3SetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001717 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001718 }else{
1719 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001720 }
1721
drhe5f9c642003-01-13 23:27:31 +00001722 /* Check for authorization to create an index.
1723 */
1724#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001725 {
1726 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001727 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001728 goto exit_create_index;
1729 }
1730 i = SQLITE_CREATE_INDEX;
1731 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001732 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001733 goto exit_create_index;
1734 }
drhe5f9c642003-01-13 23:27:31 +00001735 }
1736#endif
1737
drh75897232000-05-29 14:26:00 +00001738 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001739 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001740 ** So create a fake list to simulate this.
1741 */
1742 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001743 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001744 nullId.n = strlen(nullId.z);
danielk19774adee202004-05-08 08:23:19 +00001745 pList = sqlite3IdListAppend(0, &nullId);
drh75897232000-05-29 14:26:00 +00001746 if( pList==0 ) goto exit_create_index;
1747 }
1748
1749 /*
1750 ** Allocate the index structure.
1751 */
drhdcc581c2000-05-30 13:44:19 +00001752 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drhd3d39e92004-05-20 22:16:29 +00001753 (sizeof(int) + sizeof(CollSeq*))*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001754 if( pIndex==0 ) goto exit_create_index;
drhd3d39e92004-05-20 22:16:29 +00001755 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nId];
drh967e8b72000-06-21 13:59:10 +00001756 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001757 strcpy(pIndex->zName, zName);
1758 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001759 pIndex->nColumn = pList->nId;
drhea1ba172003-04-20 00:00:23 +00001760 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001761 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00001762 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00001763
drh1ccde152000-06-17 13:12:39 +00001764 /* Scan the names of the columns of the table to be indexed and
1765 ** load the column indices into the Index structure. Report an error
1766 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001767 */
1768 for(i=0; i<pList->nId; i++){
1769 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001770 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001771 }
1772 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001773 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00001774 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001775 sqliteFree(pIndex);
1776 goto exit_create_index;
1777 }
drh967e8b72000-06-21 13:59:10 +00001778 pIndex->aiColumn[i] = j;
drhd3d39e92004-05-20 22:16:29 +00001779 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
drh75897232000-05-29 14:26:00 +00001780 }
drhd3d39e92004-05-20 22:16:29 +00001781 pIndex->keyInfo.nField = pList->nId;
drh75897232000-05-29 14:26:00 +00001782
1783 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001784 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001785 */
drhd24cc422003-03-27 12:51:24 +00001786 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001787 Index *p;
danielk19774adee202004-05-08 08:23:19 +00001788 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00001789 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001790 if( p ){
1791 assert( p==pIndex ); /* Malloc must have failed */
1792 sqliteFree(pIndex);
1793 goto exit_create_index;
1794 }
drh5e00f6c2001-09-13 13:46:56 +00001795 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001796 }
drh9cfcf5d2002-01-29 18:41:24 +00001797
1798 /* When adding an index to the list of indices for a table, make
1799 ** sure all indices labeled OE_Replace come after all those labeled
1800 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1801 ** and INSERT.
1802 */
1803 if( onError!=OE_Replace || pTab->pIndex==0
1804 || pTab->pIndex->onError==OE_Replace){
1805 pIndex->pNext = pTab->pIndex;
1806 pTab->pIndex = pIndex;
1807 }else{
1808 Index *pOther = pTab->pIndex;
1809 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1810 pOther = pOther->pNext;
1811 }
1812 pIndex->pNext = pOther->pNext;
1813 pOther->pNext = pIndex;
1814 }
drh75897232000-05-29 14:26:00 +00001815
drh1d85d932004-02-14 23:05:52 +00001816 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhd78eeee2001-09-13 16:18:53 +00001817 ** "sqlite_master" table on the disk. So do not write to the disk
drh1d85d932004-02-14 23:05:52 +00001818 ** again. Extract the table number from the db->init.newTnum field.
drhd78eeee2001-09-13 16:18:53 +00001819 */
danielk1977cbb18d22004-05-28 11:37:27 +00001820 if( db->init.busy && pTblName!=0 ){
drh1d85d932004-02-14 23:05:52 +00001821 pIndex->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001822 }
1823
drh1d85d932004-02-14 23:05:52 +00001824 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00001825 ** involves writing the index into the master table and filling in the
1826 ** index with the current table contents.
1827 **
drh1d85d932004-02-14 23:05:52 +00001828 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
1829 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00001830 ** CREATE INDEX statements are read out of the master table. In
1831 ** the latter case the index already exists on disk, which is why
1832 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001833 **
danielk1977cbb18d22004-05-28 11:37:27 +00001834 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001835 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1836 ** has just been created, it contains no data and the index initialization
1837 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001838 */
drh1d85d932004-02-14 23:05:52 +00001839 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00001840 int n;
drhadbca9c2001-09-27 15:11:53 +00001841 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001842 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00001843
danielk19774adee202004-05-08 08:23:19 +00001844 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001845 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00001846 if( pTblName!=0 ){
1847 sqlite3BeginWriteOperation(pParse, 0, iDb);
1848 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00001849 }
danielk19774adee202004-05-08 08:23:19 +00001850 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001851 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
1852 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
1853 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001854 sqlite3VdbeOp3(v, OP_CreateIndex, 0, iDb,(char*)&pIndex->tnum,P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001855 pIndex->tnum = 0;
danielk1977cbb18d22004-05-28 11:37:27 +00001856 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00001857 sqlite3VdbeCode(v,
drh701a0ae2004-02-22 20:05:00 +00001858 OP_Dup, 0, 0,
danielk1977cbb18d22004-05-28 11:37:27 +00001859 OP_Integer, iDb, 0,
drh701a0ae2004-02-22 20:05:00 +00001860 0);
drhd3d39e92004-05-20 22:16:29 +00001861 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
1862 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00001863 }
danielk19770f69c1e2004-05-29 11:24:50 +00001864 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00001865 if( pStart && pEnd ){
drh51846b52004-05-28 16:00:21 +00001866 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001867 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001868 n = Addr(pEnd->z) - Addr(pName->z) + 1;
1869 sqlite3VdbeChangeP3(v, -1, pName->z, n);
1870 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh75897232000-05-29 14:26:00 +00001871 }
danielk197784ac9d02004-05-18 09:58:06 +00001872 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001873 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001874 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00001875 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00001876 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
1877 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00001878 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00001879 lbl2 = sqlite3VdbeMakeLabel(v);
1880 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00001881 lbl1 = sqlite3VdbeCurrentAddr(v);
1882 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00001883 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00001884 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001885 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
1886 sqlite3VdbeResolveLabel(v, lbl2);
1887 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
1888 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001889 }
danielk1977cbb18d22004-05-28 11:37:27 +00001890 if( pTblName!=0 ){
drhf57b3392001-10-08 13:22:32 +00001891 if( !isTemp ){
danielk1977cbb18d22004-05-28 11:37:27 +00001892 sqlite3ChangeCookie(db, v, iDb);
drhf57b3392001-10-08 13:22:32 +00001893 }
danielk19774adee202004-05-08 08:23:19 +00001894 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1895 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001896 }
drh75897232000-05-29 14:26:00 +00001897 }
1898
drh75897232000-05-29 14:26:00 +00001899 /* Clean up before exiting */
1900exit_create_index:
danielk19774adee202004-05-08 08:23:19 +00001901 sqlite3IdListDelete(pList);
danielk1977cbb18d22004-05-28 11:37:27 +00001902 /* sqlite3SrcListDelete(pTable); */
drh75897232000-05-29 14:26:00 +00001903 sqliteFree(zName);
1904 return;
1905}
1906
1907/*
drh74e24cd2002-01-09 03:19:59 +00001908** This routine will drop an existing named index. This routine
1909** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001910*/
danielk19774adee202004-05-08 08:23:19 +00001911void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001912 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001913 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001914 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001915
danielk197724b03fd2004-05-10 10:34:34 +00001916 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001917 assert( pName->nSrc==1 );
danielk19774adee202004-05-08 08:23:19 +00001918 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001919 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00001920 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drhd24cc422003-03-27 12:51:24 +00001921 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001922 }
drh485b39b2002-07-13 03:11:52 +00001923 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00001924 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00001925 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00001926 goto exit_drop_index;
1927 }
danielk1977a8858102004-05-28 12:11:21 +00001928/*
drhd24cc422003-03-27 12:51:24 +00001929 if( pIndex->iDb>1 ){
danielk19774adee202004-05-08 08:23:19 +00001930 sqlite3ErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00001931 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00001932 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001933 }
danielk1977a8858102004-05-28 12:11:21 +00001934*/
drhe5f9c642003-01-13 23:27:31 +00001935#ifndef SQLITE_OMIT_AUTHORIZATION
1936 {
1937 int code = SQLITE_DROP_INDEX;
1938 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00001939 const char *zDb = db->aDb[pIndex->iDb].zName;
1940 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00001941 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001942 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001943 }
drhd24cc422003-03-27 12:51:24 +00001944 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001945 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001946 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001947 }
drhed6c8672003-01-12 18:02:16 +00001948 }
drhe5f9c642003-01-13 23:27:31 +00001949#endif
drh75897232000-05-29 14:26:00 +00001950
1951 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00001952 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001953 if( v ){
drh905793e2004-02-21 13:31:09 +00001954 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001955 { OP_Rewind, 0, ADDR(9), 0},
danielk19770f69c1e2004-05-29 11:24:50 +00001956 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001957 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001958 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001959 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001960 { OP_Eq, 0, ADDR(8), 0},
1961 { OP_Next, 0, ADDR(3), 0},
1962 { OP_Goto, 0, ADDR(9), 0},
1963 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001964 };
1965 int base;
1966
danielk19774adee202004-05-08 08:23:19 +00001967 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
1968 sqlite3OpenMasterTable(v, pIndex->iDb);
1969 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1970 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001971 if( pIndex->iDb!=1 ){
1972 sqlite3ChangeCookie(db, v, pIndex->iDb);
drhf57b3392001-10-08 13:22:32 +00001973 }
danielk19774adee202004-05-08 08:23:19 +00001974 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1975 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
1976 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001977 }
1978
drhe0bc4042002-06-25 01:09:11 +00001979 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001980 */
1981 if( !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +00001982 sqlite3UnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001983 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001984 }
drhd24cc422003-03-27 12:51:24 +00001985
1986exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00001987 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001988}
1989
1990/*
drh75897232000-05-29 14:26:00 +00001991** Append a new element to the given IdList. Create a new IdList if
1992** need be.
drhdaffd0e2001-04-11 14:28:42 +00001993**
1994** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001995*/
danielk19774adee202004-05-08 08:23:19 +00001996IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00001997 if( pList==0 ){
1998 pList = sqliteMalloc( sizeof(IdList) );
1999 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002000 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002001 }
drh4305d102003-07-30 12:34:12 +00002002 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002003 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002004 pList->nAlloc = pList->nAlloc*2 + 5;
2005 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002006 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002007 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002008 return 0;
drh75897232000-05-29 14:26:00 +00002009 }
drh6d4abfb2001-10-22 02:58:08 +00002010 pList->a = a;
drh75897232000-05-29 14:26:00 +00002011 }
2012 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
2013 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00002014 char **pz = &pList->a[pList->nId].zName;
danielk19774adee202004-05-08 08:23:19 +00002015 sqlite3SetNString(pz, pToken->z, pToken->n, 0);
drhdaffd0e2001-04-11 14:28:42 +00002016 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002017 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002018 return 0;
2019 }else{
danielk19774adee202004-05-08 08:23:19 +00002020 sqlite3Dequote(*pz);
drhdaffd0e2001-04-11 14:28:42 +00002021 }
drh75897232000-05-29 14:26:00 +00002022 }
2023 pList->nId++;
2024 return pList;
2025}
2026
2027/*
drhad3cab52002-05-24 02:04:32 +00002028** Append a new table name to the given SrcList. Create a new SrcList if
2029** need be. A new entry is created in the SrcList even if pToken is NULL.
2030**
2031** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002032**
2033** If pDatabase is not null, it means that the table has an optional
2034** database name prefix. Like this: "database.table". The pDatabase
2035** points to the table name and the pTable points to the database name.
2036** The SrcList.a[].zName field is filled with the table name which might
2037** come from pTable (if pDatabase is NULL) or from pDatabase.
2038** SrcList.a[].zDatabase is filled with the database name from pTable,
2039** or with NULL if no database is specified.
2040**
2041** In other words, if call like this:
2042**
danielk19774adee202004-05-08 08:23:19 +00002043** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002044**
2045** Then B is a table name and the database name is unspecified. If called
2046** like this:
2047**
danielk19774adee202004-05-08 08:23:19 +00002048** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002049**
2050** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002051*/
danielk19774adee202004-05-08 08:23:19 +00002052SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00002053 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002054 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002055 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002056 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002057 }
drh4305d102003-07-30 12:34:12 +00002058 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002059 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002060 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002061 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002062 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002063 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002064 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002065 return 0;
2066 }
drh113088e2003-03-20 01:16:58 +00002067 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002068 }
2069 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002070 if( pDatabase && pDatabase->z==0 ){
2071 pDatabase = 0;
2072 }
2073 if( pDatabase && pTable ){
2074 Token *pTemp = pDatabase;
2075 pDatabase = pTable;
2076 pTable = pTemp;
2077 }
2078 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00002079 char **pz = &pList->a[pList->nSrc].zName;
danielk19774adee202004-05-08 08:23:19 +00002080 sqlite3SetNString(pz, pTable->z, pTable->n, 0);
drh113088e2003-03-20 01:16:58 +00002081 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002082 sqlite3SrcListDelete(pList);
drh113088e2003-03-20 01:16:58 +00002083 return 0;
2084 }else{
danielk19774adee202004-05-08 08:23:19 +00002085 sqlite3Dequote(*pz);
drh113088e2003-03-20 01:16:58 +00002086 }
2087 }
2088 if( pDatabase ){
2089 char **pz = &pList->a[pList->nSrc].zDatabase;
danielk19774adee202004-05-08 08:23:19 +00002090 sqlite3SetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00002091 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002092 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002093 return 0;
2094 }else{
danielk19774adee202004-05-08 08:23:19 +00002095 sqlite3Dequote(*pz);
drhad3cab52002-05-24 02:04:32 +00002096 }
2097 }
drh63eb5f22003-04-29 16:20:44 +00002098 pList->a[pList->nSrc].iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002099 pList->nSrc++;
2100 return pList;
2101}
2102
2103/*
drh63eb5f22003-04-29 16:20:44 +00002104** Assign cursors to all tables in a SrcList
2105*/
danielk19774adee202004-05-08 08:23:19 +00002106void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002107 int i;
2108 for(i=0; i<pList->nSrc; i++){
2109 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002110 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002111 }
2112 }
2113}
2114
2115/*
drh75897232000-05-29 14:26:00 +00002116** Add an alias to the last identifier on the given identifier list.
2117*/
danielk19774adee202004-05-08 08:23:19 +00002118void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002119 if( pList && pList->nSrc>0 ){
2120 int i = pList->nSrc - 1;
danielk19774adee202004-05-08 08:23:19 +00002121 sqlite3SetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
2122 sqlite3Dequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00002123 }
2124}
2125
2126/*
drhad3cab52002-05-24 02:04:32 +00002127** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002128*/
danielk19774adee202004-05-08 08:23:19 +00002129void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002130 int i;
2131 if( pList==0 ) return;
2132 for(i=0; i<pList->nId; i++){
2133 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002134 }
2135 sqliteFree(pList->a);
2136 sqliteFree(pList);
2137}
2138
2139/*
drhad2d8302002-05-24 20:31:36 +00002140** Return the index in pList of the identifier named zId. Return -1
2141** if not found.
2142*/
danielk19774adee202004-05-08 08:23:19 +00002143int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002144 int i;
2145 if( pList==0 ) return -1;
2146 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002147 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002148 }
2149 return -1;
2150}
2151
2152/*
drhad3cab52002-05-24 02:04:32 +00002153** Delete an entire SrcList including all its substructure.
2154*/
danielk19774adee202004-05-08 08:23:19 +00002155void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002156 int i;
2157 if( pList==0 ) return;
2158 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002159 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002160 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002161 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002162 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
danielk19774adee202004-05-08 08:23:19 +00002163 sqlite3DeleteTable(0, pList->a[i].pTab);
drhdaffd0e2001-04-11 14:28:42 +00002164 }
danielk19774adee202004-05-08 08:23:19 +00002165 sqlite3SelectDelete(pList->a[i].pSelect);
2166 sqlite3ExprDelete(pList->a[i].pOn);
2167 sqlite3IdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002168 }
drh75897232000-05-29 14:26:00 +00002169 sqliteFree(pList);
2170}
2171
drh982cef72000-05-30 16:27:03 +00002172/*
drhc4a3c772001-04-04 11:48:57 +00002173** Begin a transaction
2174*/
danielk19774adee202004-05-08 08:23:19 +00002175void sqlite3BeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002176 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002177 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002178
drh001bbcb2003-03-19 03:14:00 +00002179 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002180 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002181 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002182
2183 v = sqlite3GetVdbe(pParse);
2184 if( !v ) return;
2185 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
2186
2187 /* FIX ME: Need to deal with onError */
drhc4a3c772001-04-04 11:48:57 +00002188}
2189
2190/*
2191** Commit a transaction
2192*/
danielk19774adee202004-05-08 08:23:19 +00002193void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002194 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002195 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002196
drh001bbcb2003-03-19 03:14:00 +00002197 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002198 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002199 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002200
2201 v = sqlite3GetVdbe(pParse);
2202 if( v ){
2203 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002204 }
drhc4a3c772001-04-04 11:48:57 +00002205}
2206
2207/*
2208** Rollback a transaction
2209*/
danielk19774adee202004-05-08 08:23:19 +00002210void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002211 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002212 Vdbe *v;
2213
drh001bbcb2003-03-19 03:14:00 +00002214 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002215 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002216 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002217
danielk19774adee202004-05-08 08:23:19 +00002218 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002219 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002220 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002221 }
drhc4a3c772001-04-04 11:48:57 +00002222}
drhf57b14a2001-09-14 18:54:08 +00002223
2224/*
drh001bbcb2003-03-19 03:14:00 +00002225** Generate VDBE code that will verify the schema cookie for all
2226** named database files.
2227*/
danielk19774adee202004-05-08 08:23:19 +00002228void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh001bbcb2003-03-19 03:14:00 +00002229 sqlite *db = pParse->db;
danielk19774adee202004-05-08 08:23:19 +00002230 Vdbe *v = sqlite3GetVdbe(pParse);
drh8bf8dc92003-05-17 17:35:10 +00002231 assert( iDb>=0 && iDb<db->nDb );
2232 assert( db->aDb[iDb].pBt!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002233 if( iDb!=1 && (iDb>63 || !(pParse->cookieMask & ((u64)1<<iDb))) ){
danielk19774adee202004-05-08 08:23:19 +00002234 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
danielk19771d850a72004-05-31 08:26:49 +00002235 pParse->cookieMask |= ((u64)1<<iDb);
drh001bbcb2003-03-19 03:14:00 +00002236 }
drh001bbcb2003-03-19 03:14:00 +00002237}
2238
2239/*
drh1c928532002-01-31 15:54:21 +00002240** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002241** might change the database.
2242**
2243** This routine starts a new transaction if we are not already within
2244** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002245** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002246** be set for operations that might fail (due to a constraint) part of
2247** the way through and which will need to undo some writes without having to
2248** rollback the whole transaction. For operations where all constraints
2249** can be checked before any changes are made to the database, it is never
2250** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002251**
drh8bf8dc92003-05-17 17:35:10 +00002252** Only database iDb and the temp database are made writable by this call.
2253** If iDb==0, then the main and temp databases are made writable. If
2254** iDb==1 then only the temp database is made writable. If iDb>1 then the
2255** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002256*/
drh7f0f12e2004-05-21 13:39:50 +00002257void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002258 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002259 if( v==0 ) return;
danielk19771d850a72004-05-31 08:26:49 +00002260 sqlite3VdbeAddOp(v, OP_Transaction, iDb, 0);
2261 sqlite3CodeVerifySchema(pParse, iDb);
2262 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002263 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002264 }
2265 if( iDb!=1 ){
2266 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002267 }
2268}
2269
2270/*
drh1c928532002-01-31 15:54:21 +00002271** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002272** the database. If a statement transaction was started, then emit
2273** an OP_Commit that will cause the changes to be committed to disk.
2274**
2275** Note that checkpoints are automatically committed at the end of
2276** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002277** sqlite3BeginWriteOperation() but there should only be a single
2278** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002279*/
danielk19774adee202004-05-08 08:23:19 +00002280void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002281 /* Delete me! */
2282 return;
drh1c928532002-01-31 15:54:21 +00002283}