blob: 108bb4bdf3f4251fa0a55b892e108a23e8e25454 [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**
drhcd61c282002-03-06 22:01:34 +000028** $Id: build.c,v 1.86 2002/03/06 22:01:36 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/*
34** This routine is called after a single SQL statement has been
drh1ccde152000-06-17 13:12:39 +000035** parsed and we want to execute the VDBE code to implement
36** that statement. Prior action routines should have already
drh75897232000-05-29 14:26:00 +000037** constructed VDBE code to do the work of the SQL statement.
38** This routine just has to execute the VDBE code.
39**
40** Note that if an error occurred, it might be the case that
41** no VDBE code was generated.
42*/
43void sqliteExec(Parse *pParse){
drh4c504392000-10-16 22:06:40 +000044 int rc = SQLITE_OK;
drhbe0072d2001-09-13 14:46:09 +000045 sqlite *db = pParse->db;
drhdaffd0e2001-04-11 14:28:42 +000046 if( sqlite_malloc_failed ) return;
drh3fc190c2001-09-14 03:24:23 +000047 if( pParse->pVdbe && pParse->nErr==0 ){
drh75897232000-05-29 14:26:00 +000048 if( pParse->explain ){
drh4c504392000-10-16 22:06:40 +000049 rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg,
50 &pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +000051 }else{
drh3fc190c2001-09-14 03:24:23 +000052 FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
drh75897232000-05-29 14:26:00 +000053 sqliteVdbeTrace(pParse->pVdbe, trace);
drh4c504392000-10-16 22:06:40 +000054 rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg,
drhbe0072d2001-09-13 14:46:09 +000055 &pParse->zErrMsg, db->pBusyArg,
56 db->xBusyCallback);
drhecdc7532001-09-23 02:35:53 +000057 if( rc ) pParse->nErr++;
drh75897232000-05-29 14:26:00 +000058 }
59 sqliteVdbeDelete(pParse->pVdbe);
60 pParse->pVdbe = 0;
drhd8bc7082000-06-07 23:51:50 +000061 pParse->colNamesSet = 0;
drh4c504392000-10-16 22:06:40 +000062 pParse->rc = rc;
drh50e5dad2001-09-15 00:57:28 +000063 pParse->schemaVerified = 0;
drh75897232000-05-29 14:26:00 +000064 }
65}
66
67/*
drhf57b3392001-10-08 13:22:32 +000068** Locate the in-memory structure that describes
69** a particular database table given the name
drh75897232000-05-29 14:26:00 +000070** of that table. Return NULL if not found.
71*/
drha76b5df2002-02-23 02:32:10 +000072Table *sqliteFindTable(sqlite *db, const char *zName){
drhafa4a022001-09-24 03:12:39 +000073 Table *p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +000074 return p;
drh75897232000-05-29 14:26:00 +000075}
76
77/*
drhf57b3392001-10-08 13:22:32 +000078** Locate the in-memory structure that describes
79** a particular index given the name of that index.
80** Return NULL if not found.
drh75897232000-05-29 14:26:00 +000081*/
drha76b5df2002-02-23 02:32:10 +000082Index *sqliteFindIndex(sqlite *db, const char *zName){
drhafa4a022001-09-24 03:12:39 +000083 Index *p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
drh74e24cd2002-01-09 03:19:59 +000084 return p;
drh75897232000-05-29 14:26:00 +000085}
86
87/*
88** Remove the given index from the index hash table, and free
89** its memory structures.
90**
drhd229ca92002-01-09 13:30:41 +000091** The index is removed from the database hash tables but
92** it is not unlinked from the Table that it indexes.
drhdaffd0e2001-04-11 14:28:42 +000093** Unlinking from the Table must be done by the calling function.
drh75897232000-05-29 14:26:00 +000094*/
drh74e24cd2002-01-09 03:19:59 +000095static void sqliteDeleteIndex(sqlite *db, Index *p){
drhd229ca92002-01-09 13:30:41 +000096 Index *pOld;
97 assert( db!=0 && p->zName!=0 );
98 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
99 if( pOld!=0 && pOld!=p ){
100 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
drh75897232000-05-29 14:26:00 +0000101 }
drhd229ca92002-01-09 13:30:41 +0000102 sqliteHashInsert(&db->idxDrop, p, 0, 0);
drh74e24cd2002-01-09 03:19:59 +0000103 sqliteFree(p);
drh75897232000-05-29 14:26:00 +0000104}
105
106/*
drhbeae3192001-09-22 18:12:08 +0000107** Unlink the given index from its table, then remove
drhf57b3392001-10-08 13:22:32 +0000108** the index from the index hash table and free its memory
drh5e00f6c2001-09-13 13:46:56 +0000109** structures.
110*/
drh6d4abfb2001-10-22 02:58:08 +0000111void sqliteUnlinkAndDeleteIndex(sqlite *db, Index *pIndex){
drh5e00f6c2001-09-13 13:46:56 +0000112 if( pIndex->pTable->pIndex==pIndex ){
113 pIndex->pTable->pIndex = pIndex->pNext;
114 }else{
115 Index *p;
116 for(p=pIndex->pTable->pIndex; p && p->pNext!=pIndex; p=p->pNext){}
117 if( p && p->pNext==pIndex ){
118 p->pNext = pIndex->pNext;
119 }
120 }
121 sqliteDeleteIndex(db, pIndex);
122}
123
124/*
drh74e24cd2002-01-09 03:19:59 +0000125** Move the given index to the pending DROP INDEX queue if it has
126** been committed. If this index was never committed, then just
127** delete it.
128**
129** Indices on the pending drop queue are deleted when a COMMIT is
130** executed. If a ROLLBACK occurs, the indices are moved back into
131** the main index hash table.
132*/
drhda9e0342002-01-10 14:31:48 +0000133static void sqlitePendingDropIndex(sqlite *db, Index *p){
drh74e24cd2002-01-09 03:19:59 +0000134 if( !p->isCommit ){
135 sqliteUnlinkAndDeleteIndex(db, p);
136 }else{
137 Index *pOld;
138 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
drhd229ca92002-01-09 13:30:41 +0000139 if( pOld!=0 && pOld!=p ){
140 sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
141 }
drh74e24cd2002-01-09 03:19:59 +0000142 sqliteHashInsert(&db->idxDrop, p, 0, p);
143 p->isDropped = 1;
144 }
145}
146
147/*
drh75897232000-05-29 14:26:00 +0000148** Remove the memory data structures associated with the given
drh967e8b72000-06-21 13:59:10 +0000149** Table. No changes are made to disk by this routine.
drh75897232000-05-29 14:26:00 +0000150**
151** This routine just deletes the data structure. It does not unlink
drhd9b02572001-04-15 00:37:09 +0000152** the table data structure from the hash table. But it does destroy
drh75897232000-05-29 14:26:00 +0000153** memory structures of the indices associated with the table.
drhdaffd0e2001-04-11 14:28:42 +0000154**
155** Indices associated with the table are unlinked from the "db"
156** data structure if db!=NULL. If db==NULL, indices attached to
157** the table are deleted, but it is assumed they have already been
158** unlinked.
drh75897232000-05-29 14:26:00 +0000159*/
160void sqliteDeleteTable(sqlite *db, Table *pTable){
161 int i;
162 Index *pIndex, *pNext;
163 if( pTable==0 ) return;
164 for(i=0; i<pTable->nCol; i++){
drh7020f652000-06-03 18:06:52 +0000165 sqliteFree(pTable->aCol[i].zName);
166 sqliteFree(pTable->aCol[i].zDflt);
drh382c0242001-10-06 16:33:02 +0000167 sqliteFree(pTable->aCol[i].zType);
drh75897232000-05-29 14:26:00 +0000168 }
169 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
170 pNext = pIndex->pNext;
171 sqliteDeleteIndex(db, pIndex);
172 }
drh6e142f52000-06-08 13:36:40 +0000173 sqliteFree(pTable->zName);
drh7020f652000-06-03 18:06:52 +0000174 sqliteFree(pTable->aCol);
drha76b5df2002-02-23 02:32:10 +0000175 sqliteSelectDelete(pTable->pSelect);
drh75897232000-05-29 14:26:00 +0000176 sqliteFree(pTable);
177}
178
179/*
drh5edc3122001-09-13 21:53:09 +0000180** Unlink the given table from the hash tables and the delete the
drh74e24cd2002-01-09 03:19:59 +0000181** table structure with all its indices.
drh5edc3122001-09-13 21:53:09 +0000182*/
drh74e24cd2002-01-09 03:19:59 +0000183static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
drhd229ca92002-01-09 13:30:41 +0000184 Table *pOld;
185 assert( db!=0 );
186 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
187 assert( pOld==0 || pOld==p );
188 sqliteHashInsert(&db->tblDrop, p, 0, 0);
drh74e24cd2002-01-09 03:19:59 +0000189 sqliteDeleteTable(db, p);
190}
191
192/*
193** Move the given table to the pending DROP TABLE queue if it has
194** been committed. If this table was never committed, then just
195** delete it. Do the same for all its indices.
196**
197** Table on the drop queue are not actually deleted until a COMMIT
198** statement is executed. If a ROLLBACK occurs instead of a COMMIT,
199** then the tables on the drop queue are moved back into the main
200** hash table.
201*/
drhda9e0342002-01-10 14:31:48 +0000202static void sqlitePendingDropTable(sqlite *db, Table *pTbl){
drh74e24cd2002-01-09 03:19:59 +0000203 if( !pTbl->isCommit ){
204 sqliteUnlinkAndDeleteTable(db, pTbl);
205 }else{
206 Table *pOld;
207 Index *pIndex, *pNext;
208 pOld = sqliteHashInsert(&db->tblHash, pTbl->zName, strlen(pTbl->zName)+1,0);
209 assert( pOld==pTbl );
210 sqliteHashInsert(&db->tblDrop, pTbl, 0, pTbl);
211 for(pIndex = pTbl->pIndex; pIndex; pIndex=pNext){
212 pNext = pIndex->pNext;
213 sqlitePendingDropIndex(db, pIndex);
214 }
215 }
drh5edc3122001-09-13 21:53:09 +0000216}
217
218/*
drh5e00f6c2001-09-13 13:46:56 +0000219** Check all Tables and Indexes in the internal hash table and commit
220** any additions or deletions to those hash tables.
221**
222** When executing CREATE TABLE and CREATE INDEX statements, the Table
223** and Index structures are created and added to the hash tables, but
224** the "isCommit" field is not set. This routine sets those fields.
drh74e24cd2002-01-09 03:19:59 +0000225** When executing DROP TABLE and DROP INDEX, the table or index structures
226** are moved out of tblHash and idxHash into tblDrop and idxDrop. This
227** routine deletes the structure in tblDrop and idxDrop.
drh5e00f6c2001-09-13 13:46:56 +0000228**
229** See also: sqliteRollbackInternalChanges()
230*/
231void sqliteCommitInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000232 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000233 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drh50e5dad2001-09-15 00:57:28 +0000234 db->schema_cookie = db->next_cookie;
drhbeae3192001-09-22 18:12:08 +0000235 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
236 Table *pTable = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000237 pTable->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000238 }
drh74e24cd2002-01-09 03:19:59 +0000239 for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
drhbeae3192001-09-22 18:12:08 +0000240 Table *pTable = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000241 sqliteDeleteTable(db, pTable);
drhbeae3192001-09-22 18:12:08 +0000242 }
drh74e24cd2002-01-09 03:19:59 +0000243 sqliteHashClear(&db->tblDrop);
drhbeae3192001-09-22 18:12:08 +0000244 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
drh4a324312001-12-21 14:30:42 +0000245 Index *pIndex = sqliteHashData(pElem);
drh74e24cd2002-01-09 03:19:59 +0000246 pIndex->isCommit = 1;
drh5e00f6c2001-09-13 13:46:56 +0000247 }
drh74e24cd2002-01-09 03:19:59 +0000248 while( (pElem=sqliteHashFirst(&db->idxDrop))!=0 ){
drhbeae3192001-09-22 18:12:08 +0000249 Index *pIndex = sqliteHashData(pElem);
250 sqliteUnlinkAndDeleteIndex(db, pIndex);
251 }
drh74e24cd2002-01-09 03:19:59 +0000252 sqliteHashClear(&db->idxDrop);
drh5e00f6c2001-09-13 13:46:56 +0000253 db->flags &= ~SQLITE_InternChanges;
254}
255
256/*
257** This routine runs when one or more CREATE TABLE, CREATE INDEX,
drhf57b3392001-10-08 13:22:32 +0000258** DROP TABLE, or DROP INDEX statements gets rolled back. The
drh5e00f6c2001-09-13 13:46:56 +0000259** additions or deletions of Table and Index structures in the
260** internal hash tables are undone.
261**
262** See also: sqliteCommitInternalChanges()
263*/
264void sqliteRollbackInternalChanges(sqlite *db){
drhbeae3192001-09-22 18:12:08 +0000265 Hash toDelete;
266 HashElem *pElem;
drh5e00f6c2001-09-13 13:46:56 +0000267 if( (db->flags & SQLITE_InternChanges)==0 ) return;
drhbeae3192001-09-22 18:12:08 +0000268 sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
drh50e5dad2001-09-15 00:57:28 +0000269 db->next_cookie = db->schema_cookie;
drhbeae3192001-09-22 18:12:08 +0000270 for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
271 Table *pTable = sqliteHashData(pElem);
272 if( !pTable->isCommit ){
273 sqliteHashInsert(&toDelete, pTable, 0, pTable);
drh5e00f6c2001-09-13 13:46:56 +0000274 }
275 }
drhbeae3192001-09-22 18:12:08 +0000276 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
277 Table *pTable = sqliteHashData(pElem);
278 sqliteUnlinkAndDeleteTable(db, pTable);
279 }
280 sqliteHashClear(&toDelete);
drh74e24cd2002-01-09 03:19:59 +0000281 for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
282 Table *pOld, *p = sqliteHashData(pElem);
283 assert( p->isCommit );
284 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
285 assert( pOld==0 || pOld==p );
286 }
287 sqliteHashClear(&db->tblDrop);
drhbeae3192001-09-22 18:12:08 +0000288 for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
drh4a324312001-12-21 14:30:42 +0000289 Index *pIndex = sqliteHashData(pElem);
drhbeae3192001-09-22 18:12:08 +0000290 if( !pIndex->isCommit ){
291 sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
drh5e00f6c2001-09-13 13:46:56 +0000292 }
293 }
drhbeae3192001-09-22 18:12:08 +0000294 for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
295 Index *pIndex = sqliteHashData(pElem);
296 sqliteUnlinkAndDeleteIndex(db, pIndex);
297 }
298 sqliteHashClear(&toDelete);
drh74e24cd2002-01-09 03:19:59 +0000299 for(pElem=sqliteHashFirst(&db->idxDrop); pElem; pElem=sqliteHashNext(pElem)){
300 Index *pOld, *p = sqliteHashData(pElem);
301 assert( p->isCommit );
302 p->isDropped = 0;
303 pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, p);
304 assert( pOld==0 || pOld==p );
305 }
306 sqliteHashClear(&db->idxDrop);
drh5e00f6c2001-09-13 13:46:56 +0000307 db->flags &= ~SQLITE_InternChanges;
308}
309
310/*
drh1ccde152000-06-17 13:12:39 +0000311** Construct the name of a user table or index from a token.
drh75897232000-05-29 14:26:00 +0000312**
313** Space to hold the name is obtained from sqliteMalloc() and must
314** be freed by the calling function.
315*/
drhcce7d172000-05-31 15:34:51 +0000316char *sqliteTableNameFromToken(Token *pName){
drh6e142f52000-06-08 13:36:40 +0000317 char *zName = sqliteStrNDup(pName->z, pName->n);
drh982cef72000-05-30 16:27:03 +0000318 sqliteDequote(zName);
drh75897232000-05-29 14:26:00 +0000319 return zName;
320}
321
322/*
323** Begin constructing a new table representation in memory. This is
324** the first of several action routines that get called in response
drhd9b02572001-04-15 00:37:09 +0000325** to a CREATE TABLE statement. In particular, this routine is called
326** after seeing tokens "CREATE" and "TABLE" and the table name. The
drhf57b3392001-10-08 13:22:32 +0000327** pStart token is the CREATE and pName is the table name. The isTemp
328** flag is true if the "TEMP" or "TEMPORARY" keyword occurs in between
329** CREATE and TABLE.
drhd9b02572001-04-15 00:37:09 +0000330**
drhf57b3392001-10-08 13:22:32 +0000331** The new table record is initialized and put in pParse->pNewTable.
332** As more of the CREATE TABLE statement is parsed, additional action
333** routines will be called to add more information to this record.
334** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
335** is called to complete the construction of the new table record.
drh75897232000-05-29 14:26:00 +0000336*/
drhf57b3392001-10-08 13:22:32 +0000337void sqliteStartTable(Parse *pParse, Token *pStart, Token *pName, int isTemp){
drh75897232000-05-29 14:26:00 +0000338 Table *pTable;
drhf57b3392001-10-08 13:22:32 +0000339 Index *pIdx;
drh75897232000-05-29 14:26:00 +0000340 char *zName;
drhbe0072d2001-09-13 14:46:09 +0000341 sqlite *db = pParse->db;
drhadbca9c2001-09-27 15:11:53 +0000342 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000343
344 pParse->sFirstToken = *pStart;
345 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +0000346 if( zName==0 ) return;
drhf57b3392001-10-08 13:22:32 +0000347
348 /* Before trying to create a temporary table, make sure the Btree for
349 ** holding temporary tables is open.
350 */
351 if( isTemp && db->pBeTemp==0 ){
352 int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
353 if( rc!=SQLITE_OK ){
354 sqliteSetNString(&pParse->zErrMsg, "unable to open a temporary database "
355 "file for storing temporary tables", 0);
356 pParse->nErr++;
357 return;
358 }
359 if( db->flags & SQLITE_InTrans ){
360 rc = sqliteBtreeBeginTrans(db->pBeTemp);
361 if( rc!=SQLITE_OK ){
362 sqliteSetNString(&pParse->zErrMsg, "unable to get a write lock on "
drh1c928532002-01-31 15:54:21 +0000363 "the temporary database file", 0);
drhf57b3392001-10-08 13:22:32 +0000364 pParse->nErr++;
365 return;
366 }
367 }
368 }
369
370 /* Make sure the new table name does not collide with an existing
371 ** index or table name. Issue an error message if it does.
372 **
373 ** If we are re-reading the sqlite_master table because of a schema
374 ** change and a new permanent table is found whose name collides with
375 ** an existing temporary table, then ignore the new permanent table.
376 ** We will continue parsing, but the pParse->nameClash flag will be set
377 ** so we will know to discard the table record once parsing has finished.
378 */
drhbe0072d2001-09-13 14:46:09 +0000379 pTable = sqliteFindTable(db, zName);
drh75897232000-05-29 14:26:00 +0000380 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +0000381 if( pTable->isTemp && pParse->initFlag ){
382 pParse->nameClash = 1;
383 }else{
384 sqliteSetNString(&pParse->zErrMsg, "table ", 0, pName->z, pName->n,
385 " already exists", 0, 0);
386 sqliteFree(zName);
387 pParse->nErr++;
388 return;
389 }
390 }else{
391 pParse->nameClash = 0;
drh75897232000-05-29 14:26:00 +0000392 }
drhf57b3392001-10-08 13:22:32 +0000393 if( (pIdx = sqliteFindIndex(db, zName))!=0 &&
394 (!pIdx->pTable->isTemp || !pParse->initFlag) ){
drh1d37e282000-05-30 03:12:21 +0000395 sqliteSetString(&pParse->zErrMsg, "there is already an index named ",
396 zName, 0);
drh75897232000-05-29 14:26:00 +0000397 sqliteFree(zName);
398 pParse->nErr++;
399 return;
400 }
401 pTable = sqliteMalloc( sizeof(Table) );
drh6d4abfb2001-10-22 02:58:08 +0000402 if( pTable==0 ){
403 sqliteFree(zName);
404 return;
405 }
drh75897232000-05-29 14:26:00 +0000406 pTable->zName = zName;
drh75897232000-05-29 14:26:00 +0000407 pTable->nCol = 0;
drh7020f652000-06-03 18:06:52 +0000408 pTable->aCol = 0;
drh4a324312001-12-21 14:30:42 +0000409 pTable->iPKey = -1;
drh75897232000-05-29 14:26:00 +0000410 pTable->pIndex = 0;
drhf57b3392001-10-08 13:22:32 +0000411 pTable->isTemp = isTemp;
drhbe0072d2001-09-13 14:46:09 +0000412 if( pParse->pNewTable ) sqliteDeleteTable(db, pParse->pNewTable);
drh75897232000-05-29 14:26:00 +0000413 pParse->pNewTable = pTable;
drh17f71932002-02-21 12:01:27 +0000414
415 /* Begin generating the code that will insert the table record into
416 ** the SQLITE_MASTER table. Note in particular that we must go ahead
417 ** and allocate the record number for the table entry now. Before any
418 ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause
419 ** indices to be created and the table record must come before the
420 ** indices. Hence, the record number for the table must be allocated
421 ** now.
422 */
drhadbca9c2001-09-27 15:11:53 +0000423 if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
drh1c928532002-01-31 15:54:21 +0000424 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +0000425 if( !isTemp ){
drh603240c2002-03-05 01:11:12 +0000426 sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
427 sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
drh99fcd712001-10-13 01:06:47 +0000428 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
429 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
drh17f71932002-02-21 12:01:27 +0000430 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
431 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
432 sqliteVdbeAddOp(v, OP_String, 0, 0);
433 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000434 }
drh5e00f6c2001-09-13 13:46:56 +0000435 }
drh75897232000-05-29 14:26:00 +0000436}
437
438/*
439** Add a new column to the table currently being constructed.
drhd9b02572001-04-15 00:37:09 +0000440**
441** The parser calls this routine once for each column declaration
442** in a CREATE TABLE statement. sqliteStartTable() gets called
443** first to get things going. Then this routine is called for each
444** column.
drh75897232000-05-29 14:26:00 +0000445*/
446void sqliteAddColumn(Parse *pParse, Token *pName){
447 Table *p;
448 char **pz;
449 if( (p = pParse->pNewTable)==0 ) return;
450 if( (p->nCol & 0x7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000451 Column *aNew;
452 aNew = sqliteRealloc( p->aCol, (p->nCol+8)*sizeof(p->aCol[0]));
453 if( aNew==0 ) return;
454 p->aCol = aNew;
drh75897232000-05-29 14:26:00 +0000455 }
drh7020f652000-06-03 18:06:52 +0000456 memset(&p->aCol[p->nCol], 0, sizeof(p->aCol[0]));
457 pz = &p->aCol[p->nCol++].zName;
drh75897232000-05-29 14:26:00 +0000458 sqliteSetNString(pz, pName->z, pName->n, 0);
drh982cef72000-05-30 16:27:03 +0000459 sqliteDequote(*pz);
drh75897232000-05-29 14:26:00 +0000460}
461
462/*
drh382c0242001-10-06 16:33:02 +0000463** This routine is called by the parser while in the middle of
464** parsing a CREATE TABLE statement. A "NOT NULL" constraint has
465** been seen on a column. This routine sets the notNull flag on
466** the column currently under construction.
467*/
drh9cfcf5d2002-01-29 18:41:24 +0000468void sqliteAddNotNull(Parse *pParse, int onError){
drh382c0242001-10-06 16:33:02 +0000469 Table *p;
470 int i;
471 if( (p = pParse->pNewTable)==0 ) return;
472 i = p->nCol-1;
drh9cfcf5d2002-01-29 18:41:24 +0000473 if( i>=0 ) p->aCol[i].notNull = onError;
drh382c0242001-10-06 16:33:02 +0000474}
475
476/*
477** This routine is called by the parser while in the middle of
478** parsing a CREATE TABLE statement. The pFirst token is the first
479** token in the sequence of tokens that describe the type of the
480** column currently under construction. pLast is the last token
481** in the sequence. Use this information to construct a string
482** that contains the typename of the column and store that string
483** in zType.
484*/
485void sqliteAddColumnType(Parse *pParse, Token *pFirst, Token *pLast){
486 Table *p;
487 int i, j;
488 int n;
489 char *z, **pz;
490 if( (p = pParse->pNewTable)==0 ) return;
491 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000492 if( i<0 ) return;
drh382c0242001-10-06 16:33:02 +0000493 pz = &p->aCol[i].zType;
drh5a2c2c22001-11-21 02:21:11 +0000494 n = pLast->n + Addr(pLast->z) - Addr(pFirst->z);
drh382c0242001-10-06 16:33:02 +0000495 sqliteSetNString(pz, pFirst->z, n, 0);
496 z = *pz;
drhf57b3392001-10-08 13:22:32 +0000497 if( z==0 ) return;
drh382c0242001-10-06 16:33:02 +0000498 for(i=j=0; z[i]; i++){
499 int c = z[i];
500 if( isspace(c) ) continue;
501 z[j++] = c;
502 }
503 z[j] = 0;
504}
505
506/*
drh7020f652000-06-03 18:06:52 +0000507** The given token is the default value for the last column added to
508** the table currently under construction. If "minusFlag" is true, it
509** means the value token was preceded by a minus sign.
drhd9b02572001-04-15 00:37:09 +0000510**
511** This routine is called by the parser while in the middle of
512** parsing a CREATE TABLE statement.
drh7020f652000-06-03 18:06:52 +0000513*/
514void sqliteAddDefaultValue(Parse *pParse, Token *pVal, int minusFlag){
515 Table *p;
516 int i;
517 char **pz;
518 if( (p = pParse->pNewTable)==0 ) return;
519 i = p->nCol-1;
drhf57b3392001-10-08 13:22:32 +0000520 if( i<0 ) return;
drh7020f652000-06-03 18:06:52 +0000521 pz = &p->aCol[i].zDflt;
522 if( minusFlag ){
523 sqliteSetNString(pz, "-", 1, pVal->z, pVal->n, 0);
524 }else{
525 sqliteSetNString(pz, pVal->z, pVal->n, 0);
526 }
527 sqliteDequote(*pz);
528}
529
530/*
drh4a324312001-12-21 14:30:42 +0000531** Designate the PRIMARY KEY for the table. pList is a list of names
532** of columns that form the primary key. If pList is NULL, then the
533** most recently added column of the table is the primary key.
534**
535** A table can have at most one primary key. If the table already has
536** a primary key (and this is the second primary key) then create an
537** error.
538**
539** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
540** then we will try to use that column as the row id. (Exception:
541** For backwards compatibility with older databases, do not do this
542** if the file format version number is less than 1.) Set the Table.iPKey
543** field of the table under construction to be the index of the
544** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
545** no INTEGER PRIMARY KEY.
546**
547** If the key is not an INTEGER PRIMARY KEY, then create a unique
548** index for the key. No index is created for INTEGER PRIMARY KEYs.
549*/
drh9cfcf5d2002-01-29 18:41:24 +0000550void sqliteAddPrimaryKey(Parse *pParse, IdList *pList, int onError){
drh4a324312001-12-21 14:30:42 +0000551 Table *pTab = pParse->pNewTable;
552 char *zType = 0;
553 int iCol = -1;
554 if( pTab==0 ) return;
555 if( pTab->hasPrimKey ){
556 sqliteSetString(&pParse->zErrMsg, "table \"", pTab->zName,
557 "\" has more than one primary key", 0);
558 pParse->nErr++;
559 return;
560 }
561 pTab->hasPrimKey = 1;
562 if( pList==0 ){
563 iCol = pTab->nCol - 1;
564 }else if( pList->nId==1 ){
565 for(iCol=0; iCol<pTab->nCol; iCol++){
566 if( sqliteStrICmp(pList->a[0].zName, pTab->aCol[iCol].zName)==0 ) break;
567 }
568 }
569 if( iCol>=0 && iCol<pTab->nCol ){
570 zType = pTab->aCol[iCol].zType;
571 }
572 if( pParse->db->file_format>=1 &&
573 zType && sqliteStrICmp(zType, "INTEGER")==0 ){
574 pTab->iPKey = iCol;
drh9cfcf5d2002-01-29 18:41:24 +0000575 pTab->keyConf = onError;
drh4a324312001-12-21 14:30:42 +0000576 }else{
drh9cfcf5d2002-01-29 18:41:24 +0000577 sqliteCreateIndex(pParse, 0, 0, pList, onError, 0, 0);
drh4a324312001-12-21 14:30:42 +0000578 }
579}
580
581/*
drh50e5dad2001-09-15 00:57:28 +0000582** Come up with a new random value for the schema cookie. Make sure
583** the new value is different from the old.
584**
585** The schema cookie is used to determine when the schema for the
586** database changes. After each schema change, the cookie value
587** changes. When a process first reads the schema it records the
588** cookie. Thereafter, whenever it goes to access the database,
589** it checks the cookie to make sure the schema has not changed
590** since it was last read.
591**
592** This plan is not completely bullet-proof. It is possible for
593** the schema to change multiple times and for the cookie to be
594** set back to prior value. But schema changes are infrequent
595** and the probability of hitting the same cookie value is only
596** 1 chance in 2^32. So we're safe enough.
597*/
598static void changeCookie(sqlite *db){
599 if( db->next_cookie==db->schema_cookie ){
drhb8ca3072001-12-05 00:21:20 +0000600 db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
drh50e5dad2001-09-15 00:57:28 +0000601 db->flags |= SQLITE_InternChanges;
602 }
603}
604
605/*
drh969fa7c2002-02-18 18:30:32 +0000606** Measure the number of characters needed to output the given
607** identifier. The number returned includes any quotes used
608** but does not include the null terminator.
609*/
610static int identLength(const char *z){
611 int n;
drh17f71932002-02-21 12:01:27 +0000612 int needQuote = 0;
613 for(n=0; *z; n++, z++){
614 if( *z=='\'' ){ n++; needQuote=1; }
drh969fa7c2002-02-18 18:30:32 +0000615 }
drh17f71932002-02-21 12:01:27 +0000616 return n + needQuote*2;
drh969fa7c2002-02-18 18:30:32 +0000617}
618
619/*
620** Write an identifier onto the end of the given string. Add
621** quote characters as needed.
622*/
623static void identPut(char *z, int *pIdx, char *zIdent){
drh17f71932002-02-21 12:01:27 +0000624 int i, j, needQuote;
drh969fa7c2002-02-18 18:30:32 +0000625 i = *pIdx;
drh17f71932002-02-21 12:01:27 +0000626 for(j=0; zIdent[j]; j++){
627 if( !isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
628 }
629 needQuote = zIdent[j]!=0 || isdigit(zIdent[0])
630 || sqliteKeywordCode(zIdent, j)!=TK_ID;
631 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000632 for(j=0; zIdent[j]; j++){
633 z[i++] = zIdent[j];
634 if( zIdent[j]=='\'' ) z[i++] = '\'';
635 }
drh17f71932002-02-21 12:01:27 +0000636 if( needQuote ) z[i++] = '\'';
drh969fa7c2002-02-18 18:30:32 +0000637 z[i] = 0;
638 *pIdx = i;
639}
640
641/*
642** Generate a CREATE TABLE statement appropriate for the given
643** table. Memory to hold the text of the statement is obtained
644** from sqliteMalloc() and must be freed by the calling function.
645*/
646static char *createTableStmt(Table *p){
647 int i, k, n;
648 char *zStmt;
649 char *zSep, *zSep2, *zEnd;
650 n = 0;
651 for(i=0; i<p->nCol; i++){
652 n += identLength(p->aCol[i].zName);
653 }
654 n += identLength(p->zName);
655 if( n<40 ){
656 zSep = "";
657 zSep2 = ",";
658 zEnd = ")";
659 }else{
660 zSep = "\n ";
661 zSep2 = ",\n ";
662 zEnd = "\n)";
663 }
664 n += 25 + 6*p->nCol;
665 zStmt = sqliteMalloc( n );
666 if( zStmt==0 ) return 0;
667 assert( !p->isTemp );
668 strcpy(zStmt, "CREATE TABLE ");
669 k = strlen(zStmt);
670 identPut(zStmt, &k, p->zName);
671 zStmt[k++] = '(';
672 for(i=0; i<p->nCol; i++){
673 strcpy(&zStmt[k], zSep);
674 k += strlen(&zStmt[k]);
675 zSep = zSep2;
676 identPut(zStmt, &k, p->aCol[i].zName);
677 }
678 strcpy(&zStmt[k], zEnd);
679 return zStmt;
680}
681
682/*
drh75897232000-05-29 14:26:00 +0000683** This routine is called to report the final ")" that terminates
684** a CREATE TABLE statement.
685**
drhf57b3392001-10-08 13:22:32 +0000686** The table structure that other action routines have been building
687** is added to the internal hash tables, assuming no errors have
688** occurred.
drh75897232000-05-29 14:26:00 +0000689**
drh1ccde152000-06-17 13:12:39 +0000690** An entry for the table is made in the master table on disk,
drhf57b3392001-10-08 13:22:32 +0000691** unless this is a temporary table or initFlag==1. When initFlag==1,
692** it means we are reading the sqlite_master table because we just
693** connected to the database or because the sqlite_master table has
694** recently changes, so the entry for this table already exists in
695** the sqlite_master table. We do not want to create it again.
drh969fa7c2002-02-18 18:30:32 +0000696**
697** If the pSelect argument is not NULL, it means that this routine
698** was called to create a table generated from a
699** "CREATE TABLE ... AS SELECT ..." statement. The column names of
700** the new table will match the result set of the SELECT.
drh75897232000-05-29 14:26:00 +0000701*/
drh969fa7c2002-02-18 18:30:32 +0000702void sqliteEndTable(Parse *pParse, Token *pEnd, Select *pSelect){
drh75897232000-05-29 14:26:00 +0000703 Table *p;
drhbe0072d2001-09-13 14:46:09 +0000704 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000705
drh969fa7c2002-02-18 18:30:32 +0000706 if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite_malloc_failed ) return;
drh28037572000-08-02 13:47:41 +0000707 p = pParse->pNewTable;
drhdaffd0e2001-04-11 14:28:42 +0000708 if( p==0 ) return;
drh75897232000-05-29 14:26:00 +0000709
drhf57b3392001-10-08 13:22:32 +0000710 /* Add the table to the in-memory representation of the database.
drh75897232000-05-29 14:26:00 +0000711 */
drhad75e982001-10-09 04:19:46 +0000712 assert( pParse->nameClash==0 || pParse->initFlag==1 );
drhf57b3392001-10-08 13:22:32 +0000713 if( pParse->explain==0 && pParse->nameClash==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000714 Table *pOld;
715 pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
716 if( pOld ){
drh74e24cd2002-01-09 03:19:59 +0000717 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
drh6d4abfb2001-10-22 02:58:08 +0000718 return;
719 }
drh75897232000-05-29 14:26:00 +0000720 pParse->pNewTable = 0;
drhbe0072d2001-09-13 14:46:09 +0000721 db->nTable++;
drh5e00f6c2001-09-13 13:46:56 +0000722 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +0000723 }
724
drh969fa7c2002-02-18 18:30:32 +0000725 /* If the table is generated from a SELECT, then construct the
726 ** list of columns and the text of the table.
727 */
728 if( pSelect ){
729 Table *pSelTab = sqliteResultSetOfSelect(pParse, 0, pSelect);
drh17f71932002-02-21 12:01:27 +0000730 if( pSelTab==0 ) return;
drh969fa7c2002-02-18 18:30:32 +0000731 assert( p->aCol==0 );
732 p->nCol = pSelTab->nCol;
733 p->aCol = pSelTab->aCol;
734 pSelTab->nCol = 0;
735 pSelTab->aCol = 0;
736 sqliteDeleteTable(0, pSelTab);
737 }
738
drhd78eeee2001-09-13 16:18:53 +0000739 /* If the initFlag is 1 it means we are reading the SQL off the
740 ** "sqlite_master" table on the disk. So do not write to the disk
drhe3c41372001-09-17 20:25:58 +0000741 ** again. Extract the root page number for the table from the
742 ** pParse->newTnum field. (The page number should have been put
drh382c0242001-10-06 16:33:02 +0000743 ** there by the sqliteOpenCb routine.)
drhd78eeee2001-09-13 16:18:53 +0000744 */
745 if( pParse->initFlag ){
746 p->tnum = pParse->newTnum;
747 }
748
drhe3c41372001-09-17 20:25:58 +0000749 /* If not initializing, then create a record for the new table
drh17f71932002-02-21 12:01:27 +0000750 ** in the SQLITE_MASTER table of the database. The record number
751 ** for the new table entry should already be on the stack.
drhf57b3392001-10-08 13:22:32 +0000752 **
753 ** If this is a TEMPORARY table, then just create the table. Do not
754 ** make an entry in SQLITE_MASTER.
drh75897232000-05-29 14:26:00 +0000755 */
756 if( !pParse->initFlag ){
drh4ff6dfa2002-03-03 23:06:00 +0000757 int n;
drhd8bc7082000-06-07 23:51:50 +0000758 Vdbe *v;
drh75897232000-05-29 14:26:00 +0000759
drhd8bc7082000-06-07 23:51:50 +0000760 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +0000761 if( v==0 ) return;
drh4ff6dfa2002-03-03 23:06:00 +0000762 if( p->pSelect==0 ){
763 /* A regular table */
764 sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
765 sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
766 }else{
767 /* A view */
768 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
769 }
drh969fa7c2002-02-18 18:30:32 +0000770 p->tnum = 0;
drhf57b3392001-10-08 13:22:32 +0000771 if( !p->isTemp ){
drh17f71932002-02-21 12:01:27 +0000772 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
drh99fcd712001-10-13 01:06:47 +0000773 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh4ff6dfa2002-03-03 23:06:00 +0000774 if( p->pSelect==0 ){
775 sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
776 }else{
777 sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
778 }
drh99fcd712001-10-13 01:06:47 +0000779 sqliteVdbeAddOp(v, OP_String, 0, 0);
780 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
781 sqliteVdbeAddOp(v, OP_String, 0, 0);
782 sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
drh969fa7c2002-02-18 18:30:32 +0000783 sqliteVdbeAddOp(v, OP_Dup, 4, 0);
784 sqliteVdbeAddOp(v, OP_String, 0, 0);
785 if( pSelect ){
786 char *z = createTableStmt(p);
787 n = z ? strlen(z) : 0;
788 sqliteVdbeChangeP3(v, -1, z, n);
789 sqliteFree(z);
790 }else{
791 assert( pEnd!=0 );
792 n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
793 sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
794 }
drh99fcd712001-10-13 01:06:47 +0000795 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +0000796 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000797 changeCookie(db);
drh603240c2002-03-05 01:11:12 +0000798 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
799 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh99fcd712001-10-13 01:06:47 +0000800 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +0000801 }
drh969fa7c2002-02-18 18:30:32 +0000802 if( pSelect ){
803 int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
804 sqliteVdbeAddOp(v, op, 1, 0);
805 pParse->nTab = 2;
drh832508b2002-03-02 17:04:07 +0000806 sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
drh969fa7c2002-02-18 18:30:32 +0000807 }
drh1c928532002-01-31 15:54:21 +0000808 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +0000809 }
810}
811
812/*
drha76b5df2002-02-23 02:32:10 +0000813** The parser calls this routine in order to create a new VIEW
814*/
815void sqliteCreateView(
816 Parse *pParse, /* The parsing context */
817 Token *pBegin, /* The CREATE token that begins the statement */
818 Token *pName, /* The token that holds the name of the view */
819 Select *pSelect /* A SELECT statement that will become the new view */
820){
821 Token sEnd;
drha76b5df2002-02-23 02:32:10 +0000822 Table *p;
drh4ff6dfa2002-03-03 23:06:00 +0000823 const char *z;
drha76b5df2002-02-23 02:32:10 +0000824 int n, offset;
825
826 sqliteStartTable(pParse, pBegin, pName, 0);
827 p = pParse->pNewTable;
drh417be792002-03-03 18:59:40 +0000828 if( p==0 ){
829 sqliteSelectDelete(pSelect);
830 return;
831 }
drha76b5df2002-02-23 02:32:10 +0000832 p->pSelect = pSelect;
drh417be792002-03-03 18:59:40 +0000833 if( !pParse->initFlag ){
834 if( sqliteViewGetColumnNames(pParse, p) ){
835 return;
836 }
837 }
drha76b5df2002-02-23 02:32:10 +0000838 sEnd = pParse->sLastToken;
839 if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){
840 sEnd.z += sEnd.n;
841 }
842 sEnd.n = 0;
843 n = ((int)sEnd.z) - (int)pBegin->z;
drh4ff6dfa2002-03-03 23:06:00 +0000844 z = pBegin->z;
845 while( n>0 && (z[n-1]==';' || isspace(z[n-1])) ){ n--; }
846 sEnd.z = &z[n-1];
847 sEnd.n = 1;
848 z = p->pSelect->zSelect = sqliteStrNDup(z, n);
drh417be792002-03-03 18:59:40 +0000849 if( z ){
850 offset = ((int)z) - (int)pBegin->z;
851 sqliteSelectMoveStrings(p->pSelect, offset);
852 sqliteEndTable(pParse, &sEnd, 0);
853 }
drha76b5df2002-02-23 02:32:10 +0000854 return;
drh417be792002-03-03 18:59:40 +0000855}
drha76b5df2002-02-23 02:32:10 +0000856
drh417be792002-03-03 18:59:40 +0000857/*
858** The Table structure pTable is really a VIEW. Fill in the names of
859** the columns of the view in the pTable structure. Return the number
860** of errors. If an error is seen leave an error message in pPare->zErrMsg.
861*/
862int sqliteViewGetColumnNames(Parse *pParse, Table *pTable){
863 ExprList *pEList;
864 Select *pSel;
865 Table *pSelTab;
866 int nErr = 0;
867
868 assert( pTable );
869
870 /* A positive nCol means the columns names for this view are
871 ** already known.
872 */
873 if( pTable->nCol>0 ) return 0;
874
875 /* A negative nCol is a special marker meaning that we are currently
876 ** trying to compute the column names. If we enter this routine with
877 ** a negative nCol, it means two or more views form a loop, like this:
878 **
879 ** CREATE VIEW one AS SELECT * FROM two;
880 ** CREATE VIEW two AS SELECT * FROM one;
881 */
882 if( pTable->nCol<0 ){
883 sqliteSetString(&pParse->zErrMsg, "view ", pTable->zName,
884 " is circularly defined", 0);
885 pParse->nErr++;
886 return 1;
887 }
888
889 /* If we get this far, it means we need to compute the table names.
890 */
891 assert( pTable->pSelect ); /* If nCol==0, then pTable must be a VIEW */
892 pSel = pTable->pSelect;
893
894 /* Note that the call to sqliteResultSetOfSelect() will expand any
895 ** "*" elements in this list. But we will need to restore the list
896 ** back to its original configuration afterwards, so we save a copy of
897 ** the original in pEList.
898 */
899 pEList = pSel->pEList;
900 pSel->pEList = sqliteExprListDup(pEList);
901 if( pSel->pEList==0 ){
902 pSel->pEList = pEList;
903 return 1; /* Malloc failed */
904 }
905 pTable->nCol = -1;
906 pSelTab = sqliteResultSetOfSelect(pParse, 0, pSel);
907 if( pSelTab ){
908 assert( pTable->aCol==0 );
909 pTable->nCol = pSelTab->nCol;
910 pTable->aCol = pSelTab->aCol;
911 pSelTab->nCol = 0;
912 pSelTab->aCol = 0;
913 sqliteDeleteTable(0, pSelTab);
914 pParse->db->flags |= SQLITE_UnresetViews;
915 }else{
916 pTable->nCol = 0;
917 nErr++;
918 }
919 sqliteSelectUnbind(pSel);
920 sqliteExprListDelete(pSel->pEList);
921 pSel->pEList = pEList;
922 return nErr;
923}
924
925/*
926** Clear the column names from the VIEW pTable.
927**
928** This routine is called whenever any other table or view is modified.
929** The view passed into this routine might depend directly or indirectly
930** on the modified or deleted table so we need to clear the old column
931** names so that they will be recomputed.
932*/
933static void sqliteViewResetColumnNames(Table *pTable){
934 int i;
935 if( pTable==0 || pTable->pSelect==0 ) return;
936 if( pTable->nCol==0 ) return;
937 for(i=0; i<pTable->nCol; i++){
938 sqliteFree(pTable->aCol[i].zName);
939 sqliteFree(pTable->aCol[i].zDflt);
940 sqliteFree(pTable->aCol[i].zType);
941 }
942 sqliteFree(pTable->aCol);
943 pTable->aCol = 0;
944 pTable->nCol = 0;
945}
946
947/*
948** Clear the column names from every VIEW.
949*/
950void sqliteViewResetAll(sqlite *db){
951 HashElem *i;
952 if( (db->flags & SQLITE_UnresetViews)==0 ) return;
953 for(i=sqliteHashFirst(&db->tblHash); i; i=sqliteHashNext(i)){
954 Table *pTab = sqliteHashData(i);
955 if( pTab->pSelect ){
956 sqliteViewResetColumnNames(pTab);
957 }
958 }
959 db->flags &= ~SQLITE_UnresetViews;
drha76b5df2002-02-23 02:32:10 +0000960}
961
962/*
drh75897232000-05-29 14:26:00 +0000963** Given a token, look up a table with that name. If not found, leave
964** an error for the parser to find and return NULL.
965*/
drhcce7d172000-05-31 15:34:51 +0000966Table *sqliteTableFromToken(Parse *pParse, Token *pTok){
drhdaffd0e2001-04-11 14:28:42 +0000967 char *zName;
968 Table *pTab;
969 zName = sqliteTableNameFromToken(pTok);
970 if( zName==0 ) return 0;
971 pTab = sqliteFindTable(pParse->db, zName);
drh75897232000-05-29 14:26:00 +0000972 sqliteFree(zName);
973 if( pTab==0 ){
drhb24fcbe2000-05-29 23:30:50 +0000974 sqliteSetNString(&pParse->zErrMsg, "no such table: ", 0,
975 pTok->z, pTok->n, 0);
drh75897232000-05-29 14:26:00 +0000976 pParse->nErr++;
977 }
978 return pTab;
979}
980
981/*
982** This routine is called to do the work of a DROP TABLE statement.
drhd9b02572001-04-15 00:37:09 +0000983** pName is the name of the table to be dropped.
drh75897232000-05-29 14:26:00 +0000984*/
drh4ff6dfa2002-03-03 23:06:00 +0000985void sqliteDropTable(Parse *pParse, Token *pName, int isView){
drh75897232000-05-29 14:26:00 +0000986 Table *pTable;
drh75897232000-05-29 14:26:00 +0000987 Vdbe *v;
988 int base;
drh5edc3122001-09-13 21:53:09 +0000989 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000990
drhdaffd0e2001-04-11 14:28:42 +0000991 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +0000992 pTable = sqliteTableFromToken(pParse, pName);
993 if( pTable==0 ) return;
994 if( pTable->readOnly ){
drh1d37e282000-05-30 03:12:21 +0000995 sqliteSetString(&pParse->zErrMsg, "table ", pTable->zName,
996 " may not be dropped", 0);
drh75897232000-05-29 14:26:00 +0000997 pParse->nErr++;
998 return;
999 }
drh4ff6dfa2002-03-03 23:06:00 +00001000 if( isView && pTable->pSelect==0 ){
1001 sqliteSetString(&pParse->zErrMsg, "use DROP TABLE to delete table ",
1002 pTable->zName, 0);
1003 pParse->nErr++;
1004 return;
1005 }
1006 if( !isView && pTable->pSelect ){
1007 sqliteSetString(&pParse->zErrMsg, "use DROP VIEW to delete view ",
1008 pTable->zName, 0);
1009 pParse->nErr++;
1010 return;
1011 }
drh75897232000-05-29 14:26:00 +00001012
drh1ccde152000-06-17 13:12:39 +00001013 /* Generate code to remove the table from the master table
1014 ** on disk.
1015 */
drhd8bc7082000-06-07 23:51:50 +00001016 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001017 if( v ){
1018 static VdbeOp dropTable[] = {
drhecdc7532001-09-23 02:35:53 +00001019 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +00001020 { OP_Rewind, 0, ADDR(9), 0},
drhd78eeee2001-09-13 16:18:53 +00001021 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +00001022 { OP_MemStore, 1, 1, 0},
1023 { OP_MemLoad, 1, 0, 0}, /* 4 */
drhe3c41372001-09-17 20:25:58 +00001024 { OP_Column, 0, 2, 0},
drh6b563442001-11-07 16:48:26 +00001025 { OP_Ne, 0, ADDR(8), 0},
drh75897232000-05-29 14:26:00 +00001026 { OP_Delete, 0, 0, 0},
drh6b563442001-11-07 16:48:26 +00001027 { OP_Next, 0, ADDR(4), 0}, /* 8 */
drh603240c2002-03-05 01:11:12 +00001028 { OP_Integer, 0, 0, 0}, /* 9 */
1029 { OP_SetCookie, 0, 0, 0},
drh75897232000-05-29 14:26:00 +00001030 { OP_Close, 0, 0, 0},
1031 };
1032 Index *pIdx;
drh1c928532002-01-31 15:54:21 +00001033 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +00001034 if( !pTable->isTemp ){
1035 base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
drh0a36c572002-02-18 22:49:59 +00001036 sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
drhf57b3392001-10-08 13:22:32 +00001037 changeCookie(db);
1038 sqliteVdbeChangeP1(v, base+9, db->next_cookie);
1039 }
drh4ff6dfa2002-03-03 23:06:00 +00001040 if( !isView ){
1041 sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
1042 for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
1043 sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
1044 }
drh5e00f6c2001-09-13 13:46:56 +00001045 }
drh1c928532002-01-31 15:54:21 +00001046 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001047 }
1048
drh74e24cd2002-01-09 03:19:59 +00001049 /* Move the table (and all its indices) to the pending DROP queue.
1050 ** Or, if the table was never committed, just delete it. If the table
1051 ** has been committed and is placed on the pending DROP queue, then the
1052 ** delete will occur when sqliteCommitInternalChanges() executes.
drh75897232000-05-29 14:26:00 +00001053 **
1054 ** Exception: if the SQL statement began with the EXPLAIN keyword,
drh5e00f6c2001-09-13 13:46:56 +00001055 ** then no changes should be made.
drh75897232000-05-29 14:26:00 +00001056 */
1057 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +00001058 sqlitePendingDropTable(db, pTable);
drh5edc3122001-09-13 21:53:09 +00001059 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001060 }
drh417be792002-03-03 18:59:40 +00001061 sqliteViewResetAll(db);
drh75897232000-05-29 14:26:00 +00001062}
1063
1064/*
1065** Create a new index for an SQL table. pIndex is the name of the index
1066** and pTable is the name of the table that is to be indexed. Both will
drhadbca9c2001-09-27 15:11:53 +00001067** be NULL for a primary key or an index that is created to satisfy a
1068** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
drh382c0242001-10-06 16:33:02 +00001069** as the table to be indexed. pParse->pNewTable is a table that is
1070** currently being constructed by a CREATE TABLE statement.
drh75897232000-05-29 14:26:00 +00001071**
drh382c0242001-10-06 16:33:02 +00001072** pList is a list of columns to be indexed. pList will be NULL if this
1073** is a primary key or unique-constraint on the most recent column added
1074** to the table currently under construction.
drh75897232000-05-29 14:26:00 +00001075*/
1076void sqliteCreateIndex(
1077 Parse *pParse, /* All information about this parse */
1078 Token *pName, /* Name of the index. May be NULL */
1079 Token *pTable, /* Name of the table to index. Use pParse->pNewTable if 0 */
drh1ccde152000-06-17 13:12:39 +00001080 IdList *pList, /* A list of columns to be indexed */
drh9cfcf5d2002-01-29 18:41:24 +00001081 int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
drh75897232000-05-29 14:26:00 +00001082 Token *pStart, /* The CREATE token that begins a CREATE TABLE statement */
1083 Token *pEnd /* The ")" that closes the CREATE INDEX statement */
1084){
1085 Table *pTab; /* Table to be indexed */
1086 Index *pIndex; /* The index to be created */
1087 char *zName = 0;
drhbeae3192001-09-22 18:12:08 +00001088 int i, j;
drhf57b3392001-10-08 13:22:32 +00001089 Token nullId; /* Fake token for an empty ID list */
drhbe0072d2001-09-13 14:46:09 +00001090 sqlite *db = pParse->db;
drhf57b3392001-10-08 13:22:32 +00001091 int hideName = 0; /* Do not put table name in the hash table */
drh75897232000-05-29 14:26:00 +00001092
drhdaffd0e2001-04-11 14:28:42 +00001093 if( pParse->nErr || sqlite_malloc_failed ) goto exit_create_index;
1094
drh75897232000-05-29 14:26:00 +00001095 /*
1096 ** Find the table that is to be indexed. Return early if not found.
1097 */
1098 if( pTable!=0 ){
drhe3c41372001-09-17 20:25:58 +00001099 assert( pName!=0 );
drh75897232000-05-29 14:26:00 +00001100 pTab = sqliteTableFromToken(pParse, pTable);
1101 }else{
drhe3c41372001-09-17 20:25:58 +00001102 assert( pName==0 );
drh75897232000-05-29 14:26:00 +00001103 pTab = pParse->pNewTable;
1104 }
1105 if( pTab==0 || pParse->nErr ) goto exit_create_index;
1106 if( pTab->readOnly ){
drhb24fcbe2000-05-29 23:30:50 +00001107 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
1108 " may not have new indices added", 0);
drh75897232000-05-29 14:26:00 +00001109 pParse->nErr++;
1110 goto exit_create_index;
1111 }
drha76b5df2002-02-23 02:32:10 +00001112 if( pTab->pSelect ){
1113 sqliteSetString(&pParse->zErrMsg, "views may not be indexed", 0);
1114 pParse->nErr++;
1115 goto exit_create_index;
1116 }
drh75897232000-05-29 14:26:00 +00001117
drhf57b3392001-10-08 13:22:32 +00001118 /* If this index is created while re-reading the schema from sqlite_master
1119 ** but the table associated with this index is a temporary table, it can
drh4a324312001-12-21 14:30:42 +00001120 ** only mean that the table that this index is really associated with is
1121 ** one whose name is hidden behind a temporary table with the same name.
drhf57b3392001-10-08 13:22:32 +00001122 ** Since its table has been suppressed, we need to also suppress the
1123 ** index.
1124 */
1125 if( pParse->initFlag && pTab->isTemp ){
1126 goto exit_create_index;
1127 }
1128
drh75897232000-05-29 14:26:00 +00001129 /*
1130 ** Find the name of the index. Make sure there is not already another
drhf57b3392001-10-08 13:22:32 +00001131 ** index or table with the same name.
1132 **
1133 ** Exception: If we are reading the names of permanent indices from the
1134 ** sqlite_master table (because some other process changed the schema) and
1135 ** one of the index names collides with the name of a temporary table or
1136 ** index, then we will continue to process this index, but we will not
1137 ** store its name in the hash table. Set the hideName flag to accomplish
1138 ** this.
1139 **
1140 ** If pName==0 it means that we are
drhadbca9c2001-09-27 15:11:53 +00001141 ** dealing with a primary key or UNIQUE constraint. We have to invent our
1142 ** own name.
drh75897232000-05-29 14:26:00 +00001143 */
1144 if( pName ){
drhf57b3392001-10-08 13:22:32 +00001145 Index *pISameName; /* Another index with the same name */
1146 Table *pTSameName; /* A table with same name as the index */
drh75897232000-05-29 14:26:00 +00001147 zName = sqliteTableNameFromToken(pName);
drhe3c41372001-09-17 20:25:58 +00001148 if( zName==0 ) goto exit_create_index;
drhf57b3392001-10-08 13:22:32 +00001149 if( (pISameName = sqliteFindIndex(db, zName))!=0 ){
1150 if( pISameName->pTable->isTemp && pParse->initFlag ){
1151 hideName = 1;
1152 }else{
1153 sqliteSetString(&pParse->zErrMsg, "index ", zName,
1154 " already exists", 0);
1155 pParse->nErr++;
1156 goto exit_create_index;
1157 }
drhe3c41372001-09-17 20:25:58 +00001158 }
drhf57b3392001-10-08 13:22:32 +00001159 if( (pTSameName = sqliteFindTable(db, zName))!=0 ){
1160 if( pTSameName->isTemp && pParse->initFlag ){
1161 hideName = 1;
1162 }else{
1163 sqliteSetString(&pParse->zErrMsg, "there is already a table named ",
1164 zName, 0);
1165 pParse->nErr++;
1166 goto exit_create_index;
1167 }
drhe3c41372001-09-17 20:25:58 +00001168 }
drh75897232000-05-29 14:26:00 +00001169 }else{
drhadbca9c2001-09-27 15:11:53 +00001170 char zBuf[30];
1171 int n;
1172 Index *pLoop;
1173 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
1174 sprintf(zBuf,"%d)",n);
drh75897232000-05-29 14:26:00 +00001175 zName = 0;
drhadbca9c2001-09-27 15:11:53 +00001176 sqliteSetString(&zName, "(", pTab->zName, " autoindex ", zBuf, 0);
drhe3c41372001-09-17 20:25:58 +00001177 if( zName==0 ) goto exit_create_index;
drhda9e0342002-01-10 14:31:48 +00001178 hideName = sqliteFindIndex(db, zName)!=0;
drh75897232000-05-29 14:26:00 +00001179 }
1180
1181 /* If pList==0, it means this routine was called to make a primary
drh1ccde152000-06-17 13:12:39 +00001182 ** key out of the last column added to the table under construction.
drh75897232000-05-29 14:26:00 +00001183 ** So create a fake list to simulate this.
1184 */
1185 if( pList==0 ){
drh7020f652000-06-03 18:06:52 +00001186 nullId.z = pTab->aCol[pTab->nCol-1].zName;
drh75897232000-05-29 14:26:00 +00001187 nullId.n = strlen(nullId.z);
1188 pList = sqliteIdListAppend(0, &nullId);
1189 if( pList==0 ) goto exit_create_index;
1190 }
1191
1192 /*
1193 ** Allocate the index structure.
1194 */
drhdcc581c2000-05-30 13:44:19 +00001195 pIndex = sqliteMalloc( sizeof(Index) + strlen(zName) + 1 +
drh75897232000-05-29 14:26:00 +00001196 sizeof(int)*pList->nId );
drhdaffd0e2001-04-11 14:28:42 +00001197 if( pIndex==0 ) goto exit_create_index;
drh967e8b72000-06-21 13:59:10 +00001198 pIndex->aiColumn = (int*)&pIndex[1];
1199 pIndex->zName = (char*)&pIndex->aiColumn[pList->nId];
drh75897232000-05-29 14:26:00 +00001200 strcpy(pIndex->zName, zName);
1201 pIndex->pTable = pTab;
drh967e8b72000-06-21 13:59:10 +00001202 pIndex->nColumn = pList->nId;
drh9cfcf5d2002-01-29 18:41:24 +00001203 pIndex->onError = pIndex->isUnique = onError;
drh75897232000-05-29 14:26:00 +00001204
drh1ccde152000-06-17 13:12:39 +00001205 /* Scan the names of the columns of the table to be indexed and
1206 ** load the column indices into the Index structure. Report an error
1207 ** if any column is not found.
drh75897232000-05-29 14:26:00 +00001208 */
1209 for(i=0; i<pList->nId; i++){
1210 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +00001211 if( sqliteStrICmp(pList->a[i].zName, pTab->aCol[j].zName)==0 ) break;
drh75897232000-05-29 14:26:00 +00001212 }
1213 if( j>=pTab->nCol ){
drhb24fcbe2000-05-29 23:30:50 +00001214 sqliteSetString(&pParse->zErrMsg, "table ", pTab->zName,
drh1ccde152000-06-17 13:12:39 +00001215 " has no column named ", pList->a[i].zName, 0);
drh75897232000-05-29 14:26:00 +00001216 pParse->nErr++;
1217 sqliteFree(pIndex);
1218 goto exit_create_index;
1219 }
drh967e8b72000-06-21 13:59:10 +00001220 pIndex->aiColumn[i] = j;
drh75897232000-05-29 14:26:00 +00001221 }
1222
1223 /* Link the new Index structure to its table and to the other
drhadbca9c2001-09-27 15:11:53 +00001224 ** in-memory database structures.
drh75897232000-05-29 14:26:00 +00001225 */
drhf57b3392001-10-08 13:22:32 +00001226 if( !pParse->explain && !hideName ){
drh6d4abfb2001-10-22 02:58:08 +00001227 Index *p;
1228 p = sqliteHashInsert(&db->idxHash, pIndex->zName, strlen(zName)+1, pIndex);
1229 if( p ){
1230 assert( p==pIndex ); /* Malloc must have failed */
1231 sqliteFree(pIndex);
1232 goto exit_create_index;
1233 }
drh5e00f6c2001-09-13 13:46:56 +00001234 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001235 }
drh9cfcf5d2002-01-29 18:41:24 +00001236
1237 /* When adding an index to the list of indices for a table, make
1238 ** sure all indices labeled OE_Replace come after all those labeled
1239 ** OE_Ignore. This is necessary for the correct operation of UPDATE
1240 ** and INSERT.
1241 */
1242 if( onError!=OE_Replace || pTab->pIndex==0
1243 || pTab->pIndex->onError==OE_Replace){
1244 pIndex->pNext = pTab->pIndex;
1245 pTab->pIndex = pIndex;
1246 }else{
1247 Index *pOther = pTab->pIndex;
1248 while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
1249 pOther = pOther->pNext;
1250 }
1251 pIndex->pNext = pOther->pNext;
1252 pOther->pNext = pIndex;
1253 }
drh75897232000-05-29 14:26:00 +00001254
drhd78eeee2001-09-13 16:18:53 +00001255 /* If the initFlag is 1 it means we are reading the SQL off the
1256 ** "sqlite_master" table on the disk. So do not write to the disk
1257 ** again. Extract the table number from the pParse->newTnum field.
1258 */
drhadbca9c2001-09-27 15:11:53 +00001259 if( pParse->initFlag && pTable!=0 ){
drhd78eeee2001-09-13 16:18:53 +00001260 pIndex->tnum = pParse->newTnum;
1261 }
1262
drh75897232000-05-29 14:26:00 +00001263 /* If the initFlag is 0 then create the index on disk. This
1264 ** involves writing the index into the master table and filling in the
1265 ** index with the current table contents.
1266 **
1267 ** The initFlag is 0 when the user first enters a CREATE INDEX
1268 ** command. The initFlag is 1 when a database is opened and
1269 ** CREATE INDEX statements are read out of the master table. In
1270 ** the latter case the index already exists on disk, which is why
1271 ** we don't want to recreate it.
drh5edc3122001-09-13 21:53:09 +00001272 **
1273 ** If pTable==0 it means this index is generated as a primary key
drh382c0242001-10-06 16:33:02 +00001274 ** or UNIQUE constraint of a CREATE TABLE statement. Since the table
1275 ** has just been created, it contains no data and the index initialization
1276 ** step can be skipped.
drh75897232000-05-29 14:26:00 +00001277 */
drhadbca9c2001-09-27 15:11:53 +00001278 else if( pParse->initFlag==0 ){
drh75897232000-05-29 14:26:00 +00001279 int n;
drhadbca9c2001-09-27 15:11:53 +00001280 Vdbe *v;
drh75897232000-05-29 14:26:00 +00001281 int lbl1, lbl2;
1282 int i;
drhadbca9c2001-09-27 15:11:53 +00001283 int addr;
drhf57b3392001-10-08 13:22:32 +00001284 int isTemp = pTab->isTemp;
drh75897232000-05-29 14:26:00 +00001285
drhd8bc7082000-06-07 23:51:50 +00001286 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001287 if( v==0 ) goto exit_create_index;
drhadbca9c2001-09-27 15:11:53 +00001288 if( pTable!=0 ){
drh1c928532002-01-31 15:54:21 +00001289 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +00001290 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001291 sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
1292 sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001293 }
drhadbca9c2001-09-27 15:11:53 +00001294 }
drhf57b3392001-10-08 13:22:32 +00001295 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001296 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1297 sqliteVdbeAddOp(v, OP_String, 0, 0);
1298 sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
1299 sqliteVdbeAddOp(v, OP_String, 0, 0);
1300 sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
1301 sqliteVdbeAddOp(v, OP_String, 0, 0);
1302 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001303 }
drh99fcd712001-10-13 01:06:47 +00001304 addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
1305 sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
drhadbca9c2001-09-27 15:11:53 +00001306 pIndex->tnum = 0;
1307 if( pTable ){
drhf57b3392001-10-08 13:22:32 +00001308 if( isTemp ){
drh99fcd712001-10-13 01:06:47 +00001309 sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001310 }else{
drh99fcd712001-10-13 01:06:47 +00001311 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1312 sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
drhf57b3392001-10-08 13:22:32 +00001313 }
drh5e00f6c2001-09-13 13:46:56 +00001314 }
drhf57b3392001-10-08 13:22:32 +00001315 if( !isTemp ){
drh99fcd712001-10-13 01:06:47 +00001316 addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001317 if( pStart && pEnd ){
drh5a2c2c22001-11-21 02:21:11 +00001318 n = Addr(pEnd->z) - Addr(pStart->z) + 1;
drhf57b3392001-10-08 13:22:32 +00001319 sqliteVdbeChangeP3(v, addr, pStart->z, n);
1320 }
drh99fcd712001-10-13 01:06:47 +00001321 sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
drh6b125452002-01-28 15:53:03 +00001322 sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
drh75897232000-05-29 14:26:00 +00001323 }
drhadbca9c2001-09-27 15:11:53 +00001324 if( pTable ){
drh99fcd712001-10-13 01:06:47 +00001325 sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
1326 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhadbca9c2001-09-27 15:11:53 +00001327 lbl2 = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001328 sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
1329 lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
drhadbca9c2001-09-27 15:11:53 +00001330 for(i=0; i<pIndex->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00001331 sqliteVdbeAddOp(v, OP_Column, 2, pIndex->aiColumn[i]);
drhadbca9c2001-09-27 15:11:53 +00001332 }
drh99fcd712001-10-13 01:06:47 +00001333 sqliteVdbeAddOp(v, OP_MakeIdxKey, pIndex->nColumn, 0);
drh9cfcf5d2002-01-29 18:41:24 +00001334 sqliteVdbeAddOp(v, OP_IdxPut, 1, pIndex->onError!=OE_None);
drh6b563442001-11-07 16:48:26 +00001335 sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
drh99fcd712001-10-13 01:06:47 +00001336 sqliteVdbeResolveLabel(v, lbl2);
drh99fcd712001-10-13 01:06:47 +00001337 sqliteVdbeAddOp(v, OP_Close, 2, 0);
1338 sqliteVdbeAddOp(v, OP_Close, 1, 0);
drh75897232000-05-29 14:26:00 +00001339 }
drhadbca9c2001-09-27 15:11:53 +00001340 if( pTable!=0 ){
drhf57b3392001-10-08 13:22:32 +00001341 if( !isTemp ){
1342 changeCookie(db);
drh603240c2002-03-05 01:11:12 +00001343 sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
1344 sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
drh99fcd712001-10-13 01:06:47 +00001345 sqliteVdbeAddOp(v, OP_Close, 0, 0);
drhf57b3392001-10-08 13:22:32 +00001346 }
drh1c928532002-01-31 15:54:21 +00001347 sqliteEndWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001348 }
drh75897232000-05-29 14:26:00 +00001349 }
1350
drh75897232000-05-29 14:26:00 +00001351 /* Clean up before exiting */
1352exit_create_index:
1353 sqliteIdListDelete(pList);
1354 sqliteFree(zName);
1355 return;
1356}
1357
1358/*
drh74e24cd2002-01-09 03:19:59 +00001359** This routine will drop an existing named index. This routine
1360** implements the DROP INDEX statement.
drh75897232000-05-29 14:26:00 +00001361*/
1362void sqliteDropIndex(Parse *pParse, Token *pName){
1363 Index *pIndex;
1364 char *zName;
1365 Vdbe *v;
drhbe0072d2001-09-13 14:46:09 +00001366 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +00001367
drhdaffd0e2001-04-11 14:28:42 +00001368 if( pParse->nErr || sqlite_malloc_failed ) return;
drh75897232000-05-29 14:26:00 +00001369 zName = sqliteTableNameFromToken(pName);
drhdaffd0e2001-04-11 14:28:42 +00001370 if( zName==0 ) return;
drhbe0072d2001-09-13 14:46:09 +00001371 pIndex = sqliteFindIndex(db, zName);
drh75897232000-05-29 14:26:00 +00001372 sqliteFree(zName);
1373 if( pIndex==0 ){
drh1d37e282000-05-30 03:12:21 +00001374 sqliteSetNString(&pParse->zErrMsg, "no such index: ", 0,
1375 pName->z, pName->n, 0);
drh75897232000-05-29 14:26:00 +00001376 pParse->nErr++;
1377 return;
1378 }
1379
1380 /* Generate code to remove the index and from the master table */
drhd8bc7082000-06-07 23:51:50 +00001381 v = sqliteGetVdbe(pParse);
drh75897232000-05-29 14:26:00 +00001382 if( v ){
1383 static VdbeOp dropIndex[] = {
drhecdc7532001-09-23 02:35:53 +00001384 { OP_OpenWrite, 0, 2, MASTER_NAME},
drh6b563442001-11-07 16:48:26 +00001385 { OP_Rewind, 0, ADDR(10),0},
drhd78eeee2001-09-13 16:18:53 +00001386 { OP_String, 0, 0, 0}, /* 2 */
drh6b563442001-11-07 16:48:26 +00001387 { OP_MemStore, 1, 1, 0},
1388 { OP_MemLoad, 1, 0, 0}, /* 4 */
drh5e00f6c2001-09-13 13:46:56 +00001389 { OP_Column, 0, 1, 0},
drh6b563442001-11-07 16:48:26 +00001390 { OP_Eq, 0, ADDR(9), 0},
1391 { OP_Next, 0, ADDR(4), 0},
1392 { OP_Goto, 0, ADDR(10),0},
1393 { OP_Delete, 0, 0, 0}, /* 9 */
drh603240c2002-03-05 01:11:12 +00001394 { OP_Integer, 0, 0, 0}, /* 10 */
1395 { OP_SetCookie, 0, 0, 0},
drh75897232000-05-29 14:26:00 +00001396 { OP_Close, 0, 0, 0},
1397 };
1398 int base;
drhf57b3392001-10-08 13:22:32 +00001399 Table *pTab = pIndex->pTable;
drh75897232000-05-29 14:26:00 +00001400
drh1c928532002-01-31 15:54:21 +00001401 sqliteBeginWriteOperation(pParse);
drhf57b3392001-10-08 13:22:32 +00001402 if( !pTab->isTemp ){
1403 base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
drh99fcd712001-10-13 01:06:47 +00001404 sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC);
drhf57b3392001-10-08 13:22:32 +00001405 changeCookie(db);
drh6b563442001-11-07 16:48:26 +00001406 sqliteVdbeChangeP1(v, base+10, db->next_cookie);
drhf57b3392001-10-08 13:22:32 +00001407 }
drh99fcd712001-10-13 01:06:47 +00001408 sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
drh1c928532002-01-31 15:54:21 +00001409 sqliteEndWriteOperation(pParse);
drh75897232000-05-29 14:26:00 +00001410 }
1411
drh74e24cd2002-01-09 03:19:59 +00001412 /* Move the index onto the pending DROP queue. Or, if the index was
1413 ** never committed, just delete it. Indices on the pending DROP queue
1414 ** get deleted by sqliteCommitInternalChanges() when the user executes
1415 ** a COMMIT. Or if a rollback occurs, the elements of the DROP queue
1416 ** are moved back into the main hash table.
drh75897232000-05-29 14:26:00 +00001417 */
1418 if( !pParse->explain ){
drh74e24cd2002-01-09 03:19:59 +00001419 sqlitePendingDropIndex(db, pIndex);
drh5e00f6c2001-09-13 13:46:56 +00001420 db->flags |= SQLITE_InternChanges;
drh75897232000-05-29 14:26:00 +00001421 }
1422}
1423
1424/*
drh75897232000-05-29 14:26:00 +00001425** Append a new element to the given IdList. Create a new IdList if
1426** need be.
drhdaffd0e2001-04-11 14:28:42 +00001427**
1428** A new IdList is returned, or NULL if malloc() fails.
drh75897232000-05-29 14:26:00 +00001429*/
1430IdList *sqliteIdListAppend(IdList *pList, Token *pToken){
1431 if( pList==0 ){
1432 pList = sqliteMalloc( sizeof(IdList) );
1433 if( pList==0 ) return 0;
1434 }
1435 if( (pList->nId & 7)==0 ){
drh6d4abfb2001-10-22 02:58:08 +00001436 struct IdList_item *a;
1437 a = sqliteRealloc(pList->a, (pList->nId+8)*sizeof(pList->a[0]) );
1438 if( a==0 ){
drhdaffd0e2001-04-11 14:28:42 +00001439 sqliteIdListDelete(pList);
1440 return 0;
drh75897232000-05-29 14:26:00 +00001441 }
drh6d4abfb2001-10-22 02:58:08 +00001442 pList->a = a;
drh75897232000-05-29 14:26:00 +00001443 }
1444 memset(&pList->a[pList->nId], 0, sizeof(pList->a[0]));
1445 if( pToken ){
drhdaffd0e2001-04-11 14:28:42 +00001446 char **pz = &pList->a[pList->nId].zName;
1447 sqliteSetNString(pz, pToken->z, pToken->n, 0);
1448 if( *pz==0 ){
1449 sqliteIdListDelete(pList);
1450 return 0;
1451 }else{
1452 sqliteDequote(*pz);
1453 }
drh75897232000-05-29 14:26:00 +00001454 }
1455 pList->nId++;
1456 return pList;
1457}
1458
1459/*
1460** Add an alias to the last identifier on the given identifier list.
1461*/
1462void sqliteIdListAddAlias(IdList *pList, Token *pToken){
1463 if( pList && pList->nId>0 ){
1464 int i = pList->nId - 1;
1465 sqliteSetNString(&pList->a[i].zAlias, pToken->z, pToken->n, 0);
drh982cef72000-05-30 16:27:03 +00001466 sqliteDequote(pList->a[i].zAlias);
drh75897232000-05-29 14:26:00 +00001467 }
1468}
1469
1470/*
drha76b5df2002-02-23 02:32:10 +00001471** Delete an entire IdList.
drh75897232000-05-29 14:26:00 +00001472*/
1473void sqliteIdListDelete(IdList *pList){
1474 int i;
1475 if( pList==0 ) return;
1476 for(i=0; i<pList->nId; i++){
1477 sqliteFree(pList->a[i].zName);
1478 sqliteFree(pList->a[i].zAlias);
drhff78bd22002-02-27 01:47:11 +00001479 if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){
drhdaffd0e2001-04-11 14:28:42 +00001480 sqliteDeleteTable(0, pList->a[i].pTab);
1481 }
drhff78bd22002-02-27 01:47:11 +00001482 sqliteSelectDelete(pList->a[i].pSelect);
drh75897232000-05-29 14:26:00 +00001483 }
1484 sqliteFree(pList->a);
1485 sqliteFree(pList);
1486}
1487
drh982cef72000-05-30 16:27:03 +00001488/*
1489** The COPY command is for compatibility with PostgreSQL and specificially
1490** for the ability to read the output of pg_dump. The format is as
1491** follows:
1492**
1493** COPY table FROM file [USING DELIMITERS string]
1494**
1495** "table" is an existing table name. We will read lines of code from
1496** file to fill this table with data. File might be "stdin". The optional
1497** delimiter string identifies the field separators. The default is a tab.
1498*/
1499void sqliteCopy(
1500 Parse *pParse, /* The parser context */
1501 Token *pTableName, /* The name of the table into which we will insert */
1502 Token *pFilename, /* The file from which to obtain information */
drhb419a922002-01-30 16:17:23 +00001503 Token *pDelimiter, /* Use this as the field delimiter */
1504 int onError /* What to do if a constraint fails */
drh982cef72000-05-30 16:27:03 +00001505){
1506 Table *pTab;
1507 char *zTab;
drh1c928532002-01-31 15:54:21 +00001508 int i;
drh982cef72000-05-30 16:27:03 +00001509 Vdbe *v;
1510 int addr, end;
1511 Index *pIdx;
drhbe0072d2001-09-13 14:46:09 +00001512 sqlite *db = pParse->db;
drh982cef72000-05-30 16:27:03 +00001513
1514 zTab = sqliteTableNameFromToken(pTableName);
drhdaffd0e2001-04-11 14:28:42 +00001515 if( sqlite_malloc_failed || zTab==0 ) goto copy_cleanup;
drhef2daf52002-03-04 02:26:15 +00001516 pTab = sqliteTableNameToTable(pParse, zTab);
drh982cef72000-05-30 16:27:03 +00001517 sqliteFree(zTab);
drhef2daf52002-03-04 02:26:15 +00001518 if( pTab==0 ) goto copy_cleanup;
drhd8bc7082000-06-07 23:51:50 +00001519 v = sqliteGetVdbe(pParse);
drh982cef72000-05-30 16:27:03 +00001520 if( v ){
drhf57b3392001-10-08 13:22:32 +00001521 int openOp;
drh663fc632002-02-02 18:49:19 +00001522 sqliteBeginMultiWriteOperation(pParse);
drh99fcd712001-10-13 01:06:47 +00001523 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
drh982cef72000-05-30 16:27:03 +00001524 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
drhb7665992000-05-30 17:30:35 +00001525 sqliteVdbeDequoteP3(v, addr);
drhf57b3392001-10-08 13:22:32 +00001526 openOp = pTab->isTemp ? OP_OpenWrAux : OP_OpenWrite;
drh99fcd712001-10-13 01:06:47 +00001527 sqliteVdbeAddOp(v, openOp, 0, pTab->tnum);
1528 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001529 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
drh99fcd712001-10-13 01:06:47 +00001530 sqliteVdbeAddOp(v, openOp, i, pIdx->tnum);
1531 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh982cef72000-05-30 16:27:03 +00001532 }
drhb419a922002-01-30 16:17:23 +00001533 if( db->flags & SQLITE_CountRows ){
1534 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
1535 }
drh982cef72000-05-30 16:27:03 +00001536 end = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00001537 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
drh982cef72000-05-30 16:27:03 +00001538 if( pDelimiter ){
1539 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
1540 sqliteVdbeDequoteP3(v, addr);
1541 }else{
1542 sqliteVdbeChangeP3(v, addr, "\t", 1);
1543 }
drh8aff1012001-12-22 14:49:24 +00001544 if( pTab->iPKey>=0 ){
1545 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
1546 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
1547 }else{
1548 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
1549 }
drh982cef72000-05-30 16:27:03 +00001550 for(i=0; i<pTab->nCol; i++){
drh8aff1012001-12-22 14:49:24 +00001551 if( i==pTab->iPKey ){
1552 /* The integer primary key column is filled with NULL since its
1553 ** value is always pulled from the record number */
1554 sqliteVdbeAddOp(v, OP_String, 0, 0);
1555 }else{
1556 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
1557 }
drh982cef72000-05-30 16:27:03 +00001558 }
drhb419a922002-01-30 16:17:23 +00001559 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, 0, 0, onError, addr);
1560 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0);
1561 if( (db->flags & SQLITE_CountRows)!=0 ){
1562 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
drh982cef72000-05-30 16:27:03 +00001563 }
drh99fcd712001-10-13 01:06:47 +00001564 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
1565 sqliteVdbeResolveLabel(v, end);
1566 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh1c928532002-01-31 15:54:21 +00001567 sqliteEndWriteOperation(pParse);
drhb419a922002-01-30 16:17:23 +00001568 if( db->flags & SQLITE_CountRows ){
1569 sqliteVdbeAddOp(v, OP_ColumnCount, 1, 0);
1570 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
1571 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
1572 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
1573 }
drh982cef72000-05-30 16:27:03 +00001574 }
1575
1576copy_cleanup:
1577 return;
1578}
drhdce2cbe2000-05-31 02:27:49 +00001579
1580/*
1581** The non-standard VACUUM command is used to clean up the database,
1582** collapse free space, etc. It is modelled after the VACUUM command
1583** in PostgreSQL.
drh1dd397f2002-02-03 03:34:07 +00001584**
drh1bffb9c2002-02-03 17:37:36 +00001585** In version 1.0.x of SQLite, the VACUUM command would call
1586** gdbm_reorganize() on all the database tables. But beginning
1587** with 2.0.0, SQLite no longer uses GDBM so this command has
1588** become a no-op.
drhdce2cbe2000-05-31 02:27:49 +00001589*/
1590void sqliteVacuum(Parse *pParse, Token *pTableName){
drh1bffb9c2002-02-03 17:37:36 +00001591 /* Do nothing */
drhdce2cbe2000-05-31 02:27:49 +00001592}
drhc4a3c772001-04-04 11:48:57 +00001593
1594/*
1595** Begin a transaction
1596*/
drh1c928532002-01-31 15:54:21 +00001597void sqliteBeginTransaction(Parse *pParse, int onError){
drhc4a3c772001-04-04 11:48:57 +00001598 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001599
drhc4a3c772001-04-04 11:48:57 +00001600 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001601 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001602 if( db->flags & SQLITE_InTrans ) return;
drh1c928532002-01-31 15:54:21 +00001603 sqliteBeginWriteOperation(pParse);
drh5e00f6c2001-09-13 13:46:56 +00001604 db->flags |= SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001605 db->onError = onError;
drhc4a3c772001-04-04 11:48:57 +00001606}
1607
1608/*
1609** Commit a transaction
1610*/
1611void sqliteCommitTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001612 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001613
drhc4a3c772001-04-04 11:48:57 +00001614 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001615 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001616 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001617 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001618 sqliteEndWriteOperation(pParse);
1619 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001620}
1621
1622/*
1623** Rollback a transaction
1624*/
1625void sqliteRollbackTransaction(Parse *pParse){
drhc4a3c772001-04-04 11:48:57 +00001626 sqlite *db;
drh5e00f6c2001-09-13 13:46:56 +00001627 Vdbe *v;
1628
drhc4a3c772001-04-04 11:48:57 +00001629 if( pParse==0 || (db=pParse->db)==0 || db->pBe==0 ) return;
drhdaffd0e2001-04-11 14:28:42 +00001630 if( pParse->nErr || sqlite_malloc_failed ) return;
drhc4a3c772001-04-04 11:48:57 +00001631 if( (db->flags & SQLITE_InTrans)==0 ) return;
drh5e00f6c2001-09-13 13:46:56 +00001632 v = sqliteGetVdbe(pParse);
1633 if( v ){
drh99fcd712001-10-13 01:06:47 +00001634 sqliteVdbeAddOp(v, OP_Rollback, 0, 0);
drhc4a3c772001-04-04 11:48:57 +00001635 }
drh5e00f6c2001-09-13 13:46:56 +00001636 db->flags &= ~SQLITE_InTrans;
drh1c928532002-01-31 15:54:21 +00001637 db->onError = OE_Default;
drhc4a3c772001-04-04 11:48:57 +00001638}
drhf57b14a2001-09-14 18:54:08 +00001639
1640/*
drh1c928532002-01-31 15:54:21 +00001641** Generate VDBE code that prepares for doing an operation that
drh663fc632002-02-02 18:49:19 +00001642** might change the database. The operation will be atomic in the
1643** sense that it will either do its changes completely or not at
1644** all. So there is not need to set a checkpoint is a transaction
1645** is already in effect.
drh1c928532002-01-31 15:54:21 +00001646*/
1647void sqliteBeginWriteOperation(Parse *pParse){
1648 Vdbe *v;
1649 v = sqliteGetVdbe(pParse);
1650 if( v==0 ) return;
drh663fc632002-02-02 18:49:19 +00001651 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
drh1c928532002-01-31 15:54:21 +00001652 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1653 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1654 pParse->schemaVerified = 1;
1655 }
1656}
1657
1658/*
drh663fc632002-02-02 18:49:19 +00001659** Generate VDBE code that prepares for doing an operation that
1660** might change the database. The operation might not be atomic in
1661** the sense that an error may be discovered and the operation might
1662** abort after some changes have been made. If we are in the middle
1663** of a transaction, then this sets a checkpoint. If we are not in
1664** a transaction, then start a transaction.
1665*/
1666void sqliteBeginMultiWriteOperation(Parse *pParse){
1667 Vdbe *v;
1668 v = sqliteGetVdbe(pParse);
1669 if( v==0 ) return;
1670 if( (pParse->db->flags & SQLITE_InTrans)==0 ){
1671 sqliteVdbeAddOp(v, OP_Transaction, 0, 0);
1672 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1673 pParse->schemaVerified = 1;
1674 }else{
1675 sqliteVdbeAddOp(v, OP_Checkpoint, 0, 0);
1676 }
1677}
1678
1679/*
drh1c928532002-01-31 15:54:21 +00001680** Generate code that concludes an operation that may have changed
1681** the database. This is a companion function to BeginWriteOperation().
1682** If a transaction was started, then commit it. If a checkpoint was
1683** started then commit that.
1684*/
1685void sqliteEndWriteOperation(Parse *pParse){
1686 Vdbe *v;
1687 v = sqliteGetVdbe(pParse);
1688 if( v==0 ) return;
1689 if( pParse->db->flags & SQLITE_InTrans ){
1690 /* Do Nothing */
1691 }else{
1692 sqliteVdbeAddOp(v, OP_Commit, 0, 0);
1693 }
1694}
1695
1696
1697/*
drhf57b14a2001-09-14 18:54:08 +00001698** Interpret the given string as a boolean value.
1699*/
1700static int getBoolean(char *z){
1701 static char *azTrue[] = { "yes", "on", "true" };
1702 int i;
1703 if( z[0]==0 ) return 0;
1704 if( isdigit(z[0]) || (z[0]=='-' && isdigit(z[1])) ){
1705 return atoi(z);
1706 }
1707 for(i=0; i<sizeof(azTrue)/sizeof(azTrue[0]); i++){
1708 if( sqliteStrICmp(z,azTrue[i])==0 ) return 1;
1709 }
1710 return 0;
1711}
1712
1713/*
1714** Process a pragma statement.
1715**
1716** Pragmas are of this form:
1717**
1718** PRAGMA id = value
1719**
1720** The identifier might also be a string. The value is a string, and
1721** identifier, or a number. If minusFlag is true, then the value is
1722** a number that was preceded by a minus sign.
1723*/
1724void sqlitePragma(Parse *pParse, Token *pLeft, Token *pRight, int minusFlag){
1725 char *zLeft = 0;
1726 char *zRight = 0;
1727 sqlite *db = pParse->db;
1728
1729 zLeft = sqliteStrNDup(pLeft->z, pLeft->n);
1730 sqliteDequote(zLeft);
1731 if( minusFlag ){
1732 zRight = 0;
1733 sqliteSetNString(&zRight, "-", 1, pRight->z, pRight->n, 0);
1734 }else{
1735 zRight = sqliteStrNDup(pRight->z, pRight->n);
1736 sqliteDequote(zRight);
1737 }
1738
drhcd61c282002-03-06 22:01:34 +00001739 /*
1740 ** PRAGMA default_cache_size
1741 ** PRAGMA default_cache_size=N
1742 **
1743 ** The first form reports the current persistent setting for the
1744 ** page cache size. The value returned is the maximum number of
1745 ** pages in the page cache. The second form sets both the current
1746 ** page cache size value and the persistent page cache size value
1747 ** stored in the database file.
1748 **
1749 ** The default cache size is stored in meta-value 2 of page 1 of the
1750 ** database file. The cache size is actually the absolute value of
1751 ** this memory location. The sign of meta-value 2 determines the
1752 ** synchronous setting. A negative value means synchronous is off
1753 ** and a positive value means synchronous is on.
1754 */
1755 if( sqliteStrICmp(zLeft,"default_cache_size")==0 ){
drh603240c2002-03-05 01:11:12 +00001756 static VdbeOp getCacheSize[] = {
1757 { OP_ReadCookie, 0, 2, 0},
1758 { OP_AbsValue, 0, 0, 0},
drhcd61c282002-03-06 22:01:34 +00001759 { OP_Dup, 0, 0, 0},
1760 { OP_Integer, 0, 0, 0},
1761 { OP_Ne, 0, 6, 0},
1762 { OP_Integer, MAX_PAGES,0, 0},
drh603240c2002-03-05 01:11:12 +00001763 { OP_ColumnCount, 1, 0, 0},
1764 { OP_ColumnName, 0, 0, "cache_size"},
1765 { OP_Callback, 1, 0, 0},
1766 };
1767 Vdbe *v = sqliteGetVdbe(pParse);
1768 if( v==0 ) return;
1769 if( pRight->z==pLeft->z ){
1770 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1771 }else{
1772 int addr;
1773 int size = atoi(zRight);
1774 if( size<0 ) size = -size;
1775 sqliteBeginWriteOperation(pParse);
1776 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1777 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
1778 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1779 sqliteVdbeAddOp(v, OP_Ge, 0, addr+3);
1780 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
1781 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
1782 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00001783 db->cache_size = db->cache_size<0 ? -size : size;
1784 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00001785 }
1786 }else
1787
drhcd61c282002-03-06 22:01:34 +00001788 /*
1789 ** PRAGMA cache_size
1790 ** PRAGMA cache_size=N
1791 **
1792 ** The first form reports the current local setting for the
1793 ** page cache size. The local setting can be different from
1794 ** the persistent cache size value that is stored in the database
1795 ** file itself. The value returned is the maximum number of
1796 ** pages in the page cache. The second form sets the local
1797 ** page cache size value. It does not change the persistent
1798 ** cache size stored on the disk so the cache size will revert
1799 ** to its default value when the database is closed and reopened.
1800 ** N should be a positive integer.
1801 */
1802 if( sqliteStrICmp(zLeft,"cache_size")==0 ){
1803 static VdbeOp getCacheSize[] = {
1804 { OP_ColumnCount, 1, 0, 0},
1805 { OP_ColumnName, 0, 0, "cache_size"},
1806 { OP_Callback, 1, 0, 0},
1807 };
1808 Vdbe *v = sqliteGetVdbe(pParse);
1809 if( v==0 ) return;
1810 if( pRight->z==pLeft->z ){
1811 int size = db->cache_size;;
1812 if( size<0 ) size = -size;
1813 sqliteVdbeAddOp(v, OP_Integer, size, 0);
1814 sqliteVdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
1815 }else{
1816 int size = atoi(zRight);
1817 if( size<0 ) size = -size;
1818 if( db->cache_size<0 ) size = -size;
1819 db->cache_size = size;
1820 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
1821 }
1822 }else
1823
1824 /*
1825 ** PRAGMA default_synchronous
1826 ** PRAGMA default_synchronous=BOOLEAN
1827 **
1828 ** The first form returns the persistent value of the "synchronous" setting
1829 ** that is stored in the database. This is the synchronous setting that
1830 ** is used whenever the database is opened unless overridden by a separate
1831 ** "synchronous" pragma. The second form changes the persistent and the
1832 ** local synchronous setting to the value given.
1833 **
1834 ** If synchronous is on, SQLite will do an fsync() system call at strategic
1835 ** points to insure that all previously written data has actually been
1836 ** written onto the disk surface before continuing. This mode insures that
1837 ** the database will always be in a consistent state event if the operating
1838 ** system crashes or power to the computer is interrupted unexpectedly.
1839 ** When synchronous is off, SQLite will not wait for changes to actually
1840 ** be written to the disk before continuing. As soon as it hands changes
1841 ** to the operating system, it assumes that the changes are permanent and
1842 ** it continues going. The database cannot be corrupted by a program crash
1843 ** even with synchronous off, but an operating system crash or power loss
1844 ** could potentially corrupt data. On the other hand, synchronous off is
1845 ** faster than synchronous on.
1846 */
1847 if( sqliteStrICmp(zLeft,"default_synchronous")==0 ){
drh603240c2002-03-05 01:11:12 +00001848 static VdbeOp getSync[] = {
1849 { OP_Integer, 0, 0, 0},
1850 { OP_ReadCookie, 0, 2, 0},
1851 { OP_Integer, 0, 0, 0},
1852 { OP_Lt, 0, 5, 0},
1853 { OP_AddImm, 1, 0, 0},
1854 { OP_ColumnCount, 1, 0, 0},
1855 { OP_ColumnName, 0, 0, "synchronous"},
1856 { OP_Callback, 1, 0, 0},
1857 };
1858 Vdbe *v = sqliteGetVdbe(pParse);
1859 if( v==0 ) return;
1860 if( pRight->z==pLeft->z ){
1861 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
1862 }else{
1863 int addr;
drhcd61c282002-03-06 22:01:34 +00001864 int size = db->cache_size;
1865 if( size<0 ) size = -size;
drh603240c2002-03-05 01:11:12 +00001866 sqliteBeginWriteOperation(pParse);
1867 sqliteVdbeAddOp(v, OP_ReadCookie, 0, 2);
drhcd61c282002-03-06 22:01:34 +00001868 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1869 addr = sqliteVdbeAddOp(v, OP_Integer, 0, 0);
1870 sqliteVdbeAddOp(v, OP_Ne, 0, addr+3);
1871 sqliteVdbeAddOp(v, OP_AddImm, MAX_PAGES, 0);
drh603240c2002-03-05 01:11:12 +00001872 sqliteVdbeAddOp(v, OP_AbsValue, 0, 0);
1873 if( !getBoolean(zRight) ){
1874 sqliteVdbeAddOp(v, OP_Negative, 0, 0);
drhcd61c282002-03-06 22:01:34 +00001875 size = -size;
drh603240c2002-03-05 01:11:12 +00001876 }
1877 sqliteVdbeAddOp(v, OP_SetCookie, 0, 2);
1878 sqliteEndWriteOperation(pParse);
drhcd61c282002-03-06 22:01:34 +00001879 db->cache_size = size;
1880 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
1881 }
1882 }else
1883
1884 /*
1885 ** PRAGMA synchronous
1886 ** PRAGMA synchronous=BOOLEAN
1887 **
1888 ** Return or set the local value of the synchronous flag. Changing
1889 ** the local value does not make changes to the disk file and the
1890 ** default value will be restored the next time the database is
1891 ** opened.
1892 */
1893 if( sqliteStrICmp(zLeft,"synchronous")==0 ){
1894 static VdbeOp getSync[] = {
1895 { OP_ColumnCount, 1, 0, 0},
1896 { OP_ColumnName, 0, 0, "synchronous"},
1897 { OP_Callback, 1, 0, 0},
1898 };
1899 Vdbe *v = sqliteGetVdbe(pParse);
1900 if( v==0 ) return;
1901 if( pRight->z==pLeft->z ){
1902 sqliteVdbeAddOp(v, OP_Integer, db->cache_size>=0, 0);
1903 sqliteVdbeAddOpList(v, ArraySize(getSync), getSync);
1904 }else{
1905 int size = db->cache_size;
1906 if( size<0 ) size = -size;
1907 if( !getBoolean(zRight) ) size = -size;
1908 db->cache_size = size;
1909 sqliteBtreeSetCacheSize(db->pBe, db->cache_size);
drh603240c2002-03-05 01:11:12 +00001910 }
drhf57b14a2001-09-14 18:54:08 +00001911 }else
1912
1913 if( sqliteStrICmp(zLeft, "vdbe_trace")==0 ){
1914 if( getBoolean(zRight) ){
1915 db->flags |= SQLITE_VdbeTrace;
1916 }else{
1917 db->flags &= ~SQLITE_VdbeTrace;
1918 }
1919 }else
1920
drh382c0242001-10-06 16:33:02 +00001921 if( sqliteStrICmp(zLeft, "full_column_names")==0 ){
1922 if( getBoolean(zRight) ){
1923 db->flags |= SQLITE_FullColNames;
1924 }else{
1925 db->flags &= ~SQLITE_FullColNames;
1926 }
1927 }else
1928
drhc3a64ba2001-11-22 00:01:27 +00001929 if( sqliteStrICmp(zLeft, "result_set_details")==0 ){
1930 if( getBoolean(zRight) ){
1931 db->flags |= SQLITE_ResultDetails;
1932 }else{
1933 db->flags &= ~SQLITE_ResultDetails;
1934 }
1935 }else
1936
drh1bee3d72001-10-15 00:44:35 +00001937 if( sqliteStrICmp(zLeft, "count_changes")==0 ){
1938 if( getBoolean(zRight) ){
1939 db->flags |= SQLITE_CountRows;
1940 }else{
1941 db->flags &= ~SQLITE_CountRows;
1942 }
1943 }else
1944
drh6a535342001-10-19 16:44:56 +00001945 if( sqliteStrICmp(zLeft, "empty_result_callbacks")==0 ){
1946 if( getBoolean(zRight) ){
1947 db->flags |= SQLITE_NullCallback;
1948 }else{
1949 db->flags &= ~SQLITE_NullCallback;
1950 }
1951 }else
1952
drh382c0242001-10-06 16:33:02 +00001953 if( sqliteStrICmp(zLeft, "table_info")==0 ){
1954 Table *pTab;
1955 Vdbe *v;
1956 pTab = sqliteFindTable(db, zRight);
1957 if( pTab ) v = sqliteGetVdbe(pParse);
1958 if( pTab && v ){
1959 static VdbeOp tableInfoPreface[] = {
1960 { OP_ColumnCount, 5, 0, 0},
1961 { OP_ColumnName, 0, 0, "cid"},
1962 { OP_ColumnName, 1, 0, "name"},
1963 { OP_ColumnName, 2, 0, "type"},
1964 { OP_ColumnName, 3, 0, "notnull"},
1965 { OP_ColumnName, 4, 0, "dflt_value"},
1966 };
1967 int i;
1968 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
drh417be792002-03-03 18:59:40 +00001969 sqliteViewGetColumnNames(pParse, pTab);
drh382c0242001-10-06 16:33:02 +00001970 for(i=0; i<pTab->nCol; i++){
drh99fcd712001-10-13 01:06:47 +00001971 sqliteVdbeAddOp(v, OP_Integer, i, 0);
1972 sqliteVdbeAddOp(v, OP_String, 0, 0);
1973 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zName, P3_STATIC);
1974 sqliteVdbeAddOp(v, OP_String, 0, 0);
1975 sqliteVdbeChangeP3(v, -1,
1976 pTab->aCol[i].zType ? pTab->aCol[i].zType : "text", P3_STATIC);
1977 sqliteVdbeAddOp(v, OP_Integer, pTab->aCol[i].notNull, 0);
1978 sqliteVdbeAddOp(v, OP_String, 0, 0);
1979 sqliteVdbeChangeP3(v, -1, pTab->aCol[i].zDflt, P3_STATIC);
1980 sqliteVdbeAddOp(v, OP_Callback, 5, 0);
drh382c0242001-10-06 16:33:02 +00001981 }
1982 }
1983 }else
1984
1985 if( sqliteStrICmp(zLeft, "index_info")==0 ){
1986 Index *pIdx;
1987 Table *pTab;
1988 Vdbe *v;
1989 pIdx = sqliteFindIndex(db, zRight);
1990 if( pIdx ) v = sqliteGetVdbe(pParse);
1991 if( pIdx && v ){
1992 static VdbeOp tableInfoPreface[] = {
1993 { OP_ColumnCount, 3, 0, 0},
1994 { OP_ColumnName, 0, 0, "seqno"},
1995 { OP_ColumnName, 1, 0, "cid"},
1996 { OP_ColumnName, 2, 0, "name"},
1997 };
1998 int i;
1999 pTab = pIdx->pTable;
2000 sqliteVdbeAddOpList(v, ArraySize(tableInfoPreface), tableInfoPreface);
2001 for(i=0; i<pIdx->nColumn; i++){
drh99fcd712001-10-13 01:06:47 +00002002 int cnum = pIdx->aiColumn[i];
2003 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2004 sqliteVdbeAddOp(v, OP_Integer, cnum, 0);
2005 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh417be792002-03-03 18:59:40 +00002006 assert( pTab->nCol>cnum );
drh99fcd712001-10-13 01:06:47 +00002007 sqliteVdbeChangeP3(v, -1, pTab->aCol[cnum].zName, P3_STATIC);
2008 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh382c0242001-10-06 16:33:02 +00002009 }
2010 }
2011 }else
2012
drh81a20f22001-10-12 17:30:04 +00002013 if( sqliteStrICmp(zLeft, "index_list")==0 ){
2014 Index *pIdx;
2015 Table *pTab;
2016 Vdbe *v;
2017 pTab = sqliteFindTable(db, zRight);
2018 if( pTab ){
2019 v = sqliteGetVdbe(pParse);
2020 pIdx = pTab->pIndex;
2021 }
2022 if( pTab && pIdx && v ){
2023 int i = 0;
2024 static VdbeOp indexListPreface[] = {
2025 { OP_ColumnCount, 3, 0, 0},
2026 { OP_ColumnName, 0, 0, "seq"},
2027 { OP_ColumnName, 1, 0, "name"},
2028 { OP_ColumnName, 2, 0, "unique"},
2029 };
2030
2031 sqliteVdbeAddOpList(v, ArraySize(indexListPreface), indexListPreface);
2032 while(pIdx){
drh99fcd712001-10-13 01:06:47 +00002033 sqliteVdbeAddOp(v, OP_Integer, i, 0);
2034 sqliteVdbeAddOp(v, OP_String, 0, 0);
2035 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9cfcf5d2002-01-29 18:41:24 +00002036 sqliteVdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0);
drh99fcd712001-10-13 01:06:47 +00002037 sqliteVdbeAddOp(v, OP_Callback, 3, 0);
drh81a20f22001-10-12 17:30:04 +00002038 ++i;
2039 pIdx = pIdx->pNext;
2040 }
2041 }
2042 }else
2043
drhf57b14a2001-09-14 18:54:08 +00002044#ifndef NDEBUG
2045 if( sqliteStrICmp(zLeft, "parser_trace")==0 ){
2046 extern void sqliteParserTrace(FILE*, char *);
2047 if( getBoolean(zRight) ){
2048 sqliteParserTrace(stdout, "parser: ");
2049 }else{
2050 sqliteParserTrace(0, 0);
2051 }
2052 }else
2053#endif
2054
drhaaab5722002-02-19 13:39:21 +00002055 if( sqliteStrICmp(zLeft, "integrity_check")==0 ){
drh1bffb9c2002-02-03 17:37:36 +00002056 static VdbeOp checkDb[] = {
2057 { OP_SetInsert, 0, 0, "2"},
2058 { OP_Open, 0, 2, 0},
2059 { OP_Rewind, 0, 6, 0},
2060 { OP_Column, 0, 3, 0},
2061 { OP_SetInsert, 0, 0, 0},
2062 { OP_Next, 0, 3, 0},
drhaaab5722002-02-19 13:39:21 +00002063 { OP_IntegrityCk, 0, 0, 0},
drh1bffb9c2002-02-03 17:37:36 +00002064 { OP_ColumnCount, 1, 0, 0},
drh4ff6dfa2002-03-03 23:06:00 +00002065 { OP_ColumnName, 0, 0, "integrity_check"},
drh1bffb9c2002-02-03 17:37:36 +00002066 { OP_Callback, 1, 0, 0},
2067 };
2068 Vdbe *v = sqliteGetVdbe(pParse);
2069 if( v==0 ) return;
2070 sqliteVdbeAddOpList(v, ArraySize(checkDb), checkDb);
2071 }else
drh1bffb9c2002-02-03 17:37:36 +00002072
drhf57b3392001-10-08 13:22:32 +00002073 {}
2074 sqliteFree(zLeft);
2075 sqliteFree(zRight);
drhf57b14a2001-09-14 18:54:08 +00002076}