blob: 871b996759b7089c25632c69484e740abc844b0f [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**
drhc275b4e2004-07-19 17:25:24 +000026** $Id: build.c,v 1.238 2004/07/19 17:25:25 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 }
83 }
84
85 /* Get the VDBE program ready for execution
86 */
drhb86ccfb2003-01-28 23:13:10 +000087 if( v && pParse->nErr==0 ){
88 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +000089 sqlite3VdbeTrace(v, trace);
90 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +000091 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +000092 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +000093 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +000094 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +000095 }
drha226d052002-09-25 19:04:07 +000096 pParse->nTab = 0;
97 pParse->nMem = 0;
98 pParse->nSet = 0;
99 pParse->nAgg = 0;
drh7c972de2003-09-06 22:18:07 +0000100 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000101 pParse->cookieMask = 0;
drhc275b4e2004-07-19 17:25:24 +0000102 pParse->cookieGoto = 0;
drh75897232000-05-29 14:26:00 +0000103}
104
105/*
danielk19778a414492004-06-29 08:59:35 +0000106** Locate the in-memory structure that describes a particular database
107** table given the name of that table and (optionally) the name of the
108** database containing the table. Return NULL if not found.
drha69d9162003-04-17 22:57:53 +0000109**
danielk19778a414492004-06-29 08:59:35 +0000110** If zDatabase is 0, all databases are searched for the table and the
111** first matching table is returned. (No checking for duplicate table
112** names is done.) The search order is TEMP first, then MAIN, then any
113** auxiliary databases added using the ATTACH command.
drhf26e09c2003-05-31 16:21:12 +0000114**
danielk19774adee202004-05-08 08:23:19 +0000115** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000116*/
danielk19774adee202004-05-08 08:23:19 +0000117Table *sqlite3FindTable(sqlite *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000118 Table *p = 0;
119 int i;
drh645f63e2004-06-22 13:22:40 +0000120 assert( zName!=0 );
danielk19778a414492004-06-29 08:59:35 +0000121 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
122 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000123 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000124 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
125 p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000126 if( p ) break;
127 }
drh74e24cd2002-01-09 03:19:59 +0000128 return p;
drh75897232000-05-29 14:26:00 +0000129}
130
131/*
danielk19778a414492004-06-29 08:59:35 +0000132** Locate the in-memory structure that describes a particular database
133** table given the name of that table and (optionally) the name of the
134** database containing the table. Return NULL if not found. Also leave an
135** error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000136**
danielk19778a414492004-06-29 08:59:35 +0000137** The difference between this routine and sqlite3FindTable() is that this
138** routine leaves an error message in pParse->zErrMsg where
139** sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000140*/
danielk19774adee202004-05-08 08:23:19 +0000141Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000142 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000143
danielk19778a414492004-06-29 08:59:35 +0000144 /* Read the database schema. If an error occurs, leave an error message
145 ** and code in pParse and return NULL. */
146 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
147 return 0;
148 }
149
danielk19774adee202004-05-08 08:23:19 +0000150 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000151 if( p==0 ){
danielk19778a414492004-06-29 08:59:35 +0000152 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000153 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
154 }else if( sqlite3FindTable(pParse->db, zName, 0)!=0 ){
155 sqlite3ErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
drhf26e09c2003-05-31 16:21:12 +0000156 zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000157 }else{
danielk19774adee202004-05-08 08:23:19 +0000158 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000159 }
drha6ecd332004-06-10 00:29:09 +0000160 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000161 }
162 return p;
163}
164
165/*
166** Locate the in-memory structure that describes
167** a particular index given the name of that index
168** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000169** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000170**
171** If zDatabase is 0, all databases are searched for the
172** table and the first matching index is returned. (No checking
173** for duplicate index names is done.) The search order is
174** TEMP first, then MAIN, then any auxiliary databases added
175** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000176*/
danielk19774adee202004-05-08 08:23:19 +0000177Index *sqlite3FindIndex(sqlite *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000178 Index *p = 0;
179 int i;
danielk19778a414492004-06-29 08:59:35 +0000180 assert( (db->flags & SQLITE_Initialized) || db->init.busy );
181 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000182 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000183 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
184 p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000185 if( p ) break;
186 }
drh74e24cd2002-01-09 03:19:59 +0000187 return p;
drh75897232000-05-29 14:26:00 +0000188}
189
190/*
191** Remove the given index from the index hash table, and free
192** its memory structures.
193**
drhd229ca92002-01-09 13:30:41 +0000194** The index is removed from the database hash tables but
195** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000196** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000197*/
drh74e24cd2002-01-09 03:19:59 +0000198static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000199 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000200
drhd229ca92002-01-09 13:30:41 +0000201 assert( db!=0 && p->zName!=0 );
danielk19774adee202004-05-08 08:23:19 +0000202 pOld = sqlite3HashInsert(&db->aDb[p->iDb].idxHash, p->zName,
drhd24cc422003-03-27 12:51:24 +0000203 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000204 if( pOld!=0 && pOld!=p ){
danielk19774adee202004-05-08 08:23:19 +0000205 sqlite3HashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
drhd24cc422003-03-27 12:51:24 +0000206 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000207 }
danielk1977a37cdde2004-05-16 11:15:36 +0000208 if( p->zColAff ){
209 sqliteFree(p->zColAff);
210 }
drh74e24cd2002-01-09 03:19:59 +0000211 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000212}
213
214/*
drhbeae3192001-09-22 18:12:08 +0000215** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000216** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000217** structures.
218*/
danielk19774adee202004-05-08 08:23:19 +0000219void sqlite3UnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000220 if( pIndex->pTable->pIndex==pIndex ){
221 pIndex->pTable->pIndex = pIndex->pNext;
222 }else{
223 Index *p;
224 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
225 if( p && p->pNext==pIndex ){
226 p->pNext = pIndex->pNext;
227 }
228 }
229 sqliteDeleteIndex(db, pIndex);
230}
231
232/*
drhe0bc4042002-06-25 01:09:11 +0000233** Erase all schema information from the in-memory hash tables of
drh0e3d7472004-06-19 17:33:07 +0000234** a sigle database. This routine is called to reclaim memory
235** before the closes. It is also called during a rollback
danielk1977e0d4b062004-06-28 01:11:46 +0000236** if there were schema changes during the transaction or if a
237** schema-cookie mismatch occurs.
drh1c2d8412003-03-31 00:30:47 +0000238**
239** If iDb<=0 then reset the internal schema tables for all database
240** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000241** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000242*/
danielk19774adee202004-05-08 08:23:19 +0000243void sqlite3ResetInternalSchema(sqlite *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000244 HashElem *pElem;
245 Hash temp1;
246 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000247 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000248
drh1c2d8412003-03-31 00:30:47 +0000249 assert( iDb>=0 && iDb<db->nDb );
250 db->flags &= ~SQLITE_Initialized;
251 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000252 Db *pDb = &db->aDb[i];
253 temp1 = pDb->tblHash;
254 temp2 = pDb->trigHash;
danielk19774adee202004-05-08 08:23:19 +0000255 sqlite3HashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
256 sqlite3HashClear(&pDb->aFKey);
257 sqlite3HashClear(&pDb->idxHash);
drhd24cc422003-03-27 12:51:24 +0000258 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
259 Trigger *pTrigger = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000260 sqlite3DeleteTrigger(pTrigger);
drhd24cc422003-03-27 12:51:24 +0000261 }
danielk19774adee202004-05-08 08:23:19 +0000262 sqlite3HashClear(&temp2);
263 sqlite3HashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
drhd24cc422003-03-27 12:51:24 +0000264 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
265 Table *pTab = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000266 sqlite3DeleteTable(db, pTab);
drhd24cc422003-03-27 12:51:24 +0000267 }
danielk19774adee202004-05-08 08:23:19 +0000268 sqlite3HashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000269 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000270 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000271 }
drh1c2d8412003-03-31 00:30:47 +0000272 assert( iDb==0 );
273 db->flags &= ~SQLITE_InternChanges;
274
275 /* If one or more of the auxiliary database files has been closed,
276 ** then remove then from the auxiliary database list. We take the
277 ** opportunity to do this here since we have just deleted all of the
278 ** schema hash tables and therefore do not have to make any changes
279 ** to any of those tables.
280 */
drh4d189ca2004-02-12 18:46:38 +0000281 for(i=0; i<db->nDb; i++){
282 struct Db *pDb = &db->aDb[i];
283 if( pDb->pBt==0 ){
284 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
285 pDb->pAux = 0;
286 }
287 }
drh1c2d8412003-03-31 00:30:47 +0000288 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000289 struct Db *pDb = &db->aDb[i];
290 if( pDb->pBt==0 ){
291 sqliteFree(pDb->zName);
292 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000293 continue;
294 }
295 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000296 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000297 }
drh8bf8dc92003-05-17 17:35:10 +0000298 j++;
drh1c2d8412003-03-31 00:30:47 +0000299 }
300 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
301 db->nDb = j;
302 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
303 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
304 sqliteFree(db->aDb);
305 db->aDb = db->aDbStatic;
306 }
drhe0bc4042002-06-25 01:09:11 +0000307}
308
309/*
310** This routine is called whenever a rollback occurs. If there were
311** schema changes during the transaction, then we have to reset the
312** internal hash tables and reload them from disk.
313*/
danielk19774adee202004-05-08 08:23:19 +0000314void sqlite3RollbackInternalChanges(sqlite *db){
drhe0bc4042002-06-25 01:09:11 +0000315 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000316 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000317 }
318}
319
320/*
321** This routine is called when a commit occurs.
322*/
danielk19774adee202004-05-08 08:23:19 +0000323void sqlite3CommitInternalChanges(sqlite *db){
drhe0bc4042002-06-25 01:09:11 +0000324 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000325}
326
327/*
drh75897232000-05-29 14:26:00 +0000328** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000329** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000330**
331** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000332** the table data structure from the hash table. Nor does it remove
333** foreign keys from the sqlite.aFKey hash table. But it does destroy
334** memory structures of the indices and foreign keys associated with
335** the table.
drhdaffd0e2001-04-11 14:28:42 +0000336**
337** Indices associated with the table are unlinked from the "db"
338** data structure if db!=NULL. If db==NULL, indices attached to
339** the table are deleted, but it is assumed they have already been
340** unlinked.
drh75897232000-05-29 14:26:00 +0000341*/
danielk19774adee202004-05-08 08:23:19 +0000342void sqlite3DeleteTable(sqlite *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000343 int i;
344 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000345 FKey *pFKey, *pNextFKey;
346
drh75897232000-05-29 14:26:00 +0000347 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000348
349 /* Delete all indices associated with this table
350 */
351 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
352 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000353 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000354 sqliteDeleteIndex(db, pIndex);
355 }
356
357 /* Delete all foreign keys associated with this table. The keys
358 ** should have already been unlinked from the db->aFKey hash table
359 */
360 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
361 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000362 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000363 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000364 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000365 sqliteFree(pFKey);
366 }
367
368 /* Delete the Table structure itself.
369 */
drh75897232000-05-29 14:26:00 +0000370 for(i=0; i<pTable->nCol; i++){
drh0e3d7472004-06-19 17:33:07 +0000371 Column *pCol = &pTable->aCol[i];
372 sqliteFree(pCol->zName);
373 sqliteFree(pCol->zDflt);
374 sqliteFree(pCol->zType);
drh75897232000-05-29 14:26:00 +0000375 }
drh6e142f52000-06-08 13:36:40 +0000376 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000377 sqliteFree(pTable->aCol);
danielk19773d1bfea2004-05-14 11:00:53 +0000378 if( pTable->zColAff ){
379 sqliteFree(pTable->zColAff);
380 }
danielk19774adee202004-05-08 08:23:19 +0000381 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000382 sqliteFree(pTable);
383}
384
385/*
drh5edc3122001-09-13 21:53:09 +0000386** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000387** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000388*/
drh74e24cd2002-01-09 03:19:59 +0000389static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000390 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000391 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000392 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000393 assert( db!=0 );
drh0e3d7472004-06-19 17:33:07 +0000394 pOld = sqlite3HashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1,0);
drhd229ca92002-01-09 13:30:41 +0000395 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000396 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
397 int nTo = strlen(pF1->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +0000398 pF2 = sqlite3HashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000399 if( pF2==pF1 ){
danielk19774adee202004-05-08 08:23:19 +0000400 sqlite3HashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000401 }else{
402 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
403 if( pF2 ){
404 pF2->pNextTo = pF1->pNextTo;
405 }
406 }
407 }
danielk19774adee202004-05-08 08:23:19 +0000408 sqlite3DeleteTable(db, p);
drh74e24cd2002-01-09 03:19:59 +0000409}
410
411/*
drha99db3b2004-06-19 14:49:12 +0000412** Given a token, return a string that consists of the text of that
413** token with any quotations removed. Space to hold the returned string
414** is obtained from sqliteMalloc() and must be freed by the calling
415** function.
drh75897232000-05-29 14:26:00 +0000416**
drha99db3b2004-06-19 14:49:12 +0000417** Tokens are really just pointers into the original SQL text and so
418** are not \000 terminated and are not persistent. The returned string
419** is \000 terminated and is persistent.
drh75897232000-05-29 14:26:00 +0000420*/
drha99db3b2004-06-19 14:49:12 +0000421char *sqlite3NameFromToken(Token *pName){
422 char *zName;
423 if( pName ){
424 zName = sqliteStrNDup(pName->z, pName->n);
425 sqlite3Dequote(zName);
426 }else{
427 zName = 0;
428 }
drh75897232000-05-29 14:26:00 +0000429 return zName;
430}
431
432/*
danielk1977cbb18d22004-05-28 11:37:27 +0000433** Open the sqlite_master table stored in database number iDb for
434** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000435*/
danielk1977cbb18d22004-05-28 11:37:27 +0000436void sqlite3OpenMasterTable(Vdbe *v, int iDb){
437 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000438 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000439 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000440}
441
442/*
danielk1977cbb18d22004-05-28 11:37:27 +0000443** The token *pName contains the name of a database (either "main" or
444** "temp" or the name of an attached db). This routine returns the
445** index of the named database in db->aDb[], or -1 if the named db
446** does not exist.
447*/
448int findDb(sqlite3 *db, Token *pName){
449 int i;
450 for(i=0; i<db->nDb; i++){
451 if( pName->n==strlen(db->aDb[i].zName) &&
452 0==sqlite3StrNICmp(db->aDb[i].zName, pName->z, pName->n) ){
453 return i;
454 }
455 }
456 return -1;
457}
458
drh0e3d7472004-06-19 17:33:07 +0000459/* The table or view or trigger name is passed to this routine via tokens
460** pName1 and pName2. If the table name was fully qualified, for example:
461**
462** CREATE TABLE xxx.yyy (...);
463**
464** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
465** the table name is not fully qualified, i.e.:
466**
467** CREATE TABLE yyy(...);
468**
469** Then pName1 is set to "yyy" and pName2 is "".
470**
471** This routine sets the *ppUnqual pointer to point at the token (pName1 or
472** pName2) that stores the unqualified table name. The index of the
473** database "xxx" is returned.
474*/
danielk1977ef2cb632004-05-29 02:37:19 +0000475int sqlite3TwoPartName(
drh0e3d7472004-06-19 17:33:07 +0000476 Parse *pParse, /* Parsing and code generating context */
477 Token *pName1, /* The "xxx" in the name "xxx.yyy" */
478 Token *pName2, /* The "yyy" in the name "xxx.yyy" */
479 Token **pUnqual /* Write the unqualified object name here */
danielk1977cbb18d22004-05-28 11:37:27 +0000480){
drh0e3d7472004-06-19 17:33:07 +0000481 int iDb; /* Database holding the object */
danielk1977cbb18d22004-05-28 11:37:27 +0000482 sqlite3 *db = pParse->db;
483
484 if( pName2 && pName2->n>0 ){
485 assert( !db->init.busy );
486 *pUnqual = pName2;
487 iDb = findDb(db, pName1);
488 if( iDb<0 ){
489 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
490 pParse->nErr++;
491 return -1;
492 }
493 }else{
494 assert( db->init.iDb==0 || db->init.busy );
495 iDb = db->init.iDb;
496 *pUnqual = pName1;
497 }
498 return iDb;
499}
500
501/*
danielk1977d8123362004-06-12 09:25:12 +0000502** This routine is used to check if the UTF-8 string zName is a legal
503** unqualified name for a new schema object (table, index, view or
504** trigger). All names are legal except those that begin with the string
505** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
506** is reserved for internal use.
507*/
508int sqlite3CheckObjectName(Parse *pParse, const char *zName){
509 if( !pParse->db->init.busy && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
510 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
511 return SQLITE_ERROR;
512 }
513 return SQLITE_OK;
514}
515
516/*
drh75897232000-05-29 14:26:00 +0000517** Begin constructing a new table representation in memory. This is
518** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000519** to a CREATE TABLE statement. In particular, this routine is called
520** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000521** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000522** flag is true if the table should be stored in the auxiliary database
523** file instead of in the main database file. This is normally the case
524** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000525** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000526**
drhf57b3392001-10-08 13:22:32 +0000527** The new table record is initialized and put in pParse->pNewTable.
528** As more of the CREATE TABLE statement is parsed, additional action
529** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000530** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000531** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000532*/
danielk19774adee202004-05-08 08:23:19 +0000533void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000534 Parse *pParse, /* Parser context */
535 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000536 Token *pName1, /* First part of the name of the table or view */
537 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000538 int isTemp, /* True if this is a TEMP table */
539 int isView /* True if this is a VIEW */
540){
drh75897232000-05-29 14:26:00 +0000541 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000542 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000543 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000544 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000545 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000546 int iDb; /* Database number to create the table in */
547 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000548
danielk1977cbb18d22004-05-28 11:37:27 +0000549 /* The table or view name to create is passed to this routine via tokens
550 ** pName1 and pName2. If the table name was fully qualified, for example:
551 **
552 ** CREATE TABLE xxx.yyy (...);
553 **
554 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
555 ** the table name is not fully qualified, i.e.:
556 **
557 ** CREATE TABLE yyy(...);
558 **
559 ** Then pName1 is set to "yyy" and pName2 is "".
560 **
561 ** The call below sets the pName pointer to point at the token (pName1 or
562 ** pName2) that stores the unqualified table name. The variable iDb is
563 ** set to the index of the database that the table or view is to be
564 ** created in.
565 */
danielk1977ef2cb632004-05-29 02:37:19 +0000566 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000567 if( iDb<0 ) return;
568 if( isTemp && iDb>1 ){
569 /* If creating a temp table, the name may not be qualified */
570 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
571 pParse->nErr++;
572 return;
573 }
574 if( isTemp ) iDb = 1;
575
576 pParse->sNameToken = *pName;
drha99db3b2004-06-19 14:49:12 +0000577 zName = sqlite3NameFromToken(pName);
danielk1977e0048402004-06-15 16:51:01 +0000578 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000579 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
danielk1977e0048402004-06-15 16:51:01 +0000580 sqliteFree(zName);
danielk1977d8123362004-06-12 09:25:12 +0000581 return;
582 }
drh1d85d932004-02-14 23:05:52 +0000583 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000584#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000585 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000586 {
587 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000588 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000589 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000590 sqliteFree(zName);
591 return;
592 }
drhe5f9c642003-01-13 23:27:31 +0000593 if( isView ){
594 if( isTemp ){
595 code = SQLITE_CREATE_TEMP_VIEW;
596 }else{
597 code = SQLITE_CREATE_VIEW;
598 }
599 }else{
600 if( isTemp ){
601 code = SQLITE_CREATE_TEMP_TABLE;
602 }else{
603 code = SQLITE_CREATE_TABLE;
604 }
605 }
danielk19774adee202004-05-08 08:23:19 +0000606 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000607 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000608 return;
609 }
610 }
611#endif
drhf57b3392001-10-08 13:22:32 +0000612
613 /* Before trying to create a temporary table, make sure the Btree for
614 ** holding temporary tables is open.
615 */
drhd24cc422003-03-27 12:51:24 +0000616 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +0000617 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000618 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000619 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
drhf7a9e1a2004-02-22 18:40:56 +0000620 "file for storing temporary tables");
drhf57b3392001-10-08 13:22:32 +0000621 pParse->nErr++;
danielk19772b444852004-06-29 07:45:33 +0000622 pParse->rc = rc;
danielk1977e0048402004-06-15 16:51:01 +0000623 sqliteFree(zName);
drhf57b3392001-10-08 13:22:32 +0000624 return;
625 }
danielk1977ee5741e2004-05-31 10:01:34 +0000626 if( db->flags & !db->autoCommit ){
danielk197740b38dc2004-06-26 08:38:24 +0000627 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1);
drhf57b3392001-10-08 13:22:32 +0000628 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000629 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
drhf7a9e1a2004-02-22 18:40:56 +0000630 "the temporary database file");
danielk1977e0048402004-06-15 16:51:01 +0000631 sqliteFree(zName);
danielk19772b444852004-06-29 07:45:33 +0000632 pParse->rc = rc;
drhf57b3392001-10-08 13:22:32 +0000633 return;
634 }
635 }
636 }
637
638 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000639 ** index or table name in the same database. Issue an error message if
640 ** it does.
drhf57b3392001-10-08 13:22:32 +0000641 */
danielk19778a414492004-06-29 08:59:35 +0000642 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19773df6b252004-05-29 10:23:19 +0000643 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
644 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000645 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000646 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000647 return;
drh75897232000-05-29 14:26:00 +0000648 }
danielk19778a414492004-06-29 08:59:35 +0000649 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
650 ( iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000651 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000652 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000653 return;
654 }
655 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000656 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000657 pParse->rc = SQLITE_NOMEM;
658 pParse->nErr++;
drh6d4abfb2001-10-22 02:58:08 +0000659 sqliteFree(zName);
660 return;
661 }
drh75897232000-05-29 14:26:00 +0000662 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000663 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000664 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000665 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000666 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000667 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000668 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000669 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000670
671 /* Begin generating the code that will insert the table record into
672 ** the SQLITE_MASTER table. Note in particular that we must go ahead
673 ** and allocate the record number for the table entry now. Before any
674 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
675 ** indices to be created and the table record must come before the
676 ** indices. Hence, the record number for the table must be allocated
677 ** now.
678 */
danielk19774adee202004-05-08 08:23:19 +0000679 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000680 sqlite3BeginWriteOperation(pParse, 0, iDb);
danielk1977d008cfe2004-06-19 02:22:10 +0000681 /* Every time a new table is created the file-format
682 ** and encoding meta-values are set in the database, in
683 ** case this is the first table created.
684 */
685 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
686 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
687 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
688 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
689
danielk1977cbb18d22004-05-28 11:37:27 +0000690 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000691 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
692 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000693 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000694 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000695 }
drh75897232000-05-29 14:26:00 +0000696}
697
698/*
699** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000700**
701** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000702** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000703** first to get things going. Then this routine is called for each
704** column.
drh75897232000-05-29 14:26:00 +0000705*/
danielk19774adee202004-05-08 08:23:19 +0000706void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000707 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000708 int i;
drha99db3b2004-06-19 14:49:12 +0000709 char *z;
drhc9b84a12002-06-20 11:36:48 +0000710 Column *pCol;
drh75897232000-05-29 14:26:00 +0000711 if( (p = pParse->pNewTable)==0 ) return;
drha99db3b2004-06-19 14:49:12 +0000712 z = sqlite3NameFromToken(pName);
drh97fc3d02002-05-22 21:27:03 +0000713 if( z==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000714 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000715 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
716 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000717 sqliteFree(z);
718 return;
719 }
720 }
drh75897232000-05-29 14:26:00 +0000721 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000722 Column *aNew;
723 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
724 if( aNew==0 ) return;
725 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000726 }
drhc9b84a12002-06-20 11:36:48 +0000727 pCol = &p->aCol[p->nCol];
728 memset(pCol, 0, sizeof(p->aCol[0]));
729 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000730
731 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000732 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
733 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000734 */
danielk19774f057f92004-06-08 00:02:33 +0000735 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000736 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000737 p->nCol++;
drh75897232000-05-29 14:26:00 +0000738}
739
740/*
drh382c0242001-10-06 16:33:02 +0000741** This routine is called by the parser while in the middle of
742** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
743** been seen on a column. This routine sets the notNull flag on
744** the column currently under construction.
745*/
danielk19774adee202004-05-08 08:23:19 +0000746void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000747 Table *p;
748 int i;
749 if( (p = pParse->pNewTable)==0 ) return;
750 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000751 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000752}
753
754/*
755** This routine is called by the parser while in the middle of
756** parsing a CREATE TABLE statement. The pFirst token is the first
757** token in the sequence of tokens that describe the type of the
758** column currently under construction. pLast is the last token
759** in the sequence. Use this information to construct a string
760** that contains the typename of the column and store that string
761** in zType.
762*/
danielk19774adee202004-05-08 08:23:19 +0000763void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000764 Table *p;
765 int i, j;
766 int n;
767 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000768 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000769 if( (p = pParse->pNewTable)==0 ) return;
770 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000771 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000772 pCol = &p->aCol[i];
773 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000774 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000775 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000776 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000777 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000778 for(i=j=0; z[i]; i++){
779 int c = z[i];
780 if( isspace(c) ) continue;
781 z[j++] = c;
782 }
783 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000784 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000785}
786
787/*
drh7020f652000-06-03 18:06:52 +0000788** The given token is the default value for the last column added to
789** the table currently under construction. If "minusFlag" is true, it
790** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000791**
792** This routine is called by the parser while in the middle of
793** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000794*/
danielk19774adee202004-05-08 08:23:19 +0000795void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000796 Table *p;
797 int i;
798 char **pz;
799 if( (p = pParse->pNewTable)==0 ) return;
800 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000801 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000802 pz = &p->aCol[i].zDflt;
803 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000804 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000805 }else{
danielk19774adee202004-05-08 08:23:19 +0000806 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000807 }
danielk19774adee202004-05-08 08:23:19 +0000808 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000809}
810
811/*
drh4a324312001-12-21 14:30:42 +0000812** Designate the PRIMARY KEY for the table. pList is a list of names
813** of columns that form the primary key. If pList is NULL, then the
814** most recently added column of the table is the primary key.
815**
816** A table can have at most one primary key. If the table already has
817** a primary key (and this is the second primary key) then create an
818** error.
819**
820** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
821** then we will try to use that column as the row id. (Exception:
822** For backwards compatibility with older databases, do not do this
823** if the file format version number is less than 1.) Set the Table.iPKey
824** field of the table under construction to be the index of the
825** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
826** no INTEGER PRIMARY KEY.
827**
828** If the key is not an INTEGER PRIMARY KEY, then create a unique
829** index for the key. No index is created for INTEGER PRIMARY KEYs.
830*/
danielk19770202b292004-06-09 09:55:16 +0000831void sqlite3AddPrimaryKey(Parse *pParse, ExprList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000832 Table *pTab = pParse->pNewTable;
833 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000834 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000835 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000836 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000837 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000838 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000839 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000840 }
841 pTab->hasPrimKey = 1;
842 if( pList==0 ){
843 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000844 pTab->aCol[iCol].isPrimKey = 1;
845 }else{
danielk19770202b292004-06-09 09:55:16 +0000846 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000847 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000848 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
849 break;
850 }
drh78100cc2003-08-23 22:40:53 +0000851 }
852 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000853 }
danielk19770202b292004-06-09 09:55:16 +0000854 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000855 }
856 if( iCol>=0 && iCol<pTab->nCol ){
857 zType = pTab->aCol[iCol].zType;
858 }
danielk19773d68f032004-05-11 07:11:51 +0000859 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000860 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000861 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000862 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000863 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000864 pList = 0;
drh4a324312001-12-21 14:30:42 +0000865 }
drhe0194f22003-02-26 13:52:51 +0000866
867primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000868 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000869 return;
drh4a324312001-12-21 14:30:42 +0000870}
871
872/*
drhd3d39e92004-05-20 22:16:29 +0000873** Set the collation function of the most recently parsed table column
874** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000875*/
drhd3d39e92004-05-20 22:16:29 +0000876void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000877 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000878 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000879 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000880 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000881
drhd3d39e92004-05-20 22:16:29 +0000882 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000883 i = p->nCol-1;
884
885 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
886 p->aCol[i].pColl = pColl;
887
888 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
889 ** then an index may have been created on this column before the
890 ** collation type was added. Correct this if it is the case.
891 */
892 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
893 assert( pIdx->nColumn==1 );
894 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000895 }
896}
897
898/*
danielk19770202b292004-06-09 09:55:16 +0000899** Locate and return an entry from the db.aCollSeq hash table. If the entry
900** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +0000901** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +0000902**
danielk1977466be562004-06-10 02:16:01 +0000903** Each pointer stored in the sqlite3.aCollSeq hash table contains an
904** array of three CollSeq structures. The first is the collation sequence
905** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +0000906**
danielk1977466be562004-06-10 02:16:01 +0000907** Stored immediately after the three collation sequences is a copy of
908** the collation sequence name. A pointer to this string is stored in
909** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +0000910*/
danielk1977466be562004-06-10 02:16:01 +0000911static CollSeq * findCollSeqEntry(
912 sqlite *db,
913 const char *zName,
danielk19770202b292004-06-09 09:55:16 +0000914 int nName,
915 int create
drhd3d39e92004-05-20 22:16:29 +0000916){
917 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000918 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000919 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +0000920
danielk19770202b292004-06-09 09:55:16 +0000921 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +0000922 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +0000923 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000924 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000925 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +0000926 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000927 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +0000928 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000929 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +0000930 memcpy(pColl[0].zName, zName, nName);
931 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +0000932 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +0000933 }
drhd3d39e92004-05-20 22:16:29 +0000934 }
drhd3d39e92004-05-20 22:16:29 +0000935 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000936}
937
danielk1977466be562004-06-10 02:16:01 +0000938/*
939** Parameter zName points to a UTF-8 encoded string nName bytes long.
940** Return the CollSeq* pointer for the collation sequence named zName
941** for the encoding 'enc' from the database 'db'.
942**
943** If the entry specified is not found and 'create' is true, then create a
944** new entry. Otherwise return NULL.
945*/
946CollSeq *sqlite3FindCollSeq(
947 sqlite *db,
948 u8 enc,
949 const char *zName,
950 int nName,
951 int create
952){
953 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
954 if( pColl ) switch( enc ){
danielk1977dc8453f2004-06-12 00:42:34 +0000955 case SQLITE_UTF8:
danielk1977466be562004-06-10 02:16:01 +0000956 break;
danielk1977dc8453f2004-06-12 00:42:34 +0000957 case SQLITE_UTF16LE:
danielk19774e6af132004-06-10 14:01:08 +0000958 pColl = &pColl[1];
danielk1977466be562004-06-10 02:16:01 +0000959 break;
danielk1977dc8453f2004-06-12 00:42:34 +0000960 case SQLITE_UTF16BE:
danielk19774e6af132004-06-10 14:01:08 +0000961 pColl = &pColl[2];
danielk1977466be562004-06-10 02:16:01 +0000962 break;
963 default:
964 assert(!"Cannot happen");
965 }
966 return pColl;
967}
968
danielk1977e159fdf2004-06-21 10:45:06 +0000969/*
970** Invoke the 'collation needed' callback to request a collation sequence
971** in the database text encoding of name zName, length nName.
972** If the collation sequence
973*/
danielk19777cedc8d2004-06-10 10:50:08 +0000974static void callCollNeeded(sqlite *db, const char *zName, int nName){
danielk19777cedc8d2004-06-10 10:50:08 +0000975 assert( !db->xCollNeeded || !db->xCollNeeded16 );
976 if( nName<0 ) nName = strlen(zName);
977 if( db->xCollNeeded ){
danielk19778a6c5502004-06-22 12:18:32 +0000978 char *zExternal = sqliteStrNDup(zName, nName);
danielk19777cedc8d2004-06-10 10:50:08 +0000979 if( !zExternal ) return;
danielk1977e159fdf2004-06-21 10:45:06 +0000980 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
981 sqliteFree(zExternal);
danielk19777cedc8d2004-06-10 10:50:08 +0000982 }
983 if( db->xCollNeeded16 ){
danielk19778a6c5502004-06-22 12:18:32 +0000984 char const *zExternal;
danielk1977bfd6cce2004-06-18 04:24:54 +0000985 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
986 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
987 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +0000988 if( !zExternal ) return;
989 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
990 }
danielk19777cedc8d2004-06-10 10:50:08 +0000991}
992
danielk1977e159fdf2004-06-21 10:45:06 +0000993/*
994** This routine is called if the collation factory fails to deliver a
995** collation function in the best encoding but there may be other versions
996** of this collation function (for other text encodings) available. Use one
997** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
998** possible.
999*/
danielk19777cedc8d2004-06-10 10:50:08 +00001000static int synthCollSeq(Parse *pParse, CollSeq *pColl){
drhda71ce12004-06-21 18:14:45 +00001001 CollSeq *pColl2;
danielk19777cedc8d2004-06-10 10:50:08 +00001002 char *z = pColl->zName;
1003 int n = strlen(z);
drhda71ce12004-06-21 18:14:45 +00001004 sqlite *db = pParse->db;
1005 int i;
1006 static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
1007 for(i=0; i<3; i++){
1008 pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, n, 0);
1009 if( pColl2->xCmp!=0 ){
1010 memcpy(pColl, pColl2, sizeof(CollSeq));
1011 return SQLITE_OK;
danielk19777cedc8d2004-06-10 10:50:08 +00001012 }
danielk19777cedc8d2004-06-10 10:50:08 +00001013 }
drhda71ce12004-06-21 18:14:45 +00001014 if( pParse->nErr==0 ){
1015 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ",
1016 -1, z, n, 0);
1017 }
1018 pParse->nErr++;
1019 return SQLITE_ERROR;
danielk19777cedc8d2004-06-10 10:50:08 +00001020}
1021
1022/*
1023** This routine is called on a collation sequence before it is used to
1024** check that it is defined. An undefined collation sequence exists when
1025** a database is loaded that contains references to collation sequences
1026** that have not been defined by sqlite3_create_collation() etc.
1027**
1028** If required, this routine calls the 'collation needed' callback to
1029** request a definition of the collating sequence. If this doesn't work,
1030** an equivalent collating sequence that uses a text encoding different
1031** from the main database is substituted, if one is available.
1032*/
1033int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1034 if( pColl && !pColl->xCmp ){
danielk1977e159fdf2004-06-21 10:45:06 +00001035 /* No collation sequence of this type for this encoding is registered.
1036 ** Call the collation factory to see if it can supply us with one.
1037 */
danielk19777cedc8d2004-06-10 10:50:08 +00001038 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1039 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1040 return SQLITE_ERROR;
1041 }
1042 }
1043 return SQLITE_OK;
1044}
1045
drhda71ce12004-06-21 18:14:45 +00001046/*
1047** Call sqlite3CheckCollSeq() for all collating sequences in an index,
1048** in order to verify that all the necessary collating sequences are
1049** loaded.
1050*/
danielk19777cedc8d2004-06-10 10:50:08 +00001051int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1052 if( pIdx ){
1053 int i;
1054 for(i=0; i<pIdx->nColumn; i++){
1055 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1056 return SQLITE_ERROR;
1057 }
1058 }
1059 }
1060 return SQLITE_OK;
1061}
1062
danielk1977466be562004-06-10 02:16:01 +00001063/*
1064** This function returns the collation sequence for database native text
1065** encoding identified by the string zName, length nName.
1066**
1067** If the requested collation sequence is not available, or not available
1068** in the database native encoding, the collation factory is invoked to
1069** request it. If the collation factory does not supply such a sequence,
1070** and the sequence is available in another text encoding, then that is
1071** returned instead.
1072**
1073** If no versions of the requested collations sequence are available, or
1074** another error occurs, NULL is returned and an error message written into
1075** pParse.
1076*/
danielk19770202b292004-06-09 09:55:16 +00001077CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001078 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001079 u8 initbusy = pParse->db->init.busy;
1080 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
1081 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001082 /* No collation sequence of this type for this encoding is registered.
1083 ** Call the collation factory to see if it can supply us with one.
1084 */
danielk19777cedc8d2004-06-10 10:50:08 +00001085 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001086 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001087 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001088 /* There may be a version of the collation sequence that requires
1089 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001090 */
danielk19777cedc8d2004-06-10 10:50:08 +00001091 if( synthCollSeq(pParse, pColl) ){
1092 return 0;
danielk1977466be562004-06-10 02:16:01 +00001093 }
1094 }
1095 }
1096
1097 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001098 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001099 if( pParse->nErr==0 ){
danielk1977466be562004-06-10 02:16:01 +00001100 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ", -1,
danielk19770202b292004-06-09 09:55:16 +00001101 zName, nName, 0);
1102 }
1103 pParse->nErr++;
danielk19777cedc8d2004-06-10 10:50:08 +00001104 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001105 }
1106 return pColl;
1107}
1108
1109
1110
danielk1977a37cdde2004-05-16 11:15:36 +00001111/*
drh1ad3b9e2004-05-20 12:10:20 +00001112** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +00001113** associated affinity type.
1114*/
1115char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +00001116 int n, i;
1117 struct {
drh1ad3b9e2004-05-20 12:10:20 +00001118 const char *zSub; /* Keywords substring to search for */
drhda71ce12004-06-21 18:14:45 +00001119 char nSub; /* length of zSub */
drh1ad3b9e2004-05-20 12:10:20 +00001120 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +00001121 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +00001122 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +00001123 {"CHAR", 4, SQLITE_AFF_TEXT},
1124 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +00001125 {"TEXT", 4, SQLITE_AFF_TEXT},
1126 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +00001127 };
1128
drh9c054832004-05-31 18:51:57 +00001129 if( nType==0 ){
1130 return SQLITE_AFF_NONE;
1131 }
drh1ad3b9e2004-05-20 12:10:20 +00001132 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
1133 int c1 = substrings[i].zSub[0];
1134 int c2 = tolower(c1);
1135 int limit = nType - substrings[i].nSub;
1136 const char *z = substrings[i].zSub;
1137 for(n=0; n<=limit; n++){
1138 int c = zType[n];
1139 if( (c==c1 || c==c2)
1140 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +00001141 return substrings[i].affinity;
1142 }
1143 }
1144 }
drh1ad3b9e2004-05-20 12:10:20 +00001145 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +00001146}
1147
1148/*
drh50e5dad2001-09-15 00:57:28 +00001149** Come up with a new random value for the schema cookie. Make sure
1150** the new value is different from the old.
1151**
1152** The schema cookie is used to determine when the schema for the
1153** database changes. After each schema change, the cookie value
1154** changes. When a process first reads the schema it records the
1155** cookie. Thereafter, whenever it goes to access the database,
1156** it checks the cookie to make sure the schema has not changed
1157** since it was last read.
1158**
1159** This plan is not completely bullet-proof. It is possible for
1160** the schema to change multiple times and for the cookie to be
1161** set back to prior value. But schema changes are infrequent
1162** and the probability of hitting the same cookie value is only
1163** 1 chance in 2^32. So we're safe enough.
1164*/
danielk1977cbb18d22004-05-28 11:37:27 +00001165void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00001166 unsigned char r;
1167 int *pSchemaCookie = &(db->aDb[iDb].schema_cookie);
1168
1169 sqlite3Randomness(1, &r);
1170 *pSchemaCookie = *pSchemaCookie + r + 1;
1171 db->flags |= SQLITE_InternChanges;
1172 sqlite3VdbeAddOp(v, OP_Integer, *pSchemaCookie, 0);
1173 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001174}
1175
1176/*
drh969fa7c2002-02-18 18:30:32 +00001177** Measure the number of characters needed to output the given
1178** identifier. The number returned includes any quotes used
1179** but does not include the null terminator.
1180*/
1181static int identLength(const char *z){
1182 int n;
drh17f71932002-02-21 12:01:27 +00001183 int needQuote = 0;
1184 for(n=0; *z; n++, z++){
1185 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +00001186 }
drh17f71932002-02-21 12:01:27 +00001187 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +00001188}
1189
1190/*
1191** Write an identifier onto the end of the given string. Add
1192** quote characters as needed.
1193*/
1194static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +00001195 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001196 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001197 for(j=0; zIdent[j]; j++){
1198 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1199 }
1200 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001201 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh17f71932002-02-21 12:01:27 +00001202 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +00001203 for(j=0; zIdent[j]; j++){
1204 z[i++] = zIdent[j];
1205 if( zIdent[j]=='\'' ) z[i++] = '\'';
1206 }
drh17f71932002-02-21 12:01:27 +00001207 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +00001208 z[i] = 0;
1209 *pIdx = i;
1210}
1211
1212/*
1213** Generate a CREATE TABLE statement appropriate for the given
1214** table. Memory to hold the text of the statement is obtained
1215** from sqliteMalloc() and must be freed by the calling function.
1216*/
1217static char *createTableStmt(Table *p){
1218 int i, k, n;
1219 char *zStmt;
1220 char *zSep, *zSep2, *zEnd;
1221 n = 0;
1222 for(i=0; i<p->nCol; i++){
1223 n += identLength(p->aCol[i].zName);
danielk1977517eb642004-06-07 10:00:31 +00001224 if( p->aCol[i].zType ){
1225 n += (strlen(p->aCol[i].zType) + 1);
1226 }
drh969fa7c2002-02-18 18:30:32 +00001227 }
1228 n += identLength(p->zName);
1229 if( n<40 ){
1230 zSep = "";
1231 zSep2 = ",";
1232 zEnd = ")";
1233 }else{
1234 zSep = "\n ";
1235 zSep2 = ",\n ";
1236 zEnd = "\n)";
1237 }
drhe0bc4042002-06-25 01:09:11 +00001238 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001239 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001240 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001241 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001242 k = strlen(zStmt);
1243 identPut(zStmt, &k, p->zName);
1244 zStmt[k++] = '(';
1245 for(i=0; i<p->nCol; i++){
1246 strcpy(&zStmt[k], zSep);
1247 k += strlen(&zStmt[k]);
1248 zSep = zSep2;
1249 identPut(zStmt, &k, p->aCol[i].zName);
danielk1977517eb642004-06-07 10:00:31 +00001250 if( p->aCol[i].zType ){
1251 zStmt[k++] = ' ';
1252 strcpy(&zStmt[k], p->aCol[i].zType);
1253 k += strlen(p->aCol[i].zType);
1254 }
drh969fa7c2002-02-18 18:30:32 +00001255 }
1256 strcpy(&zStmt[k], zEnd);
1257 return zStmt;
1258}
1259
1260/*
drh75897232000-05-29 14:26:00 +00001261** This routine is called to report the final ")" that terminates
1262** a CREATE TABLE statement.
1263**
drhf57b3392001-10-08 13:22:32 +00001264** The table structure that other action routines have been building
1265** is added to the internal hash tables, assuming no errors have
1266** occurred.
drh75897232000-05-29 14:26:00 +00001267**
drh1d85d932004-02-14 23:05:52 +00001268** An entry for the table is made in the master table on disk, unless
1269** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001270** it means we are reading the sqlite_master table because we just
1271** connected to the database or because the sqlite_master table has
1272** recently changes, so the entry for this table already exists in
1273** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001274**
1275** If the pSelect argument is not NULL, it means that this routine
1276** was called to create a table generated from a
1277** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1278** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001279*/
danielk19774adee202004-05-08 08:23:19 +00001280void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001281 Table *p;
drhbe0072d2001-09-13 14:46:09 +00001282 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001283
danielk197724b03fd2004-05-10 10:34:34 +00001284 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001285 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001286 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001287
danielk1977517eb642004-06-07 10:00:31 +00001288 assert( !db->init.busy || !pSelect );
1289
drh1d85d932004-02-14 23:05:52 +00001290 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001291 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1292 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001293 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001294 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001295 */
drh1d85d932004-02-14 23:05:52 +00001296 if( db->init.busy ){
1297 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001298 }
1299
drhe3c41372001-09-17 20:25:58 +00001300 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001301 ** in the SQLITE_MASTER table of the database. The record number
1302 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001303 **
drhe0bc4042002-06-25 01:09:11 +00001304 ** If this is a TEMPORARY table, write the entry into the auxiliary
1305 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001306 */
drh1d85d932004-02-14 23:05:52 +00001307 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001308 int n;
drhd8bc7082000-06-07 23:51:50 +00001309 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001310
danielk19774adee202004-05-08 08:23:19 +00001311 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001312 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001313
drh4ff6dfa2002-03-03 23:06:00 +00001314 if( p->pSelect==0 ){
1315 /* A regular table */
danielk19774adee202004-05-08 08:23:19 +00001316 sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
drh4ff6dfa2002-03-03 23:06:00 +00001317 }else{
1318 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001319 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001320 }
drh969fa7c2002-02-18 18:30:32 +00001321 p->tnum = 0;
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);
danielk1977517eb642004-06-07 10:00:31 +00001382
danielk19774adee202004-05-08 08:23:19 +00001383 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001384 }
drh17e9e292003-02-01 13:53:28 +00001385
1386 /* Add the table to the in-memory representation of the database.
1387 */
drhd24cc422003-03-27 12:51:24 +00001388 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001389 Table *pOld;
1390 FKey *pFKey;
danielk19774adee202004-05-08 08:23:19 +00001391 pOld = sqlite3HashInsert(&db->aDb[p->iDb].tblHash,
drhd24cc422003-03-27 12:51:24 +00001392 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001393 if( pOld ){
1394 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1395 return;
1396 }
1397 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1398 int nTo = strlen(pFKey->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +00001399 pFKey->pNextTo = sqlite3HashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
1400 sqlite3HashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001401 }
1402 pParse->pNewTable = 0;
1403 db->nTable++;
1404 db->flags |= SQLITE_InternChanges;
1405 }
drh75897232000-05-29 14:26:00 +00001406}
1407
1408/*
drha76b5df2002-02-23 02:32:10 +00001409** The parser calls this routine in order to create a new VIEW
1410*/
danielk19774adee202004-05-08 08:23:19 +00001411void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001412 Parse *pParse, /* The parsing context */
1413 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001414 Token *pName1, /* The token that holds the name of the view */
1415 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001416 Select *pSelect, /* A SELECT statement that will become the new view */
1417 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001418){
drha76b5df2002-02-23 02:32:10 +00001419 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001420 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001421 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001422 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001423 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001424 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001425
danielk197748dec7e2004-05-28 12:33:30 +00001426 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001427 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001428 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001429 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001430 return;
1431 }
danielk1977ef2cb632004-05-29 02:37:19 +00001432 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001433 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1434 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001435 ){
danielk19774adee202004-05-08 08:23:19 +00001436 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001437 return;
1438 }
drh174b6192002-12-03 02:22:52 +00001439
drh4b59ab52002-08-24 18:24:51 +00001440 /* Make a copy of the entire SELECT statement that defines the view.
1441 ** This will force all the Expr.token.z values to be dynamically
1442 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001443 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001444 */
danielk19774adee202004-05-08 08:23:19 +00001445 p->pSelect = sqlite3SelectDup(pSelect);
1446 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001447 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001449 }
drh4b59ab52002-08-24 18:24:51 +00001450
1451 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1452 ** the end.
1453 */
drha76b5df2002-02-23 02:32:10 +00001454 sEnd = pParse->sLastToken;
1455 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1456 sEnd.z += sEnd.n;
1457 }
1458 sEnd.n = 0;
drhb089c0b2004-06-26 14:46:39 +00001459 n = sEnd.z - pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001460 z = pBegin->z;
1461 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1462 sEnd.z = &z[n-1];
1463 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001464
danielk19774adee202004-05-08 08:23:19 +00001465 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1466 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001467 return;
drh417be792002-03-03 18:59:40 +00001468}
drha76b5df2002-02-23 02:32:10 +00001469
drh417be792002-03-03 18:59:40 +00001470/*
1471** The Table structure pTable is really a VIEW. Fill in the names of
1472** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001473** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001474*/
danielk19774adee202004-05-08 08:23:19 +00001475int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001476 ExprList *pEList;
1477 Select *pSel;
1478 Table *pSelTab;
1479 int nErr = 0;
1480
1481 assert( pTable );
1482
1483 /* A positive nCol means the columns names for this view are
1484 ** already known.
1485 */
1486 if( pTable->nCol>0 ) return 0;
1487
1488 /* A negative nCol is a special marker meaning that we are currently
1489 ** trying to compute the column names. If we enter this routine with
1490 ** a negative nCol, it means two or more views form a loop, like this:
1491 **
1492 ** CREATE VIEW one AS SELECT * FROM two;
1493 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001494 **
1495 ** Actually, this error is caught previously and so the following test
1496 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001497 */
1498 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001499 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001500 return 1;
1501 }
1502
1503 /* If we get this far, it means we need to compute the table names.
1504 */
1505 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1506 pSel = pTable->pSelect;
1507
danielk19774adee202004-05-08 08:23:19 +00001508 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001509 ** "*" elements in this list. But we will need to restore the list
1510 ** back to its original configuration afterwards, so we save a copy of
1511 ** the original in pEList.
1512 */
1513 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001514 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001515 if( pSel->pEList==0 ){
1516 pSel->pEList = pEList;
1517 return 1; /* Malloc failed */
1518 }
1519 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001520 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001521 if( pSelTab ){
1522 assert( pTable->aCol==0 );
1523 pTable->nCol = pSelTab->nCol;
1524 pTable->aCol = pSelTab->aCol;
1525 pSelTab->nCol = 0;
1526 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001527 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001528 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001529 }else{
1530 pTable->nCol = 0;
1531 nErr++;
1532 }
danielk19774adee202004-05-08 08:23:19 +00001533 sqlite3SelectUnbind(pSel);
1534 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001535 pSel->pEList = pEList;
1536 return nErr;
1537}
1538
1539/*
1540** Clear the column names from the VIEW pTable.
1541**
1542** This routine is called whenever any other table or view is modified.
1543** The view passed into this routine might depend directly or indirectly
1544** on the modified or deleted table so we need to clear the old column
1545** names so that they will be recomputed.
1546*/
1547static void sqliteViewResetColumnNames(Table *pTable){
1548 int i;
drh701a0ae2004-02-22 20:05:00 +00001549 Column *pCol;
1550 assert( pTable!=0 && pTable->pSelect!=0 );
1551 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1552 sqliteFree(pCol->zName);
1553 sqliteFree(pCol->zDflt);
1554 sqliteFree(pCol->zType);
drh417be792002-03-03 18:59:40 +00001555 }
1556 sqliteFree(pTable->aCol);
1557 pTable->aCol = 0;
1558 pTable->nCol = 0;
1559}
1560
1561/*
drh8bf8dc92003-05-17 17:35:10 +00001562** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001563*/
drhd24cc422003-03-27 12:51:24 +00001564static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001565 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001566 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001567 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001568 Table *pTab = sqliteHashData(i);
1569 if( pTab->pSelect ){
1570 sqliteViewResetColumnNames(pTab);
1571 }
1572 }
drh8bf8dc92003-05-17 17:35:10 +00001573 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001574}
1575
danielk19778a414492004-06-29 08:59:35 +00001576#if 0
drha76b5df2002-02-23 02:32:10 +00001577/*
drh75897232000-05-29 14:26:00 +00001578** Given a token, look up a table with that name. If not found, leave
1579** an error for the parser to find and return NULL.
1580*/
danielk19774adee202004-05-08 08:23:19 +00001581Table *sqlite3TableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001582 char *zName;
1583 Table *pTab;
drha99db3b2004-06-19 14:49:12 +00001584 zName = sqlite3NameFromToken(pTok);
drhdaffd0e2001-04-11 14:28:42 +00001585 if( zName==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001586 pTab = sqlite3FindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001587 sqliteFree(zName);
1588 if( pTab==0 ){
danielk19774adee202004-05-08 08:23:19 +00001589 sqlite3ErrorMsg(pParse, "no such table: %T", pTok);
drha6ecd332004-06-10 00:29:09 +00001590 pParse->checkSchema = 1;
drh75897232000-05-29 14:26:00 +00001591 }
1592 return pTab;
1593}
danielk19778a414492004-06-29 08:59:35 +00001594#endif
drh75897232000-05-29 14:26:00 +00001595
1596/*
1597** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001598** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001599*/
danielk1977a8858102004-05-28 12:11:21 +00001600void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1601 Table *pTab;
drh75897232000-05-29 14:26:00 +00001602 Vdbe *v;
1603 int base;
drh5edc3122001-09-13 21:53:09 +00001604 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001605 int iDb;
drh75897232000-05-29 14:26:00 +00001606
danielk1977a8858102004-05-28 12:11:21 +00001607 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1608 assert( pName->nSrc==1 );
1609 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1610
1611 if( pTab==0 ) goto exit_drop_table;
1612 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001613 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001614#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001615 {
1616 int code;
danielk1977a8858102004-05-28 12:11:21 +00001617 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1618 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001619 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001620 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001621 }
drhe5f9c642003-01-13 23:27:31 +00001622 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001623 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001624 code = SQLITE_DROP_TEMP_VIEW;
1625 }else{
1626 code = SQLITE_DROP_VIEW;
1627 }
1628 }else{
drhd24cc422003-03-27 12:51:24 +00001629 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001630 code = SQLITE_DROP_TEMP_TABLE;
1631 }else{
1632 code = SQLITE_DROP_TABLE;
1633 }
1634 }
danielk1977a8858102004-05-28 12:11:21 +00001635 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1636 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001637 }
danielk1977a8858102004-05-28 12:11:21 +00001638 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1639 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001640 }
drhe5f9c642003-01-13 23:27:31 +00001641 }
1642#endif
danielk1977a8858102004-05-28 12:11:21 +00001643 if( pTab->readOnly ){
1644 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001645 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001646 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001647 }
danielk1977a8858102004-05-28 12:11:21 +00001648 if( isView && pTab->pSelect==0 ){
1649 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1650 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001651 }
danielk1977a8858102004-05-28 12:11:21 +00001652 if( !isView && pTab->pSelect ){
1653 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1654 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001655 }
drh75897232000-05-29 14:26:00 +00001656
drh1ccde152000-06-17 13:12:39 +00001657 /* Generate code to remove the table from the master table
1658 ** on disk.
1659 */
danielk19774adee202004-05-08 08:23:19 +00001660 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001661 if( v ){
drh905793e2004-02-21 13:31:09 +00001662 static VdbeOpList dropTable[] = {
danielk19778e227872004-06-07 07:52:17 +00001663 { OP_Rewind, 0, ADDR(13), 0},
1664 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001665 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001666 { OP_MemLoad, 1, 0, 0}, /* 3 */
danielk19778e227872004-06-07 07:52:17 +00001667 { OP_Column, 0, 2, 0}, /* sqlite_master.tbl_name */
1668 { OP_Ne, 0, ADDR(12), 0},
1669 { OP_String8, 0, 0, "trigger"},
1670 { OP_Column, 0, 2, 0}, /* sqlite_master.type */
1671 { OP_Eq, 0, ADDR(12), 0},
drh75897232000-05-29 14:26:00 +00001672 { OP_Delete, 0, 0, 0},
danielk19778e227872004-06-07 07:52:17 +00001673 { OP_Rewind, 0, ADDR(13), 0},
danielk19778d059842004-05-12 11:24:02 +00001674 { OP_Goto, 0, ADDR(3), 0},
danielk19778e227872004-06-07 07:52:17 +00001675 { OP_Next, 0, ADDR(3), 0}, /* 12 */
drh75897232000-05-29 14:26:00 +00001676 };
1677 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001678 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001679 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001680
danielk19778e227872004-06-07 07:52:17 +00001681 /* Drop all triggers associated with the table being dropped. Code
1682 ** is generated to remove entries from sqlite_master and/or
1683 ** sqlite_temp_master if required.
1684 */
danielk1977a8858102004-05-28 12:11:21 +00001685 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001686 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001687 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001688 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drhe0bc4042002-06-25 01:09:11 +00001689 if( pParse->explain ){
1690 pTrigger = pTrigger->pNext;
1691 }else{
danielk1977a8858102004-05-28 12:11:21 +00001692 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001693 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001694 }
drh8bf8dc92003-05-17 17:35:10 +00001695
danielk19778e227872004-06-07 07:52:17 +00001696 /* Drop all SQLITE_MASTER table and index entries that refer to the
1697 ** table. The program name loops through the master table and deletes
1698 ** every row that refers to a table of the same name as the one being
1699 ** dropped. Triggers are handled seperately because a trigger can be
1700 ** created in the temp database that refers to a table in another
1701 ** database.
1702 */
danielk1977a8858102004-05-28 12:11:21 +00001703 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001704 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001705 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
danielk19778e227872004-06-07 07:52:17 +00001706 sqlite3ChangeCookie(db, v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001707 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001708 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001709 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1710 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001711 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001712 }
drh5e00f6c2001-09-13 13:46:56 +00001713 }
danielk19774adee202004-05-08 08:23:19 +00001714 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001715 }
1716
drhe0bc4042002-06-25 01:09:11 +00001717 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001718 **
1719 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001720 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001721 */
1722 if( !pParse->explain ){
danielk1977a8858102004-05-28 12:11:21 +00001723 sqliteUnlinkAndDeleteTable(db, pTab);
drh5edc3122001-09-13 21:53:09 +00001724 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001725 }
drhd24cc422003-03-27 12:51:24 +00001726 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001727
1728exit_drop_table:
1729 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001730}
1731
1732/*
drhc2eef3b2002-08-31 18:53:06 +00001733** This routine is called to create a new foreign key on the table
1734** currently under construction. pFromCol determines which columns
1735** in the current table point to the foreign key. If pFromCol==0 then
1736** connect the key to the last column inserted. pTo is the name of
1737** the table referred to. pToCol is a list of tables in the other
1738** pTo table that the foreign key points to. flags contains all
1739** information about the conflict resolution algorithms specified
1740** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1741**
1742** An FKey structure is created and added to the table currently
1743** under construction in the pParse->pNewTable field. The new FKey
1744** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001745** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001746**
1747** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001748** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001749*/
danielk19774adee202004-05-08 08:23:19 +00001750void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001751 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001752 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001753 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001754 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001755 int flags /* Conflict resolution algorithms. */
1756){
1757 Table *p = pParse->pNewTable;
1758 int nByte;
1759 int i;
1760 int nCol;
1761 char *z;
1762 FKey *pFKey = 0;
1763
1764 assert( pTo!=0 );
1765 if( p==0 || pParse->nErr ) goto fk_end;
1766 if( pFromCol==0 ){
1767 int iCol = p->nCol-1;
1768 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001769 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001770 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001771 " should reference only one column of table %T",
1772 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001773 goto fk_end;
1774 }
1775 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001776 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001777 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001778 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001779 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001780 goto fk_end;
1781 }else{
danielk19770202b292004-06-09 09:55:16 +00001782 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001783 }
1784 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1785 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001786 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001787 nByte += strlen(pToCol->a[i].zName) + 1;
1788 }
1789 }
1790 pFKey = sqliteMalloc( nByte );
1791 if( pFKey==0 ) goto fk_end;
1792 pFKey->pFrom = p;
1793 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001794 z = (char*)&pFKey[1];
1795 pFKey->aCol = (struct sColMap*)z;
1796 z += sizeof(struct sColMap)*nCol;
1797 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001798 memcpy(z, pTo->z, pTo->n);
1799 z[pTo->n] = 0;
1800 z += pTo->n+1;
1801 pFKey->pNextTo = 0;
1802 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001803 if( pFromCol==0 ){
1804 pFKey->aCol[0].iFrom = p->nCol-1;
1805 }else{
1806 for(i=0; i<nCol; i++){
1807 int j;
1808 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001809 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001810 pFKey->aCol[i].iFrom = j;
1811 break;
1812 }
1813 }
1814 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001815 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001816 "unknown column \"%s\" in foreign key definition",
1817 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001818 goto fk_end;
1819 }
1820 }
1821 }
1822 if( pToCol ){
1823 for(i=0; i<nCol; i++){
1824 int n = strlen(pToCol->a[i].zName);
1825 pFKey->aCol[i].zCol = z;
1826 memcpy(z, pToCol->a[i].zName, n);
1827 z[n] = 0;
1828 z += n+1;
1829 }
1830 }
1831 pFKey->isDeferred = 0;
1832 pFKey->deleteConf = flags & 0xff;
1833 pFKey->updateConf = (flags >> 8 ) & 0xff;
1834 pFKey->insertConf = (flags >> 16 ) & 0xff;
1835
1836 /* Link the foreign key to the table as the last step.
1837 */
1838 p->pFKey = pFKey;
1839 pFKey = 0;
1840
1841fk_end:
1842 sqliteFree(pFKey);
danielk19770202b292004-06-09 09:55:16 +00001843 sqlite3ExprListDelete(pFromCol);
1844 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001845}
1846
1847/*
1848** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1849** clause is seen as part of a foreign key definition. The isDeferred
1850** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1851** The behavior of the most recently created foreign key is adjusted
1852** accordingly.
1853*/
danielk19774adee202004-05-08 08:23:19 +00001854void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001855 Table *pTab;
1856 FKey *pFKey;
1857 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1858 pFKey->isDeferred = isDeferred;
1859}
1860
1861/*
drh75897232000-05-29 14:26:00 +00001862** Create a new index for an SQL table. pIndex is the name of the index
1863** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001864** be NULL for a primary key or an index that is created to satisfy a
1865** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001866** as the table to be indexed. pParse->pNewTable is a table that is
1867** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001868**
drh382c0242001-10-06 16:33:02 +00001869** pList is a list of columns to be indexed. pList will be NULL if this
1870** is a primary key or unique-constraint on the most recent column added
1871** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001872*/
danielk19774adee202004-05-08 08:23:19 +00001873void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001874 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001875 Token *pName1, /* First part of index name. May be NULL */
1876 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00001877 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
1878 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001879 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001880 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1881 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1882){
danielk1977cbb18d22004-05-28 11:37:27 +00001883 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00001884 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00001885 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001886 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001887 Token nullId; /* Fake token for an empty ID list */
1888 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001889 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001890 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001891
danielk1977cbb18d22004-05-28 11:37:27 +00001892 int iDb; /* Index of the database that is being written */
1893 Token *pName = 0; /* Unqualified name of the index to create */
1894
danielk197724b03fd2004-05-10 10:34:34 +00001895 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001896
drh75897232000-05-29 14:26:00 +00001897 /*
1898 ** Find the table that is to be indexed. Return early if not found.
1899 */
danielk1977cbb18d22004-05-28 11:37:27 +00001900 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001901
1902 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001903 ** to search for the table. 'Fix' the table name to this db
1904 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001905 */
1906 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001907 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001908 if( iDb<0 ) goto exit_create_index;
1909
danielk1977ef2cb632004-05-29 02:37:19 +00001910 /* If the index name was unqualified, check if the the table
1911 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001912 */
danielk1977ef2cb632004-05-29 02:37:19 +00001913 pTab = sqlite3SrcListLookup(pParse, pTblName);
1914 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1915 iDb = 1;
1916 }
1917
1918 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1919 sqlite3FixSrcList(&sFix, pTblName)
1920 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001921 goto exit_create_index;
1922 }
danielk1977ef2cb632004-05-29 02:37:19 +00001923 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1924 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001925 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001926 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001927 }else{
drhe3c41372001-09-17 20:25:58 +00001928 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001929 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001930 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001931 }
danielk1977cbb18d22004-05-28 11:37:27 +00001932
drh75897232000-05-29 14:26:00 +00001933 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001934 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001935 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001936 goto exit_create_index;
1937 }
drha76b5df2002-02-23 02:32:10 +00001938 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001939 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001940 goto exit_create_index;
1941 }
drh4925ca02003-11-27 00:48:57 +00001942 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001943
1944 /*
1945 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001946 ** index or table with the same name.
1947 **
1948 ** Exception: If we are reading the names of permanent indices from the
1949 ** sqlite_master table (because some other process changed the schema) and
1950 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001951 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001952 **
1953 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001954 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1955 ** own name.
drh75897232000-05-29 14:26:00 +00001956 */
danielk1977d8123362004-06-12 09:25:12 +00001957 if( pName ){
drha99db3b2004-06-19 14:49:12 +00001958 zName = sqlite3NameFromToken(pName);
danielk19778a414492004-06-29 08:59:35 +00001959 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001960 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001961 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00001962 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001963 }
danielk1977d8123362004-06-12 09:25:12 +00001964 if( !db->init.busy ){
1965 Index *pISameName; /* Another index with the same name */
1966 Table *pTSameName; /* A table with same name as the index */
danielk19778a414492004-06-29 08:59:35 +00001967 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001968 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
1969 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
1970 goto exit_create_index;
1971 }
1972 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1973 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
1974 goto exit_create_index;
1975 }
drhe3c41372001-09-17 20:25:58 +00001976 }
drhd24cc422003-03-27 12:51:24 +00001977 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001978 char zBuf[30];
1979 int n;
1980 Index *pLoop;
1981 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00001982 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00001983 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00001984 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001985 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00001986 }
1987
drhe5f9c642003-01-13 23:27:31 +00001988 /* Check for authorization to create an index.
1989 */
1990#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001991 {
1992 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001993 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001994 goto exit_create_index;
1995 }
1996 i = SQLITE_CREATE_INDEX;
1997 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001998 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001999 goto exit_create_index;
2000 }
drhe5f9c642003-01-13 23:27:31 +00002001 }
2002#endif
2003
drh75897232000-05-29 14:26:00 +00002004 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00002005 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00002006 ** So create a fake list to simulate this.
2007 */
2008 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00002009 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00002010 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00002011 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00002012 if( pList==0 ) goto exit_create_index;
2013 }
2014
2015 /*
2016 ** Allocate the index structure.
2017 */
drhdcc581c2000-05-30 13:44:19 +00002018 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00002019 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00002020 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00002021 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
2022 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00002023 strcpy(pIndex->zName, zName);
2024 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00002025 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00002026 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00002027 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00002028 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00002029
drh1ccde152000-06-17 13:12:39 +00002030 /* Scan the names of the columns of the table to be indexed and
2031 ** load the column indices into the Index structure. Report an error
2032 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00002033 */
danielk19770202b292004-06-09 09:55:16 +00002034 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00002035 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002036 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002037 }
2038 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002039 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00002040 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002041 goto exit_create_index;
2042 }
drh967e8b72000-06-21 13:59:10 +00002043 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00002044 if( pList->a[i].pExpr ){
2045 assert( pList->a[i].pExpr->pColl );
2046 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
2047 }else{
2048 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
2049 }
2050 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00002051 if( !db->init.busy &&
2052 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2053 ){
2054 goto exit_create_index;
2055 }
drh75897232000-05-29 14:26:00 +00002056 }
danielk19770202b292004-06-09 09:55:16 +00002057 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002058
danielk1977d8123362004-06-12 09:25:12 +00002059 if( pTab==pParse->pNewTable ){
2060 /* This routine has been called to create an automatic index as a
2061 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2062 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2063 ** i.e. one of:
2064 **
2065 ** CREATE TABLE t(x PRIMARY KEY, y);
2066 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2067 **
2068 ** Either way, check to see if the table already has such an index. If
2069 ** so, don't bother creating this one. This only applies to
2070 ** automatically created indices. Users can do as they wish with
2071 ** explicit indices.
2072 */
2073 Index *pIdx;
2074 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2075 int k;
2076 assert( pIdx->onError!=OE_None );
2077 assert( pIdx->autoIndex );
2078 assert( pIndex->onError!=OE_None );
2079
2080 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2081 for(k=0; k<pIdx->nColumn; k++){
2082 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2083 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2084 }
2085 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002086 if( pIdx->onError!=pIndex->onError ){
2087 /* This constraint creates the same index as a previous
2088 ** constraint specified somewhere in the CREATE TABLE statement.
2089 ** However the ON CONFLICT clauses are different. If both this
2090 ** constraint and the previous equivalent constraint have explicit
2091 ** ON CONFLICT clauses this is an error. Otherwise, use the
2092 ** explicitly specified behaviour for the index.
2093 */
2094 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2095 sqlite3ErrorMsg(pParse,
2096 "conflicting ON CONFLICT clauses specified", 0);
2097 }
2098 if( pIdx->onError==OE_Default ){
2099 pIdx->onError = pIndex->onError;
2100 }
2101 }
danielk1977d8123362004-06-12 09:25:12 +00002102 goto exit_create_index;
2103 }
2104 }
2105 }
2106
drh75897232000-05-29 14:26:00 +00002107 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002108 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002109 */
drhd24cc422003-03-27 12:51:24 +00002110 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00002111 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002112 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002113 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002114 if( p ){
2115 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002116 goto exit_create_index;
2117 }
drh5e00f6c2001-09-13 13:46:56 +00002118 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00002119 }
drh9cfcf5d2002-01-29 18:41:24 +00002120
drh1d85d932004-02-14 23:05:52 +00002121 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhd78eeee2001-09-13 16:18:53 +00002122 ** "sqlite_master" table on the disk. So do not write to the disk
drh1d85d932004-02-14 23:05:52 +00002123 ** again. Extract the table number from the db->init.newTnum field.
drhd78eeee2001-09-13 16:18:53 +00002124 */
danielk1977cbb18d22004-05-28 11:37:27 +00002125 if( db->init.busy && pTblName!=0 ){
drh1d85d932004-02-14 23:05:52 +00002126 pIndex->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00002127 }
2128
drh1d85d932004-02-14 23:05:52 +00002129 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002130 ** involves writing the index into the master table and filling in the
2131 ** index with the current table contents.
2132 **
drh1d85d932004-02-14 23:05:52 +00002133 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2134 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002135 ** CREATE INDEX statements are read out of the master table. In
2136 ** the latter case the index already exists on disk, which is why
2137 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002138 **
danielk1977cbb18d22004-05-28 11:37:27 +00002139 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002140 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2141 ** has just been created, it contains no data and the index initialization
2142 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002143 */
drh1d85d932004-02-14 23:05:52 +00002144 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00002145 int n;
drhadbca9c2001-09-27 15:11:53 +00002146 Vdbe *v;
drh75897232000-05-29 14:26:00 +00002147 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00002148
danielk19774adee202004-05-08 08:23:19 +00002149 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002150 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00002151 if( pTblName!=0 ){
2152 sqlite3BeginWriteOperation(pParse, 0, iDb);
2153 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00002154 }
danielk19774adee202004-05-08 08:23:19 +00002155 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00002156 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
2157 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
2158 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002159 sqlite3VdbeOp3(v, OP_CreateIndex, 0, iDb,(char*)&pIndex->tnum,P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00002160 pIndex->tnum = 0;
danielk1977cbb18d22004-05-28 11:37:27 +00002161 if( pTblName ){
drha99db3b2004-06-19 14:49:12 +00002162 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
2163 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002164 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
2165 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00002166 }
danielk19770f69c1e2004-05-29 11:24:50 +00002167 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00002168 if( pStart && pEnd ){
danielk19770202b292004-06-09 09:55:16 +00002169 if( onError==OE_None ){
2170 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
2171 }else{
2172 sqlite3VdbeChangeP3(v, -1, "CREATE UNIQUE INDEX ", P3_STATIC);
2173 }
danielk19770f69c1e2004-05-29 11:24:50 +00002174 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002175 n = Addr(pEnd->z) - Addr(pName->z) + 1;
2176 sqlite3VdbeChangeP3(v, -1, pName->z, n);
danielk197772c952a2004-06-21 09:06:41 +00002177 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drh75897232000-05-29 14:26:00 +00002178 }
danielk197784ac9d02004-05-18 09:58:06 +00002179 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002180 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002181 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00002182 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002183 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
2184 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00002185 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00002186 lbl2 = sqlite3VdbeMakeLabel(v);
2187 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00002188 lbl1 = sqlite3VdbeCurrentAddr(v);
2189 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00002190 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00002191 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002192 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
2193 sqlite3VdbeResolveLabel(v, lbl2);
2194 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
2195 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00002196 }
danielk1977cbb18d22004-05-28 11:37:27 +00002197 if( pTblName!=0 ){
drhc275b4e2004-07-19 17:25:24 +00002198 sqlite3ChangeCookie(db, v, iDb);
danielk19774adee202004-05-08 08:23:19 +00002199 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2200 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002201 }
drh75897232000-05-29 14:26:00 +00002202 }
2203
danielk1977d8123362004-06-12 09:25:12 +00002204 /* When adding an index to the list of indices for a table, make
2205 ** sure all indices labeled OE_Replace come after all those labeled
2206 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2207 ** and INSERT.
2208 */
2209 if( onError!=OE_Replace || pTab->pIndex==0
2210 || pTab->pIndex->onError==OE_Replace){
2211 pIndex->pNext = pTab->pIndex;
2212 pTab->pIndex = pIndex;
2213 }else{
2214 Index *pOther = pTab->pIndex;
2215 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2216 pOther = pOther->pNext;
2217 }
2218 pIndex->pNext = pOther->pNext;
2219 pOther->pNext = pIndex;
2220 }
2221 pIndex = 0;
2222
drh75897232000-05-29 14:26:00 +00002223 /* Clean up before exiting */
2224exit_create_index:
danielk1977d8123362004-06-12 09:25:12 +00002225 if( pIndex ) sqliteFree(pIndex);
danielk19770202b292004-06-09 09:55:16 +00002226 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002227 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002228 sqliteFree(zName);
2229 return;
2230}
2231
2232/*
drh74e24cd2002-01-09 03:19:59 +00002233** This routine will drop an existing named index. This routine
2234** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002235*/
danielk19774adee202004-05-08 08:23:19 +00002236void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002237 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002238 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00002239 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002240
danielk197724b03fd2004-05-10 10:34:34 +00002241 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002242 assert( pName->nSrc==1 );
danielk19778a414492004-06-29 08:59:35 +00002243 if( SQLITE_OK!=sqlite3ReadSchema(pParse) ) return;
danielk19774adee202004-05-08 08:23:19 +00002244 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002245 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002246 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002247 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002248 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002249 }
drh485b39b2002-07-13 03:11:52 +00002250 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002251 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002252 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002253 goto exit_drop_index;
2254 }
danielk1977a8858102004-05-28 12:11:21 +00002255/*
drhd24cc422003-03-27 12:51:24 +00002256 if( pIndex->iDb>1 ){
danielk19774adee202004-05-08 08:23:19 +00002257 sqlite3ErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00002258 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00002259 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00002260 }
danielk1977a8858102004-05-28 12:11:21 +00002261*/
drhe5f9c642003-01-13 23:27:31 +00002262#ifndef SQLITE_OMIT_AUTHORIZATION
2263 {
2264 int code = SQLITE_DROP_INDEX;
2265 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002266 const char *zDb = db->aDb[pIndex->iDb].zName;
2267 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002268 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002269 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002270 }
drhd24cc422003-03-27 12:51:24 +00002271 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002272 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002273 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002274 }
drhed6c8672003-01-12 18:02:16 +00002275 }
drhe5f9c642003-01-13 23:27:31 +00002276#endif
drh75897232000-05-29 14:26:00 +00002277
2278 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002279 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002280 if( v ){
drh905793e2004-02-21 13:31:09 +00002281 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00002282 { OP_Rewind, 0, ADDR(9), 0},
danielk19770f69c1e2004-05-29 11:24:50 +00002283 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00002284 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002285 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00002286 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002287 { OP_Eq, 0, ADDR(8), 0},
2288 { OP_Next, 0, ADDR(3), 0},
2289 { OP_Goto, 0, ADDR(9), 0},
2290 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00002291 };
2292 int base;
2293
danielk19774adee202004-05-08 08:23:19 +00002294 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
2295 sqlite3OpenMasterTable(v, pIndex->iDb);
2296 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
2297 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
drhc275b4e2004-07-19 17:25:24 +00002298 sqlite3ChangeCookie(db, v, pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002299 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2300 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
2301 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00002302 }
2303
drhe0bc4042002-06-25 01:09:11 +00002304 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00002305 */
2306 if( !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +00002307 sqlite3UnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00002308 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00002309 }
drhd24cc422003-03-27 12:51:24 +00002310
2311exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002312 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002313}
2314
2315/*
drh75897232000-05-29 14:26:00 +00002316** Append a new element to the given IdList. Create a new IdList if
2317** need be.
drhdaffd0e2001-04-11 14:28:42 +00002318**
2319** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002320*/
danielk19774adee202004-05-08 08:23:19 +00002321IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002322 if( pList==0 ){
2323 pList = sqliteMalloc( sizeof(IdList) );
2324 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002325 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002326 }
drh4305d102003-07-30 12:34:12 +00002327 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002328 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002329 pList->nAlloc = pList->nAlloc*2 + 5;
2330 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002331 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002332 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002333 return 0;
drh75897232000-05-29 14:26:00 +00002334 }
drh6d4abfb2001-10-22 02:58:08 +00002335 pList->a = a;
drh75897232000-05-29 14:26:00 +00002336 }
2337 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
drha99db3b2004-06-19 14:49:12 +00002338 pList->a[pList->nId].zName = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002339 pList->nId++;
2340 return pList;
2341}
2342
2343/*
drhad3cab52002-05-24 02:04:32 +00002344** Append a new table name to the given SrcList. Create a new SrcList if
2345** need be. A new entry is created in the SrcList even if pToken is NULL.
2346**
2347** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002348**
2349** If pDatabase is not null, it means that the table has an optional
2350** database name prefix. Like this: "database.table". The pDatabase
2351** points to the table name and the pTable points to the database name.
2352** The SrcList.a[].zName field is filled with the table name which might
2353** come from pTable (if pDatabase is NULL) or from pDatabase.
2354** SrcList.a[].zDatabase is filled with the database name from pTable,
2355** or with NULL if no database is specified.
2356**
2357** In other words, if call like this:
2358**
danielk19774adee202004-05-08 08:23:19 +00002359** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002360**
2361** Then B is a table name and the database name is unspecified. If called
2362** like this:
2363**
danielk19774adee202004-05-08 08:23:19 +00002364** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002365**
2366** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002367*/
danielk19774adee202004-05-08 08:23:19 +00002368SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drha99db3b2004-06-19 14:49:12 +00002369 struct SrcList_item *pItem;
drhad3cab52002-05-24 02:04:32 +00002370 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002371 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002372 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002373 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002374 }
drh4305d102003-07-30 12:34:12 +00002375 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002376 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002377 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002378 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002379 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002380 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002381 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002382 return 0;
2383 }
drh113088e2003-03-20 01:16:58 +00002384 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002385 }
drha99db3b2004-06-19 14:49:12 +00002386 pItem = &pList->a[pList->nSrc];
2387 memset(pItem, 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002388 if( pDatabase && pDatabase->z==0 ){
2389 pDatabase = 0;
2390 }
2391 if( pDatabase && pTable ){
2392 Token *pTemp = pDatabase;
2393 pDatabase = pTable;
2394 pTable = pTemp;
2395 }
drha99db3b2004-06-19 14:49:12 +00002396 pItem->zName = sqlite3NameFromToken(pTable);
2397 pItem->zDatabase = sqlite3NameFromToken(pDatabase);
2398 pItem->iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002399 pList->nSrc++;
2400 return pList;
2401}
2402
2403/*
drh63eb5f22003-04-29 16:20:44 +00002404** Assign cursors to all tables in a SrcList
2405*/
danielk19774adee202004-05-08 08:23:19 +00002406void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002407 int i;
2408 for(i=0; i<pList->nSrc; i++){
2409 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002410 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002411 }
2412 }
2413}
2414
2415/*
drh75897232000-05-29 14:26:00 +00002416** Add an alias to the last identifier on the given identifier list.
2417*/
danielk19774adee202004-05-08 08:23:19 +00002418void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002419 if( pList && pList->nSrc>0 ){
drha99db3b2004-06-19 14:49:12 +00002420 pList->a[pList->nSrc-1].zAlias = sqlite3NameFromToken(pToken);
drh75897232000-05-29 14:26:00 +00002421 }
2422}
2423
2424/*
drhad3cab52002-05-24 02:04:32 +00002425** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002426*/
danielk19774adee202004-05-08 08:23:19 +00002427void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002428 int i;
2429 if( pList==0 ) return;
2430 for(i=0; i<pList->nId; i++){
2431 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002432 }
2433 sqliteFree(pList->a);
2434 sqliteFree(pList);
2435}
2436
2437/*
drhad2d8302002-05-24 20:31:36 +00002438** Return the index in pList of the identifier named zId. Return -1
2439** if not found.
2440*/
danielk19774adee202004-05-08 08:23:19 +00002441int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002442 int i;
2443 if( pList==0 ) return -1;
2444 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002445 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002446 }
2447 return -1;
2448}
2449
2450/*
drhad3cab52002-05-24 02:04:32 +00002451** Delete an entire SrcList including all its substructure.
2452*/
danielk19774adee202004-05-08 08:23:19 +00002453void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002454 int i;
2455 if( pList==0 ) return;
2456 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002457 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002458 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002459 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002460 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
danielk19774adee202004-05-08 08:23:19 +00002461 sqlite3DeleteTable(0, pList->a[i].pTab);
drhdaffd0e2001-04-11 14:28:42 +00002462 }
danielk19774adee202004-05-08 08:23:19 +00002463 sqlite3SelectDelete(pList->a[i].pSelect);
2464 sqlite3ExprDelete(pList->a[i].pOn);
2465 sqlite3IdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002466 }
drh75897232000-05-29 14:26:00 +00002467 sqliteFree(pList);
2468}
2469
drh982cef72000-05-30 16:27:03 +00002470/*
drhc4a3c772001-04-04 11:48:57 +00002471** Begin a transaction
2472*/
danielk197733752f82004-05-31 08:55:33 +00002473void sqlite3BeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002474 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002475 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002476
drh001bbcb2003-03-19 03:14:00 +00002477 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002478 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002479 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002480
2481 v = sqlite3GetVdbe(pParse);
2482 if( !v ) return;
2483 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002484}
2485
2486/*
2487** Commit a transaction
2488*/
danielk19774adee202004-05-08 08:23:19 +00002489void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002490 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002491 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002492
drh001bbcb2003-03-19 03:14:00 +00002493 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002494 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002495 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002496
2497 v = sqlite3GetVdbe(pParse);
2498 if( v ){
2499 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002500 }
drhc4a3c772001-04-04 11:48:57 +00002501}
2502
2503/*
2504** Rollback a transaction
2505*/
danielk19774adee202004-05-08 08:23:19 +00002506void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002507 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002508 Vdbe *v;
2509
drh001bbcb2003-03-19 03:14:00 +00002510 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002511 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002512 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002513
danielk19774adee202004-05-08 08:23:19 +00002514 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002515 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002516 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002517 }
drhc4a3c772001-04-04 11:48:57 +00002518}
drhf57b14a2001-09-14 18:54:08 +00002519
2520/*
drh80242052004-06-09 00:48:12 +00002521** Generate VDBE code that will verify the schema cookie and start
2522** a read-transaction for all named database files.
2523**
2524** It is important that all schema cookies be verified and all
2525** read transactions be started before anything else happens in
2526** the VDBE program. But this routine can be called after much other
2527** code has been generated. So here is what we do:
2528**
drhc275b4e2004-07-19 17:25:24 +00002529** The first time this routine is called, we code an OP_Goto that
drh80242052004-06-09 00:48:12 +00002530** will jump to a subroutine at the end of the program. Then we
2531** record every database that needs its schema verified in the
2532** pParse->cookieMask field. Later, after all other code has been
2533** generated, the subroutine that does the cookie verifications and
drhc275b4e2004-07-19 17:25:24 +00002534** starts the transactions will be coded and the OP_Goto P2 value
drh80242052004-06-09 00:48:12 +00002535** will be made to point to that subroutine. The generation of the
2536** cookie verification subroutine code happens in sqlite3FinishCoding().
drhc275b4e2004-07-19 17:25:24 +00002537**
2538** If iDb<0 then code the OP_Goto only - don't set flag to verify the
2539** schema on any databases. This can be used to position the OP_Goto
2540** early in the code, before we know if any database tables will be used.
drh001bbcb2003-03-19 03:14:00 +00002541*/
danielk19774adee202004-05-08 08:23:19 +00002542void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh80242052004-06-09 00:48:12 +00002543 sqlite *db;
2544 Vdbe *v;
2545 int mask;
2546
2547 v = sqlite3GetVdbe(pParse);
2548 if( v==0 ) return; /* This only happens if there was a prior error */
2549 db = pParse->db;
drhc275b4e2004-07-19 17:25:24 +00002550 if( pParse->cookieGoto==0 ){
2551 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1;
drh80242052004-06-09 00:48:12 +00002552 }
drhc275b4e2004-07-19 17:25:24 +00002553 if( iDb>=0 ){
2554 assert( iDb<db->nDb );
2555 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2556 assert( iDb<32 );
2557 mask = 1<<iDb;
2558 if( (pParse->cookieMask & mask)==0 ){
2559 pParse->cookieMask |= mask;
2560 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
2561 }
drh001bbcb2003-03-19 03:14:00 +00002562 }
drh001bbcb2003-03-19 03:14:00 +00002563}
2564
2565/*
drh1c928532002-01-31 15:54:21 +00002566** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002567** might change the database.
2568**
2569** This routine starts a new transaction if we are not already within
2570** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002571** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002572** be set for operations that might fail (due to a constraint) part of
2573** the way through and which will need to undo some writes without having to
2574** rollback the whole transaction. For operations where all constraints
2575** can be checked before any changes are made to the database, it is never
2576** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002577**
drh8bf8dc92003-05-17 17:35:10 +00002578** Only database iDb and the temp database are made writable by this call.
2579** If iDb==0, then the main and temp databases are made writable. If
2580** iDb==1 then only the temp database is made writable. If iDb>1 then the
2581** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002582*/
drh7f0f12e2004-05-21 13:39:50 +00002583void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002584 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002585 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002586 sqlite3CodeVerifySchema(pParse, iDb);
2587 pParse->writeMask |= 1<<iDb;
danielk19771d850a72004-05-31 08:26:49 +00002588 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002589 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002590 }
2591 if( iDb!=1 ){
2592 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002593 }
2594}
2595
2596/*
drh1c928532002-01-31 15:54:21 +00002597** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002598** the database. If a statement transaction was started, then emit
2599** an OP_Commit that will cause the changes to be committed to disk.
2600**
2601** Note that checkpoints are automatically committed at the end of
2602** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002603** sqlite3BeginWriteOperation() but there should only be a single
2604** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002605*/
danielk19774adee202004-05-08 08:23:19 +00002606void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002607 /* Delete me! */
2608 return;
drh1c928532002-01-31 15:54:21 +00002609}
danielk1977bfd6cce2004-06-18 04:24:54 +00002610
2611/*
2612** Return the transient sqlite3_value object used for encoding conversions
2613** during SQL compilation.
2614*/
2615sqlite3_value *sqlite3GetTransientValue(sqlite *db){
2616 if( !db->pValue ){
2617 db->pValue = sqlite3ValueNew();
2618 }
2619 return db->pValue;
2620}