/ Check-in [7cb1c3ba]
Login

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

Overview
Comment:Simplifications to the Expr object: Remove Expr.span completely and convert Expr.token into a char* Expr.zToken. Also simplify the Token object by removing the Token.dyn and Token.quoted fields. (CVS 6681)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:7cb1c3ba0759539cb035978fdaff6316775986f3
User & Date: drh 2009-05-27 10:31:29
Context
2009-05-28
01:00
Additional refinements to Expr handling. Restore compression of trigger expressions. Change Expr.zToken to Expr.u.zToken and added Expr.u.iValue. Remove an unnecessary ExprDup from CHECK constraint processing. And so forth. (CVS 6682) check-in: 4ac2bdfb user: drh tags: trunk
2009-05-27
10:31
Simplifications to the Expr object: Remove Expr.span completely and convert Expr.token into a char* Expr.zToken. Also simplify the Token object by removing the Token.dyn and Token.quoted fields. (CVS 6681) check-in: 7cb1c3ba user: drh tags: trunk
2009-05-25
14:17
Fix the rtree test module so that it works even if the ext/ subfolder is omitted from the tree. (CVS 6679) check-in: 086206e1 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.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 used to generate VDBE code
    13     13   ** that implements the ALTER TABLE command.
    14     14   **
    15         -** $Id: alter.c,v 1.58 2009/05/12 17:46:54 drh Exp $
           15  +** $Id: alter.c,v 1.59 2009/05/27 10:31:29 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** The code in this file only exists if we are not omitting the
    21     21   ** ALTER TABLE logic from the build.
    22     22   */
................................................................................
    62     62       do {
    63     63         if( !*zCsr ){
    64     64           /* Ran out of input before finding an opening bracket. Return NULL. */
    65     65           return;
    66     66         }
    67     67   
    68     68         /* Store the token that zCsr points to in tname. */
    69         -      tname.z = zCsr;
           69  +      tname.z = (char*)zCsr;
    70     70         tname.n = len;
    71     71   
    72     72         /* Advance zCsr to the next token. Store that token type in 'token',
    73     73         ** and its length in 'len' (to be used next iteration of this loop).
    74     74         */
    75     75         do {
    76     76           zCsr += len;
    77     77           len = sqlite3GetToken(zCsr, &token);
    78     78         } while( token==TK_SPACE );
    79     79         assert( len>0 );
    80     80       } while( token!=TK_LP && token!=TK_USING );
    81     81   
    82         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
           82  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
    83     83          zTableName, tname.z+tname.n);
    84     84       sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
    85     85     }
    86     86   }
    87     87   
    88     88   #ifndef SQLITE_OMIT_TRIGGER
    89     89   /* This function is used by SQL generated to implement the
................................................................................
   121    121   
   122    122         if( !*zCsr ){
   123    123           /* Ran out of input before finding the table name. Return NULL. */
   124    124           return;
   125    125         }
   126    126   
   127    127         /* Store the token that zCsr points to in tname. */
   128         -      tname.z = zCsr;
          128  +      tname.z = (char*)zCsr;
   129    129         tname.n = len;
   130    130   
   131    131         /* Advance zCsr to the next token. Store that token type in 'token',
   132    132         ** and its length in 'len' (to be used next iteration of this loop).
   133    133         */
   134    134         do {
   135    135           zCsr += len;
................................................................................
   151    151           dist = 0;
   152    152         }
   153    153       } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
   154    154   
   155    155       /* Variable tname now contains the token that is the old table-name
   156    156       ** in the CREATE TRIGGER statement.
   157    157       */
   158         -    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
          158  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, 
   159    159          zTableName, tname.z+tname.n);
   160    160       sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
   161    161     }
   162    162   }
   163    163   #endif   /* !SQLITE_OMIT_TRIGGER */
   164    164   
   165    165   /*
................................................................................
   632    632     memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
   633    633     for(i=0; i<pNew->nCol; i++){
   634    634       Column *pCol = &pNew->aCol[i];
   635    635       pCol->zName = sqlite3DbStrDup(db, pCol->zName);
   636    636       pCol->zColl = 0;
   637    637       pCol->zType = 0;
   638    638       pCol->pDflt = 0;
          639  +    pCol->zDflt = 0;
   639    640     }
   640    641     pNew->pSchema = db->aDb[iDb].pSchema;
   641    642     pNew->addColOffset = pTab->addColOffset;
   642    643     pNew->nRef = 1;
   643    644   
   644    645     /* Begin a transaction and increment the schema cookie.  */
   645    646     sqlite3BeginWriteOperation(pParse, 0, iDb);

Changes to src/attach.c.

     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   ** This file contains code used to implement the ATTACH and DETACH commands.
    13     13   **
    14         -** $Id: attach.c,v 1.90 2009/05/01 06:19:21 danielk1977 Exp $
           14  +** $Id: attach.c,v 1.91 2009/05/27 10:31:29 drh Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   #ifndef SQLITE_OMIT_ATTACH
    19     19   /*
    20     20   ** Resolve an expression that was part of an ATTACH or DETACH statement. This
    21     21   ** is slightly different from resolving a normal SQL expression, because simple
................................................................................
    37     37   static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
    38     38   {
    39     39     int rc = SQLITE_OK;
    40     40     if( pExpr ){
    41     41       if( pExpr->op!=TK_ID ){
    42     42         rc = sqlite3ResolveExprNames(pName, pExpr);
    43     43         if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
    44         -        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span);
           44  +        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->zToken);
    45     45           return SQLITE_ERROR;
    46     46         }
    47     47       }else{
    48     48         pExpr->op = TK_STRING;
    49     49       }
    50     50     }
    51     51     return rc;
................................................................................
   294    294   ){
   295    295     int rc;
   296    296     NameContext sName;
   297    297     Vdbe *v;
   298    298     sqlite3* db = pParse->db;
   299    299     int regArgs;
   300    300   
   301         -#ifndef SQLITE_OMIT_AUTHORIZATION
   302         -  assert( db->mallocFailed || pAuthArg );
   303         -  if( pAuthArg ){
   304         -    char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span);
   305         -    if( !zAuthArg ){
   306         -      goto attach_end;
   307         -    }
   308         -    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
   309         -    sqlite3DbFree(db, zAuthArg);
   310         -    if(rc!=SQLITE_OK ){
   311         -      goto attach_end;
   312         -    }
   313         -  }
   314         -#endif /* SQLITE_OMIT_AUTHORIZATION */
   315         -
   316    301     memset(&sName, 0, sizeof(NameContext));
   317    302     sName.pParse = pParse;
   318    303   
   319    304     if( 
   320    305         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
   321    306         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
   322    307         SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
   323    308     ){
   324    309       pParse->nErr++;
   325    310       goto attach_end;
   326    311     }
          312  +
          313  +#ifndef SQLITE_OMIT_AUTHORIZATION
          314  +  if( pAuthArg ){
          315  +    char *zAuthArg = pAuthArg->zToken;
          316  +    if( zAuthArg==0 ){
          317  +      goto attach_end;
          318  +    }
          319  +    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
          320  +    if(rc!=SQLITE_OK ){
          321  +      goto attach_end;
          322  +    }
          323  +  }
          324  +#endif /* SQLITE_OMIT_AUTHORIZATION */
          325  +
   327    326   
   328    327     v = sqlite3GetVdbe(pParse);
   329    328     regArgs = sqlite3GetTempRange(pParse, 4);
   330    329     sqlite3ExprCode(pParse, pFilename, regArgs);
   331    330     sqlite3ExprCode(pParse, pDbname, regArgs+1);
   332    331     sqlite3ExprCode(pParse, pKey, regArgs+2);
   333    332   
................................................................................
   483    482     return 0;
   484    483   }
   485    484   int sqlite3FixExpr(
   486    485     DbFixer *pFix,     /* Context of the fixation */
   487    486     Expr *pExpr        /* The expression to be fixed to one database */
   488    487   ){
   489    488     while( pExpr ){
   490         -    if( ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_SpanToken) ) break;
          489  +    if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break;
   491    490       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   492    491         if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
   493    492       }else{
   494    493         if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
   495    494       }
   496    495       if( sqlite3FixExpr(pFix, pExpr->pRight) ){
   497    496         return 1;

Changes to src/build.c.

    18     18   **     CREATE INDEX
    19     19   **     DROP INDEX
    20     20   **     creating ID lists
    21     21   **     BEGIN TRANSACTION
    22     22   **     COMMIT
    23     23   **     ROLLBACK
    24     24   **
    25         -** $Id: build.c,v 1.544 2009/05/13 22:58:29 drh Exp $
           25  +** $Id: build.c,v 1.545 2009/05/27 10:31:29 drh Exp $
    26     26   */
    27     27   #include "sqliteInt.h"
    28     28   
    29     29   /*
    30     30   ** This routine is called when a new SQL statement is beginning to
    31     31   ** be parsed.  Initialize the pParse structure as needed.
    32     32   */
................................................................................
   466    466     sqlite3 *db = pTable->dbMem;
   467    467     testcase( db==0 );
   468    468     assert( pTable!=0 );
   469    469     if( (pCol = pTable->aCol)!=0 ){
   470    470       for(i=0; i<pTable->nCol; i++, pCol++){
   471    471         sqlite3DbFree(db, pCol->zName);
   472    472         sqlite3ExprDelete(db, pCol->pDflt);
          473  +      sqlite3DbFree(db, pCol->zDflt);
   473    474         sqlite3DbFree(db, pCol->zType);
   474    475         sqlite3DbFree(db, pCol->zColl);
   475    476       }
   476    477       sqlite3DbFree(db, pTable->aCol);
   477    478     }
   478    479     pTable->aCol = 0;
   479    480     pTable->nCol = 0;
................................................................................
   564    565   ** are not \000 terminated and are not persistent.  The returned string
   565    566   ** is \000 terminated and is persistent.
   566    567   */
   567    568   char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
   568    569     char *zName;
   569    570     if( pName ){
   570    571       zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
   571         -    if( pName->quoted ) sqlite3Dequote(zName);
          572  +    sqlite3Dequote(zName);
   572    573     }else{
   573    574       zName = 0;
   574    575     }
   575    576     return zName;
   576    577   }
   577    578   
   578    579   /*
................................................................................
   998    999   ** 'REAL'        | SQLITE_AFF_REAL
   999   1000   ** 'FLOA'        | SQLITE_AFF_REAL
  1000   1001   ** 'DOUB'        | SQLITE_AFF_REAL
  1001   1002   **
  1002   1003   ** If none of the substrings in the above table are found,
  1003   1004   ** SQLITE_AFF_NUMERIC is returned.
  1004   1005   */
  1005         -char sqlite3AffinityType(const Token *pType){
         1006  +char sqlite3AffinityType(const char *zIn){
  1006   1007     u32 h = 0;
  1007   1008     char aff = SQLITE_AFF_NUMERIC;
  1008         -  const unsigned char *zIn = pType->z;
  1009         -  const unsigned char *zEnd = &pType->z[pType->n];
  1010   1009   
  1011         -  while( zIn!=zEnd ){
  1012         -    h = (h<<8) + sqlite3UpperToLower[*zIn];
         1010  +  if( zIn ) while( zIn[0] ){
         1011  +    h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
  1013   1012       zIn++;
  1014   1013       if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
  1015   1014         aff = SQLITE_AFF_TEXT; 
  1016   1015       }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
  1017   1016         aff = SQLITE_AFF_TEXT;
  1018   1017       }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
  1019   1018         aff = SQLITE_AFF_TEXT;
................................................................................
  1054   1053     Column *pCol;
  1055   1054   
  1056   1055     p = pParse->pNewTable;
  1057   1056     if( p==0 || NEVER(p->nCol<1) ) return;
  1058   1057     pCol = &p->aCol[p->nCol-1];
  1059   1058     assert( pCol->zType==0 );
  1060   1059     pCol->zType = sqlite3NameFromToken(pParse->db, pType);
  1061         -  pCol->affinity = sqlite3AffinityType(pType);
         1060  +  pCol->affinity = sqlite3AffinityType(pCol->zType);
  1062   1061   }
  1063   1062   
  1064   1063   /*
  1065   1064   ** The expression is the default value for the most recently added column
  1066   1065   ** of the table currently under construction.
  1067   1066   **
  1068   1067   ** Default value expressions must be constant.  Raise an exception if this
  1069   1068   ** is not the case.
  1070   1069   **
  1071   1070   ** This routine is called by the parser while in the middle of
  1072   1071   ** parsing a CREATE TABLE statement.
  1073   1072   */
  1074         -void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){
         1073  +void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
  1075   1074     Table *p;
  1076   1075     Column *pCol;
  1077   1076     sqlite3 *db = pParse->db;
  1078   1077     p = pParse->pNewTable;
  1079   1078     if( p!=0 ){
  1080   1079       pCol = &(p->aCol[p->nCol-1]);
  1081         -    if( !sqlite3ExprIsConstantOrFunction(pExpr) ){
         1080  +    if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
  1082   1081         sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
  1083   1082             pCol->zName);
  1084   1083       }else{
  1085   1084         /* A copy of pExpr is used instead of the original, as pExpr contains
  1086   1085         ** tokens that point to volatile memory. The 'span' of the expression
  1087   1086         ** is required by pragma table_info.
  1088   1087         */
  1089   1088         sqlite3ExprDelete(db, pCol->pDflt);
  1090         -      pCol->pDflt = sqlite3ExprDup(db, pExpr, EXPRDUP_REDUCE|EXPRDUP_SPAN);
         1089  +      pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
         1090  +      sqlite3DbFree(db, pCol->zDflt);
         1091  +      pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
         1092  +                                     pSpan->zEnd - pSpan->zStart);
  1091   1093       }
  1092   1094     }
  1093         -  sqlite3ExprDelete(db, pExpr);
         1095  +  sqlite3ExprDelete(db, pSpan->pExpr);
  1094   1096   }
  1095   1097   
  1096   1098   /*
  1097   1099   ** Designate the PRIMARY KEY for the table.  pList is a list of names 
  1098   1100   ** of columns that form the primary key.  If pList is NULL, then the
  1099   1101   ** most recently added column of the table is the primary key.
  1100   1102   **
................................................................................
  1395   1397       testcase( pCol->affinity==SQLITE_AFF_NONE );
  1396   1398       testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
  1397   1399       testcase( pCol->affinity==SQLITE_AFF_INTEGER );
  1398   1400       testcase( pCol->affinity==SQLITE_AFF_REAL );
  1399   1401       
  1400   1402       zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
  1401   1403       len = sqlite3Strlen30(zType);
  1402         -#ifndef NDEBUG
  1403         -    if( pCol->affinity!=SQLITE_AFF_NONE ){
  1404         -       Token typeToken;
  1405         -       typeToken.z = (u8*)zType;
  1406         -       typeToken.n = len;
  1407         -       assert( pCol->affinity==sqlite3AffinityType(&typeToken) );
  1408         -    }
  1409         -#endif
         1404  +    assert( pCol->affinity==SQLITE_AFF_NONE 
         1405  +            || pCol->affinity==sqlite3AffinityType(zType) );
  1410   1406       memcpy(&zStmt[k], zType, len);
  1411   1407       k += len;
  1412   1408       assert( k<=n );
  1413   1409     }
  1414   1410     sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
  1415   1411     return zStmt;
  1416   1412   }
................................................................................
  1648   1644     Token *pName2,     /* The token that holds the name of the view */
  1649   1645     Select *pSelect,   /* A SELECT statement that will become the new view */
  1650   1646     int isTemp,        /* TRUE for a TEMPORARY view */
  1651   1647     int noErr          /* Suppress error messages if VIEW already exists */
  1652   1648   ){
  1653   1649     Table *p;
  1654   1650     int n;
  1655         -  const unsigned char *z;
         1651  +  const char *z;
  1656   1652     Token sEnd;
  1657   1653     DbFixer sFix;
  1658   1654     Token *pName;
  1659   1655     int iDb;
  1660   1656     sqlite3 *db = pParse->db;
  1661   1657   
  1662   1658     if( pParse->nVar>0 ){
................................................................................
  1700   1696     */
  1701   1697     sEnd = pParse->sLastToken;
  1702   1698     if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
  1703   1699       sEnd.z += sEnd.n;
  1704   1700     }
  1705   1701     sEnd.n = 0;
  1706   1702     n = (int)(sEnd.z - pBegin->z);
  1707         -  z = (const unsigned char*)pBegin->z;
         1703  +  z = pBegin->z;
  1708   1704     while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
  1709   1705     sEnd.z = &z[n-1];
  1710   1706     sEnd.n = 1;
  1711   1707   
  1712   1708     /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
  1713   1709     sqlite3EndTable(pParse, 0, &sEnd, 0);
  1714   1710     return;
................................................................................
  2504   2500   #endif
  2505   2501   
  2506   2502     /* If pList==0, it means this routine was called to make a primary
  2507   2503     ** key out of the last column added to the table under construction.
  2508   2504     ** So create a fake list to simulate this.
  2509   2505     */
  2510   2506     if( pList==0 ){
  2511         -    nullId.z = (u8*)pTab->aCol[pTab->nCol-1].zName;
         2507  +    nullId.z = pTab->aCol[pTab->nCol-1].zName;
  2512   2508       nullId.n = sqlite3Strlen30((char*)nullId.z);
  2513         -    nullId.quoted = 0;
  2514         -    pList = sqlite3ExprListAppend(pParse, 0, 0, &nullId);
         2509  +    pList = sqlite3ExprListAppend(pParse, 0, 0);
  2515   2510       if( pList==0 ) goto exit_create_index;
         2511  +    sqlite3ExprListSetName(pParse, pList, &nullId, 0);
  2516   2512       pList->a[0].sortOrder = (u8)sortOrder;
  2517   2513     }
  2518   2514   
  2519   2515     /* Figure out how many bytes of space are required to store explicitly
  2520   2516     ** specified collation sequence names.
  2521   2517     */
  2522   2518     for(i=0; i<pList->nExpr; i++){
................................................................................
  3077   3073     /* Return a pointer to the enlarged SrcList */
  3078   3074     return pSrc;
  3079   3075   }
  3080   3076   
  3081   3077   
  3082   3078   /*
  3083   3079   ** Append a new table name to the given SrcList.  Create a new SrcList if
  3084         -** need be.  A new entry is created in the SrcList even if pToken is NULL.
         3080  +** need be.  A new entry is created in the SrcList even if pTable is NULL.
  3085   3081   **
  3086   3082   ** A SrcList is returned, or NULL if there is an OOM error.  The returned
  3087   3083   ** SrcList might be the same as the SrcList that was input or it might be
  3088   3084   ** a new one.  If an OOM error does occurs, then the prior value of pList
  3089   3085   ** that is input to this routine is automatically freed.
  3090   3086   **
  3091   3087   ** If pDatabase is not null, it means that the table has an optional
................................................................................
  3105   3101   **
  3106   3102   **         sqlite3SrcListAppend(D,A,B,C);
  3107   3103   **
  3108   3104   ** Then C is the table name and B is the database name.  If C is defined
  3109   3105   ** then so is B.  In other words, we never have a case where:
  3110   3106   **
  3111   3107   **         sqlite3SrcListAppend(D,A,0,C);
         3108  +**
         3109  +** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
         3110  +** before being added to the SrcList.
  3112   3111   */
  3113   3112   SrcList *sqlite3SrcListAppend(
  3114   3113     sqlite3 *db,        /* Connection to notify of malloc failures */
  3115   3114     SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
  3116   3115     Token *pTable,      /* Table to append */
  3117   3116     Token *pDatabase    /* Database of the table */
  3118   3117   ){

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.201 2009/05/01 21:13:37 drh Exp $
           15  +** $Id: delete.c,v 1.202 2009/05/27 10:31:29 drh 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.
................................................................................
    78     78     SelectDest dest;
    79     79     Select *pDup;
    80     80     sqlite3 *db = pParse->db;
    81     81   
    82     82     pDup = sqlite3SelectDup(db, pView->pSelect, 0);
    83     83     if( pWhere ){
    84     84       SrcList *pFrom;
    85         -    Token viewName;
    86     85       
    87     86       pWhere = sqlite3ExprDup(db, pWhere, 0);
    88         -    viewName.z = (u8*)pView->zName;
    89         -    viewName.n = (unsigned int)sqlite3Strlen30((const char*)viewName.z);
    90         -    viewName.quoted = 0;
    91         -    pFrom = sqlite3SrcListAppendFromTerm(pParse, 0, 0, 0, &viewName, pDup, 0,0);
           87  +    pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
           88  +    if( pFrom ){
           89  +      assert( pFrom->nSrc==1 );
           90  +      pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName);
           91  +      pFrom->a[0].pSelect = pDup;
           92  +      assert( pFrom->a[0].pOn==0 );
           93  +      assert( pFrom->a[0].pUsing==0 );
           94  +    }else{
           95  +      sqlite3SelectDelete(db, pDup);
           96  +    }
    92     97       pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
    93     98     }
    94     99     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
    95    100     sqlite3Select(pParse, pDup, &dest);
    96    101     sqlite3SelectDelete(db, pDup);
    97    102   }
    98    103   #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
................................................................................
   144    149     **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
   145    150     ** becomes:
   146    151     **   DELETE FROM table_a WHERE rowid IN ( 
   147    152     **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
   148    153     **   );
   149    154     */
   150    155   
   151         -  pSelectRowid = sqlite3Expr(pParse->db, TK_ROW, 0, 0, 0);
          156  +  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
   152    157     if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
   153         -  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid, 0);
          158  +  pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
   154    159     if( pEList == 0 ) goto limit_where_cleanup_2;
   155    160   
   156    161     /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
   157    162     ** and the SELECT subtree. */
   158    163     pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
   159    164     if( pSelectSrc == 0 ) {
   160    165       sqlite3ExprListDelete(pParse->db, pEList);
................................................................................
   163    168   
   164    169     /* generate the SELECT expression tree. */
   165    170     pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
   166    171                                pOrderBy,0,pLimit,pOffset);
   167    172     if( pSelect == 0 ) return 0;
   168    173   
   169    174     /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
   170         -  pWhereRowid = sqlite3Expr(pParse->db, TK_ROW, 0, 0, 0);
          175  +  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
   171    176     if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
   172    177     pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
   173    178     if( pInClause == 0 ) goto limit_where_cleanup_1;
   174    179   
   175    180     pInClause->x.pSelect = pSelect;
   176    181     pInClause->flags |= EP_xIsSelect;
   177    182     sqlite3ExprSetHeight(pParse, pInClause);

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.436 2009/05/25 11:46:29 drh Exp $
           15  +** $Id: expr.c,v 1.437 2009/05/27 10:31:29 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** Return the 'affinity' of the expression pExpr if any.
    21     21   **
    22     22   ** If pExpr is a column, a reference to a column via an 'AS' alias,
................................................................................
    36     36     int op = pExpr->op;
    37     37     if( op==TK_SELECT ){
    38     38       assert( pExpr->flags&EP_xIsSelect );
    39     39       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
    40     40     }
    41     41   #ifndef SQLITE_OMIT_CAST
    42     42     if( op==TK_CAST ){
    43         -    return sqlite3AffinityType(&pExpr->token);
           43  +    return sqlite3AffinityType(pExpr->zToken);
    44     44     }
    45     45   #endif
    46     46     if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 
    47     47      && pExpr->pTab!=0
    48     48     ){
    49     49       /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
    50     50       ** a TK_COLUMN but was previously evaluated and cached in a register */
................................................................................
   372    372     return nHeight;
   373    373   }
   374    374   #else
   375    375     #define exprSetHeight(y)
   376    376   #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
   377    377   
   378    378   /*
          379  +** This routine is the core allocator for Expr nodes.
          380  +**
   379    381   ** Construct a new expression node and return a pointer to it.  Memory
   380         -** for this node is obtained from sqlite3_malloc().  The calling function
          382  +** for this node and for the pToken argument is a single allocation
          383  +** obtained from sqlite3DbMalloc().  The calling function
   381    384   ** is responsible for making sure the node eventually gets freed.
          385  +**
          386  +** If dequote is true, then the token (if it exists) is dequoted.
          387  +** If dequote is false, no dequoting is performance.  The deQuote
          388  +** parameter is ignored if pToken is NULL or if the token does not
          389  +** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
          390  +** then the EP_DblQuoted flag is set on the expression node.
   382    391   */
   383         -Expr *sqlite3Expr(
          392  +Expr *sqlite3ExprAlloc(
   384    393     sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
   385    394     int op,                 /* Expression opcode */
   386         -  Expr *pLeft,            /* Left operand */
   387         -  Expr *pRight,           /* Right operand */
   388         -  const Token *pToken     /* Argument token */
          395  +  const Token *pToken,    /* Token argument.  Might be NULL */
          396  +  int dequote             /* True to dequote */
   389    397   ){
   390    398     Expr *pNew;
   391         -  pNew = sqlite3DbMallocZero(db, sizeof(Expr));
   392         -  if( pNew==0 ){
   393         -    /* When malloc fails, delete pLeft and pRight. Expressions passed to 
   394         -    ** this function must always be allocated with sqlite3Expr() for this 
   395         -    ** reason. 
   396         -    */
   397         -    sqlite3ExprDelete(db, pLeft);
   398         -    sqlite3ExprDelete(db, pRight);
   399         -    return 0;
   400         -  }
   401         -  pNew->op = (u8)op;
   402         -  pNew->pLeft = pLeft;
   403         -  pNew->pRight = pRight;
   404         -  pNew->iAgg = -1;
   405         -  pNew->span.z = (u8*)"";
          399  +  int nExtra;
          400  +
   406    401     if( pToken ){
   407         -    int c;
   408         -    assert( pToken->dyn==0 );
   409         -    pNew->span = *pToken;
   410         -    if( pToken->n>=2 
   411         -         && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
   412         -      sqlite3TokenCopy(db, &pNew->token, pToken);
   413         -      if( pNew->token.z ){
   414         -        pNew->token.n = sqlite3Dequote((char*)pNew->token.z);
   415         -        assert( pNew->token.n==(unsigned)sqlite3Strlen30((char*)pNew->token.z));
   416         -      }
   417         -      if( c=='"' ) pNew->flags |= EP_DblQuoted;
   418         -    }else{
   419         -      pNew->token = *pToken;
   420         -    }
   421         -    pNew->token.quoted = 0;
   422         -  }else if( pLeft ){
   423         -    if( pRight ){
   424         -      if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){
   425         -        sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
   426         -      }
   427         -      if( pRight->flags & EP_ExpCollate ){
   428         -        pNew->flags |= EP_ExpCollate;
   429         -        pNew->pColl = pRight->pColl;
   430         -      }
   431         -    }
   432         -    if( pLeft->flags & EP_ExpCollate ){
   433         -      pNew->flags |= EP_ExpCollate;
   434         -      pNew->pColl = pLeft->pColl;
   435         -    }
   436         -  }
   437         -
   438         -  exprSetHeight(pNew);
          402  +    nExtra = pToken->n+1;
          403  +  }else{
          404  +    nExtra = 0;
          405  +  }
          406  +  pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
          407  +  if( pNew ){
          408  +    pNew->op = (u8)op;
          409  +    pNew->iAgg = -1;
          410  +    if( pToken ){
          411  +      int c;
          412  +      pNew->zToken = (char*)&pNew[1];
          413  +      memcpy(pNew->zToken, pToken->z, pToken->n);
          414  +      pNew->zToken[pToken->n] = 0;
          415  +      if( dequote && nExtra>=3 
          416  +           && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
          417  +        sqlite3Dequote(pNew->zToken);
          418  +        if( c=='"' ) pNew->flags |= EP_DblQuoted;
          419  +      }
          420  +    }
          421  +#if SQLITE_MAX_EXPR_DEPTH>0
          422  +    pNew->nHeight = 1;
          423  +#endif  
          424  +  }
   439    425     return pNew;
   440    426   }
   441    427   
   442    428   /*
          429  +** Allocate a new expression node from a zero-terminated token that has
          430  +** already been dequoted.
          431  +*/
          432  +Expr *sqlite3Expr(
          433  +  sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
          434  +  int op,                 /* Expression opcode */
          435  +  const char *zToken      /* Token argument.  Might be NULL */
          436  +){
          437  +  Token x;
          438  +  x.z = zToken;
          439  +  x.n = zToken ? sqlite3Strlen30(zToken) : 0;
          440  +  return sqlite3ExprAlloc(db, op, &x, 0);
          441  +}
          442  +
          443  +/*
          444  +** Attach subtrees pLeft and pRight to the Expr node pRoot.
          445  +**
          446  +** If pRoot==NULL that means that a memory allocation error has occurred.
          447  +** In that case, delete the subtrees pLeft and pRight.
          448  +*/
          449  +void sqlite3ExprAttachSubtrees(
          450  +  sqlite3 *db,
          451  +  Expr *pRoot,
          452  +  Expr *pLeft,
          453  +  Expr *pRight
          454  +){
          455  +  if( pRoot==0 ){
          456  +    assert( db->mallocFailed );
          457  +    sqlite3ExprDelete(db, pLeft);
          458  +    sqlite3ExprDelete(db, pRight);
          459  +  }else{
          460  +    if( pRight ){
          461  +      pRoot->pRight = pRight;
          462  +      if( pRight->flags & EP_ExpCollate ){
          463  +        pRoot->flags |= EP_ExpCollate;
          464  +        pRoot->pColl = pRight->pColl;
          465  +      }
          466  +    }
          467  +    if( pLeft ){
          468  +      pRoot->pLeft = pLeft;
          469  +      if( pLeft->flags & EP_ExpCollate ){
          470  +        pRoot->flags |= EP_ExpCollate;
          471  +        pRoot->pColl = pLeft->pColl;
          472  +      }
          473  +    }
          474  +    exprSetHeight(pRoot);
          475  +  }
          476  +}
          477  +
          478  +/*
          479  +** Allocate a Expr node which joins up to two subtrees.
          480  +**
          481  +** The 
   443    482   ** Works like sqlite3Expr() except that it takes an extra Parse*
   444    483   ** argument and notifies the associated connection object if malloc fails.
   445    484   */
   446    485   Expr *sqlite3PExpr(
   447    486     Parse *pParse,          /* Parsing context */
   448    487     int op,                 /* Expression opcode */
   449    488     Expr *pLeft,            /* Left operand */
   450    489     Expr *pRight,           /* Right operand */
   451    490     const Token *pToken     /* Argument token */
   452    491   ){
   453         -  Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
   454         -  if( p ){
   455         -    sqlite3ExprCheckHeight(pParse, p->nHeight);
   456         -  }
   457         -  return p;
   458         -}
   459         -
   460         -/*
   461         -** When doing a nested parse, you can include terms in an expression
   462         -** that look like this:   #1 #2 ...  These terms refer to registers
   463         -** in the virtual machine.  #N is the N-th register.
   464         -**
   465         -** This routine is called by the parser to deal with on of those terms.
   466         -** It immediately generates code to store the value in a memory location.
   467         -** The returns an expression that will code to extract the value from
   468         -** that memory location as needed.
   469         -*/
   470         -Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
   471         -  Expr *p;
   472         -  if( pParse->nested==0 ){
   473         -    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
   474         -    return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
   475         -  }
   476         -  p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
   477         -  if( p==0 ){
   478         -    return 0;  /* Malloc failed */
   479         -  }
   480         -  p->iTable = atoi((char*)&pToken->z[1]);
          492  +  Expr *p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
          493  +  sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
   481    494     return p;
   482    495   }
   483    496   
   484    497   /*
   485    498   ** Join two expressions using an AND operator.  If either expression is
   486    499   ** NULL, then just return the other expression.
   487    500   */
   488    501   Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
   489    502     if( pLeft==0 ){
   490    503       return pRight;
   491    504     }else if( pRight==0 ){
   492    505       return pLeft;
   493    506     }else{
   494         -    return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
   495         -  }
   496         -}
   497         -
   498         -/*
   499         -** Set the Expr.span field of the given expression to span all
   500         -** text between the two given tokens.  Both tokens must be pointing
   501         -** at the same string.
   502         -*/
   503         -void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
   504         -  assert( pRight!=0 );
   505         -  assert( pLeft!=0 );
   506         -  if( pExpr ){
   507         -    pExpr->span.z = pLeft->z;
   508         -    /* The following assert() may fail when this is called 
   509         -    ** via sqlite3PExpr()/sqlite3Expr() from addWhereTerm(). */
   510         -    /* assert(pRight->z >= pLeft->z); */
   511         -    pExpr->span.n = pRight->n + (unsigned)(pRight->z - pLeft->z);
          507  +    Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
          508  +    sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
          509  +    return pNew;
   512    510     }
   513    511   }
   514    512   
   515    513   /*
   516    514   ** Construct a new expression node for a function with multiple
   517    515   ** arguments.
   518    516   */
   519    517   Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
   520    518     Expr *pNew;
   521    519     sqlite3 *db = pParse->db;
   522    520     assert( pToken );
   523         -  pNew = sqlite3DbMallocZero(db, sizeof(Expr) );
          521  +  pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
   524    522     if( pNew==0 ){
   525    523       sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
   526    524       return 0;
   527    525     }
   528         -  pNew->op = TK_FUNCTION;
   529    526     pNew->x.pList = pList;
   530    527     assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   531         -  assert( pToken->dyn==0 );
   532         -  pNew->span = *pToken;
   533         -  sqlite3TokenCopy(db, &pNew->token, pToken);
   534    528     sqlite3ExprSetHeight(pParse, pNew);
   535    529     return pNew;
   536    530   }
   537    531   
   538    532   /*
   539    533   ** Assign a variable number to an expression that encodes a wildcard
   540    534   ** in the original SQL statement.  
................................................................................
   548    542   **
   549    543   ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
   550    544   ** as the previous instance of the same wildcard.  Or if this is the first
   551    545   ** instance of the wildcard, the next sequenial variable number is
   552    546   ** assigned.
   553    547   */
   554    548   void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
   555         -  Token *pToken;
   556    549     sqlite3 *db = pParse->db;
          550  +  const char *z;
   557    551   
   558    552     if( pExpr==0 ) return;
   559         -  pToken = &pExpr->token;
   560         -  assert( pToken->n>=1 );
   561         -  assert( pToken->z!=0 );
   562         -  assert( pToken->z[0]!=0 );
   563         -  if( pToken->n==1 ){
          553  +  z = pExpr->zToken;
          554  +  assert( z!=0 );
          555  +  assert( z[0]!=0 );
          556  +  if( z[1]==0 ){
   564    557       /* Wildcard of the form "?".  Assign the next variable number */
          558  +    assert( z[0]=='?' );
   565    559       pExpr->iTable = ++pParse->nVar;
   566         -  }else if( pToken->z[0]=='?' ){
          560  +  }else if( z[0]=='?' ){
   567    561       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
   568    562       ** use it as the variable number */
   569    563       int i;
   570         -    pExpr->iTable = i = atoi((char*)&pToken->z[1]);
          564  +    pExpr->iTable = i = atoi((char*)&z[1]);
   571    565       testcase( i==0 );
   572    566       testcase( i==1 );
   573    567       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
   574    568       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
   575    569       if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
   576    570         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
   577    571             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
................................................................................
   582    576     }else{
   583    577       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
   584    578       ** number as the prior appearance of the same name, or if the name
   585    579       ** has never appeared before, reuse the same variable number
   586    580       */
   587    581       int i;
   588    582       u32 n;
   589         -    n = pToken->n;
          583  +    n = sqlite3Strlen30(z);
   590    584       for(i=0; i<pParse->nVarExpr; i++){
   591    585         Expr *pE = pParse->apVarExpr[i];
   592    586         assert( pE!=0 );
   593         -      if( pE->token.n==n && memcmp(pE->token.z, pToken->z, n)==0 ){
          587  +      if( memcmp(pE->zToken, z, n)==0 && pE->zToken[n]==0 ){
   594    588           pExpr->iTable = pE->iTable;
   595    589           break;
   596    590         }
   597    591       }
   598    592       if( i>=pParse->nVarExpr ){
   599    593         pExpr->iTable = ++pParse->nVar;
   600    594         if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
................................................................................
   618    612   }
   619    613   
   620    614   /*
   621    615   ** Clear an expression structure without deleting the structure itself.
   622    616   ** Substructure is deleted.
   623    617   */
   624    618   void sqlite3ExprClear(sqlite3 *db, Expr *p){
   625         -  if( p->token.dyn ) sqlite3DbFree(db, (char*)p->token.z);
   626         -  if( !ExprHasAnyProperty(p, EP_TokenOnly|EP_SpanToken) ){
   627         -    if( p->span.dyn ) sqlite3DbFree(db, (char*)p->span.z);
          619  +  if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
   628    620       if( ExprHasProperty(p, EP_Reduced) ){
   629    621         /* Subtrees are part of the same memory allocation when EP_Reduced set */
   630    622         if( p->pLeft ) sqlite3ExprClear(db, p->pLeft);
   631    623         if( p->pRight ) sqlite3ExprClear(db, p->pRight);
   632    624       }else{
   633    625         /* Subtrees are separate allocations when EP_Reduced is clear */
   634    626         sqlite3ExprDelete(db, p->pLeft);
   635    627         sqlite3ExprDelete(db, p->pRight);
          628  +      /* Sometimes the zToken is allocated separately */
          629  +      if( p->flags2 & EP2_FreeToken ) sqlite3DbFree(db, p->zToken);
   636    630       }
   637    631       /* x.pSelect and x.pList are always separately allocated */
   638    632       if( ExprHasProperty(p, EP_xIsSelect) ){
   639    633         sqlite3SelectDelete(db, p->x.pSelect);
   640    634       }else{
   641    635         sqlite3ExprListDelete(db, p->x.pList);
   642    636       }
................................................................................
   655    649   /*
   656    650   ** Return the number of bytes allocated for the expression structure 
   657    651   ** passed as the first argument. This is always one of EXPR_FULLSIZE,
   658    652   ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
   659    653   */
   660    654   static int exprStructSize(Expr *p){
   661    655     if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
   662         -  if( ExprHasProperty(p, EP_SpanToken) ) return EXPR_SPANTOKENSIZE;
   663    656     if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
   664    657     return EXPR_FULLSIZE;
   665    658   }
   666    659   
   667    660   /*
   668    661   ** sqlite3ExprDup() has been called to create a copy of expression p with
   669    662   ** the EXPRDUP_XXX flags passed as the second argument. This function 
................................................................................
   672    665   */
   673    666   static int dupedExprStructSize(Expr *p, int flags){
   674    667     int nSize;
   675    668     if( 0==(flags&EXPRDUP_REDUCE) ){
   676    669       nSize = EXPR_FULLSIZE;
   677    670     }else if( p->pLeft || p->pRight || p->pColl || p->x.pList ){
   678    671       nSize = EXPR_REDUCEDSIZE;
   679         -  }else if( flags&EXPRDUP_SPAN ){
   680         -    nSize = EXPR_SPANTOKENSIZE;
   681    672     }else{
   682    673       nSize = EXPR_TOKENONLYSIZE;
   683    674     }
   684    675     return nSize;
   685    676   }
   686    677   
   687    678   /*
   688    679   ** sqlite3ExprDup() has been called to create a copy of expression p with
   689    680   ** the EXPRDUP_XXX passed as the second argument. This function returns
   690    681   ** the space in bytes required to store the copy of the Expr structure
   691         -** and the copies of the Expr.token.z and Expr.span.z (if applicable)
          682  +** and the copies of the Expr.zToken (if applicable)
   692    683   ** string buffers.
   693    684   */
   694    685   static int dupedExprNodeSize(Expr *p, int flags){
   695         -  int nByte = dupedExprStructSize(p, flags) + (p->token.z ? p->token.n + 1 : 0);
   696         -  if( (flags&EXPRDUP_SPAN)!=0
   697         -   && (p->token.z!=p->span.z || p->token.n!=p->span.n)
   698         -  ){
   699         -    nByte += p->span.n;
          686  +  int nByte = dupedExprStructSize(p, flags);
          687  +  if( p->zToken ){
          688  +    nByte += sqlite3Strlen30(p->zToken)+1;
   700    689     }
   701    690     return ROUND8(nByte);
   702    691   }
   703    692   
   704    693   /*
   705    694   ** Return the number of bytes required to create a duplicate of the 
   706    695   ** expression passed as the first argument. The second argument is a
   707    696   ** mask containing EXPRDUP_XXX flags.
   708    697   **
   709    698   ** The value returned includes space to create a copy of the Expr struct
   710         -** itself and the buffer referred to by Expr.token, if any. If the 
   711         -** EXPRDUP_SPAN flag is set, then space to create a copy of the buffer
   712         -** refered to by Expr.span is also included.
          699  +** itself and the buffer referred to by Expr.zToken, if any.
   713    700   **
   714    701   ** If the EXPRDUP_REDUCE flag is set, then the return value includes 
   715    702   ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 
   716    703   ** and Expr.pRight variables (but not for any structures pointed to or 
   717    704   ** descended from the Expr.x.pList or Expr.x.pSelect variables).
   718    705   */
   719    706   static int dupedExprSize(Expr *p, int flags){
   720    707     int nByte = 0;
   721    708     if( p ){
   722    709       nByte = dupedExprNodeSize(p, flags);
   723    710       if( flags&EXPRDUP_REDUCE ){
   724         -      int f = flags&(~EXPRDUP_SPAN);
   725         -      nByte += dupedExprSize(p->pLeft, f) + dupedExprSize(p->pRight, f);
          711  +      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
   726    712       }
   727    713     }
   728    714     return nByte;
   729    715   }
   730    716   
   731    717   /*
   732    718   ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
   733    719   ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
   734         -** to store the copy of expression p, the copies of p->token and p->span 
          720  +** to store the copy of expression p, the copies of p->zToken
   735    721   ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
   736    722   ** if any. Before returning, *pzBuffer is set to the first byte passed the
   737    723   ** portion of the buffer copied into by this function.
   738    724   */
   739    725   static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
   740    726     Expr *pNew = 0;                      /* Value to return */
   741    727     if( p ){
   742         -    const int isRequireSpan = (flags&EXPRDUP_SPAN);
   743    728       const int isReduced = (flags&EXPRDUP_REDUCE);
   744    729       u8 *zAlloc;
   745    730   
   746    731       assert( pzBuffer==0 || isReduced );
   747    732   
   748    733       /* Figure out where to write the new Expr structure. */
   749    734       if( pzBuffer ){
................................................................................
   753    738       }
   754    739       pNew = (Expr *)zAlloc;
   755    740   
   756    741       if( pNew ){
   757    742         /* Set nNewSize to the size allocated for the structure pointed to
   758    743         ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
   759    744         ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
   760         -      ** by the copy of the p->token.z string (if any).
          745  +      ** by the copy of the p->zToken string (if any).
   761    746         */
   762    747         const int nNewSize = dupedExprStructSize(p, flags);
   763         -      const int nToken = (p->token.z ? p->token.n + 1 : 0);
          748  +      const int nToken = (p->zToken ? sqlite3Strlen30(p->zToken) + 1 : 0);
   764    749         if( isReduced ){
   765    750           assert( ExprHasProperty(p, EP_Reduced)==0 );
   766    751           memcpy(zAlloc, p, nNewSize);
   767    752         }else{
   768    753           int nSize = exprStructSize(p);
   769    754           memcpy(zAlloc, p, nSize);
   770    755           memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
   771    756         }
   772    757   
   773    758         /* Set the EP_Reduced and EP_TokenOnly flags appropriately. */
   774         -      pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_SpanToken);
          759  +      pNew->flags &= ~(EP_Reduced|EP_TokenOnly);
   775    760         switch( nNewSize ){
   776    761           case EXPR_REDUCEDSIZE:   pNew->flags |= EP_Reduced; break;
   777    762           case EXPR_TOKENONLYSIZE: pNew->flags |= EP_TokenOnly; break;
   778         -        case EXPR_SPANTOKENSIZE: pNew->flags |= EP_SpanToken; break;
   779    763         }
   780    764   
   781         -      /* Copy the p->token string, if any. */
          765  +      /* Copy the p->zToken string, if any. */
   782    766         if( nToken ){
   783         -        unsigned char *zToken = &zAlloc[nNewSize];
   784         -        memcpy(zToken, p->token.z, nToken-1);
   785         -        zToken[nToken-1] = '\0';
   786         -        pNew->token.dyn = 0;
   787         -        pNew->token.z = zToken;
          767  +        char *zToken = pNew->zToken = (char*)&zAlloc[nNewSize];
          768  +        memcpy(zToken, p->zToken, nToken);
   788    769         }
   789    770   
   790    771         if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
   791         -        /* Fill in the pNew->span token, if required. */
   792         -        if( isRequireSpan ){
   793         -          if( p->token.z!=p->span.z || p->token.n!=p->span.n ){
   794         -            pNew->span.z = &zAlloc[nNewSize+nToken];
   795         -            memcpy((char *)pNew->span.z, p->span.z, p->span.n);
   796         -            pNew->span.dyn = 0;
   797         -          }else{
   798         -            pNew->span.z = pNew->token.z;
   799         -            pNew->span.n = pNew->token.n;
   800         -          }
   801         -        }else{
   802         -          pNew->span.z = 0;
   803         -          pNew->span.n = 0;
   804         -        }
   805         -      }
   806         -
   807         -      if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_SpanToken)) ){
   808    772           /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
   809    773           if( ExprHasProperty(p, EP_xIsSelect) ){
   810    774             pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
   811    775           }else{
   812    776             pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
   813    777           }
   814    778         }
   815    779   
   816    780         /* Fill in pNew->pLeft and pNew->pRight. */
   817         -      if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly|EP_SpanToken) ){
          781  +      if( ExprHasAnyProperty(pNew, EP_Reduced|EP_TokenOnly) ){
   818    782           zAlloc += dupedExprNodeSize(p, flags);
   819    783           if( ExprHasProperty(pNew, EP_Reduced) ){
   820    784             pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
   821    785             pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
   822    786           }
   823    787           if( pzBuffer ){
   824    788             *pzBuffer = zAlloc;
   825    789           }
   826         -      }else if( !ExprHasAnyProperty(p, EP_TokenOnly|EP_SpanToken) ){
   827         -        pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
   828         -        pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
          790  +      }else{
          791  +        pNew->flags2 = 0;
          792  +        if( !ExprHasAnyProperty(p, EP_TokenOnly) ){
          793  +          pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
          794  +          pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
          795  +        }
   829    796         }
          797  +
   830    798       }
   831    799     }
   832    800     return pNew;
   833    801   }
   834    802   
   835    803   /*
   836    804   ** The following group of routines make deep copies of expressions,
................................................................................
   840    808   **
   841    809   ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
   842    810   ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 
   843    811   ** by subsequent calls to sqlite*ListAppend() routines.
   844    812   **
   845    813   ** Any tables that the SrcList might point to are not duplicated.
   846    814   **
   847         -** The flags parameter contains a combination of the EXPRDUP_XXX flags. If
   848         -** the EXPRDUP_SPAN flag is set in the argument parameter, then the 
   849         -** Expr.span field of the input expression is copied. If EXPRDUP_SPAN is
   850         -** clear, then the Expr.span field of the returned expression structure
   851         -** is zeroed.
   852         -**
          815  +** The flags parameter contains a combination of the EXPRDUP_XXX flags.
   853    816   ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
   854    817   ** truncated version of the usual Expr structure that will be stored as
   855    818   ** part of the in-memory representation of the database schema.
   856    819   */
   857    820   Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
   858    821     return exprDup(db, p, flags, 0);
   859    822   }
   860         -void sqlite3TokenCopy(sqlite3 *db, Token *pTo, const Token *pFrom){
   861         -  if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z);
   862         -  if( pFrom->z ){
   863         -    pTo->n = pFrom->n;
   864         -    pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
   865         -    pTo->dyn = 1;
   866         -    pTo->quoted = pFrom->quoted;
   867         -  }else{
   868         -    pTo->z = 0;
   869         -  }
   870         -}
   871    823   ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
   872    824     ExprList *pNew;
   873    825     struct ExprList_item *pItem, *pOldItem;
   874    826     int i;
   875    827     if( p==0 ) return 0;
   876    828     pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
   877    829     if( pNew==0 ) return 0;
................................................................................
   884    836     } 
   885    837     pOldItem = p->a;
   886    838     for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
   887    839       Expr *pNewExpr;
   888    840       Expr *pOldExpr = pOldItem->pExpr;
   889    841       pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr, flags);
   890    842       pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
          843  +    pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
   891    844       pItem->sortOrder = pOldItem->sortOrder;
   892    845       pItem->done = 0;
   893    846       pItem->iCol = pOldItem->iCol;
   894    847       pItem->iAlias = pOldItem->iAlias;
   895    848     }
   896    849     return pNew;
   897    850   }
................................................................................
   958    911     return pNew;
   959    912   }
   960    913   Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
   961    914     Select *pNew;
   962    915     if( p==0 ) return 0;
   963    916     pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
   964    917     if( pNew==0 ) return 0;
   965         -  /* Always make a copy of the span for top-level expressions in the
   966         -  ** expression list.  The logic in SELECT processing that determines
   967         -  ** the names of columns in the result set needs this information */
   968         -  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags|EXPRDUP_SPAN);
          918  +  pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
   969    919     pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
   970    920     pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
   971    921     pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
   972    922     pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
   973    923     pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
   974    924     pNew->op = p->op;
   975    925     pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags);
................................................................................
   991    941   }
   992    942   #endif
   993    943   
   994    944   
   995    945   /*
   996    946   ** Add a new element to the end of an expression list.  If pList is
   997    947   ** initially NULL, then create a new expression list.
          948  +**
          949  +** If a memory allocation error occurs, the entire list is freed and
          950  +** NULL is returned.  If non-NULL is returned, then it is guaranteed
          951  +** that the new entry was successfully appended.
   998    952   */
   999    953   ExprList *sqlite3ExprListAppend(
  1000    954     Parse *pParse,          /* Parsing context */
  1001    955     ExprList *pList,        /* List to which to append. Might be NULL */
  1002         -  Expr *pExpr,            /* Expression to be appended */
  1003         -  Token *pName            /* AS keyword for the expression */
          956  +  Expr *pExpr             /* Expression to be appended. Might be NULL */
  1004    957   ){
  1005    958     sqlite3 *db = pParse->db;
  1006    959     if( pList==0 ){
  1007    960       pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
  1008    961       if( pList==0 ){
  1009    962         goto no_mem;
  1010    963       }
................................................................................
  1017    970       if( a==0 ){
  1018    971         goto no_mem;
  1019    972       }
  1020    973       pList->a = a;
  1021    974       pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
  1022    975     }
  1023    976     assert( pList->a!=0 );
  1024         -  if( pExpr || pName ){
          977  +  if( 1 ){
  1025    978       struct ExprList_item *pItem = &pList->a[pList->nExpr++];
  1026    979       memset(pItem, 0, sizeof(*pItem));
  1027         -    pItem->zName = sqlite3NameFromToken(db, pName);
  1028    980       pItem->pExpr = pExpr;
  1029         -    pItem->iAlias = 0;
  1030    981     }
  1031    982     return pList;
  1032    983   
  1033    984   no_mem:     
  1034    985     /* Avoid leaking memory if malloc has failed. */
  1035    986     sqlite3ExprDelete(db, pExpr);
  1036    987     sqlite3ExprListDelete(db, pList);
  1037    988     return 0;
  1038    989   }
          990  +
          991  +/*
          992  +** Set the ExprList.a[].zName element of the most recently added item
          993  +** on the expression list.
          994  +**
          995  +** pList might be NULL following an OOM error.  But pName should never be
          996  +** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
          997  +** is set.
          998  +*/
          999  +void sqlite3ExprListSetName(
         1000  +  Parse *pParse,          /* Parsing context */
         1001  +  ExprList *pList,        /* List to which to add the span. */
         1002  +  Token *pName,           /* Name to be added */
         1003  +  int dequote             /* True to cause the name to be dequoted */
         1004  +){
         1005  +  assert( pList!=0 || pParse->db->mallocFailed!=0 );
         1006  +  if( pList ){
         1007  +    struct ExprList_item *pItem;
         1008  +    assert( pList->nExpr>0 );
         1009  +    pItem = &pList->a[pList->nExpr-1];
         1010  +    assert( pItem->zName==0 );
         1011  +    pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
         1012  +    if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
         1013  +  }
         1014  +}
         1015  +
         1016  +/*
         1017  +** Set the ExprList.a[].zSpan element of the most recently added item
         1018  +** on the expression list.
         1019  +**
         1020  +** pList might be NULL following an OOM error.  But pSpan should never be
         1021  +** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
         1022  +** is set.
         1023  +*/
         1024  +void sqlite3ExprListSetSpan(
         1025  +  Parse *pParse,          /* Parsing context */
         1026  +  ExprList *pList,        /* List to which to add the span. */
         1027  +  ExprSpan *pSpan         /* The span to be added */
         1028  +){
         1029  +  sqlite3 *db = pParse->db;
         1030  +  assert( pList!=0 || db->mallocFailed!=0 );
         1031  +  if( pList ){
         1032  +    struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
         1033  +    assert( pList->nExpr>0 );
         1034  +    assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
         1035  +    sqlite3DbFree(db, pItem->zSpan);
         1036  +    pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
         1037  +                                    pSpan->zEnd - pSpan->zStart);
         1038  +  }
         1039  +}
  1039   1040   
  1040   1041   /*
  1041   1042   ** If the expression list pEList contains more than iLimit elements,
  1042   1043   ** leave an error message in pParse.
  1043   1044   */
  1044   1045   void sqlite3ExprListCheckLength(
  1045   1046     Parse *pParse,
................................................................................
  1062   1063     struct ExprList_item *pItem;
  1063   1064     if( pList==0 ) return;
  1064   1065     assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
  1065   1066     assert( pList->nExpr<=pList->nAlloc );
  1066   1067     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
  1067   1068       sqlite3ExprDelete(db, pItem->pExpr);
  1068   1069       sqlite3DbFree(db, pItem->zName);
         1070  +    sqlite3DbFree(db, pItem->zSpan);
  1069   1071     }
  1070   1072     sqlite3DbFree(db, pList->a);
  1071   1073     sqlite3DbFree(db, pList);
  1072   1074   }
  1073   1075   
  1074   1076   /*
  1075   1077   ** These routines are Walker callbacks.  Walker.u.pi is a pointer
................................................................................
  1179   1181     int rc = 0;
  1180   1182     if( p->flags & EP_IntValue ){
  1181   1183       *pValue = p->iTable;
  1182   1184       return 1;
  1183   1185     }
  1184   1186     switch( p->op ){
  1185   1187       case TK_INTEGER: {
  1186         -      rc = sqlite3GetInt32((char*)p->token.z, pValue);
         1188  +      rc = sqlite3GetInt32(p->zToken, pValue);
  1187   1189         break;
  1188   1190       }
  1189   1191       case TK_UPLUS: {
  1190   1192         rc = sqlite3ExprIsInteger(p->pLeft, pValue);
  1191   1193         break;
  1192   1194       }
  1193   1195       case TK_UMINUS: {
................................................................................
  1574   1576   
  1575   1577       case TK_EXISTS:
  1576   1578       case TK_SELECT: {
  1577   1579         /* This has to be a scalar SELECT.  Generate code to put the
  1578   1580         ** value of this select in a memory cell and record the number
  1579   1581         ** of the memory cell in iColumn.
  1580   1582         */
  1581         -      static const Token one = { (u8*)"1", 0, 0, 1 };
         1583  +      static const Token one = { "1", 1 };
  1582   1584         Select *pSel;
  1583   1585         SelectDest dest;
  1584   1586   
  1585   1587         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
  1586   1588         pSel = pExpr->x.pSelect;
  1587   1589         sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
  1588   1590         if( pExpr->op==TK_SELECT ){
................................................................................
  1628   1630   ** Generate an instruction that will put the floating point
  1629   1631   ** value described by z[0..n-1] into register iMem.
  1630   1632   **
  1631   1633   ** The z[] string will probably not be zero-terminated.  But the 
  1632   1634   ** z[n] character is guaranteed to be something that does not look
  1633   1635   ** like the continuation of the number.
  1634   1636   */
  1635         -static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
         1637  +static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
  1636   1638     assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
  1637         -  assert( !z || !sqlite3Isdigit(z[n]) );
  1638         -  UNUSED_PARAMETER(n);
  1639   1639     if( z ){
  1640   1640       double value;
  1641   1641       char *zV;
  1642   1642       sqlite3AtoF(z, &value);
  1643   1643       if( sqlite3IsNaN(value) ){
  1644   1644         sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
  1645   1645       }else{
................................................................................
  1661   1661   */
  1662   1662   static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
  1663   1663     const char *z;
  1664   1664     if( pExpr->flags & EP_IntValue ){
  1665   1665       int i = pExpr->iTable;
  1666   1666       if( negFlag ) i = -i;
  1667   1667       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
  1668         -  }else if( (z = (char*)pExpr->token.z)!=0 ){
         1668  +  }else if( (z = pExpr->zToken)!=0 ){
  1669   1669       int i;
  1670         -    int n = pExpr->token.n;
         1670  +    int n = sqlite3Strlen30(pExpr->zToken);
  1671   1671       assert( !sqlite3Isdigit(z[n]) );
  1672   1672       if( sqlite3GetInt32(z, &i) ){
  1673   1673         if( negFlag ) i = -i;
  1674   1674         sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
  1675   1675       }else if( sqlite3FitsIn64Bits(z, negFlag) ){
  1676   1676         i64 value;
  1677   1677         char *zV;
  1678   1678         sqlite3Atoi64(z, &value);
  1679   1679         if( negFlag ) value = -value;
  1680   1680         zV = dup8bytes(v, (char*)&value);
  1681   1681         sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
  1682   1682       }else{
  1683         -      codeReal(v, z, n, negFlag, iMem);
         1683  +      codeReal(v, z, negFlag, iMem);
  1684   1684       }
  1685   1685     }
  1686   1686   }
  1687   1687   
  1688   1688   /*
  1689   1689   ** Clear a cache entry.
  1690   1690   */
................................................................................
  2074   2074         break;
  2075   2075       }
  2076   2076       case TK_INTEGER: {
  2077   2077         codeInteger(v, pExpr, 0, target);
  2078   2078         break;
  2079   2079       }
  2080   2080       case TK_FLOAT: {
  2081         -      codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
         2081  +      codeReal(v, pExpr->zToken, 0, target);
  2082   2082         break;
  2083   2083       }
  2084   2084       case TK_STRING: {
  2085         -      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0,
  2086         -                        (char*)pExpr->token.z, pExpr->token.n);
         2085  +      sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->zToken, 0);
  2087   2086         break;
  2088   2087       }
  2089   2088       case TK_NULL: {
  2090   2089         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
  2091   2090         break;
  2092   2091       }
  2093   2092   #ifndef SQLITE_OMIT_BLOB_LITERAL
  2094   2093       case TK_BLOB: {
  2095   2094         int n;
  2096   2095         const char *z;
  2097   2096         char *zBlob;
  2098         -      assert( pExpr->token.n>=3 );
  2099         -      assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
  2100         -      assert( pExpr->token.z[1]=='\'' );
  2101         -      assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
  2102         -      n = pExpr->token.n - 3;
  2103         -      z = (char*)pExpr->token.z + 2;
         2097  +      assert( pExpr->zToken[0]=='x' || pExpr->zToken[0]=='X' );
         2098  +      assert( pExpr->zToken[1]=='\'' );
         2099  +      z = &pExpr->zToken[2];
         2100  +      n = sqlite3Strlen30(z) - 1;
         2101  +      assert( z[n]=='\'' );
  2104   2102         zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
  2105   2103         sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
  2106   2104         break;
  2107   2105       }
  2108   2106   #endif
  2109   2107       case TK_VARIABLE: {
  2110   2108         int iPrior;
  2111   2109         VdbeOp *pOp;
  2112         -      if( pExpr->token.n<=1
         2110  +      assert( pExpr->zToken!=0 );
         2111  +      assert( pExpr->zToken[0]!=0 );
         2112  +      if( pExpr->zToken[1]==0
  2113   2113            && (iPrior = sqlite3VdbeCurrentAddr(v)-1)>=0
  2114   2114            && (pOp = sqlite3VdbeGetOp(v, iPrior))->opcode==OP_Variable
  2115   2115            && pOp->p1+pOp->p3==pExpr->iTable
  2116   2116            && pOp->p2+pOp->p3==target
  2117   2117            && pOp->p4.z==0
  2118   2118         ){
  2119   2119           /* If the previous instruction was a copy of the previous unnamed
................................................................................
  2120   2120           ** parameter into the previous register, then simply increment the
  2121   2121           ** repeat count on the prior instruction rather than making a new
  2122   2122           ** instruction.
  2123   2123           */
  2124   2124           pOp->p3++;
  2125   2125         }else{
  2126   2126           sqlite3VdbeAddOp3(v, OP_Variable, pExpr->iTable, target, 1);
  2127         -        if( pExpr->token.n>1 ){
  2128         -          sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
         2127  +        if( pExpr->zToken[1]!=0 ){
         2128  +          sqlite3VdbeChangeP4(v, -1, pExpr->zToken, 0);
  2129   2129           }
  2130   2130         }
  2131   2131         break;
  2132   2132       }
  2133   2133       case TK_REGISTER: {
  2134   2134         inReg = pExpr->iTable;
  2135   2135         break;
................................................................................
  2139   2139         break;
  2140   2140       }
  2141   2141   #ifndef SQLITE_OMIT_CAST
  2142   2142       case TK_CAST: {
  2143   2143         /* Expressions of the form:   CAST(pLeft AS token) */
  2144   2144         int aff, to_op;
  2145   2145         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
  2146         -      aff = sqlite3AffinityType(&pExpr->token);
         2146  +      aff = sqlite3AffinityType(pExpr->zToken);
  2147   2147         to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
  2148   2148         assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
  2149   2149         assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
  2150   2150         assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
  2151   2151         assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
  2152   2152         assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
  2153   2153         testcase( to_op==OP_ToText );
................................................................................
  2232   2232         testcase( regFree2==0 );
  2233   2233         break;
  2234   2234       }
  2235   2235       case TK_UMINUS: {
  2236   2236         Expr *pLeft = pExpr->pLeft;
  2237   2237         assert( pLeft );
  2238   2238         if( pLeft->op==TK_FLOAT ){
  2239         -        codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target);
         2239  +        codeReal(v, pLeft->zToken, 1, target);
  2240   2240         }else if( pLeft->op==TK_INTEGER ){
  2241   2241           codeInteger(v, pLeft, 1, target);
  2242   2242         }else{
  2243   2243           regFree1 = r1 = sqlite3GetTempReg(pParse);
  2244   2244           sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
  2245   2245           r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
  2246   2246           sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
................................................................................
  2275   2275         sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
  2276   2276         sqlite3VdbeJumpHere(v, addr);
  2277   2277         break;
  2278   2278       }
  2279   2279       case TK_AGG_FUNCTION: {
  2280   2280         AggInfo *pInfo = pExpr->pAggInfo;
  2281   2281         if( pInfo==0 ){
  2282         -        sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
  2283         -            &pExpr->span);
         2282  +        sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->zToken);
  2284   2283         }else{
  2285   2284           inReg = pInfo->aFunc[pExpr->iAgg].iMem;
  2286   2285         }
  2287   2286         break;
  2288   2287       }
  2289   2288       case TK_CONST_FUNC:
  2290   2289       case TK_FUNCTION: {
................................................................................
  2297   2296         int i;                 /* Loop counter */
  2298   2297         u8 enc = ENC(db);      /* The text encoding used by this database */
  2299   2298         CollSeq *pColl = 0;    /* A collating sequence */
  2300   2299   
  2301   2300         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  2302   2301         testcase( op==TK_CONST_FUNC );
  2303   2302         testcase( op==TK_FUNCTION );
  2304         -      if( ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_SpanToken) ){
         2303  +      if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
  2305   2304           pFarg = 0;
  2306   2305         }else{
  2307   2306           pFarg = pExpr->x.pList;
  2308   2307         }
  2309   2308         nFarg = pFarg ? pFarg->nExpr : 0;
  2310         -      zId = (char*)pExpr->token.z;
  2311         -      nId = pExpr->token.n;
         2309  +      zId = pExpr->zToken;
         2310  +      nId = sqlite3Strlen30(zId);
  2312   2311         pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
  2313   2312         assert( pDef!=0 );
  2314   2313         if( pFarg ){
  2315   2314           r1 = sqlite3GetTempRange(pParse, nFarg);
  2316   2315           sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
  2317   2316         }else{
  2318   2317           r1 = 0;
................................................................................
  2587   2586           return 0;
  2588   2587         }
  2589   2588         if( pExpr->affinity!=OE_Ignore ){
  2590   2589            assert( pExpr->affinity==OE_Rollback ||
  2591   2590                    pExpr->affinity == OE_Abort ||
  2592   2591                    pExpr->affinity == OE_Fail );
  2593   2592            sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->affinity, 0,
  2594         -                        (char*)pExpr->token.z, pExpr->token.n);
         2593  +                           pExpr->zToken, 0);
  2595   2594         } else {
  2596   2595            assert( pExpr->affinity == OE_Ignore );
  2597   2596            sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
  2598   2597            sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
  2599   2598            VdbeComment((v, "raise(IGNORE)"));
  2600   2599         }
  2601   2600         break;
................................................................................
  3132   3131         if( !sqlite3ExprCompare(pExprA, pExprB) ) return 0;
  3133   3132       }
  3134   3133     }else if( pA->x.pList || pB->x.pList ){
  3135   3134       return 0;
  3136   3135     }
  3137   3136   
  3138   3137     if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
  3139         -  if( pA->op!=TK_COLUMN && pA->token.z ){
  3140         -    if( pB->token.z==0 ) return 0;
  3141         -    if( pB->token.n!=pA->token.n ) return 0;
  3142         -    if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
         3138  +  if( pA->op!=TK_COLUMN && pA->zToken ){
         3139  +    if( pB->zToken==0 ) return 0;
         3140  +    if( sqlite3StrICmp(pA->zToken,pB->zToken)!=0 ){
  3143   3141         return 0;
  3144   3142       }
  3145   3143     }
  3146   3144     return 1;
  3147   3145   }
  3148   3146   
  3149   3147   
................................................................................
  3283   3281             i = addAggInfoFunc(pParse->db, pAggInfo);
  3284   3282             if( i>=0 ){
  3285   3283               assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  3286   3284               pItem = &pAggInfo->aFunc[i];
  3287   3285               pItem->pExpr = pExpr;
  3288   3286               pItem->iMem = ++pParse->nMem;
  3289   3287               pItem->pFunc = sqlite3FindFunction(pParse->db,
  3290         -                   (char*)pExpr->token.z, pExpr->token.n,
         3288  +                   pExpr->zToken, sqlite3Strlen30(pExpr->zToken),
  3291   3289                      pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
  3292   3290               if( pExpr->flags & EP_Distinct ){
  3293   3291                 pItem->iDistinct = pParse->nTab++;
  3294   3292               }else{
  3295   3293                 pItem->iDistinct = -1;
  3296   3294               }
  3297   3295             }

Changes to src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.234 2009/04/20 12:07:37 drh Exp $
           19  +** $Id: func.c,v 1.235 2009/05/27 10:31:29 drh Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <stdlib.h>
    23     23   #include <assert.h>
    24     24   #include "vdbeInt.h"
    25     25   
    26     26   /*
................................................................................
  1364   1364     if( pExpr->op!=TK_FUNCTION 
  1365   1365      || !pExpr->x.pList 
  1366   1366      || pExpr->x.pList->nExpr!=2
  1367   1367     ){
  1368   1368       return 0;
  1369   1369     }
  1370   1370     assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  1371         -  pDef = sqlite3FindFunction(db, (char*)pExpr->token.z, pExpr->token.n, 2,
  1372         -                             SQLITE_UTF8, 0);
         1371  +  pDef = sqlite3FindFunction(db, pExpr->zToken, sqlite3Strlen30(pExpr->zToken),
         1372  +                             2, SQLITE_UTF8, 0);
  1373   1373     if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){
  1374   1374       return 0;
  1375   1375     }
  1376   1376   
  1377   1377     /* The memcpy() statement assumes that the wildcard characters are
  1378   1378     ** the first three statements in the compareInfo structure.  The
  1379   1379     ** asserts() that follow verify that assumption

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.276 2009/05/11 18:22:31 drh Exp $
           17  +** @(#) $Id: parse.y,v 1.277 2009/05/27 10:31:29 drh Exp $
    18     18   */
    19     19   
    20     20   // All token codes are small integers with #defines that begin with "TK_"
    21     21   %token_prefix TK_
    22     22   
    23     23   // The type of the data attached to each token is Token.  This is also the
    24     24   // default type for non-terminals.
................................................................................
   162    162   // CREATE TABLE statement.  This includes the column name, its
   163    163   // datatype, and other keywords such as PRIMARY KEY, UNIQUE, REFERENCES,
   164    164   // NOT NULL and so forth.
   165    165   //
   166    166   column(A) ::= columnid(X) type carglist. {
   167    167     A.z = X.z;
   168    168     A.n = (int)(pParse->sLastToken.z-X.z) + pParse->sLastToken.n;
   169         -  A.quoted = 0;
   170         -  A.dyn = 0;
   171    169   }
   172    170   columnid(A) ::= nm(X). {
   173    171     sqlite3AddColumn(pParse,&X);
   174    172     A = X;
   175    173   }
   176    174   
   177    175   
................................................................................
   260    258   // "carglist" is a list of additional constraints that come after the
   261    259   // column name and column type in a CREATE TABLE statement.
   262    260   //
   263    261   carglist ::= carglist carg.
   264    262   carglist ::= .
   265    263   carg ::= CONSTRAINT nm ccons.
   266    264   carg ::= ccons.
   267         -ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,X);}
   268         -ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,X);}
   269         -ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,X);}
          265  +ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,&X);}
          266  +ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,&X);}
          267  +ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,&X);}
   270    268   ccons ::= DEFAULT MINUS(A) term(X).      {
   271         -  Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0, 0);
   272         -  sqlite3ExprSpan(p,&A,&X->span);
   273         -  sqlite3AddDefaultValue(pParse,p);
          269  +  ExprSpan v;
          270  +  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0);
          271  +  v.zStart = A.z;
          272  +  v.zEnd = X.zEnd;
          273  +  sqlite3AddDefaultValue(pParse,&v);
   274    274   }
   275    275   ccons ::= DEFAULT id(X).              {
   276         -  Expr *p = sqlite3PExpr(pParse, TK_STRING, 0, 0, &X);
   277         -  sqlite3AddDefaultValue(pParse,p);
          276  +  ExprSpan v;
          277  +  spanExpr(&v, pParse, TK_STRING, &X);
          278  +  sqlite3AddDefaultValue(pParse,&v);
   278    279   }
   279    280   
   280    281   // In addition to the type name, we also care about the primary key and
   281    282   // UNIQUE constraints.
   282    283   //
   283    284   ccons ::= NULL onconf.
   284         -ccons ::= NOT NULL onconf(R).               {sqlite3AddNotNull(pParse, R);}
          285  +ccons ::= NOT NULL onconf(R).    {sqlite3AddNotNull(pParse, R);}
   285    286   ccons ::= PRIMARY KEY sortorder(Z) onconf(R) autoinc(I).
   286         -                                     {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
   287         -ccons ::= UNIQUE onconf(R).    {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
   288         -ccons ::= CHECK LP expr(X) RP.       {sqlite3AddCheckConstraint(pParse,X);}
          287  +                                 {sqlite3AddPrimaryKey(pParse,0,R,I,Z);}
          288  +ccons ::= UNIQUE onconf(R).      {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0,0,0);}
          289  +ccons ::= CHECK LP expr(X) RP.   {sqlite3AddCheckConstraint(pParse,X.pExpr);}
   289    290   ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
   290         -                                {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
   291         -ccons ::= defer_subclause(D).   {sqlite3DeferForeignKey(pParse,D);}
   292         -ccons ::= COLLATE ids(C).  {sqlite3AddCollateType(pParse, &C);}
          291  +                                 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
          292  +ccons ::= defer_subclause(D).    {sqlite3DeferForeignKey(pParse,D);}
          293  +ccons ::= COLLATE ids(C).        {sqlite3AddCollateType(pParse, &C);}
   293    294   
   294    295   // The optional AUTOINCREMENT keyword
   295    296   %type autoinc {int}
   296    297   autoinc(X) ::= .          {X = 0;}
   297    298   autoinc(X) ::= AUTOINCR.  {X = 1;}
   298    299   
   299    300   // The next group of rules parses the arguments to a REFERENCES clause
................................................................................
   328    329   conslist_opt(A) ::= .                   {A.n = 0; A.z = 0;}
   329    330   conslist_opt(A) ::= COMMA(X) conslist.  {A = X;}
   330    331   conslist ::= conslist COMMA tcons.
   331    332   conslist ::= conslist tcons.
   332    333   conslist ::= tcons.
   333    334   tcons ::= CONSTRAINT nm.
   334    335   tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
   335         -                                         {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
          336  +                                 {sqlite3AddPrimaryKey(pParse,X,R,I,0);}
   336    337   tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
   337    338                                    {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0,0,0);}
   338         -tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
          339  +tcons ::= CHECK LP expr(E) RP onconf.
          340  +                                 {sqlite3AddCheckConstraint(pParse,E.pExpr);}
   339    341   tcons ::= FOREIGN KEY LP idxlist(FA) RP
   340    342             REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
   341    343       sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
   342    344       sqlite3DeferForeignKey(pParse, D);
   343    345   }
   344    346   %type defer_subclause_opt {int}
   345    347   defer_subclause_opt(A) ::= .                    {A = 0;}
................................................................................
   429    431   %type selcollist {ExprList*}
   430    432   %destructor selcollist {sqlite3ExprListDelete(pParse->db, $$);}
   431    433   %type sclp {ExprList*}
   432    434   %destructor sclp {sqlite3ExprListDelete(pParse->db, $$);}
   433    435   sclp(A) ::= selcollist(X) COMMA.             {A = X;}
   434    436   sclp(A) ::= .                                {A = 0;}
   435    437   selcollist(A) ::= sclp(P) expr(X) as(Y).     {
   436         -   A = sqlite3ExprListAppend(pParse,P,X,Y.n?&Y:0);
          438  +   A = sqlite3ExprListAppend(pParse, P, X.pExpr);
          439  +   if( Y.n>0 ) sqlite3ExprListSetName(pParse, A, &Y, 1);
          440  +   sqlite3ExprListSetSpan(pParse,A,&X);
   437    441   }
   438    442   selcollist(A) ::= sclp(P) STAR. {
   439         -  Expr *p = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0);
   440         -  A = sqlite3ExprListAppend(pParse, P, p, 0);
          443  +  Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
          444  +  A = sqlite3ExprListAppend(pParse, P, p);
   441    445   }
   442    446   selcollist(A) ::= sclp(P) nm(X) DOT STAR(Y). {
   443    447     Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &Y);
   444    448     Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
   445    449     Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
   446         -  A = sqlite3ExprListAppend(pParse,P, pDot, 0);
          450  +  A = sqlite3ExprListAppend(pParse,P, pDot);
   447    451   }
   448    452   
   449    453   // An option "AS <id>" phrase that can follow one of the expressions that
   450    454   // define the result set, or one of the tables in the FROM clause.
   451    455   //
   452    456   %type as {Token}
   453    457   as(X) ::= AS nm(Y).    {X = Y;}
................................................................................
   526    530   joinop(X) ::= JOIN_KW(A) JOIN.         { X = sqlite3JoinType(pParse,&A,0,0); }
   527    531   joinop(X) ::= JOIN_KW(A) nm(B) JOIN.   { X = sqlite3JoinType(pParse,&A,&B,0); }
   528    532   joinop(X) ::= JOIN_KW(A) nm(B) nm(C) JOIN.
   529    533                                          { X = sqlite3JoinType(pParse,&A,&B,&C); }
   530    534   
   531    535   %type on_opt {Expr*}
   532    536   %destructor on_opt {sqlite3ExprDelete(pParse->db, $$);}
   533         -on_opt(N) ::= ON expr(E).   {N = E;}
          537  +on_opt(N) ::= ON expr(E).   {N = E.pExpr;}
   534    538   on_opt(N) ::= .             {N = 0;}
   535    539   
   536    540   // Note that this block abuses the Token type just a little. If there is
   537    541   // no "INDEXED BY" clause, the returned token is empty (z==0 && n==0). If
   538    542   // there is an INDEXED BY clause, then the token is populated as per normal,
   539    543   // with z pointing to the token data and n containing the number of bytes
   540    544   // in the token.
................................................................................
   560    564   %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
   561    565   %type sortitem {Expr*}
   562    566   %destructor sortitem {sqlite3ExprDelete(pParse->db, $$);}
   563    567   
   564    568   orderby_opt(A) ::= .                          {A = 0;}
   565    569   orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
   566    570   sortlist(A) ::= sortlist(X) COMMA sortitem(Y) sortorder(Z). {
   567         -  A = sqlite3ExprListAppend(pParse,X,Y,0);
          571  +  A = sqlite3ExprListAppend(pParse,X,Y);
   568    572     if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
   569    573   }
   570    574   sortlist(A) ::= sortitem(Y) sortorder(Z). {
   571         -  A = sqlite3ExprListAppend(pParse,0,Y,0);
          575  +  A = sqlite3ExprListAppend(pParse,0,Y);
   572    576     if( A && A->a ) A->a[0].sortOrder = (u8)Z;
   573    577   }
   574         -sortitem(A) ::= expr(X).   {A = X;}
          578  +sortitem(A) ::= expr(X).   {A = X.pExpr;}
   575    579   
   576    580   %type sortorder {int}
   577    581   
   578    582   sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
   579    583   sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
   580    584   sortorder(A) ::= .              {A = SQLITE_SO_ASC;}
   581    585   
................................................................................
   583    587   %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
   584    588   groupby_opt(A) ::= .                      {A = 0;}
   585    589   groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
   586    590   
   587    591   %type having_opt {Expr*}
   588    592   %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
   589    593   having_opt(A) ::= .                {A = 0;}
   590         -having_opt(A) ::= HAVING expr(X).  {A = X;}
          594  +having_opt(A) ::= HAVING expr(X).  {A = X.pExpr;}
   591    595   
   592    596   %type limit_opt {struct LimitVal}
   593    597   
   594    598   // The destructor for limit_opt will never fire in the current grammar.
   595    599   // The limit_opt non-terminal only occurs at the end of a single production
   596    600   // rule for SELECT statements.  As soon as the rule that create the 
   597    601   // limit_opt non-terminal reduces, the SELECT statement rule will also
................................................................................
   598    602   // reduce.  So there is never a limit_opt non-terminal on the stack 
   599    603   // except as a transient.  So there is never anything to destroy.
   600    604   //
   601    605   //%destructor limit_opt {
   602    606   //  sqlite3ExprDelete(pParse->db, $$.pLimit);
   603    607   //  sqlite3ExprDelete(pParse->db, $$.pOffset);
   604    608   //}
   605         -limit_opt(A) ::= .                     {A.pLimit = 0; A.pOffset = 0;}
   606         -limit_opt(A) ::= LIMIT expr(X).        {A.pLimit = X; A.pOffset = 0;}
          609  +limit_opt(A) ::= .                    {A.pLimit = 0; A.pOffset = 0;}
          610  +limit_opt(A) ::= LIMIT expr(X).       {A.pLimit = X.pExpr; A.pOffset = 0;}
   607    611   limit_opt(A) ::= LIMIT expr(X) OFFSET expr(Y). 
   608         -                                       {A.pLimit = X; A.pOffset = Y;}
          612  +                                      {A.pLimit = X.pExpr; A.pOffset = Y.pExpr;}
   609    613   limit_opt(A) ::= LIMIT expr(X) COMMA expr(Y). 
   610         -                                       {A.pOffset = X; A.pLimit = Y;}
          614  +                                      {A.pOffset = X.pExpr; A.pLimit = Y.pExpr;}
   611    615   
   612    616   /////////////////////////// The DELETE statement /////////////////////////////
   613    617   //
   614    618   %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   615    619   cmd ::= DELETE FROM fullname(X) indexed_opt(I) where_opt(W) 
   616    620           orderby_opt(O) limit_opt(L). {
   617    621     sqlite3SrcListIndexedBy(pParse, X, &I);
................................................................................
   626    630   }
   627    631   %endif
   628    632   
   629    633   %type where_opt {Expr*}
   630    634   %destructor where_opt {sqlite3ExprDelete(pParse->db, $$);}
   631    635   
   632    636   where_opt(A) ::= .                    {A = 0;}
   633         -where_opt(A) ::= WHERE expr(X).       {A = X;}
          637  +where_opt(A) ::= WHERE expr(X).       {A = X.pExpr;}
   634    638   
   635    639   ////////////////////////// The UPDATE command ////////////////////////////////
   636    640   //
   637    641   %ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
   638    642   cmd ::= UPDATE orconf(R) fullname(X) indexed_opt(I) SET setlist(Y) where_opt(W) orderby_opt(O) limit_opt(L).  {
   639    643     sqlite3SrcListIndexedBy(pParse, X, &I);
   640    644     sqlite3ExprListCheckLength(pParse,Y,"set list"); 
................................................................................
   649    653     sqlite3Update(pParse,X,Y,W,R);
   650    654   }
   651    655   %endif
   652    656   
   653    657   %type setlist {ExprList*}
   654    658   %destructor setlist {sqlite3ExprListDelete(pParse->db, $$);}
   655    659   
   656         -setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y).
   657         -    {A = sqlite3ExprListAppend(pParse,Z,Y,&X);}
   658         -setlist(A) ::= nm(X) EQ expr(Y).
   659         -    {A = sqlite3ExprListAppend(pParse,0,Y,&X);}
          660  +setlist(A) ::= setlist(Z) COMMA nm(X) EQ expr(Y). {
          661  +  A = sqlite3ExprListAppend(pParse, Z, Y.pExpr);
          662  +  sqlite3ExprListSetName(pParse, A, &X, 1);
          663  +}
          664  +setlist(A) ::= nm(X) EQ expr(Y). {
          665  +  A = sqlite3ExprListAppend(pParse, 0, Y.pExpr);
          666  +  sqlite3ExprListSetName(pParse, A, &X, 1);
          667  +}
   660    668   
   661    669   ////////////////////////// The INSERT command /////////////////////////////////
   662    670   //
   663    671   cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) 
   664    672           VALUES LP itemlist(Y) RP.
   665    673               {sqlite3Insert(pParse, X, Y, 0, F, R);}
   666    674   cmd ::= insert_cmd(R) INTO fullname(X) inscollist_opt(F) select(S).
................................................................................
   673    681   insert_cmd(A) ::= REPLACE.            {A = OE_Replace;}
   674    682   
   675    683   
   676    684   %type itemlist {ExprList*}
   677    685   %destructor itemlist {sqlite3ExprListDelete(pParse->db, $$);}
   678    686   
   679    687   itemlist(A) ::= itemlist(X) COMMA expr(Y).
   680         -    {A = sqlite3ExprListAppend(pParse,X,Y,0);}
          688  +    {A = sqlite3ExprListAppend(pParse,X,Y.pExpr);}
   681    689   itemlist(A) ::= expr(X).
   682         -    {A = sqlite3ExprListAppend(pParse,0,X,0);}
          690  +    {A = sqlite3ExprListAppend(pParse,0,X.pExpr);}
   683    691   
   684    692   %type inscollist_opt {IdList*}
   685    693   %destructor inscollist_opt {sqlite3IdListDelete(pParse->db, $$);}
   686    694   %type inscollist {IdList*}
   687    695   %destructor inscollist {sqlite3IdListDelete(pParse->db, $$);}
   688    696   
   689    697   inscollist_opt(A) ::= .                       {A = 0;}
................................................................................
   692    700       {A = sqlite3IdListAppend(pParse->db,X,&Y);}
   693    701   inscollist(A) ::= nm(Y).
   694    702       {A = sqlite3IdListAppend(pParse->db,0,&Y);}
   695    703   
   696    704   /////////////////////////// Expression Processing /////////////////////////////
   697    705   //
   698    706   
   699         -%type expr {Expr*}
   700         -%destructor expr {sqlite3ExprDelete(pParse->db, $$);}
   701         -%type term {Expr*}
   702         -%destructor term {sqlite3ExprDelete(pParse->db, $$);}
          707  +%type expr {ExprSpan}
          708  +%destructor expr {sqlite3ExprDelete(pParse->db, $$.pExpr);}
          709  +%type term {ExprSpan}
          710  +%destructor term {sqlite3ExprDelete(pParse->db, $$.pExpr);}
          711  +
          712  +%include {
          713  +  /* This is a utility routine used to set the ExprSpan.zStart and
          714  +  ** ExprSpan.zEnd values of pOut so that the span covers the complete
          715  +  ** range of text beginning with pStart and going to the end of pEnd.
          716  +  */
          717  +  static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
          718  +    pOut->zStart = pStart->z;
          719  +    pOut->zEnd = &pEnd->z[pEnd->n];
          720  +  }
          721  +
          722  +  /* Construct a new Expr object from a single identifier.  Use the
          723  +  ** new Expr to populate pOut.  Set the span of pOut to be the identifier
          724  +  ** that created the expression.
          725  +  */
          726  +  static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
          727  +    pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
          728  +    pOut->zStart = pValue->z;
          729  +    pOut->zEnd = &pValue->z[pValue->n];
          730  +  }
          731  +}
   703    732   
   704    733   expr(A) ::= term(X).             {A = X;}
   705         -expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
   706         -term(A) ::= NULL(X).             {A = sqlite3PExpr(pParse, @X, 0, 0, &X);}
   707         -expr(A) ::= id(X).               {A = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);}
   708         -expr(A) ::= JOIN_KW(X).          {A = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);}
          734  +expr(A) ::= LP(B) expr(X) RP(E). {A.pExpr = X.pExpr; spanSet(&A,&B,&E);}
          735  +term(A) ::= NULL(X).             {spanExpr(&A, pParse, @X, &X);}
          736  +expr(A) ::= id(X).               {spanExpr(&A, pParse, TK_ID, &X);}
          737  +expr(A) ::= JOIN_KW(X).          {spanExpr(&A, pParse, TK_ID, &X);}
   709    738   expr(A) ::= nm(X) DOT nm(Y). {
   710    739     Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
   711    740     Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
   712         -  A = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
          741  +  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
          742  +  spanSet(&A,&X,&Y);
   713    743   }
   714    744   expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
   715    745     Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
   716    746     Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Y);
   717    747     Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &Z);
   718    748     Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
   719         -  A = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
          749  +  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
          750  +  spanSet(&A,&X,&Z);
   720    751   }
   721         -term(A) ::= INTEGER|FLOAT|BLOB(X).  {A = sqlite3PExpr(pParse, @X, 0, 0, &X);}
   722         -term(A) ::= STRING(X).       {A = sqlite3PExpr(pParse, @X, 0, 0, &X);}
   723         -expr(A) ::= REGISTER(X).     {A = sqlite3RegisterExpr(pParse, &X);}
          752  +term(A) ::= INTEGER|FLOAT|BLOB(X).  {spanExpr(&A, pParse, @X, &X);}
          753  +term(A) ::= STRING(X).              {spanExpr(&A, pParse, @X, &X);}
          754  +expr(A) ::= REGISTER(X).     {
          755  +  /* When doing a nested parse, one can include terms in an expression
          756  +  ** that look like this:   #1 #2 ...  These terms refer to registers
          757  +  ** in the virtual machine.  #N is the N-th register. */
          758  +  if( pParse->nested==0 ){
          759  +    sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &X);
          760  +    A.pExpr = 0;
          761  +  }else{
          762  +    A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &X);
          763  +    if( A.pExpr ) sqlite3GetInt32(&X.z[1], &A.pExpr->iTable);
          764  +  }
          765  +  spanSet(&A, &X, &X);
          766  +}
   724    767   expr(A) ::= VARIABLE(X).     {
   725         -  Token *pToken = &X;
   726         -  Expr *pExpr = A = sqlite3PExpr(pParse, TK_VARIABLE, 0, 0, pToken);
   727         -  sqlite3ExprAssignVarNumber(pParse, pExpr);
          768  +  spanExpr(&A, pParse, TK_VARIABLE, &X);
          769  +  sqlite3ExprAssignVarNumber(pParse, A.pExpr);
          770  +  spanSet(&A, &X, &X);
   728    771   }
   729    772   expr(A) ::= expr(E) COLLATE ids(C). {
   730         -  A = sqlite3ExprSetColl(pParse, E, &C);
          773  +  A.pExpr = sqlite3ExprSetColl(pParse, E.pExpr, &C);
          774  +  A.zStart = E.zStart;
          775  +  A.zEnd = &C.z[C.n];
   731    776   }
   732    777   %ifndef SQLITE_OMIT_CAST
   733    778   expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
   734         -  A = sqlite3PExpr(pParse, TK_CAST, E, 0, &T);
   735         -  sqlite3ExprSpan(A,&X,&Y);
          779  +  A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
          780  +  spanSet(&A,&X,&Y);
   736    781   }
   737    782   %endif  SQLITE_OMIT_CAST
   738    783   expr(A) ::= ID(X) LP distinct(D) exprlist(Y) RP(E). {
   739    784     if( Y && Y->nExpr>SQLITE_MAX_FUNCTION_ARG ){
   740    785       sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
   741    786     }
   742         -  A = sqlite3ExprFunction(pParse, Y, &X);
   743         -  sqlite3ExprSpan(A,&X,&E);
   744         -  if( D && A ){
   745         -    A->flags |= EP_Distinct;
          787  +  A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
          788  +  spanSet(&A,&X,&E);
          789  +  if( D && A.pExpr ){
          790  +    A.pExpr->flags |= EP_Distinct;
   746    791     }
   747    792   }
   748    793   expr(A) ::= ID(X) LP STAR RP(E). {
   749         -  A = sqlite3ExprFunction(pParse, 0, &X);
   750         -  sqlite3ExprSpan(A,&X,&E);
          794  +  A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
          795  +  spanSet(&A,&X,&E);
   751    796   }
   752    797   term(A) ::= CTIME_KW(OP). {
   753    798     /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
   754    799     ** treated as functions that return constants */
   755         -  A = sqlite3ExprFunction(pParse, 0,&OP);
   756         -  if( A ){
   757         -    A->op = TK_CONST_FUNC;  
   758         -    A->span = OP;
          800  +  A.pExpr = sqlite3ExprFunction(pParse, 0,&OP);
          801  +  if( A.pExpr ){
          802  +    A.pExpr->op = TK_CONST_FUNC;  
          803  +  }
          804  +  spanSet(&A, &OP, &OP);
          805  +}
          806  +
          807  +%include {
          808  +  /* This routine constructs a binary expression node out of two ExprSpan
          809  +  ** objects and uses the result to populate a new ExprSpan object.
          810  +  */
          811  +  static void spanBinaryExpr(
          812  +    ExprSpan *pOut,     /* Write the result here */
          813  +    Parse *pParse,      /* The parsing context.  Errors accumulate here */
          814  +    int op,             /* The binary operation */
          815  +    ExprSpan *pLeft,    /* The left operand */
          816  +    ExprSpan *pRight    /* The right operand */
          817  +  ){
          818  +    pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
          819  +    pOut->zStart = pLeft->zStart;
          820  +    pOut->zEnd = pRight->zEnd;
   759    821     }
   760    822   }
   761         -expr(A) ::= expr(X) AND(OP) expr(Y).       {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
   762         -expr(A) ::= expr(X) OR(OP) expr(Y).        {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
          823  +
          824  +expr(A) ::= expr(X) AND(OP) expr(Y).    {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
          825  +expr(A) ::= expr(X) OR(OP) expr(Y).     {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   763    826   expr(A) ::= expr(X) LT|GT|GE|LE(OP) expr(Y).
   764         -                                           {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
   765         -expr(A) ::= expr(X) EQ|NE(OP) expr(Y).     {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
          827  +                                        {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
          828  +expr(A) ::= expr(X) EQ|NE(OP) expr(Y).  {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   766    829   expr(A) ::= expr(X) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
   767         -                                           {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
   768         -expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).{A = sqlite3PExpr(pParse,@OP,X,Y,0);}
          830  +                                        {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
          831  +expr(A) ::= expr(X) PLUS|MINUS(OP) expr(Y).
          832  +                                        {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   769    833   expr(A) ::= expr(X) STAR|SLASH|REM(OP) expr(Y).
   770         -                                           {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
   771         -expr(A) ::= expr(X) CONCAT(OP) expr(Y).    {A = sqlite3PExpr(pParse,@OP,X,Y,0);}
          834  +                                        {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
          835  +expr(A) ::= expr(X) CONCAT(OP) expr(Y). {spanBinaryExpr(&A,pParse,@OP,&X,&Y);}
   772    836   %type likeop {struct LikeOp}
   773    837   likeop(A) ::= LIKE_KW(X).     {A.eOperator = X; A.not = 0;}
   774    838   likeop(A) ::= NOT LIKE_KW(X). {A.eOperator = X; A.not = 1;}
   775    839   likeop(A) ::= MATCH(X).       {A.eOperator = X; A.not = 0;}
   776    840   likeop(A) ::= NOT MATCH(X).   {A.eOperator = X; A.not = 1;}
   777         -%type escape {Expr*}
   778         -%destructor escape {sqlite3ExprDelete(pParse->db, $$);}
          841  +%type escape {ExprSpan}
          842  +%destructor escape {sqlite3ExprDelete(pParse->db, $$.pExpr);}
   779    843   escape(X) ::= ESCAPE expr(A). [ESCAPE] {X = A;}
   780         -escape(X) ::= .               [ESCAPE] {X = 0;}
          844  +escape(X) ::= .               [ESCAPE] {memset(&X,0,sizeof(X));}
   781    845   expr(A) ::= expr(X) likeop(OP) expr(Y) escape(E).  [LIKE_KW]  {
   782    846     ExprList *pList;
   783         -  pList = sqlite3ExprListAppend(pParse,0, Y, 0);
   784         -  pList = sqlite3ExprListAppend(pParse,pList, X, 0);
   785         -  if( E ){
   786         -    pList = sqlite3ExprListAppend(pParse,pList, E, 0);
          847  +  pList = sqlite3ExprListAppend(pParse,0, Y.pExpr);
          848  +  pList = sqlite3ExprListAppend(pParse,pList, X.pExpr);
          849  +  if( E.pExpr ){
          850  +    pList = sqlite3ExprListAppend(pParse,pList, E.pExpr);
          851  +  }
          852  +  A.pExpr = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
          853  +  if( OP.not ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
          854  +  A.zStart = X.zStart;
          855  +  A.zEnd = Y.zEnd;
          856  +  if( A.pExpr ) A.pExpr->flags |= EP_InfixFunc;
          857  +}
          858  +
          859  +%include {
          860  +  /* Construct an expression node for a unary postfix operator
          861  +  */
          862  +  static void spanUnaryPostfix(
          863  +    ExprSpan *pOut,        /* Write the new expression node here */
          864  +    Parse *pParse,         /* Parsing context to record errors */
          865  +    int op,                /* The operator */
          866  +    ExprSpan *pOperand,    /* The operand */
          867  +    Token *pPostOp         /* The operand token for setting the span */
          868  +  ){
          869  +    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
          870  +    pOut->zStart = pOperand->zStart;
          871  +    pOut->zEnd = &pPostOp->z[pPostOp->n];
          872  +  }                           
          873  +}
          874  +
          875  +expr(A) ::= expr(X) ISNULL|NOTNULL(E).   {spanUnaryPostfix(&A,pParse,@E,&X,&E);}
          876  +expr(A) ::= expr(X) IS NULL(E).   {spanUnaryPostfix(&A,pParse,TK_ISNULL,&X,&E);}
          877  +expr(A) ::= expr(X) NOT NULL(E). {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);}
          878  +expr(A) ::= expr(X) IS NOT NULL(E).
          879  +                                 {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);}
          880  +
          881  +%include {
          882  +  /* Construct an expression node for a unary prefix operator
          883  +  */
          884  +  static void spanUnaryPrefix(
          885  +    ExprSpan *pOut,        /* Write the new expression node here */
          886  +    Parse *pParse,         /* Parsing context to record errors */
          887  +    int op,                /* The operator */
          888  +    ExprSpan *pOperand,    /* The operand */
          889  +    Token *pPreOp         /* The operand token for setting the span */
          890  +  ){
          891  +    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
          892  +    pOut->zStart = pPreOp->z;
          893  +    pOut->zEnd = pOperand->zEnd;
   787    894     }
   788         -  A = sqlite3ExprFunction(pParse, pList, &OP.eOperator);
   789         -  if( OP.not ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
   790         -  sqlite3ExprSpan(A, &X->span, &Y->span);
   791         -  if( A ) A->flags |= EP_InfixFunc;
   792         -}
   793         -
   794         -expr(A) ::= expr(X) ISNULL|NOTNULL(E). {
   795         -  A = sqlite3PExpr(pParse, @E, X, 0, 0);
   796         -  sqlite3ExprSpan(A,&X->span,&E);
   797         -}
   798         -expr(A) ::= expr(X) IS NULL(E). {
   799         -  A = sqlite3PExpr(pParse, TK_ISNULL, X, 0, 0);
   800         -  sqlite3ExprSpan(A,&X->span,&E);
   801         -}
   802         -expr(A) ::= expr(X) NOT NULL(E). {
   803         -  A = sqlite3PExpr(pParse, TK_NOTNULL, X, 0, 0);
   804         -  sqlite3ExprSpan(A,&X->span,&E);
   805         -}
   806         -expr(A) ::= expr(X) IS NOT NULL(E). {
   807         -  A = sqlite3PExpr(pParse, TK_NOTNULL, X, 0, 0);
   808         -  sqlite3ExprSpan(A,&X->span,&E);
   809         -}
   810         -expr(A) ::= NOT(B) expr(X). {
   811         -  A = sqlite3PExpr(pParse, @B, X, 0, 0);
   812         -  sqlite3ExprSpan(A,&B,&X->span);
   813         -}
   814         -expr(A) ::= BITNOT(B) expr(X). {
   815         -  A = sqlite3PExpr(pParse, @B, X, 0, 0);
   816         -  sqlite3ExprSpan(A,&B,&X->span);
   817         -}
   818         -expr(A) ::= MINUS(B) expr(X). [UMINUS] {
   819         -  A = sqlite3PExpr(pParse, TK_UMINUS, X, 0, 0);
   820         -  sqlite3ExprSpan(A,&B,&X->span);
   821         -}
   822         -expr(A) ::= PLUS(B) expr(X). [UPLUS] {
   823         -  A = sqlite3PExpr(pParse, TK_UPLUS, X, 0, 0);
   824         -  sqlite3ExprSpan(A,&B,&X->span);
   825         -}
          895  +}
          896  +
          897  +
          898  +
          899  +expr(A) ::= NOT(B) expr(X).    {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
          900  +expr(A) ::= BITNOT(B) expr(X). {spanUnaryPrefix(&A,pParse,@B,&X,&B);}
          901  +expr(A) ::= MINUS(B) expr(X). [UMINUS]
          902  +                               {spanUnaryPrefix(&A,pParse,TK_UMINUS,&X,&B);}
          903  +expr(A) ::= PLUS(B) expr(X). [UPLUS]
          904  +                               {spanUnaryPrefix(&A,pParse,TK_UPLUS,&X,&B);}
          905  +
   826    906   %type between_op {int}
   827    907   between_op(A) ::= BETWEEN.     {A = 0;}
   828    908   between_op(A) ::= NOT BETWEEN. {A = 1;}
   829    909   expr(A) ::= expr(W) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
   830         -  ExprList *pList = sqlite3ExprListAppend(pParse,0, X, 0);
   831         -  pList = sqlite3ExprListAppend(pParse,pList, Y, 0);
   832         -  A = sqlite3PExpr(pParse, TK_BETWEEN, W, 0, 0);
   833         -  if( A ){
   834         -    A->x.pList = pList;
          910  +  ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
          911  +  pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
          912  +  A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, W.pExpr, 0, 0);
          913  +  if( A.pExpr ){
          914  +    A.pExpr->x.pList = pList;
   835    915     }else{
   836    916       sqlite3ExprListDelete(pParse->db, pList);
   837    917     } 
   838         -  if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
   839         -  sqlite3ExprSpan(A,&W->span,&Y->span);
          918  +  if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
          919  +  A.zStart = W.zStart;
          920  +  A.zEnd = Y.zEnd;
   840    921   }
   841    922   %ifndef SQLITE_OMIT_SUBQUERY
   842    923     %type in_op {int}
   843    924     in_op(A) ::= IN.      {A = 0;}
   844    925     in_op(A) ::= NOT IN.  {A = 1;}
   845    926     expr(A) ::= expr(X) in_op(N) LP exprlist(Y) RP(E). [IN] {
   846         -    A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
   847         -    if( A ){
   848         -      A->x.pList = Y;
   849         -      sqlite3ExprSetHeight(pParse, A);
          927  +    A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
          928  +    if( A.pExpr ){
          929  +      A.pExpr->x.pList = Y;
          930  +      sqlite3ExprSetHeight(pParse, A.pExpr);
   850    931       }else{
   851    932         sqlite3ExprListDelete(pParse->db, Y);
   852    933       }
   853         -    if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
   854         -    sqlite3ExprSpan(A,&X->span,&E);
          934  +    if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
          935  +    A.zStart = X.zStart;
          936  +    A.zEnd = &E.z[E.n];
   855    937     }
   856    938     expr(A) ::= LP(B) select(X) RP(E). {
   857         -    A = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
   858         -    if( A ){
   859         -      A->x.pSelect = X;
   860         -      ExprSetProperty(A, EP_xIsSelect);
   861         -      sqlite3ExprSetHeight(pParse, A);
          939  +    A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
          940  +    if( A.pExpr ){
          941  +      A.pExpr->x.pSelect = X;
          942  +      ExprSetProperty(A.pExpr, EP_xIsSelect);
          943  +      sqlite3ExprSetHeight(pParse, A.pExpr);
   862    944       }else{
   863    945         sqlite3SelectDelete(pParse->db, X);
   864    946       }
   865         -    sqlite3ExprSpan(A,&B,&E);
          947  +    A.zStart = B.z;
          948  +    A.zEnd = &E.z[E.n];
   866    949     }
   867    950     expr(A) ::= expr(X) in_op(N) LP select(Y) RP(E).  [IN] {
   868         -    A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
   869         -    if( A ){
   870         -      A->x.pSelect = Y;
   871         -      ExprSetProperty(A, EP_xIsSelect);
   872         -      sqlite3ExprSetHeight(pParse, A);
          951  +    A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
          952  +    if( A.pExpr ){
          953  +      A.pExpr->x.pSelect = Y;
          954  +      ExprSetProperty(A.pExpr, EP_xIsSelect);
          955  +      sqlite3ExprSetHeight(pParse, A.pExpr);
   873    956       }else{
   874    957         sqlite3SelectDelete(pParse->db, Y);
   875    958       }
   876         -    if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
   877         -    sqlite3ExprSpan(A,&X->span,&E);
          959  +    if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
          960  +    A.zStart = X.zStart;
          961  +    A.zEnd = &E.z[E.n];
   878    962     }
   879    963     expr(A) ::= expr(X) in_op(N) nm(Y) dbnm(Z). [IN] {
   880    964       SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
   881         -    A = sqlite3PExpr(pParse, TK_IN, X, 0, 0);
   882         -    if( A ){
   883         -      A->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
   884         -      ExprSetProperty(A, EP_xIsSelect);
   885         -      sqlite3ExprSetHeight(pParse, A);
          965  +    A.pExpr = sqlite3PExpr(pParse, TK_IN, X.pExpr, 0, 0);
          966  +    if( A.pExpr ){
          967  +      A.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
          968  +      ExprSetProperty(A.pExpr, EP_xIsSelect);
          969  +      sqlite3ExprSetHeight(pParse, A.pExpr);
   886    970       }else{
   887    971         sqlite3SrcListDelete(pParse->db, pSrc);
   888    972       }
   889         -    if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0, 0);
   890         -    sqlite3ExprSpan(A,&X->span,Z.z?&Z:&Y);
          973  +    if( N ) A.pExpr = sqlite3PExpr(pParse, TK_NOT, A.pExpr, 0, 0);
          974  +    A.zStart = X.zStart;
          975  +    A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
   891    976     }
   892    977     expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
   893         -    Expr *p = A = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
          978  +    Expr *p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
   894    979       if( p ){
   895    980         p->x.pSelect = Y;
   896         -      ExprSetProperty(A, EP_xIsSelect);
   897         -      sqlite3ExprSpan(p,&B,&E);
   898         -      sqlite3ExprSetHeight(pParse, A);
          981  +      ExprSetProperty(p, EP_xIsSelect);
          982  +      sqlite3ExprSetHeight(pParse, p);
   899    983       }else{
   900    984         sqlite3SelectDelete(pParse->db, Y);
   901    985       }
          986  +    A.zStart = B.z;
          987  +    A.zEnd = &E.z[E.n];
   902    988     }
   903    989   %endif SQLITE_OMIT_SUBQUERY
   904    990   
   905    991   /* CASE expressions */
   906    992   expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
   907         -  A = sqlite3PExpr(pParse, TK_CASE, X, Z, 0);
   908         -  if( A ){
   909         -    A->x.pList = Y;
   910         -    sqlite3ExprSetHeight(pParse, A);
          993  +  A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, Z, 0);
          994  +  if( A.pExpr ){
          995  +    A.pExpr->x.pList = Y;
          996  +    sqlite3ExprSetHeight(pParse, A.pExpr);
   911    997     }else{
   912    998       sqlite3ExprListDelete(pParse->db, Y);
   913    999     }
   914         -  sqlite3ExprSpan(A, &C, &E);
         1000  +  A.zStart = C.z;
         1001  +  A.zEnd = &E.z[E.n];
   915   1002   }
   916   1003   %type case_exprlist {ExprList*}
   917   1004   %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
   918   1005   case_exprlist(A) ::= case_exprlist(X) WHEN expr(Y) THEN expr(Z). {
   919         -  A = sqlite3ExprListAppend(pParse,X, Y, 0);
   920         -  A = sqlite3ExprListAppend(pParse,A, Z, 0);
         1006  +  A = sqlite3ExprListAppend(pParse,X, Y.pExpr);
         1007  +  A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
   921   1008   }
   922   1009   case_exprlist(A) ::= WHEN expr(Y) THEN expr(Z). {
   923         -  A = sqlite3ExprListAppend(pParse,0, Y, 0);
   924         -  A = sqlite3ExprListAppend(pParse,A, Z, 0);
         1010  +  A = sqlite3ExprListAppend(pParse,0, Y.pExpr);
         1011  +  A = sqlite3ExprListAppend(pParse,A, Z.pExpr);
   925   1012   }
   926   1013   %type case_else {Expr*}
   927   1014   %destructor case_else {sqlite3ExprDelete(pParse->db, $$);}
   928         -case_else(A) ::=  ELSE expr(X).         {A = X;}
         1015  +case_else(A) ::=  ELSE expr(X).         {A = X.pExpr;}
   929   1016   case_else(A) ::=  .                     {A = 0;} 
   930   1017   %type case_operand {Expr*}
   931   1018   %destructor case_operand {sqlite3ExprDelete(pParse->db, $$);}
   932         -case_operand(A) ::= expr(X).            {A = X;} 
         1019  +case_operand(A) ::= expr(X).            {A = X.pExpr;} 
   933   1020   case_operand(A) ::= .                   {A = 0;} 
   934   1021   
   935   1022   %type exprlist {ExprList*}
   936   1023   %destructor exprlist {sqlite3ExprListDelete(pParse->db, $$);}
   937   1024   %type nexprlist {ExprList*}
   938   1025   %destructor nexprlist {sqlite3ExprListDelete(pParse->db, $$);}
   939   1026   
   940   1027   exprlist(A) ::= nexprlist(X).                {A = X;}
   941   1028   exprlist(A) ::= .                            {A = 0;}
   942   1029   nexprlist(A) ::= nexprlist(X) COMMA expr(Y).
   943         -    {A = sqlite3ExprListAppend(pParse,X,Y,0);}
         1030  +    {A = sqlite3ExprListAppend(pParse,X,Y.pExpr);}
   944   1031   nexprlist(A) ::= expr(Y).
   945         -    {A = sqlite3ExprListAppend(pParse,0,Y,0);}
         1032  +    {A = sqlite3ExprListAppend(pParse,0,Y.pExpr);}
   946   1033   
   947   1034   
   948   1035   ///////////////////////////// The CREATE INDEX command ///////////////////////
   949   1036   //
   950   1037   cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
   951   1038           ON nm(Y) LP idxlist(Z) RP(E). {
   952   1039     sqlite3CreateIndex(pParse, &X, &D, 
................................................................................
   964   1051   %destructor idxlist_opt {sqlite3ExprListDelete(pParse->db, $$);}
   965   1052   
   966   1053   idxlist_opt(A) ::= .                         {A = 0;}
   967   1054   idxlist_opt(A) ::= LP idxlist(X) RP.         {A = X;}
   968   1055   idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z).  {
   969   1056     Expr *p = 0;
   970   1057     if( C.n>0 ){
   971         -    p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
         1058  +    p = sqlite3Expr(pParse->db, TK_COLUMN, 0);
   972   1059       sqlite3ExprSetColl(pParse, p, &C);
   973   1060     }
   974         -  A = sqlite3ExprListAppend(pParse,X, p, &Y);
         1061  +  A = sqlite3ExprListAppend(pParse,X, p);
         1062  +  sqlite3ExprListSetName(pParse,A,&Y,1);
   975   1063     sqlite3ExprListCheckLength(pParse, A, "index");
   976   1064     if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
   977   1065   }
   978   1066   idxlist(A) ::= nm(Y) collate(C) sortorder(Z). {
   979   1067     Expr *p = 0;
   980   1068     if( C.n>0 ){
   981   1069       p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
   982   1070       sqlite3ExprSetColl(pParse, p, &C);
   983   1071     }
   984         -  A = sqlite3ExprListAppend(pParse,0, p, &Y);
         1072  +  A = sqlite3ExprListAppend(pParse,0, p);
         1073  +  sqlite3ExprListSetName(pParse, A, &Y, 1);
   985   1074     sqlite3ExprListCheckLength(pParse, A, "index");
   986   1075     if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z;
   987   1076   }
   988   1077   
   989   1078   %type collate {Token}
   990         -collate(C) ::= .                {C.z = 0; C.n = 0;}
         1079  +collate(C) ::= .                 {C.z = 0; C.n = 0;}
   991   1080   collate(C) ::= COLLATE ids(X).   {C = X;}
   992   1081   
   993   1082   
   994   1083   ///////////////////////////// The DROP INDEX command /////////////////////////
   995   1084   //
   996   1085   cmd ::= DROP INDEX ifexists(E) fullname(X).   {sqlite3DropIndex(pParse, X, E);}
   997   1086   
................................................................................
  1061   1150   
  1062   1151   foreach_clause ::= .
  1063   1152   foreach_clause ::= FOR EACH ROW.
  1064   1153   
  1065   1154   %type when_clause {Expr*}
  1066   1155   %destructor when_clause {sqlite3ExprDelete(pParse->db, $$);}
  1067   1156   when_clause(A) ::= .             { A = 0; }
  1068         -when_clause(A) ::= WHEN expr(X). { A = X; }
         1157  +when_clause(A) ::= WHEN expr(X). { A = X.pExpr; }
  1069   1158   
  1070   1159   %type trigger_cmd_list {TriggerStep*}
  1071   1160   %destructor trigger_cmd_list {sqlite3DeleteTriggerStep(pParse->db, $$);}
  1072   1161   trigger_cmd_list(A) ::= trigger_cmd_list(Y) trigger_cmd(X) SEMI. {
  1073         -/*
  1074         -  if( Y ){
  1075         -    Y->pLast->pNext = X;
  1076         -  }else{
  1077         -    Y = X;
  1078         -  }
  1079         -*/
  1080   1162     assert( Y!=0 );
  1081   1163     Y->pLast->pNext = X;
  1082   1164     Y->pLast = X;
  1083   1165     A = Y;
  1084   1166   }
  1085   1167   trigger_cmd_list(A) ::= trigger_cmd(X) SEMI. { 
  1086         -  /* if( X ) */
  1087   1168     assert( X!=0 );
  1088   1169     X->pLast = X;
  1089   1170     A = X;
  1090   1171   }
  1091   1172   
  1092   1173   %type trigger_cmd {TriggerStep*}
  1093   1174   %destructor trigger_cmd {sqlite3DeleteTriggerStep(pParse->db, $$);}
................................................................................
  1108   1189                  {A = sqlite3TriggerDeleteStep(pParse->db, &X, Y);}
  1109   1190   
  1110   1191   // SELECT
  1111   1192   trigger_cmd(A) ::= select(X).  {A = sqlite3TriggerSelectStep(pParse->db, X); }
  1112   1193   
  1113   1194   // The special RAISE expression that may occur in trigger programs
  1114   1195   expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
  1115         -  A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
  1116         -  if( A ){
  1117         -    A->affinity = OE_Ignore;
  1118         -    sqlite3ExprSpan(A, &X, &Y);
         1196  +  A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
         1197  +  if( A.pExpr ){
         1198  +    A.pExpr->affinity = OE_Ignore;
  1119   1199     }
         1200  +  A.zStart = X.z;
         1201  +  A.zEnd = &Y.z[Y.n];
  1120   1202   }
  1121   1203   expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
  1122         -  A = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z); 
  1123         -  if( A ) {
  1124         -    A->affinity = (char)T;
  1125         -    sqlite3ExprSpan(A, &X, &Y);
         1204  +  A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z); 
         1205  +  if( A.pExpr ) {
         1206  +    A.pExpr->affinity = (char)T;
  1126   1207     }
         1208  +  A.zStart = X.z;
         1209  +  A.zEnd = &Y.z[Y.n];
  1127   1210   }
  1128   1211   %endif  !SQLITE_OMIT_TRIGGER
  1129   1212   
  1130   1213   %type raisetype {int}
  1131   1214   raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
  1132   1215   raisetype(A) ::= ABORT.     {A = OE_Abort;}
  1133   1216   raisetype(A) ::= FAIL.      {A = OE_Fail;}
................................................................................
  1139   1222     sqlite3DropTrigger(pParse,X,NOERR);
  1140   1223   }
  1141   1224   %endif  !SQLITE_OMIT_TRIGGER
  1142   1225   
  1143   1226   //////////////////////// ATTACH DATABASE file AS name /////////////////////////
  1144   1227   %ifndef SQLITE_OMIT_ATTACH
  1145   1228   cmd ::= ATTACH database_kw_opt expr(F) AS expr(D) key_opt(K). {
  1146         -  sqlite3Attach(pParse, F, D, K);
         1229  +  sqlite3Attach(pParse, F.pExpr, D.pExpr, K);
  1147   1230   }
  1148   1231   cmd ::= DETACH database_kw_opt expr(D). {
  1149         -  sqlite3Detach(pParse, D);
         1232  +  sqlite3Detach(pParse, D.pExpr);
  1150   1233   }
  1151   1234   
  1152   1235   %type key_opt {Expr*}
  1153   1236   %destructor key_opt {sqlite3ExprDelete(pParse->db, $$);}
  1154   1237   key_opt(A) ::= .                     { A = 0; }
  1155         -key_opt(A) ::= KEY expr(X).          { A = X; }
         1238  +key_opt(A) ::= KEY expr(X).          { A = X.pExpr; }
  1156   1239   
  1157   1240   database_kw_opt ::= DATABASE.
  1158   1241   database_kw_opt ::= .
  1159   1242   %endif SQLITE_OMIT_ATTACH
  1160   1243   
  1161   1244   ////////////////////////// REINDEX collation //////////////////////////////////
  1162   1245   %ifndef SQLITE_OMIT_REINDEX

Changes to src/pragma.c.

     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   ** This file contains code used to implement the PRAGMA command.
    13     13   **
    14         -** $Id: pragma.c,v 1.209 2009/04/07 22:05:43 drh Exp $
           14  +** $Id: pragma.c,v 1.210 2009/05/27 10:31:29 drh Exp $
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   
    18     18   /* Ignore this whole file if pragmas are disabled
    19     19   */
    20     20   #if !defined(SQLITE_OMIT_PRAGMA) && !defined(SQLITE_OMIT_PARSER)
    21     21   
................................................................................
   836    836             continue;
   837    837           }
   838    838           sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
   839    839           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
   840    840           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
   841    841              pCol->zType ? pCol->zType : "", 0);
   842    842           sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
   843         -        if( pCol->pDflt ){
   844         -          const Token *p = &pCol->pDflt->span;
   845         -          assert( p->z );
   846         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)p->z, p->n);
          843  +        if( pCol->zDflt ){
          844  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
   847    845           }else{
   848    846             sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
   849    847           }
   850    848           sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
   851    849           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
   852    850         }
   853    851       }

Changes to src/resolve.c.

    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains routines used for walking the parser tree and
    14     14   ** resolve all identifiers by associating them with a particular
    15     15   ** table and column.
    16     16   **
    17         -** $Id: resolve.c,v 1.22 2009/05/05 15:46:43 drh Exp $
           17  +** $Id: resolve.c,v 1.23 2009/05/27 10:31:29 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <stdlib.h>
    21     21   #include <string.h>
    22     22   
    23     23   /*
    24     24   ** Turn the pExpr expression into an alias for the iCol-th column of the
................................................................................
    59     59     sqlite3 *db;           /* The database connection */
    60     60   
    61     61     assert( iCol>=0 && iCol<pEList->nExpr );
    62     62     pOrig = pEList->a[iCol].pExpr;
    63     63     assert( pOrig!=0 );
    64     64     assert( pOrig->flags & EP_Resolved );
    65     65     db = pParse->db;
    66         -  pDup = sqlite3ExprDup(db, pOrig, 0);
    67         -  if( pDup==0 ) return;
    68         -  sqlite3TokenCopy(db, &pDup->token, &pOrig->token);
    69         -  if( pDup->op!=TK_COLUMN && zType[0]!='G' ){
           66  +  if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
           67  +    pDup = sqlite3ExprDup(db, pOrig, 0);
    70     68       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
    71     69       if( pDup==0 ) return;
    72     70       if( pEList->a[iCol].iAlias==0 ){
    73     71         pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
    74     72       }
    75     73       pDup->iTable = pEList->a[iCol].iAlias;
           74  +  }else{
           75  +    char *zToken = pOrig->zToken;
           76  +    pOrig->zToken = 0;
           77  +    pDup = sqlite3ExprDup(db, pOrig, 0);
           78  +    pOrig->zToken = zToken;
           79  +    if( pDup==0 ) return;
           80  +    if( zToken ){
           81  +      assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 );
           82  +      pDup->flags2 |= EP2_FreeToken;
           83  +      pDup->zToken = sqlite3DbStrDup(db, zToken);
           84  +    }
    76     85     }
    77     86     if( pExpr->flags & EP_ExpCollate ){
    78     87       pDup->pColl = pExpr->pColl;
    79     88       pDup->flags |= EP_ExpCollate;
    80     89     }
    81     90     sqlite3ExprClear(db, pExpr);
    82     91     memcpy(pExpr, pDup, sizeof(*pExpr));
................................................................................
    96    105   **    pExpr->pTab          Points to the Table structure of X.Y (even if
    97    106   **                         X and/or Y are implied.)
    98    107   **    pExpr->iColumn       Set to the column number within the table.
    99    108   **    pExpr->op            Set to TK_COLUMN.
   100    109   **    pExpr->pLeft         Any expression this points to is deleted
   101    110   **    pExpr->pRight        Any expression this points to is deleted.
   102    111   **
   103         -** The pDbToken is the name of the database (the "X").  This value may be
          112  +** The zDb variable is the name of the database (the "X").  This value may be
   104    113   ** NULL meaning that name is of the form Y.Z or Z.  Any available database
   105         -** can be used.  The pTableToken is the name of the table (the "Y").  This
   106         -** value can be NULL if pDbToken is also NULL.  If pTableToken is NULL it
          114  +** can be used.  The zTable variable is the name of the table (the "Y").  This
          115  +** value can be NULL if zDb is also NULL.  If zTable is NULL it
   107    116   ** means that the form of the name is Z and that columns from any table
   108    117   ** can be used.
   109    118   **
   110    119   ** If the name cannot be resolved unambiguously, leave an error message
   111    120   ** in pParse and return non-zero.  Return zero on success.
   112    121   */
   113    122   static int lookupName(
   114    123     Parse *pParse,       /* The parsing context */
   115         -  Token *pDbToken,     /* Name of the database containing table, or NULL */
   116         -  Token *pTableToken,  /* Name of table containing column, or NULL */
   117         -  Token *pColumnToken, /* Name of the column. */
          124  +  const char *zDb,     /* Name of the database containing table, or NULL */
          125  +  const char *zTab,    /* Name of table containing column, or NULL */
          126  +  const char *zCol,    /* Name of the column. */
   118    127     NameContext *pNC,    /* The name context used to resolve the name */
   119    128     Expr *pExpr          /* Make this EXPR node point to the selected column */
   120    129   ){
   121         -  char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */
   122         -  char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */
   123         -  char *zCol = 0;      /* Name of the column.  The "Z" */
   124    130     int i, j;            /* Loop counters */
   125    131     int cnt = 0;                      /* Number of matching column names */
   126    132     int cntTab = 0;                   /* Number of matching table names */
   127    133     sqlite3 *db = pParse->db;         /* The database connection */
   128    134     struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
   129    135     struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
   130    136     NameContext *pTopNC = pNC;        /* First namecontext in the list */
   131    137     Schema *pSchema = 0;              /* Schema of the expression */
   132    138   
   133         -  assert( pNC ); /* the name context cannot be NULL. */
   134         -  assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
   135         -
   136         -  /* Dequote and zero-terminate the names */
   137         -  zDb = sqlite3NameFromToken(db, pDbToken);
   138         -  zTab = sqlite3NameFromToken(db, pTableToken);
   139         -  zCol = sqlite3NameFromToken(db, pColumnToken);
   140         -  if( db->mallocFailed ){
   141         -    goto lookupname_end;
   142         -  }
          139  +  assert( pNC );     /* the name context cannot be NULL. */
          140  +  assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
   143    141   
   144    142     /* Initialize the node to no-match */
   145    143     pExpr->iTable = -1;
   146    144     pExpr->pTab = 0;
   147    145   
   148    146     /* Start at the inner-most context and move outward until a match is found */
   149    147     while( pNC && cnt==0 ){
................................................................................
   289    287             Expr *pOrig;
   290    288             assert( pExpr->pLeft==0 && pExpr->pRight==0 );
   291    289             assert( pExpr->x.pList==0 );
   292    290             assert( pExpr->x.pSelect==0 );
   293    291             pOrig = pEList->a[j].pExpr;
   294    292             if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
   295    293               sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
   296         -            sqlite3DbFree(db, zCol);
   297    294               return 2;
   298    295             }
   299    296             resolveAlias(pParse, pEList, j, pExpr, "");
   300    297             cnt = 1;
   301    298             pMatch = 0;
   302    299             assert( zTab==0 && zDb==0 );
   303         -          goto lookupname_end_2;
          300  +          goto lookupname_end;
   304    301           }
   305    302         } 
   306    303       }
   307    304   
   308    305       /* Advance to the next name context.  The loop will exit when either
   309    306       ** we have a match (cnt>0) or when we run out of name contexts.
   310    307       */
................................................................................
   320    317     ** case, we need to return right away and not make any changes to
   321    318     ** pExpr.
   322    319     **
   323    320     ** Because no reference was made to outer contexts, the pNC->nRef
   324    321     ** fields are not changed in any context.
   325    322     */
   326    323     if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
   327         -    sqlite3DbFree(db, zCol);
   328    324       pExpr->op = TK_STRING;
   329    325       pExpr->pTab = 0;
   330    326       return 0;
   331    327     }
   332    328   
   333    329     /*
   334    330     ** cnt==0 means there was not match.  cnt>1 means there were two or
................................................................................
   359    355       if( n>=BMS ){
   360    356         n = BMS-1;
   361    357       }
   362    358       assert( pMatch->iCursor==pExpr->iTable );
   363    359       pMatch->colUsed |= ((Bitmask)1)<<n;
   364    360     }
   365    361   
   366         -lookupname_end:
   367    362     /* Clean up and return
   368    363     */
   369         -  sqlite3DbFree(db, zDb);
   370         -  sqlite3DbFree(db, zTab);
   371    364     sqlite3ExprDelete(db, pExpr->pLeft);
   372    365     pExpr->pLeft = 0;
   373    366     sqlite3ExprDelete(db, pExpr->pRight);
   374    367     pExpr->pRight = 0;
   375    368     pExpr->op = TK_COLUMN;
   376         -lookupname_end_2:
   377         -  sqlite3DbFree(db, zCol);
          369  +lookupname_end:
   378    370     if( cnt==1 ){
   379    371       assert( pNC!=0 );
   380    372       sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
   381    373       /* Increment the nRef value on all name contexts from TopNC up to
   382    374       ** the point where the name matched. */
   383    375       for(;;){
   384    376         assert( pTopNC!=0 );
................................................................................
   443    435         break;
   444    436       }
   445    437   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
   446    438   
   447    439       /* A lone identifier is the name of a column.
   448    440       */
   449    441       case TK_ID: {
   450         -      lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
          442  +      lookupName(pParse, 0, 0, pExpr->zToken, pNC, pExpr);
   451    443         return WRC_Prune;
   452    444       }
   453    445     
   454    446       /* A table name and column name:     ID.ID
   455    447       ** Or a database, table and column:  ID.ID.ID
   456    448       */
   457    449       case TK_DOT: {
   458         -      Token *pColumn;
   459         -      Token *pTable;
   460         -      Token *pDb;
          450  +      const char *zColumn;
          451  +      const char *zTable;
          452  +      const char *zDb;
   461    453         Expr *pRight;
   462    454   
   463    455         /* if( pSrcList==0 ) break; */
   464    456         pRight = pExpr->pRight;
   465    457         if( pRight->op==TK_ID ){
   466         -        pDb = 0;
   467         -        pTable = &pExpr->pLeft->token;
   468         -        pColumn = &pRight->token;
          458  +        zDb = 0;
          459  +        zTable = pExpr->pLeft->zToken;
          460  +        zColumn = pRight->zToken;
   469    461         }else{
   470    462           assert( pRight->op==TK_DOT );
   471         -        pDb = &pExpr->pLeft->token;
   472         -        pTable = &pRight->pLeft->token;
   473         -        pColumn = &pRight->pRight->token;
          463  +        zDb = pExpr->pLeft->zToken;
          464  +        zTable = pRight->pLeft->zToken;
          465  +        zColumn = pRight->pRight->zToken;
   474    466         }
   475         -      lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
          467  +      lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
   476    468         return WRC_Prune;
   477    469       }
   478    470   
   479    471       /* Resolve function names
   480    472       */
   481    473       case TK_CONST_FUNC:
   482    474       case TK_FUNCTION: {
................................................................................
   488    480         int auth;                   /* Authorization to use the function */
   489    481         int nId;                    /* Number of characters in function name */
   490    482         const char *zId;            /* The function name. */
   491    483         FuncDef *pDef;              /* Information about the function */
   492    484         u8 enc = ENC(pParse->db);   /* The database encoding */
   493    485   
   494    486         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   495         -      zId = (char*)pExpr->token.z;
   496         -      nId = pExpr->token.n;
          487  +      zId = pExpr->zToken;
          488  +      nId = sqlite3Strlen30(zId);
   497    489         pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
   498    490         if( pDef==0 ){
   499    491           pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
   500    492           if( pDef==0 ){
   501    493             no_such_func = 1;
   502    494           }else{
   503    495             wrong_num_args = 1;
................................................................................
   590    582   static int resolveAsName(
   591    583     Parse *pParse,     /* Parsing context for error messages */
   592    584     ExprList *pEList,  /* List of expressions to scan */
   593    585     Expr *pE           /* Expression we are trying to match */
   594    586   ){
   595    587     int i;             /* Loop counter */
   596    588   
   597         -  if( pE->op==TK_ID || (pE->op==TK_STRING && pE->token.z[0]!='\'') ){
   598         -    sqlite3 *db = pParse->db;
   599         -    char *zCol = sqlite3NameFromToken(db, &pE->token);
   600         -    if( zCol==0 ){
   601         -      return -1;
   602         -    }
          589  +  if( pE->op==TK_ID || (pE->op==TK_STRING && pE->zToken[0]!='\'') ){
          590  +    char *zCol = pE->zToken;
   603    591       for(i=0; i<pEList->nExpr; i++){
   604    592         char *zAs = pEList->a[i].zName;
   605    593         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
   606         -        sqlite3DbFree(db, zCol);
   607    594           return i+1;
   608    595         }
   609    596       }
   610         -    sqlite3DbFree(db, zCol);
   611    597     }
   612    598     return 0;
   613    599   }
   614    600   
   615    601   /*
   616    602   ** pE is a pointer to an expression which is a single term in the
   617    603   ** ORDER BY of a compound SELECT.  The expression has not been
................................................................................
   754    740             return 1;
   755    741           }
   756    742         }
   757    743         if( iCol>0 ){
   758    744           CollSeq *pColl = pE->pColl;
   759    745           int flags = pE->flags & EP_ExpCollate;
   760    746           sqlite3ExprDelete(db, pE);
   761         -        pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0, 0, 0);
          747  +        pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
   762    748           if( pE==0 ) return 1;
   763    749           pE->pColl = pColl;
   764    750           pE->flags |= EP_IntValue | flags;
   765    751           pE->iTable = iCol;
   766    752           pItem->iCol = (u16)iCol;
   767    753           pItem->done = 1;
   768    754         }else{

Changes to src/select.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 SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.518 2009/05/19 19:04:58 drh Exp $
           15  +** $Id: select.c,v 1.519 2009/05/27 10:31:29 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Delete all the content of a Select structure but do not deallocate
    22     22   ** the select structure itself.
................................................................................
    67     67     pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
    68     68     assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
    69     69     if( pNew==0 ){
    70     70       pNew = &standin;
    71     71       memset(pNew, 0, sizeof(*pNew));
    72     72     }
    73     73     if( pEList==0 ){
    74         -    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0,0,0), 0);
           74  +    pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
    75     75     }
    76     76     pNew->pEList = pEList;
    77     77     pNew->pSrc = pSrc;
    78     78     pNew->pWhere = pWhere;
    79     79     pNew->pGroupBy = pGroupBy;
    80     80     pNew->pHaving = pHaving;
    81     81     pNew->pOrderBy = pOrderBy;
................................................................................
   187    187     int i;
   188    188     for(i=0; i<pTab->nCol; i++){
   189    189       if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
   190    190     }
   191    191     return -1;
   192    192   }
   193    193   
   194         -/*
   195         -** Set the value of a token to a '\000'-terminated string.
   196         -*/
   197         -static void setToken(Token *p, const char *z){
   198         -  p->z = (u8*)z;
   199         -  p->n = z ? sqlite3Strlen30(z) : 0;
   200         -  p->dyn = 0;
   201         -  p->quoted = 0;
   202         -}
   203         -
   204    194   /*
   205    195   ** Create an expression node for an identifier with the name of zName
   206    196   */
   207    197   Expr *sqlite3CreateIdExpr(Parse *pParse, const char *zName){
   208         -  Token dummy;
   209         -  setToken(&dummy, zName);
   210         -  return sqlite3PExpr(pParse, TK_ID, 0, 0, &dummy);
          198  +  return sqlite3Expr(pParse->db, TK_ID, zName);
   211    199   }
   212    200   
   213    201   /*
   214    202   ** Add a term to the WHERE expression in *ppExpr that requires the
   215    203   ** zCol column to be equal in the two tables pTab1 and pTab2.
   216    204   */
   217    205   static void addWhereTerm(
................................................................................
  1068   1056         if( iCol<0 ){
  1069   1057           zCol = "rowid";
  1070   1058         }else{
  1071   1059           zCol = pTab->aCol[iCol].zName;
  1072   1060         }
  1073   1061         if( !shortNames && !fullNames ){
  1074   1062           sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
  1075         -            sqlite3DbStrNDup(db, (char*)p->span.z, p->span.n), SQLITE_DYNAMIC);
         1063  +            sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
  1076   1064         }else if( fullNames ){
  1077   1065           char *zName = 0;
  1078   1066           zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
  1079   1067           sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
  1080   1068         }else{
  1081   1069           sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
  1082   1070         }
  1083   1071       }else{
  1084   1072         sqlite3VdbeSetColName(v, i, COLNAME_NAME, 
  1085         -          sqlite3DbStrNDup(db, (char*)p->span.z, p->span.n), SQLITE_DYNAMIC);
         1073  +          sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
  1086   1074       }
  1087   1075     }
  1088   1076     generateColumnTypes(pParse, pTabList, pEList);
  1089   1077   }
  1090   1078   
  1091   1079   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  1092   1080   /*
................................................................................
  1135   1123     *pnCol = nCol = pEList->nExpr;
  1136   1124     aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
  1137   1125     if( aCol==0 ) return SQLITE_NOMEM;
  1138   1126     for(i=0, pCol=aCol; i<nCol; i++, pCol++){
  1139   1127       /* Get an appropriate name for the column
  1140   1128       */
  1141   1129       p = pEList->a[i].pExpr;
  1142         -    assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 );
         1130  +    assert( p->pRight==0 || p->pRight->zToken==0 || p->pRight->zToken[0]!=0 );
  1143   1131       if( (zName = pEList->a[i].zName)!=0 ){
  1144   1132         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1145   1133         zName = sqlite3DbStrDup(db, zName);
  1146   1134       }else{
  1147   1135         Expr *pColExpr = p;  /* The expression that is the result column name */
  1148   1136         Table *pTab;         /* Table associated with this expression */
  1149   1137         while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
................................................................................
  1150   1138         if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
  1151   1139           /* For columns use the column name name */
  1152   1140           int iCol = pColExpr->iColumn;
  1153   1141           pTab = pColExpr->pTab;
  1154   1142           if( iCol<0 ) iCol = pTab->iPKey;
  1155   1143           zName = sqlite3MPrintf(db, "%s",
  1156   1144                    iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
         1145  +      }else if( pColExpr->op==TK_ID ){
         1146  +        zName = sqlite3MPrintf(db, "%s", pColExpr->zToken);
  1157   1147         }else{
  1158   1148           /* Use the original text of the column expression as its name */
  1159         -        Token *pToken = (pColExpr->span.z?&pColExpr->span:&pColExpr->token);
  1160         -        zName = sqlite3MPrintf(db, "%T", pToken);
         1149  +        zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
  1161   1150         }
  1162   1151       }
  1163   1152       if( db->mallocFailed ){
  1164   1153         sqlite3DbFree(db, zName);
  1165   1154         break;
  1166   1155       }
  1167   1156   
................................................................................
  2052   2041       for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
  2053   2042         struct ExprList_item *pItem;
  2054   2043         for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
  2055   2044           assert( pItem->iCol>0 );
  2056   2045           if( pItem->iCol==i ) break;
  2057   2046         }
  2058   2047         if( j==nOrderBy ){
  2059         -        Expr *pNew = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, 0);
         2048  +        Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
  2060   2049           if( pNew==0 ) return SQLITE_NOMEM;
  2061   2050           pNew->flags |= EP_IntValue;
  2062   2051           pNew->iTable = i;
  2063         -        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew, 0);
         2052  +        pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
  2064   2053           pOrderBy->a[nOrderBy++].iCol = (u16)i;
  2065   2054         }
  2066   2055       }
  2067   2056     }
  2068   2057   
  2069   2058     /* Compute the comparison permutation and keyinfo that is used with
  2070   2059     ** the permutation used to determine if the next
................................................................................
  2341   2330   ** This routine is part of the flattening procedure.  A subquery
  2342   2331   ** whose result set is defined by pEList appears as entry in the
  2343   2332   ** FROM clause of a SELECT such that the VDBE cursor assigned to that
  2344   2333   ** FORM clause entry is iTable.  This routine make the necessary 
  2345   2334   ** changes to pExpr so that it refers directly to the source table
  2346   2335   ** of the subquery rather the result set of the subquery.
  2347   2336   */
  2348         -static void substExpr(
         2337  +static Expr *substExpr(
  2349   2338     sqlite3 *db,        /* Report malloc errors to this connection */
  2350   2339     Expr *pExpr,        /* Expr in which substitution occurs */
  2351   2340     int iTable,         /* Table to be substituted */
  2352   2341     ExprList *pEList    /* Substitute expressions */
  2353   2342   ){
  2354         -  if( pExpr==0 ) return;
         2343  +  if( pExpr==0 ) return 0;
  2355   2344     if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
  2356   2345       if( pExpr->iColumn<0 ){
  2357   2346         pExpr->op = TK_NULL;
  2358   2347       }else{
  2359   2348         Expr *pNew;
  2360   2349         assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
  2361   2350         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
  2362         -      pNew = pEList->a[pExpr->iColumn].pExpr;
  2363         -      assert( pNew!=0 );
  2364         -      pExpr->op = pNew->op;
  2365         -      assert( pExpr->pLeft==0 );
  2366         -      pExpr->pLeft = sqlite3ExprDup(db, pNew->pLeft, 0);
  2367         -      assert( pExpr->pRight==0 );
  2368         -      pExpr->pRight = sqlite3ExprDup(db, pNew->pRight, 0);
  2369         -      pExpr->iTable = pNew->iTable;
  2370         -      pExpr->pTab = pNew->pTab;
  2371         -      pExpr->iColumn = pNew->iColumn;
  2372         -      pExpr->iAgg = pNew->iAgg;
  2373         -      sqlite3TokenCopy(db, &pExpr->token, &pNew->token);
  2374         -      sqlite3TokenCopy(db, &pExpr->span, &pNew->span);
  2375         -      assert( pExpr->x.pList==0 && pExpr->x.pSelect==0 );
  2376         -      if( ExprHasProperty(pNew, EP_xIsSelect) ){
  2377         -        pExpr->x.pSelect = sqlite3SelectDup(db, pNew->x.pSelect, 0);
  2378         -      }else{
  2379         -        pExpr->x.pList = sqlite3ExprListDup(db, pNew->x.pList, 0);
  2380         -      }
  2381         -      pExpr->flags = pNew->flags;
  2382         -      pExpr->pAggInfo = pNew->pAggInfo;
  2383         -      pNew->pAggInfo = 0;
         2351  +      pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
         2352  +      sqlite3ExprDelete(db, pExpr);
         2353  +      pExpr = pNew;
  2384   2354       }
  2385   2355     }else{
  2386         -    substExpr(db, pExpr->pLeft, iTable, pEList);
  2387         -    substExpr(db, pExpr->pRight, iTable, pEList);
         2356  +    pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
         2357  +    pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
  2388   2358       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  2389   2359         substSelect(db, pExpr->x.pSelect, iTable, pEList);
  2390   2360       }else{
  2391   2361         substExprList(db, pExpr->x.pList, iTable, pEList);
  2392   2362       }
  2393   2363     }
         2364  +  return pExpr;
  2394   2365   }
  2395   2366   static void substExprList(
  2396   2367     sqlite3 *db,         /* Report malloc errors here */
  2397   2368     ExprList *pList,     /* List to scan and in which to make substitutes */
  2398   2369     int iTable,          /* Table to be substituted */
  2399   2370     ExprList *pEList     /* Substitute values */
  2400   2371   ){
  2401   2372     int i;
  2402   2373     if( pList==0 ) return;
  2403   2374     for(i=0; i<pList->nExpr; i++){
  2404         -    substExpr(db, pList->a[i].pExpr, iTable, pEList);
         2375  +    pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
  2405   2376     }
  2406   2377   }
  2407   2378   static void substSelect(
  2408   2379     sqlite3 *db,         /* Report malloc errors here */
  2409   2380     Select *p,           /* SELECT statement in which to make substitutions */
  2410   2381     int iTable,          /* Table to be replaced */
  2411   2382     ExprList *pEList     /* Substitute values */
................................................................................
  2413   2384     SrcList *pSrc;
  2414   2385     struct SrcList_item *pItem;
  2415   2386     int i;
  2416   2387     if( !p ) return;
  2417   2388     substExprList(db, p->pEList, iTable, pEList);
  2418   2389     substExprList(db, p->pGroupBy, iTable, pEList);
  2419   2390     substExprList(db, p->pOrderBy, iTable, pEList);
  2420         -  substExpr(db, p->pHaving, iTable, pEList);
  2421         -  substExpr(db, p->pWhere, iTable, pEList);
         2391  +  p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
         2392  +  p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
  2422   2393     substSelect(db, p->pPrior, iTable, pEList);
  2423   2394     pSrc = p->pSrc;
  2424   2395     assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
  2425   2396     if( ALWAYS(pSrc) ){
  2426   2397       for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
  2427   2398         substSelect(db, pItem->pSelect, iTable, pEList);
  2428   2399       }
................................................................................
  2836   2807       **
  2837   2808       ** We look at every expression in the outer query and every place we see
  2838   2809       ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
  2839   2810       */
  2840   2811       pList = pParent->pEList;
  2841   2812       for(i=0; i<pList->nExpr; i++){
  2842   2813         if( pList->a[i].zName==0 ){
  2843         -        Expr *pExpr = pList->a[i].pExpr;
  2844         -        if( ALWAYS(pExpr->span.z!=0) ){
  2845         -          pList->a[i].zName = 
  2846         -               sqlite3DbStrNDup(db, (char*)pExpr->span.z, pExpr->span.n);
         2814  +        const char *zSpan = pList->a[i].zSpan;
         2815  +        if( zSpan ){
         2816  +          pList->a[i].zName = sqlite3DbStrDup(db, zSpan);
  2847   2817           }
  2848   2818         }
  2849   2819       }
  2850   2820       substExprList(db, pParent->pEList, iParent, pSub->pEList);
  2851   2821       if( isAgg ){
  2852   2822         substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
  2853         -      substExpr(db, pParent->pHaving, iParent, pSub->pEList);
         2823  +      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
  2854   2824       }
  2855   2825       if( pSub->pOrderBy ){
  2856   2826         assert( pParent->pOrderBy==0 );
  2857   2827         pParent->pOrderBy = pSub->pOrderBy;
  2858   2828         pSub->pOrderBy = 0;
  2859   2829       }else if( pParent->pOrderBy ){
  2860   2830         substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
................................................................................
  2864   2834       }else{
  2865   2835         pWhere = 0;
  2866   2836       }
  2867   2837       if( subqueryIsAgg ){
  2868   2838         assert( pParent->pHaving==0 );
  2869   2839         pParent->pHaving = pParent->pWhere;
  2870   2840         pParent->pWhere = pWhere;
  2871         -      substExpr(db, pParent->pHaving, iParent, pSub->pEList);
         2841  +      pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
  2872   2842         pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, 
  2873   2843                                     sqlite3ExprDup(db, pSub->pHaving, 0));
  2874   2844         assert( pParent->pGroupBy==0 );
  2875   2845         pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
  2876   2846       }else{
  2877         -      substExpr(db, pParent->pWhere, iParent, pSub->pEList);
         2847  +      pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
  2878   2848         pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
  2879   2849       }
  2880   2850     
  2881   2851       /* The flattened query is distinct if either the inner or the
  2882   2852       ** outer query is distinct. 
  2883   2853       */
  2884   2854       pParent->selFlags |= pSub->selFlags & SF_Distinct;
................................................................................
  2922   2892     if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL;
  2923   2893     pExpr = pEList->a[0].pExpr;
  2924   2894     if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
  2925   2895     if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0;
  2926   2896     pEList = pExpr->x.pList;
  2927   2897     if( pEList==0 || pEList->nExpr!=1 ) return 0;
  2928   2898     if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL;
  2929         -  if( pExpr->token.n!=3 ) return WHERE_ORDERBY_NORMAL;
  2930         -  if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){
         2899  +  if( sqlite3StrICmp(pExpr->zToken,"min")==0 ){
  2931   2900       return WHERE_ORDERBY_MIN;
  2932         -  }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){
         2901  +  }else if( sqlite3StrICmp(pExpr->zToken,"max")==0 ){
  2933   2902       return WHERE_ORDERBY_MAX;
  2934   2903     }
  2935   2904     return WHERE_ORDERBY_NORMAL;
  2936   2905   }
  2937   2906   
  2938   2907   /*
  2939   2908   ** The select statement passed as the first argument is an aggregate query.
................................................................................
  3131   3100   
  3132   3101       for(k=0; k<pEList->nExpr; k++){
  3133   3102         Expr *pE = a[k].pExpr;
  3134   3103         assert( pE->op!=TK_DOT || pE->pRight!=0 );
  3135   3104         if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){
  3136   3105           /* This particular expression does not need to be expanded.
  3137   3106           */
  3138         -        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0);
         3107  +        pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
  3139   3108           if( pNew ){
  3140   3109             pNew->a[pNew->nExpr-1].zName = a[k].zName;
         3110  +          pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
         3111  +          a[k].zName = 0;
         3112  +          a[k].zSpan = 0;
  3141   3113           }
  3142   3114           a[k].pExpr = 0;
  3143         -        a[k].zName = 0;
  3144   3115         }else{
  3145   3116           /* This expression is a "*" or a "TABLE.*" and needs to be
  3146   3117           ** expanded. */
  3147   3118           int tableSeen = 0;      /* Set to 1 when TABLE matches */
  3148   3119           char *zTName;            /* text of name of TABLE */
  3149   3120           if( pE->op==TK_DOT ){
  3150   3121             assert( pE->pLeft!=0 );
  3151         -          zTName = sqlite3NameFromToken(db, &pE->pLeft->token);
         3122  +          zTName = pE->pLeft->zToken;
  3152   3123           }else{
  3153   3124             zTName = 0;
  3154   3125           }
  3155   3126           for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
  3156   3127             Table *pTab = pFrom->pTab;
  3157   3128             char *zTabName = pFrom->zAlias;
  3158   3129             if( zTabName==0 ){
................................................................................
  3162   3133             if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
  3163   3134               continue;
  3164   3135             }
  3165   3136             tableSeen = 1;
  3166   3137             for(j=0; j<pTab->nCol; j++){
  3167   3138               Expr *pExpr, *pRight;
  3168   3139               char *zName = pTab->aCol[j].zName;
         3140  +            char *zColname;  /* The computed column name */
         3141  +            char *zToFree;   /* Malloced string that needs to be freed */
         3142  +            Token sColname;  /* Computed column name as a token */
  3169   3143   
  3170   3144               /* If a column is marked as 'hidden' (currently only possible
  3171   3145               ** for virtual tables), do not include it in the expanded
  3172   3146               ** result-set list.
  3173   3147               */
  3174   3148               if( IsHiddenColumn(&pTab->aCol[j]) ){
  3175   3149                 assert(IsVirtual(pTab));
................................................................................
  3186   3160                 }
  3187   3161                 if( sqlite3IdListIndex(pLeft[1].pUsing, zName)>=0 ){
  3188   3162                   /* In a join with a USING clause, omit columns in the
  3189   3163                   ** using clause from the table on the right. */
  3190   3164                   continue;
  3191   3165                 }
  3192   3166               }
  3193         -            pRight = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
  3194         -            if( pRight==0 ) break;
  3195         -            setToken(&pRight->token, zName);
         3167  +            pRight = sqlite3Expr(db, TK_ID, zName);
         3168  +            zColname = zName;
         3169  +            zToFree = 0;
  3196   3170               if( longNames || pTabList->nSrc>1 ){
  3197         -              Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0);
         3171  +              Expr *pLeft;
         3172  +              pLeft = sqlite3Expr(db, TK_ID, zTabName);
  3198   3173                 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
  3199         -              if( pExpr==0 ) break;
  3200         -              setToken(&pLeft->token, zTabName);
  3201         -              setToken(&pExpr->span, 
  3202         -                  sqlite3MPrintf(db, "%s.%s", zTabName, zName));
  3203         -              pExpr->span.dyn = 1;
  3204         -              pExpr->token.z = 0;
  3205         -              pExpr->token.n = 0;
  3206         -              pExpr->token.dyn = 0;
         3174  +              if( longNames ){
         3175  +                zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
         3176  +                zToFree = zColname;
         3177  +              }
  3207   3178               }else{
  3208   3179                 pExpr = pRight;
  3209         -              pExpr->span = pExpr->token;
  3210         -              pExpr->span.dyn = 0;
  3211   3180               }
  3212         -            if( longNames ){
  3213         -              pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pExpr->span);
  3214         -            }else{
  3215         -              pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pRight->token);
  3216         -            }
         3181  +            pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
         3182  +            sColname.z = zColname;
         3183  +            sColname.n = sqlite3Strlen30(zColname);
         3184  +            sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
         3185  +            sqlite3DbFree(db, zToFree);
  3217   3186             }
  3218   3187           }
  3219   3188           if( !tableSeen ){
  3220   3189             if( zTName ){
  3221   3190               sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
  3222   3191             }else{
  3223   3192               sqlite3ErrorMsg(pParse, "no tables specified");
  3224   3193             }
  3225   3194           }
  3226         -        sqlite3DbFree(db, zTName);
  3227   3195         }
  3228   3196       }
  3229   3197       sqlite3ExprListDelete(db, pEList);
  3230   3198       p->pEList = pNew;
  3231   3199     }
  3232   3200   #if SQLITE_MAX_COLUMN
  3233   3201     if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
................................................................................
  4171   4139   ** during the execution of complex SELECT statements.
  4172   4140   **
  4173   4141   ** These routine are not called anywhere from within the normal
  4174   4142   ** code base.  Then are intended to be called from within the debugger
  4175   4143   ** or from temporary "printf" statements inserted for debugging.
  4176   4144   */
  4177   4145   void sqlite3PrintExpr(Expr *p){
  4178         -  if( p->token.z && p->token.n>0 ){
  4179         -    sqlite3DebugPrintf("(%.*s", p->token.n, p->token.z);
         4146  +  if( p->zToken ){
         4147  +    sqlite3DebugPrintf("(%s", p->zToken);
  4180   4148     }else{
  4181   4149       sqlite3DebugPrintf("(%d", p->op);
  4182   4150     }
  4183   4151     if( p->pLeft ){
  4184   4152       sqlite3DebugPrintf(" ");
  4185   4153       sqlite3PrintExpr(p->pLeft);
  4186   4154     }

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.875 2009/05/20 02:40:46 drh Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.876 2009/05/27 10:31:29 drh Exp $
    15     15   */
    16     16   #ifndef _SQLITEINT_H_
    17     17   #define _SQLITEINT_H_
    18     18   
    19     19   /*
    20     20   ** Include the configuration header output by 'configure' if we're using the
    21     21   ** autoconf-based build
................................................................................
   575    575   typedef struct RowSet RowSet;
   576    576   typedef struct CollSeq CollSeq;
   577    577   typedef struct Column Column;
   578    578   typedef struct Db Db;
   579    579   typedef struct Schema Schema;
   580    580   typedef struct Expr Expr;
   581    581   typedef struct ExprList ExprList;
          582  +typedef struct ExprSpan ExprSpan;
   582    583   typedef struct FKey FKey;
   583    584   typedef struct FuncDef FuncDef;
   584    585   typedef struct FuncDefHash FuncDefHash;
   585    586   typedef struct IdList IdList;
   586    587   typedef struct Index Index;
   587    588   typedef struct KeyClass KeyClass;
   588    589   typedef struct KeyInfo KeyInfo;
................................................................................
  1011   1012   /*
  1012   1013   ** information about each column of an SQL table is held in an instance
  1013   1014   ** of this structure.
  1014   1015   */
  1015   1016   struct Column {
  1016   1017     char *zName;     /* Name of this column */
  1017   1018     Expr *pDflt;     /* Default value of this column */
         1019  +  char *zDflt;     /* Original text of the default value */
  1018   1020     char *zType;     /* Data type for this column */
  1019   1021     char *zColl;     /* Collating sequence.  If NULL, use the default */
  1020   1022     u8 notNull;      /* True if there is a NOT NULL constraint */
  1021   1023     u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
  1022   1024     char affinity;   /* One of the SQLITE_AFF_... values */
  1023   1025   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1024   1026     u8 isHidden;     /* True if this column is 'hidden' */
................................................................................
  1356   1358   ** this structure.  Tokens are also used as part of an expression.
  1357   1359   **
  1358   1360   ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
  1359   1361   ** may contain random values.  Do not make any assumptions about Token.dyn
  1360   1362   ** and Token.n when Token.z==0.
  1361   1363   */
  1362   1364   struct Token {
  1363         -  const unsigned char *z; /* Text of the token.  Not NULL-terminated! */
  1364         -  unsigned dyn    : 1;    /* True for malloced memory, false for static */
  1365         -  unsigned quoted : 1;    /* True if token still has its quotes */
  1366         -  unsigned n      : 30;   /* Number of characters in this token */
         1365  +  const char *z;     /* Text of the token.  Not NULL-terminated! */
         1366  +  unsigned int n;    /* Number of characters in this token */
  1367   1367   };
  1368   1368   
  1369   1369   /*
  1370   1370   ** An instance of this structure contains information needed to generate
  1371   1371   ** code for a SELECT that contains aggregate functions.
  1372   1372   **
  1373   1373   ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
................................................................................
  1460   1460   **
  1461   1461   ** ALLOCATION NOTES:
  1462   1462   **
  1463   1463   ** Expr objects can use a lot of memory space in database schema.  To
  1464   1464   ** help reduce memory requirements, sometimes an Expr object will be
  1465   1465   ** truncated.  And to reduce the number of memory allocations, sometimes
  1466   1466   ** two or more Expr objects will be stored in a single memory allocation,
  1467         -** together with Expr.token and/or Expr.span strings.
         1467  +** together with Expr.token strings.
  1468   1468   **
  1469         -** If the EP_Reduced, EP_SpanToken, and EP_TokenOnly flags are set when
         1469  +** If the EP_Reduced and EP_TokenOnly flags are set when
  1470   1470   ** an Expr object is truncated.  When EP_Reduced is set, then all
  1471   1471   ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
  1472   1472   ** are contained within the same memory allocation.  Note, however, that
  1473   1473   ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  1474   1474   ** allocated, regardless of whether or not EP_Reduced is set.
  1475   1475   */
  1476   1476   struct Expr {
  1477   1477     u8 op;                 /* Operation performed by this node */
  1478   1478     char affinity;         /* The affinity of the column or 0 if not a column */
  1479         -  VVA_ONLY(u8 vvaFlags;) /* Flags used for VV&A only.  EVVA_* below. */
  1480   1479     u16 flags;             /* Various flags.  EP_* See below */
  1481         -  Token token;           /* An operand token */
         1480  +  char *zToken;          /* Token value. Zero terminated and dequoted */
  1482   1481   
  1483   1482     /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  1484         -  ** space is allocated for the fields below this point. An attempt to
  1485         -  ** access them will result in a segfault or malfunction. 
  1486         -  *********************************************************************/
  1487         -
  1488         -  Token span;            /* Complete text of the expression */
  1489         -
  1490         -  /* If the EP_SpanToken flag is set in the Expr.flags mask, then no
  1491   1483     ** space is allocated for the fields below this point. An attempt to
  1492   1484     ** access them will result in a segfault or malfunction. 
  1493   1485     *********************************************************************/
  1494   1486   
  1495   1487     Expr *pLeft;           /* Left subnode */
  1496   1488     Expr *pRight;          /* Right subnode */
  1497   1489     union {
................................................................................
  1501   1493     CollSeq *pColl;        /* The collation type of the column or 0 */
  1502   1494   
  1503   1495     /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  1504   1496     ** space is allocated for the fields below this point. An attempt to
  1505   1497     ** access them will result in a segfault or malfunction.
  1506   1498     *********************************************************************/
  1507   1499   
  1508         -  int iTable, iColumn;   /* When op==TK_COLUMN, then this expr node means the
  1509         -                         ** iColumn-th field of the iTable-th table. */
         1500  +  int iTable;            /* TK_COLUMN: cursor number of table holding column
         1501  +                         ** TK_REGISTER: register number 
         1502  +                         ** EP_IntValue: integer value */
         1503  +  i16 iColumn;           /* TK_COLUMN: column index.  -1 for rowid */
         1504  +  i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
         1505  +  i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
         1506  +  u16 flags2;            /* Second set of flags.  EP2_... */
  1510   1507     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  1511         -  int iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  1512         -  int iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
  1513   1508     Table *pTab;           /* Table for TK_COLUMN expressions. */
  1514   1509   #if SQLITE_MAX_EXPR_DEPTH>0
  1515   1510     int nHeight;           /* Height of the tree headed by this node */
  1516   1511   #endif
  1517   1512   };
  1518   1513   
  1519   1514   /*
................................................................................
  1531   1526   #define EP_AnyAff     0x0200  /* Can take a cached column of any affinity */
  1532   1527   #define EP_FixedDest  0x0400  /* Result needed in a specific register */
  1533   1528   #define EP_IntValue   0x0800  /* Integer value contained in iTable */
  1534   1529   #define EP_xIsSelect  0x1000  /* x.pSelect is valid (otherwise x.pList is) */
  1535   1530   
  1536   1531   #define EP_Reduced    0x2000  /* Expr struct is EXPR_REDUCEDSIZE bytes only */
  1537   1532   #define EP_TokenOnly  0x4000  /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
  1538         -#define EP_SpanToken  0x8000  /* Expr size is EXPR_SPANTOKENSIZE bytes */
         1533  +#define EP_Static     0x8000  /* Held in memory not obtained from malloc() */
  1539   1534   
  1540   1535   /*
  1541         -** The following are the meanings of bits in the Expr.vvaFlags field.
  1542         -** This information is only used when SQLite is compiled with
  1543         -** SQLITE_DEBUG defined.
         1536  +** The following are the meanings of bits in the Expr.flags2 field.
  1544   1537   */
  1545         -#ifndef NDEBUG
  1546         -#define EVVA_ReadOnlyToken  0x01  /* Expr.token.z is read-only */
  1547         -#endif
         1538  +#define EP2_FreeToken 0x0001  /* Need to call sqlite3DbFree() on Expr.zToken */
  1548   1539   
  1549   1540   /*
  1550   1541   ** These macros can be used to test, set, or clear bits in the 
  1551   1542   ** Expr.flags field.
  1552   1543   */
  1553   1544   #define ExprHasProperty(E,P)     (((E)->flags&(P))==(P))
  1554   1545   #define ExprHasAnyProperty(E,P)  (((E)->flags&(P))!=0)
................................................................................
  1558   1549   /*
  1559   1550   ** Macros to determine the number of bytes required by a normal Expr 
  1560   1551   ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags 
  1561   1552   ** and an Expr struct with the EP_TokenOnly flag set.
  1562   1553   */
  1563   1554   #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
  1564   1555   #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
  1565         -#define EXPR_SPANTOKENSIZE      offsetof(Expr,pLeft)   /* Fewer features */
  1566         -#define EXPR_TOKENONLYSIZE      offsetof(Expr,span)    /* Smallest possible */
         1556  +#define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
  1567   1557   
  1568   1558   /*
  1569   1559   ** Flags passed to the sqlite3ExprDup() function. See the header comment 
  1570   1560   ** above sqlite3ExprDup() for details.
  1571   1561   */
  1572   1562   #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
  1573         -#define EXPRDUP_SPAN           0x0002  /* Make a copy of Expr.span */
  1574   1563   
  1575   1564   /*
  1576   1565   ** A list of expressions.  Each expression may optionally have a
  1577   1566   ** name.  An expr/name combination can be used in several ways, such
  1578   1567   ** as the list of "expr AS ID" fields following a "SELECT" or in the
  1579   1568   ** list of "ID = expr" items in an UPDATE.  A list of expressions can
  1580   1569   ** also be used as the argument to a function, in which case the a.zName
................................................................................
  1583   1572   struct ExprList {
  1584   1573     int nExpr;             /* Number of expressions on the list */
  1585   1574     int nAlloc;            /* Number of entries allocated below */
  1586   1575     int iECursor;          /* VDBE Cursor associated with this ExprList */
  1587   1576     struct ExprList_item {
  1588   1577       Expr *pExpr;           /* The list of expressions */
  1589   1578       char *zName;           /* Token associated with this expression */
         1579  +    char *zSpan;           /* Original text of the expression */
  1590   1580       u8 sortOrder;          /* 1 for DESC or 0 for ASC */
  1591   1581       u8 done;               /* A flag to indicate when processing is finished */
  1592   1582       u16 iCol;              /* For ORDER BY, column number in result set */
  1593   1583       u16 iAlias;            /* Index into Parse.aAlias[] for zName */
  1594   1584     } *a;                  /* One entry for each expression */
  1595   1585   };
         1586  +
         1587  +/*
         1588  +** An instance of this structure is used by the parser to record both
         1589  +** the parse tree for an expression and the span of input text for an
         1590  +** expression.
         1591  +*/
         1592  +struct ExprSpan {
         1593  +  Expr *pExpr;          /* The expression parse tree */
         1594  +  const char *zStart;   /* First character of input text */
         1595  +  const char *zEnd;     /* One character past the end of input text */
         1596  +};
  1596   1597   
  1597   1598   /*
  1598   1599   ** An instance of this structure can hold a simple list of identifiers,
  1599   1600   ** such as the list "a,b,c" in the following statements:
  1600   1601   **
  1601   1602   **      INSERT INTO t(a,b,c) VALUES ...;
  1602   1603   **      CREATE INDEX idx ON t(a,b,c);
................................................................................
  2040   2041     char *name;             /* The name of the trigger                        */
  2041   2042     char *table;            /* The table or view to which the trigger applies */
  2042   2043     u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
  2043   2044     u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
  2044   2045     Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
  2045   2046     IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
  2046   2047                                the <column-list> is stored here */
  2047         -  Token nameToken;        /* Token containing zName. Use during parsing only */
  2048   2048     Schema *pSchema;        /* Schema containing the trigger */
  2049   2049     Schema *pTabSchema;     /* Schema containing the table */
  2050   2050     TriggerStep *step_list; /* Link list of trigger program steps             */
  2051   2051     Trigger *pNext;         /* Next trigger associated with the table */
  2052   2052   };
  2053   2053   
  2054   2054   /*
................................................................................
  2074   2074    * "SELECT" statement. The meanings of the other members is determined by the 
  2075   2075    * value of "op" as follows:
  2076   2076    *
  2077   2077    * (op == TK_INSERT)
  2078   2078    * orconf    -> stores the ON CONFLICT algorithm
  2079   2079    * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
  2080   2080    *              this stores a pointer to the SELECT statement. Otherwise NULL.
  2081         - * target    -> A token holding the name of the table to insert into.
         2081  + * target    -> A token holding the quoted name of the table to insert into.
  2082   2082    * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
  2083   2083    *              this stores values to be inserted. Otherwise NULL.
  2084   2084    * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ... 
  2085   2085    *              statement, then this stores the column-names to be
  2086   2086    *              inserted into.
  2087   2087    *
  2088   2088    * (op == TK_DELETE)
  2089         - * target    -> A token holding the name of the table to delete from.
         2089  + * target    -> A token holding the quoted name of the table to delete from.
  2090   2090    * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
  2091   2091    *              Otherwise NULL.
  2092   2092    * 
  2093   2093    * (op == TK_UPDATE)
  2094         - * target    -> A token holding the name of the table to update rows of.
         2094  + * target    -> A token holding the quoted name of the table to update rows of.
  2095   2095    * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
  2096   2096    *              Otherwise NULL.
  2097   2097    * pExprList -> A list of the columns to update and the expressions to update
  2098   2098    *              them to. See sqlite3Update() documentation of "pChanges"
  2099   2099    *              argument.
  2100   2100    * 
  2101   2101    */
................................................................................
  2102   2102   struct TriggerStep {
  2103   2103     int op;              /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
  2104   2104     int orconf;          /* OE_Rollback etc. */
  2105   2105     Trigger *pTrig;      /* The trigger that this step is a part of */
  2106   2106   
  2107   2107     Select *pSelect;     /* Valid for SELECT and sometimes 
  2108   2108                             INSERT steps (when pExprList == 0) */
  2109         -  Token target;        /* Valid for DELETE, UPDATE, INSERT steps */
         2109  +  Token target;        /* Target table for DELETE, UPDATE, INSERT.  Quoted */
  2110   2110     Expr *pWhere;        /* Valid for DELETE, UPDATE steps */
  2111   2111     ExprList *pExprList; /* Valid for UPDATE statements and sometimes 
  2112   2112                              INSERT steps (when pSelect == 0)         */
  2113   2113     IdList *pIdList;     /* Valid for INSERT statements only */
  2114   2114     TriggerStep *pNext;  /* Next in the link-list */
  2115   2115     TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
  2116   2116   };
................................................................................
  2380   2380   int sqlite3KeywordCode(const unsigned char*, int);
  2381   2381   int sqlite3RunParser(Parse*, const char*, char **);
  2382   2382   void sqlite3FinishCoding(Parse*);
  2383   2383   int sqlite3GetTempReg(Parse*);
  2384   2384   void sqlite3ReleaseTempReg(Parse*,int);
  2385   2385   int sqlite3GetTempRange(Parse*,int);
  2386   2386   void sqlite3ReleaseTempRange(Parse*,int,int);
  2387         -Expr *sqlite3Expr(sqlite3*, int, Expr*, Expr*, const Token*);
         2387  +Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
         2388  +Expr *sqlite3Expr(sqlite3*,int,const char*);
         2389  +void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  2388   2390   Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
  2389   2391   Expr *sqlite3RegisterExpr(Parse*,Token*);
  2390   2392   Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
  2391         -void sqlite3ExprSpan(Expr*,Token*,Token*);
  2392   2393   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
  2393   2394   void sqlite3ExprAssignVarNumber(Parse*, Expr*);
  2394   2395   void sqlite3ExprClear(sqlite3*, Expr*);
  2395   2396   void sqlite3ExprDelete(sqlite3*, Expr*);
  2396         -ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*,Token*);
         2397  +ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
         2398  +void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
         2399  +void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
  2397   2400   void sqlite3ExprListDelete(sqlite3*, ExprList*);
  2398   2401   int sqlite3Init(sqlite3*, char**);
  2399   2402   int sqlite3InitCallback(void*, int, char**, char**);
  2400   2403   void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
  2401   2404   void sqlite3ResetInternalSchema(sqlite3*, int);
  2402   2405   void sqlite3BeginParse(Parse*,int);
  2403   2406   void sqlite3CommitInternalChanges(sqlite3*);
................................................................................
  2405   2408   void sqlite3OpenMasterTable(Parse *, int);
  2406   2409   void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
  2407   2410   void sqlite3AddColumn(Parse*,Token*);
  2408   2411   void sqlite3AddNotNull(Parse*, int);
  2409   2412   void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  2410   2413   void sqlite3AddCheckConstraint(Parse*, Expr*);
  2411   2414   void sqlite3AddColumnType(Parse*,Token*);
  2412         -void sqlite3AddDefaultValue(Parse*,Expr*);
         2415  +void sqlite3AddDefaultValue(Parse*,ExprSpan*);
  2413   2416   void sqlite3AddCollateType(Parse*, Token*);
  2414   2417   void sqlite3EndTable(Parse*,Token*,Token*,Select*);
  2415   2418   
  2416   2419   Bitvec *sqlite3BitvecCreate(u32);
  2417   2420   int sqlite3BitvecTest(Bitvec*, u32);
  2418   2421   int sqlite3BitvecSet(Bitvec*, u32);
  2419   2422   void sqlite3BitvecClear(Bitvec*, u32);
................................................................................
  2519   2522   int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
  2520   2523   void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
  2521   2524                                        int*,int,int,int,int,int*);
  2522   2525   void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int,int,int);
  2523   2526   int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
  2524   2527   void sqlite3BeginWriteOperation(Parse*, int, int);
  2525   2528   Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
  2526         -void sqlite3TokenCopy(sqlite3*,Token*,const Token*);
  2527   2529   ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
  2528   2530   SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
  2529   2531   IdList *sqlite3IdListDup(sqlite3*,IdList*);
  2530   2532   Select *sqlite3SelectDup(sqlite3*,Select*,int);
  2531   2533   void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  2532   2534   FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
  2533   2535   void sqlite3RegisterBuiltinFunctions(sqlite3*);
................................................................................
  2690   2692   int sqlite3ResolveExprNames(NameContext*, Expr*);
  2691   2693   void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
  2692   2694   int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
  2693   2695   void sqlite3ColumnDefault(Vdbe *, Table *, int);
  2694   2696   void sqlite3AlterFinishAddColumn(Parse *, Token *);
  2695   2697   void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
  2696   2698   CollSeq *sqlite3GetCollSeq(sqlite3*, CollSeq *, const char*);
  2697         -char sqlite3AffinityType(const Token*);
         2699  +char sqlite3AffinityType(const char*);
  2698   2700   void sqlite3Analyze(Parse*, Token*, Token*);
  2699   2701   int sqlite3InvokeBusyHandler(BusyHandler*);
  2700   2702   int sqlite3FindDb(sqlite3*, Token*);
  2701   2703   int sqlite3FindDbName(sqlite3 *, const char *);
  2702   2704   int sqlite3AnalysisLoad(sqlite3*,int iDB);
  2703   2705   void sqlite3DefaultRowEst(Index*);
  2704   2706   void sqlite3RegisterLikeFunctions(sqlite3*, int);

Changes to src/tokenize.c.

    11     11   *************************************************************************
    12     12   ** An tokenizer for SQL
    13     13   **
    14     14   ** This file contains C code that splits an SQL input string up into
    15     15   ** individual tokens and sends those tokens one-by-one over to the
    16     16   ** parser for analysis.
    17     17   **
    18         -** $Id: tokenize.c,v 1.156 2009/05/01 21:13:37 drh Exp $
           18  +** $Id: tokenize.c,v 1.157 2009/05/27 10:31:29 drh Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include <stdlib.h>
    22     22   
    23     23   /*
    24     24   ** The charMap() macro maps alphabetic characters into their
    25     25   ** lower-case ASCII equivalent.  On ASCII machines, this is just
................................................................................
   400    400     i = 0;
   401    401     assert( pzErrMsg!=0 );
   402    402     pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
   403    403     if( pEngine==0 ){
   404    404       db->mallocFailed = 1;
   405    405       return SQLITE_NOMEM;
   406    406     }
   407         -  assert( pParse->sLastToken.dyn==0 );
   408    407     assert( pParse->pNewTable==0 );
   409    408     assert( pParse->pNewTrigger==0 );
   410    409     assert( pParse->nVar==0 );
   411    410     assert( pParse->nVarExpr==0 );
   412    411     assert( pParse->nVarExprAlloc==0 );
   413    412     assert( pParse->apVarExpr==0 );
   414    413     enableLookaside = db->lookaside.bEnabled;
   415    414     if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
   416         -  pParse->sLastToken.quoted = 1;
   417    415     while( !db->mallocFailed && zSql[i]!=0 ){
   418    416       assert( i>=0 );
   419         -    pParse->sLastToken.z = (u8*)&zSql[i];
   420         -    assert( pParse->sLastToken.dyn==0 );
   421         -    assert( pParse->sLastToken.quoted );
          417  +    pParse->sLastToken.z = &zSql[i];
   422    418       pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
   423    419       i += pParse->sLastToken.n;
   424    420       if( i>mxSqlLen ){
   425    421         pParse->rc = SQLITE_TOOBIG;
   426    422         break;
   427    423       }
   428    424       switch( tokenType ){

Changes to src/trigger.c.

     6      6   **    May you do good and not evil.
     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   **
    12     12   **
    13         -** $Id: trigger.c,v 1.139 2009/05/09 00:18:38 drh Exp $
           13  +** $Id: trigger.c,v 1.140 2009/05/27 10:31:29 drh Exp $
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   
    17     17   #ifndef SQLITE_OMIT_TRIGGER
    18     18   /*
    19     19   ** Delete a linked list of TriggerStep structures.
    20     20   */
    21     21   void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
    22     22     while( pTriggerStep ){
    23     23       TriggerStep * pTmp = pTriggerStep;
    24     24       pTriggerStep = pTriggerStep->pNext;
    25     25   
    26         -    if( pTmp->target.dyn ) sqlite3DbFree(db, (char*)pTmp->target.z);
    27     26       sqlite3ExprDelete(db, pTmp->pWhere);
    28     27       sqlite3ExprListDelete(db, pTmp->pExprList);
    29     28       sqlite3SelectDelete(db, pTmp->pSelect);
    30     29       sqlite3IdListDelete(db, pTmp->pIdList);
    31     30   
    32     31       sqlite3DbFree(db, pTmp);
    33     32     }
................................................................................
   214    213     pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
   215    214     pTrigger->pSchema = db->aDb[iDb].pSchema;
   216    215     pTrigger->pTabSchema = pTab->pSchema;
   217    216     pTrigger->op = (u8)op;
   218    217     pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
   219    218     pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
   220    219     pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
   221         -  sqlite3TokenCopy(db, &pTrigger->nameToken, pName);
   222    220     assert( pParse->pNewTrigger==0 );
   223    221     pParse->pNewTrigger = pTrigger;
   224    222   
   225    223   trigger_cleanup:
   226    224     sqlite3DbFree(db, zName);
   227    225     sqlite3SrcListDelete(db, pTableName);
   228    226     sqlite3IdListDelete(db, pColumns);
................................................................................
   244    242     Token *pAll             /* Token that describes the complete CREATE TRIGGER */
   245    243   ){
   246    244     Trigger *pTrig = pParse->pNewTrigger;    /* Trigger being finished */
   247    245     char *zName;                             /* Name of trigger */
   248    246     sqlite3 *db = pParse->db;                /* The database */
   249    247     DbFixer sFix;
   250    248     int iDb;                                 /* Database containing the trigger */
          249  +  Token nameToken;           /* Trigger name for error reporting */
   251    250   
   252    251     pTrig = pParse->pNewTrigger;
   253    252     pParse->pNewTrigger = 0;
   254    253     if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
   255    254     zName = pTrig->name;
   256    255     iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
   257    256     pTrig->step_list = pStepList;
   258    257     while( pStepList ){
   259    258       pStepList->pTrig = pTrig;
   260    259       pStepList = pStepList->pNext;
   261    260     }
   262         -  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &pTrig->nameToken) 
          261  +  nameToken.z = pTrig->name;
          262  +  nameToken.n = sqlite3Strlen30(nameToken.z);
          263  +  if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) 
   263    264             && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){
   264    265       goto triggerfinish_cleanup;
   265    266     }
   266    267   
   267    268     /* if we are not initializing, and this trigger is not on a TEMP table, 
   268    269     ** build the sqlite_master entry
   269    270     */
................................................................................
   305    306   
   306    307   triggerfinish_cleanup:
   307    308     sqlite3DeleteTrigger(db, pTrig);
   308    309     assert( !pParse->pNewTrigger );
   309    310     sqlite3DeleteTriggerStep(db, pStepList);
   310    311   }
   311    312   
   312         -/*
   313         -** Make a copy of all components of the given trigger step.  This has
   314         -** the effect of copying all Expr.token.z values into memory obtained
   315         -** from sqlite3_malloc().  As initially created, the Expr.token.z values
   316         -** all point to the input string that was fed to the parser.  But that
   317         -** string is ephemeral - it will go away as soon as the sqlite3_exec()
   318         -** call that started the parser exits.  This routine makes a persistent
   319         -** copy of all the Expr.token.z strings so that the TriggerStep structure
   320         -** will be valid even after the sqlite3_exec() call returns.
   321         -*/
   322         -static void sqlitePersistTriggerStep(sqlite3 *db, TriggerStep *p){
   323         -  if( p->target.z ){
   324         -    p->target.z = (u8*)sqlite3DbStrNDup(db, (char*)p->target.z, p->target.n);
   325         -    p->target.dyn = 1;
   326         -  }
   327         -  if( p->pSelect ){
   328         -    Select *pNew = sqlite3SelectDup(db, p->pSelect, 1);
   329         -    sqlite3SelectDelete(db, p->pSelect);
   330         -    p->pSelect = pNew;
   331         -  }
   332         -  if( p->pWhere ){
   333         -    Expr *pNew = sqlite3ExprDup(db, p->pWhere, EXPRDUP_REDUCE);
   334         -    sqlite3ExprDelete(db, p->pWhere);
   335         -    p->pWhere = pNew;
   336         -  }
   337         -  if( p->pExprList ){
   338         -    ExprList *pNew = sqlite3ExprListDup(db, p->pExprList, 1);
   339         -    sqlite3ExprListDelete(db, p->pExprList);
   340         -    p->pExprList = pNew;
   341         -  }
   342         -  if( p->pIdList ){
   343         -    IdList *pNew = sqlite3IdListDup(db, p->pIdList);
   344         -    sqlite3IdListDelete(db, p->pIdList);
   345         -    p->pIdList = pNew;
   346         -  }
   347         -}
   348         -
   349    313   /*
   350    314   ** Turn a SELECT statement (that the pSelect parameter points to) into
   351    315   ** a trigger step.  Return a pointer to a TriggerStep structure.
   352    316   **
   353    317   ** The parser calls this routine when it finds a SELECT statement in
   354    318   ** body of a TRIGGER.  
   355    319   */
   356    320   TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
   357    321     TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
   358    322     if( pTriggerStep==0 ) {
   359    323       sqlite3SelectDelete(db, pSelect);
   360    324       return 0;
   361    325     }
   362         -
   363    326     pTriggerStep->op = TK_SELECT;
   364    327     pTriggerStep->pSelect = pSelect;
   365    328     pTriggerStep->orconf = OE_Default;
   366         -  sqlitePersistTriggerStep(db, pTriggerStep);
          329  +  return pTriggerStep;
          330  +}
   367    331   
          332  +/*
          333  +** Allocate space to hold a new trigger step.  The allocated space
          334  +** holds both the TriggerStep object and the TriggerStep.target.z string.
          335  +**
          336  +** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
          337  +*/
          338  +static TriggerStep *triggerStepAllocate(
          339  +  sqlite3 *db,                /* Database connection */
          340  +  int op,                     /* Trigger opcode */
          341  +  Token *pName                /* The target name */
          342  +){
          343  +  TriggerStep *pTriggerStep;
          344  +
          345  +  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
          346  +  if( pTriggerStep ){
          347  +    char *z = (char*)&pTriggerStep[1];
          348  +    memcpy(z, pName->z, pName->n);
          349  +    pTriggerStep->target.z = z;
          350  +    pTriggerStep->target.n = pName->n;
          351  +    pTriggerStep->op = op;
          352  +  }
   368    353     return pTriggerStep;
   369    354   }
   370    355   
   371    356   /*
   372    357   ** Build a trigger step out of an INSERT statement.  Return a pointer
   373    358   ** to the new trigger step.
   374    359   **
................................................................................
   384    369     int orconf          /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
   385    370   ){
   386    371     TriggerStep *pTriggerStep;
   387    372   
   388    373     assert(pEList == 0 || pSelect == 0);
   389    374     assert(pEList != 0 || pSelect != 0 || db->mallocFailed);
   390    375   
   391         -  pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
          376  +  pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
   392    377     if( pTriggerStep ){
   393         -    pTriggerStep->op = TK_INSERT;
   394    378       pTriggerStep->pSelect = pSelect;
   395         -    pTriggerStep->target  = *pTableName;
   396    379       pTriggerStep->pIdList = pColumn;
   397    380       pTriggerStep->pExprList = pEList;
   398    381       pTriggerStep->orconf = orconf;
   399         -    sqlitePersistTriggerStep(db, pTriggerStep);
   400    382     }else{
   401    383       sqlite3IdListDelete(db, pColumn);
   402    384       sqlite3ExprListDelete(db, pEList);
   403    385       sqlite3SelectDelete(db, pSelect);
   404    386     }
   405    387   
   406    388     return pTriggerStep;
................................................................................
   414    396   TriggerStep *sqlite3TriggerUpdateStep(
   415    397     sqlite3 *db,         /* The database connection */
   416    398     Token *pTableName,   /* Name of the table to be updated */
   417    399     ExprList *pEList,    /* The SET clause: list of column and new values */
   418    400     Expr *pWhere,        /* The WHERE clause */
   419    401     int orconf           /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
   420    402   ){
   421         -  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
          403  +  TriggerStep *pTriggerStep;
          404  +
          405  +  pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
   422    406     if( pTriggerStep==0 ){
   423    407        sqlite3ExprListDelete(db, pEList);
   424    408        sqlite3ExprDelete(db, pWhere);
   425    409        return 0;
   426    410     }
   427         -
   428         -  pTriggerStep->op = TK_UPDATE;
   429         -  pTriggerStep->target  = *pTableName;
   430    411     pTriggerStep->pExprList = pEList;
   431    412     pTriggerStep->pWhere = pWhere;
   432    413     pTriggerStep->orconf = orconf;
   433         -  sqlitePersistTriggerStep(db, pTriggerStep);
   434         -
   435    414     return pTriggerStep;
   436    415   }
   437    416   
   438    417   /*
   439    418   ** Construct a trigger step that implements a DELETE statement and return
   440    419   ** a pointer to that trigger step.  The parser calls this routine when it
   441    420   ** sees a DELETE statement inside the body of a CREATE TRIGGER.
   442    421   */
   443    422   TriggerStep *sqlite3TriggerDeleteStep(
   444    423     sqlite3 *db,            /* Database connection */
   445    424     Token *pTableName,      /* The table from which rows are deleted */
   446    425     Expr *pWhere            /* The WHERE clause */
   447    426   ){
   448         -  TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
          427  +  TriggerStep *pTriggerStep;
          428  +
          429  +  pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
   449    430     if( pTriggerStep==0 ){
   450    431       sqlite3ExprDelete(db, pWhere);
   451    432       return 0;
   452    433     }
   453         -
   454         -  pTriggerStep->op = TK_DELETE;
   455         -  pTriggerStep->target  = *pTableName;
   456    434     pTriggerStep->pWhere = pWhere;
   457    435     pTriggerStep->orconf = OE_Default;
   458         -  sqlitePersistTriggerStep(db, pTriggerStep);
   459    436   
   460    437     return pTriggerStep;
   461    438   }
   462    439   
   463    440   /* 
   464    441   ** Recursively delete a Trigger structure
   465    442   */
................................................................................
   466    443   void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
   467    444     if( pTrigger==0 ) return;
   468    445     sqlite3DeleteTriggerStep(db, pTrigger->step_list);
   469    446     sqlite3DbFree(db, pTrigger->name);
   470    447     sqlite3DbFree(db, pTrigger->table);
   471    448     sqlite3ExprDelete(db, pTrigger->pWhen);
   472    449     sqlite3IdListDelete(db, pTrigger->pColumns);
   473         -  assert( pTrigger->nameToken.dyn );
   474         -  sqlite3DbFree(db, (char*)pTrigger->nameToken.z);
   475    450     sqlite3DbFree(db, pTrigger);
   476    451   }
   477    452   
   478    453   /*
   479    454   ** This function is called to drop a trigger from the database schema. 
   480    455   **
   481    456   ** This may be called directly from the parser and therefore identifies
................................................................................
   665    640     Parse *pParse,       /* The parsing context */
   666    641     TriggerStep *pStep   /* The trigger containing the target token */
   667    642   ){
   668    643     Token sDb;           /* Dummy database name token */
   669    644     int iDb;             /* Index of the database to use */
   670    645     SrcList *pSrc;       /* SrcList to be returned */
   671    646   
   672         -  iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
   673         -  if( iDb==0 || iDb>=2 ){
   674         -    assert( iDb<pParse->db->nDb );
   675         -    sDb.z = (u8*)pParse->db->aDb[iDb].zName;
   676         -    sDb.n = sqlite3Strlen30((char*)sDb.z);
   677         -    sDb.quoted = 0;
   678         -    pSrc = sqlite3SrcListAppend(pParse->db, 0, &sDb, &pStep->target);
   679         -  } else {
   680         -    pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
          647  +  pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
          648  +  if( pSrc ){
          649  +    assert( pSrc->nSrc>0 );
          650  +    assert( pSrc->a!=0 );
          651  +    iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
          652  +    if( iDb==0 || iDb>=2 ){
          653  +      sqlite3 *db = pParse->db;
          654  +      assert( iDb<pParse->db->nDb );
          655  +      pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
          656  +    }
   681    657     }
   682    658     return pSrc;
   683    659   }
   684    660   
   685    661   /*
   686    662   ** Generate VDBE code for zero or more statements inside the body of a
   687    663   ** trigger.  

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.200 2009/05/05 15:46:10 drh Exp $
           15  +** $Id: update.c,v 1.201 2009/05/27 10:31:29 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   #ifndef SQLITE_OMIT_VIRTUALTABLE
    20     20   /* Forward declaration */
    21     21   static void updateVirtualTable(
    22     22     Parse *pParse,       /* The parsing context */
................................................................................
   626    626     const char *pVtab = (const char*)pTab->pVtab;
   627    627     SelectDest dest;
   628    628   
   629    629     /* Construct the SELECT statement that will find the new values for
   630    630     ** all updated rows. 
   631    631     */
   632    632     pEList = sqlite3ExprListAppend(pParse, 0, 
   633         -                                 sqlite3CreateIdExpr(pParse, "_rowid_"), 0);
          633  +                                 sqlite3CreateIdExpr(pParse, "_rowid_"));
   634    634     if( pRowid ){
   635    635       pEList = sqlite3ExprListAppend(pParse, pEList,
   636         -                                   sqlite3ExprDup(db, pRowid, 0), 0);
          636  +                                   sqlite3ExprDup(db, pRowid, 0));
   637    637     }
   638    638     assert( pTab->iPKey<0 );
   639    639     for(i=0; i<pTab->nCol; i++){
   640    640       if( aXRef[i]>=0 ){
   641    641         pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
   642    642       }else{
   643    643         pExpr = sqlite3CreateIdExpr(pParse, pTab->aCol[i].zName);
   644    644       }
   645         -    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr, 0);
          645  +    pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
   646    646     }
   647    647     pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
   648    648     
   649    649     /* Create the ephemeral table into which the update results will
   650    650     ** be stored.
   651    651     */
   652    652     assert( v );

Changes to src/util.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Utility functions used throughout sqlite.
    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17         -** $Id: util.c,v 1.254 2009/05/06 19:03:14 drh Exp $
           17  +** $Id: util.c,v 1.255 2009/05/27 10:31:29 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include <stdarg.h>
    21     21   #ifdef SQLITE_HAVE_ISNAN
    22     22   # include <math.h>
    23     23   #endif
    24     24   
................................................................................
   104    104   **
   105    105   ** The value returned will never be negative.  Nor will it ever be greater
   106    106   ** than the actual length of the string.  For very long strings (greater
   107    107   ** than 1GiB) the value returned might be less than the true string length.
   108    108   */
   109    109   int sqlite3Strlen30(const char *z){
   110    110     const char *z2 = z;
          111  +  if( z==0 ) return 0;
   111    112     while( *z2 ){ z2++; }
   112    113     return 0x3fffffff & (int)(z2 - z);
   113    114   }
   114    115   
   115    116   /*
   116    117   ** Set the most recent error code and error string for the sqlite
   117    118   ** handle "db". The error code is set to "err_code".

Changes to src/vdbemem.c.

    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains code use to manipulate "Mem" structure.  A "Mem"
    14     14   ** stores a single value in the VDBE.  Mem is an opaque structure visible
    15     15   ** only within the VDBE.  Interface routines refer to a Mem using the
    16     16   ** name sqlite_value
    17     17   **
    18         -** $Id: vdbemem.c,v 1.144 2009/05/05 12:54:50 drh Exp $
           18  +** $Id: vdbemem.c,v 1.145 2009/05/27 10:31:29 drh Exp $
    19     19   */
    20     20   #include "sqliteInt.h"
    21     21   #include "vdbeInt.h"
    22     22   
    23     23   /*
    24     24   ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
    25     25   ** P if required.
................................................................................
   970    970     if( !pExpr ){
   971    971       *ppVal = 0;
   972    972       return SQLITE_OK;
   973    973     }
   974    974     op = pExpr->op;
   975    975   
   976    976     if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
   977         -    zVal = sqlite3DbStrNDup(db, (char*)pExpr->token.z, pExpr->token.n);
          977  +    zVal = sqlite3DbStrDup(db, pExpr->zToken);
   978    978       pVal = sqlite3ValueNew(db);
   979    979       if( !zVal || !pVal ) goto no_mem;
   980    980       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
   981    981       if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
   982    982         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
   983    983       }else{
   984    984         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
................................................................................
   992    992         /* (double)-1 In case of SQLITE_OMIT_FLOATING_POINT... */
   993    993         pVal->r = (double)-1 * pVal->r;
   994    994       }
   995    995     }
   996    996   #ifndef SQLITE_OMIT_BLOB_LITERAL
   997    997     else if( op==TK_BLOB ){
   998    998       int nVal;
   999         -    assert( pExpr->token.n>=3 );
  1000         -    assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
  1001         -    assert( pExpr->token.z[1]=='\'' );
  1002         -    assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
          999  +    assert( pExpr->zToken[0]=='x' || pExpr->zToken[0]=='X' );
         1000  +    assert( pExpr->zToken[1]=='\'' );
  1003   1001       pVal = sqlite3ValueNew(db);
  1004   1002       if( !pVal ) goto no_mem;
  1005         -    nVal = pExpr->token.n - 3;
  1006         -    zVal = (char*)pExpr->token.z + 2;
         1003  +    zVal = &pExpr->zToken[2];
         1004  +    nVal = sqlite3Strlen30(zVal)-1;
         1005  +    assert( zVal[nVal]=='\'' );
  1007   1006       sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
  1008   1007                            0, SQLITE_DYNAMIC);
  1009   1008     }
  1010   1009   #endif
  1011   1010   
  1012   1011     *ppVal = pVal;
  1013   1012     return SQLITE_OK;

Changes to src/walker.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 walking the parser tree for
    13     13   ** an SQL statement.
    14     14   **
    15         -** $Id: walker.c,v 1.4 2009/04/08 13:51:52 drh Exp $
           15  +** $Id: walker.c,v 1.5 2009/05/27 10:31:29 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <stdlib.h>
    19     19   #include <string.h>
    20     20   
    21     21   
    22     22   /*
................................................................................
    38     38   ** The return value from this routine is WRC_Abort to abandon the tree walk
    39     39   ** and WRC_Continue to continue.
    40     40   */
    41     41   int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
    42     42     int rc;
    43     43     if( pExpr==0 ) return WRC_Continue;
    44     44     testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
    45         -  testcase( ExprHasProperty(pExpr, EP_SpanToken) );
    46     45     testcase( ExprHasProperty(pExpr, EP_Reduced) );
    47     46     rc = pWalker->xExprCallback(pWalker, pExpr);
    48     47     if( rc==WRC_Continue
    49         -              && !ExprHasAnyProperty(pExpr,EP_TokenOnly|EP_SpanToken) ){
           48  +              && !ExprHasAnyProperty(pExpr,EP_TokenOnly) ){
    50     49       if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
    51     50       if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
    52     51       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
    53     52         if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
    54     53       }else{
    55     54         if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
    56     55       }

Changes to src/where.c.

    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.  This module is responsible for
    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   **
    19         -** $Id: where.c,v 1.397 2009/05/22 15:43:27 danielk1977 Exp $
           19  +** $Id: where.c,v 1.398 2009/05/27 10:31:29 drh Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   
    23     23   /*
    24     24   ** Trace output macros
    25     25   */
    26     26   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
................................................................................
   624    624     int *pisComplete, /* True if the only wildcard is % in the last character */
   625    625     int *pnoCase      /* True if uppercase is equivalent to lowercase */
   626    626   ){
   627    627     const char *z;             /* String on RHS of LIKE operator */
   628    628     Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
   629    629     ExprList *pList;           /* List of operands to the LIKE operator */
   630    630     int c;                     /* One character in z[] */
   631         -  int n;                     /* Length of string z[] */
   632    631     int cnt;                   /* Number of non-wildcard prefix characters */
   633    632     char wc[3];                /* Wildcard characters */
   634    633     CollSeq *pColl;            /* Collating sequence for LHS */
   635    634     sqlite3 *db = pParse->db;  /* Database connection */
   636    635   
   637    636     if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
   638    637       return 0;
................................................................................
   655    654       /* No collation is defined for the ROWID.  Use the default. */
   656    655       pColl = db->pDfltColl;
   657    656     }
   658    657     if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) &&
   659    658         (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){
   660    659       return 0;
   661    660     }
   662         -  z = (const char*)pRight->token.z;
          661  +  z = pRight->zToken;
   663    662     cnt = 0;
   664    663     if( z ){
   665         -    n = pRight->token.n;
   666         -    while( cnt<n && (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
          664  +    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
   667    665         cnt++;
   668    666       }
   669    667     }
   670         -  if( cnt==0 || 255==(u8)z[cnt-1] ){
          668  +  if( cnt==0 || c==0 || 255==(u8)z[cnt-1] ){
   671    669       return 0;
   672    670     }
   673    671     *pisComplete = z[cnt]==wc[0] && z[cnt+1]==0;
   674    672     *pnPattern = cnt;
   675    673     return 1;
   676    674   }
   677    675   #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
................................................................................
   689    687     Expr *pExpr      /* Test this expression */
   690    688   ){
   691    689     ExprList *pList;
   692    690   
   693    691     if( pExpr->op!=TK_FUNCTION ){
   694    692       return 0;
   695    693     }
   696         -  if( pExpr->token.n!=5 ||
   697         -       sqlite3StrNICmp((const char*)pExpr->token.z,"match",5)!=0 ){
          694  +  if( sqlite3StrICmp(pExpr->zToken,"match")!=0 ){
   698    695       return 0;
   699    696     }
   700    697     pList = pExpr->x.pList;
   701    698     if( pList->nExpr!=2 ){
   702    699       return 0;
   703    700     }
   704    701     if( pList->a[1].pExpr->op != TK_COLUMN ){
................................................................................
   959    956   
   960    957         for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
   961    958           if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
   962    959           assert( pOrTerm->eOperator==WO_EQ );
   963    960           assert( pOrTerm->leftCursor==iCursor );
   964    961           assert( pOrTerm->u.leftColumn==iColumn );
   965    962           pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
   966         -        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup, 0);
          963  +        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
   967    964           pLeft = pOrTerm->pExpr->pLeft;
   968    965         }
   969    966         assert( pLeft!=0 );
   970    967         pDup = sqlite3ExprDup(db, pLeft, 0);
   971         -      pNew = sqlite3Expr(db, TK_IN, pDup, 0, 0);
          968  +      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
   972    969         if( pNew ){
   973    970           int idxNew;
   974    971           transferJoinMarkings(pNew, pExpr);
   975    972           assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   976    973           pNew->x.pList = pList;
   977    974           idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
   978    975           testcase( idxNew==0 );
................................................................................
  1117   1114       int i;
  1118   1115       static const u8 ops[] = {TK_GE, TK_LE};
  1119   1116       assert( pList!=0 );
  1120   1117       assert( pList->nExpr==2 );
  1121   1118       for(i=0; i<2; i++){
  1122   1119         Expr *pNewExpr;
  1123   1120         int idxNew;
  1124         -      pNewExpr = sqlite3Expr(db, ops[i], sqlite3ExprDup(db, pExpr->pLeft, 0),
         1121  +      pNewExpr = sqlite3PExpr(pParse, ops[i], 
         1122  +                             sqlite3ExprDup(db, pExpr->pLeft, 0),
  1125   1123                                sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
  1126   1124         idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1127   1125         testcase( idxNew==0 );
  1128   1126         exprAnalyze(pSrc, pWC, idxNew);
  1129   1127         pTerm = &pWC->a[idxTerm];
  1130   1128         pWC->a[idxNew].iParent = idxTerm;
  1131   1129       }
................................................................................
  1159   1157       Expr *pLeft, *pRight;
  1160   1158       Expr *pStr1, *pStr2;
  1161   1159       Expr *pNewExpr1, *pNewExpr2;
  1162   1160       int idxNew1, idxNew2;
  1163   1161   
  1164   1162       pLeft = pExpr->x.pList->a[1].pExpr;
  1165   1163       pRight = pExpr->x.pList->a[0].pExpr;
  1166         -    pStr1 = sqlite3PExpr(pParse, TK_STRING, 0, 0, 0);
  1167         -    if( pStr1 ){
  1168         -      sqlite3TokenCopy(db, &pStr1->token, &pRight->token);
  1169         -      pStr1->token.n = nPattern;
  1170         -    }
         1164  +    pStr1 = sqlite3Expr(db, TK_STRING, pRight->zToken);
         1165  +    if( pStr1 ) pStr1->zToken[nPattern] = 0;
  1171   1166       pStr2 = sqlite3ExprDup(db, pStr1, 0);
  1172   1167       if( !db->mallocFailed ){
  1173   1168         u8 c, *pC;
  1174         -      /* assert( pStr2->token.dyn ); */
  1175         -      pC = (u8*)&pStr2->token.z[nPattern-1];
         1169  +      pC = (u8*)&pStr2->zToken[nPattern-1];
  1176   1170         c = *pC;
  1177   1171         if( noCase ){
  1178   1172           if( c=='@' ) isComplete = 0;
  1179   1173           c = sqlite3UpperToLower[c];
  1180   1174         }
  1181   1175         *pC = c + 1;
  1182   1176       }
................................................................................
  1212   1206   
  1213   1207       pRight = pExpr->x.pList->a[0].pExpr;
  1214   1208       pLeft = pExpr->x.pList->a[1].pExpr;
  1215   1209       prereqExpr = exprTableUsage(pMaskSet, pRight);
  1216   1210       prereqColumn = exprTableUsage(pMaskSet, pLeft);
  1217   1211       if( (prereqExpr & prereqColumn)==0 ){
  1218   1212         Expr *pNewExpr;
  1219         -      pNewExpr = sqlite3Expr(db, TK_MATCH, 0, sqlite3ExprDup(db, pRight, 0), 0);
         1213  +      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
         1214  +                              0, sqlite3ExprDup(db, pRight, 0), 0);
  1220   1215         idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1221   1216         testcase( idxNew==0 );
  1222   1217         pNewTerm = &pWC->a[idxNew];
  1223   1218         pNewTerm->prereqRight = prereqExpr;
  1224   1219         pNewTerm->leftCursor = pLeft->iTable;
  1225   1220         pNewTerm->u.leftColumn = pLeft->iColumn;
  1226   1221         pNewTerm->eOperator = WO_MATCH;
................................................................................
  2292   2287       struct InLoop *pIn;
  2293   2288   
  2294   2289       assert( pX->op==TK_IN );
  2295   2290       iReg = iTarget;
  2296   2291       eType = sqlite3FindInIndex(pParse, pX, 0);
  2297   2292       iTab = pX->iTable;
  2298   2293       sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
  2299         -    VdbeComment((v, "%.*s", pX->span.n, pX->span.z));
  2300   2294       assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
  2301   2295       if( pLevel->u.in.nIn==0 ){
  2302   2296         pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  2303   2297       }
  2304   2298       pLevel->u.in.nIn++;
  2305   2299       pLevel->u.in.aInLoop =
  2306   2300          sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,

Changes to test/attach.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.  The
    12     12   # focus of this script is testing the ATTACH and DETACH commands
    13     13   # and related functionality.
    14     14   #
    15         -# $Id: attach.test,v 1.49 2008/07/12 14:52:20 drh Exp $
           15  +# $Id: attach.test,v 1.50 2009/05/27 10:31:30 drh Exp $
    16     16   #
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20     20   
    21     21   ifcapable !attach {
    22     22     finish_test
................................................................................
   752    752     do_test attach-7.1 {
   753    753       file delete -force test.db test.db-journal
   754    754       sqlite3 db test.db
   755    755       catchsql {
   756    756         DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY 
   757    757         REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL )
   758    758       }
   759         -  } {1 {invalid name: "RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY 
   760         -      REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL )"}}
          759  +  } {1 {invalid name: ""}}
   761    760   }
   762    761   
   763    762   # Create a malformed file (a file that is not a valid database)
   764    763   # and try to attach it
   765    764   #
   766    765   do_test attach-8.1 {
   767    766     set fd [open test2.db w]

Changes to test/select1.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 file is testing the SELECT statement.
    13     13   #
    14         -# $Id: select1.test,v 1.68 2009/04/23 14:58:40 danielk1977 Exp $
           14  +# $Id: select1.test,v 1.69 2009/05/27 10:31:30 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   # Try to select on a non-existant table.
    20     20   #
    21     21   do_test select1-1.1 {
................................................................................
   302    302   do_test select1-4.3 {
   303    303     set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1,f2)}} msg]
   304    304     lappend v $msg
   305    305   } {0 {11 33}}
   306    306   do_test select1-4.4 {
   307    307     set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1)}} msg]
   308    308     lappend v $msg
   309         -} {1 {misuse of aggregate: min(f1)}}
          309  +} {1 {misuse of aggregate: min()}}
   310    310   
   311    311   # The restriction not allowing constants in the ORDER BY clause
   312    312   # has been removed.  See ticket #1768
   313    313   #do_test select1-4.5 {
   314    314   #  catchsql {
   315    315   #    SELECT f1 FROM test1 ORDER BY 8.4;
   316    316   #  }

Changes to test/tkt1514.test.

    18     18   source $testdir/tester.tcl
    19     19   
    20     20   do_test tkt1514-1.1 {
    21     21     catchsql {
    22     22       CREATE TABLE t1(a,b);
    23     23       SELECT a FROM t1 WHERE max(b)<10 GROUP BY a;
    24     24     }
    25         -} {1 {misuse of aggregate: max(b)}}
           25  +} {1 {misuse of aggregate: max()}}
    26     26   
    27     27   finish_test

Changes to test/tkt3508.test.

     6      6   #    May you do good and not evil.
     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. 
    12     12   #
    13         -# $Id: tkt3508.test,v 1.3 2008/11/26 20:09:15 pweilbacher Exp $
           13  +# $Id: tkt3508.test,v 1.4 2009/05/27 10:31:30 drh Exp $
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   
    18     18   do_test tkt3508-1.1 {
    19     19     catchsql {
    20     20       CREATE TABLE modificationsTmp (
................................................................................
    29     29         MODIFICATION_TYPE VARCHAR(80),
    30     30         EXPERIMENT_TYPE VARCHAR(80),
    31     31         REFERENCE_ID VARCHAR(80)
    32     32       );
    33     33       select SUBSTRATE_HPRD_ID, count(substrate_refseq_id) as c
    34     34         from modificationsTmp where c > 1 group by SUBSTRATE_HPRD_ID;
    35     35     }
    36         -} {1 {misuse of aggregate: }}
           36  +} {1 {misuse of aggregate: count()}}
    37     37   
    38     38   finish_test