/ Check-in [9d5abc1d]
Login

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

Overview
Comment:Support for a future ALTER TABLE command to add columns with default values. (CVS 2367)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9d5abc1ddf6da37563c12d5a0401b89bb4e51c59
User & Date: danielk1977 2005-03-09 12:26:51
Context
2005-03-09
13:09
Fix a file corruption bug in CREATE INDEX in auto-vacuum databases. (CVS 2368) check-in: 64c4c717 user: danielk1977 tags: trunk
12:26
Support for a future ALTER TABLE command to add columns with default values. (CVS 2367) check-in: 9d5abc1d user: danielk1977 tags: trunk
2005-03-02
05:18
Print an error message and quit if the regression tests are run as root. Ticket #1153. (CVS 2366) check-in: 9b960787 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/delete.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 C code routines that are called by the parser
    13     13   ** in order to generate code for DELETE FROM statements.
    14     14   **
    15         -** $Id: delete.c,v 1.100 2005/01/29 08:32:45 danielk1977 Exp $
           15  +** $Id: delete.c,v 1.101 2005/03/09 12:26:51 danielk1977 Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Look up every table that is named in pSrc.  If any table is not found,
    21     21   ** add an error message to pParse->zErrMsg and return NULL.  If all tables
    22     22   ** are found, return a pointer to the last table.
................................................................................
   432    432     sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
   433    433     for(j=0; j<pIdx->nColumn; j++){
   434    434       int idx = pIdx->aiColumn[j];
   435    435       if( idx==pTab->iPKey ){
   436    436         sqlite3VdbeAddOp(v, OP_Dup, j, 0);
   437    437       }else{
   438    438         sqlite3VdbeAddOp(v, OP_Column, iCur, idx);
          439  +      sqlite3ColumnDefault(v, pTab, idx);
   439    440       }
   440    441     }
   441    442     sqlite3VdbeAddOp(v, OP_MakeRecord, pIdx->nColumn, (1<<24));
   442    443     sqlite3IndexAffinityStr(v, pIdx);
   443    444   }

Changes to src/expr.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 routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.194 2005/02/12 08:59:57 danielk1977 Exp $
           15  +** $Id: expr.c,v 1.195 2005/03/09 12:26:51 danielk1977 Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   /*
    21     21   ** Return the 'affinity' of the expression pExpr if any.
    22     22   **
................................................................................
   399    399       assert( pNew->token.z==0 );
   400    400     }
   401    401     pNew->span.z = 0;
   402    402     pNew->pLeft = sqlite3ExprDup(p->pLeft);
   403    403     pNew->pRight = sqlite3ExprDup(p->pRight);
   404    404     pNew->pList = sqlite3ExprListDup(p->pList);
   405    405     pNew->pSelect = sqlite3SelectDup(p->pSelect);
          406  +  pNew->pTab = p->pTab;
   406    407     return pNew;
   407    408   }
   408    409   void sqlite3TokenCopy(Token *pTo, Token *pFrom){
   409    410     if( pTo->dyn ) sqliteFree((char*)pTo->z);
   410    411     if( pFrom->z ){
   411    412       pTo->n = pFrom->n;
   412    413       pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
................................................................................
   843    844           cntTab++;
   844    845           for(j=0; j < pTab->nCol; j++, pCol++) {
   845    846             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
   846    847               cnt++;
   847    848               pExpr->iColumn = j==pTab->iPKey ? -1 : j;
   848    849               pExpr->affinity = pTab->aCol[j].affinity;
   849    850               pExpr->pColl = pTab->aCol[j].pColl;
          851  +            pExpr->pTab = pTab;
   850    852               break;
   851    853             }
   852    854           }
   853    855         }
   854    856       }
   855    857   #endif /* !defined(SQLITE_OMIT_TRIGGER) */
   856    858   
................................................................................
   954    956     pExpr->pLeft = 0;
   955    957     sqlite3ExprDelete(pExpr->pRight);
   956    958     pExpr->pRight = 0;
   957    959     pExpr->op = TK_COLUMN;
   958    960     if( cnt==1 ){
   959    961       assert( pNC!=0 );
   960    962       sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
          963  +    if( pMatch && !pMatch->pSelect ){
          964  +      pExpr->pTab = pMatch->pTab;
          965  +    }
   961    966     }
   962    967     return cnt!=1;
   963    968   }
   964    969   
   965    970   /*
   966    971   ** pExpr is a node that defines a function of some kind.  It might
   967    972   ** be a syntactic function like "count(x)" or it might be a function
................................................................................
  1381   1386     op = pExpr->op;
  1382   1387     switch( op ){
  1383   1388       case TK_COLUMN: {
  1384   1389         if( !pParse->fillAgg && pExpr->iAgg>=0 ){
  1385   1390           sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
  1386   1391         }else if( pExpr->iColumn>=0 ){
  1387   1392           sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
  1388         -#ifndef NDEBUG
  1389         -        if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
  1390         -          VdbeComment((v, "# %T", &pExpr->span));
  1391         -        }
  1392         -#endif
         1393  +        sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
  1393   1394         }else{
  1394   1395           sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
  1395   1396         }
  1396   1397         break;
  1397   1398       }
  1398   1399       case TK_INTEGER: {
  1399   1400         codeInteger(v, pExpr->token.z, pExpr->token.n);

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.281 2005/02/19 08:18:06 danielk1977 Exp $
           17  +** $Id: main.c,v 1.282 2005/03/09 12:26:51 danielk1977 Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** The following constant value is used by the SQLITE_BIGENDIAN and
................................................................................
   254    254     if( iDb==0 ){
   255    255       db->file_format = meta[1];
   256    256       if( db->file_format==0 ){
   257    257         /* This happens if the database was initially empty */
   258    258         db->file_format = 1;
   259    259       }
   260    260   
   261         -    if( db->file_format==2 ){
          261  +    if( db->file_format==2 || db->file_format==3 ){
   262    262         /* File format 2 is treated exactly as file format 1. New 
   263    263         ** databases are created with file format 1.
   264    264         */ 
   265    265         db->file_format = 1;
   266    266       }
   267    267     }
   268    268   
   269    269     /*
   270    270     ** file_format==1    Version 3.0.0.
   271    271     ** file_format==2    Version 3.1.3.
          272  +  ** file_format==3    Version 3.1.4.
   272    273     **
   273    274     ** Version 3.0 can only use files with file_format==1. Version 3.1.3
   274    275     ** can read and write files with file_format==1 or file_format==2.
          276  +  ** Version 3.1.4 can read and write file formats 1, 2 and 3.
   275    277     */
   276         -  if( meta[1]>2 ){
          278  +  if( meta[1]>3 ){
   277    279       sqlite3BtreeCloseCursor(curMain);
   278    280       sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
   279    281       return SQLITE_ERROR;
   280    282     }
   281    283   
   282    284     sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
   283    285   

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.166 2005/02/14 06:38:40 danielk1977 Exp $
           17  +** @(#) $Id: parse.y,v 1.167 2005/03/09 12:26:51 danielk1977 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     if( pParse->zErrMsg==0 ){
................................................................................
   599    599     Expr *temp3 = sqlite3Expr(TK_ID, 0, 0, &Z);
   600    600     Expr *temp4 = sqlite3Expr(TK_DOT, temp2, temp3, 0);
   601    601     A = sqlite3Expr(TK_DOT, temp1, temp4, 0);
   602    602   }
   603    603   term(A) ::= INTEGER(X).      {A = sqlite3Expr(@X, 0, 0, &X);}
   604    604   term(A) ::= FLOAT(X).        {A = sqlite3Expr(@X, 0, 0, &X);}
   605    605   term(A) ::= STRING(X).       {A = sqlite3Expr(@X, 0, 0, &X);}
   606         -expr(A) ::= BLOB(X).         {A = sqlite3Expr(@X, 0, 0, &X);}
          606  +term(A) ::= BLOB(X).         {A = sqlite3Expr(@X, 0, 0, &X);}
   607    607   expr(A) ::= REGISTER(X).     {A = sqlite3RegisterExpr(pParse, &X);}
   608    608   expr(A) ::= VARIABLE(X).     {
   609    609     Token *pToken = &X;
   610    610     Expr *pExpr = A = sqlite3Expr(TK_VARIABLE, 0, 0, pToken);
   611    611     sqlite3ExprAssignVarNumber(pParse, pExpr);
   612    612   }
   613    613   expr(A) ::= ID(X) LP exprlist(Y) RP(E). {

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.371 2005/02/15 21:36:18 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.372 2005/03/09 12:26:51 danielk1977 Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** These #defines should enable >2GB file support on Posix if the
    21     21   ** underlying operating system supports it.  If the OS lacks
................................................................................
   801    801   ** gives a different answer at different times during statement processing
   802    802   ** then iTable is the address of a subroutine that computes the subquery.
   803    803   **
   804    804   ** The Expr.pSelect field points to a SELECT statement.  The SELECT might
   805    805   ** be the right operand of an IN operator.  Or, if a scalar SELECT appears
   806    806   ** in an expression the opcode is TK_SELECT and Expr.pSelect is the only
   807    807   ** operand.
          808  +**
          809  +** If the Expr is of type OP_Column, and the table it is selecting from
          810  +** is a disk table or the "old.*" pseudo-table, then pTab points to the
          811  +** corresponding table definition.
   808    812   */
   809    813   struct Expr {
   810    814     u8 op;                 /* Operation performed by this node */
   811    815     char affinity;         /* The affinity of the column or 0 if not a column */
   812    816     u8 iDb;                /* Database referenced by this expression */
   813    817     u8 flags;              /* Various flags.  See below */
   814    818     CollSeq *pColl;        /* The collation type of the column or 0 */
................................................................................
   820    824     int iTable, iColumn;   /* When op==TK_COLUMN, then this expr node means the
   821    825                            ** iColumn-th field of the iTable-th table. */
   822    826     int iAgg;              /* When op==TK_COLUMN and pParse->fillAgg==FALSE, pull
   823    827                            ** result from the iAgg-th element of the aggregator */
   824    828     int iAggCtx;           /* The value to pass as P1 of OP_AggGet. */
   825    829     Select *pSelect;       /* When the expression is a sub-select.  Also the
   826    830                            ** right side of "<expr> IN (<select>)" */
          831  +  Table *pTab;           /* Table for OP_Column expressions. */
   827    832   };
   828    833   
   829    834   /*
   830    835   ** The following are the meanings of bits in the Expr.flags field.
   831    836   */
   832    837   #define EP_FromJoin     0x0001  /* Originated in ON or USING clause of a join */
   833    838   #define EP_Agg          0x0002  /* Contains one or more aggregate functions */
................................................................................
  1532   1537   
  1533   1538   const void *sqlite3ValueText(sqlite3_value*, u8);
  1534   1539   int sqlite3ValueBytes(sqlite3_value*, u8);
  1535   1540   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, void(*)(void*));
  1536   1541   void sqlite3ValueFree(sqlite3_value*);
  1537   1542   sqlite3_value *sqlite3ValueNew();
  1538   1543   sqlite3_value *sqlite3GetTransientValue(sqlite3*db);
         1544  +int sqlite3ValueFromExpr(Expr *, u8, u8, sqlite3_value **);
         1545  +void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  1539   1546   extern const unsigned char sqlite3UpperToLower[];
  1540   1547   void sqlite3RootPageMoved(Db*, int, int);
  1541   1548   void sqlite3Reindex(Parse*, Token*, Token*);
  1542   1549   void sqlite3AlterFunctions(sqlite3*);
  1543   1550   void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
  1544   1551   int sqlite3GetToken(const unsigned char *, int *);
  1545   1552   void sqlite3NestedParse(Parse*, const char*, ...);
  1546   1553   void sqlite3ExpirePreparedStatements(sqlite3*);
  1547   1554   void sqlite3CodeSubselect(Parse *, Expr *);
  1548   1555   int sqlite3SelectResolve(Parse *, Select *, NameContext *);
         1556  +void sqlite3ColumnDefault(Vdbe *, Table *, int);
  1549   1557   
  1550   1558   #endif

Changes to src/update.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 C code routines that are called by the parser
    13     13   ** to handle UPDATE statements.
    14     14   **
    15         -** $Id: update.c,v 1.104 2005/01/29 08:32:45 danielk1977 Exp $
           15  +** $Id: update.c,v 1.105 2005/03/09 12:26:51 danielk1977 Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
           18  +
           19  +/*
           20  +** The most recently coded instruction was an OP_Column to retrieve column
           21  +** 'i' of table pTab. This routine sets the P3 parameter of the 
           22  +** OP_Column to the default value, if any.
           23  +**
           24  +** The default value of a column is specified by a DEFAULT clause in the 
           25  +** column definition. This was either supplied by the user when the table
           26  +** was created, or added later to the table definition by an ALTER TABLE
           27  +** command. If the latter, then the row-records in the table btree on disk
           28  +** may not contain a value for the column and the default value, taken
           29  +** from the P3 parameter of the OP_Column instruction, is returned instead.
           30  +** If the former, then all row-records are guaranteed to include a value
           31  +** for the column and the P3 value is not required.
           32  +**
           33  +** Column definitions created by an ALTER TABLE command may only have 
           34  +** literal default values specified: a number, null or a string. (If a more
           35  +** complicated default expression value was provided, it is evaluated 
           36  +** when the ALTER TABLE is executed and one of the literal values written
           37  +** into the sqlite_master table.)
           38  +**
           39  +** Therefore, the P3 parameter is only required if the default value for
           40  +** the column is a literal number, string or null. The sqlite3ValueFromExpr()
           41  +** function is capable of transforming these types of expressions into
           42  +** sqlite3_value objects.
           43  +*/
           44  +void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){
           45  +  if( pTab && !pTab->pSelect ){
           46  +    sqlite3_value *pValue;
           47  +    u8 enc = sqlite3VdbeDb(v)->enc;
           48  +    Column *pCol = &pTab->aCol[i];
           49  +    sqlite3ValueFromExpr(pCol->pDflt, enc, pCol->affinity, &pValue);
           50  +    sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM);
           51  +  }
           52  +}
    18     53   
    19     54   /*
    20     55   ** Process an UPDATE statement.
    21     56   **
    22     57   **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
    23     58   **          \_______/ \________/     \______/       \________________/
    24     59   *            onError   pTabList      pChanges             pWhere
................................................................................
   290    325         if( i==pTab->iPKey ){
   291    326           sqlite3VdbeAddOp(v, OP_String8, 0, 0);
   292    327           continue;
   293    328         }
   294    329         j = aXRef[i];
   295    330         if( j<0 ){
   296    331           sqlite3VdbeAddOp(v, OP_Column, iCur, i);
          332  +        sqlite3ColumnDefault(v, pTab, i);
   297    333         }else{
   298    334           sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr);
   299    335         }
   300    336       }
   301    337       sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
   302    338       if( !isView ){
   303    339         sqlite3TableAffinityStr(v, pTab);
................................................................................
   374    410         if( i==pTab->iPKey ){
   375    411           sqlite3VdbeAddOp(v, OP_String8, 0, 0);
   376    412           continue;
   377    413         }
   378    414         j = aXRef[i];
   379    415         if( j<0 ){
   380    416           sqlite3VdbeAddOp(v, OP_Column, iCur, i);
          417  +        sqlite3ColumnDefault(v, pTab, i);
   381    418         }else{
   382    419           sqlite3ExprCode(pParse, pChanges->a[j].pExpr);
   383    420         }
   384    421       }
   385    422   
   386    423       /* Do constraint checks
   387    424       */

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.456 2005/02/19 08:18:06 danielk1977 Exp $
           46  +** $Id: vdbe.c,v 1.457 2005/03/09 12:26:51 danielk1977 Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
   298    298           if( ((double)pRec->i)==pRec->r ){
   299    299             pRec->flags |= MEM_Int;
   300    300           }
   301    301         }
   302    302       }
   303    303     }
   304    304   }
          305  +
          306  +/*
          307  +** Exported version of applyAffinity(). This one works on sqlite3_value*, 
          308  +** not the internal Mem* type.
          309  +*/
          310  +void sqlite3ValueApplyAffinity(sqlite3_value *pVal, u8 affinity, u8 enc){
          311  +  applyAffinity((Mem *)pVal, affinity, enc);
          312  +}
   305    313   
   306    314   #ifdef SQLITE_DEBUG
   307    315   /*
   308    316   ** Write a nice string representation of the contents of cell pMem
   309    317   ** into buffer zBuf, length nBuf.
   310    318   */
   311    319   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf, int nBuf){
................................................................................
  1673   1681     if( (!pC->keyAsData && pC->zeroData) || (pC->keyAsData && pC->intKey) ){
  1674   1682       rc = SQLITE_CORRUPT;
  1675   1683       goto abort_due_to_error;
  1676   1684     }
  1677   1685     break;
  1678   1686   }
  1679   1687   
  1680         -/* Opcode: Column P1 P2 *
         1688  +/* Opcode: Column P1 P2 P3
  1681   1689   **
  1682   1690   ** Interpret the data that cursor P1 points to as a structure built using
  1683   1691   ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
  1684   1692   ** information about the format of the data.) Push onto the stack the value
  1685   1693   ** of the P2-th column contained in the data. If there are less that (P2+1) 
  1686   1694   ** values in the record, push a NULL onto the stack.
  1687   1695   **
................................................................................
  1907   1915           goto op_column_out;
  1908   1916         }
  1909   1917         zData = sMem.z;
  1910   1918       }
  1911   1919       sqlite3VdbeSerialGet(zData, aType[p2], pTos);
  1912   1920       pTos->enc = db->enc;
  1913   1921     }else{
  1914         -    pTos->flags = MEM_Null;
         1922  +    if( pOp->p3 ){
         1923  +      sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static);
         1924  +    }else{
         1925  +      pTos->flags = MEM_Null;
         1926  +    }
  1915   1927     }
  1916   1928   
  1917   1929     /* If we dynamically allocated space to hold the data (in the
  1918   1930     ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  1919   1931     ** dynamically allocated space over to the pTos structure rather.
  1920   1932     ** This prevents a memory copy.
  1921   1933     */

Changes to src/vdbe.h.

    11     11   *************************************************************************
    12     12   ** Header file for the Virtual DataBase Engine (VDBE)
    13     13   **
    14     14   ** This header defines the interface to the virtual database engine
    15     15   ** or VDBE.  The VDBE implements an abstract machine that runs a
    16     16   ** simple program to access and modify the underlying database.
    17     17   **
    18         -** $Id: vdbe.h,v 1.92 2005/01/29 08:32:45 danielk1977 Exp $
           18  +** $Id: vdbe.h,v 1.93 2005/03/09 12:26:51 danielk1977 Exp $
    19     19   */
    20     20   #ifndef _SQLITE_VDBE_H_
    21     21   #define _SQLITE_VDBE_H_
    22     22   #include <stdio.h>
    23     23   
    24     24   /*
    25     25   ** A single VDBE is an opaque structure named "Vdbe".  Only routines
................................................................................
    65     65   #define P3_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
    66     66   #define P3_STATIC   (-2)  /* Pointer to a static string */
    67     67   #define P3_POINTER  (-3)  /* P3 is a pointer to some structure or object */
    68     68   #define P3_COLLSEQ  (-4)  /* P3 is a pointer to a CollSeq structure */
    69     69   #define P3_FUNCDEF  (-5)  /* P3 is a pointer to a FuncDef structure */
    70     70   #define P3_KEYINFO  (-6)  /* P3 is a pointer to a KeyInfo structure */
    71     71   #define P3_VDBEFUNC (-7)  /* P3 is a pointer to a VdbeFunc structure */
           72  +#define P3_MEM      (-8)  /* P3 is a pointer to a Mem*    structure */
    72     73   
    73     74   /* When adding a P3 argument using P3_KEYINFO, a copy of the KeyInfo structure
    74     75   ** is made.  That copy is freed when the Vdbe is finalized.  But if the
    75     76   ** argument is P3_KEYINFO_HANDOFF, the passed in pointer is used.  It still
    76     77   ** gets freed when the Vdbe is finalized so it still should be obtained
    77     78   ** from a single sqliteMalloc().  But no copy is made and the calling
    78     79   ** function should *not* try to free the KeyInfo.
................................................................................
   116    117   int sqlite3VdbeCurrentAddr(Vdbe*);
   117    118   void sqlite3VdbeTrace(Vdbe*,FILE*);
   118    119   int sqlite3VdbeReset(Vdbe*);
   119    120   int sqliteVdbeSetVariables(Vdbe*,int,const char**);
   120    121   void sqlite3VdbeSetNumCols(Vdbe*,int);
   121    122   int sqlite3VdbeSetColName(Vdbe*, int, const char *, int);
   122    123   void sqlite3VdbeCountChanges(Vdbe*);
          124  +sqlite3 *sqlite3VdbeDb(Vdbe*);
   123    125   
   124    126   #ifndef NDEBUG
   125    127     void sqlite3VdbeComment(Vdbe*, const char*, ...);
   126    128   # define VdbeComment(X)  sqlite3VdbeComment X
   127    129   #else
   128    130   # define VdbeComment(X)
   129    131   #endif
   130    132   
   131    133   #endif

Changes to src/vdbeaux.c.

  1416   1416           sqliteFree(pOp->p3);
  1417   1417         }
  1418   1418         if( pOp->p3type==P3_VDBEFUNC ){
  1419   1419           VdbeFunc *pVdbeFunc = (VdbeFunc *)pOp->p3;
  1420   1420           sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
  1421   1421           sqliteFree(pVdbeFunc);
  1422   1422         }
         1423  +      if( pOp->p3type==P3_MEM ){
         1424  +        sqlite3ValueFree((sqlite3_value*)pOp->p3);
         1425  +      }
  1423   1426       }
  1424   1427       sqliteFree(p->aOp);
  1425   1428     }
  1426   1429     releaseMemArray(p->aVar, p->nVar);
  1427   1430     sqliteFree(p->aLabel);
  1428   1431     sqliteFree(p->aStack);
  1429   1432     releaseMemArray(p->aColName, p->nResColumn*2);
................................................................................
  1843   1846   */
  1844   1847   void sqlite3ExpirePreparedStatements(sqlite3 *db){
  1845   1848     Vdbe *p;
  1846   1849     for(p = db->pVdbe; p; p=p->pNext){
  1847   1850       p->expired = 1;
  1848   1851     }
  1849   1852   }
         1853  +
         1854  +/*
         1855  +** Return the database associated with the Vdbe.
         1856  +*/
         1857  +sqlite3 *sqlite3VdbeDb(Vdbe *v){
         1858  +  return v->db;
         1859  +}

Changes to src/vdbemem.c.

   695    695     Mem *p = sqliteMalloc(sizeof(*p));
   696    696     if( p ){
   697    697       p->flags = MEM_Null;
   698    698       p->type = SQLITE_NULL;
   699    699     }
   700    700     return p;
   701    701   }
          702  +
          703  +/*
          704  +** Create a new sqlite3_value object, containing the value of pExpr.
          705  +**
          706  +** This only works for very simple expressions that consist of one constant
          707  +** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
          708  +** be converted directly into a value, then the value is allocated and
          709  +** a pointer written to *ppVal. The caller is responsible for deallocating
          710  +** the value by passing it to sqlite3ValueFree() later on. If the expression
          711  +** cannot be converted to a value, then *ppVal is set to NULL.
          712  +*/
          713  +int sqlite3ValueFromExpr(
          714  +  Expr *pExpr, 
          715  +  u8 enc, 
          716  +  u8 affinity,
          717  +  sqlite3_value **ppVal
          718  +){
          719  +  int op;
          720  +  char *zVal = 0;
          721  +  sqlite3_value *pVal = 0;
          722  +
          723  +  if( !pExpr ){
          724  +    *ppVal = 0;
          725  +    return SQLITE_OK;
          726  +  }
          727  +  op = pExpr->op;
          728  +
          729  +  if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
          730  +    zVal = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
          731  +    pVal = sqlite3ValueNew();
          732  +    if( !zVal || !pVal ) goto no_mem;
          733  +    sqlite3Dequote(zVal);
          734  +    sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX);
          735  +    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
          736  +      sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
          737  +    }else{
          738  +      sqlite3ValueApplyAffinity(pVal, affinity, enc);
          739  +    }
          740  +  }else if( op==TK_UMINUS ) {
          741  +    if( SQLITE_OK==sqlite3ValueFromExpr(pExpr->pLeft, enc, affinity, &pVal) ){
          742  +      pVal->i = -1 * pVal->i;
          743  +      pVal->r = -1.0 * pVal->r;
          744  +    }
          745  +  }
          746  +#ifndef SQLITE_OMIT_BLOB_LITERAL
          747  +  else if( op==TK_BLOB ){
          748  +    int nVal;
          749  +    pVal = sqlite3ValueNew();
          750  +    zVal = sqliteStrNDup(pExpr->token.z+1, pExpr->token.n-1);
          751  +    if( !zVal || !pVal ) goto no_mem;
          752  +    sqlite3Dequote(zVal);
          753  +    nVal = strlen(zVal)/2;
          754  +    sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(zVal), nVal, 0, sqlite3FreeX);
          755  +    sqliteFree(zVal);
          756  +  }
          757  +#endif
          758  +
          759  +  *ppVal = pVal;
          760  +  return SQLITE_OK;
          761  +
          762  +no_mem:
          763  +  sqliteFree(zVal);
          764  +  sqlite3ValueFree(pVal);
          765  +  *ppVal = 0;
          766  +  return SQLITE_NOMEM;
          767  +}
   702    768   
   703    769   /*
   704    770   ** Change the string value of an sqlite3_value object
   705    771   */
   706    772   void sqlite3ValueSetStr(
   707    773     sqlite3_value *v, 
   708    774     int n, 

Changes to test/alter2.test.

     9      9   #
    10     10   #*************************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script is testing that SQLite can handle a subtle 
    13     13   # file format change that may be used in the future to implement
    14     14   # "ALTER TABLE ... ADD COLUMN".
    15     15   #
    16         -# $Id: alter2.test,v 1.2 2005/02/26 18:10:45 drh Exp $
           16  +# $Id: alter2.test,v 1.3 2005/03/09 12:26:51 danielk1977 Exp $
    17     17   #
    18     18   
    19     19   set testdir [file dirname $argv0]
    20     20   source $testdir/tester.tcl
    21     21   
    22     22   # We have to have pragmas in order to do this test
    23     23   ifcapable {!pragma} return
................................................................................
    53     53     set bt [btree_open $fname 10 0]
    54     54     set meta [btree_get_meta $bt]
    55     55     btree_close $bt
    56     56     lindex $meta 2
    57     57   }
    58     58   
    59     59   # This procedure sets the SQL statement stored for table $tbl in the
    60         -# sqlite_master table of file 'test.db' to $sql.
           60  +# sqlite_master table of file 'test.db' to $sql. Also set the file format
           61  +# to the supplied value. This is 2 if the added column has a default that is
           62  +# NULL, or 3 otherwise. 
    61     63   #
    62         -proc alter_table {tbl sql} {
           64  +proc alter_table {tbl sql {file_format 2}} {
    63     65     sqlite3 dbat test.db
    64     66     dbat eval {
    65     67       PRAGMA writable_schema = 1;
    66     68       UPDATE sqlite_master SET sql = $sql WHERE name = $tbl AND type = 'table';
    67     69       PRAGMA writable_schema = 0;
    68     70     }
    69     71     dbat close
................................................................................
   218    220         SELECT * FROM clog;
   219    221       }
   220    222     } {{} 2 {} 6 {} 10}
   221    223   }
   222    224   
   223    225   #---------------------------------------------------------------------
   224    226   # Check that an error occurs if the database is upgraded to a file
   225         -# format that SQLite does not support (in this case 3). Note: The 
          227  +# format that SQLite does not support (in this case 4). Note: The 
   226    228   # file format is checked each time the schema is read, so changing the
   227    229   # file format requires incrementing the schema cookie.
   228    230   #
   229    231   do_test alter2-4.1 {
   230         -  set_file_format 3
          232  +  set_file_format 4
   231    233   } {}
   232    234   do_test alter2-4.2 {
   233    235     catchsql {
   234    236       SELECT * FROM sqlite_master;
   235    237     }
   236    238   } {1 {unsupported file format}}
   237    239   do_test alter2-4.3 {
................................................................................
   286    288   } {1}
   287    289   do_test alter2-6.3 {
   288    290     execsql {
   289    291       CREATE TABLE t1(a, b);
   290    292     }
   291    293     get_file_format 
   292    294   } {2}
          295  +
          296  +#---------------------------------------------------------------------
          297  +# Test that types and values for columns added with default values 
          298  +# other than NULL work with SELECT statements.
          299  +#
          300  +do_test alter2-7.1 {
          301  +  execsql {
          302  +    DROP TABLE t1;
          303  +    CREATE TABLE t1(a);
          304  +    INSERT INTO t1 VALUES(1);
          305  +    INSERT INTO t1 VALUES(2);
          306  +    INSERT INTO t1 VALUES(3);
          307  +    INSERT INTO t1 VALUES(4);
          308  +    SELECT * FROM t1;
          309  +  }
          310  +} {1 2 3 4}
          311  +do_test alter2-7.2 {
          312  +  set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')}
          313  +  alter_table t1 $sql 3
          314  +  execsql {
          315  +    SELECT * FROM t1 LIMIT 1;
          316  +  }
          317  +} {1 123 123}
          318  +do_test alter2-7.3 {
          319  +  execsql {
          320  +    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
          321  +  }
          322  +} {1 integer 123 text 123 integer}
          323  +do_test alter2-7.4 {
          324  +  execsql {
          325  +    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
          326  +  }
          327  +} {1 integer 123 text 123 integer}
          328  +do_test alter2-7.5 {
          329  +  set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)}
          330  +  alter_table t1 $sql 3
          331  +  execsql {
          332  +    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
          333  +  }
          334  +} {1 integer -123.0 real 5 text}
          335  +
          336  +#-----------------------------------------------------------------------
          337  +# Test that UPDATE trigger tables work with default values, and that when
          338  +# a row is updated the default values are correctly transfered to the 
          339  +# new row.
          340  +# 
          341  +ifcapable trigger {
          342  +db function set_val {set ::val}
          343  +  do_test alter2-8.1 {
          344  +    execsql {
          345  +      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
          346  +      SELECT set_val(
          347  +          old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '||
          348  +          new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c) 
          349  +      );
          350  +      END;
          351  +    }
          352  +    list
          353  +  } {}
          354  +}
          355  +do_test alter2-8.2 {
          356  +  execsql {
          357  +    UPDATE t1 SET c = 10 WHERE a = 1;
          358  +    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
          359  +  }
          360  +} {1 integer -123.0 real 10 text}
          361  +ifcapable trigger {
          362  +  do_test alter2-8.3 {
          363  +    set ::val
          364  +  } {-123 real 5 text -123 real 10 text}
          365  +}
          366  +
          367  +#-----------------------------------------------------------------------
          368  +# Test that DELETE trigger tables work with default values, and that when
          369  +# a row is updated the default values are correctly transfered to the 
          370  +# new row.
          371  +# 
          372  +ifcapable trigger {
          373  +  do_test alter2-9.1 {
          374  +    execsql {
          375  +      CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN
          376  +      SELECT set_val(
          377  +          old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)
          378  +      );
          379  +      END;
          380  +    }
          381  +    list
          382  +  } {}
          383  +  do_test alter2-9.2 {
          384  +    execsql {
          385  +      DELETE FROM t1 WHERE a = 2;
          386  +    }
          387  +    set ::val
          388  +  } {-123 real 5 text}
          389  +}
          390  +
          391  +#-----------------------------------------------------------------------
          392  +# Test creating an index on a column added with a default value. 
          393  +#
          394  +do_test alter2-10.1 {
          395  +  execsql {
          396  +    CREATE TABLE t2(a);
          397  +    INSERT INTO t2 VALUES('a');
          398  +    INSERT INTO t2 VALUES('b');
          399  +    INSERT INTO t2 VALUES('c');
          400  +    INSERT INTO t2 VALUES('d');
          401  +  }
          402  +  alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3
          403  +  catchsql {
          404  +    SELECT * FROM sqlite_master;
          405  +  }
          406  +  execsql {
          407  +    SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1;
          408  +  }
          409  +} {'a' X'ABCD' NULL}
          410  +do_test alter2-10.2 {
          411  +  execsql {
          412  +    CREATE INDEX i1 ON t2(b);
          413  +    SELECT a FROM t2 WHERE b = X'ABCD';
          414  +  }
          415  +} {a b c d}
          416  +do_test alter2-10.3 {
          417  +  execsql {
          418  +    DELETE FROM t2 WHERE a = 'c';
          419  +    SELECT a FROM t2 WHERE b = X'ABCD';
          420  +  }
          421  +} {a b d}
          422  +do_test alter2-10.4 {
          423  +  execsql {
          424  +    SELECT count(b) FROM t2 WHERE b = X'ABCD';
          425  +  }
          426  +} {3}
   293    427   
   294    428   finish_test
          429  +

Changes to test/capi3.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     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.  The
    12     12   # focus of this script testing the callback-free C/C++ API.
    13     13   #
    14         -# $Id: capi3.test,v 1.30 2005/02/19 08:18:06 danielk1977 Exp $
           14  +# $Id: capi3.test,v 1.31 2005/03/09 12:26:51 danielk1977 Exp $
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20     20   # Return the UTF-16 representation of the supplied UTF-8 string $str.
    21     21   # If $nt is true, append two 0x00 bytes as a nul terminator.
................................................................................
   483    483   if {![sqlite3 -has-codec]} {
   484    484     # Test what happens when the library encounters a newer file format.
   485    485     # Do this by updating the file format via the btree layer.
   486    486     do_test capi3-7.1 {
   487    487       set ::bt [btree_open test.db 10 0]
   488    488       btree_begin_transaction $::bt
   489    489       set meta [btree_get_meta $::bt]
   490         -    lset meta 2 3
          490  +    lset meta 2 4
   491    491       eval [concat btree_update_meta $::bt [lrange $meta 0 end]]
   492    492       btree_commit $::bt
   493    493       btree_close $::bt
   494    494     } {}
   495    495     do_test capi3-7.2 {
   496    496       sqlite3 db test.db
   497    497       catchsql {

Changes to www/opcode.tcl.

     1      1   #
     2      2   # Run this Tcl script to generate the sqlite.html file.
     3      3   #
     4         -set rcsid {$Id: opcode.tcl,v 1.14 2004/10/10 17:24:55 drh Exp $}
            4  +set rcsid {$Id: opcode.tcl,v 1.15 2005/03/09 12:26:51 danielk1977 Exp $}
     5      5   source common.tcl
     6      6   header {SQLite Virtual Machine Opcodes}
     7      7   puts {
     8      8   <h2>SQLite Virtual Machine Opcodes</h2>
     9      9   }
    10     10   
    11     11   set fd [open [lindex $argv 0] r]
................................................................................
    16     16     set line [string trim $line]
    17     17     if {[string index $line 1]!="*"} {
    18     18       set current_op {}
    19     19       continue
    20     20     }
    21     21     if {[regexp {^/\* Opcode: } $line]} {
    22     22       set current_op [lindex $line 2]
    23         -    set Opcode($current_op:args) [lrange $line 3 end]
           23  +    set txt [lrange $line 3 end]
           24  +    regsub -all {>} $txt {\&gt;} txt
           25  +    regsub -all {<} $txt {\&lt;} txt
           26  +    set Opcode($current_op:args) $txt
    24     27       lappend OpcodeList $current_op
    25     28       continue
    26     29     }
    27     30     if {$current_op==""} continue
    28     31     if {[regexp {^\*/} $line]} {
    29     32       set current_op {}
    30     33       continue
    31     34     }
    32     35     set line [string trim [string range $line 3 end]]
    33     36     if {$line==""} {
    34     37       append Opcode($current_op:text) \n<p>
    35     38     } else {
           39  +    regsub -all {>} $line {\&gt;} line
           40  +    regsub -all {<} $line {\&lt;} line
    36     41       append Opcode($current_op:text) \n$line
    37     42     }
    38     43   }
    39     44   unset file
    40     45   
    41     46   puts {
    42     47   <h3>Introduction</h3>