blob: 764169df47e9d644b6cbfd0ea899d98dc1bc5f18 [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**
drh71c697e2004-08-08 23:39:19 +000026** $Id: build.c,v 1.246 2004/08/08 23:39:19 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){
53 sqlite *db;
54 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);
99 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +0000100 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000101 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +0000102 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +0000103 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000104 }
drha226d052002-09-25 19:04:07 +0000105 pParse->nTab = 0;
106 pParse->nMem = 0;
107 pParse->nSet = 0;
108 pParse->nAgg = 0;
drh7c972de2003-09-06 22:18:07 +0000109 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000110 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000111 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000112}
113
114/*
danielk19778a414492004-06-29 08:59:35 +0000115** Locate the in-memory structure that describes a particular database
116** table given the name of that table and (optionally) the name of the
117** database containing the table. Return NULL if not found.
drha69d9162003-04-17 22:57:53 +0000118**
danielk19778a414492004-06-29 08:59:35 +0000119** If zDatabase is 0, all databases are searched for the table and the
120** first matching table is returned. (No checking for duplicate table
121** names is done.) The search order is TEMP first, then MAIN, then any
122** auxiliary databases added using the ATTACH command.
drhf26e09c2003-05-31 16:21:12 +0000123**
danielk19774adee202004-05-08 08:23:19 +0000124** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000125*/
danielk19774adee202004-05-08 08:23:19 +0000126Table *sqlite3FindTable(sqlite *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000127 Table *p = 0;
128 int i;
drh645f63e2004-06-22 13:22:40 +0000129 assert( zName!=0 );
danielk19778a414492004-06-29 08:59:35 +0000130 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
131 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000132 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000133 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
134 p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000135 if( p ) break;
136 }
drh74e24cd2002-01-09 03:19:59 +0000137 return p;
drh75897232000-05-29 14:26:00 +0000138}
139
140/*
danielk19778a414492004-06-29 08:59:35 +0000141** Locate the in-memory structure that describes a particular database
142** table given the name of that table and (optionally) the name of the
143** database containing the table. Return NULL if not found. Also leave an
144** error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000145**
danielk19778a414492004-06-29 08:59:35 +0000146** The difference between this routine and sqlite3FindTable() is that this
147** routine leaves an error message in pParse->zErrMsg where
148** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000149*/
danielk19774adee202004-05-08 08:23:19 +0000150Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000151 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000152
danielk19778a414492004-06-29 08:59:35 +0000153 /* Read the database schema. If an error occurs, leave an error message
154 ** and code in pParse and return NULL. */
155 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
156 return 0;
157 }
158
danielk19774adee202004-05-08 08:23:19 +0000159 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000160 if( p==0 ){
danielk19778a414492004-06-29 08:59:35 +0000161 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000162 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
163 }else if( sqlite3FindTable(pParse->db, zName, 0)!=0 ){
164 sqlite3ErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
drhf26e09c2003-05-31 16:21:12 +0000165 zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000166 }else{
danielk19774adee202004-05-08 08:23:19 +0000167 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000168 }
drha6ecd332004-06-10 00:29:09 +0000169 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000170 }
171 return p;
172}
173
174/*
175** Locate the in-memory structure that describes
176** a particular index given the name of that index
177** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000178** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000179**
180** If zDatabase is 0, all databases are searched for the
181** table and the first matching index is returned. (No checking
182** for duplicate index names is done.) The search order is
183** TEMP first, then MAIN, then any auxiliary databases added
184** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000185*/
danielk19774adee202004-05-08 08:23:19 +0000186Index *sqlite3FindIndex(sqlite *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000187 Index *p = 0;
188 int i;
danielk19778a414492004-06-29 08:59:35 +0000189 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
190 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000191 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000192 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
193 p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000194 if( p ) break;
195 }
drh74e24cd2002-01-09 03:19:59 +0000196 return p;
drh75897232000-05-29 14:26:00 +0000197}
198
199/*
drh956bc922004-07-24 17:38:29 +0000200** Reclaim the memory used by an index
201*/
202static void freeIndex(Index *p){
203 sqliteFree(p->zColAff);
204 sqliteFree(p);
205}
206
207/*
drh75897232000-05-29 14:26:00 +0000208** Remove the given index from the index hash table, and free
209** its memory structures.
210**
drhd229ca92002-01-09 13:30:41 +0000211** The index is removed from the database hash tables but
212** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000213** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000214*/
drh74e24cd2002-01-09 03:19:59 +0000215static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000216 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000217
drhd229ca92002-01-09 13:30:41 +0000218 assert( db!=0 && p->zName!=0 );
danielk19774adee202004-05-08 08:23:19 +0000219 pOld = sqlite3HashInsert(&db->aDb[p->iDb].idxHash, p->zName,
drhd24cc422003-03-27 12:51:24 +0000220 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000221 if( pOld!=0 && pOld!=p ){
danielk19774adee202004-05-08 08:23:19 +0000222 sqlite3HashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
drhd24cc422003-03-27 12:51:24 +0000223 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000224 }
drh956bc922004-07-24 17:38:29 +0000225 freeIndex(p);
drh75897232000-05-29 14:26:00 +0000226}
227
228/*
drhbeae3192001-09-22 18:12:08 +0000229** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000230** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000231** structures.
232*/
drh956bc922004-07-24 17:38:29 +0000233void sqlite3UnlinkAndDeleteIndex(sqlite *db, int iDb, const char *zIdxName){
234 Index *pIndex;
235 int len;
236
237 len = strlen(zIdxName);
238 pIndex = sqlite3HashInsert(&db->aDb[iDb].idxHash, zIdxName, len+1, 0);
239 if( pIndex ){
240 if( pIndex->pTable->pIndex==pIndex ){
241 pIndex->pTable->pIndex = pIndex->pNext;
242 }else{
243 Index *p;
244 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
245 if( p && p->pNext==pIndex ){
246 p->pNext = pIndex->pNext;
247 }
drh5e00f6c2001-09-13 13:46:56 +0000248 }
drh956bc922004-07-24 17:38:29 +0000249 freeIndex(pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000250 }
drh956bc922004-07-24 17:38:29 +0000251 db->flags |= SQLITE_InternChanges;
drh5e00f6c2001-09-13 13:46:56 +0000252}
253
254/*
drhe0bc4042002-06-25 01:09:11 +0000255** Erase all schema information from the in-memory hash tables of
drh234c39d2004-07-24 03:30:47 +0000256** a single database. This routine is called to reclaim memory
257** before the database closes. It is also called during a rollback
danielk1977e0d4b062004-06-28 01:11:46 +0000258** if there were schema changes during the transaction or if a
259** schema-cookie mismatch occurs.
drh1c2d8412003-03-31 00:30:47 +0000260**
261** If iDb<=0 then reset the internal schema tables for all database
262** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000263** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000264*/
danielk19774adee202004-05-08 08:23:19 +0000265void sqlite3ResetInternalSchema(sqlite *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000266 HashElem *pElem;
267 Hash temp1;
268 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000269 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000270
drh1c2d8412003-03-31 00:30:47 +0000271 assert( iDb>=0 && iDb<db->nDb );
272 db->flags &= ~SQLITE_Initialized;
273 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000274 Db *pDb = &db->aDb[i];
275 temp1 = pDb->tblHash;
276 temp2 = pDb->trigHash;
danielk19774adee202004-05-08 08:23:19 +0000277 sqlite3HashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
278 sqlite3HashClear(&pDb->aFKey);
279 sqlite3HashClear(&pDb->idxHash);
drhd24cc422003-03-27 12:51:24 +0000280 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
281 Trigger *pTrigger = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000282 sqlite3DeleteTrigger(pTrigger);
drhd24cc422003-03-27 12:51:24 +0000283 }
danielk19774adee202004-05-08 08:23:19 +0000284 sqlite3HashClear(&temp2);
285 sqlite3HashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
drhd24cc422003-03-27 12:51:24 +0000286 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
287 Table *pTab = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000288 sqlite3DeleteTable(db, pTab);
drhd24cc422003-03-27 12:51:24 +0000289 }
danielk19774adee202004-05-08 08:23:19 +0000290 sqlite3HashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000291 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000292 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000293 }
drh1c2d8412003-03-31 00:30:47 +0000294 assert( iDb==0 );
295 db->flags &= ~SQLITE_InternChanges;
296
297 /* If one or more of the auxiliary database files has been closed,
298 ** then remove then from the auxiliary database list. We take the
299 ** opportunity to do this here since we have just deleted all of the
300 ** schema hash tables and therefore do not have to make any changes
301 ** to any of those tables.
302 */
drh4d189ca2004-02-12 18:46:38 +0000303 for(i=0; i<db->nDb; i++){
304 struct Db *pDb = &db->aDb[i];
305 if( pDb->pBt==0 ){
306 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
307 pDb->pAux = 0;
308 }
309 }
drh1c2d8412003-03-31 00:30:47 +0000310 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000311 struct Db *pDb = &db->aDb[i];
312 if( pDb->pBt==0 ){
313 sqliteFree(pDb->zName);
314 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000315 continue;
316 }
317 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000318 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000319 }
drh8bf8dc92003-05-17 17:35:10 +0000320 j++;
drh1c2d8412003-03-31 00:30:47 +0000321 }
322 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
323 db->nDb = j;
324 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
325 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
326 sqliteFree(db->aDb);
327 db->aDb = db->aDbStatic;
328 }
drhe0bc4042002-06-25 01:09:11 +0000329}
330
331/*
332** This routine is called whenever a rollback occurs. If there were
333** schema changes during the transaction, then we have to reset the
334** internal hash tables and reload them from disk.
335*/
danielk19774adee202004-05-08 08:23:19 +0000336void sqlite3RollbackInternalChanges(sqlite *db){
drhe0bc4042002-06-25 01:09:11 +0000337 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000338 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000339 }
340}
341
342/*
343** This routine is called when a commit occurs.
344*/
danielk19774adee202004-05-08 08:23:19 +0000345void sqlite3CommitInternalChanges(sqlite *db){
drhe0bc4042002-06-25 01:09:11 +0000346 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000347}
348
349/*
drh956bc922004-07-24 17:38:29 +0000350** Clear the column names from a table or view.
351*/
352static void sqliteResetColumnNames(Table *pTable){
353 int i;
354 Column *pCol;
355 assert( pTable!=0 );
356 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
357 sqliteFree(pCol->zName);
358 sqliteFree(pCol->zDflt);
359 sqliteFree(pCol->zType);
360 }
361 sqliteFree(pTable->aCol);
362 pTable->aCol = 0;
363 pTable->nCol = 0;
364}
365
366/*
drh75897232000-05-29 14:26:00 +0000367** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000368** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000369**
370** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000371** the table data structure from the hash table. Nor does it remove
372** foreign keys from the sqlite.aFKey hash table. But it does destroy
373** memory structures of the indices and foreign keys associated with
374** the table.
drhdaffd0e2001-04-11 14:28:42 +0000375**
376** Indices associated with the table are unlinked from the "db"
377** data structure if db!=NULL. If db==NULL, indices attached to
378** the table are deleted, but it is assumed they have already been
379** unlinked.
drh75897232000-05-29 14:26:00 +0000380*/
danielk19774adee202004-05-08 08:23:19 +0000381void sqlite3DeleteTable(sqlite *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000382 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000383 FKey *pFKey, *pNextFKey;
384
drh75897232000-05-29 14:26:00 +0000385 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000386
387 /* Delete all indices associated with this table
388 */
389 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
390 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000391 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000392 sqliteDeleteIndex(db, pIndex);
393 }
394
395 /* Delete all foreign keys associated with this table. The keys
396 ** should have already been unlinked from the db->aFKey hash table
397 */
398 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
399 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000400 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000401 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000402 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000403 sqliteFree(pFKey);
404 }
405
406 /* Delete the Table structure itself.
407 */
drh956bc922004-07-24 17:38:29 +0000408 sqliteResetColumnNames(pTable);
drh6e142f52000-06-08 13:36:40 +0000409 sqliteFree(pTable->zName);
drh956bc922004-07-24 17:38:29 +0000410 sqliteFree(pTable->zColAff);
danielk19774adee202004-05-08 08:23:19 +0000411 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000412 sqliteFree(pTable);
413}
414
415/*
drh5edc3122001-09-13 21:53:09 +0000416** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000417** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000418*/
drh956bc922004-07-24 17:38:29 +0000419void sqlite3UnlinkAndDeleteTable(sqlite *db, int iDb, const char *zTabName){
420 Table *p;
drhc2eef3b2002-08-31 18:53:06 +0000421 FKey *pF1, *pF2;
drh956bc922004-07-24 17:38:29 +0000422 Db *pDb;
423
drhd229ca92002-01-09 13:30:41 +0000424 assert( db!=0 );
drh956bc922004-07-24 17:38:29 +0000425 assert( iDb>=0 && iDb<db->nDb );
426 assert( zTabName && zTabName[0] );
427 pDb = &db->aDb[iDb];
428 p = sqlite3HashInsert(&pDb->tblHash, zTabName, strlen(zTabName)+1, 0);
429 if( p ){
430 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
431 int nTo = strlen(pF1->zTo) + 1;
432 pF2 = sqlite3HashFind(&pDb->aFKey, pF1->zTo, nTo);
433 if( pF2==pF1 ){
434 sqlite3HashInsert(&pDb->aFKey, pF1->zTo, nTo, pF1->pNextTo);
435 }else{
436 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
437 if( pF2 ){
438 pF2->pNextTo = pF1->pNextTo;
439 }
drhc2eef3b2002-08-31 18:53:06 +0000440 }
441 }
drh956bc922004-07-24 17:38:29 +0000442 sqlite3DeleteTable(db, p);
drhc2eef3b2002-08-31 18:53:06 +0000443 }
drh956bc922004-07-24 17:38:29 +0000444 db->flags |= SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000445}
446
447/*
drha99db3b2004-06-19 14:49:12 +0000448** Given a token, return a string that consists of the text of that
449** token with any quotations removed. Space to hold the returned string
450** is obtained from sqliteMalloc() and must be freed by the calling
451** function.
drh75897232000-05-29 14:26:00 +0000452**
drha99db3b2004-06-19 14:49:12 +0000453** Tokens are really just pointers into the original SQL text and so
454** are not \000 terminated and are not persistent. The returned string
455** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000456*/
drha99db3b2004-06-19 14:49:12 +0000457char *sqlite3NameFromToken(Token *pName){
458 char *zName;
459 if( pName ){
460 zName = sqliteStrNDup(pName->z, pName->n);
461 sqlite3Dequote(zName);
462 }else{
463 zName = 0;
464 }
drh75897232000-05-29 14:26:00 +0000465 return zName;
466}
467
468/*
danielk1977cbb18d22004-05-28 11:37:27 +0000469** Open the sqlite_master table stored in database number iDb for
470** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000471*/
danielk1977cbb18d22004-05-28 11:37:27 +0000472void sqlite3OpenMasterTable(Vdbe *v, int iDb){
473 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000474 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000475 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000476}
477
478/*
danielk1977cbb18d22004-05-28 11:37:27 +0000479** The token *pName contains the name of a database (either "main" or
480** "temp" or the name of an attached db). This routine returns the
481** index of the named database in db->aDb[], or -1 if the named db
482** does not exist.
483*/
484int findDb(sqlite3 *db, Token *pName){
485 int i;
drh90f5ecb2004-07-22 01:19:35 +0000486 Db *pDb;
487 for(pDb=db->aDb, i=0; i<db->nDb; i++, pDb++){
488 if( pName->n==strlen(pDb->zName) &&
489 0==sqlite3StrNICmp(pDb->zName, pName->z, pName->n) ){
danielk1977cbb18d22004-05-28 11:37:27 +0000490 return i;
491 }
492 }
493 return -1;
494}
495
drh0e3d7472004-06-19 17:33:07 +0000496/* The table or view or trigger name is passed to this routine via tokens
497** pName1 and pName2. If the table name was fully qualified, for example:
498**
499** CREATE TABLE xxx.yyy (...);
500**
501** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
502** the table name is not fully qualified, i.e.:
503**
504** CREATE TABLE yyy(...);
505**
506** Then pName1 is set to "yyy" and pName2 is "".
507**
508** This routine sets the *ppUnqual pointer to point at the token (pName1 or
509** pName2) that stores the unqualified table name. The index of the
510** database "xxx" is returned.
511*/
danielk1977ef2cb632004-05-29 02:37:19 +0000512int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000513 Parse *pParse, /* Parsing and code generating context */
drh90f5ecb2004-07-22 01:19:35 +0000514 Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */
drh0e3d7472004-06-19 17:33:07 +0000515 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
516 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000517){
drh0e3d7472004-06-19 17:33:07 +0000518 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000519 sqlite3 *db = pParse->db;
520
521 if( pName2 && pName2->n>0 ){
522 assert( !db->init.busy );
523 *pUnqual = pName2;
524 iDb = findDb(db, pName1);
525 if( iDb<0 ){
526 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
527 pParse->nErr++;
528 return -1;
529 }
530 }else{
531 assert( db->init.iDb==0 || db->init.busy );
532 iDb = db->init.iDb;
533 *pUnqual = pName1;
534 }
535 return iDb;
536}
537
538/*
danielk1977d8123362004-06-12 09:25:12 +0000539** This routine is used to check if the UTF-8 string zName is a legal
540** unqualified name for a new schema object (table, index, view or
541** trigger). All names are legal except those that begin with the string
542** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
543** is reserved for internal use.
544*/
545int sqlite3CheckObjectName(Parse *pParse, const char *zName){
546 if( !pParse->db->init.busy && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
547 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
548 return SQLITE_ERROR;
549 }
550 return SQLITE_OK;
551}
552
553/*
drh75897232000-05-29 14:26:00 +0000554** Begin constructing a new table representation in memory. This is
555** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000556** to a CREATE TABLE statement. In particular, this routine is called
557** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000558** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000559** flag is true if the table should be stored in the auxiliary database
560** file instead of in the main database file. This is normally the case
561** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000562** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000563**
drhf57b3392001-10-08 13:22:32 +0000564** The new table record is initialized and put in pParse->pNewTable.
565** As more of the CREATE TABLE statement is parsed, additional action
566** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000567** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000568** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000569*/
danielk19774adee202004-05-08 08:23:19 +0000570void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000571 Parse *pParse, /* Parser context */
572 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000573 Token *pName1, /* First part of the name of the table or view */
574 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000575 int isTemp, /* True if this is a TEMP table */
576 int isView /* True if this is a VIEW */
577){
drh75897232000-05-29 14:26:00 +0000578 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000579 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000580 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000581 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000582 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000583 int iDb; /* Database number to create the table in */
584 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000585
danielk1977cbb18d22004-05-28 11:37:27 +0000586 /* The table or view name to create is passed to this routine via tokens
587 ** pName1 and pName2. If the table name was fully qualified, for example:
588 **
589 ** CREATE TABLE xxx.yyy (...);
590 **
591 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
592 ** the table name is not fully qualified, i.e.:
593 **
594 ** CREATE TABLE yyy(...);
595 **
596 ** Then pName1 is set to "yyy" and pName2 is "".
597 **
598 ** The call below sets the pName pointer to point at the token (pName1 or
599 ** pName2) that stores the unqualified table name. The variable iDb is
600 ** set to the index of the database that the table or view is to be
601 ** created in.
602 */
danielk1977ef2cb632004-05-29 02:37:19 +0000603 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000604 if( iDb<0 ) return;
605 if( isTemp && iDb>1 ){
606 /* If creating a temp table, the name may not be qualified */
607 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
608 pParse->nErr++;
609 return;
610 }
611 if( isTemp ) iDb = 1;
612
613 pParse->sNameToken = *pName;
drha99db3b2004-06-19 14:49:12 +0000614 zName = sqlite3NameFromToken(pName);
danielk1977e0048402004-06-15 16:51:01 +0000615 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000616 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
danielk1977e0048402004-06-15 16:51:01 +0000617 sqliteFree(zName);
danielk1977d8123362004-06-12 09:25:12 +0000618 return;
619 }
drh1d85d932004-02-14 23:05:52 +0000620 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000621#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000622 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000623 {
624 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000625 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000626 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000627 sqliteFree(zName);
628 return;
629 }
drhe5f9c642003-01-13 23:27:31 +0000630 if( isView ){
631 if( isTemp ){
632 code = SQLITE_CREATE_TEMP_VIEW;
633 }else{
634 code = SQLITE_CREATE_VIEW;
635 }
636 }else{
637 if( isTemp ){
638 code = SQLITE_CREATE_TEMP_TABLE;
639 }else{
640 code = SQLITE_CREATE_TABLE;
641 }
642 }
danielk19774adee202004-05-08 08:23:19 +0000643 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000644 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000645 return;
646 }
647 }
648#endif
drhf57b3392001-10-08 13:22:32 +0000649
650 /* Before trying to create a temporary table, make sure the Btree for
651 ** holding temporary tables is open.
652 */
drhd24cc422003-03-27 12:51:24 +0000653 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +0000654 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000655 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000656 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
drhf7a9e1a2004-02-22 18:40:56 +0000657 "file for storing temporary tables");
drhf57b3392001-10-08 13:22:32 +0000658 pParse->nErr++;
danielk19772b444852004-06-29 07:45:33 +0000659 pParse->rc = rc;
danielk1977e0048402004-06-15 16:51:01 +0000660 sqliteFree(zName);
drhf57b3392001-10-08 13:22:32 +0000661 return;
662 }
danielk1977ee5741e2004-05-31 10:01:34 +0000663 if( db->flags & !db->autoCommit ){
danielk197740b38dc2004-06-26 08:38:24 +0000664 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
drhf57b3392001-10-08 13:22:32 +0000665 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000666 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
drhf7a9e1a2004-02-22 18:40:56 +0000667 "the temporary database file");
danielk1977e0048402004-06-15 16:51:01 +0000668 sqliteFree(zName);
danielk19772b444852004-06-29 07:45:33 +0000669 pParse->rc = rc;
drhf57b3392001-10-08 13:22:32 +0000670 return;
671 }
672 }
673 }
674
675 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000676 ** index or table name in the same database. Issue an error message if
677 ** it does.
drhf57b3392001-10-08 13:22:32 +0000678 */
danielk19778a414492004-06-29 08:59:35 +0000679 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19773df6b252004-05-29 10:23:19 +0000680 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
681 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000682 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000683 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000684 return;
drh75897232000-05-29 14:26:00 +0000685 }
danielk19778a414492004-06-29 08:59:35 +0000686 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
687 ( iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000688 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000689 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000690 return;
691 }
692 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000693 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000694 pParse->rc = SQLITE_NOMEM;
695 pParse->nErr++;
drh6d4abfb2001-10-22 02:58:08 +0000696 sqliteFree(zName);
697 return;
698 }
drh75897232000-05-29 14:26:00 +0000699 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000700 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000701 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000702 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000703 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000704 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000705 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000706 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000707
708 /* Begin generating the code that will insert the table record into
709 ** the SQLITE_MASTER table. Note in particular that we must go ahead
710 ** and allocate the record number for the table entry now. Before any
711 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
712 ** indices to be created and the table record must come before the
713 ** indices. Hence, the record number for the table must be allocated
714 ** now.
715 */
danielk19774adee202004-05-08 08:23:19 +0000716 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000717 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977d008cfe2004-06-19 02:22:10 +0000718 /* Every time a new table is created the file-format
719 ** and encoding meta-values are set in the database, in
720 ** case this is the first table created.
721 */
722 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
723 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
724 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
725 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
726
danielk1977cbb18d22004-05-28 11:37:27 +0000727 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000728 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
729 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000730 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000731 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000732 }
drh75897232000-05-29 14:26:00 +0000733}
734
735/*
736** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000737**
738** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000739** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000740** first to get things going. Then this routine is called for each
741** column.
drh75897232000-05-29 14:26:00 +0000742*/
danielk19774adee202004-05-08 08:23:19 +0000743void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000744 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000745 int i;
drha99db3b2004-06-19 14:49:12 +0000746 char *z;
drhc9b84a12002-06-20 11:36:48 +0000747 Column *pCol;
drh75897232000-05-29 14:26:00 +0000748 if( (p = pParse->pNewTable)==0 ) return;
drha99db3b2004-06-19 14:49:12 +0000749 z = sqlite3NameFromToken(pName);
drh97fc3d02002-05-22 21:27:03 +0000750 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000751 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000752 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
753 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000754 sqliteFree(z);
755 return;
756 }
757 }
drh75897232000-05-29 14:26:00 +0000758 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000759 Column *aNew;
760 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
761 if( aNew==0 ) return;
762 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000763 }
drhc9b84a12002-06-20 11:36:48 +0000764 pCol = &p->aCol[p->nCol];
765 memset(pCol, 0, sizeof(p->aCol[0]));
766 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000767
768 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000769 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
770 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000771 */
danielk19774f057f92004-06-08 00:02:33 +0000772 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000773 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000774 p->nCol++;
drh75897232000-05-29 14:26:00 +0000775}
776
777/*
drh382c0242001-10-06 16:33:02 +0000778** This routine is called by the parser while in the middle of
779** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
780** been seen on a column. This routine sets the notNull flag on
781** the column currently under construction.
782*/
danielk19774adee202004-05-08 08:23:19 +0000783void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000784 Table *p;
785 int i;
786 if( (p = pParse->pNewTable)==0 ) return;
787 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000788 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000789}
790
791/*
792** This routine is called by the parser while in the middle of
793** parsing a CREATE TABLE statement. The pFirst token is the first
794** token in the sequence of tokens that describe the type of the
795** column currently under construction. pLast is the last token
796** in the sequence. Use this information to construct a string
797** that contains the typename of the column and store that string
798** in zType.
799*/
danielk19774adee202004-05-08 08:23:19 +0000800void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000801 Table *p;
802 int i, j;
803 int n;
804 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000805 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000806 if( (p = pParse->pNewTable)==0 ) return;
807 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000808 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000809 pCol = &p->aCol[i];
810 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000811 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000812 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000813 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000814 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000815 for(i=j=0; z[i]; i++){
816 int c = z[i];
817 if( isspace(c) ) continue;
818 z[j++] = c;
819 }
820 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000821 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000822}
823
824/*
drh7020f652000-06-03 18:06:52 +0000825** The given token is the default value for the last column added to
826** the table currently under construction. If "minusFlag" is true, it
827** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000828**
829** This routine is called by the parser while in the middle of
830** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000831*/
danielk19774adee202004-05-08 08:23:19 +0000832void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000833 Table *p;
834 int i;
835 char **pz;
836 if( (p = pParse->pNewTable)==0 ) return;
837 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000838 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000839 pz = &p->aCol[i].zDflt;
840 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000841 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000842 }else{
danielk19774adee202004-05-08 08:23:19 +0000843 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000844 }
danielk19774adee202004-05-08 08:23:19 +0000845 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000846}
847
848/*
drh4a324312001-12-21 14:30:42 +0000849** Designate the PRIMARY KEY for the table. pList is a list of names
850** of columns that form the primary key. If pList is NULL, then the
851** most recently added column of the table is the primary key.
852**
853** A table can have at most one primary key. If the table already has
854** a primary key (and this is the second primary key) then create an
855** error.
856**
857** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
858** then we will try to use that column as the row id. (Exception:
859** For backwards compatibility with older databases, do not do this
860** if the file format version number is less than 1.) Set the Table.iPKey
861** field of the table under construction to be the index of the
862** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
863** no INTEGER PRIMARY KEY.
864**
865** If the key is not an INTEGER PRIMARY KEY, then create a unique
866** index for the key. No index is created for INTEGER PRIMARY KEYs.
867*/
danielk19770202b292004-06-09 09:55:16 +0000868void sqlite3AddPrimaryKey(Parse *pParse, ExprList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000869 Table *pTab = pParse->pNewTable;
870 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000871 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000872 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000873 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000874 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000875 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000876 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000877 }
878 pTab->hasPrimKey = 1;
879 if( pList==0 ){
880 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000881 pTab->aCol[iCol].isPrimKey = 1;
882 }else{
danielk19770202b292004-06-09 09:55:16 +0000883 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000884 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000885 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
886 break;
887 }
drh78100cc2003-08-23 22:40:53 +0000888 }
889 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000890 }
danielk19770202b292004-06-09 09:55:16 +0000891 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000892 }
893 if( iCol>=0 && iCol<pTab->nCol ){
894 zType = pTab->aCol[iCol].zType;
895 }
danielk19773d68f032004-05-11 07:11:51 +0000896 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000897 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000898 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000899 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000900 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000901 pList = 0;
drh4a324312001-12-21 14:30:42 +0000902 }
drhe0194f22003-02-26 13:52:51 +0000903
904primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000905 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000906 return;
drh4a324312001-12-21 14:30:42 +0000907}
908
909/*
drhd3d39e92004-05-20 22:16:29 +0000910** Set the collation function of the most recently parsed table column
911** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000912*/
drhd3d39e92004-05-20 22:16:29 +0000913void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000914 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000915 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000916 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000917 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000918
drhd3d39e92004-05-20 22:16:29 +0000919 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000920 i = p->nCol-1;
921
922 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
923 p->aCol[i].pColl = pColl;
924
925 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
926 ** then an index may have been created on this column before the
927 ** collation type was added. Correct this if it is the case.
928 */
929 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
930 assert( pIdx->nColumn==1 );
931 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000932 }
933}
934
935/*
danielk19770202b292004-06-09 09:55:16 +0000936** Locate and return an entry from the db.aCollSeq hash table. If the entry
937** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +0000938** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +0000939**
danielk1977466be562004-06-10 02:16:01 +0000940** Each pointer stored in the sqlite3.aCollSeq hash table contains an
941** array of three CollSeq structures. The first is the collation sequence
942** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +0000943**
danielk1977466be562004-06-10 02:16:01 +0000944** Stored immediately after the three collation sequences is a copy of
945** the collation sequence name. A pointer to this string is stored in
946** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +0000947*/
danielk1977466be562004-06-10 02:16:01 +0000948static CollSeq * findCollSeqEntry(
949 sqlite *db,
950 const char *zName,
danielk19770202b292004-06-09 09:55:16 +0000951 int nName,
952 int create
drhd3d39e92004-05-20 22:16:29 +0000953){
954 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000955 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000956 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +0000957
danielk19770202b292004-06-09 09:55:16 +0000958 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +0000959 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +0000960 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000961 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000962 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +0000963 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000964 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +0000965 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000966 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +0000967 memcpy(pColl[0].zName, zName, nName);
968 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +0000969 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +0000970 }
drhd3d39e92004-05-20 22:16:29 +0000971 }
drhd3d39e92004-05-20 22:16:29 +0000972 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000973}
974
danielk1977466be562004-06-10 02:16:01 +0000975/*
976** Parameter zName points to a UTF-8 encoded string nName bytes long.
977** Return the CollSeq* pointer for the collation sequence named zName
978** for the encoding 'enc' from the database 'db'.
979**
980** If the entry specified is not found and 'create' is true, then create a
981** new entry. Otherwise return NULL.
982*/
983CollSeq *sqlite3FindCollSeq(
984 sqlite *db,
985 u8 enc,
986 const char *zName,
987 int nName,
988 int create
989){
990 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
drh6d08b4d2004-07-20 12:45:22 +0000991 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
992 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
993 if( pColl ) pColl += enc-1;
danielk1977466be562004-06-10 02:16:01 +0000994 return pColl;
995}
996
danielk1977e159fdf2004-06-21 10:45:06 +0000997/*
998** Invoke the 'collation needed' callback to request a collation sequence
999** in the database text encoding of name zName, length nName.
1000** If the collation sequence
1001*/
danielk19777cedc8d2004-06-10 10:50:08 +00001002static void callCollNeeded(sqlite *db, const char *zName, int nName){
danielk19777cedc8d2004-06-10 10:50:08 +00001003 assert( !db->xCollNeeded || !db->xCollNeeded16 );
1004 if( nName<0 ) nName = strlen(zName);
1005 if( db->xCollNeeded ){
danielk19778a6c5502004-06-22 12:18:32 +00001006 char *zExternal = sqliteStrNDup(zName, nName);
danielk19777cedc8d2004-06-10 10:50:08 +00001007 if( !zExternal ) return;
danielk1977e159fdf2004-06-21 10:45:06 +00001008 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
1009 sqliteFree(zExternal);
danielk19777cedc8d2004-06-10 10:50:08 +00001010 }
1011 if( db->xCollNeeded16 ){
danielk19778a6c5502004-06-22 12:18:32 +00001012 char const *zExternal;
danielk1977bfd6cce2004-06-18 04:24:54 +00001013 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
1014 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
1015 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +00001016 if( !zExternal ) return;
1017 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
1018 }
danielk19777cedc8d2004-06-10 10:50:08 +00001019}
1020
danielk1977e159fdf2004-06-21 10:45:06 +00001021/*
1022** This routine is called if the collation factory fails to deliver a
1023** collation function in the best encoding but there may be other versions
1024** of this collation function (for other text encodings) available. Use one
1025** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
1026** possible.
1027*/
danielk19777cedc8d2004-06-10 10:50:08 +00001028static int synthCollSeq(Parse *pParse, CollSeq *pColl){
drhda71ce12004-06-21 18:14:45 +00001029 CollSeq *pColl2;
danielk19777cedc8d2004-06-10 10:50:08 +00001030 char *z = pColl->zName;
1031 int n = strlen(z);
drhda71ce12004-06-21 18:14:45 +00001032 sqlite *db = pParse->db;
1033 int i;
1034 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
1035 for(i=0; i<3; i++){
1036 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
1037 if( pColl2->xCmp!=0 ){
1038 memcpy(pColl, pColl2, sizeof(CollSeq));
1039 return SQLITE_OK;
danielk19777cedc8d2004-06-10 10:50:08 +00001040 }
danielk19777cedc8d2004-06-10 10:50:08 +00001041 }
drhda71ce12004-06-21 18:14:45 +00001042 if( pParse->nErr==0 ){
1043 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ",
1044 -1, z, n, 0);
1045 }
1046 pParse->nErr++;
1047 return SQLITE_ERROR;
danielk19777cedc8d2004-06-10 10:50:08 +00001048}
1049
1050/*
1051** This routine is called on a collation sequence before it is used to
1052** check that it is defined. An undefined collation sequence exists when
1053** a database is loaded that contains references to collation sequences
1054** that have not been defined by sqlite3_create_collation() etc.
1055**
1056** If required, this routine calls the 'collation needed' callback to
1057** request a definition of the collating sequence. If this doesn't work,
1058** an equivalent collating sequence that uses a text encoding different
1059** from the main database is substituted, if one is available.
1060*/
1061int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1062 if( pColl && !pColl->xCmp ){
danielk1977e159fdf2004-06-21 10:45:06 +00001063 /* No collation sequence of this type for this encoding is registered.
1064 ** Call the collation factory to see if it can supply us with one.
1065 */
danielk19777cedc8d2004-06-10 10:50:08 +00001066 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1067 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1068 return SQLITE_ERROR;
1069 }
1070 }
1071 return SQLITE_OK;
1072}
1073
drhda71ce12004-06-21 18:14:45 +00001074/*
1075** Call sqlite3CheckCollSeq() for all collating sequences in an index,
1076** in order to verify that all the necessary collating sequences are
1077** loaded.
1078*/
danielk19777cedc8d2004-06-10 10:50:08 +00001079int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1080 if( pIdx ){
1081 int i;
1082 for(i=0; i<pIdx->nColumn; i++){
1083 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1084 return SQLITE_ERROR;
1085 }
1086 }
1087 }
1088 return SQLITE_OK;
1089}
1090
danielk1977466be562004-06-10 02:16:01 +00001091/*
1092** This function returns the collation sequence for database native text
1093** encoding identified by the string zName, length nName.
1094**
1095** If the requested collation sequence is not available, or not available
1096** in the database native encoding, the collation factory is invoked to
1097** request it. If the collation factory does not supply such a sequence,
1098** and the sequence is available in another text encoding, then that is
1099** returned instead.
1100**
1101** If no versions of the requested collations sequence are available, or
1102** another error occurs, NULL is returned and an error message written into
1103** pParse.
1104*/
danielk19770202b292004-06-09 09:55:16 +00001105CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001106 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001107 u8 initbusy = pParse->db->init.busy;
1108 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
1109 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001110 /* No collation sequence of this type for this encoding is registered.
1111 ** Call the collation factory to see if it can supply us with one.
1112 */
danielk19777cedc8d2004-06-10 10:50:08 +00001113 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001114 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001115 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001116 /* There may be a version of the collation sequence that requires
1117 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001118 */
danielk19777cedc8d2004-06-10 10:50:08 +00001119 if( synthCollSeq(pParse, pColl) ){
1120 return 0;
danielk1977466be562004-06-10 02:16:01 +00001121 }
1122 }
1123 }
1124
1125 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001126 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001127 if( pParse->nErr==0 ){
danielk1977466be562004-06-10 02:16:01 +00001128 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ", -1,
danielk19770202b292004-06-09 09:55:16 +00001129 zName, nName, 0);
1130 }
1131 pParse->nErr++;
danielk19777cedc8d2004-06-10 10:50:08 +00001132 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001133 }
1134 return pColl;
1135}
1136
1137
1138
danielk1977a37cdde2004-05-16 11:15:36 +00001139/*
drh1ad3b9e2004-05-20 12:10:20 +00001140** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +00001141** associated affinity type.
1142*/
1143char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +00001144 int n, i;
1145 struct {
drh1ad3b9e2004-05-20 12:10:20 +00001146 const char *zSub; /* Keywords substring to search for */
drhda71ce12004-06-21 18:14:45 +00001147 char nSub; /* length of zSub */
drh1ad3b9e2004-05-20 12:10:20 +00001148 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +00001149 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +00001150 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +00001151 {"CHAR", 4, SQLITE_AFF_TEXT},
1152 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +00001153 {"TEXT", 4, SQLITE_AFF_TEXT},
1154 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +00001155 };
1156
drh9c054832004-05-31 18:51:57 +00001157 if( nType==0 ){
1158 return SQLITE_AFF_NONE;
1159 }
drh1ad3b9e2004-05-20 12:10:20 +00001160 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
1161 int c1 = substrings[i].zSub[0];
1162 int c2 = tolower(c1);
1163 int limit = nType - substrings[i].nSub;
1164 const char *z = substrings[i].zSub;
1165 for(n=0; n<=limit; n++){
1166 int c = zType[n];
1167 if( (c==c1 || c==c2)
1168 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +00001169 return substrings[i].affinity;
1170 }
1171 }
1172 }
drh1ad3b9e2004-05-20 12:10:20 +00001173 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +00001174}
1175
1176/*
drh3f7d4e42004-07-24 14:35:58 +00001177** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001178**
1179** The schema cookie is used to determine when the schema for the
1180** database changes. After each schema change, the cookie value
1181** changes. When a process first reads the schema it records the
1182** cookie. Thereafter, whenever it goes to access the database,
1183** it checks the cookie to make sure the schema has not changed
1184** since it was last read.
1185**
1186** This plan is not completely bullet-proof. It is possible for
1187** the schema to change multiple times and for the cookie to be
1188** set back to prior value. But schema changes are infrequent
1189** and the probability of hitting the same cookie value is only
1190** 1 chance in 2^32. So we're safe enough.
1191*/
danielk1977cbb18d22004-05-28 11:37:27 +00001192void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){
drh3f7d4e42004-07-24 14:35:58 +00001193 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].schema_cookie+1, 0);
danielk19771d850a72004-05-31 08:26:49 +00001194 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001195}
1196
1197/*
drh969fa7c2002-02-18 18:30:32 +00001198** Measure the number of characters needed to output the given
1199** identifier. The number returned includes any quotes used
1200** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001201**
1202** The estimate is conservative. It might be larger that what is
1203** really needed.
drh969fa7c2002-02-18 18:30:32 +00001204*/
1205static int identLength(const char *z){
1206 int n;
drh17f71932002-02-21 12:01:27 +00001207 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001208 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001209 }
drh234c39d2004-07-24 03:30:47 +00001210 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001211}
1212
1213/*
1214** Write an identifier onto the end of the given string. Add
1215** quote characters as needed.
1216*/
drh4c755c02004-08-08 20:22:17 +00001217static void identPut(char *z, int *pIdx, char *zSignedIdent){
1218 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001219 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001220 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001221 for(j=0; zIdent[j]; j++){
1222 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1223 }
1224 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001225 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh234c39d2004-07-24 03:30:47 +00001226 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001227 for(j=0; zIdent[j]; j++){
1228 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001229 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001230 }
drh234c39d2004-07-24 03:30:47 +00001231 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001232 z[i] = 0;
1233 *pIdx = i;
1234}
1235
1236/*
1237** Generate a CREATE TABLE statement appropriate for the given
1238** table. Memory to hold the text of the statement is obtained
1239** from sqliteMalloc() and must be freed by the calling function.
1240*/
1241static char *createTableStmt(Table *p){
1242 int i, k, n;
1243 char *zStmt;
drh234c39d2004-07-24 03:30:47 +00001244 char *zSep, *zSep2, *zEnd, *z;
1245 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001246 n = 0;
drh234c39d2004-07-24 03:30:47 +00001247 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1248 n += identLength(pCol->zName);
1249 z = pCol->zType;
1250 if( z ){
1251 n += (strlen(z) + 1);
danielk1977517eb642004-06-07 10:00:31 +00001252 }
drh969fa7c2002-02-18 18:30:32 +00001253 }
1254 n += identLength(p->zName);
drh234c39d2004-07-24 03:30:47 +00001255 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001256 zSep = "";
1257 zSep2 = ",";
1258 zEnd = ")";
1259 }else{
1260 zSep = "\n ";
1261 zSep2 = ",\n ";
1262 zEnd = "\n)";
1263 }
drhe0bc4042002-06-25 01:09:11 +00001264 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001265 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001266 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001267 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001268 k = strlen(zStmt);
1269 identPut(zStmt, &k, p->zName);
1270 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001271 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drh969fa7c2002-02-18 18:30:32 +00001272 strcpy(&zStmt[k], zSep);
1273 k += strlen(&zStmt[k]);
1274 zSep = zSep2;
drh234c39d2004-07-24 03:30:47 +00001275 identPut(zStmt, &k, pCol->zName);
1276 if( (z = pCol->zType)!=0 ){
danielk1977517eb642004-06-07 10:00:31 +00001277 zStmt[k++] = ' ';
drh234c39d2004-07-24 03:30:47 +00001278 strcpy(&zStmt[k], z);
1279 k += strlen(z);
danielk1977517eb642004-06-07 10:00:31 +00001280 }
drh969fa7c2002-02-18 18:30:32 +00001281 }
1282 strcpy(&zStmt[k], zEnd);
1283 return zStmt;
1284}
1285
1286/*
drh75897232000-05-29 14:26:00 +00001287** This routine is called to report the final ")" that terminates
1288** a CREATE TABLE statement.
1289**
drhf57b3392001-10-08 13:22:32 +00001290** The table structure that other action routines have been building
1291** is added to the internal hash tables, assuming no errors have
1292** occurred.
drh75897232000-05-29 14:26:00 +00001293**
drh1d85d932004-02-14 23:05:52 +00001294** An entry for the table is made in the master table on disk, unless
1295** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001296** it means we are reading the sqlite_master table because we just
1297** connected to the database or because the sqlite_master table has
1298** recently changes, so the entry for this table already exists in
1299** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001300**
1301** If the pSelect argument is not NULL, it means that this routine
1302** was called to create a table generated from a
1303** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1304** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001305*/
danielk19774adee202004-05-08 08:23:19 +00001306void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001307 Table *p;
drhbe0072d2001-09-13 14:46:09 +00001308 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001309
danielk197724b03fd2004-05-10 10:34:34 +00001310 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001311 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001312 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001313
danielk1977517eb642004-06-07 10:00:31 +00001314 assert( !db->init.busy || !pSelect );
1315
drh1d85d932004-02-14 23:05:52 +00001316 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001317 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1318 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001319 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001320 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001321 */
drh1d85d932004-02-14 23:05:52 +00001322 if( db->init.busy ){
1323 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001324 }
1325
drhe3c41372001-09-17 20:25:58 +00001326 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001327 ** in the SQLITE_MASTER table of the database. The record number
1328 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001329 **
drhe0bc4042002-06-25 01:09:11 +00001330 ** If this is a TEMPORARY table, write the entry into the auxiliary
1331 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001332 */
drh1d85d932004-02-14 23:05:52 +00001333 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001334 int n;
drhd8bc7082000-06-07 23:51:50 +00001335 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001336
danielk19774adee202004-05-08 08:23:19 +00001337 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001338 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001339
drh4ff6dfa2002-03-03 23:06:00 +00001340 if( p->pSelect==0 ){
1341 /* A regular table */
drh234c39d2004-07-24 03:30:47 +00001342 sqlite3VdbeAddOp(v, OP_CreateTable, p->iDb, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001343 }else{
1344 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001345 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001346 }
danielk1977517eb642004-06-07 10:00:31 +00001347
1348 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1349
1350 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1351 ** statement to populate the new table. The root-page number for the
1352 ** new table is on the top of the vdbe stack.
1353 **
1354 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1355 ** suitable state to query for the column names and types to be used
1356 ** by the new table.
1357 */
1358 if( pSelect ){
1359 Table *pSelTab;
1360 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1361 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1362 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1363 pParse->nTab = 2;
1364 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1365 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1366 if( pParse->nErr==0 ){
1367 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1368 if( pSelTab==0 ) return;
1369 assert( p->aCol==0 );
1370 p->nCol = pSelTab->nCol;
1371 p->aCol = pSelTab->aCol;
1372 pSelTab->nCol = 0;
1373 pSelTab->aCol = 0;
1374 sqlite3DeleteTable(0, pSelTab);
1375 }
1376 }
1377
1378 sqlite3OpenMasterTable(v, p->iDb);
1379
1380 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view",P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001381 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1382 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk1977517eb642004-06-07 10:00:31 +00001383 sqlite3VdbeAddOp(v, OP_Pull, 3, 0);
1384
drhe0bc4042002-06-25 01:09:11 +00001385 if( pSelect ){
1386 char *z = createTableStmt(p);
1387 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001388 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001389 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001390 sqliteFree(z);
1391 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001392 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001393 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001394 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001395 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001396 }
drhe0bc4042002-06-25 01:09:11 +00001397 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001398 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001399 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001400 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
danielk197772c952a2004-06-21 09:06:41 +00001401 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drhe0bc4042002-06-25 01:09:11 +00001402 }
danielk197784ac9d02004-05-18 09:58:06 +00001403 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001404 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drhc275b4e2004-07-19 17:25:24 +00001405 sqlite3ChangeCookie(db, v, p->iDb);
danielk19774adee202004-05-08 08:23:19 +00001406 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh234c39d2004-07-24 03:30:47 +00001407 sqlite3VdbeOp3(v, OP_ParseSchema, p->iDb, 0,
1408 sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC);
danielk1977517eb642004-06-07 10:00:31 +00001409
danielk19774adee202004-05-08 08:23:19 +00001410 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001411 }
drh17e9e292003-02-01 13:53:28 +00001412
1413 /* Add the table to the in-memory representation of the database.
1414 */
drh234c39d2004-07-24 03:30:47 +00001415 if( db->init.busy && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001416 Table *pOld;
drhbe5c89a2004-07-26 00:31:09 +00001417 FKey *pFKey;
1418 Db *pDb = &db->aDb[p->iDb];
1419 pOld = sqlite3HashInsert(&pDb->tblHash, p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001420 if( pOld ){
1421 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1422 return;
1423 }
1424 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1425 int nTo = strlen(pFKey->zTo) + 1;
drhbe5c89a2004-07-26 00:31:09 +00001426 pFKey->pNextTo = sqlite3HashFind(&pDb->aFKey, pFKey->zTo, nTo);
1427 sqlite3HashInsert(&pDb->aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001428 }
1429 pParse->pNewTable = 0;
1430 db->nTable++;
1431 db->flags |= SQLITE_InternChanges;
1432 }
drh75897232000-05-29 14:26:00 +00001433}
1434
1435/*
drha76b5df2002-02-23 02:32:10 +00001436** The parser calls this routine in order to create a new VIEW
1437*/
danielk19774adee202004-05-08 08:23:19 +00001438void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001439 Parse *pParse, /* The parsing context */
1440 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001441 Token *pName1, /* The token that holds the name of the view */
1442 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001443 Select *pSelect, /* A SELECT statement that will become the new view */
1444 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001445){
drha76b5df2002-02-23 02:32:10 +00001446 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001447 int n;
drh4c755c02004-08-08 20:22:17 +00001448 const unsigned char *z;
drh4b59ab52002-08-24 18:24:51 +00001449 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001450 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001451 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001452
danielk197748dec7e2004-05-28 12:33:30 +00001453 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001454 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001455 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001456 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001457 return;
1458 }
danielk1977ef2cb632004-05-29 02:37:19 +00001459 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001460 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1461 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001462 ){
danielk19774adee202004-05-08 08:23:19 +00001463 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001464 return;
1465 }
drh174b6192002-12-03 02:22:52 +00001466
drh4b59ab52002-08-24 18:24:51 +00001467 /* Make a copy of the entire SELECT statement that defines the view.
1468 ** This will force all the Expr.token.z values to be dynamically
1469 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001470 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001471 */
danielk19774adee202004-05-08 08:23:19 +00001472 p->pSelect = sqlite3SelectDup(pSelect);
1473 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001474 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001475 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001476 }
drh4b59ab52002-08-24 18:24:51 +00001477
1478 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1479 ** the end.
1480 */
drha76b5df2002-02-23 02:32:10 +00001481 sEnd = pParse->sLastToken;
1482 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1483 sEnd.z += sEnd.n;
1484 }
1485 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001486 n = sEnd.z - pBegin->z;
drh4c755c02004-08-08 20:22:17 +00001487 z = (const unsigned char*)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001488 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1489 sEnd.z = &z[n-1];
1490 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001491
danielk19774adee202004-05-08 08:23:19 +00001492 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1493 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001494 return;
drh417be792002-03-03 18:59:40 +00001495}
drha76b5df2002-02-23 02:32:10 +00001496
drh417be792002-03-03 18:59:40 +00001497/*
1498** The Table structure pTable is really a VIEW. Fill in the names of
1499** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001500** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001501*/
danielk19774adee202004-05-08 08:23:19 +00001502int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001503 ExprList *pEList;
1504 Select *pSel;
1505 Table *pSelTab;
1506 int nErr = 0;
1507
1508 assert( pTable );
1509
1510 /* A positive nCol means the columns names for this view are
1511 ** already known.
1512 */
1513 if( pTable->nCol>0 ) return 0;
1514
1515 /* A negative nCol is a special marker meaning that we are currently
1516 ** trying to compute the column names. If we enter this routine with
1517 ** a negative nCol, it means two or more views form a loop, like this:
1518 **
1519 ** CREATE VIEW one AS SELECT * FROM two;
1520 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001521 **
1522 ** Actually, this error is caught previously and so the following test
1523 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001524 */
1525 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001526 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001527 return 1;
1528 }
1529
1530 /* If we get this far, it means we need to compute the table names.
1531 */
1532 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1533 pSel = pTable->pSelect;
1534
danielk19774adee202004-05-08 08:23:19 +00001535 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001536 ** "*" elements in this list. But we will need to restore the list
1537 ** back to its original configuration afterwards, so we save a copy of
1538 ** the original in pEList.
1539 */
1540 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001541 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001542 if( pSel->pEList==0 ){
1543 pSel->pEList = pEList;
1544 return 1; /* Malloc failed */
1545 }
1546 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001547 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001548 if( pSelTab ){
1549 assert( pTable->aCol==0 );
1550 pTable->nCol = pSelTab->nCol;
1551 pTable->aCol = pSelTab->aCol;
1552 pSelTab->nCol = 0;
1553 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001554 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001555 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001556 }else{
1557 pTable->nCol = 0;
1558 nErr++;
1559 }
danielk19774adee202004-05-08 08:23:19 +00001560 sqlite3SelectUnbind(pSel);
1561 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001562 pSel->pEList = pEList;
1563 return nErr;
1564}
1565
1566/*
drh8bf8dc92003-05-17 17:35:10 +00001567** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001568*/
drhd24cc422003-03-27 12:51:24 +00001569static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001570 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001571 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001572 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001573 Table *pTab = sqliteHashData(i);
1574 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001575 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001576 }
1577 }
drh8bf8dc92003-05-17 17:35:10 +00001578 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001579}
1580
drh75897232000-05-29 14:26:00 +00001581/*
1582** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001583** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001584*/
danielk1977a8858102004-05-28 12:11:21 +00001585void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1586 Table *pTab;
drh75897232000-05-29 14:26:00 +00001587 Vdbe *v;
1588 int base;
drh5edc3122001-09-13 21:53:09 +00001589 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001590 int iDb;
drh75897232000-05-29 14:26:00 +00001591
danielk1977a8858102004-05-28 12:11:21 +00001592 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1593 assert( pName->nSrc==1 );
1594 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1595
1596 if( pTab==0 ) goto exit_drop_table;
1597 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001598 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001599#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001600 {
1601 int code;
danielk1977a8858102004-05-28 12:11:21 +00001602 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1603 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001604 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001605 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001606 }
drhe5f9c642003-01-13 23:27:31 +00001607 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001608 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001609 code = SQLITE_DROP_TEMP_VIEW;
1610 }else{
1611 code = SQLITE_DROP_VIEW;
1612 }
1613 }else{
drhd24cc422003-03-27 12:51:24 +00001614 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001615 code = SQLITE_DROP_TEMP_TABLE;
1616 }else{
1617 code = SQLITE_DROP_TABLE;
1618 }
1619 }
danielk1977a8858102004-05-28 12:11:21 +00001620 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1621 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001622 }
danielk1977a8858102004-05-28 12:11:21 +00001623 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1624 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001625 }
drhe5f9c642003-01-13 23:27:31 +00001626 }
1627#endif
danielk1977a8858102004-05-28 12:11:21 +00001628 if( pTab->readOnly ){
1629 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001630 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001631 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001632 }
danielk1977a8858102004-05-28 12:11:21 +00001633 if( isView && pTab->pSelect==0 ){
1634 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1635 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001636 }
danielk1977a8858102004-05-28 12:11:21 +00001637 if( !isView && pTab->pSelect ){
1638 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1639 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001640 }
drh75897232000-05-29 14:26:00 +00001641
drh1ccde152000-06-17 13:12:39 +00001642 /* Generate code to remove the table from the master table
1643 ** on disk.
1644 */
danielk19774adee202004-05-08 08:23:19 +00001645 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001646 if( v ){
drh905793e2004-02-21 13:31:09 +00001647 static VdbeOpList dropTable[] = {
danielk19778e227872004-06-07 07:52:17 +00001648 { OP_Rewind, 0, ADDR(13), 0},
1649 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001650 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001651 { OP_MemLoad, 1, 0, 0}, /* 3 */
danielk19778e227872004-06-07 07:52:17 +00001652 { OP_Column, 0, 2, 0}, /* sqlite_master.tbl_name */
1653 { OP_Ne, 0, ADDR(12), 0},
1654 { OP_String8, 0, 0, "trigger"},
1655 { OP_Column, 0, 2, 0}, /* sqlite_master.type */
1656 { OP_Eq, 0, ADDR(12), 0},
drh75897232000-05-29 14:26:00 +00001657 { OP_Delete, 0, 0, 0},
danielk19778e227872004-06-07 07:52:17 +00001658 { OP_Rewind, 0, ADDR(13), 0},
danielk19778d059842004-05-12 11:24:02 +00001659 { OP_Goto, 0, ADDR(3), 0},
danielk19778e227872004-06-07 07:52:17 +00001660 { OP_Next, 0, ADDR(3), 0}, /* 12 */
drh75897232000-05-29 14:26:00 +00001661 };
1662 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001663 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001664 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001665
danielk19778e227872004-06-07 07:52:17 +00001666 /* Drop all triggers associated with the table being dropped. Code
1667 ** is generated to remove entries from sqlite_master and/or
1668 ** sqlite_temp_master if required.
1669 */
danielk1977a8858102004-05-28 12:11:21 +00001670 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001671 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001672 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001673 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drh956bc922004-07-24 17:38:29 +00001674 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00001675 }
drh8bf8dc92003-05-17 17:35:10 +00001676
danielk19778e227872004-06-07 07:52:17 +00001677 /* Drop all SQLITE_MASTER table and index entries that refer to the
1678 ** table. The program name loops through the master table and deletes
1679 ** every row that refers to a table of the same name as the one being
1680 ** dropped. Triggers are handled seperately because a trigger can be
1681 ** created in the temp database that refers to a table in another
1682 ** database.
1683 */
danielk1977a8858102004-05-28 12:11:21 +00001684 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001685 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001686 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
danielk19778e227872004-06-07 07:52:17 +00001687 sqlite3ChangeCookie(db, v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001688 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001689 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001690 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1691 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001692 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001693 }
drh5e00f6c2001-09-13 13:46:56 +00001694 }
drh956bc922004-07-24 17:38:29 +00001695 sqlite3VdbeOp3(v, OP_DropTable, pTab->iDb, 0, pTab->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00001696 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001697 }
drhd24cc422003-03-27 12:51:24 +00001698 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001699
1700exit_drop_table:
1701 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001702}
1703
1704/*
drhc2eef3b2002-08-31 18:53:06 +00001705** This routine is called to create a new foreign key on the table
1706** currently under construction. pFromCol determines which columns
1707** in the current table point to the foreign key. If pFromCol==0 then
1708** connect the key to the last column inserted. pTo is the name of
1709** the table referred to. pToCol is a list of tables in the other
1710** pTo table that the foreign key points to. flags contains all
1711** information about the conflict resolution algorithms specified
1712** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1713**
1714** An FKey structure is created and added to the table currently
1715** under construction in the pParse->pNewTable field. The new FKey
1716** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001717** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001718**
1719** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001720** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001721*/
danielk19774adee202004-05-08 08:23:19 +00001722void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001723 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001724 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001725 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001726 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001727 int flags /* Conflict resolution algorithms. */
1728){
1729 Table *p = pParse->pNewTable;
1730 int nByte;
1731 int i;
1732 int nCol;
1733 char *z;
1734 FKey *pFKey = 0;
1735
1736 assert( pTo!=0 );
1737 if( p==0 || pParse->nErr ) goto fk_end;
1738 if( pFromCol==0 ){
1739 int iCol = p->nCol-1;
1740 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001741 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001742 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001743 " should reference only one column of table %T",
1744 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001745 goto fk_end;
1746 }
1747 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001748 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001749 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001750 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001751 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001752 goto fk_end;
1753 }else{
danielk19770202b292004-06-09 09:55:16 +00001754 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001755 }
1756 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1757 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001758 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001759 nByte += strlen(pToCol->a[i].zName) + 1;
1760 }
1761 }
1762 pFKey = sqliteMalloc( nByte );
1763 if( pFKey==0 ) goto fk_end;
1764 pFKey->pFrom = p;
1765 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001766 z = (char*)&pFKey[1];
1767 pFKey->aCol = (struct sColMap*)z;
1768 z += sizeof(struct sColMap)*nCol;
1769 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001770 memcpy(z, pTo->z, pTo->n);
1771 z[pTo->n] = 0;
1772 z += pTo->n+1;
1773 pFKey->pNextTo = 0;
1774 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001775 if( pFromCol==0 ){
1776 pFKey->aCol[0].iFrom = p->nCol-1;
1777 }else{
1778 for(i=0; i<nCol; i++){
1779 int j;
1780 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001781 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001782 pFKey->aCol[i].iFrom = j;
1783 break;
1784 }
1785 }
1786 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001787 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001788 "unknown column \"%s\" in foreign key definition",
1789 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001790 goto fk_end;
1791 }
1792 }
1793 }
1794 if( pToCol ){
1795 for(i=0; i<nCol; i++){
1796 int n = strlen(pToCol->a[i].zName);
1797 pFKey->aCol[i].zCol = z;
1798 memcpy(z, pToCol->a[i].zName, n);
1799 z[n] = 0;
1800 z += n+1;
1801 }
1802 }
1803 pFKey->isDeferred = 0;
1804 pFKey->deleteConf = flags & 0xff;
1805 pFKey->updateConf = (flags >> 8 ) & 0xff;
1806 pFKey->insertConf = (flags >> 16 ) & 0xff;
1807
1808 /* Link the foreign key to the table as the last step.
1809 */
1810 p->pFKey = pFKey;
1811 pFKey = 0;
1812
1813fk_end:
1814 sqliteFree(pFKey);
danielk19770202b292004-06-09 09:55:16 +00001815 sqlite3ExprListDelete(pFromCol);
1816 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001817}
1818
1819/*
1820** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1821** clause is seen as part of a foreign key definition. The isDeferred
1822** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1823** The behavior of the most recently created foreign key is adjusted
1824** accordingly.
1825*/
danielk19774adee202004-05-08 08:23:19 +00001826void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001827 Table *pTab;
1828 FKey *pFKey;
1829 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1830 pFKey->isDeferred = isDeferred;
1831}
1832
1833/*
drh75897232000-05-29 14:26:00 +00001834** Create a new index for an SQL table. pIndex is the name of the index
1835** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001836** be NULL for a primary key or an index that is created to satisfy a
1837** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001838** as the table to be indexed. pParse->pNewTable is a table that is
1839** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001840**
drh382c0242001-10-06 16:33:02 +00001841** pList is a list of columns to be indexed. pList will be NULL if this
1842** is a primary key or unique-constraint on the most recent column added
1843** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001844*/
danielk19774adee202004-05-08 08:23:19 +00001845void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001846 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001847 Token *pName1, /* First part of index name. May be NULL */
1848 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00001849 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
1850 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001851 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001852 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1853 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1854){
danielk1977cbb18d22004-05-28 11:37:27 +00001855 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00001856 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00001857 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001858 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001859 Token nullId; /* Fake token for an empty ID list */
1860 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001861 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001862 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001863
danielk1977cbb18d22004-05-28 11:37:27 +00001864 int iDb; /* Index of the database that is being written */
1865 Token *pName = 0; /* Unqualified name of the index to create */
1866
danielk197724b03fd2004-05-10 10:34:34 +00001867 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001868
drh75897232000-05-29 14:26:00 +00001869 /*
1870 ** Find the table that is to be indexed. Return early if not found.
1871 */
danielk1977cbb18d22004-05-28 11:37:27 +00001872 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001873
1874 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001875 ** to search for the table. 'Fix' the table name to this db
1876 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001877 */
1878 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001879 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001880 if( iDb<0 ) goto exit_create_index;
1881
danielk1977ef2cb632004-05-29 02:37:19 +00001882 /* If the index name was unqualified, check if the the table
1883 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001884 */
danielk1977ef2cb632004-05-29 02:37:19 +00001885 pTab = sqlite3SrcListLookup(pParse, pTblName);
1886 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1887 iDb = 1;
1888 }
1889
1890 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1891 sqlite3FixSrcList(&sFix, pTblName)
1892 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001893 goto exit_create_index;
1894 }
danielk1977ef2cb632004-05-29 02:37:19 +00001895 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1896 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001897 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001898 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001899 }else{
drhe3c41372001-09-17 20:25:58 +00001900 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001901 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001902 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001903 }
danielk1977cbb18d22004-05-28 11:37:27 +00001904
drh75897232000-05-29 14:26:00 +00001905 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001906 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001907 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001908 goto exit_create_index;
1909 }
drha76b5df2002-02-23 02:32:10 +00001910 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001911 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001912 goto exit_create_index;
1913 }
drh4925ca02003-11-27 00:48:57 +00001914 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001915
1916 /*
1917 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001918 ** index or table with the same name.
1919 **
1920 ** Exception: If we are reading the names of permanent indices from the
1921 ** sqlite_master table (because some other process changed the schema) and
1922 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001923 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001924 **
1925 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001926 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1927 ** own name.
drh75897232000-05-29 14:26:00 +00001928 */
danielk1977d8123362004-06-12 09:25:12 +00001929 if( pName ){
drha99db3b2004-06-19 14:49:12 +00001930 zName = sqlite3NameFromToken(pName);
danielk19778a414492004-06-29 08:59:35 +00001931 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001932 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001933 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00001934 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001935 }
danielk1977d8123362004-06-12 09:25:12 +00001936 if( !db->init.busy ){
1937 Index *pISameName; /* Another index with the same name */
1938 Table *pTSameName; /* A table with same name as the index */
danielk19778a414492004-06-29 08:59:35 +00001939 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001940 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
1941 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
1942 goto exit_create_index;
1943 }
1944 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1945 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
1946 goto exit_create_index;
1947 }
drhe3c41372001-09-17 20:25:58 +00001948 }
drhd24cc422003-03-27 12:51:24 +00001949 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001950 char zBuf[30];
1951 int n;
1952 Index *pLoop;
1953 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00001954 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00001955 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00001956 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001957 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00001958 }
1959
drhe5f9c642003-01-13 23:27:31 +00001960 /* Check for authorization to create an index.
1961 */
1962#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001963 {
1964 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001965 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001966 goto exit_create_index;
1967 }
1968 i = SQLITE_CREATE_INDEX;
1969 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001970 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001971 goto exit_create_index;
1972 }
drhe5f9c642003-01-13 23:27:31 +00001973 }
1974#endif
1975
drh75897232000-05-29 14:26:00 +00001976 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001977 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001978 ** So create a fake list to simulate this.
1979 */
1980 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001981 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001982 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00001983 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00001984 if( pList==0 ) goto exit_create_index;
1985 }
1986
1987 /*
1988 ** Allocate the index structure.
1989 */
drhdcc581c2000-05-30 13:44:19 +00001990 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00001991 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00001992 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00001993 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
1994 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00001995 strcpy(pIndex->zName, zName);
1996 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00001997 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00001998 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001999 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00002000 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00002001
drh1ccde152000-06-17 13:12:39 +00002002 /* Scan the names of the columns of the table to be indexed and
2003 ** load the column indices into the Index structure. Report an error
2004 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00002005 */
danielk19770202b292004-06-09 09:55:16 +00002006 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00002007 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002008 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002009 }
2010 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002011 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00002012 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002013 goto exit_create_index;
2014 }
drh967e8b72000-06-21 13:59:10 +00002015 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00002016 if( pList->a[i].pExpr ){
2017 assert( pList->a[i].pExpr->pColl );
2018 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
2019 }else{
2020 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
2021 }
2022 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00002023 if( !db->init.busy &&
2024 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2025 ){
2026 goto exit_create_index;
2027 }
drh75897232000-05-29 14:26:00 +00002028 }
danielk19770202b292004-06-09 09:55:16 +00002029 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002030
danielk1977d8123362004-06-12 09:25:12 +00002031 if( pTab==pParse->pNewTable ){
2032 /* This routine has been called to create an automatic index as a
2033 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2034 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2035 ** i.e. one of:
2036 **
2037 ** CREATE TABLE t(x PRIMARY KEY, y);
2038 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2039 **
2040 ** Either way, check to see if the table already has such an index. If
2041 ** so, don't bother creating this one. This only applies to
2042 ** automatically created indices. Users can do as they wish with
2043 ** explicit indices.
2044 */
2045 Index *pIdx;
2046 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2047 int k;
2048 assert( pIdx->onError!=OE_None );
2049 assert( pIdx->autoIndex );
2050 assert( pIndex->onError!=OE_None );
2051
2052 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2053 for(k=0; k<pIdx->nColumn; k++){
2054 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2055 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2056 }
2057 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002058 if( pIdx->onError!=pIndex->onError ){
2059 /* This constraint creates the same index as a previous
2060 ** constraint specified somewhere in the CREATE TABLE statement.
2061 ** However the ON CONFLICT clauses are different. If both this
2062 ** constraint and the previous equivalent constraint have explicit
2063 ** ON CONFLICT clauses this is an error. Otherwise, use the
2064 ** explicitly specified behaviour for the index.
2065 */
2066 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2067 sqlite3ErrorMsg(pParse,
2068 "conflicting ON CONFLICT clauses specified", 0);
2069 }
2070 if( pIdx->onError==OE_Default ){
2071 pIdx->onError = pIndex->onError;
2072 }
2073 }
danielk1977d8123362004-06-12 09:25:12 +00002074 goto exit_create_index;
2075 }
2076 }
2077 }
2078
drh75897232000-05-29 14:26:00 +00002079 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002080 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002081 */
drh234c39d2004-07-24 03:30:47 +00002082 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002083 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002084 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002085 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002086 if( p ){
2087 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002088 goto exit_create_index;
2089 }
drh5e00f6c2001-09-13 13:46:56 +00002090 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002091 if( pTblName!=0 ){
2092 pIndex->tnum = db->init.newTnum;
2093 }
drhd78eeee2001-09-13 16:18:53 +00002094 }
2095
drh1d85d932004-02-14 23:05:52 +00002096 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002097 ** involves writing the index into the master table and filling in the
2098 ** index with the current table contents.
2099 **
drh1d85d932004-02-14 23:05:52 +00002100 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2101 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002102 ** CREATE INDEX statements are read out of the master table. In
2103 ** the latter case the index already exists on disk, which is why
2104 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002105 **
danielk1977cbb18d22004-05-28 11:37:27 +00002106 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002107 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2108 ** has just been created, it contains no data and the index initialization
2109 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002110 */
drh1d85d932004-02-14 23:05:52 +00002111 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00002112 int n;
drhadbca9c2001-09-27 15:11:53 +00002113 Vdbe *v;
drh75897232000-05-29 14:26:00 +00002114 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00002115
danielk19774adee202004-05-08 08:23:19 +00002116 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002117 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00002118 if( pTblName!=0 ){
2119 sqlite3BeginWriteOperation(pParse, 0, iDb);
2120 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00002121 }
danielk19774adee202004-05-08 08:23:19 +00002122 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00002123 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
2124 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
2125 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
drh234c39d2004-07-24 03:30:47 +00002126 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002127 if( pTblName ){
drha99db3b2004-06-19 14:49:12 +00002128 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2129 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002130 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
2131 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00002132 }
danielk19770f69c1e2004-05-29 11:24:50 +00002133 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00002134 if( pStart && pEnd ){
danielk19770202b292004-06-09 09:55:16 +00002135 if( onError==OE_None ){
2136 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
2137 }else{
2138 sqlite3VdbeChangeP3(v, -1, "CREATE UNIQUE INDEX ", P3_STATIC);
2139 }
danielk19770f69c1e2004-05-29 11:24:50 +00002140 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002141 n = Addr(pEnd->z) - Addr(pName->z) + 1;
2142 sqlite3VdbeChangeP3(v, -1, pName->z, n);
danielk197772c952a2004-06-21 09:06:41 +00002143 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drh75897232000-05-29 14:26:00 +00002144 }
danielk197784ac9d02004-05-18 09:58:06 +00002145 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002146 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002147 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00002148 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002149 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
2150 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00002151 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00002152 lbl2 = sqlite3VdbeMakeLabel(v);
2153 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00002154 lbl1 = sqlite3VdbeCurrentAddr(v);
2155 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00002156 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00002157 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002158 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
2159 sqlite3VdbeResolveLabel(v, lbl2);
2160 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
2161 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drhc275b4e2004-07-19 17:25:24 +00002162 sqlite3ChangeCookie(db, v, iDb);
danielk19774adee202004-05-08 08:23:19 +00002163 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2164 sqlite3EndWriteOperation(pParse);
drh234c39d2004-07-24 03:30:47 +00002165 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
2166 sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC);
drh5e00f6c2001-09-13 13:46:56 +00002167 }
drh75897232000-05-29 14:26:00 +00002168 }
2169
danielk1977d8123362004-06-12 09:25:12 +00002170 /* When adding an index to the list of indices for a table, make
2171 ** sure all indices labeled OE_Replace come after all those labeled
2172 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2173 ** and INSERT.
2174 */
drh234c39d2004-07-24 03:30:47 +00002175 if( db->init.busy || pTblName==0 ){
2176 if( onError!=OE_Replace || pTab->pIndex==0
2177 || pTab->pIndex->onError==OE_Replace){
2178 pIndex->pNext = pTab->pIndex;
2179 pTab->pIndex = pIndex;
2180 }else{
2181 Index *pOther = pTab->pIndex;
2182 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2183 pOther = pOther->pNext;
2184 }
2185 pIndex->pNext = pOther->pNext;
2186 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002187 }
drh234c39d2004-07-24 03:30:47 +00002188 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002189 }
danielk1977d8123362004-06-12 09:25:12 +00002190
drh75897232000-05-29 14:26:00 +00002191 /* Clean up before exiting */
2192exit_create_index:
drh956bc922004-07-24 17:38:29 +00002193 if( pIndex ){
2194 freeIndex(pIndex);
2195 }
danielk19770202b292004-06-09 09:55:16 +00002196 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002197 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002198 sqliteFree(zName);
2199 return;
2200}
2201
2202/*
drh74e24cd2002-01-09 03:19:59 +00002203** This routine will drop an existing named index. This routine
2204** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002205*/
danielk19774adee202004-05-08 08:23:19 +00002206void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002207 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002208 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00002209 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002210
danielk197724b03fd2004-05-10 10:34:34 +00002211 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002212 assert( pName->nSrc==1 );
danielk19778a414492004-06-29 08:59:35 +00002213 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19774adee202004-05-08 08:23:19 +00002214 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002215 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002216 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002217 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002218 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002219 }
drh485b39b2002-07-13 03:11:52 +00002220 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002221 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002222 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002223 goto exit_drop_index;
2224 }
drhe5f9c642003-01-13 23:27:31 +00002225#ifndef SQLITE_OMIT_AUTHORIZATION
2226 {
2227 int code = SQLITE_DROP_INDEX;
2228 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002229 const char *zDb = db->aDb[pIndex->iDb].zName;
2230 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002231 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002232 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002233 }
drhd24cc422003-03-27 12:51:24 +00002234 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002235 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002236 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002237 }
drhed6c8672003-01-12 18:02:16 +00002238 }
drhe5f9c642003-01-13 23:27:31 +00002239#endif
drh75897232000-05-29 14:26:00 +00002240
2241 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002242 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002243 if( v ){
drh905793e2004-02-21 13:31:09 +00002244 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00002245 { OP_Rewind, 0, ADDR(9), 0},
drh234c39d2004-07-24 03:30:47 +00002246 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00002247 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002248 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00002249 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002250 { OP_Eq, 0, ADDR(8), 0},
2251 { OP_Next, 0, ADDR(3), 0},
2252 { OP_Goto, 0, ADDR(9), 0},
2253 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00002254 };
2255 int base;
2256
danielk19774adee202004-05-08 08:23:19 +00002257 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
2258 sqlite3OpenMasterTable(v, pIndex->iDb);
2259 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
2260 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
drhc275b4e2004-07-19 17:25:24 +00002261 sqlite3ChangeCookie(db, v, pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002262 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2263 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh956bc922004-07-24 17:38:29 +00002264 sqlite3VdbeOp3(v, OP_DropIndex, pIndex->iDb, 0, pIndex->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00002265 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00002266 }
2267
drhd24cc422003-03-27 12:51:24 +00002268exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002269 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002270}
2271
2272/*
drh75897232000-05-29 14:26:00 +00002273** Append a new element to the given IdList. Create a new IdList if
2274** need be.
drhdaffd0e2001-04-11 14:28:42 +00002275**
2276** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002277*/
danielk19774adee202004-05-08 08:23:19 +00002278IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002279 if( pList==0 ){
2280 pList = sqliteMalloc( sizeof(IdList) );
2281 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002282 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002283 }
drh4305d102003-07-30 12:34:12 +00002284 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002285 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002286 pList->nAlloc = pList->nAlloc*2 + 5;
2287 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002288 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002289 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002290 return 0;
drh75897232000-05-29 14:26:00 +00002291 }
drh6d4abfb2001-10-22 02:58:08 +00002292 pList->a = a;
drh75897232000-05-29 14:26:00 +00002293 }
2294 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
drha99db3b2004-06-19 14:49:12 +00002295 pList->a[pList->nId].zName = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002296 pList->nId++;
2297 return pList;
2298}
2299
2300/*
drhad3cab52002-05-24 02:04:32 +00002301** Append a new table name to the given SrcList. Create a new SrcList if
2302** need be. A new entry is created in the SrcList even if pToken is NULL.
2303**
2304** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002305**
2306** If pDatabase is not null, it means that the table has an optional
2307** database name prefix. Like this: "database.table". The pDatabase
2308** points to the table name and the pTable points to the database name.
2309** The SrcList.a[].zName field is filled with the table name which might
2310** come from pTable (if pDatabase is NULL) or from pDatabase.
2311** SrcList.a[].zDatabase is filled with the database name from pTable,
2312** or with NULL if no database is specified.
2313**
2314** In other words, if call like this:
2315**
danielk19774adee202004-05-08 08:23:19 +00002316** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002317**
2318** Then B is a table name and the database name is unspecified. If called
2319** like this:
2320**
danielk19774adee202004-05-08 08:23:19 +00002321** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002322**
2323** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002324*/
danielk19774adee202004-05-08 08:23:19 +00002325SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drha99db3b2004-06-19 14:49:12 +00002326 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002327 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002328 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002329 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002330 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002331 }
drh4305d102003-07-30 12:34:12 +00002332 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002333 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002334 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002335 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002336 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002337 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002338 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002339 return 0;
2340 }
drh113088e2003-03-20 01:16:58 +00002341 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002342 }
drha99db3b2004-06-19 14:49:12 +00002343 pItem = &pList->a[pList->nSrc];
2344 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002345 if( pDatabase && pDatabase->z==0 ){
2346 pDatabase = 0;
2347 }
2348 if( pDatabase && pTable ){
2349 Token *pTemp = pDatabase;
2350 pDatabase = pTable;
2351 pTable = pTemp;
2352 }
drha99db3b2004-06-19 14:49:12 +00002353 pItem->zName = sqlite3NameFromToken(pTable);
2354 pItem->zDatabase = sqlite3NameFromToken(pDatabase);
2355 pItem->iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002356 pList->nSrc++;
2357 return pList;
2358}
2359
2360/*
drh63eb5f22003-04-29 16:20:44 +00002361** Assign cursors to all tables in a SrcList
2362*/
danielk19774adee202004-05-08 08:23:19 +00002363void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002364 int i;
2365 for(i=0; i<pList->nSrc; i++){
2366 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002367 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002368 }
2369 }
2370}
2371
2372/*
drh75897232000-05-29 14:26:00 +00002373** Add an alias to the last identifier on the given identifier list.
2374*/
danielk19774adee202004-05-08 08:23:19 +00002375void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002376 if( pList && pList->nSrc>0 ){
drha99db3b2004-06-19 14:49:12 +00002377 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002378 }
2379}
2380
2381/*
drhad3cab52002-05-24 02:04:32 +00002382** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002383*/
danielk19774adee202004-05-08 08:23:19 +00002384void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002385 int i;
2386 if( pList==0 ) return;
2387 for(i=0; i<pList->nId; i++){
2388 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002389 }
2390 sqliteFree(pList->a);
2391 sqliteFree(pList);
2392}
2393
2394/*
drhad2d8302002-05-24 20:31:36 +00002395** Return the index in pList of the identifier named zId. Return -1
2396** if not found.
2397*/
danielk19774adee202004-05-08 08:23:19 +00002398int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002399 int i;
2400 if( pList==0 ) return -1;
2401 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002402 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002403 }
2404 return -1;
2405}
2406
2407/*
drhad3cab52002-05-24 02:04:32 +00002408** Delete an entire SrcList including all its substructure.
2409*/
danielk19774adee202004-05-08 08:23:19 +00002410void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002411 int i;
drhbe5c89a2004-07-26 00:31:09 +00002412 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002413 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00002414 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
2415 sqliteFree(pItem->zDatabase);
2416 sqliteFree(pItem->zName);
2417 sqliteFree(pItem->zAlias);
2418 if( pItem->pTab && pItem->pTab->isTransient ){
2419 sqlite3DeleteTable(0, pItem->pTab);
drhdaffd0e2001-04-11 14:28:42 +00002420 }
drhbe5c89a2004-07-26 00:31:09 +00002421 sqlite3SelectDelete(pItem->pSelect);
2422 sqlite3ExprDelete(pItem->pOn);
2423 sqlite3IdListDelete(pItem->pUsing);
drh75897232000-05-29 14:26:00 +00002424 }
drh75897232000-05-29 14:26:00 +00002425 sqliteFree(pList);
2426}
2427
drh982cef72000-05-30 16:27:03 +00002428/*
drhc4a3c772001-04-04 11:48:57 +00002429** Begin a transaction
2430*/
danielk197733752f82004-05-31 08:55:33 +00002431void sqlite3BeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002432 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002433 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002434
drh001bbcb2003-03-19 03:14:00 +00002435 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002436 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002437 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002438
2439 v = sqlite3GetVdbe(pParse);
2440 if( !v ) return;
2441 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002442}
2443
2444/*
2445** Commit a transaction
2446*/
danielk19774adee202004-05-08 08:23:19 +00002447void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002448 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002449 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002450
drh001bbcb2003-03-19 03:14:00 +00002451 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002452 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002453 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002454
2455 v = sqlite3GetVdbe(pParse);
2456 if( v ){
2457 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002458 }
drhc4a3c772001-04-04 11:48:57 +00002459}
2460
2461/*
2462** Rollback a transaction
2463*/
danielk19774adee202004-05-08 08:23:19 +00002464void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002465 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002466 Vdbe *v;
2467
drh001bbcb2003-03-19 03:14:00 +00002468 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002469 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002470 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002471
danielk19774adee202004-05-08 08:23:19 +00002472 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002473 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002474 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002475 }
drhc4a3c772001-04-04 11:48:57 +00002476}
drhf57b14a2001-09-14 18:54:08 +00002477
2478/*
drh80242052004-06-09 00:48:12 +00002479** Generate VDBE code that will verify the schema cookie and start
2480** a read-transaction for all named database files.
2481**
2482** It is important that all schema cookies be verified and all
2483** read transactions be started before anything else happens in
2484** the VDBE program. But this routine can be called after much other
2485** code has been generated. So here is what we do:
2486**
drhc275b4e2004-07-19 17:25:24 +00002487** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00002488** will jump to a subroutine at the end of the program. Then we
2489** record every database that needs its schema verified in the
2490** pParse->cookieMask field. Later, after all other code has been
2491** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00002492** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00002493** will be made to point to that subroutine. The generation of the
2494** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00002495**
2496** If iDb<0 then code the OP_Goto only - don't set flag to verify the
2497** schema on any databases. This can be used to position the OP_Goto
2498** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00002499*/
danielk19774adee202004-05-08 08:23:19 +00002500void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh80242052004-06-09 00:48:12 +00002501 sqlite *db;
2502 Vdbe *v;
2503 int mask;
2504
2505 v = sqlite3GetVdbe(pParse);
2506 if( v==0 ) return; /* This only happens if there was a prior error */
2507 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00002508 if( pParse->cookieGoto==0 ){
2509 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00002510 }
drhc275b4e2004-07-19 17:25:24 +00002511 if( iDb>=0 ){
2512 assert( iDb<db->nDb );
2513 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2514 assert( iDb<32 );
2515 mask = 1<<iDb;
2516 if( (pParse->cookieMask & mask)==0 ){
2517 pParse->cookieMask |= mask;
2518 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
2519 }
drh001bbcb2003-03-19 03:14:00 +00002520 }
drh001bbcb2003-03-19 03:14:00 +00002521}
2522
2523/*
drh1c928532002-01-31 15:54:21 +00002524** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002525** might change the database.
2526**
2527** This routine starts a new transaction if we are not already within
2528** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002529** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002530** be set for operations that might fail (due to a constraint) part of
2531** the way through and which will need to undo some writes without having to
2532** rollback the whole transaction. For operations where all constraints
2533** can be checked before any changes are made to the database, it is never
2534** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002535**
drh8bf8dc92003-05-17 17:35:10 +00002536** Only database iDb and the temp database are made writable by this call.
2537** If iDb==0, then the main and temp databases are made writable. If
2538** iDb==1 then only the temp database is made writable. If iDb>1 then the
2539** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002540*/
drh7f0f12e2004-05-21 13:39:50 +00002541void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002542 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002543 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002544 sqlite3CodeVerifySchema(pParse, iDb);
2545 pParse->writeMask |= 1<<iDb;
danielk19771d850a72004-05-31 08:26:49 +00002546 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002547 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002548 }
2549 if( iDb!=1 ){
2550 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002551 }
2552}
2553
2554/*
drh1c928532002-01-31 15:54:21 +00002555** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002556** the database. If a statement transaction was started, then emit
2557** an OP_Commit that will cause the changes to be committed to disk.
2558**
2559** Note that checkpoints are automatically committed at the end of
2560** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002561** sqlite3BeginWriteOperation() but there should only be a single
2562** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002563*/
danielk19774adee202004-05-08 08:23:19 +00002564void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002565 /* Delete me! */
2566 return;
drh1c928532002-01-31 15:54:21 +00002567}
danielk1977bfd6cce2004-06-18 04:24:54 +00002568
2569/*
2570** Return the transient sqlite3_value object used for encoding conversions
2571** during SQL compilation.
2572*/
2573sqlite3_value *sqlite3GetTransientValue(sqlite *db){
2574 if( !db->pValue ){
2575 db->pValue = sqlite3ValueNew();
2576 }
2577 return db->pValue;
2578}