blob: ba6da93c3e387464291375c6711dfcaa454ba264 [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**
drh113088e2003-03-20 01:16:58 +000028** $Id: build.c,v 1.133 2003/03/20 01:16:58 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/*
drhb86ccfb2003-01-28 23:13:10 +000052** This is a fake callback procedure used when sqlite_exec() is
53** invoked with a NULL callback pointer. If we pass a NULL callback
54** pointer into sqliteVdbeExec() it will return at every OP_Callback,
55** which we do not want it to do. So we substitute a pointer to this
56** procedure in place of the NULL.
57*/
58static int fakeCallback(void *NotUsed, int n, char **az1, char **az2){
59 return 0;
60}
61
62/*
drh75897232000-05-29 14:26:00 +000063** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000064** parsed and we want to execute the VDBE code to implement
65** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000066** constructed VDBE code to do the work of the SQL statement.
67** This routine just has to execute the VDBE code.
68**
69** Note that if an error occurred, it might be the case that
70** no VDBE code was generated.
71*/
72void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000073 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000074 sqlite *db = pParse->db;
drhb86ccfb2003-01-28 23:13:10 +000075 Vdbe *v = pParse->pVdbe;
76 int (*xCallback)(void*,int,char**,char**);
77
drhdaffd0e2001-04-11 14:28:42 +000078 if( sqlite_malloc_failed ) return;
drhb86ccfb2003-01-28 23:13:10 +000079 xCallback = pParse->xCallback;
80 if( xCallback==0 && pParse->useCallback ) xCallback = fakeCallback;
81 if( v && pParse->nErr==0 ){
82 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
83 sqliteVdbeTrace(v, trace);
84 sqliteVdbeMakeReady(v, xCallback, pParse->pArg, pParse->explain);
85 if( pParse->useCallback ){
86 if( pParse->explain ){
87 rc = sqliteVdbeList(v);
drh001bbcb2003-03-19 03:14:00 +000088 db->next_cookie = db->aDb[0].schema_cookie;
drhb86ccfb2003-01-28 23:13:10 +000089 }else{
90 sqliteVdbeExec(v);
91 }
92 rc = sqliteVdbeFinalize(v, &pParse->zErrMsg);
drhecdc7532001-09-23 02:35:53 +000093 if( rc ) pParse->nErr++;
drhb86ccfb2003-01-28 23:13:10 +000094 pParse->pVdbe = 0;
95 pParse->rc = rc;
96 if( rc ) pParse->nErr++;
97 }else{
98 pParse->rc = pParse->nErr ? SQLITE_ERROR : SQLITE_DONE;
drh75897232000-05-29 14:26:00 +000099 }
drhd8bc7082000-06-07 23:51:50 +0000100 pParse->colNamesSet = 0;
drh50e5dad2001-09-15 00:57:28 +0000101 pParse->schemaVerified = 0;
drh483750b2003-01-29 18:46:51 +0000102 }else if( pParse->useCallback==0 ){
103 pParse->rc = SQLITE_ERROR;
drh75897232000-05-29 14:26:00 +0000104 }
drha226d052002-09-25 19:04:07 +0000105 pParse->nTab = 0;
106 pParse->nMem = 0;
107 pParse->nSet = 0;
108 pParse->nAgg = 0;
drh75897232000-05-29 14:26:00 +0000109}
110
111/*
drhf57b3392001-10-08 13:22:32 +0000112** Locate the in-memory structure that describes
113** a particular database table given the name
drh75897232000-05-29 14:26:00 +0000114** of that table. Return NULL if not found.
115*/
drha76b5df2002-02-23 02:32:10 +0000116Table *sqliteFindTable(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +0000117 Table *p;
118 p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +0000119 return p;
drh75897232000-05-29 14:26:00 +0000120}
121
122/*
drhf57b3392001-10-08 13:22:32 +0000123** Locate the in-memory structure that describes
124** a particular index given the name of that index.
125** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000126*/
drha76b5df2002-02-23 02:32:10 +0000127Index *sqliteFindIndex(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +0000128 Index *p;
129 p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +0000130 return p;
drh75897232000-05-29 14:26:00 +0000131}
132
133/*
134** Remove the given index from the index hash table, and free
135** its memory structures.
136**
drhd229ca92002-01-09 13:30:41 +0000137** The index is removed from the database hash tables but
138** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000139** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000140*/
drh74e24cd2002-01-09 03:19:59 +0000141static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000142 Index *pOld;
143 assert( db!=0 && p->zName!=0 );
144 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
145 if( pOld!=0 && pOld!=p ){
146 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000147 }
drh74e24cd2002-01-09 03:19:59 +0000148 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000149}
150
151/*
drhbeae3192001-09-22 18:12:08 +0000152** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000153** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000154** structures.
155*/
drh6d4abfb2001-10-22 02:58:08 +0000156void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000157 if( pIndex->pTable->pIndex==pIndex ){
158 pIndex->pTable->pIndex = pIndex->pNext;
159 }else{
160 Index *p;
161 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
162 if( p && p->pNext==pIndex ){
163 p->pNext = pIndex->pNext;
164 }
165 }
166 sqliteDeleteIndex(db, pIndex);
167}
168
169/*
drhe0bc4042002-06-25 01:09:11 +0000170** Erase all schema information from the in-memory hash tables of
171** database connection. This routine is called to reclaim memory
172** before the connection closes. It is also called during a rollback
173** if there were schema changes during the transaction.
drh74e24cd2002-01-09 03:19:59 +0000174*/
drhe0bc4042002-06-25 01:09:11 +0000175void sqliteResetInternalSchema(sqlite *db){
176 HashElem *pElem;
177 Hash temp1;
178 Hash temp2;
179
drhc2eef3b2002-08-31 18:53:06 +0000180 sqliteHashClear(&db->aFKey);
drhe0bc4042002-06-25 01:09:11 +0000181 temp1 = db->tblHash;
182 temp2 = db->trigHash;
183 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
184 sqliteHashClear(&db->idxHash);
185 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
186 Trigger *pTrigger = sqliteHashData(pElem);
187 sqliteDeleteTrigger(pTrigger);
drh74e24cd2002-01-09 03:19:59 +0000188 }
drhe0bc4042002-06-25 01:09:11 +0000189 sqliteHashClear(&temp2);
190 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
191 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
192 Table *pTab = sqliteHashData(pElem);
193 sqliteDeleteTable(db, pTab);
194 }
195 sqliteHashClear(&temp1);
196 db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges);
197}
198
199/*
200** This routine is called whenever a rollback occurs. If there were
201** schema changes during the transaction, then we have to reset the
202** internal hash tables and reload them from disk.
203*/
204void sqliteRollbackInternalChanges(sqlite *db){
205 if( db->flags & SQLITE_InternChanges ){
206 sqliteResetInternalSchema(db);
207 }
208}
209
210/*
211** This routine is called when a commit occurs.
212*/
213void sqliteCommitInternalChanges(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000214 db->aDb[0].schema_cookie = db->next_cookie;
drhe0bc4042002-06-25 01:09:11 +0000215 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000216}
217
218/*
drh75897232000-05-29 14:26:00 +0000219** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000220** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000221**
222** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000223** the table data structure from the hash table. Nor does it remove
224** foreign keys from the sqlite.aFKey hash table. But it does destroy
225** memory structures of the indices and foreign keys associated with
226** the table.
drhdaffd0e2001-04-11 14:28:42 +0000227**
228** Indices associated with the table are unlinked from the "db"
229** data structure if db!=NULL. If db==NULL, indices attached to
230** the table are deleted, but it is assumed they have already been
231** unlinked.
drh75897232000-05-29 14:26:00 +0000232*/
233void sqliteDeleteTable(sqlite *db, Table *pTable){
234 int i;
235 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000236 FKey *pFKey, *pNextFKey;
237
drh75897232000-05-29 14:26:00 +0000238 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000239
240 /* Delete all indices associated with this table
241 */
242 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
243 pNext = pIndex->pNext;
244 sqliteDeleteIndex(db, pIndex);
245 }
246
247 /* Delete all foreign keys associated with this table. The keys
248 ** should have already been unlinked from the db->aFKey hash table
249 */
250 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
251 pNextFKey = pFKey->pNextFrom;
252 assert( sqliteHashFind(&db->aFKey,pFKey->zTo,strlen(pFKey->zTo)+1)!=pFKey );
253 sqliteFree(pFKey);
254 }
255
256 /* Delete the Table structure itself.
257 */
drh75897232000-05-29 14:26:00 +0000258 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000259 sqliteFree(pTable->aCol[i].zName);
260 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000261 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000262 }
drh6e142f52000-06-08 13:36:40 +0000263 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000264 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000265 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000266 sqliteFree(pTable);
267}
268
269/*
drh5edc3122001-09-13 21:53:09 +0000270** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000271** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000272*/
drh74e24cd2002-01-09 03:19:59 +0000273static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000274 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000275 FKey *pF1, *pF2;
drhd229ca92002-01-09 13:30:41 +0000276 assert( db!=0 );
277 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
278 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000279 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
280 int nTo = strlen(pF1->zTo) + 1;
281 pF2 = sqliteHashFind(&db->aFKey, pF1->zTo, nTo);
282 if( pF2==pF1 ){
283 sqliteHashInsert(&db->aFKey, pF1->zTo, nTo, pF1->pNextTo);
284 }else{
285 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
286 if( pF2 ){
287 pF2->pNextTo = pF1->pNextTo;
288 }
289 }
290 }
drh74e24cd2002-01-09 03:19:59 +0000291 sqliteDeleteTable(db, p);
292}
293
294/*
drh1ccde152000-06-17 13:12:39 +0000295** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000296**
297** Space to hold the name is obtained from sqliteMalloc() and must
298** be freed by the calling function.
299*/
drhcce7d172000-05-31 15:34:51 +0000300char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000301 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000302 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000303 return zName;
304}
305
306/*
drhe0bc4042002-06-25 01:09:11 +0000307** Generate code to open the appropriate master table. The table
308** opened will be SQLITE_MASTER for persistent tables and
309** SQLITE_TEMP_MASTER for temporary tables. The table is opened
310** on cursor 0.
311*/
312void sqliteOpenMasterTable(Vdbe *v, int isTemp){
drh001bbcb2003-03-19 03:14:00 +0000313 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
314 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
drhe0bc4042002-06-25 01:09:11 +0000315}
316
317/*
drh75897232000-05-29 14:26:00 +0000318** Begin constructing a new table representation in memory. This is
319** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000320** to a CREATE TABLE statement. In particular, this routine is called
321** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000322** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000323** flag is true if the table should be stored in the auxiliary database
324** file instead of in the main database file. This is normally the case
325** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000326** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000327**
drhf57b3392001-10-08 13:22:32 +0000328** The new table record is initialized and put in pParse->pNewTable.
329** As more of the CREATE TABLE statement is parsed, additional action
330** routines will be called to add more information to this record.
331** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
332** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000333*/
drhe5f9c642003-01-13 23:27:31 +0000334void sqliteStartTable(
335 Parse *pParse, /* Parser context */
336 Token *pStart, /* The "CREATE" token */
337 Token *pName, /* Name of table or view to create */
338 int isTemp, /* True if this is a TEMP table */
339 int isView /* True if this is a VIEW */
340){
drh75897232000-05-29 14:26:00 +0000341 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000342 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000343 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000344 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000345 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000346
347 pParse->sFirstToken = *pStart;
348 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000349 if( zName==0 ) return;
drhe5f9c642003-01-13 23:27:31 +0000350#ifndef SQLITE_OMIT_AUTHORIZATION
351 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drh77ad4e42003-01-14 02:49:27 +0000352 sqliteFree(zName);
drhed6c8672003-01-12 18:02:16 +0000353 return;
354 }
drhe5f9c642003-01-13 23:27:31 +0000355 {
356 int code;
357 if( isView ){
358 if( isTemp ){
359 code = SQLITE_CREATE_TEMP_VIEW;
360 }else{
361 code = SQLITE_CREATE_VIEW;
362 }
363 }else{
364 if( isTemp ){
365 code = SQLITE_CREATE_TEMP_TABLE;
366 }else{
367 code = SQLITE_CREATE_TABLE;
368 }
369 }
370 if( sqliteAuthCheck(pParse, code, zName, 0) ){
drh77ad4e42003-01-14 02:49:27 +0000371 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000372 return;
373 }
374 }
375#endif
376
drhf57b3392001-10-08 13:22:32 +0000377
378 /* Before trying to create a temporary table, make sure the Btree for
379 ** holding temporary tables is open.
380 */
drh001bbcb2003-03-19 03:14:00 +0000381 if( isTemp && db->aDb[1].pBt==0 ){
382 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000383 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000384 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000385 "file for storing temporary tables", 0);
386 pParse->nErr++;
387 return;
388 }
389 if( db->flags & SQLITE_InTrans ){
drh001bbcb2003-03-19 03:14:00 +0000390 rc = sqliteBtreeBeginTrans(db->aDb[1].pBt);
drhf57b3392001-10-08 13:22:32 +0000391 if( rc!=SQLITE_OK ){
392 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000393 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000394 pParse->nErr++;
395 return;
396 }
397 }
398 }
399
400 /* Make sure the new table name does not collide with an existing
401 ** index or table name. Issue an error message if it does.
402 **
403 ** If we are re-reading the sqlite_master table because of a schema
404 ** change and a new permanent table is found whose name collides with
405 ** an existing temporary table, then ignore the new permanent table.
406 ** We will continue parsing, but the pParse->nameClash flag will be set
407 ** so we will know to discard the table record once parsing has finished.
408 */
drhbe0072d2001-09-13 14:46:09 +0000409 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000410 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000411 if( pTable->isTemp && pParse->initFlag ){
412 pParse->nameClash = 1;
413 }else{
414 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
415 " already exists", 0, 0);
416 sqliteFree(zName);
417 pParse->nErr++;
418 return;
419 }
420 }else{
421 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000422 }
drhf57b3392001-10-08 13:22:32 +0000423 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
424 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000425 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
426 zName, 0);
drh75897232000-05-29 14:26:00 +0000427 sqliteFree(zName);
428 pParse->nErr++;
429 return;
430 }
431 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000432 if( pTable==0 ){
433 sqliteFree(zName);
434 return;
435 }
drh75897232000-05-29 14:26:00 +0000436 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000437 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000438 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000439 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000440 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000441 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000442 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000443 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000444
445 /* Begin generating the code that will insert the table record into
446 ** the SQLITE_MASTER table. Note in particular that we must go ahead
447 ** and allocate the record number for the table entry now. Before any
448 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
449 ** indices to be created and the table record must come before the
450 ** indices. Hence, the record number for the table must be allocated
451 ** now.
452 */
drhadbca9c2001-09-27 15:11:53 +0000453 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000454 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000455 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000456 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
457 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000458 }
drhe0bc4042002-06-25 01:09:11 +0000459 sqliteOpenMasterTable(v, isTemp);
460 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
461 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
462 sqliteVdbeAddOp(v, OP_String, 0, 0);
463 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000464 }
drh75897232000-05-29 14:26:00 +0000465}
466
467/*
468** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000469**
470** The parser calls this routine once for each column declaration
471** in a CREATE TABLE statement. sqliteStartTable() gets called
472** first to get things going. Then this routine is called for each
473** column.
drh75897232000-05-29 14:26:00 +0000474*/
475void sqliteAddColumn(Parse *pParse, Token *pName){
476 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000477 int i;
478 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000479 Column *pCol;
drh75897232000-05-29 14:26:00 +0000480 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000481 sqliteSetNString(&z, pName->z, pName->n, 0);
482 if( z==0 ) return;
483 sqliteDequote(z);
484 for(i=0; i<p->nCol; i++){
485 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
486 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
487 pParse->nErr++;
488 sqliteFree(z);
489 return;
490 }
491 }
drh75897232000-05-29 14:26:00 +0000492 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000493 Column *aNew;
494 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
495 if( aNew==0 ) return;
496 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000497 }
drhc9b84a12002-06-20 11:36:48 +0000498 pCol = &p->aCol[p->nCol];
499 memset(pCol, 0, sizeof(p->aCol[0]));
500 pCol->zName = z;
501 pCol->sortOrder = SQLITE_SO_NUM;
502 p->nCol++;
drh75897232000-05-29 14:26:00 +0000503}
504
505/*
drh382c0242001-10-06 16:33:02 +0000506** This routine is called by the parser while in the middle of
507** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
508** been seen on a column. This routine sets the notNull flag on
509** the column currently under construction.
510*/
drh9cfcf5d2002-01-29 18:41:24 +0000511void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000512 Table *p;
513 int i;
514 if( (p = pParse->pNewTable)==0 ) return;
515 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000516 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000517}
518
519/*
520** This routine is called by the parser while in the middle of
521** parsing a CREATE TABLE statement. The pFirst token is the first
522** token in the sequence of tokens that describe the type of the
523** column currently under construction. pLast is the last token
524** in the sequence. Use this information to construct a string
525** that contains the typename of the column and store that string
526** in zType.
527*/
528void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
529 Table *p;
530 int i, j;
531 int n;
532 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000533 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000534 if( (p = pParse->pNewTable)==0 ) return;
535 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000536 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000537 pCol = &p->aCol[i];
538 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000539 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000540 sqliteSetNString(pz, pFirst->z, n, 0);
541 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000542 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000543 for(i=j=0; z[i]; i++){
544 int c = z[i];
545 if( isspace(c) ) continue;
546 z[j++] = c;
547 }
548 z[j] = 0;
drh3d037a92002-08-15 01:26:09 +0000549 if( pParse->db->file_format>=4 ){
drhfcb78a42003-01-18 20:11:05 +0000550 pCol->sortOrder = sqliteCollateType(z, n);
551 }else{
552 pCol->sortOrder = SQLITE_SO_NUM;
drhc9b84a12002-06-20 11:36:48 +0000553 }
drh382c0242001-10-06 16:33:02 +0000554}
555
556/*
drh7020f652000-06-03 18:06:52 +0000557** The given token is the default value for the last column added to
558** the table currently under construction. If "minusFlag" is true, it
559** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000560**
561** This routine is called by the parser while in the middle of
562** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000563*/
564void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
565 Table *p;
566 int i;
567 char **pz;
568 if( (p = pParse->pNewTable)==0 ) return;
569 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000570 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000571 pz = &p->aCol[i].zDflt;
572 if( minusFlag ){
573 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
574 }else{
575 sqliteSetNString(pz, pVal->z, pVal->n, 0);
576 }
577 sqliteDequote(*pz);
578}
579
580/*
drh4a324312001-12-21 14:30:42 +0000581** Designate the PRIMARY KEY for the table. pList is a list of names
582** of columns that form the primary key. If pList is NULL, then the
583** most recently added column of the table is the primary key.
584**
585** A table can have at most one primary key. If the table already has
586** a primary key (and this is the second primary key) then create an
587** error.
588**
589** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
590** then we will try to use that column as the row id. (Exception:
591** For backwards compatibility with older databases, do not do this
592** if the file format version number is less than 1.) Set the Table.iPKey
593** field of the table under construction to be the index of the
594** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
595** no INTEGER PRIMARY KEY.
596**
597** If the key is not an INTEGER PRIMARY KEY, then create a unique
598** index for the key. No index is created for INTEGER PRIMARY KEYs.
599*/
drh9cfcf5d2002-01-29 18:41:24 +0000600void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000601 Table *pTab = pParse->pNewTable;
602 char *zType = 0;
603 int iCol = -1;
drhe0194f22003-02-26 13:52:51 +0000604 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000605 if( pTab->hasPrimKey ){
606 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
607 "\" has more than one primary key", 0);
608 pParse->nErr++;
drhe0194f22003-02-26 13:52:51 +0000609 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000610 }
611 pTab->hasPrimKey = 1;
612 if( pList==0 ){
613 iCol = pTab->nCol - 1;
614 }else if( pList->nId==1 ){
615 for(iCol=0; iCol<pTab->nCol; iCol++){
616 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
617 }
618 }
619 if( iCol>=0 && iCol<pTab->nCol ){
620 zType = pTab->aCol[iCol].zType;
621 }
622 if( pParse->db->file_format>=1 &&
623 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
624 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000625 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000626 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000627 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000628 pList = 0;
drh4a324312001-12-21 14:30:42 +0000629 }
drhe0194f22003-02-26 13:52:51 +0000630
631primary_key_exit:
632 sqliteIdListDelete(pList);
633 return;
drh4a324312001-12-21 14:30:42 +0000634}
635
636/*
drhfcb78a42003-01-18 20:11:05 +0000637** Return the appropriate collating type given a type name.
638**
639** The collation type is text (SQLITE_SO_TEXT) if the type
640** name contains the character stream "text" or "blob" or
641** "clob". Any other type name is collated as numeric
642** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000643*/
drhfcb78a42003-01-18 20:11:05 +0000644int sqliteCollateType(const char *zType, int nType){
645 int i;
drhfcb78a42003-01-18 20:11:05 +0000646 for(i=0; i<nType-1; i++){
647 switch( zType[i] ){
648 case 'b':
649 case 'B': {
650 if( i<nType-3 && sqliteStrNICmp(&zType[i],"blob",4)==0 ){
651 return SQLITE_SO_TEXT;
652 }
653 break;
654 }
655 case 'c':
656 case 'C': {
657 if( i<nType-3 && (sqliteStrNICmp(&zType[i],"char",4)==0 ||
658 sqliteStrNICmp(&zType[i],"clob",4)==0)
659 ){
660 return SQLITE_SO_TEXT;
661 }
662 break;
663 }
664 case 'x':
665 case 'X': {
666 if( i>=2 && sqliteStrNICmp(&zType[i-2],"text",4)==0 ){
667 return SQLITE_SO_TEXT;
668 }
669 break;
670 }
671 default: {
672 break;
673 }
674 }
drh8e2ca022002-06-17 17:07:19 +0000675 }
drhfcb78a42003-01-18 20:11:05 +0000676 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000677}
678
679/*
680** This routine is called by the parser while in the middle of
681** parsing a CREATE TABLE statement. A "COLLATE" clause has
682** been seen on a column. This routine sets the Column.sortOrder on
683** the column currently under construction.
684*/
685void sqliteAddCollateType(Parse *pParse, int collType){
686 Table *p;
687 int i;
688 if( (p = pParse->pNewTable)==0 ) return;
689 i = p->nCol-1;
690 if( i>=0 ) p->aCol[i].sortOrder = collType;
691}
692
693/*
drh50e5dad2001-09-15 00:57:28 +0000694** Come up with a new random value for the schema cookie. Make sure
695** the new value is different from the old.
696**
697** The schema cookie is used to determine when the schema for the
698** database changes. After each schema change, the cookie value
699** changes. When a process first reads the schema it records the
700** cookie. Thereafter, whenever it goes to access the database,
701** it checks the cookie to make sure the schema has not changed
702** since it was last read.
703**
704** This plan is not completely bullet-proof. It is possible for
705** the schema to change multiple times and for the cookie to be
706** set back to prior value. But schema changes are infrequent
707** and the probability of hitting the same cookie value is only
708** 1 chance in 2^32. So we're safe enough.
709*/
drhe0bc4042002-06-25 01:09:11 +0000710void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh001bbcb2003-03-19 03:14:00 +0000711 if( db->next_cookie==db->aDb[0].schema_cookie ){
712 db->next_cookie = db->aDb[0].schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000713 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000714 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
715 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000716 }
717}
718
719/*
drh969fa7c2002-02-18 18:30:32 +0000720** Measure the number of characters needed to output the given
721** identifier. The number returned includes any quotes used
722** but does not include the null terminator.
723*/
724static int identLength(const char *z){
725 int n;
drh17f71932002-02-21 12:01:27 +0000726 int needQuote = 0;
727 for(n=0; *z; n++, z++){
728 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000729 }
drh17f71932002-02-21 12:01:27 +0000730 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000731}
732
733/*
734** Write an identifier onto the end of the given string. Add
735** quote characters as needed.
736*/
737static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000738 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000739 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000740 for(j=0; zIdent[j]; j++){
741 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
742 }
743 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
744 || sqliteKeywordCode(zIdent, j)!=TK_ID;
745 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000746 for(j=0; zIdent[j]; j++){
747 z[i++] = zIdent[j];
748 if( zIdent[j]=='\'' ) z[i++] = '\'';
749 }
drh17f71932002-02-21 12:01:27 +0000750 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000751 z[i] = 0;
752 *pIdx = i;
753}
754
755/*
756** Generate a CREATE TABLE statement appropriate for the given
757** table. Memory to hold the text of the statement is obtained
758** from sqliteMalloc() and must be freed by the calling function.
759*/
760static char *createTableStmt(Table *p){
761 int i, k, n;
762 char *zStmt;
763 char *zSep, *zSep2, *zEnd;
764 n = 0;
765 for(i=0; i<p->nCol; i++){
766 n += identLength(p->aCol[i].zName);
767 }
768 n += identLength(p->zName);
769 if( n<40 ){
770 zSep = "";
771 zSep2 = ",";
772 zEnd = ")";
773 }else{
774 zSep = "\n ";
775 zSep2 = ",\n ";
776 zEnd = "\n)";
777 }
drhe0bc4042002-06-25 01:09:11 +0000778 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000779 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000780 if( zStmt==0 ) return 0;
drhe0bc4042002-06-25 01:09:11 +0000781 strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000782 k = strlen(zStmt);
783 identPut(zStmt, &k, p->zName);
784 zStmt[k++] = '(';
785 for(i=0; i<p->nCol; i++){
786 strcpy(&zStmt[k], zSep);
787 k += strlen(&zStmt[k]);
788 zSep = zSep2;
789 identPut(zStmt, &k, p->aCol[i].zName);
790 }
791 strcpy(&zStmt[k], zEnd);
792 return zStmt;
793}
794
795/*
drh75897232000-05-29 14:26:00 +0000796** This routine is called to report the final ")" that terminates
797** a CREATE TABLE statement.
798**
drhf57b3392001-10-08 13:22:32 +0000799** The table structure that other action routines have been building
800** is added to the internal hash tables, assuming no errors have
801** occurred.
drh75897232000-05-29 14:26:00 +0000802**
drh1ccde152000-06-17 13:12:39 +0000803** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000804** unless this is a temporary table or initFlag==1. When initFlag==1,
805** it means we are reading the sqlite_master table because we just
806** connected to the database or because the sqlite_master table has
807** recently changes, so the entry for this table already exists in
808** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000809**
810** If the pSelect argument is not NULL, it means that this routine
811** was called to create a table generated from a
812** "CREATE TABLE ... AS SELECT ..." statement. The column names of
813** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000814*/
drh969fa7c2002-02-18 18:30:32 +0000815void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000816 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000817 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000818
drh969fa7c2002-02-18 18:30:32 +0000819 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000820 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000821 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000822
drh969fa7c2002-02-18 18:30:32 +0000823 /* If the table is generated from a SELECT, then construct the
824 ** list of columns and the text of the table.
825 */
826 if( pSelect ){
827 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000828 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000829 assert( p->aCol==0 );
830 p->nCol = pSelTab->nCol;
831 p->aCol = pSelTab->aCol;
832 pSelTab->nCol = 0;
833 pSelTab->aCol = 0;
834 sqliteDeleteTable(0, pSelTab);
835 }
836
drhd78eeee2001-09-13 16:18:53 +0000837 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000838 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
839 ** So do not write to the disk again. Extract the root page number
840 ** for the table from the pParse->newTnum field. (The page number
841 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000842 */
843 if( pParse->initFlag ){
844 p->tnum = pParse->newTnum;
845 }
846
drhe3c41372001-09-17 20:25:58 +0000847 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000848 ** in the SQLITE_MASTER table of the database. The record number
849 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000850 **
drhe0bc4042002-06-25 01:09:11 +0000851 ** If this is a TEMPORARY table, write the entry into the auxiliary
852 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000853 */
854 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000855 int n;
drhd8bc7082000-06-07 23:51:50 +0000856 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000857
drhd8bc7082000-06-07 23:51:50 +0000858 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000859 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000860 if( p->pSelect==0 ){
861 /* A regular table */
862 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
863 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
864 }else{
865 /* A view */
866 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
867 }
drh969fa7c2002-02-18 18:30:32 +0000868 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000869 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
870 sqliteVdbeAddOp(v, OP_String, 0, 0);
871 if( p->pSelect==0 ){
872 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
873 }else{
874 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000875 }
drhe0bc4042002-06-25 01:09:11 +0000876 sqliteVdbeAddOp(v, OP_String, 0, 0);
877 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
878 sqliteVdbeAddOp(v, OP_String, 0, 0);
879 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
880 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
881 sqliteVdbeAddOp(v, OP_String, 0, 0);
882 if( pSelect ){
883 char *z = createTableStmt(p);
884 n = z ? strlen(z) : 0;
885 sqliteVdbeChangeP3(v, -1, z, n);
886 sqliteFree(z);
887 }else{
888 assert( pEnd!=0 );
889 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
890 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
891 }
892 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
893 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
894 if( !p->isTemp ){
895 sqliteChangeCookie(db, v);
896 }
897 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000898 if( pSelect ){
drh001bbcb2003-03-19 03:14:00 +0000899 sqliteVdbeAddOp(v, OP_Integer, p->isTemp, 0);
900 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh969fa7c2002-02-18 18:30:32 +0000901 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000902 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000903 }
drh1c928532002-01-31 15:54:21 +0000904 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000905 }
drh17e9e292003-02-01 13:53:28 +0000906
907 /* Add the table to the in-memory representation of the database.
908 */
909 assert( pParse->nameClash==0 || pParse->initFlag==1 );
910 if( pParse->explain==0 && pParse->nameClash==0 && pParse->nErr==0 ){
911 Table *pOld;
912 FKey *pFKey;
913 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
914 if( pOld ){
915 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
916 return;
917 }
918 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
919 int nTo = strlen(pFKey->zTo) + 1;
920 pFKey->pNextTo = sqliteHashFind(&db->aFKey, pFKey->zTo, nTo);
921 sqliteHashInsert(&db->aFKey, pFKey->zTo, nTo, pFKey);
922 }
923 pParse->pNewTable = 0;
924 db->nTable++;
925 db->flags |= SQLITE_InternChanges;
926 }
drh75897232000-05-29 14:26:00 +0000927}
928
929/*
drha76b5df2002-02-23 02:32:10 +0000930** The parser calls this routine in order to create a new VIEW
931*/
932void sqliteCreateView(
933 Parse *pParse, /* The parsing context */
934 Token *pBegin, /* The CREATE token that begins the statement */
935 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000936 Select *pSelect, /* A SELECT statement that will become the new view */
937 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000938){
drha76b5df2002-02-23 02:32:10 +0000939 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000940 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000941 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000942 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000943
drhe5f9c642003-01-13 23:27:31 +0000944 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +0000945 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +0000946 if( p==0 || pParse->nErr ){
drh417be792002-03-03 18:59:40 +0000947 sqliteSelectDelete(pSelect);
948 return;
949 }
drh174b6192002-12-03 02:22:52 +0000950
drh4b59ab52002-08-24 18:24:51 +0000951 /* Make a copy of the entire SELECT statement that defines the view.
952 ** This will force all the Expr.token.z values to be dynamically
953 ** allocated rather than point to the input string - which means that
954 ** they will persist after the current sqlite_exec() call returns.
955 */
956 p->pSelect = sqliteSelectDup(pSelect);
957 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +0000958 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +0000959 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +0000960 }
drh4b59ab52002-08-24 18:24:51 +0000961
962 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
963 ** the end.
964 */
drha76b5df2002-02-23 02:32:10 +0000965 sEnd = pParse->sLastToken;
966 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
967 sEnd.z += sEnd.n;
968 }
969 sEnd.n = 0;
970 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000971 z = pBegin->z;
972 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
973 sEnd.z = &z[n-1];
974 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +0000975
976 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
977 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +0000978 return;
drh417be792002-03-03 18:59:40 +0000979}
drha76b5df2002-02-23 02:32:10 +0000980
drh417be792002-03-03 18:59:40 +0000981/*
982** The Table structure pTable is really a VIEW. Fill in the names of
983** the columns of the view in the pTable structure. Return the number
984** of errors. If an error is seen leave an error message in pPare->zErrMsg.
985*/
986int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
987 ExprList *pEList;
988 Select *pSel;
989 Table *pSelTab;
990 int nErr = 0;
991
992 assert( pTable );
993
994 /* A positive nCol means the columns names for this view are
995 ** already known.
996 */
997 if( pTable->nCol>0 ) return 0;
998
999 /* A negative nCol is a special marker meaning that we are currently
1000 ** trying to compute the column names. If we enter this routine with
1001 ** a negative nCol, it means two or more views form a loop, like this:
1002 **
1003 ** CREATE VIEW one AS SELECT * FROM two;
1004 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001005 **
1006 ** Actually, this error is caught previously and so the following test
1007 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001008 */
1009 if( pTable->nCol<0 ){
1010 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
1011 " is circularly defined", 0);
1012 pParse->nErr++;
1013 return 1;
1014 }
1015
1016 /* If we get this far, it means we need to compute the table names.
1017 */
1018 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1019 pSel = pTable->pSelect;
1020
1021 /* Note that the call to sqliteResultSetOfSelect() will expand any
1022 ** "*" elements in this list. But we will need to restore the list
1023 ** back to its original configuration afterwards, so we save a copy of
1024 ** the original in pEList.
1025 */
1026 pEList = pSel->pEList;
1027 pSel->pEList = sqliteExprListDup(pEList);
1028 if( pSel->pEList==0 ){
1029 pSel->pEList = pEList;
1030 return 1; /* Malloc failed */
1031 }
1032 pTable->nCol = -1;
1033 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1034 if( pSelTab ){
1035 assert( pTable->aCol==0 );
1036 pTable->nCol = pSelTab->nCol;
1037 pTable->aCol = pSelTab->aCol;
1038 pSelTab->nCol = 0;
1039 pSelTab->aCol = 0;
1040 sqliteDeleteTable(0, pSelTab);
1041 pParse->db->flags |= SQLITE_UnresetViews;
1042 }else{
1043 pTable->nCol = 0;
1044 nErr++;
1045 }
1046 sqliteSelectUnbind(pSel);
1047 sqliteExprListDelete(pSel->pEList);
1048 pSel->pEList = pEList;
1049 return nErr;
1050}
1051
1052/*
1053** Clear the column names from the VIEW pTable.
1054**
1055** This routine is called whenever any other table or view is modified.
1056** The view passed into this routine might depend directly or indirectly
1057** on the modified or deleted table so we need to clear the old column
1058** names so that they will be recomputed.
1059*/
1060static void sqliteViewResetColumnNames(Table *pTable){
1061 int i;
1062 if( pTable==0 || pTable->pSelect==0 ) return;
1063 if( pTable->nCol==0 ) return;
1064 for(i=0; i<pTable->nCol; i++){
1065 sqliteFree(pTable->aCol[i].zName);
1066 sqliteFree(pTable->aCol[i].zDflt);
1067 sqliteFree(pTable->aCol[i].zType);
1068 }
1069 sqliteFree(pTable->aCol);
1070 pTable->aCol = 0;
1071 pTable->nCol = 0;
1072}
1073
1074/*
1075** Clear the column names from every VIEW.
1076*/
1077void sqliteViewResetAll(sqlite *db){
1078 HashElem *i;
1079 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
1080 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
1081 Table *pTab = sqliteHashData(i);
1082 if( pTab->pSelect ){
1083 sqliteViewResetColumnNames(pTab);
1084 }
1085 }
1086 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001087}
1088
1089/*
drh75897232000-05-29 14:26:00 +00001090** Given a token, look up a table with that name. If not found, leave
1091** an error for the parser to find and return NULL.
1092*/
drhcce7d172000-05-31 15:34:51 +00001093Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001094 char *zName;
1095 Table *pTab;
1096 zName = sqliteTableNameFromToken(pTok);
1097 if( zName==0 ) return 0;
1098 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +00001099 sqliteFree(zName);
1100 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001101 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1102 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001103 pParse->nErr++;
1104 }
1105 return pTab;
1106}
1107
1108/*
1109** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001110** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001111*/
drh4ff6dfa2002-03-03 23:06:00 +00001112void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001113 Table *pTable;
drh75897232000-05-29 14:26:00 +00001114 Vdbe *v;
1115 int base;
drh5edc3122001-09-13 21:53:09 +00001116 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001117
drhdaffd0e2001-04-11 14:28:42 +00001118 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001119 pTable = sqliteTableFromToken(pParse, pName);
1120 if( pTable==0 ) return;
drhe5f9c642003-01-13 23:27:31 +00001121#ifndef SQLITE_OMIT_AUTHORIZATION
1122 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTable->isTemp),0)){
drhed6c8672003-01-12 18:02:16 +00001123 return;
1124 }
drhe5f9c642003-01-13 23:27:31 +00001125 {
1126 int code;
1127 if( isView ){
1128 if( pTable->isTemp ){
1129 code = SQLITE_DROP_TEMP_VIEW;
1130 }else{
1131 code = SQLITE_DROP_VIEW;
1132 }
1133 }else{
1134 if( pTable->isTemp ){
1135 code = SQLITE_DROP_TEMP_TABLE;
1136 }else{
1137 code = SQLITE_DROP_TABLE;
1138 }
1139 }
1140 if( sqliteAuthCheck(pParse, code, pTable->zName, 0) ){
1141 return;
1142 }
drh77ad4e42003-01-14 02:49:27 +00001143 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0) ){
1144 return;
1145 }
drhe5f9c642003-01-13 23:27:31 +00001146 }
1147#endif
drh75897232000-05-29 14:26:00 +00001148 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001149 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1150 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001151 pParse->nErr++;
1152 return;
1153 }
drh4ff6dfa2002-03-03 23:06:00 +00001154 if( isView && pTable->pSelect==0 ){
1155 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1156 pTable->zName, 0);
1157 pParse->nErr++;
1158 return;
1159 }
1160 if( !isView && pTable->pSelect ){
1161 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1162 pTable->zName, 0);
1163 pParse->nErr++;
1164 return;
1165 }
drh75897232000-05-29 14:26:00 +00001166
drh1ccde152000-06-17 13:12:39 +00001167 /* Generate code to remove the table from the master table
1168 ** on disk.
1169 */
drhd8bc7082000-06-07 23:51:50 +00001170 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001171 if( v ){
1172 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001173 { OP_Rewind, 0, ADDR(8), 0},
1174 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001175 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001176 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001177 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001178 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001179 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001180 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001181 };
1182 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001183 Trigger *pTrigger;
drhcabb0812002-09-14 13:47:32 +00001184 sqliteBeginWriteOperation(pParse, 0, pTable->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001185 sqliteOpenMasterTable(v, pTable->isTemp);
danielk1977c3f9bad2002-05-15 08:30:12 +00001186 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001187 pTrigger = pTable->pTrigger;
1188 while( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001189 Token tt;
1190 tt.z = pTable->pTrigger->name;
1191 tt.n = strlen(pTable->pTrigger->name);
1192 sqliteDropTrigger(pParse, &tt, 1);
drhe0bc4042002-06-25 01:09:11 +00001193 if( pParse->explain ){
1194 pTrigger = pTrigger->pNext;
1195 }else{
1196 pTrigger = pTable->pTrigger;
1197 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001198 }
drhe0bc4042002-06-25 01:09:11 +00001199 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1200 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001201 if( !pTable->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001202 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001203 }
drhe0bc4042002-06-25 01:09:11 +00001204 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001205 if( !isView ){
1206 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1207 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1208 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1209 }
drh5e00f6c2001-09-13 13:46:56 +00001210 }
drh1c928532002-01-31 15:54:21 +00001211 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001212 }
1213
drhe0bc4042002-06-25 01:09:11 +00001214 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001215 **
1216 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001217 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001218 */
1219 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001220 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001221 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001222 }
drh417be792002-03-03 18:59:40 +00001223 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001224}
1225
1226/*
drh38640e12002-07-05 21:42:36 +00001227** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1228** opcode and adds that P3 string to the most recently inserted instruction
1229** in the virtual machine. The P3 string consists of a single character
1230** for each column in the index pIdx of table pTab. If the column uses
1231** a numeric sort order, then the P3 string character corresponding to
1232** that column is 'n'. If the column uses a text sort order, then the
1233** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1234** additional information. See also the sqliteAddKeyType() routine.
1235*/
1236void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1237 char *zType;
1238 Table *pTab;
1239 int i, n;
1240 assert( pIdx!=0 && pIdx->pTable!=0 );
1241 pTab = pIdx->pTable;
1242 n = pIdx->nColumn;
drh8c1238a2003-01-02 14:43:55 +00001243 zType = sqliteMallocRaw( n+1 );
drh38640e12002-07-05 21:42:36 +00001244 if( zType==0 ) return;
1245 for(i=0; i<n; i++){
1246 int iCol = pIdx->aiColumn[i];
1247 assert( iCol>=0 && iCol<pTab->nCol );
1248 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1249 zType[i] = 't';
1250 }else{
1251 zType[i] = 'n';
1252 }
1253 }
1254 zType[n] = 0;
1255 sqliteVdbeChangeP3(v, -1, zType, n);
1256 sqliteFree(zType);
1257}
1258
1259/*
drhc2eef3b2002-08-31 18:53:06 +00001260** This routine is called to create a new foreign key on the table
1261** currently under construction. pFromCol determines which columns
1262** in the current table point to the foreign key. If pFromCol==0 then
1263** connect the key to the last column inserted. pTo is the name of
1264** the table referred to. pToCol is a list of tables in the other
1265** pTo table that the foreign key points to. flags contains all
1266** information about the conflict resolution algorithms specified
1267** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1268**
1269** An FKey structure is created and added to the table currently
1270** under construction in the pParse->pNewTable field. The new FKey
1271** is not linked into db->aFKey at this point - that does not happen
1272** until sqliteEndTable().
1273**
1274** The foreign key is set for IMMEDIATE processing. A subsequent call
1275** to sqliteDeferForeignKey() might change this to DEFERRED.
1276*/
1277void sqliteCreateForeignKey(
1278 Parse *pParse, /* Parsing context */
1279 IdList *pFromCol, /* Columns in this table that point to other table */
1280 Token *pTo, /* Name of the other table */
1281 IdList *pToCol, /* Columns in the other table */
1282 int flags /* Conflict resolution algorithms. */
1283){
1284 Table *p = pParse->pNewTable;
1285 int nByte;
1286 int i;
1287 int nCol;
1288 char *z;
1289 FKey *pFKey = 0;
1290
1291 assert( pTo!=0 );
1292 if( p==0 || pParse->nErr ) goto fk_end;
1293 if( pFromCol==0 ){
1294 int iCol = p->nCol-1;
1295 if( iCol<0 ) goto fk_end;
1296 if( pToCol && pToCol->nId!=1 ){
1297 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1298 p->aCol[iCol].zName, -1,
1299 " should reference only one column of table ", -1,
1300 pTo->z, pTo->n, 0);
1301 pParse->nErr++;
1302 goto fk_end;
1303 }
1304 nCol = 1;
1305 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1306 sqliteSetString(&pParse->zErrMsg,
1307 "number of columns in foreign key does not match the number of "
1308 "columns in the referenced table", 0);
1309 pParse->nErr++;
1310 goto fk_end;
1311 }else{
1312 nCol = pFromCol->nId;
1313 }
1314 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1315 if( pToCol ){
1316 for(i=0; i<pToCol->nId; i++){
1317 nByte += strlen(pToCol->a[i].zName) + 1;
1318 }
1319 }
1320 pFKey = sqliteMalloc( nByte );
1321 if( pFKey==0 ) goto fk_end;
1322 pFKey->pFrom = p;
1323 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001324 z = (char*)&pFKey[1];
1325 pFKey->aCol = (struct sColMap*)z;
1326 z += sizeof(struct sColMap)*nCol;
1327 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001328 memcpy(z, pTo->z, pTo->n);
1329 z[pTo->n] = 0;
1330 z += pTo->n+1;
1331 pFKey->pNextTo = 0;
1332 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001333 if( pFromCol==0 ){
1334 pFKey->aCol[0].iFrom = p->nCol-1;
1335 }else{
1336 for(i=0; i<nCol; i++){
1337 int j;
1338 for(j=0; j<p->nCol; j++){
1339 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1340 pFKey->aCol[i].iFrom = j;
1341 break;
1342 }
1343 }
1344 if( j>=p->nCol ){
1345 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1346 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1347 pParse->nErr++;
1348 goto fk_end;
1349 }
1350 }
1351 }
1352 if( pToCol ){
1353 for(i=0; i<nCol; i++){
1354 int n = strlen(pToCol->a[i].zName);
1355 pFKey->aCol[i].zCol = z;
1356 memcpy(z, pToCol->a[i].zName, n);
1357 z[n] = 0;
1358 z += n+1;
1359 }
1360 }
1361 pFKey->isDeferred = 0;
1362 pFKey->deleteConf = flags & 0xff;
1363 pFKey->updateConf = (flags >> 8 ) & 0xff;
1364 pFKey->insertConf = (flags >> 16 ) & 0xff;
1365
1366 /* Link the foreign key to the table as the last step.
1367 */
1368 p->pFKey = pFKey;
1369 pFKey = 0;
1370
1371fk_end:
1372 sqliteFree(pFKey);
1373 sqliteIdListDelete(pFromCol);
1374 sqliteIdListDelete(pToCol);
1375}
1376
1377/*
1378** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1379** clause is seen as part of a foreign key definition. The isDeferred
1380** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1381** The behavior of the most recently created foreign key is adjusted
1382** accordingly.
1383*/
1384void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1385 Table *pTab;
1386 FKey *pFKey;
1387 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1388 pFKey->isDeferred = isDeferred;
1389}
1390
1391/*
drh75897232000-05-29 14:26:00 +00001392** Create a new index for an SQL table. pIndex is the name of the index
1393** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001394** be NULL for a primary key or an index that is created to satisfy a
1395** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001396** as the table to be indexed. pParse->pNewTable is a table that is
1397** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001398**
drh382c0242001-10-06 16:33:02 +00001399** pList is a list of columns to be indexed. pList will be NULL if this
1400** is a primary key or unique-constraint on the most recent column added
1401** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001402*/
1403void sqliteCreateIndex(
1404 Parse *pParse, /* All information about this parse */
1405 Token *pName, /* Name of the index. May be NULL */
1406 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001407 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001408 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001409 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1410 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1411){
1412 Table *pTab; /* Table to be indexed */
1413 Index *pIndex; /* The index to be created */
1414 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001415 int i, j;
drhf57b3392001-10-08 13:22:32 +00001416 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001417 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001418 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001419
drhdaffd0e2001-04-11 14:28:42 +00001420 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1421
drh75897232000-05-29 14:26:00 +00001422 /*
1423 ** Find the table that is to be indexed. Return early if not found.
1424 */
1425 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001426 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001427 pTab = sqliteTableFromToken(pParse, pTable);
1428 }else{
drhe3c41372001-09-17 20:25:58 +00001429 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001430 pTab = pParse->pNewTable;
1431 }
1432 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1433 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001434 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1435 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001436 pParse->nErr++;
1437 goto exit_create_index;
1438 }
drha76b5df2002-02-23 02:32:10 +00001439 if( pTab->pSelect ){
1440 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1441 pParse->nErr++;
1442 goto exit_create_index;
1443 }
drh75897232000-05-29 14:26:00 +00001444
drhf57b3392001-10-08 13:22:32 +00001445 /* If this index is created while re-reading the schema from sqlite_master
1446 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001447 ** only mean that the table that this index is really associated with is
1448 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001449 ** Since its table has been suppressed, we need to also suppress the
1450 ** index.
1451 */
drhe0bc4042002-06-25 01:09:11 +00001452 if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){
drhf57b3392001-10-08 13:22:32 +00001453 goto exit_create_index;
1454 }
1455
drh75897232000-05-29 14:26:00 +00001456 /*
1457 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001458 ** index or table with the same name.
1459 **
1460 ** Exception: If we are reading the names of permanent indices from the
1461 ** sqlite_master table (because some other process changed the schema) and
1462 ** one of the index names collides with the name of a temporary table or
1463 ** index, then we will continue to process this index, but we will not
1464 ** store its name in the hash table. Set the hideName flag to accomplish
1465 ** this.
1466 **
1467 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001468 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1469 ** own name.
drh75897232000-05-29 14:26:00 +00001470 */
1471 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001472 Index *pISameName; /* Another index with the same name */
1473 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001474 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001475 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001476 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1477 if( pISameName->pTable->isTemp && pParse->initFlag ){
1478 hideName = 1;
1479 }else{
1480 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1481 " already exists", 0);
1482 pParse->nErr++;
1483 goto exit_create_index;
1484 }
drhe3c41372001-09-17 20:25:58 +00001485 }
drhf57b3392001-10-08 13:22:32 +00001486 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1487 if( pTSameName->isTemp && pParse->initFlag ){
1488 hideName = 1;
1489 }else{
1490 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1491 zName, 0);
1492 pParse->nErr++;
1493 goto exit_create_index;
1494 }
drhe3c41372001-09-17 20:25:58 +00001495 }
drh75897232000-05-29 14:26:00 +00001496 }else{
drhadbca9c2001-09-27 15:11:53 +00001497 char zBuf[30];
1498 int n;
1499 Index *pLoop;
1500 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1501 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001502 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001503 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001504 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001505 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001506 }
1507
drhe5f9c642003-01-13 23:27:31 +00001508 /* Check for authorization to create an index.
1509 */
1510#ifndef SQLITE_OMIT_AUTHORIZATION
1511 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(pTab->isTemp), 0) ){
1512 goto exit_create_index;
1513 }
1514 i = SQLITE_CREATE_INDEX;
1515 if( pTab->isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
1516 if( sqliteAuthCheck(pParse, i, zName, pTab->zName) ){
1517 goto exit_create_index;
1518 }
1519#endif
1520
drh75897232000-05-29 14:26:00 +00001521 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001522 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001523 ** So create a fake list to simulate this.
1524 */
1525 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001526 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001527 nullId.n = strlen(nullId.z);
1528 pList = sqliteIdListAppend(0, &nullId);
1529 if( pList==0 ) goto exit_create_index;
1530 }
1531
1532 /*
1533 ** Allocate the index structure.
1534 */
drhdcc581c2000-05-30 13:44:19 +00001535 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001536 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001537 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001538 pIndex->aiColumn = (int*)&pIndex[1];
1539 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001540 strcpy(pIndex->zName, zName);
1541 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001542 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001543 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001544 pIndex->autoIndex = pName==0;
drh75897232000-05-29 14:26:00 +00001545
drh1ccde152000-06-17 13:12:39 +00001546 /* Scan the names of the columns of the table to be indexed and
1547 ** load the column indices into the Index structure. Report an error
1548 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001549 */
1550 for(i=0; i<pList->nId; i++){
1551 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001552 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001553 }
1554 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001555 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001556 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001557 pParse->nErr++;
1558 sqliteFree(pIndex);
1559 goto exit_create_index;
1560 }
drh967e8b72000-06-21 13:59:10 +00001561 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001562 }
1563
1564 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001565 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001566 */
drhf57b3392001-10-08 13:22:32 +00001567 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001568 Index *p;
1569 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1570 if( p ){
1571 assert( p==pIndex ); /* Malloc must have failed */
1572 sqliteFree(pIndex);
1573 goto exit_create_index;
1574 }
drh5e00f6c2001-09-13 13:46:56 +00001575 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001576 }
drh9cfcf5d2002-01-29 18:41:24 +00001577
1578 /* When adding an index to the list of indices for a table, make
1579 ** sure all indices labeled OE_Replace come after all those labeled
1580 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1581 ** and INSERT.
1582 */
1583 if( onError!=OE_Replace || pTab->pIndex==0
1584 || pTab->pIndex->onError==OE_Replace){
1585 pIndex->pNext = pTab->pIndex;
1586 pTab->pIndex = pIndex;
1587 }else{
1588 Index *pOther = pTab->pIndex;
1589 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1590 pOther = pOther->pNext;
1591 }
1592 pIndex->pNext = pOther->pNext;
1593 pOther->pNext = pIndex;
1594 }
drh75897232000-05-29 14:26:00 +00001595
drhd78eeee2001-09-13 16:18:53 +00001596 /* If the initFlag is 1 it means we are reading the SQL off the
1597 ** "sqlite_master" table on the disk. So do not write to the disk
1598 ** again. Extract the table number from the pParse->newTnum field.
1599 */
drhadbca9c2001-09-27 15:11:53 +00001600 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001601 pIndex->tnum = pParse->newTnum;
1602 }
1603
drh75897232000-05-29 14:26:00 +00001604 /* If the initFlag is 0 then create the index on disk. This
1605 ** involves writing the index into the master table and filling in the
1606 ** index with the current table contents.
1607 **
1608 ** The initFlag is 0 when the user first enters a CREATE INDEX
1609 ** command. The initFlag is 1 when a database is opened and
1610 ** CREATE INDEX statements are read out of the master table. In
1611 ** the latter case the index already exists on disk, which is why
1612 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001613 **
1614 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001615 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1616 ** has just been created, it contains no data and the index initialization
1617 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001618 */
drhadbca9c2001-09-27 15:11:53 +00001619 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001620 int n;
drhadbca9c2001-09-27 15:11:53 +00001621 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001622 int lbl1, lbl2;
1623 int i;
drhadbca9c2001-09-27 15:11:53 +00001624 int addr;
drhf57b3392001-10-08 13:22:32 +00001625 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001626
drhd8bc7082000-06-07 23:51:50 +00001627 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001628 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001629 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001630 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001631 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001632 }
drhe0bc4042002-06-25 01:09:11 +00001633 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1634 sqliteVdbeAddOp(v, OP_String, 0, 0);
1635 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1636 sqliteVdbeAddOp(v, OP_String, 0, 0);
1637 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1638 sqliteVdbeAddOp(v, OP_String, 0, 0);
1639 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001640 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1641 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001642 pIndex->tnum = 0;
1643 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001644 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00001645 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
1646 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drh5e00f6c2001-09-13 13:46:56 +00001647 }
drhe0bc4042002-06-25 01:09:11 +00001648 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1649 if( pStart && pEnd ){
1650 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1651 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001652 }
drhe0bc4042002-06-25 01:09:11 +00001653 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1654 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001655 if( pTable ){
drh001bbcb2003-03-19 03:14:00 +00001656 sqliteVdbeAddOp(v, OP_Integer, isTemp, 0);
1657 sqliteVdbeAddOp(v, OP_OpenRead, 2, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00001658 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001659 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001660 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1661 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001662 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001663 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001664 }
drh99fcd712001-10-13 01:06:47 +00001665 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001666 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001667 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh483750b2003-01-29 18:46:51 +00001668 sqliteVdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
drh6b563442001-11-07 16:48:26 +00001669 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001670 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001671 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1672 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001673 }
drhadbca9c2001-09-27 15:11:53 +00001674 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001675 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001676 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001677 }
drhe0bc4042002-06-25 01:09:11 +00001678 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001679 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001680 }
drh75897232000-05-29 14:26:00 +00001681 }
1682
drh75897232000-05-29 14:26:00 +00001683 /* Clean up before exiting */
1684exit_create_index:
1685 sqliteIdListDelete(pList);
1686 sqliteFree(zName);
1687 return;
1688}
1689
1690/*
drh74e24cd2002-01-09 03:19:59 +00001691** This routine will drop an existing named index. This routine
1692** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001693*/
1694void sqliteDropIndex(Parse *pParse, Token *pName){
1695 Index *pIndex;
1696 char *zName;
1697 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001698 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001699
drhdaffd0e2001-04-11 14:28:42 +00001700 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001701 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001702 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001703 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001704 sqliteFree(zName);
1705 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001706 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1707 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001708 pParse->nErr++;
1709 return;
1710 }
drh485b39b2002-07-13 03:11:52 +00001711 if( pIndex->autoIndex ){
1712 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1713 "or PRIMARY KEY constraint cannot be dropped", 0);
1714 pParse->nErr++;
1715 return;
1716 }
drhe5f9c642003-01-13 23:27:31 +00001717#ifndef SQLITE_OMIT_AUTHORIZATION
1718 {
1719 int code = SQLITE_DROP_INDEX;
1720 Table *pTab = pIndex->pTable;
1721 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTab->isTemp), 0) ){
1722 return;
1723 }
1724 if( pTab->isTemp ) code = SQLITE_DROP_TEMP_INDEX;
drh77ad4e42003-01-14 02:49:27 +00001725 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName) ){
drhe5f9c642003-01-13 23:27:31 +00001726 return;
1727 }
drhed6c8672003-01-12 18:02:16 +00001728 }
drhe5f9c642003-01-13 23:27:31 +00001729#endif
drh75897232000-05-29 14:26:00 +00001730
1731 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001732 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001733 if( v ){
1734 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001735 { OP_Rewind, 0, ADDR(9), 0},
1736 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001737 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001738 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001739 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001740 { OP_Eq, 0, ADDR(8), 0},
1741 { OP_Next, 0, ADDR(3), 0},
1742 { OP_Goto, 0, ADDR(9), 0},
1743 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001744 };
1745 int base;
drhf57b3392001-10-08 13:22:32 +00001746 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001747
drhcabb0812002-09-14 13:47:32 +00001748 sqliteBeginWriteOperation(pParse, 0, pTab->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001749 sqliteOpenMasterTable(v, pTab->isTemp);
1750 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1751 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001752 if( !pTab->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001753 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001754 }
drhe0bc4042002-06-25 01:09:11 +00001755 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001756 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001757 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001758 }
1759
drhe0bc4042002-06-25 01:09:11 +00001760 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001761 */
1762 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001763 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001764 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001765 }
1766}
1767
1768/*
drh75897232000-05-29 14:26:00 +00001769** Append a new element to the given IdList. Create a new IdList if
1770** need be.
drhdaffd0e2001-04-11 14:28:42 +00001771**
1772** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001773*/
1774IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1775 if( pList==0 ){
1776 pList = sqliteMalloc( sizeof(IdList) );
1777 if( pList==0 ) return 0;
1778 }
1779 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001780 struct IdList_item *a;
1781 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1782 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001783 sqliteIdListDelete(pList);
1784 return 0;
drh75897232000-05-29 14:26:00 +00001785 }
drh6d4abfb2001-10-22 02:58:08 +00001786 pList->a = a;
drh75897232000-05-29 14:26:00 +00001787 }
1788 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1789 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001790 char **pz = &pList->a[pList->nId].zName;
1791 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1792 if( *pz==0 ){
1793 sqliteIdListDelete(pList);
1794 return 0;
1795 }else{
1796 sqliteDequote(*pz);
1797 }
drh75897232000-05-29 14:26:00 +00001798 }
1799 pList->nId++;
1800 return pList;
1801}
1802
1803/*
drhad3cab52002-05-24 02:04:32 +00001804** Append a new table name to the given SrcList. Create a new SrcList if
1805** need be. A new entry is created in the SrcList even if pToken is NULL.
1806**
1807** A new SrcList is returned, or NULL if malloc() fails.
drh113088e2003-03-20 01:16:58 +00001808**
1809** If pDatabase is not null, it means that the table has an optional
1810** database name prefix. Like this: "database.table". The pDatabase
1811** points to the table name and the pTable points to the database name.
1812** The SrcList.a[].zName field is filled with the table name which might
1813** come from pTable (if pDatabase is NULL) or from pDatabase.
1814** SrcList.a[].zDatabase is filled with the database name from pTable,
1815** or with NULL if no database is specified.
1816**
1817** In other words, if call like this:
1818**
1819** sqliteSrcListAppend(A,B,0);
1820**
1821** Then B is a table name and the database name is unspecified. If called
1822** like this:
1823**
1824** sqliteSrcListAppend(A,B,C);
1825**
1826** Then C is the table name and B is the database name.
drhad3cab52002-05-24 02:04:32 +00001827*/
drh113088e2003-03-20 01:16:58 +00001828SrcList *sqliteSrcListAppend(SrcList *pList, Token *pTable, Token *pDatabase){
drhad3cab52002-05-24 02:04:32 +00001829 if( pList==0 ){
drh113088e2003-03-20 01:16:58 +00001830 pList = sqliteMalloc( sizeof(SrcList) );
drhad3cab52002-05-24 02:04:32 +00001831 if( pList==0 ) return 0;
1832 }
drh113088e2003-03-20 01:16:58 +00001833 if( (pList->nSrc & 7)==1 ){
1834 SrcList *pNew;
1835 pNew = sqliteRealloc(pList,
1836 sizeof(*pList) + (pList->nSrc+8)*sizeof(pList->a[0]) );
1837 if( pNew==0 ){
drhad3cab52002-05-24 02:04:32 +00001838 sqliteSrcListDelete(pList);
1839 return 0;
1840 }
drh113088e2003-03-20 01:16:58 +00001841 pList = pNew;
drhad3cab52002-05-24 02:04:32 +00001842 }
1843 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
drh113088e2003-03-20 01:16:58 +00001844 if( pDatabase && pDatabase->z==0 ){
1845 pDatabase = 0;
1846 }
1847 if( pDatabase && pTable ){
1848 Token *pTemp = pDatabase;
1849 pDatabase = pTable;
1850 pTable = pTemp;
1851 }
1852 if( pTable ){
drhad3cab52002-05-24 02:04:32 +00001853 char **pz = &pList->a[pList->nSrc].zName;
drh113088e2003-03-20 01:16:58 +00001854 sqliteSetNString(pz, pTable->z, pTable->n, 0);
1855 if( *pz==0 ){
1856 sqliteSrcListDelete(pList);
1857 return 0;
1858 }else{
1859 sqliteDequote(*pz);
1860 }
1861 }
1862 if( pDatabase ){
1863 char **pz = &pList->a[pList->nSrc].zDatabase;
1864 sqliteSetNString(pz, pDatabase->z, pDatabase->n, 0);
drhad3cab52002-05-24 02:04:32 +00001865 if( *pz==0 ){
1866 sqliteSrcListDelete(pList);
1867 return 0;
1868 }else{
1869 sqliteDequote(*pz);
1870 }
1871 }
1872 pList->nSrc++;
1873 return pList;
1874}
1875
1876/*
drh75897232000-05-29 14:26:00 +00001877** Add an alias to the last identifier on the given identifier list.
1878*/
drhad3cab52002-05-24 02:04:32 +00001879void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1880 if( pList && pList->nSrc>0 ){
1881 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001882 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001883 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001884 }
1885}
1886
1887/*
drhad3cab52002-05-24 02:04:32 +00001888** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001889*/
1890void sqliteIdListDelete(IdList *pList){
1891 int i;
1892 if( pList==0 ) return;
1893 for(i=0; i<pList->nId; i++){
1894 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001895 }
1896 sqliteFree(pList->a);
1897 sqliteFree(pList);
1898}
1899
1900/*
drhad2d8302002-05-24 20:31:36 +00001901** Return the index in pList of the identifier named zId. Return -1
1902** if not found.
1903*/
1904int sqliteIdListIndex(IdList *pList, const char *zName){
1905 int i;
1906 if( pList==0 ) return -1;
1907 for(i=0; i<pList->nId; i++){
1908 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1909 }
1910 return -1;
1911}
1912
1913/*
drhad3cab52002-05-24 02:04:32 +00001914** Delete an entire SrcList including all its substructure.
1915*/
1916void sqliteSrcListDelete(SrcList *pList){
1917 int i;
1918 if( pList==0 ) return;
1919 for(i=0; i<pList->nSrc; i++){
drh113088e2003-03-20 01:16:58 +00001920 sqliteFree(pList->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +00001921 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001922 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001923 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001924 sqliteDeleteTable(0, pList->a[i].pTab);
1925 }
drhff78bd22002-02-27 01:47:11 +00001926 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001927 sqliteExprDelete(pList->a[i].pOn);
1928 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001929 }
drh75897232000-05-29 14:26:00 +00001930 sqliteFree(pList);
1931}
1932
drh982cef72000-05-30 16:27:03 +00001933/*
1934** The COPY command is for compatibility with PostgreSQL and specificially
1935** for the ability to read the output of pg_dump. The format is as
1936** follows:
1937**
1938** COPY table FROM file [USING DELIMITERS string]
1939**
1940** "table" is an existing table name. We will read lines of code from
1941** file to fill this table with data. File might be "stdin". The optional
1942** delimiter string identifies the field separators. The default is a tab.
1943*/
1944void sqliteCopy(
1945 Parse *pParse, /* The parser context */
1946 Token *pTableName, /* The name of the table into which we will insert */
1947 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001948 Token *pDelimiter, /* Use this as the field delimiter */
1949 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001950){
1951 Table *pTab;
1952 char *zTab;
drh1c928532002-01-31 15:54:21 +00001953 int i;
drh982cef72000-05-30 16:27:03 +00001954 Vdbe *v;
1955 int addr, end;
1956 Index *pIdx;
drh77ad4e42003-01-14 02:49:27 +00001957 char *zFile = 0;
drhbe0072d2001-09-13 14:46:09 +00001958 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001959
drh77ad4e42003-01-14 02:49:27 +00001960
drh982cef72000-05-30 16:27:03 +00001961 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001962 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001963 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001964 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001965 if( pTab==0 ) goto copy_cleanup;
drh77ad4e42003-01-14 02:49:27 +00001966 zFile = sqliteStrNDup(pFilename->z, pFilename->n);
1967 sqliteDequote(zFile);
1968 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, zFile)
1969 || sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile) ){
drhed6c8672003-01-12 18:02:16 +00001970 goto copy_cleanup;
1971 }
drhd8bc7082000-06-07 23:51:50 +00001972 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001973 if( v ){
drhcabb0812002-09-14 13:47:32 +00001974 sqliteBeginWriteOperation(pParse, 1, pTab->isTemp);
drh99fcd712001-10-13 01:06:47 +00001975 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001976 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001977 sqliteVdbeDequoteP3(v, addr);
drh001bbcb2003-03-19 03:14:00 +00001978 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
1979 sqliteVdbeAddOp(v, OP_OpenWrite, 0, pTab->tnum);
drh99fcd712001-10-13 01:06:47 +00001980 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001981 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh001bbcb2003-03-19 03:14:00 +00001982 sqliteVdbeAddOp(v, OP_Integer, pTab->isTemp, 0);
1983 sqliteVdbeAddOp(v, OP_OpenWrite, i, pIdx->tnum);
drh99fcd712001-10-13 01:06:47 +00001984 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001985 }
drhb419a922002-01-30 16:17:23 +00001986 if( db->flags & SQLITE_CountRows ){
1987 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1988 }
drh982cef72000-05-30 16:27:03 +00001989 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001990 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001991 if( pDelimiter ){
1992 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1993 sqliteVdbeDequoteP3(v, addr);
1994 }else{
1995 sqliteVdbeChangeP3(v, addr, "\t", 1);
1996 }
drh8aff1012001-12-22 14:49:24 +00001997 if( pTab->iPKey>=0 ){
1998 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1999 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
2000 }else{
2001 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
2002 }
drh982cef72000-05-30 16:27:03 +00002003 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00002004 if( i==pTab->iPKey ){
2005 /* The integer primary key column is filled with NULL since its
2006 ** value is always pulled from the record number */
2007 sqliteVdbeAddOp(v, OP_String, 0, 0);
2008 }else{
2009 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
2010 }
drh982cef72000-05-30 16:27:03 +00002011 }
drhb419a922002-01-30 16:17:23 +00002012 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
2013 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
2014 if( (db->flags & SQLITE_CountRows)!=0 ){
2015 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00002016 }
drh99fcd712001-10-13 01:06:47 +00002017 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
2018 sqliteVdbeResolveLabel(v, end);
2019 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00002020 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00002021 if( db->flags & SQLITE_CountRows ){
drhb419a922002-01-30 16:17:23 +00002022 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
2023 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
2024 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
2025 }
drh982cef72000-05-30 16:27:03 +00002026 }
2027
2028copy_cleanup:
drh77ad4e42003-01-14 02:49:27 +00002029 sqliteFree(zFile);
drh982cef72000-05-30 16:27:03 +00002030 return;
2031}
drhdce2cbe2000-05-31 02:27:49 +00002032
2033/*
2034** The non-standard VACUUM command is used to clean up the database,
2035** collapse free space, etc. It is modelled after the VACUUM command
2036** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00002037**
drh1bffb9c2002-02-03 17:37:36 +00002038** In version 1.0.x of SQLite, the VACUUM command would call
2039** gdbm_reorganize() on all the database tables. But beginning
2040** with 2.0.0, SQLite no longer uses GDBM so this command has
2041** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00002042*/
2043void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00002044 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00002045}
drhc4a3c772001-04-04 11:48:57 +00002046
2047/*
2048** Begin a transaction
2049*/
drh1c928532002-01-31 15:54:21 +00002050void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002051 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002052
drh001bbcb2003-03-19 03:14:00 +00002053 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002054 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002055 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002056 if( db->flags & SQLITE_InTrans ){
2057 pParse->nErr++;
2058 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
2059 "within a transaction", 0);
2060 return;
2061 }
drhcabb0812002-09-14 13:47:32 +00002062 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002063 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002064 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00002065}
2066
2067/*
2068** Commit a transaction
2069*/
2070void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002071 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002072
drh001bbcb2003-03-19 03:14:00 +00002073 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002074 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002075 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002076 if( (db->flags & SQLITE_InTrans)==0 ){
2077 pParse->nErr++;
2078 sqliteSetString(&pParse->zErrMsg,
2079 "cannot commit - no transaction is active", 0);
2080 return;
2081 }
drh5e00f6c2001-09-13 13:46:56 +00002082 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002083 sqliteEndWriteOperation(pParse);
2084 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002085}
2086
2087/*
2088** Rollback a transaction
2089*/
2090void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002091 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002092 Vdbe *v;
2093
drh001bbcb2003-03-19 03:14:00 +00002094 if( pParse==0 || (db=pParse->db)==0 || db->aDb[0].pBt==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002095 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002096 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002097 if( (db->flags & SQLITE_InTrans)==0 ){
2098 pParse->nErr++;
2099 sqliteSetString(&pParse->zErrMsg,
2100 "cannot rollback - no transaction is active", 0);
2101 return;
2102 }
drh5e00f6c2001-09-13 13:46:56 +00002103 v = sqliteGetVdbe(pParse);
2104 if( v ){
drh99fcd712001-10-13 01:06:47 +00002105 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002106 }
drh5e00f6c2001-09-13 13:46:56 +00002107 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002108 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002109}
drhf57b14a2001-09-14 18:54:08 +00002110
2111/*
drh001bbcb2003-03-19 03:14:00 +00002112** Generate VDBE code that will verify the schema cookie for all
2113** named database files.
2114*/
2115void sqliteCodeVerifySchema(Parse *pParse){
2116 int i;
2117 sqlite *db = pParse->db;
2118 Vdbe *v = sqliteGetVdbe(pParse);
2119 for(i=0; i<db->nDb; i++){
drh113088e2003-03-20 01:16:58 +00002120 if( i==1 || db->aDb[i].pBt==0 ) continue;
drh001bbcb2003-03-19 03:14:00 +00002121 sqliteVdbeAddOp(v, OP_VerifyCookie, 0, db->aDb[i].schema_cookie);
2122 }
2123 pParse->schemaVerified = 1;
2124}
2125
2126/*
drh1c928532002-01-31 15:54:21 +00002127** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002128** might change the database.
2129**
2130** This routine starts a new transaction if we are not already within
2131** a transaction. If we are already within a transaction, then a checkpoint
2132** is set if the setCheckpoint parameter is true. A checkpoint should
2133** be set for operations that might fail (due to a constraint) part of
2134** the way through and which will need to undo some writes without having to
2135** rollback the whole transaction. For operations where all constraints
2136** can be checked before any changes are made to the database, it is never
2137** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002138**
2139** The tempOnly flag indicates that only temporary tables will be changed
2140** during this write operation. The primary database table is not
2141** write-locked. Only the temporary database file gets a write lock.
2142** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00002143*/
drhcabb0812002-09-14 13:47:32 +00002144void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00002145 Vdbe *v;
2146 v = sqliteGetVdbe(pParse);
2147 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00002148 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00002149 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh001bbcb2003-03-19 03:14:00 +00002150 sqliteVdbeAddOp(v, OP_Transaction, 1, 0);
drhcabb0812002-09-14 13:47:32 +00002151 if( !tempOnly ){
drh001bbcb2003-03-19 03:14:00 +00002152 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
2153 sqliteCodeVerifySchema(pParse);
drhcabb0812002-09-14 13:47:32 +00002154 }
drhc977f7f2002-05-21 11:38:11 +00002155 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00002156 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
drh001bbcb2003-03-19 03:14:00 +00002157 sqliteVdbeAddOp(v, OP_Checkpoint, 1, 0);
drh663fc632002-02-02 18:49:19 +00002158 }
2159}
2160
2161/*
drh1c928532002-01-31 15:54:21 +00002162** Generate code that concludes an operation that may have changed
2163** the database. This is a companion function to BeginWriteOperation().
2164** If a transaction was started, then commit it. If a checkpoint was
2165** started then commit that.
2166*/
2167void sqliteEndWriteOperation(Parse *pParse){
2168 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002169 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002170 v = sqliteGetVdbe(pParse);
2171 if( v==0 ) return;
2172 if( pParse->db->flags & SQLITE_InTrans ){
2173 /* Do Nothing */
2174 }else{
2175 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2176 }
2177}
2178
2179
2180/*
drhf57b14a2001-09-14 18:54:08 +00002181** Interpret the given string as a boolean value.
2182*/
2183static int getBoolean(char *z){
2184 static char *azTrue[] = { "yes", "on", "true" };
2185 int i;
2186 if( z[0]==0 ) return 0;
2187 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2188 return atoi(z);
2189 }
2190 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
2191 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
2192 }
2193 return 0;
2194}
2195
2196/*
drh973b6e32003-02-12 14:09:42 +00002197** Interpret the given string as a safety level. Return 0 for OFF,
2198** 1 for ON or NORMAL and 2 for FULL.
2199**
2200** Note that the values returned are one less that the values that
2201** should be passed into sqliteBtreeSetSafetyLevel(). The is done
2202** to support legacy SQL code. The safety level used to be boolean
2203** and older scripts may have used numbers 0 for OFF and 1 for ON.
2204*/
2205static int getSafetyLevel(char *z){
2206 static const struct {
2207 const char *zWord;
2208 int val;
2209 } aKey[] = {
2210 { "no", 0 },
2211 { "off", 0 },
2212 { "false", 0 },
2213 { "yes", 1 },
2214 { "on", 1 },
2215 { "true", 1 },
2216 { "full", 2 },
2217 };
2218 int i;
2219 if( z[0]==0 ) return 1;
2220 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2221 return atoi(z);
2222 }
2223 for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){
2224 if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val;
2225 }
2226 return 1;
2227}
2228
2229/*
drhf57b14a2001-09-14 18:54:08 +00002230** Process a pragma statement.
2231**
2232** Pragmas are of this form:
2233**
2234** PRAGMA id = value
2235**
2236** The identifier might also be a string. The value is a string, and
2237** identifier, or a number. If minusFlag is true, then the value is
2238** a number that was preceded by a minus sign.
2239*/
2240void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
2241 char *zLeft = 0;
2242 char *zRight = 0;
2243 sqlite *db = pParse->db;
drhdde85d92003-03-01 19:45:34 +00002244 Vdbe *v = sqliteGetVdbe(pParse);
2245 if( v==0 ) return;
drhf57b14a2001-09-14 18:54:08 +00002246
2247 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
2248 sqliteDequote(zLeft);
2249 if( minusFlag ){
2250 zRight = 0;
2251 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
2252 }else{
2253 zRight = sqliteStrNDup(pRight->z, pRight->n);
2254 sqliteDequote(zRight);
2255 }
drhbf0c78a2003-01-14 02:54:08 +00002256 if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight) ){
2257 sqliteFree(zLeft);
2258 sqliteFree(zRight);
2259 return;
2260 }
drhf57b14a2001-09-14 18:54:08 +00002261
drhcd61c282002-03-06 22:01:34 +00002262 /*
2263 ** PRAGMA default_cache_size
2264 ** PRAGMA default_cache_size=N
2265 **
2266 ** The first form reports the current persistent setting for the
2267 ** page cache size. The value returned is the maximum number of
2268 ** pages in the page cache. The second form sets both the current
2269 ** page cache size value and the persistent page cache size value
2270 ** stored in the database file.
2271 **
2272 ** The default cache size is stored in meta-value 2 of page 1 of the
2273 ** database file. The cache size is actually the absolute value of
2274 ** this memory location. The sign of meta-value 2 determines the
2275 ** synchronous setting. A negative value means synchronous is off
2276 ** and a positive value means synchronous is on.
2277 */
2278 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00002279 static VdbeOp getCacheSize[] = {
2280 { OP_ReadCookie, 0, 2, 0},
2281 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00002282 { OP_Dup, 0, 0, 0},
2283 { OP_Integer, 0, 0, 0},
2284 { OP_Ne, 0, 6, 0},
2285 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00002286 { OP_ColumnName, 0, 0, "cache_size"},
2287 { OP_Callback, 1, 0, 0},
2288 };
drh603240c2002-03-05 01:11:12 +00002289 if( pRight->z==pLeft->z ){
2290 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2291 }else{
2292 int addr;
2293 int size = atoi(zRight);
2294 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002295 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002296 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2297 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2298 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2299 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2300 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2301 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2302 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002303 db->cache_size = db->cache_size<0 ? -size : size;
drh001bbcb2003-03-19 03:14:00 +00002304 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002305 }
2306 }else
2307
drhcd61c282002-03-06 22:01:34 +00002308 /*
2309 ** PRAGMA cache_size
2310 ** PRAGMA cache_size=N
2311 **
2312 ** The first form reports the current local setting for the
2313 ** page cache size. The local setting can be different from
2314 ** the persistent cache size value that is stored in the database
2315 ** file itself. The value returned is the maximum number of
2316 ** pages in the page cache. The second form sets the local
2317 ** page cache size value. It does not change the persistent
2318 ** cache size stored on the disk so the cache size will revert
2319 ** to its default value when the database is closed and reopened.
2320 ** N should be a positive integer.
2321 */
2322 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2323 static VdbeOp getCacheSize[] = {
drhcd61c282002-03-06 22:01:34 +00002324 { OP_ColumnName, 0, 0, "cache_size"},
2325 { OP_Callback, 1, 0, 0},
2326 };
drhcd61c282002-03-06 22:01:34 +00002327 if( pRight->z==pLeft->z ){
2328 int size = db->cache_size;;
2329 if( size<0 ) size = -size;
2330 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2331 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2332 }else{
2333 int size = atoi(zRight);
2334 if( size<0 ) size = -size;
2335 if( db->cache_size<0 ) size = -size;
2336 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002337 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
drhcd61c282002-03-06 22:01:34 +00002338 }
2339 }else
2340
2341 /*
2342 ** PRAGMA default_synchronous
drh973b6e32003-02-12 14:09:42 +00002343 ** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002344 **
2345 ** The first form returns the persistent value of the "synchronous" setting
2346 ** that is stored in the database. This is the synchronous setting that
2347 ** is used whenever the database is opened unless overridden by a separate
2348 ** "synchronous" pragma. The second form changes the persistent and the
2349 ** local synchronous setting to the value given.
2350 **
drh973b6e32003-02-12 14:09:42 +00002351 ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls
2352 ** to make sure data is committed to disk. Write operations are very fast,
2353 ** but a power failure can leave the database in an inconsistent state.
2354 ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to
2355 ** make sure data is being written to disk. The risk of corruption due to
2356 ** a power loss in this mode is negligible but non-zero. If synchronous
2357 ** is FULL, extra fsync()s occur to reduce the risk of corruption to near
2358 ** zero, but with a write performance penalty. The default mode is NORMAL.
drhcd61c282002-03-06 22:01:34 +00002359 */
2360 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00002361 static VdbeOp getSync[] = {
drh973b6e32003-02-12 14:09:42 +00002362 { OP_ColumnName, 0, 0, "synchronous"},
2363 { OP_ReadCookie, 0, 3, 0},
2364 { OP_Dup, 0, 0, 0},
2365 { OP_If, 0, 0, 0}, /* 3 */
drh603240c2002-03-05 01:11:12 +00002366 { OP_ReadCookie, 0, 2, 0},
2367 { OP_Integer, 0, 0, 0},
2368 { OP_Lt, 0, 5, 0},
2369 { OP_AddImm, 1, 0, 0},
drh603240c2002-03-05 01:11:12 +00002370 { OP_Callback, 1, 0, 0},
drh973b6e32003-02-12 14:09:42 +00002371 { OP_Halt, 0, 0, 0},
2372 { OP_AddImm, -1, 0, 0}, /* 10 */
2373 { OP_Callback, 1, 0, 0}
drh603240c2002-03-05 01:11:12 +00002374 };
drh603240c2002-03-05 01:11:12 +00002375 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002376 int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2377 sqliteVdbeChangeP2(v, addr+3, addr+10);
drh603240c2002-03-05 01:11:12 +00002378 }else{
2379 int addr;
drhcd61c282002-03-06 22:01:34 +00002380 int size = db->cache_size;
2381 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002382 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002383 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002384 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2385 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2386 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2387 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002388 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
drh973b6e32003-02-12 14:09:42 +00002389 db->safety_level = getSafetyLevel(zRight)+1;
2390 if( db->safety_level==1 ){
drh603240c2002-03-05 01:11:12 +00002391 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002392 size = -size;
drh603240c2002-03-05 01:11:12 +00002393 }
2394 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
drh973b6e32003-02-12 14:09:42 +00002395 sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0);
2396 sqliteVdbeAddOp(v, OP_SetCookie, 0, 3);
drh603240c2002-03-05 01:11:12 +00002397 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002398 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002399 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2400 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
drhcd61c282002-03-06 22:01:34 +00002401 }
2402 }else
2403
2404 /*
2405 ** PRAGMA synchronous
drh973b6e32003-02-12 14:09:42 +00002406 ** PRAGMA synchronous=OFF|ON|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002407 **
2408 ** Return or set the local value of the synchronous flag. Changing
2409 ** the local value does not make changes to the disk file and the
2410 ** default value will be restored the next time the database is
2411 ** opened.
2412 */
2413 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2414 static VdbeOp getSync[] = {
drhcd61c282002-03-06 22:01:34 +00002415 { OP_ColumnName, 0, 0, "synchronous"},
2416 { OP_Callback, 1, 0, 0},
2417 };
drhcd61c282002-03-06 22:01:34 +00002418 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002419 sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
drhcd61c282002-03-06 22:01:34 +00002420 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2421 }else{
2422 int size = db->cache_size;
2423 if( size<0 ) size = -size;
drh973b6e32003-02-12 14:09:42 +00002424 db->safety_level = getSafetyLevel(zRight)+1;
2425 if( db->safety_level==1 ) size = -size;
drhcd61c282002-03-06 22:01:34 +00002426 db->cache_size = size;
drh001bbcb2003-03-19 03:14:00 +00002427 sqliteBtreeSetCacheSize(db->aDb[0].pBt, db->cache_size);
2428 sqliteBtreeSetSafetyLevel(db->aDb[0].pBt, db->safety_level);
drh603240c2002-03-05 01:11:12 +00002429 }
drhf57b14a2001-09-14 18:54:08 +00002430 }else
2431
danielk1977c3f9bad2002-05-15 08:30:12 +00002432 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2433 if( getBoolean(zRight) ){
2434 always_code_trigger_setup = 1;
2435 }else{
2436 always_code_trigger_setup = 0;
2437 }
2438 }else
2439
drhf57b14a2001-09-14 18:54:08 +00002440 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2441 if( getBoolean(zRight) ){
2442 db->flags |= SQLITE_VdbeTrace;
2443 }else{
2444 db->flags &= ~SQLITE_VdbeTrace;
2445 }
2446 }else
2447
drh382c0242001-10-06 16:33:02 +00002448 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2449 if( getBoolean(zRight) ){
2450 db->flags |= SQLITE_FullColNames;
2451 }else{
2452 db->flags &= ~SQLITE_FullColNames;
2453 }
2454 }else
2455
drh5080aaa2002-07-11 12:18:16 +00002456 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2457 if( getBoolean(zRight) ){
2458 db->flags |= SQLITE_ReportTypes;
2459 }else{
2460 db->flags &= ~SQLITE_ReportTypes;
2461 }
2462 }else
2463
drhc3a64ba2001-11-22 00:01:27 +00002464 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2465 if( getBoolean(zRight) ){
2466 db->flags |= SQLITE_ResultDetails;
2467 }else{
2468 db->flags &= ~SQLITE_ResultDetails;
2469 }
2470 }else
2471
drh1bee3d72001-10-15 00:44:35 +00002472 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2473 if( getBoolean(zRight) ){
2474 db->flags |= SQLITE_CountRows;
2475 }else{
2476 db->flags &= ~SQLITE_CountRows;
2477 }
2478 }else
2479
drh6a535342001-10-19 16:44:56 +00002480 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2481 if( getBoolean(zRight) ){
2482 db->flags |= SQLITE_NullCallback;
2483 }else{
2484 db->flags &= ~SQLITE_NullCallback;
2485 }
2486 }else
2487
drh382c0242001-10-06 16:33:02 +00002488 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2489 Table *pTab;
drh382c0242001-10-06 16:33:02 +00002490 pTab = sqliteFindTable(db, zRight);
drhdde85d92003-03-01 19:45:34 +00002491 if( pTab ){
drh382c0242001-10-06 16:33:02 +00002492 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002493 { OP_ColumnName, 0, 0, "cid"},
2494 { OP_ColumnName, 1, 0, "name"},
2495 { OP_ColumnName, 2, 0, "type"},
2496 { OP_ColumnName, 3, 0, "notnull"},
2497 { OP_ColumnName, 4, 0, "dflt_value"},
2498 };
2499 int i;
2500 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002501 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002502 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002503 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2504 sqliteVdbeAddOp(v, OP_String, 0, 0);
2505 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2506 sqliteVdbeAddOp(v, OP_String, 0, 0);
2507 sqliteVdbeChangeP3(v, -1,
drh3c2007a2002-10-20 16:00:27 +00002508 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00002509 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2510 sqliteVdbeAddOp(v, OP_String, 0, 0);
2511 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2512 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002513 }
2514 }
2515 }else
2516
2517 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2518 Index *pIdx;
2519 Table *pTab;
drh382c0242001-10-06 16:33:02 +00002520 pIdx = sqliteFindIndex(db, zRight);
drhdde85d92003-03-01 19:45:34 +00002521 if( pIdx ){
drh382c0242001-10-06 16:33:02 +00002522 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002523 { OP_ColumnName, 0, 0, "seqno"},
2524 { OP_ColumnName, 1, 0, "cid"},
2525 { OP_ColumnName, 2, 0, "name"},
2526 };
2527 int i;
2528 pTab = pIdx->pTable;
2529 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2530 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002531 int cnum = pIdx->aiColumn[i];
2532 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2533 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2534 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002535 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002536 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2537 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002538 }
2539 }
2540 }else
2541
drh81a20f22001-10-12 17:30:04 +00002542 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2543 Index *pIdx;
2544 Table *pTab;
drh81a20f22001-10-12 17:30:04 +00002545 pTab = sqliteFindTable(db, zRight);
2546 if( pTab ){
2547 v = sqliteGetVdbe(pParse);
2548 pIdx = pTab->pIndex;
2549 }
drhdde85d92003-03-01 19:45:34 +00002550 if( pTab && pIdx ){
drh81a20f22001-10-12 17:30:04 +00002551 int i = 0;
2552 static VdbeOp indexListPreface[] = {
drh81a20f22001-10-12 17:30:04 +00002553 { OP_ColumnName, 0, 0, "seq"},
2554 { OP_ColumnName, 1, 0, "name"},
2555 { OP_ColumnName, 2, 0, "unique"},
2556 };
2557
2558 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2559 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002560 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2561 sqliteVdbeAddOp(v, OP_String, 0, 0);
2562 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002563 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002564 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002565 ++i;
2566 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002567 }
2568 }
2569 }else
2570
drhf57b14a2001-09-14 18:54:08 +00002571#ifndef NDEBUG
2572 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2573 extern void sqliteParserTrace(FILE*, char *);
2574 if( getBoolean(zRight) ){
2575 sqliteParserTrace(stdout, "parser: ");
2576 }else{
2577 sqliteParserTrace(0, 0);
2578 }
2579 }else
2580#endif
2581
drhaaab5722002-02-19 13:39:21 +00002582 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002583 static VdbeOp checkDb[] = {
2584 { OP_SetInsert, 0, 0, "2"},
drh001bbcb2003-03-19 03:14:00 +00002585 { OP_Integer, 0, 0, 0},
2586 { OP_OpenRead, 0, 2, 0},
2587 { OP_Rewind, 0, 7, 0},
2588 { OP_Column, 0, 3, 0}, /* 4 */
drh1bffb9c2002-02-03 17:37:36 +00002589 { OP_SetInsert, 0, 0, 0},
drh001bbcb2003-03-19 03:14:00 +00002590 { OP_Next, 0, 4, 0},
2591 { OP_IntegrityCk, 0, 0, 0}, /* 7 */
drh4ff6dfa2002-03-03 23:06:00 +00002592 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002593 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002594 { OP_SetInsert, 1, 0, "2"},
drh001bbcb2003-03-19 03:14:00 +00002595 { OP_Integer, 1, 0, 0},
2596 { OP_OpenRead, 1, 2, 0},
2597 { OP_Rewind, 1, 17, 0},
2598 { OP_Column, 1, 3, 0}, /* 14 */
drh21504322002-06-25 13:16:02 +00002599 { OP_SetInsert, 1, 0, 0},
drh001bbcb2003-03-19 03:14:00 +00002600 { OP_Next, 1, 14, 0},
2601 { OP_IntegrityCk, 1, 1, 0}, /* 17 */
drh21504322002-06-25 13:16:02 +00002602 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002603 };
drh1bffb9c2002-02-03 17:37:36 +00002604 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2605 }else
drh1bffb9c2002-02-03 17:37:36 +00002606
drhf57b3392001-10-08 13:22:32 +00002607 {}
2608 sqliteFree(zLeft);
2609 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002610}