/ Check-in [9c1432bf]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added support for the "sqlite_temp_master" table. Increased the version number to 2.5.2. (CVS 640)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:9c1432bf7485258e485bd652e3acdaeabbfe8850
User & Date: drh 2002-06-25 01:09:11
Context
2002-06-25
01:09
Version 2.5.2 (CVS 641) check-in: 756310ca user: drh tags: trunk
01:09
Added support for the "sqlite_temp_master" table. Increased the version number to 2.5.2. (CVS 640) check-in: 9c1432bf user: drh tags: trunk
2002-06-24
22:01
Partial fix for a problem with LEFT OUTER JOIN. It used to be that the test for the right-hand table not matching the left table occurred after all ON, USING, WHERE clause processing. The test should occur after ON and USING clauses are checked but before the WHERE clause is check. This fix works as long as the total number of "AND" separated terms in the ON, USING, and WHERE clause does not exceed 32. To do: make this work for any number of terms and add test cases. that (CVS 639) check-in: 8b6574cf user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to VERSION.

     1         -2.5.1
            1  +2.5.2

Changes to src/build.c.

    21     21   **     COPY
    22     22   **     VACUUM
    23     23   **     BEGIN TRANSACTION
    24     24   **     COMMIT
    25     25   **     ROLLBACK
    26     26   **     PRAGMA
    27     27   **
    28         -** $Id: build.c,v 1.97 2002/06/20 11:36:49 drh Exp $
           28  +** $Id: build.c,v 1.98 2002/06/25 01:09:11 drh Exp $
    29     29   */
    30     30   #include "sqliteInt.h"
    31     31   #include <ctype.h>
           32  +
           33  +/*
           34  +** This routine is called when a new SQL statement is beginning to
           35  +** be parsed.  Check to see if the schema for the database needs
           36  +** to be read from the SQLITE_MASTER and SQLITE_TEMP_MASTER tables.
           37  +** If it does, then read it.
           38  +*/
           39  +void sqliteBeginParse(Parse *pParse, int explainFlag){
           40  +  sqlite *db = pParse->db;
           41  +  pParse->explain = explainFlag;
           42  +  if((db->flags & SQLITE_Initialized)==0 && pParse->initFlag==0 ){
           43  +    int rc = sqliteInit(db, &pParse->zErrMsg);
           44  +    if( rc!=SQLITE_OK ){
           45  +      pParse->rc = rc;
           46  +      pParse->nErr++;
           47  +    }
           48  +  }
           49  +}
    32     50   
    33     51   /*
    34     52   ** This routine is called after a single SQL statement has been
    35     53   ** parsed and we want to execute the VDBE code to implement 
    36     54   ** that statement.  Prior action routines should have already
    37     55   ** constructed VDBE code to do the work of the SQL statement.
    38     56   ** This routine just has to execute the VDBE code.
................................................................................
    44     62     int rc = SQLITE_OK;
    45     63     sqlite *db = pParse->db;
    46     64     if( sqlite_malloc_failed ) return;
    47     65     if( pParse->pVdbe && pParse->nErr==0 ){
    48     66       if( pParse->explain ){
    49     67         rc = sqliteVdbeList(pParse->pVdbe, pParse->xCallback, pParse->pArg, 
    50     68                             &pParse->zErrMsg);
           69  +      db->next_cookie = db->schema_cookie;
    51     70       }else{
    52     71         FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0;
    53     72         sqliteVdbeTrace(pParse->pVdbe, trace);
    54     73         rc = sqliteVdbeExec(pParse->pVdbe, pParse->xCallback, pParse->pArg, 
    55     74                             &pParse->zErrMsg, db->pBusyArg,
    56     75                             db->xBusyCallback);
    57     76         if( rc ) pParse->nErr++;
................................................................................
    66     85   
    67     86   /*
    68     87   ** Locate the in-memory structure that describes 
    69     88   ** a particular database table given the name
    70     89   ** of that table.  Return NULL if not found.
    71     90   */
    72     91   Table *sqliteFindTable(sqlite *db, const char *zName){
    73         -  Table *p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
           92  +  Table *p;
           93  +  p = sqliteHashFind(&db->tblHash, zName, strlen(zName)+1);
    74     94     return p;
    75     95   }
    76     96   
    77     97   /*
    78     98   ** Locate the in-memory structure that describes 
    79     99   ** a particular index given the name of that index.
    80    100   ** Return NULL if not found.
    81    101   */
    82    102   Index *sqliteFindIndex(sqlite *db, const char *zName){
    83         -  Index *p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
          103  +  Index *p;
          104  +  p = sqliteHashFind(&db->idxHash, zName, strlen(zName)+1);
    84    105     return p;
    85    106   }
    86    107   
    87    108   /*
    88    109   ** Remove the given index from the index hash table, and free
    89    110   ** its memory structures.
    90    111   **
................................................................................
    95    116   static void sqliteDeleteIndex(sqlite *db, Index *p){
    96    117     Index *pOld;
    97    118     assert( db!=0 && p->zName!=0 );
    98    119     pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
    99    120     if( pOld!=0 && pOld!=p ){
   100    121       sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
   101    122     }
   102         -  sqliteHashInsert(&db->idxDrop, p, 0, 0);
   103    123     sqliteFree(p);
   104    124   }
   105    125   
   106    126   /*
   107    127   ** Unlink the given index from its table, then remove
   108    128   ** the index from the index hash table and free its memory
   109    129   ** structures.
................................................................................
   118    138         p->pNext = pIndex->pNext;
   119    139       }
   120    140     }
   121    141     sqliteDeleteIndex(db, pIndex);
   122    142   }
   123    143   
   124    144   /*
   125         -** 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         -*/
   133         -static void sqlitePendingDropIndex(sqlite *db, Index *p){
   134         -  if( !p->isCommit ){
   135         -    sqliteUnlinkAndDeleteIndex(db, p);
   136         -  }else{
   137         -    Index *pOld;
   138         -    pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, 0);
   139         -    if( pOld!=0 && pOld!=p ){
   140         -      sqliteHashInsert(&db->idxHash, pOld->zName, strlen(pOld->zName)+1, pOld);
   141         -    }
   142         -    sqliteHashInsert(&db->idxDrop, p, 0, p);
   143         -    p->isDropped = 1;
   144         -  }
          145  +** Erase all schema information from the in-memory hash tables of
          146  +** database connection.  This routine is called to reclaim memory
          147  +** before the connection closes.  It is also called during a rollback
          148  +** if there were schema changes during the transaction.
          149  +*/
          150  +void sqliteResetInternalSchema(sqlite *db){
          151  +  HashElem *pElem;
          152  +  Hash temp1;
          153  +  Hash temp2;
          154  +
          155  +  temp1 = db->tblHash;
          156  +  temp2 = db->trigHash;
          157  +  sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
          158  +  sqliteHashClear(&db->idxHash);
          159  +  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
          160  +    Trigger *pTrigger = sqliteHashData(pElem);
          161  +    sqliteDeleteTrigger(pTrigger);
          162  +  }
          163  +  sqliteHashClear(&temp2);
          164  +  sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
          165  +  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
          166  +    Table *pTab = sqliteHashData(pElem);
          167  +    sqliteDeleteTable(db, pTab);
          168  +  }
          169  +  sqliteHashClear(&temp1);
          170  +  db->flags &= ~(SQLITE_Initialized|SQLITE_InternChanges);
          171  +}
          172  +
          173  +/*
          174  +** This routine is called whenever a rollback occurs.  If there were
          175  +** schema changes during the transaction, then we have to reset the
          176  +** internal hash tables and reload them from disk.
          177  +*/
          178  +void sqliteRollbackInternalChanges(sqlite *db){
          179  +  if( db->flags & SQLITE_InternChanges ){
          180  +    sqliteResetInternalSchema(db);
          181  +  }
          182  +}
          183  +
          184  +/*
          185  +** This routine is called when a commit occurs.
          186  +*/
          187  +void sqliteCommitInternalChanges(sqlite *db){
          188  +  db->schema_cookie = db->next_cookie;
          189  +  db->flags &= ~SQLITE_InternChanges;
   145    190   }
   146    191   
   147    192   /*
   148    193   ** Remove the memory data structures associated with the given
   149    194   ** Table.  No changes are made to disk by this routine.
   150    195   **
   151    196   ** This routine just deletes the data structure.  It does not unlink
................................................................................
   181    226   ** table structure with all its indices.
   182    227   */
   183    228   static void sqliteUnlinkAndDeleteTable(sqlite *db, Table *p){
   184    229     Table *pOld;
   185    230     assert( db!=0 );
   186    231     pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, 0);
   187    232     assert( pOld==0 || pOld==p );
   188         -  sqliteHashInsert(&db->tblDrop, p, 0, 0);
   189    233     sqliteDeleteTable(db, p);
   190    234   }
   191    235   
   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         -*/
   202         -static void sqlitePendingDropTable(sqlite *db, Table *pTbl){
   203         -  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         -  }
   216         -}
   217         -
   218         -/*
   219         -** 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.
   225         -** 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.
   228         -**
   229         -** See also: sqliteRollbackInternalChanges()
   230         -*/
   231         -void sqliteCommitInternalChanges(sqlite *db){
   232         -  HashElem *pElem;
   233         -  if( (db->flags & SQLITE_InternChanges)==0 ) return;
   234         -  db->schema_cookie = db->next_cookie;
   235         -  for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
   236         -    Table *pTable = sqliteHashData(pElem);
   237         -    pTable->isCommit = 1;
   238         -  }
   239         -  for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
   240         -    Table *pTable = sqliteHashData(pElem);
   241         -    sqliteDeleteTable(db, pTable);
   242         -  }
   243         -  sqliteHashClear(&db->tblDrop);
   244         -  for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
   245         -    Index *pIndex = sqliteHashData(pElem);
   246         -    pIndex->isCommit = 1;
   247         -  }
   248         -  while( (pElem=sqliteHashFirst(&db->idxDrop))!=0 ){
   249         -    Index *pIndex = sqliteHashData(pElem);
   250         -    sqliteUnlinkAndDeleteIndex(db, pIndex);
   251         -  }
   252         -  sqliteHashClear(&db->idxDrop);
   253         -
   254         -  /* Set the commit flag on all triggers added this transaction */
   255         -  for(pElem=sqliteHashFirst(&db->trigHash); pElem; pElem=sqliteHashNext(pElem)){
   256         -    Trigger *pTrigger = sqliteHashData(pElem);
   257         -    pTrigger->isCommit = 1;
   258         -  }
   259         -
   260         -  /* Delete the structures for triggers removed this transaction */
   261         -  pElem = sqliteHashFirst(&db->trigDrop);
   262         -  while( pElem ){
   263         -    Trigger *pTrigger = sqliteHashData(pElem);
   264         -    sqliteDeleteTrigger(pTrigger);
   265         -    pElem = sqliteHashNext(pElem);
   266         -  }
   267         -  sqliteHashClear(&db->trigDrop);
   268         -
   269         -  db->flags &= ~SQLITE_InternChanges;
   270         -}
   271         -
   272         -/*
   273         -** This routine runs when one or more CREATE TABLE, CREATE INDEX,
   274         -** DROP TABLE, or DROP INDEX statements gets rolled back.  The
   275         -** additions or deletions of Table and Index structures in the
   276         -** internal hash tables are undone.
   277         -**
   278         -** See also: sqliteCommitInternalChanges()
   279         -*/
   280         -void sqliteRollbackInternalChanges(sqlite *db){
   281         -  Hash toDelete;
   282         -  HashElem *pElem;
   283         -  if( (db->flags & SQLITE_InternChanges)==0 ) return;
   284         -  sqliteHashInit(&toDelete, SQLITE_HASH_POINTER, 0);
   285         -  db->next_cookie = db->schema_cookie;
   286         -  for(pElem=sqliteHashFirst(&db->tblHash); pElem; pElem=sqliteHashNext(pElem)){
   287         -    Table *pTable = sqliteHashData(pElem);
   288         -    if( !pTable->isCommit ){
   289         -      sqliteHashInsert(&toDelete, pTable, 0, pTable);
   290         -    }
   291         -  }
   292         -  for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
   293         -    Table *pTable = sqliteHashData(pElem);
   294         -    sqliteUnlinkAndDeleteTable(db, pTable);
   295         -  }
   296         -  sqliteHashClear(&toDelete);
   297         -  for(pElem=sqliteHashFirst(&db->tblDrop); pElem; pElem=sqliteHashNext(pElem)){
   298         -    Table *pOld, *p = sqliteHashData(pElem);
   299         -    assert( p->isCommit );
   300         -    pOld = sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
   301         -    assert( pOld==0 || pOld==p );
   302         -  }
   303         -  sqliteHashClear(&db->tblDrop);
   304         -  for(pElem=sqliteHashFirst(&db->idxHash); pElem; pElem=sqliteHashNext(pElem)){
   305         -    Index *pIndex = sqliteHashData(pElem);
   306         -    if( !pIndex->isCommit ){
   307         -      sqliteHashInsert(&toDelete, pIndex, 0, pIndex);
   308         -    }
   309         -  }
   310         -  for(pElem=sqliteHashFirst(&toDelete); pElem; pElem=sqliteHashNext(pElem)){
   311         -    Index *pIndex = sqliteHashData(pElem);
   312         -    sqliteUnlinkAndDeleteIndex(db, pIndex);
   313         -  }
   314         -  sqliteHashClear(&toDelete);
   315         -  for(pElem=sqliteHashFirst(&db->idxDrop); pElem; pElem=sqliteHashNext(pElem)){
   316         -    Index *pOld, *p = sqliteHashData(pElem);
   317         -    assert( p->isCommit );
   318         -    p->isDropped = 0;
   319         -    pOld = sqliteHashInsert(&db->idxHash, p->zName, strlen(p->zName)+1, p);
   320         -    assert( pOld==0 || pOld==p );
   321         -  }
   322         -  sqliteHashClear(&db->idxDrop);
   323         -
   324         -  /* Remove any triggers that haven't been commited yet */
   325         -  for(pElem = sqliteHashFirst(&db->trigHash); pElem; 
   326         -      pElem = (pElem?sqliteHashNext(pElem):0)){
   327         -    Trigger *pTrigger = sqliteHashData(pElem);
   328         -    if( !pTrigger->isCommit ){
   329         -      Table *pTbl = sqliteFindTable(db, pTrigger->table);
   330         -      if( pTbl ){
   331         -        if( pTbl->pTrigger == pTrigger ){
   332         -          pTbl->pTrigger = pTrigger->pNext;
   333         -        }else{
   334         -          Trigger *cc = pTbl->pTrigger;
   335         -          while( cc ){
   336         -            if( cc->pNext == pTrigger ){
   337         -              cc->pNext = cc->pNext->pNext;
   338         -              break;
   339         -            }
   340         -            cc = cc->pNext;
   341         -          }
   342         -          assert(cc);
   343         -        }
   344         -      }
   345         -      sqliteHashInsert(&db->trigHash, pTrigger->name,
   346         -              1 + strlen(pTrigger->name), 0);
   347         -      sqliteDeleteTrigger(pTrigger);
   348         -      pElem = sqliteHashFirst(&db->trigHash);
   349         -    }
   350         -  }
   351         -
   352         -  /* Any triggers that were dropped - put 'em back in place */
   353         -  for(pElem = sqliteHashFirst(&db->trigDrop); pElem; 
   354         -      pElem = sqliteHashNext(pElem)){
   355         -    Trigger *pTrigger = sqliteHashData(pElem);
   356         -    Table *pTbl = sqliteFindTable(db, pTrigger->table);
   357         -    sqliteHashInsert(&db->trigHash, pTrigger->name, 
   358         -        strlen(pTrigger->name) + 1, pTrigger);
   359         -    pTrigger->pNext = pTbl->pTrigger;
   360         -    pTbl->pTrigger = pTrigger;
   361         -  }
   362         -
   363         -  sqliteHashClear(&db->trigDrop);
   364         -  db->flags &= ~SQLITE_InternChanges;
   365         -}
   366         -
   367    236   /*
   368    237   ** Construct the name of a user table or index from a token.
   369    238   **
   370    239   ** Space to hold the name is obtained from sqliteMalloc() and must
   371    240   ** be freed by the calling function.
   372    241   */
   373    242   char *sqliteTableNameFromToken(Token *pName){
   374    243     char *zName = sqliteStrNDup(pName->z, pName->n);
   375    244     sqliteDequote(zName);
   376    245     return zName;
   377    246   }
          247  +
          248  +/*
          249  +** Generate code to open the appropriate master table.  The table
          250  +** opened will be SQLITE_MASTER for persistent tables and 
          251  +** SQLITE_TEMP_MASTER for temporary tables.  The table is opened
          252  +** on cursor 0.
          253  +*/
          254  +void sqliteOpenMasterTable(Vdbe *v, int isTemp){
          255  +  if( isTemp ){
          256  +    sqliteVdbeAddOp(v, OP_OpenWrAux, 0, 2);
          257  +    sqliteVdbeChangeP3(v, -1, TEMP_MASTER_NAME, P3_STATIC);
          258  +  }else{
          259  +    sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
          260  +    sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
          261  +  }
          262  +}
   378    263   
   379    264   /*
   380    265   ** Begin constructing a new table representation in memory.  This is
   381    266   ** the first of several action routines that get called in response
   382    267   ** to a CREATE TABLE statement.  In particular, this routine is called
   383    268   ** after seeing tokens "CREATE" and "TABLE" and the table name.  The
   384    269   ** pStart token is the CREATE and pName is the table name.  The isTemp
   385         -** flag is true if the "TEMP" or "TEMPORARY" keyword occurs in between
          270  +** flag is true if the table should be stored in the auxiliary database
          271  +** file instead of in the main database file.  This is normally the case
          272  +** when the "TEMP" or "TEMPORARY" keyword occurs in between
   386    273   ** CREATE and TABLE.
   387    274   **
   388    275   ** The new table record is initialized and put in pParse->pNewTable.
   389    276   ** As more of the CREATE TABLE statement is parsed, additional action
   390    277   ** routines will be called to add more information to this record.
   391    278   ** At the end of the CREATE TABLE statement, the sqliteEndTable() routine
   392    279   ** is called to complete the construction of the new table record.
................................................................................
   404    291   
   405    292     /* Before trying to create a temporary table, make sure the Btree for
   406    293     ** holding temporary tables is open.
   407    294     */
   408    295     if( isTemp && db->pBeTemp==0 ){
   409    296       int rc = sqliteBtreeOpen(0, 0, MAX_PAGES, &db->pBeTemp);
   410    297       if( rc!=SQLITE_OK ){
   411         -      sqliteSetNString(&pParse->zErrMsg, "unable to open a temporary database "
          298  +      sqliteSetString(&pParse->zErrMsg, "unable to open a temporary database "
   412    299           "file for storing temporary tables", 0);
   413    300         pParse->nErr++;
   414    301         return;
   415    302       }
   416    303       if( db->flags & SQLITE_InTrans ){
   417    304         rc = sqliteBtreeBeginTrans(db->pBeTemp);
   418    305         if( rc!=SQLITE_OK ){
................................................................................
   478    365     ** now.
   479    366     */
   480    367     if( !pParse->initFlag && (v = sqliteGetVdbe(pParse))!=0 ){
   481    368       sqliteBeginWriteOperation(pParse, 0);
   482    369       if( !isTemp ){
   483    370         sqliteVdbeAddOp(v, OP_Integer, db->file_format, 0);
   484    371         sqliteVdbeAddOp(v, OP_SetCookie, 0, 1);
   485         -      sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
   486         -      sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
   487         -      sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
   488         -      sqliteVdbeAddOp(v, OP_Dup, 0, 0);
   489         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
   490         -      sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
   491    372       }
          373  +    sqliteOpenMasterTable(v, isTemp);
          374  +    sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
          375  +    sqliteVdbeAddOp(v, OP_Dup, 0, 0);
          376  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
          377  +    sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
   492    378     }
   493    379   }
   494    380   
   495    381   /*
   496    382   ** Add a new column to the table currently being constructed.
   497    383   **
   498    384   ** The parser calls this routine once for each column declaration
................................................................................
   721    607   **
   722    608   ** This plan is not completely bullet-proof.  It is possible for
   723    609   ** the schema to change multiple times and for the cookie to be
   724    610   ** set back to prior value.  But schema changes are infrequent
   725    611   ** and the probability of hitting the same cookie value is only
   726    612   ** 1 chance in 2^32.  So we're safe enough.
   727    613   */
   728         -void sqliteChangeCookie(sqlite *db){
          614  +void sqliteChangeCookie(sqlite *db, Vdbe *v){
   729    615     if( db->next_cookie==db->schema_cookie ){
   730    616       db->next_cookie = db->schema_cookie + sqliteRandomByte() + 1;
   731    617       db->flags |= SQLITE_InternChanges;
          618  +    sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
          619  +    sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
   732    620     }
   733    621   }
   734    622   
   735    623   /*
   736    624   ** Measure the number of characters needed to output the given
   737    625   ** identifier.  The number returned includes any quotes used
   738    626   ** but does not include the null terminator.
................................................................................
   787    675       zSep2 = ",";
   788    676       zEnd = ")";
   789    677     }else{
   790    678       zSep = "\n  ";
   791    679       zSep2 = ",\n  ";
   792    680       zEnd = "\n)";
   793    681     }
   794         -  n += 25 + 6*p->nCol;
          682  +  n += 35 + 6*p->nCol;
   795    683     zStmt = sqliteMalloc( n );
   796    684     if( zStmt==0 ) return 0;
   797         -  assert( !p->isTemp );
   798         -  strcpy(zStmt, "CREATE TABLE ");
          685  +  strcpy(zStmt, p->isTemp ? "CREATE TEMP TABLE " : "CREATE TABLE ");
   799    686     k = strlen(zStmt);
   800    687     identPut(zStmt, &k, p->zName);
   801    688     zStmt[k++] = '(';
   802    689     for(i=0; i<p->nCol; i++){
   803    690       strcpy(&zStmt[k], zSep);
   804    691       k += strlen(&zStmt[k]);
   805    692       zSep = zSep2;
................................................................................
   863    750       p->aCol = pSelTab->aCol;
   864    751       pSelTab->nCol = 0;
   865    752       pSelTab->aCol = 0;
   866    753       sqliteDeleteTable(0, pSelTab);
   867    754     }
   868    755   
   869    756     /* If the initFlag is 1 it means we are reading the SQL off the
   870         -  ** "sqlite_master" table on the disk.  So do not write to the disk
   871         -  ** again.  Extract the root page number for the table from the 
   872         -  ** pParse->newTnum field.  (The page number should have been put
   873         -  ** there by the sqliteOpenCb routine.)
          757  +  ** "sqlite_master" or "sqlite_temp_master" table on the disk.
          758  +  ** So do not write to the disk again.  Extract the root page number
          759  +  ** for the table from the pParse->newTnum field.  (The page number
          760  +  ** should have been put there by the sqliteOpenCb routine.)
   874    761     */
   875    762     if( pParse->initFlag ){
   876    763       p->tnum = pParse->newTnum;
   877    764     }
   878    765   
   879    766     /* If not initializing, then create a record for the new table
   880    767     ** in the SQLITE_MASTER table of the database.  The record number
   881    768     ** for the new table entry should already be on the stack.
   882    769     **
   883         -  ** If this is a TEMPORARY table, then just create the table.  Do not
   884         -  ** make an entry in SQLITE_MASTER.
          770  +  ** If this is a TEMPORARY table, write the entry into the auxiliary
          771  +  ** file instead of into the main database file.
   885    772     */
   886    773     if( !pParse->initFlag ){
   887    774       int n;
   888    775       Vdbe *v;
   889    776   
   890    777       v = sqliteGetVdbe(pParse);
   891    778       if( v==0 ) return;
................................................................................
   894    781         sqliteVdbeAddOp(v, OP_CreateTable, 0, p->isTemp);
   895    782         sqliteVdbeChangeP3(v, -1, (char *)&p->tnum, P3_POINTER);
   896    783       }else{
   897    784         /* A view */
   898    785         sqliteVdbeAddOp(v, OP_Integer, 0, 0);
   899    786       }
   900    787       p->tnum = 0;
          788  +    sqliteVdbeAddOp(v, OP_Pull, 1, 0);
          789  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
          790  +    if( p->pSelect==0 ){
          791  +      sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
          792  +    }else{
          793  +      sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
          794  +    }
          795  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
          796  +    sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
          797  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
          798  +    sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
          799  +    sqliteVdbeAddOp(v, OP_Dup, 4, 0);
          800  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
          801  +    if( pSelect ){
          802  +      char *z = createTableStmt(p);
          803  +      n = z ? strlen(z) : 0;
          804  +      sqliteVdbeChangeP3(v, -1, z, n);
          805  +      sqliteFree(z);
          806  +    }else{
          807  +      assert( pEnd!=0 );
          808  +      n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
          809  +      sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
          810  +    }
          811  +    sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
          812  +    sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
   901    813       if( !p->isTemp ){
   902         -      sqliteVdbeAddOp(v, OP_Pull, 1, 0);
   903         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
   904         -      if( p->pSelect==0 ){
   905         -        sqliteVdbeChangeP3(v, -1, "table", P3_STATIC);
   906         -      }else{
   907         -        sqliteVdbeChangeP3(v, -1, "view", P3_STATIC);
   908         -      }
   909         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
   910         -      sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
   911         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
   912         -      sqliteVdbeChangeP3(v, -1, p->zName, P3_STATIC);
   913         -      sqliteVdbeAddOp(v, OP_Dup, 4, 0);
   914         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
   915         -      if( pSelect ){
   916         -        char *z = createTableStmt(p);
   917         -        n = z ? strlen(z) : 0;
   918         -        sqliteVdbeChangeP3(v, -1, z, n);
   919         -        sqliteFree(z);
   920         -      }else{
   921         -        assert( pEnd!=0 );
   922         -        n = Addr(pEnd->z) - Addr(pParse->sFirstToken.z) + 1;
   923         -        sqliteVdbeChangeP3(v, -1, pParse->sFirstToken.z, n);
   924         -      }
   925         -      sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
   926         -      sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
   927         -      sqliteChangeCookie(db);
   928         -      sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
   929         -      sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
   930         -      sqliteVdbeAddOp(v, OP_Close, 0, 0);
          814  +      sqliteChangeCookie(db, v);
   931    815       }
          816  +    sqliteVdbeAddOp(v, OP_Close, 0, 0);
   932    817       if( pSelect ){
   933    818         int op = p->isTemp ? OP_OpenWrAux : OP_OpenWrite;
   934    819         sqliteVdbeAddOp(v, op, 1, 0);
   935    820         pParse->nTab = 2;
   936    821         sqliteSelect(pParse, pSelect, SRT_Table, 1, 0, 0, 0);
   937    822       }
   938    823       sqliteEndWriteOperation(pParse);
................................................................................
  1147   1032   
  1148   1033     /* Generate code to remove the table from the master table
  1149   1034     ** on disk.
  1150   1035     */
  1151   1036     v = sqliteGetVdbe(pParse);
  1152   1037     if( v ){
  1153   1038       static VdbeOp dropTable[] = {
  1154         -      { OP_OpenWrite,  0, 2,        MASTER_NAME},
  1155         -      { OP_Rewind,     0, ADDR(9),  0},
  1156         -      { OP_String,     0, 0,        0}, /* 2 */
         1039  +      { OP_Rewind,     0, ADDR(8),  0},
         1040  +      { OP_String,     0, 0,        0}, /* 1 */
  1157   1041         { OP_MemStore,   1, 1,        0},
  1158         -      { OP_MemLoad,    1, 0,        0}, /* 4 */
         1042  +      { OP_MemLoad,    1, 0,        0}, /* 3 */
  1159   1043         { OP_Column,     0, 2,        0},
  1160         -      { OP_Ne,         0, ADDR(8),  0},
         1044  +      { OP_Ne,         0, ADDR(7),  0},
  1161   1045         { OP_Delete,     0, 0,        0},
  1162         -      { OP_Next,       0, ADDR(4),  0}, /* 8 */
  1163         -      { OP_Integer,    0, 0,        0}, /* 9 */
  1164         -      { OP_SetCookie,  0, 0,        0},
  1165         -      { OP_Close,      0, 0,        0},
         1046  +      { OP_Next,       0, ADDR(3),  0}, /* 7 */
  1166   1047       };
  1167   1048       Index *pIdx;
         1049  +    Trigger *pTrigger;
  1168   1050       sqliteBeginWriteOperation(pParse, 0);
         1051  +    sqliteOpenMasterTable(v, pTable->isTemp);
  1169   1052       /* Drop all triggers associated with the table being dropped */
  1170         -    while( pTable->pTrigger ){
         1053  +    pTrigger = pTable->pTrigger;
         1054  +    while( pTrigger ){
  1171   1055         Token tt;
  1172   1056         tt.z = pTable->pTrigger->name;
  1173   1057         tt.n = strlen(pTable->pTrigger->name);
  1174   1058         sqliteDropTrigger(pParse, &tt, 1);
         1059  +      if( pParse->explain ){
         1060  +        pTrigger = pTrigger->pNext;
         1061  +      }else{
         1062  +        pTrigger = pTable->pTrigger;
         1063  +      }
  1175   1064       }
         1065  +    base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
         1066  +    sqliteVdbeChangeP3(v, base+1, pTable->zName, 0);
  1176   1067       if( !pTable->isTemp ){
  1177         -      base = sqliteVdbeAddOpList(v, ArraySize(dropTable), dropTable);
  1178         -      sqliteVdbeChangeP3(v, base+2, pTable->zName, 0);
  1179         -      sqliteChangeCookie(db);
  1180         -      sqliteVdbeChangeP1(v, base+9, db->next_cookie);
         1068  +      sqliteChangeCookie(db, v);
  1181   1069       }
         1070  +    sqliteVdbeAddOp(v, OP_Close, 0, 0);
  1182   1071       if( !isView ){
  1183   1072         sqliteVdbeAddOp(v, OP_Destroy, pTable->tnum, pTable->isTemp);
  1184   1073         for(pIdx=pTable->pIndex; pIdx; pIdx=pIdx->pNext){
  1185   1074           sqliteVdbeAddOp(v, OP_Destroy, pIdx->tnum, pTable->isTemp);
  1186   1075         }
  1187   1076       }
  1188   1077       sqliteEndWriteOperation(pParse);
  1189   1078     }
  1190   1079   
  1191         -  /* Move the table (and all its indices) to the pending DROP queue.
  1192         -  ** Or, if the table was never committed, just delete it.  If the table
  1193         -  ** has been committed and is placed on the pending DROP queue, then the
  1194         -  ** delete will occur when sqliteCommitInternalChanges() executes.
         1080  +  /* Delete the in-memory description of the table.
  1195   1081     **
  1196   1082     ** Exception: if the SQL statement began with the EXPLAIN keyword,
  1197   1083     ** then no changes should be made.
  1198   1084     */
  1199   1085     if( !pParse->explain ){
  1200         -    sqlitePendingDropTable(db, pTable);
         1086  +    sqliteUnlinkAndDeleteTable(db, pTable);
  1201   1087       db->flags |= SQLITE_InternChanges;
  1202   1088     }
  1203   1089     sqliteViewResetAll(db);
  1204   1090   }
  1205   1091   
  1206   1092   /*
  1207   1093   ** Create a new index for an SQL table.  pIndex is the name of the index 
................................................................................
  1260   1146     /* If this index is created while re-reading the schema from sqlite_master
  1261   1147     ** but the table associated with this index is a temporary table, it can
  1262   1148     ** only mean that the table that this index is really associated with is
  1263   1149     ** one whose name is hidden behind a temporary table with the same name.
  1264   1150     ** Since its table has been suppressed, we need to also suppress the
  1265   1151     ** index.
  1266   1152     */
  1267         -  if( pParse->initFlag && pTab->isTemp ){
         1153  +  if( pParse->initFlag && !pParse->isTemp && pTab->isTemp ){
  1268   1154       goto exit_create_index;
  1269   1155     }
  1270   1156   
  1271   1157     /*
  1272   1158     ** Find the name of the index.  Make sure there is not already another
  1273   1159     ** index or table with the same name.  
  1274   1160     **
................................................................................
  1425   1311       int addr;
  1426   1312       int isTemp = pTab->isTemp;
  1427   1313   
  1428   1314       v = sqliteGetVdbe(pParse);
  1429   1315       if( v==0 ) goto exit_create_index;
  1430   1316       if( pTable!=0 ){
  1431   1317         sqliteBeginWriteOperation(pParse, 0);
  1432         -      if( !isTemp ){
  1433         -        sqliteVdbeAddOp(v, OP_OpenWrite, 0, 2);
  1434         -        sqliteVdbeChangeP3(v, -1, MASTER_NAME, P3_STATIC);
  1435         -      }
         1318  +      sqliteOpenMasterTable(v, isTemp);
  1436   1319       }
  1437         -    if( !isTemp ){
  1438         -      sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
  1439         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
  1440         -      sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
  1441         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
  1442         -      sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
  1443         -      sqliteVdbeAddOp(v, OP_String, 0, 0);
  1444         -      sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
  1445         -    }
         1320  +    sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
         1321  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
         1322  +    sqliteVdbeChangeP3(v, -1, "index", P3_STATIC);
         1323  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
         1324  +    sqliteVdbeChangeP3(v, -1, pIndex->zName, P3_STATIC);
         1325  +    sqliteVdbeAddOp(v, OP_String, 0, 0);
         1326  +    sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
  1446   1327       addr = sqliteVdbeAddOp(v, OP_CreateIndex, 0, isTemp);
  1447   1328       sqliteVdbeChangeP3(v, addr, (char*)&pIndex->tnum, P3_POINTER);
  1448   1329       pIndex->tnum = 0;
  1449   1330       if( pTable ){
         1331  +      sqliteVdbeAddOp(v, OP_Dup, 0, 0);
  1450   1332         if( isTemp ){
  1451   1333           sqliteVdbeAddOp(v, OP_OpenWrAux, 1, 0);
  1452   1334         }else{
  1453         -        sqliteVdbeAddOp(v, OP_Dup, 0, 0);
  1454   1335           sqliteVdbeAddOp(v, OP_OpenWrite, 1, 0);
  1455   1336         }
  1456   1337       }
  1457         -    if( !isTemp ){
  1458         -      addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
  1459         -      if( pStart && pEnd ){
  1460         -        n = Addr(pEnd->z) - Addr(pStart->z) + 1;
  1461         -        sqliteVdbeChangeP3(v, addr, pStart->z, n);
  1462         -      }
  1463         -      sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
  1464         -      sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
         1338  +    addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
         1339  +    if( pStart && pEnd ){
         1340  +      n = Addr(pEnd->z) - Addr(pStart->z) + 1;
         1341  +      sqliteVdbeChangeP3(v, addr, pStart->z, n);
  1465   1342       }
         1343  +    sqliteVdbeAddOp(v, OP_MakeRecord, 5, 0);
         1344  +    sqliteVdbeAddOp(v, OP_PutIntKey, 0, 0);
  1466   1345       if( pTable ){
  1467   1346         sqliteVdbeAddOp(v, isTemp ? OP_OpenAux : OP_Open, 2, pTab->tnum);
  1468   1347         sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
  1469   1348         lbl2 = sqliteVdbeMakeLabel(v);
  1470   1349         sqliteVdbeAddOp(v, OP_Rewind, 2, lbl2);
  1471   1350         lbl1 = sqliteVdbeAddOp(v, OP_Recno, 2, 0);
  1472   1351         for(i=0; i<pIndex->nColumn; i++){
................................................................................
  1477   1356         sqliteVdbeAddOp(v, OP_Next, 2, lbl1);
  1478   1357         sqliteVdbeResolveLabel(v, lbl2);
  1479   1358         sqliteVdbeAddOp(v, OP_Close, 2, 0);
  1480   1359         sqliteVdbeAddOp(v, OP_Close, 1, 0);
  1481   1360       }
  1482   1361       if( pTable!=0 ){
  1483   1362         if( !isTemp ){
  1484         -        sqliteChangeCookie(db);
  1485         -        sqliteVdbeAddOp(v, OP_Integer, db->next_cookie, 0);
  1486         -        sqliteVdbeAddOp(v, OP_SetCookie, 0, 0);
  1487         -        sqliteVdbeAddOp(v, OP_Close, 0, 0);
         1363  +        sqliteChangeCookie(db, v);
  1488   1364         }
         1365  +      sqliteVdbeAddOp(v, OP_Close, 0, 0);
  1489   1366         sqliteEndWriteOperation(pParse);
  1490   1367       }
  1491   1368     }
  1492   1369   
  1493   1370     /* Clean up before exiting */
  1494   1371   exit_create_index:
  1495   1372     sqliteIdListDelete(pList);
................................................................................
  1519   1396       return;
  1520   1397     }
  1521   1398   
  1522   1399     /* Generate code to remove the index and from the master table */
  1523   1400     v = sqliteGetVdbe(pParse);
  1524   1401     if( v ){
  1525   1402       static VdbeOp dropIndex[] = {
  1526         -      { OP_OpenWrite,  0, 2,       MASTER_NAME},
  1527         -      { OP_Rewind,     0, ADDR(10),0}, 
  1528         -      { OP_String,     0, 0,       0}, /* 2 */
         1403  +      { OP_Rewind,     0, ADDR(9), 0}, 
         1404  +      { OP_String,     0, 0,       0}, /* 1 */
  1529   1405         { OP_MemStore,   1, 1,       0},
  1530         -      { OP_MemLoad,    1, 0,       0}, /* 4 */
         1406  +      { OP_MemLoad,    1, 0,       0}, /* 3 */
  1531   1407         { OP_Column,     0, 1,       0},
  1532         -      { OP_Eq,         0, ADDR(9), 0},
  1533         -      { OP_Next,       0, ADDR(4), 0},
  1534         -      { OP_Goto,       0, ADDR(10),0},
  1535         -      { OP_Delete,     0, 0,       0}, /* 9 */
  1536         -      { OP_Integer,    0, 0,       0}, /* 10 */
  1537         -      { OP_SetCookie,  0, 0,       0},
  1538         -      { OP_Close,      0, 0,       0},
         1408  +      { OP_Eq,         0, ADDR(8), 0},
         1409  +      { OP_Next,       0, ADDR(3), 0},
         1410  +      { OP_Goto,       0, ADDR(9), 0},
         1411  +      { OP_Delete,     0, 0,       0}, /* 8 */
  1539   1412       };
  1540   1413       int base;
  1541   1414       Table *pTab = pIndex->pTable;
  1542   1415   
  1543   1416       sqliteBeginWriteOperation(pParse, 0);
         1417  +    sqliteOpenMasterTable(v, pTab->isTemp);
         1418  +    base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
         1419  +    sqliteVdbeChangeP3(v, base+1, pIndex->zName, 0);
  1544   1420       if( !pTab->isTemp ){
  1545         -      base = sqliteVdbeAddOpList(v, ArraySize(dropIndex), dropIndex);
  1546         -      sqliteVdbeChangeP3(v, base+2, pIndex->zName, P3_STATIC);
  1547         -      sqliteChangeCookie(db);
  1548         -      sqliteVdbeChangeP1(v, base+10, db->next_cookie);
         1421  +      sqliteChangeCookie(db, v);
  1549   1422       }
         1423  +    sqliteVdbeAddOp(v, OP_Close, 0, 0);
  1550   1424       sqliteVdbeAddOp(v, OP_Destroy, pIndex->tnum, pTab->isTemp);
  1551   1425       sqliteEndWriteOperation(pParse);
  1552   1426     }
  1553   1427   
  1554         -  /* Move the index onto the pending DROP queue.  Or, if the index was
  1555         -  ** never committed, just delete it.  Indices on the pending DROP queue
  1556         -  ** get deleted by sqliteCommitInternalChanges() when the user executes
  1557         -  ** a COMMIT.  Or if a rollback occurs, the elements of the DROP queue
  1558         -  ** are moved back into the main hash table.
         1428  +  /* Delete the in-memory description of this index.
  1559   1429     */
  1560   1430     if( !pParse->explain ){
  1561         -    sqlitePendingDropIndex(db, pIndex);
         1431  +    sqliteUnlinkAndDeleteIndex(db, pIndex);
  1562   1432       db->flags |= SQLITE_InternChanges;
  1563   1433     }
  1564   1434   }
  1565   1435   
  1566   1436   /*
  1567   1437   ** Append a new element to the given IdList.  Create a new IdList if
  1568   1438   ** need be.

Changes to src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.83 2002/06/22 02:33:38 drh Exp $
           17  +** $Id: main.c,v 1.84 2002/06/25 01:09:11 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** This is the callback routine for the code that initializes the
................................................................................
    26     26   **
    27     27   ** Each callback contains the following information:
    28     28   **
    29     29   **     argv[0] = "file-format" or "schema-cookie" or "table" or "index"
    30     30   **     argv[1] = table or index name or meta statement type.
    31     31   **     argv[2] = root page number for table or index.  NULL for meta.
    32     32   **     argv[3] = SQL create statement for the table or index
           33  +**     argv[4] = "1" for temporary files, "0" for main database
    33     34   **
    34     35   */
    35     36   int sqliteInitCallback(void *pDb, int argc, char **argv, char **azColName){
    36     37     sqlite *db = (sqlite*)pDb;
    37     38     Parse sParse;
    38     39     int nErr = 0;
    39     40   
    40     41     /* TODO: Do some validity checks on all fields.  In particular,
    41     42     ** make sure fields do not contain NULLs. Otherwise we might core
    42     43     ** when attempting to initialize from a corrupt database file. */
    43     44   
    44         -  assert( argc==4 );
           45  +  assert( argc==5 );
    45     46     switch( argv[0][0] ){
    46         -    case 'c': {  /* Recommended pager cache size */
    47         -      int size = atoi(argv[3]);
    48         -      if( size==0 ){ size = MAX_PAGES; }
    49         -      db->cache_size = size;
    50         -      sqliteBtreeSetCacheSize(db->pBe, size);
    51         -      break;
    52         -    }
    53         -    case 'f': {  /* File format */
    54         -      /*
    55         -      ** file_format==1  Version 2.1.0.
    56         -      ** file_format==2  Version 2.2.0.  Integer primary key.
    57         -      ** file_format==3  Version 2.6.0.  Separate text and numeric datatypes.
    58         -      */
    59         -      db->file_format = atoi(argv[3]);
    60         -      break;
    61         -    }
    62         -    case 's': { /* Schema cookie */
    63         -      db->schema_cookie = atoi(argv[3]);
    64         -      db->next_cookie = db->schema_cookie;
    65         -      break;
    66         -    }
    67     47       case 'v':
    68     48       case 'i':
    69     49       case 't': {  /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
    70     50         if( argv[3] && argv[3][0] ){
    71     51           /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
    72     52           ** But because sParse.initFlag is set to 1, no VDBE code is generated
    73     53           ** or executed.  All the parser does is build the internal data
    74     54           ** structures that describe the table, index, or view.
    75     55           */
    76     56           memset(&sParse, 0, sizeof(sParse));
    77     57           sParse.db = db;
    78     58           sParse.initFlag = 1;
           59  +        sParse.isTemp = argv[4][0] - '0';
    79     60           sParse.newTnum = atoi(argv[2]);
    80     61           sqliteRunParser(&sParse, argv[3], 0);
    81     62         }else{
    82     63           /* If the SQL column is blank it means this is an index that
    83     64           ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
    84     65           ** constraint for a CREATE TABLE.  The index should have already
    85     66           ** been created when we processed the CREATE TABLE.  All we have
................................................................................
   116     97   ** After the database is initialized, the SQLITE_Initialized
   117     98   ** bit is set in the flags field of the sqlite structure.  An
   118     99   ** attempt is made to initialize the database as soon as it
   119    100   ** is opened.  If that fails (perhaps because another process
   120    101   ** has the sqlite_master table locked) than another attempt
   121    102   ** is made the first time the database is accessed.
   122    103   */
   123         -static int sqliteInit(sqlite *db, char **pzErrMsg){
   124         -  Vdbe *vdbe;
          104  +int sqliteInit(sqlite *db, char **pzErrMsg){
   125    105     int rc;
          106  +  BtCursor *curMain;
          107  +  int size;
          108  +  Table *pTab;
          109  +  char *azArg[6];
          110  +  int meta[SQLITE_N_BTREE_META];
          111  +  Parse sParse;
   126    112   
   127    113     /*
   128    114     ** The master database table has a structure like this
   129    115     */
   130    116     static char master_schema[] = 
   131         -     "CREATE TABLE " MASTER_NAME " (\n"
          117  +     "CREATE TABLE sqlite_master(\n"
          118  +     "  type text,\n"
          119  +     "  name text,\n"
          120  +     "  tbl_name text,\n"
          121  +     "  rootpage integer,\n"
          122  +     "  sql text\n"
          123  +     ")"
          124  +  ;
          125  +  static char temp_master_schema[] = 
          126  +     "CREATE TEMP TABLE sqlite_temp_master(\n"
   132    127        "  type text,\n"
   133    128        "  name text,\n"
   134    129        "  tbl_name text,\n"
   135    130        "  rootpage integer,\n"
   136    131        "  sql text\n"
   137    132        ")"
   138    133     ;
   139    134   
   140         -  /* The following VDBE program is used to initialize the internal
   141         -  ** structure holding the tables and indexes of the database.
   142         -  ** The database contains a special table named "sqlite_master"
   143         -  ** defined as follows:
          135  +  /* The following SQL will read the schema from the master tables.
          136  +  ** The first version works with SQLite file formats 2 or greater.
          137  +  ** The second version is for format 1 files.
   144    138     **
   145         -  **    CREATE TABLE sqlite_master (
   146         -  **        type       text,    --  Either "table" or "index" or "meta"
   147         -  **        name       text,    --  Name of table or index
   148         -  **        tbl_name   text,    --  Associated table 
   149         -  **        rootpage   integer, --  The integer page number of root page
   150         -  **        sql        text     --  The CREATE statement for this object
   151         -  **    );
   152         -  **
   153         -  ** The sqlite_master table contains a single entry for each table
   154         -  ** and each index.  The "type" column tells whether the entry is
   155         -  ** a table or index.  The "name" column is the name of the object.
   156         -  ** The "tbl_name" is the name of the associated table.  For tables,
   157         -  ** the tbl_name column is always the same as name.  For indices, the
   158         -  ** tbl_name column contains the name of the table that the index
   159         -  ** indexes.  The "rootpage" column holds the number of the root page
   160         -  ** for the b-tree for the table or index.  Finally, the "sql" column
   161         -  ** contains the complete text of the CREATE TABLE or CREATE INDEX
   162         -  ** statement that originally created the table or index.  If an index
   163         -  ** was created to fulfill a PRIMARY KEY or UNIQUE constraint on a table,
   164         -  ** then the "sql" column is NULL.
   165         -  **
   166         -  ** In format 1, entries in the sqlite_master table are in a random
   167         -  ** order.  Two passes must be made through the table to initialize
   168         -  ** internal data structures.  The first pass reads table definitions
   169         -  ** and the second pass read index definitions.  Having two passes
   170         -  ** insures that indices appear after their tables.
   171         -  **
   172         -  ** In format 2, entries appear in chronological order.  Only a single
   173         -  ** pass needs to be made through the table since everything will be
   174         -  ** in the write order.  VIEWs may only occur in format 2.
   175         -  **
   176         -  ** The following program invokes its callback on the SQL for each
   177         -  ** table then goes back and invokes the callback on the
   178         -  ** SQL for each index.  The callback will invoke the
   179         -  ** parser to build the internal representation of the
   180         -  ** database scheme.
          139  +  ** Beginning with file format 2, the rowid for new table entries
          140  +  ** (including entries in sqlite_master) is an increasing integer.
          141  +  ** So for file format 2 and later, we can play back sqlite_master
          142  +  ** and all the CREATE statements will appear in the right order.
          143  +  ** But with file format 1, table entries were random and so we
          144  +  ** have to make sure the CREATE TABLEs occur before their corresponding
          145  +  ** CREATE INDEXs.  (We don't have to deal with CREATE VIEW or
          146  +  ** CREATE TRIGGER in file format 1 because those constructs did
          147  +  ** not exist then.) 
          148  +  */
          149  +  static char init_script[] = 
          150  +     "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
          151  +     "UNION ALL "
          152  +     "SELECT type, name, rootpage, sql, 0 FROM sqlite_master";
          153  +  static char older_init_script[] = 
          154  +     "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master "
          155  +     "UNION ALL "
          156  +     "SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
          157  +     "WHERE type='table' "
          158  +     "UNION ALL "
          159  +     "SELECT type, name, rootpage, sql, 0 FROM sqlite_master "
          160  +     "WHERE type='index'";
          161  +
          162  +
          163  +  /* Construct the schema tables: sqlite_master and sqlite_temp_master
          164  +  */
          165  +  azArg[0] = "table";
          166  +  azArg[1] = MASTER_NAME;
          167  +  azArg[2] = "2";
          168  +  azArg[3] = master_schema;
          169  +  azArg[4] = "0";
          170  +  azArg[5] = 0;
          171  +  sqliteInitCallback(db, 5, azArg, 0);
          172  +  pTab = sqliteFindTable(db, MASTER_NAME);
          173  +  if( pTab ){
          174  +    pTab->readOnly = 1;
          175  +  }
          176  +  azArg[1] = TEMP_MASTER_NAME;
          177  +  azArg[3] = temp_master_schema;
          178  +  azArg[4] = "1";
          179  +  sqliteInitCallback(db, 5, azArg, 0);
          180  +  pTab = sqliteFindTable(db, TEMP_MASTER_NAME);
          181  +  if( pTab ){
          182  +    pTab->readOnly = 1;
          183  +  }
          184  +
          185  +  /* Create a cursor to hold the database open
          186  +  */
          187  +  if( db->pBe==0 ) return SQLITE_OK;
          188  +  rc = sqliteBtreeCursor(db->pBe, 2, 0, &curMain);
          189  +  if( rc ) return rc;
          190  +
          191  +  /* Get the database meta information
          192  +  */
          193  +  rc = sqliteBtreeGetMeta(db->pBe, meta);
          194  +  if( rc ){
          195  +    sqliteBtreeCloseCursor(curMain);
          196  +    return rc;
          197  +  }
          198  +  db->schema_cookie = meta[1];
          199  +  db->next_cookie = db->schema_cookie;
          200  +  db->file_format = meta[2];
          201  +  size = meta[3];
          202  +  if( size==0 ){ size = MAX_PAGES; }
          203  +  db->cache_size = size;
          204  +  sqliteBtreeSetCacheSize(db->pBe, size);
          205  +
          206  +  /*
          207  +  **     file_format==1    Version 2.1.0.
          208  +  **     file_format==2    Version 2.2.0. Add support for INTEGER PRIMARY KEY.
          209  +  **     file_format==3    Version 2.6.0. Add support for separate numeric and
          210  +  **                       text datatypes.
   181    211     */
   182         -  static VdbeOp initProg[] = {
   183         -    /* Send the file format to the callback routine
   184         -    */
   185         -    { OP_Open,       0, 2,  0},
   186         -    { OP_String,     0, 0,  "file-format"},
   187         -    { OP_String,     0, 0,  0},
   188         -    { OP_String,     0, 0,  0},
   189         -    { OP_ReadCookie, 0, 1,  0},
   190         -    { OP_Callback,   4, 0,  0},
   191         -
   192         -    /* Send the recommended pager cache size to the callback routine
   193         -    */
   194         -    { OP_String,     0, 0,  "cache-size"},
   195         -    { OP_String,     0, 0,  0},
   196         -    { OP_String,     0, 0,  0},
   197         -    { OP_ReadCookie, 0, 2,  0},
   198         -    { OP_Callback,   4, 0,  0},
   199         -
   200         -    /* Send the initial schema cookie to the callback
   201         -    */
   202         -    { OP_String,     0, 0,  "schema_cookie"},
   203         -    { OP_String,     0, 0,  0},
   204         -    { OP_String,     0, 0,  0},
   205         -    { OP_ReadCookie, 0, 0,  0},
   206         -    { OP_Callback,   4, 0,  0},
   207         -
   208         -    /* Check the file format.  If the format number is 2 or more,
   209         -    ** then do a single pass through the SQLITE_MASTER table.  For
   210         -    ** a format number of less than 2, jump forward to a different
   211         -    ** algorithm that makes two passes through the SQLITE_MASTER table,
   212         -    ** once for tables and a second time for indices.
   213         -    */
   214         -    { OP_ReadCookie, 0, 1,  0},
   215         -    { OP_Integer,    2, 0,  0},
   216         -    { OP_Lt,         0, 28, 0},
   217         -
   218         -    /* This is the code for doing a single scan through the SQLITE_MASTER
   219         -    ** table.  This code runs for format 2 and greater.
   220         -    */
   221         -    { OP_Rewind,     0, 26, 0},
   222         -    { OP_Column,     0, 0,  0},           /* 20 */
   223         -    { OP_Column,     0, 1,  0},
   224         -    { OP_Column,     0, 3,  0},
   225         -    { OP_Column,     0, 4,  0},
   226         -    { OP_Callback,   4, 0,  0},
   227         -    { OP_Next,       0, 20, 0},
   228         -    { OP_Close,      0, 0,  0},           /* 26 */
   229         -    { OP_Halt,       0, 0,  0},
   230         -
   231         -    /* This is the code for doing two passes through SQLITE_MASTER.  This
   232         -    ** code runs for file format 1.
   233         -    */
   234         -    { OP_Rewind,     0, 48, 0},           /* 28 */
   235         -    { OP_Column,     0, 0,  0},           /* 29 */
   236         -    { OP_String,     0, 0,  "table"},
   237         -    { OP_Ne,         0, 37, 0},
   238         -    { OP_Column,     0, 0,  0},
   239         -    { OP_Column,     0, 1,  0},
   240         -    { OP_Column,     0, 3,  0},
   241         -    { OP_Column,     0, 4,  0},
   242         -    { OP_Callback,   4, 0,  0},
   243         -    { OP_Next,       0, 29, 0},           /* 37 */
   244         -    { OP_Rewind,     0, 48, 0},           /* 38 */
   245         -    { OP_Column,     0, 0,  0},           /* 39 */
   246         -    { OP_String,     0, 0,  "index"},
   247         -    { OP_Ne,         0, 47, 0},
   248         -    { OP_Column,     0, 0,  0},
   249         -    { OP_Column,     0, 1,  0},
   250         -    { OP_Column,     0, 3,  0},
   251         -    { OP_Column,     0, 4,  0},
   252         -    { OP_Callback,   4, 0,  0},
   253         -    { OP_Next,       0, 39, 0},           /* 47 */
   254         -    { OP_Close,      0, 0,  0},           /* 48 */
   255         -    { OP_Halt,       0, 0,  0},
   256         -  };
   257         -
   258         -  /* Create a virtual machine to run the initialization program.  Run
   259         -  ** the program.  Then delete the virtual machine.
   260         -  */
   261         -  vdbe = sqliteVdbeCreate(db);
   262         -  if( vdbe==0 ){
   263         -    sqliteSetString(pzErrMsg, "out of memory", 0);
   264         -    return SQLITE_NOMEM;
   265         -  }
   266         -  sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
   267         -  rc = sqliteVdbeExec(vdbe, sqliteInitCallback, db, pzErrMsg, 
   268         -                      db->pBusyArg, db->xBusyCallback);
   269         -  sqliteVdbeDelete(vdbe);
   270         -  if( rc==SQLITE_OK && db->nTable==0 ){
          212  +  if( db->file_format==0 ){
   271    213       db->file_format = 2;
   272         -  }
   273         -  if( rc==SQLITE_OK && db->file_format>2 ){
          214  +  }else if( db->file_format>2 ){
          215  +    sqliteBtreeCloseCursor(curMain);
   274    216       sqliteSetString(pzErrMsg, "unsupported file format", 0);
   275    217       rc = SQLITE_ERROR;
   276    218     }
   277    219   
   278         -  /* The schema for the SQLITE_MASTER table is not stored in the
   279         -  ** database itself.  We have to invoke the callback one extra
   280         -  ** time to get it to process the SQLITE_MASTER table defintion.
          220  +  /* Read the schema information out of the schema tables
   281    221     */
   282         -  if( rc==SQLITE_OK ){
   283         -    Table *pTab;
   284         -    char *azArg[6];
   285         -    azArg[0] = "table";
   286         -    azArg[1] = MASTER_NAME;
   287         -    azArg[2] = "2";
   288         -    azArg[3] = master_schema;
   289         -    azArg[4] = 0;
   290         -    sqliteInitCallback(db, 4, azArg, 0);
   291         -    pTab = sqliteFindTable(db, MASTER_NAME);
   292         -    if( pTab ){
   293         -      pTab->readOnly = 1;
   294         -    }
          222  +  memset(&sParse, 0, sizeof(sParse));
          223  +  sParse.db = db;
          224  +  sParse.pBe = db->pBe;
          225  +  sParse.xCallback = sqliteInitCallback;
          226  +  sParse.pArg = (void*)db;
          227  +  sParse.initFlag = 1;
          228  +  sqliteRunParser(&sParse,
          229  +      db->file_format>=2 ? init_script : older_init_script,
          230  +      pzErrMsg);
          231  +  if( sqlite_malloc_failed ){
          232  +    sqliteSetString(pzErrMsg, "out of memory", 0);
          233  +    sParse.rc = SQLITE_NOMEM;
          234  +    sqliteBtreeRollback(db->pBe);
          235  +    sqliteResetInternalSchema(db);
          236  +  }
          237  +  if( sParse.rc==SQLITE_OK ){
   295    238       db->flags |= SQLITE_Initialized;
   296    239       sqliteCommitInternalChanges(db);
          240  +  }else{
          241  +    db->flags &= ~SQLITE_Initialized;
          242  +    sqliteResetInternalSchema(db);
   297    243     }
   298         -  return rc;
          244  +  sqliteBtreeCloseCursor(curMain);
          245  +  return sParse.rc;
   299    246   }
   300    247   
   301    248   /*
   302    249   ** The version of the library
   303    250   */
   304    251   const char sqlite_version[] = SQLITE_VERSION;
   305    252   
................................................................................
   329    276     /* Allocate the sqlite data structure */
   330    277     db = sqliteMalloc( sizeof(sqlite) );
   331    278     if( pzErrMsg ) *pzErrMsg = 0;
   332    279     if( db==0 ) goto no_mem_on_open;
   333    280     sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
   334    281     sqliteHashInit(&db->idxHash, SQLITE_HASH_STRING, 0);
   335    282     sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
   336         -  sqliteHashInit(&db->trigDrop, SQLITE_HASH_STRING, 0);
   337         -  sqliteHashInit(&db->tblDrop, SQLITE_HASH_POINTER, 0);
   338         -  sqliteHashInit(&db->idxDrop, SQLITE_HASH_POINTER, 0);
   339    283     sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
   340    284     sqliteRegisterBuiltinFunctions(db);
   341    285     db->onError = OE_Default;
   342    286     db->priorNewRowid = 0;
   343    287     db->magic = SQLITE_MAGIC_BUSY;
   344    288     
   345    289     /* Open the backend database driver */
................................................................................
   373    317   
   374    318   no_mem_on_open:
   375    319     sqliteSetString(pzErrMsg, "out of memory", 0);
   376    320     sqliteStrRealloc(pzErrMsg);
   377    321     return 0;
   378    322   }
   379    323   
   380         -/*
   381         -** Erase all schema information from the schema hash table.  Except
   382         -** tables that are created using CREATE TEMPORARY TABLE are preserved
   383         -** if the preserveTemps flag is true.
   384         -**
   385         -** The database schema is normally read in once when the database
   386         -** is first opened and stored in a hash table in the sqlite structure.
   387         -** This routine erases the stored schema.  This erasure occurs because
   388         -** either the database is being closed or because some other process
   389         -** changed the schema and this process needs to reread it.
   390         -*/
   391         -static void clearHashTable(sqlite *db, int preserveTemps){
   392         -  HashElem *pElem;
   393         -  Hash temp1;
   394         -  Hash temp2;
   395         -
   396         -  /* Make sure there are no uncommited DROPs */
   397         -  assert( sqliteHashFirst(&db->tblDrop)==0 || sqlite_malloc_failed );
   398         -  assert( sqliteHashFirst(&db->idxDrop)==0 || sqlite_malloc_failed );
   399         -  assert( sqliteHashFirst(&db->trigDrop)==0 || sqlite_malloc_failed );
   400         -  temp1 = db->tblHash;
   401         -  temp2 = db->trigHash;
   402         -  sqliteHashInit(&db->trigHash, SQLITE_HASH_STRING, 0);
   403         -  sqliteHashClear(&db->idxHash);
   404         -
   405         -  for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
   406         -    Trigger * pTrigger = sqliteHashData(pElem);
   407         -    Table *pTab = sqliteFindTable(db, pTrigger->table);
   408         -    assert(pTab);
   409         -    if( pTab->isTemp && preserveTemps ){ 
   410         -      sqliteHashInsert(&db->trigHash, pTrigger->name, strlen(pTrigger->name), 
   411         -          pTrigger);
   412         -    }else{
   413         -      sqliteDeleteTrigger(pTrigger);
   414         -    }
   415         -  }
   416         -  sqliteHashClear(&temp2);
   417         -
   418         -  sqliteHashInit(&db->tblHash, SQLITE_HASH_STRING, 0);
   419         -
   420         -  for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
   421         -    Table *pTab = sqliteHashData(pElem);
   422         -    if( preserveTemps && pTab->isTemp ){
   423         -      Index *pIdx;
   424         -      int nName = strlen(pTab->zName);
   425         -      Table *pOld = sqliteHashInsert(&db->tblHash, pTab->zName, nName+1, pTab);
   426         -      if( pOld!=0 ){
   427         -        assert( pOld==pTab );   /* Malloc failed on the HashInsert */
   428         -        sqliteDeleteTable(db, pOld);
   429         -        continue;
   430         -      }
   431         -      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   432         -        int n = strlen(pIdx->zName)+1;
   433         -        Index *pOldIdx;
   434         -        pOldIdx = sqliteHashInsert(&db->idxHash, pIdx->zName, n, pIdx);
   435         -        if( pOld ){
   436         -          assert( pOldIdx==pIdx );
   437         -          sqliteUnlinkAndDeleteIndex(db, pOldIdx);
   438         -        }
   439         -      }
   440         -    }else{
   441         -      sqliteDeleteTable(db, pTab);
   442         -    }
   443         -  }
   444         -  sqliteHashClear(&temp1);
   445         -  db->flags &= ~SQLITE_Initialized;
   446         -}
   447         -
   448    324   /*
   449    325   ** Return the ROWID of the most recent insert
   450    326   */
   451    327   int sqlite_last_insert_rowid(sqlite *db){
   452    328     return db->lastRowid;
   453    329   }
   454    330   
................................................................................
   463    339   ** Close an existing SQLite database
   464    340   */
   465    341   void sqlite_close(sqlite *db){
   466    342     HashElem *i;
   467    343     if( sqliteSafetyCheck(db) || sqliteSafetyOn(db) ){ return; }
   468    344     db->magic = SQLITE_MAGIC_CLOSED;
   469    345     sqliteBtreeClose(db->pBe);
   470         -  sqliteRollbackInternalChanges(db);
   471         -  clearHashTable(db, 0);
          346  +  sqliteResetInternalSchema(db);
   472    347     if( db->pBeTemp ){
   473    348       sqliteBtreeClose(db->pBeTemp);
   474    349     }
   475    350     for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
   476    351       FuncDef *pFunc, *pNext;
   477    352       for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
   478    353         pNext = pFunc->pNext;
................................................................................
   634    509     sqliteRunParser(&sParse, zSql, pzErrMsg);
   635    510     if( sqlite_malloc_failed ){
   636    511       sqliteSetString(pzErrMsg, "out of memory", 0);
   637    512       sParse.rc = SQLITE_NOMEM;
   638    513       sqliteBtreeRollback(db->pBe);
   639    514       if( db->pBeTemp ) sqliteBtreeRollback(db->pBeTemp);
   640    515       db->flags &= ~SQLITE_InTrans;
   641         -    clearHashTable(db, 0);
          516  +    sqliteResetInternalSchema(db);
   642    517     }
   643    518     sqliteStrRealloc(pzErrMsg);
   644    519     if( sParse.rc==SQLITE_SCHEMA ){
   645         -    clearHashTable(db, 1);
          520  +    sqliteResetInternalSchema(db);
   646    521     }
   647    522     db->recursionDepth--;
   648    523     if( sqliteSafetyOff(db) ) goto exec_misuse;
   649    524     return sParse.rc;
   650    525   
   651    526   exec_misuse:
   652    527     if( pzErrMsg ){

Changes to src/parse.y.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains SQLite's grammar for SQL.  Process this file
    13     13   ** using the lemon parser generator to generate C code that runs
    14     14   ** the parser.  Lemon will also generate a header file containing
    15     15   ** numeric codes for all of the tokens.
    16     16   **
    17         -** @(#) $Id: parse.y,v 1.74 2002/06/17 17:07:20 drh Exp $
           17  +** @(#) $Id: parse.y,v 1.75 2002/06/25 01:09:12 drh Exp $
    18     18   */
    19     19   %token_prefix TK_
    20     20   %token_type {Token}
    21     21   %default_type {Token}
    22     22   %extra_argument {Parse *pParse}
    23     23   %syntax_error {
    24     24     sqliteSetString(&pParse->zErrMsg,"syntax error",0);
................................................................................
    61     61   input ::= cmdlist.
    62     62   
    63     63   // A list of commands is zero or more commands
    64     64   //
    65     65   cmdlist ::= ecmd.
    66     66   cmdlist ::= cmdlist ecmd.
    67     67   ecmd ::= explain cmd SEMI.  {sqliteExec(pParse);}
    68         -ecmd ::= cmd SEMI.          {sqliteExec(pParse);}
    69     68   ecmd ::= SEMI.
    70         -explain ::= EXPLAIN.    {pParse->explain = 1;}
           69  +explain ::= EXPLAIN.    { sqliteBeginParse(pParse, 1); }
           70  +explain ::= .           { sqliteBeginParse(pParse, 0); }
    71     71   
    72     72   ///////////////////// Begin and end transactions. ////////////////////////////
    73     73   //
    74     74   
    75     75   cmd ::= BEGIN trans_opt onconf(R).  {sqliteBeginTransaction(pParse,R);}
    76     76   trans_opt ::= .
    77     77   trans_opt ::= TRANSACTION.
................................................................................
    83     83   ///////////////////// The CREATE TABLE statement ////////////////////////////
    84     84   //
    85     85   cmd ::= create_table create_table_args.
    86     86   create_table ::= CREATE(X) temp(T) TABLE ids(Y). {
    87     87      sqliteStartTable(pParse,&X,&Y,T);
    88     88   }
    89     89   %type temp {int}
    90         -temp(A) ::= TEMP.  {A = 1;}
    91         -temp(A) ::= .      {A = 0;}
           90  +temp(A) ::= TEMP.  {A = pParse->isTemp || !pParse->initFlag;}
           91  +temp(A) ::= .      {A = pParse->isTemp;}
    92     92   create_table_args ::= LP columnlist conslist_opt RP(X). {
    93     93     sqliteEndTable(pParse,&X,0);
    94     94   }
    95     95   create_table_args ::= AS select(S). {
    96     96     sqliteEndTable(pParse,0,S);
    97     97     sqliteSelectDelete(S);
    98     98   }

Changes to src/shell.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code to implement the "sqlite" command line
    13     13   ** utility for accessing SQLite databases.
    14     14   **
    15         -** $Id: shell.c,v 1.57 2002/05/21 13:02:24 drh Exp $
           15  +** $Id: shell.c,v 1.58 2002/06/25 01:09:12 drh Exp $
    16     16   */
    17     17   #include <stdlib.h>
    18     18   #include <string.h>
    19     19   #include <stdio.h>
    20     20   #include "sqlite.h"
    21     21   #include <ctype.h>
    22     22   
................................................................................
   656    656       char *zErrMsg = 0;
   657    657       memcpy(&data, p, sizeof(data));
   658    658       data.showHeader = 0;
   659    659       data.mode = MODE_List;
   660    660       sqlite_exec_printf(db,
   661    661         "SELECT name FROM sqlite_master "
   662    662         "WHERE type='index' AND tbl_name LIKE '%q' "
   663         -      "ORDER BY name",
   664         -      callback, &data, &zErrMsg, azArg[1]
          663  +      "UNION ALL "
          664  +      "SELECT name FROM sqlite_temp_master "
          665  +      "WHERE type='index' AND tbl_name LIKE '%q' "
          666  +      "ORDER BY 1",
          667  +      callback, &data, &zErrMsg, azArg[1], azArg[1]
   665    668       );
   666    669       if( zErrMsg ){
   667    670         fprintf(stderr,"Error: %s\n", zErrMsg);
   668    671         free(zErrMsg);
   669    672       }
   670    673     }else
   671    674   
................................................................................
   792    795                         "  rootpage integer,\n"
   793    796                         "  sql text\n"
   794    797                         ")";
   795    798           new_argv[1] = 0;
   796    799           new_colv[0] = "sql";
   797    800           new_colv[1] = 0;
   798    801           callback(&data, 1, new_argv, new_colv);
          802  +      }else if( sqliteStrICmp(azArg[1],"sqlite_temp_master")==0 ){
          803  +        char *new_argv[2], *new_colv[2];
          804  +        new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
          805  +                      "  type text,\n"
          806  +                      "  name text,\n"
          807  +                      "  tbl_name text,\n"
          808  +                      "  rootpage integer,\n"
          809  +                      "  sql text\n"
          810  +                      ")";
          811  +        new_argv[1] = 0;
          812  +        new_colv[0] = "sql";
          813  +        new_colv[1] = 0;
          814  +        callback(&data, 1, new_argv, new_colv);
   799    815         }else{
   800    816           sqlite_exec_printf(db,
   801         -          "SELECT sql FROM sqlite_master "
          817  +          "SELECT sql FROM "
          818  +          "  (SELECT * FROM sqlite_master UNION ALL"
          819  +          "   SELECT * FROM sqlite_temp_master) "
   802    820             "WHERE tbl_name LIKE '%q' AND type!='meta' AND sql NOTNULL "
   803         -          "ORDER BY type DESC, name",
          821  +          "ORDER BY substr(type,2,1), name",
   804    822             callback, &data, &zErrMsg, azArg[1]);
   805    823         }
   806    824       }else{
   807    825         sqlite_exec(db,
   808         -         "SELECT sql FROM sqlite_master "
          826  +         "SELECT sql FROM "
          827  +         "  (SELECT * FROM sqlite_master UNION ALL"
          828  +         "   SELECT * FROM sqlite_temp_master) "
   809    829            "WHERE type!='meta' AND sql NOTNULL "
   810         -         "ORDER BY tbl_name, type DESC, name",
          830  +         "ORDER BY substr(type,2,1), name",
   811    831            callback, &data, &zErrMsg
   812    832         );
   813    833       }
   814    834       if( zErrMsg ){
   815    835         fprintf(stderr,"Error: %s\n", zErrMsg);
   816    836         free(zErrMsg);
   817    837       }
................................................................................
   842    862       char **azResult;
   843    863       int nRow, rc;
   844    864       char *zErrMsg;
   845    865       if( nArg==1 ){
   846    866         rc = sqlite_get_table(db,
   847    867           "SELECT name FROM sqlite_master "
   848    868           "WHERE type IN ('table','view') "
   849         -        "ORDER BY name",
          869  +        "UNION ALL "
          870  +        "SELECT name FROM sqlite_temp_master "
          871  +        "WHERE type IN ('table','view') "
          872  +        "ORDER BY 1",
   850    873           &azResult, &nRow, 0, &zErrMsg
   851    874         );
   852    875       }else{
   853    876         rc = sqlite_get_table_printf(db,
   854    877           "SELECT name FROM sqlite_master "
   855    878           "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
   856         -        "ORDER BY name",
   857         -        &azResult, &nRow, 0, &zErrMsg, azArg[1]
          879  +        "UNION ALL "
          880  +        "SELECT name FROM sqlite_temp_master "
          881  +        "WHERE type IN ('table','view') AND name LIKE '%%%q%%' "
          882  +        "ORDER BY 1",
          883  +        &azResult, &nRow, 0, &zErrMsg, azArg[1], azArg[1]
   858    884         );
   859    885       }
   860    886       if( zErrMsg ){
   861    887         fprintf(stderr,"Error: %s\n", zErrMsg);
   862    888         free(zErrMsg);
   863    889       }
   864    890       if( rc==SQLITE_OK ){

Changes to src/sqliteInt.h.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.130 2002/06/24 22:01:58 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.131 2002/06/25 01:09:12 drh Exp $
    15     15   */
    16     16   #include "sqlite.h"
    17     17   #include "hash.h"
    18     18   #include "vdbe.h"
    19     19   #include "parse.h"
    20     20   #include "btree.h"
    21     21   #include <stdio.h>
................................................................................
   141    141   #endif
   142    142   
   143    143   /*
   144    144   ** Name of the master database table.  The master database table
   145    145   ** is a special table that holds the names and attributes of all
   146    146   ** user tables and indices.
   147    147   */
   148         -#define MASTER_NAME   "sqlite_master"
          148  +#define MASTER_NAME       "sqlite_master"
          149  +#define TEMP_MASTER_NAME  "sqlite_temp_master"
   149    150   
   150    151   /*
   151    152   ** A convenience macro that returns the number of elements in
   152    153   ** an array.
   153    154   */
   154    155   #define ArraySize(X)    (sizeof(X)/sizeof(X[0]))
   155    156   
................................................................................
   198    199     int next_cookie;              /* Value of schema_cookie after commit */
   199    200     int cache_size;               /* Number of pages to use in the cache */
   200    201     int nTable;                   /* Number of tables in the database */
   201    202     void *pBusyArg;               /* 1st Argument to the busy callback */
   202    203     int (*xBusyCallback)(void *,const char*,int);  /* The busy callback */
   203    204     Hash tblHash;                 /* All tables indexed by name */
   204    205     Hash idxHash;                 /* All (named) indices indexed by name */
   205         -  Hash tblDrop;                 /* Uncommitted DROP TABLEs */
   206         -  Hash idxDrop;                 /* Uncommitted DROP INDEXs */
          206  +  Hash trigHash;                /* All triggers indexed by name */
   207    207     Hash aFunc;                   /* All functions that can be in SQL exprs */
   208    208     int lastRowid;                /* ROWID of most recent insert */
   209    209     int priorNewRowid;            /* Last randomly generated ROWID */
   210    210     int onError;                  /* Default conflict algorithm */
   211    211     int magic;                    /* Magic number for detect library misuse */
   212    212     int nChange;                  /* Number of rows changed */
   213    213     int recursionDepth;           /* Number of nested calls to sqlite_exec() */
   214         -
   215         -  Hash trigHash;                /* All triggers indexed by name */
   216         -  Hash trigDrop;                /* Uncommited dropped triggers */
   217    214   };
   218    215   
   219    216   /*
   220    217   ** Possible values for the sqlite.flags.
   221    218   */
   222    219   #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
   223    220   #define SQLITE_Initialized    0x00000002  /* True after initialization */
................................................................................
   321    318     int nCol;        /* Number of columns in this table */
   322    319     Column *aCol;    /* Information about each column */
   323    320     int iPKey;       /* If not less then 0, use aCol[iPKey] as the primary key */
   324    321     Index *pIndex;   /* List of SQL indexes on this table. */
   325    322     int tnum;        /* Root BTree node for this table (see note above) */
   326    323     Select *pSelect; /* NULL for tables.  Points to definition if a view. */
   327    324     u8 readOnly;     /* True if this table should not be written by the user */
   328         -  u8 isCommit;     /* True if creation of this table has been committed */
   329    325     u8 isTemp;       /* True if stored in db->pBeTemp instead of db->pBe */
   330    326     u8 isTransient;  /* True if automatically deleted when VDBE finishes */
   331    327     u8 hasPrimKey;   /* True if there exists a primary key */
   332    328     u8 keyConf;      /* What to do in case of uniqueness conflict on iPKey */
   333         -
   334    329     Trigger *pTrigger; /* List of SQL triggers on this table */
   335    330   };
   336    331   
   337    332   /*
   338    333   ** SQLite supports 5 different ways to resolve a contraint
   339    334   ** error.  ROLLBACK processing means that a constraint violation
   340    335   ** causes the operation in process to fail and for the current transaction
................................................................................
   382    377   struct Index {
   383    378     char *zName;     /* Name of this index */
   384    379     int nColumn;     /* Number of columns in the table used by this index */
   385    380     int *aiColumn;   /* Which columns are used by this index.  1st is 0 */
   386    381     Table *pTable;   /* The SQL table being indexed */
   387    382     int tnum;        /* Page containing root of this index in database file */
   388    383     u8 isUnique;     /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
   389         -  u8 isCommit;     /* True if creation of this index has been committed */
   390         -  u8 isDropped;    /* True if a DROP INDEX has executed on this index */
   391    384     u8 onError;      /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
   392    385     Index *pNext;    /* The next index associated with the same table */
   393    386   };
   394    387   
   395    388   /*
   396    389   ** Each token coming out of the lexer is an instance of
   397    390   ** this structure.
................................................................................
   644    637     void *pArg;          /* First argument to the callback function */
   645    638     char *zErrMsg;       /* An error message */
   646    639     Token sErrToken;     /* The token at which the error occurred */
   647    640     Token sFirstToken;   /* The first token parsed */
   648    641     Token sLastToken;    /* The last token parsed */
   649    642     Table *pNewTable;    /* A table being constructed by CREATE TABLE */
   650    643     Vdbe *pVdbe;         /* An engine for executing database bytecode */
   651         -  int colNamesSet;     /* TRUE after OP_ColumnCount has been issued to pVdbe */
   652         -  int explain;         /* True if the EXPLAIN flag is found on the query */
   653         -  int initFlag;        /* True if reparsing CREATE TABLEs */
   654         -  int nameClash;       /* A permanent table name clashes with temp table name */
          644  +  u8 colNamesSet;      /* TRUE after OP_ColumnCount has been issued to pVdbe */
          645  +  u8 explain;          /* True if the EXPLAIN flag is found on the query */
          646  +  u8 initFlag;         /* True if reparsing CREATE TABLEs */
          647  +  u8 nameClash;        /* A permanent table name clashes with temp table name */
          648  +  u8 useAgg;           /* If true, extract field values from the aggregator
          649  +                       ** while generating expressions.  Normally false */
          650  +  u8 schemaVerified;   /* True if an OP_VerifySchema has been coded someplace
          651  +                       ** other than after an OP_Transaction */
          652  +  u8 isTemp;           /* True if parsing temporary tables */
   655    653     int newTnum;         /* Table number to use when reparsing CREATE TABLEs */
   656    654     int nErr;            /* Number of errors seen */
   657    655     int nTab;            /* Number of previously allocated VDBE cursors */
   658    656     int nMem;            /* Number of memory cells used so far */
   659    657     int nSet;            /* Number of sets used so far */
   660    658     int nAgg;            /* Number of aggregate expressions */
   661    659     AggExpr *aAgg;       /* An array of aggregate expressions */
   662         -  int useAgg;          /* If true, extract field values from the aggregator
   663         -                       ** while generating expressions.  Normally false */
   664         -  int schemaVerified;  /* True if an OP_VerifySchema has been coded someplace
   665         -                       ** other than after an OP_Transaction */
   666    660     TriggerStack *trigStack;
   667    661   };
   668    662   
   669    663   /*
   670    664    * Each trigger present in the database schema is stored as an instance of
   671    665    * struct Trigger. 
   672    666    *
................................................................................
   707    701     IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
   708    702                                the <column-list> is stored here */
   709    703     int foreach;            /* One of TK_ROW or TK_STATEMENT */
   710    704   
   711    705     TriggerStep *step_list; /* Link list of trigger program steps             */
   712    706     char *strings;          /* pointer to allocation of Token strings */
   713    707     Trigger *pNext;         /* Next trigger associated with the table */
   714         -  int isCommit;           /* Set to TRUE once the trigger has been committed */
   715    708   };
   716    709   
   717    710   /*
   718    711    * An instance of struct TriggerStep is used to store a single SQL statement
   719    712    * that is a part of a trigger-program. 
   720    713    *
   721    714    * Instances of struct TriggerStep are stored in a singly linked list (linked
................................................................................
   843    836   Expr *sqliteExpr(int, Expr*, Expr*, Token*);
   844    837   void sqliteExprSpan(Expr*,Token*,Token*);
   845    838   Expr *sqliteExprFunction(ExprList*, Token*);
   846    839   void sqliteExprDelete(Expr*);
   847    840   ExprList *sqliteExprListAppend(ExprList*,Expr*,Token*);
   848    841   void sqliteExprListDelete(ExprList*);
   849    842   void sqlitePragma(Parse*,Token*,Token*,int);
   850         -void sqliteCommitInternalChanges(sqlite*);
          843  +void sqliteResetInternalSchema(sqlite*);
          844  +int sqliteInit(sqlite*, char**);
          845  +void sqliteBeginParse(Parse*,int);
   851    846   void sqliteRollbackInternalChanges(sqlite*);
          847  +void sqliteCommitInternalChanges(sqlite*);
   852    848   Table *sqliteResultSetOfSelect(Parse*,char*,Select*);
          849  +void sqliteOpenMasterTable(Vdbe *v, int);
   853    850   void sqliteStartTable(Parse*,Token*,Token*,int);
   854    851   void sqliteAddColumn(Parse*,Token*);
   855    852   void sqliteAddNotNull(Parse*, int);
   856    853   void sqliteAddPrimaryKey(Parse*, IdList*, int);
   857    854   void sqliteAddColumnType(Parse*,Token*,Token*);
   858    855   void sqliteAddDefaultValue(Parse*,Token*,int);
   859    856   int sqliteCollateType(Parse*, Token*);
................................................................................
   925    922   IdList *sqliteIdListDup(IdList*);
   926    923   Select *sqliteSelectDup(Select*);
   927    924   FuncDef *sqliteFindFunction(sqlite*,const char*,int,int,int);
   928    925   void sqliteRegisterBuiltinFunctions(sqlite*);
   929    926   int sqliteSafetyOn(sqlite*);
   930    927   int sqliteSafetyOff(sqlite*);
   931    928   int sqliteSafetyCheck(sqlite*);
   932         -void sqliteChangeCookie(sqlite *);
          929  +void sqliteChangeCookie(sqlite*, Vdbe*);
   933    930   void sqliteCreateTrigger(Parse*, Token*, int, int, IdList*, Token*, 
   934    931                            int, Expr*, TriggerStep*, char const*,int);
   935    932   void sqliteDropTrigger(Parse*, Token*, int);
   936    933   int sqliteTriggersExist(Parse* , Trigger* , int , int , int, ExprList*);
   937    934   int sqliteCodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int, 
   938    935                            int, int);
   939    936   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);

Changes to src/trigger.c.

    65     65         goto trigger_cleanup;
    66     66       }
    67     67       if( sqliteStrICmp(tab->zName, MASTER_NAME)==0 ){
    68     68         sqliteSetString(&pParse->zErrMsg, "cannot create trigger on system "
    69     69            "table: " MASTER_NAME, 0);
    70     70         pParse->nErr++;
    71     71         goto trigger_cleanup;
           72  +    }
           73  +    if( sqliteStrICmp(tab->zName, TEMP_MASTER_NAME)==0 ){
           74  +      sqliteSetString(&pParse->zErrMsg, "cannot create trigger on system "
           75  +         "table: " TEMP_MASTER_NAME, 0);
           76  +      pParse->nErr++;
           77  +      goto trigger_cleanup;
    72     78       }
    73     79       if( tab->pSelect && tr_tm != TK_INSTEAD ){
    74     80         sqliteSetNString(&pParse->zErrMsg, "cannot create ", -1,
    75     81   	  (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", -1, " trigger on view: ", -1
    76     82             , pTableName->z, pTableName->n, 0);
    77     83         goto trigger_cleanup;
    78     84       }
................................................................................
    96    102     if( sqlite_malloc_failed ) goto trigger_cleanup;
    97    103     nt->op = op;
    98    104     nt->tr_tm = tr_tm;
    99    105     nt->pWhen = pWhen;
   100    106     nt->pColumns = pColumns;
   101    107     nt->foreach = foreach;
   102    108     nt->step_list = pStepList;
   103         -  nt->isCommit = 0;
   104    109     offset = (int)(nt->strings - zData);
   105    110     sqliteExprMoveStrings(nt->pWhen, offset);
   106    111   
   107    112     ss = nt->step_list;
   108    113     while( ss ){
   109    114       sqliteSelectMoveStrings(ss->pSelect, offset);
   110    115       if( ss->target.z ){
................................................................................
   115    120   
   116    121       ss = ss->pNext;
   117    122     }
   118    123   
   119    124     /* if we are not initializing, and this trigger is not on a TEMP table, 
   120    125     ** build the sqlite_master entry
   121    126     */
   122         -  if( !pParse->initFlag && !tab->isTemp ){
          127  +  if( !pParse->initFlag ){
   123    128       static VdbeOp insertTrig[] = {
   124         -      { OP_OpenWrite,  0, 2,  MASTER_NAME},
   125    129         { OP_NewRecno,   0, 0,  0          },
   126    130         { OP_String,     0, 0,  "trigger"  },
   127         -      { OP_String,     0, 0,  0          },  /* 3: trigger name */
   128         -      { OP_String,     0, 0,  0          },  /* 4: table name */
          131  +      { OP_String,     0, 0,  0          },  /* 2: trigger name */
          132  +      { OP_String,     0, 0,  0          },  /* 3: table name */
   129    133         { OP_Integer,    0, 0,  0          },
   130         -      { OP_String,     0, 0,  0          },  /* 6: SQL */
          134  +      { OP_String,     0, 0,  0          },  /* 5: SQL */
   131    135         { OP_MakeRecord, 5, 0,  0          },
   132    136         { OP_PutIntKey,  0, 0,  0          },
   133         -      { OP_Integer,    0, 0,  0          },  /* 9: Next cookie */
   134         -      { OP_SetCookie,  0, 0,  0          },
   135         -      { OP_Close,      0, 0,  0          },
   136    137       };
   137    138       int addr;
   138    139       Vdbe *v;
   139    140   
   140    141       /* Make an entry in the sqlite_master table */
   141    142       v = sqliteGetVdbe(pParse);
   142    143       if( v==0 ) goto trigger_cleanup;
   143    144       sqliteBeginWriteOperation(pParse, 0);
          145  +    sqliteOpenMasterTable(v, tab->isTemp);
   144    146       addr = sqliteVdbeAddOpList(v, ArraySize(insertTrig), insertTrig);
   145         -    sqliteVdbeChangeP3(v, addr+3, nt->name, 0); 
   146         -    sqliteVdbeChangeP3(v, addr+4, nt->table, 0); 
   147         -    sqliteVdbeChangeP3(v, addr+6, nt->strings, 0);
   148         -    sqliteChangeCookie(pParse->db);
   149         -    sqliteVdbeChangeP1(v, addr+9, pParse->db->next_cookie);
          147  +    sqliteVdbeChangeP3(v, addr, tab->isTemp ? TEMP_MASTER_NAME : MASTER_NAME,
          148  +                       P3_STATIC);
          149  +    sqliteVdbeChangeP3(v, addr+2, nt->name, 0); 
          150  +    sqliteVdbeChangeP3(v, addr+3, nt->table, 0); 
          151  +    sqliteVdbeChangeP3(v, addr+5, nt->strings, 0);
          152  +    if( !tab->isTemp ){
          153  +      sqliteChangeCookie(pParse->db, v);
          154  +    }
          155  +    sqliteVdbeAddOp(v, OP_Close, 0, 0);
   150    156       sqliteEndWriteOperation(pParse);
   151    157     }
   152    158   
   153    159     if( !pParse->explain ){
   154    160       /* Stick it in the hash-table */
   155    161       sqliteHashInsert(&(pParse->db->trigHash), nt->name, pName->n + 1, nt);
   156    162   
................................................................................
   308    314    * sqliteDropTable(). In the latter case the "nested" argument is true.
   309    315    *
   310    316    * Note that this function does not delete the trigger entirely. Instead it
   311    317    * removes it from the internal schema and places it in the trigDrop hash 
   312    318    * table. This is so that the trigger can be restored into the database schema
   313    319    * if the transaction is rolled back.
   314    320    */
   315         -void sqliteDropTrigger(Parse *pParse, Token *pName, int nested)
   316         -{
          321  +void sqliteDropTrigger(Parse *pParse, Token *pName, int nested){
   317    322     char *zName;
   318    323     Trigger *pTrigger;
   319    324     Table   *pTable;
          325  +  Vdbe *v;
   320    326   
   321    327     zName = sqliteStrNDup(pName->z, pName->n);
   322    328   
   323    329     /* ensure that the trigger being dropped exists */
   324    330     pTrigger = sqliteHashFind(&(pParse->db->trigHash), zName, pName->n + 1); 
   325    331     if( !pTrigger ){
   326    332       sqliteSetNString(&pParse->zErrMsg, "no such trigger: ", -1,
   327    333           zName, -1, 0);
   328    334       sqliteFree(zName);
   329    335       return;
   330    336     }
   331    337   
   332    338     /*
   333         -   * If this is not an "explain", do the following:
   334         -   * 1. Remove the trigger from its associated table structure
   335         -   * 2. Move the trigger from the trigHash hash to trigDrop
          339  +   * If this is not an "explain", then delete the trigger structure.
   336    340      */
   337    341     if( !pParse->explain ){
   338         -    /* 1 */
   339    342       pTable = sqliteFindTable(pParse->db, pTrigger->table);
   340    343       assert(pTable);
   341    344       if( pTable->pTrigger == pTrigger ){
   342    345         pTable->pTrigger = pTrigger->pNext;
   343    346       }else{
   344    347         Trigger *cc = pTable->pTrigger;
   345    348         while( cc ){ 
................................................................................
   347    350             cc->pNext = cc->pNext->pNext;
   348    351             break;
   349    352           }
   350    353           cc = cc->pNext;
   351    354         }
   352    355         assert(cc);
   353    356       }
   354         -
   355         -    /* 2 */
   356         -    sqliteHashInsert(&(pParse->db->trigHash), zName, 
   357         -        pName->n + 1, NULL);
   358         -    sqliteHashInsert(&(pParse->db->trigDrop), pTrigger->name, 
   359         -        pName->n + 1, pTrigger);
          357  +    sqliteHashInsert(&(pParse->db->trigHash), zName, pName->n + 1, NULL);
          358  +    sqliteDeleteTrigger(pTrigger);
   360    359     }
   361    360   
   362         -  /* Unless this is a trigger on a TEMP TABLE, generate code to destroy the
   363         -   * database record of the trigger */
   364         -  if( !pTable->isTemp ){
          361  +  /* Generate code to destroy the database record of the trigger.
          362  +  */
          363  +  if( pTable!=0 && !nested && (v = sqliteGetVdbe(pParse))!=0 ){
   365    364       int base;
   366    365       static VdbeOp dropTrigger[] = {
   367         -      { OP_OpenWrite,  0, 2,        MASTER_NAME},
   368         -      { OP_Rewind,     0, ADDR(9),  0},
   369         -      { OP_String,     0, 0,        0}, /* 2 */
          366  +      { OP_Rewind,     0, ADDR(8),  0},
          367  +      { OP_String,     0, 0,        0}, /* 1 */
   370    368         { OP_MemStore,   1, 1,        0},
   371         -      { OP_MemLoad,    1, 0,        0}, /* 4 */
          369  +      { OP_MemLoad,    1, 0,        0}, /* 3 */
   372    370         { OP_Column,     0, 1,        0},
   373         -      { OP_Ne,         0, ADDR(8),  0},
          371  +      { OP_Ne,         0, ADDR(7),  0},
   374    372         { OP_Delete,     0, 0,        0},
   375         -      { OP_Next,       0, ADDR(4),  0}, /* 8 */
   376         -      { OP_Integer,    0, 0,        0}, /* 9 */
   377         -      { OP_SetCookie,  0, 0,        0},
   378         -      { OP_Close,      0, 0,        0},
          373  +      { OP_Next,       0, ADDR(3),  0}, /* 7 */
   379    374       };
   380    375   
   381         -    if( !nested ){
   382         -      sqliteBeginWriteOperation(pParse, 0);
   383         -    }
   384         -    base = sqliteVdbeAddOpList(pParse->pVdbe, 
   385         -        ArraySize(dropTrigger), dropTrigger);
   386         -    sqliteVdbeChangeP3(pParse->pVdbe, base+2, zName, 0);
   387         -    if( !nested ){
   388         -      sqliteChangeCookie(pParse->db);
          376  +    sqliteBeginWriteOperation(pParse, 0);
          377  +    sqliteOpenMasterTable(v, pTable->isTemp);
          378  +    base = sqliteVdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
          379  +    sqliteVdbeChangeP3(v, base+1, zName, 0);
          380  +    if( !pTable->isTemp ){
          381  +      sqliteChangeCookie(pParse->db, v);
   389    382       }
   390         -    sqliteVdbeChangeP1(pParse->pVdbe, base+9, pParse->db->next_cookie);
   391         -    if( !nested ){
   392         -      sqliteEndWriteOperation(pParse);
   393         -    }
          383  +    sqliteVdbeAddOp(v, OP_Close, 0, 0);
          384  +    sqliteEndWriteOperation(pParse);
   394    385     }
   395    386   
   396    387     sqliteFree(zName);
   397    388   }
   398    389   
   399    390   /*
   400    391   ** pEList is the SET clause of an UPDATE statement.  Each entry

Changes to src/vdbe.c.

    26     26   ** type to the other occurs as necessary.
    27     27   ** 
    28     28   ** Most of the code in this file is taken up by the sqliteVdbeExec()
    29     29   ** function which does the work of interpreting a VDBE program.
    30     30   ** But other routines are also provided to help in building up
    31     31   ** a program instruction by instruction.
    32     32   **
    33         -** $Id: vdbe.c,v 1.158 2002/06/21 23:01:50 drh Exp $
           33  +** $Id: vdbe.c,v 1.159 2002/06/25 01:09:12 drh Exp $
    34     34   */
    35     35   #include "sqliteInt.h"
    36     36   #include <ctype.h>
    37     37   
    38     38   /*
    39     39   ** The following global variable is incremented every time a cursor
    40     40   ** moves, either by the OP_MoveTo or the OP_Next opcode.  The test
................................................................................
  3017   3017     Btree *pX;
  3018   3018     switch( pOp->opcode ){
  3019   3019       case OP_Open:        wrFlag = 0;  pX = pBt;          break;
  3020   3020       case OP_OpenWrite:   wrFlag = 1;  pX = pBt;          break;
  3021   3021       case OP_OpenAux:     wrFlag = 0;  pX = db->pBeTemp;  break;
  3022   3022       case OP_OpenWrAux:   wrFlag = 1;  pX = db->pBeTemp;  break;
  3023   3023     }
  3024         -  assert( pX!=0 );
  3025   3024     if( p2<=0 ){
  3026   3025       if( tos<0 ) goto not_enough_stack;
  3027   3026       Integerify(p, tos);
  3028   3027       p2 = p->aStack[tos].i;
  3029   3028       POPSTACK;
  3030   3029       if( p2<2 ){
  3031   3030         sqliteSetString(pzErrMsg, "root page number less than 2", 0);
................................................................................
  3043   3042         memset(&p->aCsr[j], 0, sizeof(Cursor));
  3044   3043       }
  3045   3044       p->nCursor = i+1;
  3046   3045     }
  3047   3046     cleanupCursor(&p->aCsr[i]);
  3048   3047     memset(&p->aCsr[i], 0, sizeof(Cursor));
  3049   3048     p->aCsr[i].nullRow = 1;
         3049  +  if( pX==0 ) break;
  3050   3050     do{
  3051   3051       rc = sqliteBtreeCursor(pX, p2, wrFlag, &p->aCsr[i].pCursor);
  3052   3052       switch( rc ){
  3053   3053         case SQLITE_BUSY: {
  3054   3054           if( xBusy==0 || (*xBusy)(pBusyArg, pOp->p3, ++busy)==0 ){
  3055   3055             sqliteSetString(pzErrMsg, sqlite_error_string(rc), 0);
  3056   3056             busy = 0;

Changes to src/where.c.

     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  Also found here are subroutines
    14     14   ** to generate VDBE code to evaluate expressions.
    15     15   **
    16         -** $Id: where.c,v 1.55 2002/06/24 22:01:59 drh Exp $
           16  +** $Id: where.c,v 1.56 2002/06/25 01:09:12 drh Exp $
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   
    20     20   /*
    21     21   ** The query generator uses an array of instances of this structure to
    22     22   ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
    23     23   ** clause subexpression is separated from the others by an AND operator.
................................................................................
   475    475       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   476    476         int eqMask = 0;  /* Index columns covered by an x=... term */
   477    477         int ltMask = 0;  /* Index columns covered by an x<... term */
   478    478         int gtMask = 0;  /* Index columns covered by an x>... term */
   479    479         int inMask = 0;  /* Index columns covered by an x IN .. term */
   480    480         int nEq, m, score;
   481    481   
   482         -      if( pIdx->isDropped ) continue;   /* Ignore dropped indices */
   483    482         if( pIdx->nColumn>32 ) continue;  /* Ignore indices too many columns */
   484    483         for(j=0; j<nExpr; j++){
   485    484           if( aExpr[j].idxLeft==idx 
   486    485                && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
   487    486             int iColumn = aExpr[j].p->pLeft->iColumn;
   488    487             int k;
   489    488             for(k=0; k<pIdx->nColumn; k++){

Changes to test/temptable.test.

     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.
    12     12   #
    13     13   # This file implements tests for temporary tables and indices.
    14     14   #
    15         -# $Id: temptable.test,v 1.6 2002/06/06 23:16:06 drh Exp $
           15  +# $Id: temptable.test,v 1.7 2002/06/25 01:09:12 drh Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   # Create an alternative connection to the database
    21     21   #
    22     22   do_test temptable-1.0 {
................................................................................
   110    110     execsql {COMMIT}
   111    111     catchsql {SELECT * FROM t2}
   112    112   } {0 {1 2}}
   113    113   do_test temptable-2.5 {
   114    114     set r [catch {db2 eval {SELECT * FROM t2}} msg]
   115    115     lappend r $msg
   116    116   } {1 {no such table: t2}}
   117         -
   118    117   
   119    118   # Make sure indices on temporary tables are also temporary.
   120    119   #
   121    120   do_test temptable-3.1 {
   122    121     execsql {
   123    122       CREATE INDEX i2 ON t2(x);
   124    123       SELECT name FROM sqlite_master WHERE type='index';

Changes to test/trigger3.test.

   154    154       catchsql {
   155    155   	INSERT INTO tbl_view VALUES(3, 2, 3);
   156    156       }
   157    157   } {1 {View abort}}
   158    158   
   159    159   catchsql { DROP TABLE tbl; } 
   160    160   catchsql { DROP TABLE tbl2; } 
   161         -catchsql { DROP VIEW tbl_view; } 
          161  +catchsql { DROP VIEW tbl_view; }
   162    162   
   163         -
          163  +finish_test

Changes to www/changes.tcl.

    20     20   }
    21     21   
    22     22   
    23     23   proc chng {date desc} {
    24     24     puts "<DT><B>$date</B></DT>"
    25     25     puts "<DD><P><UL>$desc</UL></P></DD>"
    26     26   }
           27  +
           28  +chng {2002 Jun 24 (2.5.2)} {
           29  +<li>Added the new <b>SQLITE_TEMP_MASTER</b> table which records the schema
           30  +    for temporary tables in the same way that <b>SQLITE_MASTER</b> does for
           31  +    persistent tables.</li>
           32  +<li>Added an optimization to UNION ALL</li>
           33  +<li>Fixed a bug in the processing of LEFT OUTER JOIN</li>
           34  +<li>The LIMIT clause now works on subselects</li>
           35  +<li>ORDER BY works on subselects</li>
           36  +<li>There is a new TypeOf() function used to determine if an expression
           37  +    is numeric or text.</li>
           38  +<li>Autoincrement now works for INSERT from a SELECT.</li>
           39  +}
    27     40   
    28     41   chng {2002 Jun 19 (2.5.1)} {
    29     42   <li>The query optimizer now attempts to implement the ORDER BY clause
    30     43       using an index.  Sorting is still used if not suitable index is
    31     44       available.</li>
    32     45   }
    33     46   

Changes to www/faq.tcl.

     1      1   #
     2      2   # Run this script to generated a faq.html output file
     3      3   #
     4         -set rcsid {$Id: faq.tcl,v 1.10 2002/04/25 00:21:50 drh Exp $}
            4  +set rcsid {$Id: faq.tcl,v 1.11 2002/06/25 01:09:13 drh Exp $}
     5      5   
     6      6   puts {<html>
     7      7   <head>
     8      8     <title>SQLite Frequently Asked Questions</title>
     9      9   </head>
    10     10   <body bgcolor="white">
    11     11   <h1 align="center">Frequently Asked Questions</h1>
................................................................................
   272    272     to implement the PRIMARY KEY or UNIQUE constraints) the <b>sql</b> field
   273    273     is NULL.</p>
   274    274   
   275    275     <p>The SQLITE_MASTER table is read-only.  You cannot change this table
   276    276     using UPDATE, INSERT, or DELETE.  The table is automatically updated by
   277    277     CREATE TABLE, CREATE INDEX, DROP TABLE, and DROP INDEX commands.</p>
   278    278   
   279         -  <p>Temporary tables do not appear in the SQLITE_MASTER table.  At this time
   280         -  there is no way to get a listing of temporary tables and indices.</p>
          279  +  <p>Temporary tables do not appear in the SQLITE_MASTER table.  Temporary
          280  +  tables and their indices and triggers occur in another special table
          281  +  named SQLITE_TEMP_MASTER.  SQLITE_TEMP_MASTER works just like SQLITE_MASTER
          282  +  except that it is only visible to the application that created the 
          283  +  temporary tables.  To get a list of all tables, both permanent and
          284  +  temporary, one can use a command similar to the following:
          285  +</blockquote><pre>
          286  +SELECT name FROM 
          287  +   (SELECT * FROM sqlite_master UNION ALL
          288  +    SELECT * FROM sqlite_temp_master)
          289  +WHERE type='table'
          290  +ORDER BY name
          291  +</pre></blockquote>
   281    292   }
   282    293   
   283    294   faq {
   284    295     Are there any known size limits to SQLite databases?
   285    296   } {
   286    297     <p>Internally, SQLite can handle databases up to 2^40 bytes (1 terabyte)
   287    298     in size.  But the backend interface to POSIX and Win32 limits files to

Changes to www/lang.tcl.

     1      1   #
     2      2   # Run this Tcl script to generate the sqlite.html file.
     3      3   #
     4         -set rcsid {$Id: lang.tcl,v 1.40 2002/06/12 22:33:54 drh Exp $}
            4  +set rcsid {$Id: lang.tcl,v 1.41 2002/06/25 01:09:13 drh Exp $}
     5      5   
     6      6   puts {<html>
     7      7   <head>
     8      8     <title>Query Language Understood By SQLite</title>
     9      9   </head>
    10     10   <body bgcolor=white>
    11     11   <h1 align=center>
................................................................................
   232    232   conflict resolution algorithm, that algorithm is used in place of
   233    233   the default algorithm specified here.
   234    234   See the section titled
   235    235   <a href="#conflict">ON CONFLICT</a> for additional information.</p>
   236    236   
   237    237   <p>The exact text
   238    238   of each CREATE INDEX statement is stored in the <b>sqlite_master</b>
   239         -table.  Everytime the database is opened, all CREATE INDEX statements
          239  +or <b>sqlite_temp_master</b> table, depending on whether the table
          240  +being indexed is temporary.  Everytime the database is opened,
          241  +all CREATE INDEX statements
   240    242   are read from the <b>sqlite_master</b> table and used to regenerate
   241    243   SQLite's internal representation of the index layout.</p>
   242    244   }
   243    245   
   244    246   
   245    247   Section {CREATE TABLE} {createtable}
   246    248   
................................................................................
   271    273   ON CONFLICT <conflict-algorithm>
   272    274   }
   273    275   
   274    276   puts {
   275    277   <p>A CREATE TABLE statement is basically the keywords "CREATE TABLE"
   276    278   followed by the name of a new table and a parenthesized list of column
   277    279   definitions and constraints.  The table name can be either an identifier
   278         -or a string.  The only reserved table name is "<b>sqlite_master</b>" which
   279         -is the name of the table that records the database schema.</p>
          280  +or a string.  Tables names that begin with "<b>sqlite_</b>" are reserved
          281  +for use by the engine.</p>
   280    282   
   281    283   <p>Each column definition is the name of the column followed by the
   282    284   datatype for that column, then one or more optional column constraints.
   283    285   The datatype for the column is (usually) ignored and may be omitted.
   284    286   All information is stored as null-terminated strings.
   285    287   The UNIQUE constraint causes an index to be created on the specified
   286    288   columns.  This index must contain unique keys.
................................................................................
   339    341   of each CREATE TABLE statement is stored in the <b>sqlite_master</b>
   340    342   table.  Everytime the database is opened, all CREATE TABLE statements
   341    343   are read from the <b>sqlite_master</b> table and used to regenerate
   342    344   SQLite's internal representation of the table layout.
   343    345   If the original command was a CREATE TABLE AS then then an equivalent
   344    346   CREATE TABLE statement is synthesized and store in <b>sqlite_master</b>
   345    347   in place of the original command.
          348  +The text of CREATE TEMPORARY TABLE statements are stored in the
          349  +<b>sqlite_temp_master</b> table.
   346    350   </p>
   347    351   }
   348    352   Section {CREATE TRIGGER} createtrigger
   349    353   
   350    354   Syntax {sql-statement} {
   351    355   CREATE TRIGGER <trigger-name> [ BEFORE | AFTER ]
   352    356   <database-event> ON <table-name>

Changes to www/sqlite.tcl.

     1      1   #
     2      2   # Run this Tcl script to generate the sqlite.html file.
     3      3   #
     4         -set rcsid {$Id: sqlite.tcl,v 1.16 2001/11/24 13:23:05 drh Exp $}
            4  +set rcsid {$Id: sqlite.tcl,v 1.17 2002/06/25 01:09:13 drh Exp $}
     5      5   
     6      6   puts {<html>
     7      7   <head>
     8      8     <title>sqlite: A program of interacting with SQLite databases</title>
     9      9   </head>
    10     10   <body bgcolor=white>
    11     11   <h1 align=center>
................................................................................
   113    113   But you cannot execute DROP TABLE, UPDATE, INSERT or DELETE against
   114    114   the sqlite_master table.  The sqlite_master
   115    115   table is updated automatically as you create or drop tables and
   116    116   indices from the database.  You can not make manual changes
   117    117   to the sqlite_master table.
   118    118   </p>
   119    119   
          120  +<p>
          121  +The schema for TEMPORARY tables is not stored in the "sqlite_master" table
          122  +since TEMPORARY tables are not visible to applications other than the
          123  +application that created the table.  The schema for TEMPORARY tables
          124  +is stored in another special table named "sqlite_temp_master".  The
          125  +"sqlite_temp_master" table is temporary itself.
          126  +</p>
          127  +
   120    128   <h2>Special commands to sqlite</h2>
   121    129   
   122    130   <p>
   123    131   Most of the time, sqlite just reads lines of input and passes them
   124    132   on to the SQLite library for execution.
   125    133   But if an input line begins with a dot ("."), then
   126    134   that line is intercepted and interpreted by the sqlite program itself.
................................................................................
   331    339   }
   332    340   
   333    341   puts {
   334    342   <p>The ".tables" command is the same as setting list mode then
   335    343   executing the following query:</p>
   336    344   
   337    345   <blockquote><pre>
   338         -SELECT name FROM sqlite_master 
   339         -WHERE type='table' 
          346  +SELECT name FROM sqlite_master WHERE type='table' 
          347  +UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table'
   340    348   ORDER BY name;
   341    349   </pre></blockquote>
   342    350   
   343    351   <p>In fact, if you look at the source code to the sqlite program
   344    352   (found in the source tree in the file src/shell.c) you'll find
   345    353   exactly the above query.</p>
   346    354   
................................................................................
   372    380   }
   373    381   
   374    382   puts {
   375    383   <p>The ".schema" command accomplishes the same thing as setting
   376    384   list mode, then entering the following query:</p>
   377    385   
   378    386   <blockquote><pre>
   379         -SELECT sql FROM sqlite_master
          387  +SELECT sql FROM 
          388  +   (SELECT * FROM sqlite_master UNION ALL
          389  +    SELECT * FROM sqlite_temp_master)
   380    390   WHERE type!='meta'
   381    391   ORDER BY tbl_name, type DESC, name
   382    392   </pre></blockquote>
   383    393   
   384    394   <p>Or, if you give an argument to ".schema" because you only
   385    395   want the schema for a single table, the query looks like this:</p>
   386    396   
   387    397   <blockquote><pre>
   388         -SELECT sql FROM sqlite_master
          398  +SELECT sql FROM
          399  +   (SELECT * FROM sqlite_master UNION ALL
          400  +    SELECT * FROM sqlite_temp_master)
   389    401   WHERE tbl_name LIKE '%s' AND type!='meta'
   390    402   ORDER BY type DESC, name
   391    403   </pre></blockquote>
   392    404   
   393    405   <p>The <b>%s</b> in the query above is replaced by the argument
   394    406   to ".schema", of course.  Notice that the argument to the ".schema"
   395    407   command appears to the right of an SQL LIKE operator.  So you can