blob: eb9ea05a0f19202329a8e8f5cb2c1276599cbcb5 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This file contains C code routines that are called by the parser
drhbed86902000-06-02 13:27:59 +000025** when syntax rules are reduced. The routines in this file handle
drh1ccde152000-06-17 13:12:39 +000026** the following kinds of syntax:
drh75897232000-05-29 14:26:00 +000027**
drhbed86902000-06-02 13:27:59 +000028** CREATE TABLE
29** DROP TABLE
30** CREATE INDEX
31** DROP INDEX
32** creating expressions and ID lists
33** COPY
34** VACUUM
35**
drh50e5dad2001-09-15 00:57:28 +000036** $Id: build.c,v 1.35 2001/09/15 00:57:28 drh Exp $
drh75897232000-05-29 14:26:00 +000037*/
38#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000039#include <ctype.h>
drh75897232000-05-29 14:26:00 +000040
41/*
42** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000043** parsed and we want to execute the VDBE code to implement
44** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000045** constructed VDBE code to do the work of the SQL statement.
46** This routine just has to execute the VDBE code.
47**
48** Note that if an error occurred, it might be the case that
49** no VDBE code was generated.
50*/
51void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000052 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000053 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000054 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000055 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000056 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000057 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
58 &pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +000059 }else{
drh3fc190c2001-09-14 03:24:23 +000060 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000061 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000062 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000063 &pParse->zErrMsg, db->pBusyArg,
64 db->xBusyCallback);
drh75897232000-05-29 14:26:00 +000065 }
66 sqliteVdbeDelete(pParse->pVdbe);
67 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000068 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000069 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000070 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000071 }
72}
73
74/*
75** Construct a new expression node and return a pointer to it.
76*/
77Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
78 Expr *pNew;
79 pNew = sqliteMalloc( sizeof(Expr) );
80 if( pNew==0 ) return 0;
81 pNew->op = op;
82 pNew->pLeft = pLeft;
83 pNew->pRight = pRight;
84 if( pToken ){
85 pNew->token = *pToken;
86 }else{
87 pNew->token.z = "";
88 pNew->token.n = 0;
89 }
drhe1b6a5b2000-07-29 13:06:59 +000090 if( pLeft && pRight ){
91 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
92 }else{
93 pNew->span = pNew->token;
94 }
drh75897232000-05-29 14:26:00 +000095 return pNew;
96}
97
98/*
drhe1b6a5b2000-07-29 13:06:59 +000099** Set the Expr.token field of the given expression to span all
100** text between the two given tokens.
101*/
102void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drhdaffd0e2001-04-11 14:28:42 +0000103 if( pExpr ){
104 pExpr->span.z = pLeft->z;
105 pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z;
106 }
drhe1b6a5b2000-07-29 13:06:59 +0000107}
108
109/*
drh75897232000-05-29 14:26:00 +0000110** Construct a new expression node for a function with multiple
111** arguments.
112*/
113Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
114 Expr *pNew;
115 pNew = sqliteMalloc( sizeof(Expr) );
116 if( pNew==0 ) return 0;
117 pNew->op = TK_FUNCTION;
118 pNew->pList = pList;
119 if( pToken ){
120 pNew->token = *pToken;
121 }else{
122 pNew->token.z = "";
123 pNew->token.n = 0;
124 }
125 return pNew;
126}
127
128/*
129** Recursively delete an expression tree.
130*/
131void sqliteExprDelete(Expr *p){
132 if( p==0 ) return;
133 if( p->pLeft ) sqliteExprDelete(p->pLeft);
134 if( p->pRight ) sqliteExprDelete(p->pRight);
drh19a775c2000-06-05 18:54:46 +0000135 if( p->pList ) sqliteExprListDelete(p->pList);
136 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
drh75897232000-05-29 14:26:00 +0000137 sqliteFree(p);
138}
139
140/*
141** Locate the in-memory structure that describes the
142** format of a particular database table given the name
143** of that table. Return NULL if not found.
144*/
145Table *sqliteFindTable(sqlite *db, char *zName){
146 Table *pTable;
147 int h;
148
149 h = sqliteHashNoCase(zName, 0) % N_HASH;
150 for(pTable=db->apTblHash[h]; pTable; pTable=pTable->pHash){
151 if( sqliteStrICmp(pTable->zName, zName)==0 ) return pTable;
152 }
153 return 0;
154}
155
156/*
157** Locate the in-memory structure that describes the
drh1ccde152000-06-17 13:12:39 +0000158** format of a particular index given the name
159** of that index. Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000160*/
161Index *sqliteFindIndex(sqlite *db, char *zName){
162 Index *p;
163 int h;
164
165 h = sqliteHashNoCase(zName, 0) % N_HASH;
166 for(p=db->apIdxHash[h]; p; p=p->pHash){
167 if( sqliteStrICmp(p->zName, zName)==0 ) return p;
168 }
169 return 0;
170}
171
172/*
173** Remove the given index from the index hash table, and free
174** its memory structures.
175**
drhdaffd0e2001-04-11 14:28:42 +0000176** The index is removed from the database hash table if db!=NULL.
177** But it is not unlinked from the Table that is being indexed.
178** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000179*/
180static void sqliteDeleteIndex(sqlite *db, Index *pIndex){
181 int h;
drhdaffd0e2001-04-11 14:28:42 +0000182 if( pIndex->zName && db ){
drh75897232000-05-29 14:26:00 +0000183 h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH;
184 if( db->apIdxHash[h]==pIndex ){
185 db->apIdxHash[h] = pIndex->pHash;
186 }else{
187 Index *p;
188 for(p=db->apIdxHash[h]; p && p->pHash!=pIndex; p=p->pHash){}
189 if( p && p->pHash==pIndex ){
190 p->pHash = pIndex->pHash;
191 }
192 }
193 }
194 sqliteFree(pIndex);
195}
196
197/*
drh5e00f6c2001-09-13 13:46:56 +0000198** Unlink the given index from its table, then remove
199** the index from the index hash table, and free its memory
200** structures.
201*/
202static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
203 if( pIndex->pTable->pIndex==pIndex ){
204 pIndex->pTable->pIndex = pIndex->pNext;
205 }else{
206 Index *p;
207 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
208 if( p && p->pNext==pIndex ){
209 p->pNext = pIndex->pNext;
210 }
211 }
212 sqliteDeleteIndex(db, pIndex);
213}
214
215/*
drh75897232000-05-29 14:26:00 +0000216** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000217** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000218**
219** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000220** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000221** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000222**
223** Indices associated with the table are unlinked from the "db"
224** data structure if db!=NULL. If db==NULL, indices attached to
225** the table are deleted, but it is assumed they have already been
226** unlinked.
drh75897232000-05-29 14:26:00 +0000227*/
228void sqliteDeleteTable(sqlite *db, Table *pTable){
229 int i;
230 Index *pIndex, *pNext;
231 if( pTable==0 ) return;
232 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000233 sqliteFree(pTable->aCol[i].zName);
234 sqliteFree(pTable->aCol[i].zDflt);
drh75897232000-05-29 14:26:00 +0000235 }
236 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
237 pNext = pIndex->pNext;
238 sqliteDeleteIndex(db, pIndex);
239 }
drh6e142f52000-06-08 13:36:40 +0000240 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000241 sqliteFree(pTable->aCol);
drh75897232000-05-29 14:26:00 +0000242 sqliteFree(pTable);
243}
244
245/*
drh5edc3122001-09-13 21:53:09 +0000246** Unlink the given table from the hash tables and the delete the
247** table structure and all its indices.
248*/
249static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){
250 if( pTable->zName && db ){
251 int h = sqliteHashNoCase(pTable->zName, 0) % N_HASH;
252 if( db->apTblHash[h]==pTable ){
253 db->apTblHash[h] = pTable->pHash;
254 }else{
255 Table *p;
256 for(p=db->apTblHash[h]; p && p->pHash!=pTable; p=p->pHash){}
257 if( p && p->pHash==pTable ){
258 p->pHash = pTable->pHash;
259 }
260 }
261 }
262 sqliteDeleteTable(db, pTable);
263}
264
265/*
drh5e00f6c2001-09-13 13:46:56 +0000266** Check all Tables and Indexes in the internal hash table and commit
267** any additions or deletions to those hash tables.
268**
269** When executing CREATE TABLE and CREATE INDEX statements, the Table
270** and Index structures are created and added to the hash tables, but
271** the "isCommit" field is not set. This routine sets those fields.
272** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of
273** Table and Index structures is set but the structures are not unlinked
274** from the hash tables nor deallocated. This routine handles that
275** deallocation.
276**
277** See also: sqliteRollbackInternalChanges()
278*/
279void sqliteCommitInternalChanges(sqlite *db){
280 int i;
281 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drh50e5dad2001-09-15 00:57:28 +0000282 db->schema_cookie = db->next_cookie;
drh5e00f6c2001-09-13 13:46:56 +0000283 for(i=0; i<N_HASH; i++){
284 Table *pTable, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000285 for(pTable = db->apTblHash[i]; pTable; pTable=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000286 pNext = pTable->pHash;
287 if( pTable->isDelete ){
drh5edc3122001-09-13 21:53:09 +0000288 sqliteUnlinkAndDeleteTable(db, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000289 }else if( pTable->isCommit==0 ){
290 pTable->isCommit = 1;
291 }
292 }
293 }
294 for(i=0; i<N_HASH; i++){
295 Index *pIndex, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000296 for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000297 pNext = pIndex->pHash;
298 if( pIndex->isDelete ){
299 sqliteUnlinkAndDeleteIndex(db, pIndex);
300 }else if( pIndex->isCommit==0 ){
301 pIndex->isCommit = 1;
302 }
303 }
304 }
305 db->flags &= ~SQLITE_InternChanges;
306}
307
308/*
309** This routine runs when one or more CREATE TABLE, CREATE INDEX,
310** DROP TABLE, or DROP INDEX statements get rolled back. The
311** additions or deletions of Table and Index structures in the
312** internal hash tables are undone.
313**
314** See also: sqliteCommitInternalChanges()
315*/
316void sqliteRollbackInternalChanges(sqlite *db){
317 int i;
318 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drh50e5dad2001-09-15 00:57:28 +0000319 db->next_cookie = db->schema_cookie;
drh5e00f6c2001-09-13 13:46:56 +0000320 for(i=0; i<N_HASH; i++){
321 Table *pTable, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000322 for(pTable = db->apTblHash[i]; pTable; pTable=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000323 pNext = pTable->pHash;
324 if( !pTable->isCommit ){
drh5edc3122001-09-13 21:53:09 +0000325 sqliteUnlinkAndDeleteTable(db, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000326 }else if( pTable->isDelete ){
327 pTable->isDelete = 0;
328 }
329 }
330 }
331 for(i=0; i<N_HASH; i++){
332 Index *pIndex, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000333 for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000334 pNext = pIndex->pHash;
335 if( !pIndex->isCommit ){
336 sqliteUnlinkAndDeleteIndex(db, pIndex);
337 }else if( pIndex->isDelete ){
338 pIndex->isDelete = 0;
339 }
340 }
341 }
342 db->flags &= ~SQLITE_InternChanges;
343}
344
345/*
drh1ccde152000-06-17 13:12:39 +0000346** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000347**
348** Space to hold the name is obtained from sqliteMalloc() and must
349** be freed by the calling function.
350*/
drhcce7d172000-05-31 15:34:51 +0000351char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000352 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000353 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000354 return zName;
355}
356
357/*
358** Begin constructing a new table representation in memory. This is
359** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000360** to a CREATE TABLE statement. In particular, this routine is called
361** after seeing tokens "CREATE" and "TABLE" and the table name. The
362** pStart token is the CREATE and pName is the table name.
363**
364** The new table is constructed in files of the pParse structure. As
365** more of the CREATE TABLE statement is parsed, additional action
366** routines are called to build up more of the table.
drh75897232000-05-29 14:26:00 +0000367*/
368void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName){
369 Table *pTable;
370 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000371 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000372
373 pParse->sFirstToken = *pStart;
374 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000375 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000376 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000377 if( pTable!=0 ){
drh1d37e282000-05-30 03:12:21 +0000378 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
379 " already exists", 0, 0);
drh75897232000-05-29 14:26:00 +0000380 sqliteFree(zName);
381 pParse->nErr++;
382 return;
383 }
drhbe0072d2001-09-13 14:46:09 +0000384 if( sqliteFindIndex(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000385 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
386 zName, 0);
drh75897232000-05-29 14:26:00 +0000387 sqliteFree(zName);
388 pParse->nErr++;
389 return;
390 }
391 pTable = sqliteMalloc( sizeof(Table) );
drhdaffd0e2001-04-11 14:28:42 +0000392 if( pTable==0 ) return;
drh75897232000-05-29 14:26:00 +0000393 pTable->zName = zName;
394 pTable->pHash = 0;
395 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000396 pTable->aCol = 0;
drh75897232000-05-29 14:26:00 +0000397 pTable->pIndex = 0;
drhbe0072d2001-09-13 14:46:09 +0000398 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000399 pParse->pNewTable = pTable;
drhbe0072d2001-09-13 14:46:09 +0000400 if( !pParse->initFlag && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000401 Vdbe *v = sqliteGetVdbe(pParse);
402 if( v ){
403 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000404 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000405 }
406 }
drh75897232000-05-29 14:26:00 +0000407}
408
409/*
410** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000411**
412** The parser calls this routine once for each column declaration
413** in a CREATE TABLE statement. sqliteStartTable() gets called
414** first to get things going. Then this routine is called for each
415** column.
drh75897232000-05-29 14:26:00 +0000416*/
417void sqliteAddColumn(Parse *pParse, Token *pName){
418 Table *p;
419 char **pz;
420 if( (p = pParse->pNewTable)==0 ) return;
421 if( (p->nCol & 0x7)==0 ){
drh7020f652000-06-03 18:06:52 +0000422 p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
drhdaffd0e2001-04-11 14:28:42 +0000423 if( p->aCol==0 ){
424 p->nCol = 0;
425 return;
426 }
drh75897232000-05-29 14:26:00 +0000427 }
drh7020f652000-06-03 18:06:52 +0000428 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
429 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000430 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000431 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000432}
433
434/*
drh7020f652000-06-03 18:06:52 +0000435** The given token is the default value for the last column added to
436** the table currently under construction. If "minusFlag" is true, it
437** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000438**
439** This routine is called by the parser while in the middle of
440** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000441*/
442void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
443 Table *p;
444 int i;
445 char **pz;
446 if( (p = pParse->pNewTable)==0 ) return;
447 i = p->nCol-1;
448 pz = &p->aCol[i].zDflt;
449 if( minusFlag ){
450 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
451 }else{
452 sqliteSetNString(pz, pVal->z, pVal->n, 0);
453 }
454 sqliteDequote(*pz);
455}
456
457/*
drh50e5dad2001-09-15 00:57:28 +0000458** Come up with a new random value for the schema cookie. Make sure
459** the new value is different from the old.
460**
461** The schema cookie is used to determine when the schema for the
462** database changes. After each schema change, the cookie value
463** changes. When a process first reads the schema it records the
464** cookie. Thereafter, whenever it goes to access the database,
465** it checks the cookie to make sure the schema has not changed
466** since it was last read.
467**
468** This plan is not completely bullet-proof. It is possible for
469** the schema to change multiple times and for the cookie to be
470** set back to prior value. But schema changes are infrequent
471** and the probability of hitting the same cookie value is only
472** 1 chance in 2^32. So we're safe enough.
473*/
474static void changeCookie(sqlite *db){
475 if( db->next_cookie==db->schema_cookie ){
476 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
477 db->flags |= SQLITE_InternChanges;
478 }
479}
480
481/*
drh75897232000-05-29 14:26:00 +0000482** This routine is called to report the final ")" that terminates
483** a CREATE TABLE statement.
484**
485** The table structure is added to the internal hash tables.
486**
drh1ccde152000-06-17 13:12:39 +0000487** An entry for the table is made in the master table on disk,
488** unless initFlag==1. When initFlag==1, it means we are reading
489** the master table because we just connected to the database, so
drh75897232000-05-29 14:26:00 +0000490** the entry for this table already exists in the master table.
491** We do not want to create it again.
492*/
493void sqliteEndTable(Parse *pParse, Token *pEnd){
494 Table *p;
495 int h;
drhbe0072d2001-09-13 14:46:09 +0000496 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000497
drhdaffd0e2001-04-11 14:28:42 +0000498 if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000499 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000500 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000501
502 /* Add the table to the in-memory representation of the database
503 */
drhdaffd0e2001-04-11 14:28:42 +0000504 if( pParse->explain==0 ){
drh75897232000-05-29 14:26:00 +0000505 h = sqliteHashNoCase(p->zName, 0) % N_HASH;
drhbe0072d2001-09-13 14:46:09 +0000506 p->pHash = db->apTblHash[h];
507 db->apTblHash[h] = p;
drh75897232000-05-29 14:26:00 +0000508 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000509 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000510 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000511 }
512
drhd78eeee2001-09-13 16:18:53 +0000513 /* If the initFlag is 1 it means we are reading the SQL off the
514 ** "sqlite_master" table on the disk. So do not write to the disk
515 ** again. Extract the table number from the pParse->newTnum field.
516 */
517 if( pParse->initFlag ){
518 p->tnum = pParse->newTnum;
519 }
520
drh75897232000-05-29 14:26:00 +0000521 /* If not initializing, then create the table on disk.
522 */
523 if( !pParse->initFlag ){
524 static VdbeOp addTable[] = {
drh5edc3122001-09-13 21:53:09 +0000525 { OP_Open, 0, 2, MASTER_NAME},
drh5e00f6c2001-09-13 13:46:56 +0000526 { OP_NewRecno, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000527 { OP_String, 0, 0, "table" },
drh75897232000-05-29 14:26:00 +0000528 { OP_String, 0, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +0000529 { OP_CreateTable, 0, 0, 0},
drh305cea62000-05-29 17:44:25 +0000530 { OP_String, 0, 0, 0}, /* 5 */
drh5e00f6c2001-09-13 13:46:56 +0000531 { OP_String, 0, 0, 0}, /* 6 */
drhd78eeee2001-09-13 16:18:53 +0000532 { OP_MakeRecord, 5, 0, 0},
drh28037572000-08-02 13:47:41 +0000533 { OP_Put, 0, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000534 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000535 };
536 int n, base;
drhd8bc7082000-06-07 23:51:50 +0000537 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000538
drhd8bc7082000-06-07 23:51:50 +0000539 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000540 if( v==0 ) return;
541 n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1;
542 base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable);
drh75897232000-05-29 14:26:00 +0000543 sqliteVdbeChangeP3(v, base+3, p->zName, 0);
drh5e00f6c2001-09-13 13:46:56 +0000544 sqliteVdbeTableRootAddr(v, &p->tnum);
545 sqliteVdbeChangeP3(v, base+5, p->zName, 0);
546 sqliteVdbeChangeP3(v, base+6, pParse->sFirstToken.z, n);
drh50e5dad2001-09-15 00:57:28 +0000547 changeCookie(db);
548 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
drh28037572000-08-02 13:47:41 +0000549 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drh5edc3122001-09-13 21:53:09 +0000550 if( p->pIndex ){
551 /* If the table has a primary key, create an index in the database
552 ** for that key. */
553 Index *pIndex = p->pIndex;
554 assert( pIndex->pNext==0 );
555 assert( pIndex->tnum==0 );
556 sqliteVdbeAddOp(v, OP_CreateIndex, 0, 0, 0, 0),
557 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
558 }
drhbe0072d2001-09-13 14:46:09 +0000559 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000560 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
561 }
drh75897232000-05-29 14:26:00 +0000562 }
563}
564
565/*
566** Given a token, look up a table with that name. If not found, leave
567** an error for the parser to find and return NULL.
568*/
drhcce7d172000-05-31 15:34:51 +0000569Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +0000570 char *zName;
571 Table *pTab;
572 zName = sqliteTableNameFromToken(pTok);
573 if( zName==0 ) return 0;
574 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +0000575 sqliteFree(zName);
576 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +0000577 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
578 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +0000579 pParse->nErr++;
580 }
581 return pTab;
582}
583
584/*
585** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +0000586** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +0000587*/
588void sqliteDropTable(Parse *pParse, Token *pName){
589 Table *pTable;
drh75897232000-05-29 14:26:00 +0000590 Vdbe *v;
591 int base;
drh5edc3122001-09-13 21:53:09 +0000592 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000593
drhdaffd0e2001-04-11 14:28:42 +0000594 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000595 pTable = sqliteTableFromToken(pParse, pName);
596 if( pTable==0 ) return;
597 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +0000598 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
599 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +0000600 pParse->nErr++;
601 return;
602 }
603
drh1ccde152000-06-17 13:12:39 +0000604 /* Generate code to remove the table from the master table
605 ** on disk.
606 */
drhd8bc7082000-06-07 23:51:50 +0000607 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000608 if( v ){
609 static VdbeOp dropTable[] = {
drh5edc3122001-09-13 21:53:09 +0000610 { OP_Open, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000611 { OP_Rewind, 0, 0, 0},
612 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000613 { OP_Next, 0, ADDR(9), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000614 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000615 { OP_Column, 0, 3, 0},
drhd78eeee2001-09-13 16:18:53 +0000616 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000617 { OP_Delete, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000618 { OP_Goto, 0, ADDR(3), 0},
drh5edc3122001-09-13 21:53:09 +0000619 { OP_Destroy, 0, 0, 0}, /* 9 */
drh50e5dad2001-09-15 00:57:28 +0000620 { OP_SetCookie, 0, 0, 0}, /* 10 */
drh75897232000-05-29 14:26:00 +0000621 { OP_Close, 0, 0, 0},
622 };
623 Index *pIdx;
drh5edc3122001-09-13 21:53:09 +0000624 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000625 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000626 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000627 }
drh75897232000-05-29 14:26:00 +0000628 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh5edc3122001-09-13 21:53:09 +0000629 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
630 sqliteVdbeChangeP1(v, base+9, pTable->tnum);
drh50e5dad2001-09-15 00:57:28 +0000631 changeCookie(db);
632 sqliteVdbeChangeP1(v, base+10, db->next_cookie);
drh75897232000-05-29 14:26:00 +0000633 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drh5e00f6c2001-09-13 13:46:56 +0000634 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, 0, 0, 0);
635 }
drh5edc3122001-09-13 21:53:09 +0000636 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000637 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000638 }
639 }
640
drh5e00f6c2001-09-13 13:46:56 +0000641 /* Mark the in-memory Table structure as being deleted. The actually
642 ** deletion occurs inside of sqliteCommitInternalChanges().
drh75897232000-05-29 14:26:00 +0000643 **
644 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +0000645 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +0000646 */
647 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000648 pTable->isDelete = 1;
drh5edc3122001-09-13 21:53:09 +0000649 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000650 }
651}
652
653/*
654** Create a new index for an SQL table. pIndex is the name of the index
655** and pTable is the name of the table that is to be indexed. Both will
656** be NULL for a primary key. In that case, use pParse->pNewTable as the
657** table to be indexed.
658**
drh1ccde152000-06-17 13:12:39 +0000659** pList is a list of columns to be indexed. pList will be NULL if the
660** most recently added column of the table is labeled as the primary key.
drh75897232000-05-29 14:26:00 +0000661*/
662void sqliteCreateIndex(
663 Parse *pParse, /* All information about this parse */
664 Token *pName, /* Name of the index. May be NULL */
665 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +0000666 IdList *pList, /* A list of columns to be indexed */
drh75897232000-05-29 14:26:00 +0000667 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
668 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
669){
670 Table *pTab; /* Table to be indexed */
671 Index *pIndex; /* The index to be created */
672 char *zName = 0;
673 int i, j, h;
674 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +0000675 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000676
drhdaffd0e2001-04-11 14:28:42 +0000677 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
678
drh75897232000-05-29 14:26:00 +0000679 /*
680 ** Find the table that is to be indexed. Return early if not found.
681 */
682 if( pTable!=0 ){
683 pTab = sqliteTableFromToken(pParse, pTable);
684 }else{
685 pTab = pParse->pNewTable;
686 }
687 if( pTab==0 || pParse->nErr ) goto exit_create_index;
688 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +0000689 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
690 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +0000691 pParse->nErr++;
692 goto exit_create_index;
693 }
694
695 /*
696 ** Find the name of the index. Make sure there is not already another
697 ** index or table with the same name.
698 */
699 if( pName ){
700 zName = sqliteTableNameFromToken(pName);
701 }else{
702 zName = 0;
703 sqliteSetString(&zName, pTab->zName, "__primary_key", 0);
704 }
drhdaffd0e2001-04-11 14:28:42 +0000705 if( zName==0 ) goto exit_create_index;
drhbe0072d2001-09-13 14:46:09 +0000706 if( sqliteFindIndex(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000707 sqliteSetString(&pParse->zErrMsg, "index ", zName,
708 " already exists", 0);
drh75897232000-05-29 14:26:00 +0000709 pParse->nErr++;
710 goto exit_create_index;
711 }
drhbe0072d2001-09-13 14:46:09 +0000712 if( sqliteFindTable(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000713 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
714 zName, 0);
drh75897232000-05-29 14:26:00 +0000715 pParse->nErr++;
716 goto exit_create_index;
717 }
718
719 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +0000720 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +0000721 ** So create a fake list to simulate this.
722 */
723 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +0000724 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +0000725 nullId.n = strlen(nullId.z);
726 pList = sqliteIdListAppend(0, &nullId);
727 if( pList==0 ) goto exit_create_index;
728 }
729
730 /*
731 ** Allocate the index structure.
732 */
drhdcc581c2000-05-30 13:44:19 +0000733 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +0000734 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +0000735 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +0000736 pIndex->aiColumn = (int*)&pIndex[1];
737 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +0000738 strcpy(pIndex->zName, zName);
739 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +0000740 pIndex->nColumn = pList->nId;
drh75897232000-05-29 14:26:00 +0000741
drh1ccde152000-06-17 13:12:39 +0000742 /* Scan the names of the columns of the table to be indexed and
743 ** load the column indices into the Index structure. Report an error
744 ** if any column is not found.
drh75897232000-05-29 14:26:00 +0000745 */
746 for(i=0; i<pList->nId; i++){
747 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000748 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +0000749 }
750 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +0000751 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +0000752 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +0000753 pParse->nErr++;
754 sqliteFree(pIndex);
755 goto exit_create_index;
756 }
drh967e8b72000-06-21 13:59:10 +0000757 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +0000758 }
759
760 /* Link the new Index structure to its table and to the other
761 ** in-memory database structures.
762 */
763 if( pParse->explain==0 ){
764 h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH;
drhbe0072d2001-09-13 14:46:09 +0000765 pIndex->pHash = db->apIdxHash[h];
766 db->apIdxHash[h] = pIndex;
drh75897232000-05-29 14:26:00 +0000767 pIndex->pNext = pTab->pIndex;
768 pTab->pIndex = pIndex;
drh5e00f6c2001-09-13 13:46:56 +0000769 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000770 }
771
drhd78eeee2001-09-13 16:18:53 +0000772 /* If the initFlag is 1 it means we are reading the SQL off the
773 ** "sqlite_master" table on the disk. So do not write to the disk
774 ** again. Extract the table number from the pParse->newTnum field.
775 */
776 if( pParse->initFlag ){
777 pIndex->tnum = pParse->newTnum;
778 }
779
drh75897232000-05-29 14:26:00 +0000780 /* If the initFlag is 0 then create the index on disk. This
781 ** involves writing the index into the master table and filling in the
782 ** index with the current table contents.
783 **
784 ** The initFlag is 0 when the user first enters a CREATE INDEX
785 ** command. The initFlag is 1 when a database is opened and
786 ** CREATE INDEX statements are read out of the master table. In
787 ** the latter case the index already exists on disk, which is why
788 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +0000789 **
790 ** If pTable==0 it means this index is generated as a primary key
791 ** and those does not have a CREATE INDEX statement to add to the
792 ** master table. Also, since primary keys are created at the same
793 ** time as tables, the table will be empty so there is no need to
794 ** initialize the index. Hence, skip all the code generation if
795 ** pTable==0.
drh75897232000-05-29 14:26:00 +0000796 */
drh5edc3122001-09-13 21:53:09 +0000797 else if( pParse->initFlag==0 && pTable!=0 ){
drh75897232000-05-29 14:26:00 +0000798 static VdbeOp addTable[] = {
drh5edc3122001-09-13 21:53:09 +0000799 { OP_Open, 2, 2, MASTER_NAME},
drh5e00f6c2001-09-13 13:46:56 +0000800 { OP_NewRecno, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000801 { OP_String, 0, 0, "index"},
drh75897232000-05-29 14:26:00 +0000802 { OP_String, 0, 0, 0}, /* 3 */
drh5edc3122001-09-13 21:53:09 +0000803 { OP_CreateIndex, 1, 0, 0},
804 { OP_Dup, 0, 0, 0},
805 { OP_Open, 1, 0, 0}, /* 6 */
806 { OP_String, 0, 0, 0}, /* 7 */
807 { OP_String, 0, 0, 0}, /* 8 */
drh5e00f6c2001-09-13 13:46:56 +0000808 { OP_MakeRecord, 5, 0, 0},
drhbed86902000-06-02 13:27:59 +0000809 { OP_Put, 2, 0, 0},
drh50e5dad2001-09-15 00:57:28 +0000810 { OP_SetCookie, 0, 0, 0}, /* 11 */
drhbed86902000-06-02 13:27:59 +0000811 { OP_Close, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000812 };
813 int n;
814 Vdbe *v = pParse->pVdbe;
815 int lbl1, lbl2;
816 int i;
817
drhd8bc7082000-06-07 23:51:50 +0000818 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000819 if( v==0 ) goto exit_create_index;
drhbe0072d2001-09-13 14:46:09 +0000820 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000821 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000822 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000823 }
drh75897232000-05-29 14:26:00 +0000824 if( pStart && pEnd ){
825 int base;
826 n = (int)pEnd->z - (int)pStart->z + 1;
827 base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable);
drhb24fcbe2000-05-29 23:30:50 +0000828 sqliteVdbeChangeP3(v, base+3, pIndex->zName, 0);
drh5e00f6c2001-09-13 13:46:56 +0000829 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
drh5edc3122001-09-13 21:53:09 +0000830 sqliteVdbeChangeP3(v, base+6, pIndex->zName, 0);
831 sqliteVdbeChangeP3(v, base+7, pTab->zName, 0);
832 sqliteVdbeChangeP3(v, base+8, pStart->z, n);
drh50e5dad2001-09-15 00:57:28 +0000833 changeCookie(db);
834 sqliteVdbeChangeP1(v, base+11, db->next_cookie);
drh75897232000-05-29 14:26:00 +0000835 }
drh5edc3122001-09-13 21:53:09 +0000836 sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0);
drh75897232000-05-29 14:26:00 +0000837 lbl1 = sqliteVdbeMakeLabel(v);
838 lbl2 = sqliteVdbeMakeLabel(v);
drhd78eeee2001-09-13 16:18:53 +0000839 sqliteVdbeAddOp(v, OP_Rewind, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000840 sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1);
drhbe0072d2001-09-13 14:46:09 +0000841 sqliteVdbeAddOp(v, OP_Recno, 0, 0, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000842 for(i=0; i<pIndex->nColumn; i++){
drh5e00f6c2001-09-13 13:46:56 +0000843 sqliteVdbeAddOp(v, OP_Column, 0, pIndex->aiColumn[i], 0, 0);
drh75897232000-05-29 14:26:00 +0000844 }
drh5e00f6c2001-09-13 13:46:56 +0000845 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000846 sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0);
847 sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0);
848 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2);
drh75897232000-05-29 14:26:00 +0000849 sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000850 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drhbe0072d2001-09-13 14:46:09 +0000851 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000852 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
853 }
drh75897232000-05-29 14:26:00 +0000854 }
855
856 /* Reclaim memory on an EXPLAIN call.
857 */
858 if( pParse->explain ){
859 sqliteFree(pIndex);
860 }
861
862 /* Clean up before exiting */
863exit_create_index:
864 sqliteIdListDelete(pList);
865 sqliteFree(zName);
866 return;
867}
868
869/*
870** This routine will drop an existing named index.
871*/
872void sqliteDropIndex(Parse *pParse, Token *pName){
873 Index *pIndex;
874 char *zName;
875 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +0000876 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000877
drhdaffd0e2001-04-11 14:28:42 +0000878 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000879 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000880 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000881 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +0000882 sqliteFree(zName);
883 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +0000884 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
885 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +0000886 pParse->nErr++;
887 return;
888 }
889
890 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +0000891 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000892 if( v ){
893 static VdbeOp dropIndex[] = {
drh5edc3122001-09-13 21:53:09 +0000894 { OP_Open, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000895 { OP_Rewind, 0, 0, 0},
896 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000897 { OP_Next, 0, ADDR(8), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000898 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000899 { OP_Column, 0, 1, 0},
drhd78eeee2001-09-13 16:18:53 +0000900 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000901 { OP_Delete, 0, 0, 0},
drh5edc3122001-09-13 21:53:09 +0000902 { OP_Destroy, 0, 0, 0}, /* 8 */
drh50e5dad2001-09-15 00:57:28 +0000903 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000904 { OP_Close, 0, 0, 0},
905 };
906 int base;
907
drhbe0072d2001-09-13 14:46:09 +0000908 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000909 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000910 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000911 }
drh75897232000-05-29 14:26:00 +0000912 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh3fc190c2001-09-14 03:24:23 +0000913 sqliteVdbeChangeP3(v, base+2, pIndex->zName, 0);
drh5edc3122001-09-13 21:53:09 +0000914 sqliteVdbeChangeP1(v, base+8, pIndex->tnum);
drh50e5dad2001-09-15 00:57:28 +0000915 changeCookie(db);
916 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
drhbe0072d2001-09-13 14:46:09 +0000917 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000918 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
919 }
drh75897232000-05-29 14:26:00 +0000920 }
921
drh5e00f6c2001-09-13 13:46:56 +0000922 /* Mark the internal Index structure for deletion by the
923 ** sqliteCommitInternalChanges routine.
drh75897232000-05-29 14:26:00 +0000924 */
925 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000926 pIndex->isDelete = 1;
927 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000928 }
929}
930
931/*
932** Add a new element to the end of an expression list. If pList is
933** initially NULL, then create a new expression list.
934*/
935ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
936 int i;
937 if( pList==0 ){
938 pList = sqliteMalloc( sizeof(ExprList) );
drhdaffd0e2001-04-11 14:28:42 +0000939 if( pList==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000940 }
drh75897232000-05-29 14:26:00 +0000941 if( (pList->nExpr & 7)==0 ){
942 int n = pList->nExpr + 8;
943 pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
944 if( pList->a==0 ){
945 pList->nExpr = 0;
946 return pList;
947 }
948 }
949 i = pList->nExpr++;
950 pList->a[i].pExpr = pExpr;
951 pList->a[i].zName = 0;
952 if( pName ){
953 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000954 sqliteDequote(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +0000955 }
956 return pList;
957}
958
959/*
960** Delete an entire expression list.
961*/
962void sqliteExprListDelete(ExprList *pList){
963 int i;
964 if( pList==0 ) return;
965 for(i=0; i<pList->nExpr; i++){
966 sqliteExprDelete(pList->a[i].pExpr);
967 sqliteFree(pList->a[i].zName);
968 }
969 sqliteFree(pList->a);
970 sqliteFree(pList);
971}
972
973/*
974** Append a new element to the given IdList. Create a new IdList if
975** need be.
drhdaffd0e2001-04-11 14:28:42 +0000976**
977** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +0000978*/
979IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
980 if( pList==0 ){
981 pList = sqliteMalloc( sizeof(IdList) );
982 if( pList==0 ) return 0;
983 }
984 if( (pList->nId & 7)==0 ){
985 pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
986 if( pList->a==0 ){
987 pList->nId = 0;
drhdaffd0e2001-04-11 14:28:42 +0000988 sqliteIdListDelete(pList);
989 return 0;
drh75897232000-05-29 14:26:00 +0000990 }
991 }
992 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
993 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +0000994 char **pz = &pList->a[pList->nId].zName;
995 sqliteSetNString(pz, pToken->z, pToken->n, 0);
996 if( *pz==0 ){
997 sqliteIdListDelete(pList);
998 return 0;
999 }else{
1000 sqliteDequote(*pz);
1001 }
drh75897232000-05-29 14:26:00 +00001002 }
1003 pList->nId++;
1004 return pList;
1005}
1006
1007/*
1008** Add an alias to the last identifier on the given identifier list.
1009*/
1010void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1011 if( pList && pList->nId>0 ){
1012 int i = pList->nId - 1;
1013 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001014 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001015 }
1016}
1017
1018/*
1019** Delete an entire IdList
1020*/
1021void sqliteIdListDelete(IdList *pList){
1022 int i;
1023 if( pList==0 ) return;
1024 for(i=0; i<pList->nId; i++){
1025 sqliteFree(pList->a[i].zName);
1026 sqliteFree(pList->a[i].zAlias);
drhdaffd0e2001-04-11 14:28:42 +00001027 if( pList->a[i].pSelect ){
1028 sqliteFree(pList->a[i].zName);
1029 sqliteSelectDelete(pList->a[i].pSelect);
1030 sqliteDeleteTable(0, pList->a[i].pTab);
1031 }
drh75897232000-05-29 14:26:00 +00001032 }
1033 sqliteFree(pList->a);
1034 sqliteFree(pList);
1035}
1036
drh982cef72000-05-30 16:27:03 +00001037
1038/*
1039** The COPY command is for compatibility with PostgreSQL and specificially
1040** for the ability to read the output of pg_dump. The format is as
1041** follows:
1042**
1043** COPY table FROM file [USING DELIMITERS string]
1044**
1045** "table" is an existing table name. We will read lines of code from
1046** file to fill this table with data. File might be "stdin". The optional
1047** delimiter string identifies the field separators. The default is a tab.
1048*/
1049void sqliteCopy(
1050 Parse *pParse, /* The parser context */
1051 Token *pTableName, /* The name of the table into which we will insert */
1052 Token *pFilename, /* The file from which to obtain information */
1053 Token *pDelimiter /* Use this as the field delimiter */
1054){
1055 Table *pTab;
1056 char *zTab;
1057 int i, j;
1058 Vdbe *v;
1059 int addr, end;
1060 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001061 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001062
1063 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001064 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001065 pTab = sqliteFindTable(db, zTab);
drh982cef72000-05-30 16:27:03 +00001066 sqliteFree(zTab);
1067 if( pTab==0 ){
1068 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1069 pTableName->z, pTableName->n, 0);
1070 pParse->nErr++;
1071 goto copy_cleanup;
1072 }
1073 if( pTab->readOnly ){
1074 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1075 " may not be modified", 0);
1076 pParse->nErr++;
1077 goto copy_cleanup;
1078 }
drhd8bc7082000-06-07 23:51:50 +00001079 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001080 if( v ){
drhbe0072d2001-09-13 14:46:09 +00001081 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001082 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001083 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00001084 }
drh982cef72000-05-30 16:27:03 +00001085 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0);
1086 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001087 sqliteVdbeDequoteP3(v, addr);
drh5e00f6c2001-09-13 13:46:56 +00001088 sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0);
drh982cef72000-05-30 16:27:03 +00001089 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh5e00f6c2001-09-13 13:46:56 +00001090 sqliteVdbeAddOp(v, OP_Open, i, pIdx->tnum, pIdx->zName, 0);
drh982cef72000-05-30 16:27:03 +00001091 }
1092 end = sqliteVdbeMakeLabel(v);
1093 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0);
1094 if( pDelimiter ){
1095 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1096 sqliteVdbeDequoteP3(v, addr);
1097 }else{
1098 sqliteVdbeChangeP3(v, addr, "\t", 1);
1099 }
drh5e00f6c2001-09-13 13:46:56 +00001100 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001101 if( pTab->pIndex ){
1102 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1103 }
1104 for(i=0; i<pTab->nCol; i++){
drh5e00f6c2001-09-13 13:46:56 +00001105 sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001106 }
1107 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
1108 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
1109 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1110 if( pIdx->pNext ){
1111 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1112 }
drh967e8b72000-06-21 13:59:10 +00001113 for(j=0; j<pIdx->nColumn; j++){
drh5e00f6c2001-09-13 13:46:56 +00001114 sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001115 }
drh5e00f6c2001-09-13 13:46:56 +00001116 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001117 sqliteVdbeAddOp(v, OP_PutIdx, i, 0, 0, 0);
1118 }
1119 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
1120 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end);
drhbe0072d2001-09-13 14:46:09 +00001121 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001122 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1123 }
drh982cef72000-05-30 16:27:03 +00001124 }
1125
1126copy_cleanup:
1127 return;
1128}
drhdce2cbe2000-05-31 02:27:49 +00001129
1130/*
1131** The non-standard VACUUM command is used to clean up the database,
1132** collapse free space, etc. It is modelled after the VACUUM command
1133** in PostgreSQL.
1134*/
1135void sqliteVacuum(Parse *pParse, Token *pTableName){
1136 char *zName;
1137 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001138 sqlite *db = pParse->db;
drhdce2cbe2000-05-31 02:27:49 +00001139
drhdaffd0e2001-04-11 14:28:42 +00001140 if( pParse->nErr || sqlite_malloc_failed ) return;
drhdce2cbe2000-05-31 02:27:49 +00001141 if( pTableName ){
1142 zName = sqliteTableNameFromToken(pTableName);
1143 }else{
1144 zName = 0;
1145 }
drhbe0072d2001-09-13 14:46:09 +00001146 if( zName && sqliteFindIndex(db, zName)==0
1147 && sqliteFindTable(db, zName)==0 ){
drhdce2cbe2000-05-31 02:27:49 +00001148 sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0);
1149 pParse->nErr++;
1150 goto vacuum_cleanup;
1151 }
drhd8bc7082000-06-07 23:51:50 +00001152 v = sqliteGetVdbe(pParse);
drhdce2cbe2000-05-31 02:27:49 +00001153 if( v==0 ) goto vacuum_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001154 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001155 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001156 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00001157 }
drhdce2cbe2000-05-31 02:27:49 +00001158 if( zName ){
1159 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0);
1160 }else{
1161 int h;
1162 Table *pTab;
1163 Index *pIdx;
1164 for(h=0; h<N_HASH; h++){
drhbe0072d2001-09-13 14:46:09 +00001165 for(pTab=db->apTblHash[h]; pTab; pTab=pTab->pHash){
drhdce2cbe2000-05-31 02:27:49 +00001166 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0);
1167 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1168 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0);
1169 }
1170 }
1171 }
1172 }
drhbe0072d2001-09-13 14:46:09 +00001173 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001174 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1175 }
drhdce2cbe2000-05-31 02:27:49 +00001176
1177vacuum_cleanup:
1178 sqliteFree(zName);
1179 return;
1180}
drhc4a3c772001-04-04 11:48:57 +00001181
1182/*
1183** Begin a transaction
1184*/
1185void sqliteBeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001186 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001187 Vdbe *v;
1188
drhc4a3c772001-04-04 11:48:57 +00001189 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001190 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001191 if( db->flags & SQLITE_InTrans ) return;
drh5e00f6c2001-09-13 13:46:56 +00001192 v = sqliteGetVdbe(pParse);
1193 if( v ){
1194 sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001195 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001196 }
drh5e00f6c2001-09-13 13:46:56 +00001197 db->flags |= SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001198}
1199
1200/*
1201** Commit a transaction
1202*/
1203void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001204 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001205 Vdbe *v;
1206
drhc4a3c772001-04-04 11:48:57 +00001207 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001208 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001209 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001210 v = sqliteGetVdbe(pParse);
1211 if( v ){
1212 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001213 }
drh5e00f6c2001-09-13 13:46:56 +00001214 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001215}
1216
1217/*
1218** Rollback a transaction
1219*/
1220void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001221 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001222 Vdbe *v;
1223
drhc4a3c772001-04-04 11:48:57 +00001224 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001225 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001226 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001227 v = sqliteGetVdbe(pParse);
1228 if( v ){
1229 sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001230 }
drh5e00f6c2001-09-13 13:46:56 +00001231 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001232}
drhf57b14a2001-09-14 18:54:08 +00001233
1234/*
1235** Interpret the given string as a boolean value.
1236*/
1237static int getBoolean(char *z){
1238 static char *azTrue[] = { "yes", "on", "true" };
1239 int i;
1240 if( z[0]==0 ) return 0;
1241 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1242 return atoi(z);
1243 }
1244 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1245 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1246 }
1247 return 0;
1248}
1249
1250/*
1251** Process a pragma statement.
1252**
1253** Pragmas are of this form:
1254**
1255** PRAGMA id = value
1256**
1257** The identifier might also be a string. The value is a string, and
1258** identifier, or a number. If minusFlag is true, then the value is
1259** a number that was preceded by a minus sign.
1260*/
1261void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1262 char *zLeft = 0;
1263 char *zRight = 0;
1264 sqlite *db = pParse->db;
1265
1266 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1267 sqliteDequote(zLeft);
1268 if( minusFlag ){
1269 zRight = 0;
1270 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1271 }else{
1272 zRight = sqliteStrNDup(pRight->z, pRight->n);
1273 sqliteDequote(zRight);
1274 }
1275
1276 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1277 int size = atoi(zRight);
1278 sqliteBtreeSetCacheSize(db->pBe, size);
1279 }else
1280
1281 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1282 if( getBoolean(zRight) ){
1283 db->flags |= SQLITE_VdbeTrace;
1284 }else{
1285 db->flags &= ~SQLITE_VdbeTrace;
1286 }
1287 }else
1288
1289#ifndef NDEBUG
1290 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
1291 extern void sqliteParserTrace(FILE*, char *);
1292 if( getBoolean(zRight) ){
1293 sqliteParserTrace(stdout, "parser: ");
1294 }else{
1295 sqliteParserTrace(0, 0);
1296 }
1297 }else
1298#endif
1299
1300 if( zLeft ) sqliteFree(zLeft);
1301 if( zRight ) sqliteFree(zRight);
1302}