blob: 44c4827328932d30adf6bb3709d13a8c5a668580 [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**
drha69d9162003-04-17 22:57:53 +000026** $Id: build.c,v 1.146 2003/04/17 22:57:53 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*/
37void sqliteBeginParse(Parse *pParse, int explainFlag){
38 sqlite *db = pParse->db;
39 pParse->explain = explainFlag;
40 if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){
41 int rc = sqliteInit(db, &pParse->zErrMsg);
42 if( rc!=SQLITE_OK ){
43 pParse->rc = rc;
44 pParse->nErr++;
45 }
46 }
47}
48
49/*
drhb86ccfb2003-01-28 23:13:10 +000050** This is a fake callback procedure used when sqlite_exec() is
51** invoked with a NULL callback pointer. If we pass a NULL callback
52** pointer into sqliteVdbeExec() it will return at every OP_Callback,
53** which we do not want it to do. So we substitute a pointer to this
54** procedure in place of the NULL.
55*/
56static int fakeCallback(void *NotUsed, int n, char **az1, char **az2){
57 return 0;
58}
59
60/*
drh75897232000-05-29 14:26:00 +000061** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000062** parsed and we want to execute the VDBE code to implement
63** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000064** constructed VDBE code to do the work of the SQL statement.
65** This routine just has to execute the VDBE code.
66**
67** Note that if an error occurred, it might be the case that
68** no VDBE code was generated.
69*/
70void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000071 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000072 sqlite *db = pParse->db;
drhb86ccfb2003-01-28 23:13:10 +000073 Vdbe *v = pParse->pVdbe;
74 int (*xCallback)(void*,int,char**,char**);
75
drhdaffd0e2001-04-11 14:28:42 +000076 if( sqlite_malloc_failed ) return;
drhb86ccfb2003-01-28 23:13:10 +000077 xCallback = pParse->xCallback;
78 if( xCallback==0 && pParse->useCallback ) xCallback = fakeCallback;
79 if( v && pParse->nErr==0 ){
80 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
81 sqliteVdbeTrace(v, trace);
82 sqliteVdbeMakeReady(v, xCallback, pParse->pArg, pParse->explain);
83 if( pParse->useCallback ){
84 if( pParse->explain ){
85 rc = sqliteVdbeList(v);
drh001bbcb2003-03-19 03:14:00 +000086 db->next_cookie = db->aDb[0].schema_cookie;
drhb86ccfb2003-01-28 23:13:10 +000087 }else{
88 sqliteVdbeExec(v);
89 }
90 rc = sqliteVdbeFinalize(v, &pParse->zErrMsg);
drhecdc7532001-09-23 02:35:53 +000091 if( rc ) pParse->nErr++;
drhb86ccfb2003-01-28 23:13:10 +000092 pParse->pVdbe = 0;
93 pParse->rc = rc;
94 if( rc ) pParse->nErr++;
95 }else{
96 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drh75897232000-05-29 14:26:00 +000097 }
drhd8bc7082000-06-07 23:51:50 +000098 pParse->colNamesSet = 0;
drh50e5dad2001-09-15 00:57:28 +000099 pParse->schemaVerified = 0;
drh483750b2003-01-29 18:46:51 +0000100 }else if( pParse->useCallback==0 ){
101 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000102 }
drha226d052002-09-25 19:04:07 +0000103 pParse->nTab = 0;
104 pParse->nMem = 0;
105 pParse->nSet = 0;
106 pParse->nAgg = 0;
drh75897232000-05-29 14:26:00 +0000107}
108
109/*
drhf57b3392001-10-08 13:22:32 +0000110** Locate the in-memory structure that describes
111** a particular database table given the name
drha69d9162003-04-17 22:57:53 +0000112** of that table and (optionally) the name of the database
113** containing the table. Return NULL if not found.
114**
115** See also sqliteLocateTable().
drh75897232000-05-29 14:26:00 +0000116*/
drhd24cc422003-03-27 12:51:24 +0000117Table *sqliteFindTable(sqlite *db, const char *zName, const char *zDatabase){
118 Table *p = 0;
119 int i;
120 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000121 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
122 if( zDatabase!=0 && sqliteStrICmp(zDatabase, db->aDb[j].zName) ) continue;
123 p = sqliteHashFind(&db->aDb[j].tblHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000124 if( p ) break;
125 }
drh74e24cd2002-01-09 03:19:59 +0000126 return p;
drh75897232000-05-29 14:26:00 +0000127}
128
129/*
drhf57b3392001-10-08 13:22:32 +0000130** Locate the in-memory structure that describes
drha69d9162003-04-17 22:57:53 +0000131** a particular database table given the name
132** of that table and (optionally) the name of the database
133** containing the table. Return NULL if not found.
134**
135** If pParse->useDb is not negative, then the table must be
136** located in that database. If a different database is specified,
137** an error message is generated into pParse->zErrMsg.
138*/
139Table *sqliteLocateTable(Parse *pParse, const char *zName, const char *zDbase){
140 sqlite *db;
141 const char *zUse;
142 Table *p;
143 db = pParse->db;
144 if( pParse->useDb<0 ){
145 p = sqliteFindTable(db, zName, zDbase);
146 }else {
147 assert( pParse->useDb<db->nDb );
148 assert( db->aDb[pParse->useDb].pBt!=0 );
149 zUse = db->aDb[pParse->useDb].zName;
150 if( zDbase && pParse->useDb!=1 && sqliteStrICmp(zDbase, zUse)!=0 ){
151 sqliteErrorMsg(pParse,"cannot use database %s in this context", zDbase);
152 return 0;
153 }
154 p = sqliteFindTable(db, zName, zUse);
155 if( p==0 && pParse->useDb==1 && zDbase==0 ){
156 p = sqliteFindTable(db, zName, 0);
157 }
158 }
159 if( p==0 ){
160 if( zDbase ){
161 sqliteErrorMsg(pParse, "no such table: %s.%s", zDbase, zName);
162 }else{
163 sqliteErrorMsg(pParse, "no such table: %s", zName);
164 }
165 }
166 return p;
167}
168
169/*
170** Locate the in-memory structure that describes
171** a particular index given the name of that index
172** and the name of the database that contains the index.
drhf57b3392001-10-08 13:22:32 +0000173** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000174*/
drhd24cc422003-03-27 12:51:24 +0000175Index *sqliteFindIndex(sqlite *db, const char *zName, const char *zDb){
176 Index *p = 0;
177 int i;
178 for(i=0; i<db->nDb; i++){
drh812d7a22003-03-27 13:50:00 +0000179 int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */
180 if( zDb && sqliteStrICmp(zDb, db->aDb[j].zName) ) continue;
181 p = sqliteHashFind(&db->aDb[j].idxHash, zName, strlen(zName)+1);
drhd24cc422003-03-27 12:51:24 +0000182 if( p ) break;
183 }
drh74e24cd2002-01-09 03:19:59 +0000184 return p;
drh75897232000-05-29 14:26:00 +0000185}
186
187/*
188** Remove the given index from the index hash table, and free
189** its memory structures.
190**
drhd229ca92002-01-09 13:30:41 +0000191** The index is removed from the database hash tables but
192** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000193** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000194*/
drh74e24cd2002-01-09 03:19:59 +0000195static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000196 Index *pOld;
drhd24cc422003-03-27 12:51:24 +0000197
drhd229ca92002-01-09 13:30:41 +0000198 assert( db!=0 && p->zName!=0 );
drhd24cc422003-03-27 12:51:24 +0000199 pOld = sqliteHashInsert(&db->aDb[p->iDb].idxHash, p->zName,
200 strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000201 if( pOld!=0 && pOld!=p ){
drhd24cc422003-03-27 12:51:24 +0000202 sqliteHashInsert(&db->aDb[p->iDb].idxHash, pOld->zName,
203 strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000204 }
drh74e24cd2002-01-09 03:19:59 +0000205 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000206}
207
208/*
drhbeae3192001-09-22 18:12:08 +0000209** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000210** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000211** structures.
212*/
drh6d4abfb2001-10-22 02:58:08 +0000213void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000214 if( pIndex->pTable->pIndex==pIndex ){
215 pIndex->pTable->pIndex = pIndex->pNext;
216 }else{
217 Index *p;
218 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
219 if( p && p->pNext==pIndex ){
220 p->pNext = pIndex->pNext;
221 }
222 }
223 sqliteDeleteIndex(db, pIndex);
224}
225
226/*
drhe0bc4042002-06-25 01:09:11 +0000227** Erase all schema information from the in-memory hash tables of
228** database connection. This routine is called to reclaim memory
229** before the connection closes. It is also called during a rollback
230** if there were schema changes during the transaction.
drh1c2d8412003-03-31 00:30:47 +0000231**
232** If iDb<=0 then reset the internal schema tables for all database
233** files. If iDb>=2 then reset the internal schema for only the
234** single file indicates.
drh74e24cd2002-01-09 03:19:59 +0000235*/
drh1c2d8412003-03-31 00:30:47 +0000236void sqliteResetInternalSchema(sqlite *db, int iDb){
drhe0bc4042002-06-25 01:09:11 +0000237 HashElem *pElem;
238 Hash temp1;
239 Hash temp2;
drh1c2d8412003-03-31 00:30:47 +0000240 int i, j;
drhe0bc4042002-06-25 01:09:11 +0000241
drh1c2d8412003-03-31 00:30:47 +0000242 assert( iDb>=0 && iDb<db->nDb );
243 db->flags &= ~SQLITE_Initialized;
244 for(i=iDb; i<db->nDb; i++){
drhd24cc422003-03-27 12:51:24 +0000245 Db *pDb = &db->aDb[i];
246 temp1 = pDb->tblHash;
247 temp2 = pDb->trigHash;
248 sqliteHashInit(&pDb->trigHash, SQLITE_HASH_STRING, 0);
249 sqliteHashClear(&pDb->aFKey);
250 sqliteHashClear(&pDb->idxHash);
251 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
252 Trigger *pTrigger = sqliteHashData(pElem);
253 sqliteDeleteTrigger(pTrigger);
254 }
255 sqliteHashClear(&temp2);
256 sqliteHashInit(&pDb->tblHash, SQLITE_HASH_STRING, 0);
257 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
258 Table *pTab = sqliteHashData(pElem);
259 sqliteDeleteTable(db, pTab);
260 }
261 sqliteHashClear(&temp1);
drh1c2d8412003-03-31 00:30:47 +0000262 db->aDb[i].flags &= ~SQLITE_Initialized;
263 if( iDb>0 ) return;
drh74e24cd2002-01-09 03:19:59 +0000264 }
drh1c2d8412003-03-31 00:30:47 +0000265 assert( iDb==0 );
266 db->flags &= ~SQLITE_InternChanges;
267
268 /* If one or more of the auxiliary database files has been closed,
269 ** then remove then from the auxiliary database list. We take the
270 ** opportunity to do this here since we have just deleted all of the
271 ** schema hash tables and therefore do not have to make any changes
272 ** to any of those tables.
273 */
274 for(i=j=2; i<db->nDb; i++){
275 if( db->aDb[i].pBt==0 ){
276 sqliteFree(db->aDb[i].zName);
277 db->aDb[i].zName = 0;
278 continue;
279 }
280 if( j<i ){
281 db->aDb[j++] = db->aDb[i];
282 }
283 }
284 memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
285 db->nDb = j;
286 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
287 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
288 sqliteFree(db->aDb);
289 db->aDb = db->aDbStatic;
290 }
drhe0bc4042002-06-25 01:09:11 +0000291}
292
293/*
294** This routine is called whenever a rollback occurs. If there were
295** schema changes during the transaction, then we have to reset the
296** internal hash tables and reload them from disk.
297*/
298void sqliteRollbackInternalChanges(sqlite *db){
299 if( db->flags & SQLITE_InternChanges ){
drh1c2d8412003-03-31 00:30:47 +0000300 sqliteResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000301 }
302}
303
304/*
305** This routine is called when a commit occurs.
306*/
307void sqliteCommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000308 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000309 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000310}
311
312/*
drh75897232000-05-29 14:26:00 +0000313** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000314** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000315**
316** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000317** the table data structure from the hash table. Nor does it remove
318** foreign keys from the sqlite.aFKey hash table. But it does destroy
319** memory structures of the indices and foreign keys associated with
320** the table.
drhdaffd0e2001-04-11 14:28:42 +0000321**
322** Indices associated with the table are unlinked from the "db"
323** data structure if db!=NULL. If db==NULL, indices attached to
324** the table are deleted, but it is assumed they have already been
325** unlinked.
drh75897232000-05-29 14:26:00 +0000326*/
327void sqliteDeleteTable(sqlite *db, Table *pTable){
328 int i;
329 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000330 FKey *pFKey, *pNextFKey;
331
drh75897232000-05-29 14:26:00 +0000332 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000333
334 /* Delete all indices associated with this table
335 */
336 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
337 pNext = pIndex->pNext;
drhd24cc422003-03-27 12:51:24 +0000338 assert( pIndex->iDb==pTable->iDb || (pTable->iDb==0 && pIndex->iDb==1) );
drhc2eef3b2002-08-31 18:53:06 +0000339 sqliteDeleteIndex(db, pIndex);
340 }
341
342 /* Delete all foreign keys associated with this table. The keys
343 ** should have already been unlinked from the db->aFKey hash table
344 */
345 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
346 pNextFKey = pFKey->pNextFrom;
drhd24cc422003-03-27 12:51:24 +0000347 assert( pTable->iDb<db->nDb );
348 assert( sqliteHashFind(&db->aDb[pTable->iDb].aFKey,
349 pFKey->zTo, strlen(pFKey->zTo)+1)!=pFKey );
drhc2eef3b2002-08-31 18:53:06 +0000350 sqliteFree(pFKey);
351 }
352
353 /* Delete the Table structure itself.
354 */
drh75897232000-05-29 14:26:00 +0000355 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000356 sqliteFree(pTable->aCol[i].zName);
357 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000358 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000359 }
drh6e142f52000-06-08 13:36:40 +0000360 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000361 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000362 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000363 sqliteFree(pTable);
364}
365
366/*
drh5edc3122001-09-13 21:53:09 +0000367** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000368** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000369*/
drh74e24cd2002-01-09 03:19:59 +0000370static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000371 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000372 FKey *pF1, *pF2;
drhd24cc422003-03-27 12:51:24 +0000373 int i = p->iDb;
drhd229ca92002-01-09 13:30:41 +0000374 assert( db!=0 );
drhd24cc422003-03-27 12:51:24 +0000375 pOld = sqliteHashInsert(&db->aDb[i].tblHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000376 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000377 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
378 int nTo = strlen(pF1->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +0000379 pF2 = sqliteHashFind(&db->aDb[i].aFKey, pF1->zTo, nTo);
drhc2eef3b2002-08-31 18:53:06 +0000380 if( pF2==pF1 ){
drhd24cc422003-03-27 12:51:24 +0000381 sqliteHashInsert(&db->aDb[i].aFKey, pF1->zTo, nTo, pF1->pNextTo);
drhc2eef3b2002-08-31 18:53:06 +0000382 }else{
383 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
384 if( pF2 ){
385 pF2->pNextTo = pF1->pNextTo;
386 }
387 }
388 }
drh74e24cd2002-01-09 03:19:59 +0000389 sqliteDeleteTable(db, p);
390}
391
392/*
drh1ccde152000-06-17 13:12:39 +0000393** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000394**
395** Space to hold the name is obtained from sqliteMalloc() and must
396** be freed by the calling function.
397*/
drhcce7d172000-05-31 15:34:51 +0000398char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000399 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000400 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000401 return zName;
402}
403
404/*
drhe0bc4042002-06-25 01:09:11 +0000405** Generate code to open the appropriate master table. The table
406** opened will be SQLITE_MASTER for persistent tables and
407** SQLITE_TEMP_MASTER for temporary tables. The table is opened
408** on cursor 0.
409*/
410void sqliteOpenMasterTable(Vdbe *v, int isTemp){
drh001bbcb2003-03-19 03:14:00 +0000411 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
412 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
drhe0bc4042002-06-25 01:09:11 +0000413}
414
415/*
drh75897232000-05-29 14:26:00 +0000416** Begin constructing a new table representation in memory. This is
417** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000418** to a CREATE TABLE statement. In particular, this routine is called
419** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000420** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000421** flag is true if the table should be stored in the auxiliary database
422** file instead of in the main database file. This is normally the case
423** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000424** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000425**
drhf57b3392001-10-08 13:22:32 +0000426** The new table record is initialized and put in pParse->pNewTable.
427** As more of the CREATE TABLE statement is parsed, additional action
428** routines will be called to add more information to this record.
429** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
430** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000431*/
drhe5f9c642003-01-13 23:27:31 +0000432void sqliteStartTable(
433 Parse *pParse, /* Parser context */
434 Token *pStart, /* The "CREATE" token */
435 Token *pName, /* Name of table or view to create */
436 int isTemp, /* True if this is a TEMP table */
437 int isView /* True if this is a VIEW */
438){
drh75897232000-05-29 14:26:00 +0000439 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000440 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000441 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000442 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000443 Vdbe *v;
drh1c2d8412003-03-31 00:30:47 +0000444 int iDb;
drh75897232000-05-29 14:26:00 +0000445
446 pParse->sFirstToken = *pStart;
447 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000448 if( zName==0 ) return;
drhd24cc422003-03-27 12:51:24 +0000449 if( pParse->iDb==1 ) isTemp = 1;
drhe5f9c642003-01-13 23:27:31 +0000450#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +0000451 assert( (isTemp & 1)==isTemp );
drhe5f9c642003-01-13 23:27:31 +0000452 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drh77ad4e42003-01-14 02:49:27 +0000453 sqliteFree(zName);
drhed6c8672003-01-12 18:02:16 +0000454 return;
455 }
drhe5f9c642003-01-13 23:27:31 +0000456 {
457 int code;
458 if( isView ){
459 if( isTemp ){
460 code = SQLITE_CREATE_TEMP_VIEW;
461 }else{
462 code = SQLITE_CREATE_VIEW;
463 }
464 }else{
465 if( isTemp ){
466 code = SQLITE_CREATE_TEMP_TABLE;
467 }else{
468 code = SQLITE_CREATE_TABLE;
469 }
470 }
471 if( sqliteAuthCheck(pParse, code, zName, 0) ){
drh77ad4e42003-01-14 02:49:27 +0000472 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000473 return;
474 }
475 }
476#endif
477
drhf57b3392001-10-08 13:22:32 +0000478
479 /* Before trying to create a temporary table, make sure the Btree for
480 ** holding temporary tables is open.
481 */
drhd24cc422003-03-27 12:51:24 +0000482 if( isTemp && db->aDb[1].pBt==0 && !pParse->explain ){
drh13bff812003-04-15 01:19:47 +0000483 int rc = sqliteBtreeFactory(db, 0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000484 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000485 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000486 "file for storing temporary tables", 0);
487 pParse->nErr++;
488 return;
489 }
490 if( db->flags & SQLITE_InTrans ){
drh001bbcb2003-03-19 03:14:00 +0000491 rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000492 if( rc!=SQLITE_OK ){
493 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000494 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000495 pParse->nErr++;
496 return;
497 }
498 }
499 }
500
501 /* Make sure the new table name does not collide with an existing
502 ** index or table name. Issue an error message if it does.
503 **
504 ** If we are re-reading the sqlite_master table because of a schema
505 ** change and a new permanent table is found whose name collides with
drhd24cc422003-03-27 12:51:24 +0000506 ** an existing temporary table, that is not an error.
drhf57b3392001-10-08 13:22:32 +0000507 */
drhd24cc422003-03-27 12:51:24 +0000508 pTable = sqliteFindTable(db, zName, 0);
drh1c2d8412003-03-31 00:30:47 +0000509 iDb = isTemp ? 1 : pParse->iDb;
510 if( pTable!=0 && (pTable->iDb==iDb || !pParse->initFlag) ){
drhd24cc422003-03-27 12:51:24 +0000511 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
512 " already exists", 0, 0);
513 sqliteFree(zName);
514 pParse->nErr++;
515 return;
drh75897232000-05-29 14:26:00 +0000516 }
drhd24cc422003-03-27 12:51:24 +0000517 if( (pIdx = sqliteFindIndex(db, zName, 0))!=0 &&
518 (pIdx->iDb==0 || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000519 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
520 zName, 0);
drh75897232000-05-29 14:26:00 +0000521 sqliteFree(zName);
522 pParse->nErr++;
523 return;
524 }
525 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000526 if( pTable==0 ){
527 sqliteFree(zName);
528 return;
529 }
drh75897232000-05-29 14:26:00 +0000530 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000531 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000532 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000533 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000534 pTable->pIndex = 0;
drh1c2d8412003-03-31 00:30:47 +0000535 pTable->iDb = iDb;
drhbe0072d2001-09-13 14:46:09 +0000536 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000537 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000538
539 /* Begin generating the code that will insert the table record into
540 ** the SQLITE_MASTER table. Note in particular that we must go ahead
541 ** and allocate the record number for the table entry now. Before any
542 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
543 ** indices to be created and the table record must come before the
544 ** indices. Hence, the record number for the table must be allocated
545 ** now.
546 */
drhadbca9c2001-09-27 15:11:53 +0000547 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000548 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000549 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000550 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
551 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000552 }
drhe0bc4042002-06-25 01:09:11 +0000553 sqliteOpenMasterTable(v, isTemp);
554 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
555 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
556 sqliteVdbeAddOp(v, OP_String, 0, 0);
557 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000558 }
drh75897232000-05-29 14:26:00 +0000559}
560
561/*
562** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000563**
564** The parser calls this routine once for each column declaration
565** in a CREATE TABLE statement. sqliteStartTable() gets called
566** first to get things going. Then this routine is called for each
567** column.
drh75897232000-05-29 14:26:00 +0000568*/
569void sqliteAddColumn(Parse *pParse, Token *pName){
570 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000571 int i;
572 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000573 Column *pCol;
drh75897232000-05-29 14:26:00 +0000574 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000575 sqliteSetNString(&z, pName->z, pName->n, 0);
576 if( z==0 ) return;
577 sqliteDequote(z);
578 for(i=0; i<p->nCol; i++){
579 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
580 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
581 pParse->nErr++;
582 sqliteFree(z);
583 return;
584 }
585 }
drh75897232000-05-29 14:26:00 +0000586 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000587 Column *aNew;
588 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
589 if( aNew==0 ) return;
590 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000591 }
drhc9b84a12002-06-20 11:36:48 +0000592 pCol = &p->aCol[p->nCol];
593 memset(pCol, 0, sizeof(p->aCol[0]));
594 pCol->zName = z;
595 pCol->sortOrder = SQLITE_SO_NUM;
596 p->nCol++;
drh75897232000-05-29 14:26:00 +0000597}
598
599/*
drh382c0242001-10-06 16:33:02 +0000600** This routine is called by the parser while in the middle of
601** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
602** been seen on a column. This routine sets the notNull flag on
603** the column currently under construction.
604*/
drh9cfcf5d2002-01-29 18:41:24 +0000605void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000606 Table *p;
607 int i;
608 if( (p = pParse->pNewTable)==0 ) return;
609 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000610 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000611}
612
613/*
614** This routine is called by the parser while in the middle of
615** parsing a CREATE TABLE statement. The pFirst token is the first
616** token in the sequence of tokens that describe the type of the
617** column currently under construction. pLast is the last token
618** in the sequence. Use this information to construct a string
619** that contains the typename of the column and store that string
620** in zType.
621*/
622void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
623 Table *p;
624 int i, j;
625 int n;
626 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000627 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000628 if( (p = pParse->pNewTable)==0 ) return;
629 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000630 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000631 pCol = &p->aCol[i];
632 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000633 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000634 sqliteSetNString(pz, pFirst->z, n, 0);
635 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000636 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000637 for(i=j=0; z[i]; i++){
638 int c = z[i];
639 if( isspace(c) ) continue;
640 z[j++] = c;
641 }
642 z[j] = 0;
drh3d037a92002-08-15 01:26:09 +0000643 if( pParse->db->file_format>=4 ){
drhfcb78a42003-01-18 20:11:05 +0000644 pCol->sortOrder = sqliteCollateType(z, n);
645 }else{
646 pCol->sortOrder = SQLITE_SO_NUM;
drhc9b84a12002-06-20 11:36:48 +0000647 }
drh382c0242001-10-06 16:33:02 +0000648}
649
650/*
drh7020f652000-06-03 18:06:52 +0000651** The given token is the default value for the last column added to
652** the table currently under construction. If "minusFlag" is true, it
653** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000654**
655** This routine is called by the parser while in the middle of
656** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000657*/
658void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
659 Table *p;
660 int i;
661 char **pz;
662 if( (p = pParse->pNewTable)==0 ) return;
663 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000664 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000665 pz = &p->aCol[i].zDflt;
666 if( minusFlag ){
667 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
668 }else{
669 sqliteSetNString(pz, pVal->z, pVal->n, 0);
670 }
671 sqliteDequote(*pz);
672}
673
674/*
drh4a324312001-12-21 14:30:42 +0000675** Designate the PRIMARY KEY for the table. pList is a list of names
676** of columns that form the primary key. If pList is NULL, then the
677** most recently added column of the table is the primary key.
678**
679** A table can have at most one primary key. If the table already has
680** a primary key (and this is the second primary key) then create an
681** error.
682**
683** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
684** then we will try to use that column as the row id. (Exception:
685** For backwards compatibility with older databases, do not do this
686** if the file format version number is less than 1.) Set the Table.iPKey
687** field of the table under construction to be the index of the
688** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
689** no INTEGER PRIMARY KEY.
690**
691** If the key is not an INTEGER PRIMARY KEY, then create a unique
692** index for the key. No index is created for INTEGER PRIMARY KEYs.
693*/
drh9cfcf5d2002-01-29 18:41:24 +0000694void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000695 Table *pTab = pParse->pNewTable;
696 char *zType = 0;
697 int iCol = -1;
drhe0194f22003-02-26 13:52:51 +0000698 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000699 if( pTab->hasPrimKey ){
700 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
701 "\" has more than one primary key", 0);
702 pParse->nErr++;
drhe0194f22003-02-26 13:52:51 +0000703 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000704 }
705 pTab->hasPrimKey = 1;
706 if( pList==0 ){
707 iCol = pTab->nCol - 1;
708 }else if( pList->nId==1 ){
709 for(iCol=0; iCol<pTab->nCol; iCol++){
710 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
711 }
712 }
713 if( iCol>=0 && iCol<pTab->nCol ){
714 zType = pTab->aCol[iCol].zType;
715 }
716 if( pParse->db->file_format>=1 &&
717 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
718 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000719 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000720 }else{
drhd24cc422003-03-27 12:51:24 +0000721 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000722 pList = 0;
drh4a324312001-12-21 14:30:42 +0000723 }
drhe0194f22003-02-26 13:52:51 +0000724
725primary_key_exit:
726 sqliteIdListDelete(pList);
727 return;
drh4a324312001-12-21 14:30:42 +0000728}
729
730/*
drhfcb78a42003-01-18 20:11:05 +0000731** Return the appropriate collating type given a type name.
732**
733** The collation type is text (SQLITE_SO_TEXT) if the type
734** name contains the character stream "text" or "blob" or
735** "clob". Any other type name is collated as numeric
736** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000737*/
drhfcb78a42003-01-18 20:11:05 +0000738int sqliteCollateType(const char *zType, int nType){
739 int i;
drhfcb78a42003-01-18 20:11:05 +0000740 for(i=0; i<nType-1; i++){
741 switch( zType[i] ){
742 case 'b':
743 case 'B': {
744 if( i<nType-3 && sqliteStrNICmp(&zType[i],"blob",4)==0 ){
745 return SQLITE_SO_TEXT;
746 }
747 break;
748 }
749 case 'c':
750 case 'C': {
751 if( i<nType-3 && (sqliteStrNICmp(&zType[i],"char",4)==0 ||
752 sqliteStrNICmp(&zType[i],"clob",4)==0)
753 ){
754 return SQLITE_SO_TEXT;
755 }
756 break;
757 }
758 case 'x':
759 case 'X': {
760 if( i>=2 && sqliteStrNICmp(&zType[i-2],"text",4)==0 ){
761 return SQLITE_SO_TEXT;
762 }
763 break;
764 }
765 default: {
766 break;
767 }
768 }
drh8e2ca022002-06-17 17:07:19 +0000769 }
drhfcb78a42003-01-18 20:11:05 +0000770 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000771}
772
773/*
774** This routine is called by the parser while in the middle of
775** parsing a CREATE TABLE statement. A "COLLATE" clause has
776** been seen on a column. This routine sets the Column.sortOrder on
777** the column currently under construction.
778*/
779void sqliteAddCollateType(Parse *pParse, int collType){
780 Table *p;
781 int i;
782 if( (p = pParse->pNewTable)==0 ) return;
783 i = p->nCol-1;
784 if( i>=0 ) p->aCol[i].sortOrder = collType;
785}
786
787/*
drh50e5dad2001-09-15 00:57:28 +0000788** Come up with a new random value for the schema cookie. Make sure
789** the new value is different from the old.
790**
791** The schema cookie is used to determine when the schema for the
792** database changes. After each schema change, the cookie value
793** changes. When a process first reads the schema it records the
794** cookie. Thereafter, whenever it goes to access the database,
795** it checks the cookie to make sure the schema has not changed
796** since it was last read.
797**
798** This plan is not completely bullet-proof. It is possible for
799** the schema to change multiple times and for the cookie to be
800** set back to prior value. But schema changes are infrequent
801** and the probability of hitting the same cookie value is only
802** 1 chance in 2^32. So we're safe enough.
803*/
drhe0bc4042002-06-25 01:09:11 +0000804void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh001bbcb2003-03-19 03:14:00 +0000805 if( db->next_cookie==db->aDb[0].schema_cookie ){
806 db->next_cookie = db->aDb[0].schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000807 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000808 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
809 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000810 }
811}
812
813/*
drh969fa7c2002-02-18 18:30:32 +0000814** Measure the number of characters needed to output the given
815** identifier. The number returned includes any quotes used
816** but does not include the null terminator.
817*/
818static int identLength(const char *z){
819 int n;
drh17f71932002-02-21 12:01:27 +0000820 int needQuote = 0;
821 for(n=0; *z; n++, z++){
822 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000823 }
drh17f71932002-02-21 12:01:27 +0000824 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000825}
826
827/*
828** Write an identifier onto the end of the given string. Add
829** quote characters as needed.
830*/
831static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000832 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000833 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000834 for(j=0; zIdent[j]; j++){
835 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
836 }
837 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
838 || sqliteKeywordCode(zIdent, j)!=TK_ID;
839 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000840 for(j=0; zIdent[j]; j++){
841 z[i++] = zIdent[j];
842 if( zIdent[j]=='\'' ) z[i++] = '\'';
843 }
drh17f71932002-02-21 12:01:27 +0000844 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000845 z[i] = 0;
846 *pIdx = i;
847}
848
849/*
850** Generate a CREATE TABLE statement appropriate for the given
851** table. Memory to hold the text of the statement is obtained
852** from sqliteMalloc() and must be freed by the calling function.
853*/
854static char *createTableStmt(Table *p){
855 int i, k, n;
856 char *zStmt;
857 char *zSep, *zSep2, *zEnd;
858 n = 0;
859 for(i=0; i<p->nCol; i++){
860 n += identLength(p->aCol[i].zName);
861 }
862 n += identLength(p->zName);
863 if( n<40 ){
864 zSep = "";
865 zSep2 = ",";
866 zEnd = ")";
867 }else{
868 zSep = "\n ";
869 zSep2 = ",\n ";
870 zEnd = "\n)";
871 }
drhe0bc4042002-06-25 01:09:11 +0000872 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000873 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000874 if( zStmt==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +0000875 strcpy(zStmt, p->iDb==1 ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000876 k = strlen(zStmt);
877 identPut(zStmt, &k, p->zName);
878 zStmt[k++] = '(';
879 for(i=0; i<p->nCol; i++){
880 strcpy(&zStmt[k], zSep);
881 k += strlen(&zStmt[k]);
882 zSep = zSep2;
883 identPut(zStmt, &k, p->aCol[i].zName);
884 }
885 strcpy(&zStmt[k], zEnd);
886 return zStmt;
887}
888
889/*
drh75897232000-05-29 14:26:00 +0000890** This routine is called to report the final ")" that terminates
891** a CREATE TABLE statement.
892**
drhf57b3392001-10-08 13:22:32 +0000893** The table structure that other action routines have been building
894** is added to the internal hash tables, assuming no errors have
895** occurred.
drh75897232000-05-29 14:26:00 +0000896**
drh1ccde152000-06-17 13:12:39 +0000897** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000898** unless this is a temporary table or initFlag==1. When initFlag==1,
899** it means we are reading the sqlite_master table because we just
900** connected to the database or because the sqlite_master table has
901** recently changes, so the entry for this table already exists in
902** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000903**
904** If the pSelect argument is not NULL, it means that this routine
905** was called to create a table generated from a
906** "CREATE TABLE ... AS SELECT ..." statement. The column names of
907** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000908*/
drh969fa7c2002-02-18 18:30:32 +0000909void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000910 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000911 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000912
drh969fa7c2002-02-18 18:30:32 +0000913 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000914 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000915 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000916
drh969fa7c2002-02-18 18:30:32 +0000917 /* If the table is generated from a SELECT, then construct the
918 ** list of columns and the text of the table.
919 */
920 if( pSelect ){
921 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000922 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000923 assert( p->aCol==0 );
924 p->nCol = pSelTab->nCol;
925 p->aCol = pSelTab->aCol;
926 pSelTab->nCol = 0;
927 pSelTab->aCol = 0;
928 sqliteDeleteTable(0, pSelTab);
929 }
930
drhd78eeee2001-09-13 16:18:53 +0000931 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000932 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
933 ** So do not write to the disk again. Extract the root page number
934 ** for the table from the pParse->newTnum field. (The page number
935 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000936 */
937 if( pParse->initFlag ){
938 p->tnum = pParse->newTnum;
939 }
940
drhe3c41372001-09-17 20:25:58 +0000941 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000942 ** in the SQLITE_MASTER table of the database. The record number
943 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000944 **
drhe0bc4042002-06-25 01:09:11 +0000945 ** If this is a TEMPORARY table, write the entry into the auxiliary
946 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000947 */
948 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000949 int n;
drhd8bc7082000-06-07 23:51:50 +0000950 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000951
drhd8bc7082000-06-07 23:51:50 +0000952 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000953 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000954 if( p->pSelect==0 ){
955 /* A regular table */
drhd24cc422003-03-27 12:51:24 +0000956 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->iDb);
drh4ff6dfa2002-03-03 23:06:00 +0000957 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
958 }else{
959 /* A view */
960 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
961 }
drh969fa7c2002-02-18 18:30:32 +0000962 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000963 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
964 sqliteVdbeAddOp(v, OP_String, 0, 0);
965 if( p->pSelect==0 ){
966 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
967 }else{
968 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000969 }
drhe0bc4042002-06-25 01:09:11 +0000970 sqliteVdbeAddOp(v, OP_String, 0, 0);
971 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
972 sqliteVdbeAddOp(v, OP_String, 0, 0);
973 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
974 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
975 sqliteVdbeAddOp(v, OP_String, 0, 0);
976 if( pSelect ){
977 char *z = createTableStmt(p);
978 n = z ? strlen(z) : 0;
979 sqliteVdbeChangeP3(v, -1, z, n);
980 sqliteFree(z);
981 }else{
982 assert( pEnd!=0 );
983 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
984 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
985 }
986 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
987 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhd24cc422003-03-27 12:51:24 +0000988 if( !p->iDb ){
drhe0bc4042002-06-25 01:09:11 +0000989 sqliteChangeCookie(db, v);
990 }
991 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000992 if( pSelect ){
drhd24cc422003-03-27 12:51:24 +0000993 sqliteVdbeAddOp(v, OP_Integer, p->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +0000994 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +0000995 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000996 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000997 }
drh1c928532002-01-31 15:54:21 +0000998 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000999 }
drh17e9e292003-02-01 13:53:28 +00001000
1001 /* Add the table to the in-memory representation of the database.
1002 */
drhd24cc422003-03-27 12:51:24 +00001003 if( pParse->explain==0 && pParse->nErr==0 ){
drh17e9e292003-02-01 13:53:28 +00001004 Table *pOld;
1005 FKey *pFKey;
drhd24cc422003-03-27 12:51:24 +00001006 pOld = sqliteHashInsert(&db->aDb[p->iDb].tblHash,
1007 p->zName, strlen(p->zName)+1, p);
drh17e9e292003-02-01 13:53:28 +00001008 if( pOld ){
1009 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
1010 return;
1011 }
1012 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
1013 int nTo = strlen(pFKey->zTo) + 1;
drhd24cc422003-03-27 12:51:24 +00001014 pFKey->pNextTo = sqliteHashFind(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo);
1015 sqliteHashInsert(&db->aDb[p->iDb].aFKey, pFKey->zTo, nTo, pFKey);
drh17e9e292003-02-01 13:53:28 +00001016 }
1017 pParse->pNewTable = 0;
1018 db->nTable++;
1019 db->flags |= SQLITE_InternChanges;
1020 }
drh75897232000-05-29 14:26:00 +00001021}
1022
1023/*
drha76b5df2002-02-23 02:32:10 +00001024** The parser calls this routine in order to create a new VIEW
1025*/
1026void sqliteCreateView(
1027 Parse *pParse, /* The parsing context */
1028 Token *pBegin, /* The CREATE token that begins the statement */
1029 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +00001030 Select *pSelect, /* A SELECT statement that will become the new view */
1031 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +00001032){
drha76b5df2002-02-23 02:32:10 +00001033 Table *p;
drh4b59ab52002-08-24 18:24:51 +00001034 int n;
drh4ff6dfa2002-03-03 23:06:00 +00001035 const char *z;
drh4b59ab52002-08-24 18:24:51 +00001036 Token sEnd;
drha76b5df2002-02-23 02:32:10 +00001037
drhe5f9c642003-01-13 23:27:31 +00001038 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +00001039 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +00001040 if( p==0 || pParse->nErr ){
drh417be792002-03-03 18:59:40 +00001041 sqliteSelectDelete(pSelect);
1042 return;
1043 }
drh174b6192002-12-03 02:22:52 +00001044
drh4b59ab52002-08-24 18:24:51 +00001045 /* Make a copy of the entire SELECT statement that defines the view.
1046 ** This will force all the Expr.token.z values to be dynamically
1047 ** allocated rather than point to the input string - which means that
1048 ** they will persist after the current sqlite_exec() call returns.
1049 */
1050 p->pSelect = sqliteSelectDup(pSelect);
1051 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +00001052 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +00001053 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +00001054 }
drh4b59ab52002-08-24 18:24:51 +00001055
1056 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
1057 ** the end.
1058 */
drha76b5df2002-02-23 02:32:10 +00001059 sEnd = pParse->sLastToken;
1060 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
1061 sEnd.z += sEnd.n;
1062 }
1063 sEnd.n = 0;
1064 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +00001065 z = pBegin->z;
1066 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
1067 sEnd.z = &z[n-1];
1068 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +00001069
1070 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
1071 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +00001072 return;
drh417be792002-03-03 18:59:40 +00001073}
drha76b5df2002-02-23 02:32:10 +00001074
drh417be792002-03-03 18:59:40 +00001075/*
1076** The Table structure pTable is really a VIEW. Fill in the names of
1077** the columns of the view in the pTable structure. Return the number
1078** of errors. If an error is seen leave an error message in pPare->zErrMsg.
1079*/
1080int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
1081 ExprList *pEList;
1082 Select *pSel;
1083 Table *pSelTab;
1084 int nErr = 0;
1085
1086 assert( pTable );
1087
1088 /* A positive nCol means the columns names for this view are
1089 ** already known.
1090 */
1091 if( pTable->nCol>0 ) return 0;
1092
1093 /* A negative nCol is a special marker meaning that we are currently
1094 ** trying to compute the column names. If we enter this routine with
1095 ** a negative nCol, it means two or more views form a loop, like this:
1096 **
1097 ** CREATE VIEW one AS SELECT * FROM two;
1098 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001099 **
1100 ** Actually, this error is caught previously and so the following test
1101 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001102 */
1103 if( pTable->nCol<0 ){
1104 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
1105 " is circularly defined", 0);
1106 pParse->nErr++;
1107 return 1;
1108 }
1109
1110 /* If we get this far, it means we need to compute the table names.
1111 */
1112 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1113 pSel = pTable->pSelect;
1114
1115 /* Note that the call to sqliteResultSetOfSelect() will expand any
1116 ** "*" elements in this list. But we will need to restore the list
1117 ** back to its original configuration afterwards, so we save a copy of
1118 ** the original in pEList.
1119 */
1120 pEList = pSel->pEList;
1121 pSel->pEList = sqliteExprListDup(pEList);
1122 if( pSel->pEList==0 ){
1123 pSel->pEList = pEList;
1124 return 1; /* Malloc failed */
1125 }
1126 pTable->nCol = -1;
1127 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1128 if( pSelTab ){
1129 assert( pTable->aCol==0 );
1130 pTable->nCol = pSelTab->nCol;
1131 pTable->aCol = pSelTab->aCol;
1132 pSelTab->nCol = 0;
1133 pSelTab->aCol = 0;
1134 sqliteDeleteTable(0, pSelTab);
drhd24cc422003-03-27 12:51:24 +00001135 pParse->db->aDb[pTable->iDb].flags |= SQLITE_UnresetViews;
drh417be792002-03-03 18:59:40 +00001136 }else{
1137 pTable->nCol = 0;
1138 nErr++;
1139 }
1140 sqliteSelectUnbind(pSel);
1141 sqliteExprListDelete(pSel->pEList);
1142 pSel->pEList = pEList;
1143 return nErr;
1144}
1145
1146/*
1147** Clear the column names from the VIEW pTable.
1148**
1149** This routine is called whenever any other table or view is modified.
1150** The view passed into this routine might depend directly or indirectly
1151** on the modified or deleted table so we need to clear the old column
1152** names so that they will be recomputed.
1153*/
1154static void sqliteViewResetColumnNames(Table *pTable){
1155 int i;
1156 if( pTable==0 || pTable->pSelect==0 ) return;
1157 if( pTable->nCol==0 ) return;
1158 for(i=0; i<pTable->nCol; i++){
1159 sqliteFree(pTable->aCol[i].zName);
1160 sqliteFree(pTable->aCol[i].zDflt);
1161 sqliteFree(pTable->aCol[i].zType);
1162 }
1163 sqliteFree(pTable->aCol);
1164 pTable->aCol = 0;
1165 pTable->nCol = 0;
1166}
1167
1168/*
1169** Clear the column names from every VIEW.
1170*/
drhd24cc422003-03-27 12:51:24 +00001171static void sqliteViewResetAll(sqlite *db, int idx){
drh417be792002-03-03 18:59:40 +00001172 HashElem *i;
drhd24cc422003-03-27 12:51:24 +00001173 if( (db->aDb[idx].flags & SQLITE_UnresetViews)==0 ) return;
1174 for(i=sqliteHashFirst(&db->aDb[idx].tblHash); i; i=sqliteHashNext(i)){
drh417be792002-03-03 18:59:40 +00001175 Table *pTab = sqliteHashData(i);
1176 if( pTab->pSelect ){
1177 sqliteViewResetColumnNames(pTab);
1178 }
1179 }
drhd24cc422003-03-27 12:51:24 +00001180 db->aDb[idx].flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001181}
1182
1183/*
drh75897232000-05-29 14:26:00 +00001184** Given a token, look up a table with that name. If not found, leave
1185** an error for the parser to find and return NULL.
1186*/
drhcce7d172000-05-31 15:34:51 +00001187Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001188 char *zName;
1189 Table *pTab;
1190 zName = sqliteTableNameFromToken(pTok);
1191 if( zName==0 ) return 0;
drhd24cc422003-03-27 12:51:24 +00001192 pTab = sqliteFindTable(pParse->db, zName, 0);
drh75897232000-05-29 14:26:00 +00001193 sqliteFree(zName);
1194 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001195 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1196 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001197 pParse->nErr++;
1198 }
1199 return pTab;
1200}
1201
1202/*
1203** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001204** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001205*/
drh4ff6dfa2002-03-03 23:06:00 +00001206void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001207 Table *pTable;
drh75897232000-05-29 14:26:00 +00001208 Vdbe *v;
1209 int base;
drh5edc3122001-09-13 21:53:09 +00001210 sqlite *db = pParse->db;
drhd24cc422003-03-27 12:51:24 +00001211 int iDb;
drh75897232000-05-29 14:26:00 +00001212
drhdaffd0e2001-04-11 14:28:42 +00001213 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001214 pTable = sqliteTableFromToken(pParse, pName);
1215 if( pTable==0 ) return;
drhd24cc422003-03-27 12:51:24 +00001216 iDb = pTable->iDb;
drhe5f9c642003-01-13 23:27:31 +00001217#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001218 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTable->iDb),0)){
drhed6c8672003-01-12 18:02:16 +00001219 return;
1220 }
drhe5f9c642003-01-13 23:27:31 +00001221 {
1222 int code;
1223 if( isView ){
drhd24cc422003-03-27 12:51:24 +00001224 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001225 code = SQLITE_DROP_TEMP_VIEW;
1226 }else{
1227 code = SQLITE_DROP_VIEW;
1228 }
1229 }else{
drhd24cc422003-03-27 12:51:24 +00001230 if( iDb==1 ){
drhe5f9c642003-01-13 23:27:31 +00001231 code = SQLITE_DROP_TEMP_TABLE;
1232 }else{
1233 code = SQLITE_DROP_TABLE;
1234 }
1235 }
1236 if( sqliteAuthCheck(pParse, code, pTable->zName, 0) ){
1237 return;
1238 }
drh77ad4e42003-01-14 02:49:27 +00001239 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0) ){
1240 return;
1241 }
drhe5f9c642003-01-13 23:27:31 +00001242 }
1243#endif
drh75897232000-05-29 14:26:00 +00001244 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001245 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1246 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001247 pParse->nErr++;
1248 return;
1249 }
drh4ff6dfa2002-03-03 23:06:00 +00001250 if( isView && pTable->pSelect==0 ){
1251 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1252 pTable->zName, 0);
1253 pParse->nErr++;
1254 return;
1255 }
1256 if( !isView && pTable->pSelect ){
1257 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1258 pTable->zName, 0);
1259 pParse->nErr++;
1260 return;
1261 }
drh75897232000-05-29 14:26:00 +00001262
drh1ccde152000-06-17 13:12:39 +00001263 /* Generate code to remove the table from the master table
1264 ** on disk.
1265 */
drhd8bc7082000-06-07 23:51:50 +00001266 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001267 if( v ){
1268 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001269 { OP_Rewind, 0, ADDR(8), 0},
1270 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001271 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001272 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001273 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001274 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001275 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001276 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001277 };
1278 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001279 Trigger *pTrigger;
drhd24cc422003-03-27 12:51:24 +00001280 sqliteBeginWriteOperation(pParse, 0, pTable->iDb);
1281 sqliteOpenMasterTable(v, pTable->iDb);
danielk1977c3f9bad2002-05-15 08:30:12 +00001282 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001283 pTrigger = pTable->pTrigger;
1284 while( pTrigger ){
drhd24cc422003-03-27 12:51:24 +00001285 SrcList *pNm;
1286 assert( pTrigger->iDb==pTable->iDb );
1287 pNm = sqliteSrcListAppend(0, 0, 0);
1288 pNm->a[0].zName = sqliteStrDup(pTrigger->name);
1289 pNm->a[0].zDatabase = sqliteStrDup(db->aDb[pTable->iDb].zName);
1290 sqliteDropTrigger(pParse, pNm, 1);
drhe0bc4042002-06-25 01:09:11 +00001291 if( pParse->explain ){
1292 pTrigger = pTrigger->pNext;
1293 }else{
1294 pTrigger = pTable->pTrigger;
1295 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001296 }
drhe0bc4042002-06-25 01:09:11 +00001297 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1298 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001299 if( !pTable->iDb ){
drhe0bc4042002-06-25 01:09:11 +00001300 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001301 }
drhe0bc4042002-06-25 01:09:11 +00001302 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001303 if( !isView ){
drhd24cc422003-03-27 12:51:24 +00001304 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001305 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drhd24cc422003-03-27 12:51:24 +00001306 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->iDb);
drh4ff6dfa2002-03-03 23:06:00 +00001307 }
drh5e00f6c2001-09-13 13:46:56 +00001308 }
drh1c928532002-01-31 15:54:21 +00001309 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001310 }
1311
drhe0bc4042002-06-25 01:09:11 +00001312 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001313 **
1314 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001315 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001316 */
1317 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001318 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001319 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001320 }
drhd24cc422003-03-27 12:51:24 +00001321 sqliteViewResetAll(db, iDb);
drh75897232000-05-29 14:26:00 +00001322}
1323
1324/*
drh38640e12002-07-05 21:42:36 +00001325** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1326** opcode and adds that P3 string to the most recently inserted instruction
1327** in the virtual machine. The P3 string consists of a single character
1328** for each column in the index pIdx of table pTab. If the column uses
1329** a numeric sort order, then the P3 string character corresponding to
1330** that column is 'n'. If the column uses a text sort order, then the
1331** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1332** additional information. See also the sqliteAddKeyType() routine.
1333*/
1334void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1335 char *zType;
1336 Table *pTab;
1337 int i, n;
1338 assert( pIdx!=0 && pIdx->pTable!=0 );
1339 pTab = pIdx->pTable;
1340 n = pIdx->nColumn;
drh8c1238a2003-01-02 14:43:55 +00001341 zType = sqliteMallocRaw( n+1 );
drh38640e12002-07-05 21:42:36 +00001342 if( zType==0 ) return;
1343 for(i=0; i<n; i++){
1344 int iCol = pIdx->aiColumn[i];
1345 assert( iCol>=0 && iCol<pTab->nCol );
1346 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1347 zType[i] = 't';
1348 }else{
1349 zType[i] = 'n';
1350 }
1351 }
1352 zType[n] = 0;
1353 sqliteVdbeChangeP3(v, -1, zType, n);
1354 sqliteFree(zType);
1355}
1356
1357/*
drhc2eef3b2002-08-31 18:53:06 +00001358** This routine is called to create a new foreign key on the table
1359** currently under construction. pFromCol determines which columns
1360** in the current table point to the foreign key. If pFromCol==0 then
1361** connect the key to the last column inserted. pTo is the name of
1362** the table referred to. pToCol is a list of tables in the other
1363** pTo table that the foreign key points to. flags contains all
1364** information about the conflict resolution algorithms specified
1365** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1366**
1367** An FKey structure is created and added to the table currently
1368** under construction in the pParse->pNewTable field. The new FKey
1369** is not linked into db->aFKey at this point - that does not happen
1370** until sqliteEndTable().
1371**
1372** The foreign key is set for IMMEDIATE processing. A subsequent call
1373** to sqliteDeferForeignKey() might change this to DEFERRED.
1374*/
1375void sqliteCreateForeignKey(
1376 Parse *pParse, /* Parsing context */
1377 IdList *pFromCol, /* Columns in this table that point to other table */
1378 Token *pTo, /* Name of the other table */
1379 IdList *pToCol, /* Columns in the other table */
1380 int flags /* Conflict resolution algorithms. */
1381){
1382 Table *p = pParse->pNewTable;
1383 int nByte;
1384 int i;
1385 int nCol;
1386 char *z;
1387 FKey *pFKey = 0;
1388
1389 assert( pTo!=0 );
1390 if( p==0 || pParse->nErr ) goto fk_end;
1391 if( pFromCol==0 ){
1392 int iCol = p->nCol-1;
1393 if( iCol<0 ) goto fk_end;
1394 if( pToCol && pToCol->nId!=1 ){
1395 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1396 p->aCol[iCol].zName, -1,
1397 " should reference only one column of table ", -1,
1398 pTo->z, pTo->n, 0);
1399 pParse->nErr++;
1400 goto fk_end;
1401 }
1402 nCol = 1;
1403 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1404 sqliteSetString(&pParse->zErrMsg,
1405 "number of columns in foreign key does not match the number of "
1406 "columns in the referenced table", 0);
1407 pParse->nErr++;
1408 goto fk_end;
1409 }else{
1410 nCol = pFromCol->nId;
1411 }
1412 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1413 if( pToCol ){
1414 for(i=0; i<pToCol->nId; i++){
1415 nByte += strlen(pToCol->a[i].zName) + 1;
1416 }
1417 }
1418 pFKey = sqliteMalloc( nByte );
1419 if( pFKey==0 ) goto fk_end;
1420 pFKey->pFrom = p;
1421 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001422 z = (char*)&pFKey[1];
1423 pFKey->aCol = (struct sColMap*)z;
1424 z += sizeof(struct sColMap)*nCol;
1425 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001426 memcpy(z, pTo->z, pTo->n);
1427 z[pTo->n] = 0;
1428 z += pTo->n+1;
1429 pFKey->pNextTo = 0;
1430 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001431 if( pFromCol==0 ){
1432 pFKey->aCol[0].iFrom = p->nCol-1;
1433 }else{
1434 for(i=0; i<nCol; i++){
1435 int j;
1436 for(j=0; j<p->nCol; j++){
1437 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1438 pFKey->aCol[i].iFrom = j;
1439 break;
1440 }
1441 }
1442 if( j>=p->nCol ){
1443 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1444 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1445 pParse->nErr++;
1446 goto fk_end;
1447 }
1448 }
1449 }
1450 if( pToCol ){
1451 for(i=0; i<nCol; i++){
1452 int n = strlen(pToCol->a[i].zName);
1453 pFKey->aCol[i].zCol = z;
1454 memcpy(z, pToCol->a[i].zName, n);
1455 z[n] = 0;
1456 z += n+1;
1457 }
1458 }
1459 pFKey->isDeferred = 0;
1460 pFKey->deleteConf = flags & 0xff;
1461 pFKey->updateConf = (flags >> 8 ) & 0xff;
1462 pFKey->insertConf = (flags >> 16 ) & 0xff;
1463
1464 /* Link the foreign key to the table as the last step.
1465 */
1466 p->pFKey = pFKey;
1467 pFKey = 0;
1468
1469fk_end:
1470 sqliteFree(pFKey);
1471 sqliteIdListDelete(pFromCol);
1472 sqliteIdListDelete(pToCol);
1473}
1474
1475/*
1476** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1477** clause is seen as part of a foreign key definition. The isDeferred
1478** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1479** The behavior of the most recently created foreign key is adjusted
1480** accordingly.
1481*/
1482void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1483 Table *pTab;
1484 FKey *pFKey;
1485 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1486 pFKey->isDeferred = isDeferred;
1487}
1488
1489/*
drh75897232000-05-29 14:26:00 +00001490** Create a new index for an SQL table. pIndex is the name of the index
1491** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001492** be NULL for a primary key or an index that is created to satisfy a
1493** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001494** as the table to be indexed. pParse->pNewTable is a table that is
1495** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001496**
drh382c0242001-10-06 16:33:02 +00001497** pList is a list of columns to be indexed. pList will be NULL if this
1498** is a primary key or unique-constraint on the most recent column added
1499** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001500*/
1501void sqliteCreateIndex(
1502 Parse *pParse, /* All information about this parse */
1503 Token *pName, /* Name of the index. May be NULL */
drhd24cc422003-03-27 12:51:24 +00001504 SrcList *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001505 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001506 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drhd24cc422003-03-27 12:51:24 +00001507 int isTemp, /* True if this is a temporary index */
drh75897232000-05-29 14:26:00 +00001508 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1509 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1510){
1511 Table *pTab; /* Table to be indexed */
1512 Index *pIndex; /* The index to be created */
1513 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001514 int i, j;
drhf57b3392001-10-08 13:22:32 +00001515 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001516 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001517
drhdaffd0e2001-04-11 14:28:42 +00001518 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1519
drh75897232000-05-29 14:26:00 +00001520 /*
1521 ** Find the table that is to be indexed. Return early if not found.
1522 */
1523 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001524 assert( pName!=0 );
drhd24cc422003-03-27 12:51:24 +00001525 assert( pTable->nSrc==1 );
drh812d7a22003-03-27 13:50:00 +00001526 pTab = sqliteSrcListLookup(pParse, pTable);
drh75897232000-05-29 14:26:00 +00001527 }else{
drhe3c41372001-09-17 20:25:58 +00001528 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001529 pTab = pParse->pNewTable;
1530 }
1531 if( pTab==0 || pParse->nErr ) goto exit_create_index;
drh0be9df02003-03-30 00:19:49 +00001532 if( pTab->readOnly ){
1533 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1534 " may not be indexed", 0);
1535 pParse->nErr++;
1536 goto exit_create_index;
1537 }
drh1c2d8412003-03-31 00:30:47 +00001538 if( !isTemp && pTab->iDb>=2 && pParse->initFlag==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001539 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drhd24cc422003-03-27 12:51:24 +00001540 " may not have non-temporary indices added", 0);
drh75897232000-05-29 14:26:00 +00001541 pParse->nErr++;
1542 goto exit_create_index;
1543 }
drha76b5df2002-02-23 02:32:10 +00001544 if( pTab->pSelect ){
1545 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1546 pParse->nErr++;
1547 goto exit_create_index;
1548 }
drhd24cc422003-03-27 12:51:24 +00001549 if( pTab->iDb==1 ){
1550 isTemp = 1;
1551 }
drh75897232000-05-29 14:26:00 +00001552
1553 /*
1554 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001555 ** index or table with the same name.
1556 **
1557 ** Exception: If we are reading the names of permanent indices from the
1558 ** sqlite_master table (because some other process changed the schema) and
1559 ** one of the index names collides with the name of a temporary table or
drhd24cc422003-03-27 12:51:24 +00001560 ** index, then we will continue to process this index.
drhf57b3392001-10-08 13:22:32 +00001561 **
1562 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001563 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1564 ** own name.
drh75897232000-05-29 14:26:00 +00001565 */
drhd24cc422003-03-27 12:51:24 +00001566 if( pName && !pParse->initFlag ){
drhf57b3392001-10-08 13:22:32 +00001567 Index *pISameName; /* Another index with the same name */
1568 Table *pTSameName; /* A table with same name as the index */
drhd24cc422003-03-27 12:51:24 +00001569 zName = sqliteStrNDup(pName->z, pName->n);
drhe3c41372001-09-17 20:25:58 +00001570 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001571 if( (pISameName = sqliteFindIndex(db, zName, 0))!=0 ){
1572 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1573 " already exists", 0);
1574 pParse->nErr++;
1575 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001576 }
drhd24cc422003-03-27 12:51:24 +00001577 if( (pTSameName = sqliteFindTable(db, zName, 0))!=0 ){
1578 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1579 zName, 0);
1580 pParse->nErr++;
1581 goto exit_create_index;
drhe3c41372001-09-17 20:25:58 +00001582 }
drhd24cc422003-03-27 12:51:24 +00001583 }else if( pName==0 ){
drhadbca9c2001-09-27 15:11:53 +00001584 char zBuf[30];
1585 int n;
1586 Index *pLoop;
1587 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1588 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001589 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001590 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001591 if( zName==0 ) goto exit_create_index;
drhd24cc422003-03-27 12:51:24 +00001592 }else{
1593 zName = sqliteStrNDup(pName->z, pName->n);
drh75897232000-05-29 14:26:00 +00001594 }
1595
drhe5f9c642003-01-13 23:27:31 +00001596 /* Check for authorization to create an index.
1597 */
1598#ifndef SQLITE_OMIT_AUTHORIZATION
drhd24cc422003-03-27 12:51:24 +00001599 assert( isTemp==0 || isTemp==1 );
1600 assert( pTab->iDb==pParse->iDb || isTemp==1 );
1601 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drhe5f9c642003-01-13 23:27:31 +00001602 goto exit_create_index;
1603 }
1604 i = SQLITE_CREATE_INDEX;
drhd24cc422003-03-27 12:51:24 +00001605 if( isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
drhe5f9c642003-01-13 23:27:31 +00001606 if( sqliteAuthCheck(pParse, i, zName, pTab->zName) ){
1607 goto exit_create_index;
1608 }
1609#endif
1610
drh75897232000-05-29 14:26:00 +00001611 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001612 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001613 ** So create a fake list to simulate this.
1614 */
1615 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001616 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001617 nullId.n = strlen(nullId.z);
1618 pList = sqliteIdListAppend(0, &nullId);
1619 if( pList==0 ) goto exit_create_index;
1620 }
1621
1622 /*
1623 ** Allocate the index structure.
1624 */
drhdcc581c2000-05-30 13:44:19 +00001625 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001626 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001627 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001628 pIndex->aiColumn = (int*)&pIndex[1];
1629 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001630 strcpy(pIndex->zName, zName);
1631 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001632 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001633 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001634 pIndex->autoIndex = pName==0;
drhd24cc422003-03-27 12:51:24 +00001635 pIndex->iDb = isTemp ? 1 : pParse->iDb;
drh75897232000-05-29 14:26:00 +00001636
drh1ccde152000-06-17 13:12:39 +00001637 /* Scan the names of the columns of the table to be indexed and
1638 ** load the column indices into the Index structure. Report an error
1639 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001640 */
1641 for(i=0; i<pList->nId; i++){
1642 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001643 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001644 }
1645 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001646 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001647 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001648 pParse->nErr++;
1649 sqliteFree(pIndex);
1650 goto exit_create_index;
1651 }
drh967e8b72000-06-21 13:59:10 +00001652 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001653 }
1654
1655 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001656 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001657 */
drhd24cc422003-03-27 12:51:24 +00001658 if( !pParse->explain ){
drh6d4abfb2001-10-22 02:58:08 +00001659 Index *p;
drhd24cc422003-03-27 12:51:24 +00001660 p = sqliteHashInsert(&db->aDb[isTemp].idxHash,
1661 pIndex->zName, strlen(zName)+1, pIndex);
drh6d4abfb2001-10-22 02:58:08 +00001662 if( p ){
1663 assert( p==pIndex ); /* Malloc must have failed */
1664 sqliteFree(pIndex);
1665 goto exit_create_index;
1666 }
drh5e00f6c2001-09-13 13:46:56 +00001667 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001668 }
drh9cfcf5d2002-01-29 18:41:24 +00001669
1670 /* When adding an index to the list of indices for a table, make
1671 ** sure all indices labeled OE_Replace come after all those labeled
1672 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1673 ** and INSERT.
1674 */
1675 if( onError!=OE_Replace || pTab->pIndex==0
1676 || pTab->pIndex->onError==OE_Replace){
1677 pIndex->pNext = pTab->pIndex;
1678 pTab->pIndex = pIndex;
1679 }else{
1680 Index *pOther = pTab->pIndex;
1681 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1682 pOther = pOther->pNext;
1683 }
1684 pIndex->pNext = pOther->pNext;
1685 pOther->pNext = pIndex;
1686 }
drh75897232000-05-29 14:26:00 +00001687
drhd78eeee2001-09-13 16:18:53 +00001688 /* If the initFlag is 1 it means we are reading the SQL off the
1689 ** "sqlite_master" table on the disk. So do not write to the disk
1690 ** again. Extract the table number from the pParse->newTnum field.
1691 */
drhadbca9c2001-09-27 15:11:53 +00001692 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001693 pIndex->tnum = pParse->newTnum;
1694 }
1695
drh75897232000-05-29 14:26:00 +00001696 /* If the initFlag is 0 then create the index on disk. This
1697 ** involves writing the index into the master table and filling in the
1698 ** index with the current table contents.
1699 **
1700 ** The initFlag is 0 when the user first enters a CREATE INDEX
1701 ** command. The initFlag is 1 when a database is opened and
1702 ** CREATE INDEX statements are read out of the master table. In
1703 ** the latter case the index already exists on disk, which is why
1704 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001705 **
1706 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001707 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1708 ** has just been created, it contains no data and the index initialization
1709 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001710 */
drhadbca9c2001-09-27 15:11:53 +00001711 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001712 int n;
drhadbca9c2001-09-27 15:11:53 +00001713 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001714 int lbl1, lbl2;
1715 int i;
drhadbca9c2001-09-27 15:11:53 +00001716 int addr;
drh75897232000-05-29 14:26:00 +00001717
drhd8bc7082000-06-07 23:51:50 +00001718 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001719 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001720 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001721 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001722 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001723 }
drhe0bc4042002-06-25 01:09:11 +00001724 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1725 sqliteVdbeAddOp(v, OP_String, 0, 0);
1726 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1727 sqliteVdbeAddOp(v, OP_String, 0, 0);
1728 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1729 sqliteVdbeAddOp(v, OP_String, 0, 0);
1730 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001731 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1732 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001733 pIndex->tnum = 0;
1734 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001735 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00001736 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
1737 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00001738 }
drhe0bc4042002-06-25 01:09:11 +00001739 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1740 if( pStart && pEnd ){
1741 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1742 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001743 }
drhe0bc4042002-06-25 01:09:11 +00001744 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1745 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001746 if( pTable ){
drhd24cc422003-03-27 12:51:24 +00001747 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00001748 sqliteVdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00001749 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001750 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001751 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1752 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001753 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001754 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001755 }
drh99fcd712001-10-13 01:06:47 +00001756 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001757 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001758 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh483750b2003-01-29 18:46:51 +00001759 sqliteVdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
drh6b563442001-11-07 16:48:26 +00001760 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001761 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001762 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1763 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001764 }
drhadbca9c2001-09-27 15:11:53 +00001765 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001766 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001767 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001768 }
drhe0bc4042002-06-25 01:09:11 +00001769 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001770 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001771 }
drh75897232000-05-29 14:26:00 +00001772 }
1773
drh75897232000-05-29 14:26:00 +00001774 /* Clean up before exiting */
1775exit_create_index:
1776 sqliteIdListDelete(pList);
drhd24cc422003-03-27 12:51:24 +00001777 sqliteSrcListDelete(pTable);
drh75897232000-05-29 14:26:00 +00001778 sqliteFree(zName);
1779 return;
1780}
1781
1782/*
drh74e24cd2002-01-09 03:19:59 +00001783** This routine will drop an existing named index. This routine
1784** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001785*/
drhd24cc422003-03-27 12:51:24 +00001786void sqliteDropIndex(Parse *pParse, SrcList *pName){
drh75897232000-05-29 14:26:00 +00001787 Index *pIndex;
drh75897232000-05-29 14:26:00 +00001788 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001789 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001790
drhdaffd0e2001-04-11 14:28:42 +00001791 if( pParse->nErr || sqlite_malloc_failed ) return;
drhd24cc422003-03-27 12:51:24 +00001792 assert( pName->nSrc==1 );
1793 pIndex = sqliteFindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
drh75897232000-05-29 14:26:00 +00001794 if( pIndex==0 ){
drhda93d232003-03-31 02:12:46 +00001795 sqliteErrorMsg(pParse, "no such index: %S", pName, 0);
drhd24cc422003-03-27 12:51:24 +00001796 goto exit_drop_index;
drh75897232000-05-29 14:26:00 +00001797 }
drh485b39b2002-07-13 03:11:52 +00001798 if( pIndex->autoIndex ){
drhda93d232003-03-31 02:12:46 +00001799 sqliteErrorMsg(pParse, "index associated with UNIQUE "
drh485b39b2002-07-13 03:11:52 +00001800 "or PRIMARY KEY constraint cannot be dropped", 0);
drhd24cc422003-03-27 12:51:24 +00001801 goto exit_drop_index;
1802 }
1803 if( pIndex->iDb>1 ){
drhda93d232003-03-31 02:12:46 +00001804 sqliteErrorMsg(pParse, "cannot alter schema of attached "
drhd24cc422003-03-27 12:51:24 +00001805 "databases", 0);
drhd24cc422003-03-27 12:51:24 +00001806 goto exit_drop_index;
drh485b39b2002-07-13 03:11:52 +00001807 }
drhe5f9c642003-01-13 23:27:31 +00001808#ifndef SQLITE_OMIT_AUTHORIZATION
1809 {
1810 int code = SQLITE_DROP_INDEX;
1811 Table *pTab = pIndex->pTable;
drhd24cc422003-03-27 12:51:24 +00001812 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pIndex->iDb), 0) ){
1813 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001814 }
drhd24cc422003-03-27 12:51:24 +00001815 if( pIndex->iDb ) code = SQLITE_DROP_TEMP_INDEX;
drh77ad4e42003-01-14 02:49:27 +00001816 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName) ){
drhd24cc422003-03-27 12:51:24 +00001817 goto exit_drop_index;
drhe5f9c642003-01-13 23:27:31 +00001818 }
drhed6c8672003-01-12 18:02:16 +00001819 }
drhe5f9c642003-01-13 23:27:31 +00001820#endif
drh75897232000-05-29 14:26:00 +00001821
1822 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001823 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001824 if( v ){
1825 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001826 { OP_Rewind, 0, ADDR(9), 0},
1827 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001828 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001829 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001830 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001831 { OP_Eq, 0, ADDR(8), 0},
1832 { OP_Next, 0, ADDR(3), 0},
1833 { OP_Goto, 0, ADDR(9), 0},
1834 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001835 };
1836 int base;
1837
drhd24cc422003-03-27 12:51:24 +00001838 sqliteBeginWriteOperation(pParse, 0, pIndex->iDb);
1839 sqliteOpenMasterTable(v, pIndex->iDb);
drhe0bc4042002-06-25 01:09:11 +00001840 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1841 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhd24cc422003-03-27 12:51:24 +00001842 if( pIndex->iDb==0 ){
drhe0bc4042002-06-25 01:09:11 +00001843 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001844 }
drhe0bc4042002-06-25 01:09:11 +00001845 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhd24cc422003-03-27 12:51:24 +00001846 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pIndex->iDb);
drh1c928532002-01-31 15:54:21 +00001847 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001848 }
1849
drhe0bc4042002-06-25 01:09:11 +00001850 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001851 */
1852 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001853 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001854 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001855 }
drhd24cc422003-03-27 12:51:24 +00001856
1857exit_drop_index:
1858 sqliteSrcListDelete(pName);
drh75897232000-05-29 14:26:00 +00001859}
1860
1861/*
drh75897232000-05-29 14:26:00 +00001862** Append a new element to the given IdList. Create a new IdList if
1863** need be.
drhdaffd0e2001-04-11 14:28:42 +00001864**
1865** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001866*/
1867IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1868 if( pList==0 ){
1869 pList = sqliteMalloc( sizeof(IdList) );
1870 if( pList==0 ) return 0;
1871 }
1872 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001873 struct IdList_item *a;
1874 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1875 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001876 sqliteIdListDelete(pList);
1877 return 0;
drh75897232000-05-29 14:26:00 +00001878 }
drh6d4abfb2001-10-22 02:58:08 +00001879 pList->a = a;
drh75897232000-05-29 14:26:00 +00001880 }
1881 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1882 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001883 char **pz = &pList->a[pList->nId].zName;
1884 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1885 if( *pz==0 ){
1886 sqliteIdListDelete(pList);
1887 return 0;
1888 }else{
1889 sqliteDequote(*pz);
1890 }
drh75897232000-05-29 14:26:00 +00001891 }
1892 pList->nId++;
1893 return pList;
1894}
1895
1896/*
drhad3cab52002-05-24 02:04:32 +00001897** Append a new table name to the given SrcList. Create a new SrcList if
1898** need be. A new entry is created in the SrcList even if pToken is NULL.
1899**
1900** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00001901**
1902** If pDatabase is not null, it means that the table has an optional
1903** database name prefix. Like this: "database.table". The pDatabase
1904** points to the table name and the pTable points to the database name.
1905** The SrcList.a[].zName field is filled with the table name which might
1906** come from pTable (if pDatabase is NULL) or from pDatabase.
1907** SrcList.a[].zDatabase is filled with the database name from pTable,
1908** or with NULL if no database is specified.
1909**
1910** In other words, if call like this:
1911**
1912** sqliteSrcListAppend(A,B,0);
1913**
1914** Then B is a table name and the database name is unspecified. If called
1915** like this:
1916**
1917** sqliteSrcListAppend(A,B,C);
1918**
1919** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00001920*/
drh113088e2003-03-20 01:16:58 +00001921SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00001922 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00001923 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00001924 if( pList==0 ) return 0;
1925 }
drh113088e2003-03-20 01:16:58 +00001926 if( (pList->nSrc & 7)==1 ){
1927 SrcList *pNew;
1928 pNew = sqliteRealloc(pList,
1929 sizeof(*pList) + (pList->nSrc+8)*sizeof(pList->a[0]) );
1930 if( pNew==0 ){
drhad3cab52002-05-24 02:04:32 +00001931 sqliteSrcListDelete(pList);
1932 return 0;
1933 }
drh113088e2003-03-20 01:16:58 +00001934 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00001935 }
1936 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00001937 if( pDatabase && pDatabase->z==0 ){
1938 pDatabase = 0;
1939 }
1940 if( pDatabase && pTable ){
1941 Token *pTemp = pDatabase;
1942 pDatabase = pTable;
1943 pTable = pTemp;
1944 }
1945 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00001946 char **pz = &pList->a[pList->nSrc].zName;
drh113088e2003-03-20 01:16:58 +00001947 sqliteSetNString(pz, pTable->z, pTable->n, 0);
1948 if( *pz==0 ){
1949 sqliteSrcListDelete(pList);
1950 return 0;
1951 }else{
1952 sqliteDequote(*pz);
1953 }
1954 }
1955 if( pDatabase ){
1956 char **pz = &pList->a[pList->nSrc].zDatabase;
1957 sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00001958 if( *pz==0 ){
1959 sqliteSrcListDelete(pList);
1960 return 0;
1961 }else{
1962 sqliteDequote(*pz);
1963 }
1964 }
1965 pList->nSrc++;
1966 return pList;
1967}
1968
1969/*
drh75897232000-05-29 14:26:00 +00001970** Add an alias to the last identifier on the given identifier list.
1971*/
drhad3cab52002-05-24 02:04:32 +00001972void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1973 if( pList && pList->nSrc>0 ){
1974 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001975 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001976 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001977 }
1978}
1979
1980/*
drhad3cab52002-05-24 02:04:32 +00001981** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001982*/
1983void sqliteIdListDelete(IdList *pList){
1984 int i;
1985 if( pList==0 ) return;
1986 for(i=0; i<pList->nId; i++){
1987 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001988 }
1989 sqliteFree(pList->a);
1990 sqliteFree(pList);
1991}
1992
1993/*
drhad2d8302002-05-24 20:31:36 +00001994** Return the index in pList of the identifier named zId. Return -1
1995** if not found.
1996*/
1997int sqliteIdListIndex(IdList *pList, const char *zName){
1998 int i;
1999 if( pList==0 ) return -1;
2000 for(i=0; i<pList->nId; i++){
2001 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
2002 }
2003 return -1;
2004}
2005
2006/*
drhad3cab52002-05-24 02:04:32 +00002007** Delete an entire SrcList including all its substructure.
2008*/
2009void sqliteSrcListDelete(SrcList *pList){
2010 int i;
2011 if( pList==0 ) return;
2012 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00002013 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00002014 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00002015 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00002016 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00002017 sqliteDeleteTable(0, pList->a[i].pTab);
2018 }
drhff78bd22002-02-27 01:47:11 +00002019 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00002020 sqliteExprDelete(pList->a[i].pOn);
2021 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00002022 }
drh75897232000-05-29 14:26:00 +00002023 sqliteFree(pList);
2024}
2025
drh982cef72000-05-30 16:27:03 +00002026/*
drhc4a3c772001-04-04 11:48:57 +00002027** Begin a transaction
2028*/
drh1c928532002-01-31 15:54:21 +00002029void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002030 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002031
drh001bbcb2003-03-19 03:14:00 +00002032 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002033 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002034 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002035 if( db->flags & SQLITE_InTrans ){
drhda93d232003-03-31 02:12:46 +00002036 sqliteErrorMsg(pParse, "cannot start a transaction within a transaction");
drh6b8b8742002-08-18 20:28:06 +00002037 return;
2038 }
drhcabb0812002-09-14 13:47:32 +00002039 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002040 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002041 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00002042}
2043
2044/*
2045** Commit a transaction
2046*/
2047void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002048 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002049
drh001bbcb2003-03-19 03:14:00 +00002050 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002051 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002052 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002053 if( (db->flags & SQLITE_InTrans)==0 ){
drhda93d232003-03-31 02:12:46 +00002054 sqliteErrorMsg(pParse, "cannot commit - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002055 return;
2056 }
drh5e00f6c2001-09-13 13:46:56 +00002057 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002058 sqliteEndWriteOperation(pParse);
2059 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002060}
2061
2062/*
2063** Rollback a transaction
2064*/
2065void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002066 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002067 Vdbe *v;
2068
drh001bbcb2003-03-19 03:14:00 +00002069 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002070 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002071 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002072 if( (db->flags & SQLITE_InTrans)==0 ){
drhda93d232003-03-31 02:12:46 +00002073 sqliteErrorMsg(pParse, "cannot rollback - no transaction is active");
drh6b8b8742002-08-18 20:28:06 +00002074 return;
2075 }
drh5e00f6c2001-09-13 13:46:56 +00002076 v = sqliteGetVdbe(pParse);
2077 if( v ){
drh99fcd712001-10-13 01:06:47 +00002078 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002079 }
drh5e00f6c2001-09-13 13:46:56 +00002080 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002081 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002082}
drhf57b14a2001-09-14 18:54:08 +00002083
2084/*
drh001bbcb2003-03-19 03:14:00 +00002085** Generate VDBE code that will verify the schema cookie for all
2086** named database files.
2087*/
2088void sqliteCodeVerifySchema(Parse *pParse){
2089 int i;
2090 sqlite *db = pParse->db;
2091 Vdbe *v = sqliteGetVdbe(pParse);
2092 for(i=0; i<db->nDb; i++){
drh113088e2003-03-20 01:16:58 +00002093 if( i==1 || db->aDb[i].pBt==0 ) continue;
drh1c2d8412003-03-31 00:30:47 +00002094 sqliteVdbeAddOp(v, OP_VerifyCookie, i, db->aDb[i].schema_cookie);
drh001bbcb2003-03-19 03:14:00 +00002095 }
2096 pParse->schemaVerified = 1;
2097}
2098
2099/*
drh1c928532002-01-31 15:54:21 +00002100** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002101** might change the database.
2102**
2103** This routine starts a new transaction if we are not already within
2104** a transaction. If we are already within a transaction, then a checkpoint
2105** is set if the setCheckpoint parameter is true. A checkpoint should
2106** be set for operations that might fail (due to a constraint) part of
2107** the way through and which will need to undo some writes without having to
2108** rollback the whole transaction. For operations where all constraints
2109** can be checked before any changes are made to the database, it is never
2110** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002111**
2112** The tempOnly flag indicates that only temporary tables will be changed
2113** during this write operation. The primary database table is not
2114** write-locked. Only the temporary database file gets a write lock.
2115** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00002116*/
drhcabb0812002-09-14 13:47:32 +00002117void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00002118 Vdbe *v;
2119 v = sqliteGetVdbe(pParse);
2120 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00002121 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00002122 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh001bbcb2003-03-19 03:14:00 +00002123 sqliteVdbeAddOp(v, OP_Transaction, 1, 0);
drhcabb0812002-09-14 13:47:32 +00002124 if( !tempOnly ){
drha69d9162003-04-17 22:57:53 +00002125 int i;
2126 sqlite *db = pParse->db;
drh001bbcb2003-03-19 03:14:00 +00002127 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
drha69d9162003-04-17 22:57:53 +00002128 for(i=2; i<db->nDb; i++){
2129 if( db->aDb[i].pBt==0 ) continue;
2130 sqliteVdbeAddOp(v, OP_Transaction, i, 0);
2131 }
drh001bbcb2003-03-19 03:14:00 +00002132 sqliteCodeVerifySchema(pParse);
drhcabb0812002-09-14 13:47:32 +00002133 }
drhc977f7f2002-05-21 11:38:11 +00002134 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00002135 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00002136 sqliteVdbeAddOp(v, OP_Checkpoint, 1, 0);
drh663fc632002-02-02 18:49:19 +00002137 }
2138}
2139
2140/*
drh1c928532002-01-31 15:54:21 +00002141** Generate code that concludes an operation that may have changed
2142** the database. This is a companion function to BeginWriteOperation().
2143** If a transaction was started, then commit it. If a checkpoint was
2144** started then commit that.
2145*/
2146void sqliteEndWriteOperation(Parse *pParse){
2147 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002148 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002149 v = sqliteGetVdbe(pParse);
2150 if( v==0 ) return;
2151 if( pParse->db->flags & SQLITE_InTrans ){
2152 /* Do Nothing */
2153 }else{
2154 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2155 }
2156}