blob: 4c1102d8fcb4a8434bcb4ccb4f206fed0c7da34a [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**
drhc2eef3b2002-08-31 18:53:06 +000028** $Id: build.c,v 1.111 2002/08/31 18:53:06 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 ){
drhc977f7f2002-05-21 11:38:11 +0000401 sqliteBeginWriteOperation(pParse, 0);
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;
drhc977f7f2002-05-21 11:38:11 +00001108 sqliteBeginWriteOperation(pParse, 0);
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;
1248 pFKey->zTo = z = (char*)&pFKey[1];
1249 memcpy(z, pTo->z, pTo->n);
1250 z[pTo->n] = 0;
1251 z += pTo->n+1;
1252 pFKey->pNextTo = 0;
1253 pFKey->nCol = nCol;
1254 pFKey->aCol = (struct sColMap*)z;
1255 z += sizeof(struct sColMap)*nCol;
1256 if( pFromCol==0 ){
1257 pFKey->aCol[0].iFrom = p->nCol-1;
1258 }else{
1259 for(i=0; i<nCol; i++){
1260 int j;
1261 for(j=0; j<p->nCol; j++){
1262 if( sqliteStrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
1263 pFKey->aCol[i].iFrom = j;
1264 break;
1265 }
1266 }
1267 if( j>=p->nCol ){
1268 sqliteSetString(&pParse->zErrMsg, "unknown column \"",
1269 pFromCol->a[i].zName, "\" in foreign key definition", 0);
1270 pParse->nErr++;
1271 goto fk_end;
1272 }
1273 }
1274 }
1275 if( pToCol ){
1276 for(i=0; i<nCol; i++){
1277 int n = strlen(pToCol->a[i].zName);
1278 pFKey->aCol[i].zCol = z;
1279 memcpy(z, pToCol->a[i].zName, n);
1280 z[n] = 0;
1281 z += n+1;
1282 }
1283 }
1284 pFKey->isDeferred = 0;
1285 pFKey->deleteConf = flags & 0xff;
1286 pFKey->updateConf = (flags >> 8 ) & 0xff;
1287 pFKey->insertConf = (flags >> 16 ) & 0xff;
1288
1289 /* Link the foreign key to the table as the last step.
1290 */
1291 p->pFKey = pFKey;
1292 pFKey = 0;
1293
1294fk_end:
1295 sqliteFree(pFKey);
1296 sqliteIdListDelete(pFromCol);
1297 sqliteIdListDelete(pToCol);
1298}
1299
1300/*
1301** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
1302** clause is seen as part of a foreign key definition. The isDeferred
1303** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
1304** The behavior of the most recently created foreign key is adjusted
1305** accordingly.
1306*/
1307void sqliteDeferForeignKey(Parse *pParse, int isDeferred){
1308 Table *pTab;
1309 FKey *pFKey;
1310 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
1311 pFKey->isDeferred = isDeferred;
1312}
1313
1314/*
drh75897232000-05-29 14:26:00 +00001315** Create a new index for an SQL table. pIndex is the name of the index
1316** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001317** be NULL for a primary key or an index that is created to satisfy a
1318** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001319** as the table to be indexed. pParse->pNewTable is a table that is
1320** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001321**
drh382c0242001-10-06 16:33:02 +00001322** pList is a list of columns to be indexed. pList will be NULL if this
1323** is a primary key or unique-constraint on the most recent column added
1324** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001325*/
1326void sqliteCreateIndex(
1327 Parse *pParse, /* All information about this parse */
1328 Token *pName, /* Name of the index. May be NULL */
1329 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001330 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001331 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001332 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1333 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1334){
1335 Table *pTab; /* Table to be indexed */
1336 Index *pIndex; /* The index to be created */
1337 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001338 int i, j;
drhf57b3392001-10-08 13:22:32 +00001339 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001340 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001341 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001342
drhdaffd0e2001-04-11 14:28:42 +00001343 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1344
drh75897232000-05-29 14:26:00 +00001345 /*
1346 ** Find the table that is to be indexed. Return early if not found.
1347 */
1348 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001349 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001350 pTab = sqliteTableFromToken(pParse, pTable);
1351 }else{
drhe3c41372001-09-17 20:25:58 +00001352 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001353 pTab = pParse->pNewTable;
1354 }
1355 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1356 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001357 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1358 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001359 pParse->nErr++;
1360 goto exit_create_index;
1361 }
drha76b5df2002-02-23 02:32:10 +00001362 if( pTab->pSelect ){
1363 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1364 pParse->nErr++;
1365 goto exit_create_index;
1366 }
drh75897232000-05-29 14:26:00 +00001367
drhf57b3392001-10-08 13:22:32 +00001368 /* If this index is created while re-reading the schema from sqlite_master
1369 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001370 ** only mean that the table that this index is really associated with is
1371 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001372 ** Since its table has been suppressed, we need to also suppress the
1373 ** index.
1374 */
drhe0bc4042002-06-25 01:09:11 +00001375 if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){
drhf57b3392001-10-08 13:22:32 +00001376 goto exit_create_index;
1377 }
1378
drh75897232000-05-29 14:26:00 +00001379 /*
1380 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001381 ** index or table with the same name.
1382 **
1383 ** Exception: If we are reading the names of permanent indices from the
1384 ** sqlite_master table (because some other process changed the schema) and
1385 ** one of the index names collides with the name of a temporary table or
1386 ** index, then we will continue to process this index, but we will not
1387 ** store its name in the hash table. Set the hideName flag to accomplish
1388 ** this.
1389 **
1390 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001391 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1392 ** own name.
drh75897232000-05-29 14:26:00 +00001393 */
1394 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001395 Index *pISameName; /* Another index with the same name */
1396 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001397 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001398 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001399 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1400 if( pISameName->pTable->isTemp && pParse->initFlag ){
1401 hideName = 1;
1402 }else{
1403 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1404 " already exists", 0);
1405 pParse->nErr++;
1406 goto exit_create_index;
1407 }
drhe3c41372001-09-17 20:25:58 +00001408 }
drhf57b3392001-10-08 13:22:32 +00001409 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1410 if( pTSameName->isTemp && pParse->initFlag ){
1411 hideName = 1;
1412 }else{
1413 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1414 zName, 0);
1415 pParse->nErr++;
1416 goto exit_create_index;
1417 }
drhe3c41372001-09-17 20:25:58 +00001418 }
drh75897232000-05-29 14:26:00 +00001419 }else{
drhadbca9c2001-09-27 15:11:53 +00001420 char zBuf[30];
1421 int n;
1422 Index *pLoop;
1423 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1424 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001425 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001426 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001427 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001428 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001429 }
1430
1431 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001432 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001433 ** So create a fake list to simulate this.
1434 */
1435 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001436 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001437 nullId.n = strlen(nullId.z);
1438 pList = sqliteIdListAppend(0, &nullId);
1439 if( pList==0 ) goto exit_create_index;
1440 }
1441
1442 /*
1443 ** Allocate the index structure.
1444 */
drhdcc581c2000-05-30 13:44:19 +00001445 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001446 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001447 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001448 pIndex->aiColumn = (int*)&pIndex[1];
1449 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001450 strcpy(pIndex->zName, zName);
1451 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001452 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001453 pIndex->onError = pIndex->isUnique = onError;
drh485b39b2002-07-13 03:11:52 +00001454 pIndex->autoIndex = pName==0;
drh75897232000-05-29 14:26:00 +00001455
drh1ccde152000-06-17 13:12:39 +00001456 /* Scan the names of the columns of the table to be indexed and
1457 ** load the column indices into the Index structure. Report an error
1458 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001459 */
1460 for(i=0; i<pList->nId; i++){
1461 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001462 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001463 }
1464 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001465 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001466 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001467 pParse->nErr++;
1468 sqliteFree(pIndex);
1469 goto exit_create_index;
1470 }
drh967e8b72000-06-21 13:59:10 +00001471 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001472 }
1473
1474 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001475 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001476 */
drhf57b3392001-10-08 13:22:32 +00001477 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001478 Index *p;
1479 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1480 if( p ){
1481 assert( p==pIndex ); /* Malloc must have failed */
1482 sqliteFree(pIndex);
1483 goto exit_create_index;
1484 }
drh5e00f6c2001-09-13 13:46:56 +00001485 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001486 }
drh9cfcf5d2002-01-29 18:41:24 +00001487
1488 /* When adding an index to the list of indices for a table, make
1489 ** sure all indices labeled OE_Replace come after all those labeled
1490 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1491 ** and INSERT.
1492 */
1493 if( onError!=OE_Replace || pTab->pIndex==0
1494 || pTab->pIndex->onError==OE_Replace){
1495 pIndex->pNext = pTab->pIndex;
1496 pTab->pIndex = pIndex;
1497 }else{
1498 Index *pOther = pTab->pIndex;
1499 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1500 pOther = pOther->pNext;
1501 }
1502 pIndex->pNext = pOther->pNext;
1503 pOther->pNext = pIndex;
1504 }
drh75897232000-05-29 14:26:00 +00001505
drhd78eeee2001-09-13 16:18:53 +00001506 /* If the initFlag is 1 it means we are reading the SQL off the
1507 ** "sqlite_master" table on the disk. So do not write to the disk
1508 ** again. Extract the table number from the pParse->newTnum field.
1509 */
drhadbca9c2001-09-27 15:11:53 +00001510 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001511 pIndex->tnum = pParse->newTnum;
1512 }
1513
drh75897232000-05-29 14:26:00 +00001514 /* If the initFlag is 0 then create the index on disk. This
1515 ** involves writing the index into the master table and filling in the
1516 ** index with the current table contents.
1517 **
1518 ** The initFlag is 0 when the user first enters a CREATE INDEX
1519 ** command. The initFlag is 1 when a database is opened and
1520 ** CREATE INDEX statements are read out of the master table. In
1521 ** the latter case the index already exists on disk, which is why
1522 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001523 **
1524 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001525 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1526 ** has just been created, it contains no data and the index initialization
1527 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001528 */
drhadbca9c2001-09-27 15:11:53 +00001529 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001530 int n;
drhadbca9c2001-09-27 15:11:53 +00001531 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001532 int lbl1, lbl2;
1533 int i;
drhadbca9c2001-09-27 15:11:53 +00001534 int addr;
drhf57b3392001-10-08 13:22:32 +00001535 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001536
drhd8bc7082000-06-07 23:51:50 +00001537 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001538 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001539 if( pTable!=0 ){
drhc977f7f2002-05-21 11:38:11 +00001540 sqliteBeginWriteOperation(pParse, 0);
drhe0bc4042002-06-25 01:09:11 +00001541 sqliteOpenMasterTable(v, isTemp);
drhadbca9c2001-09-27 15:11:53 +00001542 }
drhe0bc4042002-06-25 01:09:11 +00001543 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1544 sqliteVdbeAddOp(v, OP_String, 0, 0);
1545 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1546 sqliteVdbeAddOp(v, OP_String, 0, 0);
1547 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1548 sqliteVdbeAddOp(v, OP_String, 0, 0);
1549 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh99fcd712001-10-13 01:06:47 +00001550 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1551 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001552 pIndex->tnum = 0;
1553 if( pTable ){
drhe0bc4042002-06-25 01:09:11 +00001554 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001555 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001556 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001557 }else{
drh99fcd712001-10-13 01:06:47 +00001558 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001559 }
drh5e00f6c2001-09-13 13:46:56 +00001560 }
drhe0bc4042002-06-25 01:09:11 +00001561 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
1562 if( pStart && pEnd ){
1563 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
1564 sqliteVdbeChangeP3(v, addr, pStart->z, n);
drh75897232000-05-29 14:26:00 +00001565 }
drhe0bc4042002-06-25 01:09:11 +00001566 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
1567 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhadbca9c2001-09-27 15:11:53 +00001568 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001569 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1570 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001571 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001572 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1573 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001574 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001575 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001576 }
drh99fcd712001-10-13 01:06:47 +00001577 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh491791a2002-07-18 00:34:09 +00001578 if( db->file_format>=4 ) sqliteAddIdxKeyType(v, pIndex);
drh9cfcf5d2002-01-29 18:41:24 +00001579 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001580 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001581 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001582 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1583 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001584 }
drhadbca9c2001-09-27 15:11:53 +00001585 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001586 if( !isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001587 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001588 }
drhe0bc4042002-06-25 01:09:11 +00001589 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh1c928532002-01-31 15:54:21 +00001590 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001591 }
drh75897232000-05-29 14:26:00 +00001592 }
1593
drh75897232000-05-29 14:26:00 +00001594 /* Clean up before exiting */
1595exit_create_index:
1596 sqliteIdListDelete(pList);
1597 sqliteFree(zName);
1598 return;
1599}
1600
1601/*
drh74e24cd2002-01-09 03:19:59 +00001602** This routine will drop an existing named index. This routine
1603** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001604*/
1605void sqliteDropIndex(Parse *pParse, Token *pName){
1606 Index *pIndex;
1607 char *zName;
1608 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001609 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001610
drhdaffd0e2001-04-11 14:28:42 +00001611 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001612 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001613 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001614 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001615 sqliteFree(zName);
1616 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001617 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1618 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001619 pParse->nErr++;
1620 return;
1621 }
drh485b39b2002-07-13 03:11:52 +00001622 if( pIndex->autoIndex ){
1623 sqliteSetString(&pParse->zErrMsg, "index associated with UNIQUE "
1624 "or PRIMARY KEY constraint cannot be dropped", 0);
1625 pParse->nErr++;
1626 return;
1627 }
drh75897232000-05-29 14:26:00 +00001628
1629 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001630 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001631 if( v ){
1632 static VdbeOp dropIndex[] = {
drhe0bc4042002-06-25 01:09:11 +00001633 { OP_Rewind, 0, ADDR(9), 0},
1634 { OP_String, 0, 0, 0}, /* 1 */
drh6b563442001-11-07 16:48:26 +00001635 { OP_MemStore, 1, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001636 { OP_MemLoad, 1, 0, 0}, /* 3 */
drh5e00f6c2001-09-13 13:46:56 +00001637 { OP_Column, 0, 1, 0},
drhe0bc4042002-06-25 01:09:11 +00001638 { OP_Eq, 0, ADDR(8), 0},
1639 { OP_Next, 0, ADDR(3), 0},
1640 { OP_Goto, 0, ADDR(9), 0},
1641 { OP_Delete, 0, 0, 0}, /* 8 */
drh75897232000-05-29 14:26:00 +00001642 };
1643 int base;
drhf57b3392001-10-08 13:22:32 +00001644 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001645
drhc977f7f2002-05-21 11:38:11 +00001646 sqliteBeginWriteOperation(pParse, 0);
drhe0bc4042002-06-25 01:09:11 +00001647 sqliteOpenMasterTable(v, pTab->isTemp);
1648 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
1649 sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001650 if( !pTab->isTemp ){
drhe0bc4042002-06-25 01:09:11 +00001651 sqliteChangeCookie(db, v);
drhf57b3392001-10-08 13:22:32 +00001652 }
drhe0bc4042002-06-25 01:09:11 +00001653 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001654 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001655 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001656 }
1657
drhe0bc4042002-06-25 01:09:11 +00001658 /* Delete the in-memory description of this index.
drh75897232000-05-29 14:26:00 +00001659 */
1660 if( !pParse->explain ){
drhe0bc4042002-06-25 01:09:11 +00001661 sqliteUnlinkAndDeleteIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001662 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001663 }
1664}
1665
1666/*
drh75897232000-05-29 14:26:00 +00001667** Append a new element to the given IdList. Create a new IdList if
1668** need be.
drhdaffd0e2001-04-11 14:28:42 +00001669**
1670** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001671*/
1672IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1673 if( pList==0 ){
1674 pList = sqliteMalloc( sizeof(IdList) );
1675 if( pList==0 ) return 0;
1676 }
1677 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001678 struct IdList_item *a;
1679 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1680 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001681 sqliteIdListDelete(pList);
1682 return 0;
drh75897232000-05-29 14:26:00 +00001683 }
drh6d4abfb2001-10-22 02:58:08 +00001684 pList->a = a;
drh75897232000-05-29 14:26:00 +00001685 }
1686 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1687 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001688 char **pz = &pList->a[pList->nId].zName;
1689 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1690 if( *pz==0 ){
1691 sqliteIdListDelete(pList);
1692 return 0;
1693 }else{
1694 sqliteDequote(*pz);
1695 }
drh75897232000-05-29 14:26:00 +00001696 }
1697 pList->nId++;
1698 return pList;
1699}
1700
1701/*
drhad3cab52002-05-24 02:04:32 +00001702** Append a new table name to the given SrcList. Create a new SrcList if
1703** need be. A new entry is created in the SrcList even if pToken is NULL.
1704**
1705** A new SrcList is returned, or NULL if malloc() fails.
1706*/
1707SrcList *sqliteSrcListAppend(SrcList *pList, Token *pToken){
1708 if( pList==0 ){
1709 pList = sqliteMalloc( sizeof(IdList) );
1710 if( pList==0 ) return 0;
1711 }
1712 if( (pList->nSrc & 7)==0 ){
1713 struct SrcList_item *a;
1714 a = sqliteRealloc(pList->a, (pList->nSrc+8)*sizeof(pList->a[0]) );
1715 if( a==0 ){
1716 sqliteSrcListDelete(pList);
1717 return 0;
1718 }
1719 pList->a = a;
1720 }
1721 memset(&pList->a[pList->nSrc], 0, sizeof(pList->a[0]));
1722 if( pToken ){
1723 char **pz = &pList->a[pList->nSrc].zName;
1724 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1725 if( *pz==0 ){
1726 sqliteSrcListDelete(pList);
1727 return 0;
1728 }else{
1729 sqliteDequote(*pz);
1730 }
1731 }
1732 pList->nSrc++;
1733 return pList;
1734}
1735
1736/*
drh75897232000-05-29 14:26:00 +00001737** Add an alias to the last identifier on the given identifier list.
1738*/
drhad3cab52002-05-24 02:04:32 +00001739void sqliteSrcListAddAlias(SrcList *pList, Token *pToken){
1740 if( pList && pList->nSrc>0 ){
1741 int i = pList->nSrc - 1;
drh75897232000-05-29 14:26:00 +00001742 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001743 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001744 }
1745}
1746
1747/*
drhad3cab52002-05-24 02:04:32 +00001748** Delete an IdList.
drh75897232000-05-29 14:26:00 +00001749*/
1750void sqliteIdListDelete(IdList *pList){
1751 int i;
1752 if( pList==0 ) return;
1753 for(i=0; i<pList->nId; i++){
1754 sqliteFree(pList->a[i].zName);
drhad3cab52002-05-24 02:04:32 +00001755 }
1756 sqliteFree(pList->a);
1757 sqliteFree(pList);
1758}
1759
1760/*
drhad2d8302002-05-24 20:31:36 +00001761** Return the index in pList of the identifier named zId. Return -1
1762** if not found.
1763*/
1764int sqliteIdListIndex(IdList *pList, const char *zName){
1765 int i;
1766 if( pList==0 ) return -1;
1767 for(i=0; i<pList->nId; i++){
1768 if( sqliteStrICmp(pList->a[i].zName, zName)==0 ) return i;
1769 }
1770 return -1;
1771}
1772
1773/*
drhad3cab52002-05-24 02:04:32 +00001774** Delete an entire SrcList including all its substructure.
1775*/
1776void sqliteSrcListDelete(SrcList *pList){
1777 int i;
1778 if( pList==0 ) return;
1779 for(i=0; i<pList->nSrc; i++){
1780 sqliteFree(pList->a[i].zName);
drh75897232000-05-29 14:26:00 +00001781 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001782 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001783 sqliteDeleteTable(0, pList->a[i].pTab);
1784 }
drhff78bd22002-02-27 01:47:11 +00001785 sqliteSelectDelete(pList->a[i].pSelect);
drhad3cab52002-05-24 02:04:32 +00001786 sqliteExprDelete(pList->a[i].pOn);
1787 sqliteIdListDelete(pList->a[i].pUsing);
drh75897232000-05-29 14:26:00 +00001788 }
1789 sqliteFree(pList->a);
1790 sqliteFree(pList);
1791}
1792
drh982cef72000-05-30 16:27:03 +00001793/*
1794** The COPY command is for compatibility with PostgreSQL and specificially
1795** for the ability to read the output of pg_dump. The format is as
1796** follows:
1797**
1798** COPY table FROM file [USING DELIMITERS string]
1799**
1800** "table" is an existing table name. We will read lines of code from
1801** file to fill this table with data. File might be "stdin". The optional
1802** delimiter string identifies the field separators. The default is a tab.
1803*/
1804void sqliteCopy(
1805 Parse *pParse, /* The parser context */
1806 Token *pTableName, /* The name of the table into which we will insert */
1807 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001808 Token *pDelimiter, /* Use this as the field delimiter */
1809 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001810){
1811 Table *pTab;
1812 char *zTab;
drh1c928532002-01-31 15:54:21 +00001813 int i;
drh982cef72000-05-30 16:27:03 +00001814 Vdbe *v;
1815 int addr, end;
1816 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001817 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001818
1819 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001820 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001821 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001822 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001823 if( pTab==0 ) goto copy_cleanup;
drhd8bc7082000-06-07 23:51:50 +00001824 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001825 if( v ){
drhf57b3392001-10-08 13:22:32 +00001826 int openOp;
drhc977f7f2002-05-21 11:38:11 +00001827 sqliteBeginWriteOperation(pParse, 1);
drh99fcd712001-10-13 01:06:47 +00001828 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001829 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001830 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001831 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001832 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1833 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001834 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001835 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1836 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001837 }
drhb419a922002-01-30 16:17:23 +00001838 if( db->flags & SQLITE_CountRows ){
1839 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1840 }
drh982cef72000-05-30 16:27:03 +00001841 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001842 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001843 if( pDelimiter ){
1844 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1845 sqliteVdbeDequoteP3(v, addr);
1846 }else{
1847 sqliteVdbeChangeP3(v, addr, "\t", 1);
1848 }
drh8aff1012001-12-22 14:49:24 +00001849 if( pTab->iPKey>=0 ){
1850 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1851 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1852 }else{
1853 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1854 }
drh982cef72000-05-30 16:27:03 +00001855 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001856 if( i==pTab->iPKey ){
1857 /* The integer primary key column is filled with NULL since its
1858 ** value is always pulled from the record number */
1859 sqliteVdbeAddOp(v, OP_String, 0, 0);
1860 }else{
1861 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1862 }
drh982cef72000-05-30 16:27:03 +00001863 }
drhb419a922002-01-30 16:17:23 +00001864 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1865 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1866 if( (db->flags & SQLITE_CountRows)!=0 ){
1867 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001868 }
drh99fcd712001-10-13 01:06:47 +00001869 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1870 sqliteVdbeResolveLabel(v, end);
1871 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001872 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001873 if( db->flags & SQLITE_CountRows ){
1874 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1875 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1876 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1877 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1878 }
drh982cef72000-05-30 16:27:03 +00001879 }
1880
1881copy_cleanup:
1882 return;
1883}
drhdce2cbe2000-05-31 02:27:49 +00001884
1885/*
1886** The non-standard VACUUM command is used to clean up the database,
1887** collapse free space, etc. It is modelled after the VACUUM command
1888** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001889**
drh1bffb9c2002-02-03 17:37:36 +00001890** In version 1.0.x of SQLite, the VACUUM command would call
1891** gdbm_reorganize() on all the database tables. But beginning
1892** with 2.0.0, SQLite no longer uses GDBM so this command has
1893** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001894*/
1895void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001896 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001897}
drhc4a3c772001-04-04 11:48:57 +00001898
1899/*
1900** Begin a transaction
1901*/
drh1c928532002-01-31 15:54:21 +00001902void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001903 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001904
drhc4a3c772001-04-04 11:48:57 +00001905 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001906 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001907 if( db->flags & SQLITE_InTrans ){
1908 pParse->nErr++;
1909 sqliteSetString(&pParse->zErrMsg, "cannot start a transaction "
1910 "within a transaction", 0);
1911 return;
1912 }
drhc977f7f2002-05-21 11:38:11 +00001913 sqliteBeginWriteOperation(pParse, 0);
drh5e00f6c2001-09-13 13:46:56 +00001914 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001915 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001916}
1917
1918/*
1919** Commit a transaction
1920*/
1921void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001922 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001923
drhc4a3c772001-04-04 11:48:57 +00001924 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001925 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001926 if( (db->flags & SQLITE_InTrans)==0 ){
1927 pParse->nErr++;
1928 sqliteSetString(&pParse->zErrMsg,
1929 "cannot commit - no transaction is active", 0);
1930 return;
1931 }
drh5e00f6c2001-09-13 13:46:56 +00001932 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001933 sqliteEndWriteOperation(pParse);
1934 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001935}
1936
1937/*
1938** Rollback a transaction
1939*/
1940void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001941 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001942 Vdbe *v;
1943
drhc4a3c772001-04-04 11:48:57 +00001944 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001945 if( pParse->nErr || sqlite_malloc_failed ) return;
drh6b8b8742002-08-18 20:28:06 +00001946 if( (db->flags & SQLITE_InTrans)==0 ){
1947 pParse->nErr++;
1948 sqliteSetString(&pParse->zErrMsg,
1949 "cannot rollback - no transaction is active", 0);
1950 return;
1951 }
drh5e00f6c2001-09-13 13:46:56 +00001952 v = sqliteGetVdbe(pParse);
1953 if( v ){
drh99fcd712001-10-13 01:06:47 +00001954 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001955 }
drh5e00f6c2001-09-13 13:46:56 +00001956 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001957 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001958}
drhf57b14a2001-09-14 18:54:08 +00001959
1960/*
drh1c928532002-01-31 15:54:21 +00001961** Generate VDBE code that prepares for doing an operation that
drhc977f7f2002-05-21 11:38:11 +00001962** might change the database.
1963**
1964** This routine starts a new transaction if we are not already within
1965** a transaction. If we are already within a transaction, then a checkpoint
1966** is set if the setCheckpoint parameter is true. A checkpoint should
1967** be set for operations that might fail (due to a constraint) part of
1968** the way through and which will need to undo some writes without having to
1969** rollback the whole transaction. For operations where all constraints
1970** can be checked before any changes are made to the database, it is never
1971** necessary to undo a write and the checkpoint should not be set.
drh1c928532002-01-31 15:54:21 +00001972*/
drhc977f7f2002-05-21 11:38:11 +00001973void sqliteBeginWriteOperation(Parse *pParse, int setCheckpoint){
drh663fc632002-02-02 18:49:19 +00001974 Vdbe *v;
1975 v = sqliteGetVdbe(pParse);
1976 if( v==0 ) return;
drhdc379452002-05-15 12:45:43 +00001977 if( pParse->trigStack ) return; /* if this is in a trigger */
drh663fc632002-02-02 18:49:19 +00001978 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
1979 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1980 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1981 pParse->schemaVerified = 1;
drhc977f7f2002-05-21 11:38:11 +00001982 }else if( setCheckpoint ){
drh663fc632002-02-02 18:49:19 +00001983 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1984 }
1985}
1986
1987/*
drh1c928532002-01-31 15:54:21 +00001988** Generate code that concludes an operation that may have changed
1989** the database. This is a companion function to BeginWriteOperation().
1990** If a transaction was started, then commit it. If a checkpoint was
1991** started then commit that.
1992*/
1993void sqliteEndWriteOperation(Parse *pParse){
1994 Vdbe *v;
danielk1977f29ce552002-05-19 23:43:12 +00001995 if( pParse->trigStack ) return; /* if this is in a trigger */
drh1c928532002-01-31 15:54:21 +00001996 v = sqliteGetVdbe(pParse);
1997 if( v==0 ) return;
1998 if( pParse->db->flags & SQLITE_InTrans ){
1999 /* Do Nothing */
2000 }else{
2001 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
2002 }
2003}
2004
2005
2006/*
drhf57b14a2001-09-14 18:54:08 +00002007** Interpret the given string as a boolean value.
2008*/
2009static int getBoolean(char *z){
2010 static char *azTrue[] = { "yes", "on", "true" };
2011 int i;
2012 if( z[0]==0 ) return 0;
2013 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
2014 return atoi(z);
2015 }
2016 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
2017 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
2018 }
2019 return 0;
2020}
2021
2022/*
2023** Process a pragma statement.
2024**
2025** Pragmas are of this form:
2026**
2027** PRAGMA id = value
2028**
2029** The identifier might also be a string. The value is a string, and
2030** identifier, or a number. If minusFlag is true, then the value is
2031** a number that was preceded by a minus sign.
2032*/
2033void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
2034 char *zLeft = 0;
2035 char *zRight = 0;
2036 sqlite *db = pParse->db;
2037
2038 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
2039 sqliteDequote(zLeft);
2040 if( minusFlag ){
2041 zRight = 0;
2042 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
2043 }else{
2044 zRight = sqliteStrNDup(pRight->z, pRight->n);
2045 sqliteDequote(zRight);
2046 }
2047
drhcd61c282002-03-06 22:01:34 +00002048 /*
2049 ** PRAGMA default_cache_size
2050 ** PRAGMA default_cache_size=N
2051 **
2052 ** The first form reports the current persistent setting for the
2053 ** page cache size. The value returned is the maximum number of
2054 ** pages in the page cache. The second form sets both the current
2055 ** page cache size value and the persistent page cache size value
2056 ** stored in the database file.
2057 **
2058 ** The default cache size is stored in meta-value 2 of page 1 of the
2059 ** database file. The cache size is actually the absolute value of
2060 ** this memory location. The sign of meta-value 2 determines the
2061 ** synchronous setting. A negative value means synchronous is off
2062 ** and a positive value means synchronous is on.
2063 */
2064 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00002065 static VdbeOp getCacheSize[] = {
2066 { OP_ReadCookie, 0, 2, 0},
2067 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00002068 { OP_Dup, 0, 0, 0},
2069 { OP_Integer, 0, 0, 0},
2070 { OP_Ne, 0, 6, 0},
2071 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00002072 { OP_ColumnCount, 1, 0, 0},
2073 { OP_ColumnName, 0, 0, "cache_size"},
2074 { OP_Callback, 1, 0, 0},
2075 };
2076 Vdbe *v = sqliteGetVdbe(pParse);
2077 if( v==0 ) return;
2078 if( pRight->z==pLeft->z ){
2079 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2080 }else{
2081 int addr;
2082 int size = atoi(zRight);
2083 if( size<0 ) size = -size;
drhc977f7f2002-05-21 11:38:11 +00002084 sqliteBeginWriteOperation(pParse, 0);
drh603240c2002-03-05 01:11:12 +00002085 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2086 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
2087 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2088 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
2089 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
2090 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2091 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002092 db->cache_size = db->cache_size<0 ? -size : size;
2093 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002094 }
2095 }else
2096
drhcd61c282002-03-06 22:01:34 +00002097 /*
2098 ** PRAGMA cache_size
2099 ** PRAGMA cache_size=N
2100 **
2101 ** The first form reports the current local setting for the
2102 ** page cache size. The local setting can be different from
2103 ** the persistent cache size value that is stored in the database
2104 ** file itself. The value returned is the maximum number of
2105 ** pages in the page cache. The second form sets the local
2106 ** page cache size value. It does not change the persistent
2107 ** cache size stored on the disk so the cache size will revert
2108 ** to its default value when the database is closed and reopened.
2109 ** N should be a positive integer.
2110 */
2111 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
2112 static VdbeOp getCacheSize[] = {
2113 { OP_ColumnCount, 1, 0, 0},
2114 { OP_ColumnName, 0, 0, "cache_size"},
2115 { OP_Callback, 1, 0, 0},
2116 };
2117 Vdbe *v = sqliteGetVdbe(pParse);
2118 if( v==0 ) return;
2119 if( pRight->z==pLeft->z ){
2120 int size = db->cache_size;;
2121 if( size<0 ) size = -size;
2122 sqliteVdbeAddOp(v, OP_Integer, size, 0);
2123 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
2124 }else{
2125 int size = atoi(zRight);
2126 if( size<0 ) size = -size;
2127 if( db->cache_size<0 ) size = -size;
2128 db->cache_size = size;
2129 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2130 }
2131 }else
2132
2133 /*
2134 ** PRAGMA default_synchronous
2135 ** PRAGMA default_synchronous=BOOLEAN
2136 **
2137 ** The first form returns the persistent value of the "synchronous" setting
2138 ** that is stored in the database. This is the synchronous setting that
2139 ** is used whenever the database is opened unless overridden by a separate
2140 ** "synchronous" pragma. The second form changes the persistent and the
2141 ** local synchronous setting to the value given.
2142 **
2143 ** If synchronous is on, SQLite will do an fsync() system call at strategic
2144 ** points to insure that all previously written data has actually been
2145 ** written onto the disk surface before continuing. This mode insures that
2146 ** the database will always be in a consistent state event if the operating
2147 ** system crashes or power to the computer is interrupted unexpectedly.
2148 ** When synchronous is off, SQLite will not wait for changes to actually
2149 ** be written to the disk before continuing. As soon as it hands changes
2150 ** to the operating system, it assumes that the changes are permanent and
2151 ** it continues going. The database cannot be corrupted by a program crash
2152 ** even with synchronous off, but an operating system crash or power loss
2153 ** could potentially corrupt data. On the other hand, synchronous off is
2154 ** faster than synchronous on.
2155 */
2156 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00002157 static VdbeOp getSync[] = {
2158 { OP_Integer, 0, 0, 0},
2159 { OP_ReadCookie, 0, 2, 0},
2160 { OP_Integer, 0, 0, 0},
2161 { OP_Lt, 0, 5, 0},
2162 { OP_AddImm, 1, 0, 0},
2163 { OP_ColumnCount, 1, 0, 0},
2164 { OP_ColumnName, 0, 0, "synchronous"},
2165 { OP_Callback, 1, 0, 0},
2166 };
2167 Vdbe *v = sqliteGetVdbe(pParse);
2168 if( v==0 ) return;
2169 if( pRight->z==pLeft->z ){
2170 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2171 }else{
2172 int addr;
drhcd61c282002-03-06 22:01:34 +00002173 int size = db->cache_size;
2174 if( size<0 ) size = -size;
drhc977f7f2002-05-21 11:38:11 +00002175 sqliteBeginWriteOperation(pParse, 0);
drh603240c2002-03-05 01:11:12 +00002176 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00002177 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
2178 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
2179 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
2180 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00002181 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
2182 if( !getBoolean(zRight) ){
2183 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00002184 size = -size;
drh603240c2002-03-05 01:11:12 +00002185 }
2186 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
2187 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00002188 db->cache_size = size;
2189 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
2190 }
2191 }else
2192
2193 /*
2194 ** PRAGMA synchronous
2195 ** PRAGMA synchronous=BOOLEAN
2196 **
2197 ** Return or set the local value of the synchronous flag. Changing
2198 ** the local value does not make changes to the disk file and the
2199 ** default value will be restored the next time the database is
2200 ** opened.
2201 */
2202 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
2203 static VdbeOp getSync[] = {
2204 { OP_ColumnCount, 1, 0, 0},
2205 { OP_ColumnName, 0, 0, "synchronous"},
2206 { OP_Callback, 1, 0, 0},
2207 };
2208 Vdbe *v = sqliteGetVdbe(pParse);
2209 if( v==0 ) return;
2210 if( pRight->z==pLeft->z ){
2211 sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0);
2212 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
2213 }else{
2214 int size = db->cache_size;
2215 if( size<0 ) size = -size;
2216 if( !getBoolean(zRight) ) size = -size;
2217 db->cache_size = size;
2218 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00002219 }
drhf57b14a2001-09-14 18:54:08 +00002220 }else
2221
danielk1977c3f9bad2002-05-15 08:30:12 +00002222 if( sqliteStrICmp(zLeft, "trigger_overhead_test")==0 ){
2223 if( getBoolean(zRight) ){
2224 always_code_trigger_setup = 1;
2225 }else{
2226 always_code_trigger_setup = 0;
2227 }
2228 }else
2229
drhf57b14a2001-09-14 18:54:08 +00002230 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
2231 if( getBoolean(zRight) ){
2232 db->flags |= SQLITE_VdbeTrace;
2233 }else{
2234 db->flags &= ~SQLITE_VdbeTrace;
2235 }
2236 }else
2237
drh382c0242001-10-06 16:33:02 +00002238 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
2239 if( getBoolean(zRight) ){
2240 db->flags |= SQLITE_FullColNames;
2241 }else{
2242 db->flags &= ~SQLITE_FullColNames;
2243 }
2244 }else
2245
drh5080aaa2002-07-11 12:18:16 +00002246 if( sqliteStrICmp(zLeft, "show_datatypes")==0 ){
2247 if( getBoolean(zRight) ){
2248 db->flags |= SQLITE_ReportTypes;
2249 }else{
2250 db->flags &= ~SQLITE_ReportTypes;
2251 }
2252 }else
2253
drhc3a64ba2001-11-22 00:01:27 +00002254 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
2255 if( getBoolean(zRight) ){
2256 db->flags |= SQLITE_ResultDetails;
2257 }else{
2258 db->flags &= ~SQLITE_ResultDetails;
2259 }
2260 }else
2261
drh1bee3d72001-10-15 00:44:35 +00002262 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
2263 if( getBoolean(zRight) ){
2264 db->flags |= SQLITE_CountRows;
2265 }else{
2266 db->flags &= ~SQLITE_CountRows;
2267 }
2268 }else
2269
drh6a535342001-10-19 16:44:56 +00002270 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
2271 if( getBoolean(zRight) ){
2272 db->flags |= SQLITE_NullCallback;
2273 }else{
2274 db->flags &= ~SQLITE_NullCallback;
2275 }
2276 }else
2277
drh382c0242001-10-06 16:33:02 +00002278 if( sqliteStrICmp(zLeft, "table_info")==0 ){
2279 Table *pTab;
2280 Vdbe *v;
2281 pTab = sqliteFindTable(db, zRight);
2282 if( pTab ) v = sqliteGetVdbe(pParse);
2283 if( pTab && v ){
2284 static VdbeOp tableInfoPreface[] = {
2285 { OP_ColumnCount, 5, 0, 0},
2286 { OP_ColumnName, 0, 0, "cid"},
2287 { OP_ColumnName, 1, 0, "name"},
2288 { OP_ColumnName, 2, 0, "type"},
2289 { OP_ColumnName, 3, 0, "notnull"},
2290 { OP_ColumnName, 4, 0, "dflt_value"},
2291 };
2292 int i;
2293 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00002294 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00002295 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00002296 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2297 sqliteVdbeAddOp(v, OP_String, 0, 0);
2298 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
2299 sqliteVdbeAddOp(v, OP_String, 0, 0);
2300 sqliteVdbeChangeP3(v, -1,
2301 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC);
2302 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
2303 sqliteVdbeAddOp(v, OP_String, 0, 0);
2304 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
2305 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00002306 }
2307 }
2308 }else
2309
2310 if( sqliteStrICmp(zLeft, "index_info")==0 ){
2311 Index *pIdx;
2312 Table *pTab;
2313 Vdbe *v;
2314 pIdx = sqliteFindIndex(db, zRight);
2315 if( pIdx ) v = sqliteGetVdbe(pParse);
2316 if( pIdx && v ){
2317 static VdbeOp tableInfoPreface[] = {
2318 { OP_ColumnCount, 3, 0, 0},
2319 { OP_ColumnName, 0, 0, "seqno"},
2320 { OP_ColumnName, 1, 0, "cid"},
2321 { OP_ColumnName, 2, 0, "name"},
2322 };
2323 int i;
2324 pTab = pIdx->pTable;
2325 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2326 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002327 int cnum = pIdx->aiColumn[i];
2328 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2329 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2330 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002331 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002332 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2333 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002334 }
2335 }
2336 }else
2337
drh81a20f22001-10-12 17:30:04 +00002338 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2339 Index *pIdx;
2340 Table *pTab;
2341 Vdbe *v;
2342 pTab = sqliteFindTable(db, zRight);
2343 if( pTab ){
2344 v = sqliteGetVdbe(pParse);
2345 pIdx = pTab->pIndex;
2346 }
2347 if( pTab && pIdx && v ){
2348 int i = 0;
2349 static VdbeOp indexListPreface[] = {
2350 { OP_ColumnCount, 3, 0, 0},
2351 { OP_ColumnName, 0, 0, "seq"},
2352 { OP_ColumnName, 1, 0, "name"},
2353 { OP_ColumnName, 2, 0, "unique"},
2354 };
2355
2356 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2357 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002358 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2359 sqliteVdbeAddOp(v, OP_String, 0, 0);
2360 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002361 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002362 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh9adf9ac2002-05-15 11:44:13 +00002363 ++i;
2364 pIdx = pIdx->pNext;
drh81a20f22001-10-12 17:30:04 +00002365 }
2366 }
2367 }else
2368
drhf57b14a2001-09-14 18:54:08 +00002369#ifndef NDEBUG
2370 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2371 extern void sqliteParserTrace(FILE*, char *);
2372 if( getBoolean(zRight) ){
2373 sqliteParserTrace(stdout, "parser: ");
2374 }else{
2375 sqliteParserTrace(0, 0);
2376 }
2377 }else
2378#endif
2379
drhaaab5722002-02-19 13:39:21 +00002380 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002381 static VdbeOp checkDb[] = {
2382 { OP_SetInsert, 0, 0, "2"},
2383 { OP_Open, 0, 2, 0},
2384 { OP_Rewind, 0, 6, 0},
drh21504322002-06-25 13:16:02 +00002385 { OP_Column, 0, 3, 0}, /* 3 */
drh1bffb9c2002-02-03 17:37:36 +00002386 { OP_SetInsert, 0, 0, 0},
2387 { OP_Next, 0, 3, 0},
drh21504322002-06-25 13:16:02 +00002388 { OP_IntegrityCk, 0, 0, 0}, /* 6 */
drh1bffb9c2002-02-03 17:37:36 +00002389 { OP_ColumnCount, 1, 0, 0},
drh4ff6dfa2002-03-03 23:06:00 +00002390 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002391 { OP_Callback, 1, 0, 0},
drh21504322002-06-25 13:16:02 +00002392 { OP_SetInsert, 1, 0, "2"},
2393 { OP_OpenAux, 1, 2, 0},
2394 { OP_Rewind, 1, 16, 0},
2395 { OP_Column, 1, 3, 0}, /* 13 */
2396 { OP_SetInsert, 1, 0, 0},
2397 { OP_Next, 1, 13, 0},
2398 { OP_IntegrityCk, 1, 1, 0}, /* 16 */
2399 { OP_Callback, 1, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002400 };
2401 Vdbe *v = sqliteGetVdbe(pParse);
2402 if( v==0 ) return;
2403 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2404 }else
drh1bffb9c2002-02-03 17:37:36 +00002405
drhf57b3392001-10-08 13:22:32 +00002406 {}
2407 sqliteFree(zLeft);
2408 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002409}