blob: ea40e30022d3610f05a1c4635b9f4d62d8eaedd8 [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**
drh3c2007a2002-10-20 16:00:27 +000028** $Id: build.c,v 1.115 2002/10/20 16:00:28 drh Exp $
drh75897232000-05-29 14:26:00 +000029*/
30#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000031#include <ctype.h>
drh75897232000-05-29 14:26:00 +000032
33/*
drhe0bc4042002-06-25 01:09:11 +000034** This routine is called when a new SQL statement is beginning to
35** be parsed. Check to see if the schema for the database needs
36** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
37** If it does, then read it.
38*/
39void sqliteBeginParse(Parse *pParse, int explainFlag){
40 sqlite *db = pParse->db;
41 pParse->explain = explainFlag;
42 if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){
43 int rc = sqliteInit(db, &pParse->zErrMsg);
44 if( rc!=SQLITE_OK ){
45 pParse->rc = rc;
46 pParse->nErr++;
47 }
48 }
49}
50
51/*
drh75897232000-05-29 14:26:00 +000052** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000053** parsed and we want to execute the VDBE code to implement
54** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000055** constructed VDBE code to do the work of the SQL statement.
56** This routine just has to execute the VDBE code.
57**
58** Note that if an error occurred, it might be the case that
59** no VDBE code was generated.
60*/
61void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000062 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000063 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000064 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000065 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000066 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000067 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
68 &pParse->zErrMsg);
drhe0bc4042002-06-25 01:09:11 +000069 db->next_cookie = db->schema_cookie;
drh75897232000-05-29 14:26:00 +000070 }else{
drh3fc190c2001-09-14 03:24:23 +000071 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000072 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000073 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000074 &pParse->zErrMsg, db->pBusyArg,
75 db->xBusyCallback);
drhecdc7532001-09-23 02:35:53 +000076 if( rc ) pParse->nErr++;
drh75897232000-05-29 14:26:00 +000077 }
78 sqliteVdbeDelete(pParse->pVdbe);
79 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000080 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000081 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000082 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000083 }
drha226d052002-09-25 19:04:07 +000084 pParse->nTab = 0;
85 pParse->nMem = 0;
86 pParse->nSet = 0;
87 pParse->nAgg = 0;
drh75897232000-05-29 14:26:00 +000088}
89
90/*
drhf57b3392001-10-08 13:22:32 +000091** Locate the in-memory structure that describes
92** a particular database table given the name
drh75897232000-05-29 14:26:00 +000093** of that table. Return NULL if not found.
94*/
drha76b5df2002-02-23 02:32:10 +000095Table *sqliteFindTable(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +000096 Table *p;
97 p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +000098 return p;
drh75897232000-05-29 14:26:00 +000099}
100
101/*
drhf57b3392001-10-08 13:22:32 +0000102** Locate the in-memory structure that describes
103** a particular index given the name of that index.
104** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000105*/
drha76b5df2002-02-23 02:32:10 +0000106Index *sqliteFindIndex(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +0000107 Index *p;
108 p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +0000109 return p;
drh75897232000-05-29 14:26:00 +0000110}
111
112/*
113** Remove the given index from the index hash table, and free
114** its memory structures.
115**
drhd229ca92002-01-09 13:30:41 +0000116** The index is removed from the database hash tables but
117** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000118** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000119*/
drh74e24cd2002-01-09 03:19:59 +0000120static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000121 Index *pOld;
122 assert( db!=0 && p->zName!=0 );
123 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
124 if( pOld!=0 && pOld!=p ){
125 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000126 }
drh74e24cd2002-01-09 03:19:59 +0000127 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000128}
129
130/*
drhbeae3192001-09-22 18:12:08 +0000131** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000132** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000133** structures.
134*/
drh6d4abfb2001-10-22 02:58:08 +0000135void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000136 if( pIndex->pTable->pIndex==pIndex ){
137 pIndex->pTable->pIndex = pIndex->pNext;
138 }else{
139 Index *p;
140 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
141 if( p && p->pNext==pIndex ){
142 p->pNext = pIndex->pNext;
143 }
144 }
145 sqliteDeleteIndex(db, pIndex);
146}
147
148/*
drhe0bc4042002-06-25 01:09:11 +0000149** Erase all schema information from the in-memory hash tables of
150** database connection. This routine is called to reclaim memory
151** before the connection closes. It is also called during a rollback
152** if there were schema changes during the transaction.
drh74e24cd2002-01-09 03:19:59 +0000153*/
drhe0bc4042002-06-25 01:09:11 +0000154void sqliteResetInternalSchema(sqlite *db){
155 HashElem *pElem;
156 Hash temp1;
157 Hash temp2;
158
drhc2eef3b2002-08-31 18:53:06 +0000159 sqliteHashClear(&db->aFKey);
drhe0bc4042002-06-25 01:09:11 +0000160 temp1 = db->tblHash;
161 temp2 = db->trigHash;
162 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
163 sqliteHashClear(&db->idxHash);
164 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
165 Trigger *pTrigger = sqliteHashData(pElem);
166 sqliteDeleteTrigger(pTrigger);
drh74e24cd2002-01-09 03:19:59 +0000167 }
drhe0bc4042002-06-25 01:09:11 +0000168 sqliteHashClear(&temp2);
169 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
170 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
171 Table *pTab = sqliteHashData(pElem);
172 sqliteDeleteTable(db, pTab);
173 }
174 sqliteHashClear(&temp1);
175 db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges);
176}
177
178/*
179** This routine is called whenever a rollback occurs. If there were
180** schema changes during the transaction, then we have to reset the
181** internal hash tables and reload them from disk.
182*/
183void sqliteRollbackInternalChanges(sqlite *db){
184 if( db->flags & SQLITE_InternChanges ){
185 sqliteResetInternalSchema(db);
186 }
187}
188
189/*
190** This routine is called when a commit occurs.
191*/
192void sqliteCommitInternalChanges(sqlite *db){
193 db->schema_cookie = db->next_cookie;
194 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000195}
196
197/*
drh75897232000-05-29 14:26:00 +0000198** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000199** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000200**
201** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000202** the table data structure from the hash table. Nor does it remove
203** foreign keys from the sqlite.aFKey hash table. But it does destroy
204** memory structures of the indices and foreign keys associated with
205** the table.
drhdaffd0e2001-04-11 14:28:42 +0000206**
207** Indices associated with the table are unlinked from the "db"
208** data structure if db!=NULL. If db==NULL, indices attached to
209** the table are deleted, but it is assumed they have already been
210** unlinked.
drh75897232000-05-29 14:26:00 +0000211*/
212void sqliteDeleteTable(sqlite *db, Table *pTable){
213 int i;
214 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000215 FKey *pFKey, *pNextFKey;
216
drh75897232000-05-29 14:26:00 +0000217 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000218
219 /* Delete all indices associated with this table
220 */
221 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
222 pNext = pIndex->pNext;
223 sqliteDeleteIndex(db, pIndex);
224 }
225
226 /* Delete all foreign keys associated with this table. The keys
227 ** should have already been unlinked from the db->aFKey hash table
228 */
229 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
230 pNextFKey = pFKey->pNextFrom;
231 assert( sqliteHashFind(&db->aFKey,pFKey->zTo,strlen(pFKey->zTo)+1)!=pFKey );
232 sqliteFree(pFKey);
233 }
234
235 /* Delete the Table structure itself.
236 */
drh75897232000-05-29 14:26:00 +0000237 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000238 sqliteFree(pTable->aCol[i].zName);
239 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000240 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000241 }
drh6e142f52000-06-08 13:36:40 +0000242 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000243 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000244 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000245 sqliteFree(pTable);
246}
247
248/*
drh5edc3122001-09-13 21:53:09 +0000249** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000250** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000251*/
drh74e24cd2002-01-09 03:19:59 +0000252static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000253 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000254 FKey *pF1, *pF2;
drhd229ca92002-01-09 13:30:41 +0000255 assert( db!=0 );
256 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
257 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000258 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
259 int nTo = strlen(pF1->zTo) + 1;
260 pF2 = sqliteHashFind(&db->aFKey, pF1->zTo, nTo);
261 if( pF2==pF1 ){
262 sqliteHashInsert(&db->aFKey, pF1->zTo, nTo, pF1->pNextTo);
263 }else{
264 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
265 if( pF2 ){
266 pF2->pNextTo = pF1->pNextTo;
267 }
268 }
269 }
drh74e24cd2002-01-09 03:19:59 +0000270 sqliteDeleteTable(db, p);
271}
272
273/*
drh1ccde152000-06-17 13:12:39 +0000274** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000275**
276** Space to hold the name is obtained from sqliteMalloc() and must
277** be freed by the calling function.
278*/
drhcce7d172000-05-31 15:34:51 +0000279char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000280 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000281 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000282 return zName;
283}
284
285/*
drhe0bc4042002-06-25 01:09:11 +0000286** Generate code to open the appropriate master table. The table
287** opened will be SQLITE_MASTER for persistent tables and
288** SQLITE_TEMP_MASTER for temporary tables. The table is opened
289** on cursor 0.
290*/
291void sqliteOpenMasterTable(Vdbe *v, int isTemp){
292 if( isTemp ){
293 sqliteVdbeAddOp(v, OP_OpenWrAux, 0, 2);
294 sqliteVdbeChangeP3(v, -1, TEMP_MASTER_NAME, P3_STATIC);
295 }else{
296 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
297 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
298 }
299}
300
301/*
drh75897232000-05-29 14:26:00 +0000302** Begin constructing a new table representation in memory. This is
303** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000304** to a CREATE TABLE statement. In particular, this routine is called
305** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000306** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000307** flag is true if the table should be stored in the auxiliary database
308** file instead of in the main database file. This is normally the case
309** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000310** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000311**
drhf57b3392001-10-08 13:22:32 +0000312** The new table record is initialized and put in pParse->pNewTable.
313** As more of the CREATE TABLE statement is parsed, additional action
314** routines will be called to add more information to this record.
315** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
316** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000317*/
drhf57b3392001-10-08 13:22:32 +0000318void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){
drh75897232000-05-29 14:26:00 +0000319 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000320 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000321 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000322 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000323 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000324
325 pParse->sFirstToken = *pStart;
326 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000327 if( zName==0 ) return;
drhf57b3392001-10-08 13:22:32 +0000328
329 /* Before trying to create a temporary table, make sure the Btree for
330 ** holding temporary tables is open.
331 */
332 if( isTemp && db->pBeTemp==0 ){
333 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
334 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000335 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000336 "file for storing temporary tables", 0);
337 pParse->nErr++;
338 return;
339 }
340 if( db->flags & SQLITE_InTrans ){
341 rc = sqliteBtreeBeginTrans(db->pBeTemp);
342 if( rc!=SQLITE_OK ){
343 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000344 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000345 pParse->nErr++;
346 return;
347 }
348 }
349 }
350
351 /* Make sure the new table name does not collide with an existing
352 ** index or table name. Issue an error message if it does.
353 **
354 ** If we are re-reading the sqlite_master table because of a schema
355 ** change and a new permanent table is found whose name collides with
356 ** an existing temporary table, then ignore the new permanent table.
357 ** We will continue parsing, but the pParse->nameClash flag will be set
358 ** so we will know to discard the table record once parsing has finished.
359 */
drhbe0072d2001-09-13 14:46:09 +0000360 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000361 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000362 if( pTable->isTemp && pParse->initFlag ){
363 pParse->nameClash = 1;
364 }else{
365 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
366 " already exists", 0, 0);
367 sqliteFree(zName);
368 pParse->nErr++;
369 return;
370 }
371 }else{
372 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000373 }
drhf57b3392001-10-08 13:22:32 +0000374 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
375 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000376 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
377 zName, 0);
drh75897232000-05-29 14:26:00 +0000378 sqliteFree(zName);
379 pParse->nErr++;
380 return;
381 }
382 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000383 if( pTable==0 ){
384 sqliteFree(zName);
385 return;
386 }
drh75897232000-05-29 14:26:00 +0000387 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000388 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000389 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000390 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000391 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000392 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000393 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000394 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000395
396 /* Begin generating the code that will insert the table record into
397 ** the SQLITE_MASTER table. Note in particular that we must go ahead
398 ** and allocate the record number for the table entry now. Before any
399 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
400 ** indices to be created and the table record must come before the
401 ** indices. Hence, the record number for the table must be allocated
402 ** now.
403 */
drhadbca9c2001-09-27 15:11:53 +0000404 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000405 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000406 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000407 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
408 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000409 }
drhe0bc4042002-06-25 01:09:11 +0000410 sqliteOpenMasterTable(v, isTemp);
411 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
412 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
413 sqliteVdbeAddOp(v, OP_String, 0, 0);
414 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000415 }
drh75897232000-05-29 14:26:00 +0000416}
417
418/*
419** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000420**
421** The parser calls this routine once for each column declaration
422** in a CREATE TABLE statement. sqliteStartTable() gets called
423** first to get things going. Then this routine is called for each
424** column.
drh75897232000-05-29 14:26:00 +0000425*/
426void sqliteAddColumn(Parse *pParse, Token *pName){
427 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000428 int i;
429 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000430 Column *pCol;
drh75897232000-05-29 14:26:00 +0000431 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000432 sqliteSetNString(&z, pName->z, pName->n, 0);
433 if( z==0 ) return;
434 sqliteDequote(z);
435 for(i=0; i<p->nCol; i++){
436 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
437 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
438 pParse->nErr++;
439 sqliteFree(z);
440 return;
441 }
442 }
drh75897232000-05-29 14:26:00 +0000443 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000444 Column *aNew;
445 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
446 if( aNew==0 ) return;
447 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000448 }
drhc9b84a12002-06-20 11:36:48 +0000449 pCol = &p->aCol[p->nCol];
450 memset(pCol, 0, sizeof(p->aCol[0]));
451 pCol->zName = z;
452 pCol->sortOrder = SQLITE_SO_NUM;
453 p->nCol++;
drh75897232000-05-29 14:26:00 +0000454}
455
456/*
drh382c0242001-10-06 16:33:02 +0000457** This routine is called by the parser while in the middle of
458** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
459** been seen on a column. This routine sets the notNull flag on
460** the column currently under construction.
461*/
drh9cfcf5d2002-01-29 18:41:24 +0000462void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000463 Table *p;
464 int i;
465 if( (p = pParse->pNewTable)==0 ) return;
466 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000467 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000468}
469
470/*
471** This routine is called by the parser while in the middle of
472** parsing a CREATE TABLE statement. The pFirst token is the first
473** token in the sequence of tokens that describe the type of the
474** column currently under construction. pLast is the last token
475** in the sequence. Use this information to construct a string
476** that contains the typename of the column and store that string
477** in zType.
478*/
479void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
480 Table *p;
481 int i, j;
482 int n;
483 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000484 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000485 if( (p = pParse->pNewTable)==0 ) return;
486 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000487 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000488 pCol = &p->aCol[i];
489 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000490 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000491 sqliteSetNString(pz, pFirst->z, n, 0);
492 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000493 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000494 for(i=j=0; z[i]; i++){
495 int c = z[i];
496 if( isspace(c) ) continue;
497 z[j++] = c;
498 }
499 z[j] = 0;
drhc9b84a12002-06-20 11:36:48 +0000500 pCol->sortOrder = SQLITE_SO_NUM;
drh3d037a92002-08-15 01:26:09 +0000501 if( pParse->db->file_format>=4 ){
502 for(i=0; z[i]; i++){
503 switch( z[i] ){
504 case 'b':
505 case 'B': {
506 if( sqliteStrNICmp(&z[i],"blob",4)==0 ){
507 pCol->sortOrder = SQLITE_SO_TEXT;
508 return;
509 }
510 break;
drh38640e12002-07-05 21:42:36 +0000511 }
drh3d037a92002-08-15 01:26:09 +0000512 case 'c':
513 case 'C': {
514 if( sqliteStrNICmp(&z[i],"char",4)==0 ||
515 sqliteStrNICmp(&z[i],"clob",4)==0 ){
516 pCol->sortOrder = SQLITE_SO_TEXT;
517 return;
518 }
519 break;
drhc9b84a12002-06-20 11:36:48 +0000520 }
drh3d037a92002-08-15 01:26:09 +0000521 case 'x':
522 case 'X': {
523 if( i>=2 && sqliteStrNICmp(&z[i-2],"text",4)==0 ){
524 pCol->sortOrder = SQLITE_SO_TEXT;
525 return;
526 }
527 break;
drhc9b84a12002-06-20 11:36:48 +0000528 }
drh3d037a92002-08-15 01:26:09 +0000529 default: {
530 break;
531 }
drhc9b84a12002-06-20 11:36:48 +0000532 }
533 }
534 }
drh382c0242001-10-06 16:33:02 +0000535}
536
537/*
drh7020f652000-06-03 18:06:52 +0000538** The given token is the default value for the last column added to
539** the table currently under construction. If "minusFlag" is true, it
540** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000541**
542** This routine is called by the parser while in the middle of
543** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000544*/
545void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
546 Table *p;
547 int i;
548 char **pz;
549 if( (p = pParse->pNewTable)==0 ) return;
550 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000551 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000552 pz = &p->aCol[i].zDflt;
553 if( minusFlag ){
554 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
555 }else{
556 sqliteSetNString(pz, pVal->z, pVal->n, 0);
557 }
558 sqliteDequote(*pz);
559}
560
561/*
drh4a324312001-12-21 14:30:42 +0000562** Designate the PRIMARY KEY for the table. pList is a list of names
563** of columns that form the primary key. If pList is NULL, then the
564** most recently added column of the table is the primary key.
565**
566** A table can have at most one primary key. If the table already has
567** a primary key (and this is the second primary key) then create an
568** error.
569**
570** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
571** then we will try to use that column as the row id. (Exception:
572** For backwards compatibility with older databases, do not do this
573** if the file format version number is less than 1.) Set the Table.iPKey
574** field of the table under construction to be the index of the
575** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
576** no INTEGER PRIMARY KEY.
577**
578** If the key is not an INTEGER PRIMARY KEY, then create a unique
579** index for the key. No index is created for INTEGER PRIMARY KEYs.
580*/
drh9cfcf5d2002-01-29 18:41:24 +0000581void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000582 Table *pTab = pParse->pNewTable;
583 char *zType = 0;
584 int iCol = -1;
585 if( pTab==0 ) return;
586 if( pTab->hasPrimKey ){
587 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
588 "\" has more than one primary key", 0);
589 pParse->nErr++;
590 return;
591 }
592 pTab->hasPrimKey = 1;
593 if( pList==0 ){
594 iCol = pTab->nCol - 1;
595 }else if( pList->nId==1 ){
596 for(iCol=0; iCol<pTab->nCol; iCol++){
597 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
598 }
599 }
600 if( iCol>=0 && iCol<pTab->nCol ){
601 zType = pTab->aCol[iCol].zType;
602 }
603 if( pParse->db->file_format>=1 &&
604 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
605 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000606 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000607 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000608 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drh4a324312001-12-21 14:30:42 +0000609 }
610}
611
612/*
drh8e2ca022002-06-17 17:07:19 +0000613** Return the appropriate collating type given the collation type token.
614** Report an error if the type is undefined.
615*/
616int sqliteCollateType(Parse *pParse, Token *pType){
617 if( pType==0 ) return SQLITE_SO_UNK;
618 if( pType->n==4 && sqliteStrNICmp(pType->z, "text", 4)==0 ){
619 return SQLITE_SO_TEXT;
620 }
621 if( pType->n==7 && sqliteStrNICmp(pType->z, "numeric", 7)==0 ){
622 return SQLITE_SO_NUM;
623 }
624 sqliteSetNString(&pParse->zErrMsg, "unknown collating type: ", -1,
625 pType->z, pType->n, 0);
626 pParse->nErr++;
627 return SQLITE_SO_UNK;
628}
629
630/*
631** This routine is called by the parser while in the middle of
632** parsing a CREATE TABLE statement. A "COLLATE" clause has
633** been seen on a column. This routine sets the Column.sortOrder on
634** the column currently under construction.
635*/
636void sqliteAddCollateType(Parse *pParse, int collType){
637 Table *p;
638 int i;
639 if( (p = pParse->pNewTable)==0 ) return;
640 i = p->nCol-1;
641 if( i>=0 ) p->aCol[i].sortOrder = collType;
642}
643
644/*
drh50e5dad2001-09-15 00:57:28 +0000645** Come up with a new random value for the schema cookie. Make sure
646** the new value is different from the old.
647**
648** The schema cookie is used to determine when the schema for the
649** database changes. After each schema change, the cookie value
650** changes. When a process first reads the schema it records the
651** cookie. Thereafter, whenever it goes to access the database,
652** it checks the cookie to make sure the schema has not changed
653** since it was last read.
654**
655** This plan is not completely bullet-proof. It is possible for
656** the schema to change multiple times and for the cookie to be
657** set back to prior value. But schema changes are infrequent
658** and the probability of hitting the same cookie value is only
659** 1 chance in 2^32. So we're safe enough.
660*/
drhe0bc4042002-06-25 01:09:11 +0000661void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh50e5dad2001-09-15 00:57:28 +0000662 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000663 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000664 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000665 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
666 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000667 }
668}
669
670/*
drh969fa7c2002-02-18 18:30:32 +0000671** Measure the number of characters needed to output the given
672** identifier. The number returned includes any quotes used
673** but does not include the null terminator.
674*/
675static int identLength(const char *z){
676 int n;
drh17f71932002-02-21 12:01:27 +0000677 int needQuote = 0;
678 for(n=0; *z; n++, z++){
679 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000680 }
drh17f71932002-02-21 12:01:27 +0000681 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000682}
683
684/*
685** Write an identifier onto the end of the given string. Add
686** quote characters as needed.
687*/
688static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000689 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000690 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000691 for(j=0; zIdent[j]; j++){
692 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
693 }
694 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
695 || sqliteKeywordCode(zIdent, j)!=TK_ID;
696 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000697 for(j=0; zIdent[j]; j++){
698 z[i++] = zIdent[j];
699 if( zIdent[j]=='\'' ) z[i++] = '\'';
700 }
drh17f71932002-02-21 12:01:27 +0000701 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000702 z[i] = 0;
703 *pIdx = i;
704}
705
706/*
707** Generate a CREATE TABLE statement appropriate for the given
708** table. Memory to hold the text of the statement is obtained
709** from sqliteMalloc() and must be freed by the calling function.
710*/
711static char *createTableStmt(Table *p){
712 int i, k, n;
713 char *zStmt;
714 char *zSep, *zSep2, *zEnd;
715 n = 0;
716 for(i=0; i<p->nCol; i++){
717 n += identLength(p->aCol[i].zName);
718 }
719 n += identLength(p->zName);
720 if( n<40 ){
721 zSep = "";
722 zSep2 = ",";
723 zEnd = ")";
724 }else{
725 zSep = "\n ";
726 zSep2 = ",\n ";
727 zEnd = "\n)";
728 }
drhe0bc4042002-06-25 01:09:11 +0000729 n += 35 + 6*p->nCol;
drh969fa7c2002-02-18 18:30:32 +0000730 zStmt = sqliteMalloc( n );
731 if( zStmt==0 ) return 0;
drhe0bc4042002-06-25 01:09:11 +0000732 strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000733 k = strlen(zStmt);
734 identPut(zStmt, &k, p->zName);
735 zStmt[k++] = '(';
736 for(i=0; i<p->nCol; i++){
737 strcpy(&zStmt[k], zSep);
738 k += strlen(&zStmt[k]);
739 zSep = zSep2;
740 identPut(zStmt, &k, p->aCol[i].zName);
741 }
742 strcpy(&zStmt[k], zEnd);
743 return zStmt;
744}
745
746/*
drh75897232000-05-29 14:26:00 +0000747** This routine is called to report the final ")" that terminates
748** a CREATE TABLE statement.
749**
drhf57b3392001-10-08 13:22:32 +0000750** The table structure that other action routines have been building
751** is added to the internal hash tables, assuming no errors have
752** occurred.
drh75897232000-05-29 14:26:00 +0000753**
drh1ccde152000-06-17 13:12:39 +0000754** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000755** unless this is a temporary table or initFlag==1. When initFlag==1,
756** it means we are reading the sqlite_master table because we just
757** connected to the database or because the sqlite_master table has
758** recently changes, so the entry for this table already exists in
759** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000760**
761** If the pSelect argument is not NULL, it means that this routine
762** was called to create a table generated from a
763** "CREATE TABLE ... AS SELECT ..." statement. The column names of
764** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000765*/
drh969fa7c2002-02-18 18:30:32 +0000766void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000767 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000768 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000769
drh969fa7c2002-02-18 18:30:32 +0000770 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000771 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000772 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000773
drhf57b3392001-10-08 13:22:32 +0000774 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000775 */
drhad75e982001-10-09 04:19:46 +0000776 assert( pParse->nameClash==0 || pParse->initFlag==1 );
drhf57b3392001-10-08 13:22:32 +0000777 if( pParse->explain==0 && pParse->nameClash==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000778 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000779 FKey *pFKey;
drh6d4abfb2001-10-22 02:58:08 +0000780 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
781 if( pOld ){
drh74e24cd2002-01-09 03:19:59 +0000782 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh6d4abfb2001-10-22 02:58:08 +0000783 return;
784 }
drhc2eef3b2002-08-31 18:53:06 +0000785 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
786 int nTo = strlen(pFKey->zTo) + 1;
787 pFKey->pNextTo = sqliteHashFind(&db->aFKey, pFKey->zTo, nTo);
788 sqliteHashInsert(&db->aFKey, pFKey->zTo, nTo, pFKey);
789 }
drh75897232000-05-29 14:26:00 +0000790 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000791 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000792 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000793 }
794
drh969fa7c2002-02-18 18:30:32 +0000795 /* If the table is generated from a SELECT, then construct the
796 ** list of columns and the text of the table.
797 */
798 if( pSelect ){
799 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000800 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000801 assert( p->aCol==0 );
802 p->nCol = pSelTab->nCol;
803 p->aCol = pSelTab->aCol;
804 pSelTab->nCol = 0;
805 pSelTab->aCol = 0;
806 sqliteDeleteTable(0, pSelTab);
807 }
808
drhd78eeee2001-09-13 16:18:53 +0000809 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000810 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
811 ** So do not write to the disk again. Extract the root page number
812 ** for the table from the pParse->newTnum field. (The page number
813 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000814 */
815 if( pParse->initFlag ){
816 p->tnum = pParse->newTnum;
817 }
818
drhe3c41372001-09-17 20:25:58 +0000819 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000820 ** in the SQLITE_MASTER table of the database. The record number
821 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000822 **
drhe0bc4042002-06-25 01:09:11 +0000823 ** If this is a TEMPORARY table, write the entry into the auxiliary
824 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000825 */
826 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000827 int n;
drhd8bc7082000-06-07 23:51:50 +0000828 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000829
drhd8bc7082000-06-07 23:51:50 +0000830 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000831 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000832 if( p->pSelect==0 ){
833 /* A regular table */
834 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
835 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
836 }else{
837 /* A view */
838 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
839 }
drh969fa7c2002-02-18 18:30:32 +0000840 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000841 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
842 sqliteVdbeAddOp(v, OP_String, 0, 0);
843 if( p->pSelect==0 ){
844 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
845 }else{
846 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000847 }
drhe0bc4042002-06-25 01:09:11 +0000848 sqliteVdbeAddOp(v, OP_String, 0, 0);
849 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
850 sqliteVdbeAddOp(v, OP_String, 0, 0);
851 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
852 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
853 sqliteVdbeAddOp(v, OP_String, 0, 0);
854 if( pSelect ){
855 char *z = createTableStmt(p);
856 n = z ? strlen(z) : 0;
857 sqliteVdbeChangeP3(v, -1, z, n);
858 sqliteFree(z);
859 }else{
860 assert( pEnd!=0 );
861 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
862 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
863 }
864 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
865 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
866 if( !p->isTemp ){
867 sqliteChangeCookie(db, v);
868 }
869 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000870 if( pSelect ){
871 int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
872 sqliteVdbeAddOp(v, op, 1, 0);
873 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000874 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000875 }
drh1c928532002-01-31 15:54:21 +0000876 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000877 }
878}
879
880/*
drha76b5df2002-02-23 02:32:10 +0000881** The parser calls this routine in order to create a new VIEW
882*/
883void sqliteCreateView(
884 Parse *pParse, /* The parsing context */
885 Token *pBegin, /* The CREATE token that begins the statement */
886 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000887 Select *pSelect, /* A SELECT statement that will become the new view */
888 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000889){
drha76b5df2002-02-23 02:32:10 +0000890 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000891 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000892 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000893 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000894
drh6276c1c2002-07-08 22:03:32 +0000895 sqliteStartTable(pParse, pBegin, pName, isTemp);
drha76b5df2002-02-23 02:32:10 +0000896 p = pParse->pNewTable;
drh417be792002-03-03 18:59:40 +0000897 if( p==0 ){
898 sqliteSelectDelete(pSelect);
899 return;
900 }
drh0f18b452002-05-08 21:30:15 +0000901 /* Ignore ORDER BY clauses on a SELECT */
902 if( pSelect->pOrderBy ){
903 sqliteExprListDelete(pSelect->pOrderBy);
904 pSelect->pOrderBy = 0;
905 }
drh4b59ab52002-08-24 18:24:51 +0000906 /* Make a copy of the entire SELECT statement that defines the view.
907 ** This will force all the Expr.token.z values to be dynamically
908 ** allocated rather than point to the input string - which means that
909 ** they will persist after the current sqlite_exec() call returns.
910 */
911 p->pSelect = sqliteSelectDup(pSelect);
912 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +0000913 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +0000914 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +0000915 }
drh4b59ab52002-08-24 18:24:51 +0000916
917 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
918 ** the end.
919 */
drha76b5df2002-02-23 02:32:10 +0000920 sEnd = pParse->sLastToken;
921 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
922 sEnd.z += sEnd.n;
923 }
924 sEnd.n = 0;
925 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000926 z = pBegin->z;
927 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
928 sEnd.z = &z[n-1];
929 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +0000930
931 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
932 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +0000933 return;
drh417be792002-03-03 18:59:40 +0000934}
drha76b5df2002-02-23 02:32:10 +0000935
drh417be792002-03-03 18:59:40 +0000936/*
937** The Table structure pTable is really a VIEW. Fill in the names of
938** the columns of the view in the pTable structure. Return the number
939** of errors. If an error is seen leave an error message in pPare->zErrMsg.
940*/
941int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
942 ExprList *pEList;
943 Select *pSel;
944 Table *pSelTab;
945 int nErr = 0;
946
947 assert( pTable );
948
949 /* A positive nCol means the columns names for this view are
950 ** already known.
951 */
952 if( pTable->nCol>0 ) return 0;
953
954 /* A negative nCol is a special marker meaning that we are currently
955 ** trying to compute the column names. If we enter this routine with
956 ** a negative nCol, it means two or more views form a loop, like this:
957 **
958 ** CREATE VIEW one AS SELECT * FROM two;
959 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +0000960 **
961 ** Actually, this error is caught previously and so the following test
962 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +0000963 */
964 if( pTable->nCol<0 ){
965 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
966 " is circularly defined", 0);
967 pParse->nErr++;
968 return 1;
969 }
970
971 /* If we get this far, it means we need to compute the table names.
972 */
973 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
974 pSel = pTable->pSelect;
975
976 /* Note that the call to sqliteResultSetOfSelect() will expand any
977 ** "*" elements in this list. But we will need to restore the list
978 ** back to its original configuration afterwards, so we save a copy of
979 ** the original in pEList.
980 */
981 pEList = pSel->pEList;
982 pSel->pEList = sqliteExprListDup(pEList);
983 if( pSel->pEList==0 ){
984 pSel->pEList = pEList;
985 return 1; /* Malloc failed */
986 }
987 pTable->nCol = -1;
988 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
989 if( pSelTab ){
990 assert( pTable->aCol==0 );
991 pTable->nCol = pSelTab->nCol;
992 pTable->aCol = pSelTab->aCol;
993 pSelTab->nCol = 0;
994 pSelTab->aCol = 0;
995 sqliteDeleteTable(0, pSelTab);
996 pParse->db->flags |= SQLITE_UnresetViews;
997 }else{
998 pTable->nCol = 0;
999 nErr++;
1000 }
1001 sqliteSelectUnbind(pSel);
1002 sqliteExprListDelete(pSel->pEList);
1003 pSel->pEList = pEList;
1004 return nErr;
1005}
1006
1007/*
1008** Clear the column names from the VIEW pTable.
1009**
1010** This routine is called whenever any other table or view is modified.
1011** The view passed into this routine might depend directly or indirectly
1012** on the modified or deleted table so we need to clear the old column
1013** names so that they will be recomputed.
1014*/
1015static void sqliteViewResetColumnNames(Table *pTable){
1016 int i;
1017 if( pTable==0 || pTable->pSelect==0 ) return;
1018 if( pTable->nCol==0 ) return;
1019 for(i=0; i<pTable->nCol; i++){
1020 sqliteFree(pTable->aCol[i].zName);
1021 sqliteFree(pTable->aCol[i].zDflt);
1022 sqliteFree(pTable->aCol[i].zType);
1023 }
1024 sqliteFree(pTable->aCol);
1025 pTable->aCol = 0;
1026 pTable->nCol = 0;
1027}
1028
1029/*
1030** Clear the column names from every VIEW.
1031*/
1032void sqliteViewResetAll(sqlite *db){
1033 HashElem *i;
1034 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
1035 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
1036 Table *pTab = sqliteHashData(i);
1037 if( pTab->pSelect ){
1038 sqliteViewResetColumnNames(pTab);
1039 }
1040 }
1041 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001042}
1043
1044/*
drh75897232000-05-29 14:26:00 +00001045** Given a token, look up a table with that name. If not found, leave
1046** an error for the parser to find and return NULL.
1047*/
drhcce7d172000-05-31 15:34:51 +00001048Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001049 char *zName;
1050 Table *pTab;
1051 zName = sqliteTableNameFromToken(pTok);
1052 if( zName==0 ) return 0;
1053 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +00001054 sqliteFree(zName);
1055 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001056 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1057 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001058 pParse->nErr++;
1059 }
1060 return pTab;
1061}
1062
1063/*
1064** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001065** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001066*/
drh4ff6dfa2002-03-03 23:06:00 +00001067void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001068 Table *pTable;
drh75897232000-05-29 14:26:00 +00001069 Vdbe *v;
1070 int base;
drh5edc3122001-09-13 21:53:09 +00001071 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001072
drhdaffd0e2001-04-11 14:28:42 +00001073 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001074 pTable = sqliteTableFromToken(pParse, pName);
1075 if( pTable==0 ) return;
1076 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001077 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1078 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001079 pParse->nErr++;
1080 return;
1081 }
drh4ff6dfa2002-03-03 23:06:00 +00001082 if( isView && pTable->pSelect==0 ){
1083 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1084 pTable->zName, 0);
1085 pParse->nErr++;
1086 return;
1087 }
1088 if( !isView && pTable->pSelect ){
1089 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1090 pTable->zName, 0);
1091 pParse->nErr++;
1092 return;
1093 }
drh75897232000-05-29 14:26:00 +00001094
drh1ccde152000-06-17 13:12:39 +00001095 /* Generate code to remove the table from the master table
1096 ** on disk.
1097 */
drhd8bc7082000-06-07 23:51:50 +00001098 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001099 if( v ){
1100 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001101 { OP_Rewind, 0, ADDR(8), 0},
1102 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001103 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001104 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001105 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001106 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001107 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001108 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001109 };
1110 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001111 Trigger *pTrigger;
drhcabb0812002-09-14 13:47:32 +00001112 sqliteBeginWriteOperation(pParse, 0, pTable->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001113 sqliteOpenMasterTable(v, pTable->isTemp);
danielk1977c3f9bad2002-05-15 08:30:12 +00001114 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001115 pTrigger = pTable->pTrigger;
1116 while( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001117 Token tt;
1118 tt.z = pTable->pTrigger->name;
1119 tt.n = strlen(pTable->pTrigger->name);
1120 sqliteDropTrigger(pParse, &tt, 1);
drhe0bc4042002-06-25 01:09:11 +00001121 if( pParse->explain ){
1122 pTrigger = pTrigger->pNext;
1123 }else{
1124 pTrigger = pTable->pTrigger;
1125 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001126 }
drhe0bc4042002-06-25 01:09:11 +00001127 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1128 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001129 if( !pTable->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001130 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001131 }
drhe0bc4042002-06-25 01:09:11 +00001132 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001133 if( !isView ){
1134 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1135 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1136 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1137 }
drh5e00f6c2001-09-13 13:46:56 +00001138 }
drh1c928532002-01-31 15:54:21 +00001139 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001140 }
1141
drhe0bc4042002-06-25 01:09:11 +00001142 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001143 **
1144 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001145 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001146 */
1147 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001148 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001149 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001150 }
drh417be792002-03-03 18:59:40 +00001151 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001152}
1153
1154/*
drh38640e12002-07-05 21:42:36 +00001155** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1156** opcode and adds that P3 string to the most recently inserted instruction
1157** in the virtual machine. The P3 string consists of a single character
1158** for each column in the index pIdx of table pTab. If the column uses
1159** a numeric sort order, then the P3 string character corresponding to
1160** that column is 'n'. If the column uses a text sort order, then the
1161** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1162** additional information. See also the sqliteAddKeyType() routine.
1163*/
1164void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1165 char *zType;
1166 Table *pTab;
1167 int i, n;
1168 assert( pIdx!=0 && pIdx->pTable!=0 );
1169 pTab = pIdx->pTable;
1170 n = pIdx->nColumn;
1171 zType = sqliteMalloc( n+1 );
1172 if( zType==0 ) return;
1173 for(i=0; i<n; i++){
1174 int iCol = pIdx->aiColumn[i];
1175 assert( iCol>=0 && iCol<pTab->nCol );
1176 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1177 zType[i] = 't';
1178 }else{
1179 zType[i] = 'n';
1180 }
1181 }
1182 zType[n] = 0;
1183 sqliteVdbeChangeP3(v, -1, zType, n);
1184 sqliteFree(zType);
1185}
1186
1187/*
drhc2eef3b2002-08-31 18:53:06 +00001188** This routine is called to create a new foreign key on the table
1189** currently under construction. pFromCol determines which columns
1190** in the current table point to the foreign key. If pFromCol==0 then
1191** connect the key to the last column inserted. pTo is the name of
1192** the table referred to. pToCol is a list of tables in the other
1193** pTo table that the foreign key points to. flags contains all
1194** information about the conflict resolution algorithms specified
1195** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1196**
1197** An FKey structure is created and added to the table currently
1198** under construction in the pParse->pNewTable field. The new FKey
1199** is not linked into db->aFKey at this point - that does not happen
1200** until sqliteEndTable().
1201**
1202** The foreign key is set for IMMEDIATE processing. A subsequent call
1203** to sqliteDeferForeignKey() might change this to DEFERRED.
1204*/
1205void sqliteCreateForeignKey(
1206 Parse *pParse, /* Parsing context */
1207 IdList *pFromCol, /* Columns in this table that point to other table */
1208 Token *pTo, /* Name of the other table */
1209 IdList *pToCol, /* Columns in the other table */
1210 int flags /* Conflict resolution algorithms. */
1211){
1212 Table *p = pParse->pNewTable;
1213 int nByte;
1214 int i;
1215 int nCol;
1216 char *z;
1217 FKey *pFKey = 0;
1218
1219 assert( pTo!=0 );
1220 if( p==0 || pParse->nErr ) goto fk_end;
1221 if( pFromCol==0 ){
1222 int iCol = p->nCol-1;
1223 if( iCol<0 ) goto fk_end;
1224 if( pToCol && pToCol->nId!=1 ){
1225 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1226 p->aCol[iCol].zName, -1,
1227 " should reference only one column of table ", -1,
1228 pTo->z, pTo->n, 0);
1229 pParse->nErr++;
1230 goto fk_end;
1231 }
1232 nCol = 1;
1233 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1234 sqliteSetString(&pParse->zErrMsg,
1235 "number of columns in foreign key does not match the number of "
1236 "columns in the referenced table", 0);
1237 pParse->nErr++;
1238 goto fk_end;
1239 }else{
1240 nCol = pFromCol->nId;
1241 }
1242 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1243 if( pToCol ){
1244 for(i=0; i<pToCol->nId; i++){
1245 nByte += strlen(pToCol->a[i].zName) + 1;
1246 }
1247 }
1248 pFKey = sqliteMalloc( nByte );
1249 if( pFKey==0 ) goto fk_end;
1250 pFKey->pFrom = p;
1251 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001252 z = (char*)&pFKey[1];
1253 pFKey->aCol = (struct sColMap*)z;
1254 z += sizeof(struct sColMap)*nCol;
1255 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001256 memcpy(z, pTo->z, pTo->n);
1257 z[pTo->n] = 0;
1258 z += pTo->n+1;
1259 pFKey->pNextTo = 0;
1260 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001261 if( pFromCol==0 ){
1262 pFKey->aCol[0].iFrom = p->nCol-1;
1263 }else{
1264 for(i=0; i<nCol; i++){
1265 int j;
1266 for(j=0; j<p->nCol; j++){
1267 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1268 pFKey->aCol[i].iFrom = j;
1269 break;
1270 }
1271 }
1272 if( j>=p->nCol ){
1273 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1274 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1275 pParse->nErr++;
1276 goto fk_end;
1277 }
1278 }
1279 }
1280 if( pToCol ){
1281 for(i=0; i<nCol; i++){
1282 int n = strlen(pToCol->a[i].zName);
1283 pFKey->aCol[i].zCol = z;
1284 memcpy(z, pToCol->a[i].zName, n);
1285 z[n] = 0;
1286 z += n+1;
1287 }
1288 }
1289 pFKey->isDeferred = 0;
1290 pFKey->deleteConf = flags & 0xff;
1291 pFKey->updateConf = (flags >> 8 ) & 0xff;
1292 pFKey->insertConf = (flags >> 16 ) & 0xff;
1293
1294 /* Link the foreign key to the table as the last step.
1295 */
1296 p->pFKey = pFKey;
1297 pFKey = 0;
1298
1299fk_end:
1300 sqliteFree(pFKey);
1301 sqliteIdListDelete(pFromCol);
1302 sqliteIdListDelete(pToCol);
1303}
1304
1305/*
1306** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1307** clause is seen as part of a foreign key definition. The isDeferred
1308** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1309** The behavior of the most recently created foreign key is adjusted
1310** accordingly.
1311*/
1312void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1313 Table *pTab;
1314 FKey *pFKey;
1315 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1316 pFKey->isDeferred = isDeferred;
1317}
1318
1319/*
drh75897232000-05-29 14:26:00 +00001320** Create a new index for an SQL table. pIndex is the name of the index
1321** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001322** be NULL for a primary key or an index that is created to satisfy a
1323** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001324** as the table to be indexed. pParse->pNewTable is a table that is
1325** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001326**
drh382c0242001-10-06 16:33:02 +00001327** pList is a list of columns to be indexed. pList will be NULL if this
1328** is a primary key or unique-constraint on the most recent column added
1329** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001330*/
1331void sqliteCreateIndex(
1332 Parse *pParse, /* All information about this parse */
1333 Token *pName, /* Name of the index. May be NULL */
1334 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001335 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001336 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001337 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1338 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1339){
1340 Table *pTab; /* Table to be indexed */
1341 Index *pIndex; /* The index to be created */
1342 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001343 int i, j;
drhf57b3392001-10-08 13:22:32 +00001344 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001345 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001346 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001347
drhdaffd0e2001-04-11 14:28:42 +00001348 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1349
drh75897232000-05-29 14:26:00 +00001350 /*
1351 ** Find the table that is to be indexed. Return early if not found.
1352 */
1353 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001354 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001355 pTab = sqliteTableFromToken(pParse, pTable);
1356 }else{
drhe3c41372001-09-17 20:25:58 +00001357 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001358 pTab = pParse->pNewTable;
1359 }
1360 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1361 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001362 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1363 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001364 pParse->nErr++;
1365 goto exit_create_index;
1366 }
drha76b5df2002-02-23 02:32:10 +00001367 if( pTab->pSelect ){
1368 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1369 pParse->nErr++;
1370 goto exit_create_index;
1371 }
drh75897232000-05-29 14:26:00 +00001372
drhf57b3392001-10-08 13:22:32 +00001373 /* If this index is created while re-reading the schema from sqlite_master
1374 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001375 ** only mean that the table that this index is really associated with is
1376 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001377 ** Since its table has been suppressed, we need to also suppress the
1378 ** index.
1379 */
drhe0bc4042002-06-25 01:09:11 +00001380 if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){
drhf57b3392001-10-08 13:22:32 +00001381 goto exit_create_index;
1382 }
1383
drh75897232000-05-29 14:26:00 +00001384 /*
1385 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001386 ** index or table with the same name.
1387 **
1388 ** Exception: If we are reading the names of permanent indices from the
1389 ** sqlite_master table (because some other process changed the schema) and
1390 ** one of the index names collides with the name of a temporary table or
1391 ** index, then we will continue to process this index, but we will not
1392 ** store its name in the hash table. Set the hideName flag to accomplish
1393 ** this.
1394 **
1395 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001396 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1397 ** own name.
drh75897232000-05-29 14:26:00 +00001398 */
1399 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001400 Index *pISameName; /* Another index with the same name */
1401 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001402 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001403 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001404 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1405 if( pISameName->pTable->isTemp && pParse->initFlag ){
1406 hideName = 1;
1407 }else{
1408 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1409 " already exists", 0);
1410 pParse->nErr++;
1411 goto exit_create_index;
1412 }
drhe3c41372001-09-17 20:25:58 +00001413 }
drhf57b3392001-10-08 13:22:32 +00001414 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1415 if( pTSameName->isTemp && pParse->initFlag ){
1416 hideName = 1;
1417 }else{
1418 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1419 zName, 0);
1420 pParse->nErr++;
1421 goto exit_create_index;
1422 }
drhe3c41372001-09-17 20:25:58 +00001423 }
drh75897232000-05-29 14:26:00 +00001424 }else{
drhadbca9c2001-09-27 15:11:53 +00001425 char zBuf[30];
1426 int n;
1427 Index *pLoop;
1428 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1429 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001430 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001431 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001432 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001433 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001434 }
1435
1436 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001437 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001438 ** So create a fake list to simulate this.
1439 */
1440 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001441 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001442 nullId.n = strlen(nullId.z);
1443 pList = sqliteIdListAppend(0, &nullId);
1444 if( pList==0 ) goto exit_create_index;
1445 }
1446
1447 /*
1448 ** Allocate the index structure.
1449 */
drhdcc581c2000-05-30 13:44:19 +00001450 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001451 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001452 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001453 pIndex->aiColumn = (int*)&pIndex[1];
1454 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001455 strcpy(pIndex->zName, zName);
1456 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001457 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001458 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001459 pIndex->autoIndex = pName==0;
drh75897232000-05-29 14:26:00 +00001460
drh1ccde152000-06-17 13:12:39 +00001461 /* Scan the names of the columns of the table to be indexed and
1462 ** load the column indices into the Index structure. Report an error
1463 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001464 */
1465 for(i=0; i<pList->nId; i++){
1466 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001467 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001468 }
1469 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001470 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001471 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001472 pParse->nErr++;
1473 sqliteFree(pIndex);
1474 goto exit_create_index;
1475 }
drh967e8b72000-06-21 13:59:10 +00001476 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001477 }
1478
1479 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001480 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001481 */
drhf57b3392001-10-08 13:22:32 +00001482 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001483 Index *p;
1484 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1485 if( p ){
1486 assert( p==pIndex ); /* Malloc must have failed */
1487 sqliteFree(pIndex);
1488 goto exit_create_index;
1489 }
drh5e00f6c2001-09-13 13:46:56 +00001490 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001491 }
drh9cfcf5d2002-01-29 18:41:24 +00001492
1493 /* When adding an index to the list of indices for a table, make
1494 ** sure all indices labeled OE_Replace come after all those labeled
1495 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1496 ** and INSERT.
1497 */
1498 if( onError!=OE_Replace || pTab->pIndex==0
1499 || pTab->pIndex->onError==OE_Replace){
1500 pIndex->pNext = pTab->pIndex;
1501 pTab->pIndex = pIndex;
1502 }else{
1503 Index *pOther = pTab->pIndex;
1504 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1505 pOther = pOther->pNext;
1506 }
1507 pIndex->pNext = pOther->pNext;
1508 pOther->pNext = pIndex;
1509 }
drh75897232000-05-29 14:26:00 +00001510
drhd78eeee2001-09-13 16:18:53 +00001511 /* If the initFlag is 1 it means we are reading the SQL off the
1512 ** "sqlite_master" table on the disk. So do not write to the disk
1513 ** again. Extract the table number from the pParse->newTnum field.
1514 */
drhadbca9c2001-09-27 15:11:53 +00001515 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001516 pIndex->tnum = pParse->newTnum;
1517 }
1518
drh75897232000-05-29 14:26:00 +00001519 /* If the initFlag is 0 then create the index on disk. This
1520 ** involves writing the index into the master table and filling in the
1521 ** index with the current table contents.
1522 **
1523 ** The initFlag is 0 when the user first enters a CREATE INDEX
1524 ** command. The initFlag is 1 when a database is opened and
1525 ** CREATE INDEX statements are read out of the master table. In
1526 ** the latter case the index already exists on disk, which is why
1527 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001528 **
1529 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001530 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1531 ** has just been created, it contains no data and the index initialization
1532 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001533 */
drhadbca9c2001-09-27 15:11:53 +00001534 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001535 int n;
drhadbca9c2001-09-27 15:11:53 +00001536 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001537 int lbl1, lbl2;
1538 int i;
drhadbca9c2001-09-27 15:11:53 +00001539 int addr;
drhf57b3392001-10-08 13:22:32 +00001540 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001541
drhd8bc7082000-06-07 23:51:50 +00001542 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001543 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001544 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001545 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001546 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001547 }
drhe0bc4042002-06-25 01:09:11 +00001548 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1549 sqliteVdbeAddOp(v, OP_String, 0, 0);
1550 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1551 sqliteVdbeAddOp(v, OP_String, 0, 0);
1552 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1553 sqliteVdbeAddOp(v, OP_String, 0, 0);
1554 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001555 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1556 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001557 pIndex->tnum = 0;
1558 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001559 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001560 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001561 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001562 }else{
drh99fcd712001-10-13 01:06:47 +00001563 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001564 }
drh5e00f6c2001-09-13 13:46:56 +00001565 }
drhe0bc4042002-06-25 01:09:11 +00001566 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1567 if( pStart && pEnd ){
1568 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1569 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001570 }
drhe0bc4042002-06-25 01:09:11 +00001571 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1572 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001573 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001574 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1575 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001576 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001577 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1578 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001579 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001580 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001581 }
drh99fcd712001-10-13 01:06:47 +00001582 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001583 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001584 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001585 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001586 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001587 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1588 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001589 }
drhadbca9c2001-09-27 15:11:53 +00001590 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001591 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001592 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001593 }
drhe0bc4042002-06-25 01:09:11 +00001594 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001595 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001596 }
drh75897232000-05-29 14:26:00 +00001597 }
1598
drh75897232000-05-29 14:26:00 +00001599 /* Clean up before exiting */
1600exit_create_index:
1601 sqliteIdListDelete(pList);
1602 sqliteFree(zName);
1603 return;
1604}
1605
1606/*
drh74e24cd2002-01-09 03:19:59 +00001607** This routine will drop an existing named index. This routine
1608** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001609*/
1610void sqliteDropIndex(Parse *pParse, Token *pName){
1611 Index *pIndex;
1612 char *zName;
1613 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001614 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001615
drhdaffd0e2001-04-11 14:28:42 +00001616 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001617 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001618 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001619 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001620 sqliteFree(zName);
1621 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001622 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1623 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001624 pParse->nErr++;
1625 return;
1626 }
drh485b39b2002-07-13 03:11:52 +00001627 if( pIndex->autoIndex ){
1628 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1629 "or PRIMARY KEY constraint cannot be dropped", 0);
1630 pParse->nErr++;
1631 return;
1632 }
drh75897232000-05-29 14:26:00 +00001633
1634 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001635 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001636 if( v ){
1637 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001638 { OP_Rewind, 0, ADDR(9), 0},
1639 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001640 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001641 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001642 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001643 { OP_Eq, 0, ADDR(8), 0},
1644 { OP_Next, 0, ADDR(3), 0},
1645 { OP_Goto, 0, ADDR(9), 0},
1646 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001647 };
1648 int base;
drhf57b3392001-10-08 13:22:32 +00001649 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001650
drhcabb0812002-09-14 13:47:32 +00001651 sqliteBeginWriteOperation(pParse, 0, pTab->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001652 sqliteOpenMasterTable(v, pTab->isTemp);
1653 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1654 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001655 if( !pTab->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001656 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001657 }
drhe0bc4042002-06-25 01:09:11 +00001658 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001659 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001660 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001661 }
1662
drhe0bc4042002-06-25 01:09:11 +00001663 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001664 */
1665 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001666 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001667 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001668 }
1669}
1670
1671/*
drh75897232000-05-29 14:26:00 +00001672** Append a new element to the given IdList. Create a new IdList if
1673** need be.
drhdaffd0e2001-04-11 14:28:42 +00001674**
1675** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001676*/
1677IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1678 if( pList==0 ){
1679 pList = sqliteMalloc( sizeof(IdList) );
1680 if( pList==0 ) return 0;
1681 }
1682 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001683 struct IdList_item *a;
1684 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1685 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001686 sqliteIdListDelete(pList);
1687 return 0;
drh75897232000-05-29 14:26:00 +00001688 }
drh6d4abfb2001-10-22 02:58:08 +00001689 pList->a = a;
drh75897232000-05-29 14:26:00 +00001690 }
1691 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1692 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001693 char **pz = &pList->a[pList->nId].zName;
1694 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1695 if( *pz==0 ){
1696 sqliteIdListDelete(pList);
1697 return 0;
1698 }else{
1699 sqliteDequote(*pz);
1700 }
drh75897232000-05-29 14:26:00 +00001701 }
1702 pList->nId++;
1703 return pList;
1704}
1705
1706/*
drhad3cab52002-05-24 02:04:32 +00001707** Append a new table name to the given SrcList. Create a new SrcList if
1708** need be. A new entry is created in the SrcList even if pToken is NULL.
1709**
1710** A new SrcList is returned, or NULL if malloc() fails.
1711*/
1712SrcList *sqliteSrcListAppend(SrcList *pList, Token *pToken){
1713 if( pList==0 ){
1714 pList = sqliteMalloc( sizeof(IdList) );
1715 if( pList==0 ) return 0;
1716 }
1717 if( (pList->nSrc & 7)==0 ){
1718 struct SrcList_item *a;
1719 a = sqliteRealloc(pList->a, (pList->nSrc+8)*sizeof(pList->a[0]) );
1720 if( a==0 ){
1721 sqliteSrcListDelete(pList);
1722 return 0;
1723 }
1724 pList->a = a;
1725 }
1726 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
1727 if( pToken ){
1728 char **pz = &pList->a[pList->nSrc].zName;
1729 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1730 if( *pz==0 ){
1731 sqliteSrcListDelete(pList);
1732 return 0;
1733 }else{
1734 sqliteDequote(*pz);
1735 }
1736 }
1737 pList->nSrc++;
1738 return pList;
1739}
1740
1741/*
drh75897232000-05-29 14:26:00 +00001742** Add an alias to the last identifier on the given identifier list.
1743*/
drhad3cab52002-05-24 02:04:32 +00001744void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1745 if( pList && pList->nSrc>0 ){
1746 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001747 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001748 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001749 }
1750}
1751
1752/*
drhad3cab52002-05-24 02:04:32 +00001753** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001754*/
1755void sqliteIdListDelete(IdList *pList){
1756 int i;
1757 if( pList==0 ) return;
1758 for(i=0; i<pList->nId; i++){
1759 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001760 }
1761 sqliteFree(pList->a);
1762 sqliteFree(pList);
1763}
1764
1765/*
drhad2d8302002-05-24 20:31:36 +00001766** Return the index in pList of the identifier named zId. Return -1
1767** if not found.
1768*/
1769int sqliteIdListIndex(IdList *pList, const char *zName){
1770 int i;
1771 if( pList==0 ) return -1;
1772 for(i=0; i<pList->nId; i++){
1773 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1774 }
1775 return -1;
1776}
1777
1778/*
drhad3cab52002-05-24 02:04:32 +00001779** Delete an entire SrcList including all its substructure.
1780*/
1781void sqliteSrcListDelete(SrcList *pList){
1782 int i;
1783 if( pList==0 ) return;
1784 for(i=0; i<pList->nSrc; i++){
1785 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001786 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001787 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001788 sqliteDeleteTable(0, pList->a[i].pTab);
1789 }
drhff78bd22002-02-27 01:47:11 +00001790 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001791 sqliteExprDelete(pList->a[i].pOn);
1792 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001793 }
1794 sqliteFree(pList->a);
1795 sqliteFree(pList);
1796}
1797
drh982cef72000-05-30 16:27:03 +00001798/*
1799** The COPY command is for compatibility with PostgreSQL and specificially
1800** for the ability to read the output of pg_dump. The format is as
1801** follows:
1802**
1803** COPY table FROM file [USING DELIMITERS string]
1804**
1805** "table" is an existing table name. We will read lines of code from
1806** file to fill this table with data. File might be "stdin". The optional
1807** delimiter string identifies the field separators. The default is a tab.
1808*/
1809void sqliteCopy(
1810 Parse *pParse, /* The parser context */
1811 Token *pTableName, /* The name of the table into which we will insert */
1812 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001813 Token *pDelimiter, /* Use this as the field delimiter */
1814 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001815){
1816 Table *pTab;
1817 char *zTab;
drh1c928532002-01-31 15:54:21 +00001818 int i;
drh982cef72000-05-30 16:27:03 +00001819 Vdbe *v;
1820 int addr, end;
1821 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001822 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001823
1824 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001825 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001826 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001827 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001828 if( pTab==0 ) goto copy_cleanup;
drhd8bc7082000-06-07 23:51:50 +00001829 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001830 if( v ){
drhf57b3392001-10-08 13:22:32 +00001831 int openOp;
drhcabb0812002-09-14 13:47:32 +00001832 sqliteBeginWriteOperation(pParse, 1, pTab->isTemp);
drh99fcd712001-10-13 01:06:47 +00001833 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001834 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001835 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001836 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001837 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1838 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001839 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001840 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1841 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001842 }
drhb419a922002-01-30 16:17:23 +00001843 if( db->flags & SQLITE_CountRows ){
1844 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1845 }
drh982cef72000-05-30 16:27:03 +00001846 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001847 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001848 if( pDelimiter ){
1849 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1850 sqliteVdbeDequoteP3(v, addr);
1851 }else{
1852 sqliteVdbeChangeP3(v, addr, "\t", 1);
1853 }
drh8aff1012001-12-22 14:49:24 +00001854 if( pTab->iPKey>=0 ){
1855 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1856 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1857 }else{
1858 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1859 }
drh982cef72000-05-30 16:27:03 +00001860 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001861 if( i==pTab->iPKey ){
1862 /* The integer primary key column is filled with NULL since its
1863 ** value is always pulled from the record number */
1864 sqliteVdbeAddOp(v, OP_String, 0, 0);
1865 }else{
1866 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1867 }
drh982cef72000-05-30 16:27:03 +00001868 }
drhb419a922002-01-30 16:17:23 +00001869 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1870 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1871 if( (db->flags & SQLITE_CountRows)!=0 ){
1872 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001873 }
drh99fcd712001-10-13 01:06:47 +00001874 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1875 sqliteVdbeResolveLabel(v, end);
1876 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001877 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001878 if( db->flags & SQLITE_CountRows ){
1879 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1880 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1881 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1882 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1883 }
drh982cef72000-05-30 16:27:03 +00001884 }
1885
1886copy_cleanup:
1887 return;
1888}
drhdce2cbe2000-05-31 02:27:49 +00001889
1890/*
1891** The non-standard VACUUM command is used to clean up the database,
1892** collapse free space, etc. It is modelled after the VACUUM command
1893** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001894**
drh1bffb9c2002-02-03 17:37:36 +00001895** In version 1.0.x of SQLite, the VACUUM command would call
1896** gdbm_reorganize() on all the database tables. But beginning
1897** with 2.0.0, SQLite no longer uses GDBM so this command has
1898** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001899*/
1900void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001901 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001902}
drhc4a3c772001-04-04 11:48:57 +00001903
1904/*
1905** Begin a transaction
1906*/
drh1c928532002-01-31 15:54:21 +00001907void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001908 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001909
drhc4a3c772001-04-04 11:48:57 +00001910 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001911 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001912 if( db->flags & SQLITE_InTrans ){
1913 pParse->nErr++;
1914 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
1915 "within a transaction", 0);
1916 return;
1917 }
drhcabb0812002-09-14 13:47:32 +00001918 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00001919 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001920 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001921}
1922
1923/*
1924** Commit a transaction
1925*/
1926void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001927 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001928
drhc4a3c772001-04-04 11:48:57 +00001929 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001930 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001931 if( (db->flags & SQLITE_InTrans)==0 ){
1932 pParse->nErr++;
1933 sqliteSetString(&pParse->zErrMsg,
1934 "cannot commit - no transaction is active", 0);
1935 return;
1936 }
drh5e00f6c2001-09-13 13:46:56 +00001937 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001938 sqliteEndWriteOperation(pParse);
1939 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001940}
1941
1942/*
1943** Rollback a transaction
1944*/
1945void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001946 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001947 Vdbe *v;
1948
drhc4a3c772001-04-04 11:48:57 +00001949 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001950 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001951 if( (db->flags & SQLITE_InTrans)==0 ){
1952 pParse->nErr++;
1953 sqliteSetString(&pParse->zErrMsg,
1954 "cannot rollback - no transaction is active", 0);
1955 return;
1956 }
drh5e00f6c2001-09-13 13:46:56 +00001957 v = sqliteGetVdbe(pParse);
1958 if( v ){
drh99fcd712001-10-13 01:06:47 +00001959 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001960 }
drh5e00f6c2001-09-13 13:46:56 +00001961 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001962 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001963}
drhf57b14a2001-09-14 18:54:08 +00001964
1965/*
drh1c928532002-01-31 15:54:21 +00001966** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00001967** might change the database.
1968**
1969** This routine starts a new transaction if we are not already within
1970** a transaction. If we are already within a transaction, then a checkpoint
1971** is set if the setCheckpoint parameter is true. A checkpoint should
1972** be set for operations that might fail (due to a constraint) part of
1973** the way through and which will need to undo some writes without having to
1974** rollback the whole transaction. For operations where all constraints
1975** can be checked before any changes are made to the database, it is never
1976** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00001977**
1978** The tempOnly flag indicates that only temporary tables will be changed
1979** during this write operation. The primary database table is not
1980** write-locked. Only the temporary database file gets a write lock.
1981** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00001982*/
drhcabb0812002-09-14 13:47:32 +00001983void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00001984 Vdbe *v;
1985 v = sqliteGetVdbe(pParse);
1986 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00001987 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00001988 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drhcabb0812002-09-14 13:47:32 +00001989 sqliteVdbeAddOp(v, OP_Transaction, tempOnly, 0);
1990 if( !tempOnly ){
1991 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1992 pParse->schemaVerified = 1;
1993 }
drhc977f7f2002-05-21 11:38:11 +00001994 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00001995 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1996 }
1997}
1998
1999/*
drh1c928532002-01-31 15:54:21 +00002000** Generate code that concludes an operation that may have changed
2001** the database. This is a companion function to BeginWriteOperation().
2002** If a transaction was started, then commit it. If a checkpoint was
2003** started then commit that.
2004*/
2005void sqliteEndWriteOperation(Parse *pParse){
2006 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002007 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002008 v = sqliteGetVdbe(pParse);
2009 if( v==0 ) return;
2010 if( pParse->db->flags & SQLITE_InTrans ){
2011 /* Do Nothing */
2012 }else{
2013 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2014 }
2015}
2016
2017
2018/*
drhf57b14a2001-09-14 18:54:08 +00002019** Interpret the given string as a boolean value.
2020*/
2021static int getBoolean(char *z){
2022 static char *azTrue[] = { "yes", "on", "true" };
2023 int i;
2024 if( z[0]==0 ) return 0;
2025 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2026 return atoi(z);
2027 }
2028 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
2029 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
2030 }
2031 return 0;
2032}
2033
2034/*
2035** Process a pragma statement.
2036**
2037** Pragmas are of this form:
2038**
2039** PRAGMA id = value
2040**
2041** The identifier might also be a string. The value is a string, and
2042** identifier, or a number. If minusFlag is true, then the value is
2043** a number that was preceded by a minus sign.
2044*/
2045void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
2046 char *zLeft = 0;
2047 char *zRight = 0;
2048 sqlite *db = pParse->db;
2049
2050 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
2051 sqliteDequote(zLeft);
2052 if( minusFlag ){
2053 zRight = 0;
2054 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
2055 }else{
2056 zRight = sqliteStrNDup(pRight->z, pRight->n);
2057 sqliteDequote(zRight);
2058 }
2059
drhcd61c282002-03-06 22:01:34 +00002060 /*
2061 ** PRAGMA default_cache_size
2062 ** PRAGMA default_cache_size=N
2063 **
2064 ** The first form reports the current persistent setting for the
2065 ** page cache size. The value returned is the maximum number of
2066 ** pages in the page cache. The second form sets both the current
2067 ** page cache size value and the persistent page cache size value
2068 ** stored in the database file.
2069 **
2070 ** The default cache size is stored in meta-value 2 of page 1 of the
2071 ** database file. The cache size is actually the absolute value of
2072 ** this memory location. The sign of meta-value 2 determines the
2073 ** synchronous setting. A negative value means synchronous is off
2074 ** and a positive value means synchronous is on.
2075 */
2076 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00002077 static VdbeOp getCacheSize[] = {
2078 { OP_ReadCookie, 0, 2, 0},
2079 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00002080 { OP_Dup, 0, 0, 0},
2081 { OP_Integer, 0, 0, 0},
2082 { OP_Ne, 0, 6, 0},
2083 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00002084 { OP_ColumnCount, 1, 0, 0},
2085 { OP_ColumnName, 0, 0, "cache_size"},
2086 { OP_Callback, 1, 0, 0},
2087 };
2088 Vdbe *v = sqliteGetVdbe(pParse);
2089 if( v==0 ) return;
2090 if( pRight->z==pLeft->z ){
2091 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2092 }else{
2093 int addr;
2094 int size = atoi(zRight);
2095 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002096 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002097 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2098 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2099 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2100 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2101 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2102 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2103 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002104 db->cache_size = db->cache_size<0 ? -size : size;
2105 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002106 }
2107 }else
2108
drhcd61c282002-03-06 22:01:34 +00002109 /*
2110 ** PRAGMA cache_size
2111 ** PRAGMA cache_size=N
2112 **
2113 ** The first form reports the current local setting for the
2114 ** page cache size. The local setting can be different from
2115 ** the persistent cache size value that is stored in the database
2116 ** file itself. The value returned is the maximum number of
2117 ** pages in the page cache. The second form sets the local
2118 ** page cache size value. It does not change the persistent
2119 ** cache size stored on the disk so the cache size will revert
2120 ** to its default value when the database is closed and reopened.
2121 ** N should be a positive integer.
2122 */
2123 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2124 static VdbeOp getCacheSize[] = {
2125 { OP_ColumnCount, 1, 0, 0},
2126 { OP_ColumnName, 0, 0, "cache_size"},
2127 { OP_Callback, 1, 0, 0},
2128 };
2129 Vdbe *v = sqliteGetVdbe(pParse);
2130 if( v==0 ) return;
2131 if( pRight->z==pLeft->z ){
2132 int size = db->cache_size;;
2133 if( size<0 ) size = -size;
2134 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2135 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2136 }else{
2137 int size = atoi(zRight);
2138 if( size<0 ) size = -size;
2139 if( db->cache_size<0 ) size = -size;
2140 db->cache_size = size;
2141 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2142 }
2143 }else
2144
2145 /*
2146 ** PRAGMA default_synchronous
2147 ** PRAGMA default_synchronous=BOOLEAN
2148 **
2149 ** The first form returns the persistent value of the "synchronous" setting
2150 ** that is stored in the database. This is the synchronous setting that
2151 ** is used whenever the database is opened unless overridden by a separate
2152 ** "synchronous" pragma. The second form changes the persistent and the
2153 ** local synchronous setting to the value given.
2154 **
2155 ** If synchronous is on, SQLite will do an fsync() system call at strategic
2156 ** points to insure that all previously written data has actually been
2157 ** written onto the disk surface before continuing. This mode insures that
2158 ** the database will always be in a consistent state event if the operating
2159 ** system crashes or power to the computer is interrupted unexpectedly.
2160 ** When synchronous is off, SQLite will not wait for changes to actually
2161 ** be written to the disk before continuing. As soon as it hands changes
2162 ** to the operating system, it assumes that the changes are permanent and
2163 ** it continues going. The database cannot be corrupted by a program crash
2164 ** even with synchronous off, but an operating system crash or power loss
2165 ** could potentially corrupt data. On the other hand, synchronous off is
2166 ** faster than synchronous on.
2167 */
2168 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00002169 static VdbeOp getSync[] = {
2170 { OP_Integer, 0, 0, 0},
2171 { OP_ReadCookie, 0, 2, 0},
2172 { OP_Integer, 0, 0, 0},
2173 { OP_Lt, 0, 5, 0},
2174 { OP_AddImm, 1, 0, 0},
2175 { OP_ColumnCount, 1, 0, 0},
2176 { OP_ColumnName, 0, 0, "synchronous"},
2177 { OP_Callback, 1, 0, 0},
2178 };
2179 Vdbe *v = sqliteGetVdbe(pParse);
2180 if( v==0 ) return;
2181 if( pRight->z==pLeft->z ){
2182 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2183 }else{
2184 int addr;
drhcd61c282002-03-06 22:01:34 +00002185 int size = db->cache_size;
2186 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002187 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002188 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002189 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2190 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2191 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2192 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002193 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
2194 if( !getBoolean(zRight) ){
2195 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002196 size = -size;
drh603240c2002-03-05 01:11:12 +00002197 }
2198 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2199 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002200 db->cache_size = size;
2201 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2202 }
2203 }else
2204
2205 /*
2206 ** PRAGMA synchronous
2207 ** PRAGMA synchronous=BOOLEAN
2208 **
2209 ** Return or set the local value of the synchronous flag. Changing
2210 ** the local value does not make changes to the disk file and the
2211 ** default value will be restored the next time the database is
2212 ** opened.
2213 */
2214 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2215 static VdbeOp getSync[] = {
2216 { OP_ColumnCount, 1, 0, 0},
2217 { OP_ColumnName, 0, 0, "synchronous"},
2218 { OP_Callback, 1, 0, 0},
2219 };
2220 Vdbe *v = sqliteGetVdbe(pParse);
2221 if( v==0 ) return;
2222 if( pRight->z==pLeft->z ){
2223 sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0);
2224 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2225 }else{
2226 int size = db->cache_size;
2227 if( size<0 ) size = -size;
2228 if( !getBoolean(zRight) ) size = -size;
2229 db->cache_size = size;
2230 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002231 }
drhf57b14a2001-09-14 18:54:08 +00002232 }else
2233
danielk1977c3f9bad2002-05-15 08:30:12 +00002234 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2235 if( getBoolean(zRight) ){
2236 always_code_trigger_setup = 1;
2237 }else{
2238 always_code_trigger_setup = 0;
2239 }
2240 }else
2241
drhf57b14a2001-09-14 18:54:08 +00002242 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2243 if( getBoolean(zRight) ){
2244 db->flags |= SQLITE_VdbeTrace;
2245 }else{
2246 db->flags &= ~SQLITE_VdbeTrace;
2247 }
2248 }else
2249
drh382c0242001-10-06 16:33:02 +00002250 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2251 if( getBoolean(zRight) ){
2252 db->flags |= SQLITE_FullColNames;
2253 }else{
2254 db->flags &= ~SQLITE_FullColNames;
2255 }
2256 }else
2257
drh5080aaa2002-07-11 12:18:16 +00002258 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2259 if( getBoolean(zRight) ){
2260 db->flags |= SQLITE_ReportTypes;
2261 }else{
2262 db->flags &= ~SQLITE_ReportTypes;
2263 }
2264 }else
2265
drhc3a64ba2001-11-22 00:01:27 +00002266 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2267 if( getBoolean(zRight) ){
2268 db->flags |= SQLITE_ResultDetails;
2269 }else{
2270 db->flags &= ~SQLITE_ResultDetails;
2271 }
2272 }else
2273
drh1bee3d72001-10-15 00:44:35 +00002274 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2275 if( getBoolean(zRight) ){
2276 db->flags |= SQLITE_CountRows;
2277 }else{
2278 db->flags &= ~SQLITE_CountRows;
2279 }
2280 }else
2281
drh6a535342001-10-19 16:44:56 +00002282 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2283 if( getBoolean(zRight) ){
2284 db->flags |= SQLITE_NullCallback;
2285 }else{
2286 db->flags &= ~SQLITE_NullCallback;
2287 }
2288 }else
2289
drh382c0242001-10-06 16:33:02 +00002290 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2291 Table *pTab;
2292 Vdbe *v;
2293 pTab = sqliteFindTable(db, zRight);
2294 if( pTab ) v = sqliteGetVdbe(pParse);
2295 if( pTab && v ){
2296 static VdbeOp tableInfoPreface[] = {
2297 { OP_ColumnCount, 5, 0, 0},
2298 { OP_ColumnName, 0, 0, "cid"},
2299 { OP_ColumnName, 1, 0, "name"},
2300 { OP_ColumnName, 2, 0, "type"},
2301 { OP_ColumnName, 3, 0, "notnull"},
2302 { OP_ColumnName, 4, 0, "dflt_value"},
2303 };
2304 int i;
2305 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002306 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002307 for(i=0; i<pTab->nCol; i++){
drh3c2007a2002-10-20 16:00:27 +00002308 char *zType;
drh99fcd712001-10-13 01:06:47 +00002309 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2310 sqliteVdbeAddOp(v, OP_String, 0, 0);
2311 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2312 sqliteVdbeAddOp(v, OP_String, 0, 0);
2313 sqliteVdbeChangeP3(v, -1,
drh3c2007a2002-10-20 16:00:27 +00002314 pTab->aCol[i].zType ? pTab->aCol[i].zType : "numeric", P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00002315 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2316 sqliteVdbeAddOp(v, OP_String, 0, 0);
2317 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2318 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002319 }
2320 }
2321 }else
2322
2323 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2324 Index *pIdx;
2325 Table *pTab;
2326 Vdbe *v;
2327 pIdx = sqliteFindIndex(db, zRight);
2328 if( pIdx ) v = sqliteGetVdbe(pParse);
2329 if( pIdx && v ){
2330 static VdbeOp tableInfoPreface[] = {
2331 { OP_ColumnCount, 3, 0, 0},
2332 { OP_ColumnName, 0, 0, "seqno"},
2333 { OP_ColumnName, 1, 0, "cid"},
2334 { OP_ColumnName, 2, 0, "name"},
2335 };
2336 int i;
2337 pTab = pIdx->pTable;
2338 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2339 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002340 int cnum = pIdx->aiColumn[i];
2341 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2342 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2343 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002344 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002345 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2346 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002347 }
2348 }
2349 }else
2350
drh81a20f22001-10-12 17:30:04 +00002351 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2352 Index *pIdx;
2353 Table *pTab;
2354 Vdbe *v;
2355 pTab = sqliteFindTable(db, zRight);
2356 if( pTab ){
2357 v = sqliteGetVdbe(pParse);
2358 pIdx = pTab->pIndex;
2359 }
2360 if( pTab && pIdx && v ){
2361 int i = 0;
2362 static VdbeOp indexListPreface[] = {
2363 { OP_ColumnCount, 3, 0, 0},
2364 { OP_ColumnName, 0, 0, "seq"},
2365 { OP_ColumnName, 1, 0, "name"},
2366 { OP_ColumnName, 2, 0, "unique"},
2367 };
2368
2369 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2370 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002371 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2372 sqliteVdbeAddOp(v, OP_String, 0, 0);
2373 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002374 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002375 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002376 ++i;
2377 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002378 }
2379 }
2380 }else
2381
drhf57b14a2001-09-14 18:54:08 +00002382#ifndef NDEBUG
2383 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2384 extern void sqliteParserTrace(FILE*, char *);
2385 if( getBoolean(zRight) ){
2386 sqliteParserTrace(stdout, "parser: ");
2387 }else{
2388 sqliteParserTrace(0, 0);
2389 }
2390 }else
2391#endif
2392
drhaaab5722002-02-19 13:39:21 +00002393 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002394 static VdbeOp checkDb[] = {
2395 { OP_SetInsert, 0, 0, "2"},
2396 { OP_Open, 0, 2, 0},
2397 { OP_Rewind, 0, 6, 0},
drh21504322002-06-25 13:16:02 +00002398 { OP_Column, 0, 3, 0}, /* 3 */
drh1bffb9c2002-02-03 17:37:36 +00002399 { OP_SetInsert, 0, 0, 0},
2400 { OP_Next, 0, 3, 0},
drh21504322002-06-25 13:16:02 +00002401 { OP_IntegrityCk, 0, 0, 0}, /* 6 */
drh1bffb9c2002-02-03 17:37:36 +00002402 { OP_ColumnCount, 1, 0, 0},
drh4ff6dfa2002-03-03 23:06:00 +00002403 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002404 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002405 { OP_SetInsert, 1, 0, "2"},
2406 { OP_OpenAux, 1, 2, 0},
2407 { OP_Rewind, 1, 16, 0},
2408 { OP_Column, 1, 3, 0}, /* 13 */
2409 { OP_SetInsert, 1, 0, 0},
2410 { OP_Next, 1, 13, 0},
2411 { OP_IntegrityCk, 1, 1, 0}, /* 16 */
2412 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002413 };
2414 Vdbe *v = sqliteGetVdbe(pParse);
2415 if( v==0 ) return;
2416 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2417 }else
drh1bffb9c2002-02-03 17:37:36 +00002418
drhf57b3392001-10-08 13:22:32 +00002419 {}
2420 sqliteFree(zLeft);
2421 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002422}