blob: 5f89a717f737887f211b63f59511c59e5c7acbfe [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
drh832508b2002-03-02 17:04:07 +000020** creating ID lists
drhbed86902000-06-02 13:27:59 +000021** 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**
danielk1977f29ce552002-05-19 23:43:12 +000028** $Id: build.c,v 1.91 2002/05/19 23:43:14 danielk1977 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** Locate the in-memory structure that describes
69** a particular database table given the name
drh75897232000-05-29 14:26:00 +000070** of that table. Return NULL if not found.
71*/
drha76b5df2002-02-23 02:32:10 +000072Table *sqliteFindTable(sqlite *db, const char *zName){
drhafa4a022001-09-24 03:12:39 +000073 Table *p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +000074 return p;
drh75897232000-05-29 14:26:00 +000075}
76
77/*
drhf57b3392001-10-08 13:22:32 +000078** Locate the in-memory structure that describes
79** a particular index given the name of that index.
80** Return NULL if not found.
drh75897232000-05-29 14:26:00 +000081*/
drha76b5df2002-02-23 02:32:10 +000082Index *sqliteFindIndex(sqlite *db, const char *zName){
drhafa4a022001-09-24 03:12:39 +000083 Index *p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +000084 return p;
drh75897232000-05-29 14:26:00 +000085}
86
87/*
88** Remove the given index from the index hash table, and free
89** its memory structures.
90**
drhd229ca92002-01-09 13:30:41 +000091** The index is removed from the database hash tables but
92** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +000093** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +000094*/
drh74e24cd2002-01-09 03:19:59 +000095static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +000096 Index *pOld;
97 assert( db!=0 && p->zName!=0 );
98 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
99 if( pOld!=0 && pOld!=p ){
100 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000101 }
drhd229ca92002-01-09 13:30:41 +0000102 sqliteHashInsert(&db->idxDrop, p, 0, 0);
drh74e24cd2002-01-09 03:19:59 +0000103 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000104}
105
106/*
drhbeae3192001-09-22 18:12:08 +0000107** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000108** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000109** structures.
110*/
drh6d4abfb2001-10-22 02:58:08 +0000111void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000112 if( pIndex->pTable->pIndex==pIndex ){
113 pIndex->pTable->pIndex = pIndex->pNext;
114 }else{
115 Index *p;
116 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
117 if( p && p->pNext==pIndex ){
118 p->pNext = pIndex->pNext;
119 }
120 }
121 sqliteDeleteIndex(db, pIndex);
122}
123
124/*
drh74e24cd2002-01-09 03:19:59 +0000125** Move the given index to the pending DROP INDEX queue if it has
126** been committed. If this index was never committed, then just
127** delete it.
128**
129** Indices on the pending drop queue are deleted when a COMMIT is
130** executed. If a ROLLBACK occurs, the indices are moved back into
131** the main index hash table.
132*/
drhda9e0342002-01-10 14:31:48 +0000133static void sqlitePendingDropIndex(sqlite *db, Index *p){
drh74e24cd2002-01-09 03:19:59 +0000134 if( !p->isCommit ){
135 sqliteUnlinkAndDeleteIndex(db, p);
136 }else{
137 Index *pOld;
138 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000139 if( pOld!=0 && pOld!=p ){
140 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
141 }
drh74e24cd2002-01-09 03:19:59 +0000142 sqliteHashInsert(&db->idxDrop, p, 0, p);
143 p->isDropped = 1;
144 }
145}
146
147/*
drh75897232000-05-29 14:26:00 +0000148** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000149** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000150**
151** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000152** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000153** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000154**
155** Indices associated with the table are unlinked from the "db"
156** data structure if db!=NULL. If db==NULL, indices attached to
157** the table are deleted, but it is assumed they have already been
158** unlinked.
drh75897232000-05-29 14:26:00 +0000159*/
160void sqliteDeleteTable(sqlite *db, Table *pTable){
161 int i;
162 Index *pIndex, *pNext;
163 if( pTable==0 ) return;
164 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000165 sqliteFree(pTable->aCol[i].zName);
166 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000167 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000168 }
169 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
170 pNext = pIndex->pNext;
171 sqliteDeleteIndex(db, pIndex);
172 }
drh6e142f52000-06-08 13:36:40 +0000173 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000174 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000175 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000176 sqliteFree(pTable);
177}
178
179/*
drh5edc3122001-09-13 21:53:09 +0000180** Unlink the given table from the hash tables and the delete the
drh74e24cd2002-01-09 03:19:59 +0000181** table structure with all its indices.
drh5edc3122001-09-13 21:53:09 +0000182*/
drh74e24cd2002-01-09 03:19:59 +0000183static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000184 Table *pOld;
185 assert( db!=0 );
186 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
187 assert( pOld==0 || pOld==p );
188 sqliteHashInsert(&db->tblDrop, p, 0, 0);
drh74e24cd2002-01-09 03:19:59 +0000189 sqliteDeleteTable(db, p);
190}
191
192/*
193** Move the given table to the pending DROP TABLE queue if it has
194** been committed. If this table was never committed, then just
195** delete it. Do the same for all its indices.
196**
197** Table on the drop queue are not actually deleted until a COMMIT
198** statement is executed. If a ROLLBACK occurs instead of a COMMIT,
199** then the tables on the drop queue are moved back into the main
200** hash table.
201*/
drhda9e0342002-01-10 14:31:48 +0000202static void sqlitePendingDropTable(sqlite *db, Table *pTbl){
drh74e24cd2002-01-09 03:19:59 +0000203 if( !pTbl->isCommit ){
204 sqliteUnlinkAndDeleteTable(db, pTbl);
205 }else{
206 Table *pOld;
207 Index *pIndex, *pNext;
208 pOld = sqliteHashInsert(&db->tblHash, pTbl->zName, strlen(pTbl->zName)+1,0);
209 assert( pOld==pTbl );
210 sqliteHashInsert(&db->tblDrop, pTbl, 0, pTbl);
211 for(pIndex = pTbl->pIndex; pIndex; pIndex=pNext){
212 pNext = pIndex->pNext;
213 sqlitePendingDropIndex(db, pIndex);
214 }
215 }
drh5edc3122001-09-13 21:53:09 +0000216}
217
218/*
drh5e00f6c2001-09-13 13:46:56 +0000219** Check all Tables and Indexes in the internal hash table and commit
220** any additions or deletions to those hash tables.
221**
222** When executing CREATE TABLE and CREATE INDEX statements, the Table
223** and Index structures are created and added to the hash tables, but
224** the "isCommit" field is not set. This routine sets those fields.
drh74e24cd2002-01-09 03:19:59 +0000225** When executing DROP TABLE and DROP INDEX, the table or index structures
226** are moved out of tblHash and idxHash into tblDrop and idxDrop. This
227** routine deletes the structure in tblDrop and idxDrop.
drh5e00f6c2001-09-13 13:46:56 +0000228**
229** See also: sqliteRollbackInternalChanges()
230*/
231void sqliteCommitInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000232 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000233 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drh50e5dad2001-09-15 00:57:28 +0000234 db->schema_cookie = db->next_cookie;
drhbeae3192001-09-22 18:12:08 +0000235 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
236 Table *pTable = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000237 pTable->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000238 }
drh74e24cd2002-01-09 03:19:59 +0000239 for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
drhbeae3192001-09-22 18:12:08 +0000240 Table *pTable = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000241 sqliteDeleteTable(db, pTable);
drhbeae3192001-09-22 18:12:08 +0000242 }
drh74e24cd2002-01-09 03:19:59 +0000243 sqliteHashClear(&db->tblDrop);
drhbeae3192001-09-22 18:12:08 +0000244 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
drh4a324312001-12-21 14:30:42 +0000245 Index *pIndex = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000246 pIndex->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000247 }
drh74e24cd2002-01-09 03:19:59 +0000248 while( (pElem=sqliteHashFirst(&db->idxDrop))!=0 ){
drhbeae3192001-09-22 18:12:08 +0000249 Index *pIndex = sqliteHashData(pElem);
250 sqliteUnlinkAndDeleteIndex(db, pIndex);
251 }
drh74e24cd2002-01-09 03:19:59 +0000252 sqliteHashClear(&db->idxDrop);
danielk1977c3f9bad2002-05-15 08:30:12 +0000253
254 /* Set the commit flag on all triggers added this transaction */
255 for(pElem=sqliteHashFirst(&db->trigHash); pElem; pElem=sqliteHashNext(pElem)){
256 Trigger *pTrigger = sqliteHashData(pElem);
257 pTrigger->isCommit = 1;
258 }
259
260 /* Delete the structures for triggers removed this transaction */
261 pElem = sqliteHashFirst(&db->trigDrop);
danielk1977f29ce552002-05-19 23:43:12 +0000262 while( pElem ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000263 Trigger *pTrigger = sqliteHashData(pElem);
264 sqliteDeleteTrigger(pTrigger);
265 pElem = sqliteHashNext(pElem);
266 }
267 sqliteHashClear(&db->trigDrop);
268
drh5e00f6c2001-09-13 13:46:56 +0000269 db->flags &= ~SQLITE_InternChanges;
270}
271
272/*
273** This routine runs when one or more CREATE TABLE, CREATE INDEX,
drhf57b3392001-10-08 13:22:32 +0000274** DROP TABLE, or DROP INDEX statements gets rolled back. The
drh5e00f6c2001-09-13 13:46:56 +0000275** additions or deletions of Table and Index structures in the
276** internal hash tables are undone.
277**
278** See also: sqliteCommitInternalChanges()
279*/
280void sqliteRollbackInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000281 Hash toDelete;
282 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000283 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drhbeae3192001-09-22 18:12:08 +0000284 sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
drh50e5dad2001-09-15 00:57:28 +0000285 db->next_cookie = db->schema_cookie;
drhbeae3192001-09-22 18:12:08 +0000286 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
287 Table *pTable = sqliteHashData(pElem);
288 if( !pTable->isCommit ){
289 sqliteHashInsert(&toDelete, pTable, 0, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000290 }
291 }
drhbeae3192001-09-22 18:12:08 +0000292 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
293 Table *pTable = sqliteHashData(pElem);
294 sqliteUnlinkAndDeleteTable(db, pTable);
295 }
296 sqliteHashClear(&toDelete);
drh74e24cd2002-01-09 03:19:59 +0000297 for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
298 Table *pOld, *p = sqliteHashData(pElem);
299 assert( p->isCommit );
300 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
301 assert( pOld==0 || pOld==p );
302 }
303 sqliteHashClear(&db->tblDrop);
drhbeae3192001-09-22 18:12:08 +0000304 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
drh4a324312001-12-21 14:30:42 +0000305 Index *pIndex = sqliteHashData(pElem);
drhbeae3192001-09-22 18:12:08 +0000306 if( !pIndex->isCommit ){
307 sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000308 }
309 }
drhbeae3192001-09-22 18:12:08 +0000310 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
311 Index *pIndex = sqliteHashData(pElem);
312 sqliteUnlinkAndDeleteIndex(db, pIndex);
313 }
314 sqliteHashClear(&toDelete);
drh74e24cd2002-01-09 03:19:59 +0000315 for(pElem=sqliteHashFirst(&db->idxDrop); pElem; pElem=sqliteHashNext(pElem)){
316 Index *pOld, *p = sqliteHashData(pElem);
317 assert( p->isCommit );
318 p->isDropped = 0;
319 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, p);
320 assert( pOld==0 || pOld==p );
321 }
322 sqliteHashClear(&db->idxDrop);
danielk1977c3f9bad2002-05-15 08:30:12 +0000323
324 /* Remove any triggers that haven't been commited yet */
325 for(pElem = sqliteHashFirst(&db->trigHash); pElem;
danielk1977f29ce552002-05-19 23:43:12 +0000326 pElem = (pElem?sqliteHashNext(pElem):0)){
drh9adf9ac2002-05-15 11:44:13 +0000327 Trigger *pTrigger = sqliteHashData(pElem);
328 if( !pTrigger->isCommit ){
329 Table *pTbl = sqliteFindTable(db, pTrigger->table);
330 if( pTbl ){
331 if( pTbl->pTrigger == pTrigger ){
332 pTbl->pTrigger = pTrigger->pNext;
333 }else{
334 Trigger *cc = pTbl->pTrigger;
335 while( cc ){
danielk1977f29ce552002-05-19 23:43:12 +0000336 if( cc->pNext == pTrigger ){
drh9adf9ac2002-05-15 11:44:13 +0000337 cc->pNext = cc->pNext->pNext;
338 break;
339 }
340 cc = cc->pNext;
341 }
342 assert(cc);
343 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000344 }
345 sqliteHashInsert(&db->trigHash, pTrigger->name,
drh9adf9ac2002-05-15 11:44:13 +0000346 1 + strlen(pTrigger->name), 0);
danielk1977c3f9bad2002-05-15 08:30:12 +0000347 sqliteDeleteTrigger(pTrigger);
348 pElem = sqliteHashFirst(&db->trigHash);
349 }
350 }
351
352 /* Any triggers that were dropped - put 'em back in place */
353 for(pElem = sqliteHashFirst(&db->trigDrop); pElem;
danielk1977f29ce552002-05-19 23:43:12 +0000354 pElem = sqliteHashNext(pElem)){
355 Trigger *pTrigger = sqliteHashData(pElem);
356 Table *pTbl = sqliteFindTable(db, pTrigger->table);
danielk1977c3f9bad2002-05-15 08:30:12 +0000357 sqliteHashInsert(&db->trigHash, pTrigger->name,
drh9adf9ac2002-05-15 11:44:13 +0000358 strlen(pTrigger->name) + 1, pTrigger);
danielk1977f29ce552002-05-19 23:43:12 +0000359 pTrigger->pNext = pTbl->pTrigger;
360 pTbl->pTrigger = pTrigger;
danielk1977c3f9bad2002-05-15 08:30:12 +0000361 }
362
363 sqliteHashClear(&db->trigDrop);
drh5e00f6c2001-09-13 13:46:56 +0000364 db->flags &= ~SQLITE_InternChanges;
365}
366
367/*
drh1ccde152000-06-17 13:12:39 +0000368** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000369**
370** Space to hold the name is obtained from sqliteMalloc() and must
371** be freed by the calling function.
372*/
drhcce7d172000-05-31 15:34:51 +0000373char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000374 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000375 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000376 return zName;
377}
378
379/*
380** Begin constructing a new table representation in memory. This is
381** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000382** to a CREATE TABLE statement. In particular, this routine is called
383** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000384** pStart token is the CREATE and pName is the table name. The isTemp
385** flag is true if the "TEMP" or "TEMPORARY" keyword occurs in between
386** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000387**
drhf57b3392001-10-08 13:22:32 +0000388** The new table record is initialized and put in pParse->pNewTable.
389** As more of the CREATE TABLE statement is parsed, additional action
390** routines will be called to add more information to this record.
391** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
392** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000393*/
drhf57b3392001-10-08 13:22:32 +0000394void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){
drh75897232000-05-29 14:26:00 +0000395 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000396 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000397 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000398 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000399 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000400
401 pParse->sFirstToken = *pStart;
402 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000403 if( zName==0 ) return;
drhf57b3392001-10-08 13:22:32 +0000404
405 /* Before trying to create a temporary table, make sure the Btree for
406 ** holding temporary tables is open.
407 */
408 if( isTemp && db->pBeTemp==0 ){
409 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
410 if( rc!=SQLITE_OK ){
411 sqliteSetNString(&pParse->zErrMsg, "unable to open a temporary database "
412 "file for storing temporary tables", 0);
413 pParse->nErr++;
414 return;
415 }
416 if( db->flags & SQLITE_InTrans ){
417 rc = sqliteBtreeBeginTrans(db->pBeTemp);
418 if( rc!=SQLITE_OK ){
419 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000420 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000421 pParse->nErr++;
422 return;
423 }
424 }
425 }
426
427 /* Make sure the new table name does not collide with an existing
428 ** index or table name. Issue an error message if it does.
429 **
430 ** If we are re-reading the sqlite_master table because of a schema
431 ** change and a new permanent table is found whose name collides with
432 ** an existing temporary table, then ignore the new permanent table.
433 ** We will continue parsing, but the pParse->nameClash flag will be set
434 ** so we will know to discard the table record once parsing has finished.
435 */
drhbe0072d2001-09-13 14:46:09 +0000436 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000437 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000438 if( pTable->isTemp && pParse->initFlag ){
439 pParse->nameClash = 1;
440 }else{
441 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
442 " already exists", 0, 0);
443 sqliteFree(zName);
444 pParse->nErr++;
445 return;
446 }
447 }else{
448 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000449 }
drhf57b3392001-10-08 13:22:32 +0000450 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
451 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000452 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
453 zName, 0);
drh75897232000-05-29 14:26:00 +0000454 sqliteFree(zName);
455 pParse->nErr++;
456 return;
457 }
458 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000459 if( pTable==0 ){
460 sqliteFree(zName);
461 return;
462 }
drh75897232000-05-29 14:26:00 +0000463 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000464 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000465 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000466 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000467 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000468 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000469 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000470 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000471
472 /* Begin generating the code that will insert the table record into
473 ** the SQLITE_MASTER table. Note in particular that we must go ahead
474 ** and allocate the record number for the table entry now. Before any
475 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
476 ** indices to be created and the table record must come before the
477 ** indices. Hence, the record number for the table must be allocated
478 ** now.
479 */
drhadbca9c2001-09-27 15:11:53 +0000480 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drh1c928532002-01-31 15:54:21 +0000481 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +0000482 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000483 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
484 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drh99fcd712001-10-13 01:06:47 +0000485 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
486 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
drh17f71932002-02-21 12:01:27 +0000487 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
488 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
489 sqliteVdbeAddOp(v, OP_String, 0, 0);
490 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000491 }
drh5e00f6c2001-09-13 13:46:56 +0000492 }
drh75897232000-05-29 14:26:00 +0000493}
494
495/*
496** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000497**
498** The parser calls this routine once for each column declaration
499** in a CREATE TABLE statement. sqliteStartTable() gets called
500** first to get things going. Then this routine is called for each
501** column.
drh75897232000-05-29 14:26:00 +0000502*/
503void sqliteAddColumn(Parse *pParse, Token *pName){
504 Table *p;
505 char **pz;
506 if( (p = pParse->pNewTable)==0 ) return;
507 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000508 Column *aNew;
509 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
510 if( aNew==0 ) return;
511 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000512 }
drh7020f652000-06-03 18:06:52 +0000513 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
514 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000515 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000516 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000517}
518
519/*
drh382c0242001-10-06 16:33:02 +0000520** This routine is called by the parser while in the middle of
521** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
522** been seen on a column. This routine sets the notNull flag on
523** the column currently under construction.
524*/
drh9cfcf5d2002-01-29 18:41:24 +0000525void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000526 Table *p;
527 int i;
528 if( (p = pParse->pNewTable)==0 ) return;
529 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000530 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000531}
532
533/*
534** This routine is called by the parser while in the middle of
535** parsing a CREATE TABLE statement. The pFirst token is the first
536** token in the sequence of tokens that describe the type of the
537** column currently under construction. pLast is the last token
538** in the sequence. Use this information to construct a string
539** that contains the typename of the column and store that string
540** in zType.
541*/
542void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
543 Table *p;
544 int i, j;
545 int n;
546 char *z, **pz;
547 if( (p = pParse->pNewTable)==0 ) return;
548 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000549 if( i<0 ) return;
drh382c0242001-10-06 16:33:02 +0000550 pz = &p->aCol[i].zType;
drh5a2c2c22001-11-21 02:21:11 +0000551 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000552 sqliteSetNString(pz, pFirst->z, n, 0);
553 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000554 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000555 for(i=j=0; z[i]; i++){
556 int c = z[i];
557 if( isspace(c) ) continue;
558 z[j++] = c;
559 }
560 z[j] = 0;
561}
562
563/*
drh7020f652000-06-03 18:06:52 +0000564** The given token is the default value for the last column added to
565** the table currently under construction. If "minusFlag" is true, it
566** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000567**
568** This routine is called by the parser while in the middle of
569** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000570*/
571void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
572 Table *p;
573 int i;
574 char **pz;
575 if( (p = pParse->pNewTable)==0 ) return;
576 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000577 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000578 pz = &p->aCol[i].zDflt;
579 if( minusFlag ){
580 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
581 }else{
582 sqliteSetNString(pz, pVal->z, pVal->n, 0);
583 }
584 sqliteDequote(*pz);
585}
586
587/*
drh4a324312001-12-21 14:30:42 +0000588** Designate the PRIMARY KEY for the table. pList is a list of names
589** of columns that form the primary key. If pList is NULL, then the
590** most recently added column of the table is the primary key.
591**
592** A table can have at most one primary key. If the table already has
593** a primary key (and this is the second primary key) then create an
594** error.
595**
596** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
597** then we will try to use that column as the row id. (Exception:
598** For backwards compatibility with older databases, do not do this
599** if the file format version number is less than 1.) Set the Table.iPKey
600** field of the table under construction to be the index of the
601** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
602** no INTEGER PRIMARY KEY.
603**
604** If the key is not an INTEGER PRIMARY KEY, then create a unique
605** index for the key. No index is created for INTEGER PRIMARY KEYs.
606*/
drh9cfcf5d2002-01-29 18:41:24 +0000607void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000608 Table *pTab = pParse->pNewTable;
609 char *zType = 0;
610 int iCol = -1;
611 if( pTab==0 ) return;
612 if( pTab->hasPrimKey ){
613 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
614 "\" has more than one primary key", 0);
615 pParse->nErr++;
616 return;
617 }
618 pTab->hasPrimKey = 1;
619 if( pList==0 ){
620 iCol = pTab->nCol - 1;
621 }else if( pList->nId==1 ){
622 for(iCol=0; iCol<pTab->nCol; iCol++){
623 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
624 }
625 }
626 if( iCol>=0 && iCol<pTab->nCol ){
627 zType = pTab->aCol[iCol].zType;
628 }
629 if( pParse->db->file_format>=1 &&
630 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
631 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000632 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000633 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000634 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drh4a324312001-12-21 14:30:42 +0000635 }
636}
637
638/*
drh50e5dad2001-09-15 00:57:28 +0000639** Come up with a new random value for the schema cookie. Make sure
640** the new value is different from the old.
641**
642** The schema cookie is used to determine when the schema for the
643** database changes. After each schema change, the cookie value
644** changes. When a process first reads the schema it records the
645** cookie. Thereafter, whenever it goes to access the database,
646** it checks the cookie to make sure the schema has not changed
647** since it was last read.
648**
649** This plan is not completely bullet-proof. It is possible for
650** the schema to change multiple times and for the cookie to be
651** set back to prior value. But schema changes are infrequent
652** and the probability of hitting the same cookie value is only
653** 1 chance in 2^32. So we're safe enough.
654*/
drhdc379452002-05-15 12:45:43 +0000655void sqliteChangeCookie(sqlite *db){
drh50e5dad2001-09-15 00:57:28 +0000656 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000657 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000658 db->flags |= SQLITE_InternChanges;
659 }
660}
661
662/*
drh969fa7c2002-02-18 18:30:32 +0000663** Measure the number of characters needed to output the given
664** identifier. The number returned includes any quotes used
665** but does not include the null terminator.
666*/
667static int identLength(const char *z){
668 int n;
drh17f71932002-02-21 12:01:27 +0000669 int needQuote = 0;
670 for(n=0; *z; n++, z++){
671 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000672 }
drh17f71932002-02-21 12:01:27 +0000673 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000674}
675
676/*
677** Write an identifier onto the end of the given string. Add
678** quote characters as needed.
679*/
680static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000681 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000682 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000683 for(j=0; zIdent[j]; j++){
684 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
685 }
686 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
687 || sqliteKeywordCode(zIdent, j)!=TK_ID;
688 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000689 for(j=0; zIdent[j]; j++){
690 z[i++] = zIdent[j];
691 if( zIdent[j]=='\'' ) z[i++] = '\'';
692 }
drh17f71932002-02-21 12:01:27 +0000693 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000694 z[i] = 0;
695 *pIdx = i;
696}
697
698/*
699** Generate a CREATE TABLE statement appropriate for the given
700** table. Memory to hold the text of the statement is obtained
701** from sqliteMalloc() and must be freed by the calling function.
702*/
703static char *createTableStmt(Table *p){
704 int i, k, n;
705 char *zStmt;
706 char *zSep, *zSep2, *zEnd;
707 n = 0;
708 for(i=0; i<p->nCol; i++){
709 n += identLength(p->aCol[i].zName);
710 }
711 n += identLength(p->zName);
712 if( n<40 ){
713 zSep = "";
714 zSep2 = ",";
715 zEnd = ")";
716 }else{
717 zSep = "\n ";
718 zSep2 = ",\n ";
719 zEnd = "\n)";
720 }
721 n += 25 + 6*p->nCol;
722 zStmt = sqliteMalloc( n );
723 if( zStmt==0 ) return 0;
724 assert( !p->isTemp );
725 strcpy(zStmt, "CREATE TABLE ");
726 k = strlen(zStmt);
727 identPut(zStmt, &k, p->zName);
728 zStmt[k++] = '(';
729 for(i=0; i<p->nCol; i++){
730 strcpy(&zStmt[k], zSep);
731 k += strlen(&zStmt[k]);
732 zSep = zSep2;
733 identPut(zStmt, &k, p->aCol[i].zName);
734 }
735 strcpy(&zStmt[k], zEnd);
736 return zStmt;
737}
738
739/*
drh75897232000-05-29 14:26:00 +0000740** This routine is called to report the final ")" that terminates
741** a CREATE TABLE statement.
742**
drhf57b3392001-10-08 13:22:32 +0000743** The table structure that other action routines have been building
744** is added to the internal hash tables, assuming no errors have
745** occurred.
drh75897232000-05-29 14:26:00 +0000746**
drh1ccde152000-06-17 13:12:39 +0000747** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000748** unless this is a temporary table or initFlag==1. When initFlag==1,
749** it means we are reading the sqlite_master table because we just
750** connected to the database or because the sqlite_master table has
751** recently changes, so the entry for this table already exists in
752** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000753**
754** If the pSelect argument is not NULL, it means that this routine
755** was called to create a table generated from a
756** "CREATE TABLE ... AS SELECT ..." statement. The column names of
757** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000758*/
drh969fa7c2002-02-18 18:30:32 +0000759void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000760 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000761 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000762
drh969fa7c2002-02-18 18:30:32 +0000763 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000764 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000765 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000766
drhf57b3392001-10-08 13:22:32 +0000767 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000768 */
drhad75e982001-10-09 04:19:46 +0000769 assert( pParse->nameClash==0 || pParse->initFlag==1 );
drhf57b3392001-10-08 13:22:32 +0000770 if( pParse->explain==0 && pParse->nameClash==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000771 Table *pOld;
772 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
773 if( pOld ){
drh74e24cd2002-01-09 03:19:59 +0000774 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh6d4abfb2001-10-22 02:58:08 +0000775 return;
776 }
drh75897232000-05-29 14:26:00 +0000777 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000778 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000779 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000780 }
781
drh969fa7c2002-02-18 18:30:32 +0000782 /* If the table is generated from a SELECT, then construct the
783 ** list of columns and the text of the table.
784 */
785 if( pSelect ){
786 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000787 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000788 assert( p->aCol==0 );
789 p->nCol = pSelTab->nCol;
790 p->aCol = pSelTab->aCol;
791 pSelTab->nCol = 0;
792 pSelTab->aCol = 0;
793 sqliteDeleteTable(0, pSelTab);
794 }
795
drhd78eeee2001-09-13 16:18:53 +0000796 /* If the initFlag is 1 it means we are reading the SQL off the
797 ** "sqlite_master" table on the disk. So do not write to the disk
drhe3c41372001-09-17 20:25:58 +0000798 ** again. Extract the root page number for the table from the
799 ** pParse->newTnum field. (The page number should have been put
drh382c0242001-10-06 16:33:02 +0000800 ** there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000801 */
802 if( pParse->initFlag ){
803 p->tnum = pParse->newTnum;
804 }
805
drhe3c41372001-09-17 20:25:58 +0000806 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000807 ** in the SQLITE_MASTER table of the database. The record number
808 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000809 **
810 ** If this is a TEMPORARY table, then just create the table. Do not
811 ** make an entry in SQLITE_MASTER.
drh75897232000-05-29 14:26:00 +0000812 */
813 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000814 int n;
drhd8bc7082000-06-07 23:51:50 +0000815 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000816
drhd8bc7082000-06-07 23:51:50 +0000817 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000818 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000819 if( p->pSelect==0 ){
820 /* A regular table */
821 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
822 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
823 }else{
824 /* A view */
825 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
826 }
drh969fa7c2002-02-18 18:30:32 +0000827 p->tnum = 0;
drhf57b3392001-10-08 13:22:32 +0000828 if( !p->isTemp ){
drh17f71932002-02-21 12:01:27 +0000829 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
drh99fcd712001-10-13 01:06:47 +0000830 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +0000831 if( p->pSelect==0 ){
832 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
833 }else{
834 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
835 }
drh99fcd712001-10-13 01:06:47 +0000836 sqliteVdbeAddOp(v, OP_String, 0, 0);
837 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
838 sqliteVdbeAddOp(v, OP_String, 0, 0);
839 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
drh969fa7c2002-02-18 18:30:32 +0000840 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
841 sqliteVdbeAddOp(v, OP_String, 0, 0);
842 if( pSelect ){
843 char *z = createTableStmt(p);
844 n = z ? strlen(z) : 0;
845 sqliteVdbeChangeP3(v, -1, z, n);
846 sqliteFree(z);
847 }else{
848 assert( pEnd!=0 );
849 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
850 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
851 }
drh99fcd712001-10-13 01:06:47 +0000852 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +0000853 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhdc379452002-05-15 12:45:43 +0000854 sqliteChangeCookie(db);
drh603240c2002-03-05 01:11:12 +0000855 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
856 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh99fcd712001-10-13 01:06:47 +0000857 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000858 }
drh969fa7c2002-02-18 18:30:32 +0000859 if( pSelect ){
860 int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
861 sqliteVdbeAddOp(v, op, 1, 0);
862 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000863 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000864 }
drh1c928532002-01-31 15:54:21 +0000865 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000866 }
867}
868
869/*
drha76b5df2002-02-23 02:32:10 +0000870** The parser calls this routine in order to create a new VIEW
871*/
872void sqliteCreateView(
873 Parse *pParse, /* The parsing context */
874 Token *pBegin, /* The CREATE token that begins the statement */
875 Token *pName, /* The token that holds the name of the view */
876 Select *pSelect /* A SELECT statement that will become the new view */
877){
878 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000879 Table *p;
drh4ff6dfa2002-03-03 23:06:00 +0000880 const char *z;
drha76b5df2002-02-23 02:32:10 +0000881 int n, offset;
882
883 sqliteStartTable(pParse, pBegin, pName, 0);
884 p = pParse->pNewTable;
drh417be792002-03-03 18:59:40 +0000885 if( p==0 ){
886 sqliteSelectDelete(pSelect);
887 return;
888 }
drh0f18b452002-05-08 21:30:15 +0000889 /* Ignore ORDER BY clauses on a SELECT */
890 if( pSelect->pOrderBy ){
891 sqliteExprListDelete(pSelect->pOrderBy);
892 pSelect->pOrderBy = 0;
893 }
drha76b5df2002-02-23 02:32:10 +0000894 p->pSelect = pSelect;
drh417be792002-03-03 18:59:40 +0000895 if( !pParse->initFlag ){
896 if( sqliteViewGetColumnNames(pParse, p) ){
897 return;
898 }
899 }
drha76b5df2002-02-23 02:32:10 +0000900 sEnd = pParse->sLastToken;
901 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
902 sEnd.z += sEnd.n;
903 }
904 sEnd.n = 0;
905 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000906 z = pBegin->z;
907 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
908 sEnd.z = &z[n-1];
909 sEnd.n = 1;
910 z = p->pSelect->zSelect = sqliteStrNDup(z, n);
drh417be792002-03-03 18:59:40 +0000911 if( z ){
912 offset = ((int)z) - (int)pBegin->z;
913 sqliteSelectMoveStrings(p->pSelect, offset);
914 sqliteEndTable(pParse, &sEnd, 0);
915 }
drha76b5df2002-02-23 02:32:10 +0000916 return;
drh417be792002-03-03 18:59:40 +0000917}
drha76b5df2002-02-23 02:32:10 +0000918
drh417be792002-03-03 18:59:40 +0000919/*
920** The Table structure pTable is really a VIEW. Fill in the names of
921** the columns of the view in the pTable structure. Return the number
922** of errors. If an error is seen leave an error message in pPare->zErrMsg.
923*/
924int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
925 ExprList *pEList;
926 Select *pSel;
927 Table *pSelTab;
928 int nErr = 0;
929
930 assert( pTable );
931
932 /* A positive nCol means the columns names for this view are
933 ** already known.
934 */
935 if( pTable->nCol>0 ) return 0;
936
937 /* A negative nCol is a special marker meaning that we are currently
938 ** trying to compute the column names. If we enter this routine with
939 ** a negative nCol, it means two or more views form a loop, like this:
940 **
941 ** CREATE VIEW one AS SELECT * FROM two;
942 ** CREATE VIEW two AS SELECT * FROM one;
943 */
944 if( pTable->nCol<0 ){
945 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
946 " is circularly defined", 0);
947 pParse->nErr++;
948 return 1;
949 }
950
951 /* If we get this far, it means we need to compute the table names.
952 */
953 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
954 pSel = pTable->pSelect;
955
956 /* Note that the call to sqliteResultSetOfSelect() will expand any
957 ** "*" elements in this list. But we will need to restore the list
958 ** back to its original configuration afterwards, so we save a copy of
959 ** the original in pEList.
960 */
961 pEList = pSel->pEList;
962 pSel->pEList = sqliteExprListDup(pEList);
963 if( pSel->pEList==0 ){
964 pSel->pEList = pEList;
965 return 1; /* Malloc failed */
966 }
967 pTable->nCol = -1;
968 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
969 if( pSelTab ){
970 assert( pTable->aCol==0 );
971 pTable->nCol = pSelTab->nCol;
972 pTable->aCol = pSelTab->aCol;
973 pSelTab->nCol = 0;
974 pSelTab->aCol = 0;
975 sqliteDeleteTable(0, pSelTab);
976 pParse->db->flags |= SQLITE_UnresetViews;
977 }else{
978 pTable->nCol = 0;
979 nErr++;
980 }
981 sqliteSelectUnbind(pSel);
982 sqliteExprListDelete(pSel->pEList);
983 pSel->pEList = pEList;
984 return nErr;
985}
986
987/*
988** Clear the column names from the VIEW pTable.
989**
990** This routine is called whenever any other table or view is modified.
991** The view passed into this routine might depend directly or indirectly
992** on the modified or deleted table so we need to clear the old column
993** names so that they will be recomputed.
994*/
995static void sqliteViewResetColumnNames(Table *pTable){
996 int i;
997 if( pTable==0 || pTable->pSelect==0 ) return;
998 if( pTable->nCol==0 ) return;
999 for(i=0; i<pTable->nCol; i++){
1000 sqliteFree(pTable->aCol[i].zName);
1001 sqliteFree(pTable->aCol[i].zDflt);
1002 sqliteFree(pTable->aCol[i].zType);
1003 }
1004 sqliteFree(pTable->aCol);
1005 pTable->aCol = 0;
1006 pTable->nCol = 0;
1007}
1008
1009/*
1010** Clear the column names from every VIEW.
1011*/
1012void sqliteViewResetAll(sqlite *db){
1013 HashElem *i;
1014 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
1015 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
1016 Table *pTab = sqliteHashData(i);
1017 if( pTab->pSelect ){
1018 sqliteViewResetColumnNames(pTab);
1019 }
1020 }
1021 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001022}
1023
1024/*
drh75897232000-05-29 14:26:00 +00001025** Given a token, look up a table with that name. If not found, leave
1026** an error for the parser to find and return NULL.
1027*/
drhcce7d172000-05-31 15:34:51 +00001028Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001029 char *zName;
1030 Table *pTab;
1031 zName = sqliteTableNameFromToken(pTok);
1032 if( zName==0 ) return 0;
1033 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +00001034 sqliteFree(zName);
1035 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001036 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1037 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001038 pParse->nErr++;
1039 }
1040 return pTab;
1041}
1042
1043/*
1044** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001045** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001046*/
drh4ff6dfa2002-03-03 23:06:00 +00001047void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001048 Table *pTable;
drh75897232000-05-29 14:26:00 +00001049 Vdbe *v;
1050 int base;
drh5edc3122001-09-13 21:53:09 +00001051 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001052
drhdaffd0e2001-04-11 14:28:42 +00001053 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001054 pTable = sqliteTableFromToken(pParse, pName);
1055 if( pTable==0 ) return;
1056 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001057 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1058 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001059 pParse->nErr++;
1060 return;
1061 }
drh4ff6dfa2002-03-03 23:06:00 +00001062 if( isView && pTable->pSelect==0 ){
1063 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1064 pTable->zName, 0);
1065 pParse->nErr++;
1066 return;
1067 }
1068 if( !isView && pTable->pSelect ){
1069 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1070 pTable->zName, 0);
1071 pParse->nErr++;
1072 return;
1073 }
drh75897232000-05-29 14:26:00 +00001074
drh1ccde152000-06-17 13:12:39 +00001075 /* Generate code to remove the table from the master table
1076 ** on disk.
1077 */
drhd8bc7082000-06-07 23:51:50 +00001078 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001079 if( v ){
1080 static VdbeOp dropTable[] = {
drhecdc7532001-09-23 02:35:53 +00001081 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +00001082 { OP_Rewind, 0, ADDR(9), 0},
drhd78eeee2001-09-13 16:18:53 +00001083 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +00001084 { OP_MemStore, 1, 1, 0},
1085 { OP_MemLoad, 1, 0, 0}, /* 4 */
drhe3c41372001-09-17 20:25:58 +00001086 { OP_Column, 0, 2, 0},
drh6b563442001-11-07 16:48:26 +00001087 { OP_Ne, 0, ADDR(8), 0},
drh75897232000-05-29 14:26:00 +00001088 { OP_Delete, 0, 0, 0},
drh6b563442001-11-07 16:48:26 +00001089 { OP_Next, 0, ADDR(4), 0}, /* 8 */
drh603240c2002-03-05 01:11:12 +00001090 { OP_Integer, 0, 0, 0}, /* 9 */
1091 { OP_SetCookie, 0, 0, 0},
drh75897232000-05-29 14:26:00 +00001092 { OP_Close, 0, 0, 0},
1093 };
1094 Index *pIdx;
drh1c928532002-01-31 15:54:21 +00001095 sqliteBeginWriteOperation(pParse);
danielk1977c3f9bad2002-05-15 08:30:12 +00001096 /* Drop all triggers associated with the table being dropped */
drhdc379452002-05-15 12:45:43 +00001097 while( pTable->pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001098 Token tt;
1099 tt.z = pTable->pTrigger->name;
1100 tt.n = strlen(pTable->pTrigger->name);
1101 sqliteDropTrigger(pParse, &tt, 1);
1102 }
drhf57b3392001-10-08 13:22:32 +00001103 if( !pTable->isTemp ){
1104 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh0a36c572002-02-18 22:49:59 +00001105 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
drhdc379452002-05-15 12:45:43 +00001106 sqliteChangeCookie(db);
drhf57b3392001-10-08 13:22:32 +00001107 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
1108 }
drh4ff6dfa2002-03-03 23:06:00 +00001109 if( !isView ){
1110 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1111 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1112 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1113 }
drh5e00f6c2001-09-13 13:46:56 +00001114 }
drh1c928532002-01-31 15:54:21 +00001115 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001116 }
1117
drh74e24cd2002-01-09 03:19:59 +00001118 /* Move the table (and all its indices) to the pending DROP queue.
1119 ** Or, if the table was never committed, just delete it. If the table
1120 ** has been committed and is placed on the pending DROP queue, then the
1121 ** delete will occur when sqliteCommitInternalChanges() executes.
drh75897232000-05-29 14:26:00 +00001122 **
1123 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001124 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001125 */
1126 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +00001127 sqlitePendingDropTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001128 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001129 }
drh417be792002-03-03 18:59:40 +00001130 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001131}
1132
1133/*
1134** Create a new index for an SQL table. pIndex is the name of the index
1135** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001136** be NULL for a primary key or an index that is created to satisfy a
1137** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001138** as the table to be indexed. pParse->pNewTable is a table that is
1139** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001140**
drh382c0242001-10-06 16:33:02 +00001141** pList is a list of columns to be indexed. pList will be NULL if this
1142** is a primary key or unique-constraint on the most recent column added
1143** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001144*/
1145void sqliteCreateIndex(
1146 Parse *pParse, /* All information about this parse */
1147 Token *pName, /* Name of the index. May be NULL */
1148 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001149 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001150 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001151 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1152 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1153){
1154 Table *pTab; /* Table to be indexed */
1155 Index *pIndex; /* The index to be created */
1156 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001157 int i, j;
drhf57b3392001-10-08 13:22:32 +00001158 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001159 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001160 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001161
drhdaffd0e2001-04-11 14:28:42 +00001162 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1163
drh75897232000-05-29 14:26:00 +00001164 /*
1165 ** Find the table that is to be indexed. Return early if not found.
1166 */
1167 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001168 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001169 pTab = sqliteTableFromToken(pParse, pTable);
1170 }else{
drhe3c41372001-09-17 20:25:58 +00001171 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001172 pTab = pParse->pNewTable;
1173 }
1174 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1175 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001176 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1177 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001178 pParse->nErr++;
1179 goto exit_create_index;
1180 }
drha76b5df2002-02-23 02:32:10 +00001181 if( pTab->pSelect ){
1182 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1183 pParse->nErr++;
1184 goto exit_create_index;
1185 }
drh75897232000-05-29 14:26:00 +00001186
drhf57b3392001-10-08 13:22:32 +00001187 /* If this index is created while re-reading the schema from sqlite_master
1188 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001189 ** only mean that the table that this index is really associated with is
1190 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001191 ** Since its table has been suppressed, we need to also suppress the
1192 ** index.
1193 */
1194 if( pParse->initFlag && pTab->isTemp ){
1195 goto exit_create_index;
1196 }
1197
drh75897232000-05-29 14:26:00 +00001198 /*
1199 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001200 ** index or table with the same name.
1201 **
1202 ** Exception: If we are reading the names of permanent indices from the
1203 ** sqlite_master table (because some other process changed the schema) and
1204 ** one of the index names collides with the name of a temporary table or
1205 ** index, then we will continue to process this index, but we will not
1206 ** store its name in the hash table. Set the hideName flag to accomplish
1207 ** this.
1208 **
1209 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001210 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1211 ** own name.
drh75897232000-05-29 14:26:00 +00001212 */
1213 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001214 Index *pISameName; /* Another index with the same name */
1215 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001216 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001217 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001218 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1219 if( pISameName->pTable->isTemp && pParse->initFlag ){
1220 hideName = 1;
1221 }else{
1222 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1223 " already exists", 0);
1224 pParse->nErr++;
1225 goto exit_create_index;
1226 }
drhe3c41372001-09-17 20:25:58 +00001227 }
drhf57b3392001-10-08 13:22:32 +00001228 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1229 if( pTSameName->isTemp && pParse->initFlag ){
1230 hideName = 1;
1231 }else{
1232 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1233 zName, 0);
1234 pParse->nErr++;
1235 goto exit_create_index;
1236 }
drhe3c41372001-09-17 20:25:58 +00001237 }
drh75897232000-05-29 14:26:00 +00001238 }else{
drhadbca9c2001-09-27 15:11:53 +00001239 char zBuf[30];
1240 int n;
1241 Index *pLoop;
1242 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1243 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001244 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001245 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001246 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001247 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001248 }
1249
1250 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001251 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001252 ** So create a fake list to simulate this.
1253 */
1254 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001255 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001256 nullId.n = strlen(nullId.z);
1257 pList = sqliteIdListAppend(0, &nullId);
1258 if( pList==0 ) goto exit_create_index;
1259 }
1260
1261 /*
1262 ** Allocate the index structure.
1263 */
drhdcc581c2000-05-30 13:44:19 +00001264 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001265 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001266 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001267 pIndex->aiColumn = (int*)&pIndex[1];
1268 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001269 strcpy(pIndex->zName, zName);
1270 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001271 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001272 pIndex->onError = pIndex->isUnique = onError;
drh75897232000-05-29 14:26:00 +00001273
drh1ccde152000-06-17 13:12:39 +00001274 /* Scan the names of the columns of the table to be indexed and
1275 ** load the column indices into the Index structure. Report an error
1276 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001277 */
1278 for(i=0; i<pList->nId; i++){
1279 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001280 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001281 }
1282 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001283 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001284 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001285 pParse->nErr++;
1286 sqliteFree(pIndex);
1287 goto exit_create_index;
1288 }
drh967e8b72000-06-21 13:59:10 +00001289 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001290 }
1291
1292 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001293 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001294 */
drhf57b3392001-10-08 13:22:32 +00001295 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001296 Index *p;
1297 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1298 if( p ){
1299 assert( p==pIndex ); /* Malloc must have failed */
1300 sqliteFree(pIndex);
1301 goto exit_create_index;
1302 }
drh5e00f6c2001-09-13 13:46:56 +00001303 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001304 }
drh9cfcf5d2002-01-29 18:41:24 +00001305
1306 /* When adding an index to the list of indices for a table, make
1307 ** sure all indices labeled OE_Replace come after all those labeled
1308 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1309 ** and INSERT.
1310 */
1311 if( onError!=OE_Replace || pTab->pIndex==0
1312 || pTab->pIndex->onError==OE_Replace){
1313 pIndex->pNext = pTab->pIndex;
1314 pTab->pIndex = pIndex;
1315 }else{
1316 Index *pOther = pTab->pIndex;
1317 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1318 pOther = pOther->pNext;
1319 }
1320 pIndex->pNext = pOther->pNext;
1321 pOther->pNext = pIndex;
1322 }
drh75897232000-05-29 14:26:00 +00001323
drhd78eeee2001-09-13 16:18:53 +00001324 /* If the initFlag is 1 it means we are reading the SQL off the
1325 ** "sqlite_master" table on the disk. So do not write to the disk
1326 ** again. Extract the table number from the pParse->newTnum field.
1327 */
drhadbca9c2001-09-27 15:11:53 +00001328 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001329 pIndex->tnum = pParse->newTnum;
1330 }
1331
drh75897232000-05-29 14:26:00 +00001332 /* If the initFlag is 0 then create the index on disk. This
1333 ** involves writing the index into the master table and filling in the
1334 ** index with the current table contents.
1335 **
1336 ** The initFlag is 0 when the user first enters a CREATE INDEX
1337 ** command. The initFlag is 1 when a database is opened and
1338 ** CREATE INDEX statements are read out of the master table. In
1339 ** the latter case the index already exists on disk, which is why
1340 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001341 **
1342 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001343 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1344 ** has just been created, it contains no data and the index initialization
1345 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001346 */
drhadbca9c2001-09-27 15:11:53 +00001347 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001348 int n;
drhadbca9c2001-09-27 15:11:53 +00001349 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001350 int lbl1, lbl2;
1351 int i;
drhadbca9c2001-09-27 15:11:53 +00001352 int addr;
drhf57b3392001-10-08 13:22:32 +00001353 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001354
drhd8bc7082000-06-07 23:51:50 +00001355 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001356 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001357 if( pTable!=0 ){
drh1c928532002-01-31 15:54:21 +00001358 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +00001359 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001360 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
1361 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001362 }
drhadbca9c2001-09-27 15:11:53 +00001363 }
drhf57b3392001-10-08 13:22:32 +00001364 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001365 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1366 sqliteVdbeAddOp(v, OP_String, 0, 0);
1367 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1368 sqliteVdbeAddOp(v, OP_String, 0, 0);
1369 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1370 sqliteVdbeAddOp(v, OP_String, 0, 0);
1371 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001372 }
drh99fcd712001-10-13 01:06:47 +00001373 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1374 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001375 pIndex->tnum = 0;
1376 if( pTable ){
drhf57b3392001-10-08 13:22:32 +00001377 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001378 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001379 }else{
drh99fcd712001-10-13 01:06:47 +00001380 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1381 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001382 }
drh5e00f6c2001-09-13 13:46:56 +00001383 }
drhf57b3392001-10-08 13:22:32 +00001384 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001385 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001386 if( pStart && pEnd ){
drh5a2c2c22001-11-21 02:21:11 +00001387 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
drhf57b3392001-10-08 13:22:32 +00001388 sqliteVdbeChangeP3(v, addr, pStart->z, n);
1389 }
drh99fcd712001-10-13 01:06:47 +00001390 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +00001391 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh75897232000-05-29 14:26:00 +00001392 }
drhadbca9c2001-09-27 15:11:53 +00001393 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001394 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1395 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001396 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001397 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1398 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001399 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001400 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001401 }
drh99fcd712001-10-13 01:06:47 +00001402 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001403 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001404 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001405 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001406 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1407 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001408 }
drhadbca9c2001-09-27 15:11:53 +00001409 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001410 if( !isTemp ){
drhdc379452002-05-15 12:45:43 +00001411 sqliteChangeCookie(db);
drh603240c2002-03-05 01:11:12 +00001412 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
1413 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001414 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001415 }
drh1c928532002-01-31 15:54:21 +00001416 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001417 }
drh75897232000-05-29 14:26:00 +00001418 }
1419
drh75897232000-05-29 14:26:00 +00001420 /* Clean up before exiting */
1421exit_create_index:
1422 sqliteIdListDelete(pList);
1423 sqliteFree(zName);
1424 return;
1425}
1426
1427/*
drh74e24cd2002-01-09 03:19:59 +00001428** This routine will drop an existing named index. This routine
1429** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001430*/
1431void sqliteDropIndex(Parse *pParse, Token *pName){
1432 Index *pIndex;
1433 char *zName;
1434 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001435 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001436
drhdaffd0e2001-04-11 14:28:42 +00001437 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001438 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001439 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001440 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001441 sqliteFree(zName);
1442 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001443 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1444 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001445 pParse->nErr++;
1446 return;
1447 }
1448
1449 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001450 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001451 if( v ){
1452 static VdbeOp dropIndex[] = {
drhecdc7532001-09-23 02:35:53 +00001453 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +00001454 { OP_Rewind, 0, ADDR(10),0},
drhd78eeee2001-09-13 16:18:53 +00001455 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +00001456 { OP_MemStore, 1, 1, 0},
1457 { OP_MemLoad, 1, 0, 0}, /* 4 */
drh5e00f6c2001-09-13 13:46:56 +00001458 { OP_Column, 0, 1, 0},
drh6b563442001-11-07 16:48:26 +00001459 { OP_Eq, 0, ADDR(9), 0},
1460 { OP_Next, 0, ADDR(4), 0},
1461 { OP_Goto, 0, ADDR(10),0},
1462 { OP_Delete, 0, 0, 0}, /* 9 */
drh603240c2002-03-05 01:11:12 +00001463 { OP_Integer, 0, 0, 0}, /* 10 */
1464 { OP_SetCookie, 0, 0, 0},
drh75897232000-05-29 14:26:00 +00001465 { OP_Close, 0, 0, 0},
1466 };
1467 int base;
drhf57b3392001-10-08 13:22:32 +00001468 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001469
drh1c928532002-01-31 15:54:21 +00001470 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +00001471 if( !pTab->isTemp ){
1472 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh99fcd712001-10-13 01:06:47 +00001473 sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC);
drhdc379452002-05-15 12:45:43 +00001474 sqliteChangeCookie(db);
drh6b563442001-11-07 16:48:26 +00001475 sqliteVdbeChangeP1(v, base+10, db->next_cookie);
drhf57b3392001-10-08 13:22:32 +00001476 }
drh99fcd712001-10-13 01:06:47 +00001477 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001478 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001479 }
1480
drh74e24cd2002-01-09 03:19:59 +00001481 /* Move the index onto the pending DROP queue. Or, if the index was
1482 ** never committed, just delete it. Indices on the pending DROP queue
1483 ** get deleted by sqliteCommitInternalChanges() when the user executes
1484 ** a COMMIT. Or if a rollback occurs, the elements of the DROP queue
1485 ** are moved back into the main hash table.
drh75897232000-05-29 14:26:00 +00001486 */
1487 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +00001488 sqlitePendingDropIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001489 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001490 }
1491}
1492
1493/*
drh75897232000-05-29 14:26:00 +00001494** Append a new element to the given IdList. Create a new IdList if
1495** need be.
drhdaffd0e2001-04-11 14:28:42 +00001496**
1497** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001498*/
1499IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1500 if( pList==0 ){
1501 pList = sqliteMalloc( sizeof(IdList) );
1502 if( pList==0 ) return 0;
1503 }
1504 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001505 struct IdList_item *a;
1506 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1507 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001508 sqliteIdListDelete(pList);
1509 return 0;
drh75897232000-05-29 14:26:00 +00001510 }
drh6d4abfb2001-10-22 02:58:08 +00001511 pList->a = a;
drh75897232000-05-29 14:26:00 +00001512 }
1513 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1514 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001515 char **pz = &pList->a[pList->nId].zName;
1516 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1517 if( *pz==0 ){
1518 sqliteIdListDelete(pList);
1519 return 0;
1520 }else{
1521 sqliteDequote(*pz);
1522 }
drh75897232000-05-29 14:26:00 +00001523 }
1524 pList->nId++;
1525 return pList;
1526}
1527
1528/*
1529** Add an alias to the last identifier on the given identifier list.
1530*/
1531void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1532 if( pList && pList->nId>0 ){
1533 int i = pList->nId - 1;
1534 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001535 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001536 }
1537}
1538
1539/*
drha76b5df2002-02-23 02:32:10 +00001540** Delete an entire IdList.
drh75897232000-05-29 14:26:00 +00001541*/
1542void sqliteIdListDelete(IdList *pList){
1543 int i;
1544 if( pList==0 ) return;
1545 for(i=0; i<pList->nId; i++){
1546 sqliteFree(pList->a[i].zName);
1547 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001548 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001549 sqliteDeleteTable(0, pList->a[i].pTab);
1550 }
drhff78bd22002-02-27 01:47:11 +00001551 sqliteSelectDelete(pList->a[i].pSelect);
drh75897232000-05-29 14:26:00 +00001552 }
1553 sqliteFree(pList->a);
1554 sqliteFree(pList);
1555}
1556
drh982cef72000-05-30 16:27:03 +00001557/*
1558** The COPY command is for compatibility with PostgreSQL and specificially
1559** for the ability to read the output of pg_dump. The format is as
1560** follows:
1561**
1562** COPY table FROM file [USING DELIMITERS string]
1563**
1564** "table" is an existing table name. We will read lines of code from
1565** file to fill this table with data. File might be "stdin". The optional
1566** delimiter string identifies the field separators. The default is a tab.
1567*/
1568void sqliteCopy(
1569 Parse *pParse, /* The parser context */
1570 Token *pTableName, /* The name of the table into which we will insert */
1571 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001572 Token *pDelimiter, /* Use this as the field delimiter */
1573 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001574){
1575 Table *pTab;
1576 char *zTab;
drh1c928532002-01-31 15:54:21 +00001577 int i;
drh982cef72000-05-30 16:27:03 +00001578 Vdbe *v;
1579 int addr, end;
1580 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001581 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001582
1583 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001584 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001585 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001586 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001587 if( pTab==0 ) goto copy_cleanup;
drhd8bc7082000-06-07 23:51:50 +00001588 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001589 if( v ){
drhf57b3392001-10-08 13:22:32 +00001590 int openOp;
drh663fc632002-02-02 18:49:19 +00001591 sqliteBeginMultiWriteOperation(pParse);
drh99fcd712001-10-13 01:06:47 +00001592 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001593 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001594 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001595 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001596 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1597 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001598 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001599 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1600 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001601 }
drhb419a922002-01-30 16:17:23 +00001602 if( db->flags & SQLITE_CountRows ){
1603 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1604 }
drh982cef72000-05-30 16:27:03 +00001605 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001606 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001607 if( pDelimiter ){
1608 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1609 sqliteVdbeDequoteP3(v, addr);
1610 }else{
1611 sqliteVdbeChangeP3(v, addr, "\t", 1);
1612 }
drh8aff1012001-12-22 14:49:24 +00001613 if( pTab->iPKey>=0 ){
1614 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1615 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1616 }else{
1617 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1618 }
drh982cef72000-05-30 16:27:03 +00001619 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001620 if( i==pTab->iPKey ){
1621 /* The integer primary key column is filled with NULL since its
1622 ** value is always pulled from the record number */
1623 sqliteVdbeAddOp(v, OP_String, 0, 0);
1624 }else{
1625 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1626 }
drh982cef72000-05-30 16:27:03 +00001627 }
drhb419a922002-01-30 16:17:23 +00001628 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1629 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1630 if( (db->flags & SQLITE_CountRows)!=0 ){
1631 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001632 }
drh99fcd712001-10-13 01:06:47 +00001633 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1634 sqliteVdbeResolveLabel(v, end);
1635 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001636 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001637 if( db->flags & SQLITE_CountRows ){
1638 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1639 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1640 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1641 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1642 }
drh982cef72000-05-30 16:27:03 +00001643 }
1644
1645copy_cleanup:
1646 return;
1647}
drhdce2cbe2000-05-31 02:27:49 +00001648
1649/*
1650** The non-standard VACUUM command is used to clean up the database,
1651** collapse free space, etc. It is modelled after the VACUUM command
1652** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001653**
drh1bffb9c2002-02-03 17:37:36 +00001654** In version 1.0.x of SQLite, the VACUUM command would call
1655** gdbm_reorganize() on all the database tables. But beginning
1656** with 2.0.0, SQLite no longer uses GDBM so this command has
1657** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001658*/
1659void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001660 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001661}
drhc4a3c772001-04-04 11:48:57 +00001662
1663/*
1664** Begin a transaction
1665*/
drh1c928532002-01-31 15:54:21 +00001666void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001667 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001668
drhc4a3c772001-04-04 11:48:57 +00001669 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001670 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001671 if( db->flags & SQLITE_InTrans ) return;
drh1c928532002-01-31 15:54:21 +00001672 sqliteBeginWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001673 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001674 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001675}
1676
1677/*
1678** Commit a transaction
1679*/
1680void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001681 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001682
drhc4a3c772001-04-04 11:48:57 +00001683 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001684 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001685 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001686 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001687 sqliteEndWriteOperation(pParse);
1688 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001689}
1690
1691/*
1692** Rollback a transaction
1693*/
1694void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001695 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001696 Vdbe *v;
1697
drhc4a3c772001-04-04 11:48:57 +00001698 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001699 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001700 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001701 v = sqliteGetVdbe(pParse);
1702 if( v ){
drh99fcd712001-10-13 01:06:47 +00001703 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001704 }
drh5e00f6c2001-09-13 13:46:56 +00001705 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001706 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001707}
drhf57b14a2001-09-14 18:54:08 +00001708
1709/*
drh1c928532002-01-31 15:54:21 +00001710** Generate VDBE code that prepares for doing an operation that
drh663fc632002-02-02 18:49:19 +00001711** might change the database. The operation will be atomic in the
1712** sense that it will either do its changes completely or not at
drhdc379452002-05-15 12:45:43 +00001713** all. So there is no need to set a checkpoint is a transaction
drh663fc632002-02-02 18:49:19 +00001714** is already in effect.
drh1c928532002-01-31 15:54:21 +00001715*/
1716void sqliteBeginWriteOperation(Parse *pParse){
1717 Vdbe *v;
1718 v = sqliteGetVdbe(pParse);
1719 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00001720 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00001721 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh1c928532002-01-31 15:54:21 +00001722 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1723 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1724 pParse->schemaVerified = 1;
1725 }
1726}
1727
1728/*
drh663fc632002-02-02 18:49:19 +00001729** Generate VDBE code that prepares for doing an operation that
1730** might change the database. The operation might not be atomic in
1731** the sense that an error may be discovered and the operation might
1732** abort after some changes have been made. If we are in the middle
1733** of a transaction, then this sets a checkpoint. If we are not in
1734** a transaction, then start a transaction.
1735*/
1736void sqliteBeginMultiWriteOperation(Parse *pParse){
1737 Vdbe *v;
1738 v = sqliteGetVdbe(pParse);
1739 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00001740 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00001741 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
1742 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1743 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1744 pParse->schemaVerified = 1;
1745 }else{
1746 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1747 }
1748}
1749
1750/*
drh1c928532002-01-31 15:54:21 +00001751** Generate code that concludes an operation that may have changed
1752** the database. This is a companion function to BeginWriteOperation().
1753** If a transaction was started, then commit it. If a checkpoint was
1754** started then commit that.
1755*/
1756void sqliteEndWriteOperation(Parse *pParse){
1757 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00001758 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00001759 v = sqliteGetVdbe(pParse);
1760 if( v==0 ) return;
1761 if( pParse->db->flags & SQLITE_InTrans ){
1762 /* Do Nothing */
1763 }else{
1764 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
1765 }
1766}
1767
1768
1769/*
drhf57b14a2001-09-14 18:54:08 +00001770** Interpret the given string as a boolean value.
1771*/
1772static int getBoolean(char *z){
1773 static char *azTrue[] = { "yes", "on", "true" };
1774 int i;
1775 if( z[0]==0 ) return 0;
1776 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1777 return atoi(z);
1778 }
1779 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1780 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1781 }
1782 return 0;
1783}
1784
1785/*
1786** Process a pragma statement.
1787**
1788** Pragmas are of this form:
1789**
1790** PRAGMA id = value
1791**
1792** The identifier might also be a string. The value is a string, and
1793** identifier, or a number. If minusFlag is true, then the value is
1794** a number that was preceded by a minus sign.
1795*/
1796void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1797 char *zLeft = 0;
1798 char *zRight = 0;
1799 sqlite *db = pParse->db;
1800
1801 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1802 sqliteDequote(zLeft);
1803 if( minusFlag ){
1804 zRight = 0;
1805 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1806 }else{
1807 zRight = sqliteStrNDup(pRight->z, pRight->n);
1808 sqliteDequote(zRight);
1809 }
1810
drhcd61c282002-03-06 22:01:34 +00001811 /*
1812 ** PRAGMA default_cache_size
1813 ** PRAGMA default_cache_size=N
1814 **
1815 ** The first form reports the current persistent setting for the
1816 ** page cache size. The value returned is the maximum number of
1817 ** pages in the page cache. The second form sets both the current
1818 ** page cache size value and the persistent page cache size value
1819 ** stored in the database file.
1820 **
1821 ** The default cache size is stored in meta-value 2 of page 1 of the
1822 ** database file. The cache size is actually the absolute value of
1823 ** this memory location. The sign of meta-value 2 determines the
1824 ** synchronous setting. A negative value means synchronous is off
1825 ** and a positive value means synchronous is on.
1826 */
1827 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00001828 static VdbeOp getCacheSize[] = {
1829 { OP_ReadCookie, 0, 2, 0},
1830 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00001831 { OP_Dup, 0, 0, 0},
1832 { OP_Integer, 0, 0, 0},
1833 { OP_Ne, 0, 6, 0},
1834 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00001835 { OP_ColumnCount, 1, 0, 0},
1836 { OP_ColumnName, 0, 0, "cache_size"},
1837 { OP_Callback, 1, 0, 0},
1838 };
1839 Vdbe *v = sqliteGetVdbe(pParse);
1840 if( v==0 ) return;
1841 if( pRight->z==pLeft->z ){
1842 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1843 }else{
1844 int addr;
1845 int size = atoi(zRight);
1846 if( size<0 ) size = -size;
1847 sqliteBeginWriteOperation(pParse);
1848 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1849 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
1850 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1851 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1852 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
1853 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
1854 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00001855 db->cache_size = db->cache_size<0 ? -size : size;
1856 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00001857 }
1858 }else
1859
drhcd61c282002-03-06 22:01:34 +00001860 /*
1861 ** PRAGMA cache_size
1862 ** PRAGMA cache_size=N
1863 **
1864 ** The first form reports the current local setting for the
1865 ** page cache size. The local setting can be different from
1866 ** the persistent cache size value that is stored in the database
1867 ** file itself. The value returned is the maximum number of
1868 ** pages in the page cache. The second form sets the local
1869 ** page cache size value. It does not change the persistent
1870 ** cache size stored on the disk so the cache size will revert
1871 ** to its default value when the database is closed and reopened.
1872 ** N should be a positive integer.
1873 */
1874 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1875 static VdbeOp getCacheSize[] = {
1876 { OP_ColumnCount, 1, 0, 0},
1877 { OP_ColumnName, 0, 0, "cache_size"},
1878 { OP_Callback, 1, 0, 0},
1879 };
1880 Vdbe *v = sqliteGetVdbe(pParse);
1881 if( v==0 ) return;
1882 if( pRight->z==pLeft->z ){
1883 int size = db->cache_size;;
1884 if( size<0 ) size = -size;
1885 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1886 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1887 }else{
1888 int size = atoi(zRight);
1889 if( size<0 ) size = -size;
1890 if( db->cache_size<0 ) size = -size;
1891 db->cache_size = size;
1892 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
1893 }
1894 }else
1895
1896 /*
1897 ** PRAGMA default_synchronous
1898 ** PRAGMA default_synchronous=BOOLEAN
1899 **
1900 ** The first form returns the persistent value of the "synchronous" setting
1901 ** that is stored in the database. This is the synchronous setting that
1902 ** is used whenever the database is opened unless overridden by a separate
1903 ** "synchronous" pragma. The second form changes the persistent and the
1904 ** local synchronous setting to the value given.
1905 **
1906 ** If synchronous is on, SQLite will do an fsync() system call at strategic
1907 ** points to insure that all previously written data has actually been
1908 ** written onto the disk surface before continuing. This mode insures that
1909 ** the database will always be in a consistent state event if the operating
1910 ** system crashes or power to the computer is interrupted unexpectedly.
1911 ** When synchronous is off, SQLite will not wait for changes to actually
1912 ** be written to the disk before continuing. As soon as it hands changes
1913 ** to the operating system, it assumes that the changes are permanent and
1914 ** it continues going. The database cannot be corrupted by a program crash
1915 ** even with synchronous off, but an operating system crash or power loss
1916 ** could potentially corrupt data. On the other hand, synchronous off is
1917 ** faster than synchronous on.
1918 */
1919 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00001920 static VdbeOp getSync[] = {
1921 { OP_Integer, 0, 0, 0},
1922 { OP_ReadCookie, 0, 2, 0},
1923 { OP_Integer, 0, 0, 0},
1924 { OP_Lt, 0, 5, 0},
1925 { OP_AddImm, 1, 0, 0},
1926 { OP_ColumnCount, 1, 0, 0},
1927 { OP_ColumnName, 0, 0, "synchronous"},
1928 { OP_Callback, 1, 0, 0},
1929 };
1930 Vdbe *v = sqliteGetVdbe(pParse);
1931 if( v==0 ) return;
1932 if( pRight->z==pLeft->z ){
1933 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
1934 }else{
1935 int addr;
drhcd61c282002-03-06 22:01:34 +00001936 int size = db->cache_size;
1937 if( size<0 ) size = -size;
drh603240c2002-03-05 01:11:12 +00001938 sqliteBeginWriteOperation(pParse);
1939 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00001940 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1941 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1942 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
1943 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00001944 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
1945 if( !getBoolean(zRight) ){
1946 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00001947 size = -size;
drh603240c2002-03-05 01:11:12 +00001948 }
1949 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
1950 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00001951 db->cache_size = size;
1952 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
1953 }
1954 }else
1955
1956 /*
1957 ** PRAGMA synchronous
1958 ** PRAGMA synchronous=BOOLEAN
1959 **
1960 ** Return or set the local value of the synchronous flag. Changing
1961 ** the local value does not make changes to the disk file and the
1962 ** default value will be restored the next time the database is
1963 ** opened.
1964 */
1965 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
1966 static VdbeOp getSync[] = {
1967 { OP_ColumnCount, 1, 0, 0},
1968 { OP_ColumnName, 0, 0, "synchronous"},
1969 { OP_Callback, 1, 0, 0},
1970 };
1971 Vdbe *v = sqliteGetVdbe(pParse);
1972 if( v==0 ) return;
1973 if( pRight->z==pLeft->z ){
1974 sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0);
1975 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
1976 }else{
1977 int size = db->cache_size;
1978 if( size<0 ) size = -size;
1979 if( !getBoolean(zRight) ) size = -size;
1980 db->cache_size = size;
1981 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00001982 }
drhf57b14a2001-09-14 18:54:08 +00001983 }else
1984
danielk1977c3f9bad2002-05-15 08:30:12 +00001985 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
1986 if( getBoolean(zRight) ){
1987 always_code_trigger_setup = 1;
1988 }else{
1989 always_code_trigger_setup = 0;
1990 }
1991 }else
1992
drhf57b14a2001-09-14 18:54:08 +00001993 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1994 if( getBoolean(zRight) ){
1995 db->flags |= SQLITE_VdbeTrace;
1996 }else{
1997 db->flags &= ~SQLITE_VdbeTrace;
1998 }
1999 }else
2000
drh382c0242001-10-06 16:33:02 +00002001 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2002 if( getBoolean(zRight) ){
2003 db->flags |= SQLITE_FullColNames;
2004 }else{
2005 db->flags &= ~SQLITE_FullColNames;
2006 }
2007 }else
2008
drhc3a64ba2001-11-22 00:01:27 +00002009 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2010 if( getBoolean(zRight) ){
2011 db->flags |= SQLITE_ResultDetails;
2012 }else{
2013 db->flags &= ~SQLITE_ResultDetails;
2014 }
2015 }else
2016
drh1bee3d72001-10-15 00:44:35 +00002017 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2018 if( getBoolean(zRight) ){
2019 db->flags |= SQLITE_CountRows;
2020 }else{
2021 db->flags &= ~SQLITE_CountRows;
2022 }
2023 }else
2024
drh6a535342001-10-19 16:44:56 +00002025 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2026 if( getBoolean(zRight) ){
2027 db->flags |= SQLITE_NullCallback;
2028 }else{
2029 db->flags &= ~SQLITE_NullCallback;
2030 }
2031 }else
2032
drh382c0242001-10-06 16:33:02 +00002033 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2034 Table *pTab;
2035 Vdbe *v;
2036 pTab = sqliteFindTable(db, zRight);
2037 if( pTab ) v = sqliteGetVdbe(pParse);
2038 if( pTab && v ){
2039 static VdbeOp tableInfoPreface[] = {
2040 { OP_ColumnCount, 5, 0, 0},
2041 { OP_ColumnName, 0, 0, "cid"},
2042 { OP_ColumnName, 1, 0, "name"},
2043 { OP_ColumnName, 2, 0, "type"},
2044 { OP_ColumnName, 3, 0, "notnull"},
2045 { OP_ColumnName, 4, 0, "dflt_value"},
2046 };
2047 int i;
2048 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002049 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002050 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002051 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2052 sqliteVdbeAddOp(v, OP_String, 0, 0);
2053 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2054 sqliteVdbeAddOp(v, OP_String, 0, 0);
2055 sqliteVdbeChangeP3(v, -1,
2056 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC);
2057 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2058 sqliteVdbeAddOp(v, OP_String, 0, 0);
2059 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2060 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002061 }
2062 }
2063 }else
2064
2065 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2066 Index *pIdx;
2067 Table *pTab;
2068 Vdbe *v;
2069 pIdx = sqliteFindIndex(db, zRight);
2070 if( pIdx ) v = sqliteGetVdbe(pParse);
2071 if( pIdx && v ){
2072 static VdbeOp tableInfoPreface[] = {
2073 { OP_ColumnCount, 3, 0, 0},
2074 { OP_ColumnName, 0, 0, "seqno"},
2075 { OP_ColumnName, 1, 0, "cid"},
2076 { OP_ColumnName, 2, 0, "name"},
2077 };
2078 int i;
2079 pTab = pIdx->pTable;
2080 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2081 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002082 int cnum = pIdx->aiColumn[i];
2083 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2084 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2085 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002086 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002087 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2088 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002089 }
2090 }
2091 }else
2092
drh81a20f22001-10-12 17:30:04 +00002093 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2094 Index *pIdx;
2095 Table *pTab;
2096 Vdbe *v;
2097 pTab = sqliteFindTable(db, zRight);
2098 if( pTab ){
2099 v = sqliteGetVdbe(pParse);
2100 pIdx = pTab->pIndex;
2101 }
2102 if( pTab && pIdx && v ){
2103 int i = 0;
2104 static VdbeOp indexListPreface[] = {
2105 { OP_ColumnCount, 3, 0, 0},
2106 { OP_ColumnName, 0, 0, "seq"},
2107 { OP_ColumnName, 1, 0, "name"},
2108 { OP_ColumnName, 2, 0, "unique"},
2109 };
2110
2111 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2112 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002113 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2114 sqliteVdbeAddOp(v, OP_String, 0, 0);
2115 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002116 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002117 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002118 ++i;
2119 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002120 }
2121 }
2122 }else
2123
drhf57b14a2001-09-14 18:54:08 +00002124#ifndef NDEBUG
2125 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2126 extern void sqliteParserTrace(FILE*, char *);
2127 if( getBoolean(zRight) ){
2128 sqliteParserTrace(stdout, "parser: ");
2129 }else{
2130 sqliteParserTrace(0, 0);
2131 }
2132 }else
2133#endif
2134
drhaaab5722002-02-19 13:39:21 +00002135 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002136 static VdbeOp checkDb[] = {
2137 { OP_SetInsert, 0, 0, "2"},
2138 { OP_Open, 0, 2, 0},
2139 { OP_Rewind, 0, 6, 0},
2140 { OP_Column, 0, 3, 0},
2141 { OP_SetInsert, 0, 0, 0},
2142 { OP_Next, 0, 3, 0},
drhaaab5722002-02-19 13:39:21 +00002143 { OP_IntegrityCk, 0, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002144 { OP_ColumnCount, 1, 0, 0},
drh4ff6dfa2002-03-03 23:06:00 +00002145 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002146 { OP_Callback, 1, 0, 0},
2147 };
2148 Vdbe *v = sqliteGetVdbe(pParse);
2149 if( v==0 ) return;
2150 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2151 }else
drh1bffb9c2002-02-03 17:37:36 +00002152
drhf57b3392001-10-08 13:22:32 +00002153 {}
2154 sqliteFree(zLeft);
2155 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002156}