blob: afb1fb47e918b2b9159b5d7a6d48e13a8ce9f6ca [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**
drha6ecd332004-06-10 00:29:09 +000026** $Id: build.c,v 1.213 2004/06/10 00:29:09 drh Exp $
drh75897232000-05-29 14:26:00 +000027*/
28#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000029#include <ctype.h>
drh75897232000-05-29 14:26:00 +000030
31/*
drhe0bc4042002-06-25 01:09:11 +000032** This routine is called when a new SQL statement is beginning to
33** be parsed. Check to see if the schema for the database needs
34** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
35** If it does, then read it.
36*/
danielk19774adee202004-05-08 08:23:19 +000037void sqlite3BeginParse(Parse *pParse, int explainFlag){
drhe0bc4042002-06-25 01:09:11 +000038 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/*
drh75897232000-05-29 14:26:00 +0000483** Begin constructing a new table representation in memory. This is
484** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000485** to a CREATE TABLE statement. In particular, this routine is called
486** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000487** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000488** flag is true if the table should be stored in the auxiliary database
489** file instead of in the main database file. This is normally the case
490** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000491** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000492**
drhf57b3392001-10-08 13:22:32 +0000493** The new table record is initialized and put in pParse->pNewTable.
494** As more of the CREATE TABLE statement is parsed, additional action
495** routines will be called to add more information to this record.
danielk19774adee202004-05-08 08:23:19 +0000496** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
drhf57b3392001-10-08 13:22:32 +0000497** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000498*/
danielk19774adee202004-05-08 08:23:19 +0000499void sqlite3StartTable(
drhe5f9c642003-01-13 23:27:31 +0000500 Parse *pParse, /* Parser context */
501 Token *pStart, /* The "CREATE" token */
danielk1977cbb18d22004-05-28 11:37:27 +0000502 Token *pName1, /* First part of the name of the table or view */
503 Token *pName2, /* Second part of the name of the table or view */
drhe5f9c642003-01-13 23:27:31 +0000504 int isTemp, /* True if this is a TEMP table */
505 int isView /* True if this is a VIEW */
506){
drh75897232000-05-29 14:26:00 +0000507 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000508 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000509 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000510 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000511 Vdbe *v;
danielk1977cbb18d22004-05-28 11:37:27 +0000512 int iDb; /* Database number to create the table in */
513 Token *pName; /* Unqualified name of the table to create */
drh75897232000-05-29 14:26:00 +0000514
danielk1977cbb18d22004-05-28 11:37:27 +0000515 /* The table or view name to create is passed to this routine via tokens
516 ** pName1 and pName2. If the table name was fully qualified, for example:
517 **
518 ** CREATE TABLE xxx.yyy (...);
519 **
520 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
521 ** the table name is not fully qualified, i.e.:
522 **
523 ** CREATE TABLE yyy(...);
524 **
525 ** Then pName1 is set to "yyy" and pName2 is "".
526 **
527 ** The call below sets the pName pointer to point at the token (pName1 or
528 ** pName2) that stores the unqualified table name. The variable iDb is
529 ** set to the index of the database that the table or view is to be
530 ** created in.
531 */
danielk1977ef2cb632004-05-29 02:37:19 +0000532 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +0000533 if( iDb<0 ) return;
534 if( isTemp && iDb>1 ){
535 /* If creating a temp table, the name may not be qualified */
536 sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
537 pParse->nErr++;
538 return;
539 }
540 if( isTemp ) iDb = 1;
541
542 pParse->sNameToken = *pName;
danielk19774adee202004-05-08 08:23:19 +0000543 zName = sqlite3TableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000544 if( zName==0 ) return;
drh1d85d932004-02-14 23:05:52 +0000545 if( db->init.iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000546#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000547 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000548 {
549 int code;
danielk1977cbb18d22004-05-28 11:37:27 +0000550 char *zDb = db->aDb[iDb].zName;
danielk19774adee202004-05-08 08:23:19 +0000551 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +0000552 sqliteFree(zName);
553 return;
554 }
drhe5f9c642003-01-13 23:27:31 +0000555 if( isView ){
556 if( isTemp ){
557 code = SQLITE_CREATE_TEMP_VIEW;
558 }else{
559 code = SQLITE_CREATE_VIEW;
560 }
561 }else{
562 if( isTemp ){
563 code = SQLITE_CREATE_TEMP_TABLE;
564 }else{
565 code = SQLITE_CREATE_TABLE;
566 }
567 }
danielk19774adee202004-05-08 08:23:19 +0000568 if( sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
drh77ad4e42003-01-14 02:49:27 +0000569 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000570 return;
571 }
572 }
573#endif
drhf57b3392001-10-08 13:22:32 +0000574
575 /* Before trying to create a temporary table, make sure the Btree for
576 ** holding temporary tables is open.
577 */
drhd24cc422003-03-27 12:51:24 +0000578 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +0000579 int rc = sqlite3BtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000580 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000581 sqlite3ErrorMsg(pParse, "unable to open a temporary database "
drhf7a9e1a2004-02-22 18:40:56 +0000582 "file for storing temporary tables");
drhf57b3392001-10-08 13:22:32 +0000583 pParse->nErr++;
584 return;
585 }
danielk1977ee5741e2004-05-31 10:01:34 +0000586 if( db->flags & !db->autoCommit ){
danielk197713adf8a2004-06-03 16:08:41 +0000587 rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1, 0);
drhf57b3392001-10-08 13:22:32 +0000588 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +0000589 sqlite3ErrorMsg(pParse, "unable to get a write lock on "
drhf7a9e1a2004-02-22 18:40:56 +0000590 "the temporary database file");
drhf57b3392001-10-08 13:22:32 +0000591 return;
592 }
593 }
594 }
595
596 /* Make sure the new table name does not collide with an existing
danielk19773df6b252004-05-29 10:23:19 +0000597 ** index or table name in the same database. Issue an error message if
598 ** it does.
drhf57b3392001-10-08 13:22:32 +0000599 */
danielk19773df6b252004-05-29 10:23:19 +0000600 pTable = sqlite3FindTable(db, zName, db->aDb[iDb].zName);
601 if( pTable ){
danielk19774adee202004-05-08 08:23:19 +0000602 sqlite3ErrorMsg(pParse, "table %T already exists", pName);
drhd24cc422003-03-27 12:51:24 +0000603 sqliteFree(zName);
drhd24cc422003-03-27 12:51:24 +0000604 return;
drh75897232000-05-29 14:26:00 +0000605 }
danielk19774adee202004-05-08 08:23:19 +0000606 if( (pIdx = sqlite3FindIndex(db, zName, 0))!=0 &&
drh1d85d932004-02-14 23:05:52 +0000607 (pIdx->iDb==0 || !db->init.busy) ){
danielk19774adee202004-05-08 08:23:19 +0000608 sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
drh75897232000-05-29 14:26:00 +0000609 sqliteFree(zName);
drh75897232000-05-29 14:26:00 +0000610 return;
611 }
612 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000613 if( pTable==0 ){
614 sqliteFree(zName);
615 return;
616 }
drh75897232000-05-29 14:26:00 +0000617 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000618 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000619 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000620 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000621 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000622 pTable->iDb = iDb;
danielk19774adee202004-05-08 08:23:19 +0000623 if( pParse->pNewTable ) sqlite3DeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000624 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000625
626 /* Begin generating the code that will insert the table record into
627 ** the SQLITE_MASTER table. Note in particular that we must go ahead
628 ** and allocate the record number for the table entry now. Before any
629 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
630 ** indices to be created and the table record must come before the
631 ** indices. Hence, the record number for the table must be allocated
632 ** now.
633 */
danielk19774adee202004-05-08 08:23:19 +0000634 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +0000635 sqlite3BeginWriteOperation(pParse, 0, iDb);
drhf57b3392001-10-08 13:22:32 +0000636 if( !isTemp ){
danielk1977cbb18d22004-05-28 11:37:27 +0000637 /* Every time a new table is created the file-format
638 ** and encoding meta-values are set in the database, in
639 ** case this is the first table created.
640 */
danielk19774adee202004-05-08 08:23:19 +0000641 sqlite3VdbeAddOp(v, OP_Integer, db->file_format, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000642 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1);
danielk1977172bc392004-05-22 08:09:11 +0000643 sqlite3VdbeAddOp(v, OP_Integer, db->enc, 0);
danielk1977cbb18d22004-05-28 11:37:27 +0000644 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4);
drhf57b3392001-10-08 13:22:32 +0000645 }
danielk1977cbb18d22004-05-28 11:37:27 +0000646 sqlite3OpenMasterTable(v, iDb);
danielk19774adee202004-05-08 08:23:19 +0000647 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
648 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +0000649 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000650 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000651 }
drh75897232000-05-29 14:26:00 +0000652}
653
654/*
655** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000656**
657** The parser calls this routine once for each column declaration
danielk19774adee202004-05-08 08:23:19 +0000658** in a CREATE TABLE statement. sqlite3StartTable() gets called
drhd9b02572001-04-15 00:37:09 +0000659** first to get things going. Then this routine is called for each
660** column.
drh75897232000-05-29 14:26:00 +0000661*/
danielk19774adee202004-05-08 08:23:19 +0000662void sqlite3AddColumn(Parse *pParse, Token *pName){
drh75897232000-05-29 14:26:00 +0000663 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000664 int i;
665 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000666 Column *pCol;
drh75897232000-05-29 14:26:00 +0000667 if( (p = pParse->pNewTable)==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000668 sqlite3SetNString(&z, pName->z, pName->n, 0);
drh97fc3d02002-05-22 21:27:03 +0000669 if( z==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000670 sqlite3Dequote(z);
drh97fc3d02002-05-22 21:27:03 +0000671 for(i=0; i<p->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000672 if( sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
673 sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
drh97fc3d02002-05-22 21:27:03 +0000674 sqliteFree(z);
675 return;
676 }
677 }
drh75897232000-05-29 14:26:00 +0000678 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000679 Column *aNew;
680 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
681 if( aNew==0 ) return;
682 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000683 }
drhc9b84a12002-06-20 11:36:48 +0000684 pCol = &p->aCol[p->nCol];
685 memset(pCol, 0, sizeof(p->aCol[0]));
686 pCol->zName = z;
danielk1977a37cdde2004-05-16 11:15:36 +0000687
688 /* If there is no type specified, columns have the default affinity
danielk19774f057f92004-06-08 00:02:33 +0000689 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
690 ** be called next to set pCol->affinity correctly.
danielk1977a37cdde2004-05-16 11:15:36 +0000691 */
danielk19774f057f92004-06-08 00:02:33 +0000692 pCol->affinity = SQLITE_AFF_NONE;
drhd3d39e92004-05-20 22:16:29 +0000693 pCol->pColl = pParse->db->pDfltColl;
drhc9b84a12002-06-20 11:36:48 +0000694 p->nCol++;
drh75897232000-05-29 14:26:00 +0000695}
696
697/*
drh382c0242001-10-06 16:33:02 +0000698** This routine is called by the parser while in the middle of
699** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
700** been seen on a column. This routine sets the notNull flag on
701** the column currently under construction.
702*/
danielk19774adee202004-05-08 08:23:19 +0000703void sqlite3AddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000704 Table *p;
705 int i;
706 if( (p = pParse->pNewTable)==0 ) return;
707 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000708 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000709}
710
711/*
712** This routine is called by the parser while in the middle of
713** parsing a CREATE TABLE statement. The pFirst token is the first
714** token in the sequence of tokens that describe the type of the
715** column currently under construction. pLast is the last token
716** in the sequence. Use this information to construct a string
717** that contains the typename of the column and store that string
718** in zType.
719*/
danielk19774adee202004-05-08 08:23:19 +0000720void sqlite3AddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
drh382c0242001-10-06 16:33:02 +0000721 Table *p;
722 int i, j;
723 int n;
724 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000725 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000726 if( (p = pParse->pNewTable)==0 ) return;
727 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000728 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000729 pCol = &p->aCol[i];
730 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000731 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
danielk19774adee202004-05-08 08:23:19 +0000732 sqlite3SetNString(pz, pFirst->z, n, 0);
drh382c0242001-10-06 16:33:02 +0000733 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000734 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000735 for(i=j=0; z[i]; i++){
736 int c = z[i];
737 if( isspace(c) ) continue;
738 z[j++] = c;
739 }
740 z[j] = 0;
danielk1977a37cdde2004-05-16 11:15:36 +0000741 pCol->affinity = sqlite3AffinityType(z, n);
drh382c0242001-10-06 16:33:02 +0000742}
743
744/*
drh7020f652000-06-03 18:06:52 +0000745** The given token is the default value for the last column added to
746** the table currently under construction. If "minusFlag" is true, it
747** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000748**
749** This routine is called by the parser while in the middle of
750** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000751*/
danielk19774adee202004-05-08 08:23:19 +0000752void sqlite3AddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
drh7020f652000-06-03 18:06:52 +0000753 Table *p;
754 int i;
755 char **pz;
756 if( (p = pParse->pNewTable)==0 ) return;
757 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000758 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000759 pz = &p->aCol[i].zDflt;
760 if( minusFlag ){
danielk19774adee202004-05-08 08:23:19 +0000761 sqlite3SetNString(pz, "-", 1, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000762 }else{
danielk19774adee202004-05-08 08:23:19 +0000763 sqlite3SetNString(pz, pVal->z, pVal->n, 0);
drh7020f652000-06-03 18:06:52 +0000764 }
danielk19774adee202004-05-08 08:23:19 +0000765 sqlite3Dequote(*pz);
drh7020f652000-06-03 18:06:52 +0000766}
767
768/*
drh4a324312001-12-21 14:30:42 +0000769** Designate the PRIMARY KEY for the table. pList is a list of names
770** of columns that form the primary key. If pList is NULL, then the
771** most recently added column of the table is the primary key.
772**
773** A table can have at most one primary key. If the table already has
774** a primary key (and this is the second primary key) then create an
775** error.
776**
777** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
778** then we will try to use that column as the row id. (Exception:
779** For backwards compatibility with older databases, do not do this
780** if the file format version number is less than 1.) Set the Table.iPKey
781** field of the table under construction to be the index of the
782** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
783** no INTEGER PRIMARY KEY.
784**
785** If the key is not an INTEGER PRIMARY KEY, then create a unique
786** index for the key. No index is created for INTEGER PRIMARY KEYs.
787*/
danielk19770202b292004-06-09 09:55:16 +0000788void sqlite3AddPrimaryKey(Parse *pParse, ExprList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000789 Table *pTab = pParse->pNewTable;
790 char *zType = 0;
drh78100cc2003-08-23 22:40:53 +0000791 int iCol = -1, i;
drhe0194f22003-02-26 13:52:51 +0000792 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000793 if( pTab->hasPrimKey ){
danielk19774adee202004-05-08 08:23:19 +0000794 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +0000795 "table \"%s\" has more than one primary key", pTab->zName);
drhe0194f22003-02-26 13:52:51 +0000796 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000797 }
798 pTab->hasPrimKey = 1;
799 if( pList==0 ){
800 iCol = pTab->nCol - 1;
drh78100cc2003-08-23 22:40:53 +0000801 pTab->aCol[iCol].isPrimKey = 1;
802 }else{
danielk19770202b292004-06-09 09:55:16 +0000803 for(i=0; i<pList->nExpr; i++){
drh78100cc2003-08-23 22:40:53 +0000804 for(iCol=0; iCol<pTab->nCol; iCol++){
drhd3d39e92004-05-20 22:16:29 +0000805 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
806 break;
807 }
drh78100cc2003-08-23 22:40:53 +0000808 }
809 if( iCol<pTab->nCol ) pTab->aCol[iCol].isPrimKey = 1;
drh4a324312001-12-21 14:30:42 +0000810 }
danielk19770202b292004-06-09 09:55:16 +0000811 if( pList->nExpr>1 ) iCol = -1;
drh4a324312001-12-21 14:30:42 +0000812 }
813 if( iCol>=0 && iCol<pTab->nCol ){
814 zType = pTab->aCol[iCol].zType;
815 }
danielk19773d68f032004-05-11 07:11:51 +0000816 if( zType && sqlite3StrICmp(zType, "INTEGER")==0 ){
drh4a324312001-12-21 14:30:42 +0000817 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000818 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000819 }else{
danielk1977cbb18d22004-05-28 11:37:27 +0000820 sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000821 pList = 0;
drh4a324312001-12-21 14:30:42 +0000822 }
drhe0194f22003-02-26 13:52:51 +0000823
824primary_key_exit:
danielk19770202b292004-06-09 09:55:16 +0000825 sqlite3ExprListDelete(pList);
drhe0194f22003-02-26 13:52:51 +0000826 return;
drh4a324312001-12-21 14:30:42 +0000827}
828
829/*
drhd3d39e92004-05-20 22:16:29 +0000830** Set the collation function of the most recently parsed table column
831** to the CollSeq given.
drh8e2ca022002-06-17 17:07:19 +0000832*/
drhd3d39e92004-05-20 22:16:29 +0000833void sqlite3AddCollateType(Parse *pParse, const char *zType, int nType){
drh8e2ca022002-06-17 17:07:19 +0000834 Table *p;
danielk19770202b292004-06-09 09:55:16 +0000835 Index *pIdx;
drhd3d39e92004-05-20 22:16:29 +0000836 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000837 int i;
danielk1977a37cdde2004-05-16 11:15:36 +0000838
drhd3d39e92004-05-20 22:16:29 +0000839 if( (p = pParse->pNewTable)==0 ) return;
danielk19770202b292004-06-09 09:55:16 +0000840 i = p->nCol-1;
841
842 pColl = sqlite3LocateCollSeq(pParse, zType, nType);
843 p->aCol[i].pColl = pColl;
844
845 /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
846 ** then an index may have been created on this column before the
847 ** collation type was added. Correct this if it is the case.
848 */
849 for(pIdx = p->pIndex; pIdx; pIdx=pIdx->pNext){
850 assert( pIdx->nColumn==1 );
851 if( pIdx->aiColumn[0]==i ) pIdx->keyInfo.aColl[0] = pColl;
drhd3d39e92004-05-20 22:16:29 +0000852 }
853}
854
855/*
danielk19770202b292004-06-09 09:55:16 +0000856** Locate and return an entry from the db.aCollSeq hash table. If the entry
857** specified by zName and nName is not found and parameter 'create' is
858** true, then create a new entry.
drhd3d39e92004-05-20 22:16:29 +0000859**
danielk19770202b292004-06-09 09:55:16 +0000860** FIX ME: For now, return NULL if create is not true and the entry is not
861** found. But this needs to change to call the collation factory.
drhd3d39e92004-05-20 22:16:29 +0000862**
danielk19770202b292004-06-09 09:55:16 +0000863** FIX ME: If we have a UTF-8 version of the collation function, and a
864** UTF-16 version would be better, should the collation factory be called?
865** If so should a flag be set to say that we already requested such a
866** function and couldn't get one?
drhd3d39e92004-05-20 22:16:29 +0000867*/
danielk19770202b292004-06-09 09:55:16 +0000868CollSeq *sqlite3FindCollSeq(
869 sqlite *db,
870 const char *zName,
871 int nName,
872 int create
drhd3d39e92004-05-20 22:16:29 +0000873){
874 CollSeq *pColl;
danielk19770202b292004-06-09 09:55:16 +0000875 if( nName<0 ) nName = strlen(zName);
drhd3d39e92004-05-20 22:16:29 +0000876 pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
danielk19770202b292004-06-09 09:55:16 +0000877 if( 0==pColl && create ){
878 pColl = sqliteMalloc( sizeof(*pColl) + nName + 1 );
879 if( pColl ){
880 pColl->zName = (char*)&pColl[1];
881 memcpy(pColl->zName, zName, nName+1);
882 sqlite3HashInsert(&db->aCollSeq, pColl->zName, nName, pColl);
drhd3d39e92004-05-20 22:16:29 +0000883 }
drhd3d39e92004-05-20 22:16:29 +0000884 }
drhd3d39e92004-05-20 22:16:29 +0000885 return pColl;
danielk1977a37cdde2004-05-16 11:15:36 +0000886}
887
danielk19770202b292004-06-09 09:55:16 +0000888CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, int nName){
889 CollSeq *pColl = sqlite3FindCollSeq(pParse->db, zName, nName, 0);
890 if( !pColl ){
891 if( pParse->nErr==0 ){
892 sqlite3SetNString(&pParse->zErrMsg,
893 "no such collation sequence: ", -1,
894 zName, nName, 0);
895 }
896 pParse->nErr++;
897 }
898 return pColl;
899}
900
901
902
danielk1977a37cdde2004-05-16 11:15:36 +0000903/*
drh1ad3b9e2004-05-20 12:10:20 +0000904** Scan the column type name zType (length nType) and return the
danielk1977a37cdde2004-05-16 11:15:36 +0000905** associated affinity type.
906*/
907char sqlite3AffinityType(const char *zType, int nType){
danielk1977a37cdde2004-05-16 11:15:36 +0000908 int n, i;
909 struct {
drh1ad3b9e2004-05-20 12:10:20 +0000910 const char *zSub; /* Keywords substring to search for */
911 int nSub; /* length of zSub */
912 char affinity; /* Affinity to return if it matches */
danielk1977a37cdde2004-05-16 11:15:36 +0000913 } substrings[] = {
drh1ad3b9e2004-05-20 12:10:20 +0000914 {"INT", 3, SQLITE_AFF_INTEGER},
danielk1977a37cdde2004-05-16 11:15:36 +0000915 {"CHAR", 4, SQLITE_AFF_TEXT},
916 {"CLOB", 4, SQLITE_AFF_TEXT},
drh1ad3b9e2004-05-20 12:10:20 +0000917 {"TEXT", 4, SQLITE_AFF_TEXT},
918 {"BLOB", 4, SQLITE_AFF_NONE},
danielk1977a37cdde2004-05-16 11:15:36 +0000919 };
920
drh9c054832004-05-31 18:51:57 +0000921 if( nType==0 ){
922 return SQLITE_AFF_NONE;
923 }
drh1ad3b9e2004-05-20 12:10:20 +0000924 for(i=0; i<sizeof(substrings)/sizeof(substrings[0]); i++){
925 int c1 = substrings[i].zSub[0];
926 int c2 = tolower(c1);
927 int limit = nType - substrings[i].nSub;
928 const char *z = substrings[i].zSub;
929 for(n=0; n<=limit; n++){
930 int c = zType[n];
931 if( (c==c1 || c==c2)
932 && 0==sqlite3StrNICmp(&zType[n], z, substrings[i].nSub) ){
danielk1977a37cdde2004-05-16 11:15:36 +0000933 return substrings[i].affinity;
934 }
935 }
936 }
drh1ad3b9e2004-05-20 12:10:20 +0000937 return SQLITE_AFF_NUMERIC;
drh8e2ca022002-06-17 17:07:19 +0000938}
939
940/*
drh50e5dad2001-09-15 00:57:28 +0000941** Come up with a new random value for the schema cookie. Make sure
942** the new value is different from the old.
943**
944** The schema cookie is used to determine when the schema for the
945** database changes. After each schema change, the cookie value
946** changes. When a process first reads the schema it records the
947** cookie. Thereafter, whenever it goes to access the database,
948** it checks the cookie to make sure the schema has not changed
949** since it was last read.
950**
951** This plan is not completely bullet-proof. It is possible for
952** the schema to change multiple times and for the cookie to be
953** set back to prior value. But schema changes are infrequent
954** and the probability of hitting the same cookie value is only
955** 1 chance in 2^32. So we're safe enough.
956*/
danielk1977cbb18d22004-05-28 11:37:27 +0000957void sqlite3ChangeCookie(sqlite *db, Vdbe *v, int iDb){
danielk19771d850a72004-05-31 08:26:49 +0000958 unsigned char r;
959 int *pSchemaCookie = &(db->aDb[iDb].schema_cookie);
960
961 sqlite3Randomness(1, &r);
962 *pSchemaCookie = *pSchemaCookie + r + 1;
963 db->flags |= SQLITE_InternChanges;
964 sqlite3VdbeAddOp(v, OP_Integer, *pSchemaCookie, 0);
965 sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0);
drh50e5dad2001-09-15 00:57:28 +0000966}
967
968/*
drh969fa7c2002-02-18 18:30:32 +0000969** Measure the number of characters needed to output the given
970** identifier. The number returned includes any quotes used
971** but does not include the null terminator.
972*/
973static int identLength(const char *z){
974 int n;
drh17f71932002-02-21 12:01:27 +0000975 int needQuote = 0;
976 for(n=0; *z; n++, z++){
977 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000978 }
drh17f71932002-02-21 12:01:27 +0000979 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000980}
981
982/*
983** Write an identifier onto the end of the given string. Add
984** quote characters as needed.
985*/
986static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000987 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000988 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000989 for(j=0; zIdent[j]; j++){
990 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
991 }
992 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
danielk19774adee202004-05-08 08:23:19 +0000993 || sqlite3KeywordCode(zIdent, j)!=TK_ID;
drh17f71932002-02-21 12:01:27 +0000994 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000995 for(j=0; zIdent[j]; j++){
996 z[i++] = zIdent[j];
997 if( zIdent[j]=='\'' ) z[i++] = '\'';
998 }
drh17f71932002-02-21 12:01:27 +0000999 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +00001000 z[i] = 0;
1001 *pIdx = i;
1002}
1003
1004/*
1005** Generate a CREATE TABLE statement appropriate for the given
1006** table. Memory to hold the text of the statement is obtained
1007** from sqliteMalloc() and must be freed by the calling function.
1008*/
1009static char *createTableStmt(Table *p){
1010 int i, k, n;
1011 char *zStmt;
1012 char *zSep, *zSep2, *zEnd;
1013 n = 0;
1014 for(i=0; i<p->nCol; i++){
1015 n += identLength(p->aCol[i].zName);
danielk1977517eb642004-06-07 10:00:31 +00001016 if( p->aCol[i].zType ){
1017 n += (strlen(p->aCol[i].zType) + 1);
1018 }
drh969fa7c2002-02-18 18:30:32 +00001019 }
1020 n += identLength(p->zName);
1021 if( n<40 ){
1022 zSep = "";
1023 zSep2 = ",";
1024 zEnd = ")";
1025 }else{
1026 zSep = "\n ";
1027 zSep2 = ",\n ";
1028 zEnd = "\n)";
1029 }
drhe0bc4042002-06-25 01:09:11 +00001030 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +00001031 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +00001032 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001033 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +00001034 k = strlen(zStmt);
1035 identPut(zStmt, &k, p->zName);
1036 zStmt[k++] = '(';
1037 for(i=0; i<p->nCol; i++){
1038 strcpy(&zStmt[k], zSep);
1039 k += strlen(&zStmt[k]);
1040 zSep = zSep2;
1041 identPut(zStmt, &k, p->aCol[i].zName);
danielk1977517eb642004-06-07 10:00:31 +00001042 if( p->aCol[i].zType ){
1043 zStmt[k++] = ' ';
1044 strcpy(&zStmt[k], p->aCol[i].zType);
1045 k += strlen(p->aCol[i].zType);
1046 }
drh969fa7c2002-02-18 18:30:32 +00001047 }
1048 strcpy(&zStmt[k], zEnd);
1049 return zStmt;
1050}
1051
1052/*
drh75897232000-05-29 14:26:00 +00001053** This routine is called to report the final ")" that terminates
1054** a CREATE TABLE statement.
1055**
drhf57b3392001-10-08 13:22:32 +00001056** The table structure that other action routines have been building
1057** is added to the internal hash tables, assuming no errors have
1058** occurred.
drh75897232000-05-29 14:26:00 +00001059**
drh1d85d932004-02-14 23:05:52 +00001060** An entry for the table is made in the master table on disk, unless
1061** this is a temporary table or db->init.busy==1. When db->init.busy==1
drhf57b3392001-10-08 13:22:32 +00001062** it means we are reading the sqlite_master table because we just
1063** connected to the database or because the sqlite_master table has
1064** recently changes, so the entry for this table already exists in
1065** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +00001066**
1067** If the pSelect argument is not NULL, it means that this routine
1068** was called to create a table generated from a
1069** "CREATE TABLE ... AS SELECT ..." statement. The column names of
1070** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +00001071*/
danielk19774adee202004-05-08 08:23:19 +00001072void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +00001073 Table *p;
drhbe0072d2001-09-13 14:46:09 +00001074 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001075
danielk197724b03fd2004-05-10 10:34:34 +00001076 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +00001077 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +00001078 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +00001079
danielk1977517eb642004-06-07 10:00:31 +00001080 assert( !db->init.busy || !pSelect );
1081
drh969fa7c2002-02-18 18:30:32 +00001082 /* If the table is generated from a SELECT, then construct the
1083 ** list of columns and the text of the table.
1084 */
1085 if( pSelect ){
drh969fa7c2002-02-18 18:30:32 +00001086 }
1087
drh1d85d932004-02-14 23:05:52 +00001088 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +00001089 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
1090 ** So do not write to the disk again. Extract the root page number
drh1d85d932004-02-14 23:05:52 +00001091 ** for the table from the db->init.newTnum field. (The page number
drhe0bc4042002-06-25 01:09:11 +00001092 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +00001093 */
drh1d85d932004-02-14 23:05:52 +00001094 if( db->init.busy ){
1095 p->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001096 }
1097
drhe3c41372001-09-17 20:25:58 +00001098 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +00001099 ** in the SQLITE_MASTER table of the database. The record number
1100 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +00001101 **
drhe0bc4042002-06-25 01:09:11 +00001102 ** If this is a TEMPORARY table, write the entry into the auxiliary
1103 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +00001104 */
drh1d85d932004-02-14 23:05:52 +00001105 if( !db->init.busy ){
drh4ff6dfa2002-03-03 23:06:00 +00001106 int n;
drhd8bc7082000-06-07 23:51:50 +00001107 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001108
danielk19774adee202004-05-08 08:23:19 +00001109 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001110 if( v==0 ) return;
danielk1977517eb642004-06-07 10:00:31 +00001111
drh4ff6dfa2002-03-03 23:06:00 +00001112 if( p->pSelect==0 ){
1113 /* A regular table */
danielk19774adee202004-05-08 08:23:19 +00001114 sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER);
drh4ff6dfa2002-03-03 23:06:00 +00001115 }else{
1116 /* A view */
danielk19774adee202004-05-08 08:23:19 +00001117 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001118 }
drh969fa7c2002-02-18 18:30:32 +00001119 p->tnum = 0;
danielk1977517eb642004-06-07 10:00:31 +00001120
1121 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1122
1123 /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
1124 ** statement to populate the new table. The root-page number for the
1125 ** new table is on the top of the vdbe stack.
1126 **
1127 ** Once the SELECT has been coded by sqlite3Select(), it is in a
1128 ** suitable state to query for the column names and types to be used
1129 ** by the new table.
1130 */
1131 if( pSelect ){
1132 Table *pSelTab;
1133 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1134 sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0);
1135 sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0);
1136 pParse->nTab = 2;
1137 sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0);
1138 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
1139 if( pParse->nErr==0 ){
1140 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect);
1141 if( pSelTab==0 ) return;
1142 assert( p->aCol==0 );
1143 p->nCol = pSelTab->nCol;
1144 p->aCol = pSelTab->aCol;
1145 pSelTab->nCol = 0;
1146 pSelTab->aCol = 0;
1147 sqlite3DeleteTable(0, pSelTab);
1148 }
1149 }
1150
1151 sqlite3OpenMasterTable(v, p->iDb);
1152
1153 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view",P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001154 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
1155 sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0);
danielk1977517eb642004-06-07 10:00:31 +00001156 sqlite3VdbeAddOp(v, OP_Pull, 3, 0);
1157
drhe0bc4042002-06-25 01:09:11 +00001158 if( pSelect ){
1159 char *z = createTableStmt(p);
1160 n = z ? strlen(z) : 0;
danielk19770f69c1e2004-05-29 11:24:50 +00001161 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001162 sqlite3VdbeChangeP3(v, -1, z, n);
drhe0bc4042002-06-25 01:09:11 +00001163 sqliteFree(z);
1164 }else{
danielk1977cbb18d22004-05-28 11:37:27 +00001165 if( p->pSelect ){
danielk19770f69c1e2004-05-29 11:24:50 +00001166 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE VIEW ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001167 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001168 sqlite3VdbeOp3(v, OP_String8, 0, 0, "CREATE TABLE ", P3_STATIC);
danielk1977cbb18d22004-05-28 11:37:27 +00001169 }
drhe0bc4042002-06-25 01:09:11 +00001170 assert( pEnd!=0 );
danielk1977cbb18d22004-05-28 11:37:27 +00001171 n = Addr(pEnd->z) - Addr(pParse->sNameToken.z) + 1;
danielk19770f69c1e2004-05-29 11:24:50 +00001172 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001173 sqlite3VdbeChangeP3(v, -1, pParse->sNameToken.z, n);
1174 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drhe0bc4042002-06-25 01:09:11 +00001175 }
danielk197784ac9d02004-05-18 09:58:06 +00001176 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001177 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk19771d850a72004-05-31 08:26:49 +00001178 if( p->iDb!=1 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001179 sqlite3ChangeCookie(db, v, p->iDb);
drhe0bc4042002-06-25 01:09:11 +00001180 }
danielk19774adee202004-05-08 08:23:19 +00001181 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
danielk1977517eb642004-06-07 10:00:31 +00001182
danielk19774adee202004-05-08 08:23:19 +00001183 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001184 }
drh17e9e292003-02-01 13:53:28 +00001185
1186 /* Add the table to the in-memory representation of the database.
1187 */
drhd24cc422003-03-27 12:51:24 +00001188 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001189 Table *pOld;
1190 FKey *pFKey;
danielk19774adee202004-05-08 08:23:19 +00001191 pOld = sqlite3HashInsert(&db->aDb[p->iDb].tblHash,
drhd24cc422003-03-27 12:51:24 +00001192 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001193 if( pOld ){
1194 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1195 return;
1196 }
1197 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1198 int nTo = strlen(pFKey->zTo) + 1;
danielk19774adee202004-05-08 08:23:19 +00001199 pFKey->pNextTo = sqlite3HashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
1200 sqlite3HashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001201 }
1202 pParse->pNewTable = 0;
1203 db->nTable++;
1204 db->flags |= SQLITE_InternChanges;
1205 }
drh75897232000-05-29 14:26:00 +00001206}
1207
1208/*
drha76b5df2002-02-23 02:32:10 +00001209** The parser calls this routine in order to create a new VIEW
1210*/
danielk19774adee202004-05-08 08:23:19 +00001211void sqlite3CreateView(
drha76b5df2002-02-23 02:32:10 +00001212 Parse *pParse, /* The parsing context */
1213 Token *pBegin, /* The CREATE token that begins the statement */
danielk197748dec7e2004-05-28 12:33:30 +00001214 Token *pName1, /* The token that holds the name of the view */
1215 Token *pName2, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001216 Select *pSelect, /* A SELECT statement that will become the new view */
1217 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001218){
drha76b5df2002-02-23 02:32:10 +00001219 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001220 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001221 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001222 Token sEnd;
drhf26e09c2003-05-31 16:21:12 +00001223 DbFixer sFix;
danielk197748dec7e2004-05-28 12:33:30 +00001224 Token *pName;
drha76b5df2002-02-23 02:32:10 +00001225
danielk197748dec7e2004-05-28 12:33:30 +00001226 sqlite3StartTable(pParse, pBegin, pName1, pName2, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001227 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001228 if( p==0 || pParse->nErr ){
danielk19774adee202004-05-08 08:23:19 +00001229 sqlite3SelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001230 return;
1231 }
danielk1977ef2cb632004-05-29 02:37:19 +00001232 sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk19774adee202004-05-08 08:23:19 +00001233 if( sqlite3FixInit(&sFix, pParse, p->iDb, "view", pName)
1234 && sqlite3FixSelect(&sFix, pSelect)
drhf26e09c2003-05-31 16:21:12 +00001235 ){
danielk19774adee202004-05-08 08:23:19 +00001236 sqlite3SelectDelete(pSelect);
drhf26e09c2003-05-31 16:21:12 +00001237 return;
1238 }
drh174b6192002-12-03 02:22:52 +00001239
drh4b59ab52002-08-24 18:24:51 +00001240 /* Make a copy of the entire SELECT statement that defines the view.
1241 ** This will force all the Expr.token.z values to be dynamically
1242 ** allocated rather than point to the input string - which means that
danielk197724b03fd2004-05-10 10:34:34 +00001243 ** they will persist after the current sqlite3_exec() call returns.
drh4b59ab52002-08-24 18:24:51 +00001244 */
danielk19774adee202004-05-08 08:23:19 +00001245 p->pSelect = sqlite3SelectDup(pSelect);
1246 sqlite3SelectDelete(pSelect);
drh1d85d932004-02-14 23:05:52 +00001247 if( !pParse->db->init.busy ){
danielk19774adee202004-05-08 08:23:19 +00001248 sqlite3ViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001249 }
drh4b59ab52002-08-24 18:24:51 +00001250
1251 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1252 ** the end.
1253 */
drha76b5df2002-02-23 02:32:10 +00001254 sEnd = pParse->sLastToken;
1255 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1256 sEnd.z += sEnd.n;
1257 }
1258 sEnd.n = 0;
1259 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001260 z = pBegin->z;
1261 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1262 sEnd.z = &z[n-1];
1263 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001264
danielk19774adee202004-05-08 08:23:19 +00001265 /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
1266 sqlite3EndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001267 return;
drh417be792002-03-03 18:59:40 +00001268}
drha76b5df2002-02-23 02:32:10 +00001269
drh417be792002-03-03 18:59:40 +00001270/*
1271** The Table structure pTable is really a VIEW. Fill in the names of
1272** the columns of the view in the pTable structure. Return the number
jplyoncfa56842004-01-19 04:55:56 +00001273** of errors. If an error is seen leave an error message in pParse->zErrMsg.
drh417be792002-03-03 18:59:40 +00001274*/
danielk19774adee202004-05-08 08:23:19 +00001275int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
drh417be792002-03-03 18:59:40 +00001276 ExprList *pEList;
1277 Select *pSel;
1278 Table *pSelTab;
1279 int nErr = 0;
1280
1281 assert( pTable );
1282
1283 /* A positive nCol means the columns names for this view are
1284 ** already known.
1285 */
1286 if( pTable->nCol>0 ) return 0;
1287
1288 /* A negative nCol is a special marker meaning that we are currently
1289 ** trying to compute the column names. If we enter this routine with
1290 ** a negative nCol, it means two or more views form a loop, like this:
1291 **
1292 ** CREATE VIEW one AS SELECT * FROM two;
1293 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001294 **
1295 ** Actually, this error is caught previously and so the following test
1296 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001297 */
1298 if( pTable->nCol<0 ){
danielk19774adee202004-05-08 08:23:19 +00001299 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
drh417be792002-03-03 18:59:40 +00001300 return 1;
1301 }
1302
1303 /* If we get this far, it means we need to compute the table names.
1304 */
1305 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1306 pSel = pTable->pSelect;
1307
danielk19774adee202004-05-08 08:23:19 +00001308 /* Note that the call to sqlite3ResultSetOfSelect() will expand any
drh417be792002-03-03 18:59:40 +00001309 ** "*" elements in this list. But we will need to restore the list
1310 ** back to its original configuration afterwards, so we save a copy of
1311 ** the original in pEList.
1312 */
1313 pEList = pSel->pEList;
danielk19774adee202004-05-08 08:23:19 +00001314 pSel->pEList = sqlite3ExprListDup(pEList);
drh417be792002-03-03 18:59:40 +00001315 if( pSel->pEList==0 ){
1316 pSel->pEList = pEList;
1317 return 1; /* Malloc failed */
1318 }
1319 pTable->nCol = -1;
danielk19774adee202004-05-08 08:23:19 +00001320 pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSel);
drh417be792002-03-03 18:59:40 +00001321 if( pSelTab ){
1322 assert( pTable->aCol==0 );
1323 pTable->nCol = pSelTab->nCol;
1324 pTable->aCol = pSelTab->aCol;
1325 pSelTab->nCol = 0;
1326 pSelTab->aCol = 0;
danielk19774adee202004-05-08 08:23:19 +00001327 sqlite3DeleteTable(0, pSelTab);
drh8bf8dc92003-05-17 17:35:10 +00001328 DbSetProperty(pParse->db, pTable->iDb, DB_UnresetViews);
drh417be792002-03-03 18:59:40 +00001329 }else{
1330 pTable->nCol = 0;
1331 nErr++;
1332 }
danielk19774adee202004-05-08 08:23:19 +00001333 sqlite3SelectUnbind(pSel);
1334 sqlite3ExprListDelete(pSel->pEList);
drh417be792002-03-03 18:59:40 +00001335 pSel->pEList = pEList;
1336 return nErr;
1337}
1338
1339/*
1340** Clear the column names from the VIEW pTable.
1341**
1342** This routine is called whenever any other table or view is modified.
1343** The view passed into this routine might depend directly or indirectly
1344** on the modified or deleted table so we need to clear the old column
1345** names so that they will be recomputed.
1346*/
1347static void sqliteViewResetColumnNames(Table *pTable){
1348 int i;
drh701a0ae2004-02-22 20:05:00 +00001349 Column *pCol;
1350 assert( pTable!=0 && pTable->pSelect!=0 );
1351 for(i=0, pCol=pTable->aCol; i<pTable->nCol; i++, pCol++){
1352 sqliteFree(pCol->zName);
1353 sqliteFree(pCol->zDflt);
1354 sqliteFree(pCol->zType);
drh417be792002-03-03 18:59:40 +00001355 }
1356 sqliteFree(pTable->aCol);
1357 pTable->aCol = 0;
1358 pTable->nCol = 0;
1359}
1360
1361/*
drh8bf8dc92003-05-17 17:35:10 +00001362** Clear the column names from every VIEW in database idx.
drh417be792002-03-03 18:59:40 +00001363*/
drhd24cc422003-03-27 12:51:24 +00001364static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001365 HashElem *i;
drh8bf8dc92003-05-17 17:35:10 +00001366 if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
drhd24cc422003-03-27 12:51:24 +00001367 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001368 Table *pTab = sqliteHashData(i);
1369 if( pTab->pSelect ){
1370 sqliteViewResetColumnNames(pTab);
1371 }
1372 }
drh8bf8dc92003-05-17 17:35:10 +00001373 DbClearProperty(db, idx, DB_UnresetViews);
drha76b5df2002-02-23 02:32:10 +00001374}
1375
1376/*
drh75897232000-05-29 14:26:00 +00001377** Given a token, look up a table with that name. If not found, leave
1378** an error for the parser to find and return NULL.
1379*/
danielk19774adee202004-05-08 08:23:19 +00001380Table *sqlite3TableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001381 char *zName;
1382 Table *pTab;
danielk19774adee202004-05-08 08:23:19 +00001383 zName = sqlite3TableNameFromToken(pTok);
drhdaffd0e2001-04-11 14:28:42 +00001384 if( zName==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001385 pTab = sqlite3FindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001386 sqliteFree(zName);
1387 if( pTab==0 ){
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3ErrorMsg(pParse, "no such table: %T", pTok);
drha6ecd332004-06-10 00:29:09 +00001389 pParse->checkSchema = 1;
drh75897232000-05-29 14:26:00 +00001390 }
1391 return pTab;
1392}
1393
1394/*
1395** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001396** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001397*/
danielk1977a8858102004-05-28 12:11:21 +00001398void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView){
1399 Table *pTab;
drh75897232000-05-29 14:26:00 +00001400 Vdbe *v;
1401 int base;
drh5edc3122001-09-13 21:53:09 +00001402 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001403 int iDb;
drh75897232000-05-29 14:26:00 +00001404
danielk1977a8858102004-05-28 12:11:21 +00001405 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_drop_table;
1406 assert( pName->nSrc==1 );
1407 pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase);
1408
1409 if( pTab==0 ) goto exit_drop_table;
1410 iDb = pTab->iDb;
drhe22a3342003-04-22 20:30:37 +00001411 assert( iDb>=0 && iDb<db->nDb );
drhe5f9c642003-01-13 23:27:31 +00001412#ifndef SQLITE_OMIT_AUTHORIZATION
drhe5f9c642003-01-13 23:27:31 +00001413 {
1414 int code;
danielk1977a8858102004-05-28 12:11:21 +00001415 const char *zTab = SCHEMA_TABLE(pTab->iDb);
1416 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001417 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
danielk1977a8858102004-05-28 12:11:21 +00001418 goto exit_drop_table;
drhe22a3342003-04-22 20:30:37 +00001419 }
drhe5f9c642003-01-13 23:27:31 +00001420 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001421 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001422 code = SQLITE_DROP_TEMP_VIEW;
1423 }else{
1424 code = SQLITE_DROP_VIEW;
1425 }
1426 }else{
drhd24cc422003-03-27 12:51:24 +00001427 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001428 code = SQLITE_DROP_TEMP_TABLE;
1429 }else{
1430 code = SQLITE_DROP_TABLE;
1431 }
1432 }
danielk1977a8858102004-05-28 12:11:21 +00001433 if( sqlite3AuthCheck(pParse, code, pTab->zName, 0, zDb) ){
1434 goto exit_drop_table;
drhe5f9c642003-01-13 23:27:31 +00001435 }
danielk1977a8858102004-05-28 12:11:21 +00001436 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
1437 goto exit_drop_table;
drh77ad4e42003-01-14 02:49:27 +00001438 }
drhe5f9c642003-01-13 23:27:31 +00001439 }
1440#endif
danielk1977a8858102004-05-28 12:11:21 +00001441 if( pTab->readOnly ){
1442 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
drh75897232000-05-29 14:26:00 +00001443 pParse->nErr++;
danielk1977a8858102004-05-28 12:11:21 +00001444 goto exit_drop_table;
drh75897232000-05-29 14:26:00 +00001445 }
danielk1977a8858102004-05-28 12:11:21 +00001446 if( isView && pTab->pSelect==0 ){
1447 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
1448 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001449 }
danielk1977a8858102004-05-28 12:11:21 +00001450 if( !isView && pTab->pSelect ){
1451 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
1452 goto exit_drop_table;
drh4ff6dfa2002-03-03 23:06:00 +00001453 }
drh75897232000-05-29 14:26:00 +00001454
drh1ccde152000-06-17 13:12:39 +00001455 /* Generate code to remove the table from the master table
1456 ** on disk.
1457 */
danielk19774adee202004-05-08 08:23:19 +00001458 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001459 if( v ){
drh905793e2004-02-21 13:31:09 +00001460 static VdbeOpList dropTable[] = {
danielk19778e227872004-06-07 07:52:17 +00001461 { OP_Rewind, 0, ADDR(13), 0},
1462 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001463 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001464 { OP_MemLoad, 1, 0, 0}, /* 3 */
danielk19778e227872004-06-07 07:52:17 +00001465 { OP_Column, 0, 2, 0}, /* sqlite_master.tbl_name */
1466 { OP_Ne, 0, ADDR(12), 0},
1467 { OP_String8, 0, 0, "trigger"},
1468 { OP_Column, 0, 2, 0}, /* sqlite_master.type */
1469 { OP_Eq, 0, ADDR(12), 0},
drh75897232000-05-29 14:26:00 +00001470 { OP_Delete, 0, 0, 0},
danielk19778e227872004-06-07 07:52:17 +00001471 { OP_Rewind, 0, ADDR(13), 0},
danielk19778d059842004-05-12 11:24:02 +00001472 { OP_Goto, 0, ADDR(3), 0},
danielk19778e227872004-06-07 07:52:17 +00001473 { OP_Next, 0, ADDR(3), 0}, /* 12 */
drh75897232000-05-29 14:26:00 +00001474 };
1475 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001476 Trigger *pTrigger;
danielk1977a8858102004-05-28 12:11:21 +00001477 sqlite3BeginWriteOperation(pParse, 0, pTab->iDb);
drh8bf8dc92003-05-17 17:35:10 +00001478
danielk19778e227872004-06-07 07:52:17 +00001479 /* Drop all triggers associated with the table being dropped. Code
1480 ** is generated to remove entries from sqlite_master and/or
1481 ** sqlite_temp_master if required.
1482 */
danielk1977a8858102004-05-28 12:11:21 +00001483 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001484 while( pTrigger ){
danielk1977a8858102004-05-28 12:11:21 +00001485 assert( pTrigger->iDb==pTab->iDb || pTrigger->iDb==1 );
danielk19774adee202004-05-08 08:23:19 +00001486 sqlite3DropTriggerPtr(pParse, pTrigger, 1);
drhe0bc4042002-06-25 01:09:11 +00001487 if( pParse->explain ){
1488 pTrigger = pTrigger->pNext;
1489 }else{
danielk1977a8858102004-05-28 12:11:21 +00001490 pTrigger = pTab->pTrigger;
drhe0bc4042002-06-25 01:09:11 +00001491 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001492 }
drh8bf8dc92003-05-17 17:35:10 +00001493
danielk19778e227872004-06-07 07:52:17 +00001494 /* Drop all SQLITE_MASTER table and index entries that refer to the
1495 ** table. The program name loops through the master table and deletes
1496 ** every row that refers to a table of the same name as the one being
1497 ** dropped. Triggers are handled seperately because a trigger can be
1498 ** created in the temp database that refers to a table in another
1499 ** database.
1500 */
danielk1977a8858102004-05-28 12:11:21 +00001501 sqlite3OpenMasterTable(v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001502 base = sqlite3VdbeAddOpList(v, ArraySize(dropTable), dropTable);
danielk1977a8858102004-05-28 12:11:21 +00001503 sqlite3VdbeChangeP3(v, base+1, pTab->zName, 0);
danielk19778e227872004-06-07 07:52:17 +00001504 sqlite3ChangeCookie(db, v, pTab->iDb);
danielk19774adee202004-05-08 08:23:19 +00001505 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001506 if( !isView ){
danielk1977a8858102004-05-28 12:11:21 +00001507 sqlite3VdbeAddOp(v, OP_Destroy, pTab->tnum, pTab->iDb);
1508 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
danielk19774adee202004-05-08 08:23:19 +00001509 sqlite3VdbeAddOp(v, OP_Destroy, pIdx->tnum, pIdx->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001510 }
drh5e00f6c2001-09-13 13:46:56 +00001511 }
danielk19774adee202004-05-08 08:23:19 +00001512 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001513 }
1514
drhe0bc4042002-06-25 01:09:11 +00001515 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001516 **
1517 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001518 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001519 */
1520 if( !pParse->explain ){
danielk1977a8858102004-05-28 12:11:21 +00001521 sqliteUnlinkAndDeleteTable(db, pTab);
drh5edc3122001-09-13 21:53:09 +00001522 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001523 }
drhd24cc422003-03-27 12:51:24 +00001524 sqliteViewResetAll(db, iDb);
danielk1977a8858102004-05-28 12:11:21 +00001525
1526exit_drop_table:
1527 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001528}
1529
1530/*
drhc2eef3b2002-08-31 18:53:06 +00001531** This routine is called to create a new foreign key on the table
1532** currently under construction. pFromCol determines which columns
1533** in the current table point to the foreign key. If pFromCol==0 then
1534** connect the key to the last column inserted. pTo is the name of
1535** the table referred to. pToCol is a list of tables in the other
1536** pTo table that the foreign key points to. flags contains all
1537** information about the conflict resolution algorithms specified
1538** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1539**
1540** An FKey structure is created and added to the table currently
1541** under construction in the pParse->pNewTable field. The new FKey
1542** is not linked into db->aFKey at this point - that does not happen
danielk19774adee202004-05-08 08:23:19 +00001543** until sqlite3EndTable().
drhc2eef3b2002-08-31 18:53:06 +00001544**
1545** The foreign key is set for IMMEDIATE processing. A subsequent call
danielk19774adee202004-05-08 08:23:19 +00001546** to sqlite3DeferForeignKey() might change this to DEFERRED.
drhc2eef3b2002-08-31 18:53:06 +00001547*/
danielk19774adee202004-05-08 08:23:19 +00001548void sqlite3CreateForeignKey(
drhc2eef3b2002-08-31 18:53:06 +00001549 Parse *pParse, /* Parsing context */
danielk19770202b292004-06-09 09:55:16 +00001550 ExprList *pFromCol, /* Columns in this table that point to other table */
drhc2eef3b2002-08-31 18:53:06 +00001551 Token *pTo, /* Name of the other table */
danielk19770202b292004-06-09 09:55:16 +00001552 ExprList *pToCol, /* Columns in the other table */
drhc2eef3b2002-08-31 18:53:06 +00001553 int flags /* Conflict resolution algorithms. */
1554){
1555 Table *p = pParse->pNewTable;
1556 int nByte;
1557 int i;
1558 int nCol;
1559 char *z;
1560 FKey *pFKey = 0;
1561
1562 assert( pTo!=0 );
1563 if( p==0 || pParse->nErr ) goto fk_end;
1564 if( pFromCol==0 ){
1565 int iCol = p->nCol-1;
1566 if( iCol<0 ) goto fk_end;
danielk19770202b292004-06-09 09:55:16 +00001567 if( pToCol && pToCol->nExpr!=1 ){
danielk19774adee202004-05-08 08:23:19 +00001568 sqlite3ErrorMsg(pParse, "foreign key on %s"
drhf7a9e1a2004-02-22 18:40:56 +00001569 " should reference only one column of table %T",
1570 p->aCol[iCol].zName, pTo);
drhc2eef3b2002-08-31 18:53:06 +00001571 goto fk_end;
1572 }
1573 nCol = 1;
danielk19770202b292004-06-09 09:55:16 +00001574 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001575 sqlite3ErrorMsg(pParse,
drhc2eef3b2002-08-31 18:53:06 +00001576 "number of columns in foreign key does not match the number of "
drhf7a9e1a2004-02-22 18:40:56 +00001577 "columns in the referenced table");
drhc2eef3b2002-08-31 18:53:06 +00001578 goto fk_end;
1579 }else{
danielk19770202b292004-06-09 09:55:16 +00001580 nCol = pFromCol->nExpr;
drhc2eef3b2002-08-31 18:53:06 +00001581 }
1582 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1583 if( pToCol ){
danielk19770202b292004-06-09 09:55:16 +00001584 for(i=0; i<pToCol->nExpr; i++){
drhc2eef3b2002-08-31 18:53:06 +00001585 nByte += strlen(pToCol->a[i].zName) + 1;
1586 }
1587 }
1588 pFKey = sqliteMalloc( nByte );
1589 if( pFKey==0 ) goto fk_end;
1590 pFKey->pFrom = p;
1591 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001592 z = (char*)&pFKey[1];
1593 pFKey->aCol = (struct sColMap*)z;
1594 z += sizeof(struct sColMap)*nCol;
1595 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001596 memcpy(z, pTo->z, pTo->n);
1597 z[pTo->n] = 0;
1598 z += pTo->n+1;
1599 pFKey->pNextTo = 0;
1600 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001601 if( pFromCol==0 ){
1602 pFKey->aCol[0].iFrom = p->nCol-1;
1603 }else{
1604 for(i=0; i<nCol; i++){
1605 int j;
1606 for(j=0; j<p->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001607 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
drhc2eef3b2002-08-31 18:53:06 +00001608 pFKey->aCol[i].iFrom = j;
1609 break;
1610 }
1611 }
1612 if( j>=p->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001613 sqlite3ErrorMsg(pParse,
drhf7a9e1a2004-02-22 18:40:56 +00001614 "unknown column \"%s\" in foreign key definition",
1615 pFromCol->a[i].zName);
drhc2eef3b2002-08-31 18:53:06 +00001616 goto fk_end;
1617 }
1618 }
1619 }
1620 if( pToCol ){
1621 for(i=0; i<nCol; i++){
1622 int n = strlen(pToCol->a[i].zName);
1623 pFKey->aCol[i].zCol = z;
1624 memcpy(z, pToCol->a[i].zName, n);
1625 z[n] = 0;
1626 z += n+1;
1627 }
1628 }
1629 pFKey->isDeferred = 0;
1630 pFKey->deleteConf = flags & 0xff;
1631 pFKey->updateConf = (flags >> 8 ) & 0xff;
1632 pFKey->insertConf = (flags >> 16 ) & 0xff;
1633
1634 /* Link the foreign key to the table as the last step.
1635 */
1636 p->pFKey = pFKey;
1637 pFKey = 0;
1638
1639fk_end:
1640 sqliteFree(pFKey);
danielk19770202b292004-06-09 09:55:16 +00001641 sqlite3ExprListDelete(pFromCol);
1642 sqlite3ExprListDelete(pToCol);
drhc2eef3b2002-08-31 18:53:06 +00001643}
1644
1645/*
1646** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1647** clause is seen as part of a foreign key definition. The isDeferred
1648** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1649** The behavior of the most recently created foreign key is adjusted
1650** accordingly.
1651*/
danielk19774adee202004-05-08 08:23:19 +00001652void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
drhc2eef3b2002-08-31 18:53:06 +00001653 Table *pTab;
1654 FKey *pFKey;
1655 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1656 pFKey->isDeferred = isDeferred;
1657}
1658
1659/*
drh75897232000-05-29 14:26:00 +00001660** Create a new index for an SQL table. pIndex is the name of the index
1661** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001662** be NULL for a primary key or an index that is created to satisfy a
1663** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001664** as the table to be indexed. pParse->pNewTable is a table that is
1665** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001666**
drh382c0242001-10-06 16:33:02 +00001667** pList is a list of columns to be indexed. pList will be NULL if this
1668** is a primary key or unique-constraint on the most recent column added
1669** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001670*/
danielk19774adee202004-05-08 08:23:19 +00001671void sqlite3CreateIndex(
drh75897232000-05-29 14:26:00 +00001672 Parse *pParse, /* All information about this parse */
danielk1977cbb18d22004-05-28 11:37:27 +00001673 Token *pName1, /* First part of index name. May be NULL */
1674 Token *pName2, /* Second part of index name. May be NULL */
danielk19770202b292004-06-09 09:55:16 +00001675 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
1676 ExprList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001677 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001678 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1679 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1680){
danielk1977cbb18d22004-05-28 11:37:27 +00001681 Table *pTab = 0; /* Table to be indexed */
drh75897232000-05-29 14:26:00 +00001682 Index *pIndex; /* The index to be created */
1683 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001684 int i, j;
drhf26e09c2003-05-31 16:21:12 +00001685 Token nullId; /* Fake token for an empty ID list */
1686 DbFixer sFix; /* For assigning database names to pTable */
drh4925ca02003-11-27 00:48:57 +00001687 int isTemp; /* True for a temporary index */
drhbe0072d2001-09-13 14:46:09 +00001688 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001689
danielk1977cbb18d22004-05-28 11:37:27 +00001690 int iDb; /* Index of the database that is being written */
1691 Token *pName = 0; /* Unqualified name of the index to create */
1692
danielk197724b03fd2004-05-10 10:34:34 +00001693 if( pParse->nErr || sqlite3_malloc_failed ) goto exit_create_index;
drhdaffd0e2001-04-11 14:28:42 +00001694
drh75897232000-05-29 14:26:00 +00001695 /*
1696 ** Find the table that is to be indexed. Return early if not found.
1697 */
danielk1977cbb18d22004-05-28 11:37:27 +00001698 if( pTblName!=0 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001699
1700 /* Use the two-part index name to determine the database
danielk1977ef2cb632004-05-29 02:37:19 +00001701 ** to search for the table. 'Fix' the table name to this db
1702 ** before looking up the table.
danielk1977cbb18d22004-05-28 11:37:27 +00001703 */
1704 assert( pName1 && pName2 );
danielk1977ef2cb632004-05-29 02:37:19 +00001705 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
danielk1977cbb18d22004-05-28 11:37:27 +00001706 if( iDb<0 ) goto exit_create_index;
1707
danielk1977ef2cb632004-05-29 02:37:19 +00001708 /* If the index name was unqualified, check if the the table
1709 ** is a temp table. If so, set the database to 1.
danielk1977cbb18d22004-05-28 11:37:27 +00001710 */
danielk1977ef2cb632004-05-29 02:37:19 +00001711 pTab = sqlite3SrcListLookup(pParse, pTblName);
1712 if( pName2 && pName2->n==0 && pTab && pTab->iDb==1 ){
1713 iDb = 1;
1714 }
1715
1716 if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) &&
1717 sqlite3FixSrcList(&sFix, pTblName)
1718 ){
danielk1977cbb18d22004-05-28 11:37:27 +00001719 goto exit_create_index;
1720 }
danielk1977ef2cb632004-05-29 02:37:19 +00001721 pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName,
1722 pTblName->a[0].zDatabase);
danielk1977cbb18d22004-05-28 11:37:27 +00001723 if( !pTab ) goto exit_create_index;
danielk1977ef2cb632004-05-29 02:37:19 +00001724 assert( iDb==pTab->iDb );
drh75897232000-05-29 14:26:00 +00001725 }else{
drhe3c41372001-09-17 20:25:58 +00001726 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001727 pTab = pParse->pNewTable;
danielk1977cbb18d22004-05-28 11:37:27 +00001728 iDb = pTab->iDb;
drh75897232000-05-29 14:26:00 +00001729 }
danielk1977cbb18d22004-05-28 11:37:27 +00001730
drh75897232000-05-29 14:26:00 +00001731 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001732 if( pTab->readOnly ){
danielk19774adee202004-05-08 08:23:19 +00001733 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
drh0be9df02003-03-30 00:19:49 +00001734 goto exit_create_index;
1735 }
drha76b5df2002-02-23 02:32:10 +00001736 if( pTab->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001737 sqlite3ErrorMsg(pParse, "views may not be indexed");
drha76b5df2002-02-23 02:32:10 +00001738 goto exit_create_index;
1739 }
drh4925ca02003-11-27 00:48:57 +00001740 isTemp = pTab->iDb==1;
drh75897232000-05-29 14:26:00 +00001741
1742 /*
1743 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001744 ** index or table with the same name.
1745 **
1746 ** Exception: If we are reading the names of permanent indices from the
1747 ** sqlite_master table (because some other process changed the schema) and
1748 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001749 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001750 **
1751 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001752 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1753 ** own name.
drh75897232000-05-29 14:26:00 +00001754 */
drh1d85d932004-02-14 23:05:52 +00001755 if( pName && !db->init.busy ){
drhf57b3392001-10-08 13:22:32 +00001756 Index *pISameName; /* Another index with the same name */
1757 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001758 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001759 if( zName==0 ) goto exit_create_index;
danielk19773df6b252004-05-29 10:23:19 +00001760 if( (pISameName = sqlite3FindIndex(db, zName, db->aDb[iDb].zName))!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001761 sqlite3ErrorMsg(pParse, "index %s already exists", zName);
drhd24cc422003-03-27 12:51:24 +00001762 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001763 }
danielk19774adee202004-05-08 08:23:19 +00001764 if( (pTSameName = sqlite3FindTable(db, zName, 0))!=0 ){
1765 sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
drhd24cc422003-03-27 12:51:24 +00001766 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001767 }
drhd24cc422003-03-27 12:51:24 +00001768 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001769 char zBuf[30];
1770 int n;
1771 Index *pLoop;
1772 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1773 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001774 zName = 0;
danielk19774adee202004-05-08 08:23:19 +00001775 sqlite3SetString(&zName, "(", pTab->zName, " autoindex ", zBuf, (char*)0);
drhe3c41372001-09-17 20:25:58 +00001776 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001777 }else{
1778 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001779 }
1780
drhe5f9c642003-01-13 23:27:31 +00001781 /* Check for authorization to create an index.
1782 */
1783#ifndef SQLITE_OMIT_AUTHORIZATION
drhe22a3342003-04-22 20:30:37 +00001784 {
1785 const char *zDb = db->aDb[pTab->iDb].zName;
danielk19774adee202004-05-08 08:23:19 +00001786 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001787 goto exit_create_index;
1788 }
1789 i = SQLITE_CREATE_INDEX;
1790 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00001791 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
drhe22a3342003-04-22 20:30:37 +00001792 goto exit_create_index;
1793 }
drhe5f9c642003-01-13 23:27:31 +00001794 }
1795#endif
1796
drh75897232000-05-29 14:26:00 +00001797 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001798 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001799 ** So create a fake list to simulate this.
1800 */
1801 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001802 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001803 nullId.n = strlen(nullId.z);
danielk19770202b292004-06-09 09:55:16 +00001804 pList = sqlite3ExprListAppend(0, 0, &nullId);
drh75897232000-05-29 14:26:00 +00001805 if( pList==0 ) goto exit_create_index;
1806 }
1807
1808 /*
1809 ** Allocate the index structure.
1810 */
drhdcc581c2000-05-30 13:44:19 +00001811 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
danielk19770202b292004-06-09 09:55:16 +00001812 (sizeof(int) + sizeof(CollSeq*))*pList->nExpr );
drhdaffd0e2001-04-11 14:28:42 +00001813 if( pIndex==0 ) goto exit_create_index;
danielk19770202b292004-06-09 09:55:16 +00001814 pIndex->aiColumn = (int*)&pIndex->keyInfo.aColl[pList->nExpr];
1815 pIndex->zName = (char*)&pIndex->aiColumn[pList->nExpr];
drh75897232000-05-29 14:26:00 +00001816 strcpy(pIndex->zName, zName);
1817 pIndex->pTable = pTab;
danielk19770202b292004-06-09 09:55:16 +00001818 pIndex->nColumn = pList->nExpr;
drhea1ba172003-04-20 00:00:23 +00001819 pIndex->onError = onError;
drh485b39b2002-07-13 03:11:52 +00001820 pIndex->autoIndex = pName==0;
danielk1977cbb18d22004-05-28 11:37:27 +00001821 pIndex->iDb = iDb;
drh75897232000-05-29 14:26:00 +00001822
drh1ccde152000-06-17 13:12:39 +00001823 /* Scan the names of the columns of the table to be indexed and
1824 ** load the column indices into the Index structure. Report an error
1825 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001826 */
danielk19770202b292004-06-09 09:55:16 +00001827 for(i=0; i<pList->nExpr; i++){
drh75897232000-05-29 14:26:00 +00001828 for(j=0; j<pTab->nCol; j++){
danielk19774adee202004-05-08 08:23:19 +00001829 if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001830 }
1831 if( j>=pTab->nCol ){
danielk19774adee202004-05-08 08:23:19 +00001832 sqlite3ErrorMsg(pParse, "table %s has no column named %s",
drhf7a9e1a2004-02-22 18:40:56 +00001833 pTab->zName, pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001834 sqliteFree(pIndex);
1835 goto exit_create_index;
1836 }
drh967e8b72000-06-21 13:59:10 +00001837 pIndex->aiColumn[i] = j;
danielk19770202b292004-06-09 09:55:16 +00001838 if( pList->a[i].pExpr ){
1839 assert( pList->a[i].pExpr->pColl );
1840 pIndex->keyInfo.aColl[i] = pList->a[i].pExpr->pColl;
1841 }else{
1842 pIndex->keyInfo.aColl[i] = pTab->aCol[j].pColl;
1843 }
1844 assert( pIndex->keyInfo.aColl[i] );
drh75897232000-05-29 14:26:00 +00001845 }
danielk19770202b292004-06-09 09:55:16 +00001846 pIndex->keyInfo.nField = pList->nExpr;
drh75897232000-05-29 14:26:00 +00001847
1848 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001849 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001850 */
drhd24cc422003-03-27 12:51:24 +00001851 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001852 Index *p;
danielk19774adee202004-05-08 08:23:19 +00001853 p = sqlite3HashInsert(&db->aDb[pIndex->iDb].idxHash,
drh3c8bf552003-07-01 18:13:14 +00001854 pIndex->zName, strlen(pIndex->zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001855 if( p ){
1856 assert( p==pIndex ); /* Malloc must have failed */
1857 sqliteFree(pIndex);
1858 goto exit_create_index;
1859 }
drh5e00f6c2001-09-13 13:46:56 +00001860 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001861 }
drh9cfcf5d2002-01-29 18:41:24 +00001862
1863 /* When adding an index to the list of indices for a table, make
1864 ** sure all indices labeled OE_Replace come after all those labeled
1865 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1866 ** and INSERT.
1867 */
1868 if( onError!=OE_Replace || pTab->pIndex==0
1869 || pTab->pIndex->onError==OE_Replace){
1870 pIndex->pNext = pTab->pIndex;
1871 pTab->pIndex = pIndex;
1872 }else{
1873 Index *pOther = pTab->pIndex;
1874 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1875 pOther = pOther->pNext;
1876 }
1877 pIndex->pNext = pOther->pNext;
1878 pOther->pNext = pIndex;
1879 }
drh75897232000-05-29 14:26:00 +00001880
drh1d85d932004-02-14 23:05:52 +00001881 /* If the db->init.busy is 1 it means we are reading the SQL off the
drhd78eeee2001-09-13 16:18:53 +00001882 ** "sqlite_master" table on the disk. So do not write to the disk
drh1d85d932004-02-14 23:05:52 +00001883 ** again. Extract the table number from the db->init.newTnum field.
drhd78eeee2001-09-13 16:18:53 +00001884 */
danielk1977cbb18d22004-05-28 11:37:27 +00001885 if( db->init.busy && pTblName!=0 ){
drh1d85d932004-02-14 23:05:52 +00001886 pIndex->tnum = db->init.newTnum;
drhd78eeee2001-09-13 16:18:53 +00001887 }
1888
drh1d85d932004-02-14 23:05:52 +00001889 /* If the db->init.busy is 0 then create the index on disk. This
drh75897232000-05-29 14:26:00 +00001890 ** involves writing the index into the master table and filling in the
1891 ** index with the current table contents.
1892 **
drh1d85d932004-02-14 23:05:52 +00001893 ** The db->init.busy is 0 when the user first enters a CREATE INDEX
1894 ** command. db->init.busy is 1 when a database is opened and
drh75897232000-05-29 14:26:00 +00001895 ** CREATE INDEX statements are read out of the master table. In
1896 ** the latter case the index already exists on disk, which is why
1897 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001898 **
danielk1977cbb18d22004-05-28 11:37:27 +00001899 ** If pTblName==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001900 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1901 ** has just been created, it contains no data and the index initialization
1902 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001903 */
drh1d85d932004-02-14 23:05:52 +00001904 else if( db->init.busy==0 ){
drh75897232000-05-29 14:26:00 +00001905 int n;
drhadbca9c2001-09-27 15:11:53 +00001906 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001907 int lbl1, lbl2;
drh75897232000-05-29 14:26:00 +00001908
danielk19774adee202004-05-08 08:23:19 +00001909 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001910 if( v==0 ) goto exit_create_index;
danielk1977cbb18d22004-05-28 11:37:27 +00001911 if( pTblName!=0 ){
1912 sqlite3BeginWriteOperation(pParse, 0, iDb);
1913 sqlite3OpenMasterTable(v, iDb);
drhadbca9c2001-09-27 15:11:53 +00001914 }
danielk19774adee202004-05-08 08:23:19 +00001915 sqlite3VdbeAddOp(v, OP_NewRecno, 0, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001916 sqlite3VdbeOp3(v, OP_String8, 0, 0, "index", P3_STATIC);
1917 sqlite3VdbeOp3(v, OP_String8, 0, 0, pIndex->zName, 0);
1918 sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001919 sqlite3VdbeOp3(v, OP_CreateIndex, 0, iDb,(char*)&pIndex->tnum,P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001920 pIndex->tnum = 0;
danielk1977cbb18d22004-05-28 11:37:27 +00001921 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00001922 sqlite3VdbeCode(v,
drh701a0ae2004-02-22 20:05:00 +00001923 OP_Dup, 0, 0,
danielk1977cbb18d22004-05-28 11:37:27 +00001924 OP_Integer, iDb, 0,
drh701a0ae2004-02-22 20:05:00 +00001925 0);
drhd3d39e92004-05-20 22:16:29 +00001926 sqlite3VdbeOp3(v, OP_OpenWrite, 1, 0,
1927 (char*)&pIndex->keyInfo, P3_KEYINFO);
drh5e00f6c2001-09-13 13:46:56 +00001928 }
danielk19770f69c1e2004-05-29 11:24:50 +00001929 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhe0bc4042002-06-25 01:09:11 +00001930 if( pStart && pEnd ){
danielk19770202b292004-06-09 09:55:16 +00001931 if( onError==OE_None ){
1932 sqlite3VdbeChangeP3(v, -1, "CREATE INDEX ", P3_STATIC);
1933 }else{
1934 sqlite3VdbeChangeP3(v, -1, "CREATE UNIQUE INDEX ", P3_STATIC);
1935 }
danielk19770f69c1e2004-05-29 11:24:50 +00001936 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001937 n = Addr(pEnd->z) - Addr(pName->z) + 1;
1938 sqlite3VdbeChangeP3(v, -1, pName->z, n);
1939 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh75897232000-05-29 14:26:00 +00001940 }
danielk197784ac9d02004-05-18 09:58:06 +00001941 sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001942 sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00001943 if( pTblName ){
danielk19774adee202004-05-08 08:23:19 +00001944 sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +00001945 sqlite3VdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
1946 /* VdbeComment((v, "%s", pTab->zName)); */
danielk1977b4964b72004-05-18 01:23:38 +00001947 sqlite3VdbeAddOp(v, OP_SetNumColumns, 2, pTab->nCol);
danielk19774adee202004-05-08 08:23:19 +00001948 lbl2 = sqlite3VdbeMakeLabel(v);
1949 sqlite3VdbeAddOp(v, OP_Rewind, 2, lbl2);
drh51846b52004-05-28 16:00:21 +00001950 lbl1 = sqlite3VdbeCurrentAddr(v);
1951 sqlite3GenerateIndexKey(v, pIndex, 2);
danielk19774adee202004-05-08 08:23:19 +00001952 sqlite3VdbeOp3(v, OP_IdxPut, 1, pIndex->onError!=OE_None,
drh701a0ae2004-02-22 20:05:00 +00001953 "indexed columns are not unique", P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +00001954 sqlite3VdbeAddOp(v, OP_Next, 2, lbl1);
1955 sqlite3VdbeResolveLabel(v, lbl2);
1956 sqlite3VdbeAddOp(v, OP_Close, 2, 0);
1957 sqlite3VdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001958 }
danielk1977cbb18d22004-05-28 11:37:27 +00001959 if( pTblName!=0 ){
drhf57b3392001-10-08 13:22:32 +00001960 if( !isTemp ){
danielk1977cbb18d22004-05-28 11:37:27 +00001961 sqlite3ChangeCookie(db, v, iDb);
drhf57b3392001-10-08 13:22:32 +00001962 }
danielk19774adee202004-05-08 08:23:19 +00001963 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
1964 sqlite3EndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001965 }
drh75897232000-05-29 14:26:00 +00001966 }
1967
drh75897232000-05-29 14:26:00 +00001968 /* Clean up before exiting */
1969exit_create_index:
danielk19770202b292004-06-09 09:55:16 +00001970 sqlite3ExprListDelete(pList);
danielk1977cbb18d22004-05-28 11:37:27 +00001971 /* sqlite3SrcListDelete(pTable); */
drh75897232000-05-29 14:26:00 +00001972 sqliteFree(zName);
1973 return;
1974}
1975
1976/*
drh74e24cd2002-01-09 03:19:59 +00001977** This routine will drop an existing named index. This routine
1978** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001979*/
danielk19774adee202004-05-08 08:23:19 +00001980void sqlite3DropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001981 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001982 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001983 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001984
danielk197724b03fd2004-05-10 10:34:34 +00001985 if( pParse->nErr || sqlite3_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001986 assert( pName->nSrc==1 );
danielk19774adee202004-05-08 08:23:19 +00001987 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001988 if( pIndex==0 ){
danielk19774adee202004-05-08 08:23:19 +00001989 sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
drha6ecd332004-06-10 00:29:09 +00001990 pParse->checkSchema = 1;
drhd24cc422003-03-27 12:51:24 +00001991 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001992 }
drh485b39b2002-07-13 03:11:52 +00001993 if( pIndex->autoIndex ){
danielk19774adee202004-05-08 08:23:19 +00001994 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00001995 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00001996 goto exit_drop_index;
1997 }
danielk1977a8858102004-05-28 12:11:21 +00001998/*
drhd24cc422003-03-27 12:51:24 +00001999 if( pIndex->iDb>1 ){
danielk19774adee202004-05-08 08:23:19 +00002000 sqlite3ErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00002001 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00002002 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00002003 }
danielk1977a8858102004-05-28 12:11:21 +00002004*/
drhe5f9c642003-01-13 23:27:31 +00002005#ifndef SQLITE_OMIT_AUTHORIZATION
2006 {
2007 int code = SQLITE_DROP_INDEX;
2008 Table *pTab = pIndex->pTable;
drhe22a3342003-04-22 20:30:37 +00002009 const char *zDb = db->aDb[pIndex->iDb].zName;
2010 const char *zTab = SCHEMA_TABLE(pIndex->iDb);
danielk19774adee202004-05-08 08:23:19 +00002011 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002012 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002013 }
drhd24cc422003-03-27 12:51:24 +00002014 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
danielk19774adee202004-05-08 08:23:19 +00002015 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
drhd24cc422003-03-27 12:51:24 +00002016 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00002017 }
drhed6c8672003-01-12 18:02:16 +00002018 }
drhe5f9c642003-01-13 23:27:31 +00002019#endif
drh75897232000-05-29 14:26:00 +00002020
2021 /* Generate code to remove the index and from the master table */
danielk19774adee202004-05-08 08:23:19 +00002022 v = sqlite3GetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00002023 if( v ){
drh905793e2004-02-21 13:31:09 +00002024 static VdbeOpList dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00002025 { OP_Rewind, 0, ADDR(9), 0},
danielk19770f69c1e2004-05-29 11:24:50 +00002026 { OP_String8, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00002027 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002028 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00002029 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00002030 { OP_Eq, 0, ADDR(8), 0},
2031 { OP_Next, 0, ADDR(3), 0},
2032 { OP_Goto, 0, ADDR(9), 0},
2033 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00002034 };
2035 int base;
2036
danielk19774adee202004-05-08 08:23:19 +00002037 sqlite3BeginWriteOperation(pParse, 0, pIndex->iDb);
2038 sqlite3OpenMasterTable(v, pIndex->iDb);
2039 base = sqlite3VdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
2040 sqlite3VdbeChangeP3(v, base+1, pIndex->zName, 0);
danielk1977cbb18d22004-05-28 11:37:27 +00002041 if( pIndex->iDb!=1 ){
2042 sqlite3ChangeCookie(db, v, pIndex->iDb);
drhf57b3392001-10-08 13:22:32 +00002043 }
danielk19774adee202004-05-08 08:23:19 +00002044 sqlite3VdbeAddOp(v, OP_Close, 0, 0);
2045 sqlite3VdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
2046 sqlite3EndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00002047 }
2048
drhe0bc4042002-06-25 01:09:11 +00002049 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00002050 */
2051 if( !pParse->explain ){
danielk19774adee202004-05-08 08:23:19 +00002052 sqlite3UnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00002053 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00002054 }
drhd24cc422003-03-27 12:51:24 +00002055
2056exit_drop_index:
danielk19774adee202004-05-08 08:23:19 +00002057 sqlite3SrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00002058}
2059
2060/*
drh75897232000-05-29 14:26:00 +00002061** Append a new element to the given IdList. Create a new IdList if
2062** need be.
drhdaffd0e2001-04-11 14:28:42 +00002063**
2064** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00002065*/
danielk19774adee202004-05-08 08:23:19 +00002066IdList *sqlite3IdListAppend(IdList *pList, Token *pToken){
drh75897232000-05-29 14:26:00 +00002067 if( pList==0 ){
2068 pList = sqliteMalloc( sizeof(IdList) );
2069 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002070 pList->nAlloc = 0;
drh75897232000-05-29 14:26:00 +00002071 }
drh4305d102003-07-30 12:34:12 +00002072 if( pList->nId>=pList->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00002073 struct IdList_item *a;
drh4305d102003-07-30 12:34:12 +00002074 pList->nAlloc = pList->nAlloc*2 + 5;
2075 a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]) );
drh6d4abfb2001-10-22 02:58:08 +00002076 if( a==0 ){
danielk19774adee202004-05-08 08:23:19 +00002077 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002078 return 0;
drh75897232000-05-29 14:26:00 +00002079 }
drh6d4abfb2001-10-22 02:58:08 +00002080 pList->a = a;
drh75897232000-05-29 14:26:00 +00002081 }
2082 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
2083 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00002084 char **pz = &pList->a[pList->nId].zName;
danielk19774adee202004-05-08 08:23:19 +00002085 sqlite3SetNString(pz, pToken->z, pToken->n, 0);
drhdaffd0e2001-04-11 14:28:42 +00002086 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002087 sqlite3IdListDelete(pList);
drhdaffd0e2001-04-11 14:28:42 +00002088 return 0;
2089 }else{
danielk19774adee202004-05-08 08:23:19 +00002090 sqlite3Dequote(*pz);
drhdaffd0e2001-04-11 14:28:42 +00002091 }
drh75897232000-05-29 14:26:00 +00002092 }
2093 pList->nId++;
2094 return pList;
2095}
2096
2097/*
drhad3cab52002-05-24 02:04:32 +00002098** Append a new table name to the given SrcList. Create a new SrcList if
2099** need be. A new entry is created in the SrcList even if pToken is NULL.
2100**
2101** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00002102**
2103** If pDatabase is not null, it means that the table has an optional
2104** database name prefix. Like this: "database.table". The pDatabase
2105** points to the table name and the pTable points to the database name.
2106** The SrcList.a[].zName field is filled with the table name which might
2107** come from pTable (if pDatabase is NULL) or from pDatabase.
2108** SrcList.a[].zDatabase is filled with the database name from pTable,
2109** or with NULL if no database is specified.
2110**
2111** In other words, if call like this:
2112**
danielk19774adee202004-05-08 08:23:19 +00002113** sqlite3SrcListAppend(A,B,0);
drh113088e2003-03-20 01:16:58 +00002114**
2115** Then B is a table name and the database name is unspecified. If called
2116** like this:
2117**
danielk19774adee202004-05-08 08:23:19 +00002118** sqlite3SrcListAppend(A,B,C);
drh113088e2003-03-20 01:16:58 +00002119**
2120** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00002121*/
danielk19774adee202004-05-08 08:23:19 +00002122SrcList *sqlite3SrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00002123 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00002124 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00002125 if( pList==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00002126 pList->nAlloc = 1;
drhad3cab52002-05-24 02:04:32 +00002127 }
drh4305d102003-07-30 12:34:12 +00002128 if( pList->nSrc>=pList->nAlloc ){
drh113088e2003-03-20 01:16:58 +00002129 SrcList *pNew;
drh4305d102003-07-30 12:34:12 +00002130 pList->nAlloc *= 2;
drh113088e2003-03-20 01:16:58 +00002131 pNew = sqliteRealloc(pList,
drh4305d102003-07-30 12:34:12 +00002132 sizeof(*pList) + (pList->nAlloc-1)*sizeof(pList->a[0]) );
drh113088e2003-03-20 01:16:58 +00002133 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +00002134 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002135 return 0;
2136 }
drh113088e2003-03-20 01:16:58 +00002137 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00002138 }
2139 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00002140 if( pDatabase && pDatabase->z==0 ){
2141 pDatabase = 0;
2142 }
2143 if( pDatabase && pTable ){
2144 Token *pTemp = pDatabase;
2145 pDatabase = pTable;
2146 pTable = pTemp;
2147 }
2148 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00002149 char **pz = &pList->a[pList->nSrc].zName;
danielk19774adee202004-05-08 08:23:19 +00002150 sqlite3SetNString(pz, pTable->z, pTable->n, 0);
drh113088e2003-03-20 01:16:58 +00002151 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002152 sqlite3SrcListDelete(pList);
drh113088e2003-03-20 01:16:58 +00002153 return 0;
2154 }else{
danielk19774adee202004-05-08 08:23:19 +00002155 sqlite3Dequote(*pz);
drh113088e2003-03-20 01:16:58 +00002156 }
2157 }
2158 if( pDatabase ){
2159 char **pz = &pList->a[pList->nSrc].zDatabase;
danielk19774adee202004-05-08 08:23:19 +00002160 sqlite3SetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00002161 if( *pz==0 ){
danielk19774adee202004-05-08 08:23:19 +00002162 sqlite3SrcListDelete(pList);
drhad3cab52002-05-24 02:04:32 +00002163 return 0;
2164 }else{
danielk19774adee202004-05-08 08:23:19 +00002165 sqlite3Dequote(*pz);
drhad3cab52002-05-24 02:04:32 +00002166 }
2167 }
drh63eb5f22003-04-29 16:20:44 +00002168 pList->a[pList->nSrc].iCursor = -1;
drhad3cab52002-05-24 02:04:32 +00002169 pList->nSrc++;
2170 return pList;
2171}
2172
2173/*
drh63eb5f22003-04-29 16:20:44 +00002174** Assign cursors to all tables in a SrcList
2175*/
danielk19774adee202004-05-08 08:23:19 +00002176void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
drh63eb5f22003-04-29 16:20:44 +00002177 int i;
2178 for(i=0; i<pList->nSrc; i++){
2179 if( pList->a[i].iCursor<0 ){
drh6a3ea0e2003-05-02 14:32:12 +00002180 pList->a[i].iCursor = pParse->nTab++;
drh63eb5f22003-04-29 16:20:44 +00002181 }
2182 }
2183}
2184
2185/*
drh75897232000-05-29 14:26:00 +00002186** Add an alias to the last identifier on the given identifier list.
2187*/
danielk19774adee202004-05-08 08:23:19 +00002188void sqlite3SrcListAddAlias(SrcList *pList, Token *pToken){
drhad3cab52002-05-24 02:04:32 +00002189 if( pList && pList->nSrc>0 ){
2190 int i = pList->nSrc - 1;
danielk19774adee202004-05-08 08:23:19 +00002191 sqlite3SetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
2192 sqlite3Dequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00002193 }
2194}
2195
2196/*
drhad3cab52002-05-24 02:04:32 +00002197** Delete an IdList.
drh75897232000-05-29 14:26:00 +00002198*/
danielk19774adee202004-05-08 08:23:19 +00002199void sqlite3IdListDelete(IdList *pList){
drh75897232000-05-29 14:26:00 +00002200 int i;
2201 if( pList==0 ) return;
2202 for(i=0; i<pList->nId; i++){
2203 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00002204 }
2205 sqliteFree(pList->a);
2206 sqliteFree(pList);
2207}
2208
2209/*
drhad2d8302002-05-24 20:31:36 +00002210** Return the index in pList of the identifier named zId. Return -1
2211** if not found.
2212*/
danielk19774adee202004-05-08 08:23:19 +00002213int sqlite3IdListIndex(IdList *pList, const char *zName){
drhad2d8302002-05-24 20:31:36 +00002214 int i;
2215 if( pList==0 ) return -1;
2216 for(i=0; i<pList->nId; i++){
danielk19774adee202004-05-08 08:23:19 +00002217 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +00002218 }
2219 return -1;
2220}
2221
2222/*
drhad3cab52002-05-24 02:04:32 +00002223** Delete an entire SrcList including all its substructure.
2224*/
danielk19774adee202004-05-08 08:23:19 +00002225void sqlite3SrcListDelete(SrcList *pList){
drhad3cab52002-05-24 02:04:32 +00002226 int i;
2227 if( pList==0 ) return;
2228 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002229 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002230 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002231 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002232 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
danielk19774adee202004-05-08 08:23:19 +00002233 sqlite3DeleteTable(0, pList->a[i].pTab);
drhdaffd0e2001-04-11 14:28:42 +00002234 }
danielk19774adee202004-05-08 08:23:19 +00002235 sqlite3SelectDelete(pList->a[i].pSelect);
2236 sqlite3ExprDelete(pList->a[i].pOn);
2237 sqlite3IdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002238 }
drh75897232000-05-29 14:26:00 +00002239 sqliteFree(pList);
2240}
2241
drh982cef72000-05-30 16:27:03 +00002242/*
drhc4a3c772001-04-04 11:48:57 +00002243** Begin a transaction
2244*/
danielk197733752f82004-05-31 08:55:33 +00002245void sqlite3BeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002246 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002247 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002248
drh001bbcb2003-03-19 03:14:00 +00002249 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002250 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002251 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002252
2253 v = sqlite3GetVdbe(pParse);
2254 if( !v ) return;
2255 sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002256}
2257
2258/*
2259** Commit a transaction
2260*/
danielk19774adee202004-05-08 08:23:19 +00002261void sqlite3CommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002262 sqlite *db;
danielk19771d850a72004-05-31 08:26:49 +00002263 Vdbe *v;
drh5e00f6c2001-09-13 13:46:56 +00002264
drh001bbcb2003-03-19 03:14:00 +00002265 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002266 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002267 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002268
2269 v = sqlite3GetVdbe(pParse);
2270 if( v ){
2271 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0);
drh02f75f12004-02-24 01:04:11 +00002272 }
drhc4a3c772001-04-04 11:48:57 +00002273}
2274
2275/*
2276** Rollback a transaction
2277*/
danielk19774adee202004-05-08 08:23:19 +00002278void sqlite3RollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002279 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002280 Vdbe *v;
2281
drh001bbcb2003-03-19 03:14:00 +00002282 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
danielk197724b03fd2004-05-10 10:34:34 +00002283 if( pParse->nErr || sqlite3_malloc_failed ) return;
danielk19774adee202004-05-08 08:23:19 +00002284 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ) return;
danielk19771d850a72004-05-31 08:26:49 +00002285
danielk19774adee202004-05-08 08:23:19 +00002286 v = sqlite3GetVdbe(pParse);
drh5e00f6c2001-09-13 13:46:56 +00002287 if( v ){
danielk19771d850a72004-05-31 08:26:49 +00002288 sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1);
drh02f75f12004-02-24 01:04:11 +00002289 }
drhc4a3c772001-04-04 11:48:57 +00002290}
drhf57b14a2001-09-14 18:54:08 +00002291
2292/*
drh80242052004-06-09 00:48:12 +00002293** Generate VDBE code that will verify the schema cookie and start
2294** a read-transaction for all named database files.
2295**
2296** It is important that all schema cookies be verified and all
2297** read transactions be started before anything else happens in
2298** the VDBE program. But this routine can be called after much other
2299** code has been generated. So here is what we do:
2300**
2301** The first time this routine is called, we code an OP_Gosub that
2302** will jump to a subroutine at the end of the program. Then we
2303** record every database that needs its schema verified in the
2304** pParse->cookieMask field. Later, after all other code has been
2305** generated, the subroutine that does the cookie verifications and
2306** starts the transactions will be coded and the OP_Gosub P2 value
2307** will be made to point to that subroutine. The generation of the
2308** cookie verification subroutine code happens in sqlite3FinishCoding().
drh001bbcb2003-03-19 03:14:00 +00002309*/
danielk19774adee202004-05-08 08:23:19 +00002310void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
drh80242052004-06-09 00:48:12 +00002311 sqlite *db;
2312 Vdbe *v;
2313 int mask;
2314
2315 v = sqlite3GetVdbe(pParse);
2316 if( v==0 ) return; /* This only happens if there was a prior error */
2317 db = pParse->db;
drh8bf8dc92003-05-17 17:35:10 +00002318 assert( iDb>=0 && iDb<db->nDb );
drh80242052004-06-09 00:48:12 +00002319 assert( db->aDb[iDb].pBt!=0 || iDb==1 );
2320 assert( iDb<32 );
2321 if( pParse->cookieMask==0 ){
2322 pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
2323 }
2324 mask = 1<<iDb;
2325 if( (pParse->cookieMask & mask)==0 ){
2326 pParse->cookieMask |= mask;
2327 pParse->cookieValue[iDb] = db->aDb[iDb].schema_cookie;
drh001bbcb2003-03-19 03:14:00 +00002328 }
drh001bbcb2003-03-19 03:14:00 +00002329}
2330
2331/*
drh1c928532002-01-31 15:54:21 +00002332** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002333** might change the database.
2334**
2335** This routine starts a new transaction if we are not already within
2336** a transaction. If we are already within a transaction, then a checkpoint
drh7f0f12e2004-05-21 13:39:50 +00002337** is set if the setStatement parameter is true. A checkpoint should
drhc977f7f2002-05-21 11:38:11 +00002338** be set for operations that might fail (due to a constraint) part of
2339** the way through and which will need to undo some writes without having to
2340** rollback the whole transaction. For operations where all constraints
2341** can be checked before any changes are made to the database, it is never
2342** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002343**
drh8bf8dc92003-05-17 17:35:10 +00002344** Only database iDb and the temp database are made writable by this call.
2345** If iDb==0, then the main and temp databases are made writable. If
2346** iDb==1 then only the temp database is made writable. If iDb>1 then the
2347** specified auxiliary database and the temp database are made writable.
drh1c928532002-01-31 15:54:21 +00002348*/
drh7f0f12e2004-05-21 13:39:50 +00002349void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
danielk197711146c92004-05-31 11:51:44 +00002350 sqlite *db = pParse->db;
danielk19771d850a72004-05-31 08:26:49 +00002351 Vdbe *v = sqlite3GetVdbe(pParse);
drh663fc632002-02-02 18:49:19 +00002352 if( v==0 ) return;
drh80242052004-06-09 00:48:12 +00002353 sqlite3CodeVerifySchema(pParse, iDb);
2354 pParse->writeMask |= 1<<iDb;
danielk19771d850a72004-05-31 08:26:49 +00002355 if( setStatement ){
drh7f0f12e2004-05-21 13:39:50 +00002356 sqlite3VdbeAddOp(v, OP_Statement, iDb, 0);
danielk19771d850a72004-05-31 08:26:49 +00002357 }
2358 if( iDb!=1 ){
2359 sqlite3BeginWriteOperation(pParse, setStatement, 1);
drh663fc632002-02-02 18:49:19 +00002360 }
2361}
2362
2363/*
drh1c928532002-01-31 15:54:21 +00002364** Generate code that concludes an operation that may have changed
drh8bf8dc92003-05-17 17:35:10 +00002365** the database. If a statement transaction was started, then emit
2366** an OP_Commit that will cause the changes to be committed to disk.
2367**
2368** Note that checkpoints are automatically committed at the end of
2369** a statement. Note also that there can be multiple calls to
danielk19774adee202004-05-08 08:23:19 +00002370** sqlite3BeginWriteOperation() but there should only be a single
2371** call to sqlite3EndWriteOperation() at the conclusion of the statement.
drh1c928532002-01-31 15:54:21 +00002372*/
danielk19774adee202004-05-08 08:23:19 +00002373void sqlite3EndWriteOperation(Parse *pParse){
danielk19771d850a72004-05-31 08:26:49 +00002374 /* Delete me! */
2375 return;
drh1c928532002-01-31 15:54:21 +00002376}