blob: c7159f0d6cfc4dc1d2a0f9dd3d6f31e83c987edb [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**
drh1bffb9c2002-02-03 17:37:36 +000028** $Id: build.c,v 1.74 2002/02/03 17:37:36 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/*
drhf57b3392001-10-08 13:22:32 +000068** Construct a new expression node and return a pointer to it. Memory
69** for this node is obtained from sqliteMalloc(). The calling function
70** is responsible for making sure the node eventually gets freed.
drh75897232000-05-29 14:26:00 +000071*/
72Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
73 Expr *pNew;
74 pNew = sqliteMalloc( sizeof(Expr) );
drh6d4abfb2001-10-22 02:58:08 +000075 if( pNew==0 ){
76 sqliteExprDelete(pLeft);
77 sqliteExprDelete(pRight);
78 return 0;
79 }
drh75897232000-05-29 14:26:00 +000080 pNew->op = op;
81 pNew->pLeft = pLeft;
82 pNew->pRight = pRight;
83 if( pToken ){
84 pNew->token = *pToken;
85 }else{
86 pNew->token.z = "";
87 pNew->token.n = 0;
88 }
drhe1b6a5b2000-07-29 13:06:59 +000089 if( pLeft && pRight ){
90 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
91 }else{
92 pNew->span = pNew->token;
93 }
drh75897232000-05-29 14:26:00 +000094 return pNew;
95}
96
97/*
drhe1b6a5b2000-07-29 13:06:59 +000098** Set the Expr.token field of the given expression to span all
99** text between the two given tokens.
100*/
101void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drhdaffd0e2001-04-11 14:28:42 +0000102 if( pExpr ){
103 pExpr->span.z = pLeft->z;
drh5a2c2c22001-11-21 02:21:11 +0000104 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drhdaffd0e2001-04-11 14:28:42 +0000105 }
drhe1b6a5b2000-07-29 13:06:59 +0000106}
107
108/*
drh75897232000-05-29 14:26:00 +0000109** Construct a new expression node for a function with multiple
110** arguments.
111*/
112Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
113 Expr *pNew;
114 pNew = sqliteMalloc( sizeof(Expr) );
drh6d4abfb2001-10-22 02:58:08 +0000115 if( pNew==0 ){
116 sqliteExprListDelete(pList);
117 return 0;
118 }
drh75897232000-05-29 14:26:00 +0000119 pNew->op = TK_FUNCTION;
120 pNew->pList = pList;
121 if( pToken ){
122 pNew->token = *pToken;
123 }else{
124 pNew->token.z = "";
125 pNew->token.n = 0;
126 }
127 return pNew;
128}
129
130/*
drhf57b3392001-10-08 13:22:32 +0000131** Locate the in-memory structure that describes
132** a particular database table given the name
drh75897232000-05-29 14:26:00 +0000133** of that table. Return NULL if not found.
134*/
135Table *sqliteFindTable(sqlite *db, char *zName){
drhafa4a022001-09-24 03:12:39 +0000136 Table *p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +0000137 return p;
drh75897232000-05-29 14:26:00 +0000138}
139
140/*
drhf57b3392001-10-08 13:22:32 +0000141** Locate the in-memory structure that describes
142** a particular index given the name of that index.
143** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000144*/
145Index *sqliteFindIndex(sqlite *db, char *zName){
drhafa4a022001-09-24 03:12:39 +0000146 Index *p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +0000147 return p;
drh75897232000-05-29 14:26:00 +0000148}
149
150/*
151** Remove the given index from the index hash table, and free
152** its memory structures.
153**
drhd229ca92002-01-09 13:30:41 +0000154** The index is removed from the database hash tables but
155** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000156** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000157*/
drh74e24cd2002-01-09 03:19:59 +0000158static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000159 Index *pOld;
160 assert( db!=0 && p->zName!=0 );
161 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
162 if( pOld!=0 && pOld!=p ){
163 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000164 }
drhd229ca92002-01-09 13:30:41 +0000165 sqliteHashInsert(&db->idxDrop, p, 0, 0);
drh74e24cd2002-01-09 03:19:59 +0000166 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000167}
168
169/*
drhbeae3192001-09-22 18:12:08 +0000170** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000171** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000172** structures.
173*/
drh6d4abfb2001-10-22 02:58:08 +0000174void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000175 if( pIndex->pTable->pIndex==pIndex ){
176 pIndex->pTable->pIndex = pIndex->pNext;
177 }else{
178 Index *p;
179 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
180 if( p && p->pNext==pIndex ){
181 p->pNext = pIndex->pNext;
182 }
183 }
184 sqliteDeleteIndex(db, pIndex);
185}
186
187/*
drh74e24cd2002-01-09 03:19:59 +0000188** Move the given index to the pending DROP INDEX queue if it has
189** been committed. If this index was never committed, then just
190** delete it.
191**
192** Indices on the pending drop queue are deleted when a COMMIT is
193** executed. If a ROLLBACK occurs, the indices are moved back into
194** the main index hash table.
195*/
drhda9e0342002-01-10 14:31:48 +0000196static void sqlitePendingDropIndex(sqlite *db, Index *p){
drh74e24cd2002-01-09 03:19:59 +0000197 if( !p->isCommit ){
198 sqliteUnlinkAndDeleteIndex(db, p);
199 }else{
200 Index *pOld;
201 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000202 if( pOld!=0 && pOld!=p ){
203 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
204 }
drh74e24cd2002-01-09 03:19:59 +0000205 sqliteHashInsert(&db->idxDrop, p, 0, p);
206 p->isDropped = 1;
207 }
208}
209
210/*
drh75897232000-05-29 14:26:00 +0000211** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000212** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000213**
214** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000215** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000216** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000217**
218** Indices associated with the table are unlinked from the "db"
219** data structure if db!=NULL. If db==NULL, indices attached to
220** the table are deleted, but it is assumed they have already been
221** unlinked.
drh75897232000-05-29 14:26:00 +0000222*/
223void sqliteDeleteTable(sqlite *db, Table *pTable){
224 int i;
225 Index *pIndex, *pNext;
226 if( pTable==0 ) return;
227 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000228 sqliteFree(pTable->aCol[i].zName);
229 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000230 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000231 }
232 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
233 pNext = pIndex->pNext;
234 sqliteDeleteIndex(db, pIndex);
235 }
drh6e142f52000-06-08 13:36:40 +0000236 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000237 sqliteFree(pTable->aCol);
drh75897232000-05-29 14:26:00 +0000238 sqliteFree(pTable);
239}
240
241/*
drh5edc3122001-09-13 21:53:09 +0000242** Unlink the given table from the hash tables and the delete the
drh74e24cd2002-01-09 03:19:59 +0000243** table structure with all its indices.
drh5edc3122001-09-13 21:53:09 +0000244*/
drh74e24cd2002-01-09 03:19:59 +0000245static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000246 Table *pOld;
247 assert( db!=0 );
248 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
249 assert( pOld==0 || pOld==p );
250 sqliteHashInsert(&db->tblDrop, p, 0, 0);
drh74e24cd2002-01-09 03:19:59 +0000251 sqliteDeleteTable(db, p);
252}
253
254/*
255** Move the given table to the pending DROP TABLE queue if it has
256** been committed. If this table was never committed, then just
257** delete it. Do the same for all its indices.
258**
259** Table on the drop queue are not actually deleted until a COMMIT
260** statement is executed. If a ROLLBACK occurs instead of a COMMIT,
261** then the tables on the drop queue are moved back into the main
262** hash table.
263*/
drhda9e0342002-01-10 14:31:48 +0000264static void sqlitePendingDropTable(sqlite *db, Table *pTbl){
drh74e24cd2002-01-09 03:19:59 +0000265 if( !pTbl->isCommit ){
266 sqliteUnlinkAndDeleteTable(db, pTbl);
267 }else{
268 Table *pOld;
269 Index *pIndex, *pNext;
270 pOld = sqliteHashInsert(&db->tblHash, pTbl->zName, strlen(pTbl->zName)+1,0);
271 assert( pOld==pTbl );
272 sqliteHashInsert(&db->tblDrop, pTbl, 0, pTbl);
273 for(pIndex = pTbl->pIndex; pIndex; pIndex=pNext){
274 pNext = pIndex->pNext;
275 sqlitePendingDropIndex(db, pIndex);
276 }
277 }
drh5edc3122001-09-13 21:53:09 +0000278}
279
280/*
drh5e00f6c2001-09-13 13:46:56 +0000281** Check all Tables and Indexes in the internal hash table and commit
282** any additions or deletions to those hash tables.
283**
284** When executing CREATE TABLE and CREATE INDEX statements, the Table
285** and Index structures are created and added to the hash tables, but
286** the "isCommit" field is not set. This routine sets those fields.
drh74e24cd2002-01-09 03:19:59 +0000287** When executing DROP TABLE and DROP INDEX, the table or index structures
288** are moved out of tblHash and idxHash into tblDrop and idxDrop. This
289** routine deletes the structure in tblDrop and idxDrop.
drh5e00f6c2001-09-13 13:46:56 +0000290**
291** See also: sqliteRollbackInternalChanges()
292*/
293void sqliteCommitInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000294 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000295 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drh50e5dad2001-09-15 00:57:28 +0000296 db->schema_cookie = db->next_cookie;
drhbeae3192001-09-22 18:12:08 +0000297 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
298 Table *pTable = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000299 pTable->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000300 }
drh74e24cd2002-01-09 03:19:59 +0000301 for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
drhbeae3192001-09-22 18:12:08 +0000302 Table *pTable = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000303 sqliteDeleteTable(db, pTable);
drhbeae3192001-09-22 18:12:08 +0000304 }
drh74e24cd2002-01-09 03:19:59 +0000305 sqliteHashClear(&db->tblDrop);
drhbeae3192001-09-22 18:12:08 +0000306 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
drh4a324312001-12-21 14:30:42 +0000307 Index *pIndex = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000308 pIndex->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000309 }
drh74e24cd2002-01-09 03:19:59 +0000310 while( (pElem=sqliteHashFirst(&db->idxDrop))!=0 ){
drhbeae3192001-09-22 18:12:08 +0000311 Index *pIndex = sqliteHashData(pElem);
312 sqliteUnlinkAndDeleteIndex(db, pIndex);
313 }
drh74e24cd2002-01-09 03:19:59 +0000314 sqliteHashClear(&db->idxDrop);
drh5e00f6c2001-09-13 13:46:56 +0000315 db->flags &= ~SQLITE_InternChanges;
316}
317
318/*
319** This routine runs when one or more CREATE TABLE, CREATE INDEX,
drhf57b3392001-10-08 13:22:32 +0000320** DROP TABLE, or DROP INDEX statements gets rolled back. The
drh5e00f6c2001-09-13 13:46:56 +0000321** additions or deletions of Table and Index structures in the
322** internal hash tables are undone.
323**
324** See also: sqliteCommitInternalChanges()
325*/
326void sqliteRollbackInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000327 Hash toDelete;
328 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000329 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drhbeae3192001-09-22 18:12:08 +0000330 sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
drh50e5dad2001-09-15 00:57:28 +0000331 db->next_cookie = db->schema_cookie;
drhbeae3192001-09-22 18:12:08 +0000332 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
333 Table *pTable = sqliteHashData(pElem);
334 if( !pTable->isCommit ){
335 sqliteHashInsert(&toDelete, pTable, 0, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000336 }
337 }
drhbeae3192001-09-22 18:12:08 +0000338 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
339 Table *pTable = sqliteHashData(pElem);
340 sqliteUnlinkAndDeleteTable(db, pTable);
341 }
342 sqliteHashClear(&toDelete);
drh74e24cd2002-01-09 03:19:59 +0000343 for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
344 Table *pOld, *p = sqliteHashData(pElem);
345 assert( p->isCommit );
346 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
347 assert( pOld==0 || pOld==p );
348 }
349 sqliteHashClear(&db->tblDrop);
drhbeae3192001-09-22 18:12:08 +0000350 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
drh4a324312001-12-21 14:30:42 +0000351 Index *pIndex = sqliteHashData(pElem);
drhbeae3192001-09-22 18:12:08 +0000352 if( !pIndex->isCommit ){
353 sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000354 }
355 }
drhbeae3192001-09-22 18:12:08 +0000356 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
357 Index *pIndex = sqliteHashData(pElem);
358 sqliteUnlinkAndDeleteIndex(db, pIndex);
359 }
360 sqliteHashClear(&toDelete);
drh74e24cd2002-01-09 03:19:59 +0000361 for(pElem=sqliteHashFirst(&db->idxDrop); pElem; pElem=sqliteHashNext(pElem)){
362 Index *pOld, *p = sqliteHashData(pElem);
363 assert( p->isCommit );
364 p->isDropped = 0;
365 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, p);
366 assert( pOld==0 || pOld==p );
367 }
368 sqliteHashClear(&db->idxDrop);
drh5e00f6c2001-09-13 13:46:56 +0000369 db->flags &= ~SQLITE_InternChanges;
370}
371
372/*
drh1ccde152000-06-17 13:12:39 +0000373** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000374**
375** Space to hold the name is obtained from sqliteMalloc() and must
376** be freed by the calling function.
377*/
drhcce7d172000-05-31 15:34:51 +0000378char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000379 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000380 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000381 return zName;
382}
383
384/*
385** Begin constructing a new table representation in memory. This is
386** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000387** to a CREATE TABLE statement. In particular, this routine is called
388** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000389** pStart token is the CREATE and pName is the table name. The isTemp
390** flag is true if the "TEMP" or "TEMPORARY" keyword occurs in between
391** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000392**
drhf57b3392001-10-08 13:22:32 +0000393** The new table record is initialized and put in pParse->pNewTable.
394** As more of the CREATE TABLE statement is parsed, additional action
395** routines will be called to add more information to this record.
396** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
397** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000398*/
drhf57b3392001-10-08 13:22:32 +0000399void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){
drh75897232000-05-29 14:26:00 +0000400 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000401 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000402 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000403 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000404 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000405
406 pParse->sFirstToken = *pStart;
407 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000408 if( zName==0 ) return;
drhf57b3392001-10-08 13:22:32 +0000409
410 /* Before trying to create a temporary table, make sure the Btree for
411 ** holding temporary tables is open.
412 */
413 if( isTemp && db->pBeTemp==0 ){
414 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
415 if( rc!=SQLITE_OK ){
416 sqliteSetNString(&pParse->zErrMsg, "unable to open a temporary database "
417 "file for storing temporary tables", 0);
418 pParse->nErr++;
419 return;
420 }
421 if( db->flags & SQLITE_InTrans ){
422 rc = sqliteBtreeBeginTrans(db->pBeTemp);
423 if( rc!=SQLITE_OK ){
424 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000425 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000426 pParse->nErr++;
427 return;
428 }
429 }
430 }
431
432 /* Make sure the new table name does not collide with an existing
433 ** index or table name. Issue an error message if it does.
434 **
435 ** If we are re-reading the sqlite_master table because of a schema
436 ** change and a new permanent table is found whose name collides with
437 ** an existing temporary table, then ignore the new permanent table.
438 ** We will continue parsing, but the pParse->nameClash flag will be set
439 ** so we will know to discard the table record once parsing has finished.
440 */
drhbe0072d2001-09-13 14:46:09 +0000441 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000442 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000443 if( pTable->isTemp && pParse->initFlag ){
444 pParse->nameClash = 1;
445 }else{
446 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
447 " already exists", 0, 0);
448 sqliteFree(zName);
449 pParse->nErr++;
450 return;
451 }
452 }else{
453 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000454 }
drhf57b3392001-10-08 13:22:32 +0000455 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
456 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000457 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
458 zName, 0);
drh75897232000-05-29 14:26:00 +0000459 sqliteFree(zName);
460 pParse->nErr++;
461 return;
462 }
463 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000464 if( pTable==0 ){
465 sqliteFree(zName);
466 return;
467 }
drh75897232000-05-29 14:26:00 +0000468 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000469 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000470 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000471 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000472 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000473 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000474 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000475 pParse->pNewTable = pTable;
drhadbca9c2001-09-27 15:11:53 +0000476 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drh1c928532002-01-31 15:54:21 +0000477 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +0000478 if( !isTemp ){
drh4a324312001-12-21 14:30:42 +0000479 sqliteVdbeAddOp(v, OP_SetCookie, db->file_format, 1);
drh99fcd712001-10-13 01:06:47 +0000480 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
481 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000482 }
drh5e00f6c2001-09-13 13:46:56 +0000483 }
drh75897232000-05-29 14:26:00 +0000484}
485
486/*
487** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000488**
489** The parser calls this routine once for each column declaration
490** in a CREATE TABLE statement. sqliteStartTable() gets called
491** first to get things going. Then this routine is called for each
492** column.
drh75897232000-05-29 14:26:00 +0000493*/
494void sqliteAddColumn(Parse *pParse, Token *pName){
495 Table *p;
496 char **pz;
497 if( (p = pParse->pNewTable)==0 ) return;
498 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000499 Column *aNew;
500 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
501 if( aNew==0 ) return;
502 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000503 }
drh7020f652000-06-03 18:06:52 +0000504 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
505 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000506 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000507 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000508}
509
510/*
drh382c0242001-10-06 16:33:02 +0000511** This routine is called by the parser while in the middle of
512** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
513** been seen on a column. This routine sets the notNull flag on
514** the column currently under construction.
515*/
drh9cfcf5d2002-01-29 18:41:24 +0000516void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000517 Table *p;
518 int i;
519 if( (p = pParse->pNewTable)==0 ) return;
520 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000521 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000522}
523
524/*
525** This routine is called by the parser while in the middle of
526** parsing a CREATE TABLE statement. The pFirst token is the first
527** token in the sequence of tokens that describe the type of the
528** column currently under construction. pLast is the last token
529** in the sequence. Use this information to construct a string
530** that contains the typename of the column and store that string
531** in zType.
532*/
533void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
534 Table *p;
535 int i, j;
536 int n;
537 char *z, **pz;
538 if( (p = pParse->pNewTable)==0 ) return;
539 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000540 if( i<0 ) return;
drh382c0242001-10-06 16:33:02 +0000541 pz = &p->aCol[i].zType;
drh5a2c2c22001-11-21 02:21:11 +0000542 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000543 sqliteSetNString(pz, pFirst->z, n, 0);
544 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000545 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000546 for(i=j=0; z[i]; i++){
547 int c = z[i];
548 if( isspace(c) ) continue;
549 z[j++] = c;
550 }
551 z[j] = 0;
552}
553
554/*
drh7020f652000-06-03 18:06:52 +0000555** The given token is the default value for the last column added to
556** the table currently under construction. If "minusFlag" is true, it
557** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000558**
559** This routine is called by the parser while in the middle of
560** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000561*/
562void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
563 Table *p;
564 int i;
565 char **pz;
566 if( (p = pParse->pNewTable)==0 ) return;
567 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000568 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000569 pz = &p->aCol[i].zDflt;
570 if( minusFlag ){
571 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
572 }else{
573 sqliteSetNString(pz, pVal->z, pVal->n, 0);
574 }
575 sqliteDequote(*pz);
576}
577
578/*
drh4a324312001-12-21 14:30:42 +0000579** Designate the PRIMARY KEY for the table. pList is a list of names
580** of columns that form the primary key. If pList is NULL, then the
581** most recently added column of the table is the primary key.
582**
583** A table can have at most one primary key. If the table already has
584** a primary key (and this is the second primary key) then create an
585** error.
586**
587** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
588** then we will try to use that column as the row id. (Exception:
589** For backwards compatibility with older databases, do not do this
590** if the file format version number is less than 1.) Set the Table.iPKey
591** field of the table under construction to be the index of the
592** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
593** no INTEGER PRIMARY KEY.
594**
595** If the key is not an INTEGER PRIMARY KEY, then create a unique
596** index for the key. No index is created for INTEGER PRIMARY KEYs.
597*/
drh9cfcf5d2002-01-29 18:41:24 +0000598void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000599 Table *pTab = pParse->pNewTable;
600 char *zType = 0;
601 int iCol = -1;
602 if( pTab==0 ) return;
603 if( pTab->hasPrimKey ){
604 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
605 "\" has more than one primary key", 0);
606 pParse->nErr++;
607 return;
608 }
609 pTab->hasPrimKey = 1;
610 if( pList==0 ){
611 iCol = pTab->nCol - 1;
612 }else if( pList->nId==1 ){
613 for(iCol=0; iCol<pTab->nCol; iCol++){
614 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
615 }
616 }
617 if( iCol>=0 && iCol<pTab->nCol ){
618 zType = pTab->aCol[iCol].zType;
619 }
620 if( pParse->db->file_format>=1 &&
621 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
622 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000623 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000624 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000625 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drh4a324312001-12-21 14:30:42 +0000626 }
627}
628
629/*
drh50e5dad2001-09-15 00:57:28 +0000630** Come up with a new random value for the schema cookie. Make sure
631** the new value is different from the old.
632**
633** The schema cookie is used to determine when the schema for the
634** database changes. After each schema change, the cookie value
635** changes. When a process first reads the schema it records the
636** cookie. Thereafter, whenever it goes to access the database,
637** it checks the cookie to make sure the schema has not changed
638** since it was last read.
639**
640** This plan is not completely bullet-proof. It is possible for
641** the schema to change multiple times and for the cookie to be
642** set back to prior value. But schema changes are infrequent
643** and the probability of hitting the same cookie value is only
644** 1 chance in 2^32. So we're safe enough.
645*/
646static void changeCookie(sqlite *db){
647 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000648 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000649 db->flags |= SQLITE_InternChanges;
650 }
651}
652
653/*
drh75897232000-05-29 14:26:00 +0000654** This routine is called to report the final ")" that terminates
655** a CREATE TABLE statement.
656**
drhf57b3392001-10-08 13:22:32 +0000657** The table structure that other action routines have been building
658** is added to the internal hash tables, assuming no errors have
659** occurred.
drh75897232000-05-29 14:26:00 +0000660**
drh1ccde152000-06-17 13:12:39 +0000661** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000662** unless this is a temporary table or initFlag==1. When initFlag==1,
663** it means we are reading the sqlite_master table because we just
664** connected to the database or because the sqlite_master table has
665** recently changes, so the entry for this table already exists in
666** the sqlite_master table. We do not want to create it again.
drh75897232000-05-29 14:26:00 +0000667*/
668void sqliteEndTable(Parse *pParse, Token *pEnd){
669 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000670 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000671
drhdaffd0e2001-04-11 14:28:42 +0000672 if( pEnd==0 || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000673 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000674 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000675
drhf57b3392001-10-08 13:22:32 +0000676 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000677 */
drhad75e982001-10-09 04:19:46 +0000678 assert( pParse->nameClash==0 || pParse->initFlag==1 );
drhf57b3392001-10-08 13:22:32 +0000679 if( pParse->explain==0 && pParse->nameClash==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000680 Table *pOld;
681 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
682 if( pOld ){
drh74e24cd2002-01-09 03:19:59 +0000683 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh6d4abfb2001-10-22 02:58:08 +0000684 return;
685 }
drh75897232000-05-29 14:26:00 +0000686 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000687 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000688 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000689 }
690
drhd78eeee2001-09-13 16:18:53 +0000691 /* If the initFlag is 1 it means we are reading the SQL off the
692 ** "sqlite_master" table on the disk. So do not write to the disk
drhe3c41372001-09-17 20:25:58 +0000693 ** again. Extract the root page number for the table from the
694 ** pParse->newTnum field. (The page number should have been put
drh382c0242001-10-06 16:33:02 +0000695 ** there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000696 */
697 if( pParse->initFlag ){
698 p->tnum = pParse->newTnum;
699 }
700
drhe3c41372001-09-17 20:25:58 +0000701 /* If not initializing, then create a record for the new table
702 ** in the SQLITE_MASTER table of the database.
drhf57b3392001-10-08 13:22:32 +0000703 **
704 ** If this is a TEMPORARY table, then just create the table. Do not
705 ** make an entry in SQLITE_MASTER.
drh75897232000-05-29 14:26:00 +0000706 */
707 if( !pParse->initFlag ){
drhadbca9c2001-09-27 15:11:53 +0000708 int n, addr;
drhd8bc7082000-06-07 23:51:50 +0000709 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000710
drhd8bc7082000-06-07 23:51:50 +0000711 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000712 if( v==0 ) return;
drh5a2c2c22001-11-21 02:21:11 +0000713 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
drhf57b3392001-10-08 13:22:32 +0000714 if( !p->isTemp ){
drh99fcd712001-10-13 01:06:47 +0000715 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
716 sqliteVdbeAddOp(v, OP_String, 0, 0);
717 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
718 sqliteVdbeAddOp(v, OP_String, 0, 0);
719 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
720 sqliteVdbeAddOp(v, OP_String, 0, 0);
721 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000722 }
drh653851f2001-12-15 02:35:59 +0000723 addr = sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
drh99fcd712001-10-13 01:06:47 +0000724 sqliteVdbeChangeP3(v, addr, (char *)&p->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +0000725 p->tnum = 0;
drhf57b3392001-10-08 13:22:32 +0000726 if( !p->isTemp ){
drh99fcd712001-10-13 01:06:47 +0000727 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000728 sqliteVdbeChangeP3(v, addr, pParse->sFirstToken.z, n);
drh99fcd712001-10-13 01:06:47 +0000729 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +0000730 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000731 changeCookie(db);
drh99fcd712001-10-13 01:06:47 +0000732 sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0);
733 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000734 }
drh1c928532002-01-31 15:54:21 +0000735 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000736 }
737}
738
739/*
740** Given a token, look up a table with that name. If not found, leave
741** an error for the parser to find and return NULL.
742*/
drhcce7d172000-05-31 15:34:51 +0000743Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +0000744 char *zName;
745 Table *pTab;
746 zName = sqliteTableNameFromToken(pTok);
747 if( zName==0 ) return 0;
748 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +0000749 sqliteFree(zName);
750 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +0000751 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
752 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +0000753 pParse->nErr++;
754 }
755 return pTab;
756}
757
758/*
759** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +0000760** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +0000761*/
762void sqliteDropTable(Parse *pParse, Token *pName){
763 Table *pTable;
drh75897232000-05-29 14:26:00 +0000764 Vdbe *v;
765 int base;
drh5edc3122001-09-13 21:53:09 +0000766 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000767
drhdaffd0e2001-04-11 14:28:42 +0000768 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000769 pTable = sqliteTableFromToken(pParse, pName);
770 if( pTable==0 ) return;
771 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +0000772 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
773 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +0000774 pParse->nErr++;
775 return;
776 }
777
drh1ccde152000-06-17 13:12:39 +0000778 /* Generate code to remove the table from the master table
779 ** on disk.
780 */
drhd8bc7082000-06-07 23:51:50 +0000781 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000782 if( v ){
783 static VdbeOp dropTable[] = {
drhecdc7532001-09-23 02:35:53 +0000784 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +0000785 { OP_Rewind, 0, ADDR(9), 0},
drhd78eeee2001-09-13 16:18:53 +0000786 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +0000787 { OP_MemStore, 1, 1, 0},
788 { OP_MemLoad, 1, 0, 0}, /* 4 */
drhe3c41372001-09-17 20:25:58 +0000789 { OP_Column, 0, 2, 0},
drh6b563442001-11-07 16:48:26 +0000790 { OP_Ne, 0, ADDR(8), 0},
drh75897232000-05-29 14:26:00 +0000791 { OP_Delete, 0, 0, 0},
drh6b563442001-11-07 16:48:26 +0000792 { OP_Next, 0, ADDR(4), 0}, /* 8 */
drhf57b3392001-10-08 13:22:32 +0000793 { OP_SetCookie, 0, 0, 0}, /* 9 */
drh75897232000-05-29 14:26:00 +0000794 { OP_Close, 0, 0, 0},
795 };
796 Index *pIdx;
drh1c928532002-01-31 15:54:21 +0000797 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +0000798 if( !pTable->isTemp ){
799 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh99fcd712001-10-13 01:06:47 +0000800 sqliteVdbeChangeP3(v, base+2, pTable->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000801 changeCookie(db);
802 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
803 }
drh99fcd712001-10-13 01:06:47 +0000804 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
drh75897232000-05-29 14:26:00 +0000805 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
drh99fcd712001-10-13 01:06:47 +0000806 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
drh5e00f6c2001-09-13 13:46:56 +0000807 }
drh1c928532002-01-31 15:54:21 +0000808 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000809 }
810
drh74e24cd2002-01-09 03:19:59 +0000811 /* Move the table (and all its indices) to the pending DROP queue.
812 ** Or, if the table was never committed, just delete it. If the table
813 ** has been committed and is placed on the pending DROP queue, then the
814 ** delete will occur when sqliteCommitInternalChanges() executes.
drh75897232000-05-29 14:26:00 +0000815 **
816 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +0000817 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +0000818 */
819 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +0000820 sqlitePendingDropTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +0000821 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000822 }
823}
824
825/*
826** Create a new index for an SQL table. pIndex is the name of the index
827** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +0000828** be NULL for a primary key or an index that is created to satisfy a
829** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +0000830** as the table to be indexed. pParse->pNewTable is a table that is
831** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +0000832**
drh382c0242001-10-06 16:33:02 +0000833** pList is a list of columns to be indexed. pList will be NULL if this
834** is a primary key or unique-constraint on the most recent column added
835** to the table currently under construction.
drh75897232000-05-29 14:26:00 +0000836*/
837void sqliteCreateIndex(
838 Parse *pParse, /* All information about this parse */
839 Token *pName, /* Name of the index. May be NULL */
840 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +0000841 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +0000842 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +0000843 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
844 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
845){
846 Table *pTab; /* Table to be indexed */
847 Index *pIndex; /* The index to be created */
848 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +0000849 int i, j;
drhf57b3392001-10-08 13:22:32 +0000850 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +0000851 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +0000852 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +0000853
drhdaffd0e2001-04-11 14:28:42 +0000854 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
855
drh75897232000-05-29 14:26:00 +0000856 /*
857 ** Find the table that is to be indexed. Return early if not found.
858 */
859 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +0000860 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +0000861 pTab = sqliteTableFromToken(pParse, pTable);
862 }else{
drhe3c41372001-09-17 20:25:58 +0000863 assert( pName==0 );
drh75897232000-05-29 14:26:00 +0000864 pTab = pParse->pNewTable;
865 }
866 if( pTab==0 || pParse->nErr ) goto exit_create_index;
867 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +0000868 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
869 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +0000870 pParse->nErr++;
871 goto exit_create_index;
872 }
873
drhf57b3392001-10-08 13:22:32 +0000874 /* If this index is created while re-reading the schema from sqlite_master
875 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +0000876 ** only mean that the table that this index is really associated with is
877 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +0000878 ** Since its table has been suppressed, we need to also suppress the
879 ** index.
880 */
881 if( pParse->initFlag && pTab->isTemp ){
882 goto exit_create_index;
883 }
884
drh75897232000-05-29 14:26:00 +0000885 /*
886 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +0000887 ** index or table with the same name.
888 **
889 ** Exception: If we are reading the names of permanent indices from the
890 ** sqlite_master table (because some other process changed the schema) and
891 ** one of the index names collides with the name of a temporary table or
892 ** index, then we will continue to process this index, but we will not
893 ** store its name in the hash table. Set the hideName flag to accomplish
894 ** this.
895 **
896 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +0000897 ** dealing with a primary key or UNIQUE constraint. We have to invent our
898 ** own name.
drh75897232000-05-29 14:26:00 +0000899 */
900 if( pName ){
drhf57b3392001-10-08 13:22:32 +0000901 Index *pISameName; /* Another index with the same name */
902 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +0000903 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +0000904 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +0000905 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
906 if( pISameName->pTable->isTemp && pParse->initFlag ){
907 hideName = 1;
908 }else{
909 sqliteSetString(&pParse->zErrMsg, "index ", zName,
910 " already exists", 0);
911 pParse->nErr++;
912 goto exit_create_index;
913 }
drhe3c41372001-09-17 20:25:58 +0000914 }
drhf57b3392001-10-08 13:22:32 +0000915 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
916 if( pTSameName->isTemp && pParse->initFlag ){
917 hideName = 1;
918 }else{
919 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
920 zName, 0);
921 pParse->nErr++;
922 goto exit_create_index;
923 }
drhe3c41372001-09-17 20:25:58 +0000924 }
drh75897232000-05-29 14:26:00 +0000925 }else{
drhadbca9c2001-09-27 15:11:53 +0000926 char zBuf[30];
927 int n;
928 Index *pLoop;
929 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
930 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +0000931 zName = 0;
drhadbca9c2001-09-27 15:11:53 +0000932 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +0000933 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +0000934 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +0000935 }
936
937 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +0000938 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +0000939 ** So create a fake list to simulate this.
940 */
941 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +0000942 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +0000943 nullId.n = strlen(nullId.z);
944 pList = sqliteIdListAppend(0, &nullId);
945 if( pList==0 ) goto exit_create_index;
946 }
947
948 /*
949 ** Allocate the index structure.
950 */
drhdcc581c2000-05-30 13:44:19 +0000951 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +0000952 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +0000953 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +0000954 pIndex->aiColumn = (int*)&pIndex[1];
955 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +0000956 strcpy(pIndex->zName, zName);
957 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +0000958 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +0000959 pIndex->onError = pIndex->isUnique = onError;
drh75897232000-05-29 14:26:00 +0000960
drh1ccde152000-06-17 13:12:39 +0000961 /* Scan the names of the columns of the table to be indexed and
962 ** load the column indices into the Index structure. Report an error
963 ** if any column is not found.
drh75897232000-05-29 14:26:00 +0000964 */
965 for(i=0; i<pList->nId; i++){
966 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000967 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +0000968 }
969 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +0000970 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +0000971 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +0000972 pParse->nErr++;
973 sqliteFree(pIndex);
974 goto exit_create_index;
975 }
drh967e8b72000-06-21 13:59:10 +0000976 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +0000977 }
978
979 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +0000980 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +0000981 */
drhf57b3392001-10-08 13:22:32 +0000982 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +0000983 Index *p;
984 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
985 if( p ){
986 assert( p==pIndex ); /* Malloc must have failed */
987 sqliteFree(pIndex);
988 goto exit_create_index;
989 }
drh5e00f6c2001-09-13 13:46:56 +0000990 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000991 }
drh9cfcf5d2002-01-29 18:41:24 +0000992
993 /* When adding an index to the list of indices for a table, make
994 ** sure all indices labeled OE_Replace come after all those labeled
995 ** OE_Ignore. This is necessary for the correct operation of UPDATE
996 ** and INSERT.
997 */
998 if( onError!=OE_Replace || pTab->pIndex==0
999 || pTab->pIndex->onError==OE_Replace){
1000 pIndex->pNext = pTab->pIndex;
1001 pTab->pIndex = pIndex;
1002 }else{
1003 Index *pOther = pTab->pIndex;
1004 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1005 pOther = pOther->pNext;
1006 }
1007 pIndex->pNext = pOther->pNext;
1008 pOther->pNext = pIndex;
1009 }
drh75897232000-05-29 14:26:00 +00001010
drhd78eeee2001-09-13 16:18:53 +00001011 /* If the initFlag is 1 it means we are reading the SQL off the
1012 ** "sqlite_master" table on the disk. So do not write to the disk
1013 ** again. Extract the table number from the pParse->newTnum field.
1014 */
drhadbca9c2001-09-27 15:11:53 +00001015 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001016 pIndex->tnum = pParse->newTnum;
1017 }
1018
drh75897232000-05-29 14:26:00 +00001019 /* If the initFlag is 0 then create the index on disk. This
1020 ** involves writing the index into the master table and filling in the
1021 ** index with the current table contents.
1022 **
1023 ** The initFlag is 0 when the user first enters a CREATE INDEX
1024 ** command. The initFlag is 1 when a database is opened and
1025 ** CREATE INDEX statements are read out of the master table. In
1026 ** the latter case the index already exists on disk, which is why
1027 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001028 **
1029 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001030 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1031 ** has just been created, it contains no data and the index initialization
1032 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001033 */
drhadbca9c2001-09-27 15:11:53 +00001034 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001035 int n;
drhadbca9c2001-09-27 15:11:53 +00001036 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001037 int lbl1, lbl2;
1038 int i;
drhadbca9c2001-09-27 15:11:53 +00001039 int addr;
drhf57b3392001-10-08 13:22:32 +00001040 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001041
drhd8bc7082000-06-07 23:51:50 +00001042 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001043 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001044 if( pTable!=0 ){
drh1c928532002-01-31 15:54:21 +00001045 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +00001046 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001047 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
1048 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001049 }
drhadbca9c2001-09-27 15:11:53 +00001050 }
drhf57b3392001-10-08 13:22:32 +00001051 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001052 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1053 sqliteVdbeAddOp(v, OP_String, 0, 0);
1054 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1055 sqliteVdbeAddOp(v, OP_String, 0, 0);
1056 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1057 sqliteVdbeAddOp(v, OP_String, 0, 0);
1058 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001059 }
drh99fcd712001-10-13 01:06:47 +00001060 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1061 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001062 pIndex->tnum = 0;
1063 if( pTable ){
drhf57b3392001-10-08 13:22:32 +00001064 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001065 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001066 }else{
drh99fcd712001-10-13 01:06:47 +00001067 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1068 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001069 }
drh5e00f6c2001-09-13 13:46:56 +00001070 }
drhf57b3392001-10-08 13:22:32 +00001071 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001072 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001073 if( pStart && pEnd ){
drh5a2c2c22001-11-21 02:21:11 +00001074 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
drhf57b3392001-10-08 13:22:32 +00001075 sqliteVdbeChangeP3(v, addr, pStart->z, n);
1076 }
drh99fcd712001-10-13 01:06:47 +00001077 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +00001078 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh75897232000-05-29 14:26:00 +00001079 }
drhadbca9c2001-09-27 15:11:53 +00001080 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001081 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1082 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001083 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001084 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1085 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001086 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001087 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001088 }
drh99fcd712001-10-13 01:06:47 +00001089 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001090 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001091 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001092 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001093 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1094 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001095 }
drhadbca9c2001-09-27 15:11:53 +00001096 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001097 if( !isTemp ){
1098 changeCookie(db);
drh99fcd712001-10-13 01:06:47 +00001099 sqliteVdbeAddOp(v, OP_SetCookie, db->next_cookie, 0);
1100 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001101 }
drh1c928532002-01-31 15:54:21 +00001102 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001103 }
drh75897232000-05-29 14:26:00 +00001104 }
1105
drh75897232000-05-29 14:26:00 +00001106 /* Clean up before exiting */
1107exit_create_index:
1108 sqliteIdListDelete(pList);
1109 sqliteFree(zName);
1110 return;
1111}
1112
1113/*
drh74e24cd2002-01-09 03:19:59 +00001114** This routine will drop an existing named index. This routine
1115** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001116*/
1117void sqliteDropIndex(Parse *pParse, Token *pName){
1118 Index *pIndex;
1119 char *zName;
1120 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001121 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001122
drhdaffd0e2001-04-11 14:28:42 +00001123 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001124 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001125 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001126 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001127 sqliteFree(zName);
1128 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001129 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1130 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001131 pParse->nErr++;
1132 return;
1133 }
1134
1135 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001136 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001137 if( v ){
1138 static VdbeOp dropIndex[] = {
drhecdc7532001-09-23 02:35:53 +00001139 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +00001140 { OP_Rewind, 0, ADDR(10),0},
drhd78eeee2001-09-13 16:18:53 +00001141 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +00001142 { OP_MemStore, 1, 1, 0},
1143 { OP_MemLoad, 1, 0, 0}, /* 4 */
drh5e00f6c2001-09-13 13:46:56 +00001144 { OP_Column, 0, 1, 0},
drh6b563442001-11-07 16:48:26 +00001145 { OP_Eq, 0, ADDR(9), 0},
1146 { OP_Next, 0, ADDR(4), 0},
1147 { OP_Goto, 0, ADDR(10),0},
1148 { OP_Delete, 0, 0, 0}, /* 9 */
1149 { OP_SetCookie, 0, 0, 0}, /* 10 */
drh75897232000-05-29 14:26:00 +00001150 { OP_Close, 0, 0, 0},
1151 };
1152 int base;
drhf57b3392001-10-08 13:22:32 +00001153 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001154
drh1c928532002-01-31 15:54:21 +00001155 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +00001156 if( !pTab->isTemp ){
1157 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh99fcd712001-10-13 01:06:47 +00001158 sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001159 changeCookie(db);
drh6b563442001-11-07 16:48:26 +00001160 sqliteVdbeChangeP1(v, base+10, db->next_cookie);
drhf57b3392001-10-08 13:22:32 +00001161 }
drh99fcd712001-10-13 01:06:47 +00001162 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001163 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001164 }
1165
drh74e24cd2002-01-09 03:19:59 +00001166 /* Move the index onto the pending DROP queue. Or, if the index was
1167 ** never committed, just delete it. Indices on the pending DROP queue
1168 ** get deleted by sqliteCommitInternalChanges() when the user executes
1169 ** a COMMIT. Or if a rollback occurs, the elements of the DROP queue
1170 ** are moved back into the main hash table.
drh75897232000-05-29 14:26:00 +00001171 */
1172 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +00001173 sqlitePendingDropIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001174 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001175 }
1176}
1177
1178/*
1179** Add a new element to the end of an expression list. If pList is
1180** initially NULL, then create a new expression list.
1181*/
1182ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
1183 int i;
1184 if( pList==0 ){
1185 pList = sqliteMalloc( sizeof(ExprList) );
drh6d4abfb2001-10-22 02:58:08 +00001186 if( pList==0 ){
1187 sqliteExprDelete(pExpr);
1188 return 0;
1189 }
drh75897232000-05-29 14:26:00 +00001190 }
drh75897232000-05-29 14:26:00 +00001191 if( (pList->nExpr & 7)==0 ){
1192 int n = pList->nExpr + 8;
drh6d4abfb2001-10-22 02:58:08 +00001193 struct ExprList_item *a;
1194 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
1195 if( a==0 ){
1196 sqliteExprDelete(pExpr);
drh75897232000-05-29 14:26:00 +00001197 return pList;
1198 }
drh6d4abfb2001-10-22 02:58:08 +00001199 pList->a = a;
drh75897232000-05-29 14:26:00 +00001200 }
drh17e24df2001-11-06 14:10:41 +00001201 if( pExpr || pName ){
drhbf4133c2001-10-13 02:59:08 +00001202 i = pList->nExpr++;
1203 pList->a[i].pExpr = pExpr;
1204 pList->a[i].zName = 0;
1205 if( pName ){
1206 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
1207 sqliteDequote(pList->a[i].zName);
1208 }
drh75897232000-05-29 14:26:00 +00001209 }
1210 return pList;
1211}
1212
1213/*
1214** Delete an entire expression list.
1215*/
1216void sqliteExprListDelete(ExprList *pList){
1217 int i;
1218 if( pList==0 ) return;
1219 for(i=0; i<pList->nExpr; i++){
1220 sqliteExprDelete(pList->a[i].pExpr);
1221 sqliteFree(pList->a[i].zName);
1222 }
1223 sqliteFree(pList->a);
1224 sqliteFree(pList);
1225}
1226
1227/*
1228** Append a new element to the given IdList. Create a new IdList if
1229** need be.
drhdaffd0e2001-04-11 14:28:42 +00001230**
1231** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001232*/
1233IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1234 if( pList==0 ){
1235 pList = sqliteMalloc( sizeof(IdList) );
1236 if( pList==0 ) return 0;
1237 }
1238 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001239 struct IdList_item *a;
1240 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1241 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001242 sqliteIdListDelete(pList);
1243 return 0;
drh75897232000-05-29 14:26:00 +00001244 }
drh6d4abfb2001-10-22 02:58:08 +00001245 pList->a = a;
drh75897232000-05-29 14:26:00 +00001246 }
1247 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1248 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001249 char **pz = &pList->a[pList->nId].zName;
1250 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1251 if( *pz==0 ){
1252 sqliteIdListDelete(pList);
1253 return 0;
1254 }else{
1255 sqliteDequote(*pz);
1256 }
drh75897232000-05-29 14:26:00 +00001257 }
1258 pList->nId++;
1259 return pList;
1260}
1261
1262/*
1263** Add an alias to the last identifier on the given identifier list.
1264*/
1265void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1266 if( pList && pList->nId>0 ){
1267 int i = pList->nId - 1;
1268 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001269 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001270 }
1271}
1272
1273/*
1274** Delete an entire IdList
1275*/
1276void sqliteIdListDelete(IdList *pList){
1277 int i;
1278 if( pList==0 ) return;
1279 for(i=0; i<pList->nId; i++){
1280 sqliteFree(pList->a[i].zName);
1281 sqliteFree(pList->a[i].zAlias);
drhdaffd0e2001-04-11 14:28:42 +00001282 if( pList->a[i].pSelect ){
1283 sqliteFree(pList->a[i].zName);
1284 sqliteSelectDelete(pList->a[i].pSelect);
1285 sqliteDeleteTable(0, pList->a[i].pTab);
1286 }
drh75897232000-05-29 14:26:00 +00001287 }
1288 sqliteFree(pList->a);
1289 sqliteFree(pList);
1290}
1291
drh982cef72000-05-30 16:27:03 +00001292
1293/*
1294** The COPY command is for compatibility with PostgreSQL and specificially
1295** for the ability to read the output of pg_dump. The format is as
1296** follows:
1297**
1298** COPY table FROM file [USING DELIMITERS string]
1299**
1300** "table" is an existing table name. We will read lines of code from
1301** file to fill this table with data. File might be "stdin". The optional
1302** delimiter string identifies the field separators. The default is a tab.
1303*/
1304void sqliteCopy(
1305 Parse *pParse, /* The parser context */
1306 Token *pTableName, /* The name of the table into which we will insert */
1307 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001308 Token *pDelimiter, /* Use this as the field delimiter */
1309 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001310){
1311 Table *pTab;
1312 char *zTab;
drh1c928532002-01-31 15:54:21 +00001313 int i;
drh982cef72000-05-30 16:27:03 +00001314 Vdbe *v;
1315 int addr, end;
1316 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001317 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001318
1319 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001320 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhbe0072d2001-09-13 14:46:09 +00001321 pTab = sqliteFindTable(db, zTab);
drh982cef72000-05-30 16:27:03 +00001322 sqliteFree(zTab);
1323 if( pTab==0 ){
1324 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1325 pTableName->z, pTableName->n, 0);
1326 pParse->nErr++;
1327 goto copy_cleanup;
1328 }
1329 if( pTab->readOnly ){
1330 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1331 " may not be modified", 0);
1332 pParse->nErr++;
1333 goto copy_cleanup;
1334 }
drhd8bc7082000-06-07 23:51:50 +00001335 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001336 if( v ){
drhf57b3392001-10-08 13:22:32 +00001337 int openOp;
drh663fc632002-02-02 18:49:19 +00001338 sqliteBeginMultiWriteOperation(pParse);
drh99fcd712001-10-13 01:06:47 +00001339 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001340 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001341 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001342 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001343 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1344 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001345 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001346 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1347 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001348 }
drhb419a922002-01-30 16:17:23 +00001349 if( db->flags & SQLITE_CountRows ){
1350 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1351 }
drh982cef72000-05-30 16:27:03 +00001352 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001353 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001354 if( pDelimiter ){
1355 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1356 sqliteVdbeDequoteP3(v, addr);
1357 }else{
1358 sqliteVdbeChangeP3(v, addr, "\t", 1);
1359 }
drh8aff1012001-12-22 14:49:24 +00001360 if( pTab->iPKey>=0 ){
1361 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1362 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1363 }else{
1364 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1365 }
drh982cef72000-05-30 16:27:03 +00001366 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001367 if( i==pTab->iPKey ){
1368 /* The integer primary key column is filled with NULL since its
1369 ** value is always pulled from the record number */
1370 sqliteVdbeAddOp(v, OP_String, 0, 0);
1371 }else{
1372 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1373 }
drh982cef72000-05-30 16:27:03 +00001374 }
drhb419a922002-01-30 16:17:23 +00001375 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1376 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1377 if( (db->flags & SQLITE_CountRows)!=0 ){
1378 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001379 }
drh99fcd712001-10-13 01:06:47 +00001380 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1381 sqliteVdbeResolveLabel(v, end);
1382 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001383 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001384 if( db->flags & SQLITE_CountRows ){
1385 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1386 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1387 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1388 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1389 }
drh982cef72000-05-30 16:27:03 +00001390 }
1391
1392copy_cleanup:
1393 return;
1394}
drhdce2cbe2000-05-31 02:27:49 +00001395
1396/*
1397** The non-standard VACUUM command is used to clean up the database,
1398** collapse free space, etc. It is modelled after the VACUUM command
1399** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001400**
drh1bffb9c2002-02-03 17:37:36 +00001401** In version 1.0.x of SQLite, the VACUUM command would call
1402** gdbm_reorganize() on all the database tables. But beginning
1403** with 2.0.0, SQLite no longer uses GDBM so this command has
1404** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001405*/
1406void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001407 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001408}
drhc4a3c772001-04-04 11:48:57 +00001409
1410/*
1411** Begin a transaction
1412*/
drh1c928532002-01-31 15:54:21 +00001413void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001414 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001415
drhc4a3c772001-04-04 11:48:57 +00001416 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001417 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001418 if( db->flags & SQLITE_InTrans ) return;
drh1c928532002-01-31 15:54:21 +00001419 sqliteBeginWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001420 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001421 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001422}
1423
1424/*
1425** Commit a transaction
1426*/
1427void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001428 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001429
drhc4a3c772001-04-04 11:48:57 +00001430 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001431 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001432 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001433 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001434 sqliteEndWriteOperation(pParse);
1435 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001436}
1437
1438/*
1439** Rollback a transaction
1440*/
1441void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001442 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001443 Vdbe *v;
1444
drhc4a3c772001-04-04 11:48:57 +00001445 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001446 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001447 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001448 v = sqliteGetVdbe(pParse);
1449 if( v ){
drh99fcd712001-10-13 01:06:47 +00001450 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001451 }
drh5e00f6c2001-09-13 13:46:56 +00001452 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001453 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001454}
drhf57b14a2001-09-14 18:54:08 +00001455
1456/*
drh1c928532002-01-31 15:54:21 +00001457** Generate VDBE code that prepares for doing an operation that
drh663fc632002-02-02 18:49:19 +00001458** might change the database. The operation will be atomic in the
1459** sense that it will either do its changes completely or not at
1460** all. So there is not need to set a checkpoint is a transaction
1461** is already in effect.
drh1c928532002-01-31 15:54:21 +00001462*/
1463void sqliteBeginWriteOperation(Parse *pParse){
1464 Vdbe *v;
1465 v = sqliteGetVdbe(pParse);
1466 if( v==0 ) return;
drh663fc632002-02-02 18:49:19 +00001467 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh1c928532002-01-31 15:54:21 +00001468 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1469 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1470 pParse->schemaVerified = 1;
1471 }
1472}
1473
1474/*
drh663fc632002-02-02 18:49:19 +00001475** Generate VDBE code that prepares for doing an operation that
1476** might change the database. The operation might not be atomic in
1477** the sense that an error may be discovered and the operation might
1478** abort after some changes have been made. If we are in the middle
1479** of a transaction, then this sets a checkpoint. If we are not in
1480** a transaction, then start a transaction.
1481*/
1482void sqliteBeginMultiWriteOperation(Parse *pParse){
1483 Vdbe *v;
1484 v = sqliteGetVdbe(pParse);
1485 if( v==0 ) return;
1486 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
1487 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1488 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1489 pParse->schemaVerified = 1;
1490 }else{
1491 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1492 }
1493}
1494
1495/*
drh1c928532002-01-31 15:54:21 +00001496** Generate code that concludes an operation that may have changed
1497** the database. This is a companion function to BeginWriteOperation().
1498** If a transaction was started, then commit it. If a checkpoint was
1499** started then commit that.
1500*/
1501void sqliteEndWriteOperation(Parse *pParse){
1502 Vdbe *v;
1503 v = sqliteGetVdbe(pParse);
1504 if( v==0 ) return;
1505 if( pParse->db->flags & SQLITE_InTrans ){
1506 /* Do Nothing */
1507 }else{
1508 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
1509 }
1510}
1511
1512
1513/*
drhf57b14a2001-09-14 18:54:08 +00001514** Interpret the given string as a boolean value.
1515*/
1516static int getBoolean(char *z){
1517 static char *azTrue[] = { "yes", "on", "true" };
1518 int i;
1519 if( z[0]==0 ) return 0;
1520 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1521 return atoi(z);
1522 }
1523 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1524 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1525 }
1526 return 0;
1527}
1528
1529/*
1530** Process a pragma statement.
1531**
1532** Pragmas are of this form:
1533**
1534** PRAGMA id = value
1535**
1536** The identifier might also be a string. The value is a string, and
1537** identifier, or a number. If minusFlag is true, then the value is
1538** a number that was preceded by a minus sign.
1539*/
1540void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1541 char *zLeft = 0;
1542 char *zRight = 0;
1543 sqlite *db = pParse->db;
1544
1545 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1546 sqliteDequote(zLeft);
1547 if( minusFlag ){
1548 zRight = 0;
1549 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1550 }else{
1551 zRight = sqliteStrNDup(pRight->z, pRight->n);
1552 sqliteDequote(zRight);
1553 }
1554
1555 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1556 int size = atoi(zRight);
1557 sqliteBtreeSetCacheSize(db->pBe, size);
1558 }else
1559
1560 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1561 if( getBoolean(zRight) ){
1562 db->flags |= SQLITE_VdbeTrace;
1563 }else{
1564 db->flags &= ~SQLITE_VdbeTrace;
1565 }
1566 }else
1567
drh382c0242001-10-06 16:33:02 +00001568 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
1569 if( getBoolean(zRight) ){
1570 db->flags |= SQLITE_FullColNames;
1571 }else{
1572 db->flags &= ~SQLITE_FullColNames;
1573 }
1574 }else
1575
drhc3a64ba2001-11-22 00:01:27 +00001576 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
1577 if( getBoolean(zRight) ){
1578 db->flags |= SQLITE_ResultDetails;
1579 }else{
1580 db->flags &= ~SQLITE_ResultDetails;
1581 }
1582 }else
1583
drh1bee3d72001-10-15 00:44:35 +00001584 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
1585 if( getBoolean(zRight) ){
1586 db->flags |= SQLITE_CountRows;
1587 }else{
1588 db->flags &= ~SQLITE_CountRows;
1589 }
1590 }else
1591
drh6a535342001-10-19 16:44:56 +00001592 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
1593 if( getBoolean(zRight) ){
1594 db->flags |= SQLITE_NullCallback;
1595 }else{
1596 db->flags &= ~SQLITE_NullCallback;
1597 }
1598 }else
1599
drh382c0242001-10-06 16:33:02 +00001600 if( sqliteStrICmp(zLeft, "table_info")==0 ){
1601 Table *pTab;
1602 Vdbe *v;
1603 pTab = sqliteFindTable(db, zRight);
1604 if( pTab ) v = sqliteGetVdbe(pParse);
1605 if( pTab && v ){
1606 static VdbeOp tableInfoPreface[] = {
1607 { OP_ColumnCount, 5, 0, 0},
1608 { OP_ColumnName, 0, 0, "cid"},
1609 { OP_ColumnName, 1, 0, "name"},
1610 { OP_ColumnName, 2, 0, "type"},
1611 { OP_ColumnName, 3, 0, "notnull"},
1612 { OP_ColumnName, 4, 0, "dflt_value"},
1613 };
1614 int i;
1615 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
1616 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00001617 sqliteVdbeAddOp(v, OP_Integer, i, 0);
1618 sqliteVdbeAddOp(v, OP_String, 0, 0);
1619 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
1620 sqliteVdbeAddOp(v, OP_String, 0, 0);
1621 sqliteVdbeChangeP3(v, -1,
1622 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC);
1623 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
1624 sqliteVdbeAddOp(v, OP_String, 0, 0);
1625 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
1626 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00001627 }
1628 }
1629 }else
1630
1631 if( sqliteStrICmp(zLeft, "index_info")==0 ){
1632 Index *pIdx;
1633 Table *pTab;
1634 Vdbe *v;
1635 pIdx = sqliteFindIndex(db, zRight);
1636 if( pIdx ) v = sqliteGetVdbe(pParse);
1637 if( pIdx && v ){
1638 static VdbeOp tableInfoPreface[] = {
1639 { OP_ColumnCount, 3, 0, 0},
1640 { OP_ColumnName, 0, 0, "seqno"},
1641 { OP_ColumnName, 1, 0, "cid"},
1642 { OP_ColumnName, 2, 0, "name"},
1643 };
1644 int i;
1645 pTab = pIdx->pTable;
1646 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
1647 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001648 int cnum = pIdx->aiColumn[i];
1649 sqliteVdbeAddOp(v, OP_Integer, i, 0);
1650 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
1651 sqliteVdbeAddOp(v, OP_String, 0, 0);
1652 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
1653 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00001654 }
1655 }
1656 }else
1657
drh81a20f22001-10-12 17:30:04 +00001658 if( sqliteStrICmp(zLeft, "index_list")==0 ){
1659 Index *pIdx;
1660 Table *pTab;
1661 Vdbe *v;
1662 pTab = sqliteFindTable(db, zRight);
1663 if( pTab ){
1664 v = sqliteGetVdbe(pParse);
1665 pIdx = pTab->pIndex;
1666 }
1667 if( pTab && pIdx && v ){
1668 int i = 0;
1669 static VdbeOp indexListPreface[] = {
1670 { OP_ColumnCount, 3, 0, 0},
1671 { OP_ColumnName, 0, 0, "seq"},
1672 { OP_ColumnName, 1, 0, "name"},
1673 { OP_ColumnName, 2, 0, "unique"},
1674 };
1675
1676 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
1677 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00001678 sqliteVdbeAddOp(v, OP_Integer, i, 0);
1679 sqliteVdbeAddOp(v, OP_String, 0, 0);
1680 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00001681 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00001682 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh81a20f22001-10-12 17:30:04 +00001683 ++i;
1684 pIdx = pIdx->pNext;
1685 }
1686 }
1687 }else
1688
drhf57b14a2001-09-14 18:54:08 +00001689#ifndef NDEBUG
1690 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
1691 extern void sqliteParserTrace(FILE*, char *);
1692 if( getBoolean(zRight) ){
1693 sqliteParserTrace(stdout, "parser: ");
1694 }else{
1695 sqliteParserTrace(0, 0);
1696 }
1697 }else
1698#endif
1699
drh1bffb9c2002-02-03 17:37:36 +00001700#ifndef NDEBUG
1701 if( sqliteStrICmp(zLeft, "sanity_check")==0 ){
1702 static VdbeOp checkDb[] = {
1703 { OP_SetInsert, 0, 0, "2"},
1704 { OP_Open, 0, 2, 0},
1705 { OP_Rewind, 0, 6, 0},
1706 { OP_Column, 0, 3, 0},
1707 { OP_SetInsert, 0, 0, 0},
1708 { OP_Next, 0, 3, 0},
1709 { OP_SanityCheck, 0, 0, 0},
1710 { OP_ColumnCount, 1, 0, 0},
1711 { OP_ColumnName, 0, 0, "sanity_check"},
1712 { OP_Callback, 1, 0, 0},
1713 };
1714 Vdbe *v = sqliteGetVdbe(pParse);
1715 if( v==0 ) return;
1716 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
1717 }else
1718#endif
1719
drhf57b3392001-10-08 13:22:32 +00001720 {}
1721 sqliteFree(zLeft);
1722 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00001723}