blob: 24862028cf64b60709700680e91440499e739478 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This file contains C code routines that are called by the SQLite parser
13** when syntax rules are reduced. The routines in this file handle the
14** following kinds of SQL syntax:
drh75897232000-05-29 14:26:00 +000015**
drhbed86902000-06-02 13:27:59 +000016** CREATE TABLE
17** DROP TABLE
18** CREATE INDEX
19** DROP INDEX
20** creating expressions and ID lists
21** COPY
22** VACUUM
drhb19a2bc2001-09-16 00:13:26 +000023** BEGIN TRANSACTION
24** COMMIT
25** ROLLBACK
26** PRAGMA
drhbed86902000-06-02 13:27:59 +000027**
drhecdc7532001-09-23 02:35:53 +000028** $Id: build.c,v 1.39 2001/09/23 02:35:53 drh Exp $
drh75897232000-05-29 14:26:00 +000029*/
30#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000031#include <ctype.h>
drh75897232000-05-29 14:26:00 +000032
33/*
34** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000035** parsed and we want to execute the VDBE code to implement
36** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000037** constructed VDBE code to do the work of the SQL statement.
38** This routine just has to execute the VDBE code.
39**
40** Note that if an error occurred, it might be the case that
41** no VDBE code was generated.
42*/
43void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000044 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000045 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000046 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000047 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000048 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000049 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
50 &pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +000051 }else{
drh3fc190c2001-09-14 03:24:23 +000052 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000053 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000054 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000055 &pParse->zErrMsg, db->pBusyArg,
56 db->xBusyCallback);
drhecdc7532001-09-23 02:35:53 +000057 if( rc ) pParse->nErr++;
drh75897232000-05-29 14:26:00 +000058 }
59 sqliteVdbeDelete(pParse->pVdbe);
60 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000061 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000062 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000063 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000064 }
65}
66
67/*
68** Construct a new expression node and return a pointer to it.
69*/
70Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
71 Expr *pNew;
72 pNew = sqliteMalloc( sizeof(Expr) );
73 if( pNew==0 ) return 0;
74 pNew->op = op;
75 pNew->pLeft = pLeft;
76 pNew->pRight = pRight;
77 if( pToken ){
78 pNew->token = *pToken;
79 }else{
80 pNew->token.z = "";
81 pNew->token.n = 0;
82 }
drhe1b6a5b2000-07-29 13:06:59 +000083 if( pLeft && pRight ){
84 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
85 }else{
86 pNew->span = pNew->token;
87 }
drh75897232000-05-29 14:26:00 +000088 return pNew;
89}
90
91/*
drhe1b6a5b2000-07-29 13:06:59 +000092** Set the Expr.token field of the given expression to span all
93** text between the two given tokens.
94*/
95void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drhdaffd0e2001-04-11 14:28:42 +000096 if( pExpr ){
97 pExpr->span.z = pLeft->z;
98 pExpr->span.n = pRight->n + (int)pRight->z - (int)pLeft->z;
99 }
drhe1b6a5b2000-07-29 13:06:59 +0000100}
101
102/*
drh75897232000-05-29 14:26:00 +0000103** Construct a new expression node for a function with multiple
104** arguments.
105*/
106Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
107 Expr *pNew;
108 pNew = sqliteMalloc( sizeof(Expr) );
109 if( pNew==0 ) return 0;
110 pNew->op = TK_FUNCTION;
111 pNew->pList = pList;
112 if( pToken ){
113 pNew->token = *pToken;
114 }else{
115 pNew->token.z = "";
116 pNew->token.n = 0;
117 }
118 return pNew;
119}
120
121/*
122** Recursively delete an expression tree.
123*/
124void sqliteExprDelete(Expr *p){
125 if( p==0 ) return;
126 if( p->pLeft ) sqliteExprDelete(p->pLeft);
127 if( p->pRight ) sqliteExprDelete(p->pRight);
drh19a775c2000-06-05 18:54:46 +0000128 if( p->pList ) sqliteExprListDelete(p->pList);
129 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
drh75897232000-05-29 14:26:00 +0000130 sqliteFree(p);
131}
132
133/*
134** Locate the in-memory structure that describes the
135** format of a particular database table given the name
136** of that table. Return NULL if not found.
137*/
138Table *sqliteFindTable(sqlite *db, char *zName){
drhbeae3192001-09-22 18:12:08 +0000139 return sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh75897232000-05-29 14:26:00 +0000140}
141
142/*
143** Locate the in-memory structure that describes the
drh1ccde152000-06-17 13:12:39 +0000144** format of a particular index given the name
145** of that index. Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000146*/
147Index *sqliteFindIndex(sqlite *db, char *zName){
drhbeae3192001-09-22 18:12:08 +0000148 return sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh75897232000-05-29 14:26:00 +0000149}
150
151/*
152** Remove the given index from the index hash table, and free
153** its memory structures.
154**
drhdaffd0e2001-04-11 14:28:42 +0000155** The index is removed from the database hash table if db!=NULL.
156** But it is not unlinked from the Table that is being indexed.
157** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000158*/
159static void sqliteDeleteIndex(sqlite *db, Index *pIndex){
drhdaffd0e2001-04-11 14:28:42 +0000160 if( pIndex->zName && db ){
drhbeae3192001-09-22 18:12:08 +0000161 sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(pIndex->zName)+1, 0);
drh75897232000-05-29 14:26:00 +0000162 }
163 sqliteFree(pIndex);
164}
165
166/*
drhbeae3192001-09-22 18:12:08 +0000167** Unlink the given index from its table, then remove
drh5e00f6c2001-09-13 13:46:56 +0000168** the index from the index hash table, and free its memory
169** structures.
170*/
171static void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
172 if( pIndex->pTable->pIndex==pIndex ){
173 pIndex->pTable->pIndex = pIndex->pNext;
174 }else{
175 Index *p;
176 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
177 if( p && p->pNext==pIndex ){
178 p->pNext = pIndex->pNext;
179 }
180 }
181 sqliteDeleteIndex(db, pIndex);
182}
183
184/*
drh75897232000-05-29 14:26:00 +0000185** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000186** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000187**
188** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000189** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000190** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000191**
192** Indices associated with the table are unlinked from the "db"
193** data structure if db!=NULL. If db==NULL, indices attached to
194** the table are deleted, but it is assumed they have already been
195** unlinked.
drh75897232000-05-29 14:26:00 +0000196*/
197void sqliteDeleteTable(sqlite *db, Table *pTable){
198 int i;
199 Index *pIndex, *pNext;
200 if( pTable==0 ) return;
201 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000202 sqliteFree(pTable->aCol[i].zName);
203 sqliteFree(pTable->aCol[i].zDflt);
drh75897232000-05-29 14:26:00 +0000204 }
205 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
206 pNext = pIndex->pNext;
207 sqliteDeleteIndex(db, pIndex);
208 }
drh6e142f52000-06-08 13:36:40 +0000209 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000210 sqliteFree(pTable->aCol);
drh75897232000-05-29 14:26:00 +0000211 sqliteFree(pTable);
212}
213
214/*
drh5edc3122001-09-13 21:53:09 +0000215** Unlink the given table from the hash tables and the delete the
216** table structure and all its indices.
217*/
218static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *pTable){
219 if( pTable->zName && db ){
drhbeae3192001-09-22 18:12:08 +0000220 sqliteHashInsert(&db->tblHash, pTable->zName, strlen(pTable->zName)+1, 0);
drh5edc3122001-09-13 21:53:09 +0000221 }
222 sqliteDeleteTable(db, pTable);
223}
224
225/*
drh5e00f6c2001-09-13 13:46:56 +0000226** Check all Tables and Indexes in the internal hash table and commit
227** any additions or deletions to those hash tables.
228**
229** When executing CREATE TABLE and CREATE INDEX statements, the Table
230** and Index structures are created and added to the hash tables, but
231** the "isCommit" field is not set. This routine sets those fields.
232** When executing DROP TABLE and DROP INDEX, the "isDelete" fields of
233** Table and Index structures is set but the structures are not unlinked
234** from the hash tables nor deallocated. This routine handles that
235** deallocation.
236**
237** See also: sqliteRollbackInternalChanges()
238*/
239void sqliteCommitInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000240 Hash toDelete;
241 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000242 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drhbeae3192001-09-22 18:12:08 +0000243 sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
drh50e5dad2001-09-15 00:57:28 +0000244 db->schema_cookie = db->next_cookie;
drhbeae3192001-09-22 18:12:08 +0000245 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
246 Table *pTable = sqliteHashData(pElem);
247 if( pTable->isDelete ){
248 sqliteHashInsert(&toDelete, pTable, 0, pTable);
249 }else{
250 pTable->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000251 }
252 }
drhbeae3192001-09-22 18:12:08 +0000253 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
254 Table *pTable = sqliteHashData(pElem);
255 sqliteUnlinkAndDeleteTable(db, pTable);
256 }
257 sqliteHashClear(&toDelete);
258 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
259 Table *pIndex = sqliteHashData(pElem);
260 if( pIndex->isDelete ){
261 sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
262 }else{
263 pIndex->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000264 }
265 }
drhbeae3192001-09-22 18:12:08 +0000266 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
267 Index *pIndex = sqliteHashData(pElem);
268 sqliteUnlinkAndDeleteIndex(db, pIndex);
269 }
270 sqliteHashClear(&toDelete);
drh5e00f6c2001-09-13 13:46:56 +0000271 db->flags &= ~SQLITE_InternChanges;
272}
273
274/*
275** This routine runs when one or more CREATE TABLE, CREATE INDEX,
276** DROP TABLE, or DROP INDEX statements get rolled back. The
277** additions or deletions of Table and Index structures in the
278** internal hash tables are undone.
279**
280** See also: sqliteCommitInternalChanges()
281*/
282void sqliteRollbackInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000283 Hash toDelete;
284 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000285 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drhbeae3192001-09-22 18:12:08 +0000286 sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
drh50e5dad2001-09-15 00:57:28 +0000287 db->next_cookie = db->schema_cookie;
drhbeae3192001-09-22 18:12:08 +0000288 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
289 Table *pTable = sqliteHashData(pElem);
290 if( !pTable->isCommit ){
291 sqliteHashInsert(&toDelete, pTable, 0, pTable);
292 }else{
293 pTable->isDelete = 0;
drh5e00f6c2001-09-13 13:46:56 +0000294 }
295 }
drhbeae3192001-09-22 18:12:08 +0000296 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
297 Table *pTable = sqliteHashData(pElem);
298 sqliteUnlinkAndDeleteTable(db, pTable);
299 }
300 sqliteHashClear(&toDelete);
301 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
302 Table *pIndex = sqliteHashData(pElem);
303 if( !pIndex->isCommit ){
304 sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
305 }else{
306 pIndex->isDelete = 0;
drh5e00f6c2001-09-13 13:46:56 +0000307 }
308 }
drhbeae3192001-09-22 18:12:08 +0000309 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
310 Index *pIndex = sqliteHashData(pElem);
311 sqliteUnlinkAndDeleteIndex(db, pIndex);
312 }
313 sqliteHashClear(&toDelete);
drh5e00f6c2001-09-13 13:46:56 +0000314 db->flags &= ~SQLITE_InternChanges;
315}
316
317/*
drh1ccde152000-06-17 13:12:39 +0000318** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000319**
320** Space to hold the name is obtained from sqliteMalloc() and must
321** be freed by the calling function.
322*/
drhcce7d172000-05-31 15:34:51 +0000323char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000324 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000325 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000326 return zName;
327}
328
329/*
330** Begin constructing a new table representation in memory. This is
331** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000332** to a CREATE TABLE statement. In particular, this routine is called
333** after seeing tokens "CREATE" and "TABLE" and the table name. The
334** pStart token is the CREATE and pName is the table name.
335**
336** The new table is constructed in files of the pParse structure. As
337** more of the CREATE TABLE statement is parsed, additional action
338** routines are called to build up more of the table.
drh75897232000-05-29 14:26:00 +0000339*/
340void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName){
341 Table *pTable;
342 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000343 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000344
345 pParse->sFirstToken = *pStart;
346 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000347 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000348 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000349 if( pTable!=0 ){
drh1d37e282000-05-30 03:12:21 +0000350 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
351 " already exists", 0, 0);
drh75897232000-05-29 14:26:00 +0000352 sqliteFree(zName);
353 pParse->nErr++;
354 return;
355 }
drhbe0072d2001-09-13 14:46:09 +0000356 if( sqliteFindIndex(db, zName) ){
drh1d37e282000-05-30 03:12:21 +0000357 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
358 zName, 0);
drh75897232000-05-29 14:26:00 +0000359 sqliteFree(zName);
360 pParse->nErr++;
361 return;
362 }
363 pTable = sqliteMalloc( sizeof(Table) );
drhdaffd0e2001-04-11 14:28:42 +0000364 if( pTable==0 ) return;
drh75897232000-05-29 14:26:00 +0000365 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000366 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000367 pTable->aCol = 0;
drh75897232000-05-29 14:26:00 +0000368 pTable->pIndex = 0;
drhbe0072d2001-09-13 14:46:09 +0000369 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000370 pParse->pNewTable = pTable;
drhbe0072d2001-09-13 14:46:09 +0000371 if( !pParse->initFlag && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000372 Vdbe *v = sqliteGetVdbe(pParse);
373 if( v ){
374 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000375 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000376 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000377 }
378 }
drh75897232000-05-29 14:26:00 +0000379}
380
381/*
382** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000383**
384** The parser calls this routine once for each column declaration
385** in a CREATE TABLE statement. sqliteStartTable() gets called
386** first to get things going. Then this routine is called for each
387** column.
drh75897232000-05-29 14:26:00 +0000388*/
389void sqliteAddColumn(Parse *pParse, Token *pName){
390 Table *p;
391 char **pz;
392 if( (p = pParse->pNewTable)==0 ) return;
393 if( (p->nCol & 0x7)==0 ){
drh7020f652000-06-03 18:06:52 +0000394 p->aCol = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
drhdaffd0e2001-04-11 14:28:42 +0000395 if( p->aCol==0 ){
396 p->nCol = 0;
397 return;
398 }
drh75897232000-05-29 14:26:00 +0000399 }
drh7020f652000-06-03 18:06:52 +0000400 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
401 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000402 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000403 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000404}
405
406/*
drh7020f652000-06-03 18:06:52 +0000407** The given token is the default value for the last column added to
408** the table currently under construction. If "minusFlag" is true, it
409** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000410**
411** This routine is called by the parser while in the middle of
412** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000413*/
414void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
415 Table *p;
416 int i;
417 char **pz;
418 if( (p = pParse->pNewTable)==0 ) return;
419 i = p->nCol-1;
420 pz = &p->aCol[i].zDflt;
421 if( minusFlag ){
422 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
423 }else{
424 sqliteSetNString(pz, pVal->z, pVal->n, 0);
425 }
426 sqliteDequote(*pz);
427}
428
429/*
drh50e5dad2001-09-15 00:57:28 +0000430** Come up with a new random value for the schema cookie. Make sure
431** the new value is different from the old.
432**
433** The schema cookie is used to determine when the schema for the
434** database changes. After each schema change, the cookie value
435** changes. When a process first reads the schema it records the
436** cookie. Thereafter, whenever it goes to access the database,
437** it checks the cookie to make sure the schema has not changed
438** since it was last read.
439**
440** This plan is not completely bullet-proof. It is possible for
441** the schema to change multiple times and for the cookie to be
442** set back to prior value. But schema changes are infrequent
443** and the probability of hitting the same cookie value is only
444** 1 chance in 2^32. So we're safe enough.
445*/
446static void changeCookie(sqlite *db){
447 if( db->next_cookie==db->schema_cookie ){
448 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
449 db->flags |= SQLITE_InternChanges;
450 }
451}
452
453/*
drh75897232000-05-29 14:26:00 +0000454** This routine is called to report the final ")" that terminates
455** a CREATE TABLE statement.
456**
457** The table structure is added to the internal hash tables.
458**
drh1ccde152000-06-17 13:12:39 +0000459** An entry for the table is made in the master table on disk,
460** unless initFlag==1. When initFlag==1, it means we are reading
461** the master table because we just connected to the database, so
drh75897232000-05-29 14:26:00 +0000462** the entry for this table already exists in the master table.
463** We do not want to create it again.
464*/
465void sqliteEndTable(Parse *pParse, Token *pEnd){
466 Table *p;
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 ){
drhbeae3192001-09-22 18:12:08 +0000476 sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
drh75897232000-05-29 14:26:00 +0000477 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000478 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000479 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000480 }
481
drhd78eeee2001-09-13 16:18:53 +0000482 /* If the initFlag is 1 it means we are reading the SQL off the
483 ** "sqlite_master" table on the disk. So do not write to the disk
drhe3c41372001-09-17 20:25:58 +0000484 ** again. Extract the root page number for the table from the
485 ** pParse->newTnum field. (The page number should have been put
486 ** there by the sqliteOpenCb routine.) If the table has a primary
487 ** key, the root page of the index associated with the primary key
488 ** should be in pParse->newKnum.
drhd78eeee2001-09-13 16:18:53 +0000489 */
490 if( pParse->initFlag ){
491 p->tnum = pParse->newTnum;
drhe3c41372001-09-17 20:25:58 +0000492 if( p->pIndex ){
493 p->pIndex->tnum = pParse->newKnum;
494 }
drhd78eeee2001-09-13 16:18:53 +0000495 }
496
drhe3c41372001-09-17 20:25:58 +0000497 /* If not initializing, then create a record for the new table
498 ** in the SQLITE_MASTER table of the database.
drh75897232000-05-29 14:26:00 +0000499 */
500 if( !pParse->initFlag ){
drh75897232000-05-29 14:26:00 +0000501 int n, base;
drhd8bc7082000-06-07 23:51:50 +0000502 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000503
drhd8bc7082000-06-07 23:51:50 +0000504 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000505 if( v==0 ) return;
506 n = (int)pEnd->z - (int)pParse->sFirstToken.z + 1;
drhecdc7532001-09-23 02:35:53 +0000507 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2, MASTER_NAME, 0);
drhe3c41372001-09-17 20:25:58 +0000508 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
509 sqliteVdbeAddOp(v, OP_String, 0, 0, "table", 0);
510 sqliteVdbeAddOp(v, OP_String, 0, 0, p->zName, 0);
511 sqliteVdbeAddOp(v, OP_String, 0, 0, p->zName, 0);
512 sqliteVdbeAddOp(v, OP_CreateTable, 0, 0, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000513 sqliteVdbeTableRootAddr(v, &p->tnum);
drh5edc3122001-09-13 21:53:09 +0000514 if( p->pIndex ){
515 /* If the table has a primary key, create an index in the database
drhe3c41372001-09-17 20:25:58 +0000516 ** for that key and record the root page of the index in the "knum"
517 ** column of of the SQLITE_MASTER table.
518 */
drh5edc3122001-09-13 21:53:09 +0000519 Index *pIndex = p->pIndex;
520 assert( pIndex->pNext==0 );
521 assert( pIndex->tnum==0 );
522 sqliteVdbeAddOp(v, OP_CreateIndex, 0, 0, 0, 0),
523 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
drhe3c41372001-09-17 20:25:58 +0000524 }else{
525 /* If the table does not have a primary key, the "knum" column is
526 ** fill with a NULL value.
527 */
528 sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0);
drh5edc3122001-09-13 21:53:09 +0000529 }
drhe3c41372001-09-17 20:25:58 +0000530 base = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
531 sqliteVdbeChangeP3(v, base, pParse->sFirstToken.z, n);
532 sqliteVdbeAddOp(v, OP_MakeRecord, 6, 0, 0, 0);
533 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
534 changeCookie(db);
535 sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0, 0, 0);
536 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drhbe0072d2001-09-13 14:46:09 +0000537 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000538 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
539 }
drh75897232000-05-29 14:26:00 +0000540 }
541}
542
543/*
544** Given a token, look up a table with that name. If not found, leave
545** an error for the parser to find and return NULL.
546*/
drhcce7d172000-05-31 15:34:51 +0000547Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +0000548 char *zName;
549 Table *pTab;
550 zName = sqliteTableNameFromToken(pTok);
551 if( zName==0 ) return 0;
552 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +0000553 sqliteFree(zName);
554 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +0000555 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
556 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +0000557 pParse->nErr++;
558 }
559 return pTab;
560}
561
562/*
563** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +0000564** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +0000565*/
566void sqliteDropTable(Parse *pParse, Token *pName){
567 Table *pTable;
drh75897232000-05-29 14:26:00 +0000568 Vdbe *v;
569 int base;
drh5edc3122001-09-13 21:53:09 +0000570 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000571
drhdaffd0e2001-04-11 14:28:42 +0000572 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000573 pTable = sqliteTableFromToken(pParse, pName);
574 if( pTable==0 ) return;
575 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +0000576 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
577 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +0000578 pParse->nErr++;
579 return;
580 }
581
drh1ccde152000-06-17 13:12:39 +0000582 /* Generate code to remove the table from the master table
583 ** on disk.
584 */
drhd8bc7082000-06-07 23:51:50 +0000585 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000586 if( v ){
587 static VdbeOp dropTable[] = {
drhecdc7532001-09-23 02:35:53 +0000588 { OP_OpenWrite, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000589 { OP_Rewind, 0, 0, 0},
590 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000591 { OP_Next, 0, ADDR(9), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000592 { OP_Dup, 0, 0, 0},
drhe3c41372001-09-17 20:25:58 +0000593 { OP_Column, 0, 2, 0},
drhd78eeee2001-09-13 16:18:53 +0000594 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000595 { OP_Delete, 0, 0, 0},
drhd78eeee2001-09-13 16:18:53 +0000596 { OP_Goto, 0, ADDR(3), 0},
drh5edc3122001-09-13 21:53:09 +0000597 { OP_Destroy, 0, 0, 0}, /* 9 */
drh50e5dad2001-09-15 00:57:28 +0000598 { OP_SetCookie, 0, 0, 0}, /* 10 */
drh75897232000-05-29 14:26:00 +0000599 { OP_Close, 0, 0, 0},
600 };
601 Index *pIdx;
drh5edc3122001-09-13 21:53:09 +0000602 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000603 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000604 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000605 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000606 }
drh75897232000-05-29 14:26:00 +0000607 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh5edc3122001-09-13 21:53:09 +0000608 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
609 sqliteVdbeChangeP1(v, base+9, pTable->tnum);
drh50e5dad2001-09-15 00:57:28 +0000610 changeCookie(db);
611 sqliteVdbeChangeP1(v, base+10, db->next_cookie);
drh75897232000-05-29 14:26:00 +0000612 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drh5e00f6c2001-09-13 13:46:56 +0000613 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, 0, 0, 0);
614 }
drh5edc3122001-09-13 21:53:09 +0000615 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000616 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000617 }
618 }
619
drh5e00f6c2001-09-13 13:46:56 +0000620 /* Mark the in-memory Table structure as being deleted. The actually
621 ** deletion occurs inside of sqliteCommitInternalChanges().
drh75897232000-05-29 14:26:00 +0000622 **
623 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +0000624 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +0000625 */
626 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000627 pTable->isDelete = 1;
drh5edc3122001-09-13 21:53:09 +0000628 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000629 }
630}
631
632/*
633** Create a new index for an SQL table. pIndex is the name of the index
634** and pTable is the name of the table that is to be indexed. Both will
635** be NULL for a primary key. In that case, use pParse->pNewTable as the
636** table to be indexed.
637**
drh1ccde152000-06-17 13:12:39 +0000638** pList is a list of columns to be indexed. pList will be NULL if the
639** most recently added column of the table is labeled as the primary key.
drh75897232000-05-29 14:26:00 +0000640*/
641void sqliteCreateIndex(
642 Parse *pParse, /* All information about this parse */
643 Token *pName, /* Name of the index. May be NULL */
644 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +0000645 IdList *pList, /* A list of columns to be indexed */
drh75897232000-05-29 14:26:00 +0000646 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
647 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
648){
649 Table *pTab; /* Table to be indexed */
650 Index *pIndex; /* The index to be created */
651 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +0000652 int i, j;
drh75897232000-05-29 14:26:00 +0000653 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +0000654 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000655
drhdaffd0e2001-04-11 14:28:42 +0000656 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
657
drh75897232000-05-29 14:26:00 +0000658 /*
659 ** Find the table that is to be indexed. Return early if not found.
660 */
661 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +0000662 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +0000663 pTab = sqliteTableFromToken(pParse, pTable);
664 }else{
drhe3c41372001-09-17 20:25:58 +0000665 assert( pName==0 );
drh75897232000-05-29 14:26:00 +0000666 pTab = pParse->pNewTable;
667 }
668 if( pTab==0 || pParse->nErr ) goto exit_create_index;
669 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +0000670 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
671 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +0000672 pParse->nErr++;
673 goto exit_create_index;
674 }
675
676 /*
677 ** Find the name of the index. Make sure there is not already another
drhe3c41372001-09-17 20:25:58 +0000678 ** index or table with the same name. If pName==0 it means that we are
679 ** dealing with a primary key, which has no name, so this step can be
680 ** skipped.
drh75897232000-05-29 14:26:00 +0000681 */
682 if( pName ){
683 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +0000684 if( zName==0 ) goto exit_create_index;
685 if( sqliteFindIndex(db, zName) ){
686 sqliteSetString(&pParse->zErrMsg, "index ", zName,
687 " already exists", 0);
688 pParse->nErr++;
689 goto exit_create_index;
690 }
691 if( sqliteFindTable(db, zName) ){
692 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
693 zName, 0);
694 pParse->nErr++;
695 goto exit_create_index;
696 }
drh75897232000-05-29 14:26:00 +0000697 }else{
698 zName = 0;
drhe3c41372001-09-17 20:25:58 +0000699 sqliteSetString(&zName, pTab->zName, " (primary key)", 0);
700 if( zName==0 ) goto exit_create_index;
drh75897232000-05-29 14:26:00 +0000701 }
702
703 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +0000704 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +0000705 ** So create a fake list to simulate this.
706 */
707 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +0000708 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +0000709 nullId.n = strlen(nullId.z);
710 pList = sqliteIdListAppend(0, &nullId);
711 if( pList==0 ) goto exit_create_index;
712 }
713
714 /*
715 ** Allocate the index structure.
716 */
drhdcc581c2000-05-30 13:44:19 +0000717 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +0000718 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +0000719 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +0000720 pIndex->aiColumn = (int*)&pIndex[1];
721 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +0000722 strcpy(pIndex->zName, zName);
723 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +0000724 pIndex->nColumn = pList->nId;
drh75897232000-05-29 14:26:00 +0000725
drh1ccde152000-06-17 13:12:39 +0000726 /* Scan the names of the columns of the table to be indexed and
727 ** load the column indices into the Index structure. Report an error
728 ** if any column is not found.
drh75897232000-05-29 14:26:00 +0000729 */
730 for(i=0; i<pList->nId; i++){
731 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000732 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +0000733 }
734 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +0000735 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +0000736 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +0000737 pParse->nErr++;
738 sqliteFree(pIndex);
739 goto exit_create_index;
740 }
drh967e8b72000-06-21 13:59:10 +0000741 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +0000742 }
743
744 /* Link the new Index structure to its table and to the other
drhe3c41372001-09-17 20:25:58 +0000745 ** in-memory database structures. Note that primary key indices
746 ** do not appear in the index hash table.
drh75897232000-05-29 14:26:00 +0000747 */
748 if( pParse->explain==0 ){
drhe3c41372001-09-17 20:25:58 +0000749 if( pName!=0 ){
drhbeae3192001-09-22 18:12:08 +0000750 char *zName = pIndex->zName;;
751 sqliteHashInsert(&db->idxHash, zName, strlen(zName)+1, pIndex);
drhe3c41372001-09-17 20:25:58 +0000752 }
drh75897232000-05-29 14:26:00 +0000753 pIndex->pNext = pTab->pIndex;
754 pTab->pIndex = pIndex;
drh5e00f6c2001-09-13 13:46:56 +0000755 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000756 }
757
drhd78eeee2001-09-13 16:18:53 +0000758 /* If the initFlag is 1 it means we are reading the SQL off the
759 ** "sqlite_master" table on the disk. So do not write to the disk
760 ** again. Extract the table number from the pParse->newTnum field.
761 */
762 if( pParse->initFlag ){
763 pIndex->tnum = pParse->newTnum;
764 }
765
drh75897232000-05-29 14:26:00 +0000766 /* If the initFlag is 0 then create the index on disk. This
767 ** involves writing the index into the master table and filling in the
768 ** index with the current table contents.
769 **
770 ** The initFlag is 0 when the user first enters a CREATE INDEX
771 ** command. The initFlag is 1 when a database is opened and
772 ** CREATE INDEX statements are read out of the master table. In
773 ** the latter case the index already exists on disk, which is why
774 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +0000775 **
776 ** If pTable==0 it means this index is generated as a primary key
777 ** and those does not have a CREATE INDEX statement to add to the
778 ** master table. Also, since primary keys are created at the same
779 ** time as tables, the table will be empty so there is no need to
780 ** initialize the index. Hence, skip all the code generation if
781 ** pTable==0.
drh75897232000-05-29 14:26:00 +0000782 */
drh5edc3122001-09-13 21:53:09 +0000783 else if( pParse->initFlag==0 && pTable!=0 ){
drh75897232000-05-29 14:26:00 +0000784 static VdbeOp addTable[] = {
drhecdc7532001-09-23 02:35:53 +0000785 { OP_OpenWrite, 2, 2, MASTER_NAME},
drh5e00f6c2001-09-13 13:46:56 +0000786 { OP_NewRecno, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000787 { OP_String, 0, 0, "index"},
drh75897232000-05-29 14:26:00 +0000788 { OP_String, 0, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +0000789 { OP_String, 0, 0, 0}, /* 4 */
drh5edc3122001-09-13 21:53:09 +0000790 { OP_CreateIndex, 1, 0, 0},
791 { OP_Dup, 0, 0, 0},
drhecdc7532001-09-23 02:35:53 +0000792 { OP_OpenWrite, 1, 0, 0}, /* 7 */
drhe3c41372001-09-17 20:25:58 +0000793 { OP_Null, 0, 0, 0},
794 { OP_String, 0, 0, 0}, /* 9 */
795 { OP_MakeRecord, 6, 0, 0},
drhbed86902000-06-02 13:27:59 +0000796 { OP_Put, 2, 0, 0},
drhe3c41372001-09-17 20:25:58 +0000797 { OP_SetCookie, 0, 0, 0}, /* 12 */
drhbed86902000-06-02 13:27:59 +0000798 { OP_Close, 2, 0, 0},
drh75897232000-05-29 14:26:00 +0000799 };
800 int n;
801 Vdbe *v = pParse->pVdbe;
802 int lbl1, lbl2;
803 int i;
804
drhd8bc7082000-06-07 23:51:50 +0000805 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000806 if( v==0 ) goto exit_create_index;
drhbe0072d2001-09-13 14:46:09 +0000807 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000808 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000809 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000810 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000811 }
drh75897232000-05-29 14:26:00 +0000812 if( pStart && pEnd ){
813 int base;
814 n = (int)pEnd->z - (int)pStart->z + 1;
815 base = sqliteVdbeAddOpList(v, ArraySize(addTable), addTable);
drhb24fcbe2000-05-29 23:30:50 +0000816 sqliteVdbeChangeP3(v, base+3, pIndex->zName, 0);
drhe3c41372001-09-17 20:25:58 +0000817 sqliteVdbeChangeP3(v, base+4, pTab->zName, 0);
drh5e00f6c2001-09-13 13:46:56 +0000818 sqliteVdbeIndexRootAddr(v, &pIndex->tnum);
drhe3c41372001-09-17 20:25:58 +0000819 sqliteVdbeChangeP3(v, base+7, pIndex->zName, 0);
820 sqliteVdbeChangeP3(v, base+9, pStart->z, n);
drh50e5dad2001-09-15 00:57:28 +0000821 changeCookie(db);
drhe3c41372001-09-17 20:25:58 +0000822 sqliteVdbeChangeP1(v, base+12, db->next_cookie);
drh75897232000-05-29 14:26:00 +0000823 }
drh5edc3122001-09-13 21:53:09 +0000824 sqliteVdbeAddOp(v, OP_Open, 0, pTab->tnum, pTab->zName, 0);
drh75897232000-05-29 14:26:00 +0000825 lbl1 = sqliteVdbeMakeLabel(v);
826 lbl2 = sqliteVdbeMakeLabel(v);
drhd78eeee2001-09-13 16:18:53 +0000827 sqliteVdbeAddOp(v, OP_Rewind, 0, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000828 sqliteVdbeAddOp(v, OP_Next, 0, lbl2, 0, lbl1);
drhbe0072d2001-09-13 14:46:09 +0000829 sqliteVdbeAddOp(v, OP_Recno, 0, 0, 0, 0);
drh967e8b72000-06-21 13:59:10 +0000830 for(i=0; i<pIndex->nColumn; i++){
drh5e00f6c2001-09-13 13:46:56 +0000831 sqliteVdbeAddOp(v, OP_Column, 0, pIndex->aiColumn[i], 0, 0);
drh75897232000-05-29 14:26:00 +0000832 }
drh5e00f6c2001-09-13 13:46:56 +0000833 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0, 0, 0);
drh75897232000-05-29 14:26:00 +0000834 sqliteVdbeAddOp(v, OP_PutIdx, 1, 0, 0, 0);
835 sqliteVdbeAddOp(v, OP_Goto, 0, lbl1, 0, 0);
836 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, lbl2);
drh75897232000-05-29 14:26:00 +0000837 sqliteVdbeAddOp(v, OP_Close, 1, 0, 0, 0);
drhbed86902000-06-02 13:27:59 +0000838 sqliteVdbeAddOp(v, OP_Close, 0, 0, 0, 0);
drhbe0072d2001-09-13 14:46:09 +0000839 if( pTable!=0 && (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000840 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
841 }
drh75897232000-05-29 14:26:00 +0000842 }
843
844 /* Reclaim memory on an EXPLAIN call.
845 */
846 if( pParse->explain ){
847 sqliteFree(pIndex);
848 }
849
850 /* Clean up before exiting */
851exit_create_index:
852 sqliteIdListDelete(pList);
853 sqliteFree(zName);
854 return;
855}
856
857/*
858** This routine will drop an existing named index.
859*/
860void sqliteDropIndex(Parse *pParse, Token *pName){
861 Index *pIndex;
862 char *zName;
863 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +0000864 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000865
drhdaffd0e2001-04-11 14:28:42 +0000866 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000867 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000868 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +0000869 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +0000870 sqliteFree(zName);
871 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +0000872 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
873 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +0000874 pParse->nErr++;
875 return;
876 }
877
878 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +0000879 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000880 if( v ){
881 static VdbeOp dropIndex[] = {
drhecdc7532001-09-23 02:35:53 +0000882 { OP_OpenWrite, 0, 2, MASTER_NAME},
drhd78eeee2001-09-13 16:18:53 +0000883 { OP_Rewind, 0, 0, 0},
884 { OP_String, 0, 0, 0}, /* 2 */
drh5edc3122001-09-13 21:53:09 +0000885 { OP_Next, 0, ADDR(8), 0}, /* 3 */
drh75897232000-05-29 14:26:00 +0000886 { OP_Dup, 0, 0, 0},
drh5e00f6c2001-09-13 13:46:56 +0000887 { OP_Column, 0, 1, 0},
drhd78eeee2001-09-13 16:18:53 +0000888 { OP_Ne, 0, ADDR(3), 0},
drh75897232000-05-29 14:26:00 +0000889 { OP_Delete, 0, 0, 0},
drh5edc3122001-09-13 21:53:09 +0000890 { OP_Destroy, 0, 0, 0}, /* 8 */
drh50e5dad2001-09-15 00:57:28 +0000891 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000892 { OP_Close, 0, 0, 0},
893 };
894 int base;
895
drhbe0072d2001-09-13 14:46:09 +0000896 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000897 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000898 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +0000899 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +0000900 }
drh75897232000-05-29 14:26:00 +0000901 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh3fc190c2001-09-14 03:24:23 +0000902 sqliteVdbeChangeP3(v, base+2, pIndex->zName, 0);
drh5edc3122001-09-13 21:53:09 +0000903 sqliteVdbeChangeP1(v, base+8, pIndex->tnum);
drh50e5dad2001-09-15 00:57:28 +0000904 changeCookie(db);
905 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
drhbe0072d2001-09-13 14:46:09 +0000906 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +0000907 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
908 }
drh75897232000-05-29 14:26:00 +0000909 }
910
drh5e00f6c2001-09-13 13:46:56 +0000911 /* Mark the internal Index structure for deletion by the
912 ** sqliteCommitInternalChanges routine.
drh75897232000-05-29 14:26:00 +0000913 */
914 if( !pParse->explain ){
drh5e00f6c2001-09-13 13:46:56 +0000915 pIndex->isDelete = 1;
916 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000917 }
918}
919
920/*
921** Add a new element to the end of an expression list. If pList is
922** initially NULL, then create a new expression list.
923*/
924ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
925 int i;
926 if( pList==0 ){
927 pList = sqliteMalloc( sizeof(ExprList) );
drhdaffd0e2001-04-11 14:28:42 +0000928 if( pList==0 ) return 0;
drh75897232000-05-29 14:26:00 +0000929 }
drh75897232000-05-29 14:26:00 +0000930 if( (pList->nExpr & 7)==0 ){
931 int n = pList->nExpr + 8;
932 pList->a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
933 if( pList->a==0 ){
934 pList->nExpr = 0;
935 return pList;
936 }
937 }
938 i = pList->nExpr++;
939 pList->a[i].pExpr = pExpr;
940 pList->a[i].zName = 0;
941 if( pName ){
942 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000943 sqliteDequote(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +0000944 }
945 return pList;
946}
947
948/*
949** Delete an entire expression list.
950*/
951void sqliteExprListDelete(ExprList *pList){
952 int i;
953 if( pList==0 ) return;
954 for(i=0; i<pList->nExpr; i++){
955 sqliteExprDelete(pList->a[i].pExpr);
956 sqliteFree(pList->a[i].zName);
957 }
958 sqliteFree(pList->a);
959 sqliteFree(pList);
960}
961
962/*
963** Append a new element to the given IdList. Create a new IdList if
964** need be.
drhdaffd0e2001-04-11 14:28:42 +0000965**
966** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +0000967*/
968IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
969 if( pList==0 ){
970 pList = sqliteMalloc( sizeof(IdList) );
971 if( pList==0 ) return 0;
972 }
973 if( (pList->nId & 7)==0 ){
974 pList->a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
975 if( pList->a==0 ){
976 pList->nId = 0;
drhdaffd0e2001-04-11 14:28:42 +0000977 sqliteIdListDelete(pList);
978 return 0;
drh75897232000-05-29 14:26:00 +0000979 }
980 }
981 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
982 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +0000983 char **pz = &pList->a[pList->nId].zName;
984 sqliteSetNString(pz, pToken->z, pToken->n, 0);
985 if( *pz==0 ){
986 sqliteIdListDelete(pList);
987 return 0;
988 }else{
989 sqliteDequote(*pz);
990 }
drh75897232000-05-29 14:26:00 +0000991 }
992 pList->nId++;
993 return pList;
994}
995
996/*
997** Add an alias to the last identifier on the given identifier list.
998*/
999void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1000 if( pList && pList->nId>0 ){
1001 int i = pList->nId - 1;
1002 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001003 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001004 }
1005}
1006
1007/*
1008** Delete an entire IdList
1009*/
1010void sqliteIdListDelete(IdList *pList){
1011 int i;
1012 if( pList==0 ) return;
1013 for(i=0; i<pList->nId; i++){
1014 sqliteFree(pList->a[i].zName);
1015 sqliteFree(pList->a[i].zAlias);
drhdaffd0e2001-04-11 14:28:42 +00001016 if( pList->a[i].pSelect ){
1017 sqliteFree(pList->a[i].zName);
1018 sqliteSelectDelete(pList->a[i].pSelect);
1019 sqliteDeleteTable(0, pList->a[i].pTab);
1020 }
drh75897232000-05-29 14:26:00 +00001021 }
1022 sqliteFree(pList->a);
1023 sqliteFree(pList);
1024}
1025
drh982cef72000-05-30 16:27:03 +00001026
1027/*
1028** The COPY command is for compatibility with PostgreSQL and specificially
1029** for the ability to read the output of pg_dump. The format is as
1030** follows:
1031**
1032** COPY table FROM file [USING DELIMITERS string]
1033**
1034** "table" is an existing table name. We will read lines of code from
1035** file to fill this table with data. File might be "stdin". The optional
1036** delimiter string identifies the field separators. The default is a tab.
1037*/
1038void sqliteCopy(
1039 Parse *pParse, /* The parser context */
1040 Token *pTableName, /* The name of the table into which we will insert */
1041 Token *pFilename, /* The file from which to obtain information */
1042 Token *pDelimiter /* Use this as the field delimiter */
1043){
1044 Table *pTab;
1045 char *zTab;
1046 int i, j;
1047 Vdbe *v;
1048 int addr, end;
1049 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001050 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001051
1052 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001053 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001054 pTab = sqliteFindTable(db, zTab);
drh982cef72000-05-30 16:27:03 +00001055 sqliteFree(zTab);
1056 if( pTab==0 ){
1057 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1058 pTableName->z, pTableName->n, 0);
1059 pParse->nErr++;
1060 goto copy_cleanup;
1061 }
1062 if( pTab->readOnly ){
1063 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1064 " may not be modified", 0);
1065 pParse->nErr++;
1066 goto copy_cleanup;
1067 }
drhd8bc7082000-06-07 23:51:50 +00001068 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001069 if( v ){
drhbe0072d2001-09-13 14:46:09 +00001070 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001071 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001072 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +00001073 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +00001074 }
drh982cef72000-05-30 16:27:03 +00001075 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0, 0, 0);
1076 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001077 sqliteVdbeDequoteP3(v, addr);
drhecdc7532001-09-23 02:35:53 +00001078 sqliteVdbeAddOp(v, OP_OpenWrite, 0, pTab->tnum, pTab->zName, 0);
drh982cef72000-05-30 16:27:03 +00001079 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drhecdc7532001-09-23 02:35:53 +00001080 sqliteVdbeAddOp(v, OP_OpenWrite, i, pIdx->tnum, pIdx->zName, 0);
drh982cef72000-05-30 16:27:03 +00001081 }
1082 end = sqliteVdbeMakeLabel(v);
1083 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end, 0, 0);
1084 if( pDelimiter ){
1085 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1086 sqliteVdbeDequoteP3(v, addr);
1087 }else{
1088 sqliteVdbeChangeP3(v, addr, "\t", 1);
1089 }
drh5e00f6c2001-09-13 13:46:56 +00001090 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001091 if( pTab->pIndex ){
1092 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1093 }
1094 for(i=0; i<pTab->nCol; i++){
drh5e00f6c2001-09-13 13:46:56 +00001095 sqliteVdbeAddOp(v, OP_FileColumn, i, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001096 }
1097 sqliteVdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0, 0, 0);
1098 sqliteVdbeAddOp(v, OP_Put, 0, 0, 0, 0);
1099 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
1100 if( pIdx->pNext ){
1101 sqliteVdbeAddOp(v, OP_Dup, 0, 0, 0, 0);
1102 }
drh967e8b72000-06-21 13:59:10 +00001103 for(j=0; j<pIdx->nColumn; j++){
drh5e00f6c2001-09-13 13:46:56 +00001104 sqliteVdbeAddOp(v, OP_FileColumn, pIdx->aiColumn[j], 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001105 }
drh5e00f6c2001-09-13 13:46:56 +00001106 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIdx->nColumn, 0, 0, 0);
drh982cef72000-05-30 16:27:03 +00001107 sqliteVdbeAddOp(v, OP_PutIdx, i, 0, 0, 0);
1108 }
1109 sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0);
1110 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, end);
drhbe0072d2001-09-13 14:46:09 +00001111 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001112 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1113 }
drh982cef72000-05-30 16:27:03 +00001114 }
1115
1116copy_cleanup:
1117 return;
1118}
drhdce2cbe2000-05-31 02:27:49 +00001119
1120/*
1121** The non-standard VACUUM command is used to clean up the database,
1122** collapse free space, etc. It is modelled after the VACUUM command
1123** in PostgreSQL.
1124*/
1125void sqliteVacuum(Parse *pParse, Token *pTableName){
1126 char *zName;
1127 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001128 sqlite *db = pParse->db;
drhdce2cbe2000-05-31 02:27:49 +00001129
drhdaffd0e2001-04-11 14:28:42 +00001130 if( pParse->nErr || sqlite_malloc_failed ) return;
drhdce2cbe2000-05-31 02:27:49 +00001131 if( pTableName ){
1132 zName = sqliteTableNameFromToken(pTableName);
1133 }else{
1134 zName = 0;
1135 }
drhbe0072d2001-09-13 14:46:09 +00001136 if( zName && sqliteFindIndex(db, zName)==0
1137 && sqliteFindTable(db, zName)==0 ){
drhdce2cbe2000-05-31 02:27:49 +00001138 sqliteSetString(&pParse->zErrMsg, "no such table or index: ", zName, 0);
1139 pParse->nErr++;
1140 goto vacuum_cleanup;
1141 }
drhd8bc7082000-06-07 23:51:50 +00001142 v = sqliteGetVdbe(pParse);
drhdce2cbe2000-05-31 02:27:49 +00001143 if( v==0 ) goto vacuum_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001144 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001145 sqliteVdbeAddOp(v, OP_Transaction, 0, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001146 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +00001147 pParse->schemaVerified = 1;
drh5e00f6c2001-09-13 13:46:56 +00001148 }
drhdce2cbe2000-05-31 02:27:49 +00001149 if( zName ){
1150 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, zName, 0);
1151 }else{
drhdce2cbe2000-05-31 02:27:49 +00001152 Table *pTab;
1153 Index *pIdx;
drhbeae3192001-09-22 18:12:08 +00001154 HashElem *pE;
1155 for(pE=sqliteHashFirst(&db->tblHash); pE; pE=sqliteHashNext(pE)){
1156 pTab = sqliteHashData(pE);
1157 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pTab->zName, 0);
1158 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1159 sqliteVdbeAddOp(v, OP_Reorganize, 0, 0, pIdx->zName, 0);
drhdce2cbe2000-05-31 02:27:49 +00001160 }
1161 }
1162 }
drhbe0072d2001-09-13 14:46:09 +00001163 if( (db->flags & SQLITE_InTrans)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00001164 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
1165 }
drhdce2cbe2000-05-31 02:27:49 +00001166
1167vacuum_cleanup:
1168 sqliteFree(zName);
1169 return;
1170}
drhc4a3c772001-04-04 11:48:57 +00001171
1172/*
1173** Begin a transaction
1174*/
1175void sqliteBeginTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001176 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001177 Vdbe *v;
1178
drhc4a3c772001-04-04 11:48:57 +00001179 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001180 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001181 if( db->flags & SQLITE_InTrans ) return;
drh5e00f6c2001-09-13 13:46:56 +00001182 v = sqliteGetVdbe(pParse);
1183 if( v ){
1184 sqliteVdbeAddOp(v, OP_Transaction, 1, 0, 0, 0);
drh50e5dad2001-09-15 00:57:28 +00001185 sqliteVdbeAddOp(v, OP_VerifyCookie, db->schema_cookie, 0, 0, 0);
drhecdc7532001-09-23 02:35:53 +00001186 pParse->schemaVerified = 1;
drhc4a3c772001-04-04 11:48:57 +00001187 }
drh5e00f6c2001-09-13 13:46:56 +00001188 db->flags |= SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001189}
1190
1191/*
1192** Commit a transaction
1193*/
1194void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001195 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001196 Vdbe *v;
1197
drhc4a3c772001-04-04 11:48:57 +00001198 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001199 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001200 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001201 v = sqliteGetVdbe(pParse);
1202 if( v ){
1203 sqliteVdbeAddOp(v, OP_Commit, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001204 }
drh5e00f6c2001-09-13 13:46:56 +00001205 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001206}
1207
1208/*
1209** Rollback a transaction
1210*/
1211void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001212 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001213 Vdbe *v;
1214
drhc4a3c772001-04-04 11:48:57 +00001215 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001216 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001217 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001218 v = sqliteGetVdbe(pParse);
1219 if( v ){
1220 sqliteVdbeAddOp(v, OP_Rollback, 0, 0, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001221 }
drh5e00f6c2001-09-13 13:46:56 +00001222 db->flags &= ~SQLITE_InTrans;
drhc4a3c772001-04-04 11:48:57 +00001223}
drhf57b14a2001-09-14 18:54:08 +00001224
1225/*
1226** Interpret the given string as a boolean value.
1227*/
1228static int getBoolean(char *z){
1229 static char *azTrue[] = { "yes", "on", "true" };
1230 int i;
1231 if( z[0]==0 ) return 0;
1232 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1233 return atoi(z);
1234 }
1235 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1236 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1237 }
1238 return 0;
1239}
1240
1241/*
1242** Process a pragma statement.
1243**
1244** Pragmas are of this form:
1245**
1246** PRAGMA id = value
1247**
1248** The identifier might also be a string. The value is a string, and
1249** identifier, or a number. If minusFlag is true, then the value is
1250** a number that was preceded by a minus sign.
1251*/
1252void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1253 char *zLeft = 0;
1254 char *zRight = 0;
1255 sqlite *db = pParse->db;
1256
1257 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1258 sqliteDequote(zLeft);
1259 if( minusFlag ){
1260 zRight = 0;
1261 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1262 }else{
1263 zRight = sqliteStrNDup(pRight->z, pRight->n);
1264 sqliteDequote(zRight);
1265 }
1266
1267 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1268 int size = atoi(zRight);
1269 sqliteBtreeSetCacheSize(db->pBe, size);
1270 }else
1271
1272 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1273 if( getBoolean(zRight) ){
1274 db->flags |= SQLITE_VdbeTrace;
1275 }else{
1276 db->flags &= ~SQLITE_VdbeTrace;
1277 }
1278 }else
1279
1280#ifndef NDEBUG
1281 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
1282 extern void sqliteParserTrace(FILE*, char *);
1283 if( getBoolean(zRight) ){
1284 sqliteParserTrace(stdout, "parser: ");
1285 }else{
1286 sqliteParserTrace(0, 0);
1287 }
1288 }else
1289#endif
1290
1291 if( zLeft ) sqliteFree(zLeft);
1292 if( zRight ) sqliteFree(zRight);
1293}