blob: f0dd8ef67a08dfadde13ed644d1ef68df6181b26 [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**
drhe0194f22003-02-26 13:52:51 +000028** $Id: build.c,v 1.130 2003/02/26 13:52:51 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);
88 db->next_cookie = db->schema_cookie;
89 }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){
214 db->schema_cookie = db->next_cookie;
215 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){
313 if( isTemp ){
314 sqliteVdbeAddOp(v, OP_OpenWrAux, 0, 2);
315 sqliteVdbeChangeP3(v, -1, TEMP_MASTER_NAME, P3_STATIC);
316 }else{
317 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
318 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
319 }
320}
321
322/*
drh75897232000-05-29 14:26:00 +0000323** Begin constructing a new table representation in memory. This is
324** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000325** to a CREATE TABLE statement. In particular, this routine is called
326** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000327** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000328** flag is true if the table should be stored in the auxiliary database
329** file instead of in the main database file. This is normally the case
330** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000331** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000332**
drhf57b3392001-10-08 13:22:32 +0000333** The new table record is initialized and put in pParse->pNewTable.
334** As more of the CREATE TABLE statement is parsed, additional action
335** routines will be called to add more information to this record.
336** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
337** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000338*/
drhe5f9c642003-01-13 23:27:31 +0000339void sqliteStartTable(
340 Parse *pParse, /* Parser context */
341 Token *pStart, /* The "CREATE" token */
342 Token *pName, /* Name of table or view to create */
343 int isTemp, /* True if this is a TEMP table */
344 int isView /* True if this is a VIEW */
345){
drh75897232000-05-29 14:26:00 +0000346 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000347 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000348 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000349 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000350 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000351
352 pParse->sFirstToken = *pStart;
353 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000354 if( zName==0 ) return;
drhe5f9c642003-01-13 23:27:31 +0000355#ifndef SQLITE_OMIT_AUTHORIZATION
356 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0) ){
drh77ad4e42003-01-14 02:49:27 +0000357 sqliteFree(zName);
drhed6c8672003-01-12 18:02:16 +0000358 return;
359 }
drhe5f9c642003-01-13 23:27:31 +0000360 {
361 int code;
362 if( isView ){
363 if( isTemp ){
364 code = SQLITE_CREATE_TEMP_VIEW;
365 }else{
366 code = SQLITE_CREATE_VIEW;
367 }
368 }else{
369 if( isTemp ){
370 code = SQLITE_CREATE_TEMP_TABLE;
371 }else{
372 code = SQLITE_CREATE_TABLE;
373 }
374 }
375 if( sqliteAuthCheck(pParse, code, zName, 0) ){
drh77ad4e42003-01-14 02:49:27 +0000376 sqliteFree(zName);
drhe5f9c642003-01-13 23:27:31 +0000377 return;
378 }
379 }
380#endif
381
drhf57b3392001-10-08 13:22:32 +0000382
383 /* Before trying to create a temporary table, make sure the Btree for
384 ** holding temporary tables is open.
385 */
386 if( isTemp && db->pBeTemp==0 ){
387 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
388 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000389 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000390 "file for storing temporary tables", 0);
391 pParse->nErr++;
392 return;
393 }
394 if( db->flags & SQLITE_InTrans ){
395 rc = sqliteBtreeBeginTrans(db->pBeTemp);
396 if( rc!=SQLITE_OK ){
397 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000398 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000399 pParse->nErr++;
400 return;
401 }
402 }
403 }
404
405 /* Make sure the new table name does not collide with an existing
406 ** index or table name. Issue an error message if it does.
407 **
408 ** If we are re-reading the sqlite_master table because of a schema
409 ** change and a new permanent table is found whose name collides with
410 ** an existing temporary table, then ignore the new permanent table.
411 ** We will continue parsing, but the pParse->nameClash flag will be set
412 ** so we will know to discard the table record once parsing has finished.
413 */
drhbe0072d2001-09-13 14:46:09 +0000414 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000415 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000416 if( pTable->isTemp && pParse->initFlag ){
417 pParse->nameClash = 1;
418 }else{
419 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
420 " already exists", 0, 0);
421 sqliteFree(zName);
422 pParse->nErr++;
423 return;
424 }
425 }else{
426 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000427 }
drhf57b3392001-10-08 13:22:32 +0000428 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
429 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000430 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
431 zName, 0);
drh75897232000-05-29 14:26:00 +0000432 sqliteFree(zName);
433 pParse->nErr++;
434 return;
435 }
436 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000437 if( pTable==0 ){
438 sqliteFree(zName);
439 return;
440 }
drh75897232000-05-29 14:26:00 +0000441 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000442 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000443 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000444 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000445 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000446 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000447 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000448 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000449
450 /* Begin generating the code that will insert the table record into
451 ** the SQLITE_MASTER table. Note in particular that we must go ahead
452 ** and allocate the record number for the table entry now. Before any
453 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
454 ** indices to be created and the table record must come before the
455 ** indices. Hence, the record number for the table must be allocated
456 ** now.
457 */
drhadbca9c2001-09-27 15:11:53 +0000458 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000459 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000460 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000461 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
462 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000463 }
drhe0bc4042002-06-25 01:09:11 +0000464 sqliteOpenMasterTable(v, isTemp);
465 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
466 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
467 sqliteVdbeAddOp(v, OP_String, 0, 0);
468 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000469 }
drh75897232000-05-29 14:26:00 +0000470}
471
472/*
473** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000474**
475** The parser calls this routine once for each column declaration
476** in a CREATE TABLE statement. sqliteStartTable() gets called
477** first to get things going. Then this routine is called for each
478** column.
drh75897232000-05-29 14:26:00 +0000479*/
480void sqliteAddColumn(Parse *pParse, Token *pName){
481 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000482 int i;
483 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000484 Column *pCol;
drh75897232000-05-29 14:26:00 +0000485 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000486 sqliteSetNString(&z, pName->z, pName->n, 0);
487 if( z==0 ) return;
488 sqliteDequote(z);
489 for(i=0; i<p->nCol; i++){
490 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
491 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
492 pParse->nErr++;
493 sqliteFree(z);
494 return;
495 }
496 }
drh75897232000-05-29 14:26:00 +0000497 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000498 Column *aNew;
499 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
500 if( aNew==0 ) return;
501 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000502 }
drhc9b84a12002-06-20 11:36:48 +0000503 pCol = &p->aCol[p->nCol];
504 memset(pCol, 0, sizeof(p->aCol[0]));
505 pCol->zName = z;
506 pCol->sortOrder = SQLITE_SO_NUM;
507 p->nCol++;
drh75897232000-05-29 14:26:00 +0000508}
509
510/*
drh382c0242001-10-06 16:33:02 +0000511** This routine is called by the parser while in the middle of
512** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
513** been seen on a column. This routine sets the notNull flag on
514** the column currently under construction.
515*/
drh9cfcf5d2002-01-29 18:41:24 +0000516void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000517 Table *p;
518 int i;
519 if( (p = pParse->pNewTable)==0 ) return;
520 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000521 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000522}
523
524/*
525** This routine is called by the parser while in the middle of
526** parsing a CREATE TABLE statement. The pFirst token is the first
527** token in the sequence of tokens that describe the type of the
528** column currently under construction. pLast is the last token
529** in the sequence. Use this information to construct a string
530** that contains the typename of the column and store that string
531** in zType.
532*/
533void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
534 Table *p;
535 int i, j;
536 int n;
537 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000538 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000539 if( (p = pParse->pNewTable)==0 ) return;
540 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000541 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000542 pCol = &p->aCol[i];
543 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000544 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000545 sqliteSetNString(pz, pFirst->z, n, 0);
546 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000547 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000548 for(i=j=0; z[i]; i++){
549 int c = z[i];
550 if( isspace(c) ) continue;
551 z[j++] = c;
552 }
553 z[j] = 0;
drh3d037a92002-08-15 01:26:09 +0000554 if( pParse->db->file_format>=4 ){
drhfcb78a42003-01-18 20:11:05 +0000555 pCol->sortOrder = sqliteCollateType(z, n);
556 }else{
557 pCol->sortOrder = SQLITE_SO_NUM;
drhc9b84a12002-06-20 11:36:48 +0000558 }
drh382c0242001-10-06 16:33:02 +0000559}
560
561/*
drh7020f652000-06-03 18:06:52 +0000562** The given token is the default value for the last column added to
563** the table currently under construction. If "minusFlag" is true, it
564** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000565**
566** This routine is called by the parser while in the middle of
567** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000568*/
569void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
570 Table *p;
571 int i;
572 char **pz;
573 if( (p = pParse->pNewTable)==0 ) return;
574 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000575 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000576 pz = &p->aCol[i].zDflt;
577 if( minusFlag ){
578 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
579 }else{
580 sqliteSetNString(pz, pVal->z, pVal->n, 0);
581 }
582 sqliteDequote(*pz);
583}
584
585/*
drh4a324312001-12-21 14:30:42 +0000586** Designate the PRIMARY KEY for the table. pList is a list of names
587** of columns that form the primary key. If pList is NULL, then the
588** most recently added column of the table is the primary key.
589**
590** A table can have at most one primary key. If the table already has
591** a primary key (and this is the second primary key) then create an
592** error.
593**
594** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
595** then we will try to use that column as the row id. (Exception:
596** For backwards compatibility with older databases, do not do this
597** if the file format version number is less than 1.) Set the Table.iPKey
598** field of the table under construction to be the index of the
599** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
600** no INTEGER PRIMARY KEY.
601**
602** If the key is not an INTEGER PRIMARY KEY, then create a unique
603** index for the key. No index is created for INTEGER PRIMARY KEYs.
604*/
drh9cfcf5d2002-01-29 18:41:24 +0000605void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000606 Table *pTab = pParse->pNewTable;
607 char *zType = 0;
608 int iCol = -1;
drhe0194f22003-02-26 13:52:51 +0000609 if( pTab==0 ) goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000610 if( pTab->hasPrimKey ){
611 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
612 "\" has more than one primary key", 0);
613 pParse->nErr++;
drhe0194f22003-02-26 13:52:51 +0000614 goto primary_key_exit;
drh4a324312001-12-21 14:30:42 +0000615 }
616 pTab->hasPrimKey = 1;
617 if( pList==0 ){
618 iCol = pTab->nCol - 1;
619 }else if( pList->nId==1 ){
620 for(iCol=0; iCol<pTab->nCol; iCol++){
621 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
622 }
623 }
624 if( iCol>=0 && iCol<pTab->nCol ){
625 zType = pTab->aCol[iCol].zType;
626 }
627 if( pParse->db->file_format>=1 &&
628 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
629 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000630 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000631 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000632 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drhe0194f22003-02-26 13:52:51 +0000633 pList = 0;
drh4a324312001-12-21 14:30:42 +0000634 }
drhe0194f22003-02-26 13:52:51 +0000635
636primary_key_exit:
637 sqliteIdListDelete(pList);
638 return;
drh4a324312001-12-21 14:30:42 +0000639}
640
641/*
drhfcb78a42003-01-18 20:11:05 +0000642** Return the appropriate collating type given a type name.
643**
644** The collation type is text (SQLITE_SO_TEXT) if the type
645** name contains the character stream "text" or "blob" or
646** "clob". Any other type name is collated as numeric
647** (SQLITE_SO_NUM).
drh8e2ca022002-06-17 17:07:19 +0000648*/
drhfcb78a42003-01-18 20:11:05 +0000649int sqliteCollateType(const char *zType, int nType){
650 int i;
drhfcb78a42003-01-18 20:11:05 +0000651 for(i=0; i<nType-1; i++){
652 switch( zType[i] ){
653 case 'b':
654 case 'B': {
655 if( i<nType-3 && sqliteStrNICmp(&zType[i],"blob",4)==0 ){
656 return SQLITE_SO_TEXT;
657 }
658 break;
659 }
660 case 'c':
661 case 'C': {
662 if( i<nType-3 && (sqliteStrNICmp(&zType[i],"char",4)==0 ||
663 sqliteStrNICmp(&zType[i],"clob",4)==0)
664 ){
665 return SQLITE_SO_TEXT;
666 }
667 break;
668 }
669 case 'x':
670 case 'X': {
671 if( i>=2 && sqliteStrNICmp(&zType[i-2],"text",4)==0 ){
672 return SQLITE_SO_TEXT;
673 }
674 break;
675 }
676 default: {
677 break;
678 }
679 }
drh8e2ca022002-06-17 17:07:19 +0000680 }
drhfcb78a42003-01-18 20:11:05 +0000681 return SQLITE_SO_NUM;
drh8e2ca022002-06-17 17:07:19 +0000682}
683
684/*
685** This routine is called by the parser while in the middle of
686** parsing a CREATE TABLE statement. A "COLLATE" clause has
687** been seen on a column. This routine sets the Column.sortOrder on
688** the column currently under construction.
689*/
690void sqliteAddCollateType(Parse *pParse, int collType){
691 Table *p;
692 int i;
693 if( (p = pParse->pNewTable)==0 ) return;
694 i = p->nCol-1;
695 if( i>=0 ) p->aCol[i].sortOrder = collType;
696}
697
698/*
drh50e5dad2001-09-15 00:57:28 +0000699** Come up with a new random value for the schema cookie. Make sure
700** the new value is different from the old.
701**
702** The schema cookie is used to determine when the schema for the
703** database changes. After each schema change, the cookie value
704** changes. When a process first reads the schema it records the
705** cookie. Thereafter, whenever it goes to access the database,
706** it checks the cookie to make sure the schema has not changed
707** since it was last read.
708**
709** This plan is not completely bullet-proof. It is possible for
710** the schema to change multiple times and for the cookie to be
711** set back to prior value. But schema changes are infrequent
712** and the probability of hitting the same cookie value is only
713** 1 chance in 2^32. So we're safe enough.
714*/
drhe0bc4042002-06-25 01:09:11 +0000715void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh50e5dad2001-09-15 00:57:28 +0000716 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000717 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000718 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000719 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
720 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000721 }
722}
723
724/*
drh969fa7c2002-02-18 18:30:32 +0000725** Measure the number of characters needed to output the given
726** identifier. The number returned includes any quotes used
727** but does not include the null terminator.
728*/
729static int identLength(const char *z){
730 int n;
drh17f71932002-02-21 12:01:27 +0000731 int needQuote = 0;
732 for(n=0; *z; n++, z++){
733 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000734 }
drh17f71932002-02-21 12:01:27 +0000735 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000736}
737
738/*
739** Write an identifier onto the end of the given string. Add
740** quote characters as needed.
741*/
742static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000743 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000744 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000745 for(j=0; zIdent[j]; j++){
746 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
747 }
748 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
749 || sqliteKeywordCode(zIdent, j)!=TK_ID;
750 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000751 for(j=0; zIdent[j]; j++){
752 z[i++] = zIdent[j];
753 if( zIdent[j]=='\'' ) z[i++] = '\'';
754 }
drh17f71932002-02-21 12:01:27 +0000755 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000756 z[i] = 0;
757 *pIdx = i;
758}
759
760/*
761** Generate a CREATE TABLE statement appropriate for the given
762** table. Memory to hold the text of the statement is obtained
763** from sqliteMalloc() and must be freed by the calling function.
764*/
765static char *createTableStmt(Table *p){
766 int i, k, n;
767 char *zStmt;
768 char *zSep, *zSep2, *zEnd;
769 n = 0;
770 for(i=0; i<p->nCol; i++){
771 n += identLength(p->aCol[i].zName);
772 }
773 n += identLength(p->zName);
774 if( n<40 ){
775 zSep = "";
776 zSep2 = ",";
777 zEnd = ")";
778 }else{
779 zSep = "\n ";
780 zSep2 = ",\n ";
781 zEnd = "\n)";
782 }
drhe0bc4042002-06-25 01:09:11 +0000783 n += 35 + 6*p->nCol;
drh8c1238a2003-01-02 14:43:55 +0000784 zStmt = sqliteMallocRaw( n );
drh969fa7c2002-02-18 18:30:32 +0000785 if( zStmt==0 ) return 0;
drhe0bc4042002-06-25 01:09:11 +0000786 strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000787 k = strlen(zStmt);
788 identPut(zStmt, &k, p->zName);
789 zStmt[k++] = '(';
790 for(i=0; i<p->nCol; i++){
791 strcpy(&zStmt[k], zSep);
792 k += strlen(&zStmt[k]);
793 zSep = zSep2;
794 identPut(zStmt, &k, p->aCol[i].zName);
795 }
796 strcpy(&zStmt[k], zEnd);
797 return zStmt;
798}
799
800/*
drh75897232000-05-29 14:26:00 +0000801** This routine is called to report the final ")" that terminates
802** a CREATE TABLE statement.
803**
drhf57b3392001-10-08 13:22:32 +0000804** The table structure that other action routines have been building
805** is added to the internal hash tables, assuming no errors have
806** occurred.
drh75897232000-05-29 14:26:00 +0000807**
drh1ccde152000-06-17 13:12:39 +0000808** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000809** unless this is a temporary table or initFlag==1. When initFlag==1,
810** it means we are reading the sqlite_master table because we just
811** connected to the database or because the sqlite_master table has
812** recently changes, so the entry for this table already exists in
813** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000814**
815** If the pSelect argument is not NULL, it means that this routine
816** was called to create a table generated from a
817** "CREATE TABLE ... AS SELECT ..." statement. The column names of
818** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000819*/
drh969fa7c2002-02-18 18:30:32 +0000820void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000821 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000822 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000823
drh969fa7c2002-02-18 18:30:32 +0000824 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000825 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000826 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000827
drh969fa7c2002-02-18 18:30:32 +0000828 /* If the table is generated from a SELECT, then construct the
829 ** list of columns and the text of the table.
830 */
831 if( pSelect ){
832 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000833 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000834 assert( p->aCol==0 );
835 p->nCol = pSelTab->nCol;
836 p->aCol = pSelTab->aCol;
837 pSelTab->nCol = 0;
838 pSelTab->aCol = 0;
839 sqliteDeleteTable(0, pSelTab);
840 }
841
drhd78eeee2001-09-13 16:18:53 +0000842 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000843 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
844 ** So do not write to the disk again. Extract the root page number
845 ** for the table from the pParse->newTnum field. (The page number
846 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000847 */
848 if( pParse->initFlag ){
849 p->tnum = pParse->newTnum;
850 }
851
drhe3c41372001-09-17 20:25:58 +0000852 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000853 ** in the SQLITE_MASTER table of the database. The record number
854 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000855 **
drhe0bc4042002-06-25 01:09:11 +0000856 ** If this is a TEMPORARY table, write the entry into the auxiliary
857 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000858 */
859 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000860 int n;
drhd8bc7082000-06-07 23:51:50 +0000861 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000862
drhd8bc7082000-06-07 23:51:50 +0000863 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000864 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000865 if( p->pSelect==0 ){
866 /* A regular table */
867 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
868 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
869 }else{
870 /* A view */
871 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
872 }
drh969fa7c2002-02-18 18:30:32 +0000873 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000874 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
875 sqliteVdbeAddOp(v, OP_String, 0, 0);
876 if( p->pSelect==0 ){
877 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
878 }else{
879 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000880 }
drhe0bc4042002-06-25 01:09:11 +0000881 sqliteVdbeAddOp(v, OP_String, 0, 0);
882 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
883 sqliteVdbeAddOp(v, OP_String, 0, 0);
884 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
885 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
886 sqliteVdbeAddOp(v, OP_String, 0, 0);
887 if( pSelect ){
888 char *z = createTableStmt(p);
889 n = z ? strlen(z) : 0;
890 sqliteVdbeChangeP3(v, -1, z, n);
891 sqliteFree(z);
892 }else{
893 assert( pEnd!=0 );
894 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
895 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
896 }
897 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
898 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
899 if( !p->isTemp ){
900 sqliteChangeCookie(db, v);
901 }
902 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000903 if( pSelect ){
904 int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
905 sqliteVdbeAddOp(v, op, 1, 0);
906 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000907 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000908 }
drh1c928532002-01-31 15:54:21 +0000909 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000910 }
drh17e9e292003-02-01 13:53:28 +0000911
912 /* Add the table to the in-memory representation of the database.
913 */
914 assert( pParse->nameClash==0 || pParse->initFlag==1 );
915 if( pParse->explain==0 && pParse->nameClash==0 && pParse->nErr==0 ){
916 Table *pOld;
917 FKey *pFKey;
918 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
919 if( pOld ){
920 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
921 return;
922 }
923 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
924 int nTo = strlen(pFKey->zTo) + 1;
925 pFKey->pNextTo = sqliteHashFind(&db->aFKey, pFKey->zTo, nTo);
926 sqliteHashInsert(&db->aFKey, pFKey->zTo, nTo, pFKey);
927 }
928 pParse->pNewTable = 0;
929 db->nTable++;
930 db->flags |= SQLITE_InternChanges;
931 }
drh75897232000-05-29 14:26:00 +0000932}
933
934/*
drha76b5df2002-02-23 02:32:10 +0000935** The parser calls this routine in order to create a new VIEW
936*/
937void sqliteCreateView(
938 Parse *pParse, /* The parsing context */
939 Token *pBegin, /* The CREATE token that begins the statement */
940 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000941 Select *pSelect, /* A SELECT statement that will become the new view */
942 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000943){
drha76b5df2002-02-23 02:32:10 +0000944 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000945 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000946 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000947 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000948
drhe5f9c642003-01-13 23:27:31 +0000949 sqliteStartTable(pParse, pBegin, pName, isTemp, 1);
drha76b5df2002-02-23 02:32:10 +0000950 p = pParse->pNewTable;
drhed6c8672003-01-12 18:02:16 +0000951 if( p==0 || pParse->nErr ){
drh417be792002-03-03 18:59:40 +0000952 sqliteSelectDelete(pSelect);
953 return;
954 }
drh174b6192002-12-03 02:22:52 +0000955
drh4b59ab52002-08-24 18:24:51 +0000956 /* Make a copy of the entire SELECT statement that defines the view.
957 ** This will force all the Expr.token.z values to be dynamically
958 ** allocated rather than point to the input string - which means that
959 ** they will persist after the current sqlite_exec() call returns.
960 */
961 p->pSelect = sqliteSelectDup(pSelect);
962 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +0000963 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +0000964 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +0000965 }
drh4b59ab52002-08-24 18:24:51 +0000966
967 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
968 ** the end.
969 */
drha76b5df2002-02-23 02:32:10 +0000970 sEnd = pParse->sLastToken;
971 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
972 sEnd.z += sEnd.n;
973 }
974 sEnd.n = 0;
975 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000976 z = pBegin->z;
977 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
978 sEnd.z = &z[n-1];
979 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +0000980
981 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
982 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +0000983 return;
drh417be792002-03-03 18:59:40 +0000984}
drha76b5df2002-02-23 02:32:10 +0000985
drh417be792002-03-03 18:59:40 +0000986/*
987** The Table structure pTable is really a VIEW. Fill in the names of
988** the columns of the view in the pTable structure. Return the number
989** of errors. If an error is seen leave an error message in pPare->zErrMsg.
990*/
991int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
992 ExprList *pEList;
993 Select *pSel;
994 Table *pSelTab;
995 int nErr = 0;
996
997 assert( pTable );
998
999 /* A positive nCol means the columns names for this view are
1000 ** already known.
1001 */
1002 if( pTable->nCol>0 ) return 0;
1003
1004 /* A negative nCol is a special marker meaning that we are currently
1005 ** trying to compute the column names. If we enter this routine with
1006 ** a negative nCol, it means two or more views form a loop, like this:
1007 **
1008 ** CREATE VIEW one AS SELECT * FROM two;
1009 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +00001010 **
1011 ** Actually, this error is caught previously and so the following test
1012 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +00001013 */
1014 if( pTable->nCol<0 ){
1015 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
1016 " is circularly defined", 0);
1017 pParse->nErr++;
1018 return 1;
1019 }
1020
1021 /* If we get this far, it means we need to compute the table names.
1022 */
1023 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
1024 pSel = pTable->pSelect;
1025
1026 /* Note that the call to sqliteResultSetOfSelect() will expand any
1027 ** "*" elements in this list. But we will need to restore the list
1028 ** back to its original configuration afterwards, so we save a copy of
1029 ** the original in pEList.
1030 */
1031 pEList = pSel->pEList;
1032 pSel->pEList = sqliteExprListDup(pEList);
1033 if( pSel->pEList==0 ){
1034 pSel->pEList = pEList;
1035 return 1; /* Malloc failed */
1036 }
1037 pTable->nCol = -1;
1038 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
1039 if( pSelTab ){
1040 assert( pTable->aCol==0 );
1041 pTable->nCol = pSelTab->nCol;
1042 pTable->aCol = pSelTab->aCol;
1043 pSelTab->nCol = 0;
1044 pSelTab->aCol = 0;
1045 sqliteDeleteTable(0, pSelTab);
1046 pParse->db->flags |= SQLITE_UnresetViews;
1047 }else{
1048 pTable->nCol = 0;
1049 nErr++;
1050 }
1051 sqliteSelectUnbind(pSel);
1052 sqliteExprListDelete(pSel->pEList);
1053 pSel->pEList = pEList;
1054 return nErr;
1055}
1056
1057/*
1058** Clear the column names from the VIEW pTable.
1059**
1060** This routine is called whenever any other table or view is modified.
1061** The view passed into this routine might depend directly or indirectly
1062** on the modified or deleted table so we need to clear the old column
1063** names so that they will be recomputed.
1064*/
1065static void sqliteViewResetColumnNames(Table *pTable){
1066 int i;
1067 if( pTable==0 || pTable->pSelect==0 ) return;
1068 if( pTable->nCol==0 ) return;
1069 for(i=0; i<pTable->nCol; i++){
1070 sqliteFree(pTable->aCol[i].zName);
1071 sqliteFree(pTable->aCol[i].zDflt);
1072 sqliteFree(pTable->aCol[i].zType);
1073 }
1074 sqliteFree(pTable->aCol);
1075 pTable->aCol = 0;
1076 pTable->nCol = 0;
1077}
1078
1079/*
1080** Clear the column names from every VIEW.
1081*/
1082void sqliteViewResetAll(sqlite *db){
1083 HashElem *i;
1084 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
1085 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
1086 Table *pTab = sqliteHashData(i);
1087 if( pTab->pSelect ){
1088 sqliteViewResetColumnNames(pTab);
1089 }
1090 }
1091 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001092}
1093
1094/*
drh75897232000-05-29 14:26:00 +00001095** Given a token, look up a table with that name. If not found, leave
1096** an error for the parser to find and return NULL.
1097*/
drhcce7d172000-05-31 15:34:51 +00001098Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001099 char *zName;
1100 Table *pTab;
1101 zName = sqliteTableNameFromToken(pTok);
1102 if( zName==0 ) return 0;
1103 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +00001104 sqliteFree(zName);
1105 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001106 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1107 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001108 pParse->nErr++;
1109 }
1110 return pTab;
1111}
1112
1113/*
1114** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001115** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001116*/
drh4ff6dfa2002-03-03 23:06:00 +00001117void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001118 Table *pTable;
drh75897232000-05-29 14:26:00 +00001119 Vdbe *v;
1120 int base;
drh5edc3122001-09-13 21:53:09 +00001121 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001122
drhdaffd0e2001-04-11 14:28:42 +00001123 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001124 pTable = sqliteTableFromToken(pParse, pName);
1125 if( pTable==0 ) return;
drhe5f9c642003-01-13 23:27:31 +00001126#ifndef SQLITE_OMIT_AUTHORIZATION
1127 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTable->isTemp),0)){
drhed6c8672003-01-12 18:02:16 +00001128 return;
1129 }
drhe5f9c642003-01-13 23:27:31 +00001130 {
1131 int code;
1132 if( isView ){
1133 if( pTable->isTemp ){
1134 code = SQLITE_DROP_TEMP_VIEW;
1135 }else{
1136 code = SQLITE_DROP_VIEW;
1137 }
1138 }else{
1139 if( pTable->isTemp ){
1140 code = SQLITE_DROP_TEMP_TABLE;
1141 }else{
1142 code = SQLITE_DROP_TABLE;
1143 }
1144 }
1145 if( sqliteAuthCheck(pParse, code, pTable->zName, 0) ){
1146 return;
1147 }
drh77ad4e42003-01-14 02:49:27 +00001148 if( sqliteAuthCheck(pParse, SQLITE_DELETE, pTable->zName, 0) ){
1149 return;
1150 }
drhe5f9c642003-01-13 23:27:31 +00001151 }
1152#endif
drh75897232000-05-29 14:26:00 +00001153 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001154 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1155 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001156 pParse->nErr++;
1157 return;
1158 }
drh4ff6dfa2002-03-03 23:06:00 +00001159 if( isView && pTable->pSelect==0 ){
1160 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1161 pTable->zName, 0);
1162 pParse->nErr++;
1163 return;
1164 }
1165 if( !isView && pTable->pSelect ){
1166 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1167 pTable->zName, 0);
1168 pParse->nErr++;
1169 return;
1170 }
drh75897232000-05-29 14:26:00 +00001171
drh1ccde152000-06-17 13:12:39 +00001172 /* Generate code to remove the table from the master table
1173 ** on disk.
1174 */
drhd8bc7082000-06-07 23:51:50 +00001175 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001176 if( v ){
1177 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001178 { OP_Rewind, 0, ADDR(8), 0},
1179 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001180 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001181 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001182 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001183 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001184 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001185 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001186 };
1187 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001188 Trigger *pTrigger;
drhcabb0812002-09-14 13:47:32 +00001189 sqliteBeginWriteOperation(pParse, 0, pTable->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001190 sqliteOpenMasterTable(v, pTable->isTemp);
danielk1977c3f9bad2002-05-15 08:30:12 +00001191 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001192 pTrigger = pTable->pTrigger;
1193 while( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001194 Token tt;
1195 tt.z = pTable->pTrigger->name;
1196 tt.n = strlen(pTable->pTrigger->name);
1197 sqliteDropTrigger(pParse, &tt, 1);
drhe0bc4042002-06-25 01:09:11 +00001198 if( pParse->explain ){
1199 pTrigger = pTrigger->pNext;
1200 }else{
1201 pTrigger = pTable->pTrigger;
1202 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001203 }
drhe0bc4042002-06-25 01:09:11 +00001204 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1205 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001206 if( !pTable->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001207 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001208 }
drhe0bc4042002-06-25 01:09:11 +00001209 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001210 if( !isView ){
1211 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1212 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1213 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1214 }
drh5e00f6c2001-09-13 13:46:56 +00001215 }
drh1c928532002-01-31 15:54:21 +00001216 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001217 }
1218
drhe0bc4042002-06-25 01:09:11 +00001219 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001220 **
1221 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001222 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001223 */
1224 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001225 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001226 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001227 }
drh417be792002-03-03 18:59:40 +00001228 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001229}
1230
1231/*
drh38640e12002-07-05 21:42:36 +00001232** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1233** opcode and adds that P3 string to the most recently inserted instruction
1234** in the virtual machine. The P3 string consists of a single character
1235** for each column in the index pIdx of table pTab. If the column uses
1236** a numeric sort order, then the P3 string character corresponding to
1237** that column is 'n'. If the column uses a text sort order, then the
1238** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1239** additional information. See also the sqliteAddKeyType() routine.
1240*/
1241void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1242 char *zType;
1243 Table *pTab;
1244 int i, n;
1245 assert( pIdx!=0 && pIdx->pTable!=0 );
1246 pTab = pIdx->pTable;
1247 n = pIdx->nColumn;
drh8c1238a2003-01-02 14:43:55 +00001248 zType = sqliteMallocRaw( n+1 );
drh38640e12002-07-05 21:42:36 +00001249 if( zType==0 ) return;
1250 for(i=0; i<n; i++){
1251 int iCol = pIdx->aiColumn[i];
1252 assert( iCol>=0 && iCol<pTab->nCol );
1253 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1254 zType[i] = 't';
1255 }else{
1256 zType[i] = 'n';
1257 }
1258 }
1259 zType[n] = 0;
1260 sqliteVdbeChangeP3(v, -1, zType, n);
1261 sqliteFree(zType);
1262}
1263
1264/*
drhc2eef3b2002-08-31 18:53:06 +00001265** This routine is called to create a new foreign key on the table
1266** currently under construction. pFromCol determines which columns
1267** in the current table point to the foreign key. If pFromCol==0 then
1268** connect the key to the last column inserted. pTo is the name of
1269** the table referred to. pToCol is a list of tables in the other
1270** pTo table that the foreign key points to. flags contains all
1271** information about the conflict resolution algorithms specified
1272** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1273**
1274** An FKey structure is created and added to the table currently
1275** under construction in the pParse->pNewTable field. The new FKey
1276** is not linked into db->aFKey at this point - that does not happen
1277** until sqliteEndTable().
1278**
1279** The foreign key is set for IMMEDIATE processing. A subsequent call
1280** to sqliteDeferForeignKey() might change this to DEFERRED.
1281*/
1282void sqliteCreateForeignKey(
1283 Parse *pParse, /* Parsing context */
1284 IdList *pFromCol, /* Columns in this table that point to other table */
1285 Token *pTo, /* Name of the other table */
1286 IdList *pToCol, /* Columns in the other table */
1287 int flags /* Conflict resolution algorithms. */
1288){
1289 Table *p = pParse->pNewTable;
1290 int nByte;
1291 int i;
1292 int nCol;
1293 char *z;
1294 FKey *pFKey = 0;
1295
1296 assert( pTo!=0 );
1297 if( p==0 || pParse->nErr ) goto fk_end;
1298 if( pFromCol==0 ){
1299 int iCol = p->nCol-1;
1300 if( iCol<0 ) goto fk_end;
1301 if( pToCol && pToCol->nId!=1 ){
1302 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1303 p->aCol[iCol].zName, -1,
1304 " should reference only one column of table ", -1,
1305 pTo->z, pTo->n, 0);
1306 pParse->nErr++;
1307 goto fk_end;
1308 }
1309 nCol = 1;
1310 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1311 sqliteSetString(&pParse->zErrMsg,
1312 "number of columns in foreign key does not match the number of "
1313 "columns in the referenced table", 0);
1314 pParse->nErr++;
1315 goto fk_end;
1316 }else{
1317 nCol = pFromCol->nId;
1318 }
1319 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1320 if( pToCol ){
1321 for(i=0; i<pToCol->nId; i++){
1322 nByte += strlen(pToCol->a[i].zName) + 1;
1323 }
1324 }
1325 pFKey = sqliteMalloc( nByte );
1326 if( pFKey==0 ) goto fk_end;
1327 pFKey->pFrom = p;
1328 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001329 z = (char*)&pFKey[1];
1330 pFKey->aCol = (struct sColMap*)z;
1331 z += sizeof(struct sColMap)*nCol;
1332 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001333 memcpy(z, pTo->z, pTo->n);
1334 z[pTo->n] = 0;
1335 z += pTo->n+1;
1336 pFKey->pNextTo = 0;
1337 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001338 if( pFromCol==0 ){
1339 pFKey->aCol[0].iFrom = p->nCol-1;
1340 }else{
1341 for(i=0; i<nCol; i++){
1342 int j;
1343 for(j=0; j<p->nCol; j++){
1344 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1345 pFKey->aCol[i].iFrom = j;
1346 break;
1347 }
1348 }
1349 if( j>=p->nCol ){
1350 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1351 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1352 pParse->nErr++;
1353 goto fk_end;
1354 }
1355 }
1356 }
1357 if( pToCol ){
1358 for(i=0; i<nCol; i++){
1359 int n = strlen(pToCol->a[i].zName);
1360 pFKey->aCol[i].zCol = z;
1361 memcpy(z, pToCol->a[i].zName, n);
1362 z[n] = 0;
1363 z += n+1;
1364 }
1365 }
1366 pFKey->isDeferred = 0;
1367 pFKey->deleteConf = flags & 0xff;
1368 pFKey->updateConf = (flags >> 8 ) & 0xff;
1369 pFKey->insertConf = (flags >> 16 ) & 0xff;
1370
1371 /* Link the foreign key to the table as the last step.
1372 */
1373 p->pFKey = pFKey;
1374 pFKey = 0;
1375
1376fk_end:
1377 sqliteFree(pFKey);
1378 sqliteIdListDelete(pFromCol);
1379 sqliteIdListDelete(pToCol);
1380}
1381
1382/*
1383** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1384** clause is seen as part of a foreign key definition. The isDeferred
1385** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1386** The behavior of the most recently created foreign key is adjusted
1387** accordingly.
1388*/
1389void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1390 Table *pTab;
1391 FKey *pFKey;
1392 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1393 pFKey->isDeferred = isDeferred;
1394}
1395
1396/*
drh75897232000-05-29 14:26:00 +00001397** Create a new index for an SQL table. pIndex is the name of the index
1398** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001399** be NULL for a primary key or an index that is created to satisfy a
1400** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001401** as the table to be indexed. pParse->pNewTable is a table that is
1402** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001403**
drh382c0242001-10-06 16:33:02 +00001404** pList is a list of columns to be indexed. pList will be NULL if this
1405** is a primary key or unique-constraint on the most recent column added
1406** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001407*/
1408void sqliteCreateIndex(
1409 Parse *pParse, /* All information about this parse */
1410 Token *pName, /* Name of the index. May be NULL */
1411 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001412 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001413 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001414 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1415 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1416){
1417 Table *pTab; /* Table to be indexed */
1418 Index *pIndex; /* The index to be created */
1419 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001420 int i, j;
drhf57b3392001-10-08 13:22:32 +00001421 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001422 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001423 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001424
drhdaffd0e2001-04-11 14:28:42 +00001425 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1426
drh75897232000-05-29 14:26:00 +00001427 /*
1428 ** Find the table that is to be indexed. Return early if not found.
1429 */
1430 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001431 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001432 pTab = sqliteTableFromToken(pParse, pTable);
1433 }else{
drhe3c41372001-09-17 20:25:58 +00001434 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001435 pTab = pParse->pNewTable;
1436 }
1437 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1438 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001439 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1440 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001441 pParse->nErr++;
1442 goto exit_create_index;
1443 }
drha76b5df2002-02-23 02:32:10 +00001444 if( pTab->pSelect ){
1445 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1446 pParse->nErr++;
1447 goto exit_create_index;
1448 }
drh75897232000-05-29 14:26:00 +00001449
drhf57b3392001-10-08 13:22:32 +00001450 /* If this index is created while re-reading the schema from sqlite_master
1451 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001452 ** only mean that the table that this index is really associated with is
1453 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001454 ** Since its table has been suppressed, we need to also suppress the
1455 ** index.
1456 */
drhe0bc4042002-06-25 01:09:11 +00001457 if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){
drhf57b3392001-10-08 13:22:32 +00001458 goto exit_create_index;
1459 }
1460
drh75897232000-05-29 14:26:00 +00001461 /*
1462 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001463 ** index or table with the same name.
1464 **
1465 ** Exception: If we are reading the names of permanent indices from the
1466 ** sqlite_master table (because some other process changed the schema) and
1467 ** one of the index names collides with the name of a temporary table or
1468 ** index, then we will continue to process this index, but we will not
1469 ** store its name in the hash table. Set the hideName flag to accomplish
1470 ** this.
1471 **
1472 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001473 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1474 ** own name.
drh75897232000-05-29 14:26:00 +00001475 */
1476 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001477 Index *pISameName; /* Another index with the same name */
1478 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001479 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001480 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001481 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1482 if( pISameName->pTable->isTemp && pParse->initFlag ){
1483 hideName = 1;
1484 }else{
1485 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1486 " already exists", 0);
1487 pParse->nErr++;
1488 goto exit_create_index;
1489 }
drhe3c41372001-09-17 20:25:58 +00001490 }
drhf57b3392001-10-08 13:22:32 +00001491 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1492 if( pTSameName->isTemp && pParse->initFlag ){
1493 hideName = 1;
1494 }else{
1495 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1496 zName, 0);
1497 pParse->nErr++;
1498 goto exit_create_index;
1499 }
drhe3c41372001-09-17 20:25:58 +00001500 }
drh75897232000-05-29 14:26:00 +00001501 }else{
drhadbca9c2001-09-27 15:11:53 +00001502 char zBuf[30];
1503 int n;
1504 Index *pLoop;
1505 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1506 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001507 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001508 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001509 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001510 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001511 }
1512
drhe5f9c642003-01-13 23:27:31 +00001513 /* Check for authorization to create an index.
1514 */
1515#ifndef SQLITE_OMIT_AUTHORIZATION
1516 if( sqliteAuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(pTab->isTemp), 0) ){
1517 goto exit_create_index;
1518 }
1519 i = SQLITE_CREATE_INDEX;
1520 if( pTab->isTemp ) i = SQLITE_CREATE_TEMP_INDEX;
1521 if( sqliteAuthCheck(pParse, i, zName, pTab->zName) ){
1522 goto exit_create_index;
1523 }
1524#endif
1525
drh75897232000-05-29 14:26:00 +00001526 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001527 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001528 ** So create a fake list to simulate this.
1529 */
1530 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001531 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001532 nullId.n = strlen(nullId.z);
1533 pList = sqliteIdListAppend(0, &nullId);
1534 if( pList==0 ) goto exit_create_index;
1535 }
1536
1537 /*
1538 ** Allocate the index structure.
1539 */
drhdcc581c2000-05-30 13:44:19 +00001540 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001541 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001542 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001543 pIndex->aiColumn = (int*)&pIndex[1];
1544 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001545 strcpy(pIndex->zName, zName);
1546 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001547 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001548 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001549 pIndex->autoIndex = pName==0;
drh75897232000-05-29 14:26:00 +00001550
drh1ccde152000-06-17 13:12:39 +00001551 /* Scan the names of the columns of the table to be indexed and
1552 ** load the column indices into the Index structure. Report an error
1553 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001554 */
1555 for(i=0; i<pList->nId; i++){
1556 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001557 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001558 }
1559 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001560 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001561 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001562 pParse->nErr++;
1563 sqliteFree(pIndex);
1564 goto exit_create_index;
1565 }
drh967e8b72000-06-21 13:59:10 +00001566 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001567 }
1568
1569 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001570 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001571 */
drhf57b3392001-10-08 13:22:32 +00001572 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001573 Index *p;
1574 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1575 if( p ){
1576 assert( p==pIndex ); /* Malloc must have failed */
1577 sqliteFree(pIndex);
1578 goto exit_create_index;
1579 }
drh5e00f6c2001-09-13 13:46:56 +00001580 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001581 }
drh9cfcf5d2002-01-29 18:41:24 +00001582
1583 /* When adding an index to the list of indices for a table, make
1584 ** sure all indices labeled OE_Replace come after all those labeled
1585 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1586 ** and INSERT.
1587 */
1588 if( onError!=OE_Replace || pTab->pIndex==0
1589 || pTab->pIndex->onError==OE_Replace){
1590 pIndex->pNext = pTab->pIndex;
1591 pTab->pIndex = pIndex;
1592 }else{
1593 Index *pOther = pTab->pIndex;
1594 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1595 pOther = pOther->pNext;
1596 }
1597 pIndex->pNext = pOther->pNext;
1598 pOther->pNext = pIndex;
1599 }
drh75897232000-05-29 14:26:00 +00001600
drhd78eeee2001-09-13 16:18:53 +00001601 /* If the initFlag is 1 it means we are reading the SQL off the
1602 ** "sqlite_master" table on the disk. So do not write to the disk
1603 ** again. Extract the table number from the pParse->newTnum field.
1604 */
drhadbca9c2001-09-27 15:11:53 +00001605 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001606 pIndex->tnum = pParse->newTnum;
1607 }
1608
drh75897232000-05-29 14:26:00 +00001609 /* If the initFlag is 0 then create the index on disk. This
1610 ** involves writing the index into the master table and filling in the
1611 ** index with the current table contents.
1612 **
1613 ** The initFlag is 0 when the user first enters a CREATE INDEX
1614 ** command. The initFlag is 1 when a database is opened and
1615 ** CREATE INDEX statements are read out of the master table. In
1616 ** the latter case the index already exists on disk, which is why
1617 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001618 **
1619 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001620 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1621 ** has just been created, it contains no data and the index initialization
1622 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001623 */
drhadbca9c2001-09-27 15:11:53 +00001624 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001625 int n;
drhadbca9c2001-09-27 15:11:53 +00001626 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001627 int lbl1, lbl2;
1628 int i;
drhadbca9c2001-09-27 15:11:53 +00001629 int addr;
drhf57b3392001-10-08 13:22:32 +00001630 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001631
drhd8bc7082000-06-07 23:51:50 +00001632 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001633 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001634 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001635 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001636 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001637 }
drhe0bc4042002-06-25 01:09:11 +00001638 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1639 sqliteVdbeAddOp(v, OP_String, 0, 0);
1640 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1641 sqliteVdbeAddOp(v, OP_String, 0, 0);
1642 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1643 sqliteVdbeAddOp(v, OP_String, 0, 0);
1644 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001645 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1646 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001647 pIndex->tnum = 0;
1648 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001649 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001650 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001651 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001652 }else{
drh99fcd712001-10-13 01:06:47 +00001653 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001654 }
drh5e00f6c2001-09-13 13:46:56 +00001655 }
drhe0bc4042002-06-25 01:09:11 +00001656 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1657 if( pStart && pEnd ){
1658 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1659 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001660 }
drhe0bc4042002-06-25 01:09:11 +00001661 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1662 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001663 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001664 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1665 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001666 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001667 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1668 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001669 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001670 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001671 }
drh99fcd712001-10-13 01:06:47 +00001672 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001673 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001674 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh483750b2003-01-29 18:46:51 +00001675 sqliteVdbeChangeP3(v, -1, "indexed columns are not unique", P3_STATIC);
drh6b563442001-11-07 16:48:26 +00001676 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001677 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001678 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1679 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001680 }
drhadbca9c2001-09-27 15:11:53 +00001681 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001682 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001683 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001684 }
drhe0bc4042002-06-25 01:09:11 +00001685 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001686 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001687 }
drh75897232000-05-29 14:26:00 +00001688 }
1689
drh75897232000-05-29 14:26:00 +00001690 /* Clean up before exiting */
1691exit_create_index:
1692 sqliteIdListDelete(pList);
1693 sqliteFree(zName);
1694 return;
1695}
1696
1697/*
drh74e24cd2002-01-09 03:19:59 +00001698** This routine will drop an existing named index. This routine
1699** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001700*/
1701void sqliteDropIndex(Parse *pParse, Token *pName){
1702 Index *pIndex;
1703 char *zName;
1704 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001705 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001706
drhdaffd0e2001-04-11 14:28:42 +00001707 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001708 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001709 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001710 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001711 sqliteFree(zName);
1712 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001713 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1714 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001715 pParse->nErr++;
1716 return;
1717 }
drh485b39b2002-07-13 03:11:52 +00001718 if( pIndex->autoIndex ){
1719 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1720 "or PRIMARY KEY constraint cannot be dropped", 0);
1721 pParse->nErr++;
1722 return;
1723 }
drhe5f9c642003-01-13 23:27:31 +00001724#ifndef SQLITE_OMIT_AUTHORIZATION
1725 {
1726 int code = SQLITE_DROP_INDEX;
1727 Table *pTab = pIndex->pTable;
1728 if( sqliteAuthCheck(pParse, SQLITE_DELETE, SCHEMA_TABLE(pTab->isTemp), 0) ){
1729 return;
1730 }
1731 if( pTab->isTemp ) code = SQLITE_DROP_TEMP_INDEX;
drh77ad4e42003-01-14 02:49:27 +00001732 if( sqliteAuthCheck(pParse, code, pIndex->zName, pTab->zName) ){
drhe5f9c642003-01-13 23:27:31 +00001733 return;
1734 }
drhed6c8672003-01-12 18:02:16 +00001735 }
drhe5f9c642003-01-13 23:27:31 +00001736#endif
drh75897232000-05-29 14:26:00 +00001737
1738 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001739 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001740 if( v ){
1741 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001742 { OP_Rewind, 0, ADDR(9), 0},
1743 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001744 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001745 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001746 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001747 { OP_Eq, 0, ADDR(8), 0},
1748 { OP_Next, 0, ADDR(3), 0},
1749 { OP_Goto, 0, ADDR(9), 0},
1750 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001751 };
1752 int base;
drhf57b3392001-10-08 13:22:32 +00001753 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001754
drhcabb0812002-09-14 13:47:32 +00001755 sqliteBeginWriteOperation(pParse, 0, pTab->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001756 sqliteOpenMasterTable(v, pTab->isTemp);
1757 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1758 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001759 if( !pTab->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001760 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001761 }
drhe0bc4042002-06-25 01:09:11 +00001762 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001763 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001764 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001765 }
1766
drhe0bc4042002-06-25 01:09:11 +00001767 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001768 */
1769 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001770 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001771 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001772 }
1773}
1774
1775/*
drh75897232000-05-29 14:26:00 +00001776** Append a new element to the given IdList. Create a new IdList if
1777** need be.
drhdaffd0e2001-04-11 14:28:42 +00001778**
1779** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001780*/
1781IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1782 if( pList==0 ){
1783 pList = sqliteMalloc( sizeof(IdList) );
1784 if( pList==0 ) return 0;
1785 }
1786 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001787 struct IdList_item *a;
1788 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1789 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001790 sqliteIdListDelete(pList);
1791 return 0;
drh75897232000-05-29 14:26:00 +00001792 }
drh6d4abfb2001-10-22 02:58:08 +00001793 pList->a = a;
drh75897232000-05-29 14:26:00 +00001794 }
1795 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1796 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001797 char **pz = &pList->a[pList->nId].zName;
1798 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1799 if( *pz==0 ){
1800 sqliteIdListDelete(pList);
1801 return 0;
1802 }else{
1803 sqliteDequote(*pz);
1804 }
drh75897232000-05-29 14:26:00 +00001805 }
1806 pList->nId++;
1807 return pList;
1808}
1809
1810/*
drhad3cab52002-05-24 02:04:32 +00001811** Append a new table name to the given SrcList. Create a new SrcList if
1812** need be. A new entry is created in the SrcList even if pToken is NULL.
1813**
1814** A new SrcList is returned, or NULL if malloc() fails.
1815*/
1816SrcList *sqliteSrcListAppend(SrcList *pList, Token *pToken){
1817 if( pList==0 ){
1818 pList = sqliteMalloc( sizeof(IdList) );
1819 if( pList==0 ) return 0;
1820 }
1821 if( (pList->nSrc & 7)==0 ){
1822 struct SrcList_item *a;
1823 a = sqliteRealloc(pList->a, (pList->nSrc+8)*sizeof(pList->a[0]) );
1824 if( a==0 ){
1825 sqliteSrcListDelete(pList);
1826 return 0;
1827 }
1828 pList->a = a;
1829 }
1830 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
1831 if( pToken ){
1832 char **pz = &pList->a[pList->nSrc].zName;
1833 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1834 if( *pz==0 ){
1835 sqliteSrcListDelete(pList);
1836 return 0;
1837 }else{
1838 sqliteDequote(*pz);
1839 }
1840 }
1841 pList->nSrc++;
1842 return pList;
1843}
1844
1845/*
drh75897232000-05-29 14:26:00 +00001846** Add an alias to the last identifier on the given identifier list.
1847*/
drhad3cab52002-05-24 02:04:32 +00001848void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1849 if( pList && pList->nSrc>0 ){
1850 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001851 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001852 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001853 }
1854}
1855
1856/*
drhad3cab52002-05-24 02:04:32 +00001857** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001858*/
1859void sqliteIdListDelete(IdList *pList){
1860 int i;
1861 if( pList==0 ) return;
1862 for(i=0; i<pList->nId; i++){
1863 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001864 }
1865 sqliteFree(pList->a);
1866 sqliteFree(pList);
1867}
1868
1869/*
drhad2d8302002-05-24 20:31:36 +00001870** Return the index in pList of the identifier named zId. Return -1
1871** if not found.
1872*/
1873int sqliteIdListIndex(IdList *pList, const char *zName){
1874 int i;
1875 if( pList==0 ) return -1;
1876 for(i=0; i<pList->nId; i++){
1877 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1878 }
1879 return -1;
1880}
1881
1882/*
drhad3cab52002-05-24 02:04:32 +00001883** Delete an entire SrcList including all its substructure.
1884*/
1885void sqliteSrcListDelete(SrcList *pList){
1886 int i;
1887 if( pList==0 ) return;
1888 for(i=0; i<pList->nSrc; i++){
1889 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001890 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001891 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001892 sqliteDeleteTable(0, pList->a[i].pTab);
1893 }
drhff78bd22002-02-27 01:47:11 +00001894 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001895 sqliteExprDelete(pList->a[i].pOn);
1896 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001897 }
1898 sqliteFree(pList->a);
1899 sqliteFree(pList);
1900}
1901
drh982cef72000-05-30 16:27:03 +00001902/*
1903** The COPY command is for compatibility with PostgreSQL and specificially
1904** for the ability to read the output of pg_dump. The format is as
1905** follows:
1906**
1907** COPY table FROM file [USING DELIMITERS string]
1908**
1909** "table" is an existing table name. We will read lines of code from
1910** file to fill this table with data. File might be "stdin". The optional
1911** delimiter string identifies the field separators. The default is a tab.
1912*/
1913void sqliteCopy(
1914 Parse *pParse, /* The parser context */
1915 Token *pTableName, /* The name of the table into which we will insert */
1916 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001917 Token *pDelimiter, /* Use this as the field delimiter */
1918 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001919){
1920 Table *pTab;
1921 char *zTab;
drh1c928532002-01-31 15:54:21 +00001922 int i;
drh982cef72000-05-30 16:27:03 +00001923 Vdbe *v;
1924 int addr, end;
1925 Index *pIdx;
drh77ad4e42003-01-14 02:49:27 +00001926 char *zFile = 0;
drhbe0072d2001-09-13 14:46:09 +00001927 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001928
drh77ad4e42003-01-14 02:49:27 +00001929
drh982cef72000-05-30 16:27:03 +00001930 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001931 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001932 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001933 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001934 if( pTab==0 ) goto copy_cleanup;
drh77ad4e42003-01-14 02:49:27 +00001935 zFile = sqliteStrNDup(pFilename->z, pFilename->n);
1936 sqliteDequote(zFile);
1937 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, zFile)
1938 || sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile) ){
drhed6c8672003-01-12 18:02:16 +00001939 goto copy_cleanup;
1940 }
drhd8bc7082000-06-07 23:51:50 +00001941 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001942 if( v ){
drhf57b3392001-10-08 13:22:32 +00001943 int openOp;
drhcabb0812002-09-14 13:47:32 +00001944 sqliteBeginWriteOperation(pParse, 1, pTab->isTemp);
drh99fcd712001-10-13 01:06:47 +00001945 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001946 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001947 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001948 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001949 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1950 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001951 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001952 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1953 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001954 }
drhb419a922002-01-30 16:17:23 +00001955 if( db->flags & SQLITE_CountRows ){
1956 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1957 }
drh982cef72000-05-30 16:27:03 +00001958 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001959 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001960 if( pDelimiter ){
1961 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1962 sqliteVdbeDequoteP3(v, addr);
1963 }else{
1964 sqliteVdbeChangeP3(v, addr, "\t", 1);
1965 }
drh8aff1012001-12-22 14:49:24 +00001966 if( pTab->iPKey>=0 ){
1967 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1968 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1969 }else{
1970 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1971 }
drh982cef72000-05-30 16:27:03 +00001972 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001973 if( i==pTab->iPKey ){
1974 /* The integer primary key column is filled with NULL since its
1975 ** value is always pulled from the record number */
1976 sqliteVdbeAddOp(v, OP_String, 0, 0);
1977 }else{
1978 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1979 }
drh982cef72000-05-30 16:27:03 +00001980 }
drhb419a922002-01-30 16:17:23 +00001981 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1982 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1983 if( (db->flags & SQLITE_CountRows)!=0 ){
1984 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001985 }
drh99fcd712001-10-13 01:06:47 +00001986 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1987 sqliteVdbeResolveLabel(v, end);
1988 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001989 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001990 if( db->flags & SQLITE_CountRows ){
drhb419a922002-01-30 16:17:23 +00001991 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1992 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1993 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1994 }
drh982cef72000-05-30 16:27:03 +00001995 }
1996
1997copy_cleanup:
drh77ad4e42003-01-14 02:49:27 +00001998 sqliteFree(zFile);
drh982cef72000-05-30 16:27:03 +00001999 return;
2000}
drhdce2cbe2000-05-31 02:27:49 +00002001
2002/*
2003** The non-standard VACUUM command is used to clean up the database,
2004** collapse free space, etc. It is modelled after the VACUUM command
2005** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00002006**
drh1bffb9c2002-02-03 17:37:36 +00002007** In version 1.0.x of SQLite, the VACUUM command would call
2008** gdbm_reorganize() on all the database tables. But beginning
2009** with 2.0.0, SQLite no longer uses GDBM so this command has
2010** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00002011*/
2012void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00002013 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00002014}
drhc4a3c772001-04-04 11:48:57 +00002015
2016/*
2017** Begin a transaction
2018*/
drh1c928532002-01-31 15:54:21 +00002019void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00002020 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002021
drhc4a3c772001-04-04 11:48:57 +00002022 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002023 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002024 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002025 if( db->flags & SQLITE_InTrans ){
2026 pParse->nErr++;
2027 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
2028 "within a transaction", 0);
2029 return;
2030 }
drhcabb0812002-09-14 13:47:32 +00002031 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00002032 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002033 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00002034}
2035
2036/*
2037** Commit a transaction
2038*/
2039void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002040 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002041
drhc4a3c772001-04-04 11:48:57 +00002042 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002043 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002044 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002045 if( (db->flags & SQLITE_InTrans)==0 ){
2046 pParse->nErr++;
2047 sqliteSetString(&pParse->zErrMsg,
2048 "cannot commit - no transaction is active", 0);
2049 return;
2050 }
drh5e00f6c2001-09-13 13:46:56 +00002051 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002052 sqliteEndWriteOperation(pParse);
2053 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002054}
2055
2056/*
2057** Rollback a transaction
2058*/
2059void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00002060 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00002061 Vdbe *v;
2062
drhc4a3c772001-04-04 11:48:57 +00002063 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00002064 if( pParse->nErr || sqlite_malloc_failed ) return;
drhe5f9c642003-01-13 23:27:31 +00002065 if( sqliteAuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0) ) return;
drh6b8b8742002-08-18 20:28:06 +00002066 if( (db->flags & SQLITE_InTrans)==0 ){
2067 pParse->nErr++;
2068 sqliteSetString(&pParse->zErrMsg,
2069 "cannot rollback - no transaction is active", 0);
2070 return;
2071 }
drh5e00f6c2001-09-13 13:46:56 +00002072 v = sqliteGetVdbe(pParse);
2073 if( v ){
drh99fcd712001-10-13 01:06:47 +00002074 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00002075 }
drh5e00f6c2001-09-13 13:46:56 +00002076 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00002077 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00002078}
drhf57b14a2001-09-14 18:54:08 +00002079
2080/*
drh1c928532002-01-31 15:54:21 +00002081** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00002082** might change the database.
2083**
2084** This routine starts a new transaction if we are not already within
2085** a transaction. If we are already within a transaction, then a checkpoint
2086** is set if the setCheckpoint parameter is true. A checkpoint should
2087** be set for operations that might fail (due to a constraint) part of
2088** the way through and which will need to undo some writes without having to
2089** rollback the whole transaction. For operations where all constraints
2090** can be checked before any changes are made to the database, it is never
2091** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00002092**
2093** The tempOnly flag indicates that only temporary tables will be changed
2094** during this write operation. The primary database table is not
2095** write-locked. Only the temporary database file gets a write lock.
2096** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00002097*/
drhcabb0812002-09-14 13:47:32 +00002098void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00002099 Vdbe *v;
2100 v = sqliteGetVdbe(pParse);
2101 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00002102 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00002103 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drhcabb0812002-09-14 13:47:32 +00002104 sqliteVdbeAddOp(v, OP_Transaction, tempOnly, 0);
2105 if( !tempOnly ){
2106 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
2107 pParse->schemaVerified = 1;
2108 }
drhc977f7f2002-05-21 11:38:11 +00002109 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00002110 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
2111 }
2112}
2113
2114/*
drh1c928532002-01-31 15:54:21 +00002115** Generate code that concludes an operation that may have changed
2116** the database. This is a companion function to BeginWriteOperation().
2117** If a transaction was started, then commit it. If a checkpoint was
2118** started then commit that.
2119*/
2120void sqliteEndWriteOperation(Parse *pParse){
2121 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002122 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002123 v = sqliteGetVdbe(pParse);
2124 if( v==0 ) return;
2125 if( pParse->db->flags & SQLITE_InTrans ){
2126 /* Do Nothing */
2127 }else{
2128 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2129 }
2130}
2131
2132
2133/*
drhf57b14a2001-09-14 18:54:08 +00002134** Interpret the given string as a boolean value.
2135*/
2136static int getBoolean(char *z){
2137 static char *azTrue[] = { "yes", "on", "true" };
2138 int i;
2139 if( z[0]==0 ) return 0;
2140 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2141 return atoi(z);
2142 }
2143 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
2144 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
2145 }
2146 return 0;
2147}
2148
2149/*
drh973b6e32003-02-12 14:09:42 +00002150** Interpret the given string as a safety level. Return 0 for OFF,
2151** 1 for ON or NORMAL and 2 for FULL.
2152**
2153** Note that the values returned are one less that the values that
2154** should be passed into sqliteBtreeSetSafetyLevel(). The is done
2155** to support legacy SQL code. The safety level used to be boolean
2156** and older scripts may have used numbers 0 for OFF and 1 for ON.
2157*/
2158static int getSafetyLevel(char *z){
2159 static const struct {
2160 const char *zWord;
2161 int val;
2162 } aKey[] = {
2163 { "no", 0 },
2164 { "off", 0 },
2165 { "false", 0 },
2166 { "yes", 1 },
2167 { "on", 1 },
2168 { "true", 1 },
2169 { "full", 2 },
2170 };
2171 int i;
2172 if( z[0]==0 ) return 1;
2173 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2174 return atoi(z);
2175 }
2176 for(i=0; i<sizeof(aKey)/sizeof(aKey[0]); i++){
2177 if( sqliteStrICmp(z,aKey[i].zWord)==0 ) return aKey[i].val;
2178 }
2179 return 1;
2180}
2181
2182/*
drhf57b14a2001-09-14 18:54:08 +00002183** Process a pragma statement.
2184**
2185** Pragmas are of this form:
2186**
2187** PRAGMA id = value
2188**
2189** The identifier might also be a string. The value is a string, and
2190** identifier, or a number. If minusFlag is true, then the value is
2191** a number that was preceded by a minus sign.
2192*/
2193void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
2194 char *zLeft = 0;
2195 char *zRight = 0;
2196 sqlite *db = pParse->db;
2197
2198 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
2199 sqliteDequote(zLeft);
2200 if( minusFlag ){
2201 zRight = 0;
2202 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
2203 }else{
2204 zRight = sqliteStrNDup(pRight->z, pRight->n);
2205 sqliteDequote(zRight);
2206 }
drhbf0c78a2003-01-14 02:54:08 +00002207 if( sqliteAuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight) ){
2208 sqliteFree(zLeft);
2209 sqliteFree(zRight);
2210 return;
2211 }
drhf57b14a2001-09-14 18:54:08 +00002212
drhcd61c282002-03-06 22:01:34 +00002213 /*
2214 ** PRAGMA default_cache_size
2215 ** PRAGMA default_cache_size=N
2216 **
2217 ** The first form reports the current persistent setting for the
2218 ** page cache size. The value returned is the maximum number of
2219 ** pages in the page cache. The second form sets both the current
2220 ** page cache size value and the persistent page cache size value
2221 ** stored in the database file.
2222 **
2223 ** The default cache size is stored in meta-value 2 of page 1 of the
2224 ** database file. The cache size is actually the absolute value of
2225 ** this memory location. The sign of meta-value 2 determines the
2226 ** synchronous setting. A negative value means synchronous is off
2227 ** and a positive value means synchronous is on.
2228 */
2229 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00002230 static VdbeOp getCacheSize[] = {
2231 { OP_ReadCookie, 0, 2, 0},
2232 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00002233 { OP_Dup, 0, 0, 0},
2234 { OP_Integer, 0, 0, 0},
2235 { OP_Ne, 0, 6, 0},
2236 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00002237 { OP_ColumnName, 0, 0, "cache_size"},
2238 { OP_Callback, 1, 0, 0},
2239 };
2240 Vdbe *v = sqliteGetVdbe(pParse);
2241 if( v==0 ) return;
2242 if( pRight->z==pLeft->z ){
2243 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2244 }else{
2245 int addr;
2246 int size = atoi(zRight);
2247 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002248 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002249 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2250 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2251 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2252 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2253 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2254 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2255 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002256 db->cache_size = db->cache_size<0 ? -size : size;
2257 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002258 }
2259 }else
2260
drhcd61c282002-03-06 22:01:34 +00002261 /*
2262 ** PRAGMA cache_size
2263 ** PRAGMA cache_size=N
2264 **
2265 ** The first form reports the current local setting for the
2266 ** page cache size. The local setting can be different from
2267 ** the persistent cache size value that is stored in the database
2268 ** file itself. The value returned is the maximum number of
2269 ** pages in the page cache. The second form sets the local
2270 ** page cache size value. It does not change the persistent
2271 ** cache size stored on the disk so the cache size will revert
2272 ** to its default value when the database is closed and reopened.
2273 ** N should be a positive integer.
2274 */
2275 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2276 static VdbeOp getCacheSize[] = {
drhcd61c282002-03-06 22:01:34 +00002277 { OP_ColumnName, 0, 0, "cache_size"},
2278 { OP_Callback, 1, 0, 0},
2279 };
2280 Vdbe *v = sqliteGetVdbe(pParse);
2281 if( v==0 ) return;
2282 if( pRight->z==pLeft->z ){
2283 int size = db->cache_size;;
2284 if( size<0 ) size = -size;
2285 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2286 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2287 }else{
2288 int size = atoi(zRight);
2289 if( size<0 ) size = -size;
2290 if( db->cache_size<0 ) size = -size;
2291 db->cache_size = size;
2292 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2293 }
2294 }else
2295
2296 /*
2297 ** PRAGMA default_synchronous
drh973b6e32003-02-12 14:09:42 +00002298 ** PRAGMA default_synchronous=ON|OFF|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002299 **
2300 ** The first form returns the persistent value of the "synchronous" setting
2301 ** that is stored in the database. This is the synchronous setting that
2302 ** is used whenever the database is opened unless overridden by a separate
2303 ** "synchronous" pragma. The second form changes the persistent and the
2304 ** local synchronous setting to the value given.
2305 **
drh973b6e32003-02-12 14:09:42 +00002306 ** If synchronous is OFF, SQLite does not attempt any fsync() systems calls
2307 ** to make sure data is committed to disk. Write operations are very fast,
2308 ** but a power failure can leave the database in an inconsistent state.
2309 ** If synchronous is ON or NORMAL, SQLite will do an fsync() system call to
2310 ** make sure data is being written to disk. The risk of corruption due to
2311 ** a power loss in this mode is negligible but non-zero. If synchronous
2312 ** is FULL, extra fsync()s occur to reduce the risk of corruption to near
2313 ** zero, but with a write performance penalty. The default mode is NORMAL.
drhcd61c282002-03-06 22:01:34 +00002314 */
2315 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00002316 static VdbeOp getSync[] = {
drh973b6e32003-02-12 14:09:42 +00002317 { OP_ColumnName, 0, 0, "synchronous"},
2318 { OP_ReadCookie, 0, 3, 0},
2319 { OP_Dup, 0, 0, 0},
2320 { OP_If, 0, 0, 0}, /* 3 */
drh603240c2002-03-05 01:11:12 +00002321 { OP_ReadCookie, 0, 2, 0},
2322 { OP_Integer, 0, 0, 0},
2323 { OP_Lt, 0, 5, 0},
2324 { OP_AddImm, 1, 0, 0},
drh603240c2002-03-05 01:11:12 +00002325 { OP_Callback, 1, 0, 0},
drh973b6e32003-02-12 14:09:42 +00002326 { OP_Halt, 0, 0, 0},
2327 { OP_AddImm, -1, 0, 0}, /* 10 */
2328 { OP_Callback, 1, 0, 0}
drh603240c2002-03-05 01:11:12 +00002329 };
2330 Vdbe *v = sqliteGetVdbe(pParse);
2331 if( v==0 ) return;
2332 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002333 int addr = sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2334 sqliteVdbeChangeP2(v, addr+3, addr+10);
drh603240c2002-03-05 01:11:12 +00002335 }else{
2336 int addr;
drhcd61c282002-03-06 22:01:34 +00002337 int size = db->cache_size;
2338 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002339 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002340 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002341 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2342 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2343 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2344 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002345 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
drh973b6e32003-02-12 14:09:42 +00002346 db->safety_level = getSafetyLevel(zRight)+1;
2347 if( db->safety_level==1 ){
drh603240c2002-03-05 01:11:12 +00002348 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002349 size = -size;
drh603240c2002-03-05 01:11:12 +00002350 }
2351 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
drh973b6e32003-02-12 14:09:42 +00002352 sqliteVdbeAddOp(v, OP_Integer, db->safety_level, 0);
2353 sqliteVdbeAddOp(v, OP_SetCookie, 0, 3);
drh603240c2002-03-05 01:11:12 +00002354 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002355 db->cache_size = size;
2356 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh973b6e32003-02-12 14:09:42 +00002357 sqliteBtreeSetSafetyLevel(db->pBe, db->safety_level);
drhcd61c282002-03-06 22:01:34 +00002358 }
2359 }else
2360
2361 /*
2362 ** PRAGMA synchronous
drh973b6e32003-02-12 14:09:42 +00002363 ** PRAGMA synchronous=OFF|ON|NORMAL|FULL
drhcd61c282002-03-06 22:01:34 +00002364 **
2365 ** Return or set the local value of the synchronous flag. Changing
2366 ** the local value does not make changes to the disk file and the
2367 ** default value will be restored the next time the database is
2368 ** opened.
2369 */
2370 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2371 static VdbeOp getSync[] = {
drhcd61c282002-03-06 22:01:34 +00002372 { OP_ColumnName, 0, 0, "synchronous"},
2373 { OP_Callback, 1, 0, 0},
2374 };
2375 Vdbe *v = sqliteGetVdbe(pParse);
2376 if( v==0 ) return;
2377 if( pRight->z==pLeft->z ){
drh973b6e32003-02-12 14:09:42 +00002378 sqliteVdbeAddOp(v, OP_Integer, db->safety_level-1, 0);
drhcd61c282002-03-06 22:01:34 +00002379 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2380 }else{
2381 int size = db->cache_size;
2382 if( size<0 ) size = -size;
drh973b6e32003-02-12 14:09:42 +00002383 db->safety_level = getSafetyLevel(zRight)+1;
2384 if( db->safety_level==1 ) size = -size;
drhcd61c282002-03-06 22:01:34 +00002385 db->cache_size = size;
2386 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh973b6e32003-02-12 14:09:42 +00002387 sqliteBtreeSetSafetyLevel(db->pBe, db->safety_level);
drh603240c2002-03-05 01:11:12 +00002388 }
drhf57b14a2001-09-14 18:54:08 +00002389 }else
2390
danielk1977c3f9bad2002-05-15 08:30:12 +00002391 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2392 if( getBoolean(zRight) ){
2393 always_code_trigger_setup = 1;
2394 }else{
2395 always_code_trigger_setup = 0;
2396 }
2397 }else
2398
drhf57b14a2001-09-14 18:54:08 +00002399 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2400 if( getBoolean(zRight) ){
2401 db->flags |= SQLITE_VdbeTrace;
2402 }else{
2403 db->flags &= ~SQLITE_VdbeTrace;
2404 }
2405 }else
2406
drh382c0242001-10-06 16:33:02 +00002407 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2408 if( getBoolean(zRight) ){
2409 db->flags |= SQLITE_FullColNames;
2410 }else{
2411 db->flags &= ~SQLITE_FullColNames;
2412 }
2413 }else
2414
drh5080aaa2002-07-11 12:18:16 +00002415 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2416 if( getBoolean(zRight) ){
2417 db->flags |= SQLITE_ReportTypes;
2418 }else{
2419 db->flags &= ~SQLITE_ReportTypes;
2420 }
2421 }else
2422
drhc3a64ba2001-11-22 00:01:27 +00002423 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2424 if( getBoolean(zRight) ){
2425 db->flags |= SQLITE_ResultDetails;
2426 }else{
2427 db->flags &= ~SQLITE_ResultDetails;
2428 }
2429 }else
2430
drh1bee3d72001-10-15 00:44:35 +00002431 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2432 if( getBoolean(zRight) ){
2433 db->flags |= SQLITE_CountRows;
2434 }else{
2435 db->flags &= ~SQLITE_CountRows;
2436 }
2437 }else
2438
drh6a535342001-10-19 16:44:56 +00002439 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2440 if( getBoolean(zRight) ){
2441 db->flags |= SQLITE_NullCallback;
2442 }else{
2443 db->flags &= ~SQLITE_NullCallback;
2444 }
2445 }else
2446
drh382c0242001-10-06 16:33:02 +00002447 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2448 Table *pTab;
2449 Vdbe *v;
2450 pTab = sqliteFindTable(db, zRight);
2451 if( pTab ) v = sqliteGetVdbe(pParse);
2452 if( pTab && v ){
2453 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002454 { OP_ColumnName, 0, 0, "cid"},
2455 { OP_ColumnName, 1, 0, "name"},
2456 { OP_ColumnName, 2, 0, "type"},
2457 { OP_ColumnName, 3, 0, "notnull"},
2458 { OP_ColumnName, 4, 0, "dflt_value"},
2459 };
2460 int i;
2461 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002462 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002463 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002464 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2465 sqliteVdbeAddOp(v, OP_String, 0, 0);
2466 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2467 sqliteVdbeAddOp(v, OP_String, 0, 0);
2468 sqliteVdbeChangeP3(v, -1,
drh3c2007a2002-10-20 16:00:27 +00002469 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00002470 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2471 sqliteVdbeAddOp(v, OP_String, 0, 0);
2472 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2473 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002474 }
2475 }
2476 }else
2477
2478 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2479 Index *pIdx;
2480 Table *pTab;
2481 Vdbe *v;
2482 pIdx = sqliteFindIndex(db, zRight);
2483 if( pIdx ) v = sqliteGetVdbe(pParse);
2484 if( pIdx && v ){
2485 static VdbeOp tableInfoPreface[] = {
drh382c0242001-10-06 16:33:02 +00002486 { OP_ColumnName, 0, 0, "seqno"},
2487 { OP_ColumnName, 1, 0, "cid"},
2488 { OP_ColumnName, 2, 0, "name"},
2489 };
2490 int i;
2491 pTab = pIdx->pTable;
2492 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2493 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002494 int cnum = pIdx->aiColumn[i];
2495 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2496 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2497 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002498 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002499 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2500 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002501 }
2502 }
2503 }else
2504
drh81a20f22001-10-12 17:30:04 +00002505 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2506 Index *pIdx;
2507 Table *pTab;
2508 Vdbe *v;
2509 pTab = sqliteFindTable(db, zRight);
2510 if( pTab ){
2511 v = sqliteGetVdbe(pParse);
2512 pIdx = pTab->pIndex;
2513 }
2514 if( pTab && pIdx && v ){
2515 int i = 0;
2516 static VdbeOp indexListPreface[] = {
drh81a20f22001-10-12 17:30:04 +00002517 { OP_ColumnName, 0, 0, "seq"},
2518 { OP_ColumnName, 1, 0, "name"},
2519 { OP_ColumnName, 2, 0, "unique"},
2520 };
2521
2522 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2523 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002524 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2525 sqliteVdbeAddOp(v, OP_String, 0, 0);
2526 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002527 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002528 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002529 ++i;
2530 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002531 }
2532 }
2533 }else
2534
drhf57b14a2001-09-14 18:54:08 +00002535#ifndef NDEBUG
2536 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2537 extern void sqliteParserTrace(FILE*, char *);
2538 if( getBoolean(zRight) ){
2539 sqliteParserTrace(stdout, "parser: ");
2540 }else{
2541 sqliteParserTrace(0, 0);
2542 }
2543 }else
2544#endif
2545
drhaaab5722002-02-19 13:39:21 +00002546 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002547 static VdbeOp checkDb[] = {
2548 { OP_SetInsert, 0, 0, "2"},
2549 { OP_Open, 0, 2, 0},
2550 { OP_Rewind, 0, 6, 0},
drh21504322002-06-25 13:16:02 +00002551 { OP_Column, 0, 3, 0}, /* 3 */
drh1bffb9c2002-02-03 17:37:36 +00002552 { OP_SetInsert, 0, 0, 0},
2553 { OP_Next, 0, 3, 0},
drh21504322002-06-25 13:16:02 +00002554 { OP_IntegrityCk, 0, 0, 0}, /* 6 */
drh4ff6dfa2002-03-03 23:06:00 +00002555 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002556 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002557 { OP_SetInsert, 1, 0, "2"},
2558 { OP_OpenAux, 1, 2, 0},
drh836faa42003-01-11 13:30:57 +00002559 { OP_Rewind, 1, 15, 0},
2560 { OP_Column, 1, 3, 0}, /* 12 */
drh21504322002-06-25 13:16:02 +00002561 { OP_SetInsert, 1, 0, 0},
drh836faa42003-01-11 13:30:57 +00002562 { OP_Next, 1, 12, 0},
2563 { OP_IntegrityCk, 1, 1, 0}, /* 15 */
drh21504322002-06-25 13:16:02 +00002564 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002565 };
2566 Vdbe *v = sqliteGetVdbe(pParse);
2567 if( v==0 ) return;
2568 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2569 }else
drh1bffb9c2002-02-03 17:37:36 +00002570
drhf57b3392001-10-08 13:22:32 +00002571 {}
2572 sqliteFree(zLeft);
2573 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002574}