blob: 0114aeb47a1a9dbe85214fd2a1b48459464edfbf [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**
danielk1977bfd6cce2004-06-18 04:24:54 +000026** $Id: build.c,v 1.221 2004/06/18 04:24:54 danielk1977 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 sqlite *db = pParse->db;
drh8bf8dc92003-05-17 17:35:10 +000039 int i;
drhe0bc4042002-06-25 01:09:11 +000040 pParse->explain = explainFlag;
danielk19778e227872004-06-07 07:52:17 +000041#if 0
drh1d85d932004-02-14 23:05:52 +000042 if((db->flags & SQLITE_Initialized)==0 && db->init.busy==0 ){
danielk19774adee202004-05-08 08:23:19 +000043 int rc = sqlite3Init(db, &pParse->zErrMsg);
drhe0bc4042002-06-25 01:09:11 +000044 if( rc!=SQLITE_OK ){
45 pParse->rc = rc;
46 pParse->nErr++;
47 }
48 }
danielk19778e227872004-06-07 07:52:17 +000049#endif
drh8bf8dc92003-05-17 17:35:10 +000050 for(i=0; i<db->nDb; i++){
51 DbClearProperty(db, i, DB_Locked);
52 if( !db->aDb[i].inTrans ){
53 DbClearProperty(db, i, DB_Cookie);
54 }
55 }
drh7c972de2003-09-06 22:18:07 +000056 pParse->nVar = 0;
drhe0bc4042002-06-25 01:09:11 +000057}
58
59/*
drh75897232000-05-29 14:26:00 +000060** This routine is called after a single SQL statement has been
drh80242052004-06-09 00:48:12 +000061** parsed and a VDBE program to execute that statement has been
62** prepared. This routine puts the finishing touches on the
63** VDBE program and resets the pParse structure for the next
64** parse.
drh75897232000-05-29 14:26:00 +000065**
66** Note that if an error occurred, it might be the case that
67** no VDBE code was generated.
68*/
drh80242052004-06-09 00:48:12 +000069void sqlite3FinishCoding(Parse *pParse){
70 sqlite *db;
71 Vdbe *v;
drhb86ccfb2003-01-28 23:13:10 +000072
danielk197724b03fd2004-05-10 10:34:34 +000073 if( sqlite3_malloc_failed ) return;
drh80242052004-06-09 00:48:12 +000074
75 /* Begin by generating some termination code at the end of the
76 ** vdbe program
77 */
78 db = pParse->db;
79 v = sqlite3GetVdbe(pParse);
80 if( v ){
81 sqlite3VdbeAddOp(v, OP_Halt, 0, 0);
82 if( pParse->cookieMask!=0 ){
83 u32 mask;
84 int iDb;
85 sqlite3VdbeChangeP2(v, pParse->cookieGoto, sqlite3VdbeCurrentAddr(v));
86 for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
87 if( (mask & pParse->cookieMask)==0 ) continue;
88 sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
89 if( iDb!=1 ){
90 sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]);
91 }
92 }
93 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto+1);
94 }
95 }
96
97 /* Get the VDBE program ready for execution
98 */
drhb86ccfb2003-01-28 23:13:10 +000099 if( v && pParse->nErr==0 ){
100 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
danielk19774adee202004-05-08 08:23:19 +0000101 sqlite3VdbeTrace(v, trace);
102 sqlite3VdbeMakeReady(v, pParse->nVar, pParse->explain);
drh826fb5a2004-02-14 23:59:57 +0000103 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drhd8bc7082000-06-07 23:51:50 +0000104 pParse->colNamesSet = 0;
drh826fb5a2004-02-14 23:59:57 +0000105 }else if( pParse->rc==SQLITE_OK ){
drh483750b2003-01-29 18:46:51 +0000106 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000107 }
drha226d052002-09-25 19:04:07 +0000108 pParse->nTab = 0;
109 pParse->nMem = 0;
110 pParse->nSet = 0;
111 pParse->nAgg = 0;
drh7c972de2003-09-06 22:18:07 +0000112 pParse->nVar = 0;
drh80242052004-06-09 00:48:12 +0000113 pParse->cookieMask = 0;
drh75897232000-05-29 14:26:00 +0000114}
115
116/*
drhf57b3392001-10-08 13:22:32 +0000117** Locate the in-memory structure that describes
118** a particular database table given the name
drha69d9162003-04-17 22:57:53 +0000119** of that table and (optionally) the name of the database
120** containing the table. Return NULL if not found.
121**
drhf26e09c2003-05-31 16:21:12 +0000122** If zDatabase is 0, all databases are searched for the
123** table and the first matching table is returned. (No checking
124** for duplicate table names is done.) The search order is
125** TEMP first, then MAIN, then any auxiliary databases added
126** using the ATTACH command.
127**
danielk19774adee202004-05-08 08:23:19 +0000128** See also sqlite3LocateTable().
drh75897232000-05-29 14:26:00 +0000129*/
danielk19774adee202004-05-08 08:23:19 +0000130Table *sqlite3FindTable(sqlite *db, const char *zName, const char *zDatabase){
drhd24cc422003-03-27 12:51:24 +0000131 Table *p = 0;
132 int i;
danielk1977c0391392004-06-09 12:30:04 +0000133 int rc = sqlite3ReadSchema(db, 0);
danielk19778e227872004-06-07 07:52:17 +0000134 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000135 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000136 if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
137 p = sqlite3HashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000138 if( p ) break;
139 }
drh74e24cd2002-01-09 03:19:59 +0000140 return p;
drh75897232000-05-29 14:26:00 +0000141}
142
143/*
drhf57b3392001-10-08 13:22:32 +0000144** Locate the in-memory structure that describes
drha69d9162003-04-17 22:57:53 +0000145** a particular database table given the name
146** of that table and (optionally) the name of the database
147** containing the table. Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000148** Also leave an error message in pParse->zErrMsg.
drha69d9162003-04-17 22:57:53 +0000149**
danielk19774adee202004-05-08 08:23:19 +0000150** The difference between this routine and sqlite3FindTable()
drhf26e09c2003-05-31 16:21:12 +0000151** is that this routine leaves an error message in pParse->zErrMsg
danielk19774adee202004-05-08 08:23:19 +0000152** where sqlite3FindTable() does not.
drha69d9162003-04-17 22:57:53 +0000153*/
danielk19774adee202004-05-08 08:23:19 +0000154Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){
drha69d9162003-04-17 22:57:53 +0000155 Table *p;
drhf26e09c2003-05-31 16:21:12 +0000156
danielk19774adee202004-05-08 08:23:19 +0000157 p = sqlite3FindTable(pParse->db, zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000158 if( p==0 ){
159 if( zDbase ){
danielk19774adee202004-05-08 08:23:19 +0000160 sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
161 }else if( sqlite3FindTable(pParse->db, zName, 0)!=0 ){
162 sqlite3ErrorMsg(pParse, "table \"%s\" is not in database \"%s\"",
drhf26e09c2003-05-31 16:21:12 +0000163 zName, zDbase);
drha69d9162003-04-17 22:57:53 +0000164 }else{
danielk19774adee202004-05-08 08:23:19 +0000165 sqlite3ErrorMsg(pParse, "no such table: %s", zName);
drha69d9162003-04-17 22:57:53 +0000166 }
drha6ecd332004-06-10 00:29:09 +0000167 pParse->checkSchema = 1;
drha69d9162003-04-17 22:57:53 +0000168 }
169 return p;
170}
171
172/*
173** Locate the in-memory structure that describes
174** a particular index given the name of that index
175** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000176** Return NULL if not found.
drhf26e09c2003-05-31 16:21:12 +0000177**
178** If zDatabase is 0, all databases are searched for the
179** table and the first matching index is returned. (No checking
180** for duplicate index names is done.) The search order is
181** TEMP first, then MAIN, then any auxiliary databases added
182** using the ATTACH command.
drh75897232000-05-29 14:26:00 +0000183*/
danielk19774adee202004-05-08 08:23:19 +0000184Index *sqlite3FindIndex(sqlite *db, const char *zName, const char *zDb){
drhd24cc422003-03-27 12:51:24 +0000185 Index *p = 0;
186 int i;
danielk1977c0391392004-06-09 12:30:04 +0000187 int rc = sqlite3ReadSchema(db, 0);
danielk19778e227872004-06-07 07:52:17 +0000188 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000189 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
danielk19774adee202004-05-08 08:23:19 +0000190 if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
191 p = sqlite3HashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000192 if( p ) break;
193 }
drh74e24cd2002-01-09 03:19:59 +0000194 return p;
drh75897232000-05-29 14:26:00 +0000195}
196
197/*
198** Remove the given index from the index hash table, and free
199** its memory structures.
200**
drhd229ca92002-01-09 13:30:41 +0000201** The index is removed from the database hash tables but
202** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000203** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000204*/
drh74e24cd2002-01-09 03:19:59 +0000205static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000206 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000207
drhd229ca92002-01-09 13:30:41 +0000208 assert( db!=0 && p->zName!=0 );
danielk19774adee202004-05-08 08:23:19 +0000209 pOld = sqlite3HashInsert(&db->aDb[p->iDb].idxHash, p->zName,
drhd24cc422003-03-27 12:51:24 +0000210 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000211 if( pOld!=0 && pOld!=p ){
danielk19774adee202004-05-08 08:23:19 +0000212 sqlite3HashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
drhd24cc422003-03-27 12:51:24 +0000213 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000214 }
danielk1977a37cdde2004-05-16 11:15:36 +0000215 if( p->zColAff ){
216 sqliteFree(p->zColAff);
217 }
drh74e24cd2002-01-09 03:19:59 +0000218 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000219}
220
221/*
drhbeae3192001-09-22 18:12:08 +0000222** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000223** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000224** structures.
225*/
danielk19774adee202004-05-08 08:23:19 +0000226void sqlite3UnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000227 if( pIndex->pTable->pIndex==pIndex ){
228 pIndex->pTable->pIndex = pIndex->pNext;
229 }else{
230 Index *p;
231 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
232 if( p && p->pNext==pIndex ){
233 p->pNext = pIndex->pNext;
234 }
235 }
236 sqliteDeleteIndex(db, pIndex);
237}
238
239/*
drhe0bc4042002-06-25 01:09:11 +0000240** Erase all schema information from the in-memory hash tables of
241** database connection. This routine is called to reclaim memory
242** before the connection closes. It is also called during a rollback
243** if there were schema changes during the transaction.
drh1c2d8412003-03-31 00:30:47 +0000244**
245** If iDb<=0 then reset the internal schema tables for all database
246** files. If iDb>=2 then reset the internal schema for only the
jplyoncfa56842004-01-19 04:55:56 +0000247** single file indicated.
drh74e24cd2002-01-09 03:19:59 +0000248*/
danielk19774adee202004-05-08 08:23:19 +0000249void sqlite3ResetInternalSchema(sqlite *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000250 HashElem *pElem;
251 Hash temp1;
252 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000253 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000254
drh1c2d8412003-03-31 00:30:47 +0000255 assert( iDb>=0 && iDb<db->nDb );
256 db->flags &= ~SQLITE_Initialized;
257 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000258 Db *pDb = &db->aDb[i];
259 temp1 = pDb->tblHash;
260 temp2 = pDb->trigHash;
danielk19774adee202004-05-08 08:23:19 +0000261 sqlite3HashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
262 sqlite3HashClear(&pDb->aFKey);
263 sqlite3HashClear(&pDb->idxHash);
drhd24cc422003-03-27 12:51:24 +0000264 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
265 Trigger *pTrigger = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000266 sqlite3DeleteTrigger(pTrigger);
drhd24cc422003-03-27 12:51:24 +0000267 }
danielk19774adee202004-05-08 08:23:19 +0000268 sqlite3HashClear(&temp2);
269 sqlite3HashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
drhd24cc422003-03-27 12:51:24 +0000270 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
271 Table *pTab = sqliteHashData(pElem);
danielk19774adee202004-05-08 08:23:19 +0000272 sqlite3DeleteTable(db, pTab);
drhd24cc422003-03-27 12:51:24 +0000273 }
danielk19774adee202004-05-08 08:23:19 +0000274 sqlite3HashClear(&temp1);
drh8bf8dc92003-05-17 17:35:10 +0000275 DbClearProperty(db, i, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000276 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000277 }
drh1c2d8412003-03-31 00:30:47 +0000278 assert( iDb==0 );
279 db->flags &= ~SQLITE_InternChanges;
280
281 /* If one or more of the auxiliary database files has been closed,
282 ** then remove then from the auxiliary database list. We take the
283 ** opportunity to do this here since we have just deleted all of the
284 ** schema hash tables and therefore do not have to make any changes
285 ** to any of those tables.
286 */
drh4d189ca2004-02-12 18:46:38 +0000287 for(i=0; i<db->nDb; i++){
288 struct Db *pDb = &db->aDb[i];
289 if( pDb->pBt==0 ){
290 if( pDb->pAux && pDb->xFreeAux ) pDb->xFreeAux(pDb->pAux);
291 pDb->pAux = 0;
292 }
293 }
drh1c2d8412003-03-31 00:30:47 +0000294 for(i=j=2; i<db->nDb; i++){
drh4d189ca2004-02-12 18:46:38 +0000295 struct Db *pDb = &db->aDb[i];
296 if( pDb->pBt==0 ){
297 sqliteFree(pDb->zName);
298 pDb->zName = 0;
drh1c2d8412003-03-31 00:30:47 +0000299 continue;
300 }
301 if( j<i ){
drh8bf8dc92003-05-17 17:35:10 +0000302 db->aDb[j] = db->aDb[i];
drh1c2d8412003-03-31 00:30:47 +0000303 }
drh8bf8dc92003-05-17 17:35:10 +0000304 j++;
drh1c2d8412003-03-31 00:30:47 +0000305 }
306 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
307 db->nDb = j;
308 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
309 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
310 sqliteFree(db->aDb);
311 db->aDb = db->aDbStatic;
312 }
drhe0bc4042002-06-25 01:09:11 +0000313}
314
315/*
316** This routine is called whenever a rollback occurs. If there were
317** schema changes during the transaction, then we have to reset the
318** internal hash tables and reload them from disk.
319*/
danielk19774adee202004-05-08 08:23:19 +0000320void sqlite3RollbackInternalChanges(sqlite *db){
drhe0bc4042002-06-25 01:09:11 +0000321 if( db->flags & SQLITE_InternChanges ){
danielk19774adee202004-05-08 08:23:19 +0000322 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000323 }
324}
325
326/*
327** This routine is called when a commit occurs.
328*/
danielk19774adee202004-05-08 08:23:19 +0000329void sqlite3CommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000330 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000331 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000332}
333
334/*
drh75897232000-05-29 14:26:00 +0000335** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000336** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000337**
338** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000339** the table data structure from the hash table. Nor does it remove
340** foreign keys from the sqlite.aFKey hash table. But it does destroy
341** memory structures of the indices and foreign keys associated with
342** the table.
drhdaffd0e2001-04-11 14:28:42 +0000343**
344** Indices associated with the table are unlinked from the "db"
345** data structure if db!=NULL. If db==NULL, indices attached to
346** the table are deleted, but it is assumed they have already been
347** unlinked.
drh75897232000-05-29 14:26:00 +0000348*/
danielk19774adee202004-05-08 08:23:19 +0000349void sqlite3DeleteTable(sqlite *db, Table *pTable){
drh75897232000-05-29 14:26:00 +0000350 int i;
351 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000352 FKey *pFKey, *pNextFKey;
353
drh75897232000-05-29 14:26:00 +0000354 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000355
356 /* Delete all indices associated with this table
357 */
358 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
359 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000360 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000361 sqliteDeleteIndex(db, pIndex);
362 }
363
364 /* Delete all foreign keys associated with this table. The keys
365 ** should have already been unlinked from the db->aFKey hash table
366 */
367 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
368 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000369 assert( pTable->iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000370 assert( sqlite3HashFind(&db->aDb[pTable->iDb].aFKey,
drhd24cc422003-03-27 12:51:24 +0000371 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000372 sqliteFree(pFKey);
373 }
374
375 /* Delete the Table structure itself.
376 */
drh75897232000-05-29 14:26:00 +0000377 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000378 sqliteFree(pTable->aCol[i].zName);
379 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000380 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000381 }
drh6e142f52000-06-08 13:36:40 +0000382 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000383 sqliteFree(pTable->aCol);
danielk19773d1bfea2004-05-14 11:00:53 +0000384 if( pTable->zColAff ){
385 sqliteFree(pTable->zColAff);
386 }
danielk19774adee202004-05-08 08:23:19 +0000387 sqlite3SelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000388 sqliteFree(pTable);
389}
390
391/*
drh5edc3122001-09-13 21:53:09 +0000392** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000393** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000394*/
drh74e24cd2002-01-09 03:19:59 +0000395static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000396 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000397 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000398 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000399 assert( db!=0 );
danielk19774adee202004-05-08 08:23:19 +0000400 pOld = sqlite3HashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000401 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000402 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
403 int nTo = strlen(pF1->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +0000404 pF2 = sqlite3HashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000405 if( pF2==pF1 ){
danielk19774adee202004-05-08 08:23:19 +0000406 sqlite3HashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000407 }else{
408 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
409 if( pF2 ){
410 pF2->pNextTo = pF1->pNextTo;
411 }
412 }
413 }
danielk19774adee202004-05-08 08:23:19 +0000414 sqlite3DeleteTable(db, p);
drh74e24cd2002-01-09 03:19:59 +0000415}
416
417/*
drh1ccde152000-06-17 13:12:39 +0000418** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000419**
420** Space to hold the name is obtained from sqliteMalloc() and must
421** be freed by the calling function.
422*/
danielk19774adee202004-05-08 08:23:19 +0000423char *sqlite3TableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000424 char *zName = sqliteStrNDup(pName->z, pName->n);
danielk19774adee202004-05-08 08:23:19 +0000425 sqlite3Dequote(zName);
drh75897232000-05-29 14:26:00 +0000426 return zName;
427}
428
429/*
danielk1977cbb18d22004-05-28 11:37:27 +0000430** Open the sqlite_master table stored in database number iDb for
431** writing. The table is opened using cursor 0.
drhe0bc4042002-06-25 01:09:11 +0000432*/
danielk1977cbb18d22004-05-28 11:37:27 +0000433void sqlite3OpenMasterTable(Vdbe *v, int iDb){
434 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
danielk19778e150812004-05-10 01:17:37 +0000435 sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT);
danielk1977b4964b72004-05-18 01:23:38 +0000436 sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */
drhe0bc4042002-06-25 01:09:11 +0000437}
438
439/*
danielk1977cbb18d22004-05-28 11:37:27 +0000440** The token *pName contains the name of a database (either "main" or
441** "temp" or the name of an attached db). This routine returns the
442** index of the named database in db->aDb[], or -1 if the named db
443** does not exist.
444*/
445int findDb(sqlite3 *db, Token *pName){
446 int i;
447 for(i=0; i<db->nDb; i++){
448 if( pName->n==strlen(db->aDb[i].zName) &&
449 0==sqlite3StrNICmp(db->aDb[i].zName, pName->z, pName->n) ){
450 return i;
451 }
452 }
453 return -1;
454}
455
danielk1977ef2cb632004-05-29 02:37:19 +0000456int sqlite3TwoPartName(
danielk1977cbb18d22004-05-28 11:37:27 +0000457 Parse *pParse,
458 Token *pName1,
459 Token *pName2,
460 Token **pUnqual
461){
462 int iDb;
463 sqlite3 *db = pParse->db;
464
465 if( pName2 && pName2->n>0 ){
466 assert( !db->init.busy );
467 *pUnqual = pName2;
468 iDb = findDb(db, pName1);
469 if( iDb<0 ){
470 sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
471 pParse->nErr++;
472 return -1;
473 }
474 }else{
475 assert( db->init.iDb==0 || db->init.busy );
476 iDb = db->init.iDb;
477 *pUnqual = pName1;
478 }
479 return iDb;
480}
481
482/*
danielk1977d8123362004-06-12 09:25:12 +0000483** This routine is used to check if the UTF-8 string zName is a legal
484** unqualified name for a new schema object (table, index, view or
485** trigger). All names are legal except those that begin with the string
486** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
487** is reserved for internal use.
488*/
489int sqlite3CheckObjectName(Parse *pParse, const char *zName){
490 if( !pParse->db->init.busy && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
491 sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
492 return SQLITE_ERROR;
493 }
494 return SQLITE_OK;
495}
496
497/*
drh75897232000-05-29 14:26:00 +0000498** Begin constructing a new table representation in memory. This is
499** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000500** to a CREATE TABLE statement. In particular, this routine is called
501** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000502** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000503** flag is true if the table should be stored in the auxiliary database
504** file instead of in the main database file. This is normally the case
505** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000506** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000507**
drhf57b3392001-10-08 13:22:32 +0000508** The new table record is initialized and put in pParse->pNewTable.
509** As more of the CREATE TABLE statement is parsed, additional action
510** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000511** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000512** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000513*/
danielk19774adee202004-05-08 08:23:19 +0000514void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000515 Parse *pParse, /* Parser context */
516 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000517 Token *pName1, /* First part of the name of the table or view */
518 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000519 int isTemp, /* True if this is a TEMP table */
520 int isView /* True if this is a VIEW */
521){
drh75897232000-05-29 14:26:00 +0000522 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000523 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000524 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000525 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000526 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000527 int iDb; /* Database number to create the table in */
528 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000529
danielk1977cbb18d22004-05-28 11:37:27 +0000530 /* The table or view name to create is passed to this routine via tokens
531 ** pName1 and pName2. If the table name was fully qualified, for example:
532 **
533 ** CREATE TABLE xxx.yyy (...);
534 **
535 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
536 ** the table name is not fully qualified, i.e.:
537 **
538 ** CREATE TABLE yyy(...);
539 **
540 ** Then pName1 is set to "yyy" and pName2 is "".
541 **
542 ** The call below sets the pName pointer to point at the token (pName1 or
543 ** pName2) that stores the unqualified table name. The variable iDb is
544 ** set to the index of the database that the table or view is to be
545 ** created in.
546 */
danielk1977ef2cb632004-05-29 02:37:19 +0000547 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000548 if( iDb<0 ) return;
549 if( isTemp && iDb>1 ){
550 /* If creating a temp table, the name may not be qualified */
551 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
552 pParse->nErr++;
553 return;
554 }
555 if( isTemp ) iDb = 1;
556
557 pParse->sNameToken = *pName;
danielk19774adee202004-05-08 08:23:19 +0000558 zName = sqlite3TableNameFromToken(pName);
danielk1977e0048402004-06-15 16:51:01 +0000559 if( zName==0 ) return;
danielk1977d8123362004-06-12 09:25:12 +0000560 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
danielk1977e0048402004-06-15 16:51:01 +0000561 sqliteFree(zName);
danielk1977d8123362004-06-12 09:25:12 +0000562 return;
563 }
drh1d85d932004-02-14 23:05:52 +0000564 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000565#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000566 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000567 {
568 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000569 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000570 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000571 sqliteFree(zName);
572 return;
573 }
drhe5f9c642003-01-13 23:27:31 +0000574 if( isView ){
575 if( isTemp ){
576 code = SQLITE_CREATE_TEMP_VIEW;
577 }else{
578 code = SQLITE_CREATE_VIEW;
579 }
580 }else{
581 if( isTemp ){
582 code = SQLITE_CREATE_TEMP_TABLE;
583 }else{
584 code = SQLITE_CREATE_TABLE;
585 }
586 }
danielk19774adee202004-05-08 08:23:19 +0000587 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000588 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000589 return;
590 }
591 }
592#endif
drhf57b3392001-10-08 13:22:32 +0000593
594 /* Before trying to create a temporary table, make sure the Btree for
595 ** holding temporary tables is open.
596 */
drhd24cc422003-03-27 12:51:24 +0000597 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +0000598 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000599 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000600 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
drhf7a9e1a2004-02-22 18:40:56 +0000601 "file for storing temporary tables");
drhf57b3392001-10-08 13:22:32 +0000602 pParse->nErr++;
danielk1977e0048402004-06-15 16:51:01 +0000603 sqliteFree(zName);
drhf57b3392001-10-08 13:22:32 +0000604 return;
605 }
danielk1977ee5741e2004-05-31 10:01:34 +0000606 if( db->flags & !db->autoCommit ){
danielk197713adf8a2004-06-03 16:08:41 +0000607 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1, 0);
drhf57b3392001-10-08 13:22:32 +0000608 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000609 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
drhf7a9e1a2004-02-22 18:40:56 +0000610 "the temporary database file");
danielk1977e0048402004-06-15 16:51:01 +0000611 sqliteFree(zName);
drhf57b3392001-10-08 13:22:32 +0000612 return;
613 }
614 }
615 }
616
617 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000618 ** index or table name in the same database. Issue an error message if
619 ** it does.
drhf57b3392001-10-08 13:22:32 +0000620 */
danielk19773df6b252004-05-29 10:23:19 +0000621 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
622 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000623 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000624 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000625 return;
drh75897232000-05-29 14:26:00 +0000626 }
danielk19774adee202004-05-08 08:23:19 +0000627 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
drh1d85d932004-02-14 23:05:52 +0000628 (pIdx->iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000629 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000630 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000631 return;
632 }
633 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000634 if( pTable==0 ){
danielk1977e0048402004-06-15 16:51:01 +0000635 pParse->rc = SQLITE_NOMEM;
636 pParse->nErr++;
drh6d4abfb2001-10-22 02:58:08 +0000637 sqliteFree(zName);
638 return;
639 }
drh75897232000-05-29 14:26:00 +0000640 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000641 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000642 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000643 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000644 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000645 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000646 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000647 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000648
649 /* Begin generating the code that will insert the table record into
650 ** the SQLITE_MASTER table. Note in particular that we must go ahead
651 ** and allocate the record number for the table entry now. Before any
652 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
653 ** indices to be created and the table record must come before the
654 ** indices. Hence, the record number for the table must be allocated
655 ** now.
656 */
danielk19774adee202004-05-08 08:23:19 +0000657 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000658 sqlite3BeginWriteOperation(pParse, 0, iDb);
drhf57b3392001-10-08 13:22:32 +0000659 if( !isTemp ){
danielk1977cbb18d22004-05-28 11:37:27 +0000660 /* Every time a new table is created the file-format
661 ** and encoding meta-values are set in the database, in
662 ** case this is the first table created.
663 */
danielk19774adee202004-05-08 08:23:19 +0000664 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000665 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
danielk1977172bc392004-05-22 08:09:11 +0000666 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000667 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
drhf57b3392001-10-08 13:22:32 +0000668 }
danielk1977cbb18d22004-05-28 11:37:27 +0000669 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000670 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
671 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000672 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000673 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000674 }
drh75897232000-05-29 14:26:00 +0000675}
676
677/*
678** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000679**
680** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000681** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000682** first to get things going. Then this routine is called for each
683** column.
drh75897232000-05-29 14:26:00 +0000684*/
danielk19774adee202004-05-08 08:23:19 +0000685void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000686 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000687 int i;
688 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000689 Column *pCol;
drh75897232000-05-29 14:26:00 +0000690 if( (p = pParse->pNewTable)==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000691 sqlite3SetNString(&z, pName->z, pName->n, 0);
drh97fc3d02002-05-22 21:27:03 +0000692 if( z==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000693 sqlite3Dequote(z);
drh97fc3d02002-05-22 21:27:03 +0000694 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000695 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
696 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000697 sqliteFree(z);
698 return;
699 }
700 }
drh75897232000-05-29 14:26:00 +0000701 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000702 Column *aNew;
703 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
704 if( aNew==0 ) return;
705 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000706 }
drhc9b84a12002-06-20 11:36:48 +0000707 pCol = &p->aCol[p->nCol];
708 memset(pCol, 0, sizeof(p->aCol[0]));
709 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000710
711 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000712 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
713 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000714 */
danielk19774f057f92004-06-08 00:02:33 +0000715 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000716 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000717 p->nCol++;
drh75897232000-05-29 14:26:00 +0000718}
719
720/*
drh382c0242001-10-06 16:33:02 +0000721** This routine is called by the parser while in the middle of
722** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
723** been seen on a column. This routine sets the notNull flag on
724** the column currently under construction.
725*/
danielk19774adee202004-05-08 08:23:19 +0000726void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000727 Table *p;
728 int i;
729 if( (p = pParse->pNewTable)==0 ) return;
730 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000731 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000732}
733
734/*
735** This routine is called by the parser while in the middle of
736** parsing a CREATE TABLE statement. The pFirst token is the first
737** token in the sequence of tokens that describe the type of the
738** column currently under construction. pLast is the last token
739** in the sequence. Use this information to construct a string
740** that contains the typename of the column and store that string
741** in zType.
742*/
danielk19774adee202004-05-08 08:23:19 +0000743void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000744 Table *p;
745 int i, j;
746 int n;
747 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000748 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000749 if( (p = pParse->pNewTable)==0 ) return;
750 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000751 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000752 pCol = &p->aCol[i];
753 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000754 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000755 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000756 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000757 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000758 for(i=j=0; z[i]; i++){
759 int c = z[i];
760 if( isspace(c) ) continue;
761 z[j++] = c;
762 }
763 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000764 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000765}
766
767/*
drh7020f652000-06-03 18:06:52 +0000768** The given token is the default value for the last column added to
769** the table currently under construction. If "minusFlag" is true, it
770** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000771**
772** This routine is called by the parser while in the middle of
773** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000774*/
danielk19774adee202004-05-08 08:23:19 +0000775void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000776 Table *p;
777 int i;
778 char **pz;
779 if( (p = pParse->pNewTable)==0 ) return;
780 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000781 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000782 pz = &p->aCol[i].zDflt;
783 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000784 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000785 }else{
danielk19774adee202004-05-08 08:23:19 +0000786 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000787 }
danielk19774adee202004-05-08 08:23:19 +0000788 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000789}
790
791/*
drh4a324312001-12-21 14:30:42 +0000792** Designate the PRIMARY KEY for the table. pList is a list of names
793** of columns that form the primary key. If pList is NULL, then the
794** most recently added column of the table is the primary key.
795**
796** A table can have at most one primary key. If the table already has
797** a primary key (and this is the second primary key) then create an
798** error.
799**
800** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
801** then we will try to use that column as the row id. (Exception:
802** For backwards compatibility with older databases, do not do this
803** if the file format version number is less than 1.) Set the Table.iPKey
804** field of the table under construction to be the index of the
805** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
806** no INTEGER PRIMARY KEY.
807**
808** If the key is not an INTEGER PRIMARY KEY, then create a unique
809** index for the key. No index is created for INTEGER PRIMARY KEYs.
810*/
danielk19770202b292004-06-09 09:55:16 +0000811void sqlite3AddPrimaryKey(Parse *pParse, ExprList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000812 Table *pTab = pParse->pNewTable;
813 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000814 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000815 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000816 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000817 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000818 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000819 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000820 }
821 pTab->hasPrimKey = 1;
822 if( pList==0 ){
823 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000824 pTab->aCol[iCol].isPrimKey = 1;
825 }else{
danielk19770202b292004-06-09 09:55:16 +0000826 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000827 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000828 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
829 break;
830 }
drh78100cc2003-08-23 22:40:53 +0000831 }
832 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000833 }
danielk19770202b292004-06-09 09:55:16 +0000834 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000835 }
836 if( iCol>=0 && iCol<pTab->nCol ){
837 zType = pTab->aCol[iCol].zType;
838 }
danielk19773d68f032004-05-11 07:11:51 +0000839 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000840 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000841 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000842 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000843 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000844 pList = 0;
drh4a324312001-12-21 14:30:42 +0000845 }
drhe0194f22003-02-26 13:52:51 +0000846
847primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000848 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000849 return;
drh4a324312001-12-21 14:30:42 +0000850}
851
852/*
drhd3d39e92004-05-20 22:16:29 +0000853** Set the collation function of the most recently parsed table column
854** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000855*/
drhd3d39e92004-05-20 22:16:29 +0000856void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000857 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000858 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000859 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000860 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000861
drhd3d39e92004-05-20 22:16:29 +0000862 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000863 i = p->nCol-1;
864
865 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
866 p->aCol[i].pColl = pColl;
867
868 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
869 ** then an index may have been created on this column before the
870 ** collation type was added. Correct this if it is the case.
871 */
872 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
873 assert( pIdx->nColumn==1 );
874 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000875 }
876}
877
878/*
danielk19770202b292004-06-09 09:55:16 +0000879** Locate and return an entry from the db.aCollSeq hash table. If the entry
880** specified by zName and nName is not found and parameter 'create' is
danielk1977466be562004-06-10 02:16:01 +0000881** true, then create a new entry. Otherwise return NULL.
drhd3d39e92004-05-20 22:16:29 +0000882**
danielk1977466be562004-06-10 02:16:01 +0000883** Each pointer stored in the sqlite3.aCollSeq hash table contains an
884** array of three CollSeq structures. The first is the collation sequence
885** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
drhd3d39e92004-05-20 22:16:29 +0000886**
danielk1977466be562004-06-10 02:16:01 +0000887** Stored immediately after the three collation sequences is a copy of
888** the collation sequence name. A pointer to this string is stored in
889** each collation sequence structure.
drhd3d39e92004-05-20 22:16:29 +0000890*/
danielk1977466be562004-06-10 02:16:01 +0000891static CollSeq * findCollSeqEntry(
892 sqlite *db,
893 const char *zName,
danielk19770202b292004-06-09 09:55:16 +0000894 int nName,
895 int create
drhd3d39e92004-05-20 22:16:29 +0000896){
897 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000898 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000899 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk1977466be562004-06-10 02:16:01 +0000900
danielk19770202b292004-06-09 09:55:16 +0000901 if( 0==pColl && create ){
danielk1977466be562004-06-10 02:16:01 +0000902 pColl = sqliteMalloc( 3*sizeof(*pColl) + nName + 1 );
danielk19770202b292004-06-09 09:55:16 +0000903 if( pColl ){
danielk1977466be562004-06-10 02:16:01 +0000904 pColl[0].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000905 pColl[0].enc = SQLITE_UTF8;
danielk1977466be562004-06-10 02:16:01 +0000906 pColl[1].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000907 pColl[1].enc = SQLITE_UTF16LE;
danielk1977466be562004-06-10 02:16:01 +0000908 pColl[2].zName = (char*)&pColl[3];
danielk1977dc8453f2004-06-12 00:42:34 +0000909 pColl[2].enc = SQLITE_UTF16BE;
danielk19777cedc8d2004-06-10 10:50:08 +0000910 memcpy(pColl[0].zName, zName, nName);
911 pColl[0].zName[nName] = 0;
danielk1977466be562004-06-10 02:16:01 +0000912 sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +0000913 }
drhd3d39e92004-05-20 22:16:29 +0000914 }
drhd3d39e92004-05-20 22:16:29 +0000915 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000916}
917
danielk1977466be562004-06-10 02:16:01 +0000918/*
919** Parameter zName points to a UTF-8 encoded string nName bytes long.
920** Return the CollSeq* pointer for the collation sequence named zName
921** for the encoding 'enc' from the database 'db'.
922**
923** If the entry specified is not found and 'create' is true, then create a
924** new entry. Otherwise return NULL.
925*/
926CollSeq *sqlite3FindCollSeq(
927 sqlite *db,
928 u8 enc,
929 const char *zName,
930 int nName,
931 int create
932){
933 CollSeq *pColl = findCollSeqEntry(db, zName, nName, create);
934 if( pColl ) switch( enc ){
danielk1977dc8453f2004-06-12 00:42:34 +0000935 case SQLITE_UTF8:
danielk1977466be562004-06-10 02:16:01 +0000936 break;
danielk1977dc8453f2004-06-12 00:42:34 +0000937 case SQLITE_UTF16LE:
danielk19774e6af132004-06-10 14:01:08 +0000938 pColl = &pColl[1];
danielk1977466be562004-06-10 02:16:01 +0000939 break;
danielk1977dc8453f2004-06-12 00:42:34 +0000940 case SQLITE_UTF16BE:
danielk19774e6af132004-06-10 14:01:08 +0000941 pColl = &pColl[2];
danielk1977466be562004-06-10 02:16:01 +0000942 break;
943 default:
944 assert(!"Cannot happen");
945 }
946 return pColl;
947}
948
danielk19777cedc8d2004-06-10 10:50:08 +0000949static void callCollNeeded(sqlite *db, const char *zName, int nName){
950 /* No collation sequence of this type for this encoding is registered.
951 ** Call the collation factory to see if it can supply us with one.
952 */
953 char *zExternal = 0;
954 assert( !db->xCollNeeded || !db->xCollNeeded16 );
955 if( nName<0 ) nName = strlen(zName);
956 if( db->xCollNeeded ){
957 zExternal = sqliteStrNDup(zName, nName);
958 if( !zExternal ) return;
959 db->xCollNeeded(db->pCollNeededArg, db, (int)db->enc, zExternal);
960 }
961 if( db->xCollNeeded16 ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000962 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
963 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
964 zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
danielk19777cedc8d2004-06-10 10:50:08 +0000965 if( !zExternal ) return;
966 db->xCollNeeded16(db->pCollNeededArg, db, (int)db->enc, zExternal);
967 }
danielk19777cedc8d2004-06-10 10:50:08 +0000968}
969
970static int synthCollSeq(Parse *pParse, CollSeq *pColl){
971 /* The collation factory failed to deliver a function but there may be
972 ** other versions of this collation function (for other text encodings)
973 ** available. Use one of these instead. Avoid a UTF-8 <-> UTF-16
974 ** conversion if possible.
975 */
976 CollSeq *pColl2 = 0;
977 char *z = pColl->zName;
978 int n = strlen(z);
979 switch( pParse->db->enc ){
danielk1977dc8453f2004-06-12 00:42:34 +0000980 case SQLITE_UTF16LE:
981 pColl2 = sqlite3FindCollSeq(pParse->db, SQLITE_UTF16BE, z, n, 0);
danielk19777cedc8d2004-06-10 10:50:08 +0000982 assert( pColl2 );
983 if( pColl2->xCmp ) break;
danielk1977dc8453f2004-06-12 00:42:34 +0000984 pColl2 = sqlite3FindCollSeq(pParse->db, SQLITE_UTF8, z, n, 0);
danielk19777cedc8d2004-06-10 10:50:08 +0000985 assert( pColl2 );
986 break;
987
danielk1977dc8453f2004-06-12 00:42:34 +0000988 case SQLITE_UTF16BE:
989 pColl2 = sqlite3FindCollSeq(pParse->db,SQLITE_UTF16LE, z, n, 0);
danielk19777cedc8d2004-06-10 10:50:08 +0000990 assert( pColl2 );
991 if( pColl2->xCmp ) break;
danielk1977dc8453f2004-06-12 00:42:34 +0000992 pColl2 = sqlite3FindCollSeq(pParse->db,SQLITE_UTF8, z, n, 0);
danielk19777cedc8d2004-06-10 10:50:08 +0000993 assert( pColl2 );
994 break;
995
danielk1977dc8453f2004-06-12 00:42:34 +0000996 case SQLITE_UTF8:
997 pColl2 = sqlite3FindCollSeq(pParse->db,SQLITE_UTF16BE, z, n, 0);
danielk19777cedc8d2004-06-10 10:50:08 +0000998 assert( pColl2 );
999 if( pColl2->xCmp ) break;
danielk1977dc8453f2004-06-12 00:42:34 +00001000 pColl2 = sqlite3FindCollSeq(pParse->db,SQLITE_UTF16LE, z, n, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001001 assert( pColl2 );
1002 break;
1003 }
1004 if( pColl2->xCmp ){
1005 memcpy(pColl, pColl2, sizeof(CollSeq));
1006 }else{
1007 if( pParse->nErr==0 ){
1008 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ",
1009 -1, z, n, 0);
1010 }
1011 pParse->nErr++;
1012 return SQLITE_ERROR;
1013 }
1014 return SQLITE_OK;
1015}
1016
1017/*
1018** This routine is called on a collation sequence before it is used to
1019** check that it is defined. An undefined collation sequence exists when
1020** a database is loaded that contains references to collation sequences
1021** that have not been defined by sqlite3_create_collation() etc.
1022**
1023** If required, this routine calls the 'collation needed' callback to
1024** request a definition of the collating sequence. If this doesn't work,
1025** an equivalent collating sequence that uses a text encoding different
1026** from the main database is substituted, if one is available.
1027*/
1028int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
1029 if( pColl && !pColl->xCmp ){
1030 callCollNeeded(pParse->db, pColl->zName, strlen(pColl->zName));
1031 if( !pColl->xCmp && synthCollSeq(pParse, pColl) ){
1032 return SQLITE_ERROR;
1033 }
1034 }
1035 return SQLITE_OK;
1036}
1037
1038int sqlite3CheckIndexCollSeq(Parse *pParse, Index *pIdx){
1039 if( pIdx ){
1040 int i;
1041 for(i=0; i<pIdx->nColumn; i++){
1042 if( sqlite3CheckCollSeq(pParse, pIdx->keyInfo.aColl[i]) ){
1043 return SQLITE_ERROR;
1044 }
1045 }
1046 }
1047 return SQLITE_OK;
1048}
1049
danielk1977466be562004-06-10 02:16:01 +00001050/*
1051** This function returns the collation sequence for database native text
1052** encoding identified by the string zName, length nName.
1053**
1054** If the requested collation sequence is not available, or not available
1055** in the database native encoding, the collation factory is invoked to
1056** request it. If the collation factory does not supply such a sequence,
1057** and the sequence is available in another text encoding, then that is
1058** returned instead.
1059**
1060** If no versions of the requested collations sequence are available, or
1061** another error occurs, NULL is returned and an error message written into
1062** pParse.
1063*/
danielk19770202b292004-06-09 09:55:16 +00001064CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
danielk1977466be562004-06-10 02:16:01 +00001065 u8 enc = pParse->db->enc;
danielk19777cedc8d2004-06-10 10:50:08 +00001066 u8 initbusy = pParse->db->init.busy;
1067 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, initbusy);
1068 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk1977466be562004-06-10 02:16:01 +00001069 /* No collation sequence of this type for this encoding is registered.
1070 ** Call the collation factory to see if it can supply us with one.
1071 */
danielk19777cedc8d2004-06-10 10:50:08 +00001072 callCollNeeded(pParse->db, zName, nName);
danielk1977466be562004-06-10 02:16:01 +00001073 pColl = sqlite3FindCollSeq(pParse->db, enc, zName, nName, 0);
danielk1977466be562004-06-10 02:16:01 +00001074 if( pColl && !pColl->xCmp ){
danielk19777cedc8d2004-06-10 10:50:08 +00001075 /* There may be a version of the collation sequence that requires
1076 ** translation between encodings. Search for it with synthCollSeq().
danielk1977466be562004-06-10 02:16:01 +00001077 */
danielk19777cedc8d2004-06-10 10:50:08 +00001078 if( synthCollSeq(pParse, pColl) ){
1079 return 0;
danielk1977466be562004-06-10 02:16:01 +00001080 }
1081 }
1082 }
1083
1084 /* If nothing has been found, write the error message into pParse */
danielk19777cedc8d2004-06-10 10:50:08 +00001085 if( !initbusy && (!pColl || !pColl->xCmp) ){
danielk19770202b292004-06-09 09:55:16 +00001086 if( pParse->nErr==0 ){
danielk1977466be562004-06-10 02:16:01 +00001087 sqlite3SetNString(&pParse->zErrMsg, "no such collation sequence: ", -1,
danielk19770202b292004-06-09 09:55:16 +00001088 zName, nName, 0);
1089 }
1090 pParse->nErr++;
danielk19777cedc8d2004-06-10 10:50:08 +00001091 pColl = 0;
danielk19770202b292004-06-09 09:55:16 +00001092 }
1093 return pColl;
1094}
1095
1096
1097
danielk1977a37cdde2004-05-16 11:15:36 +00001098/*
drh1ad3b9e2004-05-20 12:10:20 +00001099** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +00001100** associated affinity type.
1101*/
1102char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +00001103 int n, i;
1104 struct {
drh1ad3b9e2004-05-20 12:10:20 +00001105 const char *zSub; /* Keywords substring to search for */
1106 int nSub; /* length of zSub */
1107 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +00001108 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +00001109 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +00001110 {"CHAR", 4, SQLITE_AFF_TEXT},
1111 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +00001112 {"TEXT", 4, SQLITE_AFF_TEXT},
1113 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +00001114 };
1115
drh9c054832004-05-31 18:51:57 +00001116 if( nType==0 ){
1117 return SQLITE_AFF_NONE;
1118 }
drh1ad3b9e2004-05-20 12:10:20 +00001119 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
1120 int c1 = substrings[i].zSub[0];
1121 int c2 = tolower(c1);
1122 int limit = nType - substrings[i].nSub;
1123 const char *z = substrings[i].zSub;
1124 for(n=0; n<=limit; n++){
1125 int c = zType[n];
1126 if( (c==c1 || c==c2)
1127 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +00001128 return substrings[i].affinity;
1129 }
1130 }
1131 }
drh1ad3b9e2004-05-20 12:10:20 +00001132 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +00001133}
1134
1135/*
drh50e5dad2001-09-15 00:57:28 +00001136** Come up with a new random value for the schema cookie. Make sure
1137** the new value is different from the old.
1138**
1139** The schema cookie is used to determine when the schema for the
1140** database changes. After each schema change, the cookie value
1141** changes. When a process first reads the schema it records the
1142** cookie. Thereafter, whenever it goes to access the database,
1143** it checks the cookie to make sure the schema has not changed
1144** since it was last read.
1145**
1146** This plan is not completely bullet-proof. It is possible for
1147** the schema to change multiple times and for the cookie to be
1148** set back to prior value. But schema changes are infrequent
1149** and the probability of hitting the same cookie value is only
1150** 1 chance in 2^32. So we're safe enough.
1151*/
danielk1977cbb18d22004-05-28 11:37:27 +00001152void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00001153 unsigned char r;
1154 int *pSchemaCookie = &(db->aDb[iDb].schema_cookie);
1155
1156 sqlite3Randomness(1, &r);
1157 *pSchemaCookie = *pSchemaCookie + r + 1;
1158 db->flags |= SQLITE_InternChanges;
1159 sqlite3VdbeAddOp(v, OP_Integer, *pSchemaCookie, 0);
1160 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +00001161}
1162
1163/*
drh969fa7c2002-02-18 18:30:32 +00001164** Measure the number of characters needed to output the given
1165** identifier. The number returned includes any quotes used
1166** but does not include the null terminator.
1167*/
1168static int identLength(const char *z){
1169 int n;
drh17f71932002-02-21 12:01:27 +00001170 int needQuote = 0;
1171 for(n=0; *z; n++, z++){
1172 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +00001173 }
drh17f71932002-02-21 12:01:27 +00001174 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +00001175}
1176
1177/*
1178** Write an identifier onto the end of the given string. Add
1179** quote characters as needed.
1180*/
1181static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +00001182 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +00001183 i = *pIdx;
drh17f71932002-02-21 12:01:27 +00001184 for(j=0; zIdent[j]; j++){
1185 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
1186 }
1187 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +00001188 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh17f71932002-02-21 12:01:27 +00001189 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +00001190 for(j=0; zIdent[j]; j++){
1191 z[i++] = zIdent[j];
1192 if( zIdent[j]=='\'' ) z[i++] = '\'';
1193 }
drh17f71932002-02-21 12:01:27 +00001194 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +00001195 z[i] = 0;
1196 *pIdx = i;
1197}
1198
1199/*
1200** Generate a CREATE TABLE statement appropriate for the given
1201** table. Memory to hold the text of the statement is obtained
1202** from sqliteMalloc() and must be freed by the calling function.
1203*/
1204static char *createTableStmt(Table *p){
1205 int i, k, n;
1206 char *zStmt;
1207 char *zSep, *zSep2, *zEnd;
1208 n = 0;
1209 for(i=0; i<p->nCol; i++){
1210 n += identLength(p->aCol[i].zName);
danielk1977517eb642004-06-07 10:00:31 +00001211 if( p->aCol[i].zType ){
1212 n += (strlen(p->aCol[i].zType) + 1);
1213 }
drh969fa7c2002-02-18 18:30:32 +00001214 }
1215 n += identLength(p->zName);
1216 if( n<40 ){
1217 zSep = "";
1218 zSep2 = ",";
1219 zEnd = ")";
1220 }else{
1221 zSep = "\n ";
1222 zSep2 = ",\n ";
1223 zEnd = "\n)";
1224 }
drhe0bc4042002-06-25 01:09:11 +00001225 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001226 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001227 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001228 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001229 k = strlen(zStmt);
1230 identPut(zStmt, &k, p->zName);
1231 zStmt[k++] = '(';
1232 for(i=0; i<p->nCol; i++){
1233 strcpy(&zStmt[k], zSep);
1234 k += strlen(&zStmt[k]);
1235 zSep = zSep2;
1236 identPut(zStmt, &k, p->aCol[i].zName);
danielk1977517eb642004-06-07 10:00:31 +00001237 if( p->aCol[i].zType ){
1238 zStmt[k++] = ' ';
1239 strcpy(&zStmt[k], p->aCol[i].zType);
1240 k += strlen(p->aCol[i].zType);
1241 }
drh969fa7c2002-02-18 18:30:32 +00001242 }
1243 strcpy(&zStmt[k], zEnd);
1244 return zStmt;
1245}
1246
1247/*
drh75897232000-05-29 14:26:00 +00001248** This routine is called to report the final ")" that terminates
1249** a CREATE TABLE statement.
1250**
drhf57b3392001-10-08 13:22:32 +00001251** The table structure that other action routines have been building
1252** is added to the internal hash tables, assuming no errors have
1253** occurred.
drh75897232000-05-29 14:26:00 +00001254**
drh1d85d932004-02-14 23:05:52 +00001255** An entry for the table is made in the master table on disk, unless
1256** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001257** it means we are reading the sqlite_master table because we just
1258** connected to the database or because the sqlite_master table has
1259** recently changes, so the entry for this table already exists in
1260** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001261**
1262** If the pSelect argument is not NULL, it means that this routine
1263** was called to create a table generated from a
1264** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1265** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001266*/
danielk19774adee202004-05-08 08:23:19 +00001267void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001268 Table *p;
drhbe0072d2001-09-13 14:46:09 +00001269 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001270
danielk197724b03fd2004-05-10 10:34:34 +00001271 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001272 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001273 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001274
danielk1977517eb642004-06-07 10:00:31 +00001275 assert( !db->init.busy || !pSelect );
1276
drh969fa7c2002-02-18 18:30:32 +00001277 /* If the table is generated from a SELECT, then construct the
1278 ** list of columns and the text of the table.
1279 */
1280 if( pSelect ){
drh969fa7c2002-02-18 18:30:32 +00001281 }
1282
drh1d85d932004-02-14 23:05:52 +00001283 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001284 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1285 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001286 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001287 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001288 */
drh1d85d932004-02-14 23:05:52 +00001289 if( db->init.busy ){
1290 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001291 }
1292
drhe3c41372001-09-17 20:25:58 +00001293 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001294 ** in the SQLITE_MASTER table of the database. The record number
1295 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001296 **
drhe0bc4042002-06-25 01:09:11 +00001297 ** If this is a TEMPORARY table, write the entry into the auxiliary
1298 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001299 */
drh1d85d932004-02-14 23:05:52 +00001300 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001301 int n;
drhd8bc7082000-06-07 23:51:50 +00001302 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001303
danielk19774adee202004-05-08 08:23:19 +00001304 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001305 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001306
drh4ff6dfa2002-03-03 23:06:00 +00001307 if( p->pSelect==0 ){
1308 /* A regular table */
danielk19774adee202004-05-08 08:23:19 +00001309 sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
drh4ff6dfa2002-03-03 23:06:00 +00001310 }else{
1311 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001312 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001313 }
drh969fa7c2002-02-18 18:30:32 +00001314 p->tnum = 0;
danielk1977517eb642004-06-07 10:00:31 +00001315
1316 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1317
1318 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1319 ** statement to populate the new table. The root-page number for the
1320 ** new table is on the top of the vdbe stack.
1321 **
1322 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1323 ** suitable state to query for the column names and types to be used
1324 ** by the new table.
1325 */
1326 if( pSelect ){
1327 Table *pSelTab;
1328 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1329 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1330 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1331 pParse->nTab = 2;
1332 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1333 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1334 if( pParse->nErr==0 ){
1335 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1336 if( pSelTab==0 ) return;
1337 assert( p->aCol==0 );
1338 p->nCol = pSelTab->nCol;
1339 p->aCol = pSelTab->aCol;
1340 pSelTab->nCol = 0;
1341 pSelTab->aCol = 0;
1342 sqlite3DeleteTable(0, pSelTab);
1343 }
1344 }
1345
1346 sqlite3OpenMasterTable(v, p->iDb);
1347
1348 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view",P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001349 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1350 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk1977517eb642004-06-07 10:00:31 +00001351 sqlite3VdbeAddOp(v, OP_Pull, 3, 0);
1352
drhe0bc4042002-06-25 01:09:11 +00001353 if( pSelect ){
1354 char *z = createTableStmt(p);
1355 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001356 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001357 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001358 sqliteFree(z);
1359 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001360 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001361 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001362 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001363 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001364 }
drhe0bc4042002-06-25 01:09:11 +00001365 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001366 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001367 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001368 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
1369 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drhe0bc4042002-06-25 01:09:11 +00001370 }
danielk197784ac9d02004-05-18 09:58:06 +00001371 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001372 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk19771d850a72004-05-31 08:26:49 +00001373 if( p->iDb!=1 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001374 sqlite3ChangeCookie(db, v, p->iDb);
drhe0bc4042002-06-25 01:09:11 +00001375 }
danielk19774adee202004-05-08 08:23:19 +00001376 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
danielk1977517eb642004-06-07 10:00:31 +00001377
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001379 }
drh17e9e292003-02-01 13:53:28 +00001380
1381 /* Add the table to the in-memory representation of the database.
1382 */
drhd24cc422003-03-27 12:51:24 +00001383 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001384 Table *pOld;
1385 FKey *pFKey;
danielk19774adee202004-05-08 08:23:19 +00001386 pOld = sqlite3HashInsert(&db->aDb[p->iDb].tblHash,
drhd24cc422003-03-27 12:51:24 +00001387 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001388 if( pOld ){
1389 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1390 return;
1391 }
1392 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1393 int nTo = strlen(pFKey->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +00001394 pFKey->pNextTo = sqlite3HashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
1395 sqlite3HashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001396 }
1397 pParse->pNewTable = 0;
1398 db->nTable++;
1399 db->flags |= SQLITE_InternChanges;
1400 }
drh75897232000-05-29 14:26:00 +00001401}
1402
1403/*
drha76b5df2002-02-23 02:32:10 +00001404** The parser calls this routine in order to create a new VIEW
1405*/
danielk19774adee202004-05-08 08:23:19 +00001406void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001407 Parse *pParse, /* The parsing context */
1408 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001409 Token *pName1, /* The token that holds the name of the view */
1410 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001411 Select *pSelect, /* A SELECT statement that will become the new view */
1412 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001413){
drha76b5df2002-02-23 02:32:10 +00001414 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001415 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001416 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001417 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001418 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001419 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001420
danielk197748dec7e2004-05-28 12:33:30 +00001421 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001422 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001423 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001424 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001425 return;
1426 }
danielk1977ef2cb632004-05-29 02:37:19 +00001427 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001428 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1429 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001430 ){
danielk19774adee202004-05-08 08:23:19 +00001431 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001432 return;
1433 }
drh174b6192002-12-03 02:22:52 +00001434
drh4b59ab52002-08-24 18:24:51 +00001435 /* Make a copy of the entire SELECT statement that defines the view.
1436 ** This will force all the Expr.token.z values to be dynamically
1437 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001438 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001439 */
danielk19774adee202004-05-08 08:23:19 +00001440 p->pSelect = sqlite3SelectDup(pSelect);
1441 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001442 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001443 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001444 }
drh4b59ab52002-08-24 18:24:51 +00001445
1446 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1447 ** the end.
1448 */
drha76b5df2002-02-23 02:32:10 +00001449 sEnd = pParse->sLastToken;
1450 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1451 sEnd.z += sEnd.n;
1452 }
1453 sEnd.n = 0;
1454 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001455 z = pBegin->z;
1456 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1457 sEnd.z = &z[n-1];
1458 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001459
danielk19774adee202004-05-08 08:23:19 +00001460 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1461 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001462 return;
drh417be792002-03-03 18:59:40 +00001463}
drha76b5df2002-02-23 02:32:10 +00001464
drh417be792002-03-03 18:59:40 +00001465/*
1466** The Table structure pTable is really a VIEW. Fill in the names of
1467** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001468** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001469*/
danielk19774adee202004-05-08 08:23:19 +00001470int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001471 ExprList *pEList;
1472 Select *pSel;
1473 Table *pSelTab;
1474 int nErr = 0;
1475
1476 assert( pTable );
1477
1478 /* A positive nCol means the columns names for this view are
1479 ** already known.
1480 */
1481 if( pTable->nCol>0 ) return 0;
1482
1483 /* A negative nCol is a special marker meaning that we are currently
1484 ** trying to compute the column names. If we enter this routine with
1485 ** a negative nCol, it means two or more views form a loop, like this:
1486 **
1487 ** CREATE VIEW one AS SELECT * FROM two;
1488 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001489 **
1490 ** Actually, this error is caught previously and so the following test
1491 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001492 */
1493 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001494 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001495 return 1;
1496 }
1497
1498 /* If we get this far, it means we need to compute the table names.
1499 */
1500 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1501 pSel = pTable->pSelect;
1502
danielk19774adee202004-05-08 08:23:19 +00001503 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001504 ** "*" elements in this list. But we will need to restore the list
1505 ** back to its original configuration afterwards, so we save a copy of
1506 ** the original in pEList.
1507 */
1508 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001509 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001510 if( pSel->pEList==0 ){
1511 pSel->pEList = pEList;
1512 return 1; /* Malloc failed */
1513 }
1514 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001515 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001516 if( pSelTab ){
1517 assert( pTable->aCol==0 );
1518 pTable->nCol = pSelTab->nCol;
1519 pTable->aCol = pSelTab->aCol;
1520 pSelTab->nCol = 0;
1521 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001522 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001523 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001524 }else{
1525 pTable->nCol = 0;
1526 nErr++;
1527 }
danielk19774adee202004-05-08 08:23:19 +00001528 sqlite3SelectUnbind(pSel);
1529 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001530 pSel->pEList = pEList;
1531 return nErr;
1532}
1533
1534/*
1535** Clear the column names from the VIEW pTable.
1536**
1537** This routine is called whenever any other table or view is modified.
1538** The view passed into this routine might depend directly or indirectly
1539** on the modified or deleted table so we need to clear the old column
1540** names so that they will be recomputed.
1541*/
1542static void sqliteViewResetColumnNames(Table *pTable){
1543 int i;
drh701a0ae2004-02-22 20:05:00 +00001544 Column *pCol;
1545 assert( pTable!=0 && pTable->pSelect!=0 );
1546 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1547 sqliteFree(pCol->zName);
1548 sqliteFree(pCol->zDflt);
1549 sqliteFree(pCol->zType);
drh417be792002-03-03 18:59:40 +00001550 }
1551 sqliteFree(pTable->aCol);
1552 pTable->aCol = 0;
1553 pTable->nCol = 0;
1554}
1555
1556/*
drh8bf8dc92003-05-17 17:35:10 +00001557** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001558*/
drhd24cc422003-03-27 12:51:24 +00001559static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001560 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001561 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001562 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001563 Table *pTab = sqliteHashData(i);
1564 if( pTab->pSelect ){
1565 sqliteViewResetColumnNames(pTab);
1566 }
1567 }
drh8bf8dc92003-05-17 17:35:10 +00001568 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001569}
1570
1571/*
drh75897232000-05-29 14:26:00 +00001572** Given a token, look up a table with that name. If not found, leave
1573** an error for the parser to find and return NULL.
1574*/
danielk19774adee202004-05-08 08:23:19 +00001575Table *sqlite3TableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001576 char *zName;
1577 Table *pTab;
danielk19774adee202004-05-08 08:23:19 +00001578 zName = sqlite3TableNameFromToken(pTok);
drhdaffd0e2001-04-11 14:28:42 +00001579 if( zName==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001580 pTab = sqlite3FindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001581 sqliteFree(zName);
1582 if( pTab==0 ){
danielk19774adee202004-05-08 08:23:19 +00001583 sqlite3ErrorMsg(pParse, "no such table: %T", pTok);
drha6ecd332004-06-10 00:29:09 +00001584 pParse->checkSchema = 1;
drh75897232000-05-29 14:26:00 +00001585 }
1586 return pTab;
1587}
1588
1589/*
1590** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001591** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001592*/
danielk1977a8858102004-05-28 12:11:21 +00001593void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1594 Table *pTab;
drh75897232000-05-29 14:26:00 +00001595 Vdbe *v;
1596 int base;
drh5edc3122001-09-13 21:53:09 +00001597 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001598 int iDb;
drh75897232000-05-29 14:26:00 +00001599
danielk1977a8858102004-05-28 12:11:21 +00001600 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1601 assert( pName->nSrc==1 );
1602 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1603
1604 if( pTab==0 ) goto exit_drop_table;
1605 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001606 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001607#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001608 {
1609 int code;
danielk1977a8858102004-05-28 12:11:21 +00001610 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1611 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001612 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001613 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001614 }
drhe5f9c642003-01-13 23:27:31 +00001615 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001616 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001617 code = SQLITE_DROP_TEMP_VIEW;
1618 }else{
1619 code = SQLITE_DROP_VIEW;
1620 }
1621 }else{
drhd24cc422003-03-27 12:51:24 +00001622 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001623 code = SQLITE_DROP_TEMP_TABLE;
1624 }else{
1625 code = SQLITE_DROP_TABLE;
1626 }
1627 }
danielk1977a8858102004-05-28 12:11:21 +00001628 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1629 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001630 }
danielk1977a8858102004-05-28 12:11:21 +00001631 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1632 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001633 }
drhe5f9c642003-01-13 23:27:31 +00001634 }
1635#endif
danielk1977a8858102004-05-28 12:11:21 +00001636 if( pTab->readOnly ){
1637 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001638 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001639 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001640 }
danielk1977a8858102004-05-28 12:11:21 +00001641 if( isView && pTab->pSelect==0 ){
1642 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1643 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001644 }
danielk1977a8858102004-05-28 12:11:21 +00001645 if( !isView && pTab->pSelect ){
1646 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1647 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001648 }
drh75897232000-05-29 14:26:00 +00001649
drh1ccde152000-06-17 13:12:39 +00001650 /* Generate code to remove the table from the master table
1651 ** on disk.
1652 */
danielk19774adee202004-05-08 08:23:19 +00001653 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001654 if( v ){
drh905793e2004-02-21 13:31:09 +00001655 static VdbeOpList dropTable[] = {
danielk19778e227872004-06-07 07:52:17 +00001656 { OP_Rewind, 0, ADDR(13), 0},
1657 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001658 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001659 { OP_MemLoad, 1, 0, 0}, /* 3 */
danielk19778e227872004-06-07 07:52:17 +00001660 { OP_Column, 0, 2, 0}, /* sqlite_master.tbl_name */
1661 { OP_Ne, 0, ADDR(12), 0},
1662 { OP_String8, 0, 0, "trigger"},
1663 { OP_Column, 0, 2, 0}, /* sqlite_master.type */
1664 { OP_Eq, 0, ADDR(12), 0},
drh75897232000-05-29 14:26:00 +00001665 { OP_Delete, 0, 0, 0},
danielk19778e227872004-06-07 07:52:17 +00001666 { OP_Rewind, 0, ADDR(13), 0},
danielk19778d059842004-05-12 11:24:02 +00001667 { OP_Goto, 0, ADDR(3), 0},
danielk19778e227872004-06-07 07:52:17 +00001668 { OP_Next, 0, ADDR(3), 0}, /* 12 */
drh75897232000-05-29 14:26:00 +00001669 };
1670 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001671 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001672 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001673
danielk19778e227872004-06-07 07:52:17 +00001674 /* Drop all triggers associated with the table being dropped. Code
1675 ** is generated to remove entries from sqlite_master and/or
1676 ** sqlite_temp_master if required.
1677 */
danielk1977a8858102004-05-28 12:11:21 +00001678 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001679 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001680 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001681 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drhe0bc4042002-06-25 01:09:11 +00001682 if( pParse->explain ){
1683 pTrigger = pTrigger->pNext;
1684 }else{
danielk1977a8858102004-05-28 12:11:21 +00001685 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001686 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001687 }
drh8bf8dc92003-05-17 17:35:10 +00001688
danielk19778e227872004-06-07 07:52:17 +00001689 /* Drop all SQLITE_MASTER table and index entries that refer to the
1690 ** table. The program name loops through the master table and deletes
1691 ** every row that refers to a table of the same name as the one being
1692 ** dropped. Triggers are handled seperately because a trigger can be
1693 ** created in the temp database that refers to a table in another
1694 ** database.
1695 */
danielk1977a8858102004-05-28 12:11:21 +00001696 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001697 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001698 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
danielk19778e227872004-06-07 07:52:17 +00001699 sqlite3ChangeCookie(db, v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001700 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001701 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001702 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1703 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001704 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001705 }
drh5e00f6c2001-09-13 13:46:56 +00001706 }
danielk19774adee202004-05-08 08:23:19 +00001707 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001708 }
1709
drhe0bc4042002-06-25 01:09:11 +00001710 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001711 **
1712 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001713 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001714 */
1715 if( !pParse->explain ){
danielk1977a8858102004-05-28 12:11:21 +00001716 sqliteUnlinkAndDeleteTable(db, pTab);
drh5edc3122001-09-13 21:53:09 +00001717 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001718 }
drhd24cc422003-03-27 12:51:24 +00001719 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001720
1721exit_drop_table:
1722 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001723}
1724
1725/*
drhc2eef3b2002-08-31 18:53:06 +00001726** This routine is called to create a new foreign key on the table
1727** currently under construction. pFromCol determines which columns
1728** in the current table point to the foreign key. If pFromCol==0 then
1729** connect the key to the last column inserted. pTo is the name of
1730** the table referred to. pToCol is a list of tables in the other
1731** pTo table that the foreign key points to. flags contains all
1732** information about the conflict resolution algorithms specified
1733** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1734**
1735** An FKey structure is created and added to the table currently
1736** under construction in the pParse->pNewTable field. The new FKey
1737** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001738** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001739**
1740** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001741** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001742*/
danielk19774adee202004-05-08 08:23:19 +00001743void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001744 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001745 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001746 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001747 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001748 int flags /* Conflict resolution algorithms. */
1749){
1750 Table *p = pParse->pNewTable;
1751 int nByte;
1752 int i;
1753 int nCol;
1754 char *z;
1755 FKey *pFKey = 0;
1756
1757 assert( pTo!=0 );
1758 if( p==0 || pParse->nErr ) goto fk_end;
1759 if( pFromCol==0 ){
1760 int iCol = p->nCol-1;
1761 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001762 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001763 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001764 " should reference only one column of table %T",
1765 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001766 goto fk_end;
1767 }
1768 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001769 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001770 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001771 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001772 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001773 goto fk_end;
1774 }else{
danielk19770202b292004-06-09 09:55:16 +00001775 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001776 }
1777 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1778 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001779 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001780 nByte += strlen(pToCol->a[i].zName) + 1;
1781 }
1782 }
1783 pFKey = sqliteMalloc( nByte );
1784 if( pFKey==0 ) goto fk_end;
1785 pFKey->pFrom = p;
1786 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001787 z = (char*)&pFKey[1];
1788 pFKey->aCol = (struct sColMap*)z;
1789 z += sizeof(struct sColMap)*nCol;
1790 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001791 memcpy(z, pTo->z, pTo->n);
1792 z[pTo->n] = 0;
1793 z += pTo->n+1;
1794 pFKey->pNextTo = 0;
1795 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001796 if( pFromCol==0 ){
1797 pFKey->aCol[0].iFrom = p->nCol-1;
1798 }else{
1799 for(i=0; i<nCol; i++){
1800 int j;
1801 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001802 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001803 pFKey->aCol[i].iFrom = j;
1804 break;
1805 }
1806 }
1807 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001808 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001809 "unknown column \"%s\" in foreign key definition",
1810 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001811 goto fk_end;
1812 }
1813 }
1814 }
1815 if( pToCol ){
1816 for(i=0; i<nCol; i++){
1817 int n = strlen(pToCol->a[i].zName);
1818 pFKey->aCol[i].zCol = z;
1819 memcpy(z, pToCol->a[i].zName, n);
1820 z[n] = 0;
1821 z += n+1;
1822 }
1823 }
1824 pFKey->isDeferred = 0;
1825 pFKey->deleteConf = flags & 0xff;
1826 pFKey->updateConf = (flags >> 8 ) & 0xff;
1827 pFKey->insertConf = (flags >> 16 ) & 0xff;
1828
1829 /* Link the foreign key to the table as the last step.
1830 */
1831 p->pFKey = pFKey;
1832 pFKey = 0;
1833
1834fk_end:
1835 sqliteFree(pFKey);
danielk19770202b292004-06-09 09:55:16 +00001836 sqlite3ExprListDelete(pFromCol);
1837 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001838}
1839
1840/*
1841** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1842** clause is seen as part of a foreign key definition. The isDeferred
1843** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1844** The behavior of the most recently created foreign key is adjusted
1845** accordingly.
1846*/
danielk19774adee202004-05-08 08:23:19 +00001847void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001848 Table *pTab;
1849 FKey *pFKey;
1850 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1851 pFKey->isDeferred = isDeferred;
1852}
1853
1854/*
drh75897232000-05-29 14:26:00 +00001855** Create a new index for an SQL table. pIndex is the name of the index
1856** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001857** be NULL for a primary key or an index that is created to satisfy a
1858** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001859** as the table to be indexed. pParse->pNewTable is a table that is
1860** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001861**
drh382c0242001-10-06 16:33:02 +00001862** pList is a list of columns to be indexed. pList will be NULL if this
1863** is a primary key or unique-constraint on the most recent column added
1864** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001865*/
danielk19774adee202004-05-08 08:23:19 +00001866void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001867 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001868 Token *pName1, /* First part of index name. May be NULL */
1869 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00001870 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
1871 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001872 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001873 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1874 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1875){
danielk1977cbb18d22004-05-28 11:37:27 +00001876 Table *pTab = 0; /* Table to be indexed */
danielk1977d8123362004-06-12 09:25:12 +00001877 Index *pIndex = 0; /* The index to be created */
drh75897232000-05-29 14:26:00 +00001878 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001879 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001880 Token nullId; /* Fake token for an empty ID list */
1881 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001882 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001883 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001884
danielk1977cbb18d22004-05-28 11:37:27 +00001885 int iDb; /* Index of the database that is being written */
1886 Token *pName = 0; /* Unqualified name of the index to create */
1887
danielk197724b03fd2004-05-10 10:34:34 +00001888 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001889
drh75897232000-05-29 14:26:00 +00001890 /*
1891 ** Find the table that is to be indexed. Return early if not found.
1892 */
danielk1977cbb18d22004-05-28 11:37:27 +00001893 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001894
1895 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001896 ** to search for the table. 'Fix' the table name to this db
1897 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001898 */
1899 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001900 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001901 if( iDb<0 ) goto exit_create_index;
1902
danielk1977ef2cb632004-05-29 02:37:19 +00001903 /* If the index name was unqualified, check if the the table
1904 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001905 */
danielk1977ef2cb632004-05-29 02:37:19 +00001906 pTab = sqlite3SrcListLookup(pParse, pTblName);
1907 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1908 iDb = 1;
1909 }
1910
1911 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1912 sqlite3FixSrcList(&sFix, pTblName)
1913 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001914 goto exit_create_index;
1915 }
danielk1977ef2cb632004-05-29 02:37:19 +00001916 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1917 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001918 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001919 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001920 }else{
drhe3c41372001-09-17 20:25:58 +00001921 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001922 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001923 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001924 }
danielk1977cbb18d22004-05-28 11:37:27 +00001925
drh75897232000-05-29 14:26:00 +00001926 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001927 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001928 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001929 goto exit_create_index;
1930 }
drha76b5df2002-02-23 02:32:10 +00001931 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001932 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001933 goto exit_create_index;
1934 }
drh4925ca02003-11-27 00:48:57 +00001935 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001936
1937 /*
1938 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001939 ** index or table with the same name.
1940 **
1941 ** Exception: If we are reading the names of permanent indices from the
1942 ** sqlite_master table (because some other process changed the schema) and
1943 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001944 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001945 **
1946 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001947 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1948 ** own name.
drh75897232000-05-29 14:26:00 +00001949 */
danielk1977d8123362004-06-12 09:25:12 +00001950 if( pName ){
1951 zName = sqlite3TableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001952 if( zName==0 ) goto exit_create_index;
danielk1977d8123362004-06-12 09:25:12 +00001953 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
drhd24cc422003-03-27 12:51:24 +00001954 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001955 }
danielk1977d8123362004-06-12 09:25:12 +00001956 if( !db->init.busy ){
1957 Index *pISameName; /* Another index with the same name */
1958 Table *pTSameName; /* A table with same name as the index */
1959 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
1960 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
1961 goto exit_create_index;
1962 }
1963 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1964 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
1965 goto exit_create_index;
1966 }
drhe3c41372001-09-17 20:25:58 +00001967 }
drhd24cc422003-03-27 12:51:24 +00001968 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001969 char zBuf[30];
1970 int n;
1971 Index *pLoop;
1972 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
danielk1977d8123362004-06-12 09:25:12 +00001973 sprintf(zBuf,"_%d",n);
drh75897232000-05-29 14:26:00 +00001974 zName = 0;
danielk1977d8123362004-06-12 09:25:12 +00001975 sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001976 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +00001977 }
1978
drhe5f9c642003-01-13 23:27:31 +00001979 /* Check for authorization to create an index.
1980 */
1981#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001982 {
1983 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001984 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001985 goto exit_create_index;
1986 }
1987 i = SQLITE_CREATE_INDEX;
1988 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001989 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001990 goto exit_create_index;
1991 }
drhe5f9c642003-01-13 23:27:31 +00001992 }
1993#endif
1994
drh75897232000-05-29 14:26:00 +00001995 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001996 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001997 ** So create a fake list to simulate this.
1998 */
1999 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00002000 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00002001 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00002002 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00002003 if( pList==0 ) goto exit_create_index;
2004 }
2005
2006 /*
2007 ** Allocate the index structure.
2008 */
drhdcc581c2000-05-30 13:44:19 +00002009 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00002010 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00002011 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00002012 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
2013 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00002014 strcpy(pIndex->zName, zName);
2015 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00002016 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00002017 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00002018 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00002019 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00002020
drh1ccde152000-06-17 13:12:39 +00002021 /* Scan the names of the columns of the table to be indexed and
2022 ** load the column indices into the Index structure. Report an error
2023 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00002024 */
danielk19770202b292004-06-09 09:55:16 +00002025 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00002026 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00002027 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00002028 }
2029 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00002030 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00002031 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002032 goto exit_create_index;
2033 }
drh967e8b72000-06-21 13:59:10 +00002034 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00002035 if( pList->a[i].pExpr ){
2036 assert( pList->a[i].pExpr->pColl );
2037 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
2038 }else{
2039 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
2040 }
2041 assert( pIndex->keyInfo.aColl[i] );
danielk19777cedc8d2004-06-10 10:50:08 +00002042 if( !db->init.busy &&
2043 sqlite3CheckCollSeq(pParse, pIndex->keyInfo.aColl[i])
2044 ){
2045 goto exit_create_index;
2046 }
drh75897232000-05-29 14:26:00 +00002047 }
danielk19770202b292004-06-09 09:55:16 +00002048 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00002049
danielk1977d8123362004-06-12 09:25:12 +00002050 if( pTab==pParse->pNewTable ){
2051 /* This routine has been called to create an automatic index as a
2052 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
2053 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
2054 ** i.e. one of:
2055 **
2056 ** CREATE TABLE t(x PRIMARY KEY, y);
2057 ** CREATE TABLE t(x, y, UNIQUE(x, y));
2058 **
2059 ** Either way, check to see if the table already has such an index. If
2060 ** so, don't bother creating this one. This only applies to
2061 ** automatically created indices. Users can do as they wish with
2062 ** explicit indices.
2063 */
2064 Index *pIdx;
2065 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2066 int k;
2067 assert( pIdx->onError!=OE_None );
2068 assert( pIdx->autoIndex );
2069 assert( pIndex->onError!=OE_None );
2070
2071 if( pIdx->nColumn!=pIndex->nColumn ) continue;
2072 for(k=0; k<pIdx->nColumn; k++){
2073 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
2074 if( pIdx->keyInfo.aColl[k]!=pIndex->keyInfo.aColl[k] ) break;
2075 }
2076 if( k==pIdx->nColumn ){
danielk1977f736b772004-06-17 06:13:34 +00002077 if( pIdx->onError!=pIndex->onError ){
2078 /* This constraint creates the same index as a previous
2079 ** constraint specified somewhere in the CREATE TABLE statement.
2080 ** However the ON CONFLICT clauses are different. If both this
2081 ** constraint and the previous equivalent constraint have explicit
2082 ** ON CONFLICT clauses this is an error. Otherwise, use the
2083 ** explicitly specified behaviour for the index.
2084 */
2085 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
2086 sqlite3ErrorMsg(pParse,
2087 "conflicting ON CONFLICT clauses specified", 0);
2088 }
2089 if( pIdx->onError==OE_Default ){
2090 pIdx->onError = pIndex->onError;
2091 }
2092 }
danielk1977d8123362004-06-12 09:25:12 +00002093 goto exit_create_index;
2094 }
2095 }
2096 }
2097
drh75897232000-05-29 14:26:00 +00002098 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00002099 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00002100 */
drhd24cc422003-03-27 12:51:24 +00002101 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00002102 Index *p;
danielk19774adee202004-05-08 08:23:19 +00002103 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00002104 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00002105 if( p ){
2106 assert( p==pIndex ); /* Malloc must have failed */
drh6d4abfb2001-10-22 02:58:08 +00002107 goto exit_create_index;
2108 }
drh5e00f6c2001-09-13 13:46:56 +00002109 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00002110 }
drh9cfcf5d2002-01-29 18:41:24 +00002111
drh1d85d932004-02-14 23:05:52 +00002112 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhd78eeee2001-09-13 16:18:53 +00002113 ** "sqlite_master" table on the disk. So do not write to the disk
drh1d85d932004-02-14 23:05:52 +00002114 ** again. Extract the table number from the db->init.newTnum field.
drhd78eeee2001-09-13 16:18:53 +00002115 */
danielk1977cbb18d22004-05-28 11:37:27 +00002116 if( db->init.busy && pTblName!=0 ){
drh1d85d932004-02-14 23:05:52 +00002117 pIndex->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00002118 }
2119
drh1d85d932004-02-14 23:05:52 +00002120 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00002121 ** involves writing the index into the master table and filling in the
2122 ** index with the current table contents.
2123 **
drh1d85d932004-02-14 23:05:52 +00002124 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
2125 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00002126 ** CREATE INDEX statements are read out of the master table. In
2127 ** the latter case the index already exists on disk, which is why
2128 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00002129 **
danielk1977cbb18d22004-05-28 11:37:27 +00002130 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00002131 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
2132 ** has just been created, it contains no data and the index initialization
2133 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00002134 */
drh1d85d932004-02-14 23:05:52 +00002135 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00002136 int n;
drhadbca9c2001-09-27 15:11:53 +00002137 Vdbe *v;
drh75897232000-05-29 14:26:00 +00002138 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00002139
danielk19774adee202004-05-08 08:23:19 +00002140 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002141 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00002142 if( pTblName!=0 ){
2143 sqlite3BeginWriteOperation(pParse, 0, iDb);
2144 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00002145 }
danielk19774adee202004-05-08 08:23:19 +00002146 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00002147 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
2148 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
2149 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002150 sqlite3VdbeOp3(v, OP_CreateIndex, 0, iDb,(char*)&pIndex->tnum,P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00002151 pIndex->tnum = 0;
danielk1977cbb18d22004-05-28 11:37:27 +00002152 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00002153 sqlite3VdbeCode(v,
drh701a0ae2004-02-22 20:05:00 +00002154 OP_Dup, 0, 0,
danielk1977cbb18d22004-05-28 11:37:27 +00002155 OP_Integer, iDb, 0,
drh701a0ae2004-02-22 20:05:00 +00002156 0);
drhd3d39e92004-05-20 22:16:29 +00002157 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
2158 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00002159 }
danielk19770f69c1e2004-05-29 11:24:50 +00002160 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00002161 if( pStart && pEnd ){
danielk19770202b292004-06-09 09:55:16 +00002162 if( onError==OE_None ){
2163 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
2164 }else{
2165 sqlite3VdbeChangeP3(v, -1, "CREATE UNIQUE INDEX ", P3_STATIC);
2166 }
danielk19770f69c1e2004-05-29 11:24:50 +00002167 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002168 n = Addr(pEnd->z) - Addr(pName->z) + 1;
2169 sqlite3VdbeChangeP3(v, -1, pName->z, n);
2170 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh75897232000-05-29 14:26:00 +00002171 }
danielk197784ac9d02004-05-18 09:58:06 +00002172 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002173 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002174 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00002175 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00002176 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
2177 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00002178 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00002179 lbl2 = sqlite3VdbeMakeLabel(v);
2180 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00002181 lbl1 = sqlite3VdbeCurrentAddr(v);
2182 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00002183 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00002184 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00002185 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
2186 sqlite3VdbeResolveLabel(v, lbl2);
2187 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
2188 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00002189 }
danielk1977cbb18d22004-05-28 11:37:27 +00002190 if( pTblName!=0 ){
drhf57b3392001-10-08 13:22:32 +00002191 if( !isTemp ){
danielk1977cbb18d22004-05-28 11:37:27 +00002192 sqlite3ChangeCookie(db, v, iDb);
drhf57b3392001-10-08 13:22:32 +00002193 }
danielk19774adee202004-05-08 08:23:19 +00002194 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2195 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002196 }
drh75897232000-05-29 14:26:00 +00002197 }
2198
danielk1977d8123362004-06-12 09:25:12 +00002199 /* When adding an index to the list of indices for a table, make
2200 ** sure all indices labeled OE_Replace come after all those labeled
2201 ** OE_Ignore. This is necessary for the correct operation of UPDATE
2202 ** and INSERT.
2203 */
2204 if( onError!=OE_Replace || pTab->pIndex==0
2205 || pTab->pIndex->onError==OE_Replace){
2206 pIndex->pNext = pTab->pIndex;
2207 pTab->pIndex = pIndex;
2208 }else{
2209 Index *pOther = pTab->pIndex;
2210 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
2211 pOther = pOther->pNext;
2212 }
2213 pIndex->pNext = pOther->pNext;
2214 pOther->pNext = pIndex;
2215 }
2216 pIndex = 0;
2217
drh75897232000-05-29 14:26:00 +00002218 /* Clean up before exiting */
2219exit_create_index:
danielk1977d8123362004-06-12 09:25:12 +00002220 if( pIndex ) sqliteFree(pIndex);
danielk19770202b292004-06-09 09:55:16 +00002221 sqlite3ExprListDelete(pList);
danielk1977e0048402004-06-15 16:51:01 +00002222 sqlite3SrcListDelete(pTblName);
drh75897232000-05-29 14:26:00 +00002223 sqliteFree(zName);
2224 return;
2225}
2226
2227/*
drh74e24cd2002-01-09 03:19:59 +00002228** This routine will drop an existing named index. This routine
2229** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00002230*/
danielk19774adee202004-05-08 08:23:19 +00002231void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00002232 Index *pIndex;
drh75897232000-05-29 14:26:00 +00002233 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00002234 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00002235
danielk197724b03fd2004-05-10 10:34:34 +00002236 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00002237 assert( pName->nSrc==1 );
danielk19774adee202004-05-08 08:23:19 +00002238 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00002239 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00002240 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00002241 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00002242 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00002243 }
drh485b39b2002-07-13 03:11:52 +00002244 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00002245 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00002246 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00002247 goto exit_drop_index;
2248 }
danielk1977a8858102004-05-28 12:11:21 +00002249/*
drhd24cc422003-03-27 12:51:24 +00002250 if( pIndex->iDb>1 ){
danielk19774adee202004-05-08 08:23:19 +00002251 sqlite3ErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00002252 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00002253 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00002254 }
danielk1977a8858102004-05-28 12:11:21 +00002255*/
drhe5f9c642003-01-13 23:27:31 +00002256#ifndef SQLITE_OMIT_AUTHORIZATION
2257 {
2258 int code = SQLITE_DROP_INDEX;
2259 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002260 const char *zDb = db->aDb[pIndex->iDb].zName;
2261 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002262 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002263 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002264 }
drhd24cc422003-03-27 12:51:24 +00002265 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002266 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002267 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002268 }
drhed6c8672003-01-12 18:02:16 +00002269 }
drhe5f9c642003-01-13 23:27:31 +00002270#endif
drh75897232000-05-29 14:26:00 +00002271
2272 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002273 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002274 if( v ){
drh905793e2004-02-21 13:31:09 +00002275 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00002276 { OP_Rewind, 0, ADDR(9), 0},
danielk19770f69c1e2004-05-29 11:24:50 +00002277 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00002278 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002279 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00002280 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002281 { OP_Eq, 0, ADDR(8), 0},
2282 { OP_Next, 0, ADDR(3), 0},
2283 { OP_Goto, 0, ADDR(9), 0},
2284 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00002285 };
2286 int base;
2287
danielk19774adee202004-05-08 08:23:19 +00002288 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
2289 sqlite3OpenMasterTable(v, pIndex->iDb);
2290 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
2291 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002292 if( pIndex->iDb!=1 ){
2293 sqlite3ChangeCookie(db, v, pIndex->iDb);
drhf57b3392001-10-08 13:22:32 +00002294 }
danielk19774adee202004-05-08 08:23:19 +00002295 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2296 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
2297 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00002298 }
2299
drhe0bc4042002-06-25 01:09:11 +00002300 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00002301 */
2302 if( !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +00002303 sqlite3UnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00002304 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00002305 }
drhd24cc422003-03-27 12:51:24 +00002306
2307exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002308 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002309}
2310
2311/*
drh75897232000-05-29 14:26:00 +00002312** Append a new element to the given IdList. Create a new IdList if
2313** need be.
drhdaffd0e2001-04-11 14:28:42 +00002314**
2315** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002316*/
danielk19774adee202004-05-08 08:23:19 +00002317IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002318 if( pList==0 ){
2319 pList = sqliteMalloc( sizeof(IdList) );
2320 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002321 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002322 }
drh4305d102003-07-30 12:34:12 +00002323 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002324 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002325 pList->nAlloc = pList->nAlloc*2 + 5;
2326 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002327 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002328 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002329 return 0;
drh75897232000-05-29 14:26:00 +00002330 }
drh6d4abfb2001-10-22 02:58:08 +00002331 pList->a = a;
drh75897232000-05-29 14:26:00 +00002332 }
2333 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
2334 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00002335 char **pz = &pList->a[pList->nId].zName;
danielk19774adee202004-05-08 08:23:19 +00002336 sqlite3SetNString(pz, pToken->z, pToken->n, 0);
drhdaffd0e2001-04-11 14:28:42 +00002337 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002338 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002339 return 0;
2340 }else{
danielk19774adee202004-05-08 08:23:19 +00002341 sqlite3Dequote(*pz);
drhdaffd0e2001-04-11 14:28:42 +00002342 }
drh75897232000-05-29 14:26:00 +00002343 }
2344 pList->nId++;
2345 return pList;
2346}
2347
2348/*
drhad3cab52002-05-24 02:04:32 +00002349** Append a new table name to the given SrcList. Create a new SrcList if
2350** need be. A new entry is created in the SrcList even if pToken is NULL.
2351**
2352** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002353**
2354** If pDatabase is not null, it means that the table has an optional
2355** database name prefix. Like this: "database.table". The pDatabase
2356** points to the table name and the pTable points to the database name.
2357** The SrcList.a[].zName field is filled with the table name which might
2358** come from pTable (if pDatabase is NULL) or from pDatabase.
2359** SrcList.a[].zDatabase is filled with the database name from pTable,
2360** or with NULL if no database is specified.
2361**
2362** In other words, if call like this:
2363**
danielk19774adee202004-05-08 08:23:19 +00002364** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002365**
2366** Then B is a table name and the database name is unspecified. If called
2367** like this:
2368**
danielk19774adee202004-05-08 08:23:19 +00002369** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002370**
2371** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002372*/
danielk19774adee202004-05-08 08:23:19 +00002373SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00002374 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002375 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002376 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002377 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002378 }
drh4305d102003-07-30 12:34:12 +00002379 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002380 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002381 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002382 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002383 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002384 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002385 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002386 return 0;
2387 }
drh113088e2003-03-20 01:16:58 +00002388 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002389 }
2390 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002391 if( pDatabase && pDatabase->z==0 ){
2392 pDatabase = 0;
2393 }
2394 if( pDatabase && pTable ){
2395 Token *pTemp = pDatabase;
2396 pDatabase = pTable;
2397 pTable = pTemp;
2398 }
2399 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00002400 char **pz = &pList->a[pList->nSrc].zName;
danielk19774adee202004-05-08 08:23:19 +00002401 sqlite3SetNString(pz, pTable->z, pTable->n, 0);
drh113088e2003-03-20 01:16:58 +00002402 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002403 sqlite3SrcListDelete(pList);
drh113088e2003-03-20 01:16:58 +00002404 return 0;
2405 }else{
danielk19774adee202004-05-08 08:23:19 +00002406 sqlite3Dequote(*pz);
drh113088e2003-03-20 01:16:58 +00002407 }
2408 }
2409 if( pDatabase ){
2410 char **pz = &pList->a[pList->nSrc].zDatabase;
danielk19774adee202004-05-08 08:23:19 +00002411 sqlite3SetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00002412 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002413 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002414 return 0;
2415 }else{
danielk19774adee202004-05-08 08:23:19 +00002416 sqlite3Dequote(*pz);
drhad3cab52002-05-24 02:04:32 +00002417 }
2418 }
drh63eb5f22003-04-29 16:20:44 +00002419 pList->a[pList->nSrc].iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002420 pList->nSrc++;
2421 return pList;
2422}
2423
2424/*
drh63eb5f22003-04-29 16:20:44 +00002425** Assign cursors to all tables in a SrcList
2426*/
danielk19774adee202004-05-08 08:23:19 +00002427void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002428 int i;
2429 for(i=0; i<pList->nSrc; i++){
2430 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002431 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002432 }
2433 }
2434}
2435
2436/*
drh75897232000-05-29 14:26:00 +00002437** Add an alias to the last identifier on the given identifier list.
2438*/
danielk19774adee202004-05-08 08:23:19 +00002439void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002440 if( pList && pList->nSrc>0 ){
2441 int i = pList->nSrc - 1;
danielk19774adee202004-05-08 08:23:19 +00002442 sqlite3SetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
2443 sqlite3Dequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00002444 }
2445}
2446
2447/*
drhad3cab52002-05-24 02:04:32 +00002448** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002449*/
danielk19774adee202004-05-08 08:23:19 +00002450void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002451 int i;
2452 if( pList==0 ) return;
2453 for(i=0; i<pList->nId; i++){
2454 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002455 }
2456 sqliteFree(pList->a);
2457 sqliteFree(pList);
2458}
2459
2460/*
drhad2d8302002-05-24 20:31:36 +00002461** Return the index in pList of the identifier named zId. Return -1
2462** if not found.
2463*/
danielk19774adee202004-05-08 08:23:19 +00002464int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002465 int i;
2466 if( pList==0 ) return -1;
2467 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002468 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002469 }
2470 return -1;
2471}
2472
2473/*
drhad3cab52002-05-24 02:04:32 +00002474** Delete an entire SrcList including all its substructure.
2475*/
danielk19774adee202004-05-08 08:23:19 +00002476void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002477 int i;
2478 if( pList==0 ) return;
2479 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002480 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002481 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002482 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002483 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
danielk19774adee202004-05-08 08:23:19 +00002484 sqlite3DeleteTable(0, pList->a[i].pTab);
drhdaffd0e2001-04-11 14:28:42 +00002485 }
danielk19774adee202004-05-08 08:23:19 +00002486 sqlite3SelectDelete(pList->a[i].pSelect);
2487 sqlite3ExprDelete(pList->a[i].pOn);
2488 sqlite3IdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002489 }
drh75897232000-05-29 14:26:00 +00002490 sqliteFree(pList);
2491}
2492
drh982cef72000-05-30 16:27:03 +00002493/*
drhc4a3c772001-04-04 11:48:57 +00002494** Begin a transaction
2495*/
danielk197733752f82004-05-31 08:55:33 +00002496void sqlite3BeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002497 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002498 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002499
drh001bbcb2003-03-19 03:14:00 +00002500 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002501 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002502 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002503
2504 v = sqlite3GetVdbe(pParse);
2505 if( !v ) return;
2506 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002507}
2508
2509/*
2510** Commit a transaction
2511*/
danielk19774adee202004-05-08 08:23:19 +00002512void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002513 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002514 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002515
drh001bbcb2003-03-19 03:14:00 +00002516 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002517 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002518 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002519
2520 v = sqlite3GetVdbe(pParse);
2521 if( v ){
2522 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002523 }
drhc4a3c772001-04-04 11:48:57 +00002524}
2525
2526/*
2527** Rollback a transaction
2528*/
danielk19774adee202004-05-08 08:23:19 +00002529void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002530 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002531 Vdbe *v;
2532
drh001bbcb2003-03-19 03:14:00 +00002533 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002534 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002535 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002536
danielk19774adee202004-05-08 08:23:19 +00002537 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002538 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002539 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002540 }
drhc4a3c772001-04-04 11:48:57 +00002541}
drhf57b14a2001-09-14 18:54:08 +00002542
2543/*
drh80242052004-06-09 00:48:12 +00002544** Generate VDBE code that will verify the schema cookie and start
2545** a read-transaction for all named database files.
2546**
2547** It is important that all schema cookies be verified and all
2548** read transactions be started before anything else happens in
2549** the VDBE program. But this routine can be called after much other
2550** code has been generated. So here is what we do:
2551**
2552** The first time this routine is called, we code an OP_Gosub that
2553** will jump to a subroutine at the end of the program. Then we
2554** record every database that needs its schema verified in the
2555** pParse->cookieMask field. Later, after all other code has been
2556** generated, the subroutine that does the cookie verifications and
2557** starts the transactions will be coded and the OP_Gosub P2 value
2558** will be made to point to that subroutine. The generation of the
2559** cookie verification subroutine code happens in sqlite3FinishCoding().
drh001bbcb2003-03-19 03:14:00 +00002560*/
danielk19774adee202004-05-08 08:23:19 +00002561void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh80242052004-06-09 00:48:12 +00002562 sqlite *db;
2563 Vdbe *v;
2564 int mask;
2565
2566 v = sqlite3GetVdbe(pParse);
2567 if( v==0 ) return; /* This only happens if there was a prior error */
2568 db = pParse->db;
drh8bf8dc92003-05-17 17:35:10 +00002569 assert( iDb>=0 && iDb<db->nDb );
drh80242052004-06-09 00:48:12 +00002570 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2571 assert( iDb<32 );
2572 if( pParse->cookieMask==0 ){
2573 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
2574 }
2575 mask = 1<<iDb;
2576 if( (pParse->cookieMask & mask)==0 ){
2577 pParse->cookieMask |= mask;
2578 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
drh001bbcb2003-03-19 03:14:00 +00002579 }
drh001bbcb2003-03-19 03:14:00 +00002580}
2581
2582/*
drh1c928532002-01-31 15:54:21 +00002583** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002584** might change the database.
2585**
2586** This routine starts a new transaction if we are not already within
2587** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002588** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002589** be set for operations that might fail (due to a constraint) part of
2590** the way through and which will need to undo some writes without having to
2591** rollback the whole transaction. For operations where all constraints
2592** can be checked before any changes are made to the database, it is never
2593** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002594**
drh8bf8dc92003-05-17 17:35:10 +00002595** Only database iDb and the temp database are made writable by this call.
2596** If iDb==0, then the main and temp databases are made writable. If
2597** iDb==1 then only the temp database is made writable. If iDb>1 then the
2598** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002599*/
drh7f0f12e2004-05-21 13:39:50 +00002600void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk19771d850a72004-05-31 08:26:49 +00002601 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002602 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002603 sqlite3CodeVerifySchema(pParse, iDb);
2604 pParse->writeMask |= 1<<iDb;
danielk19771d850a72004-05-31 08:26:49 +00002605 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002606 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002607 }
2608 if( iDb!=1 ){
2609 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002610 }
2611}
2612
2613/*
drh1c928532002-01-31 15:54:21 +00002614** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002615** the database. If a statement transaction was started, then emit
2616** an OP_Commit that will cause the changes to be committed to disk.
2617**
2618** Note that checkpoints are automatically committed at the end of
2619** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002620** sqlite3BeginWriteOperation() but there should only be a single
2621** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002622*/
danielk19774adee202004-05-08 08:23:19 +00002623void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002624 /* Delete me! */
2625 return;
drh1c928532002-01-31 15:54:21 +00002626}
danielk1977bfd6cce2004-06-18 04:24:54 +00002627
2628/*
2629** Return the transient sqlite3_value object used for encoding conversions
2630** during SQL compilation.
2631*/
2632sqlite3_value *sqlite3GetTransientValue(sqlite *db){
2633 if( !db->pValue ){
2634 db->pValue = sqlite3ValueNew();
2635 }
2636 return db->pValue;
2637}
2638