blob: 9dc6db201431033d49dc049fa770b2fe830ac2e6 [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
drhbed86902000-06-02 13:27:59 +000024**
danielk1977e6efa742004-11-10 11:55:10 +000025** $Id: build.c,v 1.275 2004/11/10 11:55:12 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000026*/
27#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000028#include <ctype.h>
drh75897232000-05-29 14:26:00 +000029
30/*
drhe0bc4042002-06-25 01:09:11 +000031** This routine is called when a new SQL statement is beginning to
32** be parsed. Check to see if the schema for the database needs
33** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
34** If it does, then read it.
35*/
danielk19774adee202004-05-08 08:23:19 +000036void sqlite3BeginParse(Parse *pParse, int explainFlag){
drhe0bc4042002-06-25 01:09:11 +000037 pParse->explain = explainFlag;
drh7c972de2003-09-06 22:18:07 +000038 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000039}
40
41/*
drh75897232000-05-29 14:26:00 +000042** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12 +000043** parsed and a VDBE program to execute that statement has been
44** prepared. This routine puts the finishing touches on the
45** VDBE program and resets the pParse structure for the next
46** parse.
drh75897232000-05-29 14:26:00 +000047**
48** Note that if an error occurred, it might be the case that
49** no VDBE code was generated.
50*/
drh80242052004-06-09 00:48:12 +000051void sqlite3FinishCoding(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +000052 sqlite3 *db;
drh80242052004-06-09 00:48:12 +000053 Vdbe *v;
drhb86ccfb2003-01-28 23:13:10 +000054
danielk197724b03fd2004-05-10 10:34:34 +000055 if( sqlite3_malloc_failed ) return;
drh205f48e2004-11-05 00:43:11 +000056 if( pParse->nested ) return;
drh80242052004-06-09 00:48:12 +000057
58 /* Begin by generating some termination code at the end of the
59 ** vdbe program
60 */
61 db = pParse->db;
62 v = sqlite3GetVdbe(pParse);
63 if( v ){
64 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
drh0e3d7472004-06-19 17:33:07 +000065
66 /* The cookie mask contains one bit for each database file open.
67 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
68 ** set for each database that is used. Generate code to start a
69 ** transaction on each used database and to verify the schema cookie
70 ** on each used database.
71 */
drhc275b4e2004-07-19 17:25:24 +000072 if( pParse->cookieGoto>0 ){
drh80242052004-06-09 00:48:12 +000073 u32 mask;
74 int iDb;
drhc275b4e2004-07-19 17:25:24 +000075 sqlite3VdbeChangeP2(v, pParse->cookieGoto-1, sqlite3VdbeCurrentAddr(v));
drh80242052004-06-09 00:48:12 +000076 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
77 if( (mask & pParse->cookieMask)==0 ) continue;
78 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
drhc275b4e2004-07-19 17:25:24 +000079 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
drh80242052004-06-09 00:48:12 +000080 }
drhc275b4e2004-07-19 17:25:24 +000081 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto);
drh80242052004-06-09 00:48:12 +000082 }
drh80242052004-06-09 00:48:12 +000083
drh71c697e2004-08-08 23:39:19 +000084 /* Add a No-op that contains the complete text of the compiled SQL
85 ** statement as its P3 argument. This does not change the functionality
drhc16a03b2004-09-15 13:38:10 +000086 ** of the program.
87 **
88 ** This is used to implement sqlite3_trace() functionality.
drh71c697e2004-08-08 23:39:19 +000089 */
90 sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql);
drh71c697e2004-08-08 23:39:19 +000091 }
92
drh3f7d4e42004-07-24 14:35:58 +000093
drh80242052004-06-09 00:48:12 +000094 /* Get the VDBE program ready for execution
95 */
drhb86ccfb2003-01-28 23:13:10 +000096 if( v && pParse->nErr==0 ){
97 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +000098 sqlite3VdbeTrace(v, trace);
drh290c1942004-08-21 17:54:45 +000099 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3,
100 pParse->nTab+3, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +0000101 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000102 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +0000103 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +0000104 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000105 }
drha226d052002-09-25 19:04:07 +0000106 pParse->nTab = 0;
107 pParse->nMem = 0;
108 pParse->nSet = 0;
109 pParse->nAgg = 0;
drh7c972de2003-09-06 22:18:07 +0000110 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000111 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000112 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000113}
114
115/*
drh205f48e2004-11-05 00:43:11 +0000116** Run the parser and code generator recursively in order to generate
117** code for the SQL statement given onto the end of the pParse context
118** currently under construction. When the parser is run recursively
119** this way, the final OP_Halt is not appended and other initialization
120** and finalization steps are omitted because those are handling by the
121** outermost parser.
122**
123** Not everything is nestable. This facility is designed to permit
124** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
drhf1974842004-11-05 03:56:00 +0000125** care if you decide to try to use this routine for some other purposes.
drh205f48e2004-11-05 00:43:11 +0000126*/
127void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
128 va_list ap;
129 char *zSql;
130 int rc;
drhf1974842004-11-05 03:56:00 +0000131# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
132 char saveBuf[SAVE_SZ];
133
drh205f48e2004-11-05 00:43:11 +0000134 if( pParse->nErr ) return;
135 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
136 va_start(ap, zFormat);
137 zSql = sqlite3VMPrintf(zFormat, ap);
138 va_end(ap);
139 pParse->nested++;
drhf1974842004-11-05 03:56:00 +0000140 memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
141 memset(&pParse->nVar, 0, SAVE_SZ);
drh205f48e2004-11-05 00:43:11 +0000142 rc = sqlite3RunParser(pParse, zSql, 0);
143 sqliteFree(zSql);
drhf1974842004-11-05 03:56:00 +0000144 memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
drh205f48e2004-11-05 00:43:11 +0000145 pParse->nested--;
146}
147
148/*
danielk19778a414492004-06-29 08:59:35 +0000149** Locate the in-memory structure that describes a particular database
150** table given the name of that table and (optionally) the name of the
151** database containing the table. Return NULL if not found.
drha69d9162003-04-17 22:57:53 +0000152**
danielk19778a414492004-06-29 08:59:35 +0000153** If zDatabase is 0, all databases are searched for the table and the
154** first matching table is returned. (No checking for duplicate table
155** names is done.) The search order is TEMP first, then MAIN, then any
156** auxiliary databases added using the ATTACH command.
drhf26e09c2003-05-31 16:21:12 +0000157**
danielk19774adee202004-05-08 08:23:19 +0000158** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000159*/
drh9bb575f2004-09-06 17:24:11 +0000160Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000161 Table *p = 0;
162 int i;
drh645f63e2004-06-22 13:22:40 +0000163 assert( zName!=0 );
danielk19778a414492004-06-29 08:59:35 +0000164 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
165 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000166 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000167 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
168 p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000169 if( p ) break;
170 }
drh74e24cd2002-01-09 03:19:59 +0000171 return p;
drh75897232000-05-29 14:26:00 +0000172}
173
174/*
danielk19778a414492004-06-29 08:59:35 +0000175** Locate the in-memory structure that describes a particular database
176** table given the name of that table and (optionally) the name of the
177** database containing the table. Return NULL if not found. Also leave an
178** error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000179**
danielk19778a414492004-06-29 08:59:35 +0000180** The difference between this routine and sqlite3FindTable() is that this
181** routine leaves an error message in pParse->zErrMsg where
182** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000183*/
danielk19774adee202004-05-08 08:23:19 +0000184Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000185 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000186
danielk19778a414492004-06-29 08:59:35 +0000187 /* Read the database schema. If an error occurs, leave an error message
188 ** and code in pParse and return NULL. */
189 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
190 return 0;
191 }
192
danielk19774adee202004-05-08 08:23:19 +0000193 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000194 if( p==0 ){
danielk19778a414492004-06-29 08:59:35 +0000195 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000196 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
197 }else if( sqlite3FindTable(pParse->db, zName, 0)!=0 ){
198 sqlite3ErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
drhf26e09c2003-05-31 16:21:12 +0000199 zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000200 }else{
danielk19774adee202004-05-08 08:23:19 +0000201 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000202 }
drha6ecd332004-06-10 00:29:09 +0000203 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000204 }
205 return p;
206}
207
208/*
209** Locate the in-memory structure that describes
210** a particular index given the name of that index
211** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000212** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000213**
214** If zDatabase is 0, all databases are searched for the
215** table and the first matching index is returned. (No checking
216** for duplicate index names is done.) The search order is
217** TEMP first, then MAIN, then any auxiliary databases added
218** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000219*/
drh9bb575f2004-09-06 17:24:11 +0000220Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000221 Index *p = 0;
222 int i;
danielk19778a414492004-06-29 08:59:35 +0000223 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
224 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000225 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000226 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
227 p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000228 if( p ) break;
229 }
drh74e24cd2002-01-09 03:19:59 +0000230 return p;
drh75897232000-05-29 14:26:00 +0000231}
232
233/*
drh956bc922004-07-24 17:38:29 +0000234** Reclaim the memory used by an index
235*/
236static void freeIndex(Index *p){
237 sqliteFree(p->zColAff);
238 sqliteFree(p);
239}
240
241/*
drh75897232000-05-29 14:26:00 +0000242** Remove the given index from the index hash table, and free
243** its memory structures.
244**
drhd229ca92002-01-09 13:30:41 +0000245** The index is removed from the database hash tables but
246** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000247** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000248*/
drh9bb575f2004-09-06 17:24:11 +0000249static void sqliteDeleteIndex(sqlite3 *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000250 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000251
drhd229ca92002-01-09 13:30:41 +0000252 assert( db!=0 && p->zName!=0 );
danielk19774adee202004-05-08 08:23:19 +0000253 pOld = sqlite3HashInsert(&db->aDb[p->iDb].idxHash, p->zName,
drhd24cc422003-03-27 12:51:24 +0000254 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000255 if( pOld!=0 && pOld!=p ){
danielk19774adee202004-05-08 08:23:19 +0000256 sqlite3HashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
drhd24cc422003-03-27 12:51:24 +0000257 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000258 }
drh956bc922004-07-24 17:38:29 +0000259 freeIndex(p);
drh75897232000-05-29 14:26:00 +0000260}
261
262/*
drhbeae3192001-09-22 18:12:08 +0000263** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000264** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000265** structures.
266*/
drh9bb575f2004-09-06 17:24:11 +0000267void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
drh956bc922004-07-24 17:38:29 +0000268 Index *pIndex;
269 int len;
270
271 len = strlen(zIdxName);
272 pIndex = sqlite3HashInsert(&db->aDb[iDb].idxHash, zIdxName, len+1, 0);
273 if( pIndex ){
274 if( pIndex->pTable->pIndex==pIndex ){
275 pIndex->pTable->pIndex = pIndex->pNext;
276 }else{
277 Index *p;
278 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
279 if( p && p->pNext==pIndex ){
280 p->pNext = pIndex->pNext;
281 }
drh5e00f6c2001-09-13 13:46:56 +0000282 }
drh956bc922004-07-24 17:38:29 +0000283 freeIndex(pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000284 }
drh956bc922004-07-24 17:38:29 +0000285 db->flags |= SQLITE_InternChanges;
drh5e00f6c2001-09-13 13:46:56 +0000286}
287
288/*
drhe0bc4042002-06-25 01:09:11 +0000289** Erase all schema information from the in-memory hash tables of
drh234c39d2004-07-24 03:30:47 +0000290** a single database. This routine is called to reclaim memory
291** before the database closes. It is also called during a rollback
danielk1977e0d4b062004-06-28 01:11:46 +0000292** if there were schema changes during the transaction or if a
293** schema-cookie mismatch occurs.
drh1c2d8412003-03-31 00:30:47 +0000294**
295** If iDb<=0 then reset the internal schema tables for all database
296** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000297** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000298*/
drh9bb575f2004-09-06 17:24:11 +0000299void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000300 HashElem *pElem;
301 Hash temp1;
302 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000303 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000304
drh1c2d8412003-03-31 00:30:47 +0000305 assert( iDb>=0 && iDb<db->nDb );
306 db->flags &= ~SQLITE_Initialized;
307 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000308 Db *pDb = &db->aDb[i];
309 temp1 = pDb->tblHash;
310 temp2 = pDb->trigHash;
danielk19774adee202004-05-08 08:23:19 +0000311 sqlite3HashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
312 sqlite3HashClear(&pDb->aFKey);
313 sqlite3HashClear(&pDb->idxHash);
drhd24cc422003-03-27 12:51:24 +0000314 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
drh40e016e2004-11-04 14:47:11 +0000315 sqlite3DeleteTrigger((Trigger*)sqliteHashData(pElem));
drhd24cc422003-03-27 12:51:24 +0000316 }
danielk19774adee202004-05-08 08:23:19 +0000317 sqlite3HashClear(&temp2);
318 sqlite3HashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
drhd24cc422003-03-27 12:51:24 +0000319 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
320 Table *pTab = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000321 sqlite3DeleteTable(db, pTab);
drhd24cc422003-03-27 12:51:24 +0000322 }
danielk19774adee202004-05-08 08:23:19 +0000323 sqlite3HashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000324 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000325 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000326 }
drh1c2d8412003-03-31 00:30:47 +0000327 assert( iDb==0 );
328 db->flags &= ~SQLITE_InternChanges;
329
330 /* If one or more of the auxiliary database files has been closed,
331 ** then remove then from the auxiliary database list. We take the
332 ** opportunity to do this here since we have just deleted all of the
333 ** schema hash tables and therefore do not have to make any changes
334 ** to any of those tables.
335 */
drh4d189ca2004-02-12 18:46:38 +0000336 for(i=0; i<db->nDb; i++){
337 struct Db *pDb = &db->aDb[i];
338 if( pDb->pBt==0 ){
339 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
340 pDb->pAux = 0;
341 }
342 }
drh1c2d8412003-03-31 00:30:47 +0000343 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000344 struct Db *pDb = &db->aDb[i];
345 if( pDb->pBt==0 ){
346 sqliteFree(pDb->zName);
347 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000348 continue;
349 }
350 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000351 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000352 }
drh8bf8dc92003-05-17 17:35:10 +0000353 j++;
drh1c2d8412003-03-31 00:30:47 +0000354 }
355 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
356 db->nDb = j;
357 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
358 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
359 sqliteFree(db->aDb);
360 db->aDb = db->aDbStatic;
361 }
drhe0bc4042002-06-25 01:09:11 +0000362}
363
364/*
365** This routine is called whenever a rollback occurs. If there were
366** schema changes during the transaction, then we have to reset the
367** internal hash tables and reload them from disk.
368*/
drh9bb575f2004-09-06 17:24:11 +0000369void sqlite3RollbackInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000370 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000371 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000372 }
373}
374
375/*
376** This routine is called when a commit occurs.
377*/
drh9bb575f2004-09-06 17:24:11 +0000378void sqlite3CommitInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000379 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000380}
381
382/*
drh956bc922004-07-24 17:38:29 +0000383** Clear the column names from a table or view.
384*/
385static void sqliteResetColumnNames(Table *pTable){
386 int i;
387 Column *pCol;
388 assert( pTable!=0 );
389 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
390 sqliteFree(pCol->zName);
danielk19777977a172004-11-09 12:44:37 +0000391 sqlite3ExprDelete(pCol->pDflt);
drh956bc922004-07-24 17:38:29 +0000392 sqliteFree(pCol->zType);
393 }
394 sqliteFree(pTable->aCol);
395 pTable->aCol = 0;
396 pTable->nCol = 0;
397}
398
399/*
drh75897232000-05-29 14:26:00 +0000400** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000401** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000402**
403** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000404** the table data structure from the hash table. Nor does it remove
405** foreign keys from the sqlite.aFKey hash table. But it does destroy
406** memory structures of the indices and foreign keys associated with
407** the table.
drhdaffd0e2001-04-11 14:28:42 +0000408**
409** Indices associated with the table are unlinked from the "db"
410** data structure if db!=NULL. If db==NULL, indices attached to
411** the table are deleted, but it is assumed they have already been
412** unlinked.
drh75897232000-05-29 14:26:00 +0000413*/
drh9bb575f2004-09-06 17:24:11 +0000414void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000415 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000416 FKey *pFKey, *pNextFKey;
417
drh75897232000-05-29 14:26:00 +0000418 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000419
420 /* Delete all indices associated with this table
421 */
422 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
423 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000424 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000425 sqliteDeleteIndex(db, pIndex);
426 }
427
428 /* Delete all foreign keys associated with this table. The keys
429 ** should have already been unlinked from the db->aFKey hash table
430 */
431 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
432 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000433 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000434 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000435 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000436 sqliteFree(pFKey);
437 }
438
439 /* Delete the Table structure itself.
440 */
drh956bc922004-07-24 17:38:29 +0000441 sqliteResetColumnNames(pTable);
drh6e142f52000-06-08 13:36:40 +0000442 sqliteFree(pTable->zName);
drh956bc922004-07-24 17:38:29 +0000443 sqliteFree(pTable->zColAff);
danielk19774adee202004-05-08 08:23:19 +0000444 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000445 sqliteFree(pTable);
446}
447
448/*
drh5edc3122001-09-13 21:53:09 +0000449** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000450** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000451*/
drh9bb575f2004-09-06 17:24:11 +0000452void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
drh956bc922004-07-24 17:38:29 +0000453 Table *p;
drhc2eef3b2002-08-31 18:53:06 +0000454 FKey *pF1, *pF2;
drh956bc922004-07-24 17:38:29 +0000455 Db *pDb;
456
drhd229ca92002-01-09 13:30:41 +0000457 assert( db!=0 );
drh956bc922004-07-24 17:38:29 +0000458 assert( iDb>=0 && iDb<db->nDb );
459 assert( zTabName && zTabName[0] );
460 pDb = &db->aDb[iDb];
461 p = sqlite3HashInsert(&pDb->tblHash, zTabName, strlen(zTabName)+1, 0);
462 if( p ){
463 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
464 int nTo = strlen(pF1->zTo) + 1;
465 pF2 = sqlite3HashFind(&pDb->aFKey, pF1->zTo, nTo);
466 if( pF2==pF1 ){
467 sqlite3HashInsert(&pDb->aFKey, pF1->zTo, nTo, pF1->pNextTo);
468 }else{
469 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
470 if( pF2 ){
471 pF2->pNextTo = pF1->pNextTo;
472 }
drhc2eef3b2002-08-31 18:53:06 +0000473 }
474 }
drh956bc922004-07-24 17:38:29 +0000475 sqlite3DeleteTable(db, p);
drhc2eef3b2002-08-31 18:53:06 +0000476 }
drh956bc922004-07-24 17:38:29 +0000477 db->flags |= SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000478}
479
480/*
drha99db3b2004-06-19 14:49:12 +0000481** Given a token, return a string that consists of the text of that
482** token with any quotations removed. Space to hold the returned string
483** is obtained from sqliteMalloc() and must be freed by the calling
484** function.
drh75897232000-05-29 14:26:00 +0000485**
drha99db3b2004-06-19 14:49:12 +0000486** Tokens are really just pointers into the original SQL text and so
487** are not \000 terminated and are not persistent. The returned string
488** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000489*/
drha99db3b2004-06-19 14:49:12 +0000490char *sqlite3NameFromToken(Token *pName){
491 char *zName;
492 if( pName ){
493 zName = sqliteStrNDup(pName->z, pName->n);
494 sqlite3Dequote(zName);
495 }else{
496 zName = 0;
497 }
drh75897232000-05-29 14:26:00 +0000498 return zName;
499}
500
501/*
danielk1977cbb18d22004-05-28 11:37:27 +0000502** Open the sqlite_master table stored in database number iDb for
503** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000504*/
danielk1977cbb18d22004-05-28 11:37:27 +0000505void sqlite3OpenMasterTable(Vdbe *v, int iDb){
506 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000507 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000508 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000509}
510
511/*
danielk1977cbb18d22004-05-28 11:37:27 +0000512** The token *pName contains the name of a database (either "main" or
513** "temp" or the name of an attached db). This routine returns the
514** index of the named database in db->aDb[], or -1 if the named db
515** does not exist.
516*/
517int findDb(sqlite3 *db, Token *pName){
518 int i;
drh90f5ecb2004-07-22 01:19:35 +0000519 Db *pDb;
520 for(pDb=db->aDb, i=0; i<db->nDb; i++, pDb++){
521 if( pName->n==strlen(pDb->zName) &&
522 0==sqlite3StrNICmp(pDb->zName, pName->z, pName->n) ){
danielk1977cbb18d22004-05-28 11:37:27 +0000523 return i;
524 }
525 }
526 return -1;
527}
528
drh0e3d7472004-06-19 17:33:07 +0000529/* The table or view or trigger name is passed to this routine via tokens
530** pName1 and pName2. If the table name was fully qualified, for example:
531**
532** CREATE TABLE xxx.yyy (...);
533**
534** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
535** the table name is not fully qualified, i.e.:
536**
537** CREATE TABLE yyy(...);
538**
539** Then pName1 is set to "yyy" and pName2 is "".
540**
541** This routine sets the *ppUnqual pointer to point at the token (pName1 or
542** pName2) that stores the unqualified table name. The index of the
543** database "xxx" is returned.
544*/
danielk1977ef2cb632004-05-29 02:37:19 +0000545int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000546 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35 +0000547 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07 +0000548 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
549 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000550){
drh0e3d7472004-06-19 17:33:07 +0000551 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000552 sqlite3 *db = pParse->db;
553
554 if( pName2 && pName2->n>0 ){
555 assert( !db->init.busy );
556 *pUnqual = pName2;
557 iDb = findDb(db, pName1);
558 if( iDb<0 ){
559 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
560 pParse->nErr++;
561 return -1;
562 }
563 }else{
564 assert( db->init.iDb==0 || db->init.busy );
565 iDb = db->init.iDb;
566 *pUnqual = pName1;
567 }
568 return iDb;
569}
570
571/*
danielk1977d8123362004-06-12 09:25:12 +0000572** This routine is used to check if the UTF-8 string zName is a legal
573** unqualified name for a new schema object (table, index, view or
574** trigger). All names are legal except those that begin with the string
575** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
576** is reserved for internal use.
577*/
578int sqlite3CheckObjectName(Parse *pParse, const char *zName){
drhf1974842004-11-05 03:56:00 +0000579 if( !pParse->db->init.busy && pParse->nested==0
580 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
danielk1977d8123362004-06-12 09:25:12 +0000581 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
582 return SQLITE_ERROR;
583 }
584 return SQLITE_OK;
585}
586
587/*
drh75897232000-05-29 14:26:00 +0000588** Begin constructing a new table representation in memory. This is
589** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000590** to a CREATE TABLE statement. In particular, this routine is called
591** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000592** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000593** flag is true if the table should be stored in the auxiliary database
594** file instead of in the main database file. This is normally the case
595** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000596** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000597**
drhf57b3392001-10-08 13:22:32 +0000598** The new table record is initialized and put in pParse->pNewTable.
599** As more of the CREATE TABLE statement is parsed, additional action
600** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000601** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000602** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000603*/
danielk19774adee202004-05-08 08:23:19 +0000604void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000605 Parse *pParse, /* Parser context */
606 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000607 Token *pName1, /* First part of the name of the table or view */
608 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000609 int isTemp, /* True if this is a TEMP table */
610 int isView /* True if this is a VIEW */
611){
drh75897232000-05-29 14:26:00 +0000612 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000613 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000614 char *zName;
drh9bb575f2004-09-06 17:24:11 +0000615 sqlite3 *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000616 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000617 int iDb; /* Database number to create the table in */
618 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000619
danielk1977cbb18d22004-05-28 11:37:27 +0000620 /* The table or view name to create is passed to this routine via tokens
621 ** pName1 and pName2. If the table name was fully qualified, for example:
622 **
623 ** CREATE TABLE xxx.yyy (...);
624 **
625 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
626 ** the table name is not fully qualified, i.e.:
627 **
628 ** CREATE TABLE yyy(...);
629 **
630 ** Then pName1 is set to "yyy" and pName2 is "".
631 **
632 ** The call below sets the pName pointer to point at the token (pName1 or
633 ** pName2) that stores the unqualified table name. The variable iDb is
634 ** set to the index of the database that the table or view is to be
635 ** created in.
636 */
danielk1977ef2cb632004-05-29 02:37:19 +0000637 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000638 if( iDb<0 ) return;
639 if( isTemp && iDb>1 ){
640 /* If creating a temp table, the name may not be qualified */
641 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
642 pParse->nErr++;
643 return;
644 }
645 if( isTemp ) iDb = 1;
646
647 pParse->sNameToken = *pName;
drha99db3b2004-06-19 14:49:12 +0000648 zName = sqlite3NameFromToken(pName);
danielk1977e0048402004-06-15 16:51:01 +0000649 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000650 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
danielk1977e0048402004-06-15 16:51:01 +0000651 sqliteFree(zName);
danielk1977d8123362004-06-12 09:25:12 +0000652 return;
653 }
drh1d85d932004-02-14 23:05:52 +0000654 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000655#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000656 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000657 {
658 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000659 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000660 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000661 sqliteFree(zName);
662 return;
663 }
drhe5f9c642003-01-13 23:27:31 +0000664 if( isView ){
665 if( isTemp ){
666 code = SQLITE_CREATE_TEMP_VIEW;
667 }else{
668 code = SQLITE_CREATE_VIEW;
669 }
670 }else{
671 if( isTemp ){
672 code = SQLITE_CREATE_TEMP_TABLE;
673 }else{
674 code = SQLITE_CREATE_TABLE;
675 }
676 }
danielk19774adee202004-05-08 08:23:19 +0000677 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000678 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000679 return;
680 }
681 }
682#endif
drhf57b3392001-10-08 13:22:32 +0000683
drhf57b3392001-10-08 13:22:32 +0000684 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000685 ** index or table name in the same database. Issue an error message if
686 ** it does.
drhf57b3392001-10-08 13:22:32 +0000687 */
danielk19778a414492004-06-29 08:59:35 +0000688 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19773df6b252004-05-29 10:23:19 +0000689 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
690 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000691 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000692 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000693 return;
drh75897232000-05-29 14:26:00 +0000694 }
danielk19778a414492004-06-29 08:59:35 +0000695 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
696 ( iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000697 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000698 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000699 return;
700 }
701 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000702 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000703 pParse->rc = SQLITE_NOMEM;
704 pParse->nErr++;
drh6d4abfb2001-10-22 02:58:08 +0000705 sqliteFree(zName);
706 return;
707 }
drh75897232000-05-29 14:26:00 +0000708 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000709 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000710 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000711 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000712 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000713 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000714 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000715 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000716
drh4794f732004-11-05 17:17:50 +0000717 /* If this is the magic sqlite_sequence table used by autoincrement,
718 ** then record a pointer to this table in the main database structure
719 ** so that INSERT can find the table easily.
720 */
721#ifndef SQLITE_OMIT_AUTOINCREMENT
722 if( strcmp(zName, "sqlite_sequence")==0 ){
723 assert( db->aDb[iDb].pSeqTab==0 );
724 db->aDb[iDb].pSeqTab = pTable;
725 }
726#endif
727
drh17f71932002-02-21 12:01:27 +0000728 /* Begin generating the code that will insert the table record into
729 ** the SQLITE_MASTER table. Note in particular that we must go ahead
730 ** and allocate the record number for the table entry now. Before any
731 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
732 ** indices to be created and the table record must come before the
733 ** indices. Hence, the record number for the table must be allocated
734 ** now.
735 */
danielk19774adee202004-05-08 08:23:19 +0000736 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000737 sqlite3BeginWriteOperation(pParse, 0, iDb);
drhb17131a2004-11-05 22:18:49 +0000738
danielk1977d008cfe2004-06-19 02:22:10 +0000739 /* Every time a new table is created the file-format
740 ** and encoding meta-values are set in the database, in
741 ** case this is the first table created.
742 */
743 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
744 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
745 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
746 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
747
drh4794f732004-11-05 17:17:50 +0000748 /* This just creates a place-holder record in the sqlite_master table.
749 ** The record created does not contain anything yet. It will be replaced
750 ** by the real entry in code generated at sqlite3EndTable().
drhb17131a2004-11-05 22:18:49 +0000751 **
752 ** The rowid for the new entry is left on the top of the stack.
753 ** The rowid value is needed by the code that sqlite3EndTable will
754 ** generate.
drh4794f732004-11-05 17:17:50 +0000755 */
danielk1977cbb18d22004-05-28 11:37:27 +0000756 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000757 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
758 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000759 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000760 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977e6efa742004-11-10 11:55:10 +0000761 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000762 }
drh75897232000-05-29 14:26:00 +0000763}
764
765/*
766** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000767**
768** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000769** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000770** first to get things going. Then this routine is called for each
771** column.
drh75897232000-05-29 14:26:00 +0000772*/
danielk19774adee202004-05-08 08:23:19 +0000773void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000774 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000775 int i;
drha99db3b2004-06-19 14:49:12 +0000776 char *z;
drhc9b84a12002-06-20 11:36:48 +0000777 Column *pCol;
drh75897232000-05-29 14:26:00 +0000778 if( (p = pParse->pNewTable)==0 ) return;
drha99db3b2004-06-19 14:49:12 +0000779 z = sqlite3NameFromToken(pName);
drh97fc3d02002-05-22 21:27:03 +0000780 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000781 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000782 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
783 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000784 sqliteFree(z);
785 return;
786 }
787 }
drh75897232000-05-29 14:26:00 +0000788 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000789 Column *aNew;
790 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
791 if( aNew==0 ) return;
792 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000793 }
drhc9b84a12002-06-20 11:36:48 +0000794 pCol = &p->aCol[p->nCol];
795 memset(pCol, 0, sizeof(p->aCol[0]));
796 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000797
798 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000799 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
800 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000801 */
danielk19774f057f92004-06-08 00:02:33 +0000802 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000803 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000804 p->nCol++;
drh75897232000-05-29 14:26:00 +0000805}
806
807/*
drh382c0242001-10-06 16:33:02 +0000808** This routine is called by the parser while in the middle of
809** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
810** been seen on a column. This routine sets the notNull flag on
811** the column currently under construction.
812*/
danielk19774adee202004-05-08 08:23:19 +0000813void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000814 Table *p;
815 int i;
816 if( (p = pParse->pNewTable)==0 ) return;
817 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000818 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000819}
820
821/*
822** This routine is called by the parser while in the middle of
823** parsing a CREATE TABLE statement. The pFirst token is the first
824** token in the sequence of tokens that describe the type of the
825** column currently under construction. pLast is the last token
826** in the sequence. Use this information to construct a string
827** that contains the typename of the column and store that string
828** in zType.
829*/
danielk19774adee202004-05-08 08:23:19 +0000830void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000831 Table *p;
832 int i, j;
833 int n;
834 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000835 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000836 if( (p = pParse->pNewTable)==0 ) return;
837 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000838 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000839 pCol = &p->aCol[i];
840 pz = &pCol->zType;
drh596bd232004-09-30 14:22:47 +0000841 n = pLast->n + (pLast->z - pFirst->z);
drhae29ffb2004-09-25 14:39:18 +0000842 assert( pCol->zType==0 );
843 z = pCol->zType = sqlite3MPrintf("%.*s", n, pFirst->z);
drhf57b3392001-10-08 13:22:32 +0000844 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000845 for(i=j=0; z[i]; i++){
846 int c = z[i];
847 if( isspace(c) ) continue;
848 z[j++] = c;
849 }
850 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000851 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000852}
853
854/*
danielk19777977a172004-11-09 12:44:37 +0000855** The expression is the default value for the most recently added column
856** of the table currently under construction.
857**
858** Default value expressions must be constant. Raise an exception if this
859** is not the case.
drhd9b02572001-04-15 00:37:09 +0000860**
861** This routine is called by the parser while in the middle of
862** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000863*/
danielk19777977a172004-11-09 12:44:37 +0000864void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){
drh7020f652000-06-03 18:06:52 +0000865 Table *p;
danielk19777977a172004-11-09 12:44:37 +0000866 Column *pCol;
drh7020f652000-06-03 18:06:52 +0000867 if( (p = pParse->pNewTable)==0 ) return;
danielk19777977a172004-11-09 12:44:37 +0000868 pCol = &(p->aCol[p->nCol-1]);
869 if( !sqlite3ExprIsConstant(pExpr) ){
870 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
871 pCol->zName);
872 }else{
873 sqlite3ExprDelete(pCol->pDflt);
874 pCol->pDflt = sqlite3ExprDup(pExpr);
875 sqlite3ExprCheck(pParse, pExpr, 0, 0);
876 }
877 sqlite3ExprDelete(pExpr);
drh7020f652000-06-03 18:06:52 +0000878}
879
880/*
drh4a324312001-12-21 14:30:42 +0000881** Designate the PRIMARY KEY for the table. pList is a list of names
882** of columns that form the primary key. If pList is NULL, then the
883** most recently added column of the table is the primary key.
884**
885** A table can have at most one primary key. If the table already has
886** a primary key (and this is the second primary key) then create an
887** error.
888**
889** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
890** then we will try to use that column as the row id. (Exception:
891** For backwards compatibility with older databases, do not do this
892** if the file format version number is less than 1.) Set the Table.iPKey
893** field of the table under construction to be the index of the
894** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
895** no INTEGER PRIMARY KEY.
896**
897** If the key is not an INTEGER PRIMARY KEY, then create a unique
898** index for the key. No index is created for INTEGER PRIMARY KEYs.
899*/
drh205f48e2004-11-05 00:43:11 +0000900void sqlite3AddPrimaryKey(
901 Parse *pParse, /* Parsing context */
902 ExprList *pList, /* List of field names to be indexed */
903 int onError, /* What to do with a uniqueness conflict */
904 int autoInc /* True if the AUTOINCREMENT keyword is present */
905){
drh4a324312001-12-21 14:30:42 +0000906 Table *pTab = pParse->pNewTable;
907 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000908 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000909 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000910 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000911 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000912 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000913 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000914 }
915 pTab->hasPrimKey = 1;
916 if( pList==0 ){
917 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000918 pTab->aCol[iCol].isPrimKey = 1;
919 }else{
danielk19770202b292004-06-09 09:55:16 +0000920 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000921 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000922 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
923 break;
924 }
drh78100cc2003-08-23 22:40:53 +0000925 }
926 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000927 }
danielk19770202b292004-06-09 09:55:16 +0000928 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000929 }
930 if( iCol>=0 && iCol<pTab->nCol ){
931 zType = pTab->aCol[iCol].zType;
932 }
danielk19773d68f032004-05-11 07:11:51 +0000933 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000934 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000935 pTab->keyConf = onError;
drh205f48e2004-11-05 00:43:11 +0000936 pTab->autoInc = autoInc;
937 }else if( autoInc ){
drh4794f732004-11-05 17:17:50 +0000938#ifndef SQLITE_OMIT_AUTOINCREMENT
drh205f48e2004-11-05 00:43:11 +0000939 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
940 "INTEGER PRIMARY KEY");
drh4794f732004-11-05 17:17:50 +0000941#endif
drh4a324312001-12-21 14:30:42 +0000942 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000943 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000944 pList = 0;
drh4a324312001-12-21 14:30:42 +0000945 }
drhe0194f22003-02-26 13:52:51 +0000946
947primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000948 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000949 return;
drh4a324312001-12-21 14:30:42 +0000950}
951
952/*
drhd3d39e92004-05-20 22:16:29 +0000953** Set the collation function of the most recently parsed table column
954** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000955*/
drhd3d39e92004-05-20 22:16:29 +0000956void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000957 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000958 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000959 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000960 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000961
drhd3d39e92004-05-20 22:16:29 +0000962 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000963 i = p->nCol-1;
964
965 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
966 p->aCol[i].pColl = pColl;
967
968 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
969 ** then an index may have been created on this column before the
970 ** collation type was added. Correct this if it is the case.
971 */
972 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
973 assert( pIdx->nColumn==1 );
974 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000975 }
976}
977
978/*
danielk19770202b292004-06-09 09:55:16 +0000979** Locate and return an entry from the db.aCollSeq hash table. If the entry
980** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +0000981** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +0000982**
danielk1977466be562004-06-10 02:16:01 +0000983** Each pointer stored in the sqlite3.aCollSeq hash table contains an
984** array of three CollSeq structures. The first is the collation sequence
985** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +0000986**
danielk1977466be562004-06-10 02:16:01 +0000987** Stored immediately after the three collation sequences is a copy of
988** the collation sequence name. A pointer to this string is stored in
989** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +0000990*/
danielk1977466be562004-06-10 02:16:01 +0000991static CollSeq * findCollSeqEntry(
drh9bb575f2004-09-06 17:24:11 +0000992 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +0000993 const char *zName,
danielk19770202b292004-06-09 09:55:16 +0000994 int nName,
995 int create
drhd3d39e92004-05-20 22:16:29 +0000996){
997 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000998 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000999 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001000
danielk19770202b292004-06-09 09:55:16 +00001001 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +00001002 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +00001003 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +00001004 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +00001005 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +00001006 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +00001007 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +00001008 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +00001009 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +00001010 memcpy(pColl[0].zName, zName, nName);
1011 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +00001012 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +00001013 }
drhd3d39e92004-05-20 22:16:29 +00001014 }
drhd3d39e92004-05-20 22:16:29 +00001015 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +00001016}
1017
danielk1977466be562004-06-10 02:16:01 +00001018/*
1019** Parameter zName points to a UTF-8 encoded string nName bytes long.
1020** Return the CollSeq* pointer for the collation sequence named zName
1021** for the encoding 'enc' from the database 'db'.
1022**
1023** If the entry specified is not found and 'create' is true, then create a
1024** new entry. Otherwise return NULL.
1025*/
1026CollSeq *sqlite3FindCollSeq(
drh9bb575f2004-09-06 17:24:11 +00001027 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +00001028 u8 enc,
1029 const char *zName,
1030 int nName,
1031 int create
1032){
1033 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
drh6d08b4d2004-07-20 12:45:22 +00001034 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
1035 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
1036 if( pColl ) pColl += enc-1;
danielk1977466be562004-06-10 02:16:01 +00001037 return pColl;
1038}
1039
danielk1977e159fdf2004-06-21 10:45:06 +00001040/*
1041** Invoke the 'collation needed' callback to request a collation sequence
1042** in the database text encoding of name zName, length nName.
1043** If the collation sequence
1044*/
drh9bb575f2004-09-06 17:24:11 +00001045static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
danielk19777cedc8d2004-06-10 10:50:08 +00001046 assert( !db->xCollNeeded || !db->xCollNeeded16 );
1047 if( nName<0 ) nName = strlen(zName);
1048 if( db->xCollNeeded ){
danielk19778a6c5502004-06-22 12:18:32 +00001049 char *zExternal = sqliteStrNDup(zName, nName);
danielk19777cedc8d2004-06-10 10:50:08 +00001050 if( !zExternal ) return;
danielk1977e159fdf2004-06-21 10:45:06 +00001051 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
1052 sqliteFree(zExternal);
danielk19777cedc8d2004-06-10 10:50:08 +00001053 }
1054 if( db->xCollNeeded16 ){
danielk19778a6c5502004-06-22 12:18:32 +00001055 char const *zExternal;
danielk1977bfd6cce2004-06-18 04:24:54 +00001056 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
1057 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
1058 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +00001059 if( !zExternal ) return;
1060 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
1061 }
danielk19777cedc8d2004-06-10 10:50:08 +00001062}
1063
danielk1977e159fdf2004-06-21 10:45:06 +00001064/*
1065** This routine is called if the collation factory fails to deliver a
1066** collation function in the best encoding but there may be other versions
1067** of this collation function (for other text encodings) available. Use one
1068** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
1069** possible.
1070*/
danielk19777cedc8d2004-06-10 10:50:08 +00001071static int synthCollSeq(Parse *pParse, CollSeq *pColl){
drhda71ce12004-06-21 18:14:45 +00001072 CollSeq *pColl2;
danielk19777cedc8d2004-06-10 10:50:08 +00001073 char *z = pColl->zName;
1074 int n = strlen(z);
drh9bb575f2004-09-06 17:24:11 +00001075 sqlite3 *db = pParse->db;
drhda71ce12004-06-21 18:14:45 +00001076 int i;
1077 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
1078 for(i=0; i<3; i++){
1079 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
1080 if( pColl2->xCmp!=0 ){
1081 memcpy(pColl, pColl2, sizeof(CollSeq));
1082 return SQLITE_OK;
danielk19777cedc8d2004-06-10 10:50:08 +00001083 }
danielk19777cedc8d2004-06-10 10:50:08 +00001084 }
drhda71ce12004-06-21 18:14:45 +00001085 if( pParse->nErr==0 ){
drhae29ffb2004-09-25 14:39:18 +00001086 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", n, z);
drhda71ce12004-06-21 18:14:45 +00001087 }
1088 pParse->nErr++;
1089 return SQLITE_ERROR;
danielk19777cedc8d2004-06-10 10:50:08 +00001090}
1091
1092/*
1093** This routine is called on a collation sequence before it is used to
1094** check that it is defined. An undefined collation sequence exists when
1095** a database is loaded that contains references to collation sequences
1096** that have not been defined by sqlite3_create_collation() etc.
1097**
1098** If required, this routine calls the 'collation needed' callback to
1099** request a definition of the collating sequence. If this doesn't work,
1100** an equivalent collating sequence that uses a text encoding different
1101** from the main database is substituted, if one is available.
1102*/
1103int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1104 if( pColl && !pColl->xCmp ){
danielk1977e159fdf2004-06-21 10:45:06 +00001105 /* No collation sequence of this type for this encoding is registered.
1106 ** Call the collation factory to see if it can supply us with one.
1107 */
danielk19777cedc8d2004-06-10 10:50:08 +00001108 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1109 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1110 return SQLITE_ERROR;
1111 }
1112 }
1113 return SQLITE_OK;
1114}
1115
drhda71ce12004-06-21 18:14:45 +00001116/*
1117** Call sqlite3CheckCollSeq() for all collating sequences in an index,
1118** in order to verify that all the necessary collating sequences are
1119** loaded.
1120*/
danielk19777cedc8d2004-06-10 10:50:08 +00001121int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1122 if( pIdx ){
1123 int i;
1124 for(i=0; i<pIdx->nColumn; i++){
1125 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1126 return SQLITE_ERROR;
1127 }
1128 }
1129 }
1130 return SQLITE_OK;
1131}
1132
danielk1977466be562004-06-10 02:16:01 +00001133/*
1134** This function returns the collation sequence for database native text
1135** encoding identified by the string zName, length nName.
1136**
1137** If the requested collation sequence is not available, or not available
1138** in the database native encoding, the collation factory is invoked to
1139** request it. If the collation factory does not supply such a sequence,
1140** and the sequence is available in another text encoding, then that is
1141** returned instead.
1142**
1143** If no versions of the requested collations sequence are available, or
1144** another error occurs, NULL is returned and an error message written into
1145** pParse.
1146*/
danielk19770202b292004-06-09 09:55:16 +00001147CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001148 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001149 u8 initbusy = pParse->db->init.busy;
1150 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
drhae29ffb2004-09-25 14:39:18 +00001151 if( nName<0 ) nName = strlen(zName);
danielk19777cedc8d2004-06-10 10:50:08 +00001152 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001153 /* No collation sequence of this type for this encoding is registered.
1154 ** Call the collation factory to see if it can supply us with one.
1155 */
danielk19777cedc8d2004-06-10 10:50:08 +00001156 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001157 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001158 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001159 /* There may be a version of the collation sequence that requires
1160 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001161 */
danielk19777cedc8d2004-06-10 10:50:08 +00001162 if( synthCollSeq(pParse, pColl) ){
1163 return 0;
danielk1977466be562004-06-10 02:16:01 +00001164 }
1165 }
1166 }
1167
1168 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001169 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001170 if( pParse->nErr==0 ){
drhae29ffb2004-09-25 14:39:18 +00001171 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName);
danielk19770202b292004-06-09 09:55:16 +00001172 }
danielk19777cedc8d2004-06-10 10:50:08 +00001173 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001174 }
1175 return pColl;
1176}
1177
1178
1179
danielk1977a37cdde2004-05-16 11:15:36 +00001180/*
drh1ad3b9e2004-05-20 12:10:20 +00001181** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +00001182** associated affinity type.
1183*/
1184char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +00001185 int n, i;
drh57196282004-10-06 15:41:16 +00001186 static const struct {
drh1ad3b9e2004-05-20 12:10:20 +00001187 const char *zSub; /* Keywords substring to search for */
drhda71ce12004-06-21 18:14:45 +00001188 char nSub; /* length of zSub */
drh1ad3b9e2004-05-20 12:10:20 +00001189 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +00001190 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +00001191 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +00001192 {"CHAR", 4, SQLITE_AFF_TEXT},
1193 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +00001194 {"TEXT", 4, SQLITE_AFF_TEXT},
1195 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +00001196 };
1197
drh9c054832004-05-31 18:51:57 +00001198 if( nType==0 ){
1199 return SQLITE_AFF_NONE;
1200 }
drh1ad3b9e2004-05-20 12:10:20 +00001201 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
1202 int c1 = substrings[i].zSub[0];
1203 int c2 = tolower(c1);
1204 int limit = nType - substrings[i].nSub;
1205 const char *z = substrings[i].zSub;
1206 for(n=0; n<=limit; n++){
1207 int c = zType[n];
1208 if( (c==c1 || c==c2)
1209 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +00001210 return substrings[i].affinity;
1211 }
1212 }
1213 }
drh1ad3b9e2004-05-20 12:10:20 +00001214 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +00001215}
1216
1217/*
drh3f7d4e42004-07-24 14:35:58 +00001218** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001219**
1220** The schema cookie is used to determine when the schema for the
1221** database changes. After each schema change, the cookie value
1222** changes. When a process first reads the schema it records the
1223** cookie. Thereafter, whenever it goes to access the database,
1224** it checks the cookie to make sure the schema has not changed
1225** since it was last read.
1226**
1227** This plan is not completely bullet-proof. It is possible for
1228** the schema to change multiple times and for the cookie to be
1229** set back to prior value. But schema changes are infrequent
1230** and the probability of hitting the same cookie value is only
1231** 1 chance in 2^32. So we're safe enough.
1232*/
drh9bb575f2004-09-06 17:24:11 +00001233void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){
drh3f7d4e42004-07-24 14:35:58 +00001234 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].schema_cookie+1, 0);
danielk19771d850a72004-05-31 08:26:49 +00001235 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001236}
1237
1238/*
drh969fa7c2002-02-18 18:30:32 +00001239** Measure the number of characters needed to output the given
1240** identifier. The number returned includes any quotes used
1241** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001242**
1243** The estimate is conservative. It might be larger that what is
1244** really needed.
drh969fa7c2002-02-18 18:30:32 +00001245*/
1246static int identLength(const char *z){
1247 int n;
drh17f71932002-02-21 12:01:27 +00001248 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001249 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001250 }
drh234c39d2004-07-24 03:30:47 +00001251 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001252}
1253
1254/*
1255** Write an identifier onto the end of the given string. Add
1256** quote characters as needed.
1257*/
drh4c755c02004-08-08 20:22:17 +00001258static void identPut(char *z, int *pIdx, char *zSignedIdent){
1259 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001260 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001261 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001262 for(j=0; zIdent[j]; j++){
1263 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1264 }
1265 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001266 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh234c39d2004-07-24 03:30:47 +00001267 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001268 for(j=0; zIdent[j]; j++){
1269 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001270 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001271 }
drh234c39d2004-07-24 03:30:47 +00001272 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001273 z[i] = 0;
1274 *pIdx = i;
1275}
1276
1277/*
1278** Generate a CREATE TABLE statement appropriate for the given
1279** table. Memory to hold the text of the statement is obtained
1280** from sqliteMalloc() and must be freed by the calling function.
1281*/
1282static char *createTableStmt(Table *p){
1283 int i, k, n;
1284 char *zStmt;
drh234c39d2004-07-24 03:30:47 +00001285 char *zSep, *zSep2, *zEnd, *z;
1286 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001287 n = 0;
drh234c39d2004-07-24 03:30:47 +00001288 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1289 n += identLength(pCol->zName);
1290 z = pCol->zType;
1291 if( z ){
1292 n += (strlen(z) + 1);
danielk1977517eb642004-06-07 10:00:31 +00001293 }
drh969fa7c2002-02-18 18:30:32 +00001294 }
1295 n += identLength(p->zName);
drh234c39d2004-07-24 03:30:47 +00001296 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001297 zSep = "";
1298 zSep2 = ",";
1299 zEnd = ")";
1300 }else{
1301 zSep = "\n ";
1302 zSep2 = ",\n ";
1303 zEnd = "\n)";
1304 }
drhe0bc4042002-06-25 01:09:11 +00001305 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001306 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001307 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001308 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001309 k = strlen(zStmt);
1310 identPut(zStmt, &k, p->zName);
1311 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001312 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drh969fa7c2002-02-18 18:30:32 +00001313 strcpy(&zStmt[k], zSep);
1314 k += strlen(&zStmt[k]);
1315 zSep = zSep2;
drh234c39d2004-07-24 03:30:47 +00001316 identPut(zStmt, &k, pCol->zName);
1317 if( (z = pCol->zType)!=0 ){
danielk1977517eb642004-06-07 10:00:31 +00001318 zStmt[k++] = ' ';
drh234c39d2004-07-24 03:30:47 +00001319 strcpy(&zStmt[k], z);
1320 k += strlen(z);
danielk1977517eb642004-06-07 10:00:31 +00001321 }
drh969fa7c2002-02-18 18:30:32 +00001322 }
1323 strcpy(&zStmt[k], zEnd);
1324 return zStmt;
1325}
1326
1327/*
drh75897232000-05-29 14:26:00 +00001328** This routine is called to report the final ")" that terminates
1329** a CREATE TABLE statement.
1330**
drhf57b3392001-10-08 13:22:32 +00001331** The table structure that other action routines have been building
1332** is added to the internal hash tables, assuming no errors have
1333** occurred.
drh75897232000-05-29 14:26:00 +00001334**
drh1d85d932004-02-14 23:05:52 +00001335** An entry for the table is made in the master table on disk, unless
1336** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001337** it means we are reading the sqlite_master table because we just
1338** connected to the database or because the sqlite_master table has
1339** recently changes, so the entry for this table already exists in
1340** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001341**
1342** If the pSelect argument is not NULL, it means that this routine
1343** was called to create a table generated from a
1344** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1345** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001346*/
danielk19774adee202004-05-08 08:23:19 +00001347void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001348 Table *p;
drh9bb575f2004-09-06 17:24:11 +00001349 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001350
danielk197724b03fd2004-05-10 10:34:34 +00001351 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001352 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001353 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001354
danielk1977517eb642004-06-07 10:00:31 +00001355 assert( !db->init.busy || !pSelect );
1356
drh1d85d932004-02-14 23:05:52 +00001357 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001358 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1359 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001360 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001361 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001362 */
drh1d85d932004-02-14 23:05:52 +00001363 if( db->init.busy ){
1364 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001365 }
1366
drhe3c41372001-09-17 20:25:58 +00001367 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001368 ** in the SQLITE_MASTER table of the database. The record number
1369 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001370 **
drhe0bc4042002-06-25 01:09:11 +00001371 ** If this is a TEMPORARY table, write the entry into the auxiliary
1372 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001373 */
drh1d85d932004-02-14 23:05:52 +00001374 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001375 int n;
drhd8bc7082000-06-07 23:51:50 +00001376 Vdbe *v;
drh4794f732004-11-05 17:17:50 +00001377 char *zType; /* "view" or "table" */
1378 char *zType2; /* "VIEW" or "TABLE" */
1379 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
drh75897232000-05-29 14:26:00 +00001380
danielk19774adee202004-05-08 08:23:19 +00001381 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001382 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001383
danielk1977e6efa742004-11-10 11:55:10 +00001384 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1385
drh4794f732004-11-05 17:17:50 +00001386 /* Create the rootpage for the new table and push it onto the stack.
1387 ** A view has no rootpage, so just push a zero onto the stack for
1388 ** views. Initialize zType at the same time.
1389 */
drh4ff6dfa2002-03-03 23:06:00 +00001390 if( p->pSelect==0 ){
1391 /* A regular table */
drh234c39d2004-07-24 03:30:47 +00001392 sqlite3VdbeAddOp(v, OP_CreateTable, p->iDb, 0);
drh4794f732004-11-05 17:17:50 +00001393 zType = "table";
1394 zType2 = "TABLE";
drh4ff6dfa2002-03-03 23:06:00 +00001395 }else{
1396 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4794f732004-11-05 17:17:50 +00001398 zType = "view";
1399 zType2 = "VIEW";
drh4ff6dfa2002-03-03 23:06:00 +00001400 }
danielk1977517eb642004-06-07 10:00:31 +00001401
danielk1977517eb642004-06-07 10:00:31 +00001402 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1403 ** statement to populate the new table. The root-page number for the
1404 ** new table is on the top of the vdbe stack.
1405 **
1406 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1407 ** suitable state to query for the column names and types to be used
1408 ** by the new table.
1409 */
1410 if( pSelect ){
1411 Table *pSelTab;
1412 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1413 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1414 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1415 pParse->nTab = 2;
1416 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1417 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1418 if( pParse->nErr==0 ){
1419 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1420 if( pSelTab==0 ) return;
1421 assert( p->aCol==0 );
1422 p->nCol = pSelTab->nCol;
1423 p->aCol = pSelTab->aCol;
1424 pSelTab->nCol = 0;
1425 pSelTab->aCol = 0;
1426 sqlite3DeleteTable(0, pSelTab);
1427 }
1428 }
drh4794f732004-11-05 17:17:50 +00001429
drh4794f732004-11-05 17:17:50 +00001430 /* Compute the complete text of the CREATE statement */
1431 if( pSelect ){
1432 zStmt = createTableStmt(p);
1433 }else{
1434 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
1435 zStmt = sqlite3MPrintf("CREATE %s %.*s", zType2, n, pParse->sNameToken.z);
1436 }
1437
1438 /* A slot for the record has already been allocated in the
1439 ** SQLITE_MASTER table. We just need to update that slot with all
1440 ** the information we've collected. The rowid for the preallocated
1441 ** slot is the 2nd item on the stack. The top of the stack is the
1442 ** root page for the new table (or a 0 if this is a view).
1443 */
1444 sqlite3NestedParse(pParse,
1445 "UPDATE %Q.%s "
1446 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#0, sql=%Q "
1447 "WHERE rowid=#1",
1448 db->aDb[p->iDb].zName, SCHEMA_TABLE(p->iDb),
1449 zType,
1450 p->zName,
1451 p->zName,
1452 zStmt
1453 );
1454 sqliteFree(zStmt);
1455
1456 /* Reparse everything to update our internal data structures */
1457 sqlite3ChangeCookie(db, v, p->iDb);
drh234c39d2004-07-24 03:30:47 +00001458 sqlite3VdbeOp3(v, OP_ParseSchema, p->iDb, 0,
1459 sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC);
drh75897232000-05-29 14:26:00 +00001460 }
drh17e9e292003-02-01 13:53:28 +00001461
1462 /* Add the table to the in-memory representation of the database.
1463 */
drh234c39d2004-07-24 03:30:47 +00001464 if( db->init.busy && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001465 Table *pOld;
drhbe5c89a2004-07-26 00:31:09 +00001466 FKey *pFKey;
1467 Db *pDb = &db->aDb[p->iDb];
1468 pOld = sqlite3HashInsert(&pDb->tblHash, p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001469 if( pOld ){
1470 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1471 return;
1472 }
1473 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1474 int nTo = strlen(pFKey->zTo) + 1;
drhbe5c89a2004-07-26 00:31:09 +00001475 pFKey->pNextTo = sqlite3HashFind(&pDb->aFKey, pFKey->zTo, nTo);
1476 sqlite3HashInsert(&pDb->aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001477 }
1478 pParse->pNewTable = 0;
1479 db->nTable++;
1480 db->flags |= SQLITE_InternChanges;
1481 }
drh75897232000-05-29 14:26:00 +00001482}
1483
drhb7f91642004-10-31 02:22:47 +00001484#ifndef SQLITE_OMIT_VIEW
drh75897232000-05-29 14:26:00 +00001485/*
drha76b5df2002-02-23 02:32:10 +00001486** The parser calls this routine in order to create a new VIEW
1487*/
danielk19774adee202004-05-08 08:23:19 +00001488void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001489 Parse *pParse, /* The parsing context */
1490 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001491 Token *pName1, /* The token that holds the name of the view */
1492 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001493 Select *pSelect, /* A SELECT statement that will become the new view */
1494 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001495){
drha76b5df2002-02-23 02:32:10 +00001496 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001497 int n;
drh4c755c02004-08-08 20:22:17 +00001498 const unsigned char *z;
drh4b59ab52002-08-24 18:24:51 +00001499 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001500 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001501 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001502
danielk197748dec7e2004-05-28 12:33:30 +00001503 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001504 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001505 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001506 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001507 return;
1508 }
danielk1977ef2cb632004-05-29 02:37:19 +00001509 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001510 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1511 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001512 ){
danielk19774adee202004-05-08 08:23:19 +00001513 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001514 return;
1515 }
drh174b6192002-12-03 02:22:52 +00001516
drh4b59ab52002-08-24 18:24:51 +00001517 /* Make a copy of the entire SELECT statement that defines the view.
1518 ** This will force all the Expr.token.z values to be dynamically
1519 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001520 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001521 */
danielk19774adee202004-05-08 08:23:19 +00001522 p->pSelect = sqlite3SelectDup(pSelect);
1523 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001524 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001525 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001526 }
drh4b59ab52002-08-24 18:24:51 +00001527
1528 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1529 ** the end.
1530 */
drha76b5df2002-02-23 02:32:10 +00001531 sEnd = pParse->sLastToken;
1532 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1533 sEnd.z += sEnd.n;
1534 }
1535 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001536 n = sEnd.z - pBegin->z;
drh4c755c02004-08-08 20:22:17 +00001537 z = (const unsigned char*)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001538 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1539 sEnd.z = &z[n-1];
1540 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001541
danielk19774adee202004-05-08 08:23:19 +00001542 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1543 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001544 return;
drh417be792002-03-03 18:59:40 +00001545}
drhb7f91642004-10-31 02:22:47 +00001546#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001547
drhb7f91642004-10-31 02:22:47 +00001548#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001549/*
1550** The Table structure pTable is really a VIEW. Fill in the names of
1551** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001552** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001553*/
danielk19774adee202004-05-08 08:23:19 +00001554int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001555 ExprList *pEList;
1556 Select *pSel;
1557 Table *pSelTab;
1558 int nErr = 0;
1559
1560 assert( pTable );
1561
1562 /* A positive nCol means the columns names for this view are
1563 ** already known.
1564 */
1565 if( pTable->nCol>0 ) return 0;
1566
1567 /* A negative nCol is a special marker meaning that we are currently
1568 ** trying to compute the column names. If we enter this routine with
1569 ** a negative nCol, it means two or more views form a loop, like this:
1570 **
1571 ** CREATE VIEW one AS SELECT * FROM two;
1572 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001573 **
1574 ** Actually, this error is caught previously and so the following test
1575 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001576 */
1577 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001578 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001579 return 1;
1580 }
1581
1582 /* If we get this far, it means we need to compute the table names.
1583 */
1584 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1585 pSel = pTable->pSelect;
1586
danielk19774adee202004-05-08 08:23:19 +00001587 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001588 ** "*" elements in this list. But we will need to restore the list
1589 ** back to its original configuration afterwards, so we save a copy of
1590 ** the original in pEList.
1591 */
1592 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001593 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001594 if( pSel->pEList==0 ){
1595 pSel->pEList = pEList;
1596 return 1; /* Malloc failed */
1597 }
1598 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001599 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001600 if( pSelTab ){
1601 assert( pTable->aCol==0 );
1602 pTable->nCol = pSelTab->nCol;
1603 pTable->aCol = pSelTab->aCol;
1604 pSelTab->nCol = 0;
1605 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001606 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001607 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001608 }else{
1609 pTable->nCol = 0;
1610 nErr++;
1611 }
danielk19774adee202004-05-08 08:23:19 +00001612 sqlite3SelectUnbind(pSel);
1613 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001614 pSel->pEList = pEList;
1615 return nErr;
1616}
drhb7f91642004-10-31 02:22:47 +00001617#endif /* SQLITE_OMIT_VIEW */
drh417be792002-03-03 18:59:40 +00001618
drhb7f91642004-10-31 02:22:47 +00001619#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001620/*
drh8bf8dc92003-05-17 17:35:10 +00001621** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001622*/
drh9bb575f2004-09-06 17:24:11 +00001623static void sqliteViewResetAll(sqlite3 *db, int idx){
drh417be792002-03-03 18:59:40 +00001624 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001625 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001626 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001627 Table *pTab = sqliteHashData(i);
1628 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001629 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001630 }
1631 }
drh8bf8dc92003-05-17 17:35:10 +00001632 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001633}
drhb7f91642004-10-31 02:22:47 +00001634#else
1635# define sqliteViewResetAll(A,B)
1636#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001637
drh75897232000-05-29 14:26:00 +00001638/*
danielk1977a0bf2652004-11-04 14:30:04 +00001639** This function is called by the VDBE to adjust the internal schema
1640** used by SQLite when the btree layer moves a table root page. The
1641** root-page of a table or index in database iDb has changed from iFrom
1642** to iTo.
1643*/
1644#ifndef SQLITE_OMIT_AUTOVACUUM
1645void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
1646 HashElem *pElem;
1647
1648 for(pElem=sqliteHashFirst(&pDb->tblHash); pElem; pElem=sqliteHashNext(pElem)){
1649 Table *pTab = sqliteHashData(pElem);
1650 if( pTab->tnum==iFrom ){
1651 pTab->tnum = iTo;
1652 return;
1653 }
1654 }
1655 for(pElem=sqliteHashFirst(&pDb->idxHash); pElem; pElem=sqliteHashNext(pElem)){
1656 Index *pIdx = sqliteHashData(pElem);
1657 if( pIdx->tnum==iFrom ){
1658 pIdx->tnum = iTo;
1659 return;
1660 }
1661 }
1662 assert(0);
1663}
1664#endif
1665
1666/*
1667** Write code to erase the table with root-page iTable from database iDb.
1668** Also write code to modify the sqlite_master table and internal schema
1669** if a root-page of another table is moved by the btree-layer whilst
1670** erasing iTable (this can happen with an auto-vacuum database).
1671*/
drh4e0cff62004-11-05 05:10:28 +00001672static void destroyRootPage(Parse *pParse, int iTable, int iDb){
1673 Vdbe *v = sqlite3GetVdbe(pParse);
drh40e016e2004-11-04 14:47:11 +00001674 sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb);
1675#ifndef SQLITE_OMIT_AUTOVACUUM
drh4e0cff62004-11-05 05:10:28 +00001676 /* OP_Destroy pushes an integer onto the stack. If this integer
1677 ** is non-zero, then it is the root page number of a table moved to
1678 ** location iTable. The following code modifis the sqlite_master table to
1679 ** reflect this.
1680 **
1681 ** The "#0" in the SQL is a special constant that means whatever value
1682 ** is on the top of the stack. See sqlite3RegisterExpr().
1683 */
danielk197763e3e9f2004-11-05 09:19:27 +00001684 sqlite3NestedParse(pParse,
drh4794f732004-11-05 17:17:50 +00001685 "UPDATE %Q.%s SET rootpage=%d WHERE #0 AND rootpage=#0",
drh4e0cff62004-11-05 05:10:28 +00001686 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable);
danielk1977a0bf2652004-11-04 14:30:04 +00001687#endif
1688}
1689
1690/*
1691** Write VDBE code to erase table pTab and all associated indices on disk.
1692** Code to update the sqlite_master tables and internal schema definitions
1693** in case a root-page belonging to another table is moved by the btree layer
1694** is also added (this can happen with an auto-vacuum database).
1695*/
drh4e0cff62004-11-05 05:10:28 +00001696static void destroyTable(Parse *pParse, Table *pTab){
danielk1977a0bf2652004-11-04 14:30:04 +00001697#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001698 Index *pIdx;
drh4e0cff62004-11-05 05:10:28 +00001699 destroyRootPage(pParse, pTab->tnum, pTab->iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001700 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh7a638582004-11-05 05:23:59 +00001701 destroyRootPage(pParse, pIdx->tnum, pIdx->iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001702 }
1703#else
1704 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1705 ** is not defined), then it is important to call OP_Destroy on the
1706 ** table and index root-pages in order, starting with the numerically
1707 ** largest root-page number. This guarantees that none of the root-pages
1708 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1709 ** following were coded:
1710 **
1711 ** OP_Destroy 4 0
1712 ** ...
1713 ** OP_Destroy 5 0
1714 **
1715 ** and root page 5 happened to be the largest root-page number in the
1716 ** database, then root page 5 would be moved to page 4 by the
1717 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1718 ** a free-list page.
1719 */
1720 int iTab = pTab->tnum;
1721 int iDestroyed = 0;
1722
1723 while( 1 ){
1724 Index *pIdx;
1725 int iLargest = 0;
1726
1727 if( iDestroyed==0 || iTab<iDestroyed ){
1728 iLargest = iTab;
1729 }
1730 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1731 int iIdx = pIdx->tnum;
1732 assert( pIdx->iDb==pTab->iDb );
1733 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
1734 iLargest = iIdx;
1735 }
1736 }
1737 if( iLargest==0 ) return;
drh4e0cff62004-11-05 05:10:28 +00001738 destroyRootPage(pParse, iLargest, pTab->iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001739 iDestroyed = iLargest;
1740 }
1741#endif
1742}
1743
1744/*
drh75897232000-05-29 14:26:00 +00001745** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001746** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001747*/
danielk1977a8858102004-05-28 12:11:21 +00001748void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1749 Table *pTab;
drh75897232000-05-29 14:26:00 +00001750 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00001751 sqlite3 *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001752 int iDb;
drh75897232000-05-29 14:26:00 +00001753
danielk1977a8858102004-05-28 12:11:21 +00001754 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1755 assert( pName->nSrc==1 );
1756 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1757
1758 if( pTab==0 ) goto exit_drop_table;
1759 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001760 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001761#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001762 {
1763 int code;
danielk1977a8858102004-05-28 12:11:21 +00001764 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1765 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001766 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001767 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001768 }
drhe5f9c642003-01-13 23:27:31 +00001769 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001770 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001771 code = SQLITE_DROP_TEMP_VIEW;
1772 }else{
1773 code = SQLITE_DROP_VIEW;
1774 }
1775 }else{
drhd24cc422003-03-27 12:51:24 +00001776 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001777 code = SQLITE_DROP_TEMP_TABLE;
1778 }else{
1779 code = SQLITE_DROP_TABLE;
1780 }
1781 }
danielk1977a8858102004-05-28 12:11:21 +00001782 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1783 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001784 }
danielk1977a8858102004-05-28 12:11:21 +00001785 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1786 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001787 }
drhe5f9c642003-01-13 23:27:31 +00001788 }
1789#endif
danielk1977a8858102004-05-28 12:11:21 +00001790 if( pTab->readOnly ){
1791 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001792 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001793 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001794 }
danielk1977a8858102004-05-28 12:11:21 +00001795 if( isView && pTab->pSelect==0 ){
1796 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1797 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001798 }
danielk1977a8858102004-05-28 12:11:21 +00001799 if( !isView && pTab->pSelect ){
1800 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1801 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001802 }
drh75897232000-05-29 14:26:00 +00001803
drh1ccde152000-06-17 13:12:39 +00001804 /* Generate code to remove the table from the master table
1805 ** on disk.
1806 */
danielk19774adee202004-05-08 08:23:19 +00001807 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001808 if( v ){
drhe0bc4042002-06-25 01:09:11 +00001809 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001810 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001811
danielk19778e227872004-06-07 07:52:17 +00001812 /* Drop all triggers associated with the table being dropped. Code
1813 ** is generated to remove entries from sqlite_master and/or
1814 ** sqlite_temp_master if required.
1815 */
danielk1977a8858102004-05-28 12:11:21 +00001816 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001817 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001818 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001819 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drh956bc922004-07-24 17:38:29 +00001820 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00001821 }
drh8bf8dc92003-05-17 17:35:10 +00001822
danielk19778e227872004-06-07 07:52:17 +00001823 /* Drop all SQLITE_MASTER table and index entries that refer to the
1824 ** table. The program name loops through the master table and deletes
1825 ** every row that refers to a table of the same name as the one being
1826 ** dropped. Triggers are handled seperately because a trigger can be
1827 ** created in the temp database that refers to a table in another
1828 ** database.
1829 */
drhf1974842004-11-05 03:56:00 +00001830 sqlite3NestedParse(pParse,
drh4794f732004-11-05 17:17:50 +00001831 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
drhf1974842004-11-05 03:56:00 +00001832 db->aDb[pTab->iDb].zName, SCHEMA_TABLE(pTab->iDb), pTab->zName);
drh4ff6dfa2002-03-03 23:06:00 +00001833 if( !isView ){
drh4e0cff62004-11-05 05:10:28 +00001834 destroyTable(pParse, pTab);
drh5e00f6c2001-09-13 13:46:56 +00001835 }
drh956bc922004-07-24 17:38:29 +00001836 sqlite3VdbeOp3(v, OP_DropTable, pTab->iDb, 0, pTab->zName, 0);
drh75897232000-05-29 14:26:00 +00001837 }
drhd24cc422003-03-27 12:51:24 +00001838 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001839
1840exit_drop_table:
1841 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001842}
1843
1844/*
drhc2eef3b2002-08-31 18:53:06 +00001845** This routine is called to create a new foreign key on the table
1846** currently under construction. pFromCol determines which columns
1847** in the current table point to the foreign key. If pFromCol==0 then
1848** connect the key to the last column inserted. pTo is the name of
1849** the table referred to. pToCol is a list of tables in the other
1850** pTo table that the foreign key points to. flags contains all
1851** information about the conflict resolution algorithms specified
1852** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1853**
1854** An FKey structure is created and added to the table currently
1855** under construction in the pParse->pNewTable field. The new FKey
1856** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001857** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001858**
1859** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001860** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001861*/
danielk19774adee202004-05-08 08:23:19 +00001862void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001863 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001864 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001865 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001866 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001867 int flags /* Conflict resolution algorithms. */
1868){
drhb7f91642004-10-31 02:22:47 +00001869#ifndef SQLITE_OMIT_FOREIGN_KEY
drh40e016e2004-11-04 14:47:11 +00001870 FKey *pFKey = 0;
drhc2eef3b2002-08-31 18:53:06 +00001871 Table *p = pParse->pNewTable;
1872 int nByte;
1873 int i;
1874 int nCol;
1875 char *z;
drhc2eef3b2002-08-31 18:53:06 +00001876
1877 assert( pTo!=0 );
1878 if( p==0 || pParse->nErr ) goto fk_end;
1879 if( pFromCol==0 ){
1880 int iCol = p->nCol-1;
1881 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001882 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001883 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001884 " should reference only one column of table %T",
1885 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001886 goto fk_end;
1887 }
1888 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001889 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001890 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001891 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001892 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001893 goto fk_end;
1894 }else{
danielk19770202b292004-06-09 09:55:16 +00001895 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001896 }
1897 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1898 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001899 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001900 nByte += strlen(pToCol->a[i].zName) + 1;
1901 }
1902 }
1903 pFKey = sqliteMalloc( nByte );
1904 if( pFKey==0 ) goto fk_end;
1905 pFKey->pFrom = p;
1906 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001907 z = (char*)&pFKey[1];
1908 pFKey->aCol = (struct sColMap*)z;
1909 z += sizeof(struct sColMap)*nCol;
1910 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001911 memcpy(z, pTo->z, pTo->n);
1912 z[pTo->n] = 0;
1913 z += pTo->n+1;
1914 pFKey->pNextTo = 0;
1915 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001916 if( pFromCol==0 ){
1917 pFKey->aCol[0].iFrom = p->nCol-1;
1918 }else{
1919 for(i=0; i<nCol; i++){
1920 int j;
1921 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001922 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001923 pFKey->aCol[i].iFrom = j;
1924 break;
1925 }
1926 }
1927 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001928 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001929 "unknown column \"%s\" in foreign key definition",
1930 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001931 goto fk_end;
1932 }
1933 }
1934 }
1935 if( pToCol ){
1936 for(i=0; i<nCol; i++){
1937 int n = strlen(pToCol->a[i].zName);
1938 pFKey->aCol[i].zCol = z;
1939 memcpy(z, pToCol->a[i].zName, n);
1940 z[n] = 0;
1941 z += n+1;
1942 }
1943 }
1944 pFKey->isDeferred = 0;
1945 pFKey->deleteConf = flags & 0xff;
1946 pFKey->updateConf = (flags >> 8 ) & 0xff;
1947 pFKey->insertConf = (flags >> 16 ) & 0xff;
1948
1949 /* Link the foreign key to the table as the last step.
1950 */
1951 p->pFKey = pFKey;
1952 pFKey = 0;
1953
1954fk_end:
1955 sqliteFree(pFKey);
drhb7f91642004-10-31 02:22:47 +00001956#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
danielk19770202b292004-06-09 09:55:16 +00001957 sqlite3ExprListDelete(pFromCol);
1958 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001959}
1960
1961/*
1962** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1963** clause is seen as part of a foreign key definition. The isDeferred
1964** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1965** The behavior of the most recently created foreign key is adjusted
1966** accordingly.
1967*/
danielk19774adee202004-05-08 08:23:19 +00001968void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhb7f91642004-10-31 02:22:47 +00001969#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:06 +00001970 Table *pTab;
1971 FKey *pFKey;
1972 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1973 pFKey->isDeferred = isDeferred;
drhb7f91642004-10-31 02:22:47 +00001974#endif
drhc2eef3b2002-08-31 18:53:06 +00001975}
1976
1977/*
drh063336a2004-11-05 20:58:39 +00001978** Generate code that will erase and refill index *pIdx. This is
1979** used to initialize a newly created index or to recompute the
1980** content of an index in response to a REINDEX command.
1981**
1982** if memRootPage is not negative, it means that the index is newly
1983** created. The memory cell specified by memRootPage contains the
1984** root page number of the index. If memRootPage is negative, then
1985** the index already exists and must be cleared before being refilled and
1986** the root page number of the index is taken from pIndex->tnum.
1987*/
1988static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
1989 Table *pTab = pIndex->pTable; /* The table that is indexed */
1990 int iTab = pParse->nTab; /* Btree cursor used for pTab */
1991 int iIdx = pParse->nTab+1; /* Btree cursor used for pIndex */
1992 int addr1; /* Address of top of loop */
1993 int tnum; /* Root page of index */
1994 Vdbe *v; /* Generate code into this virtual machine */
drh4343fea2004-11-05 23:46:15 +00001995 int isUnique; /* True for a unique index */
drh063336a2004-11-05 20:58:39 +00001996
1997 v = sqlite3GetVdbe(pParse);
1998 if( v==0 ) return;
1999 if( memRootPage>=0 ){
2000 sqlite3VdbeAddOp(v, OP_MemLoad, memRootPage, 0);
2001 tnum = 0;
2002 }else{
2003 tnum = pIndex->tnum;
2004 sqlite3VdbeAddOp(v, OP_Clear, tnum, pIndex->iDb);
2005 }
2006 sqlite3VdbeAddOp(v, OP_Integer, pIndex->iDb, 0);
2007 sqlite3VdbeOp3(v, OP_OpenWrite, iIdx, tnum,
2008 (char*)&pIndex->keyInfo, P3_KEYINFO);
2009 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2010 sqlite3VdbeAddOp(v, OP_OpenRead, iTab, pTab->tnum);
2011 sqlite3VdbeAddOp(v, OP_SetNumColumns, iTab, pTab->nCol);
2012 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0);
2013 sqlite3GenerateIndexKey(v, pIndex, iTab);
drh4343fea2004-11-05 23:46:15 +00002014 isUnique = pIndex->onError!=OE_None;
2015 sqlite3VdbeAddOp(v, OP_IdxPut, iIdx, isUnique);
2016 if( isUnique ){
2017 sqlite3VdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
2018 }
drh063336a2004-11-05 20:58:39 +00002019 sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1);
2020 sqlite3VdbeChangeP2(v, addr1, sqlite3VdbeCurrentAddr(v));
2021 sqlite3VdbeAddOp(v, OP_Close, iTab, 0);
2022 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
2023}
2024
2025/*
drh75897232000-05-29 14:26:00 +00002026** Create a new index for an SQL table. pIndex is the name of the index
2027** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00002028** be NULL for a primary key or an index that is created to satisfy a
2029** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00002030** as the table to be indexed. pParse->pNewTable is a table that is
2031** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00002032**
drh382c0242001-10-06 16:33:02 +00002033** pList is a list of columns to be indexed. pList will be NULL if this
2034** is a primary key or unique-constraint on the most recent column added
2035** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00002036*/
danielk19774adee202004-05-08 08:23:19 +00002037void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00002038 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00002039 Token *pName1, /* First part of index name. May be NULL */
2040 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00002041 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
2042 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00002043 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00002044 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
2045 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
2046){
danielk1977cbb18d22004-05-28 11:37:27 +00002047 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00002048 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00002049 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00002050 int i, j;
drhf26e09c2003-05-31 16:21:12 +00002051 Token nullId; /* Fake token for an empty ID list */
2052 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00002053 int isTemp; /* True for a temporary index */
drh9bb575f2004-09-06 17:24:11 +00002054 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002055
danielk1977cbb18d22004-05-28 11:37:27 +00002056 int iDb; /* Index of the database that is being written */
2057 Token *pName = 0; /* Unqualified name of the index to create */
2058
danielk197724b03fd2004-05-10 10:34:34 +00002059 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00002060
drh75897232000-05-29 14:26:00 +00002061 /*
2062 ** Find the table that is to be indexed. Return early if not found.
2063 */
danielk1977cbb18d22004-05-28 11:37:27 +00002064 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002065
2066 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00002067 ** to search for the table. 'Fix' the table name to this db
2068 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00002069 */
2070 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00002071 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00002072 if( iDb<0 ) goto exit_create_index;
2073
danielk1977ef2cb632004-05-29 02:37:19 +00002074 /* If the index name was unqualified, check if the the table
2075 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00002076 */
danielk1977ef2cb632004-05-29 02:37:19 +00002077 pTab = sqlite3SrcListLookup(pParse, pTblName);
2078 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
2079 iDb = 1;
2080 }
2081
2082 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
2083 sqlite3FixSrcList(&sFix, pTblName)
2084 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002085 goto exit_create_index;
2086 }
danielk1977ef2cb632004-05-29 02:37:19 +00002087 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
2088 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00002089 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00002090 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00002091 }else{
drhe3c41372001-09-17 20:25:58 +00002092 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00002093 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00002094 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00002095 }
danielk1977cbb18d22004-05-28 11:37:27 +00002096
drh75897232000-05-29 14:26:00 +00002097 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00002098 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00002099 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00002100 goto exit_create_index;
2101 }
drha76b5df2002-02-23 02:32:10 +00002102 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00002103 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00002104 goto exit_create_index;
2105 }
drh4925ca02003-11-27 00:48:57 +00002106 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00002107
2108 /*
2109 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00002110 ** index or table with the same name.
2111 **
2112 ** Exception: If we are reading the names of permanent indices from the
2113 ** sqlite_master table (because some other process changed the schema) and
2114 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00002115 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00002116 **
2117 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00002118 ** dealing with a primary key or UNIQUE constraint. We have to invent our
2119 ** own name.
drh75897232000-05-29 14:26:00 +00002120 */
danielk1977d8123362004-06-12 09:25:12 +00002121 if( pName ){
drha99db3b2004-06-19 14:49:12 +00002122 zName = sqlite3NameFromToken(pName);
danielk19778a414492004-06-29 08:59:35 +00002123 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002124 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002125 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00002126 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002127 }
danielk1977d8123362004-06-12 09:25:12 +00002128 if( !db->init.busy ){
2129 Index *pISameName; /* Another index with the same name */
2130 Table *pTSameName; /* A table with same name as the index */
danielk19778a414492004-06-29 08:59:35 +00002131 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002132 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
2133 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
2134 goto exit_create_index;
2135 }
2136 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
2137 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
2138 goto exit_create_index;
2139 }
drhe3c41372001-09-17 20:25:58 +00002140 }
drhd24cc422003-03-27 12:51:24 +00002141 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00002142 char zBuf[30];
2143 int n;
2144 Index *pLoop;
2145 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00002146 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00002147 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00002148 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00002149 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00002150 }
2151
drhe5f9c642003-01-13 23:27:31 +00002152 /* Check for authorization to create an index.
2153 */
2154#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00002155 {
2156 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00002157 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002158 goto exit_create_index;
2159 }
2160 i = SQLITE_CREATE_INDEX;
2161 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002162 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002163 goto exit_create_index;
2164 }
drhe5f9c642003-01-13 23:27:31 +00002165 }
2166#endif
2167
drh75897232000-05-29 14:26:00 +00002168 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00002169 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00002170 ** So create a fake list to simulate this.
2171 */
2172 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00002173 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00002174 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00002175 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00002176 if( pList==0 ) goto exit_create_index;
2177 }
2178
2179 /*
2180 ** Allocate the index structure.
2181 */
drhdcc581c2000-05-30 13:44:19 +00002182 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00002183 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00002184 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00002185 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
2186 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00002187 strcpy(pIndex->zName, zName);
2188 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00002189 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00002190 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00002191 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00002192 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00002193
drh1ccde152000-06-17 13:12:39 +00002194 /* Scan the names of the columns of the table to be indexed and
2195 ** load the column indices into the Index structure. Report an error
2196 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00002197 */
danielk19770202b292004-06-09 09:55:16 +00002198 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00002199 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002200 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002201 }
2202 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002203 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00002204 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002205 goto exit_create_index;
2206 }
drh967e8b72000-06-21 13:59:10 +00002207 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00002208 if( pList->a[i].pExpr ){
2209 assert( pList->a[i].pExpr->pColl );
2210 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
2211 }else{
2212 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
2213 }
2214 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00002215 if( !db->init.busy &&
2216 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2217 ){
2218 goto exit_create_index;
2219 }
drh75897232000-05-29 14:26:00 +00002220 }
danielk19770202b292004-06-09 09:55:16 +00002221 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002222
danielk1977d8123362004-06-12 09:25:12 +00002223 if( pTab==pParse->pNewTable ){
2224 /* This routine has been called to create an automatic index as a
2225 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2226 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2227 ** i.e. one of:
2228 **
2229 ** CREATE TABLE t(x PRIMARY KEY, y);
2230 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2231 **
2232 ** Either way, check to see if the table already has such an index. If
2233 ** so, don't bother creating this one. This only applies to
2234 ** automatically created indices. Users can do as they wish with
2235 ** explicit indices.
2236 */
2237 Index *pIdx;
2238 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2239 int k;
2240 assert( pIdx->onError!=OE_None );
2241 assert( pIdx->autoIndex );
2242 assert( pIndex->onError!=OE_None );
2243
2244 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2245 for(k=0; k<pIdx->nColumn; k++){
2246 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2247 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2248 }
2249 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002250 if( pIdx->onError!=pIndex->onError ){
2251 /* This constraint creates the same index as a previous
2252 ** constraint specified somewhere in the CREATE TABLE statement.
2253 ** However the ON CONFLICT clauses are different. If both this
2254 ** constraint and the previous equivalent constraint have explicit
2255 ** ON CONFLICT clauses this is an error. Otherwise, use the
2256 ** explicitly specified behaviour for the index.
2257 */
2258 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2259 sqlite3ErrorMsg(pParse,
2260 "conflicting ON CONFLICT clauses specified", 0);
2261 }
2262 if( pIdx->onError==OE_Default ){
2263 pIdx->onError = pIndex->onError;
2264 }
2265 }
danielk1977d8123362004-06-12 09:25:12 +00002266 goto exit_create_index;
2267 }
2268 }
2269 }
2270
drh75897232000-05-29 14:26:00 +00002271 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002272 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002273 */
drh234c39d2004-07-24 03:30:47 +00002274 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002275 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002276 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002277 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002278 if( p ){
2279 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002280 goto exit_create_index;
2281 }
drh5e00f6c2001-09-13 13:46:56 +00002282 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002283 if( pTblName!=0 ){
2284 pIndex->tnum = db->init.newTnum;
2285 }
drhd78eeee2001-09-13 16:18:53 +00002286 }
2287
drh1d85d932004-02-14 23:05:52 +00002288 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002289 ** involves writing the index into the master table and filling in the
2290 ** index with the current table contents.
2291 **
drh1d85d932004-02-14 23:05:52 +00002292 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2293 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002294 ** CREATE INDEX statements are read out of the master table. In
2295 ** the latter case the index already exists on disk, which is why
2296 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002297 **
danielk1977cbb18d22004-05-28 11:37:27 +00002298 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002299 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2300 ** has just been created, it contains no data and the index initialization
2301 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002302 */
drh1d85d932004-02-14 23:05:52 +00002303 else if( db->init.busy==0 ){
drhadbca9c2001-09-27 15:11:53 +00002304 Vdbe *v;
drh063336a2004-11-05 20:58:39 +00002305 char *zStmt;
2306 int iMem = pParse->nMem++;
drh75897232000-05-29 14:26:00 +00002307
danielk19774adee202004-05-08 08:23:19 +00002308 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002309 if( v==0 ) goto exit_create_index;
drh063336a2004-11-05 20:58:39 +00002310
2311 /* Create the rootpage for the index
2312 */
2313 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh234c39d2004-07-24 03:30:47 +00002314 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
drh063336a2004-11-05 20:58:39 +00002315 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2316
2317 /* Gather the complete text of the CREATE INDEX statement into
2318 ** the zStmt variable
2319 */
drhe0bc4042002-06-25 01:09:11 +00002320 if( pStart && pEnd ){
drh063336a2004-11-05 20:58:39 +00002321 /* A named index with an explicit CREATE INDEX statement */
2322 zStmt = sqlite3MPrintf("CREATE%s INDEX %.*q",
2323 onError==OE_None ? "" : " UNIQUE",
2324 Addr(pEnd->z) - Addr(pName->z) + 1,
2325 pName->z);
2326 }else{
2327 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
drhe497f002004-11-07 13:01:49 +00002328 /* zStmt = sqlite3MPrintf(""); */
2329 zStmt = 0;
drh75897232000-05-29 14:26:00 +00002330 }
drh063336a2004-11-05 20:58:39 +00002331
2332 /* Add an entry in sqlite_master for this index
2333 */
2334 sqlite3NestedParse(pParse,
drhe497f002004-11-07 13:01:49 +00002335 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#0,%Q);",
drh063336a2004-11-05 20:58:39 +00002336 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2337 pIndex->zName,
2338 pTab->zName,
2339 zStmt
2340 );
2341 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2342 sqliteFree(zStmt);
2343
2344 /* Fill the index with data and reparse the schema
2345 */
danielk1977cbb18d22004-05-28 11:37:27 +00002346 if( pTblName ){
drh063336a2004-11-05 20:58:39 +00002347 sqlite3RefillIndex(pParse, pIndex, iMem);
drhc275b4e2004-07-19 17:25:24 +00002348 sqlite3ChangeCookie(db, v, iDb);
drh234c39d2004-07-24 03:30:47 +00002349 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
2350 sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC);
drh5e00f6c2001-09-13 13:46:56 +00002351 }
drh75897232000-05-29 14:26:00 +00002352 }
2353
danielk1977d8123362004-06-12 09:25:12 +00002354 /* When adding an index to the list of indices for a table, make
2355 ** sure all indices labeled OE_Replace come after all those labeled
2356 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2357 ** and INSERT.
2358 */
drh234c39d2004-07-24 03:30:47 +00002359 if( db->init.busy || pTblName==0 ){
2360 if( onError!=OE_Replace || pTab->pIndex==0
2361 || pTab->pIndex->onError==OE_Replace){
2362 pIndex->pNext = pTab->pIndex;
2363 pTab->pIndex = pIndex;
2364 }else{
2365 Index *pOther = pTab->pIndex;
2366 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2367 pOther = pOther->pNext;
2368 }
2369 pIndex->pNext = pOther->pNext;
2370 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002371 }
drh234c39d2004-07-24 03:30:47 +00002372 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002373 }
danielk1977d8123362004-06-12 09:25:12 +00002374
drh75897232000-05-29 14:26:00 +00002375 /* Clean up before exiting */
2376exit_create_index:
drh956bc922004-07-24 17:38:29 +00002377 if( pIndex ){
2378 freeIndex(pIndex);
2379 }
danielk19770202b292004-06-09 09:55:16 +00002380 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002381 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002382 sqliteFree(zName);
2383 return;
2384}
2385
2386/*
drh74e24cd2002-01-09 03:19:59 +00002387** This routine will drop an existing named index. This routine
2388** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002389*/
danielk19774adee202004-05-08 08:23:19 +00002390void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002391 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002392 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00002393 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002394
danielk197724b03fd2004-05-10 10:34:34 +00002395 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002396 assert( pName->nSrc==1 );
danielk19778a414492004-06-29 08:59:35 +00002397 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19774adee202004-05-08 08:23:19 +00002398 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002399 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002400 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002401 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002402 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002403 }
drh485b39b2002-07-13 03:11:52 +00002404 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002405 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002406 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002407 goto exit_drop_index;
2408 }
drhe5f9c642003-01-13 23:27:31 +00002409#ifndef SQLITE_OMIT_AUTHORIZATION
2410 {
2411 int code = SQLITE_DROP_INDEX;
2412 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002413 const char *zDb = db->aDb[pIndex->iDb].zName;
2414 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002415 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002416 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002417 }
drhd24cc422003-03-27 12:51:24 +00002418 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002419 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002420 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002421 }
drhed6c8672003-01-12 18:02:16 +00002422 }
drhe5f9c642003-01-13 23:27:31 +00002423#endif
drh75897232000-05-29 14:26:00 +00002424
2425 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002426 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002427 if( v ){
drhb17131a2004-11-05 22:18:49 +00002428 int iDb = pIndex->iDb;
2429 sqlite3NestedParse(pParse,
2430 "DELETE FROM %Q.%s WHERE name=%Q",
2431 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2432 pIndex->zName
2433 );
2434 sqlite3ChangeCookie(db, v, iDb);
2435 destroyRootPage(pParse, pIndex->tnum, iDb);
2436 sqlite3VdbeOp3(v, OP_DropIndex, iDb, 0, pIndex->zName, 0);
drh75897232000-05-29 14:26:00 +00002437 }
2438
drhd24cc422003-03-27 12:51:24 +00002439exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002440 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002441}
2442
2443/*
drh75897232000-05-29 14:26:00 +00002444** Append a new element to the given IdList. Create a new IdList if
2445** need be.
drhdaffd0e2001-04-11 14:28:42 +00002446**
2447** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002448*/
danielk19774adee202004-05-08 08:23:19 +00002449IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002450 if( pList==0 ){
2451 pList = sqliteMalloc( sizeof(IdList) );
2452 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002453 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002454 }
drh4305d102003-07-30 12:34:12 +00002455 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002456 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002457 pList->nAlloc = pList->nAlloc*2 + 5;
2458 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002459 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002460 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002461 return 0;
drh75897232000-05-29 14:26:00 +00002462 }
drh6d4abfb2001-10-22 02:58:08 +00002463 pList->a = a;
drh75897232000-05-29 14:26:00 +00002464 }
2465 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
drha99db3b2004-06-19 14:49:12 +00002466 pList->a[pList->nId].zName = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002467 pList->nId++;
2468 return pList;
2469}
2470
2471/*
drhad3cab52002-05-24 02:04:32 +00002472** Append a new table name to the given SrcList. Create a new SrcList if
2473** need be. A new entry is created in the SrcList even if pToken is NULL.
2474**
2475** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002476**
2477** If pDatabase is not null, it means that the table has an optional
2478** database name prefix. Like this: "database.table". The pDatabase
2479** points to the table name and the pTable points to the database name.
2480** The SrcList.a[].zName field is filled with the table name which might
2481** come from pTable (if pDatabase is NULL) or from pDatabase.
2482** SrcList.a[].zDatabase is filled with the database name from pTable,
2483** or with NULL if no database is specified.
2484**
2485** In other words, if call like this:
2486**
danielk19774adee202004-05-08 08:23:19 +00002487** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002488**
2489** Then B is a table name and the database name is unspecified. If called
2490** like this:
2491**
danielk19774adee202004-05-08 08:23:19 +00002492** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002493**
2494** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002495*/
danielk19774adee202004-05-08 08:23:19 +00002496SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drha99db3b2004-06-19 14:49:12 +00002497 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002498 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002499 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002500 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002501 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002502 }
drh4305d102003-07-30 12:34:12 +00002503 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002504 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002505 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002506 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002507 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002508 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002509 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002510 return 0;
2511 }
drh113088e2003-03-20 01:16:58 +00002512 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002513 }
drha99db3b2004-06-19 14:49:12 +00002514 pItem = &pList->a[pList->nSrc];
2515 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002516 if( pDatabase && pDatabase->z==0 ){
2517 pDatabase = 0;
2518 }
2519 if( pDatabase && pTable ){
2520 Token *pTemp = pDatabase;
2521 pDatabase = pTable;
2522 pTable = pTemp;
2523 }
drha99db3b2004-06-19 14:49:12 +00002524 pItem->zName = sqlite3NameFromToken(pTable);
2525 pItem->zDatabase = sqlite3NameFromToken(pDatabase);
2526 pItem->iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002527 pList->nSrc++;
2528 return pList;
2529}
2530
2531/*
drh63eb5f22003-04-29 16:20:44 +00002532** Assign cursors to all tables in a SrcList
2533*/
danielk19774adee202004-05-08 08:23:19 +00002534void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002535 int i;
2536 for(i=0; i<pList->nSrc; i++){
2537 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002538 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002539 }
2540 }
2541}
2542
2543/*
drh75897232000-05-29 14:26:00 +00002544** Add an alias to the last identifier on the given identifier list.
2545*/
danielk19774adee202004-05-08 08:23:19 +00002546void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002547 if( pList && pList->nSrc>0 ){
drha99db3b2004-06-19 14:49:12 +00002548 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002549 }
2550}
2551
2552/*
drhad3cab52002-05-24 02:04:32 +00002553** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002554*/
danielk19774adee202004-05-08 08:23:19 +00002555void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002556 int i;
2557 if( pList==0 ) return;
2558 for(i=0; i<pList->nId; i++){
2559 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002560 }
2561 sqliteFree(pList->a);
2562 sqliteFree(pList);
2563}
2564
2565/*
drhad2d8302002-05-24 20:31:36 +00002566** Return the index in pList of the identifier named zId. Return -1
2567** if not found.
2568*/
danielk19774adee202004-05-08 08:23:19 +00002569int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002570 int i;
2571 if( pList==0 ) return -1;
2572 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002573 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002574 }
2575 return -1;
2576}
2577
2578/*
drhad3cab52002-05-24 02:04:32 +00002579** Delete an entire SrcList including all its substructure.
2580*/
danielk19774adee202004-05-08 08:23:19 +00002581void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002582 int i;
drhbe5c89a2004-07-26 00:31:09 +00002583 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002584 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00002585 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
2586 sqliteFree(pItem->zDatabase);
2587 sqliteFree(pItem->zName);
2588 sqliteFree(pItem->zAlias);
2589 if( pItem->pTab && pItem->pTab->isTransient ){
2590 sqlite3DeleteTable(0, pItem->pTab);
drhdaffd0e2001-04-11 14:28:42 +00002591 }
drhbe5c89a2004-07-26 00:31:09 +00002592 sqlite3SelectDelete(pItem->pSelect);
2593 sqlite3ExprDelete(pItem->pOn);
2594 sqlite3IdListDelete(pItem->pUsing);
drh75897232000-05-29 14:26:00 +00002595 }
drh75897232000-05-29 14:26:00 +00002596 sqliteFree(pList);
2597}
2598
drh982cef72000-05-30 16:27:03 +00002599/*
drhc4a3c772001-04-04 11:48:57 +00002600** Begin a transaction
2601*/
drh684917c2004-10-05 02:41:42 +00002602void sqlite3BeginTransaction(Parse *pParse, int type){
drh9bb575f2004-09-06 17:24:11 +00002603 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002604 Vdbe *v;
drh684917c2004-10-05 02:41:42 +00002605 int i;
drh5e00f6c2001-09-13 13:46:56 +00002606
drh001bbcb2003-03-19 03:14:00 +00002607 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002608 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002609 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002610
2611 v = sqlite3GetVdbe(pParse);
2612 if( !v ) return;
drh684917c2004-10-05 02:41:42 +00002613 if( type!=TK_DEFERRED ){
2614 for(i=0; i<db->nDb; i++){
2615 sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
2616 }
2617 }
danielk19771d850a72004-05-31 08:26:49 +00002618 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002619}
2620
2621/*
2622** Commit a transaction
2623*/
danielk19774adee202004-05-08 08:23:19 +00002624void sqlite3CommitTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002625 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002626 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002627
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, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002631
2632 v = sqlite3GetVdbe(pParse);
2633 if( v ){
2634 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002635 }
drhc4a3c772001-04-04 11:48:57 +00002636}
2637
2638/*
2639** Rollback a transaction
2640*/
danielk19774adee202004-05-08 08:23:19 +00002641void sqlite3RollbackTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002642 sqlite3 *db;
drh5e00f6c2001-09-13 13:46:56 +00002643 Vdbe *v;
2644
drh001bbcb2003-03-19 03:14:00 +00002645 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002646 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002647 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002648
danielk19774adee202004-05-08 08:23:19 +00002649 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002650 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002651 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002652 }
drhc4a3c772001-04-04 11:48:57 +00002653}
drhf57b14a2001-09-14 18:54:08 +00002654
2655/*
drhdc3ff9c2004-08-18 02:10:15 +00002656** Make sure the TEMP database is open and available for use. Return
2657** the number of errors. Leave any error messages in the pParse structure.
2658*/
2659static int sqlite3OpenTempDatabase(Parse *pParse){
2660 sqlite3 *db = pParse->db;
2661 if( db->aDb[1].pBt==0 && !pParse->explain ){
2662 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
2663 if( rc!=SQLITE_OK ){
2664 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
2665 "file for storing temporary tables");
2666 pParse->rc = rc;
2667 return 1;
2668 }
2669 if( db->flags & !db->autoCommit ){
2670 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
2671 if( rc!=SQLITE_OK ){
2672 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
2673 "the temporary database file");
2674 pParse->rc = rc;
2675 return 1;
2676 }
2677 }
2678 }
2679 return 0;
2680}
2681
2682/*
drh80242052004-06-09 00:48:12 +00002683** Generate VDBE code that will verify the schema cookie and start
2684** a read-transaction for all named database files.
2685**
2686** It is important that all schema cookies be verified and all
2687** read transactions be started before anything else happens in
2688** the VDBE program. But this routine can be called after much other
2689** code has been generated. So here is what we do:
2690**
drhc275b4e2004-07-19 17:25:24 +00002691** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00002692** will jump to a subroutine at the end of the program. Then we
2693** record every database that needs its schema verified in the
2694** pParse->cookieMask field. Later, after all other code has been
2695** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00002696** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00002697** will be made to point to that subroutine. The generation of the
2698** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00002699**
2700** If iDb<0 then code the OP_Goto only - don't set flag to verify the
2701** schema on any databases. This can be used to position the OP_Goto
2702** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00002703*/
danielk19774adee202004-05-08 08:23:19 +00002704void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh9bb575f2004-09-06 17:24:11 +00002705 sqlite3 *db;
drh80242052004-06-09 00:48:12 +00002706 Vdbe *v;
2707 int mask;
2708
2709 v = sqlite3GetVdbe(pParse);
2710 if( v==0 ) return; /* This only happens if there was a prior error */
2711 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00002712 if( pParse->cookieGoto==0 ){
2713 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00002714 }
drhc275b4e2004-07-19 17:25:24 +00002715 if( iDb>=0 ){
2716 assert( iDb<db->nDb );
2717 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2718 assert( iDb<32 );
2719 mask = 1<<iDb;
2720 if( (pParse->cookieMask & mask)==0 ){
2721 pParse->cookieMask |= mask;
2722 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
drhdc3ff9c2004-08-18 02:10:15 +00002723 if( iDb==1 ){
2724 sqlite3OpenTempDatabase(pParse);
2725 }
drhc275b4e2004-07-19 17:25:24 +00002726 }
drh001bbcb2003-03-19 03:14:00 +00002727 }
drh001bbcb2003-03-19 03:14:00 +00002728}
2729
2730/*
drh1c928532002-01-31 15:54:21 +00002731** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002732** might change the database.
2733**
2734** This routine starts a new transaction if we are not already within
2735** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002736** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002737** be set for operations that might fail (due to a constraint) part of
2738** the way through and which will need to undo some writes without having to
2739** rollback the whole transaction. For operations where all constraints
2740** can be checked before any changes are made to the database, it is never
2741** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002742**
drh8bf8dc92003-05-17 17:35:10 +00002743** Only database iDb and the temp database are made writable by this call.
2744** If iDb==0, then the main and temp databases are made writable. If
2745** iDb==1 then only the temp database is made writable. If iDb>1 then the
2746** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002747*/
drh7f0f12e2004-05-21 13:39:50 +00002748void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002749 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002750 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002751 sqlite3CodeVerifySchema(pParse, iDb);
2752 pParse->writeMask |= 1<<iDb;
danielk197763e3e9f2004-11-05 09:19:27 +00002753 if( setStatement && pParse->nested==0 ){
drh7f0f12e2004-05-21 13:39:50 +00002754 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002755 }
drh34f47322004-08-18 15:58:22 +00002756 if( iDb!=1 && pParse->db->aDb[1].pBt!=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002757 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002758 }
2759}
2760
danielk1977bfd6cce2004-06-18 04:24:54 +00002761/*
2762** Return the transient sqlite3_value object used for encoding conversions
2763** during SQL compilation.
2764*/
drh9bb575f2004-09-06 17:24:11 +00002765sqlite3_value *sqlite3GetTransientValue(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +00002766 if( !db->pValue ){
2767 db->pValue = sqlite3ValueNew();
2768 }
2769 return db->pValue;
2770}
drh4343fea2004-11-05 23:46:15 +00002771
2772/*
2773** Check to see if pIndex uses the collating sequence pColl. Return
2774** true if it does and false if it does not.
2775*/
2776#ifndef SQLITE_OMIT_REINDEX
2777static int collationMatch(CollSeq *pColl, Index *pIndex){
2778 int n = pIndex->keyInfo.nField;
2779 CollSeq **pp = pIndex->keyInfo.aColl;
2780 while( n-- ){
2781 if( *pp==pColl ) return 1;
2782 pp++;
2783 }
2784 return 0;
2785}
2786#endif
2787
2788/*
2789** Recompute all indices of pTab that use the collating sequence pColl.
2790** If pColl==0 then recompute all indices of pTab.
2791*/
2792#ifndef SQLITE_OMIT_REINDEX
2793void reindexTable(Parse *pParse, Table *pTab, CollSeq *pColl){
2794 Index *pIndex; /* An index associated with pTab */
2795
2796 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
2797 if( pColl==0 || collationMatch(pColl,pIndex) ){
2798 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
2799 sqlite3RefillIndex(pParse, pIndex, -1);
2800 }
2801 }
2802}
2803#endif
2804
2805/*
2806** Recompute all indices of all tables in all databases where the
2807** indices use the collating sequence pColl. If pColl==0 then recompute
2808** all indices everywhere.
2809*/
2810#ifndef SQLITE_OMIT_REINDEX
2811void reindexDatabases(Parse *pParse, CollSeq *pColl){
2812 Db *pDb; /* A single database */
2813 int iDb; /* The database index number */
2814 sqlite3 *db = pParse->db; /* The database connection */
2815 HashElem *k; /* For looping over tables in pDb */
2816 Table *pTab; /* A table in the database */
2817
2818 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
2819 if( pDb==0 ) continue;
2820 for(k=sqliteHashFirst(&pDb->tblHash); k; k=sqliteHashNext(k)){
2821 pTab = (Table*)sqliteHashData(k);
2822 reindexTable(pParse, pTab, pColl);
2823 }
2824 }
2825}
2826#endif
2827
2828/*
drheee46cf2004-11-06 00:02:48 +00002829** Generate code for the REINDEX command.
2830**
2831** REINDEX -- 1
2832** REINDEX <collation> -- 2
2833** REINDEX ?<database>.?<tablename> -- 3
2834** REINDEX ?<database>.?<indexname> -- 4
2835**
2836** Form 1 causes all indices in all attached databases to be rebuilt.
2837** Form 2 rebuilds all indices in all databases that use the named
2838** collating function. Forms 3 and 4 rebuild the named index or all
2839** indices associated with the named table.
drh4343fea2004-11-05 23:46:15 +00002840*/
2841#ifndef SQLITE_OMIT_REINDEX
2842void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
2843 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
2844 char *z; /* Name of a table or index */
2845 const char *zDb; /* Name of the database */
2846 Table *pTab; /* A table in the database */
2847 Index *pIndex; /* An index associated with pTab */
2848 int iDb; /* The database index number */
2849 sqlite3 *db = pParse->db; /* The database connection */
2850 Token *pObjName; /* Name of the table or index to be reindexed */
2851
drhe497f002004-11-07 13:01:49 +00002852 if( pName1==0 || pName1->z==0 ){
drh4343fea2004-11-05 23:46:15 +00002853 reindexDatabases(pParse, 0);
2854 return;
drhe497f002004-11-07 13:01:49 +00002855 }else if( pName2==0 || pName2->z==0 ){
drh4343fea2004-11-05 23:46:15 +00002856 pColl = sqlite3FindCollSeq(db, db->enc, pName1->z, pName1->n, 0);
2857 if( pColl ){
2858 reindexDatabases(pParse, pColl);
2859 return;
2860 }
2861 }
2862 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
2863 if( iDb<0 ) return;
2864 z = sqlite3NameFromToken(pObjName);
2865 zDb = db->aDb[iDb].zName;
2866 pTab = sqlite3FindTable(db, z, zDb);
2867 if( pTab ){
2868 reindexTable(pParse, pTab, 0);
2869 sqliteFree(z);
2870 return;
2871 }
2872 pIndex = sqlite3FindIndex(db, z, zDb);
2873 sqliteFree(z);
2874 if( pIndex ){
2875 sqlite3BeginWriteOperation(pParse, 0, iDb);
2876 sqlite3RefillIndex(pParse, pIndex, -1);
2877 return;
2878 }
2879 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
2880}
2881#endif