blob: 30f8d04b9e52052ca876c2f4180985f2ebb2ab4e [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**
danielk197713adf8a2004-06-03 16:08:41 +000026** $Id: build.c,v 1.206 2004/06/03 16:08:41 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 }
danielk1977ee5741e2004-05-31 10:01:34 +0000558 if( db->flags & !db->autoCommit ){
danielk197713adf8a2004-06-03 16:08:41 +0000559 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1, 0);
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
drh9c054832004-05-31 18:51:57 +0000889 if( nType==0 ){
890 return SQLITE_AFF_NONE;
891 }
drh1ad3b9e2004-05-20 12:10:20 +0000892 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
893 int c1 = substrings[i].zSub[0];
894 int c2 = tolower(c1);
895 int limit = nType - substrings[i].nSub;
896 const char *z = substrings[i].zSub;
897 for(n=0; n<=limit; n++){
898 int c = zType[n];
899 if( (c==c1 || c==c2)
900 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +0000901 return substrings[i].affinity;
902 }
903 }
904 }
drh1ad3b9e2004-05-20 12:10:20 +0000905 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +0000906}
907
908/*
drh50e5dad2001-09-15 00:57:28 +0000909** Come up with a new random value for the schema cookie. Make sure
910** the new value is different from the old.
911**
912** The schema cookie is used to determine when the schema for the
913** database changes. After each schema change, the cookie value
914** changes. When a process first reads the schema it records the
915** cookie. Thereafter, whenever it goes to access the database,
916** it checks the cookie to make sure the schema has not changed
917** since it was last read.
918**
919** This plan is not completely bullet-proof. It is possible for
920** the schema to change multiple times and for the cookie to be
921** set back to prior value. But schema changes are infrequent
922** and the probability of hitting the same cookie value is only
923** 1 chance in 2^32. So we're safe enough.
924*/
danielk1977cbb18d22004-05-28 11:37:27 +0000925void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){
danielk19771d850a72004-05-31 08:26:49 +0000926 unsigned char r;
927 int *pSchemaCookie = &(db->aDb[iDb].schema_cookie);
928
929 sqlite3Randomness(1, &r);
930 *pSchemaCookie = *pSchemaCookie + r + 1;
931 db->flags |= SQLITE_InternChanges;
932 sqlite3VdbeAddOp(v, OP_Integer, *pSchemaCookie, 0);
933 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
934/*
drh001bbcb2003-03-19 03:14:00 +0000935 if( db->next_cookie==db->aDb[0].schema_cookie ){
drhbbd82df2004-02-11 09:46:30 +0000936 unsigned char r;
danielk19774adee202004-05-08 08:23:19 +0000937 sqlite3Randomness(1, &r);
drhbbd82df2004-02-11 09:46:30 +0000938 db->next_cookie = db->aDb[0].schema_cookie + r + 1;
drh50e5dad2001-09-15 00:57:28 +0000939 db->flags |= SQLITE_InternChanges;
danielk19774adee202004-05-08 08:23:19 +0000940 sqlite3VdbeAddOp(v, OP_Integer, db->next_cookie, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000941 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +0000942 }
danielk19771d850a72004-05-31 08:26:49 +0000943*/
drh50e5dad2001-09-15 00:57:28 +0000944}
945
946/*
drh969fa7c2002-02-18 18:30:32 +0000947** Measure the number of characters needed to output the given
948** identifier. The number returned includes any quotes used
949** but does not include the null terminator.
950*/
951static int identLength(const char *z){
952 int n;
drh17f71932002-02-21 12:01:27 +0000953 int needQuote = 0;
954 for(n=0; *z; n++, z++){
955 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000956 }
drh17f71932002-02-21 12:01:27 +0000957 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000958}
959
960/*
961** Write an identifier onto the end of the given string. Add
962** quote characters as needed.
963*/
964static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000965 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000966 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000967 for(j=0; zIdent[j]; j++){
968 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
969 }
970 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +0000971 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh17f71932002-02-21 12:01:27 +0000972 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000973 for(j=0; zIdent[j]; j++){
974 z[i++] = zIdent[j];
975 if( zIdent[j]=='\'' ) z[i++] = '\'';
976 }
drh17f71932002-02-21 12:01:27 +0000977 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000978 z[i] = 0;
979 *pIdx = i;
980}
981
982/*
983** Generate a CREATE TABLE statement appropriate for the given
984** table. Memory to hold the text of the statement is obtained
985** from sqliteMalloc() and must be freed by the calling function.
986*/
987static char *createTableStmt(Table *p){
988 int i, k, n;
989 char *zStmt;
990 char *zSep, *zSep2, *zEnd;
991 n = 0;
992 for(i=0; i<p->nCol; i++){
993 n += identLength(p->aCol[i].zName);
994 }
995 n += identLength(p->zName);
996 if( n<40 ){
997 zSep = "";
998 zSep2 = ",";
999 zEnd = ")";
1000 }else{
1001 zSep = "\n ";
1002 zSep2 = ",\n ";
1003 zEnd = "\n)";
1004 }
drhe0bc4042002-06-25 01:09:11 +00001005 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001006 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001007 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001008 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001009 k = strlen(zStmt);
1010 identPut(zStmt, &k, p->zName);
1011 zStmt[k++] = '(';
1012 for(i=0; i<p->nCol; i++){
1013 strcpy(&zStmt[k], zSep);
1014 k += strlen(&zStmt[k]);
1015 zSep = zSep2;
1016 identPut(zStmt, &k, p->aCol[i].zName);
1017 }
1018 strcpy(&zStmt[k], zEnd);
1019 return zStmt;
1020}
1021
1022/*
drh75897232000-05-29 14:26:00 +00001023** This routine is called to report the final ")" that terminates
1024** a CREATE TABLE statement.
1025**
drhf57b3392001-10-08 13:22:32 +00001026** The table structure that other action routines have been building
1027** is added to the internal hash tables, assuming no errors have
1028** occurred.
drh75897232000-05-29 14:26:00 +00001029**
drh1d85d932004-02-14 23:05:52 +00001030** An entry for the table is made in the master table on disk, unless
1031** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001032** it means we are reading the sqlite_master table because we just
1033** connected to the database or because the sqlite_master table has
1034** recently changes, so the entry for this table already exists in
1035** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001036**
1037** If the pSelect argument is not NULL, it means that this routine
1038** was called to create a table generated from a
1039** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1040** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001041*/
danielk19774adee202004-05-08 08:23:19 +00001042void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001043 Table *p;
drhbe0072d2001-09-13 14:46:09 +00001044 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001045
danielk197724b03fd2004-05-10 10:34:34 +00001046 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001047 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001048 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001049
drh969fa7c2002-02-18 18:30:32 +00001050 /* If the table is generated from a SELECT, then construct the
1051 ** list of columns and the text of the table.
1052 */
1053 if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001054 Table *pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +00001055 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +00001056 assert( p->aCol==0 );
1057 p->nCol = pSelTab->nCol;
1058 p->aCol = pSelTab->aCol;
1059 pSelTab->nCol = 0;
1060 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001061 sqlite3DeleteTable(0, pSelTab);
drh969fa7c2002-02-18 18:30:32 +00001062 }
1063
drh1d85d932004-02-14 23:05:52 +00001064 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001065 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1066 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001067 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001068 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001069 */
drh1d85d932004-02-14 23:05:52 +00001070 if( db->init.busy ){
1071 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001072 }
1073
drhe3c41372001-09-17 20:25:58 +00001074 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001075 ** in the SQLITE_MASTER table of the database. The record number
1076 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001077 **
drhe0bc4042002-06-25 01:09:11 +00001078 ** If this is a TEMPORARY table, write the entry into the auxiliary
1079 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001080 */
drh1d85d932004-02-14 23:05:52 +00001081 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001082 int n;
drhd8bc7082000-06-07 23:51:50 +00001083 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001084
danielk19774adee202004-05-08 08:23:19 +00001085 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001086 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +00001087 if( p->pSelect==0 ){
1088 /* A regular table */
danielk19774adee202004-05-08 08:23:19 +00001089 sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
drh4ff6dfa2002-03-03 23:06:00 +00001090 }else{
1091 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001092 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001093 }
drh969fa7c2002-02-18 18:30:32 +00001094 p->tnum = 0;
danielk19774adee202004-05-08 08:23:19 +00001095 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001096 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view", P3_STATIC);
1097 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1098 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00001099 sqlite3VdbeAddOp(v, OP_Dup, 4, 0);
drhe0bc4042002-06-25 01:09:11 +00001100 if( pSelect ){
1101 char *z = createTableStmt(p);
1102 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001103 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001104 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001105 sqliteFree(z);
1106 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001107 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001108 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001109 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001110 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001111 }
drhe0bc4042002-06-25 01:09:11 +00001112 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001113 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001114 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001115 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
1116 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drhe0bc4042002-06-25 01:09:11 +00001117 }
danielk197784ac9d02004-05-18 09:58:06 +00001118 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001119 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk19771d850a72004-05-31 08:26:49 +00001120 if( p->iDb!=1 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001121 sqlite3ChangeCookie(db, v, p->iDb);
drhe0bc4042002-06-25 01:09:11 +00001122 }
danielk19774adee202004-05-08 08:23:19 +00001123 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +00001124 if( pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001125 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1126 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +00001127 pParse->nTab = 2;
danielk197784ac9d02004-05-18 09:58:06 +00001128 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +00001129 }
danielk19774adee202004-05-08 08:23:19 +00001130 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001131 }
drh17e9e292003-02-01 13:53:28 +00001132
1133 /* Add the table to the in-memory representation of the database.
1134 */
drhd24cc422003-03-27 12:51:24 +00001135 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001136 Table *pOld;
1137 FKey *pFKey;
danielk19774adee202004-05-08 08:23:19 +00001138 pOld = sqlite3HashInsert(&db->aDb[p->iDb].tblHash,
drhd24cc422003-03-27 12:51:24 +00001139 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001140 if( pOld ){
1141 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1142 return;
1143 }
1144 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1145 int nTo = strlen(pFKey->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +00001146 pFKey->pNextTo = sqlite3HashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
1147 sqlite3HashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001148 }
1149 pParse->pNewTable = 0;
1150 db->nTable++;
1151 db->flags |= SQLITE_InternChanges;
1152 }
drh75897232000-05-29 14:26:00 +00001153}
1154
1155/*
drha76b5df2002-02-23 02:32:10 +00001156** The parser calls this routine in order to create a new VIEW
1157*/
danielk19774adee202004-05-08 08:23:19 +00001158void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001159 Parse *pParse, /* The parsing context */
1160 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001161 Token *pName1, /* The token that holds the name of the view */
1162 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001163 Select *pSelect, /* A SELECT statement that will become the new view */
1164 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001165){
drha76b5df2002-02-23 02:32:10 +00001166 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001167 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001168 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001169 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001170 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001171 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001172
danielk197748dec7e2004-05-28 12:33:30 +00001173 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001174 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001175 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001176 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001177 return;
1178 }
danielk1977ef2cb632004-05-29 02:37:19 +00001179 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001180 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1181 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001182 ){
danielk19774adee202004-05-08 08:23:19 +00001183 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001184 return;
1185 }
drh174b6192002-12-03 02:22:52 +00001186
drh4b59ab52002-08-24 18:24:51 +00001187 /* Make a copy of the entire SELECT statement that defines the view.
1188 ** This will force all the Expr.token.z values to be dynamically
1189 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001190 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001191 */
danielk19774adee202004-05-08 08:23:19 +00001192 p->pSelect = sqlite3SelectDup(pSelect);
1193 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001194 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001195 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001196 }
drh4b59ab52002-08-24 18:24:51 +00001197
1198 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1199 ** the end.
1200 */
drha76b5df2002-02-23 02:32:10 +00001201 sEnd = pParse->sLastToken;
1202 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1203 sEnd.z += sEnd.n;
1204 }
1205 sEnd.n = 0;
1206 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001207 z = pBegin->z;
1208 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1209 sEnd.z = &z[n-1];
1210 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001211
danielk19774adee202004-05-08 08:23:19 +00001212 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1213 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001214 return;
drh417be792002-03-03 18:59:40 +00001215}
drha76b5df2002-02-23 02:32:10 +00001216
drh417be792002-03-03 18:59:40 +00001217/*
1218** The Table structure pTable is really a VIEW. Fill in the names of
1219** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001220** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001221*/
danielk19774adee202004-05-08 08:23:19 +00001222int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001223 ExprList *pEList;
1224 Select *pSel;
1225 Table *pSelTab;
1226 int nErr = 0;
1227
1228 assert( pTable );
1229
1230 /* A positive nCol means the columns names for this view are
1231 ** already known.
1232 */
1233 if( pTable->nCol>0 ) return 0;
1234
1235 /* A negative nCol is a special marker meaning that we are currently
1236 ** trying to compute the column names. If we enter this routine with
1237 ** a negative nCol, it means two or more views form a loop, like this:
1238 **
1239 ** CREATE VIEW one AS SELECT * FROM two;
1240 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001241 **
1242 ** Actually, this error is caught previously and so the following test
1243 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001244 */
1245 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001246 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001247 return 1;
1248 }
1249
1250 /* If we get this far, it means we need to compute the table names.
1251 */
1252 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1253 pSel = pTable->pSelect;
1254
danielk19774adee202004-05-08 08:23:19 +00001255 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001256 ** "*" elements in this list. But we will need to restore the list
1257 ** back to its original configuration afterwards, so we save a copy of
1258 ** the original in pEList.
1259 */
1260 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001261 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001262 if( pSel->pEList==0 ){
1263 pSel->pEList = pEList;
1264 return 1; /* Malloc failed */
1265 }
1266 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001267 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001268 if( pSelTab ){
1269 assert( pTable->aCol==0 );
1270 pTable->nCol = pSelTab->nCol;
1271 pTable->aCol = pSelTab->aCol;
1272 pSelTab->nCol = 0;
1273 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001274 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001275 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001276 }else{
1277 pTable->nCol = 0;
1278 nErr++;
1279 }
danielk19774adee202004-05-08 08:23:19 +00001280 sqlite3SelectUnbind(pSel);
1281 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001282 pSel->pEList = pEList;
1283 return nErr;
1284}
1285
1286/*
1287** Clear the column names from the VIEW pTable.
1288**
1289** This routine is called whenever any other table or view is modified.
1290** The view passed into this routine might depend directly or indirectly
1291** on the modified or deleted table so we need to clear the old column
1292** names so that they will be recomputed.
1293*/
1294static void sqliteViewResetColumnNames(Table *pTable){
1295 int i;
drh701a0ae2004-02-22 20:05:00 +00001296 Column *pCol;
1297 assert( pTable!=0 && pTable->pSelect!=0 );
1298 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1299 sqliteFree(pCol->zName);
1300 sqliteFree(pCol->zDflt);
1301 sqliteFree(pCol->zType);
drh417be792002-03-03 18:59:40 +00001302 }
1303 sqliteFree(pTable->aCol);
1304 pTable->aCol = 0;
1305 pTable->nCol = 0;
1306}
1307
1308/*
drh8bf8dc92003-05-17 17:35:10 +00001309** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001310*/
drhd24cc422003-03-27 12:51:24 +00001311static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001312 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001313 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001314 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001315 Table *pTab = sqliteHashData(i);
1316 if( pTab->pSelect ){
1317 sqliteViewResetColumnNames(pTab);
1318 }
1319 }
drh8bf8dc92003-05-17 17:35:10 +00001320 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001321}
1322
1323/*
drh75897232000-05-29 14:26:00 +00001324** Given a token, look up a table with that name. If not found, leave
1325** an error for the parser to find and return NULL.
1326*/
danielk19774adee202004-05-08 08:23:19 +00001327Table *sqlite3TableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001328 char *zName;
1329 Table *pTab;
danielk19774adee202004-05-08 08:23:19 +00001330 zName = sqlite3TableNameFromToken(pTok);
drhdaffd0e2001-04-11 14:28:42 +00001331 if( zName==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001332 pTab = sqlite3FindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001333 sqliteFree(zName);
1334 if( pTab==0 ){
danielk19774adee202004-05-08 08:23:19 +00001335 sqlite3ErrorMsg(pParse, "no such table: %T", pTok);
drh75897232000-05-29 14:26:00 +00001336 }
1337 return pTab;
1338}
1339
1340/*
1341** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001342** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001343*/
danielk1977a8858102004-05-28 12:11:21 +00001344void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1345 Table *pTab;
drh75897232000-05-29 14:26:00 +00001346 Vdbe *v;
1347 int base;
drh5edc3122001-09-13 21:53:09 +00001348 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001349 int iDb;
drh75897232000-05-29 14:26:00 +00001350
danielk1977a8858102004-05-28 12:11:21 +00001351 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1352 assert( pName->nSrc==1 );
1353 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1354
1355 if( pTab==0 ) goto exit_drop_table;
1356 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001357 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001358#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001359 {
1360 int code;
danielk1977a8858102004-05-28 12:11:21 +00001361 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1362 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001363 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001364 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001365 }
drhe5f9c642003-01-13 23:27:31 +00001366 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001367 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001368 code = SQLITE_DROP_TEMP_VIEW;
1369 }else{
1370 code = SQLITE_DROP_VIEW;
1371 }
1372 }else{
drhd24cc422003-03-27 12:51:24 +00001373 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001374 code = SQLITE_DROP_TEMP_TABLE;
1375 }else{
1376 code = SQLITE_DROP_TABLE;
1377 }
1378 }
danielk1977a8858102004-05-28 12:11:21 +00001379 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1380 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001381 }
danielk1977a8858102004-05-28 12:11:21 +00001382 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1383 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001384 }
drhe5f9c642003-01-13 23:27:31 +00001385 }
1386#endif
danielk1977a8858102004-05-28 12:11:21 +00001387 if( pTab->readOnly ){
1388 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001389 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001390 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001391 }
danielk1977a8858102004-05-28 12:11:21 +00001392 if( isView && pTab->pSelect==0 ){
1393 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1394 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001395 }
danielk1977a8858102004-05-28 12:11:21 +00001396 if( !isView && pTab->pSelect ){
1397 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1398 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001399 }
drh75897232000-05-29 14:26:00 +00001400
drh1ccde152000-06-17 13:12:39 +00001401 /* Generate code to remove the table from the master table
1402 ** on disk.
1403 */
danielk19774adee202004-05-08 08:23:19 +00001404 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001405 if( v ){
drh905793e2004-02-21 13:31:09 +00001406 static VdbeOpList dropTable[] = {
danielk19778d059842004-05-12 11:24:02 +00001407 { OP_Rewind, 0, ADDR(10), 0},
danielk19770f69c1e2004-05-29 11:24:50 +00001408 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001409 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001410 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001411 { OP_Column, 0, 2, 0},
danielk19778d059842004-05-12 11:24:02 +00001412 { OP_Ne, 0, ADDR(9), 0},
drh75897232000-05-29 14:26:00 +00001413 { OP_Delete, 0, 0, 0},
danielk19778d059842004-05-12 11:24:02 +00001414 { OP_Rewind, 0, ADDR(10), 0},
1415 { OP_Goto, 0, ADDR(3), 0},
1416 { OP_Next, 0, ADDR(3), 0}, /* 9 */
drh75897232000-05-29 14:26:00 +00001417 };
1418 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001419 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001420 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001421
danielk1977c3f9bad2002-05-15 08:30:12 +00001422 /* Drop all triggers associated with the table being dropped */
danielk1977a8858102004-05-28 12:11:21 +00001423 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001424 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001425 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001426 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drhe0bc4042002-06-25 01:09:11 +00001427 if( pParse->explain ){
1428 pTrigger = pTrigger->pNext;
1429 }else{
danielk1977a8858102004-05-28 12:11:21 +00001430 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001431 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001432 }
drh8bf8dc92003-05-17 17:35:10 +00001433
1434 /* Drop all SQLITE_MASTER entries that refer to the table */
danielk1977a8858102004-05-28 12:11:21 +00001435 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001436 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001437 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
drh8bf8dc92003-05-17 17:35:10 +00001438
1439 /* Drop all SQLITE_TEMP_MASTER entries that refer to the table */
danielk1977a8858102004-05-28 12:11:21 +00001440 if( pTab->iDb!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001441 sqlite3OpenMasterTable(v, 1);
1442 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001443 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
drh8bf8dc92003-05-17 17:35:10 +00001444 }
1445
danielk1977a8858102004-05-28 12:11:21 +00001446 if( pTab->iDb!=1 ){ /* Temp database has no schema cookie */
1447 sqlite3ChangeCookie(db, v, pTab->iDb);
drhf57b3392001-10-08 13:22:32 +00001448 }
danielk19774adee202004-05-08 08:23:19 +00001449 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001450 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001451 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1452 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001453 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001454 }
drh5e00f6c2001-09-13 13:46:56 +00001455 }
danielk19774adee202004-05-08 08:23:19 +00001456 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001457 }
1458
drhe0bc4042002-06-25 01:09:11 +00001459 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001460 **
1461 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001462 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001463 */
1464 if( !pParse->explain ){
danielk1977a8858102004-05-28 12:11:21 +00001465 sqliteUnlinkAndDeleteTable(db, pTab);
drh5edc3122001-09-13 21:53:09 +00001466 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001467 }
drhd24cc422003-03-27 12:51:24 +00001468 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001469
1470exit_drop_table:
1471 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001472}
1473
1474/*
drhc2eef3b2002-08-31 18:53:06 +00001475** This routine is called to create a new foreign key on the table
1476** currently under construction. pFromCol determines which columns
1477** in the current table point to the foreign key. If pFromCol==0 then
1478** connect the key to the last column inserted. pTo is the name of
1479** the table referred to. pToCol is a list of tables in the other
1480** pTo table that the foreign key points to. flags contains all
1481** information about the conflict resolution algorithms specified
1482** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1483**
1484** An FKey structure is created and added to the table currently
1485** under construction in the pParse->pNewTable field. The new FKey
1486** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001487** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001488**
1489** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001490** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001491*/
danielk19774adee202004-05-08 08:23:19 +00001492void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001493 Parse *pParse, /* Parsing context */
1494 IdList *pFromCol, /* Columns in this table that point to other table */
1495 Token *pTo, /* Name of the other table */
1496 IdList *pToCol, /* Columns in the other table */
1497 int flags /* Conflict resolution algorithms. */
1498){
1499 Table *p = pParse->pNewTable;
1500 int nByte;
1501 int i;
1502 int nCol;
1503 char *z;
1504 FKey *pFKey = 0;
1505
1506 assert( pTo!=0 );
1507 if( p==0 || pParse->nErr ) goto fk_end;
1508 if( pFromCol==0 ){
1509 int iCol = p->nCol-1;
1510 if( iCol<0 ) goto fk_end;
1511 if( pToCol && pToCol->nId!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001512 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001513 " should reference only one column of table %T",
1514 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001515 goto fk_end;
1516 }
1517 nCol = 1;
1518 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
danielk19774adee202004-05-08 08:23:19 +00001519 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001520 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001521 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001522 goto fk_end;
1523 }else{
1524 nCol = pFromCol->nId;
1525 }
1526 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1527 if( pToCol ){
1528 for(i=0; i<pToCol->nId; i++){
1529 nByte += strlen(pToCol->a[i].zName) + 1;
1530 }
1531 }
1532 pFKey = sqliteMalloc( nByte );
1533 if( pFKey==0 ) goto fk_end;
1534 pFKey->pFrom = p;
1535 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001536 z = (char*)&pFKey[1];
1537 pFKey->aCol = (struct sColMap*)z;
1538 z += sizeof(struct sColMap)*nCol;
1539 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001540 memcpy(z, pTo->z, pTo->n);
1541 z[pTo->n] = 0;
1542 z += pTo->n+1;
1543 pFKey->pNextTo = 0;
1544 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001545 if( pFromCol==0 ){
1546 pFKey->aCol[0].iFrom = p->nCol-1;
1547 }else{
1548 for(i=0; i<nCol; i++){
1549 int j;
1550 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001551 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001552 pFKey->aCol[i].iFrom = j;
1553 break;
1554 }
1555 }
1556 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001557 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001558 "unknown column \"%s\" in foreign key definition",
1559 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001560 goto fk_end;
1561 }
1562 }
1563 }
1564 if( pToCol ){
1565 for(i=0; i<nCol; i++){
1566 int n = strlen(pToCol->a[i].zName);
1567 pFKey->aCol[i].zCol = z;
1568 memcpy(z, pToCol->a[i].zName, n);
1569 z[n] = 0;
1570 z += n+1;
1571 }
1572 }
1573 pFKey->isDeferred = 0;
1574 pFKey->deleteConf = flags & 0xff;
1575 pFKey->updateConf = (flags >> 8 ) & 0xff;
1576 pFKey->insertConf = (flags >> 16 ) & 0xff;
1577
1578 /* Link the foreign key to the table as the last step.
1579 */
1580 p->pFKey = pFKey;
1581 pFKey = 0;
1582
1583fk_end:
1584 sqliteFree(pFKey);
danielk19774adee202004-05-08 08:23:19 +00001585 sqlite3IdListDelete(pFromCol);
1586 sqlite3IdListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001587}
1588
1589/*
1590** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1591** clause is seen as part of a foreign key definition. The isDeferred
1592** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1593** The behavior of the most recently created foreign key is adjusted
1594** accordingly.
1595*/
danielk19774adee202004-05-08 08:23:19 +00001596void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001597 Table *pTab;
1598 FKey *pFKey;
1599 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1600 pFKey->isDeferred = isDeferred;
1601}
1602
1603/*
drh75897232000-05-29 14:26:00 +00001604** Create a new index for an SQL table. pIndex is the name of the index
1605** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001606** be NULL for a primary key or an index that is created to satisfy a
1607** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001608** as the table to be indexed. pParse->pNewTable is a table that is
1609** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001610**
drh382c0242001-10-06 16:33:02 +00001611** pList is a list of columns to be indexed. pList will be NULL if this
1612** is a primary key or unique-constraint on the most recent column added
1613** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001614*/
danielk19774adee202004-05-08 08:23:19 +00001615void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001616 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001617 Token *pName1, /* First part of index name. May be NULL */
1618 Token *pName2, /* Second part of index name. May be NULL */
danielk1977ef2cb632004-05-29 02:37:19 +00001619 SrcList *pTblName, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001620 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001621 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001622 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1623 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1624){
danielk1977cbb18d22004-05-28 11:37:27 +00001625 Table *pTab = 0; /* Table to be indexed */
drh75897232000-05-29 14:26:00 +00001626 Index *pIndex; /* The index to be created */
1627 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001628 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001629 Token nullId; /* Fake token for an empty ID list */
1630 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001631 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001632 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001633
danielk1977cbb18d22004-05-28 11:37:27 +00001634 int iDb; /* Index of the database that is being written */
1635 Token *pName = 0; /* Unqualified name of the index to create */
1636
danielk197724b03fd2004-05-10 10:34:34 +00001637 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001638
drh75897232000-05-29 14:26:00 +00001639 /*
1640 ** Find the table that is to be indexed. Return early if not found.
1641 */
danielk1977cbb18d22004-05-28 11:37:27 +00001642 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001643
1644 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001645 ** to search for the table. 'Fix' the table name to this db
1646 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001647 */
1648 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001649 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001650 if( iDb<0 ) goto exit_create_index;
1651
danielk1977ef2cb632004-05-29 02:37:19 +00001652 /* If the index name was unqualified, check if the the table
1653 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001654 */
danielk1977ef2cb632004-05-29 02:37:19 +00001655 pTab = sqlite3SrcListLookup(pParse, pTblName);
1656 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1657 iDb = 1;
1658 }
1659
1660 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1661 sqlite3FixSrcList(&sFix, pTblName)
1662 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001663 goto exit_create_index;
1664 }
danielk1977ef2cb632004-05-29 02:37:19 +00001665 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1666 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001667 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001668 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001669 }else{
drhe3c41372001-09-17 20:25:58 +00001670 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001671 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001672 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001673 }
danielk1977cbb18d22004-05-28 11:37:27 +00001674
drh75897232000-05-29 14:26:00 +00001675 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001676 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001677 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001678 goto exit_create_index;
1679 }
drha76b5df2002-02-23 02:32:10 +00001680 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001681 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001682 goto exit_create_index;
1683 }
drh4925ca02003-11-27 00:48:57 +00001684 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001685
1686 /*
1687 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001688 ** index or table with the same name.
1689 **
1690 ** Exception: If we are reading the names of permanent indices from the
1691 ** sqlite_master table (because some other process changed the schema) and
1692 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001693 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001694 **
1695 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001696 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1697 ** own name.
drh75897232000-05-29 14:26:00 +00001698 */
drh1d85d932004-02-14 23:05:52 +00001699 if( pName && !db->init.busy ){
drhf57b3392001-10-08 13:22:32 +00001700 Index *pISameName; /* Another index with the same name */
1701 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001702 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001703 if( zName==0 ) goto exit_create_index;
danielk19773df6b252004-05-29 10:23:19 +00001704 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001705 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
drhd24cc422003-03-27 12:51:24 +00001706 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001707 }
danielk19774adee202004-05-08 08:23:19 +00001708 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1709 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
drhd24cc422003-03-27 12:51:24 +00001710 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001711 }
drhd24cc422003-03-27 12:51:24 +00001712 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001713 char zBuf[30];
1714 int n;
1715 Index *pLoop;
1716 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1717 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001718 zName = 0;
danielk19774adee202004-05-08 08:23:19 +00001719 sqlite3SetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001720 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001721 }else{
1722 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001723 }
1724
drhe5f9c642003-01-13 23:27:31 +00001725 /* Check for authorization to create an index.
1726 */
1727#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001728 {
1729 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001730 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001731 goto exit_create_index;
1732 }
1733 i = SQLITE_CREATE_INDEX;
1734 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001735 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001736 goto exit_create_index;
1737 }
drhe5f9c642003-01-13 23:27:31 +00001738 }
1739#endif
1740
drh75897232000-05-29 14:26:00 +00001741 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001742 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001743 ** So create a fake list to simulate this.
1744 */
1745 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001746 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001747 nullId.n = strlen(nullId.z);
danielk19774adee202004-05-08 08:23:19 +00001748 pList = sqlite3IdListAppend(0, &nullId);
drh75897232000-05-29 14:26:00 +00001749 if( pList==0 ) goto exit_create_index;
1750 }
1751
1752 /*
1753 ** Allocate the index structure.
1754 */
drhdcc581c2000-05-30 13:44:19 +00001755 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drhd3d39e92004-05-20 22:16:29 +00001756 (sizeof(int) + sizeof(CollSeq*))*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001757 if( pIndex==0 ) goto exit_create_index;
drhd3d39e92004-05-20 22:16:29 +00001758 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nId];
drh967e8b72000-06-21 13:59:10 +00001759 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001760 strcpy(pIndex->zName, zName);
1761 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001762 pIndex->nColumn = pList->nId;
drhea1ba172003-04-20 00:00:23 +00001763 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001764 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00001765 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00001766
drh1ccde152000-06-17 13:12:39 +00001767 /* Scan the names of the columns of the table to be indexed and
1768 ** load the column indices into the Index structure. Report an error
1769 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001770 */
1771 for(i=0; i<pList->nId; i++){
1772 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001773 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001774 }
1775 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001776 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00001777 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001778 sqliteFree(pIndex);
1779 goto exit_create_index;
1780 }
drh967e8b72000-06-21 13:59:10 +00001781 pIndex->aiColumn[i] = j;
drhd3d39e92004-05-20 22:16:29 +00001782 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
drh75897232000-05-29 14:26:00 +00001783 }
drhd3d39e92004-05-20 22:16:29 +00001784 pIndex->keyInfo.nField = pList->nId;
drh75897232000-05-29 14:26:00 +00001785
1786 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001787 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001788 */
drhd24cc422003-03-27 12:51:24 +00001789 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001790 Index *p;
danielk19774adee202004-05-08 08:23:19 +00001791 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00001792 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001793 if( p ){
1794 assert( p==pIndex ); /* Malloc must have failed */
1795 sqliteFree(pIndex);
1796 goto exit_create_index;
1797 }
drh5e00f6c2001-09-13 13:46:56 +00001798 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001799 }
drh9cfcf5d2002-01-29 18:41:24 +00001800
1801 /* When adding an index to the list of indices for a table, make
1802 ** sure all indices labeled OE_Replace come after all those labeled
1803 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1804 ** and INSERT.
1805 */
1806 if( onError!=OE_Replace || pTab->pIndex==0
1807 || pTab->pIndex->onError==OE_Replace){
1808 pIndex->pNext = pTab->pIndex;
1809 pTab->pIndex = pIndex;
1810 }else{
1811 Index *pOther = pTab->pIndex;
1812 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1813 pOther = pOther->pNext;
1814 }
1815 pIndex->pNext = pOther->pNext;
1816 pOther->pNext = pIndex;
1817 }
drh75897232000-05-29 14:26:00 +00001818
drh1d85d932004-02-14 23:05:52 +00001819 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhd78eeee2001-09-13 16:18:53 +00001820 ** "sqlite_master" table on the disk. So do not write to the disk
drh1d85d932004-02-14 23:05:52 +00001821 ** again. Extract the table number from the db->init.newTnum field.
drhd78eeee2001-09-13 16:18:53 +00001822 */
danielk1977cbb18d22004-05-28 11:37:27 +00001823 if( db->init.busy && pTblName!=0 ){
drh1d85d932004-02-14 23:05:52 +00001824 pIndex->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001825 }
1826
drh1d85d932004-02-14 23:05:52 +00001827 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00001828 ** involves writing the index into the master table and filling in the
1829 ** index with the current table contents.
1830 **
drh1d85d932004-02-14 23:05:52 +00001831 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
1832 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00001833 ** CREATE INDEX statements are read out of the master table. In
1834 ** the latter case the index already exists on disk, which is why
1835 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001836 **
danielk1977cbb18d22004-05-28 11:37:27 +00001837 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001838 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1839 ** has just been created, it contains no data and the index initialization
1840 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001841 */
drh1d85d932004-02-14 23:05:52 +00001842 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00001843 int n;
drhadbca9c2001-09-27 15:11:53 +00001844 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001845 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00001846
danielk19774adee202004-05-08 08:23:19 +00001847 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001848 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00001849 if( pTblName!=0 ){
1850 sqlite3BeginWriteOperation(pParse, 0, iDb);
1851 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00001852 }
danielk19774adee202004-05-08 08:23:19 +00001853 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001854 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
1855 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
1856 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001857 sqlite3VdbeOp3(v, OP_CreateIndex, 0, iDb,(char*)&pIndex->tnum,P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001858 pIndex->tnum = 0;
danielk1977cbb18d22004-05-28 11:37:27 +00001859 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00001860 sqlite3VdbeCode(v,
drh701a0ae2004-02-22 20:05:00 +00001861 OP_Dup, 0, 0,
danielk1977cbb18d22004-05-28 11:37:27 +00001862 OP_Integer, iDb, 0,
drh701a0ae2004-02-22 20:05:00 +00001863 0);
drhd3d39e92004-05-20 22:16:29 +00001864 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
1865 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00001866 }
danielk19770f69c1e2004-05-29 11:24:50 +00001867 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00001868 if( pStart && pEnd ){
drh51846b52004-05-28 16:00:21 +00001869 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001870 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001871 n = Addr(pEnd->z) - Addr(pName->z) + 1;
1872 sqlite3VdbeChangeP3(v, -1, pName->z, n);
1873 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh75897232000-05-29 14:26:00 +00001874 }
danielk197784ac9d02004-05-18 09:58:06 +00001875 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001876 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001877 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00001878 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00001879 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
1880 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00001881 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00001882 lbl2 = sqlite3VdbeMakeLabel(v);
1883 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00001884 lbl1 = sqlite3VdbeCurrentAddr(v);
1885 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00001886 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00001887 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001888 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
1889 sqlite3VdbeResolveLabel(v, lbl2);
1890 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
1891 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001892 }
danielk1977cbb18d22004-05-28 11:37:27 +00001893 if( pTblName!=0 ){
drhf57b3392001-10-08 13:22:32 +00001894 if( !isTemp ){
danielk1977cbb18d22004-05-28 11:37:27 +00001895 sqlite3ChangeCookie(db, v, iDb);
drhf57b3392001-10-08 13:22:32 +00001896 }
danielk19774adee202004-05-08 08:23:19 +00001897 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1898 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001899 }
drh75897232000-05-29 14:26:00 +00001900 }
1901
drh75897232000-05-29 14:26:00 +00001902 /* Clean up before exiting */
1903exit_create_index:
danielk19774adee202004-05-08 08:23:19 +00001904 sqlite3IdListDelete(pList);
danielk1977cbb18d22004-05-28 11:37:27 +00001905 /* sqlite3SrcListDelete(pTable); */
drh75897232000-05-29 14:26:00 +00001906 sqliteFree(zName);
1907 return;
1908}
1909
1910/*
drh74e24cd2002-01-09 03:19:59 +00001911** This routine will drop an existing named index. This routine
1912** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001913*/
danielk19774adee202004-05-08 08:23:19 +00001914void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001915 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001916 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001917 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001918
danielk197724b03fd2004-05-10 10:34:34 +00001919 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001920 assert( pName->nSrc==1 );
danielk19774adee202004-05-08 08:23:19 +00001921 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001922 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00001923 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drhd24cc422003-03-27 12:51:24 +00001924 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001925 }
drh485b39b2002-07-13 03:11:52 +00001926 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00001927 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00001928 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00001929 goto exit_drop_index;
1930 }
danielk1977a8858102004-05-28 12:11:21 +00001931/*
drhd24cc422003-03-27 12:51:24 +00001932 if( pIndex->iDb>1 ){
danielk19774adee202004-05-08 08:23:19 +00001933 sqlite3ErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00001934 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00001935 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001936 }
danielk1977a8858102004-05-28 12:11:21 +00001937*/
drhe5f9c642003-01-13 23:27:31 +00001938#ifndef SQLITE_OMIT_AUTHORIZATION
1939 {
1940 int code = SQLITE_DROP_INDEX;
1941 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00001942 const char *zDb = db->aDb[pIndex->iDb].zName;
1943 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00001944 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001945 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001946 }
drhd24cc422003-03-27 12:51:24 +00001947 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001948 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00001949 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001950 }
drhed6c8672003-01-12 18:02:16 +00001951 }
drhe5f9c642003-01-13 23:27:31 +00001952#endif
drh75897232000-05-29 14:26:00 +00001953
1954 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00001955 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001956 if( v ){
drh905793e2004-02-21 13:31:09 +00001957 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001958 { OP_Rewind, 0, ADDR(9), 0},
danielk19770f69c1e2004-05-29 11:24:50 +00001959 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001960 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001961 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001962 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001963 { OP_Eq, 0, ADDR(8), 0},
1964 { OP_Next, 0, ADDR(3), 0},
1965 { OP_Goto, 0, ADDR(9), 0},
1966 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001967 };
1968 int base;
1969
danielk19774adee202004-05-08 08:23:19 +00001970 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
1971 sqlite3OpenMasterTable(v, pIndex->iDb);
1972 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1973 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001974 if( pIndex->iDb!=1 ){
1975 sqlite3ChangeCookie(db, v, pIndex->iDb);
drhf57b3392001-10-08 13:22:32 +00001976 }
danielk19774adee202004-05-08 08:23:19 +00001977 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1978 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
1979 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001980 }
1981
drhe0bc4042002-06-25 01:09:11 +00001982 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001983 */
1984 if( !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +00001985 sqlite3UnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001986 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001987 }
drhd24cc422003-03-27 12:51:24 +00001988
1989exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00001990 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001991}
1992
1993/*
drh75897232000-05-29 14:26:00 +00001994** Append a new element to the given IdList. Create a new IdList if
1995** need be.
drhdaffd0e2001-04-11 14:28:42 +00001996**
1997** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001998*/
danielk19774adee202004-05-08 08:23:19 +00001999IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002000 if( pList==0 ){
2001 pList = sqliteMalloc( sizeof(IdList) );
2002 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002003 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002004 }
drh4305d102003-07-30 12:34:12 +00002005 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002006 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002007 pList->nAlloc = pList->nAlloc*2 + 5;
2008 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002009 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002010 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002011 return 0;
drh75897232000-05-29 14:26:00 +00002012 }
drh6d4abfb2001-10-22 02:58:08 +00002013 pList->a = a;
drh75897232000-05-29 14:26:00 +00002014 }
2015 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
2016 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00002017 char **pz = &pList->a[pList->nId].zName;
danielk19774adee202004-05-08 08:23:19 +00002018 sqlite3SetNString(pz, pToken->z, pToken->n, 0);
drhdaffd0e2001-04-11 14:28:42 +00002019 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002020 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002021 return 0;
2022 }else{
danielk19774adee202004-05-08 08:23:19 +00002023 sqlite3Dequote(*pz);
drhdaffd0e2001-04-11 14:28:42 +00002024 }
drh75897232000-05-29 14:26:00 +00002025 }
2026 pList->nId++;
2027 return pList;
2028}
2029
2030/*
drhad3cab52002-05-24 02:04:32 +00002031** Append a new table name to the given SrcList. Create a new SrcList if
2032** need be. A new entry is created in the SrcList even if pToken is NULL.
2033**
2034** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002035**
2036** If pDatabase is not null, it means that the table has an optional
2037** database name prefix. Like this: "database.table". The pDatabase
2038** points to the table name and the pTable points to the database name.
2039** The SrcList.a[].zName field is filled with the table name which might
2040** come from pTable (if pDatabase is NULL) or from pDatabase.
2041** SrcList.a[].zDatabase is filled with the database name from pTable,
2042** or with NULL if no database is specified.
2043**
2044** In other words, if call like this:
2045**
danielk19774adee202004-05-08 08:23:19 +00002046** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002047**
2048** Then B is a table name and the database name is unspecified. If called
2049** like this:
2050**
danielk19774adee202004-05-08 08:23:19 +00002051** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002052**
2053** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002054*/
danielk19774adee202004-05-08 08:23:19 +00002055SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00002056 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002057 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002058 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002059 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002060 }
drh4305d102003-07-30 12:34:12 +00002061 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002062 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002063 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002064 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002065 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002066 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002067 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002068 return 0;
2069 }
drh113088e2003-03-20 01:16:58 +00002070 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002071 }
2072 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002073 if( pDatabase && pDatabase->z==0 ){
2074 pDatabase = 0;
2075 }
2076 if( pDatabase && pTable ){
2077 Token *pTemp = pDatabase;
2078 pDatabase = pTable;
2079 pTable = pTemp;
2080 }
2081 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00002082 char **pz = &pList->a[pList->nSrc].zName;
danielk19774adee202004-05-08 08:23:19 +00002083 sqlite3SetNString(pz, pTable->z, pTable->n, 0);
drh113088e2003-03-20 01:16:58 +00002084 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002085 sqlite3SrcListDelete(pList);
drh113088e2003-03-20 01:16:58 +00002086 return 0;
2087 }else{
danielk19774adee202004-05-08 08:23:19 +00002088 sqlite3Dequote(*pz);
drh113088e2003-03-20 01:16:58 +00002089 }
2090 }
2091 if( pDatabase ){
2092 char **pz = &pList->a[pList->nSrc].zDatabase;
danielk19774adee202004-05-08 08:23:19 +00002093 sqlite3SetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00002094 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002095 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002096 return 0;
2097 }else{
danielk19774adee202004-05-08 08:23:19 +00002098 sqlite3Dequote(*pz);
drhad3cab52002-05-24 02:04:32 +00002099 }
2100 }
drh63eb5f22003-04-29 16:20:44 +00002101 pList->a[pList->nSrc].iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002102 pList->nSrc++;
2103 return pList;
2104}
2105
2106/*
drh63eb5f22003-04-29 16:20:44 +00002107** Assign cursors to all tables in a SrcList
2108*/
danielk19774adee202004-05-08 08:23:19 +00002109void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002110 int i;
2111 for(i=0; i<pList->nSrc; i++){
2112 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002113 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002114 }
2115 }
2116}
2117
2118/*
drh75897232000-05-29 14:26:00 +00002119** Add an alias to the last identifier on the given identifier list.
2120*/
danielk19774adee202004-05-08 08:23:19 +00002121void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002122 if( pList && pList->nSrc>0 ){
2123 int i = pList->nSrc - 1;
danielk19774adee202004-05-08 08:23:19 +00002124 sqlite3SetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
2125 sqlite3Dequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00002126 }
2127}
2128
2129/*
drhad3cab52002-05-24 02:04:32 +00002130** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002131*/
danielk19774adee202004-05-08 08:23:19 +00002132void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002133 int i;
2134 if( pList==0 ) return;
2135 for(i=0; i<pList->nId; i++){
2136 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002137 }
2138 sqliteFree(pList->a);
2139 sqliteFree(pList);
2140}
2141
2142/*
drhad2d8302002-05-24 20:31:36 +00002143** Return the index in pList of the identifier named zId. Return -1
2144** if not found.
2145*/
danielk19774adee202004-05-08 08:23:19 +00002146int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002147 int i;
2148 if( pList==0 ) return -1;
2149 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002150 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002151 }
2152 return -1;
2153}
2154
2155/*
drhad3cab52002-05-24 02:04:32 +00002156** Delete an entire SrcList including all its substructure.
2157*/
danielk19774adee202004-05-08 08:23:19 +00002158void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002159 int i;
2160 if( pList==0 ) return;
2161 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002162 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002163 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002164 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002165 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
danielk19774adee202004-05-08 08:23:19 +00002166 sqlite3DeleteTable(0, pList->a[i].pTab);
drhdaffd0e2001-04-11 14:28:42 +00002167 }
danielk19774adee202004-05-08 08:23:19 +00002168 sqlite3SelectDelete(pList->a[i].pSelect);
2169 sqlite3ExprDelete(pList->a[i].pOn);
2170 sqlite3IdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002171 }
drh75897232000-05-29 14:26:00 +00002172 sqliteFree(pList);
2173}
2174
drh982cef72000-05-30 16:27:03 +00002175/*
drhc4a3c772001-04-04 11:48:57 +00002176** Begin a transaction
2177*/
danielk197733752f82004-05-31 08:55:33 +00002178void sqlite3BeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002179 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002180 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002181
drh001bbcb2003-03-19 03:14:00 +00002182 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002183 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002184 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002185
2186 v = sqlite3GetVdbe(pParse);
2187 if( !v ) return;
2188 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002189}
2190
2191/*
2192** Commit a transaction
2193*/
danielk19774adee202004-05-08 08:23:19 +00002194void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002195 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002196 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002197
drh001bbcb2003-03-19 03:14:00 +00002198 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002199 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002200 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002201
2202 v = sqlite3GetVdbe(pParse);
2203 if( v ){
2204 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002205 }
drhc4a3c772001-04-04 11:48:57 +00002206}
2207
2208/*
2209** Rollback a transaction
2210*/
danielk19774adee202004-05-08 08:23:19 +00002211void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002212 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002213 Vdbe *v;
2214
drh001bbcb2003-03-19 03:14:00 +00002215 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002216 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002217 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002218
danielk19774adee202004-05-08 08:23:19 +00002219 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002220 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002221 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002222 }
drhc4a3c772001-04-04 11:48:57 +00002223}
drhf57b14a2001-09-14 18:54:08 +00002224
2225/*
drh001bbcb2003-03-19 03:14:00 +00002226** Generate VDBE code that will verify the schema cookie for all
2227** named database files.
2228*/
danielk19774adee202004-05-08 08:23:19 +00002229void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh001bbcb2003-03-19 03:14:00 +00002230 sqlite *db = pParse->db;
danielk19774adee202004-05-08 08:23:19 +00002231 Vdbe *v = sqlite3GetVdbe(pParse);
danielk197711146c92004-05-31 11:51:44 +00002232 if( v==0 ) return;
drh8bf8dc92003-05-17 17:35:10 +00002233 assert( iDb>=0 && iDb<db->nDb );
2234 assert( db->aDb[iDb].pBt!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002235 if( iDb!=1 && (iDb>63 || !(pParse->cookieMask & ((u64)1<<iDb))) ){
danielk197711146c92004-05-31 11:51:44 +00002236 sqlite3VdbeAddOp(v, OP_Transaction, iDb, 0);
danielk19774adee202004-05-08 08:23:19 +00002237 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
danielk19771d850a72004-05-31 08:26:49 +00002238 pParse->cookieMask |= ((u64)1<<iDb);
drh001bbcb2003-03-19 03:14:00 +00002239 }
drh001bbcb2003-03-19 03:14:00 +00002240}
2241
2242/*
drh1c928532002-01-31 15:54:21 +00002243** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002244** might change the database.
2245**
2246** This routine starts a new transaction if we are not already within
2247** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002248** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002249** be set for operations that might fail (due to a constraint) part of
2250** the way through and which will need to undo some writes without having to
2251** rollback the whole transaction. For operations where all constraints
2252** can be checked before any changes are made to the database, it is never
2253** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002254**
drh8bf8dc92003-05-17 17:35:10 +00002255** Only database iDb and the temp database are made writable by this call.
2256** If iDb==0, then the main and temp databases are made writable. If
2257** iDb==1 then only the temp database is made writable. If iDb>1 then the
2258** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002259*/
drh7f0f12e2004-05-21 13:39:50 +00002260void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk197711146c92004-05-31 11:51:44 +00002261 sqlite *db = pParse->db;
danielk19771d850a72004-05-31 08:26:49 +00002262 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002263 if( v==0 ) return;
danielk1977ee5741e2004-05-31 10:01:34 +00002264 sqlite3VdbeAddOp(v, OP_Transaction, iDb, 1);
danielk197711146c92004-05-31 11:51:44 +00002265 if( iDb!=1 && (iDb>63 || !(pParse->cookieMask & ((u64)1<<iDb))) ){
2266 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, db->aDb[iDb].schema_cookie);
2267 pParse->cookieMask |= ((u64)1<<iDb);
2268 }
danielk19771d850a72004-05-31 08:26:49 +00002269 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002270 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002271 }
2272 if( iDb!=1 ){
2273 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002274 }
2275}
2276
2277/*
drh1c928532002-01-31 15:54:21 +00002278** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002279** the database. If a statement transaction was started, then emit
2280** an OP_Commit that will cause the changes to be committed to disk.
2281**
2282** Note that checkpoints are automatically committed at the end of
2283** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002284** sqlite3BeginWriteOperation() but there should only be a single
2285** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002286*/
danielk19774adee202004-05-08 08:23:19 +00002287void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002288 /* Delete me! */
2289 return;
drh1c928532002-01-31 15:54:21 +00002290}