blob: 147f5f09047a09cd8f8971994cdd213fdc0bcc9f [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**
danielk1977201f7162005-02-01 02:13:29 +000025** $Id: build.c,v 1.306 2005/02/01 02:13:29 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
drh23bf66d2004-12-14 03:34:34 +000032** be parsed. Initialize the pParse structure as needed.
drhe0bc4042002-06-25 01:09:11 +000033*/
danielk19774adee202004-05-08 08:23:19 +000034void sqlite3BeginParse(Parse *pParse, int explainFlag){
drhe0bc4042002-06-25 01:09:11 +000035 pParse->explain = explainFlag;
drh7c972de2003-09-06 22:18:07 +000036 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000037}
38
39/*
drh75897232000-05-29 14:26:00 +000040** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12 +000041** parsed and a VDBE program to execute that statement has been
42** prepared. This routine puts the finishing touches on the
43** VDBE program and resets the pParse structure for the next
44** parse.
drh75897232000-05-29 14:26:00 +000045**
46** Note that if an error occurred, it might be the case that
47** no VDBE code was generated.
48*/
drh80242052004-06-09 00:48:12 +000049void sqlite3FinishCoding(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +000050 sqlite3 *db;
drh80242052004-06-09 00:48:12 +000051 Vdbe *v;
drhb86ccfb2003-01-28 23:13:10 +000052
danielk197724b03fd2004-05-10 10:34:34 +000053 if( sqlite3_malloc_failed ) return;
drh205f48e2004-11-05 00:43:11 +000054 if( pParse->nested ) return;
drh80242052004-06-09 00:48:12 +000055
56 /* Begin by generating some termination code at the end of the
57 ** vdbe program
58 */
59 db = pParse->db;
60 v = sqlite3GetVdbe(pParse);
61 if( v ){
62 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
drh0e3d7472004-06-19 17:33:07 +000063
64 /* The cookie mask contains one bit for each database file open.
65 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
66 ** set for each database that is used. Generate code to start a
67 ** transaction on each used database and to verify the schema cookie
68 ** on each used database.
69 */
drhc275b4e2004-07-19 17:25:24 +000070 if( pParse->cookieGoto>0 ){
drh80242052004-06-09 00:48:12 +000071 u32 mask;
72 int iDb;
drhc275b4e2004-07-19 17:25:24 +000073 sqlite3VdbeChangeP2(v, pParse->cookieGoto-1, sqlite3VdbeCurrentAddr(v));
drh80242052004-06-09 00:48:12 +000074 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
75 if( (mask & pParse->cookieMask)==0 ) continue;
76 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
drhc275b4e2004-07-19 17:25:24 +000077 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
drh80242052004-06-09 00:48:12 +000078 }
drhc275b4e2004-07-19 17:25:24 +000079 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto);
drh80242052004-06-09 00:48:12 +000080 }
drh80242052004-06-09 00:48:12 +000081
drh71c697e2004-08-08 23:39:19 +000082 /* Add a No-op that contains the complete text of the compiled SQL
83 ** statement as its P3 argument. This does not change the functionality
drhc16a03b2004-09-15 13:38:10 +000084 ** of the program.
85 **
drh23bf66d2004-12-14 03:34:34 +000086 ** This is used to implement sqlite3_trace().
drh71c697e2004-08-08 23:39:19 +000087 */
88 sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql);
drh71c697e2004-08-08 23:39:19 +000089 }
90
drh3f7d4e42004-07-24 14:35:58 +000091
drh80242052004-06-09 00:48:12 +000092 /* Get the VDBE program ready for execution
93 */
drhb86ccfb2003-01-28 23:13:10 +000094 if( v && pParse->nErr==0 ){
95 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +000096 sqlite3VdbeTrace(v, trace);
drh290c1942004-08-21 17:54:45 +000097 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3,
danielk1977b3bce662005-01-29 08:32:43 +000098 pParse->nTab+3, pParse->nMaxDepth+1, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +000099 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000100 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +0000101 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +0000102 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000103 }
drha226d052002-09-25 19:04:07 +0000104 pParse->nTab = 0;
105 pParse->nMem = 0;
106 pParse->nSet = 0;
drh7c972de2003-09-06 22:18:07 +0000107 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000108 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000109 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000110}
111
112/*
drh205f48e2004-11-05 00:43:11 +0000113** Run the parser and code generator recursively in order to generate
114** code for the SQL statement given onto the end of the pParse context
115** currently under construction. When the parser is run recursively
116** this way, the final OP_Halt is not appended and other initialization
117** and finalization steps are omitted because those are handling by the
118** outermost parser.
119**
120** Not everything is nestable. This facility is designed to permit
121** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use
drhf1974842004-11-05 03:56:00 +0000122** care if you decide to try to use this routine for some other purposes.
drh205f48e2004-11-05 00:43:11 +0000123*/
124void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
125 va_list ap;
126 char *zSql;
127 int rc;
drhf1974842004-11-05 03:56:00 +0000128# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar))
129 char saveBuf[SAVE_SZ];
130
drh205f48e2004-11-05 00:43:11 +0000131 if( pParse->nErr ) return;
132 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
133 va_start(ap, zFormat);
134 zSql = sqlite3VMPrintf(zFormat, ap);
135 va_end(ap);
drh73c42a12004-11-20 18:13:10 +0000136 if( zSql==0 ){
137 return; /* A malloc must have failed */
138 }
drh205f48e2004-11-05 00:43:11 +0000139 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);
drh2958a4e2004-11-12 03:56:15 +0000324 pDb->pSeqTab = 0;
drh8bf8dc92003-05-17 17:35:10 +0000325 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000326 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000327 }
drh1c2d8412003-03-31 00:30:47 +0000328 assert( iDb==0 );
329 db->flags &= ~SQLITE_InternChanges;
330
331 /* If one or more of the auxiliary database files has been closed,
332 ** then remove then from the auxiliary database list. We take the
333 ** opportunity to do this here since we have just deleted all of the
334 ** schema hash tables and therefore do not have to make any changes
335 ** to any of those tables.
336 */
drh4d189ca2004-02-12 18:46:38 +0000337 for(i=0; i<db->nDb; i++){
338 struct Db *pDb = &db->aDb[i];
339 if( pDb->pBt==0 ){
340 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
341 pDb->pAux = 0;
342 }
343 }
drh1c2d8412003-03-31 00:30:47 +0000344 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000345 struct Db *pDb = &db->aDb[i];
346 if( pDb->pBt==0 ){
347 sqliteFree(pDb->zName);
348 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000349 continue;
350 }
351 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000352 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000353 }
drh8bf8dc92003-05-17 17:35:10 +0000354 j++;
drh1c2d8412003-03-31 00:30:47 +0000355 }
356 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
357 db->nDb = j;
358 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
359 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
360 sqliteFree(db->aDb);
361 db->aDb = db->aDbStatic;
362 }
drhe0bc4042002-06-25 01:09:11 +0000363}
364
365/*
366** This routine is called whenever a rollback occurs. If there were
367** schema changes during the transaction, then we have to reset the
368** internal hash tables and reload them from disk.
369*/
drh9bb575f2004-09-06 17:24:11 +0000370void sqlite3RollbackInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000371 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000372 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000373 }
374}
375
376/*
377** This routine is called when a commit occurs.
378*/
drh9bb575f2004-09-06 17:24:11 +0000379void sqlite3CommitInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000380 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000381}
382
383/*
drh956bc922004-07-24 17:38:29 +0000384** Clear the column names from a table or view.
385*/
386static void sqliteResetColumnNames(Table *pTable){
387 int i;
388 Column *pCol;
389 assert( pTable!=0 );
390 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
391 sqliteFree(pCol->zName);
danielk19777977a172004-11-09 12:44:37 +0000392 sqlite3ExprDelete(pCol->pDflt);
drh956bc922004-07-24 17:38:29 +0000393 sqliteFree(pCol->zType);
394 }
395 sqliteFree(pTable->aCol);
396 pTable->aCol = 0;
397 pTable->nCol = 0;
398}
399
400/*
drh75897232000-05-29 14:26:00 +0000401** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000402** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000403**
404** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000405** the table data structure from the hash table. Nor does it remove
406** foreign keys from the sqlite.aFKey hash table. But it does destroy
407** memory structures of the indices and foreign keys associated with
408** the table.
drhdaffd0e2001-04-11 14:28:42 +0000409**
410** Indices associated with the table are unlinked from the "db"
411** data structure if db!=NULL. If db==NULL, indices attached to
412** the table are deleted, but it is assumed they have already been
413** unlinked.
drh75897232000-05-29 14:26:00 +0000414*/
drh9bb575f2004-09-06 17:24:11 +0000415void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000416 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000417 FKey *pFKey, *pNextFKey;
418
drh75897232000-05-29 14:26:00 +0000419 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000420
421 /* Delete all indices associated with this table
422 */
423 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
424 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000425 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000426 sqliteDeleteIndex(db, pIndex);
427 }
428
danielk1977576ec6b2005-01-21 11:55:25 +0000429#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:06 +0000430 /* Delete all foreign keys associated with this table. The keys
431 ** should have already been unlinked from the db->aFKey hash table
432 */
433 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
434 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000435 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000436 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000437 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000438 sqliteFree(pFKey);
439 }
danielk1977576ec6b2005-01-21 11:55:25 +0000440#endif
drhc2eef3b2002-08-31 18:53:06 +0000441
442 /* Delete the Table structure itself.
443 */
drh956bc922004-07-24 17:38:29 +0000444 sqliteResetColumnNames(pTable);
drh6e142f52000-06-08 13:36:40 +0000445 sqliteFree(pTable->zName);
drh956bc922004-07-24 17:38:29 +0000446 sqliteFree(pTable->zColAff);
danielk19774adee202004-05-08 08:23:19 +0000447 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000448 sqliteFree(pTable);
449}
450
451/*
drh5edc3122001-09-13 21:53:09 +0000452** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000453** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000454*/
drh9bb575f2004-09-06 17:24:11 +0000455void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
drh956bc922004-07-24 17:38:29 +0000456 Table *p;
drhc2eef3b2002-08-31 18:53:06 +0000457 FKey *pF1, *pF2;
drh956bc922004-07-24 17:38:29 +0000458 Db *pDb;
459
drhd229ca92002-01-09 13:30:41 +0000460 assert( db!=0 );
drh956bc922004-07-24 17:38:29 +0000461 assert( iDb>=0 && iDb<db->nDb );
462 assert( zTabName && zTabName[0] );
463 pDb = &db->aDb[iDb];
464 p = sqlite3HashInsert(&pDb->tblHash, zTabName, strlen(zTabName)+1, 0);
465 if( p ){
danielk1977576ec6b2005-01-21 11:55:25 +0000466#ifndef SQLITE_OMIT_FOREIGN_KEY
drh956bc922004-07-24 17:38:29 +0000467 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
468 int nTo = strlen(pF1->zTo) + 1;
469 pF2 = sqlite3HashFind(&pDb->aFKey, pF1->zTo, nTo);
470 if( pF2==pF1 ){
471 sqlite3HashInsert(&pDb->aFKey, pF1->zTo, nTo, pF1->pNextTo);
472 }else{
473 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
474 if( pF2 ){
475 pF2->pNextTo = pF1->pNextTo;
476 }
drhc2eef3b2002-08-31 18:53:06 +0000477 }
478 }
danielk1977576ec6b2005-01-21 11:55:25 +0000479#endif
drh956bc922004-07-24 17:38:29 +0000480 sqlite3DeleteTable(db, p);
drhc2eef3b2002-08-31 18:53:06 +0000481 }
drh956bc922004-07-24 17:38:29 +0000482 db->flags |= SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000483}
484
485/*
drha99db3b2004-06-19 14:49:12 +0000486** Given a token, return a string that consists of the text of that
487** token with any quotations removed. Space to hold the returned string
488** is obtained from sqliteMalloc() and must be freed by the calling
489** function.
drh75897232000-05-29 14:26:00 +0000490**
drha99db3b2004-06-19 14:49:12 +0000491** Tokens are really just pointers into the original SQL text and so
492** are not \000 terminated and are not persistent. The returned string
493** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000494*/
drha99db3b2004-06-19 14:49:12 +0000495char *sqlite3NameFromToken(Token *pName){
496 char *zName;
497 if( pName ){
498 zName = sqliteStrNDup(pName->z, pName->n);
499 sqlite3Dequote(zName);
500 }else{
501 zName = 0;
502 }
drh75897232000-05-29 14:26:00 +0000503 return zName;
504}
505
506/*
danielk1977cbb18d22004-05-28 11:37:27 +0000507** Open the sqlite_master table stored in database number iDb for
508** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000509*/
danielk1977cbb18d22004-05-28 11:37:27 +0000510void sqlite3OpenMasterTable(Vdbe *v, int iDb){
511 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000512 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000513 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000514}
515
516/*
danielk1977cbb18d22004-05-28 11:37:27 +0000517** The token *pName contains the name of a database (either "main" or
518** "temp" or the name of an attached db). This routine returns the
519** index of the named database in db->aDb[], or -1 if the named db
520** does not exist.
521*/
drh23bf66d2004-12-14 03:34:34 +0000522static int findDb(sqlite3 *db, Token *pName){
danielk1977576ec6b2005-01-21 11:55:25 +0000523 int i = -1; /* Database number */
drh73c42a12004-11-20 18:13:10 +0000524 int n; /* Number of characters in the name */
525 Db *pDb; /* A database whose name space is being searched */
526 char *zName; /* Name we are searching for */
527
528 zName = sqlite3NameFromToken(pName);
529 if( zName ){
530 n = strlen(zName);
danielk1977576ec6b2005-01-21 11:55:25 +0000531 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
drh73c42a12004-11-20 18:13:10 +0000532 if( n==strlen(pDb->zName) && 0==sqlite3StrICmp(pDb->zName, zName) ){
danielk1977576ec6b2005-01-21 11:55:25 +0000533 break;
drh73c42a12004-11-20 18:13:10 +0000534 }
danielk1977cbb18d22004-05-28 11:37:27 +0000535 }
drh73c42a12004-11-20 18:13:10 +0000536 sqliteFree(zName);
danielk1977cbb18d22004-05-28 11:37:27 +0000537 }
danielk1977576ec6b2005-01-21 11:55:25 +0000538 return i;
danielk1977cbb18d22004-05-28 11:37:27 +0000539}
540
drh0e3d7472004-06-19 17:33:07 +0000541/* The table or view or trigger name is passed to this routine via tokens
542** pName1 and pName2. If the table name was fully qualified, for example:
543**
544** CREATE TABLE xxx.yyy (...);
545**
546** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
547** the table name is not fully qualified, i.e.:
548**
549** CREATE TABLE yyy(...);
550**
551** Then pName1 is set to "yyy" and pName2 is "".
552**
553** This routine sets the *ppUnqual pointer to point at the token (pName1 or
554** pName2) that stores the unqualified table name. The index of the
555** database "xxx" is returned.
556*/
danielk1977ef2cb632004-05-29 02:37:19 +0000557int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000558 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35 +0000559 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07 +0000560 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
561 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000562){
drh0e3d7472004-06-19 17:33:07 +0000563 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000564 sqlite3 *db = pParse->db;
565
566 if( pName2 && pName2->n>0 ){
567 assert( !db->init.busy );
568 *pUnqual = pName2;
569 iDb = findDb(db, pName1);
570 if( iDb<0 ){
571 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
572 pParse->nErr++;
573 return -1;
574 }
575 }else{
576 assert( db->init.iDb==0 || db->init.busy );
577 iDb = db->init.iDb;
578 *pUnqual = pName1;
579 }
580 return iDb;
581}
582
583/*
danielk1977d8123362004-06-12 09:25:12 +0000584** This routine is used to check if the UTF-8 string zName is a legal
585** unqualified name for a new schema object (table, index, view or
586** trigger). All names are legal except those that begin with the string
587** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
588** is reserved for internal use.
589*/
590int sqlite3CheckObjectName(Parse *pParse, const char *zName){
drhf1974842004-11-05 03:56:00 +0000591 if( !pParse->db->init.busy && pParse->nested==0
592 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
danielk1977d8123362004-06-12 09:25:12 +0000593 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
594 return SQLITE_ERROR;
595 }
596 return SQLITE_OK;
597}
598
599/*
drh75897232000-05-29 14:26:00 +0000600** Begin constructing a new table representation in memory. This is
601** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000602** to a CREATE TABLE statement. In particular, this routine is called
603** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000604** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000605** flag is true if the table should be stored in the auxiliary database
606** file instead of in the main database file. This is normally the case
607** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000608** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000609**
drhf57b3392001-10-08 13:22:32 +0000610** The new table record is initialized and put in pParse->pNewTable.
611** As more of the CREATE TABLE statement is parsed, additional action
612** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000613** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000614** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000615*/
danielk19774adee202004-05-08 08:23:19 +0000616void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000617 Parse *pParse, /* Parser context */
618 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000619 Token *pName1, /* First part of the name of the table or view */
620 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000621 int isTemp, /* True if this is a TEMP table */
622 int isView /* True if this is a VIEW */
623){
drh75897232000-05-29 14:26:00 +0000624 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000625 Index *pIdx;
drh23bf66d2004-12-14 03:34:34 +0000626 char *zName = 0; /* The name of the new table */
drh9bb575f2004-09-06 17:24:11 +0000627 sqlite3 *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000628 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000629 int iDb; /* Database number to create the table in */
630 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000631
danielk1977cbb18d22004-05-28 11:37:27 +0000632 /* The table or view name to create is passed to this routine via tokens
633 ** pName1 and pName2. If the table name was fully qualified, for example:
634 **
635 ** CREATE TABLE xxx.yyy (...);
636 **
637 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
638 ** the table name is not fully qualified, i.e.:
639 **
640 ** CREATE TABLE yyy(...);
641 **
642 ** Then pName1 is set to "yyy" and pName2 is "".
643 **
644 ** The call below sets the pName pointer to point at the token (pName1 or
645 ** pName2) that stores the unqualified table name. The variable iDb is
646 ** set to the index of the database that the table or view is to be
647 ** created in.
648 */
danielk1977ef2cb632004-05-29 02:37:19 +0000649 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000650 if( iDb<0 ) return;
651 if( isTemp && iDb>1 ){
652 /* If creating a temp table, the name may not be qualified */
653 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
danielk1977cbb18d22004-05-28 11:37:27 +0000654 return;
655 }
656 if( isTemp ) iDb = 1;
657
658 pParse->sNameToken = *pName;
drha99db3b2004-06-19 14:49:12 +0000659 zName = sqlite3NameFromToken(pName);
danielk1977e0048402004-06-15 16:51:01 +0000660 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000661 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drh23bf66d2004-12-14 03:34:34 +0000662 goto begin_table_error;
danielk1977d8123362004-06-12 09:25:12 +0000663 }
drh1d85d932004-02-14 23:05:52 +0000664 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000665#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000666 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000667 {
668 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000669 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000670 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drh23bf66d2004-12-14 03:34:34 +0000671 goto begin_table_error;
drhe22a3342003-04-22 20:30:37 +0000672 }
drhe5f9c642003-01-13 23:27:31 +0000673 if( isView ){
674 if( isTemp ){
675 code = SQLITE_CREATE_TEMP_VIEW;
676 }else{
677 code = SQLITE_CREATE_VIEW;
678 }
679 }else{
680 if( isTemp ){
681 code = SQLITE_CREATE_TEMP_TABLE;
682 }else{
683 code = SQLITE_CREATE_TABLE;
684 }
685 }
danielk19774adee202004-05-08 08:23:19 +0000686 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh23bf66d2004-12-14 03:34:34 +0000687 goto begin_table_error;
drhe5f9c642003-01-13 23:27:31 +0000688 }
689 }
690#endif
drhf57b3392001-10-08 13:22:32 +0000691
drhf57b3392001-10-08 13:22:32 +0000692 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000693 ** index or table name in the same database. Issue an error message if
694 ** it does.
drhf57b3392001-10-08 13:22:32 +0000695 */
danielk19775558a8a2005-01-17 07:53:44 +0000696 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
697 goto begin_table_error;
698 }
danielk19773df6b252004-05-29 10:23:19 +0000699 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
700 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000701 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drh23bf66d2004-12-14 03:34:34 +0000702 goto begin_table_error;
drh75897232000-05-29 14:26:00 +0000703 }
danielk19778a414492004-06-29 08:59:35 +0000704 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
705 ( iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000706 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh23bf66d2004-12-14 03:34:34 +0000707 goto begin_table_error;
drh75897232000-05-29 14:26:00 +0000708 }
709 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000710 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000711 pParse->rc = SQLITE_NOMEM;
712 pParse->nErr++;
drh23bf66d2004-12-14 03:34:34 +0000713 goto begin_table_error;
drh6d4abfb2001-10-22 02:58:08 +0000714 }
drh75897232000-05-29 14:26:00 +0000715 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000716 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000717 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000718 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000719 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000720 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000721 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000722 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000723
drh4794f732004-11-05 17:17:50 +0000724 /* If this is the magic sqlite_sequence table used by autoincrement,
725 ** then record a pointer to this table in the main database structure
726 ** so that INSERT can find the table easily.
727 */
728#ifndef SQLITE_OMIT_AUTOINCREMENT
729 if( strcmp(zName, "sqlite_sequence")==0 ){
drh4794f732004-11-05 17:17:50 +0000730 db->aDb[iDb].pSeqTab = pTable;
731 }
732#endif
733
drh17f71932002-02-21 12:01:27 +0000734 /* Begin generating the code that will insert the table record into
735 ** the SQLITE_MASTER table. Note in particular that we must go ahead
736 ** and allocate the record number for the table entry now. Before any
737 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
738 ** indices to be created and the table record must come before the
739 ** indices. Hence, the record number for the table must be allocated
740 ** now.
741 */
danielk19774adee202004-05-08 08:23:19 +0000742 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000743 sqlite3BeginWriteOperation(pParse, 0, iDb);
drhb17131a2004-11-05 22:18:49 +0000744
danielk1977d008cfe2004-06-19 02:22:10 +0000745 /* Every time a new table is created the file-format
746 ** and encoding meta-values are set in the database, in
747 ** case this is the first table created.
748 */
749 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
750 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
751 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
752 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
753
drh4794f732004-11-05 17:17:50 +0000754 /* This just creates a place-holder record in the sqlite_master table.
755 ** The record created does not contain anything yet. It will be replaced
756 ** by the real entry in code generated at sqlite3EndTable().
drhb17131a2004-11-05 22:18:49 +0000757 **
758 ** The rowid for the new entry is left on the top of the stack.
759 ** The rowid value is needed by the code that sqlite3EndTable will
760 ** generate.
drh4794f732004-11-05 17:17:50 +0000761 */
danielk1977a21c6b62005-01-24 10:25:59 +0000762#ifndef SQLITE_OMIT_VIEW
763 if( isView ){
764 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
765 }else
766#endif
767 {
768 sqlite3VdbeAddOp(v, OP_CreateTable, iDb, 0);
769 }
danielk1977cbb18d22004-05-28 11:37:27 +0000770 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000771 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
772 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000773 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000774 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977e6efa742004-11-10 11:55:10 +0000775 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
danielk1977a21c6b62005-01-24 10:25:59 +0000776 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +0000777 }
drh23bf66d2004-12-14 03:34:34 +0000778
779 /* Normal (non-error) return. */
780 return;
781
782 /* If an error occurs, we jump here */
783begin_table_error:
784 sqliteFree(zName);
785 return;
drh75897232000-05-29 14:26:00 +0000786}
787
788/*
danielk1977c60e9b82005-01-31 12:42:29 +0000789** This macro is used to compare two strings in a case-insensitive manner.
790** It is slightly faster than calling sqlite3StrICmp() directly, but
791** produces larger code.
792**
793** WARNING: This macro is not compatible with the strcmp() family. It
794** returns true if the two strings are equal, otherwise false.
795*/
796#define STRICMP(x, y) (\
797sqlite3UpperToLower[*(unsigned char *)(x)]== \
798sqlite3UpperToLower[*(unsigned char *)(y)] \
799&& sqlite3StrICmp((x)+1,(y)+1)==0 )
800
801/*
drh75897232000-05-29 14:26:00 +0000802** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000803**
804** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000805** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000806** first to get things going. Then this routine is called for each
807** column.
drh75897232000-05-29 14:26:00 +0000808*/
danielk19774adee202004-05-08 08:23:19 +0000809void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000810 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000811 int i;
drha99db3b2004-06-19 14:49:12 +0000812 char *z;
drhc9b84a12002-06-20 11:36:48 +0000813 Column *pCol;
drh75897232000-05-29 14:26:00 +0000814 if( (p = pParse->pNewTable)==0 ) return;
drha99db3b2004-06-19 14:49:12 +0000815 z = sqlite3NameFromToken(pName);
drh97fc3d02002-05-22 21:27:03 +0000816 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000817 for(i=0; i<p->nCol; i++){
danielk1977c60e9b82005-01-31 12:42:29 +0000818 if( STRICMP(z, p->aCol[i].zName) ){
danielk19774adee202004-05-08 08:23:19 +0000819 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000820 sqliteFree(z);
821 return;
822 }
823 }
drh75897232000-05-29 14:26:00 +0000824 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000825 Column *aNew;
826 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
827 if( aNew==0 ) return;
828 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000829 }
drhc9b84a12002-06-20 11:36:48 +0000830 pCol = &p->aCol[p->nCol];
831 memset(pCol, 0, sizeof(p->aCol[0]));
832 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000833
834 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000835 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
836 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000837 */
danielk19774f057f92004-06-08 00:02:33 +0000838 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000839 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000840 p->nCol++;
drh75897232000-05-29 14:26:00 +0000841}
842
843/*
drh382c0242001-10-06 16:33:02 +0000844** This routine is called by the parser while in the middle of
845** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
846** been seen on a column. This routine sets the notNull flag on
847** the column currently under construction.
848*/
danielk19774adee202004-05-08 08:23:19 +0000849void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000850 Table *p;
851 int i;
852 if( (p = pParse->pNewTable)==0 ) return;
853 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000854 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000855}
856
857/*
danielk197752a83fb2005-01-31 12:56:44 +0000858** Scan the column type name zType (length nType) and return the
859** associated affinity type.
danielk1977b3dff962005-02-01 01:21:55 +0000860**
861** This routine does a case-independent search of zType for the
862** substrings in the following table. If one of the substrings is
863** found, the corresponding affinity is returned. If zType contains
864** more than one of the substrings, entries toward the top of
865** the table take priority. For example, if zType is 'BLOBINT',
866** SQLITE_AFF_INTEGER is returned.
867**
868** Substring | Affinity
869** --------------------------------
870** 'INT' | SQLITE_AFF_INTEGER
871** 'CHAR' | SQLITE_AFF_TEXT
872** 'CLOB' | SQLITE_AFF_TEXT
873** 'TEXT' | SQLITE_AFF_TEXT
874** 'BLOB' | SQLITE_AFF_NONE
875**
876** If none of the substrings in the above table are found,
877** SQLITE_AFF_NUMERIC is returned.
danielk197752a83fb2005-01-31 12:56:44 +0000878*/
879static char sqlite3AffinityType(const char *zType, int nType){
danielk1977b3dff962005-02-01 01:21:55 +0000880 u32 h = 0;
881 char aff = SQLITE_AFF_NUMERIC;
882 const unsigned char *zIn = zType;
883 const unsigned char *zEnd = (zIn+nType);
danielk197752a83fb2005-01-31 12:56:44 +0000884
danielk1977b3dff962005-02-01 01:21:55 +0000885 while( zIn!=zEnd ){
886 h = (h<<8) + sqlite3UpperToLower[*zIn];
887 zIn++;
danielk1977201f7162005-02-01 02:13:29 +0000888 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
889 aff = SQLITE_AFF_TEXT;
890 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
891 aff = SQLITE_AFF_TEXT;
892 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
893 aff = SQLITE_AFF_TEXT;
894 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
895 && aff==SQLITE_AFF_NUMERIC ){
danielk1977b3dff962005-02-01 01:21:55 +0000896 aff = SQLITE_AFF_NONE;
danielk1977201f7162005-02-01 02:13:29 +0000897 }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */
danielk1977b3dff962005-02-01 01:21:55 +0000898 aff = SQLITE_AFF_INTEGER;
899 break;
danielk197752a83fb2005-01-31 12:56:44 +0000900 }
901 }
danielk1977b3dff962005-02-01 01:21:55 +0000902
903 return aff;
danielk197752a83fb2005-01-31 12:56:44 +0000904}
905
906/*
drh382c0242001-10-06 16:33:02 +0000907** This routine is called by the parser while in the middle of
908** parsing a CREATE TABLE statement. The pFirst token is the first
909** token in the sequence of tokens that describe the type of the
910** column currently under construction. pLast is the last token
911** in the sequence. Use this information to construct a string
912** that contains the typename of the column and store that string
913** in zType.
914*/
danielk19774adee202004-05-08 08:23:19 +0000915void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000916 Table *p;
917 int i, j;
918 int n;
danielk1977c60e9b82005-01-31 12:42:29 +0000919 char *z;
920 const unsigned char *zIn;
921
drhc9b84a12002-06-20 11:36:48 +0000922 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000923 if( (p = pParse->pNewTable)==0 ) return;
924 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000925 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000926 pCol = &p->aCol[i];
danielk1977c60e9b82005-01-31 12:42:29 +0000927 zIn = pFirst->z;
928 n = pLast->n + (pLast->z - zIn);
drhae29ffb2004-09-25 14:39:18 +0000929 assert( pCol->zType==0 );
danielk1977c60e9b82005-01-31 12:42:29 +0000930 z = pCol->zType = sqliteMallocRaw(n+1);
drhf57b3392001-10-08 13:22:32 +0000931 if( z==0 ) return;
danielk1977c60e9b82005-01-31 12:42:29 +0000932 for(i=j=0; i<n; i++){
933 int c = zIn[i];
drh382c0242001-10-06 16:33:02 +0000934 if( isspace(c) ) continue;
935 z[j++] = c;
936 }
937 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000938 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000939}
940
941/*
danielk19777977a172004-11-09 12:44:37 +0000942** The expression is the default value for the most recently added column
943** of the table currently under construction.
944**
945** Default value expressions must be constant. Raise an exception if this
946** is not the case.
drhd9b02572001-04-15 00:37:09 +0000947**
948** This routine is called by the parser while in the middle of
949** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000950*/
danielk19777977a172004-11-09 12:44:37 +0000951void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){
drh7020f652000-06-03 18:06:52 +0000952 Table *p;
danielk19777977a172004-11-09 12:44:37 +0000953 Column *pCol;
drh7020f652000-06-03 18:06:52 +0000954 if( (p = pParse->pNewTable)==0 ) return;
danielk19777977a172004-11-09 12:44:37 +0000955 pCol = &(p->aCol[p->nCol-1]);
956 if( !sqlite3ExprIsConstant(pExpr) ){
957 sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
958 pCol->zName);
959 }else{
960 sqlite3ExprDelete(pCol->pDflt);
961 pCol->pDflt = sqlite3ExprDup(pExpr);
danielk19777977a172004-11-09 12:44:37 +0000962 }
963 sqlite3ExprDelete(pExpr);
drh7020f652000-06-03 18:06:52 +0000964}
965
966/*
drh4a324312001-12-21 14:30:42 +0000967** Designate the PRIMARY KEY for the table. pList is a list of names
968** of columns that form the primary key. If pList is NULL, then the
969** most recently added column of the table is the primary key.
970**
971** A table can have at most one primary key. If the table already has
972** a primary key (and this is the second primary key) then create an
973** error.
974**
975** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
drh23bf66d2004-12-14 03:34:34 +0000976** then we will try to use that column as the rowid. Set the Table.iPKey
drh4a324312001-12-21 14:30:42 +0000977** field of the table under construction to be the index of the
978** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
979** no INTEGER PRIMARY KEY.
980**
981** If the key is not an INTEGER PRIMARY KEY, then create a unique
982** index for the key. No index is created for INTEGER PRIMARY KEYs.
983*/
drh205f48e2004-11-05 00:43:11 +0000984void sqlite3AddPrimaryKey(
985 Parse *pParse, /* Parsing context */
986 ExprList *pList, /* List of field names to be indexed */
987 int onError, /* What to do with a uniqueness conflict */
988 int autoInc /* True if the AUTOINCREMENT keyword is present */
989){
drh4a324312001-12-21 14:30:42 +0000990 Table *pTab = pParse->pNewTable;
991 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000992 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000993 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000994 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000995 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000996 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000997 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000998 }
999 pTab->hasPrimKey = 1;
1000 if( pList==0 ){
1001 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +00001002 pTab->aCol[iCol].isPrimKey = 1;
1003 }else{
danielk19770202b292004-06-09 09:55:16 +00001004 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +00001005 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +00001006 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
1007 break;
1008 }
drh78100cc2003-08-23 22:40:53 +00001009 }
1010 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +00001011 }
danielk19770202b292004-06-09 09:55:16 +00001012 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +00001013 }
1014 if( iCol>=0 && iCol<pTab->nCol ){
1015 zType = pTab->aCol[iCol].zType;
1016 }
danielk19773d68f032004-05-11 07:11:51 +00001017 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +00001018 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +00001019 pTab->keyConf = onError;
drh205f48e2004-11-05 00:43:11 +00001020 pTab->autoInc = autoInc;
1021 }else if( autoInc ){
drh4794f732004-11-05 17:17:50 +00001022#ifndef SQLITE_OMIT_AUTOINCREMENT
drh205f48e2004-11-05 00:43:11 +00001023 sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
1024 "INTEGER PRIMARY KEY");
drh4794f732004-11-05 17:17:50 +00001025#endif
drh4a324312001-12-21 14:30:42 +00001026 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001027 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +00001028 pList = 0;
drh4a324312001-12-21 14:30:42 +00001029 }
drhe0194f22003-02-26 13:52:51 +00001030
1031primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +00001032 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +00001033 return;
drh4a324312001-12-21 14:30:42 +00001034}
1035
1036/*
drhd3d39e92004-05-20 22:16:29 +00001037** Set the collation function of the most recently parsed table column
1038** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +00001039*/
drhd3d39e92004-05-20 22:16:29 +00001040void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +00001041 Table *p;
danielk19770202b292004-06-09 09:55:16 +00001042 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +00001043 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +00001044 int i;
danielk1977a37cdde2004-05-16 11:15:36 +00001045
drhd3d39e92004-05-20 22:16:29 +00001046 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +00001047 i = p->nCol-1;
1048
1049 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
1050 p->aCol[i].pColl = pColl;
1051
1052 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
1053 ** then an index may have been created on this column before the
1054 ** collation type was added. Correct this if it is the case.
1055 */
1056 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
1057 assert( pIdx->nColumn==1 );
1058 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +00001059 }
1060}
1061
1062/*
danielk19770202b292004-06-09 09:55:16 +00001063** Locate and return an entry from the db.aCollSeq hash table. If the entry
1064** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +00001065** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +00001066**
danielk1977466be562004-06-10 02:16:01 +00001067** Each pointer stored in the sqlite3.aCollSeq hash table contains an
1068** array of three CollSeq structures. The first is the collation sequence
1069** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +00001070**
danielk1977466be562004-06-10 02:16:01 +00001071** Stored immediately after the three collation sequences is a copy of
1072** the collation sequence name. A pointer to this string is stored in
1073** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +00001074*/
danielk1977466be562004-06-10 02:16:01 +00001075static CollSeq * findCollSeqEntry(
drh9bb575f2004-09-06 17:24:11 +00001076 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +00001077 const char *zName,
danielk19770202b292004-06-09 09:55:16 +00001078 int nName,
1079 int create
drhd3d39e92004-05-20 22:16:29 +00001080){
1081 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +00001082 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +00001083 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001084
danielk19770202b292004-06-09 09:55:16 +00001085 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +00001086 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +00001087 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +00001088 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +00001089 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +00001090 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +00001091 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +00001092 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +00001093 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +00001094 memcpy(pColl[0].zName, zName, nName);
1095 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +00001096 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +00001097 }
drhd3d39e92004-05-20 22:16:29 +00001098 }
drhd3d39e92004-05-20 22:16:29 +00001099 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +00001100}
1101
danielk1977466be562004-06-10 02:16:01 +00001102/*
1103** Parameter zName points to a UTF-8 encoded string nName bytes long.
1104** Return the CollSeq* pointer for the collation sequence named zName
1105** for the encoding 'enc' from the database 'db'.
1106**
1107** If the entry specified is not found and 'create' is true, then create a
1108** new entry. Otherwise return NULL.
1109*/
1110CollSeq *sqlite3FindCollSeq(
drh9bb575f2004-09-06 17:24:11 +00001111 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +00001112 u8 enc,
1113 const char *zName,
1114 int nName,
1115 int create
1116){
1117 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
drh6d08b4d2004-07-20 12:45:22 +00001118 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
1119 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
1120 if( pColl ) pColl += enc-1;
danielk1977466be562004-06-10 02:16:01 +00001121 return pColl;
1122}
1123
danielk1977e159fdf2004-06-21 10:45:06 +00001124/*
1125** Invoke the 'collation needed' callback to request a collation sequence
1126** in the database text encoding of name zName, length nName.
1127** If the collation sequence
1128*/
drh9bb575f2004-09-06 17:24:11 +00001129static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
danielk19777cedc8d2004-06-10 10:50:08 +00001130 assert( !db->xCollNeeded || !db->xCollNeeded16 );
1131 if( nName<0 ) nName = strlen(zName);
1132 if( db->xCollNeeded ){
danielk19778a6c5502004-06-22 12:18:32 +00001133 char *zExternal = sqliteStrNDup(zName, nName);
danielk19777cedc8d2004-06-10 10:50:08 +00001134 if( !zExternal ) return;
danielk1977e159fdf2004-06-21 10:45:06 +00001135 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
1136 sqliteFree(zExternal);
danielk19777cedc8d2004-06-10 10:50:08 +00001137 }
danielk1977576ec6b2005-01-21 11:55:25 +00001138#ifndef SQLITE_OMIT_UTF16
danielk19777cedc8d2004-06-10 10:50:08 +00001139 if( db->xCollNeeded16 ){
danielk19778a6c5502004-06-22 12:18:32 +00001140 char const *zExternal;
danielk1977bfd6cce2004-06-18 04:24:54 +00001141 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
1142 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
1143 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +00001144 if( !zExternal ) return;
1145 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
1146 }
danielk1977576ec6b2005-01-21 11:55:25 +00001147#endif
danielk19777cedc8d2004-06-10 10:50:08 +00001148}
1149
danielk1977e159fdf2004-06-21 10:45:06 +00001150/*
1151** This routine is called if the collation factory fails to deliver a
1152** collation function in the best encoding but there may be other versions
1153** of this collation function (for other text encodings) available. Use one
1154** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
1155** possible.
1156*/
danielk19777cedc8d2004-06-10 10:50:08 +00001157static int synthCollSeq(Parse *pParse, CollSeq *pColl){
drhda71ce12004-06-21 18:14:45 +00001158 CollSeq *pColl2;
danielk19777cedc8d2004-06-10 10:50:08 +00001159 char *z = pColl->zName;
1160 int n = strlen(z);
drh9bb575f2004-09-06 17:24:11 +00001161 sqlite3 *db = pParse->db;
drhda71ce12004-06-21 18:14:45 +00001162 int i;
1163 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
1164 for(i=0; i<3; i++){
1165 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
1166 if( pColl2->xCmp!=0 ){
1167 memcpy(pColl, pColl2, sizeof(CollSeq));
1168 return SQLITE_OK;
danielk19777cedc8d2004-06-10 10:50:08 +00001169 }
danielk19777cedc8d2004-06-10 10:50:08 +00001170 }
drhda71ce12004-06-21 18:14:45 +00001171 if( pParse->nErr==0 ){
drhae29ffb2004-09-25 14:39:18 +00001172 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", n, z);
drhda71ce12004-06-21 18:14:45 +00001173 }
1174 pParse->nErr++;
1175 return SQLITE_ERROR;
danielk19777cedc8d2004-06-10 10:50:08 +00001176}
1177
1178/*
1179** This routine is called on a collation sequence before it is used to
1180** check that it is defined. An undefined collation sequence exists when
1181** a database is loaded that contains references to collation sequences
1182** that have not been defined by sqlite3_create_collation() etc.
1183**
1184** If required, this routine calls the 'collation needed' callback to
1185** request a definition of the collating sequence. If this doesn't work,
1186** an equivalent collating sequence that uses a text encoding different
1187** from the main database is substituted, if one is available.
1188*/
1189int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1190 if( pColl && !pColl->xCmp ){
danielk1977e159fdf2004-06-21 10:45:06 +00001191 /* No collation sequence of this type for this encoding is registered.
1192 ** Call the collation factory to see if it can supply us with one.
1193 */
danielk19777cedc8d2004-06-10 10:50:08 +00001194 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1195 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1196 return SQLITE_ERROR;
1197 }
1198 }
1199 return SQLITE_OK;
1200}
1201
drhda71ce12004-06-21 18:14:45 +00001202/*
1203** Call sqlite3CheckCollSeq() for all collating sequences in an index,
1204** in order to verify that all the necessary collating sequences are
1205** loaded.
1206*/
danielk19777cedc8d2004-06-10 10:50:08 +00001207int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1208 if( pIdx ){
1209 int i;
1210 for(i=0; i<pIdx->nColumn; i++){
1211 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1212 return SQLITE_ERROR;
1213 }
1214 }
1215 }
1216 return SQLITE_OK;
1217}
1218
danielk1977466be562004-06-10 02:16:01 +00001219/*
1220** This function returns the collation sequence for database native text
1221** encoding identified by the string zName, length nName.
1222**
1223** If the requested collation sequence is not available, or not available
1224** in the database native encoding, the collation factory is invoked to
1225** request it. If the collation factory does not supply such a sequence,
1226** and the sequence is available in another text encoding, then that is
1227** returned instead.
1228**
1229** If no versions of the requested collations sequence are available, or
1230** another error occurs, NULL is returned and an error message written into
1231** pParse.
1232*/
danielk19770202b292004-06-09 09:55:16 +00001233CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001234 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001235 u8 initbusy = pParse->db->init.busy;
1236 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
drhae29ffb2004-09-25 14:39:18 +00001237 if( nName<0 ) nName = strlen(zName);
danielk19777cedc8d2004-06-10 10:50:08 +00001238 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001239 /* No collation sequence of this type for this encoding is registered.
1240 ** Call the collation factory to see if it can supply us with one.
1241 */
danielk19777cedc8d2004-06-10 10:50:08 +00001242 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001243 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001244 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001245 /* There may be a version of the collation sequence that requires
1246 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001247 */
danielk19777cedc8d2004-06-10 10:50:08 +00001248 if( synthCollSeq(pParse, pColl) ){
1249 return 0;
danielk1977466be562004-06-10 02:16:01 +00001250 }
1251 }
1252 }
1253
1254 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001255 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001256 if( pParse->nErr==0 ){
drhae29ffb2004-09-25 14:39:18 +00001257 sqlite3ErrorMsg(pParse, "no such collation sequence: %.*s", nName, zName);
danielk19770202b292004-06-09 09:55:16 +00001258 }
danielk19777cedc8d2004-06-10 10:50:08 +00001259 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001260 }
1261 return pColl;
1262}
1263
1264
drh8e2ca022002-06-17 17:07:19 +00001265/*
drh3f7d4e42004-07-24 14:35:58 +00001266** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001267**
1268** The schema cookie is used to determine when the schema for the
1269** database changes. After each schema change, the cookie value
1270** changes. When a process first reads the schema it records the
1271** cookie. Thereafter, whenever it goes to access the database,
1272** it checks the cookie to make sure the schema has not changed
1273** since it was last read.
1274**
1275** This plan is not completely bullet-proof. It is possible for
1276** the schema to change multiple times and for the cookie to be
1277** set back to prior value. But schema changes are infrequent
1278** and the probability of hitting the same cookie value is only
1279** 1 chance in 2^32. So we're safe enough.
1280*/
drh9bb575f2004-09-06 17:24:11 +00001281void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){
drh3f7d4e42004-07-24 14:35:58 +00001282 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].schema_cookie+1, 0);
danielk19771d850a72004-05-31 08:26:49 +00001283 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001284}
1285
1286/*
drh969fa7c2002-02-18 18:30:32 +00001287** Measure the number of characters needed to output the given
1288** identifier. The number returned includes any quotes used
1289** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001290**
1291** The estimate is conservative. It might be larger that what is
1292** really needed.
drh969fa7c2002-02-18 18:30:32 +00001293*/
1294static int identLength(const char *z){
1295 int n;
drh17f71932002-02-21 12:01:27 +00001296 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001297 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001298 }
drh234c39d2004-07-24 03:30:47 +00001299 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001300}
1301
1302/*
1303** Write an identifier onto the end of the given string. Add
1304** quote characters as needed.
1305*/
drh4c755c02004-08-08 20:22:17 +00001306static void identPut(char *z, int *pIdx, char *zSignedIdent){
1307 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001308 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001309 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001310 for(j=0; zIdent[j]; j++){
1311 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1312 }
1313 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001314 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh234c39d2004-07-24 03:30:47 +00001315 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001316 for(j=0; zIdent[j]; j++){
1317 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001318 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001319 }
drh234c39d2004-07-24 03:30:47 +00001320 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001321 z[i] = 0;
1322 *pIdx = i;
1323}
1324
1325/*
1326** Generate a CREATE TABLE statement appropriate for the given
1327** table. Memory to hold the text of the statement is obtained
1328** from sqliteMalloc() and must be freed by the calling function.
1329*/
1330static char *createTableStmt(Table *p){
1331 int i, k, n;
1332 char *zStmt;
drh234c39d2004-07-24 03:30:47 +00001333 char *zSep, *zSep2, *zEnd, *z;
1334 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001335 n = 0;
drh234c39d2004-07-24 03:30:47 +00001336 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1337 n += identLength(pCol->zName);
1338 z = pCol->zType;
1339 if( z ){
1340 n += (strlen(z) + 1);
danielk1977517eb642004-06-07 10:00:31 +00001341 }
drh969fa7c2002-02-18 18:30:32 +00001342 }
1343 n += identLength(p->zName);
drh234c39d2004-07-24 03:30:47 +00001344 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001345 zSep = "";
1346 zSep2 = ",";
1347 zEnd = ")";
1348 }else{
1349 zSep = "\n ";
1350 zSep2 = ",\n ";
1351 zEnd = "\n)";
1352 }
drhe0bc4042002-06-25 01:09:11 +00001353 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001354 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001355 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001356 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001357 k = strlen(zStmt);
1358 identPut(zStmt, &k, p->zName);
1359 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001360 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drh969fa7c2002-02-18 18:30:32 +00001361 strcpy(&zStmt[k], zSep);
1362 k += strlen(&zStmt[k]);
1363 zSep = zSep2;
drh234c39d2004-07-24 03:30:47 +00001364 identPut(zStmt, &k, pCol->zName);
1365 if( (z = pCol->zType)!=0 ){
danielk1977517eb642004-06-07 10:00:31 +00001366 zStmt[k++] = ' ';
drh234c39d2004-07-24 03:30:47 +00001367 strcpy(&zStmt[k], z);
1368 k += strlen(z);
danielk1977517eb642004-06-07 10:00:31 +00001369 }
drh969fa7c2002-02-18 18:30:32 +00001370 }
1371 strcpy(&zStmt[k], zEnd);
1372 return zStmt;
1373}
1374
1375/*
drh75897232000-05-29 14:26:00 +00001376** This routine is called to report the final ")" that terminates
1377** a CREATE TABLE statement.
1378**
drhf57b3392001-10-08 13:22:32 +00001379** The table structure that other action routines have been building
1380** is added to the internal hash tables, assuming no errors have
1381** occurred.
drh75897232000-05-29 14:26:00 +00001382**
drh1d85d932004-02-14 23:05:52 +00001383** An entry for the table is made in the master table on disk, unless
1384** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001385** it means we are reading the sqlite_master table because we just
1386** connected to the database or because the sqlite_master table has
1387** recently changes, so the entry for this table already exists in
1388** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001389**
1390** If the pSelect argument is not NULL, it means that this routine
1391** was called to create a table generated from a
1392** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1393** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001394*/
danielk19774adee202004-05-08 08:23:19 +00001395void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001396 Table *p;
drh9bb575f2004-09-06 17:24:11 +00001397 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001398
danielk197724b03fd2004-05-10 10:34:34 +00001399 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001400 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001401 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001402
danielk1977517eb642004-06-07 10:00:31 +00001403 assert( !db->init.busy || !pSelect );
1404
drh1d85d932004-02-14 23:05:52 +00001405 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001406 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1407 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001408 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001409 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001410 */
drh1d85d932004-02-14 23:05:52 +00001411 if( db->init.busy ){
1412 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001413 }
1414
drhe3c41372001-09-17 20:25:58 +00001415 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001416 ** in the SQLITE_MASTER table of the database. The record number
1417 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001418 **
drhe0bc4042002-06-25 01:09:11 +00001419 ** If this is a TEMPORARY table, write the entry into the auxiliary
1420 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001421 */
drh1d85d932004-02-14 23:05:52 +00001422 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001423 int n;
drhd8bc7082000-06-07 23:51:50 +00001424 Vdbe *v;
drh4794f732004-11-05 17:17:50 +00001425 char *zType; /* "view" or "table" */
1426 char *zType2; /* "VIEW" or "TABLE" */
1427 char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */
drh75897232000-05-29 14:26:00 +00001428
danielk19774adee202004-05-08 08:23:19 +00001429 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001430 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001431
danielk1977e6efa742004-11-10 11:55:10 +00001432 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1433
drh4794f732004-11-05 17:17:50 +00001434 /* Create the rootpage for the new table and push it onto the stack.
1435 ** A view has no rootpage, so just push a zero onto the stack for
1436 ** views. Initialize zType at the same time.
1437 */
drh4ff6dfa2002-03-03 23:06:00 +00001438 if( p->pSelect==0 ){
1439 /* A regular table */
danielk1977a21c6b62005-01-24 10:25:59 +00001440 /* sqlite3VdbeAddOp(v, OP_CreateTable, p->iDb, 0); */
drh4794f732004-11-05 17:17:50 +00001441 zType = "table";
1442 zType2 = "TABLE";
danielk1977576ec6b2005-01-21 11:55:25 +00001443#ifndef SQLITE_OMIT_VIEW
drh4ff6dfa2002-03-03 23:06:00 +00001444 }else{
1445 /* A view */
danielk1977a21c6b62005-01-24 10:25:59 +00001446 /* sqlite3VdbeAddOp(v, OP_Integer, 0, 0); */
drh4794f732004-11-05 17:17:50 +00001447 zType = "view";
1448 zType2 = "VIEW";
danielk1977576ec6b2005-01-21 11:55:25 +00001449#endif
drh4ff6dfa2002-03-03 23:06:00 +00001450 }
danielk1977517eb642004-06-07 10:00:31 +00001451
danielk1977517eb642004-06-07 10:00:31 +00001452 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1453 ** statement to populate the new table. The root-page number for the
1454 ** new table is on the top of the vdbe stack.
1455 **
1456 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1457 ** suitable state to query for the column names and types to be used
1458 ** by the new table.
1459 */
1460 if( pSelect ){
1461 Table *pSelTab;
1462 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1463 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1464 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1465 pParse->nTab = 2;
danielk1977b3bce662005-01-29 08:32:43 +00001466 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
danielk1977517eb642004-06-07 10:00:31 +00001467 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1468 if( pParse->nErr==0 ){
1469 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1470 if( pSelTab==0 ) return;
1471 assert( p->aCol==0 );
1472 p->nCol = pSelTab->nCol;
1473 p->aCol = pSelTab->aCol;
1474 pSelTab->nCol = 0;
1475 pSelTab->aCol = 0;
1476 sqlite3DeleteTable(0, pSelTab);
1477 }
1478 }
drh4794f732004-11-05 17:17:50 +00001479
drh4794f732004-11-05 17:17:50 +00001480 /* Compute the complete text of the CREATE statement */
1481 if( pSelect ){
1482 zStmt = createTableStmt(p);
1483 }else{
1484 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
1485 zStmt = sqlite3MPrintf("CREATE %s %.*s", zType2, n, pParse->sNameToken.z);
1486 }
1487
1488 /* A slot for the record has already been allocated in the
1489 ** SQLITE_MASTER table. We just need to update that slot with all
1490 ** the information we've collected. The rowid for the preallocated
1491 ** slot is the 2nd item on the stack. The top of the stack is the
1492 ** root page for the new table (or a 0 if this is a view).
1493 */
1494 sqlite3NestedParse(pParse,
1495 "UPDATE %Q.%s "
1496 "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#0, sql=%Q "
1497 "WHERE rowid=#1",
1498 db->aDb[p->iDb].zName, SCHEMA_TABLE(p->iDb),
1499 zType,
1500 p->zName,
1501 p->zName,
1502 zStmt
1503 );
1504 sqliteFree(zStmt);
drh2958a4e2004-11-12 03:56:15 +00001505 sqlite3ChangeCookie(db, v, p->iDb);
1506
1507#ifndef SQLITE_OMIT_AUTOINCREMENT
1508 /* Check to see if we need to create an sqlite_sequence table for
1509 ** keeping track of autoincrement keys.
1510 */
1511 if( p->autoInc ){
1512 Db *pDb = &db->aDb[p->iDb];
1513 if( pDb->pSeqTab==0 ){
1514 sqlite3NestedParse(pParse,
drhf3388142004-11-13 03:48:06 +00001515 "CREATE TABLE %Q.sqlite_sequence(name,seq)",
1516 pDb->zName
drh2958a4e2004-11-12 03:56:15 +00001517 );
1518 }
1519 }
1520#endif
drh4794f732004-11-05 17:17:50 +00001521
1522 /* Reparse everything to update our internal data structures */
drh234c39d2004-07-24 03:30:47 +00001523 sqlite3VdbeOp3(v, OP_ParseSchema, p->iDb, 0,
1524 sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC);
drh75897232000-05-29 14:26:00 +00001525 }
drh17e9e292003-02-01 13:53:28 +00001526
drh2958a4e2004-11-12 03:56:15 +00001527
drh17e9e292003-02-01 13:53:28 +00001528 /* Add the table to the in-memory representation of the database.
1529 */
drh234c39d2004-07-24 03:30:47 +00001530 if( db->init.busy && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001531 Table *pOld;
drhbe5c89a2004-07-26 00:31:09 +00001532 FKey *pFKey;
1533 Db *pDb = &db->aDb[p->iDb];
1534 pOld = sqlite3HashInsert(&pDb->tblHash, p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001535 if( pOld ){
1536 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1537 return;
1538 }
danielk1977576ec6b2005-01-21 11:55:25 +00001539#ifndef SQLITE_OMIT_FOREIGN_KEY
drh17e9e292003-02-01 13:53:28 +00001540 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1541 int nTo = strlen(pFKey->zTo) + 1;
drhbe5c89a2004-07-26 00:31:09 +00001542 pFKey->pNextTo = sqlite3HashFind(&pDb->aFKey, pFKey->zTo, nTo);
1543 sqlite3HashInsert(&pDb->aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001544 }
danielk1977576ec6b2005-01-21 11:55:25 +00001545#endif
drh17e9e292003-02-01 13:53:28 +00001546 pParse->pNewTable = 0;
1547 db->nTable++;
1548 db->flags |= SQLITE_InternChanges;
1549 }
drh75897232000-05-29 14:26:00 +00001550}
1551
drhb7f91642004-10-31 02:22:47 +00001552#ifndef SQLITE_OMIT_VIEW
drh75897232000-05-29 14:26:00 +00001553/*
drha76b5df2002-02-23 02:32:10 +00001554** The parser calls this routine in order to create a new VIEW
1555*/
danielk19774adee202004-05-08 08:23:19 +00001556void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001557 Parse *pParse, /* The parsing context */
1558 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001559 Token *pName1, /* The token that holds the name of the view */
1560 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001561 Select *pSelect, /* A SELECT statement that will become the new view */
1562 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001563){
drha76b5df2002-02-23 02:32:10 +00001564 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001565 int n;
drh4c755c02004-08-08 20:22:17 +00001566 const unsigned char *z;
drh4b59ab52002-08-24 18:24:51 +00001567 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001568 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001569 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001570
danielk197748dec7e2004-05-28 12:33:30 +00001571 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001572 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001573 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001574 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001575 return;
1576 }
danielk1977ef2cb632004-05-29 02:37:19 +00001577 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001578 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1579 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001580 ){
danielk19774adee202004-05-08 08:23:19 +00001581 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001582 return;
1583 }
drh174b6192002-12-03 02:22:52 +00001584
drh4b59ab52002-08-24 18:24:51 +00001585 /* Make a copy of the entire SELECT statement that defines the view.
1586 ** This will force all the Expr.token.z values to be dynamically
1587 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001588 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001589 */
danielk19774adee202004-05-08 08:23:19 +00001590 p->pSelect = sqlite3SelectDup(pSelect);
1591 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001592 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001593 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001594 }
drh4b59ab52002-08-24 18:24:51 +00001595
1596 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1597 ** the end.
1598 */
drha76b5df2002-02-23 02:32:10 +00001599 sEnd = pParse->sLastToken;
1600 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1601 sEnd.z += sEnd.n;
1602 }
1603 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001604 n = sEnd.z - pBegin->z;
drh4c755c02004-08-08 20:22:17 +00001605 z = (const unsigned char*)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001606 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1607 sEnd.z = &z[n-1];
1608 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001609
danielk19774adee202004-05-08 08:23:19 +00001610 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1611 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001612 return;
drh417be792002-03-03 18:59:40 +00001613}
drhb7f91642004-10-31 02:22:47 +00001614#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001615
drhb7f91642004-10-31 02:22:47 +00001616#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001617/*
1618** The Table structure pTable is really a VIEW. Fill in the names of
1619** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001620** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001621*/
danielk19774adee202004-05-08 08:23:19 +00001622int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh9b3187e2005-01-18 14:45:47 +00001623 Table *pSelTab; /* A fake table from which we get the result set */
1624 Select *pSel; /* Copy of the SELECT that implements the view */
1625 int nErr = 0; /* Number of errors encountered */
1626 int n; /* Temporarily holds the number of cursors assigned */
drh417be792002-03-03 18:59:40 +00001627
1628 assert( pTable );
1629
1630 /* A positive nCol means the columns names for this view are
1631 ** already known.
1632 */
1633 if( pTable->nCol>0 ) return 0;
1634
1635 /* A negative nCol is a special marker meaning that we are currently
1636 ** trying to compute the column names. If we enter this routine with
1637 ** a negative nCol, it means two or more views form a loop, like this:
1638 **
1639 ** CREATE VIEW one AS SELECT * FROM two;
1640 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001641 **
1642 ** Actually, this error is caught previously and so the following test
1643 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001644 */
1645 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001646 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001647 return 1;
1648 }
1649
1650 /* If we get this far, it means we need to compute the table names.
drh9b3187e2005-01-18 14:45:47 +00001651 ** Note that the call to sqlite3ResultSetOfSelect() will expand any
1652 ** "*" elements in the results set of the view and will assign cursors
1653 ** to the elements of the FROM clause. But we do not want these changes
1654 ** to be permanent. So the computation is done on a copy of the SELECT
1655 ** statement that defines the view.
drh417be792002-03-03 18:59:40 +00001656 */
drh9b3187e2005-01-18 14:45:47 +00001657 assert( pTable->pSelect );
1658 pSel = sqlite3SelectDup(pTable->pSelect);
1659 n = pParse->nTab;
1660 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
drh417be792002-03-03 18:59:40 +00001661 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001662 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh9b3187e2005-01-18 14:45:47 +00001663 pParse->nTab = n;
drh417be792002-03-03 18:59:40 +00001664 if( pSelTab ){
1665 assert( pTable->aCol==0 );
1666 pTable->nCol = pSelTab->nCol;
1667 pTable->aCol = pSelTab->aCol;
1668 pSelTab->nCol = 0;
1669 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001670 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001671 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001672 }else{
1673 pTable->nCol = 0;
1674 nErr++;
1675 }
drh9b3187e2005-01-18 14:45:47 +00001676 sqlite3SelectDelete(pSel);
drh417be792002-03-03 18:59:40 +00001677 return nErr;
1678}
drhb7f91642004-10-31 02:22:47 +00001679#endif /* SQLITE_OMIT_VIEW */
drh417be792002-03-03 18:59:40 +00001680
drhb7f91642004-10-31 02:22:47 +00001681#ifndef SQLITE_OMIT_VIEW
drh417be792002-03-03 18:59:40 +00001682/*
drh8bf8dc92003-05-17 17:35:10 +00001683** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001684*/
drh9bb575f2004-09-06 17:24:11 +00001685static void sqliteViewResetAll(sqlite3 *db, int idx){
drh417be792002-03-03 18:59:40 +00001686 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001687 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001688 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001689 Table *pTab = sqliteHashData(i);
1690 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001691 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001692 }
1693 }
drh8bf8dc92003-05-17 17:35:10 +00001694 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001695}
drhb7f91642004-10-31 02:22:47 +00001696#else
1697# define sqliteViewResetAll(A,B)
1698#endif /* SQLITE_OMIT_VIEW */
drha76b5df2002-02-23 02:32:10 +00001699
drh75897232000-05-29 14:26:00 +00001700/*
danielk1977a0bf2652004-11-04 14:30:04 +00001701** This function is called by the VDBE to adjust the internal schema
1702** used by SQLite when the btree layer moves a table root page. The
1703** root-page of a table or index in database iDb has changed from iFrom
1704** to iTo.
1705*/
1706#ifndef SQLITE_OMIT_AUTOVACUUM
1707void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){
1708 HashElem *pElem;
1709
1710 for(pElem=sqliteHashFirst(&pDb->tblHash); pElem; pElem=sqliteHashNext(pElem)){
1711 Table *pTab = sqliteHashData(pElem);
1712 if( pTab->tnum==iFrom ){
1713 pTab->tnum = iTo;
1714 return;
1715 }
1716 }
1717 for(pElem=sqliteHashFirst(&pDb->idxHash); pElem; pElem=sqliteHashNext(pElem)){
1718 Index *pIdx = sqliteHashData(pElem);
1719 if( pIdx->tnum==iFrom ){
1720 pIdx->tnum = iTo;
1721 return;
1722 }
1723 }
1724 assert(0);
1725}
1726#endif
1727
1728/*
1729** Write code to erase the table with root-page iTable from database iDb.
1730** Also write code to modify the sqlite_master table and internal schema
1731** if a root-page of another table is moved by the btree-layer whilst
1732** erasing iTable (this can happen with an auto-vacuum database).
1733*/
drh4e0cff62004-11-05 05:10:28 +00001734static void destroyRootPage(Parse *pParse, int iTable, int iDb){
1735 Vdbe *v = sqlite3GetVdbe(pParse);
drh40e016e2004-11-04 14:47:11 +00001736 sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb);
1737#ifndef SQLITE_OMIT_AUTOVACUUM
drh4e0cff62004-11-05 05:10:28 +00001738 /* OP_Destroy pushes an integer onto the stack. If this integer
1739 ** is non-zero, then it is the root page number of a table moved to
drh81db88e2004-12-07 12:29:17 +00001740 ** location iTable. The following code modifies the sqlite_master table to
drh4e0cff62004-11-05 05:10:28 +00001741 ** reflect this.
1742 **
1743 ** The "#0" in the SQL is a special constant that means whatever value
1744 ** is on the top of the stack. See sqlite3RegisterExpr().
1745 */
danielk197763e3e9f2004-11-05 09:19:27 +00001746 sqlite3NestedParse(pParse,
drh4794f732004-11-05 17:17:50 +00001747 "UPDATE %Q.%s SET rootpage=%d WHERE #0 AND rootpage=#0",
drh4e0cff62004-11-05 05:10:28 +00001748 pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable);
danielk1977a0bf2652004-11-04 14:30:04 +00001749#endif
1750}
1751
1752/*
1753** Write VDBE code to erase table pTab and all associated indices on disk.
1754** Code to update the sqlite_master tables and internal schema definitions
1755** in case a root-page belonging to another table is moved by the btree layer
1756** is also added (this can happen with an auto-vacuum database).
1757*/
drh4e0cff62004-11-05 05:10:28 +00001758static void destroyTable(Parse *pParse, Table *pTab){
danielk1977a0bf2652004-11-04 14:30:04 +00001759#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001760 Index *pIdx;
drh4e0cff62004-11-05 05:10:28 +00001761 destroyRootPage(pParse, pTab->tnum, pTab->iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001762 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh7a638582004-11-05 05:23:59 +00001763 destroyRootPage(pParse, pIdx->tnum, pIdx->iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001764 }
1765#else
1766 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
1767 ** is not defined), then it is important to call OP_Destroy on the
1768 ** table and index root-pages in order, starting with the numerically
1769 ** largest root-page number. This guarantees that none of the root-pages
1770 ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
1771 ** following were coded:
1772 **
1773 ** OP_Destroy 4 0
1774 ** ...
1775 ** OP_Destroy 5 0
1776 **
1777 ** and root page 5 happened to be the largest root-page number in the
1778 ** database, then root page 5 would be moved to page 4 by the
1779 ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
1780 ** a free-list page.
1781 */
1782 int iTab = pTab->tnum;
1783 int iDestroyed = 0;
1784
1785 while( 1 ){
1786 Index *pIdx;
1787 int iLargest = 0;
1788
1789 if( iDestroyed==0 || iTab<iDestroyed ){
1790 iLargest = iTab;
1791 }
1792 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1793 int iIdx = pIdx->tnum;
1794 assert( pIdx->iDb==pTab->iDb );
1795 if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
1796 iLargest = iIdx;
1797 }
1798 }
1799 if( iLargest==0 ) return;
drh4e0cff62004-11-05 05:10:28 +00001800 destroyRootPage(pParse, iLargest, pTab->iDb);
danielk1977a0bf2652004-11-04 14:30:04 +00001801 iDestroyed = iLargest;
1802 }
1803#endif
1804}
1805
1806/*
drh75897232000-05-29 14:26:00 +00001807** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001808** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001809*/
danielk1977a8858102004-05-28 12:11:21 +00001810void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1811 Table *pTab;
drh75897232000-05-29 14:26:00 +00001812 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00001813 sqlite3 *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001814 int iDb;
drh75897232000-05-29 14:26:00 +00001815
danielk1977a8858102004-05-28 12:11:21 +00001816 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1817 assert( pName->nSrc==1 );
1818 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1819
1820 if( pTab==0 ) goto exit_drop_table;
1821 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001822 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001823#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001824 {
1825 int code;
danielk1977a8858102004-05-28 12:11:21 +00001826 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1827 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001828 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001829 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001830 }
drhe5f9c642003-01-13 23:27:31 +00001831 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001832 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001833 code = SQLITE_DROP_TEMP_VIEW;
1834 }else{
1835 code = SQLITE_DROP_VIEW;
1836 }
1837 }else{
drhd24cc422003-03-27 12:51:24 +00001838 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001839 code = SQLITE_DROP_TEMP_TABLE;
1840 }else{
1841 code = SQLITE_DROP_TABLE;
1842 }
1843 }
danielk1977a8858102004-05-28 12:11:21 +00001844 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1845 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001846 }
danielk1977a8858102004-05-28 12:11:21 +00001847 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1848 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001849 }
drhe5f9c642003-01-13 23:27:31 +00001850 }
1851#endif
drhf3388142004-11-13 03:48:06 +00001852 if( pTab->readOnly || pTab==db->aDb[iDb].pSeqTab ){
danielk1977a8858102004-05-28 12:11:21 +00001853 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
danielk1977a8858102004-05-28 12:11:21 +00001854 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001855 }
danielk1977576ec6b2005-01-21 11:55:25 +00001856
1857#ifndef SQLITE_OMIT_VIEW
1858 /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
1859 ** on a table.
1860 */
danielk1977a8858102004-05-28 12:11:21 +00001861 if( isView && pTab->pSelect==0 ){
1862 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1863 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001864 }
danielk1977a8858102004-05-28 12:11:21 +00001865 if( !isView && pTab->pSelect ){
1866 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1867 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001868 }
danielk1977576ec6b2005-01-21 11:55:25 +00001869#endif
drh75897232000-05-29 14:26:00 +00001870
drh1ccde152000-06-17 13:12:39 +00001871 /* Generate code to remove the table from the master table
1872 ** on disk.
1873 */
danielk19774adee202004-05-08 08:23:19 +00001874 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001875 if( v ){
drhe0bc4042002-06-25 01:09:11 +00001876 Trigger *pTrigger;
drh2958a4e2004-11-12 03:56:15 +00001877 int iDb = pTab->iDb;
1878 Db *pDb = &db->aDb[iDb];
1879 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh8bf8dc92003-05-17 17:35:10 +00001880
danielk19778e227872004-06-07 07:52:17 +00001881 /* Drop all triggers associated with the table being dropped. Code
1882 ** is generated to remove entries from sqlite_master and/or
1883 ** sqlite_temp_master if required.
1884 */
danielk1977a8858102004-05-28 12:11:21 +00001885 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001886 while( pTrigger ){
drh2958a4e2004-11-12 03:56:15 +00001887 assert( pTrigger->iDb==iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001888 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drh956bc922004-07-24 17:38:29 +00001889 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00001890 }
drh8bf8dc92003-05-17 17:35:10 +00001891
danielk19774d36b812004-11-19 07:07:30 +00001892#ifndef SQLITE_OMIT_AUTOINCREMENT
1893 /* Remove any entries of the sqlite_sequence table associated with
1894 ** the table being dropped. This is done before the table is dropped
1895 ** at the btree level, in case the sqlite_sequence table needs to
1896 ** move as a result of the drop (can happen in auto-vacuum mode).
1897 */
1898 if( pTab->autoInc ){
1899 sqlite3NestedParse(pParse,
1900 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
1901 pDb->zName, pTab->zName
1902 );
1903 }
1904#endif
1905
danielk19778e227872004-06-07 07:52:17 +00001906 /* Drop all SQLITE_MASTER table and index entries that refer to the
1907 ** table. The program name loops through the master table and deletes
1908 ** every row that refers to a table of the same name as the one being
1909 ** dropped. Triggers are handled seperately because a trigger can be
1910 ** created in the temp database that refers to a table in another
1911 ** database.
1912 */
drhf1974842004-11-05 03:56:00 +00001913 sqlite3NestedParse(pParse,
drh4794f732004-11-05 17:17:50 +00001914 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
drh2958a4e2004-11-12 03:56:15 +00001915 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
drh4ff6dfa2002-03-03 23:06:00 +00001916 if( !isView ){
drh4e0cff62004-11-05 05:10:28 +00001917 destroyTable(pParse, pTab);
drh5e00f6c2001-09-13 13:46:56 +00001918 }
drh2958a4e2004-11-12 03:56:15 +00001919
danielk1977a21c6b62005-01-24 10:25:59 +00001920 /* Remove the table entry from SQLite's internal schema and modify
1921 ** the schema cookie.
drh2958a4e2004-11-12 03:56:15 +00001922 */
1923 sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
danielk1977a21c6b62005-01-24 10:25:59 +00001924 sqlite3ChangeCookie(db, v, iDb);
drh75897232000-05-29 14:26:00 +00001925 }
drhd24cc422003-03-27 12:51:24 +00001926 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001927
1928exit_drop_table:
1929 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001930}
1931
1932/*
drhc2eef3b2002-08-31 18:53:06 +00001933** This routine is called to create a new foreign key on the table
1934** currently under construction. pFromCol determines which columns
1935** in the current table point to the foreign key. If pFromCol==0 then
1936** connect the key to the last column inserted. pTo is the name of
1937** the table referred to. pToCol is a list of tables in the other
1938** pTo table that the foreign key points to. flags contains all
1939** information about the conflict resolution algorithms specified
1940** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1941**
1942** An FKey structure is created and added to the table currently
1943** under construction in the pParse->pNewTable field. The new FKey
1944** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001945** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001946**
1947** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001948** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001949*/
danielk19774adee202004-05-08 08:23:19 +00001950void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001951 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001952 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001953 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001954 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001955 int flags /* Conflict resolution algorithms. */
1956){
drhb7f91642004-10-31 02:22:47 +00001957#ifndef SQLITE_OMIT_FOREIGN_KEY
drh40e016e2004-11-04 14:47:11 +00001958 FKey *pFKey = 0;
drhc2eef3b2002-08-31 18:53:06 +00001959 Table *p = pParse->pNewTable;
1960 int nByte;
1961 int i;
1962 int nCol;
1963 char *z;
drhc2eef3b2002-08-31 18:53:06 +00001964
1965 assert( pTo!=0 );
1966 if( p==0 || pParse->nErr ) goto fk_end;
1967 if( pFromCol==0 ){
1968 int iCol = p->nCol-1;
1969 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001970 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001971 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001972 " should reference only one column of table %T",
1973 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001974 goto fk_end;
1975 }
1976 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001977 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001978 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001979 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001980 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001981 goto fk_end;
1982 }else{
danielk19770202b292004-06-09 09:55:16 +00001983 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001984 }
1985 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1986 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001987 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001988 nByte += strlen(pToCol->a[i].zName) + 1;
1989 }
1990 }
1991 pFKey = sqliteMalloc( nByte );
1992 if( pFKey==0 ) goto fk_end;
1993 pFKey->pFrom = p;
1994 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001995 z = (char*)&pFKey[1];
1996 pFKey->aCol = (struct sColMap*)z;
1997 z += sizeof(struct sColMap)*nCol;
1998 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001999 memcpy(z, pTo->z, pTo->n);
2000 z[pTo->n] = 0;
2001 z += pTo->n+1;
2002 pFKey->pNextTo = 0;
2003 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00002004 if( pFromCol==0 ){
2005 pFKey->aCol[0].iFrom = p->nCol-1;
2006 }else{
2007 for(i=0; i<nCol; i++){
2008 int j;
2009 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002010 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00002011 pFKey->aCol[i].iFrom = j;
2012 break;
2013 }
2014 }
2015 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002016 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00002017 "unknown column \"%s\" in foreign key definition",
2018 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00002019 goto fk_end;
2020 }
2021 }
2022 }
2023 if( pToCol ){
2024 for(i=0; i<nCol; i++){
2025 int n = strlen(pToCol->a[i].zName);
2026 pFKey->aCol[i].zCol = z;
2027 memcpy(z, pToCol->a[i].zName, n);
2028 z[n] = 0;
2029 z += n+1;
2030 }
2031 }
2032 pFKey->isDeferred = 0;
2033 pFKey->deleteConf = flags & 0xff;
2034 pFKey->updateConf = (flags >> 8 ) & 0xff;
2035 pFKey->insertConf = (flags >> 16 ) & 0xff;
2036
2037 /* Link the foreign key to the table as the last step.
2038 */
2039 p->pFKey = pFKey;
2040 pFKey = 0;
2041
2042fk_end:
2043 sqliteFree(pFKey);
drhb7f91642004-10-31 02:22:47 +00002044#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
danielk19770202b292004-06-09 09:55:16 +00002045 sqlite3ExprListDelete(pFromCol);
2046 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00002047}
2048
2049/*
2050** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
2051** clause is seen as part of a foreign key definition. The isDeferred
2052** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
2053** The behavior of the most recently created foreign key is adjusted
2054** accordingly.
2055*/
danielk19774adee202004-05-08 08:23:19 +00002056void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhb7f91642004-10-31 02:22:47 +00002057#ifndef SQLITE_OMIT_FOREIGN_KEY
drhc2eef3b2002-08-31 18:53:06 +00002058 Table *pTab;
2059 FKey *pFKey;
2060 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
2061 pFKey->isDeferred = isDeferred;
drhb7f91642004-10-31 02:22:47 +00002062#endif
drhc2eef3b2002-08-31 18:53:06 +00002063}
2064
2065/*
drh063336a2004-11-05 20:58:39 +00002066** Generate code that will erase and refill index *pIdx. This is
2067** used to initialize a newly created index or to recompute the
2068** content of an index in response to a REINDEX command.
2069**
2070** if memRootPage is not negative, it means that the index is newly
2071** created. The memory cell specified by memRootPage contains the
2072** root page number of the index. If memRootPage is negative, then
2073** the index already exists and must be cleared before being refilled and
2074** the root page number of the index is taken from pIndex->tnum.
2075*/
2076static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
2077 Table *pTab = pIndex->pTable; /* The table that is indexed */
2078 int iTab = pParse->nTab; /* Btree cursor used for pTab */
2079 int iIdx = pParse->nTab+1; /* Btree cursor used for pIndex */
2080 int addr1; /* Address of top of loop */
2081 int tnum; /* Root page of index */
2082 Vdbe *v; /* Generate code into this virtual machine */
drh4343fea2004-11-05 23:46:15 +00002083 int isUnique; /* True for a unique index */
drh063336a2004-11-05 20:58:39 +00002084
danielk19771d54df82004-11-23 15:41:16 +00002085#ifndef SQLITE_OMIT_AUTHORIZATION
2086 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
2087 pParse->db->aDb[pIndex->iDb].zName ) ){
2088 return;
2089 }
2090#endif
2091
danielk197733a5edc2005-01-27 00:22:02 +00002092 /* Ensure all the required collation sequences are available. This
2093 ** routine will invoke the collation-needed callback if necessary (and
2094 ** if one has been registered).
2095 */
2096 if( sqlite3CheckIndexCollSeq(pParse, pIndex) ){
2097 return;
2098 }
2099
drh063336a2004-11-05 20:58:39 +00002100 v = sqlite3GetVdbe(pParse);
2101 if( v==0 ) return;
2102 if( memRootPage>=0 ){
2103 sqlite3VdbeAddOp(v, OP_MemLoad, memRootPage, 0);
2104 tnum = 0;
2105 }else{
2106 tnum = pIndex->tnum;
2107 sqlite3VdbeAddOp(v, OP_Clear, tnum, pIndex->iDb);
2108 }
2109 sqlite3VdbeAddOp(v, OP_Integer, pIndex->iDb, 0);
2110 sqlite3VdbeOp3(v, OP_OpenWrite, iIdx, tnum,
2111 (char*)&pIndex->keyInfo, P3_KEYINFO);
2112 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
2113 sqlite3VdbeAddOp(v, OP_OpenRead, iTab, pTab->tnum);
2114 sqlite3VdbeAddOp(v, OP_SetNumColumns, iTab, pTab->nCol);
2115 addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0);
2116 sqlite3GenerateIndexKey(v, pIndex, iTab);
drh4343fea2004-11-05 23:46:15 +00002117 isUnique = pIndex->onError!=OE_None;
2118 sqlite3VdbeAddOp(v, OP_IdxPut, iIdx, isUnique);
2119 if( isUnique ){
2120 sqlite3VdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
2121 }
drh063336a2004-11-05 20:58:39 +00002122 sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1);
2123 sqlite3VdbeChangeP2(v, addr1, sqlite3VdbeCurrentAddr(v));
2124 sqlite3VdbeAddOp(v, OP_Close, iTab, 0);
2125 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
2126}
2127
2128/*
drh23bf66d2004-12-14 03:34:34 +00002129** Create a new index for an SQL table. pName1.pName2 is the name of the index
2130** and pTblList is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00002131** be NULL for a primary key or an index that is created to satisfy a
2132** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00002133** as the table to be indexed. pParse->pNewTable is a table that is
2134** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00002135**
drh382c0242001-10-06 16:33:02 +00002136** pList is a list of columns to be indexed. pList will be NULL if this
2137** is a primary key or unique-constraint on the most recent column added
2138** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00002139*/
danielk19774adee202004-05-08 08:23:19 +00002140void sqlite3CreateIndex(
drh23bf66d2004-12-14 03:34:34 +00002141 Parse *pParse, /* All information about this parse */
2142 Token *pName1, /* First part of index name. May be NULL */
2143 Token *pName2, /* Second part of index name. May be NULL */
2144 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
danielk19770202b292004-06-09 09:55:16 +00002145 ExprList *pList, /* A list of columns to be indexed */
drh23bf66d2004-12-14 03:34:34 +00002146 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
2147 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
2148 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
drh75897232000-05-29 14:26:00 +00002149){
drh23bf66d2004-12-14 03:34:34 +00002150 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00002151 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00002152 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00002153 int i, j;
drhf26e09c2003-05-31 16:21:12 +00002154 Token nullId; /* Fake token for an empty ID list */
2155 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00002156 int isTemp; /* True for a temporary index */
drh9bb575f2004-09-06 17:24:11 +00002157 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002158
danielk1977cbb18d22004-05-28 11:37:27 +00002159 int iDb; /* Index of the database that is being written */
2160 Token *pName = 0; /* Unqualified name of the index to create */
2161
danielk197724b03fd2004-05-10 10:34:34 +00002162 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00002163
drh75897232000-05-29 14:26:00 +00002164 /*
2165 ** Find the table that is to be indexed. Return early if not found.
2166 */
danielk1977cbb18d22004-05-28 11:37:27 +00002167 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002168
2169 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00002170 ** to search for the table. 'Fix' the table name to this db
2171 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00002172 */
2173 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00002174 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00002175 if( iDb<0 ) goto exit_create_index;
2176
danielk1977ef2cb632004-05-29 02:37:19 +00002177 /* If the index name was unqualified, check if the the table
2178 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00002179 */
danielk1977ef2cb632004-05-29 02:37:19 +00002180 pTab = sqlite3SrcListLookup(pParse, pTblName);
2181 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
2182 iDb = 1;
2183 }
2184
2185 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
2186 sqlite3FixSrcList(&sFix, pTblName)
2187 ){
danielk1977cbb18d22004-05-28 11:37:27 +00002188 goto exit_create_index;
2189 }
danielk1977ef2cb632004-05-29 02:37:19 +00002190 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
2191 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00002192 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00002193 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00002194 }else{
drhe3c41372001-09-17 20:25:58 +00002195 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00002196 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00002197 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00002198 }
danielk1977cbb18d22004-05-28 11:37:27 +00002199
drh75897232000-05-29 14:26:00 +00002200 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00002201 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00002202 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00002203 goto exit_create_index;
2204 }
danielk1977576ec6b2005-01-21 11:55:25 +00002205#ifndef SQLITE_OMIT_VIEW
drha76b5df2002-02-23 02:32:10 +00002206 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00002207 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00002208 goto exit_create_index;
2209 }
danielk1977576ec6b2005-01-21 11:55:25 +00002210#endif
drh4925ca02003-11-27 00:48:57 +00002211 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00002212
2213 /*
2214 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00002215 ** index or table with the same name.
2216 **
2217 ** Exception: If we are reading the names of permanent indices from the
2218 ** sqlite_master table (because some other process changed the schema) and
2219 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00002220 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00002221 **
2222 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00002223 ** dealing with a primary key or UNIQUE constraint. We have to invent our
2224 ** own name.
drh75897232000-05-29 14:26:00 +00002225 */
danielk1977d8123362004-06-12 09:25:12 +00002226 if( pName ){
drha99db3b2004-06-19 14:49:12 +00002227 zName = sqlite3NameFromToken(pName);
danielk19778a414492004-06-29 08:59:35 +00002228 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002229 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002230 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00002231 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00002232 }
danielk1977d8123362004-06-12 09:25:12 +00002233 if( !db->init.busy ){
2234 Index *pISameName; /* Another index with the same name */
2235 Table *pTSameName; /* A table with same name as the index */
danielk19778a414492004-06-29 08:59:35 +00002236 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00002237 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
2238 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
2239 goto exit_create_index;
2240 }
2241 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
2242 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
2243 goto exit_create_index;
2244 }
drhe3c41372001-09-17 20:25:58 +00002245 }
danielk1977a21c6b62005-01-24 10:25:59 +00002246 }else{
drhadbca9c2001-09-27 15:11:53 +00002247 char zBuf[30];
2248 int n;
2249 Index *pLoop;
2250 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00002251 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00002252 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00002253 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00002254 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00002255 }
2256
drhe5f9c642003-01-13 23:27:31 +00002257 /* Check for authorization to create an index.
2258 */
2259#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00002260 {
2261 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00002262 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002263 goto exit_create_index;
2264 }
2265 i = SQLITE_CREATE_INDEX;
2266 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002267 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00002268 goto exit_create_index;
2269 }
drhe5f9c642003-01-13 23:27:31 +00002270 }
2271#endif
2272
drh75897232000-05-29 14:26:00 +00002273 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00002274 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00002275 ** So create a fake list to simulate this.
2276 */
2277 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00002278 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00002279 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00002280 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00002281 if( pList==0 ) goto exit_create_index;
2282 }
2283
2284 /*
2285 ** Allocate the index structure.
2286 */
drhdcc581c2000-05-30 13:44:19 +00002287 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00002288 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00002289 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00002290 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
2291 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00002292 strcpy(pIndex->zName, zName);
2293 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00002294 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00002295 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00002296 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00002297 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00002298
drh1ccde152000-06-17 13:12:39 +00002299 /* Scan the names of the columns of the table to be indexed and
2300 ** load the column indices into the Index structure. Report an error
2301 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00002302 */
danielk19770202b292004-06-09 09:55:16 +00002303 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00002304 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002305 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002306 }
2307 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002308 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00002309 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002310 goto exit_create_index;
2311 }
drh967e8b72000-06-21 13:59:10 +00002312 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00002313 if( pList->a[i].pExpr ){
2314 assert( pList->a[i].pExpr->pColl );
2315 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
2316 }else{
2317 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
2318 }
2319 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00002320 if( !db->init.busy &&
2321 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2322 ){
2323 goto exit_create_index;
2324 }
drh75897232000-05-29 14:26:00 +00002325 }
danielk19770202b292004-06-09 09:55:16 +00002326 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002327
danielk1977d8123362004-06-12 09:25:12 +00002328 if( pTab==pParse->pNewTable ){
2329 /* This routine has been called to create an automatic index as a
2330 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2331 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2332 ** i.e. one of:
2333 **
2334 ** CREATE TABLE t(x PRIMARY KEY, y);
2335 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2336 **
2337 ** Either way, check to see if the table already has such an index. If
2338 ** so, don't bother creating this one. This only applies to
2339 ** automatically created indices. Users can do as they wish with
2340 ** explicit indices.
2341 */
2342 Index *pIdx;
2343 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2344 int k;
2345 assert( pIdx->onError!=OE_None );
2346 assert( pIdx->autoIndex );
2347 assert( pIndex->onError!=OE_None );
2348
2349 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2350 for(k=0; k<pIdx->nColumn; k++){
2351 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2352 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2353 }
2354 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002355 if( pIdx->onError!=pIndex->onError ){
2356 /* This constraint creates the same index as a previous
2357 ** constraint specified somewhere in the CREATE TABLE statement.
2358 ** However the ON CONFLICT clauses are different. If both this
2359 ** constraint and the previous equivalent constraint have explicit
2360 ** ON CONFLICT clauses this is an error. Otherwise, use the
2361 ** explicitly specified behaviour for the index.
2362 */
2363 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2364 sqlite3ErrorMsg(pParse,
2365 "conflicting ON CONFLICT clauses specified", 0);
2366 }
2367 if( pIdx->onError==OE_Default ){
2368 pIdx->onError = pIndex->onError;
2369 }
2370 }
danielk1977d8123362004-06-12 09:25:12 +00002371 goto exit_create_index;
2372 }
2373 }
2374 }
2375
drh75897232000-05-29 14:26:00 +00002376 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002377 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002378 */
drh234c39d2004-07-24 03:30:47 +00002379 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002380 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002381 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002382 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002383 if( p ){
2384 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002385 goto exit_create_index;
2386 }
drh5e00f6c2001-09-13 13:46:56 +00002387 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002388 if( pTblName!=0 ){
2389 pIndex->tnum = db->init.newTnum;
2390 }
drhd78eeee2001-09-13 16:18:53 +00002391 }
2392
drh1d85d932004-02-14 23:05:52 +00002393 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002394 ** involves writing the index into the master table and filling in the
2395 ** index with the current table contents.
2396 **
drh1d85d932004-02-14 23:05:52 +00002397 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2398 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002399 ** CREATE INDEX statements are read out of the master table. In
2400 ** the latter case the index already exists on disk, which is why
2401 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002402 **
danielk1977cbb18d22004-05-28 11:37:27 +00002403 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002404 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2405 ** has just been created, it contains no data and the index initialization
2406 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002407 */
drh1d85d932004-02-14 23:05:52 +00002408 else if( db->init.busy==0 ){
drhadbca9c2001-09-27 15:11:53 +00002409 Vdbe *v;
drh063336a2004-11-05 20:58:39 +00002410 char *zStmt;
2411 int iMem = pParse->nMem++;
drh75897232000-05-29 14:26:00 +00002412
danielk19774adee202004-05-08 08:23:19 +00002413 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002414 if( v==0 ) goto exit_create_index;
drh063336a2004-11-05 20:58:39 +00002415
2416 /* Create the rootpage for the index
2417 */
2418 sqlite3BeginWriteOperation(pParse, 0, iDb);
drh234c39d2004-07-24 03:30:47 +00002419 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
drh063336a2004-11-05 20:58:39 +00002420 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2421
2422 /* Gather the complete text of the CREATE INDEX statement into
2423 ** the zStmt variable
2424 */
drhe0bc4042002-06-25 01:09:11 +00002425 if( pStart && pEnd ){
drh063336a2004-11-05 20:58:39 +00002426 /* A named index with an explicit CREATE INDEX statement */
danielk19779fd2a9a2004-11-12 13:42:30 +00002427 zStmt = sqlite3MPrintf("CREATE%s INDEX %.*s",
drh063336a2004-11-05 20:58:39 +00002428 onError==OE_None ? "" : " UNIQUE",
2429 Addr(pEnd->z) - Addr(pName->z) + 1,
2430 pName->z);
2431 }else{
2432 /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
drhe497f002004-11-07 13:01:49 +00002433 /* zStmt = sqlite3MPrintf(""); */
2434 zStmt = 0;
drh75897232000-05-29 14:26:00 +00002435 }
drh063336a2004-11-05 20:58:39 +00002436
2437 /* Add an entry in sqlite_master for this index
2438 */
2439 sqlite3NestedParse(pParse,
drhe497f002004-11-07 13:01:49 +00002440 "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#0,%Q);",
drh063336a2004-11-05 20:58:39 +00002441 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2442 pIndex->zName,
2443 pTab->zName,
2444 zStmt
2445 );
2446 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2447 sqliteFree(zStmt);
2448
danielk1977a21c6b62005-01-24 10:25:59 +00002449 /* Fill the index with data and reparse the schema. Code an OP_Expire
2450 ** to invalidate all pre-compiled statements.
drh063336a2004-11-05 20:58:39 +00002451 */
danielk1977cbb18d22004-05-28 11:37:27 +00002452 if( pTblName ){
drh063336a2004-11-05 20:58:39 +00002453 sqlite3RefillIndex(pParse, pIndex, iMem);
drhc275b4e2004-07-19 17:25:24 +00002454 sqlite3ChangeCookie(db, v, iDb);
drh234c39d2004-07-24 03:30:47 +00002455 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
2456 sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC);
danielk1977a21c6b62005-01-24 10:25:59 +00002457 sqlite3VdbeAddOp(v, OP_Expire, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002458 }
drh75897232000-05-29 14:26:00 +00002459 }
2460
danielk1977d8123362004-06-12 09:25:12 +00002461 /* When adding an index to the list of indices for a table, make
2462 ** sure all indices labeled OE_Replace come after all those labeled
2463 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2464 ** and INSERT.
2465 */
drh234c39d2004-07-24 03:30:47 +00002466 if( db->init.busy || pTblName==0 ){
2467 if( onError!=OE_Replace || pTab->pIndex==0
2468 || pTab->pIndex->onError==OE_Replace){
2469 pIndex->pNext = pTab->pIndex;
2470 pTab->pIndex = pIndex;
2471 }else{
2472 Index *pOther = pTab->pIndex;
2473 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2474 pOther = pOther->pNext;
2475 }
2476 pIndex->pNext = pOther->pNext;
2477 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002478 }
drh234c39d2004-07-24 03:30:47 +00002479 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002480 }
danielk1977d8123362004-06-12 09:25:12 +00002481
drh75897232000-05-29 14:26:00 +00002482 /* Clean up before exiting */
2483exit_create_index:
drh956bc922004-07-24 17:38:29 +00002484 if( pIndex ){
2485 freeIndex(pIndex);
2486 }
danielk19770202b292004-06-09 09:55:16 +00002487 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002488 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002489 sqliteFree(zName);
2490 return;
2491}
2492
2493/*
drh74e24cd2002-01-09 03:19:59 +00002494** This routine will drop an existing named index. This routine
2495** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002496*/
danielk19774adee202004-05-08 08:23:19 +00002497void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002498 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002499 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00002500 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002501
danielk197724b03fd2004-05-10 10:34:34 +00002502 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002503 assert( pName->nSrc==1 );
danielk19778a414492004-06-29 08:59:35 +00002504 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19774adee202004-05-08 08:23:19 +00002505 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002506 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002507 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002508 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002509 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002510 }
drh485b39b2002-07-13 03:11:52 +00002511 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002512 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002513 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002514 goto exit_drop_index;
2515 }
drhe5f9c642003-01-13 23:27:31 +00002516#ifndef SQLITE_OMIT_AUTHORIZATION
2517 {
2518 int code = SQLITE_DROP_INDEX;
2519 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002520 const char *zDb = db->aDb[pIndex->iDb].zName;
2521 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002522 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002523 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002524 }
drhd24cc422003-03-27 12:51:24 +00002525 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002526 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002527 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002528 }
drhed6c8672003-01-12 18:02:16 +00002529 }
drhe5f9c642003-01-13 23:27:31 +00002530#endif
drh75897232000-05-29 14:26:00 +00002531
2532 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002533 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002534 if( v ){
drhb17131a2004-11-05 22:18:49 +00002535 int iDb = pIndex->iDb;
2536 sqlite3NestedParse(pParse,
2537 "DELETE FROM %Q.%s WHERE name=%Q",
2538 db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
2539 pIndex->zName
2540 );
2541 sqlite3ChangeCookie(db, v, iDb);
2542 destroyRootPage(pParse, pIndex->tnum, iDb);
2543 sqlite3VdbeOp3(v, OP_DropIndex, iDb, 0, pIndex->zName, 0);
drh75897232000-05-29 14:26:00 +00002544 }
2545
drhd24cc422003-03-27 12:51:24 +00002546exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002547 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002548}
2549
2550/*
drh75897232000-05-29 14:26:00 +00002551** Append a new element to the given IdList. Create a new IdList if
2552** need be.
drhdaffd0e2001-04-11 14:28:42 +00002553**
2554** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002555*/
danielk19774adee202004-05-08 08:23:19 +00002556IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002557 if( pList==0 ){
2558 pList = sqliteMalloc( sizeof(IdList) );
2559 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002560 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002561 }
drh4305d102003-07-30 12:34:12 +00002562 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002563 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002564 pList->nAlloc = pList->nAlloc*2 + 5;
2565 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002566 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002567 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002568 return 0;
drh75897232000-05-29 14:26:00 +00002569 }
drh6d4abfb2001-10-22 02:58:08 +00002570 pList->a = a;
drh75897232000-05-29 14:26:00 +00002571 }
2572 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
drha99db3b2004-06-19 14:49:12 +00002573 pList->a[pList->nId].zName = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002574 pList->nId++;
2575 return pList;
2576}
2577
2578/*
drhad3cab52002-05-24 02:04:32 +00002579** Append a new table name to the given SrcList. Create a new SrcList if
2580** need be. A new entry is created in the SrcList even if pToken is NULL.
2581**
2582** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002583**
2584** If pDatabase is not null, it means that the table has an optional
2585** database name prefix. Like this: "database.table". The pDatabase
2586** points to the table name and the pTable points to the database name.
2587** The SrcList.a[].zName field is filled with the table name which might
2588** come from pTable (if pDatabase is NULL) or from pDatabase.
2589** SrcList.a[].zDatabase is filled with the database name from pTable,
2590** or with NULL if no database is specified.
2591**
2592** In other words, if call like this:
2593**
danielk19774adee202004-05-08 08:23:19 +00002594** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002595**
2596** Then B is a table name and the database name is unspecified. If called
2597** like this:
2598**
danielk19774adee202004-05-08 08:23:19 +00002599** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002600**
2601** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002602*/
danielk19774adee202004-05-08 08:23:19 +00002603SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drha99db3b2004-06-19 14:49:12 +00002604 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002605 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002606 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002607 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002608 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002609 }
drh4305d102003-07-30 12:34:12 +00002610 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002611 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002612 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002613 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002614 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002615 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002616 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002617 return 0;
2618 }
drh113088e2003-03-20 01:16:58 +00002619 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002620 }
drha99db3b2004-06-19 14:49:12 +00002621 pItem = &pList->a[pList->nSrc];
2622 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002623 if( pDatabase && pDatabase->z==0 ){
2624 pDatabase = 0;
2625 }
2626 if( pDatabase && pTable ){
2627 Token *pTemp = pDatabase;
2628 pDatabase = pTable;
2629 pTable = pTemp;
2630 }
drha99db3b2004-06-19 14:49:12 +00002631 pItem->zName = sqlite3NameFromToken(pTable);
2632 pItem->zDatabase = sqlite3NameFromToken(pDatabase);
2633 pItem->iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002634 pList->nSrc++;
2635 return pList;
2636}
2637
2638/*
drh63eb5f22003-04-29 16:20:44 +00002639** Assign cursors to all tables in a SrcList
2640*/
danielk19774adee202004-05-08 08:23:19 +00002641void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002642 int i;
drh9b3187e2005-01-18 14:45:47 +00002643 struct SrcList_item *pItem;
2644 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
2645 if( pItem->iCursor>=0 ) break;
2646 pItem->iCursor = pParse->nTab++;
2647 if( pItem->pSelect ){
2648 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
drh63eb5f22003-04-29 16:20:44 +00002649 }
2650 }
2651}
2652
2653/*
drh75897232000-05-29 14:26:00 +00002654** Add an alias to the last identifier on the given identifier list.
2655*/
danielk19774adee202004-05-08 08:23:19 +00002656void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002657 if( pList && pList->nSrc>0 ){
drha99db3b2004-06-19 14:49:12 +00002658 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002659 }
2660}
2661
2662/*
drhad3cab52002-05-24 02:04:32 +00002663** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002664*/
danielk19774adee202004-05-08 08:23:19 +00002665void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002666 int i;
2667 if( pList==0 ) return;
2668 for(i=0; i<pList->nId; i++){
2669 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002670 }
2671 sqliteFree(pList->a);
2672 sqliteFree(pList);
2673}
2674
2675/*
drhad2d8302002-05-24 20:31:36 +00002676** Return the index in pList of the identifier named zId. Return -1
2677** if not found.
2678*/
danielk19774adee202004-05-08 08:23:19 +00002679int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002680 int i;
2681 if( pList==0 ) return -1;
2682 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002683 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002684 }
2685 return -1;
2686}
2687
2688/*
drhad3cab52002-05-24 02:04:32 +00002689** Delete an entire SrcList including all its substructure.
2690*/
danielk19774adee202004-05-08 08:23:19 +00002691void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002692 int i;
drhbe5c89a2004-07-26 00:31:09 +00002693 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002694 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00002695 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
2696 sqliteFree(pItem->zDatabase);
2697 sqliteFree(pItem->zName);
2698 sqliteFree(pItem->zAlias);
2699 if( pItem->pTab && pItem->pTab->isTransient ){
2700 sqlite3DeleteTable(0, pItem->pTab);
drhdaffd0e2001-04-11 14:28:42 +00002701 }
drhbe5c89a2004-07-26 00:31:09 +00002702 sqlite3SelectDelete(pItem->pSelect);
2703 sqlite3ExprDelete(pItem->pOn);
2704 sqlite3IdListDelete(pItem->pUsing);
drh75897232000-05-29 14:26:00 +00002705 }
drh75897232000-05-29 14:26:00 +00002706 sqliteFree(pList);
2707}
2708
drh982cef72000-05-30 16:27:03 +00002709/*
drhc4a3c772001-04-04 11:48:57 +00002710** Begin a transaction
2711*/
drh684917c2004-10-05 02:41:42 +00002712void sqlite3BeginTransaction(Parse *pParse, int type){
drh9bb575f2004-09-06 17:24:11 +00002713 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002714 Vdbe *v;
drh684917c2004-10-05 02:41:42 +00002715 int i;
drh5e00f6c2001-09-13 13:46:56 +00002716
drh001bbcb2003-03-19 03:14:00 +00002717 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002718 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002719 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002720
2721 v = sqlite3GetVdbe(pParse);
2722 if( !v ) return;
drh684917c2004-10-05 02:41:42 +00002723 if( type!=TK_DEFERRED ){
2724 for(i=0; i<db->nDb; i++){
2725 sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
2726 }
2727 }
danielk19771d850a72004-05-31 08:26:49 +00002728 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002729}
2730
2731/*
2732** Commit a transaction
2733*/
danielk19774adee202004-05-08 08:23:19 +00002734void sqlite3CommitTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002735 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002736 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002737
drh001bbcb2003-03-19 03:14:00 +00002738 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002739 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002740 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002741
2742 v = sqlite3GetVdbe(pParse);
2743 if( v ){
2744 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002745 }
drhc4a3c772001-04-04 11:48:57 +00002746}
2747
2748/*
2749** Rollback a transaction
2750*/
danielk19774adee202004-05-08 08:23:19 +00002751void sqlite3RollbackTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002752 sqlite3 *db;
drh5e00f6c2001-09-13 13:46:56 +00002753 Vdbe *v;
2754
drh001bbcb2003-03-19 03:14:00 +00002755 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002756 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002757 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002758
danielk19774adee202004-05-08 08:23:19 +00002759 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002760 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002761 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002762 }
drhc4a3c772001-04-04 11:48:57 +00002763}
drhf57b14a2001-09-14 18:54:08 +00002764
2765/*
drhdc3ff9c2004-08-18 02:10:15 +00002766** Make sure the TEMP database is open and available for use. Return
2767** the number of errors. Leave any error messages in the pParse structure.
2768*/
2769static int sqlite3OpenTempDatabase(Parse *pParse){
2770 sqlite3 *db = pParse->db;
2771 if( db->aDb[1].pBt==0 && !pParse->explain ){
2772 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
2773 if( rc!=SQLITE_OK ){
2774 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
2775 "file for storing temporary tables");
2776 pParse->rc = rc;
2777 return 1;
2778 }
2779 if( db->flags & !db->autoCommit ){
2780 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
2781 if( rc!=SQLITE_OK ){
2782 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
2783 "the temporary database file");
2784 pParse->rc = rc;
2785 return 1;
2786 }
2787 }
2788 }
2789 return 0;
2790}
2791
2792/*
drh80242052004-06-09 00:48:12 +00002793** Generate VDBE code that will verify the schema cookie and start
2794** a read-transaction for all named database files.
2795**
2796** It is important that all schema cookies be verified and all
2797** read transactions be started before anything else happens in
2798** the VDBE program. But this routine can be called after much other
2799** code has been generated. So here is what we do:
2800**
drhc275b4e2004-07-19 17:25:24 +00002801** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00002802** will jump to a subroutine at the end of the program. Then we
2803** record every database that needs its schema verified in the
2804** pParse->cookieMask field. Later, after all other code has been
2805** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00002806** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00002807** will be made to point to that subroutine. The generation of the
2808** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00002809**
2810** If iDb<0 then code the OP_Goto only - don't set flag to verify the
2811** schema on any databases. This can be used to position the OP_Goto
2812** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00002813*/
danielk19774adee202004-05-08 08:23:19 +00002814void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh9bb575f2004-09-06 17:24:11 +00002815 sqlite3 *db;
drh80242052004-06-09 00:48:12 +00002816 Vdbe *v;
2817 int mask;
2818
2819 v = sqlite3GetVdbe(pParse);
2820 if( v==0 ) return; /* This only happens if there was a prior error */
2821 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00002822 if( pParse->cookieGoto==0 ){
2823 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00002824 }
drhc275b4e2004-07-19 17:25:24 +00002825 if( iDb>=0 ){
2826 assert( iDb<db->nDb );
2827 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2828 assert( iDb<32 );
2829 mask = 1<<iDb;
2830 if( (pParse->cookieMask & mask)==0 ){
2831 pParse->cookieMask |= mask;
2832 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
drhdc3ff9c2004-08-18 02:10:15 +00002833 if( iDb==1 ){
2834 sqlite3OpenTempDatabase(pParse);
2835 }
drhc275b4e2004-07-19 17:25:24 +00002836 }
drh001bbcb2003-03-19 03:14:00 +00002837 }
drh001bbcb2003-03-19 03:14:00 +00002838}
2839
2840/*
drh1c928532002-01-31 15:54:21 +00002841** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002842** might change the database.
2843**
2844** This routine starts a new transaction if we are not already within
2845** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002846** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002847** be set for operations that might fail (due to a constraint) part of
2848** the way through and which will need to undo some writes without having to
2849** rollback the whole transaction. For operations where all constraints
2850** can be checked before any changes are made to the database, it is never
2851** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002852**
drh8bf8dc92003-05-17 17:35:10 +00002853** Only database iDb and the temp database are made writable by this call.
2854** If iDb==0, then the main and temp databases are made writable. If
2855** iDb==1 then only the temp database is made writable. If iDb>1 then the
2856** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002857*/
drh7f0f12e2004-05-21 13:39:50 +00002858void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002859 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002860 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002861 sqlite3CodeVerifySchema(pParse, iDb);
2862 pParse->writeMask |= 1<<iDb;
danielk197763e3e9f2004-11-05 09:19:27 +00002863 if( setStatement && pParse->nested==0 ){
drh7f0f12e2004-05-21 13:39:50 +00002864 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002865 }
drh34f47322004-08-18 15:58:22 +00002866 if( iDb!=1 && pParse->db->aDb[1].pBt!=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002867 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002868 }
2869}
2870
danielk1977576ec6b2005-01-21 11:55:25 +00002871#ifndef SQLITE_OMIT_UTF16
danielk1977bfd6cce2004-06-18 04:24:54 +00002872/*
2873** Return the transient sqlite3_value object used for encoding conversions
2874** during SQL compilation.
2875*/
drh9bb575f2004-09-06 17:24:11 +00002876sqlite3_value *sqlite3GetTransientValue(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +00002877 if( !db->pValue ){
2878 db->pValue = sqlite3ValueNew();
2879 }
2880 return db->pValue;
2881}
danielk1977576ec6b2005-01-21 11:55:25 +00002882#endif
drh4343fea2004-11-05 23:46:15 +00002883
2884/*
2885** Check to see if pIndex uses the collating sequence pColl. Return
2886** true if it does and false if it does not.
2887*/
2888#ifndef SQLITE_OMIT_REINDEX
2889static int collationMatch(CollSeq *pColl, Index *pIndex){
2890 int n = pIndex->keyInfo.nField;
2891 CollSeq **pp = pIndex->keyInfo.aColl;
2892 while( n-- ){
2893 if( *pp==pColl ) return 1;
2894 pp++;
2895 }
2896 return 0;
2897}
2898#endif
2899
2900/*
2901** Recompute all indices of pTab that use the collating sequence pColl.
2902** If pColl==0 then recompute all indices of pTab.
2903*/
2904#ifndef SQLITE_OMIT_REINDEX
2905void reindexTable(Parse *pParse, Table *pTab, CollSeq *pColl){
2906 Index *pIndex; /* An index associated with pTab */
2907
2908 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
2909 if( pColl==0 || collationMatch(pColl,pIndex) ){
2910 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
2911 sqlite3RefillIndex(pParse, pIndex, -1);
2912 }
2913 }
2914}
2915#endif
2916
2917/*
2918** Recompute all indices of all tables in all databases where the
2919** indices use the collating sequence pColl. If pColl==0 then recompute
2920** all indices everywhere.
2921*/
2922#ifndef SQLITE_OMIT_REINDEX
2923void reindexDatabases(Parse *pParse, CollSeq *pColl){
2924 Db *pDb; /* A single database */
2925 int iDb; /* The database index number */
2926 sqlite3 *db = pParse->db; /* The database connection */
2927 HashElem *k; /* For looping over tables in pDb */
2928 Table *pTab; /* A table in the database */
2929
2930 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
2931 if( pDb==0 ) continue;
2932 for(k=sqliteHashFirst(&pDb->tblHash); k; k=sqliteHashNext(k)){
2933 pTab = (Table*)sqliteHashData(k);
2934 reindexTable(pParse, pTab, pColl);
2935 }
2936 }
2937}
2938#endif
2939
2940/*
drheee46cf2004-11-06 00:02:48 +00002941** Generate code for the REINDEX command.
2942**
2943** REINDEX -- 1
2944** REINDEX <collation> -- 2
2945** REINDEX ?<database>.?<tablename> -- 3
2946** REINDEX ?<database>.?<indexname> -- 4
2947**
2948** Form 1 causes all indices in all attached databases to be rebuilt.
2949** Form 2 rebuilds all indices in all databases that use the named
2950** collating function. Forms 3 and 4 rebuild the named index or all
2951** indices associated with the named table.
drh4343fea2004-11-05 23:46:15 +00002952*/
2953#ifndef SQLITE_OMIT_REINDEX
2954void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
2955 CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */
2956 char *z; /* Name of a table or index */
2957 const char *zDb; /* Name of the database */
2958 Table *pTab; /* A table in the database */
2959 Index *pIndex; /* An index associated with pTab */
2960 int iDb; /* The database index number */
2961 sqlite3 *db = pParse->db; /* The database connection */
2962 Token *pObjName; /* Name of the table or index to be reindexed */
2963
danielk197733a5edc2005-01-27 00:22:02 +00002964 /* Read the database schema. If an error occurs, leave an error message
2965 ** and code in pParse and return NULL. */
2966 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
danielk1977e63739a2005-01-27 00:33:37 +00002967 return;
danielk197733a5edc2005-01-27 00:22:02 +00002968 }
2969
drhe497f002004-11-07 13:01:49 +00002970 if( pName1==0 || pName1->z==0 ){
drh4343fea2004-11-05 23:46:15 +00002971 reindexDatabases(pParse, 0);
2972 return;
drhe497f002004-11-07 13:01:49 +00002973 }else if( pName2==0 || pName2->z==0 ){
drh4343fea2004-11-05 23:46:15 +00002974 pColl = sqlite3FindCollSeq(db, db->enc, pName1->z, pName1->n, 0);
2975 if( pColl ){
2976 reindexDatabases(pParse, pColl);
2977 return;
2978 }
2979 }
2980 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
2981 if( iDb<0 ) return;
2982 z = sqlite3NameFromToken(pObjName);
2983 zDb = db->aDb[iDb].zName;
2984 pTab = sqlite3FindTable(db, z, zDb);
2985 if( pTab ){
2986 reindexTable(pParse, pTab, 0);
2987 sqliteFree(z);
2988 return;
2989 }
2990 pIndex = sqlite3FindIndex(db, z, zDb);
2991 sqliteFree(z);
2992 if( pIndex ){
2993 sqlite3BeginWriteOperation(pParse, 0, iDb);
2994 sqlite3RefillIndex(pParse, pIndex, -1);
2995 return;
2996 }
2997 sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
2998}
2999#endif
danielk19779fd2a9a2004-11-12 13:42:30 +00003000
3001#ifndef SQLITE_OMIT_ALTERTABLE
3002/*
3003** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
3004** command.
3005*/
3006void sqlite3AlterRenameTable(
3007 Parse *pParse, /* Parser context. */
3008 SrcList *pSrc, /* The table to rename. */
3009 Token *pName /* The new table name. */
3010){
3011 int iDb; /* Database that contains the table */
danielk19771c8c23c2004-11-12 15:53:37 +00003012 char *zDb; /* Name of database iDb */
danielk19779fd2a9a2004-11-12 13:42:30 +00003013 Table *pTab; /* Table being renamed */
danielk19779fd2a9a2004-11-12 13:42:30 +00003014 char *zName = 0; /* NULL-terminated version of pName */
3015 char *zWhere = 0; /* Where clause of schema elements to reparse */
danielk19771c8c23c2004-11-12 15:53:37 +00003016 sqlite3 *db = pParse->db; /* Database connection */
danielk19779fd2a9a2004-11-12 13:42:30 +00003017 Vdbe *v;
danielk1977343e9262004-11-19 05:14:54 +00003018#ifndef SQLITE_OMIT_TRIGGER
3019 char *zTempTrig = 0; /* Where clause to locate temp triggers */
3020#endif
danielk19779fd2a9a2004-11-12 13:42:30 +00003021
3022 assert( pSrc->nSrc==1 );
3023
3024 pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase);
danielk19778b840012004-11-23 16:31:16 +00003025 if( !pTab ) goto exit_alter_table;
danielk19779fd2a9a2004-11-12 13:42:30 +00003026 iDb = pTab->iDb;
danielk19771c8c23c2004-11-12 15:53:37 +00003027 zDb = db->aDb[iDb].zName;
danielk19779fd2a9a2004-11-12 13:42:30 +00003028
3029 /* Get a NULL terminated version of the new table name. */
3030 zName = sqlite3NameFromToken(pName);
danielk19778b840012004-11-23 16:31:16 +00003031 if( !zName ) goto exit_alter_table;
danielk19779fd2a9a2004-11-12 13:42:30 +00003032
3033 /* Check that a table or index named 'zName' does not already exist
3034 ** in database iDb. If so, this is an error.
3035 */
danielk19771c8c23c2004-11-12 15:53:37 +00003036 if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
danielk19779fd2a9a2004-11-12 13:42:30 +00003037 sqlite3ErrorMsg(pParse,
3038 "there is already another table or index with this name: %s", zName);
danielk19778b840012004-11-23 16:31:16 +00003039 goto exit_alter_table;
danielk19779fd2a9a2004-11-12 13:42:30 +00003040 }
3041
danielk1977023f4172004-11-19 08:41:34 +00003042 /* Make sure it is not a system table being altered, or a reserved name
3043 ** that the table is being renamed to.
3044 */
3045 if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
3046 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
danielk19778b840012004-11-23 16:31:16 +00003047 goto exit_alter_table;
danielk1977023f4172004-11-19 08:41:34 +00003048 }
3049 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
danielk19778b840012004-11-23 16:31:16 +00003050 goto exit_alter_table;
danielk1977023f4172004-11-19 08:41:34 +00003051 }
3052
danielk19771c8c23c2004-11-12 15:53:37 +00003053#ifndef SQLITE_OMIT_AUTHORIZATION
3054 /* Invoke the authorization callback. */
3055 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
danielk19778b840012004-11-23 16:31:16 +00003056 goto exit_alter_table;
danielk19771c8c23c2004-11-12 15:53:37 +00003057 }
3058#endif
3059
danielk19779fd2a9a2004-11-12 13:42:30 +00003060 /* Begin a transaction and code the VerifyCookie for database iDb.
3061 ** Then modify the schema cookie (since the ALTER TABLE modifies the
3062 ** schema).
3063 */
3064 v = sqlite3GetVdbe(pParse);
danielk19771c8c23c2004-11-12 15:53:37 +00003065 if( v==0 ){
danielk19778b840012004-11-23 16:31:16 +00003066 goto exit_alter_table;
danielk19771c8c23c2004-11-12 15:53:37 +00003067 }
danielk19779fd2a9a2004-11-12 13:42:30 +00003068 sqlite3BeginWriteOperation(pParse, 0, iDb);
3069 sqlite3ChangeCookie(db, v, iDb);
3070
3071 /* Modify the sqlite_master table to use the new table name. */
3072 sqlite3NestedParse(pParse,
3073 "UPDATE %Q.%s SET "
danielk1977d641d642004-11-18 15:44:29 +00003074#ifdef SQLITE_OMIT_TRIGGER
danielk19779fd2a9a2004-11-12 13:42:30 +00003075 "sql = sqlite_alter_table(sql, %Q), "
danielk1977d641d642004-11-18 15:44:29 +00003076#else
3077 "sql = CASE "
3078 "WHEN type = 'trigger' THEN sqlite_alter_trigger(sql, %Q)"
3079 "ELSE sqlite_alter_table(sql, %Q) END, "
3080#endif
danielk19779fd2a9a2004-11-12 13:42:30 +00003081 "tbl_name = %Q, "
3082 "name = CASE "
3083 "WHEN type='table' THEN %Q "
3084 "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
3085 "'sqlite_autoindex_' || %Q || substr(name, %d+18,10) "
3086 "ELSE name END "
danielk197783715c32005-01-21 00:44:22 +00003087 "WHERE tbl_name=%Q AND "
3088 "(type='table' OR type='index' OR type='trigger');",
danielk197781650dc2004-11-22 11:51:13 +00003089 zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
3090#ifndef SQLITE_OMIT_TRIGGER
3091zName,
3092#endif
danielk19779fd2a9a2004-11-12 13:42:30 +00003093 zName, strlen(pTab->zName), pTab->zName
3094 );
3095
danielk1977aacd7322004-11-19 08:02:14 +00003096#ifndef SQLITE_OMIT_AUTOINCREMENT
3097 /* If the sqlite_sequence table exists in this database, then update
3098 ** it with the new table name.
3099 */
3100 if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
3101 sqlite3NestedParse(pParse,
3102 "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
3103 zDb, zName, pTab->zName);
3104 }
3105#endif
3106
danielk1977343e9262004-11-19 05:14:54 +00003107#ifndef SQLITE_OMIT_TRIGGER
3108 /* If there are TEMP triggers on this table, modify the sqlite_temp_master
3109 ** table. Don't do this if the table being ALTERed is itself located in
3110 ** the temp database.
3111 */
3112 if( iDb!=1 ){
3113 Trigger *pTrig;
3114 char *tmp = 0;
3115 for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
3116 if( pTrig->iDb==1 ){
3117 if( !zTempTrig ){
3118 zTempTrig =
danielk197783715c32005-01-21 00:44:22 +00003119 sqlite3MPrintf("type = 'trigger' AND (name=%Q", pTrig->name);
danielk1977343e9262004-11-19 05:14:54 +00003120 }else{
3121 tmp = zTempTrig;
danielk197783715c32005-01-21 00:44:22 +00003122 zTempTrig = sqlite3MPrintf("%s OR name=%Q", zTempTrig, pTrig->name);
danielk1977343e9262004-11-19 05:14:54 +00003123 sqliteFree(tmp);
3124 }
3125 }
3126 }
3127 if( zTempTrig ){
3128 tmp = zTempTrig;
3129 zTempTrig = sqlite3MPrintf("%s)", zTempTrig);
3130 sqliteFree(tmp);
3131 sqlite3NestedParse(pParse,
3132 "UPDATE sqlite_temp_master SET "
3133 "sql = sqlite_alter_trigger(sql, %Q), "
3134 "tbl_name = %Q "
3135 "WHERE %s;", zName, zName, zTempTrig);
3136 }
3137 }
3138#endif
3139
danielk19779fd2a9a2004-11-12 13:42:30 +00003140 /* Drop the elements of the in-memory schema that refered to the table
3141 ** renamed and load the new versions from the database.
3142 */
3143 if( pParse->nErr==0 ){
danielk1977d641d642004-11-18 15:44:29 +00003144#ifndef SQLITE_OMIT_TRIGGER
3145 Trigger *pTrig;
3146 for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
danielk1977343e9262004-11-19 05:14:54 +00003147 assert( pTrig->iDb==iDb || pTrig->iDb==1 );
3148 sqlite3VdbeOp3(v, OP_DropTrigger, pTrig->iDb, 0, pTrig->name, 0);
danielk1977d641d642004-11-18 15:44:29 +00003149 }
3150#endif
danielk19779fd2a9a2004-11-12 13:42:30 +00003151 sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0);
3152 zWhere = sqlite3MPrintf("tbl_name=%Q", zName);
3153 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC);
danielk1977343e9262004-11-19 05:14:54 +00003154#ifndef SQLITE_OMIT_TRIGGER
3155 if( zTempTrig ){
3156 sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zTempTrig, P3_DYNAMIC);
3157 }
3158 }else{
3159 sqliteFree(zTempTrig);
3160#endif
danielk19779fd2a9a2004-11-12 13:42:30 +00003161 }
3162
danielk19778b840012004-11-23 16:31:16 +00003163exit_alter_table:
3164 sqlite3SrcListDelete(pSrc);
danielk19779fd2a9a2004-11-12 13:42:30 +00003165 sqliteFree(zName);
3166}
3167#endif