blob: 4caad2d34da36be351c2c6d5cb46d2ba57f10e8f [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**
drh4b59ab52002-08-24 18:24:51 +000028** $Id: build.c,v 1.110 2002/08/24 18:24:53 drh Exp $
drh75897232000-05-29 14:26:00 +000029*/
30#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000031#include <ctype.h>
drh75897232000-05-29 14:26:00 +000032
33/*
drhe0bc4042002-06-25 01:09:11 +000034** This routine is called when a new SQL statement is beginning to
35** be parsed. Check to see if the schema for the database needs
36** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
37** If it does, then read it.
38*/
39void sqliteBeginParse(Parse *pParse, int explainFlag){
40 sqlite *db = pParse->db;
41 pParse->explain = explainFlag;
42 if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){
43 int rc = sqliteInit(db, &pParse->zErrMsg);
44 if( rc!=SQLITE_OK ){
45 pParse->rc = rc;
46 pParse->nErr++;
47 }
48 }
49}
50
51/*
drh75897232000-05-29 14:26:00 +000052** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000053** parsed and we want to execute the VDBE code to implement
54** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000055** constructed VDBE code to do the work of the SQL statement.
56** This routine just has to execute the VDBE code.
57**
58** Note that if an error occurred, it might be the case that
59** no VDBE code was generated.
60*/
61void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000062 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000063 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000064 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000065 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000066 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000067 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
68 &pParse->zErrMsg);
drhe0bc4042002-06-25 01:09:11 +000069 db->next_cookie = db->schema_cookie;
drh75897232000-05-29 14:26:00 +000070 }else{
drh3fc190c2001-09-14 03:24:23 +000071 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000072 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000073 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000074 &pParse->zErrMsg, db->pBusyArg,
75 db->xBusyCallback);
drhecdc7532001-09-23 02:35:53 +000076 if( rc ) pParse->nErr++;
drh75897232000-05-29 14:26:00 +000077 }
78 sqliteVdbeDelete(pParse->pVdbe);
79 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000080 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000081 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000082 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000083 }
84}
85
86/*
drhf57b3392001-10-08 13:22:32 +000087** Locate the in-memory structure that describes
88** a particular database table given the name
drh75897232000-05-29 14:26:00 +000089** of that table. Return NULL if not found.
90*/
drha76b5df2002-02-23 02:32:10 +000091Table *sqliteFindTable(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +000092 Table *p;
93 p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +000094 return p;
drh75897232000-05-29 14:26:00 +000095}
96
97/*
drhf57b3392001-10-08 13:22:32 +000098** Locate the in-memory structure that describes
99** a particular index given the name of that index.
100** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000101*/
drha76b5df2002-02-23 02:32:10 +0000102Index *sqliteFindIndex(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +0000103 Index *p;
104 p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +0000105 return p;
drh75897232000-05-29 14:26:00 +0000106}
107
108/*
109** Remove the given index from the index hash table, and free
110** its memory structures.
111**
drhd229ca92002-01-09 13:30:41 +0000112** The index is removed from the database hash tables but
113** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000114** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000115*/
drh74e24cd2002-01-09 03:19:59 +0000116static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000117 Index *pOld;
118 assert( db!=0 && p->zName!=0 );
119 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
120 if( pOld!=0 && pOld!=p ){
121 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000122 }
drh74e24cd2002-01-09 03:19:59 +0000123 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000124}
125
126/*
drhbeae3192001-09-22 18:12:08 +0000127** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000128** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000129** structures.
130*/
drh6d4abfb2001-10-22 02:58:08 +0000131void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000132 if( pIndex->pTable->pIndex==pIndex ){
133 pIndex->pTable->pIndex = pIndex->pNext;
134 }else{
135 Index *p;
136 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
137 if( p && p->pNext==pIndex ){
138 p->pNext = pIndex->pNext;
139 }
140 }
141 sqliteDeleteIndex(db, pIndex);
142}
143
144/*
drhe0bc4042002-06-25 01:09:11 +0000145** Erase all schema information from the in-memory hash tables of
146** database connection. This routine is called to reclaim memory
147** before the connection closes. It is also called during a rollback
148** if there were schema changes during the transaction.
drh74e24cd2002-01-09 03:19:59 +0000149*/
drhe0bc4042002-06-25 01:09:11 +0000150void sqliteResetInternalSchema(sqlite *db){
151 HashElem *pElem;
152 Hash temp1;
153 Hash temp2;
154
155 temp1 = db->tblHash;
156 temp2 = db->trigHash;
157 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
158 sqliteHashClear(&db->idxHash);
159 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
160 Trigger *pTrigger = sqliteHashData(pElem);
161 sqliteDeleteTrigger(pTrigger);
drh74e24cd2002-01-09 03:19:59 +0000162 }
drhe0bc4042002-06-25 01:09:11 +0000163 sqliteHashClear(&temp2);
164 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
165 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
166 Table *pTab = sqliteHashData(pElem);
167 sqliteDeleteTable(db, pTab);
168 }
169 sqliteHashClear(&temp1);
170 db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges);
171}
172
173/*
174** This routine is called whenever a rollback occurs. If there were
175** schema changes during the transaction, then we have to reset the
176** internal hash tables and reload them from disk.
177*/
178void sqliteRollbackInternalChanges(sqlite *db){
179 if( db->flags & SQLITE_InternChanges ){
180 sqliteResetInternalSchema(db);
181 }
182}
183
184/*
185** This routine is called when a commit occurs.
186*/
187void sqliteCommitInternalChanges(sqlite *db){
188 db->schema_cookie = db->next_cookie;
189 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000190}
191
192/*
drh75897232000-05-29 14:26:00 +0000193** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000194** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000195**
196** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000197** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000198** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000199**
200** Indices associated with the table are unlinked from the "db"
201** data structure if db!=NULL. If db==NULL, indices attached to
202** the table are deleted, but it is assumed they have already been
203** unlinked.
drh75897232000-05-29 14:26:00 +0000204*/
205void sqliteDeleteTable(sqlite *db, Table *pTable){
206 int i;
207 Index *pIndex, *pNext;
208 if( pTable==0 ) return;
209 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000210 sqliteFree(pTable->aCol[i].zName);
211 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000212 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000213 }
214 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
215 pNext = pIndex->pNext;
216 sqliteDeleteIndex(db, pIndex);
217 }
drh6e142f52000-06-08 13:36:40 +0000218 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000219 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000220 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000221 sqliteFree(pTable);
222}
223
224/*
drh5edc3122001-09-13 21:53:09 +0000225** Unlink the given table from the hash tables and the delete the
drh74e24cd2002-01-09 03:19:59 +0000226** table structure with all its indices.
drh5edc3122001-09-13 21:53:09 +0000227*/
drh74e24cd2002-01-09 03:19:59 +0000228static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000229 Table *pOld;
230 assert( db!=0 );
231 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
232 assert( pOld==0 || pOld==p );
drh74e24cd2002-01-09 03:19:59 +0000233 sqliteDeleteTable(db, p);
234}
235
236/*
drh1ccde152000-06-17 13:12:39 +0000237** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000238**
239** Space to hold the name is obtained from sqliteMalloc() and must
240** be freed by the calling function.
241*/
drhcce7d172000-05-31 15:34:51 +0000242char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000243 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000244 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000245 return zName;
246}
247
248/*
drhe0bc4042002-06-25 01:09:11 +0000249** Generate code to open the appropriate master table. The table
250** opened will be SQLITE_MASTER for persistent tables and
251** SQLITE_TEMP_MASTER for temporary tables. The table is opened
252** on cursor 0.
253*/
254void sqliteOpenMasterTable(Vdbe *v, int isTemp){
255 if( isTemp ){
256 sqliteVdbeAddOp(v, OP_OpenWrAux, 0, 2);
257 sqliteVdbeChangeP3(v, -1, TEMP_MASTER_NAME, P3_STATIC);
258 }else{
259 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
260 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
261 }
262}
263
264/*
drh75897232000-05-29 14:26:00 +0000265** Begin constructing a new table representation in memory. This is
266** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000267** to a CREATE TABLE statement. In particular, this routine is called
268** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000269** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000270** flag is true if the table should be stored in the auxiliary database
271** file instead of in the main database file. This is normally the case
272** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000273** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000274**
drhf57b3392001-10-08 13:22:32 +0000275** The new table record is initialized and put in pParse->pNewTable.
276** As more of the CREATE TABLE statement is parsed, additional action
277** routines will be called to add more information to this record.
278** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
279** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000280*/
drhf57b3392001-10-08 13:22:32 +0000281void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){
drh75897232000-05-29 14:26:00 +0000282 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000283 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000284 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000285 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000286 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000287
288 pParse->sFirstToken = *pStart;
289 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000290 if( zName==0 ) return;
drhf57b3392001-10-08 13:22:32 +0000291
292 /* Before trying to create a temporary table, make sure the Btree for
293 ** holding temporary tables is open.
294 */
295 if( isTemp && db->pBeTemp==0 ){
296 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
297 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000298 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000299 "file for storing temporary tables", 0);
300 pParse->nErr++;
301 return;
302 }
303 if( db->flags & SQLITE_InTrans ){
304 rc = sqliteBtreeBeginTrans(db->pBeTemp);
305 if( rc!=SQLITE_OK ){
306 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000307 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000308 pParse->nErr++;
309 return;
310 }
311 }
312 }
313
314 /* Make sure the new table name does not collide with an existing
315 ** index or table name. Issue an error message if it does.
316 **
317 ** If we are re-reading the sqlite_master table because of a schema
318 ** change and a new permanent table is found whose name collides with
319 ** an existing temporary table, then ignore the new permanent table.
320 ** We will continue parsing, but the pParse->nameClash flag will be set
321 ** so we will know to discard the table record once parsing has finished.
322 */
drhbe0072d2001-09-13 14:46:09 +0000323 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000324 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000325 if( pTable->isTemp && pParse->initFlag ){
326 pParse->nameClash = 1;
327 }else{
328 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
329 " already exists", 0, 0);
330 sqliteFree(zName);
331 pParse->nErr++;
332 return;
333 }
334 }else{
335 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000336 }
drhf57b3392001-10-08 13:22:32 +0000337 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
338 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000339 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
340 zName, 0);
drh75897232000-05-29 14:26:00 +0000341 sqliteFree(zName);
342 pParse->nErr++;
343 return;
344 }
345 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000346 if( pTable==0 ){
347 sqliteFree(zName);
348 return;
349 }
drh75897232000-05-29 14:26:00 +0000350 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000351 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000352 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000353 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000354 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000355 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000356 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000357 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000358
359 /* Begin generating the code that will insert the table record into
360 ** the SQLITE_MASTER table. Note in particular that we must go ahead
361 ** and allocate the record number for the table entry now. Before any
362 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
363 ** indices to be created and the table record must come before the
364 ** indices. Hence, the record number for the table must be allocated
365 ** now.
366 */
drhadbca9c2001-09-27 15:11:53 +0000367 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhc977f7f2002-05-21 11:38:11 +0000368 sqliteBeginWriteOperation(pParse, 0);
drhf57b3392001-10-08 13:22:32 +0000369 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000370 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
371 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000372 }
drhe0bc4042002-06-25 01:09:11 +0000373 sqliteOpenMasterTable(v, isTemp);
374 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
375 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
376 sqliteVdbeAddOp(v, OP_String, 0, 0);
377 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000378 }
drh75897232000-05-29 14:26:00 +0000379}
380
381/*
382** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000383**
384** The parser calls this routine once for each column declaration
385** in a CREATE TABLE statement. sqliteStartTable() gets called
386** first to get things going. Then this routine is called for each
387** column.
drh75897232000-05-29 14:26:00 +0000388*/
389void sqliteAddColumn(Parse *pParse, Token *pName){
390 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000391 int i;
392 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000393 Column *pCol;
drh75897232000-05-29 14:26:00 +0000394 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000395 sqliteSetNString(&z, pName->z, pName->n, 0);
396 if( z==0 ) return;
397 sqliteDequote(z);
398 for(i=0; i<p->nCol; i++){
399 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
400 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
401 pParse->nErr++;
402 sqliteFree(z);
403 return;
404 }
405 }
drh75897232000-05-29 14:26:00 +0000406 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000407 Column *aNew;
408 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
409 if( aNew==0 ) return;
410 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000411 }
drhc9b84a12002-06-20 11:36:48 +0000412 pCol = &p->aCol[p->nCol];
413 memset(pCol, 0, sizeof(p->aCol[0]));
414 pCol->zName = z;
415 pCol->sortOrder = SQLITE_SO_NUM;
416 p->nCol++;
drh75897232000-05-29 14:26:00 +0000417}
418
419/*
drh382c0242001-10-06 16:33:02 +0000420** This routine is called by the parser while in the middle of
421** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
422** been seen on a column. This routine sets the notNull flag on
423** the column currently under construction.
424*/
drh9cfcf5d2002-01-29 18:41:24 +0000425void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000426 Table *p;
427 int i;
428 if( (p = pParse->pNewTable)==0 ) return;
429 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000430 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000431}
432
433/*
434** This routine is called by the parser while in the middle of
435** parsing a CREATE TABLE statement. The pFirst token is the first
436** token in the sequence of tokens that describe the type of the
437** column currently under construction. pLast is the last token
438** in the sequence. Use this information to construct a string
439** that contains the typename of the column and store that string
440** in zType.
441*/
442void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
443 Table *p;
444 int i, j;
445 int n;
446 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000447 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000448 if( (p = pParse->pNewTable)==0 ) return;
449 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000450 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000451 pCol = &p->aCol[i];
452 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000453 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000454 sqliteSetNString(pz, pFirst->z, n, 0);
455 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000456 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000457 for(i=j=0; z[i]; i++){
458 int c = z[i];
459 if( isspace(c) ) continue;
460 z[j++] = c;
461 }
462 z[j] = 0;
drhc9b84a12002-06-20 11:36:48 +0000463 pCol->sortOrder = SQLITE_SO_NUM;
drh3d037a92002-08-15 01:26:09 +0000464 if( pParse->db->file_format>=4 ){
465 for(i=0; z[i]; i++){
466 switch( z[i] ){
467 case 'b':
468 case 'B': {
469 if( sqliteStrNICmp(&z[i],"blob",4)==0 ){
470 pCol->sortOrder = SQLITE_SO_TEXT;
471 return;
472 }
473 break;
drh38640e12002-07-05 21:42:36 +0000474 }
drh3d037a92002-08-15 01:26:09 +0000475 case 'c':
476 case 'C': {
477 if( sqliteStrNICmp(&z[i],"char",4)==0 ||
478 sqliteStrNICmp(&z[i],"clob",4)==0 ){
479 pCol->sortOrder = SQLITE_SO_TEXT;
480 return;
481 }
482 break;
drhc9b84a12002-06-20 11:36:48 +0000483 }
drh3d037a92002-08-15 01:26:09 +0000484 case 'x':
485 case 'X': {
486 if( i>=2 && sqliteStrNICmp(&z[i-2],"text",4)==0 ){
487 pCol->sortOrder = SQLITE_SO_TEXT;
488 return;
489 }
490 break;
drhc9b84a12002-06-20 11:36:48 +0000491 }
drh3d037a92002-08-15 01:26:09 +0000492 default: {
493 break;
494 }
drhc9b84a12002-06-20 11:36:48 +0000495 }
496 }
497 }
drh382c0242001-10-06 16:33:02 +0000498}
499
500/*
drh7020f652000-06-03 18:06:52 +0000501** The given token is the default value for the last column added to
502** the table currently under construction. If "minusFlag" is true, it
503** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000504**
505** This routine is called by the parser while in the middle of
506** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000507*/
508void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
509 Table *p;
510 int i;
511 char **pz;
512 if( (p = pParse->pNewTable)==0 ) return;
513 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000514 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000515 pz = &p->aCol[i].zDflt;
516 if( minusFlag ){
517 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
518 }else{
519 sqliteSetNString(pz, pVal->z, pVal->n, 0);
520 }
521 sqliteDequote(*pz);
522}
523
524/*
drh4a324312001-12-21 14:30:42 +0000525** Designate the PRIMARY KEY for the table. pList is a list of names
526** of columns that form the primary key. If pList is NULL, then the
527** most recently added column of the table is the primary key.
528**
529** A table can have at most one primary key. If the table already has
530** a primary key (and this is the second primary key) then create an
531** error.
532**
533** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
534** then we will try to use that column as the row id. (Exception:
535** For backwards compatibility with older databases, do not do this
536** if the file format version number is less than 1.) Set the Table.iPKey
537** field of the table under construction to be the index of the
538** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
539** no INTEGER PRIMARY KEY.
540**
541** If the key is not an INTEGER PRIMARY KEY, then create a unique
542** index for the key. No index is created for INTEGER PRIMARY KEYs.
543*/
drh9cfcf5d2002-01-29 18:41:24 +0000544void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000545 Table *pTab = pParse->pNewTable;
546 char *zType = 0;
547 int iCol = -1;
548 if( pTab==0 ) return;
549 if( pTab->hasPrimKey ){
550 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
551 "\" has more than one primary key", 0);
552 pParse->nErr++;
553 return;
554 }
555 pTab->hasPrimKey = 1;
556 if( pList==0 ){
557 iCol = pTab->nCol - 1;
558 }else if( pList->nId==1 ){
559 for(iCol=0; iCol<pTab->nCol; iCol++){
560 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
561 }
562 }
563 if( iCol>=0 && iCol<pTab->nCol ){
564 zType = pTab->aCol[iCol].zType;
565 }
566 if( pParse->db->file_format>=1 &&
567 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
568 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000569 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000570 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000571 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drh4a324312001-12-21 14:30:42 +0000572 }
573}
574
575/*
drh8e2ca022002-06-17 17:07:19 +0000576** Return the appropriate collating type given the collation type token.
577** Report an error if the type is undefined.
578*/
579int sqliteCollateType(Parse *pParse, Token *pType){
580 if( pType==0 ) return SQLITE_SO_UNK;
581 if( pType->n==4 && sqliteStrNICmp(pType->z, "text", 4)==0 ){
582 return SQLITE_SO_TEXT;
583 }
584 if( pType->n==7 && sqliteStrNICmp(pType->z, "numeric", 7)==0 ){
585 return SQLITE_SO_NUM;
586 }
587 sqliteSetNString(&pParse->zErrMsg, "unknown collating type: ", -1,
588 pType->z, pType->n, 0);
589 pParse->nErr++;
590 return SQLITE_SO_UNK;
591}
592
593/*
594** This routine is called by the parser while in the middle of
595** parsing a CREATE TABLE statement. A "COLLATE" clause has
596** been seen on a column. This routine sets the Column.sortOrder on
597** the column currently under construction.
598*/
599void sqliteAddCollateType(Parse *pParse, int collType){
600 Table *p;
601 int i;
602 if( (p = pParse->pNewTable)==0 ) return;
603 i = p->nCol-1;
604 if( i>=0 ) p->aCol[i].sortOrder = collType;
605}
606
607/*
drh50e5dad2001-09-15 00:57:28 +0000608** Come up with a new random value for the schema cookie. Make sure
609** the new value is different from the old.
610**
611** The schema cookie is used to determine when the schema for the
612** database changes. After each schema change, the cookie value
613** changes. When a process first reads the schema it records the
614** cookie. Thereafter, whenever it goes to access the database,
615** it checks the cookie to make sure the schema has not changed
616** since it was last read.
617**
618** This plan is not completely bullet-proof. It is possible for
619** the schema to change multiple times and for the cookie to be
620** set back to prior value. But schema changes are infrequent
621** and the probability of hitting the same cookie value is only
622** 1 chance in 2^32. So we're safe enough.
623*/
drhe0bc4042002-06-25 01:09:11 +0000624void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh50e5dad2001-09-15 00:57:28 +0000625 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000626 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000627 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000628 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
629 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000630 }
631}
632
633/*
drh969fa7c2002-02-18 18:30:32 +0000634** Measure the number of characters needed to output the given
635** identifier. The number returned includes any quotes used
636** but does not include the null terminator.
637*/
638static int identLength(const char *z){
639 int n;
drh17f71932002-02-21 12:01:27 +0000640 int needQuote = 0;
641 for(n=0; *z; n++, z++){
642 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000643 }
drh17f71932002-02-21 12:01:27 +0000644 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000645}
646
647/*
648** Write an identifier onto the end of the given string. Add
649** quote characters as needed.
650*/
651static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000652 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000653 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000654 for(j=0; zIdent[j]; j++){
655 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
656 }
657 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
658 || sqliteKeywordCode(zIdent, j)!=TK_ID;
659 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000660 for(j=0; zIdent[j]; j++){
661 z[i++] = zIdent[j];
662 if( zIdent[j]=='\'' ) z[i++] = '\'';
663 }
drh17f71932002-02-21 12:01:27 +0000664 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000665 z[i] = 0;
666 *pIdx = i;
667}
668
669/*
670** Generate a CREATE TABLE statement appropriate for the given
671** table. Memory to hold the text of the statement is obtained
672** from sqliteMalloc() and must be freed by the calling function.
673*/
674static char *createTableStmt(Table *p){
675 int i, k, n;
676 char *zStmt;
677 char *zSep, *zSep2, *zEnd;
678 n = 0;
679 for(i=0; i<p->nCol; i++){
680 n += identLength(p->aCol[i].zName);
681 }
682 n += identLength(p->zName);
683 if( n<40 ){
684 zSep = "";
685 zSep2 = ",";
686 zEnd = ")";
687 }else{
688 zSep = "\n ";
689 zSep2 = ",\n ";
690 zEnd = "\n)";
691 }
drhe0bc4042002-06-25 01:09:11 +0000692 n += 35 + 6*p->nCol;
drh969fa7c2002-02-18 18:30:32 +0000693 zStmt = sqliteMalloc( n );
694 if( zStmt==0 ) return 0;
drhe0bc4042002-06-25 01:09:11 +0000695 strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000696 k = strlen(zStmt);
697 identPut(zStmt, &k, p->zName);
698 zStmt[k++] = '(';
699 for(i=0; i<p->nCol; i++){
700 strcpy(&zStmt[k], zSep);
701 k += strlen(&zStmt[k]);
702 zSep = zSep2;
703 identPut(zStmt, &k, p->aCol[i].zName);
704 }
705 strcpy(&zStmt[k], zEnd);
706 return zStmt;
707}
708
709/*
drh75897232000-05-29 14:26:00 +0000710** This routine is called to report the final ")" that terminates
711** a CREATE TABLE statement.
712**
drhf57b3392001-10-08 13:22:32 +0000713** The table structure that other action routines have been building
714** is added to the internal hash tables, assuming no errors have
715** occurred.
drh75897232000-05-29 14:26:00 +0000716**
drh1ccde152000-06-17 13:12:39 +0000717** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000718** unless this is a temporary table or initFlag==1. When initFlag==1,
719** it means we are reading the sqlite_master table because we just
720** connected to the database or because the sqlite_master table has
721** recently changes, so the entry for this table already exists in
722** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000723**
724** If the pSelect argument is not NULL, it means that this routine
725** was called to create a table generated from a
726** "CREATE TABLE ... AS SELECT ..." statement. The column names of
727** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000728*/
drh969fa7c2002-02-18 18:30:32 +0000729void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000730 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000731 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000732
drh969fa7c2002-02-18 18:30:32 +0000733 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000734 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000735 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000736
drhf57b3392001-10-08 13:22:32 +0000737 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000738 */
drhad75e982001-10-09 04:19:46 +0000739 assert( pParse->nameClash==0 || pParse->initFlag==1 );
drhf57b3392001-10-08 13:22:32 +0000740 if( pParse->explain==0 && pParse->nameClash==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000741 Table *pOld;
742 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
743 if( pOld ){
drh74e24cd2002-01-09 03:19:59 +0000744 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh6d4abfb2001-10-22 02:58:08 +0000745 return;
746 }
drh75897232000-05-29 14:26:00 +0000747 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000748 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000749 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000750 }
751
drh969fa7c2002-02-18 18:30:32 +0000752 /* If the table is generated from a SELECT, then construct the
753 ** list of columns and the text of the table.
754 */
755 if( pSelect ){
756 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000757 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000758 assert( p->aCol==0 );
759 p->nCol = pSelTab->nCol;
760 p->aCol = pSelTab->aCol;
761 pSelTab->nCol = 0;
762 pSelTab->aCol = 0;
763 sqliteDeleteTable(0, pSelTab);
764 }
765
drhd78eeee2001-09-13 16:18:53 +0000766 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000767 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
768 ** So do not write to the disk again. Extract the root page number
769 ** for the table from the pParse->newTnum field. (The page number
770 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000771 */
772 if( pParse->initFlag ){
773 p->tnum = pParse->newTnum;
774 }
775
drhe3c41372001-09-17 20:25:58 +0000776 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000777 ** in the SQLITE_MASTER table of the database. The record number
778 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000779 **
drhe0bc4042002-06-25 01:09:11 +0000780 ** If this is a TEMPORARY table, write the entry into the auxiliary
781 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000782 */
783 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000784 int n;
drhd8bc7082000-06-07 23:51:50 +0000785 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000786
drhd8bc7082000-06-07 23:51:50 +0000787 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000788 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000789 if( p->pSelect==0 ){
790 /* A regular table */
791 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
792 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
793 }else{
794 /* A view */
795 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
796 }
drh969fa7c2002-02-18 18:30:32 +0000797 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000798 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
799 sqliteVdbeAddOp(v, OP_String, 0, 0);
800 if( p->pSelect==0 ){
801 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
802 }else{
803 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000804 }
drhe0bc4042002-06-25 01:09:11 +0000805 sqliteVdbeAddOp(v, OP_String, 0, 0);
806 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
807 sqliteVdbeAddOp(v, OP_String, 0, 0);
808 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
809 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
810 sqliteVdbeAddOp(v, OP_String, 0, 0);
811 if( pSelect ){
812 char *z = createTableStmt(p);
813 n = z ? strlen(z) : 0;
814 sqliteVdbeChangeP3(v, -1, z, n);
815 sqliteFree(z);
816 }else{
817 assert( pEnd!=0 );
818 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
819 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
820 }
821 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
822 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
823 if( !p->isTemp ){
824 sqliteChangeCookie(db, v);
825 }
826 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000827 if( pSelect ){
828 int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
829 sqliteVdbeAddOp(v, op, 1, 0);
830 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000831 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000832 }
drh1c928532002-01-31 15:54:21 +0000833 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000834 }
835}
836
837/*
drha76b5df2002-02-23 02:32:10 +0000838** The parser calls this routine in order to create a new VIEW
839*/
840void sqliteCreateView(
841 Parse *pParse, /* The parsing context */
842 Token *pBegin, /* The CREATE token that begins the statement */
843 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000844 Select *pSelect, /* A SELECT statement that will become the new view */
845 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000846){
drha76b5df2002-02-23 02:32:10 +0000847 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000848 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000849 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000850 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000851
drh6276c1c2002-07-08 22:03:32 +0000852 sqliteStartTable(pParse, pBegin, pName, isTemp);
drha76b5df2002-02-23 02:32:10 +0000853 p = pParse->pNewTable;
drh417be792002-03-03 18:59:40 +0000854 if( p==0 ){
855 sqliteSelectDelete(pSelect);
856 return;
857 }
drh0f18b452002-05-08 21:30:15 +0000858 /* Ignore ORDER BY clauses on a SELECT */
859 if( pSelect->pOrderBy ){
860 sqliteExprListDelete(pSelect->pOrderBy);
861 pSelect->pOrderBy = 0;
862 }
drh4b59ab52002-08-24 18:24:51 +0000863 /* Make a copy of the entire SELECT statement that defines the view.
864 ** This will force all the Expr.token.z values to be dynamically
865 ** allocated rather than point to the input string - which means that
866 ** they will persist after the current sqlite_exec() call returns.
867 */
868 p->pSelect = sqliteSelectDup(pSelect);
869 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +0000870 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +0000871 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +0000872 }
drh4b59ab52002-08-24 18:24:51 +0000873
874 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
875 ** the end.
876 */
drha76b5df2002-02-23 02:32:10 +0000877 sEnd = pParse->sLastToken;
878 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
879 sEnd.z += sEnd.n;
880 }
881 sEnd.n = 0;
882 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000883 z = pBegin->z;
884 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
885 sEnd.z = &z[n-1];
886 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +0000887
888 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
889 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +0000890 return;
drh417be792002-03-03 18:59:40 +0000891}
drha76b5df2002-02-23 02:32:10 +0000892
drh417be792002-03-03 18:59:40 +0000893/*
894** The Table structure pTable is really a VIEW. Fill in the names of
895** the columns of the view in the pTable structure. Return the number
896** of errors. If an error is seen leave an error message in pPare->zErrMsg.
897*/
898int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
899 ExprList *pEList;
900 Select *pSel;
901 Table *pSelTab;
902 int nErr = 0;
903
904 assert( pTable );
905
906 /* A positive nCol means the columns names for this view are
907 ** already known.
908 */
909 if( pTable->nCol>0 ) return 0;
910
911 /* A negative nCol is a special marker meaning that we are currently
912 ** trying to compute the column names. If we enter this routine with
913 ** a negative nCol, it means two or more views form a loop, like this:
914 **
915 ** CREATE VIEW one AS SELECT * FROM two;
916 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +0000917 **
918 ** Actually, this error is caught previously and so the following test
919 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +0000920 */
921 if( pTable->nCol<0 ){
922 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
923 " is circularly defined", 0);
924 pParse->nErr++;
925 return 1;
926 }
927
928 /* If we get this far, it means we need to compute the table names.
929 */
930 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
931 pSel = pTable->pSelect;
932
933 /* Note that the call to sqliteResultSetOfSelect() will expand any
934 ** "*" elements in this list. But we will need to restore the list
935 ** back to its original configuration afterwards, so we save a copy of
936 ** the original in pEList.
937 */
938 pEList = pSel->pEList;
939 pSel->pEList = sqliteExprListDup(pEList);
940 if( pSel->pEList==0 ){
941 pSel->pEList = pEList;
942 return 1; /* Malloc failed */
943 }
944 pTable->nCol = -1;
945 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
946 if( pSelTab ){
947 assert( pTable->aCol==0 );
948 pTable->nCol = pSelTab->nCol;
949 pTable->aCol = pSelTab->aCol;
950 pSelTab->nCol = 0;
951 pSelTab->aCol = 0;
952 sqliteDeleteTable(0, pSelTab);
953 pParse->db->flags |= SQLITE_UnresetViews;
954 }else{
955 pTable->nCol = 0;
956 nErr++;
957 }
958 sqliteSelectUnbind(pSel);
959 sqliteExprListDelete(pSel->pEList);
960 pSel->pEList = pEList;
961 return nErr;
962}
963
964/*
965** Clear the column names from the VIEW pTable.
966**
967** This routine is called whenever any other table or view is modified.
968** The view passed into this routine might depend directly or indirectly
969** on the modified or deleted table so we need to clear the old column
970** names so that they will be recomputed.
971*/
972static void sqliteViewResetColumnNames(Table *pTable){
973 int i;
974 if( pTable==0 || pTable->pSelect==0 ) return;
975 if( pTable->nCol==0 ) return;
976 for(i=0; i<pTable->nCol; i++){
977 sqliteFree(pTable->aCol[i].zName);
978 sqliteFree(pTable->aCol[i].zDflt);
979 sqliteFree(pTable->aCol[i].zType);
980 }
981 sqliteFree(pTable->aCol);
982 pTable->aCol = 0;
983 pTable->nCol = 0;
984}
985
986/*
987** Clear the column names from every VIEW.
988*/
989void sqliteViewResetAll(sqlite *db){
990 HashElem *i;
991 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
992 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
993 Table *pTab = sqliteHashData(i);
994 if( pTab->pSelect ){
995 sqliteViewResetColumnNames(pTab);
996 }
997 }
998 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +0000999}
1000
1001/*
drh75897232000-05-29 14:26:00 +00001002** Given a token, look up a table with that name. If not found, leave
1003** an error for the parser to find and return NULL.
1004*/
drhcce7d172000-05-31 15:34:51 +00001005Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001006 char *zName;
1007 Table *pTab;
1008 zName = sqliteTableNameFromToken(pTok);
1009 if( zName==0 ) return 0;
1010 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +00001011 sqliteFree(zName);
1012 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001013 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1014 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001015 pParse->nErr++;
1016 }
1017 return pTab;
1018}
1019
1020/*
1021** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001022** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001023*/
drh4ff6dfa2002-03-03 23:06:00 +00001024void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001025 Table *pTable;
drh75897232000-05-29 14:26:00 +00001026 Vdbe *v;
1027 int base;
drh5edc3122001-09-13 21:53:09 +00001028 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001029
drhdaffd0e2001-04-11 14:28:42 +00001030 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001031 pTable = sqliteTableFromToken(pParse, pName);
1032 if( pTable==0 ) return;
1033 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001034 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1035 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001036 pParse->nErr++;
1037 return;
1038 }
drh4ff6dfa2002-03-03 23:06:00 +00001039 if( isView && pTable->pSelect==0 ){
1040 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1041 pTable->zName, 0);
1042 pParse->nErr++;
1043 return;
1044 }
1045 if( !isView && pTable->pSelect ){
1046 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1047 pTable->zName, 0);
1048 pParse->nErr++;
1049 return;
1050 }
drh75897232000-05-29 14:26:00 +00001051
drh1ccde152000-06-17 13:12:39 +00001052 /* Generate code to remove the table from the master table
1053 ** on disk.
1054 */
drhd8bc7082000-06-07 23:51:50 +00001055 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001056 if( v ){
1057 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001058 { OP_Rewind, 0, ADDR(8), 0},
1059 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001060 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001061 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001062 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001063 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001064 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001065 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001066 };
1067 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001068 Trigger *pTrigger;
drhc977f7f2002-05-21 11:38:11 +00001069 sqliteBeginWriteOperation(pParse, 0);
drhe0bc4042002-06-25 01:09:11 +00001070 sqliteOpenMasterTable(v, pTable->isTemp);
danielk1977c3f9bad2002-05-15 08:30:12 +00001071 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001072 pTrigger = pTable->pTrigger;
1073 while( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001074 Token tt;
1075 tt.z = pTable->pTrigger->name;
1076 tt.n = strlen(pTable->pTrigger->name);
1077 sqliteDropTrigger(pParse, &tt, 1);
drhe0bc4042002-06-25 01:09:11 +00001078 if( pParse->explain ){
1079 pTrigger = pTrigger->pNext;
1080 }else{
1081 pTrigger = pTable->pTrigger;
1082 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001083 }
drhe0bc4042002-06-25 01:09:11 +00001084 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1085 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001086 if( !pTable->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001087 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001088 }
drhe0bc4042002-06-25 01:09:11 +00001089 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001090 if( !isView ){
1091 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1092 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1093 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1094 }
drh5e00f6c2001-09-13 13:46:56 +00001095 }
drh1c928532002-01-31 15:54:21 +00001096 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001097 }
1098
drhe0bc4042002-06-25 01:09:11 +00001099 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001100 **
1101 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001102 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001103 */
1104 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001105 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001106 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001107 }
drh417be792002-03-03 18:59:40 +00001108 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001109}
1110
1111/*
drh38640e12002-07-05 21:42:36 +00001112** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1113** opcode and adds that P3 string to the most recently inserted instruction
1114** in the virtual machine. The P3 string consists of a single character
1115** for each column in the index pIdx of table pTab. If the column uses
1116** a numeric sort order, then the P3 string character corresponding to
1117** that column is 'n'. If the column uses a text sort order, then the
1118** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1119** additional information. See also the sqliteAddKeyType() routine.
1120*/
1121void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1122 char *zType;
1123 Table *pTab;
1124 int i, n;
1125 assert( pIdx!=0 && pIdx->pTable!=0 );
1126 pTab = pIdx->pTable;
1127 n = pIdx->nColumn;
1128 zType = sqliteMalloc( n+1 );
1129 if( zType==0 ) return;
1130 for(i=0; i<n; i++){
1131 int iCol = pIdx->aiColumn[i];
1132 assert( iCol>=0 && iCol<pTab->nCol );
1133 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1134 zType[i] = 't';
1135 }else{
1136 zType[i] = 'n';
1137 }
1138 }
1139 zType[n] = 0;
1140 sqliteVdbeChangeP3(v, -1, zType, n);
1141 sqliteFree(zType);
1142}
1143
1144/*
drh75897232000-05-29 14:26:00 +00001145** Create a new index for an SQL table. pIndex is the name of the index
1146** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001147** be NULL for a primary key or an index that is created to satisfy a
1148** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001149** as the table to be indexed. pParse->pNewTable is a table that is
1150** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001151**
drh382c0242001-10-06 16:33:02 +00001152** pList is a list of columns to be indexed. pList will be NULL if this
1153** is a primary key or unique-constraint on the most recent column added
1154** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001155*/
1156void sqliteCreateIndex(
1157 Parse *pParse, /* All information about this parse */
1158 Token *pName, /* Name of the index. May be NULL */
1159 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001160 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001161 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001162 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1163 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1164){
1165 Table *pTab; /* Table to be indexed */
1166 Index *pIndex; /* The index to be created */
1167 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001168 int i, j;
drhf57b3392001-10-08 13:22:32 +00001169 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001170 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001171 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001172
drhdaffd0e2001-04-11 14:28:42 +00001173 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1174
drh75897232000-05-29 14:26:00 +00001175 /*
1176 ** Find the table that is to be indexed. Return early if not found.
1177 */
1178 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001179 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001180 pTab = sqliteTableFromToken(pParse, pTable);
1181 }else{
drhe3c41372001-09-17 20:25:58 +00001182 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001183 pTab = pParse->pNewTable;
1184 }
1185 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1186 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001187 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1188 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001189 pParse->nErr++;
1190 goto exit_create_index;
1191 }
drha76b5df2002-02-23 02:32:10 +00001192 if( pTab->pSelect ){
1193 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1194 pParse->nErr++;
1195 goto exit_create_index;
1196 }
drh75897232000-05-29 14:26:00 +00001197
drhf57b3392001-10-08 13:22:32 +00001198 /* If this index is created while re-reading the schema from sqlite_master
1199 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001200 ** only mean that the table that this index is really associated with is
1201 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001202 ** Since its table has been suppressed, we need to also suppress the
1203 ** index.
1204 */
drhe0bc4042002-06-25 01:09:11 +00001205 if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){
drhf57b3392001-10-08 13:22:32 +00001206 goto exit_create_index;
1207 }
1208
drh75897232000-05-29 14:26:00 +00001209 /*
1210 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001211 ** index or table with the same name.
1212 **
1213 ** Exception: If we are reading the names of permanent indices from the
1214 ** sqlite_master table (because some other process changed the schema) and
1215 ** one of the index names collides with the name of a temporary table or
1216 ** index, then we will continue to process this index, but we will not
1217 ** store its name in the hash table. Set the hideName flag to accomplish
1218 ** this.
1219 **
1220 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001221 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1222 ** own name.
drh75897232000-05-29 14:26:00 +00001223 */
1224 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001225 Index *pISameName; /* Another index with the same name */
1226 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001227 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001228 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001229 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1230 if( pISameName->pTable->isTemp && pParse->initFlag ){
1231 hideName = 1;
1232 }else{
1233 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1234 " already exists", 0);
1235 pParse->nErr++;
1236 goto exit_create_index;
1237 }
drhe3c41372001-09-17 20:25:58 +00001238 }
drhf57b3392001-10-08 13:22:32 +00001239 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1240 if( pTSameName->isTemp && pParse->initFlag ){
1241 hideName = 1;
1242 }else{
1243 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1244 zName, 0);
1245 pParse->nErr++;
1246 goto exit_create_index;
1247 }
drhe3c41372001-09-17 20:25:58 +00001248 }
drh75897232000-05-29 14:26:00 +00001249 }else{
drhadbca9c2001-09-27 15:11:53 +00001250 char zBuf[30];
1251 int n;
1252 Index *pLoop;
1253 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1254 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001255 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001256 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001257 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001258 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001259 }
1260
1261 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001262 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001263 ** So create a fake list to simulate this.
1264 */
1265 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001266 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001267 nullId.n = strlen(nullId.z);
1268 pList = sqliteIdListAppend(0, &nullId);
1269 if( pList==0 ) goto exit_create_index;
1270 }
1271
1272 /*
1273 ** Allocate the index structure.
1274 */
drhdcc581c2000-05-30 13:44:19 +00001275 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001276 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001277 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001278 pIndex->aiColumn = (int*)&pIndex[1];
1279 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001280 strcpy(pIndex->zName, zName);
1281 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001282 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001283 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001284 pIndex->autoIndex = pName==0;
drh75897232000-05-29 14:26:00 +00001285
drh1ccde152000-06-17 13:12:39 +00001286 /* Scan the names of the columns of the table to be indexed and
1287 ** load the column indices into the Index structure. Report an error
1288 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001289 */
1290 for(i=0; i<pList->nId; i++){
1291 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001292 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001293 }
1294 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001295 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001296 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001297 pParse->nErr++;
1298 sqliteFree(pIndex);
1299 goto exit_create_index;
1300 }
drh967e8b72000-06-21 13:59:10 +00001301 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001302 }
1303
1304 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001305 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001306 */
drhf57b3392001-10-08 13:22:32 +00001307 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001308 Index *p;
1309 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1310 if( p ){
1311 assert( p==pIndex ); /* Malloc must have failed */
1312 sqliteFree(pIndex);
1313 goto exit_create_index;
1314 }
drh5e00f6c2001-09-13 13:46:56 +00001315 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001316 }
drh9cfcf5d2002-01-29 18:41:24 +00001317
1318 /* When adding an index to the list of indices for a table, make
1319 ** sure all indices labeled OE_Replace come after all those labeled
1320 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1321 ** and INSERT.
1322 */
1323 if( onError!=OE_Replace || pTab->pIndex==0
1324 || pTab->pIndex->onError==OE_Replace){
1325 pIndex->pNext = pTab->pIndex;
1326 pTab->pIndex = pIndex;
1327 }else{
1328 Index *pOther = pTab->pIndex;
1329 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1330 pOther = pOther->pNext;
1331 }
1332 pIndex->pNext = pOther->pNext;
1333 pOther->pNext = pIndex;
1334 }
drh75897232000-05-29 14:26:00 +00001335
drhd78eeee2001-09-13 16:18:53 +00001336 /* If the initFlag is 1 it means we are reading the SQL off the
1337 ** "sqlite_master" table on the disk. So do not write to the disk
1338 ** again. Extract the table number from the pParse->newTnum field.
1339 */
drhadbca9c2001-09-27 15:11:53 +00001340 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001341 pIndex->tnum = pParse->newTnum;
1342 }
1343
drh75897232000-05-29 14:26:00 +00001344 /* If the initFlag is 0 then create the index on disk. This
1345 ** involves writing the index into the master table and filling in the
1346 ** index with the current table contents.
1347 **
1348 ** The initFlag is 0 when the user first enters a CREATE INDEX
1349 ** command. The initFlag is 1 when a database is opened and
1350 ** CREATE INDEX statements are read out of the master table. In
1351 ** the latter case the index already exists on disk, which is why
1352 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001353 **
1354 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001355 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1356 ** has just been created, it contains no data and the index initialization
1357 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001358 */
drhadbca9c2001-09-27 15:11:53 +00001359 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001360 int n;
drhadbca9c2001-09-27 15:11:53 +00001361 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001362 int lbl1, lbl2;
1363 int i;
drhadbca9c2001-09-27 15:11:53 +00001364 int addr;
drhf57b3392001-10-08 13:22:32 +00001365 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001366
drhd8bc7082000-06-07 23:51:50 +00001367 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001368 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001369 if( pTable!=0 ){
drhc977f7f2002-05-21 11:38:11 +00001370 sqliteBeginWriteOperation(pParse, 0);
drhe0bc4042002-06-25 01:09:11 +00001371 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001372 }
drhe0bc4042002-06-25 01:09:11 +00001373 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1374 sqliteVdbeAddOp(v, OP_String, 0, 0);
1375 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1376 sqliteVdbeAddOp(v, OP_String, 0, 0);
1377 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1378 sqliteVdbeAddOp(v, OP_String, 0, 0);
1379 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001380 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1381 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001382 pIndex->tnum = 0;
1383 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001384 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001385 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001386 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001387 }else{
drh99fcd712001-10-13 01:06:47 +00001388 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001389 }
drh5e00f6c2001-09-13 13:46:56 +00001390 }
drhe0bc4042002-06-25 01:09:11 +00001391 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1392 if( pStart && pEnd ){
1393 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1394 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001395 }
drhe0bc4042002-06-25 01:09:11 +00001396 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1397 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001398 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001399 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1400 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001401 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001402 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1403 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001404 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001405 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001406 }
drh99fcd712001-10-13 01:06:47 +00001407 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001408 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001409 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001410 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001411 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001412 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1413 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001414 }
drhadbca9c2001-09-27 15:11:53 +00001415 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001416 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001417 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001418 }
drhe0bc4042002-06-25 01:09:11 +00001419 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001420 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001421 }
drh75897232000-05-29 14:26:00 +00001422 }
1423
drh75897232000-05-29 14:26:00 +00001424 /* Clean up before exiting */
1425exit_create_index:
1426 sqliteIdListDelete(pList);
1427 sqliteFree(zName);
1428 return;
1429}
1430
1431/*
drh74e24cd2002-01-09 03:19:59 +00001432** This routine will drop an existing named index. This routine
1433** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001434*/
1435void sqliteDropIndex(Parse *pParse, Token *pName){
1436 Index *pIndex;
1437 char *zName;
1438 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001439 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001440
drhdaffd0e2001-04-11 14:28:42 +00001441 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001442 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001443 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001444 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001445 sqliteFree(zName);
1446 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001447 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1448 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001449 pParse->nErr++;
1450 return;
1451 }
drh485b39b2002-07-13 03:11:52 +00001452 if( pIndex->autoIndex ){
1453 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1454 "or PRIMARY KEY constraint cannot be dropped", 0);
1455 pParse->nErr++;
1456 return;
1457 }
drh75897232000-05-29 14:26:00 +00001458
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[] = {
drhe0bc4042002-06-25 01:09:11 +00001463 { OP_Rewind, 0, ADDR(9), 0},
1464 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001465 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001466 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001467 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001468 { OP_Eq, 0, ADDR(8), 0},
1469 { OP_Next, 0, ADDR(3), 0},
1470 { OP_Goto, 0, ADDR(9), 0},
1471 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001472 };
1473 int base;
drhf57b3392001-10-08 13:22:32 +00001474 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001475
drhc977f7f2002-05-21 11:38:11 +00001476 sqliteBeginWriteOperation(pParse, 0);
drhe0bc4042002-06-25 01:09:11 +00001477 sqliteOpenMasterTable(v, pTab->isTemp);
1478 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1479 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001480 if( !pTab->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001481 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001482 }
drhe0bc4042002-06-25 01:09:11 +00001483 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001484 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001485 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001486 }
1487
drhe0bc4042002-06-25 01:09:11 +00001488 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001489 */
1490 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001491 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001492 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001493 }
1494}
1495
1496/*
drh75897232000-05-29 14:26:00 +00001497** Append a new element to the given IdList. Create a new IdList if
1498** need be.
drhdaffd0e2001-04-11 14:28:42 +00001499**
1500** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001501*/
1502IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1503 if( pList==0 ){
1504 pList = sqliteMalloc( sizeof(IdList) );
1505 if( pList==0 ) return 0;
1506 }
1507 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001508 struct IdList_item *a;
1509 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1510 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001511 sqliteIdListDelete(pList);
1512 return 0;
drh75897232000-05-29 14:26:00 +00001513 }
drh6d4abfb2001-10-22 02:58:08 +00001514 pList->a = a;
drh75897232000-05-29 14:26:00 +00001515 }
1516 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1517 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001518 char **pz = &pList->a[pList->nId].zName;
1519 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1520 if( *pz==0 ){
1521 sqliteIdListDelete(pList);
1522 return 0;
1523 }else{
1524 sqliteDequote(*pz);
1525 }
drh75897232000-05-29 14:26:00 +00001526 }
1527 pList->nId++;
1528 return pList;
1529}
1530
1531/*
drhad3cab52002-05-24 02:04:32 +00001532** Append a new table name to the given SrcList. Create a new SrcList if
1533** need be. A new entry is created in the SrcList even if pToken is NULL.
1534**
1535** A new SrcList is returned, or NULL if malloc() fails.
1536*/
1537SrcList *sqliteSrcListAppend(SrcList *pList, Token *pToken){
1538 if( pList==0 ){
1539 pList = sqliteMalloc( sizeof(IdList) );
1540 if( pList==0 ) return 0;
1541 }
1542 if( (pList->nSrc & 7)==0 ){
1543 struct SrcList_item *a;
1544 a = sqliteRealloc(pList->a, (pList->nSrc+8)*sizeof(pList->a[0]) );
1545 if( a==0 ){
1546 sqliteSrcListDelete(pList);
1547 return 0;
1548 }
1549 pList->a = a;
1550 }
1551 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
1552 if( pToken ){
1553 char **pz = &pList->a[pList->nSrc].zName;
1554 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1555 if( *pz==0 ){
1556 sqliteSrcListDelete(pList);
1557 return 0;
1558 }else{
1559 sqliteDequote(*pz);
1560 }
1561 }
1562 pList->nSrc++;
1563 return pList;
1564}
1565
1566/*
drh75897232000-05-29 14:26:00 +00001567** Add an alias to the last identifier on the given identifier list.
1568*/
drhad3cab52002-05-24 02:04:32 +00001569void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1570 if( pList && pList->nSrc>0 ){
1571 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001572 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001573 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001574 }
1575}
1576
1577/*
drhad3cab52002-05-24 02:04:32 +00001578** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001579*/
1580void sqliteIdListDelete(IdList *pList){
1581 int i;
1582 if( pList==0 ) return;
1583 for(i=0; i<pList->nId; i++){
1584 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001585 }
1586 sqliteFree(pList->a);
1587 sqliteFree(pList);
1588}
1589
1590/*
drhad2d8302002-05-24 20:31:36 +00001591** Return the index in pList of the identifier named zId. Return -1
1592** if not found.
1593*/
1594int sqliteIdListIndex(IdList *pList, const char *zName){
1595 int i;
1596 if( pList==0 ) return -1;
1597 for(i=0; i<pList->nId; i++){
1598 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1599 }
1600 return -1;
1601}
1602
1603/*
drhad3cab52002-05-24 02:04:32 +00001604** Delete an entire SrcList including all its substructure.
1605*/
1606void sqliteSrcListDelete(SrcList *pList){
1607 int i;
1608 if( pList==0 ) return;
1609 for(i=0; i<pList->nSrc; i++){
1610 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001611 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001612 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001613 sqliteDeleteTable(0, pList->a[i].pTab);
1614 }
drhff78bd22002-02-27 01:47:11 +00001615 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001616 sqliteExprDelete(pList->a[i].pOn);
1617 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001618 }
1619 sqliteFree(pList->a);
1620 sqliteFree(pList);
1621}
1622
drh982cef72000-05-30 16:27:03 +00001623/*
1624** The COPY command is for compatibility with PostgreSQL and specificially
1625** for the ability to read the output of pg_dump. The format is as
1626** follows:
1627**
1628** COPY table FROM file [USING DELIMITERS string]
1629**
1630** "table" is an existing table name. We will read lines of code from
1631** file to fill this table with data. File might be "stdin". The optional
1632** delimiter string identifies the field separators. The default is a tab.
1633*/
1634void sqliteCopy(
1635 Parse *pParse, /* The parser context */
1636 Token *pTableName, /* The name of the table into which we will insert */
1637 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001638 Token *pDelimiter, /* Use this as the field delimiter */
1639 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001640){
1641 Table *pTab;
1642 char *zTab;
drh1c928532002-01-31 15:54:21 +00001643 int i;
drh982cef72000-05-30 16:27:03 +00001644 Vdbe *v;
1645 int addr, end;
1646 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001647 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001648
1649 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001650 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001651 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001652 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001653 if( pTab==0 ) goto copy_cleanup;
drhd8bc7082000-06-07 23:51:50 +00001654 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001655 if( v ){
drhf57b3392001-10-08 13:22:32 +00001656 int openOp;
drhc977f7f2002-05-21 11:38:11 +00001657 sqliteBeginWriteOperation(pParse, 1);
drh99fcd712001-10-13 01:06:47 +00001658 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001659 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001660 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001661 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001662 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1663 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001664 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001665 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1666 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001667 }
drhb419a922002-01-30 16:17:23 +00001668 if( db->flags & SQLITE_CountRows ){
1669 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1670 }
drh982cef72000-05-30 16:27:03 +00001671 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001672 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001673 if( pDelimiter ){
1674 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1675 sqliteVdbeDequoteP3(v, addr);
1676 }else{
1677 sqliteVdbeChangeP3(v, addr, "\t", 1);
1678 }
drh8aff1012001-12-22 14:49:24 +00001679 if( pTab->iPKey>=0 ){
1680 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1681 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1682 }else{
1683 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1684 }
drh982cef72000-05-30 16:27:03 +00001685 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001686 if( i==pTab->iPKey ){
1687 /* The integer primary key column is filled with NULL since its
1688 ** value is always pulled from the record number */
1689 sqliteVdbeAddOp(v, OP_String, 0, 0);
1690 }else{
1691 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1692 }
drh982cef72000-05-30 16:27:03 +00001693 }
drhb419a922002-01-30 16:17:23 +00001694 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1695 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1696 if( (db->flags & SQLITE_CountRows)!=0 ){
1697 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001698 }
drh99fcd712001-10-13 01:06:47 +00001699 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1700 sqliteVdbeResolveLabel(v, end);
1701 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001702 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001703 if( db->flags & SQLITE_CountRows ){
1704 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1705 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1706 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1707 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1708 }
drh982cef72000-05-30 16:27:03 +00001709 }
1710
1711copy_cleanup:
1712 return;
1713}
drhdce2cbe2000-05-31 02:27:49 +00001714
1715/*
1716** The non-standard VACUUM command is used to clean up the database,
1717** collapse free space, etc. It is modelled after the VACUUM command
1718** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001719**
drh1bffb9c2002-02-03 17:37:36 +00001720** In version 1.0.x of SQLite, the VACUUM command would call
1721** gdbm_reorganize() on all the database tables. But beginning
1722** with 2.0.0, SQLite no longer uses GDBM so this command has
1723** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001724*/
1725void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001726 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001727}
drhc4a3c772001-04-04 11:48:57 +00001728
1729/*
1730** Begin a transaction
1731*/
drh1c928532002-01-31 15:54:21 +00001732void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001733 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001734
drhc4a3c772001-04-04 11:48:57 +00001735 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001736 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001737 if( db->flags & SQLITE_InTrans ){
1738 pParse->nErr++;
1739 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
1740 "within a transaction", 0);
1741 return;
1742 }
drhc977f7f2002-05-21 11:38:11 +00001743 sqliteBeginWriteOperation(pParse, 0);
drh5e00f6c2001-09-13 13:46:56 +00001744 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001745 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001746}
1747
1748/*
1749** Commit a transaction
1750*/
1751void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001752 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001753
drhc4a3c772001-04-04 11:48:57 +00001754 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001755 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001756 if( (db->flags & SQLITE_InTrans)==0 ){
1757 pParse->nErr++;
1758 sqliteSetString(&pParse->zErrMsg,
1759 "cannot commit - no transaction is active", 0);
1760 return;
1761 }
drh5e00f6c2001-09-13 13:46:56 +00001762 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001763 sqliteEndWriteOperation(pParse);
1764 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001765}
1766
1767/*
1768** Rollback a transaction
1769*/
1770void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001771 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001772 Vdbe *v;
1773
drhc4a3c772001-04-04 11:48:57 +00001774 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001775 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001776 if( (db->flags & SQLITE_InTrans)==0 ){
1777 pParse->nErr++;
1778 sqliteSetString(&pParse->zErrMsg,
1779 "cannot rollback - no transaction is active", 0);
1780 return;
1781 }
drh5e00f6c2001-09-13 13:46:56 +00001782 v = sqliteGetVdbe(pParse);
1783 if( v ){
drh99fcd712001-10-13 01:06:47 +00001784 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001785 }
drh5e00f6c2001-09-13 13:46:56 +00001786 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001787 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001788}
drhf57b14a2001-09-14 18:54:08 +00001789
1790/*
drh1c928532002-01-31 15:54:21 +00001791** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00001792** might change the database.
1793**
1794** This routine starts a new transaction if we are not already within
1795** a transaction. If we are already within a transaction, then a checkpoint
1796** is set if the setCheckpoint parameter is true. A checkpoint should
1797** be set for operations that might fail (due to a constraint) part of
1798** the way through and which will need to undo some writes without having to
1799** rollback the whole transaction. For operations where all constraints
1800** can be checked before any changes are made to the database, it is never
1801** necessary to undo a write and the checkpoint should not be set.
drh1c928532002-01-31 15:54:21 +00001802*/
drhc977f7f2002-05-21 11:38:11 +00001803void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint){
drh663fc632002-02-02 18:49:19 +00001804 Vdbe *v;
1805 v = sqliteGetVdbe(pParse);
1806 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00001807 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00001808 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
1809 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1810 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1811 pParse->schemaVerified = 1;
drhc977f7f2002-05-21 11:38:11 +00001812 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00001813 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1814 }
1815}
1816
1817/*
drh1c928532002-01-31 15:54:21 +00001818** Generate code that concludes an operation that may have changed
1819** the database. This is a companion function to BeginWriteOperation().
1820** If a transaction was started, then commit it. If a checkpoint was
1821** started then commit that.
1822*/
1823void sqliteEndWriteOperation(Parse *pParse){
1824 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00001825 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00001826 v = sqliteGetVdbe(pParse);
1827 if( v==0 ) return;
1828 if( pParse->db->flags & SQLITE_InTrans ){
1829 /* Do Nothing */
1830 }else{
1831 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
1832 }
1833}
1834
1835
1836/*
drhf57b14a2001-09-14 18:54:08 +00001837** Interpret the given string as a boolean value.
1838*/
1839static int getBoolean(char *z){
1840 static char *azTrue[] = { "yes", "on", "true" };
1841 int i;
1842 if( z[0]==0 ) return 0;
1843 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1844 return atoi(z);
1845 }
1846 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1847 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1848 }
1849 return 0;
1850}
1851
1852/*
1853** Process a pragma statement.
1854**
1855** Pragmas are of this form:
1856**
1857** PRAGMA id = value
1858**
1859** The identifier might also be a string. The value is a string, and
1860** identifier, or a number. If minusFlag is true, then the value is
1861** a number that was preceded by a minus sign.
1862*/
1863void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1864 char *zLeft = 0;
1865 char *zRight = 0;
1866 sqlite *db = pParse->db;
1867
1868 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1869 sqliteDequote(zLeft);
1870 if( minusFlag ){
1871 zRight = 0;
1872 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1873 }else{
1874 zRight = sqliteStrNDup(pRight->z, pRight->n);
1875 sqliteDequote(zRight);
1876 }
1877
drhcd61c282002-03-06 22:01:34 +00001878 /*
1879 ** PRAGMA default_cache_size
1880 ** PRAGMA default_cache_size=N
1881 **
1882 ** The first form reports the current persistent setting for the
1883 ** page cache size. The value returned is the maximum number of
1884 ** pages in the page cache. The second form sets both the current
1885 ** page cache size value and the persistent page cache size value
1886 ** stored in the database file.
1887 **
1888 ** The default cache size is stored in meta-value 2 of page 1 of the
1889 ** database file. The cache size is actually the absolute value of
1890 ** this memory location. The sign of meta-value 2 determines the
1891 ** synchronous setting. A negative value means synchronous is off
1892 ** and a positive value means synchronous is on.
1893 */
1894 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00001895 static VdbeOp getCacheSize[] = {
1896 { OP_ReadCookie, 0, 2, 0},
1897 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00001898 { OP_Dup, 0, 0, 0},
1899 { OP_Integer, 0, 0, 0},
1900 { OP_Ne, 0, 6, 0},
1901 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00001902 { OP_ColumnCount, 1, 0, 0},
1903 { OP_ColumnName, 0, 0, "cache_size"},
1904 { OP_Callback, 1, 0, 0},
1905 };
1906 Vdbe *v = sqliteGetVdbe(pParse);
1907 if( v==0 ) return;
1908 if( pRight->z==pLeft->z ){
1909 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1910 }else{
1911 int addr;
1912 int size = atoi(zRight);
1913 if( size<0 ) size = -size;
drhc977f7f2002-05-21 11:38:11 +00001914 sqliteBeginWriteOperation(pParse, 0);
drh603240c2002-03-05 01:11:12 +00001915 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1916 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
1917 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1918 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1919 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
1920 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
1921 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00001922 db->cache_size = db->cache_size<0 ? -size : size;
1923 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00001924 }
1925 }else
1926
drhcd61c282002-03-06 22:01:34 +00001927 /*
1928 ** PRAGMA cache_size
1929 ** PRAGMA cache_size=N
1930 **
1931 ** The first form reports the current local setting for the
1932 ** page cache size. The local setting can be different from
1933 ** the persistent cache size value that is stored in the database
1934 ** file itself. The value returned is the maximum number of
1935 ** pages in the page cache. The second form sets the local
1936 ** page cache size value. It does not change the persistent
1937 ** cache size stored on the disk so the cache size will revert
1938 ** to its default value when the database is closed and reopened.
1939 ** N should be a positive integer.
1940 */
1941 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1942 static VdbeOp getCacheSize[] = {
1943 { OP_ColumnCount, 1, 0, 0},
1944 { OP_ColumnName, 0, 0, "cache_size"},
1945 { OP_Callback, 1, 0, 0},
1946 };
1947 Vdbe *v = sqliteGetVdbe(pParse);
1948 if( v==0 ) return;
1949 if( pRight->z==pLeft->z ){
1950 int size = db->cache_size;;
1951 if( size<0 ) size = -size;
1952 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1953 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1954 }else{
1955 int size = atoi(zRight);
1956 if( size<0 ) size = -size;
1957 if( db->cache_size<0 ) size = -size;
1958 db->cache_size = size;
1959 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
1960 }
1961 }else
1962
1963 /*
1964 ** PRAGMA default_synchronous
1965 ** PRAGMA default_synchronous=BOOLEAN
1966 **
1967 ** The first form returns the persistent value of the "synchronous" setting
1968 ** that is stored in the database. This is the synchronous setting that
1969 ** is used whenever the database is opened unless overridden by a separate
1970 ** "synchronous" pragma. The second form changes the persistent and the
1971 ** local synchronous setting to the value given.
1972 **
1973 ** If synchronous is on, SQLite will do an fsync() system call at strategic
1974 ** points to insure that all previously written data has actually been
1975 ** written onto the disk surface before continuing. This mode insures that
1976 ** the database will always be in a consistent state event if the operating
1977 ** system crashes or power to the computer is interrupted unexpectedly.
1978 ** When synchronous is off, SQLite will not wait for changes to actually
1979 ** be written to the disk before continuing. As soon as it hands changes
1980 ** to the operating system, it assumes that the changes are permanent and
1981 ** it continues going. The database cannot be corrupted by a program crash
1982 ** even with synchronous off, but an operating system crash or power loss
1983 ** could potentially corrupt data. On the other hand, synchronous off is
1984 ** faster than synchronous on.
1985 */
1986 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00001987 static VdbeOp getSync[] = {
1988 { OP_Integer, 0, 0, 0},
1989 { OP_ReadCookie, 0, 2, 0},
1990 { OP_Integer, 0, 0, 0},
1991 { OP_Lt, 0, 5, 0},
1992 { OP_AddImm, 1, 0, 0},
1993 { OP_ColumnCount, 1, 0, 0},
1994 { OP_ColumnName, 0, 0, "synchronous"},
1995 { OP_Callback, 1, 0, 0},
1996 };
1997 Vdbe *v = sqliteGetVdbe(pParse);
1998 if( v==0 ) return;
1999 if( pRight->z==pLeft->z ){
2000 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2001 }else{
2002 int addr;
drhcd61c282002-03-06 22:01:34 +00002003 int size = db->cache_size;
2004 if( size<0 ) size = -size;
drhc977f7f2002-05-21 11:38:11 +00002005 sqliteBeginWriteOperation(pParse, 0);
drh603240c2002-03-05 01:11:12 +00002006 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002007 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2008 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2009 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2010 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002011 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
2012 if( !getBoolean(zRight) ){
2013 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002014 size = -size;
drh603240c2002-03-05 01:11:12 +00002015 }
2016 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2017 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002018 db->cache_size = size;
2019 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2020 }
2021 }else
2022
2023 /*
2024 ** PRAGMA synchronous
2025 ** PRAGMA synchronous=BOOLEAN
2026 **
2027 ** Return or set the local value of the synchronous flag. Changing
2028 ** the local value does not make changes to the disk file and the
2029 ** default value will be restored the next time the database is
2030 ** opened.
2031 */
2032 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2033 static VdbeOp getSync[] = {
2034 { OP_ColumnCount, 1, 0, 0},
2035 { OP_ColumnName, 0, 0, "synchronous"},
2036 { OP_Callback, 1, 0, 0},
2037 };
2038 Vdbe *v = sqliteGetVdbe(pParse);
2039 if( v==0 ) return;
2040 if( pRight->z==pLeft->z ){
2041 sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0);
2042 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2043 }else{
2044 int size = db->cache_size;
2045 if( size<0 ) size = -size;
2046 if( !getBoolean(zRight) ) size = -size;
2047 db->cache_size = size;
2048 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002049 }
drhf57b14a2001-09-14 18:54:08 +00002050 }else
2051
danielk1977c3f9bad2002-05-15 08:30:12 +00002052 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2053 if( getBoolean(zRight) ){
2054 always_code_trigger_setup = 1;
2055 }else{
2056 always_code_trigger_setup = 0;
2057 }
2058 }else
2059
drhf57b14a2001-09-14 18:54:08 +00002060 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2061 if( getBoolean(zRight) ){
2062 db->flags |= SQLITE_VdbeTrace;
2063 }else{
2064 db->flags &= ~SQLITE_VdbeTrace;
2065 }
2066 }else
2067
drh382c0242001-10-06 16:33:02 +00002068 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2069 if( getBoolean(zRight) ){
2070 db->flags |= SQLITE_FullColNames;
2071 }else{
2072 db->flags &= ~SQLITE_FullColNames;
2073 }
2074 }else
2075
drh5080aaa2002-07-11 12:18:16 +00002076 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2077 if( getBoolean(zRight) ){
2078 db->flags |= SQLITE_ReportTypes;
2079 }else{
2080 db->flags &= ~SQLITE_ReportTypes;
2081 }
2082 }else
2083
drhc3a64ba2001-11-22 00:01:27 +00002084 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2085 if( getBoolean(zRight) ){
2086 db->flags |= SQLITE_ResultDetails;
2087 }else{
2088 db->flags &= ~SQLITE_ResultDetails;
2089 }
2090 }else
2091
drh1bee3d72001-10-15 00:44:35 +00002092 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2093 if( getBoolean(zRight) ){
2094 db->flags |= SQLITE_CountRows;
2095 }else{
2096 db->flags &= ~SQLITE_CountRows;
2097 }
2098 }else
2099
drh6a535342001-10-19 16:44:56 +00002100 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2101 if( getBoolean(zRight) ){
2102 db->flags |= SQLITE_NullCallback;
2103 }else{
2104 db->flags &= ~SQLITE_NullCallback;
2105 }
2106 }else
2107
drh382c0242001-10-06 16:33:02 +00002108 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2109 Table *pTab;
2110 Vdbe *v;
2111 pTab = sqliteFindTable(db, zRight);
2112 if( pTab ) v = sqliteGetVdbe(pParse);
2113 if( pTab && v ){
2114 static VdbeOp tableInfoPreface[] = {
2115 { OP_ColumnCount, 5, 0, 0},
2116 { OP_ColumnName, 0, 0, "cid"},
2117 { OP_ColumnName, 1, 0, "name"},
2118 { OP_ColumnName, 2, 0, "type"},
2119 { OP_ColumnName, 3, 0, "notnull"},
2120 { OP_ColumnName, 4, 0, "dflt_value"},
2121 };
2122 int i;
2123 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002124 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002125 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002126 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2127 sqliteVdbeAddOp(v, OP_String, 0, 0);
2128 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2129 sqliteVdbeAddOp(v, OP_String, 0, 0);
2130 sqliteVdbeChangeP3(v, -1,
2131 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC);
2132 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2133 sqliteVdbeAddOp(v, OP_String, 0, 0);
2134 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2135 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002136 }
2137 }
2138 }else
2139
2140 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2141 Index *pIdx;
2142 Table *pTab;
2143 Vdbe *v;
2144 pIdx = sqliteFindIndex(db, zRight);
2145 if( pIdx ) v = sqliteGetVdbe(pParse);
2146 if( pIdx && v ){
2147 static VdbeOp tableInfoPreface[] = {
2148 { OP_ColumnCount, 3, 0, 0},
2149 { OP_ColumnName, 0, 0, "seqno"},
2150 { OP_ColumnName, 1, 0, "cid"},
2151 { OP_ColumnName, 2, 0, "name"},
2152 };
2153 int i;
2154 pTab = pIdx->pTable;
2155 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2156 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002157 int cnum = pIdx->aiColumn[i];
2158 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2159 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2160 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002161 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002162 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2163 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002164 }
2165 }
2166 }else
2167
drh81a20f22001-10-12 17:30:04 +00002168 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2169 Index *pIdx;
2170 Table *pTab;
2171 Vdbe *v;
2172 pTab = sqliteFindTable(db, zRight);
2173 if( pTab ){
2174 v = sqliteGetVdbe(pParse);
2175 pIdx = pTab->pIndex;
2176 }
2177 if( pTab && pIdx && v ){
2178 int i = 0;
2179 static VdbeOp indexListPreface[] = {
2180 { OP_ColumnCount, 3, 0, 0},
2181 { OP_ColumnName, 0, 0, "seq"},
2182 { OP_ColumnName, 1, 0, "name"},
2183 { OP_ColumnName, 2, 0, "unique"},
2184 };
2185
2186 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2187 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002188 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2189 sqliteVdbeAddOp(v, OP_String, 0, 0);
2190 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002191 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002192 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002193 ++i;
2194 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002195 }
2196 }
2197 }else
2198
drhf57b14a2001-09-14 18:54:08 +00002199#ifndef NDEBUG
2200 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2201 extern void sqliteParserTrace(FILE*, char *);
2202 if( getBoolean(zRight) ){
2203 sqliteParserTrace(stdout, "parser: ");
2204 }else{
2205 sqliteParserTrace(0, 0);
2206 }
2207 }else
2208#endif
2209
drhaaab5722002-02-19 13:39:21 +00002210 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002211 static VdbeOp checkDb[] = {
2212 { OP_SetInsert, 0, 0, "2"},
2213 { OP_Open, 0, 2, 0},
2214 { OP_Rewind, 0, 6, 0},
drh21504322002-06-25 13:16:02 +00002215 { OP_Column, 0, 3, 0}, /* 3 */
drh1bffb9c2002-02-03 17:37:36 +00002216 { OP_SetInsert, 0, 0, 0},
2217 { OP_Next, 0, 3, 0},
drh21504322002-06-25 13:16:02 +00002218 { OP_IntegrityCk, 0, 0, 0}, /* 6 */
drh1bffb9c2002-02-03 17:37:36 +00002219 { OP_ColumnCount, 1, 0, 0},
drh4ff6dfa2002-03-03 23:06:00 +00002220 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002221 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002222 { OP_SetInsert, 1, 0, "2"},
2223 { OP_OpenAux, 1, 2, 0},
2224 { OP_Rewind, 1, 16, 0},
2225 { OP_Column, 1, 3, 0}, /* 13 */
2226 { OP_SetInsert, 1, 0, 0},
2227 { OP_Next, 1, 13, 0},
2228 { OP_IntegrityCk, 1, 1, 0}, /* 16 */
2229 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002230 };
2231 Vdbe *v = sqliteGetVdbe(pParse);
2232 if( v==0 ) return;
2233 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2234 }else
drh1bffb9c2002-02-03 17:37:36 +00002235
drhf57b3392001-10-08 13:22:32 +00002236 {}
2237 sqliteFree(zLeft);
2238 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002239}