blob: b351ac1489055be022ce87b572d6728d03ea4c7a [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**
drhdc3ff9c2004-08-18 02:10:15 +000026** $Id: build.c,v 1.247 2004/08/18 02:10:15 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
drhf57b3392001-10-08 13:22:32 +0000650 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000651 ** index or table name in the same database. Issue an error message if
652 ** it does.
drhf57b3392001-10-08 13:22:32 +0000653 */
danielk19778a414492004-06-29 08:59:35 +0000654 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19773df6b252004-05-29 10:23:19 +0000655 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
656 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000657 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000658 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000659 return;
drh75897232000-05-29 14:26:00 +0000660 }
danielk19778a414492004-06-29 08:59:35 +0000661 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
662 ( iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000663 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000664 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000665 return;
666 }
667 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000668 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000669 pParse->rc = SQLITE_NOMEM;
670 pParse->nErr++;
drh6d4abfb2001-10-22 02:58:08 +0000671 sqliteFree(zName);
672 return;
673 }
drh75897232000-05-29 14:26:00 +0000674 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000675 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000676 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000677 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000678 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000679 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000680 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000681 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000682
683 /* Begin generating the code that will insert the table record into
684 ** the SQLITE_MASTER table. Note in particular that we must go ahead
685 ** and allocate the record number for the table entry now. Before any
686 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
687 ** indices to be created and the table record must come before the
688 ** indices. Hence, the record number for the table must be allocated
689 ** now.
690 */
danielk19774adee202004-05-08 08:23:19 +0000691 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000692 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977d008cfe2004-06-19 02:22:10 +0000693 /* Every time a new table is created the file-format
694 ** and encoding meta-values are set in the database, in
695 ** case this is the first table created.
696 */
697 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
698 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
699 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
700 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
701
danielk1977cbb18d22004-05-28 11:37:27 +0000702 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000703 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
704 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000705 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000706 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000707 }
drh75897232000-05-29 14:26:00 +0000708}
709
710/*
711** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000712**
713** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000714** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000715** first to get things going. Then this routine is called for each
716** column.
drh75897232000-05-29 14:26:00 +0000717*/
danielk19774adee202004-05-08 08:23:19 +0000718void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000719 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000720 int i;
drha99db3b2004-06-19 14:49:12 +0000721 char *z;
drhc9b84a12002-06-20 11:36:48 +0000722 Column *pCol;
drh75897232000-05-29 14:26:00 +0000723 if( (p = pParse->pNewTable)==0 ) return;
drha99db3b2004-06-19 14:49:12 +0000724 z = sqlite3NameFromToken(pName);
drh97fc3d02002-05-22 21:27:03 +0000725 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000726 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000727 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
728 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000729 sqliteFree(z);
730 return;
731 }
732 }
drh75897232000-05-29 14:26:00 +0000733 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000734 Column *aNew;
735 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
736 if( aNew==0 ) return;
737 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000738 }
drhc9b84a12002-06-20 11:36:48 +0000739 pCol = &p->aCol[p->nCol];
740 memset(pCol, 0, sizeof(p->aCol[0]));
741 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000742
743 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000744 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
745 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000746 */
danielk19774f057f92004-06-08 00:02:33 +0000747 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000748 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000749 p->nCol++;
drh75897232000-05-29 14:26:00 +0000750}
751
752/*
drh382c0242001-10-06 16:33:02 +0000753** This routine is called by the parser while in the middle of
754** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
755** been seen on a column. This routine sets the notNull flag on
756** the column currently under construction.
757*/
danielk19774adee202004-05-08 08:23:19 +0000758void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000759 Table *p;
760 int i;
761 if( (p = pParse->pNewTable)==0 ) return;
762 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000763 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000764}
765
766/*
767** This routine is called by the parser while in the middle of
768** parsing a CREATE TABLE statement. The pFirst token is the first
769** token in the sequence of tokens that describe the type of the
770** column currently under construction. pLast is the last token
771** in the sequence. Use this information to construct a string
772** that contains the typename of the column and store that string
773** in zType.
774*/
danielk19774adee202004-05-08 08:23:19 +0000775void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000776 Table *p;
777 int i, j;
778 int n;
779 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000780 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000781 if( (p = pParse->pNewTable)==0 ) return;
782 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000783 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000784 pCol = &p->aCol[i];
785 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000786 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000787 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000788 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000789 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000790 for(i=j=0; z[i]; i++){
791 int c = z[i];
792 if( isspace(c) ) continue;
793 z[j++] = c;
794 }
795 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000796 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000797}
798
799/*
drh7020f652000-06-03 18:06:52 +0000800** The given token is the default value for the last column added to
801** the table currently under construction. If "minusFlag" is true, it
802** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000803**
804** This routine is called by the parser while in the middle of
805** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000806*/
danielk19774adee202004-05-08 08:23:19 +0000807void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000808 Table *p;
809 int i;
810 char **pz;
811 if( (p = pParse->pNewTable)==0 ) return;
812 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000813 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000814 pz = &p->aCol[i].zDflt;
815 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000816 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000817 }else{
danielk19774adee202004-05-08 08:23:19 +0000818 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000819 }
danielk19774adee202004-05-08 08:23:19 +0000820 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000821}
822
823/*
drh4a324312001-12-21 14:30:42 +0000824** Designate the PRIMARY KEY for the table. pList is a list of names
825** of columns that form the primary key. If pList is NULL, then the
826** most recently added column of the table is the primary key.
827**
828** A table can have at most one primary key. If the table already has
829** a primary key (and this is the second primary key) then create an
830** error.
831**
832** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
833** then we will try to use that column as the row id. (Exception:
834** For backwards compatibility with older databases, do not do this
835** if the file format version number is less than 1.) Set the Table.iPKey
836** field of the table under construction to be the index of the
837** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
838** no INTEGER PRIMARY KEY.
839**
840** If the key is not an INTEGER PRIMARY KEY, then create a unique
841** index for the key. No index is created for INTEGER PRIMARY KEYs.
842*/
danielk19770202b292004-06-09 09:55:16 +0000843void sqlite3AddPrimaryKey(Parse *pParse, ExprList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000844 Table *pTab = pParse->pNewTable;
845 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000846 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000847 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000848 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000850 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000851 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000852 }
853 pTab->hasPrimKey = 1;
854 if( pList==0 ){
855 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000856 pTab->aCol[iCol].isPrimKey = 1;
857 }else{
danielk19770202b292004-06-09 09:55:16 +0000858 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000859 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000860 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
861 break;
862 }
drh78100cc2003-08-23 22:40:53 +0000863 }
864 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000865 }
danielk19770202b292004-06-09 09:55:16 +0000866 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000867 }
868 if( iCol>=0 && iCol<pTab->nCol ){
869 zType = pTab->aCol[iCol].zType;
870 }
danielk19773d68f032004-05-11 07:11:51 +0000871 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000872 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000873 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000874 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000875 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000876 pList = 0;
drh4a324312001-12-21 14:30:42 +0000877 }
drhe0194f22003-02-26 13:52:51 +0000878
879primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000880 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000881 return;
drh4a324312001-12-21 14:30:42 +0000882}
883
884/*
drhd3d39e92004-05-20 22:16:29 +0000885** Set the collation function of the most recently parsed table column
886** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000887*/
drhd3d39e92004-05-20 22:16:29 +0000888void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000889 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000890 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000891 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000892 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000893
drhd3d39e92004-05-20 22:16:29 +0000894 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000895 i = p->nCol-1;
896
897 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
898 p->aCol[i].pColl = pColl;
899
900 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
901 ** then an index may have been created on this column before the
902 ** collation type was added. Correct this if it is the case.
903 */
904 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
905 assert( pIdx->nColumn==1 );
906 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000907 }
908}
909
910/*
danielk19770202b292004-06-09 09:55:16 +0000911** Locate and return an entry from the db.aCollSeq hash table. If the entry
912** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +0000913** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +0000914**
danielk1977466be562004-06-10 02:16:01 +0000915** Each pointer stored in the sqlite3.aCollSeq hash table contains an
916** array of three CollSeq structures. The first is the collation sequence
917** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +0000918**
danielk1977466be562004-06-10 02:16:01 +0000919** Stored immediately after the three collation sequences is a copy of
920** the collation sequence name. A pointer to this string is stored in
921** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +0000922*/
danielk1977466be562004-06-10 02:16:01 +0000923static CollSeq * findCollSeqEntry(
924 sqlite *db,
925 const char *zName,
danielk19770202b292004-06-09 09:55:16 +0000926 int nName,
927 int create
drhd3d39e92004-05-20 22:16:29 +0000928){
929 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000930 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000931 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +0000932
danielk19770202b292004-06-09 09:55:16 +0000933 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +0000934 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +0000935 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000936 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000937 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +0000938 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000939 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +0000940 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000941 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +0000942 memcpy(pColl[0].zName, zName, nName);
943 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +0000944 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +0000945 }
drhd3d39e92004-05-20 22:16:29 +0000946 }
drhd3d39e92004-05-20 22:16:29 +0000947 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000948}
949
danielk1977466be562004-06-10 02:16:01 +0000950/*
951** Parameter zName points to a UTF-8 encoded string nName bytes long.
952** Return the CollSeq* pointer for the collation sequence named zName
953** for the encoding 'enc' from the database 'db'.
954**
955** If the entry specified is not found and 'create' is true, then create a
956** new entry. Otherwise return NULL.
957*/
958CollSeq *sqlite3FindCollSeq(
959 sqlite *db,
960 u8 enc,
961 const char *zName,
962 int nName,
963 int create
964){
965 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
drh6d08b4d2004-07-20 12:45:22 +0000966 assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
967 assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
968 if( pColl ) pColl += enc-1;
danielk1977466be562004-06-10 02:16:01 +0000969 return pColl;
970}
971
danielk1977e159fdf2004-06-21 10:45:06 +0000972/*
973** Invoke the 'collation needed' callback to request a collation sequence
974** in the database text encoding of name zName, length nName.
975** If the collation sequence
976*/
danielk19777cedc8d2004-06-10 10:50:08 +0000977static void callCollNeeded(sqlite *db, const char *zName, int nName){
danielk19777cedc8d2004-06-10 10:50:08 +0000978 assert( !db->xCollNeeded || !db->xCollNeeded16 );
979 if( nName<0 ) nName = strlen(zName);
980 if( db->xCollNeeded ){
danielk19778a6c5502004-06-22 12:18:32 +0000981 char *zExternal = sqliteStrNDup(zName, nName);
danielk19777cedc8d2004-06-10 10:50:08 +0000982 if( !zExternal ) return;
danielk1977e159fdf2004-06-21 10:45:06 +0000983 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
984 sqliteFree(zExternal);
danielk19777cedc8d2004-06-10 10:50:08 +0000985 }
986 if( db->xCollNeeded16 ){
danielk19778a6c5502004-06-22 12:18:32 +0000987 char const *zExternal;
danielk1977bfd6cce2004-06-18 04:24:54 +0000988 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
989 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
990 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +0000991 if( !zExternal ) return;
992 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
993 }
danielk19777cedc8d2004-06-10 10:50:08 +0000994}
995
danielk1977e159fdf2004-06-21 10:45:06 +0000996/*
997** This routine is called if the collation factory fails to deliver a
998** collation function in the best encoding but there may be other versions
999** of this collation function (for other text encodings) available. Use one
1000** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
1001** possible.
1002*/
danielk19777cedc8d2004-06-10 10:50:08 +00001003static int synthCollSeq(Parse *pParse, CollSeq *pColl){
drhda71ce12004-06-21 18:14:45 +00001004 CollSeq *pColl2;
danielk19777cedc8d2004-06-10 10:50:08 +00001005 char *z = pColl->zName;
1006 int n = strlen(z);
drhda71ce12004-06-21 18:14:45 +00001007 sqlite *db = pParse->db;
1008 int i;
1009 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
1010 for(i=0; i<3; i++){
1011 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
1012 if( pColl2->xCmp!=0 ){
1013 memcpy(pColl, pColl2, sizeof(CollSeq));
1014 return SQLITE_OK;
danielk19777cedc8d2004-06-10 10:50:08 +00001015 }
danielk19777cedc8d2004-06-10 10:50:08 +00001016 }
drhda71ce12004-06-21 18:14:45 +00001017 if( pParse->nErr==0 ){
1018 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ",
1019 -1, z, n, 0);
1020 }
1021 pParse->nErr++;
1022 return SQLITE_ERROR;
danielk19777cedc8d2004-06-10 10:50:08 +00001023}
1024
1025/*
1026** This routine is called on a collation sequence before it is used to
1027** check that it is defined. An undefined collation sequence exists when
1028** a database is loaded that contains references to collation sequences
1029** that have not been defined by sqlite3_create_collation() etc.
1030**
1031** If required, this routine calls the 'collation needed' callback to
1032** request a definition of the collating sequence. If this doesn't work,
1033** an equivalent collating sequence that uses a text encoding different
1034** from the main database is substituted, if one is available.
1035*/
1036int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1037 if( pColl && !pColl->xCmp ){
danielk1977e159fdf2004-06-21 10:45:06 +00001038 /* No collation sequence of this type for this encoding is registered.
1039 ** Call the collation factory to see if it can supply us with one.
1040 */
danielk19777cedc8d2004-06-10 10:50:08 +00001041 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1042 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1043 return SQLITE_ERROR;
1044 }
1045 }
1046 return SQLITE_OK;
1047}
1048
drhda71ce12004-06-21 18:14:45 +00001049/*
1050** Call sqlite3CheckCollSeq() for all collating sequences in an index,
1051** in order to verify that all the necessary collating sequences are
1052** loaded.
1053*/
danielk19777cedc8d2004-06-10 10:50:08 +00001054int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1055 if( pIdx ){
1056 int i;
1057 for(i=0; i<pIdx->nColumn; i++){
1058 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1059 return SQLITE_ERROR;
1060 }
1061 }
1062 }
1063 return SQLITE_OK;
1064}
1065
danielk1977466be562004-06-10 02:16:01 +00001066/*
1067** This function returns the collation sequence for database native text
1068** encoding identified by the string zName, length nName.
1069**
1070** If the requested collation sequence is not available, or not available
1071** in the database native encoding, the collation factory is invoked to
1072** request it. If the collation factory does not supply such a sequence,
1073** and the sequence is available in another text encoding, then that is
1074** returned instead.
1075**
1076** If no versions of the requested collations sequence are available, or
1077** another error occurs, NULL is returned and an error message written into
1078** pParse.
1079*/
danielk19770202b292004-06-09 09:55:16 +00001080CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001081 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001082 u8 initbusy = pParse->db->init.busy;
1083 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
1084 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001085 /* No collation sequence of this type for this encoding is registered.
1086 ** Call the collation factory to see if it can supply us with one.
1087 */
danielk19777cedc8d2004-06-10 10:50:08 +00001088 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001089 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001090 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001091 /* There may be a version of the collation sequence that requires
1092 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001093 */
danielk19777cedc8d2004-06-10 10:50:08 +00001094 if( synthCollSeq(pParse, pColl) ){
1095 return 0;
danielk1977466be562004-06-10 02:16:01 +00001096 }
1097 }
1098 }
1099
1100 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001101 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001102 if( pParse->nErr==0 ){
danielk1977466be562004-06-10 02:16:01 +00001103 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ", -1,
danielk19770202b292004-06-09 09:55:16 +00001104 zName, nName, 0);
1105 }
1106 pParse->nErr++;
danielk19777cedc8d2004-06-10 10:50:08 +00001107 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001108 }
1109 return pColl;
1110}
1111
1112
1113
danielk1977a37cdde2004-05-16 11:15:36 +00001114/*
drh1ad3b9e2004-05-20 12:10:20 +00001115** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +00001116** associated affinity type.
1117*/
1118char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +00001119 int n, i;
1120 struct {
drh1ad3b9e2004-05-20 12:10:20 +00001121 const char *zSub; /* Keywords substring to search for */
drhda71ce12004-06-21 18:14:45 +00001122 char nSub; /* length of zSub */
drh1ad3b9e2004-05-20 12:10:20 +00001123 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +00001124 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +00001125 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +00001126 {"CHAR", 4, SQLITE_AFF_TEXT},
1127 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +00001128 {"TEXT", 4, SQLITE_AFF_TEXT},
1129 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +00001130 };
1131
drh9c054832004-05-31 18:51:57 +00001132 if( nType==0 ){
1133 return SQLITE_AFF_NONE;
1134 }
drh1ad3b9e2004-05-20 12:10:20 +00001135 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
1136 int c1 = substrings[i].zSub[0];
1137 int c2 = tolower(c1);
1138 int limit = nType - substrings[i].nSub;
1139 const char *z = substrings[i].zSub;
1140 for(n=0; n<=limit; n++){
1141 int c = zType[n];
1142 if( (c==c1 || c==c2)
1143 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +00001144 return substrings[i].affinity;
1145 }
1146 }
1147 }
drh1ad3b9e2004-05-20 12:10:20 +00001148 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +00001149}
1150
1151/*
drh3f7d4e42004-07-24 14:35:58 +00001152** Generate code that will increment the schema cookie.
drh50e5dad2001-09-15 00:57:28 +00001153**
1154** The schema cookie is used to determine when the schema for the
1155** database changes. After each schema change, the cookie value
1156** changes. When a process first reads the schema it records the
1157** cookie. Thereafter, whenever it goes to access the database,
1158** it checks the cookie to make sure the schema has not changed
1159** since it was last read.
1160**
1161** This plan is not completely bullet-proof. It is possible for
1162** the schema to change multiple times and for the cookie to be
1163** set back to prior value. But schema changes are infrequent
1164** and the probability of hitting the same cookie value is only
1165** 1 chance in 2^32. So we're safe enough.
1166*/
danielk1977cbb18d22004-05-28 11:37:27 +00001167void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){
drh3f7d4e42004-07-24 14:35:58 +00001168 sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].schema_cookie+1, 0);
danielk19771d850a72004-05-31 08:26:49 +00001169 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001170}
1171
1172/*
drh969fa7c2002-02-18 18:30:32 +00001173** Measure the number of characters needed to output the given
1174** identifier. The number returned includes any quotes used
1175** but does not include the null terminator.
drh234c39d2004-07-24 03:30:47 +00001176**
1177** The estimate is conservative. It might be larger that what is
1178** really needed.
drh969fa7c2002-02-18 18:30:32 +00001179*/
1180static int identLength(const char *z){
1181 int n;
drh17f71932002-02-21 12:01:27 +00001182 for(n=0; *z; n++, z++){
drh234c39d2004-07-24 03:30:47 +00001183 if( *z=='"' ){ n++; }
drh969fa7c2002-02-18 18:30:32 +00001184 }
drh234c39d2004-07-24 03:30:47 +00001185 return n + 2;
drh969fa7c2002-02-18 18:30:32 +00001186}
1187
1188/*
1189** Write an identifier onto the end of the given string. Add
1190** quote characters as needed.
1191*/
drh4c755c02004-08-08 20:22:17 +00001192static void identPut(char *z, int *pIdx, char *zSignedIdent){
1193 unsigned char *zIdent = (unsigned char*)zSignedIdent;
drh17f71932002-02-21 12:01:27 +00001194 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001195 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001196 for(j=0; zIdent[j]; j++){
1197 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1198 }
1199 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001200 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh234c39d2004-07-24 03:30:47 +00001201 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001202 for(j=0; zIdent[j]; j++){
1203 z[i++] = zIdent[j];
drh234c39d2004-07-24 03:30:47 +00001204 if( zIdent[j]=='"' ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001205 }
drh234c39d2004-07-24 03:30:47 +00001206 if( needQuote ) z[i++] = '"';
drh969fa7c2002-02-18 18:30:32 +00001207 z[i] = 0;
1208 *pIdx = i;
1209}
1210
1211/*
1212** Generate a CREATE TABLE statement appropriate for the given
1213** table. Memory to hold the text of the statement is obtained
1214** from sqliteMalloc() and must be freed by the calling function.
1215*/
1216static char *createTableStmt(Table *p){
1217 int i, k, n;
1218 char *zStmt;
drh234c39d2004-07-24 03:30:47 +00001219 char *zSep, *zSep2, *zEnd, *z;
1220 Column *pCol;
drh969fa7c2002-02-18 18:30:32 +00001221 n = 0;
drh234c39d2004-07-24 03:30:47 +00001222 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
1223 n += identLength(pCol->zName);
1224 z = pCol->zType;
1225 if( z ){
1226 n += (strlen(z) + 1);
danielk1977517eb642004-06-07 10:00:31 +00001227 }
drh969fa7c2002-02-18 18:30:32 +00001228 }
1229 n += identLength(p->zName);
drh234c39d2004-07-24 03:30:47 +00001230 if( n<50 ){
drh969fa7c2002-02-18 18:30:32 +00001231 zSep = "";
1232 zSep2 = ",";
1233 zEnd = ")";
1234 }else{
1235 zSep = "\n ";
1236 zSep2 = ",\n ";
1237 zEnd = "\n)";
1238 }
drhe0bc4042002-06-25 01:09:11 +00001239 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001240 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001241 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001242 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001243 k = strlen(zStmt);
1244 identPut(zStmt, &k, p->zName);
1245 zStmt[k++] = '(';
drh234c39d2004-07-24 03:30:47 +00001246 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
drh969fa7c2002-02-18 18:30:32 +00001247 strcpy(&zStmt[k], zSep);
1248 k += strlen(&zStmt[k]);
1249 zSep = zSep2;
drh234c39d2004-07-24 03:30:47 +00001250 identPut(zStmt, &k, pCol->zName);
1251 if( (z = pCol->zType)!=0 ){
danielk1977517eb642004-06-07 10:00:31 +00001252 zStmt[k++] = ' ';
drh234c39d2004-07-24 03:30:47 +00001253 strcpy(&zStmt[k], z);
1254 k += strlen(z);
danielk1977517eb642004-06-07 10:00:31 +00001255 }
drh969fa7c2002-02-18 18:30:32 +00001256 }
1257 strcpy(&zStmt[k], zEnd);
1258 return zStmt;
1259}
1260
1261/*
drh75897232000-05-29 14:26:00 +00001262** This routine is called to report the final ")" that terminates
1263** a CREATE TABLE statement.
1264**
drhf57b3392001-10-08 13:22:32 +00001265** The table structure that other action routines have been building
1266** is added to the internal hash tables, assuming no errors have
1267** occurred.
drh75897232000-05-29 14:26:00 +00001268**
drh1d85d932004-02-14 23:05:52 +00001269** An entry for the table is made in the master table on disk, unless
1270** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001271** it means we are reading the sqlite_master table because we just
1272** connected to the database or because the sqlite_master table has
1273** recently changes, so the entry for this table already exists in
1274** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001275**
1276** If the pSelect argument is not NULL, it means that this routine
1277** was called to create a table generated from a
1278** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1279** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001280*/
danielk19774adee202004-05-08 08:23:19 +00001281void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001282 Table *p;
drhbe0072d2001-09-13 14:46:09 +00001283 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001284
danielk197724b03fd2004-05-10 10:34:34 +00001285 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001286 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001287 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001288
danielk1977517eb642004-06-07 10:00:31 +00001289 assert( !db->init.busy || !pSelect );
1290
drh1d85d932004-02-14 23:05:52 +00001291 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001292 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1293 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001294 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001295 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001296 */
drh1d85d932004-02-14 23:05:52 +00001297 if( db->init.busy ){
1298 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001299 }
1300
drhe3c41372001-09-17 20:25:58 +00001301 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001302 ** in the SQLITE_MASTER table of the database. The record number
1303 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001304 **
drhe0bc4042002-06-25 01:09:11 +00001305 ** If this is a TEMPORARY table, write the entry into the auxiliary
1306 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001307 */
drh1d85d932004-02-14 23:05:52 +00001308 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001309 int n;
drhd8bc7082000-06-07 23:51:50 +00001310 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001311
danielk19774adee202004-05-08 08:23:19 +00001312 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001313 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001314
drh4ff6dfa2002-03-03 23:06:00 +00001315 if( p->pSelect==0 ){
1316 /* A regular table */
drh234c39d2004-07-24 03:30:47 +00001317 sqlite3VdbeAddOp(v, OP_CreateTable, p->iDb, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001318 }else{
1319 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001320 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001321 }
danielk1977517eb642004-06-07 10:00:31 +00001322
1323 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1324
1325 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1326 ** statement to populate the new table. The root-page number for the
1327 ** new table is on the top of the vdbe stack.
1328 **
1329 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1330 ** suitable state to query for the column names and types to be used
1331 ** by the new table.
1332 */
1333 if( pSelect ){
1334 Table *pSelTab;
1335 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1336 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1337 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1338 pParse->nTab = 2;
1339 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1340 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1341 if( pParse->nErr==0 ){
1342 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1343 if( pSelTab==0 ) return;
1344 assert( p->aCol==0 );
1345 p->nCol = pSelTab->nCol;
1346 p->aCol = pSelTab->aCol;
1347 pSelTab->nCol = 0;
1348 pSelTab->aCol = 0;
1349 sqlite3DeleteTable(0, pSelTab);
1350 }
1351 }
1352
1353 sqlite3OpenMasterTable(v, p->iDb);
1354
1355 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view",P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001356 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1357 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk1977517eb642004-06-07 10:00:31 +00001358 sqlite3VdbeAddOp(v, OP_Pull, 3, 0);
1359
drhe0bc4042002-06-25 01:09:11 +00001360 if( pSelect ){
1361 char *z = createTableStmt(p);
1362 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001363 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001364 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001365 sqliteFree(z);
1366 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001367 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001368 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001369 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001370 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001371 }
drhe0bc4042002-06-25 01:09:11 +00001372 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001373 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001374 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001375 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
danielk197772c952a2004-06-21 09:06:41 +00001376 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drhe0bc4042002-06-25 01:09:11 +00001377 }
danielk197784ac9d02004-05-18 09:58:06 +00001378 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001379 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drhc275b4e2004-07-19 17:25:24 +00001380 sqlite3ChangeCookie(db, v, p->iDb);
danielk19774adee202004-05-08 08:23:19 +00001381 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh234c39d2004-07-24 03:30:47 +00001382 sqlite3VdbeOp3(v, OP_ParseSchema, p->iDb, 0,
1383 sqlite3MPrintf("tbl_name='%q'",p->zName), P3_DYNAMIC);
danielk1977517eb642004-06-07 10:00:31 +00001384
danielk19774adee202004-05-08 08:23:19 +00001385 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001386 }
drh17e9e292003-02-01 13:53:28 +00001387
1388 /* Add the table to the in-memory representation of the database.
1389 */
drh234c39d2004-07-24 03:30:47 +00001390 if( db->init.busy && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001391 Table *pOld;
drhbe5c89a2004-07-26 00:31:09 +00001392 FKey *pFKey;
1393 Db *pDb = &db->aDb[p->iDb];
1394 pOld = sqlite3HashInsert(&pDb->tblHash, p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001395 if( pOld ){
1396 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1397 return;
1398 }
1399 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1400 int nTo = strlen(pFKey->zTo) + 1;
drhbe5c89a2004-07-26 00:31:09 +00001401 pFKey->pNextTo = sqlite3HashFind(&pDb->aFKey, pFKey->zTo, nTo);
1402 sqlite3HashInsert(&pDb->aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001403 }
1404 pParse->pNewTable = 0;
1405 db->nTable++;
1406 db->flags |= SQLITE_InternChanges;
1407 }
drh75897232000-05-29 14:26:00 +00001408}
1409
1410/*
drha76b5df2002-02-23 02:32:10 +00001411** The parser calls this routine in order to create a new VIEW
1412*/
danielk19774adee202004-05-08 08:23:19 +00001413void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001414 Parse *pParse, /* The parsing context */
1415 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001416 Token *pName1, /* The token that holds the name of the view */
1417 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001418 Select *pSelect, /* A SELECT statement that will become the new view */
1419 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001420){
drha76b5df2002-02-23 02:32:10 +00001421 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001422 int n;
drh4c755c02004-08-08 20:22:17 +00001423 const unsigned char *z;
drh4b59ab52002-08-24 18:24:51 +00001424 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001425 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001426 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001427
danielk197748dec7e2004-05-28 12:33:30 +00001428 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001429 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001430 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001431 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001432 return;
1433 }
danielk1977ef2cb632004-05-29 02:37:19 +00001434 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001435 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1436 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001437 ){
danielk19774adee202004-05-08 08:23:19 +00001438 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001439 return;
1440 }
drh174b6192002-12-03 02:22:52 +00001441
drh4b59ab52002-08-24 18:24:51 +00001442 /* Make a copy of the entire SELECT statement that defines the view.
1443 ** This will force all the Expr.token.z values to be dynamically
1444 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001445 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001446 */
danielk19774adee202004-05-08 08:23:19 +00001447 p->pSelect = sqlite3SelectDup(pSelect);
1448 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001449 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001450 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001451 }
drh4b59ab52002-08-24 18:24:51 +00001452
1453 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1454 ** the end.
1455 */
drha76b5df2002-02-23 02:32:10 +00001456 sEnd = pParse->sLastToken;
1457 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1458 sEnd.z += sEnd.n;
1459 }
1460 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001461 n = sEnd.z - pBegin->z;
drh4c755c02004-08-08 20:22:17 +00001462 z = (const unsigned char*)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001463 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1464 sEnd.z = &z[n-1];
1465 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001466
danielk19774adee202004-05-08 08:23:19 +00001467 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1468 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001469 return;
drh417be792002-03-03 18:59:40 +00001470}
drha76b5df2002-02-23 02:32:10 +00001471
drh417be792002-03-03 18:59:40 +00001472/*
1473** The Table structure pTable is really a VIEW. Fill in the names of
1474** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001475** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001476*/
danielk19774adee202004-05-08 08:23:19 +00001477int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001478 ExprList *pEList;
1479 Select *pSel;
1480 Table *pSelTab;
1481 int nErr = 0;
1482
1483 assert( pTable );
1484
1485 /* A positive nCol means the columns names for this view are
1486 ** already known.
1487 */
1488 if( pTable->nCol>0 ) return 0;
1489
1490 /* A negative nCol is a special marker meaning that we are currently
1491 ** trying to compute the column names. If we enter this routine with
1492 ** a negative nCol, it means two or more views form a loop, like this:
1493 **
1494 ** CREATE VIEW one AS SELECT * FROM two;
1495 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001496 **
1497 ** Actually, this error is caught previously and so the following test
1498 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001499 */
1500 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001501 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001502 return 1;
1503 }
1504
1505 /* If we get this far, it means we need to compute the table names.
1506 */
1507 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1508 pSel = pTable->pSelect;
1509
danielk19774adee202004-05-08 08:23:19 +00001510 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001511 ** "*" elements in this list. But we will need to restore the list
1512 ** back to its original configuration afterwards, so we save a copy of
1513 ** the original in pEList.
1514 */
1515 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001516 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001517 if( pSel->pEList==0 ){
1518 pSel->pEList = pEList;
1519 return 1; /* Malloc failed */
1520 }
1521 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001522 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001523 if( pSelTab ){
1524 assert( pTable->aCol==0 );
1525 pTable->nCol = pSelTab->nCol;
1526 pTable->aCol = pSelTab->aCol;
1527 pSelTab->nCol = 0;
1528 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001529 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001530 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001531 }else{
1532 pTable->nCol = 0;
1533 nErr++;
1534 }
danielk19774adee202004-05-08 08:23:19 +00001535 sqlite3SelectUnbind(pSel);
1536 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001537 pSel->pEList = pEList;
1538 return nErr;
1539}
1540
1541/*
drh8bf8dc92003-05-17 17:35:10 +00001542** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001543*/
drhd24cc422003-03-27 12:51:24 +00001544static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001545 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001546 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001547 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001548 Table *pTab = sqliteHashData(i);
1549 if( pTab->pSelect ){
drh956bc922004-07-24 17:38:29 +00001550 sqliteResetColumnNames(pTab);
drh417be792002-03-03 18:59:40 +00001551 }
1552 }
drh8bf8dc92003-05-17 17:35:10 +00001553 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001554}
1555
drh75897232000-05-29 14:26:00 +00001556/*
1557** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001558** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001559*/
danielk1977a8858102004-05-28 12:11:21 +00001560void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1561 Table *pTab;
drh75897232000-05-29 14:26:00 +00001562 Vdbe *v;
1563 int base;
drh5edc3122001-09-13 21:53:09 +00001564 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001565 int iDb;
drh75897232000-05-29 14:26:00 +00001566
danielk1977a8858102004-05-28 12:11:21 +00001567 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1568 assert( pName->nSrc==1 );
1569 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1570
1571 if( pTab==0 ) goto exit_drop_table;
1572 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001573 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001574#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001575 {
1576 int code;
danielk1977a8858102004-05-28 12:11:21 +00001577 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1578 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001579 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001580 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001581 }
drhe5f9c642003-01-13 23:27:31 +00001582 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001583 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001584 code = SQLITE_DROP_TEMP_VIEW;
1585 }else{
1586 code = SQLITE_DROP_VIEW;
1587 }
1588 }else{
drhd24cc422003-03-27 12:51:24 +00001589 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001590 code = SQLITE_DROP_TEMP_TABLE;
1591 }else{
1592 code = SQLITE_DROP_TABLE;
1593 }
1594 }
danielk1977a8858102004-05-28 12:11:21 +00001595 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1596 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001597 }
danielk1977a8858102004-05-28 12:11:21 +00001598 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1599 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001600 }
drhe5f9c642003-01-13 23:27:31 +00001601 }
1602#endif
danielk1977a8858102004-05-28 12:11:21 +00001603 if( pTab->readOnly ){
1604 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001605 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001606 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001607 }
danielk1977a8858102004-05-28 12:11:21 +00001608 if( isView && pTab->pSelect==0 ){
1609 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1610 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001611 }
danielk1977a8858102004-05-28 12:11:21 +00001612 if( !isView && pTab->pSelect ){
1613 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1614 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001615 }
drh75897232000-05-29 14:26:00 +00001616
drh1ccde152000-06-17 13:12:39 +00001617 /* Generate code to remove the table from the master table
1618 ** on disk.
1619 */
danielk19774adee202004-05-08 08:23:19 +00001620 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001621 if( v ){
drh905793e2004-02-21 13:31:09 +00001622 static VdbeOpList dropTable[] = {
danielk19778e227872004-06-07 07:52:17 +00001623 { OP_Rewind, 0, ADDR(13), 0},
1624 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001625 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001626 { OP_MemLoad, 1, 0, 0}, /* 3 */
danielk19778e227872004-06-07 07:52:17 +00001627 { OP_Column, 0, 2, 0}, /* sqlite_master.tbl_name */
1628 { OP_Ne, 0, ADDR(12), 0},
1629 { OP_String8, 0, 0, "trigger"},
1630 { OP_Column, 0, 2, 0}, /* sqlite_master.type */
1631 { OP_Eq, 0, ADDR(12), 0},
drh75897232000-05-29 14:26:00 +00001632 { OP_Delete, 0, 0, 0},
danielk19778e227872004-06-07 07:52:17 +00001633 { OP_Rewind, 0, ADDR(13), 0},
danielk19778d059842004-05-12 11:24:02 +00001634 { OP_Goto, 0, ADDR(3), 0},
danielk19778e227872004-06-07 07:52:17 +00001635 { OP_Next, 0, ADDR(3), 0}, /* 12 */
drh75897232000-05-29 14:26:00 +00001636 };
1637 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001638 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001639 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001640
danielk19778e227872004-06-07 07:52:17 +00001641 /* Drop all triggers associated with the table being dropped. Code
1642 ** is generated to remove entries from sqlite_master and/or
1643 ** sqlite_temp_master if required.
1644 */
danielk1977a8858102004-05-28 12:11:21 +00001645 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001646 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001647 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001648 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drh956bc922004-07-24 17:38:29 +00001649 pTrigger = pTrigger->pNext;
danielk1977c3f9bad2002-05-15 08:30:12 +00001650 }
drh8bf8dc92003-05-17 17:35:10 +00001651
danielk19778e227872004-06-07 07:52:17 +00001652 /* Drop all SQLITE_MASTER table and index entries that refer to the
1653 ** table. The program name loops through the master table and deletes
1654 ** every row that refers to a table of the same name as the one being
1655 ** dropped. Triggers are handled seperately because a trigger can be
1656 ** created in the temp database that refers to a table in another
1657 ** database.
1658 */
danielk1977a8858102004-05-28 12:11:21 +00001659 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001660 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001661 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
danielk19778e227872004-06-07 07:52:17 +00001662 sqlite3ChangeCookie(db, v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001663 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001664 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001665 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1666 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001667 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001668 }
drh5e00f6c2001-09-13 13:46:56 +00001669 }
drh956bc922004-07-24 17:38:29 +00001670 sqlite3VdbeOp3(v, OP_DropTable, pTab->iDb, 0, pTab->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00001671 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001672 }
drhd24cc422003-03-27 12:51:24 +00001673 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001674
1675exit_drop_table:
1676 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001677}
1678
1679/*
drhc2eef3b2002-08-31 18:53:06 +00001680** This routine is called to create a new foreign key on the table
1681** currently under construction. pFromCol determines which columns
1682** in the current table point to the foreign key. If pFromCol==0 then
1683** connect the key to the last column inserted. pTo is the name of
1684** the table referred to. pToCol is a list of tables in the other
1685** pTo table that the foreign key points to. flags contains all
1686** information about the conflict resolution algorithms specified
1687** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1688**
1689** An FKey structure is created and added to the table currently
1690** under construction in the pParse->pNewTable field. The new FKey
1691** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001692** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001693**
1694** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001695** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001696*/
danielk19774adee202004-05-08 08:23:19 +00001697void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001698 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001699 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001700 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001701 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001702 int flags /* Conflict resolution algorithms. */
1703){
1704 Table *p = pParse->pNewTable;
1705 int nByte;
1706 int i;
1707 int nCol;
1708 char *z;
1709 FKey *pFKey = 0;
1710
1711 assert( pTo!=0 );
1712 if( p==0 || pParse->nErr ) goto fk_end;
1713 if( pFromCol==0 ){
1714 int iCol = p->nCol-1;
1715 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001716 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001717 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001718 " should reference only one column of table %T",
1719 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001720 goto fk_end;
1721 }
1722 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001723 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001724 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001725 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001726 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001727 goto fk_end;
1728 }else{
danielk19770202b292004-06-09 09:55:16 +00001729 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001730 }
1731 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1732 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001733 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001734 nByte += strlen(pToCol->a[i].zName) + 1;
1735 }
1736 }
1737 pFKey = sqliteMalloc( nByte );
1738 if( pFKey==0 ) goto fk_end;
1739 pFKey->pFrom = p;
1740 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001741 z = (char*)&pFKey[1];
1742 pFKey->aCol = (struct sColMap*)z;
1743 z += sizeof(struct sColMap)*nCol;
1744 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001745 memcpy(z, pTo->z, pTo->n);
1746 z[pTo->n] = 0;
1747 z += pTo->n+1;
1748 pFKey->pNextTo = 0;
1749 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001750 if( pFromCol==0 ){
1751 pFKey->aCol[0].iFrom = p->nCol-1;
1752 }else{
1753 for(i=0; i<nCol; i++){
1754 int j;
1755 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001756 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001757 pFKey->aCol[i].iFrom = j;
1758 break;
1759 }
1760 }
1761 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001762 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001763 "unknown column \"%s\" in foreign key definition",
1764 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001765 goto fk_end;
1766 }
1767 }
1768 }
1769 if( pToCol ){
1770 for(i=0; i<nCol; i++){
1771 int n = strlen(pToCol->a[i].zName);
1772 pFKey->aCol[i].zCol = z;
1773 memcpy(z, pToCol->a[i].zName, n);
1774 z[n] = 0;
1775 z += n+1;
1776 }
1777 }
1778 pFKey->isDeferred = 0;
1779 pFKey->deleteConf = flags & 0xff;
1780 pFKey->updateConf = (flags >> 8 ) & 0xff;
1781 pFKey->insertConf = (flags >> 16 ) & 0xff;
1782
1783 /* Link the foreign key to the table as the last step.
1784 */
1785 p->pFKey = pFKey;
1786 pFKey = 0;
1787
1788fk_end:
1789 sqliteFree(pFKey);
danielk19770202b292004-06-09 09:55:16 +00001790 sqlite3ExprListDelete(pFromCol);
1791 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001792}
1793
1794/*
1795** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1796** clause is seen as part of a foreign key definition. The isDeferred
1797** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1798** The behavior of the most recently created foreign key is adjusted
1799** accordingly.
1800*/
danielk19774adee202004-05-08 08:23:19 +00001801void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001802 Table *pTab;
1803 FKey *pFKey;
1804 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1805 pFKey->isDeferred = isDeferred;
1806}
1807
1808/*
drh75897232000-05-29 14:26:00 +00001809** Create a new index for an SQL table. pIndex is the name of the index
1810** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001811** be NULL for a primary key or an index that is created to satisfy a
1812** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001813** as the table to be indexed. pParse->pNewTable is a table that is
1814** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001815**
drh382c0242001-10-06 16:33:02 +00001816** pList is a list of columns to be indexed. pList will be NULL if this
1817** is a primary key or unique-constraint on the most recent column added
1818** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001819*/
danielk19774adee202004-05-08 08:23:19 +00001820void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001821 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001822 Token *pName1, /* First part of index name. May be NULL */
1823 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00001824 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
1825 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001826 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001827 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1828 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1829){
danielk1977cbb18d22004-05-28 11:37:27 +00001830 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00001831 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00001832 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001833 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001834 Token nullId; /* Fake token for an empty ID list */
1835 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001836 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001837 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001838
danielk1977cbb18d22004-05-28 11:37:27 +00001839 int iDb; /* Index of the database that is being written */
1840 Token *pName = 0; /* Unqualified name of the index to create */
1841
danielk197724b03fd2004-05-10 10:34:34 +00001842 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001843
drh75897232000-05-29 14:26:00 +00001844 /*
1845 ** Find the table that is to be indexed. Return early if not found.
1846 */
danielk1977cbb18d22004-05-28 11:37:27 +00001847 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001848
1849 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001850 ** to search for the table. 'Fix' the table name to this db
1851 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001852 */
1853 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001854 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001855 if( iDb<0 ) goto exit_create_index;
1856
danielk1977ef2cb632004-05-29 02:37:19 +00001857 /* If the index name was unqualified, check if the the table
1858 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001859 */
danielk1977ef2cb632004-05-29 02:37:19 +00001860 pTab = sqlite3SrcListLookup(pParse, pTblName);
1861 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1862 iDb = 1;
1863 }
1864
1865 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1866 sqlite3FixSrcList(&sFix, pTblName)
1867 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001868 goto exit_create_index;
1869 }
danielk1977ef2cb632004-05-29 02:37:19 +00001870 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1871 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001872 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001873 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001874 }else{
drhe3c41372001-09-17 20:25:58 +00001875 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001876 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001877 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001878 }
danielk1977cbb18d22004-05-28 11:37:27 +00001879
drh75897232000-05-29 14:26:00 +00001880 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001881 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001882 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001883 goto exit_create_index;
1884 }
drha76b5df2002-02-23 02:32:10 +00001885 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001886 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001887 goto exit_create_index;
1888 }
drh4925ca02003-11-27 00:48:57 +00001889 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001890
1891 /*
1892 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001893 ** index or table with the same name.
1894 **
1895 ** Exception: If we are reading the names of permanent indices from the
1896 ** sqlite_master table (because some other process changed the schema) and
1897 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001898 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001899 **
1900 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001901 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1902 ** own name.
drh75897232000-05-29 14:26:00 +00001903 */
danielk1977d8123362004-06-12 09:25:12 +00001904 if( pName ){
drha99db3b2004-06-19 14:49:12 +00001905 zName = sqlite3NameFromToken(pName);
danielk19778a414492004-06-29 08:59:35 +00001906 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001907 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001908 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00001909 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001910 }
danielk1977d8123362004-06-12 09:25:12 +00001911 if( !db->init.busy ){
1912 Index *pISameName; /* Another index with the same name */
1913 Table *pTSameName; /* A table with same name as the index */
danielk19778a414492004-06-29 08:59:35 +00001914 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001915 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
1916 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
1917 goto exit_create_index;
1918 }
1919 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1920 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
1921 goto exit_create_index;
1922 }
drhe3c41372001-09-17 20:25:58 +00001923 }
drhd24cc422003-03-27 12:51:24 +00001924 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001925 char zBuf[30];
1926 int n;
1927 Index *pLoop;
1928 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00001929 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00001930 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00001931 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001932 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00001933 }
1934
drhe5f9c642003-01-13 23:27:31 +00001935 /* Check for authorization to create an index.
1936 */
1937#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001938 {
1939 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001940 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001941 goto exit_create_index;
1942 }
1943 i = SQLITE_CREATE_INDEX;
1944 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001945 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001946 goto exit_create_index;
1947 }
drhe5f9c642003-01-13 23:27:31 +00001948 }
1949#endif
1950
drh75897232000-05-29 14:26:00 +00001951 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001952 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001953 ** So create a fake list to simulate this.
1954 */
1955 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001956 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001957 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00001958 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00001959 if( pList==0 ) goto exit_create_index;
1960 }
1961
1962 /*
1963 ** Allocate the index structure.
1964 */
drhdcc581c2000-05-30 13:44:19 +00001965 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00001966 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00001967 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00001968 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
1969 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00001970 strcpy(pIndex->zName, zName);
1971 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00001972 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00001973 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001974 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00001975 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00001976
drh1ccde152000-06-17 13:12:39 +00001977 /* Scan the names of the columns of the table to be indexed and
1978 ** load the column indices into the Index structure. Report an error
1979 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001980 */
danielk19770202b292004-06-09 09:55:16 +00001981 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00001982 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001983 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001984 }
1985 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001986 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00001987 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001988 goto exit_create_index;
1989 }
drh967e8b72000-06-21 13:59:10 +00001990 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00001991 if( pList->a[i].pExpr ){
1992 assert( pList->a[i].pExpr->pColl );
1993 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
1994 }else{
1995 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
1996 }
1997 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00001998 if( !db->init.busy &&
1999 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2000 ){
2001 goto exit_create_index;
2002 }
drh75897232000-05-29 14:26:00 +00002003 }
danielk19770202b292004-06-09 09:55:16 +00002004 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002005
danielk1977d8123362004-06-12 09:25:12 +00002006 if( pTab==pParse->pNewTable ){
2007 /* This routine has been called to create an automatic index as a
2008 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2009 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2010 ** i.e. one of:
2011 **
2012 ** CREATE TABLE t(x PRIMARY KEY, y);
2013 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2014 **
2015 ** Either way, check to see if the table already has such an index. If
2016 ** so, don't bother creating this one. This only applies to
2017 ** automatically created indices. Users can do as they wish with
2018 ** explicit indices.
2019 */
2020 Index *pIdx;
2021 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2022 int k;
2023 assert( pIdx->onError!=OE_None );
2024 assert( pIdx->autoIndex );
2025 assert( pIndex->onError!=OE_None );
2026
2027 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2028 for(k=0; k<pIdx->nColumn; k++){
2029 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2030 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2031 }
2032 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002033 if( pIdx->onError!=pIndex->onError ){
2034 /* This constraint creates the same index as a previous
2035 ** constraint specified somewhere in the CREATE TABLE statement.
2036 ** However the ON CONFLICT clauses are different. If both this
2037 ** constraint and the previous equivalent constraint have explicit
2038 ** ON CONFLICT clauses this is an error. Otherwise, use the
2039 ** explicitly specified behaviour for the index.
2040 */
2041 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2042 sqlite3ErrorMsg(pParse,
2043 "conflicting ON CONFLICT clauses specified", 0);
2044 }
2045 if( pIdx->onError==OE_Default ){
2046 pIdx->onError = pIndex->onError;
2047 }
2048 }
danielk1977d8123362004-06-12 09:25:12 +00002049 goto exit_create_index;
2050 }
2051 }
2052 }
2053
drh75897232000-05-29 14:26:00 +00002054 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002055 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002056 */
drh234c39d2004-07-24 03:30:47 +00002057 if( db->init.busy ){
drh6d4abfb2001-10-22 02:58:08 +00002058 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002059 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002060 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002061 if( p ){
2062 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002063 goto exit_create_index;
2064 }
drh5e00f6c2001-09-13 13:46:56 +00002065 db->flags |= SQLITE_InternChanges;
drh234c39d2004-07-24 03:30:47 +00002066 if( pTblName!=0 ){
2067 pIndex->tnum = db->init.newTnum;
2068 }
drhd78eeee2001-09-13 16:18:53 +00002069 }
2070
drh1d85d932004-02-14 23:05:52 +00002071 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002072 ** involves writing the index into the master table and filling in the
2073 ** index with the current table contents.
2074 **
drh1d85d932004-02-14 23:05:52 +00002075 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2076 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002077 ** CREATE INDEX statements are read out of the master table. In
2078 ** the latter case the index already exists on disk, which is why
2079 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002080 **
danielk1977cbb18d22004-05-28 11:37:27 +00002081 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002082 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2083 ** has just been created, it contains no data and the index initialization
2084 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002085 */
drh1d85d932004-02-14 23:05:52 +00002086 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00002087 int n;
drhadbca9c2001-09-27 15:11:53 +00002088 Vdbe *v;
drh75897232000-05-29 14:26:00 +00002089 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00002090
danielk19774adee202004-05-08 08:23:19 +00002091 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002092 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00002093 if( pTblName!=0 ){
2094 sqlite3BeginWriteOperation(pParse, 0, iDb);
2095 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00002096 }
danielk19774adee202004-05-08 08:23:19 +00002097 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00002098 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
2099 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
2100 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
drh234c39d2004-07-24 03:30:47 +00002101 sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002102 if( pTblName ){
drha99db3b2004-06-19 14:49:12 +00002103 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2104 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002105 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
2106 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00002107 }
danielk19770f69c1e2004-05-29 11:24:50 +00002108 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00002109 if( pStart && pEnd ){
danielk19770202b292004-06-09 09:55:16 +00002110 if( onError==OE_None ){
2111 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
2112 }else{
2113 sqlite3VdbeChangeP3(v, -1, "CREATE UNIQUE INDEX ", P3_STATIC);
2114 }
danielk19770f69c1e2004-05-29 11:24:50 +00002115 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002116 n = Addr(pEnd->z) - Addr(pName->z) + 1;
2117 sqlite3VdbeChangeP3(v, -1, pName->z, n);
danielk197772c952a2004-06-21 09:06:41 +00002118 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drh75897232000-05-29 14:26:00 +00002119 }
danielk197784ac9d02004-05-18 09:58:06 +00002120 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002121 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002122 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00002123 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002124 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
2125 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00002126 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00002127 lbl2 = sqlite3VdbeMakeLabel(v);
2128 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00002129 lbl1 = sqlite3VdbeCurrentAddr(v);
2130 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00002131 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00002132 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002133 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
2134 sqlite3VdbeResolveLabel(v, lbl2);
2135 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
2136 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drhc275b4e2004-07-19 17:25:24 +00002137 sqlite3ChangeCookie(db, v, iDb);
danielk19774adee202004-05-08 08:23:19 +00002138 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2139 sqlite3EndWriteOperation(pParse);
drh234c39d2004-07-24 03:30:47 +00002140 sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0,
2141 sqlite3MPrintf("name='%q'", pIndex->zName), P3_DYNAMIC);
drh5e00f6c2001-09-13 13:46:56 +00002142 }
drh75897232000-05-29 14:26:00 +00002143 }
2144
danielk1977d8123362004-06-12 09:25:12 +00002145 /* When adding an index to the list of indices for a table, make
2146 ** sure all indices labeled OE_Replace come after all those labeled
2147 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2148 ** and INSERT.
2149 */
drh234c39d2004-07-24 03:30:47 +00002150 if( db->init.busy || pTblName==0 ){
2151 if( onError!=OE_Replace || pTab->pIndex==0
2152 || pTab->pIndex->onError==OE_Replace){
2153 pIndex->pNext = pTab->pIndex;
2154 pTab->pIndex = pIndex;
2155 }else{
2156 Index *pOther = pTab->pIndex;
2157 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2158 pOther = pOther->pNext;
2159 }
2160 pIndex->pNext = pOther->pNext;
2161 pOther->pNext = pIndex;
danielk1977d8123362004-06-12 09:25:12 +00002162 }
drh234c39d2004-07-24 03:30:47 +00002163 pIndex = 0;
danielk1977d8123362004-06-12 09:25:12 +00002164 }
danielk1977d8123362004-06-12 09:25:12 +00002165
drh75897232000-05-29 14:26:00 +00002166 /* Clean up before exiting */
2167exit_create_index:
drh956bc922004-07-24 17:38:29 +00002168 if( pIndex ){
2169 freeIndex(pIndex);
2170 }
danielk19770202b292004-06-09 09:55:16 +00002171 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002172 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002173 sqliteFree(zName);
2174 return;
2175}
2176
2177/*
drh74e24cd2002-01-09 03:19:59 +00002178** This routine will drop an existing named index. This routine
2179** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002180*/
danielk19774adee202004-05-08 08:23:19 +00002181void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002182 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002183 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00002184 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002185
danielk197724b03fd2004-05-10 10:34:34 +00002186 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002187 assert( pName->nSrc==1 );
danielk19778a414492004-06-29 08:59:35 +00002188 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19774adee202004-05-08 08:23:19 +00002189 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002190 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002191 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002192 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002193 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002194 }
drh485b39b2002-07-13 03:11:52 +00002195 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002196 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002197 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002198 goto exit_drop_index;
2199 }
drhe5f9c642003-01-13 23:27:31 +00002200#ifndef SQLITE_OMIT_AUTHORIZATION
2201 {
2202 int code = SQLITE_DROP_INDEX;
2203 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002204 const char *zDb = db->aDb[pIndex->iDb].zName;
2205 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002206 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002207 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002208 }
drhd24cc422003-03-27 12:51:24 +00002209 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002210 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002211 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002212 }
drhed6c8672003-01-12 18:02:16 +00002213 }
drhe5f9c642003-01-13 23:27:31 +00002214#endif
drh75897232000-05-29 14:26:00 +00002215
2216 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002217 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002218 if( v ){
drh905793e2004-02-21 13:31:09 +00002219 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00002220 { OP_Rewind, 0, ADDR(9), 0},
drh234c39d2004-07-24 03:30:47 +00002221 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00002222 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002223 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00002224 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002225 { OP_Eq, 0, ADDR(8), 0},
2226 { OP_Next, 0, ADDR(3), 0},
2227 { OP_Goto, 0, ADDR(9), 0},
2228 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00002229 };
2230 int base;
2231
danielk19774adee202004-05-08 08:23:19 +00002232 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
2233 sqlite3OpenMasterTable(v, pIndex->iDb);
2234 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
2235 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
drhc275b4e2004-07-19 17:25:24 +00002236 sqlite3ChangeCookie(db, v, pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002237 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2238 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh956bc922004-07-24 17:38:29 +00002239 sqlite3VdbeOp3(v, OP_DropIndex, pIndex->iDb, 0, pIndex->zName, 0);
danielk19774adee202004-05-08 08:23:19 +00002240 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00002241 }
2242
drhd24cc422003-03-27 12:51:24 +00002243exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002244 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002245}
2246
2247/*
drh75897232000-05-29 14:26:00 +00002248** Append a new element to the given IdList. Create a new IdList if
2249** need be.
drhdaffd0e2001-04-11 14:28:42 +00002250**
2251** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002252*/
danielk19774adee202004-05-08 08:23:19 +00002253IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002254 if( pList==0 ){
2255 pList = sqliteMalloc( sizeof(IdList) );
2256 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002257 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002258 }
drh4305d102003-07-30 12:34:12 +00002259 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002260 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002261 pList->nAlloc = pList->nAlloc*2 + 5;
2262 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002263 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002264 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002265 return 0;
drh75897232000-05-29 14:26:00 +00002266 }
drh6d4abfb2001-10-22 02:58:08 +00002267 pList->a = a;
drh75897232000-05-29 14:26:00 +00002268 }
2269 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
drha99db3b2004-06-19 14:49:12 +00002270 pList->a[pList->nId].zName = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002271 pList->nId++;
2272 return pList;
2273}
2274
2275/*
drhad3cab52002-05-24 02:04:32 +00002276** Append a new table name to the given SrcList. Create a new SrcList if
2277** need be. A new entry is created in the SrcList even if pToken is NULL.
2278**
2279** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002280**
2281** If pDatabase is not null, it means that the table has an optional
2282** database name prefix. Like this: "database.table". The pDatabase
2283** points to the table name and the pTable points to the database name.
2284** The SrcList.a[].zName field is filled with the table name which might
2285** come from pTable (if pDatabase is NULL) or from pDatabase.
2286** SrcList.a[].zDatabase is filled with the database name from pTable,
2287** or with NULL if no database is specified.
2288**
2289** In other words, if call like this:
2290**
danielk19774adee202004-05-08 08:23:19 +00002291** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002292**
2293** Then B is a table name and the database name is unspecified. If called
2294** like this:
2295**
danielk19774adee202004-05-08 08:23:19 +00002296** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002297**
2298** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002299*/
danielk19774adee202004-05-08 08:23:19 +00002300SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drha99db3b2004-06-19 14:49:12 +00002301 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002302 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002303 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002304 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002305 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002306 }
drh4305d102003-07-30 12:34:12 +00002307 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002308 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002309 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002310 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002311 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002312 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002313 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002314 return 0;
2315 }
drh113088e2003-03-20 01:16:58 +00002316 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002317 }
drha99db3b2004-06-19 14:49:12 +00002318 pItem = &pList->a[pList->nSrc];
2319 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002320 if( pDatabase && pDatabase->z==0 ){
2321 pDatabase = 0;
2322 }
2323 if( pDatabase && pTable ){
2324 Token *pTemp = pDatabase;
2325 pDatabase = pTable;
2326 pTable = pTemp;
2327 }
drha99db3b2004-06-19 14:49:12 +00002328 pItem->zName = sqlite3NameFromToken(pTable);
2329 pItem->zDatabase = sqlite3NameFromToken(pDatabase);
2330 pItem->iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002331 pList->nSrc++;
2332 return pList;
2333}
2334
2335/*
drh63eb5f22003-04-29 16:20:44 +00002336** Assign cursors to all tables in a SrcList
2337*/
danielk19774adee202004-05-08 08:23:19 +00002338void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002339 int i;
2340 for(i=0; i<pList->nSrc; i++){
2341 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002342 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002343 }
2344 }
2345}
2346
2347/*
drh75897232000-05-29 14:26:00 +00002348** Add an alias to the last identifier on the given identifier list.
2349*/
danielk19774adee202004-05-08 08:23:19 +00002350void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002351 if( pList && pList->nSrc>0 ){
drha99db3b2004-06-19 14:49:12 +00002352 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002353 }
2354}
2355
2356/*
drhad3cab52002-05-24 02:04:32 +00002357** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002358*/
danielk19774adee202004-05-08 08:23:19 +00002359void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002360 int i;
2361 if( pList==0 ) return;
2362 for(i=0; i<pList->nId; i++){
2363 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002364 }
2365 sqliteFree(pList->a);
2366 sqliteFree(pList);
2367}
2368
2369/*
drhad2d8302002-05-24 20:31:36 +00002370** Return the index in pList of the identifier named zId. Return -1
2371** if not found.
2372*/
danielk19774adee202004-05-08 08:23:19 +00002373int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002374 int i;
2375 if( pList==0 ) return -1;
2376 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002377 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002378 }
2379 return -1;
2380}
2381
2382/*
drhad3cab52002-05-24 02:04:32 +00002383** Delete an entire SrcList including all its substructure.
2384*/
danielk19774adee202004-05-08 08:23:19 +00002385void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002386 int i;
drhbe5c89a2004-07-26 00:31:09 +00002387 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002388 if( pList==0 ) return;
drhbe5c89a2004-07-26 00:31:09 +00002389 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
2390 sqliteFree(pItem->zDatabase);
2391 sqliteFree(pItem->zName);
2392 sqliteFree(pItem->zAlias);
2393 if( pItem->pTab && pItem->pTab->isTransient ){
2394 sqlite3DeleteTable(0, pItem->pTab);
drhdaffd0e2001-04-11 14:28:42 +00002395 }
drhbe5c89a2004-07-26 00:31:09 +00002396 sqlite3SelectDelete(pItem->pSelect);
2397 sqlite3ExprDelete(pItem->pOn);
2398 sqlite3IdListDelete(pItem->pUsing);
drh75897232000-05-29 14:26:00 +00002399 }
drh75897232000-05-29 14:26:00 +00002400 sqliteFree(pList);
2401}
2402
drh982cef72000-05-30 16:27:03 +00002403/*
drhc4a3c772001-04-04 11:48:57 +00002404** Begin a transaction
2405*/
danielk197733752f82004-05-31 08:55:33 +00002406void sqlite3BeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002407 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002408 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002409
drh001bbcb2003-03-19 03:14:00 +00002410 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002411 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002412 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002413
2414 v = sqlite3GetVdbe(pParse);
2415 if( !v ) return;
2416 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002417}
2418
2419/*
2420** Commit a transaction
2421*/
danielk19774adee202004-05-08 08:23:19 +00002422void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002423 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002424 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002425
drh001bbcb2003-03-19 03:14:00 +00002426 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002427 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002428 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002429
2430 v = sqlite3GetVdbe(pParse);
2431 if( v ){
2432 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002433 }
drhc4a3c772001-04-04 11:48:57 +00002434}
2435
2436/*
2437** Rollback a transaction
2438*/
danielk19774adee202004-05-08 08:23:19 +00002439void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002440 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002441 Vdbe *v;
2442
drh001bbcb2003-03-19 03:14:00 +00002443 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002444 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002445 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002446
danielk19774adee202004-05-08 08:23:19 +00002447 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002448 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002449 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002450 }
drhc4a3c772001-04-04 11:48:57 +00002451}
drhf57b14a2001-09-14 18:54:08 +00002452
2453/*
drhdc3ff9c2004-08-18 02:10:15 +00002454** Make sure the TEMP database is open and available for use. Return
2455** the number of errors. Leave any error messages in the pParse structure.
2456*/
2457static int sqlite3OpenTempDatabase(Parse *pParse){
2458 sqlite3 *db = pParse->db;
2459 if( db->aDb[1].pBt==0 && !pParse->explain ){
2460 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
2461 if( rc!=SQLITE_OK ){
2462 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
2463 "file for storing temporary tables");
2464 pParse->rc = rc;
2465 return 1;
2466 }
2467 if( db->flags & !db->autoCommit ){
2468 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
2469 if( rc!=SQLITE_OK ){
2470 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
2471 "the temporary database file");
2472 pParse->rc = rc;
2473 return 1;
2474 }
2475 }
2476 }
2477 return 0;
2478}
2479
2480/*
drh80242052004-06-09 00:48:12 +00002481** Generate VDBE code that will verify the schema cookie and start
2482** a read-transaction for all named database files.
2483**
2484** It is important that all schema cookies be verified and all
2485** read transactions be started before anything else happens in
2486** the VDBE program. But this routine can be called after much other
2487** code has been generated. So here is what we do:
2488**
drhc275b4e2004-07-19 17:25:24 +00002489** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00002490** will jump to a subroutine at the end of the program. Then we
2491** record every database that needs its schema verified in the
2492** pParse->cookieMask field. Later, after all other code has been
2493** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00002494** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00002495** will be made to point to that subroutine. The generation of the
2496** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00002497**
2498** If iDb<0 then code the OP_Goto only - don't set flag to verify the
2499** schema on any databases. This can be used to position the OP_Goto
2500** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00002501*/
danielk19774adee202004-05-08 08:23:19 +00002502void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh80242052004-06-09 00:48:12 +00002503 sqlite *db;
2504 Vdbe *v;
2505 int mask;
2506
2507 v = sqlite3GetVdbe(pParse);
2508 if( v==0 ) return; /* This only happens if there was a prior error */
2509 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00002510 if( pParse->cookieGoto==0 ){
2511 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00002512 }
drhc275b4e2004-07-19 17:25:24 +00002513 if( iDb>=0 ){
2514 assert( iDb<db->nDb );
2515 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2516 assert( iDb<32 );
2517 mask = 1<<iDb;
2518 if( (pParse->cookieMask & mask)==0 ){
2519 pParse->cookieMask |= mask;
2520 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
drhdc3ff9c2004-08-18 02:10:15 +00002521 if( iDb==1 ){
2522 sqlite3OpenTempDatabase(pParse);
2523 }
drhc275b4e2004-07-19 17:25:24 +00002524 }
drh001bbcb2003-03-19 03:14:00 +00002525 }
drh001bbcb2003-03-19 03:14:00 +00002526}
2527
2528/*
drh1c928532002-01-31 15:54:21 +00002529** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002530** might change the database.
2531**
2532** This routine starts a new transaction if we are not already within
2533** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002534** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002535** be set for operations that might fail (due to a constraint) part of
2536** the way through and which will need to undo some writes without having to
2537** rollback the whole transaction. For operations where all constraints
2538** can be checked before any changes are made to the database, it is never
2539** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002540**
drh8bf8dc92003-05-17 17:35:10 +00002541** Only database iDb and the temp database are made writable by this call.
2542** If iDb==0, then the main and temp databases are made writable. If
2543** iDb==1 then only the temp database is made writable. If iDb>1 then the
2544** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002545*/
drh7f0f12e2004-05-21 13:39:50 +00002546void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002547 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002548 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002549 sqlite3CodeVerifySchema(pParse, iDb);
2550 pParse->writeMask |= 1<<iDb;
danielk19771d850a72004-05-31 08:26:49 +00002551 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002552 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002553 }
2554 if( iDb!=1 ){
2555 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002556 }
2557}
2558
2559/*
drh1c928532002-01-31 15:54:21 +00002560** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002561** the database. If a statement transaction was started, then emit
2562** an OP_Commit that will cause the changes to be committed to disk.
2563**
2564** Note that checkpoints are automatically committed at the end of
2565** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002566** sqlite3BeginWriteOperation() but there should only be a single
2567** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002568*/
danielk19774adee202004-05-08 08:23:19 +00002569void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002570 /* Delete me! */
2571 return;
drh1c928532002-01-31 15:54:21 +00002572}
danielk1977bfd6cce2004-06-18 04:24:54 +00002573
2574/*
2575** Return the transient sqlite3_value object used for encoding conversions
2576** during SQL compilation.
2577*/
2578sqlite3_value *sqlite3GetTransientValue(sqlite *db){
2579 if( !db->pValue ){
2580 db->pValue = sqlite3ValueNew();
2581 }
2582 return db->pValue;
2583}