blob: 23c2fbeefd79e3b7ceeea8a0876dc62b5d4faf2f [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**
drh205f48e2004-11-05 00:43:11 +000026** $Id: build.c,v 1.261 2004/11/05 00:43:12 drh Exp $
drh75897232000-05-29 14:26:00 +000027*/
28#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000029#include <ctype.h>
drh75897232000-05-29 14:26:00 +000030
31/*
drhe0bc4042002-06-25 01:09:11 +000032** This routine is called when a new SQL statement is beginning to
33** be parsed. Check to see if the schema for the database needs
34** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
35** If it does, then read it.
36*/
danielk19774adee202004-05-08 08:23:19 +000037void sqlite3BeginParse(Parse *pParse, int explainFlag){
drhe0bc4042002-06-25 01:09:11 +000038 pParse->explain = explainFlag;
drh7c972de2003-09-06 22:18:07 +000039 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000040}
41
42/*
drh75897232000-05-29 14:26:00 +000043** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12 +000044** parsed and a VDBE program to execute that statement has been
45** prepared. This routine puts the finishing touches on the
46** VDBE program and resets the pParse structure for the next
47** parse.
drh75897232000-05-29 14:26:00 +000048**
49** Note that if an error occurred, it might be the case that
50** no VDBE code was generated.
51*/
drh80242052004-06-09 00:48:12 +000052void sqlite3FinishCoding(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +000053 sqlite3 *db;
drh80242052004-06-09 00:48:12 +000054 Vdbe *v;
drhb86ccfb2003-01-28 23:13:10 +000055
danielk197724b03fd2004-05-10 10:34:34 +000056 if( sqlite3_malloc_failed ) return;
drh205f48e2004-11-05 00:43:11 +000057 if( pParse->nested ) return;
drh80242052004-06-09 00:48:12 +000058
59 /* Begin by generating some termination code at the end of the
60 ** vdbe program
61 */
62 db = pParse->db;
63 v = sqlite3GetVdbe(pParse);
64 if( v ){
65 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
drh0e3d7472004-06-19 17:33:07 +000066
67 /* The cookie mask contains one bit for each database file open.
68 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
69 ** set for each database that is used. Generate code to start a
70 ** transaction on each used database and to verify the schema cookie
71 ** on each used database.
72 */
drhc275b4e2004-07-19 17:25:24 +000073 if( pParse->cookieGoto>0 ){
drh80242052004-06-09 00:48:12 +000074 u32 mask;
75 int iDb;
drhc275b4e2004-07-19 17:25:24 +000076 sqlite3VdbeChangeP2(v, pParse->cookieGoto-1, sqlite3VdbeCurrentAddr(v));
drh80242052004-06-09 00:48:12 +000077 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
78 if( (mask & pParse->cookieMask)==0 ) continue;
79 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
drhc275b4e2004-07-19 17:25:24 +000080 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
drh80242052004-06-09 00:48:12 +000081 }
drhc275b4e2004-07-19 17:25:24 +000082 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto);
drh80242052004-06-09 00:48:12 +000083 }
drh80242052004-06-09 00:48:12 +000084
drh71c697e2004-08-08 23:39:19 +000085 /* Add a No-op that contains the complete text of the compiled SQL
86 ** statement as its P3 argument. This does not change the functionality
drhc16a03b2004-09-15 13:38:10 +000087 ** of the program.
88 **
89 ** This is used to implement sqlite3_trace() functionality.
drh71c697e2004-08-08 23:39:19 +000090 */
91 sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql);
drh71c697e2004-08-08 23:39:19 +000092 }
93
drh3f7d4e42004-07-24 14:35:58 +000094
drh80242052004-06-09 00:48:12 +000095 /* Get the VDBE program ready for execution
96 */
drhb86ccfb2003-01-28 23:13:10 +000097 if( v && pParse->nErr==0 ){
98 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +000099 sqlite3VdbeTrace(v, trace);
drh290c1942004-08-21 17:54:45 +0000100 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3,
101 pParse->nTab+3, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +0000102 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000103 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +0000104 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +0000105 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000106 }
drha226d052002-09-25 19:04:07 +0000107 pParse->nTab = 0;
108 pParse->nMem = 0;
109 pParse->nSet = 0;
110 pParse->nAgg = 0;
drh7c972de2003-09-06 22:18:07 +0000111 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000112 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000113 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000114}
115
116/*
drh205f48e2004-11-05 00:43:11 +0000117** Run the parser and code generator recursively in order to generate
118** code for the SQL statement given onto the end of the pParse context
119** currently under construction. When the parser is run recursively
120** this way, the final OP_Halt is not appended and other initialization
121** and finalization steps are omitted because those are handling by the
122** outermost parser.
123**
124** Not everything is nestable. This facility is designed to permit
125** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
126** care if you decide to try to use this routine for some other purpose.
127*/
128void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
129 va_list ap;
130 char *zSql;
131 int rc;
132 Parse savedState;
133 if( pParse->nErr ) return;
134 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
135 va_start(ap, zFormat);
136 zSql = sqlite3VMPrintf(zFormat, ap);
137 va_end(ap);
138 pParse->nested++;
139 savedState = *pParse;
140 rc = sqlite3RunParser(pParse, zSql, 0);
141 sqliteFree(zSql);
142 pParse->nested--;
143}
144
145/*
danielk19778a414492004-06-29 08:59:35 +0000146** Locate the in-memory structure that describes a particular database
147** table given the name of that table and (optionally) the name of the
148** database containing the table. Return NULL if not found.
drha69d9162003-04-17 22:57:53 +0000149**
danielk19778a414492004-06-29 08:59:35 +0000150** If zDatabase is 0, all databases are searched for the table and the
151** first matching table is returned. (No checking for duplicate table
152** names is done.) The search order is TEMP first, then MAIN, then any
153** auxiliary databases added using the ATTACH command.
drhf26e09c2003-05-31 16:21:12 +0000154**
danielk19774adee202004-05-08 08:23:19 +0000155** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000156*/
drh9bb575f2004-09-06 17:24:11 +0000157Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000158 Table *p = 0;
159 int i;
drh645f63e2004-06-22 13:22:40 +0000160 assert( zName!=0 );
danielk19778a414492004-06-29 08:59:35 +0000161 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
162 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000163 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000164 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
165 p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000166 if( p ) break;
167 }
drh74e24cd2002-01-09 03:19:59 +0000168 return p;
drh75897232000-05-29 14:26:00 +0000169}
170
171/*
danielk19778a414492004-06-29 08:59:35 +0000172** Locate the in-memory structure that describes a particular database
173** table given the name of that table and (optionally) the name of the
174** database containing the table. Return NULL if not found. Also leave an
175** error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000176**
danielk19778a414492004-06-29 08:59:35 +0000177** The difference between this routine and sqlite3FindTable() is that this
178** routine leaves an error message in pParse->zErrMsg where
179** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000180*/
danielk19774adee202004-05-08 08:23:19 +0000181Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000182 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000183
danielk19778a414492004-06-29 08:59:35 +0000184 /* Read the database schema. If an error occurs, leave an error message
185 ** and code in pParse and return NULL. */
186 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
187 return 0;
188 }
189
danielk19774adee202004-05-08 08:23:19 +0000190 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000191 if( p==0 ){
danielk19778a414492004-06-29 08:59:35 +0000192 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000193 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
194 }else if( sqlite3FindTable(pParse->db, zName, 0)!=0 ){
195 sqlite3ErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
drhf26e09c2003-05-31 16:21:12 +0000196 zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000197 }else{
danielk19774adee202004-05-08 08:23:19 +0000198 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000199 }
drha6ecd332004-06-10 00:29:09 +0000200 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000201 }
202 return p;
203}
204
205/*
206** Locate the in-memory structure that describes
207** a particular index given the name of that index
208** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000209** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000210**
211** If zDatabase is 0, all databases are searched for the
212** table and the first matching index is returned. (No checking
213** for duplicate index names is done.) The search order is
214** TEMP first, then MAIN, then any auxiliary databases added
215** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000216*/
drh9bb575f2004-09-06 17:24:11 +0000217Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000218 Index *p = 0;
219 int i;
danielk19778a414492004-06-29 08:59:35 +0000220 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
221 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000222 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000223 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
224 p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000225 if( p ) break;
226 }
drh74e24cd2002-01-09 03:19:59 +0000227 return p;
drh75897232000-05-29 14:26:00 +0000228}
229
230/*
drh956bc922004-07-24 17:38:29 +0000231** Reclaim the memory used by an index
232*/
233static void freeIndex(Index *p){
234 sqliteFree(p->zColAff);
235 sqliteFree(p);
236}
237
238/*
drh75897232000-05-29 14:26:00 +0000239** Remove the given index from the index hash table, and free
240** its memory structures.
241**
drhd229ca92002-01-09 13:30:41 +0000242** The index is removed from the database hash tables but
243** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000244** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000245*/
drh9bb575f2004-09-06 17:24:11 +0000246static void sqliteDeleteIndex(sqlite3 *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000247 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000248
drhd229ca92002-01-09 13:30:41 +0000249 assert( db!=0 && p->zName!=0 );
danielk19774adee202004-05-08 08:23:19 +0000250 pOld = sqlite3HashInsert(&db->aDb[p->iDb].idxHash, p->zName,
drhd24cc422003-03-27 12:51:24 +0000251 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000252 if( pOld!=0 && pOld!=p ){
danielk19774adee202004-05-08 08:23:19 +0000253 sqlite3HashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
drhd24cc422003-03-27 12:51:24 +0000254 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000255 }
drh956bc922004-07-24 17:38:29 +0000256 freeIndex(p);
drh75897232000-05-29 14:26:00 +0000257}
258
259/*
drhbeae3192001-09-22 18:12:08 +0000260** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000261** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000262** structures.
263*/
drh9bb575f2004-09-06 17:24:11 +0000264void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
drh956bc922004-07-24 17:38:29 +0000265 Index *pIndex;
266 int len;
267
268 len = strlen(zIdxName);
269 pIndex = sqlite3HashInsert(&db->aDb[iDb].idxHash, zIdxName, len+1, 0);
270 if( pIndex ){
271 if( pIndex->pTable->pIndex==pIndex ){
272 pIndex->pTable->pIndex = pIndex->pNext;
273 }else{
274 Index *p;
275 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
276 if( p && p->pNext==pIndex ){
277 p->pNext = pIndex->pNext;
278 }
drh5e00f6c2001-09-13 13:46:56 +0000279 }
drh956bc922004-07-24 17:38:29 +0000280 freeIndex(pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000281 }
drh956bc922004-07-24 17:38:29 +0000282 db->flags |= SQLITE_InternChanges;
drh5e00f6c2001-09-13 13:46:56 +0000283}
284
285/*
drhe0bc4042002-06-25 01:09:11 +0000286** Erase all schema information from the in-memory hash tables of
drh234c39d2004-07-24 03:30:47 +0000287** a single database. This routine is called to reclaim memory
288** before the database closes. It is also called during a rollback
danielk1977e0d4b062004-06-28 01:11:46 +0000289** if there were schema changes during the transaction or if a
290** schema-cookie mismatch occurs.
drh1c2d8412003-03-31 00:30:47 +0000291**
292** If iDb<=0 then reset the internal schema tables for all database
293** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000294** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000295*/
drh9bb575f2004-09-06 17:24:11 +0000296void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000297 HashElem *pElem;
298 Hash temp1;
299 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000300 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000301
drh1c2d8412003-03-31 00:30:47 +0000302 assert( iDb>=0 && iDb<db->nDb );
303 db->flags &= ~SQLITE_Initialized;
304 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000305 Db *pDb = &db->aDb[i];
306 temp1 = pDb->tblHash;
307 temp2 = pDb->trigHash;
danielk19774adee202004-05-08 08:23:19 +0000308 sqlite3HashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
309 sqlite3HashClear(&pDb->aFKey);
310 sqlite3HashClear(&pDb->idxHash);
drhd24cc422003-03-27 12:51:24 +0000311 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
drh40e016e2004-11-04 14:47:11 +0000312 sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
drhd24cc422003-03-27 12:51:24 +0000313 }
danielk19774adee202004-05-08 08:23:19 +0000314 sqlite3HashClear(&temp2);
315 sqlite3HashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
drhd24cc422003-03-27 12:51:24 +0000316 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
317 Table *pTab = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000318 sqlite3DeleteTable(db, pTab);
drhd24cc422003-03-27 12:51:24 +0000319 }
danielk19774adee202004-05-08 08:23:19 +0000320 sqlite3HashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000321 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000322 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000323 }
drh1c2d8412003-03-31 00:30:47 +0000324 assert( iDb==0 );
325 db->flags &= ~SQLITE_InternChanges;
326
327 /* If one or more of the auxiliary database files has been closed,
328 ** then remove then from the auxiliary database list. We take the
329 ** opportunity to do this here since we have just deleted all of the
330 ** schema hash tables and therefore do not have to make any changes
331 ** to any of those tables.
332 */
drh4d189ca2004-02-12 18:46:38 +0000333 for(i=0; i<db->nDb; i++){
334 struct Db *pDb = &db->aDb[i];
335 if( pDb->pBt==0 ){
336 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
337 pDb->pAux = 0;
338 }
339 }
drh1c2d8412003-03-31 00:30:47 +0000340 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000341 struct Db *pDb = &db->aDb[i];
342 if( pDb->pBt==0 ){
343 sqliteFree(pDb->zName);
344 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000345 continue;
346 }
347 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000348 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000349 }
drh8bf8dc92003-05-17 17:35:10 +0000350 j++;
drh1c2d8412003-03-31 00:30:47 +0000351 }
352 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
353 db->nDb = j;
354 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
355 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
356 sqliteFree(db->aDb);
357 db->aDb = db->aDbStatic;
358 }
drhe0bc4042002-06-25 01:09:11 +0000359}
360
361/*
362** This routine is called whenever a rollback occurs. If there were
363** schema changes during the transaction, then we have to reset the
364** internal hash tables and reload them from disk.
365*/
drh9bb575f2004-09-06 17:24:11 +0000366void sqlite3RollbackInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000367 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000368 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000369 }
370}
371
372/*
373** This routine is called when a commit occurs.
374*/
drh9bb575f2004-09-06 17:24:11 +0000375void sqlite3CommitInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000376 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000377}
378
379/*
drh956bc922004-07-24 17:38:29 +0000380** Clear the column names from a table or view.
381*/
382static void sqliteResetColumnNames(Table *pTable){
383 int i;
384 Column *pCol;
385 assert( pTable!=0 );
386 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
387 sqliteFree(pCol->zName);
388 sqliteFree(pCol->zDflt);
389 sqliteFree(pCol->zType);
390 }
391 sqliteFree(pTable->aCol);
392 pTable->aCol = 0;
393 pTable->nCol = 0;
394}
395
396/*
drh75897232000-05-29 14:26:00 +0000397** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000398** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000399**
400** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000401** the table data structure from the hash table. Nor does it remove
402** foreign keys from the sqlite.aFKey hash table. But it does destroy
403** memory structures of the indices and foreign keys associated with
404** the table.
drhdaffd0e2001-04-11 14:28:42 +0000405**
406** Indices associated with the table are unlinked from the "db"
407** data structure if db!=NULL. If db==NULL, indices attached to
408** the table are deleted, but it is assumed they have already been
409** unlinked.
drh75897232000-05-29 14:26:00 +0000410*/
drh9bb575f2004-09-06 17:24:11 +0000411void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000412 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000413 FKey *pFKey, *pNextFKey;
414
drh75897232000-05-29 14:26:00 +0000415 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000416
417 /* Delete all indices associated with this table
418 */
419 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
420 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000421 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000422 sqliteDeleteIndex(db, pIndex);
423 }
424
425 /* Delete all foreign keys associated with this table. The keys
426 ** should have already been unlinked from the db->aFKey hash table
427 */
428 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
429 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000430 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000431 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000432 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000433 sqliteFree(pFKey);
434 }
435
436 /* Delete the Table structure itself.
437 */
drh956bc922004-07-24 17:38:29 +0000438 sqliteResetColumnNames(pTable);
drh6e142f52000-06-08 13:36:40 +0000439 sqliteFree(pTable->zName);
drh956bc922004-07-24 17:38:29 +0000440 sqliteFree(pTable->zColAff);
danielk19774adee202004-05-08 08:23:19 +0000441 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000442 sqliteFree(pTable);
443}
444
445/*
drh5edc3122001-09-13 21:53:09 +0000446** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000447** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000448*/
drh9bb575f2004-09-06 17:24:11 +0000449void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
drh956bc922004-07-24 17:38:29 +0000450 Table *p;
drhc2eef3b2002-08-31 18:53:06 +0000451 FKey *pF1, *pF2;
drh956bc922004-07-24 17:38:29 +0000452 Db *pDb;
453
drhd229ca92002-01-09 13:30:41 +0000454 assert( db!=0 );
drh956bc922004-07-24 17:38:29 +0000455 assert( iDb>=0 && iDb<db->nDb );
456 assert( zTabName && zTabName[0] );
457 pDb = &db->aDb[iDb];
458 p = sqlite3HashInsert(&pDb->tblHash, zTabName, strlen(zTabName)+1, 0);
459 if( p ){
460 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
461 int nTo = strlen(pF1->zTo) + 1;
462 pF2 = sqlite3HashFind(&pDb->aFKey, pF1->zTo, nTo);
463 if( pF2==pF1 ){
464 sqlite3HashInsert(&pDb->aFKey, pF1->zTo, nTo, pF1->pNextTo);
465 }else{
466 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
467 if( pF2 ){
468 pF2->pNextTo = pF1->pNextTo;
469 }
drhc2eef3b2002-08-31 18:53:06 +0000470 }
471 }
drh956bc922004-07-24 17:38:29 +0000472 sqlite3DeleteTable(db, p);
drhc2eef3b2002-08-31 18:53:06 +0000473 }
drh956bc922004-07-24 17:38:29 +0000474 db->flags |= SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000475}
476
477/*
drha99db3b2004-06-19 14:49:12 +0000478** Given a token, return a string that consists of the text of that
479** token with any quotations removed. Space to hold the returned string
480** is obtained from sqliteMalloc() and must be freed by the calling
481** function.
drh75897232000-05-29 14:26:00 +0000482**
drha99db3b2004-06-19 14:49:12 +0000483** Tokens are really just pointers into the original SQL text and so
484** are not \000 terminated and are not persistent. The returned string
485** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000486*/
drha99db3b2004-06-19 14:49:12 +0000487char *sqlite3NameFromToken(Token *pName){
488 char *zName;
489 if( pName ){
490 zName = sqliteStrNDup(pName->z, pName->n);
491 sqlite3Dequote(zName);
492 }else{
493 zName = 0;
494 }
drh75897232000-05-29 14:26:00 +0000495 return zName;
496}
497
498/*
danielk1977cbb18d22004-05-28 11:37:27 +0000499** Open the sqlite_master table stored in database number iDb for
500** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000501*/
danielk1977cbb18d22004-05-28 11:37:27 +0000502void sqlite3OpenMasterTable(Vdbe *v, int iDb){
503 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000504 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000505 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000506}
507
508/*
danielk1977cbb18d22004-05-28 11:37:27 +0000509** The token *pName contains the name of a database (either "main" or
510** "temp" or the name of an attached db). This routine returns the
511** index of the named database in db->aDb[], or -1 if the named db
512** does not exist.
513*/
514int findDb(sqlite3 *db, Token *pName){
515 int i;
drh90f5ecb2004-07-22 01:19:35 +0000516 Db *pDb;
517 for(pDb=db->aDb, i=0; i<db->nDb; i++, pDb++){
518 if( pName->n==strlen(pDb->zName) &&
519 0==sqlite3StrNICmp(pDb->zName, pName->z, pName->n) ){
danielk1977cbb18d22004-05-28 11:37:27 +0000520 return i;
521 }
522 }
523 return -1;
524}
525
drh0e3d7472004-06-19 17:33:07 +0000526/* The table or view or trigger name is passed to this routine via tokens
527** pName1 and pName2. If the table name was fully qualified, for example:
528**
529** CREATE TABLE xxx.yyy (...);
530**
531** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
532** the table name is not fully qualified, i.e.:
533**
534** CREATE TABLE yyy(...);
535**
536** Then pName1 is set to "yyy" and pName2 is "".
537**
538** This routine sets the *ppUnqual pointer to point at the token (pName1 or
539** pName2) that stores the unqualified table name. The index of the
540** database "xxx" is returned.
541*/
danielk1977ef2cb632004-05-29 02:37:19 +0000542int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000543 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35 +0000544 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07 +0000545 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
546 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000547){
drh0e3d7472004-06-19 17:33:07 +0000548 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000549 sqlite3 *db = pParse->db;
550
551 if( pName2 && pName2->n>0 ){
552 assert( !db->init.busy );
553 *pUnqual = pName2;
554 iDb = findDb(db, pName1);
555 if( iDb<0 ){
556 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
557 pParse->nErr++;
558 return -1;
559 }
560 }else{
561 assert( db->init.iDb==0 || db->init.busy );
562 iDb = db->init.iDb;
563 *pUnqual = pName1;
564 }
565 return iDb;
566}
567
568/*
danielk1977d8123362004-06-12 09:25:12 +0000569** This routine is used to check if the UTF-8 string zName is a legal
570** unqualified name for a new schema object (table, index, view or
571** trigger). All names are legal except those that begin with the string
572** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
573** is reserved for internal use.
574*/
575int sqlite3CheckObjectName(Parse *pParse, const char *zName){
576 if( !pParse->db->init.busy && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
577 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
578 return SQLITE_ERROR;
579 }
580 return SQLITE_OK;
581}
582
583/*
drh75897232000-05-29 14:26:00 +0000584** Begin constructing a new table representation in memory. This is
585** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000586** to a CREATE TABLE statement. In particular, this routine is called
587** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000588** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000589** flag is true if the table should be stored in the auxiliary database
590** file instead of in the main database file. This is normally the case
591** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000592** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000593**
drhf57b3392001-10-08 13:22:32 +0000594** The new table record is initialized and put in pParse->pNewTable.
595** As more of the CREATE TABLE statement is parsed, additional action
596** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000597** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000598** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000599*/
danielk19774adee202004-05-08 08:23:19 +0000600void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000601 Parse *pParse, /* Parser context */
602 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000603 Token *pName1, /* First part of the name of the table or view */
604 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000605 int isTemp, /* True if this is a TEMP table */
606 int isView /* True if this is a VIEW */
607){
drh75897232000-05-29 14:26:00 +0000608 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000609 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000610 char *zName;
drh9bb575f2004-09-06 17:24:11 +0000611 sqlite3 *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000612 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000613 int iDb; /* Database number to create the table in */
614 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000615
danielk1977cbb18d22004-05-28 11:37:27 +0000616 /* The table or view name to create is passed to this routine via tokens
617 ** pName1 and pName2. If the table name was fully qualified, for example:
618 **
619 ** CREATE TABLE xxx.yyy (...);
620 **
621 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
622 ** the table name is not fully qualified, i.e.:
623 **
624 ** CREATE TABLE yyy(...);
625 **
626 ** Then pName1 is set to "yyy" and pName2 is "".
627 **
628 ** The call below sets the pName pointer to point at the token (pName1 or
629 ** pName2) that stores the unqualified table name. The variable iDb is
630 ** set to the index of the database that the table or view is to be
631 ** created in.
632 */
danielk1977ef2cb632004-05-29 02:37:19 +0000633 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000634 if( iDb<0 ) return;
635 if( isTemp && iDb>1 ){
636 /* If creating a temp table, the name may not be qualified */
637 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
638 pParse->nErr++;
639 return;
640 }
641 if( isTemp ) iDb = 1;
642
643 pParse->sNameToken = *pName;
drha99db3b2004-06-19 14:49:12 +0000644 zName = sqlite3NameFromToken(pName);
danielk1977e0048402004-06-15 16:51:01 +0000645 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000646 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
danielk1977e0048402004-06-15 16:51:01 +0000647 sqliteFree(zName);
danielk1977d8123362004-06-12 09:25:12 +0000648 return;
649 }
drh1d85d932004-02-14 23:05:52 +0000650 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000651#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000652 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000653 {
654 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000655 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000656 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000657 sqliteFree(zName);
658 return;
659 }
drhe5f9c642003-01-13 23:27:31 +0000660 if( isView ){
661 if( isTemp ){
662 code = SQLITE_CREATE_TEMP_VIEW;
663 }else{
664 code = SQLITE_CREATE_VIEW;
665 }
666 }else{
667 if( isTemp ){
668 code = SQLITE_CREATE_TEMP_TABLE;
669 }else{
670 code = SQLITE_CREATE_TABLE;
671 }
672 }
danielk19774adee202004-05-08 08:23:19 +0000673 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000674 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000675 return;
676 }
677 }
678#endif
drhf57b3392001-10-08 13:22:32 +0000679
drhf57b3392001-10-08 13:22:32 +0000680 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000681 ** index or table name in the same database. Issue an error message if
682 ** it does.
drhf57b3392001-10-08 13:22:32 +0000683 */
danielk19778a414492004-06-29 08:59:35 +0000684 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19773df6b252004-05-29 10:23:19 +0000685 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
686 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000687 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000688 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000689 return;
drh75897232000-05-29 14:26:00 +0000690 }
danielk19778a414492004-06-29 08:59:35 +0000691 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
692 ( iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000693 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000694 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000695 return;
696 }
697 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000698 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000699 pParse->rc = SQLITE_NOMEM;
700 pParse->nErr++;
drh6d4abfb2001-10-22 02:58:08 +0000701 sqliteFree(zName);
702 return;
703 }
drh75897232000-05-29 14:26:00 +0000704 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000705 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000706 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000707 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000708 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000709 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000710 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000711 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000712
713 /* Begin generating the code that will insert the table record into
714 ** the SQLITE_MASTER table. Note in particular that we must go ahead
715 ** and allocate the record number for the table entry now. Before any
716 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
717 ** indices to be created and the table record must come before the
718 ** indices. Hence, the record number for the table must be allocated
719 ** now.
720 */
danielk19774adee202004-05-08 08:23:19 +0000721 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000722 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977d008cfe2004-06-19 02:22:10 +0000723 /* Every time a new table is created the file-format
724 ** and encoding meta-values are set in the database, in
725 ** case this is the first table created.
726 */
727 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
728 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
729 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
730 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
731
danielk1977cbb18d22004-05-28 11:37:27 +0000732 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
734 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000735 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000736 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000737 }
drh75897232000-05-29 14:26:00 +0000738}
739
740/*
741** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000742**
743** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000744** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000745** first to get things going. Then this routine is called for each
746** column.
drh75897232000-05-29 14:26:00 +0000747*/
danielk19774adee202004-05-08 08:23:19 +0000748void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000749 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000750 int i;
drha99db3b2004-06-19 14:49:12 +0000751 char *z;
drhc9b84a12002-06-20 11:36:48 +0000752 Column *pCol;
drh75897232000-05-29 14:26:00 +0000753 if( (p = pParse->pNewTable)==0 ) return;
drha99db3b2004-06-19 14:49:12 +0000754 z = sqlite3NameFromToken(pName);
drh97fc3d02002-05-22 21:27:03 +0000755 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000756 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000757 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
758 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000759 sqliteFree(z);
760 return;
761 }
762 }
drh75897232000-05-29 14:26:00 +0000763 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000764 Column *aNew;
765 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
766 if( aNew==0 ) return;
767 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000768 }
drhc9b84a12002-06-20 11:36:48 +0000769 pCol = &p->aCol[p->nCol];
770 memset(pCol, 0, sizeof(p->aCol[0]));
771 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000772
773 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000774 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
775 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000776 */
danielk19774f057f92004-06-08 00:02:33 +0000777 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000778 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000779 p->nCol++;
drh75897232000-05-29 14:26:00 +0000780}
781
782/*
drh382c0242001-10-06 16:33:02 +0000783** This routine is called by the parser while in the middle of
784** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
785** been seen on a column. This routine sets the notNull flag on
786** the column currently under construction.
787*/
danielk19774adee202004-05-08 08:23:19 +0000788void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000789 Table *p;
790 int i;
791 if( (p = pParse->pNewTable)==0 ) return;
792 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000793 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000794}
795
796/*
797** This routine is called by the parser while in the middle of
798** parsing a CREATE TABLE statement. The pFirst token is the first
799** token in the sequence of tokens that describe the type of the
800** column currently under construction. pLast is the last token
801** in the sequence. Use this information to construct a string
802** that contains the typename of the column and store that string
803** in zType.
804*/
danielk19774adee202004-05-08 08:23:19 +0000805void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000806 Table *p;
807 int i, j;
808 int n;
809 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000810 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000811 if( (p = pParse->pNewTable)==0 ) return;
812 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000813 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000814 pCol = &p->aCol[i];
815 pz = &pCol->zType;
drh596bd232004-09-30 14:22:47 +0000816 n = pLast->n + (pLast->z - pFirst->z);
drhae29ffb2004-09-25 14:39:18 +0000817 assert( pCol->zType==0 );
818 z = pCol->zType = sqlite3MPrintf("%.*s", n, pFirst->z);
drhf57b3392001-10-08 13:22:32 +0000819 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000820 for(i=j=0; z[i]; i++){
821 int c = z[i];
822 if( isspace(c) ) continue;
823 z[j++] = c;
824 }
825 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000826 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000827}
828
829/*
drh7020f652000-06-03 18:06:52 +0000830** The given token is the default value for the last column added to
831** the table currently under construction. If "minusFlag" is true, it
832** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000833**
834** This routine is called by the parser while in the middle of
835** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000836*/
danielk19774adee202004-05-08 08:23:19 +0000837void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000838 Table *p;
839 int i;
drhae29ffb2004-09-25 14:39:18 +0000840 char *z;
drh7020f652000-06-03 18:06:52 +0000841 if( (p = pParse->pNewTable)==0 ) return;
842 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000843 if( i<0 ) return;
drhae29ffb2004-09-25 14:39:18 +0000844 assert( p->aCol[i].zDflt==0 );
845 z = p->aCol[i].zDflt = sqlite3MPrintf("%s%T", minusFlag ? "-" : "", pVal);
846 sqlite3Dequote(z);
drh7020f652000-06-03 18:06:52 +0000847}
848
849/*
drh4a324312001-12-21 14:30:42 +0000850** Designate the PRIMARY KEY for the table. pList is a list of names
851** of columns that form the primary key. If pList is NULL, then the
852** most recently added column of the table is the primary key.
853**
854** A table can have at most one primary key. If the table already has
855** a primary key (and this is the second primary key) then create an
856** error.
857**
858** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
859** then we will try to use that column as the row id. (Exception:
860** For backwards compatibility with older databases, do not do this
861** if the file format version number is less than 1.) Set the Table.iPKey
862** field of the table under construction to be the index of the
863** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
864** no INTEGER PRIMARY KEY.
865**
866** If the key is not an INTEGER PRIMARY KEY, then create a unique
867** index for the key. No index is created for INTEGER PRIMARY KEYs.
868*/
drh205f48e2004-11-05 00:43:11 +0000869void sqlite3AddPrimaryKey(
870 Parse *pParse, /* Parsing context */
871 ExprList *pList, /* List of field names to be indexed */
872 int onError, /* What to do with a uniqueness conflict */
873 int autoInc /* True if the AUTOINCREMENT keyword is present */
874){
drh4a324312001-12-21 14:30:42 +0000875 Table *pTab = pParse->pNewTable;
876 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000877 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000878 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000879 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000880 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000881 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000882 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000883 }
884 pTab->hasPrimKey = 1;
885 if( pList==0 ){
886 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000887 pTab->aCol[iCol].isPrimKey = 1;
888 }else{
danielk19770202b292004-06-09 09:55:16 +0000889 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000890 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000891 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
892 break;
893 }
drh78100cc2003-08-23 22:40:53 +0000894 }
895 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000896 }
danielk19770202b292004-06-09 09:55:16 +0000897 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000898 }
899 if( iCol>=0 && iCol<pTab->nCol ){
900 zType = pTab->aCol[iCol].zType;
901 }
danielk19773d68f032004-05-11 07:11:51 +0000902 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000903 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000904 pTab->keyConf = onError;
drh205f48e2004-11-05 00:43:11 +0000905 pTab->autoInc = autoInc;
906 }else if( autoInc ){
907 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
908 "INTEGER PRIMARY KEY");
drh4a324312001-12-21 14:30:42 +0000909 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000910 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000911 pList = 0;
drh4a324312001-12-21 14:30:42 +0000912 }
drhe0194f22003-02-26 13:52:51 +0000913
914primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000915 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000916 return;
drh4a324312001-12-21 14:30:42 +0000917}
918
919/*
drhd3d39e92004-05-20 22:16:29 +0000920** Set the collation function of the most recently parsed table column
921** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000922*/
drhd3d39e92004-05-20 22:16:29 +0000923void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000924 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000925 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000926 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000927 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000928
drhd3d39e92004-05-20 22:16:29 +0000929 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000930 i = p->nCol-1;
931
932 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
933 p->aCol[i].pColl = pColl;
934
935 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
936 ** then an index may have been created on this column before the
937 ** collation type was added. Correct this if it is the case.
938 */
939 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
940 assert( pIdx->nColumn==1 );
941 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000942 }
943}
944
945/*
danielk19770202b292004-06-09 09:55:16 +0000946** Locate and return an entry from the db.aCollSeq hash table. If the entry
947** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +0000948** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +0000949**
danielk1977466be562004-06-10 02:16:01 +0000950** Each pointer stored in the sqlite3.aCollSeq hash table contains an
951** array of three CollSeq structures. The first is the collation sequence
952** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +0000953**
danielk1977466be562004-06-10 02:16:01 +0000954** Stored immediately after the three collation sequences is a copy of
955** the collation sequence name. A pointer to this string is stored in
956** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +0000957*/
danielk1977466be562004-06-10 02:16:01 +0000958static CollSeq * findCollSeqEntry(
drh9bb575f2004-09-06 17:24:11 +0000959 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +0000960 const char *zName,
danielk19770202b292004-06-09 09:55:16 +0000961 int nName,
962 int create
drhd3d39e92004-05-20 22:16:29 +0000963){
964 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000965 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000966 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +0000967
danielk19770202b292004-06-09 09:55:16 +0000968 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +0000969 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +0000970 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000971 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000972 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +0000973 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000974 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +0000975 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000976 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +0000977 memcpy(pColl[0].zName, zName, nName);
978 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +0000979 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +0000980 }
drhd3d39e92004-05-20 22:16:29 +0000981 }
drhd3d39e92004-05-20 22:16:29 +0000982 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000983}
984
danielk1977466be562004-06-10 02:16:01 +0000985/*
986** Parameter zName points to a UTF-8 encoded string nName bytes long.
987** Return the CollSeq* pointer for the collation sequence named zName
988** for the encoding 'enc' from the database 'db'.
989**
990** If the entry specified is not found and 'create' is true, then create a
991** new entry. Otherwise return NULL.
992*/
993CollSeq *sqlite3FindCollSeq(
drh9bb575f2004-09-06 17:24:11 +0000994 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +0000995 u8 enc,
996 const char *zName,
997 int nName,
998 int create
999){
1000 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
drh6d08b4d2004-07-20 12:45:22 +00001001 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
1002 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
1003 if( pColl ) pColl += enc-1;
danielk1977466be562004-06-10 02:16:01 +00001004 return pColl;
1005}
1006
danielk1977e159fdf2004-06-21 10:45:06 +00001007/*
1008** Invoke the 'collation needed' callback to request a collation sequence
1009** in the database text encoding of name zName, length nName.
1010** If the collation sequence
1011*/
drh9bb575f2004-09-06 17:24:11 +00001012static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
danielk19777cedc8d2004-06-10 10:50:08 +00001013 assert( !db->xCollNeeded || !db->xCollNeeded16 );
1014 if( nName<0 ) nName = strlen(zName);
1015 if( db->xCollNeeded ){
danielk19778a6c5502004-06-22 12:18:32 +00001016 char *zExternal = sqliteStrNDup(zName, nName);
danielk19777cedc8d2004-06-10 10:50:08 +00001017 if( !zExternal ) return;
danielk1977e159fdf2004-06-21 10:45:06 +00001018 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
1019 sqliteFree(zExternal);
danielk19777cedc8d2004-06-10 10:50:08 +00001020 }
1021 if( db->xCollNeeded16 ){
danielk19778a6c5502004-06-22 12:18:32 +00001022 char const *zExternal;
danielk1977bfd6cce2004-06-18 04:24:54 +00001023 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
1024 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
1025 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +00001026 if( !zExternal ) return;
1027 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
1028 }
danielk19777cedc8d2004-06-10 10:50:08 +00001029}
1030
danielk1977e159fdf2004-06-21 10:45:06 +00001031/*
1032** This routine is called if the collation factory fails to deliver a
1033** collation function in the best encoding but there may be other versions
1034** of this collation function (for other text encodings) available. Use one
1035** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
1036** possible.
1037*/
danielk19777cedc8d2004-06-10 10:50:08 +00001038static int synthCollSeq(Parse *pParse, CollSeq *pColl){
drhda71ce12004-06-21 18:14:45 +00001039 CollSeq *pColl2;
danielk19777cedc8d2004-06-10 10:50:08 +00001040 char *z = pColl->zName;
1041 int n = strlen(z);
drh9bb575f2004-09-06 17:24:11 +00001042 sqlite3 *db = pParse->db;
drhda71ce12004-06-21 18:14:45 +00001043 int i;
1044 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
1045 for(i=0; i<3; i++){
1046 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
1047 if( pColl2->xCmp!=0 ){
1048 memcpy(pColl, pColl2, sizeof(CollSeq));
1049 return SQLITE_OK;
danielk19777cedc8d2004-06-10 10:50:08 +00001050 }
danielk19777cedc8d2004-06-10 10:50:08 +00001051 }
drhda71ce12004-06-21 18:14:45 +00001052 if( pParse->nErr==0 ){
drhae29ffb2004-09-25 14:39:18 +00001053 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", n, z);
drhda71ce12004-06-21 18:14:45 +00001054 }
1055 pParse->nErr++;
1056 return SQLITE_ERROR;
danielk19777cedc8d2004-06-10 10:50:08 +00001057}
1058
1059/*
1060** This routine is called on a collation sequence before it is used to
1061** check that it is defined. An undefined collation sequence exists when
1062** a database is loaded that contains references to collation sequences
1063** that have not been defined by sqlite3_create_collation() etc.
1064**
1065** If required, this routine calls the 'collation needed' callback to
1066** request a definition of the collating sequence. If this doesn't work,
1067** an equivalent collating sequence that uses a text encoding different
1068** from the main database is substituted, if one is available.
1069*/
1070int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1071 if( pColl && !pColl->xCmp ){
danielk1977e159fdf2004-06-21 10:45:06 +00001072 /* No collation sequence of this type for this encoding is registered.
1073 ** Call the collation factory to see if it can supply us with one.
1074 */
danielk19777cedc8d2004-06-10 10:50:08 +00001075 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1076 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1077 return SQLITE_ERROR;
1078 }
1079 }
1080 return SQLITE_OK;
1081}
1082
drhda71ce12004-06-21 18:14:45 +00001083/*
1084** Call sqlite3CheckCollSeq() for all collating sequences in an index,
1085** in order to verify that all the necessary collating sequences are
1086** loaded.
1087*/
danielk19777cedc8d2004-06-10 10:50:08 +00001088int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1089 if( pIdx ){
1090 int i;
1091 for(i=0; i<pIdx->nColumn; i++){
1092 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1093 return SQLITE_ERROR;
1094 }
1095 }
1096 }
1097 return SQLITE_OK;
1098}
1099
danielk1977466be562004-06-10 02:16:01 +00001100/*
1101** This function returns the collation sequence for database native text
1102** encoding identified by the string zName, length nName.
1103**
1104** If the requested collation sequence is not available, or not available
1105** in the database native encoding, the collation factory is invoked to
1106** request it. If the collation factory does not supply such a sequence,
1107** and the sequence is available in another text encoding, then that is
1108** returned instead.
1109**
1110** If no versions of the requested collations sequence are available, or
1111** another error occurs, NULL is returned and an error message written into
1112** pParse.
1113*/
danielk19770202b292004-06-09 09:55:16 +00001114CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001115 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001116 u8 initbusy = pParse->db->init.busy;
1117 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
drhae29ffb2004-09-25 14:39:18 +00001118 if( nName<0 ) nName = strlen(zName);
danielk19777cedc8d2004-06-10 10:50:08 +00001119 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001120 /* No collation sequence of this type for this encoding is registered.
1121 ** Call the collation factory to see if it can supply us with one.
1122 */
danielk19777cedc8d2004-06-10 10:50:08 +00001123 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001124 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001125 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001126 /* There may be a version of the collation sequence that requires
1127 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001128 */
danielk19777cedc8d2004-06-10 10:50:08 +00001129 if( synthCollSeq(pParse, pColl) ){
1130 return 0;
danielk1977466be562004-06-10 02:16:01 +00001131 }
1132 }
1133 }
1134
1135 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001136 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001137 if( pParse->nErr==0 ){
drhae29ffb2004-09-25 14:39:18 +00001138 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName);
danielk19770202b292004-06-09 09:55:16 +00001139 }
danielk19777cedc8d2004-06-10 10:50:08 +00001140 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001141 }
1142 return pColl;
1143}
1144
1145
1146
danielk1977a37cdde2004-05-16 11:15:36 +00001147/*
drh1ad3b9e2004-05-20 12:10:20 +00001148** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +00001149** associated affinity type.
1150*/
1151char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +00001152 int n, i;
drh57196282004-10-06 15:41:16 +00001153 static const struct {
drh1ad3b9e2004-05-20 12:10:20 +00001154 const char *zSub; /* Keywords substring to search for */
drhda71ce12004-06-21 18:14:45 +00001155 char nSub; /* length of zSub */
drh1ad3b9e2004-05-20 12:10:20 +00001156 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +00001157 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +00001158 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +00001159 {"CHAR", 4, SQLITE_AFF_TEXT},
1160 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +00001161 {"TEXT", 4, SQLITE_AFF_TEXT},
1162 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +00001163 };
1164
drh9c054832004-05-31 18:51:57 +00001165 if( nType==0 ){
1166 return SQLITE_AFF_NONE;
1167 }
drh1ad3b9e2004-05-20 12:10:20 +00001168 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
1169 int c1 = substrings[i].zSub[0];
1170 int c2 = tolower(c1);
1171 int limit = nType - substrings[i].nSub;
1172 const char *z = substrings[i].zSub;
1173 for(n=0; n<=limit; n++){
1174 int c = zType[n];
1175 if( (c==c1 || c==c2)
1176 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +00001177 return substrings[i].affinity;
1178 }
1179 }
1180 }
drh1ad3b9e2004-05-20 12:10:20 +00001181 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +00001182}
1183
1184/*
drh3f7d4e42004-07-24 14:35:58 +00001185** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001186**
1187** The schema cookie is used to determine when the schema for the
1188** database changes. After each schema change, the cookie value
1189** changes. When a process first reads the schema it records the
1190** cookie. Thereafter, whenever it goes to access the database,
1191** it checks the cookie to make sure the schema has not changed
1192** since it was last read.
1193**
1194** This plan is not completely bullet-proof. It is possible for
1195** the schema to change multiple times and for the cookie to be
1196** set back to prior value. But schema changes are infrequent
1197** and the probability of hitting the same cookie value is only
1198** 1 chance in 2^32. So we're safe enough.
1199*/
drh9bb575f2004-09-06 17:24:11 +00001200void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){
drh3f7d4e42004-07-24 14:35:58 +00001201 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].schema_cookie+1, 0);
danielk19771d850a72004-05-31 08:26:49 +00001202 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001203}
1204
1205/*
drh969fa7c2002-02-18 18:30:32 +00001206** Measure the number of characters needed to output the given
1207** identifier. The number returned includes any quotes used
1208** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001209**
1210** The estimate is conservative. It might be larger that what is
1211** really needed.
drh969fa7c2002-02-18 18:30:32 +00001212*/
1213static int identLength(const char *z){
1214 int n;
drh17f71932002-02-21 12:01:27 +00001215 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001216 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001217 }
drh234c39d2004-07-24 03:30:47 +00001218 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001219}
1220
1221/*
1222** Write an identifier onto the end of the given string. Add
1223** quote characters as needed.
1224*/
drh4c755c02004-08-08 20:22:17 +00001225static void identPut(char *z, int *pIdx, char *zSignedIdent){
1226 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001227 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001228 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001229 for(j=0; zIdent[j]; j++){
1230 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1231 }
1232 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001233 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh234c39d2004-07-24 03:30:47 +00001234 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001235 for(j=0; zIdent[j]; j++){
1236 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001237 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001238 }
drh234c39d2004-07-24 03:30:47 +00001239 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001240 z[i] = 0;
1241 *pIdx = i;
1242}
1243
1244/*
1245** Generate a CREATE TABLE statement appropriate for the given
1246** table. Memory to hold the text of the statement is obtained
1247** from sqliteMalloc() and must be freed by the calling function.
1248*/
1249static char *createTableStmt(Table *p){
1250 int i, k, n;
1251 char *zStmt;
drh234c39d2004-07-24 03:30:47 +00001252 char *zSep, *zSep2, *zEnd, *z;
1253 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001254 n = 0;
drh234c39d2004-07-24 03:30:47 +00001255 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1256 n += identLength(pCol->zName);
1257 z = pCol->zType;
1258 if( z ){
1259 n += (strlen(z) + 1);
danielk1977517eb642004-06-07 10:00:31 +00001260 }
drh969fa7c2002-02-18 18:30:32 +00001261 }
1262 n += identLength(p->zName);
drh234c39d2004-07-24 03:30:47 +00001263 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001264 zSep = "";
1265 zSep2 = ",";
1266 zEnd = ")";
1267 }else{
1268 zSep = "\n ";
1269 zSep2 = ",\n ";
1270 zEnd = "\n)";
1271 }
drhe0bc4042002-06-25 01:09:11 +00001272 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001273 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001274 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001275 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001276 k = strlen(zStmt);
1277 identPut(zStmt, &k, p->zName);
1278 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001279 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drh969fa7c2002-02-18 18:30:32 +00001280 strcpy(&zStmt[k], zSep);
1281 k += strlen(&zStmt[k]);
1282 zSep = zSep2;
drh234c39d2004-07-24 03:30:47 +00001283 identPut(zStmt, &k, pCol->zName);
1284 if( (z = pCol->zType)!=0 ){
danielk1977517eb642004-06-07 10:00:31 +00001285 zStmt[k++] = ' ';
drh234c39d2004-07-24 03:30:47 +00001286 strcpy(&zStmt[k], z);
1287 k += strlen(z);
danielk1977517eb642004-06-07 10:00:31 +00001288 }
drh969fa7c2002-02-18 18:30:32 +00001289 }
1290 strcpy(&zStmt[k], zEnd);
1291 return zStmt;
1292}
1293
1294/*
drh75897232000-05-29 14:26:00 +00001295** This routine is called to report the final ")" that terminates
1296** a CREATE TABLE statement.
1297**
drhf57b3392001-10-08 13:22:32 +00001298** The table structure that other action routines have been building
1299** is added to the internal hash tables, assuming no errors have
1300** occurred.
drh75897232000-05-29 14:26:00 +00001301**
drh1d85d932004-02-14 23:05:52 +00001302** An entry for the table is made in the master table on disk, unless
1303** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001304** it means we are reading the sqlite_master table because we just
1305** connected to the database or because the sqlite_master table has
1306** recently changes, so the entry for this table already exists in
1307** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001308**
1309** If the pSelect argument is not NULL, it means that this routine
1310** was called to create a table generated from a
1311** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1312** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001313*/
danielk19774adee202004-05-08 08:23:19 +00001314void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001315 Table *p;
drh9bb575f2004-09-06 17:24:11 +00001316 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001317
danielk197724b03fd2004-05-10 10:34:34 +00001318 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001319 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001320 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001321
danielk1977517eb642004-06-07 10:00:31 +00001322 assert( !db->init.busy || !pSelect );
1323
drh1d85d932004-02-14 23:05:52 +00001324 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001325 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1326 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001327 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001328 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001329 */
drh1d85d932004-02-14 23:05:52 +00001330 if( db->init.busy ){
1331 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001332 }
1333
drhe3c41372001-09-17 20:25:58 +00001334 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001335 ** in the SQLITE_MASTER table of the database. The record number
1336 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001337 **
drhe0bc4042002-06-25 01:09:11 +00001338 ** If this is a TEMPORARY table, write the entry into the auxiliary
1339 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001340 */
drh1d85d932004-02-14 23:05:52 +00001341 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001342 int n;
drhd8bc7082000-06-07 23:51:50 +00001343 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001344
danielk19774adee202004-05-08 08:23:19 +00001345 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001346 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001347
drh4ff6dfa2002-03-03 23:06:00 +00001348 if( p->pSelect==0 ){
1349 /* A regular table */
drh234c39d2004-07-24 03:30:47 +00001350 sqlite3VdbeAddOp(v, OP_CreateTable, p->iDb, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001351 }else{
1352 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001353 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001354 }
danielk1977517eb642004-06-07 10:00:31 +00001355
1356 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1357
1358 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1359 ** statement to populate the new table. The root-page number for the
1360 ** new table is on the top of the vdbe stack.
1361 **
1362 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1363 ** suitable state to query for the column names and types to be used
1364 ** by the new table.
1365 */
1366 if( pSelect ){
1367 Table *pSelTab;
1368 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1369 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1370 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1371 pParse->nTab = 2;
1372 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1373 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1374 if( pParse->nErr==0 ){
1375 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1376 if( pSelTab==0 ) return;
1377 assert( p->aCol==0 );
1378 p->nCol = pSelTab->nCol;
1379 p->aCol = pSelTab->aCol;
1380 pSelTab->nCol = 0;
1381 pSelTab->aCol = 0;
1382 sqlite3DeleteTable(0, pSelTab);
1383 }
1384 }
1385
1386 sqlite3OpenMasterTable(v, p->iDb);
1387
1388 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view",P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001389 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1390 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk1977517eb642004-06-07 10:00:31 +00001391 sqlite3VdbeAddOp(v, OP_Pull, 3, 0);
1392
drhe0bc4042002-06-25 01:09:11 +00001393 if( pSelect ){
1394 char *z = createTableStmt(p);
1395 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001396 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001398 sqliteFree(z);
1399 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001400 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001401 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001402 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001403 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001404 }
drhe0bc4042002-06-25 01:09:11 +00001405 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001406 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001407 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001408 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
drh855eb1c2004-08-31 13:45:11 +00001409 sqlite3VdbeAddOp(v, OP_Concat, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00001410 }
danielk197784ac9d02004-05-18 09:58:06 +00001411 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001412 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drhc275b4e2004-07-19 17:25:24 +00001413 sqlite3ChangeCookie(db, v, p->iDb);
danielk19774adee202004-05-08 08:23:19 +00001414 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh234c39d2004-07-24 03:30:47 +00001415 sqlite3VdbeOp3(v, OP_ParseSchema, p->iDb, 0,
1416 sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC);
drh75897232000-05-29 14:26:00 +00001417 }
drh17e9e292003-02-01 13:53:28 +00001418
1419 /* Add the table to the in-memory representation of the database.
1420 */
drh234c39d2004-07-24 03:30:47 +00001421 if( db->init.busy && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001422 Table *pOld;
drhbe5c89a2004-07-26 00:31:09 +00001423 FKey *pFKey;
1424 Db *pDb = &db->aDb[p->iDb];
1425 pOld = sqlite3HashInsert(&pDb->tblHash, p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001426 if( pOld ){
1427 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1428 return;
1429 }
1430 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1431 int nTo = strlen(pFKey->zTo) + 1;
drhbe5c89a2004-07-26 00:31:09 +00001432 pFKey->pNextTo = sqlite3HashFind(&pDb->aFKey, pFKey->zTo, nTo);
1433 sqlite3HashInsert(&pDb->aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001434 }
1435 pParse->pNewTable = 0;
1436 db->nTable++;
1437 db->flags |= SQLITE_InternChanges;
1438 }
drh75897232000-05-29 14:26:00 +00001439}
1440
drhb7f91642004-10-31 02:22:47 +00001441#ifndef SQLITE_OMIT_VIEW
drh75897232000-05-29 14:26:00 +00001442/*
drha76b5df2002-02-23 02:32:10 +00001443** The parser calls this routine in order to create a new VIEW
1444*/
danielk19774adee202004-05-08 08:23:19 +00001445void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001446 Parse *pParse, /* The parsing context */
1447 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001448 Token *pName1, /* The token that holds the name of the view */
1449 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001450 Select *pSelect, /* A SELECT statement that will become the new view */
1451 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001452){
drha76b5df2002-02-23 02:32:10 +00001453 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001454 int n;
drh4c755c02004-08-08 20:22:17 +00001455 const unsigned char *z;
drh4b59ab52002-08-24 18:24:51 +00001456 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001457 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001458 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001459
danielk197748dec7e2004-05-28 12:33:30 +00001460 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001461 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001462 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001463 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001464 return;
1465 }
danielk1977ef2cb632004-05-29 02:37:19 +00001466 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001467 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1468 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001469 ){
danielk19774adee202004-05-08 08:23:19 +00001470 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001471 return;
1472 }
drh174b6192002-12-03 02:22:52 +00001473
drh4b59ab52002-08-24 18:24:51 +00001474 /* Make a copy of the entire SELECT statement that defines the view.
1475 ** This will force all the Expr.token.z values to be dynamically
1476 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001477 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001478 */
danielk19774adee202004-05-08 08:23:19 +00001479 p->pSelect = sqlite3SelectDup(pSelect);
1480 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001481 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001482 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001483 }
drh4b59ab52002-08-24 18:24:51 +00001484
1485 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1486 ** the end.
1487 */
drha76b5df2002-02-23 02:32:10 +00001488 sEnd = pParse->sLastToken;
1489 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1490 sEnd.z += sEnd.n;
1491 }
1492 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001493 n = sEnd.z - pBegin->z;
drh4c755c02004-08-08 20:22:17 +00001494 z = (const unsigned char*)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001495 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1496 sEnd.z = &z[n-1];
1497 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001498
danielk19774adee202004-05-08 08:23:19 +00001499 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1500 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001501 return;
drh417be792002-03-03 18:59:40 +00001502}
drhb7f91642004-10-31 02:22:47 +00001503#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001504
drhb7f91642004-10-31 02:22:47 +00001505#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001506/*
1507** The Table structure pTable is really a VIEW. Fill in the names of
1508** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001509** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001510*/
danielk19774adee202004-05-08 08:23:19 +00001511int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001512 ExprList *pEList;
1513 Select *pSel;
1514 Table *pSelTab;
1515 int nErr = 0;
1516
1517 assert( pTable );
1518
1519 /* A positive nCol means the columns names for this view are
1520 ** already known.
1521 */
1522 if( pTable->nCol>0 ) return 0;
1523
1524 /* A negative nCol is a special marker meaning that we are currently
1525 ** trying to compute the column names. If we enter this routine with
1526 ** a negative nCol, it means two or more views form a loop, like this:
1527 **
1528 ** CREATE VIEW one AS SELECT * FROM two;
1529 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001530 **
1531 ** Actually, this error is caught previously and so the following test
1532 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001533 */
1534 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001535 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001536 return 1;
1537 }
1538
1539 /* If we get this far, it means we need to compute the table names.
1540 */
1541 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1542 pSel = pTable->pSelect;
1543
danielk19774adee202004-05-08 08:23:19 +00001544 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001545 ** "*" elements in this list. But we will need to restore the list
1546 ** back to its original configuration afterwards, so we save a copy of
1547 ** the original in pEList.
1548 */
1549 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001550 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001551 if( pSel->pEList==0 ){
1552 pSel->pEList = pEList;
1553 return 1; /* Malloc failed */
1554 }
1555 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001556 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001557 if( pSelTab ){
1558 assert( pTable->aCol==0 );
1559 pTable->nCol = pSelTab->nCol;
1560 pTable->aCol = pSelTab->aCol;
1561 pSelTab->nCol = 0;
1562 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001563 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001564 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001565 }else{
1566 pTable->nCol = 0;
1567 nErr++;
1568 }
danielk19774adee202004-05-08 08:23:19 +00001569 sqlite3SelectUnbind(pSel);
1570 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001571 pSel->pEList = pEList;
1572 return nErr;
1573}
drhb7f91642004-10-31 02:22:47 +00001574#endif /* SQLITE_OMIT_VIEW */
drh417be792002-03-03 18:59:40 +00001575
drhb7f91642004-10-31 02:22:47 +00001576#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001577/*
drh8bf8dc92003-05-17 17:35:10 +00001578** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001579*/
drh9bb575f2004-09-06 17:24:11 +00001580static void sqliteViewResetAll(sqlite3 *db, int idx){
drh417be792002-03-03 18:59:40 +00001581 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001582 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001583 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001584 Table *pTab = sqliteHashData(i);
1585 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001586 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001587 }
1588 }
drh8bf8dc92003-05-17 17:35:10 +00001589 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001590}
drhb7f91642004-10-31 02:22:47 +00001591#else
1592# define sqliteViewResetAll(A,B)
1593#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001594
drh75897232000-05-29 14:26:00 +00001595/*
danielk1977a0bf2652004-11-04 14:30:04 +00001596** This function is called by the VDBE to adjust the internal schema
1597** used by SQLite when the btree layer moves a table root page. The
1598** root-page of a table or index in database iDb has changed from iFrom
1599** to iTo.
1600*/
1601#ifndef SQLITE_OMIT_AUTOVACUUM
1602void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
1603 HashElem *pElem;
1604
1605 for(pElem=sqliteHashFirst(&pDb->tblHash); pElem; pElem=sqliteHashNext(pElem)){
1606 Table *pTab = sqliteHashData(pElem);
1607 if( pTab->tnum==iFrom ){
1608 pTab->tnum = iTo;
1609 return;
1610 }
1611 }
1612 for(pElem=sqliteHashFirst(&pDb->idxHash); pElem; pElem=sqliteHashNext(pElem)){
1613 Index *pIdx = sqliteHashData(pElem);
1614 if( pIdx->tnum==iFrom ){
1615 pIdx->tnum = iTo;
1616 return;
1617 }
1618 }
1619 assert(0);
1620}
1621#endif
1622
1623/*
1624** Write code to erase the table with root-page iTable from database iDb.
1625** Also write code to modify the sqlite_master table and internal schema
1626** if a root-page of another table is moved by the btree-layer whilst
1627** erasing iTable (this can happen with an auto-vacuum database).
1628*/
1629static void destroyRootPage(Vdbe *v, int iTable, int iDb){
1630#ifndef SQLITE_OMIT_AUTOVACUUM
1631 int base;
danielk1977a0bf2652004-11-04 14:30:04 +00001632 /* If SQLITE_OMIT_AUTOVACUUM is not defined, then OP_Destroy pushes
1633 ** an integer onto the stack. If this integer is non-zero, then it is
1634 ** the root page number of a table moved to location iTable. The
1635 ** following writes VDBE code to modify the sqlite_master table to
1636 ** reflect this. It is assumed that cursor number 0 is a write-cursor
1637 ** opened on the sqlite_master table.
1638 */
1639 static const VdbeOpList updateMaster[] = {
1640 /* If the Op_Destroy pushed a 0 onto the stack, then skip the following
1641 ** code. sqlite_master does not need updating in this case.
1642 */
1643 { OP_Dup, 0, 0, 0},
1644 { OP_Integer, 0, 0, 0},
1645 { OP_Eq, 0, ADDR(17), 0},
1646
1647 /* Search for the sqlite_master row containing root-page iTable. */
1648 { OP_Rewind, 0, ADDR(8), 0},
1649 { OP_Dup, 0, 0, 0}, /* 4 */
1650 { OP_Column, 0, 3, 0}, /* 5 */
1651 { OP_Eq, 0, ADDR(9), 0},
1652 { OP_Next, 0, ADDR(4), 0},
1653 { OP_Halt, SQLITE_CORRUPT, OE_Fail, 0}, /* 8 */
1654 { OP_Recno, 0, 0, 0}, /* 9 */
1655
1656 /* Cursor 0 now points at the row that will be updated. The top of
1657 ** the stack is the rowid of that row. The next value on the stack is
1658 ** the new value for the root-page field.
1659 */
1660 { OP_Column, 0, 0, 0}, /* 10 */
1661 { OP_Column, 0, 1, 0},
1662 { OP_Column, 0, 2, 0},
1663 { OP_Integer, 4, 0, 0}, /* 13 */
1664 { OP_Column, 0, 4, 0},
1665 { OP_MakeRecord, 5, 0, 0},
1666 { OP_PutIntKey, 0, 0, 0} /* 16 */
1667 };
drh40e016e2004-11-04 14:47:11 +00001668#endif
danielk1977a0bf2652004-11-04 14:30:04 +00001669
drh40e016e2004-11-04 14:47:11 +00001670 sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb);
1671#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a0bf2652004-11-04 14:30:04 +00001672 base = sqlite3VdbeAddOpList(v, ArraySize(updateMaster), updateMaster);
1673 sqlite3VdbeChangeP1(v, base+13, iTable);
1674#endif
1675}
1676
1677/*
1678** Write VDBE code to erase table pTab and all associated indices on disk.
1679** Code to update the sqlite_master tables and internal schema definitions
1680** in case a root-page belonging to another table is moved by the btree layer
1681** is also added (this can happen with an auto-vacuum database).
1682*/
1683static void destroyTable(Vdbe *v, Table *pTab){
1684#ifdef SQLITE_OMIT_AUTOVACUUM
1685 destroyRootPage(v, pTab->tnum, pTab->iDb);
1686 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1687 destroyRootPage(v, pIdx->tnum, pIdx->iDb);
1688 }
1689#else
1690 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1691 ** is not defined), then it is important to call OP_Destroy on the
1692 ** table and index root-pages in order, starting with the numerically
1693 ** largest root-page number. This guarantees that none of the root-pages
1694 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1695 ** following were coded:
1696 **
1697 ** OP_Destroy 4 0
1698 ** ...
1699 ** OP_Destroy 5 0
1700 **
1701 ** and root page 5 happened to be the largest root-page number in the
1702 ** database, then root page 5 would be moved to page 4 by the
1703 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1704 ** a free-list page.
1705 */
1706 int iTab = pTab->tnum;
1707 int iDestroyed = 0;
1708
1709 while( 1 ){
1710 Index *pIdx;
1711 int iLargest = 0;
1712
1713 if( iDestroyed==0 || iTab<iDestroyed ){
1714 iLargest = iTab;
1715 }
1716 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1717 int iIdx = pIdx->tnum;
1718 assert( pIdx->iDb==pTab->iDb );
1719 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
1720 iLargest = iIdx;
1721 }
1722 }
1723 if( iLargest==0 ) return;
1724 destroyRootPage(v, iLargest, pTab->iDb);
1725 iDestroyed = iLargest;
1726 }
1727#endif
1728}
1729
1730/*
drh75897232000-05-29 14:26:00 +00001731** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001732** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001733*/
danielk1977a8858102004-05-28 12:11:21 +00001734void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1735 Table *pTab;
drh75897232000-05-29 14:26:00 +00001736 Vdbe *v;
1737 int base;
drh9bb575f2004-09-06 17:24:11 +00001738 sqlite3 *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001739 int iDb;
drh75897232000-05-29 14:26:00 +00001740
danielk1977a8858102004-05-28 12:11:21 +00001741 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1742 assert( pName->nSrc==1 );
1743 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1744
1745 if( pTab==0 ) goto exit_drop_table;
1746 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001747 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001748#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001749 {
1750 int code;
danielk1977a8858102004-05-28 12:11:21 +00001751 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1752 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001753 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001754 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001755 }
drhe5f9c642003-01-13 23:27:31 +00001756 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001757 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001758 code = SQLITE_DROP_TEMP_VIEW;
1759 }else{
1760 code = SQLITE_DROP_VIEW;
1761 }
1762 }else{
drhd24cc422003-03-27 12:51:24 +00001763 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001764 code = SQLITE_DROP_TEMP_TABLE;
1765 }else{
1766 code = SQLITE_DROP_TABLE;
1767 }
1768 }
danielk1977a8858102004-05-28 12:11:21 +00001769 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1770 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001771 }
danielk1977a8858102004-05-28 12:11:21 +00001772 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1773 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001774 }
drhe5f9c642003-01-13 23:27:31 +00001775 }
1776#endif
danielk1977a8858102004-05-28 12:11:21 +00001777 if( pTab->readOnly ){
1778 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001779 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001780 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001781 }
danielk1977a8858102004-05-28 12:11:21 +00001782 if( isView && pTab->pSelect==0 ){
1783 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1784 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001785 }
danielk1977a8858102004-05-28 12:11:21 +00001786 if( !isView && pTab->pSelect ){
1787 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1788 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001789 }
drh75897232000-05-29 14:26:00 +00001790
drh1ccde152000-06-17 13:12:39 +00001791 /* Generate code to remove the table from the master table
1792 ** on disk.
1793 */
danielk19774adee202004-05-08 08:23:19 +00001794 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001795 if( v ){
drh57196282004-10-06 15:41:16 +00001796 static const VdbeOpList dropTable[] = {
danielk19778e227872004-06-07 07:52:17 +00001797 { OP_Rewind, 0, ADDR(13), 0},
1798 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001799 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001800 { OP_MemLoad, 1, 0, 0}, /* 3 */
danielk19778e227872004-06-07 07:52:17 +00001801 { OP_Column, 0, 2, 0}, /* sqlite_master.tbl_name */
1802 { OP_Ne, 0, ADDR(12), 0},
1803 { OP_String8, 0, 0, "trigger"},
1804 { OP_Column, 0, 2, 0}, /* sqlite_master.type */
1805 { OP_Eq, 0, ADDR(12), 0},
drh75897232000-05-29 14:26:00 +00001806 { OP_Delete, 0, 0, 0},
danielk19778e227872004-06-07 07:52:17 +00001807 { OP_Rewind, 0, ADDR(13), 0},
danielk19778d059842004-05-12 11:24:02 +00001808 { OP_Goto, 0, ADDR(3), 0},
danielk19778e227872004-06-07 07:52:17 +00001809 { OP_Next, 0, ADDR(3), 0}, /* 12 */
drh75897232000-05-29 14:26:00 +00001810 };
danielk1977a0bf2652004-11-04 14:30:04 +00001811 /* Index *pIdx; */
drhe0bc4042002-06-25 01:09:11 +00001812 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001813 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001814
danielk19778e227872004-06-07 07:52:17 +00001815 /* Drop all triggers associated with the table being dropped. Code
1816 ** is generated to remove entries from sqlite_master and/or
1817 ** sqlite_temp_master if required.
1818 */
danielk1977a8858102004-05-28 12:11:21 +00001819 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001820 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001821 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001822 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drh956bc922004-07-24 17:38:29 +00001823 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00001824 }
drh8bf8dc92003-05-17 17:35:10 +00001825
danielk19778e227872004-06-07 07:52:17 +00001826 /* Drop all SQLITE_MASTER table and index entries that refer to the
1827 ** table. The program name loops through the master table and deletes
1828 ** every row that refers to a table of the same name as the one being
1829 ** dropped. Triggers are handled seperately because a trigger can be
1830 ** created in the temp database that refers to a table in another
1831 ** database.
1832 */
danielk1977a8858102004-05-28 12:11:21 +00001833 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001834 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001835 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
danielk19778e227872004-06-07 07:52:17 +00001836 sqlite3ChangeCookie(db, v, pTab->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001837 if( !isView ){
danielk1977a0bf2652004-11-04 14:30:04 +00001838/*
danielk1977a8858102004-05-28 12:11:21 +00001839 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1840 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001841 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001842 }
danielk1977a0bf2652004-11-04 14:30:04 +00001843*/
1844 destroyTable(v, pTab);
drh5e00f6c2001-09-13 13:46:56 +00001845 }
danielk1977a0bf2652004-11-04 14:30:04 +00001846 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh956bc922004-07-24 17:38:29 +00001847 sqlite3VdbeOp3(v, OP_DropTable, pTab->iDb, 0, pTab->zName, 0);
drh75897232000-05-29 14:26:00 +00001848 }
drhd24cc422003-03-27 12:51:24 +00001849 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001850
1851exit_drop_table:
1852 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001853}
1854
1855/*
drhc2eef3b2002-08-31 18:53:06 +00001856** This routine is called to create a new foreign key on the table
1857** currently under construction. pFromCol determines which columns
1858** in the current table point to the foreign key. If pFromCol==0 then
1859** connect the key to the last column inserted. pTo is the name of
1860** the table referred to. pToCol is a list of tables in the other
1861** pTo table that the foreign key points to. flags contains all
1862** information about the conflict resolution algorithms specified
1863** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1864**
1865** An FKey structure is created and added to the table currently
1866** under construction in the pParse->pNewTable field. The new FKey
1867** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001868** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001869**
1870** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001871** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001872*/
danielk19774adee202004-05-08 08:23:19 +00001873void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001874 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001875 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001876 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001877 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001878 int flags /* Conflict resolution algorithms. */
1879){
drhb7f91642004-10-31 02:22:47 +00001880#ifndef SQLITE_OMIT_FOREIGN_KEY
drh40e016e2004-11-04 14:47:11 +00001881 FKey *pFKey = 0;
drhc2eef3b2002-08-31 18:53:06 +00001882 Table *p = pParse->pNewTable;
1883 int nByte;
1884 int i;
1885 int nCol;
1886 char *z;
drhc2eef3b2002-08-31 18:53:06 +00001887
1888 assert( pTo!=0 );
1889 if( p==0 || pParse->nErr ) goto fk_end;
1890 if( pFromCol==0 ){
1891 int iCol = p->nCol-1;
1892 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001893 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001894 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001895 " should reference only one column of table %T",
1896 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001897 goto fk_end;
1898 }
1899 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001900 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001901 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001902 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001903 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001904 goto fk_end;
1905 }else{
danielk19770202b292004-06-09 09:55:16 +00001906 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001907 }
1908 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1909 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001910 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001911 nByte += strlen(pToCol->a[i].zName) + 1;
1912 }
1913 }
1914 pFKey = sqliteMalloc( nByte );
1915 if( pFKey==0 ) goto fk_end;
1916 pFKey->pFrom = p;
1917 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001918 z = (char*)&pFKey[1];
1919 pFKey->aCol = (struct sColMap*)z;
1920 z += sizeof(struct sColMap)*nCol;
1921 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001922 memcpy(z, pTo->z, pTo->n);
1923 z[pTo->n] = 0;
1924 z += pTo->n+1;
1925 pFKey->pNextTo = 0;
1926 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001927 if( pFromCol==0 ){
1928 pFKey->aCol[0].iFrom = p->nCol-1;
1929 }else{
1930 for(i=0; i<nCol; i++){
1931 int j;
1932 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001933 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001934 pFKey->aCol[i].iFrom = j;
1935 break;
1936 }
1937 }
1938 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001939 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001940 "unknown column \"%s\" in foreign key definition",
1941 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001942 goto fk_end;
1943 }
1944 }
1945 }
1946 if( pToCol ){
1947 for(i=0; i<nCol; i++){
1948 int n = strlen(pToCol->a[i].zName);
1949 pFKey->aCol[i].zCol = z;
1950 memcpy(z, pToCol->a[i].zName, n);
1951 z[n] = 0;
1952 z += n+1;
1953 }
1954 }
1955 pFKey->isDeferred = 0;
1956 pFKey->deleteConf = flags & 0xff;
1957 pFKey->updateConf = (flags >> 8 ) & 0xff;
1958 pFKey->insertConf = (flags >> 16 ) & 0xff;
1959
1960 /* Link the foreign key to the table as the last step.
1961 */
1962 p->pFKey = pFKey;
1963 pFKey = 0;
1964
1965fk_end:
1966 sqliteFree(pFKey);
drhb7f91642004-10-31 02:22:47 +00001967#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
danielk19770202b292004-06-09 09:55:16 +00001968 sqlite3ExprListDelete(pFromCol);
1969 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001970}
1971
1972/*
1973** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1974** clause is seen as part of a foreign key definition. The isDeferred
1975** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1976** The behavior of the most recently created foreign key is adjusted
1977** accordingly.
1978*/
danielk19774adee202004-05-08 08:23:19 +00001979void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhb7f91642004-10-31 02:22:47 +00001980#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:06 +00001981 Table *pTab;
1982 FKey *pFKey;
1983 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1984 pFKey->isDeferred = isDeferred;
drhb7f91642004-10-31 02:22:47 +00001985#endif
drhc2eef3b2002-08-31 18:53:06 +00001986}
1987
1988/*
drh75897232000-05-29 14:26:00 +00001989** Create a new index for an SQL table. pIndex is the name of the index
1990** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001991** be NULL for a primary key or an index that is created to satisfy a
1992** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001993** as the table to be indexed. pParse->pNewTable is a table that is
1994** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001995**
drh382c0242001-10-06 16:33:02 +00001996** pList is a list of columns to be indexed. pList will be NULL if this
1997** is a primary key or unique-constraint on the most recent column added
1998** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001999*/
danielk19774adee202004-05-08 08:23:19 +00002000void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00002001 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00002002 Token *pName1, /* First part of index name. May be NULL */
2003 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00002004 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
2005 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00002006 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00002007 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
2008 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
2009){
danielk1977cbb18d22004-05-28 11:37:27 +00002010 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00002011 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00002012 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00002013 int i, j;
drhf26e09c2003-05-31 16:21:12 +00002014 Token nullId; /* Fake token for an empty ID list */
2015 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00002016 int isTemp; /* True for a temporary index */
drh9bb575f2004-09-06 17:24:11 +00002017 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002018
danielk1977cbb18d22004-05-28 11:37:27 +00002019 int iDb; /* Index of the database that is being written */
2020 Token *pName = 0; /* Unqualified name of the index to create */
2021
danielk197724b03fd2004-05-10 10:34:34 +00002022 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00002023
drh75897232000-05-29 14:26:00 +00002024 /*
2025 ** Find the table that is to be indexed. Return early if not found.
2026 */
danielk1977cbb18d22004-05-28 11:37:27 +00002027 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002028
2029 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00002030 ** to search for the table. 'Fix' the table name to this db
2031 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00002032 */
2033 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00002034 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00002035 if( iDb<0 ) goto exit_create_index;
2036
danielk1977ef2cb632004-05-29 02:37:19 +00002037 /* If the index name was unqualified, check if the the table
2038 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00002039 */
danielk1977ef2cb632004-05-29 02:37:19 +00002040 pTab = sqlite3SrcListLookup(pParse, pTblName);
2041 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
2042 iDb = 1;
2043 }
2044
2045 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
2046 sqlite3FixSrcList(&sFix, pTblName)
2047 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002048 goto exit_create_index;
2049 }
danielk1977ef2cb632004-05-29 02:37:19 +00002050 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
2051 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00002052 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00002053 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00002054 }else{
drhe3c41372001-09-17 20:25:58 +00002055 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00002056 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00002057 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00002058 }
danielk1977cbb18d22004-05-28 11:37:27 +00002059
drh75897232000-05-29 14:26:00 +00002060 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00002061 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00002062 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00002063 goto exit_create_index;
2064 }
drha76b5df2002-02-23 02:32:10 +00002065 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00002066 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00002067 goto exit_create_index;
2068 }
drh4925ca02003-11-27 00:48:57 +00002069 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00002070
2071 /*
2072 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00002073 ** index or table with the same name.
2074 **
2075 ** Exception: If we are reading the names of permanent indices from the
2076 ** sqlite_master table (because some other process changed the schema) and
2077 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00002078 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00002079 **
2080 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00002081 ** dealing with a primary key or UNIQUE constraint. We have to invent our
2082 ** own name.
drh75897232000-05-29 14:26:00 +00002083 */
danielk1977d8123362004-06-12 09:25:12 +00002084 if( pName ){
drha99db3b2004-06-19 14:49:12 +00002085 zName = sqlite3NameFromToken(pName);
danielk19778a414492004-06-29 08:59:35 +00002086 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002087 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002088 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00002089 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002090 }
danielk1977d8123362004-06-12 09:25:12 +00002091 if( !db->init.busy ){
2092 Index *pISameName; /* Another index with the same name */
2093 Table *pTSameName; /* A table with same name as the index */
danielk19778a414492004-06-29 08:59:35 +00002094 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002095 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
2096 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
2097 goto exit_create_index;
2098 }
2099 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
2100 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
2101 goto exit_create_index;
2102 }
drhe3c41372001-09-17 20:25:58 +00002103 }
drhd24cc422003-03-27 12:51:24 +00002104 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00002105 char zBuf[30];
2106 int n;
2107 Index *pLoop;
2108 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00002109 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00002110 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00002111 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00002112 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00002113 }
2114
drhe5f9c642003-01-13 23:27:31 +00002115 /* Check for authorization to create an index.
2116 */
2117#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00002118 {
2119 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00002120 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002121 goto exit_create_index;
2122 }
2123 i = SQLITE_CREATE_INDEX;
2124 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002125 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002126 goto exit_create_index;
2127 }
drhe5f9c642003-01-13 23:27:31 +00002128 }
2129#endif
2130
drh75897232000-05-29 14:26:00 +00002131 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00002132 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00002133 ** So create a fake list to simulate this.
2134 */
2135 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00002136 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00002137 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00002138 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00002139 if( pList==0 ) goto exit_create_index;
2140 }
2141
2142 /*
2143 ** Allocate the index structure.
2144 */
drhdcc581c2000-05-30 13:44:19 +00002145 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00002146 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00002147 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00002148 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
2149 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00002150 strcpy(pIndex->zName, zName);
2151 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00002152 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00002153 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00002154 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00002155 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00002156
drh1ccde152000-06-17 13:12:39 +00002157 /* Scan the names of the columns of the table to be indexed and
2158 ** load the column indices into the Index structure. Report an error
2159 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00002160 */
danielk19770202b292004-06-09 09:55:16 +00002161 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00002162 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002163 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002164 }
2165 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002166 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00002167 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002168 goto exit_create_index;
2169 }
drh967e8b72000-06-21 13:59:10 +00002170 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00002171 if( pList->a[i].pExpr ){
2172 assert( pList->a[i].pExpr->pColl );
2173 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
2174 }else{
2175 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
2176 }
2177 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00002178 if( !db->init.busy &&
2179 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2180 ){
2181 goto exit_create_index;
2182 }
drh75897232000-05-29 14:26:00 +00002183 }
danielk19770202b292004-06-09 09:55:16 +00002184 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002185
danielk1977d8123362004-06-12 09:25:12 +00002186 if( pTab==pParse->pNewTable ){
2187 /* This routine has been called to create an automatic index as a
2188 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2189 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2190 ** i.e. one of:
2191 **
2192 ** CREATE TABLE t(x PRIMARY KEY, y);
2193 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2194 **
2195 ** Either way, check to see if the table already has such an index. If
2196 ** so, don't bother creating this one. This only applies to
2197 ** automatically created indices. Users can do as they wish with
2198 ** explicit indices.
2199 */
2200 Index *pIdx;
2201 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2202 int k;
2203 assert( pIdx->onError!=OE_None );
2204 assert( pIdx->autoIndex );
2205 assert( pIndex->onError!=OE_None );
2206
2207 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2208 for(k=0; k<pIdx->nColumn; k++){
2209 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2210 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2211 }
2212 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002213 if( pIdx->onError!=pIndex->onError ){
2214 /* This constraint creates the same index as a previous
2215 ** constraint specified somewhere in the CREATE TABLE statement.
2216 ** However the ON CONFLICT clauses are different. If both this
2217 ** constraint and the previous equivalent constraint have explicit
2218 ** ON CONFLICT clauses this is an error. Otherwise, use the
2219 ** explicitly specified behaviour for the index.
2220 */
2221 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2222 sqlite3ErrorMsg(pParse,
2223 "conflicting ON CONFLICT clauses specified", 0);
2224 }
2225 if( pIdx->onError==OE_Default ){
2226 pIdx->onError = pIndex->onError;
2227 }
2228 }
danielk1977d8123362004-06-12 09:25:12 +00002229 goto exit_create_index;
2230 }
2231 }
2232 }
2233
drh75897232000-05-29 14:26:00 +00002234 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002235 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002236 */
drh234c39d2004-07-24 03:30:47 +00002237 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002238 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002239 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002240 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002241 if( p ){
2242 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002243 goto exit_create_index;
2244 }
drh5e00f6c2001-09-13 13:46:56 +00002245 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002246 if( pTblName!=0 ){
2247 pIndex->tnum = db->init.newTnum;
2248 }
drhd78eeee2001-09-13 16:18:53 +00002249 }
2250
drh1d85d932004-02-14 23:05:52 +00002251 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002252 ** involves writing the index into the master table and filling in the
2253 ** index with the current table contents.
2254 **
drh1d85d932004-02-14 23:05:52 +00002255 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2256 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002257 ** CREATE INDEX statements are read out of the master table. In
2258 ** the latter case the index already exists on disk, which is why
2259 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002260 **
danielk1977cbb18d22004-05-28 11:37:27 +00002261 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002262 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2263 ** has just been created, it contains no data and the index initialization
2264 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002265 */
drh1d85d932004-02-14 23:05:52 +00002266 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00002267 int n;
drhadbca9c2001-09-27 15:11:53 +00002268 Vdbe *v;
drh75897232000-05-29 14:26:00 +00002269 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00002270
danielk19774adee202004-05-08 08:23:19 +00002271 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002272 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00002273 if( pTblName!=0 ){
2274 sqlite3BeginWriteOperation(pParse, 0, iDb);
2275 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00002276 }
danielk19774adee202004-05-08 08:23:19 +00002277 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00002278 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
2279 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
2280 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
drh234c39d2004-07-24 03:30:47 +00002281 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002282 if( pTblName ){
drha99db3b2004-06-19 14:49:12 +00002283 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2284 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002285 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
2286 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00002287 }
danielk19770f69c1e2004-05-29 11:24:50 +00002288 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00002289 if( pStart && pEnd ){
danielk19770202b292004-06-09 09:55:16 +00002290 if( onError==OE_None ){
2291 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
2292 }else{
2293 sqlite3VdbeChangeP3(v, -1, "CREATE UNIQUE INDEX ", P3_STATIC);
2294 }
danielk19770f69c1e2004-05-29 11:24:50 +00002295 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002296 n = Addr(pEnd->z) - Addr(pName->z) + 1;
2297 sqlite3VdbeChangeP3(v, -1, pName->z, n);
drh855eb1c2004-08-31 13:45:11 +00002298 sqlite3VdbeAddOp(v, OP_Concat, 0, 0);
drh75897232000-05-29 14:26:00 +00002299 }
danielk197784ac9d02004-05-18 09:58:06 +00002300 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002301 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002302 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00002303 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002304 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
2305 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00002306 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00002307 lbl2 = sqlite3VdbeMakeLabel(v);
2308 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00002309 lbl1 = sqlite3VdbeCurrentAddr(v);
2310 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00002311 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00002312 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002313 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
2314 sqlite3VdbeResolveLabel(v, lbl2);
2315 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
2316 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drhc275b4e2004-07-19 17:25:24 +00002317 sqlite3ChangeCookie(db, v, iDb);
danielk19774adee202004-05-08 08:23:19 +00002318 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh234c39d2004-07-24 03:30:47 +00002319 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
2320 sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC);
drh5e00f6c2001-09-13 13:46:56 +00002321 }
drh75897232000-05-29 14:26:00 +00002322 }
2323
danielk1977d8123362004-06-12 09:25:12 +00002324 /* When adding an index to the list of indices for a table, make
2325 ** sure all indices labeled OE_Replace come after all those labeled
2326 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2327 ** and INSERT.
2328 */
drh234c39d2004-07-24 03:30:47 +00002329 if( db->init.busy || pTblName==0 ){
2330 if( onError!=OE_Replace || pTab->pIndex==0
2331 || pTab->pIndex->onError==OE_Replace){
2332 pIndex->pNext = pTab->pIndex;
2333 pTab->pIndex = pIndex;
2334 }else{
2335 Index *pOther = pTab->pIndex;
2336 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2337 pOther = pOther->pNext;
2338 }
2339 pIndex->pNext = pOther->pNext;
2340 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002341 }
drh234c39d2004-07-24 03:30:47 +00002342 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002343 }
danielk1977d8123362004-06-12 09:25:12 +00002344
drh75897232000-05-29 14:26:00 +00002345 /* Clean up before exiting */
2346exit_create_index:
drh956bc922004-07-24 17:38:29 +00002347 if( pIndex ){
2348 freeIndex(pIndex);
2349 }
danielk19770202b292004-06-09 09:55:16 +00002350 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002351 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002352 sqliteFree(zName);
2353 return;
2354}
2355
2356/*
drh74e24cd2002-01-09 03:19:59 +00002357** This routine will drop an existing named index. This routine
2358** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002359*/
danielk19774adee202004-05-08 08:23:19 +00002360void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002361 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002362 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00002363 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002364
danielk197724b03fd2004-05-10 10:34:34 +00002365 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002366 assert( pName->nSrc==1 );
danielk19778a414492004-06-29 08:59:35 +00002367 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19774adee202004-05-08 08:23:19 +00002368 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002369 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002370 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002371 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002372 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002373 }
drh485b39b2002-07-13 03:11:52 +00002374 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002375 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002376 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002377 goto exit_drop_index;
2378 }
drhe5f9c642003-01-13 23:27:31 +00002379#ifndef SQLITE_OMIT_AUTHORIZATION
2380 {
2381 int code = SQLITE_DROP_INDEX;
2382 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002383 const char *zDb = db->aDb[pIndex->iDb].zName;
2384 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002385 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002386 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002387 }
drhd24cc422003-03-27 12:51:24 +00002388 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002389 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002390 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002391 }
drhed6c8672003-01-12 18:02:16 +00002392 }
drhe5f9c642003-01-13 23:27:31 +00002393#endif
drh75897232000-05-29 14:26:00 +00002394
2395 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002396 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002397 if( v ){
drh57196282004-10-06 15:41:16 +00002398 static const VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00002399 { OP_Rewind, 0, ADDR(9), 0},
drh234c39d2004-07-24 03:30:47 +00002400 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00002401 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002402 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00002403 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002404 { OP_Eq, 0, ADDR(8), 0},
2405 { OP_Next, 0, ADDR(3), 0},
2406 { OP_Goto, 0, ADDR(9), 0},
2407 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00002408 };
2409 int base;
2410
danielk19774adee202004-05-08 08:23:19 +00002411 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
2412 sqlite3OpenMasterTable(v, pIndex->iDb);
2413 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
2414 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
drhc275b4e2004-07-19 17:25:24 +00002415 sqlite3ChangeCookie(db, v, pIndex->iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00002416 /* sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb); */
2417 destroyRootPage(v, pIndex->tnum, pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002418 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh956bc922004-07-24 17:38:29 +00002419 sqlite3VdbeOp3(v, OP_DropIndex, pIndex->iDb, 0, pIndex->zName, 0);
drh75897232000-05-29 14:26:00 +00002420 }
2421
drhd24cc422003-03-27 12:51:24 +00002422exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002423 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002424}
2425
2426/*
drh75897232000-05-29 14:26:00 +00002427** Append a new element to the given IdList. Create a new IdList if
2428** need be.
drhdaffd0e2001-04-11 14:28:42 +00002429**
2430** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002431*/
danielk19774adee202004-05-08 08:23:19 +00002432IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002433 if( pList==0 ){
2434 pList = sqliteMalloc( sizeof(IdList) );
2435 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002436 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002437 }
drh4305d102003-07-30 12:34:12 +00002438 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002439 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002440 pList->nAlloc = pList->nAlloc*2 + 5;
2441 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002442 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002443 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002444 return 0;
drh75897232000-05-29 14:26:00 +00002445 }
drh6d4abfb2001-10-22 02:58:08 +00002446 pList->a = a;
drh75897232000-05-29 14:26:00 +00002447 }
2448 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
drha99db3b2004-06-19 14:49:12 +00002449 pList->a[pList->nId].zName = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002450 pList->nId++;
2451 return pList;
2452}
2453
2454/*
drhad3cab52002-05-24 02:04:32 +00002455** Append a new table name to the given SrcList. Create a new SrcList if
2456** need be. A new entry is created in the SrcList even if pToken is NULL.
2457**
2458** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002459**
2460** If pDatabase is not null, it means that the table has an optional
2461** database name prefix. Like this: "database.table". The pDatabase
2462** points to the table name and the pTable points to the database name.
2463** The SrcList.a[].zName field is filled with the table name which might
2464** come from pTable (if pDatabase is NULL) or from pDatabase.
2465** SrcList.a[].zDatabase is filled with the database name from pTable,
2466** or with NULL if no database is specified.
2467**
2468** In other words, if call like this:
2469**
danielk19774adee202004-05-08 08:23:19 +00002470** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002471**
2472** Then B is a table name and the database name is unspecified. If called
2473** like this:
2474**
danielk19774adee202004-05-08 08:23:19 +00002475** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002476**
2477** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002478*/
danielk19774adee202004-05-08 08:23:19 +00002479SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drha99db3b2004-06-19 14:49:12 +00002480 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002481 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002482 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002483 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002484 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002485 }
drh4305d102003-07-30 12:34:12 +00002486 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002487 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002488 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002489 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002490 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002491 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002492 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002493 return 0;
2494 }
drh113088e2003-03-20 01:16:58 +00002495 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002496 }
drha99db3b2004-06-19 14:49:12 +00002497 pItem = &pList->a[pList->nSrc];
2498 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002499 if( pDatabase && pDatabase->z==0 ){
2500 pDatabase = 0;
2501 }
2502 if( pDatabase && pTable ){
2503 Token *pTemp = pDatabase;
2504 pDatabase = pTable;
2505 pTable = pTemp;
2506 }
drha99db3b2004-06-19 14:49:12 +00002507 pItem->zName = sqlite3NameFromToken(pTable);
2508 pItem->zDatabase = sqlite3NameFromToken(pDatabase);
2509 pItem->iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002510 pList->nSrc++;
2511 return pList;
2512}
2513
2514/*
drh63eb5f22003-04-29 16:20:44 +00002515** Assign cursors to all tables in a SrcList
2516*/
danielk19774adee202004-05-08 08:23:19 +00002517void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002518 int i;
2519 for(i=0; i<pList->nSrc; i++){
2520 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002521 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002522 }
2523 }
2524}
2525
2526/*
drh75897232000-05-29 14:26:00 +00002527** Add an alias to the last identifier on the given identifier list.
2528*/
danielk19774adee202004-05-08 08:23:19 +00002529void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002530 if( pList && pList->nSrc>0 ){
drha99db3b2004-06-19 14:49:12 +00002531 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002532 }
2533}
2534
2535/*
drhad3cab52002-05-24 02:04:32 +00002536** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002537*/
danielk19774adee202004-05-08 08:23:19 +00002538void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002539 int i;
2540 if( pList==0 ) return;
2541 for(i=0; i<pList->nId; i++){
2542 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002543 }
2544 sqliteFree(pList->a);
2545 sqliteFree(pList);
2546}
2547
2548/*
drhad2d8302002-05-24 20:31:36 +00002549** Return the index in pList of the identifier named zId. Return -1
2550** if not found.
2551*/
danielk19774adee202004-05-08 08:23:19 +00002552int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002553 int i;
2554 if( pList==0 ) return -1;
2555 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002556 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002557 }
2558 return -1;
2559}
2560
2561/*
drhad3cab52002-05-24 02:04:32 +00002562** Delete an entire SrcList including all its substructure.
2563*/
danielk19774adee202004-05-08 08:23:19 +00002564void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002565 int i;
drhbe5c89a2004-07-26 00:31:09 +00002566 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002567 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00002568 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
2569 sqliteFree(pItem->zDatabase);
2570 sqliteFree(pItem->zName);
2571 sqliteFree(pItem->zAlias);
2572 if( pItem->pTab && pItem->pTab->isTransient ){
2573 sqlite3DeleteTable(0, pItem->pTab);
drhdaffd0e2001-04-11 14:28:42 +00002574 }
drhbe5c89a2004-07-26 00:31:09 +00002575 sqlite3SelectDelete(pItem->pSelect);
2576 sqlite3ExprDelete(pItem->pOn);
2577 sqlite3IdListDelete(pItem->pUsing);
drh75897232000-05-29 14:26:00 +00002578 }
drh75897232000-05-29 14:26:00 +00002579 sqliteFree(pList);
2580}
2581
drh982cef72000-05-30 16:27:03 +00002582/*
drhc4a3c772001-04-04 11:48:57 +00002583** Begin a transaction
2584*/
drh684917c2004-10-05 02:41:42 +00002585void sqlite3BeginTransaction(Parse *pParse, int type){
drh9bb575f2004-09-06 17:24:11 +00002586 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002587 Vdbe *v;
drh684917c2004-10-05 02:41:42 +00002588 int i;
drh5e00f6c2001-09-13 13:46:56 +00002589
drh001bbcb2003-03-19 03:14:00 +00002590 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002591 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002592 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002593
2594 v = sqlite3GetVdbe(pParse);
2595 if( !v ) return;
drh684917c2004-10-05 02:41:42 +00002596 if( type!=TK_DEFERRED ){
2597 for(i=0; i<db->nDb; i++){
2598 sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
2599 }
2600 }
danielk19771d850a72004-05-31 08:26:49 +00002601 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002602}
2603
2604/*
2605** Commit a transaction
2606*/
danielk19774adee202004-05-08 08:23:19 +00002607void sqlite3CommitTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002608 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002609 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002610
drh001bbcb2003-03-19 03:14:00 +00002611 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002612 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002613 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002614
2615 v = sqlite3GetVdbe(pParse);
2616 if( v ){
2617 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002618 }
drhc4a3c772001-04-04 11:48:57 +00002619}
2620
2621/*
2622** Rollback a transaction
2623*/
danielk19774adee202004-05-08 08:23:19 +00002624void sqlite3RollbackTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002625 sqlite3 *db;
drh5e00f6c2001-09-13 13:46:56 +00002626 Vdbe *v;
2627
drh001bbcb2003-03-19 03:14:00 +00002628 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002629 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002630 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002631
danielk19774adee202004-05-08 08:23:19 +00002632 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002633 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002634 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002635 }
drhc4a3c772001-04-04 11:48:57 +00002636}
drhf57b14a2001-09-14 18:54:08 +00002637
2638/*
drhdc3ff9c2004-08-18 02:10:15 +00002639** Make sure the TEMP database is open and available for use. Return
2640** the number of errors. Leave any error messages in the pParse structure.
2641*/
2642static int sqlite3OpenTempDatabase(Parse *pParse){
2643 sqlite3 *db = pParse->db;
2644 if( db->aDb[1].pBt==0 && !pParse->explain ){
2645 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
2646 if( rc!=SQLITE_OK ){
2647 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
2648 "file for storing temporary tables");
2649 pParse->rc = rc;
2650 return 1;
2651 }
2652 if( db->flags & !db->autoCommit ){
2653 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
2654 if( rc!=SQLITE_OK ){
2655 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
2656 "the temporary database file");
2657 pParse->rc = rc;
2658 return 1;
2659 }
2660 }
2661 }
2662 return 0;
2663}
2664
2665/*
drh80242052004-06-09 00:48:12 +00002666** Generate VDBE code that will verify the schema cookie and start
2667** a read-transaction for all named database files.
2668**
2669** It is important that all schema cookies be verified and all
2670** read transactions be started before anything else happens in
2671** the VDBE program. But this routine can be called after much other
2672** code has been generated. So here is what we do:
2673**
drhc275b4e2004-07-19 17:25:24 +00002674** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00002675** will jump to a subroutine at the end of the program. Then we
2676** record every database that needs its schema verified in the
2677** pParse->cookieMask field. Later, after all other code has been
2678** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00002679** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00002680** will be made to point to that subroutine. The generation of the
2681** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00002682**
2683** If iDb<0 then code the OP_Goto only - don't set flag to verify the
2684** schema on any databases. This can be used to position the OP_Goto
2685** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00002686*/
danielk19774adee202004-05-08 08:23:19 +00002687void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh9bb575f2004-09-06 17:24:11 +00002688 sqlite3 *db;
drh80242052004-06-09 00:48:12 +00002689 Vdbe *v;
2690 int mask;
2691
2692 v = sqlite3GetVdbe(pParse);
2693 if( v==0 ) return; /* This only happens if there was a prior error */
2694 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00002695 if( pParse->cookieGoto==0 ){
2696 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00002697 }
drhc275b4e2004-07-19 17:25:24 +00002698 if( iDb>=0 ){
2699 assert( iDb<db->nDb );
2700 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2701 assert( iDb<32 );
2702 mask = 1<<iDb;
2703 if( (pParse->cookieMask & mask)==0 ){
2704 pParse->cookieMask |= mask;
2705 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
drhdc3ff9c2004-08-18 02:10:15 +00002706 if( iDb==1 ){
2707 sqlite3OpenTempDatabase(pParse);
2708 }
drhc275b4e2004-07-19 17:25:24 +00002709 }
drh001bbcb2003-03-19 03:14:00 +00002710 }
drh001bbcb2003-03-19 03:14:00 +00002711}
2712
2713/*
drh1c928532002-01-31 15:54:21 +00002714** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002715** might change the database.
2716**
2717** This routine starts a new transaction if we are not already within
2718** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002719** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002720** be set for operations that might fail (due to a constraint) part of
2721** the way through and which will need to undo some writes without having to
2722** rollback the whole transaction. For operations where all constraints
2723** can be checked before any changes are made to the database, it is never
2724** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002725**
drh8bf8dc92003-05-17 17:35:10 +00002726** Only database iDb and the temp database are made writable by this call.
2727** If iDb==0, then the main and temp databases are made writable. If
2728** iDb==1 then only the temp database is made writable. If iDb>1 then the
2729** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002730*/
drh7f0f12e2004-05-21 13:39:50 +00002731void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002732 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002733 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002734 sqlite3CodeVerifySchema(pParse, iDb);
2735 pParse->writeMask |= 1<<iDb;
danielk19771d850a72004-05-31 08:26:49 +00002736 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002737 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002738 }
drh34f47322004-08-18 15:58:22 +00002739 if( iDb!=1 && pParse->db->aDb[1].pBt!=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002740 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002741 }
2742}
2743
danielk1977bfd6cce2004-06-18 04:24:54 +00002744/*
2745** Return the transient sqlite3_value object used for encoding conversions
2746** during SQL compilation.
2747*/
drh9bb575f2004-09-06 17:24:11 +00002748sqlite3_value *sqlite3GetTransientValue(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +00002749 if( !db->pValue ){
2750 db->pValue = sqlite3ValueNew();
2751 }
2752 return db->pValue;
2753}