blob: 436e0d2620edb6e409b5b0954e1ff6c240d065ae [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**
drh5edc3122001-09-13 21:53:09 +000036** $Id: build.c,v 1.32 2001/09/13 21:53:10 drh Exp $
drh75897232000-05-29 14:26:00 +000037*/
38#include "sqliteInt.h"
39
40/*
41** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000042** parsed and we want to execute the VDBE code to implement
43** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000044** constructed VDBE code to do the work of the SQL statement.
45** This routine just has to execute the VDBE code.
46**
47** Note that if an error occurred, it might be the case that
48** no VDBE code was generated.
49*/
50void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000051 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000052 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000053 if( sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +000054 if( pParse->pVdbe ){
55 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000056 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
57 &pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +000058 }else{
drhbe0072d2001-09-13 14:46:09 +000059 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stderr : 0;
drh75897232000-05-29 14:26:00 +000060 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000061 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000062 &pParse->zErrMsg, db->pBusyArg,
63 db->xBusyCallback);
drh75897232000-05-29 14:26:00 +000064 }
65 sqliteVdbeDelete(pParse->pVdbe);
66 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000067 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000068 pParse->rc = rc;
drh75897232000-05-29 14:26:00 +000069 }
70}
71
72/*
73** Construct a new expression node and return a pointer to it.
74*/
75Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
76 Expr *pNew;
77 pNew = sqliteMalloc( sizeof(Expr) );
78 if( pNew==0 ) return 0;
79 pNew->op = op;
80 pNew->pLeft = pLeft;
81 pNew->pRight = pRight;
82 if( pToken ){
83 pNew->token = *pToken;
84 }else{
85 pNew->token.z = "";
86 pNew->token.n = 0;
87 }
drhe1b6a5b2000-07-29 13:06:59 +000088 if( pLeft && pRight ){
89 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
90 }else{
91 pNew->span = pNew->token;
92 }
drh75897232000-05-29 14:26:00 +000093 return pNew;
94}
95
96/*
drhe1b6a5b2000-07-29 13:06:59 +000097** Set the Expr.token field of the given expression to span all
98** text between the two given tokens.
99*/
100void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drhdaffd0e2001-04-11 14:28:42 +0000101 if( pExpr ){
102 pExpr->span.z = pLeft->z;
103 pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z;
104 }
drhe1b6a5b2000-07-29 13:06:59 +0000105}
106
107/*
drh75897232000-05-29 14:26:00 +0000108** Construct a new expression node for a function with multiple
109** arguments.
110*/
111Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
112 Expr *pNew;
113 pNew = sqliteMalloc( sizeof(Expr) );
114 if( pNew==0 ) return 0;
115 pNew->op = TK_FUNCTION;
116 pNew->pList = pList;
117 if( pToken ){
118 pNew->token = *pToken;
119 }else{
120 pNew->token.z = "";
121 pNew->token.n = 0;
122 }
123 return pNew;
124}
125
126/*
127** Recursively delete an expression tree.
128*/
129void sqliteExprDelete(Expr *p){
130 if( p==0 ) return;
131 if( p->pLeft ) sqliteExprDelete(p->pLeft);
132 if( p->pRight ) sqliteExprDelete(p->pRight);
drh19a775c2000-06-05 18:54:46 +0000133 if( p->pList ) sqliteExprListDelete(p->pList);
134 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
drh75897232000-05-29 14:26:00 +0000135 sqliteFree(p);
136}
137
138/*
139** Locate the in-memory structure that describes the
140** format of a particular database table given the name
141** of that table. Return NULL if not found.
142*/
143Table *sqliteFindTable(sqlite *db, char *zName){
144 Table *pTable;
145 int h;
146
147 h = sqliteHashNoCase(zName, 0) % N_HASH;
148 for(pTable=db->apTblHash[h]; pTable; pTable=pTable->pHash){
149 if( sqliteStrICmp(pTable->zName, zName)==0 ) return pTable;
150 }
151 return 0;
152}
153
154/*
155** Locate the in-memory structure that describes the
drh1ccde152000-06-17 13:12:39 +0000156** format of a particular index given the name
157** of that index. Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000158*/
159Index *sqliteFindIndex(sqlite *db, char *zName){
160 Index *p;
161 int h;
162
163 h = sqliteHashNoCase(zName, 0) % N_HASH;
164 for(p=db->apIdxHash[h]; p; p=p->pHash){
165 if( sqliteStrICmp(p->zName, zName)==0 ) return p;
166 }
167 return 0;
168}
169
170/*
171** Remove the given index from the index hash table, and free
172** its memory structures.
173**
drhdaffd0e2001-04-11 14:28:42 +0000174** The index is removed from the database hash table if db!=NULL.
175** But it is not unlinked from the Table that is being indexed.
176** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000177*/
178static void sqliteDeleteIndex(sqlite *db, Index *pIndex){
179 int h;
drhdaffd0e2001-04-11 14:28:42 +0000180 if( pIndex->zName && db ){
drh75897232000-05-29 14:26:00 +0000181 h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH;
182 if( db->apIdxHash[h]==pIndex ){
183 db->apIdxHash[h] = pIndex->pHash;
184 }else{
185 Index *p;
186 for(p=db->apIdxHash[h]; p && p->pHash!=pIndex; p=p->pHash){}
187 if( p && p->pHash==pIndex ){
188 p->pHash = pIndex->pHash;
189 }
190 }
191 }
192 sqliteFree(pIndex);
193}
194
195/*
drh5e00f6c2001-09-13 13:46:56 +0000196** Unlink the given index from its table, then remove
197** the index from the index hash table, and free its memory
198** structures.
199*/
200static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
201 if( pIndex->pTable->pIndex==pIndex ){
202 pIndex->pTable->pIndex = pIndex->pNext;
203 }else{
204 Index *p;
205 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
206 if( p && p->pNext==pIndex ){
207 p->pNext = pIndex->pNext;
208 }
209 }
210 sqliteDeleteIndex(db, pIndex);
211}
212
213/*
drh75897232000-05-29 14:26:00 +0000214** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000215** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000216**
217** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000218** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000219** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000220**
221** Indices associated with the table are unlinked from the "db"
222** data structure if db!=NULL. If db==NULL, indices attached to
223** the table are deleted, but it is assumed they have already been
224** unlinked.
drh75897232000-05-29 14:26:00 +0000225*/
226void sqliteDeleteTable(sqlite *db, Table *pTable){
227 int i;
228 Index *pIndex, *pNext;
229 if( pTable==0 ) return;
230 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000231 sqliteFree(pTable->aCol[i].zName);
232 sqliteFree(pTable->aCol[i].zDflt);
drh75897232000-05-29 14:26:00 +0000233 }
234 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
235 pNext = pIndex->pNext;
236 sqliteDeleteIndex(db, pIndex);
237 }
drh6e142f52000-06-08 13:36:40 +0000238 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000239 sqliteFree(pTable->aCol);
drh75897232000-05-29 14:26:00 +0000240 sqliteFree(pTable);
241}
242
243/*
drh5edc3122001-09-13 21:53:09 +0000244** Unlink the given table from the hash tables and the delete the
245** table structure and all its indices.
246*/
247static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){
248 if( pTable->zName && db ){
249 int h = sqliteHashNoCase(pTable->zName, 0) % N_HASH;
250 if( db->apTblHash[h]==pTable ){
251 db->apTblHash[h] = pTable->pHash;
252 }else{
253 Table *p;
254 for(p=db->apTblHash[h]; p && p->pHash!=pTable; p=p->pHash){}
255 if( p && p->pHash==pTable ){
256 p->pHash = pTable->pHash;
257 }
258 }
259 }
260 sqliteDeleteTable(db, pTable);
261}
262
263/*
drh5e00f6c2001-09-13 13:46:56 +0000264** Check all Tables and Indexes in the internal hash table and commit
265** any additions or deletions to those hash tables.
266**
267** When executing CREATE TABLE and CREATE INDEX statements, the Table
268** and Index structures are created and added to the hash tables, but
269** the "isCommit" field is not set. This routine sets those fields.
270** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of
271** Table and Index structures is set but the structures are not unlinked
272** from the hash tables nor deallocated. This routine handles that
273** deallocation.
274**
275** See also: sqliteRollbackInternalChanges()
276*/
277void sqliteCommitInternalChanges(sqlite *db){
278 int i;
279 if( (db->flags & SQLITE_InternChanges)==0 ) return;
280 for(i=0; i<N_HASH; i++){
281 Table *pTable, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000282 for(pTable = db->apTblHash[i]; pTable; pTable=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000283 pNext = pTable->pHash;
284 if( pTable->isDelete ){
drh5edc3122001-09-13 21:53:09 +0000285 sqliteUnlinkAndDeleteTable(db, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000286 }else if( pTable->isCommit==0 ){
287 pTable->isCommit = 1;
288 }
289 }
290 }
291 for(i=0; i<N_HASH; i++){
292 Index *pIndex, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000293 for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000294 pNext = pIndex->pHash;
295 if( pIndex->isDelete ){
296 sqliteUnlinkAndDeleteIndex(db, pIndex);
297 }else if( pIndex->isCommit==0 ){
298 pIndex->isCommit = 1;
299 }
300 }
301 }
302 db->flags &= ~SQLITE_InternChanges;
303}
304
305/*
306** This routine runs when one or more CREATE TABLE, CREATE INDEX,
307** DROP TABLE, or DROP INDEX statements get rolled back. The
308** additions or deletions of Table and Index structures in the
309** internal hash tables are undone.
310**
311** See also: sqliteCommitInternalChanges()
312*/
313void sqliteRollbackInternalChanges(sqlite *db){
314 int i;
315 if( (db->flags & SQLITE_InternChanges)==0 ) return;
316 for(i=0; i<N_HASH; i++){
317 Table *pTable, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000318 for(pTable = db->apTblHash[i]; pTable; pTable=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000319 pNext = pTable->pHash;
320 if( !pTable->isCommit ){
drh5edc3122001-09-13 21:53:09 +0000321 sqliteUnlinkAndDeleteTable(db, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000322 }else if( pTable->isDelete ){
323 pTable->isDelete = 0;
324 }
325 }
326 }
327 for(i=0; i<N_HASH; i++){
328 Index *pIndex, *pNext;
drhbe0072d2001-09-13 14:46:09 +0000329 for(pIndex = db->apIdxHash[i]; pIndex; pIndex=pNext){
drh5e00f6c2001-09-13 13:46:56 +0000330 pNext = pIndex->pHash;
331 if( !pIndex->isCommit ){
332 sqliteUnlinkAndDeleteIndex(db, pIndex);
333 }else if( pIndex->isDelete ){
334 pIndex->isDelete = 0;
335 }
336 }
337 }
338 db->flags &= ~SQLITE_InternChanges;
339}
340
341/*
drh1ccde152000-06-17 13:12:39 +0000342** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000343**
344** Space to hold the name is obtained from sqliteMalloc() and must
345** be freed by the calling function.
346*/
drhcce7d172000-05-31 15:34:51 +0000347char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000348 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000349 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000350 return zName;
351}
352
353/*
354** Begin constructing a new table representation in memory. This is
355** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000356** to a CREATE TABLE statement. In particular, this routine is called
357** after seeing tokens "CREATE" and "TABLE" and the table name. The
358** pStart token is the CREATE and pName is the table name.
359**
360** The new table is constructed in files of the pParse structure. As
361** more of the CREATE TABLE statement is parsed, additional action
362** routines are called to build up more of the table.
drh75897232000-05-29 14:26:00 +0000363*/
364void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName){
365 Table *pTable;
366 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000367 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000368
369 pParse->sFirstToken = *pStart;
370 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000371 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000372 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000373 if( pTable!=0 ){
drh1d37e282000-05-30 03:12:21 +0000374 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
375 " already exists", 0, 0);
drh75897232000-05-29 14:26:00 +0000376 sqliteFree(zName);
377 pParse->nErr++;
378 return;
379 }
drhbe0072d2001-09-13 14:46:09 +0000380 if( sqliteFindIndex(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000381 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
382 zName, 0);
drh75897232000-05-29 14:26:00 +0000383 sqliteFree(zName);
384 pParse->nErr++;
385 return;
386 }
387 pTable = sqliteMalloc( sizeof(Table) );
drhdaffd0e2001-04-11 14:28:42 +0000388 if( pTable==0 ) return;
drh75897232000-05-29 14:26:00 +0000389 pTable->zName = zName;
390 pTable->pHash = 0;
391 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000392 pTable->aCol = 0;
drh75897232000-05-29 14:26:00 +0000393 pTable->pIndex = 0;
drhbe0072d2001-09-13 14:46:09 +0000394 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000395 pParse->pNewTable = pTable;
drhbe0072d2001-09-13 14:46:09 +0000396 if( !pParse->initFlag && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000397 Vdbe *v = sqliteGetVdbe(pParse);
398 if( v ){
399 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
400 }
401 }
drh75897232000-05-29 14:26:00 +0000402}
403
404/*
405** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000406**
407** The parser calls this routine once for each column declaration
408** in a CREATE TABLE statement. sqliteStartTable() gets called
409** first to get things going. Then this routine is called for each
410** column.
drh75897232000-05-29 14:26:00 +0000411*/
412void sqliteAddColumn(Parse *pParse, Token *pName){
413 Table *p;
414 char **pz;
415 if( (p = pParse->pNewTable)==0 ) return;
416 if( (p->nCol & 0x7)==0 ){
drh7020f652000-06-03 18:06:52 +0000417 p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
drhdaffd0e2001-04-11 14:28:42 +0000418 if( p->aCol==0 ){
419 p->nCol = 0;
420 return;
421 }
drh75897232000-05-29 14:26:00 +0000422 }
drh7020f652000-06-03 18:06:52 +0000423 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
424 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000425 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000426 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000427}
428
429/*
drh7020f652000-06-03 18:06:52 +0000430** The given token is the default value for the last column added to
431** the table currently under construction. If "minusFlag" is true, it
432** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000433**
434** This routine is called by the parser while in the middle of
435** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000436*/
437void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
438 Table *p;
439 int i;
440 char **pz;
441 if( (p = pParse->pNewTable)==0 ) return;
442 i = p->nCol-1;
443 pz = &p->aCol[i].zDflt;
444 if( minusFlag ){
445 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
446 }else{
447 sqliteSetNString(pz, pVal->z, pVal->n, 0);
448 }
449 sqliteDequote(*pz);
450}
451
452/*
drh75897232000-05-29 14:26:00 +0000453** This routine is called to report the final ")" that terminates
454** a CREATE TABLE statement.
455**
456** The table structure is added to the internal hash tables.
457**
drh1ccde152000-06-17 13:12:39 +0000458** An entry for the table is made in the master table on disk,
459** unless initFlag==1. When initFlag==1, it means we are reading
460** the master table because we just connected to the database, so
drh75897232000-05-29 14:26:00 +0000461** the entry for this table already exists in the master table.
462** We do not want to create it again.
463*/
464void sqliteEndTable(Parse *pParse, Token *pEnd){
465 Table *p;
466 int h;
drhbe0072d2001-09-13 14:46:09 +0000467 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000468
drhdaffd0e2001-04-11 14:28:42 +0000469 if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000470 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000471 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000472
473 /* Add the table to the in-memory representation of the database
474 */
drhdaffd0e2001-04-11 14:28:42 +0000475 if( pParse->explain==0 ){
drh75897232000-05-29 14:26:00 +0000476 h = sqliteHashNoCase(p->zName, 0) % N_HASH;
drhbe0072d2001-09-13 14:46:09 +0000477 p->pHash = db->apTblHash[h];
478 db->apTblHash[h] = p;
drh75897232000-05-29 14:26:00 +0000479 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000480 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000481 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000482 }
483
drhd78eeee2001-09-13 16:18:53 +0000484 /* If the initFlag is 1 it means we are reading the SQL off the
485 ** "sqlite_master" table on the disk. So do not write to the disk
486 ** again. Extract the table number from the pParse->newTnum field.
487 */
488 if( pParse->initFlag ){
489 p->tnum = pParse->newTnum;
490 }
491
drh75897232000-05-29 14:26:00 +0000492 /* If not initializing, then create the table on disk.
493 */
494 if( !pParse->initFlag ){
495 static VdbeOp addTable[] = {
drh5edc3122001-09-13 21:53:09 +0000496 { OP_Open, 0, 2, MASTER_NAME},
drh5e00f6c2001-09-13 13:46:56 +0000497 { OP_NewRecno, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000498 { OP_String, 0, 0, "table" },
drh75897232000-05-29 14:26:00 +0000499 { OP_String, 0, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +0000500 { OP_CreateTable, 0, 0, 0},
drh305cea62000-05-29 17:44:25 +0000501 { OP_String, 0, 0, 0}, /* 5 */
drh5e00f6c2001-09-13 13:46:56 +0000502 { OP_String, 0, 0, 0}, /* 6 */
drhd78eeee2001-09-13 16:18:53 +0000503 { OP_MakeRecord, 5, 0, 0},
drh28037572000-08-02 13:47:41 +0000504 { OP_Put, 0, 0, 0},
drh75897232000-05-29 14:26:00 +0000505 };
506 int n, base;
drhd8bc7082000-06-07 23:51:50 +0000507 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000508
drhd8bc7082000-06-07 23:51:50 +0000509 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000510 if( v==0 ) return;
511 n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1;
512 base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable);
drh75897232000-05-29 14:26:00 +0000513 sqliteVdbeChangeP3(v, base+3, p->zName, 0);
drh5e00f6c2001-09-13 13:46:56 +0000514 sqliteVdbeTableRootAddr(v, &p->tnum);
515 sqliteVdbeChangeP3(v, base+5, p->zName, 0);
516 sqliteVdbeChangeP3(v, base+6, pParse->sFirstToken.z, n);
drh28037572000-08-02 13:47:41 +0000517 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drh5edc3122001-09-13 21:53:09 +0000518 if( p->pIndex ){
519 /* If the table has a primary key, create an index in the database
520 ** for that key. */
521 Index *pIndex = p->pIndex;
522 assert( pIndex->pNext==0 );
523 assert( pIndex->tnum==0 );
524 sqliteVdbeAddOp(v, OP_CreateIndex, 0, 0, 0, 0),
525 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
526 }
drhbe0072d2001-09-13 14:46:09 +0000527 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000528 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
529 }
drh75897232000-05-29 14:26:00 +0000530 }
531}
532
533/*
534** Given a token, look up a table with that name. If not found, leave
535** an error for the parser to find and return NULL.
536*/
drhcce7d172000-05-31 15:34:51 +0000537Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +0000538 char *zName;
539 Table *pTab;
540 zName = sqliteTableNameFromToken(pTok);
541 if( zName==0 ) return 0;
542 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +0000543 sqliteFree(zName);
544 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +0000545 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
546 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +0000547 pParse->nErr++;
548 }
549 return pTab;
550}
551
552/*
553** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +0000554** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +0000555*/
556void sqliteDropTable(Parse *pParse, Token *pName){
557 Table *pTable;
drh75897232000-05-29 14:26:00 +0000558 Vdbe *v;
559 int base;
drh5edc3122001-09-13 21:53:09 +0000560 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000561
drhdaffd0e2001-04-11 14:28:42 +0000562 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000563 pTable = sqliteTableFromToken(pParse, pName);
564 if( pTable==0 ) return;
565 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +0000566 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
567 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +0000568 pParse->nErr++;
569 return;
570 }
571
drh1ccde152000-06-17 13:12:39 +0000572 /* Generate code to remove the table from the master table
573 ** on disk.
574 */
drhd8bc7082000-06-07 23:51:50 +0000575 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000576 if( v ){
577 static VdbeOp dropTable[] = {
drh5edc3122001-09-13 21:53:09 +0000578 { OP_Open, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000579 { OP_Rewind, 0, 0, 0},
580 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000581 { OP_Next, 0, ADDR(9), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000582 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000583 { OP_Column, 0, 3, 0},
drhd78eeee2001-09-13 16:18:53 +0000584 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000585 { OP_Delete, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000586 { OP_Goto, 0, ADDR(3), 0},
drh5edc3122001-09-13 21:53:09 +0000587 { OP_Destroy, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000588 { OP_Close, 0, 0, 0},
589 };
590 Index *pIdx;
drh5edc3122001-09-13 21:53:09 +0000591 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000592 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
593 }
drh75897232000-05-29 14:26:00 +0000594 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh5edc3122001-09-13 21:53:09 +0000595 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
596 sqliteVdbeChangeP1(v, base+9, pTable->tnum);
drh75897232000-05-29 14:26:00 +0000597 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drh5e00f6c2001-09-13 13:46:56 +0000598 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, 0, 0, 0);
599 }
drh5edc3122001-09-13 21:53:09 +0000600 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000601 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000602 }
603 }
604
drh5e00f6c2001-09-13 13:46:56 +0000605 /* Mark the in-memory Table structure as being deleted. The actually
606 ** deletion occurs inside of sqliteCommitInternalChanges().
drh75897232000-05-29 14:26:00 +0000607 **
608 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +0000609 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +0000610 */
611 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000612 pTable->isDelete = 1;
drh5edc3122001-09-13 21:53:09 +0000613 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000614 }
615}
616
617/*
618** Create a new index for an SQL table. pIndex is the name of the index
619** and pTable is the name of the table that is to be indexed. Both will
620** be NULL for a primary key. In that case, use pParse->pNewTable as the
621** table to be indexed.
622**
drh1ccde152000-06-17 13:12:39 +0000623** pList is a list of columns to be indexed. pList will be NULL if the
624** most recently added column of the table is labeled as the primary key.
drh75897232000-05-29 14:26:00 +0000625*/
626void sqliteCreateIndex(
627 Parse *pParse, /* All information about this parse */
628 Token *pName, /* Name of the index. May be NULL */
629 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +0000630 IdList *pList, /* A list of columns to be indexed */
drh75897232000-05-29 14:26:00 +0000631 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
632 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
633){
634 Table *pTab; /* Table to be indexed */
635 Index *pIndex; /* The index to be created */
636 char *zName = 0;
637 int i, j, h;
638 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +0000639 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000640
drhdaffd0e2001-04-11 14:28:42 +0000641 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
642
drh75897232000-05-29 14:26:00 +0000643 /*
644 ** Find the table that is to be indexed. Return early if not found.
645 */
646 if( pTable!=0 ){
647 pTab = sqliteTableFromToken(pParse, pTable);
648 }else{
649 pTab = pParse->pNewTable;
650 }
651 if( pTab==0 || pParse->nErr ) goto exit_create_index;
652 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +0000653 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
654 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +0000655 pParse->nErr++;
656 goto exit_create_index;
657 }
658
659 /*
660 ** Find the name of the index. Make sure there is not already another
661 ** index or table with the same name.
662 */
663 if( pName ){
664 zName = sqliteTableNameFromToken(pName);
665 }else{
666 zName = 0;
667 sqliteSetString(&zName, pTab->zName, "__primary_key", 0);
668 }
drhdaffd0e2001-04-11 14:28:42 +0000669 if( zName==0 ) goto exit_create_index;
drhbe0072d2001-09-13 14:46:09 +0000670 if( sqliteFindIndex(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000671 sqliteSetString(&pParse->zErrMsg, "index ", zName,
672 " already exists", 0);
drh75897232000-05-29 14:26:00 +0000673 pParse->nErr++;
674 goto exit_create_index;
675 }
drhbe0072d2001-09-13 14:46:09 +0000676 if( sqliteFindTable(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000677 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
678 zName, 0);
drh75897232000-05-29 14:26:00 +0000679 pParse->nErr++;
680 goto exit_create_index;
681 }
682
683 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +0000684 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +0000685 ** So create a fake list to simulate this.
686 */
687 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +0000688 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +0000689 nullId.n = strlen(nullId.z);
690 pList = sqliteIdListAppend(0, &nullId);
691 if( pList==0 ) goto exit_create_index;
692 }
693
694 /*
695 ** Allocate the index structure.
696 */
drhdcc581c2000-05-30 13:44:19 +0000697 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +0000698 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +0000699 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +0000700 pIndex->aiColumn = (int*)&pIndex[1];
701 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +0000702 strcpy(pIndex->zName, zName);
703 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +0000704 pIndex->nColumn = pList->nId;
drh75897232000-05-29 14:26:00 +0000705
drh1ccde152000-06-17 13:12:39 +0000706 /* Scan the names of the columns of the table to be indexed and
707 ** load the column indices into the Index structure. Report an error
708 ** if any column is not found.
drh75897232000-05-29 14:26:00 +0000709 */
710 for(i=0; i<pList->nId; i++){
711 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000712 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +0000713 }
714 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +0000715 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +0000716 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +0000717 pParse->nErr++;
718 sqliteFree(pIndex);
719 goto exit_create_index;
720 }
drh967e8b72000-06-21 13:59:10 +0000721 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +0000722 }
723
724 /* Link the new Index structure to its table and to the other
725 ** in-memory database structures.
726 */
727 if( pParse->explain==0 ){
728 h = sqliteHashNoCase(pIndex->zName, 0) % N_HASH;
drhbe0072d2001-09-13 14:46:09 +0000729 pIndex->pHash = db->apIdxHash[h];
730 db->apIdxHash[h] = pIndex;
drh75897232000-05-29 14:26:00 +0000731 pIndex->pNext = pTab->pIndex;
732 pTab->pIndex = pIndex;
drh5e00f6c2001-09-13 13:46:56 +0000733 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000734 }
735
drhd78eeee2001-09-13 16:18:53 +0000736 /* If the initFlag is 1 it means we are reading the SQL off the
737 ** "sqlite_master" table on the disk. So do not write to the disk
738 ** again. Extract the table number from the pParse->newTnum field.
739 */
740 if( pParse->initFlag ){
741 pIndex->tnum = pParse->newTnum;
742 }
743
drh75897232000-05-29 14:26:00 +0000744 /* If the initFlag is 0 then create the index on disk. This
745 ** involves writing the index into the master table and filling in the
746 ** index with the current table contents.
747 **
748 ** The initFlag is 0 when the user first enters a CREATE INDEX
749 ** command. The initFlag is 1 when a database is opened and
750 ** CREATE INDEX statements are read out of the master table. In
751 ** the latter case the index already exists on disk, which is why
752 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +0000753 **
754 ** If pTable==0 it means this index is generated as a primary key
755 ** and those does not have a CREATE INDEX statement to add to the
756 ** master table. Also, since primary keys are created at the same
757 ** time as tables, the table will be empty so there is no need to
758 ** initialize the index. Hence, skip all the code generation if
759 ** pTable==0.
drh75897232000-05-29 14:26:00 +0000760 */
drh5edc3122001-09-13 21:53:09 +0000761 else if( pParse->initFlag==0 && pTable!=0 ){
drh75897232000-05-29 14:26:00 +0000762 static VdbeOp addTable[] = {
drh5edc3122001-09-13 21:53:09 +0000763 { OP_Open, 2, 2, MASTER_NAME},
drh5e00f6c2001-09-13 13:46:56 +0000764 { OP_NewRecno, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000765 { OP_String, 0, 0, "index"},
drh75897232000-05-29 14:26:00 +0000766 { OP_String, 0, 0, 0}, /* 3 */
drh5edc3122001-09-13 21:53:09 +0000767 { OP_CreateIndex, 1, 0, 0},
768 { OP_Dup, 0, 0, 0},
769 { OP_Open, 1, 0, 0}, /* 6 */
770 { OP_String, 0, 0, 0}, /* 7 */
771 { OP_String, 0, 0, 0}, /* 8 */
drh5e00f6c2001-09-13 13:46:56 +0000772 { OP_MakeRecord, 5, 0, 0},
drhbed86902000-06-02 13:27:59 +0000773 { OP_Put, 2, 0, 0},
774 { OP_Close, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000775 };
776 int n;
777 Vdbe *v = pParse->pVdbe;
778 int lbl1, lbl2;
779 int i;
780
drhd8bc7082000-06-07 23:51:50 +0000781 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000782 if( v==0 ) goto exit_create_index;
drhbe0072d2001-09-13 14:46:09 +0000783 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000784 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
785 }
drh75897232000-05-29 14:26:00 +0000786 if( pStart && pEnd ){
787 int base;
788 n = (int)pEnd->z - (int)pStart->z + 1;
789 base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable);
drhb24fcbe2000-05-29 23:30:50 +0000790 sqliteVdbeChangeP3(v, base+3, pIndex->zName, 0);
drh5e00f6c2001-09-13 13:46:56 +0000791 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
drh5edc3122001-09-13 21:53:09 +0000792 sqliteVdbeChangeP3(v, base+6, pIndex->zName, 0);
793 sqliteVdbeChangeP3(v, base+7, pTab->zName, 0);
794 sqliteVdbeChangeP3(v, base+8, pStart->z, n);
drh75897232000-05-29 14:26:00 +0000795 }
drh5edc3122001-09-13 21:53:09 +0000796 sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0);
drh75897232000-05-29 14:26:00 +0000797 lbl1 = sqliteVdbeMakeLabel(v);
798 lbl2 = sqliteVdbeMakeLabel(v);
drhd78eeee2001-09-13 16:18:53 +0000799 sqliteVdbeAddOp(v, OP_Rewind, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000800 sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1);
drhbe0072d2001-09-13 14:46:09 +0000801 sqliteVdbeAddOp(v, OP_Recno, 0, 0, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000802 for(i=0; i<pIndex->nColumn; i++){
drh5e00f6c2001-09-13 13:46:56 +0000803 sqliteVdbeAddOp(v, OP_Column, 0, pIndex->aiColumn[i], 0, 0);
drh75897232000-05-29 14:26:00 +0000804 }
drh5e00f6c2001-09-13 13:46:56 +0000805 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000806 sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0);
807 sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0);
808 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2);
drh75897232000-05-29 14:26:00 +0000809 sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000810 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drhbe0072d2001-09-13 14:46:09 +0000811 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000812 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
813 }
drh75897232000-05-29 14:26:00 +0000814 }
815
816 /* Reclaim memory on an EXPLAIN call.
817 */
818 if( pParse->explain ){
819 sqliteFree(pIndex);
820 }
821
822 /* Clean up before exiting */
823exit_create_index:
824 sqliteIdListDelete(pList);
825 sqliteFree(zName);
826 return;
827}
828
829/*
830** This routine will drop an existing named index.
831*/
832void sqliteDropIndex(Parse *pParse, Token *pName){
833 Index *pIndex;
834 char *zName;
835 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +0000836 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000837
drhdaffd0e2001-04-11 14:28:42 +0000838 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000839 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000840 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000841 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +0000842 sqliteFree(zName);
843 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +0000844 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
845 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +0000846 pParse->nErr++;
847 return;
848 }
849
850 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +0000851 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000852 if( v ){
853 static VdbeOp dropIndex[] = {
drh5edc3122001-09-13 21:53:09 +0000854 { OP_Open, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000855 { OP_Rewind, 0, 0, 0},
856 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000857 { OP_Next, 0, ADDR(8), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000858 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000859 { OP_Column, 0, 1, 0},
drhd78eeee2001-09-13 16:18:53 +0000860 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000861 { OP_Delete, 0, 0, 0},
drh5edc3122001-09-13 21:53:09 +0000862 { OP_Destroy, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +0000863 { OP_Close, 0, 0, 0},
864 };
865 int base;
866
drhbe0072d2001-09-13 14:46:09 +0000867 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000868 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
869 }
drh75897232000-05-29 14:26:00 +0000870 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh5edc3122001-09-13 21:53:09 +0000871 sqliteVdbeChangeP1(v, base+8, pIndex->tnum);
drhbe0072d2001-09-13 14:46:09 +0000872 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000873 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
874 }
drh75897232000-05-29 14:26:00 +0000875 }
876
drh5e00f6c2001-09-13 13:46:56 +0000877 /* Mark the internal Index structure for deletion by the
878 ** sqliteCommitInternalChanges routine.
drh75897232000-05-29 14:26:00 +0000879 */
880 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000881 pIndex->isDelete = 1;
882 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000883 }
884}
885
886/*
887** Add a new element to the end of an expression list. If pList is
888** initially NULL, then create a new expression list.
889*/
890ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
891 int i;
892 if( pList==0 ){
893 pList = sqliteMalloc( sizeof(ExprList) );
drhdaffd0e2001-04-11 14:28:42 +0000894 if( pList==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000895 }
drh75897232000-05-29 14:26:00 +0000896 if( (pList->nExpr & 7)==0 ){
897 int n = pList->nExpr + 8;
898 pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
899 if( pList->a==0 ){
900 pList->nExpr = 0;
901 return pList;
902 }
903 }
904 i = pList->nExpr++;
905 pList->a[i].pExpr = pExpr;
906 pList->a[i].zName = 0;
907 if( pName ){
908 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000909 sqliteDequote(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +0000910 }
911 return pList;
912}
913
914/*
915** Delete an entire expression list.
916*/
917void sqliteExprListDelete(ExprList *pList){
918 int i;
919 if( pList==0 ) return;
920 for(i=0; i<pList->nExpr; i++){
921 sqliteExprDelete(pList->a[i].pExpr);
922 sqliteFree(pList->a[i].zName);
923 }
924 sqliteFree(pList->a);
925 sqliteFree(pList);
926}
927
928/*
929** Append a new element to the given IdList. Create a new IdList if
930** need be.
drhdaffd0e2001-04-11 14:28:42 +0000931**
932** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +0000933*/
934IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
935 if( pList==0 ){
936 pList = sqliteMalloc( sizeof(IdList) );
937 if( pList==0 ) return 0;
938 }
939 if( (pList->nId & 7)==0 ){
940 pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
941 if( pList->a==0 ){
942 pList->nId = 0;
drhdaffd0e2001-04-11 14:28:42 +0000943 sqliteIdListDelete(pList);
944 return 0;
drh75897232000-05-29 14:26:00 +0000945 }
946 }
947 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
948 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +0000949 char **pz = &pList->a[pList->nId].zName;
950 sqliteSetNString(pz, pToken->z, pToken->n, 0);
951 if( *pz==0 ){
952 sqliteIdListDelete(pList);
953 return 0;
954 }else{
955 sqliteDequote(*pz);
956 }
drh75897232000-05-29 14:26:00 +0000957 }
958 pList->nId++;
959 return pList;
960}
961
962/*
963** Add an alias to the last identifier on the given identifier list.
964*/
965void sqliteIdListAddAlias(IdList *pList, Token *pToken){
966 if( pList && pList->nId>0 ){
967 int i = pList->nId - 1;
968 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +0000969 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +0000970 }
971}
972
973/*
974** Delete an entire IdList
975*/
976void sqliteIdListDelete(IdList *pList){
977 int i;
978 if( pList==0 ) return;
979 for(i=0; i<pList->nId; i++){
980 sqliteFree(pList->a[i].zName);
981 sqliteFree(pList->a[i].zAlias);
drhdaffd0e2001-04-11 14:28:42 +0000982 if( pList->a[i].pSelect ){
983 sqliteFree(pList->a[i].zName);
984 sqliteSelectDelete(pList->a[i].pSelect);
985 sqliteDeleteTable(0, pList->a[i].pTab);
986 }
drh75897232000-05-29 14:26:00 +0000987 }
988 sqliteFree(pList->a);
989 sqliteFree(pList);
990}
991
drh982cef72000-05-30 16:27:03 +0000992
993/*
994** The COPY command is for compatibility with PostgreSQL and specificially
995** for the ability to read the output of pg_dump. The format is as
996** follows:
997**
998** COPY table FROM file [USING DELIMITERS string]
999**
1000** "table" is an existing table name. We will read lines of code from
1001** file to fill this table with data. File might be "stdin". The optional
1002** delimiter string identifies the field separators. The default is a tab.
1003*/
1004void sqliteCopy(
1005 Parse *pParse, /* The parser context */
1006 Token *pTableName, /* The name of the table into which we will insert */
1007 Token *pFilename, /* The file from which to obtain information */
1008 Token *pDelimiter /* Use this as the field delimiter */
1009){
1010 Table *pTab;
1011 char *zTab;
1012 int i, j;
1013 Vdbe *v;
1014 int addr, end;
1015 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001016 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001017
1018 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001019 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001020 pTab = sqliteFindTable(db, zTab);
drh982cef72000-05-30 16:27:03 +00001021 sqliteFree(zTab);
1022 if( pTab==0 ){
1023 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1024 pTableName->z, pTableName->n, 0);
1025 pParse->nErr++;
1026 goto copy_cleanup;
1027 }
1028 if( pTab->readOnly ){
1029 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1030 " may not be modified", 0);
1031 pParse->nErr++;
1032 goto copy_cleanup;
1033 }
drhd8bc7082000-06-07 23:51:50 +00001034 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001035 if( v ){
drhbe0072d2001-09-13 14:46:09 +00001036 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001037 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
1038 }
drh982cef72000-05-30 16:27:03 +00001039 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0);
1040 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001041 sqliteVdbeDequoteP3(v, addr);
drh5e00f6c2001-09-13 13:46:56 +00001042 sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0);
drh982cef72000-05-30 16:27:03 +00001043 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh5e00f6c2001-09-13 13:46:56 +00001044 sqliteVdbeAddOp(v, OP_Open, i, pIdx->tnum, pIdx->zName, 0);
drh982cef72000-05-30 16:27:03 +00001045 }
1046 end = sqliteVdbeMakeLabel(v);
1047 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0);
1048 if( pDelimiter ){
1049 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1050 sqliteVdbeDequoteP3(v, addr);
1051 }else{
1052 sqliteVdbeChangeP3(v, addr, "\t", 1);
1053 }
drh5e00f6c2001-09-13 13:46:56 +00001054 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001055 if( pTab->pIndex ){
1056 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1057 }
1058 for(i=0; i<pTab->nCol; i++){
drh5e00f6c2001-09-13 13:46:56 +00001059 sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001060 }
1061 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
1062 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
1063 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1064 if( pIdx->pNext ){
1065 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1066 }
drh967e8b72000-06-21 13:59:10 +00001067 for(j=0; j<pIdx->nColumn; j++){
drh5e00f6c2001-09-13 13:46:56 +00001068 sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001069 }
drh5e00f6c2001-09-13 13:46:56 +00001070 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001071 sqliteVdbeAddOp(v, OP_PutIdx, i, 0, 0, 0);
1072 }
1073 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
1074 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end);
drhbe0072d2001-09-13 14:46:09 +00001075 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001076 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1077 }
drh982cef72000-05-30 16:27:03 +00001078 }
1079
1080copy_cleanup:
1081 return;
1082}
drhdce2cbe2000-05-31 02:27:49 +00001083
1084/*
1085** The non-standard VACUUM command is used to clean up the database,
1086** collapse free space, etc. It is modelled after the VACUUM command
1087** in PostgreSQL.
1088*/
1089void sqliteVacuum(Parse *pParse, Token *pTableName){
1090 char *zName;
1091 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001092 sqlite *db = pParse->db;
drhdce2cbe2000-05-31 02:27:49 +00001093
drhdaffd0e2001-04-11 14:28:42 +00001094 if( pParse->nErr || sqlite_malloc_failed ) return;
drhdce2cbe2000-05-31 02:27:49 +00001095 if( pTableName ){
1096 zName = sqliteTableNameFromToken(pTableName);
1097 }else{
1098 zName = 0;
1099 }
drhbe0072d2001-09-13 14:46:09 +00001100 if( zName && sqliteFindIndex(db, zName)==0
1101 && sqliteFindTable(db, zName)==0 ){
drhdce2cbe2000-05-31 02:27:49 +00001102 sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0);
1103 pParse->nErr++;
1104 goto vacuum_cleanup;
1105 }
drhd8bc7082000-06-07 23:51:50 +00001106 v = sqliteGetVdbe(pParse);
drhdce2cbe2000-05-31 02:27:49 +00001107 if( v==0 ) goto vacuum_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001108 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001109 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
1110 }
drhdce2cbe2000-05-31 02:27:49 +00001111 if( zName ){
1112 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0);
1113 }else{
1114 int h;
1115 Table *pTab;
1116 Index *pIdx;
1117 for(h=0; h<N_HASH; h++){
drhbe0072d2001-09-13 14:46:09 +00001118 for(pTab=db->apTblHash[h]; pTab; pTab=pTab->pHash){
drhdce2cbe2000-05-31 02:27:49 +00001119 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0);
1120 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1121 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0);
1122 }
1123 }
1124 }
1125 }
drhbe0072d2001-09-13 14:46:09 +00001126 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001127 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1128 }
drhdce2cbe2000-05-31 02:27:49 +00001129
1130vacuum_cleanup:
1131 sqliteFree(zName);
1132 return;
1133}
drhc4a3c772001-04-04 11:48:57 +00001134
1135/*
1136** Begin a transaction
1137*/
1138void sqliteBeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001139 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001140 Vdbe *v;
1141
drhc4a3c772001-04-04 11:48:57 +00001142 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001143 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001144 if( db->flags & SQLITE_InTrans ) return;
drh5e00f6c2001-09-13 13:46:56 +00001145 v = sqliteGetVdbe(pParse);
1146 if( v ){
1147 sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001148 }
drh5e00f6c2001-09-13 13:46:56 +00001149 db->flags |= SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001150}
1151
1152/*
1153** Commit a transaction
1154*/
1155void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001156 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001157 Vdbe *v;
1158
drhc4a3c772001-04-04 11:48:57 +00001159 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001160 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001161 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001162 v = sqliteGetVdbe(pParse);
1163 if( v ){
1164 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001165 }
drh5e00f6c2001-09-13 13:46:56 +00001166 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001167}
1168
1169/*
1170** Rollback a transaction
1171*/
1172void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001173 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001174 Vdbe *v;
1175
drhc4a3c772001-04-04 11:48:57 +00001176 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001177 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001178 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001179 v = sqliteGetVdbe(pParse);
1180 if( v ){
1181 sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001182 }
drh5e00f6c2001-09-13 13:46:56 +00001183 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001184}