blob: b6a5074ec57305bd1ddbd4af0153a03e5a7a8bbd [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
20** creating expressions and ID lists
21** COPY
22** VACUUM
drhb19a2bc2001-09-16 00:13:26 +000023** BEGIN TRANSACTION
24** COMMIT
25** ROLLBACK
26** PRAGMA
drhbed86902000-06-02 13:27:59 +000027**
drhb19a2bc2001-09-16 00:13:26 +000028** $Id: build.c,v 1.36 2001/09/16 00:13:26 drh Exp $
drh75897232000-05-29 14:26:00 +000029*/
30#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000031#include <ctype.h>
drh75897232000-05-29 14:26:00 +000032
33/*
34** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000035** parsed and we want to execute the VDBE code to implement
36** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000037** constructed VDBE code to do the work of the SQL statement.
38** This routine just has to execute the VDBE code.
39**
40** Note that if an error occurred, it might be the case that
41** no VDBE code was generated.
42*/
43void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000044 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000045 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000046 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000047 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000048 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000049 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
50 &pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +000051 }else{
drh3fc190c2001-09-14 03:24:23 +000052 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000053 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000054 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000055 &pParse->zErrMsg, db->pBusyArg,
56 db->xBusyCallback);
drh75897232000-05-29 14:26:00 +000057 }
58 sqliteVdbeDelete(pParse->pVdbe);
59 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000060 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000061 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000062 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000063 }
64}
65
66/*
67** Construct a new expression node and return a pointer to it.
68*/
69Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
70 Expr *pNew;
71 pNew = sqliteMalloc( sizeof(Expr) );
72 if( pNew==0 ) return 0;
73 pNew->op = op;
74 pNew->pLeft = pLeft;
75 pNew->pRight = pRight;
76 if( pToken ){
77 pNew->token = *pToken;
78 }else{
79 pNew->token.z = "";
80 pNew->token.n = 0;
81 }
drhe1b6a5b2000-07-29 13:06:59 +000082 if( pLeft && pRight ){
83 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
84 }else{
85 pNew->span = pNew->token;
86 }
drh75897232000-05-29 14:26:00 +000087 return pNew;
88}
89
90/*
drhe1b6a5b2000-07-29 13:06:59 +000091** Set the Expr.token field of the given expression to span all
92** text between the two given tokens.
93*/
94void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drhdaffd0e2001-04-11 14:28:42 +000095 if( pExpr ){
96 pExpr->span.z = pLeft->z;
97 pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z;
98 }
drhe1b6a5b2000-07-29 13:06:59 +000099}
100
101/*
drh75897232000-05-29 14:26:00 +0000102** Construct a new expression node for a function with multiple
103** arguments.
104*/
105Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
106 Expr *pNew;
107 pNew = sqliteMalloc( sizeof(Expr) );
108 if( pNew==0 ) return 0;
109 pNew->op = TK_FUNCTION;
110 pNew->pList = pList;
111 if( pToken ){
112 pNew->token = *pToken;
113 }else{
114 pNew->token.z = "";
115 pNew->token.n = 0;
116 }
117 return pNew;
118}
119
120/*
121** Recursively delete an expression tree.
122*/
123void sqliteExprDelete(Expr *p){
124 if( p==0 ) return;
125 if( p->pLeft ) sqliteExprDelete(p->pLeft);
126 if( p->pRight ) sqliteExprDelete(p->pRight);
drh19a775c2000-06-05 18:54:46 +0000127 if( p->pList ) sqliteExprListDelete(p->pList);
128 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
drh75897232000-05-29 14:26:00 +0000129 sqliteFree(p);
130}
131
132/*
133** Locate the in-memory structure that describes the
134** format of a particular database table given the name
135** of that table. Return NULL if not found.
136*/
137Table *sqliteFindTable(sqlite *db, char *zName){
138 Table *pTable;
139 int h;
140
141 h = sqliteHashNoCase(zName, 0) % N_HASH;
142 for(pTable=db->apTblHash[h]; pTable; pTable=pTable->pHash){
143 if( sqliteStrICmp(pTable->zName, zName)==0 ) return pTable;
144 }
145 return 0;
146}
147
148/*
149** Locate the in-memory structure that describes the
drh1ccde152000-06-17 13:12:39 +0000150** format of a particular index given the name
151** of that index. Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000152*/
153Index *sqliteFindIndex(sqlite *db, char *zName){
154 Index *p;
155 int h;
156
157 h = sqliteHashNoCase(zName, 0) % N_HASH;
158 for(p=db->apIdxHash[h]; p; p=p->pHash){
159 if( sqliteStrICmp(p->zName, zName)==0 ) return p;
160 }
161 return 0;
162}
163
164/*
165** Remove the given index from the index hash table, and free
166** its memory structures.
167**
drhdaffd0e2001-04-11 14:28:42 +0000168** The index is removed from the database hash table if db!=NULL.
169** But it is not unlinked from the Table that is being indexed.
170** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000171*/
172static void sqliteDeleteIndex(sqlite *db, Index *pIndex){
173 int h;
drhdaffd0e2001-04-11 14:28:42 +0000174 if( pIndex->zName && db ){
drh75897232000-05-29 14:26:00 +0000175 h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH;
176 if( db->apIdxHash[h]==pIndex ){
177 db->apIdxHash[h] = pIndex->pHash;
178 }else{
179 Index *p;
180 for(p=db->apIdxHash[h]; p && p->pHash!=pIndex; p=p->pHash){}
181 if( p && p->pHash==pIndex ){
182 p->pHash = pIndex->pHash;
183 }
184 }
185 }
186 sqliteFree(pIndex);
187}
188
189/*
drh5e00f6c2001-09-13 13:46:56 +0000190** Unlink the given index from its table, then remove
191** the index from the index hash table, and free its memory
192** structures.
193*/
194static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
195 if( pIndex->pTable->pIndex==pIndex ){
196 pIndex->pTable->pIndex = pIndex->pNext;
197 }else{
198 Index *p;
199 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
200 if( p && p->pNext==pIndex ){
201 p->pNext = pIndex->pNext;
202 }
203 }
204 sqliteDeleteIndex(db, pIndex);
205}
206
207/*
drh75897232000-05-29 14:26:00 +0000208** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000209** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000210**
211** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000212** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000213** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000214**
215** Indices associated with the table are unlinked from the "db"
216** data structure if db!=NULL. If db==NULL, indices attached to
217** the table are deleted, but it is assumed they have already been
218** unlinked.
drh75897232000-05-29 14:26:00 +0000219*/
220void sqliteDeleteTable(sqlite *db, Table *pTable){
221 int i;
222 Index *pIndex, *pNext;
223 if( pTable==0 ) return;
224 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000225 sqliteFree(pTable->aCol[i].zName);
226 sqliteFree(pTable->aCol[i].zDflt);
drh75897232000-05-29 14:26:00 +0000227 }
228 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
229 pNext = pIndex->pNext;
230 sqliteDeleteIndex(db, pIndex);
231 }
drh6e142f52000-06-08 13:36:40 +0000232 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000233 sqliteFree(pTable->aCol);
drh75897232000-05-29 14:26:00 +0000234 sqliteFree(pTable);
235}
236
237/*
drh5edc3122001-09-13 21:53:09 +0000238** Unlink the given table from the hash tables and the delete the
239** table structure and all its indices.
240*/
241static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){
242 if( pTable->zName && db ){
243 int h = sqliteHashNoCase(pTable->zName, 0) % N_HASH;
244 if( db->apTblHash[h]==pTable ){
245 db->apTblHash[h] = pTable->pHash;
246 }else{
247 Table *p;
248 for(p=db->apTblHash[h]; p && p->pHash!=pTable; p=p->pHash){}
249 if( p && p->pHash==pTable ){
250 p->pHash = pTable->pHash;
251 }
252 }
253 }
254 sqliteDeleteTable(db, pTable);
255}
256
257/*
drh5e00f6c2001-09-13 13:46:56 +0000258** Check all Tables and Indexes in the internal hash table and commit
259** any additions or deletions to those hash tables.
260**
261** When executing CREATE TABLE and CREATE INDEX statements, the Table
262** and Index structures are created and added to the hash tables, but
263** the "isCommit" field is not set. This routine sets those fields.
264** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of
265** Table and Index structures is set but the structures are not unlinked
266** from the hash tables nor deallocated. This routine handles that
267** deallocation.
268**
269** See also: sqliteRollbackInternalChanges()
270*/
271void sqliteCommitInternalChanges(sqlite *db){
272 int i;
273 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drh50e5dad2001-09-15 00:57:28 +0000274 db->schema_cookie = db->next_cookie;
drh5e00f6c2001-09-13 13:46:56 +0000275 for(i=0; i<N_HASH; i++){
276 Table *pTable, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000277 for(pTable = db->apTblHash[i]; pTable; pTable=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000278 pNext = pTable->pHash;
279 if( pTable->isDelete ){
drh5edc3122001-09-13 21:53:09 +0000280 sqliteUnlinkAndDeleteTable(db, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000281 }else if( pTable->isCommit==0 ){
282 pTable->isCommit = 1;
283 }
284 }
285 }
286 for(i=0; i<N_HASH; i++){
287 Index *pIndex, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000288 for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000289 pNext = pIndex->pHash;
290 if( pIndex->isDelete ){
291 sqliteUnlinkAndDeleteIndex(db, pIndex);
292 }else if( pIndex->isCommit==0 ){
293 pIndex->isCommit = 1;
294 }
295 }
296 }
297 db->flags &= ~SQLITE_InternChanges;
298}
299
300/*
301** This routine runs when one or more CREATE TABLE, CREATE INDEX,
302** DROP TABLE, or DROP INDEX statements get rolled back. The
303** additions or deletions of Table and Index structures in the
304** internal hash tables are undone.
305**
306** See also: sqliteCommitInternalChanges()
307*/
308void sqliteRollbackInternalChanges(sqlite *db){
309 int i;
310 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drh50e5dad2001-09-15 00:57:28 +0000311 db->next_cookie = db->schema_cookie;
drh5e00f6c2001-09-13 13:46:56 +0000312 for(i=0; i<N_HASH; i++){
313 Table *pTable, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000314 for(pTable = db->apTblHash[i]; pTable; pTable=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000315 pNext = pTable->pHash;
316 if( !pTable->isCommit ){
drh5edc3122001-09-13 21:53:09 +0000317 sqliteUnlinkAndDeleteTable(db, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000318 }else if( pTable->isDelete ){
319 pTable->isDelete = 0;
320 }
321 }
322 }
323 for(i=0; i<N_HASH; i++){
324 Index *pIndex, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000325 for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000326 pNext = pIndex->pHash;
327 if( !pIndex->isCommit ){
328 sqliteUnlinkAndDeleteIndex(db, pIndex);
329 }else if( pIndex->isDelete ){
330 pIndex->isDelete = 0;
331 }
332 }
333 }
334 db->flags &= ~SQLITE_InternChanges;
335}
336
337/*
drh1ccde152000-06-17 13:12:39 +0000338** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000339**
340** Space to hold the name is obtained from sqliteMalloc() and must
341** be freed by the calling function.
342*/
drhcce7d172000-05-31 15:34:51 +0000343char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000344 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000345 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000346 return zName;
347}
348
349/*
350** Begin constructing a new table representation in memory. This is
351** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000352** to a CREATE TABLE statement. In particular, this routine is called
353** after seeing tokens "CREATE" and "TABLE" and the table name. The
354** pStart token is the CREATE and pName is the table name.
355**
356** The new table is constructed in files of the pParse structure. As
357** more of the CREATE TABLE statement is parsed, additional action
358** routines are called to build up more of the table.
drh75897232000-05-29 14:26:00 +0000359*/
360void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName){
361 Table *pTable;
362 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000363 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000364
365 pParse->sFirstToken = *pStart;
366 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000367 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000368 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000369 if( pTable!=0 ){
drh1d37e282000-05-30 03:12:21 +0000370 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
371 " already exists", 0, 0);
drh75897232000-05-29 14:26:00 +0000372 sqliteFree(zName);
373 pParse->nErr++;
374 return;
375 }
drhbe0072d2001-09-13 14:46:09 +0000376 if( sqliteFindIndex(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000377 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
378 zName, 0);
drh75897232000-05-29 14:26:00 +0000379 sqliteFree(zName);
380 pParse->nErr++;
381 return;
382 }
383 pTable = sqliteMalloc( sizeof(Table) );
drhdaffd0e2001-04-11 14:28:42 +0000384 if( pTable==0 ) return;
drh75897232000-05-29 14:26:00 +0000385 pTable->zName = zName;
386 pTable->pHash = 0;
387 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000388 pTable->aCol = 0;
drh75897232000-05-29 14:26:00 +0000389 pTable->pIndex = 0;
drhbe0072d2001-09-13 14:46:09 +0000390 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000391 pParse->pNewTable = pTable;
drhbe0072d2001-09-13 14:46:09 +0000392 if( !pParse->initFlag && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000393 Vdbe *v = sqliteGetVdbe(pParse);
394 if( v ){
395 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000396 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000397 }
398 }
drh75897232000-05-29 14:26:00 +0000399}
400
401/*
402** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000403**
404** The parser calls this routine once for each column declaration
405** in a CREATE TABLE statement. sqliteStartTable() gets called
406** first to get things going. Then this routine is called for each
407** column.
drh75897232000-05-29 14:26:00 +0000408*/
409void sqliteAddColumn(Parse *pParse, Token *pName){
410 Table *p;
411 char **pz;
412 if( (p = pParse->pNewTable)==0 ) return;
413 if( (p->nCol & 0x7)==0 ){
drh7020f652000-06-03 18:06:52 +0000414 p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
drhdaffd0e2001-04-11 14:28:42 +0000415 if( p->aCol==0 ){
416 p->nCol = 0;
417 return;
418 }
drh75897232000-05-29 14:26:00 +0000419 }
drh7020f652000-06-03 18:06:52 +0000420 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
421 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000422 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000423 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000424}
425
426/*
drh7020f652000-06-03 18:06:52 +0000427** The given token is the default value for the last column added to
428** the table currently under construction. If "minusFlag" is true, it
429** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000430**
431** This routine is called by the parser while in the middle of
432** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000433*/
434void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
435 Table *p;
436 int i;
437 char **pz;
438 if( (p = pParse->pNewTable)==0 ) return;
439 i = p->nCol-1;
440 pz = &p->aCol[i].zDflt;
441 if( minusFlag ){
442 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
443 }else{
444 sqliteSetNString(pz, pVal->z, pVal->n, 0);
445 }
446 sqliteDequote(*pz);
447}
448
449/*
drh50e5dad2001-09-15 00:57:28 +0000450** Come up with a new random value for the schema cookie. Make sure
451** the new value is different from the old.
452**
453** The schema cookie is used to determine when the schema for the
454** database changes. After each schema change, the cookie value
455** changes. When a process first reads the schema it records the
456** cookie. Thereafter, whenever it goes to access the database,
457** it checks the cookie to make sure the schema has not changed
458** since it was last read.
459**
460** This plan is not completely bullet-proof. It is possible for
461** the schema to change multiple times and for the cookie to be
462** set back to prior value. But schema changes are infrequent
463** and the probability of hitting the same cookie value is only
464** 1 chance in 2^32. So we're safe enough.
465*/
466static void changeCookie(sqlite *db){
467 if( db->next_cookie==db->schema_cookie ){
468 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
469 db->flags |= SQLITE_InternChanges;
470 }
471}
472
473/*
drh75897232000-05-29 14:26:00 +0000474** This routine is called to report the final ")" that terminates
475** a CREATE TABLE statement.
476**
477** The table structure is added to the internal hash tables.
478**
drh1ccde152000-06-17 13:12:39 +0000479** An entry for the table is made in the master table on disk,
480** unless initFlag==1. When initFlag==1, it means we are reading
481** the master table because we just connected to the database, so
drh75897232000-05-29 14:26:00 +0000482** the entry for this table already exists in the master table.
483** We do not want to create it again.
484*/
485void sqliteEndTable(Parse *pParse, Token *pEnd){
486 Table *p;
487 int h;
drhbe0072d2001-09-13 14:46:09 +0000488 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000489
drhdaffd0e2001-04-11 14:28:42 +0000490 if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000491 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000492 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000493
494 /* Add the table to the in-memory representation of the database
495 */
drhdaffd0e2001-04-11 14:28:42 +0000496 if( pParse->explain==0 ){
drh75897232000-05-29 14:26:00 +0000497 h = sqliteHashNoCase(p->zName, 0) % N_HASH;
drhbe0072d2001-09-13 14:46:09 +0000498 p->pHash = db->apTblHash[h];
499 db->apTblHash[h] = p;
drh75897232000-05-29 14:26:00 +0000500 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000501 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000502 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000503 }
504
drhd78eeee2001-09-13 16:18:53 +0000505 /* If the initFlag is 1 it means we are reading the SQL off the
506 ** "sqlite_master" table on the disk. So do not write to the disk
507 ** again. Extract the table number from the pParse->newTnum field.
508 */
509 if( pParse->initFlag ){
510 p->tnum = pParse->newTnum;
511 }
512
drh75897232000-05-29 14:26:00 +0000513 /* If not initializing, then create the table on disk.
514 */
515 if( !pParse->initFlag ){
516 static VdbeOp addTable[] = {
drh5edc3122001-09-13 21:53:09 +0000517 { OP_Open, 0, 2, MASTER_NAME},
drh5e00f6c2001-09-13 13:46:56 +0000518 { OP_NewRecno, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000519 { OP_String, 0, 0, "table" },
drh75897232000-05-29 14:26:00 +0000520 { OP_String, 0, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +0000521 { OP_CreateTable, 0, 0, 0},
drh305cea62000-05-29 17:44:25 +0000522 { OP_String, 0, 0, 0}, /* 5 */
drh5e00f6c2001-09-13 13:46:56 +0000523 { OP_String, 0, 0, 0}, /* 6 */
drhd78eeee2001-09-13 16:18:53 +0000524 { OP_MakeRecord, 5, 0, 0},
drh28037572000-08-02 13:47:41 +0000525 { OP_Put, 0, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000526 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000527 };
528 int n, base;
drhd8bc7082000-06-07 23:51:50 +0000529 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000530
drhd8bc7082000-06-07 23:51:50 +0000531 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000532 if( v==0 ) return;
533 n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1;
534 base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable);
drh75897232000-05-29 14:26:00 +0000535 sqliteVdbeChangeP3(v, base+3, p->zName, 0);
drh5e00f6c2001-09-13 13:46:56 +0000536 sqliteVdbeTableRootAddr(v, &p->tnum);
537 sqliteVdbeChangeP3(v, base+5, p->zName, 0);
538 sqliteVdbeChangeP3(v, base+6, pParse->sFirstToken.z, n);
drh50e5dad2001-09-15 00:57:28 +0000539 changeCookie(db);
540 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
drh28037572000-08-02 13:47:41 +0000541 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drh5edc3122001-09-13 21:53:09 +0000542 if( p->pIndex ){
543 /* If the table has a primary key, create an index in the database
544 ** for that key. */
545 Index *pIndex = p->pIndex;
546 assert( pIndex->pNext==0 );
547 assert( pIndex->tnum==0 );
548 sqliteVdbeAddOp(v, OP_CreateIndex, 0, 0, 0, 0),
549 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
550 }
drhbe0072d2001-09-13 14:46:09 +0000551 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000552 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
553 }
drh75897232000-05-29 14:26:00 +0000554 }
555}
556
557/*
558** Given a token, look up a table with that name. If not found, leave
559** an error for the parser to find and return NULL.
560*/
drhcce7d172000-05-31 15:34:51 +0000561Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +0000562 char *zName;
563 Table *pTab;
564 zName = sqliteTableNameFromToken(pTok);
565 if( zName==0 ) return 0;
566 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +0000567 sqliteFree(zName);
568 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +0000569 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
570 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +0000571 pParse->nErr++;
572 }
573 return pTab;
574}
575
576/*
577** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +0000578** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +0000579*/
580void sqliteDropTable(Parse *pParse, Token *pName){
581 Table *pTable;
drh75897232000-05-29 14:26:00 +0000582 Vdbe *v;
583 int base;
drh5edc3122001-09-13 21:53:09 +0000584 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000585
drhdaffd0e2001-04-11 14:28:42 +0000586 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000587 pTable = sqliteTableFromToken(pParse, pName);
588 if( pTable==0 ) return;
589 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +0000590 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
591 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +0000592 pParse->nErr++;
593 return;
594 }
595
drh1ccde152000-06-17 13:12:39 +0000596 /* Generate code to remove the table from the master table
597 ** on disk.
598 */
drhd8bc7082000-06-07 23:51:50 +0000599 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000600 if( v ){
601 static VdbeOp dropTable[] = {
drh5edc3122001-09-13 21:53:09 +0000602 { OP_Open, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000603 { OP_Rewind, 0, 0, 0},
604 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000605 { OP_Next, 0, ADDR(9), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000606 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000607 { OP_Column, 0, 3, 0},
drhd78eeee2001-09-13 16:18:53 +0000608 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000609 { OP_Delete, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000610 { OP_Goto, 0, ADDR(3), 0},
drh5edc3122001-09-13 21:53:09 +0000611 { OP_Destroy, 0, 0, 0}, /* 9 */
drh50e5dad2001-09-15 00:57:28 +0000612 { OP_SetCookie, 0, 0, 0}, /* 10 */
drh75897232000-05-29 14:26:00 +0000613 { OP_Close, 0, 0, 0},
614 };
615 Index *pIdx;
drh5edc3122001-09-13 21:53:09 +0000616 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000617 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000618 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000619 }
drh75897232000-05-29 14:26:00 +0000620 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh5edc3122001-09-13 21:53:09 +0000621 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
622 sqliteVdbeChangeP1(v, base+9, pTable->tnum);
drh50e5dad2001-09-15 00:57:28 +0000623 changeCookie(db);
624 sqliteVdbeChangeP1(v, base+10, db->next_cookie);
drh75897232000-05-29 14:26:00 +0000625 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drh5e00f6c2001-09-13 13:46:56 +0000626 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, 0, 0, 0);
627 }
drh5edc3122001-09-13 21:53:09 +0000628 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000629 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000630 }
631 }
632
drh5e00f6c2001-09-13 13:46:56 +0000633 /* Mark the in-memory Table structure as being deleted. The actually
634 ** deletion occurs inside of sqliteCommitInternalChanges().
drh75897232000-05-29 14:26:00 +0000635 **
636 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +0000637 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +0000638 */
639 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000640 pTable->isDelete = 1;
drh5edc3122001-09-13 21:53:09 +0000641 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000642 }
643}
644
645/*
646** Create a new index for an SQL table. pIndex is the name of the index
647** and pTable is the name of the table that is to be indexed. Both will
648** be NULL for a primary key. In that case, use pParse->pNewTable as the
649** table to be indexed.
650**
drh1ccde152000-06-17 13:12:39 +0000651** pList is a list of columns to be indexed. pList will be NULL if the
652** most recently added column of the table is labeled as the primary key.
drh75897232000-05-29 14:26:00 +0000653*/
654void sqliteCreateIndex(
655 Parse *pParse, /* All information about this parse */
656 Token *pName, /* Name of the index. May be NULL */
657 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +0000658 IdList *pList, /* A list of columns to be indexed */
drh75897232000-05-29 14:26:00 +0000659 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
660 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
661){
662 Table *pTab; /* Table to be indexed */
663 Index *pIndex; /* The index to be created */
664 char *zName = 0;
665 int i, j, h;
666 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +0000667 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000668
drhdaffd0e2001-04-11 14:28:42 +0000669 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
670
drh75897232000-05-29 14:26:00 +0000671 /*
672 ** Find the table that is to be indexed. Return early if not found.
673 */
674 if( pTable!=0 ){
675 pTab = sqliteTableFromToken(pParse, pTable);
676 }else{
677 pTab = pParse->pNewTable;
678 }
679 if( pTab==0 || pParse->nErr ) goto exit_create_index;
680 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +0000681 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
682 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +0000683 pParse->nErr++;
684 goto exit_create_index;
685 }
686
687 /*
688 ** Find the name of the index. Make sure there is not already another
689 ** index or table with the same name.
690 */
691 if( pName ){
692 zName = sqliteTableNameFromToken(pName);
693 }else{
694 zName = 0;
695 sqliteSetString(&zName, pTab->zName, "__primary_key", 0);
696 }
drhdaffd0e2001-04-11 14:28:42 +0000697 if( zName==0 ) goto exit_create_index;
drhbe0072d2001-09-13 14:46:09 +0000698 if( sqliteFindIndex(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000699 sqliteSetString(&pParse->zErrMsg, "index ", zName,
700 " already exists", 0);
drh75897232000-05-29 14:26:00 +0000701 pParse->nErr++;
702 goto exit_create_index;
703 }
drhbe0072d2001-09-13 14:46:09 +0000704 if( sqliteFindTable(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000705 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
706 zName, 0);
drh75897232000-05-29 14:26:00 +0000707 pParse->nErr++;
708 goto exit_create_index;
709 }
710
711 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +0000712 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +0000713 ** So create a fake list to simulate this.
714 */
715 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +0000716 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +0000717 nullId.n = strlen(nullId.z);
718 pList = sqliteIdListAppend(0, &nullId);
719 if( pList==0 ) goto exit_create_index;
720 }
721
722 /*
723 ** Allocate the index structure.
724 */
drhdcc581c2000-05-30 13:44:19 +0000725 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +0000726 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +0000727 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +0000728 pIndex->aiColumn = (int*)&pIndex[1];
729 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +0000730 strcpy(pIndex->zName, zName);
731 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +0000732 pIndex->nColumn = pList->nId;
drh75897232000-05-29 14:26:00 +0000733
drh1ccde152000-06-17 13:12:39 +0000734 /* Scan the names of the columns of the table to be indexed and
735 ** load the column indices into the Index structure. Report an error
736 ** if any column is not found.
drh75897232000-05-29 14:26:00 +0000737 */
738 for(i=0; i<pList->nId; i++){
739 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000740 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +0000741 }
742 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +0000743 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +0000744 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +0000745 pParse->nErr++;
746 sqliteFree(pIndex);
747 goto exit_create_index;
748 }
drh967e8b72000-06-21 13:59:10 +0000749 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +0000750 }
751
752 /* Link the new Index structure to its table and to the other
753 ** in-memory database structures.
754 */
755 if( pParse->explain==0 ){
756 h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH;
drhbe0072d2001-09-13 14:46:09 +0000757 pIndex->pHash = db->apIdxHash[h];
758 db->apIdxHash[h] = pIndex;
drh75897232000-05-29 14:26:00 +0000759 pIndex->pNext = pTab->pIndex;
760 pTab->pIndex = pIndex;
drh5e00f6c2001-09-13 13:46:56 +0000761 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000762 }
763
drhd78eeee2001-09-13 16:18:53 +0000764 /* If the initFlag is 1 it means we are reading the SQL off the
765 ** "sqlite_master" table on the disk. So do not write to the disk
766 ** again. Extract the table number from the pParse->newTnum field.
767 */
768 if( pParse->initFlag ){
769 pIndex->tnum = pParse->newTnum;
770 }
771
drh75897232000-05-29 14:26:00 +0000772 /* If the initFlag is 0 then create the index on disk. This
773 ** involves writing the index into the master table and filling in the
774 ** index with the current table contents.
775 **
776 ** The initFlag is 0 when the user first enters a CREATE INDEX
777 ** command. The initFlag is 1 when a database is opened and
778 ** CREATE INDEX statements are read out of the master table. In
779 ** the latter case the index already exists on disk, which is why
780 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +0000781 **
782 ** If pTable==0 it means this index is generated as a primary key
783 ** and those does not have a CREATE INDEX statement to add to the
784 ** master table. Also, since primary keys are created at the same
785 ** time as tables, the table will be empty so there is no need to
786 ** initialize the index. Hence, skip all the code generation if
787 ** pTable==0.
drh75897232000-05-29 14:26:00 +0000788 */
drh5edc3122001-09-13 21:53:09 +0000789 else if( pParse->initFlag==0 && pTable!=0 ){
drh75897232000-05-29 14:26:00 +0000790 static VdbeOp addTable[] = {
drh5edc3122001-09-13 21:53:09 +0000791 { OP_Open, 2, 2, MASTER_NAME},
drh5e00f6c2001-09-13 13:46:56 +0000792 { OP_NewRecno, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000793 { OP_String, 0, 0, "index"},
drh75897232000-05-29 14:26:00 +0000794 { OP_String, 0, 0, 0}, /* 3 */
drh5edc3122001-09-13 21:53:09 +0000795 { OP_CreateIndex, 1, 0, 0},
796 { OP_Dup, 0, 0, 0},
797 { OP_Open, 1, 0, 0}, /* 6 */
798 { OP_String, 0, 0, 0}, /* 7 */
799 { OP_String, 0, 0, 0}, /* 8 */
drh5e00f6c2001-09-13 13:46:56 +0000800 { OP_MakeRecord, 5, 0, 0},
drhbed86902000-06-02 13:27:59 +0000801 { OP_Put, 2, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000802 { OP_SetCookie, 0, 0, 0}, /* 11 */
drhbed86902000-06-02 13:27:59 +0000803 { OP_Close, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000804 };
805 int n;
806 Vdbe *v = pParse->pVdbe;
807 int lbl1, lbl2;
808 int i;
809
drhd8bc7082000-06-07 23:51:50 +0000810 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000811 if( v==0 ) goto exit_create_index;
drhbe0072d2001-09-13 14:46:09 +0000812 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000813 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000814 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000815 }
drh75897232000-05-29 14:26:00 +0000816 if( pStart && pEnd ){
817 int base;
818 n = (int)pEnd->z - (int)pStart->z + 1;
819 base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable);
drhb24fcbe2000-05-29 23:30:50 +0000820 sqliteVdbeChangeP3(v, base+3, pIndex->zName, 0);
drh5e00f6c2001-09-13 13:46:56 +0000821 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
drh5edc3122001-09-13 21:53:09 +0000822 sqliteVdbeChangeP3(v, base+6, pIndex->zName, 0);
823 sqliteVdbeChangeP3(v, base+7, pTab->zName, 0);
824 sqliteVdbeChangeP3(v, base+8, pStart->z, n);
drh50e5dad2001-09-15 00:57:28 +0000825 changeCookie(db);
826 sqliteVdbeChangeP1(v, base+11, db->next_cookie);
drh75897232000-05-29 14:26:00 +0000827 }
drh5edc3122001-09-13 21:53:09 +0000828 sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0);
drh75897232000-05-29 14:26:00 +0000829 lbl1 = sqliteVdbeMakeLabel(v);
830 lbl2 = sqliteVdbeMakeLabel(v);
drhd78eeee2001-09-13 16:18:53 +0000831 sqliteVdbeAddOp(v, OP_Rewind, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000832 sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1);
drhbe0072d2001-09-13 14:46:09 +0000833 sqliteVdbeAddOp(v, OP_Recno, 0, 0, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000834 for(i=0; i<pIndex->nColumn; i++){
drh5e00f6c2001-09-13 13:46:56 +0000835 sqliteVdbeAddOp(v, OP_Column, 0, pIndex->aiColumn[i], 0, 0);
drh75897232000-05-29 14:26:00 +0000836 }
drh5e00f6c2001-09-13 13:46:56 +0000837 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000838 sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0);
839 sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0);
840 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2);
drh75897232000-05-29 14:26:00 +0000841 sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000842 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drhbe0072d2001-09-13 14:46:09 +0000843 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000844 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
845 }
drh75897232000-05-29 14:26:00 +0000846 }
847
848 /* Reclaim memory on an EXPLAIN call.
849 */
850 if( pParse->explain ){
851 sqliteFree(pIndex);
852 }
853
854 /* Clean up before exiting */
855exit_create_index:
856 sqliteIdListDelete(pList);
857 sqliteFree(zName);
858 return;
859}
860
861/*
862** This routine will drop an existing named index.
863*/
864void sqliteDropIndex(Parse *pParse, Token *pName){
865 Index *pIndex;
866 char *zName;
867 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +0000868 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000869
drhdaffd0e2001-04-11 14:28:42 +0000870 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000871 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000872 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000873 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +0000874 sqliteFree(zName);
875 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +0000876 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
877 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +0000878 pParse->nErr++;
879 return;
880 }
881
882 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +0000883 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000884 if( v ){
885 static VdbeOp dropIndex[] = {
drh5edc3122001-09-13 21:53:09 +0000886 { OP_Open, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000887 { OP_Rewind, 0, 0, 0},
888 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000889 { OP_Next, 0, ADDR(8), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000890 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000891 { OP_Column, 0, 1, 0},
drhd78eeee2001-09-13 16:18:53 +0000892 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000893 { OP_Delete, 0, 0, 0},
drh5edc3122001-09-13 21:53:09 +0000894 { OP_Destroy, 0, 0, 0}, /* 8 */
drh50e5dad2001-09-15 00:57:28 +0000895 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000896 { OP_Close, 0, 0, 0},
897 };
898 int base;
899
drhbe0072d2001-09-13 14:46:09 +0000900 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000901 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000902 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000903 }
drh75897232000-05-29 14:26:00 +0000904 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh3fc190c2001-09-14 03:24:23 +0000905 sqliteVdbeChangeP3(v, base+2, pIndex->zName, 0);
drh5edc3122001-09-13 21:53:09 +0000906 sqliteVdbeChangeP1(v, base+8, pIndex->tnum);
drh50e5dad2001-09-15 00:57:28 +0000907 changeCookie(db);
908 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
drhbe0072d2001-09-13 14:46:09 +0000909 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000910 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
911 }
drh75897232000-05-29 14:26:00 +0000912 }
913
drh5e00f6c2001-09-13 13:46:56 +0000914 /* Mark the internal Index structure for deletion by the
915 ** sqliteCommitInternalChanges routine.
drh75897232000-05-29 14:26:00 +0000916 */
917 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000918 pIndex->isDelete = 1;
919 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000920 }
921}
922
923/*
924** Add a new element to the end of an expression list. If pList is
925** initially NULL, then create a new expression list.
926*/
927ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
928 int i;
929 if( pList==0 ){
930 pList = sqliteMalloc( sizeof(ExprList) );
drhdaffd0e2001-04-11 14:28:42 +0000931 if( pList==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000932 }
drh75897232000-05-29 14:26:00 +0000933 if( (pList->nExpr & 7)==0 ){
934 int n = pList->nExpr + 8;
935 pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
936 if( pList->a==0 ){
937 pList->nExpr = 0;
938 return pList;
939 }
940 }
941 i = pList->nExpr++;
942 pList->a[i].pExpr = pExpr;
943 pList->a[i].zName = 0;
944 if( pName ){
945 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000946 sqliteDequote(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +0000947 }
948 return pList;
949}
950
951/*
952** Delete an entire expression list.
953*/
954void sqliteExprListDelete(ExprList *pList){
955 int i;
956 if( pList==0 ) return;
957 for(i=0; i<pList->nExpr; i++){
958 sqliteExprDelete(pList->a[i].pExpr);
959 sqliteFree(pList->a[i].zName);
960 }
961 sqliteFree(pList->a);
962 sqliteFree(pList);
963}
964
965/*
966** Append a new element to the given IdList. Create a new IdList if
967** need be.
drhdaffd0e2001-04-11 14:28:42 +0000968**
969** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +0000970*/
971IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
972 if( pList==0 ){
973 pList = sqliteMalloc( sizeof(IdList) );
974 if( pList==0 ) return 0;
975 }
976 if( (pList->nId & 7)==0 ){
977 pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
978 if( pList->a==0 ){
979 pList->nId = 0;
drhdaffd0e2001-04-11 14:28:42 +0000980 sqliteIdListDelete(pList);
981 return 0;
drh75897232000-05-29 14:26:00 +0000982 }
983 }
984 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
985 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +0000986 char **pz = &pList->a[pList->nId].zName;
987 sqliteSetNString(pz, pToken->z, pToken->n, 0);
988 if( *pz==0 ){
989 sqliteIdListDelete(pList);
990 return 0;
991 }else{
992 sqliteDequote(*pz);
993 }
drh75897232000-05-29 14:26:00 +0000994 }
995 pList->nId++;
996 return pList;
997}
998
999/*
1000** Add an alias to the last identifier on the given identifier list.
1001*/
1002void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1003 if( pList && pList->nId>0 ){
1004 int i = pList->nId - 1;
1005 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001006 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001007 }
1008}
1009
1010/*
1011** Delete an entire IdList
1012*/
1013void sqliteIdListDelete(IdList *pList){
1014 int i;
1015 if( pList==0 ) return;
1016 for(i=0; i<pList->nId; i++){
1017 sqliteFree(pList->a[i].zName);
1018 sqliteFree(pList->a[i].zAlias);
drhdaffd0e2001-04-11 14:28:42 +00001019 if( pList->a[i].pSelect ){
1020 sqliteFree(pList->a[i].zName);
1021 sqliteSelectDelete(pList->a[i].pSelect);
1022 sqliteDeleteTable(0, pList->a[i].pTab);
1023 }
drh75897232000-05-29 14:26:00 +00001024 }
1025 sqliteFree(pList->a);
1026 sqliteFree(pList);
1027}
1028
drh982cef72000-05-30 16:27:03 +00001029
1030/*
1031** The COPY command is for compatibility with PostgreSQL and specificially
1032** for the ability to read the output of pg_dump. The format is as
1033** follows:
1034**
1035** COPY table FROM file [USING DELIMITERS string]
1036**
1037** "table" is an existing table name. We will read lines of code from
1038** file to fill this table with data. File might be "stdin". The optional
1039** delimiter string identifies the field separators. The default is a tab.
1040*/
1041void sqliteCopy(
1042 Parse *pParse, /* The parser context */
1043 Token *pTableName, /* The name of the table into which we will insert */
1044 Token *pFilename, /* The file from which to obtain information */
1045 Token *pDelimiter /* Use this as the field delimiter */
1046){
1047 Table *pTab;
1048 char *zTab;
1049 int i, j;
1050 Vdbe *v;
1051 int addr, end;
1052 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001053 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001054
1055 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001056 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001057 pTab = sqliteFindTable(db, zTab);
drh982cef72000-05-30 16:27:03 +00001058 sqliteFree(zTab);
1059 if( pTab==0 ){
1060 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1061 pTableName->z, pTableName->n, 0);
1062 pParse->nErr++;
1063 goto copy_cleanup;
1064 }
1065 if( pTab->readOnly ){
1066 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1067 " may not be modified", 0);
1068 pParse->nErr++;
1069 goto copy_cleanup;
1070 }
drhd8bc7082000-06-07 23:51:50 +00001071 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001072 if( v ){
drhbe0072d2001-09-13 14:46:09 +00001073 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001074 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001075 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00001076 }
drh982cef72000-05-30 16:27:03 +00001077 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0);
1078 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001079 sqliteVdbeDequoteP3(v, addr);
drh5e00f6c2001-09-13 13:46:56 +00001080 sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0);
drh982cef72000-05-30 16:27:03 +00001081 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh5e00f6c2001-09-13 13:46:56 +00001082 sqliteVdbeAddOp(v, OP_Open, i, pIdx->tnum, pIdx->zName, 0);
drh982cef72000-05-30 16:27:03 +00001083 }
1084 end = sqliteVdbeMakeLabel(v);
1085 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0);
1086 if( pDelimiter ){
1087 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1088 sqliteVdbeDequoteP3(v, addr);
1089 }else{
1090 sqliteVdbeChangeP3(v, addr, "\t", 1);
1091 }
drh5e00f6c2001-09-13 13:46:56 +00001092 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001093 if( pTab->pIndex ){
1094 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1095 }
1096 for(i=0; i<pTab->nCol; i++){
drh5e00f6c2001-09-13 13:46:56 +00001097 sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001098 }
1099 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
1100 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
1101 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1102 if( pIdx->pNext ){
1103 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1104 }
drh967e8b72000-06-21 13:59:10 +00001105 for(j=0; j<pIdx->nColumn; j++){
drh5e00f6c2001-09-13 13:46:56 +00001106 sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001107 }
drh5e00f6c2001-09-13 13:46:56 +00001108 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001109 sqliteVdbeAddOp(v, OP_PutIdx, i, 0, 0, 0);
1110 }
1111 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
1112 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end);
drhbe0072d2001-09-13 14:46:09 +00001113 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001114 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1115 }
drh982cef72000-05-30 16:27:03 +00001116 }
1117
1118copy_cleanup:
1119 return;
1120}
drhdce2cbe2000-05-31 02:27:49 +00001121
1122/*
1123** The non-standard VACUUM command is used to clean up the database,
1124** collapse free space, etc. It is modelled after the VACUUM command
1125** in PostgreSQL.
1126*/
1127void sqliteVacuum(Parse *pParse, Token *pTableName){
1128 char *zName;
1129 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001130 sqlite *db = pParse->db;
drhdce2cbe2000-05-31 02:27:49 +00001131
drhdaffd0e2001-04-11 14:28:42 +00001132 if( pParse->nErr || sqlite_malloc_failed ) return;
drhdce2cbe2000-05-31 02:27:49 +00001133 if( pTableName ){
1134 zName = sqliteTableNameFromToken(pTableName);
1135 }else{
1136 zName = 0;
1137 }
drhbe0072d2001-09-13 14:46:09 +00001138 if( zName && sqliteFindIndex(db, zName)==0
1139 && sqliteFindTable(db, zName)==0 ){
drhdce2cbe2000-05-31 02:27:49 +00001140 sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0);
1141 pParse->nErr++;
1142 goto vacuum_cleanup;
1143 }
drhd8bc7082000-06-07 23:51:50 +00001144 v = sqliteGetVdbe(pParse);
drhdce2cbe2000-05-31 02:27:49 +00001145 if( v==0 ) goto vacuum_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001146 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001147 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001148 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00001149 }
drhdce2cbe2000-05-31 02:27:49 +00001150 if( zName ){
1151 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0);
1152 }else{
1153 int h;
1154 Table *pTab;
1155 Index *pIdx;
1156 for(h=0; h<N_HASH; h++){
drhbe0072d2001-09-13 14:46:09 +00001157 for(pTab=db->apTblHash[h]; pTab; pTab=pTab->pHash){
drhdce2cbe2000-05-31 02:27:49 +00001158 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0);
1159 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1160 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0);
1161 }
1162 }
1163 }
1164 }
drhbe0072d2001-09-13 14:46:09 +00001165 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001166 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1167 }
drhdce2cbe2000-05-31 02:27:49 +00001168
1169vacuum_cleanup:
1170 sqliteFree(zName);
1171 return;
1172}
drhc4a3c772001-04-04 11:48:57 +00001173
1174/*
1175** Begin a transaction
1176*/
1177void sqliteBeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001178 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001179 Vdbe *v;
1180
drhc4a3c772001-04-04 11:48:57 +00001181 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001182 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001183 if( db->flags & SQLITE_InTrans ) return;
drh5e00f6c2001-09-13 13:46:56 +00001184 v = sqliteGetVdbe(pParse);
1185 if( v ){
1186 sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001187 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001188 }
drh5e00f6c2001-09-13 13:46:56 +00001189 db->flags |= SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001190}
1191
1192/*
1193** Commit a transaction
1194*/
1195void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001196 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001197 Vdbe *v;
1198
drhc4a3c772001-04-04 11:48:57 +00001199 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001200 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001201 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001202 v = sqliteGetVdbe(pParse);
1203 if( v ){
1204 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001205 }
drh5e00f6c2001-09-13 13:46:56 +00001206 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001207}
1208
1209/*
1210** Rollback a transaction
1211*/
1212void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001213 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001214 Vdbe *v;
1215
drhc4a3c772001-04-04 11:48:57 +00001216 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001217 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001218 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001219 v = sqliteGetVdbe(pParse);
1220 if( v ){
1221 sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001222 }
drh5e00f6c2001-09-13 13:46:56 +00001223 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001224}
drhf57b14a2001-09-14 18:54:08 +00001225
1226/*
1227** Interpret the given string as a boolean value.
1228*/
1229static int getBoolean(char *z){
1230 static char *azTrue[] = { "yes", "on", "true" };
1231 int i;
1232 if( z[0]==0 ) return 0;
1233 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1234 return atoi(z);
1235 }
1236 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1237 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1238 }
1239 return 0;
1240}
1241
1242/*
1243** Process a pragma statement.
1244**
1245** Pragmas are of this form:
1246**
1247** PRAGMA id = value
1248**
1249** The identifier might also be a string. The value is a string, and
1250** identifier, or a number. If minusFlag is true, then the value is
1251** a number that was preceded by a minus sign.
1252*/
1253void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1254 char *zLeft = 0;
1255 char *zRight = 0;
1256 sqlite *db = pParse->db;
1257
1258 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1259 sqliteDequote(zLeft);
1260 if( minusFlag ){
1261 zRight = 0;
1262 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1263 }else{
1264 zRight = sqliteStrNDup(pRight->z, pRight->n);
1265 sqliteDequote(zRight);
1266 }
1267
1268 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1269 int size = atoi(zRight);
1270 sqliteBtreeSetCacheSize(db->pBe, size);
1271 }else
1272
1273 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1274 if( getBoolean(zRight) ){
1275 db->flags |= SQLITE_VdbeTrace;
1276 }else{
1277 db->flags &= ~SQLITE_VdbeTrace;
1278 }
1279 }else
1280
1281#ifndef NDEBUG
1282 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
1283 extern void sqliteParserTrace(FILE*, char *);
1284 if( getBoolean(zRight) ){
1285 sqliteParserTrace(stdout, "parser: ");
1286 }else{
1287 sqliteParserTrace(0, 0);
1288 }
1289 }else
1290#endif
1291
1292 if( zLeft ) sqliteFree(zLeft);
1293 if( zRight ) sqliteFree(zRight);
1294}