blob: 3447898edadeb0b6f2405d786265b21913451f18 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This file contains C code routines that are called by the SQLite parser
13** when syntax rules are reduced. The routines in this file handle the
14** following kinds of SQL syntax:
drh75897232000-05-29 14:26:00 +000015**
drhbed86902000-06-02 13:27:59 +000016** CREATE TABLE
17** DROP TABLE
18** CREATE INDEX
19** DROP INDEX
drh832508b2002-03-02 17:04:07 +000020** creating ID lists
drhb19a2bc2001-09-16 00:13:26 +000021** BEGIN TRANSACTION
22** COMMIT
23** ROLLBACK
24** PRAGMA
drhbed86902000-06-02 13:27:59 +000025**
drh9bb575f2004-09-06 17:24:11 +000026** $Id: build.c,v 1.251 2004/09/06 17:24:12 drh Exp $
drh75897232000-05-29 14:26:00 +000027*/
28#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000029#include <ctype.h>
drh75897232000-05-29 14:26:00 +000030
31/*
drhe0bc4042002-06-25 01:09:11 +000032** This routine is called when a new SQL statement is beginning to
33** be parsed. Check to see if the schema for the database needs
34** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
35** If it does, then read it.
36*/
danielk19774adee202004-05-08 08:23:19 +000037void sqlite3BeginParse(Parse *pParse, int explainFlag){
drhe0bc4042002-06-25 01:09:11 +000038 pParse->explain = explainFlag;
drh7c972de2003-09-06 22:18:07 +000039 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000040}
41
42/*
drh75897232000-05-29 14:26:00 +000043** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12 +000044** parsed and a VDBE program to execute that statement has been
45** prepared. This routine puts the finishing touches on the
46** VDBE program and resets the pParse structure for the next
47** parse.
drh75897232000-05-29 14:26:00 +000048**
49** Note that if an error occurred, it might be the case that
50** no VDBE code was generated.
51*/
drh80242052004-06-09 00:48:12 +000052void sqlite3FinishCoding(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +000053 sqlite3 *db;
drh80242052004-06-09 00:48:12 +000054 Vdbe *v;
drhb86ccfb2003-01-28 23:13:10 +000055
danielk197724b03fd2004-05-10 10:34:34 +000056 if( sqlite3_malloc_failed ) return;
drh80242052004-06-09 00:48:12 +000057
58 /* Begin by generating some termination code at the end of the
59 ** vdbe program
60 */
61 db = pParse->db;
62 v = sqlite3GetVdbe(pParse);
63 if( v ){
64 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
drh0e3d7472004-06-19 17:33:07 +000065
66 /* The cookie mask contains one bit for each database file open.
67 ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are
68 ** set for each database that is used. Generate code to start a
69 ** transaction on each used database and to verify the schema cookie
70 ** on each used database.
71 */
drhc275b4e2004-07-19 17:25:24 +000072 if( pParse->cookieGoto>0 ){
drh80242052004-06-09 00:48:12 +000073 u32 mask;
74 int iDb;
drhc275b4e2004-07-19 17:25:24 +000075 sqlite3VdbeChangeP2(v, pParse->cookieGoto-1, sqlite3VdbeCurrentAddr(v));
drh80242052004-06-09 00:48:12 +000076 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
77 if( (mask & pParse->cookieMask)==0 ) continue;
78 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
drhc275b4e2004-07-19 17:25:24 +000079 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
drh80242052004-06-09 00:48:12 +000080 }
drhc275b4e2004-07-19 17:25:24 +000081 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto);
drh80242052004-06-09 00:48:12 +000082 }
drh80242052004-06-09 00:48:12 +000083
drh3f7d4e42004-07-24 14:35:58 +000084#ifndef NDEBUG
drh71c697e2004-08-08 23:39:19 +000085 /* Add a No-op that contains the complete text of the compiled SQL
86 ** statement as its P3 argument. This does not change the functionality
87 ** of the program. But it does make it easier to debug.
88 */
89 sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql);
drh3f7d4e42004-07-24 14:35:58 +000090#endif
drh71c697e2004-08-08 23:39:19 +000091 }
92
drh3f7d4e42004-07-24 14:35:58 +000093
drh80242052004-06-09 00:48:12 +000094 /* Get the VDBE program ready for execution
95 */
drhb86ccfb2003-01-28 23:13:10 +000096 if( v && pParse->nErr==0 ){
97 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +000098 sqlite3VdbeTrace(v, trace);
drh290c1942004-08-21 17:54:45 +000099 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem+3,
100 pParse->nTab+3, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +0000101 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000102 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +0000103 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +0000104 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000105 }
drha226d052002-09-25 19:04:07 +0000106 pParse->nTab = 0;
107 pParse->nMem = 0;
108 pParse->nSet = 0;
109 pParse->nAgg = 0;
drh7c972de2003-09-06 22:18:07 +0000110 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000111 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000112 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000113}
114
115/*
danielk19778a414492004-06-29 08:59:35 +0000116** Locate the in-memory structure that describes a particular database
117** table given the name of that table and (optionally) the name of the
118** database containing the table. Return NULL if not found.
drha69d9162003-04-17 22:57:53 +0000119**
danielk19778a414492004-06-29 08:59:35 +0000120** If zDatabase is 0, all databases are searched for the table and the
121** first matching table is returned. (No checking for duplicate table
122** names is done.) The search order is TEMP first, then MAIN, then any
123** auxiliary databases added using the ATTACH command.
drhf26e09c2003-05-31 16:21:12 +0000124**
danielk19774adee202004-05-08 08:23:19 +0000125** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000126*/
drh9bb575f2004-09-06 17:24:11 +0000127Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000128 Table *p = 0;
129 int i;
drh645f63e2004-06-22 13:22:40 +0000130 assert( zName!=0 );
danielk19778a414492004-06-29 08:59:35 +0000131 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
132 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000133 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000134 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
135 p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000136 if( p ) break;
137 }
drh74e24cd2002-01-09 03:19:59 +0000138 return p;
drh75897232000-05-29 14:26:00 +0000139}
140
141/*
danielk19778a414492004-06-29 08:59:35 +0000142** Locate the in-memory structure that describes a particular database
143** table given the name of that table and (optionally) the name of the
144** database containing the table. Return NULL if not found. Also leave an
145** error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000146**
danielk19778a414492004-06-29 08:59:35 +0000147** The difference between this routine and sqlite3FindTable() is that this
148** routine leaves an error message in pParse->zErrMsg where
149** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000150*/
danielk19774adee202004-05-08 08:23:19 +0000151Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000152 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000153
danielk19778a414492004-06-29 08:59:35 +0000154 /* Read the database schema. If an error occurs, leave an error message
155 ** and code in pParse and return NULL. */
156 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
157 return 0;
158 }
159
danielk19774adee202004-05-08 08:23:19 +0000160 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000161 if( p==0 ){
danielk19778a414492004-06-29 08:59:35 +0000162 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000163 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
164 }else if( sqlite3FindTable(pParse->db, zName, 0)!=0 ){
165 sqlite3ErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
drhf26e09c2003-05-31 16:21:12 +0000166 zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000167 }else{
danielk19774adee202004-05-08 08:23:19 +0000168 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000169 }
drha6ecd332004-06-10 00:29:09 +0000170 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000171 }
172 return p;
173}
174
175/*
176** Locate the in-memory structure that describes
177** a particular index given the name of that index
178** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000179** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000180**
181** If zDatabase is 0, all databases are searched for the
182** table and the first matching index is returned. (No checking
183** for duplicate index names is done.) The search order is
184** TEMP first, then MAIN, then any auxiliary databases added
185** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000186*/
drh9bb575f2004-09-06 17:24:11 +0000187Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000188 Index *p = 0;
189 int i;
danielk19778a414492004-06-29 08:59:35 +0000190 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
191 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000192 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000193 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
194 p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000195 if( p ) break;
196 }
drh74e24cd2002-01-09 03:19:59 +0000197 return p;
drh75897232000-05-29 14:26:00 +0000198}
199
200/*
drh956bc922004-07-24 17:38:29 +0000201** Reclaim the memory used by an index
202*/
203static void freeIndex(Index *p){
204 sqliteFree(p->zColAff);
205 sqliteFree(p);
206}
207
208/*
drh75897232000-05-29 14:26:00 +0000209** Remove the given index from the index hash table, and free
210** its memory structures.
211**
drhd229ca92002-01-09 13:30:41 +0000212** The index is removed from the database hash tables but
213** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000214** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000215*/
drh9bb575f2004-09-06 17:24:11 +0000216static void sqliteDeleteIndex(sqlite3 *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000217 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000218
drhd229ca92002-01-09 13:30:41 +0000219 assert( db!=0 && p->zName!=0 );
danielk19774adee202004-05-08 08:23:19 +0000220 pOld = sqlite3HashInsert(&db->aDb[p->iDb].idxHash, p->zName,
drhd24cc422003-03-27 12:51:24 +0000221 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000222 if( pOld!=0 && pOld!=p ){
danielk19774adee202004-05-08 08:23:19 +0000223 sqlite3HashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
drhd24cc422003-03-27 12:51:24 +0000224 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000225 }
drh956bc922004-07-24 17:38:29 +0000226 freeIndex(p);
drh75897232000-05-29 14:26:00 +0000227}
228
229/*
drhbeae3192001-09-22 18:12:08 +0000230** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000231** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000232** structures.
233*/
drh9bb575f2004-09-06 17:24:11 +0000234void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
drh956bc922004-07-24 17:38:29 +0000235 Index *pIndex;
236 int len;
237
238 len = strlen(zIdxName);
239 pIndex = sqlite3HashInsert(&db->aDb[iDb].idxHash, zIdxName, len+1, 0);
240 if( pIndex ){
241 if( pIndex->pTable->pIndex==pIndex ){
242 pIndex->pTable->pIndex = pIndex->pNext;
243 }else{
244 Index *p;
245 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
246 if( p && p->pNext==pIndex ){
247 p->pNext = pIndex->pNext;
248 }
drh5e00f6c2001-09-13 13:46:56 +0000249 }
drh956bc922004-07-24 17:38:29 +0000250 freeIndex(pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000251 }
drh956bc922004-07-24 17:38:29 +0000252 db->flags |= SQLITE_InternChanges;
drh5e00f6c2001-09-13 13:46:56 +0000253}
254
255/*
drhe0bc4042002-06-25 01:09:11 +0000256** Erase all schema information from the in-memory hash tables of
drh234c39d2004-07-24 03:30:47 +0000257** a single database. This routine is called to reclaim memory
258** before the database closes. It is also called during a rollback
danielk1977e0d4b062004-06-28 01:11:46 +0000259** if there were schema changes during the transaction or if a
260** schema-cookie mismatch occurs.
drh1c2d8412003-03-31 00:30:47 +0000261**
262** If iDb<=0 then reset the internal schema tables for all database
263** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000264** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000265*/
drh9bb575f2004-09-06 17:24:11 +0000266void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000267 HashElem *pElem;
268 Hash temp1;
269 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000270 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000271
drh1c2d8412003-03-31 00:30:47 +0000272 assert( iDb>=0 && iDb<db->nDb );
273 db->flags &= ~SQLITE_Initialized;
274 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000275 Db *pDb = &db->aDb[i];
276 temp1 = pDb->tblHash;
277 temp2 = pDb->trigHash;
danielk19774adee202004-05-08 08:23:19 +0000278 sqlite3HashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
279 sqlite3HashClear(&pDb->aFKey);
280 sqlite3HashClear(&pDb->idxHash);
drhd24cc422003-03-27 12:51:24 +0000281 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
282 Trigger *pTrigger = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000283 sqlite3DeleteTrigger(pTrigger);
drhd24cc422003-03-27 12:51:24 +0000284 }
danielk19774adee202004-05-08 08:23:19 +0000285 sqlite3HashClear(&temp2);
286 sqlite3HashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
drhd24cc422003-03-27 12:51:24 +0000287 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
288 Table *pTab = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000289 sqlite3DeleteTable(db, pTab);
drhd24cc422003-03-27 12:51:24 +0000290 }
danielk19774adee202004-05-08 08:23:19 +0000291 sqlite3HashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000292 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000293 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000294 }
drh1c2d8412003-03-31 00:30:47 +0000295 assert( iDb==0 );
296 db->flags &= ~SQLITE_InternChanges;
297
298 /* If one or more of the auxiliary database files has been closed,
299 ** then remove then from the auxiliary database list. We take the
300 ** opportunity to do this here since we have just deleted all of the
301 ** schema hash tables and therefore do not have to make any changes
302 ** to any of those tables.
303 */
drh4d189ca2004-02-12 18:46:38 +0000304 for(i=0; i<db->nDb; i++){
305 struct Db *pDb = &db->aDb[i];
306 if( pDb->pBt==0 ){
307 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
308 pDb->pAux = 0;
309 }
310 }
drh1c2d8412003-03-31 00:30:47 +0000311 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000312 struct Db *pDb = &db->aDb[i];
313 if( pDb->pBt==0 ){
314 sqliteFree(pDb->zName);
315 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000316 continue;
317 }
318 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000319 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000320 }
drh8bf8dc92003-05-17 17:35:10 +0000321 j++;
drh1c2d8412003-03-31 00:30:47 +0000322 }
323 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
324 db->nDb = j;
325 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
326 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
327 sqliteFree(db->aDb);
328 db->aDb = db->aDbStatic;
329 }
drhe0bc4042002-06-25 01:09:11 +0000330}
331
332/*
333** This routine is called whenever a rollback occurs. If there were
334** schema changes during the transaction, then we have to reset the
335** internal hash tables and reload them from disk.
336*/
drh9bb575f2004-09-06 17:24:11 +0000337void sqlite3RollbackInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000338 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000339 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000340 }
341}
342
343/*
344** This routine is called when a commit occurs.
345*/
drh9bb575f2004-09-06 17:24:11 +0000346void sqlite3CommitInternalChanges(sqlite3 *db){
drhe0bc4042002-06-25 01:09:11 +0000347 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000348}
349
350/*
drh956bc922004-07-24 17:38:29 +0000351** Clear the column names from a table or view.
352*/
353static void sqliteResetColumnNames(Table *pTable){
354 int i;
355 Column *pCol;
356 assert( pTable!=0 );
357 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
358 sqliteFree(pCol->zName);
359 sqliteFree(pCol->zDflt);
360 sqliteFree(pCol->zType);
361 }
362 sqliteFree(pTable->aCol);
363 pTable->aCol = 0;
364 pTable->nCol = 0;
365}
366
367/*
drh75897232000-05-29 14:26:00 +0000368** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000369** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000370**
371** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000372** the table data structure from the hash table. Nor does it remove
373** foreign keys from the sqlite.aFKey hash table. But it does destroy
374** memory structures of the indices and foreign keys associated with
375** the table.
drhdaffd0e2001-04-11 14:28:42 +0000376**
377** Indices associated with the table are unlinked from the "db"
378** data structure if db!=NULL. If db==NULL, indices attached to
379** the table are deleted, but it is assumed they have already been
380** unlinked.
drh75897232000-05-29 14:26:00 +0000381*/
drh9bb575f2004-09-06 17:24:11 +0000382void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000383 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000384 FKey *pFKey, *pNextFKey;
385
drh75897232000-05-29 14:26:00 +0000386 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000387
388 /* Delete all indices associated with this table
389 */
390 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
391 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000392 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000393 sqliteDeleteIndex(db, pIndex);
394 }
395
396 /* Delete all foreign keys associated with this table. The keys
397 ** should have already been unlinked from the db->aFKey hash table
398 */
399 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
400 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000401 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000402 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000403 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000404 sqliteFree(pFKey);
405 }
406
407 /* Delete the Table structure itself.
408 */
drh956bc922004-07-24 17:38:29 +0000409 sqliteResetColumnNames(pTable);
drh6e142f52000-06-08 13:36:40 +0000410 sqliteFree(pTable->zName);
drh956bc922004-07-24 17:38:29 +0000411 sqliteFree(pTable->zColAff);
danielk19774adee202004-05-08 08:23:19 +0000412 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000413 sqliteFree(pTable);
414}
415
416/*
drh5edc3122001-09-13 21:53:09 +0000417** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000418** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000419*/
drh9bb575f2004-09-06 17:24:11 +0000420void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
drh956bc922004-07-24 17:38:29 +0000421 Table *p;
drhc2eef3b2002-08-31 18:53:06 +0000422 FKey *pF1, *pF2;
drh956bc922004-07-24 17:38:29 +0000423 Db *pDb;
424
drhd229ca92002-01-09 13:30:41 +0000425 assert( db!=0 );
drh956bc922004-07-24 17:38:29 +0000426 assert( iDb>=0 && iDb<db->nDb );
427 assert( zTabName && zTabName[0] );
428 pDb = &db->aDb[iDb];
429 p = sqlite3HashInsert(&pDb->tblHash, zTabName, strlen(zTabName)+1, 0);
430 if( p ){
431 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
432 int nTo = strlen(pF1->zTo) + 1;
433 pF2 = sqlite3HashFind(&pDb->aFKey, pF1->zTo, nTo);
434 if( pF2==pF1 ){
435 sqlite3HashInsert(&pDb->aFKey, pF1->zTo, nTo, pF1->pNextTo);
436 }else{
437 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
438 if( pF2 ){
439 pF2->pNextTo = pF1->pNextTo;
440 }
drhc2eef3b2002-08-31 18:53:06 +0000441 }
442 }
drh956bc922004-07-24 17:38:29 +0000443 sqlite3DeleteTable(db, p);
drhc2eef3b2002-08-31 18:53:06 +0000444 }
drh956bc922004-07-24 17:38:29 +0000445 db->flags |= SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000446}
447
448/*
drha99db3b2004-06-19 14:49:12 +0000449** Given a token, return a string that consists of the text of that
450** token with any quotations removed. Space to hold the returned string
451** is obtained from sqliteMalloc() and must be freed by the calling
452** function.
drh75897232000-05-29 14:26:00 +0000453**
drha99db3b2004-06-19 14:49:12 +0000454** Tokens are really just pointers into the original SQL text and so
455** are not \000 terminated and are not persistent. The returned string
456** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000457*/
drha99db3b2004-06-19 14:49:12 +0000458char *sqlite3NameFromToken(Token *pName){
459 char *zName;
460 if( pName ){
461 zName = sqliteStrNDup(pName->z, pName->n);
462 sqlite3Dequote(zName);
463 }else{
464 zName = 0;
465 }
drh75897232000-05-29 14:26:00 +0000466 return zName;
467}
468
469/*
danielk1977cbb18d22004-05-28 11:37:27 +0000470** Open the sqlite_master table stored in database number iDb for
471** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000472*/
danielk1977cbb18d22004-05-28 11:37:27 +0000473void sqlite3OpenMasterTable(Vdbe *v, int iDb){
474 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000475 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000476 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000477}
478
479/*
danielk1977cbb18d22004-05-28 11:37:27 +0000480** The token *pName contains the name of a database (either "main" or
481** "temp" or the name of an attached db). This routine returns the
482** index of the named database in db->aDb[], or -1 if the named db
483** does not exist.
484*/
485int findDb(sqlite3 *db, Token *pName){
486 int i;
drh90f5ecb2004-07-22 01:19:35 +0000487 Db *pDb;
488 for(pDb=db->aDb, i=0; i<db->nDb; i++, pDb++){
489 if( pName->n==strlen(pDb->zName) &&
490 0==sqlite3StrNICmp(pDb->zName, pName->z, pName->n) ){
danielk1977cbb18d22004-05-28 11:37:27 +0000491 return i;
492 }
493 }
494 return -1;
495}
496
drh0e3d7472004-06-19 17:33:07 +0000497/* The table or view or trigger name is passed to this routine via tokens
498** pName1 and pName2. If the table name was fully qualified, for example:
499**
500** CREATE TABLE xxx.yyy (...);
501**
502** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
503** the table name is not fully qualified, i.e.:
504**
505** CREATE TABLE yyy(...);
506**
507** Then pName1 is set to "yyy" and pName2 is "".
508**
509** This routine sets the *ppUnqual pointer to point at the token (pName1 or
510** pName2) that stores the unqualified table name. The index of the
511** database "xxx" is returned.
512*/
danielk1977ef2cb632004-05-29 02:37:19 +0000513int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000514 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35 +0000515 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07 +0000516 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
517 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000518){
drh0e3d7472004-06-19 17:33:07 +0000519 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000520 sqlite3 *db = pParse->db;
521
522 if( pName2 && pName2->n>0 ){
523 assert( !db->init.busy );
524 *pUnqual = pName2;
525 iDb = findDb(db, pName1);
526 if( iDb<0 ){
527 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
528 pParse->nErr++;
529 return -1;
530 }
531 }else{
532 assert( db->init.iDb==0 || db->init.busy );
533 iDb = db->init.iDb;
534 *pUnqual = pName1;
535 }
536 return iDb;
537}
538
539/*
danielk1977d8123362004-06-12 09:25:12 +0000540** This routine is used to check if the UTF-8 string zName is a legal
541** unqualified name for a new schema object (table, index, view or
542** trigger). All names are legal except those that begin with the string
543** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
544** is reserved for internal use.
545*/
546int sqlite3CheckObjectName(Parse *pParse, const char *zName){
547 if( !pParse->db->init.busy && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
548 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
549 return SQLITE_ERROR;
550 }
551 return SQLITE_OK;
552}
553
554/*
drh75897232000-05-29 14:26:00 +0000555** Begin constructing a new table representation in memory. This is
556** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000557** to a CREATE TABLE statement. In particular, this routine is called
558** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000559** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000560** flag is true if the table should be stored in the auxiliary database
561** file instead of in the main database file. This is normally the case
562** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000563** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000564**
drhf57b3392001-10-08 13:22:32 +0000565** The new table record is initialized and put in pParse->pNewTable.
566** As more of the CREATE TABLE statement is parsed, additional action
567** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000568** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000569** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000570*/
danielk19774adee202004-05-08 08:23:19 +0000571void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000572 Parse *pParse, /* Parser context */
573 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000574 Token *pName1, /* First part of the name of the table or view */
575 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000576 int isTemp, /* True if this is a TEMP table */
577 int isView /* True if this is a VIEW */
578){
drh75897232000-05-29 14:26:00 +0000579 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000580 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000581 char *zName;
drh9bb575f2004-09-06 17:24:11 +0000582 sqlite3 *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000583 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000584 int iDb; /* Database number to create the table in */
585 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000586
danielk1977cbb18d22004-05-28 11:37:27 +0000587 /* The table or view name to create is passed to this routine via tokens
588 ** pName1 and pName2. If the table name was fully qualified, for example:
589 **
590 ** CREATE TABLE xxx.yyy (...);
591 **
592 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
593 ** the table name is not fully qualified, i.e.:
594 **
595 ** CREATE TABLE yyy(...);
596 **
597 ** Then pName1 is set to "yyy" and pName2 is "".
598 **
599 ** The call below sets the pName pointer to point at the token (pName1 or
600 ** pName2) that stores the unqualified table name. The variable iDb is
601 ** set to the index of the database that the table or view is to be
602 ** created in.
603 */
danielk1977ef2cb632004-05-29 02:37:19 +0000604 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000605 if( iDb<0 ) return;
606 if( isTemp && iDb>1 ){
607 /* If creating a temp table, the name may not be qualified */
608 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
609 pParse->nErr++;
610 return;
611 }
612 if( isTemp ) iDb = 1;
613
614 pParse->sNameToken = *pName;
drha99db3b2004-06-19 14:49:12 +0000615 zName = sqlite3NameFromToken(pName);
danielk1977e0048402004-06-15 16:51:01 +0000616 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000617 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
danielk1977e0048402004-06-15 16:51:01 +0000618 sqliteFree(zName);
danielk1977d8123362004-06-12 09:25:12 +0000619 return;
620 }
drh1d85d932004-02-14 23:05:52 +0000621 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000622#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000623 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000624 {
625 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000626 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000627 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000628 sqliteFree(zName);
629 return;
630 }
drhe5f9c642003-01-13 23:27:31 +0000631 if( isView ){
632 if( isTemp ){
633 code = SQLITE_CREATE_TEMP_VIEW;
634 }else{
635 code = SQLITE_CREATE_VIEW;
636 }
637 }else{
638 if( isTemp ){
639 code = SQLITE_CREATE_TEMP_TABLE;
640 }else{
641 code = SQLITE_CREATE_TABLE;
642 }
643 }
danielk19774adee202004-05-08 08:23:19 +0000644 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000645 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000646 return;
647 }
648 }
649#endif
drhf57b3392001-10-08 13:22:32 +0000650
drhf57b3392001-10-08 13:22:32 +0000651 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000652 ** index or table name in the same database. Issue an error message if
653 ** it does.
drhf57b3392001-10-08 13:22:32 +0000654 */
danielk19778a414492004-06-29 08:59:35 +0000655 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19773df6b252004-05-29 10:23:19 +0000656 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
657 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000658 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000659 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000660 return;
drh75897232000-05-29 14:26:00 +0000661 }
danielk19778a414492004-06-29 08:59:35 +0000662 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
663 ( iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000664 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000665 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000666 return;
667 }
668 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000669 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000670 pParse->rc = SQLITE_NOMEM;
671 pParse->nErr++;
drh6d4abfb2001-10-22 02:58:08 +0000672 sqliteFree(zName);
673 return;
674 }
drh75897232000-05-29 14:26:00 +0000675 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000676 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000677 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000678 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000679 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000680 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000681 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000682 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000683
684 /* Begin generating the code that will insert the table record into
685 ** the SQLITE_MASTER table. Note in particular that we must go ahead
686 ** and allocate the record number for the table entry now. Before any
687 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
688 ** indices to be created and the table record must come before the
689 ** indices. Hence, the record number for the table must be allocated
690 ** now.
691 */
danielk19774adee202004-05-08 08:23:19 +0000692 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000693 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977d008cfe2004-06-19 02:22:10 +0000694 /* Every time a new table is created the file-format
695 ** and encoding meta-values are set in the database, in
696 ** case this is the first table created.
697 */
698 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
699 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
700 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
701 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
702
danielk1977cbb18d22004-05-28 11:37:27 +0000703 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000704 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
705 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000706 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000707 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000708 }
drh75897232000-05-29 14:26:00 +0000709}
710
711/*
712** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000713**
714** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000715** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000716** first to get things going. Then this routine is called for each
717** column.
drh75897232000-05-29 14:26:00 +0000718*/
danielk19774adee202004-05-08 08:23:19 +0000719void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000720 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000721 int i;
drha99db3b2004-06-19 14:49:12 +0000722 char *z;
drhc9b84a12002-06-20 11:36:48 +0000723 Column *pCol;
drh75897232000-05-29 14:26:00 +0000724 if( (p = pParse->pNewTable)==0 ) return;
drha99db3b2004-06-19 14:49:12 +0000725 z = sqlite3NameFromToken(pName);
drh97fc3d02002-05-22 21:27:03 +0000726 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000727 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000728 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
729 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000730 sqliteFree(z);
731 return;
732 }
733 }
drh75897232000-05-29 14:26:00 +0000734 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000735 Column *aNew;
736 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
737 if( aNew==0 ) return;
738 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000739 }
drhc9b84a12002-06-20 11:36:48 +0000740 pCol = &p->aCol[p->nCol];
741 memset(pCol, 0, sizeof(p->aCol[0]));
742 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000743
744 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000745 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
746 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000747 */
danielk19774f057f92004-06-08 00:02:33 +0000748 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000749 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000750 p->nCol++;
drh75897232000-05-29 14:26:00 +0000751}
752
753/*
drh382c0242001-10-06 16:33:02 +0000754** This routine is called by the parser while in the middle of
755** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
756** been seen on a column. This routine sets the notNull flag on
757** the column currently under construction.
758*/
danielk19774adee202004-05-08 08:23:19 +0000759void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000760 Table *p;
761 int i;
762 if( (p = pParse->pNewTable)==0 ) return;
763 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000764 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000765}
766
767/*
768** This routine is called by the parser while in the middle of
769** parsing a CREATE TABLE statement. The pFirst token is the first
770** token in the sequence of tokens that describe the type of the
771** column currently under construction. pLast is the last token
772** in the sequence. Use this information to construct a string
773** that contains the typename of the column and store that string
774** in zType.
775*/
danielk19774adee202004-05-08 08:23:19 +0000776void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000777 Table *p;
778 int i, j;
779 int n;
780 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000781 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000782 if( (p = pParse->pNewTable)==0 ) return;
783 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000784 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000785 pCol = &p->aCol[i];
786 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000787 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000788 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000789 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000790 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000791 for(i=j=0; z[i]; i++){
792 int c = z[i];
793 if( isspace(c) ) continue;
794 z[j++] = c;
795 }
796 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000797 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000798}
799
800/*
drh7020f652000-06-03 18:06:52 +0000801** The given token is the default value for the last column added to
802** the table currently under construction. If "minusFlag" is true, it
803** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000804**
805** This routine is called by the parser while in the middle of
806** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000807*/
danielk19774adee202004-05-08 08:23:19 +0000808void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000809 Table *p;
810 int i;
811 char **pz;
812 if( (p = pParse->pNewTable)==0 ) return;
813 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000814 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000815 pz = &p->aCol[i].zDflt;
816 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000817 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000818 }else{
danielk19774adee202004-05-08 08:23:19 +0000819 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000820 }
danielk19774adee202004-05-08 08:23:19 +0000821 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000822}
823
824/*
drh4a324312001-12-21 14:30:42 +0000825** Designate the PRIMARY KEY for the table. pList is a list of names
826** of columns that form the primary key. If pList is NULL, then the
827** most recently added column of the table is the primary key.
828**
829** A table can have at most one primary key. If the table already has
830** a primary key (and this is the second primary key) then create an
831** error.
832**
833** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
834** then we will try to use that column as the row id. (Exception:
835** For backwards compatibility with older databases, do not do this
836** if the file format version number is less than 1.) Set the Table.iPKey
837** field of the table under construction to be the index of the
838** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
839** no INTEGER PRIMARY KEY.
840**
841** If the key is not an INTEGER PRIMARY KEY, then create a unique
842** index for the key. No index is created for INTEGER PRIMARY KEYs.
843*/
danielk19770202b292004-06-09 09:55:16 +0000844void sqlite3AddPrimaryKey(Parse *pParse, ExprList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000845 Table *pTab = pParse->pNewTable;
846 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000847 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000848 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000849 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000850 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000851 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000852 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000853 }
854 pTab->hasPrimKey = 1;
855 if( pList==0 ){
856 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000857 pTab->aCol[iCol].isPrimKey = 1;
858 }else{
danielk19770202b292004-06-09 09:55:16 +0000859 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000860 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000861 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
862 break;
863 }
drh78100cc2003-08-23 22:40:53 +0000864 }
865 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000866 }
danielk19770202b292004-06-09 09:55:16 +0000867 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000868 }
869 if( iCol>=0 && iCol<pTab->nCol ){
870 zType = pTab->aCol[iCol].zType;
871 }
danielk19773d68f032004-05-11 07:11:51 +0000872 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000873 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000874 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000875 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000876 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000877 pList = 0;
drh4a324312001-12-21 14:30:42 +0000878 }
drhe0194f22003-02-26 13:52:51 +0000879
880primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000881 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000882 return;
drh4a324312001-12-21 14:30:42 +0000883}
884
885/*
drhd3d39e92004-05-20 22:16:29 +0000886** Set the collation function of the most recently parsed table column
887** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000888*/
drhd3d39e92004-05-20 22:16:29 +0000889void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000890 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000891 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000892 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000893 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000894
drhd3d39e92004-05-20 22:16:29 +0000895 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000896 i = p->nCol-1;
897
898 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
899 p->aCol[i].pColl = pColl;
900
901 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
902 ** then an index may have been created on this column before the
903 ** collation type was added. Correct this if it is the case.
904 */
905 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
906 assert( pIdx->nColumn==1 );
907 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000908 }
909}
910
911/*
danielk19770202b292004-06-09 09:55:16 +0000912** Locate and return an entry from the db.aCollSeq hash table. If the entry
913** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +0000914** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +0000915**
danielk1977466be562004-06-10 02:16:01 +0000916** Each pointer stored in the sqlite3.aCollSeq hash table contains an
917** array of three CollSeq structures. The first is the collation sequence
918** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +0000919**
danielk1977466be562004-06-10 02:16:01 +0000920** Stored immediately after the three collation sequences is a copy of
921** the collation sequence name. A pointer to this string is stored in
922** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +0000923*/
danielk1977466be562004-06-10 02:16:01 +0000924static CollSeq * findCollSeqEntry(
drh9bb575f2004-09-06 17:24:11 +0000925 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +0000926 const char *zName,
danielk19770202b292004-06-09 09:55:16 +0000927 int nName,
928 int create
drhd3d39e92004-05-20 22:16:29 +0000929){
930 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000931 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000932 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +0000933
danielk19770202b292004-06-09 09:55:16 +0000934 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +0000935 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +0000936 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000937 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000938 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +0000939 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000940 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +0000941 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000942 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +0000943 memcpy(pColl[0].zName, zName, nName);
944 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +0000945 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +0000946 }
drhd3d39e92004-05-20 22:16:29 +0000947 }
drhd3d39e92004-05-20 22:16:29 +0000948 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000949}
950
danielk1977466be562004-06-10 02:16:01 +0000951/*
952** Parameter zName points to a UTF-8 encoded string nName bytes long.
953** Return the CollSeq* pointer for the collation sequence named zName
954** for the encoding 'enc' from the database 'db'.
955**
956** If the entry specified is not found and 'create' is true, then create a
957** new entry. Otherwise return NULL.
958*/
959CollSeq *sqlite3FindCollSeq(
drh9bb575f2004-09-06 17:24:11 +0000960 sqlite3 *db,
danielk1977466be562004-06-10 02:16:01 +0000961 u8 enc,
962 const char *zName,
963 int nName,
964 int create
965){
966 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
drh6d08b4d2004-07-20 12:45:22 +0000967 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
968 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
969 if( pColl ) pColl += enc-1;
danielk1977466be562004-06-10 02:16:01 +0000970 return pColl;
971}
972
danielk1977e159fdf2004-06-21 10:45:06 +0000973/*
974** Invoke the 'collation needed' callback to request a collation sequence
975** in the database text encoding of name zName, length nName.
976** If the collation sequence
977*/
drh9bb575f2004-09-06 17:24:11 +0000978static void callCollNeeded(sqlite3 *db, const char *zName, int nName){
danielk19777cedc8d2004-06-10 10:50:08 +0000979 assert( !db->xCollNeeded || !db->xCollNeeded16 );
980 if( nName<0 ) nName = strlen(zName);
981 if( db->xCollNeeded ){
danielk19778a6c5502004-06-22 12:18:32 +0000982 char *zExternal = sqliteStrNDup(zName, nName);
danielk19777cedc8d2004-06-10 10:50:08 +0000983 if( !zExternal ) return;
danielk1977e159fdf2004-06-21 10:45:06 +0000984 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
985 sqliteFree(zExternal);
danielk19777cedc8d2004-06-10 10:50:08 +0000986 }
987 if( db->xCollNeeded16 ){
danielk19778a6c5502004-06-22 12:18:32 +0000988 char const *zExternal;
danielk1977bfd6cce2004-06-18 04:24:54 +0000989 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
990 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
991 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +0000992 if( !zExternal ) return;
993 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
994 }
danielk19777cedc8d2004-06-10 10:50:08 +0000995}
996
danielk1977e159fdf2004-06-21 10:45:06 +0000997/*
998** This routine is called if the collation factory fails to deliver a
999** collation function in the best encoding but there may be other versions
1000** of this collation function (for other text encodings) available. Use one
1001** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
1002** possible.
1003*/
danielk19777cedc8d2004-06-10 10:50:08 +00001004static int synthCollSeq(Parse *pParse, CollSeq *pColl){
drhda71ce12004-06-21 18:14:45 +00001005 CollSeq *pColl2;
danielk19777cedc8d2004-06-10 10:50:08 +00001006 char *z = pColl->zName;
1007 int n = strlen(z);
drh9bb575f2004-09-06 17:24:11 +00001008 sqlite3 *db = pParse->db;
drhda71ce12004-06-21 18:14:45 +00001009 int i;
1010 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
1011 for(i=0; i<3; i++){
1012 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
1013 if( pColl2->xCmp!=0 ){
1014 memcpy(pColl, pColl2, sizeof(CollSeq));
1015 return SQLITE_OK;
danielk19777cedc8d2004-06-10 10:50:08 +00001016 }
danielk19777cedc8d2004-06-10 10:50:08 +00001017 }
drhda71ce12004-06-21 18:14:45 +00001018 if( pParse->nErr==0 ){
1019 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ",
1020 -1, z, n, 0);
1021 }
1022 pParse->nErr++;
1023 return SQLITE_ERROR;
danielk19777cedc8d2004-06-10 10:50:08 +00001024}
1025
1026/*
1027** This routine is called on a collation sequence before it is used to
1028** check that it is defined. An undefined collation sequence exists when
1029** a database is loaded that contains references to collation sequences
1030** that have not been defined by sqlite3_create_collation() etc.
1031**
1032** If required, this routine calls the 'collation needed' callback to
1033** request a definition of the collating sequence. If this doesn't work,
1034** an equivalent collating sequence that uses a text encoding different
1035** from the main database is substituted, if one is available.
1036*/
1037int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1038 if( pColl && !pColl->xCmp ){
danielk1977e159fdf2004-06-21 10:45:06 +00001039 /* No collation sequence of this type for this encoding is registered.
1040 ** Call the collation factory to see if it can supply us with one.
1041 */
danielk19777cedc8d2004-06-10 10:50:08 +00001042 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1043 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1044 return SQLITE_ERROR;
1045 }
1046 }
1047 return SQLITE_OK;
1048}
1049
drhda71ce12004-06-21 18:14:45 +00001050/*
1051** Call sqlite3CheckCollSeq() for all collating sequences in an index,
1052** in order to verify that all the necessary collating sequences are
1053** loaded.
1054*/
danielk19777cedc8d2004-06-10 10:50:08 +00001055int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1056 if( pIdx ){
1057 int i;
1058 for(i=0; i<pIdx->nColumn; i++){
1059 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1060 return SQLITE_ERROR;
1061 }
1062 }
1063 }
1064 return SQLITE_OK;
1065}
1066
danielk1977466be562004-06-10 02:16:01 +00001067/*
1068** This function returns the collation sequence for database native text
1069** encoding identified by the string zName, length nName.
1070**
1071** If the requested collation sequence is not available, or not available
1072** in the database native encoding, the collation factory is invoked to
1073** request it. If the collation factory does not supply such a sequence,
1074** and the sequence is available in another text encoding, then that is
1075** returned instead.
1076**
1077** If no versions of the requested collations sequence are available, or
1078** another error occurs, NULL is returned and an error message written into
1079** pParse.
1080*/
danielk19770202b292004-06-09 09:55:16 +00001081CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001082 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001083 u8 initbusy = pParse->db->init.busy;
1084 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
1085 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001086 /* No collation sequence of this type for this encoding is registered.
1087 ** Call the collation factory to see if it can supply us with one.
1088 */
danielk19777cedc8d2004-06-10 10:50:08 +00001089 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001090 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001091 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001092 /* There may be a version of the collation sequence that requires
1093 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001094 */
danielk19777cedc8d2004-06-10 10:50:08 +00001095 if( synthCollSeq(pParse, pColl) ){
1096 return 0;
danielk1977466be562004-06-10 02:16:01 +00001097 }
1098 }
1099 }
1100
1101 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001102 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001103 if( pParse->nErr==0 ){
danielk1977466be562004-06-10 02:16:01 +00001104 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ", -1,
danielk19770202b292004-06-09 09:55:16 +00001105 zName, nName, 0);
1106 }
1107 pParse->nErr++;
danielk19777cedc8d2004-06-10 10:50:08 +00001108 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001109 }
1110 return pColl;
1111}
1112
1113
1114
danielk1977a37cdde2004-05-16 11:15:36 +00001115/*
drh1ad3b9e2004-05-20 12:10:20 +00001116** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +00001117** associated affinity type.
1118*/
1119char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +00001120 int n, i;
1121 struct {
drh1ad3b9e2004-05-20 12:10:20 +00001122 const char *zSub; /* Keywords substring to search for */
drhda71ce12004-06-21 18:14:45 +00001123 char nSub; /* length of zSub */
drh1ad3b9e2004-05-20 12:10:20 +00001124 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +00001125 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +00001126 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +00001127 {"CHAR", 4, SQLITE_AFF_TEXT},
1128 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +00001129 {"TEXT", 4, SQLITE_AFF_TEXT},
1130 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +00001131 };
1132
drh9c054832004-05-31 18:51:57 +00001133 if( nType==0 ){
1134 return SQLITE_AFF_NONE;
1135 }
drh1ad3b9e2004-05-20 12:10:20 +00001136 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
1137 int c1 = substrings[i].zSub[0];
1138 int c2 = tolower(c1);
1139 int limit = nType - substrings[i].nSub;
1140 const char *z = substrings[i].zSub;
1141 for(n=0; n<=limit; n++){
1142 int c = zType[n];
1143 if( (c==c1 || c==c2)
1144 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +00001145 return substrings[i].affinity;
1146 }
1147 }
1148 }
drh1ad3b9e2004-05-20 12:10:20 +00001149 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +00001150}
1151
1152/*
drh3f7d4e42004-07-24 14:35:58 +00001153** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001154**
1155** The schema cookie is used to determine when the schema for the
1156** database changes. After each schema change, the cookie value
1157** changes. When a process first reads the schema it records the
1158** cookie. Thereafter, whenever it goes to access the database,
1159** it checks the cookie to make sure the schema has not changed
1160** since it was last read.
1161**
1162** This plan is not completely bullet-proof. It is possible for
1163** the schema to change multiple times and for the cookie to be
1164** set back to prior value. But schema changes are infrequent
1165** and the probability of hitting the same cookie value is only
1166** 1 chance in 2^32. So we're safe enough.
1167*/
drh9bb575f2004-09-06 17:24:11 +00001168void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){
drh3f7d4e42004-07-24 14:35:58 +00001169 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].schema_cookie+1, 0);
danielk19771d850a72004-05-31 08:26:49 +00001170 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001171}
1172
1173/*
drh969fa7c2002-02-18 18:30:32 +00001174** Measure the number of characters needed to output the given
1175** identifier. The number returned includes any quotes used
1176** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001177**
1178** The estimate is conservative. It might be larger that what is
1179** really needed.
drh969fa7c2002-02-18 18:30:32 +00001180*/
1181static int identLength(const char *z){
1182 int n;
drh17f71932002-02-21 12:01:27 +00001183 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001184 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001185 }
drh234c39d2004-07-24 03:30:47 +00001186 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001187}
1188
1189/*
1190** Write an identifier onto the end of the given string. Add
1191** quote characters as needed.
1192*/
drh4c755c02004-08-08 20:22:17 +00001193static void identPut(char *z, int *pIdx, char *zSignedIdent){
1194 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001195 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001196 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001197 for(j=0; zIdent[j]; j++){
1198 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1199 }
1200 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001201 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh234c39d2004-07-24 03:30:47 +00001202 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001203 for(j=0; zIdent[j]; j++){
1204 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001205 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001206 }
drh234c39d2004-07-24 03:30:47 +00001207 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001208 z[i] = 0;
1209 *pIdx = i;
1210}
1211
1212/*
1213** Generate a CREATE TABLE statement appropriate for the given
1214** table. Memory to hold the text of the statement is obtained
1215** from sqliteMalloc() and must be freed by the calling function.
1216*/
1217static char *createTableStmt(Table *p){
1218 int i, k, n;
1219 char *zStmt;
drh234c39d2004-07-24 03:30:47 +00001220 char *zSep, *zSep2, *zEnd, *z;
1221 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001222 n = 0;
drh234c39d2004-07-24 03:30:47 +00001223 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1224 n += identLength(pCol->zName);
1225 z = pCol->zType;
1226 if( z ){
1227 n += (strlen(z) + 1);
danielk1977517eb642004-06-07 10:00:31 +00001228 }
drh969fa7c2002-02-18 18:30:32 +00001229 }
1230 n += identLength(p->zName);
drh234c39d2004-07-24 03:30:47 +00001231 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001232 zSep = "";
1233 zSep2 = ",";
1234 zEnd = ")";
1235 }else{
1236 zSep = "\n ";
1237 zSep2 = ",\n ";
1238 zEnd = "\n)";
1239 }
drhe0bc4042002-06-25 01:09:11 +00001240 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001241 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001242 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001243 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001244 k = strlen(zStmt);
1245 identPut(zStmt, &k, p->zName);
1246 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001247 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drh969fa7c2002-02-18 18:30:32 +00001248 strcpy(&zStmt[k], zSep);
1249 k += strlen(&zStmt[k]);
1250 zSep = zSep2;
drh234c39d2004-07-24 03:30:47 +00001251 identPut(zStmt, &k, pCol->zName);
1252 if( (z = pCol->zType)!=0 ){
danielk1977517eb642004-06-07 10:00:31 +00001253 zStmt[k++] = ' ';
drh234c39d2004-07-24 03:30:47 +00001254 strcpy(&zStmt[k], z);
1255 k += strlen(z);
danielk1977517eb642004-06-07 10:00:31 +00001256 }
drh969fa7c2002-02-18 18:30:32 +00001257 }
1258 strcpy(&zStmt[k], zEnd);
1259 return zStmt;
1260}
1261
1262/*
drh75897232000-05-29 14:26:00 +00001263** This routine is called to report the final ")" that terminates
1264** a CREATE TABLE statement.
1265**
drhf57b3392001-10-08 13:22:32 +00001266** The table structure that other action routines have been building
1267** is added to the internal hash tables, assuming no errors have
1268** occurred.
drh75897232000-05-29 14:26:00 +00001269**
drh1d85d932004-02-14 23:05:52 +00001270** An entry for the table is made in the master table on disk, unless
1271** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001272** it means we are reading the sqlite_master table because we just
1273** connected to the database or because the sqlite_master table has
1274** recently changes, so the entry for this table already exists in
1275** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001276**
1277** If the pSelect argument is not NULL, it means that this routine
1278** was called to create a table generated from a
1279** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1280** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001281*/
danielk19774adee202004-05-08 08:23:19 +00001282void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001283 Table *p;
drh9bb575f2004-09-06 17:24:11 +00001284 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001285
danielk197724b03fd2004-05-10 10:34:34 +00001286 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001287 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001288 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001289
danielk1977517eb642004-06-07 10:00:31 +00001290 assert( !db->init.busy || !pSelect );
1291
drh1d85d932004-02-14 23:05:52 +00001292 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001293 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1294 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001295 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001296 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001297 */
drh1d85d932004-02-14 23:05:52 +00001298 if( db->init.busy ){
1299 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001300 }
1301
drhe3c41372001-09-17 20:25:58 +00001302 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001303 ** in the SQLITE_MASTER table of the database. The record number
1304 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001305 **
drhe0bc4042002-06-25 01:09:11 +00001306 ** If this is a TEMPORARY table, write the entry into the auxiliary
1307 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001308 */
drh1d85d932004-02-14 23:05:52 +00001309 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001310 int n;
drhd8bc7082000-06-07 23:51:50 +00001311 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001312
danielk19774adee202004-05-08 08:23:19 +00001313 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001314 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001315
drh4ff6dfa2002-03-03 23:06:00 +00001316 if( p->pSelect==0 ){
1317 /* A regular table */
drh234c39d2004-07-24 03:30:47 +00001318 sqlite3VdbeAddOp(v, OP_CreateTable, p->iDb, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001319 }else{
1320 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001322 }
danielk1977517eb642004-06-07 10:00:31 +00001323
1324 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1325
1326 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1327 ** statement to populate the new table. The root-page number for the
1328 ** new table is on the top of the vdbe stack.
1329 **
1330 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1331 ** suitable state to query for the column names and types to be used
1332 ** by the new table.
1333 */
1334 if( pSelect ){
1335 Table *pSelTab;
1336 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1337 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1338 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1339 pParse->nTab = 2;
1340 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1341 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1342 if( pParse->nErr==0 ){
1343 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1344 if( pSelTab==0 ) return;
1345 assert( p->aCol==0 );
1346 p->nCol = pSelTab->nCol;
1347 p->aCol = pSelTab->aCol;
1348 pSelTab->nCol = 0;
1349 pSelTab->aCol = 0;
1350 sqlite3DeleteTable(0, pSelTab);
1351 }
1352 }
1353
1354 sqlite3OpenMasterTable(v, p->iDb);
1355
1356 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view",P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001357 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1358 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk1977517eb642004-06-07 10:00:31 +00001359 sqlite3VdbeAddOp(v, OP_Pull, 3, 0);
1360
drhe0bc4042002-06-25 01:09:11 +00001361 if( pSelect ){
1362 char *z = createTableStmt(p);
1363 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001364 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001365 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001366 sqliteFree(z);
1367 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001368 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001369 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001370 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001371 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001372 }
drhe0bc4042002-06-25 01:09:11 +00001373 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001374 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001375 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001376 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
drh855eb1c2004-08-31 13:45:11 +00001377 sqlite3VdbeAddOp(v, OP_Concat, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00001378 }
danielk197784ac9d02004-05-18 09:58:06 +00001379 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001380 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drhc275b4e2004-07-19 17:25:24 +00001381 sqlite3ChangeCookie(db, v, p->iDb);
danielk19774adee202004-05-08 08:23:19 +00001382 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh234c39d2004-07-24 03:30:47 +00001383 sqlite3VdbeOp3(v, OP_ParseSchema, p->iDb, 0,
1384 sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC);
danielk1977517eb642004-06-07 10:00:31 +00001385
danielk19774adee202004-05-08 08:23:19 +00001386 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001387 }
drh17e9e292003-02-01 13:53:28 +00001388
1389 /* Add the table to the in-memory representation of the database.
1390 */
drh234c39d2004-07-24 03:30:47 +00001391 if( db->init.busy && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001392 Table *pOld;
drhbe5c89a2004-07-26 00:31:09 +00001393 FKey *pFKey;
1394 Db *pDb = &db->aDb[p->iDb];
1395 pOld = sqlite3HashInsert(&pDb->tblHash, p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001396 if( pOld ){
1397 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1398 return;
1399 }
1400 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1401 int nTo = strlen(pFKey->zTo) + 1;
drhbe5c89a2004-07-26 00:31:09 +00001402 pFKey->pNextTo = sqlite3HashFind(&pDb->aFKey, pFKey->zTo, nTo);
1403 sqlite3HashInsert(&pDb->aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001404 }
1405 pParse->pNewTable = 0;
1406 db->nTable++;
1407 db->flags |= SQLITE_InternChanges;
1408 }
drh75897232000-05-29 14:26:00 +00001409}
1410
1411/*
drha76b5df2002-02-23 02:32:10 +00001412** The parser calls this routine in order to create a new VIEW
1413*/
danielk19774adee202004-05-08 08:23:19 +00001414void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001415 Parse *pParse, /* The parsing context */
1416 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001417 Token *pName1, /* The token that holds the name of the view */
1418 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001419 Select *pSelect, /* A SELECT statement that will become the new view */
1420 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001421){
drha76b5df2002-02-23 02:32:10 +00001422 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001423 int n;
drh4c755c02004-08-08 20:22:17 +00001424 const unsigned char *z;
drh4b59ab52002-08-24 18:24:51 +00001425 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001426 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001427 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001428
danielk197748dec7e2004-05-28 12:33:30 +00001429 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001430 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001431 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001432 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001433 return;
1434 }
danielk1977ef2cb632004-05-29 02:37:19 +00001435 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001436 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1437 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001438 ){
danielk19774adee202004-05-08 08:23:19 +00001439 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001440 return;
1441 }
drh174b6192002-12-03 02:22:52 +00001442
drh4b59ab52002-08-24 18:24:51 +00001443 /* Make a copy of the entire SELECT statement that defines the view.
1444 ** This will force all the Expr.token.z values to be dynamically
1445 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001446 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001447 */
danielk19774adee202004-05-08 08:23:19 +00001448 p->pSelect = sqlite3SelectDup(pSelect);
1449 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001450 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001451 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001452 }
drh4b59ab52002-08-24 18:24:51 +00001453
1454 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1455 ** the end.
1456 */
drha76b5df2002-02-23 02:32:10 +00001457 sEnd = pParse->sLastToken;
1458 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1459 sEnd.z += sEnd.n;
1460 }
1461 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001462 n = sEnd.z - pBegin->z;
drh4c755c02004-08-08 20:22:17 +00001463 z = (const unsigned char*)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001464 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1465 sEnd.z = &z[n-1];
1466 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001467
danielk19774adee202004-05-08 08:23:19 +00001468 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1469 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001470 return;
drh417be792002-03-03 18:59:40 +00001471}
drha76b5df2002-02-23 02:32:10 +00001472
drh417be792002-03-03 18:59:40 +00001473/*
1474** The Table structure pTable is really a VIEW. Fill in the names of
1475** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001476** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001477*/
danielk19774adee202004-05-08 08:23:19 +00001478int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001479 ExprList *pEList;
1480 Select *pSel;
1481 Table *pSelTab;
1482 int nErr = 0;
1483
1484 assert( pTable );
1485
1486 /* A positive nCol means the columns names for this view are
1487 ** already known.
1488 */
1489 if( pTable->nCol>0 ) return 0;
1490
1491 /* A negative nCol is a special marker meaning that we are currently
1492 ** trying to compute the column names. If we enter this routine with
1493 ** a negative nCol, it means two or more views form a loop, like this:
1494 **
1495 ** CREATE VIEW one AS SELECT * FROM two;
1496 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001497 **
1498 ** Actually, this error is caught previously and so the following test
1499 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001500 */
1501 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001502 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001503 return 1;
1504 }
1505
1506 /* If we get this far, it means we need to compute the table names.
1507 */
1508 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1509 pSel = pTable->pSelect;
1510
danielk19774adee202004-05-08 08:23:19 +00001511 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001512 ** "*" elements in this list. But we will need to restore the list
1513 ** back to its original configuration afterwards, so we save a copy of
1514 ** the original in pEList.
1515 */
1516 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001517 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001518 if( pSel->pEList==0 ){
1519 pSel->pEList = pEList;
1520 return 1; /* Malloc failed */
1521 }
1522 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001523 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001524 if( pSelTab ){
1525 assert( pTable->aCol==0 );
1526 pTable->nCol = pSelTab->nCol;
1527 pTable->aCol = pSelTab->aCol;
1528 pSelTab->nCol = 0;
1529 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001530 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001531 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001532 }else{
1533 pTable->nCol = 0;
1534 nErr++;
1535 }
danielk19774adee202004-05-08 08:23:19 +00001536 sqlite3SelectUnbind(pSel);
1537 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001538 pSel->pEList = pEList;
1539 return nErr;
1540}
1541
1542/*
drh8bf8dc92003-05-17 17:35:10 +00001543** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001544*/
drh9bb575f2004-09-06 17:24:11 +00001545static void sqliteViewResetAll(sqlite3 *db, int idx){
drh417be792002-03-03 18:59:40 +00001546 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001547 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001548 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001549 Table *pTab = sqliteHashData(i);
1550 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001551 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001552 }
1553 }
drh8bf8dc92003-05-17 17:35:10 +00001554 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001555}
1556
drh75897232000-05-29 14:26:00 +00001557/*
1558** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001559** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001560*/
danielk1977a8858102004-05-28 12:11:21 +00001561void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1562 Table *pTab;
drh75897232000-05-29 14:26:00 +00001563 Vdbe *v;
1564 int base;
drh9bb575f2004-09-06 17:24:11 +00001565 sqlite3 *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001566 int iDb;
drh75897232000-05-29 14:26:00 +00001567
danielk1977a8858102004-05-28 12:11:21 +00001568 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1569 assert( pName->nSrc==1 );
1570 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1571
1572 if( pTab==0 ) goto exit_drop_table;
1573 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001574 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001575#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001576 {
1577 int code;
danielk1977a8858102004-05-28 12:11:21 +00001578 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1579 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001580 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001581 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001582 }
drhe5f9c642003-01-13 23:27:31 +00001583 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001584 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001585 code = SQLITE_DROP_TEMP_VIEW;
1586 }else{
1587 code = SQLITE_DROP_VIEW;
1588 }
1589 }else{
drhd24cc422003-03-27 12:51:24 +00001590 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001591 code = SQLITE_DROP_TEMP_TABLE;
1592 }else{
1593 code = SQLITE_DROP_TABLE;
1594 }
1595 }
danielk1977a8858102004-05-28 12:11:21 +00001596 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1597 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001598 }
danielk1977a8858102004-05-28 12:11:21 +00001599 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1600 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001601 }
drhe5f9c642003-01-13 23:27:31 +00001602 }
1603#endif
danielk1977a8858102004-05-28 12:11:21 +00001604 if( pTab->readOnly ){
1605 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001606 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001607 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001608 }
danielk1977a8858102004-05-28 12:11:21 +00001609 if( isView && pTab->pSelect==0 ){
1610 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1611 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001612 }
danielk1977a8858102004-05-28 12:11:21 +00001613 if( !isView && pTab->pSelect ){
1614 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1615 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001616 }
drh75897232000-05-29 14:26:00 +00001617
drh1ccde152000-06-17 13:12:39 +00001618 /* Generate code to remove the table from the master table
1619 ** on disk.
1620 */
danielk19774adee202004-05-08 08:23:19 +00001621 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001622 if( v ){
drh905793e2004-02-21 13:31:09 +00001623 static VdbeOpList dropTable[] = {
danielk19778e227872004-06-07 07:52:17 +00001624 { OP_Rewind, 0, ADDR(13), 0},
1625 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001626 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001627 { OP_MemLoad, 1, 0, 0}, /* 3 */
danielk19778e227872004-06-07 07:52:17 +00001628 { OP_Column, 0, 2, 0}, /* sqlite_master.tbl_name */
1629 { OP_Ne, 0, ADDR(12), 0},
1630 { OP_String8, 0, 0, "trigger"},
1631 { OP_Column, 0, 2, 0}, /* sqlite_master.type */
1632 { OP_Eq, 0, ADDR(12), 0},
drh75897232000-05-29 14:26:00 +00001633 { OP_Delete, 0, 0, 0},
danielk19778e227872004-06-07 07:52:17 +00001634 { OP_Rewind, 0, ADDR(13), 0},
danielk19778d059842004-05-12 11:24:02 +00001635 { OP_Goto, 0, ADDR(3), 0},
danielk19778e227872004-06-07 07:52:17 +00001636 { OP_Next, 0, ADDR(3), 0}, /* 12 */
drh75897232000-05-29 14:26:00 +00001637 };
1638 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001639 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001640 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001641
danielk19778e227872004-06-07 07:52:17 +00001642 /* Drop all triggers associated with the table being dropped. Code
1643 ** is generated to remove entries from sqlite_master and/or
1644 ** sqlite_temp_master if required.
1645 */
danielk1977a8858102004-05-28 12:11:21 +00001646 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001647 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001648 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001649 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drh956bc922004-07-24 17:38:29 +00001650 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00001651 }
drh8bf8dc92003-05-17 17:35:10 +00001652
danielk19778e227872004-06-07 07:52:17 +00001653 /* Drop all SQLITE_MASTER table and index entries that refer to the
1654 ** table. The program name loops through the master table and deletes
1655 ** every row that refers to a table of the same name as the one being
1656 ** dropped. Triggers are handled seperately because a trigger can be
1657 ** created in the temp database that refers to a table in another
1658 ** database.
1659 */
danielk1977a8858102004-05-28 12:11:21 +00001660 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001661 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001662 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
danielk19778e227872004-06-07 07:52:17 +00001663 sqlite3ChangeCookie(db, v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001664 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001665 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001666 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1667 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001668 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001669 }
drh5e00f6c2001-09-13 13:46:56 +00001670 }
drh956bc922004-07-24 17:38:29 +00001671 sqlite3VdbeOp3(v, OP_DropTable, pTab->iDb, 0, pTab->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00001672 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001673 }
drhd24cc422003-03-27 12:51:24 +00001674 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001675
1676exit_drop_table:
1677 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001678}
1679
1680/*
drhc2eef3b2002-08-31 18:53:06 +00001681** This routine is called to create a new foreign key on the table
1682** currently under construction. pFromCol determines which columns
1683** in the current table point to the foreign key. If pFromCol==0 then
1684** connect the key to the last column inserted. pTo is the name of
1685** the table referred to. pToCol is a list of tables in the other
1686** pTo table that the foreign key points to. flags contains all
1687** information about the conflict resolution algorithms specified
1688** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1689**
1690** An FKey structure is created and added to the table currently
1691** under construction in the pParse->pNewTable field. The new FKey
1692** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001693** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001694**
1695** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001696** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001697*/
danielk19774adee202004-05-08 08:23:19 +00001698void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001699 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001700 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001701 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001702 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001703 int flags /* Conflict resolution algorithms. */
1704){
1705 Table *p = pParse->pNewTable;
1706 int nByte;
1707 int i;
1708 int nCol;
1709 char *z;
1710 FKey *pFKey = 0;
1711
1712 assert( pTo!=0 );
1713 if( p==0 || pParse->nErr ) goto fk_end;
1714 if( pFromCol==0 ){
1715 int iCol = p->nCol-1;
1716 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001717 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001718 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001719 " should reference only one column of table %T",
1720 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001721 goto fk_end;
1722 }
1723 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001724 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001725 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001726 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001727 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001728 goto fk_end;
1729 }else{
danielk19770202b292004-06-09 09:55:16 +00001730 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001731 }
1732 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1733 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001734 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001735 nByte += strlen(pToCol->a[i].zName) + 1;
1736 }
1737 }
1738 pFKey = sqliteMalloc( nByte );
1739 if( pFKey==0 ) goto fk_end;
1740 pFKey->pFrom = p;
1741 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001742 z = (char*)&pFKey[1];
1743 pFKey->aCol = (struct sColMap*)z;
1744 z += sizeof(struct sColMap)*nCol;
1745 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001746 memcpy(z, pTo->z, pTo->n);
1747 z[pTo->n] = 0;
1748 z += pTo->n+1;
1749 pFKey->pNextTo = 0;
1750 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001751 if( pFromCol==0 ){
1752 pFKey->aCol[0].iFrom = p->nCol-1;
1753 }else{
1754 for(i=0; i<nCol; i++){
1755 int j;
1756 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001757 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001758 pFKey->aCol[i].iFrom = j;
1759 break;
1760 }
1761 }
1762 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001763 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001764 "unknown column \"%s\" in foreign key definition",
1765 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001766 goto fk_end;
1767 }
1768 }
1769 }
1770 if( pToCol ){
1771 for(i=0; i<nCol; i++){
1772 int n = strlen(pToCol->a[i].zName);
1773 pFKey->aCol[i].zCol = z;
1774 memcpy(z, pToCol->a[i].zName, n);
1775 z[n] = 0;
1776 z += n+1;
1777 }
1778 }
1779 pFKey->isDeferred = 0;
1780 pFKey->deleteConf = flags & 0xff;
1781 pFKey->updateConf = (flags >> 8 ) & 0xff;
1782 pFKey->insertConf = (flags >> 16 ) & 0xff;
1783
1784 /* Link the foreign key to the table as the last step.
1785 */
1786 p->pFKey = pFKey;
1787 pFKey = 0;
1788
1789fk_end:
1790 sqliteFree(pFKey);
danielk19770202b292004-06-09 09:55:16 +00001791 sqlite3ExprListDelete(pFromCol);
1792 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001793}
1794
1795/*
1796** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1797** clause is seen as part of a foreign key definition. The isDeferred
1798** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1799** The behavior of the most recently created foreign key is adjusted
1800** accordingly.
1801*/
danielk19774adee202004-05-08 08:23:19 +00001802void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001803 Table *pTab;
1804 FKey *pFKey;
1805 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1806 pFKey->isDeferred = isDeferred;
1807}
1808
1809/*
drh75897232000-05-29 14:26:00 +00001810** Create a new index for an SQL table. pIndex is the name of the index
1811** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001812** be NULL for a primary key or an index that is created to satisfy a
1813** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001814** as the table to be indexed. pParse->pNewTable is a table that is
1815** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001816**
drh382c0242001-10-06 16:33:02 +00001817** pList is a list of columns to be indexed. pList will be NULL if this
1818** is a primary key or unique-constraint on the most recent column added
1819** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001820*/
danielk19774adee202004-05-08 08:23:19 +00001821void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001822 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001823 Token *pName1, /* First part of index name. May be NULL */
1824 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00001825 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
1826 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001827 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001828 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1829 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1830){
danielk1977cbb18d22004-05-28 11:37:27 +00001831 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00001832 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00001833 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001834 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001835 Token nullId; /* Fake token for an empty ID list */
1836 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001837 int isTemp; /* True for a temporary index */
drh9bb575f2004-09-06 17:24:11 +00001838 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001839
danielk1977cbb18d22004-05-28 11:37:27 +00001840 int iDb; /* Index of the database that is being written */
1841 Token *pName = 0; /* Unqualified name of the index to create */
1842
danielk197724b03fd2004-05-10 10:34:34 +00001843 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001844
drh75897232000-05-29 14:26:00 +00001845 /*
1846 ** Find the table that is to be indexed. Return early if not found.
1847 */
danielk1977cbb18d22004-05-28 11:37:27 +00001848 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001849
1850 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001851 ** to search for the table. 'Fix' the table name to this db
1852 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001853 */
1854 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001855 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001856 if( iDb<0 ) goto exit_create_index;
1857
danielk1977ef2cb632004-05-29 02:37:19 +00001858 /* If the index name was unqualified, check if the the table
1859 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001860 */
danielk1977ef2cb632004-05-29 02:37:19 +00001861 pTab = sqlite3SrcListLookup(pParse, pTblName);
1862 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1863 iDb = 1;
1864 }
1865
1866 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1867 sqlite3FixSrcList(&sFix, pTblName)
1868 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001869 goto exit_create_index;
1870 }
danielk1977ef2cb632004-05-29 02:37:19 +00001871 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1872 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001873 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001874 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001875 }else{
drhe3c41372001-09-17 20:25:58 +00001876 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001877 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001878 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001879 }
danielk1977cbb18d22004-05-28 11:37:27 +00001880
drh75897232000-05-29 14:26:00 +00001881 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001882 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001883 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001884 goto exit_create_index;
1885 }
drha76b5df2002-02-23 02:32:10 +00001886 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001887 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001888 goto exit_create_index;
1889 }
drh4925ca02003-11-27 00:48:57 +00001890 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001891
1892 /*
1893 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001894 ** index or table with the same name.
1895 **
1896 ** Exception: If we are reading the names of permanent indices from the
1897 ** sqlite_master table (because some other process changed the schema) and
1898 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001899 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001900 **
1901 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001902 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1903 ** own name.
drh75897232000-05-29 14:26:00 +00001904 */
danielk1977d8123362004-06-12 09:25:12 +00001905 if( pName ){
drha99db3b2004-06-19 14:49:12 +00001906 zName = sqlite3NameFromToken(pName);
danielk19778a414492004-06-29 08:59:35 +00001907 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001908 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001909 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00001910 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001911 }
danielk1977d8123362004-06-12 09:25:12 +00001912 if( !db->init.busy ){
1913 Index *pISameName; /* Another index with the same name */
1914 Table *pTSameName; /* A table with same name as the index */
danielk19778a414492004-06-29 08:59:35 +00001915 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001916 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
1917 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
1918 goto exit_create_index;
1919 }
1920 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1921 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
1922 goto exit_create_index;
1923 }
drhe3c41372001-09-17 20:25:58 +00001924 }
drhd24cc422003-03-27 12:51:24 +00001925 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001926 char zBuf[30];
1927 int n;
1928 Index *pLoop;
1929 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00001930 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00001931 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00001932 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001933 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00001934 }
1935
drhe5f9c642003-01-13 23:27:31 +00001936 /* Check for authorization to create an index.
1937 */
1938#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001939 {
1940 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001941 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001942 goto exit_create_index;
1943 }
1944 i = SQLITE_CREATE_INDEX;
1945 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001946 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001947 goto exit_create_index;
1948 }
drhe5f9c642003-01-13 23:27:31 +00001949 }
1950#endif
1951
drh75897232000-05-29 14:26:00 +00001952 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001953 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001954 ** So create a fake list to simulate this.
1955 */
1956 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001957 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001958 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00001959 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00001960 if( pList==0 ) goto exit_create_index;
1961 }
1962
1963 /*
1964 ** Allocate the index structure.
1965 */
drhdcc581c2000-05-30 13:44:19 +00001966 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00001967 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00001968 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00001969 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
1970 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00001971 strcpy(pIndex->zName, zName);
1972 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00001973 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00001974 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001975 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00001976 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00001977
drh1ccde152000-06-17 13:12:39 +00001978 /* Scan the names of the columns of the table to be indexed and
1979 ** load the column indices into the Index structure. Report an error
1980 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001981 */
danielk19770202b292004-06-09 09:55:16 +00001982 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00001983 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001984 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001985 }
1986 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001987 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00001988 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001989 goto exit_create_index;
1990 }
drh967e8b72000-06-21 13:59:10 +00001991 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00001992 if( pList->a[i].pExpr ){
1993 assert( pList->a[i].pExpr->pColl );
1994 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
1995 }else{
1996 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
1997 }
1998 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00001999 if( !db->init.busy &&
2000 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2001 ){
2002 goto exit_create_index;
2003 }
drh75897232000-05-29 14:26:00 +00002004 }
danielk19770202b292004-06-09 09:55:16 +00002005 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002006
danielk1977d8123362004-06-12 09:25:12 +00002007 if( pTab==pParse->pNewTable ){
2008 /* This routine has been called to create an automatic index as a
2009 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2010 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2011 ** i.e. one of:
2012 **
2013 ** CREATE TABLE t(x PRIMARY KEY, y);
2014 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2015 **
2016 ** Either way, check to see if the table already has such an index. If
2017 ** so, don't bother creating this one. This only applies to
2018 ** automatically created indices. Users can do as they wish with
2019 ** explicit indices.
2020 */
2021 Index *pIdx;
2022 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2023 int k;
2024 assert( pIdx->onError!=OE_None );
2025 assert( pIdx->autoIndex );
2026 assert( pIndex->onError!=OE_None );
2027
2028 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2029 for(k=0; k<pIdx->nColumn; k++){
2030 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2031 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2032 }
2033 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002034 if( pIdx->onError!=pIndex->onError ){
2035 /* This constraint creates the same index as a previous
2036 ** constraint specified somewhere in the CREATE TABLE statement.
2037 ** However the ON CONFLICT clauses are different. If both this
2038 ** constraint and the previous equivalent constraint have explicit
2039 ** ON CONFLICT clauses this is an error. Otherwise, use the
2040 ** explicitly specified behaviour for the index.
2041 */
2042 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2043 sqlite3ErrorMsg(pParse,
2044 "conflicting ON CONFLICT clauses specified", 0);
2045 }
2046 if( pIdx->onError==OE_Default ){
2047 pIdx->onError = pIndex->onError;
2048 }
2049 }
danielk1977d8123362004-06-12 09:25:12 +00002050 goto exit_create_index;
2051 }
2052 }
2053 }
2054
drh75897232000-05-29 14:26:00 +00002055 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002056 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002057 */
drh234c39d2004-07-24 03:30:47 +00002058 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002059 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002060 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002061 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002062 if( p ){
2063 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002064 goto exit_create_index;
2065 }
drh5e00f6c2001-09-13 13:46:56 +00002066 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002067 if( pTblName!=0 ){
2068 pIndex->tnum = db->init.newTnum;
2069 }
drhd78eeee2001-09-13 16:18:53 +00002070 }
2071
drh1d85d932004-02-14 23:05:52 +00002072 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002073 ** involves writing the index into the master table and filling in the
2074 ** index with the current table contents.
2075 **
drh1d85d932004-02-14 23:05:52 +00002076 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2077 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002078 ** CREATE INDEX statements are read out of the master table. In
2079 ** the latter case the index already exists on disk, which is why
2080 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002081 **
danielk1977cbb18d22004-05-28 11:37:27 +00002082 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002083 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2084 ** has just been created, it contains no data and the index initialization
2085 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002086 */
drh1d85d932004-02-14 23:05:52 +00002087 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00002088 int n;
drhadbca9c2001-09-27 15:11:53 +00002089 Vdbe *v;
drh75897232000-05-29 14:26:00 +00002090 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00002091
danielk19774adee202004-05-08 08:23:19 +00002092 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002093 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00002094 if( pTblName!=0 ){
2095 sqlite3BeginWriteOperation(pParse, 0, iDb);
2096 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00002097 }
danielk19774adee202004-05-08 08:23:19 +00002098 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00002099 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
2100 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
2101 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
drh234c39d2004-07-24 03:30:47 +00002102 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002103 if( pTblName ){
drha99db3b2004-06-19 14:49:12 +00002104 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2105 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002106 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
2107 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00002108 }
danielk19770f69c1e2004-05-29 11:24:50 +00002109 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00002110 if( pStart && pEnd ){
danielk19770202b292004-06-09 09:55:16 +00002111 if( onError==OE_None ){
2112 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
2113 }else{
2114 sqlite3VdbeChangeP3(v, -1, "CREATE UNIQUE INDEX ", P3_STATIC);
2115 }
danielk19770f69c1e2004-05-29 11:24:50 +00002116 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002117 n = Addr(pEnd->z) - Addr(pName->z) + 1;
2118 sqlite3VdbeChangeP3(v, -1, pName->z, n);
drh855eb1c2004-08-31 13:45:11 +00002119 sqlite3VdbeAddOp(v, OP_Concat, 0, 0);
drh75897232000-05-29 14:26:00 +00002120 }
danielk197784ac9d02004-05-18 09:58:06 +00002121 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002122 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002123 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00002124 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002125 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
2126 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00002127 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00002128 lbl2 = sqlite3VdbeMakeLabel(v);
2129 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00002130 lbl1 = sqlite3VdbeCurrentAddr(v);
2131 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00002132 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00002133 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002134 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
2135 sqlite3VdbeResolveLabel(v, lbl2);
2136 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
2137 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drhc275b4e2004-07-19 17:25:24 +00002138 sqlite3ChangeCookie(db, v, iDb);
danielk19774adee202004-05-08 08:23:19 +00002139 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2140 sqlite3EndWriteOperation(pParse);
drh234c39d2004-07-24 03:30:47 +00002141 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
2142 sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC);
drh5e00f6c2001-09-13 13:46:56 +00002143 }
drh75897232000-05-29 14:26:00 +00002144 }
2145
danielk1977d8123362004-06-12 09:25:12 +00002146 /* When adding an index to the list of indices for a table, make
2147 ** sure all indices labeled OE_Replace come after all those labeled
2148 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2149 ** and INSERT.
2150 */
drh234c39d2004-07-24 03:30:47 +00002151 if( db->init.busy || pTblName==0 ){
2152 if( onError!=OE_Replace || pTab->pIndex==0
2153 || pTab->pIndex->onError==OE_Replace){
2154 pIndex->pNext = pTab->pIndex;
2155 pTab->pIndex = pIndex;
2156 }else{
2157 Index *pOther = pTab->pIndex;
2158 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2159 pOther = pOther->pNext;
2160 }
2161 pIndex->pNext = pOther->pNext;
2162 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002163 }
drh234c39d2004-07-24 03:30:47 +00002164 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002165 }
danielk1977d8123362004-06-12 09:25:12 +00002166
drh75897232000-05-29 14:26:00 +00002167 /* Clean up before exiting */
2168exit_create_index:
drh956bc922004-07-24 17:38:29 +00002169 if( pIndex ){
2170 freeIndex(pIndex);
2171 }
danielk19770202b292004-06-09 09:55:16 +00002172 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002173 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002174 sqliteFree(zName);
2175 return;
2176}
2177
2178/*
drh74e24cd2002-01-09 03:19:59 +00002179** This routine will drop an existing named index. This routine
2180** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002181*/
danielk19774adee202004-05-08 08:23:19 +00002182void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002183 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002184 Vdbe *v;
drh9bb575f2004-09-06 17:24:11 +00002185 sqlite3 *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002186
danielk197724b03fd2004-05-10 10:34:34 +00002187 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002188 assert( pName->nSrc==1 );
danielk19778a414492004-06-29 08:59:35 +00002189 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19774adee202004-05-08 08:23:19 +00002190 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002191 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002192 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002193 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002194 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002195 }
drh485b39b2002-07-13 03:11:52 +00002196 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002197 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002198 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002199 goto exit_drop_index;
2200 }
drhe5f9c642003-01-13 23:27:31 +00002201#ifndef SQLITE_OMIT_AUTHORIZATION
2202 {
2203 int code = SQLITE_DROP_INDEX;
2204 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002205 const char *zDb = db->aDb[pIndex->iDb].zName;
2206 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002207 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002208 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002209 }
drhd24cc422003-03-27 12:51:24 +00002210 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002211 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002212 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002213 }
drhed6c8672003-01-12 18:02:16 +00002214 }
drhe5f9c642003-01-13 23:27:31 +00002215#endif
drh75897232000-05-29 14:26:00 +00002216
2217 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002218 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002219 if( v ){
drh905793e2004-02-21 13:31:09 +00002220 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00002221 { OP_Rewind, 0, ADDR(9), 0},
drh234c39d2004-07-24 03:30:47 +00002222 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00002223 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002224 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00002225 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002226 { OP_Eq, 0, ADDR(8), 0},
2227 { OP_Next, 0, ADDR(3), 0},
2228 { OP_Goto, 0, ADDR(9), 0},
2229 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00002230 };
2231 int base;
2232
danielk19774adee202004-05-08 08:23:19 +00002233 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
2234 sqlite3OpenMasterTable(v, pIndex->iDb);
2235 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
2236 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
drhc275b4e2004-07-19 17:25:24 +00002237 sqlite3ChangeCookie(db, v, pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002238 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2239 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh956bc922004-07-24 17:38:29 +00002240 sqlite3VdbeOp3(v, OP_DropIndex, pIndex->iDb, 0, pIndex->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00002241 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00002242 }
2243
drhd24cc422003-03-27 12:51:24 +00002244exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002245 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002246}
2247
2248/*
drh75897232000-05-29 14:26:00 +00002249** Append a new element to the given IdList. Create a new IdList if
2250** need be.
drhdaffd0e2001-04-11 14:28:42 +00002251**
2252** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002253*/
danielk19774adee202004-05-08 08:23:19 +00002254IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002255 if( pList==0 ){
2256 pList = sqliteMalloc( sizeof(IdList) );
2257 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002258 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002259 }
drh4305d102003-07-30 12:34:12 +00002260 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002261 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002262 pList->nAlloc = pList->nAlloc*2 + 5;
2263 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002264 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002265 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002266 return 0;
drh75897232000-05-29 14:26:00 +00002267 }
drh6d4abfb2001-10-22 02:58:08 +00002268 pList->a = a;
drh75897232000-05-29 14:26:00 +00002269 }
2270 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
drha99db3b2004-06-19 14:49:12 +00002271 pList->a[pList->nId].zName = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002272 pList->nId++;
2273 return pList;
2274}
2275
2276/*
drhad3cab52002-05-24 02:04:32 +00002277** Append a new table name to the given SrcList. Create a new SrcList if
2278** need be. A new entry is created in the SrcList even if pToken is NULL.
2279**
2280** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002281**
2282** If pDatabase is not null, it means that the table has an optional
2283** database name prefix. Like this: "database.table". The pDatabase
2284** points to the table name and the pTable points to the database name.
2285** The SrcList.a[].zName field is filled with the table name which might
2286** come from pTable (if pDatabase is NULL) or from pDatabase.
2287** SrcList.a[].zDatabase is filled with the database name from pTable,
2288** or with NULL if no database is specified.
2289**
2290** In other words, if call like this:
2291**
danielk19774adee202004-05-08 08:23:19 +00002292** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002293**
2294** Then B is a table name and the database name is unspecified. If called
2295** like this:
2296**
danielk19774adee202004-05-08 08:23:19 +00002297** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002298**
2299** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002300*/
danielk19774adee202004-05-08 08:23:19 +00002301SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drha99db3b2004-06-19 14:49:12 +00002302 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002303 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002304 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002305 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002306 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002307 }
drh4305d102003-07-30 12:34:12 +00002308 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002309 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002310 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002311 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002312 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002313 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002314 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002315 return 0;
2316 }
drh113088e2003-03-20 01:16:58 +00002317 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002318 }
drha99db3b2004-06-19 14:49:12 +00002319 pItem = &pList->a[pList->nSrc];
2320 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002321 if( pDatabase && pDatabase->z==0 ){
2322 pDatabase = 0;
2323 }
2324 if( pDatabase && pTable ){
2325 Token *pTemp = pDatabase;
2326 pDatabase = pTable;
2327 pTable = pTemp;
2328 }
drha99db3b2004-06-19 14:49:12 +00002329 pItem->zName = sqlite3NameFromToken(pTable);
2330 pItem->zDatabase = sqlite3NameFromToken(pDatabase);
2331 pItem->iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002332 pList->nSrc++;
2333 return pList;
2334}
2335
2336/*
drh63eb5f22003-04-29 16:20:44 +00002337** Assign cursors to all tables in a SrcList
2338*/
danielk19774adee202004-05-08 08:23:19 +00002339void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002340 int i;
2341 for(i=0; i<pList->nSrc; i++){
2342 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002343 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002344 }
2345 }
2346}
2347
2348/*
drh75897232000-05-29 14:26:00 +00002349** Add an alias to the last identifier on the given identifier list.
2350*/
danielk19774adee202004-05-08 08:23:19 +00002351void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002352 if( pList && pList->nSrc>0 ){
drha99db3b2004-06-19 14:49:12 +00002353 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002354 }
2355}
2356
2357/*
drhad3cab52002-05-24 02:04:32 +00002358** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002359*/
danielk19774adee202004-05-08 08:23:19 +00002360void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002361 int i;
2362 if( pList==0 ) return;
2363 for(i=0; i<pList->nId; i++){
2364 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002365 }
2366 sqliteFree(pList->a);
2367 sqliteFree(pList);
2368}
2369
2370/*
drhad2d8302002-05-24 20:31:36 +00002371** Return the index in pList of the identifier named zId. Return -1
2372** if not found.
2373*/
danielk19774adee202004-05-08 08:23:19 +00002374int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002375 int i;
2376 if( pList==0 ) return -1;
2377 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002378 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002379 }
2380 return -1;
2381}
2382
2383/*
drhad3cab52002-05-24 02:04:32 +00002384** Delete an entire SrcList including all its substructure.
2385*/
danielk19774adee202004-05-08 08:23:19 +00002386void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002387 int i;
drhbe5c89a2004-07-26 00:31:09 +00002388 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002389 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00002390 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
2391 sqliteFree(pItem->zDatabase);
2392 sqliteFree(pItem->zName);
2393 sqliteFree(pItem->zAlias);
2394 if( pItem->pTab && pItem->pTab->isTransient ){
2395 sqlite3DeleteTable(0, pItem->pTab);
drhdaffd0e2001-04-11 14:28:42 +00002396 }
drhbe5c89a2004-07-26 00:31:09 +00002397 sqlite3SelectDelete(pItem->pSelect);
2398 sqlite3ExprDelete(pItem->pOn);
2399 sqlite3IdListDelete(pItem->pUsing);
drh75897232000-05-29 14:26:00 +00002400 }
drh75897232000-05-29 14:26:00 +00002401 sqliteFree(pList);
2402}
2403
drh982cef72000-05-30 16:27:03 +00002404/*
drhc4a3c772001-04-04 11:48:57 +00002405** Begin a transaction
2406*/
danielk197733752f82004-05-31 08:55:33 +00002407void sqlite3BeginTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002408 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002409 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002410
drh001bbcb2003-03-19 03:14:00 +00002411 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002412 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002413 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002414
2415 v = sqlite3GetVdbe(pParse);
2416 if( !v ) return;
2417 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002418}
2419
2420/*
2421** Commit a transaction
2422*/
danielk19774adee202004-05-08 08:23:19 +00002423void sqlite3CommitTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002424 sqlite3 *db;
danielk19771d850a72004-05-31 08:26:49 +00002425 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002426
drh001bbcb2003-03-19 03:14:00 +00002427 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002428 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002429 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002430
2431 v = sqlite3GetVdbe(pParse);
2432 if( v ){
2433 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002434 }
drhc4a3c772001-04-04 11:48:57 +00002435}
2436
2437/*
2438** Rollback a transaction
2439*/
danielk19774adee202004-05-08 08:23:19 +00002440void sqlite3RollbackTransaction(Parse *pParse){
drh9bb575f2004-09-06 17:24:11 +00002441 sqlite3 *db;
drh5e00f6c2001-09-13 13:46:56 +00002442 Vdbe *v;
2443
drh001bbcb2003-03-19 03:14:00 +00002444 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002445 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002446 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002447
danielk19774adee202004-05-08 08:23:19 +00002448 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002449 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002450 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002451 }
drhc4a3c772001-04-04 11:48:57 +00002452}
drhf57b14a2001-09-14 18:54:08 +00002453
2454/*
drhdc3ff9c2004-08-18 02:10:15 +00002455** Make sure the TEMP database is open and available for use. Return
2456** the number of errors. Leave any error messages in the pParse structure.
2457*/
2458static int sqlite3OpenTempDatabase(Parse *pParse){
2459 sqlite3 *db = pParse->db;
2460 if( db->aDb[1].pBt==0 && !pParse->explain ){
2461 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
2462 if( rc!=SQLITE_OK ){
2463 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
2464 "file for storing temporary tables");
2465 pParse->rc = rc;
2466 return 1;
2467 }
2468 if( db->flags & !db->autoCommit ){
2469 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
2470 if( rc!=SQLITE_OK ){
2471 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
2472 "the temporary database file");
2473 pParse->rc = rc;
2474 return 1;
2475 }
2476 }
2477 }
2478 return 0;
2479}
2480
2481/*
drh80242052004-06-09 00:48:12 +00002482** Generate VDBE code that will verify the schema cookie and start
2483** a read-transaction for all named database files.
2484**
2485** It is important that all schema cookies be verified and all
2486** read transactions be started before anything else happens in
2487** the VDBE program. But this routine can be called after much other
2488** code has been generated. So here is what we do:
2489**
drhc275b4e2004-07-19 17:25:24 +00002490** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00002491** will jump to a subroutine at the end of the program. Then we
2492** record every database that needs its schema verified in the
2493** pParse->cookieMask field. Later, after all other code has been
2494** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00002495** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00002496** will be made to point to that subroutine. The generation of the
2497** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00002498**
2499** If iDb<0 then code the OP_Goto only - don't set flag to verify the
2500** schema on any databases. This can be used to position the OP_Goto
2501** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00002502*/
danielk19774adee202004-05-08 08:23:19 +00002503void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh9bb575f2004-09-06 17:24:11 +00002504 sqlite3 *db;
drh80242052004-06-09 00:48:12 +00002505 Vdbe *v;
2506 int mask;
2507
2508 v = sqlite3GetVdbe(pParse);
2509 if( v==0 ) return; /* This only happens if there was a prior error */
2510 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00002511 if( pParse->cookieGoto==0 ){
2512 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00002513 }
drhc275b4e2004-07-19 17:25:24 +00002514 if( iDb>=0 ){
2515 assert( iDb<db->nDb );
2516 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2517 assert( iDb<32 );
2518 mask = 1<<iDb;
2519 if( (pParse->cookieMask & mask)==0 ){
2520 pParse->cookieMask |= mask;
2521 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
drhdc3ff9c2004-08-18 02:10:15 +00002522 if( iDb==1 ){
2523 sqlite3OpenTempDatabase(pParse);
2524 }
drhc275b4e2004-07-19 17:25:24 +00002525 }
drh001bbcb2003-03-19 03:14:00 +00002526 }
drh001bbcb2003-03-19 03:14:00 +00002527}
2528
2529/*
drh1c928532002-01-31 15:54:21 +00002530** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002531** might change the database.
2532**
2533** This routine starts a new transaction if we are not already within
2534** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002535** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002536** be set for operations that might fail (due to a constraint) part of
2537** the way through and which will need to undo some writes without having to
2538** rollback the whole transaction. For operations where all constraints
2539** can be checked before any changes are made to the database, it is never
2540** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002541**
drh8bf8dc92003-05-17 17:35:10 +00002542** Only database iDb and the temp database are made writable by this call.
2543** If iDb==0, then the main and temp databases are made writable. If
2544** iDb==1 then only the temp database is made writable. If iDb>1 then the
2545** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002546*/
drh7f0f12e2004-05-21 13:39:50 +00002547void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002548 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002549 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002550 sqlite3CodeVerifySchema(pParse, iDb);
2551 pParse->writeMask |= 1<<iDb;
danielk19771d850a72004-05-31 08:26:49 +00002552 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002553 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002554 }
drh34f47322004-08-18 15:58:22 +00002555 if( iDb!=1 && pParse->db->aDb[1].pBt!=0 ){
danielk19771d850a72004-05-31 08:26:49 +00002556 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002557 }
2558}
2559
2560/*
drh1c928532002-01-31 15:54:21 +00002561** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002562** the database. If a statement transaction was started, then emit
2563** an OP_Commit that will cause the changes to be committed to disk.
2564**
2565** Note that checkpoints are automatically committed at the end of
2566** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002567** sqlite3BeginWriteOperation() but there should only be a single
2568** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002569*/
danielk19774adee202004-05-08 08:23:19 +00002570void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002571 /* Delete me! */
2572 return;
drh1c928532002-01-31 15:54:21 +00002573}
danielk1977bfd6cce2004-06-18 04:24:54 +00002574
2575/*
2576** Return the transient sqlite3_value object used for encoding conversions
2577** during SQL compilation.
2578*/
drh9bb575f2004-09-06 17:24:11 +00002579sqlite3_value *sqlite3GetTransientValue(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +00002580 if( !db->pValue ){
2581 db->pValue = sqlite3ValueNew();
2582 }
2583 return db->pValue;
2584}