blob: 41c5eaccd68935f527aae5ab552a5970b6ac0d01 [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**
drh97fc3d02002-05-22 21:27:03 +000028** $Id: build.c,v 1.93 2002/05/22 21:27:03 drh Exp $
drh75897232000-05-29 14:26:00 +000029*/
30#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000031#include <ctype.h>
drh75897232000-05-29 14:26:00 +000032
33/*
34** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000035** parsed and we want to execute the VDBE code to implement
36** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000037** constructed VDBE code to do the work of the SQL statement.
38** This routine just has to execute the VDBE code.
39**
40** Note that if an error occurred, it might be the case that
41** no VDBE code was generated.
42*/
43void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000044 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000045 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000046 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000047 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000048 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000049 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
50 &pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +000051 }else{
drh3fc190c2001-09-14 03:24:23 +000052 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000053 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000054 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000055 &pParse->zErrMsg, db->pBusyArg,
56 db->xBusyCallback);
drhecdc7532001-09-23 02:35:53 +000057 if( rc ) pParse->nErr++;
drh75897232000-05-29 14:26:00 +000058 }
59 sqliteVdbeDelete(pParse->pVdbe);
60 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000061 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000062 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000063 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000064 }
65}
66
67/*
drhf57b3392001-10-08 13:22:32 +000068** 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 ){
drhc977f7f2002-05-21 11:38:11 +0000481 sqliteBeginWriteOperation(pParse, 0);
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;
drh97fc3d02002-05-22 21:27:03 +0000505 int i;
506 char *z = 0;
drh75897232000-05-29 14:26:00 +0000507 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000508 sqliteSetNString(&z, pName->z, pName->n, 0);
509 if( z==0 ) return;
510 sqliteDequote(z);
511 for(i=0; i<p->nCol; i++){
512 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
513 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
514 pParse->nErr++;
515 sqliteFree(z);
516 return;
517 }
518 }
drh75897232000-05-29 14:26:00 +0000519 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000520 Column *aNew;
521 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
522 if( aNew==0 ) return;
523 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000524 }
drh7020f652000-06-03 18:06:52 +0000525 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
drh97fc3d02002-05-22 21:27:03 +0000526 p->aCol[p->nCol++].zName = z;
drh75897232000-05-29 14:26:00 +0000527}
528
529/*
drh382c0242001-10-06 16:33:02 +0000530** This routine is called by the parser while in the middle of
531** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
532** been seen on a column. This routine sets the notNull flag on
533** the column currently under construction.
534*/
drh9cfcf5d2002-01-29 18:41:24 +0000535void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000536 Table *p;
537 int i;
538 if( (p = pParse->pNewTable)==0 ) return;
539 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000540 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000541}
542
543/*
544** This routine is called by the parser while in the middle of
545** parsing a CREATE TABLE statement. The pFirst token is the first
546** token in the sequence of tokens that describe the type of the
547** column currently under construction. pLast is the last token
548** in the sequence. Use this information to construct a string
549** that contains the typename of the column and store that string
550** in zType.
551*/
552void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
553 Table *p;
554 int i, j;
555 int n;
556 char *z, **pz;
557 if( (p = pParse->pNewTable)==0 ) return;
558 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000559 if( i<0 ) return;
drh382c0242001-10-06 16:33:02 +0000560 pz = &p->aCol[i].zType;
drh5a2c2c22001-11-21 02:21:11 +0000561 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000562 sqliteSetNString(pz, pFirst->z, n, 0);
563 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000564 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000565 for(i=j=0; z[i]; i++){
566 int c = z[i];
567 if( isspace(c) ) continue;
568 z[j++] = c;
569 }
570 z[j] = 0;
571}
572
573/*
drh7020f652000-06-03 18:06:52 +0000574** The given token is the default value for the last column added to
575** the table currently under construction. If "minusFlag" is true, it
576** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000577**
578** This routine is called by the parser while in the middle of
579** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000580*/
581void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
582 Table *p;
583 int i;
584 char **pz;
585 if( (p = pParse->pNewTable)==0 ) return;
586 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000587 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000588 pz = &p->aCol[i].zDflt;
589 if( minusFlag ){
590 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
591 }else{
592 sqliteSetNString(pz, pVal->z, pVal->n, 0);
593 }
594 sqliteDequote(*pz);
595}
596
597/*
drh4a324312001-12-21 14:30:42 +0000598** Designate the PRIMARY KEY for the table. pList is a list of names
599** of columns that form the primary key. If pList is NULL, then the
600** most recently added column of the table is the primary key.
601**
602** A table can have at most one primary key. If the table already has
603** a primary key (and this is the second primary key) then create an
604** error.
605**
606** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
607** then we will try to use that column as the row id. (Exception:
608** For backwards compatibility with older databases, do not do this
609** if the file format version number is less than 1.) Set the Table.iPKey
610** field of the table under construction to be the index of the
611** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
612** no INTEGER PRIMARY KEY.
613**
614** If the key is not an INTEGER PRIMARY KEY, then create a unique
615** index for the key. No index is created for INTEGER PRIMARY KEYs.
616*/
drh9cfcf5d2002-01-29 18:41:24 +0000617void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000618 Table *pTab = pParse->pNewTable;
619 char *zType = 0;
620 int iCol = -1;
621 if( pTab==0 ) return;
622 if( pTab->hasPrimKey ){
623 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
624 "\" has more than one primary key", 0);
625 pParse->nErr++;
626 return;
627 }
628 pTab->hasPrimKey = 1;
629 if( pList==0 ){
630 iCol = pTab->nCol - 1;
631 }else if( pList->nId==1 ){
632 for(iCol=0; iCol<pTab->nCol; iCol++){
633 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
634 }
635 }
636 if( iCol>=0 && iCol<pTab->nCol ){
637 zType = pTab->aCol[iCol].zType;
638 }
639 if( pParse->db->file_format>=1 &&
640 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
641 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000642 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000643 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000644 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drh4a324312001-12-21 14:30:42 +0000645 }
646}
647
648/*
drh50e5dad2001-09-15 00:57:28 +0000649** Come up with a new random value for the schema cookie. Make sure
650** the new value is different from the old.
651**
652** The schema cookie is used to determine when the schema for the
653** database changes. After each schema change, the cookie value
654** changes. When a process first reads the schema it records the
655** cookie. Thereafter, whenever it goes to access the database,
656** it checks the cookie to make sure the schema has not changed
657** since it was last read.
658**
659** This plan is not completely bullet-proof. It is possible for
660** the schema to change multiple times and for the cookie to be
661** set back to prior value. But schema changes are infrequent
662** and the probability of hitting the same cookie value is only
663** 1 chance in 2^32. So we're safe enough.
664*/
drhdc379452002-05-15 12:45:43 +0000665void sqliteChangeCookie(sqlite *db){
drh50e5dad2001-09-15 00:57:28 +0000666 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000667 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000668 db->flags |= SQLITE_InternChanges;
669 }
670}
671
672/*
drh969fa7c2002-02-18 18:30:32 +0000673** Measure the number of characters needed to output the given
674** identifier. The number returned includes any quotes used
675** but does not include the null terminator.
676*/
677static int identLength(const char *z){
678 int n;
drh17f71932002-02-21 12:01:27 +0000679 int needQuote = 0;
680 for(n=0; *z; n++, z++){
681 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000682 }
drh17f71932002-02-21 12:01:27 +0000683 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000684}
685
686/*
687** Write an identifier onto the end of the given string. Add
688** quote characters as needed.
689*/
690static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000691 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000692 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000693 for(j=0; zIdent[j]; j++){
694 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
695 }
696 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
697 || sqliteKeywordCode(zIdent, j)!=TK_ID;
698 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000699 for(j=0; zIdent[j]; j++){
700 z[i++] = zIdent[j];
701 if( zIdent[j]=='\'' ) z[i++] = '\'';
702 }
drh17f71932002-02-21 12:01:27 +0000703 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000704 z[i] = 0;
705 *pIdx = i;
706}
707
708/*
709** Generate a CREATE TABLE statement appropriate for the given
710** table. Memory to hold the text of the statement is obtained
711** from sqliteMalloc() and must be freed by the calling function.
712*/
713static char *createTableStmt(Table *p){
714 int i, k, n;
715 char *zStmt;
716 char *zSep, *zSep2, *zEnd;
717 n = 0;
718 for(i=0; i<p->nCol; i++){
719 n += identLength(p->aCol[i].zName);
720 }
721 n += identLength(p->zName);
722 if( n<40 ){
723 zSep = "";
724 zSep2 = ",";
725 zEnd = ")";
726 }else{
727 zSep = "\n ";
728 zSep2 = ",\n ";
729 zEnd = "\n)";
730 }
731 n += 25 + 6*p->nCol;
732 zStmt = sqliteMalloc( n );
733 if( zStmt==0 ) return 0;
734 assert( !p->isTemp );
735 strcpy(zStmt, "CREATE TABLE ");
736 k = strlen(zStmt);
737 identPut(zStmt, &k, p->zName);
738 zStmt[k++] = '(';
739 for(i=0; i<p->nCol; i++){
740 strcpy(&zStmt[k], zSep);
741 k += strlen(&zStmt[k]);
742 zSep = zSep2;
743 identPut(zStmt, &k, p->aCol[i].zName);
744 }
745 strcpy(&zStmt[k], zEnd);
746 return zStmt;
747}
748
749/*
drh75897232000-05-29 14:26:00 +0000750** This routine is called to report the final ")" that terminates
751** a CREATE TABLE statement.
752**
drhf57b3392001-10-08 13:22:32 +0000753** The table structure that other action routines have been building
754** is added to the internal hash tables, assuming no errors have
755** occurred.
drh75897232000-05-29 14:26:00 +0000756**
drh1ccde152000-06-17 13:12:39 +0000757** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000758** unless this is a temporary table or initFlag==1. When initFlag==1,
759** it means we are reading the sqlite_master table because we just
760** connected to the database or because the sqlite_master table has
761** recently changes, so the entry for this table already exists in
762** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000763**
764** If the pSelect argument is not NULL, it means that this routine
765** was called to create a table generated from a
766** "CREATE TABLE ... AS SELECT ..." statement. The column names of
767** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000768*/
drh969fa7c2002-02-18 18:30:32 +0000769void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000770 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000771 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000772
drh969fa7c2002-02-18 18:30:32 +0000773 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000774 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000775 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000776
drhf57b3392001-10-08 13:22:32 +0000777 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000778 */
drhad75e982001-10-09 04:19:46 +0000779 assert( pParse->nameClash==0 || pParse->initFlag==1 );
drhf57b3392001-10-08 13:22:32 +0000780 if( pParse->explain==0 && pParse->nameClash==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000781 Table *pOld;
782 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
783 if( pOld ){
drh74e24cd2002-01-09 03:19:59 +0000784 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh6d4abfb2001-10-22 02:58:08 +0000785 return;
786 }
drh75897232000-05-29 14:26:00 +0000787 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000788 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000789 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000790 }
791
drh969fa7c2002-02-18 18:30:32 +0000792 /* If the table is generated from a SELECT, then construct the
793 ** list of columns and the text of the table.
794 */
795 if( pSelect ){
796 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000797 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000798 assert( p->aCol==0 );
799 p->nCol = pSelTab->nCol;
800 p->aCol = pSelTab->aCol;
801 pSelTab->nCol = 0;
802 pSelTab->aCol = 0;
803 sqliteDeleteTable(0, pSelTab);
804 }
805
drhd78eeee2001-09-13 16:18:53 +0000806 /* If the initFlag is 1 it means we are reading the SQL off the
807 ** "sqlite_master" table on the disk. So do not write to the disk
drhe3c41372001-09-17 20:25:58 +0000808 ** again. Extract the root page number for the table from the
809 ** pParse->newTnum field. (The page number should have been put
drh382c0242001-10-06 16:33:02 +0000810 ** there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000811 */
812 if( pParse->initFlag ){
813 p->tnum = pParse->newTnum;
814 }
815
drhe3c41372001-09-17 20:25:58 +0000816 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000817 ** in the SQLITE_MASTER table of the database. The record number
818 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000819 **
820 ** If this is a TEMPORARY table, then just create the table. Do not
821 ** make an entry in SQLITE_MASTER.
drh75897232000-05-29 14:26:00 +0000822 */
823 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000824 int n;
drhd8bc7082000-06-07 23:51:50 +0000825 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000826
drhd8bc7082000-06-07 23:51:50 +0000827 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000828 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000829 if( p->pSelect==0 ){
830 /* A regular table */
831 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
832 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
833 }else{
834 /* A view */
835 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
836 }
drh969fa7c2002-02-18 18:30:32 +0000837 p->tnum = 0;
drhf57b3392001-10-08 13:22:32 +0000838 if( !p->isTemp ){
drh17f71932002-02-21 12:01:27 +0000839 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
drh99fcd712001-10-13 01:06:47 +0000840 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +0000841 if( p->pSelect==0 ){
842 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
843 }else{
844 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
845 }
drh99fcd712001-10-13 01:06:47 +0000846 sqliteVdbeAddOp(v, OP_String, 0, 0);
847 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
848 sqliteVdbeAddOp(v, OP_String, 0, 0);
849 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
drh969fa7c2002-02-18 18:30:32 +0000850 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
851 sqliteVdbeAddOp(v, OP_String, 0, 0);
852 if( pSelect ){
853 char *z = createTableStmt(p);
854 n = z ? strlen(z) : 0;
855 sqliteVdbeChangeP3(v, -1, z, n);
856 sqliteFree(z);
857 }else{
858 assert( pEnd!=0 );
859 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
860 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
861 }
drh99fcd712001-10-13 01:06:47 +0000862 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +0000863 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhdc379452002-05-15 12:45:43 +0000864 sqliteChangeCookie(db);
drh603240c2002-03-05 01:11:12 +0000865 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
866 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh99fcd712001-10-13 01:06:47 +0000867 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000868 }
drh969fa7c2002-02-18 18:30:32 +0000869 if( pSelect ){
870 int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
871 sqliteVdbeAddOp(v, op, 1, 0);
872 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000873 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000874 }
drh1c928532002-01-31 15:54:21 +0000875 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000876 }
877}
878
879/*
drha76b5df2002-02-23 02:32:10 +0000880** The parser calls this routine in order to create a new VIEW
881*/
882void sqliteCreateView(
883 Parse *pParse, /* The parsing context */
884 Token *pBegin, /* The CREATE token that begins the statement */
885 Token *pName, /* The token that holds the name of the view */
886 Select *pSelect /* A SELECT statement that will become the new view */
887){
888 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000889 Table *p;
drh4ff6dfa2002-03-03 23:06:00 +0000890 const char *z;
drha76b5df2002-02-23 02:32:10 +0000891 int n, offset;
892
893 sqliteStartTable(pParse, pBegin, pName, 0);
894 p = pParse->pNewTable;
drh417be792002-03-03 18:59:40 +0000895 if( p==0 ){
896 sqliteSelectDelete(pSelect);
897 return;
898 }
drh0f18b452002-05-08 21:30:15 +0000899 /* Ignore ORDER BY clauses on a SELECT */
900 if( pSelect->pOrderBy ){
901 sqliteExprListDelete(pSelect->pOrderBy);
902 pSelect->pOrderBy = 0;
903 }
drha76b5df2002-02-23 02:32:10 +0000904 p->pSelect = pSelect;
drh417be792002-03-03 18:59:40 +0000905 if( !pParse->initFlag ){
906 if( sqliteViewGetColumnNames(pParse, p) ){
907 return;
908 }
909 }
drha76b5df2002-02-23 02:32:10 +0000910 sEnd = pParse->sLastToken;
911 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
912 sEnd.z += sEnd.n;
913 }
914 sEnd.n = 0;
915 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000916 z = pBegin->z;
917 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
918 sEnd.z = &z[n-1];
919 sEnd.n = 1;
920 z = p->pSelect->zSelect = sqliteStrNDup(z, n);
drh417be792002-03-03 18:59:40 +0000921 if( z ){
922 offset = ((int)z) - (int)pBegin->z;
923 sqliteSelectMoveStrings(p->pSelect, offset);
924 sqliteEndTable(pParse, &sEnd, 0);
925 }
drha76b5df2002-02-23 02:32:10 +0000926 return;
drh417be792002-03-03 18:59:40 +0000927}
drha76b5df2002-02-23 02:32:10 +0000928
drh417be792002-03-03 18:59:40 +0000929/*
930** The Table structure pTable is really a VIEW. Fill in the names of
931** the columns of the view in the pTable structure. Return the number
932** of errors. If an error is seen leave an error message in pPare->zErrMsg.
933*/
934int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
935 ExprList *pEList;
936 Select *pSel;
937 Table *pSelTab;
938 int nErr = 0;
939
940 assert( pTable );
941
942 /* A positive nCol means the columns names for this view are
943 ** already known.
944 */
945 if( pTable->nCol>0 ) return 0;
946
947 /* A negative nCol is a special marker meaning that we are currently
948 ** trying to compute the column names. If we enter this routine with
949 ** a negative nCol, it means two or more views form a loop, like this:
950 **
951 ** CREATE VIEW one AS SELECT * FROM two;
952 ** CREATE VIEW two AS SELECT * FROM one;
953 */
954 if( pTable->nCol<0 ){
955 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
956 " is circularly defined", 0);
957 pParse->nErr++;
958 return 1;
959 }
960
961 /* If we get this far, it means we need to compute the table names.
962 */
963 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
964 pSel = pTable->pSelect;
965
966 /* Note that the call to sqliteResultSetOfSelect() will expand any
967 ** "*" elements in this list. But we will need to restore the list
968 ** back to its original configuration afterwards, so we save a copy of
969 ** the original in pEList.
970 */
971 pEList = pSel->pEList;
972 pSel->pEList = sqliteExprListDup(pEList);
973 if( pSel->pEList==0 ){
974 pSel->pEList = pEList;
975 return 1; /* Malloc failed */
976 }
977 pTable->nCol = -1;
978 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
979 if( pSelTab ){
980 assert( pTable->aCol==0 );
981 pTable->nCol = pSelTab->nCol;
982 pTable->aCol = pSelTab->aCol;
983 pSelTab->nCol = 0;
984 pSelTab->aCol = 0;
985 sqliteDeleteTable(0, pSelTab);
986 pParse->db->flags |= SQLITE_UnresetViews;
987 }else{
988 pTable->nCol = 0;
989 nErr++;
990 }
991 sqliteSelectUnbind(pSel);
992 sqliteExprListDelete(pSel->pEList);
993 pSel->pEList = pEList;
994 return nErr;
995}
996
997/*
998** Clear the column names from the VIEW pTable.
999**
1000** This routine is called whenever any other table or view is modified.
1001** The view passed into this routine might depend directly or indirectly
1002** on the modified or deleted table so we need to clear the old column
1003** names so that they will be recomputed.
1004*/
1005static void sqliteViewResetColumnNames(Table *pTable){
1006 int i;
1007 if( pTable==0 || pTable->pSelect==0 ) return;
1008 if( pTable->nCol==0 ) return;
1009 for(i=0; i<pTable->nCol; i++){
1010 sqliteFree(pTable->aCol[i].zName);
1011 sqliteFree(pTable->aCol[i].zDflt);
1012 sqliteFree(pTable->aCol[i].zType);
1013 }
1014 sqliteFree(pTable->aCol);
1015 pTable->aCol = 0;
1016 pTable->nCol = 0;
1017}
1018
1019/*
1020** Clear the column names from every VIEW.
1021*/
1022void sqliteViewResetAll(sqlite *db){
1023 HashElem *i;
1024 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
1025 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
1026 Table *pTab = sqliteHashData(i);
1027 if( pTab->pSelect ){
1028 sqliteViewResetColumnNames(pTab);
1029 }
1030 }
1031 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001032}
1033
1034/*
drh75897232000-05-29 14:26:00 +00001035** Given a token, look up a table with that name. If not found, leave
1036** an error for the parser to find and return NULL.
1037*/
drhcce7d172000-05-31 15:34:51 +00001038Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001039 char *zName;
1040 Table *pTab;
1041 zName = sqliteTableNameFromToken(pTok);
1042 if( zName==0 ) return 0;
1043 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +00001044 sqliteFree(zName);
1045 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001046 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1047 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001048 pParse->nErr++;
1049 }
1050 return pTab;
1051}
1052
1053/*
1054** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001055** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001056*/
drh4ff6dfa2002-03-03 23:06:00 +00001057void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001058 Table *pTable;
drh75897232000-05-29 14:26:00 +00001059 Vdbe *v;
1060 int base;
drh5edc3122001-09-13 21:53:09 +00001061 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001062
drhdaffd0e2001-04-11 14:28:42 +00001063 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001064 pTable = sqliteTableFromToken(pParse, pName);
1065 if( pTable==0 ) return;
1066 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001067 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1068 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001069 pParse->nErr++;
1070 return;
1071 }
drh4ff6dfa2002-03-03 23:06:00 +00001072 if( isView && pTable->pSelect==0 ){
1073 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1074 pTable->zName, 0);
1075 pParse->nErr++;
1076 return;
1077 }
1078 if( !isView && pTable->pSelect ){
1079 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1080 pTable->zName, 0);
1081 pParse->nErr++;
1082 return;
1083 }
drh75897232000-05-29 14:26:00 +00001084
drh1ccde152000-06-17 13:12:39 +00001085 /* Generate code to remove the table from the master table
1086 ** on disk.
1087 */
drhd8bc7082000-06-07 23:51:50 +00001088 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001089 if( v ){
1090 static VdbeOp dropTable[] = {
drhecdc7532001-09-23 02:35:53 +00001091 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +00001092 { OP_Rewind, 0, ADDR(9), 0},
drhd78eeee2001-09-13 16:18:53 +00001093 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +00001094 { OP_MemStore, 1, 1, 0},
1095 { OP_MemLoad, 1, 0, 0}, /* 4 */
drhe3c41372001-09-17 20:25:58 +00001096 { OP_Column, 0, 2, 0},
drh6b563442001-11-07 16:48:26 +00001097 { OP_Ne, 0, ADDR(8), 0},
drh75897232000-05-29 14:26:00 +00001098 { OP_Delete, 0, 0, 0},
drh6b563442001-11-07 16:48:26 +00001099 { OP_Next, 0, ADDR(4), 0}, /* 8 */
drh603240c2002-03-05 01:11:12 +00001100 { OP_Integer, 0, 0, 0}, /* 9 */
1101 { OP_SetCookie, 0, 0, 0},
drh75897232000-05-29 14:26:00 +00001102 { OP_Close, 0, 0, 0},
1103 };
1104 Index *pIdx;
drhc977f7f2002-05-21 11:38:11 +00001105 sqliteBeginWriteOperation(pParse, 0);
danielk1977c3f9bad2002-05-15 08:30:12 +00001106 /* Drop all triggers associated with the table being dropped */
drhdc379452002-05-15 12:45:43 +00001107 while( pTable->pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001108 Token tt;
1109 tt.z = pTable->pTrigger->name;
1110 tt.n = strlen(pTable->pTrigger->name);
1111 sqliteDropTrigger(pParse, &tt, 1);
1112 }
drhf57b3392001-10-08 13:22:32 +00001113 if( !pTable->isTemp ){
1114 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh0a36c572002-02-18 22:49:59 +00001115 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
drhdc379452002-05-15 12:45:43 +00001116 sqliteChangeCookie(db);
drhf57b3392001-10-08 13:22:32 +00001117 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
1118 }
drh4ff6dfa2002-03-03 23:06:00 +00001119 if( !isView ){
1120 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1121 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1122 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1123 }
drh5e00f6c2001-09-13 13:46:56 +00001124 }
drh1c928532002-01-31 15:54:21 +00001125 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001126 }
1127
drh74e24cd2002-01-09 03:19:59 +00001128 /* Move the table (and all its indices) to the pending DROP queue.
1129 ** Or, if the table was never committed, just delete it. If the table
1130 ** has been committed and is placed on the pending DROP queue, then the
1131 ** delete will occur when sqliteCommitInternalChanges() executes.
drh75897232000-05-29 14:26:00 +00001132 **
1133 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001134 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001135 */
1136 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +00001137 sqlitePendingDropTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001138 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001139 }
drh417be792002-03-03 18:59:40 +00001140 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001141}
1142
1143/*
1144** Create a new index for an SQL table. pIndex is the name of the index
1145** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001146** be NULL for a primary key or an index that is created to satisfy a
1147** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001148** as the table to be indexed. pParse->pNewTable is a table that is
1149** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001150**
drh382c0242001-10-06 16:33:02 +00001151** pList is a list of columns to be indexed. pList will be NULL if this
1152** is a primary key or unique-constraint on the most recent column added
1153** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001154*/
1155void sqliteCreateIndex(
1156 Parse *pParse, /* All information about this parse */
1157 Token *pName, /* Name of the index. May be NULL */
1158 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001159 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001160 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001161 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1162 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1163){
1164 Table *pTab; /* Table to be indexed */
1165 Index *pIndex; /* The index to be created */
1166 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001167 int i, j;
drhf57b3392001-10-08 13:22:32 +00001168 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001169 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001170 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001171
drhdaffd0e2001-04-11 14:28:42 +00001172 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1173
drh75897232000-05-29 14:26:00 +00001174 /*
1175 ** Find the table that is to be indexed. Return early if not found.
1176 */
1177 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001178 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001179 pTab = sqliteTableFromToken(pParse, pTable);
1180 }else{
drhe3c41372001-09-17 20:25:58 +00001181 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001182 pTab = pParse->pNewTable;
1183 }
1184 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1185 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001186 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1187 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001188 pParse->nErr++;
1189 goto exit_create_index;
1190 }
drha76b5df2002-02-23 02:32:10 +00001191 if( pTab->pSelect ){
1192 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1193 pParse->nErr++;
1194 goto exit_create_index;
1195 }
drh75897232000-05-29 14:26:00 +00001196
drhf57b3392001-10-08 13:22:32 +00001197 /* If this index is created while re-reading the schema from sqlite_master
1198 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001199 ** only mean that the table that this index is really associated with is
1200 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001201 ** Since its table has been suppressed, we need to also suppress the
1202 ** index.
1203 */
1204 if( pParse->initFlag && pTab->isTemp ){
1205 goto exit_create_index;
1206 }
1207
drh75897232000-05-29 14:26:00 +00001208 /*
1209 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001210 ** index or table with the same name.
1211 **
1212 ** Exception: If we are reading the names of permanent indices from the
1213 ** sqlite_master table (because some other process changed the schema) and
1214 ** one of the index names collides with the name of a temporary table or
1215 ** index, then we will continue to process this index, but we will not
1216 ** store its name in the hash table. Set the hideName flag to accomplish
1217 ** this.
1218 **
1219 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001220 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1221 ** own name.
drh75897232000-05-29 14:26:00 +00001222 */
1223 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001224 Index *pISameName; /* Another index with the same name */
1225 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001226 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001227 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001228 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1229 if( pISameName->pTable->isTemp && pParse->initFlag ){
1230 hideName = 1;
1231 }else{
1232 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1233 " already exists", 0);
1234 pParse->nErr++;
1235 goto exit_create_index;
1236 }
drhe3c41372001-09-17 20:25:58 +00001237 }
drhf57b3392001-10-08 13:22:32 +00001238 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1239 if( pTSameName->isTemp && pParse->initFlag ){
1240 hideName = 1;
1241 }else{
1242 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1243 zName, 0);
1244 pParse->nErr++;
1245 goto exit_create_index;
1246 }
drhe3c41372001-09-17 20:25:58 +00001247 }
drh75897232000-05-29 14:26:00 +00001248 }else{
drhadbca9c2001-09-27 15:11:53 +00001249 char zBuf[30];
1250 int n;
1251 Index *pLoop;
1252 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1253 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001254 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001255 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001256 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001257 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001258 }
1259
1260 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001261 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001262 ** So create a fake list to simulate this.
1263 */
1264 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001265 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001266 nullId.n = strlen(nullId.z);
1267 pList = sqliteIdListAppend(0, &nullId);
1268 if( pList==0 ) goto exit_create_index;
1269 }
1270
1271 /*
1272 ** Allocate the index structure.
1273 */
drhdcc581c2000-05-30 13:44:19 +00001274 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001275 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001276 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001277 pIndex->aiColumn = (int*)&pIndex[1];
1278 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001279 strcpy(pIndex->zName, zName);
1280 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001281 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001282 pIndex->onError = pIndex->isUnique = onError;
drh75897232000-05-29 14:26:00 +00001283
drh1ccde152000-06-17 13:12:39 +00001284 /* Scan the names of the columns of the table to be indexed and
1285 ** load the column indices into the Index structure. Report an error
1286 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001287 */
1288 for(i=0; i<pList->nId; i++){
1289 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001290 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001291 }
1292 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001293 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001294 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001295 pParse->nErr++;
1296 sqliteFree(pIndex);
1297 goto exit_create_index;
1298 }
drh967e8b72000-06-21 13:59:10 +00001299 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001300 }
1301
1302 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001303 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001304 */
drhf57b3392001-10-08 13:22:32 +00001305 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001306 Index *p;
1307 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1308 if( p ){
1309 assert( p==pIndex ); /* Malloc must have failed */
1310 sqliteFree(pIndex);
1311 goto exit_create_index;
1312 }
drh5e00f6c2001-09-13 13:46:56 +00001313 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001314 }
drh9cfcf5d2002-01-29 18:41:24 +00001315
1316 /* When adding an index to the list of indices for a table, make
1317 ** sure all indices labeled OE_Replace come after all those labeled
1318 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1319 ** and INSERT.
1320 */
1321 if( onError!=OE_Replace || pTab->pIndex==0
1322 || pTab->pIndex->onError==OE_Replace){
1323 pIndex->pNext = pTab->pIndex;
1324 pTab->pIndex = pIndex;
1325 }else{
1326 Index *pOther = pTab->pIndex;
1327 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1328 pOther = pOther->pNext;
1329 }
1330 pIndex->pNext = pOther->pNext;
1331 pOther->pNext = pIndex;
1332 }
drh75897232000-05-29 14:26:00 +00001333
drhd78eeee2001-09-13 16:18:53 +00001334 /* If the initFlag is 1 it means we are reading the SQL off the
1335 ** "sqlite_master" table on the disk. So do not write to the disk
1336 ** again. Extract the table number from the pParse->newTnum field.
1337 */
drhadbca9c2001-09-27 15:11:53 +00001338 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001339 pIndex->tnum = pParse->newTnum;
1340 }
1341
drh75897232000-05-29 14:26:00 +00001342 /* If the initFlag is 0 then create the index on disk. This
1343 ** involves writing the index into the master table and filling in the
1344 ** index with the current table contents.
1345 **
1346 ** The initFlag is 0 when the user first enters a CREATE INDEX
1347 ** command. The initFlag is 1 when a database is opened and
1348 ** CREATE INDEX statements are read out of the master table. In
1349 ** the latter case the index already exists on disk, which is why
1350 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001351 **
1352 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001353 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1354 ** has just been created, it contains no data and the index initialization
1355 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001356 */
drhadbca9c2001-09-27 15:11:53 +00001357 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001358 int n;
drhadbca9c2001-09-27 15:11:53 +00001359 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001360 int lbl1, lbl2;
1361 int i;
drhadbca9c2001-09-27 15:11:53 +00001362 int addr;
drhf57b3392001-10-08 13:22:32 +00001363 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001364
drhd8bc7082000-06-07 23:51:50 +00001365 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001366 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001367 if( pTable!=0 ){
drhc977f7f2002-05-21 11:38:11 +00001368 sqliteBeginWriteOperation(pParse, 0);
drhf57b3392001-10-08 13:22:32 +00001369 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001370 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
1371 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001372 }
drhadbca9c2001-09-27 15:11:53 +00001373 }
drhf57b3392001-10-08 13:22:32 +00001374 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001375 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1376 sqliteVdbeAddOp(v, OP_String, 0, 0);
1377 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1378 sqliteVdbeAddOp(v, OP_String, 0, 0);
1379 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1380 sqliteVdbeAddOp(v, OP_String, 0, 0);
1381 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001382 }
drh99fcd712001-10-13 01:06:47 +00001383 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1384 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001385 pIndex->tnum = 0;
1386 if( pTable ){
drhf57b3392001-10-08 13:22:32 +00001387 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001388 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001389 }else{
drh99fcd712001-10-13 01:06:47 +00001390 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1391 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001392 }
drh5e00f6c2001-09-13 13:46:56 +00001393 }
drhf57b3392001-10-08 13:22:32 +00001394 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001395 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001396 if( pStart && pEnd ){
drh5a2c2c22001-11-21 02:21:11 +00001397 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
drhf57b3392001-10-08 13:22:32 +00001398 sqliteVdbeChangeP3(v, addr, pStart->z, n);
1399 }
drh99fcd712001-10-13 01:06:47 +00001400 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +00001401 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh75897232000-05-29 14:26:00 +00001402 }
drhadbca9c2001-09-27 15:11:53 +00001403 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001404 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1405 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001406 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001407 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1408 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001409 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001410 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001411 }
drh99fcd712001-10-13 01:06:47 +00001412 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001413 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001414 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001415 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001416 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1417 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001418 }
drhadbca9c2001-09-27 15:11:53 +00001419 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001420 if( !isTemp ){
drhdc379452002-05-15 12:45:43 +00001421 sqliteChangeCookie(db);
drh603240c2002-03-05 01:11:12 +00001422 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
1423 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001424 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001425 }
drh1c928532002-01-31 15:54:21 +00001426 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001427 }
drh75897232000-05-29 14:26:00 +00001428 }
1429
drh75897232000-05-29 14:26:00 +00001430 /* Clean up before exiting */
1431exit_create_index:
1432 sqliteIdListDelete(pList);
1433 sqliteFree(zName);
1434 return;
1435}
1436
1437/*
drh74e24cd2002-01-09 03:19:59 +00001438** This routine will drop an existing named index. This routine
1439** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001440*/
1441void sqliteDropIndex(Parse *pParse, Token *pName){
1442 Index *pIndex;
1443 char *zName;
1444 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001445 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001446
drhdaffd0e2001-04-11 14:28:42 +00001447 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001448 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001449 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001450 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001451 sqliteFree(zName);
1452 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001453 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1454 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001455 pParse->nErr++;
1456 return;
1457 }
1458
1459 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001460 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001461 if( v ){
1462 static VdbeOp dropIndex[] = {
drhecdc7532001-09-23 02:35:53 +00001463 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +00001464 { OP_Rewind, 0, ADDR(10),0},
drhd78eeee2001-09-13 16:18:53 +00001465 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +00001466 { OP_MemStore, 1, 1, 0},
1467 { OP_MemLoad, 1, 0, 0}, /* 4 */
drh5e00f6c2001-09-13 13:46:56 +00001468 { OP_Column, 0, 1, 0},
drh6b563442001-11-07 16:48:26 +00001469 { OP_Eq, 0, ADDR(9), 0},
1470 { OP_Next, 0, ADDR(4), 0},
1471 { OP_Goto, 0, ADDR(10),0},
1472 { OP_Delete, 0, 0, 0}, /* 9 */
drh603240c2002-03-05 01:11:12 +00001473 { OP_Integer, 0, 0, 0}, /* 10 */
1474 { OP_SetCookie, 0, 0, 0},
drh75897232000-05-29 14:26:00 +00001475 { OP_Close, 0, 0, 0},
1476 };
1477 int base;
drhf57b3392001-10-08 13:22:32 +00001478 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001479
drhc977f7f2002-05-21 11:38:11 +00001480 sqliteBeginWriteOperation(pParse, 0);
drhf57b3392001-10-08 13:22:32 +00001481 if( !pTab->isTemp ){
1482 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh99fcd712001-10-13 01:06:47 +00001483 sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC);
drhdc379452002-05-15 12:45:43 +00001484 sqliteChangeCookie(db);
drh6b563442001-11-07 16:48:26 +00001485 sqliteVdbeChangeP1(v, base+10, db->next_cookie);
drhf57b3392001-10-08 13:22:32 +00001486 }
drh99fcd712001-10-13 01:06:47 +00001487 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001488 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001489 }
1490
drh74e24cd2002-01-09 03:19:59 +00001491 /* Move the index onto the pending DROP queue. Or, if the index was
1492 ** never committed, just delete it. Indices on the pending DROP queue
1493 ** get deleted by sqliteCommitInternalChanges() when the user executes
1494 ** a COMMIT. Or if a rollback occurs, the elements of the DROP queue
1495 ** are moved back into the main hash table.
drh75897232000-05-29 14:26:00 +00001496 */
1497 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +00001498 sqlitePendingDropIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001499 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001500 }
1501}
1502
1503/*
drh75897232000-05-29 14:26:00 +00001504** Append a new element to the given IdList. Create a new IdList if
1505** need be.
drhdaffd0e2001-04-11 14:28:42 +00001506**
1507** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001508*/
1509IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1510 if( pList==0 ){
1511 pList = sqliteMalloc( sizeof(IdList) );
1512 if( pList==0 ) return 0;
1513 }
1514 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001515 struct IdList_item *a;
1516 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1517 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001518 sqliteIdListDelete(pList);
1519 return 0;
drh75897232000-05-29 14:26:00 +00001520 }
drh6d4abfb2001-10-22 02:58:08 +00001521 pList->a = a;
drh75897232000-05-29 14:26:00 +00001522 }
1523 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1524 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001525 char **pz = &pList->a[pList->nId].zName;
1526 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1527 if( *pz==0 ){
1528 sqliteIdListDelete(pList);
1529 return 0;
1530 }else{
1531 sqliteDequote(*pz);
1532 }
drh75897232000-05-29 14:26:00 +00001533 }
1534 pList->nId++;
1535 return pList;
1536}
1537
1538/*
1539** Add an alias to the last identifier on the given identifier list.
1540*/
1541void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1542 if( pList && pList->nId>0 ){
1543 int i = pList->nId - 1;
1544 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001545 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001546 }
1547}
1548
1549/*
drha76b5df2002-02-23 02:32:10 +00001550** Delete an entire IdList.
drh75897232000-05-29 14:26:00 +00001551*/
1552void sqliteIdListDelete(IdList *pList){
1553 int i;
1554 if( pList==0 ) return;
1555 for(i=0; i<pList->nId; i++){
1556 sqliteFree(pList->a[i].zName);
1557 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001558 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001559 sqliteDeleteTable(0, pList->a[i].pTab);
1560 }
drhff78bd22002-02-27 01:47:11 +00001561 sqliteSelectDelete(pList->a[i].pSelect);
drh75897232000-05-29 14:26:00 +00001562 }
1563 sqliteFree(pList->a);
1564 sqliteFree(pList);
1565}
1566
drh982cef72000-05-30 16:27:03 +00001567/*
1568** The COPY command is for compatibility with PostgreSQL and specificially
1569** for the ability to read the output of pg_dump. The format is as
1570** follows:
1571**
1572** COPY table FROM file [USING DELIMITERS string]
1573**
1574** "table" is an existing table name. We will read lines of code from
1575** file to fill this table with data. File might be "stdin". The optional
1576** delimiter string identifies the field separators. The default is a tab.
1577*/
1578void sqliteCopy(
1579 Parse *pParse, /* The parser context */
1580 Token *pTableName, /* The name of the table into which we will insert */
1581 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001582 Token *pDelimiter, /* Use this as the field delimiter */
1583 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001584){
1585 Table *pTab;
1586 char *zTab;
drh1c928532002-01-31 15:54:21 +00001587 int i;
drh982cef72000-05-30 16:27:03 +00001588 Vdbe *v;
1589 int addr, end;
1590 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001591 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001592
1593 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001594 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001595 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001596 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001597 if( pTab==0 ) goto copy_cleanup;
drhd8bc7082000-06-07 23:51:50 +00001598 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001599 if( v ){
drhf57b3392001-10-08 13:22:32 +00001600 int openOp;
drhc977f7f2002-05-21 11:38:11 +00001601 sqliteBeginWriteOperation(pParse, 1);
drh99fcd712001-10-13 01:06:47 +00001602 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001603 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001604 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001605 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001606 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1607 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001608 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001609 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1610 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001611 }
drhb419a922002-01-30 16:17:23 +00001612 if( db->flags & SQLITE_CountRows ){
1613 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1614 }
drh982cef72000-05-30 16:27:03 +00001615 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001616 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001617 if( pDelimiter ){
1618 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1619 sqliteVdbeDequoteP3(v, addr);
1620 }else{
1621 sqliteVdbeChangeP3(v, addr, "\t", 1);
1622 }
drh8aff1012001-12-22 14:49:24 +00001623 if( pTab->iPKey>=0 ){
1624 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1625 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1626 }else{
1627 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1628 }
drh982cef72000-05-30 16:27:03 +00001629 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001630 if( i==pTab->iPKey ){
1631 /* The integer primary key column is filled with NULL since its
1632 ** value is always pulled from the record number */
1633 sqliteVdbeAddOp(v, OP_String, 0, 0);
1634 }else{
1635 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1636 }
drh982cef72000-05-30 16:27:03 +00001637 }
drhb419a922002-01-30 16:17:23 +00001638 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1639 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1640 if( (db->flags & SQLITE_CountRows)!=0 ){
1641 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001642 }
drh99fcd712001-10-13 01:06:47 +00001643 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1644 sqliteVdbeResolveLabel(v, end);
1645 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001646 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001647 if( db->flags & SQLITE_CountRows ){
1648 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1649 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1650 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1651 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1652 }
drh982cef72000-05-30 16:27:03 +00001653 }
1654
1655copy_cleanup:
1656 return;
1657}
drhdce2cbe2000-05-31 02:27:49 +00001658
1659/*
1660** The non-standard VACUUM command is used to clean up the database,
1661** collapse free space, etc. It is modelled after the VACUUM command
1662** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001663**
drh1bffb9c2002-02-03 17:37:36 +00001664** In version 1.0.x of SQLite, the VACUUM command would call
1665** gdbm_reorganize() on all the database tables. But beginning
1666** with 2.0.0, SQLite no longer uses GDBM so this command has
1667** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001668*/
1669void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001670 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001671}
drhc4a3c772001-04-04 11:48:57 +00001672
1673/*
1674** Begin a transaction
1675*/
drh1c928532002-01-31 15:54:21 +00001676void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001677 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001678
drhc4a3c772001-04-04 11:48:57 +00001679 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001680 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001681 if( db->flags & SQLITE_InTrans ) return;
drhc977f7f2002-05-21 11:38:11 +00001682 sqliteBeginWriteOperation(pParse, 0);
drh5e00f6c2001-09-13 13:46:56 +00001683 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001684 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001685}
1686
1687/*
1688** Commit a transaction
1689*/
1690void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001691 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001692
drhc4a3c772001-04-04 11:48:57 +00001693 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001694 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001695 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001696 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001697 sqliteEndWriteOperation(pParse);
1698 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001699}
1700
1701/*
1702** Rollback a transaction
1703*/
1704void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001705 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001706 Vdbe *v;
1707
drhc4a3c772001-04-04 11:48:57 +00001708 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001709 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001710 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001711 v = sqliteGetVdbe(pParse);
1712 if( v ){
drh99fcd712001-10-13 01:06:47 +00001713 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001714 }
drh5e00f6c2001-09-13 13:46:56 +00001715 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001716 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001717}
drhf57b14a2001-09-14 18:54:08 +00001718
1719/*
drh1c928532002-01-31 15:54:21 +00001720** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00001721** might change the database.
1722**
1723** This routine starts a new transaction if we are not already within
1724** a transaction. If we are already within a transaction, then a checkpoint
1725** is set if the setCheckpoint parameter is true. A checkpoint should
1726** be set for operations that might fail (due to a constraint) part of
1727** the way through and which will need to undo some writes without having to
1728** rollback the whole transaction. For operations where all constraints
1729** can be checked before any changes are made to the database, it is never
1730** necessary to undo a write and the checkpoint should not be set.
drh1c928532002-01-31 15:54:21 +00001731*/
drhc977f7f2002-05-21 11:38:11 +00001732void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint){
drh663fc632002-02-02 18:49:19 +00001733 Vdbe *v;
1734 v = sqliteGetVdbe(pParse);
1735 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00001736 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00001737 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
1738 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1739 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1740 pParse->schemaVerified = 1;
drhc977f7f2002-05-21 11:38:11 +00001741 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00001742 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1743 }
1744}
1745
1746/*
drh1c928532002-01-31 15:54:21 +00001747** Generate code that concludes an operation that may have changed
1748** the database. This is a companion function to BeginWriteOperation().
1749** If a transaction was started, then commit it. If a checkpoint was
1750** started then commit that.
1751*/
1752void sqliteEndWriteOperation(Parse *pParse){
1753 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00001754 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00001755 v = sqliteGetVdbe(pParse);
1756 if( v==0 ) return;
1757 if( pParse->db->flags & SQLITE_InTrans ){
1758 /* Do Nothing */
1759 }else{
1760 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
1761 }
1762}
1763
1764
1765/*
drhf57b14a2001-09-14 18:54:08 +00001766** Interpret the given string as a boolean value.
1767*/
1768static int getBoolean(char *z){
1769 static char *azTrue[] = { "yes", "on", "true" };
1770 int i;
1771 if( z[0]==0 ) return 0;
1772 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1773 return atoi(z);
1774 }
1775 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1776 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1777 }
1778 return 0;
1779}
1780
1781/*
1782** Process a pragma statement.
1783**
1784** Pragmas are of this form:
1785**
1786** PRAGMA id = value
1787**
1788** The identifier might also be a string. The value is a string, and
1789** identifier, or a number. If minusFlag is true, then the value is
1790** a number that was preceded by a minus sign.
1791*/
1792void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1793 char *zLeft = 0;
1794 char *zRight = 0;
1795 sqlite *db = pParse->db;
1796
1797 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1798 sqliteDequote(zLeft);
1799 if( minusFlag ){
1800 zRight = 0;
1801 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1802 }else{
1803 zRight = sqliteStrNDup(pRight->z, pRight->n);
1804 sqliteDequote(zRight);
1805 }
1806
drhcd61c282002-03-06 22:01:34 +00001807 /*
1808 ** PRAGMA default_cache_size
1809 ** PRAGMA default_cache_size=N
1810 **
1811 ** The first form reports the current persistent setting for the
1812 ** page cache size. The value returned is the maximum number of
1813 ** pages in the page cache. The second form sets both the current
1814 ** page cache size value and the persistent page cache size value
1815 ** stored in the database file.
1816 **
1817 ** The default cache size is stored in meta-value 2 of page 1 of the
1818 ** database file. The cache size is actually the absolute value of
1819 ** this memory location. The sign of meta-value 2 determines the
1820 ** synchronous setting. A negative value means synchronous is off
1821 ** and a positive value means synchronous is on.
1822 */
1823 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00001824 static VdbeOp getCacheSize[] = {
1825 { OP_ReadCookie, 0, 2, 0},
1826 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00001827 { OP_Dup, 0, 0, 0},
1828 { OP_Integer, 0, 0, 0},
1829 { OP_Ne, 0, 6, 0},
1830 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00001831 { OP_ColumnCount, 1, 0, 0},
1832 { OP_ColumnName, 0, 0, "cache_size"},
1833 { OP_Callback, 1, 0, 0},
1834 };
1835 Vdbe *v = sqliteGetVdbe(pParse);
1836 if( v==0 ) return;
1837 if( pRight->z==pLeft->z ){
1838 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1839 }else{
1840 int addr;
1841 int size = atoi(zRight);
1842 if( size<0 ) size = -size;
drhc977f7f2002-05-21 11:38:11 +00001843 sqliteBeginWriteOperation(pParse, 0);
drh603240c2002-03-05 01:11:12 +00001844 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1845 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
1846 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1847 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1848 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
1849 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
1850 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00001851 db->cache_size = db->cache_size<0 ? -size : size;
1852 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00001853 }
1854 }else
1855
drhcd61c282002-03-06 22:01:34 +00001856 /*
1857 ** PRAGMA cache_size
1858 ** PRAGMA cache_size=N
1859 **
1860 ** The first form reports the current local setting for the
1861 ** page cache size. The local setting can be different from
1862 ** the persistent cache size value that is stored in the database
1863 ** file itself. The value returned is the maximum number of
1864 ** pages in the page cache. The second form sets the local
1865 ** page cache size value. It does not change the persistent
1866 ** cache size stored on the disk so the cache size will revert
1867 ** to its default value when the database is closed and reopened.
1868 ** N should be a positive integer.
1869 */
1870 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1871 static VdbeOp getCacheSize[] = {
1872 { OP_ColumnCount, 1, 0, 0},
1873 { OP_ColumnName, 0, 0, "cache_size"},
1874 { OP_Callback, 1, 0, 0},
1875 };
1876 Vdbe *v = sqliteGetVdbe(pParse);
1877 if( v==0 ) return;
1878 if( pRight->z==pLeft->z ){
1879 int size = db->cache_size;;
1880 if( size<0 ) size = -size;
1881 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1882 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1883 }else{
1884 int size = atoi(zRight);
1885 if( size<0 ) size = -size;
1886 if( db->cache_size<0 ) size = -size;
1887 db->cache_size = size;
1888 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
1889 }
1890 }else
1891
1892 /*
1893 ** PRAGMA default_synchronous
1894 ** PRAGMA default_synchronous=BOOLEAN
1895 **
1896 ** The first form returns the persistent value of the "synchronous" setting
1897 ** that is stored in the database. This is the synchronous setting that
1898 ** is used whenever the database is opened unless overridden by a separate
1899 ** "synchronous" pragma. The second form changes the persistent and the
1900 ** local synchronous setting to the value given.
1901 **
1902 ** If synchronous is on, SQLite will do an fsync() system call at strategic
1903 ** points to insure that all previously written data has actually been
1904 ** written onto the disk surface before continuing. This mode insures that
1905 ** the database will always be in a consistent state event if the operating
1906 ** system crashes or power to the computer is interrupted unexpectedly.
1907 ** When synchronous is off, SQLite will not wait for changes to actually
1908 ** be written to the disk before continuing. As soon as it hands changes
1909 ** to the operating system, it assumes that the changes are permanent and
1910 ** it continues going. The database cannot be corrupted by a program crash
1911 ** even with synchronous off, but an operating system crash or power loss
1912 ** could potentially corrupt data. On the other hand, synchronous off is
1913 ** faster than synchronous on.
1914 */
1915 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00001916 static VdbeOp getSync[] = {
1917 { OP_Integer, 0, 0, 0},
1918 { OP_ReadCookie, 0, 2, 0},
1919 { OP_Integer, 0, 0, 0},
1920 { OP_Lt, 0, 5, 0},
1921 { OP_AddImm, 1, 0, 0},
1922 { OP_ColumnCount, 1, 0, 0},
1923 { OP_ColumnName, 0, 0, "synchronous"},
1924 { OP_Callback, 1, 0, 0},
1925 };
1926 Vdbe *v = sqliteGetVdbe(pParse);
1927 if( v==0 ) return;
1928 if( pRight->z==pLeft->z ){
1929 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
1930 }else{
1931 int addr;
drhcd61c282002-03-06 22:01:34 +00001932 int size = db->cache_size;
1933 if( size<0 ) size = -size;
drhc977f7f2002-05-21 11:38:11 +00001934 sqliteBeginWriteOperation(pParse, 0);
drh603240c2002-03-05 01:11:12 +00001935 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00001936 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1937 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1938 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
1939 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00001940 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
1941 if( !getBoolean(zRight) ){
1942 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00001943 size = -size;
drh603240c2002-03-05 01:11:12 +00001944 }
1945 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
1946 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00001947 db->cache_size = size;
1948 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
1949 }
1950 }else
1951
1952 /*
1953 ** PRAGMA synchronous
1954 ** PRAGMA synchronous=BOOLEAN
1955 **
1956 ** Return or set the local value of the synchronous flag. Changing
1957 ** the local value does not make changes to the disk file and the
1958 ** default value will be restored the next time the database is
1959 ** opened.
1960 */
1961 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
1962 static VdbeOp getSync[] = {
1963 { OP_ColumnCount, 1, 0, 0},
1964 { OP_ColumnName, 0, 0, "synchronous"},
1965 { OP_Callback, 1, 0, 0},
1966 };
1967 Vdbe *v = sqliteGetVdbe(pParse);
1968 if( v==0 ) return;
1969 if( pRight->z==pLeft->z ){
1970 sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0);
1971 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
1972 }else{
1973 int size = db->cache_size;
1974 if( size<0 ) size = -size;
1975 if( !getBoolean(zRight) ) size = -size;
1976 db->cache_size = size;
1977 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00001978 }
drhf57b14a2001-09-14 18:54:08 +00001979 }else
1980
danielk1977c3f9bad2002-05-15 08:30:12 +00001981 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
1982 if( getBoolean(zRight) ){
1983 always_code_trigger_setup = 1;
1984 }else{
1985 always_code_trigger_setup = 0;
1986 }
1987 }else
1988
drhf57b14a2001-09-14 18:54:08 +00001989 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1990 if( getBoolean(zRight) ){
1991 db->flags |= SQLITE_VdbeTrace;
1992 }else{
1993 db->flags &= ~SQLITE_VdbeTrace;
1994 }
1995 }else
1996
drh382c0242001-10-06 16:33:02 +00001997 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
1998 if( getBoolean(zRight) ){
1999 db->flags |= SQLITE_FullColNames;
2000 }else{
2001 db->flags &= ~SQLITE_FullColNames;
2002 }
2003 }else
2004
drhc3a64ba2001-11-22 00:01:27 +00002005 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2006 if( getBoolean(zRight) ){
2007 db->flags |= SQLITE_ResultDetails;
2008 }else{
2009 db->flags &= ~SQLITE_ResultDetails;
2010 }
2011 }else
2012
drh1bee3d72001-10-15 00:44:35 +00002013 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2014 if( getBoolean(zRight) ){
2015 db->flags |= SQLITE_CountRows;
2016 }else{
2017 db->flags &= ~SQLITE_CountRows;
2018 }
2019 }else
2020
drh6a535342001-10-19 16:44:56 +00002021 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2022 if( getBoolean(zRight) ){
2023 db->flags |= SQLITE_NullCallback;
2024 }else{
2025 db->flags &= ~SQLITE_NullCallback;
2026 }
2027 }else
2028
drh382c0242001-10-06 16:33:02 +00002029 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2030 Table *pTab;
2031 Vdbe *v;
2032 pTab = sqliteFindTable(db, zRight);
2033 if( pTab ) v = sqliteGetVdbe(pParse);
2034 if( pTab && v ){
2035 static VdbeOp tableInfoPreface[] = {
2036 { OP_ColumnCount, 5, 0, 0},
2037 { OP_ColumnName, 0, 0, "cid"},
2038 { OP_ColumnName, 1, 0, "name"},
2039 { OP_ColumnName, 2, 0, "type"},
2040 { OP_ColumnName, 3, 0, "notnull"},
2041 { OP_ColumnName, 4, 0, "dflt_value"},
2042 };
2043 int i;
2044 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002045 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002046 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002047 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2048 sqliteVdbeAddOp(v, OP_String, 0, 0);
2049 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2050 sqliteVdbeAddOp(v, OP_String, 0, 0);
2051 sqliteVdbeChangeP3(v, -1,
2052 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC);
2053 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2054 sqliteVdbeAddOp(v, OP_String, 0, 0);
2055 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2056 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002057 }
2058 }
2059 }else
2060
2061 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2062 Index *pIdx;
2063 Table *pTab;
2064 Vdbe *v;
2065 pIdx = sqliteFindIndex(db, zRight);
2066 if( pIdx ) v = sqliteGetVdbe(pParse);
2067 if( pIdx && v ){
2068 static VdbeOp tableInfoPreface[] = {
2069 { OP_ColumnCount, 3, 0, 0},
2070 { OP_ColumnName, 0, 0, "seqno"},
2071 { OP_ColumnName, 1, 0, "cid"},
2072 { OP_ColumnName, 2, 0, "name"},
2073 };
2074 int i;
2075 pTab = pIdx->pTable;
2076 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2077 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002078 int cnum = pIdx->aiColumn[i];
2079 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2080 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2081 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002082 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002083 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2084 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002085 }
2086 }
2087 }else
2088
drh81a20f22001-10-12 17:30:04 +00002089 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2090 Index *pIdx;
2091 Table *pTab;
2092 Vdbe *v;
2093 pTab = sqliteFindTable(db, zRight);
2094 if( pTab ){
2095 v = sqliteGetVdbe(pParse);
2096 pIdx = pTab->pIndex;
2097 }
2098 if( pTab && pIdx && v ){
2099 int i = 0;
2100 static VdbeOp indexListPreface[] = {
2101 { OP_ColumnCount, 3, 0, 0},
2102 { OP_ColumnName, 0, 0, "seq"},
2103 { OP_ColumnName, 1, 0, "name"},
2104 { OP_ColumnName, 2, 0, "unique"},
2105 };
2106
2107 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2108 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002109 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2110 sqliteVdbeAddOp(v, OP_String, 0, 0);
2111 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002112 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002113 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002114 ++i;
2115 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002116 }
2117 }
2118 }else
2119
drhf57b14a2001-09-14 18:54:08 +00002120#ifndef NDEBUG
2121 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2122 extern void sqliteParserTrace(FILE*, char *);
2123 if( getBoolean(zRight) ){
2124 sqliteParserTrace(stdout, "parser: ");
2125 }else{
2126 sqliteParserTrace(0, 0);
2127 }
2128 }else
2129#endif
2130
drhaaab5722002-02-19 13:39:21 +00002131 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002132 static VdbeOp checkDb[] = {
2133 { OP_SetInsert, 0, 0, "2"},
2134 { OP_Open, 0, 2, 0},
2135 { OP_Rewind, 0, 6, 0},
2136 { OP_Column, 0, 3, 0},
2137 { OP_SetInsert, 0, 0, 0},
2138 { OP_Next, 0, 3, 0},
drhaaab5722002-02-19 13:39:21 +00002139 { OP_IntegrityCk, 0, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002140 { OP_ColumnCount, 1, 0, 0},
drh4ff6dfa2002-03-03 23:06:00 +00002141 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002142 { OP_Callback, 1, 0, 0},
2143 };
2144 Vdbe *v = sqliteGetVdbe(pParse);
2145 if( v==0 ) return;
2146 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2147 }else
drh1bffb9c2002-02-03 17:37:36 +00002148
drhf57b3392001-10-08 13:22:32 +00002149 {}
2150 sqliteFree(zLeft);
2151 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002152}