blob: 0a0d761194a7cb7477e41e8b4b1d4b244dcaec90 [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**
drhdf68f6b2002-09-21 15:57:57 +000028** $Id: build.c,v 1.113 2002/09/21 15:57:57 drh Exp $
drh75897232000-05-29 14:26:00 +000029*/
30#include "sqliteInt.h"
drhf57b14a2001-09-14 18:54:08 +000031#include <ctype.h>
drh75897232000-05-29 14:26:00 +000032
33/*
drhe0bc4042002-06-25 01:09:11 +000034** This routine is called when a new SQL statement is beginning to
35** be parsed. Check to see if the schema for the database needs
36** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
37** If it does, then read it.
38*/
39void sqliteBeginParse(Parse *pParse, int explainFlag){
40 sqlite *db = pParse->db;
41 pParse->explain = explainFlag;
42 if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){
43 int rc = sqliteInit(db, &pParse->zErrMsg);
44 if( rc!=SQLITE_OK ){
45 pParse->rc = rc;
46 pParse->nErr++;
47 }
48 }
49}
50
51/*
drh75897232000-05-29 14:26:00 +000052** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000053** parsed and we want to execute the VDBE code to implement
54** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000055** constructed VDBE code to do the work of the SQL statement.
56** This routine just has to execute the VDBE code.
57**
58** Note that if an error occurred, it might be the case that
59** no VDBE code was generated.
60*/
61void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000062 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000063 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000064 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000065 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000066 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000067 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
68 &pParse->zErrMsg);
drhe0bc4042002-06-25 01:09:11 +000069 db->next_cookie = db->schema_cookie;
drh75897232000-05-29 14:26:00 +000070 }else{
drh3fc190c2001-09-14 03:24:23 +000071 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000072 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000073 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000074 &pParse->zErrMsg, db->pBusyArg,
75 db->xBusyCallback);
drhecdc7532001-09-23 02:35:53 +000076 if( rc ) pParse->nErr++;
drh75897232000-05-29 14:26:00 +000077 }
78 sqliteVdbeDelete(pParse->pVdbe);
79 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000080 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000081 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000082 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000083 }
84}
85
86/*
drhf57b3392001-10-08 13:22:32 +000087** Locate the in-memory structure that describes
88** a particular database table given the name
drh75897232000-05-29 14:26:00 +000089** of that table. Return NULL if not found.
90*/
drha76b5df2002-02-23 02:32:10 +000091Table *sqliteFindTable(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +000092 Table *p;
93 p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +000094 return p;
drh75897232000-05-29 14:26:00 +000095}
96
97/*
drhf57b3392001-10-08 13:22:32 +000098** Locate the in-memory structure that describes
99** a particular index given the name of that index.
100** Return NULL if not found.
drh75897232000-05-29 14:26:00 +0000101*/
drha76b5df2002-02-23 02:32:10 +0000102Index *sqliteFindIndex(sqlite *db, const char *zName){
drhe0bc4042002-06-25 01:09:11 +0000103 Index *p;
104 p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +0000105 return p;
drh75897232000-05-29 14:26:00 +0000106}
107
108/*
109** Remove the given index from the index hash table, and free
110** its memory structures.
111**
drhd229ca92002-01-09 13:30:41 +0000112** The index is removed from the database hash tables but
113** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +0000114** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +0000115*/
drh74e24cd2002-01-09 03:19:59 +0000116static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +0000117 Index *pOld;
118 assert( db!=0 && p->zName!=0 );
119 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
120 if( pOld!=0 && pOld!=p ){
121 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000122 }
drh74e24cd2002-01-09 03:19:59 +0000123 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000124}
125
126/*
drhbeae3192001-09-22 18:12:08 +0000127** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000128** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000129** structures.
130*/
drh6d4abfb2001-10-22 02:58:08 +0000131void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000132 if( pIndex->pTable->pIndex==pIndex ){
133 pIndex->pTable->pIndex = pIndex->pNext;
134 }else{
135 Index *p;
136 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
137 if( p && p->pNext==pIndex ){
138 p->pNext = pIndex->pNext;
139 }
140 }
141 sqliteDeleteIndex(db, pIndex);
142}
143
144/*
drhe0bc4042002-06-25 01:09:11 +0000145** Erase all schema information from the in-memory hash tables of
146** database connection. This routine is called to reclaim memory
147** before the connection closes. It is also called during a rollback
148** if there were schema changes during the transaction.
drh74e24cd2002-01-09 03:19:59 +0000149*/
drhe0bc4042002-06-25 01:09:11 +0000150void sqliteResetInternalSchema(sqlite *db){
151 HashElem *pElem;
152 Hash temp1;
153 Hash temp2;
154
drhc2eef3b2002-08-31 18:53:06 +0000155 sqliteHashClear(&db->aFKey);
drhe0bc4042002-06-25 01:09:11 +0000156 temp1 = db->tblHash;
157 temp2 = db->trigHash;
158 sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
159 sqliteHashClear(&db->idxHash);
160 for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
161 Trigger *pTrigger = sqliteHashData(pElem);
162 sqliteDeleteTrigger(pTrigger);
drh74e24cd2002-01-09 03:19:59 +0000163 }
drhe0bc4042002-06-25 01:09:11 +0000164 sqliteHashClear(&temp2);
165 sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
166 for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
167 Table *pTab = sqliteHashData(pElem);
168 sqliteDeleteTable(db, pTab);
169 }
170 sqliteHashClear(&temp1);
171 db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges);
172}
173
174/*
175** This routine is called whenever a rollback occurs. If there were
176** schema changes during the transaction, then we have to reset the
177** internal hash tables and reload them from disk.
178*/
179void sqliteRollbackInternalChanges(sqlite *db){
180 if( db->flags & SQLITE_InternChanges ){
181 sqliteResetInternalSchema(db);
182 }
183}
184
185/*
186** This routine is called when a commit occurs.
187*/
188void sqliteCommitInternalChanges(sqlite *db){
189 db->schema_cookie = db->next_cookie;
190 db->flags &= ~SQLITE_InternChanges;
drh74e24cd2002-01-09 03:19:59 +0000191}
192
193/*
drh75897232000-05-29 14:26:00 +0000194** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000195** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000196**
197** This routine just deletes the data structure. It does not unlink
drhc2eef3b2002-08-31 18:53:06 +0000198** the table data structure from the hash table. Nor does it remove
199** foreign keys from the sqlite.aFKey hash table. But it does destroy
200** memory structures of the indices and foreign keys associated with
201** the table.
drhdaffd0e2001-04-11 14:28:42 +0000202**
203** Indices associated with the table are unlinked from the "db"
204** data structure if db!=NULL. If db==NULL, indices attached to
205** the table are deleted, but it is assumed they have already been
206** unlinked.
drh75897232000-05-29 14:26:00 +0000207*/
208void sqliteDeleteTable(sqlite *db, Table *pTable){
209 int i;
210 Index *pIndex, *pNext;
drhc2eef3b2002-08-31 18:53:06 +0000211 FKey *pFKey, *pNextFKey;
212
drh75897232000-05-29 14:26:00 +0000213 if( pTable==0 ) return;
drhc2eef3b2002-08-31 18:53:06 +0000214
215 /* Delete all indices associated with this table
216 */
217 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
218 pNext = pIndex->pNext;
219 sqliteDeleteIndex(db, pIndex);
220 }
221
222 /* Delete all foreign keys associated with this table. The keys
223 ** should have already been unlinked from the db->aFKey hash table
224 */
225 for(pFKey=pTable->pFKey; pFKey; pFKey=pNextFKey){
226 pNextFKey = pFKey->pNextFrom;
227 assert( sqliteHashFind(&db->aFKey,pFKey->zTo,strlen(pFKey->zTo)+1)!=pFKey );
228 sqliteFree(pFKey);
229 }
230
231 /* Delete the Table structure itself.
232 */
drh75897232000-05-29 14:26:00 +0000233 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000234 sqliteFree(pTable->aCol[i].zName);
235 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000236 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000237 }
drh6e142f52000-06-08 13:36:40 +0000238 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000239 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000240 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000241 sqliteFree(pTable);
242}
243
244/*
drh5edc3122001-09-13 21:53:09 +0000245** Unlink the given table from the hash tables and the delete the
drhc2eef3b2002-08-31 18:53:06 +0000246** table structure with all its indices and foreign keys.
drh5edc3122001-09-13 21:53:09 +0000247*/
drh74e24cd2002-01-09 03:19:59 +0000248static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000249 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000250 FKey *pF1, *pF2;
drhd229ca92002-01-09 13:30:41 +0000251 assert( db!=0 );
252 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
253 assert( pOld==0 || pOld==p );
drhc2eef3b2002-08-31 18:53:06 +0000254 for(pF1=p->pFKey; pF1; pF1=pF1->pNextFrom){
255 int nTo = strlen(pF1->zTo) + 1;
256 pF2 = sqliteHashFind(&db->aFKey, pF1->zTo, nTo);
257 if( pF2==pF1 ){
258 sqliteHashInsert(&db->aFKey, pF1->zTo, nTo, pF1->pNextTo);
259 }else{
260 while( pF2 && pF2->pNextTo!=pF1 ){ pF2=pF2->pNextTo; }
261 if( pF2 ){
262 pF2->pNextTo = pF1->pNextTo;
263 }
264 }
265 }
drh74e24cd2002-01-09 03:19:59 +0000266 sqliteDeleteTable(db, p);
267}
268
269/*
drh1ccde152000-06-17 13:12:39 +0000270** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000271**
272** Space to hold the name is obtained from sqliteMalloc() and must
273** be freed by the calling function.
274*/
drhcce7d172000-05-31 15:34:51 +0000275char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000276 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000277 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000278 return zName;
279}
280
281/*
drhe0bc4042002-06-25 01:09:11 +0000282** Generate code to open the appropriate master table. The table
283** opened will be SQLITE_MASTER for persistent tables and
284** SQLITE_TEMP_MASTER for temporary tables. The table is opened
285** on cursor 0.
286*/
287void sqliteOpenMasterTable(Vdbe *v, int isTemp){
288 if( isTemp ){
289 sqliteVdbeAddOp(v, OP_OpenWrAux, 0, 2);
290 sqliteVdbeChangeP3(v, -1, TEMP_MASTER_NAME, P3_STATIC);
291 }else{
292 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
293 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
294 }
295}
296
297/*
drh75897232000-05-29 14:26:00 +0000298** Begin constructing a new table representation in memory. This is
299** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000300** to a CREATE TABLE statement. In particular, this routine is called
301** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000302** pStart token is the CREATE and pName is the table name. The isTemp
drhe0bc4042002-06-25 01:09:11 +0000303** flag is true if the table should be stored in the auxiliary database
304** file instead of in the main database file. This is normally the case
305** when the "TEMP" or "TEMPORARY" keyword occurs in between
drhf57b3392001-10-08 13:22:32 +0000306** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000307**
drhf57b3392001-10-08 13:22:32 +0000308** The new table record is initialized and put in pParse->pNewTable.
309** As more of the CREATE TABLE statement is parsed, additional action
310** routines will be called to add more information to this record.
311** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
312** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000313*/
drhf57b3392001-10-08 13:22:32 +0000314void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){
drh75897232000-05-29 14:26:00 +0000315 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000316 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000317 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000318 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000319 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000320
321 pParse->sFirstToken = *pStart;
322 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000323 if( zName==0 ) return;
drhf57b3392001-10-08 13:22:32 +0000324
325 /* Before trying to create a temporary table, make sure the Btree for
326 ** holding temporary tables is open.
327 */
328 if( isTemp && db->pBeTemp==0 ){
329 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
330 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000331 sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
drhf57b3392001-10-08 13:22:32 +0000332 "file for storing temporary tables", 0);
333 pParse->nErr++;
334 return;
335 }
336 if( db->flags & SQLITE_InTrans ){
337 rc = sqliteBtreeBeginTrans(db->pBeTemp);
338 if( rc!=SQLITE_OK ){
339 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000340 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000341 pParse->nErr++;
342 return;
343 }
344 }
345 }
346
347 /* Make sure the new table name does not collide with an existing
348 ** index or table name. Issue an error message if it does.
349 **
350 ** If we are re-reading the sqlite_master table because of a schema
351 ** change and a new permanent table is found whose name collides with
352 ** an existing temporary table, then ignore the new permanent table.
353 ** We will continue parsing, but the pParse->nameClash flag will be set
354 ** so we will know to discard the table record once parsing has finished.
355 */
drhbe0072d2001-09-13 14:46:09 +0000356 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000357 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000358 if( pTable->isTemp && pParse->initFlag ){
359 pParse->nameClash = 1;
360 }else{
361 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
362 " already exists", 0, 0);
363 sqliteFree(zName);
364 pParse->nErr++;
365 return;
366 }
367 }else{
368 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000369 }
drhf57b3392001-10-08 13:22:32 +0000370 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
371 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000372 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
373 zName, 0);
drh75897232000-05-29 14:26:00 +0000374 sqliteFree(zName);
375 pParse->nErr++;
376 return;
377 }
378 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000379 if( pTable==0 ){
380 sqliteFree(zName);
381 return;
382 }
drh75897232000-05-29 14:26:00 +0000383 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000384 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000385 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000386 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000387 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000388 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000389 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000390 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000391
392 /* Begin generating the code that will insert the table record into
393 ** the SQLITE_MASTER table. Note in particular that we must go ahead
394 ** and allocate the record number for the table entry now. Before any
395 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
396 ** indices to be created and the table record must come before the
397 ** indices. Hence, the record number for the table must be allocated
398 ** now.
399 */
drhadbca9c2001-09-27 15:11:53 +0000400 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drhcabb0812002-09-14 13:47:32 +0000401 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhf57b3392001-10-08 13:22:32 +0000402 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000403 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
404 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drhf57b3392001-10-08 13:22:32 +0000405 }
drhe0bc4042002-06-25 01:09:11 +0000406 sqliteOpenMasterTable(v, isTemp);
407 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
408 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
409 sqliteVdbeAddOp(v, OP_String, 0, 0);
410 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +0000411 }
drh75897232000-05-29 14:26:00 +0000412}
413
414/*
415** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000416**
417** The parser calls this routine once for each column declaration
418** in a CREATE TABLE statement. sqliteStartTable() gets called
419** first to get things going. Then this routine is called for each
420** column.
drh75897232000-05-29 14:26:00 +0000421*/
422void sqliteAddColumn(Parse *pParse, Token *pName){
423 Table *p;
drh97fc3d02002-05-22 21:27:03 +0000424 int i;
425 char *z = 0;
drhc9b84a12002-06-20 11:36:48 +0000426 Column *pCol;
drh75897232000-05-29 14:26:00 +0000427 if( (p = pParse->pNewTable)==0 ) return;
drh97fc3d02002-05-22 21:27:03 +0000428 sqliteSetNString(&z, pName->z, pName->n, 0);
429 if( z==0 ) return;
430 sqliteDequote(z);
431 for(i=0; i<p->nCol; i++){
432 if( sqliteStrICmp(z, p->aCol[i].zName)==0 ){
433 sqliteSetString(&pParse->zErrMsg, "duplicate column name: ", z, 0);
434 pParse->nErr++;
435 sqliteFree(z);
436 return;
437 }
438 }
drh75897232000-05-29 14:26:00 +0000439 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000440 Column *aNew;
441 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
442 if( aNew==0 ) return;
443 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000444 }
drhc9b84a12002-06-20 11:36:48 +0000445 pCol = &p->aCol[p->nCol];
446 memset(pCol, 0, sizeof(p->aCol[0]));
447 pCol->zName = z;
448 pCol->sortOrder = SQLITE_SO_NUM;
449 p->nCol++;
drh75897232000-05-29 14:26:00 +0000450}
451
452/*
drh382c0242001-10-06 16:33:02 +0000453** This routine is called by the parser while in the middle of
454** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
455** been seen on a column. This routine sets the notNull flag on
456** the column currently under construction.
457*/
drh9cfcf5d2002-01-29 18:41:24 +0000458void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000459 Table *p;
460 int i;
461 if( (p = pParse->pNewTable)==0 ) return;
462 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000463 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000464}
465
466/*
467** This routine is called by the parser while in the middle of
468** parsing a CREATE TABLE statement. The pFirst token is the first
469** token in the sequence of tokens that describe the type of the
470** column currently under construction. pLast is the last token
471** in the sequence. Use this information to construct a string
472** that contains the typename of the column and store that string
473** in zType.
474*/
475void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
476 Table *p;
477 int i, j;
478 int n;
479 char *z, **pz;
drhc9b84a12002-06-20 11:36:48 +0000480 Column *pCol;
drh382c0242001-10-06 16:33:02 +0000481 if( (p = pParse->pNewTable)==0 ) return;
482 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000483 if( i<0 ) return;
drhc9b84a12002-06-20 11:36:48 +0000484 pCol = &p->aCol[i];
485 pz = &pCol->zType;
drh5a2c2c22001-11-21 02:21:11 +0000486 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000487 sqliteSetNString(pz, pFirst->z, n, 0);
488 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000489 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000490 for(i=j=0; z[i]; i++){
491 int c = z[i];
492 if( isspace(c) ) continue;
493 z[j++] = c;
494 }
495 z[j] = 0;
drhc9b84a12002-06-20 11:36:48 +0000496 pCol->sortOrder = SQLITE_SO_NUM;
drh3d037a92002-08-15 01:26:09 +0000497 if( pParse->db->file_format>=4 ){
498 for(i=0; z[i]; i++){
499 switch( z[i] ){
500 case 'b':
501 case 'B': {
502 if( sqliteStrNICmp(&z[i],"blob",4)==0 ){
503 pCol->sortOrder = SQLITE_SO_TEXT;
504 return;
505 }
506 break;
drh38640e12002-07-05 21:42:36 +0000507 }
drh3d037a92002-08-15 01:26:09 +0000508 case 'c':
509 case 'C': {
510 if( sqliteStrNICmp(&z[i],"char",4)==0 ||
511 sqliteStrNICmp(&z[i],"clob",4)==0 ){
512 pCol->sortOrder = SQLITE_SO_TEXT;
513 return;
514 }
515 break;
drhc9b84a12002-06-20 11:36:48 +0000516 }
drh3d037a92002-08-15 01:26:09 +0000517 case 'x':
518 case 'X': {
519 if( i>=2 && sqliteStrNICmp(&z[i-2],"text",4)==0 ){
520 pCol->sortOrder = SQLITE_SO_TEXT;
521 return;
522 }
523 break;
drhc9b84a12002-06-20 11:36:48 +0000524 }
drh3d037a92002-08-15 01:26:09 +0000525 default: {
526 break;
527 }
drhc9b84a12002-06-20 11:36:48 +0000528 }
529 }
530 }
drh382c0242001-10-06 16:33:02 +0000531}
532
533/*
drh7020f652000-06-03 18:06:52 +0000534** The given token is the default value for the last column added to
535** the table currently under construction. If "minusFlag" is true, it
536** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000537**
538** This routine is called by the parser while in the middle of
539** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000540*/
541void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
542 Table *p;
543 int i;
544 char **pz;
545 if( (p = pParse->pNewTable)==0 ) return;
546 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000547 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000548 pz = &p->aCol[i].zDflt;
549 if( minusFlag ){
550 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
551 }else{
552 sqliteSetNString(pz, pVal->z, pVal->n, 0);
553 }
554 sqliteDequote(*pz);
555}
556
557/*
drh4a324312001-12-21 14:30:42 +0000558** Designate the PRIMARY KEY for the table. pList is a list of names
559** of columns that form the primary key. If pList is NULL, then the
560** most recently added column of the table is the primary key.
561**
562** A table can have at most one primary key. If the table already has
563** a primary key (and this is the second primary key) then create an
564** error.
565**
566** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
567** then we will try to use that column as the row id. (Exception:
568** For backwards compatibility with older databases, do not do this
569** if the file format version number is less than 1.) Set the Table.iPKey
570** field of the table under construction to be the index of the
571** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
572** no INTEGER PRIMARY KEY.
573**
574** If the key is not an INTEGER PRIMARY KEY, then create a unique
575** index for the key. No index is created for INTEGER PRIMARY KEYs.
576*/
drh9cfcf5d2002-01-29 18:41:24 +0000577void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000578 Table *pTab = pParse->pNewTable;
579 char *zType = 0;
580 int iCol = -1;
581 if( pTab==0 ) return;
582 if( pTab->hasPrimKey ){
583 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
584 "\" has more than one primary key", 0);
585 pParse->nErr++;
586 return;
587 }
588 pTab->hasPrimKey = 1;
589 if( pList==0 ){
590 iCol = pTab->nCol - 1;
591 }else if( pList->nId==1 ){
592 for(iCol=0; iCol<pTab->nCol; iCol++){
593 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
594 }
595 }
596 if( iCol>=0 && iCol<pTab->nCol ){
597 zType = pTab->aCol[iCol].zType;
598 }
599 if( pParse->db->file_format>=1 &&
600 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
601 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000602 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000603 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000604 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drh4a324312001-12-21 14:30:42 +0000605 }
606}
607
608/*
drh8e2ca022002-06-17 17:07:19 +0000609** Return the appropriate collating type given the collation type token.
610** Report an error if the type is undefined.
611*/
612int sqliteCollateType(Parse *pParse, Token *pType){
613 if( pType==0 ) return SQLITE_SO_UNK;
614 if( pType->n==4 && sqliteStrNICmp(pType->z, "text", 4)==0 ){
615 return SQLITE_SO_TEXT;
616 }
617 if( pType->n==7 && sqliteStrNICmp(pType->z, "numeric", 7)==0 ){
618 return SQLITE_SO_NUM;
619 }
620 sqliteSetNString(&pParse->zErrMsg, "unknown collating type: ", -1,
621 pType->z, pType->n, 0);
622 pParse->nErr++;
623 return SQLITE_SO_UNK;
624}
625
626/*
627** This routine is called by the parser while in the middle of
628** parsing a CREATE TABLE statement. A "COLLATE" clause has
629** been seen on a column. This routine sets the Column.sortOrder on
630** the column currently under construction.
631*/
632void sqliteAddCollateType(Parse *pParse, int collType){
633 Table *p;
634 int i;
635 if( (p = pParse->pNewTable)==0 ) return;
636 i = p->nCol-1;
637 if( i>=0 ) p->aCol[i].sortOrder = collType;
638}
639
640/*
drh50e5dad2001-09-15 00:57:28 +0000641** Come up with a new random value for the schema cookie. Make sure
642** the new value is different from the old.
643**
644** The schema cookie is used to determine when the schema for the
645** database changes. After each schema change, the cookie value
646** changes. When a process first reads the schema it records the
647** cookie. Thereafter, whenever it goes to access the database,
648** it checks the cookie to make sure the schema has not changed
649** since it was last read.
650**
651** This plan is not completely bullet-proof. It is possible for
652** the schema to change multiple times and for the cookie to be
653** set back to prior value. But schema changes are infrequent
654** and the probability of hitting the same cookie value is only
655** 1 chance in 2^32. So we're safe enough.
656*/
drhe0bc4042002-06-25 01:09:11 +0000657void sqliteChangeCookie(sqlite *db, Vdbe *v){
drh50e5dad2001-09-15 00:57:28 +0000658 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000659 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000660 db->flags |= SQLITE_InternChanges;
drhe0bc4042002-06-25 01:09:11 +0000661 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
662 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh50e5dad2001-09-15 00:57:28 +0000663 }
664}
665
666/*
drh969fa7c2002-02-18 18:30:32 +0000667** Measure the number of characters needed to output the given
668** identifier. The number returned includes any quotes used
669** but does not include the null terminator.
670*/
671static int identLength(const char *z){
672 int n;
drh17f71932002-02-21 12:01:27 +0000673 int needQuote = 0;
674 for(n=0; *z; n++, z++){
675 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000676 }
drh17f71932002-02-21 12:01:27 +0000677 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000678}
679
680/*
681** Write an identifier onto the end of the given string. Add
682** quote characters as needed.
683*/
684static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000685 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000686 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000687 for(j=0; zIdent[j]; j++){
688 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
689 }
690 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
691 || sqliteKeywordCode(zIdent, j)!=TK_ID;
692 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000693 for(j=0; zIdent[j]; j++){
694 z[i++] = zIdent[j];
695 if( zIdent[j]=='\'' ) z[i++] = '\'';
696 }
drh17f71932002-02-21 12:01:27 +0000697 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000698 z[i] = 0;
699 *pIdx = i;
700}
701
702/*
703** Generate a CREATE TABLE statement appropriate for the given
704** table. Memory to hold the text of the statement is obtained
705** from sqliteMalloc() and must be freed by the calling function.
706*/
707static char *createTableStmt(Table *p){
708 int i, k, n;
709 char *zStmt;
710 char *zSep, *zSep2, *zEnd;
711 n = 0;
712 for(i=0; i<p->nCol; i++){
713 n += identLength(p->aCol[i].zName);
714 }
715 n += identLength(p->zName);
716 if( n<40 ){
717 zSep = "";
718 zSep2 = ",";
719 zEnd = ")";
720 }else{
721 zSep = "\n ";
722 zSep2 = ",\n ";
723 zEnd = "\n)";
724 }
drhe0bc4042002-06-25 01:09:11 +0000725 n += 35 + 6*p->nCol;
drh969fa7c2002-02-18 18:30:32 +0000726 zStmt = sqliteMalloc( n );
727 if( zStmt==0 ) return 0;
drhe0bc4042002-06-25 01:09:11 +0000728 strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE ");
drh969fa7c2002-02-18 18:30:32 +0000729 k = strlen(zStmt);
730 identPut(zStmt, &k, p->zName);
731 zStmt[k++] = '(';
732 for(i=0; i<p->nCol; i++){
733 strcpy(&zStmt[k], zSep);
734 k += strlen(&zStmt[k]);
735 zSep = zSep2;
736 identPut(zStmt, &k, p->aCol[i].zName);
737 }
738 strcpy(&zStmt[k], zEnd);
739 return zStmt;
740}
741
742/*
drh75897232000-05-29 14:26:00 +0000743** This routine is called to report the final ")" that terminates
744** a CREATE TABLE statement.
745**
drhf57b3392001-10-08 13:22:32 +0000746** The table structure that other action routines have been building
747** is added to the internal hash tables, assuming no errors have
748** occurred.
drh75897232000-05-29 14:26:00 +0000749**
drh1ccde152000-06-17 13:12:39 +0000750** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000751** unless this is a temporary table or initFlag==1. When initFlag==1,
752** it means we are reading the sqlite_master table because we just
753** connected to the database or because the sqlite_master table has
754** recently changes, so the entry for this table already exists in
755** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000756**
757** If the pSelect argument is not NULL, it means that this routine
758** was called to create a table generated from a
759** "CREATE TABLE ... AS SELECT ..." statement. The column names of
760** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000761*/
drh969fa7c2002-02-18 18:30:32 +0000762void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000763 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000764 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000765
drh969fa7c2002-02-18 18:30:32 +0000766 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000767 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000768 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000769
drhf57b3392001-10-08 13:22:32 +0000770 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000771 */
drhad75e982001-10-09 04:19:46 +0000772 assert( pParse->nameClash==0 || pParse->initFlag==1 );
drhf57b3392001-10-08 13:22:32 +0000773 if( pParse->explain==0 && pParse->nameClash==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000774 Table *pOld;
drhc2eef3b2002-08-31 18:53:06 +0000775 FKey *pFKey;
drh6d4abfb2001-10-22 02:58:08 +0000776 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
777 if( pOld ){
drh74e24cd2002-01-09 03:19:59 +0000778 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh6d4abfb2001-10-22 02:58:08 +0000779 return;
780 }
drhc2eef3b2002-08-31 18:53:06 +0000781 for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){
782 int nTo = strlen(pFKey->zTo) + 1;
783 pFKey->pNextTo = sqliteHashFind(&db->aFKey, pFKey->zTo, nTo);
784 sqliteHashInsert(&db->aFKey, pFKey->zTo, nTo, pFKey);
785 }
drh75897232000-05-29 14:26:00 +0000786 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000787 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000788 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000789 }
790
drh969fa7c2002-02-18 18:30:32 +0000791 /* If the table is generated from a SELECT, then construct the
792 ** list of columns and the text of the table.
793 */
794 if( pSelect ){
795 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000796 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000797 assert( p->aCol==0 );
798 p->nCol = pSelTab->nCol;
799 p->aCol = pSelTab->aCol;
800 pSelTab->nCol = 0;
801 pSelTab->aCol = 0;
802 sqliteDeleteTable(0, pSelTab);
803 }
804
drhd78eeee2001-09-13 16:18:53 +0000805 /* If the initFlag is 1 it means we are reading the SQL off the
drhe0bc4042002-06-25 01:09:11 +0000806 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
807 ** So do not write to the disk again. Extract the root page number
808 ** for the table from the pParse->newTnum field. (The page number
809 ** should have been put there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000810 */
811 if( pParse->initFlag ){
812 p->tnum = pParse->newTnum;
813 }
814
drhe3c41372001-09-17 20:25:58 +0000815 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000816 ** in the SQLITE_MASTER table of the database. The record number
817 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000818 **
drhe0bc4042002-06-25 01:09:11 +0000819 ** If this is a TEMPORARY table, write the entry into the auxiliary
820 ** file instead of into the main database file.
drh75897232000-05-29 14:26:00 +0000821 */
822 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000823 int n;
drhd8bc7082000-06-07 23:51:50 +0000824 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000825
drhd8bc7082000-06-07 23:51:50 +0000826 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000827 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000828 if( p->pSelect==0 ){
829 /* A regular table */
830 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
831 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
832 }else{
833 /* A view */
834 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
835 }
drh969fa7c2002-02-18 18:30:32 +0000836 p->tnum = 0;
drhe0bc4042002-06-25 01:09:11 +0000837 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
838 sqliteVdbeAddOp(v, OP_String, 0, 0);
839 if( p->pSelect==0 ){
840 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
841 }else{
842 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
drhf57b3392001-10-08 13:22:32 +0000843 }
drhe0bc4042002-06-25 01:09:11 +0000844 sqliteVdbeAddOp(v, OP_String, 0, 0);
845 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
846 sqliteVdbeAddOp(v, OP_String, 0, 0);
847 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
848 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
849 sqliteVdbeAddOp(v, OP_String, 0, 0);
850 if( pSelect ){
851 char *z = createTableStmt(p);
852 n = z ? strlen(z) : 0;
853 sqliteVdbeChangeP3(v, -1, z, n);
854 sqliteFree(z);
855 }else{
856 assert( pEnd!=0 );
857 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
858 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
859 }
860 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
861 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
862 if( !p->isTemp ){
863 sqliteChangeCookie(db, v);
864 }
865 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000866 if( pSelect ){
867 int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
868 sqliteVdbeAddOp(v, op, 1, 0);
869 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000870 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000871 }
drh1c928532002-01-31 15:54:21 +0000872 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000873 }
874}
875
876/*
drha76b5df2002-02-23 02:32:10 +0000877** The parser calls this routine in order to create a new VIEW
878*/
879void sqliteCreateView(
880 Parse *pParse, /* The parsing context */
881 Token *pBegin, /* The CREATE token that begins the statement */
882 Token *pName, /* The token that holds the name of the view */
drh6276c1c2002-07-08 22:03:32 +0000883 Select *pSelect, /* A SELECT statement that will become the new view */
884 int isTemp /* TRUE for a TEMPORARY view */
drha76b5df2002-02-23 02:32:10 +0000885){
drha76b5df2002-02-23 02:32:10 +0000886 Table *p;
drh4b59ab52002-08-24 18:24:51 +0000887 int n;
drh4ff6dfa2002-03-03 23:06:00 +0000888 const char *z;
drh4b59ab52002-08-24 18:24:51 +0000889 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000890
drh6276c1c2002-07-08 22:03:32 +0000891 sqliteStartTable(pParse, pBegin, pName, isTemp);
drha76b5df2002-02-23 02:32:10 +0000892 p = pParse->pNewTable;
drh417be792002-03-03 18:59:40 +0000893 if( p==0 ){
894 sqliteSelectDelete(pSelect);
895 return;
896 }
drh0f18b452002-05-08 21:30:15 +0000897 /* Ignore ORDER BY clauses on a SELECT */
898 if( pSelect->pOrderBy ){
899 sqliteExprListDelete(pSelect->pOrderBy);
900 pSelect->pOrderBy = 0;
901 }
drh4b59ab52002-08-24 18:24:51 +0000902 /* Make a copy of the entire SELECT statement that defines the view.
903 ** This will force all the Expr.token.z values to be dynamically
904 ** allocated rather than point to the input string - which means that
905 ** they will persist after the current sqlite_exec() call returns.
906 */
907 p->pSelect = sqliteSelectDup(pSelect);
908 sqliteSelectDelete(pSelect);
drh417be792002-03-03 18:59:40 +0000909 if( !pParse->initFlag ){
drh4b59ab52002-08-24 18:24:51 +0000910 sqliteViewGetColumnNames(pParse, p);
drh417be792002-03-03 18:59:40 +0000911 }
drh4b59ab52002-08-24 18:24:51 +0000912
913 /* Locate the end of the CREATE VIEW statement. Make sEnd point to
914 ** the end.
915 */
drha76b5df2002-02-23 02:32:10 +0000916 sEnd = pParse->sLastToken;
917 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
918 sEnd.z += sEnd.n;
919 }
920 sEnd.n = 0;
921 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000922 z = pBegin->z;
923 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
924 sEnd.z = &z[n-1];
925 sEnd.n = 1;
drh4b59ab52002-08-24 18:24:51 +0000926
927 /* Use sqliteEndTable() to add the view to the SQLITE_MASTER table */
928 sqliteEndTable(pParse, &sEnd, 0);
drha76b5df2002-02-23 02:32:10 +0000929 return;
drh417be792002-03-03 18:59:40 +0000930}
drha76b5df2002-02-23 02:32:10 +0000931
drh417be792002-03-03 18:59:40 +0000932/*
933** The Table structure pTable is really a VIEW. Fill in the names of
934** the columns of the view in the pTable structure. Return the number
935** of errors. If an error is seen leave an error message in pPare->zErrMsg.
936*/
937int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
938 ExprList *pEList;
939 Select *pSel;
940 Table *pSelTab;
941 int nErr = 0;
942
943 assert( pTable );
944
945 /* A positive nCol means the columns names for this view are
946 ** already known.
947 */
948 if( pTable->nCol>0 ) return 0;
949
950 /* A negative nCol is a special marker meaning that we are currently
951 ** trying to compute the column names. If we enter this routine with
952 ** a negative nCol, it means two or more views form a loop, like this:
953 **
954 ** CREATE VIEW one AS SELECT * FROM two;
955 ** CREATE VIEW two AS SELECT * FROM one;
drh3b167c72002-06-28 12:18:47 +0000956 **
957 ** Actually, this error is caught previously and so the following test
958 ** should always fail. But we will leave it in place just to be safe.
drh417be792002-03-03 18:59:40 +0000959 */
960 if( pTable->nCol<0 ){
961 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
962 " is circularly defined", 0);
963 pParse->nErr++;
964 return 1;
965 }
966
967 /* If we get this far, it means we need to compute the table names.
968 */
969 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
970 pSel = pTable->pSelect;
971
972 /* Note that the call to sqliteResultSetOfSelect() will expand any
973 ** "*" elements in this list. But we will need to restore the list
974 ** back to its original configuration afterwards, so we save a copy of
975 ** the original in pEList.
976 */
977 pEList = pSel->pEList;
978 pSel->pEList = sqliteExprListDup(pEList);
979 if( pSel->pEList==0 ){
980 pSel->pEList = pEList;
981 return 1; /* Malloc failed */
982 }
983 pTable->nCol = -1;
984 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
985 if( pSelTab ){
986 assert( pTable->aCol==0 );
987 pTable->nCol = pSelTab->nCol;
988 pTable->aCol = pSelTab->aCol;
989 pSelTab->nCol = 0;
990 pSelTab->aCol = 0;
991 sqliteDeleteTable(0, pSelTab);
992 pParse->db->flags |= SQLITE_UnresetViews;
993 }else{
994 pTable->nCol = 0;
995 nErr++;
996 }
997 sqliteSelectUnbind(pSel);
998 sqliteExprListDelete(pSel->pEList);
999 pSel->pEList = pEList;
1000 return nErr;
1001}
1002
1003/*
1004** Clear the column names from the VIEW pTable.
1005**
1006** This routine is called whenever any other table or view is modified.
1007** The view passed into this routine might depend directly or indirectly
1008** on the modified or deleted table so we need to clear the old column
1009** names so that they will be recomputed.
1010*/
1011static void sqliteViewResetColumnNames(Table *pTable){
1012 int i;
1013 if( pTable==0 || pTable->pSelect==0 ) return;
1014 if( pTable->nCol==0 ) return;
1015 for(i=0; i<pTable->nCol; i++){
1016 sqliteFree(pTable->aCol[i].zName);
1017 sqliteFree(pTable->aCol[i].zDflt);
1018 sqliteFree(pTable->aCol[i].zType);
1019 }
1020 sqliteFree(pTable->aCol);
1021 pTable->aCol = 0;
1022 pTable->nCol = 0;
1023}
1024
1025/*
1026** Clear the column names from every VIEW.
1027*/
1028void sqliteViewResetAll(sqlite *db){
1029 HashElem *i;
1030 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
1031 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
1032 Table *pTab = sqliteHashData(i);
1033 if( pTab->pSelect ){
1034 sqliteViewResetColumnNames(pTab);
1035 }
1036 }
1037 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +00001038}
1039
1040/*
drh75897232000-05-29 14:26:00 +00001041** Given a token, look up a table with that name. If not found, leave
1042** an error for the parser to find and return NULL.
1043*/
drhcce7d172000-05-31 15:34:51 +00001044Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +00001045 char *zName;
1046 Table *pTab;
1047 zName = sqliteTableNameFromToken(pTok);
1048 if( zName==0 ) return 0;
1049 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +00001050 sqliteFree(zName);
1051 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +00001052 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
1053 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +00001054 pParse->nErr++;
1055 }
1056 return pTab;
1057}
1058
1059/*
1060** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +00001061** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +00001062*/
drh4ff6dfa2002-03-03 23:06:00 +00001063void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +00001064 Table *pTable;
drh75897232000-05-29 14:26:00 +00001065 Vdbe *v;
1066 int base;
drh5edc3122001-09-13 21:53:09 +00001067 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001068
drhdaffd0e2001-04-11 14:28:42 +00001069 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001070 pTable = sqliteTableFromToken(pParse, pName);
1071 if( pTable==0 ) return;
1072 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +00001073 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
1074 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +00001075 pParse->nErr++;
1076 return;
1077 }
drh4ff6dfa2002-03-03 23:06:00 +00001078 if( isView && pTable->pSelect==0 ){
1079 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1080 pTable->zName, 0);
1081 pParse->nErr++;
1082 return;
1083 }
1084 if( !isView && pTable->pSelect ){
1085 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1086 pTable->zName, 0);
1087 pParse->nErr++;
1088 return;
1089 }
drh75897232000-05-29 14:26:00 +00001090
drh1ccde152000-06-17 13:12:39 +00001091 /* Generate code to remove the table from the master table
1092 ** on disk.
1093 */
drhd8bc7082000-06-07 23:51:50 +00001094 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001095 if( v ){
1096 static VdbeOp dropTable[] = {
drhe0bc4042002-06-25 01:09:11 +00001097 { OP_Rewind, 0, ADDR(8), 0},
1098 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001099 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001100 { OP_MemLoad, 1, 0, 0}, /* 3 */
drhe3c41372001-09-17 20:25:58 +00001101 { OP_Column, 0, 2, 0},
drhe0bc4042002-06-25 01:09:11 +00001102 { OP_Ne, 0, ADDR(7), 0},
drh75897232000-05-29 14:26:00 +00001103 { OP_Delete, 0, 0, 0},
drhe0bc4042002-06-25 01:09:11 +00001104 { OP_Next, 0, ADDR(3), 0}, /* 7 */
drh75897232000-05-29 14:26:00 +00001105 };
1106 Index *pIdx;
drhe0bc4042002-06-25 01:09:11 +00001107 Trigger *pTrigger;
drhcabb0812002-09-14 13:47:32 +00001108 sqliteBeginWriteOperation(pParse, 0, pTable->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001109 sqliteOpenMasterTable(v, pTable->isTemp);
danielk1977c3f9bad2002-05-15 08:30:12 +00001110 /* Drop all triggers associated with the table being dropped */
drhe0bc4042002-06-25 01:09:11 +00001111 pTrigger = pTable->pTrigger;
1112 while( pTrigger ){
danielk1977c3f9bad2002-05-15 08:30:12 +00001113 Token tt;
1114 tt.z = pTable->pTrigger->name;
1115 tt.n = strlen(pTable->pTrigger->name);
1116 sqliteDropTrigger(pParse, &tt, 1);
drhe0bc4042002-06-25 01:09:11 +00001117 if( pParse->explain ){
1118 pTrigger = pTrigger->pNext;
1119 }else{
1120 pTrigger = pTable->pTrigger;
1121 }
danielk1977c3f9bad2002-05-15 08:30:12 +00001122 }
drhe0bc4042002-06-25 01:09:11 +00001123 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
1124 sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001125 if( !pTable->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001126 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001127 }
drhe0bc4042002-06-25 01:09:11 +00001128 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +00001129 if( !isView ){
1130 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1131 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1132 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1133 }
drh5e00f6c2001-09-13 13:46:56 +00001134 }
drh1c928532002-01-31 15:54:21 +00001135 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001136 }
1137
drhe0bc4042002-06-25 01:09:11 +00001138 /* Delete the in-memory description of the table.
drh75897232000-05-29 14:26:00 +00001139 **
1140 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001141 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001142 */
1143 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001144 sqliteUnlinkAndDeleteTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001145 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001146 }
drh417be792002-03-03 18:59:40 +00001147 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001148}
1149
1150/*
drh38640e12002-07-05 21:42:36 +00001151** This routine constructs a P3 string suitable for an OP_MakeIdxKey
1152** opcode and adds that P3 string to the most recently inserted instruction
1153** in the virtual machine. The P3 string consists of a single character
1154** for each column in the index pIdx of table pTab. If the column uses
1155** a numeric sort order, then the P3 string character corresponding to
1156** that column is 'n'. If the column uses a text sort order, then the
1157** P3 string is 't'. See the OP_MakeIdxKey opcode documentation for
1158** additional information. See also the sqliteAddKeyType() routine.
1159*/
1160void sqliteAddIdxKeyType(Vdbe *v, Index *pIdx){
1161 char *zType;
1162 Table *pTab;
1163 int i, n;
1164 assert( pIdx!=0 && pIdx->pTable!=0 );
1165 pTab = pIdx->pTable;
1166 n = pIdx->nColumn;
1167 zType = sqliteMalloc( n+1 );
1168 if( zType==0 ) return;
1169 for(i=0; i<n; i++){
1170 int iCol = pIdx->aiColumn[i];
1171 assert( iCol>=0 && iCol<pTab->nCol );
1172 if( (pTab->aCol[iCol].sortOrder & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
1173 zType[i] = 't';
1174 }else{
1175 zType[i] = 'n';
1176 }
1177 }
1178 zType[n] = 0;
1179 sqliteVdbeChangeP3(v, -1, zType, n);
1180 sqliteFree(zType);
1181}
1182
1183/*
drhc2eef3b2002-08-31 18:53:06 +00001184** This routine is called to create a new foreign key on the table
1185** currently under construction. pFromCol determines which columns
1186** in the current table point to the foreign key. If pFromCol==0 then
1187** connect the key to the last column inserted. pTo is the name of
1188** the table referred to. pToCol is a list of tables in the other
1189** pTo table that the foreign key points to. flags contains all
1190** information about the conflict resolution algorithms specified
1191** in the ON DELETE, ON UPDATE and ON INSERT clauses.
1192**
1193** An FKey structure is created and added to the table currently
1194** under construction in the pParse->pNewTable field. The new FKey
1195** is not linked into db->aFKey at this point - that does not happen
1196** until sqliteEndTable().
1197**
1198** The foreign key is set for IMMEDIATE processing. A subsequent call
1199** to sqliteDeferForeignKey() might change this to DEFERRED.
1200*/
1201void sqliteCreateForeignKey(
1202 Parse *pParse, /* Parsing context */
1203 IdList *pFromCol, /* Columns in this table that point to other table */
1204 Token *pTo, /* Name of the other table */
1205 IdList *pToCol, /* Columns in the other table */
1206 int flags /* Conflict resolution algorithms. */
1207){
1208 Table *p = pParse->pNewTable;
1209 int nByte;
1210 int i;
1211 int nCol;
1212 char *z;
1213 FKey *pFKey = 0;
1214
1215 assert( pTo!=0 );
1216 if( p==0 || pParse->nErr ) goto fk_end;
1217 if( pFromCol==0 ){
1218 int iCol = p->nCol-1;
1219 if( iCol<0 ) goto fk_end;
1220 if( pToCol && pToCol->nId!=1 ){
1221 sqliteSetNString(&pParse->zErrMsg, "foreign key on ", -1,
1222 p->aCol[iCol].zName, -1,
1223 " should reference only one column of table ", -1,
1224 pTo->z, pTo->n, 0);
1225 pParse->nErr++;
1226 goto fk_end;
1227 }
1228 nCol = 1;
1229 }else if( pToCol && pToCol->nId!=pFromCol->nId ){
1230 sqliteSetString(&pParse->zErrMsg,
1231 "number of columns in foreign key does not match the number of "
1232 "columns in the referenced table", 0);
1233 pParse->nErr++;
1234 goto fk_end;
1235 }else{
1236 nCol = pFromCol->nId;
1237 }
1238 nByte = sizeof(*pFKey) + nCol*sizeof(pFKey->aCol[0]) + pTo->n + 1;
1239 if( pToCol ){
1240 for(i=0; i<pToCol->nId; i++){
1241 nByte += strlen(pToCol->a[i].zName) + 1;
1242 }
1243 }
1244 pFKey = sqliteMalloc( nByte );
1245 if( pFKey==0 ) goto fk_end;
1246 pFKey->pFrom = p;
1247 pFKey->pNextFrom = p->pFKey;
drhdf68f6b2002-09-21 15:57:57 +00001248 z = (char*)&pFKey[1];
1249 pFKey->aCol = (struct sColMap*)z;
1250 z += sizeof(struct sColMap)*nCol;
1251 pFKey->zTo = z;
drhc2eef3b2002-08-31 18:53:06 +00001252 memcpy(z, pTo->z, pTo->n);
1253 z[pTo->n] = 0;
1254 z += pTo->n+1;
1255 pFKey->pNextTo = 0;
1256 pFKey->nCol = nCol;
drhc2eef3b2002-08-31 18:53:06 +00001257 if( pFromCol==0 ){
1258 pFKey->aCol[0].iFrom = p->nCol-1;
1259 }else{
1260 for(i=0; i<nCol; i++){
1261 int j;
1262 for(j=0; j<p->nCol; j++){
1263 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1264 pFKey->aCol[i].iFrom = j;
1265 break;
1266 }
1267 }
1268 if( j>=p->nCol ){
1269 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1270 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1271 pParse->nErr++;
1272 goto fk_end;
1273 }
1274 }
1275 }
1276 if( pToCol ){
1277 for(i=0; i<nCol; i++){
1278 int n = strlen(pToCol->a[i].zName);
1279 pFKey->aCol[i].zCol = z;
1280 memcpy(z, pToCol->a[i].zName, n);
1281 z[n] = 0;
1282 z += n+1;
1283 }
1284 }
1285 pFKey->isDeferred = 0;
1286 pFKey->deleteConf = flags & 0xff;
1287 pFKey->updateConf = (flags >> 8 ) & 0xff;
1288 pFKey->insertConf = (flags >> 16 ) & 0xff;
1289
1290 /* Link the foreign key to the table as the last step.
1291 */
1292 p->pFKey = pFKey;
1293 pFKey = 0;
1294
1295fk_end:
1296 sqliteFree(pFKey);
1297 sqliteIdListDelete(pFromCol);
1298 sqliteIdListDelete(pToCol);
1299}
1300
1301/*
1302** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1303** clause is seen as part of a foreign key definition. The isDeferred
1304** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1305** The behavior of the most recently created foreign key is adjusted
1306** accordingly.
1307*/
1308void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1309 Table *pTab;
1310 FKey *pFKey;
1311 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1312 pFKey->isDeferred = isDeferred;
1313}
1314
1315/*
drh75897232000-05-29 14:26:00 +00001316** Create a new index for an SQL table. pIndex is the name of the index
1317** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001318** be NULL for a primary key or an index that is created to satisfy a
1319** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001320** as the table to be indexed. pParse->pNewTable is a table that is
1321** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001322**
drh382c0242001-10-06 16:33:02 +00001323** pList is a list of columns to be indexed. pList will be NULL if this
1324** is a primary key or unique-constraint on the most recent column added
1325** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001326*/
1327void sqliteCreateIndex(
1328 Parse *pParse, /* All information about this parse */
1329 Token *pName, /* Name of the index. May be NULL */
1330 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001331 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001332 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001333 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1334 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1335){
1336 Table *pTab; /* Table to be indexed */
1337 Index *pIndex; /* The index to be created */
1338 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001339 int i, j;
drhf57b3392001-10-08 13:22:32 +00001340 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001341 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001342 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001343
drhdaffd0e2001-04-11 14:28:42 +00001344 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1345
drh75897232000-05-29 14:26:00 +00001346 /*
1347 ** Find the table that is to be indexed. Return early if not found.
1348 */
1349 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001350 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001351 pTab = sqliteTableFromToken(pParse, pTable);
1352 }else{
drhe3c41372001-09-17 20:25:58 +00001353 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001354 pTab = pParse->pNewTable;
1355 }
1356 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1357 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001358 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1359 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001360 pParse->nErr++;
1361 goto exit_create_index;
1362 }
drha76b5df2002-02-23 02:32:10 +00001363 if( pTab->pSelect ){
1364 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1365 pParse->nErr++;
1366 goto exit_create_index;
1367 }
drh75897232000-05-29 14:26:00 +00001368
drhf57b3392001-10-08 13:22:32 +00001369 /* If this index is created while re-reading the schema from sqlite_master
1370 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001371 ** only mean that the table that this index is really associated with is
1372 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001373 ** Since its table has been suppressed, we need to also suppress the
1374 ** index.
1375 */
drhe0bc4042002-06-25 01:09:11 +00001376 if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){
drhf57b3392001-10-08 13:22:32 +00001377 goto exit_create_index;
1378 }
1379
drh75897232000-05-29 14:26:00 +00001380 /*
1381 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001382 ** index or table with the same name.
1383 **
1384 ** Exception: If we are reading the names of permanent indices from the
1385 ** sqlite_master table (because some other process changed the schema) and
1386 ** one of the index names collides with the name of a temporary table or
1387 ** index, then we will continue to process this index, but we will not
1388 ** store its name in the hash table. Set the hideName flag to accomplish
1389 ** this.
1390 **
1391 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001392 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1393 ** own name.
drh75897232000-05-29 14:26:00 +00001394 */
1395 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001396 Index *pISameName; /* Another index with the same name */
1397 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001398 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001399 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001400 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1401 if( pISameName->pTable->isTemp && pParse->initFlag ){
1402 hideName = 1;
1403 }else{
1404 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1405 " already exists", 0);
1406 pParse->nErr++;
1407 goto exit_create_index;
1408 }
drhe3c41372001-09-17 20:25:58 +00001409 }
drhf57b3392001-10-08 13:22:32 +00001410 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1411 if( pTSameName->isTemp && pParse->initFlag ){
1412 hideName = 1;
1413 }else{
1414 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1415 zName, 0);
1416 pParse->nErr++;
1417 goto exit_create_index;
1418 }
drhe3c41372001-09-17 20:25:58 +00001419 }
drh75897232000-05-29 14:26:00 +00001420 }else{
drhadbca9c2001-09-27 15:11:53 +00001421 char zBuf[30];
1422 int n;
1423 Index *pLoop;
1424 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1425 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001426 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001427 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001428 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001429 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001430 }
1431
1432 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001433 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001434 ** So create a fake list to simulate this.
1435 */
1436 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001437 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001438 nullId.n = strlen(nullId.z);
1439 pList = sqliteIdListAppend(0, &nullId);
1440 if( pList==0 ) goto exit_create_index;
1441 }
1442
1443 /*
1444 ** Allocate the index structure.
1445 */
drhdcc581c2000-05-30 13:44:19 +00001446 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001447 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001448 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001449 pIndex->aiColumn = (int*)&pIndex[1];
1450 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001451 strcpy(pIndex->zName, zName);
1452 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001453 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001454 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001455 pIndex->autoIndex = pName==0;
drh75897232000-05-29 14:26:00 +00001456
drh1ccde152000-06-17 13:12:39 +00001457 /* Scan the names of the columns of the table to be indexed and
1458 ** load the column indices into the Index structure. Report an error
1459 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001460 */
1461 for(i=0; i<pList->nId; i++){
1462 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001463 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001464 }
1465 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001466 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001467 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001468 pParse->nErr++;
1469 sqliteFree(pIndex);
1470 goto exit_create_index;
1471 }
drh967e8b72000-06-21 13:59:10 +00001472 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001473 }
1474
1475 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001476 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001477 */
drhf57b3392001-10-08 13:22:32 +00001478 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001479 Index *p;
1480 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1481 if( p ){
1482 assert( p==pIndex ); /* Malloc must have failed */
1483 sqliteFree(pIndex);
1484 goto exit_create_index;
1485 }
drh5e00f6c2001-09-13 13:46:56 +00001486 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001487 }
drh9cfcf5d2002-01-29 18:41:24 +00001488
1489 /* When adding an index to the list of indices for a table, make
1490 ** sure all indices labeled OE_Replace come after all those labeled
1491 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1492 ** and INSERT.
1493 */
1494 if( onError!=OE_Replace || pTab->pIndex==0
1495 || pTab->pIndex->onError==OE_Replace){
1496 pIndex->pNext = pTab->pIndex;
1497 pTab->pIndex = pIndex;
1498 }else{
1499 Index *pOther = pTab->pIndex;
1500 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1501 pOther = pOther->pNext;
1502 }
1503 pIndex->pNext = pOther->pNext;
1504 pOther->pNext = pIndex;
1505 }
drh75897232000-05-29 14:26:00 +00001506
drhd78eeee2001-09-13 16:18:53 +00001507 /* If the initFlag is 1 it means we are reading the SQL off the
1508 ** "sqlite_master" table on the disk. So do not write to the disk
1509 ** again. Extract the table number from the pParse->newTnum field.
1510 */
drhadbca9c2001-09-27 15:11:53 +00001511 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001512 pIndex->tnum = pParse->newTnum;
1513 }
1514
drh75897232000-05-29 14:26:00 +00001515 /* If the initFlag is 0 then create the index on disk. This
1516 ** involves writing the index into the master table and filling in the
1517 ** index with the current table contents.
1518 **
1519 ** The initFlag is 0 when the user first enters a CREATE INDEX
1520 ** command. The initFlag is 1 when a database is opened and
1521 ** CREATE INDEX statements are read out of the master table. In
1522 ** the latter case the index already exists on disk, which is why
1523 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001524 **
1525 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001526 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1527 ** has just been created, it contains no data and the index initialization
1528 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001529 */
drhadbca9c2001-09-27 15:11:53 +00001530 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001531 int n;
drhadbca9c2001-09-27 15:11:53 +00001532 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001533 int lbl1, lbl2;
1534 int i;
drhadbca9c2001-09-27 15:11:53 +00001535 int addr;
drhf57b3392001-10-08 13:22:32 +00001536 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001537
drhd8bc7082000-06-07 23:51:50 +00001538 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001539 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001540 if( pTable!=0 ){
drhcabb0812002-09-14 13:47:32 +00001541 sqliteBeginWriteOperation(pParse, 0, isTemp);
drhe0bc4042002-06-25 01:09:11 +00001542 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001543 }
drhe0bc4042002-06-25 01:09:11 +00001544 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1545 sqliteVdbeAddOp(v, OP_String, 0, 0);
1546 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1547 sqliteVdbeAddOp(v, OP_String, 0, 0);
1548 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1549 sqliteVdbeAddOp(v, OP_String, 0, 0);
1550 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001551 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1552 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001553 pIndex->tnum = 0;
1554 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001555 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001556 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001557 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001558 }else{
drh99fcd712001-10-13 01:06:47 +00001559 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001560 }
drh5e00f6c2001-09-13 13:46:56 +00001561 }
drhe0bc4042002-06-25 01:09:11 +00001562 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1563 if( pStart && pEnd ){
1564 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1565 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001566 }
drhe0bc4042002-06-25 01:09:11 +00001567 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1568 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001569 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001570 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1571 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001572 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001573 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1574 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001575 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001576 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001577 }
drh99fcd712001-10-13 01:06:47 +00001578 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001579 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001580 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001581 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001582 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001583 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1584 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001585 }
drhadbca9c2001-09-27 15:11:53 +00001586 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001587 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001588 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001589 }
drhe0bc4042002-06-25 01:09:11 +00001590 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001591 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001592 }
drh75897232000-05-29 14:26:00 +00001593 }
1594
drh75897232000-05-29 14:26:00 +00001595 /* Clean up before exiting */
1596exit_create_index:
1597 sqliteIdListDelete(pList);
1598 sqliteFree(zName);
1599 return;
1600}
1601
1602/*
drh74e24cd2002-01-09 03:19:59 +00001603** This routine will drop an existing named index. This routine
1604** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001605*/
1606void sqliteDropIndex(Parse *pParse, Token *pName){
1607 Index *pIndex;
1608 char *zName;
1609 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001610 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001611
drhdaffd0e2001-04-11 14:28:42 +00001612 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001613 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001614 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001615 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001616 sqliteFree(zName);
1617 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001618 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1619 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001620 pParse->nErr++;
1621 return;
1622 }
drh485b39b2002-07-13 03:11:52 +00001623 if( pIndex->autoIndex ){
1624 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1625 "or PRIMARY KEY constraint cannot be dropped", 0);
1626 pParse->nErr++;
1627 return;
1628 }
drh75897232000-05-29 14:26:00 +00001629
1630 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001631 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001632 if( v ){
1633 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001634 { OP_Rewind, 0, ADDR(9), 0},
1635 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001636 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001637 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001638 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001639 { OP_Eq, 0, ADDR(8), 0},
1640 { OP_Next, 0, ADDR(3), 0},
1641 { OP_Goto, 0, ADDR(9), 0},
1642 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001643 };
1644 int base;
drhf57b3392001-10-08 13:22:32 +00001645 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001646
drhcabb0812002-09-14 13:47:32 +00001647 sqliteBeginWriteOperation(pParse, 0, pTab->isTemp);
drhe0bc4042002-06-25 01:09:11 +00001648 sqliteOpenMasterTable(v, pTab->isTemp);
1649 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1650 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001651 if( !pTab->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001652 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001653 }
drhe0bc4042002-06-25 01:09:11 +00001654 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001655 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001656 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001657 }
1658
drhe0bc4042002-06-25 01:09:11 +00001659 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001660 */
1661 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001662 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001663 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001664 }
1665}
1666
1667/*
drh75897232000-05-29 14:26:00 +00001668** Append a new element to the given IdList. Create a new IdList if
1669** need be.
drhdaffd0e2001-04-11 14:28:42 +00001670**
1671** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001672*/
1673IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1674 if( pList==0 ){
1675 pList = sqliteMalloc( sizeof(IdList) );
1676 if( pList==0 ) return 0;
1677 }
1678 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001679 struct IdList_item *a;
1680 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1681 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001682 sqliteIdListDelete(pList);
1683 return 0;
drh75897232000-05-29 14:26:00 +00001684 }
drh6d4abfb2001-10-22 02:58:08 +00001685 pList->a = a;
drh75897232000-05-29 14:26:00 +00001686 }
1687 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1688 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001689 char **pz = &pList->a[pList->nId].zName;
1690 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1691 if( *pz==0 ){
1692 sqliteIdListDelete(pList);
1693 return 0;
1694 }else{
1695 sqliteDequote(*pz);
1696 }
drh75897232000-05-29 14:26:00 +00001697 }
1698 pList->nId++;
1699 return pList;
1700}
1701
1702/*
drhad3cab52002-05-24 02:04:32 +00001703** Append a new table name to the given SrcList. Create a new SrcList if
1704** need be. A new entry is created in the SrcList even if pToken is NULL.
1705**
1706** A new SrcList is returned, or NULL if malloc() fails.
1707*/
1708SrcList *sqliteSrcListAppend(SrcList *pList, Token *pToken){
1709 if( pList==0 ){
1710 pList = sqliteMalloc( sizeof(IdList) );
1711 if( pList==0 ) return 0;
1712 }
1713 if( (pList->nSrc & 7)==0 ){
1714 struct SrcList_item *a;
1715 a = sqliteRealloc(pList->a, (pList->nSrc+8)*sizeof(pList->a[0]) );
1716 if( a==0 ){
1717 sqliteSrcListDelete(pList);
1718 return 0;
1719 }
1720 pList->a = a;
1721 }
1722 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
1723 if( pToken ){
1724 char **pz = &pList->a[pList->nSrc].zName;
1725 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1726 if( *pz==0 ){
1727 sqliteSrcListDelete(pList);
1728 return 0;
1729 }else{
1730 sqliteDequote(*pz);
1731 }
1732 }
1733 pList->nSrc++;
1734 return pList;
1735}
1736
1737/*
drh75897232000-05-29 14:26:00 +00001738** Add an alias to the last identifier on the given identifier list.
1739*/
drhad3cab52002-05-24 02:04:32 +00001740void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1741 if( pList && pList->nSrc>0 ){
1742 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001743 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001744 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001745 }
1746}
1747
1748/*
drhad3cab52002-05-24 02:04:32 +00001749** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001750*/
1751void sqliteIdListDelete(IdList *pList){
1752 int i;
1753 if( pList==0 ) return;
1754 for(i=0; i<pList->nId; i++){
1755 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001756 }
1757 sqliteFree(pList->a);
1758 sqliteFree(pList);
1759}
1760
1761/*
drhad2d8302002-05-24 20:31:36 +00001762** Return the index in pList of the identifier named zId. Return -1
1763** if not found.
1764*/
1765int sqliteIdListIndex(IdList *pList, const char *zName){
1766 int i;
1767 if( pList==0 ) return -1;
1768 for(i=0; i<pList->nId; i++){
1769 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1770 }
1771 return -1;
1772}
1773
1774/*
drhad3cab52002-05-24 02:04:32 +00001775** Delete an entire SrcList including all its substructure.
1776*/
1777void sqliteSrcListDelete(SrcList *pList){
1778 int i;
1779 if( pList==0 ) return;
1780 for(i=0; i<pList->nSrc; i++){
1781 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001782 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001783 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001784 sqliteDeleteTable(0, pList->a[i].pTab);
1785 }
drhff78bd22002-02-27 01:47:11 +00001786 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001787 sqliteExprDelete(pList->a[i].pOn);
1788 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001789 }
1790 sqliteFree(pList->a);
1791 sqliteFree(pList);
1792}
1793
drh982cef72000-05-30 16:27:03 +00001794/*
1795** The COPY command is for compatibility with PostgreSQL and specificially
1796** for the ability to read the output of pg_dump. The format is as
1797** follows:
1798**
1799** COPY table FROM file [USING DELIMITERS string]
1800**
1801** "table" is an existing table name. We will read lines of code from
1802** file to fill this table with data. File might be "stdin". The optional
1803** delimiter string identifies the field separators. The default is a tab.
1804*/
1805void sqliteCopy(
1806 Parse *pParse, /* The parser context */
1807 Token *pTableName, /* The name of the table into which we will insert */
1808 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001809 Token *pDelimiter, /* Use this as the field delimiter */
1810 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001811){
1812 Table *pTab;
1813 char *zTab;
drh1c928532002-01-31 15:54:21 +00001814 int i;
drh982cef72000-05-30 16:27:03 +00001815 Vdbe *v;
1816 int addr, end;
1817 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001818 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001819
1820 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001821 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001822 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001823 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001824 if( pTab==0 ) goto copy_cleanup;
drhd8bc7082000-06-07 23:51:50 +00001825 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001826 if( v ){
drhf57b3392001-10-08 13:22:32 +00001827 int openOp;
drhcabb0812002-09-14 13:47:32 +00001828 sqliteBeginWriteOperation(pParse, 1, pTab->isTemp);
drh99fcd712001-10-13 01:06:47 +00001829 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001830 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001831 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001832 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001833 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1834 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001835 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001836 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1837 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001838 }
drhb419a922002-01-30 16:17:23 +00001839 if( db->flags & SQLITE_CountRows ){
1840 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1841 }
drh982cef72000-05-30 16:27:03 +00001842 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001843 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001844 if( pDelimiter ){
1845 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1846 sqliteVdbeDequoteP3(v, addr);
1847 }else{
1848 sqliteVdbeChangeP3(v, addr, "\t", 1);
1849 }
drh8aff1012001-12-22 14:49:24 +00001850 if( pTab->iPKey>=0 ){
1851 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1852 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1853 }else{
1854 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1855 }
drh982cef72000-05-30 16:27:03 +00001856 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001857 if( i==pTab->iPKey ){
1858 /* The integer primary key column is filled with NULL since its
1859 ** value is always pulled from the record number */
1860 sqliteVdbeAddOp(v, OP_String, 0, 0);
1861 }else{
1862 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1863 }
drh982cef72000-05-30 16:27:03 +00001864 }
drhb419a922002-01-30 16:17:23 +00001865 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1866 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1867 if( (db->flags & SQLITE_CountRows)!=0 ){
1868 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001869 }
drh99fcd712001-10-13 01:06:47 +00001870 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1871 sqliteVdbeResolveLabel(v, end);
1872 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001873 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001874 if( db->flags & SQLITE_CountRows ){
1875 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1876 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1877 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1878 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1879 }
drh982cef72000-05-30 16:27:03 +00001880 }
1881
1882copy_cleanup:
1883 return;
1884}
drhdce2cbe2000-05-31 02:27:49 +00001885
1886/*
1887** The non-standard VACUUM command is used to clean up the database,
1888** collapse free space, etc. It is modelled after the VACUUM command
1889** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001890**
drh1bffb9c2002-02-03 17:37:36 +00001891** In version 1.0.x of SQLite, the VACUUM command would call
1892** gdbm_reorganize() on all the database tables. But beginning
1893** with 2.0.0, SQLite no longer uses GDBM so this command has
1894** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001895*/
1896void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001897 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001898}
drhc4a3c772001-04-04 11:48:57 +00001899
1900/*
1901** Begin a transaction
1902*/
drh1c928532002-01-31 15:54:21 +00001903void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001904 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001905
drhc4a3c772001-04-04 11:48:57 +00001906 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001907 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001908 if( db->flags & SQLITE_InTrans ){
1909 pParse->nErr++;
1910 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
1911 "within a transaction", 0);
1912 return;
1913 }
drhcabb0812002-09-14 13:47:32 +00001914 sqliteBeginWriteOperation(pParse, 0, 0);
drh5e00f6c2001-09-13 13:46:56 +00001915 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001916 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001917}
1918
1919/*
1920** Commit a transaction
1921*/
1922void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001923 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001924
drhc4a3c772001-04-04 11:48:57 +00001925 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001926 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001927 if( (db->flags & SQLITE_InTrans)==0 ){
1928 pParse->nErr++;
1929 sqliteSetString(&pParse->zErrMsg,
1930 "cannot commit - no transaction is active", 0);
1931 return;
1932 }
drh5e00f6c2001-09-13 13:46:56 +00001933 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001934 sqliteEndWriteOperation(pParse);
1935 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001936}
1937
1938/*
1939** Rollback a transaction
1940*/
1941void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001942 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001943 Vdbe *v;
1944
drhc4a3c772001-04-04 11:48:57 +00001945 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001946 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001947 if( (db->flags & SQLITE_InTrans)==0 ){
1948 pParse->nErr++;
1949 sqliteSetString(&pParse->zErrMsg,
1950 "cannot rollback - no transaction is active", 0);
1951 return;
1952 }
drh5e00f6c2001-09-13 13:46:56 +00001953 v = sqliteGetVdbe(pParse);
1954 if( v ){
drh99fcd712001-10-13 01:06:47 +00001955 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001956 }
drh5e00f6c2001-09-13 13:46:56 +00001957 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001958 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001959}
drhf57b14a2001-09-14 18:54:08 +00001960
1961/*
drh1c928532002-01-31 15:54:21 +00001962** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00001963** might change the database.
1964**
1965** This routine starts a new transaction if we are not already within
1966** a transaction. If we are already within a transaction, then a checkpoint
1967** is set if the setCheckpoint parameter is true. A checkpoint should
1968** be set for operations that might fail (due to a constraint) part of
1969** the way through and which will need to undo some writes without having to
1970** rollback the whole transaction. For operations where all constraints
1971** can be checked before any changes are made to the database, it is never
1972** necessary to undo a write and the checkpoint should not be set.
drhcabb0812002-09-14 13:47:32 +00001973**
1974** The tempOnly flag indicates that only temporary tables will be changed
1975** during this write operation. The primary database table is not
1976** write-locked. Only the temporary database file gets a write lock.
1977** Other processes can continue to read or write the primary database file.
drh1c928532002-01-31 15:54:21 +00001978*/
drhcabb0812002-09-14 13:47:32 +00001979void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint, int tempOnly){
drh663fc632002-02-02 18:49:19 +00001980 Vdbe *v;
1981 v = sqliteGetVdbe(pParse);
1982 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00001983 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00001984 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drhcabb0812002-09-14 13:47:32 +00001985 sqliteVdbeAddOp(v, OP_Transaction, tempOnly, 0);
1986 if( !tempOnly ){
1987 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1988 pParse->schemaVerified = 1;
1989 }
drhc977f7f2002-05-21 11:38:11 +00001990 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00001991 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1992 }
1993}
1994
1995/*
drh1c928532002-01-31 15:54:21 +00001996** Generate code that concludes an operation that may have changed
1997** the database. This is a companion function to BeginWriteOperation().
1998** If a transaction was started, then commit it. If a checkpoint was
1999** started then commit that.
2000*/
2001void sqliteEndWriteOperation(Parse *pParse){
2002 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00002003 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00002004 v = sqliteGetVdbe(pParse);
2005 if( v==0 ) return;
2006 if( pParse->db->flags & SQLITE_InTrans ){
2007 /* Do Nothing */
2008 }else{
2009 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2010 }
2011}
2012
2013
2014/*
drhf57b14a2001-09-14 18:54:08 +00002015** Interpret the given string as a boolean value.
2016*/
2017static int getBoolean(char *z){
2018 static char *azTrue[] = { "yes", "on", "true" };
2019 int i;
2020 if( z[0]==0 ) return 0;
2021 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2022 return atoi(z);
2023 }
2024 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
2025 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
2026 }
2027 return 0;
2028}
2029
2030/*
2031** Process a pragma statement.
2032**
2033** Pragmas are of this form:
2034**
2035** PRAGMA id = value
2036**
2037** The identifier might also be a string. The value is a string, and
2038** identifier, or a number. If minusFlag is true, then the value is
2039** a number that was preceded by a minus sign.
2040*/
2041void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
2042 char *zLeft = 0;
2043 char *zRight = 0;
2044 sqlite *db = pParse->db;
2045
2046 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
2047 sqliteDequote(zLeft);
2048 if( minusFlag ){
2049 zRight = 0;
2050 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
2051 }else{
2052 zRight = sqliteStrNDup(pRight->z, pRight->n);
2053 sqliteDequote(zRight);
2054 }
2055
drhcd61c282002-03-06 22:01:34 +00002056 /*
2057 ** PRAGMA default_cache_size
2058 ** PRAGMA default_cache_size=N
2059 **
2060 ** The first form reports the current persistent setting for the
2061 ** page cache size. The value returned is the maximum number of
2062 ** pages in the page cache. The second form sets both the current
2063 ** page cache size value and the persistent page cache size value
2064 ** stored in the database file.
2065 **
2066 ** The default cache size is stored in meta-value 2 of page 1 of the
2067 ** database file. The cache size is actually the absolute value of
2068 ** this memory location. The sign of meta-value 2 determines the
2069 ** synchronous setting. A negative value means synchronous is off
2070 ** and a positive value means synchronous is on.
2071 */
2072 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00002073 static VdbeOp getCacheSize[] = {
2074 { OP_ReadCookie, 0, 2, 0},
2075 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00002076 { OP_Dup, 0, 0, 0},
2077 { OP_Integer, 0, 0, 0},
2078 { OP_Ne, 0, 6, 0},
2079 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00002080 { OP_ColumnCount, 1, 0, 0},
2081 { OP_ColumnName, 0, 0, "cache_size"},
2082 { OP_Callback, 1, 0, 0},
2083 };
2084 Vdbe *v = sqliteGetVdbe(pParse);
2085 if( v==0 ) return;
2086 if( pRight->z==pLeft->z ){
2087 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2088 }else{
2089 int addr;
2090 int size = atoi(zRight);
2091 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002092 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002093 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2094 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2095 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2096 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2097 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2098 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2099 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002100 db->cache_size = db->cache_size<0 ? -size : size;
2101 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002102 }
2103 }else
2104
drhcd61c282002-03-06 22:01:34 +00002105 /*
2106 ** PRAGMA cache_size
2107 ** PRAGMA cache_size=N
2108 **
2109 ** The first form reports the current local setting for the
2110 ** page cache size. The local setting can be different from
2111 ** the persistent cache size value that is stored in the database
2112 ** file itself. The value returned is the maximum number of
2113 ** pages in the page cache. The second form sets the local
2114 ** page cache size value. It does not change the persistent
2115 ** cache size stored on the disk so the cache size will revert
2116 ** to its default value when the database is closed and reopened.
2117 ** N should be a positive integer.
2118 */
2119 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2120 static VdbeOp getCacheSize[] = {
2121 { OP_ColumnCount, 1, 0, 0},
2122 { OP_ColumnName, 0, 0, "cache_size"},
2123 { OP_Callback, 1, 0, 0},
2124 };
2125 Vdbe *v = sqliteGetVdbe(pParse);
2126 if( v==0 ) return;
2127 if( pRight->z==pLeft->z ){
2128 int size = db->cache_size;;
2129 if( size<0 ) size = -size;
2130 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2131 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2132 }else{
2133 int size = atoi(zRight);
2134 if( size<0 ) size = -size;
2135 if( db->cache_size<0 ) size = -size;
2136 db->cache_size = size;
2137 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2138 }
2139 }else
2140
2141 /*
2142 ** PRAGMA default_synchronous
2143 ** PRAGMA default_synchronous=BOOLEAN
2144 **
2145 ** The first form returns the persistent value of the "synchronous" setting
2146 ** that is stored in the database. This is the synchronous setting that
2147 ** is used whenever the database is opened unless overridden by a separate
2148 ** "synchronous" pragma. The second form changes the persistent and the
2149 ** local synchronous setting to the value given.
2150 **
2151 ** If synchronous is on, SQLite will do an fsync() system call at strategic
2152 ** points to insure that all previously written data has actually been
2153 ** written onto the disk surface before continuing. This mode insures that
2154 ** the database will always be in a consistent state event if the operating
2155 ** system crashes or power to the computer is interrupted unexpectedly.
2156 ** When synchronous is off, SQLite will not wait for changes to actually
2157 ** be written to the disk before continuing. As soon as it hands changes
2158 ** to the operating system, it assumes that the changes are permanent and
2159 ** it continues going. The database cannot be corrupted by a program crash
2160 ** even with synchronous off, but an operating system crash or power loss
2161 ** could potentially corrupt data. On the other hand, synchronous off is
2162 ** faster than synchronous on.
2163 */
2164 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00002165 static VdbeOp getSync[] = {
2166 { OP_Integer, 0, 0, 0},
2167 { OP_ReadCookie, 0, 2, 0},
2168 { OP_Integer, 0, 0, 0},
2169 { OP_Lt, 0, 5, 0},
2170 { OP_AddImm, 1, 0, 0},
2171 { OP_ColumnCount, 1, 0, 0},
2172 { OP_ColumnName, 0, 0, "synchronous"},
2173 { OP_Callback, 1, 0, 0},
2174 };
2175 Vdbe *v = sqliteGetVdbe(pParse);
2176 if( v==0 ) return;
2177 if( pRight->z==pLeft->z ){
2178 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2179 }else{
2180 int addr;
drhcd61c282002-03-06 22:01:34 +00002181 int size = db->cache_size;
2182 if( size<0 ) size = -size;
drhcabb0812002-09-14 13:47:32 +00002183 sqliteBeginWriteOperation(pParse, 0, 0);
drh603240c2002-03-05 01:11:12 +00002184 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002185 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2186 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2187 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2188 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002189 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
2190 if( !getBoolean(zRight) ){
2191 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002192 size = -size;
drh603240c2002-03-05 01:11:12 +00002193 }
2194 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2195 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002196 db->cache_size = size;
2197 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2198 }
2199 }else
2200
2201 /*
2202 ** PRAGMA synchronous
2203 ** PRAGMA synchronous=BOOLEAN
2204 **
2205 ** Return or set the local value of the synchronous flag. Changing
2206 ** the local value does not make changes to the disk file and the
2207 ** default value will be restored the next time the database is
2208 ** opened.
2209 */
2210 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2211 static VdbeOp getSync[] = {
2212 { OP_ColumnCount, 1, 0, 0},
2213 { OP_ColumnName, 0, 0, "synchronous"},
2214 { OP_Callback, 1, 0, 0},
2215 };
2216 Vdbe *v = sqliteGetVdbe(pParse);
2217 if( v==0 ) return;
2218 if( pRight->z==pLeft->z ){
2219 sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0);
2220 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2221 }else{
2222 int size = db->cache_size;
2223 if( size<0 ) size = -size;
2224 if( !getBoolean(zRight) ) size = -size;
2225 db->cache_size = size;
2226 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002227 }
drhf57b14a2001-09-14 18:54:08 +00002228 }else
2229
danielk1977c3f9bad2002-05-15 08:30:12 +00002230 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2231 if( getBoolean(zRight) ){
2232 always_code_trigger_setup = 1;
2233 }else{
2234 always_code_trigger_setup = 0;
2235 }
2236 }else
2237
drhf57b14a2001-09-14 18:54:08 +00002238 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2239 if( getBoolean(zRight) ){
2240 db->flags |= SQLITE_VdbeTrace;
2241 }else{
2242 db->flags &= ~SQLITE_VdbeTrace;
2243 }
2244 }else
2245
drh382c0242001-10-06 16:33:02 +00002246 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2247 if( getBoolean(zRight) ){
2248 db->flags |= SQLITE_FullColNames;
2249 }else{
2250 db->flags &= ~SQLITE_FullColNames;
2251 }
2252 }else
2253
drh5080aaa2002-07-11 12:18:16 +00002254 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2255 if( getBoolean(zRight) ){
2256 db->flags |= SQLITE_ReportTypes;
2257 }else{
2258 db->flags &= ~SQLITE_ReportTypes;
2259 }
2260 }else
2261
drhc3a64ba2001-11-22 00:01:27 +00002262 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2263 if( getBoolean(zRight) ){
2264 db->flags |= SQLITE_ResultDetails;
2265 }else{
2266 db->flags &= ~SQLITE_ResultDetails;
2267 }
2268 }else
2269
drh1bee3d72001-10-15 00:44:35 +00002270 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2271 if( getBoolean(zRight) ){
2272 db->flags |= SQLITE_CountRows;
2273 }else{
2274 db->flags &= ~SQLITE_CountRows;
2275 }
2276 }else
2277
drh6a535342001-10-19 16:44:56 +00002278 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2279 if( getBoolean(zRight) ){
2280 db->flags |= SQLITE_NullCallback;
2281 }else{
2282 db->flags &= ~SQLITE_NullCallback;
2283 }
2284 }else
2285
drh382c0242001-10-06 16:33:02 +00002286 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2287 Table *pTab;
2288 Vdbe *v;
2289 pTab = sqliteFindTable(db, zRight);
2290 if( pTab ) v = sqliteGetVdbe(pParse);
2291 if( pTab && v ){
2292 static VdbeOp tableInfoPreface[] = {
2293 { OP_ColumnCount, 5, 0, 0},
2294 { OP_ColumnName, 0, 0, "cid"},
2295 { OP_ColumnName, 1, 0, "name"},
2296 { OP_ColumnName, 2, 0, "type"},
2297 { OP_ColumnName, 3, 0, "notnull"},
2298 { OP_ColumnName, 4, 0, "dflt_value"},
2299 };
2300 int i;
2301 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002302 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002303 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002304 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2305 sqliteVdbeAddOp(v, OP_String, 0, 0);
2306 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2307 sqliteVdbeAddOp(v, OP_String, 0, 0);
2308 sqliteVdbeChangeP3(v, -1,
2309 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC);
2310 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2311 sqliteVdbeAddOp(v, OP_String, 0, 0);
2312 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2313 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002314 }
2315 }
2316 }else
2317
2318 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2319 Index *pIdx;
2320 Table *pTab;
2321 Vdbe *v;
2322 pIdx = sqliteFindIndex(db, zRight);
2323 if( pIdx ) v = sqliteGetVdbe(pParse);
2324 if( pIdx && v ){
2325 static VdbeOp tableInfoPreface[] = {
2326 { OP_ColumnCount, 3, 0, 0},
2327 { OP_ColumnName, 0, 0, "seqno"},
2328 { OP_ColumnName, 1, 0, "cid"},
2329 { OP_ColumnName, 2, 0, "name"},
2330 };
2331 int i;
2332 pTab = pIdx->pTable;
2333 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2334 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002335 int cnum = pIdx->aiColumn[i];
2336 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2337 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2338 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002339 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002340 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2341 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002342 }
2343 }
2344 }else
2345
drh81a20f22001-10-12 17:30:04 +00002346 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2347 Index *pIdx;
2348 Table *pTab;
2349 Vdbe *v;
2350 pTab = sqliteFindTable(db, zRight);
2351 if( pTab ){
2352 v = sqliteGetVdbe(pParse);
2353 pIdx = pTab->pIndex;
2354 }
2355 if( pTab && pIdx && v ){
2356 int i = 0;
2357 static VdbeOp indexListPreface[] = {
2358 { OP_ColumnCount, 3, 0, 0},
2359 { OP_ColumnName, 0, 0, "seq"},
2360 { OP_ColumnName, 1, 0, "name"},
2361 { OP_ColumnName, 2, 0, "unique"},
2362 };
2363
2364 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2365 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002366 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2367 sqliteVdbeAddOp(v, OP_String, 0, 0);
2368 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002369 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002370 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002371 ++i;
2372 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002373 }
2374 }
2375 }else
2376
drhf57b14a2001-09-14 18:54:08 +00002377#ifndef NDEBUG
2378 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2379 extern void sqliteParserTrace(FILE*, char *);
2380 if( getBoolean(zRight) ){
2381 sqliteParserTrace(stdout, "parser: ");
2382 }else{
2383 sqliteParserTrace(0, 0);
2384 }
2385 }else
2386#endif
2387
drhaaab5722002-02-19 13:39:21 +00002388 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002389 static VdbeOp checkDb[] = {
2390 { OP_SetInsert, 0, 0, "2"},
2391 { OP_Open, 0, 2, 0},
2392 { OP_Rewind, 0, 6, 0},
drh21504322002-06-25 13:16:02 +00002393 { OP_Column, 0, 3, 0}, /* 3 */
drh1bffb9c2002-02-03 17:37:36 +00002394 { OP_SetInsert, 0, 0, 0},
2395 { OP_Next, 0, 3, 0},
drh21504322002-06-25 13:16:02 +00002396 { OP_IntegrityCk, 0, 0, 0}, /* 6 */
drh1bffb9c2002-02-03 17:37:36 +00002397 { OP_ColumnCount, 1, 0, 0},
drh4ff6dfa2002-03-03 23:06:00 +00002398 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002399 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002400 { OP_SetInsert, 1, 0, "2"},
2401 { OP_OpenAux, 1, 2, 0},
2402 { OP_Rewind, 1, 16, 0},
2403 { OP_Column, 1, 3, 0}, /* 13 */
2404 { OP_SetInsert, 1, 0, 0},
2405 { OP_Next, 1, 13, 0},
2406 { OP_IntegrityCk, 1, 1, 0}, /* 16 */
2407 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002408 };
2409 Vdbe *v = sqliteGetVdbe(pParse);
2410 if( v==0 ) return;
2411 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2412 }else
drh1bffb9c2002-02-03 17:37:36 +00002413
drhf57b3392001-10-08 13:22:32 +00002414 {}
2415 sqliteFree(zLeft);
2416 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002417}